Spaces:
Sleeping
Sleeping
Commit ·
5bd84ec
1
Parent(s): 918db3a
P0-R1.1-R2-R2 Shared Enumeration Scope Ownership (#72)
Browse files- P0-R1.1-R2-R2 Shared Enumeration Scope Ownership (5324899e8f4c5c7c9228213c194db2668b65b264)
Files changed (2)
3103 unmodified lines310431053106310731083109311031113112311331143103 unmodified linesif mode == "asq" else "final_quantitative_study_fact_contract_failed")raise RuntimeError(f"{prefix}:{section_title}") from excenum_checked, _enum_rows = agent.repair_and_validate_enumeration_contract(sealed_section, section_name=f"Final sealed section: {section_title}")if enum_checked.strip() != sealed_section.strip():raise RuntimeError(f"final_enumeration_contract_failed:{section_title}")# Validate the completed body while the Working Abstract is still in# place. Final front matter must not be generated until all body# sections, citation cleanup, and any Discussion rebalance are frozen.3103 unmodified lines310431053106310731083109311031113112311331143103 unmodified linesif mode == "asq" else "final_quantitative_study_fact_contract_failed")raise RuntimeError(f"{prefix}:{section_title}") from exc# Post-seal structural validation is pure validation. Never# invoke a mutating enumeration repair on frozen English text.agent.validate_enumeration_contract(sealed_section, section_name=f"Final sealed section: {section_title}")# Validate the completed body while the Working Abstract is still in# place. Final front matter must not be generated until all body# sections, citation cleanup, and any Discussion rebalance are frozen.
3361 unmodified lines3362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401286 unmodified lines368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463361 unmodified linespassages.extend(re.findall(pattern, str(value)))return [passage for passage in passages if len(passage.strip()) >= 8]@classmethoddef _repair_local_discourse_after_citation_removal(cls, original, audited, section_name=''):"""Repair only enumeration/ordinal framing after fail-closed sentence removal."""before = str(original or '')value = str(audited or '')if not value or value == before:return valueordinal_words = {'first':1, 'second':2, 'third':3, 'fourth':4, 'fifth':5, 'sixth':6}ordinal_matches = list(re.finditer(r'\bThe\s+(first|second|third|fourth|fifth|sixth)\b', value, flags=re.I))if len(ordinal_matches) < 2:return valuenums = [ordinal_words[m.group(1).lower()] for m in ordinal_matches]if nums == list(range(1, len(nums)+1)):return valuewords = {1:'first',2:'second',3:'third',4:'fourth',5:'fifth',6:'sixth'}index = 0def repl(match):nonlocal indexindex += 1return 'The ' + words[index]repaired = re.sub(r'\bThe\s+(?:first|second|third|fourth|fifth|sixth)\b', repl, value, flags=re.I)count_words = {'two':2,'three':3,'four':4,'five':5,'six':6}reverse = {2:'two',3:'three',4:'four',5:'five',6:'six'}def count_repl(match):old_n = count_words.get(match.group(1).lower(), 0)noun = match.group(2)if old_n >= max(nums) and len(nums) in reverse:return f"{reverse[len(nums)]} {noun}"return match.group(0)repaired = re.sub(r'\b(two|three|four|five|six)\s+(process\s+domains?|mechanisms?|dimensions?|stages?|patterns?|components?)\b',count_repl, repaired, flags=re.I,)return repaired@staticmethoddef _qual_source_family(value):286 unmodified linesraise RuntimeError(f"qualitative_study_fact_contract_unrepaired:{section_name}:{diagnostics[:3]}")return True@classmethoddef repair_and_validate_enumeration_contract(cls, text, section_name=""):"""Repair only high-confidence ordinal shifts tied to an explicit count."""value = str(text or "")number_map = {"two":2, "three":3, "four":4, "five":5, "six":6, "2":2, "3":3, "4":4, "5":5, "6":6}ordinal_map = {"first":1, "second":2, "third":3, "fourth":4, "fifth":5, "sixth":6}reverse = {1:"first",2:"second",3:"third",4:"fourth",5:"fifth",6:"sixth"}decl = re.compile(r"\b(two|three|four|five|six|[2-6])\s+(?:provisional\s+)?"r"(domains?|mechanisms?|dimensions?|stages?|patterns?|components?|themes?|processes?|conditions?)\b",flags=re.I,)offset = 0diagnostics = []for match in list(decl.finditer(value)):count = number_map[match.group(1).lower()]start = match.end() + offsetnext_heading = re.search(r"\n#{1,3}\s+", value[start:])scope_end = start + min(3500, next_heading.start() if next_heading else 3500)scope = value[start:scope_end]ordinals = list(re.finditer(r"\bThe\s+(first|second|third|fourth|fifth|sixth)\b", scope, flags=re.I))if len(ordinals) < count:continueselected = ordinals[:count]nums = [ordinal_map[m.group(1).lower()] for m in selected]expected = list(range(1, count + 1))if nums == expected:continueconsecutive = nums == list(range(nums[0], nums[0] + count))if not consecutive:raise RuntimeError(f"enumeration_contract_mismatch:{section_name}:declared={count}:ordinals={nums}")scope_chars = list(scope)for index, omatch in reversed(list(enumerate(selected, start=1))):old = omatch.group(1)new = reverse[index]if old[:1].isupper():new = new.capitalize()a, b = omatch.start(1), omatch.end(1)scope_chars[a:b] = list(new)repaired_scope = "".join(scope_chars)value = value[:start] + repaired_scope + value[scope_end:]delta = len(repaired_scope) - len(scope)offset += deltadiagnostics.append({"declared":count, "observed":nums, "repaired":expected})# Validate the same high-confidence patterns after repair.for match in decl.finditer(value):count = number_map[match.group(1).lower()]start = match.end()next_heading = re.search(r"\n#{1,3}\s+", value[start:])scope_end = start + min(3500, next_heading.start() if next_heading else 3500)nums = [ordinal_map[m.group(1).lower()] for m in list(re.finditer(r"\bThe\s+(first|second|third|fourth|fifth|sixth)\b", value[start:scope_end], flags=re.I))[:count]]if len(nums) >= count and nums != list(range(1, count + 1)):raise RuntimeError(f"enumeration_contract_unresolved:{section_name}:declared={count}:ordinals={nums}")return value, diagnostics@classmethod3361 unmodified lines3362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560286 unmodified lines38473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193361 unmodified linespassages.extend(re.findall(pattern, str(value)))return [passage for passage in passages if len(passage.strip()) >= 8]@staticmethoddef _enumeration_declaration_pattern():return re.compile(r"\b(?P<count>two|three|four|five|six|[2-6])\s+(?:provisional\s+)?"r"(?P<noun>process\s+domains?|domains?|mechanisms?|dimensions?|stages?|patterns?|"r"components?|themes?|processes?|conditions?)\b",flags=re.I,)@staticmethoddef _enumeration_ordinal_pattern():return re.compile(r"\bThe\s+(?P<ordinal>first|second|third|fourth|fifth|sixth)\b"r"(?:\s+(?P<noun>process\s+domain|domain|mechanism|dimension|stage|pattern|"r"component|theme|process|condition)s?\b)?",flags=re.I,)@staticmethoddef _enumeration_noun_family(value):token = re.sub(r"\s+", " ", str(value or "")).strip().casefold()if "domain" in token:return "domain"for family in ("mechanism", "dimension", "stage", "pattern", "component", "theme", "process", "condition"):if family in token:return familyreturn token@classmethoddef _enumeration_group_records(cls, text):"""Return declaration-owned local ordinal runs without crossing list resets.A declaration owns only the text before the next declaration/heading (andat most 3500 characters). Within that scope an ordinal reset/regressionstarts a different list, so ``first ... first ... second`` is not flattenedinto one synthetic enumeration. This is intentionally conservative."""value = str(text or "")decl = cls._enumeration_declaration_pattern()ordinal = cls._enumeration_ordinal_pattern()number_map = {"two":2, "three":3, "four":4, "five":5, "six":6,"2":2, "3":3, "4":4, "5":5, "6":6}ordinal_map = {"first":1, "second":2, "third":3, "fourth":4, "fifth":5, "sixth":6}declarations = list(decl.finditer(value))groups = []for index, match in enumerate(declarations):start = match.end()scope_end = min(len(value), start + 3500)if index + 1 < len(declarations):scope_end = min(scope_end, declarations[index + 1].start())heading = re.search(r"\n#{1,3}\s+", value[start:scope_end])if heading:scope_end = min(scope_end, start + heading.start())scope = value[start:scope_end]declared_family = cls._enumeration_noun_family(match.group("noun"))run = []previous_num = Nonefor omatch in ordinal.finditer(scope):num = ordinal_map[omatch.group("ordinal").casefold()]explicit_family = cls._enumeration_noun_family(omatch.group("noun")) if omatch.group("noun") else ""if explicit_family and explicit_family != declared_family:if run:break# The first visible ordinal explicitly belongs to another# enumeration family, so this declaration has no owned run.breakif previous_num is not None and num <= previous_num:# Restart/regression denotes a new/nested list. Never merge it# into the current declaration merely because it is nearby.breakrun.append({"match": omatch,"number": num,"word": omatch.group("ordinal"),"abs_start": start + omatch.start("ordinal"),"abs_end": start + omatch.end("ordinal"),})previous_num = numgroups.append({"declaration": match,"immutable": cls._enumeration_context_is_immutable(value, match.start()),"count": number_map[match.group("count").casefold()],"count_surface": match.group("count"),"noun": match.group("noun"),"noun_family": declared_family,"scope_start": start,"scope_end": scope_end,"run": run,"numbers": [row["number"] for row in run],})return groups@classmethoddef _enumeration_context_is_immutable(cls, text, position):"""Protect participant quotations and externally owned numbering."""value = str(text or "")pos = max(0, min(int(position or 0), len(value)))block_start = value.rfind("\n\n", 0, pos) + 2block_end = value.find("\n\n", pos)if block_end < 0:block_end = len(value)block = value[block_start:block_end].strip()if block.startswith(("\"", "“", ">")):return Truefor start, end, sentence in cls._sentence_spans(value):if start <= pos <= end:stripped = str(sentence or "").strip()if stripped.startswith(("\"", "“", "‘", ">")):return Trueif cls._has_external_literature_marker(sentence):return Trueif re.search(r"(?i)\b(?:prior|previous|earlier|existing)\s+(?:research|stud(?:y|ies)|literature)\b",str(sentence or ""),):return Truebreakreturn False@staticmethoddef _enumeration_count_surface(old_surface, new_count):words = {2:"two", 3:"three", 4:"four", 5:"five", 6:"six"}old = str(old_surface or "")if old.isdigit():return str(new_count)replacement = words.get(int(new_count), str(new_count))if old[:1].isupper():replacement = replacement.capitalize()return replacement@classmethoddef _repair_local_discourse_after_citation_removal(cls, original, audited, section_name=''):"""Repair only a declaration-owned enumeration damaged by sentence removal.The old implementation globally renumbered every ``The first/second/...``in the section, which could merge independent or nested lists. A count isnow reduced only when the corresponding pre-audit declaration had a fulllocal run and the post-audit run demonstrably lost one or more items."""before = str(original or '')value = str(audited or '')if not value or value == before:return valuebefore_groups = cls._enumeration_group_records(before)after_groups = cls._enumeration_group_records(value)replacements = []used_before = set()reverse = {1:"first", 2:"second", 3:"third", 4:"fourth", 5:"fifth", 6:"sixth"}for after in after_groups:if after.get("immutable"):continuecount = int(after["count"])run = list(after["run"])if not (2 <= len(run) < count):continuebefore_index = Nonebefore_group = Nonefor idx, candidate in enumerate(before_groups):if idx in used_before:continueif (not candidate.get("immutable") andcandidate["count"] == count andcandidate["noun_family"] == after["noun_family"] andlen(candidate["run"]) == count):before_index = idxbefore_group = candidatebreakif before_group is None:continue# Require a complete pre-audit local run. This proves that citation# removal, rather than an originally incomplete/nested list, caused# the missing ordinal item(s).if before_group["numbers"] != list(range(1, count + 1)):continueused_before.add(before_index)new_count = len(run)decl_match = after["declaration"]replacements.append((decl_match.start("count"), decl_match.end("count"),cls._enumeration_count_surface(after["count_surface"], new_count),))for new_num, row in enumerate(run, start=1):old_word = row["word"]new_word = reverse[new_num]if old_word[:1].isupper():new_word = new_word.capitalize()if new_word.casefold() != old_word.casefold():replacements.append((row["abs_start"], row["abs_end"], new_word))if not replacements:return valuefor start, end, replacement in sorted(replacements, reverse=True):value = value[:start] + replacement + value[end:]return value@staticmethoddef _qual_source_family(value):286 unmodified linesraise RuntimeError(f"qualitative_study_fact_contract_unrepaired:{section_name}:{diagnostics[:3]}")return True@classmethoddef validate_enumeration_contract(cls, text, section_name=""):"""Validate only unambiguous declaration-owned local enumeration runs.Incomplete, nested, restarted, or adjacent independent lists are not awhole-manuscript failure condition. They are deliberately outside thedeterministic repair boundary because ownership is ambiguous."""for group in cls._enumeration_group_records(text):if group.get("immutable"):continuecount = int(group["count"])nums = list(group["numbers"])if len(nums) != count:continueexpected = list(range(1, count + 1))if nums != expected:raise RuntimeError(f"enumeration_contract_unresolved:{section_name}:"f"owner={group['noun_family']}:declared={count}:ordinals={nums}")return True@classmethoddef repair_and_validate_enumeration_contract(cls, text, section_name=""):"""Repair only a complete, declaration-owned local ordinal run.A run may be renumbered by order only when exactly the declared numberof strictly increasing ordinal markers belongs to that declaration.Resets such as ``[1, 1, 2]`` mark a second/nested list and are thereforenon-blocking rather than flattened into one synthetic sequence."""value = str(text or "")reverse = {1:"first", 2:"second", 3:"third", 4:"fourth", 5:"fifth", 6:"sixth"}diagnostics = []replacements = []for group in cls._enumeration_group_records(value):if group.get("immutable"):continuecount = int(group["count"])run = list(group["run"])nums = list(group["numbers"])if len(run) != count:# Ambiguous/incomplete/multiple-list scope: preserve authorial# prose and let later reader-facing QA treat it as diagnostic.continueexpected = list(range(1, count + 1))if nums == expected:continue# _enumeration_group_records stops at any reset/regression, so a# complete run here is strictly increasing and safe to renumber by# local order (e.g. 3/4/5 -> 1/2/3).for new_num, row in enumerate(run, start=1):old_word = row["word"]new_word = reverse[new_num]if old_word[:1].isupper():new_word = new_word.capitalize()if new_word.casefold() != old_word.casefold():replacements.append((row["abs_start"], row["abs_end"], new_word))diagnostics.append({"declared": count, "owner": group["noun_family"],"observed": nums, "repaired": expected,})if replacements:for start, end, replacement in sorted(replacements, reverse=True):value = value[:start] + replacement + value[end:]cls.validate_enumeration_contract(value, section_name=section_name)return value, diagnostics@classmethod
core/generation_runner.py+3-3
core/pipeline_agent.py+245-72