Spec: Fold lawmode into law-ai¶
- ID: 057-lawmode-fold-in
- Status: in-progress
- Owner: @bamr87
- Created: 2026-11-12
- Roadmap Epic: #226 (decision relayed from Amr via Grok Bot, 2026-11-12: fold lawmode into law-ai; not revived standalone, not retired) · PRD §5.8 · catalog
LAWAI-022 - Constitution Compliance: confirmed against docs/constitution.md. §1: every lawmode LLM call moves onto
law_ai.core.llm(LLM_PROVIDER_CHAIN, defaultclaude_code,anthropic_api,xai,ollama), soollamastays reachable and no further vendor is added; lawmode's OpenAI backend (langchain-openai, defaultgpt-4o) and its raw-requestsGrok client are not carried over, and lawmode gets no model default of its own. §2: every call is recorded throughlaw_ai.core.provenance. §5/§7: high and critical findings become HITL items, and nothing is posted to a pull request or blocks a merge without a human. §8:httpx+tenacity, norequests; this spec adds no new runtime dependency. §11: the migration code is written by Claude Code from the handoff issues, not by the planning bot (hub constitution §26).
1. Problem & Motivation¶
lawmode/ is the imported bamr87/lawmode "legal reviewer for developers": a CLI, a LangGraph pipeline of eight legal-risk chains (jurisdiction, domain, privacy, licence, security, accessibility, copyright, regulatory), a GitHub PR reviewer and a VS Code extension. It sits in the law-ai tree but outside every law-ai rule. Deep dive issue #226 found that:
- it defaults to OpenAI (
LLMConfig.provider="openai",model="gpt-4o"), a vendor constitution §1 rejects, and callsllm.invokedirectly with no provider chain, no provenance and no HITL; - its prompts are inline f-strings (AGENTS.md forbids inline prompts), and its Grok client and tools use raw
requests; - its research "tools" (CourtListener, EUR-Lex, Google Scholar, SPDX) are stubs that never make a request;
- its CLI is a single
clickcommand, but the VS Code extension runslawmode scan <file> --json, a subcommand that does not exist, so the extension cannot work; - the extension builds its shell command by string interpolation (
child_process.exec), and its build output (out/*.js,*.js.map) is committed (hub UPS-REPO-07); - its workflow is nested at
lawmode/.github/workflows/lawmode.yml, so GitHub never runs it, and no law-ai Make target or CI job lints, type-checks or tests a line of it.
Amr has decided the code is folded into law-ai: the useful parts become a first-class law-ai feature that obeys the constitution and runs under law-ai CI.
2. Origin & Background¶
- Origin: law-ai deep dive cycle 1 day 14 (2026-11-12), issue #226 ("lawmode/: decide fold-in, revive or retire"), decision relayed from Amr via Grok Bot on 2026-11-12: fold in.
- Backlog items: this spec files BL-20261112-09 … BL-20261112-12 (see
## Follow-ups Discoveredin tasks.md); it claims none. - Prior art reused, not rebuilt:
law_ai.core.llm(spec 034, ADR 0005, ADR 0007),law_ai.core.provenance, thePendingReviewHITL queue (spec 015;ingestionapp/quarantine_kinds.py), versioned prompts underbackend/law_ai/agents_runtime/prompts/, the CourtListener source inbackend/law_ai/ingestionapp/sources/courtlistener.py. - The migration plan, with the full file-by-file inventory and destination of every lawmode piece, is docs/lawmode-migration.md.
- Review question (constitution §13) — who is the stronger here, and what does this feature do for them? lawmode scores legal risk in source code (critical / high / medium / low) against jurisdictions and regulations, and a score can gate a merge. The stronger party is whoever owns the repository being scanned, or a platform that could use an automated "legally fine" verdict to ship over the objections of the people its code affects (data subjects, users who need accessibility, upstream licensors). The fold-in therefore makes the score advisory: every finding cites the authority or licence it relies on with provenance, a high or critical finding goes to a human through the HITL queue rather than auto-blocking or auto-passing, a clean scan is reported as "no findings from these checks", never as "compliant", and every output carries the lawmode disclaimer (not legal advice).
3. Goals¶
- G1: lawmode's legal-risk review runs inside the law-ai backend as
law_ai.lawmode, with every LLM call onLLM_PROVIDER_CHAIN, every call recorded through provenance, and high/critical findings routed to HITL. - G2: The moved code is under law-ai CI:
make lint,make typecheck,make testand the coverage gate cover it, and the VS Code extension has its own lint/typecheck/test in the samemaketargets. - G3: The CLI and the VS Code extension agree on one JSON contract, and the extension can actually run a scan.
- G4: Retiring the old
lawmode/tree is a separate, explicit later step that only happens with Amr's OK.
4. Non-Goals¶
- Reviving lawmode as a standalone product, package or repository.
- Archiving, deleting or retiring any part of
lawmode/or thebamr87/lawmoderepository in this spec's implementation phases (Phase 6 only proposes it; see FR-10). - A GitHub Actions workflow that comments on, or sets status checks on, law-ai pull requests (deferred: BL-20261112-11).
- Publishing the VS Code extension to the Marketplace (deferred: BL-20261112-12).
- Adding OpenAI, Gemini or any provider outside constitution §1.
- Changing LegalBench scope or the eval pins.
5. User Stories¶
- US1: As a developer on a repository, I run one command (
make lawmode-scan TARGET=…, orlawmode scan <path> --json) and get a list of legal-risk findings, each with a severity, a jurisdiction, the authority or licence it cites and a mitigation, so that I can fix issues before review. - US2: As the law-ai operator (Amr), I know every lawmode model call went through the provider chain and is recorded in the provenance log, so that a scan of private source code never leaves for an unapproved vendor and can be audited later.
- US3: As a reviewer, I see high and critical lawmode findings in the HITL queue and decide on them myself, so that no automated legal verdict blocks or passes a change without a human.
- US4: As a VS Code user, I run "LawMode: Scan" and see findings as diagnostics in the editor, so that I get the same results as the CLI where I write code.
- US5: As a maintainer, I see lawmode code fail
make lint,make typecheckormake testlike any other law-ai code, so that it does not rot again. - US6: As Amr, I decide when the old
lawmode/tree is archived or removed, after the folded feature has shipped, so that nothing is lost before its replacement is proven.
6. Functional Requirements¶
- FR-1: A Django app
law_ai.lawmodeholds lawmode's domain: the finding/report schemas (Pydantic v2, ported fromlawmode/lawmode/models.py), the policy (jurisdictions, severity gating, domains; ported fromlawmode/.lawmode/policy.yamlto Python defaults inlaw_ai/lawmode/policy.py, overridable through settings) and the review service (ported fromlawmode/lawmode/core.py). - FR-2: Every lawmode LLM call goes through
law_ai.core.llm(LLMRequest→ the provider chain). lawmode has no provider client, nolangchain-openai/langchain-anthropicimport and no model name in code. With no override it uses the chain's configured models; any lawmode-specific override is read from settings, and an Anthropic override is rejected at startup unless it isclaude-opus-5-5,claude-sonnet-5-5orclaude-haiku-5-5. - FR-3: The eight review chains run as a LangGraph graph in
backend/law_ai/agents_runtime/graphs/built on the chain adapter inlaw_ai.core.llm.langchain, each chain's prompt a versioned Markdown file underbackend/law_ai/agents_runtime/prompts/lawmode/passed withprompt_id/prompt_version. - FR-4: Each LLM call and each tool call records a provenance event through
law_ai.core.provenance(scan id, chain, prompt id/version, provider, model, input hash, findings count). - FR-5: Findings at a severity the policy marks
blockorrequire_approval(default: critical, high) create aPendingReviewitem of a new kindlawmode_finding; the scan result reports them as "awaiting human review", never as pass/fail. - FR-6: Research tools that lawmode needs are real or absent: CourtListener lookups reuse
ingestionapp/sources/courtlistener.py; an SPDX licence lookup and an EUR-Lex lookup, if kept, usehttpx+tenacitywith timeouts; the Google Scholar stub is dropped (no scraping). - FR-7: A management command
lawmode_scan(make lawmode-scan TARGET=… [DIFF=1] [JURISDICTION=…]) scans a path or agit diff, prints a human summary or--json, and writes the report artifacts that lawmode wrote under.lawmode/. The JSON schema is versioned and documented indocs/lawmode.md. - FR-8: The VS Code extension lives under law-ai's tree outside
lawmode/, runs the scan withexecFile/spawnand an argument array (no shell string), consumes the FR-7 JSON contract, and has no committed build output. - FR-9: law-ai CI covers the moved code: backend code under
make lint(ruff),make typecheck(the mypy ratchet,scripts/mypy_ratchet.py) andmake testwith the coverage gate; the extension undermake lint/make typecheck/make testvia its npm scripts in anode:20-alpinecontainer, the same pattern asmake test-lsat-static. No new paid CI and no new secrets. - FR-10: Retiring the old tree (removing or archiving
lawmode/, its nested workflow,lawmode/CLAUDE.md, and thebamr87/lawmoderepository) is a separate PR that is opened only after Amr approves it in his own words on #226 or the Phase 6 issue.
7. Non-Functional Requirements¶
- NFR-SEC-1: All external HTTP via
httpxwithtenacityretry and an explicit timeout; norequests. The extension never builds a shell command string. - NFR-SEC-2: Scanned source code is sent only to providers in
LLM_PROVIDER_CHAIN; the docs explainLLM_PROVIDER_CHAIN=ollamafor code that must not leave the machine. - NFR-AUDIT-1: Every agent/LLM/tool call logged via
law_ai.core.provenance(FR-4). - NFR-HITL-1:
human_approval_requiredis declared for the high/critical path (FR-5);ENABLE_HUMAN_APPROVAL_GATES=falseis not a lawmode bypass — the scan then reports those findings as unreviewed. - NFR-QUAL-1: New backend code meets the repo coverage gate and mypy settings; no new
# type: ignorewithout a reason. - NFR-DEP-1: No new runtime dependency (LangGraph, LangChain core, httpx, tenacity and Pydantic are already in
backend/pyproject.toml; PyYAML is not a direct dependency, so reading a per-repository.lawmode/policy.yamlis deferred to BL-20261112-10). Droppingclick,rich,gitpython,requests,langchain-openaiandlangchain-anthropicis deliberate; if an implementer needs one, the spec is amended first with a licence and security note (constitution §8).
8. Data Model Impact¶
- New
PendingReview.kindconstantlawmode_finding(code-only per spec 015 FR-6, no migration). - Optional
LawmodeScanmodel (scan id, target, policy hash, started/finished, findings JSON) — Phase 2 decides; if added, it ships with a migration and admin.
9. API Surface¶
- Management command
lawmode_scanand Make targetlawmode-scan. - A console entry
lawmode(scansubcommand) for the extension, implemented as a thin wrapper over the management command (Phase 4 decides wrapper vs.docker compose exec). - No new HTTP endpoint in this spec.
10. Agent / Crew Impact¶
New graph code_legal_review (eight chain nodes plus aggregate and HITL gate) registered with the agents runtime; no change to existing crews.
11. Acceptance Criteria¶
- AC-1: Given a repository fixture with a GPL dependency and a hard-coded personal-data field, when
make lawmode-scan TARGET=<fixture> JSON=1runs with a stubbed provider, then the JSON lists a licence finding and a privacy finding, each with severity, jurisdiction, citation and mitigation. - AC-2: Given the scan above, then every LLM call in it appears in the provenance log with provider, model and prompt id/version, and
git grep -n "openai\|gpt-4o\|requests\." backend/law_ai/lawmode backend/law_ai/agents_runtime/graphs/code_legal_review.pyis empty. - AC-3: Given a critical finding, when the scan finishes, then a
PendingReviewof kindlawmode_findingexists, the scan exit code does not signal "blocked" by itself, and the JSON marks the findingawaiting_review. - AC-4: Given the extension, when its tests run, then the scan is invoked with an argument array (no shell string) and parses the FR-7 JSON fixture into diagnostics.
- AC-5: Given a PR that breaks lawmode Python or TypeScript, then
make lint,make typecheckormake testfails in CI. - AC-6: Given an Anthropic model override other than the three allowed ids, then settings validation fails at startup.
- AC-7: Until Amr approves Phase 6 in his own words,
lawmode/is unchanged onmainby this spec's PRs.
12. LegalBench Impact¶
None: no reasoning path that LegalBench gates changes; evals stay pinned (constitution §1).
13. Risks & Open Questions¶
- R-1: Source code sent to hosted tiers may be confidential. Mitigation: NFR-SEC-2 and a docs section on pinning
ollama. - R-2: An automated legal-risk score can be read as legal advice. Mitigation: disclaimer on every output, advisory wording (§2 review question), HITL for high/critical.
- R-3: Two copies (old
lawmode/and the folded app) drift during migration. Mitigation: the old tree is frozen (no new features) from Phase 1; Phase 6 retires it with Amr's OK. - OQ-1: Does the extension call the CLI in the backend container, a local virtualenv, or a backend HTTP endpoint? Phase 5 resolves; default: the CLI via an argument array, with the command configurable.
- OQ-2: Keep EUR-Lex and SPDX lookups or drop them with the other stubs? Phase 3 resolves on evidence (do the chains use them?).
- OQ-3: Where does the extension live:
extensions/vscode-lawmode/(proposed) or underfrontend/? Phase 5 resolves.
14. Out of Scope / Follow-ups¶
BL-20261112-09 … BL-20261112-12 (see tasks.md ## Follow-ups Discovered and BACKLOG.md).