|
| 1 | +import { describe, expect } from "bun:test" |
| 2 | +import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" |
| 3 | +import { LayerNode } from "@opencode-ai/core/effect/layer-node" |
| 4 | +import { SessionV1 } from "@opencode-ai/core/v1/session" |
| 5 | +import { Effect, Layer, Option } from "effect" |
| 6 | +import { Memory } from "@/memory/memory" |
| 7 | +import { Session } from "@/session/session" |
| 8 | +import { SessionPrompt } from "@/session/prompt" |
| 9 | +import { HttpApiApp } from "@/server/routes/instance/httpapi/server" |
| 10 | +import { testEffect } from "../lib/effect" |
| 11 | + |
| 12 | +// Issue #311 regression: /memory answered "Memory remains off" and |
| 13 | +// memory_search answered "unavailable for this session" in live TUI sessions |
| 14 | +// because Memory.node was missing from the server app group. LayerNode nodes |
| 15 | +// built via Layer.provide do not re-export their dependency services, so |
| 16 | +// listing Memory.node only in per-consumer dependency arrays (SessionPrompt, |
| 17 | +// SystemPrompt, Compaction, bootstrap) never surfaced Memory.Service in the |
| 18 | +// request-time ambient context. These tests build the exact node graph the |
| 19 | +// server provides to route handlers and assert the service is present there. |
| 20 | + |
| 21 | +const appLayer = LayerNode.buildLayer(HttpApiApp.app) |
| 22 | + |
| 23 | +const appIt = testEffect(Layer.mergeAll(appLayer, CrossSpawnSpawner.defaultLayer)) |
| 24 | + |
| 25 | +const cfg = { |
| 26 | + provider: { |
| 27 | + test: { |
| 28 | + name: "Test", |
| 29 | + id: "test", |
| 30 | + env: [], |
| 31 | + npm: "@ai-sdk/openai-compatible", |
| 32 | + models: { |
| 33 | + "test-model": { |
| 34 | + id: "test-model", |
| 35 | + name: "Test Model", |
| 36 | + attachment: false, |
| 37 | + reasoning: false, |
| 38 | + temperature: false, |
| 39 | + tool_call: true, |
| 40 | + release_date: "2025-01-01", |
| 41 | + limit: { context: 100000, output: 10000 }, |
| 42 | + cost: { input: 0, output: 0 }, |
| 43 | + options: {}, |
| 44 | + }, |
| 45 | + }, |
| 46 | + options: { |
| 47 | + apiKey: "test-key", |
| 48 | + baseURL: "http://localhost:1/v1", |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | +} |
| 53 | + |
| 54 | +describe("server app graph memory wiring", () => { |
| 55 | + appIt.instance( |
| 56 | + "exposes Memory.Service in the ambient context live sessions run in", |
| 57 | + () => |
| 58 | + Effect.gen(function* () { |
| 59 | + const memory = yield* Effect.serviceOption(Memory.Service) |
| 60 | + expect(Option.isSome(memory)).toBe(true) |
| 61 | + }), |
| 62 | + { git: true }, |
| 63 | + ) |
| 64 | + |
| 65 | + const setEnabledCalls: boolean[] = [] |
| 66 | + // Same node graph, with Memory.node swapped for a recorder: proves the |
| 67 | + // /memory command branch resolves Memory.Service from the app-graph output |
| 68 | + // (not from a per-consumer dependency scope) and reaches setEnabled. |
| 69 | + const spyIt = testEffect( |
| 70 | + Layer.mergeAll( |
| 71 | + LayerNode.buildLayer(HttpApiApp.app, { |
| 72 | + replacements: [ |
| 73 | + LayerNode.replace( |
| 74 | + Memory.node, |
| 75 | + Layer.mock(Memory.Service, { |
| 76 | + setEnabled: (enabled) => |
| 77 | + Effect.sync(() => { |
| 78 | + setEnabledCalls.push(enabled) |
| 79 | + return enabled ? ("Memory on" as const) : ("Memory off" as const) |
| 80 | + }), |
| 81 | + }), |
| 82 | + ), |
| 83 | + ], |
| 84 | + }), |
| 85 | + CrossSpawnSpawner.defaultLayer, |
| 86 | + ), |
| 87 | + ) |
| 88 | + |
| 89 | + spyIt.instance( |
| 90 | + "routes the /memory command through the app graph to Memory.setEnabled", |
| 91 | + () => |
| 92 | + Effect.gen(function* () { |
| 93 | + setEnabledCalls.length = 0 |
| 94 | + const sessions = yield* Session.Service |
| 95 | + const chat = yield* sessions.create({ title: "memory wiring" }) |
| 96 | + const prompt = yield* SessionPrompt.Service |
| 97 | + |
| 98 | + const result = yield* prompt.command({ sessionID: chat.id, command: "memory", arguments: "on" }) |
| 99 | + |
| 100 | + const texts = result.parts |
| 101 | + .filter((part): part is SessionV1.TextPart => part.type === "text") |
| 102 | + .map((part) => part.text) |
| 103 | + expect(texts).toEqual(["/memory on", "Memory on"]) |
| 104 | + expect(setEnabledCalls).toEqual([true]) |
| 105 | + }), |
| 106 | + { git: true, config: cfg }, |
| 107 | + ) |
| 108 | +}) |
0 commit comments