Skip to content

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

1.9 KB
Raw Blame
/**
 * Reading a Session's source (`session_meta.source`) back from a Trace or from a forwarded meta.
 *
 * Every meta written since the source became required carries one of the six values. A Trace
 * kept from before may carry none (it was a person's conversation, or a desk or ticket Session
 * of company mode) or the retired `benchmark` (a Test Session `penguin run --source benchmark`
 * started, which `penguin run` now records as `cli`). Old Traces are never rewritten: each
 * reader narrows what it reads through this one function instead. Seeing the Trace alone, it
 * reads a missing source as `user`; the server, which also has the Session's index row, reads
 * one whose row was opened by the organization runtime as `company` instead
 * (`readRecordedSource` in the server's runtime/session-sources.ts).
 *
 * Removal: at the 0.3.0 release preparation the `benchmark` branch goes. The `undefined` branch
 * goes only together with a one-time rewrite of old heads, or stays for good — the user decides
 * then: reading never rewrites a head, so every Session created before the source was required
 * would otherwise become a malformed head, the way one without `provider` is.
 */
import type { SessionSource } from "./types.js";

const SESSION_SOURCES: ReadonlySet<unknown> = new Set<SessionSource>([
  "user",
  "api",
  "schedule",
  "subagent",
  "cli",
  "company",
]);

/** Narrows an untrusted `session_meta.source` to a SessionSource. */
export function normalizeSessionSource(value: unknown): SessionSource {
  if (SESSION_SOURCES.has(value)) return value as SessionSource;
  // compat(0.3.0): a meta written before the source was required has none; it was a person's.
  if (value === undefined) return "user";
  // compat(0.3.0): the retired `benchmark` marked Test Sessions `penguin run` started.
  if (value === "benchmark") return "cli";
  // Junk a third party wrote is never cast through: it reads as a person's conversation.
  return "user";
}