@vellumai/cli 0.8.9-staging.1 → 0.8.9-staging.3
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/node_modules/@vellumai/local-mode/src/__tests__/loopback-auth.test.ts +88 -0
- package/node_modules/@vellumai/local-mode/src/index.ts +3 -0
- package/node_modules/@vellumai/local-mode/src/lockfile.ts +15 -0
- package/node_modules/@vellumai/local-mode/src/util.ts +33 -0
- package/package.json +1 -1
- package/src/__tests__/assistant-client-refresh.test.ts +65 -4
- package/src/__tests__/client-tui-refresh.test.ts +50 -6
- package/src/__tests__/guardian-token.test.ts +130 -4
- package/src/__tests__/message.test.ts +86 -0
- package/src/__tests__/teleport.test.ts +1 -0
- package/src/__tests__/tui-midsession-refresh.test.ts +68 -9
- package/src/commands/client.ts +100 -58
- package/src/commands/hatch.ts +14 -4
- package/src/commands/message.ts +109 -19
- package/src/commands/teleport.ts +2 -0
- package/src/components/DefaultMainScreen.tsx +27 -2
- package/src/lib/__tests__/docker.test.ts +99 -0
- package/src/lib/assistant-client.ts +31 -13
- package/src/lib/docker.ts +97 -29
- package/src/lib/flag-args.test.ts +89 -0
- package/src/lib/flag-args.ts +74 -0
- package/src/lib/guardian-token.ts +54 -0
- package/src/lib/hatch-local.ts +2 -0
- package/src/lib/local.ts +6 -1
- package/src/lib/runtime-url.ts +90 -0
- package/src/lib/statefulset.ts +9 -0
|
@@ -12,7 +12,12 @@ import { Box, render as inkRender, Text, useInput, useStdout } from "ink";
|
|
|
12
12
|
import { SPECIES_CONFIG, type Species } from "../lib/constants";
|
|
13
13
|
import { lookupAssistantByIdentifier } from "../lib/assistant-config";
|
|
14
14
|
import { checkHealth } from "../lib/health-check";
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
guardianTokenDueForRenewal,
|
|
17
|
+
loadGuardianToken,
|
|
18
|
+
refreshGuardianToken,
|
|
19
|
+
} from "../lib/guardian-token";
|
|
20
|
+
import { trustedRefreshUrl } from "../lib/runtime-url";
|
|
16
21
|
import { appendHistory, loadHistory } from "../lib/input-history";
|
|
17
22
|
import { tuiLog } from "../lib/tui-log";
|
|
18
23
|
import { segmentsToPlainText } from "../lib/segments-to-plain-text";
|
|
@@ -193,6 +198,16 @@ function friendlyErrorMessage(status: number, body: string): string {
|
|
|
193
198
|
* and access-only tokens. Because the TUI threads one shared `auth` object by
|
|
194
199
|
* reference, mutating it here propagates to every later request and the SSE
|
|
195
200
|
* reconnect — no callback threading needed.
|
|
201
|
+
*
|
|
202
|
+
* SECURITY: the refresh is bound to the paired entry's persisted runtime URL.
|
|
203
|
+
* `vellum client` lets `--url`/`-u` override the runtime URL while still using
|
|
204
|
+
* the selected paired entry's stored guardian token, so a victim pointed at an
|
|
205
|
+
* attacker-controlled (or poisoned/redirected) URL that returns 401 must NOT
|
|
206
|
+
* cause us to POST the long-lived refreshToken + deviceId to that origin. We
|
|
207
|
+
* therefore (a) refuse to refresh unless `baseUrl` normalizes to one of the
|
|
208
|
+
* entry's persisted URLs, and (b) send the refresh to the persisted URL rather
|
|
209
|
+
* than the caller-supplied `baseUrl` — defense in depth if the gate is ever
|
|
210
|
+
* bypassed.
|
|
196
211
|
*/
|
|
197
212
|
export async function maybeRefreshAuthHeaders(
|
|
198
213
|
baseUrl: string,
|
|
@@ -210,11 +225,21 @@ export async function maybeRefreshAuthHeaders(
|
|
|
210
225
|
return false;
|
|
211
226
|
}
|
|
212
227
|
|
|
228
|
+
// Bind the refresh origin to the persisted paired entry: refuse (and never
|
|
229
|
+
// leak credentials) if `baseUrl` was overridden via --url or poisoned to an
|
|
230
|
+
// origin that isn't one of the entry's persisted URLs. `refreshUrl` is the
|
|
231
|
+
// trusted persisted URL we actually send to.
|
|
232
|
+
const refreshUrl = trustedRefreshUrl(lookup.entry, baseUrl);
|
|
233
|
+
if (!refreshUrl) return false;
|
|
234
|
+
|
|
213
235
|
const stored = loadGuardianToken(assistantId);
|
|
214
236
|
if (!stored || stored.accessToken !== bearer || !stored.refreshToken) {
|
|
215
237
|
return false;
|
|
216
238
|
}
|
|
217
|
-
|
|
239
|
+
// Only refresh once the token is actually due for renewal, so a forged 401
|
|
240
|
+
// on a still-valid token can't coax out the long-lived refresh credential.
|
|
241
|
+
if (!guardianTokenDueForRenewal(stored)) return false;
|
|
242
|
+
const refreshed = await refreshGuardianToken(refreshUrl, assistantId);
|
|
218
243
|
if (!refreshed?.accessToken) return false;
|
|
219
244
|
auth["Authorization"] = `Bearer ${refreshed.accessToken}`;
|
|
220
245
|
return true;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, test, expect } from "bun:test";
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "fs";
|
|
3
|
+
import { tmpdir } from "os";
|
|
4
|
+
import { join } from "path";
|
|
2
5
|
import {
|
|
3
6
|
ASSISTANT_INTERNAL_PORT,
|
|
4
7
|
AVATAR_DEVICE_ENV_VAR,
|
|
8
|
+
collectWatchTargets,
|
|
5
9
|
dockerResourceNames,
|
|
6
10
|
resolveAvatarDevicePath,
|
|
7
11
|
resolveDockerHatchMode,
|
|
@@ -277,3 +281,98 @@ describe("resolveDockerHatchMode", () => {
|
|
|
277
281
|
).toEqual({ build: false, watcher: false, fellBackToPull: true });
|
|
278
282
|
});
|
|
279
283
|
});
|
|
284
|
+
|
|
285
|
+
describe("collectWatchTargets", () => {
|
|
286
|
+
let repoRoot: string;
|
|
287
|
+
|
|
288
|
+
beforeEach(() => {
|
|
289
|
+
repoRoot = mkdtempSync(join(tmpdir(), "vellum-watch-"));
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
afterEach(() => {
|
|
293
|
+
rmSync(repoRoot, { recursive: true, force: true });
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
function scaffold(
|
|
297
|
+
relDir: string,
|
|
298
|
+
{ src = true, pkg = true, dockerfile = false } = {},
|
|
299
|
+
): void {
|
|
300
|
+
mkdirSync(join(repoRoot, relDir), { recursive: true });
|
|
301
|
+
if (src) mkdirSync(join(repoRoot, relDir, "src"), { recursive: true });
|
|
302
|
+
if (pkg) writeFileSync(join(repoRoot, relDir, "package.json"), "{}");
|
|
303
|
+
if (dockerfile) writeFileSync(join(repoRoot, relDir, "Dockerfile"), "");
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
test("scopes watch targets to src/, package.json, and the Dockerfile", () => {
|
|
307
|
+
// GIVEN the three services (each with a Dockerfile) plus a couple of
|
|
308
|
+
// shared packages (libraries, no Dockerfile)
|
|
309
|
+
scaffold("assistant", { dockerfile: true });
|
|
310
|
+
scaffold("credential-executor", { dockerfile: true });
|
|
311
|
+
scaffold("gateway", { dockerfile: true });
|
|
312
|
+
scaffold("packages/service-contracts");
|
|
313
|
+
scaffold("packages/local-mode");
|
|
314
|
+
|
|
315
|
+
// WHEN we collect the watch targets
|
|
316
|
+
const { dirs, files } = collectWatchTargets(repoRoot);
|
|
317
|
+
|
|
318
|
+
// THEN only the src/ directories are watched recursively
|
|
319
|
+
expect(dirs.sort()).toEqual(
|
|
320
|
+
[
|
|
321
|
+
join(repoRoot, "assistant", "src"),
|
|
322
|
+
join(repoRoot, "credential-executor", "src"),
|
|
323
|
+
join(repoRoot, "gateway", "src"),
|
|
324
|
+
join(repoRoot, "packages", "local-mode", "src"),
|
|
325
|
+
join(repoRoot, "packages", "service-contracts", "src"),
|
|
326
|
+
].sort(),
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
// AND the package.json manifests and service Dockerfiles are watched as
|
|
330
|
+
// individual files (packages have no Dockerfile, so none is emitted)
|
|
331
|
+
expect(files.sort()).toEqual(
|
|
332
|
+
[
|
|
333
|
+
join(repoRoot, "assistant", "package.json"),
|
|
334
|
+
join(repoRoot, "assistant", "Dockerfile"),
|
|
335
|
+
join(repoRoot, "credential-executor", "package.json"),
|
|
336
|
+
join(repoRoot, "credential-executor", "Dockerfile"),
|
|
337
|
+
join(repoRoot, "gateway", "package.json"),
|
|
338
|
+
join(repoRoot, "gateway", "Dockerfile"),
|
|
339
|
+
join(repoRoot, "packages", "local-mode", "package.json"),
|
|
340
|
+
join(repoRoot, "packages", "service-contracts", "package.json"),
|
|
341
|
+
].sort(),
|
|
342
|
+
);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
test("never watches .claude/ command symlinks that crash the watcher", () => {
|
|
346
|
+
// GIVEN an assistant service whose .claude/commands holds a dangling
|
|
347
|
+
// symlink (as it does in a fresh checkout)
|
|
348
|
+
scaffold("assistant");
|
|
349
|
+
mkdirSync(join(repoRoot, "assistant", ".claude", "commands"), {
|
|
350
|
+
recursive: true,
|
|
351
|
+
});
|
|
352
|
+
symlinkSync(
|
|
353
|
+
join(repoRoot, "does-not-exist", "do.md"),
|
|
354
|
+
join(repoRoot, "assistant", ".claude", "commands", "do.md"),
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
// WHEN we collect the watch targets
|
|
358
|
+
const { dirs, files } = collectWatchTargets(repoRoot);
|
|
359
|
+
|
|
360
|
+
// THEN no watched path reaches into the .claude/ tree
|
|
361
|
+
const all = [...dirs, ...files];
|
|
362
|
+
expect(all.some((p) => p.includes(".claude"))).toBe(false);
|
|
363
|
+
expect(dirs).toContain(join(repoRoot, "assistant", "src"));
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
test("skips roots missing a src/ directory or package.json", () => {
|
|
367
|
+
// GIVEN a service with only a manifest and a package with only a src/ dir
|
|
368
|
+
scaffold("gateway", { src: false, pkg: true });
|
|
369
|
+
scaffold("packages/contracts-only", { src: true, pkg: false });
|
|
370
|
+
|
|
371
|
+
// WHEN we collect the watch targets
|
|
372
|
+
const { dirs, files } = collectWatchTargets(repoRoot);
|
|
373
|
+
|
|
374
|
+
// THEN absent paths are not emitted
|
|
375
|
+
expect(dirs).toEqual([join(repoRoot, "packages", "contracts-only", "src")]);
|
|
376
|
+
expect(files).toEqual([join(repoRoot, "gateway", "package.json")]);
|
|
377
|
+
});
|
|
378
|
+
});
|
|
@@ -14,7 +14,11 @@
|
|
|
14
14
|
|
|
15
15
|
import { resolveAssistant } from "./assistant-config.js";
|
|
16
16
|
import { GATEWAY_PORT } from "./constants.js";
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
loadGuardianToken,
|
|
19
|
+
refreshGuardianToken,
|
|
20
|
+
guardianTokenDueForRenewal,
|
|
21
|
+
} from "./guardian-token.js";
|
|
18
22
|
|
|
19
23
|
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
20
24
|
const FALLBACK_RUNTIME_URL = `http://127.0.0.1:${GATEWAY_PORT}`;
|
|
@@ -219,21 +223,35 @@ export class AssistantClient {
|
|
|
219
223
|
|
|
220
224
|
const response = await doFetch();
|
|
221
225
|
|
|
222
|
-
// Reactive auto-refresh
|
|
223
|
-
//
|
|
224
|
-
// and
|
|
225
|
-
//
|
|
226
|
-
// just see the original 401. The platform session-auth path is never
|
|
227
|
-
// refreshed here (its token is managed by the Vellum platform).
|
|
226
|
+
// Reactive auto-refresh on a 401 for the guardian (non-session) path.
|
|
227
|
+
// Ephemeral (`--token`) and access-only sessions have no stored refresh
|
|
228
|
+
// credential and just see the original 401; the platform session-auth path
|
|
229
|
+
// is never refreshed here (its token is managed by the Vellum platform).
|
|
228
230
|
if (response.status === 401 && !this.isSessionAuth) {
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
)
|
|
233
|
-
|
|
234
|
-
|
|
231
|
+
const stored = loadGuardianToken(this._assistantId);
|
|
232
|
+
|
|
233
|
+
// Another process may have already rotated and persisted a fresh access
|
|
234
|
+
// token (e.g. a concurrent `vellum events`). Adopt it and retry — this
|
|
235
|
+
// sends no refresh credential, just picks up the newer local token.
|
|
236
|
+
if (stored?.accessToken && stored.accessToken !== this.token) {
|
|
237
|
+
this.token = stored.accessToken;
|
|
235
238
|
return doFetch();
|
|
236
239
|
}
|
|
240
|
+
|
|
241
|
+
// Otherwise only disclose the long-lived refresh token when our access
|
|
242
|
+
// token is actually due for renewal. A 401 on a still-valid token (e.g. a
|
|
243
|
+
// forged 401 from an impostor endpoint trying to coax out the refresh
|
|
244
|
+
// credential) is surfaced as-is, not refreshed.
|
|
245
|
+
if (stored?.refreshToken && guardianTokenDueForRenewal(stored)) {
|
|
246
|
+
const refreshed = await refreshGuardianToken(
|
|
247
|
+
this.runtimeUrl,
|
|
248
|
+
this._assistantId,
|
|
249
|
+
);
|
|
250
|
+
if (refreshed?.accessToken) {
|
|
251
|
+
this.token = refreshed.accessToken;
|
|
252
|
+
return doFetch();
|
|
253
|
+
}
|
|
254
|
+
}
|
|
237
255
|
}
|
|
238
256
|
|
|
239
257
|
return response;
|
package/src/lib/docker.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
existsSync,
|
|
5
5
|
mkdirSync,
|
|
6
6
|
readFileSync,
|
|
7
|
+
readdirSync,
|
|
7
8
|
watch as fsWatch,
|
|
8
9
|
} from "fs";
|
|
9
10
|
import { arch, platform } from "os";
|
|
@@ -661,6 +662,7 @@ export async function startContainers(
|
|
|
661
662
|
bootstrapSecret?: string;
|
|
662
663
|
cesServiceToken?: string;
|
|
663
664
|
extraAssistantEnv?: Record<string, string>;
|
|
665
|
+
extraGatewayEnv?: Record<string, string>;
|
|
664
666
|
gatewayPort: number;
|
|
665
667
|
imageTags: Record<ServiceName, string>;
|
|
666
668
|
instanceName: string;
|
|
@@ -788,6 +790,56 @@ export async function captureImageRefs(
|
|
|
788
790
|
return hasAll ? (refs as Record<ServiceName, string>) : null;
|
|
789
791
|
}
|
|
790
792
|
|
|
793
|
+
/**
|
|
794
|
+
* Build the set of paths the hot-reload watcher should observe, scoped to
|
|
795
|
+
* each service's `src/` tree, `package.json` manifest, and `Dockerfile`.
|
|
796
|
+
*
|
|
797
|
+
* We deliberately avoid recursively watching whole service directories.
|
|
798
|
+
* Those contain `.claude/` command symlinks — which dangle in a fresh
|
|
799
|
+
* checkout because they point at the separately-cloned `claude-skills`
|
|
800
|
+
* repo — as well as `node_modules`. `fs.watch(dir, { recursive: true })`
|
|
801
|
+
* traverses those entries and emits an unhandled `error` event on a broken
|
|
802
|
+
* symlink, which crashes the CLI process. Source code only ever lives under
|
|
803
|
+
* `src/`, so watching that tree plus the two manifests that drive the image
|
|
804
|
+
* build (`package.json` and `Dockerfile`) preserves hot-reload without
|
|
805
|
+
* walking into symlinked or generated trees. The `Dockerfile` is watched as
|
|
806
|
+
* an individual file for the same reason — editing build steps should
|
|
807
|
+
* trigger a rebuild, but the file sits next to the symlinked trees we avoid.
|
|
808
|
+
*
|
|
809
|
+
* Returning a plain record keeps this trivially unit-testable — see
|
|
810
|
+
* `__tests__/docker.test.ts`.
|
|
811
|
+
*/
|
|
812
|
+
export function collectWatchTargets(repoRoot: string): {
|
|
813
|
+
dirs: string[];
|
|
814
|
+
files: string[];
|
|
815
|
+
} {
|
|
816
|
+
const packagesDir = join(repoRoot, "packages");
|
|
817
|
+
const packageRoots = existsSync(packagesDir)
|
|
818
|
+
? readdirSync(packagesDir, { withFileTypes: true })
|
|
819
|
+
.filter((entry) => entry.isDirectory())
|
|
820
|
+
.map((entry) => join(packagesDir, entry.name))
|
|
821
|
+
: [];
|
|
822
|
+
|
|
823
|
+
const serviceRoots = [
|
|
824
|
+
join(repoRoot, "assistant"),
|
|
825
|
+
join(repoRoot, "credential-executor"),
|
|
826
|
+
join(repoRoot, "gateway"),
|
|
827
|
+
...packageRoots,
|
|
828
|
+
];
|
|
829
|
+
|
|
830
|
+
const dirs: string[] = [];
|
|
831
|
+
const files: string[] = [];
|
|
832
|
+
for (const root of serviceRoots) {
|
|
833
|
+
const srcDir = join(root, "src");
|
|
834
|
+
if (existsSync(srcDir)) dirs.push(srcDir);
|
|
835
|
+
for (const name of ["package.json", "Dockerfile"]) {
|
|
836
|
+
const file = join(root, name);
|
|
837
|
+
if (existsSync(file)) files.push(file);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
return { dirs, files };
|
|
841
|
+
}
|
|
842
|
+
|
|
791
843
|
/**
|
|
792
844
|
* Determine which services are affected by a changed file path relative
|
|
793
845
|
* to the repository root.
|
|
@@ -821,9 +873,10 @@ function affectedServices(
|
|
|
821
873
|
}
|
|
822
874
|
|
|
823
875
|
/**
|
|
824
|
-
* Watch for
|
|
825
|
-
* and packages
|
|
826
|
-
*
|
|
876
|
+
* Watch for source changes across the assistant, gateway, credential-executor,
|
|
877
|
+
* and packages services — scoped to each service's `src/` tree, `package.json`,
|
|
878
|
+
* and `Dockerfile` (see `collectWatchTargets`). When changes are detected,
|
|
879
|
+
* rebuild the affected images and restart their containers.
|
|
827
880
|
*/
|
|
828
881
|
function startFileWatcher(opts: {
|
|
829
882
|
signingKey?: string;
|
|
@@ -837,12 +890,7 @@ function startFileWatcher(opts: {
|
|
|
837
890
|
}): () => void {
|
|
838
891
|
const { gatewayPort, imageTags, instanceName, repoRoot, res } = opts;
|
|
839
892
|
|
|
840
|
-
const watchDirs =
|
|
841
|
-
join(repoRoot, "assistant"),
|
|
842
|
-
join(repoRoot, "credential-executor"),
|
|
843
|
-
join(repoRoot, "gateway"),
|
|
844
|
-
join(repoRoot, "packages"),
|
|
845
|
-
];
|
|
893
|
+
const { dirs: watchDirs, files: watchFiles } = collectWatchTargets(repoRoot);
|
|
846
894
|
|
|
847
895
|
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
|
|
848
896
|
let pendingServices = new Set<ServiceName>();
|
|
@@ -919,37 +967,53 @@ function startFileWatcher(opts: {
|
|
|
919
967
|
|
|
920
968
|
const watchers: ReturnType<typeof fsWatch>[] = [];
|
|
921
969
|
|
|
970
|
+
function onChange(fullPath: string): void {
|
|
971
|
+
const services = affectedServices(fullPath, repoRoot);
|
|
972
|
+
if (services.size === 0) return;
|
|
973
|
+
|
|
974
|
+
for (const s of services) {
|
|
975
|
+
pendingServices.add(s);
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
979
|
+
debounceTimer = setTimeout(() => {
|
|
980
|
+
debounceTimer = null;
|
|
981
|
+
rebuildAndRestart();
|
|
982
|
+
}, 500);
|
|
983
|
+
}
|
|
984
|
+
|
|
922
985
|
for (const dir of watchDirs) {
|
|
923
|
-
if (!existsSync(dir)) continue;
|
|
924
986
|
const watcher = fsWatch(dir, { recursive: true }, (_event, filename) => {
|
|
925
987
|
if (!filename) return;
|
|
926
|
-
if (
|
|
927
|
-
filename.includes("node_modules") ||
|
|
928
|
-
filename.includes(".env") ||
|
|
929
|
-
filename.startsWith(".")
|
|
930
|
-
) {
|
|
988
|
+
if (filename.includes("node_modules") || filename.includes(".env")) {
|
|
931
989
|
return;
|
|
932
990
|
}
|
|
991
|
+
onChange(join(dir, filename));
|
|
992
|
+
});
|
|
993
|
+
// fs.watch surfaces transient errors (e.g. an unreadable entry) as an
|
|
994
|
+
// `error` event, which would otherwise crash the process. Log and keep
|
|
995
|
+
// the remaining watchers running.
|
|
996
|
+
watcher.on("error", (err) => {
|
|
997
|
+
console.error(
|
|
998
|
+
`⚠️ File watcher error for ${dir}: ${err instanceof Error ? err.message : err}`,
|
|
999
|
+
);
|
|
1000
|
+
});
|
|
1001
|
+
watchers.push(watcher);
|
|
1002
|
+
}
|
|
933
1003
|
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
if (debounceTimer) clearTimeout(debounceTimer);
|
|
943
|
-
debounceTimer = setTimeout(() => {
|
|
944
|
-
debounceTimer = null;
|
|
945
|
-
rebuildAndRestart();
|
|
946
|
-
}, 500);
|
|
1004
|
+
for (const file of watchFiles) {
|
|
1005
|
+
const watcher = fsWatch(file, () => onChange(file));
|
|
1006
|
+
watcher.on("error", (err) => {
|
|
1007
|
+
console.error(
|
|
1008
|
+
`⚠️ File watcher error for ${file}: ${err instanceof Error ? err.message : err}`,
|
|
1009
|
+
);
|
|
947
1010
|
});
|
|
948
1011
|
watchers.push(watcher);
|
|
949
1012
|
}
|
|
950
1013
|
|
|
951
1014
|
console.log("👀 Watching for file changes in:");
|
|
952
|
-
console.log("
|
|
1015
|
+
console.log(" <service>/src, <service>/package.json, <service>/Dockerfile");
|
|
1016
|
+
console.log(" for assistant/, gateway/, credential-executor/, packages/*");
|
|
953
1017
|
console.log("");
|
|
954
1018
|
|
|
955
1019
|
return () => {
|
|
@@ -979,6 +1043,7 @@ export async function hatchDocker(
|
|
|
979
1043
|
name: string | null,
|
|
980
1044
|
watch: boolean = false,
|
|
981
1045
|
configValues: Record<string, string> = {},
|
|
1046
|
+
flagEnvVars: Record<string, string> = {},
|
|
982
1047
|
options: HatchDockerOptions = {},
|
|
983
1048
|
): Promise<void> {
|
|
984
1049
|
resetLogFile("hatch.log");
|
|
@@ -1258,12 +1323,15 @@ export async function hatchDocker(
|
|
|
1258
1323
|
: ownSecret;
|
|
1259
1324
|
|
|
1260
1325
|
emitProgress(4, 6, "Starting containers...");
|
|
1326
|
+
const extraGatewayEnv =
|
|
1327
|
+
Object.keys(flagEnvVars).length > 0 ? flagEnvVars : undefined;
|
|
1261
1328
|
await startContainers(
|
|
1262
1329
|
{
|
|
1263
1330
|
signingKey,
|
|
1264
1331
|
bootstrapSecret,
|
|
1265
1332
|
cesServiceToken,
|
|
1266
1333
|
extraAssistantEnv,
|
|
1334
|
+
extraGatewayEnv,
|
|
1267
1335
|
gatewayPort,
|
|
1268
1336
|
imageTags,
|
|
1269
1337
|
instanceName,
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { describe, expect, test, spyOn } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { parseFeatureFlagArgs } from "./flag-args";
|
|
4
|
+
|
|
5
|
+
describe("parseFeatureFlagArgs", () => {
|
|
6
|
+
test("single flag produces env var and empty remaining", () => {
|
|
7
|
+
const result = parseFeatureFlagArgs(["--flag", "voice-mode=true"]);
|
|
8
|
+
expect(result).toEqual({
|
|
9
|
+
envVars: { VELLUM_FLAG_VOICE_MODE: "true" },
|
|
10
|
+
remaining: [],
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("multiple flags produce multiple env vars", () => {
|
|
15
|
+
const result = parseFeatureFlagArgs([
|
|
16
|
+
"--flag",
|
|
17
|
+
"a=1",
|
|
18
|
+
"--flag",
|
|
19
|
+
"b=0",
|
|
20
|
+
]);
|
|
21
|
+
expect(result).toEqual({
|
|
22
|
+
envVars: { VELLUM_FLAG_A: "1", VELLUM_FLAG_B: "0" },
|
|
23
|
+
remaining: [],
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("flags mixed with other args preserves remaining", () => {
|
|
28
|
+
const result = parseFeatureFlagArgs([
|
|
29
|
+
"--watch",
|
|
30
|
+
"--flag",
|
|
31
|
+
"x=y",
|
|
32
|
+
"--name",
|
|
33
|
+
"foo",
|
|
34
|
+
]);
|
|
35
|
+
expect(result).toEqual({
|
|
36
|
+
envVars: { VELLUM_FLAG_X: "y" },
|
|
37
|
+
remaining: ["--watch", "--name", "foo"],
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("exits with error when --flag has no following argument", () => {
|
|
42
|
+
const exitSpy = spyOn(process, "exit").mockImplementation(() => {
|
|
43
|
+
throw new Error("process.exit");
|
|
44
|
+
});
|
|
45
|
+
const errorSpy = spyOn(console, "error").mockImplementation(() => {});
|
|
46
|
+
|
|
47
|
+
expect(() => parseFeatureFlagArgs(["--flag"])).toThrow("process.exit");
|
|
48
|
+
expect(errorSpy).toHaveBeenCalledWith(
|
|
49
|
+
"Error: --flag requires a key=value argument",
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
exitSpy.mockRestore();
|
|
53
|
+
errorSpy.mockRestore();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("exits with error when value has no equals sign", () => {
|
|
57
|
+
const exitSpy = spyOn(process, "exit").mockImplementation(() => {
|
|
58
|
+
throw new Error("process.exit");
|
|
59
|
+
});
|
|
60
|
+
const errorSpy = spyOn(console, "error").mockImplementation(() => {});
|
|
61
|
+
|
|
62
|
+
expect(() => parseFeatureFlagArgs(["--flag", "noequals"])).toThrow(
|
|
63
|
+
"process.exit",
|
|
64
|
+
);
|
|
65
|
+
expect(errorSpy).toHaveBeenCalledWith(
|
|
66
|
+
'Error: --flag value must be in key=value format, got "noequals"',
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
exitSpy.mockRestore();
|
|
70
|
+
errorSpy.mockRestore();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("exits with error when key is not kebab-case", () => {
|
|
74
|
+
const exitSpy = spyOn(process, "exit").mockImplementation(() => {
|
|
75
|
+
throw new Error("process.exit");
|
|
76
|
+
});
|
|
77
|
+
const errorSpy = spyOn(console, "error").mockImplementation(() => {});
|
|
78
|
+
|
|
79
|
+
expect(() => parseFeatureFlagArgs(["--flag", "UPPER=true"])).toThrow(
|
|
80
|
+
"process.exit",
|
|
81
|
+
);
|
|
82
|
+
expect(errorSpy).toHaveBeenCalledWith(
|
|
83
|
+
'Error: invalid flag key "UPPER". Keys must be kebab-case (e.g. "voice-mode")',
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
exitSpy.mockRestore();
|
|
87
|
+
errorSpy.mockRestore();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** Only allow simple kebab-case keys (e.g. "voice-mode", "ces-tools"). */
|
|
2
|
+
const ALLOWED_KEY_RE = /^[a-z0-9][a-z0-9-]*$/;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Extract repeatable `--flag key=value` pairs from a CLI arg list.
|
|
6
|
+
*
|
|
7
|
+
* Each `--flag` consumes the next argument as `key=value`. Keys are validated
|
|
8
|
+
* against a kebab-case pattern, then converted to env var names of the form
|
|
9
|
+
* `VELLUM_FLAG_<UPPER_SNAKE>`. All `--flag` pairs are stripped from the
|
|
10
|
+
* returned `remaining` array so downstream parsers never see them.
|
|
11
|
+
*/
|
|
12
|
+
export function parseFeatureFlagArgs(args: string[]): {
|
|
13
|
+
envVars: Record<string, string>;
|
|
14
|
+
remaining: string[];
|
|
15
|
+
} {
|
|
16
|
+
const envVars: Record<string, string> = {};
|
|
17
|
+
const remaining: string[] = [];
|
|
18
|
+
|
|
19
|
+
let i = 0;
|
|
20
|
+
while (i < args.length) {
|
|
21
|
+
if (args[i] === "--flag") {
|
|
22
|
+
if (i + 1 >= args.length) {
|
|
23
|
+
console.error("Error: --flag requires a key=value argument");
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const pair = args[i + 1]!;
|
|
28
|
+
const eqIdx = pair.indexOf("=");
|
|
29
|
+
if (eqIdx === -1) {
|
|
30
|
+
console.error(
|
|
31
|
+
`Error: --flag value must be in key=value format, got "${pair}"`,
|
|
32
|
+
);
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const key = pair.slice(0, eqIdx);
|
|
37
|
+
const value = pair.slice(eqIdx + 1);
|
|
38
|
+
|
|
39
|
+
if (!ALLOWED_KEY_RE.test(key)) {
|
|
40
|
+
console.error(
|
|
41
|
+
`Error: invalid flag key "${key}". Keys must be kebab-case (e.g. "voice-mode")`,
|
|
42
|
+
);
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const envName = `VELLUM_FLAG_${key.toUpperCase().replace(/-/g, "_")}`;
|
|
47
|
+
envVars[envName] = value;
|
|
48
|
+
i += 2;
|
|
49
|
+
} else {
|
|
50
|
+
remaining.push(args[i]!);
|
|
51
|
+
i += 1;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return { envVars, remaining };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const ENV_FLAG_PREFIX = "VELLUM_FLAG_";
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Scan `process.env` for ambient `VELLUM_FLAG_*` entries.
|
|
62
|
+
* Returns them as-is (same `Record<string, string>` shape as
|
|
63
|
+
* `parseFeatureFlagArgs().envVars`) so callers can merge both
|
|
64
|
+
* sources with `--flag` args winning over ambient env vars.
|
|
65
|
+
*/
|
|
66
|
+
export function readAmbientFlagEnvVars(): Record<string, string> {
|
|
67
|
+
const vars: Record<string, string> = {};
|
|
68
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
69
|
+
if (key.startsWith(ENV_FLAG_PREFIX) && value !== undefined) {
|
|
70
|
+
vars[key] = value;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return vars;
|
|
74
|
+
}
|
|
@@ -254,10 +254,64 @@ function releaseRefreshLock(lockPath: string): void {
|
|
|
254
254
|
* process already rotated it while we waited, we return that fresh token
|
|
255
255
|
* instead of replaying our now-stale refresh token.
|
|
256
256
|
*/
|
|
257
|
+
/**
|
|
258
|
+
* The guardian refresh token is long-lived and replayable, so we only transmit
|
|
259
|
+
* it over a confidential channel: HTTPS, or a loopback host (local dev, or a
|
|
260
|
+
* same-host reverse proxy / tunnel agent). Refreshing against a non-loopback
|
|
261
|
+
* plaintext `http://` URL is refused — an on-path attacker could otherwise
|
|
262
|
+
* capture the refresh token and rotate it into fresh credentials.
|
|
263
|
+
*
|
|
264
|
+
* A user-chosen malicious `https://` destination is intentionally out of scope:
|
|
265
|
+
* HTTPS protects the channel, and the access token already goes wherever the
|
|
266
|
+
* configured URL points. This guard targets the plaintext-interception vector.
|
|
267
|
+
*/
|
|
268
|
+
function isLoopbackHostname(hostname: string): boolean {
|
|
269
|
+
const h = hostname.toLowerCase();
|
|
270
|
+
return (
|
|
271
|
+
h === "localhost" ||
|
|
272
|
+
h === "::1" ||
|
|
273
|
+
h === "[::1]" ||
|
|
274
|
+
h === "0:0:0:0:0:0:0:1" ||
|
|
275
|
+
/^127(?:\.\d{1,3}){3}$/.test(h)
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function isConfidentialRefreshUrl(gatewayUrl: string): boolean {
|
|
280
|
+
try {
|
|
281
|
+
const url = new URL(gatewayUrl);
|
|
282
|
+
return url.protocol === "https:" || isLoopbackHostname(url.hostname);
|
|
283
|
+
} catch {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* True when a stored guardian token has reached its renewal point — now is
|
|
290
|
+
* at/after `refreshAfter` (preferred) or `accessTokenExpiresAt`. Used to gate
|
|
291
|
+
* refresh so a forged/synthetic 401 on a still-valid token can't coax out the
|
|
292
|
+
* long-lived refresh credential. Unparseable timestamps → not due.
|
|
293
|
+
*/
|
|
294
|
+
export function guardianTokenDueForRenewal(token: GuardianTokenData): boolean {
|
|
295
|
+
const raw = token.refreshAfter || token.accessTokenExpiresAt;
|
|
296
|
+
const at = new Date(raw).getTime();
|
|
297
|
+
if (!Number.isFinite(at)) return false;
|
|
298
|
+
return at <= Date.now();
|
|
299
|
+
}
|
|
300
|
+
|
|
257
301
|
export async function refreshGuardianToken(
|
|
258
302
|
gatewayUrl: string,
|
|
259
303
|
assistantId: string,
|
|
260
304
|
): Promise<GuardianTokenData | null> {
|
|
305
|
+
// Never send the long-lived refresh token over a non-loopback plaintext URL.
|
|
306
|
+
if (!isConfidentialRefreshUrl(gatewayUrl)) {
|
|
307
|
+
console.warn(
|
|
308
|
+
`Refusing to refresh the guardian token over an insecure URL (${gatewayUrl}). ` +
|
|
309
|
+
"The refresh token is only sent over https or a loopback address — " +
|
|
310
|
+
"use an https URL (e.g. a tunnel) or connect over loopback.",
|
|
311
|
+
);
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
|
|
261
315
|
const before = loadGuardianToken(assistantId);
|
|
262
316
|
if (!before) return null;
|
|
263
317
|
|
package/src/lib/hatch-local.ts
CHANGED
|
@@ -164,6 +164,7 @@ export async function hatchLocal(
|
|
|
164
164
|
watch: boolean = false,
|
|
165
165
|
keepAlive: boolean = false,
|
|
166
166
|
configValues: Record<string, string> = {},
|
|
167
|
+
flagEnvVars: Record<string, string> = {},
|
|
167
168
|
options: HatchLocalOptions = {},
|
|
168
169
|
): Promise<HatchLocalResult> {
|
|
169
170
|
const reporter = options.reporter ?? consoleLifecycleReporter;
|
|
@@ -234,6 +235,7 @@ export async function hatchLocal(
|
|
|
234
235
|
runtimeUrl = await startGateway(watch, resources, {
|
|
235
236
|
signingKey,
|
|
236
237
|
bootstrapSecret,
|
|
238
|
+
envOverrides: flagEnvVars,
|
|
237
239
|
});
|
|
238
240
|
} catch (error) {
|
|
239
241
|
// Gateway failed — stop the daemon we just started so we don't leave
|