github-router 0.3.17 → 0.3.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/main.js CHANGED
@@ -4,17 +4,16 @@ import consola from "consola";
4
4
  import fs from "node:fs/promises";
5
5
  import os from "node:os";
6
6
  import path from "node:path";
7
- import { randomBytes, randomUUID } from "node:crypto";
7
+ import { randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
8
8
  import process$1 from "node:process";
9
+ import { execFileSync, spawn } from "node:child_process";
9
10
  import fs$1 from "node:fs";
10
11
  import { Writable } from "node:stream";
11
- import { execFileSync, spawn } from "node:child_process";
12
12
  import { serve } from "srvx";
13
13
  import { getProxyForUrl } from "proxy-from-env";
14
14
  import { Agent, ProxyAgent, setGlobalDispatcher } from "undici";
15
15
  import { Hono } from "hono";
16
16
  import { cors } from "hono/cors";
17
- import { streamSSE } from "hono/streaming";
18
17
  import { events } from "fetch-event-stream";
19
18
  import { z } from "zod";
20
19
  import clipboard from "clipboardy";
@@ -35,12 +34,23 @@ const PATHS = {
35
34
  },
36
35
  get CODEX_HOME() {
37
36
  return path.join(appDir(), "codex-isolated");
37
+ },
38
+ get CLAUDE_RUNTIME_DIR() {
39
+ return path.join(appDir(), "runtime");
38
40
  }
39
41
  };
40
42
  async function ensurePaths() {
41
43
  await fs.mkdir(PATHS.APP_DIR, { recursive: true });
42
44
  await fs.mkdir(PATHS.CODEX_HOME, { recursive: true });
45
+ await fs.mkdir(PATHS.CLAUDE_RUNTIME_DIR, { recursive: true });
46
+ await chmodIfPossible(PATHS.CLAUDE_RUNTIME_DIR, 448);
43
47
  await ensureFile(PATHS.GITHUB_TOKEN_PATH);
48
+ await sweepStaleRuntimeFiles().catch((err) => {
49
+ consola.debug("Runtime sweep skipped:", err);
50
+ });
51
+ await sweepStalePeerAgentMdFiles().catch((err) => {
52
+ consola.debug("Peer-agent .md sweep skipped:", err);
53
+ });
44
54
  }
