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
4.8 KB
Raw Blame
import { test, expect } from "bun:test";

import {
  embeddingSignature,
  estimateTokens,
  isStaleSignature,
  signatureIdentityKnown,
  EMBEDDING_PRICING,
  LOCAL_EMBEDDING_MODEL,
  MODEL_NATIVE_DIMENSION,
} from "../../../src/core/search/embeddings/signature.ts";

test("embeddingSignature canonicalises provider/model/dimension", () => {
  expect(
    embeddingSignature({
      provider: "openai-compat",
      model: "text-embedding-3-small",
      dimension: 1536,
    }),
  ).toBe("openai-compat:text-embedding-3-small:1536");
});

test("embeddingSignature lowercases and trims provider + model", () => {
  expect(
    embeddingSignature({
      provider: " OpenAI-Compat ",
      model: "  Text-Embedding-3-Small ",
      dimension: 256,
    }),
  ).toBe("openai-compat:text-embedding-3-small:256");
});

test("embeddingSignature renders null model/dimension as a stable sentinel", () => {
  expect(embeddingSignature({ provider: "local", model: null, dimension: null })).toBe("local:?:?");
});

test("two identities with the same fields share a signature; different dim differs", () => {
  const a = embeddingSignature({ provider: "local", model: "hashing-ngram-v1", dimension: 256 });
  const b = embeddingSignature({ provider: "local", model: "hashing-ngram-v1", dimension: 256 });
  const c = embeddingSignature({ provider: "local", model: "hashing-ngram-v1", dimension: 512 });
  expect(a).toBe(b);
  expect(a).not.toBe(c);
});

test("local model is present in the pricing table at 0", () => {
  expect(EMBEDDING_PRICING[LOCAL_EMBEDDING_MODEL]).toBe(0);
});

test("estimateTokens sums a chars/4 heuristic and rounds up", () => {
  // "abcd" -> 1 token; "abcdefgh" -> 2 tokens.
  expect(estimateTokens(["abcd"])).toBe(1);
  expect(estimateTokens(["abcdefgh"])).toBe(2);
  expect(estimateTokens(["abcde"])).toBe(2); // ceil(5/4)
  expect(estimateTokens([])).toBe(0);
  expect(estimateTokens(["", ""])).toBe(0);
});

test("isStaleSignature is true only when active and stored differ", () => {
  expect(isStaleSignature("local:m:256", "local:m:256")).toBe(false);
  expect(isStaleSignature("local:m:256", "local:m:512")).toBe(true);
  expect(isStaleSignature("openai-compat:a:1536", "local:m:256")).toBe(true);
});

test("signatureIdentityKnown is true only for a parseable triple with no sentinel field", () => {
  expect(signatureIdentityKnown("local:hashing-ngram-v1:256")).toBe(true);
  // A provider whose name carries a colon still parses from the end.
  expect(signatureIdentityKnown("scoped:provider:m:256")).toBe(true);
  expect(signatureIdentityKnown("local:?:256")).toBe(false);
  expect(signatureIdentityKnown("local:hashing-ngram-v1:?")).toBe(false);
  expect(signatureIdentityKnown("local:?:?")).toBe(false);
  // Not a renderable triple at all: not a known identity either.
  expect(signatureIdentityKnown("garbage")).toBe(false);
});

test("an unknown-identity signature is stale against itself: unknown never compares equal", () => {
  // The `?` sentinel renders identically across processes, so two
  // unknown-identity configs share a signature string; the equality
  // layer must still refuse to call them compatible.
  expect(isStaleSignature("local:?:?", "local:?:?")).toBe(true);
  expect(isStaleSignature("openai-compat:?:1536", "openai-compat:?:1536")).toBe(true);
});

test("a known identity is never compatible with an unknown one, in either direction", () => {
  expect(isStaleSignature("local:m:256", "local:?:?")).toBe(true);
  expect(isStaleSignature("local:?:?", "local:m:256")).toBe(true);
  expect(isStaleSignature("local:m:256", "local:m:?")).toBe(true);
  expect(isStaleSignature("local:m:?", "local:m:256")).toBe(true);
});

test("signatures rendered from null-field identities stay parseable and stable", () => {
  // The sentinel rendering itself is unchanged - only the equality
  // decision learned that it means "unknown", never "compatible".
  const a = embeddingSignature({ provider: "openai-compat", model: null, dimension: 1536 });
  const b = embeddingSignature({ provider: "openai-compat", model: null, dimension: 1536 });
  expect(a).toBe("openai-compat:?:1536");
  expect(b).toBe(a);
});

test("a named model at its native dimension is a known identity; an unnamed one is not", () => {
  const native = embeddingSignature({
    provider: "openai-compat",
    model: "m",
    dimension: MODEL_NATIVE_DIMENSION,
  });
  expect(signatureIdentityKnown(native)).toBe(true);
  expect(isStaleSignature(native, native)).toBe(false);
  // A declared width is a different identity from the native one.
  expect(
    isStaleSignature(
      native,
      embeddingSignature({ provider: "openai-compat", model: "m", dimension: 512 }),
    ),
  ).toBe(true);
  const unnamed = embeddingSignature({
    provider: "openai-compat",
    model: null,
    dimension: MODEL_NATIVE_DIMENSION,
  });
  expect(isStaleSignature(unnamed, unnamed)).toBe(true);
});