Skip to content

AI, fully local on your computer.

16.5 KB
Raw Blame
import {
  appendFileSync,
  mkdirSync,
  statSync,
  renameSync,
  existsSync,
  readFileSync,
  openSync,
  writeSync,
  fsyncSync,
  closeSync,
  rmSync
} from 'node:fs'
import { join } from 'node:path'
import { encrypt, decrypt, serializeBlob, deserializeBlob, isVaultNewerError } from './security/crypto'
import { ROTATED_ENCRYPTED_LOG_NAME, newerFrameAt, shredFile } from './workspace-vault'
import { logRedactionRoots, redactLogText } from './log-redact'

// Local-only rotating logger (spec §7.11 — diagnostics logs stay on device, never uploaded).
//
// app.log can carry file names, absolute paths and model ids (never document or chat text —
// that is a hard rule, enforced at call sites), so on an ENCRYPTED workspace the log is
// encrypted at rest exactly like the database and the document cache (spec §3.5). On a
// plaintext_dev workspace the log stays a plain rotating file (matching the unencrypted dev DB).
// The paths stay in the log at rest; what LEAVES it (readLogTail: the Diagnostics panel and
// Copy; readLogFull: Save log) is redacted (#975, `log-redact.ts`): the drive, app-data, home,
// temp and app folders by placeholder (`<drive>`, …), account names as `<user>`.
//
// The wrinkle is timing: logging starts at app launch, BEFORE the vault is unlocked, so the
// key does not exist yet (startup, policy load, and the unlock attempts themselves all log).
// We therefore BUFFER every line in memory from the start. When the workspace resolves we
// either:
//   - call `attachVaultKey(key)` (encrypted unlock/create) → flush the buffer into
//     `app.log.enc` and keep writing encrypted; or
//   - call `usesPlaintextLog()` (plaintext_dev) → flush the buffer into a plain `app.log`
//     and keep appending in plaintext.
// Until one of those is called nothing touches disk — pre-unlock lines live only in memory
// and are lost if the app is killed while still locked (an accepted trade for "no sensitive
// bytes on disk before the user has authenticated"). The same is true of a session spent
// entirely at the unlock gate (never unlocked): it stays in `buffering` and is discarded on
// quit, by the same trade.
//
// DURABILITY (encrypted mode): to avoid re-encrypting ~1 MB on every info/warn line, only an
// `error` line forces an immediate flush; info/warn ride the next error, a rotation, or the
// lock/quit flush. A `lock()`/quit (or an uncaughtException) flushes via detachVaultKey, but
// a hard kill (SIGKILL, OOM, power loss, drive removal) loses the info/warn accumulated since
// the last flush — wider than the pre-unlock window above, and the price of not thrashing the
// drive on the hot path.
//
// ROTATION keeps ONE prior generation: at MAX_BYTES the buffer is sealed to `app.1.log.enc`
// and the live buffer/`app.log.enc` reset. `app.1.log.enc` is recovery-only — readLogTail and
// loadEncrypted read only the live `.enc`/buffer, so the Diagnostics tail shows the current
// generation. This mirrors the plaintext rotation (the tail reads only `app.log`).
// A v1→v2 password change deletes the rotated generation instead of re-keying it (#241).

type Level = 'info' | 'warn' | 'error'

const MAX_BYTES = 1_000_000 // rotate at ~1 MB
/** Hard cap on the in-memory buffer so a chatty session can't grow it without bound. Set
 *  above MAX_BYTES so the encrypted log can actually ROTATE at MAX_BYTES (a generation is
 *  moved to `app.1.log.enc`) before the cap ever drops lines; the cap is only the backstop
 *  for the pre-unlock window, where rotation can't run (no key yet). */
const BUFFER_MAX_BYTES = 2 * MAX_BYTES

let logDir: string | null = null
/** Plaintext log path — used only on a plaintext_dev workspace. */
let plainLogFile: string | null = null
/** Encrypted-at-rest log path — used only on an encrypted workspace once unlocked. */
let encLogFile: string | null = null

type Mode = 'buffering' | 'plaintext' | 'encrypted'
let mode: Mode = 'buffering'

/** The vault key while an encrypted workspace is unlocked; null otherwise. Never persisted. */
let vaultKey: Buffer | null = null

/**
 * The full current-log text held in memory. The source of truth in `buffering` and
 * `encrypted` modes (the on-disk `.enc` is a snapshot of this); unused in `plaintext` mode
 * (the plain file is the source of truth there). Trimmed to BUFFER_MAX_BYTES on append.
 */
