open-multi-agent-kit 0.90.4 → 0.90.6

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 (161) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/README.md +9 -9
  3. package/dist/cli/args.d.ts.map +1 -1
  4. package/dist/cli/args.js +1 -0
  5. package/dist/cli/args.js.map +1 -1
  6. package/dist/codexbar-cli.d.ts +39 -0
  7. package/dist/codexbar-cli.d.ts.map +1 -0
  8. package/dist/codexbar-cli.js +230 -0
  9. package/dist/codexbar-cli.js.map +1 -0
  10. package/dist/commands/doctor-provider.d.ts.map +1 -1
  11. package/dist/commands/doctor-provider.js +16 -3
  12. package/dist/commands/doctor-provider.js.map +1 -1
  13. package/dist/core/adaptorch-bridge.d.ts +14 -21
  14. package/dist/core/adaptorch-bridge.d.ts.map +1 -1
  15. package/dist/core/adaptorch-bridge.js +11 -18
  16. package/dist/core/adaptorch-bridge.js.map +1 -1
  17. package/dist/core/agent-session.d.ts +6 -39
  18. package/dist/core/agent-session.d.ts.map +1 -1
  19. package/dist/core/agent-session.js +37 -107
  20. package/dist/core/agent-session.js.map +1 -1
  21. package/dist/core/codexbar-adapter.d.ts +44 -0
  22. package/dist/core/codexbar-adapter.d.ts.map +1 -0
  23. package/dist/core/codexbar-adapter.js +211 -0
  24. package/dist/core/codexbar-adapter.js.map +1 -0
  25. package/dist/core/compaction/index.d.ts +2 -0
  26. package/dist/core/compaction/index.d.ts.map +1 -1
  27. package/dist/core/compaction/index.js +2 -0
  28. package/dist/core/compaction/index.js.map +1 -1
  29. package/dist/core/compaction/model-policy.d.ts +9 -0
  30. package/dist/core/compaction/model-policy.d.ts.map +1 -0
  31. package/dist/core/compaction/model-policy.js +37 -0
  32. package/dist/core/compaction/model-policy.js.map +1 -0
  33. package/dist/core/compaction/resume-policy.d.ts +6 -0
  34. package/dist/core/compaction/resume-policy.d.ts.map +1 -0
  35. package/dist/core/compaction/resume-policy.js +8 -0
  36. package/dist/core/compaction/resume-policy.js.map +1 -0
  37. package/dist/core/context-budget-v2-planner.d.ts.map +1 -1
  38. package/dist/core/context-budget-v2-planner.js +10 -2
  39. package/dist/core/context-budget-v2-planner.js.map +1 -1
  40. package/dist/core/context-budget-v2-types.d.ts +7 -1
  41. package/dist/core/context-budget-v2-types.d.ts.map +1 -1
  42. package/dist/core/context-budget-v2-types.js.map +1 -1
  43. package/dist/core/domain-loadouts.d.ts.map +1 -1
  44. package/dist/core/domain-loadouts.js +68 -1
  45. package/dist/core/domain-loadouts.js.map +1 -1
  46. package/dist/core/extensions/loader.d.ts.map +1 -1
  47. package/dist/core/extensions/loader.js +12 -0
  48. package/dist/core/extensions/loader.js.map +1 -1
  49. package/dist/core/extensions/runner.d.ts.map +1 -1
  50. package/dist/core/extensions/runner.js +3 -0
  51. package/dist/core/extensions/runner.js.map +1 -1
  52. package/dist/core/extensions/types.d.ts +15 -0
  53. package/dist/core/extensions/types.d.ts.map +1 -1
  54. package/dist/core/extensions/types.js.map +1 -1
  55. package/dist/core/grok-harness-dispatch.d.ts +22 -0
  56. package/dist/core/grok-harness-dispatch.d.ts.map +1 -0
  57. package/dist/core/grok-harness-dispatch.js +44 -0
  58. package/dist/core/grok-harness-dispatch.js.map +1 -0
  59. package/dist/core/grok-harness.d.ts +25 -0
  60. package/dist/core/grok-harness.d.ts.map +1 -0
  61. package/dist/core/grok-harness.js +61 -0
  62. package/dist/core/grok-harness.js.map +1 -0
  63. package/dist/core/grok-playbook.d.ts +7 -0
  64. package/dist/core/grok-playbook.d.ts.map +1 -0
  65. package/dist/core/grok-playbook.js +34 -0
  66. package/dist/core/grok-playbook.js.map +1 -0
  67. package/dist/core/reasoning-router-bias.d.ts +11 -23
  68. package/dist/core/reasoning-router-bias.d.ts.map +1 -1
  69. package/dist/core/reasoning-router-bias.js +9 -18
  70. package/dist/core/reasoning-router-bias.js.map +1 -1
  71. package/dist/core/reasoning-router-resolver.d.ts +44 -0
  72. package/dist/core/reasoning-router-resolver.d.ts.map +1 -0
  73. package/dist/core/reasoning-router-resolver.js +79 -0
  74. package/dist/core/reasoning-router-resolver.js.map +1 -0
  75. package/dist/core/reasoning-router-v4-weights.d.ts +28 -72
  76. package/dist/core/reasoning-router-v4-weights.d.ts.map +1 -1
  77. package/dist/core/reasoning-router-v4-weights.js +16 -60
  78. package/dist/core/reasoning-router-v4-weights.js.map +1 -1
  79. package/dist/core/reasoning-router-v4.d.ts +25 -70
  80. package/dist/core/reasoning-router-v4.d.ts.map +1 -1
  81. package/dist/core/reasoning-router-v4.js +46 -144
  82. package/dist/core/reasoning-router-v4.js.map +1 -1
  83. package/dist/core/router-feedback-collector.d.ts +13 -16
  84. package/dist/core/router-feedback-collector.d.ts.map +1 -1
  85. package/dist/core/router-feedback-collector.js +5 -8
  86. package/dist/core/router-feedback-collector.js.map +1 -1
  87. package/dist/core/sdk.d.ts.map +1 -1
  88. package/dist/core/sdk.js +31 -12
  89. package/dist/core/sdk.js.map +1 -1
  90. package/dist/core/slash-commands.d.ts.map +1 -1
  91. package/dist/core/slash-commands.js +1 -1
  92. package/dist/core/slash-commands.js.map +1 -1
  93. package/dist/main.d.ts.map +1 -1
  94. package/dist/main.js +4 -0
  95. package/dist/main.js.map +1 -1
  96. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  97. package/dist/modes/interactive/interactive-mode.js +16 -98
  98. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  99. package/dist/utils/tools-manager.d.ts +3 -2
  100. package/dist/utils/tools-manager.d.ts.map +1 -1
  101. package/dist/utils/tools-manager.js +42 -17
  102. package/dist/utils/tools-manager.js.map +1 -1
  103. package/docs/correctness-wall.md +117 -0
  104. package/docs/grok-harness.md +63 -0
  105. package/docs/loadout-domains/README.md +2 -1
  106. package/docs/loadout-domains/ai-agent-ops.md +8 -1
  107. package/docs/loadout-domains/grok-harness.md +74 -0
  108. package/docs/providers.md +4 -0
  109. package/docs/usage.md +11 -12
  110. package/examples/extensions/correctness-wall/LOADOUT.md +87 -0
  111. package/examples/extensions/correctness-wall/README.md +111 -0
  112. package/examples/extensions/correctness-wall/adjudication-fixture.ts +151 -0
  113. package/examples/extensions/correctness-wall/index.ts +470 -0
  114. package/examples/extensions/correctness-wall/repair-state.ts +77 -0
  115. package/examples/extensions/correctness-wall/wall-cache.ts +81 -0
  116. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  117. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  118. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  119. package/examples/extensions/gondolin/package-lock.json +2 -2
  120. package/examples/extensions/gondolin/package.json +1 -1
  121. package/examples/extensions/sandbox/package-lock.json +2 -2
  122. package/examples/extensions/sandbox/package.json +1 -1
  123. package/examples/extensions/subagent/agent-capability-router.test.ts +123 -0
  124. package/examples/extensions/subagent/agent-capability-router.ts +274 -0
  125. package/examples/extensions/subagent/agents/planner.md +3 -0
  126. package/examples/extensions/subagent/agents/reviewer.md +3 -0
  127. package/examples/extensions/subagent/agents/scout.md +3 -0
  128. package/examples/extensions/subagent/agents/worker.md +2 -0
  129. package/examples/extensions/subagent/agents.ts +14 -0
  130. package/examples/extensions/subagent/capabilities.ts +368 -0
  131. package/examples/extensions/subagent/domain-profiles.ts +1089 -0
  132. package/examples/extensions/subagent/index.ts +71 -3
  133. package/examples/extensions/with-deps/package-lock.json +2 -2
  134. package/examples/extensions/with-deps/package.json +1 -1
  135. package/examples/sdk/12-full-control.ts +1 -1
  136. package/npm-shrinkwrap.json +12 -12
  137. package/package.json +5 -5
  138. package/dist/core/compactor.d.ts +0 -42
  139. package/dist/core/compactor.d.ts.map +0 -1
  140. package/dist/core/compactor.js +0 -128
  141. package/dist/core/compactor.js.map +0 -1
  142. package/dist/core/reasoning-router-v2.d.ts +0 -94
  143. package/dist/core/reasoning-router-v2.d.ts.map +0 -1
  144. package/dist/core/reasoning-router-v2.js +0 -288
  145. package/dist/core/reasoning-router-v2.js.map +0 -1
  146. package/dist/core/reasoning-router-v3.d.ts +0 -15
  147. package/dist/core/reasoning-router-v3.d.ts.map +0 -1
  148. package/dist/core/reasoning-router-v3.js +0 -193
  149. package/dist/core/reasoning-router-v3.js.map +0 -1
  150. package/dist/core/reasoning-router-weights.d.ts +0 -155
  151. package/dist/core/reasoning-router-weights.d.ts.map +0 -1
  152. package/dist/core/reasoning-router-weights.js +0 -204
  153. package/dist/core/reasoning-router-weights.js.map +0 -1
  154. package/dist/core/reasoning-router.d.ts +0 -44
  155. package/dist/core/reasoning-router.d.ts.map +0 -1
  156. package/dist/core/reasoning-router.js +0 -138
  157. package/dist/core/reasoning-router.js.map +0 -1
  158. package/dist/core/token-optimizer.d.ts +0 -58
  159. package/dist/core/token-optimizer.d.ts.map +0 -1
  160. package/dist/core/token-optimizer.js +0 -179
  161. package/dist/core/token-optimizer.js.map +0 -1
