@pmate/chat-sdk 0.13.0-profile.6 → 0.13.0-profile.7

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.
@@ -81,7 +81,7 @@ export class ProfileDatabase extends Dexie {
81
81
  throw Error("PROFILE_CURSOR_REGRESSION");
82
82
  const changed = [batch.threads, batch.windows, batch.messages, batch.outbox, batch.deleteMessages, batch.deleteWindows, batch.deleteOutbox].some(rows => rows?.length);
83
83
  const localMigrations = batch.localMigrations ? [...new Set([...(current.localMigrations ?? []), ...batch.localMigrations])] : current.localMigrations;
84
- if (!changed && batch.committedSeq === current.committedSeq && (batch.receiptsReady === undefined || batch.receiptsReady === current.receiptsReady) && JSON.stringify(localMigrations) === JSON.stringify(current.localMigrations) && JSON.stringify(batch.revokedTeamIds ?? current.revokedTeamIds) === JSON.stringify(current.revokedTeamIds) && (batch.teamAccessRevision ?? current.teamAccessRevision) === current.teamAccessRevision)
84
+ if (!changed && batch.committedSeq === current.committedSeq && (batch.receiptsReady === undefined || batch.receiptsReady === current.receiptsReady) && JSON.stringify(localMigrations) === JSON.stringify(current.localMigrations) && JSON.stringify(batch.revokedTeamIds ?? current.revokedTeamIds) === JSON.stringify(current.revokedTeamIds) && (batch.teamAccessRevision ?? current.teamAccessRevision) === current.teamAccessRevision && (batch.windowStart ?? current.windowStart) === current.windowStart)
85
85
  return current;
86
86
  if (batch.messages?.length)
87
87
  await this.messages.bulkPut(batch.messages);
@@ -99,7 +99,7 @@ export class ProfileDatabase extends Dexie {
99
99
  await this.outbox.bulkDelete(batch.deleteOutbox);
100
100
  if (!Number.isSafeInteger(current.commitRevision + 1))
101
101
  throw Error("PROFILE_REVISION_EXHAUSTED");
102
- const next = { ...current, ...(batch.receiptsReady === undefined ? {} : { receiptsReady: batch.receiptsReady }), localMigrations, revokedTeamIds: batch.revokedTeamIds ?? current.revokedTeamIds, teamAccessRevision: batch.teamAccessRevision ?? current.teamAccessRevision, committedSeq: batch.committedSeq, commitRevision: current.commitRevision + 1 };
102
+ const next = { ...current, ...(batch.receiptsReady === undefined ? {} : { receiptsReady: batch.receiptsReady }), localMigrations, windowStart: batch.windowStart ?? current.windowStart, revokedTeamIds: batch.revokedTeamIds ?? current.revokedTeamIds, teamAccessRevision: batch.teamAccessRevision ?? current.teamAccessRevision, committedSeq: batch.committedSeq, commitRevision: current.commitRevision + 1 };
103
103
  await this.inboxState.put(next);
104
104
  return next;
105
105
  });
@@ -107,10 +107,18 @@ export class ProfileDatabase extends Dexie {
107
107
  /** Restore from one consistent transaction, never mix data across a switch. */
108
108
  async restore() {
109
109
  return this.transaction("r", [this.threads, this.windows, this.messages, this.inboxState, this.outbox], async () => {
110
- const current = await this.inboxState.get("current");
110
+ let current = await this.inboxState.get("current");
111
111
  const generation = current?.activeGeneration;
112
- return { current,
113
- threads: generation ? await this.threads.where("generation").equals(generation).toArray() : [],
112
+ const threads = generation ? await this.threads.where("generation").equals(generation).toArray() : [];
113
+ if (current && current.windowStart === undefined) {
114
+ // Additive migration for early Profile caches. Restoration already reads
115
+ // these records; never replace a committed range with a startup default.
116
+ const build = await this.inboxState.get(`build:${generation}`);
117
+ const inferred = build?.windowStart ?? (threads.length ? threads.reduce((start, row) => Math.max(start, row.coverage.windowStart), threads[0].coverage.windowStart) : undefined);
118
+ if (inferred !== undefined)
119
+ current = { ...current, windowStart: inferred };
120
+ }
121
+ return { current, threads,
114
122
  windows: generation ? await this.windows.where("generation").equals(generation).toArray() : [],
115
123
  messages: generation ? await this.messages.where("generation").equals(generation).toArray() : [],
116
124
  outbox: await this.outbox.toArray(),
@@ -191,7 +199,7 @@ export class ProfileDatabase extends Dexie {
191
199
  if (build.status !== "validated")
192
200
  throw Error("PROFILE_BUILD_NOT_VALIDATED");
193
201
  const next = { ...current, key: "current", activeGeneration: generation, epoch: build.epoch,
194
- committedSeq: build.committedSeq, projectionVersion: build.projectionVersion, commitRevision: current.commitRevision + 1, receiptsReady: build.receiptsReady };
202
+ committedSeq: build.committedSeq, windowStart: build.windowStart ?? current.windowStart, projectionVersion: build.projectionVersion, commitRevision: current.commitRevision + 1, receiptsReady: build.receiptsReady };
195
203
  await this.inboxState.put({ ...build, status: "active" });
196
204
  await this.inboxState.put(next);
197
205
  return next;
@@ -49,7 +49,7 @@ export class ProfileGenerationBuilder {
49
49
  committedSeq: (start - 1n).toString(), projectionVersion: 1, sourceRevision, status: "building", throughSeq: bootstrap.headSeq,
50
50
  windowStart: bootstrap.windowStart, buildRevision: 0 };
51
51
  const projection = new ProfileProjection(this.database.identity, { current: { key: "current", activeGeneration: generation,
52
- epoch: build.epoch, committedSeq: build.committedSeq, projectionVersion: 1, commitRevision: 0 }, threads: [], windows: [], messages: [] }, bootstrap.windowStart);
52
+ epoch: build.epoch, committedSeq: build.committedSeq, windowStart: build.windowStart, projectionVersion: 1, commitRevision: 0 }, threads: [], windows: [], messages: [] }, bootstrap.windowStart);
53
53
  const prepared = projection.prepareBasis(bootstrap), lease = this.lease();
54
54
  const next = { ...build, buildRevision: prepared.current.commitRevision };
55
55
  await this.database.saveGeneration(lease.ownerId, lease.fence, next, prepared);
@@ -35,12 +35,12 @@ export class ProfileOwner {
35
35
  owner.lease = lease;
36
36
  try {
37
37
  await options.database.initialize(ownerId, lease.fence, { activeGeneration: crypto.randomUUID(), epoch: options.epoch,
38
- committedSeq: "0", projectionVersion: 1 });
38
+ committedSeq: "0", projectionVersion: 1, windowStart: options.windowStart });
39
39
  const restored = await options.database.restore();
40
40
  if (!restored.current || restored.current.epoch !== options.epoch)
41
41
  throw Error("PROFILE_EPOCH_REBUILD_REQUIRED");
42
42
  const snapshot = { ...restored, current: restored.current };
43
- owner.projection = new ProfileProjection(options.database.identity, snapshot, options.windowStart);
43
+ owner.projection = new ProfileProjection(options.database.identity, snapshot, restored.current.windowStart ?? options.windowStart);
44
44
  owner.builder = new ProfileGenerationBuilder(options.database, () => owner.lease);
45
45
  owner.memory.restore(snapshot);
46
46
  owner.queue = new ProfileCommitQueue({ commit: events => owner.commit(events), windowMs: options.windowMs,
@@ -218,7 +218,7 @@ export class ProfileOwner {
218
218
  const prepared = this.projection.prepare(events);
219
219
  const committed = await this.options.database.commit({ ...prepared, ownerId: this.lease.ownerId, fence: this.lease.fence,
220
220
  generation: this.current.activeGeneration, expectedRevision: fromRevision, epoch: this.current.epoch,
221
- committedSeq: prepared.current.committedSeq, receiptsReady: prepared.current.receiptsReady, localMigrations: prepared.current.localMigrations, revokedTeamIds: prepared.current.revokedTeamIds, teamAccessRevision: prepared.current.teamAccessRevision });
221
+ committedSeq: prepared.current.committedSeq, windowStart: prepared.current.windowStart, receiptsReady: prepared.current.receiptsReady, localMigrations: prepared.current.localMigrations, revokedTeamIds: prepared.current.revokedTeamIds, teamAccessRevision: prepared.current.teamAccessRevision });
222
222
  if (committed.commitRevision === fromRevision)
223
223
  return prepared.rejected;
224
224
  prepared.current = committed;
@@ -115,6 +115,7 @@ export type ProfileCurrentState = {
115
115
  key: "current";
116
116
  epoch: string;
117
117
  committedSeq: string;
118
+ windowStart?: number;
118
119
  activeGeneration: string;
119
120
  projectionVersion: number;
120
121
  commitRevision: number;
@@ -161,6 +162,7 @@ export type ProfileWriteBatch = {
161
162
  generation: string;
162
163
  epoch: string;
163
164
  committedSeq: string;
165
+ windowStart?: number;
164
166
  receiptsReady?: boolean;
165
167
  localMigrations?: string[];
166
168
  revokedTeamIds?: string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/chat-sdk",
3
- "version": "0.13.0-profile.6",
3
+ "version": "0.13.0-profile.7",
4
4
  "description": "Local-first chat SDK, message subscriptions, reading state, and shared chat UI primitives",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",