Skip to content

fix(threads): skip chat_started analytics on an id-collision replay - #5143

Merged
pedrofrxncx merged 1 commit into
mainfrom
fix/thread-create-idempotent-analytics-w4
Jul 23, 2026
Merged

pedrofrxncx merged 1 commit into
mainfrom
fix/thread-create-idempotent-analytics-w4

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator

Source: bug found while auditing apps/mesh/src/tools/thread/create.ts (recent-changes hunt) — no existing issue.

Payoff: COLLECTION_THREADS_CREATE's own docstring says it's "Idempotent on id collisions (storage uses INSERT … ON CONFLICT DO NOTHING)", but the chat_started posthog event fired unconditionally, even on the conflict path where no row was actually inserted. This is a live path, not theoretical: shell-layout.tsx's usePanelActions comments explain the eager client-side create races the route loader's create-on-404 fallback, both sharing the same client-generated thread id — so a genuine double-call happens in normal use, and every replay inflated the chat_started count for a thread that was created exactly once.

Failure scenario: two create calls for the same id (retry, or the eager-create/route-loader race described above) → storage's ON CONFLICT DO NOTHING correctly returns the existing row both times, but the tool fired chat_started on both calls, double-counting the metric.

Fix: surface isNew from SqlThreadStorage.create() — it already knows internally whether the RETURNING insert produced a row (fresh insert) or fell through to the conflict-select (pre-existing row); this was just never exposed. The tool now only calls posthog.capture when result.isNew is true. Added does not re-fire the chat_started analytics event on an id-collision replay to create.integration.test.ts, spying on posthog.capture across two same-id create calls and asserting it fires exactly once.

To confirm: bun test apps/mesh/src/tools/thread/create.integration.test.ts (requires Postgres — not run locally per repo dev-environment constraints; CI has Postgres and will validate it, alongside the existing is idempotent: creating with the same id twice returns the same row test in the same file, which still passes unchanged).

Locally verified: bun run fmt (clean) and cd apps/mesh && bunx tsc --noEmit (clean — the two callers of storage.threads.create, create.ts and enqueue-super-agent.ts, both type-check against the new Thread & { isNew: boolean } return type without changes on the latter, since it never inspected the extra field).


Summary by cubic

Prevents duplicate chat_started analytics when thread creation replays with the same id. Surfaces isNew from storage and only emits analytics on new inserts to keep metrics accurate.

  • Bug Fixes
    • SqlThreadStorage.create() now returns Thread & { isNew: boolean } to signal insert vs conflict.
    • COLLECTION_THREADS_CREATE calls posthog.capture only when result.isNew is true.
    • Added an integration test to ensure a same-id double call fires the event once.

Written for commit 4bc3f0e. Summary will update on new commits.

Review in cubic

COLLECTION_THREADS_CREATE is documented as idempotent on id collisions
(storage INSERT ... ON CONFLICT DO NOTHING), but it fired the
"chat_started" posthog event unconditionally, even on the conflict path
where no row was actually created. A replayed/duplicate create call (the
frontend's eager thread create racing the route loader's create-on-404
fallback, both sharing the same client-generated id) double-counts the
event for a thread that was created exactly once.

Surfaces `isNew` from the storage layer (already known internally: the
INSERT ... ON CONFLICT DO NOTHING RETURNING branch returns no row iff a
conflict occurred) and only fires the event when the row was newly
inserted.
@pedrofrxncx
pedrofrxncx merged commit 4f5fc4d into main Jul 23, 2026
15 checks passed
@pedrofrxncx
pedrofrxncx deleted the fix/thread-create-idempotent-analytics-w4 branch July 23, 2026 22:01
decocms Bot pushed a commit that referenced this pull request Jul 23, 2026
PR: #5143 fix(threads): skip chat_started analytics on an id-collision replay
Bump type: patch

- decocms (apps/mesh/package.json): 4.115.0 -> 4.115.1

Deploy-Scope: server
pedrofrxncx pushed a commit that referenced this pull request Jul 27, 2026
#5240)

Reverts #5112 (share agent sandboxes by branch), #5116 (always share
agent sandboxes) and #5132 (use shared staging branch), restoring the
pre-#5112 behaviour: hosted agent sandboxes are per-user again, and a
new GitHub-backed thread gets a generated branch instead of `staging`.

Reconstructed against current paths rather than reverted: #5120 split
apps/mesh into apps/api + apps/web and hoisted branch-name.ts,
runtime-defaults.ts and thread/schema.ts into packages/shared, and #5174
deleted vm-events.ts outright, so none of the three commits reverse-apply.

Core change: SandboxId goes back to a flat `{ userId, projectRef }` and
sandboxIdKey back to `userId:projectRef`, so every hosted claim handle
changes. Live shared claims must be drained BEFORE this deploys.

Also reverted, because they only exist inside the shared model:
- #5225 withoutLegacyAgentSandboxEntries — left in place it would strip
  the sandboxMap entries the reverted server writes back, leaving
  shouldAutoStart permanently true (silent, compiles fine)
- #5219 resolveSharedThreadVm and the shared-scope branch
- #5183 stale-provisioning reset (lived inside the deleted storage)

Deliberately preserved:
- migrations 137/138 and their index.ts entries — Kysely's
  #ensureNoMissingMigrations runs unconditionally, so deleting an applied
  migration crash-loops every pod. The tables are dropped by a follow-up
  forward migration after the drain, not by this commit.
- #5114/#5118 withClaimGitLock, #5126 org-context guard, #5143 analytics
  gate, #5171 owner-keyed thread graft, #5195/#5207/#5210/#5221 client
  auto-retry, the daemon status-code train and the settings-validation train
- #5112's org-fs least-privilege narrowing and the start dedup key fix
- #5116's squashed queue-tray pluralization (en + pt-br)
- the remote-branch-name validations #5132 deleted, since this restores
  the dynamic origin/HEAD lookup that made them load-bearing

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Pedro França <[email protected]>
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.

1 participant