45
55
  async function ensureFile(filePath) {
46
56
  try {
@@ -50,6 +60,129 @@ async function ensureFile(filePath) {
50
60
  await fs.chmod(filePath, 384);
51
61
  }
52
62
  }
63
+ async function chmodIfPossible(target, mode) {
64
+ if (process.platform === "win32") return;
65
+ try {
66
+ await fs.chmod(target, mode);
67
+ } catch (err) {
68
+ consola.debug(`chmod ${target} ${mode.toString(8)} failed:`, err);
69
+ }
70
+ }
71
+ /**
72
+ * Write a runtime tempfile securely.
73
+ *
74
+ * - Mode `0o600` so other local users (multi-tenant boxes, shared
75
+ * dev containers) can't read the per-launch nonce or runtime URL.
76
+ * - `flag: "wx"` (O_CREAT | O_EXCL | O_WRONLY) refuses to overwrite
77
+ * an existing path. POSIX open(2) with O_EXCL also rejects
78
+ * pre-placed symlinks, killing the symlink-clobber attack vector.
79
+ * - The caller's responsibility to pick a path NOT yet in use.
80
+ * We intentionally do NOT pre-unlink: an `lstat` + `unlink` +
81
+ * `open(O_EXCL)` sequence still has a TOCTOU window where an
82
+ * attacker can drop a symlink between unlink and open. Letting
83
+ * `wx` fail is the safer behavior — surfaces the conflict
84
+ * instead of silently following.
85
+ */
86
+ async function writeRuntimeFileSecure(filePath, content) {
87
+ await fs.writeFile(filePath, content, {
88
+ mode: 384,
89
+ flag: "wx"
90
+ });
91
+ }
92
+ /**
93
+ * Sweep stale runtime tempfiles. Removes files whose embedded PID is no
94
+ * longer a live process. A proxy crash (`kill -9`, OS reboot) leaves
95
+ * orphans that would otherwise accumulate forever — and worse, a stale
96
+ * config pointing at a now-recycled port could route MCP traffic to
97
+ * whatever process bound that port next.
98
+ *
99
+ * Naming convention: `peer-mcp-<pid>.json` and `peer-agents-<pid>.json`.
100
+ * Files not matching either pattern are left alone — this directory
101
+ * is shared with future runtime artifacts.
102
+ *
103
+ * We deliberately do NOT age-prune files whose PID is alive. A
104
+ * legitimately long-running proxy can have a tempfile older than any
105
+ * arbitrary threshold; deleting it out from under the live process
106
+ * breaks the spawned Claude Code child's MCP/agent wiring with no clean
107
+ * recovery. PID-wraparound risk is mitigated by (a) PID reuse on Linux
108
+ * being slow under typical loads, and (b) the file is only consulted by
109
+ * github-router itself — an unrelated process that inherits the PID
110
+ * never reads it.
111
+ */
112
+ async function sweepStaleRuntimeFiles() {
113
+ const dir = PATHS.CLAUDE_RUNTIME_DIR;
114
+ let entries;
115
+ try {
116
+ entries = await fs.readdir(dir);
117
+ } catch (err) {
118
+ if (err.code === "ENOENT") return;
119
+ throw err;
120
+ }
121
+ for (const name$1 of entries) {
122
+ const match = /^peer-(?:mcp|agents)-(\d+)(?:-[0-9a-f]+)?\.json$/.exec(name$1);
123
+ if (!match) continue;
124
+ const pid = Number.parseInt(match[1], 10);
125
+ const filePath = path.join(dir, name$1);
126
+ if (isPidAlive(pid)) continue;
127
+ await fs.unlink(filePath).catch(() => {});
128
+ }
129
+ }
130
+ function isPidAlive(pid) {
131
+ if (!Number.isInteger(pid) || pid <= 0) return false;
132
+ try {
133
+ process.kill(pid, 0);
134
+ return true;
135
+ } catch (err) {
136
+ if (err.code === "EPERM") return true;
137
+ return false;
138
+ }
139
+ }
140
+ /**
141
+ * Sweep stale peer-* subagent .md files from `~/.claude/agents/`. Phase
142
+ * 2.5 writes one .md per peer agent into the canonical agents directory
143
+ * so they appear in Claude Code's Task `subagent_type` enum. Files are
144
+ * named `peer-<pid>-<rand>-<agentName>.md` so this sweep can drop
145
+ * orphans from crashed prior proxy sessions without touching the user's
146
+ * own .md files.
147
+ *
148
+ * Same liveness rule as `sweepStaleRuntimeFiles`: only delete when the
149
+ * file's embedded PID is no longer alive. Live PIDs keep their files —
150
+ * a long-running proxy doesn't lose its agent registrations.
151
+ *
152
+ * Regex tightening (Phase 2.6, codex-critic + gemini-critic 2-lab finding):
153
+ * the original sweep regex `^peer-(\d+)(?:-[0-9a-f]+)?-.+\.md$` was too
154
+ * permissive — a user-authored `peer-12345-meeting-notes.md` matches
155
+ * (`12345` = "PID", `-meeting-notes` = trailing `.+`) and would be
156
+ * silently unlinked when 12345 happens to be a dead PID (overwhelmingly
157
+ * likely). Tightened to require BOTH the 8-hex-char random suffix AND
158
+ * an exact-match persona name suffix, eliminating the risk for any
159
+ * realistic user filename.
160
+ */
161
+ async function sweepStalePeerAgentMdFiles() {
162
+ const dir = path.join(os.homedir(), ".claude", "agents");
163
+ let entries;
164
+ try {
165
+ entries = await fs.readdir(dir);
166
+ } catch (err) {
167
+ if (err.code === "ENOENT") return;
168
+ throw err;
169
+ }
170
+ for (const name$1 of entries) {
171
+ const match = PEER_AGENT_MD_FILENAME.exec(name$1);
172
+ if (!match) continue;
173
+ if (isPidAlive(Number.parseInt(match[1], 10))) continue;
174
+ await fs.unlink(path.join(dir, name$1)).catch(() => {});
175
+ }
176
+ }
177
+ /**
178
+ * Strict regex matching only files this proxy writes:
179
+ * peer-<pid>-<8 hex>-<exact persona/coordinator name>.md
180
+ * The persona-name allowlist is the load-bearing protection against
181
+ * deleting user files. Update this list whenever a new persona is added
182
+ * to `PERSONAS_READ` / `PERSONAS_WRITE` in `peer-mcp-personas.ts` or a
183
+ * new coordinator-style agent is added in `codex-mcp-config.ts`.
184
+ */
185
+ const PEER_AGENT_MD_FILENAME = /^peer-(\d+)-[0-9a-f]{8}-(?:codex-critic|codex-reviewer|gemini-critic|codex-implementer|peer-review-coordinator)\.md$/;
53
186
 
54
187
  //#endregion
55
188
  //#region src/lib/state.ts
@@ -76,14 +209,14 @@ function copilotVersion(state$1) {
76
209
  const API_VERSION = "2026-01-09";
77
210
  const copilotBaseUrl = (state$1) => state$1.copilotApiUrl ?? "https://api.githubcopilot.com";
78
211
  const copilotHeaders = (state$1, vision = false, integrationId = "vscode-chat") => {
79
- const version = copilotVersion(state$1);
212
+ const version$1 = copilotVersion(state$1);
80
213
  const headers = {
81
214
  Authorization: `Bearer ${state$1.copilotToken}`,
82
215
  "content-type": standardHeaders()["content-type"],
83
216
  "copilot-integration-id": integrationId,
84
217
  "editor-version": `vscode/${state$1.vsCodeVersion}`,
85
- "editor-plugin-version": `copilot-chat/${version}`,
86
- "user-agent": `GitHubCopilotChat/${version}`,
218
+ "editor-plugin-version": `copilot-chat/${version$1}`,
219
+ "user-agent": `GitHubCopilotChat/${version$1}`,
87
220
  "openai-intent": "conversation-panel",
88
221
  "x-interaction-type": "conversation-panel",
89
222
  "x-github-api-version": API_VERSION,
@@ -119,7 +252,7 @@ var HTTPError = class extends Error {
119
252
  }
120
253
  };
121
254
  async function forwardError(c, error) {
122
- consola.error("Error occurred:", error);
255
+ consola.error(`Error occurred at ${c.req.path}:`, error);
123
256
  if (error instanceof HTTPError) {
124
257
  const errorText = await error.response.text().catch(() => "");
125
258
  let errorJson;
@@ -473,9 +606,9 @@ const cacheVSCodeVersion = async () => {
473
606
  consola.info(`Using VSCode version: ${response}`);
474
607
  };
475
608
  const cacheCopilotVersion = async () => {
476
- const version = await getCopilotChatVersion();
477
- state.copilotVersion = version;
478
- consola.info(`Using Copilot Chat version: ${version}`);
609
+ const version$1 = await getCopilotChatVersion();
610
+ state.copilotVersion = version$1;
611
+ consola.info(`Using Copilot Chat version: ${version$1}`);
479
612
  };
480
613
 
481
614
  //#endregion
@@ -520,18 +653,62 @@ const setupCopilotToken = async () => {
520
653
  consola.debug("GitHub Copilot Token fetched successfully!");
521
654
  if (state.showToken) consola.info("Copilot token:", token);
522
655
  const refreshInterval = Math.max((refresh_in - 60) * 1e3, 1e3);
523
- setInterval(async () => {
524
- consola.debug("Refreshing Copilot token");
656
+ setInterval(() => {
657
+ refreshCopilotToken("interval");
658
+ }, refreshInterval);
659
+ };
660
+ let inflightRefresh;
661
+ let lastRefreshSuccess = 0;
662
+ let lastRefreshFailure = 0;
663
+ const REFRESH_SUCCESS_COOLDOWN_MS = 3e4;
664
+ const REFRESH_FAILURE_COOLDOWN_MS = 5e3;
665
+ async function refreshCopilotToken(reason) {
666
+ if (inflightRefresh) return inflightRefresh;
667
+ if (reason === "401-retry") {
668
+ const now = Date.now();
669
+ if (now - lastRefreshSuccess < REFRESH_SUCCESS_COOLDOWN_MS) {
670
+ consola.debug(`refreshCopilotToken(${reason}) skipped: prior success within ${REFRESH_SUCCESS_COOLDOWN_MS}ms`);
671
+ return;
672
+ }
673
+ if (now - lastRefreshFailure < REFRESH_FAILURE_COOLDOWN_MS) {
674
+ consola.debug(`refreshCopilotToken(${reason}) skipped: prior failure within ${REFRESH_FAILURE_COOLDOWN_MS}ms`);
675
+ return;
676
+ }
677
+ }
678
+ inflightRefresh = (async () => {
679
+ consola.debug(`Refreshing Copilot token (reason=${reason})`);
525
680
  try {
526
- const { token: token$1 } = await getCopilotToken();
527
- state.copilotToken = token$1;
681
+ const { token } = await getCopilotToken();
682
+ state.copilotToken = token;
683
+ lastRefreshSuccess = Date.now();
528
684
  consola.debug("Copilot token refreshed");
529
- if (state.showToken) consola.info("Refreshed Copilot token:", token$1);
685
+ if (state.showToken) consola.info("Refreshed Copilot token:", token);
530
686
  } catch (error) {
531
- consola.error("Failed to refresh Copilot token:", error);
687
+ lastRefreshFailure = Date.now();
688
+ consola.error(`Failed to refresh Copilot token (reason=${reason}):`, error);
689
+ } finally {
690
+ inflightRefresh = void 0;
532
691
  }
533
- }, refreshInterval);
534
- };
692
+ })();
693
+ return inflightRefresh;
694
+ }
695
+ /**
696
+ * Try `request()`. If it returns a 401, refresh the Copilot token (subject
697
+ * to the single-flight + refresh-storm-protection of `refreshCopilotToken`)
698
+ * and retry once. After one retry, propagate whatever the second attempt
699
+ * returned — the caller's existing 401-handling path is preserved.
700
+ *
701
+ * The `request` callback is responsible for capturing `state.copilotToken`
702
+ * locally before any await; this helper does NOT re-build the request
703
+ * itself, just re-invokes the callback after a refresh.
704
+ */
705
+ async function tryRefreshAndRetry(request, routePath) {
706
+ const first = await request();
707
+ if (first.status !== 401) return first;
708
+ consola.warn(`${routePath}: upstream returned 401, attempting one token refresh + retry`);
709
+ await refreshCopilotToken("401-retry");
710
+ return request();
711
+ }
535
712
  async function setupGitHubToken(options) {
536
713
  try {
537
714
  const githubToken = await readGithubToken();
@@ -627,13 +804,13 @@ const checkUsage = defineCommand({
627
804
  const premiumUsed = premiumTotal - premium.remaining;
628
805
  const premiumPercentUsed = premiumTotal > 0 ? premiumUsed / premiumTotal * 100 : 0;
629
806
  const premiumPercentRemaining = premium.percent_remaining;
630
- function summarizeQuota(name, snap) {
631
- if (!snap) return `${name}: N/A`;
807
+ function summarizeQuota(name$1, snap) {
808
+ if (!snap) return `${name$1}: N/A`;
632
809
  const total = snap.entitlement;
633
810
  const used = total - snap.remaining;
634
811
  const percentUsed = total > 0 ? used / total * 100 : 0;
635
812
  const percentRemaining = snap.percent_remaining;
636
- return `${name}: ${used}/${total} used (${percentUsed.toFixed(1)}% used, ${percentRemaining.toFixed(1)}% remaining)`;
813
+ return `${name$1}: ${used}/${total} used (${percentUsed.toFixed(1)}% used, ${percentRemaining.toFixed(1)}% remaining)`;
637
814
  }
638
815
  const premiumLine = `Premium: ${premiumUsed}/${premiumTotal} used (${premiumPercentUsed.toFixed(1)}% used, ${premiumPercentRemaining.toFixed(1)}% remaining)`;
639
816
  const chatLine = summarizeQuota("Chat", usage.quota_snapshots.chat);
@@ -646,100 +823,6 @@ const checkUsage = defineCommand({
646
823
  }
647
824
  });
648
825
 
649
- //#endregion
650
- //#region src/lib/file-log-reporter.ts
651
- const MAX_LOG_BYTES = 1024 * 1024;
652
- const DEDUP_MAX = 1e3;
653
- const ARG_MAX_LEN = 2048;
654
- const DEDUP_KEY_MAX_LEN = 200;
655
- const CREDENTIAL_RE = /\b(eyJ[A-Za-z0-9_-]{20,}(?:\.[A-Za-z0-9_-]+){0,2}|gh[opsu]_[A-Za-z0-9_]{20,}|Bearer\s+\S{20,})\b/g;
656
- const ALLOWED_TYPES = new Set([
657
- "fatal",
658
- "error",
659
- "warn"
660
- ]);
661
- function sanitize(line) {
662
- return line.replace(CREDENTIAL_RE, "[REDACTED]");
663
- }
664
- function serializeArg(arg) {
665
- if (typeof arg === "string") return arg;
666
- if (arg instanceof Error) {
667
- const parts = [arg.message];
668
- if (arg.stack) parts.push(arg.stack);
669
- return parts.join("\n");
670
- }
671
- return String(arg);
672
- }
673
- function formatLogLine(logObj) {
674
- return sanitize(`${logObj.date.toISOString()} [${(logObj.type ?? "error").toUpperCase()}] ${logObj.args.map((a) => {
675
- const s = serializeArg(a);
676
- return s.length > ARG_MAX_LEN ? s.slice(0, ARG_MAX_LEN) + "…" : s;
677
- }).join(" ").replace(/\r\n|\r|\n/g, "\\n")}\n`);
678
- }
679
- function makeDedupeKey(logObj) {
680
- const firstArg = logObj.args.length > 0 ? serializeArg(logObj.args[0]) : "";
681
- const key = `${logObj.type}:${firstArg}`;
682
- return key.length > DEDUP_KEY_MAX_LEN ? key.slice(0, DEDUP_KEY_MAX_LEN) : key;
683
- }
684
- function rotateIfNeeded(filePath) {
685
- let size;
686
- try {
687
- size = fs$1.statSync(filePath).size;
688
- } catch {
689
- return;
690
- }
691
- if (size <= MAX_LOG_BYTES) return;
692
- try {
693
- fs$1.renameSync(filePath, filePath + ".1");
694
- } catch {}
695
- }
696
- var FileLogReporter = class {
697
- filePath;
698
- seen = /* @__PURE__ */ new Set();
699
- writing = false;
700
- constructor(filePath) {
701
- this.filePath = filePath;
702
- rotateIfNeeded(filePath);
703
- }
704
- log(logObj, _ctx) {
705
- if (!ALLOWED_TYPES.has(logObj.type)) return;
706
- if (this.writing) return;
707
- const key = makeDedupeKey(logObj);
708
- if (this.seen.has(key)) return;
709
- if (this.seen.size >= DEDUP_MAX) this.seen.clear();
710
- this.seen.add(key);
711
- const line = formatLogLine(logObj);
712
- this.writing = true;
713
- try {
714
- const fd = fs$1.openSync(this.filePath, "a", 384);
715
- fs$1.writeSync(fd, line);
716
- fs$1.closeSync(fd);
717
- } catch {} finally {
718
- this.writing = false;
719
- }
720
- }
721
- };
722
- const nullStream = new Writable({ write(_chunk, _encoding, cb) {
723
- cb();
724
- } });
725
- /**
726
- * Switch consola to file-only mode for TUI sessions.
727
- * Removes the terminal reporter and installs a file reporter that
728
- * persists errors and warnings to disk with dedup and credential scrubbing.
729
- *
730
- * Also sinks consola's stdout/stderr streams as belt-and-suspenders:
731
- * even if a terminal reporter is re-added, it cannot write to the terminal.
732
- * Crash handlers that call process.stderr.write() directly are unaffected.
733
- * FileLogReporter uses fs.writeSync() directly and is also unaffected.
734
- */
735
- function enableFileLogging() {
736
- const reporter = new FileLogReporter(PATHS.ERROR_LOG_PATH);
737
- consola.options.throttle = 0;
738
- consola.setReporters([reporter]);
739
- consola.options.stdout = nullStream;
740
- consola.options.stderr = nullStream;
741
- }
742
-
743
826
  //#endregion
744
827
  //#region src/lib/port.ts
745
828
  const DEFAULT_PORT = 8787;
@@ -781,6 +864,18 @@ const PORT_RANGE_MAX = 65535;
781
864
  function generateRandomPort() {
782
865
  return Math.floor(Math.random() * (PORT_RANGE_MAX - PORT_RANGE_MIN + 1)) + PORT_RANGE_MIN;
783
866
  }
867
+ function envInt(key, fallback) {
868
+ const raw = process.env[key];
869
+ if (!raw) return fallback;
870
+ if (!/^[0-9]+$/.test(raw.trim())) {
871
+ consola.warn(`${key}=${JSON.stringify(raw)} is not a non-negative integer; using fallback ${fallback}`);
872
+ return fallback;
873
+ }
874
+ const parsed = Number.parseInt(raw, 10);
875
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
876
+ }
877
+ const UPSTREAM_FETCH_TIMEOUT_MS = envInt("UPSTREAM_FETCH_TIMEOUT_MS", 0);
878
+ const UPSTREAM_INACTIVITY_TIMEOUT_MS = envInt("UPSTREAM_INACTIVITY_TIMEOUT_MS", 3e5);
784
879
 
785
880
  //#endregion
786
881
  //#region src/lib/launch.ts
@@ -827,34 +922,95 @@ function sanitizeParentEnv(parent) {
827
922
  for (const key of STRIPPED_PARENT_ENV_KEYS) delete sanitized[key];
828
923
  return sanitized;
829
924
  }
830
- function commandExists(name) {
925
+ function commandExists(name$1) {
831
926
  try {
832
- execFileSync(process$1.platform === "win32" ? "where.exe" : "which", [name], { stdio: "ignore" });
927
+ execFileSync(process$1.platform === "win32" ? "where.exe" : "which", [name$1], { stdio: "ignore" });
833
928
  return true;
834
929
  } catch {
835
930
  return false;
836
931
  }
837
932
  }
933
+ /**
934
+ * Provider-config flags (`-c model_providers.github_router=...`) that
935
+ * point Codex at our proxy. Extracted from `buildCodexCmd` so the new
936
+ * `codex mcp-server` MCP-config builder can reuse the exact same
937
+ * provider definition — drift between the two paths would silently
938
+ * break the MCP wiring.
939
+ */
940
+ function buildCodexProviderConfigFlags(serverUrl) {
941
+ return [
942
+ "-c",
943
+ `model_providers.github_router={name="github-router",base_url="${serverUrl}/v1",wire_api="responses",env_key="OPENAI_API_KEY"}`,
944
+ "-c",
945
+ "model_provider=github_router"
946
+ ];
947
+ }
948
+ /**
949
+ * Inspect the installed `codex` binary. Used by the codex-MCP wiring
950
+ * in `claude.ts` to gate `--codex-cli`. Codex 0.129.0 introduced the
951
+ * `mcp-server` subcommand; older versions don't expose it, so we
952
+ * downgrade to the HTTP backend with a warning.
953
+ */
954
+ function getCodexVersion() {
955
+ if (!commandExists("codex")) return { ok: false };
956
+ let raw;
957
+ try {
958
+ raw = execFileSync("codex", ["--version"], {
959
+ encoding: "utf8",
960
+ stdio: [
961
+ "ignore",
962
+ "pipe",
963
+ "ignore"
964
+ ]
965
+ }).trim();
966
+ } catch {
967
+ return { ok: false };
968
+ }
969
+ const m = /(\d+)\.(\d+)\.(\d+)/.exec(raw);
970
+ if (!m) return {
971
+ ok: false,
972
+ version: raw
973
+ };
974
+ const major = Number.parseInt(m[1], 10);
975
+ const minor = Number.parseInt(m[2], 10);
976
+ const version$1 = `${m[1]}.${m[2]}.${m[3]}`;
977
+ return {
978
+ ok: major > 0 || major === 0 && minor >= 129,
979
+ version: version$1
980
+ };
981
+ }
982
+ /**
983
+ * Codex 0.129.0 broke two things the launcher had been relying on:
984
+ * (1) `--full-auto` was removed in favor of `--sandbox` + `--ask-for-approval`;
985
+ * passing it now exits the child immediately with
986
+ * `error: unexpected argument '--full-auto' found`.
987
+ * (2) `OPENAI_BASE_URL` is silently ignored — Codex hardcodes
988
+ * `https://api.openai.com/v1/responses` and 401s out without an
989
+ * explicit `-c model_providers.<name>.base_url` override.
990
+ *
991
+ * `buildCodexCmd` builds the launch argv that works on Codex 0.129+ while
992
+ * still being compatible with older versions that accept the same flags.
993
+ */
994
+ function buildCodexCmd(target) {
995
+ const cmd = ["codex"];
996
+ if (target.serverUrl) cmd.push(...buildCodexProviderConfigFlags(target.serverUrl));
997
+ cmd.push("--sandbox", "workspace-write", "--ask-for-approval", "on-request", "-m", target.model ?? DEFAULT_CODEX_MODEL, ...target.extraArgs);
998
+ return cmd;
999
+ }
838
1000
  function buildLaunchCommand(target) {
839
1001
  return {
840
1002
  cmd: target.kind === "claude-code" ? [
841
1003
  "claude",
842
1004
  "--dangerously-skip-permissions",
843
1005
  ...target.extraArgs
844
- ] : [
845
- "codex",
846
- "--full-auto",
847
- "-m",
848
- target.model ?? DEFAULT_CODEX_MODEL,
849
- ...target.extraArgs
850
- ],
1006
+ ] : buildCodexCmd(target),
851
1007
  env: {
852
1008
  ...sanitizeParentEnv(process$1.env),
853
1009
  ...target.envVars
854
1010
  }
855
1011
  };
856
1012
  }
857
- function launchChild(target, server$1) {
1013
+ function launchChild(target, server$1, options = {}) {
858
1014
  const { cmd, env } = buildLaunchCommand(target);
859
1015
  const executable = cmd[0];
860
1016
  if (!commandExists(executable)) {
@@ -879,6 +1035,7 @@ function launchChild(target, server$1) {
879
1035
  consola.error(msg);
880
1036
  process$1.stderr.write(msg + "\n");
881
1037
  server$1.close(true).catch(() => {});
1038
+ if (options.onShutdown) Promise.resolve(options.onShutdown()).catch(() => {});
882
1039
  process$1.exit(1);
883
1040
  }
884
1041
  let cleaned = false;
@@ -893,6 +1050,9 @@ function launchChild(target, server$1) {
893
1050
  try {
894
1051
  await server$1.close(true);
895
1052
  } catch {}
1053
+ if (options.onShutdown) try {
1054
+ await options.onShutdown();
1055
+ } catch {}
896
1056
  clearTimeout(timeout);
897
1057
  }
898
1058
  function exit(code) {
@@ -914,6 +1074,606 @@ function launchChild(target, server$1) {
914
1074
  });
915
1075
  }
916
1076
 
1077
+ //#endregion
1078
+ //#region src/lib/peer-mcp-personas.ts
1079
+ const CRITIC_RUBRIC = `
1080
+ Apply this grading rubric:
1081
+ - Score 1–5 on three axes:
1082
+ A. assumption-soundness (are stated assumptions accurate? are unstated ones load-bearing?)
1083
+ B. failure-mode coverage (which realistic failure modes are unaddressed?)
1084
+ C. alternative-considered (was a meaningfully different approach weighed and rejected with reason?)
1085
+ - If every axis scores ≥ 4, reply with the literal string "no material objection" and stop. Do not invent issues to satisfy this rubric.
1086
+ - Otherwise, the lowest-scoring axis IS your critique. Lead with that single critique; secondary observations may follow as "additional notes".
1087
+
1088
+ Reply format (markdown):
1089
+ ## Verdict
1090
+ <"no material objection" OR a one-sentence summary of the load-bearing critique>
1091
+ ## Scores
1092
+ - assumption-soundness: <n>/5
1093
+ - failure-mode coverage: <n>/5
1094
+ - alternative-considered: <n>/5
1095
+ ## Critique
1096
+ <only when at least one axis < 4 — concrete, specific, actionable>
1097
+ ## Additional notes (optional)
1098
+ <secondary observations; omit if none>
1099
+
1100
+ Self-reminder (read before every reply):
1101
+ Am I still acting as the adversarial critic per the rubric above?
1102
+ If I just produced agreement, restart and apply the grading rubric instead.
1103
+ Sycophancy is the failure mode I exist to fight; manufactured contrarianism is a different failure of the same shape — do neither.
1104
+ `.trim();
1105
+ const COLD_START_CONTRACT = `
1106
+ Cold-start contract for the lead orchestrator (Opus):
1107
+ When delegating to me, paste a self-contained brief. I have no access to your scrollback, CLAUDE.md, or the project tree. Always include:
1108
+ (a) the artifact under review verbatim (code/diff/plan text),
1109
+ (b) the constraints or "done" criteria,
1110
+ (c) any prior decisions I should not relitigate.
1111
+ If your brief lacks (a), I will reply with a one-line request for the artifact instead of speculating.
1112
+ `.trim();
1113
+ const CRITIC_BASE = `You are codex-critic, an adversarial reviewer running on gpt-5.5. Your single job is to overcome the lead orchestrator's blind spots — assumptions it didn't notice it was making, failure modes it didn't enumerate, alternatives it didn't consider.
1114
+
1115
+ You are NOT a helpful assistant. You are NOT a coach. Sycophancy is the failure mode you exist to fight. Manufactured contrarianism is a different failure of the same shape — silence on good work is a valid and welcome answer.
1116
+
1117
+ ${COLD_START_CONTRACT}
1118
+
1119
+ ${CRITIC_RUBRIC}`;
1120
+ const GEMINI_CRITIC_BASE = `You are gemini-critic, an adversarial reviewer running on Gemini 3.1 Pro. You exist to provide a second-lab perspective: your training data, RLHF priors, and attention patterns are systematically different from the lead orchestrator's (Opus, Anthropic) and from codex-critic (gpt-5.5, OpenAI). Use that to surface blind spots both miss.
1121
+
1122
+ Your strengths the lead may want to draw on:
1123
+ - long-context reasoning over large artifacts (the brief may include >50k tokens of context)
1124
+ - math, proofs, and formally-stated invariants
1125
+ - cross-checking conclusions where codex-critic has already weighed in (the lead may forward you both the artifact and codex-critic's verdict)
1126
+
1127
+ You are NOT a helpful assistant. Sycophancy is the failure mode you exist to fight; do not invent issues to look thorough.
1128
+
1129
+ ${COLD_START_CONTRACT}
1130
+
1131
+ ${CRITIC_RUBRIC}`;
1132
+ const REVIEWER_BASE = `You are codex-reviewer, a line-level code reviewer running on gpt-5.3-codex. You are the code-specialist persona — your job is to read concrete code (diffs, single files, function bodies) and surface bugs, edge cases, security issues, and idiom violations.
1133
+
1134
+ You are not a critic-of-architecture. If the brief is a plan or a high-level design, redirect: "this looks like architecture review; consider codex-critic or gemini-critic." Your tool is the magnifying glass, not the wide-angle lens.
1135
+
1136
+ ${COLD_START_CONTRACT}
1137
+
1138
+ Reply format (markdown):
1139
+ ## Summary
1140
+ <one sentence: clean / N findings / blocking issue>
1141
+ ## Findings
1142
+ For each:
1143
+ ### <severity: info | low | medium | high | critical> — <one-line title>
1144
+ - location: <file:line[-line]>
1145
+ - issue: <what's wrong, why it matters in this codebase>
1146
+ - suggested fix: <minimal change OR "needs design discussion">
1147
+ Number the findings if there are more than one. List them in severity-descending order (critical first).
1148
+ If there are zero findings of any severity, reply only with "## Summary\\nClean review — no findings." and stop.
1149
+
1150
+ Self-reminder (read before every reply):
1151
+ Am I citing real code at real line numbers in the brief? If a finding doesn't have a concrete file:line citation, drop it.
1152
+ Did I rank the finding's severity by impact-in-this-codebase, not by general-principle?
1153
+ If everything looks fine, say so cleanly — do not pad with stylistic nitpicks.`;
1154
+ const IMPLEMENTER_BASE = `You are codex-implementer, a focused implementation specialist running on gpt-5.3-codex with workspace-write access. You execute scoped, well-specified coding tasks end-to-end: read the relevant files, make the change, verify it, report back.
1155
+
1156
+ You are not a planner. If the brief is vague or missing acceptance criteria, ask the lead for the missing piece BEFORE editing anything. A wasted edit is worse than a clarifying question.
1157
+
1158
+ ${COLD_START_CONTRACT}
1159
+
1160
+ What "done" looks like for an implementation task:
1161
+ - Exactly the files specified by the brief have been changed (or you reported back why a different scope was needed).
1162
+ - The change is minimal — surrounding cleanup is out of scope unless requested.
1163
+ - You ran the relevant test(s) / typecheck / linter for the touched files and report the results.
1164
+ - The summary you return enumerates each file changed with a one-line description.
1165
+
1166
+ Reply format (markdown):
1167
+ ## Status
1168
+ <complete | needs-clarification | blocked>
1169
+ ## Files changed
1170
+ - path/one.ts: <one-line description>
1171
+ - path/two.ts: <one-line description>
1172
+ ## Verification
1173
+ <commands run + outcomes>
1174
+ ## Notes
1175
+ <anything the lead must know to integrate, e.g. follow-ups intentionally not done>
1176
+
1177
+ Resilience reminder:
1178
+ If your session terminates abnormally before "Status: complete", the lead will retry once. On recovery, ask the lead to confirm what's already been done before re-applying changes — duplicate edits are worse than a slow restart.`;
1179
+ const PERSONAS_READ = Object.freeze([
1180
+ {
1181
+ agentName: "codex-critic",
1182
+ toolNameHttp: "codex_critic",
1183
+ model: "gpt-5.5",
1184
+ endpoint: "/v1/responses",
1185
+ description: "Adversarial second opinion on plans, designs, code, or systems-engineering tradeoffs. Backed by gpt-5.5 (OpenAI) — different model, different training data, different blind spots than Opus. Uses a calibrated 1–5 grading rubric and is allowed to reply 'no material objection' on solid artifacts. **CALL BEFORE: ExitPlanMode for any plan involving >2 files or new architecture; finalizing a major design choice; TeamCreate when the team's task is non-trivial.** **CALL AFTER: any commit touching concurrency, security, or streaming code paths.** If the artifact is large (>20 KB), prefer to break it into 2-4 focused batches and call this tool once per batch IN PARALLEL — each call must complete under the Claude Code MCP per-tool-call ceiling (~150s on v2.1.138 per regression #50289), so monolithic large-artifact calls will time out client-side. Aggregate findings yourself. Always pass: (a) the artifact verbatim, (b) the constraints/'done' criteria, (c) any prior decisions. Optionally pass `effort: 'xhigh'` for explicit deep dives or `effort: 'medium'` for quick sanity checks (default 'high'). The subagent has no access to your scrollback or CLAUDE.md.",
1186
+ baseInstructions: CRITIC_BASE,
1187
+ agentPrompt: "",
1188
+ writeCapable: false,
1189
+ requiresHttp: false
1190
+ },
1191
+ {
1192
+ agentName: "gemini-critic",
1193
+ toolNameHttp: "gemini_critic",
1194
+ model: "gemini-3.1-pro-preview",
1195
+ endpoint: "/v1/chat/completions",
1196
+ description: "Adversarial second opinion from a different lab. Backed by gemini-3.1-pro-preview (Google) — different training data and RLHF priors than Opus AND codex-critic, the strongest blind-spot-buster when the lead wants triangulation across three labs. Use for long-context artifacts (>50k tokens), math/proof-shaped reasoning, or as a tie-breaker after codex-critic has weighed in. **CALL BEFORE: ExitPlanMode for plans where Opus + codex-critic agree (use as triangulation); finalizing irreversible architectural choices.** **CALL AFTER: commits where you want a third-lab cross-check.** If the artifact is large (>100 KB), prefer to break into batches and call in parallel — gemini handles long context well but each per-call MCP wait is still bounded (~150s on v2.1.138). Always pass: (a) the artifact verbatim, (b) the constraints/'done' criteria, (c) any prior decisions. The `effort` parameter is forwarded but may be silently ignored by Copilot's gemini route — gemini-3.x reasoning is largely auto-applied. The subagent has no access to your scrollback or CLAUDE.md.",
1197
+ baseInstructions: GEMINI_CRITIC_BASE,
1198
+ agentPrompt: "",
1199
+ writeCapable: false,
1200
+ requiresHttp: true
1201
+ },
1202
+ {
1203
+ agentName: "codex-reviewer",
1204
+ toolNameHttp: "codex_reviewer",
1205
+ model: "gpt-5.3-codex",
1206
+ endpoint: "/v1/responses",
1207
+ description: "Line-level code review of a specific diff or file. Backed by gpt-5.3-codex (OpenAI) — the code-specialist sibling of gpt-5.5, trained heavily on code-review datasets so it catches different bugs than Opus. Prefer over codex-critic when the artifact is a concrete diff or single file (codex-critic is for plans/designs). **CALL AFTER: any non-trivial commit (>50 lines OR touching critical paths: streaming, auth, concurrency, persistence, security).** **CALL BEFORE: opening a PR or pushing changes a peer would review.** For diffs >20 KB, split by file-group and call once per group in parallel — each per-call wait is bounded (~150s on v2.1.138). Always pass: (a) the diff or file verbatim, (b) the change's intent, (c) test status. Optionally pass `effort: 'xhigh'` when reviewing security-critical code, `effort: 'medium'` for routine reviews (default 'high'). The subagent has no access to your scrollback or CLAUDE.md.",
1208
+ baseInstructions: REVIEWER_BASE,
1209
+ agentPrompt: "",
1210
+ writeCapable: false,
1211
+ requiresHttp: false
1212
+ }
1213
+ ]);
1214
+ const PERSONAS_WRITE = Object.freeze([{
1215
+ agentName: "codex-implementer",
1216
+ toolNameHttp: "codex_implementer",
1217
+ model: "gpt-5.3-codex",
1218
+ endpoint: "/v1/responses",
1219
+ description: "Targeted implementation of a self-contained coding task — actual file edits via Codex's tool-use sandbox. Backed by gpt-5.3-codex with workspace-write access (only registered when --codex-cli is set). Use only when the task has a clear spec and acceptance criteria; for tasks needing iterative tool-use across many files, prefer a Claude teammate (Agent Team). Always pass: (a) the spec, (b) the files in scope, (c) the acceptance criteria. The subagent has no access to your scrollback or CLAUDE.md.",
1220
+ baseInstructions: IMPLEMENTER_BASE,
1221
+ agentPrompt: "",
1222
+ writeCapable: true,
1223
+ requiresHttp: false
1224
+ }]);
1225
+ /**
1226
+ * Build the agent-prompt body Claude Code uses as the subagent's full
1227
+ * system prompt. The prompt fully replaces Claude Code's default system
1228
+ * prompt (per Anthropic's subagent docs) so it must be self-sufficient.
1229
+ *
1230
+ * Two modes branch on `codexCli`:
1231
+ * - HTTP backend: subagent calls the per-persona tool
1232
+ * `mcp__gh-router-peers__<toolNameHttp>` with `{prompt, context}`;
1233
+ * model + instructions are server-baked.
1234
+ * - codex-cli backend: subagent calls the single
1235
+ * `mcp__codex-cli__codex` tool with `{prompt, model: <persona.model>,
1236
+ * base-instructions: <persona.baseInstructions>}`. Gemini stays on
1237
+ * HTTP regardless because Codex CLI can't run Gemini.
1238
+ */
1239
+ function buildAgentPrompt(persona, opts) {
1240
+ const useStdio = opts.codexCli && !persona.requiresHttp;
1241
+ const toolPath = useStdio ? "mcp__codex-cli__codex" : `mcp__gh-router-peers__${persona.toolNameHttp}`;
1242
+ const invocationBlock = useStdio ? [
1243
+ `Always invoke the \`${toolPath}\` tool with these arguments:`,
1244
+ " - `prompt`: the lead's brief, copied verbatim",
1245
+ ` - \`model\`: "${persona.model}"`,
1246
+ " - `base-instructions`: the persona text below (paste verbatim, do not paraphrase)",
1247
+ ...persona.writeCapable ? [" - `sandbox`: \"workspace-write\"", " - `approval-policy`: \"on-request\""] : [" - `sandbox`: \"read-only\""]
1248
+ ].join("\n") : [
1249
+ `Always invoke the \`${toolPath}\` tool with these arguments:`,
1250
+ " - `prompt`: the lead's brief, copied verbatim",
1251
+ " - `context` (optional): any additional file/diff content the persona needs",
1252
+ "Do NOT pass model or instructions — they are server-baked into this tool."
1253
+ ].join("\n");
1254
+ return [
1255
+ `# Subagent: ${persona.agentName}`,
1256
+ "",
1257
+ persona.baseInstructions,
1258
+ "",
1259
+ "---",
1260
+ "",
1261
+ "## Routing instructions for this subagent",
1262
+ "",
1263
+ invocationBlock,
1264
+ "",
1265
+ "When the tool returns, surface its output to the lead verbatim. Do not summarize, paraphrase, or add your own commentary on top — the lead integrates the persona's reply directly."
1266
+ ].join("\n");
1267
+ }
1268
+ /** Convenience: every persona that should be registered for the given mode. */
1269
+ function personasFor(opts) {
1270
+ const result = [];
1271
+ for (const p of PERSONAS_READ) {
1272
+ if (p.requiresHttp && !opts.geminiAvailable) continue;
1273
+ result.push(p);
1274
+ }
1275
+ if (opts.codexCli) for (const p of PERSONAS_WRITE) result.push(p);
1276
+ return result;
1277
+ }
1278
+
1279
+ //#endregion
1280
+ //#region src/lib/codex-mcp-config.ts
1281
+ /**
1282
+ * Decide which MCP backend serves the codex personas.
1283
+ *
1284
+ * - User passed `--codex-cli` AND codex 0.129+ is on PATH → "cli".
1285
+ * The peer config registers `codex-cli` as a stdio MCP server
1286
+ * spawning `codex mcp-server`; codex personas route there;
1287
+ * gemini-critic stays on the HTTP backend (Codex CLI can't run
1288
+ * Gemini).
1289
+ * - User passed `--codex-cli` but codex is missing or < 0.129 →
1290
+ * fallback to "http" with a warning. Never break
1291
+ * `github-router claude` over a missing optional dep.
1292
+ * - User did not pass `--codex-cli` → "http", read-only personas only.
1293
+ */
1294
+ function resolveCodexCliBackend(opts) {
1295
+ if (!opts.requested) return "http";
1296
+ if (!opts.codexInfo || !opts.codexInfo.ok) {
1297
+ const detail = opts.codexInfo?.version ? `installed version "${opts.codexInfo.version}" is too old (need 0.129+)` : "codex CLI not found on PATH";
1298
+ consola.warn(`--codex-cli requested but ${detail}; falling back to HTTP-only Codex MCP backend (codex-implementer will not be registered).`);
1299
+ return "http";
1300
+ }
1301
+ return "cli";
1302
+ }
1303
+ /**
1304
+ * Build the JSON payload for `claude --mcp-config <path>`.
1305
+ *
1306
+ * Always registers `gh-router-peers` (HTTP) — that's the home of all
1307
+ * read-only personas, and it's the only path Gemini can take. When
1308
+ * `codexCli` is true, also registers `codex-cli` (stdio) which spawns
1309
+ * `codex mcp-server` with the proxy's provider-config flags so codex
1310
+ * runs through our Copilot-routed billing path rather than its
1311
+ * default api.openai.com.
1312
+ */
1313
+ function buildPeerMcpConfig(serverUrl, opts) {
1314
+ const mcpServers = { "gh-router-peers": {
1315
+ type: "http",
1316
+ url: `${serverUrl}/mcp`,
1317
+ headers: { Authorization: `Bearer ${opts.nonce}` }
1318
+ } };
1319
+ if (opts.codexCli) mcpServers["codex-cli"] = {
1320
+ command: "codex",
1321
+ args: ["mcp-server", ...buildCodexProviderConfigFlags(serverUrl)],
1322
+ env: {
1323
+ OPENAI_BASE_URL: `${serverUrl}/v1`,
1324
+ OPENAI_API_KEY: "dummy",
1325
+ CODEX_HOME: opts.codexHome
1326
+ }
1327
+ };
1328
+ return { mcpServers };
1329
+ }
1330
+ /**
1331
+ * The `peer-review-coordinator` Claude Code subagent — the strongest
1332
+ * auto-invocation lever per Phase 2A of the peer-MCP plan. It is NOT a
1333
+ * peer-MCP tool; it is a regular Claude Code subagent that runs inside
1334
+ * the spawned-claude context, has tool access to the peer MCP tools
1335
+ * (codex_critic, codex_reviewer, gemini_critic), and fans out + aggregates.
1336
+ *
1337
+ * The "use proactively" phrase in the description is the documented Claude
1338
+ * Code idiom for subagents the parent (Opus) should delegate to without
1339
+ * explicit user request — at the natural checkpoints listed below.
1340
+ *
1341
+ * Empirically the polling-loop reliability for "use proactively" is ~60%
1342
+ * (claude-code-guide expert estimate); the plan calls for an acceptance
1343
+ * test (≥7/10 sessions delegate at the right checkpoints) before declaring
1344
+ * "auto-invoked". If <7/10 we flip the optional PreToolUse hook on
1345
+ * ExitPlanMode to default-on (env-disable-able).
1346
+ */
1347
+ function buildCoordinatorAgent(opts) {
1348
+ const peers = ["codex-critic"];
1349
+ if (opts.geminiAvailable) peers.push("gemini-critic");
1350
+ peers.push("codex-reviewer");
1351
+ return {
1352
+ description: "Coordinates cross-lab adversarial review. **Use proactively before ExitPlanMode for non-trivial plans and after non-trivial commits** (>50 lines OR touching streaming/auth/concurrency/persistence/security). Routes to codex-critic / codex-reviewer / gemini-critic in parallel based on artifact type and aggregates findings. Cheaper than calling each peer manually for the common case where you want a multi-lab triangulation. The subagent has no access to your scrollback or CLAUDE.md — pass the artifact verbatim.",
1353
+ prompt: [
1354
+ "# Subagent: peer-review-coordinator",
1355
+ "",
1356
+ "You orchestrate cross-lab adversarial review for the lead orchestrator (Opus). You have access to these peer-MCP subagents:",
1357
+ "",
1358
+ peers.map((p) => `- \`${p}\``).join("\n"),
1359
+ "",
1360
+ "## When the lead invokes you",
1361
+ "",
1362
+ "The lead's brief will include an artifact (plan, design, diff, or code) and a goal (e.g. 'review before exit-plan', 'review the commit I just made', 'cross-check codex-critic's verdict'). Pick the right peers for the artifact type:",
1363
+ "",
1364
+ "- **Plan / design / architecture choice** → fan out to `codex-critic`" + (opts.geminiAvailable ? " AND `gemini-critic` in parallel" : "") + ". codex-reviewer is the wrong tool for plans (it's a code-specialist, not an architecture critic).",
1365
+ "- **Concrete diff or single file** → fan out to `codex-reviewer`" + (opts.geminiAvailable ? " AND `gemini-critic` (gemini for cross-lab triangulation)" : "") + ". For very small changes (<20 lines), one `codex-reviewer` call is enough.",
1366
+ "- **Tie-breaker after codex-critic has weighed in** → call `gemini-critic`" + (opts.geminiAvailable ? "" : " (NOT REGISTERED in this session — gemini-3.x not in catalog; tie-break unavailable)") + " with the artifact AND codex-critic's verdict for cross-lab cross-check.",
1367
+ "- **Long-context artifact (>100 KB)** → prefer `gemini-critic`" + (opts.geminiAvailable ? "" : " (NOT REGISTERED in this session)") + ". Otherwise, decompose into 2-4 batches and fan out across `codex-critic` calls in parallel.",
1368
+ "",
1369
+ "## Decomposition for large artifacts",
1370
+ "",
1371
+ "Each per-call MCP wait is bounded (~150s on Claude Code v2.1.138 per regression #50289). For artifacts >20 KB, split into 2-4 logical batches BY CONCERN (not by raw size — semantic batches give better per-batch reviews) and call peers in parallel. The proxy's MCP cap allows up to 8 in-flight calls. Aggregate findings yourself before reporting back.",
1372
+ "",
1373
+ "## Aggregation contract",
1374
+ "",
1375
+ "When fan-out completes, return a SEVERITY-GROUPED, DEDUPLICATED finding list. Format:",
1376
+ "",
1377
+ " ## Findings",
1378
+ " ### HIGH",
1379
+ " 1. <one-line title> — `<file:line>` — sources: codex-critic, gemini-critic (3-lab confirmed if applicable)",
1380
+ " - bug: <one sentence>",
1381
+ " - mitigation: <one sentence>",
1382
+ " ### MEDIUM",
1383
+ " ...",
1384
+ " ### LOW",
1385
+ " ...",
1386
+ "",
1387
+ "Cite which peer raised each finding. If two or more peers raised the SAME finding (cross-lab confirmation), call it out — those are the highest-confidence bugs.",
1388
+ "",
1389
+ "## What NOT to do",
1390
+ "",
1391
+ "- Do not paraphrase or summarize per-peer verdicts BEFORE aggregating; aggregate from the raw verdicts.",
1392
+ "- Do not invent severity labels not present in the source verdicts.",
1393
+ "- Do not call peers serially (waste of wall-clock); always fan out in parallel.",
1394
+ "- Do not consult yourself — you are the coordinator, not a critic.",
1395
+ "",
1396
+ "Self-reminder (read before every reply):",
1397
+ " Did I fan out in parallel to the right peers for this artifact type?",
1398
+ " Did I aggregate findings by severity, citing which peer raised each?",
1399
+ " If two peers agreed, did I flag the cross-lab confirmation?"
1400
+ ].join("\n")
1401
+ };
1402
+ }
1403
+ /**
1404
+ * Build the JSON payload for `claude --agents <path>`.
1405
+ *
1406
+ * Always includes the read-only personas applicable to the mode (gemini
1407
+ * is dropped if absent from the catalog); adds `codex-implementer` only
1408
+ * when `codexCli` is true. Always appends the `peer-review-coordinator`
1409
+ * meta-subagent — the strongest "use proactively" auto-invocation lever
1410
+ * per Phase 2A of the peer-MCP plan.
1411
+ */
1412
+ function buildPeerAgentDefinitions(opts) {
1413
+ const out = {};
1414
+ const personas = personasFor({
1415
+ codexCli: opts.codexCli,
1416
+ geminiAvailable: opts.geminiAvailable
1417
+ });
1418
+ for (const persona of personas) out[persona.agentName] = {
1419
+ description: persona.description,
1420
+ prompt: buildAgentPrompt(persona, { codexCli: opts.codexCli })
1421
+ };
1422
+ out["peer-review-coordinator"] = buildCoordinatorAgent({
1423
+ codexCli: opts.codexCli,
1424
+ geminiAvailable: opts.geminiAvailable
1425
+ });
1426
+ return out;
1427
+ }
1428
+ /**
1429
+ * Default location Claude Code reads subagent .md files from at session
1430
+ * startup. Files placed here populate the Task `subagent_type` enum.
1431
+ *
1432
+ * We pin to the user's `~/.claude/agents/` because `getClaudeCodeEnvVars`
1433
+ * sets `CLAUDE_CONFIG_DIR=$HOME/.claude` (the Spawned-CLI auth isolation
1434
+ * trick) — the spawned child reads from this exact path.
1435
+ */
1436
+ function defaultAgentsDir() {
1437
+ return path.join(os.homedir(), ".claude", "agents");
1438
+ }
1439
+ /**
1440
+ * YAML frontmatter string-escape — sufficient for our use case where
1441
+ * descriptions can contain colons, quotes, newlines. Wraps the value
1442
+ * in double-quotes and escapes:
1443
+ * - `\` and `"` (canonical YAML)
1444
+ * - `\n`, `\r`, `\t` (whitespace controls — `\r` matters on Windows-edited
1445
+ * literals; strict YAML 1.2 parsers reject raw `\r` in double-quoted
1446
+ * scalars)
1447
+ * - other C0 control chars (\x00-\x08, \x0B, \x0C, \x0E-\x1F) and
1448
+ * DEL (\x7F) — encoded as `\xNN` so the YAML stays valid even if
1449
+ * a future description sources data from an external file
1450
+ *
1451
+ * NOT a general-purpose YAML serializer; we control the inputs.
1452
+ */
1453
+ function escapeYamlString(s) {
1454
+ return `"${s.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, (c) => `\\x${c.charCodeAt(0).toString(16).padStart(2, "0")}`)}"`;
1455
+ }
1456
+ /**
1457
+ * Strict allowlist for subagent names — controls both the YAML
1458
+ * frontmatter `name:` field AND the filename suffix. Defense-in-depth:
1459
+ * even if a future contributor wires in a dynamic agent name from
1460
+ * outside, the validator at the top of `writePeerAgentMdFiles` rejects
1461
+ * anything that wouldn't be a safe bare YAML scalar AND a safe path
1462
+ * component.
1463
+ */
1464
+ const VALID_AGENT_NAME = /^[a-z][a-z0-9-]*$/;
1465
+ /** Build a single subagent .md file body (frontmatter + system prompt). */
1466
+ function buildAgentMd(spec) {
1467
+ return [
1468
+ "---",
1469
+ `name: ${spec.name}`,
1470
+ `description: ${escapeYamlString(spec.description)}`,
1471
+ "---",
1472
+ "",
1473
+ spec.prompt,
1474
+ ""
1475
+ ].join("\n");
1476
+ }
1477
+ /**
1478
+ * Write per-launch subagent .md files into the user's `~/.claude/agents/`
1479
+ * directory so they appear in Claude Code's Task `subagent_type` enum
1480
+ * (which `--agents` JSON files do NOT, per claude-code-guide expert).
1481
+ *
1482
+ * Filenames follow `peer-<pid>-<rand>-<agentName>.md` so the boot-time
1483
+ * sweep (`sweepStalePeerAgentMdFiles` in paths.ts) can drop orphans
1484
+ * from crashed prior proxy sessions without touching the user's other
1485
+ * `.claude/agents/` files. The `name:` field in the frontmatter is the
1486
+ * canonical agent identifier — matching across files would cause Claude
1487
+ * Code to (un)deterministically pick one, so concurrent proxies running
1488
+ * the same agents need different filenames but resolve to the same
1489
+ * agent name (intended — they're the same subagent, just registered
1490
+ * twice).
1491
+ *
1492
+ * Returns the file paths plus a cleanup() that unlinks them.
1493
+ */
1494
+ async function writePeerAgentMdFiles(agents, opts) {
1495
+ for (const name$1 of Object.keys(agents)) if (!VALID_AGENT_NAME.test(name$1)) throw new Error(`writePeerAgentMdFiles: invalid agent name ${JSON.stringify(name$1)} — must match ${VALID_AGENT_NAME.source}`);
1496
+ const dir = opts.agentsDir ?? defaultAgentsDir();
1497
+ await fs.mkdir(dir, { recursive: true });
1498
+ const paths = [];
1499
+ try {
1500
+ for (const [name$1, def] of Object.entries(agents)) {
1501
+ const filePath = path.join(dir, `peer-${opts.fileSuffix}-${name$1}.md`);
1502
+ await fs.unlink(filePath).catch(() => {});
1503
+ await writeRuntimeFileSecure(filePath, buildAgentMd({
1504
+ name: name$1,
1505
+ description: def.description,
1506
+ prompt: def.prompt
1507
+ }));
1508
+ paths.push(filePath);
1509
+ }
1510
+ } catch (err) {
1511
+ await Promise.allSettled(paths.map((p) => fs.unlink(p)));
1512
+ throw err;
1513
+ }
1514
+ const cleanup = async () => {
1515
+ await Promise.allSettled(paths.map((p) => fs.unlink(p)));
1516
+ };
1517
+ return {
1518
+ paths,
1519
+ cleanup
1520
+ };
1521
+ }
1522
+ /**
1523
+ * Generate a per-launch nonce, write the MCP config + agents JSON
1524
+ * tempfiles under `CLAUDE_RUNTIME_DIR` with mode 0o600 and `O_EXCL`,
1525
+ * and return a `cleanup()` to unlink them on shutdown.
1526
+ *
1527
+ * Filenames are `peer-mcp-<pid>-<rand>.json` and `peer-agents-<pid>-<rand>.json`.
1528
+ * The PID prefix is what the boot-time sweep (`sweepStaleRuntimeFiles` in
1529
+ * paths.ts) keys off to drop orphans from crashed prior sessions; the
1530
+ * random suffix prevents two concurrent calls within the same process
1531
+ * from clobbering each other's files (e.g., a proxy that internally
1532
+ * relaunches its spawned child without restarting itself).
1533
+ */
1534
+ async function writePeerMcpRuntimeFiles(serverUrl, opts) {
1535
+ const nonce = opts.nonce ?? randomBytes(32).toString("hex");
1536
+ const runtimeDir = opts.runtimeDir ?? PATHS.CLAUDE_RUNTIME_DIR;
1537
+ const codexHome = opts.codexHome ?? PATHS.CODEX_HOME;
1538
+ await fs.mkdir(runtimeDir, { recursive: true });
1539
+ if (process.platform !== "win32") await fs.chmod(runtimeDir, 448).catch(() => {});
1540
+ const fileSuffix = `${process.pid}-${randomBytes(4).toString("hex")}`;
1541
+ const mcpConfigPath = path.join(runtimeDir, `peer-mcp-${fileSuffix}.json`);
1542
+ const agentsPath = path.join(runtimeDir, `peer-agents-${fileSuffix}.json`);
1543
+ const mcpConfig = buildPeerMcpConfig(serverUrl, {
1544
+ codexCli: opts.codexCli,
1545
+ geminiAvailable: opts.geminiAvailable,
1546
+ nonce,
1547
+ codexHome
1548
+ });
1549
+ const agents = buildPeerAgentDefinitions({
1550
+ codexCli: opts.codexCli,
1551
+ geminiAvailable: opts.geminiAvailable,
1552
+ nonce,
1553
+ codexHome
1554
+ });
1555
+ await fs.unlink(mcpConfigPath).catch(() => {});
1556
+ await fs.unlink(agentsPath).catch(() => {});
1557
+ await writeRuntimeFileSecure(mcpConfigPath, JSON.stringify(mcpConfig, null, 2));
1558
+ await writeRuntimeFileSecure(agentsPath, JSON.stringify(agents, null, 2));
1559
+ const mdResult = await writePeerAgentMdFiles(agents, {
1560
+ agentsDir: opts.agentsDir,
1561
+ fileSuffix
1562
+ });
1563
+ const personas = personasFor({
1564
+ codexCli: opts.codexCli,
1565
+ geminiAvailable: opts.geminiAvailable
1566
+ });
1567
+ const cleanup = async () => {
1568
+ await Promise.allSettled([
1569
+ fs.unlink(mcpConfigPath),
1570
+ fs.unlink(agentsPath),
1571
+ mdResult.cleanup()
1572
+ ]);
1573
+ };
1574
+ return {
1575
+ mcpConfigPath,
1576
+ agentsPath,
1577
+ agentMdPaths: mdResult.paths,
1578
+ nonce,
1579
+ personas,
1580
+ cleanup
1581
+ };
1582
+ }
1583
+
1584
+ //#endregion
1585
+ //#region src/lib/file-log-reporter.ts
1586
+ const MAX_LOG_BYTES = 1024 * 1024;
1587
+ const DEDUP_MAX = 1e3;
1588
+ const ARG_MAX_LEN = 2048;
1589
+ const DEDUP_KEY_MAX_LEN = 200;
1590
+ const CREDENTIAL_RE = /\b(eyJ[A-Za-z0-9_-]{20,}(?:\.[A-Za-z0-9_-]+){0,2}|gh[opsu]_[A-Za-z0-9_]{20,}|Bearer\s+\S{20,})\b/g;
1591
+ const ALLOWED_TYPES = new Set([
1592
+ "fatal",
1593
+ "error",
1594
+ "warn"
1595
+ ]);
1596
+ function sanitize(line) {
1597
+ return line.replace(CREDENTIAL_RE, "[REDACTED]");
1598
+ }
1599
+ function serializeArg(arg) {
1600
+ if (typeof arg === "string") return arg;
1601
+ if (arg instanceof Error) {
1602
+ const parts = [arg.message];
1603
+ if (arg.stack) parts.push(arg.stack);
1604
+ return parts.join("\n");
1605
+ }
1606
+ return String(arg);
1607
+ }
1608
+ function formatLogLine(logObj) {
1609
+ return sanitize(`${logObj.date.toISOString()} [${(logObj.type ?? "error").toUpperCase()}] ${logObj.args.map((a) => {
1610
+ const s = serializeArg(a);
1611
+ return s.length > ARG_MAX_LEN ? s.slice(0, ARG_MAX_LEN) + "…" : s;
1612
+ }).join(" ").replace(/\r\n|\r|\n/g, "\\n")}\n`);
1613
+ }
1614
+ function makeDedupeKey(logObj) {
1615
+ const firstArg = logObj.args.length > 0 ? serializeArg(logObj.args[0]) : "";
1616
+ const key = `${logObj.type}:${firstArg}`;
1617
+ return key.length > DEDUP_KEY_MAX_LEN ? key.slice(0, DEDUP_KEY_MAX_LEN) : key;
1618
+ }
1619
+ function rotateIfNeeded(filePath) {
1620
+ let size;
1621
+ try {
1622
+ size = fs$1.statSync(filePath).size;
1623
+ } catch {
1624
+ return;
1625
+ }
1626
+ if (size <= MAX_LOG_BYTES) return;
1627
+ try {
1628
+ fs$1.renameSync(filePath, filePath + ".1");
1629
+ } catch {}
1630
+ }
1631
+ var FileLogReporter = class {
1632
+ filePath;
1633
+ seen = /* @__PURE__ */ new Set();
1634
+ constructor(filePath) {
1635
+ this.filePath = filePath;
1636
+ rotateIfNeeded(filePath);
1637
+ }
1638
+ log(logObj, _ctx) {
1639
+ if (!ALLOWED_TYPES.has(logObj.type)) return;
1640
+ const key = makeDedupeKey(logObj);
1641
+ if (this.seen.has(key)) return;
1642
+ if (this.seen.size >= DEDUP_MAX) this.seen.clear();
1643
+ this.seen.add(key);
1644
+ const line = formatLogLine(logObj);
1645
+ let fd;
1646
+ try {
1647
+ fd = fs$1.openSync(this.filePath, "a", 384);
1648
+ fs$1.writeSync(fd, line);
1649
+ } catch {} finally {
1650
+ if (fd !== void 0) try {
1651
+ fs$1.closeSync(fd);
1652
+ } catch {}
1653
+ }
1654
+ }
1655
+ };
1656
+ const nullStream = new Writable({ write(_chunk, _encoding, cb) {
1657
+ cb();
1658
+ } });
1659
+ /**
1660
+ * Switch consola to file-only mode for TUI sessions.
1661
+ * Removes the terminal reporter and installs a file reporter that
1662
+ * persists errors and warnings to disk with dedup and credential scrubbing.
1663
+ *
1664
+ * Also sinks consola's stdout/stderr streams as belt-and-suspenders:
1665
+ * even if a terminal reporter is re-added, it cannot write to the terminal.
1666
+ * Crash handlers that call process.stderr.write() directly are unaffected.
1667
+ * FileLogReporter uses fs.writeSync() directly and is also unaffected.
1668
+ */
1669
+ function enableFileLogging() {
1670
+ const reporter = new FileLogReporter(PATHS.ERROR_LOG_PATH);
1671
+ consola.options.throttle = 0;
1672
+ consola.setReporters([reporter]);
1673
+ consola.options.stdout = nullStream;
1674
+ consola.options.stderr = nullStream;
1675
+ }
1676
+
917
1677
  //#endregion
918
1678
  //#region src/lib/model-validation.ts
919
1679
  const ENDPOINT_ALIASES = {
@@ -1008,6 +1768,11 @@ function initProxyFromEnv() {
1008
1768
  }
1009
1769
  }
1010
1770
 
1771
+ //#endregion
1772
+ //#region package.json
1773
+ var name = "github-router";
1774
+ var version = "0.3.18";
1775
+
1011
1776
  //#endregion
1012
1777
  //#region src/lib/approval.ts
1013
1778
  const awaitApproval = async () => {
@@ -1016,8 +1781,27 @@ const awaitApproval = async () => {
1016
1781
 
1017
1782
  //#endregion
1018
1783
  //#region src/lib/rate-limit.ts
1784
+ const RATE_LIMIT_QUEUE_TIMEOUT_MS = 5e3;
1785
+ let rateLimitChain = Promise.resolve();
1019
1786
  async function checkRateLimit(state$1) {
1020
1787
  if (state$1.rateLimitSeconds === void 0) return;
1788
+ const ticket = { aborted: false };
1789
+ const myTurn = rateLimitChain.then(() => doCheck(state$1, ticket));
1790
+ rateLimitChain = myTurn.catch(() => {});
1791
+ return Promise.race([myTurn, sleep(RATE_LIMIT_QUEUE_TIMEOUT_MS).then(() => {
1792
+ ticket.aborted = true;
1793
+ throw new HTTPError("Rate limit queue wait exceeded", Response.json({
1794
+ type: "error",
1795
+ error: {
1796
+ type: "rate_limit_error",
1797
+ message: `Rate limit queue exceeded ${RATE_LIMIT_QUEUE_TIMEOUT_MS}ms; try again`
1798
+ }
1799
+ }, { status: 429 }));
1800
+ })]);
1801
+ }
1802
+ async function doCheck(state$1, ticket) {
1803
+ if (state$1.rateLimitSeconds === void 0) return;
1804
+ if (ticket.aborted) return;
1021
1805
  const now = Date.now();
1022
1806
  if (!state$1.lastRequestTimestamp) {
1023
1807
  state$1.lastRequestTimestamp = now;
@@ -1036,6 +1820,7 @@ async function checkRateLimit(state$1) {
1036
1820
  const waitTimeMs = waitTimeSeconds * 1e3;
1037
1821
  consola.warn(`Rate limit reached. Waiting ${waitTimeSeconds} seconds before proceeding...`);
1038
1822
  await sleep(waitTimeMs);
1823
+ if (ticket.aborted) return;
1039
1824
  state$1.lastRequestTimestamp = Date.now();
1040
1825
  consola.info("Rate limit wait completed, proceeding with request");
1041
1826
  }
@@ -1098,6 +1883,169 @@ function detectCapabilityMismatch(info, model) {
1098
1883
  return err.includes("token") || err.includes("context") || err.includes("too long") || err.includes("max_tokens") || err.includes("prompt is too long");
1099
1884
  }
1100
1885
 
1886
+ //#endregion
1887
+ //#region src/lib/stream-relay.ts
1888
+ const ENCODER$2 = new TextEncoder();
1889
+ /**
1890
+ * Detect the family of "controller has already closed" errors that Bun and
1891
+ * the WHATWG streams runtime throw when an enqueue/close call races with
1892
+ * the consumer cancelling its read. These are NOT upstream failures — they
1893
+ * mean the client has finished reading (or disconnected) and we should
1894
+ * exit pull() quietly without trying to write more bytes or log noise.
1895
+ *
1896
+ * Bun's wording: `TypeError: Invalid state: Controller is already closed`.
1897
+ * Other runtimes use `TypeError: The stream is closing` or
1898
+ * `TypeError: This ReadableStream is closed` or include "errored" / "cancelled".
1899
+ */
1900
+ function isControllerClosedError(error) {
1901
+ if (!(error instanceof Error)) return false;
1902
+ const msg = error.message.toLowerCase();
1903
+ return msg.includes("controller is already closed") || msg.includes("controller is already errored") || msg.includes("readablestream is closed") || msg.includes("readablestream is already closed") || msg.includes("stream is closing") || msg.includes("stream is already closed") || msg.includes("stream is closed");
1904
+ }
1905
+ /**
1906
+ * Wrap an upstream SSE byte stream so that:
1907
+ * - Backpressure is respected (pull-based; only reads when downstream demands).
1908
+ * - Mid-stream errors (undici "terminated", AbortError, network resets) are
1909
+ * caught, logged with structured context, and converted to a final
1910
+ * Anthropic-shape `event: error` SSE event before the downstream is closed.
1911
+ * - Upstream inactivity (no chunk for `inactivityTimeoutMs`) is treated as a
1912
+ * soft failure that emits an error event rather than hanging forever.
1913
+ * - Consumer cancellation (client disconnects mid-read or finishes early)
1914
+ * is recognized and handled silently — NOT logged as an upstream error,
1915
+ * NOT followed by a futile event:error write that can corrupt the
1916
+ * terminal bytes the client has already buffered.
1917
+ *
1918
+ * Pre-byte upstream errors (failure on the very first read) are handled by
1919
+ * the same code path: an `event: error` SSE event is emitted on a 200
1920
+ * response, then the connection is closed. Even if the consumer's SDK
1921
+ * silently swallows `event: error`, the immediate close triggers the
1922
+ * client's socket-disconnect handler — the user always sees an error
1923
+ * string, never a hang.
1924
+ */
1925
+ function relayAnthropicStream(body, opts) {
1926
+ const inactivityMs = opts.inactivityTimeoutMs ?? UPSTREAM_INACTIVITY_TIMEOUT_MS;
1927
+ const reader = body.getReader();
1928
+ let bytesRelayed = 0;
1929
+ let upstreamFinished = false;
1930
+ let consumerCancelled = false;
1931
+ const safeClose = (controller) => {
1932
+ try {
1933
+ controller.close();
1934
+ } catch {}
1935
+ };
1936
+ return new ReadableStream({
1937
+ async pull(controller) {
1938
+ if (consumerCancelled || upstreamFinished) {
1939
+ safeClose(controller);
1940
+ return;
1941
+ }
1942
+ try {
1943
+ const result = await readWithInactivityTimeout(reader, inactivityMs);
1944
+ if (consumerCancelled) {
1945
+ safeClose(controller);
1946
+ return;
1947
+ }
1948
+ if (result.done) {
1949
+ if (bytesRelayed === 0) consola.warn(`Upstream returned empty SSE stream at ${opts.routePath}`);
1950
+ upstreamFinished = true;
1951
+ safeClose(controller);
1952
+ return;
1953
+ }
1954
+ if (result.value) {
1955
+ bytesRelayed += result.value.byteLength;
1956
+ try {
1957
+ controller.enqueue(result.value);
1958
+ } catch (enqueueError) {
1959
+ if (isControllerClosedError(enqueueError)) {
1960
+ consumerCancelled = true;
1961
+ return;
1962
+ }
1963
+ throw enqueueError;
1964
+ }
1965
+ }
1966
+ } catch (error) {
1967
+ upstreamFinished = true;
1968
+ if (consumerCancelled) {
1969
+ reader.cancel(error).catch(() => {});
1970
+ safeClose(controller);
1971
+ return;
1972
+ }
1973
+ const errName = error instanceof Error ? error.name : "Error";
1974
+ const errMessage = error instanceof Error ? error.message : String(error);
1975
+ consola.error(`Upstream stream interrupted at ${opts.routePath}: bytes=${bytesRelayed} errType=${errName} message=${JSON.stringify(errMessage)}`);
1976
+ const event = buildAnthropicErrorEvent(errName, errMessage);
1977
+ try {
1978
+ controller.enqueue(ENCODER$2.encode(event));
1979
+ } catch (enqueueError) {
1980
+ if (!isControllerClosedError(enqueueError)) consola.warn(`Could not deliver error event to consumer at ${opts.routePath}: ${enqueueError instanceof Error ? enqueueError.message : String(enqueueError)}`);
1981
+ }
1982
+ reader.cancel(error).catch(() => {});
1983
+ safeClose(controller);
1984
+ }
1985
+ },
1986
+ cancel(reason) {
1987
+ consumerCancelled = true;
1988
+ upstreamFinished = true;
1989
+ reader.cancel(reason).catch(() => {});
1990
+ }
1991
+ });
1992
+ }
1993
+ async function readWithInactivityTimeout(reader, timeoutMs) {
1994
+ let timeoutHandle;
1995
+ const timeoutPromise = new Promise((_, reject) => {
1996
+ timeoutHandle = setTimeout(() => {
1997
+ reject(Object.assign(/* @__PURE__ */ new Error("upstream_inactive"), { name: "InactivityTimeout" }));
1998
+ }, timeoutMs);
1999
+ });
2000
+ timeoutPromise.catch(() => {});
2001
+ try {
2002
+ return await Promise.race([reader.read(), timeoutPromise]);
2003
+ } finally {
2004
+ if (timeoutHandle !== void 0) clearTimeout(timeoutHandle);
2005
+ }
2006
+ }
2007
+ /**
2008
+ * Build the SSE wire bytes for an Anthropic-format streaming error event.
2009
+ * Per Anthropic streaming spec, errors are sent as:
2010
+ * event: error
2011
+ * data: {"type":"error","error":{"type":"...","message":"..."}}
2012
+ */
2013
+ function buildAnthropicErrorEvent(errName, errMessage) {
2014
+ const payload = {
2015
+ type: "error",
2016
+ error: {
2017
+ type: classifyStreamError(errName),
2018
+ message: `Upstream stream interrupted: ${errName}: ${errMessage}`
2019
+ }
2020
+ };
2021
+ return `event: error\ndata: ${JSON.stringify(payload)}\n\n`;
2022
+ }
2023
+ /**
2024
+ * Build the SSE wire bytes for an OpenAI-format streaming error event,
2025
+ * followed by the `data: [DONE]` terminator that OpenAI clients expect.
2026
+ */
2027
+ function buildOpenAIErrorEvent(errName, errMessage) {
2028
+ const payload = { error: {
2029
+ type: classifyStreamError(errName),
2030
+ message: `Upstream stream interrupted: ${errName}: ${errMessage}`
2031
+ } };
2032
+ return `data: ${JSON.stringify(payload)}\n\ndata: [DONE]\n\n`;
2033
+ }
2034
+ function classifyStreamError(errName) {
2035
+ if (errName === "AbortError") return "timeout_error";
2036
+ if (errName === "InactivityTimeout") return "timeout_error";
2037
+ return "api_error";
2038
+ }
2039
+ function logStreamError(routePath, error) {
2040
+ const errName = error instanceof Error ? error.name : "Error";
2041
+ const errMessage = error instanceof Error ? error.message : String(error);
2042
+ consola.error(`Upstream stream interrupted at ${routePath}: errType=${errName} message=${JSON.stringify(errMessage)}`);
2043
+ return {
2044
+ errName,
2045
+ errMessage
2046
+ };
2047
+ }
2048
+
1101
2049
  //#endregion
1102
2050
  //#region src/lib/tokenizer.ts
1103
2051
  const ENCODING_MAP = {
@@ -1296,20 +2244,29 @@ const getTokenCount = async (payload, model) => {
1296
2244
 
1297
2245
  //#endregion
1298
2246
  //#region src/services/copilot/create-chat-completions.ts
1299
- const createChatCompletions = async (payload, modelHeaders) => {
2247
+ const createChatCompletions = async (payload, modelHeaders, callerSignal) => {
1300
2248
  if (!state.copilotToken) throw new Error("Copilot token not found");
1301
2249
  const enableVision = payload.messages.some((x) => typeof x.content !== "string" && x.content?.some((x$1) => x$1.type === "image_url"));
1302
2250
  const isAgentCall = payload.messages.some((msg) => ["assistant", "tool"].includes(msg.role));
1303
- const headers = {
1304
- ...copilotHeaders(state, enableVision),
1305
- ...modelHeaders,
1306
- "X-Initiator": isAgentCall ? "agent" : "user"
2251
+ const url = `${copilotBaseUrl(state)}/chat/completions`;
2252
+ const doFetch = () => {
2253
+ const fetchInit = {
2254
+ method: "POST",
2255
+ headers: {
2256
+ ...copilotHeaders(state, enableVision),
2257
+ ...modelHeaders,
2258
+ "X-Initiator": isAgentCall ? "agent" : "user"
2259
+ },
2260
+ body: JSON.stringify(payload)
2261
+ };
2262
+ const signals = [];
2263
+ if (UPSTREAM_FETCH_TIMEOUT_MS > 0) signals.push(AbortSignal.timeout(UPSTREAM_FETCH_TIMEOUT_MS));
2264
+ if (callerSignal) signals.push(callerSignal);
2265
+ if (signals.length === 1) fetchInit.signal = signals[0];
2266
+ else if (signals.length > 1) fetchInit.signal = AbortSignal.any(signals);
2267
+ return fetch(url, fetchInit);
1307
2268
  };
1308
- const response = await fetch(`${copilotBaseUrl(state)}/chat/completions`, {
1309
- method: "POST",
1310
- headers,
1311
- body: JSON.stringify(payload)
1312
- });
2269
+ const response = await tryRefreshAndRetry(doFetch, "/chat/completions");
1313
2270
  if (!response.ok) {
1314
2271
  let errorBody = "";
1315
2272
  try {
@@ -1352,23 +2309,28 @@ const InnerSchema = z.object({
1352
2309
  annotations: z.array(z.object({ url_citation: z.object({
1353
2310
  title: z.string(),
1354
2311
  url: z.string()
1355
- }).optional() })).optional()
2312
+ }).optional() })).nullable().optional()
1356
2313
  }),
1357
- bing_searches: z.array(z.unknown()).optional()
2314
+ bing_searches: z.array(z.unknown()).nullable().optional()
1358
2315
  });
1359
2316
  const MAX_SEARCHES_PER_SECOND = 3;
1360
2317
  let searchTimestamps = [];
2318
+ let throttleChain = Promise.resolve();
1361
2319
  async function throttleSearch() {
1362
- const now = Date.now();
1363
- searchTimestamps = searchTimestamps.filter((t) => now - t < 1e3);
1364
- if (searchTimestamps.length >= MAX_SEARCHES_PER_SECOND) {
1365
- const waitMs = 1e3 - (now - searchTimestamps[0]);
1366
- if (waitMs > 0) {
1367
- consola.debug(`Web search rate limited, waiting ${waitMs}ms`);
1368
- await sleep(waitMs);
2320
+ const myTurn = throttleChain.then(async () => {
2321
+ const now = Date.now();
2322
+ searchTimestamps = searchTimestamps.filter((t) => now - t < 1e3);
2323
+ if (searchTimestamps.length >= MAX_SEARCHES_PER_SECOND) {
2324
+ const waitMs = 1e3 - (now - searchTimestamps[0]);
2325
+ if (waitMs > 0) {
2326
+ consola.debug(`Web search rate limited, waiting ${waitMs}ms`);
2327
+ await sleep(waitMs);
2328
+ }
1369
2329
  }
1370
- }
1371
- searchTimestamps.push(Date.now());
2330
+ searchTimestamps.push(Date.now());
2331
+ });
2332
+ throttleChain = myTurn.catch(() => {});
2333
+ return myTurn;
1372
2334
  }
1373
2335
  function mcpHeaders(sid) {
1374
2336
  if (!state.githubToken) throw new Error("GitHub token missing — re-run auth flow. Web search uses the GitHub PAT (not the Copilot token); the on-disk token at ~/.local/share/github-router/github_token must be present.");
@@ -1497,6 +2459,14 @@ async function searchWeb(query) {
1497
2459
 
1498
2460
  //#endregion
1499
2461
  //#region src/routes/chat-completions/handler.ts
2462
+ const ENCODER$1 = new TextEncoder();
2463
+ function formatSSE$1(chunk) {
2464
+ const parts = [];
2465
+ if (chunk.event) parts.push(`event: ${chunk.event}`);
2466
+ if (chunk.data !== void 0) for (const line of String(chunk.data).split(/\r\n|\r|\n/)) parts.push(`data: ${line}`);
2467
+ if (chunk.id !== void 0) parts.push(`id: ${String(chunk.id)}`);
2468
+ return parts.join("\n") + "\n\n";
2469
+ }
1500
2470
  async function handleCompletion$1(c) {
1501
2471
  const startTime = Date.now();
1502
2472
  await checkRateLimit(state);
@@ -1545,16 +2515,91 @@ async function handleCompletion$1(c) {
1545
2515
  inputTokens,
1546
2516
  outputTokens,
1547
2517
  status: 200,
1548
- streaming: isStreaming
1549
- }, selectedModel, startTime);
1550
- if (!isStreaming) {
1551
- if (debugEnabled) consola.debug("Non-streaming response:", JSON.stringify(response));
1552
- return c.json(response);
1553
- }
1554
- return streamSSE(c, async (stream) => {
1555
- for await (const chunk of response) {
1556
- if (debugEnabled) consola.debug("Streaming chunk:", JSON.stringify(chunk));
1557
- await stream.writeSSE(chunk);
2518
+ streaming: isStreaming
2519
+ }, selectedModel, startTime);
2520
+ if (!isStreaming) {
2521
+ if (debugEnabled) consola.debug("Non-streaming response:", JSON.stringify(response));
2522
+ return c.json(response);
2523
+ }
2524
+ const iterator = response[Symbol.asyncIterator]();
2525
+ const firstResult = await iterator.next();
2526
+ if (firstResult.done) consola.warn(`Upstream /chat/completions returned an empty stream at ${c.req.path}`);
2527
+ let pendingFirstChunk = firstResult.done ? void 0 : firstResult.value;
2528
+ let upstreamFinished = firstResult.done;
2529
+ let consumerCancelled = false;
2530
+ const safeClose = (controller) => {
2531
+ try {
2532
+ controller.close();
2533
+ } catch {}
2534
+ };
2535
+ const releaseUpstream = (reason) => {
2536
+ if (typeof iterator.return === "function") iterator.return(reason).catch(() => {});
2537
+ };
2538
+ const safeEnqueue = (controller, bytes) => {
2539
+ try {
2540
+ controller.enqueue(bytes);
2541
+ return true;
2542
+ } catch (e) {
2543
+ if (isControllerClosedError(e)) {
2544
+ consumerCancelled = true;
2545
+ releaseUpstream(e);
2546
+ return false;
2547
+ }
2548
+ throw e;
2549
+ }
2550
+ };
2551
+ return new Response(new ReadableStream({
2552
+ async pull(controller) {
2553
+ if (consumerCancelled || upstreamFinished) {
2554
+ safeClose(controller);
2555
+ return;
2556
+ }
2557
+ if (pendingFirstChunk !== void 0) {
2558
+ const chunk = pendingFirstChunk;
2559
+ pendingFirstChunk = void 0;
2560
+ if (debugEnabled) consola.debug("Streaming chunk:", JSON.stringify(chunk));
2561
+ safeEnqueue(controller, ENCODER$1.encode(formatSSE$1(chunk)));
2562
+ return;
2563
+ }
2564
+ try {
2565
+ const result = await iterator.next();
2566
+ if (consumerCancelled) {
2567
+ safeClose(controller);
2568
+ return;
2569
+ }
2570
+ if (result.done) {
2571
+ upstreamFinished = true;
2572
+ safeClose(controller);
2573
+ return;
2574
+ }
2575
+ if (result.value === void 0 || result.value === null) return;
2576
+ if (debugEnabled) consola.debug("Streaming chunk:", JSON.stringify(result.value));
2577
+ safeEnqueue(controller, ENCODER$1.encode(formatSSE$1(result.value)));
2578
+ } catch (error) {
2579
+ upstreamFinished = true;
2580
+ if (consumerCancelled) {
2581
+ releaseUpstream(error);
2582
+ safeClose(controller);
2583
+ return;
2584
+ }
2585
+ const { errName, errMessage } = logStreamError(c.req.path, error);
2586
+ safeEnqueue(controller, ENCODER$1.encode(buildOpenAIErrorEvent(errName, errMessage)));
2587
+ releaseUpstream(error);
2588
+ safeClose(controller);
2589
+ }
2590
+ },
2591
+ cancel() {
2592
+ consumerCancelled = true;
2593
+ upstreamFinished = true;
2594
+ releaseUpstream();
2595
+ }
2596
+ }), {
2597
+ status: 200,
2598
+ headers: {
2599
+ "content-type": "text/event-stream",
2600
+ "cache-control": "no-cache",
2601
+ "transfer-encoding": "chunked",
2602
+ connection: "keep-alive"
1558
2603
  }
1559
2604
  });
1560
2605
  }
@@ -1637,6 +2682,450 @@ embeddingRoutes.post("/", async (c) => {
1637
2682
  }
1638
2683
  });
1639
2684
 
2685
+ //#endregion
2686
+ //#region src/services/copilot/create-responses.ts
2687
+ const createResponses = async (payload, modelHeaders, callerSignal) => {
2688
+ if (!state.copilotToken) throw new Error("Copilot token not found");
2689
+ const enableVision = detectVision(payload.input);
2690
+ const isAgentCall = detectAgentCall(payload.input);
2691
+ const url = `${copilotBaseUrl(state)}/responses`;
2692
+ const doFetch = () => {
2693
+ const fetchInit = {
2694
+ method: "POST",
2695
+ headers: {
2696
+ ...copilotHeaders(state, enableVision),
2697
+ ...modelHeaders,
2698
+ "X-Initiator": isAgentCall ? "agent" : "user"
2699
+ },
2700
+ body: JSON.stringify(payload)
2701
+ };
2702
+ const signals = [];
2703
+ if (UPSTREAM_FETCH_TIMEOUT_MS > 0) signals.push(AbortSignal.timeout(UPSTREAM_FETCH_TIMEOUT_MS));
2704
+ if (callerSignal) signals.push(callerSignal);
2705
+ if (signals.length === 1) fetchInit.signal = signals[0];
2706
+ else if (signals.length > 1) fetchInit.signal = AbortSignal.any(signals);
2707
+ return fetch(url, fetchInit);
2708
+ };
2709
+ const response = await tryRefreshAndRetry(doFetch, "/responses");
2710
+ if (!response.ok) {
2711
+ consola.error("Failed to create responses", response);
2712
+ throw new HTTPError("Failed to create responses", response);
2713
+ }
2714
+ if (payload.stream) return events(response);
2715
+ return await response.json();
2716
+ };
2717
+ function detectVision(input) {
2718
+ if (typeof input === "string") return false;
2719
+ if (!Array.isArray(input)) return false;
2720
+ return input.some((item) => {
2721
+ if ("content" in item && Array.isArray(item.content)) return item.content.some((part) => part.type === "input_image");
2722
+ return false;
2723
+ });
2724
+ }
2725
+ function detectAgentCall(input) {
2726
+ if (typeof input === "string") return false;
2727
+ if (!Array.isArray(input)) return false;
2728
+ return input.some((item) => {
2729
+ if ("role" in item && item.role === "assistant") return true;
2730
+ if ("type" in item && (item.type === "function_call" || item.type === "function_call_output")) return true;
2731
+ return false;
2732
+ });
2733
+ }
2734
+
2735
+ //#endregion
2736
+ //#region src/routes/mcp/handler.ts
2737
+ const MCP_PROTOCOL_VERSION = "2025-06-18";
2738
+ const SERVER_NAME = "github-router-peers";
2739
+ const SERVER_VERSION = "1";
2740
+ /**
2741
+ * Reasoning effort levels accepted by Copilot's /v1/responses (gpt-5.x) and
2742
+ * /v1/chat/completions endpoints. Per the proxy's existing thinking-mode
2743
+ * translator (CLAUDE.md "Thinking-mode translation"), Copilot's adaptive-
2744
+ * thinking path uses these same buckets:
2745
+ * <2k tokens → low, <8k → medium, <24k → high, else → xhigh.
2746
+ *
2747
+ * Default `high` for peer reviews — adversarial-by-design but still cost-
2748
+ * conscious. Callers can pass `xhigh` explicitly for deep dives, or `medium`
2749
+ * for quick sanity checks.
2750
+ */
2751
+ const EFFORT_LEVELS = [
2752
+ "low",
2753
+ "medium",
2754
+ "high",
2755
+ "xhigh"
2756
+ ];
2757
+ const DEFAULT_EFFORT = "high";
2758
+ function isEffort(v) {
2759
+ return typeof v === "string" && EFFORT_LEVELS.includes(v);
2760
+ }
2761
+ /** Bounded concurrency. Originally capped at 2 (commit 4317a25) as a defensive
2762
+ * pre-launch guess against Opus's natural pattern of fanning out to all three
2763
+ * critics at once. Raised to 8 (Phase 2D of the peer-MCP plan) so the
2764
+ * decomposition pattern Phase 2B teaches Opus — "split a >20 KB artifact
2765
+ * into 2-4 batches and call in parallel" — can actually run in parallel
2766
+ * without the (3+)th call returning isError "queue full". The persona
2767
+ * handlers (`callPersona`) hold no shared mutable state — there's no race
2768
+ * the cap is hiding; the upstream Copilot's own rate-limit (surfaced as a
2769
+ * per-call 429 → tool isError) is the real backpressure mechanism. 8 covers
2770
+ * a 7-fork wave with one slot of headroom and is still a hard upper bound
2771
+ * against runaway clients. See docs/research/peer-mcp-investigation.md
2772
+ * § "Concurrency cap investigation" for the full justification. */
2773
+ const MAX_INFLIGHT_TOOLS_CALL = 8;
2774
+ let inFlightToolsCall = 0;
2775
+ const RPC_PARSE_ERROR = -32700;
2776
+ const RPC_INVALID_REQUEST = -32600;
2777
+ const RPC_METHOD_NOT_FOUND = -32601;
2778
+ const RPC_INVALID_PARAMS = -32602;
2779
+ const RPC_INTERNAL_ERROR = -32603;
2780
+ function rpcError(id, code, message, data) {
2781
+ return {
2782
+ jsonrpc: "2.0",
2783
+ id: id ?? null,
2784
+ error: data === void 0 ? {
2785
+ code,
2786
+ message
2787
+ } : {
2788
+ code,
2789
+ message,
2790
+ data
2791
+ }
2792
+ };
2793
+ }
2794
+ function rpcResult(id, result) {
2795
+ return {
2796
+ jsonrpc: "2.0",
2797
+ id: id ?? null,
2798
+ result
2799
+ };
2800
+ }
2801
+ function isLoopbackHost(host) {
2802
+ if (!host) return false;
2803
+ const idx = host.lastIndexOf(":");
2804
+ const hostname = idx >= 0 ? host.slice(0, idx) : host;
2805
+ return hostname === "127.0.0.1" || hostname === "localhost";
2806
+ }
2807
+ /**
2808
+ * Constant-time bearer compare. Random per-launch nonces aren't really
2809
+ * timing-attackable in practice, but this costs nothing.
2810
+ */
2811
+ function nonceMatches(provided, expected) {
2812
+ if (provided.length !== expected.length) return false;
2813
+ const a = Buffer.from(provided);
2814
+ const b = Buffer.from(expected);
2815
+ try {
2816
+ return timingSafeEqual(a, b);
2817
+ } catch {
2818
+ return false;
2819
+ }
2820
+ }
2821
+ function checkAuth(c) {
2822
+ if (!isLoopbackHost(c.req.header("host"))) return {
2823
+ ok: false,
2824
+ status: 403,
2825
+ reason: "non-loopback Host header rejected"
2826
+ };
2827
+ const expected = state.peerMcpNonce;
2828
+ if (!expected) return {
2829
+ ok: false,
2830
+ status: 401,
2831
+ reason: "/mcp not enabled in this proxy session"
2832
+ };
2833
+ const auth$1 = c.req.header("authorization") ?? "";
2834
+ const m = /^Bearer\s+(.+)$/i.exec(auth$1);
2835
+ if (!m || !nonceMatches(m[1], expected)) return {
2836
+ ok: false,
2837
+ status: 401,
2838
+ reason: "missing or invalid Authorization bearer"
2839
+ };
2840
+ return { ok: true };
2841
+ }
2842
+ function geminiAvailable() {
2843
+ const models = state.models?.data;
2844
+ if (!models) return false;
2845
+ return models.some((m) => /^gemini-3\..*pro/i.test(m.id));
2846
+ }
2847
+ function activePersonas() {
2848
+ return PERSONAS_READ.filter((p) => !p.requiresHttp || geminiAvailable());
2849
+ }
2850
+ function toolEntries() {
2851
+ return activePersonas().map((p) => ({
2852
+ name: p.toolNameHttp,
2853
+ description: p.description,
2854
+ inputSchema: {
2855
+ type: "object",
2856
+ required: ["prompt"],
2857
+ additionalProperties: false,
2858
+ properties: {
2859
+ prompt: {
2860
+ type: "string",
2861
+ description: "The lead's brief — the artifact under review plus constraints."
2862
+ },
2863
+ context: {
2864
+ type: "string",
2865
+ description: "Optional additional context (extra file content, prior decisions). Concatenated to the brief before sending."
2866
+ },
2867
+ effort: {
2868
+ type: "string",
2869
+ enum: [...EFFORT_LEVELS],
2870
+ description: `Reasoning depth (low | medium | high | xhigh). Default "${DEFAULT_EFFORT}". Use 'xhigh' for explicit deep dives where you want maximum reasoning. Use 'medium' for quick sanity checks. Note: for non-OpenAI models routed via /v1/chat/completions (gemini-3.x), the upstream may silently ignore this knob.`
2871
+ }
2872
+ }
2873
+ }
2874
+ }));
2875
+ }
2876
+ function buildUserText(prompt, context) {
2877
+ if (!context) return prompt;
2878
+ return `${prompt}\n\n---\n\nAdditional context:\n${context}`;
2879
+ }
2880
+ function extractResponsesText(response) {
2881
+ const out = [];
2882
+ for (const item of response.output) {
2883
+ if (typeof item !== "object" || item === null) continue;
2884
+ const obj = item;
2885
+ if (obj.type !== "message" || obj.role !== "assistant") continue;
2886
+ const content = obj.content;
2887
+ if (!Array.isArray(content)) continue;
2888
+ for (const part of content) {
2889
+ if (typeof part !== "object" || part === null) continue;
2890
+ const p = part;
2891
+ if ((p.type === "output_text" || p.type === "text") && typeof p.text === "string") out.push(p.text);
2892
+ }
2893
+ }
2894
+ return out.join("");
2895
+ }
2896
+ function extractChatCompletionText(response) {
2897
+ const choice = response.choices?.[0];
2898
+ if (!choice) return "";
2899
+ const c = choice.message?.content;
2900
+ return typeof c === "string" ? c : "";
2901
+ }
2902
+ function toolError(message) {
2903
+ return {
2904
+ content: [{
2905
+ type: "text",
2906
+ text: message
2907
+ }],
2908
+ isError: true
2909
+ };
2910
+ }
2911
+ async function callPersona(persona, prompt, context, effort) {
2912
+ const resolvedModel = resolveModel(persona.model);
2913
+ const userText = buildUserText(prompt, context);
2914
+ if (persona.endpoint === "/v1/responses") {
2915
+ const text$1 = extractResponsesText(await createResponses({
2916
+ model: resolvedModel,
2917
+ instructions: persona.baseInstructions,
2918
+ input: [{
2919
+ role: "user",
2920
+ content: [{
2921
+ type: "input_text",
2922
+ text: userText
2923
+ }]
2924
+ }],
2925
+ stream: false,
2926
+ reasoning: { effort }
2927
+ }));
2928
+ if (!text$1) return toolError(`persona ${persona.agentName}: empty assistant output`);
2929
+ return { content: [{
2930
+ type: "text",
2931
+ text: text$1
2932
+ }] };
2933
+ }
2934
+ const text = extractChatCompletionText(await createChatCompletions({
2935
+ model: resolvedModel,
2936
+ messages: [{
2937
+ role: "system",
2938
+ content: persona.baseInstructions
2939
+ }, {
2940
+ role: "user",
2941
+ content: userText
2942
+ }],
2943
+ stream: false,
2944
+ reasoning_effort: effort
2945
+ }));
2946
+ if (!text) return toolError(`persona ${persona.agentName}: empty assistant output`);
2947
+ return { content: [{
2948
+ type: "text",
2949
+ text
2950
+ }] };
2951
+ }
2952
+ function logTelemetry(t) {
2953
+ const parts = [
2954
+ `[peer-mcp]`,
2955
+ `name=${t.name}`,
2956
+ `model=${t.model}`,
2957
+ `duration_ms=${t.durationMs}`,
2958
+ `result=${t.result}`
2959
+ ];
2960
+ if (t.errorMessage) parts.push(`error=${JSON.stringify(t.errorMessage)}`);
2961
+ process.stderr.write(parts.join(" ") + "\n");
2962
+ }
2963
+ async function handleToolsCall(body) {
2964
+ const params = body.params ?? {};
2965
+ const name$1 = typeof params.name === "string" ? params.name : "";
2966
+ const args = params.arguments ?? {};
2967
+ const prompt = typeof args.prompt === "string" ? args.prompt : "";
2968
+ const context = typeof args.context === "string" ? args.context : void 0;
2969
+ let effort = DEFAULT_EFFORT;
2970
+ if (args.effort !== void 0) {
2971
+ if (!isEffort(args.effort)) return rpcError(body.id, RPC_INVALID_PARAMS, `tools/call: arguments.effort must be one of ${EFFORT_LEVELS.join("|")}; got ${JSON.stringify(args.effort)}`);
2972
+ effort = args.effort;
2973
+ }
2974
+ if (!name$1) return rpcError(body.id, RPC_INVALID_PARAMS, "tools/call missing name");
2975
+ const persona = activePersonas().find((p) => p.toolNameHttp === name$1);
2976
+ if (!persona) return rpcError(body.id, RPC_METHOD_NOT_FOUND, `tools/call: unknown tool "${name$1}"`);
2977
+ if (!prompt) return rpcError(body.id, RPC_INVALID_PARAMS, `tools/call: arguments.prompt is required`);
2978
+ if (inFlightToolsCall >= MAX_INFLIGHT_TOOLS_CALL) return rpcResult(body.id, {
2979
+ content: [{
2980
+ type: "text",
2981
+ text: `Peer MCP queue full (${MAX_INFLIGHT_TOOLS_CALL} in-flight). Retry shortly, or wait for the current persona calls to complete.`
2982
+ }],
2983
+ isError: true
2984
+ });
2985
+ inFlightToolsCall++;
2986
+ const startedAt = Date.now();
2987
+ try {
2988
+ const result = await callPersona(persona, prompt, context, effort);
2989
+ logTelemetry({
2990
+ name: persona.agentName,
2991
+ model: persona.model,
2992
+ durationMs: Date.now() - startedAt,
2993
+ result: result.isError ? "isError" : "ok"
2994
+ });
2995
+ return rpcResult(body.id, result);
2996
+ } catch (err) {
2997
+ const message = err instanceof Error ? err.message : String(err);
2998
+ logTelemetry({
2999
+ name: persona.agentName,
3000
+ model: persona.model,
3001
+ durationMs: Date.now() - startedAt,
3002
+ result: "exception",
3003
+ errorMessage: message
3004
+ });
3005
+ return rpcResult(body.id, {
3006
+ content: [{
3007
+ type: "text",
3008
+ text: `persona ${persona.agentName} failed: ${message}`
3009
+ }],
3010
+ isError: true
3011
+ });
3012
+ } finally {
3013
+ inFlightToolsCall--;
3014
+ }
3015
+ }
3016
+ async function handleRpc(_c, body) {
3017
+ if (body === null || typeof body !== "object" || Array.isArray(body)) return {
3018
+ status: 200,
3019
+ body: rpcError(null, RPC_INVALID_REQUEST, "jsonrpc 2.0 envelope required")
3020
+ };
3021
+ if (body.jsonrpc !== "2.0" || typeof body.method !== "string") return {
3022
+ status: 200,
3023
+ body: rpcError(body.id ?? null, RPC_INVALID_REQUEST, "jsonrpc 2.0 envelope required")
3024
+ };
3025
+ const isNotification = body.id === void 0;
3026
+ switch (body.method) {
3027
+ case "initialize":
3028
+ if (isNotification) return {
3029
+ status: 202,
3030
+ body: null
3031
+ };
3032
+ return {
3033
+ status: 200,
3034
+ body: rpcResult(body.id, {
3035
+ protocolVersion: MCP_PROTOCOL_VERSION,
3036
+ capabilities: { tools: { listChanged: false } },
3037
+ serverInfo: {
3038
+ name: SERVER_NAME,
3039
+ version: SERVER_VERSION
3040
+ }
3041
+ })
3042
+ };
3043
+ case "notifications/initialized": return {
3044
+ status: 202,
3045
+ body: null
3046
+ };
3047
+ case "tools/list":
3048
+ if (isNotification) return {
3049
+ status: 202,
3050
+ body: null
3051
+ };
3052
+ return {
3053
+ status: 200,
3054
+ body: rpcResult(body.id, { tools: toolEntries() })
3055
+ };
3056
+ case "tools/call":
3057
+ if (isNotification) return {
3058
+ status: 202,
3059
+ body: null
3060
+ };
3061
+ return {
3062
+ status: 200,
3063
+ body: await handleToolsCall(body)
3064
+ };
3065
+ case "ping":
3066
+ if (isNotification) return {
3067
+ status: 202,
3068
+ body: null
3069
+ };
3070
+ return {
3071
+ status: 200,
3072
+ body: rpcResult(body.id, {})
3073
+ };
3074
+ default:
3075
+ if (isNotification) return {
3076
+ status: 202,
3077
+ body: null
3078
+ };
3079
+ return {
3080
+ status: 200,
3081
+ body: rpcError(body.id, RPC_METHOD_NOT_FOUND, `unknown method: ${body.method}`)
3082
+ };
3083
+ }
3084
+ }
3085
+ async function handleMcpPost(c) {
3086
+ const auth$1 = checkAuth(c);
3087
+ if (!auth$1.ok) return c.json(rpcError(null, RPC_INVALID_REQUEST, auth$1.reason), auth$1.status);
3088
+ let body;
3089
+ try {
3090
+ body = await c.req.json();
3091
+ } catch (err) {
3092
+ consola.debug("/mcp parse error:", err);
3093
+ return c.json(rpcError(null, RPC_PARSE_ERROR, "request body is not valid JSON"), 200);
3094
+ }
3095
+ try {
3096
+ const { status, body: respBody } = await handleRpc(c, body);
3097
+ if (respBody === null) return c.body(null, status);
3098
+ return c.json(respBody, status);
3099
+ } catch (err) {
3100
+ consola.error("/mcp handler error:", err);
3101
+ const echoId = typeof body === "object" && body !== null && !Array.isArray(body) ? body.id ?? null : null;
3102
+ return c.json(rpcError(echoId, RPC_INTERNAL_ERROR, err instanceof Error ? err.message : String(err)), 200);
3103
+ }
3104
+ }
3105
+ function handleMcpDelete(c) {
3106
+ const auth$1 = checkAuth(c);
3107
+ if (!auth$1.ok) return c.json(rpcError(null, RPC_INVALID_REQUEST, auth$1.reason), auth$1.status);
3108
+ return c.body(null, 200);
3109
+ }
3110
+
3111
+ //#endregion
3112
+ //#region src/routes/mcp/route.ts
3113
+ const mcpRoutes = new Hono();
3114
+ mcpRoutes.post("/", async (c) => {
3115
+ try {
3116
+ return await handleMcpPost(c);
3117
+ } catch (error) {
3118
+ return await forwardError(c, error);
3119
+ }
3120
+ });
3121
+ mcpRoutes.delete("/", (c) => {
3122
+ try {
3123
+ return handleMcpDelete(c);
3124
+ } catch {
3125
+ return c.body(null, 500);
3126
+ }
3127
+ });
3128
+
1640
3129
  //#endregion
1641
3130
  //#region src/services/copilot/create-messages.ts
1642
3131
  /**
@@ -1676,14 +3165,18 @@ function buildHeaders(extraHeaders) {
1676
3165
  */
1677
3166
  async function createMessages(body, extraHeaders) {
1678
3167
  if (!state.copilotToken) throw new Error("Copilot token not found");
1679
- const headers = buildHeaders(extraHeaders);
1680
3168
  const url = `${copilotBaseUrl(state)}/v1/messages?beta=true`;
1681
3169
  consola.debug(`Forwarding to ${url}`);
1682
- const response = await fetch(url, {
1683
- method: "POST",
1684
- headers,
1685
- body
1686
- });
3170
+ const doFetch = () => {
3171
+ const fetchInit = {
3172
+ method: "POST",
3173
+ headers: buildHeaders(extraHeaders),
3174
+ body
3175
+ };
3176
+ if (UPSTREAM_FETCH_TIMEOUT_MS > 0) fetchInit.signal = AbortSignal.timeout(UPSTREAM_FETCH_TIMEOUT_MS);
3177
+ return fetch(url, fetchInit);
3178
+ };
3179
+ const response = await tryRefreshAndRetry(doFetch, "/v1/messages");
1687
3180
  if (!response.ok) {
1688
3181
  let errorBody = "";
1689
3182
  try {
@@ -1706,14 +3199,18 @@ async function createMessages(body, extraHeaders) {
1706
3199
  */
1707
3200
  async function countTokens(body, extraHeaders) {
1708
3201
  if (!state.copilotToken) throw new Error("Copilot token not found");
1709
- const headers = buildHeaders(extraHeaders);
1710
3202
  const url = `${copilotBaseUrl(state)}/v1/messages/count_tokens?beta=true`;
1711
3203
  consola.debug(`Forwarding to ${url}`);
1712
- const response = await fetch(url, {
1713
- method: "POST",
1714
- headers,
1715
- body
1716
- });
3204
+ const doFetch = () => {
3205
+ const fetchInit = {
3206
+ method: "POST",
3207
+ headers: buildHeaders(extraHeaders),
3208
+ body
3209
+ };
3210
+ if (UPSTREAM_FETCH_TIMEOUT_MS > 0) fetchInit.signal = AbortSignal.timeout(UPSTREAM_FETCH_TIMEOUT_MS);
3211
+ return fetch(url, fetchInit);
3212
+ };
3213
+ const response = await tryRefreshAndRetry(doFetch, "/v1/messages/count_tokens");
1717
3214
  if (!response.ok) {
1718
3215
  let errorBody = "";
1719
3216
  try {
@@ -1731,6 +3228,22 @@ async function countTokens(body, extraHeaders) {
1731
3228
  return response;
1732
3229
  }
1733
3230
 
3231
+ //#endregion
3232
+ //#region src/lib/diagnose-response.ts
3233
+ const PREVIEW_LIMIT = 200;
3234
+ async function parseJsonOrDiagnose(response, routePath) {
3235
+ const cloned = response.clone();
3236
+ try {
3237
+ return await response.json();
3238
+ } catch (error) {
3239
+ const contentType = response.headers.get("content-type") ?? "(none)";
3240
+ const bodyText = await cloned.text().catch(() => "(unreadable)");
3241
+ const preview = bodyText.length > PREVIEW_LIMIT ? bodyText.slice(0, PREVIEW_LIMIT) + "...(truncated)" : bodyText;
3242
+ consola.error(`Upstream JSON parse failed at ${routePath}: status=${response.status} content-type="${contentType}" body[0..${PREVIEW_LIMIT}]=${JSON.stringify(preview)}`);
3243
+ throw error;
3244
+ }
3245
+ }
3246
+
1734
3247
  //#endregion
1735
3248
  //#region src/routes/messages/count-tokens-handler.ts
1736
3249
  const isWebSearchTool$1 = (tool) => typeof tool.type === "string" && tool.type.startsWith("web_search") || tool.name === "web_search";
@@ -1778,7 +3291,7 @@ async function handleCountTokens(c) {
1778
3291
  ...selectedModel?.requestHeaders,
1779
3292
  ...extraHeaders
1780
3293
  });
1781
- const responseBody = await response.json();
3294
+ const responseBody = await parseJsonOrDiagnose(response, c.req.path);
1782
3295
  logRequest({
1783
3296
  method: "POST",
1784
3297
  path: c.req.path,
@@ -1969,7 +3482,17 @@ async function handleCompletion(c) {
1969
3482
  }
1970
3483
  throw error;
1971
3484
  }
1972
- if ((response.headers.get("content-type") ?? "").includes("text/event-stream")) {
3485
+ const contentType = response.headers.get("content-type") ?? "";
3486
+ const clientAcceptsSSE = (c.req.header("accept") ?? "").includes("text/event-stream");
3487
+ let isStreaming = contentType.includes("text/event-stream");
3488
+ if (!isStreaming && clientAcceptsSSE) {
3489
+ if (contentType === "" || contentType === "application/octet-stream") {
3490
+ consola.warn(`Upstream /v1/messages returned status=${response.status} content-type=${JSON.stringify(contentType)} but client requested streaming; treating response body as SSE`);
3491
+ isStreaming = true;
3492
+ }
3493
+ }
3494
+ if (debugEnabled) consola.debug(`Upstream /v1/messages: status=${response.status} content-type="${contentType}" isStreaming=${isStreaming}`);
3495
+ if (isStreaming) {
1973
3496
  logRequest({
1974
3497
  method: "POST",
1975
3498
  path: c.req.path,
@@ -1982,18 +3505,19 @@ async function handleCompletion(c) {
1982
3505
  const streamHeaders = {
1983
3506
  "content-type": "text/event-stream",
1984
3507
  "cache-control": "no-cache",
3508
+ "transfer-encoding": "chunked",
1985
3509
  connection: "keep-alive"
1986
3510
  };
1987
3511
  const requestId = response.headers.get("x-request-id");
1988
3512
  if (requestId) streamHeaders["x-request-id"] = requestId;
1989
3513
  const reqId = response.headers.get("request-id");
1990
3514
  if (reqId) streamHeaders["request-id"] = reqId;
1991
- return new Response(response.body, {
3515
+ return new Response(response.body ? relayAnthropicStream(response.body, { routePath: c.req.path }) : null, {
1992
3516
  status: response.status,
1993
3517
  headers: streamHeaders
1994
3518
  });
1995
3519
  }
1996
- const responseBody = await response.json();
3520
+ const responseBody = await parseJsonOrDiagnose(response, c.req.path);
1997
3521
  logRequest({
1998
3522
  method: "POST",
1999
3523
  path: c.req.path,
@@ -2200,49 +3724,16 @@ modelRoutes.get("/", async (c) => {
2200
3724
  }
2201
3725
  });
2202
3726
 
2203
- //#endregion
2204
- //#region src/services/copilot/create-responses.ts
2205
- const createResponses = async (payload, modelHeaders) => {
2206
- if (!state.copilotToken) throw new Error("Copilot token not found");
2207
- const enableVision = detectVision(payload.input);
2208
- const isAgentCall = detectAgentCall(payload.input);
2209
- const headers = {
2210
- ...copilotHeaders(state, enableVision),
2211
- ...modelHeaders,
2212
- "X-Initiator": isAgentCall ? "agent" : "user"
2213
- };
2214
- const response = await fetch(`${copilotBaseUrl(state)}/responses`, {
2215
- method: "POST",
2216
- headers,
2217
- body: JSON.stringify(payload)
2218
- });
2219
- if (!response.ok) {
2220
- consola.error("Failed to create responses", response);
2221
- throw new HTTPError("Failed to create responses", response);
2222
- }
2223
- if (payload.stream) return events(response);
2224
- return await response.json();
2225
- };
2226
- function detectVision(input) {
2227
- if (typeof input === "string") return false;
2228
- if (!Array.isArray(input)) return false;
2229
- return input.some((item) => {
2230
- if ("content" in item && Array.isArray(item.content)) return item.content.some((part) => part.type === "input_image");
2231
- return false;
2232
- });
2233
- }
2234
- function detectAgentCall(input) {
2235
- if (typeof input === "string") return false;
2236
- if (!Array.isArray(input)) return false;
2237
- return input.some((item) => {
2238
- if ("role" in item && item.role === "assistant") return true;
2239
- if ("type" in item && (item.type === "function_call" || item.type === "function_call_output")) return true;
2240
- return false;
2241
- });
2242
- }
2243
-
2244
3727
  //#endregion
2245
3728
  //#region src/routes/responses/handler.ts
3729
+ const ENCODER = new TextEncoder();
3730
+ function formatSSE(chunk) {
3731
+ const parts = [];
3732
+ if (chunk.event) parts.push(`event: ${chunk.event}`);
3733
+ if (chunk.data !== void 0) for (const line of String(chunk.data).split(/\r\n|\r|\n/)) parts.push(`data: ${line}`);
3734
+ if (chunk.id !== void 0) parts.push(`id: ${String(chunk.id)}`);
3735
+ return parts.join("\n") + "\n\n";
3736
+ }
2246
3737
  async function handleResponses(c) {
2247
3738
  const startTime = Date.now();
2248
3739
  await checkRateLimit(state);
@@ -2283,16 +3774,106 @@ async function handleResponses(c) {
2283
3774
  if (debugEnabled) consola.debug("Non-streaming response:", JSON.stringify(response));
2284
3775
  return c.json(response);
2285
3776
  }
2286
- return streamSSE(c, async (stream) => {
2287
- for await (const chunk of response) {
2288
- if (debugEnabled) consola.debug("Streaming chunk:", JSON.stringify(chunk));
2289
- if (chunk.data === "[DONE]") break;
2290
- if (!chunk.data) continue;
2291
- await stream.writeSSE({
2292
- data: chunk.data,
2293
- event: chunk.event,
2294
- id: chunk.id?.toString()
2295
- });
3777
+ const iterator = response[Symbol.asyncIterator]();
3778
+ let firstChunk;
3779
+ let upstreamFinished = false;
3780
+ while (true) {
3781
+ const r = await iterator.next();
3782
+ if (r.done) {
3783
+ upstreamFinished = true;
3784
+ break;
3785
+ }
3786
+ if (r.value === void 0 || r.value === null) continue;
3787
+ if (r.value.data === "[DONE]") {
3788
+ upstreamFinished = true;
3789
+ break;
3790
+ }
3791
+ if (!r.value.data) continue;
3792
+ firstChunk = r.value;
3793
+ break;
3794
+ }
3795
+ if (firstChunk === void 0) consola.warn(`Upstream /responses returned no payload events at ${c.req.path}`);
3796
+ let pendingFirstChunk = firstChunk;
3797
+ let consumerCancelled = false;
3798
+ const safeClose = (controller) => {
3799
+ try {
3800
+ controller.close();
3801
+ } catch {}
3802
+ };
3803
+ const releaseUpstream = (reason) => {
3804
+ if (typeof iterator.return === "function") iterator.return(reason).catch(() => {});
3805
+ };
3806
+ const safeEnqueue = (controller, bytes) => {
3807
+ try {
3808
+ controller.enqueue(bytes);
3809
+ return true;
3810
+ } catch (e) {
3811
+ if (isControllerClosedError(e)) {
3812
+ consumerCancelled = true;
3813
+ releaseUpstream(e);
3814
+ return false;
3815
+ }
3816
+ throw e;
3817
+ }
3818
+ };
3819
+ return new Response(new ReadableStream({
3820
+ async pull(controller) {
3821
+ if (consumerCancelled || upstreamFinished) {
3822
+ safeClose(controller);
3823
+ return;
3824
+ }
3825
+ if (pendingFirstChunk !== void 0) {
3826
+ const chunk = pendingFirstChunk;
3827
+ pendingFirstChunk = void 0;
3828
+ if (debugEnabled) consola.debug("Streaming chunk:", JSON.stringify(chunk));
3829
+ safeEnqueue(controller, ENCODER.encode(formatSSE(chunk)));
3830
+ return;
3831
+ }
3832
+ try {
3833
+ const result = await iterator.next();
3834
+ if (consumerCancelled) {
3835
+ safeClose(controller);
3836
+ return;
3837
+ }
3838
+ if (result.done) {
3839
+ upstreamFinished = true;
3840
+ safeClose(controller);
3841
+ return;
3842
+ }
3843
+ if (result.value === void 0 || result.value === null) return;
3844
+ if (result.value.data === "[DONE]") {
3845
+ upstreamFinished = true;
3846
+ safeClose(controller);
3847
+ return;
3848
+ }
3849
+ if (!result.value.data) return;
3850
+ if (debugEnabled) consola.debug("Streaming chunk:", JSON.stringify(result.value));
3851
+ safeEnqueue(controller, ENCODER.encode(formatSSE(result.value)));
3852
+ } catch (error) {
3853
+ upstreamFinished = true;
3854
+ if (consumerCancelled) {
3855
+ releaseUpstream(error);
3856
+ safeClose(controller);
3857
+ return;
3858
+ }
3859
+ const { errName, errMessage } = logStreamError(c.req.path, error);
3860
+ safeEnqueue(controller, ENCODER.encode(buildOpenAIErrorEvent(errName, errMessage)));
3861
+ releaseUpstream(error);
3862
+ safeClose(controller);
3863
+ }
3864
+ },
3865
+ cancel() {
3866
+ consumerCancelled = true;
3867
+ upstreamFinished = true;
3868
+ releaseUpstream();
3869
+ }
3870
+ }), {
3871
+ status: 200,
3872
+ headers: {
3873
+ "content-type": "text/event-stream",
3874
+ "cache-control": "no-cache",
3875
+ "transfer-encoding": "chunked",
3876
+ connection: "keep-alive"
2296
3877
  }
2297
3878
  });
2298
3879
  }
@@ -2494,6 +4075,11 @@ usageRoute.get("/", async (c) => {
2494
4075
  const server = new Hono();
2495
4076
  server.use(cors());
2496
4077
  server.get("/", (c) => c.text("Server running"));
4078
+ server.get("/version", (c) => c.json({
4079
+ name,
4080
+ version,
4081
+ gitSha: process.env.GITHUB_SHA ?? "unknown"
4082
+ }));
2497
4083
  server.on("HEAD", ["/"], (c) => c.body(null, 200));
2498
4084
  server.route("/chat/completions", completionRoutes);
2499
4085
  server.route("/responses", responsesRoutes);
@@ -2508,6 +4094,7 @@ server.route("/v1/models", modelRoutes);
2508
4094
  server.route("/v1/embeddings", embeddingRoutes);
2509
4095
  server.route("/v1/search", searchRoutes);
2510
4096
  server.route("/v1/messages", messageRoutes);
4097
+ server.route("/mcp", mcpRoutes);
2511
4098
  server.post("/api/event_logging/batch", (c) => c.body(null, 200));
2512
4099
  server.notFound((c) => c.json({
2513
4100
  type: "error",
@@ -2717,6 +4304,7 @@ function getClaudeCodeEnvVars(serverUrl, model) {
2717
4304
  ANTHROPIC_BASE_URL: serverUrl,
2718
4305
  ANTHROPIC_AUTH_TOKEN: "dummy",
2719
4306
  CLAUDE_CONFIG_DIR: path.join(os.homedir(), ".claude"),
4307
+ MCP_TIMEOUT: "600000",
2720
4308
  DISABLE_NON_ESSENTIAL_MODEL_CALLS: "1",
2721
4309
  CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1"
2722
4310
  };
@@ -2756,6 +4344,21 @@ const claude = defineCommand({
2756
4344
  alias: "m",
2757
4345
  type: "string",
2758
4346
  description: "Override the default model for Claude Code"
4347
+ },
4348
+ "codex-mcp": {
4349
+ type: "boolean",
4350
+ default: true,
4351
+ description: "Wire peer-model MCP personas (codex-critic, codex-reviewer, gemini-critic) into the spawned Claude Code session"
4352
+ },
4353
+ "codex-cli": {
4354
+ type: "boolean",
4355
+ default: false,
4356
+ description: "Add a `codex mcp-server` stdio backend so codex-implementer can mutate files. Requires codex CLI 0.129+; gracefully falls back to HTTP-only if absent."
4357
+ },
4358
+ "codex-mcp-only": {
4359
+ type: "boolean",
4360
+ default: false,
4361
+ description: "Pass --strict-mcp-config to claude code so only github-router's MCP servers are loaded (hides user's existing MCP servers)"
2759
4362
  }
2760
4363
  },
2761
4364
  async run({ args }) {
@@ -2800,12 +4403,36 @@ const claude = defineCommand({
2800
4403
  }
2801
4404
  const banner = chosenSlug === resolvedSlug ? chosenSlug : `${chosenSlug} → ${resolvedSlug}`;
2802
4405
  process$1.stderr.write(`Server ready on ${serverUrl}, launching Claude Code (${banner})...\n`);
4406
+ const envVars = getClaudeCodeEnvVars(serverUrl, chosenSlug);
4407
+ const extraArgs = args._ ?? [];
4408
+ let onShutdown;
4409
+ if (args["codex-mcp"] !== false) try {
4410
+ const requestedCli = args["codex-cli"] ?? false;
4411
+ const backend = resolveCodexCliBackend({
4412
+ requested: requestedCli,
4413
+ codexInfo: requestedCli ? getCodexVersion() : null
4414
+ });
4415
+ const geminiAvailable$1 = state.models?.data.some((m) => /^gemini-3\..*pro/i.test(m.id)) ?? false;
4416
+ if (!geminiAvailable$1) consola.info("gemini-3.1-pro-preview not found in your Copilot model catalog; gemini-critic persona will not be registered.");
4417
+ const runtime = await writePeerMcpRuntimeFiles(serverUrl, {
4418
+ codexCli: backend === "cli",
4419
+ geminiAvailable: geminiAvailable$1
4420
+ });
4421
+ state.peerMcpNonce = runtime.nonce;
4422
+ onShutdown = runtime.cleanup;
4423
+ extraArgs.push("--mcp-config", runtime.mcpConfigPath);
4424
+ if (args["codex-mcp-only"] === true) extraArgs.push("--strict-mcp-config");
4425
+ const personaNames = runtime.personas.map((p) => p.agentName).join(", ");
4426
+ process$1.stderr.write(`Peer MCP wired (backend=${backend}, personas=[${personaNames}], subagent .md files=${runtime.agentMdPaths.length}).\n`);
4427
+ } catch (err) {
4428
+ consola.warn(`Peer MCP wiring failed (claude will launch without it): ${err instanceof Error ? err.message : String(err)}`);
4429
+ }
2803
4430
  launchChild({
2804
4431
  kind: "claude-code",
2805
- envVars: getClaudeCodeEnvVars(serverUrl, chosenSlug),
2806
- extraArgs: args._ ?? [],
4432
+ envVars,
4433
+ extraArgs,
2807
4434
  model: chosenSlug
2808
- }, server$1);
4435
+ }, server$1, { onShutdown });
2809
4436
  }
2810
4437
  });
2811
4438
 
@@ -2873,7 +4500,8 @@ const codex = defineCommand({
2873
4500
  kind: "codex",
2874
4501
  envVars: getCodexEnvVars(serverUrl),
2875
4502
  extraArgs: args._ ?? [],
2876
- model: codexModel
4503
+ model: codexModel,
4504
+ serverUrl
2877
4505
  }, server$1);
2878
4506
  }
2879
4507
  });
@@ -2906,9 +4534,9 @@ async function checkTokenExists() {
2906
4534
  }
2907
4535
  }
2908
4536
  async function getDebugInfo() {
2909
- const [version, tokenExists] = await Promise.all([getPackageVersion(), checkTokenExists()]);
4537
+ const [version$1, tokenExists] = await Promise.all([getPackageVersion(), checkTokenExists()]);
2910
4538
  return {
2911
- version,
4539
+ version: version$1,
2912
4540
  runtime: getRuntimeInfo(),
2913
4541
  paths: {
2914
4542
  APP_DIR: PATHS.APP_DIR,