feat(storage): X14 Phase 2b — route extracted-text + async DOCX exports through storage.py

Continue the write-site rewiring onto the unified storage layer (INV-STG1):
- services/processor.py: extracted-text .txt → DERIVED bucket (a derived
  artifact; the DB column is the source of truth per INV-STG5, so the write
  stays non-fatal)
- services/docx_exporter.py (export_decision): DOCX → DOCUMENTS bucket via
  BytesIO → put_bytes, with a fallback to a direct disk write when the caller
  passes an output_path outside DATA_DIR
- services/analysis_docx_exporter.py (build_analysis_docx): same pattern;
  out_path is always under DATA_DIR

Under the default STORAGE_BACKEND=filesystem the bytes land at the exact
legacy path (put_bytes → DATA_DIR/key), so behaviour is unchanged. The
disk-reading bits that must stay for now (export_dir glob in _next_version)
are kept; storage-native versioning is a cutover concern.

Still on disk (sync call-sites, follow-up Phase 2c): docx_reviser
(track-changes), docx_retrofit backup, and multimodal thumbnails (rendered in
a to_thread). git-tracked text (case.json/notes/research-md/draft-md) stays on
disk by design (INV-STG7).

tests: 38 storage + docx tests green (incl. test_export_qa_gate /
test_docx_exporter_bookmarks which exercise the real export path); 242
collected, no import breakage.

Keeps G2; advances INV-STG1. Spec: docs/spec/X14-storage-minio.md.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-08 08:05:25 +00:00
parent 39f8cb7c15
commit 1f42a39ce4
3 changed files with 40 additions and 12 deletions

View File

@@ -5,6 +5,7 @@
from __future__ import annotations
import io
import logging
import re
from datetime import date
@@ -17,7 +18,7 @@ from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from legal_mcp import config
from legal_mcp.services import db
from legal_mcp.services import db, storage
logger = logging.getLogger(__name__)
@@ -474,8 +475,19 @@ async def export_decision(
pass
output_path = str(export_dir / f"{prefix}-v{next_ver}.docx")
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
doc.save(output_path)
# Persist through the storage layer (INV-STG1). Under the filesystem
# backend the bytes land at output_path exactly as before; a caller-
# provided path outside DATA_DIR falls back to a direct disk write.
buf = io.BytesIO()
doc.save(buf)
data = buf.getvalue()
_docx_ctype = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
try:
key = Path(output_path).resolve().relative_to(Path(config.DATA_DIR).resolve()).as_posix()
await storage.put_bytes(key, data, bucket=storage.Bucket.DOCUMENTS, content_type=_docx_ctype)
except ValueError:
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
Path(output_path).write_bytes(data)
logger.info("DOCX exported (mode=%s): %s", mode, output_path)
return output_path