|
| 1 | +import { describe, expect } from "bun:test" |
| 2 | +import { Effect, Layer, Schema } from "effect" |
| 3 | +import type { ToolExecutionOptions } from "ai" |
| 4 | +import type { Agent } from "@/agent/agent" |
| 5 | +import { MCP } from "@/mcp" |
| 6 | +import { Permission } from "@/permission" |
| 7 | +import { Plugin } from "@/plugin" |
| 8 | +import type { Provider } from "@/provider/provider" |
| 9 | +import { ToolRegistry } from "@/tool/registry" |
| 10 | +import { Truncate } from "@/tool/truncate" |
| 11 | +import type { Tool } from "@/tool/tool" |
| 12 | +import { PermissionV1 } from "@opencode-ai/core/v1/permission" |
| 13 | +import { Session } from "@/session/session" |
| 14 | +import type { SessionProcessor } from "@/session/processor" |
| 15 | +import type { SessionV1 } from "@opencode-ai/core/v1/session" |
| 16 | +import type { TaskPromptOps } from "@/tool/task" |
| 17 | +import { SessionTools } from "@/session/tools" |
| 18 | +import { SubagentLimits } from "@/session/subagent-limits" |
| 19 | +import { McpLazyActivation } from "@/session/mcp-lazy" |
| 20 | +import { MessageID, SessionID } from "@/session/schema" |
| 21 | +import { testEffect } from "../lib/effect" |
| 22 | + |
| 23 | +// Design-final §4.3 (Ü3): permission asks of nested subagents are routed to |
| 24 | +// the root session (permissionSessionID). Without attribution, UIs and |
| 25 | +// third-party clients lose WHO asked — so the ctx.ask wiring in |
| 26 | +// SessionTools.resolve attaches origin metadata (originSessionID / |
| 27 | +// originAgent / originDepth) whenever the ask is routed away from the asking |
| 28 | +// session. These tests pin EXACTLY that wiring; the real lineage walk is |
| 29 | +// covered by session-lineage.test.ts, so Session.Service is a stub here. |
| 30 | +const it = testEffect(Layer.mergeAll(McpLazyActivation.defaultLayer)) |
| 31 | + |
| 32 | +const ROOT = SessionID.make("ses_origin_root") |
| 33 | +const MID = SessionID.make("ses_origin_mid") |
| 34 | +const LEAF = SessionID.make("ses_origin_leaf") |
| 35 | + |
| 36 | +// A registry tool whose execute immediately asks — the seam under test. |
| 37 | +const probeTool: Tool.Def = { |
| 38 | + id: "origin_probe", |
| 39 | + description: "asks for permission and returns", |
| 40 | + parameters: Schema.Struct({}), |
| 41 | + jsonSchema: { type: "object", properties: {} }, |
| 42 | + execute: (_args, ctx) => |
| 43 | + Effect.gen(function* () { |
| 44 | + yield* ctx.ask({ permission: "origin_probe", patterns: ["p"], always: [], metadata: { base: "keep" } }) |
| 45 | + return { title: "probe", metadata: {}, output: "ok" } |
| 46 | + }), |
| 47 | +} |
| 48 | + |
| 49 | +const registryStub = ToolRegistry.Service.of({ |
| 50 | + tools: () => Effect.succeed([probeTool]), |
| 51 | +} as unknown as ToolRegistry.Interface) |
| 52 | + |
| 53 | +const pluginStub = Plugin.Service.of({ |
| 54 | + trigger: (() => Effect.succeed({})) as unknown as Plugin.Interface["trigger"], |
| 55 | +} as unknown as Plugin.Interface) |
| 56 | + |
| 57 | +const mcpStub = MCP.Service.of({ |
| 58 | + tools: () => Effect.succeed({}), |
| 59 | +} as unknown as MCP.Interface) |
| 60 | + |
| 61 | +const truncateStub = Truncate.Service.of({ |
| 62 | + cleanup: () => Effect.void, |
| 63 | + write: () => Effect.succeed("/tmp/unused"), |
| 64 | + output: (text: string) => Effect.succeed({ content: text, truncated: false as const }), |
| 65 | + limits: () => Effect.succeed({ maxLines: 2000, maxBytes: 51200 }), |
| 66 | +} as unknown as Truncate.Interface) |
| 67 | + |
| 68 | +function capturePermission() { |
| 69 | + const asks: PermissionV1.AskInput[] = [] |
| 70 | + const stub = Permission.Service.of({ |
| 71 | + ask: (input: PermissionV1.AskInput) => Effect.sync(() => void asks.push(input)), |
| 72 | + reply: () => Effect.void, |
| 73 | + list: () => Effect.succeed([]), |
| 74 | + } as unknown as Permission.Interface) |
| 75 | + return { asks, stub } |
| 76 | +} |
| 77 | + |
| 78 | +/** Session.Service stub: only `lineage` is consumed by the wiring under test. */ |
| 79 | +const sessionStub = (lineage?: Session.Interface["lineage"]) => |
| 80 | + Session.Service.of({ |
| 81 | + lineage: |
| 82 | + lineage ?? |
| 83 | + (() => Effect.die(new Error("lineage must not be walked for this resolve"))), |
| 84 | + } as unknown as Session.Interface) |
| 85 | + |
| 86 | +/** A lineage chain as `Session.lineage` returns it: self → root. */ |
| 87 | +const chainOf = (...ids: SessionID[]) => ids.map((id) => ({ id }) as Session.Info) |
| 88 | + |
| 89 | +const fakeModel = { providerID: "test", api: { id: "test-model" } } as Provider.Model |
| 90 | + |
| 91 | +function resolveInput(session: { id: SessionID; parentID?: SessionID }, permissionSessionID?: SessionID) { |
| 92 | + return { |
| 93 | + agent: { name: "build", mode: "primary", permission: [], options: {} } as Agent.Info, |
| 94 | + model: fakeModel, |
| 95 | + session: session as Session.Info, |
| 96 | + permissionSessionID, |
| 97 | + processor: { |
| 98 | + message: { id: MessageID.make("msg_origin_test") } as SessionV1.Assistant, |
| 99 | + updateToolCall: () => Effect.succeed(undefined), |
| 100 | + completeToolCall: () => Effect.void, |
| 101 | + } as unknown as Pick<SessionProcessor.Handle, "message" | "updateToolCall" | "completeToolCall">, |
| 102 | + bypassAgentCheck: false, |
| 103 | + messages: [] as SessionV1.WithParts[], |
| 104 | + promptOps: {} as TaskPromptOps, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +const provideStubs = |
| 109 | + (permission: Permission.Service["Service"], session: Session.Service["Service"]) => |
| 110 | + <A, E, R>(self: Effect.Effect<A, E, R>) => |
| 111 | + self.pipe( |
| 112 | + Effect.provideService(Plugin.Service, pluginStub), |
| 113 | + Effect.provideService(Permission.Service, permission), |
| 114 | + Effect.provideService(ToolRegistry.Service, registryStub), |
| 115 | + Effect.provideService(MCP.Service, mcpStub), |
| 116 | + Effect.provideService(Truncate.Service, truncateStub), |
| 117 | + Effect.provideService(Session.Service, session), |
| 118 | + ) |
| 119 | + |
| 120 | +const callOptions = { toolCallId: "call_origin_test", messages: [] } as unknown as ToolExecutionOptions |
| 121 | + |
| 122 | +const runProbe = ( |
| 123 | + input: ReturnType<typeof resolveInput>, |
| 124 | + permission: Permission.Service["Service"], |
| 125 | + session: Session.Service["Service"], |
| 126 | +) => |
| 127 | + Effect.gen(function* () { |
| 128 | + const tools = yield* SessionTools.resolve(input) |
| 129 | + expect(tools["origin_probe"]).toBeDefined() |
| 130 | + yield* Effect.promise(() => Promise.resolve(tools["origin_probe"].execute!({}, callOptions))) |
| 131 | + }).pipe(provideStubs(permission, session)) |
| 132 | + |
| 133 | +describe("SessionTools.resolve ask origin metadata", () => { |
| 134 | + it.instance("routed ask from depth 3 carries originSessionID/originAgent/originDepth", () => |
| 135 | + Effect.gen(function* () { |
| 136 | + const { asks, stub } = capturePermission() |
| 137 | + const session = sessionStub(() => Effect.succeed(chainOf(LEAF, MID, ROOT))) |
| 138 | + yield* runProbe(resolveInput({ id: LEAF, parentID: MID }, ROOT), stub, session) |
| 139 | + expect(asks).toHaveLength(1) |
| 140 | + const ask = asks[0]! |
| 141 | + expect(ask.sessionID).toBe(ROOT) |
| 142 | + // base request metadata is preserved, origin fields are added |
| 143 | + expect(ask.metadata.base).toBe("keep") |
| 144 | + expect(ask.metadata.originSessionID).toBe(LEAF) |
| 145 | + expect(ask.metadata.originAgent).toBe("build") |
| 146 | + expect(ask.metadata.originDepth).toBe(3) |
| 147 | + }), |
| 148 | + ) |
| 149 | + |
| 150 | + it.instance("unrouted ask (no permissionSessionID) stays byte-identical: no origin fields, no walk", () => |
| 151 | + Effect.gen(function* () { |
| 152 | + const { asks, stub } = capturePermission() |
| 153 | + yield* runProbe(resolveInput({ id: LEAF, parentID: MID }), stub, sessionStub()) |
| 154 | + expect(asks).toHaveLength(1) |
| 155 | + const ask = asks[0]! |
| 156 | + expect(ask.sessionID).toBe(LEAF) |
| 157 | + expect(ask.metadata).toEqual({ base: "keep" }) |
| 158 | + }), |
| 159 | + ) |
| 160 | + |
| 161 | + it.instance("permissionSessionID equal to the session id adds no origin fields", () => |
| 162 | + Effect.gen(function* () { |
| 163 | + const { asks, stub } = capturePermission() |
| 164 | + yield* runProbe(resolveInput({ id: LEAF, parentID: MID }, LEAF), stub, sessionStub()) |
| 165 | + expect(asks).toHaveLength(1) |
| 166 | + expect(asks[0]!.sessionID).toBe(LEAF) |
| 167 | + expect(asks[0]!.metadata).toEqual({ base: "keep" }) |
| 168 | + }), |
| 169 | + ) |
| 170 | + |
| 171 | + it.instance("routed ask from a root session reports depth 1 without a lineage walk", () => |
| 172 | + Effect.gen(function* () { |
| 173 | + const { asks, stub } = capturePermission() |
| 174 | + // parentID undefined ⇒ depth 1 by definition; the stub dies if walked. |
| 175 | + yield* runProbe(resolveInput({ id: LEAF }, ROOT), stub, sessionStub()) |
| 176 | + expect(asks).toHaveLength(1) |
| 177 | + expect(asks[0]!.metadata.originSessionID).toBe(LEAF) |
| 178 | + expect(asks[0]!.metadata.originDepth).toBe(1) |
| 179 | + }), |
| 180 | + ) |
| 181 | + |
| 182 | + it.instance("a failed lineage walk omits originDepth but never fails the ask", () => |
| 183 | + Effect.gen(function* () { |
| 184 | + const { asks, stub } = capturePermission() |
| 185 | + const session = sessionStub(() => Effect.fail(SubagentLimits.lineageError({ sessionID: LEAF }))) |
| 186 | + yield* runProbe(resolveInput({ id: LEAF, parentID: MID }, ROOT), stub, session) |
| 187 | + expect(asks).toHaveLength(1) |
| 188 | + const ask = asks[0]! |
| 189 | + expect(ask.sessionID).toBe(ROOT) |
| 190 | + expect(ask.metadata.originSessionID).toBe(LEAF) |
| 191 | + expect(ask.metadata.originAgent).toBe("build") |
| 192 | + expect("originDepth" in ask.metadata).toBe(false) |
| 193 | + }), |
| 194 | + ) |
| 195 | +}) |
0 commit comments