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.0 KB
Raw Blame
/**
 * `o2b brain inbox-drain` (Knowledge intake suite, I2, t_b0bba8cb): the
 * classify-and-route pass over staged captures.
 *
 * Dry-run is the default and writes nothing; `--apply` executes the route
 * and archives each processed capture through the seam-1 contract. Every
 * item is reported with its action and reason; unroutable items are named
 * and left in place. A rerun after apply finds no staged captures and is a
 * no-op.
 */

import {
  drainInbox,
  type DrainItem,
  type DrainReport,
} from "../../../core/brain/capture/inbox-drain.ts";
import { nextCommandField } from "../../../core/brain/next-step.ts";
import { emitNextStep } from "../../advisory-rail.ts";
import { brainVerbContext, ok, okJson, parse, resolveBrainAgent } from "../helpers.ts";

function itemJson(item: DrainItem): Record<string, unknown> {
  return {
    id: item.id,
    capture_path: item.capturePath,
    classification: item.classification,
    action: item.action,
    reason: item.reason,
    target: item.target,
    routed: item.routed,
  };
}

function reportJson(report: DrainReport): Record<string, unknown> {
  return {
    mode: report.mode,
    routed: report.routed,
    unroutable: report.unroutable,
    archive_failed: report.archiveFailed,
    skipped_corrupt: report.skippedCorrupt,
    items: report.items.map(itemJson),
  };
}

export async function cmdBrainInboxDrain(argv: string[]): Promise<number> {
  const { flags } = parse(argv, {
    vault: { type: "string" },
    agent: { type: "string" },
    apply: { type: "boolean" },
    json: { type: "boolean" },
  });
  const { config, vault } = brainVerbContext(flags);
  const agent = resolveBrainAgent(flags, config);
  const report = drainInbox(vault, {
    apply: flags["apply"] === true,
    agent,
    now: new Date(),
    configPath: config,
  });

  const pending = flags["apply"] !== true && report.items.length > 0;

  if (flags["json"] === true) {
    // no-dead-ends, phase 3: the exit was computed on the human path and
    // discarded here.
    okJson({
      ...reportJson(report),
      ...(pending ? nextCommandField("staged-captures-pending") : {}),
    });
    return 0;
  }

  ok(`inbox-drain (${report.mode}): ${report.items.length} capture(s)`);
  for (const item of report.items) {
    const targetLabel = item.target !== null ? ` -> ${item.target}` : "";
    ok(`  [${item.classification}] ${item.action}${targetLabel}: ${item.reason}`);
  }
  ok(
    `  routed ${report.routed}, unroutable ${report.unroutable}, archive-failed ${report.archiveFailed}` +
      (report.skippedCorrupt > 0
        ? `, corrupt hub-store lines dropped ${report.skippedCorrupt}`
        : ""),
  );
  if (pending) {
    // no-dead-ends, task 7: the terminal-state census found this
    // hand-written pointer, which the rail-detection could not see. One
    // mechanism, one line format. The `--json` branch above returned, so
    // this stream is provably a human one.
    emitNextStep("staged-captures-pending", { command: "brain", argv, jsonRequested: false });
  }
  return 0;
}