Files
PaperClipAI/server
Dotta e32b1b057f fix(telemetry): re-identify PostHog workspace group on Cloud stack claim (#23389)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Managed Paperclip Cloud stacks send direct server telemetry to
PostHog and identify each `workspace` group with `stack_slug` and
`primary_host`
> - Warm-pool stacks boot before a user claims them, so startup
`groupIdentify` records the placeholder `pool-<hash>` slug and host
> - When a user claims a warm-pool stack,
`applyCloudRuntimeIdentityAssertion` transitions the stack identity in
place without restarting the process, so PostHog group properties stay
stuck on `pool-<hash>` until a later restart
> - This pull request adds an `onCloudRuntimeIdentityChange` listener in
`cloud-runtime-identity` and subscribes the PostHog direct transport to
re-send `groupIdentify` with the claimed `stack_slug` and `primary_host`
> - The benefit is that PostHog workspace analytics and live dashboards
immediately reflect the real claimed stack name and host for both
browser and server-side events

## Linked Issues or Issue Description

**What happened?**

When a warm-pool Cloud stack boots, `resolveDirectTransport` announces
the workspace to PostHog with its placeholder `pool-<hash>` `stack_slug`
and `primary_host`. After a user claims the stack via
`applyCloudRuntimeIdentityAssertion`, the server updates its runtime
identity in place without restarting, so PostHog's `workspace` group
keeps the stale `pool-<hash>` `stack_slug` and `primary_host` until the
container eventually restarts.

**Expected behavior**

When a Cloud stack's runtime identity transitions from a warm-pool
placeholder to its claimed slug and canonical origin, the PostHog direct
transport immediately calls `groupIdentify` with the updated
`stack_slug` and `primary_host` and flushes.

**Steps to reproduce**

1. Start a Paperclip Cloud warm-pool instance with
`PAPERCLIP_POSTHOG_PROJECT_TOKEN` and `PAPERCLIP_POSTHOG_ENVIRONMENT`
configured and `PAPERCLIP_STACK_SLUG=pool-123`.
2. Apply a signed Cloud runtime identity assertion claiming the stack as
`gonzo` (`https://gonzo.staging.paperclip.app`).
3. Observe PostHog `workspace` group properties for the stack: before
this fix, `stack_slug` and `primary_host` remain `pool-123` /
`pool-123.staging.paperclip.app` until process restart.

**Paperclip version or commit**

`36c2ad39c8` (`origin/master`)

**Deployment mode**

Other (Paperclip Cloud managed stack)

## What Changed

- Added `onCloudRuntimeIdentityChange` listener registration and
best-effort notification in
`server/src/services/cloud-runtime-identity.ts` when
`applyCloudRuntimeIdentityAssertion` changes the runtime identity (or
`initializeCloudRuntimeIdentity` restores one), and cleared listeners in
`resetCloudRuntimeIdentityForTests`.
- Subscribed `createPostHogDirectTransport` in
`server/src/services/posthog-direct/transport.ts` to
`onCloudRuntimeIdentityChange` so matching `stackId` claims update any
queued `pendingAnnouncement` or immediately send and flush an updated
`groupIdentify` with the claimed `stack_slug` and `primary_host`;
unsubscribed on `shutdown()`.
- Added a regression test in
`server/src/__tests__/cloud-runtime-identity.test.ts` verifying initial
`pool-*` announcement, immediate `groupIdentify` + flush on claim, and
no duplicate calls on idempotent claim retry.

## Verification

- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/cloud-runtime-identity.test.ts
src/__tests__/posthog-direct-transport.test.ts
src/__tests__/posthog-direct-telemetry.test.ts` — 38/38 tests passed.
- `pnpm --filter @paperclipai/server typecheck` — passed.

## Risks

- Low risk. Listener notifications are wrapped in `try/catch` and the
PostHog `groupIdentify` + `flush` call is fire-and-forget with error
suppression, matching the existing `onCloudTrialInferenceCohortChange`
pattern so telemetry failures can never disrupt runtime identity claims.

## Model Used

- Google Gemini (Antigravity agent with tool use, file editing, and
local test execution)

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
2026-12-01 16:37:08 +00:00
..