Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

15.9 KB
Raw Blame
/**
 * sessions table repo:
 * Session index, approval mode, and auto-generated title; Session-level routes use this to look up project ownership.
 */
import type { ThinkingLevelName } from "@adelie/core/interfaces";
import type { ApprovalMode } from "../../api/types.js";
import type { SandboxSettings } from "@adelie/core/plugin";
import { Component, Use } from "@adelie/core/kernel";
import type { Db } from "../../hmr/capabilities.js";
import type { SessionIndex } from "../../mechanisms/sessions.js";

export interface SessionRow {
  sessionId: string;
  projectId: string;
  agentId: string;
  /**
   * Provider group of the session's **current** model (pairs with `modelId` to form the model
   * reference). Moves with each in-session switch (`updateModel`); the Trace's latest
   * `session_meta` is the durable truth, and the runtime reconciles a row it disagrees with.
   */
  provider: string;
  /** Upstream model_id of the session's current model (sent as-is to AgentHub; never concatenated). */
  modelId: string;
  workspace: string;
  approvalMode: ApprovalMode;
  /**
   * Thinking level pinned for THIS session (PATCH /api/sessions/:id); NULL = not pinned,
   * so runs that carry no level of their own follow the Agent config as before. Sessions
   * are never inserted with one — pinning is an explicit act inside a live conversation.
   */
  thinkingLevel?: ThinkingLevelName | null;
  /** Auto-generated session title; NULL = not yet generated (frontend shows "New Conversation"). */
  title: string | null;
  /** Archive timestamp, ISO; NULL = not archived (omitting on insert defaults to NULL). */
  archivedAt?: string | null;
  /**
   * Creating client: "web" (created via the Web App), "cli" (created through the API by
   * the CLI, or adopted from a Trace a legacy CLI-direct run left behind), "org" (opened by
   * the organization runtime — a desk or a ticket session — or spawned as a sub-session of
   * one, which inherits the stamp at registration), "api" (created by an Agent API run — the
   * only rows the API may continue); NULL = legacy row from before the column existed,
   * treated as web. Free text in SQLite, so a new value needs no migration.
   * Provenance that outlives its organization: development mode's list is the one reader
   * that filters on it (through the list's `excludeOrg`), and it hides "org" rows whether or
   * not the organization still exists. The schedule/subagent SOURCE is deliberately NOT a row field — core
   * session_meta in the Trace stays the single source of truth for it
   * (runtime/session-sources.ts); `client` is a separate, DB-only axis that meta never
   * records.
   */
  client?: "web" | "cli" | "org" | "api" | null;
  /** Cache: a Trace record exists (set at task start / adoption / subagent registration; backfilled by list hydration). */
  hasTrace?: boolean;
  /**
   * Last activity this server drove for the session, ISO: stamped once when a run starts
   * and once when it ends (see SessionManager.drive — a goal run is ONE drive over all its
   * rounds, so it stamps twice, not per round). Set to createdAt at insert, and only ever
   * moves forward (markDriven/touchLastActive clamp with MAX, so a backwards clock step
   * cannot regress it). Rows this server never drives — CLI-adopted Sessions, subagent
   * rows (their activity is driven through the parent's entry) — keep createdAt.
   */
  lastActiveAt: string;
  createdAt: string;
  /**
   * The Session's own sandbox policy, snapshotted from the server's settings at creation.
   * NULL = a row from before the column: it takes the settings at its next command.
   */
  sandbox?: SandboxSettings | null;
}

/** A stored policy, or null when the column is empty or holds something unreadable. */
function parseSandbox(raw: unknown): SandboxSettings | null {
  if (typeof raw !== "string" || raw === "") return null;
  try {
    const value = JSON.parse(raw) as Partial<SandboxSettings> | null;
    const modes = ["read-only", "workspace-write", "danger-full-access"];
    if (value === null || typeof value !== "object" || !modes.includes(value.mode as string)) {
      return null;
    }
    return {
      mode: value.mode as SandboxSettings["mode"],
      ...(value.network === "none" || value.network === "local" ? { network: value.network } : {}),
      ...(Array.isArray(value.maskPaths)
        ? { maskPaths: value.maskPaths.filter((p): p is string => typeof p === "string") }
        : {}),
      ...(typeof value.writableTemp === "boolean" ? { writableTemp: value.writableTemp } : {}),
    };
  } catch {
    return null;
  }
}

