Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
51f0f67
perf(objectql): recompute roll-up summaries with one grouped aggregat…
claude Nov 16, 2026
6659391
test(dogfood): pin roll-up recompute statement counts and value parit…
claude Nov 16, 2026
02e274b
test(objectql): pin one parent write per recompute and teach three do…
claude Nov 16, 2026
7480459
wip(objectql): roll-up batch scope deferSummaryRecompute (work in pro…
claude Nov 16, 2026
28d8762
feat(metadata-protocol): run the bulk update doors as one roll-up batch
claude Nov 16, 2026
486a266
test(objectql): pin the roll-up batch scope
claude Nov 16, 2026
9c6406e
test(metadata-protocol): pin the bulk update doors' roll-up batch
claude Nov 16, 2026
ce3dea6
test(dogfood): pin the bulk update door's roll-up batch statements an…
claude Nov 16, 2026
c37b71e
chore(changeset): roll-up batch deferral on the bulk update doors
claude Nov 16, 2026
a4adf63
test(objectql): drop an unused hook parameter
claude Nov 16, 2026
2f463e9
refactor(objectql): keep the batch-scope docblock in the facet, not t…
claude Nov 16, 2026
6b149c2
test(objectql): type the batch-scope pins' query options
claude Nov 16, 2026
e68802f
test: resolve the door pins' protocol to source; pay the lint load at…
claude Nov 16, 2026
df080eb
docs(objectql): say the batch scope is ambient; pin a hook-issued ins…
claude Nov 16, 2026
90871de
docs(objectql): a joined scope inside its own transaction recomputes …
claude Nov 16, 2026
14d8188
chore(changeset): declare the roll-up batch change semantic
claude Nov 16, 2026
54bc155
Merge remote-tracking branch 'origin/main' into claude/project-thread…
claude Nov 16, 2026
13e48a2
fix(objectql): defer only writes under the batch scope's own transaction
claude Nov 16, 2026
a35af28
feat(objectql): a roll-up a rule reads is not deferred in a batch scope
claude Nov 16, 2026
0868390
test(objectql): give the rule-read test's referenceTargetOf its READE…
claude Nov 16, 2026
056c5d5
fix(objectql): a parent's own rule, a computed field and a summary ch…
claude Nov 16, 2026
24ad072
test(core): classify the batch scope's rule-read test in the second-o…
claude Nov 16, 2026
1e91fa3
Merge remote-tracking branch 'origin/main' into claude/project-thread…
claude Nov 16, 2026
7c80b9d
test(objectql): give the computed-field hop's referenceTargetOf its R…
claude Nov 16, 2026
8191a5d
fix(objectql): a declared bound on a stored value and a stored one-ho…
claude Nov 16, 2026
fff7423
Merge remote-tracking branch 'origin/main' into claude/project-thread…
claude Nov 17, 2026
7a34cfc
test(objectql): pins for a summary precision, a unique index naming i…
claude Nov 17, 2026
584fd7d
fix(objectql): every stored computed field over a roll-up, and a summ…
claude Nov 17, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .changeset/rollup-batch-deferral-33795.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@objectstack/spec': patch
'@objectstack/objectql': minor
'@objectstack/metadata-protocol': patch
---

feat(objectql): a bulk update recomputes its parents' roll-up summaries once, at the end of the batch

Clause-②: yes (widening)
Metadata-protocol: semantic
Skills-docs: follow-up owed — `skills/objectstack-data/rules/relationships.md` (Roll-up Summaries: "the engine recomputes it server-side whenever a child is inserted/updated/deleted") does not say that a bulk update recomputes once at the end of the batch, that a summary a rule reads still recomputes after each row, or that a hook reads the pre-batch value meanwhile (ruling 6928862195); it moves as a governed skills-only change. `content/docs/data-modeling/fields.mdx` is updated here.

**Added:** an optional member on the engine slot contract, `IObjectQLEngine.deferSummaryRecompute(callback)` in `@objectstack/spec` (and the same optional member on `MetadataHostEngine` in `@objectstack/metadata-protocol`). It is a callback, entered and left by the engine as `transaction` is; a nested call joins the open scope. Inside it, the roll-up `summary` recompute every write would run after itself is recorded, and runs once when the callback settles: per child object and caller, one grouped aggregate per summary field and one write per parent for the whole batch. `ObjectQL` implements it. A caller probes for it; an engine without it keeps the per-row recompute, with the same values. Every way out of the callback recomputes: resolved, the callback's result (or `ERR_SUMMARY_RECOMPUTE` with `written` set to that result when a parent cannot be recomputed); rejected, the recompute still runs for the writes that landed and the callback's error is rethrown. Opened inside a transaction, the scope's recompute runs before the commit. Only writes under the transaction the scope was opened in (or under none) are deferred: a write under another transaction opened within the scope, such as a hook's own `ctx.api.transaction` or an atomic batch reached from inside a non-atomic one, recomputes at once inside that transaction, as before, because that transaction may commit before the scope ends.

**Changed:** the bulk update doors run their per-row loop inside that scope: `updateManyData` (`PATCH /api/v1/data/:object/updateMany`, both arms) and the `update` operation of `batchData`. A 50-row bulk update over 10 parents with two summary fields went from 400 to 182 statements (one summary field: 350 to 181; no summary field: 150, unchanged), measured on SQLite and PostgreSQL 16. The stored values are unchanged for every summary kind.

Two things a caller can see, which is why this declares `Metadata-protocol: semantic` (when an authored hook runs and what it reads move, as in `33760-async-hook-on-commit`):

- A hook or a flow step that reads a parent's summary during a bulk update reads the value from before the batch until the batch ends, as it does during a bulk create; before this change each row's hook saw the recompute of the rows before it. Rules do not change against today: a summary field a rule reads is not deferred (ruling 6928862195). A validation rule of any object that reads it one hop through a reference, a detail object's `requiredWhen` / `readonlyWhen` that reads it on `parent`, or a rule of the summary's own object that reads it on its own row, whether the rule names the summary or a formula or stored computed field calculated from it (and any summary a chain of summaries rolls up into one of those), a declared bound (`min`, `max`, `precision`, `unique`, …) or a declared unique index on the summary, and any stored computed field of its object calculated from it (the recompute's write judges that value whatever it declares: a bound, an option list, a number that is not finite), makes every roll-up of that child object recompute after each row inside the batch, so a bulk update answers such a rule as one-by-one updates do, for every summary function (two contacts set primary in one grid edit: the first is admitted, the second refused with the rule's text; an atomic edit that moves a line onto a capped invoice and back rolls back at the first move). A `parent.` predicate that reads a field that is not a summary (`parent.status == 'paid'`) does not turn the deferral off. A stored computed field of any object that reads the summary one hop (`record.invoice.line_count`) does turn it off, since it stores what it read at its own write; a `formula` does not, since it is computed when read.
- The parent's own write path (hooks, validation, audit entry, realtime event) runs once per recompute: once per parent for the whole batch, instead of once per child row, for a roll-up that is deferred. A roll-up a rule of the parent reads is not deferred, so those rules still judge every recompute.

The scope is ambient, not tied to the object being updated: a write a hook issues while the batch runs under the batch's own transaction (or under none, for a non-atomic batch), on any object (a hook's own insert, update or delete, for instance a child-row hook that inserts a row into a second child object of the same parent), has its roll-up recompute deferred to the end of the batch as well, so a summary over that second object also reads its pre-batch value until the batch ends.

