feat(web): per-event write lane for calendar event mutations
Description
Replaces the single-slot update/create/delete "queues" with a per-event serialized write lane, and makes the event form track the calendar again through a field-level merge.
Today the update machine holds one item; a START arriving while it is in mutate or a prompt state has no transition and is silently dropped, so "rename, then drag" loses whichever edit comes second while its optimistic overlay stays on screen. Optimistic update overlays are also never removed on success, and rapid edits send a stale client etag that both providers reject with 412.
Write lane
flows/write-lane.ts: one lane per event id shared by create, update and delete. While a request is in flight, later edits merge into a single pending patch (later field wins;start/end/allDaymove as a unit) that is sent once the in-flight request returns. The lane keeps exactly one optimistic overlay per event (keyed by event id), writes the server response through to everyevents.listcache and Dexie, and only clears the overlay after the awaited refetch lands. It re-keys itself when a provider returns a new id (Microsoft create / copy-and-delete move), keeps an old→new alias map so edits queued behind a prompt under the old id still land, and updates selection, form baseline, caches and Dexie accordingly. On failure it toasts, keeps the server baseline for the failed fields, and still drains later pending writes.- Staged edits: edits waiting on a scope/notify prompt, and edits deferred into a dirty form, are staged in the lane per token rather than written into a shared preview overlay. The overlay projects
baseline + sent + pending + staged;current(id)(used as the form baseline) projects lane-owned state only,shown(id)includes staged,busy(id)says whether the lane knows more than the stored copy (write in flight, queued, or settling). Dispatch hands the token toenqueue, cancel unstages it, Discard and full rehydration release deferred tokens, and the provider disposes all overlays and tokens on unmount.stage()toasts and returnsundefinedif the stored event cannot be read, and the action hooks stop there. - Race hardening: a
createenqueued while a lane already exists for the id merges in as an update instead of creating twice;applyChangeskeeps the target's id so a drag queued behind a re-key is sent under the new id; series writes report the edited occurrence back rather than the master;finish()is guarded by a per-lane generation and lane identity, and bothenqueueandfinishhandle their own failures (toast, overlay reset) instead of leaving a permanent overlay; the existing-lane path inenqueueis synchronous so a settle resuming in the same tick cannot tear down a lane a new edit already holds. update-queue.ts/create-queue.ts/delete-queue.ts: machines are now prompt-and-dispatch FIFOs;STARTis accepted in every state and queued,mutate/rollbackstates are gone. Create prompts supportonCancellike update; cancelling a create prompt puts the draft overlay back. The prompt dialogs use plainButtons for the choices: a RadixAlertDialogActioncloses the dialog itself and itsonOpenChange(false)sentCANCELinto the next queued item.use-update-action.tsx: replace-saves computechangedFields(event, previous)against the form snapshot so remote edits are never re-emitted; partial edits pass theirchangesthrough untouched, keep the lane's current id, and patch a dirty form from the lane'sshownevent after staging so two edits arriving together both survive.useUpdateActionreports whether the save was staged; deferred previews are only released when it was.use-event-mutations.ts: bare mutations; the lane owns optimistic state, rollback and the (single, awaited) list invalidation.- Providers: Google
updateuses only the etag from its own GET asIf-Matchand retries once on 412 (re-GET + reapply the sparse patch). Microsoft retries once on 412ErrorIrresolvableConflict, re-resolving the recurrence anchor (in case the master moved) and keeping the accept/decline action outside the retry so it is never re-sent.etagis removed frompatchEventInputSchemaand the payload builders. lib/interfaces.ts:EventChangesis a mapped type written directly over theCalendarEventunion.Partial<CalendarEvent>is not equivalent:Partialis homomorphic over a type parameter and distributes over the union, so astart/allDay/calendarwhose arm is not statically known is rejected (this is why two@ts-expect-errordirectives in the drag code became unnecessary).
Form
live-update.tsxfollowsformAtom.event.id(not the machine state) and takes the lane-owned event (lane.current) only while the lane isbusy, else the Dexie row, so the form sees a queued drag/RSVP immediately, stays live after a save, never adopts a staged-only edit as its baseline, and still merges remote changes while an edit is deferred.use-event-form.ts: a pristine form (or a different event) rehydrates withform.reset(values); a dirty form on the same event advances its diff baseline to the incoming event and fills only fields whose TanStack meta is not dirty (setFieldValue(..., { dontUpdateMeta: true })), always takingid/type, so the user's edits win on the next save and a drag made while typing is no longer reverted. The last hydrated event is tracked by ref so a provider re-key (which writes the returned event into the atom first) does not skip hydration. Save success resyncs throughLOAD savedinstead ofCONFIRMED, which also stops server-normalized fields (conference, attendee status) from being re-emitted on every save.- Deferred calendar edits (
pendingFieldPatchAtom) carry{ values, keys }plus their stage tokens (deferredStageTokensAtom) and no longer rewrite the reset baseline;useDiscardActionresets the form, clears the patch, releases the staged edits and marks pristine; cancelling a scope/notify prompt on a form save (create or update) discards the form instead of re-prompting on the next blur. useSaveActionbuilds the event on top of the snapshot so untouched fields (color,metadata) are not emitted as cleared.- Drags/resizes anchor on the displayed item instead of the frozen form snapshot (
use-draggable-event-actions.ts,draggable-month-event.tsx);getEventInFormis gone. event-form-state.ts:CONFIRMEDremoved; the machine stays inloadingfor the shown event.
Type of Change
- Bug fix
- Refactor / Cleanup
Related Areas
- Calendar UI
- Data/API
Testing
bun run type-checkforapps/web,packages/providers,packages/schemas,packages/api;bun run lint0 errors;oxfmt --checkclean on all touched files.- Throwaway
bun testsuites (not committed) against the lane, the machines, a real TanStackFormApi, the real hooks and the real Radix prompt dialog in JSDOM with mocked lane deps, and a stubbed Microsoft client: 34 + 15 + 1 passing. Coverage includes rename + two drags in flight coalescing to 2 requests; failed write keeps the baseline and still sends the pending write; update during an in-flight create waits and follows the new id; writes queued under the old id resolve through the alias; a second save during an in-flight create becomes an update; a drag queued behind a re-key is sent under the new id; a series save reports the occurrence back; older/stale settles cannot tear down a lane a newer write or a re-created lane owns; an enqueue in the tick a settle resolves keeps one lane; cancelling one prompt keeps another queued edit's preview; choosing in the real prompt dialog does not cancel the next queued item; cancelling a create prompt restores the draft overlay; two deferred edits compound, including two arriving before either staged; a deferred drag never advances the form baseline and is emitted on save; a staged-only lane is notbusyso remote changes still reach the form; a failing Dexie read toasts instead of rejecting; a save that could not be staged keeps its deferred previews; a re-keyed create still hydrates the form; dirty fields survive a merge while untouched fields follow the incoming event;reset()after a deferred patch returns to the baseline; the Microsoft 412 retry re-resolves the recurrence anchor and runs the RSVP action once. Each race/regression test was also run against the pre-fix code to confirm it fails there. - Manual testing performed — requires a signed-in Google/Microsoft account in the browser, which the agent environment does not have.
Notes
Per the confirmed plan: no timer debounce, no new sync/push mechanism, and no per-provider event field split in this pass.
By submitting, I confirm I understand and stand behind this code. If AI was used, I've reviewed and verified everything myself.


