Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

1.5 KB
Raw Blame
import path from 'node:path';

const writers = new Map();

/** Worker/CLI owners register one DB-first writer for their own ledger only. */
export function registerSharedFileWriter(directory, beforeWrite) {
  const root = path.resolve(directory);
  if (writers.has(root)) throw new Error('Shared My Work writer already registered');
  writers.set(root, beforeWrite);
  return () => { if (writers.get(root) === beforeWrite) writers.delete(root); };
}

/**
 * The file's name inside the ledger, always with forward slashes because that
 * is how the adapter recognizes `progress/` files. Null when it is outside.
 */
export function ledgerRelativeName(root, filePath, paths = path) {
  const name = paths.relative(root, paths.resolve(filePath));
  if (!name || name === '..' || name.startsWith(`..${paths.sep}`) || paths.isAbsolute(name)) return null;
  return name.split(paths.sep).join('/');
}

/**
 * The registered writer commits the durable decision and returns the completion that writes the file (with the redacted
 * value, never the raw input), or null when the file is not one it records. options.readHash: the hash of the file the
 * value was built from, so the writer refuses a value built from a file another writer has replaced since.
 */
export function beforeSharedFileWrite(filePath, value, options) {
  for (const [root, writer] of writers) {
    const name = ledgerRelativeName(root, filePath);
    if (name) return writer(name, value, options);
  }
  return null;
}