let buffer = ''

/**
 * #845: `app.log.enc` is in a frame a NEWER build wrote. This build cannot read it and must not replace it, so for the
 * rest of the session nothing persists or rotates over it: the session's lines stay in the in-memory buffer only
 * (readable on Diagnostics, gone at lock or quit, like a session spent at the unlock gate).
 */
let encLogNewer = false

/**
 * #845: the rotated `app.1.log.enc` is in a frame a newer build wrote. Only a rotation would overwrite it, so only the
 * rotation is skipped: `app.log.enc` (this build's own) keeps persisting, and without the rotation it holds up to the
 * buffer's cap (`BUFFER_MAX_BYTES`, oldest lines dropped first) instead of `MAX_BYTES`. The simpler of the two honest
 * options: the live log goes on working, and the newer generation stays as it is.
 */
let rotatedLogNewer = false

export function initLogging(directory: string): void {
  mkdirSync(directory, { recursive: true })
  logDir = directory
  plainLogFile = join(directory, 'app.log')
  encLogFile = join(directory, 'app.log.enc')
  mode = 'buffering'
  vaultKey = null
  buffer = ''
  encLogNewer = false
  rotatedLogNewer = false
}

/** Drop whole lines from the FRONT of `text` until it is within `maxBytes` UTF-8 bytes, so the
 *  result never starts mid-line. Byte-based (not `.length`): a multibyte path/name is 1 char
 *  but several bytes, and the on-disk `.enc` and the pre-unlock backstop are byte budgets. */
function trimToByteCap(text: string, maxBytes: number): string {
  while (Buffer.byteLength(text, 'utf8') > maxBytes) {
    const cut = text.indexOf('\n')
    if (cut === -1) return '' // a single line longer than the cap: drop it whole
    text = text.slice(cut + 1)
  }
  return text
}

/** Append a finished line to the in-memory buffer, trimming to the size cap (drop oldest). */
function pushToBuffer(line: string): void {
  buffer += line
  buffer = trimToByteCap(buffer, BUFFER_MAX_BYTES)
}

/** Write `blob` to `path` atomically: temp file + fsync + rename. */
function writeFileAtomicSync(path: string, blob: Buffer): void {
  const tmp = `${path}.tmp`
  const fd = openSync(tmp, 'w')
  try {
    writeSync(fd, blob)
    fsyncSync(fd)
  } finally {
    closeSync(fd)
  }
  renameSync(tmp, path)
}

/**
 * full-audit 2026-07-12b SEC-1 — the changePassword v1→v2 zero-key window. This module holds
 * the vault key Buffer BY REFERENCE (`encryptionKey()` hands out the live buffer), and the v1
 * migration branch of `changePassword` zero-fills the retired buffer IN PLACE while logging
 * still references it; `rekeyVaultLog` re-attaches the new key only AFTER changePassword
 * returns (registerWorkspaceIpc). A `log.error` inside that window would otherwise atomically
 * rewrite `app.log.enc` — the whole-session metadata log — under 32 zero bytes: a GCM-valid
 * blob anyone can decrypt (the §48 SEC-1 false-assurance shape). So both encrypted write
 * paths refuse an all-zero key. The refused line is NOT lost: it stays in the in-memory
 * buffer and rides the next flush once the live key is re-attached. (The belt lives here and
 * not as a rekey call inside workspace-vault: logging already imports `shredFile` FROM
 * workspace-vault, so a vault→logging call would create an import cycle.)
 */
function vaultKeyZeroed(): boolean {
  return vaultKey !== null && vaultKey.every((b) => b === 0)
}

/** Encrypt `buffer` and write it to `app.log.enc` atomically (temp + fsync + rename). */
function persistEncrypted(): void {
  if (!encLogFile || !vaultKey || vaultKeyZeroed() || encLogNewer) return
  try {
    writeFileAtomicSync(encLogFile, serializeBlob(encrypt(vaultKey, Buffer.from(buffer, 'utf8'))))
  } catch {
    /* never crash the app because logging failed */
  }
}

