You are a senior code reviewer. Review the following code changes.

## Specification

No specification provided. Focus on correctness, tests, and integration.





## Code Changes

```diff
diff --git a/expert_build/cli.py b/expert_build/cli.py
index 8dcd7a6..00ea9ad 100644
--- a/expert_build/cli.py
+++ b/expert_build/cli.py
@@ -108,6 +108,8 @@ def main():
     pipe_p.add_argument("--timeout", type=int, default=600,
                         help="LLM timeout in seconds (default: 600)")
     pipe_p.add_argument("--domain", help="Domain description for derive context")
+    pipe_p.add_argument("--resume", action="store_true",
+                        help="Resume from last saved pipeline state")
 
     # -- status --
     sub.add_parser("status", help="Show pipeline progress")
diff --git a/expert_build/pipeline.py b/expert_build/pipeline.py
index 82c7966..f5504db 100644
--- a/expert_build/pipeline.py
+++ b/expert_build/pipeline.py
@@ -2,11 +2,85 @@
 
 import json
 import sys
+import traceback
+from datetime import datetime, timezone
 from pathlib import Path
 from types import SimpleNamespace
 
 from .llm import check_model_available, invoke_sync
-from .propose import REASONS_DB
+from .propose import PROJECT_DIR, REASONS_DB
+
+STATE_FILE = Path(PROJECT_DIR) / "pipeline-state.json"
+
+STAGE_NAMES = {
+    1: "ingest",
+    2: "summarize",
+    3: "extract",
+    4: "derive",
+    5: "review",
+    6: "repair",
+    7: "deduplicate",
+    8: "export",
+}
+
+
+def _now():
+    return datetime.now(timezone.utc).isoformat(timespec="seconds")
+
+
+def _init_state(args):
+    state = {
+        "started_at": _now(),
+        "updated_at": _now(),
+        "status": "running",
+        "current_stage": None,
+        "current_cycle": None,
+        "args": {
+            "url": getattr(args, "url", None),
+            "model": args.model,
+            "rounds": args.rounds,
+            "domain": getattr(args, "domain", None),
+        },
+        "stages": {
+            f"{n}_{name}": {"status": "pending"}
+            for n, name in STAGE_NAMES.items()
+        },
+    }
+    _save_state(state)
+    return state
+
+
+def _load_state():
+    if not STATE_FILE.exists():
+        return None
+    try:
+        return json.loads(STATE_FILE.read_text())
+    except (json.JSONDecodeError, ValueError):
+        print(f"WARNING: corrupt state file {STATE_FILE}, ignoring",
+              file=sys.stderr)
+        return None
+
+
+def _save_state(state):
+    state["updated_at"] = _now()
+    STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
+    STATE_FILE.write_text(json.dumps(state, indent=2) + "\n")
+
+
+def _mark_stage(state, stage_num, status, **meta):
+    key = f"{stage_num}_{STAGE_NAMES[stage_num]}"
+    state["stages"][key]["status"] = status
+    if status == "running":
+        state["current_stage"] = stage_num
+    elif status == "completed":
+        state["stages"][key]["completed_at"] = _now()
+    state["stages"][key].update(meta)
+    _save_state(state)
+
+
+def _stage_completed(state, stage_num):
+    key = f"{stage_num}_{STAGE_NAMES[stage_num]}"
+    return state["stages"][key]["status"] == "completed"
 
 
 def _banner(stage_num, total, name):
@@ -249,52 +323,119 @@ def cmd_pipeline(args):
         print(f"Model not available: {args.model}", file=sys.stderr)
         sys.exit(1)
 
-    total_stages = 8
-    has_sources = args.url or args.pdf
-
-    # Stage 1: Ingest
-    if has_sources:
-        _banner(1, total_stages, "INGEST")
-        _stage_ingest(args)
+    resume = getattr(args, "resume", False)
+    if resume:
+        state = _load_state()
+        if not state:
+            print("No pipeline state to resume. Run without --resume first.",
+                  file=sys.stderr)
+            sys.exit(1)
+        if state["status"] == "completed":
+            print("Pipeline already completed. Run without --resume to start fresh.",
+                  file=sys.stderr)
+            return
+        print("Resuming pipeline from state file", file=sys.stderr)
+        state["status"] = "running"
+        _save_state(state)
     else:
-        print("No --url or --pdf provided, skipping ingest", file=sys.stderr)
-
-    # Stage 2: Summarize
-    _banner(2, total_stages, "SUMMARIZE")
-    _stage_summarize(args)
+        state = _init_state(args)
 
-    # Stage 3: Extract
-    _banner(3, total_stages, "EXTRACT")
-    should_continue = _stage_extract(args)
-    if not should_continue:
-        return
-
-    # Stages 4-7: Derive → Review → Repair → Deduplicate (convergence loop)
-    for cycle in range(1, args.rounds + 1):
-        label = f"cycle {cycle}/{args.rounds}"
-
-        _banner(4, total_stages, f"DERIVE ({label})")
-        added = _stage_derive(args, round_label=label)
-
-        _banner(5, total_stages, f"REVIEW ({label})")
-        review_result = _stage_review(args, round_label=label)
-
-        invalid_count = review_result.get("invalid", 0)
-
-        if invalid_count > 0:
-            _banner(6, total_stages, f"REPAIR ({label})")
-            _stage_repair(args, review_result, round_label=label)
-
-        _banner(7, total_stages, f"DEDUPLICATE ({label})")
-        _stage_deduplicate(args, round_label=label)
-
-        if invalid_count == 0 and added == 0:
-            print(f"\nConverged after {cycle} cycles "
-                  f"(0 invalids, 0 new derivations)", file=sys.stderr)
-            break
-
-    # Stage 8: Export
-    _banner(8, total_stages, "EXPORT")
-    _stage_export(args)
+    total_stages = 8
+    has_sources = args.url or args.pdf
 
-    print("\nPipeline complete.", file=sys.stderr)
+    try:
+        # Stage 1: Ingest
+        if not _stage_completed(state, 1):
+            if has_sources:
+                _banner(1, total_stages, "INGEST")
+                _mark_stage(state, 1, "running")
+                _stage_ingest(args)
+                _mark_stage(state, 1, "completed")
+            else:
+                print("No --url or --pdf provided, skipping ingest", file=sys.stderr)
+                _mark_stage(state, 1, "completed", skipped=True)
+        else:
+            print("Stage 1 (INGEST) already completed, skipping", file=sys.stderr)
+
+        # Stage 2: Summarize
+        if not _stage_completed(state, 2):
+            _banner(2, total_stages, "SUMMARIZE")
+            _mark_stage(state, 2, "running")
+            _stage_summarize(args)
+            _mark_stage(state, 2, "completed")
+        else:
+            print("Stage 2 (SUMMARIZE) already completed, skipping", file=sys.stderr)
+
+        # Stage 3: Extract
+        if not _stage_completed(state, 3):
+            _banner(3, total_stages, "EXTRACT")
+            _mark_stage(state, 3, "running")
+            should_continue = _stage_extract(args)
+            _mark_stage(state, 3, "completed")
+            if not should_continue:
+                state["status"] = "paused"
+                _save_state(state)
+                return
+        else:
+            print("Stage 3 (EXTRACT) already completed, skipping", file=sys.stderr)
+
+        # Stages 4-7: Derive → Review → Repair → Deduplicate (convergence loop)
+        if not state.get("loop_completed"):
+            start_cycle = state.get("current_cycle") or 1
+            for cycle in range(start_cycle, args.rounds + 1):
+                label = f"cycle {cycle}/{args.rounds}"
+                state["current_cycle"] = cycle
+                _save_state(state)
+
+                _banner(4, total_stages, f"DERIVE ({label})")
+                _mark_stage(state, 4, "running", cycle=cycle)
+                added = _stage_derive(args, round_label=label)
+                _mark_stage(state, 4, "completed", cycle=cycle, added=added)
+
+                _banner(5, total_stages, f"REVIEW ({label})")
+                _mark_stage(state, 5, "running", cycle=cycle)
+                review_result = _stage_review(args, round_label=label)
+                invalid_count = review_result.get("invalid", 0)
+                _mark_stage(state, 5, "completed", cycle=cycle,
+                            reviewed=review_result.get("reviewed", 0),
+                            invalid=invalid_count)
+
+                if invalid_count > 0:
+                    _banner(6, total_stages, f"REPAIR ({label})")
+                    _mark_stage(state, 6, "running", cycle=cycle)
+                    _stage_repair(args, review_result, round_label=label)
+                    _mark_stage(state, 6, "completed", cycle=cycle)
+                else:
+                    _mark_stage(state, 6, "completed", cycle=cycle, skipped=True)
+
+                _banner(7, total_stages, f"DEDUPLICATE ({label})")
+                _mark_stage(state, 7, "running", cycle=cycle)
+                _stage_deduplicate(args, round_label=label)
+                _mark_stage(state, 7, "completed", cycle=cycle)
+
+                if invalid_count == 0 and added == 0:
+                    print(f"\nConverged after {cycle} cycles "
+                          f"(0 invalids, 0 new derivations)", file=sys.stderr)
+                    break
+
+            state["loop_completed"] = True
+            _save_state(state)
+        else:
+            print("Convergence loop already completed, skipping", file=sys.stderr)
+
+        # Stage 8: Export
+        _banner(8, total_stages, "EXPORT")
+        _mark_stage(state, 8, "running")
+        _stage_export(args)
+        _mark_stage(state, 8, "completed")
+
+        state["status"] = "completed"
+        _save_state(state)
+        print("\nPipeline complete.", file=sys.stderr)
+
+    except Exception as e:
+        state["status"] = "failed"
+        state["error"] = str(e)
+        state["error_traceback"] = traceback.format_exc()
+        _save_state(state)
+        raise
diff --git a/tests/test_pipeline.py b/tests/test_pipeline.py
index fd208ba..6efbd5d 100644
--- a/tests/test_pipeline.py
+++ b/tests/test_pipeline.py
@@ -1,5 +1,6 @@
 """Tests for the pipeline command."""
 
+import json
 import types
 from pathlib import Path
 from unittest.mock import patch
@@ -14,6 +15,9 @@
     _stage_review,
     _stage_repair,
     _stage_deduplicate,
+    _load_state,
+    _save_state,
+    STATE_FILE,
 )
 from expert_build.propose import auto_accept_proposals
 
@@ -41,6 +45,7 @@ def make_pipeline_args(**overrides):
         depth=2,
         timeout=600,
         domain="Test domain",
+        resume=False,
     )
     defaults.update(overrides)
     return types.SimpleNamespace(**defaults)
@@ -303,3 +308,140 @@ def test_no_auto_accept_stops_early(self, work_dir, capsys):
 
         assert not mock_derive.called
         assert not mock_export.called
+
+
+# --- Pipeline State ---
+
+class TestPipelineState:
+    def test_state_file_created_on_run(self, work_dir):
+        args = make_pipeline_args(rounds=1, url=None, pdf=None)
+        review_result = {"reviewed": 0, "invalid": 0, "results": []}
+
+        with patch("expert_build.llm.check_model_available", return_value=True), \
+             patch("expert_build.pipeline._stage_summarize"), \
+             patch("expert_build.pipeline._stage_extract", return_value=True), \
+             patch("expert_build.pipeline._stage_derive", return_value=0), \
+             patch("expert_build.pipeline._stage_review", return_value=review_result), \
+             patch("expert_build.pipeline._stage_deduplicate"), \
+             patch("expert_build.pipeline._stage_export"), \
+             patch("expert_build.caffeinate.hold"):
+            cmd_pipeline(args)
+
+        state = _load_state()
+        assert state is not None
+        assert state["status"] == "completed"
+        assert state["stages"]["8_export"]["status"] == "completed"
+
+    def test_state_records_failure(self, work_dir):
+        args = make_pipeline_args(url=None, pdf=None)
+
+        with patch("expert_build.llm.check_model_available", return_value=True), \
+             patch("expert_build.pipeline._stage_summarize",
+                   side_effect=RuntimeError("LLM exploded")), \
+             patch("expert_build.caffeinate.hold"), \
+             pytest.raises(RuntimeError, match="LLM exploded"):
+            cmd_pipeline(args)
+
+        state = _load_state()
+        assert state["status"] == "failed"
+        assert "LLM exploded" in state["error"]
+        assert state["stages"]["2_summarize"]["status"] == "running"
+
+    def test_resume_skips_completed_stages(self, work_dir, capsys):
+        args = make_pipeline_args(rounds=1, url=None, pdf=None)
+        review_result = {"reviewed": 0, "invalid": 0, "results": []}
+
+        # First run: complete through summarize, then fail at extract
+        with patch("expert_build.llm.check_model_available", return_value=True), \
+             patch("expert_build.pipeline._stage_summarize"), \
+             patch("expert_build.pipeline._stage_extract",
+                   side_effect=RuntimeError("crash")), \
+             patch("expert_build.caffeinate.hold"), \
+             pytest.raises(RuntimeError):
+            cmd_pipeline(args)
+
+        state = _load_state()
+        assert state["stages"]["1_ingest"]["status"] == "completed"
+        assert state["stages"]["2_summarize"]["status"] == "completed"
+        assert state["stages"]["3_extract"]["status"] == "running"
+
+        # Resume: should skip ingest and summarize
+        resume_args = make_pipeline_args(rounds=1, url=None, pdf=None, resume=True)
+
+        with patch("expert_build.llm.check_model_available", return_value=True), \
+             patch("expert_build.pipeline._stage_summarize") as mock_summarize, \
+             patch("expert_build.pipeline._stage_extract", return_value=True), \
+             patch("expert_build.pipeline._stage_derive", return_value=0), \
+             patch("expert_build.pipeline._stage_review", return_value=review_result), \
+             patch("expert_build.pipeline._stage_deduplicate"), \
+             patch("expert_build.pipeline._stage_export"), \
+             patch("expert_build.caffeinate.hold"):
+            cmd_pipeline(resume_args)
+
+        assert not mock_summarize.called
+        captured = capsys.readouterr()
+        assert "already completed, skipping" in captured.err
+
+        state = _load_state()
+        assert state["status"] == "completed"
+
+    def test_resume_without_state_exits(self, work_dir):
+        args = make_pipeline_args(resume=True)
+
+        with patch("expert_build.llm.check_model_available", return_value=True), \
+             patch("expert_build.caffeinate.hold"), \
+             pytest.raises(SystemExit):
+            cmd_pipeline(args)
+
+    def test_no_auto_accept_sets_paused(self, work_dir):
+        args = make_pipeline_args(no_auto_accept=True, url=None, pdf=None)
+
+        with patch("expert_build.llm.check_model_available", return_value=True), \
+             patch("expert_build.pipeline._stage_summarize"), \
+             patch("expert_build.pipeline._stage_extract", return_value=False), \
+             patch("expert_build.caffeinate.hold"):
+            cmd_pipeline(args)
+
+        state = _load_state()
+        assert state["status"] == "paused"
+        assert state["stages"]["3_extract"]["status"] == "completed"
+
+    def test_resume_completed_pipeline_returns_early(self, work_dir, capsys):
+        """Resuming an already-completed pipeline does nothing."""
+        args = make_pipeline_args(rounds=1, url=None, pdf=None)
+        review_result = {"reviewed": 0, "invalid": 0, "results": []}
+
+        with patch("expert_build.llm.check_model_available", return_value=True), \
+             patch("expert_build.pipeline._stage_summarize"), \
+             patch("expert_build.pipeline._stage_extract", return_value=True), \
+             patch("expert_build.pipeline._stage_derive", return_value=0), \
+             patch("expert_build.pipeline._stage_review", return_value=review_result), \
+             patch("expert_build.pipeline._stage_deduplicate"), \
+             patch("expert_build.pipeline._stage_export"), \
+             patch("expert_build.caffeinate.hold"):
+            cmd_pipeline(args)
+
+        state = _load_state()
+        assert state["status"] == "completed"
+
+        # Now resume — should return early
+        resume_args = make_pipeline_args(resume=True)
+        with patch("expert_build.llm.check_model_available", return_value=True), \
+             patch("expert_build.pipeline._stage_export") as mock_export, \
+             patch("expert_build.caffeinate.hold"):
+            cmd_pipeline(resume_args)
+
+        assert not mock_export.called
+        captured = capsys.readouterr()
+        assert "already completed" in captured.err
+
+    def test_corrupt_state_file_handled(self, work_dir):
+        """Corrupt state file is treated as missing."""
+        STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
+        STATE_FILE.write_text("{truncated")
+
+        args = make_pipeline_args(resume=True)
+        with patch("expert_build.llm.check_model_available", return_value=True), \
+             patch("expert_build.caffeinate.hold"), \
+             pytest.raises(SystemExit):
+            cmd_pipeline(args)

```

