@kaelio/ktx 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/assets/python/{kaelio_ktx-0.8.0-py3-none-any.whl → kaelio_ktx-0.10.0-py3-none-any.whl} +0 -0
  2. package/assets/python/manifest.json +4 -4
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/clack.d.ts +6 -0
  5. package/dist/clack.js +17 -2
  6. package/dist/cli-program.d.ts +3 -0
  7. package/dist/cli-program.js +42 -2
  8. package/dist/cli-runtime.d.ts +3 -0
  9. package/dist/cli-runtime.js +94 -3
  10. package/dist/commands/setup-commands.js +3 -4
  11. package/dist/connection-recovery.d.ts +34 -0
  12. package/dist/connection-recovery.js +82 -0
  13. package/dist/connection.js +26 -2
  14. package/dist/connectors/bigquery/connector.d.ts +2 -5
  15. package/dist/connectors/bigquery/connector.js +2 -2
  16. package/dist/connectors/clickhouse/connector.d.ts +2 -5
  17. package/dist/connectors/clickhouse/connector.js +2 -2
  18. package/dist/connectors/mysql/connector.d.ts +7 -6
  19. package/dist/connectors/mysql/connector.js +25 -5
  20. package/dist/connectors/mysql/dialect.d.ts +1 -1
  21. package/dist/connectors/mysql/dialect.js +12 -2
  22. package/dist/connectors/postgres/connector.d.ts +2 -5
  23. package/dist/connectors/postgres/connector.js +2 -2
  24. package/dist/connectors/snowflake/connector.d.ts +2 -5
  25. package/dist/connectors/snowflake/connector.js +2 -2
  26. package/dist/connectors/sqlite/connector.d.ts +2 -5
  27. package/dist/connectors/sqlite/connector.js +2 -2
  28. package/dist/connectors/sqlserver/connector.d.ts +2 -5
  29. package/dist/connectors/sqlserver/connector.js +2 -2
  30. package/dist/context/connections/drivers.d.ts +0 -1
  31. package/dist/context/connections/drivers.js +0 -7
  32. package/dist/context/connections/query-executor.d.ts +2 -1
  33. package/dist/context/core/abort.d.ts +9 -0
  34. package/dist/context/core/abort.js +36 -0
  35. package/dist/context/ingest/adapters/historic-sql/bigquery-query-history-reader.js +71 -20
  36. package/dist/context/ingest/adapters/historic-sql/chunk-unified.js +2 -1
  37. package/dist/context/ingest/adapters/historic-sql/connection-dialect.d.ts +9 -0
  38. package/dist/context/ingest/adapters/historic-sql/connection-dialect.js +15 -4
  39. package/dist/context/ingest/adapters/historic-sql/pattern-inputs.js +8 -2
  40. package/dist/context/ingest/adapters/historic-sql/query-history-filter-picker.d.ts +30 -0
  41. package/dist/context/ingest/adapters/historic-sql/query-history-filter-picker.js +194 -0
  42. package/dist/context/ingest/adapters/historic-sql/scope-floor.d.ts +18 -0
  43. package/dist/context/ingest/adapters/historic-sql/scope-floor.js +229 -0
  44. package/dist/context/ingest/adapters/historic-sql/scope-membership.d.ts +8 -0
  45. package/dist/context/ingest/adapters/historic-sql/scope-membership.js +29 -0
  46. package/dist/context/ingest/adapters/historic-sql/snowflake-query-history-reader.js +68 -19
  47. package/dist/context/ingest/adapters/historic-sql/stage-unified.js +57 -50
  48. package/dist/context/ingest/adapters/historic-sql/types.d.ts +36 -3
  49. package/dist/context/ingest/adapters/historic-sql/types.js +14 -2
  50. package/dist/context/ingest/context-candidates/curator-pagination.service.d.ts +1 -5
  51. package/dist/context/ingest/context-candidates/curator-pagination.service.js +1 -3
  52. package/dist/context/ingest/context-evidence/sqlite-context-evidence-store.d.ts +1 -1
  53. package/dist/context/ingest/final-gate-repair.d.ts +1 -0
  54. package/dist/context/ingest/final-gate-repair.js +1 -0
  55. package/dist/context/ingest/ingest-bundle.runner.d.ts +3 -0
  56. package/dist/context/ingest/ingest-bundle.runner.js +127 -53
  57. package/dist/context/ingest/isolated-diff/patch-integrator.js +75 -5
  58. package/dist/context/ingest/isolated-diff/textual-conflict-resolver.d.ts +1 -0
  59. package/dist/context/ingest/isolated-diff/textual-conflict-resolver.js +1 -0
  60. package/dist/context/ingest/isolated-diff/work-unit-executor.d.ts +1 -0
  61. package/dist/context/ingest/local-adapters.js +21 -4
  62. package/dist/context/ingest/local-bundle-runtime.js +13 -5
  63. package/dist/context/ingest/local-ingest.d.ts +1 -0
  64. package/dist/context/ingest/local-ingest.js +13 -3
  65. package/dist/context/ingest/memory-flow/events.js +1 -1
  66. package/dist/context/ingest/memory-flow/schema.js +8 -3
  67. package/dist/context/ingest/memory-flow/types.d.ts +7 -3
  68. package/dist/context/ingest/ports.d.ts +3 -5
  69. package/dist/context/ingest/stages/stage-3-work-units.d.ts +1 -4
  70. package/dist/context/ingest/stages/stage-3-work-units.js +5 -1
  71. package/dist/context/ingest/stages/stage-4-reconciliation.d.ts +1 -4
  72. package/dist/context/ingest/stages/stage-4-reconciliation.js +1 -1
  73. package/dist/context/ingest/types.d.ts +1 -0
  74. package/dist/context/llm/ai-sdk-runtime.d.ts +3 -0
  75. package/dist/context/llm/ai-sdk-runtime.js +152 -16
  76. package/dist/context/llm/claude-code-runtime.d.ts +6 -4
  77. package/dist/context/llm/claude-code-runtime.js +127 -48
  78. package/dist/context/llm/codex-exec-events.d.ts +20 -0
  79. package/dist/context/llm/codex-exec-events.js +155 -0
  80. package/dist/context/llm/codex-isolation.d.ts +3 -0
  81. package/dist/context/llm/codex-isolation.js +5 -0
  82. package/dist/context/llm/codex-mcp-runtime-server.d.ts +24 -0
  83. package/dist/context/llm/codex-mcp-runtime-server.js +51 -0
  84. package/dist/context/llm/codex-models.d.ts +2 -0
  85. package/dist/context/llm/codex-models.js +17 -0
  86. package/dist/context/llm/codex-runtime-config.d.ts +16 -0
  87. package/dist/context/llm/codex-runtime-config.js +19 -0
  88. package/dist/context/llm/codex-runtime.d.ts +37 -0
  89. package/dist/context/llm/codex-runtime.js +347 -0
  90. package/dist/context/llm/codex-sdk-runner.d.ts +21 -0
  91. package/dist/context/llm/codex-sdk-runner.js +63 -0
  92. package/dist/context/llm/local-config.d.ts +16 -4
  93. package/dist/context/llm/local-config.js +18 -2
  94. package/dist/context/llm/rate-limit-governor.d.ts +103 -0
  95. package/dist/context/llm/rate-limit-governor.js +285 -0
  96. package/dist/context/llm/runtime-port.d.ts +3 -6
  97. package/dist/context/mcp/context-tools.js +43 -13
  98. package/dist/context/project/config.d.ts +14 -0
  99. package/dist/context/project/config.js +37 -2
  100. package/dist/context/scan/types.d.ts +15 -2
  101. package/dist/context/scan/types.js +12 -0
  102. package/dist/context/sl/description-normalization.js +4 -14
  103. package/dist/context/sql-analysis/http-sql-analysis-port.js +32 -2
  104. package/dist/context/sql-analysis/ports.d.ts +12 -2
  105. package/dist/context/tools/context-candidate-mark.tool.d.ts +2 -2
  106. package/dist/context-build-view.d.ts +13 -0
  107. package/dist/context-build-view.js +63 -32
  108. package/dist/demo-metrics.d.ts +0 -2
  109. package/dist/demo-metrics.js +1 -11
  110. package/dist/ingest.d.ts +1 -0
  111. package/dist/ingest.js +32 -3
  112. package/dist/io/buffered-command-io.d.ts +11 -0
  113. package/dist/io/buffered-command-io.js +28 -0
  114. package/dist/io/symbols.d.ts +2 -0
  115. package/dist/io/symbols.js +2 -0
  116. package/dist/llm/types.d.ts +1 -1
  117. package/dist/local-adapters.d.ts +10 -2
  118. package/dist/local-adapters.js +19 -3
  119. package/dist/memory-flow-hud.js +8 -16
  120. package/dist/next-steps.js +1 -2
  121. package/dist/progress-port-adapter.d.ts +6 -0
  122. package/dist/progress-port-adapter.js +18 -0
  123. package/dist/public-ingest.d.ts +20 -1
  124. package/dist/public-ingest.js +228 -42
  125. package/dist/reveal-password-prompt.d.ts +24 -0
  126. package/dist/reveal-password-prompt.js +78 -0
  127. package/dist/scan.js +21 -3
  128. package/dist/setup-context.d.ts +2 -0
  129. package/dist/setup-context.js +133 -27
  130. package/dist/setup-databases.d.ts +18 -1
  131. package/dist/setup-databases.js +378 -249
  132. package/dist/setup-demo-tour.js +1 -0
  133. package/dist/setup-embeddings.js +1 -1
  134. package/dist/setup-models.d.ts +11 -15
  135. package/dist/setup-models.js +140 -276
  136. package/dist/setup-prompts.js +3 -2
  137. package/dist/setup-ready-menu.d.ts +16 -2
  138. package/dist/setup-ready-menu.js +37 -5
  139. package/dist/setup-sources.js +115 -35
  140. package/dist/setup.d.ts +1 -1
  141. package/dist/setup.js +23 -11
  142. package/dist/sl.d.ts +2 -2
  143. package/dist/sl.js +20 -4
  144. package/dist/sql.js +18 -2
  145. package/dist/star-prompt/cache.d.ts +16 -0
  146. package/dist/star-prompt/cache.js +45 -0
  147. package/dist/star-prompt/star-count.d.ts +7 -0
  148. package/dist/star-prompt/star-count.js +66 -0
  149. package/dist/star-prompt/star-line.d.ts +12 -0
  150. package/dist/star-prompt/star-line.js +26 -0
  151. package/dist/status-project.d.ts +11 -0
  152. package/dist/status-project.js +50 -1
  153. package/dist/telemetry/command-hook.d.ts +1 -0
  154. package/dist/telemetry/command-hook.js +3 -1
  155. package/dist/telemetry/emitter.d.ts +10 -0
  156. package/dist/telemetry/emitter.js +31 -0
  157. package/dist/telemetry/events.d.ts +35 -6
  158. package/dist/telemetry/events.js +25 -2
  159. package/dist/telemetry/exception.d.ts +18 -0
  160. package/dist/telemetry/exception.js +162 -0
  161. package/dist/telemetry/identity.d.ts +0 -1
  162. package/dist/telemetry/identity.js +6 -6
  163. package/dist/telemetry/index.d.ts +15 -2
  164. package/dist/telemetry/index.js +15 -3
  165. package/dist/telemetry/redaction-secrets.d.ts +11 -0
  166. package/dist/telemetry/redaction-secrets.js +92 -0
  167. package/dist/telemetry/scrubber.d.ts +10 -0
  168. package/dist/telemetry/scrubber.js +20 -0
  169. package/dist/update-check/cache.d.ts +21 -0
  170. package/dist/update-check/cache.js +38 -0
  171. package/dist/update-check/channel.d.ts +15 -0
  172. package/dist/update-check/channel.js +30 -0
  173. package/dist/update-check/registry.d.ts +1 -0
  174. package/dist/update-check/registry.js +45 -0
  175. package/dist/update-check/update-check.d.ts +43 -0
  176. package/dist/update-check/update-check.js +116 -0
  177. package/package.json +12 -4
  178. package/dist/context/connections/local-query-executor.d.ts +0 -6
  179. package/dist/context/connections/local-query-executor.js +0 -39
  180. package/dist/context/connections/postgres-query-executor.d.ts +0 -25
  181. package/dist/context/connections/postgres-query-executor.js +0 -53
  182. package/dist/context/connections/sqlite-query-executor.d.ts +0 -4
  183. package/dist/context/connections/sqlite-query-executor.js +0 -74
