fix(sync): מיפוי-הסטטוסים נגזר מ-/api/status-model, וסריקה בכל החברות (legal-ai #604)
שלושה כשלים בג'וב sync-case-status, שהשביתו אותו בשקט מאז 22.7: 1. המפה הקשיחה CASE_STATUS_TO_ISSUE_STATUS נותקה מ-case_status_model.py (מקור-האמת בצד legal-ai): חסרו analyst_verified ו-research_complete שנוספו ב-851a4fb, ונשארו בה 3 מפתחות מתים (uploading/brainstorming/drafting). סטטוס חסר נפל ל-`continue` — בלי לוג ובלי שגיאה. כעת המיפוי נגזר מ- GET /api/status-model דרך resolveIssueStatus/labelFor: terminal→done, הסטטוס הראשון בסדר→todo, השאר→in_progress. סטטוס חדש בצד legal-ai לא דורש עוד עריכה כאן (G2 — מקור-אמת אחד). 2. companies[0] בלבד — מתוך שתי החברות, אחת מעולם לא נסרקה (נמדד: 135 מול 67 issues מקושרי-תיק). כעת נסרקות כל החברות, וה-companyId של המועמד שנבחר הוא זה שנכתב אליו — לא החברה הראשונה גורפת. 3. דילוג שקט על סטטוס לא-מוכר → ctx.logger.warn עם שם-הסטטוס ומספר-התיק. אימות מול השרת החי: 0 סטיות מול המפה הישנה על כל 10 הסטטוסים שכיסתה, +2 שכוסו לראשונה. סימולציה על הנתונים החיים: דילוג-שקט 3→0, שורות-לוג 3→6, כתיבות בפועל 0→0 — כלומר אין נסיגה ל-flip-flop של #446. לוגיקת בחירת-היעד (#446) לא שונתה: pickSyncTargetIssue, isWritableStatus, CLOSED_ISSUE_STATUSES ו-NON_WRITABLE_STATUSES זהות; ל-SyncCandidate נוסף שדה companyId בלבד (passthrough). 8 הטסטים הקיימים עוברים ללא שינוי באסרשנים, +7 חדשים. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
113
src/worker.ts
113
src/worker.ts
@@ -4,7 +4,12 @@ import type {
|
||||
} from "@paperclipai/plugin-sdk";
|
||||
import { definePlugin, runWorker } from "@paperclipai/plugin-sdk";
|
||||
import { LegalApi } from "./legal-api.js";
|
||||
import { pickSyncTargetIssue, type SyncCandidate } from "./sync-target.js";
|
||||
import {
|
||||
labelFor,
|
||||
pickSyncTargetIssue,
|
||||
resolveIssueStatus,
|
||||
type SyncCandidate,
|
||||
} from "./sync-target.js";
|
||||
|
||||
// Hoisted so onWebhook can access the context after setup() completes.
|
||||
let pluginCtx: PluginContext | null = null;
|
||||
@@ -19,43 +24,6 @@ const CEO_AGENT_IDS: Record<string, string> = {
|
||||
|
||||
const DEFAULT_LEGAL_API_BASE = "http://localhost:8085";
|
||||
|
||||
// Map 13 legal-ai case statuses to Paperclip issue status (used by the
|
||||
// `sync-case-status` job).
|
||||
const CASE_STATUS_TO_ISSUE_STATUS: Record<
|
||||
string,
|
||||
"todo" | "in_progress" | "done"
|
||||
> = {
|
||||
new: "todo",
|
||||
uploading: "todo",
|
||||
processing: "in_progress",
|
||||
documents_ready: "in_progress",
|
||||
outcome_set: "in_progress",
|
||||
brainstorming: "in_progress",
|
||||
direction_approved: "in_progress",
|
||||
drafting: "in_progress",
|
||||
qa_review: "in_progress",
|
||||
drafted: "in_progress",
|
||||
exported: "in_progress",
|
||||
reviewed: "in_progress",
|
||||
final: "done",
|
||||
};
|
||||
|
||||
const CASE_STATUS_LABELS: Record<string, string> = {
|
||||
new: "תיק חדש",
|
||||
uploading: "העלאת מסמכים",
|
||||
processing: "עיבוד מסמכים",
|
||||
documents_ready: "מסמכים מוכנים — הזן תוצאה",
|
||||
outcome_set: "תוצאה הוזנה — נדרש סיעור מוחות",
|
||||
brainstorming: "גיבוש כיוון בתהליך",
|
||||
direction_approved: "כיוון אושר — מוכן לכתיבה",
|
||||
drafting: "כתיבת החלטה בתהליך",
|
||||
qa_review: "בדיקת איכות",
|
||||
drafted: "טיוטה מוכנה — בדוק ושלח לדפנה",
|
||||
exported: "DOCX נוצר — ממתין לדפנה",
|
||||
reviewed: "דפנה הגיהה — העלה גרסה סופית",
|
||||
final: "גרסה סופית — לולאת למידה",
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolve `legalApiBaseUrl` from company-scoped plugin config.
|
||||
*
|
||||
@@ -879,32 +847,48 @@ const plugin = definePlugin({
|
||||
const cases = await api.listCases();
|
||||
const companies = await ctx.companies.list();
|
||||
if (!companies.length) return;
|
||||
const statusModel = await api.getStatusModel();
|
||||
|
||||
const companyId = companies[0].id;
|
||||
const issues = await ctx.issues.list({ companyId });
|
||||
|
||||
// מעבר אחד על ה-issues: state.get אחד ל-issue (לא לכל צירוף תיק×issue),
|
||||
// וקיבוץ למפה case_number → מועמדים. הקיבוץ נדרש כדי לבחור "השורש היחיד".
|
||||
// מעבר אחד על ה-issues של כל חברה: state.get אחד ל-issue (לא לכל
|
||||
// צירוף תיק×issue), וקיבוץ למפה case_number → מועמדים. הקיבוץ נדרש
|
||||
// כדי לבחור "השורש היחיד". סורק את **כל** החברות (לא רק הראשונה) —
|
||||
// legal-ai issue #604.
|
||||
const byCase = new Map<string, SyncCandidate[]>();
|
||||
for (const issue of issues) {
|
||||
const linkedCase = await ctx.state.get({
|
||||
scopeKind: "issue",
|
||||
scopeId: issue.id,
|
||||
stateKey: "legal-case-number",
|
||||
});
|
||||
if (typeof linkedCase !== "string" || !linkedCase) continue;
|
||||
const list = byCase.get(linkedCase) ?? [];
|
||||
list.push({
|
||||
id: issue.id,
|
||||
status: issue.status,
|
||||
parentId: issue.parentId,
|
||||
});
|
||||
byCase.set(linkedCase, list);
|
||||
for (const company of companies) {
|
||||
const issues = await ctx.issues.list({ companyId: company.id });
|
||||
for (const issue of issues) {
|
||||
const linkedCase = await ctx.state.get({
|
||||
scopeKind: "issue",
|
||||
scopeId: issue.id,
|
||||
stateKey: "legal-case-number",
|
||||
});
|
||||
if (typeof linkedCase !== "string" || !linkedCase) continue;
|
||||
const list = byCase.get(linkedCase) ?? [];
|
||||
list.push({
|
||||
id: issue.id,
|
||||
status: issue.status,
|
||||
parentId: issue.parentId,
|
||||
companyId: company.id,
|
||||
});
|
||||
byCase.set(linkedCase, list);
|
||||
}
|
||||
}
|
||||
|
||||
for (const legalCase of cases) {
|
||||
const targetStatus = CASE_STATUS_TO_ISSUE_STATUS[legalCase.status];
|
||||
if (!targetStatus) continue;
|
||||
const targetStatus = resolveIssueStatus(
|
||||
statusModel.statuses,
|
||||
legalCase.status,
|
||||
);
|
||||
if (targetStatus === null) {
|
||||
ctx.logger.warn(
|
||||
"sync-case-status: unknown case status — not in /api/status-model (case_status_model.py)",
|
||||
{
|
||||
caseNumber: legalCase.case_number,
|
||||
status: legalCase.status,
|
||||
},
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
const candidates = byCase.get(legalCase.case_number) ?? [];
|
||||
const { target, reason, writableRoots } =
|
||||
@@ -924,14 +908,19 @@ const plugin = definePlugin({
|
||||
if (target.status === targetStatus) continue;
|
||||
|
||||
const label =
|
||||
CASE_STATUS_LABELS[legalCase.status] || legalCase.status;
|
||||
labelFor(statusModel.statuses, legalCase.status) ??
|
||||
legalCase.status;
|
||||
|
||||
await ctx.issues.update(
|
||||
target.id,
|
||||
{ status: targetStatus },
|
||||
companyId,
|
||||
target.companyId,
|
||||
);
|
||||
await ctx.issues.createComment(
|
||||
target.id,
|
||||
`📋 ${label}`,
|
||||
target.companyId,
|
||||
);
|
||||
await ctx.issues.createComment(target.id, `📋 ${label}`, companyId);
|
||||
ctx.logger.info("Synced issue status", {
|
||||
issueId: target.id,
|
||||
caseNumber: legalCase.case_number,
|
||||
|
||||
Reference in New Issue
Block a user