Repository navigation
Expand file tree
/
Copy pathresume
More file actions
executable file
·132 lines (117 loc) · 4.42 KB
/
Copy pathresume
File metadata and controls
executable file
·132 lines (117 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env bash
# session.txt — resume helper
# Reads ./session.txt in the current folder and relaunches a saved session.
# Lines are runnable commands with an optional "# title" comment:
# claude --resume <id> # title
# codex resume <id> # title
# Tool-agnostic: it just runs the chosen line (minus the comment).
#
# resume start the newest session (last line)
# resume -l numbered list with titles (1 = oldest, at top)
# resume N start entry N (e.g. resume 1 = oldest)
# resume N show print the full command for entry N (don't run it)
# resume show print the full command for the newest entry
# resume -h help
#
# On every run it self-cleans: entries whose session no longer exists on disk
# (no Claude transcript / no Codex rollout) are dropped from session.txt.
set -uo pipefail
file="${PWD}/session.txt"
usage() { sed -n '2,17p' "$0" | sed 's/^# \{0,1\}//'; }
# --- parse args: a number (index), a list flag, a show flag, in any order ---
LIST=0; SHOW=0; NUM=""
for a in "$@"; do
case "$a" in
-h|--help) usage; exit 0 ;;
-l|--list|list) LIST=1 ;;
-s|--show|-p|--print|show|print|cmd) SHOW=1 ;;
*[0-9]) [[ "$a" =~ ^[0-9]+$ ]] && NUM="$a" ;;
esac
done
[ -f "$file" ] || { echo "session.txt: no file in $PWD" >&2; exit 1; }
# --- helpers ----------------------------------------------------------------
# tool of a line = first token (claude, codex, opencode, pi, cursor-agent, agent, gemini)
line_tool() { awk '{print $1}' <<<"$1"; }
# id of a line. Handles both the spaced form (claude --resume <id>, codex resume
# <id>, ...) and the fused form (agent --resume=<id>, cursor-agent --resume=<id>).
line_id() {
local l="$1"
case "$l" in
*--resume=*) l="${l##*--resume=}"; printf '%s\n' "${l%% *}" ;;
*--session=*) l="${l##*--session=}"; printf '%s\n' "${l%% *}" ;;
*) awk '{print $3}' <<<"$l" ;;
esac
}
# title of a line (after "# "), empty if none
line_title() { case "$1" in *"#"*) echo "${1#*# }" ;; *) echo "" ;; esac; }
# command without the trailing "# title" comment
line_cmd() { local c="${1%%#*}"; echo "${c%"${c##*[![:space:]]}"}"; }
# does the session still exist on disk? (checked against the current folder)
is_live() {
local tool="$1" id="$2"
[ -n "$id" ] || return 1
case "$tool" in
claude)
find "$HOME/.claude/projects" -name "${id}.jsonl" -print -quit 2>/dev/null | grep -q . ;;
codex)
find "$HOME/.codex/sessions" -name "rollout-*-${id}.jsonl" -print -quit 2>/dev/null | grep -q . ;;
opencode)
local db="$HOME/.local/share/opencode/opencode.db" q
command -v sqlite3 >/dev/null 2>&1 && [ -f "$db" ] || return 1
q="${id//\'/\'\'}"
[ -n "$(sqlite3 "$db" "SELECT 1 FROM session WHERE id='$q' LIMIT 1;" 2>/dev/null)" ] ;;
pi)
find "$HOME/.pi/agent/sessions" -name "*${id}*.jsonl" -print -quit 2>/dev/null | grep -q . ;;
cursor-agent|agent)
[ -n "$(find "$HOME/.cursor/chats" -maxdepth 2 -type d -name "$id" -print -quit 2>/dev/null)" ] ;;
*)
return 1 ;;
esac
}
# --- load + prune dead entries ----------------------------------------------
live=()
while IFS= read -r l; do
[ -n "$l" ] || continue
if is_live "$(line_tool "$l")" "$(line_id "$l")"; then
live+=("$l")
fi
done < <(grep -vE '^[[:space:]]*$' "$file")
# rewrite session.txt with only the live entries
if [ "${#live[@]}" -eq 0 ]; then
: > "$file"
echo "session.txt: no resumable sessions left in $PWD" >&2
exit 1
fi
printf '%s\n' "${live[@]}" > "$file"
n=${#live[@]}
# --- list -------------------------------------------------------------------
if [ "$LIST" -eq 1 ]; then
i=1
for l in "${live[@]}"; do
id="$(line_id "$l")"; short="${id:0:8}"
tool="$(line_tool "$l")"; title="$(line_title "$l")"
printf '%2d %-12s %-8s %s\n' "$i" "$tool" "$short" "$title"
i=$((i + 1))
done
exit 0
fi
# --- pick entry -------------------------------------------------------------
if [ -n "$NUM" ]; then
idx=$(( NUM - 1 ))
if [ "$idx" -lt 0 ] || [ "$idx" -ge "$n" ]; then
echo "resume: no entry $NUM (1..$n)" >&2; exit 1
fi
sel="${live[$idx]}"
else
sel="${live[$(( n - 1 ))]}"
fi
cmd="$(line_cmd "$sel")"
title="$(line_title "$sel")"
# --- show: print the full command instead of running it ---------------------
if [ "$SHOW" -eq 1 ]; then
printf '%s\n' "$cmd"
exit 0
fi
# --- run --------------------------------------------------------------------
echo "» ${title:-$cmd}" >&2
exec ${cmd}