Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/fork-adoption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,23 +342,25 @@ function persistForkLineageNow(session: Session, log: (level: string, msg: strin
* for life. The link copies nothing at link time; adoption still judges per
* block in raw-id set semantics, so a mis-guessed parent can at most be
* searched read-only, never adopted from. */
export function maybeAdoptForkBlocks(args: {
// #4942: async — the parent load below must reach the DB on network storage
// backends, or an anonymous fork of a DB-only parent starts fresh.
export async function maybeAdoptForkBlocks(args: {
session: Session;
parentId: string;
protocol: WireProtocol;
parsed: unknown;
upstreamOrigin: string;
enabled: boolean;
log: (level: string, msg: string) => void;
}): void {
}): Promise<void> {
const { session, parentId, protocol, parsed, upstreamOrigin, enabled, log } = args;
const incomingMsgs = incomingCoreMessages(protocol, parsed);
if (!incomingMsgs) {
log("info", `[fork-adoption] ${protocol} anonymous fork of ${parentId}: no adoption support in v1, starting fresh (#629)`);
return;
}
let parent = peekSession(parentId);
if (!parent) parent = getStore().loadSync(parentId, { protocol, upstreamOrigin }) ?? undefined;
if (!parent) parent = (await getStore().loadAsync(parentId, { protocol, upstreamOrigin })) ?? undefined;
if (!parent) {
log("info", `[fork-adoption] fork parent ${parentId} not found (evicted and not persisted); starting fresh (#629)`);
return;
Expand Down Expand Up @@ -516,10 +518,12 @@ export function maybeAdoptDerivedFork(args: {
* (the parent id came from the plugin register) and belongs with the
* byte-prefix strong path, which is on by default.
*/
export function reconcileDerivedLineage(
// #4942: async — parent loads below must reach the DB on network storage
// backends, or lineage of DB-only parents is never restored at boot.
export async function reconcileDerivedLineage(
sessions: Session[],
opts: { log: (level: string, msg: string) => void; adoptionEnabled: boolean },
): number {
): Promise<number> {
const { log, adoptionEnabled } = opts;
if (!adoptionEnabled) return 0;
let adopted = 0;
Expand All @@ -534,10 +538,10 @@ export function reconcileDerivedLineage(

let parent = peekSession(parentId);
if (!parent) {
parent = getStore().loadSync(parentId, {
parent = (await getStore().loadAsync(parentId, {
protocol: s.meta.protocol,
upstreamOrigin: s.meta.upstreamOrigin,
}) ?? undefined;
})) ?? undefined;
}
if (!parent) {
// Evicted before it was persisted — or restored later. Leave the
Expand Down
98 changes: 80 additions & 18 deletions src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,13 @@ export class SessionStore {
* failed to — the sidecar just stays undefined). Resolved immediately
* for sqlite/legacy boots. */
private readonly mysqlReady: Promise<void> = Promise.resolve();
/** Await the storage backend before first use (mysql pool + DDL). */
/** #4942: true when a network backend (mysql) was selected for
* unified-full mode — the sidecar attaches asynchronously, so before
* ready() resolves this.shadow is still undefined while the DB already
* carries rows. */
private readonly networkFullExpected: boolean = false;
/** Await the storage backend before first use (mysql pool + DDL). openAsync
* never rejects (it degrades to null), so awaiting this cannot throw. */
ready(): Promise<void> {
return this.mysqlReady;
}
Expand Down Expand Up @@ -484,7 +490,7 @@ export class SessionStore {
private readonly capWarnAt = new Map<string, number>();
private readonly debounceMs: number;

constructor(opts?: { dir?: string; debounceMs?: number; enabled?: boolean; log?: Logger }) {
constructor(opts?: { dir?: string; debounceMs?: number; enabled?: boolean; log?: Logger; shadow?: UnifiedShadow }) {
const debounceMs = opts?.debounceMs ?? defaultDebounce();
this.enabled = (opts?.enabled ?? true) && debounceMs >= 0;
this.debounceMs = Math.max(0, debounceMs);
Expand Down Expand Up @@ -541,17 +547,18 @@ export class SessionStore {
if (wantJsonWriterOff && !this.jsonWriterOff) {
baseLog("warn", "[storage] storage.jsonWriter=off ignored — requires storage.unified=full on the sqlite backend (network backends keep the JSON envelope as the sync read path)");
}
const legacyLoad = (rec: unknown): LegacySessionLike | null => {
const s = buildSession(rec as PersistedSession);
return toLegacyLike(s);
};
const legacyLoad = recordToLegacyView;
// #3931 step 3: network backends (mysql) can only open asynchronously
// (pool + DDL), so the sidecar attaches when it resolves — until then
// this.shadow stays undefined and every surface keeps its documented
// no-sidecar posture (envelope reads/writes, banner, replicas off).
// `ready()` gates the async consumers (saves, boot, admin).
const backendName = resolveBackendName(process.env, fileStorageBackend());
if (this.enabled && !unifiedBlockedByEncryption && backendName === "mysql") {
if (opts?.shadow !== undefined) {
// #4942 test seam: a pre-built sidecar skips the env-driven open.
this.shadow = opts.shadow;
this.networkFullExpected = !!(opts.shadow.readsUnified && opts.shadow.isAsyncBackend);
} else if (this.enabled && !unifiedBlockedByEncryption && backendName === "mysql") {
this.mysqlReady = UnifiedShadow.openAsync(
this.sessionsDir,
process.env,
Expand All @@ -564,6 +571,7 @@ export class SessionStore {
this.shadow = sh ?? undefined;
});
this.shadow = undefined;
this.networkFullExpected = storageMode === "full";
} else {
if (this.enabled && !unifiedBlockedByEncryption) {
// #5188: remember the open args when this sync attempt actually
Expand Down Expand Up @@ -765,11 +773,14 @@ export class SessionStore {
// #2960 R2.1 boot union (full mode): rows whose legacy file is gone
// (deleted, or written by a Phase 3 future that stops writing JSON)
// remain enumerable — the index is self-sufficient. Shadow stays
// file-walk: there JSON is the truth base.
if (this.shadow?.readsUnified) {
for (const id of this.shadow.listSessionIds()) {
// file-walk: there JSON is the truth base. #4942: on network backends
// the sidecar attaches asynchronously and the sync facade misses by
// design — enumerate through the async twins instead.
if (this.shadow?.readsUnified || this.networkFullExpected) {
if (this.networkFullExpected) await this.ready();
for (const id of await this.unionIds()) {
if (out.has(id)) continue;
const session = this.loadSync(id);
const session = await this.loadAsync(id);
if (session) out.set(id, session);
}
}
Expand Down Expand Up @@ -798,11 +809,13 @@ export class SessionStore {
// without a legacy file stay enumerable (the index is
// self-sufficient). bootBounded (limits path) intentionally stays
// file-based: bounded boot is the cold-start triage path for huge
// legacy trees.
if (this.shadow?.readsUnified) {
for (const id of this.shadow.listSessionIds()) {
// legacy trees. #4942: network backends enumerate through the async
// twins (the sync facade misses by design there).
if (this.shadow?.readsUnified || this.networkFullExpected) {
if (this.networkFullExpected) await this.ready();
for (const id of await this.unionIds()) {
if (out.has(id)) continue;
const session = this.loadSync(id);
const session = await this.loadAsync(id);
if (session) out.set(id, session);
}
}
Expand All @@ -821,6 +834,21 @@ export class SessionStore {
return false;
}

/** #4942: enumeration for the boot/loadAll unions. sqlite keeps the sync
* listSessionIds(); network backends enumerate through sessionSummaries —
* one query carries both the ids and updatedAt, so a budget-bounded
* pre-warm can order newest-first (matching the savedAt-desc arbitration
* of the file path) instead of alphabetical row order. Both sources
* return the SAME row set (neither filters archived tombstones). */
private async unionIds(): Promise<string[]> {
const shadow = this.shadow;
if (!shadow) return [];
if (!shadow.isAsyncBackend) return shadow.listSessionIds();
const rows = await shadow.sessionSummariesAsync();
rows.sort((a, b) => b.updatedAt - a.updatedAt);
return rows.map((r) => r.sessionId);
}

private async bootBounded(limits: { maxSessions: number; maxBytes: number }): Promise<Map<string, Session>> {
if (!Number.isFinite(limits.maxSessions) || limits.maxSessions < 0 || !Number.isFinite(limits.maxBytes) || limits.maxBytes < 0) {
throw new Error("Invalid session boot limits");
Expand Down Expand Up @@ -936,15 +964,19 @@ export class SessionStore {
// The pure-index ids that do enter are measured by their SERIALIZED
// record — the same proxy readRawFileWithBytes gives the file path
// (#2803: the budget is a RAM proxy, not a disk one).
if (this.shadow?.readsUnified) {
// #4942: network backends pre-warm through the async twins under the
// SAME budget (the sync facade misses by design there); newest-first
// ordering comes from unionIds.
if (this.shadow?.readsUnified || this.networkFullExpected) {
if (this.networkFullExpected) await this.ready();
let warmBytes = 0;
for (const item of warm) warmBytes += item.bytes;
for (const id of this.shadow.listSessionIds()) {
for (const id of await this.unionIds()) {
if (out.size >= limits.maxSessions) break;
if (warmBytes >= limits.maxBytes) break;
if (out.has(id)) continue;
if (this.stateFileOnDisk(id)) continue;
const session = this.loadSync(id);
const session = await this.loadAsync(id);
if (!session) continue;
const bytes = this.estimateDecodedBytes(session);
if (warmBytes + bytes > limits.maxBytes) continue;
Expand Down Expand Up @@ -1665,6 +1697,28 @@ export class SessionStore {
return session;
}

/** #4942: async reload seam for network backends — on mysql the sync
* facade has NO read path (viewFor/listSessionIds miss by design, #3931
* step 3), so a cold load with no local envelope used to return null and
* the request path silently started a fresh session, losing every folded
* block at each restart. Serves exactly what loadSync serves whenever
* loadSync finds something (zero behavior change); only when the sync
* path has no copy does it fall to the DB row via viewForAsync.
* Envelope-first precedence is deliberate: envelope writes are
* synchronous per save while DB writes ride the coalesced record queue,
* so an existing envelope is never older than the DB in single-writer
* deployments. Null when neither copy exists. */
async loadAsync(id: string, meta?: { protocol?: string; upstreamOrigin?: string }): Promise<Session | null> {
const sync = this.loadSync(id, meta);
if (sync) return sync;
const shadow = this.shadow;
if (!shadow || !shadow.readsUnified || !shadow.isAsyncBackend) return null;
await this.ready();
const view = await shadow.viewForAsync(id);
if (!view) return null;
return this.afterLoad(id, view);
}

/** Shared post-load fixups for the unified read path: rebuilds the
* Session from a store view (viewToRecord → buildSession, the same
* forward-compat pass as a legacy load) and applies the #408 clamp
Expand Down Expand Up @@ -3473,6 +3527,14 @@ function restoreBlockContents(entries: Iterable<readonly [string, unknown]>): Ma
return blockContents;
}

/** #4942: the record→legacy-view adapter the sidecar ingest path runs on
* every committed record — exported so the hermetic seam suite can build a
* sidecar against a stub backend with the SAME adapter the store uses. */
export function recordToLegacyView(record: unknown): LegacySessionLike | null {
const s = buildSession(record as PersistedSession);
return toLegacyLike(s);
}

function buildSession(parsed: PersistedSession): Session {
const blockContents = restoreBlockContents(Object.entries(parsed.blockContents ?? {}));
// Read grouped shape (v2+); fall back to flat fields for v1 files.
Expand Down
Loading
Loading