Skip to content

abso-core

Public
Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/src/engine/labels.ts?format=json
48 linesFirst import 100%Last change First import in version 0, 1 version changed it
1// Labels: what git calls commits. A label is a head atom plus everything it
2// needed, minus a base. Labels are stored on the graph (graph.setLabel).
3
4import type { Graph } from "./graph.ts";
5import { history } from "./sets.ts";
6import { type AtomId, EngineError, type Label } from "./types.ts";
7
8/** A label name or an atom id, resolved to an atom id (a label resolves to its head). */
9export function resolveRef(graph: Graph, ref: string): AtomId {
10 const label = graph.label(ref);
11 if (label) return label.head;
12 if (graph.has(ref)) return ref;
13 throw new EngineError("unknown-ref", `${ref} is neither a label nor an atom`);
16/** The full state a ref names: the closure of the atom (or the label's head). */
17export function stateOf(graph: Graph, ref: string): ReadonlySet<AtomId> {
18 return graph.closure(resolveRef(graph, ref));
21/**
22 * A label's contents, in history order: closure(head) minus closure(base);
23 * without a base, minus a0.
24 */
25export function labelContents(graph: Graph, label: Pick<Label, "head" | "base">): AtomId[] {
26 const include = graph.closure(label.head);
27 const exclude: ReadonlySet<AtomId> = label.base !== undefined ? stateOf(graph, label.base) : new Set(["a0"]);
28 return history(
29 graph,
30 [...include].filter((id) => !exclude.has(id)),
31 ).map((a) => a.id);
34/**
35 * Re-slice: any atom inside a label defines a smaller label (that atom plus
36 * its own closure, same base). Returns that smaller label's contents.
37 */
38export function reslice(graph: Graph, label: Pick<Label, "head" | "base">, atom: AtomId): AtomId[] {
39 if (!labelContents(graph, label).includes(atom)) {
40 throw new EngineError("not-in-set", `${atom} is not inside the label`);
41 }
42 return labelContents(graph, { head: atom, ...(label.base !== undefined ? { base: label.base } : {}) });
45/** Labels whose contents include the atom (for "why" output). */
46export function labelsContaining(graph: Graph, atom: AtomId): Label[] {
47 return graph.labels().filter((l) => labelContents(graph, l).includes(atom));