Local-first 🧠memory for Hermes Agent that lives in your Obsidian vault and remembers project context. Nightly 😴 dream passes turn repeat corrections into confirmed preferences with measurable confidence. Adapters ship for Claude Code, Codex, and OpenClaw, with an MCP server for anything else.
TypeScript
/**
* Rank and assemble: rank the fused candidate pool, then run the phases
* that can still drop or re-order a ranked row - traversal expansion, the
* relevance floor, diversity/relevance rerank, and the frontmatter-backed
* filters (status, property, degree, visibility, agent, scope).
*
* Owns the pool width, because every one of those phases is a reason to
* rank more rows than the caller asked for.
*/
import { statSync } from "node:fs";
import { join } from "node:path";
import { rerankByRelevance } from "../enrich.ts";
import { applyTraversal } from "../graph-phases.ts";
import { mmrRerank } from "../mmr.ts";
import { rankResults } from "../ranker.ts";
import { applyExactStateBarrier, type FrontmatterCache } from "../result-filters.ts";
import { semanticPoolSize } from "../semantic-phase.ts";
import { applyTemporalBridge } from "../temporal-bridge.ts";
import { applySessionPoolCap, resolveSessionPoolCap } from "./session-pool-cap.ts";
import { applyPoolFilters, resolvePoolFilters, type FilterContext } from "./pool-filters.ts";
import { RETRIEVAL_DEGRADATION, noteDegradation } from "../retrieval-trail.ts";
import type { RetrievalDegradationSink } from "../retrieval-trail.ts";
import type { CandidateSignals } from "./candidate-signals.ts";
import type { DuplicatePassageLocation } from "../search-result.ts";
import type { HydratedChunk, KeywordHit, SemanticHit, Store } from "../store.ts";
import type { ResolvedTimeRange } from "../time-range.ts";
import type { TemporalIntent } from "../temporal-intent.ts";
import type {
BrainSearchResult,
ResolvedSearchConfig,
SearchOptions,
SearchSessionFocus,
StructuredRecallQueryDocument,
WeightProfile,
} from "../types.ts";
/** Widening factors for the phases that need a pool wider than `limit`. */
const POOL_LIMIT_MULTIPLIER = 3;
const POOL_MIN_WIDTH = 30;
/**
* Joins the two halves of a duplicate-identity key. Never occurs in a
* sha256 hex digest, so the concatenation cannot be ambiguous.
*/
const DUPLICATE_KEY_SEPARATOR = ":";
/** Traversal is off at zero hops; MMR is off at a lambda of 1. */
const NO_TRAVERSAL_HOPS = 0;
const MMR_OFF_LAMBDA = 1;
/** The per-source-session pool cap is off at zero rows per session. */
const POOL_CAP_OFF = 0;
/**
* One row past the cap: enough to SEE truncation, never enough to change
* the answer. See {@link assembleRankedResults} for why the cap is
* measured rather than inferred.
*/
const CAP_PROBE_ROW = 1;
export interface AssemblyInput {
readonly store: Store;
readonly config: ResolvedSearchConfig;
readonly opts: SearchOptions;
readonly keywordHits: ReadonlyArray<KeywordHit>;
readonly semanticHits: ReadonlyArray<SemanticHit>;
readonly hydrated: ReadonlyMap<number, HydratedChunk>;
/**
* The content tier of the two-tier hydration cut (recall-quality, C5):
* hydrate `c.content` for the RANKED rows only - bounded by the rank
* cap, never the candidate pool - before the first content consumer
* (MMR) reads a row. The pool map arrives from `hydrateChunkRefs` with
* the `content` placeholder; absent callback, the pool map is assumed
* to carry real content (pre-cut shape, tests).
*/
readonly hydrateContents?: (chunkIds: ReadonlyArray<number>) => ReadonlyMap<number, string>;
readonly signals: CandidateSignals;
readonly relationalRankedChunkIds: ReadonlyArray<number>;
readonly weightProfile: WeightProfile | undefined;
readonly sessionFocus: SearchSessionFocus | null;
readonly semanticEnabled: boolean;
readonly structured: StructuredRecallQueryDocument | undefined;
readonly frontmatterCache: FrontmatterCache;
readonly timeRange: ResolvedTimeRange | null;
readonly temporalIntent: TemporalIntent | null;
readonly declaredEventTimeMs: (path: string) => number | null;
readonly limit: number;
/**
* The single instant the request resolved, forwarded to the ranker so
* the freshness layer scores against the same clock every other
* time-resolving decision in this call used. Without it the ranker
* falls back to its own `Date.now()` and a score - and therefore the
* exact-float first rung of the tie-break ladder - carries a clock
* reading taken at an arbitrary later point in the pipeline.
*/
readonly nowMs: number;
/**
* Typed degradation sink (evidence-at-the-boundary, C2), threaded like
* the pipeline's `warnings` array. This stage computed three recall
* narrowings and reported none of them: the cap that truncated the
* pool, the relevance floor that dropped ranked rows, and the scope
* filters that removed them - the last being why a query answered under
* an owner or session scope used to look exactly like an empty vault.
*/
readonly degraded: RetrievalDegradationSink;
}
interface Assembly {
readonly preVisibility: number;
readonly prePoolFilters: number;
/**
* Rows that survived the caller's own filters, BEFORE the session cap
* ran over them. The backfill's visibility signal and the trail's
* scope-drop count both read this pre-cap count: the cap is not a
* caller scope, and lumping its drops in here would report the cap as
* one.
*/
readonly filterVisible: number;
readonly visible: ReadonlyArray<BrainSearchResult>;
/** Whether the cap truncated the pool, as opposed to the pool running out. */
readonly capHit: boolean;
/** Rows the exact-duplicate merge folded into a higher-ranked row. */
readonly mergedAway: number;
/** Ranked rows the caller's relevance floor removed. */
readonly floorDropped: number;
/** Filter-passing rows the per-source-session cap dropped. */
readonly sessionCapped: number;
/** The widest group the cap judged this pass, for the re-widen width. */
readonly widestGroupRows: number;
}
/** The location record a merged-away duplicate leaves on its survivor. */
function duplicateLocation(r: BrainSearchResult): DuplicatePassageLocation {
return Object.freeze({
documentId: r.documentId,
chunkId: r.chunkId,
path: r.path,
title: r.title,
startLine: r.startLine,
endLine: r.endLine,
});
}
/**
* The duplicate-identity key of a hydrated candidate, or `null` when the
* row cannot state one.
*
* Both halves are sha256 hex written on every index run since schema v1:
* `chunks.content_hash` over the passage, `documents.content_hash` over
* the whole source file. The document half is load-bearing, not belt and
* braces - frontmatter is stripped before chunking, so two genuinely
* distinct records that share a body (the same rule with a different
* `freshness_trend`, the same prose with a different `authored_at`) hash
* their chunks identically while the ranker scores them differently on
* purpose. Requiring both means the merge fires only on a real copy of a
* file, where the merged-away row provably carries nothing but its path.
*/
function duplicateKey(hydrated: HydratedChunk | undefined): string | null {
if (hydrated?.contentHash === undefined || hydrated.documentHash === undefined) return null;
return `${hydrated.documentHash}${DUPLICATE_KEY_SEPARATOR}${hydrated.contentHash}`;
}
/**
* Fold byte-identical passages into their highest-ranked occurrence.
*
* Equal key means equal bytes at both the passage and the file level, so
* the second occurrence adds no information to the answer while consuming
* a slot in the caller's window; MMR only demoted it, and a demoted row is
* still a returned row. Every folded location is recorded on the survivor,
* so nothing is silently dropped.
*
* A near-duplicate hashes differently and is left entirely alone - the
* diversity rerank remains the only thing that judges similarity.
* A candidate whose hydrated row states no key (traversal expansions,
* hydrated through the representative-chunk read) is passed through
* untouched rather than treated as equal to its peers.
*
* `ranked` is score-sorted, so the survivor is always the best-ranked
* occurrence and the relative order of the surviving rows is preserved.
*/
function mergeExactDuplicates(
ranked: ReadonlyArray<BrainSearchResult>,
hydrated: ReadonlyMap<number, HydratedChunk>,
): { readonly results: BrainSearchResult[]; readonly mergedAway: number } {
const survivorAt = new Map<string, number>();
const foldedInto = new Map<number, DuplicatePassageLocation[]>();
const results: BrainSearchResult[] = [];
for (const r of ranked) {
const hash = duplicateKey(hydrated.get(r.chunkId));
if (hash === null) {
results.push(r);
continue;
}
const at = survivorAt.get(hash);
if (at === undefined) {
survivorAt.set(hash, results.length);
results.push(r);
continue;
}
const folded = foldedInto.get(at);
if (folded === undefined) foldedInto.set(at, [duplicateLocation(r)]);
else folded.push(duplicateLocation(r));
}
let mergedAway = 0;
for (const [at, locations] of foldedInto) {
mergedAway += locations.length;
results[at] = Object.freeze({
...results[at]!,
duplicates: Object.freeze(locations),
});
}
return { results, mergedAway };
}
export function assembleRankedResults(input: AssemblyInput): ReadonlyArray<BrainSearchResult> {
const { config, opts, limit } = input;
const filters = resolvePoolFilters(opts);
const filterContext: FilterContext = {
vault: config.vault,
store: input.store,
frontmatterCache: input.frontmatterCache,
};
// MMR and traversal both need a candidate pool wider than `limit`:
// MMR diversifies from it, and traversal seeds expansion from it (a
// narrow pool lets a high-parent expansion crowd a genuine but
// lower-ranked hit out of the final window). When both are disabled
// the pool collapses back to the historical rankLimit.
// The structured exclusion lane drops rows after this stage, so it
// widens the pool for the same reason a frontmatter filter does.
const hasStructuredExclusions = (input.structured?.lex.exclude.length ?? 0) > 0;
const mmrLambda = opts.mmrLambda ?? config.recall.mmrLambda;
const mmrActive = mmrLambda < MMR_OFF_LAMBDA;
// Relevance floor + rerank (Search & Recall Quality Suite). The floor
// drops sub-threshold candidates before the diversity rerank so a
// query with no sufficiently relevant memory returns no match; the
// rerank re-orders the qualified set by core textual relevance. Both
// off by default keep results byte-identical.
const scoreFloor = opts.threshold ?? 0;
const rerankActive = opts.rerank === true;
// Cross-encoder rerank (retrieval-precision-quality-loop, card A): the
// learned final reader step re-scores the top-K fused candidates. When
// it is on, the candidate pool must be at least `top_k` wide so a
// genuinely-relevant hit the heuristic ranker placed deep can be pulled
// into the final window. Off by default keeps the pool byte-identical.
// A decision-model reranker in shadow returns the heuristic order, so it
// must not widen the pool either: shadow output is byte-identical to
// rerank off.
// A call that skips the decision-model kind (hook surfaces) must not
// widen it either.
const crossEncoderActive =
config.rerank.enabled &&
!(
config.rerank.kind === "decision-model" &&
(config.rerank.decisionModel?.uses.rerank === "shadow" ||
opts.skipDecisionModelRerank === true)
);
const maxHops = opts.maxHops ?? config.recall.maxHops;
const traversalActive = maxHops > NO_TRAVERSAL_HOPS;
// Per-source-session pool cap (t_bb6ce095): on at a positive cap. The
// value in force is validated at this read site (review CR) - the
// per-query override used to bypass the config validation, so 1.5
// turned the cap on with a fractional comparison and NaN read as off.
const poolCap = resolveSessionPoolCap(
opts.poolMaxPerSession ?? config.recall.poolMaxPerSession ?? POOL_CAP_OFF,
);
const capConfigured = poolCap > POOL_CAP_OFF;
const baseRankLimit =
filters.canDropRows || hasStructuredExclusions ? semanticPoolSize(limit) : limit;
// The pool width the other recall phases need, judged as if the cap
// were off.
const rankLimitWithoutCap =
mmrActive || traversalActive || crossEncoderActive
? Math.max(
baseRankLimit,
limit * POOL_LIMIT_MULTIPLIER,
POOL_MIN_WIDTH,
crossEncoderActive ? config.rerank.topK : 0,
)
: baseRankLimit;
// When the cap is on it joins the widening, because a cap over an
// UNwidened pool merely truncates sooner - the ranked rows it needs to
// spread the window across sessions would sit below a rankLimit equal
// to `limit`.
const capWideRankLimit = capConfigured
? Math.max(rankLimitWithoutCap, limit * POOL_LIMIT_MULTIPLIER, POOL_MIN_WIDTH)
: rankLimitWithoutCap;
// Review L4: a cap at or above the pool width it would judge cannot
// drop a row from that pool - acting on it would read frontmatter for
// every row to remove nothing. It is treated as off: the phase never
// runs, and the pool keeps the width the other phases chose. The
// configured value still folds into the config fingerprint and the
// per-query cache key, so a change around this boundary can never
// serve a stale cached row.
const capActive = capConfigured && poolCap < capWideRankLimit;
const rankLimit = capActive ? capWideRankLimit : rankLimitWithoutCap;
// Rank → traverse → diversify → the caller's filters → the session
// cap, for a given candidate cap. Returns the pre-visibility count, the
// post-visibility list, and whether the cap was actually hit (so the
// caller can tell "the pool ran out" from "the cap truncated more").
const assemble = (rankCap: number): Assembly => {
// Rank ONE row past the cap, so "the cap bit" is measured rather than
// inferred. A pool whose length merely EQUALS the cap is the exhausted
// corpus and the truncated one at once, and reporting the first as
// `rank-cap-truncated-pool` told the caller matches existed below a cut
// that never happened - the pool ended there. `rankResults` sorts the
// whole fused candidate set and slices last, so the retained prefix is
// byte-identical to ranking at `rankCap` and the probe costs one result
// object. The rejected alternative was counting the fused candidates
// here from the lane hits: that is a second copy of the ranker's own
// admission rules (a hit whose chunk failed to hydrate is dropped, the
// relational arm adds candidates no lane matched), and two copies of
// one rule are what drift.
const probed = rankCandidates(input, rankCap + CAP_PROBE_ROW);
const capHit = probed.length > rankCap;
let ranked = capHit ? probed.slice(0, rankCap) : probed;
// Content tier (recall-quality, C5): the ranked rows - and only they
// - get their real bodies before any content-consuming phase below
// (MMR, the relevance rerank). The rows the cap sliced away never
// cost a body. Rebuilding a row whose placeholder already matches is
// a no-op, so a full-content pool map (pre-cut shape) passes through
// byte-identically.
if (input.hydrateContents !== undefined && ranked.length > 0) {
const contentById = input.hydrateContents(ranked.map((r) => r.chunkId));
if (contentById.size > 0) {
ranked = ranked.map((r) => {
const content = contentById.get(r.chunkId);
return content === undefined || content === r.content
? r
: Object.freeze({ ...r, content });
});
}
}
// Retrieval-time staleness barrier (t_b0c9d0a3): drop any exact-state
// lane artifact a stale/older index may still hold, before any later
// phase can surface it. No-op (same reference) when the pool has none,
// so a vault that never used the lane is byte-identical.
ranked = applyExactStateBarrier(ranked).slice();
// Exact-duplicate merge (what-the-index-already-knew, task D): fold
// byte-identical passages into their best-ranked occurrence BEFORE
// traversal and the diversity rerank, so a duplicate neither seeds
// expansion twice nor pays for a quadratic Jaccard comparison that
// the hash already settled. No-op when the pool holds no duplicates.
const merge = mergeExactDuplicates(ranked, input.hydrated);
ranked = merge.results;
// Link-graph traversal (v0.13.0): walk outbound links from the top
// hits and surface related documents not already matched, scored by
// decay. No-op when maxHops == 0. Runs before MMR so expansions are
// subject to the same diversity pass.
if (traversalActive && ranked.length > 0) {
ranked = applyTraversal(input.store, ranked, {
maxHops,
hopDecay: config.recall.hopDecay,
maxExpansionPerHit: config.recall.maxExpansionPerHit,
});
// Temporal bridge (t_c3871f0c): with an active time range,
// traversal expansions must stay within a padded event-time
// neighbourhood of the window - linked causes/consequences
// bridge in with proximity-decayed scores, arbitrary old
// neighbours do not. Event time = validity start, else mtime.
if (input.timeRange !== null) {
ranked = applyTemporalBridge(ranked, {
range: input.timeRange,
eventTimeMs: (path) => eventTimeOrMtimeMs(config.vault, path, input.declaredEventTimeMs),
});
}
}
// Relevance floor (Search & Recall Quality Suite): drop
// sub-threshold candidates BEFORE diversity so the rerank works over
// the qualified set only. No-op when the floor is 0.
let floorDropped = 0;
if (scoreFloor > 0) {
const qualified = ranked.filter((r) => r.score >= scoreFloor);
floorDropped = ranked.length - qualified.length;
ranked = qualified;
}
// Diversity rerank (v0.13.0). No-op when lambda >= 1 or < 2 results.
if (mmrActive) {
ranked = mmrRerank(ranked, { lambda: mmrLambda });
}
// Relevance rerank (Search & Recall Quality Suite): re-order the
// qualified set by core textual relevance, a deeper-relevance second
// pass. Opt-in; replaces the diversity ordering when requested.
if (rerankActive) {
ranked = rerankByRelevance(ranked).slice();
}
// The caller's own filters (status, property, degree, reach,
// visibility, ownership, session/project scope) run BEFORE the cap
// (review M4): only a row the filters admit consumes its group's
// quota, so a session whose first rows fail a requested filter is not
// erased from the answer by its own failing rows, and a query scoped
// to one session still gets up to `cap` rows from it. The cap is
// removal-only over the already-filtered pool, so the security
// property the filters own holds by construction: it can never
// surface a row they would withhold. No-op when the cap is off.
const filtered = applyPoolFilters(ranked, filters, filterContext);
let sessionCapped = 0;
let widestGroupRows = 0;
let visible = filtered.visible;
if (capActive) {
const capped = applySessionPoolCap(filtered.visible, {
vault: config.vault,
capPerSession: poolCap,
frontmatterCache: input.frontmatterCache,
});
visible = capped.results;
sessionCapped = capped.dropped;
widestGroupRows = capped.widestGroupRows;
}
return {
preVisibility: filtered.preVisibility,
prePoolFilters: filtered.prePoolFilters,
filterVisible: filtered.visible.length,
visible,
capHit,
mergedAway: merge.mergedAway,
floorDropped,
sessionCapped,
widestGroupRows,
};
};
// The cap the FINAL assembly ran under: the backfill below may widen it,
// and reporting the first cap would name a number that no longer bound
// the answer.
let effectiveCap = rankLimit;
let assembled = assemble(rankLimit);
// Three phases can leave fewer than `limit` rows out of a pool that was
// capped at `limit`, while more qualifying matches sit deeper in the
// candidate list:
//
// - default-scope visibility (no explicit filter, so no overfetch
// above) drops tagged pages, and the reserved-token rule drops pages
// reserved against this caller's reach; untagged vaults never drop a
// row, so this never fires for them,
// - the exact-duplicate merge folds byte-identical passages away,
// - the per-source-session cap dropped filter-passing rows (review
// M3): the capped pool can hold fewer than `limit` rows even though
// the corpus holds more.
//
// In any of these cases, and only when the narrow cap was actually hit
// (as opposed to the candidate list simply running out - without a
// truncation there is nothing deeper to admit), re-assemble ONCE at the
// wider cap from the same in-memory candidates - no extra DB fetch. A
// pool with no dropped and no merged rows never re-assembles, so its
// results stay byte-identical.
const visibilityDropped =
!filters.canDropRows && assembled.filterVisible < assembled.prePoolFilters;
const sessionUnderfilled = assembled.sessionCapped > 0;
if (
assembled.visible.length < limit &&
assembled.capHit &&
(visibilityDropped || assembled.mergedAway > 0 || sessionUnderfilled)
) {
const wideCap = Math.max(
semanticPoolSize(limit),
limit * POOL_LIMIT_MULTIPLIER,
POOL_MIN_WIDTH,
// The re-widen contract (review M3): outrun the heaviest group the
// first pass observed. `widestGroupRows` rows of one group fit in
// the width just judged; a window of `limit` rows is refillable
// from the groups the cap did not exhaust only if they are
// reachable, so rank `limit * (widestGroupRows + 1)` - one more
// pass, at a width that covers the heavy group's occupancy as the
// first pass's mix observed it. Best-effort, not a guarantee: the
// second pass admits deeper rows, so a heavier-than-observed
// occupancy (rank skewed hard toward the heavy group) can crowd
// the window again and answer short even though the corpus holds
// outside rows - and a corpus that holds nothing outside the heavy
// group always does. The trail names the final pass's numbers
// honestly rather than retrying.
sessionUnderfilled ? limit * (assembled.widestGroupRows + 1) : 0,
);
if (wideCap > rankLimit) {
assembled = assemble(wideCap);
effectiveCap = wideCap;
}
}
noteDerivedPersonAmbiguities(input.degraded, input.signals);
noteAssemblyNarrowings(input.degraded, assembled, effectiveCap, poolCap);
return assembled.visible;
}
/**
* Name the query person forms that reached several people ONLY through
* derivation, and therefore expanded nothing - the trail entry is what
* keeps "reported rather than guessed" from reading as silence. One entry
* per ambiguous form, recorded once per answer beside the assembly
* narrowings: the detail maps the form to its competing person ids,
* dot-joined. Entity ids never carry a dot, so the join is exact, and the
* form rides the detail KEY because a name form is not an identifier and
* the detail value lane carries identifiers and integers only.
*/
function noteDerivedPersonAmbiguities(
sink: RetrievalDegradationSink,
signals: CandidateSignals,
): void {
for (const ambiguity of signals.derivedAmbiguities ?? []) {
noteDegradation(sink, RETRIEVAL_DEGRADATION.personDerivedAmbiguity, {
[ambiguity.form]: ambiguity.ids.join("."),
});
}
}
/**
* Name what this stage removed, once, against the assembly that actually
* produced the answer. Recorded here rather than inside `assemble` because
* that closure can run twice for one query, and a caller reading two
* identical codes would think two separate things narrowed the pool.
*/
function noteAssemblyNarrowings(
sink: RetrievalDegradationSink,
assembled: Assembly,
cap: number,
poolCap: number,
): void {
if (assembled.capHit) {
noteDegradation(sink, RETRIEVAL_DEGRADATION.rankCapTruncatedPool, { cap });
}
if (assembled.floorDropped > 0) {
noteDegradation(sink, RETRIEVAL_DEGRADATION.relevanceFloorDroppedRows, {
dropped: assembled.floorDropped,
});
}
if (assembled.sessionCapped > 0) {
noteDegradation(sink, RETRIEVAL_DEGRADATION.sourceSessionCappedRows, {
dropped: assembled.sessionCapped,
cap: poolCap,
});
}
// `preVisibility` is the row count before the visibility, ownership and
// composite session/project scopes the CALLER requested ran - the reach
// rule is already counted out of it. The difference is the answer to
// "did a scope remove my results", which nothing could ask before; the
// reach rule is not one of those scopes, and its drop count is withheld
// rather than published (see `pipeline/pool-filters.ts`). The comparison
// reads the PRE-CAP filtered count (`filterVisible`), never `visible`:
// the session cap is not a caller scope, its drops carry their own code
// above, and counting them here would report the cap as a scope.
const scoped = assembled.preVisibility - assembled.filterVisible;
if (scoped > 0) {
noteDegradation(sink, RETRIEVAL_DEGRADATION.scopeFiltersDroppedRows, {
dropped: scoped,
before: assembled.preVisibility,
});
}
}
function rankCandidates(input: AssemblyInput, rankCap: number): BrainSearchResult[] {
const { config, opts, signals } = input;
return rankResults(
{
keyword: input.keywordHits,
semantic: input.semanticHits,
hydrated: input.hydrated,
inboundLinkSources: signals.inboundLinkSources,
tagsByDoc: signals.tagsByDoc,
...(signals.entityMatchByChunk !== undefined
? { entityMatchByChunk: signals.entityMatchByChunk }
: {}),
...(signals.derivedEntityMatchByChunk !== undefined
? { derivedEntityMatchByChunk: signals.derivedEntityMatchByChunk }
: {}),
...(signals.activationByChunk !== undefined
? { activationByChunk: signals.activationByChunk }
: {}),
...(signals.coAccessByChunk !== undefined
? { coAccessByChunk: signals.coAccessByChunk }
: {}),
...(signals.trendByDoc !== undefined ? { trendByDoc: signals.trendByDoc } : {}),
...(signals.tierByDoc !== undefined ? { tierByDoc: signals.tierByDoc } : {}),
...(signals.reuseRateByChunk !== undefined
? { reuseRateByChunk: signals.reuseRateByChunk }
: {}),
...(input.relationalRankedChunkIds.length > 0
? { relationalRankedChunkIds: input.relationalRankedChunkIds }
: {}),
...(signals.eventTimeMsByChunk !== undefined
? { eventTimeMsByChunk: signals.eventTimeMsByChunk }
: {}),
...(signals.pinnedDocIds !== undefined ? { pinnedDocIds: signals.pinnedDocIds } : {}),
},
{
keywordWeight: opts.keywordWeight ?? config.keywordWeight,
semanticWeight: opts.semanticWeight ?? config.semanticWeight,
limit: rankCap,
semanticEnabled: input.semanticEnabled,
// Metadata-boost lexical-vote gate (t_d9f863e9): the resolved
// `search_metadata_boost_gate` key, off by default.
metadataBoostGate: config.recall.metadataBoostGateEnabled === true,
// The request's clock, not the ranker's. Ranking is called up to
// twice per search (the visibility backfill re-assembles from the
// same candidates), and both passes must judge freshness against
// the same instant as the time filter and the temporal intent did.
nowMs: input.nowMs,
recency: {
shape: config.recall.recencyShape,
scale: config.recall.recencyScale,
amplitude: config.recall.recencyAmplitude,
},
...(input.weightProfile !== undefined ? { weightProfile: input.weightProfile } : {}),
...(input.sessionFocus !== undefined ? { sessionFocus: input.sessionFocus } : {}),
fusionMode: config.fusionMode,
rrfK: config.rrfK,
temporalIntent: input.temporalIntent,
},
);
}
/** Declared event time, falling back to storage mtime. */
function eventTimeOrMtimeMs(
vault: string | null,
path: string,
declaredEventTimeMs: (path: string) => number | null,
): number {
const declared = declaredEventTimeMs(path);
if (declared !== null) return declared;
// No vault: a store record has no file, as a missing one reads.
if (vault === null) return Number.NEGATIVE_INFINITY;
try {
return statSync(join(vault, path)).mtimeMs;
} catch {
return Number.NEGATIVE_INFINITY;
}
}