feat(@scripts/session-tools): improve triage row sorting logic

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
Natalie 2026-05-17 22:17:58 -07:00
parent 7138338d31
commit 98f2475f1c

View file

@ -685,20 +685,66 @@ cmd_resume() {
# picker even when triage produces > (35 - tmux_count) rows on a single # picker even when triage produces > (35 - tmux_count) rows on a single
# host that would otherwise crowd them out. # host that would otherwise crowd them out.
_tmux=$(scan_hosts | while IFS= read -r h; do list_tmux_on "$h"; done) _tmux=$(scan_hosts | while IFS= read -r h; do list_tmux_on "$h"; done)
# Triage rows: col 4 = priority, col 9 = mtime. Each host's rows come # Triage rows: col 1 = host, col 3 = uuid, col 4 = priority, col 9 = mtime.
# pre-sorted by priority desc, but the per-host blocks are concatenated # Re-sort globally by (priority asc, mtime desc) for the headline view,
# so a P5 on apricot would sit after all local rows and get truncated. # but also enforce a per-host floor below — otherwise a host with many
# Re-sort globally by (priority desc, mtime desc) so the top of the # fresh P0/P1 sessions (e.g. local with 2k+ jsonls) takes every seat and
# picker is the actual highest priority across the fleet. # remote hosts disappear from the picker entirely.
_triage=$(scan_hosts | while IFS= read -r h; do list_triage_on "$h"; done \ _triage_raw=$(scan_hosts | while IFS= read -r h; do list_triage_on "$h"; done)
| sort -t"$(printf '\t')" -k4,4n -k9,9nr) _TAB=$(printf '\t')
_triage=$(printf '%s\n' "$_triage_raw" | grep -v '^$' \
| sort -t"$_TAB" -k4,4n -k9,9nr)
_t_count=0 _t_count=0
[ -n "$_tmux" ] && _t_count=$(printf '%s\n' "$_tmux" | wc -l | tr -d ' ') [ -n "$_tmux" ] && _t_count=$(printf '%s\n' "$_tmux" | wc -l | tr -d ' ')
[ -n "$_triage" ] && _d_total=$(printf '%s\n' "$_triage" | wc -l | tr -d ' ') [ -n "$_triage" ] && _d_total=$(printf '%s\n' "$_triage" | wc -l | tr -d ' ')
_d_room=$((35 - _t_count)) _d_room=$((35 - _t_count))
[ "$_d_room" -lt 0 ] && _d_room=0 [ "$_d_room" -lt 0 ] && _d_room=0
if [ "$_d_room" -gt 0 ] && [ -n "$_triage" ]; then if [ "$_d_room" -gt 0 ] && [ -n "$_triage" ]; then
_triage_slice=$(printf '%s\n' "$_triage" | head -n "$_d_room") # Per-host floor: every host that produced rows gets at least
# _floor seats, so high-priority remote sessions can't be crowded
# out by a flood of recent local rows. Floor = max(5, room/hosts).
_hosts_with_rows=$(printf '%s\n' "$_triage" | awk -F'\t' '{print $1}' | sort -u)
_h_count=$(printf '%s\n' "$_hosts_with_rows" | grep -c . 2>/dev/null || printf 1)
[ "$_h_count" -lt 1 ] && _h_count=1
_floor=$((_d_room / _h_count))
[ "$_floor" -lt 5 ] && _floor=5
_reserved=""
for _h in $_hosts_with_rows; do
_slice=$(printf '%s\n' "$_triage" | awk -F'\t' -v h="$_h" '$1==h' \
| head -n "$_floor")
[ -z "$_slice" ] && continue
if [ -z "$_reserved" ]; then
_reserved=$_slice
else
_reserved=$(printf '%s\n%s' "$_reserved" "$_slice")
fi
done
_r_count=0
[ -n "$_reserved" ] && _r_count=$(printf '%s\n' "$_reserved" | wc -l | tr -d ' ')
if [ "$_r_count" -ge "$_d_room" ]; then
_triage_slice=$(printf '%s\n' "$_reserved" \
| sort -t"$_TAB" -k4,4n -k9,9nr \
| head -n "$_d_room")
else
_remaining=$((_d_room - _r_count))
_fill=$(printf '%s\n' "$_triage" | awk -F'\t' -v rsv="$_reserved" '
BEGIN {
n = split(rsv, lines, "\n")
for (i=1; i<=n; i++) {
split(lines[i], f, "\t")
if (f[3] != "") seen[f[3]] = 1
}
}
!($3 in seen)
' | head -n "$_remaining")
if [ -n "$_fill" ]; then
_triage_slice=$(printf '%s\n%s' "$_reserved" "$_fill" \
| sort -t"$_TAB" -k4,4n -k9,9nr)
else
_triage_slice=$(printf '%s\n' "$_reserved" \
| sort -t"$_TAB" -k4,4n -k9,9nr)
fi
fi
else else
_triage_slice="" _triage_slice=""
fi fi