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
13.2 KB
Raw Blame
/**
 * Cross-preference contradiction detector (F1).
 *
 * Surfaces pairs of confirmed preferences that are about the same
 * subject but carry an opposite sign of record. "Same subject" is a
 * structural signal - principle token overlap (jaccard) via the shared
 * `similarity.ts` walker - and the polarity comes from `sign.ts`, which
 * derives each preference's sign from its `evidenced_by` signals. There
 * is no negation dictionary and no per-language vocabulary: a rule and
 * its negation share most of their tokens, so a high-overlap pair with
 * opposite signs is the language-agnostic shape of a contradiction.
 *
 * Pure and deterministic: identical input yields identical findings on
 * every Syncthing peer.
 */

import { dominantSignOf } from "../sign.ts";
import { findSimilarPairs, tokenise } from "../similarity.ts";
import { BRAIN_HEALTH_DEFAULTS } from "../policy.ts";
import {
  READ_ALL_REFS,
  findNearDuplicates,
  type NearDuplicatePoolEntry,
} from "../near-duplicate.ts";
import {
  BRAIN_PREFERENCE_STATUS,
  type BrainPreferenceStatus,
  type BrainSignalSign,
} from "../types.ts";

/**
 * Narrow projection of {@link BrainPreference} the detector needs. The
 * full preference type satisfies this structurally, so callers pass
 * parsed preferences directly.
 */
export interface PreferenceForContradiction {
  readonly id: string;
  readonly scope?: string;
  readonly status: BrainPreferenceStatus;
  readonly principle: string;
  readonly evidenced_by: ReadonlyArray<string>;
}

export interface ContradictionFinding {
  readonly aId: string;
  readonly bId: string;
  /** Shared scope of the pair (the bucket key); `null` when unscoped. */
  readonly scope: string | null;
  readonly jaccard: number;
  readonly aSign: BrainSignalSign;
  readonly bSign: BrainSignalSign;
}

export interface DetectContradictionsOptions {
  /** Minimum principle jaccard for two preferences to count as the same subject. */
  readonly jaccard: number;
}

/**
 * A starter, deliberately multilingual negation lexicon.
 *
 * The confirmed-preference detector reads polarity from a structured
 * `signal` field and never inspects text, so it needs no lexicon. Note
 * prose carries no such field, so the stance sign must be derived from
 * the prose itself - and the only language-agnostic structural marker of
 * a reversed stance is a negation particle inserted into an otherwise
 * shared claim.
 *
 * This set is a documented DEFAULT, not a baked-in assumption: callers
 * pass `opts.negationMarkers` to override or extend it for their own
 * languages (mirroring `similarity.ts`, which keeps its stopword set
 * empty rather than bias toward English). It spans several space-
 * delimited scripts; single-glyph CJK negations are out of reach of the
 * whitespace tokeniser and left to a caller-supplied lexicon.
 */
export const DEFAULT_NEGATION_MARKERS: ReadonlySet<string> = new Set([
  // English
  "not",
  "no",
  "never",
  "none",
  "neither",
  "nor",
  "cannot",
  "without",
  // Romance (es / fr / pt / it)
  "non",
  "ne",
  "pas",
  "nunca",
  "sin",
  "sem",
  "senza",
  "sans",
  "não",
  "nao",
  // German
  "nicht",
  "kein",
  "keine",
  "nie",
  "ohne",
  // Russian
  "не",
  "нет",
  "ни",
]);

/**
 * Narrow projection of a permanent note the detector needs. The caller
 * is responsible for restricting the input to permanent notes; the
 * detector operates on whatever prose it is handed.
 */
export interface NoteForContradiction {
  readonly id: string;
  /**
   * Optional subject bucket. When present, only notes sharing a subject
   * are compared (mirrors `scope` in the preference detector); absent
   * notes fall into a single bucket and are paired purely by prose
   * token overlap.
   */
  readonly subject?: string;
  readonly text: string;
}

export interface NoteContradictionFinding {
  readonly aId: string;
  readonly bId: string;
  /** Sorted shared subject tokens, space-joined, for display. */
  readonly subject: string;
  readonly jaccard: number;
  readonly aSign: BrainSignalSign;
  readonly bSign: BrainSignalSign;
  /** The subject-bearing span quoted verbatim from note A. */
  readonly aQuote: string;
  /** The subject-bearing span quoted verbatim from note B. */
  readonly bQuote: string;
  /** Always `ask_user`: contradictions are surfaced, never auto-resolved. */
  readonly action: "ask_user";
}

