Skip to content
Merged
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
8 changes: 8 additions & 0 deletions src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,9 +988,17 @@ function maybeRetry(ctx: PluginContext): void {
state.respawn = respawn;
}
register.retryAt = Date.now() + retryIntervalMs();
// #3616: same supersession rule as bootstrap()'s own guard — a
// self-heal flight that started in a previous origin world must not
// bind its (possibly stale) origin onto the current world's register
// when it lands. bootstrap()'s `if (gen !== originGen) return
// handle.origin` only covers its OWN writes; this .then wrote the
// returned origin unconditionally, so the guard was bypassed here.
const gen = originGen;
void trackChain(respawn())
.then((origin) => {
if (origin === undefined) return;
if (gen !== originGen) return;
register.base = origin;
state.origin = origin;
void registerTools(ctx).catch(() => {});
Expand Down
10 changes: 7 additions & 3 deletions src/external-summary-credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { closeSync, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readF
import { dirname } from "node:path";
import { randomUUID } from "node:crypto";
import { configFile } from "./paths.js";
import { loadNamedProviders, loadRoutes, safeReadJson, type NamedProviderRecipe } from "./config.js";
import { loadConfigFile, loadNamedProviders, loadRoutes, type NamedProviderRecipe } from "./config.js";
import { expandExternalSummaryChain, parseExternalSummaryChain, validSummaryCredentialName } from "./external-summary-settings.js";

function validKey(value: unknown): value is string {
Expand Down Expand Up @@ -135,8 +135,12 @@ export function auditExternalSummaryCredentials(block: unknown, recipes: Record<
* because one file object can be effective for many slots. Returns
* human-readable problem lines (empty when everything resolves). */
export function auditStartupSummaryCredentials(env: NodeJS.ProcessEnv = process.env): string[] {
const file = safeReadJson(configFile());
const compress = file && typeof file === "object" && !Array.isArray(file) ? (file as { compress?: unknown }).compress : undefined;
// #3572/#3488: read the global block through loadConfigFile()'s tolerant
// JSONC parser (the same one the runtime loader uses). safeReadJson is
// strict JSON — a commented config silently evaluated to `file === null`
// here, skipping the boot audit for every global externalSummary chain
// while route-level chains (loadRoutes → loadConfigFile) still audited.
const compress = loadConfigFile().compress;
const globalBlock = compress && typeof compress === "object" && !Array.isArray(compress) ? (compress as { externalSummary?: unknown }).externalSummary : undefined;
const routes = loadRoutes(env);
const recipes = loadNamedProviders(env);
Expand Down
21 changes: 12 additions & 9 deletions src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1160,21 +1160,24 @@ export class SessionStore {
* guess into a one-line diagnosis. refs>0 with zero blocks is the
* incident signature (#3573): identity loaded but every fold missing, so
* the full history replays and preflight must rebuild from scratch.
* savedTok (stats.tokensSaved) disambiguates that signature in one grep:
* >0 proves compression ran in this session's lifetime, so zero blocks
* means folds existed and are now missing (investigate); 0 means the
* session was likely never folded (raw replay is its normal shape). The
* warn stays broad on purpose — a loss mode that erases stats must not
* be able to go silent again. */
* compressions (state.stats.compressionCount) disambiguates that
* signature in one grep: >0 proves compression ran in this session's
* lifetime, so zero blocks means folds existed and are now missing
* (investigate); 0 means the session was likely never folded (raw replay
* is its normal shape). (#2734 removed the last writer of the legacy
* stats.tokensSaved counter — reading it here logged a dead 0 and broke
* exactly this disambiguation, so the diagnostic now reads the kernel's
* live counter instead.) The warn stays broad on purpose — a loss mode
* that erases stats must not be able to go silent again. */
private logColdLoad(id: string, source: string, session: Session, savedAt?: number): void {
const refs = Object.keys(session.state.messageRefs.byRef ?? {}).length;
const blocks = session.state.blocks.length;
const active = session.state.blocks.filter((b) => b.active).length;
const savedTok = session.stats.tokensSaved;
const compressions = session.state.stats?.compressionCount ?? 0;
const ageMin = savedAt ? Math.round(Math.max(0, Date.now() - savedAt) / 60000) : undefined;
this.log("info", `[persist] cold-loaded ${id} from ${source}: ${refs} ref(s), ${blocks} block(s) (${active} active), baseline ${session.stats.lastInputTokens} tok, savedTok ${savedTok}${ageMin !== undefined ? `, savedAt ~${ageMin} min ago` : ""}`);
this.log("info", `[persist] cold-loaded ${id} from ${source}: ${refs} ref(s), ${blocks} block(s) (${active} active), baseline ${session.stats.lastInputTokens} tok, compressions ${compressions}${ageMin !== undefined ? `, savedAt ~${ageMin} min ago` : ""}`);
if (refs > 0 && blocks === 0) {
this.log("warn", `[persist] ${id} loaded with ${refs} mapped ref(s) but NO persisted blocks (savedTok=${savedTok}) — the folded history will replay raw and preflight must rebuild from scratch (#3573)`);
this.log("warn", `[persist] ${id} loaded with ${refs} mapped ref(s) but NO persisted blocks (compressions=${compressions}) — the folded history will replay raw and preflight must rebuild from scratch (#3573)`);
}
}

Expand Down
122 changes: 117 additions & 5 deletions src/web/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,119 @@
import { MESSAGES } from "./i18n.js";

// #3572 (client half): the server accepts JSONC config files (comments +
// trailing commas) since the parseBiliConfig fix, but the web editor still
// strict-parses the draft text in five places — so a user with a JSONC config
// sees the quick-config panel vanish (#3488) and cannot save. These three pure
// helpers implement the same tolerance client-side; they live in the TS module
// (importable by tests) and are inlined into the browser IIFE below via
// toString(), so there is exactly one implementation.
export function stripJsoncComments(text: string): string {
let out = "";
let i = 0;
let inString = false;
while (i < text.length) {
const c = text.charAt(i);
const d = i + 1 < text.length ? text.charAt(i + 1) : "";
if (inString) {
out += c;
if (c === "\\") {
if (d !== "") {
out += d;
i += 2;
continue;
}
} else if (c === '"') inString = false;
i += 1;
continue;
}
if (c === '"') {
inString = true;
out += c;
i += 1;
continue;
}
// Comments become spaces (not deleted) so JSON.parse error offsets stay honest.
if (c === "/" && d === "/") {
while (i < text.length && text.charAt(i) !== "\n") {
out += " ";
i += 1;
}
continue;
}
if (c === "/" && d === "*") {
while (i < text.length && !(text.charAt(i) === "*" && text.charAt(i + 1) === "/")) {
out += text.charAt(i) === "\n" ? "\n" : " ";
i += 1;
}
out += " "; // the closing */ — kept as spaces so offsets stay honest
i += 2;
continue;
}
out += c;
i += 1;
}
return out;
}

// Second pass, string-aware: drop a comma only when the next meaningful char
// closes the current object/array. Run AFTER stripJsoncComments so commas and
// closers inside string literals are the only ones left to protect.
export function stripJsoncTrailingCommas(text: string): string {
let out = "";
let i = 0;
let inString = false;
while (i < text.length) {
const c = text.charAt(i);
if (inString) {
out += c;
if (c === "\\") {
const d = i + 1 < text.length ? text.charAt(i + 1) : "";
if (d !== "") {
out += d;
i += 2;
continue;
}
} else if (c === '"') inString = false;
i += 1;
continue;
}
if (c === '"') {
inString = true;
out += c;
i += 1;
continue;
}
if (c === ",") {
let j = i + 1;
while (j < text.length && " \t\r\n".includes(text.charAt(j))) j += 1;
if (j < text.length && (text.charAt(j) === "}" || text.charAt(j) === "]")) {
i += 1;
continue;
}
}
out += c;
i += 1;
}
return out;
}

// Strict JSON first so a valid file takes the exact same path as before;
// fall back to the JSONC tolerance above; a genuinely malformed draft still
// throws, and the server's parseBiliConfig remains the authoritative check on save.
export function parseConfigDraft(text: string): unknown {
try {
return JSON.parse(text);
} catch {
// fall through to the tolerant path
}
return JSON.parse(stripJsoncTrailingCommas(stripJsoncComments(text)));
}

export const WEB_CLIENT = `(function () {
"use strict";
const stripJsoncComments = ${stripJsoncComments.toString()};
const stripJsoncTrailingCommas = ${stripJsoncTrailingCommas.toString()};
const parseConfigDraft = ${parseConfigDraft.toString()};
const MESSAGES=${JSON.stringify(MESSAGES)};
let locale = "zh-CN";
try {
Expand Down Expand Up @@ -2202,7 +2314,7 @@ export const WEB_CLIENT = `(function () {
box.innerHTML = "";
const fe = $("cfg-file-edit");
let draft = {};
try { draft = JSON.parse(fe && fe.value ? fe.value : "{}"); if (!draft || typeof draft !== "object" || Array.isArray(draft)) draft = {}; } catch (e) { return; }
try { const p = parseConfigDraft(fe && fe.value ? fe.value : "{}"); draft = p && typeof p === "object" && !Array.isArray(p) ? p : {}; } catch (e) { return; }
function compressOf(d) { return (d.compress && typeof d.compress === "object" && !Array.isArray(d.compress)) ? d.compress : null; }
const NUDGE_DEFAULT = 50000, NUDGE_STEP = 5000, NUDGE_LOW = 20000, NUDGE_HIGH = 100000;
const PRM_KERNEL_DEFAULT = 3;
Expand All @@ -2216,7 +2328,7 @@ export const WEB_CLIENT = `(function () {
}
function freshDraft() {
try {
const p = JSON.parse(fe && fe.value ? fe.value : "{}");
const p = parseConfigDraft(fe && fe.value ? fe.value : "{}");
if (!p || typeof p !== "object" || Array.isArray(p)) return {};
return p;
} catch (e) { quickBroken(true); return null; }
Expand Down Expand Up @@ -2554,7 +2666,7 @@ export const WEB_CLIENT = `(function () {
// #3488: five-state view; falls back to the legacy boolean on old servers.
const detail = cfg.externalSummaryCredentialStatus || {};
function read() {
const draft = JSON.parse(fe.value || "{}");
const draft = parseConfigDraft(fe.value || "{}");
if (!draft || typeof draft !== "object" || Array.isArray(draft)) throw new Error(t("cfg.invalid_json"));
return draft;
}
Expand Down Expand Up @@ -2899,7 +3011,7 @@ export const WEB_CLIENT = `(function () {
const el = $("cfg-file-edit");
const raw = el ? el.value : "";
let parsed;
try { parsed = JSON.parse(raw || "{}"); } catch (e) { toast(t("cfg.invalid_json"), "err"); return; }
try { parsed = parseConfigDraft(raw || "{}"); } catch (e) { toast(t("cfg.invalid_json"), "err"); return; }
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) { toast(t("cfg.invalid_json"), "err"); return; }
await putCfg(sq, { file: raw });
});
Expand All @@ -2911,7 +3023,7 @@ export const WEB_CLIENT = `(function () {
const el = $("cfg-file-edit");
const raw = el ? el.value : "";
let parsed;
try { parsed = JSON.parse(raw || "{}"); } catch (e) { toast(t("cfg.invalid_json"), "err"); return; }
try { parsed = parseConfigDraft(raw || "{}"); } catch (e) { toast(t("cfg.invalid_json"), "err"); return; }
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) { toast(t("cfg.invalid_json"), "err"); return; }
await putCfg(sf, { file: raw });
});
Expand Down
46 changes: 45 additions & 1 deletion tests/dsh-native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import os from "node:os";
import path from "node:path";
import http from "node:http";
import { pathToFileURL } from "node:url";
import { apply, planNativeDsh, shouldBootstrapNativeDsh, persistClientEvent, _attributionHealthForTest, _resetAttributionHealthForTest, _resetRegisterForTest, _setModelInfoForTest, _setSpawnForTest, _setThrewRetryForTest, _settleNativeForTest, _stateHeadersForTest, _stateRespawnForTest, _stateTakeoverGateForTest, _stateToolsReadyForTest, _stateBeforeSendForTest, _stateOnResponseForTest, _noteRoutedForTest, _resetRoutedForTest, _resetWebProfileWarningForTest, _resetTitlesForTest, type PluginContext } from "../src/agent/dsh-native.ts";
import { apply, planNativeDsh, shouldBootstrapNativeDsh, persistClientEvent, _attributionHealthForTest, _resetAttributionHealthForTest, _registerStateForTest, _resetRegisterForTest, _setModelInfoForTest, _setSpawnForTest, _setThrewRetryForTest, _settleNativeForTest, _stateHeadersForTest, _stateRespawnForTest, _stateTakeoverGateForTest, _stateToolsReadyForTest, _stateBeforeSendForTest, _stateOnResponseForTest, _noteRoutedForTest, _resetRoutedForTest, _resetWebProfileWarningForTest, _resetTitlesForTest, type PluginContext } from "../src/agent/dsh-native.ts";
import { __resetIncrementalClientForTests } from "../src/agent/incremental-client.ts";
import { BILI_COMPACTION_DISABLE_SERVICE } from "../src/agent/dsh-compaction-disable.ts";
import { once } from "node:events";
Expand Down Expand Up @@ -776,6 +776,50 @@ test("#3616 apply() attach mode: a superseded verify landing must not revert the
}
});

// #3616 (maybeRetry self-heal): the base-less respawn flight has the same
// supersession rule as bootstrap()'s own guard — pre-fix its .then wrote
// register.base/state.origin unconditionally, so a flight started in world A
// that landed after a re-activation (world B) bound the STALE origin onto the
// new world's register, exactly the bypass #3616 closed everywhere else.
test("#3616 maybeRetry self-heal: a superseded respawn flight must not bind its stale origin", async () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-selfheal-"));
try {
await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: undefined, BILI_DSH_RECOVERY_INTERVAL_MS: "5", BILI_PROVIDER_REWRITES: undefined, BILLION_CONTEXT_PLUGIN: undefined, BILI_NATIVE_DSH: undefined, BILI_MITM_HOSTS: undefined }, async () => {
_resetRegisterForTest(undefined);
let started = false;
let release: (() => void) | undefined;
const gate = new Promise<void>((resolve) => {
release = resolve;
});
_setSpawnForTest(() => {
started = true;
return gate.then(() => "http://stale-origin:1");
});
try {
const ctx1 = mockCtx();
apply(ctx1);
// World A is base-less: the recovery timer's first tick arms the
// self-heal respawn (our stub) and leaves it IN FLIGHT on the gate.
await waitFor(() => started, "self-heal respawn flight started");
// World switch: a re-activation bumps originGen and owns the
// register with a fresh base.
_resetRegisterForTest("http://new-origin:1");
release?.();
await _settleNativeForTest();
// The stale flight landed AFTER the switch — it must write
// nothing. Pre-fix register.base reverted to http://stale-origin:1.
assert.equal(_registerStateForTest().base, "http://new-origin:1");
} finally {
release?.();
_setSpawnForTest(undefined);
_resetRegisterForTest(undefined);
}
});
} finally {
rmrf(home);
}
});

test("apply() /acp-cache (#1146): forwards acp_cache bound to the initiator session, falls back to latest", async () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-cache-"));
try {
Expand Down
22 changes: 22 additions & 0 deletions tests/issue3488-credential-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,28 @@ test("startup audit walks the global/route/model cascade and reports each effect
assert.deepEqual(auditStartupSummaryCredentials({ AUDIT_A: "key-a", AUDIT_B: "key-b" }), []);
});

// #3572/#3488: the startup audit reads the GLOBAL block through
// loadConfigFile()'s tolerant JSONC parser. Pre-fix it used strict safeReadJson,
// so a commented config evaluated to null and every global externalSummary
// chain silently skipped the boot audit (route chains still audited — the
// asymmetry masked the bug).
test("startup audit still walks the global chain when the config file is JSONC", () => {
writeFileSync(biliConfig, [
"{ // commented config — valid JSONC, valid at runtime (#3572)",
" \"providers\": {",
" \"nexus\": { \"baseUrl\": \"http://127.0.0.1:9/v1\", \"api\": \"openai\", \"apiKeyEnv\": \"AUDIT_A\", \"models\": { \"free\": {} } },",
" },",
" \"compress\": { \"externalSummary\": { \"enabled\": true, \"targets\": [\"nexus/free\"] }, }, // trailing comma",
"}",
].join("\n") + "\n", "utf8");

const problems = auditStartupSummaryCredentials({});
assert.equal(problems.length, 1, `the global chain must still be audited (got: ${JSON.stringify(problems)})`);
assert.ok(problems[0].includes("AUDIT_A is not set"));

assert.deepEqual(auditStartupSummaryCredentials({ AUDIT_A: "key-a" }), []);
});

function captureConfigGet(): Promise<Record<string, unknown>> {
return new Promise((resolve, reject) => {
const res = {
Expand Down
Loading
Loading