File size: 27,518 Bytes
0c48820
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
from __future__ import annotations

import math
import re
import unicodedata
from dataclasses import dataclass
from typing import Any

@dataclass(frozen=True)
class ContextAllocationConfig:
    strategy: str = "equal_split"
    min_chars_per_doc: int = 400
    max_chars_per_doc: int = 1800
    sentence_boundary: bool = True
    cache_version: str = "context_alloc_v1"

    @classmethod
    def from_config(cls, config: dict[str, Any] | None) -> "ContextAllocationConfig":
        config = config or {}
        strategy = str(config.get("strategy") or "equal_split").strip().lower()
        if strategy not in {"equal_split", "score_weighted", "full_sources"}:
            strategy = "equal_split"

        max_chars = max(1, int(config.get("max_chars_per_doc", 1800)))
        min_chars = max(0, int(config.get("min_chars_per_doc", 400)))
        min_chars = min(min_chars, max_chars)

        return cls(
            strategy=strategy,
            min_chars_per_doc=min_chars,
            max_chars_per_doc=max_chars,
            sentence_boundary=bool(config.get("sentence_boundary", True)),
            cache_version=str(config.get("cache_version") or "context_alloc_v1"),
        )

    def cache_fingerprint(self) -> dict[str, Any]:
        return {
            "strategy": self.strategy,
            "cache_version": self.cache_version,
        }


def build_context_for_prompt(
    retrieval_result: dict[str, Any],
    *,
    query: str | None = None,
    selected_citations: list[dict[str, Any]] | None = None,
    max_context_chars: int,
    allocation_config: ContextAllocationConfig | dict[str, Any] | None = None,
) -> str:
    """Build the bounded source context sent to the answer LLM.

    Retrieval can return several long parent sections. This function allocates
    a character budget per source, formats source headers, keeps query-focused
    table/section/snippet context, and truncates on readable boundaries so the
    prompt stays within ``max_context_chars`` without losing citation metadata.
    """
    config = (
        allocation_config
        if isinstance(allocation_config, ContextAllocationConfig)
        else ContextAllocationConfig.from_config(allocation_config)
    )
    max_context_chars = max(0, int(max_context_chars))
    if max_context_chars <= 0:
        return ""

    items = _filter_retrieved_items(
        retrieval_result.get("retrieved_items") or [],
        selected_citations=selected_citations or [],
    )
    related_items = _filter_related_items(
        retrieval_result.get("related_items") or [],
        primary_items=items,
    )
    if not items:
        return truncate_text(
            str(retrieval_result.get("context_for_llm") or ""),
            max_context_chars,
            sentence_boundary=config.sentence_boundary,
        )

    headers = [_source_header(index, item) for index, item in enumerate(items, start=1)]
    if config.strategy == "full_sources":
        return _build_full_sources_context(
            items,
            related_items=related_items,
            headers=headers,
            max_context_chars=max_context_chars,
            allocation_config=config,
        )

    separator_budget = max(0, len("\n\n---\n\n") * (len(items) - 1))
    header_budget = sum(len(header) for header in headers) + separator_budget
    content_budget = max(0, max_context_chars - header_budget)

    budgets = allocate_context_budget(
        items,
        total_budget=content_budget,
        min_chars_per_doc=config.min_chars_per_doc,
        max_chars_per_doc=config.max_chars_per_doc,
        strategy=config.strategy,
    )

    blocks: list[str] = []
    for header, item, budget in zip(headers, items, budgets, strict=False):
        content = str(item.get("content") or "").strip()
        content = prepare_content_for_prompt(
            content,
            item=item,
            query=query or str(retrieval_result.get("query") or ""),
            budget=budget,
            sentence_boundary=config.sentence_boundary,
        )
        truncated_content = truncate_text(
            content,
            budget,
            sentence_boundary=config.sentence_boundary,
        )
        blocks.append(f"{header}{truncated_content}")

    return truncate_text(
        "\n\n---\n\n".join(blocks),
        max_context_chars,
        sentence_boundary=config.sentence_boundary,
    )