export interface DetectNoteContradictionsOptions {
  /** Minimum prose jaccard for two notes to count as the same subject. */
  readonly jaccard: number;
  /**
   * Negation markers used to derive each note's stance sign from prose.
   * Defaults to {@link DEFAULT_NEGATION_MARKERS}; pass a custom set to
   * override or extend it for a given language.
   */
  readonly negationMarkers?: ReadonlySet<string>;
}

/**
 * Derive a note's stance sign structurally from its prose: a note whose
 * tokens include any negation marker asserts the negative side of its
 * subject; otherwise it reads as positive. No sentiment analysis and no
 * per-language vocabulary beyond the injected marker set.
 */
export function deriveNoteStance(
  tokens: ReadonlySet<string>,
  negationMarkers: ReadonlySet<string>,
): BrainSignalSign {
  for (const t of tokens) {
    if (negationMarkers.has(t)) return "negative";
  }
  return "positive";
}

/**
 * Split prose into candidate spans on sentence terminators (Latin and
 * CJK) and hard line breaks, then return the span with the greatest
 * overlap with `subjectTokens`. Ties break to the earliest span; a note
 * with no scoring span falls back to its first non-empty span, or the
 * trimmed whole text. Deterministic for a given input.
 */
export function extractSpan(text: string, subjectTokens: ReadonlySet<string>): string {
  const segments = text
    .split(/(?<=[.!?。!?])\s+|\n+/u)
    .map((s) => s.trim())
    .filter((s) => s.length > 0);
  if (segments.length === 0) return text.trim();
  let best = segments[0]!;
  let bestScore = -1;
  for (const segment of segments) {
    const segTokens = tokenise(segment);
    let score = 0;
    for (const t of subjectTokens) if (segTokens.has(t)) score++;
    if (score > bestScore) {
      bestScore = score;
      best = segment;
    }
  }
  return best;
}

/**
 * Detect contradictory note-position pairs over permanent-note prose.
 * Notes are bucketed by `subject` (single bucket when absent), paired by
 * prose token similarity at or above `jaccard`, and kept only when the
 * two stance signs - derived structurally from each note's prose -
 * disagree. Each finding quotes the subject-bearing span from both notes
 * and carries `action: "ask_user"`: the pair is surfaced for the
 * operator to clarify, never auto-resolved.
 */
export function detectNoteContradictions(
  notes: ReadonlyArray<NoteForContradiction>,
  opts: DetectNoteContradictionsOptions,
): NoteContradictionFinding[] {
  const negationMarkers = opts.negationMarkers ?? DEFAULT_NEGATION_MARKERS;
  const signById = new Map<string, BrainSignalSign>();
  const entries = [];
  for (const n of notes) {
    const tokens = tokenise(n.text);
    signById.set(n.id, deriveNoteStance(tokens, negationMarkers));
    entries.push({
      id: n.id,
      bucketKey: n.subject ?? "",
      tokens,
      source: n,
    });
  }

  const pairs = findSimilarPairs(entries, { threshold: opts.jaccard });
  const out: NoteContradictionFinding[] = [];
  for (const pair of pairs) {
    const aSign = signById.get(pair.a.id)!;
    const bSign = signById.get(pair.b.id)!;
    if (aSign === bSign) continue;
    const shared: string[] = [];
    for (const t of pair.a.tokens) if (pair.b.tokens.has(t)) shared.push(t);
    shared.sort();
    const sharedSet = new Set(shared);
    out.push({
      aId: pair.a.id,
      bId: pair.b.id,
      subject: shared.join(" "),
      jaccard: pair.jaccard,
      aSign,
      bSign,
      aQuote: extractSpan(pair.a.source.text, sharedSet),
      bQuote: extractSpan(pair.b.source.text, sharedSet),
      action: "ask_user",
    });
  }
  out.sort((x, y) => x.aId.localeCompare(y.aId) || x.bId.localeCompare(y.bId));
  return out;
}

/** Evidence for one incoming-vs-confirmed conflict. */
export interface IncomingConflictEvidence {
  /** The confirmed preference the incoming principle resembles. */
  readonly prefId: string;
  /** Principle-token jaccard that put the pair over the threshold. */
  readonly jaccard: number;
}

/**
 * A write-time conflict advisory: the confirmed same-scope preferences an
 * incoming feedback principle resembles closely enough to be worth a
 * second look. Advisory only - it is never a reason to block a write.
 */
export interface IncomingConflictAdvisory {
  /** The scope bucket compared against; `null` for the unscoped bucket. */
  readonly scope: string | null;
  /** Non-empty, sorted by descending similarity then id. */
  readonly conflicts: ReadonlyArray<IncomingConflictEvidence>;
}

