harness-dispatch 0.7.9 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/CHANGELOG.md +657 -1
  2. package/README.md +25 -4
  3. package/config.default.yaml +30 -2
  4. package/dist/auth.d.ts +29 -0
  5. package/dist/auth.d.ts.map +1 -1
  6. package/dist/auth.js +58 -5
  7. package/dist/auth.js.map +1 -1
  8. package/dist/bin.d.ts.map +1 -1
  9. package/dist/bin.js +404 -20
  10. package/dist/bin.js.map +1 -1
  11. package/dist/breaker-store.d.ts +25 -1
  12. package/dist/breaker-store.d.ts.map +1 -1
  13. package/dist/breaker-store.js +193 -25
  14. package/dist/breaker-store.js.map +1 -1
  15. package/dist/client-register.d.ts +147 -0
  16. package/dist/client-register.d.ts.map +1 -0
  17. package/dist/client-register.js +380 -0
  18. package/dist/client-register.js.map +1 -0
  19. package/dist/config/validation.d.ts +29 -0
  20. package/dist/config/validation.d.ts.map +1 -1
  21. package/dist/config/validation.js +88 -0
  22. package/dist/config/validation.js.map +1 -1
  23. package/dist/config.d.ts.map +1 -1
  24. package/dist/config.js +55 -2
  25. package/dist/config.js.map +1 -1
  26. package/dist/dispatch-log.d.ts +14 -0
  27. package/dist/dispatch-log.d.ts.map +1 -1
  28. package/dist/dispatch-log.js +3 -0
  29. package/dist/dispatch-log.js.map +1 -1
  30. package/dist/dispatchers/openai-compatible.d.ts +20 -4
  31. package/dist/dispatchers/openai-compatible.d.ts.map +1 -1
  32. package/dist/dispatchers/openai-compatible.js +145 -17
  33. package/dist/dispatchers/openai-compatible.js.map +1 -1
  34. package/dist/http/answer-stream.d.ts +52 -0
  35. package/dist/http/answer-stream.d.ts.map +1 -0
  36. package/dist/http/answer-stream.js +47 -0
  37. package/dist/http/answer-stream.js.map +1 -0
  38. package/dist/http/parse.d.ts.map +1 -1
  39. package/dist/http/parse.js +41 -0
  40. package/dist/http/parse.js.map +1 -1
  41. package/dist/http/server.d.ts.map +1 -1
  42. package/dist/http/server.js +102 -9
  43. package/dist/http/server.js.map +1 -1
  44. package/dist/jobs/store.d.ts.map +1 -1
  45. package/dist/jobs/store.js +45 -4
  46. package/dist/jobs/store.js.map +1 -1
  47. package/dist/jobs.d.ts +24 -0
  48. package/dist/jobs.d.ts.map +1 -1
  49. package/dist/jobs.js +101 -1
  50. package/dist/jobs.js.map +1 -1
  51. package/dist/mcp/config-hot-reload.d.ts.map +1 -1
  52. package/dist/mcp/config-hot-reload.js +10 -0
  53. package/dist/mcp/config-hot-reload.js.map +1 -1
  54. package/dist/mcp/near-miss-guard.d.ts +45 -0
  55. package/dist/mcp/near-miss-guard.d.ts.map +1 -0
  56. package/dist/mcp/near-miss-guard.js +98 -0
  57. package/dist/mcp/near-miss-guard.js.map +1 -0
  58. package/dist/mcp/server.d.ts.map +1 -1
  59. package/dist/mcp/server.js +36 -0
  60. package/dist/mcp/server.js.map +1 -1
  61. package/dist/mcp/tool-schemas.d.ts.map +1 -1
  62. package/dist/mcp/tool-schemas.js +21 -4
  63. package/dist/mcp/tool-schemas.js.map +1 -1
  64. package/dist/mcp/tools.d.ts +22 -0
  65. package/dist/mcp/tools.d.ts.map +1 -1
  66. package/dist/mcp/tools.js +38 -0
  67. package/dist/mcp/tools.js.map +1 -1
  68. package/dist/mcp-clients.d.ts +63 -0
  69. package/dist/mcp-clients.d.ts.map +1 -0
  70. package/dist/mcp-clients.js +117 -0
  71. package/dist/mcp-clients.js.map +1 -0
  72. package/dist/near-miss.d.ts +63 -0
  73. package/dist/near-miss.d.ts.map +1 -0
  74. package/dist/near-miss.js +132 -0
  75. package/dist/near-miss.js.map +1 -0
  76. package/dist/quota.d.ts +31 -1
  77. package/dist/quota.d.ts.map +1 -1
  78. package/dist/quota.js +68 -6
  79. package/dist/quota.js.map +1 -1
  80. package/dist/route-policy.d.ts +10 -0
  81. package/dist/route-policy.d.ts.map +1 -1
  82. package/dist/route-policy.js +20 -2
  83. package/dist/route-policy.js.map +1 -1
  84. package/dist/router.d.ts +61 -2
  85. package/dist/router.d.ts.map +1 -1
  86. package/dist/router.js +156 -29
  87. package/dist/router.js.map +1 -1
  88. package/dist/safety.d.ts.map +1 -1
  89. package/dist/safety.js +31 -1
  90. package/dist/safety.js.map +1 -1
  91. package/dist/status.d.ts +30 -21
  92. package/dist/status.d.ts.map +1 -1
  93. package/dist/status.js +152 -8
  94. package/dist/status.js.map +1 -1
  95. package/dist/types.d.ts +25 -1
  96. package/dist/types.d.ts.map +1 -1
  97. package/dist/working-dir.d.ts.map +1 -1
  98. package/dist/working-dir.js +16 -0
  99. package/dist/working-dir.js.map +1 -1
  100. package/dist/workspace-resolve.d.ts.map +1 -1
  101. package/dist/workspace-resolve.js +119 -15
  102. package/dist/workspace-resolve.js.map +1 -1
  103. package/dist/workspaces.d.ts +83 -0
  104. package/dist/workspaces.d.ts.map +1 -1
  105. package/dist/workspaces.js +288 -15
  106. package/dist/workspaces.js.map +1 -1
  107. package/package.json +1 -1
