fix(cron): treat embedded error payloads as run failures

(cherry picked from commit 50fd31c070)
This commit is contained in:
chilu18
2026-02-23 19:16:57 +00:00
committed by Peter Steinberger
parent 75969ed5c4
commit 8c8374defa
2 changed files with 49 additions and 4 deletions

View File

@@ -27,9 +27,9 @@ function makeDeps(): CliDeps {
};
}
function mockEmbeddedTexts(texts: string[]) {
function mockEmbeddedPayloads(payloads: Array<{ text?: string; isError?: boolean }>) {
vi.mocked(runEmbeddedPiAgent).mockResolvedValue({
payloads: texts.map((text) => ({ text })),
payloads,
meta: {
durationMs: 5,
agentMeta: { sessionId: "s", provider: "p", model: "m" },
@@ -37,6 +37,10 @@ function mockEmbeddedTexts(texts: string[]) {
});
}
function mockEmbeddedTexts(texts: string[]) {
mockEmbeddedPayloads(texts.map((text) => ({ text })));
}
function mockEmbeddedOk() {
mockEmbeddedTexts(["ok"]);
}
@@ -174,6 +178,25 @@ describe("runCronIsolatedAgentTurn", () => {
});
});
it("returns error when embedded run payload is marked as error", async () => {
await withTempHome(async (home) => {
mockEmbeddedPayloads([
{
text: "⚠️ 🛠️ Exec failed: /bin/bash: line 1: python: command not found",
isError: true,
},
]);
const { res } = await runCronTurn(home, {
jobPayload: DEFAULT_AGENT_TURN_PAYLOAD,
mockTexts: null,
});
expect(res.status).toBe("error");
expect(res.error).toContain("command not found");
expect(res.summary).toContain("Exec failed");
});
});
it("passes resolved agentDir to runEmbeddedPiAgent", async () => {
await withTempHome(async (home) => {
const { res } = await runCronTurn(home, {

View File

@@ -543,6 +543,25 @@ export async function runCronIsolatedAgentTurn(params: {
(deliveryPayload?.mediaUrls?.length ?? 0) > 0 ||
Object.keys(deliveryPayload?.channelData ?? {}).length > 0;
const deliveryBestEffort = resolveCronDeliveryBestEffort(params.job);
const hasErrorPayload = payloads.some((payload) => payload?.isError === true);
const lastErrorPayloadText = [...payloads]
.toReversed()
.find((payload) => payload?.isError === true && Boolean(payload?.text?.trim()))
?.text?.trim();
const embeddedRunError = hasErrorPayload
? (lastErrorPayloadText ?? "cron isolated run returned an error payload")
: undefined;
const resolveRunOutcome = (params?: { delivered?: boolean }) =>
withRunSession({
status: hasErrorPayload ? "error" : "ok",
...(hasErrorPayload
? { error: embeddedRunError ?? "cron isolated run returned an error payload" }
: {}),
summary,
outputText,
delivered: params?.delivered,
...telemetry,
});
// Skip delivery for heartbeat-only responses (HEARTBEAT_OK with no real content).
const ackMaxChars = resolveHeartbeatAckMaxChars(agentCfg);
@@ -586,11 +605,14 @@ export async function runCronIsolatedAgentTurn(params: {
withRunSession,
});
if (deliveryResult.result) {
return deliveryResult.result;
if (!hasErrorPayload || deliveryResult.result.status !== "ok") {
return deliveryResult.result;
}
return resolveRunOutcome({ delivered: deliveryResult.result.delivered });
}
const delivered = deliveryResult.delivered;
summary = deliveryResult.summary;
outputText = deliveryResult.outputText;
return withRunSession({ status: "ok", summary, outputText, delivered, ...telemetry });
return resolveRunOutcome({ delivered });
}