export interface AdviseOnIncomingOptions {
  /**
   * Minimum principle jaccard for a confirmed preference to count as the
   * same subject. Defaults to the health-pass default
   * (`BRAIN_HEALTH_DEFAULTS.contradiction_jaccard`) so the write-time
   * advisory and the batch contradiction detector use one threshold.
   */
  readonly jaccard?: number;
}

/**
 * Compute a write-time conflict advisory for a single incoming feedback
 * principle against already-confirmed preferences. Scores through the
 * shared near-duplicate kernel (`findNearDuplicates`, over the same
 * `tokenise` + `jaccard` primitives `detectContradictions` builds on), and
 * mirrors its bucketing: only confirmed preferences in the
 * SAME scope bucket are compared (an unscoped incoming signal compares
 * against the unscoped bucket). Every confirmed same-scope preference
 * whose principle overlaps the incoming principle at or above the
 * threshold is returned as evidence.
 *
 * Unlike `detectContradictions`, this is a similarity-only signal: it does
 * not derive preference polarity (that would require the full signal-sign
 * map, which the write path deliberately does not load - only the scope
 * bucket is read). High principle overlap is the language-agnostic shape
 * of "you may be contradicting or duplicating an existing rule"; the
 * operator judges. Pure and deterministic; never throws on well-formed
 * input.
 *
 * Two kernel rules apply: a principle (incoming or confirmed) under
 * `NEAR_DUPLICATE_MIN_TOKENS` is not scored, because short principles
 * share most of their tokens by accident, and each similarity is rounded
 * to 3 decimals. Reach is not decided here: the caller hands in only the
 * preferences it may read (`write-advisory.ts` applies the reach when it
 * loads them), so the kernel's predicate is the explicit allow-all.
 *
 * Returns `null` when no confirmed same-scope preference clears the
 * threshold - i.e. there is nothing to advise about.
 */
export function adviseOnIncoming(
  principle: string,
  scope: string | undefined,
  confirmedPrefs: ReadonlyArray<PreferenceForContradiction>,
  opts: AdviseOnIncomingOptions = {},
): IncomingConflictAdvisory | null {
  const pool: NearDuplicatePoolEntry[] = [];
  for (const p of confirmedPrefs) {
    if (p.status !== BRAIN_PREFERENCE_STATUS.confirmed) continue;
    pool.push({ ref: p.id, tokens: tokenise(p.principle), bucket: p.scope ?? "" });
  }
  // The incoming signal has no id yet, so no preference id can equal the
  // empty probe ref. Every confirmed preference is scored, as before: the
  // pool is the caller's, so the kernel's cap is set to its size.
  const { matches } = findNearDuplicates({ ref: "", tokens: tokenise(principle) }, pool, {
    threshold: opts.jaccard ?? BRAIN_HEALTH_DEFAULTS.contradiction_jaccard,
    readable: READ_ALL_REFS,
    bucket: scope ?? "",
    cap: pool.length,
  });
  if (matches.length === 0) return null;
  return {
    scope: scope ?? null,
    conflicts: matches.map((m) => ({ prefId: m.ref, jaccard: m.score })),
  };
}

/**
 * Detect contradictory confirmed-preference pairs. Preferences are
 * bucketed by scope (so only same-scope rules are compared), paired by
 * principle token similarity at or above `jaccard`, and kept only when
 * the two resolved signs disagree. Preferences whose sign cannot be
 * resolved from evidence are skipped rather than guessed.
 */
export function detectContradictions(
  prefs: ReadonlyArray<PreferenceForContradiction>,
  signSignById: ReadonlyMap<string, BrainSignalSign>,
  opts: DetectContradictionsOptions,
): ContradictionFinding[] {
  const signById = new Map<string, BrainSignalSign>();
  const entries = [];
  for (const p of prefs) {
    if (p.status !== BRAIN_PREFERENCE_STATUS.confirmed) continue;
    const sign = dominantSignOf(p.evidenced_by, signSignById);
    if (sign === "unknown") continue;
    signById.set(p.id, sign);
    entries.push({
      id: p.id,
      bucketKey: p.scope ?? "",
      tokens: tokenise(p.principle),
      source: p,
    });
  }

  const pairs = findSimilarPairs(entries, { threshold: opts.jaccard });
  const out: ContradictionFinding[] = [];
  for (const pair of pairs) {
    const aSign = signById.get(pair.a.id)!;
    const bSign = signById.get(pair.b.id)!;
    if (aSign === bSign) continue;
    out.push({
      aId: pair.a.id,
      bId: pair.b.id,
      scope: pair.a.source.scope ?? null,
      jaccard: pair.jaccard,
      aSign,
      bSign,
    });
  }
  out.sort((x, y) => x.aId.localeCompare(y.aId) || x.bId.localeCompare(y.bId));
  return out;
}