/** Decrypt an existing `app.log.enc` into `buffer`, or leave the buffer as-is on failure. */
function loadEncrypted(): void {
  if (!encLogFile || !vaultKey || !existsSync(encLogFile)) return
  try {
    const prior = decrypt(vaultKey, deserializeBlob(readFileSync(encLogFile))).toString('utf8')
    // Pre-unlock buffered lines (this session) come AFTER the persisted history. Trim on a
    // line boundary (not a raw byte slice) so the merged buffer never starts mid-line.
    buffer = trimToByteCap(prior + buffer, BUFFER_MAX_BYTES)
  } catch (err) {
    // A corrupt/foreign-key log is not recoverable — start fresh rather than throw. A newer build's frame is not
    // ours to replace (#845): keep this session's log in memory only.
    if (isVaultNewerError(err)) {
      encLogNewer = true
      write('warn', 'Diagnostics log: app.log.enc was written by a newer build; this session keeps its log in memory', {
        found: err.found
      })
    }
  }
}

/**
 * The encrypted workspace just unlocked: adopt its key, fold any persisted history in with
 * this session's buffered lines, switch to encrypted mode, and flush. Called from the
 * unlock/create path. Re-callable after a lock (re-attaches the same or a new key).
 */
export function attachVaultKey(key: Buffer): void {
  if (!encLogFile) return
  vaultKey = key
  mode = 'encrypted'
  loadEncrypted()
  // #845: a rotation would overwrite the previous generation; when a newer build wrote that one, it is skipped (see
  // `rotatedLogNewer`). Checked once per attach: the newer build cannot run beside this one (single instance).
  const rotatedNewer = logDir && !rotatedLogNewer ? newerFrameAt(join(logDir, ROTATED_ENCRYPTED_LOG_NAME)) : null
  if (rotatedNewer !== null) {
    rotatedLogNewer = true
    write('warn', 'Diagnostics log: app.1.log.enc was written by a newer build; this session does not rotate onto it', {
      found: rotatedNewer
    })
  }
  persistEncrypted()
  // Migration: an OLDER build (or a crash before this build's first lock) may have left a
  // plaintext `app.log`/`app.1.log` on an encrypted drive. Shred it now that the log is
  // encrypted — those diagnostics belong inside the vault, not beside it. Best-effort.
  if (plainLogFile && logDir) {
    shredFile(plainLogFile)
    shredFile(join(logDir, 'app.1.log'))
  }
}

/**
 * The workspace locked (or the app is quitting an encrypted vault): flush the buffer one last
 * time, then drop the key and fall back to in-memory buffering. The `.enc` file and the
 * in-memory buffer both survive, so a later re-unlock continues the same log.
 */
export function detachVaultKey(): void {
  if (mode === 'encrypted') {
    persistEncrypted()
    // F14 (audit-postmerge-2026-06-29): the in-memory buffer still holds this unlocked session's
    // lines (file names, paths, model ids, settings keys — METADATA, never document/chat text, by
    // the no-content-in-logs rule). In `buffering` mode readLogTail/readLogFull return the buffer,
    // and getLogTail/exportLog are intentionally NOT lock-gated (pre-unlock diagnostics), so leaving
    // it would let a still-mounted Diagnostics screen / compromised renderer read or EXPORT the
    // just-ended session AFTER lock. Zero it AFTER the final encrypted flush; the next unlock
    // repopulates it from `app.log.enc` via loadEncrypted(). The guard is `mode === 'encrypted'`,
    // so the pre-FIRST-unlock `buffering` window (where logs are deliberately readable for
    // troubleshooting) never reaches this branch and is preserved.
    buffer = ''
  }
  vaultKey = null
  mode = 'buffering'
}

/**
 * A password change rotated the at-rest key (v1→v2 regenerates the data key; v2 keeps it).
 * Re-seal the SAME in-memory buffer under the now-current key — do NOT re-load from disk:
 * the buffer already holds the full session-plus-history log (loaded at unlock), so a
 * `loadEncrypted` here would either fail-and-discard under a rotated key, or succeed and
 * DOUBLE the history under an unchanged one. Must be called while still encrypted (the key
 * has changed underneath the controller, not been dropped). No-op off the encrypted path.
 */
export function rekeyVaultLog(key: Buffer): void {
  if (!encLogFile || mode !== 'encrypted') return
  vaultKey = key
  persistEncrypted()
}

/**
 * The workspace is plaintext_dev (no key will ever exist): flush the buffered lines into a
 * plain `app.log` and append in plaintext from here on. Matches the unencrypted dev DB.
 */
