From 8dd766ba999f3aea16ec49a61ef4cb6dafd24226 Mon Sep 17 00:00:00 2001 From: AnnatarHe Date: Sun, 29 Nov 2026 23:10:39 +0800 Subject: [PATCH 1/3] feat(cloud): record Antigravity remote agent commands from the login shell An Antigravity remote agent runs outside its VM and runs no hook there, so the hooks cloud.bash set up never fired. It sends each command in as `bash -l -c ` with JULES_SESSION_ID set, so cloud.bash now writes ~/.shelltime/bin/agy-cloud-profile.sh and sources it (bash only) from the login profile bash reads. Its EXIT trap pipes the command to `shelltime hook-ingest --agent agy --event ShellCommand` with the session, exit status and duration, then starts the daemon. It prints nothing, keeps the exit status and skips shells the command starts. Needs the CLI's ShellCommand mode (shelltime/cli#366). Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yaml | 24 ++++++++++++ CLAUDE.md | 4 +- cloud.bash | 75 +++++++++++++++++++++++++++++++++---- 3 files changed, 94 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f754e8e..c424e42 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -203,6 +203,30 @@ jobs: grep -qx "token: st_v1_agy_runtime" "$config" || { echo "expected runtime token"; cat "$config"; exit 1; } [ "$(pgrep -fxc "$daemon")" = 1 ] || { echo "expected one daemon"; pgrep -fxa "$daemon"; exit 1; } pkill -fx "$daemon" + + # A remote agent's command: its login shell reports it (to a stub here, so this doesn't + # depend on the released CLI) and leaves its output and exit status alone + [ "$(grep -c agy-cloud-profile.sh "$AGY_HOME/.profile")" = 1 ] || { echo "expected one profile line"; cat "$AGY_HOME/.profile"; exit 1; } + calls="$AGY_HOME/hook-ingest.calls" + cat >"$AGY_HOME/.shelltime/bin/shelltime" <<'STUB' + #!/bin/bash + { echo "args: $*"; echo "stdin: $(cat)"; } >>"$HOME/hook-ingest.calls" + echo '{}' + STUB + rc=0 + out=$(cd /tmp && env -u SHELLTIME_TOKEN HOME="$AGY_HOME" JULES_SESSION_ID=ci-session bash -l -c 'echo "a & b"; bash -l -c "echo nested"; sleep 0.2; exit 7') || rc=$? + [ "$out" = "$(printf 'a & b\nnested')" ] || { echo "unexpected output: $out"; exit 1; } + [ "$rc" = 7 ] || { echo "exit status $rc, expected 7"; exit 1; } + [ "$(grep -c '^args: ' "$calls")" = 1 ] || { echo "expected one report, nested shells included"; cat "$calls"; exit 1; } + grep -Eq '^args: hook-ingest --agent agy --event ShellCommand --session ci-session --exit-code 7 --duration-ms [0-9]+$' "$calls" || { cat "$calls"; exit 1; } + ms=$(sed -n 's/.*--duration-ms \([0-9]*\)$/\1/p' "$calls") + [ "$ms" -ge 200 ] || { echo "duration $ms ms, expected at least 200"; exit 1; } + grep -qxF 'stdin: echo "a & b"; bash -l -c "echo nested"; sleep 0.2; exit 7' "$calls" || { cat "$calls"; exit 1; } + # Outside a remote agent's conversation nothing is reported + rm -f "$calls" + env -u SHELLTIME_TOKEN -u JULES_SESSION_ID HOME="$AGY_HOME" bash -l -c true + [ ! -e "$calls" ] || { echo "reported without JULES_SESSION_ID"; cat "$calls"; exit 1; } + pkill -fx "$daemon" || true rm -rf "$AGY_HOME" - name: Run cloud.bash for Cursor Cloud Agents, twice diff --git a/CLAUDE.md b/CLAUDE.md index 5634b2e..10883b2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -9,7 +9,7 @@ This is the **shelltime installation** repository - a collection of shell script ## Repository Structure - `install.bash` - Main installation script that downloads shelltime CLI binaries and sets up shell hooks -- `cloud.bash` - Setup for cloud agent environments (Claude Code cloud, Google Antigravity cloud, and Cursor Cloud Agents), run from an environment's setup/install script with `curl -fsSL https://raw.githubusercontent.com/shelltime/installation/main/cloud.bash | bash`. Detects Claude Code cloud (`CLAUDE_CODE_REMOTE=true`), Antigravity cloud (OS user `antigravity`, including when setup runs as `root` on behalf of `antigravity`), Cursor Cloud Agents (`CURSOR_CLOUD=true`/`1`, `CURSOR_BACKGROUND_AGENT=true`/`1`), or an explicit `SHELLTIME_CLOUD=true` (matching `~/.cursor`/`.cursor/*`/`cursor-agent` vs `~/.gemini`/`.agents/hooks.json`/`agy`, or both in a fresh VM); outside those it installs nothing (ignoring `CURSOR_CODE_REMOTE` and `CURSOR_AGENT`, which local/SSH machines also set). Installs the Linux CLI and daemon into `$TARGET_HOME/.shelltime/bin` (and symlinks into `/usr/local/bin` when writable). For Claude Code cloud, runs `shelltime cc install` and adds a Claude Code SessionStart hook (`~/.shelltime/bin/cloud-session-start.sh`) that signs in with `proxy-injected` (or `SHELLTIME_TOKEN`) and starts the daemon in each session. For Antigravity and Cursor cloud, installs `~/.shelltime/bin/cloud-start.sh` (signs in when `SHELLTIME_TOKEN` is in the env, clears stale `/tmp/shelltime.sock`, starts `shelltime-daemon` with `SHELLTIME_CLOUD=true`) and saves `SHELLTIME_TOKEN` to `config.yaml` if present during setup. For Antigravity, also adds `~/.shelltime/bin/agy-cloud-session-start.sh` under the separate `"shelltime-cloud"` group in `~/.gemini/config/hooks.json` (so `.agents/hooks.json` never yields to it) and fixes ownership to `antigravity` when setup ran as `root`. For Cursor Cloud Agents (which ignore `~/.cursor/hooks.json`), also runs `cloud-start.sh` immediately when `SHELLTIME_TOKEN` is already set. A setup script that exits non-zero stops sessions from starting, so it always exits 0 (its `EXIT` trap reports a failure instead), and nothing runs before the final `main` call, so a truncated download installs nothing. The guides that document it are `content/blogs/track-claude-code-cloud-sessions/` and `content/blogs/track-cursor-and-antigravity/` in the web repo; keep them in step +- `cloud.bash` - Setup for cloud agent environments (Claude Code cloud, Google Antigravity cloud, and Cursor Cloud Agents), run from an environment's setup/install script with `curl -fsSL https://raw.githubusercontent.com/shelltime/installation/main/cloud.bash | bash`. Detects Claude Code cloud (`CLAUDE_CODE_REMOTE=true`), Antigravity cloud (OS user `antigravity`, including when setup runs as `root` on behalf of `antigravity`), Cursor Cloud Agents (`CURSOR_CLOUD=true`/`1`, `CURSOR_BACKGROUND_AGENT=true`/`1`), or an explicit `SHELLTIME_CLOUD=true` (matching `~/.cursor`/`.cursor/*`/`cursor-agent` vs `~/.gemini`/`.agents/hooks.json`/`agy`, or both in a fresh VM); outside those it installs nothing (ignoring `CURSOR_CODE_REMOTE` and `CURSOR_AGENT`, which local/SSH machines also set). Installs the Linux CLI and daemon into `$TARGET_HOME/.shelltime/bin` (and symlinks into `/usr/local/bin` when writable). For Claude Code cloud, runs `shelltime cc install` and adds a Claude Code SessionStart hook (`~/.shelltime/bin/cloud-session-start.sh`) that signs in with `proxy-injected` (or `SHELLTIME_TOKEN`) and starts the daemon in each session. For Antigravity and Cursor cloud, installs `~/.shelltime/bin/cloud-start.sh` (signs in when `SHELLTIME_TOKEN` is in the env, clears stale `/tmp/shelltime.sock`, starts `shelltime-daemon` with `SHELLTIME_CLOUD=true`) and saves `SHELLTIME_TOKEN` to `config.yaml` if present during setup. For Antigravity, also adds `~/.shelltime/bin/agy-cloud-session-start.sh` under the separate `"shelltime-cloud"` group in `~/.gemini/config/hooks.json` (so `.agents/hooks.json` never yields to it) and fixes ownership to `antigravity` when setup ran as `root`. A remote agent (antigravity.google.com/remote) runs outside its VM and runs no hook there: it sends each command in through `cmdlauncher` as `bash -l -c ` with `JULES_SESSION_ID` set. So setup also writes `~/.shelltime/bin/agy-cloud-profile.sh` and sources it (bash only) from the login profile bash reads (the first of `~/.bash_profile`, `~/.bash_login`, `~/.profile`, else a new `~/.profile`; one line, kept on a rerun). With `JULES_SESSION_ID` and `BASH_EXECUTION_STRING` set it adds an EXIT trap that pipes the command to `shelltime hook-ingest --agent agy --event ShellCommand --session "$JULES_SESSION_ID" --exit-code [--duration-ms ]` (from `EPOCHREALTIME`, bash 5) and then runs `cloud-start.sh`; it prints nothing, keeps the command's exit status, and skips shells the command starts (`SHELLTIME_AGY_COMMAND`). It needs a CLI with the `ShellCommand` mode. For Cursor Cloud Agents (which ignore `~/.cursor/hooks.json`), also runs `cloud-start.sh` immediately when `SHELLTIME_TOKEN` is already set. A setup script that exits non-zero stops sessions from starting, so it always exits 0 (its `EXIT` trap reports a failure instead), and nothing runs before the final `main` call, so a truncated download installs nothing. The guides that document it are `content/blogs/track-claude-code-cloud-sessions/` and `content/blogs/track-cursor-and-antigravity/` in the web repo; keep them in step - `hooks/` - Shell-specific hook scripts that track command execution. They are copies of the CLI's embedded hooks (`model/hooks/` in shelltime/cli, which `shelltime hooks install` and `shelltime init` write over these), so change both together: - `zsh.zsh` - Uses zsh's native `preexec` and `precmd` hooks - `bash.bash` - Uses `bash-preexec` library for preexec/precmd functionality @@ -30,7 +30,7 @@ Each `test-shells` job runs `bash ./install.bash` from its shell (zsh, fish or b 3. Homebrew path: the cask is installed and links `shelltime` and `shelltime-daemon` into brew's bin, with no `~/.shelltime/bin/shelltime` shadowing it. Manual path: both binaries are in `~/.shelltime/bin`, which the config puts on `PATH` 4. A new interactive shell loads the hook's functions and runs that `shelltime` (bash needs `-i`: Ubuntu's default `~/.bashrc` returns early in a non-interactive shell) -The `test-cloud` job checks that `cloud.bash` installs nothing without `CLAUDE_CODE_REMOTE=true`, then runs it twice with it on x86_64 and arm64 Linux and checks the binaries, the OTEL settings, that the SessionStart hook is registered exactly once, and that the hook prints nothing, signs in with `proxy-injected` (or `SHELLTIME_TOKEN` when set) and starts one daemon. +The `test-cloud` job checks that `cloud.bash` installs nothing without `CLAUDE_CODE_REMOTE=true`, then runs it twice with it on x86_64 and arm64 Linux and checks the binaries, the OTEL settings, that the SessionStart hook is registered exactly once, and that the hook prints nothing, signs in with `proxy-injected` (or `SHELLTIME_TOKEN` when set) and starts one daemon. The Antigravity run also checks the login profile line is there once and, with a stub `shelltime`, that a `bash -l -c` command under `JULES_SESSION_ID` keeps its output and exit status and is reported once (nested login shells included) with its duration, and that nothing is reported without `JULES_SESSION_ID`. ## How Shell Hooks Work diff --git a/cloud.bash b/cloud.bash index f218ec2..5b73f80 100755 --- a/cloud.bash +++ b/cloud.bash @@ -25,12 +25,19 @@ # # Antigravity cloud: # Pass your ShellTime API token in SHELLTIME_TOKEN (exported in the setup -# script or set in the project's environment variables) and commit -# .agents/hooks.json to your repository (`shelltime agy install`). Setup -# installs the CLI and daemon, saves SHELLTIME_TOKEN if present, and 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 in every session. +# 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 ` 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 @@ -60,6 +67,8 @@ 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. @@ -144,6 +153,7 @@ detect_cloud_env() { 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 @@ -283,6 +293,55 @@ with open(path + ".tmp", "w", encoding="utf-8") as f: 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). Without EPOCHREALTIME (bash < 5) there is no +# duration. The command is recorded before the daemon is started: without a +# daemon it goes to the pending file, which a starting daemon sends first. +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 ` 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= + set +eu + 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" | "$st/bin/shelltime" hook-ingest --agent agy --event ShellCommand \ + --session "$JULES_SESSION_ID" --exit-code "$status" ${ms:+--duration-ms "$ms"} >/dev/null 2>&1 + "$st/bin/cloud-start.sh" >/dev/null 2>&1 /dev/null; then + printf '\n# ShellTime: record the commands an Antigravity remote agent runs\n%s\n' \ + 'if [ -n "$BASH_VERSION" ] && [ -r "$HOME/.shelltime/bin/agy-cloud-profile.sh" ]; then . "$HOME/.shelltime/bin/agy-cloud-profile.sh"; fi' \ + >>"$LOGIN_PROFILE_PATH" + 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() { @@ -296,6 +355,7 @@ fix_ownership() { 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 @@ -320,6 +380,7 @@ main() { if [ "$IS_AGY_CLOUD" = true ]; then install_agy_session_hook + install_agy_shell_profile fi fix_ownership @@ -334,7 +395,7 @@ main() { 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 (ensure .agents/hooks.json is committed in your repo)" + 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 From 41043054124826811f551a76ebc977eb14a85209 Mon Sep 17 00:00:00 2001 From: AnnatarHe Date: Sun, 29 Nov 2026 23:25:09 +0800 Subject: [PATCH 2/3] fix(cloud): never hold a remote agent's command on the daemon start The EXIT trap ran cloud-start.sh in the foreground, so a command's shell could wait up to a second for the daemon's socket, longer on a sign-in. It now runs detached after the command is recorded (to the pending file when no daemon runs yet, which a starting daemon sends first). Setup also writes /etc/profile.d/shelltime-agy-cloud.sh when it may, so a ~/.bash_profile a tool creates later doesn't hide the line in ~/.profile. CI covers the line going into an existing ~/.bash_login and the command not waiting for cloud-start.sh. CLAUDE.md lists what isn't reported (exec, a command's own EXIT trap, SIGKILL). Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yaml | 24 ++++++++++++++++++++---- CLAUDE.md | 4 ++-- cloud.bash | 29 ++++++++++++++++++++--------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index c424e42..6666831 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -173,6 +173,13 @@ jobs: echo "$out" echo "$out" | grep -q "ShellTime is installed; Antigravity sessions" || { echo "run $run did not finish"; exit 1; } done + # Without a login profile the line goes into a new ~/.profile, once + [ "$(grep -c agy-cloud-profile.sh "$AGY_HOME/.profile")" = 1 ] || { echo "expected one line in .profile"; cat "$AGY_HOME/.profile"; exit 1; } + # With one, into the first bash reads + echo '# existing' >"$AGY_HOME/.bash_login" + out=$(cat ./cloud.bash | env -u CLAUDE_CODE_REMOTE HOME="$AGY_HOME" SHELLTIME_CLOUD=true SHELLTIME_TOKEN=st_v1_agy_setup bash 2>&1) || { echo "$out"; echo "cloud.bash exited non-zero"; exit 1; } + [ "$(grep -c agy-cloud-profile.sh "$AGY_HOME/.bash_login")" = 1 ] || { echo "expected one line in .bash_login"; cat "$AGY_HOME/.bash_login"; exit 1; } + [ "$(grep -c agy-cloud-profile.sh "$AGY_HOME/.profile")" = 1 ] || { echo "expected .profile unchanged"; cat "$AGY_HOME/.profile"; exit 1; } hook="$AGY_HOME/.shelltime/bin/agy-cloud-session-start.sh" hooks_json="$AGY_HOME/.gemini/config/hooks.json" @@ -204,19 +211,28 @@ jobs: [ "$(pgrep -fxc "$daemon")" = 1 ] || { echo "expected one daemon"; pgrep -fxa "$daemon"; exit 1; } pkill -fx "$daemon" - # A remote agent's command: its login shell reports it (to a stub here, so this doesn't - # depend on the released CLI) and leaves its output and exit status alone - [ "$(grep -c agy-cloud-profile.sh "$AGY_HOME/.profile")" = 1 ] || { echo "expected one profile line"; cat "$AGY_HOME/.profile"; exit 1; } + # A remote agent's command: its login shell reports it (to stubs here, so this doesn't + # depend on the released CLI), leaves its output and exit status alone, and doesn't + # wait for the daemon's start calls="$AGY_HOME/hook-ingest.calls" cat >"$AGY_HOME/.shelltime/bin/shelltime" <<'STUB' #!/bin/bash { echo "args: $*"; echo "stdin: $(cat)"; } >>"$HOME/hook-ingest.calls" echo '{}' STUB + cat >"$AGY_HOME/.shelltime/bin/cloud-start.sh" <<'STUB' + #!/bin/bash + sleep 3 + echo "cloud-start" >>"$HOME/hook-ingest.calls" + STUB rc=0 + started=$(date +%s) out=$(cd /tmp && env -u SHELLTIME_TOKEN HOME="$AGY_HOME" JULES_SESSION_ID=ci-session bash -l -c 'echo "a & b"; bash -l -c "echo nested"; sleep 0.2; exit 7') || rc=$? + [ $(( $(date +%s) - started )) -lt 3 ] || { echo "the command waited for cloud-start.sh"; exit 1; } [ "$out" = "$(printf 'a & b\nnested')" ] || { echo "unexpected output: $out"; exit 1; } [ "$rc" = 7 ] || { echo "exit status $rc, expected 7"; exit 1; } + for _ in 1 2 3 4 5 6 7 8 9 10; do grep -qx cloud-start "$calls" && break; sleep 1; done + grep -qx cloud-start "$calls" || { echo "cloud-start.sh never ran"; cat "$calls"; exit 1; } [ "$(grep -c '^args: ' "$calls")" = 1 ] || { echo "expected one report, nested shells included"; cat "$calls"; exit 1; } grep -Eq '^args: hook-ingest --agent agy --event ShellCommand --session ci-session --exit-code 7 --duration-ms [0-9]+$' "$calls" || { cat "$calls"; exit 1; } ms=$(sed -n 's/.*--duration-ms \([0-9]*\)$/\1/p' "$calls") @@ -225,8 +241,8 @@ jobs: # Outside a remote agent's conversation nothing is reported rm -f "$calls" env -u SHELLTIME_TOKEN -u JULES_SESSION_ID HOME="$AGY_HOME" bash -l -c true + sleep 4 [ ! -e "$calls" ] || { echo "reported without JULES_SESSION_ID"; cat "$calls"; exit 1; } - pkill -fx "$daemon" || true rm -rf "$AGY_HOME" - name: Run cloud.bash for Cursor Cloud Agents, twice diff --git a/CLAUDE.md b/CLAUDE.md index 10883b2..0797cb5 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -9,7 +9,7 @@ This is the **shelltime installation** repository - a collection of shell script ## Repository Structure - `install.bash` - Main installation script that downloads shelltime CLI binaries and sets up shell hooks -- `cloud.bash` - Setup for cloud agent environments (Claude Code cloud, Google Antigravity cloud, and Cursor Cloud Agents), run from an environment's setup/install script with `curl -fsSL https://raw.githubusercontent.com/shelltime/installation/main/cloud.bash | bash`. Detects Claude Code cloud (`CLAUDE_CODE_REMOTE=true`), Antigravity cloud (OS user `antigravity`, including when setup runs as `root` on behalf of `antigravity`), Cursor Cloud Agents (`CURSOR_CLOUD=true`/`1`, `CURSOR_BACKGROUND_AGENT=true`/`1`), or an explicit `SHELLTIME_CLOUD=true` (matching `~/.cursor`/`.cursor/*`/`cursor-agent` vs `~/.gemini`/`.agents/hooks.json`/`agy`, or both in a fresh VM); outside those it installs nothing (ignoring `CURSOR_CODE_REMOTE` and `CURSOR_AGENT`, which local/SSH machines also set). Installs the Linux CLI and daemon into `$TARGET_HOME/.shelltime/bin` (and symlinks into `/usr/local/bin` when writable). For Claude Code cloud, runs `shelltime cc install` and adds a Claude Code SessionStart hook (`~/.shelltime/bin/cloud-session-start.sh`) that signs in with `proxy-injected` (or `SHELLTIME_TOKEN`) and starts the daemon in each session. For Antigravity and Cursor cloud, installs `~/.shelltime/bin/cloud-start.sh` (signs in when `SHELLTIME_TOKEN` is in the env, clears stale `/tmp/shelltime.sock`, starts `shelltime-daemon` with `SHELLTIME_CLOUD=true`) and saves `SHELLTIME_TOKEN` to `config.yaml` if present during setup. For Antigravity, also adds `~/.shelltime/bin/agy-cloud-session-start.sh` under the separate `"shelltime-cloud"` group in `~/.gemini/config/hooks.json` (so `.agents/hooks.json` never yields to it) and fixes ownership to `antigravity` when setup ran as `root`. A remote agent (antigravity.google.com/remote) runs outside its VM and runs no hook there: it sends each command in through `cmdlauncher` as `bash -l -c ` with `JULES_SESSION_ID` set. So setup also writes `~/.shelltime/bin/agy-cloud-profile.sh` and sources it (bash only) from the login profile bash reads (the first of `~/.bash_profile`, `~/.bash_login`, `~/.profile`, else a new `~/.profile`; one line, kept on a rerun). With `JULES_SESSION_ID` and `BASH_EXECUTION_STRING` set it adds an EXIT trap that pipes the command to `shelltime hook-ingest --agent agy --event ShellCommand --session "$JULES_SESSION_ID" --exit-code [--duration-ms ]` (from `EPOCHREALTIME`, bash 5) and then runs `cloud-start.sh`; it prints nothing, keeps the command's exit status, and skips shells the command starts (`SHELLTIME_AGY_COMMAND`). It needs a CLI with the `ShellCommand` mode. For Cursor Cloud Agents (which ignore `~/.cursor/hooks.json`), also runs `cloud-start.sh` immediately when `SHELLTIME_TOKEN` is already set. A setup script that exits non-zero stops sessions from starting, so it always exits 0 (its `EXIT` trap reports a failure instead), and nothing runs before the final `main` call, so a truncated download installs nothing. The guides that document it are `content/blogs/track-claude-code-cloud-sessions/` and `content/blogs/track-cursor-and-antigravity/` in the web repo; keep them in step +- `cloud.bash` - Setup for cloud agent environments (Claude Code cloud, Google Antigravity cloud, and Cursor Cloud Agents), run from an environment's setup/install script with `curl -fsSL https://raw.githubusercontent.com/shelltime/installation/main/cloud.bash | bash`. Detects Claude Code cloud (`CLAUDE_CODE_REMOTE=true`), Antigravity cloud (OS user `antigravity`, including when setup runs as `root` on behalf of `antigravity`), Cursor Cloud Agents (`CURSOR_CLOUD=true`/`1`, `CURSOR_BACKGROUND_AGENT=true`/`1`), or an explicit `SHELLTIME_CLOUD=true` (matching `~/.cursor`/`.cursor/*`/`cursor-agent` vs `~/.gemini`/`.agents/hooks.json`/`agy`, or both in a fresh VM); outside those it installs nothing (ignoring `CURSOR_CODE_REMOTE` and `CURSOR_AGENT`, which local/SSH machines also set). Installs the Linux CLI and daemon into `$TARGET_HOME/.shelltime/bin` (and symlinks into `/usr/local/bin` when writable). For Claude Code cloud, runs `shelltime cc install` and adds a Claude Code SessionStart hook (`~/.shelltime/bin/cloud-session-start.sh`) that signs in with `proxy-injected` (or `SHELLTIME_TOKEN`) and starts the daemon in each session. For Antigravity and Cursor cloud, installs `~/.shelltime/bin/cloud-start.sh` (signs in when `SHELLTIME_TOKEN` is in the env, clears stale `/tmp/shelltime.sock`, starts `shelltime-daemon` with `SHELLTIME_CLOUD=true`) and saves `SHELLTIME_TOKEN` to `config.yaml` if present during setup. For Antigravity, also adds `~/.shelltime/bin/agy-cloud-session-start.sh` under the separate `"shelltime-cloud"` group in `~/.gemini/config/hooks.json` (so `.agents/hooks.json` never yields to it) and fixes ownership to `antigravity` when setup ran as `root`. A remote agent (antigravity.google.com/remote) runs outside its VM and runs no hook there: it sends each command in through `cmdlauncher` as `bash -l -c ` with `JULES_SESSION_ID` set. So setup also writes `~/.shelltime/bin/agy-cloud-profile.sh` and sources it (bash only) from the login profile bash reads (the first of `~/.bash_profile`, `~/.bash_login`, `~/.profile`, else a new `~/.profile`; one line, kept on a rerun) and, when setup may write it, from `/etc/profile.d/shelltime-agy-cloud.sh`, which still works if a tool creates a `~/.bash_profile` later. With `JULES_SESSION_ID` and `BASH_EXECUTION_STRING` set it adds an EXIT trap that pipes the command to `shelltime hook-ingest --agent agy --event ShellCommand --session "$JULES_SESSION_ID" --exit-code [--duration-ms ]` (from `EPOCHREALTIME`, bash 5) and then starts `cloud-start.sh` detached, so the command never waits on a sign-in or the daemon's start; it prints nothing, keeps the command's exit status, and runs once per command (`SHELLTIME_AGY_COMMAND` skips shells the command starts and a second profile sourcing it). A command that `exec`s, sets its own EXIT trap or is killed with SIGKILL is not reported. It needs a CLI with the `ShellCommand` mode. For Cursor Cloud Agents (which ignore `~/.cursor/hooks.json`), also runs `cloud-start.sh` immediately when `SHELLTIME_TOKEN` is already set. A setup script that exits non-zero stops sessions from starting, so it always exits 0 (its `EXIT` trap reports a failure instead), and nothing runs before the final `main` call, so a truncated download installs nothing. The guides that document it are `content/blogs/track-claude-code-cloud-sessions/` and `content/blogs/track-cursor-and-antigravity/` in the web repo; keep them in step - `hooks/` - Shell-specific hook scripts that track command execution. They are copies of the CLI's embedded hooks (`model/hooks/` in shelltime/cli, which `shelltime hooks install` and `shelltime init` write over these), so change both together: - `zsh.zsh` - Uses zsh's native `preexec` and `precmd` hooks - `bash.bash` - Uses `bash-preexec` library for preexec/precmd functionality @@ -30,7 +30,7 @@ Each `test-shells` job runs `bash ./install.bash` from its shell (zsh, fish or b 3. Homebrew path: the cask is installed and links `shelltime` and `shelltime-daemon` into brew's bin, with no `~/.shelltime/bin/shelltime` shadowing it. Manual path: both binaries are in `~/.shelltime/bin`, which the config puts on `PATH` 4. A new interactive shell loads the hook's functions and runs that `shelltime` (bash needs `-i`: Ubuntu's default `~/.bashrc` returns early in a non-interactive shell) -The `test-cloud` job checks that `cloud.bash` installs nothing without `CLAUDE_CODE_REMOTE=true`, then runs it twice with it on x86_64 and arm64 Linux and checks the binaries, the OTEL settings, that the SessionStart hook is registered exactly once, and that the hook prints nothing, signs in with `proxy-injected` (or `SHELLTIME_TOKEN` when set) and starts one daemon. The Antigravity run also checks the login profile line is there once and, with a stub `shelltime`, that a `bash -l -c` command under `JULES_SESSION_ID` keeps its output and exit status and is reported once (nested login shells included) with its duration, and that nothing is reported without `JULES_SESSION_ID`. +The `test-cloud` job checks that `cloud.bash` installs nothing without `CLAUDE_CODE_REMOTE=true`, then runs it twice with it on x86_64 and arm64 Linux and checks the binaries, the OTEL settings, that the SessionStart hook is registered exactly once, and that the hook prints nothing, signs in with `proxy-injected` (or `SHELLTIME_TOKEN` when set) and starts one daemon. The Antigravity run also checks the login profile line goes once into a new `~/.profile`, and on a third run into an existing `~/.bash_login` instead, and, with stub `shelltime` and `cloud-start.sh`, that a `bash -l -c` command under `JULES_SESSION_ID` keeps its output and exit status, doesn't wait for `cloud-start.sh`, and is reported once (nested login shells included) with its duration, and that nothing is reported without `JULES_SESSION_ID`. ## How Shell Hooks Work diff --git a/cloud.bash b/cloud.bash index 5b73f80..2e3aff8 100755 --- a/cloud.bash +++ b/cloud.bash @@ -296,9 +296,13 @@ with open(path + ".tmp", "w", encoding="utf-8") as f: # 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). Without EPOCHREALTIME (bash < 5) there is no -# duration. The command is recorded before the daemon is started: without a -# daemon it goes to the pending file, which a starting daemon sends first. +# 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 @@ -318,15 +322,24 @@ if [ -n "${JULES_SESSION_ID:-}" ] && [ -n "${BASH_EXECUTION_STRING:-}" ] && fi printf '%s' "$BASH_EXECUTION_STRING" | "$st/bin/shelltime" hook-ingest --agent agy --event ShellCommand \ --session "$JULES_SESSION_ID" --exit-code "$status" ${ms:+--duration-ms "$ms"} >/dev/null 2>&1 - "$st/bin/cloud-start.sh" >/dev/null 2>&1 /dev/null 2>&1 /etc/profile.d/shelltime-agy-cloud.sh + fi LOGIN_PROFILE_PATH="$TARGET_HOME/.profile" for name in .bash_profile .bash_login .profile; do if [ -f "$TARGET_HOME/$name" ]; then @@ -336,9 +349,7 @@ EOF 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# ShellTime: record the commands an Antigravity remote agent runs\n%s\n' \ - 'if [ -n "$BASH_VERSION" ] && [ -r "$HOME/.shelltime/bin/agy-cloud-profile.sh" ]; then . "$HOME/.shelltime/bin/agy-cloud-profile.sh"; fi' \ - >>"$LOGIN_PROFILE_PATH" + printf '\n%s\n%s\n' "$comment" "$line" >>"$LOGIN_PROFILE_PATH" fi } From b91b5e964058fe5679c438ba3f1307f9581bb171 Mon Sep 17 00:00:00 2001 From: AnnatarHe Date: Sun, 29 Nov 2026 23:29:43 +0800 Subject: [PATCH 3/3] fix(cloud): start one daemon when remote agent commands start it together Each remote agent command starts cloud-start.sh detached, so parallel commands could both find no daemon and remove each other's socket. cloud-start.sh now runs one at a time under flock (the daemon doesn't keep the lock). The EXIT trap resets IFS and globbing the command may have changed, marks the event cloud with SHELLTIME_CLOUD=true whatever the OS user, keeps its own variables from the daemon, and a profile that can't be written no longer stops the rest of setup. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test.yaml | 8 ++++++++ CLAUDE.md | 4 ++-- cloud.bash | 25 +++++++++++++++++-------- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 6666831..b797baa 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -210,6 +210,14 @@ jobs: grep -qx "token: st_v1_agy_runtime" "$config" || { echo "expected runtime token"; cat "$config"; exit 1; } [ "$(pgrep -fxc "$daemon")" = 1 ] || { echo "expected one daemon"; pgrep -fxa "$daemon"; exit 1; } pkill -fx "$daemon" + for _ in 1 2 3 4 5 6 7 8 9 10; do pgrep -fx "$daemon" >/dev/null || break; sleep 0.5; done + + # Commands that start it together (each remote agent command does) start one daemon, which keeps its socket + for _ in 1 2 3 4; do env -u SHELLTIME_TOKEN HOME="$AGY_HOME" "$AGY_HOME/.shelltime/bin/cloud-start.sh" & done + wait + [ "$(pgrep -fxc "$daemon")" = 1 ] || { echo "expected one daemon"; pgrep -fxa "$daemon"; exit 1; } + [ -S /tmp/shelltime.sock ] || { echo "daemon socket missing"; cat "$AGY_HOME/.shelltime/logs/shelltime-daemon.log"; exit 1; } + pkill -fx "$daemon" # A remote agent's command: its login shell reports it (to stubs here, so this doesn't # depend on the released CLI), leaves its output and exit status alone, and doesn't diff --git a/CLAUDE.md b/CLAUDE.md index 0797cb5..3eddc90 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -9,7 +9,7 @@ This is the **shelltime installation** repository - a collection of shell script ## Repository Structure - `install.bash` - Main installation script that downloads shelltime CLI binaries and sets up shell hooks -- `cloud.bash` - Setup for cloud agent environments (Claude Code cloud, Google Antigravity cloud, and Cursor Cloud Agents), run from an environment's setup/install script with `curl -fsSL https://raw.githubusercontent.com/shelltime/installation/main/cloud.bash | bash`. Detects Claude Code cloud (`CLAUDE_CODE_REMOTE=true`), Antigravity cloud (OS user `antigravity`, including when setup runs as `root` on behalf of `antigravity`), Cursor Cloud Agents (`CURSOR_CLOUD=true`/`1`, `CURSOR_BACKGROUND_AGENT=true`/`1`), or an explicit `SHELLTIME_CLOUD=true` (matching `~/.cursor`/`.cursor/*`/`cursor-agent` vs `~/.gemini`/`.agents/hooks.json`/`agy`, or both in a fresh VM); outside those it installs nothing (ignoring `CURSOR_CODE_REMOTE` and `CURSOR_AGENT`, which local/SSH machines also set). Installs the Linux CLI and daemon into `$TARGET_HOME/.shelltime/bin` (and symlinks into `/usr/local/bin` when writable). For Claude Code cloud, runs `shelltime cc install` and adds a Claude Code SessionStart hook (`~/.shelltime/bin/cloud-session-start.sh`) that signs in with `proxy-injected` (or `SHELLTIME_TOKEN`) and starts the daemon in each session. For Antigravity and Cursor cloud, installs `~/.shelltime/bin/cloud-start.sh` (signs in when `SHELLTIME_TOKEN` is in the env, clears stale `/tmp/shelltime.sock`, starts `shelltime-daemon` with `SHELLTIME_CLOUD=true`) and saves `SHELLTIME_TOKEN` to `config.yaml` if present during setup. For Antigravity, also adds `~/.shelltime/bin/agy-cloud-session-start.sh` under the separate `"shelltime-cloud"` group in `~/.gemini/config/hooks.json` (so `.agents/hooks.json` never yields to it) and fixes ownership to `antigravity` when setup ran as `root`. A remote agent (antigravity.google.com/remote) runs outside its VM and runs no hook there: it sends each command in through `cmdlauncher` as `bash -l -c ` with `JULES_SESSION_ID` set. So setup also writes `~/.shelltime/bin/agy-cloud-profile.sh` and sources it (bash only) from the login profile bash reads (the first of `~/.bash_profile`, `~/.bash_login`, `~/.profile`, else a new `~/.profile`; one line, kept on a rerun) and, when setup may write it, from `/etc/profile.d/shelltime-agy-cloud.sh`, which still works if a tool creates a `~/.bash_profile` later. With `JULES_SESSION_ID` and `BASH_EXECUTION_STRING` set it adds an EXIT trap that pipes the command to `shelltime hook-ingest --agent agy --event ShellCommand --session "$JULES_SESSION_ID" --exit-code [--duration-ms ]` (from `EPOCHREALTIME`, bash 5) and then starts `cloud-start.sh` detached, so the command never waits on a sign-in or the daemon's start; it prints nothing, keeps the command's exit status, and runs once per command (`SHELLTIME_AGY_COMMAND` skips shells the command starts and a second profile sourcing it). A command that `exec`s, sets its own EXIT trap or is killed with SIGKILL is not reported. It needs a CLI with the `ShellCommand` mode. For Cursor Cloud Agents (which ignore `~/.cursor/hooks.json`), also runs `cloud-start.sh` immediately when `SHELLTIME_TOKEN` is already set. A setup script that exits non-zero stops sessions from starting, so it always exits 0 (its `EXIT` trap reports a failure instead), and nothing runs before the final `main` call, so a truncated download installs nothing. The guides that document it are `content/blogs/track-claude-code-cloud-sessions/` and `content/blogs/track-cursor-and-antigravity/` in the web repo; keep them in step +- `cloud.bash` - Setup for cloud agent environments (Claude Code cloud, Google Antigravity cloud, and Cursor Cloud Agents), run from an environment's setup/install script with `curl -fsSL https://raw.githubusercontent.com/shelltime/installation/main/cloud.bash | bash`. Detects Claude Code cloud (`CLAUDE_CODE_REMOTE=true`), Antigravity cloud (OS user `antigravity`, including when setup runs as `root` on behalf of `antigravity`), Cursor Cloud Agents (`CURSOR_CLOUD=true`/`1`, `CURSOR_BACKGROUND_AGENT=true`/`1`), or an explicit `SHELLTIME_CLOUD=true` (matching `~/.cursor`/`.cursor/*`/`cursor-agent` vs `~/.gemini`/`.agents/hooks.json`/`agy`, or both in a fresh VM); outside those it installs nothing (ignoring `CURSOR_CODE_REMOTE` and `CURSOR_AGENT`, which local/SSH machines also set). Installs the Linux CLI and daemon into `$TARGET_HOME/.shelltime/bin` (and symlinks into `/usr/local/bin` when writable). For Claude Code cloud, runs `shelltime cc install` and adds a Claude Code SessionStart hook (`~/.shelltime/bin/cloud-session-start.sh`) that signs in with `proxy-injected` (or `SHELLTIME_TOKEN`) and starts the daemon in each session. For Antigravity and Cursor cloud, installs `~/.shelltime/bin/cloud-start.sh` (one at a time under `flock` on `~/.shelltime/cloud-start.lock` when `flock` exists, which the daemon doesn't inherit; signs in when `SHELLTIME_TOKEN` is in the env, clears stale `/tmp/shelltime.sock`, starts `shelltime-daemon` with `SHELLTIME_CLOUD=true`) and saves `SHELLTIME_TOKEN` to `config.yaml` if present during setup. For Antigravity, also adds `~/.shelltime/bin/agy-cloud-session-start.sh` under the separate `"shelltime-cloud"` group in `~/.gemini/config/hooks.json` (so `.agents/hooks.json` never yields to it) and fixes ownership to `antigravity` when setup ran as `root`. A remote agent (antigravity.google.com/remote) runs outside its VM and runs no hook there: it sends each command in through `cmdlauncher` as `bash -l -c ` with `JULES_SESSION_ID` set. So setup also writes `~/.shelltime/bin/agy-cloud-profile.sh` and sources it (bash only) from the login profile bash reads (the first of `~/.bash_profile`, `~/.bash_login`, `~/.profile`, else a new `~/.profile`; one line, kept on a rerun) and, when setup may write it, from `/etc/profile.d/shelltime-agy-cloud.sh`, which still works if a tool creates a `~/.bash_profile` later. With `JULES_SESSION_ID` and `BASH_EXECUTION_STRING` set it adds an EXIT trap that pipes the command to `shelltime hook-ingest --agent agy --event ShellCommand --session "$JULES_SESSION_ID" --exit-code [--duration-ms ]` (from `EPOCHREALTIME`, bash 5) (with `SHELLTIME_CLOUD=true`, so it is marked cloud whatever the OS user) and then starts `cloud-start.sh` detached without the profile's variables, so the command never waits on a sign-in or the daemon's start; it resets `IFS` and globbing first, since it runs in the command's shell; it prints nothing, keeps the command's exit status, and runs once per command (`SHELLTIME_AGY_COMMAND` skips shells the command starts and a second profile sourcing it). A command that `exec`s, sets its own EXIT trap or is killed with SIGKILL is not reported. It needs a CLI with the `ShellCommand` mode. For Cursor Cloud Agents (which ignore `~/.cursor/hooks.json`), also runs `cloud-start.sh` immediately when `SHELLTIME_TOKEN` is already set. A setup script that exits non-zero stops sessions from starting, so it always exits 0 (its `EXIT` trap reports a failure instead), and nothing runs before the final `main` call, so a truncated download installs nothing. The guides that document it are `content/blogs/track-claude-code-cloud-sessions/` and `content/blogs/track-cursor-and-antigravity/` in the web repo; keep them in step - `hooks/` - Shell-specific hook scripts that track command execution. They are copies of the CLI's embedded hooks (`model/hooks/` in shelltime/cli, which `shelltime hooks install` and `shelltime init` write over these), so change both together: - `zsh.zsh` - Uses zsh's native `preexec` and `precmd` hooks - `bash.bash` - Uses `bash-preexec` library for preexec/precmd functionality @@ -30,7 +30,7 @@ Each `test-shells` job runs `bash ./install.bash` from its shell (zsh, fish or b 3. Homebrew path: the cask is installed and links `shelltime` and `shelltime-daemon` into brew's bin, with no `~/.shelltime/bin/shelltime` shadowing it. Manual path: both binaries are in `~/.shelltime/bin`, which the config puts on `PATH` 4. A new interactive shell loads the hook's functions and runs that `shelltime` (bash needs `-i`: Ubuntu's default `~/.bashrc` returns early in a non-interactive shell) -The `test-cloud` job checks that `cloud.bash` installs nothing without `CLAUDE_CODE_REMOTE=true`, then runs it twice with it on x86_64 and arm64 Linux and checks the binaries, the OTEL settings, that the SessionStart hook is registered exactly once, and that the hook prints nothing, signs in with `proxy-injected` (or `SHELLTIME_TOKEN` when set) and starts one daemon. The Antigravity run also checks the login profile line goes once into a new `~/.profile`, and on a third run into an existing `~/.bash_login` instead, and, with stub `shelltime` and `cloud-start.sh`, that a `bash -l -c` command under `JULES_SESSION_ID` keeps its output and exit status, doesn't wait for `cloud-start.sh`, and is reported once (nested login shells included) with its duration, and that nothing is reported without `JULES_SESSION_ID`. +The `test-cloud` job checks that `cloud.bash` installs nothing without `CLAUDE_CODE_REMOTE=true`, then runs it twice with it on x86_64 and arm64 Linux and checks the binaries, the OTEL settings, that the SessionStart hook is registered exactly once, and that the hook prints nothing, signs in with `proxy-injected` (or `SHELLTIME_TOKEN` when set) and starts one daemon. The Antigravity run also checks the login profile line goes once into a new `~/.profile`, and on a third run into an existing `~/.bash_login` instead, and, with stub `shelltime` and `cloud-start.sh`, that four `cloud-start.sh` started together leave one daemon with its socket, and that a `bash -l -c` command under `JULES_SESSION_ID` keeps its output and exit status, doesn't wait for `cloud-start.sh`, and is reported once (nested login shells included) with its duration, and that nothing is reported without `JULES_SESSION_ID`. ## How Shell Hooks Work diff --git a/cloud.bash b/cloud.bash index 2e3aff8..69d4976 100755 --- a/cloud.bash +++ b/cloud.bash @@ -191,12 +191,18 @@ install_cloud_start_script() { 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 >"$st/logs/shelltime-daemon.log" 2>&1 &- & 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 @@ -315,14 +321,14 @@ if [ -n "${JULES_SESSION_ID:-}" ] && [ -n "${BASH_EXECUTION_STRING:-}" ] && export SHELLTIME_AGY_COMMAND=1 __shelltime_agy_start=${EPOCHREALTIME:-} __shelltime_agy_report() { - local status=$? st="$HOME/.shelltime" ms= - set +eu + 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" | "$st/bin/shelltime" hook-ingest --agent agy --event ShellCommand \ - --session "$JULES_SESSION_ID" --exit-code "$status" ${ms:+--duration-ms "$ms"} >/dev/null 2>&1 - ("$st/bin/cloud-start.sh" >/dev/null 2>&1 /dev/null 2>&1 + (unset SHELLTIME_AGY_COMMAND JULES_SESSION_ID; "$st/bin/cloud-start.sh" >/dev/null 2>&1 /etc/profile.d/shelltime-agy-cloud.sh + 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 @@ -349,7 +357,8 @@ EOF 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" + 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 }