@albinocrabs/o-switcher 0.1.0 → 0.2.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.
package/dist/index.cjs CHANGED
@@ -1,428 +1,7 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var index_exports = {};
32
- __export(index_exports, {
33
- ADMISSION_RESULTS: () => ADMISSION_RESULTS,
34
- BackoffConfigSchema: () => BackoffConfigSchema,
35
- ConfigValidationError: () => ConfigValidationError,
36
- DEFAULT_ALPHA: () => DEFAULT_ALPHA,
37
- DEFAULT_BACKOFF_BASE_MS: () => DEFAULT_BACKOFF_BASE_MS,
38
- DEFAULT_BACKOFF_JITTER: () => DEFAULT_BACKOFF_JITTER,
39
- DEFAULT_BACKOFF_MAX_MS: () => DEFAULT_BACKOFF_MAX_MS,
40
- DEFAULT_BACKOFF_MULTIPLIER: () => DEFAULT_BACKOFF_MULTIPLIER,
41
- DEFAULT_BACKOFF_PARAMS: () => DEFAULT_BACKOFF_PARAMS,
42
- DEFAULT_FAILOVER_BUDGET: () => DEFAULT_FAILOVER_BUDGET,
43
- DEFAULT_RETRY: () => DEFAULT_RETRY,
44
- DEFAULT_RETRY_BUDGET: () => DEFAULT_RETRY_BUDGET,
45
- DEFAULT_TIMEOUT_MS: () => DEFAULT_TIMEOUT_MS,
46
- DualBreaker: () => DualBreaker,
47
- EXCLUSION_REASONS: () => EXCLUSION_REASONS,
48
- ErrorClassSchema: () => ErrorClassSchema,
49
- HEURISTIC_PATTERNS: () => HEURISTIC_PATTERNS,
50
- INITIAL_HEALTH_SCORE: () => INITIAL_HEALTH_SCORE,
51
- PROVIDER_PATTERNS: () => PROVIDER_PATTERNS,
52
- REDACT_PATHS: () => REDACT_PATHS,
53
- SwitcherConfigSchema: () => SwitcherConfigSchema,
54
- TARGET_STATES: () => TARGET_STATES,
55
- TEMPORAL_QUOTA_PATTERN: () => TEMPORAL_QUOTA_PATTERN,
56
- TargetConfigSchema: () => TargetConfigSchema,
57
- TargetRegistry: () => TargetRegistry,
58
- addProfile: () => addProfile,
59
- applyConfigDiff: () => applyConfigDiff,
60
- checkHardRejects: () => checkHardRejects,
61
- classify: () => classify,
62
- computeBackoffMs: () => computeBackoffMs,
63
- computeConfigDiff: () => computeConfigDiff,
64
- computeCooldownMs: () => computeCooldownMs,
65
- computeScore: () => computeScore,
66
- createAdmissionController: () => createAdmissionController,
67
- createAuditCollector: () => createAuditCollector,
68
- createAuditLogger: () => createAuditLogger,
69
- createAuthWatcher: () => createAuthWatcher,
70
- createCircuitBreaker: () => createCircuitBreaker,
71
- createConcurrencyTracker: () => createConcurrencyTracker,
72
- createCooldownManager: () => createCooldownManager,
73
- createExecutionOrchestrator: () => createExecutionOrchestrator,
74
- createFailoverOrchestrator: () => createFailoverOrchestrator,
75
- createLogSubscriber: () => createLogSubscriber,
76
- createModeAdapter: () => createModeAdapter,
77
- createOperatorTools: () => createOperatorTools,
78
- createProfileTools: () => createProfileTools,
79
- createRegistry: () => createRegistry,
80
- createRequestLogger: () => createRequestLogger,
81
- createRequestTraceBuffer: () => createRequestTraceBuffer,
82
- createRetryPolicy: () => createRetryPolicy,
83
- createRoutingEventBus: () => createRoutingEventBus,
84
- createStreamBuffer: () => createStreamBuffer,
85
- createStreamStitcher: () => createStreamStitcher,
86
- detectDeploymentMode: () => detectDeploymentMode,
87
- determineContinuationMode: () => determineContinuationMode,
88
- directSignalFromResponse: () => directSignalFromResponse,
89
- disableTarget: () => disableTarget,
90
- discoverTargets: () => discoverTargets,
91
- discoverTargetsFromProfiles: () => discoverTargetsFromProfiles,
92
- drainTarget: () => drainTarget,
93
- extractRetryAfterMs: () => extractRetryAfterMs,
94
- generateCorrelationId: () => generateCorrelationId,
95
- getExclusionReason: () => getExclusionReason,
96
- getModeCapabilities: () => getModeCapabilities,
97
- getSignalFidelity: () => getSignalFidelity,
98
- getTargetStateTransition: () => getTargetStateTransition,
99
- heuristicSignalFromEvent: () => heuristicSignalFromEvent,
100
- inspectRequest: () => inspectRequest,
101
- isRetryable: () => isRetryable,
102
- listProfiles: () => listProfiles,
103
- listTargets: () => listTargets,
104
- loadProfiles: () => loadProfiles,
105
- nextProfileId: () => nextProfileId,
106
- normalizeLatency: () => normalizeLatency,
107
- pauseTarget: () => pauseTarget,
108
- reloadConfig: () => reloadConfig,
109
- removeProfile: () => removeProfile,
110
- resumeTarget: () => resumeTarget,
111
- saveProfiles: () => saveProfiles,
112
- selectTarget: () => selectTarget,
113
- updateHealthScore: () => updateHealthScore,
114
- updateLatencyEma: () => updateLatencyEma,
115
- validateBearerToken: () => validateBearerToken,
116
- validateConfig: () => validateConfig
117
- });
118
- module.exports = __toCommonJS(index_exports);
1
+ 'use strict';
119
2
 
120
- // src/config/schema.ts
121
- var import_zod = require("zod");
122
-
123
- // src/config/defaults.ts
124
- var DEFAULT_RETRY_BUDGET = 3;
125
- var DEFAULT_FAILOVER_BUDGET = 2;
126
- var DEFAULT_BACKOFF_BASE_MS = 1e3;
127
- var DEFAULT_BACKOFF_MULTIPLIER = 2;
128
- var DEFAULT_BACKOFF_MAX_MS = 3e4;
129
- var DEFAULT_BACKOFF_JITTER = "full";
130
- var DEFAULT_ROUTING_WEIGHT_HEALTH = 1;
131
- var DEFAULT_ROUTING_WEIGHT_LATENCY = 0.5;
132
- var DEFAULT_ROUTING_WEIGHT_FAILURE = 0.8;
133
- var DEFAULT_ROUTING_WEIGHT_PRIORITY = 0.3;
134
- var DEFAULT_MAX_EXPECTED_LATENCY_MS = 3e4;
135
- var DEFAULT_QUEUE_LIMIT = 100;
136
- var DEFAULT_GLOBAL_CONCURRENCY_LIMIT = 10;
137
- var DEFAULT_BACKPRESSURE_THRESHOLD = 50;
138
- var DEFAULT_CB_FAILURE_THRESHOLD = 5;
139
- var DEFAULT_CB_FAILURE_RATE_THRESHOLD = 0.5;
140
- var DEFAULT_CB_SLIDING_WINDOW_SIZE = 10;
141
- var DEFAULT_CB_HALF_OPEN_AFTER_MS = 3e4;
142
- var DEFAULT_CB_HALF_OPEN_MAX_PROBES = 1;
143
- var DEFAULT_CB_SUCCESS_THRESHOLD = 2;
144
- var DEFAULT_RETRY = 3;
145
- var DEFAULT_TIMEOUT_MS = 3e4;
146
-
147
- // src/config/schema.ts
148
- var BackoffConfigSchema = import_zod.z.object({
149
- base_ms: import_zod.z.number().positive().default(DEFAULT_BACKOFF_BASE_MS),
150
- multiplier: import_zod.z.number().positive().default(DEFAULT_BACKOFF_MULTIPLIER),
151
- max_ms: import_zod.z.number().positive().default(DEFAULT_BACKOFF_MAX_MS),
152
- jitter: import_zod.z.enum(["full", "equal", "none"]).default(DEFAULT_BACKOFF_JITTER)
153
- });
154
- var RoutingWeightsSchema = import_zod.z.object({
155
- health: import_zod.z.number().default(DEFAULT_ROUTING_WEIGHT_HEALTH),
156
- latency: import_zod.z.number().default(DEFAULT_ROUTING_WEIGHT_LATENCY),
157
- failure: import_zod.z.number().default(DEFAULT_ROUTING_WEIGHT_FAILURE),
158
- priority: import_zod.z.number().default(DEFAULT_ROUTING_WEIGHT_PRIORITY)
159
- });
160
- var CircuitBreakerConfigSchema = import_zod.z.object({
161
- failure_threshold: import_zod.z.number().int().positive().default(DEFAULT_CB_FAILURE_THRESHOLD),
162
- failure_rate_threshold: import_zod.z.number().gt(0).lt(1).default(DEFAULT_CB_FAILURE_RATE_THRESHOLD),
163
- sliding_window_size: import_zod.z.number().int().positive().default(DEFAULT_CB_SLIDING_WINDOW_SIZE),
164
- half_open_after_ms: import_zod.z.number().positive().default(DEFAULT_CB_HALF_OPEN_AFTER_MS),
165
- half_open_max_probes: import_zod.z.number().int().positive().default(DEFAULT_CB_HALF_OPEN_MAX_PROBES),
166
- success_threshold: import_zod.z.number().int().positive().default(DEFAULT_CB_SUCCESS_THRESHOLD)
167
- });
168
- var TargetConfigSchema = import_zod.z.object({
169
- target_id: import_zod.z.string().min(1),
170
- provider_id: import_zod.z.string().min(1),
171
- profile: import_zod.z.string().optional(),
172
- endpoint_id: import_zod.z.string().optional(),
173
- capabilities: import_zod.z.array(import_zod.z.string()).default([]),
174
- enabled: import_zod.z.boolean().default(true),
175
- operator_priority: import_zod.z.number().int().default(0),
176
- policy_tags: import_zod.z.array(import_zod.z.string()).default([]),
177
- retry_budget: import_zod.z.number().int().positive().optional(),
178
- failover_budget: import_zod.z.number().int().positive().optional(),
179
- backoff: BackoffConfigSchema.optional(),
180
- concurrency_limit: import_zod.z.number().int().positive().optional(),
181
- circuit_breaker: CircuitBreakerConfigSchema.optional()
182
- });
183
- var SwitcherConfigSchema = import_zod.z.object({
184
- targets: import_zod.z.array(TargetConfigSchema).min(1).optional(),
185
- retry: import_zod.z.number().int().positive().optional(),
186
- timeout: import_zod.z.number().positive().optional(),
187
- retry_budget: import_zod.z.number().int().positive().default(DEFAULT_RETRY_BUDGET),
188
- failover_budget: import_zod.z.number().int().positive().default(DEFAULT_FAILOVER_BUDGET),
189
- backoff: BackoffConfigSchema.default({
190
- base_ms: DEFAULT_BACKOFF_BASE_MS,
191
- multiplier: DEFAULT_BACKOFF_MULTIPLIER,
192
- max_ms: DEFAULT_BACKOFF_MAX_MS,
193
- jitter: DEFAULT_BACKOFF_JITTER
194
- }),
195
- deployment_mode_hint: import_zod.z.enum(["plugin-only", "server-companion", "sdk-control", "auto"]).default("auto"),
196
- routing_weights: RoutingWeightsSchema.default({
197
- health: DEFAULT_ROUTING_WEIGHT_HEALTH,
198
- latency: DEFAULT_ROUTING_WEIGHT_LATENCY,
199
- failure: DEFAULT_ROUTING_WEIGHT_FAILURE,
200
- priority: DEFAULT_ROUTING_WEIGHT_PRIORITY
201
- }),
202
- queue_limit: import_zod.z.number().int().positive().default(DEFAULT_QUEUE_LIMIT),
203
- concurrency_limit: import_zod.z.number().int().positive().default(DEFAULT_GLOBAL_CONCURRENCY_LIMIT),
204
- backpressure_threshold: import_zod.z.number().int().nonnegative().default(DEFAULT_BACKPRESSURE_THRESHOLD),
205
- circuit_breaker: CircuitBreakerConfigSchema.default({
206
- failure_threshold: DEFAULT_CB_FAILURE_THRESHOLD,
207
- failure_rate_threshold: DEFAULT_CB_FAILURE_RATE_THRESHOLD,
208
- sliding_window_size: DEFAULT_CB_SLIDING_WINDOW_SIZE,
209
- half_open_after_ms: DEFAULT_CB_HALF_OPEN_AFTER_MS,
210
- half_open_max_probes: DEFAULT_CB_HALF_OPEN_MAX_PROBES,
211
- success_threshold: DEFAULT_CB_SUCCESS_THRESHOLD
212
- }),
213
- max_expected_latency_ms: import_zod.z.number().positive().default(DEFAULT_MAX_EXPECTED_LATENCY_MS)
214
- });
215
-
216
- // src/config/loader.ts
217
- var ConfigValidationError = class extends Error {
218
- diagnostics;
219
- constructor(diagnostics) {
220
- const summary = diagnostics.map((d) => ` ${d.path}: ${d.message}`).join("\n");
221
- super(`Invalid O-Switcher configuration:
222
- ${summary}`);
223
- this.name = "ConfigValidationError";
224
- this.diagnostics = diagnostics;
225
- }
226
- };
227
- var validateConfig = (raw) => {
228
- const result = SwitcherConfigSchema.safeParse(raw);
229
- if (result.success) {
230
- const data = { ...result.data };
231
- if (data.retry !== void 0 && data.retry_budget === DEFAULT_RETRY_BUDGET) {
232
- data.retry_budget = data.retry;
233
- }
234
- if (data.timeout !== void 0 && data.max_expected_latency_ms === DEFAULT_MAX_EXPECTED_LATENCY_MS) {
235
- data.max_expected_latency_ms = data.timeout;
236
- }
237
- if (data.targets !== void 0) {
238
- const seen = /* @__PURE__ */ new Set();
239
- const duplicateDiagnostics = [];
240
- for (const target of data.targets) {
241
- const key = `${target.provider_id}::${target.profile ?? "__default__"}`;
242
- if (seen.has(key)) {
243
- duplicateDiagnostics.push({
244
- path: "targets",
245
- message: `Duplicate provider_id + profile combination: ${key}`,
246
- received: key
247
- });
248
- }
249
- seen.add(key);
250
- }
251
- if (duplicateDiagnostics.length > 0) {
252
- throw new ConfigValidationError(duplicateDiagnostics);
253
- }
254
- }
255
- return Object.freeze(data);
256
- }
257
- const diagnostics = result.error.issues.map(
258
- (issue) => ({
259
- path: issue.path.join("."),
260
- message: issue.message,
261
- received: "expected" in issue ? issue.expected : void 0
262
- })
263
- );
264
- throw new ConfigValidationError(diagnostics);
265
- };
266
-
267
- // src/config/discovery.ts
268
- var discoverTargets = (providerConfig) => {
269
- const targets = [];
270
- for (const [key, value] of Object.entries(providerConfig)) {
271
- if (!value) continue;
272
- const raw = {
273
- target_id: key,
274
- provider_id: value.id ?? key,
275
- capabilities: ["chat"],
276
- enabled: true,
277
- operator_priority: 0,
278
- policy_tags: []
279
- };
280
- const parsed = TargetConfigSchema.parse(raw);
281
- targets.push(parsed);
282
- }
283
- return targets;
284
- };
285
- var discoverTargetsFromProfiles = (store) => {
286
- const targets = [];
287
- for (const entry of Object.values(store)) {
288
- if (!entry) continue;
289
- const raw = {
290
- target_id: entry.id,
291
- provider_id: entry.provider,
292
- profile: entry.id,
293
- capabilities: ["chat"],
294
- enabled: true,
295
- operator_priority: 0,
296
- policy_tags: []
297
- };
298
- const parsed = TargetConfigSchema.parse(raw);
299
- targets.push(parsed);
300
- }
301
- return targets;
302
- };
303
-
304
- // src/registry/health.ts
305
- var INITIAL_HEALTH_SCORE = 1;
306
- var DEFAULT_ALPHA = 0.1;
307
- var updateHealthScore = (currentScore, observation, alpha = DEFAULT_ALPHA) => alpha * observation + (1 - alpha) * currentScore;
308
- var updateLatencyEma = (currentEma, observedMs, alpha = DEFAULT_ALPHA) => alpha * observedMs + (1 - alpha) * currentEma;
309
-
310
- // src/registry/registry.ts
311
- var TargetRegistry = class {
312
- targets;
313
- constructor(config) {
314
- this.targets = new Map(
315
- (config.targets ?? []).map((t) => [
316
- t.target_id,
317
- {
318
- target_id: t.target_id,
319
- provider_id: t.provider_id,
320
- profile: t.profile,
321
- endpoint_id: t.endpoint_id,
322
- capabilities: [...t.capabilities],
323
- enabled: t.enabled,
324
- state: "Active",
325
- health_score: INITIAL_HEALTH_SCORE,
326
- cooldown_until: null,
327
- latency_ema_ms: 0,
328
- failure_score: 0,
329
- operator_priority: t.operator_priority,
330
- policy_tags: [...t.policy_tags]
331
- }
332
- ])
333
- );
334
- }
335
- /** Returns the target entry for the given id, or undefined if not found. */
336
- getTarget(id) {
337
- return this.targets.get(id);
338
- }
339
- /** Returns a readonly array of all target entries. */
340
- getAllTargets() {
341
- return [...this.targets.values()];
342
- }
343
- /**
344
- * Updates the state of a target.
345
- * @returns true if the target was found and updated, false otherwise.
346
- */
347
- updateState(id, newState) {
348
- const target = this.targets.get(id);
349
- if (!target) {
350
- return false;
351
- }
352
- this.targets.set(id, { ...target, state: newState });
353
- return true;
354
- }
355
- /**
356
- * Records a success (1) or failure (0) observation for a target.
357
- * Updates health_score via EMA.
358
- */
359
- recordObservation(id, observation) {
360
- const target = this.targets.get(id);
361
- if (!target) {
362
- return;
363
- }
364
- const newHealthScore = updateHealthScore(
365
- target.health_score,
366
- observation
367
- );
368
- this.targets.set(id, { ...target, health_score: newHealthScore });
369
- }
370
- /** Sets the cooldown_until timestamp for a target. */
371
- setCooldown(id, untilMs) {
372
- const target = this.targets.get(id);
373
- if (!target) {
374
- return;
375
- }
376
- this.targets.set(id, { ...target, cooldown_until: untilMs });
377
- }
378
- /** Updates the latency EMA for a target. */
379
- updateLatency(id, ms) {
380
- const target = this.targets.get(id);
381
- if (!target) {
382
- return;
383
- }
384
- const newLatency = updateLatencyEma(target.latency_ema_ms, ms);
385
- this.targets.set(id, { ...target, latency_ema_ms: newLatency });
386
- }
387
- /**
388
- * Adds a new target entry to the registry.
389
- * @returns true if added, false if target_id already exists.
390
- */
391
- addTarget(entry) {
392
- if (this.targets.has(entry.target_id)) {
393
- return false;
394
- }
395
- this.targets.set(entry.target_id, entry);
396
- return true;
397
- }
398
- /**
399
- * Removes a target entry from the registry.
400
- * @returns true if found and removed, false if not found.
401
- */
402
- removeTarget(id) {
403
- return this.targets.delete(id);
404
- }
405
- /** Returns a deep-frozen snapshot of all targets. */
406
- getSnapshot() {
407
- const snapshot = {
408
- targets: [...this.targets.values()].map((t) => ({ ...t }))
409
- };
410
- return Object.freeze(snapshot);
411
- }
412
- };
413
- var createRegistry = (config) => new TargetRegistry(config);
414
-
415
- // src/registry/types.ts
416
- var TARGET_STATES = [
417
- "Active",
418
- "CoolingDown",
419
- "ReauthRequired",
420
- "PolicyBlocked",
421
- "CircuitOpen",
422
- "CircuitHalfOpen",
423
- "Draining",
424
- "Disabled"
425
- ];
3
+ var chunkTJ7ZGZHD_cjs = require('./chunk-TJ7ZGZHD.cjs');
4
+ var crypto = require('crypto');
426
5
 