def _build_full_sources_context(
    items: list[dict[str, Any]],
    *,
    related_items: list[dict[str, Any]] | None = None,
    headers: list[str],
    max_context_chars: int,
    allocation_config: ContextAllocationConfig,
) -> str:
    separator = "\n\n---\n\n"
    raw_contents = [
        _strip_generated_focus_sections(str(item.get("content") or "").strip())
        for item in items
    ]
    per_source_cap = max(1, int(allocation_config.max_chars_per_doc))
    capped_contents = [
        truncate_text(
            content,
            per_source_cap,
            sentence_boundary=allocation_config.sentence_boundary,
        )
        for content in raw_contents
    ]
    primary_blocks = [
        f"{header}{content}" for header, content in zip(headers, capped_contents, strict=False)
    ]
    sections: list[str] = []
    if primary_blocks:
        sections.append("PRIMARY SOURCES\n\n" + separator.join(primary_blocks))

    related = list(related_items or [])
    related_headers = [
        _source_header(index, item, source_role="related")
        for index, item in enumerate(related, start=1)
    ]
    related_contents = [
        truncate_text(
            _strip_generated_focus_sections(str(item.get("content") or "").strip()),
            per_source_cap,
            sentence_boundary=allocation_config.sentence_boundary,
        )
        for item in related
    ]
    related_blocks = [
        f"{header}{content}"
        for header, content in zip(
            related_headers,
            related_contents,
            strict=False,
        )
    ]
    if related_blocks:
        sections.append("RELATED SOURCES\n\n" + separator.join(related_blocks))

    full_context = "\n\n===\n\n".join(sections)
    if len(full_context) <= max_context_chars:
        return full_context

    all_items = [*items, *related]
    all_headers = [*headers, *related_headers]
    all_contents = [*capped_contents, *related_contents]
    separator_budget = max(0, len(separator) * max(len(all_items) - 1, 0))
    section_budget = len("PRIMARY SOURCES\n\n") + (
        len("\n\n===\n\nRELATED SOURCES\n\n") if related else 0
    )
    header_budget = sum(len(header) for header in all_headers) + separator_budget + section_budget
    content_budget = max(0, max_context_chars - header_budget)
    budgets = allocate_context_budget(
        all_items,
        total_budget=content_budget,
        min_chars_per_doc=allocation_config.min_chars_per_doc,
        max_chars_per_doc=allocation_config.max_chars_per_doc,
        strategy="score_weighted",
    )
    truncated_blocks: list[str] = []
    for header, content, budget in zip(all_headers, all_contents, budgets, strict=False):
        truncated_content = truncate_text(
            content,
            budget,
            sentence_boundary=allocation_config.sentence_boundary,
        )
        truncated_blocks.append(
            f"{header}{truncated_content}"
        )
    primary_count = len(items)
    truncated_sections: list[str] = []
    if primary_count:
        truncated_sections.append(
            "PRIMARY SOURCES\n\n"
            + separator.join(truncated_blocks[:primary_count])
        )
    if related:
        truncated_sections.append(
            "RELATED SOURCES\n\n"
            + separator.join(truncated_blocks[primary_count:])
        )
    return truncate_text(
        "\n\n===\n\n".join(truncated_sections),
        max_context_chars,
        sentence_boundary=allocation_config.sentence_boundary,
    )


def prepare_content_for_prompt(
    content: str,
    *,
    item: dict[str, Any] | None = None,
    query: str | None = None,
    budget: int = 1800,
    sentence_boundary: bool = True,
) -> str:
    """Prepare one retrieved source before prompt-level truncation.

    The context packer keeps table-like content, query-focused sections, and
    local snippets before final truncation. Citation binding stays with the
    retrieved item metadata from the retrieval layer.
    """

    content = _strip_generated_focus_sections((content or "").strip())
    if not content:
        return ""

    metadata = (item or {}).get("metadata", {}) or {}
    marker = "BẢNG/DANH SÁCH CHUẨN HÓA TỪ NGUỒN:"
    extracted_table_block = ""
    if marker in content:
        start_idx = content.find(marker)
        extracted_table_block = content[start_idx:].strip()
        content = content[:start_idx].strip()
        
        next_marker_idx = extracted_table_block.find("THÔNG TIN TRỌNG TÂM ĐÃ TÁCH TỪ NGUỒN:")
        if next_marker_idx != -1:
            extracted_table_block = extracted_table_block[:next_marker_idx].strip()

    if (
        not extracted_table_block
        and metadata.get("chunk_type") == "regulation"
        and len(content) <= budget
    ):
        return content

    query_terms = _query_terms(query or "")
    table_context = _normalized_table_context(content, metadata)
    section_context = _section_aware_context(content, query_terms)
    snippet_context = _snippet_aware_context(
        content,
        query_terms,
        max_chars=max(600, min(max(900, budget), 2200)),
        sentence_boundary=sentence_boundary,
    )

    blocks: list[str] = []
    if extracted_table_block:
        blocks.append(extracted_table_block)
    if table_context:
        blocks.append("NORMALIZED TABLE/LIST:\n" + table_context)
    if section_context and section_context not in table_context:
        blocks.append("RELATED SECTION:\n" + section_context)
    if snippet_context and snippet_context not in table_context + section_context:
        blocks.append("RELATED SNIPPET:\n" + snippet_context)

    if blocks:
        raw_context = truncate_text(
            content,
            max(800, min(max(1200, budget), 2600)),
            sentence_boundary=sentence_boundary,
        )
        if raw_context:
            blocks.append("SOURCE TEXT:\n" + raw_context)
        return "\n\n".join(blocks)

    return content


