|
| 1 | +import { test } from "worker-testbed"; |
| 2 | +import { execFileSync } from "node:child_process"; |
| 3 | +import { mkdtempSync, writeFileSync, existsSync, rmSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join, dirname } from "node:path"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | + |
| 8 | +import { |
| 9 | + addAgent, |
| 10 | + addCompletion, |
| 11 | + addState, |
| 12 | + addSwarm, |
| 13 | + addTool, |
| 14 | + commitToolOutput, |
| 15 | + executeForce, |
| 16 | + session, |
| 17 | + setConfig, |
| 18 | + State, |
| 19 | +} from "../../build/index.mjs"; |
| 20 | +import { randomString, sleep } from "functools-kit"; |
| 21 | + |
| 22 | +const HANG = Symbol("hang"); |
| 23 | +const raceHang = (p, ms = 8000) => Promise.race([p, sleep(ms).then(() => HANG)]); |
| 24 | + |
| 25 | +test("Will run no tools when maxToolCalls is zero", async ({ pass, fail }) => { |
| 26 | + setConfig({ CC_PERSIST_ENABLED_BY_DEFAULT: false }); |
| 27 | + const executed = []; |
| 28 | + |
| 29 | + addCompletion({ |
| 30 | + completionName: "mock-completion", |
| 31 | + getCompletion: async ({ agentName, messages }) => { |
| 32 | + const [last] = messages.slice(-1); |
| 33 | + if (last.content === "start") { |
| 34 | + return { |
| 35 | + agentName, |
| 36 | + content: "no-tools-answer", |
| 37 | + role: "assistant", |
| 38 | + tool_calls: [ |
| 39 | + { function: { name: "e_tool", arguments: { t: "a" } } }, |
| 40 | + { function: { name: "e_tool", arguments: { t: "b" } } }, |
| 41 | + { function: { name: "e_tool", arguments: { t: "c" } } }, |
| 42 | + ], |
| 43 | + }; |
| 44 | + } |
| 45 | + return { agentName, content: `echo:${last.content}`, role: "assistant" }; |
| 46 | + }, |
| 47 | + }); |
| 48 | + addTool({ |
| 49 | + toolName: "e_tool", |
| 50 | + validate: () => true, |
| 51 | + type: "function", |
| 52 | + function: { name: "e_tool", description: "", parameters: { type: "object", properties: {}, required: [] } }, |
| 53 | + call: async ({ toolId, clientId, agentName, params, isLast }) => { |
| 54 | + executed.push(params.t); |
| 55 | + await commitToolOutput(toolId, "x", clientId, agentName); |
| 56 | + if (isLast) await executeForce("finish", clientId); |
| 57 | + }, |
| 58 | + }); |
| 59 | + const TEST_AGENT = addAgent({ |
| 60 | + agentName: "test-agent", |
| 61 | + completion: "mock-completion", |
| 62 | + prompt: "", |
| 63 | + tools: ["e_tool"], |
| 64 | + maxToolCalls: 0, |
| 65 | + }); |
| 66 | + const TEST_SWARM = addSwarm({ swarmName: "test-swarm", agentList: [TEST_AGENT], defaultAgent: TEST_AGENT }); |
| 67 | + |
| 68 | + const chatSession = session(randomString(), TEST_SWARM); |
| 69 | + const result = await raceHang(chatSession.complete("start")); |
| 70 | + await raceHang(chatSession.dispose()); |
| 71 | + |
| 72 | + if (executed.length === 0 && result === "no-tools-answer") { |
| 73 | + pass(); |
| 74 | + return; |
| 75 | + } |
| 76 | + fail(`executed=${JSON.stringify(executed)} result=${String(result)}`); |
| 77 | +}); |
| 78 | + |
| 79 | +test("Will keep zero prior messages when keepMessages is zero", async ({ pass, fail }) => { |
| 80 | + setConfig({ CC_PERSIST_ENABLED_BY_DEFAULT: false }); |
| 81 | + let windowSnapshot = null; |
| 82 | + |
| 83 | + let turn = 0; |
| 84 | + addCompletion({ |
| 85 | + completionName: "mock-completion", |
| 86 | + getCompletion: async ({ agentName, messages }) => { |
| 87 | + turn += 1; |
| 88 | + if (turn === 3) { |
| 89 | + windowSnapshot = messages.filter((m) => m.role !== "system").map((m) => m.content); |
| 90 | + } |
| 91 | + const [last] = messages.slice(-1) ?? [{ content: "none" }]; |
| 92 | + return { agentName, content: `echo:${last?.content ?? "none"}`, role: "assistant" }; |
| 93 | + }, |
| 94 | + }); |
| 95 | + const TEST_AGENT = addAgent({ |
| 96 | + agentName: "test-agent", |
| 97 | + completion: "mock-completion", |
| 98 | + prompt: "", |
| 99 | + keepMessages: 0, |
| 100 | + }); |
| 101 | + const TEST_SWARM = addSwarm({ swarmName: "test-swarm", agentList: [TEST_AGENT], defaultAgent: TEST_AGENT }); |
| 102 | + |
| 103 | + const chatSession = session(randomString(), TEST_SWARM); |
| 104 | + await raceHang(chatSession.complete("turn1")); |
| 105 | + await raceHang(chatSession.complete("turn2")); |
| 106 | + await raceHang(chatSession.complete("turn3")); |
| 107 | + await raceHang(chatSession.dispose()); |
| 108 | + |
| 109 | + // keepMessages=0 is a zero-width window: the model sees no common (user/ |
| 110 | + // assistant) messages at all, not even the current turn's own message. |
| 111 | + if (Array.isArray(windowSnapshot) && windowSnapshot.length === 0) { |
| 112 | + pass(); |
| 113 | + return; |
| 114 | + } |
| 115 | + fail(`window=${JSON.stringify(windowSnapshot)}`); |
| 116 | +}); |
| 117 | + |
| 118 | +test("Will serve reentrant getState inside setState without deadlock", async ({ pass, fail }) => { |
| 119 | + setConfig({ CC_PERSIST_ENABLED_BY_DEFAULT: false }); |
| 120 | + |
| 121 | + addCompletion({ |
| 122 | + completionName: "mock-completion", |
| 123 | + getCompletion: async ({ agentName, messages }) => { |
| 124 | + const [last] = messages.slice(-1); |
| 125 | + return { agentName, content: `echo:${last.content}`, role: "assistant" }; |
| 126 | + }, |
| 127 | + }); |
| 128 | + addState({ stateName: "test-state", getDefaultState: () => ({ v: 10 }) }); |
| 129 | + const TEST_AGENT = addAgent({ |
| 130 | + agentName: "test-agent", |
| 131 | + completion: "mock-completion", |
| 132 | + prompt: "", |
| 133 | + states: ["test-state"], |
| 134 | + }); |
| 135 | + const TEST_SWARM = addSwarm({ swarmName: "test-swarm", agentList: [TEST_AGENT], defaultAgent: TEST_AGENT }); |
| 136 | + |
| 137 | + const CLIENT_ID = randomString(); |
| 138 | + const chatSession = session(CLIENT_ID, TEST_SWARM); |
| 139 | + const context = { clientId: CLIENT_ID, agentName: TEST_AGENT, stateName: "test-state" }; |
| 140 | + |
| 141 | + const nested = await raceHang( |
| 142 | + State.setState(async (prev) => { |
| 143 | + const inner = await State.getState(context); |
| 144 | + return { v: prev.v + inner.v }; |
| 145 | + }, context) |
| 146 | + ); |
| 147 | + |
| 148 | + // Fire-and-forget writes must still be observed in order by a following read. |
| 149 | + State.setState(() => ({ v: 100 }), context); |
| 150 | + State.setState((p) => ({ v: p.v + 1 }), context); |
| 151 | + State.setState((p) => ({ v: p.v + 1 }), context); |
| 152 | + const ordered = await raceHang(State.getState(context)); |
| 153 | + await raceHang(chatSession.dispose()); |
| 154 | + |
| 155 | + if (nested !== HANG && nested.v === 20 && ordered !== HANG && ordered.v === 102) { |
| 156 | + pass(); |
| 157 | + return; |
| 158 | + } |
| 159 | + fail(`nested=${JSON.stringify(nested)} ordered=${JSON.stringify(ordered)}`); |
| 160 | +}); |
| 161 | + |
| 162 | +test("Will contain persistence writes inside dump dir for traversal client ids", async ({ pass, fail }) => { |
| 163 | + const buildPath = join( |
| 164 | + dirname(fileURLToPath(import.meta.url)), |
| 165 | + "..", |
| 166 | + "..", |
| 167 | + "build", |
| 168 | + "index.mjs" |
| 169 | + ); |
| 170 | + const workDir = mkdtempSync(join(tmpdir(), "swarm-traversal-")); |
| 171 | + const escapeTarget = join(workDir, "ESCAPED.json"); |
| 172 | + const scriptPath = join(workDir, "scenario.mjs"); |
| 173 | + const script = ` |
| 174 | +import { addAgent, addCompletion, addState, addSwarm, session, setConfig, State } from ${JSON.stringify(buildPath)}; |
| 175 | +setConfig({ CC_PERSIST_ENABLED_BY_DEFAULT: true }); |
| 176 | +addState({ stateName: "st", getDefaultState: () => ({ v: 0 }), persist: true }); |
| 177 | +addCompletion({ completionName: "c", getCompletion: async ({ agentName, messages }) => { |
| 178 | + const [l] = messages.slice(-1); return { agentName, content: "echo:" + l.content, role: "assistant" }; }}); |
| 179 | +const A = addAgent({ agentName: "a", completion: "c", prompt: "", states: ["st"] }); |
| 180 | +const S = addSwarm({ swarmName: "s", agentList: [A], defaultAgent: A, persist: true }); |
| 181 | +const evil = "../../ESCAPED"; |
| 182 | +const cs = session(evil, S); |
| 183 | +await State.setState(() => ({ v: 99 }), { clientId: evil, agentName: A, stateName: "st" }); |
| 184 | +await cs.dispose(); |
| 185 | +console.log("DONE"); |
| 186 | +process.exit(0); |
| 187 | +`; |
| 188 | + writeFileSync(scriptPath, script); |
| 189 | + try { |
| 190 | + const out = execFileSync(process.execPath, [scriptPath], { |
| 191 | + cwd: workDir, |
| 192 | + encoding: "utf-8", |
| 193 | + timeout: 30_000, |
| 194 | + }); |
| 195 | + const escaped = existsSync(escapeTarget); |
| 196 | + if (out.includes("DONE") && !escaped) { |
| 197 | + pass(); |
| 198 | + return; |
| 199 | + } |
| 200 | + fail(`escaped=${escaped} out=${out}`); |
| 201 | + } finally { |
| 202 | + rmSync(workDir, { recursive: true, force: true }); |
| 203 | + } |
| 204 | +}); |
0 commit comments