Spec: Autonomous Testing & Recursive Content-Generation Framework¶
- ID: 018-autonomous-framework
- Status: shipped
- Owner: @bamr87
- Created: 2026-05-11
- Roadmap Epic: BACKLOG items BL-20260429-09, BL-20260502-04, BL-20260509-04
- Constitution Compliance: confirmed against docs/constitution.md — strict AO-SDLC Level 2 retained (§10), provenance & HITL invariants preserved (§4, §5), local-first product runtime preserved (§1).
1. Problem & Motivation¶
Today the repo's only autonomous loop is the weekly evolve.yml audit, which only appends BACKLOG candidates from grep + coverage parsing. There is no continuous test-quality loop (the backend/law_ai/eval/ LegalBench harness referenced from docs/legalbench.md is empty), and there is no recursive content-discovery loop beyond ad-hoc make ingest runs of IngestionScout. Legal content is effectively unbounded — statutes, regulations, opinions, and citations expand daily across jurisdictions — yet our ingestion frontier is hand-driven.
This spec adds a two-pillar autonomous framework that runs entirely on the local Ollama stack, opens PRs for every code change, and routes every ingested document through the existing HITL quarantine. No new HITL gates are removed, no auto-merge is introduced.
2. Goals¶
- G1: Stand up the missing LegalBench harness (Pillar A, "TestPilot") so reasoning regressions are detectable per docs/constitution.md §3 and §6.
- G2: Add a
TestSynthesizercrew that proposes pytest cases for the lowest-coverage modules, opening a draft PR per nightly run (Pillar A). - G3: Refactor
IngestionScoutand statute scouts behind aSOURCE_REGISTRYso adding a new adapter is a single-file change with explicit allow-list gating (Pillar B foundation). - G4: Add a
TopicFrontierLangGraph workflow that walks Neo4j for orphan citations / under-cited jurisdictions and dispatches known sources to the existingIngestionScout.run_and_commitpath — recursion expands within the registered set, never via freeform web scraping (Pillar B). - G5: Coordinate both pillars under one
AutonomyOrchestrator(Celery beat +manage.py run_autonomy) with a singleAUTONOMY_ENABLEDkill-switch and a daily digest underbackend/var/autonomy/digest-<date>.md. - G6: Claim and resolve the long-standing autonomy backlog items BL-20260429-09 (agent-self-review CI), BL-20260502-04 (LegalAnalyst onto LegalWorkflow), and BL-20260509-04 (live-network nightly Justia smoke).
3. Non-Goals¶
- NG1: No auto-merge of any PR. Every loop ends in either a draft PR (code) or a
PendingReviewrow (data). - NG2: No new external data source is added in this spec. Adding a new
SourceAdapteris its own/specifycycle per constitution §5. - NG3: No closed-source LLM fallback. All crew/graph LLM calls go through the existing local Ollama client per constitution §1.
- NG4: No freeform web scraping. The
TopicFrontieronly dispatches queries against entries inSOURCE_REGISTRYwhose name appears inAUTONOMY_ALLOWED_SOURCES. - NG5: No promotion to AO-SDLC Level 3 in this spec. Level 2 stays explicit (constitution §10).
- NG6: No new frontend dashboard in this MVP — operator surface is the daily digest file plus existing
/api/agents/runsendpoints. (Frontend dashboard is recorded as a follow-up.)
4. User Stories¶
- As a reasoning engineer, I want a single command that runs LegalBench + records a JSON report so I can check whether my prompt change regressed any tracked subset.
- As a maintainer, I want a nightly draft PR proposing new tests for our lowest-coverage modules so coverage debt does not silently accumulate.
- As an ingestion lead, I want the system to surface "you have 47 unresolved citation targets in the corpus, here are the registered adapters that could resolve them" so my ingestion plan is data-driven.
- As a HITL reviewer, I want one digest per day summarizing every autonomy run (PRs opened, BACKLOG items added, docs quarantined, LegalBench delta) so I can audit the loop in five minutes.
- As an operator, I want a single env-var (
AUTONOMY_ENABLED=false) that halts all autonomous activity without redeploying.
5. Functional Requirements¶
- FR-1: A
backend/law_ai/eval/legalbench.pyCLI exists and matches the contract in docs/legalbench.md:--subset,--model,--limit,--report. Default report path isbackend/var/legalbench/<UTC>.json. - FR-2: The harness emits one OTEL span per item via
law_ai.core.provenanceand writes a per-item record{task, item_id, prompt, response, score, latency_ms, model}into the report. - FR-3: A
backend/law_ai/eval/coverage_audit.pymodule parsescoverage.xmland returns(module, pct, missing_lines)records sorted by deficit.scripts/evolve_audit.pyconsumes it via a thin wrapper (replacing the current stdout-parsing heuristic). - FR-4: A
TestSynthesizercrew atbackend/law_ai/agents_runtime/crews/test_synthesizer.pyexposesrun(input: ProposedTestInput) -> ProposedTest(Pydantic), with a versioned prompt underagents_runtime/prompts/test_synthesizer.v1.mdand provenance logging on every LLM call. - FR-5: TestSynthesizer output is written as
<module-path>__autonomy.proposed.pydecorated with@pytest.mark.skip("autonomy-proposed")so reviewers must explicitly un-skip. - FR-6: A new GitHub Actions workflow
.github/workflows/nightly-tests.yml(cron0 6 * * *) runsmake test-coverage+make test-legalbench, callsTestSynthesizeron the worst-coverage module, and opens a draft PR (labelautonomy) usingpeter-evans/create-pull-request— same pattern asevolve.yml. - FR-7: A LegalBench regression of ≥1 pp on any tracked subset (vs.
backend/var/legalbench/baseline.json) creates aseverity:highBACKLOG item and fails the workflow. - FR-8: A
backend/law_ai/ingestionapp/sources/package exposesSOURCE_REGISTRY: dict[str, SourceAdapter].CourtListenerAdapter,ColoradoStatutesAdapter, andStatuteScoutAdapterare registered, each declaringname,jurisdiction,license,requires_human_approval,rate_limit,fetch(query) -> Iterable[RawDoc]. Existing scout behavior is preserved; the new adapters are thin wrappers. - FR-9: A settings flag
AUTONOMY_ALLOWED_SOURCES: list[str](default[]in dev, populated explicitly in prod) gates dispatch. The orchestrator refuses to call any adapter whosenameis not in the list, logging apolicy_violationprovenance event. - FR-10: A management command
python manage.py list_sourcesprints the registry as a table (name / jurisdiction / license / allow-listed / rate-limit). - FR-11: A LangGraph workflow
backend/law_ai/agents_runtime/graphs/topic_frontier.pyexposes nodesscan_graph→prioritize→dispatch→summarize, returning aContentSweepDigestPydantic model. Thedispatchnode ALWAYS routes throughIngestionScout.run_and_commit; it never writes to Neo4j directly. - FR-12: A
backend/law_ai/core/autonomy.pymodule exposesrun_cycle(kind: Literal["test_pillar","corpus_growth","digest"], cycle_id: UUID | None = None) -> AutonomyRun, idempotent oncycle_id, wrapping every loop in a single audited transaction with start + end provenance events. - FR-13: A
python manage.py run_autonomy --kind {test_pillar,corpus_growth,digest} [--source NAME] [--budget N] [--cycle-id UUID]command invokesrun_cycleand exits non-zero on policy violation. - FR-14: Celery beat schedules in
law_ai.celery:autonomy-content-sweep(corpus_growth) every 6h (budget 25 items),autonomy-nightly-tests(test_pillar) daily at 06:00 UTC,autonomy-daily-digest(digest) daily at 07:00 UTC. The existing.github/workflows/evolve.ymlcontinues to drive the weekly evolve run from CI; the beat entries are the local-stack equivalent. - FR-15: A daily
compose_digesttask writesbackend/var/autonomy/digest-<YYYY-MM-DD>.mdenumerating PRs opened, BACKLOG items appended, docs quarantined, and the LegalBench delta versus baseline. - FR-16: A global
AUTONOMY_ENABLEDsettings flag (defaultFalse) short-circuitsrun_cyclewith a single provenance event when false; no Celery dispatch, no PR action, no Neo4j read. - FR-17: New Makefile targets
autonomy-up,autonomy-status,autonomy-stopshell into theworkercontainer to start/inspect/stop the beat schedule (Docker-first per AGENTS.md).
6. Non-Functional Requirements¶
- NFR-PERF-1: A single
content_sweepcycle with--budget 25completes in <10 minutes against the local docker stack on a developer laptop (Mistral 7B q4_K_M). - NFR-PERF-2: A
nightly_testscycle with--limit 20LegalBench items per task completes in <30 minutes on the same hardware. - NFR-SEC-1: All adapter HTTP calls go through
httpx+tenacityretries + per-adapterrate_limit; no rawrequests. Adapterfetchresults are validated by Pydantic before reaching the orchestrator. - NFR-AUDIT-1: Every
run_cyclestart, end, and dispatched LLM/tool call is logged vialaw_ai.core.provenance. Every adapter dispatch records{adapter_name, query, items_returned, items_quarantined}. - NFR-HITL-1:
requires_human_approval=Trueon any adapter forces everyRawDocit returns intoPendingReviewregardless of confidence — matches constitution §5. - NFR-AUDIT-2: The daily digest is the canonical human-review surface. It MUST link every PR by URL and every quarantined doc by
PendingReview.idso a reviewer can navigate from digest → action in one hop. - NFR-SEC-2:
AUTONOMY_ENABLEDdefaults toFalse; production deployments must set it explicitly. Documented in docs/local-ai.md.
7. Data Model Impact¶
No new Neo4j node labels or edge types. No new Postgres tables in this MVP — AutonomyRun records are written to audit.AuditEvent with event_type='autonomy_cycle' and a JSON payload {cycle_id, kind, started_at, ended_at, status, summary}.
A future spec may promote AutonomyRun to its own table once the digest needs richer querying (recorded as a follow-up). All existing constraints from constitution §4 remain — no new node-creation paths bypass source / confidence / ingest_timestamp.
8. API Surface¶
No new public REST endpoints in this MVP. Internal Django management commands:
python manage.py list_sources— prints registered adapters.python manage.py run_autonomy --kind {test_pillar,corpus_growth,digest} [--source NAME] [--budget N] [--cycle-id UUID]— run one cycle.
The existing /api/agents/runs/ endpoint already surfaces audit.AuditEvent rows, so the orchestrator's autonomy_cycle events are queryable through it without new routes.
9. Agent / Crew Impact¶
New entries to add to docs/agent-roles.md:
- TestSynthesizer — proposes pytest cases for low-coverage modules. Tools:
coverage_audit.lowest_coverage_module, file-read on the target module. Output:ProposedTestPydantic model.requires_human_approval=True(output always goes into a*.proposed.pyfile behind@pytest.mark.skip). - TopicFrontier — LangGraph workflow that walks Neo4j and dispatches registered adapters. Tools:
core.neo4j.queries.list_orphan_citations,SOURCE_REGISTRY.requires_human_approval=Falseat the workflow level because every dispatched doc still hitsIngestionScout's confidence gate.
No changes to existing agents (LegalAnalyst, PrecedentHunter, IngestionScout) in this spec. BL-20260502-04 (backfill LegalAnalyst onto LegalWorkflow) is claimed but its actual code change is gated on the LegalBench baseline landing first; recorded as a follow-up if it does not fit in this cycle.
10. Acceptance Criteria¶
- AC-1:
make test-legalbenchexits 0 inside the backend container, writes a JSON report underbackend/var/legalbench/<UTC>.json, and the report contains at least one record per requested subset (FR-1, FR-2). - AC-2: Running
python manage.py run_autonomy --kind test_pillar --limit 5writes a*.proposed.pyfile underbackend/tests/autonomy/proposed/containing at least one@pytest.mark.skip("autonomy-proposed")-decorated function (FR-4, FR-5). - AC-3: Running
python manage.py run_autonomy --kind corpus_growth --source unknown_sourceexits non-zero and emits a provenance event withevent_type='policy_violation'(FR-9, NFR-AUDIT-1). - AC-4: Running
python manage.py run_autonomy --kind corpus_growth --source courtlistener --budget 3(withAUTONOMY_ENABLED=True) creates zero new:Casenodes that did not pass throughIngestionScout.run_and_commit's confidence gate (FR-11, NFR-HITL-1). Verified by an integration test that asserts every new node carries theIngestionScoutagent_version. - AC-5: With
AUTONOMY_ENABLED=False,python manage.py run_autonomy --kind corpus_growthexits 0, makes zero HTTP calls, makes zero Celery dispatches, and emits exactly one provenance event withevent_type='autonomy_disabled'(FR-16). - AC-6:
python manage.py list_sourcesprints exactly three rows forcourtlistener,colorado_statutes,statute_scoutand marks each oneallow-listed=Falseby default (FR-8, FR-10). - AC-7: After one nightly cycle in CI,
backend/var/autonomy/digest-<date>.mdexists and contains sections "PRs opened", "BACKLOG items appended", "Docs quarantined", "LegalBench delta" with at least the headers populated (FR-15, NFR-AUDIT-2). - AC-8: A simulated LegalBench regression of 2 pp (forced via a pytest fixture) causes
nightly-tests.ymlto exit non-zero AND adds oneseverity:highBACKLOG item viaevolve_audit.py(FR-7). - AC-9:
python3 scripts/spec_validator.pyandpython3 scripts/backlog_lint.pyboth exit 0 after this spec lands.
11. LegalBench Impact¶
- Tasks affected: none in this spec — TestPilot only measures LegalBench, it does not change reasoning logic.
- Expected delta: 0 pp ± 0 pp on
core_reasoningandprecedent_retrieval. The very first run establishesbackend/var/legalbench/baseline.json; no prior baseline exists. - The baseline JSON commit is gated on
backend/var/legalbench/LICENSES.mdlanding first (per-task source + license enumeration). Documented in plan §3.
12. Risks & Open Questions¶
- R-1 (med): LegalBench dataset size + Ollama latency may exceed the 30-min nightly budget on weaker hardware. Mitigation:
--limit 20per task by default; CI runs the full set monthly. — accepted. - R-2 (med): A misconfigured
AUTONOMY_ALLOWED_SOURCEScould allow a Celery worker to hammer an external API. Mitigation: per-adapterrate_limitenforced byhttpx+tenacity; orchestrator caps--budgetat 100. — accepted. - R-3 (low): TestSynthesizer may propose nonsense tests, polluting PR review queues. Mitigation:
*.proposed.py+@pytest.mark.skip("autonomy-proposed")make every proposal opt-in; never executed by default. — accepted. - R-4 (low): LegalBench upstream license drift could force us to drop tasks mid-baseline. Mitigation:
LICENSES.mdmanifest is reviewed at every spec touching the harness. — accepted. - R-5 (low): BL-20260502-04 (LegalAnalyst onto LegalWorkflow) may not fit in this cycle. Mitigation: pre-declared as a follow-up to BACKLOG; spec scope does not require it. — accepted.
13. Out of Scope / Follow-ups¶
- Read-only
frontend/app/autonomy/dashboard with Vitest test (will be a separate spec, recorded asBL-20260511-NN). - Auto-merge classes for low-risk PRs (would require a constitution amendment per §10).
- Promotion of
AutonomyRunto a dedicated Postgres table. - Multi-jurisdictional adapter additions (each new source = new spec per constitution §5).
- Streaming digest to Slack/email (out of MVP).