[bug] Interrupted-run reuse accepts missing or corrupted extracted text

Author: xujiantop-cryptoCreated Sep 30, 2026Updated Sep 30, 2026

What happened

reuse_is_safe() returns (True, "workdir intact and all sources match the current inputs") when the extraction corpus has been deleted, emptied, or replaced with unrelated content. The original input file and its recorded SHA-256 are unchanged in all three cases.

Step 0 names this helper as the executable form of the interrupted-run recovery check. Its result can therefore authorize reuse of a missing or corrupted full_text.txt, even though later generation steps read that file.

This is a follow-up to the merged source-fingerprint protection in #224: the original-source check works, but it does not establish the integrity of the extraction output. I am not claiming that an actual host agent resumed and generated a wrong skill in this reproduction; the reproduced failure is the helper's incorrect safety verdict.

What you expected

Unchanged inputs are necessary for reuse, but the extracted corpus must also exist, be readable, and match the completed extraction. Missing or changed output should return False with an actionable reason.

Source document and environment

  • Format: synthetic UTF-8 TXT, one short chapter; no copyrighted material.
  • Extractor: built-in text parser, --mode text --install-missing no.
  • OS: Windows; Python 3.14.0.
  • Version: 1.4.0, default-branch commit c108d25b0cb58e1bdc361f3de02ed9f37075152f.

Minimal reproduction

Run this from the repository root. It creates its own temporary directory and does not delete any existing file:

python
import hashlib
import tempfile
from pathlib import Path
from book_to_skill.utils import reuse_is_safe

root = Path(tempfile.mkdtemp(prefix="bts-resume-"))
source = root / "book.txt"
source.write_text("Chapter 1: Synthetic\nVerify the source.\n", encoding="utf-8")
digest = hashlib.sha256(source.read_bytes()).hexdigest()
workdir = root / "work"
workdir.mkdir()
metadata = {
    "workdir": str(workdir),
    "output_text": str(workdir / "full_text.txt"),
    "extraction_mode": "text",
    "sources": [{"filename": source.name, "sha256": digest}],
}
print(reuse_is_safe([(source, digest)], metadata, "text"))
# Actual: (True, 'workdir intact and all sources match the current inputs')
# full_text.txt does not exist.

A separate end-to-end reproduction first ran the real CLI to obtain metadata.json and full_text.txt, then tested separate workdir copies. Observed results:

Extracted corpus Current helper verdict
Original, complete True
Missing True
Empty True
Replaced with unrelated text of exactly the recorded character length True

The same-length case matters: checking only chars would still accept a changed corpus.

Code evidence

Impact

Partial temp cleanup, interrupted file writes, or accidental replacement in an explicitly reused workdir can leave metadata and source hashes intact. A missing corpus causes a later read failure; a replaced corpus can supply unrelated input for skill generation. The failure is especially confusing because the helper explicitly reports that the workdir is intact.

Proposed correction

  1. Resolve the expected corpus under the recorded workdir and require a readable regular file. Reject output paths outside that workdir. Do not add arbitrary-path reads to this check.
  2. Record an additive output_text_sha256 for the actual bytes written by a completed extraction. Reuse must stream-hash those bytes and compare the digest, alongside the existing source checks.
  3. Preserve the current ordering, which already writes metadata after the corpus. Atomic replacement of the corpus/metadata can additionally reduce partial-state windows, but is not a substitute for verifying the digest.
  4. Old metadata without an output digest cannot prove output integrity. Return a named legacy fallback and re-extract, consistent with the existing handling of missing source fingerprints.
  5. Update the existing recovery sentence rather than growing the always-loaded spec with a separate workflow.

No new runtime dependency or change to normal extraction is required. The extra work is one sequential pass over the extracted output when recording/verifying its hash. This is integrity checking for accidental incomplete/stale state, not an authenticity guarantee against an attacker who can rewrite both corpus and metadata.

Acceptance criteria

  • A complete matching corpus and matching input hashes still resume.
  • Missing, empty, unreadable, truncated and same-length replaced output return False with distinct useful reasons.
  • Output paths outside the workdir are rejected without reading those paths.
  • Legacy metadata follows a documented fresh-extraction path.
  • Existing input-change, source-set and mode mismatch regressions retain their behavior.
  • Tests use synthetic documents, including a multi-chunk output digest case; run focused recovery tests and the repository's required checks before merging a fix.

Duplicate check

Reviewed current and historical issue/PR titles and bodies, including #224. No separate issue or open PR covering extracted-output integrity was found at the audited revision.

Source: virgiliojr94/book-to-skill