Skip to content

feat: wake 1.0 — Rust hook collector sending skill telemetry over OTLP - #102

Open
moraisjose wants to merge 47 commits into
mainfrom
rust-rewrite
Open

moraisjose wants to merge 47 commits into
mainfrom
rust-rewrite

Conversation

@moraisjose

Copy link
Copy Markdown
Collaborator

Replaces #101 (same branch, renamed from v2 to rust-rewrite).

Summary

Wake 1.0 replaces the Go 0.x (local per-developer reports, consent machinery, full transcript rescans) with a small Rust binary that Claude Code hooks call asynchronously. Each SessionStart / Stop / SessionEnd reads only the new slice of the transcript. It derives one event per skill, subagent, MCP tool (aggregated) or slash command, and sends them as OTLP/HTTP JSON spans to a configured endpoint, Langfuse first.

  • Design: docs/superpowers/specs/2026-11-26-wake-v2-design.md
  • Plan: docs/superpowers/plans/2026-11-26-wake-v2-claude-code.md

What's in it

  • wake emit: incremental transcript reader that keeps allowlisted fields only. Skill windows come from the harness's own attributionSkill. Subagents are handled both foreground and background (background ones are closed by their <task-notification>). Subagent transcripts are followed up to depth 3, and tokens are never counted twice.
  • Delivery: one closed allowlist of attributes (spec §5.6) with gen_ai.* token and model fields so Langfuse prices them, and deterministic span ids. The order is enqueue, then save state, then one flush per run, with a bounded outbox (1,000 entries / 7 days; batches of 50; 5 s deadline).
  • State: per-session state with a non-blocking lock, forward-only offsets, and 14-day GC. Abandoned sessions get flushed only when both the state and the transcript have been idle for 30 min.
  • wake check: prints the resolved config without secrets and sends a test event.
  • wake install / wake uninstall [--purge]: merges three async hook groups into ~/.claude/settings.json and removes them again. It never touches APM-installed hooks, refuses files it can't parse safely, and the install + uninstall round trip is byte-identical.
  • Distribution: install.sh (checksum-verified), a GitHub Releases workflow (4 targets, ≤ 2 MB each, tag must match the crate version), and an APM package template under package/.
  • Docs: README, SECURITY.md and CONTRIBUTING rewritten for 1.0, plus docs/langfuse-setup.md.

v1 is tagged locally as v1-final. That tag is not pushed yet.

Test plan

  • cargo fmt --check, cargo clippy --all-targets -- -D warnings: clean
  • cargo test: 179 passed, 0 failed (perf test ignored in the default run, run separately in CI)
  • Perf (release): one turn of a 22 MB transcript in 18.8 ms CPU and 4.8 MB RSS (budget 50 ms / 10 MB); binary 1.65 MB
  • Real headless Claude Code sessions against a local OTLP receiver:
    • a slash-invoked skill, a model-invoked skill and a background subagent each produced the expected span, with tokens and model;
    • no prompt text, tool output or local paths appeared in the bodies.
  • wake install → check → uninstall --purge on a copy of a real settings.json: byte-identical
  • Langfuse test project: pricing of gen_ai.*, and upsert of a resent span id
  • First run of release.yml (e.g. a v1.0.0-rc.1 tag, after bumping Cargo.toml)
  • Live MCP tool aggregate (covered by fixtures only)

Known follow-ups

  • A background subagent running more than 30 min while its own session is idle can be closed early by another session's stale flush.
  • A permanent 4xx on a batch drops the whole batch (up to 50 events).
  • A <synthetic> last model yields no model on a subagent span.

🤖 Generated with Claude Code

moraisjose and others added 30 commits November 26, 2026 11:34
…telemetry over OTLP

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Handle background subagents (async_launched, closed by task-notification),
allow '_' in the token allowlist, pass session_id to derive, tighten
slash-command detection, flush open invocations of sessions that never
got SessionEnd, keep APM-installed hooks out of wake install/uninstall,
and add Langfuse JSON and upsert checks. Spec drops StopFailure, sends
MCP aggregates per turn, and simplifies the inventory entry format.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fig files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds langfuse.observation.type=generation on spans carrying a model or
tokens, so Langfuse prices them (plan Task 5 table and spec 5.6 updated).
Docs: https://langfuse.com/integrations/native/opentelemetry
      https://langfuse.com/docs/opentelemetry/get-started

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fields only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…use in either order

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tions

Foreground Agent results close a Subagent invocation at once; background
ones wait in state.pending_agents for their task-notification or session
end. MCP tool_uses aggregate per (server, tool, enclosing skill) per derive
call. Non-skill slash commands become Command events. Adds the
subagent_transcript path helper.

