Skip to content

telemetry: anonymous usage counts, off with CODEAF_TELEMETRY=off - #1095

Merged
AbirAbbas merged 36 commits into
devfrom
feat/telemetry
Sep 17, 2026
Merged

AbirAbbas merged 36 commits into
devfrom
feat/telemetry

Conversation

@santoshkumarradha

@santoshkumarradha santoshkumarradha commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Anonymous usage counts for codeaf: on by default, a notice printed once, off with CODEAF_TELEMETRY=off. The contract is docs/TELEMETRY.md; this PR is the whole pipe from the binary to the relay's door.

What is sent

Four events, all coarse: first_run (once per install), session_started, session_ended (banded counts of turns, model calls, tool calls, failures, cost, duration, stop reason, exit code) and fault (a 16-hex fingerprint of codeaf function names at the crash site, never the message). Common properties are version, channel, OS, arch, usage context (local, CI, container) and install method. The distinct id is a hash of a random install id; no person profile is created and geolocation is off.

Never sent, and tests prove it: prompts, code, file names, paths, repo names, keys, email, IP, machine name, model slugs, raw error text. The relay strips unknown keys as a second wall.

How it works

  • internal/telemetry: the events, an allowlist, a local spool (~/.codeaf/telemetry/spool.jsonl, 50 per POST, 7-day age, 1000-line cap), one deadline-bounded flush at exit, and the opt-out ladder. Session counters are three atomic adds on the hot paths and count whether or not telemetry is on; only sending asks the ladder.
  • cmd/codeaf: lifecycle in execute for chat, resume, do, exec, run and plan run; codeaf telemetry status|show|on|off; fault events from reportFault (main) and from guard.Recover through a hook (goroutine).
  • internal/config: the telemetry setting row. A project file can only turn it off, never on.
  • scripts/install.sh: records how the copy was installed and prints the notice; sends nothing.
  • scripts/release_downloads.py and a daily workflow: public GitHub release download counters as one gauge event per asset per day.

Off switches, in the order checked

CODEAF_TELEMETRY=off|0|false, DO_NOT_TRACK=1, telemetry = off in the project file, codeaf telemetry off, an empty CODEAF_TELEMETRY_ENDPOINT. Unstamped or dirty builds and test binaries never send. Nothing is sent before the notice has been shown.

Verified

  • Full make check changelog-check test-laws on Spark on this exact head (job id and result in the last comment). One failure is pre-existing on dev and untouched here: internal/release TestPublishedPinExamplesGiveVersionToBash, because README.md on dev no longer carries a | VERSION= pin example. It fails the same way on a clean dev checkout.
  • A stamped build of this head ran two real task sessions against the production relay: a clean one arrived as done with exit 0 and the model-call band filled; an interrupted one arrived as interrupted with exit 3. The relay's own checks (schema, size, enum, unknown-key stripping) were exercised end to end.
  • internal/telemetry and internal/guard pass under the race detector. Binary size stays under budget.

Known limits

  • turns is counted where the chat loop seals a turn, so task sessions report the 0 band for it.
  • The notice is not printed under --json or when stderr is not a terminal in chat mode, and until it has been printed nothing is sent.

🤖 Generated with Claude Code

santoshkumarradha and others added 30 commits September 17, 2026 01:27
…contract

Four typed events (first_run, session_started, session_ended, fault) with the
contract's allowlisted, bucketed props; the opt-out ladder with its off
reasons; a hashed install identity at 0700/0600; a fault fingerprint over
codeaf function names only; a JSON-lines spool with a deadline-bounded flush
of up to 50 events per POST, 7-day age drops and a 1000-line cap; the notice
constant with NoticeShown/MarkNoticeShown gating the flush; and Show() for a
future codeaf telemetry show.

A library only: nothing in cmd/codeaf, internal/session or internal/config
reads it yet.

Co-Authored-By: codeaf <[email protected]>
Adds the package's tests — bucketing edges, the opt-out ladder, install
identity, the notice, fingerprinting, the spool against an httptest relay
(cap, age drop, 50-per-POST splitting, a hard deadline against a hanging
server, silence on failure) and the privacy law that no sentinel string
reaches the wire. docs/TELEMETRY.md documents the notice, what is sent and
how to turn it off, with a test that fails if the doc's property table
drifts from the Go allowlist.

Fixes four drifts from the wire contract found running those tests: the
<1m duration bucket was missing, the fingerprint's argument stripping cut
too much, the rc channel suffix must be a single integer, and under go
test a config-off rung outranks the test rung in the reason reported.

