# Reservation Vendor Comments Anchor Implementation Plan > **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. **Goal:** 已标注 Reservation ID 专区的订单模板,跨页在 `Vendor Comments` 与首次 `Terms and Condition(s)` 之间确定性读取 Reservation,而不依赖固定页码或坐标。 **Architecture:** `reservation_id_region` 继续作为该能力的显式启用开关,保留原几何数据以兼容历史草稿/画布,但运行时不再消费其 `page_no/region`。运行时为已启用模板补齐全部 PDF 页的坐标证据,以视觉阅读顺序定位起止锚点,截取边界内文本,再复用现有 Reservation 候选解析、行号归属和冲突 Review 契约。 **Tech Stack:** Flask、Python、PDF/OCR 坐标证据、pytest。 ## Global Constraints - 只对包含 `definition.reservation_id_region` 的已选中模板启用;未标注模板不得全文扫描或改变结果。 - 起点为首次 `Vendor Comments`;终点为其后首次 `Terms and Condition` 或 `Terms and Conditions`,匹配忽略大小写、冒号和复数差异。 - 区块允许跨页;不存在起点、终点或二者顺序异常时不猜值,稳定进入 Review。 - Reservation 仍仅接受带 `Reservation` 标签的 5 位或 10 位纯数字;行号、冲突和越界处理复用 `reservation_region.py`。 - 原 PDF 不可变;页坐标只通过 `ensure_on_demand_coordinate_evidence(..., page_nos=None)` 取得;不新增依赖、表结构或 Dify 改动。 --- ### Task 1: Vendor Comments 区块提取契约 **Files:** - Modify: `backend/app/services/shipping_notice/reservation_region.py` - Test: `backend/tests/test_shipping_notice_reservation_id.py` **Interfaces:** - Produces `extract_reservation_from_vendor_comments_pages(pages: list[tuple[int, list[dict[str, Any]]]]) -> tuple[list[dict[str, object]], str | None]`。 - `str | None` 是 `None`、`vendor_comments_anchor_missing` 或 `terms_and_conditions_anchor_missing`;调用方据此决定 Review。 - [ ] **Step 1: Write failing tests** ```python def test_vendor_comments_window_crosses_pages_and_stops_at_first_terms_heading(): candidates, reason = extract_reservation_from_vendor_comments_pages([ (1, _glyph_lines('Vendor Comments:', 'Reservation#33008')), (2, _glyph_lines('Reservation#3000098851 for line#1', 'Terms and Conditions:', 'Reservation#99999')), ]) assert reason is None assert [item['value'] for item in candidates] == ['33008', '3000098851'] ``` - [ ] **Step 2: Verify RED** Run: `cd backend; .\\.venv\\Scripts\\python.exe -m pytest -q tests/test_shipping_notice_reservation_id.py -k vendor_comments` Expected: FAIL because the extraction function is not defined. - [ ] **Step 3: Implement minimal extractor** Implement a pure helper in `reservation_region.py` that renders each page through existing `visual_text_lines`, scans all page lines in page-number order, begins after the first normalized `Vendor Comments` heading, ends before the first later normalized `Terms and Condition(s)` heading, and passes only joined window lines to `extract_reservation_from_region_text`. Do not read coordinates, assets, or database state in this helper. - [ ] **Step 4: Verify GREEN** Run: `cd backend; .\\.venv\\Scripts\\python.exe -m pytest -q tests/test_shipping_notice_reservation_id.py` Expected: PASS, including existing ID format, line mapping and conflict tests. ### Task 2: Production routing and fail-closed boundary result **Files:** - Modify: `backend/app/services/shipping_notice/order_extraction_workflow.py` - Test: `backend/tests/test_order_extraction_pdf_po_split.py` **Interfaces:** - Replaces `_reservation_region_candidates(raw_profile, layout)` fixed-page geometry reading with a result containing `candidates` and optional `reason`. - Existing `_build_approved_profile_order_result(..., reservation_region_candidates=...)` remains the single application point for values. - [ ] **Step 1: Write failing integration tests** ```python def test_enabled_reservation_region_reads_vendor_comments_across_pages(): result = _reservation_region_candidates(raw_profile=_profile_with_region(), layout=_two_page_layout()) assert result['reason'] is None assert result['candidates'][0]['value'] == '33008' def test_profile_without_reservation_region_does_not_scan_vendor_comments(): result = _reservation_region_candidates(raw_profile={'definition': {}}, layout=_two_page_layout()) assert result == {'candidates': [], 'reason': None} ``` - [ ] **Step 2: Verify RED** Run: `cd backend; .\\.venv\\Scripts\\python.exe -m pytest -q tests/test_order_extraction_pdf_po_split.py -k reservation_region` Expected: FAIL because the fixed-page reader returns a list and does not expose dynamic cross-page candidates. - [ ] **Step 3: Implement minimal routing change** For a Profile with `reservation_id_region`, call `ensure_on_demand_coordinate_evidence(document, layout, page_nos=None)` before matching/reading. In `_reservation_region_candidates`, order available `page_glyphs` numerically, call Task 1 helper, and return `{'candidates': ..., 'reason': ...}`. If a boundary reason exists, preserve it as `reservation_region_status='needs_review'` and `reservation_region_review_reasons=[reason]`; do not write Reservation fields and do not allow STP. No special behavior for profiles without the key. - [ ] **Step 4: Verify GREEN** Run: `cd backend; .\\.venv\\Scripts\\python.exe -m pytest -q tests/test_order_extraction_pdf_po_split.py tests/test_shipping_notice_reservation_id.py` Expected: PASS; dynamic scan works across page breaks, first Terms heading terminates the window, missing boundaries become Review, and unmarked templates do not scan. ### Task 3: Documentation and final verification **Files:** - Modify: `docs/features/shipping-notice/technical.md` - Modify: `docs/funclist.md` - [ ] **Step 1: Document the enabled-template behavior** Add one concise technical section: Reservation ID 专区 is an opt-in semantic parser; it scans from `Vendor Comments` to the first `Terms and Condition(s)` in reading order across pages; geometry remains annotation evidence only; missing/ambiguous boundaries and value conflicts require Review. State that unmarked templates are unchanged. - [ ] **Step 2: Update feature list** Add the Reservation 专区 dynamic-anchor behavior to the existing shipping order-template entry without creating a parallel feature. - [ ] **Step 3: Run verification** Run: `cd backend; .\\.venv\\Scripts\\python.exe -m pytest -q tests/test_shipping_notice_reservation_id.py tests/test_order_extraction_pdf_po_split.py` Run: `git diff --check` Expected: all targeted tests PASS and no whitespace errors. If frontend files are unchanged, no frontend build is required. ## Self-Review - The plan covers opt-in gating, cross-page reading, first-stop semantics, strict Reservation validation, Review failure paths, unmarked-template isolation, tests and documentation. - It creates no database migration, dependency, Dify configuration or profile data rewrite. - Helper and routing interfaces have explicit names and return types; all listed behavior is testable.