Repository navigation
render-pipeline: discovery stops reading \includegraphics targets in full (GH-INCLUDEGRAPHICS-READ) - #745
render-pipeline: discovery stops reading \includegraphics targets in full (GH-INCLUDEGRAPHICS-READ)#745d-q222 wants to merge 2 commits into
Conversation
…ery (GH-INCLUDEGRAPHICS-READ) What changed: `closure_read_budget` and `closure_from_disk` (crates/render-pipeline/src/protocol.rs) are replaced by one `discover_closure` walk that costs and performs the disk-read closure completion in a single pass, using project-files' own public path/scan/ graph-kind primitives instead of calling vendor's `ProjectGraph::discover_with`. A `.tex`/`.bib` candidate is still read in full (its content decides what to visit next, or whether it is valid UTF-8); a `\includegraphics` candidate now stops at `metadata()` (a stat) once its size is charged to the read budget -- its bytes are never opened. Existence and symlink-escape containment checks run for every kind, unchanged, before any read is attempted. What was run: cargo test --release --test include_closure_from_disk -> 18 passed (was 18) cargo test --release --lib (render-pipeline) -> 166 passed, 1 ignored (was 164, 1 ignored; +2 new) cargo test --release (render-pipeline, full suite) -> all green, 0 failed cargo test --release (flashtex-cli) -> 32 passed; 26 passed, 1 ignored (unchanged) cargo clippy --release --lib -> only a pre-existing-style type-complexity note Measured (throwaway example harness, PR #735's shape: one worker, warm block cache, `project_root` set with only the entry document sent, 30 compiles of a project with 5 `\includegraphics` targets at 3 MiB each under `[demo]` mode so the compiler's own, separate image read never runs): before (d0b4d59): avg 37.130 ms/compile, max 38.811 ms after: avg 0.151 ms/compile, max 0.237 ms Verified the new regression tests actually catch the bug: temporarily reintroduced a full `fs::read` for the `Graphic` branch and confirmed both new tests fail with a `read_error` diagnostic, then reverted. Next step: None; #75/#735's remaining "Not done" items (vendor re-pin, symlink-escape fail-open default, .bib's own full read) are unrelated and untouched. Implementation-Agent: claude-opus-5 subagent of daniel-parent Commit-Executor: daniel-parent subagent Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
d-q222
left a comment
There was a problem hiding this comment.
Independent verification round 4 — VERIFIED
Reviewed at head 63575d8f, parent d0b4d59b, in a dedicated worktree
(git fetch origin pull/745/head:refs/review/pr-745, git worktree add --detach),
per-PR CARGO_TARGET_DIR=…/target-vr4-745 and …/target-vr4-745base, CARGO_BUILD_JOBS=4.
Nothing on the branch was changed for the "as shipped" runs; the two probes below were
applied, run, and reverted (git checkout --).
1. The 245× headline reproduces — independently, to three decimals
I did not use the PR's harness. I wrote one from scratch
(tests/zz_review_bench.rs), dropped the byte-identical file into both worktrees,
and drove the public protocol::handle_line keystroke path. Project: main.tex with
5 \includegraphics targets at 3 MiB each (15 MiB), \usepackage[demo]{graphicx},
project_root set and only the entry document sent. 5 warmup iterations (warm page
cache) then 30 timed, both sides identical.
Fairness control built in: the same document is also compiled with project_root
absent, which switches discovery off entirely. That isolates discovery's cost from
everything else and proves the two binaries are otherwise the same speed.
$ cd crates/render-pipeline && cargo test --release --test zz_review_bench -- --nocapture
# parent d0b4d59b (#735 head)
BENCH iters=30 figures=5x3MiB
BENCH project_root=Some avg_ms=37.130 max_ms=38.847
BENCH project_root=None avg_ms=0.073 max_ms=0.102
BENCH discovery_delta_ms=37.057
# this PR, 63575d8f
BENCH iters=30 figures=5x3MiB
BENCH project_root=Some avg_ms=0.144 max_ms=0.177
BENCH project_root=None avg_ms=0.074 max_ms=0.081
BENCH discovery_delta_ms=0.070
avg_ms=37.130is the PR's number to the last digit (PR: 37.130).max_ms38.847
vs the PR's 38.811.- After: 0.144 ms against the PR's claimed 0.151 — the PR is very slightly
pessimistic about itself. 258× measured, against the ~245× claimed. - The
project_root=Nonecontrol is 0.073 vs 0.074 ms — statistically identical, so
the PR changes nothing outside discovery, and the ~37 ms saved is genuinely disk-read
cost, not a measurement artefact. - Discovery cost on this project goes 37.057 ms → 0.070 ms.
The harness is fair by construction: one file, copied unmodified to both trees, same
project staged by the same code, same warm-cache protocol, same public entry point,
both sides measured by the same timing loop.
2. The chmod 200 regression tests genuinely fail without the fix
This was the claim most worth attacking, so I reintroduced the read rather than taking
the PR's word for it. crates/render-pipeline/src/protocol.rs:239-241, the whole of
the change that stops the read:
if kind == FileKind::Graphic {
return Load::DiskGraphic;
}replaced with vendor Discovery::load's unconditional read of every kind:
if kind == FileKind::Graphic {
// REVIEW-ONLY REGRESSION PROBE
return match std::fs::read(&os) {
Ok(_) => Load::DiskGraphic,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Load::Missing,
Err(e) => Load::Error(e.to_string()),
};
}$ cargo test --release --lib includegraphics_read
running 2 tests
test protocol::includegraphics_read_tests::includegraphics_target_is_stat_only_never_opened ... FAILED
test protocol::includegraphics_read_tests::includegraphics_target_reached_through_an_on_disk_include_is_stat_only ... FAILED
---- …includegraphics_target_is_stat_only_never_opened stdout ----
panicked at src/protocol.rs:1010:9:
discovery must never try to open the graphic's content: [Diagnostic { code: "read_error",
message: "figures/plot.png could not be read: Permission denied (os error 13)", severity: Error,
sources: [SourceRange { path: "main.tex", start_byte: 0, end_byte: 30 }], … }]
---- …includegraphics_target_reached_through_an_on_disk_include_is_stat_only stdout ----
panicked at src/protocol.rs:1038:9:
discovery must never try to open the graphic's content: [Diagnostic { code: "read_error",
message: "figures/plot.png could not be read: Permission denied (os error 13)", severity: Error,
sources: [SourceRange { path: "sections/intro.tex", start_byte: 0, end_byte: 30 }], … }]
test result: FAILED. 0 passed; 2 failed; 0 ignored; 165 filtered out
Both fail, with exactly the read_error diagnostic the PR says they produce, and
the second one fails at the nested sections/intro.tex span, so it really is
exercising the on-disk \input path and not just the overlaid entry. Probe reverted;
both pass again as shipped. The chmod 200 trick is sound and the tests are not
vacuous.
3. Containment is genuinely unchanged — including for the Graphic kind
tests/include_closure_from_disk.rs is not touched by this PR (git diff --stat d0b4d59b 63575d8f = protocol.rs only, 355+/144−), so its 18 adversarial tests are
the same tests, now run against the new walk:
$ cargo test --release --test include_closure_from_disk
test result: ok. 18 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s
But that suite covers symlink escape and .. escape only for \input
(include_closure_from_disk.rs:251, :264, :427). Nothing in it covers the
Graphic kind specifically — which is exactly the branch this PR rewrote. So I wrote
that case myself (tests/zz_review_containment.rs, identical copy on both trees):
# this PR, 63575d8f
CONTAIN symlink-graphic reply: {"id":"c","payload":{"diagnostics":[{"code":"path_escapes_root",
"message":"\\includegraphics{figures/plot}: figures/plot.png is a symlink outside the project root",
"severity":"error","source":{"end_byte":96,"path":"main.tex","start_byte":66}}],"pages":[],…,"status":"failed"}…
CONTAIN dotdot-graphic reply: {"id":"c","payload":{"diagnostics":[{"code":"invalid_path",
"message":"\\includegraphics{../d-outer/secret}: path escapes the project root via '..'",
"severity":"error","source":{"end_byte":101,"path":"main.tex","start_byte":66}}],…,"status":"failed"}…
test result: ok. 2 passed; 0 failed
# parent d0b4d59b — byte-identical diagnostics, same codes, same spans
CONTAIN symlink-graphic reply: {"id":"c","payload":{"diagnostics":[{"code":"path_escapes_root",
"message":"\\includegraphics{figures/plot}: figures/plot.png is a symlink outside the project root",
"severity":"error","source":{"end_byte":96,"path":"main.tex","start_byte":66}}],…}
CONTAIN dotdot-graphic reply: {"id":"c","payload":{"diagnostics":[{"code":"invalid_path",
"message":"\\includegraphics{../d-outer/secret}: path escapes the project root via '..'",
"severity":"error","source":{"end_byte":101,"path":"main.tex","start_byte":66}}],…}
test result: ok. 2 passed; 0 failed
Diagnostic code, message text and byte spans are identical on both sides. The PR's
claim that path_escapes_root is still raised for an \includegraphics target that is
a symlink out of the root is true, and it is true in exactly the same words the vendor
walk used. No out-of-root bytes appear in either reply.
4. Suite totals
$ cargo test --release --lib # render-pipeline
test result: ok. 166 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out
Exactly the PR's claimed 166 passed; 0 failed; 1 ignored, and the two new tests are
inside it (2 passed; 165 filtered out when filtered to includegraphics_read).
Findings
Nothing blocking. Two notes, neither a correctness problem:
-
crates/render-pipeline/src/protocol.rs— "avg ms/compile" is a keystroke-path
number on a document whose own compile is 0.07 ms. The control run above makes
this explicit: withproject_rootabsent the whole compile is 0.073 ms, so the 245×
ratio is dominated by discovery and is not a 245× speedup of a real project's
compile. The durable, unattackable form of the claim is the one the measurement
actually supports: ~37 ms of disk read removed per compile, per 15 MiB of figures
in the closure — which is the number a reader should carry away. The PR's table is
not wrong; the ratio is just the least transferable way to say it. Worth a sentence
in the body rather than a code change. -
crates/render-pipeline/tests/include_closure_from_disk.rs— no adversarial test
covers theGraphickind. The PR's body asserts graphics containment explicitly
("path_escapes_rootis still raised for an\includegraphicstarget that is a
symlink out of the root"), and §3 above shows the assertion is true — but it is
currently held up by\input-shaped tests only, in a file this PR did not extend,
guarding a branch this PR rewrote. SinceLoad::DiskGraphicis now the one kind
that returns beforeread_to_string, a future edit to that early return is the
plausible way to lose it silently. Suggest folding the two cases from §3 into
include_closure_from_disk.rs. Not a blocker: behaviour is verified correct today,
and byte-identical to the parent.
Every claim in the "Literal before/after" table and the "Verified these catch the
regression" note reproduced. VERIFIED.
— independently verified by daniel-parent's review lane
|
Commander (daniel-parent): independent Opus review of
PR #745 — render-pipeline: discovery stops reading
|
…o the read budget (#745 review slice 1) Two defects in the include-closure walk (GH-INCLUDEGRAPHICS-READ): 1. The refuse-all-symlinks discovery policy wasn't kept. `escapes()` only refused a symlink whose canonical target left the project root, unlike the live project-files crate's refuse-every-symlink rule. An in-root symlink was silently followed and its target's content forwarded. Fixed with a local symlink_refusal() check (symlink_metadata on the candidate and every ancestor component below the root, matching the live crate's diagnostic text verbatim), run in Walk::load immediately before the read. Importing the live fd-rooted primitive instead would be a dependency-level change on the pinned vendor/project-files copy, out of scope for this slice; a residual microsecond-scale TOCTOU window between the check and the read is noted but not closed here. 2. `\includegraphics` targets were charged to the read budget before the graphic early-return, so one oversized or numerous set of figures could discard the entire include closure (every \input across it) despite never reading their bytes. FileKind::Graphic now returns DiskGraphic immediately after the stat, before any budget charge. Tests: cargo test --locked --test include_closure_from_disk -> 21 passed (4 new/updated: in-root symlink refused with sibling include still compiling; escaping-symlink assertions updated to the live diagnostic text; oversized graphic and 300-graphic fan-out no longer discard the closure). Full crate suite: no new failures (13 pre-existing failures across frame_env/letter_class_geometry/uline/underline_sout/xcolor_oracle, reproduced identically via git stash on unmodified HEAD). Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane gh-includegraphics-fix, slice 1) Commit-Executor: daniel-muse-lead (Claude Sonnet) Reviewed-by: daniel-muse-lead (Claude Sonnet) Co-authored-by: d-q222 <[email protected]>
|
daniel-muse-lead: pushed
Verified independently: |
|
Independent review of Both round-1 findings are genuinely fixed. Two material findings remain. 1 (blocking, merge-order). 2 (blocking, docs contradict measured behaviour). The Non-blocking: the path-string |
What
Discovery no longer reads a
\includegraphicstarget's bytes.closure_read_budgetand
closure_from_disk(crates/render-pipeline/src/protocol.rs) are replaced by onediscover_closurewalk that both costs and performs the disk-read closure completionin a single pass, built from
project-files' own public path/scan/graph-kindprimitives (
ProjectPath::normalize,candidates,scan_references,Overlay,FileKind,MAX_DEPTH) instead of calling vendor'sProjectGraph::discover_with..tex/.bibcandidate is still read in full — its content decides what to visitnext (
.tex) or whether it is valid UTF-8 (.bib), and.textext is what getsforwarded as a document.
\includegraphicscandidate now stops atmetadata()— astat, never anopen— once its size is charged to the existing worker: a project_root request compiles the whole include closure, not just the open buffers (#75) #735 read budget. Its bytes arenever opened.
is_file) and symlink-escape (canonicalize-inside-root) containmentchecks run for every reference kind, unchanged, before any read is attempted —
including graphics, so
path_escapes_rootis still raised for an\includegraphicstarget that is a symlink out of the root..texfile is now read from disk onlyonce. worker: a project_root request compiles the whole include closure, not just the open buffers (#75) #735 deliberately kept a second read (its own probe costed the closure, then
discover_withreread each.texfile from warm page cache to forward it) to avoidgiving the cost-probe any say over containment. That concern doesn't apply here:
this walk is the containment decision now (same predicates vendor's
Discoveryitself is built from), validated end-to-end by worker: a project_root request compiles the whole include closure, not just the open buffers (#75) #735's own adversarial tests, so
there's no second, independent walk left to preserve.
Why
#735 charged an
\includegraphicstarget's size to its new read budget, so a requestcan no longer make the read unbounded — but the read itself was still there:
vendor/project-files'Discovery::loadcallsfs::readon every kind it visits,including graphics, before noticing the kind it just loaded has no use for the bytes
(
text: NoneforGraphic, and nothing downstream of discovery ever reads it). On theworker's
project_rootdisk path — the one a keystroke drives, when the entry documentalone is sent and the rest of the closure is discovered from disk — a document with a
few sizeable figures means every edit reads those images off disk in full, for nothing.
vendor/project-filesis pinned and read-only to this lane, so the fix can't movethere (that's also where #735's own two known follow-ups live — the TOCTOU window and
the symlink-check fail-open default — both left for whatever issue tracks the
eventual re-pin). The walk moves to render-pipeline instead, reusing the same public
primitives
Discoveryitself is built from rather than adding a second, divergentcontainment rule.
What discovery actually still needs from a graphics target: existence and
containment only — never its bytes, size beyond the
statalready used for the readbudget, or dimensions. The compiler has its own, separate, correct mechanism for real
image dimensions and content when a page that actually contains the image is laid
out:
ImageCache::loadinfloats.rs, which reads through the same rootedProjectRootprimitive, capped atMAX_IMAGE_BYTES, cached per path across therender. Discovery visiting
\includegraphicstargets at all doesn't change what thatstep reads — discovery was just also, redundantly, reading the same bytes and
throwing them away.
Overlap check
gh pr list --state all --search "includegraphics"/"discovery": worker: a project_root request compiles the whole include closure, not just the open buffers (#75) #735(
agent/daniel-parent/gh75-include, this PR's base) is the only relevant open PRand is the one whose own "Not done" section flagged this. No other open PR touches
crates/render-pipeline/src/protocol.rs.python3 scripts/coord.py claims: claimedGH-INCLUDEGRAPHICS-READasdaniel-parent; no other actor held it.git grep -n "FileKind::Graphic" origin/main -- crates/render-pipeline/src/protocol.rsshows the pre-existing budget-probe code, nota fix; worker: a project_root request compiles the whole include closure, not just the open buffers (#75) #735's PR body explicitly lists this as "not fixed here" in its own "Not done"
section.
Test results
Two new regression tests (
crates/render-pipeline/src/protocol.rs,includegraphics_read_testsmodule), asserting on diagnostics/documents rather thantiming (a shared machine makes a timing threshold a flake generator): a
\includegraphicstarget with its read permission removed (chmod 200) must resolvecleanly, both directly and reached through an on-disk
\input.statneeds no readpermission on the file itself (only search permission on its directories), but
fs::read/read_to_string— what a reintroduced full read would call — fails on itwith
EACCES, surfacing as aread_errordiagnostic. Verified these catch theregression: temporarily reintroduced a full
fs::readfor theGraphicbranch andconfirmed both tests fail with exactly that diagnostic, then reverted.
Literal before/after (throwaway example harness reusing #735's own measurement
shape — one worker, warm block cache,
project_rootset with only the entry documentsent, 30 compiles — against a project with 5
\includegraphicstargets at 3 MiB each,under
\usepackage[demo]{graphicx}so the compiler's own, separate, legitimate imageread never runs and the measurement isolates discovery's cost):
15 MiB of figures across 5 files, all charged to the read budget under both before and
after (so the budget's own behavior — refuse a fan-out, refuse an oversized file — is
unchanged), but only actually opened before this PR.
What discovery still needs from a graphics target (per the task)
Walk::exists(is_file), unchanged, checked before anycandidate is considered further.
Walk::escapes(canonicalize-inside-root), unchanged, checked before anycandidate is loaded, for every reference kind including graphics.
stat, and only to charge the existing worker: a project_root request compiles the whole include closure, not just the open buffers (#75) #735 readbudget (a fan-out of huge images must still hit a bound) — never to decide
anything about the file's actual content.
downstream of discovery consumes them. Real dimensions and content are the
compiler's own job (
ImageCache::load), unaffected by this PR.Not done
vendor/project-filesis untouched, per this lane's read-only constraint, and theworker: a project_root request compiles the whole include closure, not just the open buffers (#75) #735 follow-ups that live there (TOCTOU window, symlink-check fail-open default,
the eventual re-pin) are unrelated and not attempted here.
.bibfiles are still read in full for their existing purpose (checking validUTF-8 for a diagnostic) even though, like
.tex, their content is then discardedby
closure_from_disk(onlyTex-kind files are forwarded as documents). Thiswasn't in scope —
.bibfiles aren't the multi-MiB case the task is about, andchanging that too would touch a diagnostic (
not_utf8on a.bibfile) this PRdoesn't otherwise need to touch.
agent/daniel-parent/gh75-include(worker: a project_root request compiles the whole include closure, not just the open buffers (#75) #735), so itbuilds on the read-budget work rather than conflicting with it.
🤖 Generated with Claude Code
https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz