"""Inference-only v64 slot Gate bound to the rewrite1024 ReadTwice protocol. The Gate prompt and slot layout below are byte-identical to the historical ``selective.protocol_v43`` contract used by the best v64 Selective model. The SKIM, UPDATE, and FINAL prompts come from the current rewrite1024 protocol. """ from .protocol_readtwice import ( TEMPLATE_FINAL, TEMPLATE_SKIM, TEMPLATE_UPDATE_READTWICE, ) GATE_SLOT_MARKER = "" TEMPLATE_PARALLEL_SLOT_DENSE_GATE = """Below is a global outline of a long document, one gist per section: {total_sections} {outline} A user will ask the following question about the document: {prompt} Decide whether every focused section should be read in full during the second pass. Output READ when a section contains, or is likely to contain, factual evidence useful for answering the question, including direct answer evidence, entity identity, aliases, attributes, and multi-step bridge facts. Output SKIP when the section is unrelated or would not add useful evidence. Use the whole outline to interpret every focused entry. Missing a required bridge or answer section is more harmful than reading one additional plausible section. Do not enforce a fixed number or fraction of READ decisions; classify every section independently. All focused entries are packed below for one batched decision. The slot markers identify the entries to classify; do not reproduce them in the answer. {focus_sections} Return exactly {total_sections} decisions in outline order. Decision i is for Section i. The count in GATE brackets and the number of decisions must both match . Begin the only output line with GATE[{total_sections}]: and then emit exactly {total_sections} comma-and-space-separated decisions. Use uppercase READ or SKIP only. Output no explanation or additional line. """ def pack_focus_sections(outline, total_sections): """Turn a canonical outline into deterministic focused blocks and slots.""" entries = str(outline).splitlines() if len(entries) != int(total_sections): raise ValueError( f"outline has {len(entries)} entries, expected {total_sections}" ) blocks = [] for section_id, entry in enumerate(entries, 1): prefix = f"[Section {section_id}] " if not entry.startswith(prefix): raise ValueError( f"outline entry {section_id} does not start with {prefix!r}" ) blocks.append( "\n" "Classify this section:\n" f"\n{section_id}\n\n" "\n" f"{entry}\n" "\n" f"{GATE_SLOT_MARKER}\n" "" ) return "\n".join(blocks) def format_gate_prompt(outline, prompt, total_sections): """Render the historical v64 parallel-slot Gate prompt exactly.""" return TEMPLATE_PARALLEL_SLOT_DENSE_GATE.format( total_sections=int(total_sections), outline=str(outline), prompt=str(prompt), focus_sections=pack_focus_sections(outline, total_sections), ) def format_gate_target(decisions): """Render a canonical dense Gate target for protocol checks.""" values = [str(value).strip().upper() for value in decisions] if not values or any(value not in {"READ", "SKIP"} for value in values): raise ValueError("decisions must be a non-empty READ/SKIP sequence") return f"GATE[{len(values)}]: " + ", ".join(values) __all__ = [ "GATE_SLOT_MARKER", "TEMPLATE_FINAL", "TEMPLATE_PARALLEL_SLOT_DENSE_GATE", "TEMPLATE_SKIM", "TEMPLATE_UPDATE_READTWICE", "format_gate_prompt", "format_gate_target", "pack_focus_sections", ]