Skip to content
Open
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
9 changes: 9 additions & 0 deletions dsh.bundle.patch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
# disable the bili-compaction-gate entry itself: it carries the service the
# presets wait on; to uninstall, remove billion-context from
# dsh.profile.bundles instead.
# #6956: the dsh PLUGIN-MARKET "disable" toggle kills BOTH entries (bili-native
# AND the gate) at once — the gate service then never registers, the presets'
# inject [biliBasicCompactionPolicy] latch is unsatisfiable, and the host
# reports preset-ptc (the default) as not-found, bricking new-conversation
# init. That is a DSH-side defect (an unsatisfiable inject must degrade, not
# hang); the in-repo surface is this note + the docs/clients.md "Disabling vs
# removing" pointer. The supported off-switches are: disable bili-native ONLY
# (gate stays alive, #3662) or remove billion-context from bundles (uninstall,
# #6972).
- insert:
- id: bili-native
name: billion-context
Expand Down
28 changes: 21 additions & 7 deletions src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,40 +515,54 @@ export interface RawBodyBreakdown {
tools: number;
system: number;
other: number;
/** #6957: the top-level field(s) carrying the "other" mass, sorted desc by
* tokens. Empty when "other" is 0 or the body is not a top-level object.
* Makes a 1.6M-token "other" actionable: the operator sees WHICH field. */
otherTop: { field: string; tokens: number }[];
}

export function estimateRawBodyBreakdown(parsed: unknown): RawBodyBreakdown {
const buckets: [number, number, number, number] = [0, 0, 0, 0]; // messages, tools, system, other
const pending: [unknown, string | undefined, number][] = [];
const otherTop = new Map<string, number>();
const pending: [unknown, string | undefined, number, string | undefined][] = [];
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
for (const [k, v] of Object.entries(parsed as Record<string, unknown>)) {
if (isNonModelTopLevelKey(k)) continue;
const bucket = k === "messages" ? 0 : k === "tools" ? 1 : k === "system" ? 2 : 3;
pending.push([v, k, bucket]);
pending.push([v, k, bucket, bucket === 3 ? k : undefined]);
}
} else {
pending.push([parsed, undefined, 3]);
pending.push([parsed, undefined, 3, undefined]);
}
while (pending.length > 0) {
const [value, key, bucket] = pending.pop()!;
const [value, key, bucket, topField] = pending.pop()!;
if (typeof value === "string") {
if (!key || !NON_TEXT_BODY_KEYS.has(key)) buckets[bucket] += defaultCountTokens(value);
if (!key || !NON_TEXT_BODY_KEYS.has(key)) {
const t = defaultCountTokens(value);
buckets[bucket] += t;
if (topField !== undefined) otherTop.set(topField, (otherTop.get(topField) ?? 0) + t);
}
continue;
}
if (Array.isArray(value)) {
for (const item of value) pending.push([item, key, bucket]);
for (const item of value) pending.push([item, key, bucket, topField]);
continue;
}
if (value && typeof value === "object") {
for (const [k, v] of Object.entries(value as Record<string, unknown>)) pending.push([v, k, bucket]);
for (const [k, v] of Object.entries(value as Record<string, unknown>)) pending.push([v, k, bucket, topField]);
}
}
const otherTopArr = [...otherTop.entries()]
.filter(([, t]) => t > 0)
.sort((a, b) => b[1] - a[1])
.map(([field, tokens]) => ({ field, tokens }));
return {
total: buckets[0] + buckets[1] + buckets[2] + buckets[3],
messages: buckets[0],
tools: buckets[1],
system: buckets[2],
other: buckets[3],
otherTop: otherTopArr,
};
}

