@@ -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 | from __future__ import annotations | 6 | from __future__ import annotations |
| 4 | | 7 | |
| 5 | -import json | | |
| 6 | from dataclasses import dataclass | 8 | from dataclasses import dataclass |
| 7 | -from datetime import UTC, datetime | 9 | +from typing import TYPE_CHECKING |
| 8 | -from pathlib import Path | 10 | + |
| 9 | -from typing import TYPE_CHECKING, Any | 11 | +from dlm._pending import ( |
| 10 | - | 12 | + PendingSectionPlan, |
| 11 | -from dlm.doc.sections import Section, SectionType | 13 | + _optional_float, |
| 12 | -from dlm.io.atomic import write_text as atomic_write_text | 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 | from dlm.synth.errors import SynthError | 31 | from dlm.synth.errors import SynthError |
| 14 | | 32 | |
| 15 | if TYPE_CHECKING: | 33 | if TYPE_CHECKING: |
| 16 | from collections.abc import Sequence | 34 | from collections.abc import Sequence |
| | 35 | + from pathlib import Path |
| 17 | | 36 | |
| | 37 | + from dlm.doc.sections import Section |
| 18 | from dlm.store.paths import StorePath | 38 | from dlm.store.paths import StorePath |
| 19 | | 39 | |
| 20 | | 40 | |
| | 41 | +_SUBDIR = "synth" |
| | 42 | +_LABEL = "synth plan" |
| | 43 | + |
| | 44 | + |
| 21 | class PendingSynthPlanError(SynthError): | 45 | class PendingSynthPlanError(SynthError): |
| 22 | """Raised when the staged synth plan cannot be read or validated.""" | 46 | """Raised when the staged synth plan cannot be read or validated.""" |
| 23 | | 47 | |
| 24 | | 48 | |
| 25 | @dataclass(frozen=True) | 49 | @dataclass(frozen=True) |
| 26 | -class PendingSynthPlan: | 50 | +class PendingSynthPlan(PendingSectionPlan): |
| 27 | """One staged synth plan for a store.""" | 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 | def pending_plan_path(store: StorePath) -> Path: | 54 | def pending_plan_path(store: StorePath) -> Path: |
| 35 | """Path to the staged synth payload for `store`.""" | 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 | def save_pending_plan( | 59 | def save_pending_plan( |
@@ -43,160 +63,41 @@ def save_pending_plan( |
| 43 | sections: Sequence[Section], | 63 | sections: Sequence[Section], |
| 44 | ) -> PendingSynthPlan: | 64 | ) -> PendingSynthPlan: |
| 45 | """Persist `sections` as the staged synth plan for `store`.""" | 65 | """Persist `sections` as the staged synth plan for `store`.""" |
| 46 | - plan = PendingSynthPlan( | 66 | + return _save( # type: ignore[return-value] |
| 47 | - source_path=source_path.resolve(), | 67 | + store, |
| 48 | - created_at=_utcnow(), | 68 | + source_path=source_path, |
| 49 | - sections=tuple(sections), | 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 | def load_pending_plan(store: StorePath) -> PendingSynthPlan | None: | 75 | def load_pending_plan(store: StorePath) -> PendingSynthPlan | None: |
| 64 | """Return the staged synth plan for `store`, or None when absent.""" | 76 | """Return the staged synth plan for `store`, or None when absent.""" |
| 65 | - path = pending_plan_path(store) | 77 | + return _load( # type: ignore[return-value] |
| 66 | - if not path.exists(): | 78 | + store, |
| 67 | - return None | 79 | + subdir=_SUBDIR, |
| 68 | - try: | 80 | + plan_cls=PendingSynthPlan, |
| 69 | - raw = json.loads(path.read_text(encoding="utf-8")) | 81 | + error_cls=PendingSynthPlanError, |
| 70 | - except OSError as exc: | 82 | + label=_LABEL, |
| 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), | | |
| 103 | ) | 83 | ) |
| 104 | | 84 | |
| 105 | | 85 | |
| 106 | def clear_pending_plan(store: StorePath) -> bool: | 86 | def clear_pending_plan(store: StorePath) -> bool: |
| 107 | """Delete the staged synth plan for `store`. Returns True iff it existed.""" | 87 | """Delete the staged synth plan for `store`. Returns True iff it existed.""" |
| 108 | - path = pending_plan_path(store) | 88 | + return _clear(store, subdir=_SUBDIR) |
| 109 | - if not path.exists(): | 89 | + |
| 110 | - return False | 90 | + |
| 111 | - path.unlink() | 91 | +__all__ = [ |
| 112 | - return True | 92 | + "PendingSynthPlan", |
| 113 | - | 93 | + "PendingSynthPlanError", |
| 114 | - | 94 | + "_optional_float", |
| 115 | -def _utcnow() -> str: | 95 | + "_optional_int", |
| 116 | - return datetime.now(UTC).replace(microsecond=0).isoformat().replace("+00:00", "Z") | 96 | + "_optional_str", |
| 117 | - | 97 | + "_section_from_payload", |
| 118 | - | 98 | + "_section_to_payload", |
| 119 | -def _section_to_payload(section: Section) -> dict[str, Any]: | 99 | + "clear_pending_plan", |
| 120 | - return { | 100 | + "load_pending_plan", |
| 121 | - "type": section.type.value, | 101 | + "pending_plan_path", |
| 122 | - "content": section.content, | 102 | + "save_pending_plan", |
| 123 | - "start_line": section.start_line, | 103 | +] |
| 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 | | |