From 9819b10ee5d8219be2d849ba8b26da671738e514 Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 2 Jun 2026 17:04:36 +0800 Subject: [PATCH] fix: unwrap agentrun manager envelopes --- internal/agent/agentrun-dispatch.mjs | 8 +++++++- internal/agent/agentrun-dispatch.test.mjs | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/internal/agent/agentrun-dispatch.mjs b/internal/agent/agentrun-dispatch.mjs index 0fd56767..16f75981 100644 --- a/internal/agent/agentrun-dispatch.mjs +++ b/internal/agent/agentrun-dispatch.mjs @@ -268,7 +268,13 @@ async function postJson(fetchImpl, managerUrl, path, payload) { const text = await response.text(); const body = text ? JSON.parse(text) : null; if (!response.ok) throw new Error(`AgentRun POST ${path} failed: status=${response.status} body=${JSON.stringify(body).slice(0, 500)}`); - return body; + return unwrapAgentRunEnvelope(path, body); +} + +function unwrapAgentRunEnvelope(path, body) { + if (!body || typeof body !== "object" || !("ok" in body)) return body; + if (body.ok === true) return body.data ?? null; + throw new Error(`AgentRun POST ${path} failed: envelope=${JSON.stringify(body).slice(0, 500)}`); } function normalizeBackendProfile(value) { diff --git a/internal/agent/agentrun-dispatch.test.mjs b/internal/agent/agentrun-dispatch.test.mjs index 7d8d3ccb..e4feb8f5 100644 --- a/internal/agent/agentrun-dispatch.test.mjs +++ b/internal/agent/agentrun-dispatch.test.mjs @@ -125,6 +125,27 @@ test("dispatchHwlabAgentRun posts run command and runner job with commandId", as assert.equal(requests[2].body.transientEnv.some((item) => item.name === "UNIDESK_SSH_CLIENT_TOKEN"), false); }); +test("dispatchHwlabAgentRun unwraps AgentRun manager response envelopes", async () => { + const fetchImpl = async (url, init) => { + if (url.endsWith("/api/v1/runs")) return jsonResponse({ ok: true, data: { id: "run_hwlab_envelope" } }); + if (url.endsWith("/api/v1/runs/run_hwlab_envelope/commands")) return jsonResponse({ ok: true, data: { id: "cmd_hwlab_envelope" } }); + if (url.endsWith("/api/v1/runs/run_hwlab_envelope/runner-jobs")) return jsonResponse({ ok: true, data: { id: "job_hwlab_envelope", jobName: "agentrun-v01-runner-envelope" } }); + return jsonResponse({ ok: false, failureKind: "unexpected-path" }, { status: 404 }); + }; + + const result = await dispatchHwlabAgentRun({ + fetchImpl, + traceId: "trc_hwlab_dispatch_envelope", + prompt: "dispatch envelope smoke", + commitId, + unideskMainServerIp: "https://unidesk.example.test" + }); + + assert.equal(result.run.id, "run_hwlab_envelope"); + assert.equal(result.command.id, "cmd_hwlab_envelope"); + assert.equal(result.runnerJob.jobName, "agentrun-v01-runner-envelope"); +}); + function jsonResponse(body, { status = 200 } = {}) { return { ok: status >= 200 && status < 300,