Raven-1/tests/test_ppt_engine_materials.py
Tchen 5b4cce3f43 feat(ppt-engine): deliver where the user named, one route onto a page, and the ten templates
## Summary

This branch folds four merge requests and one unpushed commit into one, so that `main`
takes one review, one approval and one merge instead of four, and so the eight-file
overlap between them is resolved once. It carries, in this order:

- **!602** `feat/ppt_deliver_to_named_path` -- `ppt_build` takes `deliver_to`; every
  publish writes the deck under `out/` and copies the same measured bytes to the path
  the user named, recording it with `role: "delivery"`. The PDF preview rides along only
  where its own `.pdf` name is free: a file already at that stem is the user's and is
  left alone, and the reply says the preview stayed under `out/`.
- **!628** `feat/ppt_copy_route_and_page_isolation` -- `adapt` is removed, so cloning a
  template page and writing into it with `replace_text` is the only route; a page whose
  block raises loses only itself and the pages after it still build; a draft counts
  against the tier's build cap and one is released at it.
- **!632** `feat/ship_templates_and_soffice_dep` -- the ten bundled deck templates are
  tracked in git with per-file sha256 pins, so a fresh clone can build a deck; the
  LibreOffice dependency is declared and resolved through one resolver.
- **!642** `fix/ppt_copy_route_and_clone_typography` -- a copy-route lookup that finds
  nothing names the near miss before the inventory; a text frame with no paragraph at
  all can be written into; a cloned page's title size is said; `row_type_drift` compares
  the same structural slot across a run's units.
- **D57's other-page clause, rewired.** D57 gave five refusals a "did you mean" hint,
  and one of them also answered "the string you named is on page N of this template".
  That clause needs the file a page was cloned out of, and the only refusal holding it
  was `adapt`'s, which !628 removed. It is restored by reading `PPT_TEMPLATE_SOURCE`,
  which the runner already sets in the author's own process and which this module
  already reads for `bundled()`; the two helpers behind the clause are unchanged.
- **D60** `read the ground under each word, not across the whole box` -- the `unreadable`
  contrast gate also measures the render's own word boxes, as extra regions through the
  same guards, so a two-line block crossed by a narrow bright ridge can no longer elect
  the dark ground and read as legible.

Decisions the union forced, none of which drops a behaviour:

- The publish record is built !602's way, with its delivery `role`, and written the way
  D49 requires -- read back off the file, atomic, and refused rather than silent when
  the record cannot be written.
- `clear_region`, `shapes_in`, `page_box` and the refusal of a bare four-number region
  survive `adapt`'s removal; so do `_heading_rows` and its helpers, which the row
  reading loads even though the call they were written for is gone.
- The author-facing guidance names !628's single route and keeps D59's rule that a
  region must declare which reading it is.
**The decision log carries a numbering collision, on purpose.** It is a set union with
no wording changed, and the numbers are left exactly as each branch wrote them, so a
reviewer meets this rather than discovers it:

- !602 and !628 each wrote a **D37** and a **D38**, about different decisions.
- !628's **D45** is not `main`'s D45; they are unrelated entries under one number.
- !628's **D42** is `main`'s **D45** word for word, so the merged log carries one
  decision under two numbers.

Renumbering touches every cross-reference in the log, and doing that inside a
seventy-commit fold immediately before a merge is how a silent break gets in. The
numbering is being fixed on a separate branch, which carries a script and a test that
fails when a citation points at nothing.

## Type

- [x] Feature
- [x] Fix
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

Run on an interpreter carrying `python-pptx`, with `PYTHONPATH` at this worktree and its
`plugins-dist/ppt-engine`, confirmed from a neutral working directory that `raven` and
`raven_ppt` resolve out of the worktree and not the host repo.

