Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

3.6 KB
Raw Blame
/**
 * Where an engine's writing becomes live again after a relaunch — of the engine, by a resume, or of the
 * daemon, by a restart.
 *
 * Either way the engine can take a turn before the daemon has attached its transcript: a resume waits
 * on the process to be confirmed, a restart opens its request gate before the restored agents' attaches
 * have run, and every attach waits its turn in the attach queue. The attach folds what is on disk as
 * history, so a turn the engine began and finished in that window reached no window as a turn — no
 * `turn_started`, no `turn_ended`, the answer only in history. Found end to end, both ways: a message
 * typed into an agent's terminal the moment it reopened after a resume, and after a daemon restart, was
 * answered and never seen (e2e/terminal.e2e.ts).
 *
 * So the transcript's size is noted where the engine's own writing begins: by a resume just before it
 * launches the engine, and by the daemon at start for every conversation it restores. The attach that
 * follows folds the conversation only up to that byte and tails from there, so what was written after
 * it is live. A mark is committed only after an attach installs its state. Failed worker reads must
 * retain it: otherwise a retry reopens an interrupted turn as live. Time passing is not evidence that
 * the old turn resumed: a queued attach keeps its boundary until it commits or the session is released.
 */
import { statSync } from 'node:fs'

/** A transcript's size now, or null when there is no file to tail. One stat: nothing for a launch to wait on. */
export function transcriptSize(path: string): number | null {
  try { return statSync(path).size } catch { return null }
}

/**
 * Where a relaunched conversation's live writing begins, and whether a new engine was started on it. A
 * daemon's start marks every conversation it restores, including those whose engine kept running in
 * tmux; only a resume, or a restore that rebuilt the pane, starts a new engine. A turn left open before
 * a new engine started died with the engine before it: announced again at the attach, it showed the
 * interrupted message starting anew in every window (e2e/machine.e2e.ts, Codex after the tmux server
 * died mid-turn).
 */
export interface RelaunchMark {
  offset: number
  engineStarted: boolean
}

export function createRelaunchMarks() {
  const marks = new Map<string, RelaunchMark>()
  return {
    /** Before the engine is launched: its own writing starts at `offset`. */
    note(sessionId: string, offset: number, engineStarted = false): void {
      marks.set(sessionId, { offset, engineStarted })
    },
    /** A restore rebuilt this conversation's pane, so its engine is a new one. */
    engineStarted(sessionId: string): void {
      const mark = marks.get(sessionId)
      if (mark) mark.engineStarted = true
    },
    /** At the attach: retain the boundary through retries, however long its worker is unavailable. */
    read(sessionId: string): RelaunchMark | undefined {
      return marks.get(sessionId)
    },
    /** An installed attach acknowledges only the boundary it read, never a newer resume's. */
    complete(sessionId: string, mark: RelaunchMark): void {
      if (marks.get(sessionId) === mark) marks.delete(sessionId)
    },
    /** A stopped or unbound conversation has no pending attach to preserve. */
    forget(sessionId: string): void {
      marks.delete(sessionId)
    },
    /** How many marks are waiting for an attach — for tests and diagnostics. */
    get size(): number {
      return marks.size
    },
  }
}

export type RelaunchMarks = ReturnType<typeof createRelaunchMarks>