Summary by cubic
Replaces the single-slot create/update/delete queues with a per-event write lane so rapid edits serialize and coalesce instead of being dropped or rejected with stale etags, and keeps the form live against the event it shows so a drag made while typing is no longer reverted by the next save.
Write lane
- Edits arriving mid-flight merge into one pending patch; one optimistic overlay per event is written through to the
events.listcache and Dexie and cleared only after the awaited refetch. - Prompt and deferred edits stage per token, so cancelling one prompt doesn't drop another queued edit's preview; cancelling a create prompt restores the draft overlay so the draft stays visible.
- The overlay projects baseline + sent + pending + staged while the form baseline only follows lane-owned state;
LiveUpdateProvidertakes the lane's view only while busy, so staged-only edits never hide remote changes. - The lane re-keys on a new provider id and is race-safe: a save during an in-flight create merges in as an update, series writes report the edited occurrence, and an older settle can't clear a lane a newer write owns.
- The three queue machines are prompt-and-dispatch FIFOs; Google sends only the etag from its own GET as
If-Matchand retries once on 412, Microsoft dropsifMatchand retries just the sparse PATCH once on 412 — re-resolving the recurrence anchor — so the RSVP action isn't re-sent.
Form
- A pristine form rehydrates from the stored copy; a dirty form advances its baseline and fills only unedited fields, so a drag made while typing survives the next save.
- Replace-saves diff against the form snapshot via
changedFieldsso remote edits are never re-emitted;EventChangesis a non-distributing mapped type.