- `make test-python` (the whole suite, as CI runs it) -- 20826 passed, 49 skipped, and
  26 failures this machine produces on any branch: it has no `/usr/share/zoneinfo`, so
  every cron timezone case raises `ValueError: unknown timezone 'Asia/Shanghai'`. Checked
  out plain `origin/main` in a clean worktree and ran the same two files: the same 6
  failed and 20 errors, the ids identical character for character. Nothing on this branch
  touches cron or timezones. The stale `test_trunks_executor_would_have_dropped_that_output`
  that failed on the previous revision is retired on the new base and its replacement,
  `test_trunks_executor_preserves_output_on_timeout`, was run on its own and passes.
  The 6 failures and 20 errors are all cron timezone tests failing identically on plain
  `main` in a clean worktree, on a host with no `/usr/share/zoneinfo`.
- `make check-commits` -- exit 0.
- `make check-source-language` -- exit 0.
- `make check-large-files` -- exit 0.
- `make lint-imports` -- exit 0, 9 contracts kept, 0 broken.
- `ruff check` and `ruff format --check` over the 80 changed Python files -- exit 0.
- `pytest tests/test_ppt_engine_templates.py tests/test_utils_office.py
  tests/test_cli_doctor_commands.py` -- 97 passed; the ten templates are tracked and
  satisfy their pins, and the soffice resolver finds the local binary.
- The delivery contract, the record read-back, the page isolation and the near miss were
  each run as a measurement rather than asserted; the `unreadable` gate was run through
  the real LibreOffice render chain on a delivered 12-page deck and reports BLOCKING on
  page 12 under one word at 1.6:1.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [x] User-facing docs or screenshots are updated when needed

## Risk

User-visible changes: `ppt_build` gains `deliver_to` and delivers outside `out/`;
`adapt` is gone from the author-facing template module, so a program calling it fails to
import -- `title=` and `subtitle=` have no replacement and the heading is named by the
words the template put there; a draft now counts against the tier's build cap; three
gates that nothing measured against being wrong no longer refuse; and the ten templates
are now bytes in the repository rather than a private registry fetch.

Two pieces here are about not destroying something the user has. The delivery
refuses to write its `.pdf` where a file the user already owns sits at the same
stem, rather than replacing it; and the large-file roster moved out of the manifest
inside the tree being checked and into the gate's own source, so a change can no
longer grant itself the exception the gate exists to withhold.

Rollback is per piece: each of the five sources is a contiguous run of commits on this
branch, in the order listed above, so any one of them can be reverted as a range without
touching the others.

- [x] Backward compatibility considered
- [x] Security impact considered
- [x] Rollback path is clear for risky changes

## Related Issues

N/A
2026-09-11 05:52:36 +00:00

398 lines
17 KiB
Python