Expand Down
11 changes: 10 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3787,7 +3787,16 @@ interface MeasuredOutbound {
// operator WHERE (a 55.2 MiB #5000 body with 1.44 MiB of messages[] means the
// mass is tools[], not a giant paste).
export function measuredBreakdownClause(b: RawBodyBreakdown): string {
return `messages ~${b.messages.toLocaleString("en-US")} · tools[] ~${b.tools.toLocaleString("en-US")} · system ~${b.system.toLocaleString("en-US")} · other ~${b.other.toLocaleString("en-US")}`;
let s = `messages ~${b.messages.toLocaleString("en-US")} · tools[] ~${b.tools.toLocaleString("en-US")} · system ~${b.system.toLocaleString("en-US")} · other ~${b.other.toLocaleString("en-US")}`;
// #6957: when "other" carries real mass, name the specific top-level field(s)
// so the operator knows WHERE to look — a 1.6M-token "other" is only actionable
// if it says which field (the dsh→official-DeepSeek wire put the mass in a
// top-level field bili's messages-only compression can never touch).
if (b.other > 0 && b.otherTop.length > 0) {
const top = b.otherTop.slice(0, 3).map((e) => `${e.field} ~${e.tokens.toLocaleString("en-US")}`).join(", ");
s += ` (other: ${top})`;
}
return s;
}

export function measuredOutbound(prepared: { body: string | Buffer; parsedBody?: unknown }): MeasuredOutbound | undefined {
Expand Down
2 changes: 1 addition & 1 deletion tests/raw-body-walk-depth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ test("shallow plumbing: exclusion set, key inheritance and bucket split stay int

test("non-object top-level bodies land whole in the other bucket", () => {
const bd = estimateRawBodyBreakdown("plain string body");
assert.deepEqual(bd, { total: t("plain string body"), messages: 0, tools: 0, system: 0, other: t("plain string body") });
assert.deepEqual(bd, { total: t("plain string body"), messages: 0, tools: 0, system: 0, other: t("plain string body"), otherTop: [] });
assert.equal(estimateRawBodyTokens("plain string body"), t("plain string body"));
});
48 changes: 47 additions & 1 deletion tests/side-request-isolation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from "node:assert/strict";
import http from "node:http";
import { once } from "node:events";
import { defaultConfig, createInitialState, defaultCountTokens } from "acp-kernel";
import { startServer, isSideRequest, isStructuredOutputCall, outputBudgetField, restoreOutputBudget, sideRequestGuard, resolveKnownOutputCeiling, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR } from "../src/server.ts";
import { startServer, isSideRequest, isStructuredOutputCall, outputBudgetField, restoreOutputBudget, sideRequestGuard, resolveKnownOutputCeiling, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR, measuredBreakdownClause } from "../src/server.ts";
import { resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts";
import type { ProxyOptions } from "../src/config.ts";
import { recordPluginRuntimeInfo, _resetPluginStateForTest } from "../src/plugin.ts";
Expand Down Expand Up @@ -618,6 +618,52 @@ test("estimateRawBodyBreakdown: splits the same total on the top-level field (#5
assert.equal(estimateRawBodyBreakdown("plain string").total, defaultCountTokens("plain string"), "bare string counts as other");
});

test("estimateRawBodyBreakdown.otherTop: names the specific top-level field carrying the 'other' mass (#6957)", () => {
// The #6957 shape: a body whose mass rides a top-level field OTHER than
// messages/tools/system (the dsh→official-DeepSeek wire put ~1.6M tokens in
// an unknown top-level field). otherTop must name THAT field so the doom
// message is actionable.
const body = {
messages: [{ role: "user", content: "m".repeat(1_000) }],
tools: [{ type: "function", function: { name: "t", description: "d".repeat(1_000) } }],
system: "s".repeat(1_000),
// two "other" fields: the big one must sort first.
reasoning_history: "r".repeat(20_000),
metadata: "x".repeat(2_000),
};
const b = estimateRawBodyBreakdown(body);
assert.ok(b.other > 0, "the two extra top-level fields land in the other bucket");
assert.equal(b.otherTop.length, 2, "both 'other' top-level fields are named");
assert.equal(b.otherTop[0].field, "reasoning_history", "the bigger field sorts first");
assert.equal(b.otherTop[1].field, "metadata", "the smaller field follows");
assert.ok(b.otherTop[0].tokens > b.otherTop[1].tokens, "sorted desc by tokens");
// otherTop tokens sum to the other bucket (exhaustive within 'other').
assert.equal(b.otherTop.reduce((s, e) => s + e.tokens, 0), b.other, "otherTop is exhaustive over the other bucket");
// messages/tools/system fields never leak into otherTop.
assert.ok(!b.otherTop.some((e) => e.field === "messages" || e.field === "tools" || e.field === "system"), "named buckets stay out of otherTop");
// a body with no 'other' mass has an empty otherTop.
const noOther = estimateRawBodyBreakdown({ messages: [{ role: "user", content: "m" }], system: "s" });
assert.equal(noOther.otherTop.length, 0, "no other mass ⇒ empty otherTop");
// a null body has an empty otherTop (no top-level object to name).
assert.equal(estimateRawBodyBreakdown(null).otherTop.length, 0, "null body ⇒ empty otherTop");
});

test("measuredBreakdownClause: names the oversized 'other' field in the doom message (#6957)", () => {
// #6957: the doom message must say WHICH top-level field carries the "other"
// mass, so "trim the oversized dimension" is actionable.
const b = estimateRawBodyBreakdown({
messages: [{ role: "user", content: "m".repeat(1_000) }],
system: "s".repeat(1_000),
reasoning_history: "r".repeat(20_000),
});
const clause = measuredBreakdownClause(b);
assert.ok(clause.includes("other ~"), "the other bucket is still reported");
assert.ok(clause.includes("reasoning_history"), "the oversized top-level field is named");
// a body with no "other" mass gets no "(other: …)" suffix.
const noOther = estimateRawBodyBreakdown({ messages: [{ role: "user", content: "m" }], system: "s" });
assert.ok(!measuredBreakdownClause(noOther).includes("(other:"), "no other mass ⇒ no field suffix");
});

test("inspectContextOverflow: exceed_context_size_error pattern + (A / B > W) window parse (#554)", () => {
const llama = JSON.stringify({ error: { message: "exceed_context_size_error (198,277 / 198,661 > 150,528)" } });
const hit = inspectContextOverflow(400, llama);
Expand Down
Loading