def _strip_generated_focus_sections(content: str) -> str:
    """Remove evidence/context labels that may have been appended in older cached excerpts."""

    if not content:
        return ""

    generated_markers = (
        "thong tin trong tam",
        "bang danh sach da",
        "bang dong da gom",
        "dieu kien truong hop moc so lieu",
        "van ban goc lien quan",
    )
    lines = content.splitlines()
    for index, line in enumerate(lines):
        normalized = _normalize_text(line)
        if any(marker in normalized for marker in generated_markers):
            if index == 0:
                return content.strip()
            return "\n".join(lines[:index]).strip()
    return content


def _query_terms(query: str) -> list[str]:
    normalized = _normalize_text(query)
    if not normalized:
        return []

    stopwords = {
        "la",
        "co",
        "cua",
        "cho",
        "toi",
        "minh",
        "ban",
        "nhung",
        "nao",
        "gi",
        "thi",
        "duoc",
        "khong",
        "bao",
        "nhieu",
        "may",
        "ve",
        "trong",
        "the",
        "nhu",
    }
    tokens = [
        token
        for token in re.findall(r"[a-z0-9]+", normalized)
        if len(token) >= 3 and token not in stopwords
    ]

    phrases: list[str] = []
    for size in (4, 3, 2):
        for index in range(0, max(0, len(tokens) - size + 1)):
            phrase = " ".join(tokens[index : index + size])
            if len(phrase) >= 8:
                phrases.append(phrase)

    seen: set[str] = set()
    result: list[str] = []
    for term in [*phrases, *tokens]:
        if term and term not in seen:
            seen.add(term)
            result.append(term)
    return result[:24]


def _normalized_table_context(content: str, metadata: dict[str, Any]) -> str:
    marker = "BẢNG/DANH SÁCH CHUẨN HÓA TỪ NGUỒN:"
    if marker in content:
        start_idx = content.find(marker)
        table_block = content[start_idx:]
        
        # Remove any other generated sections that might be appended after it (just in case)
        next_marker_idx = table_block.find("THÔNG TIN TRỌNG TÂM ĐÃ TÁCH TỪ NGUỒN:")
        if next_marker_idx != -1:
            table_block = table_block[:next_marker_idx]
            
        return table_block.strip()

    if not _looks_like_table(metadata, content):
        return ""

    rows = _compact_structured_lines(content)
    if not rows:
        return ""

    blocks = ["STRUCTURED SOURCE LINES:"]
    blocks.extend(f"- {row}" for row in rows[:12])
    return "\n".join(blocks).strip()

def _section_aware_context(content: str, query_terms: list[str]) -> str:
    if not query_terms:
        return ""

    lines = [line.strip() for line in content.splitlines() if line.strip()]
    if len(lines) < 3:
        return ""

    relevant_indices: list[int] = []
    for index, line in enumerate(lines):
        normalized_line = _normalize_text(line)
        if _term_score(normalized_line, query_terms) >= 2:
            relevant_indices.append(index)

    if not relevant_indices:
        return ""

    selected: list[str] = []
    for index in relevant_indices[:4]:
        start = _nearest_section_start(lines, index)
        end = _nearest_section_end(lines, index, start)
        selected.extend(lines[start : end + 1])

    return "\n".join(_dedupe_preserve_order(selected)).strip()


