Skip to content

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
6.4 KB
Raw Blame
/**
 * Semantic candidate lane as the pipeline sees it: the guarded vector
 * phase plus the two caller-visible signals that only make sense once the
 * lane has run or been skipped - embedding-ABI drift and hybrid degrade.
 */

import { detectHybridDegrade } from "../enrich.ts";
import { runSemanticPhase, semanticPoolSize } from "../semantic-phase.ts";
import { contradictedAbiFields, formatEmbeddingAbiDrift } from "../store.ts";
import { RETRIEVAL_DEGRADATION, noteDegradation } from "../retrieval-trail.ts";
import type { RetrievalDegradationSink } from "../retrieval-trail.ts";
import type { SemanticEmbedTiming, SemanticPolicy } from "../semantic-phase.ts";
import type { PropertyPushdown } from "../property-pushdown.ts";
import type { Store } from "../store.ts";
import type { TransportReach } from "../../graph/transport-reach.ts";
import type { ResolvedSearchConfig } from "../types.ts";

export interface SemanticLaneInput {
  readonly store: Store;
  readonly config: ResolvedSearchConfig;
  readonly policy: SemanticPolicy;
  /** Residual query text, used when no structured vec lane was given. */
  readonly query: string;
  readonly semanticLaneQuery: string | null;
  readonly limit: number;
  readonly pathPrefix: string | undefined;
  /** Chunk-kind pushdown (recall-quality, C4); absent leaves the lane unfiltered. */
  readonly kind?: "page" | "section";
  /** Promoted property pushdown (recall-quality, C4); absent leaves it unfiltered. */
  readonly pushdown?: PropertyPushdown;
  /**
   * Keyword pool size, the other half of the hybrid-degrade signal. A
   * supplier is read when the lane completes, not when it starts: the
   * overlapped caller starts this lane before the keyword lanes run and
   * can only hand over a closure over the pool they will have produced
   * (recall-lane-latency). A plain number is the snapshot form, for
   * callers that already hold the count.
   */
  readonly keywordHitCount: number | (() => number);
  /** The composite deadline's cancellation; absent when no deadline runs. */
  readonly signal?: AbortSignal;
  /**
   * The embed-interval record to stamp (recall-lane-latency), forwarded
   * to the phase and read by the join's caller. Absent records nothing.
   */
  readonly timing?: SemanticEmbedTiming;
  /**
   * The caller's reach, forwarded to the query-embed gateway. Absent
   * resolves to remote there, so an internal caller that must embed
   * under a positive cost gate passes local explicitly.
   */
  readonly transportReach?: TransportReach;
}

export interface SemanticLaneOutcome {
  readonly hits: ReturnType<Store["semanticTopK"]>;
  readonly attempted: boolean;
  readonly warnings: string[];
  /** The typed half of {@link warnings} (evidence-at-the-boundary, C2). */
  readonly degraded: RetrievalDegradationSink;
}

export async function runSemanticLane(input: SemanticLaneInput): Promise<SemanticLaneOutcome> {
  const { store, config, policy, query, semanticLaneQuery, limit, pathPrefix } = input;
  const warnings: string[] = [];
  const degraded: RetrievalDegradationSink = [];
  let hits: ReturnType<Store["semanticTopK"]> = [];
  let attempted = false;

  if (semanticLaneQuery !== null && !policy.wantSemantic) {
    warnings.push("semantic structured lanes skipped: semantic search is disabled");
    noteDegradation(degraded, RETRIEVAL_DEGRADATION.semanticStructuredLanesSkipped);
  }
  if (policy.wantSemantic) {
    const semOutcome = await runSemanticPhase(store, config, semanticLaneQuery ?? query, {
      limit: semanticPoolSize(limit),
      pathPrefix,
      ...(input.kind !== undefined ? { kind: input.kind } : {}),
      ...(input.pushdown !== undefined ? { pushdown: input.pushdown } : {}),
      explicit: policy.explicit,
      ...(input.signal !== undefined ? { signal: input.signal } : {}),
      ...(input.transportReach !== undefined ? { transportReach: input.transportReach } : {}),
      ...(input.timing !== undefined ? { timing: input.timing } : {}),
    });
    attempted = semOutcome.attempted;
    hits = semOutcome.hits;
    for (const w of semOutcome.warnings) warnings.push(w);
    for (const d of semOutcome.degraded) degraded.push(d);
    // Embedding-ABI drift on the QUERY path (context-integrity-gates,
    // Unit E). The read open already ran the gated comparison; until
    // now nothing on this path looked at the result, so under the
    // shipped `warn` default a caller was served neighbours out of a
    // vector table written by another build with nothing to observe.
    //
    // Attached to the queries it actually describes: only when the
    // semantic lane RAN, so a keyword-only query stays byte-identical,
    // and only for fields the index CONTRADICTS. An unrecorded token
    // is a store predating the stamp - true of every index built
    // before this release - and repeating it on every query would
    // make the warning worthless; it stays on `search status`,
    // `second_brain_status` and `search check`, which is where an
    // operator goes looking.
    if (attempted) {
      const contradicted = contradictedAbiFields(store.embeddingAbiMismatches());
      if (contradicted.length > 0) {
        warnings.push(formatEmbeddingAbiDrift(contradicted));
        // The field NAMES stay in the operator-facing warning, where they
        // are actionable; the trail states how many contradicted, because
        // a count is the part a machine reads.
        noteDegradation(degraded, RETRIEVAL_DEGRADATION.semanticEmbeddingAbiDrift, {
          fields: contradicted.length,
        });
      }
    }
  }

  // Hybrid-degrade signal (Search & Recall Quality Suite): one
  // structural warning when the caller wanted the semantic lane but it
  // did not run, so the query was served keyword-only. The granular
  // runSemanticPhase warnings above explain WHY; this is the single
  // greppable flag a caller can test for. The keyword count resolves
  // HERE, at completion: under the overlapped join the lane started
  // before the keyword lanes ran, and this is the first moment the
  // pool they produced exists to be counted.
  const keywordHitCount =
    typeof input.keywordHitCount === "function" ? input.keywordHitCount() : input.keywordHitCount;
  const degrade = detectHybridDegrade({
    wantSemantic: policy.wantSemantic,
    semanticAttempted: attempted,
    keywordHitCount,
  });
  if (degrade !== null) {
    warnings.push(degrade);
    noteDegradation(degraded, RETRIEVAL_DEGRADATION.hybridDegraded);
  }

  return { hits, attempted, warnings, degraded };
}