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
10.5 KB
Raw Blame
/**
 * The `o2b search` verbs advertise every flag they parse.
 *
 * There is no per-verb `--help` for the search verbs (`o2b search query
 * --help` exits 2 with "unknown flag: --help"), so `o2b help` and `o2b
 * help --json` are the ONLY discovery surface for these flags. The
 * manifest declared none of them, which is worse than a partial list read
 * as complete: an agent parsing `help --json` concluded `search query`
 * takes nothing but `--json`.
 *
 * The same hole was still open one line away on the two index builders,
 * where `reindex` parsed `--cron-template` and `--interval` - the whole
 * cron-recipe surface - with neither reachable from help or completions.
 * So the ratchet is a table: adding a verb to {@link COVERED} is all it
 * takes to hold the next one to the same standard.
 *
 * The direction of enumeration is the point, exactly as in
 * `manifest-completeness.test.ts`: this reads the `parseFlags` schema in
 * the VERB and asks the manifest to account for each key, never the
 * reverse. A manifest cannot vouch for its own completeness.
 */

import { describe, expect, test } from "bun:test";
import { join } from "node:path";

import { nestedCommand } from "../../src/cli/command-manifest.ts";
import { VAULT_FLAGS } from "../../src/cli/search/helpers.ts";
import { SEARCH_QUERY_FLAGS } from "../../src/cli/search/verbs/query.ts";
import { parsedFlagSchema } from "../helpers/parsed-flag-schema.ts";

const VERBS_DIR = join(import.meta.dir, "..", "..", "src", "cli", "search", "verbs");

/** One verb held to the ratchet: where it is parsed, and what models it. */
interface CoveredVerb {
  /** Manifest child name under `o2b search`. */
  readonly verb: string;
  /** Source file declaring the `parseFlags` schema. */
  readonly source: string;
  /** Function declaration the schema is read from, so sibling verbs in the
   * same module cannot leak into each other's census. */
  readonly marker: string;
  /** Lower bound on the parsed flag count, so a dead regex cannot pass. */
  readonly minFlags: number;
  /** A flag whose presence in help is the reason this entry exists. */
  readonly witness: readonly [name: string, type: string];
}

const COVERED: ReadonlyArray<CoveredVerb> = Object.freeze([
  {
    verb: "query",
    source: "query.ts",
    marker: "export async function cmdSearchQuery",
    minFlags: 20,
    witness: ["explain", "boolean"],
  },
  {
    verb: "index",
    source: "indexing.ts",
    marker: "export async function cmdSearchIndex",
    minFlags: 8,
    witness: ["force", "boolean"],
  },
  {
    verb: "reindex",
    source: "indexing.ts",
    marker: "export async function cmdSearchReindex",
    minFlags: 9,
    witness: ["cron-template", "boolean"],
  },
  {
    // Added with the probe's exit codes (wiring-what-exists, E1): the
    // verb had just gained a flag that decides whether it makes a network
    // call, on a surface where an undeclared flag is an unreachable one.
    verb: "check",
    source: "check.ts",
    marker: "export async function cmdSearchCheck",
    minFlags: 5,
    witness: ["no-probe", "boolean"],
  },
  {
    // Added with the verb itself (nothing-writes-silently, unit G): a
    // repair verb whose `--apply` gate is not modelled advertises a
    // dry run with no way to reach the write it exists for.
    verb: "restamp",
    source: "restamp.ts",
    marker: "export async function cmdSearchRestamp",
    minFlags: 4,
    witness: ["apply", "boolean"],
  },
  {
    // Added with `--progress` (nothing-runs-unwatched, U1): this verb
    // grew the whole progress spine in core - an options field, a
    // counter, a terminator - with no flag to reach it, so the wiring
    // was unreachable from every surface an operator has. The flag is
    // the reach, and an undeclared flag on this family is no reach at all.
    verb: "vector-backfill",
    source: "vector-backfill.ts",
    marker: "export async function cmdSearchVectorBackfill",
    minFlags: 6,
    witness: ["progress", "boolean"],
  },
]);

/**
 * The inherited flag the manifest states once in the help header instead
 * of repeating on every entry; parity is asserted over declared flags.
 */
const INHERITED_FLAG_NAME = "json";

/**
 * The `parseFlags(argv, { ... })` schema the verb declares, as `name -> type`.
 *
 * The query verb's schema is no longer an inline literal: it is the
 * descriptor-derived {@link SEARCH_QUERY_FLAGS} export (t_50c81d5c E3),
 * read directly - same object the verb passes to its parser, so the
 * ratchet's direction is unchanged (what the verb parses must be
 * modelled) and the static source read stays only for the verbs whose
 * schemas are still literals.
 */
function verbFlagSchema(entry: CoveredVerb): ReadonlyMap<string, string> {
  if (entry.source === "query.ts") {
    return new Map(Object.entries(SEARCH_QUERY_FLAGS).map(([name, spec]) => [name, spec.type]));
  }
  return parsedFlagSchema({
    file: join(VERBS_DIR, entry.source),
    marker: entry.marker,
    callOpen: "parseFlags(argv, {",
    spreads: { "...VAULT_FLAGS": VAULT_FLAGS },
  });
}

/** The manifest's declared flags for one `o2b search` verb, as `name -> type`. */
function manifestFlagSchema(verb: string): ReadonlyMap<string, string> {
  const entry = nestedCommand("search", verb);
  expect(`the manifest models search ${verb}: ${entry !== undefined}`).toBe(
    `the manifest models search ${verb}: true`,
  );
  return new Map((entry!.flags ?? []).map((f) => [f.name, f.type]));
}