427
6
  // src/mode/detection.ts
428
7
  var detectDeploymentMode = (hint) => {
@@ -456,176 +35,248 @@ var getModeCapabilities = (mode) => {
456
35
  };
457
36
  };
458
37
 
459
- // src/audit/logger.ts
460
- var import_pino = __toESM(require("pino"), 1);
461
- var import_node_crypto = __toESM(require("crypto"), 1);
462
- var REDACT_PATHS = [
463
- "api_key",
464
- "token",
465
- "secret",
466
- "password",
467
- "authorization",
468
- "credential",
469
- "credentials",
470
- "*.api_key",
471
- "*.token",
472
- "*.secret",
473
- "*.password",
474
- "*.authorization",
475
- "*.credential",
476
- "*.credentials"
477
- ];
478
- var createAuditLogger = (options) => {
479
- const opts = {
480
- level: options?.level ?? "info",
481
- redact: {
482
- paths: [...REDACT_PATHS],
483
- censor: "[Redacted]"
484
- }
485
- };
486
- if (options?.destination) {
487
- return (0, import_pino.default)(opts, options.destination);
488
- }
489
- return (0, import_pino.default)(opts);
490
- };
491
- var createRequestLogger = (baseLogger, requestId) => baseLogger.child({ request_id: requestId });
492
- var generateCorrelationId = () => import_node_crypto.default.randomUUID();
493
-
494
- // src/errors/taxonomy.ts
495
- var import_zod2 = require("zod");
496
- var RateLimitedSchema = import_zod2.z.object({
497
- class: import_zod2.z.literal("RateLimited"),
498
- retryable: import_zod2.z.literal(true),
499
- retry_after_ms: import_zod2.z.number().optional(),
500
- provider_reason: import_zod2.z.string().optional()
501
- });
502
- var QuotaExhaustedSchema = import_zod2.z.object({
503
- class: import_zod2.z.literal("QuotaExhausted"),
504
- retryable: import_zod2.z.literal(false),
505
- provider_reason: import_zod2.z.string().optional()
506
- });
507
- var AuthFailureSchema = import_zod2.z.object({
508
- class: import_zod2.z.literal("AuthFailure"),
509
- retryable: import_zod2.z.literal(false),
510
- recovery_attempted: import_zod2.z.boolean().default(false)
511
- });
512
- var PermissionFailureSchema = import_zod2.z.object({
513
- class: import_zod2.z.literal("PermissionFailure"),
514
- retryable: import_zod2.z.literal(false)
515
- });
516
- var PolicyFailureSchema = import_zod2.z.object({
517
- class: import_zod2.z.literal("PolicyFailure"),
518
- retryable: import_zod2.z.literal(false)
519
- });
520
- var RegionRestrictionSchema = import_zod2.z.object({
521
- class: import_zod2.z.literal("RegionRestriction"),
522
- retryable: import_zod2.z.literal(false)
523
- });
524
- var ModelUnavailableSchema = import_zod2.z.object({
525
- class: import_zod2.z.literal("ModelUnavailable"),
526
- retryable: import_zod2.z.literal(false),
527
- failover_eligible: import_zod2.z.literal(true)
528
- });
529
- var TransientServerFailureSchema = import_zod2.z.object({
530
- class: import_zod2.z.literal("TransientServerFailure"),
531
- retryable: import_zod2.z.literal(true),
532
- http_status: import_zod2.z.number().optional()
533
- });
534
- var TransportFailureSchema = import_zod2.z.object({
535
- class: import_zod2.z.literal("TransportFailure"),
536
- retryable: import_zod2.z.literal(true)
537
- });
538
- var InterruptedExecutionSchema = import_zod2.z.object({
539
- class: import_zod2.z.literal("InterruptedExecution"),
540
- retryable: import_zod2.z.literal(true),
541
- partial_output_bytes: import_zod2.z.number().optional()
542
- });
543
- var ErrorClassSchema = import_zod2.z.discriminatedUnion("class", [
544
- RateLimitedSchema,
545
- QuotaExhaustedSchema,
546
- AuthFailureSchema,
547
- PermissionFailureSchema,
548
- PolicyFailureSchema,
549
- RegionRestrictionSchema,
550
- ModelUnavailableSchema,
551
- TransientServerFailureSchema,
552
- TransportFailureSchema,
553
- InterruptedExecutionSchema
554
- ]);
555
- var isRetryable = (errorClass) => errorClass.retryable;
556
- var getTargetStateTransition = (errorClass) => {
557
- switch (errorClass.class) {
558
- case "RateLimited":
559
- return "CoolingDown";
560
- case "QuotaExhausted":
561
- return "Disabled";
562
- case "AuthFailure":
563
- return "ReauthRequired";
564
- case "PermissionFailure":
565
- return "PolicyBlocked";
566
- case "PolicyFailure":
567
- return "PolicyBlocked";
568
- case "RegionRestriction":
569
- return "Disabled";
570
- case "ModelUnavailable":
571
- return null;
572
- case "TransientServerFailure":
573
- return null;
574
- case "TransportFailure":
575
- return null;
576
- case "InterruptedExecution":
577
- return null;
578
- }
579
- };
580
-
581
38
  // src/errors/corpus.ts
