Skip to content

Commit cff98c8

Browse files
NagyViktNagyViktclaude
authored
refactor(codex-fleet): extract shared bash helpers into lib/ (#157)
Move duplicated helpers out of fleet-tick.sh, cap-swap-daemon.sh, and cap-probe.sh into two sourced libraries: - scripts/codex-fleet/lib/agents.sh: derive_aid (and email_to_id alias), the canonical email -> agent-id derivation. Replaces the inline bash copy in fleet-tick.sh and the two python3 copies in cap-swap-daemon.sh and cap-probe.sh. Functionally identical (verified against alice@gmail.com, bob@magnoliavilag.hu, carol@gitguardex.com, dave@pipacsclub.org, eve+x@example.co.uk). Documents the cross-language sync requirement with rust/fleet-data/src/fleet.rs::derive_agent_id. - scripts/codex-fleet/lib/ui-helpers.sh: strip_ansi, ios_visible_len, pct_color, ios_status_chip_label. Pulled out of fleet-tick.sh. Both libs use a __CODEX_FLEET_LIB_*_SH guard so double-sourcing is a no-op and are set -u safe. Functions only, no top-level side effects. Scripts source them via "$(dirname "${BASH_SOURCE[0]}")/lib/..." so invocation from any cwd resolves correctly. Co-authored-by: NagyVikt <nagy.viktordp@gmail.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b559229 commit cff98c8

5 files changed

Lines changed: 77 additions & 70 deletions

File tree

scripts/codex-fleet/cap-probe.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,7 @@ mkdir -p "$(dirname "$LOG")"
3737
ts() { date +%H:%M:%S; }
3838
log() { echo "[$(ts)] $*" >> "$LOG"; }
3939

40-
email_to_id() {
41-
python3 -c "
42-
import sys
43-
e=sys.argv[1]; part,dom=e.split('@',1); dom=dom.split('.',1)[0]
44-
m={'magnoliavilag':'magnolia','gitguardex':'gg','pipacsclub':'pipacs'}
45-
print(f'{part}-{m.get(dom,dom)}')
46-
" "$1"
47-
}
40+
source "$(dirname "${BASH_SOURCE[0]}")/lib/agents.sh"
4841

4942
# Returns one of: healthy | capped | stale (re-probe).
5043
# Sets globals VERDICT, UNTIL_TEXT, UNTIL_EPOCH for the caller.

scripts/codex-fleet/cap-swap-daemon.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,7 @@ mkdir -p "$(dirname "$LOG")" "$(dirname "$STATUS_FILE")" "$STATE"
111111
ts() { date +%H:%M:%S; }
112112
log() { echo "[$(ts)] $*" | tee -a "$LOG"; }
113113

114-
email_to_id() {
115-
python3 -c "
116-
import sys
117-
e=sys.argv[1]; part,dom=e.split('@',1); dom=dom.split('.',1)[0]
118-
m={'magnoliavilag':'magnolia','gitguardex':'gg','pipacsclub':'pipacs'}
119-
print(f'{part}-{m.get(dom,dom)}')
120-
" "$1"
121-
}
114+
source "$(dirname "${BASH_SOURCE[0]}")/lib/agents.sh"
122115

123116
# Emails currently assigned to fleet panes (from /proc/<pid>/environ of each pane's codex)
124117
current_emails() {

scripts/codex-fleet/fleet-tick.sh

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,7 @@ declare -a FLEET_EMAILS=()
3636
declare -A IS_CURRENT
3737
CURRENT_EMAIL=""
3838

39-
# Canonical email→id derivation. Mirrors:
40-
# scripts/codex-fleet/cap-swap-daemon.sh::email_to_id
41-
# scripts/codex-fleet/full-bringup.sh (Python id map)
42-
derive_aid() {
43-
local email="$1" part dom
44-
part="${email%%@*}"
45-
dom="${email#*@}"
46-
dom="${dom%%.*}"
47-
case "$dom" in
48-
magnoliavilag) dom=magnolia ;;
49-
gitguardex) dom=gg ;;
50-
pipacsclub) dom=pipacs ;;
51-
esac
52-
printf '%s-%s' "$part" "$dom"
53-
}
39+
source "$(dirname "${BASH_SOURCE[0]}")/lib/agents.sh"
5440

