Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
5 changes: 4 additions & 1 deletion src/dsh-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,10 @@ export function planDshSpawn(
"Set BILI_DSH_BIN to the dsh executable, or reword the argument.",
);
});
const comspec = env.COMSPEC?.trim() || "cmd.exe";
// #4599: sanitized env copies keep original key casing (`ComSpec`) — an
// exact `env.COMSPEC` probe misses it on plain objects.
const comspecRaw = env.COMSPEC ?? env.ComSpec ?? env.comspec;
const comspec = comspecRaw?.trim() || "cmd.exe";
const line = `"${[command, ...args].map(quoteWinToken).join(" ")}"`;
return { command: comspec, args: ["/d", "/s", "/c", line], windowsVerbatimArguments: true };
}
Expand Down
7 changes: 6 additions & 1 deletion src/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5418,7 +5418,12 @@ export function planClientSpawn(
"Set BILI_CLIENT_BIN to the native executable (e.g. the real codex.exe) or to a script entry run under node, or reword the argument.",
);
});
const comspec = nonEmpty(env.COMSPEC) ? env.COMSPEC : "cmd.exe";
// #4599: Windows env vars are case-insensitive, but sanitized copies
// (spread objects, e.g. stripInheritedProxy's inlineEnv) keep the original
// key casing — `ComSpec` — where an exact `env.COMSPEC` probe misses it
// and silently degrades to the bare-name fallback. Probe every spelling.
const comspecRaw = env.COMSPEC ?? env.ComSpec ?? env.comspec;
const comspec = nonEmpty(comspecRaw) ? comspecRaw : "cmd.exe";
return {
command: comspec,
args: ["/d", "/s", "/c", buildWindowsCommandLine(cmd, args)],
Expand Down
91 changes: 87 additions & 4 deletions src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,30 @@ export class SessionStore {
if (outcome.superseded.size > 0) {
session.restampSuperseded = outcome.superseded;
}
// The token snapshot is keyed by ref (#3488 render-refs): renumbered
// refs would otherwise inherit the stale count of whichever message
// held the new number pre-alignment — the wire tag then disagrees
// with the legacy-restart leg beyond citation renumbering (storage
// engine-proofs wire-parity, #4599). Two-pass moves: stage all
// (oldRef → newRef → count) first, then delete, then write — a
// single-pass swap would lose entries whose newRef is another
// move's oldRef.
const snap = (state as { tokenSnapshot?: Record<string, number> }).tokenSnapshot;
if (snap && outcome.superseded.size > 0) {
const byRawNow = state.messageRefs.byRaw;
const moves: Array<[oldRef: string, newRef: string, count: number]> = [];
for (const [oldRef, rawId] of outcome.superseded) {
const count = snap[oldRef];
const newRef = byRawNow[rawId];
if (count === undefined || newRef === undefined || newRef === oldRef) continue;
moves.push([oldRef, newRef, count]);
}
if (moves.length > 0) {
for (const [oldRef] of moves) delete snap[oldRef];
for (const [, newRef, count] of moves) snap[newRef] = count;
this.log("info", `[persist] remapped ${moves.length} token-snapshot entries onto ledger-aligned refs for ${session.id} (#4599)`);
}
}
state.refLedgerAligned = true;
this.log("info", `[persist] ledger-aligned refs for ${session.id}: kept=${outcome.kept} restamped=${outcome.restamped} rehomed=${outcome.rehomed} blocked=${outcome.blocked} superseded=${outcome.superseded.size} (#4316 PR3/3)`);
this.scheduleSave(session);
Expand Down Expand Up @@ -2560,6 +2584,63 @@ export class SessionStore {
this.dropResidentMetadata(session);
}

/** #4739: where this id's record lives RIGHT NOW — the one build-scoped
* source decision shared by both lazy-field restores after a save.
* Mirrors loadSync's routing exactly: no readable unified index (never
* opened, 3-strike disabled, shadow mode serving files) or a drift latch
* (the envelope outranks the index while it exists, #3135 ④) → file;
* else index. Bound at save time, deliberately NOT re-evaluated at
* restore time — a later racing writer converges on the next save, and
* re-routing mid-burst could flip the source between the two fields of
* one saved record. */
private lazySource(id: string): "index" | "file" {
const sh = this.shadow;
if (!sh || !sh.readsUnified || sh.stats.disabled) return "file";
if (sh.driftedSessions.has(id)) return "file";
return "index";
}

/** #4739: narrow metadata restore for the dropped-metadata proxy —
* single-column index read or single-file-field envelope read instead
* of the full-session loadSync rebuild. Tries the bound source first,
* then the other copy; null only when BOTH miss/fail (keeps the proxy's
* refuse-empty throw semantics). Never imports, never schedules a save,
* never touches the drift latch. */
private narrowMetadata(id: string, source: "index" | "file", meta?: { protocol?: string; upstreamOrigin?: string }): Record<string, unknown> | null {
const fromIndex = (): Record<string, unknown> | null => this.shadow?.getSessionMetadata(id) ?? null;
const fromFile = (): Record<string, unknown> | null => {
const env = this.loadEnvelope(id, meta);
return env ? (env.payload.metadata ?? {}) : null;
};
if (source === "index") {
const idx = fromIndex();
return idx !== null ? idx : fromFile();
}
const file = fromFile();
return file !== null ? file : fromIndex();
}

/** #4739: narrow blockContents restore for the dropped-blockContents
* proxy — block rows + orphans journal (index) or the persisted
* record's map (file), never message rows. Same bound-source-first /
* other-copy-fallback / refuse-empty contract as narrowMetadata. */
private narrowBlockContents(id: string, source: "index" | "file", meta?: { protocol?: string; upstreamOrigin?: string }): Record<string, BlockContent> | null {
const fromIndex = (): Record<string, BlockContent> | null => {
const rows = this.shadow?.getBlockContents(id);
return rows ? Object.fromEntries(rows) : null;
};
const fromFile = (): Record<string, BlockContent> | null => {
const env = this.loadEnvelope(id, meta);
return env ? (env.payload.blockContents ?? {}) : null;
};
if (source === "index") {
const idx = fromIndex();
return idx !== null ? idx : fromFile();
}
const file = fromFile();
return file !== null ? file : fromIndex();
}

/** #3050 R3 ②a: once a save has captured the raw tail (envelope + the
* unified messages table via the ⑤ rawTail override), an idle session
* keeps only the bounded folded view resident — the exact shape a
Expand Down Expand Up @@ -2587,9 +2668,10 @@ export class SessionStore {
// Hand-built sessions (tests, seeds) may carry no meta — the lazy
// loader degrades to the id-only lookup, same as a protocol-less file.
const meta = session.meta ?? undefined;
// #4739: one narrow read per field instead of a full-session rebuild.
const source = this.lazySource(session.id);
session.metadata = lazyMetadata(session.id, { protocol: meta?.protocol, upstreamOrigin: meta?.upstreamOrigin }, (id, m) => {
const loaded = this.loadSync(id, m);
return loaded ? loaded.metadata : null;
return this.narrowMetadata(id, source, m);
});
}

Expand All @@ -2606,9 +2688,10 @@ export class SessionStore {
return;
}
if (existing.size === 0) return;
// #4739: one narrow read per field instead of a full-session rebuild.
const source = this.lazySource(session.id);
const lazy = new LazyBlockContents(session.id, { protocol: session.meta.protocol, upstreamOrigin: session.meta.upstreamOrigin }, (id, meta) => {
const loaded = this.loadSync(id, meta);
return loaded ? Object.fromEntries(loaded.blockContents) : null;
return this.narrowBlockContents(id, source, meta);
});
lazy.drop();
session.blockContents = lazy;
Expand Down
8 changes: 8 additions & 0 deletions src/storage/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type {
SessionSummaryRow,
WebSummaryRow,
IngestSessionInput,
SessionViewRows,
} from "./store.js";
import type {
SessionRow,
Expand All @@ -44,6 +45,7 @@ import type {
SessionSummaryRow,
WebSummaryRow,
IngestSessionInput,
SessionViewRows,
} from "./store.js";

/** Everything a storage backend must provide. Mirrors the operation surface
Expand Down Expand Up @@ -74,6 +76,9 @@ export interface StorageBackend {
/** #4316 PR3/3: fork adoption row clone (idempotent). */
cloneSessionRows(parentId: string, childId: string, throughSeq: number): Promise<boolean>;
getMessages(sessionId: string): Promise<(MessageRow & { contentHash: string })[]>;
/** #4739: the viewFor bundle from ONE consistent read snapshot (messages
* fetched only at the tail/pluginSnapshot pointer seqs). */
sessionViewRows(sessionId: string): Promise<SessionViewRows>;
getBlocks(sessionId: string): Promise<(BlockRow & { summaryHash: string; oneHash: string | null })[]>;
getDeadRefs(sessionId: string): Promise<{ ord: number; ref: string; refNum: number; recordedAt: number }[]>;
getRefs(sessionId: string): Promise<RefRow[]>;
Expand Down Expand Up @@ -189,6 +194,9 @@ export class SqliteBackend implements StorageBackend {
getMessages(sessionId: string): Promise<(MessageRow & { contentHash: string })[]> {
return Promise.resolve(this.store.getMessages(sessionId));
}
sessionViewRows(sessionId: string): Promise<SessionViewRows> {
return Promise.resolve(this.store.sessionViewRows(sessionId));
}
getBlocks(sessionId: string): Promise<(BlockRow & { summaryHash: string; oneHash: string | null })[]> {
return Promise.resolve(this.store.getBlocks(sessionId));
}
Expand Down
47 changes: 46 additions & 1 deletion src/storage/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type { Pool, PoolConnection, PoolOptions } from "mysql2/promise";
import { mkdirSync, writeFileSync, appendFileSync, readFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { log as loggerLog } from "../logger.js";
import { sha256Hex } from "./store.js";
import { sha256Hex, viewPointerSeqs } from "./store.js";
import { identityOfStoredContent } from "./identity.js";
import type { MessageContentStore } from "acp-kernel";
import type { StorageBackend } from "./backend.js";
Expand All @@ -45,6 +45,7 @@ import type {
SessionSummaryRow,
WebSummaryRow,
IngestSessionInput,
SessionViewRows,
} from "./store.js";

/** The mysql2/promise driver module, injected by openBackend() at open time.
Expand Down Expand Up @@ -1007,6 +1008,50 @@ export class MysqlBackend implements StorageBackend {
};
}

/** #4739: bounded view bundle (messages only at tail/pluginSnapshot
* pointer seqs). Per-statement reads like today's viewForAsync — no
* cross-query snapshot — which is safe here because ingest writes each
* session row and its pointed-at message rows in ONE transaction, so
* every pointer ever read resolves to rows that already exist. The
* sqlite lane carries the explicit read-transaction instead. */
async sessionViewRows(sessionId: string): Promise<SessionViewRows> {
const session = await this.getSessionRow(sessionId);
if (!session) return null;
const pointers = viewPointerSeqs(session.tailJson);
const messages: (MessageRow & { contentHash: string })[] = [];
const CHUNK = 500;
for (let i = 0; i < pointers.length; i += CHUNK) {
const chunk = [...new Set(pointers.slice(i, i + CHUNK))];
if (chunk.length === 0) continue;
const [rows] = (await this.pool.query(
`SELECT seq, msg_id, ref, ref_num, role, kind, content_hash FROM messages
WHERE session_id = ? AND retired_at IS NULL AND seq IN (${chunk.map(() => "?").join(", ")})`,
[sessionId, ...chunk],
)) as unknown as Row[][];
for (const r of rows) {
const contentHash = str(r.content_hash);
messages.push({
seq: num(r.seq),
msgId: str(r.msg_id),
ref: nullableStr(r.ref),
refNum: nullableNum(r.ref_num),
role: str(r.role),
kind: str(r.kind),
contentHash,
content: await this.hydrateMediaAsync((await this.getContent(this.pool, contentHash)) ?? ""),
});
}
}
messages.sort((a, b) => a.seq - b.seq);
return {
session,
messages,
blocks: await this.getBlocks(sessionId),
deadRefs: await this.getDeadRefs(sessionId),
refs: await this.getRefs(sessionId),
};
}

async getMessages(sessionId: string, opts?: { includeRetired?: boolean }): Promise<(MessageRow & { contentHash: string; retiredAt: number | null })[]> {
// #4390: retired rows stay out of the replay by default.
const [rows] = (await this.pool.query(
Expand Down
54 changes: 36 additions & 18 deletions src/storage/shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,15 +509,11 @@ export class UnifiedShadow {
if (this.sync === null) return null;
if (this.stats.disabled) return null;
try {
const sessionRow = this.sync.getSessionRow(id);
if (!sessionRow) return null;
const rows = {
session: sessionRow,
messages: this.sync.getMessages(id),
blocks: this.sync.getBlocks(id),
deadRefs: this.sync.getDeadRefs(id),
refs: this.sync.getRefs(id),
};
// #4739: one consistent read snapshot; messages fetched ONLY at
// the tail/pluginSnapshot pointer seqs (the historical log is not
// decoded on ordinary loads).
const rows = this.sync.sessionViewRows(id);
if (!rows) return null;
// The view's `state` object carries every MERGE_STATE_FIELDS key by
// construction; the declared Record<string, unknown> just needs a
// widening cast to reach LegacySessionLike.
Expand Down Expand Up @@ -778,6 +774,33 @@ export class UnifiedShadow {
return this.sync.getCcr(sessionId);
}

/** #4739: narrow single-column metadata read for lazy-field restore.
* Full-trust only (shadow mode's index may lag its envelopes) — callers
* gate on readsUnified; network backends degrade to null (the caller
* falls back to the envelope, exactly like today's sync loadSync). */
getSessionMetadata(sessionId: string): Record<string, unknown> | null {
if (this.sync === null) return null;
if (this.stats.disabled) return null;
try {
return this.sync.getSessionMetadata(sessionId);
} catch {
return null;
}
}

/** #4739: narrow blockContents read for lazy-field restore (block rows +
* orphans journal, no message rows). Same trust/degrade contract as
* getSessionMetadata. */
getBlockContents(sessionId: string): import("./store.ts").BlockContentsMap | null {
if (this.sync === null) return null;
if (this.stats.disabled) return null;
try {
return this.sync.getBlockContents(sessionId);
} catch {
return null;
}
}

/** #4316 PR3/3: raw id → canonical seq (single-row ids only) for the
* one-time ledger re-stamp. Shadow mode's index may lag files it has
* not re-recorded, so like getCcr this is full-trust only: sync store
Expand Down Expand Up @@ -971,15 +994,10 @@ export class UnifiedShadow {
if (this.stats.disabled) return null;
try {
const backend = this.backend!;
const sessionRow = await backend.getSessionRow(id);
if (!sessionRow) return null;
const loaded = loadLegacyView({
session: sessionRow,
messages: await backend.getMessages(id),
blocks: await backend.getBlocks(id),
deadRefs: await backend.getDeadRefs(id),
refs: await backend.getRefs(id),
}) as LegacySessionLike;
// #4739: bounded view bundle (pointer-scoped message fetch).
const rows = await backend.sessionViewRows(id);
if (!rows) return null;
const loaded = loadLegacyView(rows) as LegacySessionLike;
warnTailGaps(id, loaded as unknown as { tailGaps?: number });
return loaded;
} catch (err) {
Expand Down
Loading
Loading