## Observation Results

You previously requested observations. Here are the results:

```json
{
  "project_dir_definition": {
    "error": "No function found at 'PROJECT_DIR'",
    "file": "expert_build/propose.py"
  },
  "propose_imports": {
    "file": "expert_build/propose.py",
    "imports": [
      "hashlib",
      "json",
      "re",
      "sys"
    ],
    "from_imports": [
      {
        "module": "datetime",
        "names": [
          "date"
        ]
      },
      {
        "module": "pathlib",
        "names": [
          "Path"
        ]
      },
      {
        "module": "reasons_lib.api",
        "names": [
          "add_node",
          "list_nodes"
        ]
      },
      {
        "module": "llm",
        "names": [
          "check_model_available",
          "invoke_sync"
        ]
      },
      {
        "module": "prompts",
        "names": [
          "PROPOSE_BELIEFS"
        ]
      }
    ],
    "import_section": "\"\"\"Propose and accept beliefs from entries.\"\"\"\n\nimport hashlib\nimport json\nimport re\nimport sys\nfrom datetime import date\nfrom pathlib import Path\n\nfrom reasons_lib.api import add_node, list_nodes\n\nfrom .llm import check_model_available, invoke_sync\nfrom .prompts import PROPOSE_BELIEFS\n\nPROJECT_DIR = \".expert-build\"\nREASONS_DB = \"reasons.db\"\n\n\ndef _has_embeddings() -> bool:\n    \"\"\"Check if fastembed is available.\"\"\""
  },
  "cmd_pipeline_full": {
    "function": "cmd_pipeline",
    "file": "expert_build/pipeline.py",
    "start_line": 317,
    "end_line": 441,
    "source": "def cmd_pipeline(args):\n    \"\"\"Run end-to-end EEM construction pipeline.\"\"\"\n    from .caffeinate import hold as _caffeinate\n    _caffeinate()\n\n    if not check_model_available(args.model):\n        print(f\"Model not available: {args.model}\", file=sys.stderr)\n        sys.exit(1)\n\n    resume = getattr(args, \"resume\", False)\n    if resume:\n        state = _load_state()\n        if not state:\n            print(\"No pipeline state to resume. Run without --resume first.\",\n                  file=sys.stderr)\n            sys.exit(1)\n        if state[\"status\"] == \"completed\":\n            print(\"Pipeline already completed. Run without --resume to start fresh.\",\n                  file=sys.stderr)\n            return\n        print(\"Resuming pipeline from state file\", file=sys.stderr)\n        state[\"status\"] = \"running\"\n        _save_state(state)\n    else:\n        state = _init_state(args)\n\n    total_stages = 8\n    has_sources = args.url or args.pdf\n\n    try:\n        # Stage 1: Ingest\n        if not _stage_completed(state, 1):\n            if has_sources:\n                _banner(1, total_stages, \"INGEST\")\n                _mark_stage(state, 1, \"running\")\n                _stage_ingest(args)\n                _mark_stage(state, 1, \"completed\")\n            else:\n                print(\"No --url or --pdf provided, skipping ingest\", file=sys.stderr)\n                _mark_stage(state, 1, \"completed\", skipped=True)\n        else:\n            print(\"Stage 1 (INGEST) already completed, skipping\", file=sys.stderr)\n\n        # Stage 2: Summarize\n        if not _stage_completed(state, 2):\n            _banner(2, total_stages, \"SUMMARIZE\")\n            _mark_stage(state, 2, \"running\")\n            _stage_summarize(args)\n            _mark_stage(state, 2, \"completed\")\n        else:\n            print(\"Stage 2 (SUMMARIZE) already completed, skipping\", file=sys.stderr)\n\n        # Stage 3: Extract\n        if not _stage_completed(state, 3):\n            _banner(3, total_stages, \"EXTRACT\")\n            _mark_stage(state, 3, \"running\")\n            should_continue = _stage_extract(args)\n            _mark_stage(state, 3, \"completed\")\n            if not should_continue:\n                state[\"status\"] = \"paused\"\n                _save_state(state)\n                return\n        else:\n            print(\"Stage 3 (EXTRACT) already completed, skipping\", file=sys.stderr)\n\n        # Stages 4-7: Derive \u2192 Review \u2192 Repair \u2192 Deduplicate (convergence loop)\n        if not state.get(\"loop_completed\"):\n            start_cycle = state.get(\"current_cycle\") or 1\n            for cycle in range(start_cycle, args.rounds + 1):\n                label = f\"cycle {cycle}/{args.rounds}\"\n                state[\"current_cycle\"] = cycle\n                _save_state(state)\n\n                _banner(4, total_stages, f\"DERIVE ({label})\")\n                _mark_stage(state, 4, \"running\", cycle=cycle)\n                added = _stage_derive(args, round_label=label)\n                _mark_stage(state, 4, \"completed\", cycle=cycle, added=added)\n\n                _banner(5, total_stages, f\"REVIEW ({label})\")\n                _mark_stage(state, 5, \"running\", cycle=cycle)\n                review_result = _stage_review(args, round_label=label)\n                invalid_count = review_result.get(\"invalid\", 0)\n                _mark_stage(state, 5, \"completed\", cycle=cycle,\n                            reviewed=review_result.get(\"reviewed\", 0),\n                            invalid=invalid_count)\n\n                if invalid_count > 0:\n                    _banner(6, total_stages, f\"REPAIR ({label})\")\n                    _mark_stage(state, 6, \"running\", cycle=cycle)\n                    _stage_repair(args, review_result, round_label=label)\n                    _mark_stage(state, 6, \"completed\", cycle=cycle)\n                else:\n                    _mark_stage(state, 6, \"completed\", cycle=cycle, skipped=True)\n\n                _banner(7, total_stages, f\"DEDUPLICATE ({label})\")\n                _mark_stage(state, 7, \"running\", cycle=cycle)\n                _stage_deduplicate(args, round_label=label)\n                _mark_stage(state, 7, \"completed\", cycle=cycle)\n\n                if invalid_count == 0 and added == 0:\n                    print(f\"\\nConverged after {cycle} cycles \"\n                          f\"(0 invalids, 0 new derivations)\", file=sys.stderr)\n                    break\n\n            state[\"loop_completed\"] = True\n            _save_state(state)\n        else:\n            print(\"Convergence loop already completed, skipping\", file=sys.stderr)\n\n        # Stage 8: Export\n        _banner(8, total_stages, \"EXPORT\")\n        _mark_stage(state, 8, \"running\")\n        _stage_export(args)\n        _mark_stage(state, 8, \"completed\")\n\n        state[\"status\"] = \"completed\"\n        _save_state(state)\n        print(\"\\nPipeline complete.\", file=sys.stderr)\n\n    except Exception as e:\n        state[\"status\"] = \"failed\"\n        state[\"error\"] = str(e)\n        state[\"error_traceback\"] = traceback.format_exc()\n        _save_state(state)\n        raise"
  },
  "stage_extract_body": {
    "function": "_stage_extract",
    "file": "expert_build/pipeline.py",
    "start_line": 133,
    "end_line": 163,
    "source": "def _stage_extract(args):\n    \"\"\"Stage 3: Extract beliefs from entries and optionally auto-accept.\"\"\"\n    from .propose import cmd_propose_beliefs, cmd_accept_beliefs\n\n    prop_args = SimpleNamespace(\n        input_dir=\"entries\",\n        output=\"proposed-beliefs.md\",\n        model=args.model,\n        batch_size=5,\n        entry=None,\n    )\n    setattr(prop_args, \"all\", False)\n\n    cmd_propose_beliefs(prop_args)\n\n    if args.no_auto_accept:\n        print(\"\\nStopping after propose-beliefs (--no-auto-accept)\", file=sys.stderr)\n        print(\"Review proposed-beliefs.md, mark entries as [ACCEPT], then run:\", file=sys.stderr)\n        print(\"  expert-build accept-beliefs\", file=sys.stderr)\n        return False\n\n    proposals_path = Path(\"proposed-beliefs.md\")\n    if proposals_path.exists():\n        from .propose import auto_accept_proposals\n        auto_accept_proposals(str(proposals_path))\n        print(\"Auto-accepted all proposed beliefs\", file=sys.stderr)\n\n        accept_args = SimpleNamespace(file=\"proposed-beliefs.md\")\n        cmd_accept_beliefs(accept_args)\n\n    return True"
  },
  "stage_derive_body": {
    "function": "_stage_derive",
    "file": "expert_build/pipeline.py",
    "start_line": 166,
    "end_line": 216,
    "source": "def _stage_derive(args, round_label=\"\"):\n    \"\"\"Stage 4: Derive new beliefs until saturated or max rounds hit.\n\n    Returns total number of beliefs added.\n    \"\"\"\n    from reasons_lib.api import export_network\n    from reasons_lib.derive import build_prompt, parse_proposals, validate_proposals, apply_proposals\n\n    total_added = 0\n    prefix = f\"[{round_label}] \" if round_label else \"\"\n\n    for derive_round in range(1, args.max_derive_rounds + 1):\n        print(f\"{prefix}Derive round {derive_round}/{args.max_derive_rounds}...\",\n              file=sys.stderr)\n\n        data = export_network(db_path=REASONS_DB)\n        nodes = data.get(\"nodes\", {})\n        if not nodes:\n            print(f\"{prefix}No nodes in network\", file=sys.stderr)\n            break\n\n        prompt, stats = build_prompt(nodes, domain=args.domain)\n        print(f\"{prefix}  Network: {stats['total_in']} IN, \"\n              f\"{stats['total_derived']} derived, depth {stats['max_depth']}\",\n              file=sys.stderr)\n\n        try:\n            response = invoke_sync(prompt, model=args.model, timeout=args.timeout)\n        except Exception as e:\n            print(f\"{prefix}  Derive error: {e}\", file=sys.stderr)\n            break\n\n        proposals = parse_proposals(response)\n        if not proposals:\n            print(f\"{prefix}  Saturated (no proposals)\", file=sys.stderr)\n            break\n\n        valid, skipped = validate_proposals(proposals, nodes)\n        for p, reason in skipped:\n            print(f\"{prefix}  SKIP {p['id']}: {reason}\", file=sys.stderr)\n\n        if not valid:\n            print(f\"{prefix}  Saturated (no valid proposals)\", file=sys.stderr)\n            break\n\n        results = apply_proposals(valid, db_path=REASONS_DB)\n        added = sum(1 for _, r in results if isinstance(r, dict))\n        total_added += added\n        print(f\"{prefix}  Added {added} beliefs\", file=sys.stderr)\n\n    return total_added"
  },
  "stage_review_body": {
    "function": "_stage_review",
    "file": "expert_build/pipeline.py",
    "start_line": 219,
    "end_line": 239,
    "source": "def _stage_review(args, round_label=\"\"):\n    \"\"\"Stage 5: Review derived beliefs for validity.\n\n    Returns the review results dict.\n    \"\"\"\n    from reasons_lib.api import review_beliefs\n\n    prefix = f\"[{round_label}] \" if round_label else \"\"\n    print(f\"{prefix}Reviewing beliefs...\", file=sys.stderr)\n\n    result = review_beliefs(\n        model=args.model,\n        timeout=args.timeout,\n        db_path=REASONS_DB,\n    )\n\n    reviewed = result.get(\"reviewed\", 0)\n    invalid = result.get(\"invalid\", 0)\n    print(f\"{prefix}  Reviewed {reviewed}, invalid {invalid}\", file=sys.stderr)\n\n    return result"
  },
  "work_dir_fixture": {
    "symbol": "work_dir",
    "usages": [
      {
        "file": "tests/test_propose.py",
        "line": 21,
        "text": "def work_dir(tmp_path, monkeypatch):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 39,
        "text": "def test_proposals_written_after_each_batch(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 44,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 66,
        "text": "def test_all_batches_written_on_success(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 70,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 90,
        "text": "def test_existing_beliefs_filtered_per_batch(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 93,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 115,
        "text": "def test_failed_batch_entries_not_marked_processed(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 120,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 137,
        "text": "processed_path = work_dir / \".expert-build\" / \"proposed-entries.json\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 147,
        "text": "def test_filter_regex_matches_accept_reject_placeholder(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 151,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 173,
        "text": "def test_appends_to_existing_output_file(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 177,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_summarize.py",
        "line": 24,
        "text": "def work_dir(tmp_path, monkeypatch):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 38,
        "text": "def test_discovers_md_files(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 51,
        "text": "def test_discovers_py_files(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 64,
        "text": "def test_discovers_both_md_and_py(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 79,
        "text": "def test_ignores_other_extensions(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 93,
        "text": "def test_uses_summarize_code_for_py(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 107,
        "text": "def test_uses_summarize_for_md(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 123,
        "text": "def test_truncation_warning_for_large_file(source_dir, work_dir, capsys):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 138,
        "text": "def test_truncation_content_is_capped(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 153,
        "text": "def test_no_truncation_warning_for_small_file(source_dir, work_dir, capsys):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 169,
        "text": "def test_skips_already_summarized(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 171,
        "text": "manifest = work_dir / \".summarized\""
      },
      {
        "file": "tests/test_summarize.py",
        "line": 182,
        "text": "def test_manifest_records_processed_file(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 192,
        "text": "manifest = work_dir / \".summarized\""
      },
      {
        "file": "tests/test_summarize.py",
        "line": 199,
        "text": "def test_strips_frontmatter_before_summarizing(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 215,
        "text": "def test_skips_empty_content_after_frontmatter(source_dir, work_dir, capsys):"
      }
    ],
    "production_usages": [],
    "test_usages": [
      {
        "file": "tests/test_propose.py",
        "line": 21,
        "text": "def work_dir(tmp_path, monkeypatch):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 39,
        "text": "def test_proposals_written_after_each_batch(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 44,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 66,
        "text": "def test_all_batches_written_on_success(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 70,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 90,
        "text": "def test_existing_beliefs_filtered_per_batch(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 93,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 115,
        "text": "def test_failed_batch_entries_not_marked_processed(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 120,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 137,
        "text": "processed_path = work_dir / \".expert-build\" / \"proposed-entries.json\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 147,
        "text": "def test_filter_regex_matches_accept_reject_placeholder(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 151,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_propose.py",
        "line": 173,
        "text": "def test_appends_to_existing_output_file(entries_dir, work_dir):"
      },
      {
        "file": "tests/test_propose.py",
        "line": 177,
        "text": "output = work_dir / \"proposed-beliefs.md\""
      },
      {
        "file": "tests/test_summarize.py",
        "line": 24,
        "text": "def work_dir(tmp_path, monkeypatch):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 38,
        "text": "def test_discovers_md_files(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 51,
        "text": "def test_discovers_py_files(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 64,
        "text": "def test_discovers_both_md_and_py(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 79,
        "text": "def test_ignores_other_extensions(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 93,
        "text": "def test_uses_summarize_code_for_py(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 107,
        "text": "def test_uses_summarize_for_md(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 123,
        "text": "def test_truncation_warning_for_large_file(source_dir, work_dir, capsys):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 138,
        "text": "def test_truncation_content_is_capped(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 153,
        "text": "def test_no_truncation_warning_for_small_file(source_dir, work_dir, capsys):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 169,
        "text": "def test_skips_already_summarized(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 171,
        "text": "manifest = work_dir / \".summarized\""
      },
      {
        "file": "tests/test_summarize.py",
        "line": 182,
        "text": "def test_manifest_records_processed_file(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 192,
        "text": "manifest = work_dir / \".summarized\""
      },
      {
        "file": "tests/test_summarize.py",
        "line": 199,
        "text": "def test_strips_frontmatter_before_summarizing(source_dir, work_dir):"
      },
      {
        "file": "tests/test_summarize.py",
        "line": 215,
        "text": "def test_skips_empty_content_after_frontmatter(source_dir, work_dir, capsys):"
      }
    ],
    "production_count": 0,
    "test_count": 57,
    "total_count": 57
  },
  "test_conftest": {
    "error": "File not found: tests/conftest.py",
    "file": "tests/conftest.py"
  },
  "state_file_usages": {
    "symbol": "STATE_FILE",
    "usages": [
      {
        "file": "tests/test_pipeline.py",
        "line": 20,
        "text": "STATE_FILE,"
      },
      {
        "file": "tests/test_pipeline.py",
        "line": 440,
        "text": "STATE_FILE.parent.mkdir(parents=True, exist_ok=True)"
      },
      {
        "file": "tests/test_pipeline.py",
        "line": 441,
        "text": "STATE_FILE.write_text(\"{truncated\")"
      },
      {
        "file": "expert_build/pipeline.py",
        "line": 13,
        "text": "STATE_FILE = Path(PROJECT_DIR) / \"pipeline-state.json\""
      },
      {
        "file": "expert_build/pipeline.py",
        "line": 54,
        "text": "if not STATE_FILE.exists():"
      },
      {
        "file": "expert_build/pipeline.py",
        "line": 57,
        "text": "return json.loads(STATE_FILE.read_text())"
      },
      {
        "file": "expert_build/pipeline.py",
        "line": 59,
        "text": "print(f\"WARNING: corrupt state file {STATE_FILE}, ignoring\","
      },
      {
        "file": "expert_build/pipeline.py",
        "line": 66,
        "text": "STATE_FILE.parent.mkdir(parents=True, exist_ok=True)"
      },
      {
        "file": "expert_build/pipeline.py",
        "line": 67,
        "text": "STATE_FILE.write_text(json.dumps(state, indent=2) + \"\\n\")"
      }
    ],
    "production_usages": [
      {
        "file": "expert_build/pipeline.py",
        "line": 13,
        "text": "STATE_FILE = Path(PROJECT_DIR) / \"pipeline-state.json\""
      },
      {
        "file": "expert_build/pipeline.py",
        "line": 54,
        "text": "if not STATE_FILE.exists():"
      },
      {
        "file": "expert_build/pipeline.py",
        "line": 57,
        "text": "return json.loads(STATE_FILE.read_text())"
      },
      {
        "file": "expert_build/pipeline.py",
        "line": 59,
        "text": "print(f\"WARNING: corrupt state file {STATE_FILE}, ignoring\","
      },
      {
        "file": "expert_build/pipeline.py",
        "line": 66,
        "text": "STATE_FILE.parent.mkdir(parents=True, exist_ok=True)"
      },
      {
        "file": "expert_build/pipeline.py",
        "line": 67,
        "text": "STATE_FILE.write_text(json.dumps(state, indent=2) + \"\\n\")"
      }
    ],
    "test_usages": [
      {
        "file": "tests/test_pipeline.py",
        "line": 20,
        "text": "STATE_FILE,"
      },
      {
        "file": "tests/test_pipeline.py",
        "line": 440,
        "text": "STATE_FILE.parent.mkdir(parents=True, exist_ok=True)"
      },
      {
        "file": "tests/test_pipeline.py",
        "line": 441,
        "text": "STATE_FILE.write_text(\"{truncated\")"
      }
    ],
    "production_count": 6,
    "test_count": 3,
    "total_count": 9
  },
  "caffeinate_hold": {
    "function": "hold",
    "file": "expert_build/caffeinate.py",
    "start_line": 10,
    "end_line": 25,
    "source": "def hold():\n    \"\"\"Start caffeinate to prevent idle sleep. No-op on non-macOS.\"\"\"\n    global _process\n    if _process is not None:\n        return\n    if platform.system() != \"Darwin\":\n        return\n    try:\n        _process = subprocess.Popen(\n            [\"caffeinate\", \"-i\"],\n            stdout=subprocess.DEVNULL,\n            stderr=subprocess.DEVNULL,\n        )\n        atexit.register(release)\n    except FileNotFoundError:\n        pass"
  },
  "stage_exceptions": {
    "function": "_stage_ingest",
    "file": "expert_build/pipeline.py",
    "explicit_raises": [],
    "calls": [
      "SimpleNamespace",
      "cmd_fetch_docs",
      "print",
      "cmd_chunk_pdf"
    ]
  },
  "pipeline_imports": {
    "file": "expert_build/pipeline.py",
    "imports": [
      "json",
      "sys",
      "traceback"
    ],
    "from_imports": [
      {
        "module": "datetime",
        "names": [
          "datetime",
          "timezone"
        ]
      },
      {
        "module": "pathlib",
        "names": [
          "Path"
        ]
      },
      {
        "module": "types",
        "names": [
          "SimpleNamespace"
        ]
      },
      {
        "module": "llm",
        "names": [
          "check_model_available",
          "invoke_sync"
        ]
      },
      {
        "module": "propose",
        "names": [
          "PROJECT_DIR",
          "REASONS_DB"
        ]
      }
    ],
    "import_section": "\"\"\"End-to-end EEM construction pipeline.\"\"\"\n\nimport json\nimport sys\nimport traceback\nfrom datetime import datetime, timezone\nfrom pathlib import Path\nfrom types import SimpleNamespace\n\nfrom .llm import check_model_available, invoke_sync\nfrom .propose import PROJECT_DIR, REASONS_DB\n\nSTATE_FILE = Path(PROJECT_DIR) / \"pipeline-state.json\"\n\nSTAGE_NAMES = {\n    1: \"ingest\",\n    2: \"summarize\",\n    3: \"extract\",\n    4: \"derive\",\n    5: \"review\",\n    6: \"repair\",\n    7: \"deduplicate\",\n    8: \"export\",\n}\n\n\ndef _now():\n    return datetime.now(timezone.utc).isoformat(timespec=\"seconds\")\n\n\ndef _init_state(args):\n    state = {\n        \"started_at\": _now(),\n        \"updated_at\": _now(),\n        \"status\": \"running\",\n        \"current_stage\": None,\n        \"current_cycle\": None,\n        \"args\": {\n            \"url\": getattr(args, \"url\", None),\n            \"model\": args.model,\n            \"rounds\": args.rounds,\n            \"domain\": getattr(args, \"domain\", None),\n        },\n        \"stages\": {\n            f\"{n}_{name}\": {\"status\": \"pending\"}\n            for n, name in STAGE_NAMES.items()\n        },\n    }\n    _save_state(state)\n    return state\n\n\ndef _load_state():\n    if not STATE_FILE.exists():\n        return None\n    try:\n        return json.loads(STATE_FILE.read_text())\n    except (json.JSONDecodeError, ValueError):\n        print(f\"WARNING: corrupt state file {STATE_FILE}, ignoring\",\n              file=sys.stderr)\n        return None\n\n\ndef _save_state(state):\n    state[\"updated_at\"] = _now()\n    STATE_FILE.parent.mkdir(parents=True, exist_ok=True)\n    STATE_FILE.write_text(json.dumps(state, indent=2) + \"\\n\")\n\n\ndef _mark_stage(state, stage_num, status, **meta):\n    key = f\"{stage_num}_{STAGE_NAMES[stage_num]}\"\n    state[\"stages\"][key][\"status\"] = status\n    if status == \"running\":\n        state[\"current_stage\"] = stage_num\n    elif status == \"completed\":\n        state[\"stages\"][key][\"completed_at\"] = _now()\n    state[\"stages\"][key].update(meta)\n    _save_state(state)\n\n\ndef _stage_completed(state, stage_num):\n    key = f\"{stage_num}_{STAGE_NAMES[stage_num]}\"\n    return state[\"stages\"][key][\"status\"] == \"completed\"\n\n\ndef _banner(stage_num, total, name):\n    print(f\"\\n{'=' * 50}\", file=sys.stderr)\n    print(f\"  Stage {stage_num}/{total}: {name}\", file=sys.stderr)\n    print(f\"{'=' * 50}\\n\", file=sys.stderr)\n\n\ndef _stage_ingest(args):\n    \"\"\"Stage 1: Fetch docs or chunk PDFs into sources/.\"\"\""
  }
}
```