582
39
  var PROVIDER_PATTERNS = [
583
40
  // Anthropic
584
- { provider: "anthropic", http_status: 400, error_type_field: "error.type", error_type_value: "invalid_request_error", error_class: "PolicyFailure" },
585
- { provider: "anthropic", http_status: 401, error_type_field: "error.type", error_type_value: "authentication_error", error_class: "AuthFailure" },
586
- { provider: "anthropic", http_status: 402, error_type_field: "error.type", error_type_value: "billing_error", error_class: "QuotaExhausted" },
587
- { provider: "anthropic", http_status: 403, error_type_field: "error.type", error_type_value: "permission_error", error_class: "PermissionFailure" },
588
- { provider: "anthropic", http_status: 404, error_type_field: "error.type", error_type_value: "not_found_error", error_class: "ModelUnavailable" },
589
- { provider: "anthropic", http_status: 429, error_type_field: "error.type", error_type_value: "rate_limit_error", error_class: "RateLimited" },
590
- { provider: "anthropic", http_status: 500, error_type_field: "error.type", error_type_value: "api_error", error_class: "TransientServerFailure" },
591
- { provider: "anthropic", http_status: 504, error_type_field: "error.type", error_type_value: "timeout_error", error_class: "TransportFailure" },
592
- { provider: "anthropic", http_status: 529, error_type_field: "error.type", error_type_value: "overloaded_error", error_class: "RateLimited", notes: "Server capacity exhaustion; backoff on same provider, do NOT failover" },
41
+ {
42
+ provider: "anthropic",
43
+ http_status: 400,
44
+ error_type_field: "error.type",
45
+ error_type_value: "invalid_request_error",
46
+ error_class: "PolicyFailure"
47
+ },
48
+ {
49
+ provider: "anthropic",
50
+ http_status: 401,
51
+ error_type_field: "error.type",
52
+ error_type_value: "authentication_error",
53
+ error_class: "AuthFailure"
54
+ },
55
+ {
56
+ provider: "anthropic",
57
+ http_status: 402,
58
+ error_type_field: "error.type",
59
+ error_type_value: "billing_error",
60
+ error_class: "QuotaExhausted"
61
+ },
62
+ {
63
+ provider: "anthropic",
64
+ http_status: 403,
65
+ error_type_field: "error.type",
66
+ error_type_value: "permission_error",
67
+ error_class: "PermissionFailure"
68
+ },
69
+ {
70
+ provider: "anthropic",
71
+ http_status: 404,
72
+ error_type_field: "error.type",
73
+ error_type_value: "not_found_error",
74
+ error_class: "ModelUnavailable"
75
+ },
76
+ {
77
+ provider: "anthropic",
78
+ http_status: 429,
79
+ error_type_field: "error.type",
80
+ error_type_value: "rate_limit_error",
81
+ error_class: "RateLimited"
82
+ },
83
+ {
84
+ provider: "anthropic",
85
+ http_status: 500,
86
+ error_type_field: "error.type",
87
+ error_type_value: "api_error",
88
+ error_class: "TransientServerFailure"
89
+ },
90
+ {
91
+ provider: "anthropic",
92
+ http_status: 504,
93
+ error_type_field: "error.type",
94
+ error_type_value: "timeout_error",
95
+ error_class: "TransportFailure"
96
+ },
97
+ {
98
+ provider: "anthropic",
99
+ http_status: 529,
100
+ error_type_field: "error.type",
101
+ error_type_value: "overloaded_error",
102
+ error_class: "RateLimited",
103
+ notes: "Server capacity exhaustion; backoff on same provider, do NOT failover"
104
+ },
593
105
  // OpenAI
594
- { provider: "openai", http_status: 400, error_type_field: "error.code", error_type_value: "", error_class: "PolicyFailure" },
595
- { provider: "openai", http_status: 401, error_type_field: "error.code", error_type_value: "invalid_api_key", error_class: "AuthFailure" },
596
- { provider: "openai", http_status: 403, error_type_field: "error.code", error_type_value: "unsupported_country_region_territory", error_class: "RegionRestriction", notes: "GeoIP block \u2014 collected 2026-04-11 from real API" },
597
- { provider: "openai", http_status: 403, error_type_field: "error.code", error_type_value: "", error_class: "PermissionFailure" },
598
- { provider: "openai", http_status: 429, error_type_field: "error.code", error_type_value: "rate_limit_exceeded", error_class: "RateLimited" },
599
- { provider: "openai", http_status: 429, error_type_field: "error.code", error_type_value: "insufficient_quota", error_class: "QuotaExhausted", notes: "Billing exhausted -- NOT retryable" },
600
- { provider: "openai", http_status: 500, error_type_field: "error.code", error_type_value: "", error_class: "TransientServerFailure" },
601
- { provider: "openai", http_status: 503, error_type_field: "error.code", error_type_value: "", error_class: "TransientServerFailure" },
106
+ {
107
+ provider: "openai",
108
+ http_status: 400,
109
+ error_type_field: "error.code",
110
+ error_type_value: "",
111
+ error_class: "PolicyFailure"
112
+ },
113
+ {
114
+ provider: "openai",
115
+ http_status: 401,
116
+ error_type_field: "error.code",
117
+ error_type_value: "invalid_api_key",
118
+ error_class: "AuthFailure"
119
+ },
120
+ {
121
+ provider: "openai",
122
+ http_status: 403,
123
+ error_type_field: "error.code",
124
+ error_type_value: "unsupported_country_region_territory",
125
+ error_class: "RegionRestriction",
126
+ notes: "GeoIP block \u2014 collected 2026-04-11 from real API"
127
+ },
128
+ {
129
+ provider: "openai",
130
+ http_status: 403,
131
+ error_type_field: "error.code",
132
+ error_type_value: "",
133
+ error_class: "PermissionFailure"
134
+ },
135
+ {
136
+ provider: "openai",
137
+ http_status: 429,
138
+ error_type_field: "error.code",
139
+ error_type_value: "rate_limit_exceeded",
140
+ error_class: "RateLimited"
141
+ },
142
+ {
143
+ provider: "openai",
144
+ http_status: 429,
145
+ error_type_field: "error.code",
146
+ error_type_value: "insufficient_quota",
147
+ error_class: "QuotaExhausted",
148
+ notes: "Billing exhausted -- NOT retryable"
149
+ },
150
+ {
151
+ provider: "openai",
152
+ http_status: 500,
153
+ error_type_field: "error.code",
154
+ error_type_value: "",
155
+ error_class: "TransientServerFailure"
156
+ },
157
+ {
158
+ provider: "openai",
159
+ http_status: 503,
160
+ error_type_field: "error.code",
161
+ error_type_value: "",
162
+ error_class: "TransientServerFailure"
163
+ },
602
164
  // Google Vertex AI / Gemini
603
- { provider: "google", http_status: 400, error_type_field: "error.status", error_type_value: "INVALID_ARGUMENT", error_class: "PolicyFailure" },
604
- { provider: "google", http_status: 403, error_type_field: "error.status", error_type_value: "PERMISSION_DENIED", error_class: "PermissionFailure" },
605
- { provider: "google", http_status: 429, error_type_field: "error.status", error_type_value: "RESOURCE_EXHAUSTED", error_class: "RateLimited" },
606
- { provider: "google", http_status: 500, error_type_field: "error.status", error_type_value: "INTERNAL", error_class: "TransientServerFailure" },
607
- { provider: "google", http_status: 503, error_type_field: "error.status", error_type_value: "UNAVAILABLE", error_class: "TransientServerFailure" },
165
+ {
166
+ provider: "google",
167
+ http_status: 400,
168
+ error_type_field: "error.status",
169
+ error_type_value: "INVALID_ARGUMENT",
170
+ error_class: "PolicyFailure"
171
+ },
172
+ {
173
+ provider: "google",
174
+ http_status: 403,
175
+ error_type_field: "error.status",
176
+ error_type_value: "PERMISSION_DENIED",
177
+ error_class: "PermissionFailure"
178
+ },
179
+ {
180
+ provider: "google",
181
+ http_status: 429,
182
+ error_type_field: "error.status",
183
+ error_type_value: "RESOURCE_EXHAUSTED",
184
+ error_class: "RateLimited"
185
+ },
186
+ {
187
+ provider: "google",
188
+ http_status: 500,
189
+ error_type_field: "error.status",
190
+ error_type_value: "INTERNAL",
191
+ error_class: "TransientServerFailure"
192
+ },
193
+ {
194
+ provider: "google",
195
+ http_status: 503,
196
+ error_type_field: "error.status",
197
+ error_type_value: "UNAVAILABLE",
198
+ error_class: "TransientServerFailure"
199
+ },
608
200
  // AWS Bedrock
609
- { provider: "bedrock", http_status: 429, error_type_field: "__type", error_type_value: "ThrottlingException", error_class: "RateLimited" },
610
- { provider: "bedrock", http_status: 408, error_type_field: "__type", error_type_value: "ModelTimeoutException", error_class: "TransportFailure" },
611
- { provider: "bedrock", http_status: 424, error_type_field: "__type", error_type_value: "ModelNotReadyException", error_class: "ModelUnavailable" },
612
- { provider: "bedrock", http_status: 403, error_type_field: "__type", error_type_value: "AccessDeniedException", error_class: "PermissionFailure" },
613
- { provider: "bedrock", http_status: 400, error_type_field: "__type", error_type_value: "ValidationException", error_class: "PolicyFailure" },
614
- { provider: "bedrock", http_status: 500, error_type_field: "__type", error_type_value: "InternalServerException", error_class: "TransientServerFailure" }
201
+ {
202
+ provider: "bedrock",
203
+ http_status: 429,
204
+ error_type_field: "__type",
205
+ error_type_value: "ThrottlingException",
206
+ error_class: "RateLimited"
207
+ },
208
+ {
209
+ provider: "bedrock",
210
+ http_status: 408,
211
+ error_type_field: "__type",
212
+ error_type_value: "ModelTimeoutException",
213
+ error_class: "TransportFailure"
214
+ },
215
+ {
216
+ provider: "bedrock",
217
+ http_status: 424,
218
+ error_type_field: "__type",
219
+ error_type_value: "ModelNotReadyException",
220
+ error_class: "ModelUnavailable"
221
+ },
222
+ {
223
+ provider: "bedrock",
224
+ http_status: 403,
225
+ error_type_field: "__type",
226
+ error_type_value: "AccessDeniedException",
227
+ error_class: "PermissionFailure"
228
+ },
229
+ {
230
+ provider: "bedrock",
231
+ http_status: 400,
232
+ error_type_field: "__type",
233
+ error_type_value: "ValidationException",
234
+ error_class: "PolicyFailure"
235
+ },
236
+ {
237
+ provider: "bedrock",
238
+ http_status: 500,
239
+ error_type_field: "__type",
240
+ error_type_value: "InternalServerException",
241
+ error_class: "TransientServerFailure"
242
+ }
615
243
  ];
