Skip to content

feat(entity): allOf and anyOf, the conjunction and disjunction of invariant predicates - #1338

Merged
sebyx07 merged 4 commits into
mainfrom
fix/predicate-all-any
Nov 24, 2026
Merged

sebyx07 merged 4 commits into
mainfrom
fix/predicate-all-any

Conversation

@sebyx07

@sebyx07 sebyx07 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

What

An entity's index where or CHECK could not say two conditions at once — "both null", "at most one set": iff was the one combinator. A partial index on a is null and b is null had to be hand-written, and the drift step reported it as X_DB_INDEX_UNDECLARED.

API Renders App verdict
allOf(...predicates) (a) and (b) … SQL's and in three values: false if any is false, else unknown if any is unknown (a CHECK passes it), else true
anyOf(...predicates) (a) or (b) … SQL's or: true if any is true, else unknown if any is unknown, else false
import { allOf, anyOf, entity, invariant } from '@ultimat3/entity';

entity('slots', {
  columns: { /* … */ },
  invariants: (c) => [invariant('a_or_b_unset', anyOf(c.a.isNull(), c.b.isNull()))],
  indexes: [{ on: ['id'], where: (c) => allOf(c.a.isNull(), c.b.isNull()) }],
});
  • Two or more operands; one is X_INVARIANT_VIOLATED (the author left a condition out). A unique operand is refused as iff refuses it — the rule now lives in expr-connectives.ts, shared by all three.
  • One app-only operand (matches(fn)) makes the whole rule sql: null, never half of it; an index where refuses that by name, as before.
  • not stays absent: isNull/isNotNull are the negations a CHECK needs.

Proof

  • expr-connectives.test.ts: one corpus judged by the app and by the migration's CHECK on PGlite — agree on every row, NULL included; the SQL text; a partial index whose pg_indexes.indexdef reads WHERE ((a IS NULL) AND (b IS NULL)); an app-only operand; one operand and a unique operand refused. New file, so it failed before.
  • refuse.test.ts holds the new refusal site. bun test ./packages/entity/src: 2288 pass, 0 fail. bun run boundaries clean; scripts/public-api.ts: 0 breaks.

Docs: wiki/Entities-And-Migrations.md (the invariant table), CHANGELOG.

🤖 Generated with Claude Code

@developerz-ai

developerz-ai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

🟡 Reviewed

4 actionable comment(s) · grounded on your code

developerz-ai[bot]
developerz-ai Bot previously approved these changes Nov 24, 2026

@developerz-ai developerz-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approved.

Review summary: 10 file(s), 6 finding(s).

Critical 0 · Major 0 · Minor 6 · Nit 0

Adds allOf and anyOf to entity CHECKs and partial indexes. Dotted keys can hide operand dependencies, and the NULL documentation, parity tests, and arity repair need fixes. The tests cover SQL generation but miss two application verdict regressions.

File Findings
packages/entity/src/expr-connectives.test.ts 3 minor
packages/entity/src/expr-connectives.ts 2 minor
wiki/Entities-And-Migrations.md 1 minor

Comment thread packages/entity/src/expr-connectives.ts Resolved
Comment thread packages/entity/src/expr-connectives.ts Resolved
Comment thread wiki/Entities-And-Migrations.md Resolved
@developerz-ai
developerz-ai Bot dismissed their stale review November 24, 2026 08:50

Superseded: a newer review of this pull request, on commit 4ed04ae, replaces this approval.

@developerz-ai developerz-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approved.

Review summary: 3 file(s), 4 finding(s).

Critical 0 · Major 0 · Minor 4 · Nit 0

Adds allOf and anyOf for entity CHECKs and partial indexes. The follow-up leaves a misleading NULL guarantee, unusable arity fixes, and gaps in the parity tests. The parity tests miss false taking precedence over unknown in allOf and can pass without the SQL allOf probe constraint.

