From e324240b7e106cb97803e88afc99f69b3cf173f2 Mon Sep 17 00:00:00 2001 From: Chaim Marcus Date: Sat, 24 Oct 2026 19:06:26 +0300 Subject: [PATCH] =?UTF-8?q?fix(jobs):=20stale/weekly=20=D7=93=D7=A8=D7=9A?= =?UTF-8?q?=20fetch=20=D7=A9=D7=9C=20=D7=94-worker,=20=D7=95-sync-case-sta?= =?UTF-8?q?tus=20=D7=93=D7=A8=D7=9A=20status-index=20(legal-ai=20#1117)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ctx.http.fetch נחסם ע"י הגנת-SSRF של Paperclip כלפי 127.0.0.1:8001 (stale-case-reminder, weekly-feedback-analysis); sync-case-status קיבל 401 על GET /api/cases (session-only) ועובר לנתיב המינימלי /api/cases/status-index שנוסף ל-legal-ai (PR #1118). Co-Authored-By: Claude Sonnet 5.5 --- src/legal-api.ts | 14 ++++++++++++++ src/worker-fetch.test.ts | 24 ++++++++++++++++++++++++ src/worker-fetch.ts | 16 ++++++++++++++++ src/worker.ts | 9 +++++---- 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/worker-fetch.test.ts create mode 100644 src/worker-fetch.ts diff --git a/src/legal-api.ts b/src/legal-api.ts index 24d6e38..5a72dfb 100644 --- a/src/legal-api.ts +++ b/src/legal-api.ts @@ -36,6 +36,15 @@ export class LegalApi { return this.request("/api/cases"); } + /** + * Minimal `{case_number, status}` index for `sync-case-status` (legal-ai #1117). + * Lives on the internal machine route; the fuller `/api/cases` listing does not + * (session-only), so the sync job must not call `listCases()`. + */ + async listCaseStatuses(): Promise { + return this.request("/api/cases/status-index"); + } + async getStatusModel(): Promise { return this.request("/api/status-model"); } @@ -182,6 +191,11 @@ export class LegalApi { // Types +export interface CaseStatusEntry { + case_number: string; + status: string; +} + export interface CaseSummary { case_number: string; title: string; diff --git a/src/worker-fetch.test.ts b/src/worker-fetch.test.ts new file mode 100644 index 0000000..7cfe12b --- /dev/null +++ b/src/worker-fetch.test.ts @@ -0,0 +1,24 @@ +/// + +import assert from "node:assert/strict"; +import { test } from "node:test"; +// Excluded from tsc (tsconfig.json), run natively via `node --test` (literal `.ts`). +import { fetchLegalApi } from "./worker-fetch.ts"; + +test("fetchLegalApi uses the worker's global fetch (not the host-mediated ctx.http.fetch)", async () => { + const original = globalThis.fetch; + let called = ""; + globalThis.fetch = (async (url: string) => { + called = url; + return new Response("{}", { status: 200 }); + }) as typeof fetch; + try { + const resp = await fetchLegalApi( + "http://127.0.0.1:8001/api/cases/stale?days=30", + ); + assert.equal(resp.ok, true); + assert.equal(called, "http://127.0.0.1:8001/api/cases/stale?days=30"); + } finally { + globalThis.fetch = original; + } +}); diff --git a/src/worker-fetch.ts b/src/worker-fetch.ts new file mode 100644 index 0000000..73d9d5f --- /dev/null +++ b/src/worker-fetch.ts @@ -0,0 +1,16 @@ +/** + * `fetch` of the worker process — used by the scheduled jobs that read legal-ai's + * internal-machine routes at `http://127.0.0.1:8001`. `ctx.http.fetch` is host-mediated + * and Paperclip's SSRF guard rejects loopback/private targets ("All resolved IPs for + * 127.0.0.1 are in private/reserved ranges"), which is why `stale-case-reminder` and + * `weekly-feedback-analysis` failed (legal-ai #1117). `LegalApi` already uses this fetch. + * + * Kept in its own module (no TypeScript parameter properties) so `node --test` can + * load it in strip-only mode. + */ +export function fetchLegalApi( + url: string, + init?: RequestInit, +): Promise { + return fetch(url, init); +} diff --git a/src/worker.ts b/src/worker.ts index 20fb5f5..f732762 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -35,6 +35,7 @@ import { resolveStatusLabel, type SyncCandidate, } from "./sync-target.js"; +import { fetchLegalApi } from "./worker-fetch.js"; // Hoisted so onWebhook can access the context after setup() completes. let pluginCtx: PluginContext | null = null; @@ -972,7 +973,7 @@ const plugin = definePlugin({ // כשה-scope נדחה (legal-ai issue #637). ה-`try/catch` הישן כאן בלע // את הכשל, וה-run נרשם `succeeded` בלי לעשות כלום — 13,928 ריצות כוזבות. await runJobHandler("sync-case-status", ctx.logger, async () => { - const cases = await api.listCases(); + const cases = await api.listCaseStatuses(); // נקבע מיד — לפני כל קריאת-מארח שעלולה ליפול. אחרת ריצה // שנפלה באמצע (או `!companies.length`) הייתה מדווחת scanned=0 // ומאבדת בדיוק את האבחנה שה-issue הזה נועד לתת. @@ -1090,9 +1091,9 @@ const plugin = definePlugin({ // כשל-fetch/API אמיתי חייב **לזרוק**, לא `return` בשקט — אחרת // ה-run נרשם `succeeded` בלי שקרה כלום (legal-ai issue #637). - let resp: Awaited>; + let resp: Response; try { - resp = await ctx.http.fetch(`${apiBase}/api/cases/stale?days=30`); + resp = await fetchLegalApi(`${apiBase}/api/cases/stale?days=30`); } catch (err) { throw new Error( `stale-case-reminder: fetch failed against ${apiBase}: ${String(err)}`, @@ -1169,7 +1170,7 @@ const plugin = definePlugin({ // throws CompanyScopeUnavailableError rather than guessing. const apiBase = await resolveApiBase(ctx); - const resp = await ctx.http.fetch( + const resp = await fetchLegalApi( `${apiBase}/api/chair-feedback/weekly-summary`, ); if (!resp.ok) {