session-generate.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import { expect } from "bun:test"
  2. import {
  3. LLMClient,
  4. LLMEvent,
  5. LLMResponse,
  6. LanguageModel,
  7. SystemPart,
  8. ToolDefinition,
  9. type LLMRequest,
  10. } from "@opencode-ai/ai"
  11. import { OpenAIChat } from "@opencode-ai/ai/protocols"
  12. import { Agent } from "@opencode-ai/core/agent"
  13. import { Database } from "@opencode-ai/core/database/database"
  14. import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
  15. import { llmClient } from "@opencode-ai/core/effect/app-node-platform"
  16. import { LayerNode } from "@opencode-ai/util/effect/layer-node"
  17. import { Bus } from "@opencode-ai/core/bus"
  18. import { EventTable } from "@opencode-ai/core/event/sql"
  19. import { InstructionDiscovery } from "@opencode-ai/core/instruction-discovery"
  20. import { Instructions } from "@opencode-ai/core/instructions"
  21. import { InstructionBuiltIns } from "@opencode-ai/core/instructions/builtins"
  22. import { Location } from "@opencode-ai/core/location"
  23. import { McpInstructions } from "@opencode-ai/core/mcp/instructions"
  24. import { ID } from "@opencode-ai/core/model"
  25. import { Project } from "@opencode-ai/core/project"
  26. import { Provider } from "@opencode-ai/core/provider"
  27. import { ReferenceInstructions } from "@opencode-ai/core/reference/instructions"
  28. import { AbsolutePath } from "@opencode-ai/core/schema"
  29. import { SessionEvent } from "@opencode-ai/core/session/event"
  30. import { SessionGenerate } from "@opencode-ai/core/session/generate"
  31. import { SessionGenerateNode } from "@opencode-ai/core/session/generate-node"
  32. import { InstructionState } from "@opencode-ai/core/session/instruction-state"
  33. import { SessionMessage } from "@opencode-ai/core/session/message"
  34. import { SessionProjector } from "@opencode-ai/core/session/projector"
  35. import { SessionRunnerModel } from "@opencode-ai/core/session/runner/model"
  36. import { SessionSchema } from "@opencode-ai/core/session/schema"
  37. import {
  38. InstructionBlobTable,
  39. InstructionStateTable,
  40. SessionMessageTable,
  41. SessionPendingTable,
  42. SessionTable,
  43. } from "@opencode-ai/core/session/sql"
  44. import { SessionStore } from "@opencode-ai/core/session/store"
  45. import { SkillInstructions } from "@opencode-ai/core/skill/instructions"
  46. import { PluginHooks } from "@opencode-ai/core/plugin/hooks"
  47. import { PluginSupervisor } from "@opencode-ai/core/plugin/supervisor"
  48. import { Tool } from "@opencode-ai/core/tool"
  49. import { asc, eq } from "drizzle-orm"
  50. import { Effect, Layer, Schema, Stream } from "effect"
  51. import { testEffect } from "./lib/effect"
  52. const requests: LLMRequest[] = []
  53. let instruction: string | Instructions.Unavailable = "Initial context"
  54. const sessionID = SessionSchema.ID.make("ses_generate_test")
  55. const model = LanguageModel.make({ id: "generate-model", provider: "test", route: OpenAIChat.route })
  56. const client = Layer.mock(LLMClient.Service)({
  57. stream: () => Stream.die(new Error("unused")),
  58. generate: (request) =>
  59. Effect.sync(() => {
  60. requests.push(request)
  61. const response = LLMResponse.fromEvents([
  62. LLMEvent.stepStart({ index: 0 }),
  63. LLMEvent.textStart({ id: "generate" }),
  64. LLMEvent.textDelta({ id: "generate", text: "Transient answer" }),
  65. LLMEvent.textEnd({ id: "generate" }),
  66. LLMEvent.stepFinish({
  67. index: 0,
  68. reason: { normalized: "stop" },
  69. usage: { inputTokens: 100, outputTokens: 10 },
  70. }),
  71. LLMEvent.finish({ reason: { normalized: "stop" } }),
  72. ])
  73. if (!response) throw new Error("Incomplete generate response")
  74. return response
  75. }),
  76. })
  77. const models = Layer.mock(SessionRunnerModel.Service)({
  78. resolve: () =>
  79. Effect.succeed(
  80. SessionRunnerModel.resolved(model, {
  81. capabilities: { tools: true, input: ["text", "image"], output: ["text"] },
  82. cost: [],
  83. }),
  84. ),
  85. })
  86. const builtins = Layer.mock(InstructionBuiltIns.Service, {
  87. load: () =>
  88. Effect.succeed(
  89. Instructions.make({
  90. key: Instructions.Key.make("test/context"),
  91. codec: Schema.toCodecJson(Schema.String),
  92. read: Effect.sync(() => instruction),
  93. render: {
  94. initial: String,
  95. changed: (_previous, current) => current,
  96. },
  97. }),
  98. ),
  99. })
  100. const discovery = Layer.mock(InstructionDiscovery.Service, { load: () => Effect.succeed(Instructions.empty) })
  101. const skills = Layer.mock(SkillInstructions.Service, { load: () => Effect.succeed(Instructions.empty) })
  102. const references = Layer.mock(ReferenceInstructions.Service, { load: () => Effect.succeed(Instructions.empty) })
  103. const mcp = Layer.mock(McpInstructions.Service, { load: () => Effect.succeed(Instructions.empty) })
  104. const plugins = Layer.mock(PluginSupervisor.Service, { flush: Effect.void })
  105. const tools = Layer.mock(Tool.Service, {
  106. snapshot: () =>
  107. Effect.succeed({
  108. codeModeCatalog: [
  109. {
  110. path: "captured.lookup",
  111. description: "Captured Code Mode catalog",
  112. signature: "tools.captured.lookup(input: {}): Promise<string>",
  113. },
  114. ],
  115. definitions: [ToolDefinition.make({ name: "lookup", description: "Lookup", inputSchema: { type: "object" } })],
  116. execute: () => Effect.die(new Error("unused")),
  117. }),
  118. transform: () => Effect.die(new Error("unused")),
  119. })
  120. const it = testEffect(
  121. AppNodeBuilder.build(
  122. LayerNode.group([
  123. Database.node,
  124. Bus.node,
  125. SessionProjector.node,
  126. SessionStore.node,
  127. Agent.node,
  128. InstructionBuiltIns.node,
  129. PluginHooks.node,
  130. SessionGenerateNode.node,
  131. ]),
  132. [
  133. [llmClient, client],
  134. [SessionRunnerModel.node, models],
  135. [InstructionBuiltIns.node, builtins],
  136. [InstructionDiscovery.node, discovery],
  137. [SkillInstructions.node, skills],
  138. [ReferenceInstructions.node, references],
  139. [McpInstructions.node, mcp],
  140. [PluginSupervisor.node, plugins],
  141. [Tool.node, tools],
  142. [Location.node, Location.boundNode({ directory: AbsolutePath.make("/project") })],
  143. ],
  144. ),
  145. )
  146. const durableState = (db: Database.Interface["db"], sessionID: SessionSchema.ID) =>
  147. Effect.all({
  148. sequence: Bus.latestSequence(db, sessionID),
  149. bus: db
  150. .select()
  151. .from(EventTable)
  152. .where(eq(EventTable.aggregate_id, sessionID))
  153. .orderBy(asc(EventTable.seq))
  154. .all()
  155. .pipe(Effect.orDie),
  156. messages: db
  157. .select()
  158. .from(SessionMessageTable)
  159. .where(eq(SessionMessageTable.session_id, sessionID))
  160. .orderBy(asc(SessionMessageTable.seq))
  161. .all()
  162. .pipe(Effect.orDie),
  163. pending: db
  164. .select()
  165. .from(SessionPendingTable)
  166. .where(eq(SessionPendingTable.session_id, sessionID))
  167. .orderBy(asc(SessionPendingTable.admitted_seq))
  168. .all()
  169. .pipe(Effect.orDie),
  170. instructions: db
  171. .select()
  172. .from(InstructionStateTable)
  173. .where(eq(InstructionStateTable.session_id, sessionID))
  174. .get()
  175. .pipe(Effect.orDie),
  176. blobs: db.select().from(InstructionBlobTable).orderBy(asc(InstructionBlobTable.hash)).all().pipe(Effect.orDie),
  177. session: db.select().from(SessionTable).where(eq(SessionTable.id, sessionID)).get().pipe(Effect.orDie),
  178. })
  179. const userTexts = (request: LLMRequest) =>
  180. request.messages.flatMap((message) =>
  181. message.role === "user"
  182. ? message.content.flatMap((content) => (content.type === "text" ? [content.text] : []))
  183. : [],
  184. )
  185. const setup = Effect.gen(function* () {
  186. const { db } = yield* Database.Service
  187. const bus = yield* Bus.Service
  188. const agents = yield* Agent.Service
  189. const instructionBuiltIns = yield* InstructionBuiltIns.Service
  190. yield* agents.transform((draft) =>
  191. draft.update(Agent.ID.make("build"), (agent) => {
  192. agent.mode = "primary"
  193. }),
  194. )
  195. yield* db
  196. .insert(SessionTable)
  197. .values({
  198. id: sessionID,
  199. project_id: Project.ID.global,
  200. slug: "generate-test",
  201. directory: "/project",
  202. title: "Generate test",
  203. version: "test",
  204. agent: Agent.ID.make("build"),
  205. })
  206. .run()
  207. .pipe(Effect.orDie)
  208. return { db, bus, instructions: yield* instructionBuiltIns.load(sessionID) }
  209. })
  210. it.effect("generates from fresh settled Session context without durable mutation", () =>
  211. Effect.gen(function* () {
  212. requests.length = 0
  213. instruction = "Initial context"
  214. const { db, bus, instructions } = yield* setup
  215. yield* InstructionState.prepare(db, bus, instructions, sessionID)
  216. const existing = SessionMessage.ID.create()
  217. yield* bus.publish(SessionEvent.InputAdmitted, {
  218. sessionID,
  219. inputID: existing,
  220. input: { type: "user", data: { text: "Existing durable context" }, delivery: "steer" },
  221. })
  222. yield* bus.publish(SessionEvent.InputPromoted, { sessionID, inputID: existing })
  223. const settledAssistant = SessionMessage.ID.create()
  224. yield* bus.publish(SessionEvent.Step.Started, {
  225. sessionID,
  226. assistantMessageID: settledAssistant,
  227. agent: Agent.ID.make("build"),
  228. model: { id: ID.make("generate-model"), providerID: Provider.ID.make("test") },
  229. })
  230. yield* bus.publish(SessionEvent.Text.Started, {
  231. sessionID,
  232. assistantMessageID: settledAssistant,
  233. ordinal: 0,
  234. })
  235. yield* bus.publish(SessionEvent.Text.Ended, {
  236. sessionID,
  237. assistantMessageID: settledAssistant,
  238. ordinal: 0,
  239. text: "Settled partial answer",
  240. })
  241. const activeAssistant = SessionMessage.ID.create()
  242. yield* bus.publish(SessionEvent.Step.Started, {
  243. sessionID,
  244. assistantMessageID: activeAssistant,
  245. agent: Agent.ID.make("build"),
  246. model: { id: ID.make("generate-model"), providerID: Provider.ID.make("test") },
  247. })
  248. yield* bus.publish(SessionEvent.Tool.Input.Started, {
  249. sessionID,
  250. assistantMessageID: activeAssistant,
  251. callID: "active-call",
  252. name: "echo",
  253. })
  254. yield* bus.publish(SessionEvent.Tool.Input.Ended, {
  255. sessionID,
  256. assistantMessageID: activeAssistant,
  257. callID: "active-call",
  258. text: "{}",
  259. })
  260. yield* bus.publish(SessionEvent.Tool.Called, {
  261. sessionID,
  262. assistantMessageID: activeAssistant,
  263. callID: "active-call",
  264. input: {},
  265. executed: false,
  266. })
  267. yield* bus.publish(SessionEvent.InputAdmitted, {
  268. sessionID,
  269. inputID: SessionMessage.ID.create(),
  270. input: { type: "user", data: { text: "Queued input must remain invisible" }, delivery: "queue" },
  271. })
  272. instruction = "Changed context"
  273. const before = yield* durableState(db, sessionID)
  274. const hooks = yield* PluginHooks.Service
  275. yield* hooks.register("session", "context", (event) =>
  276. Effect.sync(() => {
  277. event.system = [SystemPart.make("Hooked system"), ...event.system]
  278. if (event.tools.lookup) event.tools.lookup.description = "Hooked lookup"
  279. }),
  280. )
  281. const generate = yield* SessionGenerate.Service
  282. const result = yield* generate.generate({ sessionID, prompt: "Summarize privately" })
  283. expect(result).toBe("Transient answer")
  284. expect(requests).toHaveLength(1)
  285. expect(requests[0]?.model).toBe(model)
  286. expect(requests[0]?.system[0]?.text).toBe("Hooked system")
  287. expect(requests[0]?.system.map((part) => part.text)).toContain("Initial context")
  288. expect(requests[0]?.http?.headers).toMatchObject({ "X-Session-Id": sessionID })
  289. expect(requests[0]?.providerOptions).toMatchObject({ openai: { promptCacheKey: sessionID } })
  290. const instructionUpdates = requests[0]?.messages.flatMap((message) =>
  291. message.role === "system"
  292. ? message.content.flatMap((content) => (content.type === "text" ? [content.text] : []))
  293. : [],
  294. )
  295. expect(instructionUpdates).toHaveLength(1)
  296. expect(instructionUpdates?.[0]).toContain("Changed context")
  297. expect(instructionUpdates?.[0]).toContain("tools.captured.lookup(input: {}): Promise<string>")
  298. expect(userTexts(requests[0])).toEqual(["Existing durable context", "Summarize privately"])
  299. expect(
  300. requests[0]?.messages.flatMap((message) =>
  301. message.role === "assistant"
  302. ? message.content.flatMap((content) => (content.type === "text" ? [content.text] : []))
  303. : [],
  304. ),
  305. ).toEqual(["Settled partial answer"])
  306. expect(requests[0]?.tools).toMatchObject([{ name: "lookup", description: "Hooked lookup" }])
  307. expect(requests[0]?.toolChoice).toBeUndefined()
  308. expect(yield* durableState(db, sessionID)).toEqual(before)
  309. }),
  310. )
  311. it.effect("blocks unavailable initial instructions before generation", () =>
  312. Effect.gen(function* () {
  313. requests.length = 0
  314. instruction = Instructions.unavailable
  315. const { db } = yield* setup
  316. const before = yield* durableState(db, sessionID)
  317. const generate = yield* SessionGenerate.Service
  318. const error = yield* generate.generate({ sessionID, prompt: "Summarize privately" }).pipe(Effect.flip)
  319. expect(error).toBeInstanceOf(Instructions.InitializationBlocked)
  320. expect(requests).toEqual([])
  321. expect(yield* durableState(db, sessionID)).toEqual(before)
  322. }),
  323. )