← all docs · north-os20 July 2026

fix: Title/entity-aware knowledge retrieval ranking — implementation plan

Status: In progress
Author: OpenAI Codex
Date: 2026-07-20
Repo: north-os
Related: design spec; NOS-183 Defect 2

Summary

Repair named-instrument retrieval at the shared @workspace/knowledge ranking seam. A compact title intent makes the existing title leg retrieve the named document, reserved title candidates and best-extent recovery keep its operative body in the pool, and a conditional stable boost makes explicit document identity outrank a same-topic different instrument. No agent-runtime fork, schema change, or checker relaxation.

Requirements trace

IDRequirementVerification
R1Named instrument’s own document outranks a same-topic different instrument.HFA fixture plus preprod rank probe.
R2Unnamed topical-query ordering does not change.Pure helper identity test and deterministic retrieval eval.
R3No exact-title match preserves semantic order and honest absence.Qualifier-miss regression test.
R4Shared retrieval path serves search_knowledge without tool-specific ranking.Knowledge and agent-runtime package tests.
R5No schema, migration, DB package, or eval-checker change.Changed-file review.

Key technical decisions


Implementation units

U1. Named-instrument intent and stable title ranking

Goal: Add a pure policy module that recognizes named legal instruments, builds a compact title query, and conditionally promotes matching source titles without dropping or duplicating hits.

Requirements: R1, R2, R3.

Dependencies: None.

Files:

Approach: Normalize punctuation and case, match the longest supported instrument phrase, derive meaningful surrounding qualifier tokens, and return a focused title query. The ranking helper performs a stable partition only when at least one candidate title satisfies the instrument plus qualifier intent. It returns a copy in original order for unnamed queries and named qualifier misses.

Patterns to follow: Generic stable-partition shape and invariants in packages/knowledge/src/retrieval/matter-boost.ts and packages/knowledge/test/retrieval/matter-boost.test.ts.

Test scenarios:

  1. HFA Bond Mortgage ranks above Environmental Indemnity Agreement while preserving order among mortgage versions and among nonmatches.
  2. Brooklyn Community Housing Services promissory-note and Kingsbrook Estates operating-agreement queries produce focused intents with their qualifiers.
  3. An unnamed limitation-of-liability topical query is an identity transformation.
  4. An “Acme” named instrument with no Acme title match does not promote another same-type document.
  5. Longest-specific phrase wins over a shorter nested type; punctuation and case do not change detection.
  6. No rows are lost or duplicated.

Verification: Pure tests establish deterministic query classification and ordering invariants.

U2. Candidate recall and shared retrieval integration

Goal: Feed title intent through the existing title leg, reserve its strongest candidates, recover their best body extents, and apply the final conditional ranking in searchKnowledgeWithAuth.

Requirements: R1, R2, R3, R4, R5.

Dependencies: U1.

Files:

Approach: Use the compact title query only when present, append deduplicated title hits after the fused cutoff, expand the revised bounded pool, and put named title source IDs first in best-extent recovery. Increase the shared cold/warm candidate cap by exactly the reserved title bound. Apply the pure title boost to the full post-rerank pool after matter provenance and before source diversity. Unnamed topical queries retain the prior title query and fused candidate construction.

Patterns to follow: Existing candidate constants, cache replay bound, TOC redirect deduplication, matter boost, and expanded-by-ID metadata projection in packages/knowledge/src/handlers/retrieval.ts.

Test scenarios:

  1. Named title hits outside the fused cutoff remain eligible for expansion and reranking.
  2. Duplicate chunk IDs from title and fused legs appear once.
  3. Named title sources receive best-extent recovery before generic top-fused sources.
  4. Cold and warm paths share the revised maximum pool bound.
  5. No recognized title intent leaves final ordering unchanged.

Verification: Package tests pass and the preprod HFA target enters and wins the final result set.

U3. Measurement and regression gates

Goal: Prove the defect is repaired without weakening broader retrieval quality.

Requirements: R1–R5.

Dependencies: U1, U2.

Files: No permanent evaluator file unless the existing deterministic suite needs a new observable contract case. The preprod probe remains outside the repository.

Approach: Re-run the identical three-query preprod rank probe at k=84, then run the existing deterministic retrieval evaluation and both affected package suites. Finish with repository typecheck and lint. Review the changed-file set to confirm no DB, migration, checker, or agent-runtime ranking changes.

Test scenarios:

  1. HFA Bond Mortgage moves from beyond rank 84 to rank 1.
  2. Named promissory note remains rank 1.
  3. Named operating agreement remains rank 1.
  4. Deterministic eval:retrieval meets its existing gate with no topical-query regression.

Verification: Record before/after ranks, package test outcomes, eval success rate, typecheck, and lint in the PR.


System-wide impact

Risks and mitigations

RiskMitigation
Same-type but wrong-entity source is over-promoted.Require qualifier overlap when a named qualifier is present; no-match is identity.
Title-only chunk wins without operative content.Prioritize its source in best-extent recovery and retain semantic reranking.
Candidate growth changes cache-hit behavior.Use one constant-derived maximum for cold construction and warm replay.
Fixed vocabulary becomes an accidental document taxonomy.Keep it limited to common instrument phrases and independent of persisted doc_type; expand only with measured misses.

Outcome

Ship only if the reproduced HFA query ranks an HFA Bond Mortgage first, the two already-correct named-instrument controls remain first, deterministic retrieval evaluation does not regress, all requested package tests pass, and root typecheck/lint pass.