File Findings
packages/entity/src/expr-connectives.test.ts 2 minor
packages/entity/src/expr-connectives.ts 1 minor
wiki/Entities-And-Migrations.md 1 minor
4 non-blocking finding(s) below major (no thread on a re-review, only blocking findings open threads)
  • packages/entity/src/expr-connectives.test.ts:65 minor · test The mixed false/NULL probe passes even if allOf returns UNKNOWN instead of FALSE or anyOf returns FALSE instead of UNKNOWN, because iff(..., false) accepts both. Add an iff probe against a true predicate for this row to distinguish the two outcomes.
  • packages/entity/src/expr-connectives.test.ts:125 minor · test The SQL assertion stays green if the all_not_true CHECK is omitted: any_not_true already rejects every row that all_not_true rejects. Test each CHECK on its own table so its refusal is observable independently.
  • packages/entity/src/expr-connectives.ts:63 minor · bug The arity fix for allOf(c.title.isNotNull()) is prose that cannot be pasted as TypeScript, and the fix for allOf() references undefined first and second. Follow CLAUDE.md and kb:review/learnings: give a pasteable repair, with any explanation in the comment.
  • wiki/Entities-And-Migrations.md:157 minor · docs The paragraph still opens by promising that a rule over a nullable column passes NULL, but its own allOf example rejects a = NULL, b = 2. Scope the opening claim to individual comparisons and say that a composite CHECK accepts TRUE or UNKNOWN only as its final verdict.

Reviewed with gpt-6.2-sol

sebyx07 and others added 4 commits November 24, 2026 04:17
…ariant predicates

An index `where` or a CHECK over several columns at once ("both null",
"at most one set") had no form: `iff` was the one combinator, so a
partial index on `a is null and b is null` was hand-written and drew
`X_DB_INDEX_UNDECLARED` as drift.

- `allOf(...predicates)` / `anyOf(...predicates)`: two or more operands,
  SQL `(a) and (b)` / `(a) or (b)`, the app verdict in SQL's three
  values. One app-only operand makes the whole rule app-only (an index
  `where` refuses that by name); a `unique` operand or fewer than two
  is `X_INVARIANT_VIOLATED` at declaration.
- `iff`'s operand rules move to `expr-connectives.ts` and are shared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Measured with the metafile walk; each raise names its module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ue; paths keep their segments

- a row only the first allOf operand refuses; each connective inside iff
  against a decided false, so unknown passes and true is refused, app and
  CHECK alike.
- operandPaths keys a path by its whole segment list.
- the one-operand refusal names only the author's own column.
- the NULL guarantee is stated per comparison; a composite's final verdict decides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… ceilings re-measured

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sebyx07
sebyx07 force-pushed the fix/predicate-all-any branch from 4ed04ae to 1364851 Compare November 24, 2026 09:18

@developerz-ai developerz-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Approved.

Review summary: 10 file(s), 4 finding(s).

Critical 0 · Major 0 · Minor 4 · Nit 0

Adds three-valued allOf and anyOf predicates for entity checks and partial indexes. The database parity test can miss a dropped allOf check, and the arity diagnostic can suggest an unbound call.

File Findings
packages/entity/src/expr-connectives.test.ts 3 minor
packages/entity/src/expr-connectives.ts 1 minor
4 non-blocking finding(s) below major (no thread on a re-review, only blocking findings open threads)
  • packages/entity/src/expr-connectives.test.ts:29 minor · test Every allOf fixture has two operands, so an evaluator that ignores the third operand passes these tests. Add a three-operand conjunction with a row where only the third predicate is false and assert that both holds and the CHECK reject it.
  • packages/entity/src/expr-connectives.test.ts:111 minor · test On both mixed rows where allOf is UNKNOWN, the anyOf CHECK already rejects the insert, so rendering allOf as TRUE still passes the SQL test, and iff against FALSE alone cannot distinguish UNKNOWN from FALSE in the app. Test each connective independently with iff against both TRUE and FALS…
  • packages/entity/src/expr-connectives.test.ts:125 minor · test The database probe still passes if the migration omits all_not_true: every row that check should reject is also rejected by any_not_true. Put the two checks on separate probe tables and compare each insert outcome to its own expected verdict.
  • packages/entity/src/expr-connectives.ts:63 minor · bug With allOf(), the fix starts with allOf(first, second), which fails when pasted because neither name is bound. Branch on operand count and lead with the actual edit: remove the empty declaration or unwrap the existing predicate for a singleton.

Reviewed with gpt-6.2-sol

@sebyx07
sebyx07 merged commit d24a9de into main Nov 24, 2026
43 checks passed
@sebyx07
sebyx07 deleted the fix/predicate-all-any branch November 24, 2026 10:02
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