fix(jobs): stale/weekly דרך fetch של ה-worker; sync-case-status דרך status-index (legal-ai #1117) #20

Merged
chaim merged 1 commits from fix/1117-job-fetch into main 2026-10-24 16:09:23 +00:00
4 changed files with 59 additions and 4 deletions

View File

@@ -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<CaseStatusEntry[]> {
return this.request("/api/cases/status-index");
}
async getStatusModel(): Promise<StatusModel> {
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;

24
src/worker-fetch.test.ts Normal file
View File

@@ -0,0 +1,24 @@
/// <reference types="node" />
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;
}
});

16
src/worker-fetch.ts Normal file
View File

@@ -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<Response> {
return fetch(url, init);
}

View File

@@ -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<ReturnType<typeof ctx.http.fetch>>;
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) {