agl 43.13.0 → 44.0.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.
Files changed (99) hide show
  1. package/changelog.md +28 -0
  2. package/dist_serve/bundle.js +4473 -4235
  3. package/dist_serve/bundle.js.map +2 -2
  4. package/dist_serve/bundle.js.third-party-notices.json +29 -29
  5. package/dist_serve/bundle.js.third-party-notices.txt +29 -29
  6. package/dist_ts/00_commitinfo_data.js +2 -2
  7. package/dist_ts/aglapi/aglapisurface.document.js +37 -4
  8. package/dist_ts/classes.authstore.d.ts +60 -3
  9. package/dist_ts/classes.authstore.js +161 -13
  10. package/dist_ts/classes.controller.d.ts +63 -0
  11. package/dist_ts/classes.controller.js +502 -43
  12. package/dist_ts/classes.embeddeddb.js +7 -2
  13. package/dist_ts/classes.flexmodels.private.d.ts +39 -0
  14. package/dist_ts/classes.flexmodels.private.js +127 -1
  15. package/dist_ts/classes.flexprojectpreparation.d.ts +21 -0
  16. package/dist_ts/classes.flexprojectpreparation.js +69 -0
  17. package/dist_ts/classes.flexpromptqueue.d.ts +21 -1
  18. package/dist_ts/classes.flexpromptqueue.js +67 -7
  19. package/dist_ts/classes.flexrequestqueue.d.ts +11 -0
  20. package/dist_ts/classes.flexrequestqueue.js +40 -0
  21. package/dist_ts/classes.flexservice.d.ts +46 -14
  22. package/dist_ts/classes.flexservice.js +179 -55
  23. package/dist_ts/classes.flexstore.d.ts +68 -3
  24. package/dist_ts/classes.flexstore.js +277 -11
  25. package/dist_ts/classes.flexsubmissionmodels.d.ts +58 -0
  26. package/dist_ts/classes.flexsubmissionmodels.js +162 -0
  27. package/dist_ts/classes.flexsubmissionstore.d.ts +70 -0
  28. package/dist_ts/classes.flexsubmissionstore.js +323 -0
  29. package/dist_ts/classes.flexsupervisor.d.ts +10 -0
  30. package/dist_ts/classes.flexsupervisor.js +27 -1
  31. package/dist_ts/classes.harnessoperationmanager.d.ts +31 -0
  32. package/dist_ts/classes.harnessoperationmanager.js +69 -7
  33. package/dist_ts/classes.mcpclient.js +4 -3
  34. package/dist_ts/classes.mcpcontrollerhost.js +3 -2
  35. package/dist_ts/classes.pendingpromptstore.d.ts +11 -3
  36. package/dist_ts/classes.pendingpromptstore.js +66 -10
  37. package/dist_ts/classes.sessionidentityservice.d.ts +18 -1
  38. package/dist_ts/classes.sessionidentityservice.js +34 -4
  39. package/dist_ts/classes.ticketstore.d.ts +5 -3
  40. package/dist_ts/classes.ticketstore.js +7 -2
  41. package/dist_ts/classes.validation.d.ts +3 -0
  42. package/dist_ts/classes.validation.js +50 -1
  43. package/dist_ts/functions.deviceapi.js +7 -3
  44. package/dist_ts/interfaces.auth.d.ts +3 -1
  45. package/dist_ts/interfaces.auth.js +1 -1
  46. package/dist_ts/interfaces.flexipc.d.ts +29 -5
  47. package/dist_ts/interfaces.flexipc.js +13 -4
  48. package/dist_ts_authswitch/classes.authoritydatabase.d.ts +1 -0
  49. package/dist_ts_authswitch/classes.authoritydatabase.js +17 -5
  50. package/dist_ts_interfaces/flexadmission.d.ts +69 -0
  51. package/dist_ts_interfaces/flexadmission.js +2 -0
  52. package/dist_ts_interfaces/index.d.ts +1 -0
  53. package/dist_ts_interfaces/index.js +2 -1
  54. package/dist_ts_interfaces/interfaces.d.ts +10 -1
  55. package/dist_ts_interfaces/interfaces.js +2 -2
  56. package/dist_ts_interfaces/requests.d.ts +2 -1
  57. package/dist_ts_interfaces/requests.js +1 -1
  58. package/dist_ts_interfaces/tickets.d.ts +9 -1
  59. package/dist_ts_interfaces/tickets.js +11 -2
  60. package/dist_ts_migration/v10_flexharnessv3migration.js +4 -1
  61. package/docs/nosqldb11-migration.md +59 -0
  62. package/package.json +10 -9
  63. package/readme.md +5 -1
  64. package/ts/00_commitinfo_data.ts +1 -1
  65. package/ts/aglapi/aglapisurface.document.ts +36 -3
  66. package/ts/classes.authstore.ts +179 -11
  67. package/ts/classes.controller.ts +498 -40
  68. package/ts/classes.embeddeddb.ts +6 -1
  69. package/ts/classes.flexmodels.private.ts +109 -0
  70. package/ts/classes.flexprojectpreparation.ts +69 -0
  71. package/ts/classes.flexpromptqueue.ts +69 -6
  72. package/ts/classes.flexrequestqueue.ts +47 -0
  73. package/ts/classes.flexservice.ts +187 -51
  74. package/ts/classes.flexstore.ts +352 -10
  75. package/ts/classes.flexsubmissionmodels.ts +89 -0
  76. package/ts/classes.flexsubmissionstore.ts +312 -0
  77. package/ts/classes.flexsupervisor.ts +26 -0
  78. package/ts/classes.harnessoperationmanager.ts +79 -6
  79. package/ts/classes.mcpclient.ts +3 -2
  80. package/ts/classes.mcpcontrollerhost.ts +2 -1
  81. package/ts/classes.pendingpromptstore.ts +55 -6
  82. package/ts/classes.sessionidentityservice.ts +33 -2
  83. package/ts/classes.ticketstore.ts +11 -4
  84. package/ts/classes.validation.ts +42 -0
  85. package/ts/functions.deviceapi.ts +6 -2
  86. package/ts/interfaces.auth.ts +3 -1
  87. package/ts/interfaces.flexipc.ts +38 -4
  88. package/ts_authswitch/classes.authoritydatabase.ts +19 -5
  89. package/ts_interfaces/flexadmission.ts +52 -0
  90. package/ts_interfaces/index.ts +2 -0
  91. package/ts_interfaces/interfaces.ts +10 -1
  92. package/ts_interfaces/requests.ts +3 -0
  93. package/ts_interfaces/tickets.ts +12 -2
  94. package/ts_migration/v10_flexharnessv3migration.ts +3 -0
  95. package/ts_web/00_commitinfo_data.ts +1 -1
  96. package/ts_web/classes.browsersubmissions.ts +279 -0
  97. package/ts_web/classes.sessiondraftsync.ts +64 -1
  98. package/ts_web/elements.harnesscontrollerapp.ts +340 -5
  99. package/ts_web/functions.flexsubmissions.ts +88 -0
@@ -0,0 +1,279 @@
1
+ const freezeSnapshot = <T>(valueArg: T): T => {
2
+ if (valueArg !== null && typeof valueArg === 'object') {
3
+ for (const value of Object.values(valueArg)) freezeSnapshot(value);
4
+ Object.freeze(valueArg);
5
+ }
6
+ return valueArg;
7
+ };
8
+
9
+ export type TBrowserSubmissionState = 'submitting' | 'admitted' | 'refused' | 'unknown' | 'blocked';
10
+
11
+ /** A click-time message, kept in this page's memory only until its exact durable owner appears. */
12
+ export interface IBrowserSubmission<TSnapshot, TReceipt> {
13
+ readonly id: string;
14
+ readonly scope: string;
15
+ readonly snapshot: TSnapshot;
16
+ readonly createdAt: number;
17
+ readonly state: TBrowserSubmissionState;
18
+ readonly attempted: boolean;
19
+ /** Sends refused with a transient code before this one; each was definitely not admitted. */
20
+ readonly retries: number;
21
+ readonly receipt?: TReceipt;
22
+ readonly error?: string;
23
+ }
24
+
25
+ export interface IBrowserSubmissionsOptions<TSnapshot, TReceipt> {
26
+ /** Resolves on durable admission, never on native creation or execution. */
27
+ admit(submissionArg: IBrowserSubmission<TSnapshot, TReceipt>): Promise<TReceipt>;
28
+ /** A missing receipt does not prove that an earlier admission did not happen. */
29
+ lookup(submissionArg: IBrowserSubmission<TSnapshot, TReceipt>): Promise<TReceipt | undefined>;
30
+ /**
31
+ * `definite`: the controller proved nothing was admitted. `transient`: a definite refusal whose
32
+ * cause may pass (the harness is starting, an operation limit), so the same ID with the same
33
+ * snapshot is sent again after a bounded backoff.
34
+ */
35
+ failure(errorArg: unknown): { definite: boolean; transient?: boolean; message: string };
36
+ changed(): void;
37
+ /** The waits before each resend of a transient refusal; its length bounds the resends. */
38
+ retryDelaysMs?: readonly number[];
39
+ /** A creation of unknown outcome keeps its dependent admissions fenced as well. */
40
+ canContinue?(receiptArg: TReceipt): boolean;
41
+ admitted?(submissionArg: IBrowserSubmission<TSnapshot, TReceipt>): void;
42
+ }
43
+
44
+ /** Waits before resending a transiently refused submission; three resends at most. */
45
+ export const defaultBrowserSubmissionRetryDelaysMs: readonly number[] = [500, 2_000, 5_000];
46
+
47
+ /**
48
+ * Admission order belongs to a browser conversation intent, not to native execution. An uncertain
49
+ * request fences later messages in that intent. Reconnection reads receipts only: an attempted
50
+ * snapshot is never sent again, including when lookup has not found a receipt yet. The one resend
51
+ * is of a definite refusal with a transient code: the same ID and snapshot, after a bounded backoff,
52
+ * while the conversation's later messages wait behind it. An unknown outcome, a lost connection or
53
+ * any other refusal is never resent.
54
+ */
55
+ export class BrowserSubmissions<TSnapshot, TReceipt> {
56
+ private entries: Array<IBrowserSubmission<TSnapshot, TReceipt>> = [];
57
+ private readonly draining = new Map<string, number>();
58
+ private readonly checking = new Map<string, Promise<void>>();
59
+ private readonly retryTimers = new Map<string, ReturnType<typeof setTimeout>>();
60
+ /** Rows asked to be read while a read of theirs was in flight. */
61
+ private readonly checkAgain = new Set<string>();
62
+ private epoch = 0;
63
+ private connected = true;
64
+ private disposed = false;
65
+
66
+ constructor(private readonly options: IBrowserSubmissionsOptions<TSnapshot, TReceipt>) {}
67
+
68
+ public get current(): readonly IBrowserSubmission<TSnapshot, TReceipt>[] {
69
+ return this.entries;
70
+ }
71
+
72
+ /** Capture and feedback are synchronous; neither waits for a draft write or an admission. */
73
+ public capture(idArg: string, scopeArg: string, snapshotArg: TSnapshot): IBrowserSubmission<TSnapshot, TReceipt> {
74
+ if (this.disposed) throw new Error('The browser submission queue is closed.');
75
+ if (this.entries.some((entry) => entry.id === idArg)) throw new Error('The submission ID is already in use.');
76
+ const fenced = !this.connected || this.entries.some((entry) => (
77
+ entry.scope === scopeArg && (entry.state === 'unknown' || entry.state === 'blocked'
78
+ || (entry.receipt !== undefined && this.options.canContinue?.(entry.receipt) === false))
79
+ ));
80
+ const entry: IBrowserSubmission<TSnapshot, TReceipt> = {
81
+ id: idArg,
82
+ scope: scopeArg,
83
+ snapshot: freezeSnapshot(structuredClone(snapshotArg)),
84
+ createdAt: Date.now(),
85
+ state: fenced ? 'blocked' : 'submitting',
86
+ attempted: false,
87
+ retries: 0,
88
+ };
89
+ this.entries = [...this.entries, entry];
90
+ this.options.changed();
91
+ void this.drain(scopeArg);
92
+ return entry;
93
+ }
94
+
95
+ /** The app tears down through this as well, so no resend timer outlives the connection. */
96
+ public disconnect(): void {
97
+ if (this.disposed) return;
98
+ this.connected = false;
99
+ this.epoch += 1;
100
+ this.clearRetryTimers();
101
+ this.entries = this.entries.map((entry) => entry.state === 'submitting'
102
+ ? { ...entry, state: entry.attempted ? 'unknown' : 'blocked',
103
+ error: entry.attempted ? 'Admission unconfirmed. Reconnect to check its receipt; this message will not be resent.' : undefined }
104
+ : entry);
105
+ this.options.changed();
106
+ }
107
+
108
+ /**
109
+ * Only reads are replayed after a disconnect, once per reconnection: an unknown outcome and every
110
+ * admitted receipt, whose changes the lost connection did not report. A receipt can release
111
+ * never-attempted followers.
112
+ */
113
+ public async reconnect(): Promise<void> {
114
+ if (this.disposed || this.connected) return;
115
+ this.connected = true;
116
+ await Promise.all(this.entries.filter((entry) => entry.state === 'unknown' || entry.state === 'admitted')
117
+ .map((entry) => this.check(entry.id)));
118
+ for (const scope of new Set(this.entries.map((entry) => entry.scope))) this.releaseFollowers(scope);
119
+ }
120
+
121
+ /**
122
+ * Reads the row's receipt; never sends. A check asked for while a read of the row is in flight
123
+ * may follow a change that read missed, so the row is read once more after it.
124
+ */
125
+ public check(idArg: string): Promise<void> {
126
+ const checking = this.checking.get(idArg);
127
+ if (checking) {
128
+ this.checkAgain.add(idArg);
129
+ return checking;
130
+ }
131
+ if (!this.checkable(idArg)) return Promise.resolve();
132
+ let task: Promise<void> | undefined;
133
+ task = (async () => {
134
+ try {
135
+ do {
136
+ this.checkAgain.delete(idArg);
137
+ await this.read(idArg);
138
+ } while (this.checkAgain.has(idArg) && this.checkable(idArg));
139
+ } finally {
140
+ // In the same turn as the last loop test: a check asked for later starts a read of its own.
141
+ this.checkAgain.delete(idArg);
142
+ if (this.checking.get(idArg) === task) this.checking.delete(idArg);
143
+ }
144
+ })();
145
+ this.checking.set(idArg, task);
146
+ return task;
147
+ }
148
+
149
+ /** Forget only rows an exact durable identity, not text or a timestamp, has replaced. */
150
+ public reconcile(isCarriedArg: (submissionArg: IBrowserSubmission<TSnapshot, TReceipt>) => boolean): void {
151
+ const kept = this.entries.filter((entry) => entry.state !== 'admitted'
152
+ || (entry.receipt !== undefined && this.options.canContinue?.(entry.receipt) === false)
153
+ || !isCarriedArg(entry));
154
+ if (kept.length === this.entries.length) return;
155
+ this.entries = kept;
156
+ this.options.changed();
157
+ }
158
+
159
+ public dispose(): void {
160
+ this.disposed = true;
161
+ this.epoch += 1;
162
+ this.entries = [];
163
+ this.clearRetryTimers();
164
+ }
165
+
166
+ private clearRetryTimers(): void {
167
+ for (const timer of this.retryTimers.values()) clearTimeout(timer);
168
+ this.retryTimers.clear();
169
+ }
170
+
171
+ private checkable(idArg: string): boolean {
172
+ const entry = this.entries.find((candidate) => candidate.id === idArg);
173
+ return entry !== undefined && (entry.state === 'unknown' || entry.state === 'admitted') && this.connected && !this.disposed;
174
+ }
175
+
176
+ private async read(idArg: string): Promise<void> {
177
+ const entry = this.entries.find((candidate) => candidate.id === idArg);
178
+ if (!entry || (entry.state !== 'unknown' && entry.state !== 'admitted') || !this.connected || this.disposed) return;
179
+ const epoch = this.epoch;
180
+ try {
181
+ const receipt = await this.options.lookup(entry);
182
+ if (this.disposed || epoch !== this.epoch || receipt === undefined) return;
183
+ const current = this.entries.find((candidate) => candidate.id === idArg);
184
+ if (!current) return;
185
+ if (current.state === 'admitted' && JSON.stringify(current.receipt) === JSON.stringify(receipt)) return;
186
+ const admitted = this.update(idArg, { state: 'admitted', receipt, error: undefined });
187
+ if (admitted) this.options.admitted?.(admitted);
188
+ if (this.options.canContinue?.(receipt) === false) this.blockFollowers(entry.scope);
189
+ else this.releaseFollowers(entry.scope);
190
+ } catch {
191
+ // A failed read proves nothing. Keep both the uncertain row and its dependent fence.
192
+ }
193
+ }
194
+
195
+ private async drain(scopeArg: string): Promise<void> {
196
+ const epoch = this.epoch;
197
+ if (this.disposed || !this.connected || this.scopeFenced(scopeArg) || this.draining.get(scopeArg) === epoch
198
+ || this.retryTimers.has(scopeArg)) return;
199
+ this.draining.set(scopeArg, epoch);
200
+ try {
201
+ while (!this.disposed && this.connected && epoch === this.epoch) {
202
+ const entry = this.entries.find((candidate) => candidate.scope === scopeArg
203
+ && (candidate.state === 'submitting' || candidate.state === 'unknown' || candidate.state === 'blocked'));
204
+ if (!entry || entry.state !== 'submitting' || entry.attempted) return;
205
+ const attempted = this.update(entry.id, { attempted: true })!;
206
+ try {
207
+ const receipt = await this.options.admit(attempted);
208
+ if (this.disposed || epoch !== this.epoch) return;
209
+ const admitted = this.update(entry.id, { state: 'admitted', receipt, error: undefined });
210
+ if (admitted) this.options.admitted?.(admitted);
211
+ if (this.options.canContinue?.(receipt) === false) {
212
+ this.blockFollowers(scopeArg);
213
+ return;
214
+ }
215
+ } catch (errorArg) {
216
+ if (this.disposed || epoch !== this.epoch) return;
217
+ const failure = this.options.failure(errorArg);
218
+ const delays = this.options.retryDelaysMs ?? defaultBrowserSubmissionRetryDelaysMs;
219
+ if (failure.definite && failure.transient && attempted.retries < delays.length) {
220
+ // Nothing was admitted: the same snapshot goes again under its ID once the wait ends.
221
+ this.update(entry.id, { attempted: false, retries: attempted.retries + 1 });
222
+ this.scheduleRetry(scopeArg, delays[attempted.retries]!);
223
+ return;
224
+ }
225
+ this.update(entry.id, { state: failure.definite ? 'refused' : 'unknown', error: failure.message });
226
+ if (!failure.definite) {
227
+ this.blockFollowers(scopeArg);
228
+ return;
229
+ }
230
+ }
231
+ }
232
+ } finally {
233
+ if (this.draining.get(scopeArg) === epoch) this.draining.delete(scopeArg);
234
+ // A receipt lookup may have resolved while the old connection's request was still settling.
235
+ if (!this.disposed && this.connected && !this.scopeFenced(scopeArg) && !this.retryTimers.has(scopeArg)
236
+ && this.entries.some((entry) => entry.scope === scopeArg && entry.state === 'submitting')) {
237
+ void this.drain(scopeArg);
238
+ }
239
+ }
240
+ }
241
+
242
+ private scheduleRetry(scopeArg: string, delayMsArg: number): void {
243
+ if (this.retryTimers.has(scopeArg)) return;
244
+ this.retryTimers.set(scopeArg, setTimeout(() => {
245
+ this.retryTimers.delete(scopeArg);
246
+ void this.drain(scopeArg);
247
+ }, delayMsArg));
248
+ }
249
+
250
+ private scopeFenced(scopeArg: string): boolean {
251
+ return this.entries.some((entry) => entry.scope === scopeArg
252
+ && (entry.state === 'unknown' || (entry.receipt !== undefined && this.options.canContinue?.(entry.receipt) === false)));
253
+ }
254
+
255
+ private blockFollowers(scopeArg: string): void {
256
+ this.entries = this.entries.map((entry) => entry.scope === scopeArg && entry.state === 'submitting' && !entry.attempted
257
+ ? { ...entry, state: 'blocked' } : entry);
258
+ this.options.changed();
259
+ }
260
+
261
+ private releaseFollowers(scopeArg: string): void {
262
+ if (this.scopeFenced(scopeArg)) return;
263
+ this.entries = this.entries.map((entry) => entry.scope === scopeArg && entry.state === 'blocked' && !entry.attempted
264
+ ? { ...entry, state: 'submitting', error: undefined } : entry);
265
+ this.options.changed();
266
+ void this.drain(scopeArg);
267
+ }
268
+
269
+ private update(idArg: string, patchArg: Partial<IBrowserSubmission<TSnapshot, TReceipt>>): IBrowserSubmission<TSnapshot, TReceipt> | undefined {
270
+ let updated: IBrowserSubmission<TSnapshot, TReceipt> | undefined;
271
+ this.entries = this.entries.map((entry) => {
272
+ if (entry.id !== idArg) return entry;
273
+ updated = { ...entry, ...patchArg };
274
+ return updated;
275
+ });
276
+ if (updated) this.options.changed();
277
+ return updated;
278
+ }
279
+ }
@@ -75,6 +75,8 @@ interface IActiveDraft {
75
75
  writeTask?: Promise<IControllerSessionDraft>;
76
76
  submissionToken?: symbol;
77
77
  pendingAppend?: IPendingDraftAppend;
78
+ detachedText?: boolean;
79
+ detachedAttachments?: boolean;
78
80
  }
79
81
 
80
82
  interface IPreparedSubmission {
@@ -193,6 +195,7 @@ export class SessionDraftSync {
193
195
  if (active.pendingAppend !== undefined && active.pendingAppend.textAtAppend !== textArg) {
194
196
  active.pendingAppend = undefined;
195
197
  }
198
+ active.detachedText = false;
196
199
  active.desiredText = textArg;
197
200
  active.dirtyText = active.desiredText !== active.authoritative.text;
198
201
  active.error = '';
@@ -253,6 +256,7 @@ export class SessionDraftSync {
253
256
  public setAttachments(attachmentsArg: readonly IControllerDraftAttachment[]): void {
254
257
  const active = this.active;
255
258
  if (!active || active.loading) return;
259
+ active.detachedAttachments = false;
256
260
  active.desiredAttachments = cloneAttachments(attachmentsArg);
257
261
  active.dirtyAttachments = !attachmentsEqual(
258
262
  active.desiredAttachments,
@@ -263,6 +267,41 @@ export class SessionDraftSync {
263
267
  this.schedule(active);
264
268
  }
265
269
 
270
+ /** Names a shared revision only when it actually contains the immutable body being captured. */
271
+ public submittedSnapshotRevision(textArg: string, attachmentsArg: readonly IControllerDraftAttachment[]): number | undefined {
272
+ const active = this.active;
273
+ return active && !active.loading && active.authoritative.text === textArg
274
+ && attachmentsEqual(active.authoritative.attachments, attachmentsArg)
275
+ ? active.authoritative.revision : undefined;
276
+ }
277
+
278
+ /**
279
+ * An immutable admission owns its body, not this mutable draft. Detach only the fields that
280
+ * still equal its click-time snapshot, synchronously; later typing/files are the next message.
281
+ * The normal writer keeps its revision-CAS ordering (including a write already in flight).
282
+ * No network result ever restores this body over newer input: a refusal belongs to its row.
283
+ */
284
+ public clearSubmittedSnapshot(
285
+ textArg: string,
286
+ attachmentsArg: readonly IControllerDraftAttachment[],
287
+ ): void {
288
+ const active = this.active;
289
+ if (!active || active.loading || active.submissionToken) return;
290
+ if (active.desiredText === textArg) {
291
+ active.desiredText = '';
292
+ active.detachedText = true;
293
+ active.pendingAppend = undefined;
294
+ }
295
+ if (attachmentsEqual(active.desiredAttachments, attachmentsArg)) {
296
+ active.desiredAttachments = [];
297
+ active.detachedAttachments = true;
298
+ }
299
+ active.dirtyText = active.desiredText !== active.authoritative.text;
300
+ active.dirtyAttachments = !attachmentsEqual(active.desiredAttachments, active.authoritative.attachments);
301
+ this.emit(active);
302
+ this.schedule(active);
303
+ }
304
+
266
305
  public async flush(): Promise<IControllerSessionDraft> {
267
306
  const active = this.active;
268
307
  if (!active) throw new Error('No session draft is active.');
@@ -445,6 +484,7 @@ export class SessionDraftSync {
445
484
  active.desiredAttachments,
446
485
  active.authoritative.attachments,
447
486
  );
487
+ if (!active.writeTask) this.releaseDetachedClear(active);
448
488
  this.emit(active);
449
489
  if (active.dirtyText || active.dirtyAttachments) this.schedule(active);
450
490
  return true;
@@ -465,7 +505,9 @@ export class SessionDraftSync {
465
505
  try {
466
506
  const draft = await this.options.transport.getDraft(activeArg.projectId, activeArg.sessionId);
467
507
  if (!this.canSynchronize(activeArg)) return;
508
+ const revisionAdvanced = draft.revision !== activeArg.authoritative.revision;
468
509
  activeArg.authoritative = cloneDraft(draft);
510
+ if (revisionAdvanced && !activeArg.writeTask) this.releaseDetachedClear(activeArg);
469
511
  const pendingAppend = activeArg.pendingAppend;
470
512
  if (pendingAppend !== undefined) {
471
513
  // The appends are re-applied to the text that is authoritative now, each one only when it
@@ -538,6 +580,9 @@ export class SessionDraftSync {
538
580
  ) {
539
581
  conflictAttempts += 1;
540
582
  await this.hydrate(activeArg);
583
+ // A detached clear never replays against another writer’s newer revision. Only
584
+ // actual next-message edits retain the ordinary whole-draft retry semantics.
585
+ this.releaseDetachedClear(activeArg);
541
586
  continue;
542
587
  }
543
588
  throw errorArg;
@@ -548,6 +593,7 @@ export class SessionDraftSync {
548
593
  activeArg.authoritative = cloneDraft(response);
549
594
  if (sentTextField && activeArg.desiredText === sentText) {
550
595
  activeArg.dirtyText = false;
596
+ activeArg.detachedText = false;
551
597
  // The text the controller now holds is the one that carried the appended blocks, so
552
598
  // nothing is left to re-apply after a later conflict.
553
599
  activeArg.pendingAppend = undefined;
@@ -555,7 +601,10 @@ export class SessionDraftSync {
555
601
  if (
556
602
  sentAttachmentField
557
603
  && attachmentsEqual(activeArg.desiredAttachments, sentAttachments)
558
- ) activeArg.dirtyAttachments = false;
604
+ ) {
605
+ activeArg.dirtyAttachments = false;
606
+ activeArg.detachedAttachments = false;
607
+ }
559
608
  }
560
609
  activeArg.dirtyText = activeArg.desiredText !== activeArg.authoritative.text;
561
610
  activeArg.dirtyAttachments = !attachmentsEqual(
@@ -583,6 +632,20 @@ export class SessionDraftSync {
583
632
  }
584
633
  }
585
634
 
635
+ /** A concurrent revision owns untouched fields; never retry a snapshot-clear over it. */
636
+ private releaseDetachedClear(activeArg: IActiveDraft): void {
637
+ if (activeArg.detachedText) {
638
+ activeArg.desiredText = activeArg.authoritative.text;
639
+ activeArg.dirtyText = false;
640
+ activeArg.detachedText = false;
641
+ }
642
+ if (activeArg.detachedAttachments) {
643
+ activeArg.desiredAttachments = cloneAttachments(activeArg.authoritative.attachments);
644
+ activeArg.dirtyAttachments = false;
645
+ activeArg.detachedAttachments = false;
646
+ }
647
+ }
648
+
586
649
  private schedule(activeArg: IActiveDraft): void {
587
650
  if (!this.isCurrent(activeArg) || activeArg.loading || activeArg.submissionToken) return;
588
651
  if (activeArg.debounceTimer) clearTimeout(activeArg.debounceTimer);