@@ -1,6 +1,6 @@
1
1
  import { createSdkMcpServer, query as defaultQuery, } from '@anthropic-ai/claude-agent-sdk';
2
2
  import { z } from 'zod';
3
- import { noopLogger } from '../../context/core/config.js';
3
+ import { createAbortError, isAbortError, throwIfAborted } from '../core/abort.js';
4
4
  import { createKtxClaudeCodeEnv } from './claude-code-env.js';
5
5
  import { resolveClaudeCodeModel } from './claude-code-models.js';
6
6
  import { createClaudeSdkTools, mcpToolIds } from './runtime-tools.js';
@@ -41,21 +41,6 @@ const STRUCTURED_OUTPUT_TOOL_NAME = 'StructuredOutput';
41
41
  function isResult(message) {
42
42
  return message.type === 'result';
43
43
  }
44
- // Skip emissions the SDK does not count toward `num_turns`: `pause_turn` continuations and
45
- // errored partials (e.g. `max_output_tokens`) it retries internally. Without this, the
46
- // runtime's step counter outruns `maxTurns` and the HUD renders e.g. `step 69/40`.
47
- function countsAsAssistantTurn(message) {
48
- if (message.type !== 'assistant' || message.parent_tool_use_id !== null) {
49
- return false;
50
- }
51
- if (message.error !== undefined) {
52
- return false;
53
- }
54
- if (message.message.stop_reason === 'pause_turn') {
55
- return false;
56
- }
57
- return true;
58
- }
59
44
  function resultError(result) {
60
45
  if (result.subtype === 'success') {
61
46
  return undefined;
@@ -104,6 +89,67 @@ function assertInitIsolation(message, allowedToolIds, expectedMcpServerNames) {
104
89
  function expectedMcpServerNames(tools) {
105
90
  return tools && Object.keys(tools).length > 0 ? new Set([KTX_MCP_SERVER_NAME]) : new Set();
106
91
  }
92
+ const CLAUDE_RATE_LIMIT_ERROR_MARKERS = /\b429\b|rate limit|too many requests|quota exceeded|overloaded|max_retries/i;
93
+ function normalizeClaudeResetAtMs(value) {
94
+ if (typeof value === 'number' && Number.isFinite(value) && value > 0) {
95
+ return Math.round(value < 10_000_000_000 ? value * 1_000 : value);
96
+ }
97
+ if (typeof value === 'string') {
98
+ const numeric = Number(value);
99
+ if (Number.isFinite(numeric) && numeric > 0) {
100
+ return normalizeClaudeResetAtMs(numeric);
101
+ }
102
+ const parsed = Date.parse(value);
103
+ return Number.isFinite(parsed) ? parsed : undefined;
104
+ }
105
+ return undefined;
106
+ }
107
+ function isClaudeRateLimitResult(result, rejectedSignal) {
108
+ const error = resultError(result);
109
+ if (!error) {
110
+ return false;
111
+ }
112
+ if (rejectedSignal?.status === 'rejected') {
113
+ return true;
114
+ }
115
+ const resultDetails = result;
116
+ const details = [
117
+ error.message,
118
+ resultDetails.stop_reason,
119
+ resultDetails.terminal_reason,
120
+ ...(resultDetails.errors ?? []),
121
+ ]
122
+ .filter((value) => typeof value === 'string' && value.length > 0)
123
+ .join('\n');
124
+ return CLAUDE_RATE_LIMIT_ERROR_MARKERS.test(details);
125
+ }
126
+ function claudeRateLimitSignal(message) {
127
+ const record = message;
128
+ if (record.type === 'rate_limit_event') {
129
+ const info = record.rate_limit_info;
130
+ if (!info)
131
+ return null;
132
+ const rawStatus = typeof info.status === 'string' ? info.status : 'allowed';
133
+ const resetAtMs = normalizeClaudeResetAtMs(info.resetsAt);
134
+ return {
135
+ provider: 'claude-subscription',
136
+ status: rawStatus === 'rejected' ? 'rejected' : rawStatus === 'allowed_warning' ? 'warning' : 'allowed',
137
+ ...(resetAtMs !== undefined ? { resetAtMs } : {}),
138
+ ...(typeof info.rateLimitType === 'string' ? { rateLimitType: info.rateLimitType } : {}),
139
+ ...(typeof info.utilization === 'number' ? { utilization: info.utilization } : {}),
140
+ };
141
+ }
142
+ if (record.subtype === 'api_retry' || record.type === 'api_retry') {
143
+ const retryDelayMs = typeof record.retry_delay_ms === 'number' ? record.retry_delay_ms : undefined;
144
+ return {
145
+ provider: 'claude-subscription',
146
+ status: 'warning',
147
+ ...(retryDelayMs !== undefined ? { retryAfterMs: retryDelayMs } : {}),
148
+ rateLimitType: 'api_retry',
149
+ };
150
+ }
151
+ return null;
152
+ }
107
153
  function managedMcpSettings(serverNames) {
108
154
  return {
109
155
  allowManagedMcpServersOnly: true,
@@ -150,28 +196,63 @@ function baseOptions(input) {
150
196
  }
151
197
  async function collectResult(params) {
152
198
  let result;
153
- for await (const message of params.query({ prompt: params.prompt, options: params.options })) {
154
- assertInitIsolation(message, params.allowedToolIds, params.expectedMcpServerNames);
155
- if (countsAsAssistantTurn(message)) {
156
- await params.onAssistantTurn?.();
157
- }
158
- if (isResult(message)) {
159
- result = message;
199
+ let rejectedRateLimitSignal;
200
+ throwIfAborted(params.abortSignal);
201
+ await params.rateLimitGovernor?.waitForReady(params.abortSignal);
202
+ throwIfAborted(params.abortSignal);
203
+ const queryResult = params.query({ prompt: params.prompt, options: params.options });
204
+ const onAbort = () => {
205
+ void Promise.resolve(queryResult.interrupt?.()).catch(() => undefined);
206
+ };
207
+ params.abortSignal?.addEventListener('abort', onAbort, { once: true });
208
+ try {
209
+ for await (const message of queryResult) {
210
+ throwIfAborted(params.abortSignal);
211
+ const rateLimitSignal = claudeRateLimitSignal(message);
212
+ if (rateLimitSignal) {
213
+ if (rateLimitSignal.status === 'rejected') {
214
+ rejectedRateLimitSignal = rateLimitSignal;
215
+ }
216
+ params.rateLimitGovernor?.report(rateLimitSignal);
217
+ }
218
+ assertInitIsolation(message, params.allowedToolIds, params.expectedMcpServerNames);
219
+ if (isResult(message)) {
220
+ result = message;
221
+ }
160
222
  }
161
223
  }
224
+ finally {
225
+ params.abortSignal?.removeEventListener('abort', onAbort);
226
+ }
227
+ if (params.abortSignal?.aborted) {
228
+ throw createAbortError();
229
+ }
162
230
  if (!result) {
163
231
  throw new Error('Claude Code query returned no result message');
164
232
  }
165
- return result;
233
+ return {
234
+ result,
235
+ ...(rejectedRateLimitSignal ? { rejectedRateLimitSignal } : {}),
236
+ };
237
+ }
238
+ async function collectResultWithRateLimitRetry(params) {
239
+ // maxRetryAttempts() returns 1 when no governor is present or pacing is
240
+ // disabled, so a rate-limited result surfaces without an extra query; the
241
+ // Claude Code SDK applies its own backoff for transient rejections.
242
+ const maxAttempts = params.rateLimitGovernor?.maxRetryAttempts() ?? 1;
243
+ for (let attempt = 0;; attempt += 1) {
244
+ const outcome = await collectResult(params);
245
+ if (!isClaudeRateLimitResult(outcome.result, outcome.rejectedRateLimitSignal) || attempt >= maxAttempts - 1) {
246
+ return outcome.result;
247
+ }
248
+ }
166
249
  }
167
250
  export class ClaudeCodeKtxLlmRuntime {
168
251
  deps;
169
252
  runQuery;
170
- logger;
171
253
  constructor(deps) {
172
254
  this.deps = deps;
173
255
  this.runQuery = deps.query ?? defaultQuery;
174
- this.logger = deps.logger ?? noopLogger;
175
256
  }
176
257
  async generateText(input) {
177
258
  const options = baseOptions({
@@ -182,12 +263,14 @@ export class ClaudeCodeKtxLlmRuntime {
182
263
  tools: input.tools,
183
264
  });
184
265
  const startedAt = Date.now();
185
- const result = await collectResult({
266
+ const result = await collectResultWithRateLimitRetry({
186
267
  query: this.runQuery,
187
268
  prompt: [input.system, input.prompt].filter(Boolean).join('\n\n'),
188
269
  options,
189
270
  allowedToolIds: new Set(mcpToolIds(input.tools ?? {})),
190
271
  expectedMcpServerNames: expectedMcpServerNames(input.tools),
272
+ rateLimitGovernor: this.deps.rateLimitGovernor,
273
+ abortSignal: input.abortSignal,
191
274
  });
192
275
  input.onMetrics?.({ totalMs: Date.now() - startedAt, usage: claudeTokenUsage(result) });
193
276
  const error = resultError(result);
@@ -216,12 +299,14 @@ export class ClaudeCodeKtxLlmRuntime {
216
299
  outputFormat: { type: 'json_schema', schema: jsonSchema(input.schema) },
217
300
  };
218
301
  const startedAt = Date.now();
219
- const result = await collectResult({
302
+ const result = await collectResultWithRateLimitRetry({
220
303
  query: this.runQuery,
221
304
  prompt: [input.system, input.prompt].filter(Boolean).join('\n\n'),
222
305
  options,
223
306
  allowedToolIds: new Set([...mcpToolIds(input.tools ?? {}), STRUCTURED_OUTPUT_TOOL_NAME]),
224
307
  expectedMcpServerNames: expectedMcpServerNames(input.tools),
308
+ rateLimitGovernor: this.deps.rateLimitGovernor,
309
+ abortSignal: input.abortSignal,
225
310
  });
226
311
  input.onMetrics?.({ totalMs: Date.now() - startedAt, usage: claudeTokenUsage(result) });
227
312
  const error = resultError(result);
@@ -234,9 +319,7 @@ export class ClaudeCodeKtxLlmRuntime {
234
319
  return input.schema.parse(result.structured_output);
235
320
  }
236
321
  async runAgentLoop(params) {
237
- let stepIndex = 0;
238
322
  const startedAt = Date.now();
239
- const stepBoundariesMs = [];
240
323
  try {
241
324
  const options = baseOptions({
242
325
  projectDir: this.deps.projectDir,
@@ -245,25 +328,14 @@ export class ClaudeCodeKtxLlmRuntime {
245
328
  maxTurns: params.stepBudget,
246
329
  tools: params.toolSet,
247
330
  });
248
- const result = await collectResult({
331
+ const result = await collectResultWithRateLimitRetry({
249
332
  query: this.runQuery,
250
333
  prompt: params.userPrompt,
251
334
  options: { ...options, systemPrompt: params.systemPrompt },
252
335
  allowedToolIds: new Set(mcpToolIds(params.toolSet)),
253
336
  expectedMcpServerNames: expectedMcpServerNames(params.toolSet),
254
- onAssistantTurn: async () => {
255
- stepIndex += 1;
256
- stepBoundariesMs.push(Date.now() - startedAt);
257
- if (!params.onStepFinish) {
258
- return;
259
- }
260
- try {
261
- await params.onStepFinish({ stepIndex, stepBudget: params.stepBudget });
262
- }
263
- catch (error) {
264
- this.logger.warn(`[claude-code-runner] onStepFinish callback threw; ignoring: ${error instanceof Error ? error.message : String(error)}`);
265
- }
266
- },
337
+ rateLimitGovernor: this.deps.rateLimitGovernor,
338
+ abortSignal: params.abortSignal,
267
339
  });
268
340
  const stopReason = mapClaudeCodeStopReason(result);
269
341
  const error = resultError(result);
@@ -272,18 +344,25 @@ export class ClaudeCodeKtxLlmRuntime {
272
344
  ...(stopReason === 'error' && error ? { error } : {}),
273
345
  metrics: {
274
346
  totalMs: Date.now() - startedAt,
275
- stepCount: stepIndex,
276
- stepBoundariesMs,
347
+ // Authoritative turn count from the SDK result. The runtime no longer
348
+ // re-derives a per-turn counter: it could not match the SDK's `num_turns`
349
+ // and overshot `maxTurns` (the source of the misleading `step 70/40`).
350
+ // Per-step boundaries require that counter and are not consumed anywhere.
351
+ stepCount: result.num_turns,
352
+ stepBoundariesMs: [],
277
353
  usage: claudeTokenUsage(result),
278
354
  },
279
355
  };
280
356
  }
281
357
  catch (error) {
358
+ if (isAbortError(error)) {
359
+ throw error;
360
+ }
282
361
  const err = error instanceof Error ? error : new Error(String(error));
283
362
  return {
284
363
  stopReason: 'error',
285
364
  error: err,
286
- metrics: { totalMs: Date.now() - startedAt, stepCount: stepIndex, stepBoundariesMs, usage: {} },
365
+ metrics: { totalMs: Date.now() - startedAt, stepCount: 0, stepBoundariesMs: [], usage: {} },
287
366
  };
288
367
  }
289
368
  }
@@ -306,7 +385,7 @@ export async function runClaudeCodeAuthProbe(input) {
306
385
  env: input.env,
307
386
  maxTurns: 1,
308
387
  });
309
- const result = await collectResult({
388
+ const result = await collectResultWithRateLimitRetry({
310
389
  query: input.query ?? defaultQuery,
311
390
  prompt: 'Reply with exactly: ok',
312
391
  options,
@@ -0,0 +1,20 @@
1
+ import type { LlmTokenUsage, RunLoopStopReason } from './runtime-port.js';
2
+ export interface CodexExecEventSummary {
3
+ finalText: string;
4
+ stopReason: RunLoopStopReason;
5
+ usage: LlmTokenUsage;
6
+ stepCount: number;
7
+ stepBoundariesMs: number[];
8
+ toolCallCount: number;
9
+ toolFailures: string[];
10
+ error?: Error;
11
+ }
12
+ interface CodexEventParseOptions {
13
+ startedAt?: number;
14
+ now?: () => number;
15
+ }
16
+ export declare function isCompletedAgentStep(event: unknown): boolean;
17
+ /** @internal */
18
+ export declare function parseCodexExecEventLine(line: string): unknown;
19
+ export declare function summarizeCodexExecEvents(events: Iterable<unknown>, options?: CodexEventParseOptions): CodexExecEventSummary;
20
+ export {};
@@ -0,0 +1,155 @@
1
+ function record(value) {
2
+ return value && typeof value === 'object' ? value : undefined;
3
+ }
4
+ /**
5
+ * Codex thread items that represent a discrete agent action consuming one loop
6
+ * step. The step budget caps the total number of these regardless of which
7
+ * capability the agent reaches for, so built-in `command_execution` (and any
8
+ * file/web action the public Codex surface still exposes) count alongside our
9
+ * own `mcp_tool_call` items rather than only the MCP ones.
10
+ */
11
+ const AGENT_STEP_ITEM_TYPES = new Set(['command_execution', 'mcp_tool_call', 'file_change', 'web_search']);
12
+ export function isCompletedAgentStep(event) {
13
+ const eventRecord = record(event);
14
+ if (eventRecord?.type !== 'item.completed') {
15
+ return false;
16
+ }
17
+ const itemType = record(eventRecord.item)?.type;
18
+ return typeof itemType === 'string' && AGENT_STEP_ITEM_TYPES.has(itemType);
19
+ }
20
+ function text(value) {
21
+ return typeof value === 'string' && value.trim().length > 0 ? value : undefined;
22
+ }
23
+ function numberValue(value) {
24
+ return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
25
+ }
26
+ function usageFrom(value) {
27
+ const usage = record(value);
28
+ if (!usage) {
29
+ return {};
30
+ }
31
+ const inputTokens = numberValue(usage.input_tokens ?? usage.inputTokens);
32
+ const outputTokens = numberValue(usage.output_tokens ?? usage.outputTokens);
33
+ const explicitTotalTokens = numberValue(usage.total_tokens ?? usage.totalTokens);
34
+ const totalTokens = explicitTotalTokens ??
35
+ (inputTokens !== undefined && outputTokens !== undefined ? inputTokens + outputTokens : undefined);
36
+ return {
37
+ ...(inputTokens !== undefined ? { inputTokens } : {}),
38
+ ...(outputTokens !== undefined ? { outputTokens } : {}),
39
+ ...(totalTokens !== undefined ? { totalTokens } : {}),
40
+ };
41
+ }
42
+ function stopReasonFrom(value) {
43
+ const reason = text(value)?.toLowerCase();
44
+ if (reason && /(budget|max_turn|max-turn|limit)/.test(reason)) {
45
+ return 'budget';
46
+ }
47
+ return 'natural';
48
+ }
49
+ function errorMessageFrom(value) {
50
+ if (value instanceof Error) {
51
+ return value.message;
52
+ }
53
+ const asRecord = record(value);
54
+ const message = text(asRecord?.message);
55
+ return message ?? text(value) ?? 'Codex turn failed';
56
+ }
57
+ /**
58
+ * Codex serializes API failures as a JSON envelope inside the event message
59
+ * (e.g. `{"type":"error","status":400,"error":{"message":"…"}}`). Surface the
60
+ * human-readable inner message so callers don't leak raw JSON; pass plain
61
+ * strings through unchanged.
62
+ */
63
+ function unwrapCodexApiErrorMessage(raw) {
64
+ const trimmed = raw.trim();
65
+ if (!trimmed.startsWith('{')) {
66
+ return raw;
67
+ }
68
+ try {
69
+ const parsed = record(JSON.parse(trimmed));
70
+ return text(record(parsed?.error)?.message) ?? text(parsed?.message) ?? raw;
71
+ }
72
+ catch {
73
+ return raw;
74
+ }
75
+ }
76
+ /** @internal */
77
+ export function parseCodexExecEventLine(line) {
78
+ try {
79
+ return JSON.parse(line);
80
+ }
81
+ catch (error) {
82
+ throw new Error(`Codex JSONL event stream was malformed: ${error instanceof Error ? error.message : String(error)}`);
83
+ }
84
+ }
85
+ export function summarizeCodexExecEvents(events, options = {}) {
86
+ const startedAt = options.startedAt ?? Date.now();
87
+ const now = options.now ?? Date.now;
88
+ let finalText = '';
89
+ let stopReason = 'natural';
90
+ let usage = {};
91
+ let turnCount = 0;
92
+ let completedStepCount = 0;
93
+ const stepBoundariesMs = [];
94
+ let toolCallCount = 0;
95
+ const toolFailures = [];
96
+ let error;
97
+ for (const event of events) {
98
+ const eventRecord = record(event);
99
+ const eventType = text(eventRecord?.type);
100
+ if (!eventRecord || !eventType) {
101
+ continue;
102
+ }
103
+ if (eventType === 'turn.started') {
104
+ turnCount += 1;
105
+ continue;
106
+ }
107
+ const item = record(eventRecord.item);
108
+ const itemType = text(item?.type);
109
+ if (eventType === 'item.started' && itemType === 'mcp_tool_call') {
110
+ toolCallCount += 1;
111
+ continue;
112
+ }
113
+ if (isCompletedAgentStep(event)) {
114
+ completedStepCount += 1;
115
+ stepBoundariesMs.push(now() - startedAt);
116
+ // Only MCP tool calls fail the loop: a non-zero `command_execution` exit
117
+ // is normal agent exploration, not a runtime error. `status` is the
118
+ // authoritative signal (the SDK always sets it); the SDK also serializes
119
+ // `error: null` on successful calls, so an explicit-null `error` must NOT
120
+ // be read as a failure — only a populated error object counts.
121
+ if (itemType === 'mcp_tool_call' && (item?.status === 'failed' || (item?.error !== undefined && item?.error !== null))) {
122
+ const name = text(item?.name) ?? text(item?.tool) ?? text(item?.tool_name) ?? 'unknown';
123
+ toolFailures.push(`${name}: ${errorMessageFrom(item?.error)}`);
124
+ }
125
+ continue;
126
+ }
127
+ if (eventType === 'item.completed' && itemType === 'agent_message') {
128
+ finalText = text(item?.text) ?? finalText;
129
+ continue;
130
+ }
131
+ if (eventType === 'turn.completed') {
132
+ usage = usageFrom(eventRecord.usage);
133
+ if (completedStepCount === 0) {
134
+ stepBoundariesMs.push(now() - startedAt);
135
+ }
136
+ stopReason = stopReasonFrom(eventRecord.reason ?? eventRecord.stop_reason ?? eventRecord.terminal_reason);
137
+ continue;
138
+ }
139
+ if (eventType === 'turn.failed' || eventType === 'error') {
140
+ stopReason = 'error';
141
+ error = new Error(unwrapCodexApiErrorMessage(errorMessageFrom(eventRecord.error ?? eventRecord.message)));
142
+ continue;
143
+ }
144
+ }
145
+ return {
146
+ finalText,
147
+ stopReason,
148
+ usage,
149
+ stepCount: completedStepCount > 0 ? completedStepCount : turnCount,
150
+ stepBoundariesMs,
151
+ toolCallCount,
152
+ toolFailures,
153
+ ...(error ? { error } : {}),
154
+ };
155
+ }
@@ -0,0 +1,3 @@
1
+ export declare const CODEX_ISOLATION_WARNING = "Codex backend isolation is limited by the public Codex SDK/CLI surface: ktx restricts the runtime MCP server to the current ktx tool set, disables Codex web search, asks for a read-only sandbox, and sets approval_policy=never, but Codex may still load user Codex config and built-in command execution or read-only file capabilities.";
2
+ export declare const CODEX_ISOLATION_WARNING_FIX = "Use llm.provider.backend: claude-code when you need stricter Claude-Code-style runtime tool isolation, or remove host Codex MCP/tool config before running untrusted prompts through the codex backend.";
3
+ export declare function formatCodexIsolationWarning(): string;
@@ -0,0 +1,5 @@
1
+ export const CODEX_ISOLATION_WARNING = 'Codex backend isolation is limited by the public Codex SDK/CLI surface: ktx restricts the runtime MCP server to the current ktx tool set, disables Codex web search, asks for a read-only sandbox, and sets approval_policy=never, but Codex may still load user Codex config and built-in command execution or read-only file capabilities.';
2
+ export const CODEX_ISOLATION_WARNING_FIX = 'Use llm.provider.backend: claude-code when you need stricter Claude-Code-style runtime tool isolation, or remove host Codex MCP/tool config before running untrusted prompts through the codex backend.';
3
+ export function formatCodexIsolationWarning() {
4
+ return `${CODEX_ISOLATION_WARNING} ${CODEX_ISOLATION_WARNING_FIX}`;
5
+ }
@@ -0,0 +1,24 @@
1
+ import type { KtxMcpServerLike } from '../mcp/types.js';
2
+ import { runKtxMcpHttpServer } from '../../mcp-http-server.js';
3
+ import type { KtxRuntimeToolSet } from './runtime-port.js';
4
+ /** @internal */
5
+ export interface CreateCodexRuntimeMcpServerInput {
6
+ server?: KtxMcpServerLike;
7
+ toolSet: KtxRuntimeToolSet;
8
+ }
9
+ export interface CodexRuntimeMcpServerHandle {
10
+ url: string;
11
+ bearerTokenEnvVar: 'KTX_CODEX_RUNTIME_MCP_TOKEN';
12
+ bearerToken: string;
13
+ close(): Promise<void>;
14
+ }
15
+ type RunServer = typeof runKtxMcpHttpServer;
16
+ export interface StartCodexRuntimeMcpServerInput {
17
+ projectDir: string;
18
+ toolSet: KtxRuntimeToolSet;
19
+ runServer?: RunServer;
20
+ }
21
+ /** @internal */
22
+ export declare function createCodexRuntimeMcpServer(input: CreateCodexRuntimeMcpServerInput): KtxMcpServerLike;
23
+ export declare function startCodexRuntimeMcpServer(input: StartCodexRuntimeMcpServerInput): Promise<CodexRuntimeMcpServerHandle>;
24
+ export {};
@@ -0,0 +1,51 @@
1
+ import { randomBytes } from 'node:crypto';
2
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
3
+ import { runKtxMcpHttpServer } from '../../mcp-http-server.js';
4
+ import { normalizeKtxRuntimeToolOutput } from './runtime-tools.js';
5
+ /** @internal */
6
+ export function createCodexRuntimeMcpServer(input) {
7
+ const server = input.server ??
8
+ new McpServer({
9
+ name: 'ktx-runtime',
10
+ version: '0.0.0',
11
+ });
12
+ for (const descriptor of Object.values(input.toolSet)) {
13
+ server.registerTool(descriptor.name, {
14
+ description: descriptor.description,
15
+ inputSchema: descriptor.inputSchema.shape,
16
+ }, async (toolInput) => {
17
+ const normalized = normalizeKtxRuntimeToolOutput(await descriptor.execute(toolInput));
18
+ return {
19
+ content: [{ type: 'text', text: normalized.markdown }],
20
+ ...(normalized.structured !== undefined && normalized.structured !== null && typeof normalized.structured === 'object'
21
+ ? { structuredContent: normalized.structured }
22
+ : {}),
23
+ };
24
+ });
25
+ }
26
+ return server;
27
+ }
28
+ function serverPort(server, fallback) {
29
+ const address = server.address();
30
+ return typeof address === 'object' && address ? address.port : fallback;
31
+ }
32
+ export async function startCodexRuntimeMcpServer(input) {
33
+ const bearerToken = randomBytes(32).toString('hex');
34
+ const runServer = input.runServer ?? runKtxMcpHttpServer;
35
+ const handle = (await runServer({
36
+ projectDir: input.projectDir,
37
+ host: '127.0.0.1',
38
+ port: 0,
39
+ token: bearerToken,
40
+ allowedHosts: ['127.0.0.1', 'localhost'],
41
+ allowedOrigins: [],
42
+ createMcpServer: () => createCodexRuntimeMcpServer({ toolSet: input.toolSet }),
43
+ }));
44
+ const port = serverPort(handle.server, 0);
45
+ return {
46
+ url: `http://127.0.0.1:${port}/mcp`,
47
+ bearerTokenEnvVar: 'KTX_CODEX_RUNTIME_MCP_TOKEN',
48
+ bearerToken,
49
+ close: () => handle.close(),
50
+ };
51
+ }
@@ -0,0 +1,2 @@
1
+ export declare const DEFAULT_CODEX_MODEL = "gpt-5.5";
2
+ export declare function resolveCodexModel(model: string): string;
@@ -0,0 +1,17 @@
1
+ export const DEFAULT_CODEX_MODEL = 'gpt-5.5';
2
+ const CODEX_MODEL_ALIASES = {
3
+ codex: DEFAULT_CODEX_MODEL,
4
+ default: DEFAULT_CODEX_MODEL,
5
+ };
6
+ const EXPLICIT_CODEX_MODEL_ID = /^(?:gpt|codex)-[a-z0-9][a-z0-9._-]*$/i;
7
+ export function resolveCodexModel(model) {
8
+ const normalized = model.trim();
9
+ const alias = CODEX_MODEL_ALIASES[normalized];
10
+ if (alias) {
11
+ return alias;
12
+ }
13
+ if (EXPLICIT_CODEX_MODEL_ID.test(normalized)) {
14
+ return normalized;
15
+ }
16
+ throw new Error(`Unsupported Codex model "${model}". Use codex, default, or a gpt-* / codex-* model id.`);
17
+ }
@@ -0,0 +1,16 @@
1
+ interface CodexRuntimeMcpConfig {
2
+ url: string;
3
+ bearerTokenEnvVar: string;
4
+ bearerToken: string;
5
+ toolNames: string[];
6
+ }
7
+ export interface BuildCodexRuntimeConfigInput {
8
+ model: string;
9
+ mcp?: CodexRuntimeMcpConfig;
10
+ }
11
+ export interface CodexRuntimeConfig {
12
+ configOverrides: Record<string, unknown>;
13
+ env: Record<string, string>;
14
+ }
15
+ export declare function buildCodexRuntimeConfig(input: BuildCodexRuntimeConfigInput): CodexRuntimeConfig;
16
+ export {};
@@ -0,0 +1,19 @@
1
+ export function buildCodexRuntimeConfig(input) {
2
+ const configOverrides = {
3
+ history: { persistence: 'none' },
4
+ };
5
+ const env = {};
6
+ if (input.mcp) {
7
+ configOverrides.mcp_servers = {
8
+ ktx: {
9
+ url: input.mcp.url,
10
+ bearer_token_env_var: input.mcp.bearerTokenEnvVar,
11
+ enabled_tools: input.mcp.toolNames,
12
+ default_tools_approval_mode: 'approve',
13
+ required: true,
14
+ },
15
+ };
16
+ env[input.mcp.bearerTokenEnvVar] = input.mcp.bearerToken;
17
+ }
18
+ return { configOverrides, env };
19
+ }
@@ -0,0 +1,37 @@
1
+ import { z } from 'zod';
2
+ import { type CodexRuntimeMcpServerHandle } from './codex-mcp-runtime-server.js';
3
+ import { type CodexSdkRunner } from './codex-sdk-runner.js';
4
+ import type { RateLimitGovernor } from './rate-limit-governor.js';
5
+ import type { KtxGenerateObjectInput, KtxGenerateTextInput, KtxLlmRuntimePort, KtxRuntimeToolSet, RunLoopParams, RunLoopResult } from './runtime-port.js';
6
+ export interface CodexKtxLlmRuntimeDeps {
7
+ projectDir: string;
8
+ modelSlots: {
9
+ default: string;
10
+ } & Partial<Record<string, string>>;
11
+ runner?: CodexSdkRunner;
12
+ startMcpServer?: (input: {
13
+ projectDir: string;
14
+ toolSet: KtxRuntimeToolSet;
15
+ }) => Promise<CodexRuntimeMcpServerHandle>;
16
+ rateLimitGovernor?: Pick<RateLimitGovernor, 'waitForReady' | 'report' | 'maxRetryAttempts'>;
17
+ }
18
+ export declare class CodexKtxLlmRuntime implements KtxLlmRuntimePort {
19
+ private readonly deps;
20
+ private readonly runner;
21
+ constructor(deps: CodexKtxLlmRuntimeDeps);
22
+ private runWithRateLimitRetry;
23
+ generateText(input: KtxGenerateTextInput): Promise<string>;
24
+ generateObject<TOutput, TSchema extends z.ZodType<TOutput>>(input: KtxGenerateObjectInput<TOutput, TSchema>): Promise<TOutput>;
25
+ runAgentLoop(params: RunLoopParams): Promise<RunLoopResult>;
26
+ }
27
+ export declare function runCodexAuthProbe(input: {
28
+ projectDir: string;
29
+ model: string;
30
+ runner?: CodexSdkRunner;
31
+ }): Promise<{
32
+ ok: true;
33
+ } | {
34
+ ok: false;
35
+ message: string;
36
+ fix: string;
37
+ }>;