Skip to content

Commit 584fd7d

Browse files
committed
fix(objectql): every stored computed field over a roll-up, and a summary's precision or unique index, count as reading it
The recompute's own write judges every value it stores against its declaration, so a stored computed field calculated from the summary is judged at each intermediate value one by one, bound or not (an option list, a value domain, a number that is not finite). The summary itself counts when it declares a bound, now including `precision`, or when a declared unique index names it. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7a34cfc commit 584fd7d

5 files changed

Lines changed: 70 additions & 26 deletions

File tree

‎.changeset/rollup-batch-deferral-33795.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Skills-docs: follow-up owed — `skills/objectstack-data/rules/relationships.md`
1616

1717
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`):
1818

19-
- 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), and a declared bound (`min`, `max`, `unique`, …) on the summary or on a stored computed field of its object calculated from it, 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.
19+
- 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.
2020
- 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.
2121

2222
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.

‎content/docs/data-modeling/fields.mdx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,10 @@ reads one of the summaries that child object rolls up: a validation rule of any
413413
object reading it one hop through a reference, a detail object's
414414
`requiredWhen` / `readonlyWhen` reading it on `parent`, or a rule of the parent
415415
object itself reading its own summary (a cap such as `record.line_count <= 5`,
416-
or a declared bound such as `min` / `max` / `unique` on the summary or on a
417-
stored computed field of the parent calculated from it). A rule that reads a
416+
or a declared bound such as `min` / `max` / `precision` / `unique` on the
417+
summary, a declared unique index naming it, or any stored computed field of the
418+
parent calculated from it, whose value the recompute's write judges whatever it
419+
declares). A rule that reads a
418420
formula or a stored computed field calculated from the summary reads the
419421
summary too, and so does a rule on a summary that a chain of summaries rolls
420422
the first one up into. A stored computed field of any object that reads the

‎packages/objectql/src/summary-rule-reads.ts‎

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@
3030
* `requiredWhen` / `readonlyWhen`. The recompute's own write is an update of
3131
* that row, judged by those rules: one by one they judge every intermediate
3232
* value (an atomic move A→B then B→A over a cap refuses the first), so
33-
* deferred they would judge only the last. A declared bound (`min`, `max`,
34-
* `minLength`, `maxLength`, `pattern`, `unique`, `required`) on a value that
35-
* write stores, the summary itself or a stored computed field, is judged the
36-
* same way and counts as a rule reading that value.
33+
* deferred they would judge only the last. That write also judges every
34+
* value it stores against its declaration, so the same holds without a
35+
* rule: a declared bound on the summary itself (`min`, `max`, `minLength`,
36+
* `maxLength`, `pattern`, `precision`, `unique`, `required`, or an
37+
* object-level unique index naming it) counts as reading it, and EVERY
38+
* stored computed field of its object counts as read, bound or not, since
39+
* the write computes it and judges it whatever it declares (a bound, an
40+
* option list, a value domain, or a number that is not finite).
3741
*
3842
* A stored computed field (an `expression` on a field that is not a
3943
* `formula`) that reads another row one hop is stored from what it read at its
@@ -64,11 +68,26 @@ const PARENT_ROOT = 'parent';
6468
const PARENT_PREDICATE_SLOTS = ['requiredWhen', 'readonlyWhen'] as const;
6569

6670
/**
67-
* The declared bounds a write judges on a stored value (`validateRecord`'s
68-
* `min_value` / `max_value` / `min_length` / `max_length` / pattern and
69-
* `required` checks, and a unique index).
71+
* The declared bounds a write judges on the summary's stored value
72+
* (`validateRecord`'s `min_value` / `max_value` / `min_length` / `max_length`
73+
* / `max_precision` and `required` checks, and a field-level unique index).
74+
* `pattern` / `regex` are read by no check today: an over-read, which costs
75+
* only statements. A declared index with `unique` is read beside this list.
7076
*/
71-
const WRITE_BOUND_KEYS = ['min', 'max', 'minLength', 'maxLength', 'pattern', 'regex', 'unique', 'required'] as const;
77+
const SUMMARY_BOUND_KEYS = ['min', 'max', 'minLength', 'maxLength', 'pattern', 'regex', 'precision', 'unique', 'required'] as const;
78+
79+
/** The fields an object-level declared index with `unique` names (`'global'`, `'organization'`, or a stored bare `true`). */
80+
function uniquelyIndexed(indexes: unknown): Set<string> {
81+
const out = new Set<string>();
82+
if (!Array.isArray(indexes)) return out;
83+
for (const index of indexes) {
84+
if (!index || typeof index !== 'object') continue;
85+
const { unique, fields } = index as { unique?: unknown; fields?: unknown };
86+
if (unique === undefined || unique === null || unique === false || !Array.isArray(fields)) continue;
87+
for (const field of fields) if (typeof field === 'string') out.add(field);
88+
}
89+
return out;
90+
}
7291

7392
/** The CEL roots a rule reads its own row under: the row, and the row before the write. */
7493
const OWN_ROW_ROOTS = ['record', 'previous'] as const;
@@ -115,7 +134,7 @@ export function ruleReadKey(object: string, field: string): string {
115134
}
116135

117136
/** An object definition as this module reads it. */
118-
type ObjectDef = { name?: unknown; fields?: Record<string, Record<string, unknown> | null | undefined>; validations?: unknown[] } | null | undefined;
137+
type ObjectDef = { name?: unknown; fields?: Record<string, Record<string, unknown> | null | undefined>; validations?: unknown[]; indexes?: unknown } | null | undefined;
119138

120139
/** The parts of a roll-up descriptor the chain closure reads. */
121140
export interface ChainLink {
@@ -189,15 +208,23 @@ export function summaryFieldsReadByRules(objects: Iterable<unknown>, links: Iter
189208
for (const root of OWN_ROW_ROOTS) for (const field of fieldsReadOn(source, root)) own.add(field);
190209
}
191210
}
192-
// (iii) A declared bound on a field the recompute's own write stores: the
193-
// summary itself, or a stored computed field (an `expression` on a field
194-
// that is not a `formula`). The write computes it onto the payload and
195-
// judges it (`validateRecord`, a unique index), so one by one every
196-
// intermediate value is judged; the closure below adds what it reads.
211+
// (iii) What the recompute's own write judges without a rule. It stores
212+
// the summary and every stored computed field (an `expression` on a field
213+
// that is not a `formula`), computed onto its payload, and judges each
214+
// against its declaration (`validateRecord`, a unique index), so one by
215+
// one every intermediate value is judged. The summary counts when it
216+
// declares a bound or a unique index names it; a stored computed field
217+
// counts always, since its type alone can refuse a value (an option, a
218+
// value domain, a number that is not finite). The closure below adds
219+
// what a stored computed field reads.
220+
const indexed = uniquelyIndexed(schema?.indexes);
197221
for (const [field, def] of Object.entries(fields)) {
198222
if (!def || typeof def !== 'object') continue;
199-
const stored = def.type === 'summary' || (def.type !== 'formula' && celSource(def.expression) !== undefined);
200-
if (stored && WRITE_BOUND_KEYS.some((key) => def[key] !== undefined && def[key] !== null && def[key] !== false)) own.add(field);
223+
if (def.type === 'summary') {
224+
if (indexed.has(field) || SUMMARY_BOUND_KEYS.some((key) => def[key] !== undefined && def[key] !== null && def[key] !== false)) own.add(field);
225+
} else if (def.type !== 'formula' && celSource(def.expression) !== undefined) {
226+
own.add(field);
227+
}
201228
}
202229
if (name) for (const field of own) out.add(ruleReadKey(name, field));
203230
// A stored computed field reading another row one hop (`record.inv.line_total`)

‎packages/qa/dogfood/test/rollup-grouped-recompute-33795.test.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,12 @@ const CAP_TEXT = 'An invoice holds at most two lines.';
165165
* The capped invoice and its lines. `rule` caps the count with a validation
166166
* rule; `computed` caps a stored computed field over it (`room = 2 -
167167
* line_count`, recomputed on the recompute's own write) with a rule; `bound`
168-
* declares `min: 0` on that field and has no rule at all.
168+
* declares `min: 0` on that field and has no rule at all; `option` declares
169+
* nothing: a stored select computed from the count names a value its option
170+
* list lacks past the cap, which the write refuses as it would any value.
169171
*/
170-
function capObjects(parent: string, line: string, mode: 'rule' | 'computed' | 'bound') {
171-
const viaComputed = mode !== 'rule';
172+
function capObjects(parent: string, line: string, mode: 'rule' | 'computed' | 'bound' | 'option') {
173+
const viaComputed = mode === 'computed' || mode === 'bound';
172174
return [
173175
{
174176
name: parent,
@@ -177,8 +179,15 @@ function capObjects(parent: string, line: string, mode: 'rule' | 'computed' | 'b
177179
name: { type: 'text', label: 'Name' },
178180
line_count: { type: 'summary', label: 'Lines', summaryOperations: { object: line, function: 'count' } },
179181
...(viaComputed ? { room: { type: 'number', label: 'Room', expression: '2 - record.line_count', ...(mode === 'bound' ? { min: 0 } : {}) } } : {}),
182+
...(mode === 'option' ? {
183+
fill: {
184+
type: 'select', label: 'Fill',
185+
options: [{ label: 'Open', value: 'open' }, { label: 'Full', value: 'full' }],
186+
expression: "record.line_count == null || record.line_count < 2 ? 'open' : (record.line_count == 2 ? 'full' : 'over')",
187+
},
188+
} : {}),
180189
},
181-
validations: mode === 'bound' ? [] : [{
190+
validations: mode === 'bound' || mode === 'option' ? [] : [{
182191
name: 'cap_lines', type: 'script', severity: 'error', message: CAP_TEXT,
183192
condition: viaComputed ? 'record.room != null && record.room < 0' : 'record.line_count != null && record.line_count > 2',
184193
}],
@@ -196,6 +205,7 @@ function capObjects(parent: string, line: string, mode: 'rule' | 'computed' | 'b
196205
const CAP = { parent: 'r33795_cap', line: 'r33795_cap_line' };
197206
const CCAP = { parent: 'r33795_ccap', line: 'r33795_ccap_line' };
198207
const BCAP = { parent: 'r33795_bcap', line: 'r33795_bcap_line' };
208+
const OCAP = { parent: 'r33795_ocap', line: 'r33795_ocap_line' };
199209

200210
/**
201211
* [#33795] A stored computed field of the CHILD that reads the parent's
@@ -281,6 +291,7 @@ const FORMULA_OBJECTS = [
281291
const OBJECTS = [
282292
...rollupPair(0), ...rollupPair(1), ...rollupPair(2), ...KIND_OBJECTS, ...RULE_OBJECTS,
283293
...capObjects(CAP.parent, CAP.line, 'rule'), ...capObjects(CCAP.parent, CCAP.line, 'computed'), ...capObjects(BCAP.parent, BCAP.line, 'bound'),
294+
...capObjects(OCAP.parent, OCAP.line, 'option'),
284295
...FORMULA_OBJECTS, ...SEEN_OBJECTS,
285296
];
286297

@@ -692,6 +703,8 @@ const PARITY_CASES: Array<{ name: string; make: ParityCase; live: (answer: any,
692703
{ name: '(ii) a line readonlyWhen reads the header computed field over its count on parent', make: roomCase, live: (_answer, state) => state.lines[1].note === null, coeLive: true },
693704
// The same cap spelled as a declared bound (`min: 0` on the stored `room`), no rule.
694705
{ name: '(iii) a declared bound on the parent stored computed field over its count', make: capCase(BCAP), live: refused },
706+
// No declaration at all: a stored select computed from the count, whose option list lacks the value past the cap.
707+
{ name: '(iii) a stored computed field over its count with no bound, refused by its own option list', make: capCase(OCAP), live: refused },
695708
// Not a rule: a child's stored field computed from the parent's summary.
696709
{ name: 'a child stored computed field reads the owner count one hop', make: rankCase, live: (_answer, state) => state.ranks[0] !== state.ranks[1], coeLive: true },
697710
];

‎packages/spec/src/contracts/objectql-engine.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,11 @@ export interface IObjectQLEngine extends IDataEngine, QueryEvaluationGate {
665665
* rule reads is not deferred (ruling 6928862195). A validation rule of any
666666
* object reading it one hop through a reference, a detail object's
667667
* `requiredWhen` / `readonlyWhen` reading it on `parent`, a rule of the
668-
* summary's own object reading it on its own row, or a declared bound
669-
* (`min`, `max`, `unique`, …) on the summary or on a stored computed field
670-
* of that object calculated from it, each counts, whether it reads the
668+
* summary's own object reading it on its own row, a declared bound
669+
* (`min`, `max`, `precision`, `unique`, …) or unique index on the summary,
670+
* or any stored computed field of that object calculated from it (the
671+
* recompute's write judges its value whatever it declares), each counts,
672+
* whether it reads the
671673
* summary directly, through a formula or stored computed field calculated
672674
* from it, or up a chain of summaries; so does a stored computed field of
673675
* any object that reads it one hop, since it stores what it read. Any of

0 commit comments

Comments
 (0)