Also folds in ruling R15: a command moves the interrupt turn start and ends
other invocations' slash-merge window, an invocation no longer counts its
own start Skill tool_use, and close() builds by OpenInvocation.kind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…agent transcripts

Subagent span keys include the launching Agent tool_use id, so a continued
subagent (same agentId) gets a distinct span. Background agents without a
tool_use id are not kept, and empty notification ids match nothing.
SessionState.root_parent_key (R17) parents skills, subagents, MCP
aggregates and commands that have no enclosing skill.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…iles

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…v2 docs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e pins

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…age entries

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…uses it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…skills, and carry the model

R29: a subagent span's tokens are its transcript total minus the tokens of the
skill invocations derived from that same transcript (both requestId-deduped, so
disjoint), and its gen_ai.request.model is the model of the transcript's last
assistant entry with usage, so Langfuse can price it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… are both idle

R27 (amends R18): SessionState saves the transcript path on every hook run. The
stale flush needs the state file and the transcript (when known and present)
both untouched for 30 minutes, so a long single turn or a long background
subagent in another live session is no longer closed early. Subagents closed by
the stale flush are followed into their own transcripts with the R16/R29 token
and model logic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
R28: queued payloads are merged 50 at a time into one ExportTraceServiceRequest
(their resourceSpans concatenated), no POST starts once the flush has run 5 s,
and the outbox is rewritten after each batch that succeeds or is rejected for
good, so a kill mid-flush keeps the progress made. The first transient failure
still ends the flush. A queued body that is not OTLP is sent on its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
R30: the outbox lock and the outbox writer created the state dir with the
default umask, and the outbox and its tmp file were 0644 although they hold
encoded spans (user email, repo). Every state directory is now created 0700 and
the outbox is written through a fresh 0600 tmp file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
moraisjose and others added 11 commits November 26, 2026 13:18
SECURITY.md still described v1 (consent tables, wake remote, doctor, the
loopback dashboard, no outbound spool, HMAC repo identity). It now covers what
v2 sends (the spec 5.6 allowlist, wake.repo in clear, email or salted
pseudonym), what stays local (0600 config with credentials, 0700 state dir,
0600 outbox of queued spans), install never touching hooks it does not own,
wake check never printing secrets, and the Langfuse key caveat (spec 5.8).
The reporting channel is unchanged.

wake --version / -V prints "wake <version>", as the report template asks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…shing

The launcher exits 0 without running anything when the binary it picks is not
executable, so a package committed without the exec bit installs cleanly and
silently sends nothing. The template README now has the chmod +x and
git update-index --chmod=+x step, and how to check for mode 100755.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
R30: a new version job compares the pushed tag (without its leading v) with the
crate version from cargo metadata, and every build job needs it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… comment fixes

R30: the README now lists the per-repo .claude/settings.local.json env level
for WAKE_DISABLED (spec 5.2), documents $XDG_CONFIG_HOME/wake/config.json, and
explains identity_salt (anonymous mode sends no user without it).
CONTRIBUTING places the hook enum in harness/claude_code/mod.rs, and the
transcript reader's header says prompt text is read transiently and never
retained or sent, rather than never held.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
R30: trace_id("s1") and span_id("k1") are pinned to hex computed
independently with shasum, so a change to the id derivation (which would re-key
every span already stored in a backend) fails a test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…otes

R30: the hook command is "<exe>" emit <event>, run by a shell. A path
containing ", $, a backtick or a backslash would break out of or be expanded
inside those quotes, so install now refuses it with InstallError::UnsafeExePath
and a clear message, before writing anything.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…from public_key/secret_key

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude Code's transcript input_tokens excludes cached tokens, while the OTel GenAI
semconv (and Langfuse) treat gen_ai.usage.input_tokens as the total and subtract the
cache fields from it. Verified against Langfuse Cloud: input 10 + cache 52,000 was
recorded as input 0; sending the total (52,010) records input 10 and prices it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ration names

The outcome becomes langfuse.observation.level (ERROR for error, WARNING for
interrupted) plus OTel span status error, the primitive's version becomes
langfuse.version, and the repository becomes langfuse.trace.name, so dashboards
can group by them. Subagents carry gen_ai.operation.name=invoke_agent and
gen_ai.agent.name; MCP tools carry execute_tool and gen_ai.tool.name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
moraisjose and others added 5 commits November 26, 2026 15:32
…ures wake

