aw-watcher-agent

v0.2.0 ActivityWatch watcher for AI coding assistants (gptme, Claude Code, Codex) packages/aw-watcher-agent View on GitHub

aw-watcher-agent

An ActivityWatch watcher for AI coding assistants (gptme, Claude Code, Codex). It logs agent sessions and per-tool activity to your local aw-server, so AI work shows up in the aw-webui Timeline alongside window/AFK data instead of looking like idle time.

Status: alpha (0.2.0). Session tracking and the Codex log-tailer work and are dogfooded against a local aw-server, as is the Claude Code log-tailer. A native gptme plugin hook for per-tool events and an aw-webui "AI work" view are not built yet.

This is the controller use case behind ActivityWatch/activitywatch#1215.

Design

Install

pipx install aw-watcher-agent   # or: pip install aw-watcher-agent / uv tool install aw-watcher-agent
# latest unreleased master:
pip install "git+https://github.com/gptme/gptme-contrib#subdirectory=packages/aw-watcher-agent"
# or, from a gptme-contrib checkout:
pip install -e packages/aw-watcher-agent

This installs the aw-watcher-agent command. You need a running aw-server (ActivityWatch's default on port 5600).

Usage

Sessions

# Create the session bucket (idempotent; emit-start also does this)
aw-watcher-agent ensure-bucket

# At session start
aw-watcher-agent emit-start \
  --harness claude-code --model claude-opus-4-7 \
  --category code --session-id 8531 --trigger autonomous --workspace myagent

# At session end (reads the saved start state, records duration + outcome)
aw-watcher-agent emit-end --session-id 8531 --outcome productive

Events land in the bucket aw-watcher-agent_<hostname> (type app.agent.session), which appears in aw-webui automatically.

Session flags (shared by emit-start, emit-end and emit-activity): --harness, --model, --category, --session-id, --trigger, --workspace, --hostname (default: the machine hostname) and --server (aw-server base URL). emit-end additionally takes --outcome and --duration (seconds; used only when no start state was recorded). Values passed to emit-end override the ones saved at start.

Start state is kept per session id under $XDG_STATE_HOME/aw-watcher-agent/ (default ~/.local/state/aw-watcher-agent/).

Claude Code without hooks (log-tailer)

tail-claude-code reads the transcripts Claude Code already writes under ~/.claude/projects/ (or $CLAUDE_CONFIG_DIR/projects) and posts one session event per working block, so Claude Code work shows up on the Timeline with no harness configuration:

aw-watcher-agent tail-claude-code            # every transcript
aw-watcher-agent tail-claude-code --gap 600  # split blocks on 10 min idle

Only timestamp, sessionId and cwd are read; prompts, replies and tool payloads are never touched. A session is split into blocks on idle gaps longer than --gap (default 15 min), so a session left open overnight is not counted as agent time. The trailing block is held back until it has been idle for --gap, and a per-transcript cursor records each successful post. Overlapping invocations skip transcripts already being processed. Cursor writes are atomic; retrying a failed later post does not replay the checkpointed prefix. Once blocks have been emitted, changing --gap for that transcript is rejected rather than posting overlapping representations. A digest of the checkpointed event metadata also rejects historical inserts, rewrites or truncation that change already emitted blocks; it does not silently skip or replay them. Only a missing cursor starts a new import: unreadable, corrupt or older count-only cursors fail closed. Preserve such a cursor and reconcile the destination events before resetting it, since removing it reimports the transcript. A failed transcript is reported on stderr without preventing other transcripts from being processed; --strict returns nonzero after the scan if any failed. This is not transactional exactly-once delivery: a crash after the server accepts a post but before its checkpoint can still duplicate that last event on retry. Events land in the same session bucket as the hooks below, with harness=claude-code and workspace set to the project directory. Use either the tailer or lifecycle hooks for a session, not both.

A resumed Claude Code session writes a new transcript that begins with copies of the earlier session's rows. In the scan path, rows whose sessionId differs from the transcript's file name are skipped, so each block is posted once, from the transcript that owns it. --file is exempt: a file you name yourself is processed as-is, because its name is not a session id and filtering by it would drop every row.

A cursor written by an older version for such a transcript recorded the copied rows; it now fails the checkpointed-prefix check. Delete that one cursor to let the transcript be re-imported. The re-import posts every row the transcript owns (the copied rows stay filtered), so reconcile the events it already posted — both the copied ones and its own — before deleting it, e.g. by timestamp, duration and session_id.

Wiring into Claude Code (or any harness with lifecycle hooks)

Point your harness's session-start / session-end hooks (for Claude Code: SessionStart / SessionEnd in settings.json) at emit-start / emit-end, passing the same --session-id to both. Do not use Claude Code's Stop hook for emit-end: it fires after every response, so the first turn closes the session and each later turn posts an empty 0s event. Keep the SessionEnd hook synchronous (not async) so the post finishes before the process exits.

Per-tool activity

Tool calls go to a sibling bucket, aw-watcher-agent-activity_<hostname> (type app.agent.activity). Adjacent calls with the same tool and status within --pulsetime seconds (default 5) merge into one Timeline block.

Emit a single tool event yourself, e.g. from a post-tool hook:

aw-watcher-agent emit-activity --harness claude-code --session-id 8531 \
  --tool shell --status success --duration-ms 1200

Per-tool activity from Codex (log-tailer)

Codex can't host an in-process hook, so tail-codex reads its rollout transcripts (~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl), pairs each function_call with its function_call_output, derives a coarse success / error / completed status, and emits one activity event per call:

# Process the most recent rollout transcript
aw-watcher-agent tail-codex

# Or a specific transcript, with a custom merge window
aw-watcher-agent tail-codex --file ~/.codex/sessions/2026/05/29/rollout-...jsonl --pulsetime 5

A cursor per transcript (in the same state directory) records what was already emitted, so it is safe to run tail-codex repeatedly from a timer or after each Codex run.

Both function_call and custom_tool_call records are paired with their corresponding outputs. Each accepted heartbeat checkpoints the call ID locally; call IDs and call kinds are not added to ActivityWatch event data. On upgrade, older function-only cursor indices are mapped to the same function call in the mixed stream. Custom calls at or before that boundary are deliberately not backfilled, so previously emitted function calls are never replayed. A cursor is updated only after a new heartbeat is accepted.

Command reference

Command Purpose
ensure-bucket Create the session bucket if missing
emit-start Record session start (zero-duration placeholder)
emit-end Replace the placeholder with one event carrying duration + --outcome
emit-activity Record one tool-activity heartbeat (--tool required)
tail-codex Emit per-tool activity from a Codex rollout transcript
tail-claude-code Emit session working blocks from Claude Code transcripts (no hooks)

Global flag: --strict (exit non-zero on errors), placed before the command: aw-watcher-agent --strict emit-end .... Run aw-watcher-agent <command> --help for all options.