feat(search): add calibrated retrieval and LangChain knowledge tool

Add exact and semantic evidence admission, measured calibration, bounded tool execution, and independent held-out evaluation preparation with regression coverage and operator guides. Real-provider quality gates remain pending.

Validation: 1300 provider-free tests passed; 3 Windows symlink skips. PostgreSQL acceptance not rerun because TEST_DATABASE_URL is unset.
This commit is contained in:
amirhosein mazaheri
2026-10-08 12:58:59 +03:30
parent 4d36f2ba35
commit fef381c41b
36 changed files with 6679 additions and 45 deletions
@@ -2,7 +2,7 @@
**Date:** 2026-10-07
**Target file:** `docs/plans/scoped-search-implementation-plan.md`
**Status:** Phase 1 first-result checkpoint implemented with provider-free PostgreSQL acceptance (2026-10-07); real-provider demonstration is pending ingestion database connectivity. Phases 2-4 remain pending. This four-phase plan replaces the earlier seven-phase proposal.
**Status:** Phase 1 first-result checkpoint implemented with provider-free PostgreSQL acceptance (2026-10-07); real-provider demonstration is pending ingestion database connectivity. Phase 2 technical retrieval correctness is implemented with provider-free unit/PostgreSQL acceptance (2026-10-08); real-model cutoff calibration remains pending explicit call authorization and approved original/document bindings. Phase 3 thin LangChain integration is technically accepted (2026-10-08): 162 affected unit and 103 affected PostgreSQL tests (including the runner shutdown repair), zero skips, with the declared search extra installed; see [Phase 3 handoff](../guides/scoped-search-phase3.md). Phase 4 historical preparatory regressions passed (2026-10-08): 1258 provider-free tests and 311 mandatory isolated PostgreSQL tests. Real quality acceptance remains not run; see [Phase 4 handoff](../guides/scoped-search-phase4.md). All six Phase 2 repairs passed 107 affected unit tests, 100 isolated PostgreSQL tests and 1193 provider-free regression tests; see [repair acceptance](../guides/scoped-search-phase2-repairs.md) and [Phase 2 results](../guides/scoped-search-phase2.md). This four-phase plan replaces the earlier seven-phase proposal.
## 1. Delivery Strategy
@@ -211,7 +211,7 @@ Use a thin `StructuredTool` adapter with an explicit argument schema and synchro
Default execution limits: four running calls, eight waiting calls, and a 75-second total deadline. SQL statement/lock timeouts are five/two seconds. Validate configuration; retain the explicit provider timeout policy.
Cancellation suppresses late results. A running blocking call holds its slot until it exits, even if its caller times out. Queue waiting consumes the deadline. Saturation and timeout return normalized technical errors. Do not create unbounded executor queues or internal provider retries.
Cancellation suppresses late results. A running blocking call holds its slot until it exits, even if its caller times out. Queue waiting consumes the deadline. Saturation and timeout return normalized technical errors. Do not create unbounded executor queues or internal provider retries. Runner closure returns nonretryable SEARCH_CANCELLED for queued/new calls and suppresses late running results. Repeated close is safe; submission and closure share a lock, submitted slots remain held until actual completion, and close joins work plus queued admission releases. Call blocking close outside active event loops and its worker pool. See [shutdown repair acceptance](../guides/scoped-search-phase3.md#shutdown-repair-acceptance-2026-10-08).
**Execution prompt**
@@ -332,3 +332,69 @@ Each phase reports its working command, observed output, actual checks, and rema
No latency or concurrent-user service-level target has been approved. Measure timings and enforce the stated execution bounds without claiming an unapproved performance target. Actual provider runs require explicit opt-in, compatible credentials, approved inputs, and a bounded operator call budget.
**Speed rule:** finish and demonstrate each usable checkpoint before expanding it. Optimize only a reproduced quality or latency problem. The MVP search milestone remains incomplete until the confirmed real-quality gate passes.
## Historical Phase 4 preparatory checkpoint (2026-10-08)
Final regression acceptance: 1258 provider-free passed, three existing Windows
symlink skips; 311 mandatory isolated PostgreSQL passed with zero skips.
pip check, tracked/new-file whitespace, Python/JSON inspection and byte
preservation of all three applied migrations passed. No production fixes.
[Original annotations](../calibration/phase4-cases.draft.json) contain 16 draft
answerable and six unsupported cases, with four explicitly empty unseen slots.
Original SHA-256 hashes are recorded; approval and actual document bindings
remain pending. This is not a completed/frozen 20-case set.
[Opt-in evaluator](../../tests/evaluate_search.py) calls canonical SearchService
with the frozen Phase 2 calibration, validates actual selection/bindings and
sources, enforces a maximum 26-query call budget, retains every failure/not-run
case, and applies the exact gate. Ten provider-free evaluation regressions pass.
[Status](../calibration/phase4-status.json) and
[operator guide](../guides/scoped-search-phase4.md) record actual results.
Real provider calls: zero. Phase 2 has no measured cutoff. Funded-profile/call
authorization, approved unseen originals, human review/DOCX inclusion and actual
ready document bindings remain missing. Phase 4 quality gate is not run;
retrieval readiness, generated-answer readiness and Live readiness are not claimed.
Cleanup confirmed zero disposable schemas and zero public tables. The created
Phase 4 test container and its anonymous volume were removed; existing containers
were preserved. Temporary original-page renderings were removed. Baseline
comparison found a concurrent change to scripts/live_voice_server.py, which this
evaluation did not edit. All other unrelated baseline files were unchanged.
## Phase 4 review repairs (2026-10-08)
The two review findings are fixed in the evaluation boundary. All 16 answerable
and six unsupported annotations were reconstructed from actual original PDFs,
infographic and DOCX, verified by strict UTF-8 read-back and synchronized with
the status snapshot. Zero unresolved populated annotations; all four unseen
slots remain empty. No original/review approvals or document bindings changed.
Question-only comparison against the supplied full Phase 2 report now rejects
exact and formatting-equivalent overlap and missing/malformed annotations before
settings/database/provider setup. NFKC, casefold, Persian/Arabic letter/digit
variants, Unicode whitespace/format controls and terminal punctuation are
comparison-only; original evidence and retrieval preprocessing remain unchanged.
Different questions may share original documents; semantic paraphrases are not
automatically detected. Safe errors identify the prerequisite/corrective action.
Current checks: **52 targeted tests passed**; full provider-free **1300 passed,
3 existing Windows symlink skips, 311 deselected**; pip check, tracked/new-file
whitespace, AST/JSON, persisted UTF-8 and all three migration byte comparisons
passed. Mandatory isolated PostgreSQL is **not run**: TEST_DATABASE_URL is not
configured. Earlier PostgreSQL counts remain historical evidence only.
[Operator guide](../guides/scoped-search-phase4.md) and
[status](../calibration/phase4-status.json) record commands and limitations.
Quality acceptance remains not_run with zero provider calls. Pending: measured
Phase 2 cutoff/funded-profile authorization and bounded budget, approved
originals/DOCX human review, actual eligible bindings and four unseen annotations.
No retrieval, generated-answer or Live readiness is claimed.
Final preservation inspection observed a change to scripts/live_voice_server.py
outside this repair. It was left untouched; all other unrelated baseline files
were unchanged. All three applied migrations match HEAD byte-for-byte. Local
documentation links and new-file inspection passed. Existing intentional Markdown
hard breaks were preserved. Temporary original-image inspection files were removed.