Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (optional): The new checks don't cover the /etc/profile.d/shelltime-agy-cloud.sh branch of install_agy_shell_profile, which is the only fallback when a tool later creates ~/.bash_profile. The runner isn't root, so that branch is skipped. A typo in the generated file, or a /etc/profile that doesn't source profile.d, goes unnoticed. Fix: add a root run (for example via sudo, then remove the file) or at least assert the file's content and that a bash -l reads it.

[ "$(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"
Expand Down Expand Up @@ -203,6 +210,47 @@ 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
# 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")
[ "$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
sleep 4
[ ! -e "$calls" ] || { echo "reported without JULES_SESSION_ID"; cat "$calls"; exit 1; }
rm -rf "$AGY_HOME"

- name: Run cloud.bash for Cursor Cloud Agents, twice
Expand Down
4 changes: 2 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` (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 <command>` 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 <status> [--duration-ms <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
Expand All @@ -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 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

Expand Down
Loading
Loading