@abloatai/ablo 0.47.0 → 0.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,305 +1,119 @@
1
1
  # Concurrency Convention
2
2
 
3
- > The governing rule for how Ablo resolves concurrent writes to shared state.
3
+ > What Ablo checks when a guarded write depends on earlier state.
4
4
 
5
- This page is the contract: the `onStale` dispositions, what a conflict is
6
- checked against, and where the convention stops. The three-layer mechanics of
7
- claiming live in [Coordination](./coordination.md).
5
+ Ablo never infers whether a write depends on earlier state. You decide, in two
6
+ places. The model's `conflict` setting in the schema says what each kind of
7
+ participant does when it hits a conflict, and it is the policy for that model.
8
+ A per-write `onStale` states the disposition for one write. Ablo enforces what
9
+ you declared and nothing else.
8
10
 
9
- ---
11
+ ## Unguarded writes
10
12
 
11
- ## 1. The principle: non-coercion
12
-
13
- **The engine surfaces the truthful current state and lets the intelligent actor —
14
- agent or human — decide what to do. It does not force a resolution.**
15
-
16
- That is the whole convention. Everything below is a consequence of it.
17
-
18
- Classical concurrency control is *coercive*: it imposes the remedy. Two-phase
19
- locking forces a block; optimistic concurrency forces an abort. Ablo's wager is
20
- that the actor in the loop (an agent reasoning over the change, or a human
21
- watching the row) is better placed to resolve a conflict than a fixed rule baked
22
- into the storage layer. So the engine's job narrows to one thing: **report what
23
- is true, on time, and get out of the way.**
24
-
25
- There are two forms of non-coercion, and they are the same principle at two
26
- moments in time:
27
-
28
- | form | when | mechanism |
29
- |---|---|---|
30
- | **Claim** | *prospective*: before you act | reserve the row; others queue. Coordinate so the conflict never forms. |
31
- | **Notification** | *in-flight*: after a concurrent change | surface the changed value; the actor resolves and re-issues. |
32
-
33
- Use a claim when you will hold the row across a slow read→reason→write gap. Use a
34
- notification when you didn't, and the premise moved under you.
35
-
36
- ---
37
-
38
- ## 2. The dispositions (`onStale`)
39
-
40
- Every guarded write (and every premise declared in §4) says what should happen
41
- when it goes stale. Three modes, split by whether they **force** an outcome:
42
-
43
- | mode | coercive? | what the engine does | who resolves | use when |
44
- |---|---|---|---|---|
45
- | `notify` | **No**: surface + delegate | Holds the write (does **not** apply it); returns a `StaleNotification` with the current value. | The actor (agent or human) reconciles and re-issues. | The aligned mode: tell the actor what changed, let it solve. |
46
- | `reject` | **Yes**: force-abort | Throws `AbloStaleContextError`; the batch is discarded. | The caller retries from scratch. | Hard invariants; legacy/strict callers. The current default. |
47
- | `overwrite` | **Yes**: force-clobber | Overwrites blindly last-writer-wins; **no** signal. | Nobody. | You genuinely own the field and concurrent values are noise. |
48
-
49
- > `notify` is the convention. `reject` and `overwrite` are escape hatches for the
50
- > two ends — "never let this be wrong" and "never bother me." They are not the
51
- > spirit; they are the boundary of it.
52
-
53
- ---
54
-
55
- ## 3. What is checked: two premises
56
-
57
- A conflict is a **premise intersection** — what your operation was based on
58
- overlaps a concurrent delta. Ablo checks two premises, and they are independent.
59
- They differ only in what declared them:
60
-
61
- | premise | declared by | question | scope |
62
- |---|---|---|---|
63
- | **Write-target** | per-op `readAt` | "did a row I'm **writing** change since I read it?" | the rows in `operations[]` |
64
- | **Batch** | batch-level `reads[]` | "did anything I **looked at** change since I read it?" | rows/groups in `reads[]`, even if not written |
65
-
66
- The write-target check alone is the narrow case the canary anomaly defeats: an
67
- agent reads `deal.stage`, writes `task.status`, and a peer moves `deal.stage` —
68
- `task` never changed, so a write-target-only check waves it through. The batch
69
- premise closes that gap.
70
-
71
- ---
72
-
73
- ## 4. The batch premise (`reads[]`)
74
-
75
- A commit may declare, at the batch level, what its writes were based on.
76
- Two granularities, developer's choice per entry:
13
+ A plain write has no stale premise:
77
14
 
78
15
  ```ts
79
- reads: [
80
- { model: 'Document', id: 's-1', readAt: N, fields?: ['title'] }, // ROW premise
81
- { group: 'workspace:abc', readAt: N, onStale: 'notify' }, // GROUP premise
82
- ]
16
+ await ablo.tasks.update({ id, data: { status: 'done' } });
83
17
  ```
84
18
 
85
- - **Row:** did this specific row (optionally these fields) change? The literal
86
- per-object premise.
87
- - **Group:** did *anything* in this sync group change? `group` is a sync-group
88
- key (`workspace:abc`, `document:s1`, `org:X`) the same unit a participant **watches
89
- and claims**. This is the more Ablo-native granularity.
19
+ If no active claim conflicts with it, the write is last-write-wins. That is a
20
+ choice rather than a fallback: use it for independent assignments where the
21
+ latest value should win. When a model's writes are never independent, say so
22
+ once in its `conflict` setting instead of at every call site.
90
23
 
91
- **Boundary a stale premise fires over the whole batch.** Each entry covers
92
- *all* the writes in the commit, so its disposition governs the batch:
93
- `reject` aborts it, `notify` holds **every** write and notifies, `overwrite`
94
- lets them land. Per-entry `onStale` defaults to `reject`.
24
+ ## Guarded writes
95
25
 
96
- ---
26
+ Pass the exact returned rows when a write is based on values previously read:
97
27
 
98
- ## 5. The notification (`StaleNotification`)
99
-
100
- The non-coercive modes hand back data instead of throwing. The signal is
101
- delivered **twice**, by design once as a value, once as an event:
102
-
103
- - On the **commit receipt**: `receipt.notifications` (and `CommitResult.notifications`).
104
- - On the **event channel**: `conflict:notified` (mirrors `reconciliation:needed` /
105
- `sync:rollback`).
106
-
107
- Shape (canonical in `coordination/schema.ts`): one advisory in two scopes, told
108
- apart by `scope`, the same way `reads[]` entries come at two granularities.
28
+ ```ts
29
+ const task = await ablo.tasks.get({ id });
30
+ const policy = await ablo.policies.get({ id: policyId });
31
+ if (!task || !policy) throw new Error('required input is missing');
32
+
33
+ await ablo.tasks.update({
34
+ id: task.id,
35
+ data: { status: 'done' },
36
+ reads: [task, policy],
37
+ });
38
+ ```
109
39
 
110
- Both scopes carry:
40
+ Ablo privately resolves each exact object to its model, id, and read watermark,
41
+ then compares those premises with current state when the write is accepted.
42
+ Clones, fabrications, and rows returned by another client are rejected locally.
111
43
 
112
- | field | meaning |
44
+ | Disposition | If the premise is stale |
113
45
  |---|---|
114
- | `object` | stable type tag: `'stale_notification'` |
115
- | `scope` | `'row'` or `'group'` which premise granularity fired |
116
- | `target` | **the row that moved**, and which parts: `{ model, id, fields }`. The same locator a claim names its subject with |
117
- | `readAt` | the watermark the committer reasoned against |
118
- | `observedSyncId` | the newest delta on the premise: re-read at/after this |
119
- | `writtenBy` | `{ kind, id }` of the concurrent author, reported faithfully |
120
-
121
- `scope: 'row'` adds:
46
+ | `reject` | Reject the write with `AbloStaleContextError`. |
47
+ | `notify` | Keep the current row, return a `StaleNotification`, and let the caller reconcile. |
48
+ | `overwrite` | Apply the new value without enforcing the stale premise. |
122
49
 
