Skip to content

AI turns documents or topics into real, native PowerPoint decks—with native shapes, transitions and animations, data-backed charts and tables on demand, audio narration from speaker notes, and support for your own .pptx templates. · by Hugo He

fix(scripts): image_search paging continues the saved pool, batch advances shortlisted rows, positional query with --batch is an error

hugohe3c973a4a0d42a
diff --git a/skills/ppt-master/scripts/docs/image.md b/skills/ppt-master/scripts/docs/image.md
index 0d6adbb0..5c0afd44 100644
--- a/skills/ppt-master/scripts/docs/image.md
+++ b/skills/ppt-master/scripts/docs/image.md
@@ -292,7 +292,7 @@ Full review / escalation flow: [`image-searcher.md`](../../references/image-sear
Output:
-- `--save-candidates`: thumbnail-only `candidates/<stem>/candidates.json`, at most 8 provider previews by default, and `review_sheet.jpg`; no target image or provenance entry. `--candidate-page N` advances through the ranked pool; `--max-candidates 0` explicitly dumps all candidates for exceptional debugging
+- `--save-candidates`: thumbnail-only `candidates/<stem>/candidates.json`, at most 8 provider previews by default, and `review_sheet.jpg`; no target image or provenance entry. `--candidate-page N` advances through the ranked pool while keeping earlier pages' entries in `candidates.json`; `--max-candidates 0` explicitly dumps all candidates for exceptional debugging
- Best-only / `--promote`: one original saved to the specified output directory (auto-converts webp → jpg via Pillow when the filename extension demands)
- Best-only / `--promote`: `image_sources.json` manifest with full provenance (provider, license, license_tier, author, source URL, dimensions, attribution_text)
- Manifest is idempotent on `filename` and written atomically; damaged existing provenance blocks replacement
@@ -316,14 +316,14 @@ The full role-level reference (intent → query translation, on-slide attributio
| `--require-terms` | — | Repeatable identity gate; comma separates groups, `A|B` aliases |
| `--save-candidates` | off | Thumbnail mode: one ranked page of previews plus `review_sheet.jpg`, no original |
| `--max-candidates` | `8` | Page size; `0` = complete pool, debugging only |
-| `--candidate-page` | `1` | Ranked page; page 2 starts at rank 9 |
+| `--candidate-page` | `1` | Ranked page; page 2 starts at rank 9. Single-query continuation inherits the saved pool request; batch reruns every `Needs-Selection` row at that page |
| `--promote <candidate>` | — | Download exactly one selected original, enforce gates, write provenance |
| `--from-url <url>` | — | Manual replacement recorded as `license_tier: manual`; works without vision |
| `--manifest <path>` | `images/image_queries.json` | Override the manifest path |
### Batch runner and ranking
-Required per item: `filename`, `query`, `status`; optional: `query_variants`, `candidate_page`, `slide`, `purpose`, `orientation`, `provider`, `strict_no_attribution`, `min_width`, `min_height`, `required_terms`. The runner revalidates every `Sourced` row against its file, dimensions, and manifest entry (drift → `Failed`), then searches all `Pending` / `Failed` rows concurrently (default concurrency 3, `--concurrency N` or `IMAGE_SEARCH_CONCURRENCY`; `1` for strict pacing on rate-sensitive free providers). Thumbnail mode writes `Needs-Selection` with `candidate_page`, `candidate_count`, `candidate_total`, `has_more_candidates`, `next_candidate_page`, and the `review_sheet` path, creating no image or provenance; to see the next page for one row set its `candidate_page` to `next_candidate_page`, reset only that row to `Pending`, and rerun. Promoting with the same `--batch` manifest moves the row to `Sourced`. Provider failures stay retryable `Failed`; clean exhaustion becomes `Needs-Manual`; status is saved after each completion.
+Required per item: `filename`, `query`, `status`; optional: `query_variants`, `candidate_page`, `slide`, `purpose`, `orientation`, `provider`, `strict_no_attribution`, `min_width`, `min_height`, `required_terms`. The runner revalidates every `Sourced` row against its file, dimensions, and manifest entry (drift → `Failed`), then searches all `Pending` / `Failed` rows concurrently (default concurrency 3, `--concurrency N` or `IMAGE_SEARCH_CONCURRENCY`; `1` for strict pacing on rate-sensitive free providers). Thumbnail mode writes `Needs-Selection` with `candidate_page`, `candidate_count`, `candidate_total`, `has_more_candidates`, `next_candidate_page`, and the `review_sheet` path, creating no image or provenance; to see the next page for one row set its `candidate_page` to `next_candidate_page`, reset only that row to `Pending`, and rerun; `--candidate-page N` on the batch run advances every `Needs-Selection` row at once. A positional query is rejected in batch mode rather than ignored. Promoting with the same `--batch` manifest moves the row to `Sourced`. Provider failures stay retryable `Failed`; clean exhaustion becomes `Needs-Manual`; status is saved after each completion.
**Ranking** orders provider metadata, never pixels, and must not be tuned into a taste engine: hard-reject invalid licenses and zero relevance; in best-only mode reject any candidate missing a `required_terms` group; in thumbnail mode keep strict matches first and admit a near match only when exactly one group is missing and the finding query still has strong relevance (marked `identity_evidence: visual-verification-required`, never auto-promoted); then metadata-verified identity in the title outranks a URL-only match; concrete query tokens match whole ASCII tokens (`office` ≠ `officer`) and dominate generic words; orientation is a small penalty, no-attribution a small bonus, pixel count capped so a huge weak match cannot beat a smaller accurate one.
diff --git a/skills/ppt-master/scripts/image_search.py b/skills/ppt-master/scripts/image_search.py
index a7d4aec7..51d0ecf2 100644
--- a/skills/ppt-master/scripts/image_search.py
+++ b/skills/ppt-master/scripts/image_search.py
@@ -768,6 +768,56 @@ def _dedupe_ranked_candidates(
return deduped
+def _retained_pool_candidates(
+ meta_path: Path,
+ review_dir: Path,
+ request: ImageSearchRequest,
+ *,
+ current_ranks: set[int],
+) -> tuple[list[dict], list[int]]:
+ """Keep earlier pages of the same pool so any saved thumbnail stays promotable.
+
+ A pool belongs together while the target filename and query are unchanged;
+ a new query replaces it. Entries whose review file disappeared are dropped.
+ """
+ if not meta_path.is_file():
+ return [], []
+ try:
+ existing = json.loads(meta_path.read_text(encoding="utf-8"))
+ except (OSError, json.JSONDecodeError):
+ return [], []
+ if not isinstance(existing, dict):
+ return [], []
+ prior_request = existing.get("request")
+ if (
+ existing.get("target_filename") != request.filename
+ or not isinstance(prior_request, dict)
+ or str(prior_request.get("query") or "") != request.query
+ ):
+ return [], []
+ retained: list[dict] = []
+ for entry in existing.get("candidates") or []:
+ if not isinstance(entry, dict):
+ continue
+ rank = entry.get("rank")
+ review = entry.get("review")
+ if (
+ not isinstance(rank, int)
+ or isinstance(rank, bool)
+ or rank in current_ranks
+ or not isinstance(review, str)
+ or not (review_dir / Path(review).name).is_file()
+ ):
+ continue
+ retained.append(entry)
+ saved_pages = [
+ page
+ for page in existing.get("saved_pages") or [existing.get("candidate_page")]
+ if isinstance(page, int) and not isinstance(page, bool) and page > 0
+ ]
+ return retained, saved_pages
+
+
def _save_candidate_thumbnails(
ranked: list[tuple[float, str, AssetCandidate]],
output_dir: Path,
@@ -888,6 +938,14 @@ def _save_candidate_thumbnails(
except OSError:
pass
+ meta_path = cand_dir / "candidates.json"
+ retained, saved_pages = _retained_pool_candidates(
+ meta_path,
+ review_dir,
+ request,
+ current_ranks={entry["rank"] for entry in pool},
+ )
+ saved_pages = sorted(set(saved_pages) | {candidate_page})
meta = {
"schema_version": 3,
"candidate_storage": "thumbnail-only",
@@ -896,6 +954,7 @@ def _save_candidate_thumbnails(
"searched_at": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
"license_stage": license_stage,
"candidate_page": candidate_page,
+ "saved_pages": saved_pages,
"page_size": max_candidates,
"candidate_total": candidate_total,
"has_more_candidates": has_more_candidates,
@@ -909,9 +968,8 @@ def _save_candidate_thumbnails(
"min_width": request.min_width,
"min_height": request.min_height,
},
- "candidates": pool,
+ "candidates": sorted(retained + pool, key=lambda entry: entry["rank"]),
}
- meta_path = cand_dir / "candidates.json"
_write_json_atomic(meta_path, meta)
review_sheet: Optional[Path] = None
@@ -1372,6 +1430,27 @@ def write_sources_manifest(path: Path, item: dict) -> Path:
# ---------------------------------------------------------------------------
+def _load_saved_candidate_request(
+ output_dir: Path,
+ target_filename: str,
+) -> Optional[dict]:
+ """Return the ``request`` block of an existing candidate pool, if any."""
+ stem = Path(target_filename).stem
+ meta_path = output_dir / "candidates" / stem / "candidates.json"
+ if not meta_path.is_file():
+ return None
+ try:
+ meta = json.loads(meta_path.read_text(encoding="utf-8"))
+ except (OSError, json.JSONDecodeError):
+ return None
+ if not isinstance(meta, dict):
+ return None
+ if meta.get("target_filename") != target_filename:
+ return None
+ request = meta.get("request")
+ return request if isinstance(request, dict) else None
+
+
def promote_candidate(
output_dir: Path,
target_filename: str,
@@ -2081,6 +2160,25 @@ def run_search_manifest(
if repaired_sourced:
save_search_manifest(manifest_path, manifest)
+ if candidate_page > 1:
+ # An explicit page on the command line continues every shortlisted
+ # row; Needs-Selection is otherwise terminal, so without this the
+ # flag would silently do nothing.
+ advanced = 0
+ for item in items:
+ if item["status"] != SEARCH_STATUS_NEEDS_SELECTION:
+ continue
+ item["candidate_page"] = candidate_page
+ item["status"] = SEARCH_STATUS_PENDING
+ advanced += 1
+ if advanced:
+ print(
+ f"[Batch] --candidate-page {candidate_page}: {advanced} "
+ "Needs-Selection row(s) rerun at that page; set one row's "
+ "candidate_page in the manifest instead to advance only it."
+ )
+ save_search_manifest(manifest_path, manifest)
+
pending_idx = [
i for i, it in enumerate(items)
if it["status"] in SEARCH_RETRYABLE_STATUSES
@@ -2473,6 +2571,11 @@ def main(argv: Optional[list[str]] = None) -> int:
parser.error("--candidate-page must be a positive integer")
if args.max_candidates == 0 and args.candidate_page != 1:
parser.error("--candidate-page must be 1 when --max-candidates is 0")
+ if args.batch and args.query and not args.promote and not args.from_url:
+ parser.error(
+ "a positional query is not used in --batch mode; put the query in "
+ "the manifest row, or drop --batch for a single-query search"
+ )
output_dir = Path(args.output)
@@ -2559,16 +2662,68 @@ def main(argv: Optional[list[str]] = None) -> int:
if not args.filename:
parser.error("--filename is required in single-query mode")
+ saved_request = _load_saved_candidate_request(output_dir, args.filename)
+ if args.candidate_page > 1 and saved_request is None:
+ parser.error(
+ f"--candidate-page {args.candidate_page} continues an existing "
+ f"candidate pool, but none was saved for {args.filename!r}; run "
+ "page 1 with --save-candidates first"
+ )
+ if saved_request is not None and args.candidate_page > 1:
+ saved_query = str(saved_request.get("query") or "")
+ if saved_query and saved_query != args.query:
+ parser.error(
+ f"--candidate-page {args.candidate_page} must keep the pool's "
+ f"query {saved_query!r}; a changed query starts a new page-1 "
+ "pool instead"
+ )
+ required_terms = _parse_required_terms(args.require_terms)
+ query_variants = _parse_query_variants(args.query_variant)
+ orientation = "" if args.orientation == "any" else args.orientation
+ min_width = args.min_width
+ min_height = args.min_height
+ if saved_request is not None and args.candidate_page > 1:
+ inherited: list[str] = []
+ if not required_terms and saved_request.get("required_terms"):
+ required_terms = _parse_required_terms(saved_request["required_terms"])
+ inherited.append(f"required_terms={list(required_terms)}")
+ if not query_variants and saved_request.get("query_variants"):
+ query_variants = _parse_query_variants(saved_request["query_variants"])
+ inherited.append(f"query_variants={list(query_variants)}")
+ saved_orientation = str(saved_request.get("orientation") or "any")
+ if not orientation and saved_orientation != "any":
+ orientation = saved_orientation
+ inherited.append(f"orientation={orientation}")
+ for field in ("min_width", "min_height"):
+ saved_value = saved_request.get(field)
+ if (
+ isinstance(saved_value, int)
+ and not isinstance(saved_value, bool)
+ and saved_value > 0
+ and getattr(args, field) == parser.get_default(field)
+ and saved_value != getattr(args, field)
+ ):
+ if field == "min_width":
+ min_width = saved_value
+ else:
+ min_height = saved_value
+ inherited.append(f"{field}={saved_value}")
+ if inherited:
+ print(
+ " continuing the saved candidate pool; inherited "
+ + ", ".join(inherited),
+ file=sys.stderr,
+ )
request = ImageSearchRequest(
query=args.query,
purpose=args.purpose,
- orientation="" if args.orientation == "any" else args.orientation,
+ orientation=orientation,
filename=args.filename,
slide=args.slide,
- min_width=args.min_width,
- min_height=args.min_height,
- required_terms=_parse_required_terms(args.require_terms),
- query_variants=_parse_query_variants(args.query_variant),
+ min_width=min_width,
+ min_height=min_height,
+ required_terms=required_terms,
+ query_variants=query_variants,
)
_warn_weak_required_terms(request.required_terms)
diff --git a/skills/ppt-master/scripts/tests/test_image_search.py b/skills/ppt-master/scripts/tests/test_image_search.py
index e0b642ad..b0ece320 100644
--- a/skills/ppt-master/scripts/tests/test_image_search.py
+++ b/skills/ppt-master/scripts/tests/test_image_search.py
@@ -94,6 +94,78 @@ if __name__ == "__main__":
class CandidatePoolContinuationTests(unittest.TestCase):
+ def test_later_pages_keep_earlier_saved_thumbnails(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ cand_dir = Path(temp_dir) / "candidates" / "hero"
+ review_dir = cand_dir / "review"
+ review_dir.mkdir(parents=True)
+ (review_dir / "candidate_02.jpg").write_bytes(b"x")
+ meta_path = cand_dir / "candidates.json"
+ meta_path.write_text(json.dumps({
+ "target_filename": "hero.jpg",
+ "candidate_page": 1,
+ "request": {"query": "bronze mask"},
+ "candidates": [
+ {"rank": 1, "filename": "candidate_01.jpg", "review": "review/candidate_01.jpg"},
+ {"rank": 2, "filename": "candidate_02.jpg", "review": "review/candidate_02.jpg"},
+ ],
+ }), encoding="utf-8")
+ request = image_search.ImageSearchRequest(
+ query="bronze mask",
+ purpose="",
+ orientation="",
+ filename="hero.jpg",
+ slide="",
+ min_width=1,
+ min_height=1,
+ )
+ retained, pages = image_search._retained_pool_candidates(
+ meta_path,
+ review_dir,
+ request,
+ current_ranks={9, 10},
+ )
+ self.assertEqual([entry["rank"] for entry in retained], [2])
+ self.assertEqual(pages, [1])
+
+ changed = image_search.ImageSearchRequest(
+ query="jade cong",
+ purpose="",
+ orientation="",
+ filename="hero.jpg",
+ slide="",
+ min_width=1,
+ min_height=1,
+ )
+ self.assertEqual(
+ image_search._retained_pool_candidates(
+ meta_path, review_dir, changed, current_ranks=set()
+ ),
+ ([], []),
+ )
+
+ def test_batch_mode_rejects_a_positional_query(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ manifest = Path(temp_dir) / "image_queries.json"
+ manifest.write_text('{"items": []}', encoding="utf-8")
+ with redirect_stderr(io.StringIO()) as stderr:
+ with self.assertRaises(SystemExit):
+ image_search.main(["--batch", str(manifest), "bronze mask"])
+ self.assertIn("positional query", stderr.getvalue())
+
+ def test_page_continuation_requires_a_saved_pool(self) -> None:
+ with tempfile.TemporaryDirectory() as temp_dir:
+ with redirect_stderr(io.StringIO()) as stderr:
+ with self.assertRaises(SystemExit):
+ image_search.main([
+ "bronze mask",
+ "--filename", "hero.jpg",
+ "-o", temp_dir,
+ "--save-candidates",
+ "--candidate-page", "2",
+ ])
+ self.assertIn("none was saved", stderr.getvalue())
+
def test_multi_frame_camera_jpeg_keeps_its_primary_frame(self) -> None:
try:
from PIL import Image