From 28234490ca46a72fcd75ed4399cf771d3aef542e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 21 Jun 2026 00:09:21 +0000 Subject: [PATCH] fix: duplicate Case Nos when registry missing and export path double-prefix allocate_case_no() returned 2026-DEL-001-001 on every call when courtroom/case-registry.yaml was absent, without creating the registry. Now initializes the registry on first allocation and increments normally. export_transcript.py joined BASE_DIR (courtroom/) with paths like courtroom/transcripts/foo.md, resolving to a nonexistent double path. Documented README usage now works via resolve_transcript_path(). Regression tests added for both issues. Co-authored-by: Jack J Burleson // LJM --- courtroom/portal/export_transcript.py | 19 ++++++++++++++++--- litigation/run.py | 13 ++++++++++++- tests/test_export_transcript.py | 19 +++++++++++++++++++ tests/test_litigation_run.py | 17 +++++++++++++++++ 4 files changed, 64 insertions(+), 4 deletions(-) diff --git a/courtroom/portal/export_transcript.py b/courtroom/portal/export_transcript.py index 0b09f24..f83d5d5 100644 --- a/courtroom/portal/export_transcript.py +++ b/courtroom/portal/export_transcript.py @@ -206,6 +206,21 @@ def apply_personality_styling(html: str) -> str: return html +def resolve_transcript_path(raw: str) -> Path: + """Resolve transcript path relative to courtroom/ (BASE_DIR). + + Accepts paths relative to repo root (courtroom/transcripts/foo.md), + courtroom/ (transcripts/foo.md), or absolute paths. + """ + src = Path(raw) + if src.is_absolute(): + return src + rel = raw.lstrip("/") + if rel.startswith("courtroom/"): + rel = rel[len("courtroom/") :] + return BASE_DIR / rel + + def extract_title(md_path: Path, content: str) -> str: """Derive a human-readable title from path or first H1.""" m = re.search(r"^#\s+(.+)$", content, re.MULTILINE) @@ -229,9 +244,7 @@ def main() -> int: parser.add_argument("-o", "--output", help="Output HTML path (default: portal/exports/.html)") args = parser.parse_args() - src = Path(args.transcript) - if not src.is_absolute(): - src = BASE_DIR / args.transcript.lstrip("/") + src = resolve_transcript_path(args.transcript) if not src.exists(): print(f"Error: File not found: {src}", file=sys.stderr) return 1 diff --git a/litigation/run.py b/litigation/run.py index c86de43..56feb79 100644 --- a/litigation/run.py +++ b/litigation/run.py @@ -72,7 +72,18 @@ def allocate_case_no(category: str = "DEL", *, deliberation: int = 1) -> str: year = datetime.now().strftime("%Y") if not REGISTRY_PATH.exists(): - return f"{year}-{category}-001-{deliberation:03d}" + case_no = f"{year}-{category}-001-{deliberation:03d}" + data = { + "year": int(year), + "last_updated": datetime.now().strftime("%Y-%m-%d"), + "categories": {category: 2}, + } + REGISTRY_PATH.parent.mkdir(parents=True, exist_ok=True) + REGISTRY_PATH.write_text( + yaml.dump(data, default_flow_style=False, sort_keys=False), + encoding="utf-8", + ) + return case_no data = yaml.safe_load(REGISTRY_PATH.read_text(encoding="utf-8")) or {} year = str(data.get("year", year)) diff --git a/tests/test_export_transcript.py b/tests/test_export_transcript.py index a65b141..bf68e11 100644 --- a/tests/test_export_transcript.py +++ b/tests/test_export_transcript.py @@ -36,3 +36,22 @@ def test_extract_title_from_dated_filename(): mod = _load_export_module() path = Path("2026-02-15-framework-enhancement-analysis.md") assert mod.extract_title(path, "") == "Framework Enhancement Analysis" + + +def test_resolve_transcript_path_accepts_repo_root_prefix(): + """README documents courtroom/transcripts/foo.md from repo root.""" + mod = _load_export_module() + sample = "2026-02-15-framework-enhancement-analysis.md" + resolved = mod.resolve_transcript_path(f"courtroom/transcripts/{sample}") + expected = REPO_ROOT / "courtroom" / "transcripts" / sample + assert resolved == expected + assert resolved.exists() + + +def test_resolve_transcript_path_accepts_courtroom_relative_prefix(): + mod = _load_export_module() + sample = "2026-02-15-framework-enhancement-analysis.md" + resolved = mod.resolve_transcript_path(f"transcripts/{sample}") + expected = REPO_ROOT / "courtroom" / "transcripts" / sample + assert resolved == expected + assert resolved.exists() diff --git a/tests/test_litigation_run.py b/tests/test_litigation_run.py index 8e46431..488f782 100644 --- a/tests/test_litigation_run.py +++ b/tests/test_litigation_run.py @@ -76,3 +76,20 @@ def test_save_transcript_filename_suffix_independent_of_case_no( assert path.name.endswith("-1.md") assert "**Case No.:** 2026-DEL-020-001" in content + + +def test_allocate_case_no_creates_registry_when_missing( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """Missing case-registry.yaml must not return duplicate 001 on every call.""" + registry = tmp_path / "case-registry.yaml" + monkeypatch.setattr(litigation_run, "REGISTRY_PATH", registry) + + first = litigation_run.allocate_case_no("DEL") + second = litigation_run.allocate_case_no("DEL") + + assert first == f"{litigation_run.datetime.now().strftime('%Y')}-DEL-001-001" + assert second == f"{litigation_run.datetime.now().strftime('%Y')}-DEL-002-001" + assert registry.exists() + data = yaml.safe_load(registry.read_text(encoding="utf-8")) + assert data["categories"]["DEL"] == 3