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

4.4 KB
Raw Blame
/**
 * Observer / KnowledgeAgent tool-attempt audit log.
 *
 * SECURITY-SENSITIVE. The Observer and KnowledgeAgent SDK sessions are
 * configured to forbid all tool use (see src/sdk/hardened-options.ts). This
 * module records every attempted tool invocation to an append-only NDJSON log
 * so that prompt-injection attempts (the model emitting a tool_use the SDK then
 * denies) leave an authoritative, persistent trail for post-incident review.
 *
 * DEPENDENCY-FREE BY DESIGN: this util intentionally does NOT import the
 * application logger. The logger writes through its own code path; if it ever
 * routed through the audit recorder we would risk infinite recursion. Mirrors
 * the best-effort appendFileSync pattern in src/utils/logger.ts (try/catch,
 * never throw, fall back to stderr).
 */

import { appendFileSync, statSync, renameSync, existsSync } from 'fs';
import { join } from 'path';
import { DATA_DIR } from '../shared/paths.js';

const AUDIT_LOG_PATH = join(DATA_DIR, 'observer-audit.log');
const ROTATE_AT_BYTES = 50 * 1024 * 1024; // 50MB
const KEEP_GENERATIONS = 3;
const MAX_INPUT_BYTES = 4096;

export interface ObserverToolAttempt {
  source: 'Observer' | 'KnowledgeAgent';
  sessionDbId?: number;
  contentSessionId?: string;
  project?: string;
  tool_name: string;
  tool_input: unknown;
  result: 'allowed' | 'denied' | 'error';
  error_message?: string;
}

/** Exposed for tests; never mutate at runtime. */
export function getObserverAuditLogPath(): string {
  return AUDIT_LOG_PATH;
}

function rotateIfNeeded(): void {
  try {
    if (!existsSync(AUDIT_LOG_PATH)) return;
    const { size } = statSync(AUDIT_LOG_PATH);
    if (size < ROTATE_AT_BYTES) return;
    for (let i = KEEP_GENERATIONS - 1; i >= 1; i--) {
      const from = `${AUDIT_LOG_PATH}.${i}`;
      const to = `${AUDIT_LOG_PATH}.${i + 1}`;
      if (existsSync(from)) renameSync(from, to);
    }
    renameSync(AUDIT_LOG_PATH, `${AUDIT_LOG_PATH}.1`);
  } catch {
    // best-effort rotation; never fail the recording call
  }
}

/**
 * Cut `s` to at most `maxBytes` UTF-8 bytes, on a code point boundary.
 *
 * The cap is a byte budget (it bounds how fast the log rotates away its own
 * history), so it cannot be measured in UTF-16 code units: 4096 CJK characters
 * are 12 KB, and a `.length` check waved that through. A plain
 * `Buffer.subarray` cut is not enough either — it can end inside a multi-byte
 * sequence, and a code-unit cut can end between the two halves of an emoji,
 * leaving a lone surrogate in the audit trail.
 */
export function truncateObserverAuditInput(input: unknown, maxBytes = MAX_INPUT_BYTES): string {
  try {
    const s = typeof input === 'string' ? input : JSON.stringify(input);
    if (s === undefined) return '[UNSERIALIZABLE]';
    if (Buffer.byteLength(s, 'utf8') <= maxBytes) return s;
    let bytes = 0;
    let end = 0;
    for (const codePoint of s) {
      const size = Buffer.byteLength(codePoint, 'utf8');
      if (bytes + size > maxBytes) break;
      bytes += size;
      end += codePoint.length;
    }
    return s.slice(0, end) + '…[TRUNCATED]';
  } catch {
    // [ANTI-PATTERN IGNORED]: JSON.stringify of arbitrary tool input is expected
    // to fail on circular/unserializable values; the '[UNSERIALIZABLE]' placeholder
    // below is the designed recovery, and this util is dependency-free by design
    // (see header) so it must not route through the application logger.
    return '[UNSERIALIZABLE]';
  }
}

function writeAuditEntry(attempt: ObserverToolAttempt): void {
  rotateIfNeeded();
  const entry = {
    ts: new Date().toISOString(),
    source: attempt.source,
    sessionDbId: attempt.sessionDbId ?? null,
    contentSessionId: attempt.contentSessionId ?? null,
    project: attempt.project ?? null,
    tool_name: attempt.tool_name,
    tool_input: truncateObserverAuditInput(attempt.tool_input),
    result: attempt.result,
    error_message: attempt.error_message ?? null,
  };
  appendFileSync(AUDIT_LOG_PATH, JSON.stringify(entry) + '\n', 'utf8');
}

/**
 * Record a single attempted tool invocation. Best-effort: a failed write is
 * logged to stderr and swallowed so the SDK message loop is never broken by an
 * audit failure.
 */
export function recordObserverToolAttempt(attempt: ObserverToolAttempt): void {
  try {
    writeAuditEntry(attempt);
  } catch (err) {
    process.stderr.write(
      `[OBSERVER-AUDIT] failed to write: ${err instanceof Error ? err.message : String(err)}\n`
    );
  }
}