|
1 | 1 | import "./chat-engine.mocks.test-support.js"; |
2 | 2 | import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { extractToolResultText } from "../agents/embedded-agent-tool-results.js"; |
| 4 | +import { createSystemAgentTool } from "../agents/tools/system-agent-tool.js"; |
| 5 | +import type { SystemAgentTurnRunner } from "./agent-turn.js"; |
3 | 6 | import { |
4 | 7 | fakeOverviewLoader, |
5 | 8 | sharedVerifiedInference, |
@@ -1048,4 +1051,67 @@ describe("SystemAgentChatEngine approval", () => { |
1048 | 1051 | expect(userTurns.some((text) => text.includes("very-secret"))).toBe(false); |
1049 | 1052 | expect(userTurns.some((text) => text.includes("<redacted secret>"))).toBe(true); |
1050 | 1053 | }); |
| 1054 | + |
| 1055 | + it("returns the delegated tool handoff through the engine without planner fallback", async () => { |
| 1056 | + const runAgentTurn = vi.fn<SystemAgentTurnRunner>(async (params) => { |
| 1057 | + const tool = createSystemAgentTool({ |
| 1058 | + surface: params.surface, |
| 1059 | + approvalArmed: params.approvalArmed, |
| 1060 | + operatorApprovalOnly: params.operatorApprovalOnly, |
| 1061 | + proposalRef: params.session.proposalRef, |
| 1062 | + }); |
| 1063 | + const result = await tool.execute("delegated-proposal", { |
| 1064 | + action: "config_set", |
| 1065 | + path: "agents.defaults.subagents.thinking", |
| 1066 | + value: "high", |
| 1067 | + }); |
| 1068 | + return { text: extractToolResultText(result) ?? "" }; |
| 1069 | + }); |
| 1070 | + const planner = vi.fn(async () => { |
| 1071 | + throw new Error("planner must not run when the loop replies"); |
| 1072 | + }); |
| 1073 | + const engine = new SystemAgentChatEngine({ |
| 1074 | + operatorApprovalOnly: true, |
| 1075 | + runAgentTurn, |
| 1076 | + planWithAssistant: planner, |
| 1077 | + deps: { loadOverview: fakeOverviewLoader() }, |
| 1078 | + }); |
| 1079 | + |
| 1080 | + const reply = await engine.handle("switch the thinking level"); |
| 1081 | + |
| 1082 | + expect(runAgentTurn).toHaveBeenCalledOnce(); |
| 1083 | + expect(planner).not.toHaveBeenCalled(); |
| 1084 | + expect(reply.text).toContain("OpenClaw operator UI"); |
| 1085 | + expect(reply.text).toContain("cannot be applied from this chat"); |
| 1086 | + expect(reply.text).not.toContain("ask the user to reply yes"); |
| 1087 | + expect(reply.action).toBe("none"); |
| 1088 | + expect(engine.getPendingOperatorProposal()?.operation).toEqual({ |
| 1089 | + kind: "config-set", |
| 1090 | + path: "agents.defaults.subagents.thinking", |
| 1091 | + value: "high", |
| 1092 | + }); |
| 1093 | + }); |
| 1094 | + |
| 1095 | + it("tells delegated messaging users an approval can't be applied from chat", async () => { |
| 1096 | + const planner = vi.fn(async () => ({ |
| 1097 | + reply: "Let's point your agent at gpt-5.5.", |
| 1098 | + command: "set default model openai/gpt-5.5", |
| 1099 | + modelLabel: "claude-cli", |
| 1100 | + })); |
| 1101 | + const engine = new SystemAgentChatEngine({ |
| 1102 | + operatorApprovalOnly: true, |
| 1103 | + runAgentTurn: async () => null, |
| 1104 | + planWithAssistant: planner, |
| 1105 | + deps: { loadOverview: fakeOverviewLoader() }, |
| 1106 | + }); |
| 1107 | + |
| 1108 | + const reply = await engine.handle("actually use an openai model"); |
| 1109 | + |
| 1110 | + expect(reply.text).toContain("cannot be applied from this chat"); |
| 1111 | + expect(reply.text).toContain("OpenClaw operator UI"); |
| 1112 | + expect(reply.text).toContain("Refused:"); |
| 1113 | + expect(reply.text).toContain("was not applied from this chat"); |
| 1114 | + expect(reply.text).not.toContain("Say yes to apply"); |
| 1115 | + expect(engine.hasPendingProposal()).toBe(true); |
| 1116 | + }); |
1051 | 1117 | }); |
0 commit comments