Repository navigation
fix(threads): skip chat_started analytics on an id-collision replay - #5143
Merged
Merged
Conversation
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.
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 onidcollisions (storage uses INSERT … ON CONFLICT DO NOTHING)", but thechat_startedposthog event fired unconditionally, even on the conflict path where no row was actually inserted. This is a live path, not theoretical:shell-layout.tsx'susePanelActionscomments 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 thechat_startedcount 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'sON CONFLICT DO NOTHINGcorrectly returns the existing row both times, but the tool firedchat_startedon both calls, double-counting the metric.Fix: surface
isNewfromSqlThreadStorage.create()— it already knows internally whether theRETURNINGinsert produced a row (fresh insert) or fell through to the conflict-select (pre-existing row); this was just never exposed. The tool now only callsposthog.capturewhenresult.isNewis true. Addeddoes not re-fire the chat_started analytics event on an id-collision replaytocreate.integration.test.ts, spying onposthog.captureacross 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 existingis idempotent: creating with the same id twice returns the same rowtest in the same file, which still passes unchanged).Locally verified:
bun run fmt(clean) andcd apps/mesh && bunx tsc --noEmit(clean — the two callers ofstorage.threads.create,create.tsandenqueue-super-agent.ts, both type-check against the newThread & { isNew: boolean }return type without changes on the latter, since it never inspected the extra field).Summary by cubic
Prevents duplicate
chat_startedanalytics when thread creation replays with the same id. SurfacesisNewfrom storage and only emits analytics on new inserts to keep metrics accurate.SqlThreadStorage.create()now returnsThread & { isNew: boolean }to signal insert vs conflict.COLLECTION_THREADS_CREATEcallsposthog.captureonly whenresult.isNewis true.Written for commit 4bc3f0e. Summary will update on new commits.