|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import type { ServerResponse } from "node:http"; |
| 3 | +import { text as readText } from "node:stream/consumers"; |
| 4 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 5 | +import { |
| 6 | + writeOpenAiResponsesSse, |
| 7 | + writeOpenAiResponsesText, |
| 8 | +} from "../../../test/helpers/openai-responses-sse.js"; |
| 9 | +import { loadAgentRuntimePluginRegistryHandle } from "../../agents/runtime-plugins.js"; |
| 10 | +import { withServer } from "../../plugin-sdk/test-helpers/http-test-server.js"; |
| 11 | +import { |
| 12 | + createOpenClawTestState, |
| 13 | + type OpenClawTestState, |
| 14 | +} from "../../test-utils/openclaw-test-state.js"; |
| 15 | +import { createTrackedTempDirs } from "../../test-utils/tracked-temp-dirs.js"; |
| 16 | +import { readSkillReviewOutcomes } from "./collection-review-state.js"; |
| 17 | +import { runSkillExperienceReview } from "./experience-review.js"; |
| 18 | +import { |
| 19 | + createExperienceReviewCandidate, |
| 20 | + createExperienceReviewMessages, |
| 21 | +} from "./experience-review.test-support.js"; |
| 22 | +import { |
| 23 | + getSkillProposalRunProgress, |
| 24 | + inspectSkillProposal, |
| 25 | + listSkillProposals, |
| 26 | +} from "./service.js"; |
| 27 | + |
| 28 | +const modelId = "gpt-5.6-luna"; |
| 29 | +const { positiveMessages } = createExperienceReviewMessages(modelId); |
| 30 | +const tempDirs = createTrackedTempDirs(); |
| 31 | +let state: OpenClawTestState; |
| 32 | +const proposalBody = [ |
| 33 | + "# Manifest Deployment", |
| 34 | + "", |
| 35 | + "1. Read the checked-in deployment manifest and collect project, region, service, and health path.", |
| 36 | + "2. Deploy with the manifest values, then fetch its health path and verify a successful response.", |
| 37 | +].join("\n"); |
| 38 | +const createArgs = { |
| 39 | + action: "create", |
| 40 | + name: "Manifest Deployment", |
| 41 | + description: "Deploy from a checked-in manifest and verify service health.", |
| 42 | + proposal_content: proposalBody, |
| 43 | +}; |
| 44 | +type Request = { |
| 45 | + model?: string; |
| 46 | + input?: Array<{ type?: string; name?: string; call_id?: string; output?: unknown }>; |
| 47 | + tools?: Array<{ name?: string }>; |
| 48 | +}; |
| 49 | +type Scenario = "proposed" | "nothing" | "rejected" | "failed"; |
| 50 | + |
| 51 | +beforeAll(async () => { |
| 52 | + state = await createOpenClawTestState({ layout: "home", prefix: "workshop-owner-contract-" }); |
| 53 | +}); |
| 54 | +afterAll(async () => { |
| 55 | + await state.cleanup(); |
| 56 | + await tempDirs.cleanup(); |
| 57 | +}); |
| 58 | + |
| 59 | +function writeToolCall(response: ServerResponse, args: Record<string, unknown>): void { |
| 60 | + const item = { |
| 61 | + type: "function_call", |
| 62 | + id: "fc_workshop_contract", |
| 63 | + call_id: "call_workshop_contract", |
| 64 | + name: "skill_workshop", |
| 65 | + arguments: JSON.stringify(args), |
| 66 | + status: "completed", |
| 67 | + }; |
| 68 | + writeOpenAiResponsesSse(response, [ |
| 69 | + { |
| 70 | + type: "response.output_item.added", |
| 71 | + output_index: 0, |
| 72 | + item: { ...item, status: "in_progress", arguments: "" }, |
| 73 | + }, |
| 74 | + { |
| 75 | + type: "response.function_call_arguments.done", |
| 76 | + item_id: item.id, |
| 77 | + output_index: 0, |
| 78 | + arguments: item.arguments, |
| 79 | + }, |
| 80 | + { type: "response.output_item.done", output_index: 0, item }, |
| 81 | + { |
| 82 | + type: "response.completed", |
| 83 | + response: { |
| 84 | + id: "resp_workshop_contract_tool", |
| 85 | + status: "completed", |
| 86 | + output: [item], |
| 87 | + usage: { input_tokens: 10, output_tokens: 10, total_tokens: 20 }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + ]); |
| 91 | +} |
| 92 | + |
| 93 | +describe("Workshop experience review through the real provider and tool owners", () => { |
| 94 | + it.each<Scenario>(["proposed", "nothing", "rejected", "failed"])( |
| 95 | + "records %s without replacing the review runner, catalog, or proposal service", |
| 96 | + async (scenario) => { |
| 97 | + const requests: Request[] = []; |
| 98 | + const handlerErrors: unknown[] = []; |
| 99 | + await withServer( |
| 100 | + (request, response) => { |
| 101 | + void (async () => { |
| 102 | + if (request.method !== "POST" || request.url !== "/v1/responses") { |
| 103 | + response.writeHead(404).end(); |
| 104 | + return; |
| 105 | + } |
| 106 | + requests.push(JSON.parse(await readText(request)) as Request); |
| 107 | + if (scenario === "failed" || requests.length > 2) { |
| 108 | + response.writeHead(400, { "content-type": "application/json" }); |
| 109 | + response.end(JSON.stringify({ error: { message: "Controlled provider rejection" } })); |
| 110 | + return; |
| 111 | + } |
| 112 | + if (requests.length === 1 && (scenario === "proposed" || scenario === "rejected")) { |
| 113 | + writeToolCall(response, scenario === "proposed" ? createArgs : { action: "create" }); |
| 114 | + return; |
| 115 | + } |
| 116 | + writeOpenAiResponsesText(response, { |
| 117 | + text: "NO_REPLY", |
| 118 | + messageId: `msg_workshop_contract_${requests.length}`, |
| 119 | + responseId: `resp_workshop_contract_${requests.length}`, |
| 120 | + }); |
| 121 | + })().catch((error: unknown) => { |
| 122 | + handlerErrors.push(error); |
| 123 | + response.writeHead(400).end(); |
| 124 | + }); |
| 125 | + }, |
| 126 | + async (baseUrl) => { |
| 127 | + const workspaceDir = await tempDirs.make(`workshop-contract-${scenario}-`); |
| 128 | + const runId = `owner-contract-${scenario}`; |
| 129 | + const messages = positiveMessages(); |
| 130 | + const candidate = await createExperienceReviewCandidate(runId, messages, { |
| 131 | + workspaceDir, |
| 132 | + modelId, |
| 133 | + baseUrl: `${baseUrl}/v1`, |
| 134 | + apiKey: "test-token-placeholder", |
| 135 | + }); |
| 136 | + // Load the real provider plugin before entering the review lane, as the live proof does. |
| 137 | + loadAgentRuntimePluginRegistryHandle({ config: candidate.config ?? {}, workspaceDir }); |
| 138 | + const outcomesBefore = new Set(Object.keys(readSkillReviewOutcomes().experienceReviews)); |
| 139 | + const run = runSkillExperienceReview(candidate, { |
| 140 | + getCurrentConfig: () => candidate.config ?? {}, |
| 141 | + }); |
| 142 | + if (scenario === "failed") { |
| 143 | + await expect(run).rejects.toThrow( |
| 144 | + "provider rejected the request schema or tool payload", |
| 145 | + ); |
| 146 | + } else { |
| 147 | + await run; |
| 148 | + } |
| 149 | + |
| 150 | + expect(handlerErrors).toEqual([]); |
| 151 | + expect(requests).toHaveLength(scenario === "proposed" || scenario === "rejected" ? 2 : 1); |
| 152 | + expect(requests[0]?.model).toBe(modelId); |
| 153 | + expect(requests[0]?.tools?.map((tool) => tool.name)).toEqual( |
| 154 | + expect.arrayContaining(["exec", "read", "skill_workshop"]), |
| 155 | + ); |
| 156 | + // Request IDs are rewritten for provider replay. Compare the actual output bodies. |
| 157 | + expect( |
| 158 | + requests[0]?.input |
| 159 | + ?.filter((item) => item.type === "function_call_output") |
| 160 | + .map((item) => item.output), |
| 161 | + ).toEqual( |
| 162 | + messages |
| 163 | + .filter((message) => message.role === "toolResult") |
| 164 | + .map((message) => |
| 165 | + message.content.map((part) => (part.type === "text" ? part.text : "")).join("\n"), |
| 166 | + ), |
| 167 | + ); |
| 168 | + |
| 169 | + const { proposals } = await listSkillProposals({ workspaceDir }); |
| 170 | + const progress = await getSkillProposalRunProgress({ workspaceDir, runId }); |
| 171 | + const outcomes = Object.entries(readSkillReviewOutcomes().experienceReviews).filter( |
| 172 | + ([key]) => !outcomesBefore.has(key), |
| 173 | + ); |
| 174 | + expect(outcomes).toHaveLength(1); |
| 175 | + const outcome = outcomes[0]![1]; |
| 176 | + if (scenario === "proposed") { |
| 177 | + expect(proposals).toHaveLength(1); |
| 178 | + const proposal = proposals[0]!; |
| 179 | + expect(proposal.status).toBe("pending"); |
| 180 | + expect(progress).toMatchObject({ mutationCount: 1, proposalIds: [proposal.id] }); |
| 181 | + const stored = await inspectSkillProposal(proposal.id, { workspaceDir }); |
| 182 | + expect(stored?.record).toMatchObject({ autonomousCapture: true, origin: { runId } }); |
| 183 | + expect(stored?.content).toContain(proposalBody); |
| 184 | + await expect(fs.stat(stored!.record.target.skillFile)).rejects.toMatchObject({ |
| 185 | + code: "ENOENT", |
| 186 | + }); |
| 187 | + expect(outcome).toMatchObject({ outcome: "proposed", proposalId: proposal.id }); |
| 188 | + const toolOutput = requests[1]?.input?.find( |
| 189 | + (item) => |
| 190 | + item.type === "function_call_output" && item.call_id === "call_workshop_contract", |
| 191 | + ); |
| 192 | + expect(toolOutput?.output).toContain(proposal.id); |
| 193 | + } else { |
| 194 | + expect(proposals).toEqual([]); |
| 195 | + expect(progress.mutationCount).toBe(0); |
| 196 | + expect(outcome).toMatchObject({ |
| 197 | + outcome: scenario === "failed" ? "failed" : "nothing", |
| 198 | + }); |
| 199 | + if (scenario === "rejected") { |
| 200 | + const toolOutput = requests[1]?.input?.find( |
| 201 | + (item) => |
| 202 | + item.type === "function_call_output" && item.call_id === "call_workshop_contract", |
| 203 | + ); |
| 204 | + expect(toolOutput?.output).toContain("required"); |
| 205 | + } |
| 206 | + } |
| 207 | + if (scenario !== "failed") { |
| 208 | + expect(outcome?.usage?.outputTokens).toBeGreaterThan(0); |
| 209 | + } |
| 210 | + }, |
| 211 | + ); |
| 212 | + }, |
| 213 | + 120_000, |
| 214 | + ); |
| 215 | +}); |
0 commit comments