Repository navigation
Expand file tree
/
Copy pathcloud.bash
More file actions
executable file
·427 lines (395 loc) · 18.5 KB
/
Copy pathcloud.bash
File metadata and controls
executable file
·427 lines (395 loc) · 18.5 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/bin/bash
#
# Sets ShellTime up in a cloud agent environment so the sessions started in it
# are tracked like the ones on your own machines. Supported environments:
#
# - Claude Code cloud (claude.ai/code, CLAUDE_CODE_REMOTE=true)
# - Google Antigravity cloud (OS user `antigravity`, or SHELLTIME_CLOUD=true)
# - Cursor Cloud Agents (CURSOR_CLOUD=true / CURSOR_BACKGROUND_AGENT=true,
# or SHELLTIME_CLOUD=true)
#
# Add this line to the environment's setup/install script:
#
# curl -fsSL https://raw.githubusercontent.com/shelltime/installation/main/cloud.bash | bash
#
# Claude Code cloud:
# Add your ShellTime API token to the environment as a network secret for
# api.shelltime.xyz, sent in the Authorization header with the prefix CLI.
# Anthropic's agent proxy adds it to the daemon's requests after they leave
# the session's VM, so the token is never in the VM, and the secret's host
# needs no network access setting. An environment without network secrets can
# hold the token in SHELLTIME_TOKEN among its environment variables instead,
# with api.shelltime.xyz allowed by its network access. Setup scripts get
# neither, so a SessionStart hook signs in and starts the daemon in every
# session.
#
# Antigravity cloud:
# Pass your ShellTime API token in SHELLTIME_TOKEN (exported in the setup
# script or set in the project's environment variables). Setup installs the
# CLI and daemon and saves SHELLTIME_TOKEN if present.
# A remote agent (antigravity.google.com/remote) runs outside its VM: it sends
# each command in as `bash -l -c <command>` with JULES_SESSION_ID set and runs
# no hook there. So setup adds a line to the login profile bash reads
# (~/.bash_profile, ~/.bash_login or ~/.profile) that sources
# ~/.shelltime/bin/agy-cloud-profile.sh, which reports each command when its
# shell exits (`shelltime hook-ingest --event ShellCommand`) and starts the
# daemon. Prompts, replies and tokens never reach the VM.
# Where Antigravity does run hooks in the VM, commit .agents/hooks.json to
# your repository (`shelltime agy install`); setup also adds a SessionStart
# hook in ~/.gemini/config/hooks.json (group "shelltime-cloud") that signs in
# (when SHELLTIME_TOKEN is in the session env) and starts the daemon.
#
# Cursor Cloud Agents:
# Add SHELLTIME_TOKEN and SHELLTIME_CLOUD=true to your environment secrets
# and commit .cursor/hooks.json to your repository (`shelltime cursor install`).
# Run cloud.bash in the environment's install step and
# `~/.shelltime/bin/cloud-start.sh` in the start step (or run cloud.bash in
# the start step directly: when SHELLTIME_TOKEN is present it signs in and
# starts the daemon immediately).
#
# Outside a cloud environment (no CLAUDE_CODE_REMOTE=true, no `antigravity`
# user, no CURSOR_CLOUD/CURSOR_BACKGROUND_AGENT, and no SHELLTIME_CLOUD=true),
# this installs nothing.
#
# Guides:
# https://shelltime.xyz/blogs/track-claude-code-cloud-sessions
# https://shelltime.xyz/blogs/track-cursor-and-antigravity
RELEASE_URL="https://github.com/shelltime/cli/releases/latest/download"
IS_CLAUDE_CLOUD=false
IS_AGY_CLOUD=false
IS_CURSOR_CLOUD=false
TARGET_USER=""
TARGET_HOME=""
ST_DIR=""
CLOUD_START_PATH=""
CLAUDE_HOOK_PATH=""
CLAUDE_SETTINGS_PATH=""
AGY_HOOK_PATH=""
AGY_HOOKS_PATH=""
AGY_PROFILE_PATH=""
LOGIN_PROFILE_PATH=""
# A setup script that exits non-zero keeps the environment's sessions from
# starting, so a failure is reported here and never passed on.
finish() {
local status=$?
set +e
[ -n "${TMP_DIR:-}" ] && rm -rf "$TMP_DIR"
if [ "$status" -ne 0 ]; then
echo "ShellTime setup failed; sessions will not be tracked" >&2
fi
exit 0
}
trap finish EXIT
set -euo pipefail
is_truthy() {
case "${1:-}" in
true | TRUE | True | 1) return 0 ;;
*) return 1 ;;
esac
}
# 0. Decide which cloud environment(s) we are in and which user/home owns the
# agent runtime (Antigravity setup scripts often run as root while the agent
# runs as the OS user `antigravity`). Note: CURSOR_CODE_REMOTE and CURSOR_AGENT
# are also set on local/SSH machines, so they are intentionally not treated as
# cloud signals on their own.
detect_cloud_env() {
TARGET_USER=$(id -un)
TARGET_HOME="$HOME"
if [ "${CLAUDE_CODE_REMOTE:-}" = "true" ]; then
IS_CLAUDE_CLOUD=true
fi
if [ "$TARGET_USER" = "antigravity" ]; then
IS_AGY_CLOUD=true
elif [ "$(id -u)" -eq 0 ] && id -u antigravity >/dev/null 2>&1; then
IS_AGY_CLOUD=true
TARGET_USER="antigravity"
TARGET_HOME=$(getent passwd antigravity 2>/dev/null | cut -d: -f6 || true)
TARGET_HOME="${TARGET_HOME:-/home/antigravity}"
fi
if is_truthy "${CURSOR_CLOUD:-}" || is_truthy "${CURSOR_BACKGROUND_AGENT:-}"; then
IS_CURSOR_CLOUD=true
fi
if [ "$IS_CLAUDE_CLOUD" = false ] && [ "$IS_AGY_CLOUD" = false ] && [ "$IS_CURSOR_CLOUD" = false ]; then
if is_truthy "${SHELLTIME_CLOUD:-}"; then
# Prefer host/agent markers over repo hook files (a repo may commit
# both .cursor/hooks.json and .agents/hooks.json).
if [ -d "$TARGET_HOME/.cursor" ] || command -v cursor-agent >/dev/null 2>&1; then
IS_CURSOR_CLOUD=true
fi
if [ -d "$TARGET_HOME/.gemini" ] || command -v agy >/dev/null 2>&1; then
IS_AGY_CLOUD=true
fi
if [ "$IS_CURSOR_CLOUD" = false ] && [ "$IS_AGY_CLOUD" = false ]; then
if [ -f ".cursor/environment.json" ] || [ -f ".cursor/hooks.json" ]; then
IS_CURSOR_CLOUD=true
fi
if [ -f ".agents/hooks.json" ]; then
IS_AGY_CLOUD=true
fi
fi
# Fresh VM before checkout/agent init: prepare both hook-based cloud agents
if [ "$IS_CURSOR_CLOUD" = false ] && [ "$IS_AGY_CLOUD" = false ]; then
IS_CURSOR_CLOUD=true
IS_AGY_CLOUD=true
fi
else
echo "cloud.bash only runs in cloud agent environments (CLAUDE_CODE_REMOTE=true, Antigravity cloud, CURSOR_CLOUD=true, or SHELLTIME_CLOUD=true); on your own machine run: curl -sSL https://shelltime.xyz/i | bash" >&2
return 1
fi
fi
ST_DIR="$TARGET_HOME/.shelltime"
CLOUD_START_PATH="$ST_DIR/bin/cloud-start.sh"
CLAUDE_HOOK_PATH="$ST_DIR/bin/cloud-session-start.sh"
CLAUDE_SETTINGS_PATH="$TARGET_HOME/.claude/settings.json"
AGY_HOOK_PATH="$ST_DIR/bin/agy-cloud-session-start.sh"
AGY_HOOKS_PATH="$TARGET_HOME/.gemini/config/hooks.json"
AGY_PROFILE_PATH="$ST_DIR/bin/agy-cloud-profile.sh"
}
# 1. CLI and daemon, where install.bash puts them (the ShellTime mod and
# repository hook commands look there and in /usr/local/bin)
install_cli() {
local arch
case "$(uname -m)" in
x86_64) arch=x86_64 ;;
aarch64 | arm64) arch=arm64 ;;
*)
echo "Unsupported architecture: $(uname -m)" >&2
return 1
;;
esac
mkdir -p "$ST_DIR/bin" "$ST_DIR/logs" "$TARGET_HOME/.local/bin"
curl -fsSL --retry 3 -o "$TMP_DIR/cli.tar.gz" "$RELEASE_URL/cli_Linux_${arch}.tar.gz"
tar -xzf "$TMP_DIR/cli.tar.gz" -C "$TMP_DIR" shelltime shelltime-daemon
install -m 0755 "$TMP_DIR/shelltime" "$TMP_DIR/shelltime-daemon" "$ST_DIR/bin/"
ln -sf "$ST_DIR/bin/shelltime" "$TARGET_HOME/.local/bin/shelltime"
if [ -w /usr/local/bin ]; then
ln -sf "$ST_DIR/bin/shelltime" /usr/local/bin/shelltime
ln -sf "$ST_DIR/bin/shelltime-daemon" /usr/local/bin/shelltime-daemon
fi
}
# Shared start helper for hook-based cloud agents (Cursor Cloud Agents `start`
# step and Antigravity's SessionStart hook): signs in if SHELLTIME_TOKEN is in
# the environment, clears any stale socket, and starts shelltime-daemon once.
install_cloud_start_script() {
cat >"$CLOUD_START_PATH" <<'EOF'
#!/bin/bash
# Starts ShellTime in a cloud agent VM (Antigravity SessionStart hook, Cursor
# Cloud Agents start step, or any VM with SHELLTIME_CLOUD=true).
export SHELLTIME_CLOUD=true
st="$HOME/.shelltime"
mkdir -p "$st/logs"
# One at a time: an Antigravity remote agent's commands each start this, often
# together, and two could both find no daemon and remove each other's socket.
# The daemon doesn't keep the lock (9>&- below).
if command -v flock >/dev/null 2>&1 && exec 9>"$st/cloud-start.lock"; then
flock -w 10 9 || exit 0
fi
if [ -n "${SHELLTIME_TOKEN:-}" ] && ! grep -qF -- "$SHELLTIME_TOKEN" "$st/config.yaml" 2>/dev/null; then
"$st/bin/shelltime" auth --token "$SHELLTIME_TOKEN" >/dev/null 2>&1 || true
fi
if [ -f "$st/config.yaml" ] && ! pgrep -fx "$st/bin/shelltime-daemon" >/dev/null 2>&1; then
rm -f /tmp/shelltime.sock
setsid nohup "$st/bin/shelltime-daemon" >>"$st/logs/shelltime-daemon.log" 2>&1 </dev/null 9>&- &
for _ in 1 2 3 4 5 6 7 8 9 10; do
[ -S /tmp/shelltime.sock ] && break
sleep 0.1 2>/dev/null || sleep 1
done
fi
exit 0
EOF
chmod 0755 "$CLOUD_START_PATH"
}
# 2a. Claude Code's OTEL exporter pointed at the daemon
connect_claude_code() {
HOME="$TARGET_HOME" "$ST_DIR/bin/shelltime" cc install
}
# 2b. Claude Code SessionStart hook: the snapshot keeps files but no processes,
# there is no systemd, and only sessions see SHELLTIME_TOKEN
install_claude_session_hook() {
cat >"$CLAUDE_HOOK_PATH" <<'EOF'
#!/bin/bash
# Claude Code SessionStart hook, installed by ShellTime's cloud.bash. Its stdout
# would go into Claude's context, so it prints nothing.
st="$HOME/.shelltime"
# With the token in a network secret, the CLI signs in with the placeholder
# proxy-injected and sends no Authorization header; the agent proxy adds the
# real one. SHELLTIME_TOKEN, for environments without network secrets, wins.
token="${SHELLTIME_TOKEN:-proxy-injected}"
# The first session, or the token changed in the environment settings
if ! grep -qF -- "$token" "$st/config.yaml" 2>/dev/null; then
"$st/bin/shelltime" auth --token "$token" >/dev/null 2>&1 || exit 0
fi
if ! pgrep -fx "$st/bin/shelltime-daemon" >/dev/null; then
setsid nohup "$st/bin/shelltime-daemon" >>"$st/logs/shelltime-daemon.log" 2>&1 </dev/null &
fi
exit 0
EOF
chmod 0755 "$CLAUDE_HOOK_PATH"
# Replaces the hook an earlier run added instead of adding a second one
jq --arg cmd "$CLAUDE_HOOK_PATH" '
.hooks.SessionStart = [(.hooks.SessionStart // [])[] | select(all(.hooks[]?; .command != $cmd))]
+ [{hooks: [{type: "command", command: $cmd}]}]
' "$CLAUDE_SETTINGS_PATH" >"$CLAUDE_SETTINGS_PATH.tmp"
mv "$CLAUDE_SETTINGS_PATH.tmp" "$CLAUDE_SETTINGS_PATH"
}
# 3a. When SHELLTIME_TOKEN is exported in the setup/install script, bake it
# into config.yaml so it survives into live sessions.
auth_if_token_present() {
if [ -n "${SHELLTIME_TOKEN:-}" ]; then
HOME="$TARGET_HOME" "$ST_DIR/bin/shelltime" auth --token "$SHELLTIME_TOKEN" >/dev/null
fi
}
# 3b. Antigravity SessionStart hook: invokes cloud-start.sh and prints {} under
# a separate hook group ("shelltime-cloud") so .agents/hooks.json never yields
# to it.
install_agy_session_hook() {
cat >"$AGY_HOOK_PATH" <<'EOF'
#!/bin/bash
# Antigravity SessionStart hook, installed by ShellTime's cloud.bash.
# Antigravity requires a JSON object on stdout.
"$HOME/.shelltime/bin/cloud-start.sh" >/dev/null 2>&1 || true
echo '{}'
exit 0
EOF
chmod 0755 "$AGY_HOOK_PATH"
mkdir -p "$(dirname "$AGY_HOOKS_PATH")"
if [ ! -s "$AGY_HOOKS_PATH" ]; then
echo '{}' >"$AGY_HOOKS_PATH"
fi
if command -v jq >/dev/null 2>&1; then
jq --arg cmd "$AGY_HOOK_PATH" \
'.["shelltime-cloud"] = {"SessionStart": [{"type": "command", "command": $cmd, "timeout": 15}]}' \
"$AGY_HOOKS_PATH" >"$AGY_HOOKS_PATH.tmp"
mv "$AGY_HOOKS_PATH.tmp" "$AGY_HOOKS_PATH"
elif command -v python3 >/dev/null 2>&1; then
python3 -c '
import json, sys
path, cmd = sys.argv[1], sys.argv[2]
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
data["shelltime-cloud"] = {"SessionStart": [{"type": "command", "command": cmd, "timeout": 15}]}
with open(path + ".tmp", "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
f.write("\n")
' "$AGY_HOOKS_PATH" "$AGY_HOOK_PATH"
mv "$AGY_HOOKS_PATH.tmp" "$AGY_HOOKS_PATH"
else
echo "Neither jq nor python3 is available to update $AGY_HOOKS_PATH" >&2
return 1
fi
}
# 3c. Antigravity remote agents: the login shell reports each command. The
# script is sourced, so it runs in the agent's shell: it must print nothing and
# leave the command's exit status alone. Shells the command starts are part of
# it (SHELLTIME_AGY_COMMAND), and so is a second profile sourcing it. Without
# EPOCHREALTIME (bash < 5) there is no duration. The command is recorded first
# (hook-ingest is bounded: no network, its waits are under a second), so
# without a daemon it goes to the pending file, which a starting daemon sends
# first; cloud-start.sh then runs detached, so the command never waits for a
# sign-in or the daemon's start. A command that execs, sets its own EXIT trap or
# is killed with SIGKILL is not reported.
install_agy_shell_profile() {
cat >"$AGY_PROFILE_PATH" <<'EOF'
# Sourced by bash login shells, installed by ShellTime's cloud.bash. An
# Antigravity remote agent runs outside its VM: it sends each command in as
# `bash -l -c <command>` with JULES_SESSION_ID naming the conversation, and
# runs no hook here. So this shell reports its command to ShellTime when it
# exits. It prints nothing and leaves the command's exit status as it was.
if [ -n "${JULES_SESSION_ID:-}" ] && [ -n "${BASH_EXECUTION_STRING:-}" ] &&
[ -z "${SHELLTIME_AGY_COMMAND:-}" ] && [ -x "$HOME/.shelltime/bin/shelltime" ]; then
export SHELLTIME_AGY_COMMAND=1
__shelltime_agy_start=${EPOCHREALTIME:-}
__shelltime_agy_report() {
local status=$? st="$HOME/.shelltime" ms= IFS=$' \t\n'
set +euf
if [ -n "$__shelltime_agy_start" ] && [ -n "${EPOCHREALTIME:-}" ]; then
ms=$(( (10#${EPOCHREALTIME//[!0-9]/} - 10#${__shelltime_agy_start//[!0-9]/}) / 1000 ))
fi
printf '%s' "$BASH_EXECUTION_STRING" | SHELLTIME_CLOUD=true "$st/bin/shelltime" hook-ingest --agent agy \
--event ShellCommand --session "$JULES_SESSION_ID" --exit-code "$status" ${ms:+--duration-ms "$ms"} >/dev/null 2>&1
(unset SHELLTIME_AGY_COMMAND JULES_SESSION_ID; "$st/bin/cloud-start.sh" >/dev/null 2>&1 </dev/null &)
return "$status"
}
trap __shelltime_agy_report EXIT
fi
EOF
# A bash login shell reads /etc/profile, which sources /etc/profile.d, then
# the first of ~/.bash_profile, ~/.bash_login and ~/.profile that exists. sh
# never reads the script.
local name comment line
comment='# ShellTime: record the commands an Antigravity remote agent runs'
line='if [ -n "$BASH_VERSION" ] && [ -r "$HOME/.shelltime/bin/agy-cloud-profile.sh" ]; then . "$HOME/.shelltime/bin/agy-cloud-profile.sh"; fi'
# Still read when a tool creates a ~/.bash_profile later, which would hide
# the line in ~/.profile
# Best effort: a profile we can't write must not stop the rest of setup
if [ -d /etc/profile.d ] && [ -w /etc/profile.d ]; then
printf '%s\n%s\n' "$comment" "$line" >/etc/profile.d/shelltime-agy-cloud.sh ||
echo "Couldn't write /etc/profile.d/shelltime-agy-cloud.sh" >&2
fi
LOGIN_PROFILE_PATH="$TARGET_HOME/.profile"
for name in .bash_profile .bash_login .profile; do
if [ -f "$TARGET_HOME/$name" ]; then
LOGIN_PROFILE_PATH="$TARGET_HOME/$name"
break
fi
done
# An earlier run's line stays the only one
if ! grep -qF "agy-cloud-profile.sh" "$LOGIN_PROFILE_PATH" 2>/dev/null; then
printf '\n%s\n%s\n' "$comment" "$line" >>"$LOGIN_PROFILE_PATH" ||
echo "Couldn't add ShellTime to $LOGIN_PROFILE_PATH; Antigravity remote agent commands won't be tracked" >&2
fi
}
# 4. When setup ran as root on behalf of `antigravity`, hand ownership of the
# files we created to that user so the runtime hooks and daemon can read config.
fix_ownership() {
if [ "$(id -u)" -ne 0 ] || [ "$TARGET_USER" = "root" ]; then
return 0
fi
local group
group=$(id -gn "$TARGET_USER")
chown -R "$TARGET_USER:$group" "$ST_DIR"
chown "$TARGET_USER:$group" "$TARGET_HOME/.local" "$TARGET_HOME/.local/bin" 2>/dev/null || true
chown -h "$TARGET_USER:$group" "$TARGET_HOME/.local/bin/shelltime" 2>/dev/null || true
if [ "$IS_AGY_CLOUD" = true ]; then
chown "$TARGET_USER:$group" "$TARGET_HOME/.gemini" "$TARGET_HOME/.gemini/config" "$AGY_HOOKS_PATH" 2>/dev/null || true
chown "$TARGET_USER:$group" "$LOGIN_PROFILE_PATH" 2>/dev/null || true
fi
if [ "$IS_CLAUDE_CLOUD" = true ]; then
chown "$TARGET_USER:$group" "$TARGET_HOME/.claude" "$CLAUDE_SETTINGS_PATH" 2>/dev/null || true
fi
}
main() {
detect_cloud_env
TMP_DIR=$(mktemp -d)
install_cli
if [ "$IS_CLAUDE_CLOUD" = true ]; then
connect_claude_code
install_claude_session_hook
fi
if [ "$IS_AGY_CLOUD" = true ] || [ "$IS_CURSOR_CLOUD" = true ]; then
install_cloud_start_script
auth_if_token_present
fi
if [ "$IS_AGY_CLOUD" = true ]; then
install_agy_session_hook
install_agy_shell_profile
fi
fix_ownership
# Cursor Cloud Agents ignore ~/.cursor/hooks.json and rely on the start
# step: when cloud.bash is run with SHELLTIME_TOKEN as the runtime user,
# start the daemon immediately so a single start-step call works too.
if [ "$IS_CURSOR_CLOUD" = true ] && [ -n "${SHELLTIME_TOKEN:-}" ] && [ "$TARGET_USER" = "$(id -un)" ]; then
HOME="$TARGET_HOME" "$CLOUD_START_PATH"
fi
if [ "$IS_CLAUDE_CLOUD" = true ] && [ "$IS_AGY_CLOUD" = false ] && [ "$IS_CURSOR_CLOUD" = false ]; then
echo "ShellTime is installed; Claude Code sessions will be tracked once your ShellTime token is a network secret for api.shelltime.xyz"
elif [ "$IS_AGY_CLOUD" = true ] && [ "$IS_CLAUDE_CLOUD" = false ] && [ "$IS_CURSOR_CLOUD" = false ]; then
echo "ShellTime is installed; Antigravity sessions will be tracked (remote agents' commands through the login shell; elsewhere commit .agents/hooks.json)"
elif [ "$IS_CURSOR_CLOUD" = true ] && [ "$IS_CLAUDE_CLOUD" = false ] && [ "$IS_AGY_CLOUD" = false ]; then
echo "ShellTime is installed; Cursor Cloud Agent sessions will be tracked (run ~/.shelltime/bin/cloud-start.sh in your start step and ensure .cursor/hooks.json is committed in your repo)"
else
echo "ShellTime is installed for cloud agent sessions"
fi
}
# Nothing runs before this line, so a download cut short installs nothing
main "$@"