@pmate/chat-sdk 0.13.0-profile.8 → 0.14.0-seq.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/README.md +57 -0
- package/lib/profile.d.ts +1 -1
- package/lib/profile.js +1 -1
- package/lib/runtime/profileChat.d.ts +7 -0
- package/lib/runtime/profileChat.js +105 -20
- package/lib/runtime/profileCommitQueue.js +3 -3
- package/lib/runtime/profileCoordinator.d.ts +25 -20
- package/lib/runtime/profileCoordinator.js +146 -128
- package/lib/runtime/profileDatabase.d.ts +6 -9
- package/lib/runtime/profileDatabase.js +10 -39
- package/lib/runtime/profileGenerationBuilder.d.ts +2 -3
- package/lib/runtime/profileGenerationBuilder.js +23 -23
- package/lib/runtime/profileInboxFeed.js +3 -2
- package/lib/runtime/profileLegacyInbox.js +2 -1
- package/lib/runtime/profileMemoryStore.d.ts +9 -3
- package/lib/runtime/profileMemoryStore.js +50 -10
- package/lib/runtime/profileOutboxDispatcher.js +4 -1
- package/lib/runtime/profileOwner.d.ts +2 -7
- package/lib/runtime/profileOwner.js +18 -46
- package/lib/runtime/profileProjection.d.ts +19 -1
- package/lib/runtime/profileProjection.js +139 -18
- package/lib/runtime/profileStoreTypes.d.ts +4 -11
- package/lib/runtime/profileWindows.d.ts +2 -0
- package/lib/runtime/profileWindows.js +13 -3
- package/lib/runtime/{profileReplica.d.ts → profileWorkerClient.d.ts} +9 -26
- package/lib/runtime/{profileReplica.js → profileWorkerClient.js} +46 -97
- package/lib/runtime/profileWorkerHost.d.ts +3 -6
- package/lib/runtime/profileWorkerHost.js +23 -30
- package/lib/runtime/readSync.d.ts +1 -1
- package/lib/runtime/readSync.js +10 -6
- package/lib/runtime/sourceCursor.d.ts +6 -0
- package/lib/runtime/sourceCursor.js +7 -0
- package/package.json +2 -2
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ProfileCurrentState, ProfileMessage, ProfileThread, ProfileWindow } from "./profileStoreTypes.js";
|
|
2
2
|
import type { ChatMessage } from "../chatTypes.js";
|
|
3
|
-
export declare const PROFILE_PROTOCOL_VERSION =
|
|
4
|
-
export declare const PROFILE_PROJECTION_VERSION =
|
|
3
|
+
export declare const PROFILE_PROTOCOL_VERSION = 2;
|
|
4
|
+
export declare const PROFILE_PROJECTION_VERSION = 2;
|
|
5
5
|
export type ProfileSnapshot = {
|
|
6
6
|
current: ProfileCurrentState;
|
|
7
7
|
threads: ProfileThread[];
|
|
@@ -24,6 +24,7 @@ export type ProfileDelta = {
|
|
|
24
24
|
export declare class ProfileMemoryStore {
|
|
25
25
|
private onError;
|
|
26
26
|
private profileId;
|
|
27
|
+
private preparePresentation;
|
|
27
28
|
private windowRows;
|
|
28
29
|
private messageWindows;
|
|
29
30
|
private windowPages;
|
|
@@ -32,6 +33,7 @@ export declare class ProfileMemoryStore {
|
|
|
32
33
|
private windowListeners;
|
|
33
34
|
private windowListListeners;
|
|
34
35
|
private dirtyWindows;
|
|
36
|
+
private dirtyWindowLists;
|
|
35
37
|
private readonly emptyWindowIds;
|
|
36
38
|
private current?;
|
|
37
39
|
private entities;
|
|
@@ -47,7 +49,11 @@ export declare class ProfileMemoryStore {
|
|
|
47
49
|
private presented;
|
|
48
50
|
private readingIndices;
|
|
49
51
|
private presentedPages;
|
|
50
|
-
constructor(onError?: (error: unknown) => void, profileId?: string);
|
|
52
|
+
constructor(onError?: (error: unknown) => void, profileId?: string, preparePresentation?: boolean);
|
|
53
|
+
/** Release a disposed Profile without mutating snapshots held by React.
|
|
54
|
+
* Worker replacement deliberately keeps this store alive; only final close
|
|
55
|
+
* may discard its entities and subscriptions. */
|
|
56
|
+
dispose(): void;
|
|
51
57
|
private notify;
|
|
52
58
|
getStatus(): {
|
|
53
59
|
phase: "ready" | "restoring";
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { ProfileWindowDirectory } from "./profileWindows.js";
|
|
2
2
|
import { ProfileReadingIndex } from "./profileReadingIndex.js";
|
|
3
3
|
import { projectMessageContent } from "../messageView.js";
|
|
4
|
-
export const PROFILE_PROTOCOL_VERSION =
|
|
5
|
-
export const PROFILE_PROJECTION_VERSION =
|
|
4
|
+
export const PROFILE_PROTOCOL_VERSION = 2;
|
|
5
|
+
export const PROFILE_PROJECTION_VERSION = 2;
|
|
6
6
|
/** Storage-free consumer shared by the owner, page adapters and WebView replicas.
|
|
7
7
|
* Only committed snapshots/deltas enter here; reads cannot initiate I/O. */
|
|
8
8
|
export class ProfileMemoryStore {
|
|
9
9
|
onError;
|
|
10
10
|
profileId;
|
|
11
|
+
preparePresentation;
|
|
11
12
|
windowRows = new Map();
|
|
12
13
|
messageWindows = new Map();
|
|
13
14
|
windowPages = new Map();
|
|
@@ -16,6 +17,7 @@ export class ProfileMemoryStore {
|
|
|
16
17
|
windowListeners = new Map();
|
|
17
18
|
windowListListeners = new Map();
|
|
18
19
|
dirtyWindows = new Set();
|
|
20
|
+
dirtyWindowLists = new Set();
|
|
19
21
|
emptyWindowIds = [];
|
|
20
22
|
current;
|
|
21
23
|
entities = new Map();
|
|
@@ -31,9 +33,38 @@ export class ProfileMemoryStore {
|
|
|
31
33
|
presented = new Map();
|
|
32
34
|
readingIndices = new Map();
|
|
33
35
|
presentedPages = new WeakMap();
|
|
34
|
-
constructor(onError = () => { }, profileId = "") {
|
|
36
|
+
constructor(onError = () => { }, profileId = "", preparePresentation = true) {
|
|
35
37
|
this.onError = onError;
|
|
36
38
|
this.profileId = profileId;
|
|
39
|
+
this.preparePresentation = preparePresentation;
|
|
40
|
+
}
|
|
41
|
+
/** Release a disposed Profile without mutating snapshots held by React.
|
|
42
|
+
* Worker replacement deliberately keeps this store alive; only final close
|
|
43
|
+
* may discard its entities and subscriptions. */
|
|
44
|
+
dispose() {
|
|
45
|
+
this.windowRows.clear();
|
|
46
|
+
this.messageWindows.clear();
|
|
47
|
+
this.windowPages.clear();
|
|
48
|
+
this.windowAccess.clear();
|
|
49
|
+
this.windowIds.clear();
|
|
50
|
+
this.windowListeners.clear();
|
|
51
|
+
this.windowListListeners.clear();
|
|
52
|
+
this.dirtyWindows.clear();
|
|
53
|
+
this.dirtyWindowLists.clear();
|
|
54
|
+
this.entities.clear();
|
|
55
|
+
this.threads.clear();
|
|
56
|
+
this.directories.clear();
|
|
57
|
+
this.listeners.clear();
|
|
58
|
+
this.listListeners.clear();
|
|
59
|
+
this.messageListeners.clear();
|
|
60
|
+
this.pageCache.clear();
|
|
61
|
+
this.epochs.clear();
|
|
62
|
+
this.presented.clear();
|
|
63
|
+
this.readingIndices.clear();
|
|
64
|
+
this.presentedPages = new WeakMap();
|
|
65
|
+
this.list = [];
|
|
66
|
+
this.current = undefined;
|
|
67
|
+
this.phase = "restoring";
|
|
37
68
|
}
|
|
38
69
|
notify(listener) { try {
|
|
39
70
|
listener();
|
|
@@ -73,6 +104,9 @@ export class ProfileMemoryStore {
|
|
|
73
104
|
windowKey(threadId, windowId) { return JSON.stringify([threadId, windowId]); }
|
|
74
105
|
installWindow(window) {
|
|
75
106
|
const key = this.windowKey(window.threadId, window.windowId);
|
|
107
|
+
const previous = this.windowRows.get(key);
|
|
108
|
+
if (!previous || (previous.count === 0) !== (window.count === 0))
|
|
109
|
+
this.dirtyWindowLists.add(window.threadId);
|
|
76
110
|
for (const ref of this.windowRows.get(key)?.refs ?? [])
|
|
77
111
|
if (this.messageWindows.get(ref.messageId) === key)
|
|
78
112
|
this.messageWindows.delete(ref.messageId);
|
|
@@ -131,7 +165,7 @@ export class ProfileMemoryStore {
|
|
|
131
165
|
}
|
|
132
166
|
restore(snapshot) {
|
|
133
167
|
const generation = snapshot.current.activeGeneration;
|
|
134
|
-
if (snapshot.current.projectionVersion
|
|
168
|
+
if (![1, PROFILE_PROJECTION_VERSION].includes(snapshot.current.projectionVersion))
|
|
135
169
|
throw Error("PROFILE_PROJECTION_INCOMPATIBLE");
|
|
136
170
|
for (const row of [...snapshot.threads, ...snapshot.windows, ...snapshot.messages])
|
|
137
171
|
if (row.generation !== generation)
|
|
@@ -174,7 +208,7 @@ export class ProfileMemoryStore {
|
|
|
174
208
|
this.prepareMessageView(row);
|
|
175
209
|
}
|
|
176
210
|
this.current = snapshot.current;
|
|
177
|
-
this.phase = snapshot.current.epoch === "uninitialized" ? "restoring" : "ready";
|
|
211
|
+
this.phase = snapshot.current.epoch === "uninitialized" || snapshot.current.projectionVersion !== PROFILE_PROJECTION_VERSION ? "restoring" : "ready";
|
|
178
212
|
this.pageCache.clear();
|
|
179
213
|
this.epochs.clear();
|
|
180
214
|
this.publish(affected, true);
|
|
@@ -291,6 +325,8 @@ export class ProfileMemoryStore {
|
|
|
291
325
|
this.readingIndices.set(message.threadId, index);
|
|
292
326
|
}
|
|
293
327
|
prepareMessageView(message) {
|
|
328
|
+
if (!this.preparePresentation)
|
|
329
|
+
return;
|
|
294
330
|
const thread = this.threads.get(message.threadId);
|
|
295
331
|
const source = thread?.readingSources.find(row => row.sourceThreadId === message.sourceThreadId);
|
|
296
332
|
const mine = message.from === this.profileId;
|
|
@@ -323,12 +359,16 @@ export class ProfileMemoryStore {
|
|
|
323
359
|
for (const threadId of affected) {
|
|
324
360
|
const blocks = this.directories.get(threadId)?.blocks ?? [];
|
|
325
361
|
const previous = this.windowIds.get(threadId);
|
|
326
|
-
//
|
|
327
|
-
//
|
|
328
|
-
if (!previous ||
|
|
329
|
-
|
|
330
|
-
|
|
362
|
+
// Durable empty blocks keep their IDs and routing boundaries, but are
|
|
363
|
+
// omitted from the display directory. Entity-only updates keep its array.
|
|
364
|
+
if (!previous || this.dirtyWindowLists.has(threadId)) {
|
|
365
|
+
const ids = blocks.filter(window => window.count > 0).map(window => window.windowId);
|
|
366
|
+
if (!previous || ids.length !== previous.length || ids.some((id, index) => id !== previous[index])) {
|
|
367
|
+
this.windowIds.set(threadId, ids);
|
|
368
|
+
changedLists.add(threadId);
|
|
369
|
+
}
|
|
331
370
|
}
|
|
371
|
+
this.dirtyWindowLists.delete(threadId);
|
|
332
372
|
const available = this.isThreadAvailable(threadId);
|
|
333
373
|
if (this.windowAccess.get(threadId) !== available) {
|
|
334
374
|
this.windowAccess.set(threadId, available);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { sourceCursor } from "./sourceCursor.js";
|
|
1
2
|
import { voiceUploader } from "./voiceUpload.js";
|
|
2
3
|
import { profileDiscussionId } from "./profileProjection.js";
|
|
3
4
|
/** Reads committed commands from owner memory. All status/ACK transitions return
|
|
@@ -128,7 +129,9 @@ export class ProfileOutboxDispatcher {
|
|
|
128
129
|
if (!response.ok)
|
|
129
130
|
throw Error(`READ_SYNC_HTTP_${response.status}`);
|
|
130
131
|
const result = await response.json();
|
|
131
|
-
if (result.success !== true || result.data?.operationId !== command.commandId || result.data.accepted !== true
|
|
132
|
+
if (result.success !== true || result.data?.operationId !== command.commandId || result.data.accepted !== true)
|
|
133
|
+
throw Error("INVALID_READING_RECEIPT");
|
|
134
|
+
if (sourceCursor(result.data).seq === "0")
|
|
132
135
|
throw Error("INVALID_READING_RECEIPT");
|
|
133
136
|
}
|
|
134
137
|
async close() {
|
|
@@ -7,13 +7,12 @@ export type ProfileOwnerOptions = {
|
|
|
7
7
|
database: ProfileDatabase;
|
|
8
8
|
epoch: string;
|
|
9
9
|
windowStart: number;
|
|
10
|
-
ownerId?: string;
|
|
11
|
-
/** Set only while the Worker holds the matching exclusive browser lock. */
|
|
12
|
-
writerLock?: string;
|
|
13
10
|
onCommitted?: (delta: ProfileDelta) => void;
|
|
14
11
|
onError?: (error: unknown) => void;
|
|
15
12
|
windowMs?: number;
|
|
16
13
|
readingWindowMs?: number;
|
|
14
|
+
/** Worker authority needs raw committed rows; its UI consumer prepares presentation. */
|
|
15
|
+
preparePresentation?: boolean;
|
|
17
16
|
retryMs?: number;
|
|
18
17
|
};
|
|
19
18
|
/** The durable owner publishes only after commit. Network requests belong to
|
|
@@ -24,13 +23,10 @@ export declare class ProfileOwner {
|
|
|
24
23
|
private projection;
|
|
25
24
|
private queue;
|
|
26
25
|
private builder;
|
|
27
|
-
private lease;
|
|
28
|
-
private timer?;
|
|
29
26
|
private cleanupTimer?;
|
|
30
27
|
private cleanupMore;
|
|
31
28
|
private cleanupTask?;
|
|
32
29
|
private cleanupStopping;
|
|
33
|
-
private lost;
|
|
34
30
|
private closed;
|
|
35
31
|
private committedListeners;
|
|
36
32
|
private synchronizer;
|
|
@@ -62,7 +58,6 @@ export declare class ProfileOwner {
|
|
|
62
58
|
private scheduleCleanup;
|
|
63
59
|
private materializeReadingIntents;
|
|
64
60
|
private report;
|
|
65
|
-
private renew;
|
|
66
61
|
private notify;
|
|
67
62
|
private commit;
|
|
68
63
|
close(): Promise<void>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ProfileCommitQueue } from "./profileCommitQueue.js";
|
|
2
2
|
import { ProfileDatabase } from "./profileDatabase.js";
|
|
3
|
-
import { ProfileMemoryStore, PROFILE_PROTOCOL_VERSION } from "./profileMemoryStore.js";
|
|
3
|
+
import { ProfileMemoryStore, PROFILE_PROTOCOL_VERSION, PROFILE_PROJECTION_VERSION } from "./profileMemoryStore.js";
|
|
4
4
|
import { ProfileProjection } from "./profileProjection.js";
|
|
5
5
|
import { ProfileGenerationBuilder } from "./profileGenerationBuilder.js";
|
|
6
6
|
/** The durable owner publishes only after commit. Network requests belong to
|
|
@@ -11,54 +11,44 @@ export class ProfileOwner {
|
|
|
11
11
|
projection;
|
|
12
12
|
queue;
|
|
13
13
|
builder;
|
|
14
|
-
lease;
|
|
15
|
-
timer;
|
|
16
14
|
cleanupTimer;
|
|
17
15
|
cleanupMore = false;
|
|
18
16
|
cleanupTask;
|
|
19
17
|
cleanupStopping = false;
|
|
20
|
-
lost = false;
|
|
21
18
|
closed = false;
|
|
22
19
|
committedListeners = new Set();
|
|
23
20
|
synchronizer = () => Promise.resolve();
|
|
24
21
|
credentialUpdater = () => { throw Error("PROFILE_NETWORK_UNAVAILABLE"); };
|
|
25
22
|
constructor(options) {
|
|
26
23
|
this.options = options;
|
|
27
|
-
this.memory = new ProfileMemoryStore(options.onError, options.database.identity.profileId);
|
|
24
|
+
this.memory = new ProfileMemoryStore(options.onError, options.database.identity.profileId, options.preparePresentation);
|
|
28
25
|
}
|
|
29
26
|
static async open(options) {
|
|
30
27
|
const owner = new ProfileOwner(options);
|
|
31
|
-
const ownerId = options.ownerId ?? crypto.randomUUID();
|
|
32
|
-
const lease = await options.database.claimWriter(ownerId, undefined, undefined, options.writerLock);
|
|
33
|
-
if (!lease)
|
|
34
|
-
throw Error("PROFILE_OWNER_UNAVAILABLE");
|
|
35
|
-
owner.lease = lease;
|
|
36
28
|
try {
|
|
37
|
-
await options.database.initialize(
|
|
38
|
-
committedSeq: "0", projectionVersion:
|
|
29
|
+
await options.database.initialize({ activeGeneration: crypto.randomUUID(), epoch: options.epoch,
|
|
30
|
+
committedSeq: "0", projectionVersion: PROFILE_PROJECTION_VERSION, windowStart: options.windowStart });
|
|
39
31
|
const restored = await options.database.restore();
|
|
40
32
|
if (!restored.current || restored.current.epoch !== options.epoch)
|
|
41
33
|
throw Error("PROFILE_EPOCH_REBUILD_REQUIRED");
|
|
42
34
|
const snapshot = { ...restored, current: restored.current };
|
|
43
35
|
owner.projection = new ProfileProjection(options.database.identity, snapshot, restored.current.windowStart ?? options.windowStart);
|
|
44
|
-
owner.builder = new ProfileGenerationBuilder(options.database
|
|
36
|
+
owner.builder = new ProfileGenerationBuilder(options.database);
|
|
45
37
|
owner.memory.restore(snapshot);
|
|
46
38
|
owner.queue = new ProfileCommitQueue({ commit: events => owner.commit(events), windowMs: options.windowMs,
|
|
47
39
|
windowMsForEvent: event => "type" in event && event.type === "observe" && event.policy === "visible" ? options.readingWindowMs ?? 500 : options.windowMs ?? 200,
|
|
48
40
|
retryMs: options.retryMs, onError: error => owner.report(error) });
|
|
49
|
-
owner.timer = setInterval(() => { void owner.renew(); }, 10000);
|
|
50
41
|
await owner.materializeReadingIntents();
|
|
51
42
|
owner.scheduleCleanup();
|
|
52
43
|
return owner;
|
|
53
44
|
}
|
|
54
45
|
catch (error) {
|
|
55
|
-
await options.database.releaseWriter(ownerId, lease.fence);
|
|
56
46
|
throw error;
|
|
57
47
|
}
|
|
58
48
|
}
|
|
59
49
|
get current() { return this.projection.current; }
|
|
60
50
|
get identity() { return this.options.database.identity; }
|
|
61
|
-
get active() { return !this.closed
|
|
51
|
+
get active() { return !this.closed; }
|
|
62
52
|
get pending() { return this.queue.pending; }
|
|
63
53
|
snapshot() { return this.projection.snapshot(); }
|
|
64
54
|
commands() { return [...this.projection.outbox.values()]; }
|
|
@@ -91,19 +81,19 @@ export class ProfileOwner {
|
|
|
91
81
|
});
|
|
92
82
|
}
|
|
93
83
|
enqueue(event, bytes, immediate = false) {
|
|
94
|
-
if (this.closed
|
|
84
|
+
if (this.closed)
|
|
95
85
|
throw Error("PROFILE_OWNER_UNAVAILABLE");
|
|
96
86
|
return this.queue.enqueue(event, bytes, immediate);
|
|
97
87
|
}
|
|
98
88
|
flush() { this.queue.flush(); }
|
|
99
89
|
async beginGeneration(bootstrap, generation = crypto.randomUUID()) {
|
|
100
|
-
if (this.closed
|
|
90
|
+
if (this.closed)
|
|
101
91
|
throw Error("PROFILE_OWNER_UNAVAILABLE");
|
|
102
92
|
await this.queue.enqueue({ ownerType: "begin", generation, bootstrap }, new TextEncoder().encode(JSON.stringify(bootstrap)).byteLength, true, true);
|
|
103
93
|
return generation;
|
|
104
94
|
}
|
|
105
95
|
async appendGeneration(generation, events) {
|
|
106
|
-
if (this.closed
|
|
96
|
+
if (this.closed)
|
|
107
97
|
throw Error("PROFILE_OWNER_UNAVAILABLE");
|
|
108
98
|
await this.queue.enqueue({ ownerType: "append", generation, events }, new TextEncoder().encode(JSON.stringify(events)).byteLength, true, true);
|
|
109
99
|
}
|
|
@@ -113,7 +103,7 @@ export class ProfileOwner {
|
|
|
113
103
|
return rows.filter((row) => row.key.startsWith("build:") && "status" in row && ["building", "validated"].includes(row.status));
|
|
114
104
|
}
|
|
115
105
|
async activateGeneration(generation) {
|
|
116
|
-
if (this.closed
|
|
106
|
+
if (this.closed)
|
|
117
107
|
throw Error("PROFILE_OWNER_UNAVAILABLE");
|
|
118
108
|
await this.queue.enqueue({ ownerType: "activate", generation }, 128, true, true);
|
|
119
109
|
await this.materializeReadingIntents();
|
|
@@ -141,8 +131,6 @@ export class ProfileOwner {
|
|
|
141
131
|
} };
|
|
142
132
|
for (const thread of this.projection.threads.values())
|
|
143
133
|
for (const [messageId, intents] of Object.entries(thread.internal?.pendingReadingIntents ?? {})) {
|
|
144
|
-
if (!this.projection.messages.get(messageId)?.sourceSeq)
|
|
145
|
-
continue;
|
|
146
134
|
for (const intent of intents) {
|
|
147
135
|
if (pending.length >= 200)
|
|
148
136
|
await flush();
|
|
@@ -152,29 +140,11 @@ export class ProfileOwner {
|
|
|
152
140
|
await flush();
|
|
153
141
|
}
|
|
154
142
|
report(error) {
|
|
155
|
-
if (error?.message === "PROFILE_WRITER_FENCED")
|
|
156
|
-
this.lost = true;
|
|
157
143
|
try {
|
|
158
144
|
this.options.onError?.(error);
|
|
159
145
|
}
|
|
160
146
|
catch { /* Reporting cannot break the commit loop. */ }
|
|
161
147
|
}
|
|
162
|
-
async renew() {
|
|
163
|
-
if (this.closed || this.lost)
|
|
164
|
-
return;
|
|
165
|
-
try {
|
|
166
|
-
const next = await this.options.database.claimWriter(this.lease.ownerId, undefined, undefined, this.lease.writerLock);
|
|
167
|
-
if (!next || next.fence !== this.lease.fence) {
|
|
168
|
-
this.lost = true;
|
|
169
|
-
this.report(Error("PROFILE_WRITER_FENCED"));
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
this.lease = next;
|
|
173
|
-
}
|
|
174
|
-
catch (error) {
|
|
175
|
-
this.report(error);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
148
|
notify(delta) {
|
|
179
149
|
try {
|
|
180
150
|
this.options.onCommitted?.(delta);
|
|
@@ -192,14 +162,12 @@ export class ProfileOwner {
|
|
|
192
162
|
}
|
|
193
163
|
}
|
|
194
164
|
async commit(events) {
|
|
195
|
-
if (this.lost)
|
|
196
|
-
throw Error("PROFILE_WRITER_FENCED");
|
|
197
165
|
const first = events[0];
|
|
198
166
|
if (first && "ownerType" in first) {
|
|
199
167
|
if (events.length !== 1)
|
|
200
168
|
throw Error("PROFILE_BARRIER_NOT_ISOLATED");
|
|
201
169
|
if (first.ownerType === "cleanup")
|
|
202
|
-
this.cleanupMore = await this.options.database.cleanupGenerations(
|
|
170
|
+
this.cleanupMore = await this.options.database.cleanupGenerations() === 200;
|
|
203
171
|
else if (first.ownerType === "begin")
|
|
204
172
|
await this.builder.begin(first.generation, first.bootstrap, this.current.commitRevision);
|
|
205
173
|
else if (first.ownerType === "append")
|
|
@@ -216,7 +184,13 @@ export class ProfileOwner {
|
|
|
216
184
|
}
|
|
217
185
|
const fromRevision = this.projection.current.commitRevision;
|
|
218
186
|
const prepared = this.projection.prepare(events);
|
|
219
|
-
|
|
187
|
+
// A replayed cursor or already-observed reading candidate must not even
|
|
188
|
+
// open a readwrite transaction. Only small sync metadata is compared here.
|
|
189
|
+
const unchanged = ![prepared.messages, prepared.windows, prepared.threads, prepared.outbox, prepared.deleteOutbox].some(rows => rows.length) &&
|
|
190
|
+
JSON.stringify({ ...prepared.current, commitRevision: fromRevision }) === JSON.stringify(this.current);
|
|
191
|
+
if (unchanged)
|
|
192
|
+
return prepared.rejected;
|
|
193
|
+
const committed = await this.options.database.commit({ ...prepared,
|
|
220
194
|
generation: this.current.activeGeneration, expectedRevision: fromRevision, epoch: this.current.epoch,
|
|
221
195
|
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
196
|
if (committed.commitRevision === fromRevision)
|
|
@@ -247,7 +221,5 @@ export class ProfileOwner {
|
|
|
247
221
|
await this.cleanupTask;
|
|
248
222
|
this.queue.close();
|
|
249
223
|
this.closed = true;
|
|
250
|
-
clearInterval(this.timer);
|
|
251
|
-
await this.options.database.releaseWriter(this.lease.ownerId, this.lease.fence);
|
|
252
224
|
}
|
|
253
225
|
}
|
|
@@ -2,10 +2,18 @@ import { type InboxEvent, type InboxMessage, type InboxBootstrap } from "./inbox
|
|
|
2
2
|
import { ProfileReadingIndex } from "./profileReadingIndex.js";
|
|
3
3
|
import { ProfileWindowDirectory } from "./profileWindows.js";
|
|
4
4
|
import type { ProfileSnapshot } from "./profileMemoryStore.js";
|
|
5
|
-
import type { ProfileCurrentState, ProfileIdentity, ProfileMessage, ProfileOutboxCommand, ProfileThread, ProfileWindow } from "./profileStoreTypes.js";
|
|
5
|
+
import type { ProfileCurrentState, ProfileIdentity, ProfileMessage, ProfileOutboxCommand, ProfileReadingSource, ProfileThread, ProfileWindow } from "./profileStoreTypes.js";
|
|
6
6
|
import type { ChatRuntimeMessage } from "./protocol.js";
|
|
7
7
|
export declare const profileDiscussionId: (parent: string, root: string) => string;
|
|
8
8
|
export type ProfileLocalOperation = {
|
|
9
|
+
type: "restoreReadingSources";
|
|
10
|
+
threadId: string;
|
|
11
|
+
sources: ProfileReadingSource[];
|
|
12
|
+
} | {
|
|
13
|
+
type: "restoreMessageReadingFacts";
|
|
14
|
+
messageId: string;
|
|
15
|
+
facts: ProfileMessage["explicitReadingFacts"];
|
|
16
|
+
} | {
|
|
9
17
|
type: "migrationCompleted";
|
|
10
18
|
id: string;
|
|
11
19
|
} | {
|
|
@@ -35,6 +43,16 @@ export type ProfileLocalOperation = {
|
|
|
35
43
|
targetId: string;
|
|
36
44
|
teamId: string;
|
|
37
45
|
title?: string;
|
|
46
|
+
} | {
|
|
47
|
+
type: "groupSnapshot";
|
|
48
|
+
threadId: string;
|
|
49
|
+
group: import("./socialReadClient.js").SocialGroup;
|
|
50
|
+
expectedControlRevision?: number;
|
|
51
|
+
expectedAccessRevision: number;
|
|
52
|
+
} | {
|
|
53
|
+
type: "groupDenied";
|
|
54
|
+
threadId: string;
|
|
55
|
+
expectedAccessRevision: number;
|
|
38
56
|
} | {
|
|
39
57
|
type: "teamAccess";
|
|
40
58
|
teamId: string;
|