Co-Authored-By: codeaf <[email protected]>
Fingerprint stability (last-( cut, .go:/main./created-by frame rules),
counted-batch flush, guarded and multi-process-safe spool, BucketDuration
edges, Configure-gated write paths, O_EXCL install id, install.json
preservation, buildinfo.Dirty accessor.

Co-Authored-By: codeaf <[email protected]>
Following the telemetry wire contract: a successful install now writes
telemetry/install.json under the state root (dir 0700, file 0600) with
the install method, channel and UTC timestamp, leaves any existing
install_id alone, and makes no request the download steps did not
already make. At the very end the installer prints the contract's
notice to stderr after one blank line, or one line saying telemetry is
off when CODEAF_TELEMETRY/DO_NOT_TRACK opts out. README gains a plain
Telemetry section pointing at docs/TELEMETRY.md, and a new
test/installer-telemetry.sh proves the marker, the notice and the
opt-out without a network.

Co-Authored-By: codeaf <[email protected]>
The review of 71ff95a6b found four things. Revert the one line that
changed `if [[ -z "${TAG:-}" ]]; then` back to `${TAG:-}`: under
set -u an unset TAG must reach api_problem, not crash. In
write_install_marker the channel lands in JSON verbatim and CHANNEL
can come from the environment, so only stable, rc, staging or dev is
recorded and anything else becomes "unknown" — with test cases for a
valid channel and for we"ird, both asserting the marker parses
(python3 -m json.tool, or jq, else a strict channel check). Fence the
README Telemetry notice in a ```text block so Markdown keeps its
alignment, wording byte for byte. Clear the two shellcheck findings in
test/installer-telemetry.sh with one block disable for SC2016/SC2034 —
ok() evals those strings, so the quoting is deliberate — and export
CODEAF_TELEMETRY/DO_NOT_TRACK where a subshell needs them, without
weakening any assertion.

Co-Authored-By: codeaf <[email protected]>
…load counts

A scheduled workflow reads the repository's releases from the public GitHub
API and sends one codeaf:release_downloads event per binary asset
(codeaf-<os>-<arch>[.exe]) — sidecars like checksums.txt are not reported.
Each event carries release_tag, channel (stable or the prerelease suffix),
prerelease, os, arch, the cumulative download_count and the UTC
snapshot_date; the event id is derived from tag, asset and date, so a
same-day rerun deduplicates instead of counting twice. No code in the
binary; counts come only from public release data.

Co-Authored-By: codeaf <[email protected]>
The dry-run unittests now also assert the identifiers the wire spec spells
out where the script and the workflow spell them: the schedule, dispatch,
contents-read permissions, built-in token, project-key secret and host
variable in .github/workflows/release-downloads.yml, the public-data
paragraph in docs/rules/promotion.md, stdlib-only imports, pagination
across API pages, the host override and the /batch/ endpoint, sidecar
skipping, draft skipping, and the channel words stable, rc, dev and
staging.

Co-Authored-By: codeaf <[email protected]>
Channel detection now follows the tag grammar in
cmd/codeaf-release/version.go: stable, rc, dev and staging tags match
their four patterns and anything else reports "unknown", so
dev-20260917-09299019bcdc is no longer mislabelled "stable".

--dry-run works without CODEAF_POSTHOG_PROJECT_KEY, the local and test
path: the batch prints with api_key "<unset>" and exits 0, and only the
sending path stops with the notice.

Every event carries "$geoip_disable": true next to
"$process_person_profile": false, since the sender is a CI runner whose
location is meaningless. The module docstring now names
release-downloads.yml, the file that exists.

The fixture holds one tag of each shape plus an unrecognised one, and
the tests assert the channel for each, including the two real dev and
staging tags.

Co-Authored-By: codeaf <[email protected]>
ScriptSourceTest read sys.stdlib_module_names unguarded; the attribute
arrived in Python 3.10, so the suite errored on macOS stock python3
(3.9.6). Guarded with getattr so the pinned import-set check stands on
its own where the attribute is absent; 32 tests pass on 3.9, 3.10 and
3.14.

Co-Authored-By: codeaf <[email protected]>
The test read .telemetry-contract.md, a working note that is not in the
repository and never will be, so it aborted with "telemetry contract is
missing" in a clean checkout. It now extracts the fenced block under
"## The notice" in docs/TELEMETRY.md — the same bytes as the Notice
constant in internal/telemetry/notice.go — and holds the installer's
TELEMETRY_NOTICE to it as before; a new assertion holds the README's
telemetry fence to it too, byte for byte, so the invariant the suite
existed to guard now has its third leg.

Comments in scripts/install.sh, internal/telemetry/telemetry.go and this
test now name docs/TELEMETRY.md as the source of the notice.

No Go code changed and no Go tests were run. shellcheck is clean; the
suite's 33 passing assertions include the two new notice checks, and the
same two stat(1)-related assertions that already failed on this GNU
machine before the change still fail with identical output.

Co-Authored-By: codeaf <[email protected]>
…TRY.md

docs/rules/promotion.md was the only home the paragraph had, and it came
with a fallback pinning the branch to the state before the telemetry
doc existed. The paragraph moves, byte for byte, into docs/TELEMETRY.md
as a final "## Download counts" section beside the rest of the telemetry
story, and promotion.md returns to its origin/dev content, so this
branch now touches no release docs.

test_no_telemetry_doc_on_this_branch asserted docs/TELEMETRY.md did not
exist and is deleted. The two tests that read the paragraph read it
where it now lives: both require a "## Download counts" section in
docs/TELEMETRY.md naming .github/workflows/release-downloads.yml,
scripts/release_downloads.py and the checksums.txt sidecar, and the
promotion.md fallback is gone.

No Go code changed and no Go tests were run; the property table in
docs/TELEMETRY.md is untouched. The suite passes: 31 tests, OK.

Co-Authored-By: codeaf <[email protected]>
On Linux `stat -f` succeeds and reports the filesystem, so the BSD-first
order never reached the GNU form and both permission checks failed.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CqfhxLrrBNQxwPNvEgoBGR
Each asserts through real calls, not a re-implemented effect: the
newRelay override lands after testHome and a flush reaches that
recorder; a default-endpoint flush is refused like a dead relay
(zero round trips, event still spooled); forcing the ladder on
cannot reopen the path.

Co-Authored-By: codeaf <[email protected]>
Snapshot returns the SessionStats fields the SessionEnded constructor
takes, so the wiring hands the struct straight over. Cost is stored as
integer micro-dollars and divided back once, in Snapshot. Count* never
ask whether telemetry is enabled and never allocate.

Co-Authored-By: codeaf <[email protected]>
One alignment fix in the many-goroutine test; no behaviour change.

Co-Authored-By: codeaf <[email protected]>
Key "telemetry", on|off, default on, env pin CODEAF_TELEMETRY, in the
interface group beside the history row. TelemetryAtIn walks the ladder —
pin, project file, profile config, default — and TelemetryOffReason
answers whether config turned the pipe off and names which rung did.
The row sits in ProjectKeys so a repository may answer for itself.

Co-Authored-By: codeaf <[email protected]>
Configure, notice, first_run and session_started at the start of every session
command, session_ended and a one-second flush at its exit. The end event does
not yet carry the session counters.
telemetryEnd builds the end event from telemetry.Snapshot so turns, model
calls, tool calls and cost reach the spool; the stop-reason switch becomes
a map over the exit ladder; and both lifecycle constructors are gated on
the ladder being on so a disabled run touches no state.

Co-Authored-By: codeaf <[email protected]>
The exact turn, call and cost assertions hold for one run of the package; a
repeated run counts into the same atomic tally again.

Co-Authored-By: codeaf <[email protected]>
santoshkumarradha and others added 6 commits September 17, 2026 03:32
A goroutine recovering a panic read the hook while a test on another
goroutine wrote it, which the race detector reported. SetOnFault stores it
once; Recover loads it.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ncelled context

stopSignals cancels the signal context at the end of every errand, so the
flag read from its Err said interrupted for a run that finished cleanly.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ites no marker

TestEverySettingRowHasATab wants every registry row on the settings panel.
TestCustomInstallOutsideTheStateRootDoesNotRunAdoption wants an install
outside the state root to leave the login's state folders alone, so the
install marker is written only when the root exists or the install is in it.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Other tests in the package drive model and tool calls through the doors
that count, so the exact bands held only when the test ran alone.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
execute starts writers under the state root that outlive the test, and the
temp dir's cleanup raced them. The lifecycle alone is what the test pins.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@santoshkumarradha

Copy link
Copy Markdown
Member Author

Verification evidence for head c828d0c5f:

  • Full suite on Spark (aarch64, go1.26.5), fleet job 20260917-101803-000555: make vet fmt-check test on the exact head in its own clone. 96 packages ok. The one failure is internal/release TestPublishedPinExamplesGiveVersionToBash, which fails identically on a clean checkout of dev at 09299019b (README.md has no | VERSION= pin example since the README rewrite); it is not touched by this PR.
  • Remaining make check targets, fleet job 20260917-102358-000556: test-packed-manual ok, size ok (bin/codeaf 53,018,889 bytes under the 54,600,000 budget), changelog-check ok, test-laws ok.
  • internal/telemetry and internal/guard pass under -race; cmd/codeaf passes with -count=2.
  • Live: a stamped build of this branch ran two real task sessions against the production relay. The clean one arrived in PostHog as session_ended with stop_reason=done, exit_code=0, model_calls=2-5; the interrupted one as stop_reason=interrupted, exit_code=3. first_run and session_started arrived once each per install. The events were personless, carried only allowlisted keys, and the spool was empty after exit.

@santoshkumarradha
santoshkumarradha marked this pull request as ready for review September 17, 2026 10:24
@santoshkumarradha

Copy link
Copy Markdown
Member Author

Hi @AbirAbbas, please review as high priority.