Files
billion-context/scripts/gate/patch-gate.mjs
ework-agent 67eeb303aa feat(gate): containerized full real-model gate + config-driven launcher
The full real-model gate (A compression / B model switch / U registry /
I install / X auto-update / P host-plugin lanes / R restart kill matrix)
moves into this repo so the lane logic versions with the code under test
and remote CI can run the SAME scripts from the candidate checkout.
Lanes I/X/R and the A/B model calls need a real model endpoint, so they
remain operator-run via the new launcher; ci-gate-plugins.yml keeps
driving Lane P.

- Dockerfile: node:22-slim + the host CLIs pinned to the versions this
  repo's own CI uses (dsh / pi / codex / opencode / claude / opencode-v1)
- entrypoint.sh: build the candidate rev, in-container ephemeral verdaccio
  (lanes U/I/X, no host registry dependency), keyed-API shims, then run
  release-gate-local (A/B/U) + plugin-lanes (P) + restart-lanes (R) +
  docker-lanes (I/X) and roll up one GREEN/RED
- docker-lanes.mjs / restart-lanes.mjs / patch-gate.mjs /
  verdaccio.config.yaml: the lane drivers and shims
- bili-gate.sh: config-driven launcher — scripts/gate/bili-gate.sh
  --config <path> [--rev REF] [--skip ...] [--keep] [--dry-run]. ALL
  host-specific facts (model endpoints, API keys, npm registry, git
  proxy/mirror, out dir) live in the local JSON config only; nothing
  machine-specific is committed here (config.example.json is
  placeholders). The stale-image guard from the old host wrapper comes
  along: on build failure the existing image is reused only after
  byte-comparing every /gate-tools lane script, drift = FATAL.
- README(.zh-CN): full lane table + the endpoint/secret contract +
  how-to-run; the old 'the containerized wrapper lives outside this repo'
  section is retired
2026-11-15 14:38:50 +08:00

82 lines
3.0 KiB
JavaScript

#!/usr/bin/env node
// patch-gate.mjs — runtime shims for scripts/release-gate-local.mjs so the gate
// works against KEYED public model APIs, not just keyless local sglang.
//
// Every patch is GUARDED: applied only when the exact upstream pattern is found
// exactly once; otherwise it prints SKIPPED (upstream already has the feature,
// or the script changed shape — fail loudly in the summary, never silently).
//
// Upstream PR (to make these shims unnecessary) should add:
// GATE_PRIMARY_KEY (default "gate-local") fed into BILI_GATE_KEY, and
// reachability-based probes (any HTTP response counts — 401/404 still proves
// the endpoint is alive; only network errors mean "service down").
import fs from "node:fs";
const file = process.argv[2];
if (!file || !fs.existsSync(file)) {
console.error("patch-gate: no target file: " + file);
process.exit(2);
}
let src = fs.readFileSync(file, "utf8");
let fail = 0;
function patchOnce(name, find, replace) {
const n = src.split(find).length - 1;
if (n === 0) {
console.log(`patch-gate: SKIP ${name} (pattern not present — already patched upstream?)`);
return;
}
if (n > 1) {
console.log(`patch-gate: FAIL ${name} (pattern found ${n}x, expected 1)`);
fail++;
return;
}
src = src.replace(find, replace);
console.log(`patch-gate: applied ${name}`);
}
// 1) Let the primary-lane credential come from the environment instead of the
// hardcoded "gate-local" bearer (dsh sends it as Authorization; bili
// forwards client credentials to the upstream).
patchOnce(
"GATE_PRIMARY_KEY credential",
'BILI_GATE_KEY: "gate-local",',
'BILI_GATE_KEY: env("GATE_PRIMARY_KEY", "gate-local"),',
);
// 2) Reachability probes: treat ANY HTTP response as "service up". Keyed APIs
// answer 401 without a bearer; some gateways 404 on /v1/models — neither
// means the model lane is unusable, and the old res.ok-only probe would
// hard-abort Lane A on every keyed public API.
patchOnce(
"probe helper",
"async function httpOk(url, ms = 5000) {",
`async function probeUp(url, ms, key) {
try {
const ac = new AbortController();
const t = setTimeout(() => ac.abort(), ms ?? 4000);
const res = await fetch(url, { signal: ac.signal, headers: key ? { authorization: "Bearer " + key } : {} });
clearTimeout(t);
return res.status > 0; // any HTTP response = endpoint alive (401/404 included)
} catch {
return false;
}
}
async function httpOk(url, ms = 5000) {`,
);
patchOnce(
"sglang probe",
"const sglangOk = await httpOk(`${G.sglangUrl}/v1/models`, 4000);",
"const sglangOk = await probeUp(`${G.sglangUrl}/v1/models`, 4000, env(\"GATE_PRIMARY_KEY\", \"gate-local\"));",
);
// Relay lane: try /v1/models first (standard OpenAI-compatible), fall back to /.
patchOnce(
"relay probe",
"const relayOk = await httpOk(`${G.relayUrl}/`, 3000);",
"const relayOk = await probeUp(`${G.relayUrl}/v1/models`, 3000, G.relayKey) || await probeUp(`${G.relayUrl}/`, 3000, G.relayKey);",
);
fs.writeFileSync(file, src);
process.exit(fail ? 1 : 0);