CL-6375: fix routine re-creation and repeated announcements - #121
Merged
Merged
Conversation
Covers a template-minted routine (presetKey) never producing a second row or a second "Created routine" notice on a repeat/racing create, the losing request compensating its own freshly-provisioned delivery workbench, and that a plain (non-preset) create is unaffected.
Root cause: ensureDefaultRoutines (default-routines.ts) seeded each
default preset ("Daily digest", "Last 30 days research") with a
"list existing routines, then POST if missing" check — a TOCTOU race,
not a DB-enforced idempotency guarantee, with no unique constraint
backing it. Two overlapping seed calls (onboarding's own "finish
setup" allows this deliberately, per pending-seed.ts) can both pass
the list check and both create a routine row, each provisioning its
own delivery workbench and each posting a "Created routine" notice —
exactly the duplicate routines, duplicate digest workbenches, and
repeated chat announcements reported in CL-6375.
Adds a nullable presetKey column to routine, unique per
(tenant_id, preset_key) while live (migrations.ts 0005). POST
/routines now accepts an optional presetKey and, when present, calls
the store's new createRoutineIfAbsent: a single INSERT ... ON
CONFLICT DO NOTHING against that index, so two concurrent creates for
the same preset are guaranteed exactly one winner. The loser gets a
200 back with the winner's own row (never a second insert, never a
fire, never a notice) and compensates (deletes) the delivery space it
had already provisioned before losing the race. Plain, non-preset
creates (Myra's own routine_create tool, the routines UI) are
unaffected — presetKey is opt-in.
ensureDefaultRoutines now sends each preset's stable assetName as
presetKey and treats a 200 response the same as a local "already
exists" skip. Its list-then-check remains as a fast path only; the
unique index is what actually prevents the duplicate now.
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.
Root cause
ensureDefaultRoutines(packages/hub-client/src/default-routines.ts), whichseeds every tenant's default routine presets ("Daily digest",
"Last 30 days research"), guarded against duplicates with a
list-existing-then-create-if-missing check — a TOCTOU race, not a real
idempotency guarantee, with no unique constraint behind it.
provisionPersonalTenantIfNeeded(packages/onboarding/src/provision.ts) cancall
seedTenant/ensureDefaultRoutinesagain on a subsequent sign-inwhenever
isFullySeededis still false, andpending-seed.ts's own headercomment explicitly allows two overlapping "finish setup" calls to both reach
the seed step. Two such calls can both pass the list-check (both see zero
existing routines), both POST, and both succeed: two "Daily digest" routine
rows, each with its own auto-provisioned delivery workbench, each posting its
own "Created routine ..." notice into chat.
Fix
routinegets a nullablepreset_keycolumn, unique per(tenant_id, preset_key)while the row is live (migration 0005,packages/routines/src/migrations.ts).RoutineStoregetscreateRoutineIfAbsent: one atomicINSERT ... ON CONFLICT DO NOTHINGagainst that index(
packages/routines/src/store.ts), so two concurrent creates for the same(tenantId, presetKey)are guaranteed exactly one winner.POST /routinesaccepts an optionalpresetKey. When present, the loserof a create-if-absent race gets a
200back with the winner's own row —never a second insert, never a fire, never an announcement — and
compensates (deletes) whatever delivery workbench it had already
provisioned before losing (
packages/routines/src/routes.ts). Plain,non-preset creates (Myra's
routine_createtool, the routines UI) areuntouched —
presetKeyis opt-in.ensureDefaultRoutinesnow sends each preset's stableassetNameaspresetKeyand treats a200the same as a local "already exists" skip.Its own list-then-check is kept as a fast path only; the unique index is
what actually prevents the duplicate now.
Tests
packages/routines/test/routes.test.ts: a repeated/racingpresetKeycreate returns one
201+ any number of200s, never a second row, poststhe "Created routine" notice exactly once, and compensates the loser's
freshly-provisioned delivery workbench. A plain create with no
presetKeyis unaffected.
packages/hub-client/test/default-routines.test.ts:presetKeyis sent aseach preset's
assetName; a200(lost the race) is read asalready-seeded, not re-disabled.
Scoped
bun test+tsc --noEmitgreen forpackages/routines,packages/hub-client,packages/onboarding, andapps/hub. TheDB-gated migration/drizzle-store suites (
store.drizzle.test.ts,migrations.test.ts) skipped locally — no reachable Postgres in thisenvironment — same skip condition as before this change.
Note for CL-6362
Did not touch any routines page UI files; this is the seed/data side only.
packages/routines/src/routes.tsandstore.tsare shared plumbing theroutines page also depends on, but no UI file changed.