|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { spawnSubagentDirect } from "./subagent-spawn.js"; |
| 3 | + |
| 4 | +type TestAgentConfig = { |
| 5 | + id?: string; |
| 6 | + workspace?: string; |
| 7 | + subagents?: { |
| 8 | + allowAgents?: string[]; |
| 9 | + }; |
| 10 | +}; |
| 11 | + |
| 12 | +type TestConfig = { |
| 13 | + agents?: { |
| 14 | + list?: TestAgentConfig[]; |
| 15 | + }; |
| 16 | +}; |
| 17 | + |
| 18 | +const hoisted = vi.hoisted(() => ({ |
| 19 | + callGatewayMock: vi.fn(), |
| 20 | + configOverride: {} as Record<string, unknown>, |
| 21 | + registerSubagentRunMock: vi.fn(), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock("../gateway/call.js", () => ({ |
| 25 | + callGateway: (opts: unknown) => hoisted.callGatewayMock(opts), |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock("../config/config.js", async (importOriginal) => { |
| 29 | + const actual = await importOriginal<typeof import("../config/config.js")>(); |
| 30 | + return { |
| 31 | + ...actual, |
| 32 | + loadConfig: () => hoisted.configOverride, |
| 33 | + }; |
| 34 | +}); |
| 35 | + |
| 36 | +vi.mock("@mariozechner/pi-ai/oauth", () => ({ |
| 37 | + getOAuthApiKey: () => "", |
| 38 | + getOAuthProviders: () => [], |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock("./subagent-registry.js", () => ({ |
| 42 | + countActiveRunsForSession: () => 0, |
| 43 | + registerSubagentRun: (args: unknown) => hoisted.registerSubagentRunMock(args), |
| 44 | +})); |
| 45 | + |
| 46 | +vi.mock("./subagent-announce.js", () => ({ |
| 47 | + buildSubagentSystemPrompt: () => "system-prompt", |
| 48 | +})); |
| 49 | + |
| 50 | +vi.mock("./subagent-depth.js", () => ({ |
| 51 | + getSubagentDepthFromSessionStore: () => 0, |
| 52 | +})); |
| 53 | + |
| 54 | +vi.mock("./model-selection.js", () => ({ |
| 55 | + resolveSubagentSpawnModelSelection: () => undefined, |
| 56 | +})); |
| 57 | + |
| 58 | +vi.mock("./sandbox/runtime-status.js", () => ({ |
| 59 | + resolveSandboxRuntimeStatus: () => ({ sandboxed: false }), |
| 60 | +})); |
| 61 | + |
| 62 | +vi.mock("../plugins/hook-runner-global.js", () => ({ |
| 63 | + getGlobalHookRunner: () => ({ hasHooks: () => false }), |
| 64 | +})); |
| 65 | + |
| 66 | +vi.mock("../utils/delivery-context.js", () => ({ |
| 67 | + normalizeDeliveryContext: (value: unknown) => value, |
| 68 | +})); |
| 69 | + |
| 70 | +vi.mock("./tools/sessions-helpers.js", () => ({ |
| 71 | + resolveMainSessionAlias: () => ({ mainKey: "main", alias: "main" }), |
| 72 | + resolveInternalSessionKey: ({ key }: { key?: string }) => key ?? "agent:main:main", |
| 73 | + resolveDisplaySessionKey: ({ key }: { key?: string }) => key ?? "agent:main:main", |
| 74 | +})); |
| 75 | + |
| 76 | +vi.mock("./agent-scope.js", () => ({ |
| 77 | + resolveAgentConfig: (cfg: TestConfig, agentId: string) => |
| 78 | + cfg.agents?.list?.find((entry) => entry.id === agentId), |
| 79 | + resolveAgentWorkspaceDir: (cfg: TestConfig, agentId: string) => |
| 80 | + cfg.agents?.list?.find((entry) => entry.id === agentId)?.workspace ?? |
| 81 | + `/tmp/workspace-${agentId}`, |
| 82 | +})); |
| 83 | + |
| 84 | +function createConfigOverride(overrides?: Record<string, unknown>) { |
| 85 | + return { |
| 86 | + session: { |
| 87 | + mainKey: "main", |
| 88 | + scope: "per-sender", |
| 89 | + }, |
| 90 | + agents: { |
| 91 | + list: [ |
| 92 | + { |
| 93 | + id: "main", |
| 94 | + workspace: "/tmp/workspace-main", |
| 95 | + }, |
| 96 | + ], |
| 97 | + }, |
| 98 | + ...overrides, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +function setupGatewayMock() { |
| 103 | + hoisted.callGatewayMock.mockImplementation( |
| 104 | + async (opts: { method?: string; params?: Record<string, unknown> }) => { |
| 105 | + if (opts.method === "sessions.patch") { |
| 106 | + return { ok: true }; |
| 107 | + } |
| 108 | + if (opts.method === "sessions.delete") { |
| 109 | + return { ok: true }; |
| 110 | + } |
| 111 | + if (opts.method === "agent") { |
| 112 | + return { runId: "run-1" }; |
| 113 | + } |
| 114 | + return {}; |
| 115 | + }, |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +function getRegisteredRun() { |
| 120 | + return hoisted.registerSubagentRunMock.mock.calls.at(0)?.[0] as |
| 121 | + | Record<string, unknown> |
| 122 | + | undefined; |
| 123 | +} |
| 124 | + |
| 125 | +describe("spawnSubagentDirect workspace inheritance", () => { |
| 126 | + beforeEach(() => { |
| 127 | + hoisted.callGatewayMock.mockClear(); |
| 128 | + hoisted.registerSubagentRunMock.mockClear(); |
| 129 | + hoisted.configOverride = createConfigOverride(); |
| 130 | + setupGatewayMock(); |
| 131 | + }); |
| 132 | + |
| 133 | + it("uses the target agent workspace for cross-agent spawns", async () => { |
| 134 | + hoisted.configOverride = createConfigOverride({ |
| 135 | + agents: { |
| 136 | + list: [ |
| 137 | + { |
| 138 | + id: "main", |
| 139 | + workspace: "/tmp/workspace-main", |
| 140 | + subagents: { |
| 141 | + allowAgents: ["ops"], |
| 142 | + }, |
| 143 | + }, |
| 144 | + { |
| 145 | + id: "ops", |
| 146 | + workspace: "/tmp/workspace-ops", |
| 147 | + }, |
| 148 | + ], |
| 149 | + }, |
| 150 | + }); |
| 151 | + |
| 152 | + const result = await spawnSubagentDirect( |
| 153 | + { |
| 154 | + task: "inspect workspace", |
| 155 | + agentId: "ops", |
| 156 | + }, |
| 157 | + { |
| 158 | + agentSessionKey: "agent:main:main", |
| 159 | + agentChannel: "telegram", |
| 160 | + agentAccountId: "123", |
| 161 | + agentTo: "456", |
| 162 | + workspaceDir: "/tmp/requester-workspace", |
| 163 | + }, |
| 164 | + ); |
| 165 | + |
| 166 | + expect(result.status).toBe("accepted"); |
| 167 | + expect(getRegisteredRun()).toMatchObject({ |
| 168 | + workspaceDir: "/tmp/workspace-ops", |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + it("preserves the inherited workspace for same-agent spawns", async () => { |
| 173 | + const result = await spawnSubagentDirect( |
| 174 | + { |
| 175 | + task: "inspect workspace", |
| 176 | + agentId: "main", |
| 177 | + }, |
| 178 | + { |
| 179 | + agentSessionKey: "agent:main:main", |
| 180 | + agentChannel: "telegram", |
| 181 | + agentAccountId: "123", |
| 182 | + agentTo: "456", |
| 183 | + workspaceDir: "/tmp/requester-workspace", |
| 184 | + }, |
| 185 | + ); |
| 186 | + |
| 187 | + expect(result.status).toBe("accepted"); |
| 188 | + expect(getRegisteredRun()).toMatchObject({ |
| 189 | + workspaceDir: "/tmp/requester-workspace", |
| 190 | + }); |
| 191 | + }); |
| 192 | +}); |
0 commit comments