5541
# Sub-task evidence files scored by subtask_progress_pct for fractional progress.
5642
SUB_EVIDENCE=(
@@ -112,15 +98,7 @@ IOS_CHIP_LEFT="◖"
11298
IOS_CHIP_RIGHT=""
11399
IOS_STATUS_CHIP_WIDTH=9
114100

115-
strip_ansi() {
116-
sed -E $'s/\x1B\\[[0-9;]*m//g' <<<"${1:-}"
117-
}
118-
119-
ios_visible_len() {
120-
local clean
121-
clean=$(strip_ansi "${1:-}")
122-
printf '%d' "${#clean}"
123-
}
101+
source "$(dirname "${BASH_SOURCE[0]}")/lib/ui-helpers.sh"
124102

125103
ios_card_top() {
126104
local title="${1:-}"
@@ -158,20 +136,6 @@ ios_card_blank() {
158136
ios_card_row "" "${1:-$IOS_CARD_WIDTH}"
159137
}
160138

161-
# Pick gradient color by percentage int
162-
pct_color() {
163-
local n="$1"
164-
[[ "$n" =~ ^[0-9]+$ ]] || { printf '%s' "$DIM"; return; }
165-
if (( n >= 90 )); then printf '%s' "$GRAD6"
166-
elif (( n >= 75 )); then printf '%s' "$GRAD5"
167-
elif (( n >= 60 )); then printf '%s' "$GRAD4"
168-
elif (( n >= 45 )); then printf '%s' "$GRAD3"
169-
elif (( n >= 30 )); then printf '%s' "$GRAD2"
170-
elif (( n >= 15 )); then printf '%s' "$GRAD1"
171-
else printf '%s' "$GRAD0"
172-
fi
173-
}
174-
175139
# Tiny block-spark for a percentage (▁..█). Kept for older tests; new UI uses
176140
# ios_progress_rail below.
177141
pct_spark() {
@@ -249,22 +213,6 @@ ios_progress_rail() {
249213
printf '%b▕%b%s%b%s%b▏%b' "$IOS_GRAY2" "$color" "$fill" "$IOS_GRAY6" "$empty" "$IOS_GRAY2" "$R"
250214
}
251215

252-
ios_status_chip_label() {
253-
local kind="${1:-idle}"
254-
local raw pad_len pad
255-
case "$kind" in
256-
run|running) raw="● running" ;;
257-
work|working|busy) raw="● working" ;;
258-
exhaust|exhausted|capped) raw="⚠ exhaust" ;;
259-
limit|limited|rate_limited|rate-limited) raw="◍ limited" ;;
260-
idle|*) raw="◌ idle" ;;
261-
esac
262-
pad_len=$(( IOS_STATUS_CHIP_WIDTH - ${#raw} ))
263-
(( pad_len < 0 )) && pad_len=0
264-
printf -v pad '%*s' "$pad_len" ""
265-
printf '%s%s' "$raw" "$pad"
266-
}
267-
268216
ios_status_chip_bg() {
269217
local kind="${1:-idle}"
270218
case "$kind" in

scripts/codex-fleet/lib/agents.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# shellcheck shell=bash
2+
# MUST stay in sync with rust/fleet-data/src/fleet.rs::derive_agent_id
3+
# (around lines 105-118). Any change to the domain-stem alias map or the
4+
# email parsing semantics here must be mirrored there.
5+
6+
if [[ -n "${__CODEX_FLEET_LIB_AGENTS_SH:-}" ]]; then
7+
return 0 2>/dev/null || exit 0
8+
fi
9+
__CODEX_FLEET_LIB_AGENTS_SH=1
10+
11+
derive_aid() {
12+
local email="${1:-}" part dom
13+
part="${email%%@*}"
14+
dom="${email#*@}"
15+
dom="${dom%%.*}"
16+
case "$dom" in
17+
magnoliavilag) dom=magnolia ;;
18+
gitguardex) dom=gg ;;
19+
pipacsclub) dom=pipacs ;;
20+
esac
21+
printf '%s-%s' "$part" "$dom"
22+
}
23+
24+
email_to_id() {
25+
derive_aid "$@"
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# shellcheck shell=bash
2+
3+
if [[ -n "${__CODEX_FLEET_LIB_UI_HELPERS_SH:-}" ]]; then
4+
return 0 2>/dev/null || exit 0
5+
fi
6+
__CODEX_FLEET_LIB_UI_HELPERS_SH=1
7+
8+
: "${IOS_STATUS_CHIP_WIDTH:=9}"
9+
10+
strip_ansi() {
11+
sed -E $'s/\x1B\\[[0-9;]*m//g' <<<"${1:-}"
12+
}
13+
14+
ios_visible_len() {
15+
local clean
16+
clean=$(strip_ansi "${1:-}")
17+
printf '%d' "${#clean}"
18+
}
19+
20+
pct_color() {
21+
local n="${1:-}"
22+
[[ "$n" =~ ^[0-9]+$ ]] || { printf '%s' "${DIM:-}"; return; }
23+
if (( n >= 90 )); then printf '%s' "${GRAD6:-}"
24+
elif (( n >= 75 )); then printf '%s' "${GRAD5:-}"
25+
elif (( n >= 60 )); then printf '%s' "${GRAD4:-}"
26+
elif (( n >= 45 )); then printf '%s' "${GRAD3:-}"
27+
elif (( n >= 30 )); then printf '%s' "${GRAD2:-}"
28+
elif (( n >= 15 )); then printf '%s' "${GRAD1:-}"
29+
else printf '%s' "${GRAD0:-}"
30+
fi
31+
}
32+
33+
ios_status_chip_label() {
34+
local kind="${1:-idle}"
35+
local raw pad_len pad
36+
case "$kind" in
37+
run|running) raw="● running" ;;
38+
work|working|busy) raw="● working" ;;
39+
exhaust|exhausted|capped) raw="⚠ exhaust" ;;
40+
limit|limited|rate_limited|rate-limited) raw="◍ limited" ;;
41+
idle|*) raw="◌ idle" ;;
42+
esac
43+
pad_len=$(( IOS_STATUS_CHIP_WIDTH - ${#raw} ))
44+
(( pad_len < 0 )) && pad_len=0
45+
printf -v pad '%*s' "$pad_len" ""
46+
printf '%s%s' "$raw" "$pad"
47+
}

0 commit comments

Comments
 (0)