@@ -109,15 +109,34 @@ def pack(
109109 continue
110110
111111 prefix = "\n \n " if context else ""
112- header = self ._header (candidate , len (packed ) + 1 )
112+ ordinal = len (packed ) + 1
113+ header = self ._header (candidate , ordinal )
113114 base = f"{ context } { prefix } { header } \n "
114- if self ._count (base ) >= budget :
115- continue
115+ excerpt = ""
116+ truncated = False
117+ reason = ""
118+ if self ._count (base ) < budget :
119+ available = budget - self ._count (base )
120+ excerpt , truncated , reason = self ._excerpt (
121+ query , candidate , available
122+ )
116123
117- available = budget - self ._count (base )
118- excerpt , truncated , reason = self ._excerpt (
119- query , candidate , available
120- )
124+ # Keep the established single-pass behavior for ordinary sources.
125+ # Only retry against the cheaper ordinal-only header when the selected
126+ # excerpt already starts with the exact displayed title (or the titled
127+ # header left no room). This removes prompt duplication without deleting
128+ # evidence or weakening the stable ``[n]`` citation bridge.
129+ if not excerpt or _starts_with_title (excerpt , record .title ):
130+ compact_base = (
131+ f"{ context } { prefix } "
132+ f"{ self ._header (candidate , ordinal , include_title = False )} \n "
133+ )
134+ if self ._count (compact_base ) < budget :
135+ compact_available = budget - self ._count (compact_base )
136+ compact = self ._excerpt (query , candidate , compact_available )
137+ if compact [0 ] and _starts_with_title (compact [0 ], record .title ):
138+ base = compact_base
139+ excerpt , truncated , reason = compact
121140 if not excerpt :
122141 continue
123142 proposed = f"{ base } { excerpt } "
@@ -370,15 +389,21 @@ def semantically_safe(excerpt: str) -> bool:
370389 high = middle - 1
371390 return best
372391
373- def _header (self , candidate : Candidate , ordinal : int ) -> str :
392+ def _header (
393+ self ,
394+ candidate : Candidate ,
395+ ordinal : int ,
396+ * ,
397+ include_title : bool = True ,
398+ ) -> str :
374399 record = candidate .record
375400 if record is None :
376401 return f"[{ ordinal } ]"
377402 # The compact source list carries identity/scope. Repeating ULIDs and
378403 # scope labels inside the context spends reader tokens without adding
379404 # evidence; the ordinal is the citation bridge.
380405 header = f"[{ ordinal } ]"
381- if record .title :
406+ if include_title and record .title :
382407 title = " " .join (record .title .split ())[:120 ]
383408 header += f" { title } "
384409 return header
@@ -415,6 +440,18 @@ def _terms(text: str) -> set[str]:
415440 return {match .group (0 ).casefold () for match in _WORD_RE .finditer (text or "" )}
416441
417442
443+ def _starts_with_title (excerpt : str , title : str ) -> bool :
444+ """Whether an excerpt already opens with the exact displayed title text."""
445+ displayed_title = " " .join ((title or "" ).split ())[:120 ].casefold ()
446+ normalized_excerpt = " " .join ((excerpt or "" ).split ()).casefold ()
447+ if not displayed_title or not normalized_excerpt .startswith (displayed_title ):
448+ return False
449+ return (
450+ len (normalized_excerpt ) == len (displayed_title )
451+ or not normalized_excerpt [len (displayed_title )].isalnum ()
452+ )
453+
454+
418455def _family_representatives (
419456 candidates : list [Candidate ],
420457) -> tuple [list [Candidate ], int ]:
0 commit comments