function mapRow(r: Record<string, unknown>): SessionRow {
  return {
    sessionId: r.session_id as string,
    projectId: r.project_id as string,
    agentId: r.agent_id as string,
    provider: r.provider as string,
    modelId: r.model_id as string,
    workspace: r.workspace as string,
    approvalMode: r.approval_mode as ApprovalMode,
    thinkingLevel: (r.thinking_level as ThinkingLevelName | null) ?? null,
    title: (r.title as string | null) ?? null,
    archivedAt: (r.archived_at as string | null) ?? null,
    client: (r.client as "web" | "cli" | "org" | "api" | null) ?? null,
    hasTrace: (r.has_trace as number) === 1,
    // The open-time backfill leaves no NULLs; the coalesce only hardens against a row
    // somehow inserted as NULL (degrades to createdAt instead of surfacing undefined).
    lastActiveAt: (r.last_active_at as string | null) ?? (r.created_at as string),
    createdAt: r.created_at as string,
    sandbox: parseSandbox(r.sandbox),
  };
}

@Component()
export class SessionsRepo implements SessionIndex {
  @Use() private readonly db!: Db;

  insert(row: SessionRow): void {
    this.runInsert("INSERT", row);
  }

  /** Idempotent insert: used when Trace directory discovery backfills a row (concurrent listing discovering the same Session no longer triggers a UNIQUE violation). */
  insertOrIgnore(row: SessionRow): void {
    this.runInsert("INSERT OR IGNORE", row);
  }

  /**
   * Allocate the source Session's next persistent fork number and insert its fork atomically.
   * Keeping both writes in one transaction prevents concurrent requests from sharing a number,
   * while retaining `fork_count` on the source means deleting an older fork never reuses it.
   */
  insertFork(sourceSessionId: string, row: SessionRow): SessionRow {
    this.db.exec("BEGIN IMMEDIATE");
    try {
      const source = this.db
        .prepare(
          `UPDATE sessions SET fork_count = fork_count + 1 WHERE session_id = ?
           RETURNING title, fork_count`,
        )
        .get(sourceSessionId) as { title: string | null; fork_count: number } | undefined;
      if (!source) throw new Error(`Source Session not found: ${sourceSessionId}`);

      const forkTitle = source.title
        ? `${source.title} (${source.fork_count})`
        : `(${source.fork_count})`;
      const forkRow = { ...row, title: forkTitle };
      this.runInsert("INSERT", forkRow);
      this.db.exec("COMMIT");
      return forkRow;
    } catch (err) {
      this.db.exec("ROLLBACK");
      throw err;
    }
  }

  /**
   * The two inserts' shared column list and binding order (they differ only in conflict
   * handling). `last_active_at` defaults **in SQL** (`COALESCE(?, ?)` over the row's own
   * created_at, bound twice — SQLite forbids referencing a sibling column inside VALUES,
   * and a column added by ALTER TABLE cannot be given a DEFAULT): the column is nullable
   * for legacy reasons, so this is the one place that can guarantee it is never written
   * NULL, whatever a caller passes.
   */
  private runInsert(verb: "INSERT" | "INSERT OR IGNORE", row: SessionRow): void {
    this.db
      .prepare(
        `${verb} INTO sessions (session_id, project_id, agent_id, provider, model_id, workspace, approval_mode, title, client, has_trace, last_active_at, created_at, sandbox)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, COALESCE(?, ?), ?, ?)`,
      )
      .run(
        row.sessionId,
        row.projectId,
        row.agentId,
        row.provider,
        row.modelId,
        row.workspace,
        row.approvalMode,
        row.title,
        row.client ?? null,
        row.hasTrace ? 1 : 0,
        row.lastActiveAt ?? null,
        row.createdAt,
        row.createdAt,
        row.sandbox ? JSON.stringify(row.sandbox) : null,
      );
  }

