Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(telemetry): reliable delivery + per-install identity in TS packages#293

Merged
KIvanow merged 3 commits into
masterfrom
fix/ts-telemetry-delivery-identity
Jul 2, 2026
Merged

fix(telemetry): reliable delivery + per-install identity in TS packages#293
KIvanow merged 3 commits into
masterfrom
fix/ts-telemetry-delivery-identity

Conversation

@KIvanow

@KIvanow KIvanow commented Jul 1, 2026

Copy link
Copy Markdown
Member

Summary

Mirror the Python analytics fixes into the four TS packages so npm consumers report install/active signals correctly.

Changes

  • Delivery: flush the *_init event immediately after capture, plus a process.once('beforeExit') backstop (removed on shutdown). Add flush() to the Analytics interface and NOOP. Deliberately no SIGINT/SIGTERM handlers — a library must not seize the host's signal handling; beforeExit matches Python atexit's normal-exit parity.
  • Identity: distinctId is now a per-install UUID persisted at ~/.betterdb/instance_id (honors XDG_STATE_HOME, override BETTERDB_INSTANCE_ID). The old Valkey-scoped id is demoted to a deployment_id property for roll-up, so a fleet sharing one Valkey no longer collapses to a single user.
  • MemoryStore: session counters rolled up into a memory_session event (on close() and a beforeExit backstop) plus a memory_consolidated event.

Client-style split preserved: memory/retrieval use client.call('GET'/'SET'), caches use client.get()/set(). Tests updated to assert install-id distinctId, deployment_id property, and flush-on-init.

Checklist

  • Unit / integration tests added
  • Docs added / updated
  • Roborev review passed — run roborev review --branch or /roborev-review-branch in Claude Code (internal)
  • Competitive analysis done / discussed (internal)
  • Blog post about it discussed (internal)

Note

Medium Risk
Changes product telemetry identity and removes runtime PostHog credential overrides (potential breaking change for custom keys); local filesystem writes for install IDs are new but analytics paths remain fail-soft.

Overview
Aligns agent-cache, agent-memory, retrieval, and semantic-cache npm analytics with the Python telemetry fixes so short-lived processes actually report usage and installs are counted correctly.

Identity: PostHog distinctId is now a per-machine install id (~/.betterdb/instance_id, XDG_STATE_HOME, or BETTERDB_INSTANCE_ID). The former Valkey-scoped UUID becomes a deployment_id event property for rolling up clients that share one store.

Delivery: Adds flush() to the analytics interface (and noop). Init events are flushed immediately after capture, with a beforeExit backstop (removed on shutdown()); no signal handlers.

Config API: analytics.apiKey / analytics.host are removed from package options and createAnalytics; only build-injected PostHog credentials apply (semantic-cache no longer falls back to BETTERDB_POSTHOG_* env at runtime).

Memory-only: MemoryStore aggregates operation counts into a one-shot memory_session event on close() or beforeExit, and emits memory_consolidated on consolidate.

Reviewed by Cursor Bugbot for commit f032954. Bugbot is set up for automated code reviews on this repo. Configure here.

Mirror the Python analytics fixes into the four TS packages so npm consumers
report install/active signals correctly.

- Delivery: flush the *_init event immediately after capture, plus a
  process.once('beforeExit') backstop (removed on shutdown). Add flush() to
  the Analytics interface and NOOP. Deliberately no SIGINT/SIGTERM handlers —
  a library must not seize the host's signal handling; beforeExit matches
  Python atexit's normal-exit parity.
- Identity: distinctId is now a per-install UUID persisted at
  ~/.betterdb/instance_id (honors XDG_STATE_HOME, override BETTERDB_INSTANCE_ID).
  The old Valkey-scoped id is demoted to a deployment_id property for roll-up,
  so a fleet sharing one Valkey no longer collapses to a single user.
- MemoryStore: session counters rolled up into a memory_session event (on
  close() and a beforeExit backstop) plus a memory_consolidated event.

Client-style split preserved: memory/retrieval use client.call('GET'/'SET'),
caches use client.get()/set(). Tests updated to assert install-id distinctId,
deployment_id property, and flush-on-init.
Comment thread packages/agent-memory/src/MemoryStore.ts
Comment thread packages/agent-cache/src/analytics.ts
KIvanow added 2 commits July 2, 2026 09:05
Remove the apiKey/host config options from AnalyticsOptions and the
per-package option types, and drop the unused BETTERDB_POSTHOG_API_KEY/HOST
runtime env override from semantic-cache. Key resolution is now uniform
across all four packages: the build-time baked key only. Tests construct
PostHogAnalytics directly (now exported) instead of injecting a key via the
factory.
- getInstallId: memoize a per-process ephemeral id when persistence fails,
  so repeated/parallel init calls report one stable distinctId instead of a
  fresh UUID each time (all 4 analytics.ts).
- MemoryStore.captureSession: only arm the one-shot flag once counts are
  non-zero, so an early beforeExit with zero activity no longer blocks a
  later close() from emitting memory_session.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit f032954. Configure here.

// Key resolution: opts.apiKey → BETTERDB_POSTHOG_API_KEY env var → baked wheel value
const bakedKey = BAKED_POSTHOG_API_KEY.startsWith('__') ? undefined : BAKED_POSTHOG_API_KEY;
const apiKey = opts?.apiKey ?? process.env.BETTERDB_POSTHOG_API_KEY ?? bakedKey;
const apiKey = BAKED_POSTHOG_API_KEY.startsWith('__') ? undefined : BAKED_POSTHOG_API_KEY;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Semantic cache env key gone

Medium Severity

createAnalytics in semantic-cache no longer reads BETTERDB_POSTHOG_API_KEY or BETTERDB_POSTHOG_HOST, or option overrides. Deployments that enabled telemetry via those env vars without a build-injected baked key now always get NOOP_ANALYTICS.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit f032954. Configure here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Intentional

@KIvanow KIvanow requested a review from jamby77 July 2, 2026 07:01
@KIvanow KIvanow merged commit 09a0b3c into master Jul 2, 2026
3 checks passed
@KIvanow KIvanow deleted the fix/ts-telemetry-delivery-identity branch July 2, 2026 13:37
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant