* ✨ feat(session): persist and synchronize read state * 🌐 fix(client): complete Japanese labels after base update * 📝 docs: refresh session read-state dependency graph * 🧪 test(web): align session menu and read-state snapshots * 🌐 fix(session): complete Japanese plugin metadata * 🧪 test(web): distinguish unread from pending user input * 🐛 修复会话阅读状态的故障隔离与生命周期 * 🐛 修复阅读状态依赖检查并补齐自动已读覆盖 * 🐛 恢复会话绿点并移除未读标题加粗 * ♻️ Keep shared session reading state in Host memory * 🌐 Complete reading-state metadata for supported locales * 📝 Refresh module graph after removing storage dependency * 🧹 Move storage dependency policy out of memory-only reading * 🐛 Wait for sidebar catalog before replay navigation * 🔧 Give Host TypeScript checks 8 GiB of heap * ✅ Align Host heap checks and Windows diagnostics * ♻️ Keep CI repairs outside the session reading PR * 🐛 Show failed unread turns with an error indicator * 🔧 Adjust npm layout budget for the reading package * 🧪 Scope projection stream assertions to observed units * 🧪 Scope permission and title assertions to their projections * ♻️ Simplify session reading updates to explicit positions * 🐛 Align session reading package with release version * 🧪 Load reading state in LiveShare viewer fixtures * 🧹 Remove persistence-only type link from memory PR * 🧪 Align context menu expectations with reading actions * 🐛 Sync reading package with the release version * 🧪 Include turn boundaries in session summary expectations * ♻️ Derive reading failure facts from Host events instead of a projection The reading service took each completion from the Agent turn-boundary projection, which forced a stateVersion bump and put that projection's whole internal state on the wire for every Session list row. The service now records `turn/end` itself, so `core/agent` and the persisted projection schema are untouched. History that ended before the service started raises no reminder, which keeps "existing history reads as read" without a startup scan. The failure flag therefore describes only turns this Host observed: marking a Session whose last turn failed before a restart shows the ordinary unread reminder. * 🐛 Preserve manual unread until explicit Session interaction * 🧪 Align cold Session reading snapshot with unloaded title * 🔖 chore(session): align session-read-state with 0.2.2-rc.2 Master moved the root and every released manifest to 0.2.2-rc.2, but the new dsh-session-read-state package still declared 0.2.2-rc.1, failing constraints, verify-version-sync, verify-npm-install-layout, and release:verify after the master merge. * ♻️ refactor(gateway): recognize Remote stream carrier loss by error code RemoteStream now decides whether a generation end or failure is a retryable carrier loss by the stable code 'gateway/carrier-lost' instead of `instanceof RemoteStreamCarrierError`. RemoteStreamCarrierError keeps working and carries that code, so existing callers are unchanged, while a Client feature package can return a coded Error from `ended` without importing the class. The new RemoteStreamCarrierFailure type describes such errors; terminal-controller classifies disconnects the same way. * ♻️ refactor(session): move Session reading into ui-session-status Fold the reading feature into the single package packages/client/ui-session-status and delete session-read-state. - Host face: SessionStatusService keeps in-memory reading records and serves the sessionStatus Remote namespace (mark, follow) with session-status/* error codes; its service key is registered only for Gateway discovery and is not declared on Context. - Client face: a package-internal reading model follows the Host stream through RemoteSnapshotStream; ctx.uiSession.sessionStatus folds the records into completionUnread and completionFailed and exposes mark and readingReady; main-view acknowledgement and the session-status/read-intent event live here. No reading service or type is exported. - verify-client-packages lets a packages/client feature package request packages/api rows through dsh.client.external; feature-to-feature requests stay rejected. - ui-workspace, ui-conversation, bundles, remotes, LiveShare, the client test runtime, docs, Agent Notes, and the upgrade guide follow. * ♻️ refactor(session): clear manual unread only when the main view switches to the Session Remove the session-status/read-intent event. ui-session-status now clears an existing manual unread mark when the main view switches to a different Session, including the first selection after a page load, once the reading baseline is ready and the body arrives. Window focus, visibility changes, a replaced binding for the same Session, and activity in the open Session preserve manual unread. ui-conversation returns to master (only the ui-session-status client tsconfig reference remains), and ui-workspace navigation no longer emits the event. Tests, the session-reading Web e2e case, READMEs, and the reading Agent Note follow the new rule. * 🐛 fix(session): address the Session status review findings - Open the reading stream only while ctx.remoteAccess allows sessionStatus/follow, so a LiveShare viewer opens no owner-only stream, keeps readingReady false, and logs no synchronization failure; access changes start or stop the stream. - Hide the unacknowledged completion of the Session the visible, focused main view retains; manual marks stay visible, completionFailed follows completionUnread, and the reminder returns on blur, hiding, or a switch. - Declare ctx.sessionStatus on Context and restore its SERVICE_PAGE and SERVICE_ROLES entries; the service and SessionRead* types document on docs/subsystems/web-client.md, which now owns the reading section with the failed field. - Document that a Host plugin reload can leave the Client with a terminal error until a page reload; drop the redundant types import and the unused publish() result. - Archived Sessions read as read and offer no reading mark in the row menu or the toggle-read shortcut. - session.toggleRead names the open dialog, open panel, or archived Session that blocks it instead of asking to select a Session. - isRemoteStreamCarrierFailure rejects a RemoteError from the Host even when it carries gateway/carrier-lost; the code is Client-local. - ./types is types-only like ui-trajectory, and files no longer ship lib/types JavaScript. * 🐛 fix(session): close the remaining Session status review gaps - The Gateway Client exports isRemoteStreamCarrierFailure, and the terminal view uses it, so a Host RemoteError carrying the carrier code shows as failed rather than disconnected. - Each access grant's reading stream carries a token: frames a revoked or superseded stream still delivers are discarded, and cleanup waits for disposal before leaving reading unavailable unless a newer grant is active. - The status table hides the viewed Session's natural reminder only while that Session is open; the panel case is a documented limitation. - The README limitation and the upgrade guide state that only the main-view switch rule, including a reload restore, clears manual unread automatically. - Add the missing blank line before the next heading in the web-client reading section. * ♻️ refactor(session): install reading under the Session status table - apply returns to master's form plus one line: UiSessionStatus is the root, published as the useSessionStatus source, and installReading() from the new src/client/read.ts follows Host reading records into it. UiSessionStatus takes master's three constructor parameters and creates its own SessionReadModel as the package-internal `reading` member; installSessionStatus is removed and acknowledge.ts stays a separate file. - The viewed Session's snapshot subscription republishes the table only when its openState changes, so streamed events do not rebuild it. - Remove the obsolete Dev Note sentence about failure outcomes. - session.toggleRead passes when the Peer may not mark reading, matching the row menu, and reports readingUnavailable only while unsynchronized. --------- Co-authored-by: imccyu <[email protected]> Co-authored-by: imccyu <[email protected]>
581 lines
21 KiB
TypeScript
581 lines
21 KiB
TypeScript
/** Cursor, page, and live-tail coordination over a reconnecting Remote stream. */
|
|
|
|
import { RemoteError } from '@deepseek-ai/dsh-typert-protocol'
|
|
import { RemoteStreamCarrierError, type RemoteStreamCarrierFailure } from './stream-client.ts'
|
|
import type {
|
|
RemoteStream,
|
|
RemoteStreamItem,
|
|
RemoteStreamOptions,
|
|
} from './remote-stream.ts'
|
|
|
|
/** Host-side stream protocol violation, marked so consumers surface it as an error state. */
|
|
function protocolViolation(message: string): RemoteError<'gateway/internal'> {
|
|
return new RemoteError('gateway/internal', message, {})
|
|
}
|
|
|
|
/** Transport-neutral opening snapshot, durable entry, or cursorless notification. */
|
|
export type RemoteJournalFrame<Entry, Cursor, Page, Notification = never> =
|
|
| { readonly type: 'opened'; readonly cursor: Cursor; readonly page: Page }
|
|
| { readonly type: 'entry'; readonly entry: Entry }
|
|
| ([Notification] extends [never]
|
|
? never
|
|
: { readonly type: 'notification'; readonly notification: Notification })
|
|
|
|
/** One journal-window update or cursorless domain notification. */
|
|
export type RemoteJournalChange<Page, Entry, Notification = never> =
|
|
| {
|
|
readonly type: 'replace'
|
|
readonly page: Page
|
|
readonly entries: readonly Entry[]
|
|
readonly hasMore: boolean
|
|
}
|
|
| {
|
|
readonly type: 'prepend'
|
|
readonly page: Page
|
|
readonly entries: readonly Entry[]
|
|
readonly hasMore: boolean
|
|
}
|
|
| { readonly type: 'append'; readonly entry: Entry }
|
|
| ([Notification] extends [never]
|
|
? never
|
|
: { readonly type: 'notification'; readonly notification: Notification })
|
|
|
|
type JournalStreamItem<Page, Entry, Cursor, Notification> = RemoteStreamItem<
|
|
RemoteJournalFrame<Entry, Cursor, Page, Notification>
|
|
>
|
|
|
|
/** Gateway capability used to create one reconnecting Remote stream. */
|
|
export interface RemoteStreamFactory {
|
|
/**
|
|
* Create one independently cancellable logical stream.
|
|
* @param options - domain-owned opener and generation-end classification.
|
|
* @returns a reconnecting single-consumer stream.
|
|
*/
|
|
$stream<Item>(options: RemoteStreamOptions<Item>): RemoteStream<Item>
|
|
}
|
|
|
|
/** Domain publication and cursor operations for one addressed journal stream. */
|
|
export interface RemoteJournalStreamOptions<Page, Entry, Cursor, Notification = never> {
|
|
/** Diagnostic stream name used in protocol failures. */
|
|
readonly name: string
|
|
/** Cursor representing a journal with no entries. */
|
|
readonly emptyCursor: Cursor
|
|
/** Read the ordered entries carried by a page. */
|
|
readonly entries: (page: Page) => readonly Entry[]
|
|
/** Read whether an older page exists. */
|
|
readonly hasMore: (page: Page) => boolean
|
|
/** Read the inclusive first durable cursor covered by one entry. */
|
|
readonly first: (entry: Entry) => Cursor
|
|
/** Read the inclusive final cursor, which must not precede the first. */
|
|
readonly last: (entry: Entry) => Cursor
|
|
/** Compare two cursors. */
|
|
readonly compare: (left: Cursor, right: Cursor) => number
|
|
/** Test whether the right cursor immediately follows the left cursor. */
|
|
readonly follows: (left: Cursor, right: Cursor) => boolean
|
|
/** Apply one complete journal-window change or cursorless notification. */
|
|
readonly publish: (change: RemoteJournalChange<Page, Entry, Notification>) => void
|
|
/** Observe a retryable carrier loss before reconnection. */
|
|
readonly carrierFailed?: (error: RemoteStreamCarrierFailure) => void
|
|
/** Publish a terminal stream, page, or protocol failure after opening. */
|
|
readonly failed: (error: unknown) => void
|
|
}
|
|
|
|
/**
|
|
* Owns snapshot-first opening, ordered live delivery, pagination, and repair.
|
|
*
|
|
* The domain retains its published window during reconnection. A replacement is
|
|
* published only after the opening page reaches the generation's cursor.
|
|
* Notifications never change a cursor and wait behind an in-flight gap repair.
|
|
*/
|
|
export abstract class RemoteJournalStream<
|
|
Page, Entry, Cursor, PageRequest = void, Notification = never,
|
|
> {
|
|
private readonly stream: RemoteStream<RemoteJournalFrame<Entry, Cursor, Page, Notification>>
|
|
private initialRequest!: PageRequest
|
|
private resumeCursor: Cursor | undefined
|
|
private hasResumeCursor = false
|
|
private generation = 0
|
|
private firstCursor: Cursor | undefined
|
|
private lastCursor: Cursor | undefined
|
|
private started = false
|
|
private opened = false
|
|
private disposed = false
|
|
private done: Promise<void> | undefined
|
|
private closing: Promise<void> | undefined
|
|
private pendingNext: Promise<IteratorResult<JournalStreamItem<Page, Entry, Cursor, Notification>>> | undefined
|
|
|
|
/**
|
|
* @param remote - Gateway factory for the reconnecting physical-generation stream.
|
|
* @param options - cursor algebra and domain publication sinks.
|
|
*/
|
|
protected constructor(
|
|
remote: RemoteStreamFactory,
|
|
private readonly options: RemoteJournalStreamOptions<Page, Entry, Cursor, Notification>,
|
|
) {
|
|
this.stream = remote.$stream<RemoteJournalFrame<Entry, Cursor, Page, Notification>>({
|
|
name: options.name,
|
|
open: signal => this.follow(this.initialRequest, signal),
|
|
ended: accepted => accepted
|
|
? new RemoteStreamCarrierError(`${options.name} ended without a terminal result`)
|
|
: protocolViolation(
|
|
`${this.hasResumeCursor ? 'resumed ' : ''}${options.name} ended before its opening cursor`,
|
|
),
|
|
...(options.carrierFailed === undefined
|
|
? {}
|
|
: { carrierFailed: options.carrierFailed }),
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Open one physical journal generation with a complete current snapshot.
|
|
* @param request - opening-window request retained for later repair.
|
|
* @param signal - cancellation lifetime of the physical generation.
|
|
* @returns opening cursor followed by live entries.
|
|
*/
|
|
protected abstract follow(
|
|
request: PageRequest,
|
|
signal: AbortSignal,
|
|
): AsyncIterable<RemoteJournalFrame<Entry, Cursor, Page, Notification>>
|
|
|
|
/**
|
|
* Read one journal page through the addressed domain source.
|
|
* @param request - domain page request.
|
|
* @param through - inclusive journal cursor that fixes the source read.
|
|
* @param signal - cancellation lifetime shared with the logical stream.
|
|
* @returns the requested page, whose tail equals `through` unless the domain request selects older entries.
|
|
*/
|
|
protected abstract readPage(request: PageRequest, through: Cursor, signal: AbortSignal): Promise<Page>
|
|
|
|
/**
|
|
* Derive an unbounded-tail request from the initial page request.
|
|
* @param initial - request used to open the journal window.
|
|
* @returns request suitable for reconnect and gap repair.
|
|
*/
|
|
protected abstract repairRequest(initial: PageRequest): PageRequest
|
|
|
|
/** Cancellation lifetime shared by follow and page calls. */
|
|
get signal(): AbortSignal {
|
|
return this.stream.signal
|
|
}
|
|
|
|
/**
|
|
* Establish follow and publish the opening snapshot carried by its first frame.
|
|
* @param request - initial tail-page request.
|
|
* @returns after the first complete window is published.
|
|
*/
|
|
async open(request: PageRequest): Promise<void> {
|
|
if (this.started) throw new Error(`${this.options.name} already opened`)
|
|
this.started = true
|
|
this.initialRequest = request
|
|
const iterator = this.stream[Symbol.asyncIterator]()
|
|
try {
|
|
const first = await this.takeNext(iterator)
|
|
if (first.done) throw protocolViolation(`${this.options.name} ended before its opening cursor`)
|
|
this.replaceGeneration(first.value, false)
|
|
this.opened = true
|
|
this.done = this.consume(iterator)
|
|
} catch (error) {
|
|
await this.stream.dispose()
|
|
throw error
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Read and prepend one older page after a successful open.
|
|
* @param request - domain page request bound to this stream's address.
|
|
* @returns after the page is applied or rejected as discontinuous.
|
|
*/
|
|
async prepend(request: PageRequest): Promise<void> {
|
|
if (!this.opened || this.disposed) throw new Error(`${this.options.name} is not open`)
|
|
const page = await this.readPage(request, this.currentCursor(), this.stream.signal)
|
|
this.stream.signal.throwIfAborted()
|
|
const entries = this.options.entries(page)
|
|
this.assertPage(entries)
|
|
const before = this.firstCursor
|
|
const accepted = before === undefined
|
|
? [...entries]
|
|
: entries.filter(entry => this.options.compare(this.options.first(entry), before) < 0)
|
|
const tail = accepted.at(-1)
|
|
if (tail !== undefined && before !== undefined
|
|
&& !this.options.follows(this.options.last(tail), before)) {
|
|
this.options.publish({ type: 'prepend', page, entries: [], hasMore: false })
|
|
throw protocolViolation(`${this.options.name} history page is discontinuous`)
|
|
}
|
|
const first = accepted[0]
|
|
if (first !== undefined) this.firstCursor = this.options.first(first)
|
|
this.options.publish({
|
|
type: 'prepend',
|
|
page,
|
|
entries: accepted,
|
|
hasMore: this.options.hasMore(page),
|
|
})
|
|
}
|
|
|
|
/** Replace the active physical generation while retaining the published window. */
|
|
restart(): void {
|
|
this.stream.restart()
|
|
}
|
|
|
|
/**
|
|
* Permanently stop follow, page requests, and the background consumer.
|
|
* @returns when no stream work or publication callback can still run.
|
|
*/
|
|
dispose(): Promise<void> {
|
|
if (this.closing !== undefined) return this.closing
|
|
this.disposed = true
|
|
const done = this.done
|
|
const closing = (async () => {
|
|
await this.stream.dispose()
|
|
await done
|
|
})()
|
|
this.closing = closing
|
|
return closing
|
|
}
|
|
|
|
private async consume(
|
|
iterator: AsyncIterator<JournalStreamItem<Page, Entry, Cursor, Notification>>,
|
|
): Promise<void> {
|
|
try {
|
|
while (true) {
|
|
const next = await this.takeNext(iterator)
|
|
if (next.done) return
|
|
const item = next.value
|
|
if (item.generation !== this.generation) {
|
|
this.replaceGeneration(item, true)
|
|
continue
|
|
}
|
|
if (item.value.type === 'opened') {
|
|
throw protocolViolation(`${this.options.name} emitted more than one opening cursor`)
|
|
}
|
|
if (item.value.type === 'notification') {
|
|
this.publishNotification(item.value.notification)
|
|
continue
|
|
}
|
|
await this.acceptEntry(item.value.entry, item, iterator)
|
|
}
|
|
} catch (error) {
|
|
if (!this.disposed) this.options.failed(error)
|
|
}
|
|
}
|
|
|
|
private replaceGeneration(
|
|
initial: JournalStreamItem<Page, Entry, Cursor, Notification>,
|
|
resumed: boolean,
|
|
): void {
|
|
const opening = this.opening(initial, resumed)
|
|
this.replaceFromOpening(opening.page, opening.cursor)
|
|
}
|
|
|
|
private opening(
|
|
item: RemoteStreamItem<RemoteJournalFrame<Entry, Cursor, Page, Notification>>,
|
|
resumed: boolean,
|
|
): { readonly cursor: Cursor; readonly page: Page } {
|
|
if (item.value.type !== 'opened') {
|
|
throw protocolViolation(`${resumed ? 'resumed ' : ''}${this.options.name} emitted an entry before its opening cursor`)
|
|
}
|
|
const cursor = item.value.cursor
|
|
if (resumed && this.lastCursor !== undefined
|
|
&& this.options.compare(cursor, this.lastCursor) < 0) {
|
|
throw protocolViolation(
|
|
`${this.options.name} resumed at a cursor behind the last applied entry`,
|
|
)
|
|
}
|
|
this.generation = item.generation
|
|
item.accept()
|
|
return { cursor, page: item.value.page }
|
|
}
|
|
|
|
/** Publish a generation's opening page without issuing a second Remote call. */
|
|
private replaceFromOpening(page: Page, cursor: Cursor): void {
|
|
this.assertPageThrough(page, cursor)
|
|
const entries = [...this.options.entries(page)]
|
|
this.assertPage(entries)
|
|
const first = entries[0]
|
|
this.firstCursor = first === undefined ? undefined : this.options.first(first)
|
|
this.lastCursor = cursor
|
|
this.setResumeCursor(cursor)
|
|
this.options.publish({
|
|
type: 'replace',
|
|
page,
|
|
entries,
|
|
hasMore: this.options.hasMore(page),
|
|
})
|
|
}
|
|
|
|
private async acceptEntry(
|
|
entry: Entry,
|
|
item: JournalStreamItem<Page, Entry, Cursor, Notification>,
|
|
iterator: AsyncIterator<JournalStreamItem<Page, Entry, Cursor, Notification>>,
|
|
): Promise<void> {
|
|
const { first, last: cursor } = this.entryRange(entry)
|
|
const last = this.lastCursor as Cursor
|
|
if (this.options.compare(cursor, last) <= 0) return
|
|
if (this.options.compare(first, last) <= 0) {
|
|
throw protocolViolation(`${this.options.name} emitted a partially overlapping entry`)
|
|
}
|
|
if (!this.options.follows(last, first)) {
|
|
const request = this.repairPageRequest()
|
|
const superseded = await this.replaceThrough(
|
|
request,
|
|
cursor,
|
|
item.generation,
|
|
item.signal,
|
|
iterator,
|
|
[entry],
|
|
[],
|
|
)
|
|
if (superseded !== undefined) {
|
|
this.replaceGeneration(superseded, true)
|
|
}
|
|
return
|
|
}
|
|
if (this.firstCursor === undefined) this.firstCursor = first
|
|
this.lastCursor = cursor
|
|
this.setResumeCursor(cursor)
|
|
this.options.publish({ type: 'append', entry })
|
|
}
|
|
|
|
private async replaceThrough(
|
|
request: PageRequest,
|
|
requiredCursor: Cursor,
|
|
generation: number,
|
|
signal: AbortSignal,
|
|
iterator: AsyncIterator<JournalStreamItem<Page, Entry, Cursor, Notification>>,
|
|
queued: Entry[],
|
|
notifications: Notification[],
|
|
): Promise<JournalStreamItem<Page, Entry, Cursor, Notification> | undefined> {
|
|
let read = await this.readPageWhileFollowing(
|
|
request,
|
|
requiredCursor,
|
|
generation,
|
|
signal,
|
|
iterator,
|
|
queued,
|
|
notifications,
|
|
)
|
|
if (read.type === 'superseded') return read.item
|
|
let page = read.page
|
|
this.assertPageThrough(page, requiredCursor)
|
|
let entries = this.mergeReplacement(page, queued)
|
|
let target = this.maxCursor(requiredCursor, queued)
|
|
if (entries === undefined || this.options.compare(this.tailCursor(entries), target) < 0) {
|
|
read = await this.readPageWhileFollowing(
|
|
this.repairPageRequest(),
|
|
target,
|
|
generation,
|
|
signal,
|
|
iterator,
|
|
queued,
|
|
notifications,
|
|
)
|
|
if (read.type === 'superseded') return read.item
|
|
page = read.page
|
|
this.assertPageThrough(page, target)
|
|
entries = this.mergeReplacement(page, queued)
|
|
target = this.maxCursor(requiredCursor, queued)
|
|
}
|
|
if (entries === undefined || this.options.compare(this.tailCursor(entries), target) < 0) {
|
|
throw protocolViolation(`${this.options.name} page did not reach its opening cursor`)
|
|
}
|
|
const first = entries[0]
|
|
/* v8 ignore next -- a successful positive-cursor replacement page cannot be empty. */
|
|
this.firstCursor = first === undefined ? undefined : this.options.first(first)
|
|
this.lastCursor = this.tailCursor(entries)
|
|
this.setResumeCursor(this.lastCursor)
|
|
this.options.publish({
|
|
type: 'replace',
|
|
page,
|
|
entries,
|
|
hasMore: this.options.hasMore(page),
|
|
})
|
|
for (const notification of notifications) {
|
|
this.publishNotification(notification)
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
private async readPageWhileFollowing(
|
|
request: PageRequest,
|
|
through: Cursor,
|
|
generation: number,
|
|
signal: AbortSignal,
|
|
iterator: AsyncIterator<JournalStreamItem<Page, Entry, Cursor, Notification>>,
|
|
queued: Entry[],
|
|
notifications: Notification[],
|
|
): Promise<
|
|
| { readonly type: 'page'; readonly page: Page }
|
|
| { readonly type: 'superseded'; readonly item: JournalStreamItem<Page, Entry, Cursor, Notification> }
|
|
> {
|
|
const page = this.readPage(request, through, signal).then(
|
|
value => ({ type: 'page' as const, value }),
|
|
(error: unknown) => ({ type: 'page-error' as const, error }),
|
|
)
|
|
while (true) {
|
|
const pending = this.nextResult(iterator)
|
|
const next = pending.then(
|
|
value => ({ type: 'next' as const, value }),
|
|
(error: unknown) => ({ type: 'next-error' as const, error }),
|
|
)
|
|
const result = await Promise.race([page, next])
|
|
if (result.type === 'page') {
|
|
signal.throwIfAborted()
|
|
return { type: 'page', page: result.value }
|
|
}
|
|
if (result.type === 'page-error') {
|
|
if (!signal.aborted || this.stream.signal.aborted) throw result.error
|
|
return this.awaitReplacementGeneration(generation, iterator, pending)
|
|
}
|
|
this.releaseNext()
|
|
if (result.type === 'next-error') throw result.error
|
|
if (result.value.done) {
|
|
signal.throwIfAborted()
|
|
throw protocolViolation(`${this.options.name} ended while reading its replacement page`)
|
|
}
|
|
const item = result.value.value
|
|
if (item.generation !== generation) return { type: 'superseded', item }
|
|
if (item.value.type === 'opened') {
|
|
throw protocolViolation(`${this.options.name} emitted more than one opening cursor`)
|
|
}
|
|
if (item.value.type === 'notification') {
|
|
notifications.push(item.value.notification)
|
|
continue
|
|
}
|
|
queued.push(item.value.entry)
|
|
}
|
|
}
|
|
|
|
private async awaitReplacementGeneration(
|
|
generation: number,
|
|
iterator: AsyncIterator<JournalStreamItem<Page, Entry, Cursor, Notification>>,
|
|
initial: Promise<IteratorResult<JournalStreamItem<Page, Entry, Cursor, Notification>>>,
|
|
): Promise<{ readonly type: 'superseded'; readonly item: JournalStreamItem<Page, Entry, Cursor, Notification> }> {
|
|
let pending = initial
|
|
while (true) {
|
|
let next: IteratorResult<JournalStreamItem<Page, Entry, Cursor, Notification>>
|
|
try {
|
|
next = await pending
|
|
} finally {
|
|
this.releaseNext()
|
|
}
|
|
if (next.done) {
|
|
this.stream.signal.throwIfAborted()
|
|
throw protocolViolation(`${this.options.name} ended while replacing an aborted page generation`)
|
|
}
|
|
const item = next.value
|
|
if (item.generation !== generation) return { type: 'superseded', item }
|
|
if (item.value.type === 'opened') {
|
|
throw protocolViolation(`${this.options.name} emitted more than one opening cursor`)
|
|
}
|
|
pending = this.nextResult(iterator)
|
|
}
|
|
}
|
|
|
|
private mergeReplacement(page: Page, queued: readonly Entry[]): Entry[] | undefined {
|
|
const entries = [...this.options.entries(page)]
|
|
this.assertPage(entries)
|
|
for (const entry of queued) this.entryRange(entry)
|
|
const sorted = [...queued].sort((left, right) => (
|
|
this.options.compare(this.options.first(left), this.options.first(right))
|
|
))
|
|
let tail = this.tailCursor(entries)
|
|
for (const entry of sorted) {
|
|
const first = this.options.first(entry)
|
|
const last = this.options.last(entry)
|
|
if (this.options.compare(last, tail) <= 0) continue
|
|
if (this.options.compare(first, tail) <= 0) {
|
|
throw protocolViolation(`${this.options.name} replacement contains a partially overlapping entry`)
|
|
}
|
|
if (!this.options.follows(tail, first)) return undefined
|
|
entries.push(entry)
|
|
tail = last
|
|
}
|
|
return entries
|
|
}
|
|
|
|
private maxCursor(cursor: Cursor, entries: readonly Entry[]): Cursor {
|
|
let result = cursor
|
|
for (const entry of entries) {
|
|
const candidate = this.options.last(entry)
|
|
if (this.options.compare(candidate, result) > 0) result = candidate
|
|
}
|
|
return result
|
|
}
|
|
|
|
private nextResult(
|
|
iterator: AsyncIterator<JournalStreamItem<Page, Entry, Cursor, Notification>>,
|
|
): Promise<IteratorResult<JournalStreamItem<Page, Entry, Cursor, Notification>>> {
|
|
this.pendingNext ??= iterator.next()
|
|
return this.pendingNext
|
|
}
|
|
|
|
private async takeNext(
|
|
iterator: AsyncIterator<JournalStreamItem<Page, Entry, Cursor, Notification>>,
|
|
): Promise<IteratorResult<JournalStreamItem<Page, Entry, Cursor, Notification>>> {
|
|
const pending = this.nextResult(iterator)
|
|
try {
|
|
return await pending
|
|
} finally {
|
|
this.releaseNext()
|
|
}
|
|
}
|
|
|
|
private releaseNext(): void {
|
|
this.pendingNext = undefined
|
|
}
|
|
|
|
private publishNotification(notification: Notification): void {
|
|
this.options.publish({
|
|
type: 'notification',
|
|
notification,
|
|
} as RemoteJournalChange<Page, Entry, Notification>)
|
|
}
|
|
|
|
private repairPageRequest(): PageRequest {
|
|
return this.repairRequest(this.initialRequest)
|
|
}
|
|
|
|
private setResumeCursor(cursor: Cursor): void {
|
|
this.resumeCursor = cursor
|
|
this.hasResumeCursor = true
|
|
}
|
|
|
|
private currentCursor(): Cursor {
|
|
return this.resumeCursor as Cursor
|
|
}
|
|
|
|
private tailCursor(entries: readonly Entry[]): Cursor {
|
|
const tail = entries.at(-1)
|
|
return tail === undefined ? this.options.emptyCursor : this.options.last(tail)
|
|
}
|
|
|
|
private assertPage(entries: readonly Entry[]): void {
|
|
const iterator = entries[Symbol.iterator]()
|
|
const first = iterator.next()
|
|
if (first.done) return
|
|
let previousRange = this.entryRange(first.value)
|
|
for (const entry of iterator) {
|
|
const range = this.entryRange(entry)
|
|
if (!this.options.follows(previousRange.last, range.first)) {
|
|
throw protocolViolation(`${this.options.name} page contains discontinuous entries`)
|
|
}
|
|
previousRange = range
|
|
}
|
|
}
|
|
|
|
private entryRange(entry: Entry): { readonly first: Cursor; readonly last: Cursor } {
|
|
const first = this.options.first(entry)
|
|
const last = this.options.last(entry)
|
|
if (this.options.compare(first, last) > 0) {
|
|
throw protocolViolation(`${this.options.name} entry has an inverted cursor range`)
|
|
}
|
|
return { first, last }
|
|
}
|
|
|
|
private assertPageThrough(page: Page, through: Cursor): void {
|
|
const tail = this.tailCursor(this.options.entries(page))
|
|
if (this.options.compare(tail, through) !== 0) {
|
|
throw protocolViolation(`${this.options.name} page did not end at its requested cursor`)
|
|
}
|
|
}
|
|
}
|