@@ -1,39 +1,59 @@ |
| 1 | | -"""Persist staged auto-synth instruction sections between CLI steps.""" |
| 1 | +"""Persist staged auto-synth instruction sections between CLI steps. |
| 2 | + |
| 3 | +I/O is shared with `dlm.preference.pending` via `dlm._pending`. |
| 4 | +""" |
| 2 | 5 | |
| 3 | 6 | from __future__ import annotations |
| 4 | 7 | |
| 5 | | -import json |
| 6 | 8 | from dataclasses import dataclass |
| 7 | | -from datetime import UTC, datetime |
| 8 | | -from pathlib import Path |
| 9 | | -from typing import TYPE_CHECKING, Any |
| 10 | | - |
| 11 | | -from dlm.doc.sections import Section, SectionType |
| 12 | | -from dlm.io.atomic import write_text as atomic_write_text |
| 9 | +from typing import TYPE_CHECKING |
| 10 | + |
| 11 | +from dlm._pending import ( |
| 12 | + PendingSectionPlan, |
| 13 | + _optional_float, |
| 14 | + _optional_int, |
| 15 | + _optional_str, |
| 16 | + _section_from_payload, |
| 17 | + _section_to_payload, |
| 18 | +) |
| 19 | +from dlm._pending import ( |
| 20 | + clear_pending_plan as _clear, |
| 21 | +) |
| 22 | +from dlm._pending import ( |
| 23 | + load_pending_plan as _load, |
| 24 | +) |
| 25 | +from dlm._pending import ( |
| 26 | + pending_plan_path as _path, |
| 27 | +) |
| 28 | +from dlm._pending import ( |
| 29 | + save_pending_plan as _save, |
| 30 | +) |
| 13 | 31 | from dlm.synth.errors import SynthError |
| 14 | 32 | |
| 15 | 33 | if TYPE_CHECKING: |
| 16 | 34 | from collections.abc import Sequence |
| 35 | + from pathlib import Path |
| 17 | 36 | |
| 37 | + from dlm.doc.sections import Section |
| 18 | 38 | from dlm.store.paths import StorePath |
| 19 | 39 | |
| 20 | 40 | |
| 41 | +_SUBDIR = "synth" |
| 42 | +_LABEL = "synth plan" |
| 43 | + |
| 44 | + |
| 21 | 45 | class PendingSynthPlanError(SynthError): |
| 22 | 46 | """Raised when the staged synth plan cannot be read or validated.""" |
| 23 | 47 | |
| 24 | 48 | |
| 25 | 49 | @dataclass(frozen=True) |
| 26 | | -class PendingSynthPlan: |
| 50 | +class PendingSynthPlan(PendingSectionPlan): |
| 27 | 51 | """One staged synth plan for a store.""" |
| 28 | 52 | |
| 29 | | - source_path: Path |
| 30 | | - created_at: str |
| 31 | | - sections: tuple[Section, ...] |
| 32 | | - |
| 33 | 53 | |
| 34 | 54 | def pending_plan_path(store: StorePath) -> Path: |
| 35 | 55 | """Path to the staged synth payload for `store`.""" |
| 36 | | - return store.root / "synth" / "pending.json" |
| 56 | + return _path(store, subdir=_SUBDIR) |
| 37 | 57 | |
| 38 | 58 | |
| 39 | 59 | def save_pending_plan( |
@@ -43,160 +63,41 @@ def save_pending_plan( |
| 43 | 63 | sections: Sequence[Section], |
| 44 | 64 | ) -> PendingSynthPlan: |
| 45 | 65 | """Persist `sections` as the staged synth plan for `store`.""" |
| 46 | | - plan = PendingSynthPlan( |
| 47 | | - source_path=source_path.resolve(), |
| 48 | | - created_at=_utcnow(), |
| 49 | | - sections=tuple(sections), |
| 66 | + return _save( # type: ignore[return-value] |
| 67 | + store, |
| 68 | + source_path=source_path, |
| 69 | + sections=sections, |
| 70 | + subdir=_SUBDIR, |
| 71 | + plan_cls=PendingSynthPlan, |
| 50 | 72 | ) |
| 51 | | - path = pending_plan_path(store) |
| 52 | | - path.parent.mkdir(parents=True, exist_ok=True) |
| 53 | | - payload = { |
| 54 | | - "schema_version": 1, |
| 55 | | - "source_path": str(plan.source_path), |
| 56 | | - "created_at": plan.created_at, |
| 57 | | - "sections": [_section_to_payload(section) for section in plan.sections], |
| 58 | | - } |
| 59 | | - atomic_write_text(path, json.dumps(payload, indent=2, sort_keys=True) + "\n") |
| 60 | | - return plan |
| 61 | 73 | |
| 62 | 74 | |
| 63 | 75 | def load_pending_plan(store: StorePath) -> PendingSynthPlan | None: |
| 64 | 76 | """Return the staged synth plan for `store`, or None when absent.""" |
| 65 | | - path = pending_plan_path(store) |
| 66 | | - if not path.exists(): |
| 67 | | - return None |
| 68 | | - try: |
| 69 | | - raw = json.loads(path.read_text(encoding="utf-8")) |
| 70 | | - except OSError as exc: |
| 71 | | - raise PendingSynthPlanError(f"could not read staged synth plan: {exc}") from exc |
| 72 | | - except json.JSONDecodeError as exc: |
| 73 | | - raise PendingSynthPlanError(f"staged synth plan is not valid JSON: {exc}") from exc |
| 74 | | - |
| 75 | | - if not isinstance(raw, dict): |
| 76 | | - raise PendingSynthPlanError("staged synth plan must be a JSON object") |
| 77 | | - if raw.get("schema_version") != 1: |
| 78 | | - raise PendingSynthPlanError( |
| 79 | | - f"unsupported staged synth plan schema_version={raw.get('schema_version')!r}" |
| 80 | | - ) |
| 81 | | - |
| 82 | | - source_path = raw.get("source_path") |
| 83 | | - created_at = raw.get("created_at") |
| 84 | | - sections_raw = raw.get("sections") |
| 85 | | - if not isinstance(source_path, str) or not source_path: |
| 86 | | - raise PendingSynthPlanError("staged synth plan is missing source_path") |
| 87 | | - if not isinstance(created_at, str) or not created_at: |
| 88 | | - raise PendingSynthPlanError("staged synth plan is missing created_at") |
| 89 | | - if not isinstance(sections_raw, list): |
| 90 | | - raise PendingSynthPlanError("staged synth plan is missing sections") |
| 91 | | - |
| 92 | | - sections: list[Section] = [] |
| 93 | | - for idx, entry in enumerate(sections_raw): |
| 94 | | - try: |
| 95 | | - sections.append(_section_from_payload(entry)) |
| 96 | | - except (TypeError, ValueError, KeyError) as exc: |
| 97 | | - raise PendingSynthPlanError(f"invalid section payload at index {idx}: {exc}") from exc |
| 98 | | - |
| 99 | | - return PendingSynthPlan( |
| 100 | | - source_path=Path(source_path), |
| 101 | | - created_at=created_at, |
| 102 | | - sections=tuple(sections), |
| 77 | + return _load( # type: ignore[return-value] |
| 78 | + store, |
| 79 | + subdir=_SUBDIR, |
| 80 | + plan_cls=PendingSynthPlan, |
| 81 | + error_cls=PendingSynthPlanError, |
| 82 | + label=_LABEL, |
| 103 | 83 | ) |
| 104 | 84 | |
| 105 | 85 | |
| 106 | 86 | def clear_pending_plan(store: StorePath) -> bool: |
| 107 | 87 | """Delete the staged synth plan for `store`. Returns True iff it existed.""" |
| 108 | | - path = pending_plan_path(store) |
| 109 | | - if not path.exists(): |
| 110 | | - return False |
| 111 | | - path.unlink() |
| 112 | | - return True |
| 113 | | - |
| 114 | | - |
| 115 | | -def _utcnow() -> str: |
| 116 | | - return datetime.now(UTC).replace(microsecond=0).isoformat().replace("+00:00", "Z") |
| 117 | | - |
| 118 | | - |
| 119 | | -def _section_to_payload(section: Section) -> dict[str, Any]: |
| 120 | | - return { |
| 121 | | - "type": section.type.value, |
| 122 | | - "content": section.content, |
| 123 | | - "start_line": section.start_line, |
| 124 | | - "adapter": section.adapter, |
| 125 | | - "tags": dict(section.tags), |
| 126 | | - "auto_harvest": section.auto_harvest, |
| 127 | | - "harvest_source": section.harvest_source, |
| 128 | | - "auto_mined": section.auto_mined, |
| 129 | | - "judge_name": section.judge_name, |
| 130 | | - "judge_score_chosen": section.judge_score_chosen, |
| 131 | | - "judge_score_rejected": section.judge_score_rejected, |
| 132 | | - "mined_at": section.mined_at, |
| 133 | | - "mined_run_id": section.mined_run_id, |
| 134 | | - "auto_synth": section.auto_synth, |
| 135 | | - "synth_teacher": section.synth_teacher, |
| 136 | | - "synth_strategy": section.synth_strategy, |
| 137 | | - "synth_at": section.synth_at, |
| 138 | | - "source_section_id": section.source_section_id, |
| 139 | | - "media_path": section.media_path, |
| 140 | | - "media_alt": section.media_alt, |
| 141 | | - "media_blob_sha": section.media_blob_sha, |
| 142 | | - "media_transcript": section.media_transcript, |
| 143 | | - } |
| 144 | | - |
| 145 | | - |
| 146 | | -def _section_from_payload(raw: object) -> Section: |
| 147 | | - if not isinstance(raw, dict): |
| 148 | | - raise TypeError(f"expected object, got {type(raw).__name__}") |
| 149 | | - section_type = SectionType(str(raw["type"])) |
| 150 | | - tags = raw.get("tags", {}) |
| 151 | | - if not isinstance(tags, dict): |
| 152 | | - raise TypeError("tags must be an object") |
| 153 | | - if not all(isinstance(k, str) and isinstance(v, str) for k, v in tags.items()): |
| 154 | | - raise TypeError("tags keys and values must be strings") |
| 155 | | - return Section( |
| 156 | | - type=section_type, |
| 157 | | - content=str(raw["content"]), |
| 158 | | - start_line=int(raw.get("start_line", 0)), |
| 159 | | - adapter=_optional_str(raw.get("adapter")), |
| 160 | | - tags=dict(tags), |
| 161 | | - auto_harvest=bool(raw.get("auto_harvest", False)), |
| 162 | | - harvest_source=_optional_str(raw.get("harvest_source")), |
| 163 | | - auto_mined=bool(raw.get("auto_mined", False)), |
| 164 | | - judge_name=_optional_str(raw.get("judge_name")), |
| 165 | | - judge_score_chosen=_optional_float(raw.get("judge_score_chosen")), |
| 166 | | - judge_score_rejected=_optional_float(raw.get("judge_score_rejected")), |
| 167 | | - mined_at=_optional_str(raw.get("mined_at")), |
| 168 | | - mined_run_id=_optional_int(raw.get("mined_run_id")), |
| 169 | | - auto_synth=bool(raw.get("auto_synth", False)), |
| 170 | | - synth_teacher=_optional_str(raw.get("synth_teacher")), |
| 171 | | - synth_strategy=_optional_str(raw.get("synth_strategy")), |
| 172 | | - synth_at=_optional_str(raw.get("synth_at")), |
| 173 | | - source_section_id=_optional_str(raw.get("source_section_id")), |
| 174 | | - media_path=_optional_str(raw.get("media_path")), |
| 175 | | - media_alt=_optional_str(raw.get("media_alt")), |
| 176 | | - media_blob_sha=_optional_str(raw.get("media_blob_sha")), |
| 177 | | - media_transcript=_optional_str(raw.get("media_transcript")), |
| 178 | | - ) |
| 179 | | - |
| 180 | | - |
| 181 | | -def _optional_str(value: object) -> str | None: |
| 182 | | - if value is None: |
| 183 | | - return None |
| 184 | | - if not isinstance(value, str): |
| 185 | | - raise TypeError(f"expected string or null, got {type(value).__name__}") |
| 186 | | - return value |
| 187 | | - |
| 188 | | - |
| 189 | | -def _optional_float(value: object) -> float | None: |
| 190 | | - if value is None: |
| 191 | | - return None |
| 192 | | - if isinstance(value, bool) or not isinstance(value, int | float): |
| 193 | | - raise TypeError(f"expected float or null, got {type(value).__name__}") |
| 194 | | - return float(value) |
| 195 | | - |
| 196 | | - |
| 197 | | -def _optional_int(value: object) -> int | None: |
| 198 | | - if value is None: |
| 199 | | - return None |
| 200 | | - if isinstance(value, bool) or not isinstance(value, int): |
| 201 | | - raise TypeError(f"expected int or null, got {type(value).__name__}") |
| 202 | | - return value |
| 88 | + return _clear(store, subdir=_SUBDIR) |
| 89 | + |
| 90 | + |
| 91 | +__all__ = [ |
| 92 | + "PendingSynthPlan", |
| 93 | + "PendingSynthPlanError", |
| 94 | + "_optional_float", |
| 95 | + "_optional_int", |
| 96 | + "_optional_str", |
| 97 | + "_section_from_payload", |
| 98 | + "_section_to_payload", |
| 99 | + "clear_pending_plan", |
| 100 | + "load_pending_plan", |
| 101 | + "pending_plan_path", |
| 102 | + "save_pending_plan", |
| 103 | +] |