@stablekernel/pi-bifrost 0.12.0 → 0.13.0
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 +5 -1
- package/package.json +1 -1
- package/src/plugin.test.ts +8 -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
|
@@ -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/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) {
|
|
@@ -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 {
|