  /**
   * Stamp `client = "org"` on the rows an organization's files name, leaving the ones
   * already stamped alone. The reconcile pass calls it with every session the desk ledger
   * and the tickets' `sessions` fields claim, so a row opened before the marker existed
   * carries the organization's provenance from the next pass on — and keeps it when the
   * organization is deleted or company mode is switched off, which is exactly when the
   * organization caches can no longer answer for it. Chunked because the id list is
   * unbounded while SQLite's parameter count is not.
   */
  markOrgClient(sessionIds: readonly string[]): void {
    const stmt = (n: number): string =>
      `UPDATE sessions SET client = 'org' WHERE session_id IN (${Array.from({ length: n }, () => "?").join(", ")}) AND client IS NOT 'org'`;
    for (let i = 0; i < sessionIds.length; i += 400) {
      const chunk = sessionIds.slice(i, i + 400);
      this.db.prepare(stmt(chunk.length)).run(...chunk);
    }
  }

  /** Flip the has_trace cache once a Trace record exists (discovery hydration); idempotent. */
  markHasTrace(sessionId: string): void {
    this.db.prepare("UPDATE sessions SET has_trace = 1 WHERE session_id = ?").run(sessionId);
  }

  /**
   * A driven run touched this Session: flip the has_trace cache and stamp last_active_at in
   * ONE statement (drive's run start — one WAL commit instead of two).
   * `MAX(COALESCE(...))` keeps the stamp monotonic: a backwards clock step (NTP, resume
   * from suspend) leaves the stored value alone rather than regressing the row.
   */
  markDriven(sessionId: string, at: string): void {
    this.db
      .prepare(
        `UPDATE sessions SET has_trace = 1, last_active_at = MAX(COALESCE(last_active_at, ''), ?)
         WHERE session_id = ?`,
      )
      .run(at, sessionId);
  }

  /** Stamp last_active_at alone (drive's run end); monotonic like markDriven. */
  touchLastActive(sessionId: string, at: string): void {
    this.db
      .prepare(
        `UPDATE sessions SET last_active_at = MAX(COALESCE(last_active_at, ''), ?)
         WHERE session_id = ?`,
      )
      .run(at, sessionId);
  }

  findById(sessionId: string): SessionRow | null {
    const r = this.db.prepare("SELECT * FROM sessions WHERE session_id = ?").get(sessionId);
    return r ? mapRow(r) : null;
  }

  /**
   * An Agent's rows, newest first (the list order the sidebar shows; served by
   * idx_sessions_agent_created). Never filtered by `client` — every row is listed
   * whichever client created it.
   */
  listByAgent(projectId: string, agentId: string): SessionRow[] {
    const rows = this.db
      .prepare(
        `SELECT * FROM sessions WHERE project_id = ? AND agent_id = ?
         ORDER BY created_at DESC, session_id DESC`,
      )
      .all(projectId, agentId);
    return rows.map(mapRow);
  }

  listByProject(projectId: string): SessionRow[] {
    const rows = this.db.prepare("SELECT * FROM sessions WHERE project_id = ?").all(projectId);
    return rows.map(mapRow);
  }

