@bendyline/gezel-cli 1.2.10 → 1.2.12

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.
@@ -8,19 +8,24 @@ import {
8
8
  IMAGE_QUALITIES,
9
9
  ImageAspectRatioSchema,
10
10
  ImageQualitySchema,
11
+ SCORECARD,
11
12
  evaluateApiCompat as evaluateApiCompat2,
12
13
  getLogLevel as getLogLevel2,
13
14
  getLogOutput as getLogOutput2,
14
15
  setLogLevel as setLogLevel2,
15
16
  setLogOutput as setLogOutput2
16
17
  } from "@bendyline/gezel";
17
- import { streamChatEvents } from "@bendyline/gezel-client";
18
+ import {
19
+ streamChatEvents
20
+ } from "@bendyline/gezel-client";
18
21
  import {
19
22
  GezelClient as GezelClient2,
20
23
  createTrustingFetch as createTrustingFetch2,
21
24
  discoverOrSpawn as discoverOrSpawn2,
22
25
  isProcessAlive as isProcessAlive2,
26
+ listProcessSnapshots,
23
27
  readRuntime as readRuntime2,
28
+ requestDaemonHealth,
24
29
  resolveDaemonEntry as resolveDaemonEntry2,
25
30
  stopOwnedDaemon as stopOwnedDaemon2,
26
31
  stopProcessByPid
@@ -28,36 +33,8 @@ import {
28
33
  import { resolveOnDeviceProvider as resolveOnDeviceProvider3 } from "@bendyline/gezel/native";
29
34
  import { Command, Option } from "commander";
30
35
 
31
- // src/cli-errors.ts
32
- function formatCliFailure(err, env2 = process.env) {
33
- if (!(err instanceof Error)) return null;
34
- if (err.name === "CliError") return err.message;
35
- if (err.name !== "GezelApiError" || env2.GEZEL_DEBUG === "1") return null;
36
- const { status, details } = err;
37
- const request = /\bon ([A-Z]+ \S+)/.exec(err.message)?.[1];
38
- const serviceText = detailText(details);
39
- if (status === 404) {
40
- const what = serviceText && serviceText.toLowerCase() !== "not found" ? serviceText : void 0;
41
- return `error: not found${what ? `: ${what}` : request ? ` (${request})` : ""}`;
42
- }
43
- const lines = [`error: ${serviceText ?? err.message}`];
44
- if (serviceText && request) lines.push(`(Gezel service answered ${status ?? "?"} to ${request})`);
45
- lines.push("Set GEZEL_DEBUG=1 to see the full error.");
46
- return lines.join("\n");
47
- }
48
- function isNotFound(err) {
49
- return err instanceof Error && err.name === "GezelApiError" && err.status === 404;
50
- }
51
- function detailText(details) {
52
- if (typeof details === "string" && details.trim()) return details.trim();
53
- if (details && typeof details === "object") {
54
- for (const key of ["message", "error"]) {
55
- const value = details[key];
56
- if (typeof value === "string" && value.trim()) return value.trim();
57
- }
58
- }
59
- return void 0;
60
- }
36
+ // src/account-command.ts
37
+ import { GezelApiError as GezelApiError2, apiErrorMessage } from "@bendyline/gezel-client";
61
38
 
62
39
  // src/connection.ts
63
40
  import { createHash, randomUUID } from "crypto";
@@ -94,6 +71,7 @@ import {
94
71
  readSystemServiceEndpoint,
95
72
  resolveDaemonEntry,
96
73
  stopOwnedDaemon,
74
+ systemServiceRuntimeIsStale,
97
75
  systemSharedAssetsDir
98
76
  } from "@bendyline/gezel-client/node";
99
77
  import { gezelHome, gezelPaths, nativePayloadVerificationPath } from "@bendyline/gezel/paths";
@@ -205,6 +183,17 @@ async function connectOwned(globals, options = {}) {
205
183
  );
206
184
  return connected.client;
207
185
  }
186
+ var NO_RUNNING_SERVICE_MESSAGE = "No Gezel service is running, and --no-start keeps this command from starting one. Run `gezel start` first, or leave out --no-start.";
187
+ async function connectRunning(globals, deps = {}) {
188
+ applyHome(globals);
189
+ const preferred = await connectPreferredService(globals);
190
+ if (preferred) return preferred.client;
191
+ const runtime = await (deps.readRuntime ?? readRuntime)();
192
+ if (!runtime || !(deps.isAlive ?? isProcessAlive)(runtime.pid)) {
193
+ throw new CliError(NO_RUNNING_SERVICE_MESSAGE);
194
+ }
195
+ return (deps.connectOwned ?? connectOwned)(globals, { announceSpawn: false });
196
+ }
208
197
  async function connectForTui(globals) {
209
198
  applyHome(globals);
210
199
  const preferred = await connectPreferredService(globals);
@@ -443,7 +432,16 @@ async function describeMachineEngineBroker(deps = {}) {
443
432
  ...health.startedAt !== void 0 ? { startedAt: health.startedAt } : {}
444
433
  };
445
434
  } catch {
446
- return { present: true, healthy: false, baseUrl: endpoint.baseUrl, port: endpoint.port };
435
+ const stale = await (deps.isRuntimeStale ?? systemServiceRuntimeIsStale)(endpoint.home).catch(
436
+ () => false
437
+ );
438
+ return {
439
+ present: true,
440
+ healthy: false,
441
+ baseUrl: endpoint.baseUrl,
442
+ port: endpoint.port,
443
+ ...stale ? { staleRuntimeDir: join(endpoint.home, "runtime") } : {}
444
+ };
447
445
  } finally {
448
446
  clearTimeout(timeout);
449
447
  }
@@ -767,6 +765,679 @@ async function resolveTuiProject(client, globals) {
767
765
  return resolveCommandProject(client, globals);
768
766
  }
769
767
 
768
+ // src/account-command.ts
769
+ async function readNewPassword(options, input = process.stdin, write = (text) => process.stderr.write(text)) {
770
+ if (options.passwordStdin) {
771
+ if (input.isTTY) throw new CliError("--password-stdin needs piped input.");
772
+ const chunks = [];
773
+ for await (const chunk of input) {
774
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
775
+ if (chunks.reduce((n, c) => n + c.length, 0) > 4096) {
776
+ throw new CliError("The password is too long.");
777
+ }
778
+ }
779
+ return Buffer.concat(chunks).toString("utf8").replace(/\r?\n$/, "");
780
+ }
781
+ if (!input.isTTY || !input.setRawMode) {
782
+ throw new CliError("No terminal to type a password into. Pipe it in with --password-stdin.");
783
+ }
784
+ const first = await promptHidden("New password: ", input, write);
785
+ const second = await promptHidden("Type it again: ", input, write);
786
+ if (first !== second) throw new CliError("The two passwords did not match.");
787
+ return first;
788
+ }
789
+ function promptHidden(prompt, input, write) {
790
+ write(prompt);
791
+ return new Promise((resolve7, reject) => {
792
+ let value = "";
793
+ const done = (error) => {
794
+ input.off?.("data", onData);
795
+ input.setRawMode?.(false);
796
+ input.pause?.();
797
+ write("\n");
798
+ if (error) reject(error);
799
+ else resolve7(value);
800
+ };
801
+ const onData = (chunk) => {
802
+ for (const ch of chunk.toString("utf8")) {
803
+ if (ch === "\r" || ch === "\n") return done();
804
+ if (ch === "") return done(new CliError("Canceled."));
805
+ if (ch === "\x7F" || ch === "\b") value = value.slice(0, -1);
806
+ else if (ch >= " ") value += ch;
807
+ }
808
+ };
809
+ input.setRawMode?.(true);
810
+ input.resume?.();
811
+ input.on?.("data", onData);
812
+ });
813
+ }
814
+ async function findAccount(client, name) {
815
+ const key = name.trim().toLowerCase();
816
+ const { accounts } = await client.listAccounts();
817
+ const match = accounts.find((a) => a.name.toLowerCase() === key || a.id === name);
818
+ if (!match) throw new CliError(`There is no account called "${name}". See "gezel account list".`);
819
+ return match;
820
+ }
821
+ async function plainly(run) {
822
+ try {
823
+ return await run();
824
+ } catch (error) {
825
+ if (error instanceof GezelApiError2 && error.status < 500 && error.status > 0) {
826
+ const details = error.details;
827
+ if (error.status === 404 && details?.error === "accounts_unavailable") {
828
+ throw new CliError("This Gezel service has no accounts.");
829
+ }
830
+ throw new CliError(apiErrorMessage(error));
831
+ }
832
+ throw error;
833
+ }
834
+ }
835
+ function describeAccount(account) {
836
+ const state = account.disabledAt ? "disabled" : account.role;
837
+ const seen = account.lastSignInAt ? `last signed in ${account.lastSignInAt.slice(0, 10)}` : "never signed in";
838
+ return `${account.name} ${state} ${seen}`;
839
+ }
840
+ function describeSession(session) {
841
+ return [
842
+ session.label ?? "unknown",
843
+ session.address ?? "",
844
+ `last seen ${session.lastSeenAt.slice(0, 16).replace("T", " ")}`
845
+ ].filter(Boolean).join(" ");
846
+ }
847
+ function registerAccountCommands(program2, connect, output = console.log, input = process.stdin) {
848
+ const account = program2.command("account").description("Manage the accounts that sign in to this Gezel from a browser");
849
+ account.command("list").description("List accounts").option("--json", "Output as JSON").action(async (options) => {
850
+ const { accounts } = await plainly(async () => (await connect()).listAccounts());
851
+ if (options.json) return output(JSON.stringify({ accounts }));
852
+ output(accounts.length ? accounts.map(describeAccount).join("\n") : "No accounts yet.");
853
+ });
854
+ account.command("create <name>").description("Add an account. Everyone you add can do everything you can.").option("--password-stdin", "Read the password from piped stdin").option("--json", "Output as JSON").action(async (name, options) => {
855
+ const client = await connect();
856
+ const password = await readNewPassword(options, input);
857
+ const created = await plainly(() => client.createAccount(name, password));
858
+ output(options.json ? JSON.stringify(created) : `${created.account.name} can now sign in.`);
859
+ });
860
+ account.command("reset-password <name>").description("Set someone's password and sign them out everywhere").option("--password-stdin", "Read the password from piped stdin").action(async (name, options) => {
861
+ const client = await connect();
862
+ const target = await findAccount(client, name);
863
+ const password = await readNewPassword(options, input);
864
+ await plainly(() => client.setAccountPassword(target.id, password));
865
+ output(`${target.name}'s password was changed. They are signed out everywhere.`);
866
+ });
867
+ for (const [verb, disabled, done] of [
868
+ ["disable", true, "can no longer sign in"],
869
+ ["enable", false, "can sign in again"]
870
+ ]) {
871
+ account.command(`${verb} <name>`).description(disabled ? "Stop an account signing in" : "Let a disabled account sign in again").action(async (name) => {
872
+ const client = await connect();
873
+ const target = await findAccount(client, name);
874
+ await plainly(() => client.updateAccount(target.id, { disabled }));
875
+ output(`${target.name} ${done}.`);
876
+ });
877
+ }
878
+ account.command("remove <name>").description("Remove an account and sign it out everywhere").option("--yes", "Do not ask for confirmation").action(async (name, options) => {
879
+ if (!options.yes)
880
+ throw new CliError("Removing an account cannot be undone. Add --yes to go ahead.");
881
+ const client = await connect();
882
+ const target = await findAccount(client, name);
883
+ await plainly(() => client.removeAccount(target.id));
884
+ output(`${target.name} was removed.`);
885
+ });
886
+ account.command("sessions <name>").description("List where an account is signed in").option("--json", "Output as JSON").action(async (name, options) => {
887
+ const client = await connect();
888
+ const target = await findAccount(client, name);
889
+ const { sessions } = await plainly(() => client.listAccountSessions(target.id));
890
+ if (options.json) return output(JSON.stringify({ sessions }));
891
+ output(
892
+ sessions.length ? sessions.map(describeSession).join("\n") : `${target.name} is not signed in anywhere.`
893
+ );
894
+ });
895
+ account.command("sign-out-all <name>").description("Sign an account out of every browser").action(async (name) => {
896
+ const client = await connect();
897
+ const target = await findAccount(client, name);
898
+ const { signedOut } = await plainly(() => client.signOutAccountEverywhere(target.id));
899
+ output(
900
+ `Signed ${target.name} out of ${signedOut} ${signedOut === 1 ? "browser" : "browsers"}.`
901
+ );
902
+ });
903
+ account.command("setup-link").description("Print a new link to create the first account (only while there is none)").action(async () => {
904
+ const { code, url } = await plainly(async () => (await connect()).createSetupLink());
905
+ if (!code)
906
+ throw new CliError('This Gezel already has an account. Use "gezel account create".');
907
+ output(url ? `Open this link to create the first account:
908
+ ${url}` : `Setup code: ${code}`);
909
+ });
910
+ }
911
+ function registerIntegrationCommands(program2, connect, output = console.log) {
912
+ const token = program2.command("integrations").description("Connect other apps to this Gezel").command("token").description("Make or revoke an app key");
913
+ token.command("create <appId>").description("Make a key for an app (OpenAI-compatible access by default)").option("--name <name>", "The name Integrations shows for the app").option(
914
+ "--scope <scope...>",
915
+ "What the key may do: openai, knowledge, images, product or cli (default: openai)"
916
+ ).option("--json", "Output as JSON").action(async (appId, options) => {
917
+ const client = await connect();
918
+ const created = await plainly(
919
+ () => client.createAppToken(appId, {
920
+ ...options.name ? { appName: options.name } : {},
921
+ ...options.scope?.length ? { scopes: options.scope } : {}
922
+ })
923
+ );
924
+ if (options.json) return output(JSON.stringify(created));
925
+ output(
926
+ [
927
+ `Key for ${created.appId} (${created.scopes.join(", ")}):`,
928
+ ` ${created.token}`,
929
+ "",
930
+ "Use it as the API key, with the base URL ending in /v1.",
931
+ 'It is shown once. Revoke it with "gezel integrations token revoke".'
932
+ ].join("\n")
933
+ );
934
+ });
935
+ token.command("revoke <appId>").description("Revoke an app's key").action(async (appId) => {
936
+ const { revoked } = await plainly(async () => (await connect()).revokeAppToken(appId));
937
+ output(revoked ? `${appId} can no longer connect.` : `${appId} had no key.`);
938
+ });
939
+ }
940
+
941
+ // src/bench-command.ts
942
+ import { execFile } from "child_process";
943
+ import { readFileSync } from "fs";
944
+ import { readFile as readFile2, writeFile as writeFile2 } from "fs/promises";
945
+ import { cpus } from "os";
946
+ import { join as join2 } from "path";
947
+ import { promisify } from "util";
948
+ import { ScorecardDatasetSchema } from "@bendyline/gezel";
949
+ import { gildePackageRoot } from "@bendyline/gezel-catalog";
950
+ import {
951
+ EVAL_SUBSET_NOTE,
952
+ SCORECARD_SUITE_IDS,
953
+ SCORECARD_SUITE_LABELS,
954
+ buildEvalMatrix,
955
+ describeEvalHost,
956
+ describeEvalRun,
957
+ evalDeviceFromHost,
958
+ evalEngineBuilds,
959
+ evalRunId,
960
+ evalRunProvenance,
961
+ evalShard,
962
+ evalSuiteResult,
963
+ isSuiteSubset,
964
+ medianDecodeTokensPerSec,
965
+ smokeSuiteIdFor
966
+ } from "@bendyline/gezel/eval";
967
+ var BENCH_CONFIRM_ABOVE_MS = 60 * 6e4;
968
+ var BENCH_SUBMIT_REPO = "bendyline/gezel";
969
+ function formatDuration(ms) {
970
+ if (ms < 6e4) return `${Math.max(1, Math.round(ms / 1e3))}s`;
971
+ const minutes = Math.round(ms / 6e4);
972
+ if (minutes < 60) return `${minutes}m`;
973
+ const hours = Math.floor(minutes / 60);
974
+ const rest = minutes % 60;
975
+ return rest ? `${hours}h ${rest}m` : `${hours}h`;
976
+ }
977
+ function resolveBenchSuites(catalog, opts) {
978
+ const wanted = !opts.suite || opts.suite === "core" || opts.suite === "default" ? ["core"] : opts.suite === "all" ? (
979
+ // Every scorecard suite this daemon's catalog carries; an older
980
+ // harness without one of them still runs the rest.
981
+ SCORECARD_SUITE_IDS.filter((id) => catalog.suites.some((s) => s.id === id))
982
+ ) : opts.suite.split(",").map((s) => s.trim()).filter(Boolean);
983
+ const suites = [];
984
+ for (const id of wanted) {
985
+ const base = catalog.suites.find((s) => s.id === id);
986
+ if (!base) {
987
+ throw new CliError(
988
+ `Unknown suite "${id}". Choose one of: ${SCORECARD_SUITE_IDS.join(", ")}, or all. \`gezel bench list\` shows every suite.`
989
+ );
990
+ }
991
+ if (opts.smoke) {
992
+ const smokeId = smokeSuiteIdFor(id, catalog);
993
+ const smoke = smokeId ? catalog.suites.find((s) => s.id === smokeId) : void 0;
994
+ if (!smoke) {
995
+ throw new CliError(`The ${id} suite has no smoke variant in this catalog.`);
996
+ }
997
+ suites.push(smoke);
998
+ } else {
999
+ suites.push(base);
1000
+ }
1001
+ }
1002
+ return suites;
1003
+ }
1004
+ function resolveBenchTarget(catalog, targets, opts) {
1005
+ const available = targets.filter((t) => t.available);
1006
+ if (opts.model) {
1007
+ const matches = available.filter(
1008
+ (t) => t.modelId === opts.model && (!opts.provider || t.provider === opts.provider)
1009
+ );
1010
+ const preferred = matches.find((t) => t.provider === catalog.defaultProvider) ?? matches[0];
1011
+ if (preferred) return preferred;
1012
+ const unavailable = targets.find(
1013
+ (t) => t.modelId === opts.model && (!opts.provider || t.provider === opts.provider)
1014
+ );
1015
+ throw new CliError(
1016
+ unavailable?.unavailableReason ? `${opts.model} can't run here: ${unavailable.unavailableReason}` : `No model "${opts.model}"${opts.provider ? ` on ${opts.provider}` : ""} is available to evaluate. \`gezel model list\` shows what is installed.`
1017
+ );
1018
+ }
1019
+ const byProvider = opts.provider ? available.filter((t) => t.provider === opts.provider) : available;
1020
+ const chosen = byProvider.find((t) => t.isDefault && t.provider === catalog.defaultProvider) ?? byProvider.find((t) => t.isDefault) ?? byProvider.find((t) => t.provider === catalog.defaultProvider) ?? byProvider[0];
1021
+ if (!chosen) {
1022
+ throw new CliError(
1023
+ "No model is available to evaluate. Download one with `gezel model pull <id>` or sign in to a cloud provider, then try again."
1024
+ );
1025
+ }
1026
+ return chosen;
1027
+ }
1028
+ function planBench(catalog, targets, opts) {
1029
+ const count = opts.count ?? 1;
1030
+ if (!Number.isInteger(count) || count < 1 || count > 10) {
1031
+ throw new CliError(
1032
+ "--count must be a whole number from 1 to 10 (3 is what it takes to quote a rate)."
1033
+ );
1034
+ }
1035
+ const suites = resolveBenchSuites(catalog, opts);
1036
+ const target = resolveBenchTarget(catalog, targets, opts);
1037
+ const worstCaseMs = suites.reduce((sum, s) => sum + s.authoredCeilingMs * count, 0);
1038
+ return { suites, target, count, worstCaseMs, smoke: Boolean(opts.smoke) };
1039
+ }
1040
+ function describePlan(plan, catalog) {
1041
+ const lines = [
1042
+ `Model: ${plan.target.label} (${plan.target.modelId} on ${plan.target.provider})`,
1043
+ `Trials per task: ${plan.count}${plan.count < catalog.minTrialsForRate ? ` \u2014 results show as counts; ${catalog.minTrialsForRate} trials is what it takes to quote a rate` : ""}`
1044
+ ];
1045
+ for (const suite of plan.suites) {
1046
+ lines.push(
1047
+ `Suite: ${suite.id} \u2014 ${suite.scenarioIds.length} tasks, at most ${formatDuration(
1048
+ suite.authoredCeilingMs * plan.count
1049
+ )}`
1050
+ );
1051
+ if (plan.smoke) lines.push(` ${suite.description}`);
1052
+ }
1053
+ lines.push(
1054
+ `At most ${formatDuration(plan.worstCaseMs)} in all. The run keeps going if this terminal closes.`
1055
+ );
1056
+ return lines;
1057
+ }
1058
+ function describeJobOutcome(job) {
1059
+ switch (job.status) {
1060
+ case "completed":
1061
+ return null;
1062
+ case "failed":
1063
+ return `The run could not start: ${job.error ?? "the harness reported a setup error"}.`;
1064
+ case "cancelled":
1065
+ return "The run was stopped before it finished.";
1066
+ case "interrupted":
1067
+ return "The Gezel service stopped while the run was in progress; the trials it finished are counted.";
1068
+ default:
1069
+ return `The run ended in state "${job.status}".`;
1070
+ }
1071
+ }
1072
+ function renderSuiteReport(suite, target, trials, minTrialsForRate) {
1073
+ const matrix = buildEvalMatrix(trials, { scenarioOrder: suite.scenarioIds });
1074
+ const column = matrix.columns.find(
1075
+ (c) => c.modelId === target.modelId && (c.provider ?? target.provider) === target.provider
1076
+ );
1077
+ const width = Math.min(40, Math.max(12, ...suite.scenarioIds.map((id) => id.length)) + 2);
1078
+ const lines = [];
1079
+ for (const scenarioId of suite.scenarioIds) {
1080
+ const cell = column ? matrix.cell(scenarioId, column.key) : void 0;
1081
+ const claim = cell ? cell.claim : "not run";
1082
+ const extra = cell?.nonModelFailures ? ` (${cell.nonModelFailures} not counted: not the model's fault)` : "";
1083
+ lines.push(` ${scenarioId.padEnd(width)}${claim}${extra}`);
1084
+ }
1085
+ const total = column ? column.claim : "not measured";
1086
+ const label = SCORECARD_SUITE_LABELS[suite.id] ?? suite.id;
1087
+ lines.push(` ${"suite".padEnd(width)}${total}`);
1088
+ if (column && column.passRate === null && column.attributableTrials > 0) {
1089
+ lines.push(
1090
+ ` A count, not a rate: ${minTrialsForRate} trials per task is the floor for quoting a percentage.`
1091
+ );
1092
+ }
1093
+ const decode = medianDecodeTokensPerSec(trials);
1094
+ lines.push(
1095
+ ` Decode: ${decode ? `${decode.toFixed(1)} tok/s (median over finished tasks)` : "not recorded"}. Prefill: not recorded by the daemon's trial index.`
1096
+ );
1097
+ return [`${label} (${suite.id})`, ...lines];
1098
+ }
1099
+ function createJobFollower(suite, count) {
1100
+ let lastTrial = null;
1101
+ let lastCompleted = -1;
1102
+ let announcedWaiting = false;
1103
+ let announcedPreflight = false;
1104
+ return {
1105
+ update(job) {
1106
+ const lines = [];
1107
+ const target = job.targets[0];
1108
+ if (job.status === "waiting-for-device" && !announcedWaiting) {
1109
+ announcedWaiting = true;
1110
+ lines.push(
1111
+ `Waiting: another evaluation${job.waitingOn ? ` (${job.waitingOn})` : ""} holds this machine's engines.`
1112
+ );
1113
+ }
1114
+ if (!target) return lines;
1115
+ if (target.preflight && !announcedPreflight) {
1116
+ announcedPreflight = true;
1117
+ const p = target.preflight;
1118
+ lines.push(
1119
+ p.admitted === null ? `Fitness check skipped${p.skippedReason ? `: ${p.skippedReason}` : ""}.` : p.admitted ? `Fitness check passed${p.decodeTokensPerSec ? ` \xB7 ${p.decodeTokensPerSec.toFixed(1)} tok/s` : ""}.` : `Fitness check failed${p.skippedReason ? `: ${p.skippedReason}` : ""}.`
1120
+ );
1121
+ }
1122
+ const planned = target.plannedTrials ?? suite.scenarioIds.length * count;
1123
+ if (target.completedTrials !== lastCompleted && lastCompleted >= 0) {
1124
+ lines.push(` ${target.completedTrials}/${planned} done, ${target.passedTrials} passed`);
1125
+ }
1126
+ lastCompleted = target.completedTrials;
1127
+ const current = target.currentTrial;
1128
+ if (current && current.trialId !== lastTrial) {
1129
+ lastTrial = current.trialId;
1130
+ lines.push(` ${current.scenarioId} (${current.trialIndex}/${planned}) running\u2026`);
1131
+ }
1132
+ return lines;
1133
+ }
1134
+ };
1135
+ }
1136
+ function finishedJob(job) {
1137
+ return job.status === "completed" || job.status === "failed" || job.status === "cancelled" || job.status === "interrupted";
1138
+ }
1139
+ async function followJob(client, job, suite, count, io, verbose = false) {
1140
+ const follower = createJobFollower(suite, count);
1141
+ let latest = job;
1142
+ for (const line of follower.update(job)) io.note(line);
1143
+ await client.streamEvalJob(job.id, (event) => {
1144
+ if (event.type === "log") {
1145
+ if (verbose) io.note(` ${event.line}`);
1146
+ return;
1147
+ }
1148
+ latest = event.job;
1149
+ for (const line of follower.update(event.job)) io.note(line);
1150
+ });
1151
+ if (!finishedJob(latest)) latest = await client.getEvalJob(job.id);
1152
+ return latest;
1153
+ }
1154
+ function defaultGildeVersion() {
1155
+ try {
1156
+ const pkg = JSON.parse(readFileSync(join2(gildePackageRoot(), "package.json"), "utf8"));
1157
+ return pkg.version ?? "unknown";
1158
+ } catch {
1159
+ return "unknown";
1160
+ }
1161
+ }
1162
+ function defaultCpuModel() {
1163
+ return cpus()[0]?.model?.trim() || void 0;
1164
+ }
1165
+ async function runBench(client, opts, io) {
1166
+ const [catalog, targetsResponse, host] = await Promise.all([
1167
+ client.getEvalCatalog(),
1168
+ client.listEvalTargets(),
1169
+ client.getSystemDiagnostics()
1170
+ ]);
1171
+ const plan = planBench(catalog, targetsResponse.targets, opts);
1172
+ const cpuModel = io.cpuModel ? io.cpuModel() : void 0;
1173
+ io.note(describeEvalHost(host, cpuModel));
1174
+ for (const line of describePlan(plan, catalog)) io.note(line);
1175
+ if (plan.worstCaseMs > BENCH_CONFIRM_ABOVE_MS && !opts.yes) {
1176
+ if (!io.confirm) {
1177
+ throw new CliError(
1178
+ `This run could take ${formatDuration(plan.worstCaseMs)}. Add --yes to start it without a prompt.`
1179
+ );
1180
+ }
1181
+ const go = await io.confirm(`Start a run of up to ${formatDuration(plan.worstCaseMs)}? [y/N] `);
1182
+ if (!go) throw new CliError("Not started.");
1183
+ }
1184
+ const startedAt = (io.now?.() ?? /* @__PURE__ */ new Date()).toISOString();
1185
+ const device = evalDeviceFromHost(host, cpuModel);
1186
+ const scenariosBySuite = {};
1187
+ const jobs = [];
1188
+ const results = [];
1189
+ const runId = evalRunId(startedAt, device, plan.target.modelId);
1190
+ const gildeVersion = (io.gildeVersion ?? defaultGildeVersion)();
1191
+ const run = evalRunProvenance({
1192
+ id: runId,
1193
+ startedAt,
1194
+ device,
1195
+ gezelVersion: host.version,
1196
+ gildeVersion,
1197
+ count: plan.count,
1198
+ engineBuilds: evalEngineBuilds(host),
1199
+ scenariosBySuite: Object.fromEntries(plan.suites.map((s) => [s.id, s.scenarioIds])),
1200
+ subset: false,
1201
+ ...opts.note ? { note: opts.note } : {}
1202
+ });
1203
+ for (const suite of plan.suites) {
1204
+ const spec = {
1205
+ suiteId: suite.id,
1206
+ count: plan.count,
1207
+ ...plan.count > 1 ? { countStrict: true } : {},
1208
+ targets: [{ provider: plan.target.provider, modelId: plan.target.modelId }],
1209
+ note: `gezel bench ${runId}`
1210
+ };
1211
+ io.note("");
1212
+ io.note(`Starting ${suite.id}\u2026`);
1213
+ const created = await client.createEvalJob(spec);
1214
+ const job = await followJob(client, created, suite, plan.count, io, opts.verbose);
1215
+ jobs.push(job);
1216
+ scenariosBySuite[suite.id] = suite.scenarioIds;
1217
+ const outcome = describeJobOutcome(job);
1218
+ if (outcome) io.note(outcome);
1219
+ const { trials } = await client.listEvalTrials({ jobId: job.id, limit: 2e3 });
1220
+ results.push(evalSuiteResult({ run, suite, target: plan.target, trials }));
1221
+ io.out("");
1222
+ for (const line of renderSuiteReport(suite, plan.target, trials, catalog.minTrialsForRate)) {
1223
+ io.out(line);
1224
+ }
1225
+ if (plan.smoke) io.out(` ${suite.description}`);
1226
+ }
1227
+ io.out("");
1228
+ io.out(describeEvalRun(run, [plan.target.provider]));
1229
+ io.out(
1230
+ `Results are kept in ${targetsResponse.environment.runsDir}; Settings \u2192 Benchmarks shows the same runs.`
1231
+ );
1232
+ const shard = evalShard(run, results);
1233
+ if (opts.json) {
1234
+ await writeFile2(opts.json, `${JSON.stringify(shard, null, 2)}
1235
+ `, "utf8");
1236
+ io.out(`Wrote ${opts.json}. Share it with \`gezel bench submit ${opts.json}\`.`);
1237
+ }
1238
+ return { run, results, jobs, shard };
1239
+ }
1240
+ function renderSuiteList(catalog) {
1241
+ const width = Math.max(...catalog.suites.map((s) => s.id.length)) + 2;
1242
+ const lines = [
1243
+ "Suites (`gezel bench --suite <id>`); times are the authored ceilings at one trial per task:"
1244
+ ];
1245
+ const scorecard = catalog.suites.filter(
1246
+ (s) => SCORECARD_SUITE_IDS.includes(s.id)
1247
+ );
1248
+ const rest = catalog.suites.filter((s) => !scorecard.includes(s));
1249
+ for (const suite of [...scorecard, ...rest]) {
1250
+ const label = SCORECARD_SUITE_LABELS[suite.id];
1251
+ lines.push(
1252
+ ` ${suite.id.padEnd(width)}${String(suite.scenarioIds.length).padStart(2)} tasks ${formatDuration(
1253
+ suite.authoredCeilingMs
1254
+ ).padStart(7)} ${label ?? suite.description}`
1255
+ );
1256
+ }
1257
+ lines.push(
1258
+ "",
1259
+ `The published scorecard runs ${SCORECARD_SUITE_IDS.join(", ")}. Add --smoke for a suite's pulse check; it is not a suite score.`
1260
+ );
1261
+ return lines;
1262
+ }
1263
+ async function renderResults(client, catalog, limit) {
1264
+ const { jobs } = await client.listEvalJobs();
1265
+ const finished = jobs.filter(finishedJob).slice(0, limit);
1266
+ if (finished.length === 0)
1267
+ return ["No finished runs on this machine yet. `gezel bench` starts one."];
1268
+ const lines = [];
1269
+ for (const job of finished) {
1270
+ const suite = job.spec.suiteId ? catalog.suites.find((s) => s.id === job.spec.suiteId) : void 0;
1271
+ const target = job.targets[0];
1272
+ const { trials } = await client.listEvalTrials({ jobId: job.id, limit: 2e3 });
1273
+ const order = suite?.scenarioIds ?? [...new Set(trials.map((t) => t.scenarioId))];
1274
+ const matrix = buildEvalMatrix(trials, { scenarioOrder: order });
1275
+ const column = target ? matrix.columns.find(
1276
+ (c) => c.modelId === target.modelId && (c.provider ?? target.provider) === target.provider
1277
+ ) : matrix.columns[0];
1278
+ const subset = suite && job.spec.scenarioIds ? isSuiteSubset(suite, job.spec.scenarioIds) : false;
1279
+ const what = suite ? `${suite.id}${subset ? " (subset)" : ""}` : `${job.spec.scenarioIds?.length ?? 0} tasks`;
1280
+ lines.push(
1281
+ `${(job.finishedAt ?? job.createdAt).slice(0, 16).replace("T", " ")} ${(target?.modelId ?? "?").padEnd(24)} ${what.padEnd(22)} ${column?.claim ?? "not measured"}${job.status !== "completed" ? ` [${job.status}]` : ""}`
1282
+ );
1283
+ if (subset) lines.push(` ${EVAL_SUBSET_NOTE}`);
1284
+ }
1285
+ return lines;
1286
+ }
1287
+ function describeSubmission(shard) {
1288
+ const run = shard.runs[0];
1289
+ const models = [...new Set(shard.results.map((r) => `${r.modelId} (${r.engine})`))];
1290
+ const suites = run.suites.join(", ");
1291
+ const title = `Bench result: ${models.join(", ")} on ${run.provenance.device.label}`;
1292
+ const body = [
1293
+ "## Community bench result",
1294
+ "",
1295
+ `- Run: \`${run.id}\``,
1296
+ `- ${describeEvalRun(run, [...new Set(shard.results.map((r) => r.engine))])}`,
1297
+ `- Suites: ${suites}`,
1298
+ ...run.note ? [`- Note: ${run.note}`] : [],
1299
+ "",
1300
+ "Results (count below three trials per task, rate otherwise):",
1301
+ ...shard.results.map((r) => {
1302
+ const matrixLike = r.cells.reduce(
1303
+ (acc, c) => {
1304
+ acc.trials += Math.max(0, c.trials - c.nonModelFailures);
1305
+ acc.successes += c.successes;
1306
+ return acc;
1307
+ },
1308
+ { trials: 0, successes: 0 }
1309
+ );
1310
+ return `- ${r.suiteId}: ${matrixLike.successes}/${matrixLike.trials}`;
1311
+ }),
1312
+ "",
1313
+ "The JSON shard produced by `gezel bench --json` is attached below. It is a `ScorecardDataset` and ingests with the same code as a published round; community rounds are shown separately from Bendyline rounds.",
1314
+ "",
1315
+ "<details><summary>shard.json</summary>",
1316
+ "",
1317
+ "```json",
1318
+ JSON.stringify(shard, null, 2),
1319
+ "```",
1320
+ "",
1321
+ "</details>"
1322
+ ].join("\n");
1323
+ return { title, body };
1324
+ }
1325
+ var execFileAsync = promisify(execFile);
1326
+ async function submitBench(file, io, deps = {}) {
1327
+ const text = await (deps.readFile ?? ((p) => readFile2(p, "utf8")))(file);
1328
+ let shard;
1329
+ try {
1330
+ shard = ScorecardDatasetSchema.parse(JSON.parse(text));
1331
+ } catch (error) {
1332
+ throw new CliError(
1333
+ `${file} is not a bench result file: ${error instanceof Error ? error.message.split("\n")[0] : String(error)}. Produce one with \`gezel bench --json <file>\`.`
1334
+ );
1335
+ }
1336
+ if (shard.runs.length !== 1) {
1337
+ throw new CliError(`${file} holds ${shard.runs.length} runs; submit one run per issue.`);
1338
+ }
1339
+ const { title, body } = describeSubmission(shard);
1340
+ const params = new URLSearchParams({ title, labels: "bench-result" });
1341
+ const url = `https://github.com/${BENCH_SUBMIT_REPO}/issues/new?${params.toString()}`;
1342
+ const gh = deps.gh ?? (async (args) => (await execFileAsync("gh", args, { encoding: "utf8" })).stdout);
1343
+ try {
1344
+ const stdout = await gh([
1345
+ "issue",
1346
+ "create",
1347
+ "--repo",
1348
+ BENCH_SUBMIT_REPO,
1349
+ "--title",
1350
+ title,
1351
+ "--label",
1352
+ "bench-result",
1353
+ "--body",
1354
+ body
1355
+ ]);
1356
+ const issueUrl = stdout.trim().split("\n").pop() ?? "";
1357
+ io.out(`Opened ${issueUrl}`);
1358
+ return { title, body, url, issueUrl };
1359
+ } catch {
1360
+ io.out("Open this page and attach the file to the issue:");
1361
+ io.out(` ${url}`);
1362
+ io.out(` file: ${file}`);
1363
+ io.note("(Install the GitHub CLI, `gh`, and sign in to have this done for you.)");
1364
+ return { title, body, url };
1365
+ }
1366
+ }
1367
+ function registerBenchCommands(program2, connect, io = {
1368
+ out: (line) => console.log(line),
1369
+ note: (line) => console.error(line),
1370
+ confirm: process.stdin.isTTY ? confirmOnTerminal : void 0,
1371
+ cpuModel: defaultCpuModel
1372
+ }) {
1373
+ const bench = program2.command("bench").description(
1374
+ "Run the model scorecard on this machine: the same tasks, checks and counting rules as the published scorecard"
1375
+ ).option(
1376
+ "--suite <id>",
1377
+ "core (default), productivity, developer, complex-work, a comma list, or all"
1378
+ ).option("--smoke", "Run the suite's pulse check instead (fast; not a suite score)").option("--model <id>", "Model to evaluate (default: the model chats use)").option("--provider <id>", "Provider for --model when a model is on more than one").option(
1379
+ "--count <n>",
1380
+ "Trials per task; 3 is what it takes to quote a rate (default 1)",
1381
+ (v) => Number.parseInt(v, 10)
1382
+ ).option("--json <file>", "Also write the results as a scorecard shard").option("--note <text>", "A note kept with the run").option("--yes", "Start without asking, however long the run").option("--verbose", "Show the harness's own log lines while it runs").action(async (opts) => {
1383
+ const client = await connect();
1384
+ await runBench(client, opts, io);
1385
+ });
1386
+ bench.command("list").description("Show the suites, how many tasks each has, and how long it can take").action(async () => {
1387
+ const client = await connect();
1388
+ for (const line of renderSuiteList(await client.getEvalCatalog())) io.out(line);
1389
+ });
1390
+ bench.command("results").description("Show finished runs on this machine").option("--limit <n>", "How many runs to show (default 20)", (v) => Number.parseInt(v, 10)).action(async (opts) => {
1391
+ const client = await connect();
1392
+ const catalog = await client.getEvalCatalog();
1393
+ for (const line of await renderResults(client, catalog, opts.limit ?? 20)) io.out(line);
1394
+ });
1395
+ bench.command("submit <file>").description("Share a results shard from `gezel bench --json` as a GitHub issue").action(async (file) => {
1396
+ await submitBench(file, io);
1397
+ });
1398
+ }
1399
+ async function confirmOnTerminal(question) {
1400
+ const { createInterface } = await import("readline/promises");
1401
+ const rl = createInterface({ input: process.stdin, output: process.stderr });
1402
+ try {
1403
+ const answer = await rl.question(question);
1404
+ return /^y(es)?$/i.test(answer.trim());
1405
+ } finally {
1406
+ rl.close();
1407
+ }
1408
+ }
1409
+
1410
+ // src/cli-errors.ts
1411
+ function formatCliFailure(err, env2 = process.env) {
1412
+ if (!(err instanceof Error)) return null;
1413
+ if (err.name === "CliError") return err.message;
1414
+ if (err.name !== "GezelApiError" || env2.GEZEL_DEBUG === "1") return null;
1415
+ const { status, details } = err;
1416
+ const request = /\bon ([A-Z]+ \S+)/.exec(err.message)?.[1];
1417
+ const serviceText = detailText(details);
1418
+ if (status === 404) {
1419
+ const what = serviceText && serviceText.toLowerCase() !== "not found" ? serviceText : void 0;
1420
+ return `error: not found${what ? `: ${what}` : request ? ` (${request})` : ""}`;
1421
+ }
1422
+ const lines = [`error: ${serviceText ?? err.message}`];
1423
+ if (serviceText && request) lines.push(`(Gezel service answered ${status ?? "?"} to ${request})`);
1424
+ lines.push("Set GEZEL_DEBUG=1 to see the full error.");
1425
+ return lines.join("\n");
1426
+ }
1427
+ function isNotFound(err) {
1428
+ return err instanceof Error && err.name === "GezelApiError" && err.status === 404;
1429
+ }
1430
+ function detailText(details) {
1431
+ if (typeof details === "string" && details.trim()) return details.trim();
1432
+ if (details && typeof details === "object") {
1433
+ for (const key of ["message", "error"]) {
1434
+ const value = details[key];
1435
+ if (typeof value === "string" && value.trim()) return value.trim();
1436
+ }
1437
+ }
1438
+ return void 0;
1439
+ }
1440
+
770
1441
  // src/craftbook-command.ts
771
1442
  import { setTimeout as delay } from "timers/promises";
772
1443
  import {
@@ -1074,7 +1745,7 @@ async function waitForMlxRuntime(client, writeProgress, options = {}) {
1074
1745
  `\rpreparing the MLX Python runtime (one-time, a few minutes): ${Math.floor(elapsed / 60)}m ${String(elapsed % 60).padStart(2, "0")}s`
1075
1746
  );
1076
1747
  shown = true;
1077
- await new Promise((resolve6) => setTimeout(resolve6, pollMs));
1748
+ await new Promise((resolve7) => setTimeout(resolve7, pollMs));
1078
1749
  continue;
1079
1750
  }
1080
1751
  if (status.phase === "error") {
@@ -1343,7 +2014,8 @@ function errorMessage(error) {
1343
2014
  import {
1344
2015
  classifySecurityLevel,
1345
2016
  resolveSecurityPolicy,
1346
- securityCapabilitiesOf
2017
+ securityCapabilitiesOf,
2018
+ securityPolicyForLevel
1347
2019
  } from "@bendyline/gezel";
1348
2020
  var SECURITY_LEVEL_LABELS = {
1349
2021
  "super-lockdown": "Super Lockdown",
@@ -1378,6 +2050,42 @@ var SECURITY_SWITCHES = [
1378
2050
  description: "Show or set on/off for gezels adding or changing the scripts in package.json"
1379
2051
  }
1380
2052
  ];
2053
+ var CAPABILITY_LABELS = [
2054
+ ["allowExternalChat", "Cloud models"],
2055
+ ["allowFileEdits", "File edits and the model\u2019s Git tools"],
2056
+ ["allowScriptExecution", "Sandboxed scripts"],
2057
+ ["allowAppNetwork", "Update checks and the connector syncs you start"],
2058
+ ["allowExternalServices", "Web search, mail and other outside services"],
2059
+ ["allowPackageScriptExecution", "Run package scripts"],
2060
+ ["allowPackageScriptEdits", "Edit package scripts"]
2061
+ ];
2062
+ var LEVEL_ALIASES = {
2063
+ "super-lockdown": "super-lockdown",
2064
+ superlockdown: "super-lockdown",
2065
+ lockdown: "lockdown",
2066
+ free: "free",
2067
+ unrestricted: "free"
2068
+ };
2069
+ function parseSecurityLevel(input) {
2070
+ const level = LEVEL_ALIASES[input.trim().toLowerCase()];
2071
+ if (!level) {
2072
+ throw new CliError(
2073
+ `Unknown security level "${input}". Choose super-lockdown, lockdown, or free (shown as Unrestricted in Settings).`
2074
+ );
2075
+ }
2076
+ return level;
2077
+ }
2078
+ function describeSecurityLevel(policy) {
2079
+ const caps = securityCapabilitiesOf(resolveSecurityPolicy({ securityPolicy: policy }));
2080
+ const level = classifySecurityLevel(caps);
2081
+ const width = Math.max(...CAPABILITY_LABELS.map(([, label]) => label.length));
2082
+ return [
2083
+ `Security level: ${SECURITY_LEVEL_LABELS[level]}${level === "custom" ? "" : ` (${level})`}`,
2084
+ ...CAPABILITY_LABELS.map(
2085
+ ([key, label]) => ` ${label.padEnd(width)} ${caps[key] ? "on" : "off"}`
2086
+ )
2087
+ ];
2088
+ }
1381
2089
  function enabledValue(state) {
1382
2090
  if (state === void 0) return void 0;
1383
2091
  if (state === "on") return true;
@@ -1386,6 +2094,34 @@ function enabledValue(state) {
1386
2094
  }
1387
2095
  function registerSecurityCommands(program2, connect) {
1388
2096
  const security = program2.command("security").description("Inspect or explicitly change security settings");
2097
+ security.command("level [action] [level]").description(
2098
+ "Show the security level, or set a preset: `level set super-lockdown|lockdown|free`"
2099
+ ).option("--json", "Output the level and its capabilities as JSON").action(
2100
+ async (action, requested, options) => {
2101
+ if (action !== void 0 && action !== "show" && action !== "set") {
2102
+ throw new CliError("Use `gezel security level`, `level show`, or `level set <level>`.");
2103
+ }
2104
+ if (action === "set" && requested === void 0) {
2105
+ throw new CliError("Name a level: super-lockdown, lockdown, or free.");
2106
+ }
2107
+ if (action !== "set" && requested !== void 0) {
2108
+ throw new CliError("To change the level, use `gezel security level set <level>`.");
2109
+ }
2110
+ const next = action === "set" ? parseSecurityLevel(requested) : void 0;
2111
+ const client = await connect();
2112
+ const policy = next ? securityPolicyForLevel(next) : {
2113
+ level: "custom",
2114
+ ...securityCapabilitiesOf(resolveSecurityPolicy(await client.getConfig()))
2115
+ };
2116
+ if (next) await client.updateConfig({ securityPolicy: policy });
2117
+ if (options.json) {
2118
+ const caps = securityCapabilitiesOf(resolveSecurityPolicy({ securityPolicy: policy }));
2119
+ console.log(JSON.stringify({ level: classifySecurityLevel(caps), ...caps }));
2120
+ return;
2121
+ }
2122
+ for (const line of describeSecurityLevel(policy)) console.log(line);
2123
+ }
2124
+ );
1389
2125
  for (const setting of SECURITY_SWITCHES) {
1390
2126
  security.command(`${setting.command} [state]`).description(setting.description).option("--json", "Output the setting as JSON").action(async (state, options) => {
1391
2127
  const enabled = enabledValue(state);
@@ -1404,6 +2140,17 @@ function registerSecurityCommands(program2, connect) {
1404
2140
  });
1405
2141
  }
1406
2142
  }
2143
+ function registerGrowthCommand(program2, connect) {
2144
+ program2.command("growth [state]").description("Show or set on/off for gezel growth: XP and the level-ups it offers").option("--json", "Output the setting as JSON").action(async (state, options) => {
2145
+ const enabled = enabledValue(state);
2146
+ const client = await connect();
2147
+ const config = enabled === void 0 ? await client.getConfig() : await client.updateConfig({ growth: { enabled } });
2148
+ const value = config.growth?.enabled !== false;
2149
+ console.log(
2150
+ options.json ? JSON.stringify({ growthEnabled: value }) : `Growth (XP and level-ups): ${value ? "on" : "off"}`
2151
+ );
2152
+ });
2153
+ }
1407
2154
  function registerProjectSettingsCommands(env2, connect, projectFor) {
1408
2155
  env2.command("indexing [state]").description("Show or set on/off for background workspace indexing in the current project").option("--json", "Output the project setting as JSON").action(async (state, options) => {
1409
2156
  const enabled = enabledValue(state);
@@ -1570,7 +2317,7 @@ function terminalPrompter(terminal = { input: process.stdin, output: process.std
1570
2317
  function ask(terminal, question, secret) {
1571
2318
  const { input, output } = terminal;
1572
2319
  return import("readline").then(
1573
- ({ clearScreenDown, createInterface, cursorTo, moveCursor }) => new Promise((resolve6, reject) => {
2320
+ ({ clearScreenDown, createInterface, cursorTo, moveCursor }) => new Promise((resolve7, reject) => {
1574
2321
  const rl = createInterface({
1575
2322
  input,
1576
2323
  output: secret ? new Writable({ write: (_chunk, _encoding, done) => done() }) : output,
@@ -1617,13 +2364,13 @@ function ask(terminal, question, secret) {
1617
2364
  if (settled) return;
1618
2365
  settle();
1619
2366
  output.write("\n");
1620
- resolve6("");
2367
+ resolve7("");
1621
2368
  });
1622
2369
  rl.question(question, (answer) => {
1623
2370
  settle();
1624
2371
  rl.close();
1625
2372
  if (secret) output.write("\n");
1626
- resolve6(answer);
2373
+ resolve7(answer);
1627
2374
  });
1628
2375
  draw();
1629
2376
  })
@@ -1743,8 +2490,8 @@ async function ensureEngines(client, engines, write) {
1743
2490
 
1744
2491
  // src/craftbook-shorthand.ts
1745
2492
  import { existsSync } from "fs";
1746
- import { readFile as readFile2, readdir } from "fs/promises";
1747
- import { join as join2 } from "path";
2493
+ import { readFile as readFile3, readdir } from "fs/promises";
2494
+ import { join as join3 } from "path";
1748
2495
  import { projectLocalCraftbooksRoot } from "@bendyline/gezel/paths";
1749
2496
  var SHORTHAND_FLAG = "--via-craftbook-shorthand";
1750
2497
  var VALUE_OPTIONS = /* @__PURE__ */ new Set(["--connect", "--token", "--home"]);
@@ -1781,7 +2528,7 @@ async function workspaceCraftbookId(workspaceDir, word) {
1781
2528
  const ids = [];
1782
2529
  for (const entry of await readdir(root, { withFileTypes: true })) {
1783
2530
  if (!entry.isDirectory()) continue;
1784
- const manifest = await readFile2(join2(root, entry.name, "manifest.json"), "utf8").then((text) => JSON.parse(text)).catch(() => null);
2531
+ const manifest = await readFile3(join3(root, entry.name, "manifest.json"), "utf8").then((text) => JSON.parse(text)).catch(() => null);
1785
2532
  if (!manifest) continue;
1786
2533
  const id = typeof manifest.id === "string" ? manifest.id : entry.name;
1787
2534
  if (typeof manifest.command === "string" && manifest.command.toLowerCase() === query) return id;
@@ -1796,6 +2543,132 @@ function shorthandEcho(id, tail) {
1796
2543
  return `\u2192 gezel do ${[id, ...tail].map(shellWord).join(" ")}`;
1797
2544
  }
1798
2545
 
2546
+ // src/daemon-stop.ts
2547
+ async function planDaemonStop(input) {
2548
+ const { pid, health } = input;
2549
+ if (pid === null) return { kind: "not-running" };
2550
+ if (!input.alive(pid)) return { kind: "stale" };
2551
+ if (health?.hostedBy === "app") return { kind: "brake" };
2552
+ if (health?.pid !== void 0) {
2553
+ if (health.pid !== pid) return { kind: "mismatch", healthPid: health.pid };
2554
+ if (health.hostedBy === "standalone") return { kind: "kill" };
2555
+ }
2556
+ const command = await input.commandOf(pid);
2557
+ if (command !== null && isGezeldCommand(command)) return { kind: "kill" };
2558
+ if (health) return { kind: "brake" };
2559
+ if (command === null) return { kind: "unconfirmed" };
2560
+ return { kind: "foreign", command };
2561
+ }
2562
+ function isGezeldCommand(command) {
2563
+ return /(?:^|[\\/\s"'])gezeld(?:\.[cm]?js|\.exe)?(?=["'\s]|$)/i.test(command);
2564
+ }
2565
+
2566
+ // src/doctor-report.ts
2567
+ import {
2568
+ describeProvenance,
2569
+ measuredClaim,
2570
+ measuredResultFor,
2571
+ measuredRowsFor,
2572
+ scorecardHardwareKey,
2573
+ scorecardHardwareLabel
2574
+ } from "@bendyline/gezel";
2575
+ var CHECK_LABELS = {
2576
+ spawn: "starts",
2577
+ toolRoundTrip: "answers a tool call",
2578
+ throughput: "decodes fast enough",
2579
+ reasoningBudget: "bounded reasoning",
2580
+ contextFit: "room for a prompt"
2581
+ };
2582
+ function renderFitness(entry, probing) {
2583
+ if (probing) return ["fitness check: running now"];
2584
+ if (!entry) {
2585
+ return [
2586
+ "fitness check: not run yet \u2014 Gezel runs it when a model is installed, or from Settings \u2192 Models"
2587
+ ];
2588
+ }
2589
+ const { record } = entry;
2590
+ const head = record.status === "probed" ? record.admitted ? "fitness check: passed" : "fitness check: FAILED" : `fitness check: ${record.status}`;
2591
+ const qualifiers = [
2592
+ entry.stale ? "the model changed since; treat as not run" : "",
2593
+ entry.hardwareChanged ? "this machine's memory changed since" : "",
2594
+ record.genTokensPerSec ? `${record.genTokensPerSec.toFixed(1)} tok/s` : "",
2595
+ record.createdAt ? record.createdAt.slice(0, 10) : ""
2596
+ ].filter(Boolean);
2597
+ const lines = [`${head}${qualifiers.length ? ` (${qualifiers.join(", ")})` : ""}`];
2598
+ for (const key of Object.keys(CHECK_LABELS)) {
2599
+ const check = record.checks[key];
2600
+ if (!check) continue;
2601
+ lines.push(` ${check.ok ? "ok " : "NO "} ${CHECK_LABELS[key].padEnd(20)} ${check.detail}`);
2602
+ }
2603
+ return lines;
2604
+ }
2605
+ function hardwareClassCoverage(dataset, modelId, device) {
2606
+ const key = scorecardHardwareKey(device);
2607
+ const name = scorecardHardwareLabel(device);
2608
+ const rounds = dataset.runs.filter(
2609
+ (run) => run.scope !== "subset" && scorecardHardwareKey(run.provenance.device) === key
2610
+ ).length;
2611
+ const mine = measuredRowsFor(dataset, modelId).filter(
2612
+ (row) => scorecardHardwareKey(row.run.provenance.device) === key
2613
+ ).length;
2614
+ if (rounds === 0) {
2615
+ return ` on ${name}: no published scorecard round has run on ${name} yet \u2014 \`gezel bench\` would be the first`;
2616
+ }
2617
+ const roundWord = rounds === 1 ? "round" : "rounds";
2618
+ if (mine === 0) {
2619
+ return ` on ${name}: ${rounds} published ${roundWord}, none with this model \u2014 \`gezel bench\` measures it here`;
2620
+ }
2621
+ return ` on ${name}: ${rounds} published ${roundWord}; ${mine} ${mine === 1 ? "result" : "results"} for this model`;
2622
+ }
2623
+ function renderMeasured(dataset, modelId, device, rows = 4) {
2624
+ const headline = measuredResultFor(dataset, modelId, device);
2625
+ const coverage = hardwareClassCoverage(dataset, modelId, device);
2626
+ if (!headline) {
2627
+ return [
2628
+ "measured: not measured in a published round \u2014 `gezel bench` measures it on this machine",
2629
+ coverage
2630
+ ];
2631
+ }
2632
+ const lines = [
2633
+ `measured: ${measuredClaim(headline)}${headline.sameHardwareClass ? "" : " \u2014 a different platform; your numbers may differ"}`,
2634
+ coverage
2635
+ ];
2636
+ for (const row of measuredRowsFor(dataset, modelId).slice(0, rows)) {
2637
+ lines.push(
2638
+ ` ${row.result.suiteId.padEnd(14)} ${row.score.claim.padEnd(36)} ${describeProvenance(row.run, [row.result.engine])}`
2639
+ );
2640
+ }
2641
+ lines.push(" Full tables: https://gezel.com/docs/model-scorecard/");
2642
+ return lines;
2643
+ }
2644
+ function renderDoctorModel(input) {
2645
+ if (!input.modelId) {
2646
+ return [
2647
+ `model: none for ${input.provider}`,
2648
+ " `gezel model list --available` shows what fits this machine; `gezel model pull <id>` downloads one."
2649
+ ];
2650
+ }
2651
+ const lines = [`model: ${input.modelId} (${input.provider})`];
2652
+ for (const line of renderFitness(input.fitness, input.probing)) lines.push(` ${line}`);
2653
+ for (const line of renderMeasured(input.dataset, input.modelId, input.device, input.rows)) {
2654
+ lines.push(` ${line}`);
2655
+ }
2656
+ return lines;
2657
+ }
2658
+ function doctorVerdict(input) {
2659
+ if (!input.nodeOk) return "Verdict: install Node.js 24 or newer, then run `gezel doctor` again.";
2660
+ if (!input.serviceReachable) {
2661
+ return 'Verdict: Gezel is installed but no service is running. `gezel start` starts one; `gezel run "hello"` starts one and sends a message.';
2662
+ }
2663
+ if (!input.modelId) {
2664
+ return "Verdict: the service runs but has no model to chat with. `gezel model list --available` shows what fits; `gezel model pull <id>` downloads one.";
2665
+ }
2666
+ if (input.fitnessAdmitted === false) {
2667
+ return `Verdict: ${input.modelId} failed its fitness check on this machine. Pick a smaller model with \`gezel model list --available\`, or re-run the check from Settings \u2192 Models.`;
2668
+ }
2669
+ return `Verdict: ready. ${input.modelId} is installed and ${input.fitnessAdmitted ? "passed its fitness check" : "has not been fitness-checked yet"}; \`gezel run "hello"\` to talk to it, \`gezel bench\` to measure it.`;
2670
+ }
2671
+
1799
2672
  // src/engagement-mode.ts
1800
2673
  var CLI_ENGAGEMENT_MODES = [
1801
2674
  {
@@ -1848,7 +2721,7 @@ var CLI_ENGAGEMENT_MODE_USAGE = CLI_ENGAGEMENT_MODES.map((option) => option.name
1848
2721
  );
1849
2722
 
1850
2723
  // src/generate.ts
1851
- import { mkdir as mkdir2, readFile as readFile3, writeFile as writeFile2 } from "fs/promises";
2724
+ import { mkdir as mkdir2, readFile as readFile4, writeFile as writeFile3 } from "fs/promises";
1852
2725
  import { basename as basename2, dirname as dirname2, resolve as resolve2 } from "path";
1853
2726
  async function resolvePromptText(parts, file, label) {
1854
2727
  const inline = (parts ?? []).join(" ").trim();
@@ -1856,7 +2729,7 @@ async function resolvePromptText(parts, file, label) {
1856
2729
  if (inline) throw new CliError(`provide either ${label} text or --file, not both.`);
1857
2730
  let raw;
1858
2731
  try {
1859
- raw = await readFile3(resolve2(file), "utf8");
2732
+ raw = await readFile4(resolve2(file), "utf8");
1860
2733
  } catch (err) {
1861
2734
  throw new CliError(`could not read --file "${file}": ${err.message}`);
1862
2735
  }
@@ -1892,7 +2765,7 @@ async function saveArtifact(client, projectId, artifactPath, output) {
1892
2765
  const bytes = Buffer.from(await blob.arrayBuffer());
1893
2766
  const dest = output ? resolve2(output) : resolve2(process.cwd(), basename2(artifactPath));
1894
2767
  await mkdir2(dirname2(dest), { recursive: true });
1895
- await writeFile2(dest, bytes);
2768
+ await writeFile3(dest, bytes);
1896
2769
  return dest;
1897
2770
  }
1898
2771
 
@@ -1948,7 +2821,7 @@ import {
1948
2821
  harnessAgentFileLine,
1949
2822
  harnessPromptInvocation
1950
2823
  } from "@bendyline/gezel";
1951
- import { GezelApiError as GezelApiError2, apiErrorMessage } from "@bendyline/gezel-client";
2824
+ import { GezelApiError as GezelApiError3, apiErrorMessage as apiErrorMessage2 } from "@bendyline/gezel-client";
1952
2825
  var STATE_WORDS = {
1953
2826
  configured: "set up",
1954
2827
  "not-configured": "not set up",
@@ -1976,7 +2849,7 @@ async function call(fn) {
1976
2849
  try {
1977
2850
  return await fn();
1978
2851
  } catch (error) {
1979
- if (error instanceof GezelApiError2) throw new CliError(apiErrorMessage(error));
2852
+ if (error instanceof GezelApiError3) throw new CliError(apiErrorMessage2(error));
1980
2853
  throw error;
1981
2854
  }
1982
2855
  }
@@ -2046,6 +2919,10 @@ function registerMcpCommands(program2, connect, output = console.log) {
2046
2919
  }
2047
2920
 
2048
2921
  // src/model-choice.ts
2922
+ import {
2923
+ measuredClaim as measuredClaim2,
2924
+ measuredResultFor as measuredResultFor2
2925
+ } from "@bendyline/gezel";
2049
2926
  async function installedChatModels(client, engine) {
2050
2927
  const { models } = engine === "mlx" ? await client.listMlxModels() : engine === "ds4" ? await client.listDs4Models() : await client.listLlamaCppModels();
2051
2928
  return models.map((model2) => model2.id);
@@ -2077,9 +2954,10 @@ async function availableChatModels(client, engine) {
2077
2954
  };
2078
2955
  }
2079
2956
  function formatAvailableChatModels(input) {
2080
- const { engine, ranked, installed, pinned } = input;
2957
+ const { engine, ranked, installed, pinned, measured } = input;
2081
2958
  if (ranked.length === 0) return `(no ${engine} chat models in the catalog for this machine)`;
2082
2959
  const width = Math.max(...ranked.map((model2) => model2.id.length));
2960
+ const measuredWidth = measured ? Math.max("not measured".length, ...ranked.map((m) => (measured.get(m.id) ?? "").length)) : 0;
2083
2961
  const lines = [`${engine} chat models for this machine, best first:`];
2084
2962
  ranked.forEach((model2, index) => {
2085
2963
  const notes = [
@@ -2087,8 +2965,9 @@ function formatAvailableChatModels(input) {
2087
2965
  installed.has(model2.id) ? "installed" : "",
2088
2966
  model2.id === pinned ? "in use" : ""
2089
2967
  ].filter(Boolean);
2968
+ const claim = measured ? ` ${(measured.get(model2.id) ?? "not measured").padEnd(measuredWidth)}` : "";
2090
2969
  lines.push(
2091
- ` ${model2.id.padEnd(width)} ${formatDownloadSize(model2.approxSizeBytes).padStart(7)} ${FIT_LABEL[model2.fit].padEnd(22)} ${model2.name}${notes.length > 0 ? ` (${notes.join(", ")})` : ""}`
2970
+ ` ${model2.id.padEnd(width)} ${formatDownloadSize(model2.approxSizeBytes).padStart(7)} ${FIT_LABEL[model2.fit].padEnd(22)}${claim} ${model2.name}${notes.length > 0 ? ` (${notes.join(", ")})` : ""}`
2092
2971
  );
2093
2972
  });
2094
2973
  const flag = providerFlag(engine, input.platformDefault);
@@ -2098,8 +2977,18 @@ function formatAvailableChatModels(input) {
2098
2977
  `Download one with \`gezel model pull <id>${flag}\`. The smallest is ${smallest.id} (${formatDownloadSize(smallest.approxSizeBytes)}).`,
2099
2978
  `\`gezel model use <id>${flag}\` picks the installed model chats use.`
2100
2979
  );
2980
+ if (measured) {
2981
+ lines.push(
2982
+ '"Fits" is about memory. The measured column is the published scorecard\'s pass rate on real jobs, on the hardware named; `gezel bench` measures a model on this machine.'
2983
+ );
2984
+ }
2101
2985
  return lines.join("\n");
2102
2986
  }
2987
+ function measuredClaimsFor(dataset, ids, device) {
2988
+ const claims = /* @__PURE__ */ new Map();
2989
+ for (const id of ids) claims.set(id, measuredClaim2(measuredResultFor2(dataset, id, device)));
2990
+ return claims;
2991
+ }
2103
2992
  async function useChatModel(client, id, options) {
2104
2993
  const order = options.requested ? [options.requested] : [options.platformDefault, ...["mlx", "llama-cpp", "ds4"]].filter(
2105
2994
  (engine2, index, all) => all.indexOf(engine2) === index
@@ -2436,7 +3325,7 @@ async function settledFirstRunConfig(client, options = {}) {
2436
3325
  let config = await client.getConfig();
2437
3326
  for (let attempt = 0; attempt < attempts; attempt += 1) {
2438
3327
  if (config.firstRunCompleted || config.provider !== "copilot") return config;
2439
- await new Promise((resolve6) => setTimeout(resolve6, delayMs));
3328
+ await new Promise((resolve7) => setTimeout(resolve7, delayMs));
2440
3329
  config = await client.getConfig();
2441
3330
  }
2442
3331
  return config;
@@ -2641,7 +3530,7 @@ var RunOutput = class {
2641
3530
  };
2642
3531
 
2643
3532
  // src/secrets-command.ts
2644
- import { GezelApiError as GezelApiError3 } from "@bendyline/gezel-client";
3533
+ import { GezelApiError as GezelApiError4 } from "@bendyline/gezel-client";
2645
3534
  var credentialFlags = {
2646
3535
  githubToken: "hasGithubToken",
2647
3536
  openaiApiKey: "hasOpenaiApiKey",
@@ -2695,7 +3584,7 @@ async function writeCredential(client, patch) {
2695
3584
  try {
2696
3585
  await client.updateConfig(patch);
2697
3586
  } catch (error) {
2698
- const status = error instanceof GezelApiError3 ? ` (HTTP ${error.status})` : "";
3587
+ const status = error instanceof GezelApiError4 ? ` (HTTP ${error.status})` : "";
2699
3588
  throw new CliError(
2700
3589
  `Could not update the credential${status}. Check the service connection and permissions.`
2701
3590
  );
@@ -2927,6 +3816,33 @@ function resolveRestoreSelection(review, opts) {
2927
3816
  return { items, skipped };
2928
3817
  }
2929
3818
 
3819
+ // src/task-output.ts
3820
+ import { mkdir as mkdir3, writeFile as writeFile4 } from "fs/promises";
3821
+ import { dirname as dirname4, isAbsolute as isAbsolute2, join as join4, relative, resolve as resolve5, sep } from "path";
3822
+ async function copyTaskOutput(client, projectId, artifactDir, outDir) {
3823
+ const root = resolve5(outDir);
3824
+ const listing = await client.listProjectArtifacts(projectId, artifactDir, true);
3825
+ const copied = [];
3826
+ for (const file of listing.files) {
3827
+ if (file.isDirectory) continue;
3828
+ const rel = relative(artifactDir, file.path).split(sep).join("/");
3829
+ if (!rel || rel.startsWith("..") || isAbsolute2(rel)) continue;
3830
+ if (rel === "inputs" || rel.startsWith("inputs/")) continue;
3831
+ const target = join4(root, rel);
3832
+ if (relative(root, target).startsWith("..")) continue;
3833
+ const blob = await client.fetchProjectArtifactBlob(projectId, file.path);
3834
+ await mkdir3(dirname4(target), { recursive: true });
3835
+ await writeFile4(target, Buffer.from(await blob.arrayBuffer()));
3836
+ copied.push(rel);
3837
+ }
3838
+ if (listing.truncated) {
3839
+ throw new CliError(
3840
+ `The task made more files than one listing returns; ${copied.length} were copied to ${root}. Open the rest from the task's folder in Gezel.`
3841
+ );
3842
+ }
3843
+ return copied;
3844
+ }
3845
+
2930
3846
  // src/unknown-command.ts
2931
3847
  function unknownCommandMessage(operands, known) {
2932
3848
  const word = operands[0] ?? "";
@@ -2974,7 +3890,7 @@ function editDistance(a, b) {
2974
3890
 
2975
3891
  // src/workflow-command.ts
2976
3892
  import { realpath as realpath2 } from "fs/promises";
2977
- import { isAbsolute as isAbsolute2, relative, resolve as resolve5 } from "path";
3893
+ import { isAbsolute as isAbsolute3, relative as relative2, resolve as resolve6 } from "path";
2978
3894
  import { pathToFileURL } from "url";
2979
3895
  import {
2980
3896
  craftbookFromDoc,
@@ -2991,10 +3907,10 @@ function validateWorkflowCraftbook(document) {
2991
3907
  }
2992
3908
  async function runWorkflow(client, projectId, workspace, file, args, log = console.error, invocation) {
2993
3909
  const named = /^[a-z][a-z0-9-]*$/.test(file);
2994
- const path = await realpath2(resolve5(workspace, named ? `.gezel/workflows/${file}.mjs` : file));
3910
+ const path = await realpath2(resolve6(workspace, named ? `.gezel/workflows/${file}.mjs` : file));
2995
3911
  if (invocation) {
2996
- const local = relative(await realpath2(workspace), path);
2997
- if (isAbsolute2(local) || local === ".." || local.startsWith("../") || local.startsWith("..\\")) {
3912
+ const local = relative2(await realpath2(workspace), path);
3913
+ if (isAbsolute3(local) || local === ".." || local.startsWith("../") || local.startsWith("..\\")) {
2998
3914
  throw new CliError("A craftbook workflow module must remain inside its project workspace.");
2999
3915
  }
3000
3916
  }
@@ -3013,7 +3929,7 @@ async function runWorkflow(client, projectId, workspace, file, args, log = conso
3013
3929
  return module.run({
3014
3930
  client,
3015
3931
  projectId,
3016
- workspace: resolve5(workspace),
3932
+ workspace: resolve6(workspace),
3017
3933
  args,
3018
3934
  ...invocation ?? {},
3019
3935
  log,
@@ -3075,6 +3991,16 @@ async function runWorkflow(client, projectId, workspace, file, args, log = conso
3075
3991
  });
3076
3992
  }
3077
3993
 
3994
+ // src/bin/node-version.ts
3995
+ var MINIMUM_NODE_MAJOR = 24;
3996
+ function unsupportedNodeMessage(version) {
3997
+ const major = Number.parseInt(version, 10);
3998
+ if (!Number.isFinite(major) || major >= MINIMUM_NODE_MAJOR) return void 0;
3999
+ return `Gezel needs Node.js ${MINIMUM_NODE_MAJOR} or newer (found v${version}).
4000
+ Install Node.js ${MINIMUM_NODE_MAJOR} or later from https://nodejs.org, then run the command again.
4001
+ `;
4002
+ }
4003
+
3078
4004
  // src/bin/gezel.ts
3079
4005
  var program = new Command();
3080
4006
  program.name("gezel").description("Gezel \u2014 assemble a team of AI companions (gezels) and put them to work.").addHelpText(
@@ -3091,9 +4017,14 @@ program.name("gezel").description("Gezel \u2014 assemble a team of AI companions
3091
4017
  "Use this folder as the active project (default and bare flag: cwd)."
3092
4018
  );
3093
4019
  var cliGlobals = () => program.opts();
4020
+ var connectForInspection = (opts) => opts.start === false ? connectRunning(cliGlobals()) : connectOwned(cliGlobals());
3094
4021
  registerSecretCommands(program, () => connectOwned(cliGlobals()));
4022
+ registerAccountCommands(program, async () => (await connectOwned(cliGlobals())).accounts);
4023
+ registerIntegrationCommands(program, () => connectOwned(cliGlobals()));
3095
4024
  registerMcpCommands(program, () => connectOwned(cliGlobals()));
4025
+ registerBenchCommands(program, () => connectOwned(cliGlobals()));
3096
4026
  registerSecurityCommands(program, () => connectOwned(cliGlobals()));
4027
+ registerGrowthCommand(program, () => connectOwned(cliGlobals()));
3097
4028
  program.hook("preAction", () => {
3098
4029
  const globals = cliGlobals();
3099
4030
  validateGlobals(globals);
@@ -3169,6 +4100,9 @@ program.command("start").description("Start the Gezel daemon (no-op if already r
3169
4100
  "--web",
3170
4101
  "Serve the web UI over HTTP on loopback and print a browser URL whose sign-in token lasts until the service restarts (reuses a daemon already serving it)."
3171
4102
  ).option("--open", "With --web, open the printed URL in your default browser.").option(
4103
+ "--accounts",
4104
+ "With --web, sign in with an account instead of a one-time URL; the first run prints a link to create one."
4105
+ ).option(
3172
4106
  "--restart",
3173
4107
  "With --web or --port, stop a running daemon started without them and start a new one."
3174
4108
  ).action(
@@ -3192,6 +4126,7 @@ program.command("start").description("Start the Gezel daemon (no-op if already r
3192
4126
  if (opts.web) {
3193
4127
  spawnEnv.GEZEL_WEB = "1";
3194
4128
  spawnEnv.GEZEL_INSECURE_TRANSPORT = "1";
4129
+ if (opts.accounts) spawnEnv.GEZEL_WEB_ACCOUNTS = "1";
3195
4130
  }
3196
4131
  if (opts.foreground) {
3197
4132
  await startForeground(spawnEnv);
@@ -3236,7 +4171,11 @@ Restart it ${wanted} with \`${[...retry, "--restart"].join(" ")}\`, or stop it f
3236
4171
  console.log(
3237
4172
  `gezeld running (version ${health2.version}) on port ${new URL(result.baseUrl).port}`
3238
4173
  );
3239
- if (opts.web) await printWebUrl(result.baseUrl, opts.open === true);
4174
+ if (opts.web && opts.accounts) {
4175
+ await printAccountsWebUrl(result.client.accounts, result.baseUrl, opts.open === true);
4176
+ } else if (opts.web) {
4177
+ await printWebUrl(result.baseUrl, opts.open === true);
4178
+ }
3240
4179
  return;
3241
4180
  }
3242
4181
  const client = await connectOwned(cliGlobals(), { announceSpawn: false });
@@ -3248,15 +4187,30 @@ async function runtimeServes(runtime, want) {
3248
4187
  if (want.port !== void 0 && runtime.port !== want.port) return false;
3249
4188
  if (!want.web) return true;
3250
4189
  if (new URL(runtime.baseUrl).protocol !== "http:") return false;
3251
- const { readFile: readFile4 } = await import("fs/promises");
4190
+ const { readFile: readFile5 } = await import("fs/promises");
3252
4191
  const { gezelPaths: gezelPaths2 } = await import("@bendyline/gezel/paths");
3253
- const token = await readFile4(gezelPaths2().runtime.webUiToken, "utf8").catch(() => "");
4192
+ const token = await readFile5(gezelPaths2().runtime.webUiToken, "utf8").catch(() => "");
3254
4193
  return token.trim().length > 0;
3255
4194
  }
4195
+ async function printAccountsWebUrl(client, baseUrl, open) {
4196
+ const origin = new URL(baseUrl).origin;
4197
+ const state = await client.getSignInState();
4198
+ let url = `${origin}/`;
4199
+ if (state.setupRequired) {
4200
+ const link = await client.createSetupLink();
4201
+ if (link.code) url = `${origin}/?setup=${encodeURIComponent(link.code)}`;
4202
+ }
4203
+ console.log("");
4204
+ console.log(
4205
+ state.setupRequired ? ` Create your account \u2192 ${url}` : ` Gezel web UI \u2192 ${url} (sign in with your account)`
4206
+ );
4207
+ console.log("");
4208
+ if (open) await openInBrowser(url);
4209
+ }
3256
4210
  async function printWebUrl(baseUrl, open) {
3257
- const { readFile: readFile4 } = await import("fs/promises");
4211
+ const { readFile: readFile5 } = await import("fs/promises");
3258
4212
  const { gezelPaths: gezelPaths2 } = await import("@bendyline/gezel/paths");
3259
- const token = (await readFile4(gezelPaths2().runtime.webUiToken, "utf8").catch(() => "")).trim();
4213
+ const token = (await readFile5(gezelPaths2().runtime.webUiToken, "utf8").catch(() => "")).trim();
3260
4214
  if (!token) {
3261
4215
  throw new CliError(
3262
4216
  "gezeld is running, but it published no web UI token, so there is no browser URL to print. Run `gezel start --web --restart`."
@@ -3302,10 +4256,10 @@ async function startForeground(env2) {
3302
4256
  process.on("SIGINT", requestStop);
3303
4257
  process.on("SIGTERM", requestStop);
3304
4258
  try {
3305
- await new Promise((resolve6) => {
4259
+ await new Promise((resolve7) => {
3306
4260
  child.on("exit", (code) => {
3307
4261
  process.exitCode = code ?? 0;
3308
- resolve6();
4262
+ resolve7();
3309
4263
  });
3310
4264
  });
3311
4265
  } finally {
@@ -3433,15 +4387,15 @@ program.command("stop").description(
3433
4387
  );
3434
4388
  }
3435
4389
  });
3436
- async function clearStaleRuntime(stoppedPid) {
3437
- const { readFile: readFile4, rm: rm3 } = await import("fs/promises");
4390
+ async function clearStaleRuntime(stoppedPid, opts = {}) {
4391
+ const { readFile: readFile5, rm: rm3 } = await import("fs/promises");
3438
4392
  const { gezelPaths: gezelPaths2 } = await import("@bendyline/gezel/paths");
3439
4393
  const runtime = gezelPaths2().runtime;
3440
4394
  const recorded = Number.parseInt(
3441
- (await readFile4(runtime.pid, "utf8").catch(() => "")).trim(),
4395
+ (await readFile5(runtime.pid, "utf8").catch(() => "")).trim(),
3442
4396
  10
3443
4397
  );
3444
- if (recorded !== stoppedPid || isProcessAlive2(stoppedPid)) return;
4398
+ if (recorded !== stoppedPid || !opts.pidReused && isProcessAlive2(stoppedPid)) return;
3445
4399
  await Promise.all(
3446
4400
  [
3447
4401
  runtime.pid,
@@ -3470,15 +4424,75 @@ async function stopUserDaemon(globals) {
3470
4424
  return;
3471
4425
  }
3472
4426
  const runtime = await readRuntime2();
3473
- if (!runtime) {
3474
- console.log("gezeld is not running");
3475
- return;
4427
+ const plan = await planDaemonStop({
4428
+ pid: runtime?.pid ?? null,
4429
+ alive: isProcessAlive2,
4430
+ health: runtime ? await runtimeHealth(runtime) : null,
4431
+ commandOf: processCommand
4432
+ });
4433
+ switch (plan.kind) {
4434
+ case "not-running":
4435
+ console.log("gezeld is not running");
4436
+ return;
4437
+ case "stale":
4438
+ await clearStaleRuntime(runtime.pid);
4439
+ console.log("gezeld is not running; removed the runtime files it left behind");
4440
+ return;
4441
+ case "foreign":
4442
+ await clearStaleRuntime(runtime.pid, { pidReused: true });
4443
+ console.log(
4444
+ `gezeld is not running. Its old process id ${runtime.pid} now belongs to another program, which was left alone; removed gezeld's leftover runtime files.`
4445
+ );
4446
+ return;
4447
+ case "unconfirmed":
4448
+ console.error(
4449
+ `gezeld (pid ${runtime.pid}) is not answering, and Gezel could not read the process list to confirm that pid is still gezeld, so it was not stopped.`
4450
+ );
4451
+ process.exitCode = 1;
4452
+ return;
4453
+ case "mismatch":
4454
+ console.error(
4455
+ `The Gezel service on port ${runtime.port} reports pid ${plan.healthPid}, but runtime/pid says ${runtime.pid}; stopped nothing. Run \`gezel status\` to see which is running.`
4456
+ );
4457
+ process.exitCode = 1;
4458
+ return;
4459
+ case "brake": {
4460
+ const result = await (await connectOwned(globals)).emergencyStopChats();
4461
+ console.log(
4462
+ `This Gezel service belongs to the Gezel app, which would start it again, so it keeps running. Stopped its AI work instead (${result.cancelledTurns} active ${result.cancelledTurns === 1 ? "chat" : "chats"}). Quit the Gezel app to shut it down.`
4463
+ );
4464
+ return;
4465
+ }
4466
+ case "kill":
4467
+ if (await stopDaemonPid(runtime.pid)) {
4468
+ console.log(`stopped gezeld pid=${runtime.pid}`);
4469
+ } else {
4470
+ console.error(`failed to confirm gezeld pid=${runtime.pid} stopped`);
4471
+ process.exitCode = 1;
4472
+ }
3476
4473
  }
3477
- if (await stopDaemonPid(runtime.pid)) {
3478
- console.log(`stopped gezeld pid=${runtime.pid}`);
3479
- } else {
3480
- console.error(`failed to confirm gezeld pid=${runtime.pid} stopped`);
3481
- process.exitCode = 1;
4474
+ }
4475
+ async function runtimeHealth(runtime) {
4476
+ try {
4477
+ const response = await requestDaemonHealth(runtime.baseUrl, {
4478
+ fetch: runtime.cert ? createTrustingFetch2({ cert: runtime.cert }) : globalThis.fetch,
4479
+ timeoutMs: 3e3
4480
+ });
4481
+ if (!response.ok) return null;
4482
+ const body = response.body ?? {};
4483
+ return {
4484
+ ...typeof body.pid === "number" ? { pid: body.pid } : {},
4485
+ ...body.hostedBy === "app" || body.hostedBy === "standalone" ? { hostedBy: body.hostedBy } : {}
4486
+ };
4487
+ } catch {
4488
+ return null;
4489
+ }
4490
+ }
4491
+ async function processCommand(pid) {
4492
+ try {
4493
+ return (await listProcessSnapshots()).find((entry) => entry.pid === pid)?.command ?? "";
4494
+ } catch {
4495
+ return null;
3482
4496
  }
3483
4497
  }
3484
4498
  program.command("mode [mode]").description("Show or set AI activity (read-only, reactive, reactive+tasks, or full-play)").action(async (requested) => {
@@ -3591,7 +4605,7 @@ Examples:
3591
4605
  "named craftbook parameter (repeatable)",
3592
4606
  (value, values) => [...values, value],
3593
4607
  []
3594
- ).option("--wait", "wait for completion; exit nonzero when blocked, canceled, or timed out").option("--timeout <seconds>", "wait budget in seconds; timeout leaves the task running", "7200").option("--json", "emit one JSON result on stdout").option(
4608
+ ).option("--wait", "wait for completion; exit nonzero when blocked, canceled, or timed out").option("--timeout <seconds>", "wait budget in seconds; timeout leaves the task running", "7200").option("--out <dir>", "wait, then copy what the task made into this folder").option("--json", "emit one JSON result on stdout").option(
3595
4609
  "--strict-sandbox",
3596
4610
  "require OS network isolation for custom scripts (CLI launches otherwise trust this recipe snapshot)"
3597
4611
  ).addOption(new Option(SHORTHAND_FLAG).hideHelp()).action(
@@ -3671,9 +4685,17 @@ Examples:
3671
4685
  trustScripts: !opts.strictSandbox,
3672
4686
  roleBasedNameOnlyMode: true
3673
4687
  });
3674
- if (opts.wait) {
4688
+ if (opts.wait || opts.out) {
3675
4689
  console.error(`started ${created.ref} \u2014 ${created.title}`);
3676
- await printTaskWait(client, created.ref, { ...opts, timeoutMs });
4690
+ const waited = await printTaskWait(client, created.ref, { ...opts, timeoutMs });
4691
+ if (opts.out && waited.outcome === "complete") {
4692
+ const dir = waited.task.artifactDir;
4693
+ if (!dir) throw new CliError(`${created.ref} finished without a task folder to copy.`);
4694
+ const copied = await copyTaskOutput(client, projectId, dir, opts.out);
4695
+ console.error(
4696
+ `copied ${copied.length} file${copied.length === 1 ? "" : "s"} to ${opts.out}`
4697
+ );
4698
+ }
3677
4699
  } else
3678
4700
  console.log(
3679
4701
  opts.json ? JSON.stringify(created) : `started ${created.ref} \u2014 ${created.title}`
@@ -3719,12 +4741,13 @@ async function printTaskWait(client, ref, opts) {
3719
4741
  console.error(`See its notes with \`gezel task notes ${result.task.ref}\`.`);
3720
4742
  }
3721
4743
  process.exitCode = result.exitCode;
4744
+ return result;
3722
4745
  }
3723
4746
  var craftbook = program.command("craftbook").description("Browse the craftbooks `gezel do` can start");
3724
4747
  craftbook.command("list [words...]").description(
3725
4748
  "List the craftbooks `gezel do` can start in this folder's project; words filter by command, title and tags"
3726
- ).option("--json", "emit the list as JSON").action(async (words, opts) => {
3727
- const client = await connectOwned(cliGlobals());
4749
+ ).option("--json", "emit the list as JSON").option("--no-start", "Use a running service only; never start one").action(async (words, opts) => {
4750
+ const client = await connectForInspection(opts);
3728
4751
  const preview = await previewRunProject(client, cliGlobals());
3729
4752
  const projectId = preview.projectId ?? "default";
3730
4753
  const [config, result] = await Promise.all([
@@ -3924,6 +4947,9 @@ handboek.command("export").description("Render the Handboek as a static HTML sit
3924
4947
  ).option("--og-out <dir>", "directory to render Open Graph cards into; must be outside --out").option(
3925
4948
  "--og-base <url>",
3926
4949
  "absolute URL the card directory is served from (e.g. https://gezel.com/og/); required with --og-out"
4950
+ ).option(
4951
+ "--apps-out <dir>",
4952
+ "also write the apps library (gezel.com/apps) into this directory, outside --out"
3927
4953
  ).action(
3928
4954
  async (opts) => {
3929
4955
  if (Boolean(opts.ogOut) !== Boolean(opts.ogBase)) {
@@ -3939,10 +4965,15 @@ handboek.command("export").description("Render the Handboek as a static HTML sit
3939
4965
  siteUrl: opts.siteUrl,
3940
4966
  canonicalBase: opts.canonicalBase,
3941
4967
  ogOut: opts.ogOut,
3942
- ogBase: opts.ogBase
4968
+ ogBase: opts.ogBase,
4969
+ appsOut: opts.appsOut
3943
4970
  });
3944
4971
  console.log(`Handboek exported: ${result.pages} pages \u2192 ${result.out}`);
4972
+ if (result.apps) {
4973
+ console.log(`Apps library: ${result.apps.apps} apps, ${result.apps.demos} with demos`);
4974
+ }
3945
4975
  for (const id of result.skipped) console.error(` skipped (no body): ${id}`);
4976
+ for (const note of result.warnings) console.error(` data table: ${note}`);
3946
4977
  const cards = result.cards;
3947
4978
  if (cards?.skipped) {
3948
4979
  console.error(` no Open Graph cards: ${cards.skipped}`);
@@ -4485,8 +5516,26 @@ function resolveModelProvider(raw) {
4485
5516
  }
4486
5517
  return resolveOnDeviceProvider3(process.platform, process.arch);
4487
5518
  }
4488
- model.command("concurrency [slots]").description("Show or set local inference slots; use auto to restore automatic sizing").option("-p, --provider <engine>", "mlx | llama-cpp | ds4 (default: platform default)").option("--json", "Output the slot override as JSON").action(async (slots, opts) => {
4489
- const provider = resolveModelProvider(opts.provider);
5519
+ var CONCURRENCY_SERVICE_PROVIDERS = [
5520
+ "codex-cli",
5521
+ "anthropic-cli",
5522
+ "copilot",
5523
+ "openai",
5524
+ "anthropic"
5525
+ ];
5526
+ model.command("concurrency [slots]").description(
5527
+ "Show or set how many turns a provider runs at once (local inference slots, or a cloud/CLI provider such as codex-cli); auto restores the default"
5528
+ ).option(
5529
+ "-p, --provider <engine>",
5530
+ `mlx | llama-cpp | ds4 | ${CONCURRENCY_SERVICE_PROVIDERS.join(" | ")} (default: platform default)`
5531
+ ).option("--json", "Output the slot override as JSON").action(async (slots, opts) => {
5532
+ const service = CONCURRENCY_SERVICE_PROVIDERS.find((name) => name === opts.provider);
5533
+ if (!service && opts.provider && !["mlx", "llama-cpp", "ds4"].includes(opts.provider)) {
5534
+ throw new CliError(
5535
+ `--provider must be one of mlx, llama-cpp, ds4, ${CONCURRENCY_SERVICE_PROVIDERS.join(", ")} (got "${opts.provider}")`
5536
+ );
5537
+ }
5538
+ const provider = service ?? resolveModelProvider(opts.provider);
4490
5539
  if (slots !== void 0 && slots !== "auto" && (!/^\d+$/.test(slots) || !Number.isSafeInteger(Number(slots)) || Number(slots) < 1)) {
4491
5540
  throw new CliError("Concurrency must be a positive integer, or auto.");
4492
5541
  }
@@ -4499,8 +5548,9 @@ model.command("concurrency [slots]").description("Show or set local inference sl
4499
5548
  await client.updateConfig({ providerConcurrency: concurrency });
4500
5549
  }
4501
5550
  const result = { provider, slots: concurrency[provider] ?? null };
5551
+ const applies = service ? "applies after a daemon restart" : "applies when idle sessions reload";
4502
5552
  console.log(
4503
- opts.json ? JSON.stringify(result) : `${provider}: ${result.slots ?? "auto"}${result.slots === null ? "" : " inference slots"}${slots === void 0 ? "" : " (applies when idle sessions reload)"}`
5553
+ opts.json ? JSON.stringify(result) : `${provider}: ${result.slots ?? "auto"}${result.slots === null ? "" : ` ${service ? "concurrent turns" : "inference slots"}`}${slots === void 0 ? "" : ` (${applies})`}`
4504
5554
  );
4505
5555
  });
4506
5556
  model.command("context <id> [tokens]").description("Show or set a model context window in tokens; use auto to clear an override").option("-p, --provider <engine>", "mlx | llama-cpp | ds4 (default: platform default)").option("--json", "Output the context override as JSON").action(
@@ -4523,16 +5573,27 @@ model.command("context <id> [tokens]").description("Show or set a model context
4523
5573
  );
4524
5574
  }
4525
5575
  );
4526
- model.command("list").description("List downloaded chat models, or with --available the catalog models that fit").option("-p, --provider <engine>", "mlx | llama-cpp | ds4 (default: platform default)").option("-a, --available", "List catalog models for this machine, with download sizes").action(async (opts) => {
4527
- const client = await connectOwned(cliGlobals());
5576
+ model.command("list").description("List downloaded chat models, or with --available the catalog models that fit").option("-p, --provider <engine>", "mlx | llama-cpp | ds4 (default: platform default)").option("-a, --available", "List catalog models for this machine, with download sizes").option("--no-start", "Use a running service only; never start one").action(async (opts) => {
5577
+ const client = await connectForInspection(opts);
4528
5578
  const provider = resolveModelProvider(opts.provider);
4529
5579
  if (opts.available) {
4530
5580
  const available = await availableChatModels(client, provider);
5581
+ const health = await client.health().catch(() => null);
5582
+ const device = {
5583
+ platform: health?.platform ?? process.platform,
5584
+ arch: process.arch,
5585
+ label: health?.platform ?? process.platform
5586
+ };
4531
5587
  console.log(
4532
5588
  formatAvailableChatModels({
4533
5589
  engine: provider,
4534
5590
  platformDefault: resolveOnDeviceProvider3(process.platform, process.arch),
4535
- ...available
5591
+ ...available,
5592
+ measured: measuredClaimsFor(
5593
+ SCORECARD,
5594
+ available.ranked.map((m) => m.id),
5595
+ device
5596
+ )
4536
5597
  })
4537
5598
  );
4538
5599
  return;
@@ -4599,6 +5660,10 @@ program.command("doctor").description("Check the Gezel installation").action(asy
4599
5660
  console.log(
4600
5661
  `machine engine: healthy (${broker.baseUrl}) version=${broker.version} \u2014 compute broker; the product API is the per-user daemon`
4601
5662
  );
5663
+ } else if (broker?.present && broker.healthy === false && broker.staleRuntimeDir) {
5664
+ console.log(
5665
+ `machine service: not running; its runtime files were left behind in ${broker.staleRuntimeDir} (remove them if you uninstalled the Gezel app)`
5666
+ );
4602
5667
  } else if (broker?.present && broker.healthy === false) {
4603
5668
  console.log(`machine service: runtime files present but unreachable (port ${broker.port})`);
4604
5669
  } else if (broker?.present) {
@@ -4609,6 +5674,7 @@ program.command("doctor").description("Check the Gezel installation").action(asy
4609
5674
  console.log("machine service: not installed");
4610
5675
  }
4611
5676
  console.log(`runtime file: ${runtime ? "present" : "missing"}`);
5677
+ let serviceReachable = Boolean(system);
4612
5678
  if (runtime) {
4613
5679
  const alive = isProcessAlive2(runtime.pid);
4614
5680
  console.log(` port=${runtime.port} pid=${runtime.pid} alive=${alive}`);
@@ -4620,12 +5686,59 @@ program.command("doctor").description("Check the Gezel installation").action(asy
4620
5686
  ...runtime.cert ? { fetch: createTrustingFetch2({ cert: runtime.cert }) } : {}
4621
5687
  });
4622
5688
  const health = await client.health();
5689
+ serviceReachable = true;
4623
5690
  const engineNote = health.machineEngineConnected !== void 0 ? ` machine engine connected: ${health.machineEngineConnected ? "yes" : "no"}` : "";
4624
5691
  console.log(` role=${health.serviceRole ?? "user"}${engineNote}`);
4625
5692
  } catch {
4626
5693
  }
4627
5694
  }
4628
5695
  }
5696
+ let modelId;
5697
+ let fitnessAdmitted = null;
5698
+ if (serviceReachable || globals.connect) {
5699
+ try {
5700
+ await withTransientConnection(globals, async (client) => {
5701
+ const [config, fitness, health] = await Promise.all([
5702
+ client.getConfig(),
5703
+ client.listModelFitness().catch(() => ({
5704
+ records: [],
5705
+ probing: []
5706
+ })),
5707
+ client.health().catch(() => null)
5708
+ ]);
5709
+ const provider = config.provider ?? "llama-cpp";
5710
+ modelId = config.defaultModel?.[provider];
5711
+ const key = modelId ? `${provider}:${modelId}` : "";
5712
+ const entry = fitness.records.find((r) => r.key === key);
5713
+ if (entry?.record.status === "probed" && !entry.stale)
5714
+ fitnessAdmitted = entry.record.admitted;
5715
+ const device = {
5716
+ platform: health?.platform ?? process.platform,
5717
+ arch: process.arch,
5718
+ label: health?.platform ?? process.platform
5719
+ };
5720
+ for (const line of renderDoctorModel({
5721
+ provider,
5722
+ modelId,
5723
+ fitness: entry,
5724
+ probing: fitness.probing.includes(key),
5725
+ dataset: SCORECARD,
5726
+ device
5727
+ })) {
5728
+ console.log(line);
5729
+ }
5730
+ });
5731
+ } catch {
5732
+ }
5733
+ }
5734
+ console.log(
5735
+ doctorVerdict({
5736
+ nodeOk: unsupportedNodeMessage(process.versions.node) === void 0,
5737
+ serviceReachable,
5738
+ modelId,
5739
+ fitnessAdmitted
5740
+ })
5741
+ );
4629
5742
  });
4630
5743
  program.command("cleanup").description("Show or reclaim the space Gezel is using on this device").option("--refresh", "Re-measure instead of using the cached sizes").option("--models", "Delete downloaded models").option("--engines", "Delete engine binaries and their caches").option("--toolsets", "Delete installed tools and the automation browser").option("--caches", "Delete catalog content and generated indexes").option("--redownloadable", "Delete everything Gezel can download again").option(
4631
5744
  "--content <what>",