Skip to content

Server: an opt-in check that an edit still means the row it was made on - #744

Merged
SunkenInTime merged 6 commits into
mainfrom
t3/server-row-lives
Nov 24, 2026
Merged

SunkenInTime merged 6 commits into
mainfrom
t3/server-row-lives

Conversation

@SunkenInTime

@SunkenInTime SunkenInTime commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

No picture: this is a server change, and no client turns it on yet.

What a player can lose today

The server identifies a row (an item, a lineup, a page, the strategy itself) by its id and a revision number. Sometimes the same id gets a brand-new row:

  • after an item, a lineup or a page is deleted and purged 30 days later, its id can be added again, and its revisions start over at 1;
  • deleting a strategy removes its rows at once, so uploading the same strategy again, or retrying a move to the cloud, brings the same ids back within minutes.

An edit a player made to the old row is then still accepted, because "revision 1" matches the new row's revision 1. A player who comes back online with a queued edit can overwrite, move or delete something a teammate made since, without anyone being asked.

What this PR does

It adds an option that the coming cloud store client will turn on. A client that sends sendsLives: true on ops:applyBatch puts, on each op, the creation time of the row it was edited against (expectedLife). If the row there now was created at a different time, the server refuses the op as row_replaced and sends back the row as it is now, so the player is asked instead of the edit landing on someone else's work.

"Life" is the row's createdAt. The server already sets it on every insert of the five row kinds, never changes it (bringing an item back, restoring a page, moving an item and copying all keep or create it correctly), and already sends it on every row.

Old clients send nothing new and get nothing new. Every shipped desktop build and the live web build: without sendsLives, the server does no extra lookup, checks nothing new, writes no new field, and returns exactly what it returns today. An expectedLife sent without sendsLives is ignored.

Schema: one optional field on operationEvents (appliedLife), written only for opted-in batches. No backfill. Old rows simply lack it.

What merging triggers: the Deploy Server workflow deploys convex/ to production, as every convex/ change on main does. Nothing else changes for players until a client release turns the option on.

Evidence

Live, on a preview deployment made from this branch (confident-marten-490; never production or the shared dev deployment):

  • A probe user created a strategy and an element, deleted the strategy (its rows are purged at once), created it again under the same id, and added the element again under the same id. On the fresh preview I first ran the internal maintenance:backfillAssetReferences from the CLI, because purges wait for that backfill.
  • The calls ran as the probe identity through npx convex run --identity with the preview's deploy key. The output, trimmed:
add, first life: {"appliedLife":1795498545733,"appliedRevision":1,"status":"applied"}
delete the strategy: {"ok":true}
the element row after the delete's purge: "purged"
add the same element id, second life: {"appliedLife":1795498549162,"appliedRevision":1,"status":"applied"}
opted-in move authored on the first life: {"current":{"life":1795498549162,"revision":1,"type":"element",…"rotation":135…},"reason":"row_replaced","status":"rejected"}
the same op id sent again without sendsLives: {"current":{"revision":1,"type":"element",…},"reason":"revision_mismatch","status":"rejected"}
the row after both: {"revision":1,"createdAt":1795498549162,"rotation":135}
a move from a client that sends no lives: {"appliedRevision":2,"status":"applied"}

The last line is today's behaviour, and what this option prevents: the old edit lands on the new row.

Today's clients, live on the same preview: today's shipped sync, through the desktop driver build on Linux, two test accounts (jules@ and mika@test.icarusstrats.com), shape-opacity-cloud and tag-color-cloud each with its teammate script. All four instances ended CLEAN, with 0 failed steps.