Each row's own hooks, validation, audit and realtime run where they ran. An atomic bulk update whose end-of-batch recompute fails rolls back and answers the same rolled-back envelope a failed row answers (the last row that landed carries `ERR_SUMMARY_RECOMPUTE`, the rows before it `ROLLED_BACK`). A non-atomic one keeps its rows written, logs the stale summary once at `error`, and answers the rows as written, as a per-row recompute failure did.
23 changes: 23 additions & 0 deletions content/docs/data-modeling/fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,29 @@ writes them in (200 rows each) and refuses the rows the import refuses, with
the same message. It writes nothing, so a row in a later batch reads the
summary as stored before the import: two such rows that fall in different
batches of a dry run both preview valid, while the import refuses the second.
**A bulk update equals one-by-one updates (批量等于逐条):** a bulk update
(`updateManyData`, the `update` operation of `batchData`, a grid bulk edit)
recomputes the parents' summaries once, when the batch ends, unless a rule
reads one of the summaries that child object rolls up: a validation rule of any
object reading it one hop through a reference, a detail object's
`requiredWhen` / `readonlyWhen` reading it on `parent`, or a rule of the parent
object itself reading its own summary (a cap such as `record.line_count <= 5`,
or a declared bound such as `min` / `max` / `precision` / `unique` on the
summary, a declared unique index naming it, or any stored computed field of the
parent calculated from it, whose value the recompute's write judges whatever it
declares). A rule that reads a
formula or a stored computed field calculated from the summary reads the
summary too, and so does a rule on a summary that a chain of summaries rolls
the first one up into. A stored computed field of any object that reads the
summary one hop (`record.invoice.line_count`) counts as well, since it stores
what it reads at its own write. Then every roll-up of that child object
recomputes after each row, so each row's rules, and the parent's own rules
judging each recompute, read it as one-by-one updates would, for every summary
function, `avg` included. A `parent.` predicate that reads a field that is not a
summary, and is not calculated from one, does not count.
**Hooks and flow steps read the value from before the batch:** inside one bulk
create or bulk update, a hook or flow step that reads a parent's summary reads
it as stored before the batch, on create and update alike.
**Concurrent writes are not guaranteed (并发不保证):** two writes in flight at
the same time each read the summary they started from, so a guard like the one
above can still admit one row from each.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ const CALLERS: Record<string, Classification> = {
kind: 'not-a-served-read',
reason: "resolves a seed's references at install, under the system identity; serves nothing to a caller",
},
// [#33795] The batch scope's rule-read test (ruling 6928862195 B).
'packages/objectql/src/summary-rule-reads.ts': {
kind: 'not-a-served-read',
reason:
"names the object a validation rule's or a computed field's one hop reaches, from the declarations in the " +
'registry, only to decide whether a roll-up recompute inside a bulk update runs at once or at the end of the ' +
'batch; it reads no record and serves nothing of the target to any caller',
},
// ADR-0100: the credential-column guard's filter / sort half.
'packages/objectql/src/credential-evaluation-guard.ts': {
kind: 'not-a-served-read',
Expand Down
9 changes: 9 additions & 0 deletions packages/metadata-protocol/src/host-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ export interface MetadataHostEngine extends IDataEngine {
* see `protocol-stored-expression-fill.ts`.
*/
captureStoredExpressions?(object: string): StoredExpressionFillCapture;
/**
* [#33795] The engine's roll-up batch scope, typed off the `objectql` slot
* contract for the reason `transaction` is, and optional there as here by
* ruling 6922040858. The bulk update doors (`runUpdateManyLoop`, and the
* update arm of `runBatchDataLoop`) run their per-row loop inside it, so a
* batch recomputes its parents' summaries once. Degrading means today's
* per-row recompute: the same values, more statements.
*/
deferSummaryRecompute?: IObjectQLEngine['deferSummaryRecompute'];
// Protocol accesses additional engine members structurally; keep it permissive
// for this relocation (behavior unchanged — the concrete engine is injected).
[key: string]: any;
Expand Down
Loading
Loading