@ultimat3/realtime 31.38.1 → 31.40.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.
package/CLAUDE.md CHANGED
@@ -111,8 +111,9 @@ A rule marked `†` has its full text in [`docs/history/realtime-rules.md`](../.
111
111
  re-lookup (`live-definition-input.test.ts`). A cold window is staled only by a table it reads.
112
112
  - **A WINDOW is per TENANT** (`windowId(queryHash, tenant)`, `live-tenant.ts`); the read runs as a
113
113
  service actor carrying ONLY that org, never the subscriber. †
114
- - **A change's tenant is its entity's `$tenantColumn`, on every producer** (`change-tenant.ts`),
115
- never a property assumed to be `orgId`; a window lists its tenanted tables (`shape.tenantTables`)
114
+ - **A change's tenant is its entity's `$tenantColumn`, on every producer and in `normalize`** (`change-tenant.ts`, `replicator-tenant.test.ts`),
115
+ never a property assumed to be `orgId`; a window's org is its TENANT alone, `null` untenanted —
116
+ never the input's `orgId` (`query-window-tenant.test.ts`; `orgIdOf` is deprecated, unused); a window lists its tenanted tables (`shape.tenantTables`)
116
117
  and `canAffect` refuses a tenant-less insert/update on one for an org-scoped window; a
117
118
  tenant-less DELETE (key-only image) passes, and `bridgeChange` keeps a delete only for an id
118
119
  the window holds (`live-tenant-column.test.ts`, `tenant-less-delete.test.ts`).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/realtime",
3
- "version": "31.38.1",
3
+ "version": "31.40.0",
4
4
  "description": "Three-tier realtime: channels, live queries, local-first sync — one protocol, one mutator shape",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -52,10 +52,10 @@
52
52
  "test": "bun test"
53
53
  },
54
54
  "dependencies": {
55
- "@ultimat3/core": "31.38.1",
56
- "@ultimat3/entity": "31.38.1",
57
- "@ultimat3/policy": "31.38.1",
58
- "@ultimat3/query": "31.38.1",
55
+ "@ultimat3/core": "31.40.0",
56
+ "@ultimat3/entity": "31.40.0",
57
+ "@ultimat3/policy": "31.40.0",
58
+ "@ultimat3/query": "31.40.0",
59
59
  "nats": "2.29.3"
60
60
  }
61
61
  }
@@ -43,12 +43,13 @@ export interface SnapshotArgs {
43
43
 
44
44
  export interface LiveQueryDefinition<R extends LiveRow = LiveRow> {
45
45
  readonly name: string;
46
- /** Dependency set for the pre-filter. `x verify` rejects a `live: true` query without one. */
46
+ /** Dependency set for the pre-filter: required by this type; `@ultimat3/query` refuses an empty `subscribes:`. */
47
47
  readonly entities: readonly string[];
48
48
  /** Read set. Lets the pre-filter skip updates that touch no column this query reads. */
49
49
  readonly columns?: readonly string[];
50
50
  /**
51
- * Bounded read (`orderBy` + `limit`, enforced by `x verify`), unfiltered by policy — and read
51
+ * Bounded read (`orderBy` + `limit`; no limit is `X_QUERY_LIVE_UNBOUNDED`, refused where
52
+ * `@ultimat3/query` builds the source — `live-bounds.ts`), unfiltered by policy — and read
52
53
  * FOR `tenant`: the org of the subscribers this window serves, `null` for subscribers that carry
53
54
  * none. A window is keyed by it (`live-tenant.ts`), so one read is never two orgs' rows.
54
55
  */
@@ -128,9 +128,10 @@ export function queryEntry(
128
128
  shape: {
129
129
  qid,
130
130
  entities,
131
- // The window's own tenant first: a change in another org is dropped before any predicate
132
- // runs, whether or not the query's `where` happens to name one.
133
- orgId: tenant ?? orgIdOf(input),
131
+ // The window's own tenant, and nothing else: a change in another org is dropped before any
132
+ // predicate runs. Never an input property spelled `orgId` — the read runs under `tenant`
133
+ // alone, so filtering by anything else drops changes that read returns (`change-tenant.ts`).
134
+ orgId: tenant,
134
135
  // Resolved once per window, so `canAffect` refuses a tenant-less change on these with a
135
136
  // lookup in a short list rather than a registry walk per change.
136
137
  tenantTables: entities.filter((table) => tenantColumnOfTable(table) !== null),
@@ -387,6 +388,11 @@ async function readSnapshot(entry: QueryEntry, phase: LiveReadPhase): Promise<Sn
387
388
  }
388
389
  }
389
390
 
391
+ /**
392
+ * @deprecated Unused by the framework, and removed at the next major. A window's org is its
393
+ * tenant — pass `tenant` to `queryEntry` — never an input property spelled `orgId`: the read runs
394
+ * under the tenant alone, and a change's own tenant is its entity's `$tenantColumn`.
395
+ */
390
396
  export function orgIdOf(input: JsonValue): string | null {
391
397
  if (typeof input !== 'object' || input === null || Array.isArray(input)) return null;
392
398
  const value = input['orgId'];
@@ -2,6 +2,7 @@
2
2
  // the subject a change is published under, and the normalize step that drops the unusable and
3
3
  // hoists the tenant out of the row. Split from `replicator.ts`, which re-exports both.
4
4
 
5
+ import { tenantColumnOfTable, tenantIn } from './change-tenant';
5
6
  import type { ChangeEvent } from './changefeed';
6
7
 
7
8
  export const CHANGE_SUBJECT_PREFIX = 'x.change';
@@ -14,7 +15,12 @@ export function changeSubject(change: ChangeEvent): string {
14
15
  return `${CHANGE_SUBJECT_PREFIX}.${change.table}.${change.orgId ?? '_'}`;
15
16
  }
16
17
 
17
- /** Drops events the pipeline cannot use and hoists the tenant id out of the row. */
18
+ /**
19
+ * Drops events the pipeline cannot use and hoists the tenant out of the row — from the column the
20
+ * table's ENTITY declares (`change-tenant.ts`), never a property assumed to be `orgId`. It read
21
+ * `row.orgId`, so a key-only DELETE on a `tenant: 'workspaceId'` table whose key holds an `orgId`
22
+ * column was published as that org's change, and the real tenant's windows dropped it.
23
+ */
18
24
  export function normalize(change: ChangeEvent): ChangeEvent | null {
19
25
  // The one change that carries no row by design; nothing to hoist a tenant out of.
20
26
  if (change.op === 'truncate') return change;
@@ -23,6 +29,6 @@ export function normalize(change: ChangeEvent): ChangeEvent | null {
23
29
  if (change.op === 'insert' && change.after === null) return null;
24
30
  if (change.op === 'delete' && change.before === null) return null;
25
31
  if (change.orgId !== null) return change;
26
- const orgId = row['orgId'];
27
- return typeof orgId === 'string' ? { ...change, orgId } : change;
32
+ const orgId = tenantIn(tenantColumnOfTable(change.table), row);
33
+ return orgId === null ? change : { ...change, orgId };
28
34
  }
package/src/server.ts CHANGED
@@ -211,6 +211,7 @@ export {
211
211
  DEFAULT_READ_DEADLINE_MS,
212
212
  type EntryOptions,
213
213
  fillWindow,
214
+ // Deprecated and unused by the framework; kept for semver, removed at the next major.
214
215
  orgIdOf,
215
216
  type PendingRead,
216
217
  type QueryEntry,