  /**
   * The most recently active row across several Projects at once — the admin user backend asks
   * it per account, whose Sessions are spread over every Project that account owns, so it must
   * be one answer rather than one answer per Project.
   *
   * The ordering key is the activity order's (`lastActiveAt` descending, `sessionId` descending
   * to break ties), computed here as code points like every other activity comparison — no
   * `localeCompare`, whose collation the SQL-side order would not share. `last_active_at` is
   * coalesced to `created_at` for the same reason `mapRow` does it: a row nothing has driven
   * yet is active from its creation.
   *
   * Chunked like `markOrgClient`: how many Projects an account owns is unbounded while SQLite's
   * parameter count is not, so the winners of each chunk are compared against each other.
   */
  latestByProjects(projectIds: readonly string[]): SessionRow | null {
    let best: SessionRow | null = null;
    for (let i = 0; i < projectIds.length; i += 400) {
      const chunk = projectIds.slice(i, i + 400);
      const placeholders = chunk.map(() => "?").join(", ");
      const r = this.db
        .prepare(
          `SELECT * FROM sessions WHERE project_id IN (${placeholders})
           ORDER BY COALESCE(last_active_at, created_at) DESC, session_id DESC LIMIT 1`,
        )
        .get(...chunk) as Record<string, unknown> | undefined;
      if (!r) continue;
      const candidate = mapRow(r);
      if (best === null) {
        best = candidate;
        continue;
      }
      // The candidate wins when it is the more recent of the two under the activity order.
      const moreRecent =
        candidate.lastActiveAt !== best.lastActiveAt
          ? candidate.lastActiveAt > best.lastActiveAt
          : candidate.sessionId > best.sessionId;
      if (moreRecent) best = candidate;
    }
    return best;
  }

  updateApprovalMode(sessionId: string, mode: ApprovalMode): void {
    this.db
      .prepare("UPDATE sessions SET approval_mode = ? WHERE session_id = ?")
      .run(mode, sessionId);
  }

  /** Replace the Session's sandbox policy snapshot (applies from its next command). */
  updateSandbox(sessionId: string, sandbox: SandboxSettings): void {
    this.db
      .prepare("UPDATE sessions SET sandbox = ? WHERE session_id = ?")
      .run(JSON.stringify(sandbox), sessionId);
  }

  /** Pin the session's thinking level (runs without one of their own then use it). */
  updateThinkingLevel(sessionId: string, level: ThinkingLevelName): void {
    this.db
      .prepare("UPDATE sessions SET thinking_level = ? WHERE session_id = ?")
      .run(level, sessionId);
  }

  updateTitle(sessionId: string, title: string): void {
    this.db.prepare("UPDATE sessions SET title = ? WHERE session_id = ?").run(title, sessionId);
  }

  /** The session's current model after an in-session switch (or a reconcile against its Trace). */
  updateModel(sessionId: string, provider: string, modelId: string): void {
    this.db
      .prepare("UPDATE sessions SET provider = ?, model_id = ? WHERE session_id = ?")
      .run(provider, modelId, sessionId);
  }

  /**
   * Writes only if the title is still NULL. Subagent-session registration and Trace
   * directory discovery backfill can race on the same row, and both are insert-only:
   * whichever inserts first determines the title. Discovery backfill can only supply NULL,
   * so this method fills the title back in without overwriting an existing one (including
   * a user rename or an already-generated title).
   */
  updateTitleIfNull(sessionId: string, title: string): void {
    this.db
      .prepare("UPDATE sessions SET title = ? WHERE session_id = ? AND title IS NULL")
      .run(title, sessionId);
  }

  /** Archive / unarchive (archivedAt = ISO or NULL). */
  setArchived(sessionId: string, archivedAt: string | null): void {
    this.db
      .prepare("UPDATE sessions SET archived_at = ? WHERE session_id = ?")
      .run(archivedAt, sessionId);
  }

  /** Self-healing: after rebuilding a broken Session with no Trace, update the primary key to the new id. */
  replaceId(oldSessionId: string, newSessionId: string): void {
    this.db
      .prepare("UPDATE sessions SET session_id = ? WHERE session_id = ?")
      .run(newSessionId, oldSessionId);
  }

  deleteByAgent(projectId: string, agentId: string): void {
    this.db
      .prepare("DELETE FROM sessions WHERE project_id = ? AND agent_id = ?")
      .run(projectId, agentId);
  }

  deleteByProject(projectId: string): void {
    this.db.prepare("DELETE FROM sessions WHERE project_id = ?").run(projectId);
  }

  deleteById(sessionId: string): void {
    this.db.prepare("DELETE FROM sessions WHERE session_id = ?").run(sessionId);
  }
}