Skip to content

🤖 Open-source, local-first alternative to Grok Bot, OpenAI dots and Claude Duet. Your own AI bots on any model: GPT, Claude, Gemini, Grok, DeepSeek, Kimi, GLM and 70+ more through one Modelflare key. Reach them in the app, WeChat or Feishu; they remember you, work in the background, run commands and use your computer. Pay per use.

fix(agent): server compaction vs real Modelflare replies, Chat fallback and probe cost

Marla7b9f517ac7ad
diff --git a/src/agent/bridge.ts b/src/agent/bridge.ts
index 29d2573..9ff5236 100644
--- a/src/agent/bridge.ts
+++ b/src/agent/bridge.ts
@@ -155,7 +155,13 @@ export class AgentBridge {
res.on("close", () => ac.abort());
// 上游迟迟不回响应头时每分钟记一条:代理"一直转圈"多半是这里卡住(代理自己要等 5 分钟才判超时并重试)
const slow = setInterval(() => log.warn("model request still waiting for upstream response", { path: p, waitedMs: Date.now() - t0 }), 60_000);
- const send = (b: Buffer | undefined) => fetch(target, { method: req.method, headers, body: b ? new Uint8Array(b) : undefined, signal: ac.signal });
+ // 最后发出去的是不是 DSH 的原请求体:改写过(换上压缩历史)的请求被拒先由 checkRewritten 用原请求重发,
+ // 不报给代理接口回退(onRejection);只有原请求被拒才可能让这个模型改走 Chat
+ let unmodified = true;
+ const send = (b: Buffer | undefined) => {
+ unmodified = b === body;
+ return fetch(target, { method: req.method, headers, body: b ? new Uint8Array(b) : undefined, signal: ac.signal });
+ };
let plan: CompactionPlan | undefined;
if (body && req.method === "POST" && this.h.compaction) {
try {
@@ -186,13 +192,14 @@ export class AgentBridge {
const bot = typeof req.headers[BOT_HEADER] === "string" ? (req.headers[BOT_HEADER] as string) : undefined;
log.warn("model request rejected by upstream", { path: p, status: r.status, ms: Date.now() - t0, bot, body: error });
this.emitProxy({ phase: "response", id, path: p, status: r.status, ms: Date.now() - t0, error });
- for (const fn of this.rejectionListeners) {
- try {
- fn({ bot, path: p, status: r.status, error });
- } catch {
- /* ignore */
+ if (unmodified)
+ for (const fn of this.rejectionListeners) {
+ try {
+ fn({ bot, path: p, status: r.status, error });
+ } catch {
+ /* ignore */
+ }
}
- }
res.writeHead(r.status, out);
return void res.end(text);
}
@@ -218,7 +225,8 @@ export class AgentBridge {
/**
* 改写过(换上压缩历史)的请求:被拒(4xx/5xx,Key 与限流问题除外)或流一开头就报错时,用原请求重发一次,
- * 回合照常进行;server-compaction 据此记下这个模型不支持。
+ * 回合照常进行;server-compaction 据此记下这个模型不支持。这次被拒不算代理接口被拒(不触发改走 Chat),
+ * 原请求重发后再被拒才算。
*/
private async checkRewritten(plan: CompactionPlan, r: Response, original: () => Promise<Response>, p: string): Promise<Response> {
if (r.status >= 400 && r.status !== 401 && r.status !== 429) {
diff --git a/src/agent/server-compaction.ts b/src/agent/server-compaction.ts
index ceb317f..361b4c8 100644
--- a/src/agent/server-compaction.ts
+++ b/src/agent/server-compaction.ts
@@ -7,7 +7,8 @@
* 所在的 Modelflare 连接),记下"这段历史(逐项哈希)→ 压缩后的窗口",按会话存盘。之后同一会话的请求只要开头逐项
* 一致,就把这段换成压缩窗口再发(系统提示用当次请求的);有任何不一致就原样发。压缩不阻塞当次请求。
* - 价格目录列出 openai-response-compact 只是提示:每个模型(连同所用的 Key)先做一次很小的压缩 + 续接探测,通过了才用,
- * 结果缓存(内存 + 磁盘,会过期)。
+ * 结果缓存(内存 + 磁盘,会过期)。压缩调用要推理、要付费(实测 30–75 秒),所以目录没列出的不探测,同一模型 + Key
+ * 同时只探测一次,"不支持"的结论 7 天内不再探测。
* - 改写过的请求被拒(4xx、流一开头就报错)时,bridge 用原请求重发一次,这里记下该模型不支持。压缩项只发回产生它的
* 同一个模型 + 同一个 Key。AMB_SERVER_COMPACTION=0 整个关掉。
*
@@ -27,10 +28,11 @@ const log = logger("agent.compaction");
export const COMPACT_ENDPOINT = "openai-response-compact";
const DAY = 24 * 3600_000;
-/** 探测结果的有效期:支持 3 天,不支持 1 天(之后重新探测);临时失败 10 分钟后再试 */
+/** 探测结果的有效期:支持 3 天,不支持 7 天(之后重新探测);临时失败 10 分钟后再试,连着失败每次加倍,最多 6 小时 */
const OK_TTL = 3 * DAY;
-const NO_TTL = DAY;
+const NO_TTL = 7 * DAY;
const RETRY_MS = 10 * 60_000;
+const RETRY_MAX_MS = 6 * 3600_000;
/** 一个会话最多留几段映射;映射文件多久没动就删 */
const KEEP_MAPPINGS = 3;
const FILE_TTL = 14 * DAY;
@@ -122,17 +124,50 @@ interface SessionState {
quietUntil?: number;
}
+/** 回复里报的用量与费用(日志用):tokens、推理 tokens、cost_in_usd_ticks(原样) */
+export type CallUsage = Partial<Record<"input" | "output" | "reasoning" | "costTicks", number>>;
+
class CompactError extends Error {
constructor(
message: string,
/** 这个模型不支持(4xx、形状不对);否则是临时问题 */
readonly unsupported: boolean,
readonly status = 0,
+ /** 上游回了用量(这次调用照样计费) */
+ readonly usage?: CallUsage,
) {
super(message);
}
}
+/** 回复里的用量与费用;一项都没有时 undefined */
+export function usageOf(j: any): CallUsage | undefined {
+ const u = j?.usage;
+ if (!u || typeof u !== "object") return undefined;
+ const out: CallUsage = {};
+ const put = (k: keyof CallUsage, v: unknown) => {
+ if (typeof v === "number" && Number.isFinite(v)) out[k] = v;
+ };
+ put("input", u.input_tokens);
+ put("output", u.output_tokens);
+ put("reasoning", u.output_tokens_details?.reasoning_tokens);
+ put("costTicks", u.cost_in_usd_ticks ?? j.cost_in_usd_ticks);
+ return Object.keys(out).length ? out : undefined;
+}
+
+/** 上游错误体里的那句话(error.message),不是 JSON 就用原文;已脱敏、压成一行 */
+function errorText(body: string, n = 200): string {
+ let m = body;
+ try {
+ const j = JSON.parse(body);
+ const x = j?.error?.message ?? j?.message;
+ if (typeof x === "string" && x.trim()) m = x;
+ } catch {
+ /* 不是 JSON */
+ }
+ return redactText(m).replace(/\s+/g, " ").trim().slice(0, n);
+}
+
export function serverCompactionEnabled(): boolean {
return process.env.AMB_SERVER_COMPACTION !== "0";
}
@@ -201,10 +236,15 @@ export function splice(lead: unknown[], window: unknown[], tail: unknown[]): unk
return [...lead, ...window, ...tail];
}
-/** /responses/compact 的回复是否可用:output 里至少有一个带密文的 compaction 项;不可用时 undefined */
+/**
+ * /responses/compact 的回复是否可用:output 里至少有一个带密文的 compaction 项;不可用时 undefined。
+ * object 按文档是 response.compaction;Modelflare(实测 grok)回的是 response,output 只有那一个 compaction 项
+ * (不保留用户消息)。两种都收,object 没有也收。
+ */
export function compactedWindow(j: any): unknown[] | undefined {
if (!j || typeof j !== "object" || !Array.isArray(j.output) || !j.output.length) return undefined;
- if (j.object !== undefined && j.object !== "response.compaction") return undefined;
+ if (j.object !== undefined && j.object !== "response.compaction" && j.object !== "response") return undefined;
+ if ((j.error && typeof j.error === "object") || j.status === "failed") return undefined;
const items: any[] = j.output;
if (!items.every((it) => it && typeof it === "object" && !Array.isArray(it) && (typeof it.type === "string" || typeof it.role === "string"))) return undefined;
if (!items.some((it) => it.type === "compaction" && typeof it.encrypted_content === "string" && it.encrypted_content.length > 0)) return undefined;
@@ -430,13 +470,14 @@ export class ServerCompaction {
: undefined;
const input = mapping ? splice(lead, mapping.window, rest.slice(mapping.k)) : j.input;
const out = mapping ? Buffer.from(JSON.stringify({ ...j, input })) : body;
- let tokens = estimateTokens(input) + estimateTokens(j.tools);
+ const est = estimateTokens(input) + estimateTokens(j.tools);
+ let tokens = est;
if (st.usage && st.usage.mapping === (mapping?.id ?? "")) tokens = Math.max(tokens, st.usage.tokens);
const growing = !!st.last && st.last.model === model && rest.length > st.last.len && hashes[st.last.len] === st.last.hash;
st.last = { len: rest.length, hash: hashes[rest.length], model };
if (ep) {
try {
- this.consider({ st, key, session, model, ep, fp, verdict, tokens, growing, input, lead, tools: j.tools, instructions: j.instructions, k: rest.length, hash: hashes[rest.length] });
+ this.consider({ st, key, session, model, ep, fp, verdict, tokens, est, growing, input, lead, tools: j.tools, instructions: j.instructions, k: rest.length, hash: hashes[rest.length] });
} catch (e) {
log.warn("server compaction check failed", { message: (e as Error).message });
}
@@ -454,7 +495,7 @@ export class ServerCompaction {
},
rejected: (status, error, originalOk) => {
if (!mapping) return;
- const snippet = redactText(error).replace(/\s+/g, " ").slice(0, 300);
+ const snippet = errorText(error, 300);
log.warn("compacted request rejected upstream; resent the original", { model, status, originalOk, error: snippet });
// 5xx 可能只是上游一时的问题:映射先留着,连着两次才不用;其余(4xx、流开头报错)这段映射不再用
if (status >= 500 && (mapping.fails = (mapping.fails ?? 0) + 1) < 2) return;
@@ -494,7 +535,10 @@ export class ServerCompaction {
ep: CompactEndpoint;
fp: string;
verdict: Verdict | undefined;
+ /** 发往上游的输入:估算与上游报的用量取大(定什么时候压) */
tokens: number;
+ /** 只按估算(和压缩后的窗口比大小,同一把尺子) */
+ est: number;
growing: boolean;
input: unknown[];
lead: unknown[];
@@ -513,6 +557,9 @@ export class ServerCompaction {
if (!c.verdict?.ok || c.tokens < line || !c.growing || c.st.busy || this.now() < (c.st.quietUntil ?? 0)) return;
const prev = c.st.mappings.at(-1);
if (prev && !prev.hits && this.now() - prev.at < QUIET_MS) return;
+ // 压缩只换掉系统提示之后的历史,系统提示和工具原样再发:它们占到一半以上时,压得再好请求也很难小三成,
+ // 先不花这次钱,等历史再长些(只在压缩线调得很低时会遇到)
+ if (estimateTokens(c.lead) + estimateTokens(c.tools) > c.est / 2) return;
this.track(this.compact(c));
}
@@ -549,30 +596,36 @@ export class ServerCompaction {
st.busy = true;
const at = this.now();
const t0 = Date.now();
+ let quiet = RETRY_MS;
try {
- const output = await this.callCompact(c.ep, { input: c.input, tools: c.tools, instructions: c.instructions });
+ const { output, usage } = await this.callCompact(c.ep, { input: c.input, tools: c.tools, instructions: c.instructions });
const window = withoutLead(output, c.lead);
+ // 改写后的请求(系统提示 + 窗口 + 工具)要比这次请求小三成以上,两边都按估算比。窗口可能只有一个 compaction 项
+ // (Modelflare 不保留用户消息),一样算;没小多少就不用,半小时内这个会话不再压
const after = estimateTokens(splice(c.lead, window, [])) + estimateTokens(c.tools);
- if (after > c.tokens * 0.7) throw new CompactError(`compacted window is not smaller (${after} vs ${c.tokens})`, false);
+ if (after > c.est * 0.7) {
+ quiet = QUIET_MS;
+ throw new CompactError(`compacted window is not smaller (${after} vs ${c.est})`, false, 200, usage);
+ }
const m: Mapping = { id: crypto.randomBytes(6).toString("hex"), model, key: fp, k: c.k, hash: c.hash, window, at, before: c.tokens, after };
st.mappings = [...st.mappings, m].slice(-KEEP_MAPPINGS);
this.saveSession(c.key, st);
- log.info("server compaction done", { model, session: c.session.slice(0, 12), before: c.tokens, after, items: window.length, ms: Date.now() - t0 });
+ log.info("server compaction done", { model, session: c.session.slice(0, 12), before: c.tokens, after, items: window.length, ms: Date.now() - t0, ...(usage && { usage }) });
this.o.onEvent?.({ session: c.session, model, ok: true, before: c.tokens, after, at, ms: Date.now() - t0 });
} catch (e) {
const err = e as CompactError;
const message = redactText(err.message).slice(0, 300);
- log.warn("server compaction failed; requests go on uncompacted", { model, session: c.session.slice(0, 12), status: err.status, unsupported: !!err.unsupported, message });
+ log.warn("server compaction failed; requests go on uncompacted", { model, session: c.session.slice(0, 12), status: err.status, unsupported: !!err.unsupported, message, ...(err.usage && { usage: err.usage }) });
if (err.unsupported) this.setVerdict(model, fp, { ok: false, at: this.now(), reason: `compact: ${message}` });
- st.quietUntil = this.now() + (err.unsupported ? QUIET_MS : RETRY_MS);
+ st.quietUntil = this.now() + (err.unsupported ? QUIET_MS : quiet);
this.o.onEvent?.({ session: c.session, model, ok: false, before: c.tokens, at, ms: Date.now() - t0, error: message });
} finally {
st.busy = false;
}
}
- /** POST /responses/compact(直连):返回压缩后的窗口;失败抛 CompactError */
- private async callCompact(ep: CompactEndpoint, req: { input: unknown[]; tools?: unknown; instructions?: unknown }): Promise<any[]> {
+ /** POST /responses/compact(直连):返回压缩后的窗口与上游报的用量;失败抛 CompactError */
+ private async callCompact(ep: CompactEndpoint, req: { input: unknown[]; tools?: unknown; instructions?: unknown }): Promise<{ output: any[]; usage?: CallUsage }> {
const url = ep.baseUrl.replace(/\/+$/, "").replace(/\/v1$/, "") + "/v1/responses/compact";
const body = { model: ep.model, input: req.input, ...(Array.isArray(req.tools) && req.tools.length ? { tools: req.tools } : {}), ...(typeof req.instructions === "string" && req.instructions ? { instructions: req.instructions } : {}) };
let r: Response;
@@ -587,22 +640,39 @@ export class ServerCompaction {
throw new CompactError(`network: ${(e as Error).message}`, false);
}
const text = await r.text().catch(() => "");
- if (r.status >= 400) throw new CompactError(`HTTP ${r.status}: ${text.slice(0, 300)}`, unsupportedStatus(r.status), r.status);
+ if (r.status >= 400) throw new CompactError(`HTTP ${r.status}: ${errorText(text, 300)}`, unsupportedStatus(r.status), r.status);
let j: any;
try {
j = JSON.parse(text);
} catch {
throw new CompactError(`not JSON: ${text.slice(0, 120)}`, true, r.status);
}
- const out = compactedWindow(j);
- if (!out) throw new CompactError("no usable compaction item in the reply", true, r.status);
- return out;
+ const usage = usageOf(j);
+ const output = compactedWindow(j);
+ if (!output) {
+ const items = Array.isArray(j?.output) ? j.output.map((it: any) => String(it?.type ?? it?.role ?? "?")).join(",") : "-";
+ throw new CompactError(`no usable compaction item in the reply (object ${String(j?.object ?? "-")}, output [${items.slice(0, 120)}])`, true, r.status, usage);
+ }
+ return { output: output as any[], usage };
}
- /** 确认这个模型 + Key 支持服务端压缩(后台;目录没列出就不探测) */
+ /** 连着几次临时失败(探测不出结论) */
+ private retries = new Map<string, number>();
+
+ /**
+ * 确认这个模型 + Key 支持服务端压缩(后台)。探测是付费的推理调用:同一模型 + Key 同时只探测一次;
+ * 目录没列出的不探测;"不支持"没过期(7 天)不再探测;临时失败后等 10 分钟起、每次加倍。
+ */
private ensureVerdict(model: string, ep: CompactEndpoint, fp: string): void {
const k = `${model}|${fp}`;
if (this.probing.has(k) || this.now() < (this.retryAt.get(k) ?? 0)) return;
+ const had = this.verdicts.get(k);
+ if (had && !had.ok && this.now() - had.at < NO_TTL) return;
+ const later = () => {
+ const n = (this.retries.get(k) ?? 0) + 1;
+ this.retries.set(k, n);
+ this.retryAt.set(k, this.now() + Math.min(RETRY_MS * 2 ** (n - 1), RETRY_MAX_MS));
+ };
const job = (async () => {
const listed = await this.o.listed(model).catch(() => undefined);
if (listed !== true) {
@@ -612,12 +682,13 @@ export class ServerCompaction {
return;
}
const v = await this.probe(model, ep);
- if (v) this.setVerdict(model, fp, v);
- else this.retryAt.set(k, this.now() + RETRY_MS);
+ if (!v) return later();
+ this.retries.delete(k);
+ this.setVerdict(model, fp, v);
})()
.catch((e) => {
log.warn("server compaction probe crashed", { model, message: (e as Error).message });
- this.retryAt.set(k, this.now() + RETRY_MS);
+ later();
})
.finally(() => this.probing.delete(k));
this.probing.set(k, job);
@@ -626,21 +697,27 @@ export class ServerCompaction {
/**
* 探测:① 直连 /responses/compact 压一段很小的对话,回复里要有带密文的 compaction 项;② 把压缩窗口按真实请求的拼法
- * 经内置网关发一次 /v1/responses 续接,上游要接受。返回 undefined = 临时问题(网络、限流、5xx),过一会儿再试。
+ * 经内置网关发一次 /v1/responses 续接,上游要接受。由 ② 定论:Modelflare 上 grok 的 ① 能过,② 带着 compaction 项
+ * 就回 400。返回 undefined = 临时问题(网络、限流、5xx),过一会儿再试。回复报了用量和费用就记进日志。
*/
async probe(model: string, ep: CompactEndpoint): Promise<Verdict | undefined> {
const t0 = Date.now();
+ const no = (reason: string): Verdict => ({ ok: false, at: this.now(), reason: `probe: ${reason}` });
let window: any[];
try {
- window = await this.callCompact(ep, { input: [...PROBE_LEAD, ...PROBE_ITEMS], tools: PROBE_TOOLS });
+ const c = await this.callCompact(ep, { input: [...PROBE_LEAD, ...PROBE_ITEMS], tools: PROBE_TOOLS });
+ window = c.output;
+ log.info("server compaction probe: compact call ok", { model, ms: Date.now() - t0, items: window.map((it) => String(it?.type ?? it?.role)).join(","), ...(c.usage && { usage: c.usage }) });
} catch (e) {
const err = e as CompactError;
- log.warn("server compaction probe: compact call failed", { model, status: err.status, unsupported: err.unsupported, message: redactText(err.message).slice(0, 300) });
- return err.unsupported ? { ok: false, at: this.now(), reason: `probe compact: ${redactText(err.message).slice(0, 200)}` } : undefined;
+ const message = redactText(err.message).slice(0, 300);
+ log.warn("server compaction probe: compact call failed", { model, status: err.status, unsupported: err.unsupported, ms: Date.now() - t0, message, ...(err.usage && { usage: err.usage }) });
+ return err.unsupported ? no(`compact call failed: ${message.slice(0, 200)}`) : undefined;
}
const up = this.o.upstream();
if (!up.baseUrl) return undefined;
const input = splice(PROBE_LEAD, withoutLead(window, PROBE_LEAD), [PROBE_ASK]);
+ const t1 = Date.now();
let r: Response;
try {
r = await this.fetch(up.baseUrl.replace(/\/+$/, "").replace(/\/v1$/, "") + "/v1/responses", {
@@ -655,20 +732,22 @@ export class ServerCompaction {
}
const text = await r.text().catch(() => "");
if (r.status >= 400) {
- const message = redactText(text).replace(/\s+/g, " ").slice(0, 300);
- log.warn("server compaction probe: follow-up rejected", { model, status: r.status, message });
- return unsupportedStatus(r.status) ? { ok: false, at: this.now(), reason: `probe follow-up HTTP ${r.status}: ${message.slice(0, 200)}` } : undefined;
+ const message = errorText(text, 300);
+ log.warn("server compaction probe: follow-up rejected", { model, status: r.status, ms: Date.now() - t1, message });
+ return unsupportedStatus(r.status) ? no(`compact call ok, but the follow-up /v1/responses carrying its compaction item was rejected (HTTP ${r.status}): ${message.slice(0, 200)}`) : undefined;
}
let j: any;
try {
j = JSON.parse(text);
} catch {
- return { ok: false, at: this.now(), reason: "probe follow-up: not JSON" };
+ return no("compact call ok, but the follow-up /v1/responses reply is not JSON");
}
+ const usage = usageOf(j);
if ((j?.object !== undefined && j.object !== "response") || j?.status === "failed" || (j?.error && typeof j.error === "object") || !Array.isArray(j?.output)) {
- return { ok: false, at: this.now(), reason: `probe follow-up: unexpected reply (${String(j?.status ?? j?.object ?? "?")})` };
+ log.warn("server compaction probe: follow-up failed", { model, ms: Date.now() - t1, status: String(j?.status ?? j?.object ?? "?"), ...(usage && { usage }) });
+ return no(`compact call ok, but the follow-up /v1/responses carrying its compaction item failed (${String(j?.status ?? j?.object ?? "?")}${j?.error?.message ? `: ${errorText(JSON.stringify(j), 160)}` : ""})`);
}
- log.info("server compaction probe passed", { model, ms: Date.now() - t0, windowItems: window.length });
+ log.info("server compaction probe passed", { model, ms: Date.now() - t0, windowItems: window.length, ...(usage && { followUpUsage: usage }) });
return { ok: true, at: this.now(), reason: "probe passed" };
}
diff --git a/test/agent.integration.test.ts b/test/agent.integration.test.ts
index 1883e97..5b74cc1 100644
--- a/test/agent.integration.test.ts
+++ b/test/agent.integration.test.ts
@@ -13,6 +13,7 @@ import { listen } from "./helpers.js";
/**
* 真实 DSH 子进程 + 脚本化的模型(会发出工具调用),Chat Completions、Responses、Anthropic Messages 三种接口都能答。
* 验证:插件加载、bridge 注入密钥、bash 沙箱、审批流程(API 批准/拒绝)、读围栏、回环 API 防护、按模型选接口。
+ * 服务端压缩(server-compaction.ts)全程接着:目录列出压缩端点的模型,压缩端点就是这个 mock。
*/
const runtime = locateDshRuntime(findAppRoot());
/** workflow 脚本先剥离 TS 类型;Electron 自带的 Node 支持,部分发行版的系统 Node 不带 amaro。 */
@@ -153,7 +154,10 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
let llm: Awaited<ReturnType<typeof listen>>;
const seenAuth = new Set<string>();
/** 上游收到的每个模型请求:走的接口、模型、请求体与请求头 */
- const calls: Array<{ api: "chat" | "responses" | "messages"; model: string; body: any; headers: http.IncomingHttpHeaders; error?: string }> = [];
+ const calls: Array<{ api: "chat" | "responses" | "messages"; model: string; body: any; headers: http.IncomingHttpHeaders; error?: string; sealed?: boolean }> = [];
+ /** 服务端压缩:/v1/responses/compact 收到的请求;sealedRejects 里的模型,带 compaction 项的 Responses 请求一律 400 */
+ const compacts: Array<{ model: string; body: any }> = [];
+ const sealedRejects = new Set<string>();
/** 测试注入的 Modelflare 目录:模型 → 支持的接口(外部端点模式下 App 不查目录) */
const catalog: Record<string, string[]> = {};
/** FLAKY:每条这样的用户消息第一次请求先失败一次(Responses 503、Messages 429),看代理会不会重试 */
@@ -170,12 +174,14 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
function plan(messages: any[]): Step {
let lastUser = -1;
for (let i = messages.length - 1; i >= 0; i--) {
- if (messages[i].role === "user" && /QUICKSORT|ESCALATE|SECRET|READFILE|LOOPBACK|PRESENT|ASKQ|NATIVES|LOOKIMG|HANG|COUNTER|\[用户消息\]/.test(textOf(messages[i].content))) { lastUser = i; break; }
+ if (messages[i].role === "user" && /QUICKSORT|ESCALATE|SECRET|READFILE|LOOPBACK|PRESENT|ASKQ|NATIVES|LOOKIMG|HANG|COUNTER|PADDED|\[用户消息\]/.test(textOf(messages[i].content))) { lastUser = i; break; }
}
const user = textOf(messages[lastUser]?.content);
const tools = messages.slice(lastUser + 1).filter((m) => m.role === "tool");
// 卡住的模型:请求收下了,一直不回
if (/HANG/.test(user)) return { hang: true };
+ // 长回复:让会话历史很快长过系统提示和工具(服务端压缩用例)
+ if (/PADDED/.test(user)) return { text: "PADDED-OK " + "z".repeat(12_000) };
// 看图:回报模型收到的图片(data URL 的头)和文字里有没有工作区路径
if (/LOOKIMG/.test(user)) {
const content = messages[lastUser]?.content;
@@ -216,14 +222,29 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
return;
}
const b = await body(req);
+ // 服务端压缩的压缩端点:照 Modelflare 实测的样子回(object 是 response,output 只有一个 compaction 项)
+ if (url.endsWith("/v1/responses/compact")) {
+ compacts.push({ model: b.model, body: b });
+ const item = { type: "compaction", id: "cmp_" + rnd(), status: "completed", summary: "earlier turns", encrypted_content: `ENC.${b.model}.${compacts.length}` };
+ const usage = { input_tokens: 900, output_tokens: 1200, output_tokens_details: { reasoning_tokens: 1000 }, total_tokens: 2100 };
+ return void res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ id: "resp_" + rnd(), object: "response", status: "completed", model: b.model, output: [item], usage }));
+ }
const api = url.endsWith("/v1/responses") ? "responses" : url.endsWith("/v1/messages") ? "messages" : "chat";
+ const sealed = api === "responses" && (b.input ?? []).some((it: any) => it?.type === "compaction");
const msgs = api === "responses" ? fromResponses(b.input) : api === "messages" ? fromAnthropic(b.messages) : b.messages;
const error = api === "responses" ? responsesError(b) : api === "messages" ? messagesError(b) : undefined;
const said = (msgs ?? []).filter((m: any) => m.role === "user").map((m: any) => textOf(m.content)).findLast((t: string) => /FLAKY/.test(t));
const flaky = api !== "chat" && b.tools && said && !flaked.has(said) ? (flaked.add(said), api === "responses" ? 503 : 429) : 0;
- calls.push({ api, model: b.model, body: b, headers: req.headers, error: error ?? (flaky ? `${flaky}` : undefined) });
+ calls.push({ api, model: b.model, body: b, headers: req.headers, error: error ?? (flaky ? `${flaky}` : undefined), sealed });
if (error) return void res.writeHead(400, { "content-type": "application/json" }).end(JSON.stringify({ type: "error", error: { type: "invalid_request_error", message: error } }));
if (flaky) return void res.writeHead(flaky, { "content-type": "application/json" }).end(JSON.stringify({ type: "error", error: { type: flaky === 429 ? "rate_limit_error" : "api_error", message: "slow down" } }));
+ // Modelflare 实测:带着 compaction 项的 /v1/responses 回 400「无效的参数」
+ if (sealed && sealedRejects.has(b.model)) {
+ calls[calls.length - 1].error = "400";
+ return void res
+ .writeHead(400, { "content-type": "application/json" })
+ .end(JSON.stringify({ error: { message: `无效的参数 (request id: ${rnd()})`, type: "invalid_request_error", param: "", code: "invalid_request" } }));
+ }
const rule = api === "chat" ? undefined : rejects[b.model];
if (rule === "busy") {
calls[calls.length - 1].error = "503";
@@ -238,6 +259,11 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
const step = b.tools ? plan(msgs) : { text: "标题" };
if ("hang" in step && step.hang) return void hung.push(res);
if (process.env.AMB_TEST_LLM_LOG) fs.appendFileSync(process.env.AMB_TEST_LLM_LOG, JSON.stringify({ url: req.url, stream: b.stream, tools: (b.tools ?? []).map((t: any) => t.function?.name ?? t.name), msgs: msgs.map((m: any) => [m.role, textOf(m.content).slice(0, 200), m.tool_calls?.map((t: any) => t.function?.name)]), step }) + "\n");
+ // 非流式的 Responses(服务端压缩探测的续接请求)
+ if (api === "responses" && b.stream === false) {
+ const msg = { type: "message", id: "msg_" + rnd(), role: "assistant", status: "completed", content: [{ type: "output_text", text: step.text ?? "", annotations: [] }] };
+ return void res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ id: "resp_" + rnd(), object: "response", status: "completed", model: b.model, output: [msg], usage: { input_tokens: 10, output_tokens: 5, total_tokens: 15 } }));
+ }
if (api === "responses") return responsesStream(res, b, step);
if (api === "messages") return messagesStream(res, b, step);
if (!b.stream) {
@@ -264,6 +290,11 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
);
// 外部端点模式下 App 不查 Modelflare 目录(只当它会 Chat);这里注入目录,让模型名决定接口
(app.agent as any).o.apiInfo = async (model: string) => (catalog[model] ? { endpoints: catalog[model] } : undefined);
+ // 服务端压缩:外部端点模式下 App 不给压缩端点(代理只走 Chat);这里按注入的目录打开,压缩端点就是这个 mock。
+ // 模型窗口都很大,平常的用例到不了压缩线,只有压缩用例把线调低
+ const sc = (app.agent as any).o.serverCompaction;
+ sc.listed = async (model: string) => (catalog[model] ? catalog[model].includes("openai-response-compact") : undefined);
+ sc.endpoint = (model: string) => ({ baseUrl: llm.url + "/v1", apiKey: "gw-key", model });
await app.start();
base = app.web!.url.replace(/\/+$/, "");
}, 60_000);
@@ -649,4 +680,115 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
expect(mine.map((c) => `${c.api}:${c.error ?? "ok"}`)).toEqual(mine.map(() => "responses:ok"));
expect(mine[0].body.input.filter((i: any) => i.type === "function_call_output")).toHaveLength(1);
}, 180_000);
+
+ // —— 服务端压缩:改写过(换上压缩历史)的请求被拒,bridge 先用原请求重发;这不算接口被拒,模型不改走 Chat ——
+ it("server compaction over Responses: a rewritten request the upstream rejects is resent unmodified; no Chat fallback", async () => {
+ const M = "gpt-6-compact-mock";
+ catalog[M] = ["openai", "openai-response", "openai-response-compact"];
+ const sc = app.agent.serverCompaction!;
+ const host = app.agent.host("default");
+ process.env.AMB_SERVER_COMPACTION_TOKENS = "2000";
+ try {
+ const from = calls.length;
+ // 过线:后台探测(直连压缩 + 经网关续接);会话往后长、历史长过系统提示后在后台压缩,下一轮带着压缩窗口发
+ let n = 0;
+ while (!callsFor(M, from).some((c) => c.sealed && c.body.stream) && n < 8) {
+ expect((await turnOn("sc-user", M, `PADDED ${++n}`)).reply).toMatch(/^PADDED-OK z+$/);
+ await sc.settled();
+ }
+ expect(sc.status()[M]).toMatchObject({ ok: true, reason: "probe passed" });
+ expect(compacts.filter((c) => c.model === M).length).toBeGreaterThanOrEqual(2);
+ const first = callsFor(M, from).find((c) => c.sealed && c.body.stream)!;
+ expect(first).toBeDefined();
+ expect(first.error).toBeUndefined();
+ expect(first.body.input[0].role).toBe("system");
+ expect(host.api).toBe("openai-responses");
+
+ // 上游开始拒收 compaction 项(Modelflare 实测的 400「无效的参数」):bridge 用原请求重发,回合照常
+ sealedRejects.add(M);
+ const next = calls.length;
+ const r = await turnOn("sc-user", M, `PADDED ${++n}`);
+ expect(r.reply).toMatch(/^PADDED-OK z+$/);
+ const mine = callsFor(M, next).filter((c) => c.body.tools && c.body.stream);
+ expect(mine.map((c) => `${c.api}:${c.sealed ? "sealed" : "plain"}:${c.error ?? "ok"}`)).toEqual(["responses:sealed:400", "responses:plain:ok"]);
+ // 不改走 Chat:没有回退记录,代理进程仍走 Responses,活动里没有"改用 Chat Completions 重试"
+ expect(app.agent.apiFallbacks.get(M)).toBeUndefined();
+ expect(host.api).toBe("openai-responses");
+ expect(r.steps.some((s) => s.kind === "retry")).toBe(false);
+ expect(sc.status()[M]).toMatchObject({ ok: false, reason: expect.stringContaining("rewritten request rejected (400): 无效的参数") });
+
+ // 之后原样发,照常走 Responses
+ const after = calls.length;
+ expect((await turnOn("sc-user", M, `PADDED ${++n}`)).reply).toMatch(/^PADDED-OK z+$/);
+ const later = callsFor(M, after).filter((c) => c.body.tools);
+ expect(later.length).toBeGreaterThan(0);
+ expect(later.map((c) => `${c.api}:${c.sealed ? "sealed" : "plain"}:${c.error ?? "ok"}`)).toEqual(later.map(() => "responses:plain:ok"));
+ expect(app.agent.apiFallbacks.get(M)).toBeUndefined();
+ } finally {
+ delete process.env.AMB_SERVER_COMPACTION_TOKENS;
+ sealedRejects.delete(M);
+ await sc.settled();
+ }
+ }, 300_000);
+
+ it("server compaction: when the unmodified resend is rejected too, the Chat fallback still applies, with the original's reason", async () => {
+ const M = "gpt-6-compact-fb-mock";
+ catalog[M] = ["openai", "openai-response", "openai-response-compact"];
+ const sc = app.agent.serverCompaction!;
+ process.env.AMB_SERVER_COMPACTION_TOKENS = "2000";
+ try {
+ const from = calls.length;
+ let n = 0;
+ while (!callsFor(M, from).some((c) => c.sealed && c.body.stream) && n < 8) {
+ expect((await turnOn("sc-fb", M, `PADDED ${++n}`)).reply).toMatch(/^PADDED-OK z+$/);
+ await sc.settled();
+ }
+ expect(callsFor(M, from).some((c) => c.sealed && c.body.stream && !c.error)).toBe(true);
+ // 改写的被拒(「无效的参数」),原请求重发后也被拒(参数不支持):只有后者报给代理接口回退
+ sealedRejects.add(M);
+ rejects[M] = "always";
+ const next = calls.length;
+ const r = await turnOn("sc-fb", M, `PADDED ${++n}`);
+ expect(r.reply).toMatch(/^PADDED-OK z+$/);
+ const mine = callsFor(M, next).filter((c) => c.body.tools && c.body.stream);
+ expect(mine.map((c) => `${c.api}:${c.sealed ? "sealed" : "plain"}:${c.error ?? "ok"}`)).toEqual(["responses:sealed:400", "responses:plain:400", "chat:plain:ok"]);
+ const fb = app.agent.apiFallbacks.get(M);
+ expect(fb).toMatchObject({ from: "openai-responses", reason: expect.stringContaining("HTTP 400: Unsupported parameter") });
+ expect(fb!.reason).not.toContain("无效的参数");
+ expect(r.steps.find((s) => s.kind === "retry")).toMatchObject({ title: "改用 Chat Completions 重试" });
+ expect(app.agent.host("default").api).toBe("openai-completions");
+ } finally {
+ delete process.env.AMB_SERVER_COMPACTION_TOKENS;
+ sealedRejects.delete(M);
+ delete rejects[M];
+ await sc.settled();
+ }
+ }, 300_000);
+
+ it("server compaction probe on a grok-like upstream: compact works, the follow-up with its item is rejected → unsupported, no rewrite, no Chat fallback", async () => {
+ const M = "gpt-6-grokish-mock";
+ catalog[M] = ["openai", "openai-response", "openai-response-compact"];
+ sealedRejects.add(M);
+ const sc = app.agent.serverCompaction!;
+ process.env.AMB_SERVER_COMPACTION_TOKENS = "2000";
+ try {
+ const from = calls.length;
+ for (let n = 1; n <= 3; n++) {
+ expect((await turnOn("sc-grok", M, `PADDED ${n}`)).reply).toMatch(/^PADDED-OK z+$/);
+ await sc.settled();
+ }
+ expect(compacts.filter((c) => c.model === M)).toHaveLength(1);
+ expect(sc.status()[M]).toMatchObject({ ok: false, reason: expect.stringMatching(/^probe: compact call ok, but the follow-up \/v1\/responses carrying its compaction item was rejected \(HTTP 400\): 无效的参数/) });
+ // 探测的续接不经 bridge:不算这个机器人的请求被拒;真实请求都原样发、都成功
+ const real = callsFor(M, from).filter((c) => c.body.stream);
+ expect(real.length).toBeGreaterThan(0);
+ expect(real.every((c) => c.api === "responses" && !c.sealed && !c.error)).toBe(true);
+ expect(app.agent.apiFallbacks.get(M)).toBeUndefined();
+ expect(app.agent.host("default").api).toBe("openai-responses");
+ } finally {
+ delete process.env.AMB_SERVER_COMPACTION_TOKENS;
+ sealedRejects.delete(M);
+ await sc.settled();
+ }
+ }, 300_000);
});
diff --git a/test/server-compaction.unit.test.ts b/test/server-compaction.unit.test.ts
index d584b71..27e194b 100644
--- a/test/server-compaction.unit.test.ts
+++ b/test/server-compaction.unit.test.ts
@@ -1,7 +1,7 @@
/**
* 服务端压缩(server-compaction.ts):前缀匹配与改写、探测与缓存、各种失败时的退回,以及经真实 bridge 的整条路径。
* 上游是按 OpenAI 文档行为写的 mock:/v1/responses/compact 保留用户消息并给出一个带密文的 compaction 项;
- * /v1/responses 只接受这个 mock 给同一模型签出的压缩项。
+ * /v1/responses 只接受这个 mock 给同一模型签出的压缩项。另有 grok 模式,照 Modelflare 实测的样子回(见 GROK_*)。
*/
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import fs from "node:fs";
@@ -9,7 +9,9 @@ import os from "node:os";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { listen } from "./helpers.js";
-import { AgentBridge } from "../src/agent/bridge.js";
+import { AgentBridge, type Rejection } from "../src/agent/bridge.js";
+import { shapeRejection } from "../src/agent/api-fallback.js";
+import { closeFileLog, initFileLog, readLogTail } from "../src/log.js";
import { AgentManager } from "../src/agent/manager.js";
import { ActivityLog } from "../src/agent/activity.js";
import { Store } from "../src/store/db.js";
@@ -25,6 +27,7 @@ import {
prefixHashes,
splitLead,
sseVerdict,
+ usageOf,
type CompactionEvent,
} from "../src/agent/server-compaction.js";
@@ -52,9 +55,20 @@ const req = (model: string, items: unknown[], session = "sess-1", lead: unknown[
const buf = (j: unknown) => Buffer.from(JSON.stringify(j));
const HEADERS = { session_id: "sess-1" };
+/**
+ * Modelflare 上 grok-4.5 / 4.7 实测:/v1/responses/compact 回 200,object 是 "response",output 只有一个 compaction 项
+ * (不保留用户消息),推理很重;而任何 /v1/responses 请求的 input 里只要带着这个项就回下面这个 400。
+ */
+const GROK_REJECT = { error: { message: "无效的参数 (request id: 2026103112000012345678)", type: "invalid_request_error", param: "", code: "invalid_request" } };
+const GROK_USAGE = { input_tokens: 812, output_tokens: 3400, output_tokens_details: { reasoning_tokens: 3100 }, total_tokens: 4212, cost_in_usd_ticks: 187_000_000 };
+
interface MockState {
- compact: "ok" | "404" | "badshape" | "503";
- items: "ok" | "reject" | "streamfail";
+ /** grok:Modelflare 实测的回复;big:compaction 项比原输入还大;slow:晚 50ms 才回 */
+ compact: "ok" | "404" | "badshape" | "503" | "grok" | "big" | "slow";
+ /** grok:带 compaction 项的 /v1/responses 一律回 GROK_REJECT;429:带 compaction 项的回 429 */
+ items: "ok" | "reject" | "streamfail" | "grok" | "429";
+ /** reject:所有 /v1/responses 都按"参数不支持"回 400(代理接口回退会认的那种) */
+ responses: "ok" | "reject";
/** 回复里报的 input_tokens = 估算 × boost */
boost: number;
/** 助手回复的文字 */
@@ -63,7 +77,7 @@ interface MockState {
/** 按文档行为的上游:记下每个请求 */
async function mockUpstream() {
- const state: MockState = { compact: "ok", items: "ok", boost: 1, reply: "ok" };
+ const state: MockState = { compact: "ok", items: "ok", responses: "ok", boost: 1, reply: "ok" };
const seen: Array<{ path: string; auth?: string; body: any }> = [];
let n = 0;
const srv = await listen(async (req, res) => {
@@ -77,6 +91,12 @@ async function mockUpstream() {
if (p === "/v1/responses/compact") {
if (state.compact === "404") return json(404, { error: { message: "Invalid URL (POST /v1/responses/compact)", type: "invalid_request_error" } });
if (state.compact === "503") return json(503, { error: { message: "upstream overloaded" } });
+ if (state.compact === "grok" || state.compact === "big") {
+ const enc = `ENC.${name}.${++n}.` + (state.compact === "big" ? "E".repeat(raw.length * 2) : "gAAAAB");
+ const item = { type: "compaction", id: `cmp_${n}`, status: "completed", summary: "The user asked for the code word; lookup returned teal.", encrypted_content: enc };
+ return json(200, { id: `resp_c${n}`, object: "response", created_at: 1, status: "completed", model: name, output: [item], usage: GROK_USAGE });
+ }
+ if (state.compact === "slow") await new Promise((r) => setTimeout(r, 50));
// 保留用户(和 developer)消息,其余压进一个 compaction 项
const kept = body.input.filter((it: any) => it.role === "user" || it.role === "developer").map((it: any, i: number) => ({ id: `msg_${i}`, type: "message", status: "completed", ...it }));
const output = state.compact === "badshape" ? kept : [...kept, { type: "compaction", id: `cmp_${++n}`, encrypted_content: `ENC.${name}.${n}` }];
@@ -85,9 +105,12 @@ async function mockUpstream() {
if (p !== "/v1/responses") return json(404, { error: { message: "not found" } });
const sealed = (body.input as any[]).filter((it) => it?.type === "compaction");
if (sealed.length) {
+ if (state.items === "grok") return json(400, GROK_REJECT);
+ if (state.items === "429") return json(429, { error: { message: "slow down", type: "rate_limit_error" } });
if (state.items === "reject") return json(400, { error: { message: "Invalid value: 'compaction'. Supported values are: 'message', 'reasoning', ...", type: "invalid_request_error" } });
if (sealed.some((it) => String(it.encrypted_content).split(".")[1] !== name)) return json(400, { error: { message: "The encrypted content could not be decrypted", code: "invalid_encrypted_content" } });
}
+ if (state.responses === "reject") return json(400, { error: { message: "Unsupported parameter: 'reasoning.summary' is not supported with this model.", type: "invalid_request_error", code: "unsupported_parameter" } });
const input_tokens = Math.round(estimateTokens(body.input) * state.boost);
const usage = { input_tokens, output_tokens: 10, total_tokens: input_tokens + 10 };
const output = [{ type: "message", id: `msg_out${seen.length}`, role: "assistant", status: "completed", content: [{ type: "output_text", text: state.reply, annotations: [] }] }];
@@ -175,15 +198,26 @@ describe("服务端压缩:纯函数", () => {
expect(a[3]).not.toBe(b[3]);
});
- it("压缩回复的校验:要有带密文的 compaction 项、object 对得上", () => {
+ it("压缩回复的校验:要有带密文的 compaction 项;object 是 response.compaction、response(Modelflare 实测)或没有", () => {
const ok = { object: "response.compaction", output: [user("hi"), { type: "compaction", id: "cmp_1", encrypted_content: "gAAA" }] };
expect(compactedWindow(ok)).toEqual(ok.output);
expect(compactedWindow({ output: ok.output })).toEqual(ok.output);
- expect(compactedWindow({ ...ok, object: "response" })).toBeUndefined();
+ expect(compactedWindow({ ...ok, object: "response" })).toEqual(ok.output);
+ // grok:object 是 response,output 只有一个 compaction 项(带 status、summary)
+ const grok = { id: "resp_c1", object: "response", status: "completed", output: [{ type: "compaction", id: "cmp_1", status: "completed", summary: "s", encrypted_content: "gAAA" }], usage: GROK_USAGE };
+ expect(compactedWindow(grok)).toEqual(grok.output);
+ expect(compactedWindow({ ...ok, object: "list" })).toBeUndefined();
+ expect(compactedWindow({ ...grok, status: "failed" })).toBeUndefined();
+ expect(compactedWindow({ ...grok, error: { message: "x" } })).toBeUndefined();
+ expect(compactedWindow({ ...grok, error: null })).toEqual(grok.output);
expect(compactedWindow({ object: "response.compaction", output: [user("hi")] })).toBeUndefined();
expect(compactedWindow({ object: "response.compaction", output: [{ type: "compaction", encrypted_content: "" }] })).toBeUndefined();
expect(compactedWindow({ object: "response.compaction", output: [1, { type: "compaction", encrypted_content: "x" }] })).toBeUndefined();
expect(compactedWindow({ error: { message: "x" } })).toBeUndefined();
+ // 用量与费用(日志用)
+ expect(usageOf(grok)).toEqual({ input: 812, output: 3400, reasoning: 3100, costTicks: 187_000_000 });
+ expect(usageOf({ usage: { input_tokens: 5 } })).toEqual({ input: 5 });
+ expect(usageOf({ output: [] })).toBeUndefined();
});
it("流开头的判断:created / in_progress 之后看第一个实质事件", async () => {
@@ -291,7 +325,7 @@ describe("服务端压缩:探测、压缩与改写", () => {
}
});
- it("压缩端点 404 / 回复里没有压缩项:记为不支持,存盘,一天内不再探测", async () => {
+ it("压缩端点 404 / 回复里没有压缩项:记为不支持,存盘,7 天内不再探测", async () => {
let now = 1_800_000_000_000;
for (const mode of ["404", "badshape"] as const) {
mock.state.compact = mode;
@@ -302,11 +336,16 @@ describe("服务端压缩:探测、压缩与改写", () => {
for (const n of [3, 5, 7]) await send(sc, req("mf/good", [user("start"), ...calls(1, n)]));
expect(mock.of("/v1/responses/compact"), mode).toHaveLength(1);
expect(mock.of("/v1/responses"), mode).toHaveLength(0);
- expect(sc.status()["mf/good"]).toMatchObject({ ok: false });
- // 重启后仍记得;过了一天重新探测
+ expect(sc.status()["mf/good"]).toMatchObject({ ok: false, reason: expect.stringMatching(mode === "404" ? /^probe: compact call failed: HTTP 404: Invalid URL/ : /^probe: compact call failed: no usable compaction item/) });
+ // 重启后仍记得;一天、六天后都不探测,过了 7 天重新探测
const again = make({ now: () => now });
await send(again, req("mf/good", [user("start"), ...calls(1, 8)]));
expect(mock.of("/v1/responses/compact"), mode).toHaveLength(1);
+ for (const days of [1, 5]) {
+ now += days * 24 * 3600_000;
+ await send(again, req("mf/good", [user("start"), ...calls(1, 9)]));
+ expect(mock.of("/v1/responses/compact"), mode).toHaveLength(1);
+ }
now += 25 * 3600_000;
await send(again, req("mf/good", [user("start"), ...calls(1, 9)]));
await send(again, req("mf/good", [user("start"), ...calls(1, 9)]));
@@ -319,11 +358,11 @@ describe("服务端压缩:探测、压缩与改写", () => {
const sc = make();
await send(sc, req("mf/good", [user("start")]));
for (const n of [3, 5, 7]) await send(sc, req("mf/good", [user("start"), ...calls(1, n)]));
- expect(sc.status()["mf/good"]).toMatchObject({ ok: false, reason: expect.stringContaining("HTTP 400") });
+ expect(sc.status()["mf/good"]).toMatchObject({ ok: false, reason: expect.stringContaining("follow-up /v1/responses carrying its compaction item was rejected (HTTP 400): Invalid value: 'compaction'") });
expect(mock.of("/v1/responses/compact")).toHaveLength(1);
});
- it("临时失败(5xx)不记结论,10 分钟后再探测", async () => {
+ it("临时失败(5xx)不记结论,10 分钟后再探测;连着失败等得更久", async () => {
let now = 1_800_000_000_000;
mock.state.compact = "503";
const sc = make({ now: () => now });
@@ -332,12 +371,129 @@ describe("服务端压缩:探测、压缩与改写", () => {
await send(sc, req("mf/good", [user("start"), ...calls(1, 5)]));
expect(mock.of("/v1/responses/compact")).toHaveLength(1);
expect(sc.status()).toEqual({});
- mock.state.compact = "ok";
now += 11 * 60_000;
await send(sc, req("mf/good", [user("start"), ...calls(1, 6)]));
+ expect(mock.of("/v1/responses/compact")).toHaveLength(2);
+ // 第二次失败后等 20 分钟
+ now += 11 * 60_000;
+ await send(sc, req("mf/good", [user("start"), ...calls(1, 7)]));
+ expect(mock.of("/v1/responses/compact")).toHaveLength(2);
+ mock.state.compact = "ok";
+ now += 10 * 60_000;
+ await send(sc, req("mf/good", [user("start"), ...calls(1, 8)]));
+ expect(mock.of("/v1/responses/compact")).toHaveLength(3);
expect(sc.status()["mf/good"]).toMatchObject({ ok: true });
});
+ it("grok(Modelflare 实测):压缩调用能过,带着 compaction 项续接被拒 → 不支持,原因写明是续接被拒;用量记日志;7 天内不再探测", async () => {
+ const logDir = fs.mkdtempSync(path.join(os.tmpdir(), "amb-sc-log-"));
+ initFileLog(logDir);
+ try {
+ let now = 1_800_000_000_000;
+ mock.state.compact = "grok";
+ mock.state.items = "grok";
+ const events: CompactionEvent[] = [];
+ const sc = make({ now: () => now, events });
+ const M = "modelflare/grok-4.7";
+ await send(sc, req(M, [user("start")]));
+ for (const n of [3, 5, 7]) await send(sc, req(M, [user("start"), ...calls(1, n)]));
+ // 一次压缩调用 + 一次续接,就此定论
+ expect(mock.of("/v1/responses/compact")).toHaveLength(1);
+ expect(mock.of("/v1/responses/compact")[0].body.model).toBe("grok-4.7");
+ const follow = mock.of("/v1/responses");
+ expect(follow).toHaveLength(1);
+ expect(follow[0].body.input.filter((it: any) => it.type === "compaction")).toEqual([expect.objectContaining({ id: "cmp_1", encrypted_content: "ENC.grok-4.7.1.gAAAAB" })]);
+ const v = sc.status()[M];
+ expect(v.ok).toBe(false);
+ expect(v.reason).toBe("probe: compact call ok, but the follow-up /v1/responses carrying its compaction item was rejected (HTTP 400): 无效的参数 (request id: 2026103112000012345678)");
+ expect(v.reason).not.toContain("no usable compaction item");
+ // 不压缩、不改写
+ expect(events).toEqual([]);
+ expect(sc.prepare("/v1/responses", HEADERS, buf(req(M, [user("start"), ...calls(1, 8)])))!.rewritten).toBe(false);
+ // 用量和费用记进日志
+ const logs = readLogTail(100, logDir).join("\n");
+ expect(logs).toMatch(/server compaction probe: compact call ok .*"usage":\{"input":812,"output":3400,"reasoning":3100,"costTicks":187000000\}/);
+ expect(logs).toMatch(/server compaction probe: follow-up rejected .*"status":400/);
+ // 别的会话、重启以后也一样:7 天内不再探测
+ const again = make({ now: () => now });
+ for (const days of [0, 1, 5]) {
+ now += days * 24 * 3600_000;
+ await send(again, req(M, [user("other"), ...calls(1, 6)], `sess-${days}`), { session_id: `sess-${days}` });
+ await send(again, req(M, [user("other"), ...calls(1, 7)], `sess-${days}`), { session_id: `sess-${days}` });
+ }
+ expect(mock.of("/v1/responses/compact")).toHaveLength(1);
+ now += 25 * 3600_000;
+ await send(again, req(M, [user("other"), ...calls(1, 8)], "sess-5"), { session_id: "sess-5" });
+ expect(mock.of("/v1/responses/compact")).toHaveLength(2);
+ expect(again.status()[M]).toMatchObject({ ok: false, reason: expect.stringContaining("follow-up /v1/responses carrying its compaction item was rejected") });
+ } finally {
+ closeFileLog();
+ fs.rmSync(logDir, { recursive: true, force: true });
+ }
+ });
+
+ it("同一模型 + Key 同时只探测一次(几个会话一起过线)", async () => {
+ const sc = make();
+ const M = "mf/good";
+ await send(sc, req(M, [user("warm")], "warm"), { session_id: "warm" });
+ mock.state.compact = "slow";
+ for (const s of ["a", "b", "c", "d"]) sc.prepare("/v1/responses", { session_id: s }, buf(req(M, [user(s), ...calls(1, 3)], s)));
+ await sc.settled();
+ for (const s of ["a", "b"]) sc.prepare("/v1/responses", { session_id: s }, buf(req(M, [user(s), ...calls(1, 3)], s)));
+ await sc.settled();
+ expect(mock.of("/v1/responses/compact")).toHaveLength(1);
+ expect(mock.of("/v1/responses")).toHaveLength(1);
+ expect(sc.status()[M]).toMatchObject({ ok: true });
+ });
+
+ it("压缩窗口只有一个 compaction 项(不保留用户消息)也照常用:系统提示 + 这一项 + 之后新增的", async () => {
+ mock.state.compact = "grok";
+ const events: CompactionEvent[] = [];
+ const sc = make({ events });
+ const M = "mf/good";
+ const h = [user("start"), ...calls(1, 5)];
+ for (const items of [h.slice(0, 1), [user("start"), ...calls(1, 3)], h]) await send(sc, req(M, items));
+ expect(sc.status()[M]).toMatchObject({ ok: true });
+ expect(events).toEqual([expect.objectContaining({ ok: true })]);
+ expect(events[0].after!).toBeLessThan(events[0].before * 0.7);
+ const plan = sc.prepare("/v1/responses", HEADERS, buf(req(M, [...h, ...call(6)])))!;
+ expect(plan.rewritten).toBe(true);
+ expect(JSON.parse(plan.body.toString()).input).toEqual([LEAD, { type: "compaction", id: "cmp_2", status: "completed", summary: expect.any(String), encrypted_content: "ENC.good.2.gAAAAB" }, ...call(6)]);
+ await sc.settled();
+ });
+
+ it("压缩后要小三成:很大的 compaction 项不用、半小时内这个会话不再压;系统提示和工具占一半以上时先不压", async () => {
+ let now = 1_800_000_000_000;
+ const events: CompactionEvent[] = [];
+ mock.state.compact = "big";
+ const sc = make({ now: () => now, events });
+ const M = "mf/good";
+ const h = [user("start"), ...calls(1, 5)];
+ for (const items of [h.slice(0, 1), [user("start"), ...calls(1, 3)], h]) await send(sc, req(M, items));
+ expect(mock.of("/v1/responses/compact")).toHaveLength(2);
+ expect(events).toEqual([expect.objectContaining({ ok: false, error: expect.stringContaining("not smaller") })]);
+ expect((await send(sc, req(M, [...h, ...call(6)])))!.rewritten).toBe(false);
+ expect(mock.of("/v1/responses/compact")).toHaveLength(2);
+ // 不是临时失败:过了 10 分钟也不重压,半小时后再压(这次压得小)
+ mock.state.compact = "grok";
+ now += 11 * 60_000;
+ await send(sc, req(M, [...h, ...calls(6, 7)]));
+ expect(mock.of("/v1/responses/compact")).toHaveLength(2);
+ now += 20 * 60_000;
+ await send(sc, req(M, [...h, ...calls(6, 8)]));
+ expect(mock.of("/v1/responses/compact")).toHaveLength(3);
+ expect(events.at(-1)).toMatchObject({ ok: true });
+
+ // 系统提示占了大半:过了线也先不压,等历史长到一半以上
+ const bigLead = [{ role: "developer", content: "S".repeat(30_000) }];
+ const lead = (n: number) => send(sc, req(M, [user("lead-heavy"), ...calls(1, n)], "sess-lead", bigLead), { session_id: "sess-lead" });
+ for (const n of [1, 2, 3, 4]) await lead(n);
+ expect(mock.of("/v1/responses/compact")).toHaveLength(3);
+ await lead(14);
+ await lead(15);
+ expect(mock.of("/v1/responses/compact")).toHaveLength(4);
+ });
+
it("改写过的请求被拒、原请求能过:这段映射作废,记下不支持,之后不再改写", async () => {
const sc = make();
const h = [user("start"), ...calls(1, 5)];
@@ -441,6 +597,78 @@ describe("服务端压缩:经 bridge", () => {
}
});
+ it("改写过的请求被拒不报给代理接口回退(不会让模型改走 Chat);原请求重发成功,回合照常", async () => {
+ const sc = make();
+ const { bridge, post } = await bridgeWith(sc);
+ const seen: Rejection[] = [];
+ const off = bridge.onRejection((r) => void seen.push(r));
+ try {
+ const h = await compacted(post);
+ // 上游像 Modelflare 那样:带 compaction 项就 400「无效的参数」,原请求能过
+ mock.state.items = "grok";
+ const before = mock.of("/v1/responses").length;
+ const r = await post(req("mf/good", h));
+ expect(r.status).toBe(200);
+ const [rewritten, original] = mock.of("/v1/responses").slice(before);
+ expect(rewritten.body.input.some((it: any) => it.type === "compaction")).toBe(true);
+ expect(original.body.input.some((it: any) => it.type === "compaction")).toBe(false);
+ expect(seen).toEqual([]);
+ expect(sc.status()["mf/good"]).toMatchObject({ ok: false, reason: expect.stringContaining("rewritten request rejected (400): 无效的参数") });
+ } finally {
+ off();
+ await bridge.stop();
+ }
+ });
+
+ it("改写过的请求被拒、原请求重发后也被拒(请求形状的问题):只报原请求那次,代理接口回退照常认", async () => {
+ const sc = make();
+ const { bridge, post } = await bridgeWith(sc);
+ const seen: Rejection[] = [];
+ const off = bridge.onRejection((r) => void seen.push(r));
+ try {
+ const h = await compacted(post);
+ mock.state.items = "grok";
+ mock.state.responses = "reject";
+ const before = mock.of("/v1/responses").length;
+ const r = await post(req("mf/good", h));
+ expect(r.status).toBe(400);
+ expect(r.text).toContain("Unsupported parameter");
+ expect(mock.of("/v1/responses").slice(before).map((s) => s.body.input.some((it: any) => it.type === "compaction"))).toEqual([true, false]);
+ expect(seen).toHaveLength(1);
+ expect(seen[0]).toMatchObject({ path: "/v1/responses", status: 400 });
+ expect(seen[0].error).toContain("Unsupported parameter");
+ expect(seen[0].error).not.toContain("无效的参数");
+ expect(shapeRejection(seen, "")).toMatch(/^HTTP 400: Unsupported parameter/);
+ // 原请求也不过:不是压缩项的问题,映射丢掉但不记"不支持"
+ expect(sc.status()["mf/good"]).toMatchObject({ ok: true });
+ } finally {
+ off();
+ await bridge.stop();
+ }
+ });
+
+ it("改写过的请求遇到 429:不重发、原样交给 DSH(它会重试),也不报给代理接口回退", async () => {
+ const sc = make();
+ const { bridge, post } = await bridgeWith(sc);
+ const seen: Rejection[] = [];
+ const off = bridge.onRejection((r) => void seen.push(r));
+ try {
+ const h = await compacted(post);
+ mock.state.items = "429";
+ const before = mock.of("/v1/responses").length;
+ expect((await post(req("mf/good", h))).status).toBe(429);
+ expect(mock.of("/v1/responses").length - before).toBe(1);
+ expect(seen).toEqual([]);
+ // 原样发的请求被拒照常报
+ mock.state.responses = "reject";
+ expect((await post(req("mf/good", [user("fresh")]))).status).toBe(400);
+ expect(seen).toHaveLength(1);
+ } finally {
+ off();
+ await bridge.stop();
+ }
+ });
+
it("流一开头就报错(还没交出任何字节):同样用原请求重发", async () => {
const sc = make();
const { bridge, post } = await bridgeWith(sc);