Skip to content

armgit / cogit

Public Imported from GitHub
0

Git for the intent behind agent-written code. Records what your coding agent decided and why, and links every commit to it.

6.2 KB
Raw Blame
import type { LineageViewModel, ViewNode, ViewSession } from "../../cli/src/view/model.js";

/**
 * Small helpers the map's UI shares: the "left off here" session, node labels,
 * and the panel's node detail. The layout lives in `gitLayout.ts` (both levels)
 * and `sessionLayout.ts` (the project's sessions).
 */

/** Everything the drill-down panel shows about a message. */
export type NodeDetail = {
  kind: ViewNode["kind"];
  title: string;
  summary?: string;
  why?: string;
  at?: string;
  decidedBy?: ViewNode["decidedBy"];
  alternatives?: ViewNode["alternatives"];
  commits?: string[];
  commitTags?: ViewNode["commitTags"];
  /** Numstat totals of the linked commits (Story 10.3). */
  change?: ViewNode["change"];
  files?: string[];
  work?: ViewNode["work"];
  agentDecisions?: ViewNode["agentDecisions"];
  /** The Shape of what the change does (Story 13.6). */
  shape?: ViewNode["shape"];
  addedAt?: string;
  touchedAt?: string;
  parent?: string | null;
  links?: string[];
  /** A user message the record does not hold yet. */
  unsummarized?: true;
  /** Owning session, so the drill-down panel can show and resume it. */
  sessionId: string;
  /** The node's id in its session, which for a message is also its chat turn's id. */
  nodeId: string;
};

export function toNodeDetail(node: ViewNode, sessionId: string): NodeDetail {
  return { ...node, sessionId, nodeId: node.id };
}

/** Where the work left off: the newest message of the most recently updated session. */
export interface Head {
  sessionId: string;
  messageId: string | null;
}

function time(value: string): number {
  const at = Date.parse(value);
  return Number.isNaN(at) ? Number.NEGATIVE_INFINITY : at;
}

function latestSession(model: LineageViewModel): ViewSession | undefined {
  const all = model.sessions;
  return all.sort(
    (a, b) => time(b.updatedAt) - time(a.updatedAt) || a.sessionId.localeCompare(b.sessionId),
  )[0];
}

/**
 * "Left off here": the newest message the user sent, in whichever session it
 * is — not the most recently updated record, since fixing an old session's
 * record must not move HEAD there. Sessions whose messages carry no time fall
 * back to the most recently updated session and its last message.
 */
export function headOf(model: LineageViewModel): Head | null {
  let best: { sessionId: string; messageId: string; at: number } | null = null;
  for (const session of model.sessions) {
    for (const message of session.messages) {
      const at = time(message.at ?? "");
      if (at === Number.NEGATIVE_INFINITY) continue;
      if (best === null || at > best.at)
        best = { sessionId: session.sessionId, messageId: message.id, at };
    }
  }
  if (best !== null) return { sessionId: best.sessionId, messageId: best.messageId };
  const latest = latestSession(model);
  if (!latest) return null;
  return {
    sessionId: latest.sessionId,
    messageId: latest.messages[latest.messages.length - 1]?.id ?? null,
  };
}

/** The highlight key of a session in the outline and the map. */
export function sessionId(id: string): string {
  return `session:${id}`;
}

/** A stable message id (Story 8.5): `h` plus 12 hex. Older records stay `m1`. */
export function isMessageHash(id: string): boolean {
  return /^h[0-9a-f]{12}$/.test(id);
}

/** `h3f2a1b8`: what the panel shows and `cogit show` accepts. */
export function shortHash(id: string): string {
  return isMessageHash(id) ? id.slice(0, 8) : id;
}

/**
 * Node ids are flat per session: the outline and the map agree on the same id
 * when the outline highlights a message.
 */
export function nodeId(sessionGroup: string, id: string): string {
  return `node:${sessionGroup}:${id}`;
}

/** The messages just before and after a message in its session, by send order. */
export function messageNeighbors(
  messages: ViewNode[],
  id: string,
): { previous: ViewNode | null; next: ViewNode | null } {
  const index = messages.findIndex((message) => message.id === id);
  if (index < 0) return { previous: null, next: null };
  return { previous: messages[index - 1] ?? null, next: messages[index + 1] ?? null };
}

/**
 * A point the map centres on. `left` and `right` are the canvas span of the
 * row it belongs to (the lanes plus the message text), so the whole row is
 * framed, not only the dot.
 */
export type MapPoint = { x: number; y: number; left?: number; right?: number };

/** Space kept between the canvas edge and a row too wide to centre. */
export const CENTRE_MARGIN = 24;

/**
 * The viewport that puts `point` in the middle of the canvas. A row wider than
 * the canvas starts at the left edge (plus a margin) instead, so its lanes and
 * the start of its text stay in view.
 */
export function centreViewport(
  point: MapPoint,
  canvas: { width: number; height: number },
  zoom: number,
): { x: number; y: number; zoom: number } {
  const y = canvas.height / 2 - point.y * zoom;
  const { left, right } = point;
  if (left !== undefined && right !== undefined) {
    const span = (right - left) * zoom;
    if (span > canvas.width - 2 * CENTRE_MARGIN) return { zoom, y, x: CENTRE_MARGIN - left * zoom };
    return { zoom, y, x: canvas.width / 2 - ((left + right) / 2) * zoom };
  }
  return { zoom, y, x: canvas.width / 2 - point.x * zoom };
}

/** The band moves in steps this tall, so a pan re-renders only when it crosses one. */
export const BAND_STEP = 400;

/**
 * The canvas rows worth drawing: what the viewport shows plus one screen above
 * and below, rounded out to `BAND_STEP`. With 10,000 sessions open, drawing
 * every one froze the page for seconds; only these are drawn, the rest wait
 * off-screen. Null (draw everything) until the canvas has a height.
 */
export function visibleBand(
  transform: readonly [number, number, number],
  height: number,
): [number, number] | null {
  if (height <= 0) return null;
  const [, y, zoom] = transform;
  const top = -y / zoom;
  const span = height / zoom;
  return [
    Math.floor((top - span) / BAND_STEP) * BAND_STEP,
    Math.ceil((top + 2 * span) / BAND_STEP) * BAND_STEP,
  ];
}

/** True when a block from `top`, `height` tall, reaches into the band. */
export function inBand(top: number, height: number, band: [number, number] | null): boolean {
  return band === null || (top + height >= band[0] && top <= band[1]);
}