heyspex 0.2.5 → 0.2.7

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.
Files changed (110) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/bin/hs.mjs +349 -38
  3. package/bin/spexcode.mjs +31 -0
  4. package/dev-factory/lib/dispatch-guard.mjs +75 -0
  5. package/dev-factory/lib/local-firecrawl.mjs +294 -0
  6. package/dev-factory/lib/runner-path.mjs +51 -0
  7. package/dev-factory/lib/seat-context.mjs +20 -4
  8. package/dev-factory/model-catalog.json +222 -222
  9. package/dev-factory/spexcrawl/firecrawl-cli.mjs +755 -0
  10. package/dev-factory/spexcrawl/spexcrawl-cli.mjs +734 -0
  11. package/dist/cli/hs-connect.mjs +40 -3
  12. package/dist/cli/hs-defer.mjs +3817 -3741
  13. package/dist/cli/hs-events.mjs +1296 -0
  14. package/dist/cli/hs-export-events.mjs +1252 -0
  15. package/dist/cli/hs-handoff.mjs +2576 -0
  16. package/dist/cli/hs-index-rebuild.mjs +228 -37
  17. package/dist/cli/hs-index-reconcile.mjs +231 -40
  18. package/dist/cli/hs-init.mjs +22 -3
  19. package/dist/cli/hs-install.mjs +22 -3
  20. package/dist/cli/hs-join.mjs +40 -3
  21. package/dist/cli/hs-next-launch.mjs +2777 -0
  22. package/dist/cli/hs-pin.mjs +1336 -0
  23. package/dist/cli/hs-prereq.mjs +1380 -0
  24. package/dist/cli/hs-revalidate.mjs +3257 -0
  25. package/dist/cli/hs-spawn.mjs +12403 -0
  26. package/dist/cli/hs-test.mjs +1046 -0
  27. package/dist/cli/hs-trash.mjs +362 -0
  28. package/dist/cli/hs-trust.mjs +197 -0
  29. package/dist/cli/hs-vault-pull.mjs +233 -42
  30. package/dist/cli/hs-vault-push.mjs +231 -40
  31. package/dist/cli/hs-vault.mjs +21 -7
  32. package/dist/cli/hs-workspace.mjs +19 -1
  33. package/dist/cli-templates/customer-vault/specs/entity-model.md +1728 -1728
  34. package/dist/cli-templates/customer-vault/specs/protocols/canonical-frontmatter-templates.md +464 -464
  35. package/dist/cli-templates/customer-vault/specs/protocols/destructive-action-checklist.md +122 -122
  36. package/dist/cli-templates/customer-vault/specs/protocols/memory-protocol.md +46 -46
  37. package/dist/cli-templates/customer-vault/specs/protocols/spec-hierarchy.md +530 -530
  38. package/dist/cli-templates/customer-vault/specs/protocols/spec-summary-authoring.md +83 -83
  39. package/dist/cli-templates/harness-commands/claude/hs-backfill.md +28 -28
  40. package/dist/cli-templates/harness-commands/claude/hs-capture.md +62 -62
  41. package/dist/cli-templates/harness-commands/claude/hs-defer.md +29 -29
  42. package/dist/cli-templates/harness-commands/claude/hs-ground.md +30 -30
  43. package/dist/cli-templates/harness-commands/claude/hs-next.md +29 -29
  44. package/dist/cli-templates/harness-commands/claude/hs-promote.md +70 -70
  45. package/dist/cli-templates/harness-commands/claude/hs-push.md +35 -35
  46. package/dist/cli-templates/harness-commands/claude/hs-qc.md +27 -27
  47. package/dist/cli-templates/harness-commands/claude/hs-res.md +83 -63
  48. package/dist/cli-templates/harness-commands/claude/hs-resolve.md +23 -23
  49. package/dist/cli-templates/harness-commands/claude/hs-view.md +28 -28
  50. package/dist/cli-templates/harness-commands/claude/kill-ports.md +32 -32
  51. package/dist/cli-templates/harness-commands/claude/prototype.md +91 -91
  52. package/dist/cli-templates/harness-commands/cursor/hs-backfill.md +22 -22
  53. package/dist/cli-templates/harness-commands/cursor/hs-capture.md +56 -56
  54. package/dist/cli-templates/harness-commands/cursor/hs-defer.md +23 -23
  55. package/dist/cli-templates/harness-commands/cursor/hs-ground.md +24 -24
  56. package/dist/cli-templates/harness-commands/cursor/hs-next.md +23 -23
  57. package/dist/cli-templates/harness-commands/cursor/hs-promote.md +64 -64
  58. package/dist/cli-templates/harness-commands/cursor/hs-push.md +29 -29
  59. package/dist/cli-templates/harness-commands/cursor/hs-qc.md +21 -21
  60. package/dist/cli-templates/harness-commands/cursor/hs-res.md +78 -58
  61. package/dist/cli-templates/harness-commands/cursor/hs-resolve.md +17 -17
  62. package/dist/cli-templates/harness-commands/cursor/hs-view.md +23 -23
  63. package/dist/cli-templates/harness-commands/cursor/kill-ports.md +26 -26
  64. package/dist/cli-templates/harness-commands/cursor/prototype.md +86 -86
  65. package/docs/copilot-plugin-install.md +9 -2
  66. package/docs/mcp-client-config.md +6 -0
  67. package/package.json +266 -257
  68. package/plugins/heyspex/.claude-plugin/plugin.json +1 -1
  69. package/plugins/heyspex/.cursor-plugin/plugin.json +1 -1
  70. package/plugins/heyspex/README.md +6 -0
  71. package/plugins/heyspex/bin/launch-mcp.mjs +17 -0
  72. package/plugins/heyspex/hooks/hooks.json +418 -418
  73. package/scripts/claude-hooks/_intent-classifier-semantic.mjs +2 -2
  74. package/scripts/claude-hooks/wip-checkpoint.mjs +17 -2
  75. package/scripts/claude-statusline/statusline-command.mjs +287 -27
  76. package/scripts/hs-companion.mjs +41 -3
  77. package/scripts/install-claude-code-hooks.mjs +104 -3
  78. package/scripts/install-copilot-hooks.mjs +21 -1
  79. package/scripts/install-cursor-hooks.mjs +4 -3
  80. package/scripts/lib/capture-execution-guard.mjs +221 -0
  81. package/scripts/lib/companion-install.mjs +768 -0
  82. package/scripts/lib/customer-kit.mjs +53 -3
  83. package/scripts/lib/hs-companion-settings.mjs +26 -0
  84. package/scripts/lib/hs-source-push.mjs +386 -386
  85. package/scripts/record-grounding.mjs +38 -0
  86. package/spexharness/doctrine/baseline/SPEXCODE-DOCTRINE.baseline.json +29 -29
  87. package/spexharness/src/adapters/copilot-models.mjs +179 -0
  88. package/spexharness/src/adapters/harness-detect.mjs +148 -0
  89. package/spexharness/src/gates/directive-obligation-gate.decide.mjs +132 -132
  90. package/spexharness/src/gates/vault-write-tier-guard.decide.mjs +153 -125
  91. package/spexharness/src/seat-context.mjs +10 -5
  92. package/spexharness/src/session-bootstrap.mjs +6 -3
  93. package/vendor/heyspex-mcp/cli.mjs +6 -1
  94. package/vendor/heyspex-mcp/config-seed.yaml +15 -15
  95. package/vendor/heyspex-mcp/dev-factory-overlay.mjs +142 -0
  96. package/vendor/heyspex-mcp/http-transport.mjs +1 -1
  97. package/vendor/heyspex-mcp/memory-tools.mjs +523 -523
  98. package/vendor/heyspex-mcp/on-deck-scheduler.mjs +1708 -1708
  99. package/vendor/heyspex-mcp/procedure-handlers.mjs +327 -327
  100. package/vendor/heyspex-mcp/procedure-tools.mjs +1209 -1181
  101. package/vendor/heyspex-mcp/research-tools.mjs +22 -2
  102. package/vendor/heyspex-mcp/seat-assign-cli.mjs +242 -0
  103. package/vendor/heyspex-mcp/seat-assignment-rules.mjs +96 -0
  104. package/vendor/heyspex-mcp/seat-assignments-client.mjs +265 -0
  105. package/vendor/heyspex-mcp/seat-defaults-cli.mjs +45 -0
  106. package/vendor/heyspex-mcp/seat-defaults.mjs +232 -0
  107. package/vendor/heyspex-mcp/seat-identity.mjs +171 -0
  108. package/vendor/heyspex-mcp/test-helpers.mjs +37 -23
  109. package/vendor/heyspex-mcp/vault-io.mjs +720 -720
  110. package/bin/postinstall.mjs +0 -88