123
- | field | meaning |
124
- |---|---|
125
- | `currentValues` | the live values of `target.fields`: the premise to reconcile against |
50
+ `notify` is useful when an agent or human can merge the new information.
51
+ `reject` is useful when the caller should restart from fresh state. Use
52
+ `overwrite` only when the newer assignment should unconditionally win.
126
53
 
127
- `scope: 'group'` adds:
54
+ ## Functional updates
128
55
 
129
- | field | meaning |
130
- |---|---|
131
- | `group` | the group premise that fired (`report:abc`) |
132
- | `propagation?` | how `target` reached `group`: `{ via, through }` |
133
- | `changed?` | how much of the group moved: `{ count, sample, truncated }` |
56
+ For a pure read-modify-write calculation, use the functional update form:
134
57
 
135
- `target` names the row that changed in **both** scopes. A group notification
136
- reports the moved row there and the premise it broke in `group`, so the two are
137
- never the same field.
58
+ ```ts
59
+ await ablo.counters.update(counterId, (current) => ({
60
+ value: current.value + 1,
61
+ }));
62
+ ```
138
63
 
139
- Only `notify` produces a notification (the write was held). `reject` throws and
140
- `overwrite` is silent — neither notifies.
64
+ It performs the read, guarded write, and bounded reconciliation loop for you.
65
+ See [Coordination](./coordination.md#functional-updates).
141
66
 
142
- ### 5.1 The receive → reconcile loop
67
+ ## Claims
143
68
 
144
- You receive the signal two ways (same payload), then re-commit against the fresh
145
- watermark. The engine never re-issues for you the actor decides.
69
+ A claim protects a target across a longer interval. By default, other
70
+ participants cannot write the claimed target, while contenders that claim it
71
+ wait their turn. Reads remain open. A model's explicit conflict policy can
72
+ choose a different disposition for a participant kind.
146
73
 
147
- ```ts
148
- // Trigger: a guarded write under the non-coercive mode.
149
- const receipt = await ablo.task.update({
150
- id, data: { status: 'blocked' },
151
- readAt: myWatermark,
152
- onStale: 'notify',
153
- });
74
+ Claims and stale guards protect different things:
154
75
 
155
- // Receive pull: the held write surfaces on the receipt.
156
- for (const n of receipt.notifications ?? []) resolve(n);
157
-
158
- // Receive — push: the same StaleNotification[] fires ambiently on the socket.
159
- ws.subscribe('conflict:notified', ({ notifications }) => notifications.forEach(resolve));
160
-
161
- function resolve(n: StaleNotification) {
162
- // n.target — the row that moved, and which fields (both scopes)
163
- // n.writtenBy — who moved it (e.g. { kind: 'agent', id: 'agent-b' })
164
- if (n.scope === 'group') {
165
- // "something in report:abc moved" — but `target` says WHICH row, so this is
166
- // a one-row re-read, not a re-read of the group.
167
- // n.propagation?.via — 'self' | 'parent' | 'transitive'
168
- return refreshRow(n.target.model, n.target.id, n.observedSyncId);
169
- }
170
- // n.currentValues — what's actually there now (e.g. { status: 'done' })
171
- if (!stillValid(n.currentValues)) return; // premise gone → drop the write
172
-
173
- return ablo.task.update({
174
- id: n.target.id,
175
- data: { status: 'blocked' },
176
- readAt: n.observedSyncId, // adopt the new high-water mark — this is what terminates the loop
177
- onStale: 'notify',
178
- });
179
- }
180
- ```
76
+ - A claim excludes other participants while it is held.
77
+ - A stale guard proves that the state a write depended on has not changed.
78
+ - A write made under a claim is still rejected if its own claimed snapshot has
79
+ become stale.
181
80
 
182
- The loop **terminates** because each retry advances `readAt` to `observedSyncId`;
183
- a peer that keeps writing only ever notifies you against a *newer* baseline, never
184
- the same one twice.
81
+ See [Coordination](./coordination.md#claims) for the API.
185
82
 
186
- ### 5.2 Reading a group notification
83
+ ## Cross-row and batch premises
187
84
 
188
- A group premise says "anything in this group", so the notification has to answer
189
- "what, exactly?" or the only correct response is to re-read the whole group —
190
- which on a busy group is how a reconcile loop turns into a large, repeated read.
85
+ Model writes and lower-level commits can declare rows they read even when the
86
+ write targets somewhere else. This protects decisions such as “update the task
87
+ only if the deal I inspected has not changed.” A stale batch premise applies to
88
+ the whole batch so atomicity is preserved.
191
89
 
192
- Four fields answer it:
90
+ Use the high-level model methods unless you are building a custom runtime. When
91
+ you do use batch premises, declare only the rows or groups that materially
92
+ influenced the decision; overly broad premises create unnecessary contention.
193
93
 
194
- - **`target`** — the row that moved and the fields that changed on it. Usually a
195
- one-row re-read.
196
- - **`group`** — the premise that fired.
197
- - **`propagation`** — how the row reached the group. `via: 'self'` means the row
198
- *is* the group's scope root; `'parent'` means one containment edge below it;
199
- `'transitive'` means further up, with `through` listing the intermediate
200
- models (`['slides', 'decks']`).
201
- - **`changed`** — how much moved, in **distinct rows** rather than deltas.
202
- `count` is the total, `sample` names the most recently changed (capped), and
203
- `truncated` says whether the sample tells the whole story.
94
+ ## Notifications
204
95
 
205
- `changed` is what lets an actor decline. One row moved, re-read one row; twelve
206
- thousand moved, stop reasoning and resync the group or abandon:
96
+ A `StaleNotification` identifies the stale premise and provides the current
97
+ state needed to reconcile. The original write has not been applied.
207
98
 
208
- ```ts
209
- if (n.changed && n.changed.count > 50) return null; // too much moved — abandon the write
210
- for (const row of n.changed?.sample ?? []) await refresh(row.model, row.id);
211
- ```
99
+ A typical loop is:
212
100
 
213
- `propagation` is absent when the commit path has no record-group spec for the
214
- model, which is the one case where the server knows a group moved without
215
- knowing the route. Treat its absence as "re-read the group", the old behavior.
101
+ 1. Inspect the current value in the notification.
102
+ 2. Recompute the intended change.
103
+ 3. Submit a new guarded write with a fresh premise.
216
104
 
217
- ### 5.3 Gating on what you know
105
+ Give this loop a retry budget. Continuous contention should surface to the
106
+ caller rather than retry forever.
218
107
 
219
- The notification tells you a belief moved. Whether that should *stop you writing*
220
- is a separate decision, and it belongs on the belief, not on the write.
108
+ ## Boundaries
221
109
 
222
- A `track` is a standing premise — what you are watching, with a watermark for
223
- when you last looked. Its disposition says what a move does to your next commit:
110
+ Concurrency control does not replace:
224
111
 
225
- ```ts
226
- await ablo.documents.track({ id: 's-1' }); // notify report it
227
- await ablo.documents.track({ id: 's-1', onStale: 'reject' }); // reject gate my next write
228
- ```
112
+ - database constraints and transactions for application invariants;
113
+ - authorization for deciding who may read or write;
114
+ - idempotency for safely replaying the same request;
115
+ - claims for exclusivity across slow, side-effecting work.
229
116
 
230
- `reject` refuses your next commit while that belief is stale, **even if the
231
- commit writes an unrelated row**. The gate is on what you *know*, not on what you
232
- are touching — which is the case a read→reason→write agent actually has.
233
-
234
- It is enforced at the commit chokepoint, so it is a guarantee rather than a
235
- convention. And the gate takes a key: it does not reopen on its own, because an
236
- agent retrying blindly would land exactly the write `reject` was asked to
237
- prevent. Re-read, then re-register the track to say you have. See
238
- [Groups](./groups.md#reporting-or-gating-onstale).
239
-
240
- A row that goes stale twice is telling you something: the read→reason→write gap
241
- wants the *prospective* guard, not the in-flight one. Escalate to a claim
242
- (§1) rather than raising `retries`.
243
-
244
- ---
245
-
246
- ## 6. Boundaries & invariants
247
-
248
- What the convention **guarantees**, and where it **stops**:
249
-
250
- 1. **Engine surfaces, actor decides.** Under `notify` the engine never
251
- repairs, merges, or re-plans. It reports `currentValues` and the actor (agent
252
- or human) owns the resolution. The engine does not distinguish them — it is
253
- actor-neutral by design.
254
-
255
- 2. **Truthfulness:** `currentValues` / `observedSyncId` reflect committed state at
256
- detection time, inside the same transaction as the write. A notification is
257
- never speculative.
258
-
259
- 3. **No livelock, which is not the same as termination.** The monotonic
260
- `sync_id` landing order is the serialization order. The stale committer
261
- always yields/recomputes — an asymmetry that rules out the symmetric
262
- notify-rewrite livelock, because each round adopts a newer `observedSyncId`
263
- and no baseline is ever reasoned against twice.
264
-
265
- It does **not** rule out starvation. A peer writing faster than your
266
- read→decide→write gap keeps winning, and the engine never re-issues on your
267
- behalf, so the rounds are yours and they are unbounded. Progress in the
268
- watermark is not progress in the work — and for an agent, each round is a
269
- model call. `update(id, fn)` bounds it for you and hands the conflict to
270
- your updater (§5.3); a hand-rolled loop must bound itself.
271
-
272
- 4. **Scope: reversible DB state only.** The convention governs writes to the
273
- shared database, which are inherently reversible (prior value in
274
- `sync_deltas`). **Irreversible external side-effects** (emails, payments,
275
- third-party calls) are *out of scope* — the engine cannot hold or undo them,
276
- so they must not be gated by `notify`.
277
-
278
- 5. **Defaults.** A plain write (no `readAt`) is last-writer-wins with **no**
279
- check. A guarded write with `readAt` but no `onStale` defaults to `reject`.
280
-
281
- 6. **Policy seam.** Custom `ConflictPolicy` functions see **write-target**
282
- conflicts (`stale_context` / `claim_held`). **Batch-premise** conflicts are
283
- resolved directly via each entry's `onStale`, not through the policy seam.
284
-
285
- 7. **Claims win when held.** A non-holder writing to a claimed row is rejected
286
- (`AbloClaimedError`) regardless of `readAt` — the prospective form takes
287
- precedence over the in-flight form. Only `user`/`system` principals may
288
- `bypass` a foreign claim; agents may not.
289
-
290
- ---
291
-
292
- ## 7. What this convention does not cover
293
-
294
- Three limits worth knowing before you rely on it.
295
-
296
- - **Irreversible external side-effects.** Emails, payments, and third-party
297
- calls are not gated by this convention (§6.4). The engine cannot hold or undo
298
- them, so never place one behind `notify`.
299
- - **A caller that declares nothing gets no check.** The batch premise catches
300
- only what you declared. Write-target checking needs a `readAt` to compare
301
- against, so a plain write with neither is last-writer-wins (§6.5). What you
302
- declare is what is protected.
303
- - **`writtenBy.kind` reports what authenticated, not what you meant.** An `sk_`
304
- key resolves to `system`, not `agent`. How identities map to participant kinds
305
- is a separate concern from this convention.
117
+ The rule is simple: the model's `conflict` setting is the policy, and each write
118
+ declares what it read. Plain writes are last-write-wins because declaring
119
+ nothing is itself a decision, so make it deliberately.