"""Source documents in, verified deck out.
These behaviours were the launcher's, each one written against a measured
failure, and they moved with the staging into the plugin's turn hook. Pinned here so the
move cannot quietly lose one.
"""
import zipfile
from pathlib import Path
import pytest
from raven_ppt.plugin import materials
def _pptx(path: Path, slides: int = 2) -> Path:
path.parent.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(path, "w") as archive:
for index in range(1, slides + 1):
archive.writestr(f"ppt/slides/slide{index}.xml", "<sld/>")
return path
# -- reading the task text ---------------------------------------------------
def test_a_fenced_block_names_the_materials():
text = 'build it\n```raven-ppt\n{"materials": ["/a.md", "/b.md"]}\n```'
assert materials.inputs_from_prompt(text) == ["/a.md", "/b.md"]
def test_a_declared_template_is_refused_the_way_the_launcher_refuses_it():
"""One channel, both entry points. The launcher raises on this declaration, so
accepting it here would make the same task text succeed over one transport and
fail over the other."""
text = '```raven-ppt\n{"materials": ["/a.md"], "template": "/house.pptx"}\n```'
with pytest.raises(materials.StagingError) as caught:
materials.inputs_from_prompt(text)
assert "ppt_template" in str(caught.value)
def test_a_template_key_that_is_not_a_path_is_left_alone():
"""A style name in an unrelated JSON block is not a path declaration."""
text = '```json\n{"materials": ["/a.md"], "template": "minimal"}\n```'
assert materials.inputs_from_prompt(text) == ["/a.md"]
def test_a_plain_json_fence_is_read_too():
text = '```json\n{"materials": ["/a.md"]}\n```'
assert materials.inputs_from_prompt(text) == ["/a.md"]
def test_a_fence_that_declares_neither_key_is_not_an_inputs_block():
"""A task may well quote unrelated JSON; only a block carrying one of the two
keys is a declaration."""
assert materials.inputs_from_prompt('```json\n{"unrelated": 1}\n```') == []
def test_malformed_json_in_a_fence_is_skipped_not_fatal():
text = '```raven-ppt\n{not json}\n```\n```raven-ppt\n{"materials": ["/a.md"]}\n```'
assert materials.inputs_from_prompt(text) == ["/a.md"]
def test_a_declared_block_does_not_filter_by_file_type(tmp_path):
"""The type list guards the prose scan alone: prose names paths that are not
material, while a declaration is explicit."""
odd = tmp_path / "notes.xyz"
odd.write_text("facts", encoding="utf-8")
declared = materials.inputs_from_prompt('```raven-ppt\n{"materials": ["%s"]}\n```' % odd)
assert declared == [str(odd)]
assert materials.materials_from_prompt(f"see {odd}") == []
def test_a_path_followed_by_non_ascii_punctuation_is_still_found(tmp_path):
"""The tail is trimmed a character at a time rather than by stripping ASCII
full stops: punctuation in any script sits against the path, and a mark that
is not ASCII stays attached -- so the suffix of the raw match is not ``.md``
and the whole document used to be dropped without a word."""
notes = tmp_path / "notes.md"
notes.write_text("facts", encoding="utf-8")
assert materials.materials_from_prompt(f"read {notes}\u3002then build") == [str(notes)]
def test_a_path_that_does_not_exist_is_not_material(tmp_path):
assert materials.materials_from_prompt(f"see {tmp_path / 'ghost.md'}") == []
def test_sources_are_deduplicated_by_real_path(tmp_path):
"""Two names for one file would be staged twice under different names, so the
prompt would claim material the run does not have."""
real = tmp_path / "notes.md"
real.write_text("facts", encoding="utf-8")
link = tmp_path / "alias.md"
link.symlink_to(real)
assert materials.unique_sources([str(real), str(link), str(real)]) == [str(real)]
# -- staging -----------------------------------------------------------------
def test_a_colliding_basename_is_suffixed_not_overwritten(tmp_path):
"""The overwrite loses material exactly as silently as a skipped copy would:
the copy succeeds, and the prompt lists two entries resolving to one file."""
one = tmp_path / "a" / "notes.md"
two = tmp_path / "b" / "notes.md"
for path, body in ((one, "first"), (two, "second")):
path.parent.mkdir(parents=True)
path.write_text(body, encoding="utf-8")
target = tmp_path / "materials"
staged = materials.stage(target, [str(one), str(two)], set())
assert [p.name for _s, p in staged] == ["notes.md", "notes-2.md"]
assert (target / "notes.md").read_text(encoding="utf-8") == "first"
assert (target / "notes-2.md").read_text(encoding="utf-8") == "second"
def test_an_unreadable_source_stops_the_run(tmp_path):
import pytest
with pytest.raises(materials.StagingError) as exc:
materials.stage(tmp_path / "materials", [str(tmp_path / "ghost.md")], set())
assert "ghost.md" in str(exc.value)
# Actionable rather than a bare errno: the message is what the caller shows.
assert "absolute path" in str(exc.value)
def test_the_prompt_block_names_the_copy_and_not_the_source(tmp_path):
"""A collision-suffixed copy has a name the source does not, so the listing is
built from the pairing rather than re-derived."""
text = materials.describe(
[("/elsewhere/notes.md", tmp_path / "materials" / "notes-2.md")],
tmp_path / "materials",
tmp_path / "out",
)
assert "notes.md (from /elsewhere/notes.md) -> " in text
assert str(tmp_path / "materials" / "notes-2.md") in text
assert f"Compile the deck under {tmp_path / 'out'}/" in text
def test_a_settled_deck_is_not_told_to_publish_again(tmp_path):
"""The turn after a delivery. Every other turn of a session ends the block with
"compile the deck", and on the turn where the user says the deck is fine that
instruction is wrong -- it asks for a build nothing needs. What stands in its
place states the record and decides nothing: whether this turn is deck work is
the model's reading of the user's message, not this function's."""
deck = _pptx(tmp_path / "out" / "intro.pptx")
text = materials.describe([], tmp_path / "materials", tmp_path / "out", [deck])
assert f"Compile the deck under {tmp_path / 'out'}/" not in text
assert str(deck) in text and "stands as published" in text
assert "another ppt_build that publishes" in text
assert "the MEDIA line comes from a publish and from nothing else" in text
def test_a_settled_deck_is_not_told_to_go_gathering(tmp_path):
"""The gathering advice is written for a deck with no material, and a deck on the
publish record has already taken it -- on that turn the text is stale, not just
redundant. Suppressing it turns on the record, the same fact the standing block
turns on, and not on any reading of what the turn is for."""
deck = _pptx(tmp_path / "out" / "intro.pptx")
text = materials.describe([], tmp_path / "materials", tmp_path / "out", [deck])
assert "No material staged for this run" not in text
assert "has to be gathered" not in text
for tool in ("web_search", "ppt_image_search", "web_fetch", "ppt_fetch"):
assert tool not in text
assert str(deck) in text and "stands as published" in text
def test_with_no_deck_standing_both_branches_keep_their_text(tmp_path):
"""The two inputs the fork was measured on, word for word. A deck that has not
been published yet reads exactly what it read before any of this.
Asserted as present in the closing block rather than as the last characters of
it: what this pins is that the sentence still reaches the author, and another
change may add a sentence of its own after it -- naming a delivery destination,
say. An `endswith` here would fail on that without the sentence having moved.
"""
compile_said = (
f" Compile the deck under {tmp_path / 'out'}/ and end your final reply with the MEDIA line naming it."
)
first = materials.describe([], tmp_path / "materials", tmp_path / "out")
assert "# No material staged for this run" in first and "has to be gathered" in first
assert compile_said in first
with_material = materials.describe(
[("/elsewhere/q3.md", tmp_path / "materials" / "q3.md")], tmp_path / "materials", tmp_path / "out"
)
assert "# Material staged for this run" in with_material and "q3.md" in with_material
assert compile_said in with_material
def test_material_staged_on_a_settled_deck_keeps_its_listing(tmp_path):
"""The two blocks compose: the deck on the record is stated, and the material this
turn staged is listed as it always was. Neither excludes the other, because the
listing is a fact about the folder and the standing deck is a fact about the
record -- and one turn can need both."""
deck = _pptx(tmp_path / "out" / "intro.pptx")
text = materials.describe(
[("/elsewhere/q3.md", tmp_path / "materials" / "q3.md")],
tmp_path / "materials",
tmp_path / "out",
[deck],
)
assert "# Material staged for this run" in text and "q3.md" in text
assert str(deck) in text and "another ppt_build that publishes" in text
assert f"Compile the deck under {tmp_path / 'out'}/" not in text
def test_only_a_published_deck_counts_as_standing(tmp_path):
"""A copy somebody put under out/ is not a deck this project published, and the
record rather than the folder is what says so."""
(tmp_path / "out").mkdir()
stray = _pptx(tmp_path / "out" / "stray.pptx")
assert materials.published_decks(tmp_path / "out", set()) == []
digest = materials._digest(stray)
assert materials.published_decks(tmp_path / "out", {digest}) == [stray]
def test_a_run_with_nothing_staged_is_told_to_gather_rather_than_refused(tmp_path):
"""The branch this block did not have. A deck author runs with or without
documents; the launcher stopped refusing and this had to follow."""
text = materials.describe([], tmp_path / "materials", tmp_path / "out")
assert "No material staged for this run" in text
for tool in ("web_search", "web_fetch", "ppt_fetch", "ppt_image_search"):
assert tool in text
# The sentence that would forbid everything: it points at an empty directory.
assert "Use only files under" not in text
assert f"Compile the deck under {tmp_path / 'out'}/" in text
# -- verifying the deck ------------------------------------------------------
def test_a_file_that_is_not_an_archive_has_no_slides(tmp_path):
broken = tmp_path / "broken.pptx"
broken.write_bytes(b"not a zip")
assert materials.slide_count(broken) == 0
def test_an_archive_with_no_slide_parts_has_no_slides(tmp_path):
empty = tmp_path / "empty.pptx"
with zipfile.ZipFile(empty, "w") as archive:
archive.writestr("docProps/app.xml", "<p/>")
assert materials.slide_count(empty) == 0
def test_a_missing_file_has_no_slides(tmp_path):
assert materials.slide_count(tmp_path / "gone.pptx") == 0
def test_the_announcement_chooses_between_this_turns_decks(tmp_path):
"""A build writes intermediates next to the deck, and both names appear in the
reply -- so the announcement is what picks, not a scan of the whole text."""
out = tmp_path / "out"
_pptx(out / "deck.pptx")
wanted = _pptx(out / "quarterly.pptx", slides=5)
reply = "I wrote deck.pptx while building.\n\nMEDIA: " + str(wanted)
assert materials.verified_deck(out, reply, {}) == (wanted, 5)
def test_the_newest_wins_when_the_reply_names_none_of_them(tmp_path):
import os
import time
out = tmp_path / "out"
old = _pptx(out / "old.pptx")
new = _pptx(out / "new.pptx", slides=4)
now = time.time()
os.utime(old, (now - 100, now - 100))
os.utime(new, (now, now))
assert materials.verified_deck(out, "all done", {}) == (new, 4)
def test_a_deck_from_an_earlier_turn_is_not_a_candidate(tmp_path):
"""One session's job directory accumulates every turn's decks, so a turn that
published nothing must not fall through to the newest and hand back an earlier
turn's file as its own result."""
out = tmp_path / "out"
earlier = _pptx(out / "earlier.pptx")
before = materials.deck_mtimes(out)
assert materials.verified_deck(out, f"MEDIA: {earlier}", before) == (None, 0)
def test_an_empty_directory_publishes_nothing(tmp_path):
assert materials.verified_deck(tmp_path / "out", "MEDIA: /nowhere.pptx", {}) == (None, 0)
# -- delivery ----------------------------------------------------------------
def test_two_paths_in_one_match_are_both_material(tmp_path):
"""A delimiter the pattern does not end on joins two names into one match.
The pattern ends a path on ASCII punctuation, so ``a.md, b.md`` arrives as two
matches and the same sentence written with an ideographic comma arrives as one.
Taking the first file in it as the answer for the whole match dropped the second
document without a word. Widening the pattern is the other way to fix this and is
worse: every mark added to it is a mark a filename may not contain.
"""
first, second = tmp_path / "one.md", tmp_path / "two.md"
for path in (first, second):
path.write_text("facts", encoding="utf-8")
for joiner in ("\u3001", "\uff09\u548c\uff08", "\u300d\u548c\u300c", "\u060c", "\u0964"):
assert materials.materials_from_prompt(f"{first}{joiner}{second}") == [
str(first),
str(second),
], joiner
def test_a_filename_holding_wide_punctuation_survives_a_second_path(tmp_path):
"""The name itself can hold the marks a prose delimiter is made of.
A pattern taught to end on full-width brackets cuts this path in half and finds
nothing, which is why the fix is in the walk and not in the character class.
"""
named = tmp_path / "\u62a5\u544a\uff08\u7ec8\u7248\uff09.md"
other = tmp_path / "b.md"
for path in (named, other):
path.write_text("facts", encoding="utf-8")
assert materials.materials_from_prompt(f"{named}\u3001{other}") == [str(named), str(other)]
def test_a_name_that_does_not_exist_does_not_hide_the_next_one(tmp_path):
"""The scan carries on past a path that names nothing, not only past a hit."""
real = tmp_path / "real.md"
real.write_text("facts", encoding="utf-8")
assert materials.materials_from_prompt(f"{tmp_path / 'ghost.md'}\u3001{real}") == [str(real)]
def test_with_a_publish_record_only_a_recorded_deck_verifies(tmp_path):
"""A copy the model made under out/ is a valid deck newer than the turn; with the
publish step's digests in hand it is not a candidate, and is named as unpublished."""
import hashlib
out = tmp_path / "out"
published = _pptx(out / "published.pptx", slides=3)
copied = _pptx(out / "copied.pptx", slides=4)
digests = {hashlib.sha256(published.read_bytes()).hexdigest()}
assert materials.verified_deck(out, f"MEDIA: {copied}", {}, digests) == (published, 3)
assert materials.verified_deck(out, f"MEDIA: {copied}", {}, set()) == (None, 0)
assert materials.unpublished_decks(out, {}, digests) == [copied]
# -- the destination the user named ------------------------------------------
def _digest(path: Path) -> str:
import hashlib
return hashlib.sha256(path.read_bytes()).hexdigest()
def test_the_delivered_path_verifies_by_its_digest(tmp_path):
"""A deck the publish step wrote outside out/ stands on the same terms as one under it."""
out = tmp_path / "out"
published = _pptx(out / "deck.pptx", slides=6)
delivered = tmp_path / "handoff" / "ravenx-intro.pptx"
delivered.parent.mkdir()
delivered.write_bytes(published.read_bytes())
digests = {_digest(published)}
assert materials.verified_deck(out, f"MEDIA: {delivered}", {}, digests, [delivered]) == (delivered, 6)
assert materials.verified_deck(out, f"MEDIA: {published}", {}, digests, [delivered]) == (published, 6)
assert materials.verified_deck(out, f"MEDIA: {delivered}", {}, digests) == (published, 6), (
"off the record, the path outside out/ is not a candidate and out/ answers"
)
def test_a_delivery_keeping_the_decks_name_is_told_apart_from_out_by_its_whole_path(tmp_path):
out = tmp_path / "out"
published = _pptx(out / "deck.pptx", slides=2)
delivered = tmp_path / "handoff" / "deck.pptx"
delivered.parent.mkdir()
delivered.write_bytes(published.read_bytes())
digests = {_digest(published)}
assert materials.verified_deck(out, f"MEDIA: {delivered}", {}, digests, [delivered]) == (delivered, 2)
assert materials.verified_deck(out, f"MEDIA: {published}", {}, digests, [delivered]) == (published, 2)
def test_an_earlier_turns_delivery_is_not_this_turns(tmp_path):
out = tmp_path / "out"
published = _pptx(out / "deck.pptx", slides=2)
delivered = tmp_path / "handoff" / "intro.pptx"
delivered.parent.mkdir()
delivered.write_bytes(published.read_bytes())
before = materials.deck_mtimes(out, [delivered])
assert set(before) == {published, delivered}
assert materials.verified_deck(out, f"MEDIA: {delivered}", before, {_digest(published)}, [delivered]) == (None, 0)
def test_the_prompt_says_where_a_named_destination_goes(tmp_path):
text = materials.describe([], tmp_path / "materials", tmp_path / "out")
assert "state that once as ppt_build's deliver_to" in text