package/dist/bin.js CHANGED
@@ -5,12 +5,14 @@
5
5
  import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
6
6
  import { promises as fs } from "node:fs";
7
7
  import path from "node:path";
8
+ import { fileURLToPath } from "node:url";
8
9
  import { parseArgs } from "node:util";
9
10
  import { ensureHttpToken, maskToken, readHttpToken, rotateHttpToken, tokenPath } from "./auth.js";
10
11
  import { AUTO_DETECT_COMMANDS, loadConfig, resolveConfigPath } from "./config.js";
11
12
  import { LeaderboardCache } from "./leaderboard.js";
12
13
  import { VERSION } from "./version.js";
13
14
  import { commandAvailable } from "./dispatchers/shared/which-available.js";
15
+ import { inspectClientEntries } from "./mcp-clients.js";
14
16
  import { buildDispatchers } from "./mcp/dispatcher-factory.js";
15
17
  import { startMcpServer } from "./mcp/server.js";
16
18
  import { initObservability } from "./observability/index.js";
@@ -21,6 +23,7 @@ import { startHttpServer } from "./http/server.js";
21
23
  import { billingIsBlocked, buildRouteBilling } from "./billing.js";
22
24
  import { effectiveSafetyProfile } from "./safety.js";
23
25
  import { configToYaml } from "./configure-yaml.js";
26
+ import { devLaunchCommand, planClientWrites, removeClientEntry, writeClientEntry, } from "./client-register.js";
24
27
  import { stateRoot } from "./state-dir.js";
25
28
  async function buildRuntime(configPath) {
26
29
  const config = await loadConfig(configPath);
@@ -39,6 +42,8 @@ function printUsage() {
39
42
  "Usage:",
40
43
  " harness-dispatch Start stdio MCP.",
41
44
  " harness-dispatch configure [--print] Detect and prepare harness config.",
45
+ " harness-dispatch connect Register this server with the MCP clients you have.",
46
+ " harness-dispatch connect --remove Take the entry back out again.",
42
47
  " harness-dispatch doctor [--json] Check install, config, auth, and routes.",
43
48
  " harness-dispatch doctor --live Run one routed probe when billing policy allows it.",
44
49
  " harness-dispatch doctor --live --allow-paid Run a live probe through paid/unknown routes.",
@@ -46,6 +51,7 @@ function printUsage() {
46
51
  " harness-dispatch status --watch Re-render status every --interval ms.",
47
52
  " harness-dispatch usage [--json] Show per-route call counts, quota, and billing kind.",
48
53
  " harness-dispatch serve [--port 3333] Serve MCP at /mcp and REST at /v1/*.",
54
+ ' harness-dispatch dispatch "<prompt>" Route one task and print the result.',
49
55
  " harness-dispatch auth show Print the HTTP bearer token.",
50
56
  " harness-dispatch auth rotate Rotate the HTTP bearer token.",
51
57
  "",
@@ -58,7 +64,15 @@ function printUsage() {
58
64
  " --print configure: print generated config YAML without writing it.",
59
65
  " --yes configure: write config.yaml instead of only previewing it.",
60
66
  " --force configure: overwrite an existing config file.",
67
+ " --clients <ids> connect: comma-separated client ids, instead of prompting.",
68
+ " --no-clients configure: skip the offer to register with clients.",
69
+ " --remove connect: remove the entry rather than write it.",
70
+ " --dev connect: point clients at THIS checkout's build, not the package.",
61
71
  " --allow-paid Allow doctor --live to probe paid or unknown-paid routes.",
72
+ " --service <id> dispatch: run exactly this route, no fallback to others.",
73
+ " --safety <profile> dispatch: read_only | workspace_edit | full_auto.",
74
+ " --task-type <type> dispatch: execute | plan | review | local.",
75
+ " --no-fallback dispatch: do not retry on another route if the first fails.",
62
76
  " -h, --help Show help.",
63
77
  " -v, --version Print the version and exit.",
64
78
  "",
@@ -125,22 +139,224 @@ async function cmdConfigure(configPath, explicitConfigPath, opts) {
125
139
  "elsewhere, or --force to overwrite it deliberately.\n");
126
140
  return 1;
127
141
  }
128
- await fs.writeFile(target, yamlText, "utf-8");
142
+ // 0600, because this file can contain a LITERAL api_key: `apiKeyForYaml`
143
+ // deliberately preserves one rather than replacing it with a `${VAR}`
144
+ // reference, so `configure` can write a real credential to disk. It was
145
+ // written with default permissions — 0644 under a typical umask — in a
146
+ // module family that is careful everywhere else (leaderboard 0600, state dir
147
+ // 0700, client-register carries the original file's mode). Found by an
148
+ // acceptance pass from reading, POSIX-only and not reproduced on Windows,
149
+ // where the mode is ignored.
150
+ //
151
+ // Applied on create only: `writeFile`'s mode does not change an existing
152
+ // file's permissions, so re-running `configure` will not silently tighten a
153
+ // file the user deliberately made group-readable.
154
+ await fs.writeFile(target, yamlText, { encoding: "utf-8", mode: 0o600 });
129
155
  const absoluteTarget = path.resolve(target);
130
156
  process.stdout.write(`Wrote ${target}.\n`);
131
- process.stdout.write("MCP snippet (uses an absolute --config path so it resolves correctly no matter what\n" +
157
+ // The last step of setup used to be "here is some JSON, paste it somewhere".
158
+ // Nobody owned the result, and the paths in it later moved — which is how one
159
+ // machine ended up running a dead entry, a dead hook, and a third client
160
+ // reading a different config, all silently. Offer to do it instead.
161
+ if (!opts.noClients) {
162
+ process.stdout.write("\n");
163
+ return cmdConnect(configPath, {
164
+ clients: opts.clients,
165
+ remove: false,
166
+ yes: false,
167
+ force: false,
168
+ // Setup registers the installed package. A checkout entry is a
169
+ // deliberate choice made by someone who knows they have a checkout, not
170
+ // something to infer during first-run setup.
171
+ dev: false,
172
+ });
173
+ }
174
+ process.stdout.write("\nMCP snippet (uses an absolute --config path so it resolves correctly no matter what\n" +
132
175
  "directory the MCP client launches from — a relative path or none at all silently\n" +
133
176
  "falls back to the shipped defaults, ignoring every edit you make to this file):\n");
134
- process.stdout.write(JSON.stringify({
135
- mcpServers: {
136
- "harness-dispatch": {
137
- command: "harness-dispatch",
138
- args: ["--config", absoluteTarget],
139
- },
140
- },
141
- }, null, 2) + "\n");
177
+ printMcpSnippet({ command: "harness-dispatch", args: ["--config", absoluteTarget] });
178
+ process.stdout.write("Or let `harness-dispatch connect` write it for you.\n");
142
179
  return 0;
143
180
  }
181
+ /** The entry, in the shape a client's config file wants it pasted. */
182
+ function printMcpSnippet(entry) {
183
+ if (entry === undefined)
184
+ return;
185
+ process.stdout.write(JSON.stringify({ mcpServers: { "harness-dispatch": entry } }, null, 2) + "\n");
186
+ }
187
+ /**
188
+ * Register this server with the MCP clients on this machine — the last step of
189
+ * setup, which used to be "paste this snippet somewhere".
190
+ *
191
+ * Interactive when a human is at a terminal, flag-driven otherwise. A prompt
192
+ * that blocks is right in front of a person and wrong in CI, so with no TTY
193
+ * and no `--clients` this reports what it WOULD do and writes nothing, rather
194
+ * than hanging or guessing.
195
+ */
196
+ async function cmdConnect(configPath, opts) {
197
+ const target = path.resolve(configPath ?? "config.yaml");
198
+ if (!existsSync(target) && !opts.remove) {
199
+ process.stderr.write(`connect: no config at ${target}. Run \`harness-dispatch configure --yes\` first —\n` +
200
+ "a client entry pointing at a config that does not exist is the failure this\n" +
201
+ "command exists to prevent.\n");
202
+ return 1;
203
+ }
204
+ // `import.meta.url` is this running file — dist/bin.js for an installed or
205
+ // built copy. "The build you are running now" is the only honest answer to
206
+ // which checkout --dev means, and it needs no flag value to get wrong.
207
+ const selfPath = fileURLToPath(import.meta.url);
208
+ const plans = planClientWrites(target, opts.dev ? { command: devLaunchCommand(selfPath) } : {});
209
+ if (opts.dev && !opts.remove) {
210
+ process.stdout.write(`Development entry: clients will launch ${selfPath} directly.\n` +
211
+ "That is an absolute path — it stops working, silently, if this directory is\n" +
212
+ "renamed or deleted. `harness-dispatch doctor` fails when that happens.\n\n");
213
+ }
214
+ const known = new Set(plans.map((p) => p.id));
215
+ const requested = opts.clients
216
+ ?.split(",")
217
+ .map((s) => s.trim())
218
+ .filter(Boolean);
219
+ const unknown = requested?.filter((id) => !known.has(id)) ?? [];
220
+ if (unknown.length > 0) {
221
+ process.stderr.write(`connect: unknown client${unknown.length > 1 ? "s" : ""}: ${unknown.join(", ")}. ` +
222
+ `Known: ${[...known].join(", ")}.\n`);
223
+ return 1;
224
+ }
225
+ const installed = plans.filter((p) => p.state !== "absent");
226
+ if (installed.length === 0) {
227
+ process.stdout.write("No MCP clients found on this machine (looked for Claude Code and Cursor).\n" +
228
+ "Nothing to register. Add this to whichever client you use, then re-run\n" +
229
+ "`harness-dispatch connect` if you install one of the two above:\n");
230
+ // Setup has to end with something you can act on. Without this, a machine
231
+ // with no client detected got a cheerful "nothing to do" and no way to
232
+ // finish wiring anything up — the snippet was the ONLY output this replaced.
233
+ printMcpSnippet(plans[0]?.desired);
234
+ return 0;
235
+ }
236
+ if (!opts.remove) {
237
+ // Stated once, up front, rather than only inside the diff for entries that
238
+ // happen to differ. What gets written should not be something you can only
239
+ // learn from a client being in a particular state.
240
+ process.stdout.write(`Entry to write: ${JSON.stringify(plans[0].desired)}\n\n`);
241
+ }
242
+ process.stdout.write(`${opts.remove ? "Removing from" : "Registering with"} clients:\n`);
243
+ for (const p of installed) {
244
+ process.stdout.write(` ${p.client.padEnd(12)} ${p.file} (${describeState(p.state, opts.remove)})\n`);
245
+ if (p.state === "differs") {
246
+ process.stdout.write(` currently: ${JSON.stringify(summariseEntry(p.current))}\n`);
247
+ }
248
+ }
249
+ const chosen = requested
250
+ ? installed.filter((p) => requested.includes(p.id))
251
+ : await chooseInteractively(installed, opts);
252
+ // Consent to replace a HAND-EDITED entry comes from ANSWERING the
253
+ // interactive prompt, which shows the difference first, or from --force.
254
+ //
255
+ // Neither `--clients` nor `--yes` counts. `--clients` says which client, not
256
+ // "overwrite whatever I put there"; `--yes` skips the question rather than
257
+ // answering it. The first version of this gate accepted "no client named" as
258
+ // consent, which let `--yes` through — the same class it was written to
259
+ // close, one flag over.
260
+ const prompted = requested === undefined && !opts.yes && process.stdin.isTTY === true;
261
+ const consented = opts.force || prompted;
262
+ if (chosen === undefined) {
263
+ process.stdout.write("Nothing written.\n");
264
+ return 0;
265
+ }
266
+ const stamp = new Date().toISOString().replace(/[:.]/g, "-");
267
+ let failed = false;
268
+ let wrote = false;
269
+ for (const plan of chosen) {
270
+ const outcome = opts.remove
271
+ ? await removeClientEntry(plan, { stamp, force: opts.force })
272
+ : await writeClientEntry(plan, { stamp, consented });
273
+ if (outcome.action === "written") {
274
+ wrote = true;
275
+ process.stdout.write(`${opts.remove ? "Removed from" : "Wrote"} ${outcome.client} (backup: ${outcome.backupPath})\n`);
276
+ }
277
+ else if (outcome.action === "unchanged") {
278
+ process.stdout.write(`${outcome.client}: ${opts.remove ? "no entry of ours to remove" : "already correct"}, nothing changed.\n`);
279
+ }
280
+ else {
281
+ failed = true;
282
+ process.stderr.write(`Skipped ${outcome.client}: ${outcome.reason}\n`);
283
+ }
284
+ }
285
+ // Only when something actually changed. Telling someone to restart an
286
+ // application after a run that wrote nothing is advice with no cause.
287
+ if (wrote && !failed) {
288
+ process.stdout.write(`\nRestart the client(s) so they pick up the ${opts.remove ? "removal" : "new server"}.\n`);
289
+ }
290
+ return failed ? 1 : 0;
291
+ }
292
+ /**
293
+ * The listing describes state from the point of view of what is about to
294
+ * happen. Under `--remove`, "already registered correctly" describes the entry
295
+ * accurately and reads as nonsense under the heading "Removing from clients",
296
+ * where the same state means "this is the one that will go".
297
+ */
298
+ function describeState(state, removing) {
299
+ if (removing) {
300
+ return {
301
+ absent: "not installed",
302
+ unreadable: "config does not parse — will be left alone",
303
+ "missing-entry": "no entry of ours to remove",
304
+ matches: "our entry is here — will be removed",
305
+ differs: "has an entry we did not write — left alone unless --force",
306
+ }[state];
307
+ }
308
+ return {
309
+ absent: "not installed",
310
+ unreadable: "config does not parse — will be left alone",
311
+ "missing-entry": "no harness-dispatch entry yet",
312
+ matches: "already registered correctly",
313
+ differs: "has a DIFFERENT entry",
314
+ }[state];
315
+ }
316
+ /** An entry's shape without its `env`, which holds live API keys on real machines. */
317
+ function summariseEntry(entry) {
318
+ if (!entry || typeof entry !== "object")
319
+ return entry;
320
+ const { env: _hidden, ...rest } = entry;
321
+ return rest;
322
+ }
323
+ /**
324
+ * Ask, when there is someone to ask.
325
+ *
326
+ * Returns undefined for "write nothing". An entry that already differs is the
327
+ * case that most needs a human: on the machine this was written for, the
328
+ * differing entry was the WORKING one.
329
+ */
330
+ async function chooseInteractively(plans, opts) {
331
+ const actionable = plans.filter((p) => p.state !== "matches" && p.state !== "unreadable");
332
+ if (actionable.length === 0)
333
+ return [];
334
+ // `--yes` skips the question; it does NOT answer it.
335
+ //
336
+ // The consent gate added for `--clients` treated "no client named" as
337
+ // "consented", and `--yes` takes that path — so an acceptance pass measured
338
+ // `connect --yes` overwriting a hand-edited entry with no prompt and no
339
+ // --force, exit 0, while the same flags on `--remove` correctly refused.
340
+ // "Do not ask me" is not the same answer as "yes, replace what I wrote".
341
+ if (opts.yes)
342
+ return actionable;
343
+ if (!process.stdin.isTTY) {
344
+ process.stdout.write("\nNot a terminal, so nothing was written. Re-run with --clients " +
345
+ `${actionable.map((p) => p.id).join(",")} (or --yes) to apply.\n`);
346
+ return undefined;
347
+ }
348
+ const { createInterface } = await import("node:readline/promises");
349
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
350
+ try {
351
+ const answer = (await rl.question(`\nApply to ${actionable.length} client(s)? [y/N] `))
352
+ .trim()
353
+ .toLowerCase();
354
+ return answer === "y" || answer === "yes" ? actionable : undefined;
355
+ }
356
+ finally {
357
+ rl.close();
358
+ }
359
+ }
144
360
  async function cmdStatus(configPath, opts) {
145
361
  const runtime = await buildRuntime(configPath);
146
362
  const render = async () => {
@@ -232,6 +448,50 @@ async function cmdDoctor(configPath, opts) {
232
448
  ok: Object.keys(runtime.config.services).length > 0,
233
449
  detail: `${Object.keys(runtime.config.services).length} configured route(s)`,
234
450
  },
451
+ // This one DOES fail, unlike the advisory git check below.
452
+ //
453
+ // A client entry naming a path that is not there is not a preference or a
454
+ // missing optional tool — there is no setup in which it is intended. And
455
+ // it is invisible from the client side: one that cannot spawn its server
456
+ // simply has no tools, which looks identical to never having installed
457
+ // anything. On this maintainer's machine that state survived a repo rename
458
+ // by months, silently, along with a hook pointing at the same dead
459
+ // directory.
460
+ //
461
+ // Not-configured is NOT a failure: a machine with no client entry gets
462
+ // "not registered with any client", ok. Only a broken one fails.
463
+ (() => {
464
+ const entries = inspectClientEntries();
465
+ const broken = entries.filter((e) => e.missingPaths.length > 0);
466
+ if (entries.length === 0) {
467
+ return {
468
+ name: "mcp-clients",
469
+ ok: true,
470
+ detail: "not registered with any MCP client this tool knows how to read " +
471
+ "(Claude Code, Cursor) — run `harness-dispatch connect` to register it",
472
+ };
473
+ }
474
+ return {
475
+ name: "mcp-clients",
476
+ ok: broken.length === 0,
477
+ detail: broken.length === 0
478
+ ? entries.map((e) => `${e.client}: ${e.entry} resolves`).join("; ")
479
+ : broken
480
+ .map((e) =>
481
+ // "references", not "launches ... from": missingPaths
482
+ // holds any path the entry names that is not there, and
483
+ // that is routinely the `--config` argument rather than
484
+ // the launch binary. An acceptance pass found the message
485
+ // reaching the right conclusion by the wrong description —
486
+ // a missing explicit --config does make the CLI exit 1,
487
+ // but it is not where the client launches from.
488
+ `${e.client} (${e.file}) references a path that does not exist: ` +
489
+ `${e.missingPaths.join(", ")} (entry: ${e.entry}) — that client has been ` +
490
+ "getting NO tools from this server, silently. `harness-dispatch connect` " +
491
+ "rewrites the entry.")
492
+ .join(" | "),
493
+ };
494
+ })(),
235
495
  // Not required to dispatch — reported, never a hard fail.
236
496
  //
237
497
  // The `workspace` tool shells out to git for diff/apply, so without it a
@@ -267,6 +527,19 @@ async function cmdDoctor(configPath, opts) {
267
527
  ? runtime.config.configWarnings.join(" | ")
268
528
  : "no unrecognized config entries",
269
529
  },
530
+ {
531
+ // Saved state that could not be read, which is NOT a config problem and
532
+ // so never reached this list: `doctor` read `configWarnings` directly
533
+ // and stayed silent about an unreadable breaker record or usage counters
534
+ // that are not reaching disk. Two acceptance passes recorded that
535
+ // silence as an open item; `status` grew a "State problems" heading and
536
+ // `doctor` did not follow.
537
+ name: "saved-state",
538
+ ok: (status.stateWarnings?.length ?? 0) === 0,
539
+ detail: status.stateWarnings && status.stateWarnings.length > 0
540
+ ? status.stateWarnings.join(" | ")
541
+ : "readable",
542
+ },
270
543
  {
271
544
  name: "routes",
272
545
  ok: status.ready.length > 0,
@@ -393,18 +666,50 @@ async function cmdAuth(action) {
393
666
  return 1;
394
667
  }
395
668
  }
396
- async function cmdRouteAlias(prompt, configPath) {
669
+ /**
670
+ * One dispatch from the command line — the CLI half of the `dispatch` MCP
671
+ * tool, named to match it (`route` stays as an alias).
672
+ *
673
+ * It took flags because it had none and that made it unusable for the one job
674
+ * it is most needed for. An acceptance pass has to exercise the build IN THE
675
+ * WORKING TREE; the MCP tool runs in whatever server process is already
676
+ * connected, which is a different artifact from a different moment. So the CLI
677
+ * is the honest path there — and it hardcoded taskType "execute" with two
678
+ * fallbacks, meaning a pass asking for one read-only call on one route could
679
+ * silently get an execute-profile run on up to three. The first acceptance
680
+ * pass to attempt a live dispatch wrote its own Node script against dist/
681
+ * rather than use this, which is the tell.
682
+ */
683
+ async function cmdDispatch(prompt, configPath, opts) {
397
684
  if (!prompt) {
398
- process.stderr.write('route: missing prompt. Usage: route "<prompt>"\n');
685
+ process.stderr.write('dispatch: missing prompt. Usage: dispatch [--service <id>] [--safety <profile>]\n' +
686
+ ' [--task-type <type>] [--no-fallback] [--json] "<prompt>"\n');
399
687
  return 1;
400
688
  }
401
689
  const runtime = await buildRuntime(configPath);
402
- const { result, decision } = await runtime.router.route(prompt, [], process.cwd(), {
403
- hints: { taskType: "execute" },
404
- maxFallbacks: 2,
405
- });
690
+ const hints = { taskType: opts.taskType ?? "execute" };
691
+ if (opts.safetyProfile !== undefined)
692
+ hints.safetyProfile = opts.safetyProfile;
693
+ // A named service goes through routeTo, which is what "run exactly this
694
+ // route" means — not route() with a hint, which can still fall elsewhere.
695
+ const { result, decision } = opts.service
696
+ ? await runtime.router.routeTo(opts.service, prompt, [], process.cwd(), {
697
+ ...(opts.safetyProfile !== undefined ? { safetyProfile: opts.safetyProfile } : {}),
698
+ ...(opts.taskType !== undefined ? { taskType: opts.taskType } : {}),
699
+ })
700
+ : await runtime.router.route(prompt, [], process.cwd(), {
701
+ hints,
702
+ maxFallbacks: opts.noFallback ? 0 : 2,
703
+ });
704
+ if (opts.json) {
705
+ process.stdout.write(`${JSON.stringify({ result, routing: decision ?? null }, null, 2)}\n`);
706
+ return result.success ? 0 : 1;
707
+ }
406
708
  if (decision) {
407
- process.stderr.write(`route: ${decision.service} (${decision.reason})\n`);
709
+ const beat = decision.candidates?.length
710
+ ? ` [${decision.candidates.map((c) => `${c.route} ${c.score}`).join(", ")}]`
711
+ : "";
712
+ process.stderr.write(`dispatch: ${decision.service} (${decision.reason})${beat}\n`);
408
713
  }
409
714
  process.stdout.write(result.output);
410
715
  if (!result.output.endsWith("\n"))
@@ -415,6 +720,24 @@ async function cmdRouteAlias(prompt, configPath) {
415
720
  }
416
721
  return 0;
417
722
  }
723
+ const SAFETY_PROFILES = ["read_only", "workspace_edit", "full_auto"];
724
+ const TASK_TYPES = ["execute", "plan", "review", "local"];
725
+ /**
726
+ * An enum-valued flag: rejected by name when it is not one of the listed
727
+ * values, never silently dropped to a default.
728
+ *
729
+ * `--safety read_onlyy` dropping to workspace_edit would hand a delegate MORE
730
+ * access than the caller asked for — the same failure the MCP and HTTP
731
+ * surfaces were both hardened against, and the reason `hints` is strict there.
732
+ */
733
+ function enumFlag(value, allowed, flag) {
734
+ if (value === undefined)
735
+ return undefined;
736
+ if (typeof value === "string" && allowed.includes(value)) {
737
+ return value;
738
+ }
739
+ throw new UsageError(`${flag}: invalid value ${JSON.stringify(value)}. Valid: ${allowed.join(", ")}.`);
740
+ }
418
741
  function parsePositiveInt(value, fallback) {
419
742
  if (typeof value !== "string")
420
743
  return fallback;
@@ -463,6 +786,14 @@ export async function main(argv) {
463
786
  yes: { type: "boolean" },
464
787
  force: { type: "boolean" },
465
788
  http: { type: "string" },
789
+ service: { type: "string" },
790
+ safety: { type: "string" },
791
+ "task-type": { type: "string" },
792
+ "no-fallback": { type: "boolean" },
793
+ clients: { type: "string" },
794
+ "no-clients": { type: "boolean" },
795
+ remove: { type: "boolean" },
796
+ dev: { type: "boolean" },
466
797
  },
467
798
  allowPositionals: true,
468
799
  strict: false,
@@ -475,6 +806,8 @@ export async function main(argv) {
475
806
  const knownFlags = new Set([
476
807
  "help", "version", "config", "json", "live", "allow-paid", "watch", "interval",
477
808
  "port", "host", "print", "yes", "force", "http",
809
+ "service", "safety", "task-type", "no-fallback",
810
+ "clients", "no-clients", "remove", "dev",
478
811
  ]);
479
812
  const unknownFlags = Object.keys(values).filter((k) => !knownFlags.has(k));
480
813
  if (unknownFlags.length > 0) {
@@ -531,6 +864,16 @@ export async function main(argv) {
531
864
  print: Boolean(values.print),
532
865
  yes: Boolean(values.yes),
533
866
  force: Boolean(values.force),
867
+ noClients: Boolean(values["no-clients"]),
868
+ clients: typeof values.clients === "string" ? values.clients : undefined,
869
+ });
870
+ case "connect":
871
+ return cmdConnect(configPath, {
872
+ clients: typeof values.clients === "string" ? values.clients : undefined,
873
+ remove: Boolean(values.remove),
874
+ yes: Boolean(values.yes),
875
+ force: Boolean(values.force),
876
+ dev: Boolean(values.dev),
534
877
  });
535
878
  case "doctor":
536
879
  return cmdDoctor(configPath, {
@@ -552,8 +895,21 @@ export async function main(argv) {
552
895
  return cmdServe(configPath, serveOpts(values));
553
896
  case "auth":
554
897
  return cmdAuth(rest[0]);
555
- case "route":
556
- return cmdRouteAlias(rest.join(" ").trim(), configPath);
898
+ // `route` kept as an alias: it was the name for two years of history, and
899
+ // `dispatch` matches the MCP tool that does the same thing. Same pattern
900
+ // as status/dashboard/list-services above.
901
+ case "dispatch":
902
+ case "route": {
903
+ const safety = enumFlag(values.safety, SAFETY_PROFILES, "--safety");
904
+ const taskType = enumFlag(values["task-type"], TASK_TYPES, "--task-type");
905
+ return cmdDispatch(rest.join(" ").trim(), configPath, {
906
+ ...(typeof values.service === "string" ? { service: values.service } : {}),
907
+ ...(safety !== undefined ? { safetyProfile: safety } : {}),
908
+ ...(taskType !== undefined ? { taskType } : {}),
909
+ noFallback: Boolean(values["no-fallback"]),
910
+ json: Boolean(values.json),
911
+ });
912
+ }
557
913
  case "mcp":
558
914
  if (values.http !== undefined) {
559
915
  return cmdServe(configPath, serveOpts({ port: values.http, host: values.host }));
@@ -565,11 +921,39 @@ export async function main(argv) {
565
921
  return 1;
566
922
  }
567
923
  }
924
+ /**
925
+ * End the process with `code`, letting the event loop drain first.
926
+ *
927
+ * `process.exit(code)` tears the loop down mid-flight, and on Windows that
928
+ * aborts: `Assertion failed: !(handle->flags & UV_HANDLE_CLOSING), src\win * async.c` and an exit status of 127, which every shell reads as "command not
929
+ * found". It fired AFTER a correct answer had been printed, so the work was
930
+ * done and the report of it was a crash.
931
+ *
932
+ * Reproduced 3/3 across three acceptance passes, and confirmed here: two
933
+ * endpoint routes, the first answering 200 with an unusable body and the
934
+ * second succeeding. The fallback is what makes it two in-flight HTTP
935
+ * connections; `--no-fallback` and a refused connection both exited 1 cleanly,
936
+ * which is why it looked intermittent. Setting `exitCode` instead removes it,
937
+ * measured on the same probe.
938
+ *
939
+ * The force-exit is the safety net `process.exit` was providing: if something
940
+ * still holds the loop open after a grace window, leave anyway rather than
941
+ * hanging a CLI. It is `unref`d, so it does not itself keep the process alive
942
+ * — every command measured here exits in under a second without it firing.
943
+ */
944
+ const EXIT_DRAIN_GRACE_MS = 3000;
945
+ function finish(code) {
946
+ process.exitCode = code;
947
+ const bail = setTimeout(() => {
948
+ process.exit(code);
949
+ }, EXIT_DRAIN_GRACE_MS);
950
+ bail.unref();
951
+ }
568
952
  const entrypoint = typeof process !== "undefined" && Array.isArray(process.argv) ? process.argv[1] : "";
569
953
  if (entrypoint && (entrypoint.endsWith("bin.ts") || entrypoint.endsWith("bin.js"))) {
570
954
  void main(process.argv.slice(2))
571
955
  .then((code) => {
572
- process.exit(code);
956
+ finish(code);
573
957
  })
574
958
  .catch((err) => {
575
959
  // A CLI user gets one actionable line, not a stack trace. Every Error is