@humanu/orchestra 0.5.76 → 0.5.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/bin/{gw.js → orchestra-cli.js} +1 -1
  2. package/bin/orchestra.js +2 -2
  3. package/install.js +41 -42
  4. package/package.json +2 -3
  5. package/resources/api/git.sh +4 -444
  6. package/resources/api/tmux.sh +4 -2791
  7. package/resources/prebuilt/linux-x64/{gw-env-copy → env-copy} +0 -0
  8. package/resources/prebuilt/linux-x64/orchestra +0 -0
  9. package/resources/prebuilt/macos-arm64/{gw-env-copy → env-copy} +0 -0
  10. package/resources/prebuilt/macos-arm64/orchestra +0 -0
  11. package/resources/prebuilt/macos-intel/{gw-env-copy → env-copy} +0 -0
  12. package/resources/prebuilt/macos-intel/orchestra +0 -0
  13. package/resources/scripts/{gw.sh → orchestra-cli.sh} +14 -14
  14. package/resources/scripts/orchestra-local.sh +6 -6
  15. package/resources/scripts/{gwr.sh → orchestra.sh} +11 -55
  16. package/resources/scripts/{shell/bridge → server/services}/ai.sh +4 -4
  17. package/resources/scripts/{gw-bridge.sh → server/services/dispatch.sh} +62 -59
  18. package/resources/scripts/server/services/git/api.sh +447 -0
  19. package/resources/scripts/{shell/git/bridge_check_branch.sh → server/services/git/check_branch_api.sh} +1 -1
  20. package/resources/scripts/{shell/git/bridge_create_worktree.sh → server/services/git/create_worktree_api.sh} +3 -3
  21. package/resources/scripts/{shell/git/bridge_create_worktree_from_existing.sh → server/services/git/create_worktree_from_existing_api.sh} +2 -2
  22. package/resources/scripts/{shell/git/bridge_create_worktree_from_remote.sh → server/services/git/create_worktree_from_remote_api.sh} +2 -2
  23. package/resources/scripts/{shell/git/bridge_delete_branch_only.sh → server/services/git/delete_branch_only_api.sh} +1 -1
  24. package/resources/scripts/{shell/git/bridge_delete_worktree.sh → server/services/git/delete_worktree_api.sh} +2 -2
  25. package/resources/scripts/{shell/git/bridge_delete_worktree_only.sh → server/services/git/delete_worktree_only_api.sh} +1 -1
  26. package/resources/scripts/{shell/git/bridge_enhanced_git_status.sh → server/services/git/enhanced_git_status_api.sh} +3 -3
  27. package/resources/scripts/{shell/git/bridge_git_status.sh → server/services/git/git_status_api.sh} +2 -2
  28. package/resources/scripts/{shell/git/bridge_list_worktrees.sh → server/services/git/list_worktrees_api.sh} +2 -2
  29. package/resources/scripts/server/services/git/merge_api.sh +12 -0
  30. package/resources/scripts/{shell/git/bridge_merge_from_primary.sh → server/services/git/merge_from_primary_api.sh} +1 -1
  31. package/resources/scripts/{shell/git/bridge_merge_into_primary.sh → server/services/git/merge_into_primary_api.sh} +1 -1
  32. package/resources/scripts/{shell/git/bridge_primary_branch.sh → server/services/git/primary_branch_api.sh} +1 -1
  33. package/resources/scripts/{shell/git/bridge_rebase_from_primary.sh → server/services/git/rebase_from_primary_api.sh} +1 -1
  34. package/resources/scripts/server/services/git/repo_api.sh +12 -0
  35. package/resources/scripts/{shell/git/bridge_repo_info.sh → server/services/git/repo_info_api.sh} +1 -1
  36. package/resources/scripts/{shell/git/bridge_squash_into_primary.sh → server/services/git/squash_into_primary_api.sh} +1 -1
  37. package/resources/scripts/{shell/git/bridge_switch_worktree.sh → server/services/git/switch_worktree_api.sh} +2 -2
  38. package/resources/scripts/server/services/git/worktree_api.sh +17 -0
  39. package/resources/scripts/{shell/bridge/utils.sh → server/services/json.sh} +23 -23
  40. package/resources/scripts/{shell/bridge → server/services/session}/tmux.sh +14 -14
  41. package/resources/scripts/server/session/tmux_api.sh +2806 -0
  42. package/resources/scripts/services.sh +6 -0
  43. package/resources/scripts/shell/AGENTS.md +63 -74
  44. package/resources/scripts/shell/build/dependencies.sh +33 -0
  45. package/resources/scripts/shell/build/install.sh +7 -0
  46. package/resources/scripts/shell/build/load.sh +10 -0
  47. package/resources/scripts/shell/build/logging.sh +6 -0
  48. package/resources/scripts/shell/build/rust.sh +18 -0
  49. package/resources/scripts/shell/build/services.sh +17 -0
  50. package/resources/scripts/shell/build/{build_usage.sh → usage.sh} +6 -6
  51. package/resources/scripts/shell/cli_load.sh +9 -0
  52. package/resources/scripts/shell/{gw_env_copy.sh → env_copy.sh} +11 -11
  53. package/resources/scripts/shell/env_copy_command.sh +2 -2
  54. package/resources/scripts/shell/git/checkout_worktree.sh +4 -4
  55. package/resources/scripts/shell/git/create_worktree.sh +2 -2
  56. package/resources/scripts/shell/git/delete_worktree.sh +1 -1
  57. package/resources/scripts/shell/git/merge.sh +1 -1
  58. package/resources/scripts/shell/git/repo.sh +1 -1
  59. package/resources/scripts/shell/git/worktree.sh +1 -1
  60. package/resources/scripts/shell/gwr/check-updates.sh +1 -1
  61. package/resources/scripts/shell/gwr_binary.sh +4 -4
  62. package/resources/scripts/shell/gwr_load.sh +1 -1
  63. package/resources/scripts/shell/gwr_services.sh +10 -0
  64. package/resources/scripts/shell/gwr_usage.sh +10 -10
  65. package/resources/scripts/shell/orchestra-command-hook.sh +15 -15
  66. package/resources/scripts/shell/orchestra-local.sh +6 -6
  67. package/resources/scripts/shell/tmux/new_session_command.sh +1 -1
  68. package/bin/gwr.js +0 -10
  69. package/resources/scripts/shell/build/build_bridge.sh +0 -17
  70. package/resources/scripts/shell/build/build_dependencies.sh +0 -33
  71. package/resources/scripts/shell/build/build_install.sh +0 -7
  72. package/resources/scripts/shell/build/build_load.sh +0 -10
  73. package/resources/scripts/shell/build/build_logging.sh +0 -6
  74. package/resources/scripts/shell/build/build_rust.sh +0 -18
  75. package/resources/scripts/shell/git/bridge_merge.sh +0 -12
  76. package/resources/scripts/shell/git/bridge_repo.sh +0 -12
  77. package/resources/scripts/shell/git/bridge_worktree.sh +0 -17
  78. package/resources/scripts/shell/gw_legacy_wrappers.sh +0 -7
  79. package/resources/scripts/shell/gw_load.sh +0 -10
  80. package/resources/scripts/shell/gwr_bridge.sh +0 -10
  81. /package/resources/scripts/shell/{gw_debug.sh → cli_debug.sh} +0 -0
  82. /package/resources/scripts/shell/{gw_err.sh → cli_err.sh} +0 -0
  83. /package/resources/scripts/shell/{gw_have_cmd.sh → cli_have_cmd.sh} +0 -0
  84. /package/resources/scripts/shell/{gw_info.sh → cli_info.sh} +0 -0
@@ -0,0 +1,2806 @@
1
+ #!/bin/bash
2
+
3
+ ###############################################################################
4
+ # tmux.sh – Dedicated tmux session management API
5
+ # ---------------------------------------------------------------------------
6
+ # This script provides a clean API for all tmux session operations.
7
+ # It can be sourced by other modules that need tmux functionality.
8
+ ###############################################################################
9
+
10
+ # Ensure we have access to core utilities
11
+ if ! declare -f repo_root >/dev/null 2>&1 && ! declare -f git_repo_root >/dev/null 2>&1; then
12
+ echo "Error: tmux.sh must be sourced from orchestra-cli.sh or have core utilities available" >&2
13
+ return 1 2>/dev/null || exit 1
14
+ fi
15
+
16
+ # Use git_repo_root if available, otherwise fallback to repo_root
17
+ if declare -f git_repo_root >/dev/null 2>&1; then
18
+ repo_root() { git_repo_root; }
19
+ fi
20
+
21
+ # --------------------------- Tmux Core API ----------------------------------
22
+
23
+ # Session delimiter (hardcoded)
24
+ # Note: tmux session names cannot contain ':'; use a safe delimiter
25
+ ORCHESTRA_SESSION_DELIM="__"
26
+
27
+ # Absolute directory that contains this script when sourced.
28
+ _TMUX_API_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
29
+ _ORCHESTRA_ROOT_DIR="$(cd "$_TMUX_API_DIR/../.." && pwd -P)"
30
+
31
+ # Helper: return delimiter
32
+ _tmux_delim() { echo "$ORCHESTRA_SESSION_DELIM"; }
33
+
34
+ # Helper: sanitize tmux session name for filesystem usage
35
+ _orchestra_history_key() {
36
+ local key="$1"
37
+ key="${key//\//_}"
38
+ key=$(echo "$key" | tr '[:space:]' '_')
39
+ key=$(echo "$key" | tr -c '[:alnum:]_-' '_')
40
+ while [[ "$key" == *"__"* ]]; do
41
+ key="${key//__/_}"
42
+ done
43
+ key="${key##_}"
44
+ key="${key%%_}"
45
+ echo "$key"
46
+ }
47
+
48
+ _tmux_normalize_app_from_command() {
49
+ local cmd="$1"
50
+ cmd="${cmd#"${cmd%%[![:space:]]*}"}"
51
+ cmd="${cmd%"${cmd##*[![:space:]]}"}"
52
+ if [[ -z "$cmd" ]]; then
53
+ return
54
+ fi
55
+
56
+ local tokens=()
57
+ read -r -a tokens <<< "$cmd"
58
+ if [[ ${#tokens[@]} -eq 0 ]]; then
59
+ return
60
+ fi
61
+
62
+ local i=0
63
+ while [[ $i -lt ${#tokens[@]} ]]; do
64
+ local t="${tokens[$i]}"
65
+ if [[ "$t" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
66
+ ((i++))
67
+ continue
68
+ fi
69
+ case "$t" in
70
+ sudo|env|command|nohup|time)
71
+ ((i++))
72
+ continue
73
+ ;;
74
+ esac
75
+ break
76
+ done
77
+
78
+ if [[ $i -ge ${#tokens[@]} ]]; then
79
+ return
80
+ fi
81
+
82
+ local base="${tokens[$i]}"
83
+ base="${base##*/}"
84
+ base="$(printf '%s' "$base" | tr '[:upper:]' '[:lower:]' | tr -c 'a-z0-9._:-' '_')"
85
+ base="${base//__/_}"
86
+ base="${base##_}"
87
+ base="${base%%_}"
88
+
89
+ case "$base" in
90
+ python3|python3.*)
91
+ base="python"
92
+ ;;
93
+ pip3|pip3.*)
94
+ base="pip"
95
+ ;;
96
+ docker-compose|docker_compose)
97
+ base="docker_compose"
98
+ ;;
99
+ nvim|nvim.app)
100
+ base="nvim"
101
+ ;;
102
+ esac
103
+
104
+ case "$base" in
105
+ ""|bash|zsh|sh|fish|tmux|login|sudo|man|less|more|cat|tail|watch|source|export|set|alias|unalias|history|bindkey)
106
+ return
107
+ ;;
108
+ esac
109
+
110
+ printf '%s' "$base"
111
+ }
112
+
113
+ _tmux_truthy() {
114
+ case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in
115
+ 1|yes|true|on)
116
+ return 0
117
+ ;;
118
+ esac
119
+ return 1
120
+ }
121
+
122
+ _tmux_is_known_tui_app() {
123
+ local app
124
+ app="$(_tmux_normalize_app_from_command "$1")"
125
+ case "$app" in
126
+ opencode|claude|vim|nvim|vi|lazygit|gitui|tig|top|htop|btop|k9s|fzf|yazi|ranger|nnn|less|man)
127
+ return 0
128
+ ;;
129
+ esac
130
+ return 1
131
+ }
132
+
133
+ _tmux_is_tui_context() {
134
+ local pane_cmd="$1"
135
+ local alternate_on="$2"
136
+ local mouse_any_flag="$3"
137
+ local pane_mode="$4"
138
+ local window_name="$5"
139
+ local pane_title="$6"
140
+
141
+ if _tmux_truthy "$alternate_on" || _tmux_truthy "$mouse_any_flag"; then
142
+ return 0
143
+ fi
144
+
145
+ if [[ -n "$pane_mode" ]]; then
146
+ return 0
147
+ fi
148
+
149
+ if _tmux_is_known_tui_app "$pane_cmd"; then
150
+ return 0
151
+ fi
152
+
153
+ local combined
154
+ combined="$(printf '%s %s' "$window_name" "$pane_title" | tr '[:upper:]' '[:lower:]')"
155
+ case "$combined" in
156
+ *opencode*|*claude*|*vim*|*nvim*|*lazygit*|*gitui*|*tig*|*k9s*|*fzf*|*yazi*|*ranger*|*nnn*)
157
+ return 0
158
+ ;;
159
+ esac
160
+
161
+ return 1
162
+ }
163
+
164
+ # Helper: absolute path to the command hook script (if present)
165
+ _orchestra_command_hook() {
166
+ local hook=""
167
+
168
+ # 1) Explicit install root set by wrappers (Homebrew/NPM)
169
+ if [[ -n "${GW_ORCHESTRATOR_ROOT-}" ]]; then
170
+ hook="$GW_ORCHESTRATOR_ROOT/shell/orchestra-command-hook.sh"
171
+ if [[ -f "$hook" ]]; then
172
+ echo "$hook"
173
+ return
174
+ fi
175
+ fi
176
+
177
+ # 2) Resolve relative to the server/session implementation location
178
+ hook="$_ORCHESTRA_ROOT_DIR/shell/orchestra-command-hook.sh"
179
+ if [[ -f "$hook" ]]; then
180
+ echo "$hook"
181
+ return
182
+ fi
183
+
184
+ # 3) Fallback to repo root for legacy/dev flows
185
+ local root
186
+ root="$(repo_root)"
187
+ if [[ -n "$root" ]]; then
188
+ hook="$root/shell/orchestra-command-hook.sh"
189
+ if [[ -f "$hook" ]]; then
190
+ echo "$hook"
191
+ return
192
+ fi
193
+ fi
194
+
195
+ echo ""
196
+ }
197
+
198
+ _orchestra_services_script() {
199
+ echo "$_ORCHESTRA_ROOT_DIR/services.sh"
200
+ }
201
+
202
+ # Source the command hook inside a tmux session to enable command history logging
203
+ _tmux_source_command_hook() {
204
+ local session="$1"
205
+ local hook
206
+ hook="$(_orchestra_command_hook)"
207
+ if [[ -z "$hook" ]]; then
208
+ return
209
+ fi
210
+ if ! tmux_session_exists "$session"; then
211
+ return
212
+ fi
213
+ local services
214
+ services="$(_orchestra_services_script)"
215
+ local panes
216
+ panes=$(tmux list-panes -t "$session" -F '#{pane_id} #{pane_current_command}' 2>/dev/null || echo "")
217
+ if [[ -z "$panes" ]]; then
218
+ return
219
+ fi
220
+ while IFS= read -r line; do
221
+ [[ -z "$line" ]] && continue
222
+ local pane_id pane_cmd
223
+ pane_id="${line%% *}"
224
+ pane_cmd="${line#* }"
225
+ case "$pane_cmd" in
226
+ bash|zsh)
227
+ tmux send-keys -t "$pane_id" "export ORCHESTRA_SERVICES_PATH='$services'; . '$hook'" C-m 2>/dev/null || true
228
+ ;;
229
+ *)
230
+ ;;
231
+ esac
232
+ done <<< "$panes"
233
+ }
234
+
235
+ # Helper: orchestra prefix including delimiter
236
+ _tmux_orch_prefix() { printf "orchestra%s" "$( _tmux_delim )"; }
237
+
238
+ _tmux_session_registry_path() {
239
+ if [[ -n "${ORCHESTRA_SESSION_REGISTRY_PATH-}" ]]; then
240
+ printf '%s\n' "$ORCHESTRA_SESSION_REGISTRY_PATH"
241
+ return
242
+ fi
243
+ printf '%s\n' "$HOME/.orchestra/orchestra.sqlite3"
244
+ }
245
+
246
+ _tmux_shared_root_for_path() {
247
+ local path="$1"
248
+ local old_pwd="$PWD"
249
+ local root=""
250
+ if [[ -n "$path" && -d "$path" ]]; then
251
+ cd "$path" 2>/dev/null || return 1
252
+ root="$(git_shared_root 2>/dev/null || git_repo_root 2>/dev/null || true)"
253
+ cd "$old_pwd" 2>/dev/null || true
254
+ fi
255
+ [[ -n "$root" ]] || return 1
256
+ printf '%s\n' "$root"
257
+ }
258
+
259
+ _tmux_branch_for_path() {
260
+ local path="$1"
261
+ local old_pwd="$PWD"
262
+ local branch=""
263
+ if [[ -n "$path" && -d "$path" ]]; then
264
+ cd "$path" 2>/dev/null || return 1
265
+ branch="$(git_current_branch 2>/dev/null || true)"
266
+ cd "$old_pwd" 2>/dev/null || true
267
+ fi
268
+ [[ -n "$branch" ]] || return 1
269
+ printf '%s\n' "$branch"
270
+ }
271
+
272
+ _tmux_registry_upsert_session() {
273
+ local repo_root="$1"
274
+ local branch="$2"
275
+ local worktree_path="$3"
276
+ local tmux_name="$4"
277
+ local db_path
278
+ db_path="$(_tmux_session_registry_path)"
279
+
280
+ [[ -n "$repo_root" && -n "$branch" && -n "$worktree_path" && -n "$tmux_name" ]] || return 1
281
+ have_cmd python3 || return 1
282
+
283
+ python3 - "$db_path" "$repo_root" "$branch" "$worktree_path" "$tmux_name" <<'PY'
284
+ import os
285
+ import sqlite3
286
+ import sys
287
+ import time
288
+
289
+ db_path, repo_root, branch, worktree_path, tmux_name = sys.argv[1:6]
290
+ os.makedirs(os.path.dirname(db_path), exist_ok=True)
291
+ slug = branch.replace("/", "-")
292
+ now = int(time.time())
293
+
294
+ conn = sqlite3.connect(db_path)
295
+ conn.execute(
296
+ """
297
+ CREATE TABLE IF NOT EXISTS sessions (
298
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
299
+ tmux_name TEXT NOT NULL UNIQUE,
300
+ repo_root TEXT NOT NULL,
301
+ worktree_path TEXT NOT NULL,
302
+ branch TEXT NOT NULL,
303
+ slug TEXT NOT NULL,
304
+ display_name TEXT,
305
+ created_at INTEGER NOT NULL,
306
+ updated_at INTEGER NOT NULL,
307
+ last_seen_at INTEGER NOT NULL
308
+ )
309
+ """
310
+ )
311
+ conn.execute(
312
+ "CREATE INDEX IF NOT EXISTS idx_sessions_worktree ON sessions(repo_root, worktree_path)"
313
+ )
314
+ conn.execute("CREATE INDEX IF NOT EXISTS idx_sessions_slug ON sessions(repo_root, slug)")
315
+ conn.execute(
316
+ """
317
+ INSERT INTO sessions (
318
+ tmux_name, repo_root, worktree_path, branch, slug,
319
+ created_at, updated_at, last_seen_at
320
+ ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?6, ?6)
321
+ ON CONFLICT(tmux_name) DO UPDATE SET
322
+ repo_root = excluded.repo_root,
323
+ worktree_path = excluded.worktree_path,
324
+ branch = excluded.branch,
325
+ slug = excluded.slug,
326
+ updated_at = excluded.updated_at,
327
+ last_seen_at = excluded.last_seen_at
328
+ """,
329
+ (tmux_name, repo_root, worktree_path, branch, slug, now),
330
+ )
331
+ conn.commit()
332
+ PY
333
+ }
334
+
335
+ _tmux_registry_rename_session() {
336
+ local old_tmux_name="$1"
337
+ local new_tmux_name="$2"
338
+ local display_name="${3:-}"
339
+ local db_path
340
+ db_path="$(_tmux_session_registry_path)"
341
+
342
+ [[ -f "$db_path" ]] || return 0
343
+ have_cmd python3 || return 1
344
+
345
+ python3 - "$db_path" "$old_tmux_name" "$new_tmux_name" "$display_name" <<'PY'
346
+ import sqlite3
347
+ import sys
348
+ import time
349
+
350
+ db_path, old_tmux_name, new_tmux_name, display_name = sys.argv[1:5]
351
+ now = int(time.time())
352
+ value = display_name if display_name else None
353
+
354
+ conn = sqlite3.connect(db_path)
355
+ conn.execute(
356
+ """
357
+ UPDATE sessions
358
+ SET tmux_name = ?1,
359
+ display_name = COALESCE(?2, display_name),
360
+ updated_at = ?3,
361
+ last_seen_at = ?3
362
+ WHERE tmux_name = ?4
363
+ """,
364
+ (new_tmux_name, value, now, old_tmux_name),
365
+ )
366
+ conn.commit()
367
+ PY
368
+ }
369
+
370
+ _tmux_registry_remove_session() {
371
+ local tmux_name="$1"
372
+ local db_path
373
+ db_path="$(_tmux_session_registry_path)"
374
+
375
+ [[ -f "$db_path" ]] || return 0
376
+ have_cmd python3 || return 1
377
+
378
+ python3 - "$db_path" "$tmux_name" <<'PY'
379
+ import sqlite3
380
+ import sys
381
+
382
+ db_path, tmux_name = sys.argv[1:3]
383
+ conn = sqlite3.connect(db_path)
384
+ conn.execute("DELETE FROM sessions WHERE tmux_name = ?1", (tmux_name,))
385
+ conn.commit()
386
+ PY
387
+ }
388
+
389
+ _tmux_registry_upsert_current_session() {
390
+ local session_name="$1"
391
+ local session_dir repo_root branch
392
+ session_dir="$(tmux display-message -t "$session_name" -p '#{pane_current_path}' 2>/dev/null || echo "")"
393
+ repo_root="$(_tmux_shared_root_for_path "$session_dir" 2>/dev/null || true)"
394
+ branch="$(_tmux_branch_for_path "$session_dir" 2>/dev/null || true)"
395
+ [[ -n "$repo_root" && -n "$branch" ]] || return 1
396
+ _tmux_registry_upsert_session "$repo_root" "$branch" "$session_dir" "$session_name"
397
+ }
398
+
399
+ _tmux_registry_sync_active_workspace_sessions() {
400
+ local repo_root="$1"
401
+ [[ -n "$repo_root" ]] || return 1
402
+ tmux_available || return 1
403
+
404
+ local session_name session_dir session_root branch display_name
405
+ while IFS= read -r session_name; do
406
+ [[ -n "$session_name" ]] || continue
407
+ display_name="$(tmux show-option -t "$session_name" -qv @orchestra_display_name 2>/dev/null || true)"
408
+ [[ -n "$display_name" ]] || continue
409
+ session_dir="$(tmux display-message -t "$session_name" -p '#{pane_current_path}' 2>/dev/null || echo "")"
410
+ session_root="$(_tmux_shared_root_for_path "$session_dir" 2>/dev/null || true)"
411
+ [[ "$session_root" == "$repo_root" ]] || continue
412
+ branch="$(_tmux_branch_for_path "$session_dir" 2>/dev/null || true)"
413
+ [[ -n "$branch" ]] || continue
414
+ _tmux_registry_upsert_session "$repo_root" "$branch" "$session_dir" "$session_name" >/dev/null 2>&1 || true
415
+ done < <(tmux list-sessions -F '#{session_name}' 2>/dev/null || true)
416
+ }
417
+
418
+ _tmux_workspace_cycle_target_cached() {
419
+ local current_session="$1"
420
+ local direction="$2"
421
+ local db_path="$3"
422
+ local active_file="$4"
423
+
424
+ python3 - "$db_path" "$current_session" "$direction" "$active_file" <<'PY'
425
+ import sqlite3
426
+ import sys
427
+
428
+ db_path, current_session, direction, active_path = sys.argv[1:5]
429
+ active = set()
430
+ active_orchestra = set()
431
+ with open(active_path, "r", encoding="utf-8") as handle:
432
+ for raw_line in handle:
433
+ line = raw_line.rstrip("\n")
434
+ if not line:
435
+ continue
436
+ parts = line.split("\t")
437
+ name = parts[0].strip()
438
+ display_name = parts[1].strip() if len(parts) > 1 else ""
439
+ if not name:
440
+ continue
441
+ active.add(name)
442
+ if display_name:
443
+ active_orchestra.add(name)
444
+
445
+ try:
446
+ conn = sqlite3.connect(db_path)
447
+ current_row = conn.execute(
448
+ "SELECT repo_root FROM sessions WHERE tmux_name = ?1",
449
+ (current_session,),
450
+ ).fetchone()
451
+ if current_row is None:
452
+ raise SystemExit(2)
453
+
454
+ registered = {
455
+ name
456
+ for (name,) in conn.execute("SELECT tmux_name FROM sessions").fetchall()
457
+ }
458
+ if active_orchestra.difference(registered):
459
+ raise SystemExit(2)
460
+
461
+ rows = conn.execute(
462
+ """
463
+ SELECT tmux_name
464
+ FROM sessions
465
+ WHERE repo_root = ?1
466
+ ORDER BY worktree_path COLLATE NOCASE, created_at, tmux_name COLLATE NOCASE
467
+ """,
468
+ (current_row[0],),
469
+ ).fetchall()
470
+ except sqlite3.Error:
471
+ raise SystemExit(2)
472
+
473
+ names = []
474
+ seen = set()
475
+ for (name,) in rows:
476
+ if name in active and name not in seen:
477
+ names.append(name)
478
+ seen.add(name)
479
+
480
+ if len(names) < 2 or current_session not in seen:
481
+ raise SystemExit(3)
482
+
483
+ index = names.index(current_session)
484
+ if direction == "next":
485
+ index = (index + 1) % len(names)
486
+ else:
487
+ index = (index - 1) % len(names)
488
+ print(names[index])
489
+ PY
490
+ }
491
+
492
+ _tmux_status_escape_text() {
493
+ local text="$1"
494
+ text="${text//\#/##}"
495
+ printf '%s' "$text"
496
+ }
497
+
498
+ _tmux_truncate_tab_label() {
499
+ local label="$1"
500
+ local max_len="${2:-14}"
501
+ if (( ${#label} > max_len )); then
502
+ printf '%s' "${label:0:max_len}"
503
+ else
504
+ printf '%s' "$label"
505
+ fi
506
+ }
507
+
508
+ _tmux_pad_menu_label() {
509
+ local label="$1"
510
+ local width="${2:-56}"
511
+
512
+ label="$(_tmux_truncate_tab_label "$label" "$width")"
513
+ printf '%-*s' "$width" "$label"
514
+ }
515
+
516
+ _tmux_workspace_session_rows() {
517
+ local current_session="$1"
518
+ local repo_root="$2"
519
+ local mode="${3:-visible}"
520
+ local db_path active_file
521
+ db_path="$(_tmux_session_registry_path)"
522
+
523
+ [[ -f "$db_path" ]] || return 0
524
+ have_cmd python3 || return 0
525
+
526
+ active_file="$(mktemp)"
527
+ tmux list-sessions -F '#{session_name}' > "$active_file" 2>/dev/null || {
528
+ rm -f "$active_file"
529
+ return 0
530
+ }
531
+
532
+ python3 - "$db_path" "$repo_root" "$current_session" "$active_file" "$mode" <<'PY'
533
+ import sqlite3
534
+ import sys
535
+
536
+ db_path, repo_root, current_session, active_path, mode = sys.argv[1:6]
537
+ with open(active_path, "r", encoding="utf-8") as handle:
538
+ active = {line.strip() for line in handle if line.strip()}
539
+
540
+ conn = sqlite3.connect(db_path)
541
+ rows = conn.execute(
542
+ """
543
+ SELECT tmux_name, COALESCE(display_name, '')
544
+ FROM sessions
545
+ WHERE repo_root = ?1
546
+ ORDER BY worktree_path COLLATE NOCASE, created_at, tmux_name COLLATE NOCASE
547
+ """,
548
+ (repo_root,),
549
+ ).fetchall()
550
+
551
+ filtered = []
552
+ seen = set()
553
+ for name, display_name in rows:
554
+ if name in active and name not in seen:
555
+ filtered.append((name, display_name or ""))
556
+ seen.add(name)
557
+
558
+ if not filtered:
559
+ raise SystemExit(0)
560
+
561
+ max_tabs = 8
562
+ if mode == "all" or len(filtered) <= max_tabs:
563
+ visible = filtered
564
+ prefix = suffix = False
565
+ else:
566
+ try:
567
+ current_index = [name for name, _ in filtered].index(current_session)
568
+ except ValueError:
569
+ current_index = 0
570
+ start = max(0, min(current_index - 2, len(filtered) - max_tabs))
571
+ end = min(len(filtered), start + max_tabs)
572
+ visible = filtered[start:end]
573
+ prefix = start > 0
574
+ suffix = end < len(filtered)
575
+
576
+ if prefix:
577
+ print("__ellipsis__\t")
578
+ for name, display_name in visible:
579
+ print(f"{name}\t{display_name}")
580
+ if suffix:
581
+ print("__ellipsis__\t")
582
+ PY
583
+ local status=$?
584
+ rm -f "$active_file"
585
+ return $status
586
+ }
587
+
588
+ _tmux_workspace_session_tabs() {
589
+ local current_session="$1"
590
+ local fallback_display_name="$2"
591
+ local session_dir repo_root rows tabs name display_name tab_label escaped_tab_label divider active_style inactive_style muted_style reset_style
592
+ local tab_max_width=24
593
+ local active_label_max_width=22
594
+
595
+ session_dir="$(tmux display-message -t "$current_session" -p '#{pane_current_path}' 2>/dev/null || echo "")"
596
+ repo_root="$(_tmux_shared_root_for_path "$session_dir" 2>/dev/null || true)"
597
+ if [[ -n "$repo_root" ]]; then
598
+ _tmux_registry_sync_active_workspace_sessions "$repo_root" >/dev/null 2>&1 || true
599
+ rows="$(_tmux_workspace_session_rows "$current_session" "$repo_root" 2>/dev/null || true)"
600
+ else
601
+ rows=""
602
+ fi
603
+
604
+ if [[ -z "$rows" ]]; then
605
+ tab_label="$(_tmux_truncate_tab_label "$fallback_display_name" "$active_label_max_width")"
606
+ escaped_tab_label="$(_tmux_status_escape_text "$tab_label")"
607
+ printf '#[bg=#414868] #[fg=#ff9e64,bg=#414868,bold]● #[fg=#c0caf5,bg=#414868,bold]%s #[default]' "$escaped_tab_label"
608
+ return
609
+ fi
610
+
611
+ # Tokyo Night palette: midnight footer, muted inactive tabs, orange active marker.
612
+ active_style="#[fg=#c0caf5,bg=#414868,bold]"
613
+ inactive_style="#[fg=#a9b1d6,bg=#24283b,nobold]"
614
+ muted_style="#[fg=#565f89,bg=#1a1b26,nobold]"
615
+ reset_style="#[default]"
616
+ tabs=""
617
+ divider="${muted_style}|${reset_style}"
618
+ while IFS=$'\t' read -r name display_name; do
619
+ [[ -n "$name" ]] || continue
620
+ if [[ -n "$tabs" ]]; then
621
+ tabs+="$divider"
622
+ fi
623
+ if [[ "$name" == "__ellipsis__" ]]; then
624
+ tab_label="⋯"
625
+ escaped_tab_label="$(_tmux_status_escape_text "$tab_label")"
626
+ tabs+="${muted_style} ${escaped_tab_label} ${reset_style}"
627
+ continue
628
+ fi
629
+
630
+ if [[ -z "$display_name" ]]; then
631
+ display_name="$(tmux_format_session_display "$name" without-timestamp)"
632
+ fi
633
+ if [[ "$name" == "$current_session" ]]; then
634
+ tab_label="$(_tmux_truncate_tab_label "$display_name" "$active_label_max_width")"
635
+ escaped_tab_label="$(_tmux_status_escape_text "$tab_label")"
636
+ tabs+="#[bg=#414868] #[fg=#ff9e64,bg=#414868,bold]● ${active_style}${escaped_tab_label} ${reset_style}"
637
+ else
638
+ tab_label="$(_tmux_truncate_tab_label "$display_name" "$tab_max_width")"
639
+ escaped_tab_label="$(_tmux_status_escape_text "$tab_label")"
640
+ tabs+="${inactive_style} ${escaped_tab_label} ${reset_style}"
641
+ fi
642
+ done <<< "$rows"
643
+
644
+ printf '%s' "$tabs"
645
+ }
646
+
647
+ _tmux_current_session_label() {
648
+ local session_display_name="$1"
649
+ local label escaped_label
650
+
651
+ label="$(_tmux_truncate_tab_label "$session_display_name" 28)"
652
+ escaped_label="$(_tmux_status_escape_text "$label")"
653
+ printf '#[fg=#ffffff,bg=#3d59a1,bold] %s #[fg=#3d59a1,bg=#1a1b26,nobold]│#[default]' "$escaped_label"
654
+ }
655
+
656
+ _tmux_orchestra_status_left() {
657
+ local session_name="$1"
658
+ local session_display_name="$3"
659
+ printf '%s%s' \
660
+ "$(_tmux_current_session_label "$session_display_name")" \
661
+ "$(_tmux_workspace_session_tabs "$session_name" "$session_display_name")"
662
+ }
663
+
664
+ _tmux_orchestra_status_right() {
665
+ printf '#[fg=#565f89,bg=#1a1b26]Ctrl+b,h for help#[default]'
666
+ }
667
+
668
+ _tmux_configure_orchestra_bindings() {
669
+ local services
670
+ services="$(_orchestra_services_script)"
671
+ [[ -f "$services" ]] || return
672
+
673
+ local quoted_services rename_command prompt_command next_command previous_command new_session_command list_command close_command close_prompt_command help_command
674
+ printf -v quoted_services '%q' "$services"
675
+ rename_command="$quoted_services manual-rename-session \\\"#{session_name}\\\" \\\"%%\\\" >/dev/null 2>&1"
676
+ prompt_command="command-prompt -p 'Rename Orchestra session:' 'run-shell -b \"$rename_command\"'"
677
+ next_command="$quoted_services cycle-workspace-session \\\"#{session_name}\\\" next \\\"#{client_tty}\\\" >/dev/null 2>&1"
678
+ previous_command="$quoted_services cycle-workspace-session \\\"#{session_name}\\\" previous \\\"#{client_tty}\\\" >/dev/null 2>&1"
679
+ new_session_command="$quoted_services create-workspace-session \\\"#{session_name}\\\" \\\"#{client_tty}\\\" >/dev/null 2>&1 || true"
680
+ list_command="$quoted_services workspace-session-menu \\\"#{session_name}\\\" \\\"#{client_tty}\\\" >/dev/null 2>&1 || true"
681
+ close_command="$quoted_services close-workspace-session \\\"#{session_name}\\\" \\\"#{client_tty}\\\" >/dev/null 2>&1 || true"
682
+ close_prompt_command="confirm-before -p 'Close current Orchestra session? (y/n)' 'run-shell -b \"$close_command\"'"
683
+ help_command="$quoted_services tmux-help-popup \\\"#{client_tty}\\\" >/dev/null 2>&1 || true"
684
+
685
+ tmux bind-key -T prefix '?' if-shell -F '#{@orchestra_display_name}' \
686
+ "run-shell -b \"$help_command\"" 'list-keys -N' >/dev/null 2>&1 || true
687
+ tmux bind-key -T prefix h if-shell -F '#{@orchestra_display_name}' \
688
+ "run-shell -b \"$help_command\"" 'refresh-client -S' >/dev/null 2>&1 || true
689
+ tmux bind-key -T prefix n if-shell -F '#{@orchestra_display_name}' \
690
+ "run-shell -b \"$new_session_command\"" 'next-window' >/dev/null 2>&1 || true
691
+ tmux bind-key -T prefix l if-shell -F '#{@orchestra_display_name}' \
692
+ "run-shell -b \"$list_command\"" 'last-window' >/dev/null 2>&1 || true
693
+ tmux bind-key -T prefix q if-shell -F '#{@orchestra_display_name}' \
694
+ "$close_prompt_command" 'display-panes' >/dev/null 2>&1 || true
695
+ tmux bind-key -T prefix X if-shell -F '#{@orchestra_display_name}' \
696
+ "$close_prompt_command" 'confirm-before -p "kill-session #S? (y/n)" kill-session' >/dev/null 2>&1 || true
697
+ tmux bind-key -T prefix r if-shell -F '#{@orchestra_display_name}' \
698
+ "$prompt_command" 'refresh-client -S' >/dev/null 2>&1 || true
699
+ tmux bind-key -T prefix '>' if-shell -F '#{@orchestra_display_name}' \
700
+ "run-shell -b \"$next_command\"" 'switch-client -n' >/dev/null 2>&1 || true
701
+ tmux bind-key -T prefix '<' if-shell -F '#{@orchestra_display_name}' \
702
+ "run-shell -b \"$previous_command\"" 'switch-client -p' >/dev/null 2>&1 || true
703
+ tmux bind-key -r -T prefix Right if-shell -F '#{@orchestra_display_name}' \
704
+ "run-shell -b \"$next_command\"" 'select-pane -R' >/dev/null 2>&1 || true
705
+ tmux bind-key -r -T prefix Left if-shell -F '#{@orchestra_display_name}' \
706
+ "run-shell -b \"$previous_command\"" 'select-pane -L' >/dev/null 2>&1 || true
707
+ }
708
+
709
+ _tmux_configure_orchestra_status() {
710
+ local session_name="$1"
711
+ local worktree_name="$2"
712
+ local session_display_name="${3:-}"
713
+ local status_left status_right
714
+ if [[ -z "$session_display_name" ]]; then
715
+ session_display_name="$(tmux_format_session_display "$session_name" without-timestamp)"
716
+ fi
717
+ status_left="$(_tmux_orchestra_status_left "$session_name" "$worktree_name" "$session_display_name")"
718
+ status_right="$(_tmux_orchestra_status_right "$worktree_name")"
719
+
720
+ tmux set-option -t "$session_name" @orchestra_display_name "$session_display_name" >/dev/null 2>&1 || true
721
+ tmux set-option -t "$session_name" @orchestra_worktree_name "$worktree_name" >/dev/null 2>&1 || true
722
+ tmux set-option -t "$session_name" status on >/dev/null 2>&1 || true
723
+ tmux set-option -t "$session_name" status-position bottom >/dev/null 2>&1 || true
724
+ tmux set-option -t "$session_name" status-style "fg=#c0caf5,bg=#1a1b26" >/dev/null 2>&1 || true
725
+ tmux set-option -t "$session_name" status-left "$status_left" >/dev/null 2>&1 || true
726
+ tmux set-option -t "$session_name" status-left-length 1000 >/dev/null 2>&1 || true
727
+ tmux set-option -t "$session_name" status-right "$status_right" >/dev/null 2>&1 || true
728
+ tmux set-option -t "$session_name" status-right-length 40 >/dev/null 2>&1 || true
729
+ tmux set-option -t "$session_name" window-status-format "" >/dev/null 2>&1 || true
730
+ tmux set-option -t "$session_name" window-status-current-format "" >/dev/null 2>&1 || true
731
+ tmux set-option -t "$session_name" window-status-separator "" >/dev/null 2>&1 || true
732
+ _tmux_configure_orchestra_bindings
733
+ }
734
+
735
+ _tmux_refresh_orchestra_session_status() {
736
+ local session_name="$1"
737
+ local session_dir branch_name worktree_name display_name old_pwd
738
+
739
+ session_dir="$(tmux display-message -t "$session_name" -p '#{pane_current_path}' 2>/dev/null || echo "")"
740
+ branch_name=""
741
+ if [[ -n "$session_dir" && -d "$session_dir" ]]; then
742
+ old_pwd="$PWD"
743
+ cd "$session_dir" 2>/dev/null || true
744
+ branch_name="$(git_current_branch 2>/dev/null || git rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
745
+ cd "$old_pwd" 2>/dev/null || true
746
+ fi
747
+
748
+ if [[ -n "$branch_name" && "$branch_name" != "detached" ]]; then
749
+ worktree_name="$branch_name"
750
+ elif [[ -n "$session_dir" && -d "$session_dir" ]]; then
751
+ worktree_name="$(basename "$session_dir")"
752
+ else
753
+ worktree_name="$branch_name"
754
+ fi
755
+
756
+ display_name="$(tmux show-option -t "$session_name" -qv @orchestra_display_name 2>/dev/null || true)"
757
+ _tmux_configure_orchestra_status "$session_name" "$worktree_name" "$display_name"
758
+ }
759
+
760
+ # Helper: split a string by multi-char delimiter into bash array named by ref
761
+ # Usage: _tmux_split_by_delim "string" "::" out_array_name
762
+ _tmux_split_by_delim() {
763
+ local _s="$1" _d="$2" _ref="$3"
764
+ local _arr=()
765
+ if [[ -z "$_d" ]]; then
766
+ _arr=("$_s")
767
+ else
768
+ while :; do
769
+ if [[ "$_s" == *"$_d"* ]]; then
770
+ _arr+=("${_s%%"$_d"*}")
771
+ _s="${_s#*"$_d"}"
772
+ else
773
+ _arr+=("$_s")
774
+ break
775
+ fi
776
+ done
777
+ fi
778
+ # Use printf with %q to properly quote array elements for eval
779
+ local _quoted=()
780
+ local _item
781
+ for _item in "${_arr[@]}"; do
782
+ printf -v _q "%q" "$_item"
783
+ _quoted+=("$_q")
784
+ done
785
+ eval "$_ref=( ${_quoted[*]} )"
786
+ }
787
+
788
+
789
+ # Check if tmux is available
790
+ tmux_available() {
791
+ have_cmd tmux
792
+ }
793
+
794
+ # Check if currently inside a tmux session
795
+ tmux_inside_session() {
796
+ [[ -n "${TMUX-}" ]]
797
+ }
798
+
799
+ # Generate random readable name for tmux sessions
800
+ tmux_generate_readable_name() {
801
+ local adjectives=(
802
+ "swift" "brave" "clever" "gentle" "bright" "calm" "eager" "fierce" "happy" "kind"
803
+ "lively" "noble" "proud" "quick" "smart" "wise" "bold" "cool" "daring" "epic"
804
+ "fuzzy" "jolly" "lucky" "merry" "peppy" "rosy" "sunny" "zesty" "crisp" "fresh"
805
+ )
806
+ local animals=(
807
+ "bear" "wolf" "fox" "eagle" "hawk" "lion" "tiger" "panda" "otter" "seal"
808
+ "whale" "shark" "dolphin" "falcon" "raven" "deer" "moose" "lynx" "badger" "heron"
809
+ "phoenix" "dragon" "griffin" "unicorn" "pegasus" "kraken" "sphinx" "chimera" "hydra" "basilisk"
810
+ )
811
+
812
+ local adj_idx=$((RANDOM % ${#adjectives[@]}))
813
+ local animal_idx=$((RANDOM % ${#animals[@]}))
814
+
815
+ echo "${adjectives[$adj_idx]}_${animals[$animal_idx]}"
816
+ }
817
+
818
+ # --------------------------- Session Management -----------------------------
819
+
820
+ # Create a new tmux session
821
+ # Usage: tmux_create_session <session_name> <working_directory>
822
+ tmux_create_session() {
823
+ local session_name="$1"
824
+ local working_dir="$2"
825
+
826
+ if ! tmux_available; then
827
+ err "tmux not installed"
828
+ return 1
829
+ fi
830
+
831
+ # Get repository info from the working directory context
832
+ local repo_name=""
833
+ local branch_name=""
834
+ local repo_root=""
835
+ local old_pwd="$PWD"
836
+ local resolved_working_dir="$working_dir"
837
+ if [[ -n "$working_dir" && -d "$working_dir" ]]; then
838
+ resolved_working_dir="$(cd "$working_dir" 2>/dev/null && pwd -P || printf '%s' "$working_dir")"
839
+ fi
840
+
841
+ # Change to working directory to get accurate git info
842
+ cd "$resolved_working_dir" 2>/dev/null || true
843
+
844
+ if command -v git >/dev/null 2>&1 && git rev-parse --git-dir >/dev/null 2>&1; then
845
+ repo_name="$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null || echo "unknown")"
846
+ branch_name="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "detached")"
847
+ repo_root="$(git_shared_root 2>/dev/null || git_repo_root 2>/dev/null || true)"
848
+ fi
849
+
850
+ # Restore original directory
851
+ cd "$old_pwd" 2>/dev/null || true
852
+
853
+ # Ensure orchestra prefix (configurable delimiter) on session name to mark origin
854
+ local ORCH_PREFIX
855
+ ORCH_PREFIX="$(_tmux_orch_prefix)"
856
+ if [[ "$session_name" != ${ORCH_PREFIX}* ]]; then
857
+ session_name="${ORCH_PREFIX}${session_name}"
858
+ fi
859
+
860
+ # Create session with custom Orchestra status configuration
861
+ tmux new-session -Ad -s "$session_name" -c "$resolved_working_dir" >/dev/null 2>&1 || {
862
+ err "Failed to create tmux session: $session_name"
863
+ return 1
864
+ }
865
+
866
+ local worktree_name=""
867
+ if [[ -n "$branch_name" && "$branch_name" != "detached" ]]; then
868
+ worktree_name="$branch_name"
869
+ else
870
+ worktree_name="$(basename "$resolved_working_dir")"
871
+ fi
872
+
873
+ if [[ -n "$repo_root" && -n "$branch_name" && "$branch_name" != "detached" ]]; then
874
+ _tmux_registry_upsert_session "$repo_root" "$branch_name" "$resolved_working_dir" "$session_name" >/dev/null 2>&1 || true
875
+ fi
876
+
877
+ # Customize the default status bar to include Orchestra info on the left
878
+ if [[ -n "$repo_name" ]]; then
879
+ _tmux_configure_orchestra_status "$session_name" "$worktree_name"
880
+ fi
881
+
882
+ echo "$session_name"
883
+ }
884
+
885
+ # Generate a short hash from a path for unique session identification
886
+ # Usage: tmux_path_hash <path>
887
+ tmux_path_hash() {
888
+ local path="$1"
889
+ # Use MD5 hash of the path, take first 8 chars
890
+ # Works on both macOS and Linux
891
+ if command -v md5sum >/dev/null 2>&1; then
892
+ echo -n "$path" | md5sum | cut -c1-8
893
+ elif command -v md5 >/dev/null 2>&1; then
894
+ echo -n "$path" | md5 -q | cut -c1-8
895
+ else
896
+ # Fallback: use cksum if neither md5 available
897
+ echo -n "$path" | cksum | cut -d' ' -f1 | cut -c1-8
898
+ fi
899
+ }
900
+
901
+ # Alias for backward compatibility
902
+ tmux_repo_hash() {
903
+ tmux_path_hash "$1"
904
+ }
905
+
906
+ # Ensure a session exists for slug+name in worktree; prints session name
907
+ # Creates sessions with format: [worktreename]_[worktreetreehash]_[datetime]_[readable_name]
908
+ # Usage: tmux_ensure_session <slug> <name> <worktree_path>
909
+ tmux_ensure_session() {
910
+ local slug="$1"
911
+ local name="$2"
912
+ local wt="$3"
913
+ local d
914
+ d="$(_tmux_delim)"
915
+ local date_part time_part
916
+ date_part="$(date +%Y%m%d)"
917
+ time_part="$(date +%H%M%S)"
918
+
919
+ # Use a repo-scoped hash to avoid cross-repo collisions
920
+ # Hash the absolute worktree path (backward-compat listing supports old slug-hash)
921
+ local worktree_hash
922
+ worktree_hash="$(tmux_path_hash "$wt")"
923
+
924
+ # If name is provided, use it; otherwise generate a random readable name with auto_ prefix
925
+ if [[ -z "$name" || "$name" == "main" ]]; then
926
+ name="auto_$(tmux_generate_readable_name)"
927
+ fi
928
+
929
+ local sess="${slug}${d}${worktree_hash}${d}${date_part}${d}${time_part}${d}${name}"
930
+ tmux_create_session "$sess" "$wt"
931
+ _tmux_source_command_hook "$sess"
932
+ }
933
+
934
+ # Create a new auto-named Orchestra session in the current session's worktree and switch to it.
935
+ # Uses the same tmux_ensure_session path as the TUI tree view create action.
936
+ # Usage: tmux_create_workspace_session <current_session> [client_tty]
937
+ tmux_create_workspace_session() {
938
+ local current_session="$1"
939
+ local target_client="${2:-}"
940
+ local session_dir branch slug new_session
941
+
942
+ if ! tmux_available; then
943
+ err "tmux not installed"
944
+ return 1
945
+ fi
946
+ if [[ -z "$current_session" ]]; then
947
+ err "tmux_create_workspace_session: current session required"
948
+ return 1
949
+ fi
950
+
951
+ session_dir="$(tmux display-message -t "$current_session" -p '#{pane_current_path}' 2>/dev/null || echo "")"
952
+ if [[ -z "$session_dir" || ! -d "$session_dir" ]]; then
953
+ err "Unable to determine current worktree path"
954
+ return 1
955
+ fi
956
+
957
+ branch="$(_tmux_branch_for_path "$session_dir" 2>/dev/null || true)"
958
+ if [[ -z "$branch" || "$branch" == "detached" ]]; then
959
+ err "Unable to determine current worktree branch"
960
+ return 1
961
+ fi
962
+
963
+ slug="$(git_branch_to_slug "$branch")"
964
+ new_session="$(tmux_ensure_session "$slug" "" "$session_dir")" || return 1
965
+
966
+ if [[ -n "$target_client" ]]; then
967
+ tmux switch-client -c "$target_client" -t "$new_session" >/dev/null 2>&1 || return 1
968
+ else
969
+ tmux switch-client -t "$new_session" >/dev/null 2>&1 || return 1
970
+ fi
971
+
972
+ printf '%s\n' "$new_session"
973
+ }
974
+
975
+ # Close the current Orchestra session after switching the client to the next one.
976
+ # Usage: tmux_close_workspace_session <current_session> [client_tty]
977
+ tmux_close_workspace_session() {
978
+ local current_session="$1"
979
+ local target_client="${2:-}"
980
+ local target_session
981
+
982
+ if ! tmux_available; then
983
+ err "tmux not installed"
984
+ return 1
985
+ fi
986
+ if [[ -z "$current_session" ]]; then
987
+ err "tmux_close_workspace_session: current session required"
988
+ return 1
989
+ fi
990
+
991
+ if ! target_session="$(tmux_workspace_cycle_target "$current_session" next)"; then
992
+ tmux display-message -d 2500 "No other Orchestra sessions; current session kept" 2>/dev/null || true
993
+ return 1
994
+ fi
995
+
996
+ if [[ -n "$target_client" ]]; then
997
+ tmux switch-client -c "$target_client" -t "$target_session" >/dev/null 2>&1 || return 1
998
+ else
999
+ tmux switch-client -t "$target_session" >/dev/null 2>&1 || return 1
1000
+ fi
1001
+
1002
+ tmux_kill_session "$current_session" >/dev/null 2>&1 || return 1
1003
+ _tmux_refresh_orchestra_session_status "$target_session" >/dev/null 2>&1 || true
1004
+ }
1005
+
1006
+ # Check if a session exists
1007
+ # Usage: tmux_session_exists <session_name>
1008
+ tmux_session_exists() {
1009
+ local session_name="$1"
1010
+ tmux_available && tmux has-session -t "$session_name" 2>/dev/null
1011
+ }
1012
+
1013
+ # Kill/delete a tmux session
1014
+ # Usage: tmux_kill_session <session_name>
1015
+ tmux_kill_session() {
1016
+ local session_name="$1"
1017
+
1018
+ if ! tmux_available; then
1019
+ err "tmux not installed"
1020
+ return 1
1021
+ fi
1022
+
1023
+ tmux kill-session -t "$session_name" 2>/dev/null || {
1024
+ err "Failed to kill session: $session_name"
1025
+ return 1
1026
+ }
1027
+ _tmux_registry_remove_session "$session_name" >/dev/null 2>&1 || true
1028
+ }
1029
+
1030
+ # Attach or switch to session
1031
+ # Usage: tmux_attach_session <session_name>
1032
+ tmux_attach_session() {
1033
+ local sess="$1"
1034
+
1035
+ if ! tmux_available; then
1036
+ err "tmux not installed"
1037
+ return 1
1038
+ fi
1039
+
1040
+ _tmux_source_command_hook "$sess"
1041
+
1042
+ # Get repository info for terminal title and banner update
1043
+ local repo_name=""
1044
+ local branch_name=""
1045
+ local session_dir=""
1046
+
1047
+ # Try to get the session's working directory
1048
+ session_dir="$(tmux display-message -t "$sess" -p '#{pane_current_path}' 2>/dev/null || echo "")"
1049
+
1050
+ if [[ -n "$session_dir" ]] && [[ -d "$session_dir" ]]; then
1051
+ # Get git info from session's directory
1052
+ local old_pwd="$PWD"
1053
+ cd "$session_dir" 2>/dev/null || true
1054
+
1055
+ if command -v git >/dev/null 2>&1 && git rev-parse --git-dir >/dev/null 2>&1; then
1056
+ repo_name="$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null || echo "unknown")"
1057
+ branch_name="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "detached")"
1058
+ fi
1059
+
1060
+ cd "$old_pwd" 2>/dev/null || true
1061
+ else
1062
+ # Fallback to current directory
1063
+ if command -v git >/dev/null 2>&1 && git rev-parse --git-dir >/dev/null 2>&1; then
1064
+ repo_name="$(basename "$(git rev-parse --show-toplevel 2>/dev/null)" 2>/dev/null || echo "unknown")"
1065
+ branch_name="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "detached")"
1066
+ fi
1067
+ fi
1068
+
1069
+ # Set terminal window title (works in most terminal emulators)
1070
+ if [[ -n "$repo_name" ]]; then
1071
+ printf "\033]0;🎼 Orchestra: %s [%s]\007" "$repo_name" "$branch_name" >/dev/tty 2>/dev/null || true
1072
+ fi
1073
+
1074
+ # Show welcome message when attaching
1075
+ if [[ -n "$repo_name" ]]; then
1076
+ tmux display-message -t "$sess" -d 2000 \
1077
+ "🎼 Orchestra Session | Repository: $repo_name | Branch: $branch_name" 2>/dev/null || true
1078
+ fi
1079
+
1080
+ _tmux_refresh_orchestra_session_status "$sess"
1081
+
1082
+ if tmux_inside_session; then
1083
+ tmux switch-client -t "$sess" >/dev/null 2>&1 || true
1084
+ else
1085
+ tmux attach -t "$sess" >/dev/null 2>&1 || true
1086
+ fi
1087
+ }
1088
+
1089
+ # Rename a tmux session while preserving the worktree prefix, repo hash, and datetime
1090
+ # Usage: tmux_rename_session <old_session_name> <new_name>
1091
+ tmux_rename_session() {
1092
+ local old_session="$1"
1093
+ local new_name="$2"
1094
+
1095
+ if ! tmux_available; then
1096
+ err "tmux not installed"
1097
+ return 1
1098
+ fi
1099
+
1100
+ local d ORCH_PREFIX
1101
+ d="$(_tmux_delim)"
1102
+ ORCH_PREFIX="$(_tmux_orch_prefix)"
1103
+
1104
+ # Detect and strip orchestra prefix for parsing
1105
+ local orch=""
1106
+ local base="$old_session"
1107
+ if [[ "$base" == ${ORCH_PREFIX}* ]]; then
1108
+ orch="$ORCH_PREFIX"
1109
+ base="${base#${ORCH_PREFIX}}"
1110
+ fi
1111
+
1112
+ # Split by current delimiter and scan from the right to tolerate delimiter inside segments
1113
+ local parts
1114
+ _tmux_split_by_delim "$base" "$d" parts
1115
+
1116
+ local n=${#parts[@]}
1117
+ local idx_time=-1 idx_date=-1 idx_hash=-1
1118
+ local i
1119
+ for (( i=n-1; i>=0; i-- )); do
1120
+ if [[ ${parts[$i]} =~ ^[0-9]{6}$ ]]; then idx_time=$i; break; fi
1121
+ done
1122
+ if (( idx_time > 0 )) && [[ ${parts[$((idx_time-1))]} =~ ^[0-9]{8}$ ]]; then
1123
+ idx_date=$((idx_time-1))
1124
+ fi
1125
+ if (( idx_date > 0 )) && [[ ${parts[$((idx_date-1))]} =~ ^[0-9a-f]{8}$ ]]; then
1126
+ idx_hash=$((idx_date-1))
1127
+ fi
1128
+
1129
+ local prefix=""
1130
+ if (( idx_time >= 0 && idx_date >= 0 )); then
1131
+ # Build prefix: slug + d + [hash + d] + date + d + time + d
1132
+ local upto=$idx_time
1133
+ local j
1134
+ for (( j=0; j<=upto; j++ )); do
1135
+ if (( j > 0 )); then prefix+="$d"; fi
1136
+ prefix+="${parts[$j]}"
1137
+ done
1138
+ prefix+="$d"
1139
+
1140
+ # Debug logging for rename parsing
1141
+ if [[ -n "${GW_DEBUG_RENAME-}" || -n "${DEBUG-}" ]]; then
1142
+ >&2 echo "[orchestra] rename DEBUG: old='$old_session' base='$base' delim='$d'"
1143
+ >&2 echo "[orchestra] parts: ${parts[*]}"
1144
+ >&2 echo "[orchestra] idx_time=$idx_time idx_date=$idx_date idx_hash=$idx_hash"
1145
+ >&2 echo "[orchestra] prefix='${orch}${prefix}' new_name='$new_name'"
1146
+ fi
1147
+ else
1148
+ err "Invalid session format"
1149
+ return 1
1150
+ fi
1151
+
1152
+ local new_session="${orch}${prefix}${new_name}"
1153
+
1154
+ tmux rename-session -t "$old_session" "$new_session" 2>/dev/null || {
1155
+ err "Failed to rename session"
1156
+ return 1
1157
+ }
1158
+ _tmux_registry_rename_session "$old_session" "$new_session" "$(format_session_display_name "$new_name")" >/dev/null 2>&1 || true
1159
+ local worktree_name
1160
+ worktree_name="$(tmux show-option -t "$new_session" -qv @orchestra_worktree_name 2>/dev/null || true)"
1161
+ if [[ -z "$worktree_name" ]]; then
1162
+ local session_dir old_pwd branch_name
1163
+ session_dir="$(tmux display-message -t "$new_session" -p '#{pane_current_path}' 2>/dev/null || echo "")"
1164
+ if [[ -n "$session_dir" && -d "$session_dir" ]]; then
1165
+ old_pwd="$PWD"
1166
+ cd "$session_dir" 2>/dev/null || true
1167
+ branch_name="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")"
1168
+ cd "$old_pwd" 2>/dev/null || true
1169
+ if [[ -n "$branch_name" && "$branch_name" != "detached" ]]; then
1170
+ worktree_name="$branch_name"
1171
+ else
1172
+ worktree_name="$(basename "$session_dir")"
1173
+ fi
1174
+ fi
1175
+ fi
1176
+ _tmux_configure_orchestra_status "$new_session" "$worktree_name"
1177
+ >&2 echo "✏️ Renamed session to: $new_name"
1178
+ return 0
1179
+ }
1180
+
1181
+ # --------------------------- Session Discovery ------------------------------
1182
+
1183
+ # Get tmux sessions for a slug prefix; prints session names sorted by creation time, oldest first
1184
+ # Expected session format: [worktreename]_[worktreehash]_[datetime]_[readable_name]
1185
+ # Usage: tmux_list_sessions_for_slug <slug> [worktree_path]
1186
+ tmux_list_sessions_for_slug() {
1187
+ local slug="$1"
1188
+ local worktree_path="${2:-}"
1189
+ tmux_available || return 0
1190
+
1191
+ local d ORCH_PREFIX
1192
+ d="$(_tmux_delim)"
1193
+ ORCH_PREFIX="$(_tmux_orch_prefix)"
1194
+
1195
+ # If worktree_path provided, match hash-based and no-hash prefixes
1196
+ if [[ -n "$worktree_path" ]]; then
1197
+ local hash_slug hash_path branch_name
1198
+ hash_slug="$(tmux_path_hash "$slug")"
1199
+ hash_path="$(tmux_path_hash "$worktree_path")"
1200
+ branch_name="$(git_worktree_path_to_branch "$worktree_path" 2>/dev/null || true)"
1201
+
1202
+ local p1_new="${ORCH_PREFIX}${slug}${d}${hash_slug}${d}"
1203
+ local p2_new="${slug}${d}${hash_slug}${d}"
1204
+ local p1_old="${ORCH_PREFIX}${slug}${d}${hash_path}${d}"
1205
+ local p2_old="${slug}${d}${hash_path}${d}"
1206
+ local p1_branch_hash=""
1207
+ local p2_branch_hash=""
1208
+ if [[ -n "$branch_name" ]]; then
1209
+ p1_branch_hash="${ORCH_PREFIX}${branch_name}${d}${hash_path}${d}"
1210
+ p2_branch_hash="${branch_name}${d}${hash_path}${d}"
1211
+ fi
1212
+
1213
+ # List sessions with any known prefix variant
1214
+ tmux list-sessions -F '#{session_name}|||#{session_created}' 2>/dev/null \
1215
+ | sed 's/|||/\t/g' \
1216
+ | awk -v a="$p1_new" -v b="$p2_new" -v c="$p1_old" -v d="$p2_old" -v e="$p1_branch_hash" -v f="$p2_branch_hash" 'BEGIN{FS="\t"} (length(a)>0 && index($1, a)==1) || (length(b)>0 && index($1, b)==1) || (length(c)>0 && index($1, c)==1) || (length(d)>0 && index($1, d)==1) || (length(e)>0 && index($1, e)==1) || (length(f)>0 && index($1, f)==1) {print $1"\t"$2}' \
1217
+ | sort -t $'\t' -k2,2n -k1,1f \
1218
+ | awk -F '\t' '{print $1}' | awk '!seen[$0]++' || true
1219
+ else
1220
+ # New-format prefix matching without hash: orchestra__slug__... or slug__...
1221
+ local prefix1="${ORCH_PREFIX}${slug}${d}"
1222
+ local prefix2="${slug}${d}"
1223
+ tmux list-sessions -F '#{session_name}|||#{session_created}' 2>/dev/null \
1224
+ | sed 's/|||/\t/g' \
1225
+ | awk -v p1="$prefix1" -v p2="$prefix2" 'BEGIN{FS="\t"} index($1, p1)==1 || index($1, p2)==1 {print $1"\t"$2}' \
1226
+ | sort -t $'\t' -k2,2n -k1,1f \
1227
+ | awk -F '\t' '{print $1}' || true
1228
+ fi
1229
+ }
1230
+
1231
+ # List all tmux sessions
1232
+ # Usage: tmux_list_all_sessions
1233
+ tmux_list_all_sessions() {
1234
+ tmux_available || return 0
1235
+ tmux list-sessions -F '#{session_name}' 2>/dev/null || true
1236
+ }
1237
+
1238
+ # Find sessions matching a pattern
1239
+ # Usage: tmux_find_session <pattern>
1240
+ tmux_find_session() {
1241
+ local pattern="$1"
1242
+
1243
+ if ! tmux_available; then
1244
+ return 1
1245
+ fi
1246
+
1247
+ # Try exact match first
1248
+ if tmux_session_exists "$pattern"; then
1249
+ echo "$pattern"
1250
+ return 0
1251
+ fi
1252
+
1253
+ # Try pattern match
1254
+ tmux_list_all_sessions | grep -E "$pattern" | head -1 || true
1255
+ }
1256
+
1257
+ # --------------------------- Session Information ----------------------------
1258
+
1259
+ # Helper function to format session display names
1260
+ # Input: session_content (e.g., "opencode_fixing_auth_bug" or "my_feature_work")
1261
+ # Output: "Opencode Fixing Auth Bug" or "My Feature Work"
1262
+ format_session_display_name() {
1263
+ local session_content="$1"
1264
+ local d
1265
+ d="$(_tmux_delim)"
1266
+ local ORCH_PREFIX
1267
+ ORCH_PREFIX="$(_tmux_orch_prefix)"
1268
+ if [[ "$session_content" == ${ORCH_PREFIX}* ]]; then
1269
+ session_content="${session_content#${ORCH_PREFIX}}"
1270
+ fi
1271
+ if [[ "$session_content" == auto_* ]]; then
1272
+ session_content="${session_content#auto_}"
1273
+ fi
1274
+ local description
1275
+ description="$(echo "$session_content" | tr '_-' ' ')"
1276
+ description="$(echo "$description" | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2));}1')"
1277
+ echo "$description"
1278
+ }
1279
+
1280
+ _tmux_format_session_timestamp() {
1281
+ local date_part="$1"
1282
+ local time_part="$2"
1283
+
1284
+ if [[ ! "$date_part" =~ ^[0-9]{8}$ || ! "$time_part" =~ ^[0-9]{6}$ ]]; then
1285
+ printf '%s %s\n' "$date_part" "$time_part"
1286
+ return
1287
+ fi
1288
+
1289
+ local month day hour minute ampm display_hour
1290
+ month=$((10#${date_part:4:2}))
1291
+ day=$((10#${date_part:6:2}))
1292
+ hour=$((10#${time_part:0:2}))
1293
+ minute="${time_part:2:2}"
1294
+
1295
+ ampm="am"
1296
+ display_hour="$hour"
1297
+ if (( hour >= 12 )); then
1298
+ ampm="pm"
1299
+ if (( hour > 12 )); then
1300
+ display_hour=$((hour - 12))
1301
+ fi
1302
+ fi
1303
+ if (( hour == 0 )); then
1304
+ display_hour=12
1305
+ fi
1306
+
1307
+ local month_names=("" "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
1308
+ local month_name="???"
1309
+ if (( month > 0 && month <= 12 )); then
1310
+ month_name="${month_names[$month]}"
1311
+ fi
1312
+
1313
+ printf '%s %s %s:%s%s\n' "$month_name" "$day" "$display_hour" "$minute" "$ampm"
1314
+ }
1315
+
1316
+ # Parse session name and format for display
1317
+ # Input: session_name (format: worktreename_repohash_YYYYMMDD_HHMMSS_readable_name or old formats)
1318
+ # Output: formatted display name
1319
+ # Usage: tmux_format_session_display <session_name>
1320
+ tmux_format_session_display() {
1321
+ local session_name="$1"
1322
+ local timestamp_mode="${2:-with-timestamp}"
1323
+
1324
+ # Handle temporary renaming sessions first
1325
+ if [[ "$session_name" =~ _renaming$ ]]; then
1326
+ # Extract base name before _renaming suffix
1327
+ local base_name="${session_name%_renaming}"
1328
+ # Recursively process the base name to get proper display
1329
+ tmux_format_session_display "$base_name" "$timestamp_mode"
1330
+ return $?
1331
+ fi
1332
+
1333
+ local d ORCH_PREFIX
1334
+ d="$(_tmux_delim)"
1335
+ ORCH_PREFIX="$(_tmux_orch_prefix)"
1336
+
1337
+ if [[ "$session_name" == ${ORCH_PREFIX}* ]]; then
1338
+ session_name="${session_name#${ORCH_PREFIX}}"
1339
+ elif [[ "$session_name" =~ ^orchestra_(.+)$ ]]; then
1340
+ session_name="${BASH_REMATCH[1]}"
1341
+ fi
1342
+
1343
+ local parts
1344
+ _tmux_split_by_delim "$session_name" "$d" parts
1345
+ local idx_time=-1 idx_date=-1 i
1346
+ for (( i=${#parts[@]}-1; i>=0; i-- )); do
1347
+ if [[ ${parts[$i]} =~ ^[0-9]{6}$ ]] && (( i > 0 )) && [[ ${parts[$((i-1))]} =~ ^[0-9]{8}$ ]]; then
1348
+ idx_time=$i
1349
+ idx_date=$((i-1))
1350
+ break
1351
+ fi
1352
+ done
1353
+ if (( idx_time >= 0 && idx_date >= 0 )); then
1354
+ local readable_name=""
1355
+ local j
1356
+ for (( j=idx_time+1; j<${#parts[@]}; j++ )); do
1357
+ if [[ -n "$readable_name" ]]; then
1358
+ readable_name+="$d"
1359
+ fi
1360
+ readable_name+="${parts[$j]}"
1361
+ done
1362
+ if [[ -n "$readable_name" ]]; then
1363
+ local formatted_name timestamp
1364
+ formatted_name="$(format_session_display_name "$readable_name")"
1365
+ timestamp="$(_tmux_format_session_timestamp "${parts[$idx_date]}" "${parts[$idx_time]}")"
1366
+ if [[ "$timestamp_mode" == "without-timestamp" ]]; then
1367
+ echo "$formatted_name"
1368
+ else
1369
+ echo "${formatted_name} (${timestamp})"
1370
+ fi
1371
+ return 0
1372
+ fi
1373
+ fi
1374
+
1375
+ # New format with repo hash: worktreename_repohash_YYYYMMDD_HHMMSS_readable_name
1376
+ if [[ "$session_name" =~ ^[^_]+_[a-f0-9]{8}_([0-9]{8})_([0-9]{6})_(.+)$ ]]; then
1377
+ local date_part="${BASH_REMATCH[1]}" # YYYYMMDD
1378
+ local time_part="${BASH_REMATCH[2]}" # HHMMSS
1379
+ local readable_name="${BASH_REMATCH[3]}" # readable_name
1380
+
1381
+ # Parse date: YYYYMMDD -> Jul 21
1382
+ local year="${date_part:0:4}"
1383
+ local month="${date_part:4:2}"
1384
+ local day="${date_part:6:2}"
1385
+
1386
+ # Parse time: HHMMSS -> 12:30am
1387
+ local hour="${time_part:0:2}"
1388
+ local minute="${time_part:2:2}"
1389
+
1390
+ # Convert to 12-hour format
1391
+ local ampm="am"
1392
+ local display_hour="$hour"
1393
+ if [[ "$hour" -ge 12 ]]; then
1394
+ ampm="pm"
1395
+ if [[ "$hour" -gt 12 ]]; then
1396
+ display_hour=$((hour - 12))
1397
+ fi
1398
+ fi
1399
+ if [[ "$hour" == "00" ]]; then
1400
+ display_hour="12"
1401
+ fi
1402
+
1403
+ # Remove leading zero from hour
1404
+ display_hour="${display_hour#0}"
1405
+
1406
+ # Convert month number to name
1407
+ local month_names=("" "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
1408
+ local month_name="${month_names[${month#0}]}"
1409
+
1410
+ # Remove leading zero from day
1411
+ local display_day="${day#0}"
1412
+
1413
+ # Format the readable name with proper formatting
1414
+ local formatted_name="$(format_session_display_name "$readable_name")"
1415
+
1416
+ if [[ "$timestamp_mode" == "without-timestamp" ]]; then
1417
+ echo "$formatted_name"
1418
+ else
1419
+ echo "${formatted_name} (${month_name} ${display_day} ${display_hour}:${minute}${ampm})"
1420
+ fi
1421
+ # Fallback: Try format without repo hash: appname_description_YYYYMMDD_HHMMSS
1422
+ elif [[ "$session_name" =~ ^(.*)_[0-9]{8}_[0-9]{6}$ ]]; then
1423
+ local session_content="${BASH_REMATCH[1]}" # appname_description
1424
+
1425
+ # Convert to proper display format
1426
+ local formatted_name="$(format_session_display_name "$session_content")"
1427
+ echo "$formatted_name"
1428
+ else
1429
+ # Check for old format without repo hash: worktreename_YYYYMMDD_HHMMSS_readable_name
1430
+ if [[ "$session_name" =~ ^[^_]+_([0-9]{8})_([0-9]{6})_(.+)$ ]]; then
1431
+ local date_part="${BASH_REMATCH[1]}" # YYYYMMDD
1432
+ local time_part="${BASH_REMATCH[2]}" # HHMMSS
1433
+ local readable_name="${BASH_REMATCH[3]}" # readable_name
1434
+
1435
+ # Parse date: YYYYMMDD -> Jul 21
1436
+ local year="${date_part:0:4}"
1437
+ local month="${date_part:4:2}"
1438
+ local day="${date_part:6:2}"
1439
+
1440
+ # Parse time: HHMMSS -> 12:30am
1441
+ local hour="${time_part:0:2}"
1442
+ local minute="${time_part:2:2}"
1443
+
1444
+ # Convert to 12-hour format
1445
+ local ampm="am"
1446
+ local display_hour="$hour"
1447
+ if [[ "$hour" -ge 12 ]]; then
1448
+ ampm="pm"
1449
+ if [[ "$hour" -gt 12 ]]; then
1450
+ display_hour=$((hour - 12))
1451
+ fi
1452
+ fi
1453
+ if [[ "$hour" == "00" ]]; then
1454
+ display_hour="12"
1455
+ fi
1456
+
1457
+ # Remove leading zero from hour
1458
+ display_hour="${display_hour#0}"
1459
+
1460
+ # Convert month number to name
1461
+ local month_names=("" "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
1462
+ local month_name="${month_names[${month#0}]}"
1463
+
1464
+ # Remove leading zero from day
1465
+ local display_day="${day#0}"
1466
+
1467
+ # Format the readable name with proper formatting
1468
+ local formatted_name="$(format_session_display_name "$readable_name")"
1469
+
1470
+ if [[ "$timestamp_mode" == "without-timestamp" ]]; then
1471
+ echo "$formatted_name"
1472
+ else
1473
+ echo "${formatted_name} (${month_name} ${display_day} ${display_hour}:${minute}${ampm})"
1474
+ fi
1475
+ else
1476
+ # Fallback for sessions that don't match either format
1477
+ echo "$session_name"
1478
+ fi
1479
+ fi
1480
+ }
1481
+
1482
+ # Get active pane id for a session (best effort)
1483
+ # Usage: tmux_get_active_pane <session_name>
1484
+ tmux_get_active_pane() {
1485
+ local s="$1"
1486
+ tmux_available || return 1
1487
+
1488
+ # Find active window id
1489
+ local win
1490
+ win="$(tmux list-windows -t "$s" -F '#{window_active} #{window_id}' 2>/dev/null | awk '$1==1{print $2; exit}')" || true
1491
+ [[ -z "$win" ]] && return 1
1492
+
1493
+ # Find active pane id within that window
1494
+ tmux list-panes -t "$win" -F '#{pane_active} #{pane_id}' 2>/dev/null | awk '$1==1{print $2; exit}' || true
1495
+ }
1496
+
1497
+ # Capture enhanced session preview showing current terminal view (bottom lines)
1498
+ # Usage: tmux_session_preview <session_name>
1499
+ tmux_session_preview() {
1500
+ local s="$1"
1501
+ tmux_available || { echo "(tmux not installed)"; return 0; }
1502
+
1503
+ local pane
1504
+ pane="$(tmux_get_active_pane "$s" || true)"
1505
+ if [[ -z "$pane" ]]; then
1506
+ echo "(no active pane found)"
1507
+ return 0
1508
+ fi
1509
+
1510
+ # Always capture from the BOTTOM of the terminal buffer (last visible lines)
1511
+ # -e flag preserves escape sequences for colors and formatting
1512
+ # -p prints to stdout
1513
+ # NO -S/-E flags means capture the current viewport (what's visible NOW)
1514
+ # This ensures we always see the bottom/most recent output
1515
+ local txt
1516
+ txt="$(tmux capture-pane -e -p -t "$pane" 2>/dev/null)"
1517
+
1518
+ if [[ -z "$txt" ]]; then
1519
+ echo "(no output yet)"
1520
+ return 0
1521
+ fi
1522
+
1523
+ # Check if the pane is idle (cursor at prompt, no running process)
1524
+ local pane_cmd pane_mode
1525
+ pane_cmd="$(tmux display-message -t "$pane" -p '#{pane_current_command}' 2>/dev/null || echo "")"
1526
+ pane_mode="$(tmux display-message -t "$pane" -p '#{pane_mode}' 2>/dev/null || echo "")"
1527
+
1528
+ # Detect idle state: shell running and not in copy mode
1529
+ local is_idle="false"
1530
+ case "$pane_cmd" in
1531
+ bash|zsh|sh|fish|dash|ksh)
1532
+ # Shell is running, check if we're at a prompt (not in copy mode)
1533
+ if [[ "$pane_mode" == "" ]]; then
1534
+ is_idle="true"
1535
+ fi
1536
+ ;;
1537
+ esac
1538
+
1539
+ # Optimized: Get terminal info in a single command instead of multiple calls
1540
+ local term_info has_rgb
1541
+ term_info="$(tmux show-environment -t "$s" TERM 2>/dev/null | cut -d= -f2 || echo "unknown")"
1542
+
1543
+ # Fast check for RGB support without full grep
1544
+ if tmux show-options -t "$s" -s terminal-overrides 2>/dev/null | grep -q "RGB"; then
1545
+ has_rgb="true"
1546
+ else
1547
+ has_rgb="false"
1548
+ fi
1549
+
1550
+ # For ANSI-preserved preview with color mode info
1551
+ # Add markers for the Rust parser to detect color capabilities and idle state
1552
+ if [[ "$has_rgb" == "true" || "$term_info" == *"direct"* || "$term_info" == *"truecolor"* ]]; then
1553
+ echo "<<<COLORMODE:RGB>>>"
1554
+ elif [[ "$term_info" == *"256color"* ]]; then
1555
+ echo "<<<COLORMODE:256>>>"
1556
+ else
1557
+ echo "<<<COLORMODE:16>>>"
1558
+ fi
1559
+
1560
+ # Add idle marker so Rust can stop polling when terminal is idle
1561
+ if [[ "$is_idle" == "true" ]]; then
1562
+ echo "<<<IDLE:true>>>"
1563
+ else
1564
+ echo "<<<IDLE:false>>>"
1565
+ fi
1566
+
1567
+ # If no content after processing, show placeholder
1568
+ if [[ -z "$txt" ]]; then
1569
+ echo "(session active, no visible output)"
1570
+ else
1571
+ echo "$txt"
1572
+ fi
1573
+ }
1574
+
1575
+ # --------------------------- Advanced Operations ----------------------------
1576
+
1577
+ # Send keys to a session and press Enter
1578
+ # Usage: tmux_send_keys <session_name> <command...>
1579
+ tmux_send_keys() {
1580
+ local session_name="$1"; shift || true
1581
+ local command_line="$*"
1582
+ if ! tmux_available; then
1583
+ err "tmux not installed"
1584
+ return 1
1585
+ fi
1586
+ if [[ -z "$session_name" || -z "$command_line" ]]; then
1587
+ err "tmux_send_keys: session and command required"
1588
+ return 1
1589
+ fi
1590
+ tmux send-keys -t "$session_name" -l -- "$command_line" 2>/dev/null || return 1
1591
+ tmux send-keys -t "$session_name" C-m 2>/dev/null || return 1
1592
+ }
1593
+
1594
+ # Show Orchestra tmux shortcuts in a popup modal.
1595
+ # Usage: tmux_show_orchestra_help_popup [client_tty]
1596
+ tmux_show_orchestra_help_popup() {
1597
+ local target_client="${1:-}"
1598
+ if ! tmux_available; then
1599
+ err "tmux not installed"
1600
+ return 1
1601
+ fi
1602
+
1603
+ local target_args=()
1604
+ if [[ -n "$target_client" ]]; then
1605
+ target_args=(-c "$target_client")
1606
+ fi
1607
+
1608
+ local popup_command
1609
+ popup_command="$(cat <<'EOF'
1610
+ bash -lc '
1611
+ trap "exit 0" INT TERM
1612
+ printf "%s\n" \
1613
+ "Orchestra tmux shortcuts" \
1614
+ "" \
1615
+ "Ctrl+b, d Detach and return to Orchestra" \
1616
+ "Ctrl+b, r Rename the current Orchestra session" \
1617
+ "Ctrl+b, n New Orchestra session in this worktree" \
1618
+ "Ctrl+b, q Close current session and switch next" \
1619
+ "Ctrl+b, l List active sessions" \
1620
+ "" \
1621
+ "Ctrl+b, Left Previous Orchestra session in this workspace" \
1622
+ "Ctrl+b, Right Next Orchestra session in this workspace" \
1623
+ "Ctrl+b, < Previous Orchestra session in this workspace" \
1624
+ "Ctrl+b, > Next Orchestra session in this workspace" \
1625
+ "" \
1626
+ "Ctrl+b, [ Copy/scroll mode" \
1627
+ "Ctrl+b, h Show this help" \
1628
+ "Ctrl+b, ? Show this help" \
1629
+ "" \
1630
+ "Press any key to close..."
1631
+ IFS= read -rsn1 _ || true
1632
+ exit 0
1633
+ '
1634
+ EOF
1635
+ )"
1636
+
1637
+ tmux display-popup "${target_args[@]}" \
1638
+ -E \
1639
+ -w 84 \
1640
+ -h 22 \
1641
+ -s "fg=#c0caf5,bg=#1a1b26" \
1642
+ -S "fg=#7aa2f7,bg=#1a1b26" \
1643
+ -T "Orchestra shortcuts" \
1644
+ "$popup_command" >/dev/null 2>&1 || true
1645
+ }
1646
+
1647
+ # Show active Orchestra sessions in a native tmux menu for arrow-key selection.
1648
+ # Usage: tmux_show_workspace_session_menu <current_session> [client_tty]
1649
+ tmux_show_workspace_session_menu() {
1650
+ local current_session="$1"
1651
+ local target_client="${2:-}"
1652
+ local session_dir repo_root rows name display_name label target_command
1653
+ local menu_width=56
1654
+ local min_menu_rows=10
1655
+ local menu_row_count=0
1656
+
1657
+ if ! tmux_available; then
1658
+ err "tmux not installed"
1659
+ return 1
1660
+ fi
1661
+ if [[ -z "$current_session" ]]; then
1662
+ err "tmux_show_workspace_session_menu: current session required"
1663
+ return 1
1664
+ fi
1665
+
1666
+ session_dir="$(tmux display-message -t "$current_session" -p '#{pane_current_path}' 2>/dev/null || echo "")"
1667
+ repo_root="$(_tmux_shared_root_for_path "$session_dir" 2>/dev/null || true)"
1668
+ if [[ -z "$repo_root" ]]; then
1669
+ tmux display-message -d 2500 "Unable to determine Orchestra workspace" 2>/dev/null || true
1670
+ return 1
1671
+ fi
1672
+
1673
+ _tmux_registry_sync_active_workspace_sessions "$repo_root" >/dev/null 2>&1 || true
1674
+ rows="$(_tmux_workspace_session_rows "$current_session" "$repo_root" all 2>/dev/null || true)"
1675
+ if [[ -z "$rows" ]]; then
1676
+ tmux display-message -d 2500 "No active Orchestra sessions in this workspace" 2>/dev/null || true
1677
+ return 1
1678
+ fi
1679
+
1680
+ local target_args=()
1681
+ if [[ -n "$target_client" ]]; then
1682
+ target_args=(-c "$target_client")
1683
+ fi
1684
+
1685
+ local menu_items=()
1686
+ while IFS=$'\t' read -r name display_name; do
1687
+ [[ -n "$name" && "$name" != "__ellipsis__" ]] || continue
1688
+ if [[ -z "$display_name" ]]; then
1689
+ display_name="$(tmux_format_session_display "$name" without-timestamp)"
1690
+ fi
1691
+ label="$(_tmux_truncate_tab_label "$display_name" 42)"
1692
+ if [[ "$name" == "$current_session" ]]; then
1693
+ label="● $label"
1694
+ else
1695
+ label=" $label"
1696
+ fi
1697
+ label="$(_tmux_pad_menu_label "$label" "$menu_width")"
1698
+ label="$(_tmux_status_escape_text "$label")"
1699
+ target_command="switch-client -t \"$name\""
1700
+ menu_items+=("$label" "" "$target_command")
1701
+ menu_row_count=$((menu_row_count + 1))
1702
+ done <<< "$rows"
1703
+
1704
+ if [[ ${#menu_items[@]} -eq 0 ]]; then
1705
+ tmux display-message -d 2500 "No active Orchestra sessions in this workspace" 2>/dev/null || true
1706
+ return 1
1707
+ fi
1708
+
1709
+ while (( menu_row_count < min_menu_rows )); do
1710
+ menu_items+=("$(_tmux_pad_menu_label "" "$menu_width")" "" "")
1711
+ menu_row_count=$((menu_row_count + 1))
1712
+ done
1713
+
1714
+ tmux display-menu "${target_args[@]}" \
1715
+ -s "fg=#c0caf5,bg=#1a1b26" \
1716
+ -S "fg=#7aa2f7,bg=#1a1b26" \
1717
+ -H "fg=#1a1b26,bg=#ff9e64,bold" \
1718
+ -T "Orchestra sessions" \
1719
+ "${menu_items[@]}" >/dev/null 2>&1 || true
1720
+ }
1721
+
1722
+ # Find the adjacent active Orchestra session registered for the current repo.
1723
+ # Usage: tmux_workspace_cycle_target <current_session> <next|previous>
1724
+ tmux_workspace_cycle_target() {
1725
+ local current_session="$1"
1726
+ local direction="$2"
1727
+ local session_dir repo_root db_path active_file target query_status
1728
+
1729
+ if ! tmux_available; then
1730
+ err "tmux not installed"
1731
+ return 1
1732
+ fi
1733
+ if [[ -z "$current_session" ]]; then
1734
+ err "tmux_workspace_cycle_target: current session required"
1735
+ return 1
1736
+ fi
1737
+ case "$direction" in
1738
+ next|previous|prev) ;;
1739
+ *)
1740
+ err "tmux_workspace_cycle_target: direction must be next or previous"
1741
+ return 1
1742
+ ;;
1743
+ esac
1744
+
1745
+ db_path="$(_tmux_session_registry_path)"
1746
+ have_cmd python3 || {
1747
+ err "python3 is required to read the Orchestra session registry"
1748
+ return 1
1749
+ }
1750
+
1751
+ active_file="$(mktemp)"
1752
+ tmux list-sessions -F $'#{session_name}\t#{@orchestra_display_name}\t#{session_last_attached}\t#{session_activity}' > "$active_file" 2>/dev/null || {
1753
+ rm -f "$active_file"
1754
+ err "Unable to list tmux sessions"
1755
+ return 1
1756
+ }
1757
+
1758
+ if [[ -f "$db_path" ]]; then
1759
+ if target="$(_tmux_workspace_cycle_target_cached "$current_session" "$direction" "$db_path" "$active_file")"; then
1760
+ query_status=0
1761
+ else
1762
+ query_status=$?
1763
+ fi
1764
+ if [[ $query_status -eq 0 && -n "$target" ]]; then
1765
+ rm -f "$active_file"
1766
+ printf '%s\n' "$target"
1767
+ return 0
1768
+ fi
1769
+ if [[ $query_status -eq 3 ]]; then
1770
+ rm -f "$active_file"
1771
+ return 1
1772
+ fi
1773
+ fi
1774
+
1775
+ _tmux_registry_upsert_current_session "$current_session" >/dev/null 2>&1 || true
1776
+
1777
+ session_dir="$(tmux display-message -t "$current_session" -p '#{pane_current_path}' 2>/dev/null || echo "")"
1778
+ repo_root="$(_tmux_shared_root_for_path "$session_dir" 2>/dev/null || true)"
1779
+ if [[ -z "$repo_root" ]]; then
1780
+ rm -f "$active_file"
1781
+ err "Unable to determine Orchestra workspace for session"
1782
+ return 1
1783
+ fi
1784
+ _tmux_registry_sync_active_workspace_sessions "$repo_root" >/dev/null 2>&1 || true
1785
+
1786
+ if [[ ! -f "$db_path" ]]; then
1787
+ rm -f "$active_file"
1788
+ err "No Orchestra session registry found"
1789
+ return 1
1790
+ fi
1791
+
1792
+ if target="$(python3 - "$db_path" "$repo_root" "$current_session" "$direction" "$active_file" <<'PY'
1793
+ import sqlite3
1794
+ import sys
1795
+
1796
+ db_path, repo_root, current_session, direction, active_path = sys.argv[1:6]
1797
+ with open(active_path, "r", encoding="utf-8") as handle:
1798
+ active = {
1799
+ line.rstrip("\n").split("\t", 1)[0].strip()
1800
+ for line in handle
1801
+ if line.rstrip("\n").split("\t", 1)[0].strip()
1802
+ }
1803
+
1804
+ conn = sqlite3.connect(db_path)
1805
+ rows = conn.execute(
1806
+ """
1807
+ SELECT tmux_name
1808
+ FROM sessions
1809
+ WHERE repo_root = ?1
1810
+ ORDER BY worktree_path COLLATE NOCASE, created_at, tmux_name COLLATE NOCASE
1811
+ """,
1812
+ (repo_root,),
1813
+ ).fetchall()
1814
+
1815
+ names = []
1816
+ seen = set()
1817
+ for (name,) in rows:
1818
+ if name in active and name not in seen:
1819
+ names.append(name)
1820
+ seen.add(name)
1821
+
1822
+ if len(names) < 2 or current_session not in seen:
1823
+ raise SystemExit(0)
1824
+
1825
+ index = names.index(current_session)
1826
+ if direction == "next":
1827
+ index = (index + 1) % len(names)
1828
+ else:
1829
+ index = (index - 1) % len(names)
1830
+ print(names[index])
1831
+ PY
1832
+ )"; then
1833
+ query_status=0
1834
+ else
1835
+ query_status=$?
1836
+ fi
1837
+ rm -f "$active_file"
1838
+ if [[ $query_status -ne 0 ]]; then
1839
+ err "Unable to query Orchestra session registry"
1840
+ return 1
1841
+ fi
1842
+ [[ -n "$target" ]] || return 1
1843
+ printf '%s\n' "$target"
1844
+ }
1845
+
1846
+ # Switch the current tmux client to an adjacent registered Orchestra session.
1847
+ # Usage: tmux_cycle_workspace_session <current_session> <next|previous> [client_tty]
1848
+ tmux_cycle_workspace_session() {
1849
+ local current_session="$1"
1850
+ local direction="$2"
1851
+ local target_client="${3:-}"
1852
+ local target_session
1853
+
1854
+ if ! target_session="$(tmux_workspace_cycle_target "$current_session" "$direction")"; then
1855
+ tmux display-message -d 2500 "No other Orchestra sessions in this workspace" 2>/dev/null || true
1856
+ return 1
1857
+ fi
1858
+
1859
+ if [[ -n "$target_client" ]]; then
1860
+ tmux switch-client -c "$target_client" -t "$target_session" >/dev/null 2>&1 || return 1
1861
+ else
1862
+ tmux switch-client -t "$target_session" >/dev/null 2>&1 || return 1
1863
+ fi
1864
+
1865
+ {
1866
+ _tmux_refresh_orchestra_session_status "$target_session" >/dev/null 2>&1 || true
1867
+ } &
1868
+ }
1869
+
1870
+ # Load .env file if it exists
1871
+ # Usage: tmux_load_env_file [env_file_path]
1872
+ tmux_load_env_file() {
1873
+ local env_file="${1:-$PWD/.env}"
1874
+ if [[ -f "$env_file" ]]; then
1875
+ # Source the .env file, but only export ANTHROPIC_API_KEY
1876
+ set -a # Auto-export variables
1877
+ source "$env_file"
1878
+ set +a # Turn off auto-export
1879
+ fi
1880
+ }
1881
+
1882
+ # Load Anthropic API key from config file or fallback to .env files
1883
+ tmux_load_anthropic_api_key() {
1884
+ # First try to load from ~/.orchestra/config.json
1885
+ local config_file="$HOME/.orchestra/config.json"
1886
+ if [[ -f "$config_file" ]] && command -v jq >/dev/null 2>&1; then
1887
+ local api_key
1888
+ api_key="$(jq -r '.anthropic_api_key // empty' "$config_file" 2>/dev/null)"
1889
+ if [[ -n "$api_key" ]] && [[ "$api_key" != "null" ]]; then
1890
+ export ANTHROPIC_API_KEY="$api_key"
1891
+ return 0
1892
+ fi
1893
+ fi
1894
+
1895
+ # Fallback to .env file loading (existing logic)
1896
+ tmux_load_env_file "$PWD/.env"
1897
+
1898
+ # If still no API key, try repo root
1899
+ if [[ -z "${ANTHROPIC_API_KEY-}" ]]; then
1900
+ local root
1901
+ root="$(repo_root)"
1902
+ if [[ -n "$root" ]]; then
1903
+ tmux_load_env_file "$root/.env"
1904
+ fi
1905
+ fi
1906
+ }
1907
+
1908
+ # Load OpenAI API key from config file or fallback to .env files
1909
+ tmux_load_openai_api_key() {
1910
+ local config_file="$HOME/.orchestra/config.json"
1911
+ if [[ -f "$config_file" ]]; then
1912
+ local api_key=""
1913
+ if command -v jq >/dev/null 2>&1; then
1914
+ api_key="$(jq -r '.openai_api_key // empty' "$config_file" 2>/dev/null)"
1915
+ elif command -v python3 >/dev/null 2>&1; then
1916
+ api_key="$(python3 - "$config_file" <<'PY'
1917
+ import json
1918
+ import sys
1919
+ try:
1920
+ data = json.load(open(sys.argv[1], 'r'))
1921
+ except Exception:
1922
+ data = {}
1923
+ value = data.get('openai_api_key') or ''
1924
+ print(value)
1925
+ PY
1926
+ )"
1927
+ elif command -v node >/dev/null 2>&1; then
1928
+ api_key="$(node - "$config_file" <<'NODE'
1929
+ const fs = require('fs');
1930
+ let value = '';
1931
+ try {
1932
+ const data = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
1933
+ value = data.openai_api_key || '';
1934
+ } catch (err) {
1935
+ value = '';
1936
+ }
1937
+ process.stdout.write(value);
1938
+ NODE
1939
+ )"
1940
+ fi
1941
+ if [[ -n "$api_key" ]] && [[ "$api_key" != "null" ]]; then
1942
+ export OPENAI_API_KEY="$api_key"
1943
+ return 0
1944
+ fi
1945
+ fi
1946
+
1947
+ tmux_load_env_file "$PWD/.env"
1948
+ if [[ -z "${OPENAI_API_KEY-}" ]]; then
1949
+ local root
1950
+ root="$(repo_root)"
1951
+ if [[ -n "$root" ]]; then
1952
+ tmux_load_env_file "$root/.env"
1953
+ fi
1954
+ fi
1955
+ }
1956
+
1957
+ # Load primary AI provider from config (defaults to anthropic)
1958
+ tmux_load_ai_primary_provider() {
1959
+ if [[ -n "${AI_PRIMARY_PROVIDER-}" ]]; then
1960
+ return 0
1961
+ fi
1962
+ local config_file="$HOME/.orchestra/config.json"
1963
+ local provider=""
1964
+ if [[ -f "$config_file" ]]; then
1965
+ if command -v jq >/dev/null 2>&1; then
1966
+ provider="$(jq -r '.ai_primary_provider // empty' "$config_file" 2>/dev/null)"
1967
+ elif command -v python3 >/dev/null 2>&1; then
1968
+ provider="$(python3 - "$config_file" <<'PY'
1969
+ import json
1970
+ import sys
1971
+ try:
1972
+ data = json.load(open(sys.argv[1], 'r'))
1973
+ except Exception:
1974
+ data = {}
1975
+ value = data.get('ai_primary_provider') or ''
1976
+ print(value)
1977
+ PY
1978
+ )"
1979
+ elif command -v node >/dev/null 2>&1; then
1980
+ provider="$(node - "$config_file" <<'NODE'
1981
+ const fs = require('fs');
1982
+ let value = '';
1983
+ try {
1984
+ const data = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
1985
+ value = data.ai_primary_provider || '';
1986
+ } catch (err) {
1987
+ value = '';
1988
+ }
1989
+ process.stdout.write(value);
1990
+ NODE
1991
+ )"
1992
+ fi
1993
+ fi
1994
+ provider="$(printf '%s' "$provider" | tr '[:upper:]' '[:lower:]')"
1995
+ if [[ "$provider" != "openai" && "$provider" != "anthropic" ]]; then
1996
+ provider="anthropic"
1997
+ fi
1998
+ export AI_PRIMARY_PROVIDER="$provider"
1999
+ }
2000
+
2001
+ # Load OpenAI model from config (defaults to gpt-4o-mini)
2002
+ tmux_load_openai_model() {
2003
+ if [[ -n "${OPENAI_MODEL-}" ]]; then
2004
+ return 0
2005
+ fi
2006
+ local config_file="$HOME/.orchestra/config.json"
2007
+ local model=""
2008
+ if [[ -f "$config_file" ]]; then
2009
+ if command -v jq >/dev/null 2>&1; then
2010
+ model="$(jq -r '.openai_model // empty' "$config_file" 2>/dev/null)"
2011
+ elif command -v python3 >/dev/null 2>&1; then
2012
+ model="$(python3 - "$config_file" <<'PY'
2013
+ import json
2014
+ import sys
2015
+ try:
2016
+ data = json.load(open(sys.argv[1], 'r'))
2017
+ except Exception:
2018
+ data = {}
2019
+ value = data.get('openai_model') or ''
2020
+ print(value)
2021
+ PY
2022
+ )"
2023
+ elif command -v node >/dev/null 2>&1; then
2024
+ model="$(node - "$config_file" <<'NODE'
2025
+ const fs = require('fs');
2026
+ let value = '';
2027
+ try {
2028
+ const data = JSON.parse(fs.readFileSync(process.argv[2], 'utf8'));
2029
+ value = data.openai_model || '';
2030
+ } catch (err) {
2031
+ value = '';
2032
+ }
2033
+ process.stdout.write(value);
2034
+ NODE
2035
+ )"
2036
+ fi
2037
+ fi
2038
+ if [[ -z "$model" || "$model" == "null" ]]; then
2039
+ model="gpt-4o-mini"
2040
+ fi
2041
+ export OPENAI_MODEL="$model"
2042
+ }
2043
+
2044
+ # Generate a descriptive name for a tmux session using AI
2045
+ # Usage: tmux_generate_ai_session_name <session_name>
2046
+ tmux_generate_ai_session_name() {
2047
+ local session="$1"
2048
+
2049
+ # Load AI provider config and API keys
2050
+ tmux_load_ai_primary_provider
2051
+ tmux_load_openai_model
2052
+ tmux_load_anthropic_api_key
2053
+ tmux_load_openai_api_key
2054
+
2055
+ local primary="${AI_PRIMARY_PROVIDER:-anthropic}"
2056
+ primary="$(printf '%s' "$primary" | tr '[:upper:]' '[:lower:]')"
2057
+ local provider=""
2058
+ if [[ "$primary" == "openai" ]]; then
2059
+ if [[ -n "${OPENAI_API_KEY-}" ]]; then
2060
+ provider="openai"
2061
+ elif [[ -n "${ANTHROPIC_API_KEY-}" ]]; then
2062
+ provider="anthropic"
2063
+ fi
2064
+ else
2065
+ if [[ -n "${ANTHROPIC_API_KEY-}" ]]; then
2066
+ provider="anthropic"
2067
+ elif [[ -n "${OPENAI_API_KEY-}" ]]; then
2068
+ provider="openai"
2069
+ fi
2070
+ fi
2071
+
2072
+ if [[ -z "$provider" ]]; then
2073
+ err "No AI API key found in config file or .env file"
2074
+ return 1
2075
+ fi
2076
+
2077
+ local openai_model="${OPENAI_MODEL:-gpt-4o-mini}"
2078
+
2079
+ local pane_cmd pane_mode window_name pane_title alternate_on mouse_any_flag
2080
+ pane_cmd="$(tmux display-message -t "$session" -p '#{pane_current_command}' 2>/dev/null || echo "")"
2081
+ pane_mode="$(tmux display-message -t "$session" -p '#{pane_mode}' 2>/dev/null || echo "")"
2082
+ window_name="$(tmux display-message -t "$session" -p '#{window_name}' 2>/dev/null || echo "")"
2083
+ pane_title="$(tmux display-message -t "$session" -p '#{pane_title}' 2>/dev/null || echo "")"
2084
+ alternate_on="$(tmux display-message -t "$session" -p '#{alternate_on}' 2>/dev/null || echo "0")"
2085
+ mouse_any_flag="$(tmux display-message -t "$session" -p '#{mouse_any_flag}' 2>/dev/null || echo "0")"
2086
+
2087
+ local history_temp=""
2088
+ local history_arg=""
2089
+ local history_dir="${ORCHESTRA_HISTORY_DIR:-$HOME/.orchestra/history}"
2090
+ local pane_id
2091
+ pane_id="$(tmux display-message -t "$session" -p '#{pane_id}' 2>/dev/null || echo "")"
2092
+ local pane_key=""
2093
+ if [[ -n "$pane_id" ]]; then
2094
+ pane_key="$(_orchestra_history_key "$pane_id")"
2095
+ fi
2096
+ local session_key
2097
+ session_key="$(_orchestra_history_key "$session")"
2098
+
2099
+ local is_tui="false"
2100
+ if _tmux_is_tui_context "$pane_cmd" "$alternate_on" "$mouse_any_flag" "$pane_mode" "$window_name" "$pane_title"; then
2101
+ is_tui="true"
2102
+ fi
2103
+
2104
+ local content=""
2105
+ local visible_content=""
2106
+ if [[ "$is_tui" == "true" ]]; then
2107
+ visible_content="$(tmux capture-pane -e -p -q -a -t "${pane_id:-$session}" 2>/dev/null || echo "")"
2108
+ fi
2109
+ local scrollback_content
2110
+ scrollback_content="$(tmux capture-pane -t "$session" -p -S -200 2>/dev/null)" || {
2111
+ err "Failed to capture tmux pane content"
2112
+ return 1
2113
+ }
2114
+ if [[ -n "$visible_content" ]]; then
2115
+ content="### Visible terminal view\n${visible_content}\n\n### Recent scrollback\n${scrollback_content}"
2116
+ else
2117
+ content="$scrollback_content"
2118
+ fi
2119
+
2120
+ local history_path=""
2121
+ if [[ -n "$pane_key" && -f "$history_dir/$pane_key.log" ]]; then
2122
+ history_path="$history_dir/$pane_key.log"
2123
+ elif [[ -n "$session_key" && -f "$history_dir/$session_key.log" ]]; then
2124
+ history_path="$history_dir/$session_key.log"
2125
+ fi
2126
+
2127
+ if [[ -n "$history_path" ]]; then
2128
+ if [[ "$OSTYPE" == "darwin"* ]]; then
2129
+ history_temp=$(mktemp -t orchestra_hist)
2130
+ else
2131
+ history_temp=$(mktemp)
2132
+ fi
2133
+ tail -n 50 "$history_path" > "$history_temp"
2134
+ history_arg="$history_temp"
2135
+ fi
2136
+
2137
+ local metadata_block=""
2138
+ metadata_block+="Session metadata:\n"
2139
+ metadata_block+="- pane_current_command: ${pane_cmd:-unknown}\n"
2140
+ metadata_block+="- window_name: ${window_name:-}\n"
2141
+ metadata_block+="- pane_title: ${pane_title:-}\n"
2142
+ metadata_block+="- pane_mode: ${pane_mode:-}\n"
2143
+ metadata_block+="- alternate_screen_active: ${alternate_on:-0}\n"
2144
+ metadata_block+="- mouse_mode_active: ${mouse_any_flag:-0}\n"
2145
+ metadata_block+="- likely_tui_app: ${is_tui}\n"
2146
+
2147
+ # If content is empty or too short, keep a placeholder
2148
+ if [[ ${#content} -lt 10 ]]; then
2149
+ content="(no terminal output captured)"
2150
+ fi
2151
+
2152
+ # Truncate content if too long (to stay within token limits)
2153
+ if [[ ${#content} -gt 8000 ]]; then
2154
+ content="${content: -8000}"
2155
+ fi
2156
+
2157
+ # Create a temporary file for the content to avoid escaping issues
2158
+ # mktemp works differently on macOS vs Linux
2159
+ local temp_file
2160
+ if [[ "$OSTYPE" == "darwin"* ]]; then
2161
+ temp_file=$(mktemp -t orchestra_tmp)
2162
+ else
2163
+ temp_file=$(mktemp)
2164
+ fi
2165
+ printf '%s' "$content" > "$temp_file"
2166
+
2167
+ # Prepare the API request using Python for proper JSON encoding
2168
+ local request_body=""
2169
+ if have_cmd python3; then
2170
+ # Use Python to safely build the request body and extract typed commands from the capture
2171
+ request_payload=$(
2172
+ python3 - "$temp_file" "$history_arg" "$provider" "$openai_model" "$pane_cmd" "$window_name" "$pane_title" "$pane_mode" "$alternate_on" "$mouse_any_flag" "$is_tui" 2>/dev/null <<'PYCODE'
2173
+ import json
2174
+ import re
2175
+ import sys
2176
+ from pathlib import Path
2177
+
2178
+ temp_path = Path(sys.argv[1])
2179
+ content = temp_path.read_text()
2180
+ history_arg = sys.argv[2] if len(sys.argv) > 2 else ""
2181
+ provider = sys.argv[3] if len(sys.argv) > 3 else "anthropic"
2182
+ openai_model = sys.argv[4] if len(sys.argv) > 4 and sys.argv[4] else "gpt-4o-mini"
2183
+ pane_cmd = sys.argv[5] if len(sys.argv) > 5 else ""
2184
+ window_name = sys.argv[6] if len(sys.argv) > 6 else ""
2185
+ pane_title = sys.argv[7] if len(sys.argv) > 7 else ""
2186
+ pane_mode = sys.argv[8] if len(sys.argv) > 8 else ""
2187
+ alternate_on = sys.argv[9] if len(sys.argv) > 9 else "0"
2188
+ mouse_any_flag = sys.argv[10] if len(sys.argv) > 10 else "0"
2189
+ is_tui = sys.argv[11] if len(sys.argv) > 11 else "false"
2190
+
2191
+ lines = content.splitlines()
2192
+ command_candidates = [] # Commands captured with high confidence
2193
+ fallback_candidates = [] # All prompt-like lines (used if we fail to classify)
2194
+
2195
+ history_commands = []
2196
+ if history_arg:
2197
+ hist_path = Path(history_arg)
2198
+ if hist_path.exists():
2199
+ for line in hist_path.read_text().splitlines():
2200
+ line = line.strip()
2201
+ if not line:
2202
+ continue
2203
+ if "\t" in line:
2204
+ history_commands.append(line.split("\t", 1)[1])
2205
+ else:
2206
+ history_commands.append(line)
2207
+ history_commands = history_commands[-50:]
2208
+
2209
+ prompt_pattern = re.compile(r"^[A-Za-z0-9_.@~/-]+$")
2210
+ prompt_sigil_pattern = re.compile(r"^(?P<prompt>[^\t]{0,120}?)(?P<sigil>[$#%])\s+(?P<cmd>.+)$")
2211
+ simple_sigil_pattern = re.compile(r"^(?P<sigil>[$#%])\s+(?P<cmd>.+)$")
2212
+ disallowed_prompts = {
2213
+ "warning",
2214
+ "error",
2215
+ "fatal",
2216
+ "hint",
2217
+ "note",
2218
+ "usage",
2219
+ "info",
2220
+ "debug",
2221
+ "trace",
2222
+ }
2223
+
2224
+ disallowed_first_tokens = {
2225
+ "warning",
2226
+ "error",
2227
+ "fatal",
2228
+ "hint",
2229
+ "note",
2230
+ "usage",
2231
+ "not",
2232
+ "see",
2233
+ "for",
2234
+ "from",
2235
+ "with",
2236
+ "and",
2237
+ "or",
2238
+ "but",
2239
+ "at",
2240
+ "to",
2241
+ "in",
2242
+ "info",
2243
+ "debug",
2244
+ "trace",
2245
+ }
2246
+
2247
+ common_commands = {
2248
+ "git",
2249
+ "gh",
2250
+ "npm",
2251
+ "pnpm",
2252
+ "yarn",
2253
+ "node",
2254
+ "npx",
2255
+ "bun",
2256
+ "cargo",
2257
+ "go",
2258
+ "python",
2259
+ "python3",
2260
+ "pip",
2261
+ "pip3",
2262
+ "poetry",
2263
+ "pipenv",
2264
+ "pytest",
2265
+ "uvicorn",
2266
+ "gunicorn",
2267
+ "flask",
2268
+ "django-admin",
2269
+ "rails",
2270
+ "bundle",
2271
+ "rake",
2272
+ "mix",
2273
+ "make",
2274
+ "cmake",
2275
+ "gradle",
2276
+ "mvn",
2277
+ "ant",
2278
+ "docker",
2279
+ "docker-compose",
2280
+ "kubectl",
2281
+ "helm",
2282
+ "terraform",
2283
+ "ansible",
2284
+ "ssh",
2285
+ "scp",
2286
+ "rsync",
2287
+ "sftp",
2288
+ "psql",
2289
+ "mysql",
2290
+ "mongo",
2291
+ "redis-cli",
2292
+ "sqlite3",
2293
+ "composer",
2294
+ "php",
2295
+ "ruby",
2296
+ "java",
2297
+ "javac",
2298
+ "deno",
2299
+ "dotnet",
2300
+ "msbuild",
2301
+ "tsc",
2302
+ "nx",
2303
+ "lerna",
2304
+ "eslint",
2305
+ "prettier",
2306
+ "ls",
2307
+ "cd",
2308
+ "pwd",
2309
+ "cat",
2310
+ "tail",
2311
+ "head",
2312
+ "less",
2313
+ "more",
2314
+ "grep",
2315
+ "rg",
2316
+ "fd",
2317
+ "find",
2318
+ "watch",
2319
+ "code",
2320
+ "open",
2321
+ "vim",
2322
+ "nvim",
2323
+ "tmux",
2324
+ "htop",
2325
+ "top",
2326
+ "brew",
2327
+ "tox",
2328
+ }
2329
+
2330
+ allowed_prefixes = (
2331
+ "./",
2332
+ "../",
2333
+ "~/",
2334
+ "bin/",
2335
+ "sbin/",
2336
+ )
2337
+
2338
+
2339
+ def clean_tokens(tokens):
2340
+ while tokens and tokens[0] in {"$", "#", "%"}:
2341
+ tokens = tokens[1:]
2342
+ while tokens and (tokens[0].endswith("$") or tokens[0].endswith("#") or tokens[0].endswith("%")):
2343
+ tokens = tokens[1:]
2344
+ return tokens
2345
+
2346
+
2347
+ def extract_prompt_and_command(raw_line):
2348
+ stripped = raw_line.strip()
2349
+ if not stripped:
2350
+ return "", ""
2351
+
2352
+ # Pattern 1: host:path:command (common bash/zsh prompts)
2353
+ if ":" in stripped:
2354
+ maybe_prompt, maybe_cmd = stripped.split(":", 1)
2355
+ maybe_prompt = maybe_prompt.strip().rstrip("#$%")
2356
+ maybe_cmd = maybe_cmd.strip()
2357
+ if maybe_prompt and maybe_cmd and prompt_pattern.match(maybe_prompt):
2358
+ prompt_lower = maybe_prompt.lower()
2359
+ if prompt_lower not in disallowed_prompts:
2360
+ return maybe_prompt, maybe_cmd
2361
+
2362
+ # Pattern 2: prompt ending with $, #, % (supports prompts without ':')
2363
+ match = prompt_sigil_pattern.match(stripped)
2364
+ if match:
2365
+ maybe_prompt = (match.group("prompt") or "").strip().rstrip("#$%")
2366
+ maybe_cmd = (match.group("cmd") or "").strip()
2367
+ if maybe_cmd:
2368
+ if maybe_prompt and maybe_prompt.lower() in disallowed_prompts:
2369
+ return "", ""
2370
+ return (maybe_prompt if maybe_prompt else match.group("sigil")), maybe_cmd
2371
+
2372
+ # Pattern 3: bare prompt lines like "$ git status"
2373
+ match = simple_sigil_pattern.match(stripped)
2374
+ if match:
2375
+ maybe_cmd = (match.group("cmd") or "").strip()
2376
+ if maybe_cmd:
2377
+ return match.group("sigil"), maybe_cmd
2378
+
2379
+ return "", ""
2380
+
2381
+
2382
+ for raw_line in lines:
2383
+ prompt_part, command_part = extract_prompt_and_command(raw_line)
2384
+ if not command_part:
2385
+ continue
2386
+
2387
+ tokens = clean_tokens(command_part.split())
2388
+ if not tokens:
2389
+ continue
2390
+
2391
+ first_token = tokens[0].lower()
2392
+ if first_token == "sudo" and len(tokens) > 1:
2393
+ first_token = tokens[1].lower()
2394
+
2395
+ if first_token in disallowed_first_tokens:
2396
+ continue
2397
+
2398
+ normalized_command = " ".join(tokens)
2399
+ formatted_line = f"{prompt_part}:{normalized_command}"
2400
+ fallback_candidates.append(formatted_line)
2401
+
2402
+ allowed = (
2403
+ first_token in common_commands
2404
+ or normalized_command.startswith(allowed_prefixes)
2405
+ or any(
2406
+ first_token.startswith(prefix)
2407
+ for prefix in (
2408
+ "git",
2409
+ "npm",
2410
+ "pnpm",
2411
+ "yarn",
2412
+ "node",
2413
+ "npx",
2414
+ "bun",
2415
+ "cargo",
2416
+ "python",
2417
+ "pip",
2418
+ "poetry",
2419
+ "pytest",
2420
+ "uvicorn",
2421
+ "docker",
2422
+ "kubectl",
2423
+ "helm",
2424
+ "terraform",
2425
+ "ansible",
2426
+ "ssh",
2427
+ "scp",
2428
+ "rsync",
2429
+ "rails",
2430
+ "bundle",
2431
+ "rake",
2432
+ "mix",
2433
+ "psql",
2434
+ "mysql",
2435
+ "mongo",
2436
+ "redis",
2437
+ )
2438
+ )
2439
+ )
2440
+
2441
+ if allowed:
2442
+ command_candidates.append(formatted_line)
2443
+
2444
+
2445
+ if history_commands:
2446
+ command_candidates = history_commands
2447
+ elif not command_candidates:
2448
+ command_candidates = fallback_candidates
2449
+
2450
+ extracted_commands = command_candidates
2451
+ history_lines = [f"{idx + 1}. {line}" for idx, line in enumerate(extracted_commands)]
2452
+ command_history = "\n".join(history_lines) if history_lines else "None detected."
2453
+ last_command_line = extracted_commands[-1] if extracted_commands else ""
2454
+ commands_detected = bool(extracted_commands)
2455
+
2456
+ # Capture the first 10 lines of output as fallback context (joined by newline)
2457
+ first_ten_lines = "\n".join(lines[:10]) if lines else ""
2458
+
2459
+ description_guidance = """Description rules:
2460
+ - Describe the primary activity in a short phrase (3-8 words).
2461
+ - Do NOT include any app/tool name or prefix in the output.
2462
+ - If git activity is visible, the description should reflect it (e.g., reviewing changes), but do not add 'git_'."""
2463
+
2464
+ metadata_summary = "\n".join(
2465
+ line
2466
+ for line in [
2467
+ f"pane_current_command: {pane_cmd}" if pane_cmd else "",
2468
+ f"window_name: {window_name}" if window_name else "",
2469
+ f"pane_title: {pane_title}" if pane_title else "",
2470
+ f"pane_mode: {pane_mode}" if pane_mode else "",
2471
+ f"alternate_screen_active: {alternate_on}",
2472
+ f"mouse_mode_active: {mouse_any_flag}",
2473
+ f"likely_tui_app: {is_tui}",
2474
+ ]
2475
+ if line
2476
+ )
2477
+
2478
+ prompt = f"""Analyze this terminal session and create a descriptive tmux session name based on what the user actually did.
2479
+
2480
+ Terminal output (last capture):
2481
+ {content}
2482
+
2483
+ Tmux metadata (use as supporting hints, not as the primary source of truth):
2484
+ {metadata_summary}
2485
+
2486
+ Extracted command history (oldest to newest):
2487
+ {command_history}
2488
+
2489
+ Most recent command (anchor for description and summary):
2490
+ {last_command_line if last_command_line else 'None detected'}
2491
+
2492
+ Focus: produce a concise description of the activity. Follow these rules:
2493
+ {description_guidance}
2494
+
2495
+ {'No clear prompt/command lines were detected. Rely on the terminal output context.' if not commands_detected else ''}
2496
+
2497
+ {'An interactive TUI is likely running. Prefer the visible screen content plus recent command history over generic app names.' if is_tui.lower() in {'1', 'true', 'yes', 'on'} else ''}
2498
+
2499
+ {'First 10 lines of terminal output (useful for identifying running app/service):\n' + first_ten_lines if not commands_detected and first_ten_lines else ''}
2500
+
2501
+ If no commands were detected, fall back to terminal output analysis but describe the main activity in the name. Identify the dominant application or process implied by the output (e.g., web server, test runner, build tool).
2502
+
2503
+ Instructions:
2504
+ 1. Produce a session name using lowercase letters, numbers, and underscores only.
2505
+ 2. Maximum length: 100 characters.
2506
+ 3. Do NOT include any app/tool name or prefix.
2507
+ 4. Describe the concrete task or state in a short phrase.
2508
+ 5. If git activity is visible anywhere, the description must reflect git activity.
2509
+ 6. Respond with ONLY the final session name, nothing else."""
2510
+
2511
+ if provider == "openai":
2512
+ request = {
2513
+ "model": openai_model,
2514
+ "max_tokens": 100,
2515
+ "temperature": 0.2,
2516
+ "messages": [
2517
+ {
2518
+ "role": "user",
2519
+ "content": prompt
2520
+ }
2521
+ ]
2522
+ }
2523
+ else:
2524
+ request = {
2525
+ "model": "claude-3-5-haiku-latest",
2526
+ "max_tokens": 100,
2527
+ "messages": [
2528
+ {
2529
+ "role": "user",
2530
+ "content": prompt
2531
+ }
2532
+ ]
2533
+ }
2534
+
2535
+ print(json.dumps(request))
2536
+ PYCODE
2537
+ )
2538
+ request_body="$request_payload"
2539
+ fi
2540
+ if [[ -z "$request_body" ]]; then
2541
+ # Fallback: base64 encode the content to avoid escaping issues
2542
+ local encoded_content
2543
+ local fallback_file
2544
+ if [[ "$OSTYPE" == "darwin"* ]]; then
2545
+ fallback_file=$(mktemp -t orchestra_fb)
2546
+ else
2547
+ fallback_file=$(mktemp)
2548
+ fi
2549
+ {
2550
+ printf '%b\n\n' "$metadata_block"
2551
+ cat "$temp_file"
2552
+ } > "$fallback_file"
2553
+ encoded_content=$(base64 < "$fallback_file" | tr -d '\n')
2554
+ rm -f "$fallback_file"
2555
+ if [[ "$provider" == "openai" ]]; then
2556
+ request_body=$(cat <<EOF
2557
+ {
2558
+ "model": "$openai_model",
2559
+ "max_tokens": 100,
2560
+ "temperature": 0.2,
2561
+ "messages": [
2562
+ {
2563
+ "role": "user",
2564
+ "content": "I'll send you base64-encoded terminal output. Please decode it, analyze it, and create a descriptive name for this tmux session.\n\nIMPORTANT: Use terminal metadata only as supporting hints. Prioritize the visible terminal content and recent command history.\n\nCRITICAL: Check for Git patterns FIRST:\n- Git commands: git status, diff, add, commit, push, pull, checkout, branch, merge, rebase, log, stash\n- Git output: 'On branch', 'Changes not staged', 'modified:', 'new file:', diff output (+/-/@@), commit hashes\n- If ANY Git patterns found, the description should reflect git activity (but do not add 'git_').\n\nDESCRIPTION ONLY: Return a short description of the activity.\n- Do NOT include the app/tool name or any prefix in the output.\n\nUse underscores only. Max 100 chars total.\n\nExamples:\n- reviewing_changes_before_commit\n- resolving_merge_conflicts\n- fixing_auth_bug\n- running_nextjs_dev_server\n\nBase64 terminal output:\n${encoded_content}\n\nRespond with ONLY the session name, nothing else."
2565
+ }
2566
+ ]
2567
+ }
2568
+ EOF
2569
+ )
2570
+ else
2571
+ request_body=$(cat <<EOF
2572
+ {
2573
+ "model": "claude-3-5-haiku-latest",
2574
+ "max_tokens": 100,
2575
+ "messages": [
2576
+ {
2577
+ "role": "user",
2578
+ "content": "I'll send you base64-encoded terminal output. Please decode it, analyze it, and create a descriptive name for this tmux session.\n\nIMPORTANT: Use terminal metadata only as supporting hints. Prioritize the visible terminal content and recent command history.\n\nCRITICAL: Check for Git patterns FIRST:\n- Git commands: git status, diff, add, commit, push, pull, checkout, branch, merge, rebase, log, stash\n- Git output: 'On branch', 'Changes not staged', 'modified:', 'new file:', diff output (+/-/@@), commit hashes\n- If ANY Git patterns found, the description should reflect git activity (but do not add 'git_').\n\nDESCRIPTION ONLY: Return a short description of the activity.\n- Do NOT include the app/tool name or any prefix in the output.\n\nUse underscores only. Max 100 chars total.\n\nExamples:\n- reviewing_changes_before_commit\n- resolving_merge_conflicts\n- fixing_auth_bug\n- running_nextjs_dev_server\n\nBase64 terminal output:\n${encoded_content}\n\nRespond with ONLY the session name, nothing else."
2579
+ }
2580
+ ]
2581
+ }
2582
+ EOF
2583
+ )
2584
+ fi
2585
+ fi
2586
+
2587
+ # Make the API call
2588
+ local response
2589
+ local new_name=""
2590
+ if [[ "$provider" == "openai" ]]; then
2591
+ # First attempt: OpenAI Responses API + Files API for structured app/description output.
2592
+ # If this path fails, we fall back to chat completions.
2593
+ local context_file=""
2594
+ if [[ "$OSTYPE" == "darwin"* ]]; then
2595
+ context_file=$(mktemp -t orchestra_ctx)
2596
+ else
2597
+ context_file=$(mktemp)
2598
+ fi
2599
+ {
2600
+ printf '%b\n\n' "$metadata_block"
2601
+ printf 'TMUX SESSION CAPTURE\n\n'
2602
+ cat "$temp_file"
2603
+ if [[ -n "$history_arg" && -f "$history_arg" ]]; then
2604
+ printf '\n\nRECENT COMMAND HISTORY\n\n'
2605
+ cat "$history_arg"
2606
+ fi
2607
+ } > "$context_file"
2608
+
2609
+ local file_upload_resp openai_file_id
2610
+ file_upload_resp=$(curl -s -X POST https://api.openai.com/v1/files \
2611
+ --max-time 20 \
2612
+ -H "Authorization: Bearer $OPENAI_API_KEY" \
2613
+ -F purpose="user_data" \
2614
+ -F "file=@$context_file;type=text/plain" 2>/dev/null)
2615
+ rm -f "$context_file"
2616
+
2617
+ openai_file_id=""
2618
+ if have_cmd python3; then
2619
+ openai_file_id="$(python3 -c 'import json,sys
2620
+ try:
2621
+ data = json.load(sys.stdin)
2622
+ except Exception:
2623
+ data = {}
2624
+ print(data.get("id", ""))' <<<"$file_upload_resp" 2>/dev/null || true)"
2625
+ fi
2626
+
2627
+ if [[ -n "$openai_file_id" ]]; then
2628
+ local responses_body responses_resp ai_desc
2629
+ responses_body=$(python3 - "$openai_model" "$openai_file_id" <<'PYCODE'
2630
+ import json
2631
+ import sys
2632
+
2633
+ model = sys.argv[1]
2634
+ file_id = sys.argv[2]
2635
+
2636
+ prompt = """Analyze the provided terminal session file and produce ONLY a short activity description.
2637
+
2638
+ Return STRICT JSON in this exact shape:
2639
+ {"description":"<short_snake_case_description>"}
2640
+
2641
+ Rules:
2642
+ - description: concise snake_case description of the activity
2643
+ - do NOT include app/tool name or any prefix in the description
2644
+ - do not include punctuation beyond underscores
2645
+ - keep it short and specific
2646
+ """
2647
+
2648
+ req = {
2649
+ "model": model,
2650
+ "temperature": 0.2,
2651
+ "max_output_tokens": 180,
2652
+ "input": [
2653
+ {
2654
+ "role": "user",
2655
+ "content": [
2656
+ {"type": "input_text", "text": prompt},
2657
+ {"type": "input_file", "file_id": file_id},
2658
+ ],
2659
+ }
2660
+ ],
2661
+ }
2662
+ print(json.dumps(req))
2663
+ PYCODE
2664
+ )
2665
+
2666
+ responses_resp=$(curl -s -X POST https://api.openai.com/v1/responses \
2667
+ --max-time 20 \
2668
+ -H "Content-Type: application/json" \
2669
+ -H "Authorization: Bearer $OPENAI_API_KEY" \
2670
+ -d "$responses_body" 2>/dev/null)
2671
+
2672
+ # Best-effort cleanup of uploaded file
2673
+ curl -s -X DELETE "https://api.openai.com/v1/files/$openai_file_id" \
2674
+ -H "Authorization: Bearer $OPENAI_API_KEY" >/dev/null 2>&1 || true
2675
+
2676
+ ai_desc="$(python3 -c 'import json
2677
+ import re
2678
+ import sys
2679
+
2680
+ def clean(value):
2681
+ value = (value or "").strip().lower()
2682
+ value = re.sub(r"[^a-z0-9_ -]+", "", value)
2683
+ value = value.replace(" ", "_")
2684
+ value = re.sub(r"_+", "_", value).strip("_")
2685
+ return value
2686
+
2687
+ try:
2688
+ data = json.load(sys.stdin)
2689
+ except Exception:
2690
+ print("")
2691
+ raise SystemExit
2692
+
2693
+ text = ""
2694
+ if isinstance(data.get("output_text"), str):
2695
+ text = data["output_text"]
2696
+
2697
+ if not text:
2698
+ for item in data.get("output", []):
2699
+ if not isinstance(item, dict):
2700
+ continue
2701
+ if item.get("type") != "message":
2702
+ continue
2703
+ for content in item.get("content", []):
2704
+ if not isinstance(content, dict):
2705
+ continue
2706
+ if isinstance(content.get("text"), str):
2707
+ text += content["text"]
2708
+
2709
+ obj = None
2710
+ if text:
2711
+ text = text.strip()
2712
+ try:
2713
+ obj = json.loads(text)
2714
+ except Exception:
2715
+ match = re.search(r"\{.*\}", text, re.S)
2716
+ if match:
2717
+ try:
2718
+ obj = json.loads(match.group(0))
2719
+ except Exception:
2720
+ obj = None
2721
+
2722
+ description = ""
2723
+ if isinstance(obj, dict):
2724
+ description = clean(str(obj.get("description", "")))
2725
+
2726
+ if not description and text:
2727
+ description = clean(text)
2728
+
2729
+ print(description)' <<<"$responses_resp" 2>/dev/null || true)"
2730
+
2731
+ if [[ -n "$ai_desc" ]]; then
2732
+ new_name="$ai_desc"
2733
+ fi
2734
+ fi
2735
+
2736
+ if [[ -z "$new_name" ]]; then
2737
+ # Fallback: existing chat completions path
2738
+ response=$(curl -s -X POST https://api.openai.com/v1/chat/completions \
2739
+ --max-time 20 \
2740
+ -H "Content-Type: application/json" \
2741
+ -H "Authorization: Bearer $OPENAI_API_KEY" \
2742
+ -d "$request_body" 2>&1) || {
2743
+ err "Failed to call OpenAI API: $response"
2744
+ return 1
2745
+ }
2746
+
2747
+ if echo "$response" | grep -q '"error"'; then
2748
+ local error_msg=$(echo "$response" | python3 -c "import sys, json; print(json.load(sys.stdin).get('error', {}).get('message', 'Unknown error'))" 2>/dev/null || echo "API error")
2749
+ err "API error: $error_msg"
2750
+ return 1
2751
+ fi
2752
+
2753
+ new_name=$(echo "$response" | python3 -c "import sys, json; data = json.load(sys.stdin); print(data.get('choices', [{}])[0].get('message', {}).get('content', ''))" 2>/dev/null) || {
2754
+ new_name=$(echo "$response" | grep -o '"content":"[^"]*"' | head -1 | sed 's/"content":"\([^"]*\)"/\1/')
2755
+ }
2756
+ fi
2757
+ else
2758
+ response=$(curl -s -X POST https://api.anthropic.com/v1/messages \
2759
+ --max-time 20 \
2760
+ -H "Content-Type: application/json" \
2761
+ -H "x-api-key: $ANTHROPIC_API_KEY" \
2762
+ -H "anthropic-version: 2023-06-01" \
2763
+ -d "$request_body" 2>&1) || {
2764
+ err "Failed to call Anthropic API: $response"
2765
+ return 1
2766
+ }
2767
+
2768
+ if echo "$response" | grep -q '"error"'; then
2769
+ local error_msg=$(echo "$response" | python3 -c "import sys, json; print(json.load(sys.stdin).get('error', {}).get('message', 'Unknown error'))" 2>/dev/null || echo "API error")
2770
+ err "API error: $error_msg"
2771
+ return 1
2772
+ fi
2773
+
2774
+ new_name=$(echo "$response" | python3 -c "import sys, json; data = json.load(sys.stdin); print(data.get('content', [{}])[0].get('text', ''))" 2>/dev/null) || {
2775
+ new_name=$(echo "$response" | grep -o '"text":"[^"]*"' | head -1 | sed 's/"text":"\([^"]*\)"/\1/')
2776
+ }
2777
+ fi
2778
+
2779
+ # Clean up temp files
2780
+ rm -f "$temp_file"
2781
+ if [[ -n "$history_temp" ]]; then
2782
+ rm -f "$history_temp"
2783
+ fi
2784
+
2785
+ # Clean up the AI description (remove any spaces, special chars except underscore and dash)
2786
+ new_name=$(echo "$new_name" | tr ' ' '_' | sed 's/[^a-zA-Z0-9_-]//g' | cut -c1-100)
2787
+ if [[ -z "$new_name" ]]; then
2788
+ err "Failed to generate name from AI response"
2789
+ return 1
2790
+ fi
2791
+
2792
+ new_name="${new_name:0:100}"
2793
+
2794
+ echo "$new_name"
2795
+ }
2796
+
2797
+ # Display tmux message
2798
+ # Usage: tmux_display_message <message> [duration_ms]
2799
+ tmux_display_message() {
2800
+ local message="$1"
2801
+ local duration="${2:-3000}"
2802
+
2803
+ if tmux_available && tmux_inside_session; then
2804
+ tmux display-message -d "$duration" "$message"
2805
+ fi
2806
+ }