From 73e37df12905182d001fe43d349c23199d830a2e Mon Sep 17 00:00:00 2001 From: Chaim Marcus Date: Sun, 17 May 2026 11:07:54 +0000 Subject: [PATCH] fix: add try-catch on agents.invoke and http.fetch in scheduled jobs - stale-case-reminder: wrap http.fetch in try-catch so network errors log and return instead of crashing the job silently - weekly-feedback-analysis: wrap agents.invoke in try-catch so CEO unavailability logs and returns instead of crashing the job Co-Authored-By: Claude Sonnet 4.6 --- src/worker.ts | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/worker.ts b/src/worker.ts index 0e6590d..8c07e45 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -713,7 +713,15 @@ const plugin = definePlugin({ const apiBase = (config.legalApiBaseUrl as string) ?? "http://localhost:8085"; - const resp = await ctx.http.fetch(`${apiBase}/api/cases/stale?days=3`); + let resp: Awaited>; + try { + resp = await ctx.http.fetch(`${apiBase}/api/cases/stale?days=3`); + } catch (err) { + ctx.logger.error("stale-case-reminder: fetch failed", { + error: String(err), + }); + return; + } if (!resp.ok) { ctx.logger.error(`stale-case-reminder: API error ${resp.status}`); return; @@ -817,14 +825,21 @@ const plugin = definePlugin({ const { company, ceoId } = mapped[0]; - await ctx.agents.invoke(ceoId, company.id, { - prompt: `ניתוח פידבק שבועי יו"ר (${data.entry_count} פריטים):\n\n${data.summary}\n\nהמשימה: עדכן את /home/chaim/legal-ai/docs/legal-decision-lessons.md עם הלקחים החדשים שעולים מהפידבק. הוסף רק לקחים חדשים שלא קיימים כבר. קבץ לפי נושא.`, - reason: "weekly-feedback-job", - }); - - ctx.logger.info( - `weekly-feedback-analysis: invoked CEO ${ceoId} (company ${company.id}) with ${data.entry_count} feedback entries`, - ); + try { + await ctx.agents.invoke(ceoId, company.id, { + prompt: `ניתוח פידבק שבועי יו"ר (${data.entry_count} פריטים):\n\n${data.summary}\n\nהמשימה: עדכן את /home/chaim/legal-ai/docs/legal-decision-lessons.md עם הלקחים החדשים שעולים מהפידבק. הוסף רק לקחים חדשים שלא קיימים כבר. קבץ לפי נושא.`, + reason: "weekly-feedback-job", + }); + ctx.logger.info( + `weekly-feedback-analysis: invoked CEO ${ceoId} (company ${company.id}) with ${data.entry_count} feedback entries`, + ); + } catch (err) { + ctx.logger.error("weekly-feedback-analysis: failed to invoke CEO", { + error: String(err), + ceoId, + companyId: company.id, + }); + } }); ctx.logger.info("Legal AI plugin ready");