@@ -0,0 +1,2576 @@
1
+ import { createRequire as __hsCreateRequire } from 'node:module';
2
+ const require = __hsCreateRequire(import.meta.url);
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __esm = (fn, res) => function __init() {
5
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
6
+ };
7
+
8
+ // scripts/lib/heyspex-home.mjs
9
+ import { homedir } from "node:os";
10
+ function heyspexHome(env = process.env) {
11
+ const override = env?.HEYSPEX_HOME;
12
+ if (typeof override === "string" && override.trim().length > 0) {
13
+ return override.trim();
14
+ }
15
+ return homedir();
16
+ }
17
+ var init_heyspex_home = __esm({
18
+ "scripts/lib/heyspex-home.mjs"() {
19
+ "use strict";
20
+ }
21
+ });
22
+
23
+ // vendor/heyspex-mcp/credentials.mjs
24
+ import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
25
+ import os, { homedir as homedir2 } from "node:os";
26
+ import { dirname as dirname2, join as join2, resolve } from "node:path";
27
+ function credentialsFilePath(homeDir = heyspexHome()) {
28
+ return join2(homeDir, ".heyspex", "credentials.yaml");
29
+ }
30
+ function parseMcpToken(raw) {
31
+ if (typeof raw !== "string" || raw.length === 0) return null;
32
+ const token = raw.match(MCP_TOKEN_LINE_RE)?.[1]?.trim();
33
+ return token ? token : null;
34
+ }
35
+ function isValidAccountId(id) {
36
+ return typeof id === "string" && ACCOUNT_ID_RE.test(id);
37
+ }
38
+ function topLevelScalar(raw, key) {
39
+ const re = new RegExp(`^${key}:[ \\t]*["']?([^"'\\r\\n#]+)`, "m");
40
+ const value = raw.match(re)?.[1]?.trim();
41
+ return value ? value : null;
42
+ }
43
+ function decodeScalar(text) {
44
+ const t = text.trim();
45
+ if (t === "" || t === "null" || t === "~") return null;
46
+ if (t.startsWith('"')) {
47
+ try {
48
+ const v = JSON.parse(t);
49
+ return typeof v === "string" && v ? v : null;
50
+ } catch {
51
+ return t.replace(/^"|"$/g, "") || null;
52
+ }
53
+ }
54
+ if (t.startsWith("'")) return t.replace(/^'|'$/g, "") || null;
55
+ const bare = t.replace(/\s+#.*$/, "").trim();
56
+ return bare || null;
57
+ }
58
+ function emptyAccount() {
59
+ return { mcp_token: null, console_url: null, email: null, paired_at: null };
60
+ }
61
+ function findAccountsBlock(lines) {
62
+ const start = lines.findIndex((l) => /^accounts:[ \t]*(#.*)?$/.test(l));
63
+ if (start < 0) return null;
64
+ let end = start + 1;
65
+ while (end < lines.length && (lines[end].trim() === "" || /^[ \t]/.test(lines[end]))) end += 1;
66
+ while (end > start + 1 && lines[end - 1].trim() === "") end -= 1;
67
+ return { start, end };
68
+ }
69
+ function parseAccountsBlock(lines) {
70
+ const accounts = {};
71
+ const block = findAccountsBlock(lines);
72
+ if (!block) return accounts;
73
+ let current = null;
74
+ for (const line of lines.slice(block.start + 1, block.end)) {
75
+ const header = line.match(/^ {2}([^\s:#][^:]*):[ \t]*(#.*)?$/);
76
+ if (header) {
77
+ const id = decodeScalar(header[1]);
78
+ current = isValidAccountId(id) ? id : null;
79
+ if (current) accounts[current] = emptyAccount();
80
+ continue;
81
+ }
82
+ const field = line.match(/^ {4}([a-z_]+):(.*)$/);
83
+ if (field && current && ACCOUNT_FIELDS.includes(field[1])) {
84
+ accounts[current][field[1]] = decodeScalar(field[2]);
85
+ }
86
+ }
87
+ return accounts;
88
+ }
89
+ function parseCredentialsStore(raw) {
90
+ const text = typeof raw === "string" ? raw : "";
91
+ if (topLevelScalar(text, "schema_version") !== String(CREDENTIALS_SCHEMA_VERSION)) {
92
+ const token = parseMcpToken(text);
93
+ if (!token) return { schemaVersion: 1, currentAccountId: null, accounts: {} };
94
+ return {
95
+ schemaVersion: 1,
96
+ currentAccountId: LEGACY_ACCOUNT_ID,
97
+ accounts: {
98
+ [LEGACY_ACCOUNT_ID]: { ...emptyAccount(), mcp_token: token, console_url: topLevelScalar(text, "console_url") }
99
+ }
100
+ };
101
+ }
102
+ const accounts = parseAccountsBlock(text.split(/\r?\n/));
103
+ const current = topLevelScalar(text, "current_account_id");
104
+ return {
105
+ schemaVersion: 2,
106
+ currentAccountId: current && accounts[current] ? current : null,
107
+ accounts
108
+ };
109
+ }
110
+ function resolveBearer({ envToken, credentialsText, accountId, legacyAccountId } = {}) {
111
+ const fromEnv = typeof envToken === "string" ? envToken.trim() : "";
112
+ if (fromEnv) return { token: fromEnv, source: "env" };
113
+ const store = parseCredentialsStore(credentialsText);
114
+ let entry;
115
+ if (typeof accountId === "string" && accountId.trim()) {
116
+ const wanted = accountId.trim();
117
+ entry = store.accounts[wanted];
118
+ if (!entry && store.schemaVersion === 1 && legacyAccountId && wanted === legacyAccountId) {
119
+ entry = store.accounts[LEGACY_ACCOUNT_ID];
120
+ }
121
+ } else {
122
+ entry = store.currentAccountId ? store.accounts[store.currentAccountId] : void 0;
123
+ }
124
+ if (entry?.mcp_token) return { token: entry.mcp_token, source: "credentials-file" };
125
+ return { token: null, source: null };
126
+ }
127
+ function readTextOrNull(read, path3) {
128
+ try {
129
+ const raw = read(path3);
130
+ return typeof raw === "string" ? raw : null;
131
+ } catch {
132
+ return null;
133
+ }
134
+ }
135
+ function readLegacyConfigAccountId(read, homeDir) {
136
+ const raw = readTextOrNull(read, join2(homeDir, ".heyspex", "config.yaml"));
137
+ const value = raw?.match(CONFIG_ACCOUNT_ID_RE)?.[1]?.trim() ?? "";
138
+ return value || null;
139
+ }
140
+ function readBearer({ env = process.env, homeDir = heyspexHome(), readFile, accountId } = {}) {
141
+ const read = readFile ?? ((p) => readFileSync(p, "utf8"));
142
+ const credentialsText = readTextOrNull(read, credentialsFilePath(homeDir));
143
+ const named = typeof accountId === "string" && accountId.trim() ? accountId.trim() : null;
144
+ const legacyAccountId = named && parseCredentialsStore(credentialsText).schemaVersion === 1 ? readLegacyConfigAccountId(read, homeDir) : null;
145
+ return resolveBearer({ envToken: env?.[MCP_TOKEN_ENV], credentialsText, accountId: named, legacyAccountId });
146
+ }
147
+ function readCurrentAccountId({ homeDir = heyspexHome(), readFile } = {}) {
148
+ const read = readFile ?? ((p) => readFileSync(p, "utf8"));
149
+ const store = parseCredentialsStore(readTextOrNull(read, credentialsFilePath(homeDir)));
150
+ if (store.schemaVersion === 2 && store.currentAccountId && store.currentAccountId !== LEGACY_ACCOUNT_ID) {
151
+ return store.currentAccountId;
152
+ }
153
+ if (store.schemaVersion === 2 && store.currentAccountId === null) return null;
154
+ return readLegacyConfigAccountId(read, homeDir);
155
+ }
156
+ var MCP_TOKEN_ENV, MCP_TOKEN_LINE_RE, CREDENTIALS_SCHEMA_VERSION, LEGACY_ACCOUNT_ID, ACCOUNT_FIELDS, ACCOUNT_ID_RE, CONFIG_ACCOUNT_ID_RE;
157
+ var init_credentials = __esm({
158
+ "vendor/heyspex-mcp/credentials.mjs"() {
159
+ "use strict";
160
+ init_heyspex_home();
161
+ MCP_TOKEN_ENV = "HEYSPEX_MCP_TOKEN";
162
+ MCP_TOKEN_LINE_RE = /^mcp_token:[ \t]*["']?([^"'\r\n#]+)/m;
163
+ CREDENTIALS_SCHEMA_VERSION = 2;
164
+ LEGACY_ACCOUNT_ID = "legacy";
165
+ ACCOUNT_FIELDS = ["mcp_token", "console_url", "email", "paired_at"];
166
+ ACCOUNT_ID_RE = /^[A-Za-z0-9][A-Za-z0-9_.@+-]*$/;
167
+ CONFIG_ACCOUNT_ID_RE = /^account_id:\s*["']?([^"'\r\n#]+)/m;
168
+ }
169
+ });
170
+
171
+ // scripts/lib/dev-mode.mjs
172
+ import { mkdirSync as mkdirSync5, readFileSync as readFileSync8, writeFileSync as writeFileSync5 } from "node:fs";
173
+ import { homedir as homedir8 } from "node:os";
174
+ import { join as join8 } from "node:path";
175
+ function devModeFilePath(homeDir = heyspexHome()) {
176
+ return join8(homeDir, ".heyspex", DEV_MODE_FILENAME);
177
+ }
178
+ function normalizeMode(raw) {
179
+ const v = String(raw ?? "").trim().toLowerCase();
180
+ if (v === "prod" || v === "production") return { mode: "prod", reliance: "cloud" };
181
+ if (v === "dev") return { mode: "dev", reliance: null };
182
+ if (v === "local") return { mode: "dev", reliance: "local" };
183
+ if (v === "cloud") return { mode: "dev", reliance: "cloud" };
184
+ return null;
185
+ }
186
+ function normalizeReliance(raw) {
187
+ const v = String(raw ?? "").trim().toLowerCase();
188
+ return v === "local" || v === "cloud" ? v : null;
189
+ }
190
+ function deriveSemantics(mode, reliance) {
191
+ const effectiveReliance = mode === "prod" ? "cloud" : reliance ?? "local";
192
+ const local = mode === "dev" && effectiveReliance === "local";
193
+ return {
194
+ reliance: effectiveReliance,
195
+ consoleUrl: local ? LOCAL_CONSOLE_URL : CLOUD_CONSOLE_URL,
196
+ readTier: local ? "disk" : "neon",
197
+ vaultWriteMode: local ? "local" : "cloud"
198
+ };
199
+ }
200
+ function readLocalDevModeRecord({ homeDir = heyspexHome(), readFile = defaultReadFile } = {}) {
201
+ let raw;
202
+ try {
203
+ raw = readFile(devModeFilePath(homeDir));
204
+ } catch {
205
+ return { record: null, parseError: null };
206
+ }
207
+ try {
208
+ const parsed = JSON.parse(raw);
209
+ const mode = parsed?.mode === "prod" || parsed?.mode === "dev" ? parsed.mode : null;
210
+ const reliance = normalizeReliance(parsed?.reliance);
211
+ if (!mode || mode === "dev" && !reliance) {
212
+ return { record: null, parseError: `invalid dev-mode record: ${JSON.stringify(parsed)}` };
213
+ }
214
+ return {
215
+ record: {
216
+ mode,
217
+ reliance: mode === "prod" ? "cloud" : reliance,
218
+ console_url: typeof parsed.console_url === "string" ? parsed.console_url : null,
219
+ updated_at: typeof parsed.updated_at === "string" ? parsed.updated_at : null,
220
+ source: typeof parsed.source === "string" ? parsed.source : null
221
+ },
222
+ parseError: null
223
+ };
224
+ } catch (err) {
225
+ return { record: null, parseError: `unparseable dev-mode.json: ${String(err?.message ?? err)}` };
226
+ }
227
+ }
228
+ function overrideCloudCheck(source, cloudKnown, staleAgainstCloud) {
229
+ if (source !== "local-override") return "not-applicable";
230
+ if (!cloudKnown) return "unverified";
231
+ return staleAgainstCloud ? "stale" : "in-sync";
232
+ }
233
+ function resolveDevMode({
234
+ env = process.env,
235
+ homeDir = heyspexHome(),
236
+ readFile = defaultReadFile,
237
+ cloudSetting = void 0
238
+ } = {}) {
239
+ const local = readLocalDevModeRecord({ homeDir, readFile });
240
+ const cloudKnown = cloudSetting !== void 0;
241
+ const cloudValue = cloudKnown && cloudSetting ? {
242
+ mode: cloudSetting.mode === "dev" ? "dev" : "prod",
243
+ reliance: normalizeReliance(cloudSetting.reliance) ?? "cloud"
244
+ } : null;
245
+ let mode;
246
+ let reliance;
247
+ let source;
248
+ let relianceSource;
249
+ const envParsed = normalizeMode(env.HEYSPEX_DEV_MODE);
250
+ if (envParsed) {
251
+ mode = envParsed.mode;
252
+ const envReliance = envParsed.reliance ?? normalizeReliance(env.HEYSPEX_DEV_RELIANCE);
253
+ reliance = envReliance ?? "local";
254
+ source = "env";
255
+ relianceSource = envReliance ? "env" : "assumed-local";
256
+ } else if (local.record) {
257
+ mode = local.record.mode;
258
+ reliance = local.record.reliance;
259
+ source = "local-override";
260
+ } else if (cloudValue) {
261
+ mode = cloudValue.mode;
262
+ reliance = cloudValue.reliance;
263
+ source = "cloud-setting";
264
+ } else {
265
+ mode = "prod";
266
+ reliance = "cloud";
267
+ source = "default";
268
+ }
269
+ const semantics = deriveSemantics(mode, reliance);
270
+ const consoleUrl = source === "local-override" && local.record?.console_url ? local.record.console_url.replace(/\/+$/, "") : semantics.consoleUrl;
271
+ const staleAgainstCloud = source === "local-override" && cloudValue !== null && (cloudValue.mode !== mode || mode === "dev" && cloudValue.reliance !== semantics.reliance);
272
+ const cloudCheck = overrideCloudCheck(source, cloudKnown, staleAgainstCloud);
273
+ return {
274
+ mode,
275
+ reliance: semantics.reliance,
276
+ consoleUrl,
277
+ readTier: semantics.readTier,
278
+ vaultWriteMode: semantics.vaultWriteMode,
279
+ sources: {
280
+ mode: source,
281
+ reliance: relianceSource ?? source,
282
+ consoleUrl: source === "local-override" && local.record?.console_url ? "local-override" : "derived",
283
+ readTier: "derived",
284
+ vaultWriteMode: "derived"
285
+ },
286
+ localOverride: {
287
+ present: local.record !== null,
288
+ record: local.record,
289
+ parseError: local.parseError,
290
+ staleAgainstCloud,
291
+ cloudCheck
292
+ },
293
+ cloudSetting: { known: cloudKnown, value: cloudValue }
294
+ };
295
+ }
296
+ var CLOUD_CONSOLE_URL, LOCAL_CONSOLE_URL, DEV_MODE_FILENAME, defaultReadFile;
297
+ var init_dev_mode = __esm({
298
+ "scripts/lib/dev-mode.mjs"() {
299
+ "use strict";
300
+ init_heyspex_home();
301
+ init_credentials();
302
+ CLOUD_CONSOLE_URL = "https://app.heyspex.com";
303
+ LOCAL_CONSOLE_URL = "http://localhost:3000";
304
+ DEV_MODE_FILENAME = "dev-mode.json";
305
+ defaultReadFile = (p) => readFileSync8(p, "utf8");
306
+ }
307
+ });
308
+
309
+ // spex-console/bin/hs-handoff.ts
310
+ import { readFileSync as readFileSync10 } from "node:fs";
311
+ import process3 from "node:process";
312
+
313
+ // spex-console/src/lib/cli/control-shared.ts
314
+ import path2 from "node:path";
315
+
316
+ // scripts/lib/hs-command.mjs
317
+ import { existsSync } from "node:fs";
318
+ import { delimiter, dirname, join } from "node:path";
319
+ import { fileURLToPath } from "node:url";
320
+ var HS_BIN_NAME = "hs";
321
+ var HS_DISPATCHER_PATH = (() => {
322
+ try {
323
+ const here = dirname(fileURLToPath(import.meta.url));
324
+ return join(dirname(dirname(here)), "bin", "hs.mjs");
325
+ } catch {
326
+ return null;
327
+ }
328
+ })();
329
+ function shellQuote(p) {
330
+ return /[\\\s]/.test(p) ? `"${p}"` : p;
331
+ }
332
+ var HS_DISPATCHER_PREFIX = HS_DISPATCHER_PATH ? `node ${shellQuote(HS_DISPATCHER_PATH)}` : HS_BIN_NAME;
333
+ function candidateNames(name, env) {
334
+ if (process.platform !== "win32") return [name];
335
+ const pathext = (env.PATHEXT || ".COM;.EXE;.BAT;.CMD").split(";").map((e) => e.trim().toLowerCase()).filter(Boolean);
336
+ const extensions = [.../* @__PURE__ */ new Set([...pathext, ".ps1"])];
337
+ return [name, ...extensions.map((ext) => name + ext)];
338
+ }
339
+ function findOnPath(name, { env = process.env, exists = existsSync } = {}) {
340
+ try {
341
+ const raw = env.PATH ?? env.Path ?? env.path ?? "";
342
+ for (const dir of String(raw).split(delimiter)) {
343
+ const trimmed = dir.trim().replace(/^"(.*)"$/, "$1");
344
+ if (!trimmed) continue;
345
+ for (const candidate of candidateNames(name, env)) {
346
+ const full = join(trimmed, candidate);
347
+ try {
348
+ if (exists(full)) return full;
349
+ } catch {
350
+ }
351
+ }
352
+ }
353
+ } catch {
354
+ }
355
+ return null;
356
+ }
357
+ var cached = null;
358
+ var HS_ON_PATH_OVERRIDE_ENV = "HEYSPEX_HS_ON_PATH";
359
+ function onPathOverride(env) {
360
+ const raw = String(env?.[HS_ON_PATH_OVERRIDE_ENV] ?? "");
361
+ if (raw === "1") return true;
362
+ if (raw === "0") return false;
363
+ return void 0;
364
+ }
365
+ function describeHsCommand({ env, exists, useCache = true } = {}) {
366
+ const injected = env !== void 0 || exists !== void 0;
367
+ const effectiveEnv = env ?? process.env;
368
+ const override = onPathOverride(effectiveEnv);
369
+ if (override !== void 0) {
370
+ return {
371
+ onPath: override,
372
+ prefix: override ? HS_BIN_NAME : HS_DISPATCHER_PREFIX,
373
+ resolvedPath: null,
374
+ dispatcherPath: HS_DISPATCHER_PATH
375
+ };
376
+ }
377
+ if (useCache && !injected && cached) return cached;
378
+ const resolvedPath = findOnPath(HS_BIN_NAME, {
379
+ env: env ?? process.env,
380
+ exists: exists ?? existsSync
381
+ });
382
+ const answer = {
383
+ onPath: resolvedPath !== null,
384
+ prefix: resolvedPath !== null ? HS_BIN_NAME : HS_DISPATCHER_PREFIX,
385
+ resolvedPath,
386
+ dispatcherPath: HS_DISPATCHER_PATH
387
+ };
388
+ if (useCache && !injected) cached = answer;
389
+ return answer;
390
+ }
391
+ function hsCommand(args = "", options = {}) {
392
+ let prefix = HS_DISPATCHER_PREFIX;
393
+ try {
394
+ prefix = describeHsCommand(options).prefix;
395
+ } catch {
396
+ }
397
+ const tail = String(args ?? "").trim();
398
+ return tail ? `${prefix} ${tail}` : prefix;
399
+ }
400
+
401
+ // vendor/heyspex-mcp/console-fetch.mjs
402
+ init_heyspex_home();
403
+ init_credentials();
404
+ import { readFileSync as readFileSync6 } from "node:fs";
405
+ import { homedir as homedir6 } from "node:os";
406
+ import { join as join6 } from "node:path";
407
+
408
+ // vendor/heyspex-mcp/session-owner.mjs
409
+ init_heyspex_home();
410
+ init_credentials();
411
+ import { readFileSync as readFileSync2 } from "node:fs";
412
+ import { homedir as homedir3 } from "node:os";
413
+ import { join as join3 } from "node:path";
414
+ function resolveSessionOwnerUncached({ env = process.env, homeDir = heyspexHome(), readFile } = {}) {
415
+ const fromEnv = typeof env?.HS_ACCOUNT_ID === "string" ? env.HS_ACCOUNT_ID.trim() : "";
416
+ if (fromEnv) return fromEnv;
417
+ return readCurrentAccountId({ homeDir, readFile });
418
+ }
419
+
420
+ // vendor/heyspex-mcp/vault-less-endpoints.mjs
421
+ var VAULT_LESS_ENDPOINTS = Object.freeze([
422
+ "* /api/mcp/session",
423
+ "* /api/mcp/pairing-codes",
424
+ "POST /api/vaults",
425
+ "GET /api/vaults",
426
+ "POST /api/workspaces",
427
+ "GET /api/workspaces",
428
+ "* /api/admin/dev-mode"
429
+ ]);
430
+ function pathnameOf(path3) {
431
+ const raw = String(path3 ?? "").split("?")[0].split("#")[0];
432
+ const noTrailing = raw.length > 1 ? raw.replace(/\/+$/, "") : raw;
433
+ return noTrailing || "/";
434
+ }
435
+ function isVaultLessEndpoint(method, path3) {
436
+ const m = String(method || "GET").toUpperCase();
437
+ const pathname = pathnameOf(path3);
438
+ return VAULT_LESS_ENDPOINTS.some((row) => {
439
+ const [rowMethod, rowPath] = row.split(" ");
440
+ return rowPath === pathname && (rowMethod === "*" || rowMethod === m);
441
+ });
442
+ }
443
+
444
+ // vendor/heyspex-mcp/write-outbox.mjs
445
+ import {
446
+ readdirSync,
447
+ readFileSync as readFileSync3,
448
+ writeFileSync as writeFileSync2,
449
+ mkdirSync as mkdirSync2,
450
+ existsSync as existsSync2,
451
+ renameSync as renameSync2,
452
+ rmSync
453
+ } from "node:fs";
454
+ import { basename, join as join4, isAbsolute as isAbsolute2 } from "node:path";
455
+
456
+ // scripts/lib/shared-path-helpers.mjs
457
+ import { realpathSync } from "node:fs";
458
+ import { isAbsolute, resolve as resolve2, sep } from "node:path";
459
+ import { fileURLToPath as fileURLToPath2 } from "node:url";
460
+
461
+ // vendor/heyspex-mcp/vault-resolve.mjs
462
+ init_heyspex_home();
463
+ import { existsSync as existsSync4, readFileSync as readFileSync5, realpathSync as realpathSync3, statSync as statSync3 } from "node:fs";
464
+ import { homedir as homedir5, tmpdir } from "node:os";
465
+
466
+ // vendor/heyspex-mcp/usable-dir-env.mjs
467
+ import { statSync } from "node:fs";
468
+ import { isAbsolute as isAbsolute3 } from "node:path";
469
+
470
+ // vendor/heyspex-mcp/vault-resolve.mjs
471
+ import { basename as basename2, dirname as dirname3, join as join5, resolve as resolve3 } from "node:path";
472
+
473
+ // vendor/heyspex-mcp/vault-binding.mjs
474
+ import {
475
+ copyFileSync,
476
+ existsSync as existsSync3,
477
+ mkdirSync as mkdirSync3,
478
+ readFileSync as readFileSync4,
479
+ realpathSync as realpathSync2,
480
+ renameSync as renameSync3,
481
+ statSync as statSync2,
482
+ unlinkSync,
483
+ writeFileSync as writeFileSync3
484
+ } from "node:fs";
485
+ import { homedir as homedir4 } from "node:os";
486
+ import path from "node:path";
487
+
488
+ // vendor/heyspex-mcp/console-fetch.mjs
489
+ var VAULT_NOT_REGISTERED = "VAULT_NOT_REGISTERED";
490
+ var VaultNotRegisteredError = class extends Error {
491
+ /**
492
+ * @param {string | null | undefined} repoRootAbs the root the resolver settled on
493
+ * @param {string | null | undefined} source the resolver tier that produced it
494
+ * @param {{ reason?: 'unregistered' | 'legacy-id', legacyId?: string | null }} [detail]
495
+ */
496
+ constructor(repoRootAbs, source, { reason = "unregistered", legacyId = null } = {}) {
497
+ const root = typeof repoRootAbs === "string" && repoRootAbs.trim() ? repoRootAbs.trim() : null;
498
+ const where = root ? ` (${root})` : "";
499
+ const why = reason === "legacy-id" ? `This repo${where} is registered on this machine under a legacy id (\`${legacyId}\`), not the vault id the console knows, and its heyspex.yaml carries no \`vault_id\` to stand in for it, ` : `This repo${where} is not connected to a HeySpex vault on this machine (\`hs connect\` has not registered it), `;
500
+ super(
501
+ why + `so the console call was refused before it was sent. Run \`${hsCommand("connect")}\`${root ? ` in ${root}` : ""} to ${reason === "legacy-id" ? "re-register" : "connect"} it, then try again. Nothing was sent.`
502
+ );
503
+ this.name = "VaultNotRegisteredError";
504
+ this.code = VAULT_NOT_REGISTERED;
505
+ this.reason = reason;
506
+ this.repoRootAbs = root;
507
+ this.source = typeof source === "string" ? source : null;
508
+ this.legacyId = legacyId;
509
+ }
510
+ };
511
+ var CLOUD_VAULT_ID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
512
+ function isCloudVaultId(value) {
513
+ return typeof value === "string" && CLOUD_VAULT_ID_RE.test(value.trim());
514
+ }
515
+ var MARKER_VAULT_ID_RE = /^vault_id:[ \t]*["']?([^"'\r\n#]+)/m;
516
+ function readMarkerVaultId(repoRootAbs, { readFile } = {}) {
517
+ if (typeof repoRootAbs !== "string" || !repoRootAbs.trim()) return null;
518
+ const read = readFile ?? ((p) => readFileSync6(p, "utf8"));
519
+ try {
520
+ const raw = read(join6(repoRootAbs, "heyspex.yaml"));
521
+ const value = String(raw ?? "").match(MARKER_VAULT_ID_RE)?.[1]?.trim() ?? "";
522
+ return value || null;
523
+ } catch {
524
+ return null;
525
+ }
526
+ }
527
+ function cloudVaultIdFor(resolved, { readMarkerVaultId: readMarker = readMarkerVaultId } = {}) {
528
+ const rowId = resolved.vaultId.trim();
529
+ if (isCloudVaultId(rowId)) return rowId;
530
+ const marker = readMarker(resolved.repoRootAbs);
531
+ if (isCloudVaultId(marker)) return marker.trim();
532
+ throw new VaultNotRegisteredError(resolved.repoRootAbs, resolved.source, { reason: "legacy-id", legacyId: rowId });
533
+ }
534
+ var SCOPE_REFUSAL_CODES = Object.freeze(["VAULT_SCOPE_MISMATCH", "ACCOUNT_MISMATCH", "VAULT_NOT_REGISTERED"]);
535
+
536
+ // spex-console/src/lib/cli/control-shared.ts
537
+ init_credentials();
538
+
539
+ // spex-console/src/lib/vault-registry/registry-file.ts
540
+ import { existsSync as existsSync5, mkdirSync as mkdirSync4, readFileSync as readFileSync7, renameSync as renameSync4, writeFileSync as writeFileSync4 } from "node:fs";
541
+ import { dirname as dirname4, join as join7, resolve as resolve4 } from "node:path";
542
+
543
+ // spex-console/src/lib/heyspex-home.ts
544
+ import { homedir as homedir7 } from "node:os";
545
+ function heyspexHome2(env = process.env) {
546
+ const override = env?.HEYSPEX_HOME;
547
+ if (typeof override === "string" && override.trim().length > 0) {
548
+ return override.trim();
549
+ }
550
+ return homedir7();
551
+ }
552
+
553
+ // spex-console/src/lib/vault-registry/types.ts
554
+ var LEGACY_REGISTRY_ACCOUNT_ID = "legacy";
555
+ var VAULT_REGISTRY_SCHEMA_VERSION = 2;
556
+ function emptyVaultRegistry() {
557
+ return {
558
+ schema_version: VAULT_REGISTRY_SCHEMA_VERSION,
559
+ current_account_id: null,
560
+ accounts: {},
561
+ workspaces: [],
562
+ vaults: []
563
+ };
564
+ }
565
+
566
+ // spex-console/src/lib/vault-registry/registry-file.ts
567
+ function vaultRegistryDir(opts = {}) {
568
+ return opts.registryDir ?? join7(heyspexHome2(), ".heyspex");
569
+ }
570
+ function vaultRegistryFilePath(opts = {}) {
571
+ return join7(vaultRegistryDir(opts), "registry.json");
572
+ }
573
+ function isNonEmptyString(v) {
574
+ return typeof v === "string" && v.trim().length > 0;
575
+ }
576
+ function accountOf(o) {
577
+ return isNonEmptyString(o.account_id) ? o.account_id : LEGACY_REGISTRY_ACCOUNT_ID;
578
+ }
579
+ function normalizeWorkspace(raw) {
580
+ if (!raw || typeof raw !== "object") return null;
581
+ const o = raw;
582
+ if (!isNonEmptyString(o.id)) return null;
583
+ if (!isNonEmptyString(o.display_name)) return null;
584
+ if (!isNonEmptyString(o.created_at)) return null;
585
+ const record = {
586
+ id: o.id,
587
+ display_name: o.display_name,
588
+ created_at: o.created_at,
589
+ archived: o.archived === true,
590
+ account_id: accountOf(o)
591
+ };
592
+ if (isNonEmptyString(o.github_org)) record.github_org = o.github_org;
593
+ return record;
594
+ }
595
+ function normalizeVault(raw) {
596
+ if (!raw || typeof raw !== "object") return null;
597
+ const o = raw;
598
+ if (!isNonEmptyString(o.id)) return null;
599
+ if (!isNonEmptyString(o.workspace_id)) return null;
600
+ if (!isNonEmptyString(o.display_name)) return null;
601
+ if (!isNonEmptyString(o.repo_root_abs)) return null;
602
+ if (typeof o.schema_version !== "number") return null;
603
+ if (!isNonEmptyString(o.registered_at)) return null;
604
+ const record = {
605
+ id: o.id,
606
+ workspace_id: o.workspace_id,
607
+ display_name: o.display_name,
608
+ repo_root_abs: o.repo_root_abs,
609
+ schema_version: o.schema_version,
610
+ registered_at: o.registered_at,
611
+ archived: o.archived === true,
612
+ account_id: accountOf(o)
613
+ };
614
+ if (isNonEmptyString(o.github_remote)) record.github_remote = o.github_remote;
615
+ return record;
616
+ }
617
+ function normalizeStoredPointers(raw) {
618
+ const o = raw && typeof raw === "object" ? raw : {};
619
+ const out = {};
620
+ for (const field of ["active_vault_id", "active_workspace_id"]) {
621
+ const v = o[field];
622
+ if (v === null || isNonEmptyString(v)) out[field] = v;
623
+ }
624
+ return out;
625
+ }
626
+ function normalizePointers(raw) {
627
+ const o = raw && typeof raw === "object" ? raw : {};
628
+ return {
629
+ active_vault_id: isNonEmptyString(o.active_vault_id) ? o.active_vault_id : null,
630
+ active_workspace_id: isNonEmptyString(o.active_workspace_id) ? o.active_workspace_id : null
631
+ };
632
+ }
633
+ function registryFileSchemaOf(json) {
634
+ if (!json || typeof json !== "object" || Array.isArray(json)) return null;
635
+ return json.schema_version === 2 ? 2 : 1;
636
+ }
637
+ function parseVaultRegistryFile(json) {
638
+ const schema = registryFileSchemaOf(json);
639
+ if (schema === null) return emptyVaultRegistry();
640
+ const o = json;
641
+ const workspacesRaw = Array.isArray(o.workspaces) ? o.workspaces : [];
642
+ const workspaces = workspacesRaw.map(normalizeWorkspace).filter((w) => w !== null);
643
+ const vaultsRaw = Array.isArray(o.vaults) ? o.vaults : [];
644
+ const vaults = vaultsRaw.map(normalizeVault).filter((v) => v !== null);
645
+ if (schema === 1) {
646
+ const legacy = normalizePointers(o);
647
+ const hasLegacyState = legacy.active_vault_id !== null || legacy.active_workspace_id !== null || workspaces.length > 0 || vaults.length > 0;
648
+ return {
649
+ schema_version: VAULT_REGISTRY_SCHEMA_VERSION,
650
+ current_account_id: hasLegacyState ? LEGACY_REGISTRY_ACCOUNT_ID : null,
651
+ accounts: hasLegacyState ? { [LEGACY_REGISTRY_ACCOUNT_ID]: legacy } : {},
652
+ workspaces,
653
+ vaults
654
+ };
655
+ }
656
+ const accounts = {};
657
+ if (o.accounts && typeof o.accounts === "object" && !Array.isArray(o.accounts)) {
658
+ for (const [id, pointers] of Object.entries(o.accounts)) {
659
+ if (isNonEmptyString(id)) accounts[id] = normalizeStoredPointers(pointers);
660
+ }
661
+ }
662
+ return {
663
+ schema_version: VAULT_REGISTRY_SCHEMA_VERSION,
664
+ current_account_id: isNonEmptyString(o.current_account_id) ? o.current_account_id : null,
665
+ accounts,
666
+ workspaces,
667
+ vaults
668
+ };
669
+ }
670
+ function readRegistryJson(opts) {
671
+ const filePath = vaultRegistryFilePath(opts);
672
+ if (!existsSync5(filePath)) return { kind: "missing" };
673
+ try {
674
+ const raw = readFileSync7(filePath, "utf8");
675
+ const json = JSON.parse(raw);
676
+ if (registryFileSchemaOf(json) === null) return { kind: "unreadable" };
677
+ return { kind: "ok", raw, json };
678
+ } catch {
679
+ return { kind: "unreadable" };
680
+ }
681
+ }
682
+ function inspectVaultRegistry(opts = {}) {
683
+ const read = readRegistryJson(opts);
684
+ if (read.kind !== "ok") {
685
+ return {
686
+ registry: emptyVaultRegistry(),
687
+ file_schema_version: null,
688
+ registry_unreadable: read.kind === "unreadable"
689
+ };
690
+ }
691
+ return {
692
+ registry: parseVaultRegistryFile(read.json),
693
+ file_schema_version: registryFileSchemaOf(read.json),
694
+ registry_unreadable: false
695
+ };
696
+ }
697
+ function loadVaultRegistry(opts = {}) {
698
+ return inspectVaultRegistry(opts).registry;
699
+ }
700
+
701
+ // spex-console/src/lib/cli/control-shared.ts
702
+ var EXIT = {
703
+ /** Success. */
704
+ OK: 0,
705
+ /**
706
+ * A read-only CHECK ran correctly and its answer is "no" — `hs connect
707
+ * --check` on an unconnected repo. Distinct from {@link EXIT.USAGE} so a
708
+ * script can tell "you typed the command wrong" from "the command worked,
709
+ * the repo is not connected". Both were `1` until 2026-09-18, which made
710
+ * `--check` unusable in a conditional: every caller had to parse stdout to
711
+ * find out which of the two had happened.
712
+ */
713
+ CHECK_FAILED: 2,
714
+ /** Usage error — unknown subcommand/flag, or a missing required argument. */
715
+ USAGE: 1,
716
+ /** Claim race lost — the spec is already claimed by another agent (409). */
717
+ CONFLICT: 9,
718
+ /** Target not found — release of an unheld claim, etc. (404). */
719
+ NOT_FOUND: 10,
720
+ /** Invalid argument — bad `--ttl`, unknown `--kind`, unparseable date. */
721
+ INVALID_ARG: 11,
722
+ /** SpexConsole unreachable (network error reaching the dev server). */
723
+ CONSOLE_UNREACHABLE: 12,
724
+ /** Server failure — a non-OK response that isn't 400/402/404/409. */
725
+ WRITE_FAILED: 13,
726
+ /** Neon control plane absent — the endpoint returned 402 (upgrade required). */
727
+ NEON_REQUIRED: 14,
728
+ /**
729
+ * A cross-account `--account <id>` override was refused — the caller is
730
+ * not a builder/admin (`resolveScopedAccountId`'s `forbidden_account_override`,
731
+ * [[HS-X3GB-EKLJ-TASK-hs-cli-account-flag-for-cross-account]]). Distinct
732
+ * from `USAGE`/`INVALID_ARG` — this is an authorization refusal, not a
733
+ * malformed command.
734
+ */
735
+ FORBIDDEN: 15,
736
+ /**
737
+ * A CLOUD write refused because no account id could be resolved (no
738
+ * `HS_ACCOUNT_ID` env var, no `account_id:` in `~/.heyspex/config.yaml`) --
739
+ * `resolveCloudAccountIdOrRefuse` in `vault-workspace-shared.ts`
740
+ * ([[HS-YU1S-UAGY-TASK-cloud-writes-refuse-without-a]]). Distinct from
741
+ * `FORBIDDEN` -- this is "not signed in at all", not "signed in but not
742
+ * authorized for this override".
743
+ */
744
+ SIGN_IN_REQUIRED: 16,
745
+ /**
746
+ * Signed in, but the account a repo would be attached to was not confirmed:
747
+ * a non-interactive install/connect with no matching `--account` / `--yes`,
748
+ * an `account_id` that disagrees with the bearer's user, or the human
749
+ * aborting the identity prompt (`install-identity.ts`,
750
+ * [[HS-HAQI-KODY-DIAG-hs-install-and-hs-connect-bind-a-new]]).
751
+ */
752
+ IDENTITY_UNCONFIRMED: 17,
753
+ /**
754
+ * The repo this verb runs in is not registered on this machine (no
755
+ * `~/.heyspex/registry.json` row for its root, or a legacy slug row with no
756
+ * cloud `vault_id` in its heyspex.yaml), so the call was refused LOCALLY —
757
+ * nothing was sent. Names `hs connect`
758
+ * ([[HS-MVG4-WW9R-TASK-mcp-and-hs-name-account-vault-on-every]]).
759
+ */
760
+ VAULT_NOT_REGISTERED: 18,
761
+ /**
762
+ * The local half of a setup verb (`hs connect`, `hs vault *`, `hs workspace
763
+ * *`) was written but the CLOUD half did not land -- the console refused
764
+ * it or was never reached. The verb's receipt names the failed half and the
765
+ * exact command to re-run
766
+ * ([[HS-STCY-WW9R-TASK-add-a-shared-receipt-that-reads-back]], invariant 6).
767
+ * Distinct from {@link EXIT.WRITE_FAILED} so a script can tell "re-run to
768
+ * sync the cloud" from a generic server failure.
769
+ */
770
+ CLOUD_WRITE_FAILED: 19,
771
+ /**
772
+ * The repo's committed `heyspex.yaml` records `account: <A>` but the machine
773
+ * is signed in as another account, so `hs connect` refused and wrote
774
+ * nothing (marker, registry, cloud). Repair: `hs login --switch-account <A>`
775
+ * ([[HS-VWUD-WW9R-TASK-record-the-connecting-account-in]], invariant 4).
776
+ */
777
+ ACCOUNT_MISMATCH: 20,
778
+ /**
779
+ * Both sides of a setup verb's receipt were read back and still DISAGREE
780
+ * after a short bounded re-read of the cloud side (a briefly stale read is
781
+ * absorbed first). The write may well have landed; what the verb cannot
782
+ * claim is that this machine and the console now say the same thing. The
783
+ * receipt prints the diffs and the command to re-run
784
+ * ([[HS-FKSR-WW9R-TASK-make-one-hs-connect-run-create-a-cloud]], invariant 6).
785
+ * Distinct from {@link EXIT.CLOUD_WRITE_FAILED} (a write that did not land).
786
+ */
787
+ RECEIPT_DISAGREE: 21
788
+ };
789
+ var VAULT_ID_HEADER = "x-heyspex-vault-id";
790
+ var ACCOUNT_ID_HEADER = "x-heyspex-account-id";
791
+ function readPairedBearer(opts = {}) {
792
+ return readBearer(opts);
793
+ }
794
+ function readPairedToken(opts = {}) {
795
+ return readPairedBearer(opts).token ?? void 0;
796
+ }
797
+ function bearerHeaderFor(consoleUrl, opts = {}) {
798
+ try {
799
+ const host = new URL(consoleUrl).hostname;
800
+ if (host === "localhost" || host === "127.0.0.1" || host === "0.0.0.0" || host === "::1") {
801
+ return {};
802
+ }
803
+ const token = readPairedToken(opts);
804
+ return token ? { authorization: `Bearer ${token}` } : {};
805
+ } catch {
806
+ return {};
807
+ }
808
+ }
809
+ function scopeHeadersFor(scope, method, path3) {
810
+ if (scope === "none") {
811
+ if (!isVaultLessEndpoint(method, path3)) {
812
+ throw new Error(
813
+ `controlRequest: scope "none" is only allowed for the vault-less endpoints (${VAULT_LESS_ENDPOINTS.join(", ")}); ${method.toUpperCase()} ${path3} is vault-scoped and must name { accountId, vaultId }.`
814
+ );
815
+ }
816
+ return {};
817
+ }
818
+ return { [VAULT_ID_HEADER]: scope.vaultId, [ACCOUNT_ID_HEADER]: scope.accountId };
819
+ }
820
+ async function controlRequest(deps, req) {
821
+ const url = `${deps.consoleUrl}${req.path}`;
822
+ const scopeHeaders = scopeHeadersFor(deps.scope, req.method, req.path);
823
+ let res;
824
+ try {
825
+ res = await deps.fetchImpl(url, {
826
+ method: req.method,
827
+ headers: {
828
+ "content-type": "application/json",
829
+ // A remote console (cloud vault write mode, [[HS-PH7O-ILNC-DIAG-hs-check-report-cannot-authenticate]])
830
+ // authenticates every control call; loopback never asked, so the header
831
+ // is attached only when the target is non-loopback and a paired token
832
+ // exists. Same `hsmcp_` bearer the MCP server presents.
833
+ ...bearerHeaderFor(deps.consoleUrl),
834
+ // Invariant 2: name the vault and the account on every call, or the
835
+ // console serves whatever its leftover pointer says.
836
+ ...scopeHeaders
837
+ },
838
+ ...req.body !== void 0 ? { body: JSON.stringify(req.body) } : {}
839
+ });
840
+ } catch (err) {
841
+ const message = err instanceof Error ? err.message : String(err);
842
+ return {
843
+ ok: false,
844
+ error: {
845
+ code: EXIT.CONSOLE_UNREACHABLE,
846
+ message: `could not reach console at ${deps.consoleUrl}: ${message}
847
+ Start the dev server or set HEYSPEX_CONSOLE_URL.`,
848
+ payload: {
849
+ ok: false,
850
+ error: "console_unreachable",
851
+ url: deps.consoleUrl,
852
+ message
853
+ },
854
+ isError: true
855
+ }
856
+ };
857
+ }
858
+ let parsed = null;
859
+ try {
860
+ parsed = await res.json();
861
+ } catch {
862
+ }
863
+ if (res.status === 402) {
864
+ return {
865
+ ok: false,
866
+ error: {
867
+ code: EXIT.NEON_REQUIRED,
868
+ message: "this command requires the Neon control plane, which is not configured (console returned 402).",
869
+ payload: {
870
+ ok: false,
871
+ error: "neon_required",
872
+ status: 402,
873
+ console_response: parsed
874
+ },
875
+ isError: true
876
+ }
877
+ };
878
+ }
879
+ return { ok: true, status: res.status, body: parsed };
880
+ }
881
+ function envelopeMessage(body) {
882
+ if (body && typeof body === "object" && "message" in body) {
883
+ const m = body.message;
884
+ if (typeof m === "string") return m;
885
+ }
886
+ return "";
887
+ }
888
+ function writeFailed(status, body) {
889
+ const detail = envelopeMessage(body);
890
+ return {
891
+ code: EXIT.WRITE_FAILED,
892
+ message: `console returned ${status}${detail ? `: ${detail}` : ""}`,
893
+ payload: { ok: false, error: "write_failed", status, console_response: body },
894
+ isError: true
895
+ };
896
+ }
897
+ function normalizeRoot(p) {
898
+ return path2.resolve(p).replace(/[\\/]+/g, "/").replace(/\/+$/, "").toLowerCase();
899
+ }
900
+ function resolveControlScope(input) {
901
+ const target = normalizeRoot(input.vaultRoot);
902
+ const row = input.vaults.find((v) => normalizeRoot(v.repo_root_abs) === target) ?? null;
903
+ if (!row) {
904
+ return {
905
+ ok: false,
906
+ error: {
907
+ code: EXIT.VAULT_NOT_REGISTERED,
908
+ message: `this repo (${input.vaultRoot}) is not connected to a HeySpex vault on this machine (\`hs connect\` has not registered it), so the call was refused before it was sent. Run \`${hsCommand("connect")}\` in ${input.vaultRoot}, then try again.`,
909
+ payload: {
910
+ ok: false,
911
+ error: "vault_not_registered",
912
+ reason: "unregistered",
913
+ vault_root: input.vaultRoot
914
+ },
915
+ isError: true
916
+ }
917
+ };
918
+ }
919
+ let vaultId;
920
+ try {
921
+ vaultId = cloudVaultIdFor(
922
+ { vaultId: row.id, repoRootAbs: row.repo_root_abs, source: "registry" },
923
+ input.readMarkerVaultId ? { readMarkerVaultId: input.readMarkerVaultId } : {}
924
+ );
925
+ } catch (err) {
926
+ if (!(err instanceof VaultNotRegisteredError)) throw err;
927
+ const refused = err;
928
+ return {
929
+ ok: false,
930
+ error: {
931
+ code: EXIT.VAULT_NOT_REGISTERED,
932
+ message: refused.message,
933
+ payload: {
934
+ ok: false,
935
+ error: "vault_not_registered",
936
+ reason: refused.reason ?? "legacy-id",
937
+ vault_root: input.vaultRoot,
938
+ legacy_id: refused.legacyId ?? row.id
939
+ },
940
+ isError: true
941
+ }
942
+ };
943
+ }
944
+ const accountId = input.accountId?.trim() ?? "";
945
+ if (!accountId) {
946
+ return {
947
+ ok: false,
948
+ error: {
949
+ code: EXIT.SIGN_IN_REQUIRED,
950
+ message: `no HeySpex account is configured on this machine (\`hs login\` has not been run), so the call was refused before it was sent. Run \`${hsCommand("login")}\` on this machine, then try again.`,
951
+ payload: { ok: false, error: "sign_in_required", vault_root: input.vaultRoot },
952
+ isError: true
953
+ }
954
+ };
955
+ }
956
+ return { ok: true, scope: { accountId, vaultId } };
957
+ }
958
+ function resolveControlScopeFromMachine(vaultRoot, opts = {}) {
959
+ const registry = loadVaultRegistry(opts.registry);
960
+ const accountId = resolveSessionOwnerUncached({ env: opts.env, homeDir: opts.homeDir });
961
+ return resolveControlScope({ vaultRoot, accountId, vaults: registry.vaults });
962
+ }
963
+
964
+ // scripts/lib/console-url-resolver.mjs
965
+ init_heyspex_home();
966
+ init_dev_mode();
967
+ import { readFileSync as readFileSync9 } from "node:fs";
968
+ import { homedir as homedir9 } from "node:os";
969
+ import { join as join9 } from "node:path";
970
+ var DEFAULT_CONSOLE_URL = LOCAL_CONSOLE_URL;
971
+ function trimTrailingSlash(url) {
972
+ return url.replace(/\/+$/, "");
973
+ }
974
+ var LOOPBACK_HOSTS = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "::1", "[::1]", "0.0.0.0"]);
975
+ function isLoopbackConsoleUrl(url) {
976
+ if (typeof url !== "string" || !url) return false;
977
+ try {
978
+ return LOOPBACK_HOSTS.has(new URL(url).hostname.toLowerCase());
979
+ } catch {
980
+ return false;
981
+ }
982
+ }
983
+ var staleLoopbackCredentialsWarned = false;
984
+ function warnStaleLoopbackCredentialsOnce(credentialsUrl, derivedUrl, { log }) {
985
+ if (staleLoopbackCredentialsWarned) return;
986
+ staleLoopbackCredentialsWarned = true;
987
+ try {
988
+ log(
989
+ `[heyspex] ignoring console_url ${credentialsUrl} from ~/.heyspex/credentials.yaml: the resolved reliance is cloud, and a loopback console cannot serve it \u2014 using ${derivedUrl} (set HEYSPEX_CONSOLE_URL to override explicitly, or run \`hs dev mode:local\` to go back to a local console).
990
+ `
991
+ );
992
+ } catch {
993
+ }
994
+ }
995
+ function readCredentialsConsoleUrl({ homeDir = heyspexHome(), readFile = defaultReadFile2 } = {}) {
996
+ try {
997
+ const raw = readFile(join9(homeDir, ".heyspex", "credentials.yaml"));
998
+ const match = String(raw ?? "").match(/^console_url:\s*["']?([^"'\r\n#]+)/m);
999
+ const value = match?.[1]?.trim();
1000
+ return value && value.length > 0 ? trimTrailingSlash(value) : null;
1001
+ } catch {
1002
+ return null;
1003
+ }
1004
+ }
1005
+ function defaultReadFile2(p) {
1006
+ return readFileSync9(p, "utf8");
1007
+ }
1008
+ function readDiscoveryFileConsoleUrl(repoRoot, { readDiscovery: readDiscovery2 = defaultReadFile2 } = {}) {
1009
+ if (!repoRoot) return null;
1010
+ try {
1011
+ const discovery = String(readDiscovery2(join9(repoRoot, ".heyspex", "console-url")) ?? "").trim();
1012
+ if (discovery.length > 0) return trimTrailingSlash(discovery);
1013
+ } catch {
1014
+ }
1015
+ return null;
1016
+ }
1017
+ var ConsoleUrlUnresolvedError = class extends Error {
1018
+ constructor() {
1019
+ super(
1020
+ "resolveConsoleUrl: local reliance with no HEYSPEX_CONSOLE_URL env, no console_url: in ~/.heyspex/credentials.yaml, and no .heyspex/console-url discovery file \u2014 refusing to silently fall back to the localhost:3000 default in strict mode."
1021
+ );
1022
+ this.name = "ConsoleUrlUnresolvedError";
1023
+ }
1024
+ };
1025
+ function resolveModeEnvelope({ env, homeDir }) {
1026
+ try {
1027
+ const resolved = resolveDevMode({ env, homeDir });
1028
+ return {
1029
+ reliance: resolved.reliance === "cloud" ? "cloud" : "local",
1030
+ consoleUrl: resolved.consoleUrl || DEFAULT_CONSOLE_URL
1031
+ };
1032
+ } catch {
1033
+ return { reliance: "local", consoleUrl: DEFAULT_CONSOLE_URL };
1034
+ }
1035
+ }
1036
+ function resolveConsoleUrl({
1037
+ repoRoot,
1038
+ env = process.env,
1039
+ homeDir = heyspexHome(),
1040
+ strict = false,
1041
+ devMode,
1042
+ log = (line) => process.stderr.write(line),
1043
+ readFile = defaultReadFile2,
1044
+ readDiscovery: readDiscovery2 = defaultReadFile2
1045
+ } = {}) {
1046
+ const envUrl = env.HEYSPEX_CONSOLE_URL?.trim();
1047
+ if (envUrl) return trimTrailingSlash(envUrl);
1048
+ const mode = devMode ?? resolveModeEnvelope({ env, homeDir });
1049
+ const cloud = mode.reliance === "cloud";
1050
+ const derivedUrl = trimTrailingSlash(
1051
+ cloud && isLoopbackConsoleUrl(mode.consoleUrl) ? CLOUD_CONSOLE_URL : mode.consoleUrl || DEFAULT_CONSOLE_URL
1052
+ );
1053
+ const credentialsUrl = readCredentialsConsoleUrl({ homeDir, readFile });
1054
+ if (credentialsUrl) {
1055
+ if (cloud && isLoopbackConsoleUrl(credentialsUrl)) {
1056
+ warnStaleLoopbackCredentialsOnce(credentialsUrl, derivedUrl, { log });
1057
+ } else {
1058
+ return credentialsUrl;
1059
+ }
1060
+ }
1061
+ if (!cloud) {
1062
+ const discoveryUrl = readDiscoveryFileConsoleUrl(repoRoot, { readDiscovery: readDiscovery2 });
1063
+ if (discoveryUrl) return discoveryUrl;
1064
+ if (strict) throw new ConsoleUrlUnresolvedError();
1065
+ }
1066
+ return derivedUrl;
1067
+ }
1068
+
1069
+ // spex-console/src/lib/cli/defer.ts
1070
+ function resolveSpexConsoleUrl(vaultRoot, readDiscovery2, opts = {}) {
1071
+ return resolveConsoleUrl({
1072
+ repoRoot: vaultRoot,
1073
+ env: opts.env,
1074
+ homeDir: opts.homeDir,
1075
+ readFile: opts.readFile,
1076
+ readDiscovery: readDiscovery2,
1077
+ log: opts.log
1078
+ });
1079
+ }
1080
+ var HELP = [
1081
+ "Usage:",
1082
+ " hs defer <spec-id> [--until <when>] [--json] [--no-daemon]",
1083
+ " hs undefer <spec-id> [--json] [--no-daemon]",
1084
+ "",
1085
+ "Time-shift a spec via its `defer_until` frontmatter field. A deferred spec is",
1086
+ "scheduler-invisible until the date passes, then auto-revives.",
1087
+ "",
1088
+ "Arguments:",
1089
+ " <spec-id> The spec to defer/undefer (e.g. HS-TASK-EWL5).",
1090
+ "",
1091
+ "Flags:",
1092
+ " --until <when> ISO date, or relative offset +Nd / +Nw / +Nm / +Ny.",
1093
+ " Defaults to +1y (matches the console Defer button).",
1094
+ " Ignored by `undefer` (which always clears the field).",
1095
+ " --json Emit a machine-readable JSON result.",
1096
+ " --no-daemon Write straight to the vault (no console needed; CI path).",
1097
+ " --help, -h Show this help.",
1098
+ "",
1099
+ "Exit codes: 0 ok \xB7 1 usage \xB7 10 not-found \xB7 11 bad-arg \xB7 12 console-unreachable \xB7 13 write-failed"
1100
+ ].join("\n");
1101
+
1102
+ // spex-console/src/lib/launch-model-aliases.ts
1103
+ var FAMILIES = [
1104
+ { canonical: "claude-opus", family: "opus", cliNativeBareForm: "opus" },
1105
+ { canonical: "claude-sonnet", family: "sonnet", cliNativeBareForm: "sonnet" },
1106
+ { canonical: "claude-fable", family: "fable", cliNativeBareForm: "fable" },
1107
+ { canonical: "claude-haiku", family: "haiku", cliNativeBareForm: null }
1108
+ ];
1109
+ var ALIAS_LOOKUP = new Map(
1110
+ FAMILIES.flatMap((entry) => [[entry.canonical, entry], [entry.family, entry]])
1111
+ );
1112
+ var MODEL_ALIAS_INPUT_FORMS = Array.from(ALIAS_LOOKUP.keys());
1113
+ function normalizeModelAlias(value) {
1114
+ const key = value.trim().toLowerCase();
1115
+ if (!key) return null;
1116
+ return ALIAS_LOOKUP.get(key) ?? null;
1117
+ }
1118
+ function isModelAliasValue(value) {
1119
+ return normalizeModelAlias(value) !== null;
1120
+ }
1121
+ function canonicalizeModelAlias(value) {
1122
+ return normalizeModelAlias(value)?.canonical ?? null;
1123
+ }
1124
+
1125
+ // dev-factory/model-catalog.json
1126
+ var model_catalog_default = {
1127
+ _comment: "SINGLE SOURCE OF TRUTH for model ids, aliases, retirements, context windows, capabilities and list prices \u2014 every cloud provider the fleet can call (DeepSeek, Anthropic, OpenAI, Gemini, OpenRouter, GitHub Models) AND every local runtime (Ollama, vLLM). Hand-edit THIS file when a vendor ships or retires a model; never scatter a new literal id through the code. Readers: dev-factory/lib/model-catalog.mjs (typed accessors + self-test), dev-factory/cloud-clients.mjs (the whole PROVIDERS table is derived from `providerKeys` below), dev-factory/lib/fleet-manifest.mjs (context/VRAM/cost tables), spexharness/src/chat/compact.mjs (window sizes), spex-console/src/lib/model-catalog.ts (the console's ONLY import path \u2014 keep it so when dev-factory merges into SpexFleet one line moves), dev-factory/judge.mjs, dev-factory/router.mjs --self-test (refuses retired ids in routing-table.json), scripts/check-model-ids.mjs (refuses retired ids in live code). ROUTING (which seat runs which model, which machine hosts which local model) stays in routing-table.json \u2014 this file says what a model IS and how it is reached, not who uses it. UPGRADE RECIPE: (1) add the new id under `models` with its window/price; (2) move the old id into `aliases` (old \u2192 new) and `retired`; (3) repoint the `roles` / `providerKeys` you want moved; (4) run `node dev-factory/lib/model-catalog.mjs --self-test && node dev-factory/router.mjs --self-test && npm run check:model-ids`. `verifiedOn` per provider records when the prices were last checked against the vendor's sheet.",
1128
+ verified: "2026-09-18",
1129
+ cloud: {
1130
+ deepseek: {
1131
+ kind: "openai",
1132
+ company: "deepseek",
1133
+ baseUrl: "https://api.deepseek.com/v1",
1134
+ betaBaseUrl: "https://api.deepseek.com/beta",
1135
+ anthropicBaseUrl: "https://api.deepseek.com/anthropic",
1136
+ userIdField: "user_id",
1137
+ _userIdFieldNote: "Wire field for per-seat cache/scheduling isolation (DeepSeek rate-limit doc: user_id isolates KVCache and scheduling). cloud-clients.mjs sends opts.userId (defaulting to opts.seat) under this name only for providers that declare it; betaBaseUrl is used only when a caller opts into strict tool schemas (opts.strictTools) \u2014 DeepSeek's strict mode is served from /beta.",
1138
+ keyEnv: "HEYSPEX_DEEPSEEK_API_KEY",
1139
+ keyEnvFallback: "DEEPSEEK_API_KEY",
1140
+ source: "https://api-docs.deepseek.com/quick_start/pricing",
1141
+ verifiedOn: "2026-09-18",
1142
+ peakHoursUtc: { days: [1, 2, 3, 4, 5], ranges: [["01:00", "04:00"], ["06:00", "10:00"]], note: "Off-peak rates are half of peak; all other hours are off-peak (DeepSeek pricing note 3)." },
1143
+ roles: {
1144
+ cheap: "deepseek-flash",
1145
+ strong: "deepseek-v4-pro",
1146
+ chatDefault: "deepseek-flash",
1147
+ judge: "deepseek-flash",
1148
+ legacyJudge: "deepseek-chat"
1149
+ },
1150
+ models: {
1151
+ "deepseek-flash": {
1152
+ displayName: "DeepSeek-V4.1-Flash",
1153
+ released: "2026-09-10",
1154
+ contextWindow: 1048576,
1155
+ maxOutputTokens: 393216,
1156
+ tier: "cheap",
1157
+ capabilities: ["api", "metered", "tools", "thinking", "json", "vision", "strict-tools", "fim", "prefix"],
1158
+ thinkingDefault: true,
1159
+ effortLevels: ["low", "high", "max"],
1160
+ concurrencyLimit: 2500,
1161
+ pricePer1M: {
1162
+ peak: { cacheHit: 6e-3, cacheMiss: 0.3, output: 1.2 },
1163
+ offPeak: { cacheHit: 3e-3, cacheMiss: 0.15, output: 0.6 }
1164
+ }
1165
+ },
1166
+ "deepseek-v4-pro": {
1167
+ displayName: "DeepSeek-V4-Pro-0813",
1168
+ released: "2026-08-13",
1169
+ contextWindow: 1048576,
1170
+ maxOutputTokens: 393216,
1171
+ tier: "strong",
1172
+ capabilities: ["api", "metered", "tools", "thinking", "json", "strict-tools", "fim", "prefix", "max_reasoning"],
1173
+ thinkingDefault: true,
1174
+ effortLevels: ["low", "high", "max"],
1175
+ concurrencyLimit: 500,
1176
+ note: "Retirement announced 2026-09-10 was reversed; service continues past 2026-09-14 at unchanged billing (pricing note 2).",
1177
+ pricePer1M: {
1178
+ peak: { cacheHit: 0.044, cacheMiss: 1.32, output: 3.96 },
1179
+ offPeak: { cacheHit: 0.022, cacheMiss: 0.66, output: 1.98 }
1180
+ }
1181
+ }
1182
+ },
1183
+ aliases: {
1184
+ "deepseek-v4-flash": "deepseek-flash",
1185
+ "deepseek-v4-flash-vision-exp": "deepseek-flash",
1186
+ "deepseek-chat": "deepseek-flash"
1187
+ },
1188
+ retired: [
1189
+ { id: "deepseek-v4-flash", on: "2026-09-10", servedBy: "deepseek-flash", note: "Legacy name still accepted by the API and routed to V4.1 Flash at the Flash price 'temporarily'." },
1190
+ { id: "deepseek-v4-flash-vision-exp", on: "2026-09-10", servedBy: "deepseek-flash" }
1191
+ ],
1192
+ unlisted: {
1193
+ "deepseek-chat": "Not on the 2026-09-10 model sheet. Kept only as the held-constant LLM-judge alias (PROVIDERS key `deepseek`); priced as deepseek-flash. Verify with --liveness before relying on it."
1194
+ },
1195
+ emitAliasesAsProviderKeys: true,
1196
+ providerKeys: {
1197
+ "deepseek-flash": { model: "deepseek-flash" },
1198
+ "deepseek-v4-pro": { model: "deepseek-v4-pro" },
1199
+ deepseek: { model: "deepseek-chat", legacyAliasOf: "deepseek-flash", note: "Legacy alias, held-constant LLM judge. Sends `deepseek-chat` on the wire, priced as deepseek-flash." }
1200
+ }
1201
+ },
1202
+ anthropic: {
1203
+ kind: "anthropic",
1204
+ company: "anthropic",
1205
+ baseUrl: "https://api.anthropic.com/v1",
1206
+ keyEnv: "HEYSPEX_ANTHROPIC_API_KEY",
1207
+ source: "claude-api skill model table (cached 2026-06-24) \u2014 first-party API rates",
1208
+ verifiedOn: "2026-06-24",
1209
+ _note: "Anthropic models are excluded from the dev-factory council by construction and are the ceiling/reference arm in benches; the PROVIDERS keys `claude` / `claude-opus` still point at the 4.x generation on purpose (flip `providerKeys` to the 5-family when the owner rules on it \u2014 the ids and prices are already here). Claude Code CLI launches use the bare alias words in `cliAliases` (resolved by spex-console/src/lib/model-registry/launch-resolve.ts), not these ids.",
1210
+ roles: {
1211
+ default: "claude-opus-5",
1212
+ strong: "claude-opus-5",
1213
+ mid: "claude-sonnet-5",
1214
+ cheap: "claude-haiku-4-5",
1215
+ max: "claude-fable-5-1",
1216
+ previousStrong: "claude-opus-4-8",
1217
+ previousMid: "claude-sonnet-4-6"
1218
+ },
1219
+ cliAliases: { opus: "claude-opus-5", sonnet: "claude-sonnet-5", haiku: "claude-haiku-4-5", fable: "claude-fable-5-1" },
1220
+ models: {
1221
+ "claude-fable-5-1": { displayName: "Claude Fable 5.1", contextWindow: 1048576, maxOutputTokens: 131072, tier: "max", capabilities: ["api", "metered", "tools", "thinking", "vision", "json"], noTemp: true, thinkingAlwaysOn: true, pricePer1M: { input: 10, output: 50 } },
1222
+ "claude-opus-5": { displayName: "Claude Opus 5", contextWindow: 1048576, maxOutputTokens: 131072, tier: "strong", capabilities: ["api", "metered", "tools", "thinking", "vision", "json"], noTemp: true, pricePer1M: { input: 5, output: 25 } },
1223
+ "claude-sonnet-5": { displayName: "Claude Sonnet 5", contextWindow: 1048576, maxOutputTokens: 131072, tier: "strong", capabilities: ["api", "metered", "tools", "thinking", "vision", "json"], noTemp: true, pricePer1M: { input: 2, output: 10 } },
1224
+ "claude-haiku-4-5": { displayName: "Claude Haiku 4.5", contextWindow: 2e5, maxOutputTokens: 65536, tier: "cheap", capabilities: ["api", "metered", "tools", "thinking", "vision"], pricePer1M: { input: 1, output: 5 } },
1225
+ "claude-opus-4-8": { displayName: "Claude Opus 4.8", contextWindow: 1048576, maxOutputTokens: 131072, tier: "strong", capabilities: ["api", "metered", "tools", "thinking", "vision", "json"], noTemp: true, generation: "previous", pricePer1M: { input: 5, output: 25 } },
1226
+ "claude-sonnet-4-6": { displayName: "Claude Sonnet 4.6", contextWindow: 1048576, maxOutputTokens: 131072, tier: "strong", capabilities: ["api", "metered", "tools", "thinking", "vision", "json"], generation: "previous", pricePer1M: { input: 3, output: 15 } }
1227
+ },
1228
+ aliases: {},
1229
+ retired: [],
1230
+ providerKeys: {
1231
+ claude: { model: "claude-sonnet-4-6", note: "Generic Claude key \u2014 previous generation kept on purpose; see _note." },
1232
+ "claude-opus": { model: "claude-opus-4-8", noTemp: true, note: "Opus key \u2014 previous generation kept on purpose. Old table priced this at $15/$75; the sheet says $5/$25." }
1233
+ }
1234
+ },
1235
+ openai: {
1236
+ kind: "openai",
1237
+ company: "openai",
1238
+ baseUrl: "https://api.openai.com/v1",
1239
+ keyEnv: "HEYSPEX_OPENAI_API_KEY",
1240
+ source: "carried over from cloud-clients.mjs PROVIDERS (no vendor re-verification in this pass)",
1241
+ verifiedOn: "2026-06-28",
1242
+ roles: { default: "gpt-4o" },
1243
+ models: {
1244
+ "gpt-4o": { contextWindow: 128e3, tier: "strong", capabilities: ["api", "metered", "tools", "vision", "json"], pricePer1M: { input: 2.5, output: 10 } }
1245
+ },
1246
+ aliases: {},
1247
+ retired: [],
1248
+ providerKeys: { openai: { model: "gpt-4o" } }
1249
+ },
1250
+ gemini: {
1251
+ kind: "openai",
1252
+ company: "google",
1253
+ baseUrl: "https://generativelanguage.googleapis.com/v1beta/openai",
1254
+ keyEnv: "HEYSPEX_GEMINI_API_KEY",
1255
+ source: "carried over from cloud-clients.mjs PROVIDERS (no vendor re-verification in this pass)",
1256
+ verifiedOn: "2026-06-28",
1257
+ roles: { default: "gemini-2.5-pro", strong: "gemini-2.5-pro" },
1258
+ models: {
1259
+ "gemini-2.5-pro": { contextWindow: 1048576, tier: "strong", capabilities: ["api", "metered", "tools", "vision", "json", "thinking"], pricePer1M: { input: 1.25, output: 10 } }
1260
+ },
1261
+ aliases: {},
1262
+ retired: [],
1263
+ providerKeys: { gemini: { model: "gemini-2.5-pro" } }
1264
+ },
1265
+ openrouter: {
1266
+ kind: "openai",
1267
+ company: "openrouter",
1268
+ baseUrl: "https://openrouter.ai/api/v1",
1269
+ keyEnv: "OPENROUTER_API_KEY",
1270
+ source: "carried over from cloud-clients.mjs PROVIDERS (OpenRouter list prices as dated per model)",
1271
+ verifiedOn: "2026-07-10",
1272
+ _note: "Same-family fallbacks hosted via OpenRouter (council key resilience, 2026-07-08 dogfood audit): a customer fleet with ONE OpenRouter key can still seat all three decorrelated council families \u2014 the MODEL family is what decorrelates blind spots, not the API host. grok-4.3 is the decorrelated director-seat primary (owner directive 2026-07-10, HS-E1O0); glm-5.2 is the 'beat Opus cheaper' candidate (HS-XNJI), not yet wired into any tier.",
1273
+ roles: { cheap: "openai/gpt-4o-mini", director: "x-ai/grok-4.3", directorFallback: "z-ai/glm-5.2", geminiFallback: "google/gemini-2.5-pro", gpt4oFallback: "openai/gpt-4o" },
1274
+ models: {
1275
+ "openai/gpt-4o-mini": { contextWindow: 128e3, tier: "cheap", capabilities: ["api", "metered", "tools", "json"], pricePer1M: { input: 0.15, output: 0.6 } },
1276
+ "openai/gpt-4o": { contextWindow: 128e3, tier: "strong", capabilities: ["api", "metered", "tools", "vision", "json"], pricePer1M: { input: 2.5, output: 10 } },
1277
+ "google/gemini-2.5-pro": { contextWindow: 1048576, tier: "strong", capabilities: ["api", "metered", "tools", "vision", "json"], pricePer1M: { input: 1.25, output: 10 } },
1278
+ "z-ai/glm-5.2": { displayName: "GLM-5.2 (Z.ai, open-weight 744B)", contextWindow: 2e5, tier: "strong", capabilities: ["api", "metered", "tools", "json"], verifiedOn: "2026-06-30", pricePer1M: { input: 0.95, output: 3 } },
1279
+ "x-ai/grok-4.3": { displayName: "Grok 4.3 (xAI)", contextWindow: 256e3, tier: "strong", capabilities: ["api", "metered", "tools", "json", "thinking", "structured-outputs"], verifiedOn: "2026-07-10", pricePer1M: { input: 1.25, output: 2.5 } }
1280
+ },
1281
+ aliases: {},
1282
+ retired: [],
1283
+ providerKeys: {
1284
+ openrouter: { model: "openai/gpt-4o-mini" },
1285
+ "gemini-openrouter": { model: "google/gemini-2.5-pro" },
1286
+ "gpt4o-openrouter": { model: "openai/gpt-4o" },
1287
+ "glm-5.2": { model: "z-ai/glm-5.2" },
1288
+ "grok-4.3": { model: "x-ai/grok-4.3" }
1289
+ }
1290
+ },
1291
+ "github-models": {
1292
+ kind: "openai",
1293
+ company: "github",
1294
+ baseUrl: "https://models.github.ai/inference",
1295
+ keyEnv: "HEYSPEX_GITHUB_TOKEN",
1296
+ keyEnvFallback: "GITHUB_TOKEN",
1297
+ source: "GitHub Models bills via Copilot credits; gpt-4o list price mirrored for the ledger (estimate)",
1298
+ verifiedOn: "2026-06-28",
1299
+ _note: "Copilot-ecosystem credits as a cloud tier (HS-32OM fleet onboarding). Model ids are publisher-namespaced.",
1300
+ roles: { default: "openai/gpt-4o" },
1301
+ models: {
1302
+ "openai/gpt-4o": { contextWindow: 128e3, tier: "strong", capabilities: ["api", "metered", "tools", "vision", "json"], pricePer1M: { input: 2.5, output: 10 } }
1303
+ },
1304
+ aliases: {},
1305
+ retired: [],
1306
+ providerKeys: { "github-models": { model: "openai/gpt-4o" } }
1307
+ }
1308
+ },
1309
+ local: {
1310
+ ollama: {
1311
+ kind: "ollama",
1312
+ _comment: "Local fleet models. WHERE each fits (canHost) and WHO uses it (purposes) live in routing-table.json; per-machine speeds live in fleet-manifest.mjs STATIC_SPEEDS. Ids are Ollama tags exactly as `ollama list` prints them. Local models have no PROVIDERS entry \u2014 callModel treats an unknown id as an Ollama id.",
1313
+ roles: {
1314
+ builder: "gpt-oss:20b",
1315
+ coder: "qwen3-coder:30b",
1316
+ judgeFallback: "qwen3-coder:30b",
1317
+ vision: "qwen3.6:27b",
1318
+ small: "qwen3:8b"
1319
+ },
1320
+ models: {
1321
+ "gpt-oss:20b": { contextWindow: 131072, vramRequiredMb: 13800, architecture: "gptoss", capabilities: ["tools", "thinking"] },
1322
+ "qwen3-coder:30b": { contextWindow: 262144, vramRequiredMb: 18600, architecture: "qwen3moe", capabilities: ["tools"] },
1323
+ "qwen3.6:27b": { contextWindow: 262144, vramRequiredMb: 17400, architecture: "qwen35", capabilities: ["tools", "thinking", "vision"] },
1324
+ "qwen2.5-coder:32b": { contextWindow: 131072, vramRequiredMb: 19900, architecture: "qwen2", capabilities: ["tools", "insert"] },
1325
+ "qwen3:8b": { contextWindow: 32768, vramRequiredMb: 5200, architecture: "qwen3", capabilities: ["tools", "thinking"] },
1326
+ "deepseek-r1:70b": { contextWindow: 131072, vramRequiredMb: 42500, architecture: "deepseek", capabilities: ["tools", "thinking"], note: "Q2_K quant \u2014 lower quality than Q4_K_M." },
1327
+ "llama3.3:70b": { contextWindow: 131072, vramRequiredMb: 42500, architecture: "llama3", capabilities: ["tools"] },
1328
+ "VibeThinker-3B": { contextWindow: 8192, vramRequiredMb: 3300, architecture: "qwen2", capabilities: [], note: "Fits anywhere \u226412GB." }
1329
+ },
1330
+ aliases: {},
1331
+ retired: []
1332
+ },
1333
+ vllm: {
1334
+ kind: "openai",
1335
+ _comment: "Night Action vLLM + DSpark pilot (RTX 3060, matched draft `deepseek-ai/dspark_qwen3_8b_block7`). AWQ quant target (bf16 ~16GB does not fit 12GB VRAM alongside the draft; AWQ ~5.6GB does). Reference: HS-XNJI Experiment 1/2. Local, free, no key \u2014 vLLM OpenAI-compat endpoint on port 8000.",
1336
+ baseUrl: "http://192.168.1.252:8000/v1",
1337
+ keyEnv: null,
1338
+ roles: { default: "Qwen/Qwen3-8B-AWQ" },
1339
+ models: {
1340
+ "Qwen/Qwen3-8B-AWQ": { contextWindow: 32768, vramRequiredMb: 5600, architecture: "qwen3", tier: "local", capabilities: ["api", "tools", "thinking"] }
1341
+ },
1342
+ aliases: {},
1343
+ retired: [],
1344
+ providerKeys: { "qwen3-8b-dspark": { model: "Qwen/Qwen3-8B-AWQ", local: true } }
1345
+ }
1346
+ }
1347
+ };
1348
+
1349
+ // dev-factory/lib/model-catalog.mjs
1350
+ var MODEL_CATALOG_PATH = new URL("../model-catalog.json", import.meta.url).href;
1351
+ function loadModelCatalog(doc = (
1352
+ /** @type {any} */
1353
+ model_catalog_default
1354
+ )) {
1355
+ return doc;
1356
+ }
1357
+ var MODEL_CATALOG = Object.freeze(loadModelCatalog());
1358
+ function buildIndex(catalog) {
1359
+ const byId = /* @__PURE__ */ new Map();
1360
+ const aliases = /* @__PURE__ */ new Map();
1361
+ const retired = /* @__PURE__ */ new Map();
1362
+ const providers = /* @__PURE__ */ new Map();
1363
+ for (const [locality, block] of Object.entries({ cloud: catalog.cloud ?? {}, local: catalog.local ?? {} })) {
1364
+ for (const [provider, p] of Object.entries(block)) {
1365
+ if (provider.startsWith("_")) continue;
1366
+ providers.set(provider, { ...p, provider, locality, kind: p.kind ?? locality });
1367
+ for (const [id, m] of Object.entries(p.models ?? {})) {
1368
+ byId.set(id, Object.freeze({ id, kind: locality, provider, tier: m.tier ?? (locality === "local" ? "local" : null), ...m }));
1369
+ }
1370
+ for (const [alias, target] of Object.entries(p.aliases ?? {})) aliases.set(alias, { target, provider, kind: locality });
1371
+ for (const r of p.retired ?? []) retired.set(r.id, { ...r, provider, kind: locality });
1372
+ }
1373
+ }
1374
+ return { byId, aliases, retired, providers };
1375
+ }
1376
+ var INDEX = buildIndex(MODEL_CATALOG);
1377
+ var ALL_MODEL_IDS = Object.freeze([...INDEX.byId.keys()]);
1378
+ var MODEL_ALIASES = Object.freeze(Object.fromEntries([...INDEX.aliases].map(([a, v]) => [a, v.target])));
1379
+ var RETIRED_MODEL_IDS = Object.freeze([...INDEX.retired.keys()]);
1380
+ var PROVIDER_NAMES = Object.freeze([...INDEX.providers.keys()]);
1381
+ function resolveModelId(id) {
1382
+ if (typeof id !== "string" || !id) return id;
1383
+ const hit = INDEX.aliases.get(id);
1384
+ return hit ? hit.target : id;
1385
+ }
1386
+ function modelInfo(id) {
1387
+ return INDEX.byId.get(resolveModelId(id)) ?? null;
1388
+ }
1389
+ function providerOf(id) {
1390
+ const m = modelInfo(id);
1391
+ return m ? INDEX.providers.get(m.provider) ?? null : null;
1392
+ }
1393
+ function providerInfo(name) {
1394
+ return INDEX.providers.get(name) ?? null;
1395
+ }
1396
+ function isLocalModel(id) {
1397
+ return modelInfo(id)?.kind === "local";
1398
+ }
1399
+ function roleBlock(name, extra = {}) {
1400
+ const p = INDEX.providers.get(name) ?? {};
1401
+ const roles = p.roles ?? {};
1402
+ const upper = Object.fromEntries(Object.entries(roles).map(([k, v]) => [k.replace(/([a-z])([A-Z])/g, "$1_$2").toUpperCase(), v]));
1403
+ return Object.freeze({
1404
+ provider: name,
1405
+ kind: p.kind ?? null,
1406
+ company: p.company ?? name,
1407
+ baseUrl: p.baseUrl ?? null,
1408
+ keyEnv: p.keyEnv ?? null,
1409
+ keyEnvFallback: p.keyEnvFallback ?? null,
1410
+ verifiedOn: p.verifiedOn ?? MODEL_CATALOG.verified,
1411
+ roles: Object.freeze({ ...roles }),
1412
+ ids: Object.freeze(Object.keys(p.models ?? {})),
1413
+ aliases: Object.freeze({ ...p.aliases ?? {} }),
1414
+ retired: Object.freeze((p.retired ?? []).map((r) => r.id)),
1415
+ ...upper,
1416
+ // e.g. roles.chatDefault → CHAT_DEFAULT, roles.cheap → CHEAP
1417
+ ...extra
1418
+ });
1419
+ }
1420
+ var DS = MODEL_CATALOG.cloud?.deepseek ?? {};
1421
+ var DEEPSEEK = roleBlock("deepseek", {
1422
+ FLASH: DS.roles?.cheap,
1423
+ PRO: DS.roles?.strong,
1424
+ anthropicBaseUrl: DS.anthropicBaseUrl ?? null,
1425
+ peakHoursUtc: DS.peakHoursUtc ?? null
1426
+ });
1427
+ var ANTHROPIC = roleBlock("anthropic", { cliAliases: Object.freeze({ ...MODEL_CATALOG.cloud?.anthropic?.cliAliases ?? {} }) });
1428
+ var OPENAI = roleBlock("openai");
1429
+ var GEMINI = roleBlock("gemini");
1430
+ var OPENROUTER = roleBlock("openrouter");
1431
+ var GITHUB_MODELS = roleBlock("github-models");
1432
+ var OLLAMA = roleBlock("ollama");
1433
+ var VLLM = roleBlock("vllm");
1434
+ function isDeepSeekPeak(date = /* @__PURE__ */ new Date()) {
1435
+ const p = DEEPSEEK.peakHoursUtc;
1436
+ if (!p) return true;
1437
+ const day = date.getUTCDay();
1438
+ if (Array.isArray(p.days) && !p.days.includes(day)) return false;
1439
+ const mins = date.getUTCHours() * 60 + date.getUTCMinutes();
1440
+ const toMin = (hhmm) => {
1441
+ const [h, m] = String(hhmm).split(":").map(Number);
1442
+ return h * 60 + (m || 0);
1443
+ };
1444
+ return (p.ranges ?? []).some(([a, b]) => mins >= toMin(a) && mins < toMin(b));
1445
+ }
1446
+ function deepseekPricePer1M(id, { cacheHit = false, peak = null, at = /* @__PURE__ */ new Date() } = {}) {
1447
+ const m = modelInfo(id);
1448
+ if (!m || m.provider !== "deepseek" || !m.pricePer1M?.peak) return null;
1449
+ const corner = peak ?? isDeepSeekPeak(at) ? m.pricePer1M.peak : m.pricePer1M.offPeak;
1450
+ return { input: cacheHit ? corner.cacheHit : corner.cacheMiss, output: corner.output };
1451
+ }
1452
+ function listPricePer1M(id) {
1453
+ const m = modelInfo(id);
1454
+ if (!m) return null;
1455
+ if (m.kind === "local") return { input: 0, output: 0 };
1456
+ if (m.provider === "deepseek") return deepseekPricePer1M(id, { peak: true });
1457
+ if (m.pricePer1M && Number.isFinite(m.pricePer1M.input)) return { input: m.pricePer1M.input, output: m.pricePer1M.output };
1458
+ return null;
1459
+ }
1460
+ function providerEntries() {
1461
+ const out = {};
1462
+ for (const [name, p] of INDEX.providers) {
1463
+ if (!p.providerKeys || !p.baseUrl) continue;
1464
+ const base = { kind: p.kind ?? "openai", baseUrl: p.baseUrl, keyEnv: p.keyEnv ?? null };
1465
+ if (p.keyEnvFallback) base.keyEnvFallback = p.keyEnvFallback;
1466
+ if (p.company && p.company !== name) base.company = p.company;
1467
+ if (p.betaBaseUrl) base.betaBaseUrl = p.betaBaseUrl;
1468
+ if (p.userIdField) base.userIdField = p.userIdField;
1469
+ const entryFor = (modelId, overrides = {}) => {
1470
+ const target = resolveModelId(modelId);
1471
+ const m = modelInfo(target);
1472
+ const price = listPricePer1M(target) ?? { input: 0, output: 0 };
1473
+ const e = { ...base, model: modelId, priceIn: price.input, priceOut: price.output, tier: m?.tier ?? (p.locality === "local" ? "local" : "strong") };
1474
+ if (m?.kind === "local" || overrides.local) e.local = true;
1475
+ if (m?.noTemp || overrides.noTemp) e.noTemp = true;
1476
+ if (overrides.legacyAliasOf) e.legacyAliasOf = overrides.legacyAliasOf;
1477
+ return e;
1478
+ };
1479
+ for (const [key, spec] of Object.entries(p.providerKeys)) out[key] = entryFor(spec.model, spec);
1480
+ if (p.emitAliasesAsProviderKeys) {
1481
+ for (const [alias, target] of Object.entries(p.aliases ?? {})) {
1482
+ if (out[alias] || Object.values(p.providerKeys).some((s) => s.model === alias)) continue;
1483
+ out[alias] = entryFor(target, { legacyAliasOf: target });
1484
+ }
1485
+ }
1486
+ }
1487
+ return out;
1488
+ }
1489
+ function fleetManifestModelRows() {
1490
+ const contextWindows = {};
1491
+ const costEstimates = {};
1492
+ for (const id of ALL_MODEL_IDS) {
1493
+ const m = modelInfo(id);
1494
+ contextWindows[id] = {
1495
+ contextWindow: m.contextWindow,
1496
+ vramRequiredMb: m.vramRequiredMb ?? null,
1497
+ architecture: m.architecture ?? m.provider,
1498
+ capabilities: m.capabilities ?? []
1499
+ };
1500
+ const price = listPricePer1M(id);
1501
+ if (m.kind === "local") {
1502
+ costEstimates[id] = { kind: "free", note: `local model, zero API cost.${m.note ? " " + m.note : ""}` };
1503
+ } else if (m.provider === "deepseek") {
1504
+ costEstimates[id] = {
1505
+ kind: "metered",
1506
+ usdPerMInput: price.input,
1507
+ usdPerMOutput: price.output,
1508
+ note: `${m.displayName ?? id}: peak cache-miss list rates (model-catalog.json, verified ${providerInfo("deepseek")?.verifiedOn}); off-peak halves, cache hits ~50x cheaper`
1509
+ };
1510
+ } else if (price) {
1511
+ costEstimates[id] = {
1512
+ kind: "metered",
1513
+ usdPerMInput: price.input,
1514
+ usdPerMOutput: price.output,
1515
+ note: `${m.displayName ?? id}: list rates (model-catalog.json, verified ${providerOf(id)?.verifiedOn ?? MODEL_CATALOG.verified})`
1516
+ };
1517
+ } else {
1518
+ costEstimates[id] = { kind: "metered", note: m.note ?? "pricing not recorded in model-catalog.json" };
1519
+ }
1520
+ }
1521
+ return { contextWindows, costEstimates };
1522
+ }
1523
+ function runSelfTest({ log = console.log } = {}) {
1524
+ let ok = true;
1525
+ const check = (name, cond) => {
1526
+ log(` ${cond ? "\u2713" : "\u2717"} ${name}`);
1527
+ if (!cond) ok = false;
1528
+ };
1529
+ log("== model-catalog self-test ==\n");
1530
+ check("catalog has a verified date", /^\d{4}-\d{2}-\d{2}$/.test(String(MODEL_CATALOG.verified)));
1531
+ check(
1532
+ "every provider role points at a current id of that provider",
1533
+ [...INDEX.providers.values()].every((p) => Object.values(p.roles ?? {}).every((id) => id in (p.models ?? {}) || (p.aliases ?? {})[id]))
1534
+ );
1535
+ check(
1536
+ "every providerKeys.model is a current id or a declared alias of that provider",
1537
+ [...INDEX.providers.values()].every((p) => Object.values(p.providerKeys ?? {}).every((s) => s.model in (p.models ?? {}) || (p.aliases ?? {})[s.model]))
1538
+ );
1539
+ check(
1540
+ "no alias target is itself an alias or retired",
1541
+ Object.values(MODEL_ALIASES).every((t) => INDEX.byId.has(t) && !INDEX.retired.has(t))
1542
+ );
1543
+ check(
1544
+ "every retired id has an alias to its successor",
1545
+ RETIRED_MODEL_IDS.every((id) => MODEL_ALIASES[id])
1546
+ );
1547
+ check(
1548
+ "no current id is also listed as retired",
1549
+ ALL_MODEL_IDS.every((id) => !INDEX.retired.has(id))
1550
+ );
1551
+ check(
1552
+ "every model has a positive context window",
1553
+ ALL_MODEL_IDS.every((id) => modelInfo(id).contextWindow > 0)
1554
+ );
1555
+ check(
1556
+ "every cloud model has a list price",
1557
+ ALL_MODEL_IDS.filter((id) => !isLocalModel(id)).every((id) => listPricePer1M(id)?.input > 0 && listPricePer1M(id)?.output > 0)
1558
+ );
1559
+ check(
1560
+ "DeepSeek prices are four-corner, positive, and off-peak is half of peak",
1561
+ DEEPSEEK.ids.every((id) => {
1562
+ const p = modelInfo(id).pricePer1M;
1563
+ return ["cacheHit", "cacheMiss", "output"].every((k) => p?.peak?.[k] > 0 && Math.abs(p.peak[k] - 2 * p.offPeak[k]) < 1e-9);
1564
+ })
1565
+ );
1566
+ check(
1567
+ "every local model has a VRAM figure",
1568
+ ALL_MODEL_IDS.filter(isLocalModel).every((id) => Number.isFinite(modelInfo(id).vramRequiredMb))
1569
+ );
1570
+ check(
1571
+ "resolveModelId maps a retired id to its successor",
1572
+ RETIRED_MODEL_IDS.every((id) => ALL_MODEL_IDS.includes(resolveModelId(id)))
1573
+ );
1574
+ check("providerEntries covers every providerKey and every DeepSeek alias, with prices", (() => {
1575
+ const e = providerEntries();
1576
+ const keys = [...INDEX.providers.values()].flatMap((p) => Object.keys(p.providerKeys ?? {}));
1577
+ return keys.every((k) => e[k]?.model && e[k].baseUrl && Number.isFinite(e[k].priceIn)) && Object.keys(DEEPSEEK.aliases).every((a) => e[a]?.legacyAliasOf || e.deepseek?.model === a);
1578
+ })());
1579
+ check(
1580
+ "isDeepSeekPeak: Tue 02:30 UTC is peak, Sat 02:30 UTC is not, Tue 12:00 UTC is not",
1581
+ isDeepSeekPeak(/* @__PURE__ */ new Date("2026-09-15T02:30:00Z")) && !isDeepSeekPeak(/* @__PURE__ */ new Date("2026-09-19T02:30:00Z")) && !isDeepSeekPeak(/* @__PURE__ */ new Date("2026-09-15T12:00:00Z"))
1582
+ );
1583
+ check(
1584
+ "fleetManifestModelRows covers every catalog id",
1585
+ Object.keys(fleetManifestModelRows().contextWindows).length === ALL_MODEL_IDS.length
1586
+ );
1587
+ check(
1588
+ "Claude CLI alias words resolve to current Anthropic ids",
1589
+ Object.values(ANTHROPIC.cliAliases).every((id) => ANTHROPIC.ids.includes(id))
1590
+ );
1591
+ log(`
1592
+ ${ok ? "\u2713 self-test PASS \u2014 catalog invariants verified." : "\u2717 self-test FAIL"}`);
1593
+ return ok ? 0 : 1;
1594
+ }
1595
+ var isMain = typeof process !== "undefined" && Array.isArray(process.argv) && Boolean(process.argv[1]) && /model-catalog\.mjs$/.test(String(process.argv[1]).replace(/\\/g, "/"));
1596
+ if (isMain) {
1597
+ const argv = process.argv.slice(2);
1598
+ if (argv.includes("--self-test")) process.exit(runSelfTest());
1599
+ if (argv.includes("--print")) {
1600
+ console.log(JSON.stringify({ DEEPSEEK, ANTHROPIC, OPENAI, GEMINI, OPENROUTER, GITHUB_MODELS, OLLAMA, VLLM, ALL_MODEL_IDS, MODEL_ALIASES, RETIRED_MODEL_IDS, providerEntries: providerEntries() }, null, 2));
1601
+ process.exit(0);
1602
+ }
1603
+ if (argv.length) {
1604
+ console.error("usage: node dev-factory/lib/model-catalog.mjs --self-test | --print");
1605
+ process.exit(64);
1606
+ }
1607
+ }
1608
+
1609
+ // spex-console/src/lib/model-catalog.ts
1610
+ var ANTHROPIC2 = ANTHROPIC;
1611
+ var OLLAMA2 = OLLAMA;
1612
+
1613
+ // spex-console/src/lib/launch-preferences.ts
1614
+ var USE_CASE_SETTINGS_KEY = {
1615
+ "task-creation": "taskCreation",
1616
+ "task-implementation": "taskImplementation",
1617
+ handoff: "handoff",
1618
+ milestone: "milestone",
1619
+ epic: "epic",
1620
+ sprint: "sprint"
1621
+ };
1622
+ var LAUNCH_SETTINGS_BUCKET_KEYS = Object.values(
1623
+ USE_CASE_SETTINGS_KEY
1624
+ );
1625
+ var DEFAULT_LAUNCH_MODEL_PREFERENCES = {
1626
+ // Every Claude default is a version-less ALIAS, never a pinned id (owner
1627
+ // ruling 2026-09-01: "I don't like seeing these numbers hard-set"). Claude
1628
+ // renumbers a family every few months, and a pinned default silently keeps
1629
+ // opening last season's model until someone notices and edits this file.
1630
+ // An alias resolves to the newest id in its family at launch time
1631
+ // (`launch-model-aliases.ts`), so the shipped default never goes stale.
1632
+ // Anyone who WANTS a pinned id can still set one — `hs next model epic
1633
+ // claude-opus-4-8` stores it byte-for-byte. Non-Claude values have no alias
1634
+ // vocabulary and stay exact: `composer-2.5-fast` is a Cursor model, and
1635
+ // `qwen3-coder:30b` a local fleet one.
1636
+ taskCreation: {
1637
+ claudeCode: "claude-opus",
1638
+ cursorAgent: "claude-opus",
1639
+ fleet: OLLAMA2.CODER,
1640
+ defaultAgent: "claude-code"
1641
+ },
1642
+ taskImplementation: {
1643
+ claudeCode: "claude-sonnet",
1644
+ cursorAgent: "composer-2.5-fast",
1645
+ fleet: OLLAMA2.CODER,
1646
+ defaultAgent: "claude-code"
1647
+ },
1648
+ // A handoff resume re-enters existing work — same weight class as task
1649
+ // implementation (Sonnet), Claude Code only by default (no --tool flag on
1650
+ // `hs handoff`). Settings key: `launchModels.handoff.*`.
1651
+ handoff: {
1652
+ claudeCode: "claude-sonnet",
1653
+ cursorAgent: "composer-2.5-fast",
1654
+ fleet: OLLAMA2.CODER,
1655
+ defaultAgent: "claude-code"
1656
+ },
1657
+ // Launching a milestone, epic or sprint means BREAKING IT DOWN into child
1658
+ // work, not writing code — the same job `taskCreation` already weights at
1659
+ // Opus, so these three inherit that weight class rather than task
1660
+ // implementation's Sonnet. Settings keys: `launchModels.{milestone,epic,sprint}.*`.
1661
+ milestone: {
1662
+ claudeCode: "claude-opus",
1663
+ cursorAgent: "claude-opus",
1664
+ fleet: OLLAMA2.CODER,
1665
+ defaultAgent: "claude-code"
1666
+ },
1667
+ epic: {
1668
+ claudeCode: "claude-opus",
1669
+ cursorAgent: "claude-opus",
1670
+ fleet: OLLAMA2.CODER,
1671
+ defaultAgent: "claude-code"
1672
+ },
1673
+ sprint: {
1674
+ claudeCode: "claude-opus",
1675
+ cursorAgent: "claude-opus",
1676
+ fleet: OLLAMA2.CODER,
1677
+ defaultAgent: "claude-code"
1678
+ },
1679
+ closeTerminalOnTaskComplete: true,
1680
+ autoClaimOnLaunch: false,
1681
+ // Handheld is the safest default (human in the loop); mvp matches the active
1682
+ // operating mode. Both are user-settable in Settings.
1683
+ defaultRunMode: "handheld",
1684
+ defaultReviewProfile: "mvp"
1685
+ };
1686
+ var CLAUDE_LAUNCH_MODEL_OPTIONS = [
1687
+ { id: ANTHROPIC2.STRONG, label: "Claude Opus 5" },
1688
+ { id: ANTHROPIC2.MID, label: "Claude Sonnet 5" },
1689
+ { id: "claude-fable-5", label: "Claude Fable 5" },
1690
+ { id: ANTHROPIC2.PREVIOUS_STRONG, label: "Claude Opus 4.8" },
1691
+ { id: ANTHROPIC2.PREVIOUS_MID, label: "Claude Sonnet 4.6" },
1692
+ { id: ANTHROPIC2.CHEAP, label: "Claude Haiku 4.5" }
1693
+ ];
1694
+ var CURSOR_LAUNCH_MODEL_OPTIONS = [
1695
+ { id: "auto", label: "Auto" },
1696
+ { id: "composer-2.5-fast", label: "Composer 2.5 Fast" },
1697
+ { id: ANTHROPIC2.PREVIOUS_STRONG, label: "Claude Opus 4.8 (via Cursor)" },
1698
+ { id: ANTHROPIC2.PREVIOUS_MID, label: "Claude Sonnet 4.6 (via Cursor)" }
1699
+ ];
1700
+ var CLAUDE_PERMISSION_MODE_OPTIONS = [
1701
+ "acceptEdits",
1702
+ "auto",
1703
+ "bypassPermissions",
1704
+ "manual",
1705
+ "dontAsk",
1706
+ "plan"
1707
+ ];
1708
+ var CURSOR_PERMISSION_MODE_OPTIONS = [];
1709
+
1710
+ // spex-console/src/lib/cli/agent-identity.ts
1711
+ function resolveAgentIdentity(argAgent, argSession) {
1712
+ const trimmedAgent = typeof argAgent === "string" ? argAgent.trim() : "";
1713
+ const trimmedSession = typeof argSession === "string" ? argSession.trim() : "";
1714
+ if (trimmedAgent && trimmedSession) {
1715
+ return { agent: trimmedAgent, sessionId: trimmedSession };
1716
+ }
1717
+ const claudeSession = (process.env.CLAUDE_CODE_SESSION_ID || process.env.CLAUDE_SESSION_ID || "").trim();
1718
+ if (claudeSession) {
1719
+ return {
1720
+ agent: trimmedAgent || "Claude Code",
1721
+ sessionId: trimmedSession || claudeSession
1722
+ };
1723
+ }
1724
+ const cursorSession = (process.env.CURSOR_TRACE_ID || process.env.CURSOR_SESSION_ID || process.env.COMPOSER_SESSION_ID || "").trim();
1725
+ if (cursorSession) {
1726
+ return {
1727
+ agent: trimmedAgent || "Cursor",
1728
+ sessionId: trimmedSession || cursorSession
1729
+ };
1730
+ }
1731
+ return {
1732
+ agent: trimmedAgent || process.env.HEYSPEX_AGENT?.trim() || "External agent",
1733
+ sessionId: trimmedSession || process.env.HEYSPEX_SESSION_ID?.trim() || process.env.HEYSPEX_AGENT_SESSION?.trim() || "local"
1734
+ };
1735
+ }
1736
+
1737
+ // spex-console/src/lib/cli/handoff.ts
1738
+ var ALL_LAUNCH_MODEL_OPTIONS = [...CLAUDE_LAUNCH_MODEL_OPTIONS, ...CURSOR_LAUNCH_MODEL_OPTIONS];
1739
+ function isKnownModelValue(value, options) {
1740
+ return options.some((o) => o.id === value) || isModelAliasValue(value);
1741
+ }
1742
+ function validModelOptionsList(options) {
1743
+ return [...options.map((o) => o.id), ...MODEL_ALIAS_INPUT_FORMS];
1744
+ }
1745
+ var ALL_LAUNCH_PERMISSION_MODE_OPTIONS = [
1746
+ ...CLAUDE_PERMISSION_MODE_OPTIONS,
1747
+ ...CURSOR_PERMISSION_MODE_OPTIONS
1748
+ ];
1749
+ var LANE_CODE_CHARSET = "ABCDEFGHJKMNPQRSTUVWXYZ23456789";
1750
+ var LANE_CODE_LENGTH = 4;
1751
+ var LANE_CODE_MAX_LENGTH = 12;
1752
+ var LANE_CODE_RE = /^(?=[0-9]*[A-Z])[A-Z0-9]{4,}$/;
1753
+ function mintLaneCode(occupied, randomUnit = Math.random) {
1754
+ const taken = new Set([...occupied].map((c) => String(c).toUpperCase()));
1755
+ let length = LANE_CODE_LENGTH;
1756
+ for (; length <= LANE_CODE_MAX_LENGTH; ) {
1757
+ const maxAttempts = Math.min(LANE_CODE_CHARSET.length ** length, 1e4);
1758
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
1759
+ let code = "";
1760
+ for (let i = 0; i < length; i++) {
1761
+ const idx = Math.floor(randomUnit() * LANE_CODE_CHARSET.length);
1762
+ code += LANE_CODE_CHARSET[Math.min(idx, LANE_CODE_CHARSET.length - 1)];
1763
+ }
1764
+ if (!LANE_CODE_RE.test(code)) continue;
1765
+ if (!taken.has(code)) return code;
1766
+ }
1767
+ length++;
1768
+ }
1769
+ throw new Error(
1770
+ `could not mint a lane code up to ${LANE_CODE_MAX_LENGTH} characters \u2014 ${occupied.size} lane(s) already in flight`
1771
+ );
1772
+ }
1773
+ function isKnownPermissionModeValue(value, options) {
1774
+ return options.includes(value);
1775
+ }
1776
+ function validateLaunchFlags(args) {
1777
+ if (args.launch && args.sub === "record") {
1778
+ return {
1779
+ code: EXIT.USAGE,
1780
+ message: "--launch is not valid with record (use load, or the bare-code shorthand)"
1781
+ };
1782
+ }
1783
+ if (args.launch && args.all) {
1784
+ return {
1785
+ code: EXIT.USAGE,
1786
+ message: "--launch is not valid with --all (launch targets a single lane)"
1787
+ };
1788
+ }
1789
+ if (args.newCode && args.sub !== "record") {
1790
+ return { code: EXIT.USAGE, message: "--new-code is only valid on record" };
1791
+ }
1792
+ if (args.newCode && args.lane !== void 0) {
1793
+ return {
1794
+ code: EXIT.USAGE,
1795
+ message: "--new-code mints the lane code, so it cannot be combined with --lane"
1796
+ };
1797
+ }
1798
+ if (args.dryRun && !args.launch) {
1799
+ return { code: EXIT.USAGE, message: "--dry-run requires --launch" };
1800
+ }
1801
+ if (args.force && !args.launch) {
1802
+ return {
1803
+ code: EXIT.USAGE,
1804
+ message: "--force requires --launch (e.g. hs handoff <CODE> --force)"
1805
+ };
1806
+ }
1807
+ if (args.modelOverride !== void 0 && !args.launch) {
1808
+ return {
1809
+ code: EXIT.USAGE,
1810
+ message: "--model requires --launch (e.g. hs handoff <CODE> --model <value> --launch)"
1811
+ };
1812
+ }
1813
+ if (args.modelOverride !== void 0 && !isKnownModelValue(args.modelOverride, ALL_LAUNCH_MODEL_OPTIONS)) {
1814
+ const validOptions = validModelOptionsList(ALL_LAUNCH_MODEL_OPTIONS);
1815
+ return {
1816
+ code: EXIT.INVALID_ARG,
1817
+ message: `unknown model "${args.modelOverride}". Valid options: ${validOptions.join(", ")}`
1818
+ };
1819
+ }
1820
+ if (args.permissionModeOverride !== void 0 && !args.launch) {
1821
+ return {
1822
+ code: EXIT.USAGE,
1823
+ message: "--permission-mode requires --launch (e.g. hs handoff <CODE> --permission-mode <mode> --launch)"
1824
+ };
1825
+ }
1826
+ if (args.permissionModeOverride !== void 0 && !isKnownPermissionModeValue(args.permissionModeOverride, ALL_LAUNCH_PERMISSION_MODE_OPTIONS)) {
1827
+ return {
1828
+ code: EXIT.INVALID_ARG,
1829
+ message: `unknown permission mode "${args.permissionModeOverride}". Valid modes: ${ALL_LAUNCH_PERMISSION_MODE_OPTIONS.join(", ")}`
1830
+ };
1831
+ }
1832
+ return null;
1833
+ }
1834
+ function parseHandoffArgs(argv) {
1835
+ const subRaw = argv[0];
1836
+ if (subRaw === "--help" || subRaw === "-h" || subRaw === void 0) {
1837
+ return { help: true };
1838
+ }
1839
+ if (subRaw === "model") {
1840
+ const args2 = { sub: "model", blockers: [], links: [], all: false, json: false };
1841
+ for (let i = 1; i < argv.length; i++) {
1842
+ const arg = argv[i];
1843
+ if (arg === "--help" || arg === "-h") return { help: true };
1844
+ else if (arg === "--json") args2.json = true;
1845
+ else if (arg.startsWith("-")) {
1846
+ return { error: { code: EXIT.USAGE, message: `unknown flag: ${arg}` } };
1847
+ } else if (args2.modelValue === void 0) {
1848
+ args2.modelValue = arg;
1849
+ } else {
1850
+ return { error: { code: EXIT.USAGE, message: `unexpected argument: ${arg}` } };
1851
+ }
1852
+ }
1853
+ return { args: args2 };
1854
+ }
1855
+ if (subRaw === "permission-mode") {
1856
+ const args2 = {
1857
+ sub: "permission-mode",
1858
+ blockers: [],
1859
+ links: [],
1860
+ all: false,
1861
+ json: false
1862
+ };
1863
+ for (let i = 1; i < argv.length; i++) {
1864
+ const arg = argv[i];
1865
+ if (arg === "--help" || arg === "-h") return { help: true };
1866
+ else if (arg === "--json") args2.json = true;
1867
+ else if (arg.startsWith("-")) {
1868
+ return { error: { code: EXIT.USAGE, message: `unknown flag: ${arg}` } };
1869
+ } else if (args2.permissionModeValue === void 0) {
1870
+ args2.permissionModeValue = arg;
1871
+ } else {
1872
+ return { error: { code: EXIT.USAGE, message: `unexpected argument: ${arg}` } };
1873
+ }
1874
+ }
1875
+ return { args: args2 };
1876
+ }
1877
+ if (subRaw === "delete") {
1878
+ const args2 = {
1879
+ sub: "delete",
1880
+ blockers: [],
1881
+ links: [],
1882
+ all: false,
1883
+ json: false
1884
+ };
1885
+ for (let i = 1; i < argv.length; i++) {
1886
+ const arg = argv[i];
1887
+ if (arg === "--help" || arg === "-h") return { help: true };
1888
+ else if (arg === "--json") args2.json = true;
1889
+ else if (arg === "--yes" || arg === "-y") args2.yes = true;
1890
+ else if (arg === "--lane" || arg.startsWith("--lane=")) {
1891
+ const v = arg.startsWith("--lane=") ? arg.slice("--lane=".length) : argv[++i];
1892
+ if (v === void 0) {
1893
+ return { error: { code: EXIT.INVALID_ARG, message: "--lane requires a value" } };
1894
+ }
1895
+ args2.lane = v;
1896
+ } else if (arg.startsWith("-")) {
1897
+ return { error: { code: EXIT.USAGE, message: `unknown flag: ${arg}` } };
1898
+ } else if (args2.lane === void 0) {
1899
+ args2.lane = arg;
1900
+ } else {
1901
+ return { error: { code: EXIT.USAGE, message: `unexpected argument: ${arg}` } };
1902
+ }
1903
+ }
1904
+ if (args2.lane === void 0) {
1905
+ return {
1906
+ error: {
1907
+ code: EXIT.USAGE,
1908
+ message: "delete needs a lane code (e.g. hs handoff delete US2R --yes)"
1909
+ }
1910
+ };
1911
+ }
1912
+ return { args: args2 };
1913
+ }
1914
+ if (subRaw !== "record" && subRaw !== "load") {
1915
+ if (!subRaw.startsWith("-")) {
1916
+ const args2 = {
1917
+ sub: "load",
1918
+ lane: subRaw,
1919
+ blockers: [],
1920
+ links: [],
1921
+ all: false,
1922
+ json: false,
1923
+ launch: true
1924
+ };
1925
+ for (let i = 1; i < argv.length; i++) {
1926
+ const arg = argv[i];
1927
+ if (arg === "--launch") args2.launch = true;
1928
+ else if (arg === "--no-launch") args2.launch = false;
1929
+ else if (arg === "--dry-run") args2.dryRun = true;
1930
+ else if (arg === "--force") args2.force = true;
1931
+ else if (arg === "--json") args2.json = true;
1932
+ else if (arg === "--model" || arg.startsWith("--model=")) {
1933
+ const v = arg.startsWith("--model=") ? arg.slice("--model=".length) : argv[++i];
1934
+ if (v === void 0) {
1935
+ return { error: { code: EXIT.INVALID_ARG, message: "--model requires a value" } };
1936
+ }
1937
+ args2.modelOverride = v;
1938
+ } else if (arg === "--permission-mode" || arg.startsWith("--permission-mode=")) {
1939
+ const v = arg.startsWith("--permission-mode=") ? arg.slice("--permission-mode=".length) : argv[++i];
1940
+ if (v === void 0) {
1941
+ return {
1942
+ error: { code: EXIT.INVALID_ARG, message: "--permission-mode requires a value" }
1943
+ };
1944
+ }
1945
+ args2.permissionModeOverride = v;
1946
+ } else {
1947
+ return { error: { code: EXIT.USAGE, message: `unknown subcommand: ${subRaw}` } };
1948
+ }
1949
+ }
1950
+ const flagError2 = validateLaunchFlags(args2);
1951
+ if (flagError2) return { error: flagError2 };
1952
+ return { args: args2 };
1953
+ }
1954
+ return { error: { code: EXIT.USAGE, message: `unknown subcommand: ${subRaw}` } };
1955
+ }
1956
+ const sub = subRaw;
1957
+ const args = { sub, blockers: [], links: [], all: false, json: false };
1958
+ const takeValue = (arg, inline, i) => {
1959
+ if (arg.startsWith(`${inline}=`)) return arg.slice(inline.length + 1);
1960
+ return argv[++i.v];
1961
+ };
1962
+ for (const counter = { v: 1 }; counter.v < argv.length; counter.v++) {
1963
+ const arg = argv[counter.v];
1964
+ if (arg === "--help" || arg === "-h") return { help: true };
1965
+ else if (arg === "--json") args.json = true;
1966
+ else if (arg === "--all") args.all = true;
1967
+ else if (arg === "--new-code") args.newCode = true;
1968
+ else if (arg === "--launch") args.launch = true;
1969
+ else if (arg === "--dry-run") args.dryRun = true;
1970
+ else if (arg === "--force") args.force = true;
1971
+ else if (arg === "--lane" || arg.startsWith("--lane=")) {
1972
+ const v = takeValue(arg, "--lane", counter);
1973
+ if (v === void 0)
1974
+ return { error: { code: EXIT.INVALID_ARG, message: "--lane requires a value" } };
1975
+ args.lane = v;
1976
+ } else if (arg === "--agent" || arg.startsWith("--agent=")) {
1977
+ const v = takeValue(arg, "--agent", counter);
1978
+ if (v === void 0)
1979
+ return { error: { code: EXIT.INVALID_ARG, message: "--agent requires a value" } };
1980
+ args.agent = v;
1981
+ } else if (arg === "--session" || arg.startsWith("--session=")) {
1982
+ const v = takeValue(arg, "--session", counter);
1983
+ if (v === void 0)
1984
+ return { error: { code: EXIT.INVALID_ARG, message: "--session requires a value" } };
1985
+ args.session = v;
1986
+ } else if (arg === "--spec" || arg.startsWith("--spec=")) {
1987
+ const v = takeValue(arg, "--spec", counter);
1988
+ if (v === void 0)
1989
+ return { error: { code: EXIT.INVALID_ARG, message: "--spec requires a value" } };
1990
+ args.specId = v;
1991
+ } else if (arg === "--phase" || arg.startsWith("--phase=")) {
1992
+ const v = takeValue(arg, "--phase", counter);
1993
+ if (v === void 0)
1994
+ return { error: { code: EXIT.INVALID_ARG, message: "--phase requires a value" } };
1995
+ args.phase = v;
1996
+ } else if (arg === "--time" || arg.startsWith("--time=")) {
1997
+ const v = takeValue(arg, "--time", counter);
1998
+ if (v === void 0)
1999
+ return { error: { code: EXIT.INVALID_ARG, message: "--time requires a value" } };
2000
+ args.estimatedTime = v;
2001
+ } else if (arg === "--blocker" || arg.startsWith("--blocker=")) {
2002
+ const v = takeValue(arg, "--blocker", counter);
2003
+ if (v === void 0)
2004
+ return { error: { code: EXIT.INVALID_ARG, message: "--blocker requires a value" } };
2005
+ args.blockers.push(v);
2006
+ } else if (arg === "--link" || arg.startsWith("--link=")) {
2007
+ const v = takeValue(arg, "--link", counter);
2008
+ if (v === void 0)
2009
+ return { error: { code: EXIT.INVALID_ARG, message: "--link requires a value" } };
2010
+ args.links.push(v);
2011
+ } else if (arg === "--model" || arg.startsWith("--model=")) {
2012
+ const v = takeValue(arg, "--model", counter);
2013
+ if (v === void 0)
2014
+ return { error: { code: EXIT.INVALID_ARG, message: "--model requires a value" } };
2015
+ args.modelOverride = v;
2016
+ } else if (arg === "--permission-mode" || arg.startsWith("--permission-mode=")) {
2017
+ const v = takeValue(arg, "--permission-mode", counter);
2018
+ if (v === void 0)
2019
+ return { error: { code: EXIT.INVALID_ARG, message: "--permission-mode requires a value" } };
2020
+ args.permissionModeOverride = v;
2021
+ } else if (arg.startsWith("-")) {
2022
+ return { error: { code: EXIT.USAGE, message: `unknown flag: ${arg}` } };
2023
+ } else {
2024
+ return { error: { code: EXIT.USAGE, message: `unexpected argument: ${arg}` } };
2025
+ }
2026
+ }
2027
+ const flagError = validateLaunchFlags(args);
2028
+ if (flagError) return { error: flagError };
2029
+ return { args };
2030
+ }
2031
+ function describeContext(c) {
2032
+ if (!c) return "(no current handoff context)";
2033
+ const parts = [c.laneKey ? `lane ${c.laneKey}` : ""];
2034
+ if (c.specId) parts.push(`spec ${c.specId}`);
2035
+ if (c.phase) parts.push(`phase ${c.phase}`);
2036
+ if (c.estimatedTime) parts.push(`~${c.estimatedTime}`);
2037
+ return parts.filter(Boolean).join(" \xB7 ");
2038
+ }
2039
+ async function launchHandoff(lane, args, deps, context) {
2040
+ const contextLine = describeContext(context);
2041
+ const body = {
2042
+ prompt: `/hs-handoff ${lane}`,
2043
+ launchUseCase: "handoff",
2044
+ ...args.dryRun ? { dryRun: true } : {},
2045
+ // The route holds a 5-minute in-flight marker per lane so a second window
2046
+ // for the same code is a no-op (the runaway-chain guard). `--force` is the
2047
+ // deliberate re-open.
2048
+ ...args.force ? { force: true } : {},
2049
+ // Per-launch override ([[HS-AWU6]]) — slots above the spec/settings ladder
2050
+ // server-side (`resolveLaunchTarget`, modelSource: "override"). The stored
2051
+ // `hs handoff model` default is never written on this path.
2052
+ ...args.modelOverride !== void 0 ? { modelOverride: args.modelOverride } : {},
2053
+ // Per-launch permission-mode override ([[HS-JKA6]]) — additive, same shape
2054
+ // as modelOverride above. Never persisted; the per-agent settings default
2055
+ // (if any) lives at `launchPermissions.handoff.{claudeCode,cursorAgent}`,
2056
+ // untouched by this flag.
2057
+ ...args.permissionModeOverride !== void 0 ? { permissionModeOverride: args.permissionModeOverride } : {}
2058
+ };
2059
+ const res = await controlRequest(deps, { method: "POST", path: "/api/agent/start", body });
2060
+ if (!res.ok) return res.error;
2061
+ if (res.status !== 200) return writeFailed(res.status, res.body);
2062
+ const payload = res.body ?? {};
2063
+ const agent = typeof payload.agent === "string" ? payload.agent : "?";
2064
+ const model = typeof payload.model === "string" ? payload.model : "?";
2065
+ const modelSource = typeof payload.modelSource === "string" ? payload.modelSource : "?";
2066
+ const modelDisplay = typeof payload.modelDisplay === "string" ? payload.modelDisplay : model;
2067
+ const prompt = typeof payload.prompt === "string" ? payload.prompt : `/hs-handoff ${lane}`;
2068
+ const permissionMode = typeof payload.permissionMode === "string" && payload.permissionMode ? payload.permissionMode : null;
2069
+ const permissionModeSource = typeof payload.permissionModeSource === "string" ? payload.permissionModeSource : null;
2070
+ const cursorPermissionModeNote = args.permissionModeOverride !== void 0 && agent === "cursor-agent" ? "\n note: cursor-agent has no --permission-mode equivalent; the requested override was ignored." : "";
2071
+ if (args.dryRun) {
2072
+ return {
2073
+ code: EXIT.OK,
2074
+ message: `${contextLine}
2075
+ [dry-run] would launch handoff resume for lane ${lane}
2076
+ agent: ${agent}
2077
+ model: ${modelDisplay} (source: ${modelSource})
2078
+ ` + (permissionMode ? ` permission: ${permissionMode} (source: ${permissionModeSource})
2079
+ ` : "") + ` prompt: ${prompt}` + cursorPermissionModeNote,
2080
+ payload: {
2081
+ ok: true,
2082
+ dryRun: true,
2083
+ lane,
2084
+ context: context ?? null,
2085
+ agent,
2086
+ model,
2087
+ modelDisplay,
2088
+ modelSource,
2089
+ permissionMode,
2090
+ permissionModeSource,
2091
+ prompt
2092
+ },
2093
+ isError: false
2094
+ };
2095
+ }
2096
+ if (payload.alreadyActive) {
2097
+ return {
2098
+ code: EXIT.OK,
2099
+ message: `${contextLine}
2100
+ handoff resume for lane ${lane} is already active (${String(payload.reason ?? "in_flight")}).
2101
+ re-open it anyway with: hs handoff ${lane} --force`,
2102
+ payload: { ok: true, lane, context: context ?? null, ...payload },
2103
+ isError: false
2104
+ };
2105
+ }
2106
+ return {
2107
+ code: EXIT.OK,
2108
+ message: `${contextLine}
2109
+ Launched handoff resume for lane ${lane} -> ${agent} (${model}).
2110
+ prompt: ${prompt}` + cursorPermissionModeNote,
2111
+ payload: { ok: true, lane, context: context ?? null, ...payload },
2112
+ isError: false
2113
+ };
2114
+ }
2115
+ async function runHandoffModel(args, deps) {
2116
+ const dryRunRes = await controlRequest(deps, {
2117
+ method: "POST",
2118
+ path: "/api/agent/start",
2119
+ body: { prompt: "/hs-handoff", launchUseCase: "handoff", dryRun: true }
2120
+ });
2121
+ if (!dryRunRes.ok) return dryRunRes.error;
2122
+ if (dryRunRes.status !== 200) return writeFailed(dryRunRes.status, dryRunRes.body);
2123
+ const current = dryRunRes.body ?? {};
2124
+ const currentModel = typeof current.model === "string" ? current.model : "?";
2125
+ const currentSource = typeof current.modelSource === "string" ? current.modelSource : "?";
2126
+ const currentAgent = typeof current.agent === "string" ? current.agent : "claude-code";
2127
+ const currentModelDisplay = typeof current.modelDisplay === "string" ? current.modelDisplay : currentModel;
2128
+ if (args.modelValue === void 0) {
2129
+ return {
2130
+ code: EXIT.OK,
2131
+ message: `handoff default model: ${currentModelDisplay} (source: ${currentSource}, agent: ${currentAgent})`,
2132
+ payload: {
2133
+ ok: true,
2134
+ model: currentModel,
2135
+ modelDisplay: currentModelDisplay,
2136
+ modelSource: currentSource,
2137
+ agent: currentAgent
2138
+ },
2139
+ isError: false
2140
+ };
2141
+ }
2142
+ const options = currentAgent === "cursor-agent" ? CURSOR_LAUNCH_MODEL_OPTIONS : CLAUDE_LAUNCH_MODEL_OPTIONS;
2143
+ if (!isKnownModelValue(args.modelValue, options)) {
2144
+ const validOptions = validModelOptionsList(options);
2145
+ return {
2146
+ code: EXIT.INVALID_ARG,
2147
+ message: `unknown model "${args.modelValue}" for agent ${currentAgent}. Valid options: ${validOptions.join(", ")}`,
2148
+ payload: {
2149
+ ok: false,
2150
+ error: "invalid_model",
2151
+ value: args.modelValue,
2152
+ agent: currentAgent,
2153
+ validOptions
2154
+ },
2155
+ isError: true
2156
+ };
2157
+ }
2158
+ const valueToStore = canonicalizeModelAlias(args.modelValue) ?? args.modelValue;
2159
+ const bucketField = currentAgent === "cursor-agent" ? "cursorAgent" : currentAgent === "dev-factory-fleet" ? "fleet" : "claudeCode";
2160
+ const res = await controlRequest(deps, {
2161
+ method: "PUT",
2162
+ path: "/api/settings",
2163
+ body: { launchModels: { handoff: { [bucketField]: valueToStore } } }
2164
+ });
2165
+ if (!res.ok) return res.error;
2166
+ if (res.status !== 200) return writeFailed(res.status, res.body);
2167
+ return {
2168
+ code: EXIT.OK,
2169
+ message: `handoff default model set -> ${valueToStore} (agent: ${currentAgent}).`,
2170
+ payload: { ok: true, model: valueToStore, agent: currentAgent },
2171
+ isError: false
2172
+ };
2173
+ }
2174
+ async function runHandoffPermissionMode(args, deps) {
2175
+ const dryRunRes = await controlRequest(deps, {
2176
+ method: "POST",
2177
+ path: "/api/agent/start",
2178
+ body: { prompt: "/hs-handoff", launchUseCase: "handoff", dryRun: true }
2179
+ });
2180
+ if (!dryRunRes.ok) return dryRunRes.error;
2181
+ if (dryRunRes.status !== 200) return writeFailed(dryRunRes.status, dryRunRes.body);
2182
+ const current = dryRunRes.body ?? {};
2183
+ const currentAgent = typeof current.agent === "string" ? current.agent : "claude-code";
2184
+ const currentMode = typeof current.permissionMode === "string" && current.permissionMode ? current.permissionMode : null;
2185
+ const currentSource = typeof current.permissionModeSource === "string" ? current.permissionModeSource : null;
2186
+ if (args.permissionModeValue === void 0) {
2187
+ return {
2188
+ code: EXIT.OK,
2189
+ message: currentMode ? `handoff default permission-mode: ${currentMode} (source: ${currentSource}, agent: ${currentAgent})` : `handoff default permission-mode: unset (agent: ${currentAgent}) \u2014 claude's own default prompting applies.`,
2190
+ payload: {
2191
+ ok: true,
2192
+ permissionMode: currentMode,
2193
+ permissionModeSource: currentSource,
2194
+ agent: currentAgent
2195
+ },
2196
+ isError: false
2197
+ };
2198
+ }
2199
+ if (currentAgent !== "claude-code") {
2200
+ return {
2201
+ code: EXIT.USAGE,
2202
+ message: `hs handoff permission-mode is not supported for agent ${currentAgent} yet (no --permission-mode equivalent). The active handoff agent must be claude-code.`,
2203
+ payload: { ok: false, error: "unsupported_agent", agent: currentAgent },
2204
+ isError: true
2205
+ };
2206
+ }
2207
+ if (!isKnownPermissionModeValue(args.permissionModeValue, CLAUDE_PERMISSION_MODE_OPTIONS)) {
2208
+ return {
2209
+ code: EXIT.INVALID_ARG,
2210
+ message: `unknown permission mode "${args.permissionModeValue}" for agent ${currentAgent}. Valid modes: ${CLAUDE_PERMISSION_MODE_OPTIONS.join(", ")}`,
2211
+ payload: {
2212
+ ok: false,
2213
+ error: "invalid_permission_mode",
2214
+ value: args.permissionModeValue,
2215
+ agent: currentAgent,
2216
+ validOptions: CLAUDE_PERMISSION_MODE_OPTIONS
2217
+ },
2218
+ isError: true
2219
+ };
2220
+ }
2221
+ const res = await controlRequest(deps, {
2222
+ method: "PUT",
2223
+ path: "/api/settings",
2224
+ body: { launchPermissions: { handoff: { claudeCode: args.permissionModeValue } } }
2225
+ });
2226
+ if (!res.ok) return res.error;
2227
+ if (res.status !== 200) return writeFailed(res.status, res.body);
2228
+ const bypassCaveat = args.permissionModeValue === "bypassPermissions" ? "\n caveat: bypassPermissions skips ALL permission prompts \u2014 trusted repos only." : "";
2229
+ return {
2230
+ code: EXIT.OK,
2231
+ message: `handoff default permission-mode set -> ${args.permissionModeValue} (agent: ${currentAgent}).${bypassCaveat}`,
2232
+ payload: { ok: true, permissionMode: args.permissionModeValue, agent: currentAgent },
2233
+ isError: false
2234
+ };
2235
+ }
2236
+ async function runHandoffDelete(lane, args, deps) {
2237
+ const path3 = `/api/control/handoff-context?lane=${encodeURIComponent(lane)}`;
2238
+ const read = await controlRequest(deps, { method: "GET", path: path3 });
2239
+ if (!read.ok) return read.error;
2240
+ if (read.status !== 200) return writeFailed(read.status, read.body);
2241
+ const existing = read.body?.context ?? null;
2242
+ if (!existing) {
2243
+ return {
2244
+ code: EXIT.NOT_FOUND,
2245
+ message: `no live lane ${lane} \u2014 nothing to delete.`,
2246
+ payload: { ok: false, error: "lane_not_found", lane },
2247
+ isError: true
2248
+ };
2249
+ }
2250
+ if (!args.yes) {
2251
+ return {
2252
+ code: EXIT.USAGE,
2253
+ message: `Would delete ${describeContext(existing)}
2254
+ blockers: ${existing.blockers?.length ?? 0} \xB7 links: ${existing.links?.length ?? 0}
2255
+ A lane holds coordination only, so this is re-recordable \u2014 but nothing
2256
+ is removed without the explicit confirm.
2257
+ delete it with: hs handoff delete ${lane} --yes`,
2258
+ payload: { ok: false, error: "confirm_required", lane, context: existing },
2259
+ isError: true
2260
+ };
2261
+ }
2262
+ const res = await controlRequest(deps, { method: "DELETE", path: path3 });
2263
+ if (!res.ok) return res.error;
2264
+ if (res.status !== 200) return writeFailed(res.status, res.body);
2265
+ return {
2266
+ code: EXIT.OK,
2267
+ message: `Deleted lane ${lane}.
2268
+ it held: ${describeContext(existing)}
2269
+ re-record it with: hs handoff record --lane ${lane} --spec ${existing.specId ?? "<id>"}`,
2270
+ payload: { ok: true, lane, deleted: existing },
2271
+ isError: false
2272
+ };
2273
+ }
2274
+ async function runHandoff(args, deps) {
2275
+ if (args.sub === "model") {
2276
+ return runHandoffModel(args, deps);
2277
+ }
2278
+ if (args.sub === "permission-mode") {
2279
+ return runHandoffPermissionMode(args, deps);
2280
+ }
2281
+ const id = resolveAgentIdentity(args.agent, args.session);
2282
+ const lane = args.lane?.trim() || id.sessionId;
2283
+ if (args.sub === "delete") {
2284
+ return runHandoffDelete(lane, args, deps);
2285
+ }
2286
+ if (args.sub === "load") {
2287
+ const path3 = args.all ? "/api/control/handoff-context" : `/api/control/handoff-context?lane=${encodeURIComponent(lane)}`;
2288
+ const res2 = await controlRequest(deps, { method: "GET", path: path3 });
2289
+ if (!res2.ok) return res2.error;
2290
+ if (res2.status !== 200) return writeFailed(res2.status, res2.body);
2291
+ if (args.all) {
2292
+ const contexts = res2.body?.contexts ?? [];
2293
+ const lines = contexts.length ? contexts.map((c) => ` ${describeContext(c)}`).join("\n") : " (no active lanes)";
2294
+ return {
2295
+ code: EXIT.OK,
2296
+ message: `${contexts.length} active lane(s):
2297
+ ${lines}`,
2298
+ payload: { ok: true, contexts },
2299
+ isError: false
2300
+ };
2301
+ }
2302
+ const context2 = res2.body?.context ?? null;
2303
+ if (args.launch) {
2304
+ if (!context2) {
2305
+ return {
2306
+ code: EXIT.NOT_FOUND,
2307
+ message: `no current handoff context for lane ${lane} \u2014 refusing to launch (empty or expired lane).`,
2308
+ payload: { ok: false, error: "lane_not_found", lane },
2309
+ isError: true
2310
+ };
2311
+ }
2312
+ return launchHandoff(lane, args, deps, context2);
2313
+ }
2314
+ return {
2315
+ code: EXIT.OK,
2316
+ message: describeContext(context2),
2317
+ payload: { ok: true, lane, context: context2 },
2318
+ isError: false
2319
+ };
2320
+ }
2321
+ let recordLane = lane;
2322
+ if (args.newCode) {
2323
+ const listed = await controlRequest(deps, {
2324
+ method: "GET",
2325
+ path: "/api/control/handoff-context"
2326
+ });
2327
+ if (!listed.ok) return listed.error;
2328
+ if (listed.status !== 200) return writeFailed(listed.status, listed.body);
2329
+ const occupied = new Set(
2330
+ (listed.body?.contexts ?? []).map((c) => String(c.laneKey ?? "").toUpperCase()).filter(Boolean)
2331
+ );
2332
+ recordLane = mintLaneCode(occupied);
2333
+ }
2334
+ const body = {
2335
+ lane_key: recordLane,
2336
+ agent: id.agent,
2337
+ ...args.specId !== void 0 ? { spec_id: args.specId } : {},
2338
+ ...args.phase !== void 0 ? { phase: args.phase } : {},
2339
+ ...args.estimatedTime !== void 0 ? { estimated_time: args.estimatedTime } : {},
2340
+ ...args.blockers.length ? { blockers: args.blockers } : {},
2341
+ ...args.links.length ? { links: args.links } : {}
2342
+ };
2343
+ const res = await controlRequest(deps, {
2344
+ method: "PUT",
2345
+ path: "/api/control/handoff-context",
2346
+ body
2347
+ });
2348
+ if (!res.ok) return res.error;
2349
+ if (res.status === 400) {
2350
+ const detail = envelopeMessage(res.body);
2351
+ return {
2352
+ code: EXIT.INVALID_ARG,
2353
+ message: `console rejected the request${detail ? `: ${detail}` : ""}`,
2354
+ payload: { ok: false, error: "invalid_request", message: detail },
2355
+ isError: true
2356
+ };
2357
+ }
2358
+ if (res.status !== 200) return writeFailed(res.status, res.body);
2359
+ const context = res.body?.context ?? null;
2360
+ if (args.newCode) {
2361
+ return {
2362
+ code: EXIT.OK,
2363
+ message: `Handoff code: ${recordLane}
2364
+ ${describeContext(context)}
2365
+ resume it with: hs handoff ${recordLane}`,
2366
+ payload: { ok: true, lane: recordLane, minted: true, context },
2367
+ isError: false
2368
+ };
2369
+ }
2370
+ return {
2371
+ code: EXIT.OK,
2372
+ message: `Handoff recorded \u2192 ${describeContext(context)}`,
2373
+ payload: { ok: true, lane, context },
2374
+ isError: false
2375
+ };
2376
+ }
2377
+ var HELP2 = [
2378
+ "Usage:",
2379
+ " hs handoff record [flags] Upsert this lane's current resume context",
2380
+ " hs handoff record --new-code [flags] Mint a fresh code, record it, print it",
2381
+ " hs handoff delete <CODE> [--yes] Drop one lane (prints it and refuses without --yes)",
2382
+ " hs handoff load [--lane <k>] [--all] Print the current resume context for a lane (never launches)",
2383
+ " hs handoff <CODE> [--dry-run] Resolve the lane, print its summary, and LAUNCH its resume (default)",
2384
+ " hs handoff <CODE> --no-launch Same resolution, print-only \u2014 the human peek, no launch",
2385
+ " hs handoff <CODE> --force Launch again even if this lane already has a window opening",
2386
+ " hs handoff load --lane <k> --launch [--dry-run] Spelled-out equivalent of the default-launch shorthand",
2387
+ " hs handoff <CODE> --model <value> One-off model override for this launch (--launch implied)",
2388
+ " hs handoff <CODE> --permission-mode <mode> One-off permission-mode override for this launch",
2389
+ " hs handoff model [<value>] Print (or set) the handoff use case's default model",
2390
+ " hs handoff permission-mode [<value>] Print (or set) the ACTIVE agent's default permission-mode",
2391
+ "",
2392
+ "`hs handoff record --new-code` MINTS the lane code rather than letting anyone",
2393
+ "invent one. Codes are 4 characters, uppercase letters and digits with the",
2394
+ "lookalikes dropped (no 0/O, no 1/I/L), always carrying a letter \u2014 the same",
2395
+ "shape every spec file key has, so a lane code reads like every other id you",
2396
+ "see. The mint checks the live lane list first, so it can never collide with a",
2397
+ 'lane already in flight. This replaces the old "pick a 4-character code and',
2398
+ 'collision-check it yourself" step, which is how a lane once ended up named',
2399
+ "FLEET.",
2400
+ "",
2401
+ "`hs handoff delete <CODE>` drops ONE lane. Rows already self-purge on a server",
2402
+ "TTL, so this is for the case a TTL cannot help with: a lane recorded under the",
2403
+ "wrong code, sitting in the listing next to the right one. There is no trash and",
2404
+ "no delete-all \u2014 a lane is coordination only (spec, phase, blockers, links), so",
2405
+ "the row printed back to you IS the undo: re-record it from what you see. The",
2406
+ "safe outcome is the default, so with no --yes it prints the lane and refuses.",
2407
+ "",
2408
+ "Wraps PUT/GET/DELETE /api/control/handoff-context (Neon control plane). Self-purging:",
2409
+ "`record` overwrites in place on the lane key (not append); rows expire on a",
2410
+ "server TTL. Coordination ONLY \u2014 durable knowledge goes to vault memory and is",
2411
+ "rejected by the endpoint.",
2412
+ "",
2413
+ "`hs handoff <CODE>` LAUNCHES by default (owner ruling 2026-09-01, [[HS-RVKN]]:",
2414
+ "why else would the command be typed if not to launch it?) \u2014 it resolves the",
2415
+ "lane FIRST (an empty/expired lane refuses before any launch call, exit 10),",
2416
+ "prints the lane summary so you see what you're resuming, then POSTs the SAME",
2417
+ "/api/agent/start the What's Next Launch button uses (launchUseCase: handoff)",
2418
+ "\u2014 never a second launcher, never a spawn from this CLI process itself.",
2419
+ "`--no-launch` skips the launch and prints the summary only \u2014 the machine-safe",
2420
+ "equivalent is the spelled-out `hs handoff load --lane <CODE>` (no --launch),",
2421
+ "which never defaults to launching. `--launch` on the shorthand is a harmless",
2422
+ "explicit alias (already the default). `--dry-run` prints the resolved",
2423
+ "agent/model/prompt without spawning, and composes with the shorthand exactly",
2424
+ "like `--model`/`--permission-mode` \u2014 none of the three require an explicit",
2425
+ "`--launch` on the bare-code form any more. Not valid on `record` or with",
2426
+ "`--all`.",
2427
+ "",
2428
+ "One lane opens ONE window: the route holds a 5-minute in-flight marker per",
2429
+ "lane, so a repeat `hs handoff <CODE>` prints `already active (in_flight)`",
2430
+ "instead of spawning a second terminal. That is what stops a launched session",
2431
+ "which re-runs the bare form from opening an endless chain of windows. Pass",
2432
+ "`--force` when you actually want the window re-opened.",
2433
+ "",
2434
+ `\`hs handoff model <value>\` accepts an exact model id (e.g. ${ANTHROPIC2.PREVIOUS_STRONG},`,
2435
+ "pinned byte-for-byte) OR a version-less alias that tracks the latest of a",
2436
+ "family as Claude updates it: claude-opus, claude-sonnet, claude-fable,",
2437
+ "claude-haiku, or the bare opus/sonnet/fable/haiku forms. An alias is stored",
2438
+ "in its canonical claude-<family> form and resolved at launch/dry-run time \u2014",
2439
+ "`hs handoff model` (no value) and `--dry-run` show both the tracking form",
2440
+ "and what it resolves to right now.",
2441
+ "",
2442
+ "`--model <value>` on a launch overrides the resolved model for THIS launch",
2443
+ "only, above the spec/settings default \u2014 the stored `hs handoff model`",
2444
+ "default is untouched. Same vocabulary as `hs handoff model <value>` (exact",
2445
+ "id or alias); unknown values are refused before any request is sent. Not",
2446
+ "valid without launching (bare-code default, or spelled-out --launch), or on",
2447
+ "record/model.",
2448
+ "",
2449
+ "`--permission-mode <mode>` on a launch overrides the resolved",
2450
+ "`claude --permission-mode` for THIS launch only, above the per-agent",
2451
+ "`launchPermissions.handoff.{claudeCode,cursorAgent}` settings default (set",
2452
+ `via \`hs handoff permission-mode <value>\`, not this flag). Valid modes: ${ALL_LAUNCH_PERMISSION_MODE_OPTIONS.join(", ")}`,
2453
+ "(the exact `claude --help` vocabulary) \u2014 unknown values are refused before",
2454
+ "any request is sent. Not valid without launching, or on record/model. Unset",
2455
+ "(no flag, no settings default) means no --permission-mode reaches the",
2456
+ "spawned claude CLI \u2014 its own default prompting mode. bypassPermissions",
2457
+ "means the spawned session skips ALL permission prompts \u2014 trusted repos",
2458
+ "only. cursor-agent has no equivalent flag; a requested override is ignored",
2459
+ "with a printed note.",
2460
+ "",
2461
+ "`hs handoff permission-mode [<value>]` ([[HS-TG56]]) prints (no value) or",
2462
+ "sets (a value) the DEFAULT `--permission-mode` for the ACTIVE handoff agent",
2463
+ "\u2014 `launchPermissions.handoff.claudeCode`, written via PUT /api/settings,",
2464
+ "using the SAME dry-run resolution `model`/`--dry-run` use to find the active",
2465
+ "agent. Validated against that agent's own vocabulary: claude-code accepts",
2466
+ `${CLAUDE_PERMISSION_MODE_OPTIONS.join(", ")}`,
2467
+ "; cursor-agent has none yet, so a cursor-active session refuses the set with",
2468
+ "a plain not-supported-yet message and writes nothing. An unknown value on",
2469
+ "claude-code is refused with that list, writing nothing. Setting",
2470
+ "bypassPermissions prints a one-line trusted-repos caveat after the",
2471
+ 'confirmation. No value with nothing configured prints "unset" plainly \u2014',
2472
+ "claude's own default prompting mode applies.",
2473
+ "",
2474
+ "Flags:",
2475
+ " --lane <key> Lane identity (default: the detected session id).",
2476
+ " --agent <name> Writing agent (default: detected identity).",
2477
+ " --session <id> Session id (default: detected identity; also default lane).",
2478
+ " --spec <id> Current spec id (record).",
2479
+ ' --phase <label> Short phase label, e.g. "slice" (record).',
2480
+ ' --time <est> Human time estimate, e.g. "~2h" (record).',
2481
+ " --blocker <line> A coordination blocker; repeatable (record).",
2482
+ " --link <id> A relevant spec id/link; repeatable (record).",
2483
+ " --all List every active lane (load).",
2484
+ " --new-code Mint the lane code instead of naming one (record only).",
2485
+ " --yes, -y Confirm a delete. Without it, delete prints and refuses.",
2486
+ " --launch Launch the resolved lane's resume session. Default true on the",
2487
+ " bare-code shorthand (this flag is then a harmless no-op alias);",
2488
+ " opt-in on the spelled-out `load` form.",
2489
+ " --no-launch Bare-code shorthand only: print the resolved summary, don't launch.",
2490
+ " --dry-run Resolve + print the launch target, don't spawn.",
2491
+ " --model <value> One-off model override for this launch only.",
2492
+ " --permission-mode <mode> One-off permission-mode override for this launch only.",
2493
+ " --json Emit a machine-readable JSON result.",
2494
+ " --help, -h Show this help.",
2495
+ "",
2496
+ "Exit codes: 0 ok \xB7 1 usage \xB7 10 not-found (empty/expired lane) \xB7 11 bad-arg \xB7",
2497
+ "12 console-unreachable \xB7 13 server-error \xB7 14 neon-required"
2498
+ ].join("\n");
2499
+
2500
+ // spex-console/bin/run-entry.ts
2501
+ import process2 from "node:process";
2502
+ var BinExit = class extends Error {
2503
+ code;
2504
+ constructor(code) {
2505
+ super(`exit ${code}`);
2506
+ this.name = "BinExit";
2507
+ this.code = code;
2508
+ }
2509
+ };
2510
+ function runEntry(name, main2, options = {}) {
2511
+ const { fallbackCode = 1, unhandledLabel = "unhandled" } = options;
2512
+ void (async () => {
2513
+ try {
2514
+ await main2();
2515
+ } catch (err) {
2516
+ if (err instanceof BinExit) {
2517
+ process2.exitCode = err.code;
2518
+ return;
2519
+ }
2520
+ const message = err instanceof Error ? err.message : String(err);
2521
+ process2.stderr.write(`${name}: ${unhandledLabel}: ${message}
2522
+ `);
2523
+ process2.exitCode = fallbackCode;
2524
+ }
2525
+ })();
2526
+ }
2527
+
2528
+ // spex-console/bin/hs-handoff.ts
2529
+ function readDiscovery(p) {
2530
+ try {
2531
+ return readFileSync10(p, "utf-8");
2532
+ } catch {
2533
+ return null;
2534
+ }
2535
+ }
2536
+ async function main() {
2537
+ const vaultRoot = process3.env.HS_VAULT_ROOT;
2538
+ if (!vaultRoot) {
2539
+ process3.stderr.write(
2540
+ "hs handoff: HS_VAULT_ROOT is not set (the dispatcher must resolve and pass it).\n"
2541
+ );
2542
+ process3.exitCode = EXIT.USAGE;
2543
+ return;
2544
+ }
2545
+ const parsed = parseHandoffArgs(process3.argv.slice(3));
2546
+ if (parsed.help) {
2547
+ process3.stdout.write(`${HELP2}
2548
+ `);
2549
+ process3.exitCode = EXIT.OK;
2550
+ return;
2551
+ }
2552
+ if (parsed.error) {
2553
+ process3.stderr.write(`hs handoff: ${parsed.error.message}
2554
+ `);
2555
+ process3.exitCode = parsed.error.code;
2556
+ return;
2557
+ }
2558
+ const consoleUrl = resolveSpexConsoleUrl(vaultRoot, readDiscovery);
2559
+ const scoped = resolveControlScopeFromMachine(vaultRoot);
2560
+ if (!scoped.ok) {
2561
+ process3.stderr.write(`hs handoff: ${scoped.error.message}
2562
+ `);
2563
+ process3.exitCode = scoped.error.code;
2564
+ return;
2565
+ }
2566
+ const result = await runHandoff(parsed.args, {
2567
+ fetchImpl: fetch,
2568
+ consoleUrl,
2569
+ scope: scoped.scope
2570
+ });
2571
+ const sink = result.isError ? process3.stderr : process3.stdout;
2572
+ sink.write(`${parsed.args.json ? JSON.stringify(result.payload) : result.message}
2573
+ `);
2574
+ process3.exitCode = result.code;
2575
+ }
2576
+ runEntry("hs handoff", main, { fallbackCode: EXIT.WRITE_FAILED });