Compare commits
15 Commits
314745d7a7
...
chore/1117
| Author | SHA1 | Date | |
|---|---|---|---|
| c5a605ab1d | |||
| 517b3ee844 | |||
| 75aa931f85 | |||
| 2d4113e581 | |||
| e324240b7e | |||
| 68f85717de | |||
| 50b29d4b12 | |||
| f3b972119b | |||
| c79a7a0b9f | |||
| 2a6165ac86 | |||
| c51f8c36e7 | |||
| 5a5fdd9162 | |||
| 76db4aadb5 | |||
| c6c0b76914 | |||
| b24d816cc0 |
41
.gitea/workflows/int9-guard.yaml
Normal file
41
.gitea/workflows/int9-guard.yaml
Normal file
@@ -0,0 +1,41 @@
|
||||
name: INV-INT9 issue.status write-ownership
|
||||
|
||||
# Hard gate for INV-INT9 (legal-ai docs/spec/X7-paperclip-client-params.md
|
||||
# §4): `issue.status` has exactly one legitimate writer — the
|
||||
# `sync-case-status` scheduled job (`ctx.jobs.register("sync-case-status", …)`
|
||||
# in src/worker.ts), which mirrors legal-ai's case status onto the linked
|
||||
# Paperclip issue. Built after legal-ai issue #446, where a second, unnoticed
|
||||
# writer flip-flopped an issue `done → in_progress` — nobody caught it because
|
||||
# nothing enforced write-ownership. This gate makes sure a third writer never
|
||||
# lands the same way again: any `ctx.issues.update(...)` call that touches
|
||||
# `status` outside the `sync-case-status` job fails the build, unless it
|
||||
# carries an explicit, justified `// noqa: INT9 — <reason>` (a bare noqa with
|
||||
# no reason is itself a violation — exceptions must be argued in writing).
|
||||
#
|
||||
# Pure Node, zero dependencies (scripts/int9-guard.mjs) — no `npm ci` needed.
|
||||
#
|
||||
# ⚠️ חסום למיזוג. אין היום runner ל-Gitea Actions בהיקף הריפו הזה: ה-runner
|
||||
# היחיד על nautilus רשום בהיקף-repo ל-`legal-ai` בלבד (`action_runner.repo_id = 6`),
|
||||
# ולכן job שנפתח כאן לא נאסף לעולם — נמדד ב-legal-ai #618: ריצה 3200 / job 3250
|
||||
# ישבה ב-`queued` עם `started_at: 1970-01-01` בזמן שה-runner היה `busy: false`.
|
||||
# ה-PR הזה נשאר פתוח **בכוונה** ומשמש כמדידה: ביום שיירשם runner לריפו הזה הוא
|
||||
# יהפוך לירוק מעצמו, וזו תהיה הראיה ש-AC2 של #618 סופק. עד אז השער רץ ידנית:
|
||||
# `npm run int9:guard` · `npm run int9:guard:self-test`.
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [main]
|
||||
push:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
int9-guard:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: INV-INT9 — issue.status write-ownership guard
|
||||
run: node scripts/int9-guard.mjs
|
||||
|
||||
- name: Self-test — הוכחה שהשער נושך (AC5)
|
||||
run: node scripts/int9-guard.mjs --self-test
|
||||
@@ -14,7 +14,9 @@
|
||||
"format": "biome format --write src/",
|
||||
"format:check": "biome format src/",
|
||||
"biome": "biome check src/",
|
||||
"biome:fix": "biome check --write src/"
|
||||
"biome:fix": "biome check --write src/",
|
||||
"int9:guard": "node scripts/int9-guard.mjs",
|
||||
"int9:guard:self-test": "node scripts/int9-guard.mjs --self-test"
|
||||
},
|
||||
"dependencies": {
|
||||
"@paperclipai/plugin-sdk": "^2026.722.0",
|
||||
|
||||
16
scripts/fixtures/int9/allowed-inside-job.ts
Normal file
16
scripts/fixtures/int9/allowed-inside-job.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
// פיקסצ'ר ל-scripts/int9-guard.mjs --self-test. לא קוד-ריצה.
|
||||
//
|
||||
// קריאה ל-issues.update({status}) בתוך jobs.register("sync-case-status", …)
|
||||
// — המסלול המותר היחיד. כולל תבנית עם סוגריים לא-מאוזנים בתוך תבנית-מחרוזת
|
||||
// והערה, כדי לבחון בפועל את הטוקנייזר (מחרוזת/הערה לא נספרות ב-matchParen).
|
||||
|
||||
export function register(ctx: any, api: any) {
|
||||
ctx.jobs.register("sync-case-status", async (job: unknown) => {
|
||||
// סוגר בודד בהערה — לא אמור לבלבל את חישוב-הטווח: )
|
||||
const label = `📋 (${api.name}`;
|
||||
|
||||
const target = await api.pickTarget();
|
||||
|
||||
await ctx.issues.update(target.id, { status: label }, target.companyId);
|
||||
});
|
||||
}
|
||||
15
scripts/fixtures/int9/non-status-update.ts
Normal file
15
scripts/fixtures/int9/non-status-update.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
// פיקסצ'ר ל-scripts/int9-guard.mjs --self-test. לא קוד-ריצה.
|
||||
//
|
||||
// שתי קריאות ל-issues.update שאינן נוגעות ב-status כלל — לא הפרה.
|
||||
// השנייה בודקת שגבול-מילה אמיתי: "statusLabel" אינו תואם ל-/\bstatus\b/
|
||||
// (אין גבול-מילה בין ה-s של status ל-L של Label).
|
||||
|
||||
export async function renameOnly(ctx: any, target: any, statusLabel: string) {
|
||||
await ctx.issues.update(target.id, { title: "x" }, target.companyId);
|
||||
|
||||
await ctx.issues.update(
|
||||
target.id,
|
||||
{ description: statusLabel },
|
||||
target.companyId,
|
||||
);
|
||||
}
|
||||
9
scripts/fixtures/int9/noqa-bare.ts
Normal file
9
scripts/fixtures/int9/noqa-bare.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
// פיקסצ'ר ל-scripts/int9-guard.mjs --self-test. לא קוד-ריצה.
|
||||
//
|
||||
// קריאה ל-issues.update({status}) מחוץ ל-sync-case-status, עם noqa בלי נימוק —
|
||||
// עדיין הפרה (AC3: חריגה חייבת להיות מנומקת בכתב).
|
||||
|
||||
export async function sloppyBackfill(ctx: any, target: any) {
|
||||
// noqa: INT9
|
||||
await ctx.issues.update(target.id, { status: "done" }, target.companyId);
|
||||
}
|
||||
9
scripts/fixtures/int9/noqa-justified.ts
Normal file
9
scripts/fixtures/int9/noqa-justified.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
// פיקסצ'ר ל-scripts/int9-guard.mjs --self-test. לא קוד-ריצה.
|
||||
//
|
||||
// קריאה ל-issues.update({status}) מחוץ ל-sync-case-status, עם חריגה מנומקת —
|
||||
// מותר.
|
||||
|
||||
export async function migrationBackfill(ctx: any, target: any) {
|
||||
// noqa: INT9 — מיגרציה חד-פעמית לתיקון סטטוסים תקועים מלפני #618, מאושרת ע"י דפנה
|
||||
await ctx.issues.update(target.id, { status: "done" }, target.companyId);
|
||||
}
|
||||
16
scripts/fixtures/int9/violating.ts
Normal file
16
scripts/fixtures/int9/violating.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
// פיקסצ'ר ל-scripts/int9-guard.mjs --self-test. לא קוד-ריצה.
|
||||
//
|
||||
// קריאה ל-issues.update({status}) מחוץ לכל jobs.register("sync-case-status", …)
|
||||
// — זו בדיוק ההפרה שהשער אמור לתפוס.
|
||||
|
||||
export async function badHandler(ctx: any, target: any) {
|
||||
await ctx.jobs.register("stale-case-reminder", async (_job: unknown) => {
|
||||
ctx.logger.info("stale-case-reminder: starting");
|
||||
});
|
||||
|
||||
await ctx.issues.update(
|
||||
target.id,
|
||||
{ status: "in_progress" },
|
||||
target.companyId,
|
||||
);
|
||||
}
|
||||
546
scripts/int9-guard.mjs
Normal file
546
scripts/int9-guard.mjs
Normal file
@@ -0,0 +1,546 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* INV-INT9 guard — enforce single write-ownership of `issue.status`
|
||||
* (legal-ai docs/spec/X7-paperclip-client-params.md §4 INV-INT9).
|
||||
*
|
||||
* `issues.status` has exactly one legitimate writer in this plugin: the
|
||||
* `sync-case-status` scheduled job (`ctx.jobs.register("sync-case-status", …)`
|
||||
* in `src/worker.ts`), which mirrors legal-ai's case status onto the linked
|
||||
* Paperclip issue. Any OTHER `ctx.issues.update(...)` call that touches
|
||||
* `status` is a second writer racing the sync job — exactly the flip-flop
|
||||
* (`done` → `in_progress`) bug class legal-ai issue #618 exists to close off
|
||||
* for good, before a second call site is ever added.
|
||||
*
|
||||
* IN SCOPE: every `.ts`/`.tsx` file under `src/` (including `*.test.ts` —
|
||||
* there is no false positive there today, and the noqa escape hatch below
|
||||
* covers the day there is one).
|
||||
*
|
||||
* OUT OF SCOPE: this is NOT a general call-graph analysis. It is a
|
||||
* structural, single-pass lexer over the source text — no type information,
|
||||
* no cross-file resolution. It answers exactly one question per call site:
|
||||
* "does the character range of this `ctx.issues.update(...)` call sit
|
||||
* inside the character range of the one whitelisted
|
||||
* `ctx.jobs.register("sync-case-status", …)` call?". Deliberately NOT a
|
||||
* per-file allowlist (the pattern `scripts/leak_guard.py` in legal-ai uses)
|
||||
* — that shape does not fit here, because the one legitimate call site and
|
||||
* every future illegitimate one live in the very same file (`worker.ts`).
|
||||
* The granularity has to be a code RANGE derived from the real structure,
|
||||
* not a file name.
|
||||
*
|
||||
* Escape hatch (G1 — fix at the source, don't paper over a false positive):
|
||||
* a call can be justified in place with
|
||||
* // noqa: INT9 — <reason, at least 3 characters>
|
||||
* on the line immediately above the call, or on any line spanned by its
|
||||
* argument list. A bare `// noqa: INT9` with no reason is itself a
|
||||
* violation (AC3 — exceptions must be justified in writing, not silent).
|
||||
*
|
||||
* Usage:
|
||||
* node scripts/int9-guard.mjs # scan src/**\/*.{ts,tsx}; exit 1 on any violation
|
||||
* node scripts/int9-guard.mjs <path>... # scan only the given files/directories
|
||||
* node scripts/int9-guard.mjs --self-test # run the fixture suite (scripts/fixtures/int9/), prove the gate bites
|
||||
*
|
||||
* NOT WIRED INTO CI YET: the Gitea Actions runner on this server is
|
||||
* registered at *repository* scope for `legal-ai` only
|
||||
* (`action_runner.repo_id = 6`), so any job queued for `plugin-legal-ai` is
|
||||
* never dispatched — measured on legal-ai issue #618, run 3200 / job 3250
|
||||
* sat in `queued` with `started_at: 1970-01-01`. Until a runner is
|
||||
* registered for this repo, run this by hand:
|
||||
* npm run int9:guard
|
||||
* npm run int9:guard:self-test
|
||||
* The workflow file itself (`.gitea/workflows/int9-guard.yaml`) lives in a
|
||||
* separate, deliberately-blocked PR, so merging it does not leave a
|
||||
* permanently-pending check on every future PR in this repo.
|
||||
*
|
||||
* Zero dependencies (node:fs / node:path / node:process only) — no `npm ci`
|
||||
* needed in CI; the runner image ships node 24.
|
||||
*/
|
||||
import { readdirSync, readFileSync, statSync } from "node:fs";
|
||||
import { join, relative, resolve } from "node:path";
|
||||
import process from "node:process";
|
||||
|
||||
const REPO_ROOT = resolve(import.meta.dirname, "..");
|
||||
const SKIP_DIRS = new Set(["node_modules", "dist", ".git"]);
|
||||
const SOURCE_EXT = new Set([".ts", ".tsx"]);
|
||||
|
||||
const SPEC_REF = "docs/spec/X7-paperclip-client-params.md §4 INV-INT9";
|
||||
|
||||
const NOQA_RE = /\/\/\s*noqa\s*:\s*INT9\b(?:\s*[-—]\s*(.*))?/i;
|
||||
|
||||
const REGEX_PREV_CHARS = new Set("(,=:[!&|?{};+-*%~^<>".split(""));
|
||||
const REGEX_PREV_WORDS = new Set([
|
||||
"return",
|
||||
"typeof",
|
||||
"case",
|
||||
"in",
|
||||
"of",
|
||||
"new",
|
||||
"delete",
|
||||
"void",
|
||||
"instanceof",
|
||||
"do",
|
||||
"else",
|
||||
"yield",
|
||||
"await",
|
||||
]);
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Tokenizer — classify every character as "plain code" or not, so that
|
||||
// structural regexes and paren-matching never see string/template
|
||||
// contents, comments, or regex-literal bodies.
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/** @returns {Uint8Array} mask[i] === 1 iff text[i] is plain code. */
|
||||
function classify(text) {
|
||||
const n = text.length;
|
||||
const mask = new Uint8Array(n);
|
||||
/** @type {Array<{kind: "template"} | {kind: "exprCode", depth: number}>} */
|
||||
const stack = [];
|
||||
let state = "code";
|
||||
let lastSignificant = "";
|
||||
let lastWord = "";
|
||||
let i = 0;
|
||||
|
||||
function noteCodeChar(ch) {
|
||||
if (/\s/.test(ch)) return;
|
||||
lastSignificant = ch;
|
||||
if (/[A-Za-z0-9_$]/.test(ch)) {
|
||||
lastWord += ch;
|
||||
} else {
|
||||
lastWord = "";
|
||||
}
|
||||
}
|
||||
|
||||
while (i < n) {
|
||||
const ch = text[i];
|
||||
|
||||
if (state === "code") {
|
||||
const next = text[i + 1];
|
||||
if (ch === "/" && next === "/") {
|
||||
state = "lcomment";
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (ch === "/" && next === "*") {
|
||||
state = "bcomment";
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (ch === "'") {
|
||||
state = "squote";
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if (ch === '"') {
|
||||
state = "dquote";
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if (ch === "`") {
|
||||
stack.push({ kind: "template" });
|
||||
state = "template";
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if (ch === "/") {
|
||||
const isRegex =
|
||||
lastSignificant === "" ||
|
||||
REGEX_PREV_CHARS.has(lastSignificant) ||
|
||||
REGEX_PREV_WORDS.has(lastWord);
|
||||
if (isRegex) {
|
||||
state = "regex";
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
// else: division — falls through to plain-code handling below.
|
||||
}
|
||||
|
||||
mask[i] = 1;
|
||||
const top = stack[stack.length - 1];
|
||||
if (ch === "{" && top && top.kind === "exprCode") {
|
||||
top.depth += 1;
|
||||
} else if (ch === "}" && top && top.kind === "exprCode") {
|
||||
top.depth -= 1;
|
||||
if (top.depth === 0) {
|
||||
stack.pop();
|
||||
state = "template";
|
||||
}
|
||||
}
|
||||
noteCodeChar(ch);
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (state === "squote" || state === "dquote") {
|
||||
const quote = state === "squote" ? "'" : '"';
|
||||
if (ch === "\\") {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (ch === quote) {
|
||||
state = "code";
|
||||
noteCodeChar(quote);
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if (ch === "\n") {
|
||||
// Unterminated string — bail back to code rather than eat the file.
|
||||
state = "code";
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (state === "template") {
|
||||
if (ch === "\\") {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (ch === "`") {
|
||||
stack.pop();
|
||||
state = "code";
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if (ch === "$" && text[i + 1] === "{") {
|
||||
stack.push({ kind: "exprCode", depth: 1 });
|
||||
state = "code";
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (state === "lcomment") {
|
||||
if (ch === "\n") {
|
||||
state = "code";
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (state === "bcomment") {
|
||||
if (ch === "*" && text[i + 1] === "/") {
|
||||
state = "code";
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (state === "regex") {
|
||||
if (ch === "\\") {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (ch === "[") {
|
||||
state = "regexclass";
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if (ch === "/") {
|
||||
i += 1;
|
||||
while (i < n && /[a-z]/i.test(text[i])) i += 1;
|
||||
state = "code";
|
||||
continue;
|
||||
}
|
||||
if (ch === "\n") {
|
||||
// Unterminated regex — bail back to code rather than eat the file.
|
||||
state = "code";
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (state === "regexclass") {
|
||||
if (ch === "\\") {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (ch === "]") {
|
||||
state = "regex";
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Unreachable, but never silently drop a character.
|
||||
i += 1;
|
||||
}
|
||||
|
||||
return mask;
|
||||
}
|
||||
|
||||
/** Yield regex matches whose full span lies entirely in plain-code region. */
|
||||
function* matchAllCode(text, mask, pattern) {
|
||||
for (const m of text.matchAll(pattern)) {
|
||||
const start = m.index;
|
||||
const end = start + m[0].length;
|
||||
let allCode = true;
|
||||
for (let i = start; i < end; i++) {
|
||||
if (!mask[i]) {
|
||||
allCode = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (allCode) yield m;
|
||||
}
|
||||
}
|
||||
|
||||
/** Index of the `)` matching the `(` at `openIndex`, counting only plain-code parens. */
|
||||
function matchParen(text, mask, openIndex, filePath) {
|
||||
let depth = 0;
|
||||
for (let i = openIndex; i < text.length; i++) {
|
||||
if (!mask[i]) continue;
|
||||
if (text[i] === "(") depth += 1;
|
||||
else if (text[i] === ")") {
|
||||
depth -= 1;
|
||||
if (depth === 0) return i;
|
||||
}
|
||||
}
|
||||
throw new Error(`${filePath}: פענוח נכשל — סוגר לא נסגר`);
|
||||
}
|
||||
|
||||
function buildLineStarts(text) {
|
||||
const starts = [0];
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
if (text[i] === "\n") starts.push(i + 1);
|
||||
}
|
||||
return starts;
|
||||
}
|
||||
|
||||
function lineOfOffset(lineStarts, idx) {
|
||||
let lo = 0;
|
||||
let hi = lineStarts.length - 1;
|
||||
while (lo < hi) {
|
||||
const mid = (lo + hi + 1) >> 1;
|
||||
if (lineStarts[mid] <= idx) lo = mid;
|
||||
else hi = mid - 1;
|
||||
}
|
||||
return lo; // 0-based line number
|
||||
}
|
||||
|
||||
const STATUS_WORD_RE = /\bstatus\b/;
|
||||
const JOBS_REGISTER_RE = /jobs\.register\s*\(/g;
|
||||
const ISSUES_UPDATE_RE = /issues\.update\s*\(/g;
|
||||
const SYNC_CASE_STATUS_LITERAL_RE = /^\s*["']sync-case-status["']/;
|
||||
|
||||
/**
|
||||
* Scan one file's source text and return a list of violation objects
|
||||
* ({ file, line, message }). Throws if a `(` is never closed (see
|
||||
* matchParen) — the caller must NOT treat that as "no violations".
|
||||
*/
|
||||
function scanFile(filePath, text) {
|
||||
const mask = classify(text);
|
||||
const lines = text.split("\n");
|
||||
const lineStarts = buildLineStarts(text);
|
||||
const rel = filePath;
|
||||
|
||||
const allowedRanges = [];
|
||||
for (const m of matchAllCode(text, mask, JOBS_REGISTER_RE)) {
|
||||
const openIdx = m.index + m[0].length - 1;
|
||||
const after = text.slice(openIdx + 1, openIdx + 60);
|
||||
if (SYNC_CASE_STATUS_LITERAL_RE.test(after)) {
|
||||
const closeIdx = matchParen(text, mask, openIdx, rel);
|
||||
allowedRanges.push([openIdx, closeIdx]);
|
||||
}
|
||||
}
|
||||
|
||||
const violations = [];
|
||||
for (const m of matchAllCode(text, mask, ISSUES_UPDATE_RE)) {
|
||||
const openIdx = m.index + m[0].length - 1;
|
||||
const closeIdx = matchParen(text, mask, openIdx, rel);
|
||||
const content = text.slice(openIdx + 1, closeIdx);
|
||||
if (!STATUS_WORD_RE.test(content)) continue; // not a status-write call
|
||||
|
||||
const inAllowedRange = allowedRanges.some(
|
||||
([s, e]) => openIdx >= s && openIdx <= e,
|
||||
);
|
||||
if (inAllowedRange) continue;
|
||||
|
||||
const callStartLine = lineOfOffset(lineStarts, m.index);
|
||||
const callEndLine = lineOfOffset(lineStarts, closeIdx);
|
||||
|
||||
let noqaMatch;
|
||||
for (let ln = callStartLine - 1; ln <= callEndLine && !noqaMatch; ln += 1) {
|
||||
if (ln < 0 || ln >= lines.length) continue;
|
||||
const m2 = NOQA_RE.exec(lines[ln]);
|
||||
if (m2) noqaMatch = m2;
|
||||
}
|
||||
|
||||
const reportLine = callStartLine + 1; // 1-based for humans
|
||||
if (noqaMatch) {
|
||||
const reason = (noqaMatch[1] ?? "").trim();
|
||||
if (reason.length >= 3) continue; // justified exception — allowed
|
||||
violations.push({
|
||||
file: rel,
|
||||
line: reportLine,
|
||||
message: "noqa: INT9 ללא נימוק — חריגה חייבת להיות מנומקת בכתב (AC3)",
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
violations.push({
|
||||
file: rel,
|
||||
line: reportLine,
|
||||
message:
|
||||
`issues.update({status}) מחוץ למסלול sync-case-status (INV-INT9). ` +
|
||||
`ראה ${SPEC_REF}. חריגה מוצדקת → // noqa: INT9 — <נימוק>.`,
|
||||
});
|
||||
}
|
||||
|
||||
return violations;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// File collection
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
function walk(dir, out) {
|
||||
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
||||
if (SKIP_DIRS.has(entry.name)) continue;
|
||||
const full = join(dir, entry.name);
|
||||
if (entry.isDirectory()) {
|
||||
walk(full, out);
|
||||
} else if (SOURCE_EXT.has(entry.name.slice(entry.name.lastIndexOf(".")))) {
|
||||
out.push(full);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function collectDefault() {
|
||||
const srcDir = join(REPO_ROOT, "src");
|
||||
return walk(srcDir, []);
|
||||
}
|
||||
|
||||
function collectFromArgs(args) {
|
||||
const out = [];
|
||||
for (const a of args) {
|
||||
const p = resolve(a);
|
||||
const st = statSync(p);
|
||||
if (st.isDirectory()) {
|
||||
walk(p, out);
|
||||
} else {
|
||||
out.push(p);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function runGate(files) {
|
||||
const violations = [];
|
||||
for (const abs of files) {
|
||||
const rel = relative(REPO_ROOT, abs);
|
||||
const text = readFileSync(abs, "utf-8");
|
||||
violations.push(...scanFile(rel, text));
|
||||
}
|
||||
return violations;
|
||||
}
|
||||
|
||||
function reportGate(violations, scannedCount) {
|
||||
if (violations.length > 0) {
|
||||
process.stderr.write(
|
||||
`✗ INV-INT9 gate — issue.status write-ownership violated ` +
|
||||
`(${violations.length} finding(s)):\n\n`,
|
||||
);
|
||||
for (const v of violations) {
|
||||
process.stderr.write(` • ${v.file}:${v.line}: ${v.message}\n`);
|
||||
}
|
||||
process.stderr.write(`\nSee ${SPEC_REF}.\n`);
|
||||
return 1;
|
||||
}
|
||||
process.stdout.write(
|
||||
`✓ INV-INT9 gate: מסלול-כתיבה יחיד ל-issue.status ` +
|
||||
`(${scannedCount} קבצים נסרקו).\n`,
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Self-test
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
const SELF_TEST_EXPECTED = {
|
||||
"violating.ts": 1,
|
||||
"allowed-inside-job.ts": 0,
|
||||
"noqa-justified.ts": 0,
|
||||
"noqa-bare.ts": 1,
|
||||
"non-status-update.ts": 0,
|
||||
};
|
||||
|
||||
function runSelfTest() {
|
||||
const fixturesDir = join(REPO_ROOT, "scripts", "fixtures", "int9");
|
||||
let pass = 0;
|
||||
let total = 0;
|
||||
const failures = [];
|
||||
|
||||
for (const [name, expected] of Object.entries(SELF_TEST_EXPECTED)) {
|
||||
total += 1;
|
||||
const abs = join(fixturesDir, name);
|
||||
let violations;
|
||||
try {
|
||||
const text = readFileSync(abs, "utf-8");
|
||||
violations = scanFile(name, text);
|
||||
} catch (err) {
|
||||
failures.push(
|
||||
`${name}: ציפינו ${expected} הפרות, אבל הסריקה זרקה שגיאה: ${String(err)}`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if (violations.length === expected) {
|
||||
pass += 1;
|
||||
} else {
|
||||
failures.push(
|
||||
`${name}: ציפינו ${expected} הפרות, התקבלו ${violations.length}` +
|
||||
(violations.length
|
||||
? `:\n${violations.map((v) => ` - ${v.file}:${v.line}: ${v.message}`).join("\n")}`
|
||||
: ""),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (failures.length > 0) {
|
||||
process.stderr.write("✗ INV-INT9 self-test נכשל:\n\n");
|
||||
for (const f of failures) process.stderr.write(` • ${f}\n`);
|
||||
return 1;
|
||||
}
|
||||
process.stdout.write(
|
||||
`✓ INV-INT9 self-test: ${pass}/${total} פיקסצ'רים תואמים לציפייה.\n`,
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Entry point
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
function main(argv) {
|
||||
if (argv.includes("--self-test")) {
|
||||
return runSelfTest();
|
||||
}
|
||||
const pathArgs = argv.filter((a) => !a.startsWith("--"));
|
||||
const files =
|
||||
pathArgs.length > 0 ? collectFromArgs(pathArgs) : collectDefault();
|
||||
let violations;
|
||||
try {
|
||||
violations = runGate(files);
|
||||
} catch (err) {
|
||||
process.stderr.write(`✗ INV-INT9 gate — ${String(err.message ?? err)}\n`);
|
||||
return 1;
|
||||
}
|
||||
return reportGate(violations, files.length);
|
||||
}
|
||||
|
||||
// `exitCode` ולא `process.exit()` — stdout/stderr אל pipe (וזה בדיוק המצב ב-CI)
|
||||
// אסינכרוניים ב-POSIX, ו-`process.exit` היה עלול לקטוע את דוח-ההפרות עצמו.
|
||||
process.exitCode = main(process.argv.slice(2));
|
||||
138
src/devil-closer.test.ts
Normal file
138
src/devil-closer.test.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
/// <reference types="node" />
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import {
|
||||
type CloseDeps,
|
||||
caseNumberFromTitle,
|
||||
closeDevilIssueIfDone,
|
||||
DEVIL_AGENT_IDS,
|
||||
} from "./devil-closer.ts";
|
||||
|
||||
const CMPA = "8639e837-4c9d-47fa-a76b-95788d651896";
|
||||
const DEVIL = DEVIL_AGENT_IDS[CMPA];
|
||||
const TITLE = "[ערר 8165-11-25] שטן-מליץ — red-team על הניתוח";
|
||||
|
||||
function deps(over: Partial<CloseDeps> = {}) {
|
||||
const closed: string[] = [];
|
||||
const d: CloseDeps = {
|
||||
getIssue: async (id) => ({
|
||||
id,
|
||||
title: TITLE,
|
||||
status: "in_progress",
|
||||
assigneeAgentId: DEVIL,
|
||||
createdAt: "2026-10-14T12:45:58Z",
|
||||
}),
|
||||
statCritique: async () => ({
|
||||
size: 39261,
|
||||
mtimeMs: Date.parse("2026-10-14T13:29:50Z"),
|
||||
}),
|
||||
closeIssue: async (id) => {
|
||||
closed.push(id);
|
||||
},
|
||||
log: () => {},
|
||||
...over,
|
||||
};
|
||||
return { d, closed };
|
||||
}
|
||||
|
||||
const RUN = { agentId: DEVIL, issueId: "i1", status: "succeeded" };
|
||||
|
||||
test("caseNumberFromTitle", () => {
|
||||
assert.equal(caseNumberFromTitle(TITLE), "8165-11-25");
|
||||
assert.equal(caseNumberFromTitle("[ערר 8027/25] x"), "8027/25");
|
||||
assert.equal(caseNumberFromTitle("בלי מספר"), null);
|
||||
});
|
||||
|
||||
test("סוגר כשהכול תקין", async () => {
|
||||
const { d, closed } = deps();
|
||||
assert.equal(await closeDevilIssueIfDone(CMPA, RUN, d), "closed");
|
||||
assert.deepEqual(closed, ["i1"]);
|
||||
});
|
||||
|
||||
test("לא סוכן שטן → לא נוגע", async () => {
|
||||
const { d, closed } = deps();
|
||||
assert.equal(
|
||||
await closeDevilIssueIfDone(CMPA, { ...RUN, agentId: "other" }, d),
|
||||
"not_devil_run",
|
||||
);
|
||||
assert.equal(closed.length, 0);
|
||||
});
|
||||
|
||||
test("חברה לא מוכרת → לא נוגע", async () => {
|
||||
const { d } = deps();
|
||||
assert.equal(await closeDevilIssueIfDone("nope", RUN, d), "not_devil_run");
|
||||
});
|
||||
|
||||
test("ריצה שלא הצליחה → לא סוגר", async () => {
|
||||
const { d, closed } = deps();
|
||||
assert.equal(
|
||||
await closeDevilIssueIfDone(CMPA, { ...RUN, status: "failed" }, d),
|
||||
"not_devil_run",
|
||||
);
|
||||
assert.equal(closed.length, 0);
|
||||
});
|
||||
|
||||
test("issue שאינו של שטן (כותרת אחרת) → לא סוגר", async () => {
|
||||
const { d, closed } = deps({
|
||||
getIssue: async (id) => ({
|
||||
id,
|
||||
title: "[ערר 1-1-1] ניתוח משפטי",
|
||||
status: "in_progress",
|
||||
assigneeAgentId: DEVIL,
|
||||
}),
|
||||
});
|
||||
assert.equal(await closeDevilIssueIfDone(CMPA, RUN, d), "not_devil_issue");
|
||||
assert.equal(closed.length, 0);
|
||||
});
|
||||
|
||||
test("כבר סגור → לא סוגר שוב", async () => {
|
||||
const { d, closed } = deps({
|
||||
getIssue: async (id) => ({
|
||||
id,
|
||||
title: TITLE,
|
||||
status: "done",
|
||||
assigneeAgentId: DEVIL,
|
||||
}),
|
||||
});
|
||||
assert.equal(await closeDevilIssueIfDone(CMPA, RUN, d), "already_closed");
|
||||
assert.equal(closed.length, 0);
|
||||
});
|
||||
|
||||
test("אין קובץ מזכר → נשאר פתוח", async () => {
|
||||
const { d, closed } = deps({ statCritique: async () => null });
|
||||
assert.equal(await closeDevilIssueIfDone(CMPA, RUN, d), "no_deliverable");
|
||||
assert.equal(closed.length, 0);
|
||||
});
|
||||
|
||||
test("קובץ ישן מלפני יצירת ה-issue → נשאר פתוח", async () => {
|
||||
const { d, closed } = deps({
|
||||
statCritique: async () => ({
|
||||
size: 39261,
|
||||
mtimeMs: Date.parse("2026-10-01T00:00:00Z"),
|
||||
}),
|
||||
});
|
||||
assert.equal(await closeDevilIssueIfDone(CMPA, RUN, d), "no_deliverable");
|
||||
assert.equal(closed.length, 0);
|
||||
});
|
||||
|
||||
test("קובץ קצר מדי → נשאר פתוח", async () => {
|
||||
const { d, closed } = deps({
|
||||
statCritique: async () => ({
|
||||
size: 300,
|
||||
mtimeMs: Date.parse("2026-10-14T13:29:50Z"),
|
||||
}),
|
||||
});
|
||||
assert.equal(await closeDevilIssueIfDone(CMPA, RUN, d), "no_deliverable");
|
||||
assert.equal(closed.length, 0);
|
||||
});
|
||||
|
||||
test("כשל גישה לקובץ → נשאר פתוח (fail-safe)", async () => {
|
||||
const { d, closed } = deps({
|
||||
statCritique: async () => {
|
||||
throw new Error("EACCES");
|
||||
},
|
||||
});
|
||||
assert.equal(await closeDevilIssueIfDone(CMPA, RUN, d), "stat_failed");
|
||||
assert.equal(closed.length, 0);
|
||||
});
|
||||
142
src/devil-closer.ts
Normal file
142
src/devil-closer.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* סגירה אוטומטית של issue "שטן-מליץ" אחרי ריצה מוצלחת (legal-ai #892).
|
||||
*
|
||||
* הרקע: סוכן Gemini רץ בלי shell (#872) ולכן אינו יכול לסגור את ה-issue שלו.
|
||||
* Paperclip מעיר את ה-CEO רק על `issue_children_completed` (ילד ב-`done`), ו-issue
|
||||
* שנשאר פתוח עובר ל-`blocked` ("successful run missing disposition") — וה-CEO אינו
|
||||
* מתעורר לעולם (אין לו heartbeat מחזורי). התוצאה בתיק 8165-11-25: המזכר נכתב והתהליך נתקע.
|
||||
*
|
||||
* הלוגיקה כאן טהורה (תלויות מוזרקות) כדי שאפשר לבדוק אותה בלי host.
|
||||
*/
|
||||
|
||||
/** חברה → agent id של שטן-מליץ באותה חברה (Fable מאז legal-ai #978; היה Gemini). */
|
||||
export const DEVIL_AGENT_IDS: Record<string, string> = {
|
||||
"42a7acd0-30c5-4cbd-ac97-7424f65df294":
|
||||
"9c86e06a-5a92-4723-af6d-e8cc6ae1d45b", // CMP
|
||||
"8639e837-4c9d-47fa-a76b-95788d651896":
|
||||
"46cc1228-a232-410b-a36b-71a6928499a2", // CMPA
|
||||
};
|
||||
|
||||
/** קובץ המזכר: critique.md (Fable, legal-ai #978); critique-gemini.md לתיקים שרצו לפני המעבר. */
|
||||
export const CRITIQUE_FILENAMES = [
|
||||
"critique.md",
|
||||
"critique-gemini.md",
|
||||
] as const;
|
||||
|
||||
export const DEFAULT_CASES_DIR = "/home/chaim/legal-ai/data/cases";
|
||||
/** מזכר קצר מזה = כנראה ריצה ריקה/חלקית, לא סוגרים. */
|
||||
export const MIN_CRITIQUE_BYTES = 2000;
|
||||
|
||||
const CLOSED = new Set(["done", "cancelled"]);
|
||||
|
||||
export interface DevilIssueLike {
|
||||
id: string;
|
||||
title: string;
|
||||
status: string;
|
||||
assigneeAgentId?: string | null;
|
||||
createdAt?: string | Date | null;
|
||||
}
|
||||
|
||||
export interface DevilRunPayload {
|
||||
agentId?: string;
|
||||
issueId?: string | null;
|
||||
status?: string;
|
||||
}
|
||||
|
||||
export interface CloseDeps {
|
||||
getIssue(issueId: string, companyId: string): Promise<DevilIssueLike | null>;
|
||||
/** גודל + זמן-שינוי של הקובץ, או null אם אינו קיים. זורק רק על כשל-גישה. */
|
||||
statCritique(
|
||||
caseNumber: string,
|
||||
): Promise<{ size: number; mtimeMs: number } | null>;
|
||||
closeIssue(issueId: string, companyId: string): Promise<void>;
|
||||
log(
|
||||
level: "info" | "warn",
|
||||
msg: string,
|
||||
meta?: Record<string, unknown>,
|
||||
): void;
|
||||
}
|
||||
|
||||
export function caseNumberFromTitle(title: string): string | null {
|
||||
const m = title.match(/\[ערר\s+([^\]\s]+)\]/);
|
||||
return m ? m[1] : null;
|
||||
}
|
||||
|
||||
export function isDevilTitle(title: string): boolean {
|
||||
return title.includes("שטן-מליץ");
|
||||
}
|
||||
|
||||
export type CloseOutcome =
|
||||
| "closed"
|
||||
| "not_devil_run"
|
||||
| "no_issue"
|
||||
| "not_devil_issue"
|
||||
| "already_closed"
|
||||
| "no_case_number"
|
||||
| "no_deliverable"
|
||||
| "stat_failed";
|
||||
|
||||
/**
|
||||
* מופעל על `agent.run.finished` (status=succeeded בלבד). סוגר את ה-issue רק אם:
|
||||
* הריצה של שטן-מליץ של אותה חברה, ה-issue שלו פתוח, וקיים מזכר (`critique.md`) עדכני ולא ריק.
|
||||
*/
|
||||
export async function closeDevilIssueIfDone(
|
||||
companyId: string | undefined,
|
||||
payload: DevilRunPayload | null,
|
||||
deps: CloseDeps,
|
||||
): Promise<CloseOutcome> {
|
||||
if (!companyId || !payload) return "not_devil_run";
|
||||
const devilId = DEVIL_AGENT_IDS[companyId];
|
||||
if (!devilId || payload.agentId !== devilId) return "not_devil_run";
|
||||
if (payload.status && payload.status !== "succeeded") return "not_devil_run";
|
||||
if (!payload.issueId) return "no_issue";
|
||||
|
||||
const issue = await deps.getIssue(payload.issueId, companyId);
|
||||
if (!issue) return "no_issue";
|
||||
if (issue.assigneeAgentId !== devilId || !isDevilTitle(issue.title)) {
|
||||
return "not_devil_issue";
|
||||
}
|
||||
if (CLOSED.has(issue.status)) return "already_closed";
|
||||
|
||||
const caseNumber = caseNumberFromTitle(issue.title);
|
||||
if (!caseNumber) return "no_case_number";
|
||||
|
||||
let st: { size: number; mtimeMs: number } | null;
|
||||
try {
|
||||
st = await deps.statCritique(caseNumber);
|
||||
} catch {
|
||||
deps.log(
|
||||
"warn",
|
||||
"devil auto-close: critique stat failed; leaving issue open",
|
||||
{
|
||||
issueId: issue.id,
|
||||
caseNumber,
|
||||
},
|
||||
);
|
||||
return "stat_failed";
|
||||
}
|
||||
const created = issue.createdAt ? new Date(issue.createdAt).getTime() : 0;
|
||||
if (!st || st.size < MIN_CRITIQUE_BYTES || st.mtimeMs < created) {
|
||||
deps.log(
|
||||
"warn",
|
||||
"devil auto-close: no fresh critique memo; leaving issue open",
|
||||
{
|
||||
issueId: issue.id,
|
||||
caseNumber,
|
||||
size: st?.size ?? null,
|
||||
},
|
||||
);
|
||||
return "no_deliverable";
|
||||
}
|
||||
|
||||
await deps.closeIssue(issue.id, companyId);
|
||||
deps.log(
|
||||
"info",
|
||||
"devil auto-close: issue closed (CEO is woken by issue_children_completed)",
|
||||
{
|
||||
issueId: issue.id,
|
||||
caseNumber,
|
||||
},
|
||||
);
|
||||
return "closed";
|
||||
}
|
||||
@@ -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,12 @@ export class LegalApi {
|
||||
|
||||
// Types
|
||||
|
||||
export interface CaseStatusEntry {
|
||||
case_number: string;
|
||||
status: string;
|
||||
updated_at?: string | null;
|
||||
}
|
||||
|
||||
export interface CaseSummary {
|
||||
case_number: string;
|
||||
title: string;
|
||||
|
||||
@@ -179,13 +179,10 @@ export default {
|
||||
},
|
||||
],
|
||||
jobs: [
|
||||
{
|
||||
jobKey: "sync-case-status",
|
||||
displayName: "Sync Legal Case Status",
|
||||
description:
|
||||
"Polls legal-ai for case status changes and updates Paperclip issues",
|
||||
schedule: "*/15 * * * *",
|
||||
},
|
||||
// הושבת (legal-ai #1117, INV-INT9 — כותב יחיד ל-issue.status): הסנכרון עבר
|
||||
// לצד-השרת (`_case_status_sync_loop` ב-legal-ai, CASE_STATUS_SYNC_MODE).
|
||||
// הסרה מההצהרה גורמת ל-Paperclip להעביר את הג'וב ל-`paused` בהתקנה הבאה.
|
||||
// ה-handler ב-worker.ts נשאר רשום אך לא יופעל.
|
||||
{
|
||||
jobKey: "stale-case-reminder",
|
||||
displayName: "תזכורת תיקים תקועים",
|
||||
|
||||
24
src/worker-fetch.test.ts
Normal file
24
src/worker-fetch.test.ts
Normal 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
16
src/worker-fetch.ts
Normal 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);
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
/// <reference types="node" />
|
||||
|
||||
import { stat } from "node:fs/promises";
|
||||
import type {
|
||||
PluginContext,
|
||||
PluginWebhookInput,
|
||||
@@ -10,6 +13,11 @@ import {
|
||||
resolveApiBaseFrom,
|
||||
runJobHandler,
|
||||
} from "./company-scope.js";
|
||||
import {
|
||||
CRITIQUE_FILENAMES,
|
||||
closeDevilIssueIfDone,
|
||||
DEFAULT_CASES_DIR,
|
||||
} from "./devil-closer.js";
|
||||
import type { StatusModelEntry } from "./legal-api.js";
|
||||
import { LegalApi } from "./legal-api.js";
|
||||
import {
|
||||
@@ -27,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;
|
||||
@@ -739,6 +748,63 @@ const plugin = definePlugin({
|
||||
}
|
||||
}
|
||||
|
||||
// ── שטן-מליץ: סגירה אוטומטית אחרי ריצה מוצלחת (legal-ai #892) ──
|
||||
// הסוכן רץ בלי shell ולכן אינו יכול לסגור את ה-issue שלו; בלי `done` ה-CEO
|
||||
// לעולם לא מתעורר (`issue_children_completed`). ראה devil-closer.ts.
|
||||
ctx.events.on("agent.run.finished", async (event) => {
|
||||
try {
|
||||
const casesDir = process.env.LEGAL_AI_CASES_DIR || DEFAULT_CASES_DIR;
|
||||
await closeDevilIssueIfDone(
|
||||
event.companyId,
|
||||
(event.payload ?? null) as Parameters<
|
||||
typeof closeDevilIssueIfDone
|
||||
>[1],
|
||||
{
|
||||
getIssue: async (id, companyId) => {
|
||||
const i = await ctx.issues.get(id, companyId);
|
||||
return i
|
||||
? {
|
||||
id: i.id,
|
||||
title: i.title,
|
||||
status: i.status,
|
||||
assigneeAgentId: i.assigneeAgentId,
|
||||
createdAt: i.createdAt,
|
||||
}
|
||||
: null;
|
||||
},
|
||||
statCritique: async (caseNumber) => {
|
||||
// critique.md = שטן מליץ על Fable (legal-ai #978); critique-gemini.md = תיקים ישנים.
|
||||
for (const name of CRITIQUE_FILENAMES) {
|
||||
try {
|
||||
const st = await stat(
|
||||
`${casesDir}/${caseNumber}/documents/research/${name}`,
|
||||
);
|
||||
return { size: st.size, mtimeMs: st.mtimeMs };
|
||||
} catch (err) {
|
||||
if ((err as NodeJS.ErrnoException).code !== "ENOENT")
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
// סטטוס בלבד, בלי comment: comment מעיר את ה-CEO פעם שנייה (user_commented).
|
||||
closeIssue: async (id, companyId) => {
|
||||
// כותב-סטטוס שני וצר בכוונה: רק issue של שטן-מליץ (נבדק בכותרת ובסוכן), רק open→done,
|
||||
// רק אחרי ריצה מוצלחת + מזכר (critique.md) עדכני (devil-closer.ts). בלי זה הסוכן
|
||||
// (בלי shell, #872) לא סוגר והתהליך נתקע; אין flip-flop כי done סופי.
|
||||
// noqa: INT9 — סגירת issue של שטן-מליץ אחרי ריצה מוצלחת (legal-ai #892), ראה devil-closer.ts
|
||||
await ctx.issues.update(id, { status: "done" }, companyId);
|
||||
},
|
||||
log: (level, msg, meta) => ctx.logger[level](msg, meta),
|
||||
},
|
||||
);
|
||||
} catch (err) {
|
||||
ctx.logger.warn("devil auto-close handler failed", {
|
||||
error: err instanceof Error ? err.name : "unknown",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
ctx.events.on("issue.comment.created", async (event) => {
|
||||
// Only intercept human comments — not agent comments (prevents loops)
|
||||
if (event.actorType !== "user") return;
|
||||
@@ -907,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 הזה נועד לתת.
|
||||
@@ -1025,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)}`,
|
||||
@@ -1104,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) {
|
||||
@@ -1325,7 +1391,7 @@ const plugin = definePlugin({
|
||||
if (!caseNumber) return null;
|
||||
const apiBase = await resolveApiBase(ctx, companyIdOf(params));
|
||||
try {
|
||||
const resp = await ctx.http.fetch(
|
||||
const resp = await fetchLegalApi(
|
||||
`${apiBase}/api/cases/${encodeURIComponent(caseNumber)}/legal-arguments`,
|
||||
);
|
||||
if (!resp.ok) {
|
||||
@@ -1362,7 +1428,7 @@ const plugin = definePlugin({
|
||||
if (!caseNumber) return [];
|
||||
const apiBase = await resolveApiBase(ctx, companyIdOf(params));
|
||||
try {
|
||||
const resp = await ctx.http.fetch(
|
||||
const resp = await fetchLegalApi(
|
||||
`${apiBase}/api/cases/${encodeURIComponent(caseNumber)}/precedents`,
|
||||
);
|
||||
if (!resp.ok) {
|
||||
@@ -1401,7 +1467,7 @@ const plugin = definePlugin({
|
||||
const url = new URL(`${apiBase}/api/missing-precedents`);
|
||||
url.searchParams.set("case_number", caseNumber);
|
||||
url.searchParams.set("status", "open");
|
||||
const resp = await ctx.http.fetch(url.toString());
|
||||
const resp = await fetchLegalApi(url.toString());
|
||||
if (!resp.ok) {
|
||||
ctx.logger.warn("legal-case-missing-precedents: API error", {
|
||||
caseNumber,
|
||||
@@ -1422,13 +1488,13 @@ const plugin = definePlugin({
|
||||
|
||||
ctx.data.register("legal-dashboard-stats", async () => {
|
||||
try {
|
||||
const cases = await api.listCases();
|
||||
const cases = await api.listCaseStatuses();
|
||||
const byStatus: Record<string, number> = {};
|
||||
let weekActivity = 0;
|
||||
const weekAgoMs = Date.now() - 7 * 24 * 60 * 60 * 1000;
|
||||
for (const c of cases) {
|
||||
byStatus[c.status] = (byStatus[c.status] ?? 0) + 1;
|
||||
const updated = (c as { updated_at?: string | null }).updated_at;
|
||||
const updated = c.updated_at;
|
||||
if (updated) {
|
||||
const t = Date.parse(updated);
|
||||
if (!Number.isNaN(t) && t >= weekAgoMs) weekActivity += 1;
|
||||
|
||||
Reference in New Issue
Block a user