Use these results to inform your review. Do not request the same observations again.


## Instructions

For each significant change (new file, modified function, etc.), provide a structured verdict.

Use this exact format for each change:

### <file_path or file_path:function_name>
VERDICT: PASS | CONCERN | BLOCK
CORRECTNESS: VALID | QUESTIONABLE | BROKEN
SPEC_COMPLIANCE: MEETS | PARTIAL | VIOLATES | N/A
ISSUE_COMPLIANCE: ADDRESSES | PARTIAL | UNRELATED | N/A
BELIEF_COMPLIANCE: CONSISTENT | VIOLATES | N/A
TEST_COVERAGE: COVERED | PARTIAL | UNTESTED
INTEGRATION: WIRED | PARTIAL | MISSING
REASONING: <brief explanation of your assessment>
---

## Review Criteria

1. **CORRECTNESS**: Does the code do what it claims? Is the logic sound?
   - VALID: Logic is correct, no bugs apparent
   - QUESTIONABLE: Logic may have edge cases or unclear behavior
   - BROKEN: Clear bugs or incorrect behavior

2. **SPEC_COMPLIANCE**: Does it meet MUST requirements from the spec?
   - MEETS: All relevant spec requirements satisfied
   - PARTIAL: Some requirements met, others missing or incomplete
   - VIOLATES: Contradicts spec requirements
   - N/A: No spec provided or not applicable