@@ -0,0 +1,230 @@
1
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import chalk from "chalk";
4
+ import { APP_NAME, getAgentDir } from "./config.js";
5
+ import { CodexBarJsonError, CodexBarUnsafeOutputError, parseCodexBarCostJson, parseCodexBarUsageJson, } from "./core/codexbar-adapter.js";
6
+ import { spawnProcessSync } from "./utils/child-process.js";
7
+ import { ensureTool } from "./utils/tools-manager.js";
8
+ const CONNECTOR_FILE = "codexbar-connector.json";
9
+ const PRIVACY_ACK_VERSION = 1;
10
+ const USAGE = `${APP_NAME} quota <connect|disconnect|status|usage|cost> ...`;
11
+ const DEFAULT_DEPS = {
12
+ getAgentDir,
13
+ exists: existsSync,
14
+ read: (path) => readFileSync(path, "utf8"),
15
+ write: (path, value) => writeFileSync(path, value, "utf8"),
16
+ mkdir: (path) => mkdirSync(path, { recursive: true }),
17
+ remove: (path) => rmSync(path, { force: true }),
18
+ ensure: ensureTool,
19
+ spawn: (command, args) => spawnProcessSync(command, [...args], { encoding: "utf8", stdio: "pipe", shell: false, windowsHide: true }),
20
+ stdout: (text) => process.stdout.write(text),
21
+ stderr: (text) => process.stderr.write(text),
22
+ };
23
+ export function parseCodexBarQuotaCommand(args) {
24
+ if (args[0] !== "quota")
25
+ return undefined;
26
+ const subcommand = args[1];
27
+ const rest = args.slice(2);
28
+ if (subcommand === undefined)
29
+ return { kind: "error", message: "Missing quota command." };
30
+ if (subcommand === "--help" || subcommand === "-h" || subcommand === "help") {
31
+ return rest.length === 0 ? { kind: "help" } : { kind: "error", message: "Unexpected argument." };
32
+ }
33
+ if (subcommand === "connect" || subcommand === "disconnect") {
34
+ if (rest[0] === undefined)
35
+ return { kind: "error", message: `Missing target for quota ${subcommand}. Expected codexbar.` };
36
+ if (rest[0] !== "codexbar")
37
+ return { kind: "error", message: "Unexpected quota target. Expected codexbar." };
38
+ return rest[1] === undefined ? { kind: subcommand } : { kind: "error", message: "Unexpected argument." };
39
+ }
40
+ if (subcommand === "status" || subcommand === "usage" || subcommand === "cost") {
41
+ return parseRunCommand(subcommand === "cost" ? "cost" : "usage", rest);
42
+ }
43
+ return { kind: "error", message: "Unexpected quota command." };
44
+ }
45
+ export async function handleCodexBarQuotaCommand(args, deps = {}) {
46
+ const command = parseCodexBarQuotaCommand(args);
47
+ if (command === undefined)
48
+ return false;
49
+ const d = { ...DEFAULT_DEPS, ...deps };
50
+ try {
51
+ switch (command.kind) {
52
+ case "help":
53
+ printHelp(d);
54
+ return true;
55
+ case "error":
56
+ return fail(d, 1, `${command.message}\n${chalk.dim(`Usage: ${USAGE}`)}`);
57
+ case "connect":
58
+ return connect(d);
59
+ case "disconnect":
60
+ return disconnect(d);
61
+ case "run":
62
+ return await run(command, d);
63
+ }
64
+ }
65
+ catch (error) {
66
+ if (error instanceof Error)
67
+ return fail(d, 1, "CodexBar quota command failed.");
68
+ throw error;
69
+ }
70
+ }
71
+ function parseRunCommand(metric, args) {
72
+ let provider;
73
+ let json = false;
74
+ for (let index = 0; index < args.length; index++) {
75
+ const arg = args[index];
76
+ if (arg === "--json") {
77
+ json = true;
78
+ continue;
79
+ }
80
+ if (arg === "--provider") {
81
+ const value = args[index + 1];
82
+ if (value === undefined || value.startsWith("-") || value.trim().length === 0) {
83
+ return { kind: "error", message: "Missing value for --provider." };
84
+ }
85
+ if (provider !== undefined)
86
+ return { kind: "error", message: "--provider can only be provided once." };
87
+ provider = value;
88
+ index++;
89
+ continue;
90
+ }
91
+ if (arg.startsWith("-"))
92
+ return { kind: "error", message: "Unknown option for quota command." };
93
+ return { kind: "error", message: "Unexpected argument." };
94
+ }
95
+ return { kind: "run", metric, provider, json };
96
+ }
97
+ function connect(d) {
98
+ const dir = d.getAgentDir();
99
+ const acknowledgement = {
100
+ codexbar: { enabled: true, privacyAck: PRIVACY_ACK_VERSION, connectedAt: new Date().toISOString() },
101
+ };
102
+ d.mkdir(dir);
103
+ d.write(connectorPath(d), `${JSON.stringify(acknowledgement, null, 2)}\n`);
104
+ d.stdout(`${chalk.green("CodexBar quota connector enabled.")}\n`);
105
+ return true;
106
+ }
107
+ function disconnect(d) {
108
+ d.remove(connectorPath(d));
109
+ d.stdout(`${chalk.green("CodexBar quota connector disabled.")}\n`);
110
+ return true;
111
+ }
112
+ async function run(command, d) {
113
+ if (!connectorEnabled(d)) {
114
+ return fail(d, 1, `CodexBar quota connector is not enabled.\nRun \`${APP_NAME} quota connect codexbar\` to opt in before checking CodexBar quota.`);
115
+ }
116
+ const codexbar = await d.ensure("codexbar", true);
117
+ if (codexbar === undefined)
118
+ return fail(d, 2, "CodexBar binary was not found.\nInstall codexbar on PATH, then rerun this command.");
119
+ const result = d.spawn(codexbar, codexbarArgs(command));
120
+ if (result.error !== undefined)
121
+ return fail(d, 1, "Failed to run CodexBar.");
122
+ if (result.status !== 0) {
123
+ const reason = result.status === null ? `signal ${result.signal ?? "unknown"}` : `exit code ${result.status}`;
124
+ return fail(d, 1, `CodexBar command failed (${reason}).`);
125
+ }
126
+ return printSummary(command, result.stdout, d);
127
+ }
128
+ function connectorPath(d) {
129
+ return join(d.getAgentDir(), CONNECTOR_FILE);
130
+ }
131
+ function connectorEnabled(d) {
132
+ const path = connectorPath(d);
133
+ if (!d.exists(path))
134
+ return false;
135
+ let parsed;
136
+ try {
137
+ parsed = JSON.parse(d.read(path));
138
+ }
139
+ catch (error) {
140
+ if (error instanceof SyntaxError)
141
+ return false;
142
+ throw error;
143
+ }
144
+ return (isRecord(parsed) &&
145
+ isRecord(parsed.codexbar) &&
146
+ parsed.codexbar.enabled === true &&
147
+ parsed.codexbar.privacyAck === PRIVACY_ACK_VERSION);
148
+ }
149
+ function codexbarArgs(command) {
150
+ const args = command.metric === "cost" ? ["cost", "--format", "json"] : ["--format", "json"];
151
+ return command.provider === undefined ? args : [...args, "--provider", command.provider];
152
+ }
153
+ function printSummary(command, stdout, d) {
154
+ try {
155
+ if (command.metric === "usage") {
156
+ const summary = parseCodexBarUsageJson(stdout);
157
+ d.stdout(command.json ? `${JSON.stringify(summary, null, 2)}\n` : usageText(summary));
158
+ return true;
159
+ }
160
+ const summary = parseCodexBarCostJson(stdout);
161
+ d.stdout(command.json ? `${JSON.stringify(summary, null, 2)}\n` : costText(summary));
162
+ return true;
163
+ }
164
+ catch (error) {
165
+ if (error instanceof CodexBarUnsafeOutputError)
166
+ return fail(d, 3, "CodexBar returned unsafe output; refusing to display it.");
167
+ if (error instanceof CodexBarJsonError)
168
+ return fail(d, 3, "CodexBar returned invalid JSON.");
169
+ throw error;
170
+ }
171
+ }
172
+ function usageText(summary) {
173
+ const lines = ["CodexBar usage", `Provider: ${summary.provider}`];
174
+ if (summary.primary !== undefined)
175
+ lines.push(windowText("Primary", summary.primary));
176
+ if (summary.secondary !== undefined)
177
+ lines.push(windowText("Secondary", summary.secondary));
178
+ if (summary.creditsRemaining !== undefined)
179
+ lines.push(`Credits remaining: ${summary.creditsRemaining}`);
180
+ if (summary.status?.indicator !== undefined)
181
+ lines.push(`Status: ${summary.status.indicator}`);
182
+ if (summary.updatedAt !== undefined)
183
+ lines.push(`Updated: ${summary.updatedAt}`);
184
+ return `${lines.join("\n")}\n`;
185
+ }
186
+ function windowText(label, window) {
187
+ const used = window.usedPercent === undefined ? "n/a" : `${window.usedPercent}% used`;
188
+ return `${label}: ${used}${window.resetsAt === undefined ? "" : `, resets at ${window.resetsAt}`}`;
189
+ }
190
+ function costText(summary) {
191
+ if (summary.length === 0)
192
+ return "CodexBar cost\nNo cost records reported.\n";
193
+ const lines = ["CodexBar cost"];
194
+ for (const entry of summary) {
195
+ lines.push(`Provider: ${entry.provider}`);
196
+ if (entry.sessionCostUSD !== undefined)
197
+ lines.push(` Session: $${entry.sessionCostUSD.toFixed(2)}`);
198
+ if (entry.last30DaysCostUSD !== undefined)
199
+ lines.push(` Last 30 days: $${entry.last30DaysCostUSD.toFixed(2)}`);
200
+ if (entry.totalCostUSD !== undefined)
201
+ lines.push(` Total: $${entry.totalCostUSD.toFixed(2)}`);
202
+ if (entry.totalTokens !== undefined)
203
+ lines.push(` Total tokens: ${entry.totalTokens}`);
204
+ if (entry.updatedAt !== undefined)
205
+ lines.push(` Updated: ${entry.updatedAt}`);
206
+ }
207
+ return `${lines.join("\n")}\n`;
208
+ }
209
+ function fail(d, exitCode, message) {
210
+ d.stderr(`${chalk.red(message)}\n`);
211
+ process.exitCode = exitCode;
212
+ return true;
213
+ }
214
+ function isRecord(value) {
215
+ return typeof value === "object" && value !== null && !Array.isArray(value);
216
+ }
217
+ function printHelp(d) {
218
+ d.stdout(`${chalk.bold("Usage:")}
219
+ ${APP_NAME} quota --help
220
+ ${APP_NAME} quota connect codexbar
221
+ ${APP_NAME} quota disconnect codexbar
222
+ ${APP_NAME} quota status [--provider <id|both|all>] [--json]
223
+ ${APP_NAME} quota usage [--provider <id|both|all>] [--json]
224
+ ${APP_NAME} quota cost [--provider <id|both|all>] [--json]
225
+
226
+ CodexBar quota commands require an explicit local opt-in acknowledgement.
227
+ The acknowledgement stores no secrets and OMK does not read or write CodexBar config.
228
+ `);
229
+ }
230
+ //# sourceMappingURL=codexbar-cli.js.map
@@ -0,0 +1 @@
1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AK,SAAS;YAAE,KAAK,CAAC,IAAI,CAAC,mBAAmB,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC;QACxF,IAAI,KAAK,CAAC,SAAS,KAAK,SAAS;YAAE,KAAK,CAAC,IAAI,CAAC,cAAc,KAAK,CAAC,SAAS,EAAE,CAAC,CAAC;IAChF,CAAC;IACD,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;AAAA,CAC/B;AAED,SAAS,IAAI,CAAC,CAAoB,EAAE,QAAgB,EAAE,OAAe,EAAW;IAC/E,CAAC,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACpC,OAAO,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC5B,OAAO,IAAI,CAAC;AAAA,CACZ;AAED,SAAS,QAAQ,CAAC,KAAc,EAAoC;IACnE,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAAA,CAC5E;AAED,SAAS,SAAS,CAAC,CAAoB,EAAQ;IAC9C,CAAC,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC;IAC7B,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,QAAQ;;;;CAIX,CAAC,CAAC;AAAA,CACF","sourcesContent":["import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport chalk from \"chalk\";\nimport { APP_NAME, getAgentDir } from \"./config.ts\";\nimport {\n\tCodexBarJsonError,\n\tCodexBarUnsafeOutputError,\n\ttype CodexBarUsageWindowSummary,\n\tparseCodexBarCostJson,\n\tparseCodexBarUsageJson,\n} from \"./core/codexbar-adapter.ts\";\nimport { spawnProcessSync } from \"./utils/child-process.ts\";\nimport { ensureTool } from \"./utils/tools-manager.ts\";\n\nconst CONNECTOR_FILE = \"codexbar-connector.json\";\nconst PRIVACY_ACK_VERSION = 1;\nconst USAGE = `${APP_NAME} quota <connect|disconnect|status|usage|cost> ...`;\n\ntype RunCommand = {\n\treadonly kind: \"run\";\n\treadonly metric: \"usage\" | \"cost\";\n\treadonly provider?: string;\n\treadonly json: boolean;\n};\n\nexport type CodexBarQuotaCommand =\n\t| { readonly kind: \"help\" }\n\t| { readonly kind: \"connect\" }\n\t| { readonly kind: \"disconnect\" }\n\t| RunCommand\n\t| { readonly kind: \"error\"; readonly message: string };\n\nexport type CodexBarQuotaSpawnResult = {\n\treadonly stdout: string;\n\treadonly status: number | null;\n\treadonly signal: string | null;\n\treadonly error?: Error;\n};\n\ntype CodexBarQuotaDeps = {\n\treadonly getAgentDir: () => string;\n\treadonly exists: (path: string) => boolean;\n\treadonly read: (path: string) => string;\n\treadonly write: (path: string, value: string) => void;\n\treadonly mkdir: (path: string) => void;\n\treadonly remove: (path: string) => void;\n\treadonly ensure: (tool: \"codexbar\", silent: true) => Promise<string | undefined>;\n\treadonly spawn: (command: string, args: readonly string[]) => CodexBarQuotaSpawnResult;\n\treadonly stdout: (text: string) => void;\n\treadonly stderr: (text: string) => void;\n};\n\nexport type CodexBarQuotaCommandDeps = Partial<CodexBarQuotaDeps>;\n\nconst DEFAULT_DEPS: CodexBarQuotaDeps = {\n\tgetAgentDir,\n\texists: existsSync,\n\tread: (path) => readFileSync(path, \"utf8\"),\n\twrite: (path, value) => writeFileSync(path, value, \"utf8\"),\n\tmkdir: (path) => mkdirSync(path, { recursive: true }),\n\tremove: (path) => rmSync(path, { force: true }),\n\tensure: ensureTool,\n\tspawn: (command, args) =>\n\t\tspawnProcessSync(command, [...args], { encoding: \"utf8\", stdio: \"pipe\", shell: false, windowsHide: true }),\n\tstdout: (text) => process.stdout.write(text),\n\tstderr: (text) => process.stderr.write(text),\n};\n\nexport function parseCodexBarQuotaCommand(args: readonly string[]): CodexBarQuotaCommand | undefined {\n\tif (args[0] !== \"quota\") return undefined;\n\tconst subcommand = args[1];\n\tconst rest = args.slice(2);\n\tif (subcommand === undefined) return { kind: \"error\", message: \"Missing quota command.\" };\n\tif (subcommand === \"--help\" || subcommand === \"-h\" || subcommand === \"help\") {\n\t\treturn rest.length === 0 ? { kind: \"help\" } : { kind: \"error\", message: \"Unexpected argument.\" };\n\t}\n\tif (subcommand === \"connect\" || subcommand === \"disconnect\") {\n\t\tif (rest[0] === undefined)\n\t\t\treturn { kind: \"error\", message: `Missing target for quota ${subcommand}. Expected codexbar.` };\n\t\tif (rest[0] !== \"codexbar\") return { kind: \"error\", message: \"Unexpected quota target. Expected codexbar.\" };\n\t\treturn rest[1] === undefined ? { kind: subcommand } : { kind: \"error\", message: \"Unexpected argument.\" };\n\t}\n\tif (subcommand === \"status\" || subcommand === \"usage\" || subcommand === \"cost\") {\n\t\treturn parseRunCommand(subcommand === \"cost\" ? \"cost\" : \"usage\", rest);\n\t}\n\treturn { kind: \"error\", message: \"Unexpected quota command.\" };\n}\n\nexport async function handleCodexBarQuotaCommand(\n\targs: readonly string[],\n\tdeps: CodexBarQuotaCommandDeps = {},\n): Promise<boolean> {\n\tconst command = parseCodexBarQuotaCommand(args);\n\tif (command === undefined) return false;\n\tconst d: CodexBarQuotaDeps = { ...DEFAULT_DEPS, ...deps };\n\n\ttry {\n\t\tswitch (command.kind) {\n\t\t\tcase \"help\":\n\t\t\t\tprintHelp(d);\n\t\t\t\treturn true;\n\t\t\tcase \"error\":\n\t\t\t\treturn fail(d, 1, `${command.message}\\n${chalk.dim(`Usage: ${USAGE}`)}`);\n\t\t\tcase \"connect\":\n\t\t\t\treturn connect(d);\n\t\t\tcase \"disconnect\":\n\t\t\t\treturn disconnect(d);\n\t\t\tcase \"run\":\n\t\t\t\treturn await run(command, d);\n\t\t}\n\t} catch (error) {\n\t\tif (error instanceof Error) return fail(d, 1, \"CodexBar quota command failed.\");\n\t\tthrow error;\n\t}\n}\n\nfunction parseRunCommand(metric: RunCommand[\"metric\"], args: readonly string[]): CodexBarQuotaCommand {\n\tlet provider: string | undefined;\n\tlet json = false;\n\tfor (let index = 0; index < args.length; index++) {\n\t\tconst arg = args[index];\n\t\tif (arg === \"--json\") {\n\t\t\tjson = true;\n\t\t\tcontinue;\n\t\t}\n\t\tif (arg === \"--provider\") {\n\t\t\tconst value = args[index + 1];\n\t\t\tif (value === undefined || value.startsWith(\"-\") || value.trim().length === 0) {\n\t\t\t\treturn { kind: \"error\", message: \"Missing value for --provider.\" };\n\t\t\t}\n\t\t\tif (provider !== undefined) return { kind: \"error\", message: \"--provider can only be provided once.\" };\n\t\t\tprovider = value;\n\t\t\tindex++;\n\t\t\tcontinue;\n\t\t}\n\t\tif (arg.startsWith(\"-\")) return { kind: \"error\", message: \"Unknown option for quota command.\" };\n\t\treturn { kind: \"error\", message: \"Unexpected argument.\" };\n\t}\n\treturn { kind: \"run\", metric, provider, json };\n}\n\nfunction connect(d: CodexBarQuotaDeps): boolean {\n\tconst dir = d.getAgentDir();\n\tconst acknowledgement = {\n\t\tcodexbar: { enabled: true, privacyAck: PRIVACY_ACK_VERSION, connectedAt: new Date().toISOString() },\n\t};\n\td.mkdir(dir);\n\td.write(connectorPath(d), `${JSON.stringify(acknowledgement, null, 2)}\\n`);\n\td.stdout(`${chalk.green(\"CodexBar quota connector enabled.\")}\\n`);\n\treturn true;\n}\n\nfunction disconnect(d: CodexBarQuotaDeps): boolean {\n\td.remove(connectorPath(d));\n\td.stdout(`${chalk.green(\"CodexBar quota connector disabled.\")}\\n`);\n\treturn true;\n}\n\nasync function run(command: RunCommand, d: CodexBarQuotaDeps): Promise<boolean> {\n\tif (!connectorEnabled(d)) {\n\t\treturn fail(\n\t\t\td,\n\t\t\t1,\n\t\t\t`CodexBar quota connector is not enabled.\\nRun \\`${APP_NAME} quota connect codexbar\\` to opt in before checking CodexBar quota.`,\n\t\t);\n\t}\n\n\tconst codexbar = await d.ensure(\"codexbar\", true);\n\tif (codexbar === undefined)\n\t\treturn fail(d, 2, \"CodexBar binary was not found.\\nInstall codexbar on PATH, then rerun this command.\");\n\n\tconst result = d.spawn(codexbar, codexbarArgs(command));\n\tif (result.error !== undefined) return fail(d, 1, \"Failed to run CodexBar.\");\n\tif (result.status !== 0) {\n\t\tconst reason = result.status === null ? `signal ${result.signal ?? \"unknown\"}` : `exit code ${result.status}`;\n\t\treturn fail(d, 1, `CodexBar command failed (${reason}).`);\n\t}\n\treturn printSummary(command, result.stdout, d);\n}\n\nfunction connectorPath(d: CodexBarQuotaDeps): string {\n\treturn join(d.getAgentDir(), CONNECTOR_FILE);\n}\n\nfunction connectorEnabled(d: CodexBarQuotaDeps): boolean {\n\tconst path = connectorPath(d);\n\tif (!d.exists(path)) return false;\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(d.read(path));\n\t} catch (error) {\n\t\tif (error instanceof SyntaxError) return false;\n\t\tthrow error;\n\t}\n\treturn (\n\t\tisRecord(parsed) &&\n\t\tisRecord(parsed.codexbar) &&\n\t\tparsed.codexbar.enabled === true &&\n\t\tparsed.codexbar.privacyAck === PRIVACY_ACK_VERSION\n\t);\n}\n\nfunction codexbarArgs(command: RunCommand): readonly string[] {\n\tconst args = command.metric === \"cost\" ? [\"cost\", \"--format\", \"json\"] : [\"--format\", \"json\"];\n\treturn command.provider === undefined ? args : [...args, \"--provider\", command.provider];\n}\n\nfunction printSummary(command: RunCommand, stdout: string, d: CodexBarQuotaDeps): boolean {\n\ttry {\n\t\tif (command.metric === \"usage\") {\n\t\t\tconst summary = parseCodexBarUsageJson(stdout);\n\t\t\td.stdout(command.json ? `${JSON.stringify(summary, null, 2)}\\n` : usageText(summary));\n\t\t\treturn true;\n\t\t}\n\t\tconst summary = parseCodexBarCostJson(stdout);\n\t\td.stdout(command.json ? `${JSON.stringify(summary, null, 2)}\\n` : costText(summary));\n\t\treturn true;\n\t} catch (error) {\n\t\tif (error instanceof CodexBarUnsafeOutputError)\n\t\t\treturn fail(d, 3, \"CodexBar returned unsafe output; refusing to display it.\");\n\t\tif (error instanceof CodexBarJsonError) return fail(d, 3, \"CodexBar returned invalid JSON.\");\n\t\tthrow error;\n\t}\n}\n\nfunction usageText(summary: ReturnType<typeof parseCodexBarUsageJson>): string {\n\tconst lines = [\"CodexBar usage\", `Provider: ${summary.provider}`];\n\tif (summary.primary !== undefined) lines.push(windowText(\"Primary\", summary.primary));\n\tif (summary.secondary !== undefined) lines.push(windowText(\"Secondary\", summary.secondary));\n\tif (summary.creditsRemaining !== undefined) lines.push(`Credits remaining: ${summary.creditsRemaining}`);\n\tif (summary.status?.indicator !== undefined) lines.push(`Status: ${summary.status.indicator}`);\n\tif (summary.updatedAt !== undefined) lines.push(`Updated: ${summary.updatedAt}`);\n\treturn `${lines.join(\"\\n\")}\\n`;\n}\n\nfunction windowText(label: string, window: CodexBarUsageWindowSummary): string {\n\tconst used = window.usedPercent === undefined ? \"n/a\" : `${window.usedPercent}% used`;\n\treturn `${label}: ${used}${window.resetsAt === undefined ? \"\" : `, resets at ${window.resetsAt}`}`;\n}\n\nfunction costText(summary: ReturnType<typeof parseCodexBarCostJson>): string {\n\tif (summary.length === 0) return \"CodexBar cost\\nNo cost records reported.\\n\";\n\tconst lines = [\"CodexBar cost\"];\n\tfor (const entry of summary) {\n\t\tlines.push(`Provider: ${entry.provider}`);\n\t\tif (entry.sessionCostUSD !== undefined) lines.push(` Session: $${entry.sessionCostUSD.toFixed(2)}`);\n\t\tif (entry.last30DaysCostUSD !== undefined) lines.push(` Last 30 days: $${entry.last30DaysCostUSD.toFixed(2)}`);\n\t\tif (entry.totalCostUSD !== undefined) lines.push(` Total: $${entry.totalCostUSD.toFixed(2)}`);\n\t\tif (entry.totalTokens !== undefined) lines.push(` Total tokens: ${entry.totalTokens}`);\n\t\tif (entry.updatedAt !== undefined) lines.push(` Updated: ${entry.updatedAt}`);\n\t}\n\treturn `${lines.join(\"\\n\")}\\n`;\n}\n\nfunction fail(d: CodexBarQuotaDeps, exitCode: number, message: string): boolean {\n\td.stderr(`${chalk.red(message)}\\n`);\n\tprocess.exitCode = exitCode;\n\treturn true;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction printHelp(d: CodexBarQuotaDeps): void {\n\td.stdout(`${chalk.bold(\"Usage:\")}\n ${APP_NAME} quota --help\n ${APP_NAME} quota connect codexbar\n ${APP_NAME} quota disconnect codexbar\n ${APP_NAME} quota status [--provider <id|both|all>] [--json]\n ${APP_NAME} quota usage [--provider <id|both|all>] [--json]\n ${APP_NAME} quota cost [--provider <id|both|all>] [--json]\n\nCodexBar quota commands require an explicit local opt-in acknowledgement.\nThe acknowledgement stores no secrets and OMK does not read or write CodexBar config.\n`);\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"doctor-provider.d.ts","sourceRoot":"","sources":["../../src/commands/doctor-provider.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,mBAAmB,GAAG,SAAS,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,SAAS,CAAC;AAEtF,MAAM,WAAW,oBAAoB;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,IAAI,GAAG,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,mBAAmB,EAAE,CAAC;IAC9B,KAAK,CAAC,EAAE;QACP,QAAQ,EAAE,mBAAmB,CAAC;QAC9B,OAAO,EAAE,MAAM,CAAC;KAChB,CAAC;CACF;AAED,MAAM,WAAW,mBAAmB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,IAAI,GAAG,MAAM,GAAG,SAAS,CAAC;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAQD,wBAAgB,WAAW,IAAI,MAAM,CAEpC;AAgCD,wBAAsB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC,CA2FxF","sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\n\nexport type DoctorErrorCategory = \"network\" | \"auth\" | \"model\" | \"config\" | \"unknown\";\n\nexport interface ProviderDoctorResult {\n\tprovider: string;\n\tstatus: \"ok\" | \"fail\";\n\tbaseUrl?: string;\n\tchecks: ProviderDoctorCheck[];\n\terror?: {\n\t\tcategory: DoctorErrorCategory;\n\t\tmessage: string;\n\t};\n}\n\nexport interface ProviderDoctorCheck {\n\tname: string;\n\tstatus: \"ok\" | \"fail\" | \"skipped\";\n\tmessage?: string;\n}\n\ninterface ProviderConfig {\n\tbaseUrl?: string;\n\tapiKey?: string;\n\tapi?: string;\n}\n\nexport function getAgentDir(): string {\n\treturn join(homedir(), \".omk\", \"agent\");\n}\n\nfunction loadJson<T>(path: string): T | undefined {\n\tif (!existsSync(path)) return undefined;\n\ttry {\n\t\treturn JSON.parse(readFileSync(path, \"utf-8\")) as T;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction resolveConfig(providerId: string): { config: ProviderConfig; source: string } | undefined {\n\tconst agentDir = getAgentDir();\n\n\tconst modelsPath = join(agentDir, \"models.json\");\n\tconst models = loadJson<{ providers?: Record<string, ProviderConfig> }>(modelsPath);\n\tif (models?.providers?.[providerId]) {\n\t\treturn { config: models.providers[providerId], source: \"models.json\" };\n\t}\n\n\tconst authPath = join(agentDir, \"auth.json\");\n\tconst auth = loadJson<Record<string, { type: string; key?: string }>>(authPath);\n\tif (auth?.[providerId]) {\n\t\treturn {\n\t\t\tconfig: { apiKey: auth[providerId].key },\n\t\t\tsource: \"auth.json\",\n\t\t};\n\t}\n\n\treturn undefined;\n}\n\nexport async function diagnoseProvider(providerId: string): Promise<ProviderDoctorResult> {\n\tconst resolved = resolveConfig(providerId);\n\tif (!resolved) {\n\t\treturn {\n\t\t\tprovider: providerId,\n\t\t\tstatus: \"fail\",\n\t\t\tchecks: [],\n\t\t\terror: { category: \"config\", message: `Provider \"${providerId}\" not found in models.json or auth.json` },\n\t\t};\n\t}\n\n\tconst { config } = resolved;\n\tconst checks: ProviderDoctorCheck[] = [];\n\n\t// 1. Config presence\n\tchecks.push({\n\t\tname: \"config-present\",\n\t\tstatus: \"ok\",\n\t\tmessage: `Found provider config in ${resolved.source}`,\n\t});\n\n\t// 2. Base URL reachability\n\tif (config.baseUrl) {\n\t\ttry {\n\t\t\tconst res = await fetch(config.baseUrl, { method: \"GET\" });\n\t\t\tchecks.push({\n\t\t\t\tname: \"base-url-reachable\",\n\t\t\t\tstatus: res.ok ? \"ok\" : \"fail\",\n\t\t\t\tmessage: `HTTP ${res.status}`,\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"base-url-reachable\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t}\n\t} else {\n\t\tchecks.push({ name: \"base-url-reachable\", status: \"skipped\", message: \"No baseUrl configured\" });\n\t}\n\n\t// 3. API key presence (never log the value)\n\tchecks.push({\n\t\tname: \"api-key-present\",\n\t\tstatus: config.apiKey ? \"ok\" : \"skipped\",\n\t\tmessage: config.apiKey ? \"API key is configured\" : \"No API key configured\",\n\t});\n\n\t// 4. Models endpoint probe\n\tif (config.baseUrl) {\n\t\ttry {\n\t\t\tconst headers: Record<string, string> = {};\n\t\t\tif (config.apiKey) headers.Authorization = `Bearer ${config.apiKey}`;\n\t\t\tconst res = await fetch(`${config.baseUrl.replace(/\\/$/, \"\")}/models`, { headers });\n\t\t\tchecks.push({\n\t\t\t\tname: \"models-endpoint\",\n\t\t\t\tstatus: res.ok ? \"ok\" : \"fail\",\n\t\t\t\tmessage: `HTTP ${res.status}`,\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"models-endpoint\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t}\n\t} else {\n\t\tchecks.push({ name: \"models-endpoint\", status: \"skipped\", message: \"No baseUrl configured\" });\n\t}\n\n\tconst failed = checks.find((c) => c.status === \"fail\");\n\tlet error: ProviderDoctorResult[\"error\"];\n\tif (failed) {\n\t\tconst category: DoctorErrorCategory =\n\t\t\tfailed.name === \"base-url-reachable\"\n\t\t\t\t? \"network\"\n\t\t\t\t: failed.name === \"api-key-present\"\n\t\t\t\t\t? \"auth\"\n\t\t\t\t\t: failed.name === \"models-endpoint\"\n\t\t\t\t\t\t? \"model\"\n\t\t\t\t\t\t: \"unknown\";\n\t\terror = { category, message: failed.message ?? `${failed.name} failed` };\n\t}\n\n\treturn {\n\t\tprovider: providerId,\n\t\tstatus: failed ? \"fail\" : \"ok\",\n\t\tbaseUrl: config.baseUrl,\n\t\tchecks,\n\t\terror,\n\t};\n}\n"]}
1
+ {"version":3,"file":"doctor-provider.d.ts","sourceRoot":"","sources":["../../src/commands/doctor-provider.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,mBAAmB,GAAG,SAAS,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,SAAS,CAAC;AAEtF,MAAM,WAAW,oBAAoB;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,IAAI,GAAG,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,mBAAmB,EAAE,CAAC;IAC9B,KAAK,CAAC,EAAE;QACP,QAAQ,EAAE,mBAAmB,CAAC;QAC9B,OAAO,EAAE,MAAM,CAAC;KAChB,CAAC;CACF;AAED,MAAM,WAAW,mBAAmB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,IAAI,GAAG,MAAM,GAAG,SAAS,CAAC;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAQD,wBAAgB,WAAW,IAAI,MAAM,CAEpC;AA4CD,wBAAsB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC,CA6FxF","sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\n\nexport type DoctorErrorCategory = \"network\" | \"auth\" | \"model\" | \"config\" | \"unknown\";\n\nexport interface ProviderDoctorResult {\n\tprovider: string;\n\tstatus: \"ok\" | \"fail\";\n\tbaseUrl?: string;\n\tchecks: ProviderDoctorCheck[];\n\terror?: {\n\t\tcategory: DoctorErrorCategory;\n\t\tmessage: string;\n\t};\n}\n\nexport interface ProviderDoctorCheck {\n\tname: string;\n\tstatus: \"ok\" | \"fail\" | \"skipped\";\n\tmessage?: string;\n}\n\ninterface ProviderConfig {\n\tbaseUrl?: string;\n\tapiKey?: string;\n\tapi?: string;\n}\n\nexport function getAgentDir(): string {\n\treturn join(homedir(), \".omk\", \"agent\");\n}\n\nfunction loadJson<T>(path: string): T | undefined {\n\tif (!existsSync(path)) return undefined;\n\ttry {\n\t\treturn JSON.parse(readFileSync(path, \"utf-8\")) as T;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction resolveConfig(providerId: string): { config: ProviderConfig; source: string } | undefined {\n\tconst agentDir = getAgentDir();\n\n\tconst modelsPath = join(agentDir, \"models.json\");\n\tconst models = loadJson<{ providers?: Record<string, ProviderConfig> }>(modelsPath);\n\tif (models?.providers?.[providerId]) {\n\t\treturn { config: models.providers[providerId], source: \"models.json\" };\n\t}\n\n\tconst authPath = join(agentDir, \"auth.json\");\n\tconst auth = loadJson<Record<string, { type: string; key?: string }>>(authPath);\n\tif (auth?.[providerId]) {\n\t\treturn {\n\t\t\tconfig: { apiKey: auth[providerId].key },\n\t\t\tsource: \"auth.json\",\n\t\t};\n\t}\n\n\t// Built-in local Grok OAuth proxy defaults (no secrets; loopback only).\n\tif (providerId === \"grok-oauth-proxy\") {\n\t\treturn {\n\t\t\tconfig: {\n\t\t\t\tbaseUrl: \"http://127.0.0.1:9996/v1\",\n\t\t\t\tapi: \"openai-completions\",\n\t\t\t\tapiKey: \"dummy\",\n\t\t\t},\n\t\t\tsource: \"built-in-grok-oauth-proxy-defaults\",\n\t\t};\n\t}\n\n\treturn undefined;\n}\n\nexport async function diagnoseProvider(providerId: string): Promise<ProviderDoctorResult> {\n\tconst resolved = resolveConfig(providerId);\n\tif (!resolved) {\n\t\treturn {\n\t\t\tprovider: providerId,\n\t\t\tstatus: \"fail\",\n\t\t\tchecks: [],\n\t\t\terror: { category: \"config\", message: `Provider \"${providerId}\" not found in models.json or auth.json` },\n\t\t};\n\t}\n\n\tconst { config } = resolved;\n\tconst checks: ProviderDoctorCheck[] = [];\n\n\t// 1. Config presence\n\tchecks.push({\n\t\tname: \"config-present\",\n\t\tstatus: \"ok\",\n\t\tmessage: `Found provider config in ${resolved.source}`,\n\t});\n\n\t// 2. Base URL / health reachability\n\t// OpenAI-compatible roots often 404 on GET /v1; prefer /health for local Grok proxy.\n\tif (config.baseUrl) {\n\t\ttry {\n\t\t\tconst probeUrl = providerId === \"grok-oauth-proxy\" ? new URL(\"/health\", config.baseUrl).href : config.baseUrl;\n\t\t\tconst res = await fetch(probeUrl, { method: \"GET\" });\n\t\t\tchecks.push({\n\t\t\t\tname: \"base-url-reachable\",\n\t\t\t\tstatus: res.ok ? \"ok\" : \"fail\",\n\t\t\t\tmessage: `${probeUrl} → HTTP ${res.status}`,\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"base-url-reachable\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t}\n\t} else {\n\t\tchecks.push({ name: \"base-url-reachable\", status: \"skipped\", message: \"No baseUrl configured\" });\n\t}\n\n\t// 3. API key presence (never log the value)\n\tchecks.push({\n\t\tname: \"api-key-present\",\n\t\tstatus: config.apiKey ? \"ok\" : \"skipped\",\n\t\tmessage: config.apiKey ? \"API key is configured\" : \"No API key configured\",\n\t});\n\n\t// 4. Models endpoint probe\n\tif (config.baseUrl) {\n\t\ttry {\n\t\t\tconst headers: Record<string, string> = {};\n\t\t\tif (config.apiKey) headers.Authorization = `Bearer ${config.apiKey}`;\n\t\t\tconst res = await fetch(`${config.baseUrl.replace(/\\/$/, \"\")}/models`, { headers });\n\t\t\tchecks.push({\n\t\t\t\tname: \"models-endpoint\",\n\t\t\t\tstatus: res.ok ? \"ok\" : \"fail\",\n\t\t\t\tmessage: `HTTP ${res.status}`,\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"models-endpoint\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t}\n\t} else {\n\t\tchecks.push({ name: \"models-endpoint\", status: \"skipped\", message: \"No baseUrl configured\" });\n\t}\n\n\tconst failed = checks.find((c) => c.status === \"fail\");\n\tlet error: ProviderDoctorResult[\"error\"];\n\tif (failed) {\n\t\tconst category: DoctorErrorCategory =\n\t\t\tfailed.name === \"base-url-reachable\"\n\t\t\t\t? \"network\"\n\t\t\t\t: failed.name === \"api-key-present\"\n\t\t\t\t\t? \"auth\"\n\t\t\t\t\t: failed.name === \"models-endpoint\"\n\t\t\t\t\t\t? \"model\"\n\t\t\t\t\t\t: \"unknown\";\n\t\terror = { category, message: failed.message ?? `${failed.name} failed` };\n\t}\n\n\treturn {\n\t\tprovider: providerId,\n\t\tstatus: failed ? \"fail\" : \"ok\",\n\t\tbaseUrl: config.baseUrl,\n\t\tchecks,\n\t\terror,\n\t};\n}\n"]}
@@ -29,6 +29,17 @@ function resolveConfig(providerId) {
29
29
  source: "auth.json",
30
30
  };
31
31
  }
32
+ // Built-in local Grok OAuth proxy defaults (no secrets; loopback only).
33
+ if (providerId === "grok-oauth-proxy") {
34
+ return {
35
+ config: {
36
+ baseUrl: "http://127.0.0.1:9996/v1",
37
+ api: "openai-completions",
38
+ apiKey: "dummy",
39
+ },
40
+ source: "built-in-grok-oauth-proxy-defaults",
41
+ };
42
+ }
32
43
  return undefined;
33
44
  }
34
45
  export async function diagnoseProvider(providerId) {
@@ -49,14 +60,16 @@ export async function diagnoseProvider(providerId) {
49
60
  status: "ok",
50
61
  message: `Found provider config in ${resolved.source}`,
51
62
  });
52
- // 2. Base URL reachability
63
+ // 2. Base URL / health reachability
64
+ // OpenAI-compatible roots often 404 on GET /v1; prefer /health for local Grok proxy.
53
65
  if (config.baseUrl) {
54
66
  try {
55
- const res = await fetch(config.baseUrl, { method: "GET" });
67
+ const probeUrl = providerId === "grok-oauth-proxy" ? new URL("/health", config.baseUrl).href : config.baseUrl;
68
+ const res = await fetch(probeUrl, { method: "GET" });
56
69
  checks.push({
57
70
  name: "base-url-reachable",
58
71
  status: res.ok ? "ok" : "fail",
59
- message: `HTTP ${res.status}`,
72
+ message: `${probeUrl} → HTTP ${res.status}`,
60
73
  });
61
74
  }
62
75
  catch (err) {
@@ -1 +1 @@
1
- {"version":3,"file":"doctor-provider.js","sourceRoot":"","sources":["../../src/commands/doctor-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AA2BjC,MAAM,UAAU,WAAW,GAAW;IACrC,OAAO,IAAI,CAAC,OAAO,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;AAAA,CACxC;AAED,SAAS,QAAQ,CAAI,IAAY,EAAiB;IACjD,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC;QAAE,OAAO,SAAS,CAAC;IACxC,IAAI,CAAC;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAM,CAAC;IACrD,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,SAAS,CAAC;IAClB,CAAC;AAAA,CACD;AAED,SAAS,aAAa,CAAC,UAAkB,EAA0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AClC,CAAC,CAAC,MAAM;gBACR,CAAC,CAAC,MAAM,CAAC,IAAI,KAAK,iBAAiB;oBAClC,CAAC,CAAC,OAAO;oBACT,CAAC,CAAC,SAAS,CAAC;QAChB,KAAK,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,GAAG,MAAM,CAAC,IAAI,SAAS,EAAE,CAAC;IAC1E,CAAC;IAED,OAAO;QACN,QAAQ,EAAE,UAAU;QACpB,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI;QAC9B,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,MAAM;QACN,KAAK;KACL,CAAC;AAAA,CACF","sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\n\nexport type DoctorErrorCategory = \"network\" | \"auth\" | \"model\" | \"config\" | \"unknown\";\n\nexport interface ProviderDoctorResult {\n\tprovider: string;\n\tstatus: \"ok\" | \"fail\";\n\tbaseUrl?: string;\n\tchecks: ProviderDoctorCheck[];\n\terror?: {\n\t\tcategory: DoctorErrorCategory;\n\t\tmessage: string;\n\t};\n}\n\nexport interface ProviderDoctorCheck {\n\tname: string;\n\tstatus: \"ok\" | \"fail\" | \"skipped\";\n\tmessage?: string;\n}\n\ninterface ProviderConfig {\n\tbaseUrl?: string;\n\tapiKey?: string;\n\tapi?: string;\n}\n\nexport function getAgentDir(): string {\n\treturn join(homedir(), \".omk\", \"agent\");\n}\n\nfunction loadJson<T>(path: string): T | undefined {\n\tif (!existsSync(path)) return undefined;\n\ttry {\n\t\treturn JSON.parse(readFileSync(path, \"utf-8\")) as T;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction resolveConfig(providerId: string): { config: ProviderConfig; source: string } | undefined {\n\tconst agentDir = getAgentDir();\n\n\tconst modelsPath = join(agentDir, \"models.json\");\n\tconst models = loadJson<{ providers?: Record<string, ProviderConfig> }>(modelsPath);\n\tif (models?.providers?.[providerId]) {\n\t\treturn { config: models.providers[providerId], source: \"models.json\" };\n\t}\n\n\tconst authPath = join(agentDir, \"auth.json\");\n\tconst auth = loadJson<Record<string, { type: string; key?: string }>>(authPath);\n\tif (auth?.[providerId]) {\n\t\treturn {\n\t\t\tconfig: { apiKey: auth[providerId].key },\n\t\t\tsource: \"auth.json\",\n\t\t};\n\t}\n\n\treturn undefined;\n}\n\nexport async function diagnoseProvider(providerId: string): Promise<ProviderDoctorResult> {\n\tconst resolved = resolveConfig(providerId);\n\tif (!resolved) {\n\t\treturn {\n\t\t\tprovider: providerId,\n\t\t\tstatus: \"fail\",\n\t\t\tchecks: [],\n\t\t\terror: { category: \"config\", message: `Provider \"${providerId}\" not found in models.json or auth.json` },\n\t\t};\n\t}\n\n\tconst { config } = resolved;\n\tconst checks: ProviderDoctorCheck[] = [];\n\n\t// 1. Config presence\n\tchecks.push({\n\t\tname: \"config-present\",\n\t\tstatus: \"ok\",\n\t\tmessage: `Found provider config in ${resolved.source}`,\n\t});\n\n\t// 2. Base URL reachability\n\tif (config.baseUrl) {\n\t\ttry {\n\t\t\tconst res = await fetch(config.baseUrl, { method: \"GET\" });\n\t\t\tchecks.push({\n\t\t\t\tname: \"base-url-reachable\",\n\t\t\t\tstatus: res.ok ? \"ok\" : \"fail\",\n\t\t\t\tmessage: `HTTP ${res.status}`,\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"base-url-reachable\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t}\n\t} else {\n\t\tchecks.push({ name: \"base-url-reachable\", status: \"skipped\", message: \"No baseUrl configured\" });\n\t}\n\n\t// 3. API key presence (never log the value)\n\tchecks.push({\n\t\tname: \"api-key-present\",\n\t\tstatus: config.apiKey ? \"ok\" : \"skipped\",\n\t\tmessage: config.apiKey ? \"API key is configured\" : \"No API key configured\",\n\t});\n\n\t// 4. Models endpoint probe\n\tif (config.baseUrl) {\n\t\ttry {\n\t\t\tconst headers: Record<string, string> = {};\n\t\t\tif (config.apiKey) headers.Authorization = `Bearer ${config.apiKey}`;\n\t\t\tconst res = await fetch(`${config.baseUrl.replace(/\\/$/, \"\")}/models`, { headers });\n\t\t\tchecks.push({\n\t\t\t\tname: \"models-endpoint\",\n\t\t\t\tstatus: res.ok ? \"ok\" : \"fail\",\n\t\t\t\tmessage: `HTTP ${res.status}`,\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"models-endpoint\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t}\n\t} else {\n\t\tchecks.push({ name: \"models-endpoint\", status: \"skipped\", message: \"No baseUrl configured\" });\n\t}\n\n\tconst failed = checks.find((c) => c.status === \"fail\");\n\tlet error: ProviderDoctorResult[\"error\"];\n\tif (failed) {\n\t\tconst category: DoctorErrorCategory =\n\t\t\tfailed.name === \"base-url-reachable\"\n\t\t\t\t? \"network\"\n\t\t\t\t: failed.name === \"api-key-present\"\n\t\t\t\t\t? \"auth\"\n\t\t\t\t\t: failed.name === \"models-endpoint\"\n\t\t\t\t\t\t? \"model\"\n\t\t\t\t\t\t: \"unknown\";\n\t\terror = { category, message: failed.message ?? `${failed.name} failed` };\n\t}\n\n\treturn {\n\t\tprovider: providerId,\n\t\tstatus: failed ? \"fail\" : \"ok\",\n\t\tbaseUrl: config.baseUrl,\n\t\tchecks,\n\t\terror,\n\t};\n}\n"]}
1
+ {"version":3,"file":"doctor-provider.js","sourceRoot":"","sources":["../../src/commands/doctor-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AA2BjC,MAAM,UAAU,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;QACN,KAAK;KACL,CAAC;AAAA,CACF","sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { join } from \"node:path\";\n\nexport type DoctorErrorCategory = \"network\" | \"auth\" | \"model\" | \"config\" | \"unknown\";\n\nexport interface ProviderDoctorResult {\n\tprovider: string;\n\tstatus: \"ok\" | \"fail\";\n\tbaseUrl?: string;\n\tchecks: ProviderDoctorCheck[];\n\terror?: {\n\t\tcategory: DoctorErrorCategory;\n\t\tmessage: string;\n\t};\n}\n\nexport interface ProviderDoctorCheck {\n\tname: string;\n\tstatus: \"ok\" | \"fail\" | \"skipped\";\n\tmessage?: string;\n}\n\ninterface ProviderConfig {\n\tbaseUrl?: string;\n\tapiKey?: string;\n\tapi?: string;\n}\n\nexport function getAgentDir(): string {\n\treturn join(homedir(), \".omk\", \"agent\");\n}\n\nfunction loadJson<T>(path: string): T | undefined {\n\tif (!existsSync(path)) return undefined;\n\ttry {\n\t\treturn JSON.parse(readFileSync(path, \"utf-8\")) as T;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction resolveConfig(providerId: string): { config: ProviderConfig; source: string } | undefined {\n\tconst agentDir = getAgentDir();\n\n\tconst modelsPath = join(agentDir, \"models.json\");\n\tconst models = loadJson<{ providers?: Record<string, ProviderConfig> }>(modelsPath);\n\tif (models?.providers?.[providerId]) {\n\t\treturn { config: models.providers[providerId], source: \"models.json\" };\n\t}\n\n\tconst authPath = join(agentDir, \"auth.json\");\n\tconst auth = loadJson<Record<string, { type: string; key?: string }>>(authPath);\n\tif (auth?.[providerId]) {\n\t\treturn {\n\t\t\tconfig: { apiKey: auth[providerId].key },\n\t\t\tsource: \"auth.json\",\n\t\t};\n\t}\n\n\t// Built-in local Grok OAuth proxy defaults (no secrets; loopback only).\n\tif (providerId === \"grok-oauth-proxy\") {\n\t\treturn {\n\t\t\tconfig: {\n\t\t\t\tbaseUrl: \"http://127.0.0.1:9996/v1\",\n\t\t\t\tapi: \"openai-completions\",\n\t\t\t\tapiKey: \"dummy\",\n\t\t\t},\n\t\t\tsource: \"built-in-grok-oauth-proxy-defaults\",\n\t\t};\n\t}\n\n\treturn undefined;\n}\n\nexport async function diagnoseProvider(providerId: string): Promise<ProviderDoctorResult> {\n\tconst resolved = resolveConfig(providerId);\n\tif (!resolved) {\n\t\treturn {\n\t\t\tprovider: providerId,\n\t\t\tstatus: \"fail\",\n\t\t\tchecks: [],\n\t\t\terror: { category: \"config\", message: `Provider \"${providerId}\" not found in models.json or auth.json` },\n\t\t};\n\t}\n\n\tconst { config } = resolved;\n\tconst checks: ProviderDoctorCheck[] = [];\n\n\t// 1. Config presence\n\tchecks.push({\n\t\tname: \"config-present\",\n\t\tstatus: \"ok\",\n\t\tmessage: `Found provider config in ${resolved.source}`,\n\t});\n\n\t// 2. Base URL / health reachability\n\t// OpenAI-compatible roots often 404 on GET /v1; prefer /health for local Grok proxy.\n\tif (config.baseUrl) {\n\t\ttry {\n\t\t\tconst probeUrl = providerId === \"grok-oauth-proxy\" ? new URL(\"/health\", config.baseUrl).href : config.baseUrl;\n\t\t\tconst res = await fetch(probeUrl, { method: \"GET\" });\n\t\t\tchecks.push({\n\t\t\t\tname: \"base-url-reachable\",\n\t\t\t\tstatus: res.ok ? \"ok\" : \"fail\",\n\t\t\t\tmessage: `${probeUrl} → HTTP ${res.status}`,\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"base-url-reachable\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t}\n\t} else {\n\t\tchecks.push({ name: \"base-url-reachable\", status: \"skipped\", message: \"No baseUrl configured\" });\n\t}\n\n\t// 3. API key presence (never log the value)\n\tchecks.push({\n\t\tname: \"api-key-present\",\n\t\tstatus: config.apiKey ? \"ok\" : \"skipped\",\n\t\tmessage: config.apiKey ? \"API key is configured\" : \"No API key configured\",\n\t});\n\n\t// 4. Models endpoint probe\n\tif (config.baseUrl) {\n\t\ttry {\n\t\t\tconst headers: Record<string, string> = {};\n\t\t\tif (config.apiKey) headers.Authorization = `Bearer ${config.apiKey}`;\n\t\t\tconst res = await fetch(`${config.baseUrl.replace(/\\/$/, \"\")}/models`, { headers });\n\t\t\tchecks.push({\n\t\t\t\tname: \"models-endpoint\",\n\t\t\t\tstatus: res.ok ? \"ok\" : \"fail\",\n\t\t\t\tmessage: `HTTP ${res.status}`,\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"models-endpoint\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t});\n\t\t}\n\t} else {\n\t\tchecks.push({ name: \"models-endpoint\", status: \"skipped\", message: \"No baseUrl configured\" });\n\t}\n\n\tconst failed = checks.find((c) => c.status === \"fail\");\n\tlet error: ProviderDoctorResult[\"error\"];\n\tif (failed) {\n\t\tconst category: DoctorErrorCategory =\n\t\t\tfailed.name === \"base-url-reachable\"\n\t\t\t\t? \"network\"\n\t\t\t\t: failed.name === \"api-key-present\"\n\t\t\t\t\t? \"auth\"\n\t\t\t\t\t: failed.name === \"models-endpoint\"\n\t\t\t\t\t\t? \"model\"\n\t\t\t\t\t\t: \"unknown\";\n\t\terror = { category, message: failed.message ?? `${failed.name} failed` };\n\t}\n\n\treturn {\n\t\tprovider: providerId,\n\t\tstatus: failed ? \"fail\" : \"ok\",\n\t\tbaseUrl: config.baseUrl,\n\t\tchecks,\n\t\terror,\n\t};\n}\n"]}
@@ -1,14 +1,13 @@
1
1
  /**
2
2
  * Reasoning-router AdaptOrch advisory bridge (Goal 009 Lane B).
3
3
  *
4
- * A default-off, advisory-only primitive for a future opt-in reasoning-router
5
- * hint source. This module is intentionally NOT wired into agent-session.ts,
6
- * reasoning-router-v2.ts, or reasoning-router-v3.ts by this lane - it only
7
- * implements the bridge's payload/result schema, sanitize/validate helpers,
8
- * TTL cache, budget counter, circuit breaker, and timeout wrapper around a
9
- * caller-injected advisory function. A future lane decides how (or whether)
10
- * to fuse a returned hint into a resolved ThinkingLevel and to actually call
11
- * this module from agent-session.ts.
4
+ * A default-off, advisory-only primitive for a future reasoning-router hint
5
+ * source. This module is intentionally NOT wired into agent-session.ts; it only
6
+ * implements the bridge's payload/result schema, sanitize/validate helpers, TTL
7
+ * cache, budget counter, circuit breaker, and timeout wrapper around a
8
+ * caller-injected advisory function. A future lane decides how (or whether) to
9
+ * fuse a returned hint into a resolved ThinkingLevel and to actually call this
10
+ * module from agent-session.ts.
12
11
  *
13
12
  * Grounded in the read-first plan at
14
13
  * .omk/goals/008-reasoning-router-advanced-accuracy-plan/laneC-privacy-adaptorch.md
@@ -27,16 +26,10 @@
27
26
  * `taskClass` and a `confidenceBand` - both closed enums - never a raw
28
27
  * numeric confidence or a `ThinkingLevel` value. Fusing a hint into a
29
28
  * resolved level is out of scope for this module.
30
- * - No product source (agent-session.ts, reasoning-router-v2/v3.ts,
31
- * reasoning-router-weights.ts) is imported here. `AdaptorchTaskClass` /
32
- * `AdaptorchLaneType` intentionally mirror `TaskClassV2` /
33
- * `ReasoningLaneTypeV2` from reasoning-router-v2.ts by value, not by
34
- * import, so this lane's narrow write scope (this file, its regression
35
- * test, and its evidence file only) cannot be affected by - or affect -
36
- * any concurrently-running Wave 1 lane's edits to the router modules. A
37
- * future wiring lane should keep these enums in sync (or replace the
38
- * local copies with a shared import) once it deliberately connects this
39
- * bridge to the router.
29
+ * - No product source is imported here. `AdaptorchTaskClass` /
30
+ * `AdaptorchLaneType` intentionally mirror the v4 task/lane unions by
31
+ * value, not by import, so this bridge remains isolated until a future
32
+ * wiring lane deliberately connects it to the router.
40
33
  * - Every outbound/inbound field is a closed enum, a boolean, or a bounded
41
34
  * integer, derived from local features only. There is structurally no
42
35
  * field shaped to carry prompt text, a prompt hash, a file/working-
@@ -56,15 +49,15 @@
56
49
  * Pure aside from `Date.now`/`setTimeout` (both injectable/wrapped so tests
57
50
  * never depend on real wall-clock time or a real MCP call).
58
51
  */
59
- /** Closed set of task classes. Mirrors TaskClassV2 in reasoning-router-v2.ts by value (see file header). */
52
+ /** Closed set of task classes. Mirrors the v4 task-class union by value (see file header). */
60
53
  export type AdaptorchTaskClass = "trivial" | "simple-edit" | "code-gen" | "debug" | "refactor" | "review" | "plan";
61
- /** Closed set of subagent lane types. Mirrors ReasoningLaneTypeV2 in reasoning-router-v2.ts by value. */
54
+ /** Closed set of subagent lane types. Mirrors the v4 lane union by value. */
62
55
  export type AdaptorchLaneType = "planner" | "security" | "explorer" | "coder" | "reviewer" | "tester";
63
56
  /** Local top1-vs-runner-up score-margin bucket (plan 4.5 ConsultPayloadV1.marginBucket). */
64
57
  export type AdaptorchMarginBucket = "tie" | "low" | "medium" | "high";
65
58
  /** Locally-derived confidence band for a returned hint. Never a raw network-supplied float (plan 4.3). */
66
59
  export type AdaptorchConfidenceBand = "low" | "medium" | "high";
67
- /** floor(log2(trimmedPromptLen+1)) clamped to [0,7] - same bucketing as reasoning-router-v2.ts's clampLenBucket. */
60
+ /** floor(log2(trimmedPromptLen+1)) clamped to [0,7]. */
68
61
  export type AdaptorchLenBucket = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
69
62
  /** Context-pressure band 0..3 - same bucketing as agent-session.ts's _computePressureBucket. */
70
63
  export type AdaptorchPressureBucket = 0 | 1 | 2 | 3;
@@ -1 +1 @@
1
- {"version":3,"file":"adaptorch-bridge.d.ts","sourceRoot":"","sources":["../../src/core/adaptorch-bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AAEH,4GAA4G;AAC5G,MAAM,MAAM,kBAAkB,GAAG,SAAS,GAAG,aAAa,GAAG,UAAU,GAAG,OAAO,GAAG,UAAU,GAAG,QAAQ,GAAG,MAAM,CAAC;AAEnH,yGAAyG;AACzG,MAAM,MAAM,iBAAiB,GAAG,SAAS,GAAG,UAAU,G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B,wFAAwF;IACxF,QAAQ,CAAC,qBAAqB,CAAC,EAAE,MAAM,CAAC;IACxC,iEAAiE;IACjE,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC,6EAA6E;IAC7E,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,gEAAgE;IAChE,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC;IACnC,uEAAuE;IACvE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,8FAA8F;AAC9F,MAAM,WAAW,oBAAoB;IACpC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;IAC9B,QAAQ,CAAC,kBAAkB,EAAE,MAAM,CAAC;IACpC,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;CACjC;AAED,MAAM,WAAW,eAAe;IAC/B;;;;;OAKG;IACH,YAAY,CAAC,OAAO,EAAE,uBAAuB,GAAG,uBAAuB,GAAG,IAAI,CAAC;IAC/E;;;;OAIG;IACH,cAAc,CAAC,OAAO,EAAE,uBAAuB,GAAG,IAAI,CAAC;IACvD;;;;;OAKG;IACH,OAAO,CAAC,OAAO,EAAE,uBAAuB,GAAG,OAAO,CAAC,uBAAuB,GAAG,IAAI,CAAC,CAAC;IACnF,4FAA4F;IAC5F,QAAQ,IAAI,oBAAoB,CAAC;CACjC;AAqJD,mGAAmG;AACnG,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,qBAAqB,GAAG,eAAe,CAEpF","sourcesContent":["/**\n * Reasoning-router AdaptOrch advisory bridge (Goal 009 Lane B).\n *\n * A default-off, advisory-only primitive for a future opt-in reasoning-router\n * hint source. This module is intentionally NOT wired into agent-session.ts,\n * reasoning-router-v2.ts, or reasoning-router-v3.ts by this lane - it only\n * implements the bridge's payload/result schema, sanitize/validate helpers,\n * TTL cache, budget counter, circuit breaker, and timeout wrapper around a\n * caller-injected advisory function. A future lane decides how (or whether)\n * to fuse a returned hint into a resolved ThinkingLevel and to actually call\n * this module from agent-session.ts.\n *\n * Grounded in the read-first plan at\n * .omk/goals/008-reasoning-router-advanced-accuracy-plan/laneC-privacy-adaptorch.md\n * (Part B, sections 4.1-4.11) and the real AdaptOrch MCP tool surface\n * documented in packages/adaptorch-wpl/src/adaptorch-client.ts. Key\n * constraints carried over from that plan:\n *\n * - The router's turn-start path is synchronous and I/O-free today (plan\n * 4.4). This module never calls an MCP transport directly and exposes a\n * synchronous, cache-read-only accessor (`getFreshHint`) plus a\n * fire-and-forget refresh (`requestRefresh`) so a future integration can\n * never block or await this module inline on the turn-start path.\n * - The real, documented AdaptOrch tool surface has no confidence or\n * reasoning-level field (plan 4.3); this module's inbound result type\n * (`AdaptorchAdvisoryResult`) therefore only carries a locally-relevant\n * `taskClass` and a `confidenceBand` - both closed enums - never a raw\n * numeric confidence or a `ThinkingLevel` value. Fusing a hint into a\n * resolved level is out of scope for this module.\n * - No product source (agent-session.ts, reasoning-router-v2/v3.ts,\n * reasoning-router-weights.ts) is imported here. `AdaptorchTaskClass` /\n * `AdaptorchLaneType` intentionally mirror `TaskClassV2` /\n * `ReasoningLaneTypeV2` from reasoning-router-v2.ts by value, not by\n * import, so this lane's narrow write scope (this file, its regression\n * test, and its evidence file only) cannot be affected by - or affect -\n * any concurrently-running Wave 1 lane's edits to the router modules. A\n * future wiring lane should keep these enums in sync (or replace the\n * local copies with a shared import) once it deliberately connects this\n * bridge to the router.\n * - Every outbound/inbound field is a closed enum, a boolean, or a bounded\n * integer, derived from local features only. There is structurally no\n * field shaped to carry prompt text, a prompt hash, a file/working-\n * directory path, a model/provider identifier, a session/user identifier,\n * a tool name, or hook output - the sanitize/validate helpers below also\n * reject any candidate object exposing such a field by name, and reject\n * any key outside the exact closed schema, so a hand-built or forged\n * payload can never smuggle one through either type.\n * - The router-bridge tool allowlist (`ADAPTORCH_READONLY_TOOL_ALLOWLIST`)\n * only ever contains the two read/local AdaptOrch tools the plan\n * identifies as in scope (the session-start capabilities probe and the\n * per-consult topology-classification call, plan 4.2/4.6); the\n * run-submission and run-cancellation tools (and any other mutating\n * tool) can never appear in it, checked both by an exact-value test and\n * by `looksMutatingToolName`'s verb-segment guard.\n *\n * Pure aside from `Date.now`/`setTimeout` (both injectable/wrapped so tests\n * never depend on real wall-clock time or a real MCP call).\n */\n\n/** Closed set of task classes. Mirrors TaskClassV2 in reasoning-router-v2.ts by value (see file header). */\nexport type AdaptorchTaskClass = \"trivial\" | \"simple-edit\" | \"code-gen\" | \"debug\" | \"refactor\" | \"review\" | \"plan\";\n\n/** Closed set of subagent lane types. Mirrors ReasoningLaneTypeV2 in reasoning-router-v2.ts by value. */\nexport type AdaptorchLaneType = \"planner\" | \"security\" | \"explorer\" | \"coder\" | \"reviewer\" | \"tester\";\n\n/** Local top1-vs-runner-up score-margin bucket (plan 4.5 ConsultPayloadV1.marginBucket). */\nexport type AdaptorchMarginBucket = \"tie\" | \"low\" | \"medium\" | \"high\";\n\n/** Locally-derived confidence band for a returned hint. Never a raw network-supplied float (plan 4.3). */\nexport type AdaptorchConfidenceBand = \"low\" | \"medium\" | \"high\";\n\n/** floor(log2(trimmedPromptLen+1)) clamped to [0,7] - same bucketing as reasoning-router-v2.ts's clampLenBucket. */\nexport type AdaptorchLenBucket = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;\n\n/** Context-pressure band 0..3 - same bucketing as agent-session.ts's _computePressureBucket. */\nexport type AdaptorchPressureBucket = 0 | 1 | 2 | 3;\n\n/**\n * Outbound advisory request (plan 4.5, field names adapted for this lane's\n * assignment: taskClass/runnerUp/marginBucket/lenBucket/hadFence/hadDiff/\n * pressureBucket/laneType). Every field is a closed enum, a boolean, or a\n * bounded integer built from local classifier features only. Structurally\n * excludes prompt text/hash, file paths, model/provider/session\n * identifiers, tool names, and hook output - there is no field shaped to\n * carry any of those.\n */\nexport interface AdaptorchConsultPayload {\n\treadonly schemaVersion: 1;\n\treadonly taskClass: AdaptorchTaskClass;\n\treadonly runnerUp: AdaptorchTaskClass;\n\treadonly marginBucket: AdaptorchMarginBucket;\n\treadonly lenBucket: AdaptorchLenBucket;\n\treadonly hadFence: boolean;\n\treadonly hadDiff: boolean;\n\treadonly pressureBucket: AdaptorchPressureBucket;\n\treadonly laneType?: AdaptorchLaneType;\n}\n\n/**\n * Inbound advisory result after local sanitation (plan 4.3, corrected\n * schema). `confidenceBand` is always synthesized/validated locally - the\n * real AdaptOrch tool contract carries no confidence or effort-level field,\n * so this type never trusts a raw numeric confidence from the advisory\n * function.\n */\nexport interface AdaptorchAdvisoryResult {\n\treadonly schemaVersion: 1;\n\treadonly taskClass: AdaptorchTaskClass;\n\treadonly confidenceBand: AdaptorchConfidenceBand;\n}\n\nconst TASK_CLASSES: readonly AdaptorchTaskClass[] = [\n\t\"trivial\",\n\t\"simple-edit\",\n\t\"code-gen\",\n\t\"debug\",\n\t\"refactor\",\n\t\"review\",\n\t\"plan\",\n];\nconst TASK_CLASS_SET: ReadonlySet<string> = new Set(TASK_CLASSES);\n\nconst LANE_TYPES: readonly AdaptorchLaneType[] = [\"planner\", \"security\", \"explorer\", \"coder\", \"reviewer\", \"tester\"];\nconst LANE_TYPE_SET: ReadonlySet<string> = new Set(LANE_TYPES);\n\nconst MARGIN_BUCKETS: readonly AdaptorchMarginBucket[] = [\"tie\", \"low\", \"medium\", \"high\"];\nconst MARGIN_BUCKET_SET: ReadonlySet<string> = new Set(MARGIN_BUCKETS);\n\nconst CONFIDENCE_BANDS: readonly AdaptorchConfidenceBand[] = [\"low\", \"medium\", \"high\"];\nconst CONFIDENCE_BAND_SET: ReadonlySet<string> = new Set(CONFIDENCE_BANDS);\n\nconst CONSULT_PAYLOAD_KEYS: ReadonlySet<string> = new Set([\n\t\"schemaVersion\",\n\t\"taskClass\",\n\t\"runnerUp\",\n\t\"marginBucket\",\n\t\"lenBucket\",\n\t\"hadFence\",\n\t\"hadDiff\",\n\t\"pressureBucket\",\n\t\"laneType\",\n]);\n\nconst ADVISORY_RESULT_KEYS: ReadonlySet<string> = new Set([\"schemaVersion\", \"taskClass\", \"confidenceBand\"]);\n\n/**\n * Field-name markers that must never appear on a payload or result object,\n * checked case-insensitively as a substring of every own key. This is\n * defense-in-depth alongside the exact-key allowlist below: even a candidate\n * that otherwise has every required field still fails if it also carries a\n * field shaped like a stored prompt, hash, path, model/provider identifier,\n * session identifier, tool name, or hook output.\n */\nconst FORBIDDEN_KEY_SUBSTRINGS: readonly string[] = [\n\t\"prompt\",\n\t\"hash\",\n\t\"path\",\n\t\"model\",\n\t\"session\",\n\t\"tool\",\n\t\"hook\",\n\t\"provider\",\n];\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/**\n * True if any own key of `record` contains a forbidden field-name marker\n * (case-insensitive). Typed `object` rather than `Record<string, unknown>` so\n * callers (including tests) can pass any concrete object shape - named\n * interface, ad-hoc literal, or an already-`Record`-typed value - without a\n * missing-index-signature type error; only `Object.keys` is used internally,\n * which accepts any `object`.\n */\nexport function containsForbiddenField(record: object): boolean {\n\tfor (const key of Object.keys(record)) {\n\t\tconst lowerKey = key.toLowerCase();\n\t\tif (FORBIDDEN_KEY_SUBSTRINGS.some((needle) => lowerKey.includes(needle))) return true;\n\t}\n\treturn false;\n}\n\nfunction hasOnlyAllowedKeys(record: Record<string, unknown>, allowed: ReadonlySet<string>): boolean {\n\treturn Object.keys(record).every((key) => allowed.has(key));\n}\n\nfunction clampInt(value: number, min: number, max: number): number {\n\tconst truncated = Number.isFinite(value) ? Math.trunc(value) : min;\n\tif (truncated < min) return min;\n\tif (truncated > max) return max;\n\treturn truncated;\n}\n\nfunction clampLenBucket(value: number): AdaptorchLenBucket {\n\treturn clampInt(value, 0, 7) as AdaptorchLenBucket;\n}\n\nfunction clampPressureBucket(value: number): AdaptorchPressureBucket {\n\treturn clampInt(value, 0, 3) as AdaptorchPressureBucket;\n}\n\n/** `value ?? fallback`, coerced to an integer and floored at `min` (used for config, not untrusted payloads). */\nfunction boundedIntOrDefault(value: number | undefined, fallback: number, min: number): number {\n\tconst raw = value ?? fallback;\n\tif (!Number.isFinite(raw)) return fallback;\n\treturn Math.max(min, Math.trunc(raw));\n}\n\n/**\n * Sanitize/validate an outbound advisory request. Returns `null` (never\n * throws) when `candidate` is not a plain object, carries a forbidden-named\n * field, carries any key outside the exact closed schema, or has a\n * wrong-typed/unknown-enum field. `lenBucket`/`pressureBucket` are clamped\n * into their bounded ranges rather than rejected, so a caller-side rounding\n * or edge-case computation can never smuggle an out-of-range integer through.\n */\nexport function sanitizeConsultPayload(candidate: unknown): AdaptorchConsultPayload | null {\n\tif (!isPlainObject(candidate)) return null;\n\tif (containsForbiddenField(candidate)) return null;\n\tif (!hasOnlyAllowedKeys(candidate, CONSULT_PAYLOAD_KEYS)) return null;\n\n\tif (candidate.schemaVersion !== 1) return null;\n\n\tconst taskClass = candidate.taskClass;\n\tif (typeof taskClass !== \"string\" || !TASK_CLASS_SET.has(taskClass)) return null;\n\n\tconst runnerUp = candidate.runnerUp;\n\tif (typeof runnerUp !== \"string\" || !TASK_CLASS_SET.has(runnerUp)) return null;\n\n\tconst marginBucket = candidate.marginBucket;\n\tif (typeof marginBucket !== \"string\" || !MARGIN_BUCKET_SET.has(marginBucket)) return null;\n\n\tconst lenBucketRaw = candidate.lenBucket;\n\tif (typeof lenBucketRaw !== \"number\") return null;\n\n\tconst pressureBucketRaw = candidate.pressureBucket;\n\tif (typeof pressureBucketRaw !== \"number\") return null;\n\n\tconst hadFence = candidate.hadFence;\n\tif (typeof hadFence !== \"boolean\") return null;\n\n\tconst hadDiff = candidate.hadDiff;\n\tif (typeof hadDiff !== \"boolean\") return null;\n\n\tconst laneTypeRaw = candidate.laneType;\n\tlet laneType: AdaptorchLaneType | undefined;\n\tif (laneTypeRaw !== undefined) {\n\t\tif (typeof laneTypeRaw !== \"string\" || !LANE_TYPE_SET.has(laneTypeRaw)) return null;\n\t\tlaneType = laneTypeRaw as AdaptorchLaneType;\n\t}\n\n\treturn {\n\t\tschemaVersion: 1,\n\t\ttaskClass: taskClass as AdaptorchTaskClass,\n\t\trunnerUp: runnerUp as AdaptorchTaskClass,\n\t\tmarginBucket: marginBucket as AdaptorchMarginBucket,\n\t\tlenBucket: clampLenBucket(lenBucketRaw),\n\t\thadFence,\n\t\thadDiff,\n\t\tpressureBucket: clampPressureBucket(pressureBucketRaw),\n\t\t...(laneType !== undefined ? { laneType } : {}),\n\t};\n}\n\n/**\n * Sanitize/validate an inbound advisory result (i.e. whatever the injected\n * advisory function returned). Returns `null` (never throws) on any shape,\n * forbidden-field, extra-key, or unknown-enum violation - the caller\n * (`AdaptorchBridge.consult`) treats `null` as \"no hint\" and falls back\n * silently, exactly like every other failure mode.\n */\nexport function sanitizeAdvisoryResult(candidate: unknown): AdaptorchAdvisoryResult | null {\n\tif (!isPlainObject(candidate)) return null;\n\tif (containsForbiddenField(candidate)) return null;\n\tif (!hasOnlyAllowedKeys(candidate, ADVISORY_RESULT_KEYS)) return null;\n\n\tif (candidate.schemaVersion !== 1) return null;\n\n\tconst taskClass = candidate.taskClass;\n\tif (typeof taskClass !== \"string\" || !TASK_CLASS_SET.has(taskClass)) return null;\n\n\tconst confidenceBand = candidate.confidenceBand;\n\tif (typeof confidenceBand !== \"string\" || !CONFIDENCE_BAND_SET.has(confidenceBand)) return null;\n\n\treturn {\n\t\tschemaVersion: 1,\n\t\ttaskClass: taskClass as AdaptorchTaskClass,\n\t\tconfidenceBand: confidenceBand as AdaptorchConfidenceBand,\n\t};\n}\n\n/**\n * Read-only/local AdaptOrch MCP tool names this bridge may ever reference,\n * grounded in the real 10-tool surface documented in\n * packages/adaptorch-wpl/src/adaptorch-client.ts (cross-checked there\n * against the AdaptOrch server's own docs) and narrowed to the two tools the\n * read-first plan puts in scope for a reasoning-effort hint (plan 4.2): the\n * session-start capabilities probe and the per-consult topology\n * classification call. This module does not call any MCP tool directly\n * today - the advisory function is fully caller-injected - so this\n * allowlist is not wired to a transport here; it exists so a future\n * transport, and this module's own tests, can assert by construction that\n * `adaptorch_run` (submit) and `adaptorch_cancel_run` (cancel), or any other\n * mutating tool, can never be reachable from router/bridge code.\n */\nexport const ADAPTORCH_READONLY_TOOL_ALLOWLIST = [\"adaptorch_capabilities\", \"adaptorch_route_topology\"] as const;\n\nexport type AdaptorchReadonlyToolName = (typeof ADAPTORCH_READONLY_TOOL_ALLOWLIST)[number];\n\n/** Underscore-segment verbs that mark an AdaptOrch-shaped tool name as mutating (write/execute/lifecycle). */\nconst MUTATING_TOOL_VERB_SEGMENTS: ReadonlySet<string> = new Set([\n\t\"run\",\n\t\"submit\",\n\t\"cancel\",\n\t\"dispatch\",\n\t\"delete\",\n\t\"execute\",\n\t\"mutate\",\n\t\"write\",\n\t\"create\",\n\t\"update\",\n\t\"remove\",\n]);\n\n/**\n * True if any underscore-delimited segment of an AdaptOrch-shaped tool name\n * is a known mutating verb. Segment-based rather than a raw substring/regex\n * match so \"adaptorch_route_topology\" is never falsely flagged (no segment\n * is a mutating verb), while \"adaptorch_run\" and \"adaptorch_cancel_run\" are\n * always caught regardless of future prefix/suffix changes.\n */\nexport function looksMutatingToolName(name: string): boolean {\n\treturn name\n\t\t.toLowerCase()\n\t\t.split(\"_\")\n\t\t.some((segment) => MUTATING_TOOL_VERB_SEGMENTS.has(segment));\n}\n\n/**\n * Type guard: true only for the two allowlisted read/local AdaptOrch tool\n * names. Also independently rejects any name `looksMutatingToolName` flags,\n * so a future accidental addition of a mutating name to the allowlist array\n * still cannot make this function report it as allowed.\n */\nexport function isAdaptorchToolAllowed(name: string): name is AdaptorchReadonlyToolName {\n\tif (looksMutatingToolName(name)) return false;\n\treturn (ADAPTORCH_READONLY_TOOL_ALLOWLIST as readonly string[]).includes(name);\n}\n\n/**\n * Timeout wrapper interface around any injected async advisory call. Races\n * the call against a timer; a slow or hung call rejects at `timeoutMs`\n * instead of leaving the caller waiting indefinitely. Always clears the\n * timer once the call settles (or once the timeout itself fires), and\n * always resolves/rejects exactly once - a synchronous throw from `run` is\n * treated the same as an asynchronous rejection.\n */\nexport type AdvisoryTimeoutWrapper = <T>(run: () => Promise<T>, timeoutMs: number) => Promise<T>;\n\nexport const withAdvisoryTimeout: AdvisoryTimeoutWrapper = <T>(\n\trun: () => Promise<T>,\n\ttimeoutMs: number,\n): Promise<T> => {\n\treturn new Promise<T>((resolve, reject) => {\n\t\tconst timer = setTimeout(() => {\n\t\t\treject(new Error(\"adaptorch-bridge: advisory call timed out\"));\n\t\t}, timeoutMs);\n\n\t\tlet pending: Promise<T>;\n\t\ttry {\n\t\t\tpending = run();\n\t\t} catch (err) {\n\t\t\tclearTimeout(timer);\n\t\t\treject(err instanceof Error ? err : new Error(String(err)));\n\t\t\treturn;\n\t\t}\n\n\t\tpending.then(\n\t\t\t(value) => {\n\t\t\t\tclearTimeout(timer);\n\t\t\t\tresolve(value);\n\t\t\t},\n\t\t\t(err: unknown) => {\n\t\t\t\tclearTimeout(timer);\n\t\t\t\treject(err instanceof Error ? err : new Error(String(err)));\n\t\t\t},\n\t\t);\n\t});\n};\n\nconst DEFAULT_TTL_MS = 5 * 60 * 1000;\nconst MIN_TTL_MS = 30 * 1000;\nconst MAX_TTL_MS = 5 * 60 * 1000;\nconst DEFAULT_MAX_TURNS_PER_ENTRY = 10;\nconst DEFAULT_CACHE_SIZE = 16;\nconst DEFAULT_MAX_CONSULTS_PER_SESSION = 5;\nconst DEFAULT_MIN_INTERVAL_MS = 60 * 1000;\nconst DEFAULT_TIMEOUT_MS = 1500;\nconst MIN_TIMEOUT_MS = 1;\nconst MAX_TIMEOUT_MS = 30 * 1000;\nconst DEFAULT_FAILURE_THRESHOLD = 3;\nconst DEFAULT_COOLDOWN_MS = 10 * 60 * 1000;\n\n/**\n * Exact TTL/cache/budget/circuit-breaker defaults from the read-first plan,\n * section 4.8. Exported so a security/review lane can confirm the shipped\n * numbers without reading this module's internals.\n */\nexport const ADAPTORCH_BRIDGE_DEFAULTS = {\n\tttlMs: DEFAULT_TTL_MS,\n\tminTtlMs: MIN_TTL_MS,\n\tmaxTtlMs: MAX_TTL_MS,\n\tmaxTurnsPerEntry: DEFAULT_MAX_TURNS_PER_ENTRY,\n\tcacheSize: DEFAULT_CACHE_SIZE,\n\tmaxConsultsPerSession: DEFAULT_MAX_CONSULTS_PER_SESSION,\n\tminIntervalMs: DEFAULT_MIN_INTERVAL_MS,\n\ttimeoutMs: DEFAULT_TIMEOUT_MS,\n\tminTimeoutMs: MIN_TIMEOUT_MS,\n\tmaxTimeoutMs: MAX_TIMEOUT_MS,\n\tfailureThreshold: DEFAULT_FAILURE_THRESHOLD,\n\tcooldownMs: DEFAULT_COOLDOWN_MS,\n} as const;\n\n/**\n * Bridge configuration. `advisoryFn` is the ONLY seam to any external\n * system - this module never imports or calls an MCP client/transport\n * itself, so it is impossible for a unit test (or a misconfigured caller)\n * to reach a real MCP server through this module by accident.\n */\nexport interface AdaptorchBridgeConfig {\n\t/**\n\t * Caller-injected advisory call. Its return value is untrusted: `consult`\n\t * always runs it through `sanitizeAdvisoryResult` before using or caching\n\t * it, and a rejected/thrown/timed-out/schema-invalid response is treated\n\t * as an ordinary failure (counts toward the circuit breaker), never as a\n\t * value to fuse.\n\t */\n\treadonly advisoryFn: (payload: AdaptorchConsultPayload) => Promise<unknown>;\n\t/** Injectable clock; defaults to `Date.now`. Tests supply a fake to avoid real wall-clock waits. */\n\treadonly now?: () => number;\n\t/** Cache TTL in ms; clamped to [30s, 5min] (plan 4.8). Default 5 minutes. */\n\treadonly ttlMs?: number;\n\t/** A cache entry also expires after this many `getFreshHint` calls (\"turns\"). Default 10. */\n\treadonly maxTurnsPerEntry?: number;\n\t/** Max distinct cached payload keys (LRU eviction beyond this). Default 16. */\n\treadonly cacheSize?: number;\n\t/** Max advisory attempts for this bridge instance's lifetime (\"session\"). Default 5. */\n\treadonly maxConsultsPerSession?: number;\n\t/** Minimum ms between advisory attempts. Default 60000 (60s). */\n\treadonly minIntervalMs?: number;\n\t/** Per-attempt timeout in ms; clamped to [1ms, 30s]. Default 1500 (1.5s). */\n\treadonly timeoutMs?: number;\n\t/** Consecutive failures before the circuit opens. Default 3. */\n\treadonly failureThreshold?: number;\n\t/** Circuit-open duration in ms once opened. Default 600000 (10min). */\n\treadonly cooldownMs?: number;\n}\n\n/** Read-only, non-sensitive introspection snapshot - never exposes payload/result content. */\nexport interface AdaptorchBridgeStats {\n\treadonly consultCount: number;\n\treadonly failureStreak: number;\n\treadonly circuitOpen: boolean;\n\treadonly circuitOpenUntilMs: number;\n\treadonly cacheEntryCount: number;\n}\n\nexport interface AdaptorchBridge {\n\t/**\n\t * Synchronous, cache-read-only. Never blocks, never throws, and never\n\t * calls the advisory function - a cache miss or an expired/invalid entry\n\t * silently returns `null` (\"no hint\"), matching the turn-start path's\n\t * synchronous, I/O-free contract (plan 4.4).\n\t */\n\tgetFreshHint(payload: AdaptorchConsultPayload): AdaptorchAdvisoryResult | null;\n\t/**\n\t * Fire-and-forget refresh for a LATER call to `getFreshHint`, never the\n\t * current one. Never throws synchronously and never rejects into the\n\t * caller - any failure is swallowed internally by `consult`.\n\t */\n\trequestRefresh(payload: AdaptorchConsultPayload): void;\n\t/**\n\t * The awaitable core operation `requestRefresh` fires without awaiting.\n\t * Gated by the circuit breaker, budget counter, and minimum interval (in\n\t * that order) before ever invoking `advisoryFn`; always resolves (never\n\t * rejects) to either a sanitized hint or `null`.\n\t */\n\tconsult(payload: AdaptorchConsultPayload): Promise<AdaptorchAdvisoryResult | null>;\n\t/** Read-only introspection for tests/diagnostics; contains no payload or result content. */\n\tgetStats(): AdaptorchBridgeStats;\n}\n\ninterface CacheEntry {\n\treadonly result: AdaptorchAdvisoryResult;\n\treadonly insertedAtMs: number;\n\treadonly insertedAtTurn: number;\n}\n\n/** Deterministic cache key built field-by-field from a sanitized payload - never from raw prompt text. */\nfunction cacheKeyFor(payload: AdaptorchConsultPayload): string {\n\treturn JSON.stringify({\n\t\ttaskClass: payload.taskClass,\n\t\trunnerUp: payload.runnerUp,\n\t\tmarginBucket: payload.marginBucket,\n\t\tlenBucket: payload.lenBucket,\n\t\thadFence: payload.hadFence,\n\t\thadDiff: payload.hadDiff,\n\t\tpressureBucket: payload.pressureBucket,\n\t\tlaneType: payload.laneType ?? null,\n\t});\n}\n\nclass AdaptorchBridgeImpl implements AdaptorchBridge {\n\tprivate readonly advisoryFn: (payload: AdaptorchConsultPayload) => Promise<unknown>;\n\tprivate readonly now: () => number;\n\tprivate readonly ttlMs: number;\n\tprivate readonly maxTurnsPerEntry: number;\n\tprivate readonly cacheSize: number;\n\tprivate readonly maxConsultsPerSession: number;\n\tprivate readonly minIntervalMs: number;\n\tprivate readonly timeoutMs: number;\n\tprivate readonly failureThreshold: number;\n\tprivate readonly cooldownMs: number;\n\n\tprivate readonly cache = new Map<string, CacheEntry>();\n\tprivate turnCounter = 0;\n\tprivate consultCount = 0;\n\tprivate failureStreak = 0;\n\tprivate lastAttemptAtMs: number | null = null;\n\tprivate circuitOpenUntilMs = 0;\n\n\tconstructor(config: AdaptorchBridgeConfig) {\n\t\tthis.advisoryFn = config.advisoryFn;\n\t\tthis.now = config.now ?? Date.now;\n\t\tthis.ttlMs = clampInt(config.ttlMs ?? DEFAULT_TTL_MS, MIN_TTL_MS, MAX_TTL_MS);\n\t\tthis.maxTurnsPerEntry = boundedIntOrDefault(config.maxTurnsPerEntry, DEFAULT_MAX_TURNS_PER_ENTRY, 1);\n\t\tthis.cacheSize = boundedIntOrDefault(config.cacheSize, DEFAULT_CACHE_SIZE, 1);\n\t\tthis.maxConsultsPerSession = boundedIntOrDefault(\n\t\t\tconfig.maxConsultsPerSession,\n\t\t\tDEFAULT_MAX_CONSULTS_PER_SESSION,\n\t\t\t0,\n\t\t);\n\t\tthis.minIntervalMs = boundedIntOrDefault(config.minIntervalMs, DEFAULT_MIN_INTERVAL_MS, 0);\n\t\tthis.timeoutMs = clampInt(config.timeoutMs ?? DEFAULT_TIMEOUT_MS, MIN_TIMEOUT_MS, MAX_TIMEOUT_MS);\n\t\tthis.failureThreshold = boundedIntOrDefault(config.failureThreshold, DEFAULT_FAILURE_THRESHOLD, 1);\n\t\tthis.cooldownMs = boundedIntOrDefault(config.cooldownMs, DEFAULT_COOLDOWN_MS, 0);\n\t}\n\n\tgetFreshHint(payload: AdaptorchConsultPayload): AdaptorchAdvisoryResult | null {\n\t\tthis.turnCounter += 1;\n\t\tconst sanitizedPayload = sanitizeConsultPayload(payload);\n\t\tif (sanitizedPayload === null) return null;\n\n\t\tconst key = cacheKeyFor(sanitizedPayload);\n\t\tconst entry = this.cache.get(key);\n\t\tif (entry === undefined) return null;\n\t\tif (!this.isEntryFresh(entry)) {\n\t\t\tthis.cache.delete(key);\n\t\t\treturn null;\n\t\t}\n\t\treturn entry.result;\n\t}\n\n\trequestRefresh(payload: AdaptorchConsultPayload): void {\n\t\tvoid this.consult(payload).catch(() => {\n\t\t\t// consult() already swallows every failure and always resolves; this\n\t\t\t// catch only guards a future refactor from letting one escape, so a\n\t\t\t// fire-and-forget refresh can never surface an unhandled rejection.\n\t\t});\n\t}\n\n\tasync consult(payload: AdaptorchConsultPayload): Promise<AdaptorchAdvisoryResult | null> {\n\t\tconst sanitizedPayload = sanitizeConsultPayload(payload);\n\t\tif (sanitizedPayload === null) return null;\n\n\t\tconst nowMs = this.now();\n\t\tif (nowMs < this.circuitOpenUntilMs) return null;\n\t\tif (this.consultCount >= this.maxConsultsPerSession) return null;\n\t\tif (this.lastAttemptAtMs !== null && nowMs - this.lastAttemptAtMs < this.minIntervalMs) return null;\n\n\t\tthis.consultCount += 1;\n\t\tthis.lastAttemptAtMs = nowMs;\n\n\t\tlet raw: unknown;\n\t\ttry {\n\t\t\traw = await withAdvisoryTimeout(() => this.advisoryFn(sanitizedPayload), this.timeoutMs);\n\t\t} catch {\n\t\t\tthis.recordFailure();\n\t\t\treturn null;\n\t\t}\n\n\t\tconst result = sanitizeAdvisoryResult(raw);\n\t\tif (result === null) {\n\t\t\tthis.recordFailure();\n\t\t\treturn null;\n\t\t}\n\n\t\tthis.failureStreak = 0;\n\t\tthis.cacheResult(sanitizedPayload, result);\n\t\treturn result;\n\t}\n\n\tgetStats(): AdaptorchBridgeStats {\n\t\tconst nowMs = this.now();\n\t\treturn {\n\t\t\tconsultCount: this.consultCount,\n\t\t\tfailureStreak: this.failureStreak,\n\t\t\tcircuitOpen: nowMs < this.circuitOpenUntilMs,\n\t\t\tcircuitOpenUntilMs: this.circuitOpenUntilMs,\n\t\t\tcacheEntryCount: this.cache.size,\n\t\t};\n\t}\n\n\tprivate recordFailure(): void {\n\t\tthis.failureStreak += 1;\n\t\tif (this.failureStreak >= this.failureThreshold) {\n\t\t\tthis.circuitOpenUntilMs = this.now() + this.cooldownMs;\n\t\t}\n\t}\n\n\tprivate isEntryFresh(entry: CacheEntry): boolean {\n\t\tconst ageMs = this.now() - entry.insertedAtMs;\n\t\tconst ageTurns = this.turnCounter - entry.insertedAtTurn;\n\t\treturn ageMs < this.ttlMs && ageTurns < this.maxTurnsPerEntry;\n\t}\n\n\t/** Eviction favors entries last refreshed by a successful consult (write-time LRU, not read-time). */\n\tprivate cacheResult(payload: AdaptorchConsultPayload, result: AdaptorchAdvisoryResult): void {\n\t\tconst key = cacheKeyFor(payload);\n\t\tif (this.cache.has(key)) this.cache.delete(key);\n\t\tthis.cache.set(key, { result, insertedAtMs: this.now(), insertedAtTurn: this.turnCounter });\n\t\twhile (this.cache.size > this.cacheSize) {\n\t\t\tconst oldestKey = this.cache.keys().next().value;\n\t\t\tif (oldestKey === undefined) break;\n\t\t\tthis.cache.delete(oldestKey);\n\t\t}\n\t}\n}\n\n/** Construct a default-off AdaptOrch advisory bridge. See `AdaptorchBridgeConfig` for tunables. */\nexport function createAdaptorchBridge(config: AdaptorchBridgeConfig): AdaptorchBridge {\n\treturn new AdaptorchBridgeImpl(config);\n}\n"]}
1
+ {"version":3,"file":"adaptorch-bridge.d.ts","s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F,QAAQ,IAAI,oBAAoB,CAAC;CACjC;AAqJD,mGAAmG;AACnG,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,qBAAqB,GAAG,eAAe,CAEpF","sourcesContent":["/**\n * Reasoning-router AdaptOrch advisory bridge (Goal 009 Lane B).\n *\n * A default-off, advisory-only primitive for a future reasoning-router hint\n * source. This module is intentionally NOT wired into agent-session.ts; it only\n * implements the bridge's payload/result schema, sanitize/validate helpers, TTL\n * cache, budget counter, circuit breaker, and timeout wrapper around a\n * caller-injected advisory function. A future lane decides how (or whether) to\n * fuse a returned hint into a resolved ThinkingLevel and to actually call this\n * module from agent-session.ts.\n *\n * Grounded in the read-first plan at\n * .omk/goals/008-reasoning-router-advanced-accuracy-plan/laneC-privacy-adaptorch.md\n * (Part B, sections 4.1-4.11) and the real AdaptOrch MCP tool surface\n * documented in packages/adaptorch-wpl/src/adaptorch-client.ts. Key\n * constraints carried over from that plan:\n *\n * - The router's turn-start path is synchronous and I/O-free today (plan\n * 4.4). This module never calls an MCP transport directly and exposes a\n * synchronous, cache-read-only accessor (`getFreshHint`) plus a\n * fire-and-forget refresh (`requestRefresh`) so a future integration can\n * never block or await this module inline on the turn-start path.\n * - The real, documented AdaptOrch tool surface has no confidence or\n * reasoning-level field (plan 4.3); this module's inbound result type\n * (`AdaptorchAdvisoryResult`) therefore only carries a locally-relevant\n * `taskClass` and a `confidenceBand` - both closed enums - never a raw\n * numeric confidence or a `ThinkingLevel` value. Fusing a hint into a\n * resolved level is out of scope for this module.\n * - No product source is imported here. `AdaptorchTaskClass` /\n * `AdaptorchLaneType` intentionally mirror the v4 task/lane unions by\n * value, not by import, so this bridge remains isolated until a future\n * wiring lane deliberately connects it to the router.\n * - Every outbound/inbound field is a closed enum, a boolean, or a bounded\n * integer, derived from local features only. There is structurally no\n * field shaped to carry prompt text, a prompt hash, a file/working-\n * directory path, a model/provider identifier, a session/user identifier,\n * a tool name, or hook output - the sanitize/validate helpers below also\n * reject any candidate object exposing such a field by name, and reject\n * any key outside the exact closed schema, so a hand-built or forged\n * payload can never smuggle one through either type.\n * - The router-bridge tool allowlist (`ADAPTORCH_READONLY_TOOL_ALLOWLIST`)\n * only ever contains the two read/local AdaptOrch tools the plan\n * identifies as in scope (the session-start capabilities probe and the\n * per-consult topology-classification call, plan 4.2/4.6); the\n * run-submission and run-cancellation tools (and any other mutating\n * tool) can never appear in it, checked both by an exact-value test and\n * by `looksMutatingToolName`'s verb-segment guard.\n *\n * Pure aside from `Date.now`/`setTimeout` (both injectable/wrapped so tests\n * never depend on real wall-clock time or a real MCP call).\n */\n\n/** Closed set of task classes. Mirrors the v4 task-class union by value (see file header). */\nexport type AdaptorchTaskClass = \"trivial\" | \"simple-edit\" | \"code-gen\" | \"debug\" | \"refactor\" | \"review\" | \"plan\";\n\n/** Closed set of subagent lane types. Mirrors the v4 lane union by value. */\nexport type AdaptorchLaneType = \"planner\" | \"security\" | \"explorer\" | \"coder\" | \"reviewer\" | \"tester\";\n\n/** Local top1-vs-runner-up score-margin bucket (plan 4.5 ConsultPayloadV1.marginBucket). */\nexport type AdaptorchMarginBucket = \"tie\" | \"low\" | \"medium\" | \"high\";\n\n/** Locally-derived confidence band for a returned hint. Never a raw network-supplied float (plan 4.3). */\nexport type AdaptorchConfidenceBand = \"low\" | \"medium\" | \"high\";\n\n/** floor(log2(trimmedPromptLen+1)) clamped to [0,7]. */\nexport type AdaptorchLenBucket = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;\n\n/** Context-pressure band 0..3 - same bucketing as agent-session.ts's _computePressureBucket. */\nexport type AdaptorchPressureBucket = 0 | 1 | 2 | 3;\n\n/**\n * Outbound advisory request (plan 4.5, field names adapted for this lane's\n * assignment: taskClass/runnerUp/marginBucket/lenBucket/hadFence/hadDiff/\n * pressureBucket/laneType). Every field is a closed enum, a boolean, or a\n * bounded integer built from local classifier features only. Structurally\n * excludes prompt text/hash, file paths, model/provider/session\n * identifiers, tool names, and hook output - there is no field shaped to\n * carry any of those.\n */\nexport interface AdaptorchConsultPayload {\n\treadonly schemaVersion: 1;\n\treadonly taskClass: AdaptorchTaskClass;\n\treadonly runnerUp: AdaptorchTaskClass;\n\treadonly marginBucket: AdaptorchMarginBucket;\n\treadonly lenBucket: AdaptorchLenBucket;\n\treadonly hadFence: boolean;\n\treadonly hadDiff: boolean;\n\treadonly pressureBucket: AdaptorchPressureBucket;\n\treadonly laneType?: AdaptorchLaneType;\n}\n\n/**\n * Inbound advisory result after local sanitation (plan 4.3, corrected\n * schema). `confidenceBand` is always synthesized/validated locally - the\n * real AdaptOrch tool contract carries no confidence or effort-level field,\n * so this type never trusts a raw numeric confidence from the advisory\n * function.\n */\nexport interface AdaptorchAdvisoryResult {\n\treadonly schemaVersion: 1;\n\treadonly taskClass: AdaptorchTaskClass;\n\treadonly confidenceBand: AdaptorchConfidenceBand;\n}\n\nconst TASK_CLASSES: readonly AdaptorchTaskClass[] = [\n\t\"trivial\",\n\t\"simple-edit\",\n\t\"code-gen\",\n\t\"debug\",\n\t\"refactor\",\n\t\"review\",\n\t\"plan\",\n];\nconst TASK_CLASS_SET: ReadonlySet<string> = new Set(TASK_CLASSES);\n\nconst LANE_TYPES: readonly AdaptorchLaneType[] = [\"planner\", \"security\", \"explorer\", \"coder\", \"reviewer\", \"tester\"];\nconst LANE_TYPE_SET: ReadonlySet<string> = new Set(LANE_TYPES);\n\nconst MARGIN_BUCKETS: readonly AdaptorchMarginBucket[] = [\"tie\", \"low\", \"medium\", \"high\"];\nconst MARGIN_BUCKET_SET: ReadonlySet<string> = new Set(MARGIN_BUCKETS);\n\nconst CONFIDENCE_BANDS: readonly AdaptorchConfidenceBand[] = [\"low\", \"medium\", \"high\"];\nconst CONFIDENCE_BAND_SET: ReadonlySet<string> = new Set(CONFIDENCE_BANDS);\n\nconst CONSULT_PAYLOAD_KEYS: ReadonlySet<string> = new Set([\n\t\"schemaVersion\",\n\t\"taskClass\",\n\t\"runnerUp\",\n\t\"marginBucket\",\n\t\"lenBucket\",\n\t\"hadFence\",\n\t\"hadDiff\",\n\t\"pressureBucket\",\n\t\"laneType\",\n]);\n\nconst ADVISORY_RESULT_KEYS: ReadonlySet<string> = new Set([\"schemaVersion\", \"taskClass\", \"confidenceBand\"]);\n\n/**\n * Field-name markers that must never appear on a payload or result object,\n * checked case-insensitively as a substring of every own key. This is\n * defense-in-depth alongside the exact-key allowlist below: even a candidate\n * that otherwise has every required field still fails if it also carries a\n * field shaped like a stored prompt, hash, path, model/provider identifier,\n * session identifier, tool name, or hook output.\n */\nconst FORBIDDEN_KEY_SUBSTRINGS: readonly string[] = [\n\t\"prompt\",\n\t\"hash\",\n\t\"path\",\n\t\"model\",\n\t\"session\",\n\t\"tool\",\n\t\"hook\",\n\t\"provider\",\n];\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/**\n * True if any own key of `record` contains a forbidden field-name marker\n * (case-insensitive). Typed `object` rather than `Record<string, unknown>` so\n * callers (including tests) can pass any concrete object shape - named\n * interface, ad-hoc literal, or an already-`Record`-typed value - without a\n * missing-index-signature type error; only `Object.keys` is used internally,\n * which accepts any `object`.\n */\nexport function containsForbiddenField(record: object): boolean {\n\tfor (const key of Object.keys(record)) {\n\t\tconst lowerKey = key.toLowerCase();\n\t\tif (FORBIDDEN_KEY_SUBSTRINGS.some((needle) => lowerKey.includes(needle))) return true;\n\t}\n\treturn false;\n}\n\nfunction hasOnlyAllowedKeys(record: Record<string, unknown>, allowed: ReadonlySet<string>): boolean {\n\treturn Object.keys(record).every((key) => allowed.has(key));\n}\n\nfunction clampInt(value: number, min: number, max: number): number {\n\tconst truncated = Number.isFinite(value) ? Math.trunc(value) : min;\n\tif (truncated < min) return min;\n\tif (truncated > max) return max;\n\treturn truncated;\n}\n\nfunction clampLenBucket(value: number): AdaptorchLenBucket {\n\treturn clampInt(value, 0, 7) as AdaptorchLenBucket;\n}\n\nfunction clampPressureBucket(value: number): AdaptorchPressureBucket {\n\treturn clampInt(value, 0, 3) as AdaptorchPressureBucket;\n}\n\n/** `value ?? fallback`, coerced to an integer and floored at `min` (used for config, not untrusted payloads). */\nfunction boundedIntOrDefault(value: number | undefined, fallback: number, min: number): number {\n\tconst raw = value ?? fallback;\n\tif (!Number.isFinite(raw)) return fallback;\n\treturn Math.max(min, Math.trunc(raw));\n}\n\n/**\n * Sanitize/validate an outbound advisory request. Returns `null` (never\n * throws) when `candidate` is not a plain object, carries a forbidden-named\n * field, carries any key outside the exact closed schema, or has a\n * wrong-typed/unknown-enum field. `lenBucket`/`pressureBucket` are clamped\n * into their bounded ranges rather than rejected, so a caller-side rounding\n * or edge-case computation can never smuggle an out-of-range integer through.\n */\nexport function sanitizeConsultPayload(candidate: unknown): AdaptorchConsultPayload | null {\n\tif (!isPlainObject(candidate)) return null;\n\tif (containsForbiddenField(candidate)) return null;\n\tif (!hasOnlyAllowedKeys(candidate, CONSULT_PAYLOAD_KEYS)) return null;\n\n\tif (candidate.schemaVersion !== 1) return null;\n\n\tconst taskClass = candidate.taskClass;\n\tif (typeof taskClass !== \"string\" || !TASK_CLASS_SET.has(taskClass)) return null;\n\n\tconst runnerUp = candidate.runnerUp;\n\tif (typeof runnerUp !== \"string\" || !TASK_CLASS_SET.has(runnerUp)) return null;\n\n\tconst marginBucket = candidate.marginBucket;\n\tif (typeof marginBucket !== \"string\" || !MARGIN_BUCKET_SET.has(marginBucket)) return null;\n\n\tconst lenBucketRaw = candidate.lenBucket;\n\tif (typeof lenBucketRaw !== \"number\") return null;\n\n\tconst pressureBucketRaw = candidate.pressureBucket;\n\tif (typeof pressureBucketRaw !== \"number\") return null;\n\n\tconst hadFence = candidate.hadFence;\n\tif (typeof hadFence !== \"boolean\") return null;\n\n\tconst hadDiff = candidate.hadDiff;\n\tif (typeof hadDiff !== \"boolean\") return null;\n\n\tconst laneTypeRaw = candidate.laneType;\n\tlet laneType: AdaptorchLaneType | undefined;\n\tif (laneTypeRaw !== undefined) {\n\t\tif (typeof laneTypeRaw !== \"string\" || !LANE_TYPE_SET.has(laneTypeRaw)) return null;\n\t\tlaneType = laneTypeRaw as AdaptorchLaneType;\n\t}\n\n\treturn {\n\t\tschemaVersion: 1,\n\t\ttaskClass: taskClass as AdaptorchTaskClass,\n\t\trunnerUp: runnerUp as AdaptorchTaskClass,\n\t\tmarginBucket: marginBucket as AdaptorchMarginBucket,\n\t\tlenBucket: clampLenBucket(lenBucketRaw),\n\t\thadFence,\n\t\thadDiff,\n\t\tpressureBucket: clampPressureBucket(pressureBucketRaw),\n\t\t...(laneType !== undefined ? { laneType } : {}),\n\t};\n}\n\n/**\n * Sanitize/validate an inbound advisory result (i.e. whatever the injected\n * advisory function returned). Returns `null` (never throws) on any shape,\n * forbidden-field, extra-key, or unknown-enum violation - the caller\n * (`AdaptorchBridge.consult`) treats `null` as \"no hint\" and falls back\n * silently, exactly like every other failure mode.\n */\nexport function sanitizeAdvisoryResult(candidate: unknown): AdaptorchAdvisoryResult | null {\n\tif (!isPlainObject(candidate)) return null;\n\tif (containsForbiddenField(candidate)) return null;\n\tif (!hasOnlyAllowedKeys(candidate, ADVISORY_RESULT_KEYS)) return null;\n\n\tif (candidate.schemaVersion !== 1) return null;\n\n\tconst taskClass = candidate.taskClass;\n\tif (typeof taskClass !== \"string\" || !TASK_CLASS_SET.has(taskClass)) return null;\n\n\tconst confidenceBand = candidate.confidenceBand;\n\tif (typeof confidenceBand !== \"string\" || !CONFIDENCE_BAND_SET.has(confidenceBand)) return null;\n\n\treturn {\n\t\tschemaVersion: 1,\n\t\ttaskClass: taskClass as AdaptorchTaskClass,\n\t\tconfidenceBand: confidenceBand as AdaptorchConfidenceBand,\n\t};\n}\n\n/**\n * Read-only/local AdaptOrch MCP tool names this bridge may ever reference,\n * grounded in the real 10-tool surface documented in\n * packages/adaptorch-wpl/src/adaptorch-client.ts (cross-checked there\n * against the AdaptOrch server's own docs) and narrowed to the two tools the\n * read-first plan puts in scope for a reasoning-effort hint (plan 4.2): the\n * session-start capabilities probe and the per-consult topology\n * classification call. This module does not call any MCP tool directly\n * today - the advisory function is fully caller-injected - so this\n * allowlist is not wired to a transport here; it exists so a future\n * transport, and this module's own tests, can assert by construction that\n * `adaptorch_run` (submit) and `adaptorch_cancel_run` (cancel), or any other\n * mutating tool, can never be reachable from router/bridge code.\n */\nexport const ADAPTORCH_READONLY_TOOL_ALLOWLIST = [\"adaptorch_capabilities\", \"adaptorch_route_topology\"] as const;\n\nexport type AdaptorchReadonlyToolName = (typeof ADAPTORCH_READONLY_TOOL_ALLOWLIST)[number];\n\n/** Underscore-segment verbs that mark an AdaptOrch-shaped tool name as mutating (write/execute/lifecycle). */\nconst MUTATING_TOOL_VERB_SEGMENTS: ReadonlySet<string> = new Set([\n\t\"run\",\n\t\"submit\",\n\t\"cancel\",\n\t\"dispatch\",\n\t\"delete\",\n\t\"execute\",\n\t\"mutate\",\n\t\"write\",\n\t\"create\",\n\t\"update\",\n\t\"remove\",\n]);\n\n/**\n * True if any underscore-delimited segment of an AdaptOrch-shaped tool name\n * is a known mutating verb. Segment-based rather than a raw substring/regex\n * match so \"adaptorch_route_topology\" is never falsely flagged (no segment\n * is a mutating verb), while \"adaptorch_run\" and \"adaptorch_cancel_run\" are\n * always caught regardless of future prefix/suffix changes.\n */\nexport function looksMutatingToolName(name: string): boolean {\n\treturn name\n\t\t.toLowerCase()\n\t\t.split(\"_\")\n\t\t.some((segment) => MUTATING_TOOL_VERB_SEGMENTS.has(segment));\n}\n\n/**\n * Type guard: true only for the two allowlisted read/local AdaptOrch tool\n * names. Also independently rejects any name `looksMutatingToolName` flags,\n * so a future accidental addition of a mutating name to the allowlist array\n * still cannot make this function report it as allowed.\n */\nexport function isAdaptorchToolAllowed(name: string): name is AdaptorchReadonlyToolName {\n\tif (looksMutatingToolName(name)) return false;\n\treturn (ADAPTORCH_READONLY_TOOL_ALLOWLIST as readonly string[]).includes(name);\n}\n\n/**\n * Timeout wrapper interface around any injected async advisory call. Races\n * the call against a timer; a slow or hung call rejects at `timeoutMs`\n * instead of leaving the caller waiting indefinitely. Always clears the\n * timer once the call settles (or once the timeout itself fires), and\n * always resolves/rejects exactly once - a synchronous throw from `run` is\n * treated the same as an asynchronous rejection.\n */\nexport type AdvisoryTimeoutWrapper = <T>(run: () => Promise<T>, timeoutMs: number) => Promise<T>;\n\nexport const withAdvisoryTimeout: AdvisoryTimeoutWrapper = <T>(\n\trun: () => Promise<T>,\n\ttimeoutMs: number,\n): Promise<T> => {\n\treturn new Promise<T>((resolve, reject) => {\n\t\tconst timer = setTimeout(() => {\n\t\t\treject(new Error(\"adaptorch-bridge: advisory call timed out\"));\n\t\t}, timeoutMs);\n\n\t\tlet pending: Promise<T>;\n\t\ttry {\n\t\t\tpending = run();\n\t\t} catch (err) {\n\t\t\tclearTimeout(timer);\n\t\t\treject(err instanceof Error ? err : new Error(String(err)));\n\t\t\treturn;\n\t\t}\n\n\t\tpending.then(\n\t\t\t(value) => {\n\t\t\t\tclearTimeout(timer);\n\t\t\t\tresolve(value);\n\t\t\t},\n\t\t\t(err: unknown) => {\n\t\t\t\tclearTimeout(timer);\n\t\t\t\treject(err instanceof Error ? err : new Error(String(err)));\n\t\t\t},\n\t\t);\n\t});\n};\n\nconst DEFAULT_TTL_MS = 5 * 60 * 1000;\nconst MIN_TTL_MS = 30 * 1000;\nconst MAX_TTL_MS = 5 * 60 * 1000;\nconst DEFAULT_MAX_TURNS_PER_ENTRY = 10;\nconst DEFAULT_CACHE_SIZE = 16;\nconst DEFAULT_MAX_CONSULTS_PER_SESSION = 5;\nconst DEFAULT_MIN_INTERVAL_MS = 60 * 1000;\nconst DEFAULT_TIMEOUT_MS = 1500;\nconst MIN_TIMEOUT_MS = 1;\nconst MAX_TIMEOUT_MS = 30 * 1000;\nconst DEFAULT_FAILURE_THRESHOLD = 3;\nconst DEFAULT_COOLDOWN_MS = 10 * 60 * 1000;\n\n/**\n * Exact TTL/cache/budget/circuit-breaker defaults from the read-first plan,\n * section 4.8. Exported so a security/review lane can confirm the shipped\n * numbers without reading this module's internals.\n */\nexport const ADAPTORCH_BRIDGE_DEFAULTS = {\n\tttlMs: DEFAULT_TTL_MS,\n\tminTtlMs: MIN_TTL_MS,\n\tmaxTtlMs: MAX_TTL_MS,\n\tmaxTurnsPerEntry: DEFAULT_MAX_TURNS_PER_ENTRY,\n\tcacheSize: DEFAULT_CACHE_SIZE,\n\tmaxConsultsPerSession: DEFAULT_MAX_CONSULTS_PER_SESSION,\n\tminIntervalMs: DEFAULT_MIN_INTERVAL_MS,\n\ttimeoutMs: DEFAULT_TIMEOUT_MS,\n\tminTimeoutMs: MIN_TIMEOUT_MS,\n\tmaxTimeoutMs: MAX_TIMEOUT_MS,\n\tfailureThreshold: DEFAULT_FAILURE_THRESHOLD,\n\tcooldownMs: DEFAULT_COOLDOWN_MS,\n} as const;\n\n/**\n * Bridge configuration. `advisoryFn` is the ONLY seam to any external\n * system - this module never imports or calls an MCP client/transport\n * itself, so it is impossible for a unit test (or a misconfigured caller)\n * to reach a real MCP server through this module by accident.\n */\nexport interface AdaptorchBridgeConfig {\n\t/**\n\t * Caller-injected advisory call. Its return value is untrusted: `consult`\n\t * always runs it through `sanitizeAdvisoryResult` before using or caching\n\t * it, and a rejected/thrown/timed-out/schema-invalid response is treated\n\t * as an ordinary failure (counts toward the circuit breaker), never as a\n\t * value to fuse.\n\t */\n\treadonly advisoryFn: (payload: AdaptorchConsultPayload) => Promise<unknown>;\n\t/** Injectable clock; defaults to `Date.now`. Tests supply a fake to avoid real wall-clock waits. */\n\treadonly now?: () => number;\n\t/** Cache TTL in ms; clamped to [30s, 5min] (plan 4.8). Default 5 minutes. */\n\treadonly ttlMs?: number;\n\t/** A cache entry also expires after this many `getFreshHint` calls (\"turns\"). Default 10. */\n\treadonly maxTurnsPerEntry?: number;\n\t/** Max distinct cached payload keys (LRU eviction beyond this). Default 16. */\n\treadonly cacheSize?: number;\n\t/** Max advisory attempts for this bridge instance's lifetime (\"session\"). Default 5. */\n\treadonly maxConsultsPerSession?: number;\n\t/** Minimum ms between advisory attempts. Default 60000 (60s). */\n\treadonly minIntervalMs?: number;\n\t/** Per-attempt timeout in ms; clamped to [1ms, 30s]. Default 1500 (1.5s). */\n\treadonly timeoutMs?: number;\n\t/** Consecutive failures before the circuit opens. Default 3. */\n\treadonly failureThreshold?: number;\n\t/** Circuit-open duration in ms once opened. Default 600000 (10min). */\n\treadonly cooldownMs?: number;\n}\n\n/** Read-only, non-sensitive introspection snapshot - never exposes payload/result content. */\nexport interface AdaptorchBridgeStats {\n\treadonly consultCount: number;\n\treadonly failureStreak: number;\n\treadonly circuitOpen: boolean;\n\treadonly circuitOpenUntilMs: number;\n\treadonly cacheEntryCount: number;\n}\n\nexport interface AdaptorchBridge {\n\t/**\n\t * Synchronous, cache-read-only. Never blocks, never throws, and never\n\t * calls the advisory function - a cache miss or an expired/invalid entry\n\t * silently returns `null` (\"no hint\"), matching the turn-start path's\n\t * synchronous, I/O-free contract (plan 4.4).\n\t */\n\tgetFreshHint(payload: AdaptorchConsultPayload): AdaptorchAdvisoryResult | null;\n\t/**\n\t * Fire-and-forget refresh for a LATER call to `getFreshHint`, never the\n\t * current one. Never throws synchronously and never rejects into the\n\t * caller - any failure is swallowed internally by `consult`.\n\t */\n\trequestRefresh(payload: AdaptorchConsultPayload): void;\n\t/**\n\t * The awaitable core operation `requestRefresh` fires without awaiting.\n\t * Gated by the circuit breaker, budget counter, and minimum interval (in\n\t * that order) before ever invoking `advisoryFn`; always resolves (never\n\t * rejects) to either a sanitized hint or `null`.\n\t */\n\tconsult(payload: AdaptorchConsultPayload): Promise<AdaptorchAdvisoryResult | null>;\n\t/** Read-only introspection for tests/diagnostics; contains no payload or result content. */\n\tgetStats(): AdaptorchBridgeStats;\n}\n\ninterface CacheEntry {\n\treadonly result: AdaptorchAdvisoryResult;\n\treadonly insertedAtMs: number;\n\treadonly insertedAtTurn: number;\n}\n\n/** Deterministic cache key built field-by-field from a sanitized payload - never from raw prompt text. */\nfunction cacheKeyFor(payload: AdaptorchConsultPayload): string {\n\treturn JSON.stringify({\n\t\ttaskClass: payload.taskClass,\n\t\trunnerUp: payload.runnerUp,\n\t\tmarginBucket: payload.marginBucket,\n\t\tlenBucket: payload.lenBucket,\n\t\thadFence: payload.hadFence,\n\t\thadDiff: payload.hadDiff,\n\t\tpressureBucket: payload.pressureBucket,\n\t\tlaneType: payload.laneType ?? null,\n\t});\n}\n\nclass AdaptorchBridgeImpl implements AdaptorchBridge {\n\tprivate readonly advisoryFn: (payload: AdaptorchConsultPayload) => Promise<unknown>;\n\tprivate readonly now: () => number;\n\tprivate readonly ttlMs: number;\n\tprivate readonly maxTurnsPerEntry: number;\n\tprivate readonly cacheSize: number;\n\tprivate readonly maxConsultsPerSession: number;\n\tprivate readonly minIntervalMs: number;\n\tprivate readonly timeoutMs: number;\n\tprivate readonly failureThreshold: number;\n\tprivate readonly cooldownMs: number;\n\n\tprivate readonly cache = new Map<string, CacheEntry>();\n\tprivate turnCounter = 0;\n\tprivate consultCount = 0;\n\tprivate failureStreak = 0;\n\tprivate lastAttemptAtMs: number | null = null;\n\tprivate circuitOpenUntilMs = 0;\n\n\tconstructor(config: AdaptorchBridgeConfig) {\n\t\tthis.advisoryFn = config.advisoryFn;\n\t\tthis.now = config.now ?? Date.now;\n\t\tthis.ttlMs = clampInt(config.ttlMs ?? DEFAULT_TTL_MS, MIN_TTL_MS, MAX_TTL_MS);\n\t\tthis.maxTurnsPerEntry = boundedIntOrDefault(config.maxTurnsPerEntry, DEFAULT_MAX_TURNS_PER_ENTRY, 1);\n\t\tthis.cacheSize = boundedIntOrDefault(config.cacheSize, DEFAULT_CACHE_SIZE, 1);\n\t\tthis.maxConsultsPerSession = boundedIntOrDefault(\n\t\t\tconfig.maxConsultsPerSession,\n\t\t\tDEFAULT_MAX_CONSULTS_PER_SESSION,\n\t\t\t0,\n\t\t);\n\t\tthis.minIntervalMs = boundedIntOrDefault(config.minIntervalMs, DEFAULT_MIN_INTERVAL_MS, 0);\n\t\tthis.timeoutMs = clampInt(config.timeoutMs ?? DEFAULT_TIMEOUT_MS, MIN_TIMEOUT_MS, MAX_TIMEOUT_MS);\n\t\tthis.failureThreshold = boundedIntOrDefault(config.failureThreshold, DEFAULT_FAILURE_THRESHOLD, 1);\n\t\tthis.cooldownMs = boundedIntOrDefault(config.cooldownMs, DEFAULT_COOLDOWN_MS, 0);\n\t}\n\n\tgetFreshHint(payload: AdaptorchConsultPayload): AdaptorchAdvisoryResult | null {\n\t\tthis.turnCounter += 1;\n\t\tconst sanitizedPayload = sanitizeConsultPayload(payload);\n\t\tif (sanitizedPayload === null) return null;\n\n\t\tconst key = cacheKeyFor(sanitizedPayload);\n\t\tconst entry = this.cache.get(key);\n\t\tif (entry === undefined) return null;\n\t\tif (!this.isEntryFresh(entry)) {\n\t\t\tthis.cache.delete(key);\n\t\t\treturn null;\n\t\t}\n\t\treturn entry.result;\n\t}\n\n\trequestRefresh(payload: AdaptorchConsultPayload): void {\n\t\tvoid this.consult(payload).catch(() => {\n\t\t\t// consult() already swallows every failure and always resolves; this\n\t\t\t// catch only guards a future refactor from letting one escape, so a\n\t\t\t// fire-and-forget refresh can never surface an unhandled rejection.\n\t\t});\n\t}\n\n\tasync consult(payload: AdaptorchConsultPayload): Promise<AdaptorchAdvisoryResult | null> {\n\t\tconst sanitizedPayload = sanitizeConsultPayload(payload);\n\t\tif (sanitizedPayload === null) return null;\n\n\t\tconst nowMs = this.now();\n\t\tif (nowMs < this.circuitOpenUntilMs) return null;\n\t\tif (this.consultCount >= this.maxConsultsPerSession) return null;\n\t\tif (this.lastAttemptAtMs !== null && nowMs - this.lastAttemptAtMs < this.minIntervalMs) return null;\n\n\t\tthis.consultCount += 1;\n\t\tthis.lastAttemptAtMs = nowMs;\n\n\t\tlet raw: unknown;\n\t\ttry {\n\t\t\traw = await withAdvisoryTimeout(() => this.advisoryFn(sanitizedPayload), this.timeoutMs);\n\t\t} catch {\n\t\t\tthis.recordFailure();\n\t\t\treturn null;\n\t\t}\n\n\t\tconst result = sanitizeAdvisoryResult(raw);\n\t\tif (result === null) {\n\t\t\tthis.recordFailure();\n\t\t\treturn null;\n\t\t}\n\n\t\tthis.failureStreak = 0;\n\t\tthis.cacheResult(sanitizedPayload, result);\n\t\treturn result;\n\t}\n\n\tgetStats(): AdaptorchBridgeStats {\n\t\tconst nowMs = this.now();\n\t\treturn {\n\t\t\tconsultCount: this.consultCount,\n\t\t\tfailureStreak: this.failureStreak,\n\t\t\tcircuitOpen: nowMs < this.circuitOpenUntilMs,\n\t\t\tcircuitOpenUntilMs: this.circuitOpenUntilMs,\n\t\t\tcacheEntryCount: this.cache.size,\n\t\t};\n\t}\n\n\tprivate recordFailure(): void {\n\t\tthis.failureStreak += 1;\n\t\tif (this.failureStreak >= this.failureThreshold) {\n\t\t\tthis.circuitOpenUntilMs = this.now() + this.cooldownMs;\n\t\t}\n\t}\n\n\tprivate isEntryFresh(entry: CacheEntry): boolean {\n\t\tconst ageMs = this.now() - entry.insertedAtMs;\n\t\tconst ageTurns = this.turnCounter - entry.insertedAtTurn;\n\t\treturn ageMs < this.ttlMs && ageTurns < this.maxTurnsPerEntry;\n\t}\n\n\t/** Eviction favors entries last refreshed by a successful consult (write-time LRU, not read-time). */\n\tprivate cacheResult(payload: AdaptorchConsultPayload, result: AdaptorchAdvisoryResult): void {\n\t\tconst key = cacheKeyFor(payload);\n\t\tif (this.cache.has(key)) this.cache.delete(key);\n\t\tthis.cache.set(key, { result, insertedAtMs: this.now(), insertedAtTurn: this.turnCounter });\n\t\twhile (this.cache.size > this.cacheSize) {\n\t\t\tconst oldestKey = this.cache.keys().next().value;\n\t\t\tif (oldestKey === undefined) break;\n\t\t\tthis.cache.delete(oldestKey);\n\t\t}\n\t}\n}\n\n/** Construct a default-off AdaptOrch advisory bridge. See `AdaptorchBridgeConfig` for tunables. */\nexport function createAdaptorchBridge(config: AdaptorchBridgeConfig): AdaptorchBridge {\n\treturn new AdaptorchBridgeImpl(config);\n}\n"]}