From 98f2475f1c39f7c8bc67eb8daac8dd59cd40b3e1 Mon Sep 17 00:00:00 2001 From: Natalie Date: Sun, 17 May 2026 22:17:58 -0700 Subject: [PATCH] =?UTF-8?q?feat(@scripts/session-tools):=20=E2=9C=A8=20imp?= =?UTF-8?q?rove=20triage=20row=20sorting=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Lilith Autocommit --- bin/rclaude | 62 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/bin/rclaude b/bin/rclaude index ab7fc73..1fa5351 100755 --- a/bin/rclaude +++ b/bin/rclaude @@ -685,20 +685,66 @@ cmd_resume() { # picker even when triage produces > (35 - tmux_count) rows on a single # host that would otherwise crowd them out. _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 - # pre-sorted by priority desc, but the per-host blocks are concatenated - # so a P5 on apricot would sit after all local rows and get truncated. - # Re-sort globally by (priority desc, mtime desc) so the top of the - # picker is the actual highest priority across the fleet. - _triage=$(scan_hosts | while IFS= read -r h; do list_triage_on "$h"; done \ - | sort -t"$(printf '\t')" -k4,4n -k9,9nr) + # Triage rows: col 1 = host, col 3 = uuid, col 4 = priority, col 9 = mtime. + # Re-sort globally by (priority asc, mtime desc) for the headline view, + # but also enforce a per-host floor below — otherwise a host with many + # fresh P0/P1 sessions (e.g. local with 2k+ jsonls) takes every seat and + # remote hosts disappear from the picker entirely. + _triage_raw=$(scan_hosts | while IFS= read -r h; do list_triage_on "$h"; done) + _TAB=$(printf '\t') + _triage=$(printf '%s\n' "$_triage_raw" | grep -v '^$' \ + | sort -t"$_TAB" -k4,4n -k9,9nr) _t_count=0 [ -n "$_tmux" ] && _t_count=$(printf '%s\n' "$_tmux" | wc -l | tr -d ' ') [ -n "$_triage" ] && _d_total=$(printf '%s\n' "$_triage" | wc -l | tr -d ' ') _d_room=$((35 - _t_count)) [ "$_d_room" -lt 0 ] && _d_room=0 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 _triage_slice="" fi