3. **ISSUE_COMPLIANCE** (only when an issue is provided): Do the changes address the problem or feature described in the issue?
   - ADDRESSES: Changes directly solve the issue's stated problem or implement the requested feature
   - PARTIAL: Changes partially address the issue but leave some aspects unresolved
   - UNRELATED: Changes do not appear related to the issue
   - N/A: No issue provided

4. **TEST_COVERAGE**: Are there tests for the new/changed code?
   - COVERED: Tests exist and cover the changes
   - PARTIAL: Some tests exist but coverage is incomplete
   - UNTESTED: No tests for the changes

5. **INTEGRATION**: Are callers updated? Is the feature usable end-to-end?
   - WIRED: Feature is fully integrated and usable
   - PARTIAL: Interface exists but callers not updated, or integration incomplete
   - MISSING: No integration with existing code

6. **BELIEF_COMPLIANCE** (only when beliefs are provided): Do the changes respect known architectural invariants, contracts, and rules?
   - CONSISTENT: Changes align with or reinforce known beliefs
   - VIOLATES: Changes contradict a specific belief — cite the belief ID
   - N/A: No beliefs provided or no relevant beliefs apply

## Verdict Guidelines

- **BLOCK**: Security issues, broken functionality, spec violations, or missing critical integration
- **CONCERN**: Missing tests, partial integration, questionable patterns, or unclear logic
- **PASS**: Correct, tested, well-integrated code

## Important

- Full function bodies for modified functions may be available in the observations section — use them to verify the complete logic, not just the diff hunks
- Related test files (prefixed with ``related_test:``) may be included in observations — check whether existing test assertions still match modified return types, signatures, or behavior. Flag any test that would break due to the changes
- If duplicate test coverage is detected (multiple test files covering the same source), note it in your review
- Focus on actual issues, not style preferences
- If a method signature is added but callers aren't updated, that's PARTIAL integration
- Be specific in reasoning - reference line numbers or function names
- When in doubt, use CONCERN rather than PASS

## Self-Review

After completing your review, add a brief self-assessment:

### SELF_REVIEW
LIMITATIONS: <what context were you missing that affected review quality?>
---

Examples of limitations:
- "Could not see full class to verify no other methods access the modified field"
- "Test file not included in diff - cannot verify coverage claims"
- "Spec file referenced but not provided"


## Feature Requests

If this review tool could be improved to help you do a better job, suggest features:

### FEATURE_REQUESTS
- <suggestion 1>
- <suggestion 2>
---

Examples:
- "Include full file context for modified functions, not just diff hunks"
- "Show callers of modified methods to verify integration"
- "Include test file alongside implementation changes"

Only include this section if you have specific suggestions. Skip if none.
