github-router 0.3.276 → 0.3.285

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/dist/{attribution-settings-VOPvSn6v.js → attribution-settings-B8M2fvhz.js} +44 -45
  2. package/dist/attribution-settings-B8M2fvhz.js.map +1 -0
  3. package/dist/{auth-CYoRwhC9.js → auth-VUL2Zxvw.js} +3 -3
  4. package/dist/{auth-CYoRwhC9.js.map → auth-VUL2Zxvw.js.map} +1 -1
  5. package/dist/browser-ext/manifest.json +1 -1
  6. package/dist/{check-usage-DDUyg8ZB.js → check-usage-CcLdFPGr.js} +4 -4
  7. package/dist/{check-usage-DDUyg8ZB.js.map → check-usage-CcLdFPGr.js.map} +1 -1
  8. package/dist/{claude-OTHh7iKz.js → claude-e-JIQGhR.js} +55 -38
  9. package/dist/claude-e-JIQGhR.js.map +1 -0
  10. package/dist/{codex-tCxK30Su.js → codex-DSnVZ9oK.js} +5 -5
  11. package/dist/{codex-tCxK30Su.js.map → codex-DSnVZ9oK.js.map} +1 -1
  12. package/dist/{debug-CJgsxoVw.js → debug-CtzYWxpJ.js} +2 -2
  13. package/dist/{debug-CJgsxoVw.js.map → debug-CtzYWxpJ.js.map} +1 -1
  14. package/dist/engine-DMveEa9x.js +2 -0
  15. package/dist/{gate-discovery-nf7PGgnS.js → gate-discovery-BCFwLm0q.js} +5 -5
  16. package/dist/{gate-discovery-nf7PGgnS.js.map → gate-discovery-BCFwLm0q.js.map} +1 -1
  17. package/dist/{get-copilot-usage-D1OAH0EU.js → get-copilot-usage-B-EDAqQb.js} +2 -2
  18. package/dist/{get-copilot-usage-D1OAH0EU.js.map → get-copilot-usage-B-EDAqQb.js.map} +1 -1
  19. package/dist/hooks.mjs +37870 -0
  20. package/dist/hooks.sha256 +1 -0
  21. package/dist/{internal-artifact-open-BCz9lnq0.js → internal-artifact-open-Dj5Nlq0L.js} +2 -2
  22. package/dist/{internal-artifact-open-BCz9lnq0.js.map → internal-artifact-open-Dj5Nlq0L.js.map} +1 -1
  23. package/dist/{internal-first-mate-guard-7v2CtMA6.js → internal-first-mate-guard-B7p4NttK.js} +1 -1
  24. package/dist/{internal-first-mate-guard-La0tIjTU.js → internal-first-mate-guard-DOgFVki5.js} +5 -4
  25. package/dist/{internal-first-mate-guard-La0tIjTU.js.map → internal-first-mate-guard-DOgFVki5.js.map} +1 -1
  26. package/dist/{internal-plan-review-jRMpHu68.js → internal-plan-review-DRFHET_B.js} +11 -4
  27. package/dist/internal-plan-review-DRFHET_B.js.map +1 -0
  28. package/dist/{internal-prompt-submit-CqWRFCuv.js → internal-prompt-submit-f1LR2P2G.js} +4 -4
  29. package/dist/{internal-prompt-submit-CqWRFCuv.js.map → internal-prompt-submit-f1LR2P2G.js.map} +1 -1
  30. package/dist/{internal-session-bind-BiHWDxuC.js → internal-session-bind-DhZhxJ3T.js} +2 -2
  31. package/dist/{internal-session-bind-BiHWDxuC.js.map → internal-session-bind-DhZhxJ3T.js.map} +1 -1
  32. package/dist/{internal-stop-hook-Ds9LwmZA.js → internal-stop-hook-Cf-7w3RH.js} +68 -20
  33. package/dist/internal-stop-hook-Cf-7w3RH.js.map +1 -0
  34. package/dist/{internal-stop-review-BOFj7R3q.js → internal-stop-review-BBsLcbPG.js} +2 -2
  35. package/dist/{internal-stop-review-BOFj7R3q.js.map → internal-stop-review-BBsLcbPG.js.map} +1 -1
  36. package/dist/{internal-worker-guard-Lu8VHj5k.js → internal-worker-guard-CKgYFiFO.js} +2 -2
  37. package/dist/{internal-worker-guard-Lu8VHj5k.js.map → internal-worker-guard-CKgYFiFO.js.map} +1 -1
  38. package/dist/{internal-workspace-header-BRMz0Yql.js → internal-workspace-header-OYgHEnFt.js} +2 -2
  39. package/dist/{internal-workspace-header-BRMz0Yql.js.map → internal-workspace-header-OYgHEnFt.js.map} +1 -1
  40. package/dist/lib/tree-sitter-pool/lifecycle-C7W8D2lx.js +165 -0
  41. package/dist/lib/tree-sitter-pool/lifecycle-Dx0SnP7Z.js +157 -0
  42. package/dist/lib/tree-sitter-pool/worker.js +287 -14
  43. package/dist/{lifecycle-LA4rAuAL.js → lifecycle-B7CHqKlF.js} +2 -2
  44. package/dist/{lifecycle-LA4rAuAL.js.map → lifecycle-B7CHqKlF.js.map} +1 -1
  45. package/dist/lifecycle-CbmHMMSD.js +2 -0
  46. package/dist/{lifecycle-AGXnd-ZY.js → lifecycle-D-rL81tT.js} +2 -2
  47. package/dist/{lifecycle-AGXnd-ZY.js.map → lifecycle-D-rL81tT.js.map} +1 -1
  48. package/dist/lifecycle-DTcZwa7U.js +2 -0
  49. package/dist/lifecycle-K9oVtGde.mjs +16 -0
  50. package/dist/main.js +18 -18
  51. package/dist/{mcp-workspace-header-CGJbNeHb.js → mcp-workspace-header-ucs2SDST.js} +5 -6
  52. package/dist/mcp-workspace-header-ucs2SDST.js.map +1 -0
  53. package/dist/{models-4Q45Q2Dw.js → models-C16mBK2M.js} +3 -3
  54. package/dist/{models-4Q45Q2Dw.js.map → models-C16mBK2M.js.map} +1 -1
  55. package/dist/{orchestration-CpkG8ScN.js → orchestration-CtM6FYNx.js} +2 -2
  56. package/dist/{orchestration-CpkG8ScN.js.map → orchestration-CtM6FYNx.js.map} +1 -1
  57. package/dist/package-root-B-osctCk.js +29 -0
  58. package/dist/package-root-B-osctCk.js.map +1 -0
  59. package/dist/paths-CV9K7Xqm.js +2 -0
  60. package/dist/{paths-j2B7b0DZ.js → paths-wLC0InjX.js} +28 -4
  61. package/dist/paths-wLC0InjX.js.map +1 -0
  62. package/dist/{peer-mcp-personas-DRL_xT4h.js → peer-mcp-personas-V6stFvpq.js} +1158 -297
  63. package/dist/peer-mcp-personas-V6stFvpq.js.map +1 -0
  64. package/dist/{plan-review-hook-Dk5zTNke.js → plan-review-hook-Lf9ISdF6.js} +5 -6
  65. package/dist/plan-review-hook-Lf9ISdF6.js.map +1 -0
  66. package/dist/{prompt-submit-hook-lvTWwaTV.js → prompt-submit-hook-BlijaOn7.js} +6 -11
  67. package/dist/prompt-submit-hook-BlijaOn7.js.map +1 -0
  68. package/dist/{provision-HzZ547dW.js → provision-BpL6gZIt.js} +4 -4
  69. package/dist/{provision-HzZ547dW.js.map → provision-BpL6gZIt.js.map} +1 -1
  70. package/dist/self-invocation-CKMjcA5F.js +380 -0
  71. package/dist/self-invocation-CKMjcA5F.js.map +1 -0
  72. package/dist/{serve-CM3OmF4I.js → serve-CXUf7RtJ.js} +34 -27
  73. package/dist/serve-CXUf7RtJ.js.map +1 -0
  74. package/dist/{server-setup-C9r7jYnz.js → server-setup-Bppjt9xO.js} +81 -207
  75. package/dist/server-setup-Bppjt9xO.js.map +1 -0
  76. package/dist/{start-KAUCsDao.js → start-C1-jrHfU.js} +3 -3
  77. package/dist/{start-KAUCsDao.js.map → start-C1-jrHfU.js.map} +1 -1
  78. package/dist/{stop-gate-hook-CaUrldLh.js → stop-gate-hook-DgJ6sW8N.js} +100 -52
  79. package/dist/stop-gate-hook-DgJ6sW8N.js.map +1 -0
  80. package/dist/{stop-gate-policy-lAabj_pP.js → stop-gate-policy-DG5yYWGn.js} +2 -2
  81. package/dist/{stop-gate-policy-lAabj_pP.js.map → stop-gate-policy-DG5yYWGn.js.map} +1 -1
  82. package/dist/{token-CxPaBEUS.js → token-CnlB0884.js} +2 -2
  83. package/dist/token-CnlB0884.js.map +1 -0
  84. package/dist/{version-_Q1WpsQp.js → version-C8x2hQrZ.js} +14 -2
  85. package/dist/version-C8x2hQrZ.js.map +1 -0
  86. package/dist/{worker-dispatch-Bj1uYyG9.js → worker-dispatch-zW8Zi69V.js} +11 -7
  87. package/dist/worker-dispatch-zW8Zi69V.js.map +1 -0
  88. package/package.json +2 -2
  89. package/dist/attribution-settings-VOPvSn6v.js.map +0 -1
  90. package/dist/claude-OTHh7iKz.js.map +0 -1
  91. package/dist/engine-BKJKMAzh.js +0 -2
  92. package/dist/internal-plan-review-jRMpHu68.js.map +0 -1
  93. package/dist/internal-stop-hook-Ds9LwmZA.js.map +0 -1
  94. package/dist/lifecycle-DCrKbaIU.js +0 -2
  95. package/dist/lifecycle-KKSTKwd6.js +0 -2
  96. package/dist/mcp-workspace-header-CGJbNeHb.js.map +0 -1
  97. package/dist/paths-DN3Nio42.js +0 -2
  98. package/dist/paths-j2B7b0DZ.js.map +0 -1
  99. package/dist/peer-mcp-personas-DRL_xT4h.js.map +0 -1
  100. package/dist/plan-review-hook-Dk5zTNke.js.map +0 -1
  101. package/dist/prompt-submit-hook-lvTWwaTV.js.map +0 -1
  102. package/dist/serve-CM3OmF4I.js.map +0 -1
  103. package/dist/server-setup-C9r7jYnz.js.map +0 -1
  104. package/dist/stop-gate-hook-CaUrldLh.js.map +0 -1
  105. package/dist/token-CxPaBEUS.js.map +0 -1
  106. package/dist/version-_Q1WpsQp.js.map +0 -1
  107. package/dist/worker-dispatch-Bj1uYyG9.js.map +0 -1
