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
2 changes: 1 addition & 1 deletion CLIENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ bili only compresses requests whose path matches a known wire protocol (`/chat/c

That outcome is now loud instead of silent (#1290):

- the client-side fetch hook logs each distinct unrouted endpoint once per process (`…is not a recognized model endpoint, so bili did not route it through the proxy…`);
- the client-side fetch hook logs each distinct unrouted **POST** endpoint once per process (`…is not a recognized model endpoint, so bili did not route it through the proxy…`); non-POST traffic — npm registries, catalog JSONs, git refs — is silent by design (#1657: a GET cannot carry a prompt);
- `unrecognizedPaths` (per-path counts) in `curl -s http://localhost:8787/__bili/stats` (loopback-only);
- an `UNRECOGNIZED PATHS (instance-level)` section in `acp_status` output while such requests exist.

Expand Down
2 changes: 1 addition & 1 deletion CLIENTS.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bili 只压缩路径匹配已知 wire 协议(`/chat/completions`、`/llm_raw_cha

这个结果现在不再静默(#1290):

- 客户端侧 fetch 钩子对每个不同的未路由端点每进程记一次日志(`…is not a recognized model endpoint, so bili did not route it through the proxy…`);
- 客户端侧 fetch 钩子对每个不同的、以 **POST** 发出的未路由端点每进程记一次日志(`…is not a recognized model endpoint, so bili did not route it through the proxy…`);非 POST 流量——npm 注册表、目录 JSON、git refs——按设计保持静默(#1657:GET 不携带 prompt,不可能是模型流量);
- `curl -s http://localhost:8787/__bili/stats` 输出 `unrecognizedPaths`(按路径计数,仅 loopback);
- 存在此类请求时,`acp_status` 输出会多一节 `UNRECOGNIZED PATHS (instance-level)`。

Expand Down
39 changes: 31 additions & 8 deletions src/agent/native-intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ export interface NativeInterceptState {
onRoutedOriginObserved?: (origin: string) => void;
/** Test/observability hook: every dispatched decision. */
onDispatch?: (url: string, action: "rewrite" | "direct" | "self" | "retry") => void;
/** #1290: observability hook — fired for every request the fetch patch lets
* through WITHOUT routing because its URL is not a recognized model endpoint
* (isModelApiUrl miss). Such requests never reach a bili proxy, so without
* this their "went direct (uncompressed)" outcome was completely silent —
* #1158's logging promise only covered the attribution gate below. Called
* per request; hosts dedup once-per-process-per-endpoint like takeoverGate.
* Undefined hosts stay silent. */
/** #1290: observability hook — fired for every POST request the fetch patch
* lets through WITHOUT routing because its URL is not a recognized model
* endpoint (isModelApiUrl miss). Such requests never reach a bili proxy,
* so without this their "went direct (uncompressed)" outcome was completely
* silent — #1158's logging promise only covered the attribution gate below.
* #1657: non-POST methods are excluded — a GET/HEAD cannot carry a prompt,
* so it cannot be model inference, and host tooling traffic (npm registries,
* catalog JSONs, git refs — ~18 lines per dsh web boot) used to drown the
* real failure lines. Called per request; hosts dedup once-per-process-
* per-endpoint like takeoverGate. Undefined hosts stay silent. */
onUnroutedModelUrl?: (url: string) => void;
}

Expand Down Expand Up @@ -232,6 +235,23 @@ function fetchUrlOf(input: string | URL | Request): string | undefined {
return undefined;
}

/** HTTP method of a fetch call, uppercased: init.method wins (spec: init
* overrides a Request-object input), then the Request object's own method,
* then the fetch default GET. */
function fetchMethodOf(input: string | URL | Request, init?: RequestInit): string {
const m = init?.method;
if (typeof m === "string") return m.toUpperCase();
try {
if (input !== null && typeof input === "object" && !(input instanceof URL)) {
const rm = (input as Request).method;
if (typeof rm === "string") return rm.toUpperCase();
}
} catch {
// fallthrough
}
return "GET";
}

/** Merge extra headers into a (input, init) pair, preserving all three
* init.headers forms (Headers instance, entries array, plain object) and
* rebuilding a Request-object input with the merged headers (its body
Expand Down Expand Up @@ -606,7 +626,10 @@ export function installNativeFetchIntercept(state: NativeInterceptState): boolea
}
}
if (!isModelApiUrl(url)) {
if (!isBiliControlUrl(url)) state.onUnroutedModelUrl?.(url);
// #1657: only POST can carry model traffic — npm registries,
// catalog JSONs, git refs and the rest of the host's tooling are
// GET and would otherwise fire the hook once per boot per endpoint.
if (!isBiliControlUrl(url) && fetchMethodOf(input, init) === "POST") state.onUnroutedModelUrl?.(url);
return send(input, init);
}
// #1117: URL shape alone cannot claim a request — every model call in
Expand Down
38 changes: 34 additions & 4 deletions tests/native-intercept.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ test("install: non-model-API URLs fire onUnroutedModelUrl so direct sends are vi
onUnroutedModelUrl: (u) => { unrouted.push(u); },
};
const { sink } = await withPatch(state, async (fetch) => {
// A third-party plugin's custom wire (commandcode's Go plan) — not a
// recognized model endpoint, so it goes direct AND is reported.
await fetch("https://api.commandcode.example/alpha/generate");
await fetch("https://api.commandcode.example/alpha/generate");
// A third-party plugin's custom wire (commandcode's Go plan, POST) —
// not a recognized model endpoint, so it goes direct AND is reported.
await fetch("https://api.commandcode.example/alpha/generate", { method: "POST" });
await fetch("https://api.commandcode.example/alpha/generate", { method: "POST" });
// A real model endpoint — routed, never reported as unrouted.
await fetch("http://127.0.0.1:8199/v1/messages");
// Bili's own control plane — direct by design, never reported either.
Expand All @@ -229,6 +229,36 @@ test("install: non-model-API URLs fire onUnroutedModelUrl so direct sends are vi
for (const u of unrouted) assert.ok(u.endsWith("/alpha/generate"), `unexpected unrouted: ${u}`);
});

test("install: only POST fires onUnroutedModelUrl — registry/catalog/git GETs stay silent (#1657)", async () => {
const unrouted: string[] = [];
const state: NativeInterceptState = {
origin: "http://127.0.0.1:40001",
ready: Promise.resolve("http://127.0.0.1:40001"),
onUnroutedModelUrl: (u) => { unrouted.push(u); },
};
const { sink } = await withPatch(state, async (fetch) => {
// dsh plugin-manager boot traffic (#1657 repro): npm registry / CDN
// catalog / git refs — all pass through direct and UNREPORTED.
await fetch("https://mirrors.cloud.tencent.com/npm/dshmarket/latest");
await fetch("https://cdn.jsdelivr.net/gh/duhu2000/dsh-mcp-connector-registry@main/catalog.json");
await fetch("https://github.com/142475/dsh-pocket.git/info/refs");
await fetch("https://api.github.com/repos/duhu2000/dsh-mcp-connector/releases/latest");
// Non-model custom wire via other methods — also unreported.
await fetch("https://api.commandcode.example/alpha/generate");
await fetch("https://api.commandcode.example/alpha/generate", { method: "HEAD" });
// The same custom wire as POST — the #1290 case, still reported.
// Lowercase init method and Request-object method both count.
await fetch("https://api.commandcode.example/alpha/generate", { method: "post" });
await fetch(new Request("https://api.commandcode.example/alpha/generate", { method: "POST" }));
});
assert.equal(sink.length, 8, "every request still passes through direct");
assert.deepEqual(
unrouted,
["https://api.commandcode.example/alpha/generate", "https://api.commandcode.example/alpha/generate"],
"only POSTs fire the hook",
);
});

test("install: proxy-origin URLs are never re-proxied (self guard)", async () => {
const state: NativeInterceptState = { origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") };
const { sink } = await withPatch(state, async (fetch) => {
Expand Down
Loading