|
| 1 | +import { afterEach, describe, expect } from "bun:test" |
| 2 | +import { Effect, Exit, Layer } from "effect" |
| 3 | +import { Agent } from "../../src/agent/agent" |
| 4 | +import { Config } from "../../src/config" |
| 5 | +import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner" |
| 6 | +import { Instance } from "../../src/project/instance" |
| 7 | +import { Permission } from "../../src/permission" |
| 8 | +import { ModelID, ProviderID } from "../../src/provider/schema" |
| 9 | +import { Session } from "../../src/session" |
| 10 | +import { MessageV2 } from "../../src/session/message-v2" |
| 11 | +import { MessageID } from "../../src/session/schema" |
| 12 | +import { ToolRegistry, Truncate } from "../../src/tool" |
| 13 | +import { provideTmpdirInstance } from "../fixture/fixture" |
| 14 | +import { testEffect } from "../lib/effect" |
| 15 | + |
| 16 | +afterEach(async () => { |
| 17 | + await Instance.disposeAll() |
| 18 | +}) |
| 19 | + |
| 20 | +const ref = { |
| 21 | + providerID: ProviderID.make("test"), |
| 22 | + modelID: ModelID.make("test-model"), |
| 23 | +} |
| 24 | + |
| 25 | +const it = testEffect( |
| 26 | + Layer.mergeAll( |
| 27 | + Agent.defaultLayer, |
| 28 | + Config.defaultLayer, |
| 29 | + CrossSpawnSpawner.defaultLayer, |
| 30 | + Session.defaultLayer, |
| 31 | + Truncate.defaultLayer, |
| 32 | + ToolRegistry.defaultLayer, |
| 33 | + ), |
| 34 | +) |
| 35 | + |
| 36 | +const seed = Effect.fn("TaskAsyncToolTest.seed")(function* (title = "Pinned") { |
| 37 | + const session = yield* Session.Service |
| 38 | + const chat = yield* session.create({ title }) |
| 39 | + const user = yield* session.updateMessage({ |
| 40 | + id: MessageID.ascending(), |
| 41 | + role: "user", |
| 42 | + sessionID: chat.id, |
| 43 | + agent: "ayaz", |
| 44 | + model: ref, |
| 45 | + time: { created: Date.now() }, |
| 46 | + }) |
| 47 | + const assistant: MessageV2.Assistant = { |
| 48 | + id: MessageID.ascending(), |
| 49 | + role: "assistant", |
| 50 | + parentID: user.id, |
| 51 | + sessionID: chat.id, |
| 52 | + mode: "ayaz", |
| 53 | + agent: "ayaz", |
| 54 | + cost: 0, |
| 55 | + path: { cwd: "/tmp", root: "/tmp" }, |
| 56 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 57 | + modelID: ref.modelID, |
| 58 | + providerID: ref.providerID, |
| 59 | + time: { created: Date.now() }, |
| 60 | + } |
| 61 | + yield* session.updateMessage(assistant) |
| 62 | + return { chat, assistant } |
| 63 | +}) |
| 64 | + |
| 65 | +describe("tool.task_async", () => { |
| 66 | + it.live("canonicalizes the legacy explore alias before permission checks", () => |
| 67 | + provideTmpdirInstance(() => |
| 68 | + Effect.gen(function* () { |
| 69 | + const registry = yield* ToolRegistry.Service |
| 70 | + const tool = (yield* registry.all()).find((item) => item.id === "task_async") |
| 71 | + expect(tool).toBeDefined() |
| 72 | + if (!tool) throw new Error("task_async tool not found") |
| 73 | + |
| 74 | + const { chat, assistant } = yield* seed() |
| 75 | + const asks: Array<Omit<Permission.Request, "id" | "sessionID" | "tool">> = [] |
| 76 | + const exit = yield* Effect.exit( |
| 77 | + tool.execute( |
| 78 | + { |
| 79 | + action: "start", |
| 80 | + description: "inspect alias", |
| 81 | + prompt: "look into the target", |
| 82 | + subagent_type: "explore", |
| 83 | + }, |
| 84 | + { |
| 85 | + sessionID: chat.id, |
| 86 | + messageID: assistant.id, |
| 87 | + agent: "ayaz", |
| 88 | + abort: new AbortController().signal, |
| 89 | + messages: [], |
| 90 | + metadata: () => Effect.void, |
| 91 | + ask: (input) => |
| 92 | + Effect.sync(() => { |
| 93 | + asks.push(input) |
| 94 | + throw new Error("stop after ask") |
| 95 | + }), |
| 96 | + }, |
| 97 | + ), |
| 98 | + ) |
| 99 | + |
| 100 | + expect(Exit.isFailure(exit)).toBe(true) |
| 101 | + expect(asks).toHaveLength(1) |
| 102 | + expect(asks[0]?.patterns).toEqual(["explorer", "start"]) |
| 103 | + expect(asks[0]?.always).toEqual(["explorer", "start"]) |
| 104 | + expect(asks[0]?.metadata).toMatchObject({ |
| 105 | + action: "start", |
| 106 | + description: "inspect alias", |
| 107 | + subagent_type: "explorer", |
| 108 | + }) |
| 109 | + }), |
| 110 | + ), |
| 111 | + ) |
| 112 | +}) |
0 commit comments