"""Candidate rewrite for Builder entries (light STAR optimization).""" from __future__ import annotations from copy import deepcopy from typing import Any def _candidate_rewrite( agent: Any, profile: dict[str, Any], entry: dict[str, Any], section: str, *, instruction: str | None = None, ) -> dict[str, Any]: try: proposal = agent.expander.expand( deepcopy(entry), context={ "job_type": profile.get("job_type"), "target_position": profile.get("target_position"), "entry_type": section, "instruction": instruction, }, ) except Exception as exc: proposal = { "generation_source": "unavailable", "fallback_reason": type(exc).__name__.casefold()[:48], } original = str(entry.get("description") or "").strip() unavailable = proposal.get("generation_source") == "unavailable" optimized = "" if unavailable else str(proposal.get("optimized_description") or "").strip() or original # The expander owns objective coverage validation. Builder must not infer # semantic omissions through lexical comparison or append source text after # an LLM rewrite. uncovered = [str(item).strip() for item in proposal.get("uncovered_facts") or [] if str(item).strip()] return { "optimized_description": optimized, "changes": proposal.get("changes") or [], "source": proposal.get("source") or "ai_expanded", "uncovered_facts": list(dict.fromkeys(uncovered))[:8], "optimization_unavailable": unavailable, **({"fallback_reason": proposal["fallback_reason"]} if proposal.get("fallback_reason") else {}), **({"generation_source": proposal["generation_source"]} if proposal.get("generation_source") else {}), }