Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

3.6 KB
Raw Blame
import type {
  ContextCompactionFallback,
  ContextCompactionMark,
  ContextCompactionRecord,
} from "./types.js";

/**
 * Identity of the checkpoint governing a session's next model request, or
 * `null` when the session has never compacted. A host that lost the reply to a
 * manual compaction compares it before and after the call: the sidecar persists
 * its checkpoint through host-core regardless, so a missing reply is not
 * evidence that the compaction failed (issue #795).
 */
export function compactionRecordId(session: unknown): string | null {
  if (typeof session !== "object" || session === null) return null;
  const compaction = (session as { compaction?: unknown }).compaction;
  if (typeof compaction !== "object" || compaction === null) return null;
  const id = (compaction as { id?: unknown }).id;
  return typeof id === "string" ? id : null;
}

/**
 * The generation counter rides inside the checkpoint's opaque `details` value:
 * the host persists that field verbatim, so it survives the transcript round
 * trip without a record schema change.
 */
export function checkpointGeneration(details: unknown): number {
  const value = (details as { generation?: unknown } | null | undefined)
    ?.generation;
  return typeof value === "number" && Number.isFinite(value) && value >= 1
    ? Math.floor(value)
    : 1;
}

/** Same four-characters-per-token heuristic the runtime uses for estimates. */
export function estimateSummaryTokens(summary: string): number {
  return Math.ceil(summary.length / 4);
}

/**
 * Whether the checkpoint's summary came from the model. The no-summary family
 * stamps `strategy: "fresh_window"` into the same opaque `details` value and
 * fills the summary with a fixed rollover marker instead.
 */
export function checkpointSummarized(details: unknown): boolean {
  const value = (details as { strategy?: unknown } | null | undefined)?.strategy;
  return value !== "fresh_window";
}

/**
 * Whether the checkpoint is the retained-tail recovery written after summary
 * generation failed (ADR 0049). Its `summary` is a carried-forward earlier
 * summary plus a fixed recovery notice, never a fresh model summary.
 */
export function checkpointFallback(
  details: unknown,
): ContextCompactionFallback | undefined {
  const value = (details as { fallback?: unknown } | null | undefined)?.fallback;
  return value === "retained_tail" ? value : undefined;
}

/**
 * Estimated context after the checkpoint, stamped into the same opaque
 * `details` value as the generation so it survives a reload without a record
 * schema change. Old checkpoints have none.
 */
export function checkpointContextTokens(details: unknown): number | undefined {
  const value = (details as { contextTokens?: unknown } | null | undefined)
    ?.contextTokens;
  return typeof value === "number" && Number.isFinite(value) && value > 0
    ? Math.round(value)
    : undefined;
}

export function contextCompactionMark(
  record: ContextCompactionRecord,
): ContextCompactionMark {
  const fallback = checkpointFallback(record.details);
  const contextTokens = checkpointContextTokens(record.details);
  return {
    id: record.id,
    createdAt: record.createdAt,
    ...(record.providerId ? { providerId: record.providerId } : {}),
    ...(record.modelId ? { modelId: record.modelId } : {}),
    throughMessageId: record.throughMessageId,
    generation: checkpointGeneration(record.details),
    summaryTokens: estimateSummaryTokens(record.summary ?? ""),
    summarized: checkpointSummarized(record.details),
    ...(contextTokens !== undefined ? { contextTokens } : {}),
    ...(fallback ? { fallback } : {}),
  };
}