diff --git a/skills/deeppapernote/SKILL.md b/skills/deeppapernote/SKILL.md index b02128b..2216b61 100644 --- a/skills/deeppapernote/SKILL.md +++ b/skills/deeppapernote/SKILL.md @@ -131,6 +131,7 @@ Final user report: - When `reader_research_direction` came from `research_memory`, say in one line that the transferable ideas followed the user's research memory. - When the save receipt's `glossary_followup.status` is `suggested`, suggest running paper-glossary on this paper and give its `source_manifest` and `note_path`; when `regenerated` is true, add that term links the glossary had added to the replaced note return only after that rerun. For any other status, say nothing about the glossary. - When the save replaced an existing note, report the save receipt's `user_notes.status`: `preserved` kept the user's My Notes; `untouched` found none to keep; `heading_missing` found no My Notes heading. Only when `user_notes.legacy_format` is true, add that the kept block predates the current note format and may be old model-written text the user can delete. +- When a save receipt `replaced_images` entry lists other notes in `also_embedded_by`, say in one line that the new crop of that figure also shows in those notes. - Add overwrite actions, preference changes, or user-relevant warnings only when they occurred. ## References diff --git a/skills/deeppapernote/scripts/write_obsidian_note.py b/skills/deeppapernote/scripts/write_obsidian_note.py index 68353dc..f7ce30b 100644 --- a/skills/deeppapernote/scripts/write_obsidian_note.py +++ b/skills/deeppapernote/scripts/write_obsidian_note.py @@ -294,7 +294,14 @@ def materialize_insert_decisions( decisions: dict, asset_subdir: str, created_paths: list[Path] | None = None, + replaced_images: list[dict] | None = None, ) -> list[dict]: + """Copy each inserted image into the paper's asset directory. + + An existing image with other bytes is backed up and replaced; each + replacement is appended to ``replaced_images`` so the caller can restore it + on rollback and report the other notes that also embed it. + """ asset_dir = target_path.parent / asset_subdir pending: list[tuple[Path, Path, str, dict]] = [] planned_hashes: dict[Path, str] = {} @@ -336,11 +343,6 @@ def materialize_insert_decisions( f"Insert decisions assign different bytes to the same image: {filename}" ) planned_hashes[dest_image] = current_sha256 - if dest_image.is_file() and file_sha256(dest_image) != current_sha256: - raise SystemExit( - f"Refusing to save because an existing paper-local image has different bytes: " - f"{filename}" - ) pending.append( ( source_image, @@ -363,19 +365,44 @@ def materialize_insert_decisions( asset_dir.mkdir(parents=True, exist_ok=True) materialized: list[dict] = [] for source_image, dest_image, current_sha256, record in pending: - if not dest_image.is_file(): - if created_paths is not None: - created_paths.append(dest_image) - shutil.copy2(source_image, dest_image) - if file_sha256(dest_image) != current_sha256: - raise SystemExit( - f"Materialized image bytes do not match the reviewed asset SHA-256: " - f"{dest_image.name}" - ) + if dest_image.is_file(): + if file_sha256(dest_image) == current_sha256: + materialized.append(record) + continue + replacement = { + "path": dest_image, + "backup": backup_file(dest_image), + "relative_path": record["relative_markdown_path"], + "also_embedded_by": notes_embedding( + target_path, record["relative_markdown_path"] + ), + } + if replaced_images is not None: + replaced_images.append(replacement) + elif created_paths is not None: + created_paths.append(dest_image) + shutil.copy2(source_image, dest_image) + if file_sha256(dest_image) != current_sha256: + raise SystemExit( + f"Materialized image bytes do not match the reviewed asset SHA-256: " + f"{dest_image.name}" + ) materialized.append(record) return materialized +def notes_embedding(target_path: Path, relative_image: str) -> list[str]: + """Name the other notes beside ``target_path`` that embed ``relative_image``.""" + names = [] + for note in sorted(target_path.parent.glob("*.md")): + if note.name == target_path.name or not note.is_file(): + continue + text = note.read_text(encoding="utf-8", errors="replace") + if note_references_image_embed(text, relative_image): + names.append(note.name) + return names + + LINT_FAILURE_DETAIL_LIMIT = 20 @@ -1093,6 +1120,7 @@ def main() -> None: note_backup = None sidecar_backup = None created_assets: list[Path] = [] + replaced_images: list[dict] = [] note_attempted = False sidecar_attempted = False try: @@ -1105,6 +1133,7 @@ def main() -> None: figure_decisions, asset_subdir, created_assets, + replaced_images, ) if figure_decisions else [] @@ -1181,6 +1210,8 @@ def main() -> None: restore_file(target_path, note_backup) for created_asset in reversed(created_assets): created_asset.unlink(missing_ok=True) + for replaced in reversed(replaced_images): + restore_file(replaced["path"], replaced["backup"]) if not asset_directory_existed: try: asset_dir.rmdir() @@ -1192,6 +1223,9 @@ def main() -> None: note_backup.unlink(missing_ok=True) if sidecar_backup is not None: sidecar_backup.unlink(missing_ok=True) + for replaced in replaced_images: + if replaced["backup"] is not None: + replaced["backup"].unlink(missing_ok=True) payload = { "status": "ok", @@ -1205,6 +1239,10 @@ def main() -> None: **new_domain, "images_dir": str(asset_dir), "materialized_figures": materialized_figures, + "replaced_images": [ + {"file": item["relative_path"], "also_embedded_by": item["also_embedded_by"]} + for item in replaced_images + ], "overwrote_existing_note": overwrote_existing_note, "admission": admission, "domain_routing_skipped": domain_routing_skipped, diff --git a/tests/test_cli_fallback.py b/tests/test_cli_fallback.py index 971d9a1..1033dfe 100644 --- a/tests/test_cli_fallback.py +++ b/tests/test_cli_fallback.py @@ -159,81 +159,6 @@ def test_write_note_reuses_source_directory_for_another_language(tmp_path: Path) assert set(sidecar["sources"]["a" * 64]["notes"]) == {"zh-CN", "en"} -def test_write_note_does_not_replace_another_language_note_image(tmp_path: Path) -> None: - vault = tmp_path / "vault" - vault.mkdir() - env = os.environ.copy() - env["DEEPPAPERNOTE_DISABLE_SHELL_CONFIG"] = "1" - - def save_args(note_text: str, language: str, source_image: Path) -> list[str]: - decisions_path = tmp_path / "insert-decisions.json" - decisions_path.write_text( - json.dumps({ - "output_language": language, - "decisions": [{ - "source_id": "Figure 1", - "decision": "insert", - "source_image_path": str(source_image), - "source_image_filename": "shared.png", - "vault_image_filename": "shared.png", - **reviewed_insert_fields(source_image), - }], - }), - encoding="utf-8", - ) - return formal_save_args(tmp_path, note_text, language, decisions_path=decisions_path) - - chinese_image = tmp_path / "chinese.png" - english_image = tmp_path / "english.png" - chinese_image.write_bytes(b"chinese-image") - english_image.write_bytes(b"english-image") - chinese_note = "# My Test Paper\n\n![Figure 1](images/shared.png)\n" - english_note = "# My Test Paper\n\n![Figure 1](images/shared.png)\n" - - first = subprocess.run( - [ - sys.executable, - str(WRITE_SCRIPT), - "--vault", - str(vault), - "--title", - "My Test Paper", - *source_manifest_args(tmp_path), - *save_args(chinese_note, "zh-CN", chinese_image), - ], - cwd=tmp_path, - env=env, - capture_output=True, - text=True, - ) - assert first.returncode == 0, first.stderr - directory = Path(json.loads(first.stdout)["note_path"]).parent - - second = subprocess.run( - [ - sys.executable, - str(WRITE_SCRIPT), - "--vault", - str(vault), - "--title", - "My Test Paper", - "--language", - "en", - *source_manifest_args(tmp_path), - *save_args(english_note, "en", english_image), - ], - cwd=tmp_path, - env=env, - capture_output=True, - text=True, - ) - - assert second.returncode != 0 - assert "existing paper-local image has different bytes" in second.stderr - assert (directory / "images/shared.png").read_bytes() == b"chinese-image" - assert not (directory / "My_Test_Paper.en.md").exists() - - def test_formal_save_rolls_back_when_sidecar_finalization_fails( tmp_path: Path, monkeypatch: pytest.MonkeyPatch, diff --git a/tests/test_image_replacement.py b/tests/test_image_replacement.py new file mode 100644 index 0000000..92a034e --- /dev/null +++ b/tests/test_image_replacement.py @@ -0,0 +1,126 @@ +"""Formal Save replaces a paper-local image whose bytes changed since the last save.""" + +from __future__ import annotations + +import hashlib +import json +import subprocess +import sys +from pathlib import Path + +import pytest +import write_obsidian_note +from formal_save_helpers import formal_save_args, reviewed_insert_fields +from test_cli_fallback import WRITE_SCRIPT, source_manifest_args + +TITLE = "My Test Paper" +NOTE = "# My Test Paper\n\n![Figure 1](images/figure.png)\n" + + +@pytest.fixture(autouse=True) +def no_shell_config(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("DEEPPAPERNOTE_DISABLE_SHELL_CONFIG", "1") + + +def save_argv(tmp_path: Path, image_bytes: bytes, language: str, overwrite: Path | None) -> list[str]: + image = tmp_path / f"crop-{hashlib.sha256(image_bytes).hexdigest()[:8]}.png" + image.write_bytes(image_bytes) + decisions_path = tmp_path / "insert-decisions.json" + decisions_path.write_text( + json.dumps({ + "output_language": language, + "decisions": [{ + "source_id": "Figure 1", + "decision": "insert", + "source_image_path": str(image), + "source_image_filename": image.name, + "vault_image_filename": "figure.png", + **reviewed_insert_fields(image), + }], + }), + encoding="utf-8", + ) + argv = [str(WRITE_SCRIPT), "--vault", str(tmp_path / "vault"), "--title", TITLE] + if language != "zh-CN": + argv += ["--language", language] + if overwrite is not None: + existing_sha256 = hashlib.sha256(overwrite.read_bytes()).hexdigest() + argv += ["--overwrite-existing-note", "--expected-existing-note-sha256", existing_sha256] + return [ + *argv, + *source_manifest_args(tmp_path), + *formal_save_args(tmp_path, NOTE, language, decisions_path=decisions_path), + ] + + +def save( + tmp_path: Path, image_bytes: bytes, language: str = "zh-CN", overwrite: Path | None = None +) -> dict: + (tmp_path / "vault").mkdir(exist_ok=True) + result = subprocess.run( + [sys.executable, *save_argv(tmp_path, image_bytes, language, overwrite)], + cwd=tmp_path, + capture_output=True, + text=True, + ) + assert result.returncode == 0, result.stderr + return json.loads(result.stdout) + + +def leftover_backups(images_dir: Path) -> list[str]: + return sorted(path.name for path in images_dir.iterdir() if path.name.endswith(".backup")) + + +def test_overwrite_replaces_an_image_only_this_note_embeds(tmp_path: Path) -> None: + first = save(tmp_path, b"old crop") + note_path = Path(first["note_path"]) + + second = save(tmp_path, b"new crop", overwrite=note_path) + + images_dir = note_path.parent / "images" + assert (images_dir / "figure.png").read_bytes() == b"new crop" + assert second["replaced_images"] == [{"file": "images/figure.png", "also_embedded_by": []}] + assert leftover_backups(images_dir) == [] + + +def test_another_language_save_replaces_the_shared_image_and_names_the_sibling( + tmp_path: Path, +) -> None: + chinese = Path(save(tmp_path, b"chinese crop")["note_path"]) + + english = save(tmp_path, b"english crop", language="en") + + assert Path(english["note_path"]).is_file() + assert (chinese.parent / "images" / "figure.png").read_bytes() == b"english crop" + assert english["replaced_images"] == [ + {"file": "images/figure.png", "also_embedded_by": [chinese.name]} + ] + + +def test_an_unchanged_image_is_not_reported_as_replaced(tmp_path: Path) -> None: + note_path = Path(save(tmp_path, b"same crop")["note_path"]) + + second = save(tmp_path, b"same crop", overwrite=note_path) + + assert second["replaced_images"] == [] + + +def test_a_failed_save_restores_the_replaced_image( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + note_path = Path(save(tmp_path, b"old crop")["note_path"]) + original_note = note_path.read_bytes() + monkeypatch.setattr(sys, "argv", save_argv(tmp_path, b"new crop", "zh-CN", note_path)) + + def fail_hidden(_path: Path) -> None: + raise OSError("simulated Hidden attribute failure") + + monkeypatch.setattr(write_obsidian_note, "ensure_sidecar_hidden", fail_hidden) + + with pytest.raises(OSError, match="simulated"): + write_obsidian_note.main() + + images_dir = note_path.parent / "images" + assert (images_dir / "figure.png").read_bytes() == b"old crop" + assert note_path.read_bytes() == original_note + assert leftover_backups(images_dir) == [] diff --git a/tests/test_visual_asset_bindings.py b/tests/test_visual_asset_bindings.py index 94ce15e..5356abe 100644 --- a/tests/test_visual_asset_bindings.py +++ b/tests/test_visual_asset_bindings.py @@ -129,16 +129,39 @@ def counting_file_sha256(path: Path) -> str: assert hashed.count(existing) == 1 -def test_save_still_refuses_a_different_existing_image(tmp_path: Path) -> None: - decision = insert_decision(figure_candidate(tmp_path)) +def replace_existing(tmp_path: Path) -> list[dict]: existing = tmp_path / "paper" / "images" / "figure.png" - existing.parent.mkdir(parents=True) + existing.parent.mkdir(parents=True, exist_ok=True) existing.write_bytes(b"other figure") + replaced: list[dict] = [] + write_obsidian_note.materialize_insert_decisions( + NOTE_TEXT, + tmp_path / "paper" / "note.md", + {"decisions": [insert_decision(figure_candidate(tmp_path))]}, + "images", + replaced_images=replaced, + ) + assert existing.read_bytes() == b"figure" + return replaced - with pytest.raises(SystemExit, match="different bytes"): - materialize(tmp_path, decision) - assert existing.read_bytes() == b"other figure" +def test_save_backs_up_a_different_existing_image_before_replacing_it(tmp_path: Path) -> None: + replaced = replace_existing(tmp_path) + + assert [item["backup"].read_bytes() for item in replaced] == [b"other figure"] + assert replaced[0]["relative_path"] == "images/figure.png" + + +def test_replacement_names_only_sibling_notes_that_embed_the_image(tmp_path: Path) -> None: + paper = tmp_path / "paper" + (paper / "nested").mkdir(parents=True) + for name in ("note.md", "note.en.md", "nested/other.md"): + (paper / name).write_text(NOTE_TEXT, encoding="utf-8") + (paper / "scratch.md").write_text("No figures here.\n", encoding="utf-8") + + replaced = replace_existing(tmp_path) + + assert replaced[0]["also_embedded_by"] == ["note.en.md"] def test_save_verifies_the_bytes_it_copies(tmp_path: Path, monkeypatch) -> None: