@stablekernel/pi-bifrost 0.12.0 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/package.json +1 -1
- package/src/index.ts +15 -5
- package/src/plugin.test.ts +33 -5
- package/src/refresh-project.test.ts +349 -0
- package/src/refresh.test.ts +13 -2
- package/src/refresh.ts +74 -0
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Pi extension that registers a [Bifrost](https://docs.getbifrost.ai) LLM gateway
|
|
|
8
8
|
- `bifrost-openai` — the OpenAI-compatible surface, every chat model the gateway serves. First-party OpenAI GPT models (not `gpt-oss`) are sent over the Responses API (`api: "openai-responses"` per model): gpt-6-class models reject chat completions through the gateway.
|
|
9
9
|
- `bifrost-anthropic` — the Anthropic Messages surface, Claude models only. The Anthropic wire carries prompt-cache control blocks that only Claude accepts, so non-Claude models are registered on the OpenAI surface instead of being listed twice with one copy broken.
|
|
10
10
|
- Fetches each surface's model list, limits and pricing through `gateway-cli models --wire=…` at startup — no catalog is checked in.
|
|
11
|
-
- Seeds reasoning capability per model: catalog-flagged models register `reasoning: true`. On the OpenAI surface they also get a `thinkingLevelMap` translating pi's thinking levels to the model's effort values (unapplicable levels are hidden, never passed through by name) plus `supportsReasoningEffort`, so pi's per-request effort picker sends the provider's own values. On the Anthropic surface the capability is flagged and pi's Claude thinking handling applies. Models with effort levels also set `supportsPerMessageEffort` (omp only) so thinking-level changes keep the prompt cache. Cache-keeping has been verified live only on `claude-opus-5-5` through Bifrost; other models remain unverified ([G-012](../../KNOWN-GAPS.md)). Toggle-only reasoning models get no effort map. The default effort itself is pi's own thinking-level default.
|
|
11
|
+
- Seeds reasoning capability per model: catalog-flagged models register `reasoning: true`. On the OpenAI surface they also get a `thinkingLevelMap` translating pi's thinking levels to the model's effort values (unapplicable levels are hidden, never passed through by name) plus `supportsReasoningEffort`, so pi's per-request effort picker sends the provider's own values. On the Anthropic surface the capability is flagged and pi's Claude thinking handling applies; `claude-sonnet-5-5` also sets `forceAdaptiveThinking`, because pi 1.4.0 otherwise sends `thinking.type=enabled` and Bifrost returns a 400 (verified live 2026-11-25; Opus 5.5, Haiku 5.5 and Fable 5.2 work without it). Models with effort levels also set `supportsPerMessageEffort` (omp only) so thinking-level changes keep the prompt cache. Cache-keeping has been verified live only on `claude-opus-5-5` through Bifrost; other models remain unverified ([G-012](../../KNOWN-GAPS.md)). Toggle-only reasoning models get no effort map. The default effort itself is pi's own thinking-level default.
|
|
12
12
|
- Adds rolling prompt-cache breakpoints for GPT-6.2 on Bedrock (`bedrock/openai.gpt-6.2-*`). Those models reuse a cached prefix only up to a content block marked `prompt_cache_breakpoint: {mode: "explicit"}`, so without markers every agent turn was a full cache write. A `before_provider_request` hook marks the last input item of each request and the item that ended the previous request; string content on a marked item is sent as a one-block `input_text` list. Other models, 6.0/6.1 included, cache implicitly and are left untouched. Verified live 2026-11-24 on `gpt-6.2-sol` through omp: cache reads per turn went from 0, 0, 0, 0 to 44.5K, 46.0K, 47.6K, 49.0K (about 97% of the prompt).
|
|
13
13
|
- Sets `user` on chat-completions requests to Fireworks models (`fireworks/*`) to a hash of the conversation's first messages, Fireworks' documented session-affinity key (Bifrost forwards it), so a session's turns tend to reach the replica that holds its cached prefix. It is a hint, not a pin: measured live 2026-11-24 on `kimi-k3.1`, follow-up turns that missed the previous prefix fell from 26% to 18% (200 turns each). The rest is Fireworks routing ([G-020](../../KNOWN-GAPS.md)). A `user` the harness already set is kept.
|
|
14
14
|
- Never holds the key: pi resolves it per request via the `!gateway-cli key print` command form, so the per-project key cascade (environment override → project → user default) applies on every turn.
|
|
@@ -26,7 +26,11 @@ implementation, one command):
|
|
|
26
26
|
`~/.config/bifrost/keys/default.env`. It then reports how many models the
|
|
27
27
|
gateway offers and which are new or gone against the ids this session
|
|
28
28
|
registered. This mode reports only — it writes nothing, and the key is
|
|
29
|
-
never printed.
|
|
29
|
+
never printed. Project lookup also honors committed `.gateway/shared.json`,
|
|
30
|
+
with personal > shared > user precedence per deployment. A saved project
|
|
31
|
+
selection is the exception: refresh refuses before making a request and
|
|
32
|
+
tells you to install `gateway-cli`, because direct discovery cannot apply
|
|
33
|
+
the project context.
|
|
30
34
|
|
|
31
35
|
Either way the answer arrives as a notification, and any failure is one
|
|
32
36
|
readable line rather than an error thrown into the session.
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -360,9 +360,12 @@ export function toPiModels(
|
|
|
360
360
|
}
|
|
361
361
|
}
|
|
362
362
|
// Anthropic-messages surface: reasoning:true tells pi the model can
|
|
363
|
-
// think; pi's built-in Claude handling applies.
|
|
364
|
-
//
|
|
365
|
-
//
|
|
363
|
+
// think; pi's built-in Claude handling applies. pi 1.4.0 does not
|
|
364
|
+
// recognize claude-sonnet-5-5 as adaptive-only and sends
|
|
365
|
+
// thinking.type=enabled, which Bifrost relays as a 400 ("thinking.type.
|
|
366
|
+
// enabled is not supported ... Use thinking.type.adaptive"); verified
|
|
367
|
+
// live 2026-11-25. forceAdaptiveThinking fixes it. Opus 5.5, Haiku 5.5
|
|
368
|
+
// and Fable 5.2 work without it (same check), so only Sonnet 5.5 sets it.
|
|
366
369
|
//
|
|
367
370
|
// supportsPerMessageEffort: omp defaults it off for non-first-party
|
|
368
371
|
// endpoints, so a thinking-level change rewrote the top-level effort
|
|
@@ -370,8 +373,11 @@ export function toPiModels(
|
|
|
370
373
|
// block onward (full-context cache miss per change). Per-message effort
|
|
371
374
|
// keeps the prefix intact; Bifrost forwards the beta (verified live
|
|
372
375
|
// 2026-11-16 on claude-opus-5-5). Only for models with effort levels.
|
|
373
|
-
if (api === "anthropic-messages"
|
|
374
|
-
|
|
376
|
+
if (api === "anthropic-messages") {
|
|
377
|
+
const compat: NonNullable<ProviderModelConfig["compat"]> = {};
|
|
378
|
+
if (map) compat.supportsPerMessageEffort = true;
|
|
379
|
+
if (ADAPTIVE_ONLY.test(m.id)) compat.forceAdaptiveThinking = true;
|
|
380
|
+
if (Object.keys(compat).length > 0) model.compat = compat;
|
|
375
381
|
}
|
|
376
382
|
}
|
|
377
383
|
out.push(model);
|
|
@@ -394,6 +400,10 @@ const PI_LEVELS = [
|
|
|
394
400
|
* not the open-weight gpt-oss line, which chat completions serves fine. */
|
|
395
401
|
const OPENAI_GPT = /(?:^|\/)openai[./]gpt-(?!oss)/;
|
|
396
402
|
|
|
403
|
+
/** Claude ids that reject thinking.type=enabled but that pi does not know are
|
|
404
|
+
* adaptive-only; verified live 2026-11-25 (Opus/Haiku 5.5, Fable 5.2 are fine). */
|
|
405
|
+
const ADAPTIVE_ONLY = /^claude-sonnet-5-5$/;
|
|
406
|
+
|
|
397
407
|
/**
|
|
398
408
|
* Build a thinkingLevelMap from a model's advertised effort values. Each pi
|
|
399
409
|
* level maps to the same-named provider value when the model accepts it, and
|
package/src/plugin.test.ts
CHANGED
|
@@ -134,26 +134,29 @@ async function runExtension(): Promise<Registered[]> {
|
|
|
134
134
|
return (await runHarness()).registered;
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
/**
|
|
137
|
+
/** Process state as the suite found it, restored after each test. */
|
|
138
138
|
const ORIGINAL_HOME = process.env.HOME;
|
|
139
|
+
const ORIGINAL_CWD = process.cwd();
|
|
139
140
|
|
|
140
141
|
/** fetch as the suite found it, restored after each test. */
|
|
141
142
|
const ORIGINAL_FETCH = globalThis.fetch;
|
|
142
143
|
|
|
143
144
|
/** Env the refresh command reads; cleared so a developer's own cannot leak in. */
|
|
144
|
-
const REFRESH_ENV = ["BIFROST_GATEWAY_URL", "BIFROST_DISCOVERY_URL", "BIFROST_VK"];
|
|
145
|
+
const REFRESH_ENV = ["BIFROST_GATEWAY_URL", "BIFROST_DISCOVERY_URL", "BIFROST_VK", "XDG_CONFIG_HOME"];
|
|
145
146
|
const ORIGINAL_ENV: Record<string, string | undefined> = {};
|
|
146
147
|
for (const name of REFRESH_ENV) ORIGINAL_ENV[name] = process.env[name];
|
|
147
148
|
|
|
148
149
|
beforeEach(() => {
|
|
149
|
-
//
|
|
150
|
-
|
|
151
|
-
process.env.HOME =
|
|
150
|
+
// An empty temp HOME and cwd keep real user and repo config out of tests.
|
|
151
|
+
const home = mkdtempSync(join(tmpdir(), "gw-home-"));
|
|
152
|
+
process.env.HOME = home;
|
|
153
|
+
process.chdir(home);
|
|
152
154
|
// The CLI-less refresh path reads these; the tests that need them set them.
|
|
153
155
|
for (const name of REFRESH_ENV) delete process.env[name];
|
|
154
156
|
});
|
|
155
157
|
|
|
156
158
|
afterEach(() => {
|
|
159
|
+
process.chdir(ORIGINAL_CWD);
|
|
157
160
|
mock.restore();
|
|
158
161
|
globalThis.fetch = ORIGINAL_FETCH;
|
|
159
162
|
for (const name of REFRESH_ENV) {
|
|
@@ -491,6 +494,31 @@ describe("reasoning", () => {
|
|
|
491
494
|
expect(model.reasoning).toBe(true);
|
|
492
495
|
expect(model.compat).toBeUndefined();
|
|
493
496
|
});
|
|
497
|
+
|
|
498
|
+
test("forces adaptive thinking for Sonnet 5.5 only", async () => {
|
|
499
|
+
// pi 1.4.0 sends thinking.type=enabled for Sonnet 5.5, which Bifrost
|
|
500
|
+
// 400s (G-084); Opus, Haiku and Fable 5.x work without the flag.
|
|
501
|
+
const { toPiModels } = await import("./index.js");
|
|
502
|
+
const models = toPiModels(
|
|
503
|
+
{
|
|
504
|
+
models: [
|
|
505
|
+
"claude-sonnet-5-5",
|
|
506
|
+
"claude-opus-5-5",
|
|
507
|
+
"claude-haiku-5-5",
|
|
508
|
+
"claude-fable-5-2",
|
|
509
|
+
].map((id): CliModel => ({ ...KIMI, id })),
|
|
510
|
+
},
|
|
511
|
+
"anthropic-messages",
|
|
512
|
+
);
|
|
513
|
+
const byId = Object.fromEntries(models.map((m) => [m.id, m.compat]));
|
|
514
|
+
expect(byId["claude-sonnet-5-5"]).toEqual({
|
|
515
|
+
supportsPerMessageEffort: true,
|
|
516
|
+
forceAdaptiveThinking: true,
|
|
517
|
+
});
|
|
518
|
+
for (const id of ["claude-opus-5-5", "claude-haiku-5-5", "claude-fable-5-2"]) {
|
|
519
|
+
expect(byId[id]).toEqual({ supportsPerMessageEffort: true });
|
|
520
|
+
}
|
|
521
|
+
});
|
|
494
522
|
});
|
|
495
523
|
|
|
496
524
|
describe("responses routing", () => {
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { refresh, type FetchLike } from "./refresh.js";
|
|
6
|
+
|
|
7
|
+
const INSTALL_MESSAGE =
|
|
8
|
+
"Bifrost project context requires gateway-cli; install gateway-cli and put it on PATH";
|
|
9
|
+
|
|
10
|
+
function fixture() {
|
|
11
|
+
const home = mkdtempSync(join(tmpdir(), "gateway-project-refresh-"));
|
|
12
|
+
const root = join(home, "repo");
|
|
13
|
+
const cwd = join(root, "nested");
|
|
14
|
+
const configHome = join(home, "config");
|
|
15
|
+
mkdirSync(cwd, { recursive: true });
|
|
16
|
+
mkdirSync(join(root, ".gateway"), { recursive: true });
|
|
17
|
+
mkdirSync(join(configHome, "bifrost"), { recursive: true });
|
|
18
|
+
const env: Record<string, string | undefined> = {
|
|
19
|
+
HOME: home,
|
|
20
|
+
XDG_CONFIG_HOME: configHome,
|
|
21
|
+
BIFROST_GATEWAY_URL: "https://gateway.example",
|
|
22
|
+
BIFROST_VK: "sk-bf-fixture",
|
|
23
|
+
};
|
|
24
|
+
const save = (scope: "user" | "repo" | "shared", contents: unknown) => {
|
|
25
|
+
const path =
|
|
26
|
+
scope === "user"
|
|
27
|
+
? join(configHome, "bifrost", "config.json")
|
|
28
|
+
: join(root, ".gateway", scope === "repo" ? "config.json" : "shared.json");
|
|
29
|
+
writeFileSync(
|
|
30
|
+
path,
|
|
31
|
+
typeof contents === "string" ? contents : JSON.stringify(contents),
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
const requests: { url: string; headers?: Record<string, string> }[] = [];
|
|
35
|
+
const commands: string[][] = [];
|
|
36
|
+
const fetchImpl: FetchLike = async (url, init) => {
|
|
37
|
+
requests.push({ url, headers: init?.headers });
|
|
38
|
+
return { ok: true, json: async () => ({ data: [{ id: "one" }] }) };
|
|
39
|
+
};
|
|
40
|
+
const exec = async (args: string[]) => {
|
|
41
|
+
commands.push(args);
|
|
42
|
+
return null;
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
home,
|
|
46
|
+
root,
|
|
47
|
+
cwd,
|
|
48
|
+
env,
|
|
49
|
+
save,
|
|
50
|
+
requests,
|
|
51
|
+
commands,
|
|
52
|
+
fetchImpl,
|
|
53
|
+
exec,
|
|
54
|
+
clean: () => rmSync(home, { recursive: true, force: true }),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
test("repo project selection refuses fallback before any request", async () => {
|
|
59
|
+
const f = fixture();
|
|
60
|
+
try {
|
|
61
|
+
f.save("repo", { bifrost_projects: { prod: { id: "project-1" } } });
|
|
62
|
+
const result = await refresh({
|
|
63
|
+
known: ["old"],
|
|
64
|
+
cwd: f.cwd,
|
|
65
|
+
env: f.env,
|
|
66
|
+
fetchImpl: f.fetchImpl,
|
|
67
|
+
exec: f.exec,
|
|
68
|
+
});
|
|
69
|
+
expect(result.error).toBe(INSTALL_MESSAGE);
|
|
70
|
+
expect(result.live).toEqual([]);
|
|
71
|
+
expect(f.requests).toHaveLength(0);
|
|
72
|
+
} finally {
|
|
73
|
+
f.clean();
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("user project selection refuses fallback through either config home", async () => {
|
|
78
|
+
for (const useXdg of [true, false]) {
|
|
79
|
+
const f = fixture();
|
|
80
|
+
try {
|
|
81
|
+
if (useXdg) {
|
|
82
|
+
f.save("user", { bifrost_projects: { prod: { id: "project-1" } } });
|
|
83
|
+
} else {
|
|
84
|
+
f.env.XDG_CONFIG_HOME = undefined;
|
|
85
|
+
const config = join(f.home, ".config", "bifrost");
|
|
86
|
+
mkdirSync(config, { recursive: true });
|
|
87
|
+
writeFileSync(
|
|
88
|
+
join(config, "config.json"),
|
|
89
|
+
JSON.stringify({ bifrost_projects: { prod: { id: "project-1" } } }),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
const result = await refresh({
|
|
93
|
+
known: [],
|
|
94
|
+
cwd: f.cwd,
|
|
95
|
+
env: f.env,
|
|
96
|
+
fetchImpl: f.fetchImpl,
|
|
97
|
+
exec: f.exec,
|
|
98
|
+
});
|
|
99
|
+
expect(result.error).toBe(INSTALL_MESSAGE);
|
|
100
|
+
expect(f.requests).toHaveLength(0);
|
|
101
|
+
} finally {
|
|
102
|
+
f.clean();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("an empty repo record masks a user project selection", async () => {
|
|
108
|
+
const f = fixture();
|
|
109
|
+
try {
|
|
110
|
+
f.save("user", { bifrost_projects: { prod: { id: "user-project" } } });
|
|
111
|
+
f.save("repo", { bifrost_projects: { prod: {} } });
|
|
112
|
+
const result = await refresh({
|
|
113
|
+
known: [],
|
|
114
|
+
cwd: f.cwd,
|
|
115
|
+
env: f.env,
|
|
116
|
+
fetchImpl: f.fetchImpl,
|
|
117
|
+
exec: f.exec,
|
|
118
|
+
});
|
|
119
|
+
expect(result.error).toBeUndefined();
|
|
120
|
+
expect(result.live).toEqual(["one"]);
|
|
121
|
+
expect(f.requests).toHaveLength(1);
|
|
122
|
+
} finally {
|
|
123
|
+
f.clean();
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("shared project selection refuses fallback before any request", async () => {
|
|
128
|
+
const f = fixture();
|
|
129
|
+
try {
|
|
130
|
+
f.save("shared", { bifrost_projects: { prod: { id: "shared-project" } } });
|
|
131
|
+
const result = await refresh({
|
|
132
|
+
known: [],
|
|
133
|
+
cwd: f.cwd,
|
|
134
|
+
env: f.env,
|
|
135
|
+
fetchImpl: f.fetchImpl,
|
|
136
|
+
exec: f.exec,
|
|
137
|
+
});
|
|
138
|
+
expect(result.error).toBe(INSTALL_MESSAGE);
|
|
139
|
+
expect(result.live).toEqual([]);
|
|
140
|
+
expect(f.requests).toHaveLength(0);
|
|
141
|
+
} finally {
|
|
142
|
+
f.clean();
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("an empty personal record masks a shared project selection", async () => {
|
|
147
|
+
const f = fixture();
|
|
148
|
+
try {
|
|
149
|
+
f.save("user", { bifrost_projects: { prod: { id: "user-project" } } });
|
|
150
|
+
f.save("shared", {
|
|
151
|
+
env: "staging",
|
|
152
|
+
bifrost_projects: { staging: { id: "shared-project" } },
|
|
153
|
+
});
|
|
154
|
+
f.save("repo", { bifrost_projects: { staging: {} } });
|
|
155
|
+
const result = await refresh({
|
|
156
|
+
known: [],
|
|
157
|
+
cwd: f.cwd,
|
|
158
|
+
env: f.env,
|
|
159
|
+
fetchImpl: f.fetchImpl,
|
|
160
|
+
exec: f.exec,
|
|
161
|
+
});
|
|
162
|
+
expect(result.error).toBeUndefined();
|
|
163
|
+
expect(result.live).toEqual(["one"]);
|
|
164
|
+
expect(f.requests).toHaveLength(1);
|
|
165
|
+
} finally {
|
|
166
|
+
f.clean();
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test("shared config with an unknown field fails closed", async () => {
|
|
171
|
+
const f = fixture();
|
|
172
|
+
try {
|
|
173
|
+
f.save("shared", { key: "not-allowed", bifrost_projects: { prod: {} } });
|
|
174
|
+
const result = await refresh({
|
|
175
|
+
known: [],
|
|
176
|
+
cwd: f.cwd,
|
|
177
|
+
env: f.env,
|
|
178
|
+
fetchImpl: f.fetchImpl,
|
|
179
|
+
exec: f.exec,
|
|
180
|
+
});
|
|
181
|
+
expect(result.error).toBe(INSTALL_MESSAGE);
|
|
182
|
+
expect(result.live).toEqual([]);
|
|
183
|
+
expect(f.requests).toHaveLength(0);
|
|
184
|
+
} finally {
|
|
185
|
+
f.clean();
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test("a staging-only selection does not block a prod fallback", async () => {
|
|
190
|
+
for (const scope of ["user", "repo"] as const) {
|
|
191
|
+
const f = fixture();
|
|
192
|
+
try {
|
|
193
|
+
f.save(scope, { bifrost_projects: { staging: { id: "stage-project" } } });
|
|
194
|
+
const result = await refresh({
|
|
195
|
+
known: [],
|
|
196
|
+
cwd: f.cwd,
|
|
197
|
+
env: f.env,
|
|
198
|
+
fetchImpl: f.fetchImpl,
|
|
199
|
+
exec: f.exec,
|
|
200
|
+
});
|
|
201
|
+
expect(result.error).toBeUndefined();
|
|
202
|
+
expect(result.live).toEqual(["one"]);
|
|
203
|
+
expect(f.requests).toHaveLength(1);
|
|
204
|
+
} finally {
|
|
205
|
+
f.clean();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("repo deployment selects the staging project for refusal", async () => {
|
|
211
|
+
const f = fixture();
|
|
212
|
+
try {
|
|
213
|
+
f.save("repo", {
|
|
214
|
+
env: "staging",
|
|
215
|
+
bifrost_projects: { staging: { id: "stage-project" } },
|
|
216
|
+
});
|
|
217
|
+
const result = await refresh({
|
|
218
|
+
known: [],
|
|
219
|
+
cwd: f.cwd,
|
|
220
|
+
env: f.env,
|
|
221
|
+
fetchImpl: f.fetchImpl,
|
|
222
|
+
exec: f.exec,
|
|
223
|
+
});
|
|
224
|
+
expect(result.error).toBe(INSTALL_MESSAGE);
|
|
225
|
+
expect(f.requests).toHaveLength(0);
|
|
226
|
+
} finally {
|
|
227
|
+
f.clean();
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test("personal deployment overrides shared deployment", async () => {
|
|
232
|
+
const f = fixture();
|
|
233
|
+
try {
|
|
234
|
+
f.save("shared", {
|
|
235
|
+
env: "prod",
|
|
236
|
+
bifrost_projects: { prod: { id: "shared-project" } },
|
|
237
|
+
});
|
|
238
|
+
f.save("repo", { env: "staging", bifrost_projects: { staging: {} } });
|
|
239
|
+
const result = await refresh({
|
|
240
|
+
known: [],
|
|
241
|
+
cwd: f.cwd,
|
|
242
|
+
env: f.env,
|
|
243
|
+
fetchImpl: f.fetchImpl,
|
|
244
|
+
exec: f.exec,
|
|
245
|
+
});
|
|
246
|
+
expect(result.error).toBeUndefined();
|
|
247
|
+
expect(result.live).toEqual(["one"]);
|
|
248
|
+
expect(f.requests).toHaveLength(1);
|
|
249
|
+
} finally {
|
|
250
|
+
f.clean();
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test("no selection preserves the main-branch request and ignores outer repo configs", async () => {
|
|
255
|
+
const f = fixture();
|
|
256
|
+
try {
|
|
257
|
+
for (const configure of [
|
|
258
|
+
() => {},
|
|
259
|
+
() => f.save("user", { bifrost_projects: { prod: {} } }),
|
|
260
|
+
() => {
|
|
261
|
+
f.save("repo", { bifrost_projects: { prod: { id: "outer" } } });
|
|
262
|
+
f.save("shared", { bifrost_projects: { prod: { id: "outer-shared" } } });
|
|
263
|
+
const nearest = join(f.cwd, ".gateway");
|
|
264
|
+
mkdirSync(nearest);
|
|
265
|
+
writeFileSync(
|
|
266
|
+
join(nearest, "config.json"),
|
|
267
|
+
JSON.stringify({
|
|
268
|
+
headers: { "x-bf-project-id": "legacy", "X-Org": "team" },
|
|
269
|
+
bifrost_projects: { prod: { id: "" }, staging: {} },
|
|
270
|
+
}),
|
|
271
|
+
);
|
|
272
|
+
},
|
|
273
|
+
]) {
|
|
274
|
+
configure();
|
|
275
|
+
const result = await refresh({
|
|
276
|
+
known: ["old"],
|
|
277
|
+
cwd: f.cwd,
|
|
278
|
+
env: f.env,
|
|
279
|
+
fetchImpl: f.fetchImpl,
|
|
280
|
+
exec: f.exec,
|
|
281
|
+
});
|
|
282
|
+
expect(result).toEqual({
|
|
283
|
+
source: "gateway",
|
|
284
|
+
live: ["one"],
|
|
285
|
+
added: ["one"],
|
|
286
|
+
removed: ["old"],
|
|
287
|
+
knownCount: 1,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
expect(f.commands).toEqual([["sync"], ["sync"], ["sync"]]);
|
|
291
|
+
expect(f.requests).toHaveLength(3);
|
|
292
|
+
for (const request of f.requests) {
|
|
293
|
+
expect(request.url).toBe("https://gateway.example/v1/models");
|
|
294
|
+
expect(request.headers).toEqual({ Authorization: "Bearer sk-bf-fixture" });
|
|
295
|
+
}
|
|
296
|
+
} finally {
|
|
297
|
+
f.clean();
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test("malformed JSON in any config fails closed with the install message", async () => {
|
|
302
|
+
for (const scope of ["user", "repo", "shared"] as const) {
|
|
303
|
+
const f = fixture();
|
|
304
|
+
try {
|
|
305
|
+
f.save(scope, "{");
|
|
306
|
+
const result = await refresh({
|
|
307
|
+
known: [],
|
|
308
|
+
cwd: f.cwd,
|
|
309
|
+
env: f.env,
|
|
310
|
+
fetchImpl: f.fetchImpl,
|
|
311
|
+
exec: f.exec,
|
|
312
|
+
});
|
|
313
|
+
expect(result.error).toBe(INSTALL_MESSAGE);
|
|
314
|
+
expect(f.requests).toHaveLength(0);
|
|
315
|
+
} finally {
|
|
316
|
+
f.clean();
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
test("a working CLI still handles a selected project without discovery", async () => {
|
|
322
|
+
const f = fixture();
|
|
323
|
+
try {
|
|
324
|
+
f.save("repo", { bifrost_projects: { prod: { id: "project-1" } } });
|
|
325
|
+
const commands: string[][] = [];
|
|
326
|
+
const result = await refresh({
|
|
327
|
+
known: [],
|
|
328
|
+
cwd: f.cwd,
|
|
329
|
+
env: f.env,
|
|
330
|
+
fetchImpl: f.fetchImpl,
|
|
331
|
+
exec: async (args) => {
|
|
332
|
+
commands.push(args);
|
|
333
|
+
return args[0] === "sync" ? "synced" : null;
|
|
334
|
+
},
|
|
335
|
+
});
|
|
336
|
+
expect(result).toEqual({
|
|
337
|
+
source: "cli",
|
|
338
|
+
live: [],
|
|
339
|
+
added: [],
|
|
340
|
+
removed: [],
|
|
341
|
+
knownCount: 0,
|
|
342
|
+
cliOutput: "synced",
|
|
343
|
+
});
|
|
344
|
+
expect(commands).toEqual([["sync"]]);
|
|
345
|
+
expect(f.requests).toHaveLength(0);
|
|
346
|
+
} finally {
|
|
347
|
+
f.clean();
|
|
348
|
+
}
|
|
349
|
+
});
|
package/src/refresh.test.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
1
|
+
import { afterAll, describe, expect, test } from "bun:test";
|
|
2
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
2
5
|
import {
|
|
3
6
|
diffModels,
|
|
4
7
|
gatewayFromEnv,
|
|
@@ -80,9 +83,13 @@ describe("liveModelIds and diffModels", () => {
|
|
|
80
83
|
});
|
|
81
84
|
|
|
82
85
|
describe("refresh", () => {
|
|
86
|
+
const cwd = mkdtempSync(join(tmpdir(), "gateway-refresh-test-"));
|
|
87
|
+
afterAll(() => rmSync(cwd, { recursive: true, force: true }));
|
|
88
|
+
|
|
83
89
|
test("the CLI wins when it answers, and no request is made", async () => {
|
|
84
90
|
const { fetchImpl, calls } = stubFetch({});
|
|
85
91
|
const result = await refresh({
|
|
92
|
+
cwd,
|
|
86
93
|
known: ["a"],
|
|
87
94
|
exec: async () => "model selection updated (openai): added 1 new model(s): b",
|
|
88
95
|
fetchImpl,
|
|
@@ -96,6 +103,7 @@ describe("refresh", () => {
|
|
|
96
103
|
test("without the CLI it lists the gateway directly and diffs the session", async () => {
|
|
97
104
|
const { fetchImpl, calls } = stubFetch({ data: [{ id: "a" }, { id: "b" }] });
|
|
98
105
|
const result = await refresh({
|
|
106
|
+
cwd,
|
|
99
107
|
known: ["a", "old"],
|
|
100
108
|
exec: async () => null,
|
|
101
109
|
fetchImpl,
|
|
@@ -115,6 +123,7 @@ describe("refresh", () => {
|
|
|
115
123
|
test("a key file stands in for the environment key", async () => {
|
|
116
124
|
const { fetchImpl } = stubFetch({ data: [{ id: "a" }] });
|
|
117
125
|
const result = await refresh({
|
|
126
|
+
cwd,
|
|
118
127
|
known: ["a"],
|
|
119
128
|
exec: async () => null,
|
|
120
129
|
readKeyFile: () => KEY,
|
|
@@ -126,10 +135,11 @@ describe("refresh", () => {
|
|
|
126
135
|
});
|
|
127
136
|
|
|
128
137
|
test("says what is missing instead of failing silently", async () => {
|
|
129
|
-
const noUrl = await refresh({ known: [], exec: async () => null, env: {} });
|
|
138
|
+
const noUrl = await refresh({ cwd, known: [], exec: async () => null, env: {} });
|
|
130
139
|
expect(noUrl.error).toContain("BIFROST_GATEWAY_URL");
|
|
131
140
|
|
|
132
141
|
const noKey = await refresh({
|
|
142
|
+
cwd,
|
|
133
143
|
known: [],
|
|
134
144
|
exec: async () => null,
|
|
135
145
|
env: { BIFROST_GATEWAY_URL: "https://gw.example" },
|
|
@@ -138,6 +148,7 @@ describe("refresh", () => {
|
|
|
138
148
|
|
|
139
149
|
const { fetchImpl } = stubFetch({}, false);
|
|
140
150
|
const rejected = await refresh({
|
|
151
|
+
cwd,
|
|
141
152
|
known: [],
|
|
142
153
|
exec: async () => null,
|
|
143
154
|
fetchImpl,
|
package/src/refresh.ts
CHANGED
|
@@ -14,6 +14,68 @@
|
|
|
14
14
|
* Byte-identical in packages/pi-bifrost and packages/opencode-bifrost;
|
|
15
15
|
* `make check-refresh-sync` fails the build if they drift.
|
|
16
16
|
*/
|
|
17
|
+
import { readFileSync } from "node:fs";
|
|
18
|
+
import { dirname, join, resolve } from "node:path";
|
|
19
|
+
|
|
20
|
+
type ProjectConfig = {
|
|
21
|
+
env?: string;
|
|
22
|
+
bifrost_projects?: Record<string, { id?: unknown }>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const PROJECT_CLI_REQUIRED =
|
|
26
|
+
"Bifrost project context requires gateway-cli; install gateway-cli and put it on PATH";
|
|
27
|
+
|
|
28
|
+
function readProjectConfig(path: string): ProjectConfig | null | undefined {
|
|
29
|
+
try {
|
|
30
|
+
return JSON.parse(readFileSync(path, "utf8")) as ProjectConfig | null;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function hasProject(
|
|
38
|
+
config: ProjectConfig | null | undefined,
|
|
39
|
+
deployment: string,
|
|
40
|
+
): boolean {
|
|
41
|
+
const project = config?.bifrost_projects?.[deployment];
|
|
42
|
+
return typeof project?.id === "string" && project.id.length > 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function projectSelected(
|
|
46
|
+
cwd: string,
|
|
47
|
+
env: Record<string, string | undefined>,
|
|
48
|
+
): boolean {
|
|
49
|
+
try {
|
|
50
|
+
const base =
|
|
51
|
+
env.XDG_CONFIG_HOME || (env.HOME ? join(env.HOME, ".config") : null);
|
|
52
|
+
const user = base
|
|
53
|
+
? readProjectConfig(join(base, "bifrost", "config.json"))
|
|
54
|
+
: undefined;
|
|
55
|
+
let repo: ProjectConfig | null | undefined;
|
|
56
|
+
let shared: ProjectConfig | null | undefined;
|
|
57
|
+
for (let dir = resolve(cwd); ; dir = dirname(dir)) {
|
|
58
|
+
repo = readProjectConfig(join(dir, ".gateway", "config.json"));
|
|
59
|
+
shared = readProjectConfig(join(dir, ".gateway", "shared.json"));
|
|
60
|
+
if (repo !== undefined || shared !== undefined || dirname(dir) === dir) break;
|
|
61
|
+
}
|
|
62
|
+
if (
|
|
63
|
+
shared &&
|
|
64
|
+
Object.keys(shared).some((field) => field !== "env" && field !== "bifrost_projects")
|
|
65
|
+
) {
|
|
66
|
+
throw new Error("shared config may only contain env and bifrost_projects");
|
|
67
|
+
}
|
|
68
|
+
const deployment = repo?.env || shared?.env || user?.env || "prod";
|
|
69
|
+
for (const config of [repo, shared, user]) {
|
|
70
|
+
if (config?.bifrost_projects && deployment in config.bifrost_projects) {
|
|
71
|
+
return hasProject(config, deployment);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
} catch {
|
|
76
|
+
return true; // malformed or unreadable config fails closed
|
|
77
|
+
}
|
|
78
|
+
}
|
|
17
79
|
|
|
18
80
|
/** The subcommand name both harnesses register. */
|
|
19
81
|
export const REFRESH_COMMAND = "bifrost-refresh";
|
|
@@ -179,6 +241,8 @@ export async function refresh(opts: {
|
|
|
179
241
|
storedKey?: () => Promise<string | null>;
|
|
180
242
|
fetchImpl?: FetchLike;
|
|
181
243
|
env?: Record<string, string | undefined>;
|
|
244
|
+
/** Same working directory as the normal CLI-backed harness path. */
|
|
245
|
+
cwd?: string;
|
|
182
246
|
}): Promise<RefreshResult> {
|
|
183
247
|
const knownCount = opts.known.length;
|
|
184
248
|
const cliOutput = await opts.exec(["sync"]);
|
|
@@ -189,6 +253,16 @@ export async function refresh(opts: {
|
|
|
189
253
|
}
|
|
190
254
|
|
|
191
255
|
const env = opts.env ?? {};
|
|
256
|
+
if (projectSelected(opts.cwd ?? process.cwd(), env)) {
|
|
257
|
+
return {
|
|
258
|
+
source: "gateway",
|
|
259
|
+
live: [],
|
|
260
|
+
added: [],
|
|
261
|
+
removed: [],
|
|
262
|
+
knownCount,
|
|
263
|
+
error: PROJECT_CLI_REQUIRED,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
192
266
|
const target = gatewayFromEnv(env);
|
|
193
267
|
if (!target) {
|
|
194
268
|
return {
|