fix: one field-resolution law across aggregation hooks, source.where, removeMany, and find() spellings

Four fixes from a consumer conformance report, one root disease — two
field-resolution regimes where there must be one:

- The delete/update aggregation hooks fed the engine a partial entity
  view (type/service/data/metadata only), so a reserved-field groupBy
  (subtype, visibility, ...) resolved to a nonexistent group on the way
  down: counts drifted upward forever after deletes, and updates moving
  an entity between reserved-field groups double-counted. The hooks now
  pass the full-fidelity view via entityForAggFromRawRecord (every
  reserved field top-level, mirroring the add path); the update sites
  pass the full get() view instead of a hand-rolled subset.
- Aggregation source.where resolved fields only against the custom
  metadata bag, so where on a reserved field silently matched nothing.
  The matcher now resolves each filtered field through
  resolveEntityField — the same single source of truth groupBy uses.
- removeMany() with no usable selector (bare array passed positionally,
  empty params, ids: []) resolved successfully having deleted nothing.
  All three now throw; the two legacy tests that pinned the silent
  no-op as 'graceful' now pin the refusal.
- find() where keys accept both spellings: a metadata.-prefixed key
  falls back to its flattened spelling when the prefixed one is not
  indexed (metadata is flattened at index time). A literal nested custom
  key named metadata still wins when indexed as spelled.

Five regression pins in aggregate-reserved-fields.test.ts (4 of 5 vary
red on the unfixed code).
This commit is contained in:
David Snelling 2026-07-19 10:54:36 -07:00
parent 42037d0cd0
commit 945d92d29e
7 changed files with 303 additions and 45 deletions

View file

@ -205,10 +205,10 @@ describe('Metadata index cleanup after remove / removeMany', () => {
}
})
it('handles empty ids array gracefully', async () => {
const result = await brain.removeMany({ ids: [] })
expect(result.successful).toHaveLength(0)
expect(result.failed).toHaveLength(0)
it('refuses an empty ids array loudly (a silent no-op is not "graceful")', async () => {
// 8.8.2: an empty selector used to resolve successfully having deleted
// NOTHING — the caller believed the delete happened. Now it throws.
await expect(brain.removeMany({ ids: [] })).rejects.toThrow(/ids: \[\]/)
})
it('handles large batch (> 1 chunk) without leaving stale index entries', async () => {