/** Both sides minus the inherited flag the header already states. */
function comparable(schema: ReadonlyMap<string, string>): ReadonlyMap<string, string> {
  const out = new Map(schema);
  out.delete(INHERITED_FLAG_NAME);
  return out;
}

/** Every disagreement between one verb's parse and its manifest entry. */
function mismatches(
  parsed: ReadonlyMap<string, string>,
  modelled: ReadonlyMap<string, string>,
  label: string,
): string[] {
  const problems: string[] = [];
  for (const [name, type] of parsed) {
    const declared = modelled.get(name);
    if (declared === undefined) problems.push(`${label} --${name} is parsed but not modelled`);
    else if (declared !== type) {
      problems.push(`${label} --${name} is modelled as ${declared} but parsed as ${type}`);
    }
  }
  return problems;
}

describe("the search verbs declare the flags they parse", () => {
  test("every parsed flag is modelled by the manifest, with the same type", () => {
    // Every verb folds into ONE comparison so the failure message is the
    // complete list of work to be done.
    const problems: string[] = [];
    for (const entry of COVERED) {
      problems.push(
        ...mismatches(
          comparable(verbFlagSchema(entry)),
          comparable(manifestFlagSchema(entry.verb)),
          entry.verb,
        ),
      );
    }
    expect(problems.toSorted().join("\n")).toBe("");
  });

  test("no modelled flag outlives the parse that would accept it", () => {
    const stale: string[] = [];
    for (const entry of COVERED) {
      const parsed = comparable(verbFlagSchema(entry));
      for (const name of comparable(manifestFlagSchema(entry.verb)).keys()) {
        if (!parsed.has(name)) stale.push(`${entry.verb} --${name}`);
      }
    }
    expect(stale.toSorted().join("\n")).toBe("");
  });

  test("each verb's witness flag is reachable from help", () => {
    for (const entry of COVERED) {
      const [name] = entry.witness;
      const modelled = manifestFlagSchema(entry.verb);
      expect(`search ${entry.verb} --${name}: ${modelled.has(name)}`).toBe(
        `search ${entry.verb} --${name}: true`,
      );
    }
  });

  test("the ratchet is not vacuous and can fail", () => {
    // Pin the measurement, not only its verdict: a regex that stopped
    // matching would report a clean sweep over an empty set.
    for (const entry of COVERED) {
      const parsed = comparable(verbFlagSchema(entry));
      expect(`${entry.verb} parsed count above floor: ${parsed.size >= entry.minFlags}`).toBe(
        `${entry.verb} parsed count above floor: true`,
      );
      expect(parsed.get(entry.witness[0])).toBe(entry.witness[1]);
      expect(parsed.get("vault")).toBe("string");
    }

    // Prove the comparison catches a violation: a verb schema carrying a
    // flag the manifest never heard of, and one whose type disagrees.
    const violating = new Map(comparable(verbFlagSchema(COVERED[0]!)));
    violating.set("undeclared-flag", "boolean");
    violating.set("limit", "boolean");
    expect(
      mismatches(violating, comparable(manifestFlagSchema("query")), "query").toSorted(),
    ).toEqual([
      "query --limit is modelled as string but parsed as boolean",
      "query --undeclared-flag is parsed but not modelled",
    ]);
  });
});

describe("the schema verb declares the flags its two subcommands parse", () => {
  // `o2b search schema` sits outside the COVERED table above for one
  // pinned reason: that table's non-vacuity floor asserts every covered
  // verb parses `--vault`, and this verb addresses the repo's committed
  // artifact, not a vault. The same standard still applies, so it is held
  // here: both subcommand parse schemas are read from the verb and the
  // manifest row must account for every flag, never the reverse.
  const MARKER = "export async function cmdSearchSchema";
  const SOURCE = join(VERBS_DIR, "schema-export.ts");

  function schemaFor(callOpen: string): ReadonlyMap<string, string> {
    return parsedFlagSchema({ file: SOURCE, marker: MARKER, callOpen });
  }

  test("every parsed flag is modelled, with the same type", () => {
    const exportSchema = schemaFor("parseFlags(argv.slice(1), {");
    const checkSchema = schemaFor("parseFlags(argv.slice(1), { json:");
    // Pin both reads so a regex that stopped matching cannot pass: export
    // parses stdout + json, check parses json only.
    expect(Object.fromEntries(exportSchema)).toEqual({ stdout: "boolean", json: "boolean" });
    expect(Object.fromEntries(checkSchema)).toEqual({ json: "boolean" });
    const manifest = comparable(manifestFlagSchema("schema"));
    expect(mismatches(comparable(exportSchema), manifest, "schema").toSorted()).toEqual([]);
    expect(mismatches(comparable(checkSchema), manifest, "schema").toSorted()).toEqual([]);
  });

  test("no modelled flag outlives the parse that would accept it", () => {
    const parsed = schemaFor("parseFlags(argv.slice(1), {");
    for (const name of comparable(manifestFlagSchema("schema")).keys()) {
      expect(`${name} parsed: ${parsed.has(name)}`).toBe(`${name} parsed: true`);
    }
  });

  test("the witness flag is reachable from help", () => {
    expect(manifestFlagSchema("schema").has("stdout")).toBe(true);
  });
});