diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4ed4fc0..f754e8e 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -115,12 +115,12 @@ jobs: - name: Outside a cloud environment cloud.bash installs nothing run: | set -euo pipefail - out=$(cat ./cloud.bash | env -u CLAUDE_CODE_REMOTE bash 2>&1) || { echo "$out"; echo "cloud.bash exited non-zero"; exit 1; } + out=$(cat ./cloud.bash | env -u CLAUDE_CODE_REMOTE -u CURSOR_CLOUD -u CURSOR_BACKGROUND_AGENT -u SHELLTIME_CLOUD bash 2>&1) || { echo "$out"; echo "cloud.bash exited non-zero"; exit 1; } echo "$out" - echo "$out" | grep -q "only runs in Claude Code cloud environments" || { echo "missing the refusal"; exit 1; } + echo "$out" | grep -q "only runs in cloud agent environments" || { echo "missing the refusal"; exit 1; } [ ! -e "$HOME/.shelltime" ] || { echo "~/.shelltime was created"; exit 1; } - - name: Run cloud.bash the way a setup script does, twice + - name: Run cloud.bash for Claude Code cloud, twice run: | set -euo pipefail for run in 1 2; do @@ -130,7 +130,7 @@ jobs: echo "$out" | grep -q "ShellTime is installed" || { echo "run $run did not finish"; exit 1; } done - - name: Verify installation + - name: Verify Claude Code cloud installation run: | set -euo pipefail hook="$HOME/.shelltime/bin/cloud-session-start.sh" @@ -159,3 +159,85 @@ jobs: grep -qx "token: proxy-injected" "$config" || { echo "expected the proxy-injected token again"; cat "$config"; exit 1; } [ "$(pgrep -fxc "$daemon")" = 1 ] || { echo "expected one daemon"; pgrep -fxa "$daemon"; exit 1; } pkill -fx "$daemon" + + - name: Run cloud.bash for Antigravity cloud, twice + run: | + set -euo pipefail + AGY_HOME=$(mktemp -d) + # Preserve an existing unrelated hook group in hooks.json + mkdir -p "$AGY_HOME/.gemini/config" + echo '{"custom-hook":{"SessionStart":[{"type":"command","command":"echo {}"}]}}' >"$AGY_HOME/.gemini/config/hooks.json" + + for run in 1 2; do + 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; } + echo "$out" + echo "$out" | grep -q "ShellTime is installed; Antigravity sessions" || { echo "run $run did not finish"; exit 1; } + done + + hook="$AGY_HOME/.shelltime/bin/agy-cloud-session-start.sh" + hooks_json="$AGY_HOME/.gemini/config/hooks.json" + config="$AGY_HOME/.shelltime/config.yaml" + daemon="$AGY_HOME/.shelltime/bin/shelltime-daemon" + + [ -x "$AGY_HOME/.shelltime/bin/shelltime" ] || { echo "shelltime missing"; exit 1; } + [ -x "$daemon" ] || { echo "shelltime-daemon missing"; exit 1; } + [ -x "$hook" ] || { echo "agy SessionStart hook missing"; exit 1; } + grep -qx "token: st_v1_agy_setup" "$config" || { echo "expected baked setup token"; cat "$config"; exit 1; } + + # Verify strict JSON, separate "shelltime-cloud" group, no "shelltime" group, existing hook preserved + jq -e '."custom-hook".SessionStart | length == 1' "$hooks_json" + jq -e 'has("shelltime") | not' "$hooks_json" + jq -e --arg cmd "$hook" '."shelltime-cloud".SessionStart | length == 1 and .[0].command == $cmd' "$hooks_json" + + # Hook prints {} (required by Antigravity), keeps baked token when env var is absent, and starts daemon once + out=$(env -u SHELLTIME_TOKEN HOME="$AGY_HOME" "$hook") + [ "$out" = "{}" ] || { echo "expected '{}', got: $out"; exit 1; } + grep -qx "token: st_v1_agy_setup" "$config" || { echo "baked token was overwritten"; cat "$config"; exit 1; } + for _ in 1 2 3 4 5; do pgrep -fx "$daemon" >/dev/null && [ -S /tmp/shelltime.sock ] && break; sleep 1; done + [ -S /tmp/shelltime.sock ] || { echo "daemon socket not created"; cat "$AGY_HOME/.shelltime/logs/shelltime-daemon.log"; exit 1; } + pgrep -fx "$daemon" >/dev/null || { echo "daemon not started"; cat "$AGY_HOME/.shelltime/logs/shelltime-daemon.log"; exit 1; } + + # Runtime SHELLTIME_TOKEN overrides baked token and doesn't start a second daemon + out=$(SHELLTIME_TOKEN=st_v1_agy_runtime HOME="$AGY_HOME" "$hook") + [ "$out" = "{}" ] || { echo "expected '{}', got: $out"; exit 1; } + 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" + rm -rf "$AGY_HOME" + + - name: Run cloud.bash for Cursor Cloud Agents, twice + run: | + set -euo pipefail + CURSOR_HOME=$(mktemp -d) + mkdir -p "$CURSOR_HOME/.cursor" + + # 1. Install step (no token yet, SHELLTIME_CLOUD=true + ~/.cursor present) + out=$(cat ./cloud.bash | env -u CLAUDE_CODE_REMOTE -u SHELLTIME_TOKEN HOME="$CURSOR_HOME" SHELLTIME_CLOUD=true bash 2>&1) || { echo "$out"; echo "cloud.bash exited non-zero"; exit 1; } + echo "$out" + echo "$out" | grep -q "ShellTime is installed; Cursor Cloud Agent sessions" || { echo "install step did not finish"; exit 1; } + + start_hook="$CURSOR_HOME/.shelltime/bin/cloud-start.sh" + config="$CURSOR_HOME/.shelltime/config.yaml" + daemon="$CURSOR_HOME/.shelltime/bin/shelltime-daemon" + + [ -x "$CURSOR_HOME/.shelltime/bin/shelltime" ] || { echo "shelltime missing"; exit 1; } + [ -x "$daemon" ] || { echo "shelltime-daemon missing"; exit 1; } + [ -x "$start_hook" ] || { echo "cloud-start.sh missing"; exit 1; } + # Cursor Cloud Agents ignore ~/.cursor/hooks.json and should not get ~/.gemini/config/hooks.json + [ ! -e "$CURSOR_HOME/.cursor/hooks.json" ] || { echo "should not write ~/.cursor/hooks.json"; exit 1; } + [ ! -e "$CURSOR_HOME/.gemini/config/hooks.json" ] || { echo "should not write ~/.gemini/config/hooks.json"; exit 1; } + + # 2. Start step with CURSOR_CLOUD=true and SHELLTIME_TOKEN starts the daemon immediately + out=$(cat ./cloud.bash | env -u CLAUDE_CODE_REMOTE -u SHELLTIME_CLOUD HOME="$CURSOR_HOME" CURSOR_CLOUD=true SHELLTIME_TOKEN=st_v1_cursor_token bash 2>&1) || { echo "$out"; echo "cloud.bash exited non-zero"; exit 1; } + echo "$out" + echo "$out" | grep -q "ShellTime is installed; Cursor Cloud Agent sessions" || { echo "start step did not finish"; exit 1; } + grep -qx "token: st_v1_cursor_token" "$config" || { echo "expected cursor token"; cat "$config"; exit 1; } + for _ in 1 2 3 4 5; do pgrep -fx "$daemon" >/dev/null && [ -S /tmp/shelltime.sock ] && break; sleep 1; done + pgrep -fx "$daemon" >/dev/null || { echo "daemon not started"; cat "$CURSOR_HOME/.shelltime/logs/shelltime-daemon.log"; exit 1; } + + # Re-running cloud-start.sh rotates token without starting a second daemon + SHELLTIME_TOKEN=st_v1_cursor_rotated HOME="$CURSOR_HOME" "$start_hook" + grep -qx "token: st_v1_cursor_rotated" "$config" || { echo "expected rotated cursor token"; cat "$config"; exit 1; } + [ "$(pgrep -fxc "$daemon")" = 1 ] || { echo "expected one daemon"; pgrep -fxa "$daemon"; exit 1; } + pkill -fx "$daemon" + rm -rf "$CURSOR_HOME" diff --git a/CLAUDE.md b/CLAUDE.md index 00796ad..5634b2e 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 Claude Code cloud environments, run from an environment's setup script with `curl -fsSL https://raw.githubusercontent.com/shelltime/installation/main/cloud.bash | bash`. Installs the Linux CLI and daemon into `~/.shelltime/bin`, runs `shelltime cc install`, and adds a Claude Code SessionStart hook (`~/.shelltime/bin/cloud-session-start.sh`, embedded in the script) that signs in and starts the daemon in each session. The token is a network secret of the environment (host `api.shelltime.xyz`, header `Authorization`, prefix `CLI`) that Anthropic's agent proxy adds to requests outside the VM, so the hook signs in with the placeholder `proxy-injected` (the CLI then sends no `Authorization` header; needs a CLI release with `model.ProxyInjectedToken`). `SHELLTIME_TOKEN` in the environment variables, for environments without network secrets, replaces the placeholder. Setup scripts get neither. It installs nothing unless `CLAUDE_CODE_REMOTE` is `true`, which cloud environments set (the setup script inherits it from `environment-manager`) and which is never true on a user's own machine. 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 guide that documents it is `content/blogs/track-claude-code-cloud-sessions/` in the web repo; keep the two 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`. 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 diff --git a/README.md b/README.md index 6dc0747..c8df9bf 100644 --- a/README.md +++ b/README.md @@ -10,15 +10,19 @@ You can install shelltime tools by running the following command in your termina curl -sSL https://shelltime.xyz/i | bash ``` -## Claude Code Cloud Environments +## Cloud Agent Environments (Claude Code, Google Antigravity & Cursor) -To track Claude Code sessions that run in the cloud ([claude.ai/code](https://claude.ai/code)), add this line to the cloud environment's setup script: +To track cloud sessions in **Claude Code** ([claude.ai/code](https://claude.ai/code)), **Google Antigravity** ([antigravity.google.com](https://antigravity.google.com)), or **Cursor Cloud Agents** ([cursor.com](https://cursor.com/docs/cloud-agent/setup)), add this line to the environment's setup/install script: ```bash curl -fsSL https://raw.githubusercontent.com/shelltime/installation/main/cloud.bash | bash ``` -Then add your ShellTime API token to the environment as a network secret: allowed website `api.shelltime.xyz`, header `Authorization`, prefix `CLI`. Anthropic's agent proxy adds it to the daemon's requests outside the session's VM, so sessions never see the token, and the host needs no network access setting. [cloud.bash](cloud.bash) installs the CLI and daemon, connects Claude Code, and adds a SessionStart hook that signs in and starts the daemon in every session. An environment without network secrets can hold the token as `SHELLTIME_TOKEN` in its environment variables instead, with `api.shelltime.xyz` allowed by its network access; the hook uses it when it's set. It never fails the setup script, and outside a cloud environment (where `CLAUDE_CODE_REMOTE` isn't `true`) it installs nothing. The full guide: [Track your Claude Code cloud sessions](https://shelltime.xyz/blogs/track-claude-code-cloud-sessions). +- **Claude Code cloud (`CLAUDE_CODE_REMOTE=true`)**: Add your ShellTime API token to the environment as a network secret: allowed website `api.shelltime.xyz`, header `Authorization`, prefix `CLI`. Anthropic's agent proxy adds it to the daemon's requests outside the session's VM, so sessions never see the token, and the host needs no network access setting. [cloud.bash](cloud.bash) installs the CLI and daemon, connects Claude Code, and adds a SessionStart hook that signs in and starts the daemon in every session. An environment without network secrets can hold the token as `SHELLTIME_TOKEN` in its environment variables instead, with `api.shelltime.xyz` allowed by its network access; the hook uses it when it's set. Guide: [Track your Claude Code cloud sessions](https://shelltime.xyz/blogs/track-claude-code-cloud-sessions). +- **Google Antigravity cloud (OS user `antigravity`, or `SHELLTIME_CLOUD=true`)**: Commit `.agents/hooks.json` to your repository (`shelltime agy install`) and pass your ShellTime API token via `SHELLTIME_TOKEN` (exported in the setup script or set in the project's environment variables). [cloud.bash](cloud.bash) installs the CLI and daemon (automatically targeting the `antigravity` user when setup runs as `root`), saves `SHELLTIME_TOKEN` into `~/.shelltime/config.yaml` when present, and registers a SessionStart hook in `~/.gemini/config/hooks.json` (`shelltime-cloud`) that starts `shelltime-daemon` in every live session. Guide: [Track Cursor and Antigravity with ShellTime](https://shelltime.xyz/blogs/track-cursor-and-antigravity). +- **Cursor Cloud Agents (`CURSOR_CLOUD=true` / `CURSOR_BACKGROUND_AGENT=true`, or `SHELLTIME_CLOUD=true`)**: Commit `.cursor/hooks.json` to your repository (`shelltime cursor install`) and add `SHELLTIME_TOKEN` + `SHELLTIME_CLOUD=true` to the environment's secrets. Run `cloud.bash` in the 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 set, it signs in and starts `shelltime-daemon` immediately). Guide: [Track Cursor and Antigravity with ShellTime](https://shelltime.xyz/blogs/track-cursor-and-antigravity). + +[cloud.bash](cloud.bash) never fails the setup script, and outside a cloud environment (no `CLAUDE_CODE_REMOTE=true`, no `antigravity` user, no `CURSOR_CLOUD`/`CURSOR_BACKGROUND_AGENT`, and no `SHELLTIME_CLOUD=true`) it installs nothing. ## What Next? diff --git a/cloud.bash b/cloud.bash index 73a9b9b..f218ec2 100755 --- a/cloud.bash +++ b/cloud.bash @@ -1,27 +1,65 @@ #!/bin/bash # -# Sets ShellTime up in a Claude Code cloud environment (claude.ai/code), so the -# sessions started in it are tracked like the ones on your own machines. Add -# this line to the environment's setup script: +# 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 # -# Then 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 this only installs; a SessionStart hook signs in and starts the daemon in -# every session. Anywhere CLAUDE_CODE_REMOTE isn't true, it installs nothing. +# 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) 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. +# +# 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). # -# Guide: https://shelltime.xyz/blogs/track-claude-code-cloud-sessions +# 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 -ST_DIR="$HOME/.shelltime" -HOOK_PATH="$ST_DIR/bin/cloud-session-start.sh" -SETTINGS_PATH="$HOME/.claude/settings.json" 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="" # A setup script that exits non-zero keeps the environment's sessions from # starting, so a failure is reported here and never passed on. @@ -38,7 +76,78 @@ finish() { trap finish EXIT set -euo pipefail -# 1. CLI and daemon, where install.bash puts them (the ShellTime mod looks there) +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" +} + +# 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 @@ -50,23 +159,53 @@ install_cli() { ;; esac - mkdir -p "$ST_DIR/bin" "$ST_DIR/logs" "$HOME/.local/bin" + 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" "$HOME/.local/bin/shelltime" + 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" +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 || sleep 1 + done +fi +exit 0 +EOF + chmod 0755 "$CLOUD_START_PATH" } -# 2. Claude Code's OTEL exporter pointed at the daemon +# 2a. Claude Code's OTEL exporter pointed at the daemon connect_claude_code() { - "$ST_DIR/bin/shelltime" cc install + HOME="$TARGET_HOME" "$ST_DIR/bin/shelltime" cc install } -# 3. The snapshot keeps files but no processes, there is no systemd, and only -# sessions see SHELLTIME_TOKEN, so a SessionStart hook signs in and starts the -# daemon in every session -install_session_hook() { - cat >"$HOOK_PATH" <<'EOF' +# 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. @@ -76,7 +215,7 @@ st="$HOME/.shelltime" # 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 +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 @@ -84,29 +223,123 @@ if ! pgrep -fx "$st/bin/shelltime-daemon" >/dev/null; then fi exit 0 EOF - chmod 0755 "$HOOK_PATH" + chmod 0755 "$CLAUDE_HOOK_PATH" # Replaces the hook an earlier run added instead of adding a second one - jq --arg cmd "$HOOK_PATH" ' + jq --arg cmd "$CLAUDE_HOOK_PATH" ' .hooks.SessionStart = [(.hooks.SessionStart // [])[] | select(all(.hooks[]?; .command != $cmd))] + [{hooks: [{type: "command", command: $cmd}]}] - ' "$SETTINGS_PATH" >"$SETTINGS_PATH.tmp" - mv "$SETTINGS_PATH.tmp" "$SETTINGS_PATH" + ' "$CLAUDE_SETTINGS_PATH" >"$CLAUDE_SETTINGS_PATH.tmp" + mv "$CLAUDE_SETTINGS_PATH.tmp" "$CLAUDE_SETTINGS_PATH" } -main() { - # Cloud environments set CLAUDE_CODE_REMOTE=true, and it's never true on - # your own machine, where this would swap in Linux binaries and add a hook - if [ "${CLAUDE_CODE_REMOTE:-}" != "true" ]; then - echo "cloud.bash only runs in Claude Code cloud environments (CLAUDE_CODE_REMOTE=true); on your own machine run: curl -sSL https://shelltime.xyz/i | bash" >&2 +# 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 +} + +# 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 + 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 - connect_claude_code - install_session_hook - echo "ShellTime is installed; Claude Code sessions will be tracked once your ShellTime token is a network secret for api.shelltime.xyz" + + 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 + 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 (ensure .agents/hooks.json is committed in your repo)" + 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