-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry-edges.test.ts
More file actions
202 lines (186 loc) · 7.56 KB
/
Copy pathregistry-edges.test.ts
File metadata and controls
202 lines (186 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Group J — registry + budget gating offline tests.
*
* Pins the gaps in the existing registry suite:
* - BudgetExceededError carries the correct `gatingKind` ("requests" vs "cost")
* - Cost limit windows: hour-cap kicks in independently of day-cap, etc.
* - Fallback chain when primary throws ProviderUnavailableError (not budget) → second tried
* - Malformed env config (wrong number of pipe-separated parts) → ConfigError at construction
* - NoProvidersAvailableError exposes attempted aliases and per-alias reasons
*/
import { describe, expect, it } from "vitest";
import {
ConfigError,
createRegistryFromEnv,
NoProvidersAvailableError,
ProviderUnavailableError,
type AdapterRegistration,
type GenerateTextResult,
type LLMPort,
type ModelPricing,
} from "../src/index.js";
const PRICING: ModelPricing = { inputPer1M: 1.0, outputPer1M: 4.0 };
function fakePort(modelId: string, alias: string, opts: { failWith?: Error } = {}): LLMPort {
return {
async generateText(): Promise<GenerateTextResult> {
if (opts.failWith) throw opts.failWith;
return {
text: `from ${alias}/${modelId}`,
// Each call uses 1M input + 1M output → $5 per call (so cost gating
// can be exercised with simple integer thresholds)
usage: { inputTokens: 1_000_000, outputTokens: 1_000_000, totalTokens: 2_000_000 },
cost: { inputUSD: 1.0, outputUSD: 4.0, totalUSD: 5.0 },
modelId,
providerAlias: alias,
latencyMs: 1,
};
},
async generateStructured() {
throw new Error("not used in this test");
},
async runAgent() {
throw new Error("not used in this test");
},
streamText: async function* () {
yield "stub";
},
streamStructured: async function* () {
yield {} as never;
},
};
}
const registration = (failWith?: Error): AdapterRegistration => ({
name: "test",
pricing: { "test-model": PRICING },
createLLMPort: (modelId, alias) => fakePort(modelId, alias, failWith ? { failWith } : {}),
});
describe("Group J: registry + budget gating offline", () => {
it("BudgetExceededError carries gatingKind='requests' when request limit trips", async () => {
const registry = createRegistryFromEnv({
env: {
LLM_PROVIDER_FAST: "test|test-model|req:1/hour",
LLM_TASK_ROUTE_TRIAGE: "fast",
},
adapters: { test: registration() },
});
const llm = registry.getPort();
await llm.generateText({ taskType: "triage", messages: [{ role: "user" as const, content: "1" }] });
// Second call: only "fast" in chain, budget exhausted → no fallback,
// and we expect NoProvidersAvailableError. Look at the reasons map
// to confirm budget gating fired with the correct gatingKind.
let caught: unknown;
try {
await llm.generateText({ taskType: "triage", messages: [{ role: "user" as const, content: "2" }] });
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(NoProvidersAvailableError);
const e = caught as NoProvidersAvailableError;
expect(e.attempted).toContain("fast");
// The reason for "fast" should mention budget / requests / exceeded
const fastReason = e.reasons["fast"]?.toLowerCase() ?? "";
expect(fastReason).toMatch(/budget|exceed|request/);
});
it("BudgetExceededError carries gatingKind='cost' when cost limit trips", async () => {
// Cost limit: $4/day. One call costs $5 → trip on first call.
const registry = createRegistryFromEnv({
env: {
LLM_PROVIDER_FAST: "test|test-model|cost:4/day",
LLM_TASK_ROUTE_TRIAGE: "fast",
},
adapters: { test: registration() },
});
const llm = registry.getPort();
// First call: $5 incurred. Recorded after success.
const r1 = await llm.generateText({ taskType: "triage", messages: [{ role: "user" as const, content: "1" }] });
expect(r1.providerAlias).toBe("fast");
// Second call: budget already exceeded.
let caught: unknown;
try {
await llm.generateText({ taskType: "triage", messages: [{ role: "user" as const, content: "2" }] });
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(NoProvidersAvailableError);
const reason = (caught as NoProvidersAvailableError).reasons["fast"]?.toLowerCase() ?? "";
expect(reason).toMatch(/cost|budget|exceed/);
});
it("runtime ProviderUnavailableError on the first provider triggers fallback to the next (alpha.7+)", async () => {
// alpha.7 BEHAVIOR: the registry walks the fallback chain on runtime
// errors matching the `runtimeFallback` predicate. Default is to walk on
// `ProviderUnavailableError`. The `fast` provider 503s, the registry
// moves to `backup`, and the call succeeds.
//
// Previously (≤ alpha.6) this test documented the OPPOSITE: runtime
// errors propagated and never fell back. See runtime-fallback.test.ts
// for the full alpha.7 surface (predicate config, NoProvidersAvailable
// on full-chain failure, streaming semantics).
const network503 = new ProviderUnavailableError("fast", new Error("503 Service Unavailable"));
const registry = createRegistryFromEnv({
env: {
LLM_PROVIDER_FAST: "test|test-model|unlimited",
LLM_PROVIDER_BACKUP: "test|test-model|unlimited",
LLM_TASK_ROUTE_TRIAGE: "fast,backup",
},
adapters: {
test: {
name: "test",
pricing: { "test-model": PRICING },
createLLMPort: (modelId, alias) =>
fakePort(modelId, alias, alias === "fast" ? { failWith: network503 } : {}),
},
},
});
const llm = registry.getPort();
const result = await llm.generateText({ taskType: "triage", messages: [{ role: "user" as const, content: "x" }] });
expect(result.providerAlias).toBe("backup");
});
it("malformed env (wrong number of pipe-separated parts) throws ConfigError at construction", () => {
expect(() =>
createRegistryFromEnv({
env: {
// Missing the budget portion — only 2 parts instead of 3
LLM_PROVIDER_FAST: "test|test-model",
LLM_TASK_ROUTE_TRIAGE: "fast",
},
adapters: { test: registration() },
}),
).toThrow(ConfigError);
expect(() =>
createRegistryFromEnv({
env: {
// Garbage budget spec
LLM_PROVIDER_FAST: "test|test-model|garbage_budget_spec",
LLM_TASK_ROUTE_TRIAGE: "fast",
},
adapters: { test: registration() },
}),
).toThrow(ConfigError);
});
it("NoProvidersAvailableError reasons map names every attempted alias (budget-exhaustion path)", async () => {
// Use budget gating (req:0/hour means immediately unavailable) to trigger
// selectModel's NoProvidersAvailableError. The reasons map should include
// an entry per chain alias, each with the budget-exhaustion message.
const registry = createRegistryFromEnv({
env: {
LLM_PROVIDER_A: "test|test-model|req:0/hour",
LLM_PROVIDER_B: "test|test-model|req:0/hour",
LLM_TASK_ROUTE_TRIAGE: "a,b",
},
adapters: { test: registration() },
});
const llm = registry.getPort();
let caught: unknown;
try {
await llm.generateText({ taskType: "triage", messages: [{ role: "user" as const, content: "x" }] });
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(NoProvidersAvailableError);
const e = caught as NoProvidersAvailableError;
expect(e.attempted).toEqual(["a", "b"]);
expect(e.reasons["a"]).toBeDefined();
expect(e.reasons["b"]).toBeDefined();
expect(e.taskType).toBe("triage");
});
});