"""Parallel-slot one-shot READ/SKIP gate for selective-read v43. Runtime behavior is still one model request over the complete outline and one ``GATE[T]`` response. The ```` markers are input-side training anchors: the SFT objective can classify all focused entries in parallel during the same forward pass, while inference continues to generate one dense mask. """ try: from .protocol_v5 import TEMPLATE_FINAL, TEMPLATE_SKIM, TEMPLATE_UPDATE_READALL except ImportError: # Script execution with selective/ on PYTHONPATH. from protocol_v5 import TEMPLATE_FINAL, TEMPLATE_SKIM, TEMPLATE_UPDATE_READALL 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): """把纲要、问题和章节数渲染为并行稠密 Gate 提示。""" 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): """把 READ/SKIP 决策序列渲染为规范 Gate 目标。""" 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)