In convex/rowLives.test.ts (18 tests):

  • Byte for byte: a five-batch sequence is run with and without sendsLives. Without it, neither the answers nor the receipts contain any life field. With the life fields taken out, the opted-in answers, receipts and row equal the plain ones exactly.

  • Inside the shipped contract: one op of every kind, each outcome and its fields pinned:

    • a strategy patch;
    • a page add, patch, settings patch, reorder and delete;
    • an element patch, reorder and delete;
    • a lineup add, patch, reorder and delete;
    • a patch that really is stale (revision_mismatch);
    • a failed op (ELEMENT_TYPE_PAYLOAD_KIND_MISMATCH);
    • a replay without the flag of an op refused as row_replaced with it.

    Every answer is in fields and reasons shipped clients know. The replay comes back revision_mismatch and writes no second receipt. The shipped-clients check cannot see replays, so this test is the guard for them.

  • Refused on the earlier life: a write and a delete authored on the earlier life of an element are refused as row_replaced, and the replacement is untouched. The same write without lives, or with expectedLife but no sendsLives, lands on it, as today. An op on its own life goes as before and its answer names the life.

  • A resent op id after its row's id was reused returns the first answer's revision and life, leaves the replacement alone, and writes no second receipt.

  • Three creation times: a strategy, its page and the page's settings each have a different creation time. A strategy patch, a page patch and a settings patch each accept only the life of the row whose revision they check.

  • Put-backs: in a batch that sends lives, an add that names an expected revision or an expected life is a put-back. A put-back of an element or a lineup group that is gone is not_found and adds nothing; tested for both kinds with a revision, a life, and both. Without lives it adds the row again, as today.

  • Trash first: a life mismatch on a page in the trash, or on an element or a lineup group on one, meets the trash (PAGE_DELETED) exactly as today. A first row_replaced answer therefore carries the row.

  • Replays read the row fresh: a row_replaced sent again comes back with the row while the row is live, and without it once the page is in the trash or purged, the receipt still there. That is how every replayed refusal works on main.

  • A page with two settings rows fails only its own op (INVALID_PAGE_CONTENT_COUNT), and the rest of the batch applies.

  • The size limit: an op exactly at the limit fits with and without a life, with and without sendsLives. One byte more does not. expectedLife is not counted.

  • A strategy made again under the same id: a deleted strategy uploaded again, and an abandoned move made again. An op authored on the old strategy is refused as row_replaced, the new strategy is untouched, and without lives the op applies as today. The community replace path makes a snapshot under a new id, so it never re-creates an id.

Reads, counted: a new cost scenario, storeLivesReads, sends one op of each kind with and without lives on two strategies made alike. It fails if an op with lives reads more documents than the same op without. The life is taken from the row each apply already holds, so there are no extra reads:

Op Documents read, with lives / without
element add 6 / 6
element patch 8 / 8
element delete 12 / 12
lineup add 8 / 8
lineup patch 14 / 14
lineup delete 18 / 18
page settings patch 5 / 5
page patch 7 / 7
page add 8 / 8
page reorder 11 / 11
strategy patch 3 / 3

