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
3.3 KB
Raw Blame
/**
 * Trigger scan (Workspace Insight Suite, t_cd1fee79): one explicit
 * pull that runs the existing report generators, normalizes their
 * findings through the adapters, and persists deduped triggers.
 */

import { runDoctor } from "../doctor.ts";
import { buildRetentionReview } from "../retention.ts";
import { collisionCandidates, detectAgentCollisions } from "../truth/collision.ts";
import { readClaimEvents } from "../truth/store.ts";
import { candidatesFromHealth, candidatesFromRetention } from "./adapters.ts";
import { createTriggers, type CreateTriggersOptions, type CreateTriggersResult } from "./store.ts";
import type { InsightCandidate } from "./types.ts";

export interface ScanTriggersOptions extends CreateTriggersOptions {
  /** Extra candidates from other producers (synthesis, ideas). */
  readonly extraCandidates?: ReadonlyArray<InsightCandidate>;
  /**
   * The vault-relative paths the scanning reader may read. When given,
   * the semantic-health and retention sources run over those records
   * only, so a withheld record yields no candidate, is counted in no
   * total and is written into no trigger. Absent: every record.
   */
  readonly readable?: (rel: string) => boolean;
}

export interface ScanTriggersResult extends CreateTriggersResult {
  readonly candidates: number;
}

/**
 * Generate trigger candidates from semantic health and retention data,
 * then persist them with cooldown dedup. Fail-soft per source: a
 * report that cannot run contributes nothing rather than aborting the
 * scan.
 */
export function scanTriggers(vault: string, opts: ScanTriggersOptions): ScanTriggersResult {
  const candidates: InsightCandidate[] = [];
  try {
    const readable = opts.readable;
    // The caller's clock rides through to every source: the doctor's
    // age-based checks (stale claims) judged the scan against the wall
    // clock while the retention and collision sources used `opts.now`,
    // so an injected `now` was only half-honoured and a scan pinned for
    // determinism still moved with the calendar.
    const doctor = runDoctor(vault, {
      now: opts.now,
      ...(readable !== undefined ? { readable } : {}),
    });
    if (doctor.semantic_health !== undefined) {
      candidates.push(...candidatesFromHealth(doctor.semantic_health));
    }
  } catch {
    // semantic health unavailable - skip the source
  }
  try {
    const review = buildRetentionReview(vault, { now: opts.now });
    const readable = opts.readable;
    candidates.push(
      ...candidatesFromRetention(
        readable === undefined
          ? review
          : { ...review, recommendations: review.recommendations.filter((r) => readable(r.path)) },
      ),
    );
  } catch {
    // retention review unavailable - skip the source
  }
  try {
    // Cross-agent collisions (t_f2b225b1): an empty ledger detects
    // nothing, so vaults without claim events stay byte-identical.
    const { events } = readClaimEvents(vault);
    if (events.length > 0) {
      candidates.push(...collisionCandidates(detectAgentCollisions(events, { now: opts.now })));
    }
  } catch {
    // claim ledger unavailable - skip the source
  }
  candidates.push(...(opts.extraCandidates ?? []));

  const result = createTriggers(vault, candidates, opts);
  return Object.freeze({ ...result, candidates: candidates.length });
}