Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from app.services.document_agent.budget import BudgetTracker, StageEnvelope
from app.services.document_agent.manifest import ToolContext, ToolResult
from app.services.document_agent.state import AgentBlackboard
from app.services.document_agent.structure.structure_anchoring import (
from app.services.document_agent.structure.anchoring_primitives import (
deserialize_skeleton_anchor,
serialize_skeleton_anchor,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Calibration orchestration across Phase-1 and structure Phase-2."""

from __future__ import annotations

from typing import Any

from app.services.document_agent.agents.calibration.procedure import (
finalize_calibration_result,
flat_toc_entries,
)
from app.services.document_agent.agents.calibration import service
from app.services.document_agent.manifest import ToolContext
from app.services.document_agent.structure.hierarchy_locator import TitleNode
from app.services.document_agent.structure.anchoring_primitives import (
SkeletonAnchor,
anchor_hierarchy_from_offset,
)


def anchor_hierarchy(
*,
nodes: list[TitleNode],
toc_hierarchies: list[dict[str, Any]] | None,
page_texts: dict[int, str],
body_pages: list[int],
page_count: int,
ctx: ToolContext | None,
) -> tuple[list[TitleNode], SkeletonAnchor]:
"""Run calibration Phase-1 and the production Phase-2 completion."""
phase1 = service.calibrate_offset(
nodes=nodes,
toc_hierarchies=toc_hierarchies,
ctx=ctx,
page_texts=page_texts,
page_count=page_count,
)
if phase1.status == "failed" and not phase1.regimes and phase1.offset is None:
return anchor_hierarchy_from_offset(
nodes=nodes,
offset_hint=None,
calibration_overrides={},
page_texts=page_texts,
body_pages=body_pages,
page_count=page_count,
ctx=ctx,
)
working, anchor, _finalized = finalize_calibration_result(
result=phase1,
entries=flat_toc_entries(toc_hierarchies),
toc_hierarchies=list(toc_hierarchies or []),
ctx=ctx,
page_count=page_count,
page_texts=page_texts,
body_pages=body_pages,
nodes=nodes,
)
return working, anchor
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,44 @@
normalize_page_kind,
parse_printed_page,
)
from app.services.document_agent.structure.structure_anchoring import (
from app.services.document_agent.structure.anchoring_primitives import (
SkeletonAnchor,
locate_null_page_parent_overrides,
offset_guided_anchoring,
prune_unanchored_toc_leaves,
serialize_skeleton_anchor,
)
from app.services.document_agent.structure import anchoring_primitives as _anchoring
from app.services.document_agent.structure.page_locate_agent import (
verify_section_page_choice,
)

# Re-export under prior names so existing imports keep working.
normalize_kind = normalize_page_kind


def offset_guided_anchoring(
*,
nodes: list[TitleNode],
offset: int,
ctx: ToolContext,
page_count: int,
calibration_overrides: dict[tuple[str, ...], TitleMatch],
) -> dict[tuple[str, ...], TitleMatch] | None:
"""Forward phase-2 anchoring while preserving the historical patch seam."""
original = _anchoring.verify_section_page_choice
_anchoring.verify_section_page_choice = verify_section_page_choice
try:
return _anchoring.offset_guided_anchoring(
nodes=nodes,
offset=offset,
ctx=ctx,
page_count=page_count,
calibration_overrides=calibration_overrides,
)
finally:
_anchoring.verify_section_page_choice = original


def pick_primary_offset(result: CalibrationResult) -> int | None:
"""Prefer decimal-regime candidate offset; else first regime with an offset."""
for regime in result.regimes:
Expand Down Expand Up @@ -298,7 +324,7 @@ def anchor_hierarchy_from_regimes(

if ctx is None:
# Offline: still apply deterministic printed+offset for this regime.
from app.services.document_agent.structure.structure_anchoring import (
from app.services.document_agent.structure.anchoring_primitives import (
bulk_offset_matches,
)

Expand Down
Loading
Loading