Commit Graph

29 Commits

Author SHA1 Message Date
b9906f6047 fix(manifest): הצהרת legal_predecessor_context ב-manifest (חשיפת הכלי)
ה-manifest.tools הוא המקור-הקובע לרישום/חשיפת כלים ב-Paperclip — ctx.tools.register
ב-worker.ts נותן רק את ה-handler. בלי הצהרה ב-manifest, הכלי לא נחשף (הלוגים הראו
toolCount=8, בלי legal_predecessor_context). מוסיף את ההצהרה → toolCount=9, הכלי חשוף.

אומת חי: reinstall+restart → logs מראים toolCount:9 + legal_predecessor_context רשום.
(המשך #220; משלים את PR #5.)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 05:16:18 +00:00
8d933386d3 Merge pull request 'feat(tools): כלי legal_predecessor_context — הקשר מריצות-קודמות לסוכן (#220)' (#5) from feat/predecessor-context into main 2026-07-07 05:12:18 +00:00
0b6117cd7e feat(tools): כלי legal_predecessor_context — הקשר מריצות-קודמות לסוכן (#220)
מוסיף כלי-plugin שהסוכן קורא בעת continuation כדי לראות מה ריצות קודמות על התיק
הסיקו (ה-summary של כל heartbeat), במקום לגלות-מחדש הקשר מאפס (blind heartbeat).

- legal-api.ts: getPredecessorForCase(caseNumber) → GET
  /api/operations/cases/{case}/predecessor (endpoint שנוסף ב-legal-ai #410).
- worker.ts: רישום הכלי (case_number param) — מעצב digest קריא (agent·issue·when·
  status + summary חתוך ל-600 תווים, newest-first).

ארכיטקטורה (ההכרעה מהדיון): הכלי חי ב-plugin — המעטפת המוצהרת שמותר לה לדבר גם
Paperclip וגם legal-ai web — ולא ב-mcp-server (הליבה נשארת עיוורת-פלטפורמה, G12).
זו הסיבה שזה plugin-tool ולא mcp→web seam.

בנייה: tsc + biome נקיים. פריסה (ידנית, pm2): npm run build → plugin install →
pm2 restart paperclip. המשך: הוראת-HEARTBEAT לקרוא בעת continuation.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-07 05:11:44 +00:00
fc5d8725f1 Merge pull request 'fix(sweep): בחירת הערת-יו"ר אחרונה לפי זמן, לא לפי מיקום-מערך' (#4) from fix/sweep-comment-ordering into main 2026-06-30 11:41:32 +00:00
b9a5bb6e80 fix(sweep): pick newest chair comment by timestamp, not array position
The sweep assumed listComments returns oldest-first and took
comments[length-1] as "the last comment". listComments actually returns
newest-first, so the sweep routed the OLDEST user comment instead of the
newest — and routed it whether or not an agent had already answered. Caught
live on CMPA-116: the sweep routed a stale 6-day-old "agent didn't activate"
gripe (already answered minutes later) instead of the chair's real pending
request ("I attached case law — go back to the precedent-researcher and check
the docs"), so the CEO correctly no-op'd and the real request stayed dropped.

Fix: order-independent. Compute the newest USER comment and newest AGENT
comment by createdAt; route only when the chair spoke last among real
participants (newest user > newest agent, or no agent comment), so an agent
reply — but NOT an automated system "draft ready" line — counts as handled.
Also fix the same latent array-order assumption in the event-handler body
fallback (reduce-by-createdAt instead of comments[length-1]).

Self-heals the stuck marker on CMPA-116 (newest-user id differs from the
wrongly-stamped one). Refs legal-ai #164.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-30 11:41:02 +00:00
0bf2e46212 Merge pull request 'feat(routing): ערובת at-least-once להערות-יו"ר → CEO (sweep-פיוס)' (#3) from feat/comment-delivery-guarantee into main 2026-06-30 10:40:44 +00:00
aeffbe65ee feat(routing): at-least-once guarantee for chair comments → CEO
The event-driven comment→CEO route (issue.comment.created) is best-effort:
a failed/coalesced/cancelled CEO invoke, or a comment that lands while an
agent is mid-run, can leave a chair comment unrouted with nothing to retry.
This is exactly how a chair comment on CMP-190 (2026-06-30) was silently
dropped — the host-native wake was cancelled (issue_execution_promoted /
assignee_changed) and no plugin route fired.

Adds a reconciliation sweep (route-pending-comments, every 2 min) that
re-routes any active issue whose LAST comment is the chair's (no agent reply
after it) and whose id ≠ the per-issue `last-routed-comment-id` marker.
Both the fast path and the sweep stamp the marker → idempotent, no
double-routing of healthy comments. "Last comment is user" precisely targets
the failure (chair commented, no agent response) and skips historically
answered comments, so first-run-after-deploy footprint is 0.

- worker.ts: extract shared routeCommentToCeo() + markCommentRouted();
  event handler stamps marker (incl. the native-handles-it branch);
  register route-pending-comments sweep with per-company/per-issue guards.
- manifest.ts: declare route-pending-comments job (*/2 * * * *).

Invariants: G12 (Paperclip touch only in the declared wrapper — plugin),
G2 (extends the existing route, no parallel path). Refs legal-ai #164.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-30 10:18:38 +00:00
0a8790b088 fix(routing): use bodySnippet fallback + warn log when listComments fails
When ctx.issues.listComments() throws, the previous catch silently
replaced commentBody with "(שגיאה בקריאת התגובה)" — causing CEO to
receive garbage text instead of an actionable instruction.

Now: falls back to payload.bodySnippet (always present in Paperclip
webhooks), logs a proper warning with the real error for diagnosis,
and if even bodySnippet is empty, sends a clear meta-instruction
telling CEO to read comments directly instead of corrupted content.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-24 14:53:31 +00:00
b6bf726570 Merge pull request 'fix(routing): comment→CEO wakeup reads issue id from event.entityId' (#2) from fix/comment-routing-ceo-entityid into main 2026-06-17 04:55:46 +00:00
2d6dd8a072 fix(routing): comment→CEO wakeup read issue id from event.entityId, not payload.issueId
The `issue.comment.created` handler read `payload.issueId`, which the current
Paperclip host never sends — the issue id arrives in `event.entityId` and the
payload carries `commentId`/`bodySnippet`/`reopened` instead. So the handler
hit "missing issueId, skipping" on every user comment and the documented
"user comment → CEO" routing was silently dead.

Diagnosed on case 8124-09-24 (CMPA): a question commented on an analyst's
`done` sub-issue never reached the CEO. The plugin skipped; Paperclip's native
reopen-on-comment wake then targeted the sub-issue's assignee (the analyst) and
the queued CEO run was cancelled with `issue_assignee_changed`.

Fix:
- issueId from `event.entityId` (fallback `payload.issueId` for host-version skew).
- Resolve full comment body by matching `payload.commentId` in listComments
  (payload only has a truncated `bodySnippet`); fall back to latest/snippet.
- Dedup guard: when the comment reopened an issue ALREADY assigned to the CEO,
  the host's native wake covers it — skip to avoid double-running the CEO.
  For issues owned by any other agent we still route to the CEO.

Invariants: upholds the "user comment routes through CEO" contract (CLAUDE.md);
touches only the platform-port shell (plugin), per G12/X15. No new parallel path.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 04:55:08 +00:00
f70023fccc feat(ui): Phase 2 — issue detail tab + dashboard widget
Plugin now mounts two React components inside Paperclip via the
SDK's UI slot mechanism. Both are read-only views over data the
plugin worker fetches from legal-ai on demand.

## Slots

* **LegalCaseTab** (type: detailTab, entityTypes: ["issue"])
  Mounted as a "ערר" tab on every issue page. Shows case summary
  (status / practice_area / appeal_subtype), legal_arguments
  grouped by party (עוררים/ועדה/משיבה/מבקשי היתר), attached
  precedents, and open missing_precedents.

* **LegalCasesWidget** (type: dashboardWidget)
  Dashboard tile with case counts by status + 7-day activity.

## Worker handlers (ctx.data.register)

Five handlers added at the end of setup() — all read-only over the
existing legal-ai HTTP API, all wrapped in try/catch so a transient
failure shows a placeholder instead of crashing the host:

- legal-case-summary           → /api/cases/{n}/details
- legal-case-arguments         → /api/cases/{n}/legal-arguments
- legal-case-precedents        → /api/cases/{n}/precedents
- legal-case-missing-precedents → /api/missing-precedents?case_number=&status=open
- legal-dashboard-stats        → in-memory aggregation over /api/cases

case_number is resolved from plugin state (scopeKind=issue,
stateKey=legal-case-number) — populated by legal_case_create.

## Build pipeline

- esbuild.ui.config.mjs uses createPluginBundlerPresets from the SDK
  to build src/ui/index.tsx → dist/ui/index.js (13.5kb, react +
  @paperclipai/plugin-sdk/ui externalized)
- package.json: build = "build:worker" (tsc) + "build:ui" (esbuild)
- tsconfig.json: jsx=react-jsx, lib += DOM
- New deps: react@19, @types/react, esbuild

## Manifest

- capabilities += ui.detailTab.register, ui.dashboardWidget.register
- entrypoints.ui = "dist/ui"
- ui.slots declared with entityTypes (not "entities" — fixed against
  PluginUiSlotDeclaration validator)

## Verified

- tsc + esbuild + biome clean
- Plugin re-installs (20 capabilities) and activates with worker
  + 8 tools + 3 jobs + 1 webhook + 2 event subs

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-26 14:04:40 +00:00
0ca7831c53 feat: adopt SDK 525 features — askUserQuestions + documents.upsert
Two new event types accepted on the existing case-status webhook
(eventType discriminator added; legacy status_change still default):

* **missing_precedent_created** → ``ctx.issues.askUserQuestions``
  with a single-choice question {upload | irrelevant | defer}.
  continuationPolicy=wake_assignee_on_accept routes the chair's
  answer straight back to the CEO heartbeat without an extra hop.

* **export_complete** → ``ctx.issues.documents.upsert`` with a
  markdown "final-decision" doc that links back to the DOCX on
  legal-ai. (The SDK's documents API stores text only — binary
  attachment isn't natively supported here.)

Manifest: +issue.documents.write capability (issue.interactions.create
was already declared in the previous SDK upgrade).

Tested: plugin activates with all 18 capabilities, 8 tools + 3 jobs
+ 1 webhook + 2 event subs registered.
2026-05-26 13:28:43 +00:00
6388062cdc chore(sdk): upgrade @paperclipai/plugin-sdk to 2026.525.0
200-version bump from 2026.325.0 → 2026.525.0 (matches host).

**Why now**: host is already on 2026.525.0; staying on 325 was unnecessary
technical debt. Production logs showed real bugs ("missing, expired,
or unknown invocation scope") that newer SDK versions are known to
have fixed.

**Compatibility**:
- TS build clean with zero changes to worker.ts or legal-api.ts.
- apiVersion: 1 still the schema (no v2 yet).
- Plugin activated successfully — 8 tools + 3 jobs + 1 webhook + 2 event
  subs all registered, worker reports "Legal AI plugin ready".

**Manifest gaps fixed**: added 3 capabilities that the worker actually
uses (one of them was the root cause of repeated host-side errors):
* `agents.invoke` — required by `ctx.agents.invoke()` calls (CEO wakeup
  from event handlers + comment routing). Previously the plugin was
  invoking agents without the declared capability, raising
  "missing scope" errors on every issue.created event.
* `issue.comments.read` — required by `ctx.issues.listComments()`
  calls in the comment routing path.
* `issue.interactions.create` — forward-looking capability for the
  AskUserQuestion-like flows the SDK exposes via `createInteraction`.

**Backup**: full 5-layer snapshot at /tmp/plugin-legal-ai-backup-*.tar.gz
+ tag `pre-sdk-upgrade-2026-05-26` (pushed). Rollback: `git reset --hard
pre-sdk-upgrade-2026-05-26 && npm ci && npm run build && reinstall`.

Closes TaskMaster #26.
2026-05-26 12:19:04 +00:00
a8999b70c2 chore: snapshot before SDK upgrade
Includes pending stale-case-reminder days fix (3→30) alongside
the backup commit, since both will be preserved by the same tag.
pre-sdk-upgrade-2026-05-26
2026-05-26 12:13:52 +00:00
73e37df129 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 <noreply@anthropic.com>
2026-05-17 11:07:54 +00:00
6a93c606b9 fix: pick first mapped CEO for weekly-feedback-analysis; align reason string
- Replace companies[0] with first company that has a CEO_AGENT_IDS entry,
  so the job doesn't silently skip if the list order is non-deterministic
- Change reason to "weekly-feedback-job" to match the CEO routing condition
  added in legal-ceo.md (was "weekly-feedback-analysis scheduled job")
- decision-lessons.md is shared between companies so one CEO invocation suffices

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-17 10:54:39 +00:00
73078946f1 feat: add idempotency guard to onWebhook
Prevent duplicate comments on rapid Paperclip retries:
- Check plugin instance state for requestId before processing
- Skip if same requestId was seen within 5 minutes
- Store requestId with ISO timestamp after first successful delivery

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-17 10:52:12 +00:00
52c3e600e7 style: biome auto-fix (const, import order, formatter)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-17 10:18:33 +00:00
98c87a5d70 perf: build case-issue map once in stale-case-reminder; add idempotency TODO
- Hoist issues.list() loop outside stale-cases loop — reduces RPCs from
  O(cases × companies × issues) to O(companies × issues)
- Add TODO comment for requestId-based idempotency guard in onWebhook

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-17 10:15:51 +00:00
09acb021eb perf: hoist companies.list() outside stale-case loop 2026-05-16 17:41:43 +00:00
06679bb061 feat: add stale-case-reminder and weekly-feedback-analysis jobs
Declare two new cron jobs in plugin.json and manifest.ts, and implement
their handlers in worker.ts. stale-case-reminder runs daily at 08:00 and
posts a warning comment on any Paperclip issue linked to a legal-ai case
that has not been updated in 3+ days. weekly-feedback-analysis runs every
Sunday at 19:00, fetches the weekly chair-feedback summary from legal-ai,
and invokes the CEO agent to update decision-lessons.md with new lessons.
2026-05-16 17:40:33 +00:00
9633617e26 fix: declare case-status webhook endpoint in manifest
Add webhooks[] array to plugin.json and manifest.ts with the
'case-status' endpointKey. Without this declaration, Paperclip
registers 0 webhooks even when onWebhook() is implemented, so
POST /api/plugins/marcusgroup.legal-ai/webhooks/case-status would
return 501 and never reach the handler.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-16 17:29:56 +00:00
f5a4cd1c62 feat: add webhooks.receive capability to plugin manifest 2026-05-16 17:19:10 +00:00
23fda8da8c fix: add payload validation guard in onWebhook handler 2026-05-16 17:18:26 +00:00
6b856dbe85 feat: add onWebhook handler for case-status events
Adds the onWebhook lifecycle hook to the definePlugin() call. When
legal-ai POSTs to /webhooks/case-status, the handler finds the linked
Paperclip issue (via plugin state scan), posts a Hebrew status comment,
and wakes the CEO agent on qa_failed. Hoists PluginContext and
CEO_AGENT_IDS to module scope so onWebhook can access them after setup().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-16 17:16:28 +00:00
bf639063f7 Route user comments through CEO agent via issue.comment.created event
Add event handler that intercepts user comments on issues and wakes the
CEO agent (instead of only the assigned agent). The CEO reads the comment,
checks for attachments, and routes to the appropriate agent.

- Add issue.comment.created event subscription
- Add agents.read, agents.invoke, issue.comments.read capabilities
- CEO receives comment body + issue context in the invoke prompt

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 13:47:52 +00:00
bec5d1bf3a Add biome config and update plugin source + dependencies
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 16:54:55 +00:00
284401a4fe Add .gitignore, remove node_modules and dist from tracking
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 08:03:57 +00:00
587a2a76ca Initial commit: Paperclip plugin for Legal AI integration
16 agent tools, event handler for auto-linking, sync job every 15m.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 08:03:43 +00:00