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

This commit is contained in:
chilu18
2026-02-23 19:16:57 +00:00
parent 8d69251475
commit 50fd31c070
2 changed files with 55 additions and 8 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

@@ -617,6 +617,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);
@@ -750,7 +769,7 @@ export async function runCronIsolatedAgentTurn(params: {
if (activeSubagentRuns > 0) {
// Parent orchestration is still in progress; avoid announcing a partial
// update to the main requester.
return withRunSession({ status: "ok", summary, outputText, ...telemetry });
return resolveRunOutcome();
}
if (
(hadActiveDescendants || expectedSubagentFollowup) &&
@@ -760,10 +779,10 @@ export async function runCronIsolatedAgentTurn(params: {
) {
// Descendants existed but no post-orchestration synthesis arrived, so
// suppress stale parent text like "on it, pulling everything together".
return withRunSession({ status: "ok", summary, outputText, ...telemetry });
return resolveRunOutcome();
}
if (synthesizedText.toUpperCase() === SILENT_REPLY_TOKEN.toUpperCase()) {
return withRunSession({ status: "ok", summary, outputText, delivered: true, ...telemetry });
return resolveRunOutcome({ delivered: true });
}
try {
if (isAborted()) {
@@ -790,7 +809,12 @@ export async function runCronIsolatedAgentTurn(params: {
waitForCompletion: false,
startedAt: runStartedAt,
endedAt: runEndedAt,
outcome: { status: "ok" },
outcome: hasErrorPayload
? {
status: "error",
error: embeddedRunError ?? "cron isolated run returned an error payload",
}
: { status: "ok" },
announceType: "cron job",
signal: abortSignal,
});
@@ -829,7 +853,7 @@ export async function runCronIsolatedAgentTurn(params: {
return failDeliveryTarget(resolvedDelivery.error.message);
}
logWarn(`[cron:${params.job.id}] ${resolvedDelivery.error.message}`);
return withRunSession({ status: "ok", summary, outputText, ...telemetry });
return resolveRunOutcome();
}
// Route text-only cron announce output back through the main session so it
@@ -855,5 +879,5 @@ export async function runCronIsolatedAgentTurn(params: {
}
}
return withRunSession({ status: "ok", summary, outputText, delivered, ...telemetry });
return resolveRunOutcome({ delivered });
}