On Linux, Command::spawn uses vfork and exec folds the parent's peak RSS into
the child's ru_maxrss. Building the 22 MB transcript in memory made the
one-turn budget measure the test process (~24.8 MB) instead of wake (~4.7 MB).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tests/check.rs names the fake key sk-lf-SECRET and its Basic auth base64 to
assert that `wake check` never prints them. Scoped to lines with that exact
fake key, never by path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Linux gnu build is 2,122,480 bytes, 25 KB over 2 MB. The step had not run
on this branch until the perf test was fixed. The profile is already minimal
(opt-level z, lto, one codegen unit, strip); panic=abort is ruled out because
emit relies on unwinding, and serde_json's preserve_order is needed by install.
Most of the size is rustls and ring. 2.5 MB still catches a heavy new dependency.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
stop_then_end_sends_expected_spans failed on the macOS runner with only the
inventory span sent. A hook holds its session flock while it runs and spawns
git; when another test thread forks at that moment, the child holds a copy of
the lock descriptor until it execs, so the first test's next hook found its own
session locked and silently skipped. Reproduced locally under load (6/200 runs);
with the hooks serialised, 0/120. Real hooks are separate processes, so only
the tests are affected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the 2.5 MB limit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@jonioliveira jonioliveira left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The Rust collector meets the #101 requirements I checked: transcript slicing, the attribute allowlist, the bounded outbox, state and locking, and the install/uninstall round trip. The release workflow and the credential handling need changes before the first tag.

Fix before tagging

  • The release no longer runs CI (release.yml).
  • A pre-release tag would be published as latest, and install.sh serves latest to everyone (release.yml).

Questions

  • Should an env-only endpoint override receive the stored keys (config.rs)?
  • Identity defaults to email (config.rs:295), so every span sends the git email to the collector unless the user opts out. Is that the intended default for a third-party collector?

Not reviewed

  • derive.rs past line 160, transcript.rs (except read_from), inventory.rs, identity.rs, repo.rs, ids.rs and event.rs.
  • The tests and fixtures, and the docs/superpowers spec and plan.
  • The "≤ 2 MB per binary" requirement: there is no evidence of it in the workflow.

Comment thread src/config.rs
let mut sources = BTreeMap::new();

let mut endpoint_cands: Vec<(Source, String)> = Vec::new();
if let Some(v) = envv("WAKE_OTLP_ENDPOINT") {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is it intended that an env-only WAKE_OTLP_ENDPOINT still gets the Basic auth built from the keys in config.json/telemetry.json? Hooks inherit the env block of a repo's .claude/settings.json, so a cloned repo could point the endpoint at its own host and receive the user's Langfuse keys.

Suggestion: attach file-sourced keys and headers only when the endpoint comes from the same layer, or when its origin matches. Otherwise send no auth.

needs: gate
# A tag that does not match the crate version would publish binaries whose
# `wake --version` disagrees with the release name.
version:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The release no longer calls the CI workflow, so a tag on a commit that never passed fmt, clippy, test or perf still builds and publishes binaries.

Suggestion: restore workflow_call: in ci.yml, and add a gate: uses: ./.github/workflows/ci.yml job that build needs.

sha256sum wake-darwin-arm64 wake-darwin-x64 wake-linux-arm64 wake-linux-x64 > checksums.txt
cat checksums.txt

- uses: softprops/action-gh-release@8a0cd651e5b2365bd640854a3fa5dfc70e4875f3 # v3.0.4

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

softprops/action-gh-release publishes every tag as a full release. A tag like v1.0.0-rc.1 would become latest, and install.sh downloads releases/latest/download, so every new install would get the rc.

Suggestion:

          prerelease: ${{ contains(github.ref_name, '-') }}

Comment thread src/main.rs
("--purge", true) => f.purge = true,
("--endpoint", false) => f.install.endpoint = Some(value()?),
("--public-key", false) => f.install.public_key = Some(value()?),
("--secret-key", false) => f.install.secret_key = Some(value()?),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

--secret-key K puts the Langfuse secret in shell history and in ps output, and docs/langfuse-setup.md and the install.sh header both recommend this form.

Suggestion: point the docs at the hidden prompt or WAKE_SECRET_KEY, and consider adding a --secret-key-stdin option.

Comment thread src/install.rs
let _ = std::process::Command::new("stty").arg(arg).status();
};
if secret {
stty("-echo");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ctrl-C at the secret-key prompt kills the process after stty -echo, so the user's terminal is left with echo off.

Suggestion: restore echo in a drop guard or a SIGINT handler.

Comment thread src/check.rs Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants