|
| 1 | +import { describe, expect, spyOn, test } from "bun:test" |
| 2 | +import { MockLanguageModelV3 } from "ai/test" |
| 3 | +import { jsonSchema, simulateReadableStream, tool } from "ai" |
| 4 | +import { Auth } from "../../src/auth" |
| 5 | +import { Config } from "../../src/config/config" |
| 6 | +import { Plugin } from "../../src/plugin" |
| 7 | +import { Instance } from "../../src/project/instance" |
| 8 | +import { ProjectID } from "../../src/project/schema" |
| 9 | +import { Provider } from "../../src/provider/provider" |
| 10 | +import { ModelID, ProviderID } from "../../src/provider/schema" |
| 11 | +import { LLM } from "../../src/session/llm" |
| 12 | +import { MessageID, SessionID } from "../../src/session/schema" |
| 13 | +import { SystemPrompt } from "../../src/session/system" |
| 14 | +import { tmpdir } from "../fixture/fixture" |
| 15 | + |
| 16 | +describe("session system cache prefix", () => { |
| 17 | + for (const providerID of ["anthropic", "amazon-bedrock"]) { |
| 18 | + for (const pluginInstructions of [false, true]) { |
| 19 | + test(`${providerID} keeps a marked prefix across directories with plugin instructions ${pluginInstructions}`, async () => { |
| 20 | + const model: Provider.Model = { |
| 21 | + id: ModelID.make("claude-test"), |
| 22 | + providerID: ProviderID.make(providerID), |
| 23 | + api: { |
| 24 | + id: "claude-test", |
| 25 | + url: "https://example.invalid", |
| 26 | + npm: providerID === "anthropic" ? "@ai-sdk/anthropic" : "@ai-sdk/amazon-bedrock", |
| 27 | + }, |
| 28 | + name: "Test model", |
| 29 | + capabilities: { |
| 30 | + temperature: false, |
| 31 | + reasoning: false, |
| 32 | + attachment: false, |
| 33 | + toolcall: true, |
| 34 | + input: { text: true, audio: false, image: false, video: false, pdf: false }, |
| 35 | + output: { text: true, audio: false, image: false, video: false, pdf: false }, |
| 36 | + interleaved: false, |
| 37 | + }, |
| 38 | + cost: { input: 0, output: 0, cache: { read: 0, write: 0 } }, |
| 39 | + limit: { context: 200_000, output: 4096 }, |
| 40 | + status: "active", |
| 41 | + options: {}, |
| 42 | + headers: {}, |
| 43 | + release_date: "2026-01-01", |
| 44 | + } |
| 45 | + const language = new MockLanguageModelV3({ |
| 46 | + provider: providerID, |
| 47 | + modelId: model.api.id, |
| 48 | + doStream: async () => ({ |
| 49 | + stream: simulateReadableStream({ |
| 50 | + chunks: [ |
| 51 | + { |
| 52 | + type: "finish" as const, |
| 53 | + finishReason: { unified: "stop" as const, raw: "stop" }, |
| 54 | + usage: { |
| 55 | + inputTokens: { total: 1, noCache: 1, cacheRead: 0, cacheWrite: 0 }, |
| 56 | + outputTokens: { total: 0, text: 0, reasoning: 0 }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + ], |
| 60 | + initialDelayInMs: null, |
| 61 | + chunkDelayInMs: null, |
| 62 | + }), |
| 63 | + }), |
| 64 | + }) |
| 65 | + const mocks = [ |
| 66 | + spyOn(Provider, "getLanguage").mockResolvedValue(language), |
| 67 | + spyOn(Provider, "getProvider").mockResolvedValue({ |
| 68 | + id: model.providerID, |
| 69 | + name: "Test provider", |
| 70 | + source: "config", |
| 71 | + env: [], |
| 72 | + options: {}, |
| 73 | + models: {}, |
| 74 | + }), |
| 75 | + spyOn(Config, "get").mockResolvedValue({}), |
| 76 | + spyOn(Auth, "get").mockResolvedValue(undefined), |
| 77 | + spyOn(Plugin, "trigger").mockImplementation(async (name, _input, output) => { |
| 78 | + if (name === "experimental.chat.system.transform" && pluginInstructions) { |
| 79 | + ;(output as { system: string[] }).system.push("Plugin rules.", "Additional plugin rules.") |
| 80 | + } |
| 81 | + return output |
| 82 | + }), |
| 83 | + ] |
| 84 | + try { |
| 85 | + await using first = await tmpdir() |
| 86 | + await using second = await tmpdir() |
| 87 | + const directories = [first.path, second.path] |
| 88 | + for (const [index, directory] of directories.entries()) { |
| 89 | + await Instance.restore( |
| 90 | + { |
| 91 | + directory, |
| 92 | + worktree: directory, |
| 93 | + project: { |
| 94 | + id: ProjectID.global, |
| 95 | + worktree: directory, |
| 96 | + time: { created: 0, updated: 0 }, |
| 97 | + sandboxes: [], |
| 98 | + }, |
| 99 | + }, |
| 100 | + async () => { |
| 101 | + const sessionID = SessionID.make(`ses_directory_${index}`) |
| 102 | + const result = await LLM.stream({ |
| 103 | + sessionID, |
| 104 | + model, |
| 105 | + agent: { name: "test", mode: "primary", options: {}, permission: [] }, |
| 106 | + user: { |
| 107 | + id: MessageID.make(`msg_directory_${index}`), |
| 108 | + sessionID, |
| 109 | + role: "user", |
| 110 | + agent: "test", |
| 111 | + model: { providerID: model.providerID, modelID: model.id }, |
| 112 | + time: { created: 0 }, |
| 113 | + system: "Custom instructions.", |
| 114 | + }, |
| 115 | + system: ["Project instructions.", "Skill instructions."], |
| 116 | + environment: await SystemPrompt.environment(model), |
| 117 | + messages: [{ role: "user", content: "Inspect the project." }], |
| 118 | + tools: { |
| 119 | + inspect: tool({ description: "Inspect the project", inputSchema: jsonSchema({ type: "object" }) }), |
| 120 | + }, |
| 121 | + abort: new AbortController().signal, |
| 122 | + }) |
| 123 | + await result.consumeStream({ |
| 124 | + onError: (error) => { |
| 125 | + throw error |
| 126 | + }, |
| 127 | + }) |
| 128 | + }, |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + expect(language.doStreamCalls).toHaveLength(2) |
| 133 | + const systems = language.doStreamCalls.map((call) => |
| 134 | + call.prompt.filter((message) => message.role === "system"), |
| 135 | + ) |
| 136 | + expect(systems[0]).toHaveLength(pluginInstructions ? 3 : 2) |
| 137 | + expect(JSON.stringify(systems[0].slice(0, -1))).toBe(JSON.stringify(systems[1].slice(0, -1))) |
| 138 | + expect(JSON.stringify(language.doStreamCalls[0].tools)).toBe(JSON.stringify(language.doStreamCalls[1].tools)) |
| 139 | + for (const [index, system] of systems.entries()) { |
| 140 | + const prefix = system.slice(0, -1) |
| 141 | + expect(prefix[0].content).toContain("Project instructions.\nSkill instructions.\nCustom instructions.") |
| 142 | + expect(prefix[0].providerOptions?.[providerID === "anthropic" ? "anthropic" : "bedrock"]).toEqual( |
| 143 | + providerID === "anthropic" |
| 144 | + ? { cacheControl: { type: "ephemeral" } } |
| 145 | + : { cachePoint: { type: "default" } }, |
| 146 | + ) |
| 147 | + if (pluginInstructions) expect(prefix[1].content).toBe("Plugin rules.\nAdditional plugin rules.") |
| 148 | + expect(JSON.stringify(prefix)).not.toContain(directories[index]) |
| 149 | + expect(JSON.stringify(prefix)).not.toContain("Today's date:") |
| 150 | + expect(system.at(-1)?.content).toContain(`Working directory: ${directories[index]}`) |
| 151 | + expect(system.at(-1)?.content).toContain(`Workspace root folder: ${directories[index]}`) |
| 152 | + expect(system.at(-1)?.content).toContain(`Today's date: ${new Date().toDateString()}`) |
| 153 | + const conversation = language.doStreamCalls[index].prompt.filter((message) => message.role !== "system") |
| 154 | + expect(JSON.stringify(conversation)).not.toContain("<env>") |
| 155 | + } |
| 156 | + } finally { |
| 157 | + for (const mock of mocks) mock.mockRestore() |
| 158 | + } |
| 159 | + }) |
| 160 | + } |
| 161 | + } |
| 162 | +}) |
0 commit comments