|
| 1 | +import { |
| 2 | + createAssistantMessageEventStream, |
| 3 | + type FauxModelDefinition, |
| 4 | + fauxAssistantMessage, |
| 5 | + registerFauxProvider, |
| 6 | +} from "@earendil-works/pi-ai"; |
| 7 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 8 | +import { AuthStorage } from "../../src/core/auth-storage.ts"; |
| 9 | +import { |
| 10 | + createSpeculativeCompactionSnapshot, |
| 11 | + runExtensionCompaction, |
| 12 | + type SpeculativeCompactionContext, |
| 13 | +} from "../../src/core/extensions/builtin/compaction/speculative.ts"; |
| 14 | +import { ModelRegistry } from "../../src/core/model-registry.ts"; |
| 15 | +import { SessionManager } from "../../src/core/session-manager.ts"; |
| 16 | + |
| 17 | +const registrations: Array<{ unregister: () => void }> = []; |
| 18 | + |
| 19 | +type Registration = ReturnType<typeof registerFauxProvider>; |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + vi.restoreAllMocks(); |
| 23 | + for (const registration of registrations.splice(0)) { |
| 24 | + registration.unregister(); |
| 25 | + } |
| 26 | +}); |
| 27 | + |
| 28 | +function createContext(registration: Registration): SpeculativeCompactionContext { |
| 29 | + const model = registration.getModel(); |
| 30 | + const authStorage = AuthStorage.inMemory(); |
| 31 | + authStorage.setRuntimeApiKey(model.provider, "faux-key"); |
| 32 | + const modelRegistry = ModelRegistry.inMemory(authStorage); |
| 33 | + modelRegistry.registerProvider(model.provider, { |
| 34 | + baseUrl: model.baseUrl, |
| 35 | + apiKey: "faux-key", |
| 36 | + api: registration.api, |
| 37 | + models: registration.models.map((registeredModel) => ({ |
| 38 | + id: registeredModel.id, |
| 39 | + name: registeredModel.name, |
| 40 | + api: registeredModel.api, |
| 41 | + reasoning: registeredModel.reasoning, |
| 42 | + input: registeredModel.input, |
| 43 | + cost: registeredModel.cost, |
| 44 | + contextWindow: registeredModel.contextWindow, |
| 45 | + maxTokens: registeredModel.maxTokens, |
| 46 | + baseUrl: registeredModel.baseUrl, |
| 47 | + })), |
| 48 | + }); |
| 49 | + const sessionManager = SessionManager.inMemory(); |
| 50 | + sessionManager.appendMessage({ |
| 51 | + role: "user", |
| 52 | + content: [{ type: "text", text: "first user ".repeat(12_000) }], |
| 53 | + timestamp: Date.now() - 3_000, |
| 54 | + }); |
| 55 | + sessionManager.appendMessage({ |
| 56 | + ...fauxAssistantMessage("first assistant ".repeat(12_000), { timestamp: Date.now() - 2_000 }), |
| 57 | + api: model.api, |
| 58 | + provider: model.provider, |
| 59 | + model: model.id, |
| 60 | + usage: { |
| 61 | + input: 50_000, |
| 62 | + output: 0, |
| 63 | + cacheRead: 0, |
| 64 | + cacheWrite: 0, |
| 65 | + totalTokens: 50_000, |
| 66 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, |
| 67 | + }, |
| 68 | + }); |
| 69 | + sessionManager.appendMessage({ |
| 70 | + role: "user", |
| 71 | + content: [{ type: "text", text: "second user ".repeat(12_000) }], |
| 72 | + timestamp: Date.now() - 1_000, |
| 73 | + }); |
| 74 | + |
| 75 | + return { |
| 76 | + model, |
| 77 | + modelRegistry, |
| 78 | + sessionManager, |
| 79 | + getContextUsage: () => ({ tokens: 50_000, contextWindow: model.contextWindow, percent: 25 }), |
| 80 | + getMessageRevision: () => 1, |
| 81 | + applyCompaction: async () => ({ applied: true, reason: "ok" }), |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +function createPendingSummary() { |
| 86 | + const model: FauxModelDefinition = { |
| 87 | + id: "faux-compaction-abort", |
| 88 | + reasoning: false, |
| 89 | + contextWindow: 128_000, |
| 90 | + maxTokens: 16_384, |
| 91 | + }; |
| 92 | + const registration = registerFauxProvider({ models: [model] }); |
| 93 | + registrations.push(registration); |
| 94 | + const context = createContext(registration); |
| 95 | + const snapshot = createSpeculativeCompactionSnapshot(context, { generation: 1 }); |
| 96 | + if (!snapshot) throw new Error("expected a compaction snapshot"); |
| 97 | + |
| 98 | + const stream = createAssistantMessageEventStream(); |
| 99 | + const started = Promise.withResolvers<void>(); |
| 100 | + vi.spyOn(context.modelRegistry!.modelRuntime, "stream").mockImplementation(() => { |
| 101 | + started.resolve(); |
| 102 | + return stream; |
| 103 | + }); |
| 104 | + |
| 105 | + return { context, snapshot, started: started.promise, stream }; |
| 106 | +} |
| 107 | + |
| 108 | +describe("speculative compaction stream cancellation", () => { |
| 109 | + it("treats a late stream rejection after caller abort as cancellation", async () => { |
| 110 | + const { context, snapshot, started, stream } = createPendingSummary(); |
| 111 | + const controller = new AbortController(); |
| 112 | + |
| 113 | + const result = runExtensionCompaction(context, snapshot, controller.signal); |
| 114 | + await started; |
| 115 | + controller.abort(); |
| 116 | + stream.fail(new Error("Assistant message stream consumption was cancelled")); |
| 117 | + |
| 118 | + await expect(result).resolves.toBeUndefined(); |
| 119 | + }); |
| 120 | + |
| 121 | + it("still surfaces a stream rejection when the caller did not abort", async () => { |
| 122 | + const { context, snapshot, started, stream } = createPendingSummary(); |
| 123 | + const failure = new Error("provider stream failed"); |
| 124 | + |
| 125 | + const result = runExtensionCompaction(context, snapshot); |
| 126 | + await started; |
| 127 | + stream.fail(failure); |
| 128 | + |
| 129 | + await expect(result).rejects.toBe(failure); |
| 130 | + }); |
| 131 | +}); |
0 commit comments