Cost tool (node tool/convex_cost/run.mjs, checked against the committed budgets; only the new scenario's budget was written):

  • All 158 metrics are within budget.
  • Of the 114 with a recorded figure, 113 are at 100% of it.
  • soloSessionStore read 96% of its recorded figure. That scenario sends no lives, so the 4% is run-to-run variance, as on the previous head.

Contract:

  • convex/function_spec.json is current, and the Dart client is regenerated (lib/collab/generated, exactly as the generator writes it).
  • Every new answer field is declared in tool/convex_opt_in_results.json as sent only with args.sendsLives. The new reason is declared by its value ("literal": "row_replaced"), not by its path, so the next new reason on that path is reported until someone decides about it. The shipped-clients check gains a test for that.
  • node tool/check_convex_shipped_clients.mjs and node tool/check_convex_deploy.mjs report no incompatibilities.
  • npx tsc --noEmit and npm run test:convex (582 tests) pass, as do the Dart client gauntlet and codegen tests, and the comments check.

Rules, for the client that turns this on

  • expectedLife names the row whose revision the op checks: the strategy for a page add, delete or reorder and for a strategy patch; the page, the page settings, the item or the lineup otherwise.
  • In a batch that sends lives, an add that names an expected revision or an expected life is a put-back, and a put-back whose row is gone is refused as not_found, because the row it names is not there. A delete whose row is gone stays a no-op. A patch whose row is gone stays not_found.
  • A page in the trash meets the trash first, so a first row_replaced answer carries the row as it is now. A replayed one carries it only while the row is live; the client treats a row_replaced without the row like any refusal without one.
  • A released build that replays an op id an opted-in build first sent is told revision_mismatch, and acts as on any stale edit. That needs both builds sharing stored ops, which no build does. Look at it again before one does.
  • expectedLife does not count toward the op size limit.
  • The check runs after a receipt replay, so a resent op id gets the answer it got the first time, with the life it landed on.
  • Known limits: an op that names a page only as a place (an add onto a page, a move to a page) carries no life for that page, so an item can still land on a page re-created under the same id; nobody's work is lost that way. Two rows created under one id within the same millisecond share a life; no client can delete and re-create a row that fast.

docs/collaborative-editing.md gains a section, "Which row an edit means", that says the same.

ci: convex contract cost analyze test

Summary by CodeRabbit

  • New Features
    • Collaborative edits can now detect when a row has been deleted and recreated, preventing stale changes from being applied to the replacement row.
    • Responses can identify the row associated with an applied change or explain when the current row differs from the one a client expected. Clients that do not use this feature retain their existing behavior.
  • Documentation
    • Added guidance on row identity, compatibility, and known limitations in collaborative editing.

… another life of its id is refused as row_replaced, and answers and receipts name the life of the row they belong to; clients that do not opt in send and receive nothing new
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 37 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fec77065-795d-498b-85cb-29563501178b
Commits

Reviewing files that changed from the base of the PR and between 3b6a665 and 5668b8b.

Files selected for processing: 3

  • convex/ops.ts
  • convex/rowLives.test.ts
  • docs/collaborative-editing.md

No actionable comments were generated in the recent review.

Walkthrough

The batch API now supports optional row-life metadata. It compares expected creation times with current rows, returns life data for opted-in clients, and records applied life in replay events. Tests, shipped-client opt-in checks, documentation, and read-cost scenarios cover the protocol.

Changes: Row-life-aware operations

Layer / File(s) Summary
Life fields and compatibility declarations
convex/function_spec.json, convex/lib/opTypes.ts, tool/check_convex_shipped_clients.mjs, tool/check_convex_shipped_clients.test.mjs, tool/convex_opt_in_results.json
Operation inputs and results add optional life fields and the row_replaced reason. The shipped-client checker supports literal-specific opt-ins for result values.
Batch row-life checks and replay
convex/ops.ts, convex/schema.ts
applyBatch checks expected row life, returns life data to opted-in clients, and stores applied life in operation events for replay.
Row-life validation and read comparison
convex/rowLives.test.ts, docs/collaborative-editing.md, tool/convex_cost/lib/scenarios.mjs, tool/convex_cost/lib/sessions.mjs, tool/convex_cost/budgets.json, tool/convex_cost/run.mjs
Tests cover row replacement, replay, compatibility, and operation-size limits. Documentation describes the contract and its limits. The cost scenario compares document reads with and without lives.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant applyBatch as ops:applyBatch
  participant TargetRow as Target row
  participant Events as operationEvents
  Client->>applyBatch: sendsLives and expectedLife
  applyBatch->>TargetRow: read snapshot and createdAt
  applyBatch->>applyBatch: compare expectedLife with row life
  applyBatch->>Events: record appliedLife when present
  applyBatch-->>Client: return operation result
Loading

Possibly related issues

Merge Risk: Minimal · previous review 3b6a6 · assessed changes only

Path filters excluded changed files. This rating applies only to the assessed changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: an opt-in server check that rejects an edit when the target row was recreated (its life differs from the expected life). The objectives and summaries describe the opt-…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: an opt-in server check that rejects an edit when the target row was recreated (its life differs from the expected life). The objectives and summaries describe the opt-in sendsLives/expectedLife check and the row_replaced rejection in ops:applyBatch, so the title matches the changeset. The wording "an edit still means the row it was made on" is somewhat abstract, but it is specific enough for a teammate scanning history to identify the primary change.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 227a97ca-cd98-47c2-abea-382f98720625
Commits

Reviewing files that changed from the base of the PR and between ec014c3 and 3b6a665.

Files ignored due to path filters: 2

  • lib/collab/generated/convex_models.dart is excluded by !**/generated/**
  • lib/collab/generated/icarus_convex_api.dart is excluded by !**/generated/**
Files selected for processing: 13

  • convex/function_spec.json
  • convex/lib/opTypes.ts
  • convex/ops.ts
  • convex/rowLives.test.ts
  • convex/schema.ts
  • docs/collaborative-editing.md
  • tool/check_convex_shipped_clients.mjs
  • tool/check_convex_shipped_clients.test.mjs
  • tool/convex_cost/budgets.json
  • tool/convex_cost/lib/scenarios.mjs
  • tool/convex_cost/lib/sessions.mjs
  • tool/convex_cost/run.mjs
  • tool/convex_opt_in_results.json

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

…laced never reaches a caller that did not opt in, even on a replay; a put-back of a gone row is not_found in any batch that sends lives; expectedLife is not counted toward the op size; a page in the trash meets the trash first; a literal opt-in for the new reason; the Dart client regenerated; tests for each, and a cost scenario that fails if an op with lives reads more
SunkenInTime and others added 2 commits November 24, 2026 00:53
…k, and a gone row's put-back is not_found in any batch that sends lives; tests for replays after trash and purge, content on a page in the trash, and every op kind pinned; the doc says when a replay carries the row and why the old reason stays
@SunkenInTime
SunkenInTime merged commit 39c7c50 into main Nov 24, 2026
24 checks passed
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