export function usesPlaintextLog(): void {
  if (!plainLogFile) return
  mode = 'plaintext'
  vaultKey = null
  try {
    rotatePlainIfNeeded()
    if (buffer.length > 0) appendFileSync(plainLogFile, buffer)
  } catch {
    /* best-effort flush of the pre-unlock buffer */
  }
  buffer = ''
}

function rotatePlainIfNeeded(): void {
  if (!plainLogFile || !logDir) return
  try {
    if (existsSync(plainLogFile) && statSync(plainLogFile).size > MAX_BYTES) {
      renameSync(plainLogFile, join(logDir, 'app.1.log'))
    }
  } catch {
    /* rotation is best-effort */
  }
}

/** Rotate the encrypted log: re-encrypt the current buffer to `app.1.log.enc`, then reset. */
function rotateEncryptedIfNeeded(): void {
  if (!encLogFile || !logDir || !vaultKey || vaultKeyZeroed() || encLogNewer || rotatedLogNewer) return
  if (Buffer.byteLength(buffer, 'utf8') <= MAX_BYTES) return
  try {
    const blob = serializeBlob(encrypt(vaultKey, Buffer.from(buffer, 'utf8')))
    // Atomic, like persistEncrypted — a crash mid-write must not corrupt the rotated copy.
    writeFileAtomicSync(join(logDir, ROTATED_ENCRYPTED_LOG_NAME), blob)
    buffer = ''
    // Drop the live `.enc`; the next persistEncrypted re-creates it from the empty buffer.
    if (existsSync(encLogFile)) rmSync(encLogFile, { force: true })
  } catch {
    /* rotation is best-effort */
  }
}

function write(level: Level, message: string, meta?: unknown): void {
  const line = `${new Date().toISOString()} [${level.toUpperCase()}] ${message}${
    meta !== undefined ? ' ' + safeJson(meta) : ''
  }\n`
  // Always echo to the console for dev visibility.
  if (level === 'error') console.error(line.trimEnd())
  else if (level === 'warn') console.warn(line.trimEnd())
  else console.log(line.trimEnd())

  if (mode === 'plaintext') {
    if (!plainLogFile) return
    rotatePlainIfNeeded()
    try {
      appendFileSync(plainLogFile, line)
    } catch {
      /* never crash the app because logging failed */
    }
    return
  }

  // buffering (pre-unlock) and encrypted both accumulate in memory.
  pushToBuffer(line)
  if (mode === 'encrypted') {
    rotateEncryptedIfNeeded()
    // Persist on every error so a crash keeps the failure; info/warn ride the next error,
    // a rotation, or the lock/quit flush (re-encrypting ~1 MB per info line is wasteful).
    if (level === 'error') persistEncrypted()
  }
}

function safeJson(v: unknown): string {
  try {
    return JSON.stringify(v)
  } catch {
    return String(v)
  }
}

export const log = {
  info: (m: string, meta?: unknown) => write('info', m, meta),
  warn: (m: string, meta?: unknown) => write('warn', m, meta),
  error: (m: string, meta?: unknown) => write('error', m, meta)
}

/** The current log as stored: the plain file in plaintext mode, the in-memory buffer otherwise (the on-disk copy is
 *  encrypted). Never handed out as it is: the two readers below redact it (#975). */
function storedLogText(): string {
  if (mode === 'plaintext') {
    if (!plainLogFile || !existsSync(plainLogFile)) return ''
    try {
      return readFileSync(plainLogFile, 'utf8')
    } catch {
      return ''
    }
  }
  return buffer
}

/**
 * The last `maxLines` lines of the local log, for the Diagnostics screen (spec §7.11
 * "show recent local logs") and its Copy button. Read-only, local-only, never uploaded. Each
 * line is redacted on the way out (#975, `log-redact.ts`): the known folders by placeholder,
 * account names as `<user>`.
 */
export function readLogTail(maxLines = 200): string[] {
  const lines = storedLogText().split(/\r?\n/)
  while (lines.length > 0 && lines[lines.length - 1] === '') lines.pop()
  const roots = logRedactionRoots()
  return lines.slice(-maxLines).map((line) => redactLogText(line, roots))
}

/**
 * The ENTIRE current log as one string (not just the tail), for the Diagnostics "Save to
 * file" action. Same source as readLogTail, redacted the same way (#975). The export itself
 * is written as plaintext to a user-chosen location, a deliberate user action (spec §7.11):
 * the user is taking diagnostics OUT of the encrypted workspace to share with support.
 */
export function readLogFull(): string {
  return redactLogText(storedLogText(), logRedactionRoots())
}