@@ -1 +0,0 @@
1
- {"version":3,"file":"plan-review-hook-Dk5zTNke.js","names":["nodePath","fs"],"sources":["../src/lib/orchestration/plan-review-hook.ts"],"sourcesContent":["import { createHash } from \"node:crypto\"\nimport { promises as fs } from \"node:fs\"\nimport nodePath from \"node:path\"\n\nimport { parseBoolEnv } from \"~/lib/exec\"\n\nimport { callMcpTool, type HookMcpRuntime, type McpToolResult } from \"./hook-mcp-client\"\nimport {\n isSubagentContext,\n type FindingsStore,\n type ReviewDebounce,\n} from \"./stop-gate-policy\"\n\n/** Minimum finalized-plan size worth spending a model review on. */\nexport const PLAN_REVIEW_MIN_CHARS = 220\n\n/** Hard wall-clock for the advisory plan critic. */\nexport const PLAN_REVIEW_TIMEOUT_MS = 25_000\n\n/** Cap the plan embedded in the critic brief so a huge plan file cannot dominate. */\nconst MAX_REVIEWED_PLAN_CHARS = 80 * 1024\n\nexport interface PlanReviewSpawnContext {\n sessionId: string\n cwd: string\n plan: string\n planHash: string\n}\n\nexport type PlanReviewDecision =\n | { kind: \"skip\"; reason: string }\n | ({ kind: \"spawn\" } & PlanReviewSpawnContext)\n\n/** Default-on advisory plan review. Opt out with GH_ROUTER_DISABLE_PLAN_REVIEW=1. */\nexport function planReviewEnabled(env: NodeJS.ProcessEnv = process.env): boolean {\n return parseBoolEnv(env.GH_ROUTER_DISABLE_PLAN_REVIEW) !== true\n}\n\n/** File-backed per-session debounce for finalized plans, separate from Stop diff hashes. */\nexport function filePlanReviewDebounce(stateDir: string): ReviewDebounce {\n const fileFor = (sid: string): string =>\n nodePath.join(stateDir, `plan-review-hash-${createHash(\"sha256\").update(sid).digest(\"hex\").slice(0, 32)}`)\n const readLast = async (sid: string): Promise<string> => {\n try {\n return (await fs.readFile(fileFor(sid), \"utf8\")).trim()\n } catch {\n return \"\"\n }\n }\n return {\n async shouldReview(sid, planHash) {\n if (planHash.length === 0) return false\n return (await readLast(sid)) !== planHash\n },\n async markReviewed(sid, planHash) {\n await fs.mkdir(stateDir, { recursive: true })\n await fs.writeFile(fileFor(sid), planHash, { mode: 0o600 })\n },\n }\n}\n\nfunction parseObject(raw: string): Record<string, unknown> | undefined {\n try {\n const parsed: unknown = JSON.parse(raw)\n if (parsed && typeof parsed === \"object\" && !Array.isArray(parsed)) return parsed as Record<string, unknown>\n } catch {\n /* malformed hook payload -> skip */\n }\n return undefined\n}\n\nasync function resolvePlanText(\n toolInput: unknown,\n readFile: (filePath: string) => Promise<string>,\n): Promise<string> {\n if (!toolInput || typeof toolInput !== \"object\") return \"\"\n const ti = toolInput as { planFilePath?: unknown; plan?: unknown }\n const planFilePath = typeof ti.planFilePath === \"string\" && ti.planFilePath.trim().length > 0\n ? ti.planFilePath.trim()\n : \"\"\n if (planFilePath.length > 0) {\n try {\n const fromFile = await readFile(planFilePath)\n if (fromFile.trim().length > 0) return fromFile\n } catch {\n /* fall through to inline plan */\n }\n }\n return typeof ti.plan === \"string\" ? ti.plan : \"\"\n}\n\n/**\n * Decide whether the ExitPlanMode PostToolUse hook should spawn a detached review.\n * Pure aside from injected file reads + debounce store. Fail-open: any malformed\n * payload, missing runtime, subagent context, trivial/absent plan, or store error\n * returns a skip decision.\n */\nexport async function decidePlanReviewHook(input: {\n stdin: string\n runtimeAvailable: boolean\n debounce: ReviewDebounce\n fallbackCwd: string\n minChars?: number\n readFile?: (filePath: string) => Promise<string>\n}): Promise<PlanReviewDecision> {\n try {\n if (!input.runtimeAvailable) return { kind: \"skip\", reason: \"missing-runtime\" }\n const payload = parseObject(input.stdin)\n if (!payload) return { kind: \"skip\", reason: \"bad-payload\" }\n if (isSubagentContext(payload)) return { kind: \"skip\", reason: \"subagent\" }\n\n const sessionId = typeof payload.session_id === \"string\" && payload.session_id.length > 0 ? payload.session_id : \"\"\n if (!sessionId) return { kind: \"skip\", reason: \"missing-session\" }\n const cwd = typeof payload.cwd === \"string\" && payload.cwd.length > 0 ? payload.cwd : input.fallbackCwd\n const plan = await resolvePlanText(\n payload.tool_input,\n input.readFile ?? ((filePath: string) => fs.readFile(filePath, \"utf8\")),\n )\n const trimmed = plan.trim()\n if (trimmed.length < (input.minChars ?? PLAN_REVIEW_MIN_CHARS)) return { kind: \"skip\", reason: \"trivial-plan\" }\n\n const planHash = createHash(\"sha256\").update(trimmed).digest(\"hex\")\n if (!(await input.debounce.shouldReview(sessionId, planHash))) return { kind: \"skip\", reason: \"debounced\" }\n await input.debounce.markReviewed(sessionId, planHash)\n return { kind: \"spawn\", sessionId, cwd, plan: trimmed, planHash }\n } catch {\n return { kind: \"skip\", reason: \"error\" }\n }\n}\n\nfunction reviewBrief(plan: string, cwd: string): string {\n const embeddedPlan =\n plan.length > MAX_REVIEWED_PLAN_CHARS\n ? `${plan.slice(0, MAX_REVIEWED_PLAN_CHARS)}\\n\\n[plan truncated at ${MAX_REVIEWED_PLAN_CHARS} characters]`\n : plan\n return (\n \"You are an independent plan reviewer. A coding agent has just finalized a plan with ExitPlanMode. \"\n + \"Your job is to find load-bearing risks before implementation starts: unstated assumptions, missing \"\n + \"failure modes, gaps in verification, dangerous sequencing, or places where the plan could satisfy the \"\n + \"letter of the request while missing the user's likely intent.\\n\\n\"\n + \"Be concise and skeptical. Do NOT rewrite the plan, do NOT praise it, and do NOT invent new product \"\n + \"requirements. Report only material objections that would change what the agent should do next. Include \"\n + \"file:line anchors when the plan names concrete files or when the risk depends on repo code. If the plan \"\n + \"is sound, say exactly: \\\"no material objection\\\".\\n\\n\"\n + `Workspace: ${cwd}\\n\\n`\n + \"FINALIZED PLAN:\\n\"\n + embeddedPlan\n )\n}\n\nfunction hasMaterialFinding(text: string): boolean {\n const normalized = text.trim().toLowerCase().replace(/[.!]+$/g, \"\")\n return normalized.length > 0 && normalized !== \"no material objection\" && normalized !== \"no material objections\"\n}\n\nfunction framePlanFindings(text: string): string {\n return (\n \"PLAN REVIEW: an independent cross-lab critic reviewed the finalized plan and found \"\n + \"the following material concern(s).\\n\"\n + text.trim()\n )\n}\n\n/** Run the bounded critic call and write material findings to the shared findings store. */\nexport async function runPlanReview(input: {\n runtime: HookMcpRuntime\n sessionId: string\n cwd: string\n plan: string\n findingsStore: FindingsStore\n timeoutMs?: number\n callReview?: (brief: string, signal: AbortSignal) => Promise<McpToolResult>\n}): Promise<void> {\n const timeoutMs = input.timeoutMs ?? PLAN_REVIEW_TIMEOUT_MS\n let timer: ReturnType<typeof setTimeout> | undefined\n const controller = new AbortController()\n try {\n const brief = reviewBrief(input.plan, input.cwd)\n const review = (input.callReview ?? ((prompt: string, signal: AbortSignal) => callMcpTool({\n runtime: input.runtime,\n group: \"peers\",\n tool: \"codex_critic\",\n args: { prompt, effort: \"high\" },\n timeoutMs,\n signal,\n })))(brief, controller.signal)\n review.catch(() => {})\n const raced = await Promise.race<McpToolResult | \"timeout\">([\n review,\n new Promise<\"timeout\">((resolve) => {\n timer = setTimeout(() => resolve(\"timeout\"), timeoutMs)\n }),\n ])\n if (raced === \"timeout\" || raced.isError || !hasMaterialFinding(raced.text)) return\n\n const framed = framePlanFindings(raced.text)\n const existing = await input.findingsStore.read(input.sessionId).catch(() => null)\n const next = existing && existing.trim().length > 0 ? `${existing.trim()}\\n\\n${framed}` : framed\n await input.findingsStore.write(input.sessionId, next)\n } catch {\n /* advisory layer must never surface an error */\n } finally {\n if (timer) clearTimeout(timer)\n controller.abort()\n }\n}\n\n/** Build the command registered for PostToolUse(ExitPlanMode). */\nexport function buildPlanReviewHookCommand(execPath: string, scriptPath: string | undefined): string {\n const q = (s: string): string => `\"${s}\"`\n if (scriptPath && scriptPath !== execPath) {\n return `${q(execPath)} ${q(scriptPath)} internal-plan-review`\n }\n return `${q(execPath)} internal-plan-review`\n}\n"],"mappings":";;;;;;;AAoBA,MAAM,0BAA0B;;AAchC,SAAgB,kBAAkB,MAAyB,QAAQ,KAAc;CAC/E,OAAO,aAAa,IAAI,6BAA6B,MAAM;AAC7D;;AAGA,SAAgB,uBAAuB,UAAkC;CACvE,MAAM,WAAW,QACfA,KAAS,KAAK,UAAU,oBAAoB,WAAW,QAAQ,CAAC,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,KAAK,CAAC,CAAC,MAAM,GAAG,EAAE,GAAG;CAC3G,MAAM,WAAW,OAAO,QAAiC;EACvD,IAAI;GACF,QAAQ,MAAMC,SAAG,SAAS,QAAQ,GAAG,GAAG,MAAM,EAAA,CAAG,KAAK;EACxD,QAAQ;GACN,OAAO;EACT;CACF;CACA,OAAO;EACL,MAAM,aAAa,KAAK,UAAU;GAChC,IAAI,SAAS,WAAW,GAAG,OAAO;GAClC,OAAQ,MAAM,SAAS,GAAG,MAAO;EACnC;EACA,MAAM,aAAa,KAAK,UAAU;GAChC,MAAMA,SAAG,MAAM,UAAU,EAAE,WAAW,KAAK,CAAC;GAC5C,MAAMA,SAAG,UAAU,QAAQ,GAAG,GAAG,UAAU,EAAE,MAAM,IAAM,CAAC;EAC5D;CACF;AACF;AAEA,SAAS,YAAY,KAAkD;CACrE,IAAI;EACF,MAAM,SAAkB,KAAK,MAAM,GAAG;EACtC,IAAI,UAAU,OAAO,WAAW,YAAY,CAAC,MAAM,QAAQ,MAAM,GAAG,OAAO;CAC7E,QAAQ,C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,IAAI,GAAG;EAE7E,MAAM,SAAS,kBAAkB,MAAM,IAAI;EAC3C,MAAM,WAAW,MAAM,MAAM,cAAc,KAAK,MAAM,SAAS,CAAC,CAAC,YAAY,IAAI;EACjF,MAAM,OAAO,YAAY,SAAS,KAAK,CAAC,CAAC,SAAS,IAAI,GAAG,SAAS,KAAK,EAAE,MAAM,WAAW;EAC1F,MAAM,MAAM,cAAc,MAAM,MAAM,WAAW,IAAI;CACvD,QAAQ,CAER,UAAU;EACR,IAAI,OAAO,aAAa,KAAK;EAC7B,WAAW,MAAM;CACnB;AACF;;AAGA,SAAgB,2BAA2B,UAAkB,YAAwC;CACnG,MAAM,KAAK,MAAsB,IAAI,EAAE;CACvC,IAAI,cAAc,eAAe,UAC/B,OAAO,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,UAAU,EAAE;CAEzC,OAAO,GAAG,EAAE,QAAQ,EAAE;AACxB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"prompt-submit-hook-lvTWwaTV.js","names":[],"sources":["../src/lib/orchestration/prompt-submit-hook.ts"],"sourcesContent":["/**\n * Pure decision logic for the `UserPromptSubmit` hook (the front-end of the\n * floor-raising surface). It serves TWO duties on the one event:\n *\n * 1. Per-prompt budget reset — clears the Stop-gate's block counter for this\n * session so `maxBlocks` is \"per user prompt\", not \"per session lifetime\"\n * (the per-prompt scoping the Stop-gate relies on).\n * 2. Goal steer — for a NON-trivial prompt, inject an advisory GOAL directive\n * (additive context, never blocking) pointing the model at `/gh-research`\n * then `/gh-orchestrate`. It raises the prior; it does not force execution.\n *\n * Top-level only: like the Stop-gate, it stands down in a subagent/teammate\n * context (the payload carries `agent_type`/`agent_id` there) so it never\n * recurses into spawned workers. Fail-open and never blocking: any error or a\n * trivial prompt yields an empty injection.\n */\n\nimport { isSubagentContext } from \"./stop-gate-policy\"\n\n/**\n * The advisory goal injected for a non-trivial prompt. Uses the skills' slash\n * invocation form. The model still decides whether to follow it; the Stop-gate\n * backstops correctness at the output end.\n */\nexport const PROMPT_STEER_GOAL =\n \"GOAL (advisory): for a non-trivial task, FIRST run /gh-research on this ask to \"\n + \"information saturation — verify the load-bearing claims against the actual code \"\n + \"before planning, and do not plan or write code until research is saturated. THEN, \"\n + \"for an implementation or change task, run /gh-orchestrate to compose and run a \"\n + \"floor-raising workflow (it checkpoints before expensive work). Skip both for a \"\n + \"trivial ask; you may decline if they do not fit.\"\n\n/**\n * Cheap, conservative complexity heuristic — a long prompt, an imperative\n * build/change verb, or an explicit multi-file scope. Trivial prompts get no\n * steer (no analysis-paralysis tax on quick asks).\n */\nexport function isNonTrivialPrompt(prompt: string): boolean {\n const p = prompt.trim()\n if (p.length === 0) return false\n if (p.length >= 280) return true\n const verb =\n /\\b(implement|build|refactor|migrate|fix|debug|diagnose|design|add|create|rewrite|optimi[sz]e|integrate|architect|investigate|audit)\\b/i\n if (verb.test(p)) return true\n // Multi-file / cross-cutting scope signal.\n return /\\b(across|throughout|every|all)\\b.*\\b(file|module|test|route|component)s?\\b/i.test(p)\n}\n\nexport interface PromptSubmitDecision {\n /** Session whose Stop-gate block budget to reset (undefined = none). */\n resetSession?: string\n /** Text to print to stdout (added to the model's context). Empty = nothing. */\n inject: string\n}\n\nexport function decidePromptSubmit(input: { stdin: string; steerEnabled: boolean }): PromptSubmitDecision {\n let payload: { session_id?: unknown; prompt?: unknown; agent_type?: unknown; agent_id?: unknown } = {}\n try {\n const p: unknown = JSON.parse(input.stdin)\n if (p && typeof p === \"object\") payload = p as typeof payload\n } catch {\n return { inject: \"\" }\n }\n // Top-level only: a subagent/teammate prompt gets neither reset nor steer.\n if (isSubagentContext(payload)) return { inject: \"\" }\n const decision: PromptSubmitDecision = { inject: \"\" }\n const sessionId = typeof payload.session_id === \"string\" && payload.session_id.length > 0 ? payload.session_id : \"\"\n if (sessionId) decision.resetSession = sessionId\n const prompt = typeof payload.prompt === \"string\" ? payload.prompt : \"\"\n if (input.steerEnabled && isNonTrivialPrompt(prompt)) decision.inject = PROMPT_STEER_GOAL\n return decision\n}\n\n// ─── UserPromptSubmit V2 (advisory: classify + grounded scope/goal) ──────────\n\n/**\n * Static encouragement injected only for a non-trivial prompt (no extra model\n * call, no latency tax): nudge parallel lexical+semantic search before\n * concluding. Mirrors the v1 advisory tone: additive, never blocking.\n */\nexport const PROMPT_SEARCH_TIP =\n \"TIP (advisory): when this task needs code context, search lexical + semantic in \"\n + \"parallel — one `mcp__search__code` call with mode:\\\"lexical\\\" and one with \"\n + \"mode:\\\"semantic\\\", issued in the same turn — before concluding.\"\n\n/** System prompt for the single gpt-5.6-sol scope/goal inference. Steers a SHORT,\n * user-derived (not invented) advisory note grounded in the search results. */\nexport const PROMPT_SCOPE_SYSTEM =\n \"You are a scoping assistant for a coding agent about to act on a user's request. \"\n + \"You are given the user's request and the results of a lexical + semantic code search \"\n + \"over the relevant repository. Produce a SHORT advisory note (<= 120 words), plain text only:\\n\"\n + \"1. SCOPE: one line — is this trivial, focused (one area), or large/cross-cutting — grounded in \"\n + \"what the search surfaced (reference the most relevant file(s) by name).\\n\"\n + \"2. GOAL: restate the user's OWN ask as a single measurable objective, in THEIR terms. Do NOT \"\n + \"invent new requirements or acceptance criteria beyond what they asked.\\n\"\n + \"3. Only if the task is large/cross-cutting, add a final line: \\\"Consider /gh-research first to \"\n + \"saturate understanding, then /gh-orchestrate to compose a floor-raising workflow.\\\" Omit it for a \"\n + \"focused or trivial task.\\n\"\n + \"This is advisory — the agent decides whether to follow it. Be concrete and concise; no preamble.\"\n\n/** Injected IO for V2, all best-effort. Each network call returns its text and\n * never throws to the orchestrator (the orchestrator wraps the enrichment in a\n * timeout + try/catch and falls open to the regex goal).\n *\n * INVARIANT for live callers: each IO call MUST self-timeout (the live wiring\n * passes a per-call timeoutMs to callMcpTool / callInference). The orchestrator's\n * outer `timeoutMs` race only bounds how long it WAITS for a result before\n * failing open — it does NOT abort the in-flight call. A non-self-timing-out IO\n * would leave a socket open until the hook process's terminal `process.exit(0)`,\n * which is the hard backstop but not a substitute for the per-call bound. */\nexport interface PromptSubmitV2IO {\n /** One `mcp__search__code` call; returns the raw result text (\"\" on failure).\n * Receives the orchestrator's AbortSignal so a timed-out enrichment cancels\n * the in-flight request (live callers thread it into the HTTP fetch). */\n searchCode: (query: string, mode: \"lexical\" | \"semantic\", signal?: AbortSignal) => Promise<string>\n /** One gpt-5.6-sol `/v1/responses` inference; returns assistant text (\"\" on failure).\n * Receives the orchestrator's AbortSignal (see `searchCode`). */\n infer: (system: string, user: string, signal?: AbortSignal) => Promise<string>\n /** Pending advisory findings from the prior turn's background review. */\n readFindings: (sessionId: string) => Promise<string | null>\n clearFindings: (sessionId: string) => Promise<void>\n /** Stash this prompt so the next Stop review can judge the diff against it. */\n storePrompt: (sessionId: string, prompt: string) => Promise<void>\n /** Wall-clock budget for the substantive-prompt enrichment (search+infer);\n * on overrun the decision falls open to the regex goal. Default 22s. */\n timeoutMs?: number\n}\n\n/** Max chars of each search-result blob fed into the scope inference. */\nconst SEARCH_CONTEXT_CAP = 6 * 1024\n\n/** Wrap the prior-turn review findings in an explicitly NON-AUTHORITATIVE frame. */\nfunction framePendingFindings(findings: string): string {\n return (\n \"ADVISORY — independent review of your PREVIOUS change (NON-AUTHORITATIVE): an independent \"\n + \"gpt-5.6-sol reviewer flagged the following. Evaluate each on its merits — fix the real ones, and \"\n + \"ignore any wrong one with a one-line reason. You are NOT obligated to act on these.\\n\"\n + findings.trim()\n )\n}\n\nfunction joinSections(sections: Array<string>): string {\n return sections\n .map((s) => s.trim())\n .filter((s) => s.length > 0)\n .join(\"\\n\\n\")\n}\n\n/**\n * V2 decision: budget reset (via resetSession) + a grounded, user-derived scope\n * note + surfaced prior-turn findings. ASYNC and IO-driven, but every IO is\n * best-effort and the substantive enrichment is timeout-bounded with a fail-open\n * to the v1 regex goal — so this never blocks and never wedges the prompt.\n *\n * - subagent/teammate -> empty (top-level only, like v1).\n * - findings -> always surfaced (+ cleared) regardless of triviality.\n * - trivial prompt -> findings only (no search tip, no model call).\n * - substantive prompt -> static search tip + parallel lexical+semantic search -> ONE gpt-5.6-sol call\n * -> grounded scope/goal note. Fail-open to PROMPT_STEER_GOAL.\n * - steerEnabled=false -> findings only (no goal/tip).\n */\nexport async function decidePromptSubmitV2(input: {\n stdin: string\n steerEnabled: boolean\n io: PromptSubmitV2IO\n}): Promise<PromptSubmitDecision> {\n let payload: { session_id?: unknown; prompt?: unknown; agent_type?: unknown; agent_id?: unknown } = {}\n try {\n const p: unknown = JSON.parse(input.stdin)\n if (p && typeof p === \"object\") payload = p as typeof payload\n } catch {\n return { inject: \"\" }\n }\n if (isSubagentContext(payload)) return { inject: \"\" }\n\n const decision: PromptSubmitDecision = { inject: \"\" }\n const sessionId = typeof payload.session_id === \"string\" && payload.session_id.length > 0 ? payload.session_id : \"\"\n if (sessionId) decision.resetSession = sessionId\n const prompt = typeof payload.prompt === \"string\" ? payload.prompt : \"\"\n\n // Stash the prompt for the next Stop review (best-effort, fire-and-await so a\n // slow disk doesn't matter — it's a tiny local write).\n if (sessionId) await input.io.storePrompt(sessionId, prompt).catch(() => {})\n\n // Read + clear any pending findings from the prior turn's background review.\n let findingsBlock = \"\"\n if (sessionId) {\n const pending = await input.io.readFindings(sessionId).catch(() => null)\n if (pending && pending.trim().length > 0) {\n findingsBlock = framePendingFindings(pending)\n await input.io.clearFindings(sessionId).catch(() => {})\n }\n }\n\n // steer disabled -> findings only.\n if (!input.steerEnabled) {\n decision.inject = findingsBlock\n return decision\n }\n\n // Trivial prompt -> no search tip and no model call; surface findings only.\n if (!isNonTrivialPrompt(prompt)) {\n decision.inject = findingsBlock\n return decision\n }\n\n // Substantive prompt -> static search tip + grounded enrichment, timeout-bounded + fail-open.\n const timeoutMs = input.io.timeoutMs ?? 22_000\n let goal = PROMPT_STEER_GOAL // fail-open default.\n let timer: ReturnType<typeof setTimeout> | undefined\n // One controller cancels BOTH search calls + the inference when the race ends\n // (timeout OR success), so a lost-race fetch never keeps the short-lived hook\n // process alive past its decision. Aborting after success is a harmless no-op.\n const controller = new AbortController()\n try {\n const enrich = (async (): Promise<string> => {\n const [lexical, semantic] = await Promise.all([\n input.io.searchCode(prompt, \"lexical\", controller.signal).catch(() => \"\"),\n input.io.searchCode(prompt, \"semantic\", controller.signal).catch(() => \"\"),\n ])\n const searchContext =\n `Lexical search results:\\n${lexical.slice(0, SEARCH_CONTEXT_CAP)}\\n\\n`\n + `Semantic search results:\\n${semantic.slice(0, SEARCH_CONTEXT_CAP)}`\n const note = await input.io.infer(\n PROMPT_SCOPE_SYSTEM,\n `USER REQUEST:\\n${prompt}\\n\\n${searchContext}`,\n controller.signal,\n )\n return note.trim()\n })()\n // Mark `enrich` handled so a post-race rejection (e.g. the inference aborting\n // after the timeout already won) can't surface as an unhandled rejection.\n enrich.catch(() => {})\n // The timer is cleared in `finally` (so when `enrich` wins, no live timer\n // lingers for the rest of `timeoutMs`).\n const raced = await Promise.race<string | \"__timeout__\">([\n enrich,\n new Promise<\"__timeout__\">((resolve) => {\n timer = setTimeout(() => resolve(\"__timeout__\"), timeoutMs)\n }),\n ])\n if (raced !== \"__timeout__\" && raced.length > 0) goal = raced\n } catch {\n // keep the fail-open regex goal.\n } finally {\n if (timer) clearTimeout(timer)\n controller.abort()\n }\n\n decision.inject = joinSections([PROMPT_SEARCH_TIP, goal, findingsBlock])\n return decision\n}\n\n/**\n * Build the shell command Claude Code runs for the `UserPromptSubmit` hook —\n * the running github-router via its node/bun binary so it works regardless of\n * PATH. Mirrors `buildStopHookCommand`.\n */\nexport function buildPromptSubmitHookCommand(execPath: string, scriptPath: string | undefined): string {\n const q = (s: string): string => `\"${s}\"`\n if (scriptPath && scriptPath !== execPath) {\n return `${q(execPath)} ${q(scriptPath)} internal-prompt-submit`\n }\n return `${q(execPath)} internal-prompt-submit`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAwBA,MAAa,oBACX;;;;;;AAYF,SAAgB,mBAAmB,QAAyB;CAC1D,MAAM,IAAI,OAAO,KAAK;CACtB,IAAI,EAAE,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AA,CACY,KAAK;EACnB,EAAA,CAAG;EAGH,OAAO,YAAY,CAAC,CAAC;EAGrB,MAAM,QAAQ,MAAM,QAAQ,KAA6B,CACvD,QACA,IAAI,SAAwB,YAAY;GACtC,QAAQ,iBAAiB,QAAQ,aAAa,GAAG,SAAS;EAC5D,CAAC,CACH,CAAC;EACD,IAAI,UAAU,iBAAiB,MAAM,SAAS,GAAG,OAAO;CAC1D,QAAQ,CAER,UAAU;EACR,IAAI,OAAO,aAAa,KAAK;EAC7B,WAAW,MAAM;CACnB;CAEA,SAAS,SAAS,aAAa;EAAC;EAAmB;EAAM;CAAa,CAAC;CACvE,OAAO;AACT;;;;;;AAOA,SAAgB,6BAA6B,UAAkB,YAAwC;CACrG,MAAM,KAAK,MAAsB,IAAI,EAAE;CACvC,IAAI,cAAc,eAAe,UAC/B,OAAO,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,UAAU,EAAE;CAEzC,OAAO,GAAG,EAAE,QAAQ,EAAE;AACxB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"serve-CM3OmF4I.js","names":["fs","fsSync","fs"],"sources":["../src/lib/serve/cloudcli.ts","../src/lib/serve/devtunnel.ts","../src/lib/serve/enhancements.ts","../src/lib/serve/provider-facade.ts","../src/lib/serve/reverse-proxy.ts","../src/serve.ts"],"sourcesContent":["import { spawn, type ChildProcess } from \"node:child_process\"\nimport { randomBytes } from \"node:crypto\"\nimport fs from \"node:fs/promises\"\nimport fsSync from \"node:fs\"\nimport http from \"node:http\"\nimport path from \"node:path\"\n\nimport consola from \"consola\"\n\nimport { runCommandCapture, resolveExecutable } from \"../exec\"\nimport { PATHS } from \"../paths\"\nimport { buildEnv } from \"../worker-agent/bash\"\n\nexport const CLOUDCLI_PACKAGE = \"@cloudcli-ai/cloudcli\"\n/** Pinned, tested known-good version. Never `@latest` (see docs/serve-control-plane.md). */\nexport const CLOUDCLI_PINNED_VERSION = \"1.36.1\"\n\n/**\n * Build the env handed to the CloudCLI child. Starts from `buildEnv()` — the\n * vetted secret-stripping ALLOWLIST used for worker bash (drops GITHUB_TOKEN,\n * GH_ROUTER_*, ANTHROPIC_AUTH_TOKEN, OPENAI_API_KEY, COPILOT_TOKEN by omission)\n * — then layers the non-secret Anthropic/Claude vars. The spawned claude still\n * authenticates because that rides the synthetic `.credentials.json` FILE in\n * CLAUDE_CONFIG_DIR, not an env token. Load-bearing: CloudCLI exposes a browser\n * terminal that can read its own env, so no secret may live there.\n */\nexport function composeCloudCliChildEnv(\n anthropicVars: Record<string, string>,\n): NodeJS.ProcessEnv {\n return { ...buildEnv(), ...anthropicVars }\n}\n\nconst SERVER_ENTRY_REL = path.join(\n \"dist-server\",\n \"server\",\n \"index.js\",\n)\n\nexport interface ResolvedCloudCli {\n /** Absolute path to the CloudCLI server entry (dist-server/server/index.js). */\n serverEntry: string\n version: string | null\n}\n\n/** Where the package's server entry lives under a given install root. */\nfunction serverEntryUnderRoot(root: string): string {\n return path.join(root, \"node_modules\", CLOUDCLI_PACKAGE, SERVER_ENTRY_REL)\n}\n\nasync function exists(p: string): Promise<boolean> {\n try {\n await fs.access(p)\n return true\n } catch {\n return false\n }\n}\n\nasync function readVersion(pkgRoot: string): Promise<string | null> {\n try {\n const raw = await fs.readFile(\n path.join(pkgRoot, \"node_modules\", CLOUDCLI_PACKAGE, \"package.json\"),\n \"utf8\",\n )\n return (JSON.parse(raw) as { version?: string }).version ?? null\n } catch {\n return null\n }\n}\n\n/**\n * Resolve the CloudCLI server entry, in order:\n * 1. an explicit `--cloudcli-path` (a package dir OR an install root),\n * 2. an existing pinned install in the router-owned dir,\n * 3. (unless `noInstall`) install the pinned version into the router-owned dir.\n * Never installs globally, never `@latest`.\n */\nexport async function resolveCloudCli(opts: {\n cliPath?: string\n noInstall: boolean\n version?: string\n}): Promise<ResolvedCloudCli> {\n const version = opts.version ?? CLOUDCLI_PINNED_VERSION\n\n // 1. explicit path — accept either a package dir or an install root.\n if (opts.cliPath) {\n const direct = path.join(opts.cliPath, SERVER_ENTRY_REL)\n const underRoot = serverEntryUnderRoot(opts.cliPath)\n if (await exists(direct)) return { serverEntry: direct, version: null }\n if (await exists(underRoot))\n return { serverEntry: underRoot, version: await readVersion(opts.cliPath) }\n throw new Error(\n `--cloudcli-path \"${opts.cliPath}\" does not contain a CloudCLI install (looked for ${SERVER_ENTRY_REL}).`,\n )\n }\n\n // 2. existing router-owned install.\n const root = PATHS.CLOUDCLI_HOME\n const entry = serverEntryUnderRoot(root)\n if (await exists(entry)) {\n return { serverEntry: entry, version: await readVersion(root) }\n }\n\n // 3. install the pinned version locally into the router-owned dir.\n if (opts.noInstall) {\n throw new Error(\n `CloudCLI is not installed and --no-install was given. Install it with:\\n npm i --prefix \"${root}\" ${CLOUDCLI_PACKAGE}@${version}`,\n )\n }\n await installCloudCli(root, version)\n if (!(await exists(entry))) {\n throw new Error(\n `CloudCLI install completed but the server entry is missing (${entry}).`,\n )\n }\n return { serverEntry: entry, version: await readVersion(root) }\n}\n\nasync function installCloudCli(root: string, version: string): Promise<void> {\n const npm = resolveExecutable(\"npm\")\n if (!npm) {\n throw new Error(\n \"npm not found on PATH — cannot install CloudCLI. Install Node.js/npm, or pass --cloudcli-path to an existing install.\",\n )\n }\n await fs.mkdir(root, { recursive: true, mode: 0o700 })\n // A local package.json so `npm i` installs into <root>/node_modules\n // instead of walking up to a parent project.\n const pkgJson = path.join(root, \"package.json\")\n if (!(await exists(pkgJson))) {\n await fs.writeFile(\n pkgJson,\n JSON.stringify({ name: \"gh-router-cloudcli-host\", private: true }, null, 2),\n )\n }\n consola.info(\n `Installing ${CLOUDCLI_PACKAGE}@${version} (one-time, ~a minute; builds native deps)…`,\n )\n const { code, stderr } = await runCommandCapture(\n [\n npm,\n \"install\",\n `${CLOUDCLI_PACKAGE}@${version}`,\n \"--prefix\",\n root,\n \"--no-audit\",\n \"--no-fund\",\n \"--loglevel=error\",\n ],\n { timeoutMs: 10 * 60_000 },\n )\n if (code !== 0) {\n throw new Error(\n `CloudCLI install failed (npm exit ${code}). This often means no network, a corporate proxy, or missing Windows build tools for native deps.\\nInstall manually then re-run with --cloudcli-path:\\n npm i --prefix \"${root}\" ${CLOUDCLI_PACKAGE}@${version}\\n${stderr.slice(0, 400)}`,\n )\n }\n}\n\nexport interface CloudCliProcess {\n child: ChildProcess\n port: number\n databasePath: string\n}\n\n/** Spawn CloudCLI on a loopback port with the given (already-filtered) env. */\nexport function spawnCloudCli(opts: {\n serverEntry: string\n port: number\n env: NodeJS.ProcessEnv\n}): CloudCliProcess {\n const databasePath = path.join(PATHS.CLOUDCLI_HOME, \"auth.db\")\n const node = resolveExecutable(\"node\") ?? process.execPath\n // Diagnostic opt-in: route CloudCLI's stdout+stderr to a log file so its\n // internal errors (SDK/DB/session-create) are inspectable. Default stays\n // \"ignore\" (CloudCLI is noisy and its logs aren't user-facing).\n const logPath = process.env.GH_ROUTER_CLOUDCLI_LOG\n let stdio: \"ignore\" | [\"ignore\", number, number] = \"ignore\"\n if (logPath) {\n try {\n const fd = fsSync.openSync(logPath, \"a\")\n stdio = [\"ignore\", fd, fd]\n } catch {\n /* unwritable log path — fall back to ignore */\n }\n }\n const child = spawn(node, [opts.serverEntry], {\n env: {\n ...opts.env,\n SERVER_PORT: String(opts.port),\n HOST: \"127.0.0.1\", // loopback only — never 0.0.0.0\n DATABASE_PATH: databasePath, // router-owned, isolated from a user's standalone CloudCLI\n // Do NOT set VITE_IS_PLATFORM: it disarms server auth and still shows a\n // login wall in the OSS bundle. Keep JWT auth on; auto-login is done by\n // the reverse proxy injecting the token.\n VITE_IS_PLATFORM: \"\",\n },\n stdio,\n windowsHide: true,\n })\n return { child, port: opts.port, databasePath }\n}\n\n// ---- health + JWT --------------------------------------------------------\n\nfunction req(\n port: number,\n method: \"GET\" | \"POST\",\n urlPath: string,\n body?: unknown,\n): Promise<{ status: number; body: string }> {\n return new Promise((resolve, reject) => {\n const data = body === undefined ? undefined : JSON.stringify(body)\n const r = http.request(\n {\n host: \"127.0.0.1\",\n port,\n path: urlPath,\n method,\n headers: data\n ? { \"content-type\": \"application/json\", \"content-length\": Buffer.byteLength(data) }\n : {},\n },\n (res) => {\n let b = \"\"\n res.on(\"data\", (d) => (b += d))\n res.on(\"end\", () => resolve({ status: res.statusCode ?? 0, body: b }))\n },\n )\n r.on(\"error\", reject)\n if (data) r.write(data)\n r.end()\n })\n}\n\nconst sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))\n\n/** Poll /api/auth/status until the server answers. Returns `needsSetup`. */\nexport async function waitForCloudCliReady(\n port: number,\n timeoutMs = 60_000,\n): Promise<{ needsSetup: boolean }> {\n const deadline = Date.now() + timeoutMs\n while (Date.now() < deadline) {\n try {\n const r = await req(port, \"GET\", \"/api/auth/status\")\n if (r.status === 200) {\n const parsed = JSON.parse(r.body) as { needsSetup?: boolean }\n return { needsSetup: parsed.needsSetup === true }\n }\n } catch {\n // not up yet\n }\n await sleep(1000)\n }\n throw new Error(`CloudCLI did not become ready within ${timeoutMs}ms`)\n}\n\nconst secretFile = () => path.join(PATHS.CLOUDCLI_HOME, \".serve-secret.json\")\n\nasync function loadPassword(username: string): Promise<string | null> {\n try {\n const raw = await fs.readFile(secretFile(), \"utf8\")\n const parsed = JSON.parse(raw) as { username?: string; password?: string }\n if (parsed.username === username && parsed.password) return parsed.password\n } catch {\n // no file yet\n }\n return null\n}\n\nasync function persistPassword(username: string, password: string): Promise<void> {\n // mode 0o600 + a 0o700 parent dir. On Windows the mode bits are ignored, but\n // the dir lives under the per-user home, which is user-ACL'd by default; the\n // password is a low-value local login for a loopback-only, Origin-gated app.\n await fs.mkdir(PATHS.CLOUDCLI_HOME, { recursive: true, mode: 0o700 })\n await fs.writeFile(\n secretFile(),\n JSON.stringify({ username, password }),\n { mode: 0o600 },\n )\n}\n\n/**\n * Ensure a CloudCLI user exists and return a valid JWT. On a fresh (isolated)\n * DB we register (persisting the password only AFTER success); on an existing\n * DB we log in with the persisted password.\n */\nexport async function mintJwt(\n port: number,\n username: string,\n needsSetup: boolean,\n): Promise<string> {\n const existing = await loadPassword(username)\n if (needsSetup) {\n const password = existing ?? randomBytes(24).toString(\"base64url\")\n const r = await req(port, \"POST\", \"/api/auth/register\", { username, password })\n if (r.status === 200) {\n const token = (JSON.parse(r.body) as { token?: string }).token\n if (token) {\n await persistPassword(username, password)\n return token\n }\n }\n // Fall through to login (e.g. a race created the user first).\n }\n const password = existing\n if (!password) {\n throw new Error(\n `CloudCLI already has a user but no stored password for \"${username}\". Delete ${path.join(PATHS.CLOUDCLI_HOME, \"auth.db\")} and retry.`,\n )\n }\n const r = await req(port, \"POST\", \"/api/auth/login\", { username, password })\n if (r.status === 200) {\n const token = (JSON.parse(r.body) as { token?: string }).token\n if (token) return token\n }\n throw new Error(\n `Could not obtain a CloudCLI session token (status ${r.status}). If a stale isolated DB exists, delete ${path.join(PATHS.CLOUDCLI_HOME, \"auth.db\")} and retry.`,\n )\n}\n","import { spawn } from \"node:child_process\"\nimport { createHash } from \"node:crypto\"\nimport os from \"node:os\"\n\nimport { killChildProcessTree, resolveExecutable, runCommandCapture } from \"../exec\"\n\n// Label stamped on every tunnel `serve --tunnel` creates, so our tunnels are\n// identifiable and sweepable (`devtunnel list -l github-router-serve`). Before\n// this, serve minted an anonymous, unlabeled tunnel per launch that was never\n// deleted — they accumulated server-side against the hard\n// `TunnelsPerUserPerCluster` (10) cap until new tunnels were denied.\nexport const SERVE_TUNNEL_LABEL = \"github-router-serve\"\n\n// A per-machine label so reuse/sweep scopes to THIS host and never touches a\n// tunnel another machine (sharing the same dev-tunnels account) owns. Hashed so\n// an arbitrary hostname maps to a label-charset-safe token.\nexport function serveTunnelMachineLabel(hostname: string = os.hostname()): string {\n return `ghr-machine-${createHash(\"sha256\").update(hostname).digest(\"hex\").slice(0, 12)}`\n}\n\n/**\n * Stable, deterministic tunnel ID for THIS machine, so `serve --tunnel` REUSES\n * one tunnel across launches → a stable, bookmarkable public URL. Derived from a\n * hash of the hostname (valid tunnel-id charset: lowercase alnum + hyphens,\n * bounded length). NOTE: the hosting URL's subdomain is a devtunnels-assigned\n * token, NOT this id — the id only anchors find/reuse; the URL is stable because\n * the same tunnel is re-hosted, not because the id appears in it.\n */\nexport function serveTunnelId(hostname: string = os.hostname()): string {\n return `ghr-serve-${createHash(\"sha256\").update(hostname).digest(\"hex\").slice(0, 12)}`\n}\n\n/**\n * Reconcile a reused tunnel's ports to exactly `[desired]`: which existing ports\n * to delete and whether the desired port must be created. Pure (unit-tested).\n * This is why reuse works across `--port` changes — `devtunnel host <id>\n * -p <newport>` fails (\"Batch update of ports is not supported\"), but per-port\n * `port delete` + `port create` does not.\n */\nexport function portsToReconcile(\n currentPorts: number[],\n desired: number,\n): { toDelete: number[]; toCreate: number | null } {\n return {\n toDelete: currentPorts.filter((p) => p !== desired),\n toCreate: currentPorts.includes(desired) ? null : desired,\n }\n}\n\nexport interface TunnelInfo {\n tunnelId: string\n labels?: string[]\n hostConnections?: number\n}\n\n/**\n * The IDs of our own idle tunnels for this machine to sweep — EXCLUDING the\n * stable reused tunnel (`exceptId`), which we keep and re-host. Pure\n * (unit-tested). A tunnel with a live host connection (a concurrent serve\n * instance) is never swept. Cleans up pre-migration random-named tunnels + any\n * crash-orphaned duplicates while preserving the one stable tunnel.\n */\nexport function serveTunnelIdsToSweep(\n tunnels: TunnelInfo[],\n machineLabel: string,\n exceptId?: string,\n): string[] {\n return tunnels\n .filter((t) => (t.labels ?? []).includes(machineLabel) && !t.hostConnections)\n .map((t) => t.tunnelId)\n .filter((id) => !exceptId || id.split(\".\")[0] !== exceptId)\n}\n\n/** List the tunnels serve owns (label-filtered). Best-effort: [] on any error. */\nasync function listServeTunnels(cli: string): Promise<TunnelInfo[]> {\n try {\n const { stdout, code } = await runCommandCapture(\n [cli, \"list\", \"-l\", SERVE_TUNNEL_LABEL, \"-j\"],\n { timeoutMs: 15_000 },\n )\n if (code !== 0) return []\n const parsed = JSON.parse(stdout) as { tunnels?: TunnelInfo[] }\n return Array.isArray(parsed?.tunnels) ? parsed.tunnels : []\n } catch {\n return []\n }\n}\n\n/** Best-effort delete of a single tunnel by full id (e.g. `fancy-fog-x.inc1`). */\nasync function deleteTunnel(cli: string, tunnelId: string): Promise<void> {\n try {\n await runCommandCapture([cli, \"delete\", tunnelId, \"-f\"], { timeoutMs: 15_000 })\n } catch {\n /* best-effort — a stale delete failing must not block hosting */\n }\n}\n\n/** Current port numbers configured on a tunnel. Best-effort: [] on any error. */\nasync function getTunnelPorts(cli: string, tunnelId: string): Promise<number[]> {\n try {\n const { stdout, code } = await runCommandCapture(\n [cli, \"port\", \"list\", tunnelId, \"-j\"],\n { timeoutMs: 15_000 },\n )\n if (code !== 0) return []\n const parsed = JSON.parse(stdout) as { ports?: Array<{ portNumber?: number }> }\n return (parsed?.ports ?? [])\n .map((p) => p.portNumber)\n .filter((n): n is number => typeof n === \"number\")\n } catch {\n return []\n }\n}\n\n/**\n * Ensure the stable per-machine tunnel exists (create it labeled if absent).\n * Returns true when the stable tunnel is present + owned by us (safe to reuse),\n * false to fall back to the anonymous-labeled host path. `existing` is the\n * pre-fetched label-scoped list (so we don't re-list).\n */\nasync function ensureStableTunnel(\n cli: string,\n stableId: string,\n machineLabel: string,\n existing: TunnelInfo[],\n): Promise<boolean> {\n const present = existing.some((t) => t.tunnelId.split(\".\")[0] === stableId)\n if (present) return true\n try {\n // `create` errors (\"Conflict\") if the id is already taken — but we only get\n // here when our label-scoped list did NOT contain it, so a conflict means a\n // DIFFERENT owner holds the id: fall back rather than reuse a foreign tunnel.\n const { code } = await runCommandCapture(\n [cli, \"create\", stableId, \"-l\", SERVE_TUNNEL_LABEL, \"-l\", machineLabel,\n \"-d\", \"github-router-serve-control-plane\"],\n { timeoutMs: 20_000 },\n )\n return code === 0\n } catch {\n return false\n }\n}\n\n/** Reconcile the tunnel's ports to exactly `[port]` (delete stale, create missing). */\nasync function reconcileTunnelPorts(cli: string, tunnelId: string, port: number): Promise<void> {\n const { toDelete, toCreate } = portsToReconcile(await getTunnelPorts(cli, tunnelId), port)\n for (const p of toDelete) {\n await runCommandCapture([cli, \"port\", \"delete\", tunnelId, \"-p\", String(p)], { timeoutMs: 15_000 }).catch(() => {})\n }\n if (toCreate != null) {\n await runCommandCapture([cli, \"port\", \"create\", tunnelId, \"-p\", String(toCreate)], { timeoutMs: 15_000 }).catch(() => {})\n }\n}\n\n\n// Matches the browser URL devtunnel prints, e.g.\n// Hosting port 5454 at https://l3rs99qw-5454.usw2.devtunnels.ms/\nconst DEVTUNNEL_URL_RE =\n /https:\\/\\/[a-z0-9][a-z0-9-]*(?:\\.[a-z0-9-]+)*\\.devtunnels\\.ms\\/?/i\n\nexport type DevtunnelFailureKind =\n | \"not-installed\"\n | \"not-logged-in\"\n | \"timeout\"\n | \"exited\"\n | \"spawn\"\n\nexport class DevtunnelError extends Error {\n readonly kind: DevtunnelFailureKind\n constructor(kind: DevtunnelFailureKind, message: string) {\n super(message)\n this.name = \"DevtunnelError\"\n this.kind = kind\n }\n}\n\nexport interface DevtunnelHandle {\n url: string\n stop: () => void\n}\n\n/** Absolute path to the `devtunnel` CLI, or null if not on PATH. */\nexport function resolveDevtunnelCli(): string | null {\n return resolveExecutable(\"devtunnel\")\n}\n\n/** Best-effort login check via `devtunnel user show`. */\nexport async function isDevtunnelLoggedIn(cli: string): Promise<boolean> {\n try {\n const { stdout, stderr, code } = await runCommandCapture(\n [cli, \"user\", \"show\"],\n { timeoutMs: 15_000 },\n )\n const out = `${stdout}\\n${stderr}`\n if (/not logged in/i.test(out)) return false\n if (/logged in as/i.test(out)) return true\n return code === 0\n } catch {\n return false\n }\n}\n\n/** Extract the dev-tunnel browser URL from CLI output. Exported for tests. */\nexport function parseDevtunnelUrl(text: string): string | null {\n const m = text.match(DEVTUNNEL_URL_RE)\n return m ? m[0].replace(/\\/+$/, \"\") : null\n}\n\n/**\n * Spawn `devtunnel <args>` and resolve once it prints its public URL. Shared by\n * the stable-reuse and anonymous-labeled host paths. Rejects (DevtunnelError) on\n * timeout, early exit, an auth error in the output, or a spawn failure.\n */\nfunction hostTunnelProcess(cli: string, args: string[], timeoutMs: number): Promise<DevtunnelHandle> {\n const child = spawn(cli, args, { stdio: [\"ignore\", \"pipe\", \"pipe\"], windowsHide: true })\n return new Promise<DevtunnelHandle>((resolve, reject) => {\n let settled = false\n const stop = () => {\n // Tree-kill: the devtunnel process spawns a helper and a plain\n // child.kill() leaves the tunnel up (verified). taskkill /T /F on\n // Windows, process-group SIGTERM on POSIX.\n try {\n killChildProcessTree(child, { detachedGroup: process.platform !== \"win32\" })\n } catch {\n try {\n child.kill()\n } catch {\n /* already gone */\n }\n }\n }\n const finish = (fn: () => void) => {\n if (settled) return\n settled = true\n clearTimeout(timer)\n fn()\n }\n const timer = setTimeout(\n () =>\n finish(() => {\n stop()\n reject(new DevtunnelError(\"timeout\", \"dev tunnel did not report a URL in time\"))\n }),\n timeoutMs,\n )\n const onData = (buf: Buffer) => {\n const text = String(buf)\n const url = parseDevtunnelUrl(text)\n if (url) {\n finish(() => resolve({ url, stop }))\n return\n }\n if (/not logged in|login (required|expired)|unauthorized/i.test(text)) {\n finish(() => {\n stop()\n reject(new DevtunnelError(\"not-logged-in\", \"dev tunnel authentication failed; run `devtunnel user login`\"))\n })\n }\n }\n child.stdout?.on(\"data\", onData)\n child.stderr?.on(\"data\", onData)\n child.once(\"error\", (err) => finish(() => reject(new DevtunnelError(\"spawn\", err.message))))\n child.once(\"exit\", (code) =>\n finish(() =>\n reject(new DevtunnelError(\"exited\", `devtunnel exited (code ${code}) before printing a URL`)),\n ),\n )\n })\n}\n\n/**\n * Host an AUTHENTICATED (never anonymous) dev tunnel forwarding `port`, resolving\n * once its public URL is printed. NEVER `--allow-anonymous` (owner-only access).\n *\n * REUSES a stable per-machine tunnel (`serveTunnelId`) across launches → a\n * STABLE, bookmarkable public URL. Each launch: ensure the stable tunnel exists\n * (create it labeled if absent), reconcile its ports to exactly `[port]` via\n * per-port delete/create (`host <id> -p <newport>` can't change ports — it 400s\n * \"Batch update of ports is not supported\"), sweep our OTHER idle labeled tunnels\n * (pre-migration random-named ones / crash orphans), then `host <id>`. The URL's\n * subdomain is a devtunnels-assigned token (not the id), but it's stable because\n * the SAME tunnel is re-hosted every launch.\n *\n * If any stable-path step fails (id already taken by another owner, CLI hiccup),\n * falls back to hosting a fresh anonymous-labeled tunnel (the prior behavior) so\n * `--tunnel` always works — just without a stable URL that launch.\n */\nexport async function startDevtunnel(\n port: number,\n opts: { timeoutMs?: number } = {},\n): Promise<DevtunnelHandle> {\n const cli = resolveDevtunnelCli()\n if (!cli) {\n throw new DevtunnelError(\n \"not-installed\",\n \"The `devtunnel` CLI is not installed. Install it (winget install Microsoft.devtunnel / brew install --cask devtunnel) then run `devtunnel user login`.\",\n )\n }\n if (!(await isDevtunnelLoggedIn(cli))) {\n throw new DevtunnelError(\n \"not-logged-in\",\n \"Not signed in to dev tunnels. Run `devtunnel user login` (or `devtunnel user login -g` for GitHub) first.\",\n )\n }\n\n const timeoutMs = opts.timeoutMs ?? 40_000\n const machineLabel = serveTunnelMachineLabel()\n const stableId = serveTunnelId()\n\n // Stable-reuse path (best-effort; any failure falls through to anonymous host).\n try {\n const owned = await listServeTunnels(cli)\n if (await ensureStableTunnel(cli, stableId, machineLabel, owned)) {\n await reconcileTunnelPorts(cli, stableId, port)\n // Sweep our OTHER idle labeled tunnels but KEEP the stable one.\n for (const id of serveTunnelIdsToSweep(owned, machineLabel, stableId)) {\n await deleteTunnel(cli, id)\n }\n return await hostTunnelProcess(cli, [\"host\", stableId], timeoutMs)\n }\n } catch {\n /* fall through to the anonymous-labeled host path */\n }\n\n // Fallback: sweep our idle labeled tunnels, then host a fresh anonymous one.\n // Labels are SEPARATE `-l` flags — the service rejects a label containing a\n // space (`must match '[\\w-=]{1,50}'`), so they can't be joined into one arg.\n try {\n for (const id of serveTunnelIdsToSweep(await listServeTunnels(cli), machineLabel)) {\n await deleteTunnel(cli, id)\n }\n } catch {\n /* best-effort sweep — never blocks hosting */\n }\n return await hostTunnelProcess(\n cli,\n [\"host\", \"-p\", String(port), \"-l\", SERVE_TUNNEL_LABEL, \"-l\", machineLabel, \"-d\", \"github-router-serve-control-plane\"],\n timeoutMs,\n )\n}\n","import path from \"node:path\"\n\nimport consola from \"consola\"\n\nimport { injectAttributionSuppressionIntoSettingsFile } from \"../attribution-settings\"\nimport {\n BUILTIN_SUBAGENT_DEFINITIONS,\n injectPeerMcpIntoMirror,\n resolveGroupKeysFromMirror,\n workersKeyOf,\n writePeerMcpRuntimeFiles,\n} from \"../codex-mcp-config\"\nimport {\n appendPeerAwarenessToMirroredClaudeMd,\n appendToolbeltAwarenessToMirroredClaudeMd,\n buildOperatingDefaultsDirective,\n type NativeAgentAvailability,\n prependOperatingDefaultsToMirroredClaudeMd,\n prependStyleDirectiveToMirroredClaudeMd,\n} from \"../claude-md-injection\"\nimport { INJECTED_SKILLS, writeInjectedSkill } from \"../injected-skills\"\nimport {\n configureServeDefaultPermissionMode,\n injectAllowRules,\n planModeAllowRules,\n sanitizeServeSettingsEnv,\n} from \"../mcp-permissions-settings\"\nimport {\n agentToolsEnabled,\n browseAgentEnabled,\n browserCompoundToolsEnabled,\n browserToolsEnabled,\n fleetToolsEnabled,\n geminiAvailable,\n nativeSubagentModel,\n brainstormModel,\n reviewerModel,\n scoutModel,\n scribeModel,\n genericModel,\n genericFastModel,\n genericCheapModel,\n standInToolEnabled,\n workerToolsEnabled,\n} from \"../mcp-capabilities\"\nimport { buildPlanReviewHookCommand, planReviewEnabled } from \"../orchestration/plan-review-hook\"\nimport { buildPromptSubmitHookCommand } from \"../orchestration/prompt-submit-hook\"\nimport { injectStopHookIntoSettingsFile } from \"../orchestration/stop-gate-hook\"\nimport { PATHS } from \"../paths\"\nimport { buildPeerAwarenessSnippet, enumerateInjectedMcpToolNames, type McpGroup } from \"../peer-mcp-personas\"\nimport { state } from \"../state\"\nimport { availableToolCommands, buildToolbeltAwareness, toolbeltEnabled } from \"../toolbelt\"\nimport {\n activeDispatchModes,\n buildWorkerGuardHookCommand,\n guardToolMatcher,\n} from \"../worker-dispatch\"\n\nexport interface ServeEnhancementsHandle {\n cleanup: () => Promise<void>\n nonce?: string\n /** Exact `mcp__<key>__<tool>` names of every injected MCP tool — seeded into\n * the reverse-proxy's `localStorage['claude-settings'].allowedTools` so\n * CloudCLI's `canUseTool` auto-approves them in PLAN mode. */\n mcpToolNames?: string[]\n}\n\nexport interface ServeEnhancementOpts {\n /** Route Codex personas through a local `codex mcp-server` (stdio) when the backend resolved to \"cli\". */\n codexCli?: boolean\n /** True when the control plane is reachable beyond loopback (a --tunnel or --public-url is active). Gates the highest-blast-radius capabilities OFF by default: the server-side browser MCP (a session-hijack / SSRF / cloud-metadata primitive when reachable remotely) and first-mate (mints a repo+workflow GitHub write token). */\n tunnelExposed?: boolean\n /** Operator opt-in to expose the browser MCP over the tunnel despite the risk. */\n browseOverTunnel?: boolean\n /** Operator opt-in to expose first-mate over the tunnel despite the risk. */\n agentsOverTunnel?: boolean\n /** Operator opt-in to expose the fleet MCP over the tunnel despite the risk\n * (fleet drives remote coding sessions with the operator's stored fleet\n * credentials — an equal-or-higher blast radius than the browser MCP). */\n fleetOverTunnel?: boolean\n}\n\nconst NOOP: ServeEnhancementsHandle = { cleanup: async () => {} }\n\n/**\n * Wire the github-router enhancement layer into the router-owned\n * CLAUDE_CONFIG_DIR mirror so a CloudCLI-spawned Claude session gets the same\n * tools `github-router claude` provides:\n * - the scoped MCP servers (peers / search / orchestrate, plus workers /\n * decide / browser / fleet / first-mate when their gate passes) — the\n * SDK-spawned claude reads them from the mirror's `.claude.json`,\n * - the peer-critic / worker / implementer subagents (`.md` files written into\n * the mirror's `agents/`),\n * - the gh-* skills (research / orchestrate / floor-keeper / worker).\n *\n * Best-effort: on any failure Claude still works, just without the extras.\n * Must run AFTER `ensureClaudeConfigMirror()` and BEFORE CloudCLI is spawned.\n * Tab-specific bits (ai-or-die hooks, artifact review) are intentionally left\n * out — they don't apply to the serve control plane.\n */\nexport async function provisionServeEnhancements(\n serverUrl: string,\n opts: ServeEnhancementOpts = {},\n): Promise<ServeEnhancementsHandle> {\n try {\n const tunnelExposed = opts.tunnelExposed === true\n const browseAllowed = browserToolsEnabled() && (!tunnelExposed || opts.browseOverTunnel === true)\n const firstMateAllowed = agentToolsEnabled() && (!tunnelExposed || opts.agentsOverTunnel === true)\n const fleetAllowed = fleetToolsEnabled() && (!tunnelExposed || opts.fleetOverTunnel === true)\n\n const enabledGroups: McpGroup[] = [\"peers\", \"search\", \"orchestrate\"]\n if (workerToolsEnabled()) enabledGroups.push(\"workers\")\n if (standInToolEnabled()) enabledGroups.push(\"decide\")\n if (browseAllowed) enabledGroups.push(\"browser\")\n if (fleetAllowed) enabledGroups.push(\"fleet\")\n if (firstMateAllowed) enabledGroups.push(\"first-mate\")\n\n const gem = geminiAvailable()\n const { keys: groupKeys } = await resolveGroupKeysFromMirror(enabledGroups)\n // Resolved once and reused by the `.md` generation, the awareness snippet,\n // and the operating-defaults directive, so the three surfaces cannot\n // disagree about which conditionally-emitted natives exist.\n const nativeAvailability: NativeAgentAvailability = {\n scoutAvailable: scoutModel() != null,\n genericAvailable: genericModel() != null,\n genericFastAvailable: genericFastModel() != null,\n genericCheapAvailable: genericCheapModel() != null,\n }\n\n const runtime = await writePeerMcpRuntimeFiles(serverUrl, {\n codexCli: opts.codexCli === true,\n geminiAvailable: gem,\n groupKeys,\n workerToolsAvailable: workerToolsEnabled(),\n browseAvailable: browseAllowed && browseAgentEnabled(),\n nativeSubagentModel: nativeSubagentModel(),\n reviewerModel: reviewerModel(),\n brainstormModel: brainstormModel(),\n scoutModel: scoutModel(),\n scribeModel: scribeModel(),\n genericModel: genericModel(),\n genericFastModel: genericFastModel(),\n genericCheapModel: genericCheapModel(),\n // Serve-only: register Claude Code's built-in Explore/Plan/general-purpose\n // subagents (the Agent SDK doesn't) so the model's habitual Agent() calls\n // resolve. Never passed by `github-router claude` (would shadow the CLI's\n // native built-ins).\n builtinSubagents: BUILTIN_SUBAGENT_DEFINITIONS,\n })\n // The proxy's /mcp handler authorizes tool calls against this per-launch\n // nonce (the same value baked into the mirrored mcpServers Authorization).\n state.peerMcpNonce = runtime.nonce\n\n const injected = await injectPeerMcpIntoMirror(serverUrl, {\n codexCli: opts.codexCli === true,\n geminiAvailable: gem,\n groupKeys,\n nonce: runtime.nonce,\n })\n\n const peerSnippet = buildPeerAwarenessSnippet({\n codexCli: opts.codexCli === true,\n geminiAvailable: gem,\n workerToolsAvailable: workerToolsEnabled(),\n standInAvailable: standInToolEnabled(),\n browseAvailable: browseAllowed,\n compoundBrowseAvailable: browseAllowed && browserCompoundToolsEnabled(),\n powerBrowseAvailable: browseAllowed && state.powerBrowseEnabled,\n agentToolsAvailable: firstMateAllowed,\n ...nativeAvailability,\n groupKeys,\n })\n\n await appendPeerAwarenessToMirroredClaudeMd(peerSnippet).catch((err) =>\n consola.warn(`Peer-awareness CLAUDE.md append failed: ${String(err)}`),\n )\n await prependStyleDirectiveToMirroredClaudeMd().catch((err) =>\n consola.warn(`Style-directive CLAUDE.md prepend failed: ${String(err)}`),\n )\n await prependOperatingDefaultsToMirroredClaudeMd(\n buildOperatingDefaultsDirective(nativeAvailability),\n ).catch((err) =>\n consola.warn(`Operating-defaults CLAUDE.md prepend failed: ${String(err)}`),\n )\n if (toolbeltEnabled()) {\n const toolbeltLine = buildToolbeltAwareness(availableToolCommands())\n if (toolbeltLine) {\n await appendToolbeltAwarenessToMirroredClaudeMd(toolbeltLine).catch((err) =>\n consola.warn(`Toolbelt CLAUDE.md append failed: ${String(err)}`),\n )\n }\n }\n\n // gh-* skills, minus the operator/tab-specific ones (first-mate, artifact).\n let skillsWritten = 0\n for (const s of INJECTED_SKILLS) {\n if (s.name.startsWith(\"gh-first-mate\") || s.name === \"gh-artifact-review\") {\n continue\n }\n const r = await writeInjectedSkill(s.name, s.md).catch(() => ({\n written: false,\n }))\n if (r.written) skillsWritten++\n }\n\n const settingsPath = path.join(PATHS.CLAUDE_CONFIG_DIR, \"settings.json\")\n\n // Suppress AI attribution in the mirror's settings.json (no-op if the user\n // already configured it).\n await injectAttributionSuppressionIntoSettingsFile(settingsPath).catch(() => {})\n\n // Strip serve-inappropriate CLAUDE_CODE_* env keys the user may have set in\n // their real settings.json — notably CLAUDE_CODE_COORDINATOR_MODE, which\n // CloudCLI applies via settingSources and which strips the single serve chat\n // agent down to delegation-only tools (a direct Glob/Read/Bash then fails\n // \"not enabled in this context\"). UNCONDITIONAL: a working toolset is needed\n // even when GH_ROUTER_SERVE_NO_AUTO_APPROVE=1 keeps permission prompts on.\n const strip = await sanitizeServeSettingsEnv(settingsPath).catch((err) => {\n consola.warn(`Could not sanitize serve settings env: ${String(err)}`)\n return { removed: [] as string[] }\n })\n if (strip.removed.length) {\n consola.info(`Serve: removed ${strip.removed.join(\", \")} from the mirror settings (single-agent surface).`)\n }\n\n // Serve's permission model: \"seamless routine + decisions to the user\".\n // Set the mirror's default permission mode to \"default\" (NON-bypass) so\n // CloudCLI's canUseTool callback stays live — routine tools pre-approved via\n // the seeded allow-list run with no prompt, while AskUserQuestion/ExitPlanMode\n // (never allow-listed) reach the human as real widgets. Bypass would skip\n // canUseTool and auto-answer those two. Opt out with GH_ROUTER_SERVE_NO_AUTO_APPROVE=1\n // (then CloudCLI prompts on every not-yet-approved tool).\n if (process.env.GH_ROUTER_SERVE_NO_AUTO_APPROVE !== \"1\") {\n await configureServeDefaultPermissionMode(settingsPath).catch((err) =>\n consola.warn(`Could not set serve permission defaults: ${String(err)}`),\n )\n }\n\n // Auto-approve github-router's injected MCP servers PLUS the native read-only\n // research tools (WebSearch/WebFetch) via allow rules (MCP on the resolved\n // keys). Redundant under the bypass default above, but load-bearing the moment\n // a serve user switches the composer to PLAN mode: plan mode re-gates tools,\n // and this is exactly the research surface wanted while planning. Runs AFTER\n // the bypass config so the entries survive its allow-list clear. Uses the\n // authoritative injected-server list (`injected.serversAdded` — includes the\n // `codex-cli` stdio server), falling back to the resolved keys + codex-cli on\n // collision.\n const injectedServerKeys = injected.ok\n ? injected.serversAdded\n : [\n ...Object.values(groupKeys).filter((k): k is string => Boolean(k)),\n ...(opts.codexCli === true ? [\"codex-cli\"] : []),\n ]\n await injectAllowRules(settingsPath, planModeAllowRules(injectedServerKeys)).catch((err) =>\n consola.warn(`Could not auto-approve injected tools: ${String(err)}`),\n )\n\n if (workerToolsEnabled()) {\n const promptCmd = buildPromptSubmitHookCommand(process.execPath, process.argv[1])\n await injectStopHookIntoSettingsFile(settingsPath, promptCmd, \"UserPromptSubmit\", 45).catch(\n (err) => consola.warn(`Could not register the UserPromptSubmit hook: ${String(err)}`),\n )\n\n if (!injected.ok) {\n consola.warn(\n \"Workers non-blocking guard NOT registered: subagent MCP injection fell back to parent-only, so worker-* dispatchers cannot reach the workers server. Raw worker tools remain usable on the main thread.\",\n )\n } else if (process.env.GH_ROUTER_DISABLE_WORKER_GUARD !== \"1\") {\n const workersKey = workersKeyOf(groupKeys)\n const modes = activeDispatchModes({ browse: browseAllowed && browseAgentEnabled() })\n const cmd = buildWorkerGuardHookCommand(\n process.execPath,\n process.argv[1],\n workersKey,\n modes,\n )\n const matcher = guardToolMatcher(workersKey, modes)\n await injectStopHookIntoSettingsFile(settingsPath, cmd, \"PreToolUse\", 10, matcher).catch(\n (err) => consola.warn(`Could not register the workers PreToolUse guard hook: ${String(err)}`),\n )\n } else {\n consola.info(\n \"Workers non-blocking guard disabled via GH_ROUTER_DISABLE_WORKER_GUARD=1; raw worker tools remain callable on the main thread.\",\n )\n }\n }\n\n if (planReviewEnabled()) {\n try {\n const command = buildPlanReviewHookCommand(process.execPath, process.argv[1])\n // Advisory PostToolUse(ExitPlanMode) hook: non-blocking; findings surface on the next prompt.\n // Verified (scripts/verify-plan-mode-cloudcli.mjs): ExitPlanMode runs as a real tool under\n // CloudCLI's SDK path, and settingSources=['project','user','local'] loads this mirror\n // settings.json hook, so PostToolUse(ExitPlanMode) fires on approval+execution.\n await injectStopHookIntoSettingsFile(settingsPath, command, \"PostToolUse\", undefined, \"ExitPlanMode\")\n } catch (err) {\n consola.warn(`Could not register the advisory plan-review hook: ${String(err)}`)\n }\n }\n\n const servers = injected.ok\n ? injected.serversAdded.join(\", \")\n : \"parent-only (user mcpServers collision)\"\n consola.info(\n `github-router tools wired into Claude sessions: MCP [${servers}], ${runtime.personas.length} subagents, ${skillsWritten} skills.`,\n )\n return {\n cleanup: runtime.cleanup,\n nonce: runtime.nonce,\n mcpToolNames: enumerateInjectedMcpToolNames(groupKeys, { codexCli: opts.codexCli === true }),\n }\n } catch (err) {\n consola.warn(\n `Could not wire the github-router MCP/skills layer: ${\n err instanceof Error ? err.message : String(err)\n }. Claude still works, without the extra tools.`,\n )\n return NOOP\n }\n}\n","import fs from \"node:fs/promises\"\nimport path from \"node:path\"\n\nimport type { Model, ModelsResponse } from \"~/services/copilot/get-models\"\n\nexport interface ProviderFacadeContext {\n getModels: () => ModelsResponse | undefined\n defaultModel: string\n claudeConfigDir: string\n}\n\nexport type ProviderFacadeKind = \"models\" | \"mcp\" | \"skills\" | \"commands\" | \"auth\"\n\ntype JsonObject = Record<string, unknown>\n\nfunction isObject(value: unknown): value is JsonObject {\n return value !== null && typeof value === \"object\" && !Array.isArray(value)\n}\n\nfunction envelopedData(value: unknown): JsonObject | null {\n if (!isObject(value) || !isObject(value.data)) return null\n return value.data\n}\n\nfunction cloneObject<T extends JsonObject>(value: T): JsonObject {\n return { ...value }\n}\n\n/**\n * Requests serve answers DIRECTLY (never forwarded to CloudCLI) with a canned\n * JSON body. `POST /api/system/update` is CloudCLI's \"update available\" button,\n * which runs a GLOBAL `npm install -g @cloudcli-ai/cloudcli@latest` in the user's\n * home dir (`server/index.js`) — mutating their global npm and never applying to\n * serve's pinned, router-owned install. Blocking it prevents that surprise\n * mutation and tells the user how updates actually work. HTTP 200 + `success:\n * false` so the UI neither runs the update nor throws an error toast. Returns\n * null for anything not blocked.\n */\nexport function facadeBlockedRequest(\n method: string,\n pathname: string,\n): { status: number; json: JsonObject } | null {\n if (method.toUpperCase() === \"POST\" && pathname === \"/api/system/update\") {\n return {\n status: 200,\n json: {\n success: false,\n managed: true,\n message:\n \"CloudCLI is managed by github-router (a pinned, router-owned install). \"\n + \"To update, run `npm i -g github-router@latest`, then restart `github-router serve`.\",\n },\n }\n }\n return null\n}\n\nexport function facadeInterceptKind(\n method: string,\n pathname: string,\n): ProviderFacadeKind | null {\n const normalizedMethod = method.toUpperCase()\n if (normalizedMethod === \"POST\" && pathname === \"/api/commands/list\") {\n return \"commands\"\n }\n if (normalizedMethod !== \"GET\") return null\n\n switch (pathname) {\n case \"/api/providers/claude/models\":\n return \"models\"\n case \"/api/providers/claude/mcp/servers\":\n return \"mcp\"\n case \"/api/providers/claude/skills\":\n return \"skills\"\n case \"/api/providers/claude/auth/status\":\n return \"auth\"\n default:\n return null\n }\n}\n\nexport async function rewriteProviderResponse(\n kind: ProviderFacadeKind,\n upstreamJson: unknown,\n ctx: ProviderFacadeContext,\n query: URLSearchParams,\n): Promise<unknown | null> {\n try {\n switch (kind) {\n case \"models\":\n return rewriteModels(upstreamJson, ctx)\n case \"mcp\":\n return rewriteMcp(upstreamJson, ctx, query)\n case \"skills\":\n return rewriteSkills(upstreamJson, ctx)\n case \"commands\":\n return rewriteCommands(upstreamJson, ctx)\n case \"auth\":\n return rewriteAuth(upstreamJson)\n }\n } catch {\n return null\n }\n}\n\nfunction rewriteModels(upstreamJson: unknown, ctx: ProviderFacadeContext): unknown | null {\n if (!isObject(upstreamJson)) return null\n const data = envelopedData(upstreamJson)\n if (!data || !isObject(data.models) || data.cache === undefined) return null\n\n const catalog = ctx.getModels()\n const models = catalog?.data\n if (!Array.isArray(models) || models.length === 0) return null\n\n const options = models\n .filter(isPickerModel)\n .map((m) => {\n const option: { value: string; label: string; description: string; effort?: JsonObject } = {\n value: m.id,\n label: m.name || m.id,\n description: `${m.vendor} · ${m.capabilities.family}`,\n }\n // Emit the effort selector the CloudCLI UI + `resolveClaudeEffort` need:\n // `effort.values` is `[{value}]` and the server maps it back with\n // `.map(v => v.value)` to validate a client-picked effort before passing\n // it to the binary. Derived from the Copilot catalog's per-model\n // `reasoning_effort` allowlist; omitted for non-reasoning models (no\n // selector shown). Default to `high` when supported (matches the shim's\n // default), else the model's top tier.\n const efforts = m.capabilities.supports?.reasoning_effort\n if (Array.isArray(efforts) && efforts.length > 0) {\n option.effort = {\n default: efforts.includes(\"high\") ? \"high\" : efforts[efforts.length - 1],\n values: efforts.map((e) => ({ value: e })),\n }\n }\n return option\n })\n if (options.length === 0) return null\n\n const values = new Set(options.map((o) => o.value))\n const upstreamDefault = typeof data.models.DEFAULT === \"string\" ? data.models.DEFAULT : undefined\n const DEFAULT = values.has(ctx.defaultModel)\n ? ctx.defaultModel\n : (options[0]?.value ?? upstreamDefault)\n\n const nextData = cloneObject(data)\n nextData.models = { OPTIONS: options, DEFAULT }\n return { ...upstreamJson, data: nextData }\n}\n\nfunction isPickerModel(model: unknown): model is Model {\n return isObject(model)\n && typeof model.id === \"string\"\n && model.model_picker_enabled !== false\n && typeof model.vendor === \"string\"\n && (model.name === undefined || typeof model.name === \"string\")\n && isObject(model.capabilities)\n && typeof model.capabilities.family === \"string\"\n}\n\nasync function rewriteMcp(\n upstreamJson: unknown,\n ctx: ProviderFacadeContext,\n query: URLSearchParams,\n): Promise<unknown | null> {\n if (!isObject(upstreamJson) || query.get(\"scope\") !== \"user\") return null\n const data = envelopedData(upstreamJson)\n if (!data || !Array.isArray(data.servers)) return null\n\n const mirror = await readJson(path.join(ctx.claudeConfigDir, \".claude.json\"))\n if (!isObject(mirror) || !isObject(mirror.mcpServers)) return null\n\n const existing = new Set(\n data.servers\n .filter(isObject)\n .map((s) => s.name)\n .filter((name): name is string => typeof name === \"string\"),\n )\n const additions: JsonObject[] = []\n for (const [name, entry] of Object.entries(mirror.mcpServers)) {\n if (existing.has(name) || !isObject(entry)) continue\n const transport = entry.type === \"http\" ? \"http\" : \"stdio\"\n const server: JsonObject = {\n provider: \"claude\",\n name,\n scope: \"user\",\n transport,\n }\n if (typeof entry.url === \"string\") server.url = entry.url\n additions.push(server)\n existing.add(name)\n }\n if (additions.length === 0) return null\n\n const nextData = cloneObject(data)\n nextData.servers = [...data.servers, ...additions]\n return { ...upstreamJson, data: nextData }\n}\n\nasync function rewriteSkills(\n upstreamJson: unknown,\n ctx: ProviderFacadeContext,\n): Promise<unknown | null> {\n if (!isObject(upstreamJson)) return null\n const data = envelopedData(upstreamJson)\n if (!data || !Array.isArray(data.skills)) return null\n\n const skills = await readSkillDirs(path.join(ctx.claudeConfigDir, \"skills\"))\n if (skills.length === 0) return null\n\n const existing = new Set(\n data.skills\n .filter(isObject)\n .map((s) => s.command)\n .filter((command): command is string => typeof command === \"string\"),\n )\n const additions = skills\n .filter((skill) => !existing.has(skill.name))\n .map((skill) => ({\n provider: \"claude\",\n name: skill.name,\n description: skill.description,\n command: skill.name,\n scope: \"user\",\n // sourcePath intentionally omitted — the UI dispatches on `command`/`name`,\n // and an absolute mirror path would leak the home dir / username to the browser.\n }))\n if (additions.length === 0) return null\n\n const nextData = cloneObject(data)\n nextData.skills = [...data.skills, ...additions]\n return { ...upstreamJson, data: nextData }\n}\n\nasync function rewriteCommands(\n upstreamJson: unknown,\n ctx: ProviderFacadeContext,\n): Promise<unknown | null> {\n if (!isObject(upstreamJson) || !Array.isArray(upstreamJson.custom)) return null\n\n const dirs = [\n path.join(ctx.claudeConfigDir, \"commands\"),\n path.join(ctx.claudeConfigDir, \".claude\", \"commands\"),\n ]\n const commandFiles = (await Promise.all(dirs.map(readCommandFiles))).flat()\n if (commandFiles.length === 0) return null\n\n const existing = new Set(\n upstreamJson.custom\n .filter(isObject)\n .map((c) => c.name)\n .filter((name): name is string => typeof name === \"string\"),\n )\n const additions = commandFiles\n .filter((command) => !existing.has(command.name))\n .map((command) => ({\n name: command.name,\n // Relative path only — an absolute mirror path would leak the home dir /\n // username to the browser. The UI dispatches on `name`.\n path: command.relativePath,\n relativePath: command.relativePath,\n description: command.description,\n namespace: \"user\",\n metadata: { type: \"custom\" },\n }))\n if (additions.length === 0) return null\n\n const custom = [...upstreamJson.custom, ...additions]\n return {\n ...upstreamJson,\n custom,\n count: countCommands(upstreamJson.builtIn) + custom.length,\n }\n}\n\nfunction rewriteAuth(upstreamJson: unknown): unknown | null {\n if (!isObject(upstreamJson)) return null\n const data = envelopedData(upstreamJson)\n if (!data) return null\n\n return {\n ...upstreamJson,\n data: {\n ...data,\n authenticated: true,\n installed: true,\n },\n }\n}\n\nasync function readJson(filePath: string): Promise<unknown | null> {\n try {\n return JSON.parse(await fs.readFile(filePath, \"utf8\"))\n } catch {\n return null\n }\n}\n\nasync function readSkillDirs(skillsDir: string): Promise<Array<{ name: string; description: string; sourcePath: string }>> {\n try {\n const entries = await fs.readdir(skillsDir, { withFileTypes: true })\n const skills = await Promise.all(\n entries\n .filter((entry) => entry.isDirectory() && entry.name.startsWith(\"gh-\"))\n .map(async (entry) => {\n const sourcePath = path.join(skillsDir, entry.name, \"SKILL.md\")\n let description = \"\"\n try {\n const md = await fs.readFile(sourcePath, \"utf8\")\n description = parseFrontmatterField(md, \"description\") ?? \"\"\n } catch {\n // Keep the skill visible even if only the directory is readable.\n }\n const name = entry.name\n return { name, description, sourcePath }\n }),\n )\n return skills\n } catch {\n return []\n }\n}\n\nasync function readCommandFiles(commandsDir: string): Promise<Array<{ name: string; relativePath: string; filePath: string; description: string }>> {\n try {\n const stat = await fs.stat(commandsDir)\n if (!stat.isDirectory()) return []\n } catch {\n return []\n }\n return readCommandFilesRecursive(commandsDir, commandsDir)\n}\n\nasync function readCommandFilesRecursive(\n root: string,\n dir: string,\n): Promise<Array<{ name: string; relativePath: string; filePath: string; description: string }>> {\n let entries\n try {\n entries = await fs.readdir(dir, { withFileTypes: true })\n } catch {\n return []\n }\n\n const out: Array<{ name: string; relativePath: string; filePath: string; description: string }> = []\n for (const entry of entries) {\n const filePath = path.join(dir, entry.name)\n if (entry.isDirectory()) {\n out.push(...await readCommandFilesRecursive(root, filePath))\n continue\n }\n if (!entry.isFile() || !entry.name.endsWith(\".md\")) continue\n const relativePath = normalizeRelative(path.relative(root, filePath))\n const name = relativePath.replace(/\\.md$/i, \"\")\n let description = \"\"\n try {\n const md = await fs.readFile(filePath, \"utf8\")\n description = parseFrontmatterField(md, \"description\") ?? firstMarkdownLine(md)\n } catch {\n // A listed file that disappears mid-read should not break the UI.\n }\n out.push({ name, relativePath, filePath, description })\n }\n return out\n}\n\nfunction parseFrontmatterField(markdown: string, field: string): string | null {\n if (!markdown.startsWith(\"---\")) return null\n const end = markdown.indexOf(\"\\n---\", 3)\n if (end < 0) return null\n const frontmatter = markdown.slice(3, end).split(/\\r?\\n/)\n for (const line of frontmatter) {\n const match = line.match(/^([A-Za-z0-9_-]+):\\s*(.*)$/)\n if (!match || match[1] !== field) continue\n return match[2].replace(/^['\"]|['\"]$/g, \"\").trim()\n }\n return null\n}\n\nfunction firstMarkdownLine(markdown: string): string {\n const body = markdown.startsWith(\"---\")\n ? markdown.slice(markdown.indexOf(\"\\n---\", 3) + 4)\n : markdown\n for (const line of body.split(/\\r?\\n/)) {\n const trimmed = line.trim()\n if (trimmed) return trimmed.replace(/^#+\\s*/, \"\")\n }\n return \"\"\n}\n\nfunction normalizeRelative(value: string): string {\n return value.split(path.sep).join(\"/\")\n}\n\nfunction countCommands(builtIn: unknown): number {\n return Array.isArray(builtIn) ? builtIn.length : 0\n}\n","import http from \"node:http\"\nimport type { AddressInfo, Socket } from \"node:net\"\n\nimport { getPackageVersion } from \"../version\"\n\n/**\n * Loopback-only identity endpoint served directly by the reverse proxy (never\n * forwarded to CloudCLI). A second `github-router serve` probes this on the\n * intended port to detect an already-running instance (single-instance guard),\n * so it can attach/refuse instead of starting a duplicate CloudCLI. Carries no\n * secret — just a service marker + version + origin.\n */\nexport const SERVE_IDENTITY_PATH = \"/__github-router-serve__\"\nexport const SERVE_IDENTITY_SERVICE = \"github-router-serve\"\n\n/**\n * Standalone HTTP + WebSocket reverse proxy that fronts a locally-running\n * CloudCLI instance under github-router's own origin. It is deliberately NOT\n * routed through the srvx/Hono app — a separate lightweight proxy avoids\n * entangling CloudCLI's raw WebSocket upgrades (`/ws` chat, `/shell` node-pty\n * terminal) with the model-API server.\n *\n * Security responsibilities CloudCLI's OSS server does not handle itself:\n * 1. Injects `localStorage['auth-token']` into the served `index.html` so the\n * SPA boots already-authenticated (true zero-login).\n * 2. Rejects foreign `Host` (DNS-rebinding defense — an `Origin`-only check\n * still lets a rebound `attacker.com → 127.0.0.1` navigation fetch the\n * token-injected HTML) AND foreign `Origin` on requests + WS upgrades\n * (CloudCLI performs NO Origin check on `/ws` or `/shell`).\n */\nexport interface ReverseProxyOptions {\n targetHost: string\n targetPort: number\n bindHost: string\n bindPort: number\n /** JWT injected into the SPA so it boots authenticated. */\n authToken: string\n /**\n * Seed CloudCLI's `claude-settings` localStorage with `skipPermissions:false`\n * and an `allowedTools` allow-list (routine built-ins + injected `mcp__*`) so\n * routine tools auto-run with no prompt while `AskUserQuestion`/`ExitPlanMode`\n * still reach the user (serve default; disabled by GH_ROUTER_SERVE_NO_AUTO_APPROVE=1).\n */\n seedToolSettings?: boolean\n /** Exact tool names to seed into the `claude-settings` `allowedTools`\n * auto-approve list (only used when `seedToolSettings`) — the routine\n * built-ins plus every exact `mcp__<key>__<tool>` name. MUST NOT include\n * `AskUserQuestion`/`ExitPlanMode`, or they would auto-resolve instead of\n * reaching the user. */\n seedAllowedTools?: string[]\n providerFacade?: {\n kindFor: (method: string, pathname: string) => string | null\n rewrite: (\n kind: string,\n upstreamJson: unknown,\n query: URLSearchParams,\n ) => Promise<unknown | null>\n /**\n * Requests answered DIRECTLY with a canned JSON body, never forwarded to\n * CloudCLI (e.g. `POST /api/system/update`, which would run a global npm\n * install). Returns null to forward normally.\n */\n blockedRequest?: (\n method: string,\n pathname: string,\n ) => { status: number; json: unknown } | null\n }\n /**\n * Extra exact `host:port` values to accept beyond loopback (e.g. a specific\n * dev-tunnel host `abc-5454.usw2.devtunnels.ms`). Used for remote access.\n */\n extraAllowedHosts?: string[]\n /** Extra exact origins to accept (e.g. `https://abc-5454.usw2.devtunnels.ms`). */\n extraAllowedOrigins?: string[]\n /**\n * Accept ANY `*.devtunnels.ms` host + `https://*.devtunnels.ms` origin. A\n * convenience for Microsoft dev tunnels when the exact URL isn't pinned. Only\n * the user's own authenticated tunnel actually forwards to this loopback port,\n * so this does not weaken the same-user loopback trust boundary.\n */\n allowDevtunnelHosts?: boolean\n}\n\nexport interface ReverseProxyHandle {\n url: string\n port: number\n close: () => Promise<void>\n}\n\nconst HEAD_CLOSE = \"</head>\"\n\n/** CloudCLI's localStorage key for per-tool chat permission settings. */\nconst CLAUDE_SETTINGS_KEY = \"claude-settings\"\n\n/**\n * The two interaction tools CloudCLI routes to the human (its\n * `TOOLS_REQUIRING_INTERACTION`). They must NEVER appear in the seeded\n * `allowedTools` — an exact-name match there would auto-approve them and defeat\n * the question widget / plan card. Stripped defensively at the seed chokepoint\n * so no upstream list (mcp names, a future caller) can reintroduce them.\n */\nconst NEVER_AUTO_APPROVE = new Set([\"AskUserQuestion\", \"ExitPlanMode\"])\n\n/**\n * Embed the token as a JS string literal. JSON.stringify escapes quotes and\n * backslashes; we additionally neutralize `<` so the value can never break out\n * of the surrounding <script> element regardless of contents.\n *\n * When `seedToolSettings` is set, also seed CloudCLI's `claude-settings`\n * localStorage with `{allowedTools:<routine+mcp>, disallowedTools:[], skipPermissions:false}`.\n * This is serve's \"seamless routine + decisions to the user\" model: `canUseTool`\n * stays LIVE (skipPermissions:false, NOT bypass), routine tools in `allowedTools`\n * auto-run with no prompt, and `AskUserQuestion`/`ExitPlanMode` — deliberately\n * NOT in the list and hard-cased by CloudCLI into `TOOLS_REQUIRING_INTERACTION` —\n * always reach the human as a real question widget / plan Approve-Reject card.\n * `skipPermissions:true` (bypass) would skip `canUseTool` entirely and silently\n * auto-answer those two, which is exactly what we are fixing.\n */\nfunction buildInjection(\n token: string,\n seedToolSettings = false,\n seedAllowedTools: string[] = [],\n): string {\n const safe = JSON.stringify(token).replace(/</g, \"\\\\u003c\")\n let body = `localStorage.setItem('auth-token',${safe});`\n if (seedToolSettings) {\n const settings = JSON.stringify({\n // CloudCLI's per-tool AUTO-APPROVE list — feeds the Agent-SDK `canUseTool`\n // fast-path (EXACT-name match, no `mcp__<server>` wildcard) AND\n // `sdkOptions.allowedTools`. We seed the routine built-ins + every exact\n // `mcp__<key>__<tool>` name so ordinary work never prompts. The two\n // interaction tools are filtered out unconditionally (NEVER_AUTO_APPROVE)\n // so they always reach the user, no matter what the caller passed.\n allowedTools: seedAllowedTools.filter((t) => !NEVER_AUTO_APPROVE.has(t)),\n disallowedTools: [],\n // NON-bypass: keep canUseTool live so AskUserQuestion/ExitPlanMode reach\n // the user. Bypass would auto-resolve them on a generated answer.\n skipPermissions: false,\n })\n const safeSettings = JSON.stringify(settings).replace(/</g, \"\\\\u003c\")\n body += `localStorage.setItem('${CLAUDE_SETTINGS_KEY}',${safeSettings});`\n }\n return `<script>try{${body}}catch(e){}</script>`\n}\n\nfunction headerLines(headers: http.IncomingHttpHeaders): string {\n return Object.entries(headers)\n .map(([k, v]) => `${k}: ${Array.isArray(v) ? v.join(\", \") : v}`)\n .join(\"\\r\\n\")\n}\n\nexport async function startReverseProxy(\n opts: ReverseProxyOptions,\n): Promise<ReverseProxyHandle> {\n const { targetHost, targetPort, bindHost, bindPort, authToken } = opts\n const ownOrigin = `http://${bindHost}:${bindPort}`\n const injection = buildInjection(authToken, opts.seedToolSettings === true, opts.seedAllowedTools ?? [])\n\n const allowedHosts = new Set([\n `127.0.0.1:${bindPort}`,\n `localhost:${bindPort}`,\n `[::1]:${bindPort}`,\n ...(opts.extraAllowedHosts ?? []).map((h) => h.toLowerCase()),\n ])\n // Loopback-only subset — the single-instance identity probe always comes from\n // 127.0.0.1, so the identity endpoint is answered only for a loopback Host\n // (never a dev-tunnel host), keeping the version off the remote surface.\n const loopbackHosts = new Set([\n `127.0.0.1:${bindPort}`,\n `localhost:${bindPort}`,\n `[::1]:${bindPort}`,\n ])\n const allowedOrigins = new Set([\n `http://127.0.0.1:${bindPort}`,\n `http://localhost:${bindPort}`,\n `http://[::1]:${bindPort}`,\n ...(opts.extraAllowedOrigins ?? []).map((o) => o.toLowerCase()),\n ])\n // `(^|\\.)devtunnels.ms$` matches `x.devtunnels.ms` but not `evildevtunnels.ms`\n // / `evil-devtunnels.ms` (the char before must be start-of-string or a dot).\n const DEVTUNNEL_RE = /(^|\\.)devtunnels\\.ms$/i\n const isDevtunnelHost = (host: string): boolean =>\n !!opts.allowDevtunnelHosts && DEVTUNNEL_RE.test(host.replace(/:\\d+$/, \"\"))\n const isDevtunnelOrigin = (origin: string): boolean => {\n if (!opts.allowDevtunnelHosts) return false\n try {\n const u = new URL(origin)\n return u.protocol === \"https:\" && DEVTUNNEL_RE.test(u.hostname)\n } catch {\n return false\n }\n }\n // Host defends against DNS-rebinding; Origin defends against cross-origin\n // browser access. A missing Origin (plain navigation / non-browser) is only\n // allowed once the Host has already been confirmed allowed.\n const hostAllowed = (host?: string): boolean =>\n !!host\n && (allowedHosts.has(host.toLowerCase()) || isDevtunnelHost(host.toLowerCase()))\n const originAllowed = (origin?: string): boolean =>\n !origin\n || allowedOrigins.has(origin.toLowerCase())\n || isDevtunnelOrigin(origin)\n\n // Track hijacked upgrade sockets so close() can destroy them — http.Server\n // .close() otherwise waits for them forever (shutdown hang).\n const liveSockets = new Set<Socket>()\n\n const server = http.createServer((clientReq, clientRes) => {\n clientReq.on(\"error\", () => clientRes.destroy())\n clientRes.on(\"error\", () => clientReq.destroy())\n\n if (!hostAllowed(clientReq.headers.host) || !originAllowed(clientReq.headers.origin)) {\n clientRes.writeHead(403, { \"content-type\": \"text/plain\" })\n clientRes.end(\"Forbidden\")\n return\n }\n\n // Single-instance identity probe — answered directly, never proxied to\n // CloudCLI. Loopback Host only (the probe is always local); carries no\n // secret and no attacker-usable URL.\n if ((clientReq.url ?? \"\").split(\"?\")[0] === SERVE_IDENTITY_PATH) {\n if (!loopbackHosts.has(String(clientReq.headers.host ?? \"\").toLowerCase())) {\n clientRes.writeHead(404, { \"content-type\": \"text/plain\" })\n clientRes.end(\"Not found\")\n return\n }\n clientRes.writeHead(200, { \"content-type\": \"application/json\" })\n clientRes.end(\n JSON.stringify({\n service: SERVE_IDENTITY_SERVICE,\n version: getPackageVersion(),\n }),\n )\n return\n }\n\n const requestUrl = new URL(clientReq.url ?? \"/\", ownOrigin)\n\n // Requests serve answers directly (never forwarded) — e.g. CloudCLI's\n // system-update button, which would run a global `npm i -g` mutation.\n const blocked = opts.providerFacade?.blockedRequest?.(\n clientReq.method ?? \"GET\",\n requestUrl.pathname,\n )\n if (blocked) {\n clientReq.resume() // drain the request body so the socket can be reused\n clientRes.writeHead(blocked.status, { \"content-type\": \"application/json\" })\n clientRes.end(JSON.stringify(blocked.json))\n return\n }\n\n const facadeKind = opts.providerFacade?.kindFor(\n clientReq.method ?? \"GET\",\n requestUrl.pathname,\n ) ?? null\n\n // Drop accept-encoding so HTML comes back uncompressed and stays\n // injectable; façade JSON rewrites also need original bytes, not gzip/br.\n const headers = { ...clientReq.headers }\n delete headers[\"accept-encoding\"]\n\n const proxyReq = http.request(\n {\n host: targetHost,\n port: targetPort,\n path: clientReq.url,\n method: clientReq.method,\n headers,\n },\n (proxyRes) => {\n proxyRes.on(\"error\", () => clientRes.destroy())\n const contentType = String(proxyRes.headers[\"content-type\"] ?? \"\")\n if (facadeKind) {\n const chunks: Buffer[] = []\n proxyRes.on(\"data\", (d) => chunks.push(Buffer.from(d)))\n proxyRes.on(\"end\", () => {\n const original = Buffer.concat(chunks)\n const sendOriginal = () => {\n // We buffered the whole body, so send it as a fixed buffer and let\n // Node re-frame — any upstream chunked/encoded framing is now wrong.\n const outHeaders = { ...proxyRes.headers }\n delete outHeaders[\"content-length\"]\n delete outHeaders[\"content-encoding\"]\n delete outHeaders[\"transfer-encoding\"]\n clientRes.writeHead(proxyRes.statusCode ?? 502, outHeaders)\n clientRes.end(original)\n }\n if (proxyRes.statusCode !== 200) {\n sendOriginal()\n return\n }\n void (async () => {\n try {\n const parsed = JSON.parse(original.toString(\"utf8\"))\n const rewritten = await opts.providerFacade?.rewrite(\n facadeKind,\n parsed,\n requestUrl.searchParams,\n )\n if (rewritten == null) {\n sendOriginal()\n return\n }\n const outHeaders = { ...proxyRes.headers }\n // We send a fresh JSON body, so any upstream length/encoding\n // framing is now wrong — drop it and let Node re-frame.\n delete outHeaders[\"content-length\"]\n delete outHeaders[\"content-encoding\"]\n delete outHeaders[\"transfer-encoding\"]\n outHeaders[\"content-type\"] = \"application/json\"\n clientRes.writeHead(200, outHeaders)\n clientRes.end(JSON.stringify(rewritten))\n } catch {\n sendOriginal()\n }\n })()\n })\n } else if (contentType.includes(\"text/html\")) {\n const chunks: Buffer[] = []\n proxyRes.on(\"data\", (d) => chunks.push(Buffer.from(d)))\n proxyRes.on(\"end\", () => {\n let html = Buffer.concat(chunks).toString(\"utf8\")\n html = html.includes(HEAD_CLOSE)\n ? html.replace(HEAD_CLOSE, injection + HEAD_CLOSE)\n : injection + html\n const outHeaders = { ...proxyRes.headers }\n // We send a fresh fixed buffer, so any length/encoding framing the\n // upstream set is now wrong — drop it and let Node re-frame.\n delete outHeaders[\"content-length\"]\n delete outHeaders[\"content-encoding\"]\n delete outHeaders[\"transfer-encoding\"]\n clientRes.writeHead(proxyRes.statusCode ?? 502, outHeaders)\n clientRes.end(html)\n })\n } else {\n clientRes.writeHead(proxyRes.statusCode ?? 502, proxyRes.headers)\n proxyRes.pipe(clientRes)\n }\n },\n )\n proxyReq.on(\"error\", (err) => {\n if (!clientRes.headersSent)\n clientRes.writeHead(502, { \"content-type\": \"text/plain\" })\n clientRes.end(`Bad gateway: ${err.message}`)\n })\n clientReq.pipe(proxyReq)\n })\n\n server.on(\"upgrade\", (clientReq, clientSock: Socket, clientHead) => {\n // Attach error handling + track the socket IMMEDIATELY, before any async\n // window (a client reset during the upstream handshake must not crash us).\n clientSock.on(\"error\", () => clientSock.destroy())\n liveSockets.add(clientSock)\n clientSock.once(\"close\", () => liveSockets.delete(clientSock))\n\n if (!hostAllowed(clientReq.headers.host) || !originAllowed(clientReq.headers.origin)) {\n clientSock.destroy()\n return\n }\n\n const proxyReq = http.request({\n host: targetHost,\n port: targetPort,\n path: clientReq.url,\n method: clientReq.method,\n headers: clientReq.headers,\n })\n proxyReq.on(\"upgrade\", (proxyRes, proxySock, proxyHead) => {\n clientSock.write(\n `HTTP/1.1 ${proxyRes.statusCode} ${proxyRes.statusMessage}\\r\\n${headerLines(\n proxyRes.headers,\n )}\\r\\n\\r\\n`,\n )\n if (clientHead && clientHead.length) proxySock.write(clientHead)\n if (proxyHead && proxyHead.length) proxySock.unshift(proxyHead)\n proxySock.pipe(clientSock)\n clientSock.pipe(proxySock)\n const cleanup = () => {\n proxySock.destroy()\n clientSock.destroy()\n }\n // Tear down on BOTH error and normal close, on either side (else the\n // paired socket leaks on every page refresh / terminal close).\n clientSock.on(\"error\", cleanup)\n proxySock.on(\"error\", cleanup)\n clientSock.on(\"close\", cleanup)\n proxySock.on(\"close\", cleanup)\n })\n // CloudCLI answered the upgrade with an ordinary response (e.g. 401/404 for\n // a missing/invalid JWT). Relay it and close so the browser WS doesn't hang.\n proxyReq.on(\"response\", (proxyRes) => {\n try {\n clientSock.write(\n `HTTP/1.1 ${proxyRes.statusCode} ${proxyRes.statusMessage}\\r\\n${headerLines(\n proxyRes.headers,\n )}\\r\\n\\r\\n`,\n )\n } catch {\n /* socket already gone */\n }\n proxyRes.on(\"data\", (d) => {\n clientSock.write(d as Buffer)\n })\n proxyRes.on(\"end\", () => clientSock.end())\n proxyRes.on(\"error\", () => clientSock.destroy())\n })\n proxyReq.on(\"error\", () => clientSock.destroy())\n proxyReq.end()\n })\n\n await new Promise<void>((resolve, reject) => {\n const onError = (err: Error) => reject(err)\n server.once(\"error\", onError)\n server.listen(bindPort, bindHost, () => {\n server.removeListener(\"error\", onError)\n resolve()\n })\n })\n\n // Defense-in-depth: refuse to run if we somehow bound a non-loopback address.\n const addr = server.address() as AddressInfo | null\n if (addr && addr.address !== \"127.0.0.1\" && addr.address !== \"::1\") {\n await new Promise<void>((r) => server.close(() => r()))\n throw new Error(\n `reverse proxy refused non-loopback bind (got ${addr.address})`,\n )\n }\n\n return {\n url: ownOrigin,\n port: bindPort,\n close: () =>\n new Promise<void>((resolve) => {\n for (const s of liveSockets) s.destroy()\n liveSockets.clear()\n const t = setTimeout(resolve, 1500) // never let shutdown hang\n server.close(() => {\n clearTimeout(t)\n resolve()\n })\n }),\n }\n}\n\n/** Exported for unit tests. */\nexport const __test = { buildInjection, HEAD_CLOSE }\n","import { spawn } from \"node:child_process\"\nimport http from \"node:http\"\nimport type { AddressInfo } from \"node:net\"\n\nimport { defineCommand } from \"citty\"\nimport consola from \"consola\"\n\nimport { provisionBrowserAssets } from \"./lib/browser-mcp/provision\"\nimport { provisionAndIndexColbert } from \"./lib/colbert\"\nimport { resolveCodexCliBackend } from \"./lib/codex-mcp-config\"\nimport { killChildProcessTree } from \"./lib/exec\"\nimport { startKeepAwake, stopKeepAwake } from \"./lib/keep-awake\"\nimport { getCodexVersion } from \"./lib/launch\"\nimport { browserToolsEnabled } from \"./lib/mcp-capabilities\"\nimport { SEAMLESS_BUILTIN_TOOLS } from \"./lib/mcp-permissions-settings\"\nimport { ensureClaudeConfigMirror, PATHS, removeOwnClaudeConfigMirror } from \"./lib/paths\"\nimport { DEFAULT_CLAUDE_MODEL_FALLBACKS, pickClaudeDefault } from \"./lib/port\"\nimport {\n getClaudeCodeEnvVars,\n parseSharedArgs,\n setupAndServe,\n sharedServerArgs,\n} from \"./lib/server-setup\"\nimport {\n composeCloudCliChildEnv,\n mintJwt,\n resolveCloudCli,\n spawnCloudCli,\n waitForCloudCliReady,\n} from \"./lib/serve/cloudcli\"\nimport { DevtunnelError, startDevtunnel } from \"./lib/serve/devtunnel\"\nimport { provisionServeEnhancements } from \"./lib/serve/enhancements\"\nimport { facadeBlockedRequest, facadeInterceptKind, rewriteProviderResponse } from \"./lib/serve/provider-facade\"\nimport {\n SERVE_IDENTITY_PATH,\n SERVE_IDENTITY_SERVICE,\n startReverseProxy,\n} from \"./lib/serve/reverse-proxy\"\nimport { runSelfUpdate } from \"./lib/self-update\"\nimport { state } from \"./lib/state\"\nimport { provisionToolbelt } from \"./lib/toolbelt/provision\"\nimport { resolveModel } from \"./lib/utils\"\nimport { getGitHubUser } from \"./services/github/get-user\"\n\nasync function getFreePort(): Promise<number> {\n return new Promise((resolve, reject) => {\n const s = http.createServer()\n s.once(\"error\", reject)\n s.listen(0, \"127.0.0.1\", () => {\n const port = (s.address() as AddressInfo).port\n s.close(() => resolve(port))\n })\n })\n}\n\n/** Default user-facing control-plane port when `--port` is not given. */\nconst DEFAULT_SERVE_PORT = 5454\n\nfunction isPortFree(port: number): Promise<boolean> {\n return new Promise((resolve) => {\n const s = http.createServer()\n s.once(\"error\", () => resolve(false))\n s.listen(port, \"127.0.0.1\", () => s.close(() => resolve(true)))\n })\n}\n\n/**\n * Probe a loopback port for an already-running `github-router serve` (its\n * identity endpoint). Returns true when the port hosts OUR serve, else false\n * (free, or a foreign process). `serve` is designed to run ONCE per machine\n * (a single machine-wide control plane for all repos), so a second launch\n * attaches to the running one instead of spawning a duplicate CloudCLI +\n * contending on the shared auth DB. We intentionally do NOT trust any URL the\n * probed service reports — a local squatter could forge the identity marker, so\n * the caller only ever opens a locally-constructed loopback origin.\n */\nfunction probeExistingServe(port: number): Promise<boolean> {\n return new Promise((resolve) => {\n const req = http.get(\n {\n host: \"127.0.0.1\",\n port,\n path: SERVE_IDENTITY_PATH,\n headers: { host: `127.0.0.1:${port}` },\n timeout: 1500,\n },\n (res) => {\n if (res.statusCode !== 200) {\n res.resume()\n resolve(false)\n return\n }\n const chunks: Buffer[] = []\n res.on(\"data\", (d) => chunks.push(Buffer.from(d)))\n res.on(\"end\", () => {\n try {\n const body = JSON.parse(Buffer.concat(chunks).toString(\"utf8\")) as {\n service?: string\n }\n resolve(body.service === SERVE_IDENTITY_SERVICE)\n } catch {\n resolve(false)\n }\n })\n res.on(\"error\", () => resolve(false))\n },\n )\n req.on(\"timeout\", () => req.destroy())\n req.on(\"error\", () => resolve(false))\n })\n}\n\n/**\n * Resolve the user-facing port: honor an explicit `--port`; otherwise use 5454\n * when free, else fall back to a random free port. (Single-instance detection —\n * an already-running github-router serve on the intended port — is handled by\n * the earlier `probeExistingServe` attach/refuse in `run`.)\n */\nasync function resolveServePort(requested?: number): Promise<number> {\n if (requested != null) return requested\n if (await isPortFree(DEFAULT_SERVE_PORT)) return DEFAULT_SERVE_PORT\n const fallback = await getFreePort()\n consola.info(`Port ${DEFAULT_SERVE_PORT} is in use; using ${fallback} instead.`)\n return fallback\n}\n\nfunction openBrowser(url: string): void {\n try {\n if (process.platform === \"win32\") {\n spawn(\"cmd\", [\"/c\", \"start\", \"\", url], {\n detached: true,\n stdio: \"ignore\",\n windowsHide: true,\n }).unref()\n } else if (process.platform === \"darwin\") {\n spawn(\"open\", [url], { detached: true, stdio: \"ignore\" }).unref()\n } else {\n spawn(\"xdg-open\", [url], { detached: true, stdio: \"ignore\" }).unref()\n }\n } catch {\n // best-effort; the URL is printed too\n }\n}\n\nexport const serve = defineCommand({\n meta: {\n name: \"serve\",\n description:\n \"Serve a browser control plane (CloudCLI) with Claude routed through the proxy\",\n },\n args: {\n ...sharedServerArgs,\n model: {\n alias: \"m\",\n type: \"string\",\n description: \"Override the default Claude model\",\n },\n \"cloudcli-path\": {\n type: \"string\",\n description: \"Use an existing CloudCLI install instead of the managed one\",\n },\n \"cloudcli-version\": {\n type: \"string\",\n description: \"Override the pinned CloudCLI version to install\",\n },\n \"codex-cli\": {\n type: \"boolean\",\n default: false,\n description: \"Route Codex personas through a local `codex mcp-server` (requires codex 0.129+; falls back to HTTP if missing)\",\n },\n \"browse-over-tunnel\": {\n type: \"boolean\",\n default: false,\n description: \"Expose the server-side browser MCP even when tunnel-exposed (session-hijack / SSRF risk — only with fully-trusted tunnel access)\",\n },\n \"agents-over-tunnel\": {\n type: \"boolean\",\n default: false,\n description: \"Expose first-mate (mints a repo+workflow GitHub write token) even when tunnel-exposed (only with fully-trusted tunnel access)\",\n },\n \"fleet-over-tunnel\": {\n type: \"boolean\",\n default: false,\n description: \"Expose the fleet MCP (drives remote coding sessions with your fleet credentials) even when tunnel-exposed (only with fully-trusted tunnel access)\",\n },\n \"no-install\": {\n type: \"boolean\",\n default: false,\n description: \"Do not auto-install CloudCLI; require an existing install\",\n },\n \"no-open\": {\n type: \"boolean\",\n default: false,\n description: \"Do not open the browser automatically\",\n },\n \"public-url\": {\n type: \"string\",\n description:\n \"Public URL(s) this is reachable at (e.g. a dev-tunnel https URL), comma-separated — allowlists their host+origin for remote access\",\n },\n tunnel: {\n type: \"boolean\",\n default: false,\n description:\n \"Auto-create + host an AUTHENTICATED (never anonymous) Microsoft dev tunnel for the serve port and print its public URL, so you can reach the control plane from anywhere. Requires the `devtunnel` CLI + `devtunnel user login`.\",\n },\n },\n async run({ args }) {\n state.serveMode = true\n\n if (process.versions.bun) {\n consola.warn(\n \"`serve` should run under Node.js. Bun's node:http upgrade sockets cannot relay the CloudCLI terminal/chat WebSockets; run the installed `github-router` binary (Node).\",\n )\n }\n\n const parsed = parseSharedArgs(args as unknown as Record<string, unknown>)\n\n // Single-instance guard: `serve` is a machine-wide control plane meant to run\n // ONCE per machine (work on any repo through its file explorer / sessions). If\n // one is already running on the intended port, attach to it (open + exit)\n // instead of spawning a second CloudCLI that would contend on the shared auth\n // DB. Only triggers for OUR serve; a foreign process on the port falls through\n // to the random-port fallback in resolveServePort.\n const intendedPort = parsed.port ?? DEFAULT_SERVE_PORT\n if (await probeExistingServe(intendedPort)) {\n // Construct the URL locally — never trust a URL the probed service reports\n // (a local squatter could forge the identity marker into an open-browser).\n const existingUrl = `http://127.0.0.1:${intendedPort}`\n consola.box(\n `github-router control plane already running\\n\\n${existingUrl}\\n\\nOpening it — stop that instance first if you want a fresh one.`,\n )\n if (args[\"no-open\"] !== true) openBrowser(existingUrl)\n return\n }\n\n // 1. github-router proxy — internal (random port). The USER-facing port is\n // the reverse proxy below, so force this one random regardless of --port.\n const { server, serverUrl } = await setupAndServe({\n ...parsed,\n port: undefined,\n silent: true,\n })\n\n // 2. provision the router-owned CLAUDE_CONFIG_DIR (synthetic creds +\n // onboarding-skip) so CloudCLI's SDK-spawned claude authenticates\n // through the proxy. Hard-fail on error (mirrors src/claude.ts).\n try {\n await ensureClaudeConfigMirror()\n } catch (err) {\n consola.error(\n `Failed to provision CLAUDE_CONFIG_DIR mirror: ${\n err instanceof Error ? err.message : String(err)\n }`,\n )\n process.exit(1)\n }\n\n // Best-effort self-update (detached, applies next launch). Runs after the\n // server is listening so the bounded probe can't delay it.\n void runSelfUpdate({ selfUpdate: args[\"self-update\"] !== false })\n\n // Best-effort ColBERT semantic-search provision. ON by default; never blocks\n // launch. `skipCwdIndex`: serve is machine-wide, so the launch cwd is usually\n // NOT a repo the user works on — per-workspace on-demand indexing (kicked by\n // the first search for a given repo) covers real queries instead.\n void provisionAndIndexColbert({ skipCwdIndex: true })\n\n // Best-effort LLM toolbelt materialization. The mirror awareness line is\n // written by provisionServeEnhancements below, matching `github-router claude`.\n void provisionToolbelt().catch((err) =>\n consola.debug(\"Toolbelt provisioning failed:\", err),\n )\n\n // Best-effort: keep the machine awake while the proxy/CloudCLI session runs.\n // Released via stopKeepAwake() in the shutdown chain below and by the module's\n // own self-registered signal/exit reaper.\n startKeepAwake()\n\n // Best-effort: materialize the browser extension + bridge into the stable\n // app-dir when browser tools are enabled.\n if (browserToolsEnabled()) {\n void provisionBrowserAssets().catch((err) =>\n consola.debug(\"Browser extension provisioning failed:\", err),\n )\n }\n\n const opusFamilyShorthand = (args.model as string | undefined)?.match(/^(\\d+\\.\\d+)$/)?.[1]\n let chosenSlug = opusFamilyShorthand\n ? pickClaudeDefault(opusFamilyShorthand)\n : ((args.model as string | undefined) ?? pickClaudeDefault())\n if (!args.model && state.models) {\n const inCache = (slug: string): boolean =>\n state.models?.data.some((m) => m.id === resolveModel(slug)) ?? false\n if (!inCache(chosenSlug)) {\n for (const fb of DEFAULT_CLAUDE_MODEL_FALLBACKS) {\n if (inCache(fb)) {\n consola.info(\n `Default model \"${chosenSlug}\" not in your Copilot model list; falling back to \"${fb}\".`,\n )\n chosenSlug = fb\n break\n }\n }\n }\n }\n\n const requestedCli = args[\"codex-cli\"] === true\n const backend = resolveCodexCliBackend({\n requested: requestedCli,\n codexInfo: requestedCli ? getCodexVersion() : null,\n })\n const tunnelExposed =\n args.tunnel === true\n || String(args[\"public-url\"] ?? \"\")\n .split(\",\")\n .map((s) => s.trim())\n .filter(Boolean).length > 0\n if (tunnelExposed && browserToolsEnabled() && args[\"browse-over-tunnel\"] !== true) {\n consola.info(\n \"Browser MCP is disabled over the tunnel (session-hijack/SSRF risk). Pass --browse-over-tunnel to enable it for fully-trusted access.\",\n )\n }\n if (tunnelExposed && state.agentsEnabled && args[\"agents-over-tunnel\"] !== true) {\n consola.info(\n \"first-mate is disabled over the tunnel (it mints a GitHub write token). Pass --agents-over-tunnel to enable it for fully-trusted access.\",\n )\n }\n if (tunnelExposed && state.fleetEnabled && args[\"fleet-over-tunnel\"] !== true) {\n consola.info(\n \"Fleet MCP is disabled over the tunnel (it drives remote coding sessions with your fleet credentials). Pass --fleet-over-tunnel to enable it for fully-trusted access.\",\n )\n }\n\n // 2b. wire the github-router enhancement layer (MCP servers + peer/worker\n // subagents + gh-* skills) into the mirror so CloudCLI-spawned Claude\n // sessions get the same tools `github-router claude` provides. Must run\n // after the mirror exists and before CloudCLI spawns claude.\n const enhancements = await provisionServeEnhancements(serverUrl, {\n codexCli: backend === \"cli\",\n tunnelExposed,\n browseOverTunnel: args[\"browse-over-tunnel\"] === true,\n agentsOverTunnel: args[\"agents-over-tunnel\"] === true,\n fleetOverTunnel: args[\"fleet-over-tunnel\"] === true,\n })\n\n // 3. filtered child env: the vetted secret-stripping allowlist (buildEnv,\n // drops GITHUB_TOKEN/GH_ROUTER_*/ANTHROPIC_AUTH_TOKEN/OPENAI_API_KEY/\n // COPILOT_TOKEN) plus the non-secret ANTHROPIC_BASE_URL/CLAUDE_CONFIG_DIR\n // /model vars. Auth rides the synthetic .credentials.json FILE, not env.\n const anthropicVars = getClaudeCodeEnvVars(serverUrl, chosenSlug)\n // CLAUDE_CODE_FORK_SUBAGENT silently no-ops under CloudCLI's headless\n // `claude --print` path (the binary's Z8() precondition disables forking\n // without an interactive session), so setting it here is dead weight that\n // would mislead the model into assuming forked subagents inherit full\n // context. Drop it for serve. (getClaudeCodeEnvVars sets it for the\n // interactive `claude` path, where forking works.)\n delete anthropicVars.CLAUDE_CODE_FORK_SUBAGENT\n if (enhancements.nonce) {\n anthropicVars.GH_ROUTER_HOOK_MCP_URL = serverUrl\n anthropicVars.GH_ROUTER_HOOK_NONCE = enhancements.nonce\n }\n const childEnv: NodeJS.ProcessEnv = composeCloudCliChildEnv(anthropicVars)\n // Placeholder only: CloudCLI's Claude provider checks this env var to mark\n // Claude as connected. Claude still authenticates through the proxy mirror.\n childEnv.ANTHROPIC_API_KEY = \"sk-ant-github-router-proxy-placeholder\"\n // CloudCLI's cloned server defines CLAUDE_FALLBACK_MODELS as an in-code object,\n // and the searched server tree has no process.env.CLAUDE_FALLBACK_MODELS reader.\n // Do not set an env var here unless CloudCLI adds a documented string format.\n\n // 4. resolve/install + spawn CloudCLI on a loopback port.\n let cloudcli\n try {\n cloudcli = await resolveCloudCli({\n cliPath: args[\"cloudcli-path\"] as string | undefined,\n noInstall: args[\"no-install\"] === true,\n version: args[\"cloudcli-version\"] as string | undefined,\n })\n } catch (err) {\n consola.error(err instanceof Error ? err.message : String(err))\n process.exit(1)\n }\n\n const ccPort = await getFreePort()\n const cc = spawnCloudCli({\n serverEntry: cloudcli.serverEntry,\n port: ccPort,\n env: childEnv,\n })\n\n // 5. shutdown wiring (we don't use launchChild — its kind union is closed).\n let shuttingDown = false\n let reverseClose: (() => Promise<void>) | null = null\n let tunnelStop: (() => void) | null = null\n const shutdown = async (): Promise<void> => {\n if (shuttingDown) return\n shuttingDown = true\n if (tunnelStop) {\n try {\n tunnelStop()\n } catch {\n /* best-effort */\n }\n }\n try {\n killChildProcessTree(cc.child, {\n detachedGroup: process.platform !== \"win32\",\n })\n } catch {\n /* best-effort */\n }\n if (reverseClose) await reverseClose().catch(() => {})\n try {\n ;(server as unknown as { close?: () => void }).close?.()\n } catch {\n /* best-effort */\n }\n await stopKeepAwake().catch(() => {})\n await enhancements.cleanup().catch(() => {})\n await removeOwnClaudeConfigMirror().catch(() => {})\n }\n // `shutdown()` must never strand the process. `.then(exit)` alone skips the\n // exit on a REJECTION, so a teardown failure leaves a process that was\n // explicitly trying to die running forever — the worst possible outcome on\n // a shutdown path. `.finally` guarantees the exit either way, and the code\n // is preserved because an operator's script reads it.\n const exitAfterShutdown = (code: number) => {\n void shutdown()\n .catch((err: unknown) => {\n consola.error(\"Shutdown failed; exiting anyway:\", err)\n })\n .finally(() => process.exit(code))\n }\n process.once(\"SIGINT\", () => exitAfterShutdown(0))\n process.once(\"SIGTERM\", () => exitAfterShutdown(0))\n cc.child.once(\"exit\", (code) => {\n if (!shuttingDown) {\n consola.error(`CloudCLI exited unexpectedly (code ${code}).`)\n exitAfterShutdown(1)\n }\n })\n cc.child.once(\"error\", (err) => {\n // spawn failure (ENOENT/EACCES) emits 'error', not 'exit' — an unhandled\n // 'error' crashes the process and bypasses shutdown, orphaning the mirror.\n if (!shuttingDown) {\n consola.error(`CloudCLI failed to start: ${err.message}`)\n exitAfterShutdown(1)\n }\n })\n\n // 6. wait for CloudCLI, seed the user from the GitHub identity, mint a JWT.\n let ready\n try {\n ready = await waitForCloudCliReady(ccPort)\n } catch (err) {\n consola.error(err instanceof Error ? err.message : String(err))\n await shutdown()\n process.exit(1)\n }\n\n let username = \"github-router\"\n try {\n username = (await getGitHubUser()).login\n } catch {\n // fall back to a generic control-plane user; not fatal\n }\n\n let token: string\n try {\n token = await mintJwt(ccPort, username, ready.needsSetup)\n } catch (err) {\n consola.error(err instanceof Error ? err.message : String(err))\n await shutdown()\n process.exit(1)\n }\n\n // 7. resolve remote-access allowlist (dev tunnels) + the user-facing port.\n const publicUrls = String(args[\"public-url\"] ?? \"\")\n .split(\",\")\n .map((s) => s.trim())\n .filter(Boolean)\n const extraAllowedHosts: string[] = []\n const extraAllowedOrigins: string[] = []\n for (const u of publicUrls) {\n try {\n const url = new URL(u)\n extraAllowedHosts.push(url.host)\n extraAllowedOrigins.push(url.origin)\n } catch {\n consola.warn(`Ignoring invalid --public-url \"${u}\"`)\n }\n }\n const tunnelMode = args.tunnel === true\n if (extraAllowedHosts.length || tunnelMode) {\n consola.warn(\n \"Remote access enabled: anyone who reaches this through the tunnel AND passes its authentication gets FULL control-plane access — including a shell on this machine. The tunnel is authenticated (never anonymous); only grant access to people you fully trust.\",\n )\n }\n\n const servePort = await resolveServePort(parsed.port)\n const providerFacade = {\n kindFor: facadeInterceptKind,\n blockedRequest: facadeBlockedRequest,\n rewrite: (kind: string, json: unknown, query: URLSearchParams) =>\n rewriteProviderResponse(\n kind as Parameters<typeof rewriteProviderResponse>[0],\n json,\n {\n getModels: () => state.models,\n // Resolve to the Copilot catalog slug so it matches the picker\n // OPTIONS values (chosenSlug may be a dashed/`[1m]`-bracketed Anthropic\n // slug; the OPTIONS values are catalog ids). Otherwise DEFAULT misses\n // and falls back to the first option.\n defaultModel: resolveModel(chosenSlug),\n claudeConfigDir: PATHS.CLAUDE_CONFIG_DIR,\n },\n query,\n ),\n }\n let reverse\n try {\n reverse = await startReverseProxy({\n targetHost: \"127.0.0.1\",\n targetPort: ccPort,\n bindHost: \"127.0.0.1\",\n bindPort: servePort,\n authToken: token,\n seedToolSettings: process.env.GH_ROUTER_SERVE_NO_AUTO_APPROVE !== \"1\",\n // Routine built-ins + every injected mcp tool name → auto-approve; the\n // two interaction tools (AskUserQuestion/ExitPlanMode) are excluded so\n // they reach the user (SEAMLESS_BUILTIN_TOOLS never lists them).\n seedAllowedTools: [...SEAMLESS_BUILTIN_TOOLS, ...(enhancements.mcpToolNames ?? [])],\n extraAllowedHosts,\n extraAllowedOrigins,\n allowDevtunnelHosts: tunnelMode,\n providerFacade,\n })\n } catch (err) {\n consola.error(\n `Failed to bind the control-plane port ${servePort}: ${\n err instanceof Error ? err.message : String(err)\n }. Is it already in use? Pass --port <n>.`,\n )\n await shutdown()\n process.exit(1)\n }\n reverseClose = reverse.close\n\n // 8. optionally auto-create + host an AUTHENTICATED dev tunnel for remote\n // access. Non-fatal: on any failure keep serving locally (and the\n // *.devtunnels.ms allowlist stays, so a manual `devtunnel host` works).\n let tunnelUrl: string | null = null\n if (tunnelMode) {\n consola.info(\"Creating an authenticated dev tunnel…\")\n try {\n const tunnel = await startDevtunnel(servePort)\n tunnelUrl = tunnel.url\n tunnelStop = tunnel.stop\n } catch (err) {\n const msg = err instanceof DevtunnelError ? err.message : String(err)\n consola.warn(\n `Could not auto-host a dev tunnel: ${msg}\\nServing locally only. To host manually once fixed: devtunnel host -p ${servePort}`,\n )\n }\n }\n\n // 9. ready.\n const publicLines = [\n ...publicUrls.map((u) => `remote: ${u}`),\n ...(tunnelUrl ? [`remote (tunnel): ${tunnelUrl} (stable — bookmark it; same URL every launch)`] : []),\n ]\n const publicBlock = publicLines.length ? `\\n${publicLines.join(\"\\n\")}` : \"\"\n consola.box(\n `🖥️ github-router control plane\\n\\nlocal: ${reverse.url}${publicBlock}\\n\\nsigned in as ${username} · model ${chosenSlug}\\nCtrl+C to stop`,\n )\n if (args[\"no-open\"] !== true) openBrowser(reverse.url)\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAaA,MAAa,mBAAmB;;;;;;;;;;AAahC,SAAgB,wBACd,eACmB;CACnB,OAAO;EAAE,GAAG,SAAS;EAAG,GAAG;CAAc;AAC3C;AAEA,MAAM,mBAAmB,KAAK,KAC5B,eACA,UACA,UACF;;AASA,SAAS,qBAAqB,MAAsB;CAClD,OAAO,KAAK,KAAK,MAAM,gBAAgB,kBAAkB,gBAAgB;AAC3E;AAEA,eAAe,OAAO,GAA6B;CACjD,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