Skip to content

Commit 458271b

Browse files
fix(memory): skip extraction when provider allowance is exhausted
1 parent 1cca06e commit 458271b

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎apps/server/src/textGeneration/TextGeneration.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,43 @@ it.effect(
187187
expect(test.getInstance.mock.calls).toEqual([[selected]]);
188188
}),
189189
);
190+
191+
it.effect.each([undefined, "2999-01-01T00:00:00.000Z", "1970-01-01T00:00:00.000Z"])(
192+
"avoids automatic memory calls until an exhausted allowance resets (%s)",
193+
(reset) =>
194+
Effect.gen(function* () {
195+
const generateKnowledgeCandidates = vi.fn(() => Effect.succeed({ candidates: [] }));
196+
const test = make(
197+
{
198+
...fromRunner("Fixture", () => Effect.die("Unexpected generation operation")),
199+
supportedOperations: ["generateKnowledgeCandidates"],
200+
generateKnowledgeCandidates,
201+
},
202+
{
203+
...providerSnapshot,
204+
usageLimits: {
205+
checkedAt: "1970-01-01T00:00:00.000Z",
206+
windows: [
207+
{
208+
id: "session",
209+
kind: "session",
210+
label: "Session",
211+
usedPercent: 100,
212+
resetsAt: reset,
213+
},
214+
],
215+
},
216+
},
217+
);
218+
const service = yield* test.service;
219+
const result = yield* service
220+
.generateKnowledgeCandidates({ ...input, runOutcome: "completed" })
221+
.pipe(Effect.result);
222+
if (reset === "1970-01-01T00:00:00.000Z") {
223+
expect(generateKnowledgeCandidates).toHaveBeenCalledOnce();
224+
} else {
225+
expect(result._tag).toBe("Failure");
226+
expect(generateKnowledgeCandidates).not.toHaveBeenCalled();
227+
}
228+
}),
229+
);

‎apps/server/src/textGeneration/TextGeneration.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const resolveInstance = Effect.fn("TextGeneration.resolveInstance")(function* (
5353
registry: ProviderInstanceRegistry.ProviderInstanceRegistry["Service"],
5454
operation: TextGenerationOperation,
5555
instanceId: ProviderInstanceId,
56+
model?: string,
5657
): Effect.fn.Return<ProviderInstance["textGeneration"], TextGenerationError> {
5758
const instance = yield* registry.getInstance(instanceId);
5859
if (!instance) {
@@ -67,6 +68,16 @@ const resolveInstance = Effect.fn("TextGeneration.resolveInstance")(function* (
6768
detail: `Provider '${instanceId}' does not support this text generation operation. Choose a model from a provider that supports it.`,
6869
});
6970
}
71+
if (operation === "generateKnowledgeCandidates") {
72+
const snapshot = yield* instance.snapshot.getSnapshot;
73+
if (exhaustedUsageWindow(snapshot, DateTime.toEpochMillis(yield* DateTime.now), model)) {
74+
return yield* new TextGenerationError({
75+
operation,
76+
detail:
77+
"Automatic memory skipped because the selected provider's usage allowance is exhausted.",
78+
});
79+
}
80+
}
7081
if (operation === "generateModelEvaluation") {
7182
const snapshot = yield* instance.snapshot.getSnapshot;
7283
if (
@@ -185,6 +196,7 @@ export const make = Effect.gen(function* () {
185196
registry,
186197
"generateKnowledgeCandidates",
187198
input.modelSelection.instanceId,
199+
input.modelSelection.model,
188200
).pipe(
189201
Effect.flatMap((textGeneration) => textGeneration.generateKnowledgeCandidates(input)),
190202
);

0 commit comments

Comments
 (0)