From f0c572c404be17c6a5686f9891c644af5a7522b6 Mon Sep 17 00:00:00 2001 From: B0LK13 Date: Mon, 18 May 2026 14:04:55 +0200 Subject: [PATCH] Add TUI batch preflight, manifest undo, and recommendations report Co-authored-by: Cursor --- CHANGELOG.md | 22 +++- README.md | 16 ++- docs/RECOMMENDATIONS-REPORT.md | 79 +++++++++++++ docs/SECURITY.md | 33 ++++++ mda_cli/__init__.py | 2 +- mda_cli/backup.py | 171 +++++++++++++++++++++++++-- mda_cli/cli.py | 103 +++++++++++++++-- mda_cli/core.py | 25 +++- mda_cli/preflight.py | 98 ++++++++++++++++ mda_cli/preview.py | 5 + mda_cli/tui.py | 204 ++++++++++++++++++++++++++++++--- pyproject.toml | 2 +- tests/test_backup.py | 166 +++++++++++++++++++++++++++ tests/test_preflight.py | 32 ++++++ tests/test_preview.py | 7 ++ tests/test_warn_tokens.py | 18 +++ 16 files changed, 941 insertions(+), 42 deletions(-) create mode 100644 docs/RECOMMENDATIONS-REPORT.md create mode 100644 docs/SECURITY.md create mode 100644 mda_cli/preflight.py create mode 100644 tests/test_preflight.py create mode 100644 tests/test_warn_tokens.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 570d2d8..1f819cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,24 @@ ## [Unreleased] -- Backup manifests: each batch run writes `~/.mda/manifests/.json` (override with `MDA_MANIFEST_DIR`); `mda restore --list` shows runs (apply not implemented yet). -- TUI: press **S** to cycle bundled skills (`markdown-document-architect`, `categorize-vault-notes`). -- Docs: `docs/LOCAL-DEV.md` for local branch workflow without GitHub CI. -- README: note that integration tests need `ANTHROPIC_API_KEY`. +## [0.3.1] + +- **TUI pre-flight:** Modal before batch **P** with file count, output mode, size/token warnings, Continue/Cancel. +- **TUI undo:** **U** restores files from the latest batch manifest `backup_path` entries (in-place runs with backups). +- **TUI manifests:** Each **P** batch writes `~/.mda/manifests/.json` for CLI/TUI restore. +- **Docs:** `docs/RECOMMENDATIONS-REPORT.md` with follow-up actions and issue status. + +## [0.3.0] + +- **Restore apply:** `mda restore ` dry-run; `mda restore --apply --yes` copies from manifest `backup_path` entries. +- **Batch checkpoint:** `--checkpoint` copies sources to `~/.mda/checkpoints//` before processing (`MDA_CHECKPOINT_DIR` override). +- **Vault scan:** `mda vault-scan ` alias for `mda script scan_vault --skill categorize-vault-notes`; TUI **Shift+V** runs scan on current folder with log output. +- **Cost warnings:** `warn_large_inputs` reports rough token budget when `--max-tokens` may be tight for the batch. +- **TUI preview:** extraction/read errors show a prominent `EXTRACTION ERROR` banner and red metadata line. +- **Security:** `docs/SECURITY.md` with API key rotation runbook (#24). +- Backup manifests: each batch run writes `~/.mda/manifests/.json`; `mda restore --list` lists runs. +- TUI: press **S** to cycle bundled skills. +- Docs: `docs/LOCAL-DEV.md`; README notes optional integration CI (`continue-on-error` on `main`). ## [0.2.10] diff --git a/README.md b/README.md index 698f3c1..0719207 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # mda-cli -**Roadmap:** [docs/ROADMAP.md](docs/ROADMAP.md) · **Gap analysis:** [docs/GAP-ANALYSIS.md](docs/GAP-ANALYSIS.md) · **Recommendations:** [docs/RECOMMENDATIONS.md](docs/RECOMMENDATIONS.md) · **Vault playbook:** [docs/VAULT-PLAYBOOK.md](docs/VAULT-PLAYBOOK.md) · **Troubleshooting:** [docs/TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md) ([tracked issues](https://github.com/B0LK13/mda-cli/issues)) +**Roadmap:** [docs/ROADMAP.md](docs/ROADMAP.md) · **Gap analysis:** [docs/GAP-ANALYSIS.md](docs/GAP-ANALYSIS.md) · **Recommendations:** [docs/RECOMMENDATIONS.md](docs/RECOMMENDATIONS.md) · **Status report:** [docs/RECOMMENDATIONS-REPORT.md](docs/RECOMMENDATIONS-REPORT.md) · **Vault playbook:** [docs/VAULT-PLAYBOOK.md](docs/VAULT-PLAYBOOK.md) · **Troubleshooting:** [docs/TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md) · **Security:** [docs/SECURITY.md](docs/SECURITY.md) ([tracked issues](https://github.com/B0LK13/mda-cli/issues)) CLI for **Markdown Document Architect (MDA)** and related bundled skills: loads `SKILL.md` and companion standards (bundled by default), calls Anthropic or OpenRouter, and writes restructured output (strips the outer fence when present). @@ -106,9 +106,16 @@ mda path\to\file.md mda path\to\vault -r --max-files 10 -q mda path\to\vault --dry-run mda path\to\file.md --in-place --backup +mda path\to\vault -r --checkpoint --max-files 10 +mda restore --list +mda restore 20260101T120000Z +mda restore 20260101T120000Z --apply --yes +mda vault-scan E:\ObsidianVault7 --recursive ``` -Optional environment variables: `MDA_SKILL` (skill id), `MDA_SKILL_DIR` (explicit skill folder), `MDA_EXTENSIONS`, `MDA_MODEL`, `MDA_MAX_TOKENS`, `MDA_API_TIMEOUT`. See [.env.example](.env.example) for a full template. +Optional environment variables: `MDA_SKILL` (skill id), `MDA_SKILL_DIR` (explicit skill folder), `MDA_EXTENSIONS`, `MDA_MODEL`, `MDA_MAX_TOKENS`, `MDA_API_TIMEOUT`, `MDA_MANIFEST_DIR`, `MDA_CHECKPOINT_DIR`. See [.env.example](.env.example) for a full template. + +**CI:** Unit tests and smoke install run on every push/PR. An optional integration job on `main` runs live API tests when `ANTHROPIC_API_KEY` is set; it uses `continue-on-error: true` so billing or missing secrets do not block releases. ### Cost guardrails @@ -128,7 +135,7 @@ mda E:\ObsidianVault7 -r --max-files 5 -q mda E:\ObsidianVault7 -r --max-files 20 --max-tokens 4096 ``` -TUI: select a small set of files manually before pressing **P**. See [docs/VAULT-PLAYBOOK.md](docs/VAULT-PLAYBOOK.md). +TUI: select a small set of files manually before pressing **P** (pre-flight confirmation with token/size warnings). After an in-place batch, press **U** to undo from the latest manifest backups. See [docs/VAULT-PLAYBOOK.md](docs/VAULT-PLAYBOOK.md). **Output naming:** sibling `*.restructured.md` for the default MDA skill (including PDF/DOCX sources). The `categorize-vault-notes` skill writes `*.restructured.txt` for non-Markdown sources. @@ -136,6 +143,8 @@ TUI: select a small set of files manually before pressing **P**. See [docs/VAULT - `--in-place` overwrites the source file. - `--backup` is available only with `--in-place` in batch mode and writes a sibling backup before replacing the file. +- `--checkpoint` copies each source file under `~/.mda/checkpoints//` before the batch starts (separate from per-file `.bak` backups). +- Each batch writes a manifest under `~/.mda/manifests/`; use `mda restore --list` and `mda restore --apply --yes` to roll back from `backup_path` entries. - Generated outputs are written atomically. If the process fails during write, the original destination file is left in place. - `--json-lines` now includes `action`, `changed`, `bytes_in`, `bytes_out`, and `backup_path` fields. @@ -158,6 +167,7 @@ Assign YAML frontmatter and optional folder hints for Obsidian vaults (flat or n mda --skill categorize-vault-notes E:\ObsidianVault7\note.md mda script --list --skill categorize-vault-notes mda script scan_vault --skill categorize-vault-notes -- E:\ObsidianVault7 --limit 20 +mda vault-scan E:\ObsidianVault7 --recursive --limit 20 mda script plan_moves --skill categorize-vault-notes -- E:\ObsidianVault7 mda script plan_moves --skill categorize-vault-notes -- E:\ObsidianVault7 --apply ``` diff --git a/docs/RECOMMENDATIONS-REPORT.md b/docs/RECOMMENDATIONS-REPORT.md new file mode 100644 index 0000000..f37f468 --- /dev/null +++ b/docs/RECOMMENDATIONS-REPORT.md @@ -0,0 +1,79 @@ +# mda-cli Recommendations Report + +**Date:** 2026-05-18 +**Version:** 0.3.1 (branch `chore/close-p0-issues`) +**Related:** [RECOMMENDATIONS.md](RECOMMENDATIONS.md) · [IMPLEMENTATION-PLAN.md](IMPLEMENTATION-PLAN.md) · [GitHub Issues](https://github.com/B0LK13/mda-cli/issues) + +--- + +## Executive summary + +Milestone A and most of Milestone B on `chore/close-p0-issues` are implemented locally: CI/release scaffolding, restore apply, batch checkpoints, vault-scan, token warnings, and TUI polish. **v0.3.1** adds **batch pre-flight confirmation** before **P**, **one-level TUI undo** (**U**) from the latest manifest `backup_path` entries, and this report. + +Remaining high-value work: merge/tag **0.3.x**, green or documented CI, full TUI skill picker (#27), vault workflow wizard (#32), and distribution docs (#17–#19). Integration tests (#4) stay optional on `main` until billing/credentials are stable. + +--- + +## Completed since last report (v0.3.0 → v0.3.1) + +| Area | Deliverable | +|------|-------------| +| **TUI** | Pre-flight modal before batch **P** — file count, output mode, size/token warnings, Continue/Cancel | +| **TUI** | **U** undo last batch via manifest `backup_path` (in-place runs with backups) | +| **TUI** | Batch writes `~/.mda/manifests/.json` after each **P** run | +| **CLI** | `mda restore --apply --yes` (v0.3.0) | +| **CLI** | `--checkpoint`, `mda vault-scan`, `warn_large_inputs` with `--max-tokens` | +| **Docs** | `docs/SECURITY.md`, `docs/LOCAL-DEV.md`, vault playbook & troubleshooting | +| **Quality** | Ruff + unit pytest in CI workflow (billing may block GitHub runs) | + +--- + +## Open GitHub issues still relevant + +| Issue | Priority | Topic | Notes | +|-------|----------|-------|-------| +| [#4](https://github.com/B0LK13/mda-cli/issues/4) | P1 | Integration CI | Optional job on `main`; needs API key + runner budget | +| [#14](https://github.com/B0LK13/mda-cli/issues/14) | P2 | Restore / rollback | **Largely done** — `restore --list` + `--apply`; TUI undo in 0.3.1 | +| [#17](https://github.com/B0LK13/mda-cli/issues/17)–[#21](https://github.com/B0LK13/mda-cli/issues/21) | P1–P2 | Distribution | Private wheel install, release checklist, pinning, PyPI/winget | +| [#22](https://github.com/B0LK13/mda-cli/issues/22)–[#23](https://github.com/B0LK13/mda-cli/issues/23) | P2 | CI quality | Coverage artifact, `ruff format --check` | +| [#25](https://github.com/B0LK13/mda-cli/issues/25)–[#26](https://github.com/B0LK13/mda-cli/issues/26) | P2–P1 | Security | Secret scanning; CI must not echo API keys | +| [#27](https://github.com/B0LK13/mda-cli/issues/27) | P1 | TUI skill picker | **S** cycles skills; full picker still open | +| [#28](https://github.com/B0LK13/mda-cli/issues/28)–[#29](https://github.com/B0LK13/mda-cli/issues/29) | P2 | TUI preview | Scroll/search; richer PDF preview | +| [#30](https://github.com/B0LK13/mda-cli/issues/30) | P2 | TUI undo | **Addressed in 0.3.1** for manifest-backed in-place batches | +| [#31](https://github.com/B0LK13/mda-cli/issues/31) | P1 | Pre-flight confirm | **Addressed in 0.3.1** | +| [#32](https://github.com/B0LK13/mda-cli/issues/32)–[#34](https://github.com/B0LK13/mda-cli/issues/34) | P1–P2 | Vault ops | Guided vault workflow; flatten wrapper; resume checkpoints | +| [#35](https://github.com/B0LK13/mda-cli/issues/35)–[#47](https://github.com/B0LK13/mda-cli/issues/47) | P2 | API cost, docs, providers | Token dashboards, docs site, multi-provider polish | + +--- + +## Follow-up actions + +| Action | Priority | Issue ref | Owner | +|--------|----------|-----------|-------| +| Merge `chore/close-p0-issues`, tag **v0.3.1**, publish release wheel | P0 | #8, #16 | Maintainer | +| Resolve or document CI billing; run smoke on Linux + Windows | P0 | #1, #2 | Maintainer | +| Close #30, #31 after verifying TUI preflight + undo in pilot | P1 | #30, #31 | Dev | +| Ship `docs/RELEASE-CHECKLIST.md` and private wheel install docs | P1 | #17, #18 | Dev | +| TUI skill picker (beyond **S** cycle) | P1 | #27 | Dev | +| Vault workflow wizard (flatten → scan → plan → MDA) | P1 | #32 | Dev | +| Enable integration job when `ANTHROPIC_API_KEY` available | P1 | #4 | DevOps | +| Coverage + format in CI | P2 | #22, #23 | Dev | +| Secret scanning in CI | P2 | #25 | DevOps | +| Evaluate PyPI vs private-only distribution | P2 | #20 | Product | + +--- + +## IMPLEMENTATION-PLAN progress (WP snapshot) + +| Work package | Status | +|--------------|--------| +| WP-A1 stabilization | ✅ Local | +| WP-B1 restore apply | ✅ | +| WP-B2 checkpoints | ✅ | +| WP-B3 vault-scan | ✅ | +| WP-B4 pre-flight (#31) | ✅ v0.3.1 | +| WP-B5 token warnings | ✅ | +| WP-B6 session undo (#30) | ✅ v0.3.1 (manifest / in-place) | +| WP-C distribution & docs | ⬜ | + +See [IMPLEMENTATION-PLAN.md](IMPLEMENTATION-PLAN.md) for full sequencing and evidence tables. diff --git a/docs/SECURITY.md b/docs/SECURITY.md new file mode 100644 index 0000000..27e30c9 --- /dev/null +++ b/docs/SECURITY.md @@ -0,0 +1,33 @@ +# Security + +mda-cli runs locally and sends document text to configured LLM providers (Anthropic, OpenRouter). This page covers secrets handling and key rotation — not WDAC or enterprise policy. + +## Secrets + +| Secret | Purpose | +|--------|---------| +| `ANTHROPIC_API_KEY` | Default provider API access | +| `OPENROUTER_API_KEY` | Fallback / alternate provider | +| `GITHUB_TOKEN` | Optional: install wheels from private GitHub Releases (see README) | + +- Copy [.env.example](../.env.example) to `.env` for local development; **never commit** `.env` or keys. +- CI integration tests use `ANTHROPIC_API_KEY` from repository secrets only on `main` (optional job; failures do not block the pipeline). + +## API key rotation + +When a key may be exposed or you rotate credentials on a schedule: + +1. **Create a new key** in the provider console (Anthropic or OpenRouter). +2. **Update local env**: edit `.env` or your shell profile; restart terminals and any running `mda` / TUI sessions. +3. **Update CI** (if used): GitHub → Settings → Secrets → update `ANTHROPIC_API_KEY`. +4. **Revoke the old key** in the provider console after confirming `mda --check` succeeds with the new key. +5. **Audit machines** that had the old key (shared PCs, backup scripts, password managers). + +## Reporting issues + +For security-sensitive bugs in this repository, contact the maintainer privately (do not open a public issue with keys or vault contents). + +## Related docs + +- [TROUBLESHOOTING.md](TROUBLESHOOTING.md) — install and runtime errors +- [README.md](../README.md) — cost guardrails and env vars diff --git a/mda_cli/__init__.py b/mda_cli/__init__.py index 0093504..ff23e64 100644 --- a/mda_cli/__init__.py +++ b/mda_cli/__init__.py @@ -1,3 +1,3 @@ """Markdown Document Architect CLI package.""" -__version__ = "0.2.10" +__version__ = "0.3.1" diff --git a/mda_cli/backup.py b/mda_cli/backup.py index fb5b017..0865f36 100644 --- a/mda_cli/backup.py +++ b/mda_cli/backup.py @@ -1,20 +1,17 @@ -"""Backup manifest tracking for batch runs (issue #14 groundwork). - -Each batch run with ``--backup`` and/or in-place writes can append a JSON manifest -under ``~/.mda/manifests/`` (override with ``MDA_MANIFEST_DIR``). Full -``mda restore`` apply is not implemented yet; use ``mda restore --list`` to inspect. -""" +"""Backup manifests, restore apply, and optional batch checkpoints (issue #14, #34).""" from __future__ import annotations import json import os +import shutil +from dataclasses import dataclass from datetime import UTC, datetime from pathlib import Path from typing import TYPE_CHECKING if TYPE_CHECKING: - from mda_cli.core import JobResult + from mda_cli.core import Job, JobResult MANIFEST_VERSION = 1 @@ -105,3 +102,163 @@ def summarize_manifest(data: dict[str, object]) -> str: if isinstance(e, dict) and e.get("backup_path") ) return f"{run_id} {created} skill={skill} jobs={jobs} backups={backups}" + + +def default_checkpoint_dir() -> Path: + """Directory where optional pre-batch source copies are stored.""" + raw = os.environ.get("MDA_CHECKPOINT_DIR", "").strip() + if raw: + return Path(raw).expanduser() + return Path.home() / ".mda" / "checkpoints" + + +def find_manifest_path(run_id: str, manifest_dir: Path | None = None) -> Path | None: + """Resolve a manifest file by run id (with or without ``.json`` suffix).""" + rid = run_id.removesuffix(".json") + root = (manifest_dir or default_manifest_dir()).expanduser() + direct = root / f"{rid}.json" + if direct.is_file(): + return direct + for path in list_manifest_files(root): + try: + data = load_manifest(path) + except (OSError, ValueError): + continue + if data.get("run_id") == rid: + return path + return None + + +@dataclass(frozen=True) +class RestoreOperation: + """One file to restore from a manifest backup_path.""" + + backup_path: Path + target_path: Path + src: Path | None + ok: bool + skip_reason: str | None = None + + +def plan_restore_operations(data: dict[str, object]) -> list[RestoreOperation]: + """Build restore operations from manifest entries (only rows with backup_path).""" + ops: list[RestoreOperation] = [] + entries = data.get("entries", []) + if not isinstance(entries, list): + return ops + for entry in entries: + if not isinstance(entry, dict): + continue + raw_backup = entry.get("backup_path") + if not raw_backup: + continue + backup_path = Path(str(raw_backup)) + dst_raw = entry.get("dst") or entry.get("src") + if not dst_raw: + ops.append( + RestoreOperation( + backup_path=backup_path, + target_path=backup_path, + src=None, + ok=bool(entry.get("ok")), + skip_reason="missing dst/src in manifest entry", + ) + ) + continue + target = Path(str(dst_raw)) + src = Path(str(entry["src"])) if entry.get("src") else None + ok = bool(entry.get("ok")) + skip: str | None = None + if not ok: + skip = "job did not succeed" + elif not backup_path.is_file(): + skip = f"backup missing: {backup_path}" + ops.append( + RestoreOperation( + backup_path=backup_path, + target_path=target, + src=src, + ok=ok, + skip_reason=skip, + ) + ) + return ops + + +def restore_from_manifest( + manifest_path: Path, + *, + dry_run: bool = False, +) -> tuple[int, int, list[str], str | None]: + """Restore all ``backup_path`` entries in one manifest. + + Returns ``(applied, skipped, messages, error)`` where ``error`` is set when + the manifest cannot be used (missing file, no backup entries). + """ + if not manifest_path.is_file(): + return 0, 0, [], f"manifest not found: {manifest_path}" + try: + data = load_manifest(manifest_path) + except (OSError, ValueError) as e: + return 0, 0, [], f"invalid manifest: {e}" + ops = plan_restore_operations(data) + if not ops: + return 0, 0, [], "no backup_path entries in manifest" + applied, skipped, messages = apply_restore_operations(ops, dry_run=dry_run) + return applied, skipped, messages, None + + +def apply_restore_operations( + ops: list[RestoreOperation], + *, + dry_run: bool = True, +) -> tuple[int, int, list[str]]: + """Copy backup_path → target_path. Returns (applied, skipped, messages).""" + applied = 0 + skipped = 0 + messages: list[str] = [] + for op in ops: + if op.skip_reason: + skipped += 1 + messages.append(f"SKIP {op.target_path}: {op.skip_reason}") + continue + if dry_run: + messages.append(f"WOULD RESTORE {op.backup_path} -> {op.target_path}") + applied += 1 + continue + try: + op.target_path.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(op.backup_path, op.target_path) + except OSError as e: + skipped += 1 + messages.append(f"FAILED {op.target_path}: {e}") + continue + applied += 1 + messages.append(f"RESTORED {op.target_path} <- {op.backup_path}") + return applied, skipped, messages + + +def write_batch_checkpoint( + *, + run_id: str, + jobs: list[Job], + base: Path | None = None, + checkpoint_dir: Path | None = None, +) -> Path: + """Copy source files to ``~/.mda/checkpoints//`` before a batch run.""" + root = (checkpoint_dir or default_checkpoint_dir()).expanduser() / run_id + root.mkdir(parents=True, exist_ok=True) + base_resolved = base.resolve() if base is not None else None + for job in jobs: + src = job.src.resolve() + if base_resolved is not None: + try: + rel = src.relative_to(base_resolved) + except ValueError: + rel = Path(src.name) + else: + rel = Path(src.name) + dest = root / rel + dest.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(src, dest) + return root diff --git a/mda_cli/cli.py b/mda_cli/cli.py index a83d3d6..0ca5d38 100644 --- a/mda_cli/cli.py +++ b/mda_cli/cli.py @@ -9,10 +9,14 @@ from mda_cli import __version__ from mda_cli.backup import ( + apply_restore_operations, + find_manifest_path, list_manifest_files, load_manifest, new_run_id, + plan_restore_operations, summarize_manifest, + write_batch_checkpoint, write_batch_manifest, ) from mda_cli.core import ( @@ -55,13 +59,62 @@ def run_restore_list() -> int: except (OSError, ValueError) as e: print(f"{path.name}\tERROR: {e}", file=sys.stderr) print( - "\nRestore apply is not implemented yet; copy files from backup_path entries " - "or use git on tracked vaults.", + "\nRestore: mda restore [--apply] [--yes] (dry-run without --apply)", file=sys.stderr, ) return 0 +def run_restore_apply( + run_id: str, + *, + apply: bool, + yes: bool, +) -> int: + """Restore files from manifest backup_path entries for one run.""" + path = find_manifest_path(run_id) + if path is None: + print(f"ERROR: no manifest for run id {run_id!r}", file=sys.stderr) + return 2 + try: + data = load_manifest(path) + except (OSError, ValueError) as e: + print(f"ERROR: {path}: {e}", file=sys.stderr) + return 2 + ops = plan_restore_operations(data) + if not ops: + print(f"No backup_path entries in manifest {path.name}.") + return 0 + dry_run = not apply + if apply and not yes: + restorable = sum(1 for o in ops if not o.skip_reason) + print( + f"About to restore {restorable} file(s) from run {data.get('run_id', run_id)}.", + file=sys.stderr, + ) + print("Re-run with --yes to confirm.", file=sys.stderr) + dry_run = True + applied, skipped, messages = apply_restore_operations(ops, dry_run=dry_run) + for line in messages: + print(line) + label = "would restore" if dry_run else "restored" + print(f"\n{label}: {applied}; skipped: {skipped}", file=sys.stderr) + if apply and not yes: + return 2 + return 0 if skipped == 0 or applied > 0 else 1 + + +def run_vault_scan(script_args: list[str]) -> int: + """Alias for ``mda script scan_vault`` with categorize-vault-notes skill.""" + return run_skill_script( + "scan_vault", + script_args, + skill_dir_arg=None, + skill_id="categorize-vault-notes", + list_only=False, + ) + + def run_check( skill_dir_arg: Path | None, *, @@ -284,6 +337,14 @@ def build_parser() -> argparse.ArgumentParser: action="store_true", help="When used with --in-place in batch mode, keep a sibling .bak copy before overwrite.", ) + p.add_argument( + "--checkpoint", + action="store_true", + help=( + "Before batch processing, copy each source file to " + "~/.mda/checkpoints// (override with MDA_CHECKPOINT_DIR)." + ), + ) p.add_argument( "-q", "--quiet", @@ -340,12 +401,25 @@ def main(argv: list[str] | None = None) -> int: rest = raw_argv[1:] if rest == ["--list"] or rest == ["-l"]: return run_restore_list() - print( - "Usage: mda restore --list\n" - " (restore apply not implemented yet; see manifest backup_path entries)", - file=sys.stderr, - ) - return 2 + apply = "--apply" in rest + yes = "--yes" in rest + positional = [t for t in rest if t not in ("--apply", "--yes")] + if len(positional) != 1: + print( + "Usage: mda restore --list\n" + " mda restore # dry-run restore plan\n" + " mda restore --apply --yes # copy from backup_path", + file=sys.stderr, + ) + return 2 + return run_restore_apply(positional[0], apply=apply, yes=yes) + + if raw_argv and raw_argv[0] in ("vault-scan", "vault_scan"): + args = _parse_script_argv(raw_argv[1:]) + script_args = list(args.script_args) + if script_args and script_args[0] == "--": + script_args = script_args[1:] + return run_vault_scan(script_args) if raw_argv and raw_argv[0] == "script": args = _parse_script_argv(raw_argv[1:]) @@ -440,7 +514,17 @@ def main(argv: list[str] | None = None) -> int: provider = resolve_provider_name(cli_provider=args.provider) ensure_api_credentials(provider) - warn_large_inputs(jobs) + warn_large_inputs(jobs, max_tokens=args.max_tokens) + + run_id = new_run_id() + if args.checkpoint: + checkpoint_root = write_batch_checkpoint( + run_id=run_id, + jobs=jobs, + base=args.target if args.target.is_dir() else args.target.parent, + ) + if not args.json_lines: + print(f"Checkpoint: {checkpoint_root}", file=sys.stderr) system = load_system_prompt(skill_dir) client, ctx = build_restructure_client( @@ -481,7 +565,6 @@ def main(argv: list[str] | None = None) -> int: job_result_record(failed, provider=ctx.provider), ) - run_id = new_run_id() manifest_path = write_batch_manifest( run_id=run_id, results=results, diff --git a/mda_cli/core.py b/mda_cli/core.py index ddafa88..9f12089 100644 --- a/mda_cli/core.py +++ b/mda_cli/core.py @@ -171,7 +171,12 @@ class JobResult: bytes_out: int = 0 -def warn_large_inputs(jobs: list[Job], *, writer: Callable[[str], None] | None = None) -> None: +def warn_large_inputs( + jobs: list[Job], + *, + max_tokens: int | None = None, + writer: Callable[[str], None] | None = None, +) -> None: """Warn when inputs may stress context limits (heuristic by file size).""" emit = writer or (lambda m: sys.stderr.write(m + "\n")) total = 0 @@ -191,6 +196,24 @@ def warn_large_inputs(jobs: list[Job], *, writer: Callable[[str], None] | None = f"WARNING: batch input is large (~{total // 1024} KiB total); " "watch for context or rate limits." ) + if max_tokens is not None and max_tokens > 0 and jobs: + # Rough input token estimate (~4 chars/token); output capped per file by max_tokens. + est_input_tokens = max(1, total // 4) + est_output_tokens = max_tokens * len(jobs) + est_total = est_input_tokens + est_output_tokens + per_file_budget = max_tokens + if est_input_tokens > per_file_budget * len(jobs): + emit( + f"WARNING: estimated input tokens (~{est_input_tokens:,}) may exceed " + f"--max-tokens ({per_file_budget:,}) per file for {len(jobs)} job(s); " + "reduce batch size or raise --max-tokens." + ) + if est_total > 200_000: + emit( + f"WARNING: rough token budget ~{est_total:,} " + f"(input ~{est_input_tokens:,} + output up to {est_output_tokens:,}); " + "use --max-files, --max-tokens, or a dry-run first." + ) def bundled_skills_root() -> Path: diff --git a/mda_cli/preflight.py b/mda_cli/preflight.py new file mode 100644 index 0000000..f137a86 --- /dev/null +++ b/mda_cli/preflight.py @@ -0,0 +1,98 @@ +"""Batch pre-flight summaries for TUI confirmation (issue #31 / WP-B5).""" + +from __future__ import annotations + +from dataclasses import dataclass + +from mda_cli.core import LARGE_BATCH_TOTAL_BYTES, LARGE_FILE_BYTES, Job + + +@dataclass(frozen=True) +class BatchPreflightSummary: + """Heuristic summary shown before a TUI batch run.""" + + file_count: int + output_mode: str + total_bytes: int + warnings: tuple[str, ...] + est_input_tokens: int + est_output_tokens: int + est_total_tokens: int + + +def build_batch_preflight( + jobs: list[Job], + *, + max_tokens: int, + output_mode: str, +) -> BatchPreflightSummary: + """Compute file count, size totals, and token budget warnings for a batch.""" + total = 0 + warnings: list[str] = [] + for job in jobs: + try: + st = job.src.stat() + except OSError: + continue + total += st.st_size + if st.st_size >= LARGE_FILE_BYTES: + warnings.append( + f"Large source {job.src.name} ({st.st_size // 1024} KiB); " + "consider splitting before MDA." + ) + if total >= LARGE_BATCH_TOTAL_BYTES: + warnings.append( + f"Batch input is large (~{total // 1024} KiB total); " + "watch for context or rate limits." + ) + + est_input_tokens = max(1, total // 4) if jobs else 0 + per_file_budget = max(1, max_tokens) + est_output_tokens = per_file_budget * len(jobs) if jobs else 0 + est_total = est_input_tokens + est_output_tokens + + if jobs and max_tokens > 0 and est_input_tokens > per_file_budget * len(jobs): + warnings.append( + f"Estimated input tokens (~{est_input_tokens:,}) may exceed " + f"--max-tokens ({per_file_budget:,}) per file for {len(jobs)} job(s)." + ) + if est_total > 200_000: + warnings.append( + f"Rough token budget ~{est_total:,} " + f"(input ~{est_input_tokens:,} + output up to {est_output_tokens:,}); " + "API cost may be significant." + ) + + return BatchPreflightSummary( + file_count=len(jobs), + output_mode=output_mode, + total_bytes=total, + warnings=tuple(warnings), + est_input_tokens=est_input_tokens, + est_output_tokens=est_output_tokens, + est_total_tokens=est_total, + ) + + +def format_preflight_message(summary: BatchPreflightSummary) -> str: + """Multi-line body for the pre-flight modal.""" + lines = [ + f"Files selected: {summary.file_count}", + f"Output mode: {summary.output_mode}", + f"Input size: ~{summary.total_bytes // 1024} KiB total", + ( + f"Token estimate (rough): input ~{summary.est_input_tokens:,}, " + f"output up to {summary.est_output_tokens:,}, " + f"total ~{summary.est_total_tokens:,}" + ), + ] + if summary.warnings: + lines.append("") + lines.append("Warnings:") + lines.extend(f" • {w}" for w in summary.warnings) + else: + lines.append("") + lines.append("No size or token warnings for this batch.") + lines.append("") + lines.append("Continue with MDA batch? (y / Enter = continue, n / Esc = cancel)") + return "\n".join(lines) diff --git a/mda_cli/preview.py b/mda_cli/preview.py index c77650d..66bedfd 100644 --- a/mda_cli/preview.py +++ b/mda_cli/preview.py @@ -46,6 +46,11 @@ def format_preview_metadata( return base +def format_preview_error_banner(error: str) -> str: + """Prominent extraction/read error text for the preview body.""" + return f"EXTRACTION ERROR\n{'=' * 16}\n{error}" + + def format_preview_display( *, metadata: str, diff --git a/mda_cli/tui.py b/mda_cli/tui.py index 562b806..2eeeea9 100644 --- a/mda_cli/tui.py +++ b/mda_cli/tui.py @@ -11,10 +11,16 @@ from textual.binding import Binding from textual.containers import Horizontal, Vertical from textual.screen import ModalScreen -from textual.widgets import DataTable, Footer, Header, Input, Label, RichLog, TextArea +from textual.widgets import Button, DataTable, Footer, Header, Input, Label, RichLog, TextArea +from mda_cli.backup import ( + new_run_id, + restore_from_manifest, + write_batch_manifest, +) from mda_cli.core import ( Job, + JobResult, find_skill_dir, jobs_from_document_files, list_bundled_skills, @@ -26,8 +32,10 @@ read_document_text, supported_extensions, ) +from mda_cli.preflight import build_batch_preflight, format_preflight_message from mda_cli.preview import ( DEFAULT_PREVIEW_MAX_CHARS, + format_preview_error_banner, format_preview_metadata, truncate_preview_text, ) @@ -84,6 +92,42 @@ def on_input_submitted(self, event: Input.Submitted) -> None: self.dismiss(p.resolve()) +class BatchPreflightScreen(ModalScreen[bool]): + """Confirm batch run after showing file count and token/size warnings.""" + + BINDINGS = [ + Binding("escape", "cancel", "Cancel", show=False), + Binding("n", "cancel", "Cancel", show=False), + Binding("y", "continue_run", "Continue", show=False), + ] + + def __init__(self, message: str) -> None: + super().__init__() + self.message = message + + def compose(self) -> ComposeResult: + yield Label("Batch pre-flight", id="preflight-title") + yield Label(self.message, id="preflight-body") + with Horizontal(id="preflight-actions"): + yield Button("Continue", variant="primary", id="preflight-continue") + yield Button("Cancel", id="preflight-cancel") + + def on_mount(self) -> None: + self.query_one("#preflight-continue", Button).focus() + + def action_cancel(self) -> None: + self.dismiss(False) + + def action_continue_run(self) -> None: + self.dismiss(True) + + def on_button_pressed(self, event: Button.Pressed) -> None: + if event.button.id == "preflight-continue": + self.dismiss(True) + elif event.button.id == "preflight-cancel": + self.dismiss(False) + + class FilterScreen(ModalScreen[str | None]): """Filter listing by substring (case-insensitive); empty clears filter.""" @@ -122,6 +166,7 @@ class MdaNavigatorApp(App[None]): #right { width: 1fr; height: 100%; border: heavy $primary; padding: 0 1; } #listing { height: 1fr; min-height: 10; } #preview-meta { height: auto; max-height: 3; text-style: bold; } + #preview-meta.error { color: $error; } #preview-body { height: 1fr; min-height: 8; } #log { height: 1fr; min-height: 10; background: $surface; } #api-status { height: auto; max-height: 3; color: $error; } @@ -134,6 +179,7 @@ class MdaNavigatorApp(App[None]): Binding("o", "cycle_output", "Output mode", show=True), Binding("i", "toggle_preview", "Preview", show=True), Binding("p", "run_mda", "Run MDA", show=True), + Binding("u", "undo_last", "Undo batch", show=True), Binding("g", "focus_out_dir", "Out folder", show=True), Binding("h", "go_home", "Home", show=True), Binding("ctrl+j", "jump_path", "Jump", show=True), @@ -141,6 +187,7 @@ class MdaNavigatorApp(App[None]): Binding("v", "toggle_hidden", "Hidden", show=True), Binding("a", "select_recursive_supported", "Recursive all", show=True), Binding("s", "cycle_skill", "Skill", show=True), + Binding("shift+v", "scan_vault", "Vault scan", show=True), ] def __init__( @@ -179,6 +226,8 @@ def __init__( self._preview_timer = None self._preview_load_id: int = 0 self._last_api_error: str | None = None + self._last_manifest_path: Path | None = None + self._recursive_last_select: bool = False bundled = list_bundled_skills() self._skill_cycle_ids: list[str] = bundled if bundled else [] if self.skill_id and self.skill_id not in self._skill_cycle_ids: @@ -226,9 +275,9 @@ def on_mount(self) -> None: f"[dim]Supported:[/] {exts}\n" "[dim]Space[/] toggle file [dim]Enter[/] open dir [dim]Backspace[/] up " "[dim]ctrl+j[/] jump [dim]f[/] filter [dim]v[/] hidden " - "[dim]a[/] recursive select [dim]S[/] skill [dim]k/j[/] up/down " - "[dim]I[/] preview pane [dim]O[/] output [dim]P[/] run " - "[dim]G[/] out-folder [dim]H[/] home" + "[dim]a[/] recursive select [dim]S[/] skill [dim]Shift+V[/] vault scan " + "[dim]k/j[/] up/down [dim]I[/] preview [dim]O[/] output [dim]P[/] run " + "[dim]U[/] undo last batch [dim]G[/] out-folder [dim]H[/] home" ) self._apply_preview_pane_visibility() @@ -485,17 +534,23 @@ def _render_preview( *, truncated: bool, ) -> None: + meta_label = self.query_one("#preview-meta", Label) meta = format_preview_metadata(path, error=error) - self.query_one("#preview-meta", Label).update(meta) if error: - text = error if not body else f"{error}\n\n{body}" + meta_label.add_class("error") + meta_label.update(f"[red]{meta}[/]") + else: + meta_label.remove_class("error") + meta_label.update(meta) + if error: + text = format_preview_error_banner(error) + if body: + text = f"{text}\n\n--- partial content ---\n{body}" elif body == "": text = "(empty)" else: text = body - if truncated and not error: - text = f"{text}\n\n… truncated" - elif truncated: + if truncated: text = f"{text}\n\n… truncated" self.query_one("#preview-body", TextArea).load_text(text) @@ -574,6 +629,7 @@ def action_select_recursive_supported(self) -> None: found = found[:cap] else: self.log_msg(f"[cyan]Recursive select:[/] {len(found)} file(s) under {self.cwd}") + self._recursive_last_select = True self.selected.update(found) self.refresh_listing() @@ -620,10 +676,56 @@ def action_cycle_skill(self) -> None: self.query_one("#skill-label", Label).update(self._skill_label()) self.log_msg(f"[cyan]Skill set to[/] {self.skill_id}") + @work + async def action_scan_vault(self) -> None: + """Run categorize-vault-notes ``scan_vault`` on the current folder.""" + import io + import sys + + from mda_cli.cli import run_skill_script + + vault = self.cwd + self.log_msg(f"[cyan]Vault scan[/] {vault} (recursive)…") + + def run_scan() -> int: + buf = io.StringIO() + old_out = sys.stdout + sys.stdout = buf + try: + code = run_skill_script( + "scan_vault", + [str(vault), "--recursive"], + skill_dir_arg=None, + skill_id="categorize-vault-notes", + list_only=False, + ) + finally: + sys.stdout = old_out + return code, buf.getvalue() + + code, output = await asyncio.to_thread(run_scan) + lines = [ln for ln in output.strip().splitlines() if ln.strip()] + preview = lines[:40] + for line in preview: + self.log_msg(f"[dim]{line}[/]") + if len(lines) > len(preview): + self.log_msg(f"[dim]… {len(lines) - len(preview)} more line(s)[/]") + if code != 0: + self.log_msg("[red]Vault scan failed.[/]") + else: + self.log_msg("[green]Vault scan complete.[/]") + def action_focus_out_dir(self) -> None: self.query_one("#out-dir", Input).focus() - def _run_batch_sync(self, skill_dir: Path, jobs: list[Job]) -> None: + def _run_batch_sync( + self, + skill_dir: Path, + jobs: list[Job], + *, + backup_in_place: bool, + skill_id: str, + ) -> tuple[list[JobResult], Path]: def thread_log(msg: str) -> None: self.call_from_thread(self.log_msg, msg) @@ -633,7 +735,7 @@ def thread_log(msg: str) -> None: model=self.model, max_tokens=self.max_tokens, ) - ok = 0 + results: list[JobResult] = [] total = len(jobs) self.call_from_thread(self._set_api_error, None) for index, job in enumerate(jobs, start=1): @@ -649,10 +751,10 @@ def thread_log(msg: str) -> None: progress=False, log=thread_log, max_attempts=self.max_attempts, + backup_in_place=backup_in_place, ) - if result.ok: - ok += 1 - else: + results.append(result) + if not result.ok: err = result.error or "processing failed" self.call_from_thread(self._set_api_error, err) thread_log(f"[red]FAILED[/] {job.src}: {err}") @@ -660,12 +762,61 @@ def thread_log(msg: str) -> None: err = str(e) self.call_from_thread(self._set_api_error, err) thread_log(f"[red]FAILED[/] {job.src}: {e}") + results.append( + JobResult(job=job, ok=False, action="failed", error=err), + ) self.call_from_thread( self.query_one("#selection-label", Label).update, self._selection_label(), ) + ok = sum(1 for r in results if r.ok) thread_log(f"[bold]Done.[/] {ok}/{total} succeeded.") + run_id = new_run_id() + manifest_path = write_batch_manifest( + run_id=run_id, + results=results, + skill_id=skill_id, + provider=ctx.provider, + target=self.cwd, + in_place=backup_in_place, + backup_enabled=backup_in_place, + ) + thread_log(f"[dim]Manifest:[/] {manifest_path}") + return results, manifest_path + + def _output_mode_label(self) -> str: + if self.output_mode == "sibling": + return "sibling (*.restructured.md)" + if self.output_mode == "in_place": + return "in-place (overwrite source)" + return "out_dir" + + @work + async def action_undo_last(self) -> None: + manifest_path = self._last_manifest_path + if manifest_path is None or not manifest_path.is_file(): + self.log_msg( + "[yellow]Nothing to undo.[/] Run a batch with **P** first " + "(in-place mode keeps backups for undo)." + ) + return + def do_restore() -> tuple[int, int, list[str], str | None]: + return restore_from_manifest(manifest_path, dry_run=False) + + applied, skipped, messages, error = await asyncio.to_thread(do_restore) + if error: + self.log_msg(f"[red]Undo failed:[/] {error}") + return + for line in messages[:20]: + self.log_msg(f"[dim]{line}[/]") + if len(messages) > 20: + self.log_msg(f"[dim]… {len(messages) - 20} more line(s)[/]") + self.log_msg( + f"[green]Undo complete.[/] Restored {applied} file(s); skipped {skipped}." + ) + + @work async def action_run_mda(self) -> None: if not self.selected: self.log_msg( @@ -714,11 +865,34 @@ async def action_run_mda(self) -> None: self.log_msg(f"[red]{e}[/]") return + preflight = build_batch_preflight( + jobs, + max_tokens=self.max_tokens, + output_mode=self._output_mode_label(), + ) + preflight_body = format_preflight_message(preflight) + if self._recursive_last_select: + preflight_body = ( + f"Recursive selection under: {self.cwd}\n\n{preflight_body}" + ) + confirmed = await self.push_screen_wait(BatchPreflightScreen(preflight_body)) + if not confirmed: + self.log_msg("[yellow]Batch cancelled.[/]") + return + self.log_msg(f"[cyan]Skill:[/] {skill_res.skill_id} ({skill_dir})") for j in jobs: self.log_msg(f"[dim]{j.src} → {j.dst}[/]") - await asyncio.to_thread(self._run_batch_sync, skill_dir, jobs) + backup_in_place = in_place + _results, manifest_path = await asyncio.to_thread( + self._run_batch_sync, + skill_dir, + jobs, + backup_in_place=backup_in_place, + skill_id=skill_res.skill_id, + ) + self._last_manifest_path = manifest_path def run_tui( diff --git a/pyproject.toml b/pyproject.toml index bb4cfe7..22a0928 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "mda-cli" -version = "0.2.10" +version = "0.3.1" description = "CLI wrapper for Markdown Document Architect — restructure Markdown via Anthropic using local SKILL.md + MDA-STANDARD.md" readme = "README.md" requires-python = ">=3.10" diff --git a/tests/test_backup.py b/tests/test_backup.py index bf1027e..805392f 100644 --- a/tests/test_backup.py +++ b/tests/test_backup.py @@ -3,9 +3,14 @@ from pathlib import Path from mda_cli.backup import ( + apply_restore_operations, + find_manifest_path, list_manifest_files, load_manifest, + plan_restore_operations, + restore_from_manifest, summarize_manifest, + write_batch_checkpoint, write_batch_manifest, ) from mda_cli.cli import main @@ -67,3 +72,164 @@ def test_restore_list_shows_manifest(tmp_path: Path, monkeypatch, capsys) -> Non assert main(["restore", "--list"]) == 0 out = capsys.readouterr().out assert "20260102T000000Z" in out + + +def test_restore_apply_dry_run(tmp_path: Path, monkeypatch, capsys) -> None: + monkeypatch.setenv("MDA_MANIFEST_DIR", str(tmp_path)) + src = tmp_path / "note.md" + src.write_text("original", encoding="utf-8") + bak = tmp_path / "note.md.bak" + bak.write_text("backup", encoding="utf-8") + src.write_text("changed", encoding="utf-8") + job = Job(src=src, dst=src) + result = JobResult( + job=job, + ok=True, + action="wrote", + changed=True, + backup_path=bak, + ) + write_batch_manifest( + run_id="run_restore_test", + results=[result], + skill_id="markdown-document-architect", + provider="anthropic", + target=tmp_path, + in_place=True, + backup_enabled=True, + manifest_dir=tmp_path, + ) + assert main(["restore", "run_restore_test"]) == 0 + out = capsys.readouterr().out + assert "WOULD RESTORE" in out + assert src.read_text(encoding="utf-8") == "changed" + + +def test_restore_apply_copies_backup(tmp_path: Path, monkeypatch, capsys) -> None: + monkeypatch.setenv("MDA_MANIFEST_DIR", str(tmp_path)) + src = tmp_path / "note.md" + src.write_text("original", encoding="utf-8") + bak = tmp_path / "note.md.bak" + bak.write_text("backup", encoding="utf-8") + src.write_text("changed", encoding="utf-8") + job = Job(src=src, dst=src) + result = JobResult( + job=job, + ok=True, + action="wrote", + changed=True, + backup_path=bak, + ) + write_batch_manifest( + run_id="run_apply_test", + results=[result], + skill_id="markdown-document-architect", + provider="anthropic", + target=tmp_path, + in_place=True, + backup_enabled=True, + manifest_dir=tmp_path, + ) + assert main(["restore", "run_apply_test", "--apply", "--yes"]) == 0 + assert src.read_text(encoding="utf-8") == "backup" + assert find_manifest_path("run_apply_test", tmp_path) is not None + + +def test_write_batch_checkpoint(tmp_path: Path, monkeypatch) -> None: + monkeypatch.setenv("MDA_CHECKPOINT_DIR", str(tmp_path / "ck")) + vault = tmp_path / "vault" + vault.mkdir() + a = vault / "a.md" + a.write_text("# A", encoding="utf-8") + sub = vault / "sub" + sub.mkdir() + b = sub / "b.md" + b.write_text("# B", encoding="utf-8") + jobs = [Job(src=a, dst=a), Job(src=b, dst=b)] + root = write_batch_checkpoint(run_id="ck1", jobs=jobs, base=vault) + assert (root / "a.md").read_text(encoding="utf-8") == "# A" + assert (root / "sub" / "b.md").read_text(encoding="utf-8") == "# B" + + +def test_plan_restore_skips_missing_backup(tmp_path: Path) -> None: + data = { + "entries": [ + { + "src": "a.md", + "dst": "a.md", + "ok": True, + "backup_path": str(tmp_path / "missing.bak"), + } + ] + } + ops = plan_restore_operations(data) + assert len(ops) == 1 + assert ops[0].skip_reason is not None + + +def test_restore_from_manifest_applies(tmp_path: Path) -> None: + src = tmp_path / "note.md" + src.write_text("changed", encoding="utf-8") + bak = tmp_path / "note.md.bak" + bak.write_text("original", encoding="utf-8") + job = Job(src=src, dst=src) + result = JobResult( + job=job, + ok=True, + action="wrote", + changed=True, + backup_path=bak, + ) + manifest = write_batch_manifest( + run_id="undo_manifest", + results=[result], + skill_id="markdown-document-architect", + provider="anthropic", + target=tmp_path, + in_place=True, + backup_enabled=True, + manifest_dir=tmp_path, + ) + applied, skipped, msgs, err = restore_from_manifest(manifest, dry_run=False) + assert err is None + assert applied == 1 + assert skipped == 0 + assert src.read_text(encoding="utf-8") == "original" + assert any("RESTORED" in m for m in msgs) + + +def test_restore_from_manifest_no_backups(tmp_path: Path) -> None: + manifest = write_batch_manifest( + run_id="empty_backups", + results=[], + skill_id="markdown-document-architect", + provider=None, + target=None, + in_place=False, + backup_enabled=False, + manifest_dir=tmp_path, + ) + applied, skipped, msgs, err = restore_from_manifest(manifest, dry_run=False) + assert applied == 0 + assert err == "no backup_path entries in manifest" + + +def test_apply_restore_operations_counts(tmp_path: Path) -> None: + from mda_cli.backup import RestoreOperation + + bak = tmp_path / "x.bak" + bak.write_text("old", encoding="utf-8") + target = tmp_path / "x.md" + target.write_text("new", encoding="utf-8") + op = RestoreOperation( + backup_path=bak, + target_path=target, + src=target, + ok=True, + skip_reason=None, + ) + applied, skipped, msgs = apply_restore_operations([op], dry_run=False) + assert applied == 1 + assert skipped == 0 + assert target.read_text(encoding="utf-8") == "old" + assert any("RESTORED" in m for m in msgs) diff --git a/tests/test_preflight.py b/tests/test_preflight.py new file mode 100644 index 0000000..5e6d811 --- /dev/null +++ b/tests/test_preflight.py @@ -0,0 +1,32 @@ +from __future__ import annotations + +from pathlib import Path + +from mda_cli.core import Job +from mda_cli.preflight import build_batch_preflight, format_preflight_message + + +def test_build_batch_preflight_token_warning(tmp_path: Path) -> None: + big = tmp_path / "big.md" + big.write_text("x" * 500_000, encoding="utf-8") + jobs = [Job(src=big, dst=big.with_suffix(".restructured.md"))] + summary = build_batch_preflight(jobs, max_tokens=4096, output_mode="sibling") + assert summary.file_count == 1 + assert summary.output_mode == "sibling" + assert summary.total_bytes >= 500_000 + assert any("max-tokens" in w.lower() or "token" in w.lower() for w in summary.warnings) + body = format_preflight_message(summary) + assert "Files selected: 1" in body + assert "Token estimate" in body + assert "Warnings:" in body + + +def test_build_batch_preflight_no_warnings_small_batch(tmp_path: Path) -> None: + small = tmp_path / "note.md" + small.write_text("# Hi\n", encoding="utf-8") + jobs = [Job(src=small, dst=small)] + summary = build_batch_preflight(jobs, max_tokens=16_000, output_mode="in_place") + assert summary.file_count == 1 + assert summary.warnings == () + body = format_preflight_message(summary) + assert "No size or token warnings" in body diff --git a/tests/test_preview.py b/tests/test_preview.py index b1f66ea..27cb8e1 100644 --- a/tests/test_preview.py +++ b/tests/test_preview.py @@ -7,6 +7,7 @@ from mda_cli.preview import ( DEFAULT_PREVIEW_MAX_CHARS, format_preview_display, + format_preview_error_banner, format_preview_metadata, truncate_preview_text, ) @@ -36,6 +37,12 @@ def test_format_preview_display_truncated() -> None: assert "… truncated" in text +def test_format_preview_error_banner() -> None: + text = format_preview_error_banner("PDF extra not installed") + assert "EXTRACTION ERROR" in text + assert "PDF extra not installed" in text + + def test_format_preview_metadata(tmp_path: Path) -> None: p = tmp_path / "note.md" p.write_text("# Hi", encoding="utf-8") diff --git a/tests/test_warn_tokens.py b/tests/test_warn_tokens.py new file mode 100644 index 0000000..357809d --- /dev/null +++ b/tests/test_warn_tokens.py @@ -0,0 +1,18 @@ +from __future__ import annotations + +from pathlib import Path + +from mda_cli.core import Job, warn_large_inputs + + +def test_warn_large_inputs_max_tokens_budget(tmp_path: Path) -> None: + big = tmp_path / "big.md" + big.write_text("x" * 500_000, encoding="utf-8") + jobs = [Job(src=big, dst=big.with_suffix(".restructured.md"))] + messages: list[str] = [] + + warn_large_inputs(jobs, max_tokens=4096, writer=messages.append) + + joined = "\n".join(messages) + assert "WARNING" in joined + assert "--max-tokens" in joined