616
244
  var HEURISTIC_PATTERNS = [
617
245
  // Transport errors (high confidence -- these are unambiguous)
618
- { pattern: /ECONNREFUSED|ECONNRESET|ETIMEDOUT/, error_class: "TransportFailure", confidence: "high" },
246
+ {
247
+ pattern: /ECONNREFUSED|ECONNRESET|ETIMEDOUT/,
248
+ error_class: "TransportFailure",
249
+ confidence: "high"
250
+ },
619
251
  // Quota patterns (before rate limit to avoid misclassification -- Pitfall 2/3)
620
- { pattern: /quota\s*(?:exceeded|exhausted)/i, error_class: "QuotaExhausted", confidence: "medium" },
621
- { pattern: /insufficient\s*quota/i, error_class: "QuotaExhausted", confidence: "medium", provider: "openai" },
252
+ {
253
+ pattern: /quota\s*(?:exceeded|exhausted)/i,
254
+ error_class: "QuotaExhausted",
255
+ confidence: "medium"
256
+ },
257
+ {
258
+ pattern: /insufficient\s*quota/i,
259
+ error_class: "QuotaExhausted",
260
+ confidence: "medium",
261
+ provider: "openai"
262
+ },
622
263
  { pattern: /billing/i, error_class: "QuotaExhausted", confidence: "low", provider: "openai" },
623
264
  // Rate limit patterns
624
265
  { pattern: /rate\s*limit/i, error_class: "RateLimited", confidence: "medium" },
625
266
  { pattern: /too many requests/i, error_class: "RateLimited", confidence: "medium" },
626
267
  { pattern: /retry\s*after/i, error_class: "RateLimited", confidence: "medium" },
627
- { pattern: /overloaded/i, error_class: "RateLimited", confidence: "medium", provider: "anthropic" },
628
- { pattern: /resource\s*exhausted/i, error_class: "RateLimited", confidence: "medium", provider: "google" },
268
+ {
269
+ pattern: /overloaded/i,
270
+ error_class: "RateLimited",
271
+ confidence: "medium",
272
+ provider: "anthropic"
273
+ },
274
+ {
275
+ pattern: /resource\s*exhausted/i,
276
+ error_class: "RateLimited",
277
+ confidence: "medium",
278
+ provider: "google"
279
+ },
629
280
  // Auth patterns
630
281
  { pattern: /authentication/i, error_class: "AuthFailure", confidence: "medium" },
631
282
  { pattern: /invalid\s*api\s*key/i, error_class: "AuthFailure", confidence: "medium" },
@@ -634,14 +285,31 @@ var HEURISTIC_PATTERNS = [
634
285
  { pattern: /permission\s*denied/i, error_class: "PermissionFailure", confidence: "medium" },
635
286
  { pattern: /forbidden/i, error_class: "PermissionFailure", confidence: "low" },
636
287
  // Model availability patterns
637
- { pattern: /model\s*not\s*(?:available|ready|found)/i, error_class: "ModelUnavailable", confidence: "medium" },
288
+ {
289
+ pattern: /model\s*not\s*(?:available|ready|found)/i,
290
+ error_class: "ModelUnavailable",
291
+ confidence: "medium"
292
+ },
638
293
  { pattern: /not\s*found/i, error_class: "ModelUnavailable", confidence: "low" },
639
294
  // Transport patterns (lower confidence than ECONNREFUSED)
640
295
  { pattern: /timeout/i, error_class: "TransportFailure", confidence: "low" },
641
296
  // Region restriction (requires both keywords)
642
- { pattern: /unsupported.country.*region.*territory/i, error_class: "RegionRestriction", confidence: "high", notes: "OpenAI GeoIP block \u2014 collected 2026-04-11" },
643
- { pattern: /country.*region.*not supported/i, error_class: "RegionRestriction", confidence: "high" },
644
- { pattern: /region.*restrict|restrict.*region/i, error_class: "RegionRestriction", confidence: "low" }
297
+ {
298
+ pattern: /unsupported.country.*region.*territory/i,
299
+ error_class: "RegionRestriction",
300
+ confidence: "high",
301
+ notes: "OpenAI GeoIP block \u2014 collected 2026-04-11"
302
+ },
303
+ {
304
+ pattern: /country.*region.*not supported/i,
305
+ error_class: "RegionRestriction",
306
+ confidence: "high"
307
+ },
308
+ {
309
+ pattern: /region.*restrict|restrict.*region/i,
310
+ error_class: "RegionRestriction",
311
+ confidence: "low"
312
+ }
645
313
  ];
646
314
  var TEMPORAL_QUOTA_PATTERN = /too many tokens per (?:day|month|hour|week)/i;
647
315
 
@@ -860,777 +528,6 @@ var heuristicSignalFromEvent = (eventType, message, providerId) => ({
860
528
  provider_id: providerId
861
529
  });
862
530
 
863
- // src/retry/backoff.ts
864
- var DEFAULT_BACKOFF_PARAMS = {
865
- base_ms: 1e3,
866
- multiplier: 2,
867
- max_ms: 3e4,
868
- jitter: "full"
869
- };
870
- var computeBackoffMs = (attempt, params, retryAfterMs) => {
871
- const rawDelay = Math.min(
872
- params.base_ms * Math.pow(params.multiplier, attempt),
873
- params.max_ms
874
- );
875
- const jitteredDelay = params.jitter === "full" ? Math.random() * rawDelay : params.jitter === "equal" ? rawDelay / 2 + Math.random() * (rawDelay / 2) : rawDelay;
876
- return Math.max(jitteredDelay, retryAfterMs ?? 0);
877
- };
878
-
879
- // src/retry/retry-policy.ts
880
- var createRetryPolicy = (budget, backoffParams = DEFAULT_BACKOFF_PARAMS) => ({
881
- budget,
882
- backoffParams,
883
- decide: (classification, attemptsSoFar) => {
884
- const errorClass = classification.error_class;
885
- if (!isRetryable(errorClass)) {
886
- return {
887
- action: "abort",
888
- reason: `Non-retryable: ${errorClass.class}`,
889
- target_state: getTargetStateTransition(errorClass)
890
- };
891
- }
892
- if (attemptsSoFar >= budget) {
893
- return {
894
- action: "exhausted",
895
- attempts_used: attemptsSoFar
896
- };
897
- }
898
- const retryAfterMs = errorClass.class === "RateLimited" ? errorClass.retry_after_ms : void 0;
899
- const delayMs = computeBackoffMs(attemptsSoFar, backoffParams, retryAfterMs);
900
- return {
901
- action: "retry",
902
- delay_ms: delayMs,
903
- attempt: attemptsSoFar + 1
904
- };
905
- }
906
- });
907
-
908
- // src/routing/types.ts
909
- var EXCLUSION_REASONS = [
910
- "disabled",
911
- "policy_blocked",
912
- "reauth_required",
913
- "cooldown_active",
914
- "circuit_open",
915
- "capability_mismatch",
916
- "draining"
917
- ];
918
- var ADMISSION_RESULTS = [
919
- "admitted",
920
- "queued",
921
- "degraded",
922
- "rejected"
923
- ];
924
-
925
- // src/routing/events.ts
926
- var import_eventemitter3 = require("eventemitter3");
927
- var createRoutingEventBus = () => new import_eventemitter3.EventEmitter();
928
-
929
- // src/routing/dual-breaker.ts
930
- var import_cockatiel = require("cockatiel");
931
- var DualBreaker = class {
932
- consecutive;
933
- count;
934
- constructor(consecutiveThreshold, countOpts) {
935
- this.consecutive = new import_cockatiel.ConsecutiveBreaker(consecutiveThreshold);
936
- this.count = new import_cockatiel.CountBreaker(countOpts);
937
- }
938
- /** Serializable state for both internal breakers. */
939
- get state() {
940
- return {
941
- consecutive: this.consecutive.state,
942
- count: this.count.state
943
- };
944
- }
945
- /** Restore state for both internal breakers. */
946
- set state(value) {
947
- const v = value;
948
- this.consecutive.state = v.consecutive;
949
- this.count.state = v.count;
950
- }
951
- /**
952
- * Record success on both breakers.
953
- * ConsecutiveBreaker.success() takes no args (resets counter).
954
- * CountBreaker.success() takes CircuitState.
955
- */
956
- success(state) {
957
- this.consecutive.success();
958
- this.count.success(state);
959
- }
960
- /**
961
- * Record failure on both breakers.
962
- * Returns true if EITHER breaker trips (OR logic).
963
- * ConsecutiveBreaker.failure() takes no args.
964
- * CountBreaker.failure() takes CircuitState.
965
- */
966
- failure(state) {
967
- const consecutiveTripped = this.consecutive.failure();
968
- const countTripped = this.count.failure(state);
969
- return consecutiveTripped || countTripped;
970
- }
971
- };
972
-
973
- // src/routing/circuit-breaker.ts
974
- var import_cockatiel2 = require("cockatiel");
975
- var import_eventemitter32 = require("eventemitter3");
976
- var COCKATIEL_TO_TARGET = /* @__PURE__ */ new Map([
977
- [import_cockatiel2.CircuitState.Closed, "Active"],
978
- [import_cockatiel2.CircuitState.Open, "CircuitOpen"],
979
- [import_cockatiel2.CircuitState.HalfOpen, "CircuitHalfOpen"],
980
- [import_cockatiel2.CircuitState.Isolated, "Disabled"]
981
- ]);
982
- var toTargetState = (state) => COCKATIEL_TO_TARGET.get(state) ?? "Disabled";
983
- var createCircuitBreaker = (targetId, config, eventBus) => {
984
- const createPolicy = () => {
985
- const breaker = new DualBreaker(config.failure_threshold, {
986
- threshold: config.failure_rate_threshold,
987
- size: config.sliding_window_size
988
- });
989
- const policy = (0, import_cockatiel2.circuitBreaker)(import_cockatiel2.handleAll, {
990
- halfOpenAfter: config.half_open_after_ms,
991
- breaker
992
- });
993
- return { policy, breaker };
994
- };
995
- let current = createPolicy();
996
- let previousState = import_cockatiel2.CircuitState.Closed;
997
- let openedAtMs = 0;
998
- const subscribeEvents = (policy) => {
999
- policy.onStateChange((newState) => {
1000
- if (newState === import_cockatiel2.CircuitState.Open) {
1001
- openedAtMs = Date.now();
1002
- }
1003
- const from = toTargetState(previousState);
1004
- const to = toTargetState(newState);
1005
- previousState = newState;
1006
- if (from !== to && eventBus) {
1007
- eventBus.emit("circuit_state_change", {
1008
- target_id: targetId,
1009
- from,
1010
- to,
1011
- reason: `state_transition`
1012
- });
1013
- }
1014
- });
1015
- };
1016
- subscribeEvents(current.policy);
1017
- const effectiveState = () => {
1018
- const raw = current.policy.state;
1019
- if (raw === import_cockatiel2.CircuitState.Open && openedAtMs > 0 && Date.now() - openedAtMs >= config.half_open_after_ms) {
1020
- return import_cockatiel2.CircuitState.HalfOpen;
1021
- }
1022
- return raw;
1023
- };
1024
- return {
1025
- state() {
1026
- return toTargetState(effectiveState());
1027
- },
1028
- async recordSuccess() {
1029
- try {
1030
- await current.policy.execute(async () => void 0);
1031
- } catch (err) {
1032
- if (err instanceof import_cockatiel2.BrokenCircuitError || err instanceof import_cockatiel2.IsolatedCircuitError) {
1033
- return;
1034
- }
1035
- throw err;
1036
- }
1037
- },
1038
- async recordFailure() {
1039
- try {
1040
- await current.policy.execute(async () => {
1041
- throw new Error("recorded-failure");
1042
- });
1043
- } catch {
1044
- }
1045
- },
1046
- allowRequest() {
1047
- return effectiveState() !== import_cockatiel2.CircuitState.Open;
1048
- },
1049
- reset() {
1050
- current = createPolicy();
1051
- previousState = import_cockatiel2.CircuitState.Closed;
1052
- openedAtMs = 0;
1053
- subscribeEvents(current.policy);
1054
- }
1055
- };
1056
- };
1057
-
1058
- // src/routing/concurrency-tracker.ts
1059
- var createConcurrencyTracker = (defaultLimit) => {
1060
- const state = /* @__PURE__ */ new Map();
1061
- const getOrCreate = (target_id) => {
1062
- const existing = state.get(target_id);
1063
- if (existing) return existing;
1064
- const entry = { active: 0, limit: defaultLimit };
1065
- state.set(target_id, entry);
1066
- return entry;
1067
- };
1068
- return {
1069
- acquire(target_id) {
1070
- const entry = getOrCreate(target_id);
1071
- if (entry.active >= entry.limit) return false;
1072
- entry.active += 1;
1073
- return true;
1074
- },
1075
- release(target_id) {
1076
- const entry = state.get(target_id);
1077
- if (!entry || entry.active <= 0) return;
1078
- entry.active -= 1;
1079
- },
1080
- headroom(target_id) {
1081
- const entry = getOrCreate(target_id);
1082
- return Math.max(0, entry.limit - entry.active);
1083
- },
1084
- active(target_id) {
1085
- const entry = state.get(target_id);
1086
- return entry ? entry.active : 0;
1087
- },
1088
- setLimit(target_id, limit) {
1089
- const entry = getOrCreate(target_id);
1090
- entry.limit = limit;
1091
- }
1092
- };
1093
- };
1094
-
1095
- // src/routing/policy-engine.ts
1096
- var normalizeLatency = (latencyEmaMs, maxExpectedMs) => Math.min(latencyEmaMs / maxExpectedMs, 1);
1097
- var computeScore = (target, weights, maxLatencyMs) => weights.health * target.health_score - weights.latency * normalizeLatency(target.latency_ema_ms, maxLatencyMs) - weights.failure * target.failure_score + weights.priority * target.operator_priority;
1098
- var getExclusionReason = (target, circuitBreakers, requiredCapabilities, excludeTargets, nowMs) => {
1099
- if (!target.enabled) {
1100
- return "disabled";
1101
- }
1102
- if (target.state === "PolicyBlocked") {
1103
- return "policy_blocked";
1104
- }
1105
- if (target.state === "ReauthRequired") {
1106
- return "reauth_required";
1107
- }
1108
- if (target.state === "Draining") {
1109
- return "draining";
1110
- }
1111
- if (target.state === "Disabled") {
1112
- return "disabled";
1113
- }
1114
- const breaker = circuitBreakers.get(target.target_id);
1115
- if (breaker !== void 0 && !breaker.allowRequest()) {
1116
- return "circuit_open";
1117
- }
1118
- if (target.cooldown_until !== null && target.cooldown_until > nowMs) {
1119
- return "cooldown_active";
1120
- }
1121
- if (requiredCapabilities.length > 0 && !requiredCapabilities.every((cap) => target.capabilities.includes(cap))) {
1122
- return "capability_mismatch";
1123
- }
1124
- return null;
1125
- };
1126
- var selectTarget = (snapshot, circuitBreakers, requiredCapabilities, weights, maxLatencyMs, nowMs, request_id, excludeTargets, logger) => {
1127
- const excluded = [];
1128
- const eligible = [];
1129
- for (const target of snapshot.targets) {
1130
- if (excludeTargets !== void 0 && excludeTargets.has(target.target_id)) {
1131
- continue;
1132
- }
1133
- const reason = getExclusionReason(
1134
- target,
1135
- circuitBreakers,
1136
- requiredCapabilities,
1137
- excludeTargets ?? /* @__PURE__ */ new Set(),
1138
- nowMs
1139
- );
1140
- if (reason !== null) {
1141
- excluded.push({ target_id: target.target_id, reason });
1142
- } else {
1143
- eligible.push(target);
1144
- }
1145
- }
1146
- const scored = eligible.map((target) => ({
1147
- target,
1148
- score: computeScore(target, weights, maxLatencyMs)
1149
- }));
1150
- scored.sort((a, b) => {
1151
- const scoreDiff = b.score - a.score;
1152
- if (scoreDiff !== 0) {
1153
- return scoreDiff;
1154
- }
1155
- return a.target.target_id.localeCompare(b.target.target_id);
1156
- });
1157
- const candidates = scored.map((s) => ({
1158
- target_id: s.target.target_id,
1159
- score: s.score,
1160
- health_score: s.target.health_score,
1161
- latency_ema_ms: s.target.latency_ema_ms
1162
- }));
1163
- const firstCandidate = candidates.length > 0 ? candidates[0] : void 0;
1164
- const selected = firstCandidate !== void 0 ? {
1165
- target_id: firstCandidate.target_id,
1166
- score: firstCandidate.score,
1167
- rank: 0
1168
- } : null;
1169
- const record = {
1170
- request_id,
1171
- timestamp_ms: nowMs,
1172
- candidates,
1173
- excluded,
1174
- selected
1175
- };
1176
- if (logger !== void 0) {
1177
- const excludedSummary = {};
1178
- for (const e of excluded) {
1179
- excludedSummary[e.reason] = (excludedSummary[e.reason] ?? 0) + 1;
1180
- }
1181
- const logFields = {
1182
- event: "target_selected",
1183
- component: "policy-engine",
1184
- request_id,
1185
- selected: selected?.target_id ?? null,
1186
- score: selected?.score ?? null,
1187
- candidates_count: candidates.length,
1188
- excluded_count: excluded.length,
1189
- excluded_summary: excludedSummary
1190
- };
1191
- if (selected !== null) {
1192
- logger.info(logFields, "Target selected");
1193
- } else {
1194
- logger.warn(logFields, "No eligible target");
1195
- }
1196
- }
1197
- return record;
1198
- };
1199
-
1200
- // src/routing/cooldown.ts
1201
- var DEFAULT_COOLDOWN_BASE_MS = 5e3;
1202
- var computeCooldownMs = (errorClass, consecutiveFailures, backoffParams, maxMs) => {
1203
- const noJitterParams = {
1204
- ...backoffParams,
1205
- jitter: "none"
1206
- };
1207
- let base;
1208
- if (errorClass.class === "RateLimited" && "retry_after_ms" in errorClass && errorClass.retry_after_ms !== void 0) {
1209
- base = Math.min(errorClass.retry_after_ms, maxMs);
1210
- } else if (errorClass.class === "RateLimited") {
1211
- base = computeBackoffMs(consecutiveFailures, noJitterParams);
1212
- } else if (errorClass.class === "TransientServerFailure") {
1213
- base = computeBackoffMs(consecutiveFailures, noJitterParams) / 2;
1214
- } else {
1215
- base = DEFAULT_COOLDOWN_BASE_MS;
1216
- }
1217
- base = Math.min(base, maxMs);
1218
- const jitterFraction = 0.1 + Math.random() * 0.15;
1219
- const jitter = jitterFraction * base;
1220
- return Math.min(base + jitter, maxMs);
1221
- };
1222
- var createCooldownManager = (registry, eventBus) => ({
1223
- setCooldown(target_id, durationMs, errorClass) {
1224
- const untilMs = Date.now() + durationMs;
1225
- registry.setCooldown(target_id, untilMs);
1226
- registry.updateState(target_id, "CoolingDown");
1227
- eventBus?.emit("cooldown_set", {
1228
- target_id,
1229
- until_ms: untilMs,
1230
- error_class: errorClass
1231
- });
1232
- },
1233
- checkExpired(nowMs) {
1234
- const expired = [];
1235
- for (const target of registry.getAllTargets()) {
1236
- if (target.state === "CoolingDown" && target.cooldown_until !== null && target.cooldown_until <= nowMs) {
1237
- registry.setCooldown(target.target_id, 0);
1238
- registry.updateState(target.target_id, "Active");
1239
- eventBus?.emit("cooldown_expired", { target_id: target.target_id });
1240
- expired.push(target.target_id);
1241
- }
1242
- }
1243
- return expired;
1244
- },
1245
- isInCooldown(target_id, nowMs) {
1246
- const target = registry.getTarget(target_id);
1247
- if (!target) {
1248
- return false;
1249
- }
1250
- return target.cooldown_until !== null && target.cooldown_until > nowMs;
1251
- }
1252
- });
1253
-
1254
- // src/routing/admission.ts
1255
- var import_p_queue = __toESM(require("p-queue"), 1);
1256
- var checkHardRejects = (ctx) => {
1257
- if (!ctx.hasEligibleTargets) {
1258
- return { result: "rejected", reason: "no eligible targets available" };
1259
- }
1260
- if (ctx.operatorPaused) {
1261
- return { result: "rejected", reason: "operator pause active" };
1262
- }
1263
- if (ctx.retryBudgetRemaining <= 0) {
1264
- return { result: "rejected", reason: "retry budget exhausted" };
1265
- }
1266
- if (ctx.failoverBudgetRemaining <= 0) {
1267
- return { result: "rejected", reason: "failover budget exhausted" };
1268
- }
1269
- return null;
1270
- };
1271
- var createAdmissionController = (config, eventBus) => {
1272
- const queue = new import_p_queue.default({
1273
- concurrency: config.concurrencyLimit
1274
- });
1275
- const emitDecision = (request_id, decision) => {
1276
- eventBus?.emit("admission_decision", {
1277
- request_id,
1278
- result: decision.result,
1279
- reason: decision.reason
1280
- });
1281
- };
1282
- const controller = {
1283
- async admit(request_id, ctx) {
1284
- const hardReject = checkHardRejects(ctx);
1285
- if (hardReject) {
1286
- emitDecision(request_id, hardReject);
1287
- return hardReject;
1288
- }
1289
- if (queue.size > config.backpressureThreshold) {
1290
- const decision2 = {
1291
- result: "degraded",
1292
- reason: `backpressure: ${queue.size} queued > ${config.backpressureThreshold} threshold`
1293
- };
1294
- emitDecision(request_id, decision2);
1295
- return decision2;
1296
- }
1297
- const total = queue.size + queue.pending;
1298
- if (total >= config.queueLimit + config.concurrencyLimit) {
1299
- const decision2 = {
1300
- result: "rejected",
1301
- reason: `queue full: ${total} >= ${config.queueLimit + config.concurrencyLimit}`
1302
- };
1303
- emitDecision(request_id, decision2);
1304
- return decision2;
1305
- }
1306
- const decision = {
1307
- result: "admitted",
1308
- reason: "capacity available"
1309
- };
1310
- emitDecision(request_id, decision);
1311
- return decision;
1312
- },
1313
- async execute(fn, request_id, ctx) {
1314
- const decision = await controller.admit(request_id, ctx);
1315
- if (decision.result === "rejected") {
1316
- return { decision };
1317
- }
1318
- const result = await queue.add(fn);
1319
- return { decision, result };
1320
- },
1321
- get pending() {
1322
- return queue.pending;
1323
- },
1324
- get size() {
1325
- return queue.size;
1326
- }
1327
- };
1328
- return controller;
1329
- };
1330
-
1331
- // src/routing/log-subscriber.ts
1332
- var createLogSubscriber = (eventBus, logger) => {
1333
- const log = logger.child({ component: "routing" });
1334
- const onCircuitStateChange = (payload) => {
1335
- log.info(
1336
- {
1337
- event: "circuit_state_change",
1338
- target_id: payload.target_id,
1339
- from: payload.from,
1340
- to: payload.to,
1341
- reason: payload.reason
1342
- },
1343
- "Circuit breaker transition"
1344
- );
1345
- };
1346
- const onCooldownSet = (payload) => {
1347
- log.info(
1348
- {
1349
- event: "cooldown_set",
1350
- target_id: payload.target_id,
1351
- duration_ms: payload.until_ms - Date.now(),
1352
- error_class: payload.error_class
1353
- },
1354
- "Cooldown set"
1355
- );
1356
- };
1357
- const onCooldownExpired = (payload) => {
1358
- log.info(
1359
- {
1360
- event: "cooldown_expired",
1361
- target_id: payload.target_id
1362
- },
1363
- "Cooldown expired"
1364
- );
1365
- };
1366
- const onAdmissionDecision = (payload) => {
1367
- const fields = {
1368
- event: "admission_decision",
1369
- request_id: payload.request_id,
1370
- result: payload.result,
1371
- reason: payload.reason
1372
- };
1373
- if (payload.result === "admitted" || payload.result === "queued") {
1374
- log.info(fields, "Admission decision");
1375
- } else {
1376
- log.warn(fields, "Admission decision");
1377
- }
1378
- };
1379
- const onHealthUpdated = (payload) => {
1380
- log.debug(
1381
- {
1382
- event: "health_updated",
1383
- target_id: payload.target_id,
1384
- old_score: payload.old_score,
1385
- new_score: payload.new_score
1386
- },
1387
- "Health score updated"
1388
- );
1389
- };
1390
- const onTargetExcluded = (payload) => {
1391
- log.debug(
1392
- {
1393
- event: "target_excluded",
1394
- target_id: payload.target_id,
1395
- reason: payload.reason
1396
- },
1397
- "Target excluded"
1398
- );
1399
- };
1400
- eventBus.on("circuit_state_change", onCircuitStateChange);
1401
- eventBus.on("cooldown_set", onCooldownSet);
1402
- eventBus.on("cooldown_expired", onCooldownExpired);
1403
- eventBus.on("admission_decision", onAdmissionDecision);
1404
- eventBus.on("health_updated", onHealthUpdated);
1405
- eventBus.on("target_excluded", onTargetExcluded);
1406
- return () => {
1407
- eventBus.off("circuit_state_change", onCircuitStateChange);
1408
- eventBus.off("cooldown_set", onCooldownSet);
1409
- eventBus.off("cooldown_expired", onCooldownExpired);
1410
- eventBus.off("admission_decision", onAdmissionDecision);
1411
- eventBus.off("health_updated", onHealthUpdated);
1412
- eventBus.off("target_excluded", onTargetExcluded);
1413
- };
1414
- };
1415
-
1416
- // src/routing/failover.ts
1417
- var createFailoverOrchestrator = (deps) => ({
1418
- async execute(request_id, attemptFn, requiredCapabilities) {
1419
- const log = deps.logger?.child({ component: "failover", request_id });
1420
- const attempts = [];
1421
- const failedTargets = /* @__PURE__ */ new Set();
1422
- let totalRetries = 0;
1423
- let totalFailovers = 0;
1424
- let lastErrorClass;
1425
- log?.info(
1426
- { event: "failover_start", retry_budget: deps.retryBudget, failover_budget: deps.failoverBudget },
1427
- "Failover loop started"
1428
- );
1429
- for (let failoverNo = 0; failoverNo <= deps.failoverBudget; failoverNo++) {
1430
- deps.cooldownManager.checkExpired(Date.now());
1431
- const snapshot = deps.registry.getSnapshot();
1432
- const selection = selectTarget(
1433
- snapshot,
1434
- deps.circuitBreakers,
1435
- requiredCapabilities,
1436
- deps.weights,
1437
- deps.maxLatencyMs,
1438
- Date.now(),
1439
- request_id,
1440
- failedTargets
1441
- );
1442
- if (!selection.selected) {
1443
- log?.warn(
1444
- { event: "no_eligible_target", failover_no: failoverNo },
1445
- "No eligible target available"
1446
- );
1447
- return {
1448
- outcome: "failure",
1449
- reason: "no eligible target available",
1450
- attempts,
1451
- total_retries: totalRetries,
1452
- total_failovers: totalFailovers,
1453
- last_error_class: lastErrorClass
1454
- };
1455
- }
1456
- const targetId = selection.selected.target_id;
1457
- const retryPolicy = createRetryPolicy(
1458
- deps.retryBudget,
1459
- deps.backoffParams
1460
- );
1461
- for (let retry = 0; retry <= deps.retryBudget; retry++) {
1462
- const attemptNo = retry + 1;
1463
- const acquired = deps.concurrency.acquire(targetId);
1464
- if (!acquired) {
1465
- log?.warn(
1466
- { event: "concurrency_full", target_id: targetId, failover_no: failoverNo },
1467
- "No concurrency headroom \u2014 failing over"
1468
- );
1469
- failedTargets.add(targetId);
1470
- break;
1471
- }
1472
- let attemptResult;
1473
- try {
1474
- attemptResult = await attemptFn(targetId);
1475
- } finally {
1476
- deps.concurrency.release(targetId);
1477
- }
1478
- if (attemptResult.success) {
1479
- const cb2 = deps.circuitBreakers.get(targetId);
1480
- if (cb2) {
1481
- await cb2.recordSuccess();
1482
- }
1483
- deps.registry.recordObservation(targetId, 1);
1484
- deps.registry.updateLatency(targetId, attemptResult.latency_ms);
1485
- log?.info(
1486
- { event: "attempt_success", target_id: targetId, attempt_no: attemptNo, failover_no: failoverNo, latency_ms: attemptResult.latency_ms },
1487
- "Attempt succeeded"
1488
- );
1489
- attempts.push({
1490
- target_id: targetId,
1491
- attempt_no: attemptNo,
1492
- failover_no: failoverNo,
1493
- outcome: "success",
1494
- latency_ms: attemptResult.latency_ms,
1495
- selection
1496
- });
1497
- return {
1498
- outcome: "success",
1499
- target_id: targetId,
1500
- attempts,
1501
- total_retries: totalRetries,
1502
- total_failovers: totalFailovers,
1503
- value: attemptResult.value
1504
- };
1505
- }
1506
- const errorClass = attemptResult.error_class;
1507
- lastErrorClass = errorClass.class;
1508
- const cb = deps.circuitBreakers.get(targetId);
1509
- if (cb) {
1510
- await cb.recordFailure();
1511
- }
1512
- deps.registry.recordObservation(targetId, 0);
1513
- const stateTransition = getTargetStateTransition(errorClass);
1514
- if (stateTransition) {
1515
- deps.registry.updateState(targetId, stateTransition);
1516
- }
1517
- if (errorClass.class === "RateLimited") {
1518
- const cooldownMs = computeCooldownMs(
1519
- errorClass,
1520
- retry,
1521
- deps.backoffParams,
1522
- deps.backoffParams.max_ms
1523
- );
1524
- deps.cooldownManager.setCooldown(
1525
- targetId,
1526
- cooldownMs,
1527
- errorClass.class
1528
- );
1529
- }
1530
- if (errorClass.class === "ModelUnavailable") {
1531
- log?.info(
1532
- { event: "early_failover", target_id: targetId, error_class: errorClass.class, failover_no: failoverNo },
1533
- "Early failover \u2014 ModelUnavailable"
1534
- );
1535
- attempts.push({
1536
- target_id: targetId,
1537
- attempt_no: attemptNo,
1538
- failover_no: failoverNo,
1539
- outcome: "failover",
1540
- error_class: errorClass.class,
1541
- latency_ms: attemptResult.latency_ms,
1542
- selection
1543
- });
1544
- failedTargets.add(targetId);
1545
- totalFailovers++;
1546
- break;
1547
- }
1548
- if (!isRetryable(errorClass)) {
1549
- log?.warn(
1550
- { event: "abort", target_id: targetId, error_class: errorClass.class },
1551
- "Non-retryable error \u2014 aborting"
1552
- );
1553
- attempts.push({
1554
- target_id: targetId,
1555
- attempt_no: attemptNo,
1556
- failover_no: failoverNo,
1557
- outcome: "abort",
1558
- error_class: errorClass.class,
1559
- latency_ms: attemptResult.latency_ms,
1560
- selection
1561
- });
1562
- return {
1563
- outcome: "failure",
1564
- reason: `non-retryable: ${errorClass.class}`,
1565
- attempts,
1566
- total_retries: totalRetries,
1567
- total_failovers: totalFailovers,
1568
- last_error_class: errorClass.class
1569
- };
1570
- }
1571
- const decision = retryPolicy.decide(
1572
- {
1573
- error_class: errorClass,
1574
- detection_mode: "direct",
1575
- confidence: "high",
1576
- raw_signal: { detection_mode: "direct" }
1577
- },
1578
- retry
1579
- );
1580
- if (decision.action === "exhausted") {
1581
- log?.info(
1582
- { event: "retry_budget_exhausted", target_id: targetId, failover_no: failoverNo },
1583
- "Retry budget exhausted \u2014 failing over"
1584
- );
1585
- attempts.push({
1586
- target_id: targetId,
1587
- attempt_no: attemptNo,
1588
- failover_no: failoverNo,
1589
- outcome: "failover",
1590
- error_class: errorClass.class,
1591
- latency_ms: attemptResult.latency_ms,
1592
- selection
1593
- });
1594
- failedTargets.add(targetId);
1595
- totalFailovers++;
1596
- break;
1597
- }
1598
- attempts.push({
1599
- target_id: targetId,
1600
- attempt_no: attemptNo,
1601
- failover_no: failoverNo,
1602
- outcome: "retry",
1603
- error_class: errorClass.class,
1604
- latency_ms: attemptResult.latency_ms,
1605
- selection
1606
- });
1607
- totalRetries++;
1608
- if (decision.action === "retry") {
1609
- log?.info(
1610
- { event: "retry", target_id: targetId, attempt_no: attemptNo, error_class: errorClass.class, delay_ms: decision.delay_ms },
1611
- "Retrying after delay"
1612
- );
1613
- await new Promise(
1614
- (resolve) => setTimeout(resolve, decision.delay_ms)
1615
- );
1616
- }
1617
- }
1618
- }
1619
- log?.warn(
1620
- { event: "failover_budget_exhausted", total_retries: totalRetries, total_failovers: totalFailovers },
1621
- "Failover budget exhausted"
1622
- );
1623
- return {
1624
- outcome: "failure",
1625
- reason: "failover budget exhausted",
1626
- attempts,
1627
- total_retries: totalRetries,
1628
- total_failovers: totalFailovers,
1629
- last_error_class: lastErrorClass
1630
- };
1631
- }
1632
- });
1633
-
1634
531
  // src/execution/stream-buffer.ts
1635
532
  var createStreamBuffer = () => {
1636
533
  const chunks = [];
@@ -1700,10 +597,7 @@ var createStreamStitcher = (_requestId) => {
1700
597
  * @param provenance - Segment provenance without visible_offset (computed here).
1701
598
  */
1702
599
  addSegment(buffer, provenance) {
1703
- const visibleOffset = segments.reduce(
1704
- (sum, entry) => sum + entry.text.length,
1705
- 0
1706
- );
600
+ const visibleOffset = segments.reduce((sum, entry) => sum + entry.text.length, 0);
1707
601
  segments.push({
1708
602
  text: buffer.snapshot(),
1709
603
  provenance: { ...provenance, visible_offset: visibleOffset }
@@ -1737,7 +631,7 @@ var createStreamStitcher = (_requestId) => {
1737
631
 
1738
632
  // src/execution/audit-collector.ts
1739
633
  var createAuditCollector = (logger, requestId) => {
1740
- const requestLogger = createRequestLogger(logger, requestId);
634
+ const requestLogger = chunkTJ7ZGZHD_cjs.createRequestLogger(logger, requestId);
1741
635
  const attempts = [];
1742
636
  const segments = [];
1743
637
  return {
@@ -1767,9 +661,9 @@ var createAuditCollector = (logger, requestId) => {
1767
661
  // src/execution/orchestrator.ts
1768
662
  var createExecutionOrchestrator = (deps) => ({
1769
663
  async execute(request) {
1770
- const requestId = request.request_id || generateCorrelationId();
664
+ const requestId = request.request_id || chunkTJ7ZGZHD_cjs.generateCorrelationId();
1771
665
  const auditCollector = createAuditCollector(deps.logger, requestId);
1772
- const stitcher = createStreamStitcher(requestId);
666
+ const stitcher = createStreamStitcher();
1773
667
  const startMs = Date.now();
1774
668
  const heuristicFlags = [];
1775
669
  let outcome = "failure";
@@ -1787,9 +681,7 @@ var createExecutionOrchestrator = (deps) => ({
1787
681
  for (const chunk of adapterResult.chunks) {
1788
682
  buffer.append(chunk);
1789
683
  }
1790
- heuristicFlags.push(
1791
- adapterResult.detection_mode === "heuristic"
1792
- );
684
+ heuristicFlags.push(adapterResult.detection_mode === "heuristic");
1793
685
  const value = { buffer, adapterResult };
1794
686
  return {
1795
687
  success: adapterResult.success,
@@ -1811,7 +703,12 @@ var createExecutionOrchestrator = (deps) => ({
1811
703
  outcome = "success";
1812
704
  finalTarget = failoverResult.target_id;
1813
705
  const attemptValue = failoverResult.value;
1814
- const continuationMode = stitcher.segmentCount() === 0 ? "same_target_resume" : "same_target_resume";
706
+ const continuationMode = stitcher.segmentCount() === 0 ? "same_target_resume" : determineContinuationMode(
707
+ stitcher.assemble().segments[stitcher.segmentCount() - 1].source_target_id,
708
+ failoverResult.target_id,
709
+ true
710
+ // same model assumption in plugin-only mode
711
+ );
1815
712
  stitcher.addSegment(attemptValue.buffer, {
1816
713
  request_id: requestId,
1817
714
  segment_id: stitcher.segmentCount(),
@@ -1826,12 +723,8 @@ var createExecutionOrchestrator = (deps) => ({
1826
723
  const stitchedOutput = stitcher.assemble();
1827
724
  const isDegraded2 = deps.mode === "plugin-only";
1828
725
  const isHeuristic2 = heuristicFlags.some(Boolean);
1829
- const uniqueModes = [
1830
- ...new Set(stitchedOutput.segments.map((s) => s.continuation_mode))
1831
- ];
1832
- const uniqueTargets = [
1833
- ...new Set(stitchedOutput.segments.map((s) => s.source_target_id))
1834
- ];
726
+ const uniqueModes = [...new Set(stitchedOutput.segments.map((s) => s.continuation_mode))];
727
+ const uniqueTargets = [...new Set(stitchedOutput.segments.map((s) => s.source_target_id))];
1835
728
  const provenance2 = {
1836
729
  request_id: requestId,
1837
730
  segments: stitchedOutput.segments,
@@ -1867,19 +760,22 @@ var createExecutionOrchestrator = (deps) => ({
1867
760
  };
1868
761
  } finally {
1869
762
  auditCollector.flush(outcome, finalTarget);
1870
- const requestLogger = createRequestLogger(deps.logger, requestId);
763
+ const requestLogger = chunkTJ7ZGZHD_cjs.createRequestLogger(deps.logger, requestId);
1871
764
  const endMs = Date.now();
1872
- requestLogger.info({
1873
- event: "request_summary",
1874
- component: "execution",
1875
- outcome,
1876
- total_attempts: summaryAttempts,
1877
- total_failovers: summaryFailovers,
1878
- total_retries: summaryRetries,
1879
- latency_ms: endMs - startMs,
1880
- targets_used: summaryTargets,
1881
- final_target: finalTarget ?? null
1882
- }, "Request complete");
765
+ requestLogger.info(
766
+ {
767
+ event: "request_summary",
768
+ component: "execution",
769
+ outcome,
770
+ total_attempts: summaryAttempts,
771
+ total_failovers: summaryFailovers,
772
+ total_retries: summaryRetries,
773
+ latency_ms: endMs - startMs,
774
+ targets_used: summaryTargets,
775
+ final_target: finalTarget ?? null
776
+ },
777
+ "Request complete"
778
+ );
1883
779
  }
1884
780
  }
1885
781
  });
@@ -1963,188 +859,17 @@ var createSdkAdapter = (_deps) => ({
1963
859
  var createModeAdapter = (mode, deps) => {
1964
860
  switch (mode) {
1965
861
  case "plugin-only":
1966
- return createPluginAdapter(deps);
862
+ return createPluginAdapter();
1967
863
  case "server-companion":
1968
- return createServerAdapter(deps);
864
+ return createServerAdapter();
1969
865
  case "sdk-control":
1970
- return createSdkAdapter(deps);
866
+ return createSdkAdapter();
1971
867
  default: {
1972
868
  const _exhaustive = mode;
1973
869
  throw new Error(`Unknown deployment mode: ${String(_exhaustive)}`);
1974
870
  }
1975
871
  }
1976
872
  };
1977
-
1978
- // src/operator/reload.ts
1979
- var computeConfigDiff = (oldConfig, newConfig) => {
1980
- const oldTargets = oldConfig.targets ?? [];
1981
- const newTargets = newConfig.targets ?? [];
1982
- const oldIds = new Set(oldTargets.map((t) => t.target_id));
1983
- const newIds = new Set(newTargets.map((t) => t.target_id));
1984
- const oldMap = new Map(oldTargets.map((t) => [t.target_id, t]));
1985
- const added = newTargets.filter((t) => !oldIds.has(t.target_id));
1986
- const removed = oldTargets.filter((t) => !newIds.has(t.target_id)).map((t) => t.target_id);
1987
- const modified = newTargets.filter((t) => {
1988
- if (!oldIds.has(t.target_id)) {
1989
- return false;
1990
- }
1991
- const old = oldMap.get(t.target_id);
1992
- if (!old) {
1993
- return false;
1994
- }
1995
- return hasConfigChanged(old, t);
1996
- });
1997
- return { added, removed, modified };
1998
- };
1999
- var hasConfigChanged = (oldTarget, newTarget) => {
2000
- if (oldTarget.profile !== newTarget.profile) return true;
2001
- if (oldTarget.enabled !== newTarget.enabled) return true;
2002
- if (oldTarget.operator_priority !== newTarget.operator_priority) return true;
2003
- if (JSON.stringify(oldTarget.policy_tags) !== JSON.stringify(newTarget.policy_tags)) return true;
2004
- if (JSON.stringify(oldTarget.capabilities) !== JSON.stringify(newTarget.capabilities)) return true;
2005
- if (oldTarget.retry_budget !== newTarget.retry_budget) return true;
2006
- if (oldTarget.failover_budget !== newTarget.failover_budget) return true;
2007
- if (oldTarget.concurrency_limit !== newTarget.concurrency_limit) return true;
2008
- return false;
2009
- };
2010
- var applyConfigDiff = (registry, diff) => {
2011
- for (const tc of diff.added) {
2012
- const entry = {
2013
- target_id: tc.target_id,
2014
- provider_id: tc.provider_id,
2015
- profile: tc.profile,
2016
- endpoint_id: tc.endpoint_id,
2017
- capabilities: [...tc.capabilities],
2018
- enabled: tc.enabled,
2019
- state: "Active",
2020
- health_score: INITIAL_HEALTH_SCORE,
2021
- cooldown_until: null,
2022
- latency_ema_ms: 0,
2023
- failure_score: 0,
2024
- operator_priority: tc.operator_priority,
2025
- policy_tags: [...tc.policy_tags]
2026
- };
2027
- registry.addTarget(entry);
2028
- }
2029
- for (const id of diff.removed) {
2030
- registry.updateState(id, "Disabled");
2031
- }
2032
- for (const tc of diff.modified) {
2033
- const existing = registry.getTarget(tc.target_id);
2034
- if (existing) {
2035
- const updated = {
2036
- ...existing,
2037
- provider_id: tc.provider_id,
2038
- profile: tc.profile,
2039
- endpoint_id: tc.endpoint_id,
2040
- capabilities: [...tc.capabilities],
2041
- enabled: tc.enabled,
2042
- operator_priority: tc.operator_priority,
2043
- policy_tags: [...tc.policy_tags]
2044
- };
2045
- registry.removeTarget(tc.target_id);
2046
- registry.addTarget(updated);
2047
- }
2048
- }
2049
- };
2050
-
2051
- // src/operator/commands.ts
2052
- var createRequestTraceBuffer = (capacity) => {
2053
- const entries = /* @__PURE__ */ new Map();
2054
- const order = [];
2055
- return {
2056
- record(entry) {
2057
- if (entries.has(entry.request_id)) {
2058
- const idx = order.indexOf(entry.request_id);
2059
- if (idx !== -1) {
2060
- order.splice(idx, 1);
2061
- }
2062
- }
2063
- while (order.length >= capacity) {
2064
- const oldest = order.shift();
2065
- if (oldest !== void 0) {
2066
- entries.delete(oldest);
2067
- }
2068
- }
2069
- entries.set(entry.request_id, entry);
2070
- order.push(entry.request_id);
2071
- },
2072
- lookup(requestId) {
2073
- return entries.get(requestId);
2074
- },
2075
- size() {
2076
- return entries.size;
2077
- }
2078
- };
2079
- };
2080
- var listTargets = (deps) => {
2081
- const allTargets = deps.registry.getAllTargets();
2082
- const targets = allTargets.map((t) => {
2083
- const cb = deps.circuitBreakers.get(t.target_id);
2084
- const cbState = cb ? cb.state() : "Active";
2085
- return {
2086
- target_id: t.target_id,
2087
- provider_id: t.provider_id,
2088
- profile: t.profile,
2089
- state: t.state,
2090
- health_score: t.health_score,
2091
- circuit_breaker_state: cbState,
2092
- cooldown_until: t.cooldown_until,
2093
- enabled: t.enabled,
2094
- latency_ema_ms: t.latency_ema_ms,
2095
- failure_score: t.failure_score,
2096
- operator_priority: t.operator_priority
2097
- };
2098
- });
2099
- return { targets, count: targets.length };
2100
- };
2101
- var targetAction = (deps, targetId, action, newState) => {
2102
- const updated = deps.registry.updateState(targetId, newState);
2103
- if (!updated) {
2104
- return { success: false, target_id: targetId, action, error: "target not found" };
2105
- }
2106
- return { success: true, target_id: targetId, action };
2107
- };
2108
- var pauseTarget = (deps, targetId) => targetAction(deps, targetId, "pause", "Disabled");
2109
- var resumeTarget = (deps, targetId) => targetAction(deps, targetId, "resume", "Active");
2110
- var drainTarget = (deps, targetId) => targetAction(deps, targetId, "drain", "Draining");
2111
- var disableTarget = (deps, targetId) => targetAction(deps, targetId, "disable", "Disabled");
2112
- var inspectRequest = (deps, requestId) => {
2113
- const trace = deps.traceBuffer.lookup(requestId);
2114
- if (!trace) {
2115
- return { found: false, request_id: requestId };
2116
- }
2117
- return { found: true, request_id: requestId, trace };
2118
- };
2119
- var reloadConfig = (deps, rawConfig) => {
2120
- try {
2121
- const newConfig = validateConfig(rawConfig);
2122
- const currentConfig = deps.configRef.current();
2123
- const diff = computeConfigDiff(currentConfig, newConfig);
2124
- applyConfigDiff(deps.registry, diff);
2125
- deps.configRef.swap(newConfig);
2126
- return {
2127
- success: true,
2128
- added: diff.added.map((t) => t.target_id),
2129
- removed: [...diff.removed],
2130
- modified: diff.modified.map((t) => t.target_id)
2131
- };
2132
- } catch (err) {
2133
- if (err instanceof ConfigValidationError) {
2134
- return {
2135
- success: false,
2136
- added: [],
2137
- removed: [],
2138
- modified: [],
2139
- diagnostics: err.diagnostics
2140
- };
2141
- }
2142
- throw err;
2143
- }
2144
- };
2145
-
2146
- // src/operator/server-auth.ts
2147
- var import_node_crypto2 = require("crypto");
2148
873
  var validateBearerToken = (authHeader, expectedToken) => {
2149
874
  if (authHeader === void 0) {
2150
875
  return { authorized: false, reason: "missing Authorization header" };
@@ -2158,428 +883,294 @@ var validateBearerToken = (authHeader, expectedToken) => {
2158
883
  }
2159
884
  const tokenBuffer = Buffer.from(token);
2160
885
  const expectedBuffer = Buffer.from(expectedToken);
2161
- if (!(0, import_node_crypto2.timingSafeEqual)(tokenBuffer, expectedBuffer)) {
886
+ if (!crypto.timingSafeEqual(tokenBuffer, expectedBuffer)) {
2162
887
  return { authorized: false, reason: "invalid token" };
2163
888
  }
2164
889
  return { authorized: true };
2165
890
  };
2166
891
 
2167
- // src/operator/plugin-tools.ts
2168
- var import_tool = require("@opencode-ai/plugin/tool");
2169
- var { schema: z3 } = import_tool.tool;
2170
- var createOperatorTools = (deps) => ({
2171
- listTargets: (0, import_tool.tool)({
2172
- description: "List all routing targets with health scores, states, and circuit breaker status.",
2173
- args: {},
2174
- async execute() {
2175
- deps.logger.info({ op: "listTargets" }, "operator: listTargets");
2176
- const result = listTargets(deps);
2177
- return JSON.stringify(result, null, 2);
2178
- }
2179
- }),
2180
- pauseTarget: (0, import_tool.tool)({
2181
- description: "Pause a target, preventing new requests from being routed to it.",
2182
- args: { target_id: z3.string().min(1) },
2183
- async execute(args) {
2184
- deps.logger.info(
2185
- { op: "pauseTarget", target_id: args.target_id },
2186
- "operator: pauseTarget"
2187
- );
2188
- const result = pauseTarget(deps, args.target_id);
2189
- return JSON.stringify(result, null, 2);
2190
- }
2191
- }),
2192
- resumeTarget: (0, import_tool.tool)({
2193
- description: "Resume a previously paused or disabled target, allowing new requests.",
2194
- args: { target_id: z3.string().min(1) },
2195
- async execute(args) {
2196
- deps.logger.info(
2197
- { op: "resumeTarget", target_id: args.target_id },
2198
- "operator: resumeTarget"
2199
- );
2200
- const result = resumeTarget(deps, args.target_id);
2201
- return JSON.stringify(result, null, 2);
2202
- }
2203
- }),
2204
- drainTarget: (0, import_tool.tool)({
2205
- description: "Drain a target, allowing in-flight requests to complete but preventing new ones.",
2206
- args: { target_id: z3.string().min(1) },
2207
- async execute(args) {
2208
- deps.logger.info(
2209
- { op: "drainTarget", target_id: args.target_id },
2210
- "operator: drainTarget"
2211
- );
2212
- const result = drainTarget(deps, args.target_id);
2213
- return JSON.stringify(result, null, 2);
2214
- }
2215
- }),
2216
- disableTarget: (0, import_tool.tool)({
2217
- description: "Disable a target entirely, removing it from routing.",
2218
- args: { target_id: z3.string().min(1) },
2219
- async execute(args) {
2220
- deps.logger.info(
2221
- { op: "disableTarget", target_id: args.target_id },
2222
- "operator: disableTarget"
2223
- );
2224
- const result = disableTarget(deps, args.target_id);
2225
- return JSON.stringify(result, null, 2);
2226
- }
2227
- }),
2228
- inspectRequest: (0, import_tool.tool)({
2229
- description: "Inspect a request trace by ID, showing attempts, segments, and outcome.",
2230
- args: { request_id: z3.string().min(1) },
2231
- async execute(args) {
2232
- deps.logger.info(
2233
- { op: "inspectRequest", request_id: args.request_id },
2234
- "operator: inspectRequest"
2235
- );
2236
- const result = inspectRequest(deps, args.request_id);
2237
- return JSON.stringify(result, null, 2);
2238
- }
2239
- }),
2240
- reloadConfig: (0, import_tool.tool)({
2241
- description: "Reload routing configuration with diff-apply. Validates new config before applying.",
2242
- args: { config: z3.record(z3.string(), z3.unknown()) },
2243
- async execute(args) {
2244
- deps.logger.info({ op: "reloadConfig" }, "operator: reloadConfig");
2245
- const result = reloadConfig(deps, args.config);
2246
- return JSON.stringify(result, null, 2);
2247
- }
2248
- })
892
+ Object.defineProperty(exports, "ADMISSION_RESULTS", {
893
+ enumerable: true,
894
+ get: function () { return chunkTJ7ZGZHD_cjs.ADMISSION_RESULTS; }
2249
895
  });
2250
-
2251
- // src/profiles/store.ts
2252
- var import_promises = require("fs/promises");
2253
- var import_node_os = require("os");
2254
- var import_node_path = require("path");
2255
- var PROFILES_DIR = (0, import_node_path.join)((0, import_node_os.homedir)(), ".local", "share", "o-switcher");
2256
- var PROFILES_PATH = (0, import_node_path.join)(PROFILES_DIR, "profiles.json");
2257
- var loadProfiles = async (path = PROFILES_PATH) => {
2258
- try {
2259
- const content = await (0, import_promises.readFile)(path, "utf-8");
2260
- return JSON.parse(content);
2261
- } catch (err) {
2262
- const code = err.code;
2263
- if (code === "ENOENT") {
2264
- return {};
2265
- }
2266
- throw err;
2267
- }
2268
- };
2269
- var saveProfiles = async (store, path = PROFILES_PATH, logger) => {
2270
- const dir = (0, import_node_path.dirname)(path);
2271
- await (0, import_promises.mkdir)(dir, { recursive: true });
2272
- const tmpPath = `${path}.tmp`;
2273
- await (0, import_promises.writeFile)(tmpPath, JSON.stringify(store, null, 2), "utf-8");
2274
- await (0, import_promises.rename)(tmpPath, path);
2275
- logger?.info({ path }, "Profiles saved to disk");
2276
- };
2277
- var credentialsMatch = (a, b) => {
2278
- if (a.type !== b.type) return false;
2279
- if (a.type === "api-key" && b.type === "api-key") {
2280
- return a.key === b.key;
2281
- }
2282
- if (a.type === "oauth" && b.type === "oauth") {
2283
- return a.refresh === b.refresh && a.access === b.access && a.expires === b.expires && a.accountId === b.accountId;
2284
- }
2285
- return false;
2286
- };
2287
- var addProfile = (store, provider, credentials) => {
2288
- const isDuplicate = Object.values(store).some(
2289
- (entry2) => entry2.provider === provider && credentialsMatch(entry2.credentials, credentials)
2290
- );
2291
- if (isDuplicate) {
2292
- return store;
2293
- }
2294
- const id = nextProfileId(store, provider);
2295
- const entry = {
2296
- id,
2297
- provider,
2298
- type: credentials.type,
2299
- credentials,
2300
- created: (/* @__PURE__ */ new Date()).toISOString()
2301
- };
2302
- return { ...store, [id]: entry };
2303
- };
2304
- var removeProfile = (store, id) => {
2305
- if (store[id] === void 0) {
2306
- return { store, removed: false };
2307
- }
2308
- const { [id]: _removed, ...rest } = store;
2309
- return { store: rest, removed: true };
2310
- };
2311
- var listProfiles = (store) => {
2312
- return Object.values(store).sort(
2313
- (a, b) => new Date(a.created).getTime() - new Date(b.created).getTime()
2314
- );
2315
- };
2316
- var nextProfileId = (store, provider) => {
2317
- const prefix = `${provider}-`;
2318
- const maxN = Object.keys(store).filter((key) => key.startsWith(prefix)).map((key) => Number(key.slice(prefix.length))).filter((n) => !Number.isNaN(n)).reduce((max, n) => Math.max(max, n), 0);
2319
- return `${provider}-${maxN + 1}`;
2320
- };
2321
-
2322
- // src/profiles/watcher.ts
2323
- var import_promises2 = require("fs/promises");
2324
- var import_node_os2 = require("os");
2325
- var import_node_path2 = require("path");
2326
- var AUTH_JSON_PATH = (0, import_node_path2.join)(
2327
- (0, import_node_os2.homedir)(),
2328
- ".local",
2329
- "share",
2330
- "opencode",
2331
- "auth.json"
2332
- );
2333
- var DEBOUNCE_MS = 100;
2334
- var readAuthJson = async (path) => {
2335
- try {
2336
- const content = await (0, import_promises2.readFile)(path, "utf-8");
2337
- return JSON.parse(content);
2338
- } catch {
2339
- return {};
2340
- }
2341
- };
2342
- var toCredential = (entry) => {
2343
- if (entry.type === "oauth" || entry.refresh !== void 0 && entry.access !== void 0) {
2344
- return {
2345
- type: "oauth",
2346
- refresh: String(entry.refresh ?? ""),
2347
- access: String(entry.access ?? ""),
2348
- expires: Number(entry.expires ?? 0),
2349
- accountId: entry.accountId !== void 0 ? String(entry.accountId) : void 0
2350
- };
2351
- }
2352
- return {
2353
- type: "api-key",
2354
- key: String(entry.key ?? "")
2355
- };
2356
- };
2357
- var entriesEqual = (a, b) => {
2358
- if (a === void 0 && b === void 0) return true;
2359
- if (a === void 0 || b === void 0) return false;
2360
- return JSON.stringify(a) === JSON.stringify(b);
2361
- };
2362
- var createAuthWatcher = (options) => {
2363
- const authPath = options?.authJsonPath ?? AUTH_JSON_PATH;
2364
- const profPath = options?.profilesPath ?? PROFILES_PATH;
2365
- const log = options?.logger?.child({ component: "profile-watcher" });
2366
- let lastKnownAuth = {};
2367
- let abortController = null;
2368
- let debounceTimer = null;
2369
- let watchPromise = null;
2370
- const processChange = async () => {
2371
- const newAuth = await readAuthJson(authPath);
2372
- let store = await loadProfiles(profPath);
2373
- let changed = false;
2374
- for (const [provider, entry] of Object.entries(newAuth)) {
2375
- if (!entry) continue;
2376
- const previousEntry = lastKnownAuth[provider];
2377
- if (previousEntry !== void 0 && !entriesEqual(previousEntry, entry)) {
2378
- log?.info({ provider, action: "credential_changed" }, "Credential change detected \u2014 saving previous credential");
2379
- const prevCredential = toCredential(previousEntry);
2380
- const newStore = addProfile(store, provider, prevCredential);
2381
- if (newStore !== store) {
2382
- store = newStore;
2383
- changed = true;
2384
- }
2385
- } else if (previousEntry === void 0) {
2386
- log?.info({ provider, action: "new_provider" }, "New provider detected");
2387
- const credential = toCredential(entry);
2388
- const newStore = addProfile(store, provider, credential);
2389
- if (newStore !== store) {
2390
- store = newStore;
2391
- changed = true;
2392
- }
2393
- }
2394
- }
2395
- if (changed) {
2396
- await saveProfiles(store, profPath);
2397
- log?.info({ profiles_saved: true }, "Profiles saved to disk");
2398
- }
2399
- lastKnownAuth = newAuth;
2400
- };
2401
- const onFileChange = () => {
2402
- if (debounceTimer !== null) {
2403
- clearTimeout(debounceTimer);
2404
- }
2405
- debounceTimer = setTimeout(() => {
2406
- debounceTimer = null;
2407
- processChange().catch((err) => {
2408
- log?.warn({ err }, "Error processing auth change");
2409
- });
2410
- }, DEBOUNCE_MS);
2411
- };
2412
- const start = async () => {
2413
- const currentAuth = await readAuthJson(authPath);
2414
- const currentStore = await loadProfiles(profPath);
2415
- log?.info({ providers: Object.keys(currentAuth) }, "Auth watcher started");
2416
- if (Object.keys(currentStore).length === 0 && Object.keys(currentAuth).length > 0) {
2417
- let store = {};
2418
- for (const [provider, entry] of Object.entries(currentAuth)) {
2419
- if (!entry) continue;
2420
- const credential = toCredential(entry);
2421
- store = addProfile(store, provider, credential);
2422
- }
2423
- await saveProfiles(store, profPath);
2424
- log?.info({ profiles_initialized: Object.keys(store).length }, "Initialized profiles from auth.json");
2425
- }
2426
- lastKnownAuth = currentAuth;
2427
- abortController = new AbortController();
2428
- const parentDir = (0, import_node_path2.dirname)(authPath);
2429
- watchPromise = (async () => {
2430
- try {
2431
- const watcher = (0, import_promises2.watch)(parentDir, {
2432
- signal: abortController.signal
2433
- });
2434
- for await (const event of watcher) {
2435
- if (event.filename === "auth.json" || event.filename === null) {
2436
- onFileChange();
2437
- }
2438
- }
2439
- } catch (err) {
2440
- const name = err.name;
2441
- if (name !== "AbortError") {
2442
- }
2443
- }
2444
- })();
2445
- };
2446
- const stop = () => {
2447
- if (debounceTimer !== null) {
2448
- clearTimeout(debounceTimer);
2449
- debounceTimer = null;
2450
- }
2451
- if (abortController !== null) {
2452
- abortController.abort();
2453
- abortController = null;
2454
- }
2455
- watchPromise = null;
2456
- log?.info("Auth watcher stopped");
2457
- };
2458
- return { start, stop };
2459
- };
2460
-
2461
- // src/profiles/tools.ts
2462
- var import_tool2 = require("@opencode-ai/plugin/tool");
2463
- var { schema: z4 } = import_tool2.tool;
2464
- var createProfileTools = (options) => ({
2465
- profilesList: (0, import_tool2.tool)({
2466
- description: "List all saved auth profiles with provider, type, and creation date.",
2467
- args: {},
2468
- async execute() {
2469
- const store = await loadProfiles(options?.profilesPath);
2470
- const profiles = listProfiles(store);
2471
- const result = profiles.map((entry) => ({
2472
- id: entry.id,
2473
- provider: entry.provider,
2474
- type: entry.type,
2475
- created: entry.created
2476
- }));
2477
- return JSON.stringify(result, null, 2);
2478
- }
2479
- }),
2480
- profilesRemove: (0, import_tool2.tool)({
2481
- description: "Remove a saved auth profile by ID.",
2482
- args: { id: z4.string().min(1) },
2483
- async execute(args) {
2484
- const store = await loadProfiles(options?.profilesPath);
2485
- const { store: newStore, removed } = removeProfile(store, args.id);
2486
- if (removed) {
2487
- await saveProfiles(newStore, options?.profilesPath);
2488
- return JSON.stringify({ success: true, id: args.id });
2489
- }
2490
- return JSON.stringify({
2491
- success: false,
2492
- id: args.id,
2493
- error: "Profile not found"
2494
- });
2495
- }
2496
- })
2497
- });
2498
- // Annotate the CommonJS export names for ESM import in node:
2499
- 0 && (module.exports = {
2500
- ADMISSION_RESULTS,
2501
- BackoffConfigSchema,
2502
- ConfigValidationError,
2503
- DEFAULT_ALPHA,
2504
- DEFAULT_BACKOFF_BASE_MS,
2505
- DEFAULT_BACKOFF_JITTER,
2506
- DEFAULT_BACKOFF_MAX_MS,
2507
- DEFAULT_BACKOFF_MULTIPLIER,
2508
- DEFAULT_BACKOFF_PARAMS,
2509
- DEFAULT_FAILOVER_BUDGET,
2510
- DEFAULT_RETRY,
2511
- DEFAULT_RETRY_BUDGET,
2512
- DEFAULT_TIMEOUT_MS,
2513
- DualBreaker,
2514
- EXCLUSION_REASONS,
2515
- ErrorClassSchema,
2516
- HEURISTIC_PATTERNS,
2517
- INITIAL_HEALTH_SCORE,
2518
- PROVIDER_PATTERNS,
2519
- REDACT_PATHS,
2520
- SwitcherConfigSchema,
2521
- TARGET_STATES,
2522
- TEMPORAL_QUOTA_PATTERN,
2523
- TargetConfigSchema,
2524
- TargetRegistry,
2525
- addProfile,
2526
- applyConfigDiff,
2527
- checkHardRejects,
2528
- classify,
2529
- computeBackoffMs,
2530
- computeConfigDiff,
2531
- computeCooldownMs,
2532
- computeScore,
2533
- createAdmissionController,
2534
- createAuditCollector,
2535
- createAuditLogger,
2536
- createAuthWatcher,
2537
- createCircuitBreaker,
2538
- createConcurrencyTracker,
2539
- createCooldownManager,
2540
- createExecutionOrchestrator,
2541
- createFailoverOrchestrator,
2542
- createLogSubscriber,
2543
- createModeAdapter,
2544
- createOperatorTools,
2545
- createProfileTools,
2546
- createRegistry,
2547
- createRequestLogger,
2548
- createRequestTraceBuffer,
2549
- createRetryPolicy,
2550
- createRoutingEventBus,
2551
- createStreamBuffer,
2552
- createStreamStitcher,
2553
- detectDeploymentMode,
2554
- determineContinuationMode,
2555
- directSignalFromResponse,
2556
- disableTarget,
2557
- discoverTargets,
2558
- discoverTargetsFromProfiles,
2559
- drainTarget,
2560
- extractRetryAfterMs,
2561
- generateCorrelationId,
2562
- getExclusionReason,
2563
- getModeCapabilities,
2564
- getSignalFidelity,
2565
- getTargetStateTransition,
2566
- heuristicSignalFromEvent,
2567
- inspectRequest,
2568
- isRetryable,
2569
- listProfiles,
2570
- listTargets,
2571
- loadProfiles,
2572
- nextProfileId,
2573
- normalizeLatency,
2574
- pauseTarget,
2575
- reloadConfig,
2576
- removeProfile,
2577
- resumeTarget,
2578
- saveProfiles,
2579
- selectTarget,
2580
- updateHealthScore,
2581
- updateLatencyEma,
2582
- validateBearerToken,
2583
- validateConfig
2584
- });
2585
- //# sourceMappingURL=index.cjs.map
896
+ Object.defineProperty(exports, "BackoffConfigSchema", {
897
+ enumerable: true,
898
+ get: function () { return chunkTJ7ZGZHD_cjs.BackoffConfigSchema; }
899
+ });
900
+ Object.defineProperty(exports, "ConfigValidationError", {
901
+ enumerable: true,
902
+ get: function () { return chunkTJ7ZGZHD_cjs.ConfigValidationError; }
903
+ });
904
+ Object.defineProperty(exports, "DEFAULT_ALPHA", {
905
+ enumerable: true,
906
+ get: function () { return chunkTJ7ZGZHD_cjs.DEFAULT_ALPHA; }
907
+ });
908
+ Object.defineProperty(exports, "DEFAULT_BACKOFF_BASE_MS", {
909
+ enumerable: true,
910
+ get: function () { return chunkTJ7ZGZHD_cjs.DEFAULT_BACKOFF_BASE_MS; }
911
+ });
912
+ Object.defineProperty(exports, "DEFAULT_BACKOFF_JITTER", {
913
+ enumerable: true,
914
+ get: function () { return chunkTJ7ZGZHD_cjs.DEFAULT_BACKOFF_JITTER; }
915
+ });
916
+ Object.defineProperty(exports, "DEFAULT_BACKOFF_MAX_MS", {
917
+ enumerable: true,
918
+ get: function () { return chunkTJ7ZGZHD_cjs.DEFAULT_BACKOFF_MAX_MS; }
919
+ });
920
+ Object.defineProperty(exports, "DEFAULT_BACKOFF_MULTIPLIER", {
921
+ enumerable: true,
922
+ get: function () { return chunkTJ7ZGZHD_cjs.DEFAULT_BACKOFF_MULTIPLIER; }
923
+ });
924
+ Object.defineProperty(exports, "DEFAULT_BACKOFF_PARAMS", {
925
+ enumerable: true,
926
+ get: function () { return chunkTJ7ZGZHD_cjs.DEFAULT_BACKOFF_PARAMS; }
927
+ });
928
+ Object.defineProperty(exports, "DEFAULT_FAILOVER_BUDGET", {
929
+ enumerable: true,
930
+ get: function () { return chunkTJ7ZGZHD_cjs.DEFAULT_FAILOVER_BUDGET; }
931
+ });
932
+ Object.defineProperty(exports, "DEFAULT_RETRY", {
933
+ enumerable: true,
934
+ get: function () { return chunkTJ7ZGZHD_cjs.DEFAULT_RETRY; }
935
+ });
936
+ Object.defineProperty(exports, "DEFAULT_RETRY_BUDGET", {
937
+ enumerable: true,
938
+ get: function () { return chunkTJ7ZGZHD_cjs.DEFAULT_RETRY_BUDGET; }
939
+ });
940
+ Object.defineProperty(exports, "DEFAULT_TIMEOUT_MS", {
941
+ enumerable: true,
942
+ get: function () { return chunkTJ7ZGZHD_cjs.DEFAULT_TIMEOUT_MS; }
943
+ });
944
+ Object.defineProperty(exports, "DualBreaker", {
945
+ enumerable: true,
946
+ get: function () { return chunkTJ7ZGZHD_cjs.DualBreaker; }
947
+ });
948
+ Object.defineProperty(exports, "EXCLUSION_REASONS", {
949
+ enumerable: true,
950
+ get: function () { return chunkTJ7ZGZHD_cjs.EXCLUSION_REASONS; }
951
+ });
952
+ Object.defineProperty(exports, "ErrorClassSchema", {
953
+ enumerable: true,
954
+ get: function () { return chunkTJ7ZGZHD_cjs.ErrorClassSchema; }
955
+ });
956
+ Object.defineProperty(exports, "INITIAL_HEALTH_SCORE", {
957
+ enumerable: true,
958
+ get: function () { return chunkTJ7ZGZHD_cjs.INITIAL_HEALTH_SCORE; }
959
+ });
960
+ Object.defineProperty(exports, "REDACT_PATHS", {
961
+ enumerable: true,
962
+ get: function () { return chunkTJ7ZGZHD_cjs.REDACT_PATHS; }
963
+ });
964
+ Object.defineProperty(exports, "SwitcherConfigSchema", {
965
+ enumerable: true,
966
+ get: function () { return chunkTJ7ZGZHD_cjs.SwitcherConfigSchema; }
967
+ });
968
+ Object.defineProperty(exports, "TARGET_STATES", {
969
+ enumerable: true,
970
+ get: function () { return chunkTJ7ZGZHD_cjs.TARGET_STATES; }
971
+ });
972
+ Object.defineProperty(exports, "TargetConfigSchema", {
973
+ enumerable: true,
974
+ get: function () { return chunkTJ7ZGZHD_cjs.TargetConfigSchema; }
975
+ });
976
+ Object.defineProperty(exports, "TargetRegistry", {
977
+ enumerable: true,
978
+ get: function () { return chunkTJ7ZGZHD_cjs.TargetRegistry; }
979
+ });
980
+ Object.defineProperty(exports, "addProfile", {
981
+ enumerable: true,
982
+ get: function () { return chunkTJ7ZGZHD_cjs.addProfile; }
983
+ });
984
+ Object.defineProperty(exports, "applyConfigDiff", {
985
+ enumerable: true,
986
+ get: function () { return chunkTJ7ZGZHD_cjs.applyConfigDiff; }
987
+ });
988
+ Object.defineProperty(exports, "checkHardRejects", {
989
+ enumerable: true,
990
+ get: function () { return chunkTJ7ZGZHD_cjs.checkHardRejects; }
991
+ });
992
+ Object.defineProperty(exports, "computeBackoffMs", {
993
+ enumerable: true,
994
+ get: function () { return chunkTJ7ZGZHD_cjs.computeBackoffMs; }
995
+ });
996
+ Object.defineProperty(exports, "computeConfigDiff", {
997
+ enumerable: true,
998
+ get: function () { return chunkTJ7ZGZHD_cjs.computeConfigDiff; }
999
+ });
1000
+ Object.defineProperty(exports, "computeCooldownMs", {
1001
+ enumerable: true,
1002
+ get: function () { return chunkTJ7ZGZHD_cjs.computeCooldownMs; }
1003
+ });
1004
+ Object.defineProperty(exports, "computeScore", {
1005
+ enumerable: true,
1006
+ get: function () { return chunkTJ7ZGZHD_cjs.computeScore; }
1007
+ });
1008
+ Object.defineProperty(exports, "createAdmissionController", {
1009
+ enumerable: true,
1010
+ get: function () { return chunkTJ7ZGZHD_cjs.createAdmissionController; }
1011
+ });
1012
+ Object.defineProperty(exports, "createAuditLogger", {
1013
+ enumerable: true,
1014
+ get: function () { return chunkTJ7ZGZHD_cjs.createAuditLogger; }
1015
+ });
1016
+ Object.defineProperty(exports, "createAuthWatcher", {
1017
+ enumerable: true,
1018
+ get: function () { return chunkTJ7ZGZHD_cjs.createAuthWatcher; }
1019
+ });
1020
+ Object.defineProperty(exports, "createCircuitBreaker", {
1021
+ enumerable: true,
1022
+ get: function () { return chunkTJ7ZGZHD_cjs.createCircuitBreaker; }
1023
+ });
1024
+ Object.defineProperty(exports, "createConcurrencyTracker", {
1025
+ enumerable: true,
1026
+ get: function () { return chunkTJ7ZGZHD_cjs.createConcurrencyTracker; }
1027
+ });
1028
+ Object.defineProperty(exports, "createCooldownManager", {
1029
+ enumerable: true,
1030
+ get: function () { return chunkTJ7ZGZHD_cjs.createCooldownManager; }
1031
+ });
1032
+ Object.defineProperty(exports, "createFailoverOrchestrator", {
1033
+ enumerable: true,
1034
+ get: function () { return chunkTJ7ZGZHD_cjs.createFailoverOrchestrator; }
1035
+ });
1036
+ Object.defineProperty(exports, "createLogSubscriber", {
1037
+ enumerable: true,
1038
+ get: function () { return chunkTJ7ZGZHD_cjs.createLogSubscriber; }
1039
+ });
1040
+ Object.defineProperty(exports, "createOperatorTools", {
1041
+ enumerable: true,
1042
+ get: function () { return chunkTJ7ZGZHD_cjs.createOperatorTools; }
1043
+ });
1044
+ Object.defineProperty(exports, "createProfileTools", {
1045
+ enumerable: true,
1046
+ get: function () { return chunkTJ7ZGZHD_cjs.createProfileTools; }
1047
+ });
1048
+ Object.defineProperty(exports, "createRegistry", {
1049
+ enumerable: true,
1050
+ get: function () { return chunkTJ7ZGZHD_cjs.createRegistry; }
1051
+ });
1052
+ Object.defineProperty(exports, "createRequestLogger", {
1053
+ enumerable: true,
1054
+ get: function () { return chunkTJ7ZGZHD_cjs.createRequestLogger; }
1055
+ });
1056
+ Object.defineProperty(exports, "createRequestTraceBuffer", {
1057
+ enumerable: true,
1058
+ get: function () { return chunkTJ7ZGZHD_cjs.createRequestTraceBuffer; }
1059
+ });
1060
+ Object.defineProperty(exports, "createRetryPolicy", {
1061
+ enumerable: true,
1062
+ get: function () { return chunkTJ7ZGZHD_cjs.createRetryPolicy; }
1063
+ });
1064
+ Object.defineProperty(exports, "createRoutingEventBus", {
1065
+ enumerable: true,
1066
+ get: function () { return chunkTJ7ZGZHD_cjs.createRoutingEventBus; }
1067
+ });
1068
+ Object.defineProperty(exports, "disableTarget", {
1069
+ enumerable: true,
1070
+ get: function () { return chunkTJ7ZGZHD_cjs.disableTarget; }
1071
+ });
1072
+ Object.defineProperty(exports, "discoverTargets", {
1073
+ enumerable: true,
1074
+ get: function () { return chunkTJ7ZGZHD_cjs.discoverTargets; }
1075
+ });
1076
+ Object.defineProperty(exports, "discoverTargetsFromProfiles", {
1077
+ enumerable: true,
1078
+ get: function () { return chunkTJ7ZGZHD_cjs.discoverTargetsFromProfiles; }
1079
+ });
1080
+ Object.defineProperty(exports, "drainTarget", {
1081
+ enumerable: true,
1082
+ get: function () { return chunkTJ7ZGZHD_cjs.drainTarget; }
1083
+ });
1084
+ Object.defineProperty(exports, "generateCorrelationId", {
1085
+ enumerable: true,
1086
+ get: function () { return chunkTJ7ZGZHD_cjs.generateCorrelationId; }
1087
+ });
1088
+ Object.defineProperty(exports, "getExclusionReason", {
1089
+ enumerable: true,
1090
+ get: function () { return chunkTJ7ZGZHD_cjs.getExclusionReason; }
1091
+ });
1092
+ Object.defineProperty(exports, "getTargetStateTransition", {
1093
+ enumerable: true,
1094
+ get: function () { return chunkTJ7ZGZHD_cjs.getTargetStateTransition; }
1095
+ });
1096
+ Object.defineProperty(exports, "inspectRequest", {
1097
+ enumerable: true,
1098
+ get: function () { return chunkTJ7ZGZHD_cjs.inspectRequest; }
1099
+ });
1100
+ Object.defineProperty(exports, "isRetryable", {
1101
+ enumerable: true,
1102
+ get: function () { return chunkTJ7ZGZHD_cjs.isRetryable; }
1103
+ });
1104
+ Object.defineProperty(exports, "listProfiles", {
1105
+ enumerable: true,
1106
+ get: function () { return chunkTJ7ZGZHD_cjs.listProfiles; }
1107
+ });
1108
+ Object.defineProperty(exports, "listTargets", {
1109
+ enumerable: true,
1110
+ get: function () { return chunkTJ7ZGZHD_cjs.listTargets; }
1111
+ });
1112
+ Object.defineProperty(exports, "loadProfiles", {
1113
+ enumerable: true,
1114
+ get: function () { return chunkTJ7ZGZHD_cjs.loadProfiles; }
1115
+ });
1116
+ Object.defineProperty(exports, "nextProfileId", {
1117
+ enumerable: true,
1118
+ get: function () { return chunkTJ7ZGZHD_cjs.nextProfileId; }
1119
+ });
1120
+ Object.defineProperty(exports, "normalizeLatency", {
1121
+ enumerable: true,
1122
+ get: function () { return chunkTJ7ZGZHD_cjs.normalizeLatency; }
1123
+ });
1124
+ Object.defineProperty(exports, "pauseTarget", {
1125
+ enumerable: true,
1126
+ get: function () { return chunkTJ7ZGZHD_cjs.pauseTarget; }
1127
+ });
1128
+ Object.defineProperty(exports, "reloadConfig", {
1129
+ enumerable: true,
1130
+ get: function () { return chunkTJ7ZGZHD_cjs.reloadConfig; }
1131
+ });
1132
+ Object.defineProperty(exports, "removeProfile", {
1133
+ enumerable: true,
1134
+ get: function () { return chunkTJ7ZGZHD_cjs.removeProfile; }
1135
+ });
1136
+ Object.defineProperty(exports, "resumeTarget", {
1137
+ enumerable: true,
1138
+ get: function () { return chunkTJ7ZGZHD_cjs.resumeTarget; }
1139
+ });
1140
+ Object.defineProperty(exports, "saveProfiles", {
1141
+ enumerable: true,
1142
+ get: function () { return chunkTJ7ZGZHD_cjs.saveProfiles; }
1143
+ });
1144
+ Object.defineProperty(exports, "selectTarget", {
1145
+ enumerable: true,
1146
+ get: function () { return chunkTJ7ZGZHD_cjs.selectTarget; }
1147
+ });
1148
+ Object.defineProperty(exports, "updateHealthScore", {
1149
+ enumerable: true,
1150
+ get: function () { return chunkTJ7ZGZHD_cjs.updateHealthScore; }
1151
+ });
1152
+ Object.defineProperty(exports, "updateLatencyEma", {
1153
+ enumerable: true,
1154
+ get: function () { return chunkTJ7ZGZHD_cjs.updateLatencyEma; }
1155
+ });
1156
+ Object.defineProperty(exports, "validateConfig", {
1157
+ enumerable: true,
1158
+ get: function () { return chunkTJ7ZGZHD_cjs.validateConfig; }
1159
+ });
1160
+ exports.HEURISTIC_PATTERNS = HEURISTIC_PATTERNS;
1161
+ exports.PROVIDER_PATTERNS = PROVIDER_PATTERNS;
1162
+ exports.TEMPORAL_QUOTA_PATTERN = TEMPORAL_QUOTA_PATTERN;
1163
+ exports.classify = classify;
1164
+ exports.createAuditCollector = createAuditCollector;
1165
+ exports.createExecutionOrchestrator = createExecutionOrchestrator;
1166
+ exports.createModeAdapter = createModeAdapter;
1167
+ exports.createStreamBuffer = createStreamBuffer;
1168
+ exports.createStreamStitcher = createStreamStitcher;
1169
+ exports.detectDeploymentMode = detectDeploymentMode;
1170
+ exports.determineContinuationMode = determineContinuationMode;
1171
+ exports.directSignalFromResponse = directSignalFromResponse;
1172
+ exports.extractRetryAfterMs = extractRetryAfterMs;
1173
+ exports.getModeCapabilities = getModeCapabilities;
1174
+ exports.getSignalFidelity = getSignalFidelity;
1175
+ exports.heuristicSignalFromEvent = heuristicSignalFromEvent;
1176
+ exports.validateBearerToken = validateBearerToken;