def _snippet_aware_context(
    content: str,
    query_terms: list[str],
    *,
    max_chars: int,
    sentence_boundary: bool,
) -> str:
    if not query_terms:
        return ""

    normalized_content = _normalize_text(content)
    best_index = -1
    best_score = 0
    best_term = ""
    for term in query_terms:
        start = 0
        while True:
            index = normalized_content.find(term, start)
            if index < 0:
                break
            window = normalized_content[max(0, index - 240) : index + 240]
            score = _term_score(window, query_terms)
            if score > best_score:
                best_score = score
                best_index = index
                best_term = term
            start = index + len(term)

    if best_index < 0 or best_score <= 0:
        return ""

    start = max(0, best_index - max_chars // 3)
    end = min(len(content), start + max_chars)
    boundary_start = _move_to_boundary(content, start, backward=True)
    if best_index - boundary_start <= max_chars // 2:
        start = boundary_start
    end = _move_to_boundary(content, end, backward=False)
    snippet = content[start:end].strip()
    truncated = truncate_text(snippet, max_chars, sentence_boundary=sentence_boundary)
    if (
        best_term
        and best_term in _normalize_text(snippet)
        and best_term not in _normalize_text(truncated)
    ):
        return truncate_text(snippet, max_chars, sentence_boundary=False)
    return truncated


def _find_sentence_with_terms(content: str, terms: list[str]) -> str:
    normalized_terms = [_normalize_text(term) for term in terms]
    candidates = re.split(r"(?<=[.!?])\s+|\n+", content)
    best = ""
    best_score = 0
    for candidate in candidates:
        normalized_candidate = _normalize_text(candidate)
        score = _term_score(normalized_candidate, normalized_terms)
        if score > best_score:
            best = candidate.strip()
            best_score = score
    return best if best_score > 0 else ""


def _looks_like_table(metadata: dict[str, Any], content: str) -> bool:
    if metadata.get("has_table") or metadata.get("chunk_type") == "table":
        return True
    return len(_compact_structured_lines(content)) >= 2


def _compact_structured_lines(content: str) -> list[str]:
    lines = [_collapse_space(line) for line in content.splitlines()]
    rows = [
        line
        for line in lines
        if line
        and (
            re.match(r"^(\d+\.|[a-z]\)|[-])\s+", line, flags=re.IGNORECASE)
            or len(re.findall(r"\b\d+(?:[,.]\d+)?\b", line)) >= 2
        )
    ]
    return _dedupe_preserve_order(rows)


def _nearest_section_start(lines: list[str], index: int) -> int:
    for cursor in range(index, -1, -1):
        if _is_section_marker(lines[cursor]):
            return cursor
    return max(0, index - 2)


def _nearest_section_end(lines: list[str], index: int, start: int) -> int:
    for cursor in range(index + 1, min(len(lines), start + 10)):
        if _is_section_marker(lines[cursor]):
            return max(index, cursor - 1)
    return min(len(lines) - 1, index + 4)


def _is_section_marker(line: str) -> bool:
    return bool(re.match(r"^(\d+\.|[a-z]\)|[IVX]+\.)\s+", line.strip(), flags=re.IGNORECASE))


def _term_score(text: str, terms: list[str]) -> int:
    score = 0
    for term in terms:
        if not term or term not in text:
            continue
        token_count = max(1, len(term.split()))
        score += token_count * token_count
    return score


def _normalize_text(text: str) -> str:
    text = unicodedata.normalize("NFD", text or "")
    text = "".join(char for char in text if unicodedata.category(char) != "Mn")
    text = text.lower().replace("đ", "d")
    return _collapse_space(text)


def _collapse_space(text: str) -> str:
    return re.sub(r"\s+", " ", text or "").strip()


def _move_to_boundary(text: str, index: int, *, backward: bool) -> int:
    index = max(0, min(len(text), index))
    boundaries = "\n.;:"
    if backward:
        candidates = [text.rfind(boundary, 0, index) for boundary in boundaries]
        best = max(candidates)
        return best + 1 if best >= 0 else index

    candidates = [text.find(boundary, index) for boundary in boundaries]
    positives = [candidate for candidate in candidates if candidate >= 0]
    return min(positives) + 1 if positives else index


def _dedupe_preserve_order(items: list[str]) -> list[str]:
    seen: set[str] = set()
    result: list[str] = []
    for item in items:
        key = _normalize_text(item)
        if key in seen:
            continue
        seen.add(key)
        result.append(item)
    return result


def allocate_context_budget(
    items: list[dict[str, Any]],
    *,
    total_budget: int,
    min_chars_per_doc: int,
    max_chars_per_doc: int,
    strategy: str = "equal_split",
) -> list[int]:
    count = len(items)
    if count == 0:
        return []

    total_budget = max(0, int(total_budget))
    max_chars = max(1, int(max_chars_per_doc))
    min_chars = min(max(0, int(min_chars_per_doc)), max_chars)
    if total_budget <= 0:
        return [0] * count

    if total_budget < count * min_chars:
        return _split_evenly(total_budget, count, cap=max_chars)

    budgets = [min_chars] * count
    remaining = total_budget - sum(budgets)
    caps = [max_chars - min_chars for _ in range(count)]

    if strategy == "score_weighted":
        weights = [_score_for_item(item, index) for index, item in enumerate(items)]
        if not any(weight > 0 for weight in weights):
            weights = [1.0] * count
    else:
        weights = [1.0] * count

    _distribute_remaining(budgets, caps, weights, remaining)
    return budgets


def truncate_text(text: str, budget: int, *, sentence_boundary: bool = True) -> str:
    text = (text or "").strip()
    budget = max(0, int(budget))
    if len(text) <= budget:
        return text
    if budget <= 0:
        return ""
    if budget <= 24:
        return text[:budget].rstrip()

    cut = text[:budget].rstrip()
    if not sentence_boundary:
        return _trim_to_word(cut)

    boundary = _last_sentence_boundary(cut)
    min_boundary = max(40, int(budget * 0.45))
    if boundary >= min_boundary:
        return cut[:boundary].rstrip()

    return _trim_to_word(cut)


def _filter_retrieved_items(
    items: list[Any],
    *,
    selected_citations: list[dict[str, Any]],
) -> list[dict[str, Any]]:
    selected_chunk_ids = {
        str(citation.get("chunk_id"))
        for citation in selected_citations
        if citation.get("chunk_id")
    }
    selected_titles = {
        str(citation.get("title") or "").strip().lower()
        for citation in selected_citations
        if citation.get("title")
    }

    filtered: list[dict[str, Any]] = []
    for item in items:
        if not isinstance(item, dict):
            continue
        metadata = item.get("metadata", {}) or {}
        chunk_id = str(item.get("chunk_id") or "")
        title = _item_title(item, metadata).lower()
        if selected_chunk_ids and chunk_id not in selected_chunk_ids:
            continue
        if not selected_chunk_ids and selected_titles and title not in selected_titles:
            continue
        filtered.append(item)
    return filtered


def _filter_related_items(
    items: list[Any],
    *,
    primary_items: list[dict[str, Any]],
) -> list[dict[str, Any]]:
    primary_ids = {
        str(item.get("chunk_id") or item.get("_id") or item.get("id") or "")
        for item in primary_items
    }
    filtered: list[dict[str, Any]] = []
    seen_ids: set[str] = set()
    for item in items:
        if not isinstance(item, dict):
            continue
        item_id = str(item.get("chunk_id") or item.get("_id") or item.get("id") or "")
        if not item_id or item_id in primary_ids or item_id in seen_ids:
            continue
        seen_ids.add(item_id)
        filtered.append(item)
    return filtered


def _legacy_source_header(index: int, item: dict[str, Any]) -> str:
    metadata = item.get("metadata", {}) or {}
    title = _item_title(item, metadata)
    
    matched_chunks = metadata.get("v7_matched_chunks", [])
    is_related_source = False
    source_seed_id = ""
    if matched_chunks:
        all_are_neighbors = all(chunk.get("_graph_depth") is not None for chunk in matched_chunks)
        if all_are_neighbors and len(matched_chunks) > 0:
            is_related_source = True
            source_seed_id = matched_chunks[0].get("_source_seed_id", "khác")
            
    if is_related_source:
        source_label = f"[NGUỒN LIÊN QUAN - được tìm thấy qua dẫn chiếu từ {source_seed_id}]"
    else:
        source_label = "[NGUỒN CHÍNH - khớp trực tiếp câu hỏi]"
        
    return "\n".join(
        [
            source_label,
            f"[Source {index}]",
            f"Title: {title}",
            f"Type: {metadata.get('chunk_type')}",
            f"Pages: {metadata.get('source_pages')}",
            "Content:",
            "",
        ]
    )


def _source_header(
    index: int,
    item: dict[str, Any],
    *,
    source_role: str = "primary",
) -> str:
    metadata = item.get("metadata", {}) or {}
    title = _item_title(item, metadata)

    if source_role == "related":
        source_label = f"[R{index}]"
        role_line = "Role: RELATED - graph supplement for context only"
        graph_lines = [
            f"Graph depth: {metadata.get('related_graph_depth')}",
            f"Linked from primary: {metadata.get('related_source_primary_id')}",
        ]
    else:
        source_label = f"[{index}]"
        role_line = "Role: PRIMARY - direct vector match for answer and citation"
        graph_lines = []

    return "\n".join(
        [
            source_label,
            role_line,
            *graph_lines,
            f"Title: {title}",
            f"Type: {metadata.get('chunk_type')}",
            f"Pages: {metadata.get('source_pages')}",
            "Content:",
            "",
        ]
    )


def _item_title(item: dict[str, Any], metadata: dict[str, Any]) -> str:
    return str(
        metadata.get("title")
        or metadata.get("form_name")
        or metadata.get("unit_name")
        or metadata.get("faculty_or_unit_name")
        or metadata.get("program_name")
        or metadata.get("faculty_name")
        or metadata.get("procedure_name")
        or metadata.get("rule_name")
        or item.get("chunk_id")
        or "Source"
    ).strip()


def _score_for_item(item: dict[str, Any], index: int) -> float:
    rerank = item.get("rerank")
    if isinstance(rerank, dict):
        value = rerank.get("final_score")
        if _is_positive_number(value):
            return float(value)

    value = item.get("score")
    if _is_positive_number(value):
        return float(value)

    return 1.0 / float(index + 1)


def _is_positive_number(value: Any) -> bool:
    try:
        number = float(value)
    except (TypeError, ValueError):
        return False
    return math.isfinite(number) and number > 0


def _split_evenly(total: int, count: int, *, cap: int) -> list[int]:
    if count <= 0:
        return []
    base = total // count
    remainder = total % count
    budgets = [min(base, cap) for _ in range(count)]
    for index in range(count):
        if remainder <= 0:
            break
        if budgets[index] < cap:
            budgets[index] += 1
            remainder -= 1
    return budgets


def _distribute_remaining(
    budgets: list[int],
    caps: list[int],
    weights: list[float],
    remaining: int,
) -> None:
    active = {index for index, cap in enumerate(caps) if cap > 0}
    remaining = max(0, int(remaining))

    while remaining > 0 and active:
        total_weight = sum(max(weights[index], 0.0) for index in active)
        if total_weight <= 0:
            effective_weights = {index: 1.0 for index in active}
        else:
            effective_weights = {index: max(weights[index], 0.0) for index in active}
        total_weight = sum(effective_weights.values()) or float(len(active))

        round_budget = remaining
        raw_shares = {
            index: round_budget * effective_weights[index] / total_weight
            for index in active
        }
        allocations = {
            index: min(caps[index], int(raw_shares[index])) for index in active
        }

        allocated_this_round = sum(allocations.values())
        if allocated_this_round < round_budget:
            leftovers = sorted(
                active,
                key=lambda index: (
                    raw_shares[index] - int(raw_shares[index]),
                    effective_weights[index],
                ),
                reverse=True,
            )
            for index in leftovers:
                if allocated_this_round >= round_budget:
                    break
                if allocations[index] >= caps[index]:
                    continue
                allocations[index] += 1
                allocated_this_round += 1

        if allocated_this_round <= 0:
            break

        for index, addition in allocations.items():
            budgets[index] += addition
            caps[index] -= addition
            remaining -= addition
            if caps[index] <= 0:
                active.remove(index)


def _last_sentence_boundary(text: str) -> int:
    matches = list(re.finditer(r"(?<=[\.\?!;:])\s+|\n{1,}", text))
    if not matches:
        return -1
    return matches[-1].end()


def _trim_to_word(text: str) -> str:
    match = re.search(r"\s+\S*$", text)
    if match and match.start() >= 24:
        return text[: match.start()].rstrip()
    return text.rstrip()