Skip to content

backnotprop/pstack / skills/statechart

statechart

Unverified

Check a TLA+ model of a system that moves between states, such as a product flow, a protocol, or a service, and draw it as a Mermaid statechart. The report gives the shortest path to each state that breaks a rule. Use for /statechart, or to show a bug as a path through states.

FileKindSize
SKILL.mdskill md8.9 KB
references/render.mdmarkdown3.6 KB
scripts/backends/cfg.tsscript1.2 KB
scripts/backends/mermaid.tsscript9.0 KB
scripts/backends/plain-text.tsscript2.3 KB
scripts/backends/report.tsscript6.9 KB
scripts/frontends/config.tsscript10.4 KB
scripts/frontends/dot.tsscript2.4 KB
scripts/frontends/marks.tsscript3.2 KB
scripts/frontends/messages.tsscript1.8 KB
scripts/frontends/spec.tsscript4.1 KB
scripts/frontends/trace.tsscript988 B
scripts/frontends/value.tsscript4.6 KB
scripts/frontends/verdict.tsscript1.7 KB
scripts/frontends/view.tsscript4.3 KB
scripts/ir.tsscript14.8 KB
scripts/package.jsonmanifest233 B
scripts/passes/annotate.tsscript3.7 KB
scripts/passes/label.tsscript8.4 KB
scripts/passes/lift.tsscript2.8 KB
scripts/passes/loop-check.tsscript2.1 KB
scripts/passes/paths.tsscript1.9 KB
scripts/passes/reach.tsscript4.0 KB
scripts/passes/region-check.tsscript3.4 KB
scripts/passes/regions.tsscript9.3 KB
scripts/pipeline.tsscript2.9 KB
scripts/run.tsscript17.8 KB
scripts/statechartexecutablescript991 B
scripts/statechart.tsscript6.8 KB
scripts/test/ConfigOracle.javaother2.7 KB
scripts/test/chart.test.tsscript40.1 KB
scripts/test/charts/Braces.mmdother312 B
scripts/test/charts/Escapes.mmdother244 B
scripts/test/charts/Phases.mmdother584 B
scripts/test/cli.test.tsscript49.0 KB
scripts/test/config-fuzz.test.tsscript8.5 KB
scripts/test/corpus.test.tsscript13.7 KB
scripts/test/corpus/Counter/ChartRun.outother2.6 KB
scripts/test/corpus/Counter/Counter.cfgconfig64 B
scripts/test/corpus/Counter/Counter.tlaother185 B
scripts/test/corpus/Counter/chart.mmdother112 B
scripts/test/corpus/Counter/graph.dotother633 B
scripts/test/corpus/Counter/report.mdmarkdown243 B
scripts/test/corpus/Counter/sany.outother37.3 KB
scripts/test/corpus/Counter/verdict-1.outother2.1 KB
scripts/test/corpus/CounterAction/ChartRun.outother2.7 KB
scripts/test/corpus/CounterAction/CounterAction.cfgconfig89 B
scripts/test/corpus/CounterAction/CounterAction.tlaother255 B
scripts/test/corpus/CounterAction/chart.mmdother518 B
scripts/test/corpus/CounterAction/graph.dotother798 B
scripts/test/corpus/CounterAction/report.mdmarkdown436 B
scripts/test/corpus/CounterAction/sany.outother42.9 KB
scripts/test/corpus/CounterAction/verdict-1.exitother3 B
scripts/test/corpus/CounterAction/verdict-1.outother2.1 KB
scripts/test/corpus/CounterAction/verdict-2.exitother3 B
scripts/test/corpus/CounterAction/verdict-2.outother3.1 KB
scripts/test/corpus/CounterAction/verdict-3.outother2.6 KB
scripts/test/corpus/CounterDeadlock/ChartRun.outother2.7 KB
scripts/test/corpus/CounterDeadlock/CounterDeadlock.cfgconfig63 B
scripts/test/corpus/CounterDeadlock/CounterDeadlock.tlaother225 B
scripts/test/corpus/CounterDeadlock/chart.mmdother462 B
scripts/test/corpus/CounterDeadlock/graph.dotother633 B
scripts/test/corpus/CounterDeadlock/report.mdmarkdown624 B
scripts/test/corpus/CounterDeadlock/sany.outother41.8 KB
scripts/test/corpus/CounterDeadlock/verdict-1.exitother3 B
scripts/test/corpus/CounterDeadlock/verdict-1.outother2.1 KB
scripts/test/corpus/CounterDeadlock/verdict-2.exitother3 B
scripts/test/corpus/CounterDeadlock/verdict-2.outother3.0 KB
scripts/test/corpus/CounterFan/ChartRun.outother3.1 KB
scripts/test/corpus/CounterFan/CounterFan.cfgconfig37 B
scripts/test/corpus/CounterFan/CounterFan.tlaother338 B
scripts/test/corpus/CounterFan/chart.mmdother1.7 KB
scripts/test/corpus/CounterFan/graph.dotother5.3 KB
scripts/test/corpus/CounterFan/report.mdmarkdown239 B
scripts/test/corpus/CounterFan/sany.outother48.4 KB
scripts/test/corpus/CounterFan/verdict-1.exitother3 B
scripts/test/corpus/CounterFan/verdict-1.outother2.4 KB
scripts/test/corpus/CounterFan/verdict-2.outother2.6 KB
scripts/test/corpus/CounterGuard/ChartRun.exitother3 B
scripts/test/corpus/CounterGuard/ChartRun.outother3.3 KB
scripts/test/corpus/CounterGuard/CounterGuard.cfgconfig66 B
scripts/test/corpus/CounterGuard/CounterGuard.tlaother192 B
scripts/test/corpus/CounterGuard/chart.mmdother63 B
scripts/test/corpus/CounterGuard/graph.dotother302 B
scripts/test/corpus/CounterGuard/report.mdmarkdown954 B
scripts/test/corpus/CounterGuard/sany.outother36.1 KB
scripts/test/corpus/CounterGuard/verdict-1.exitother3 B
scripts/test/corpus/CounterGuard/verdict-1.outother2.4 KB
scripts/test/corpus/CounterGuard/verdict-2.exitother3 B
scripts/test/corpus/CounterGuard/verdict-2.outother2.9 KB
scripts/test/corpus/CounterInitPast/ChartRun.outother2.5 KB
scripts/test/corpus/CounterInitPast/CounterInitPast.cfgconfig54 B
scripts/test/corpus/CounterInitPast/CounterInitPast.tlaother169 B
scripts/test/corpus/CounterInitPast/chart.mmdother282 B
scripts/test/corpus/CounterInitPast/graph.dotother84 B
scripts/test/corpus/CounterInitPast/report.mdmarkdown425 B
scripts/test/corpus/CounterInitPast/sany.outother35.3 KB
scripts/test/corpus/CounterInitPast/verdict-1.exitother3 B
scripts/test/corpus/CounterInitPast/verdict-1.outother1.3 KB
scripts/test/corpus/CounterInitPast/verdict-2.outother2.5 KB
scripts/test/corpus/CounterInitPastHolds/ChartRun.outother2.5 KB
scripts/test/corpus/CounterInitPastHolds/CounterInitPastHolds.cfgconfig50 B
scripts/test/corpus/CounterInitPastHolds/CounterInitPastHolds.tlaother171 B
scripts/test/corpus/CounterInitPastHolds/chart.mmdother16 B
scripts/test/corpus/CounterInitPastHolds/graph.dotother84 B
scripts/test/corpus/CounterInitPastHolds/report.mdmarkdown211 B
scripts/test/corpus/CounterInitPastHolds/sany.outother34.8 KB
scripts/test/corpus/CounterInitPastHolds/verdict-1.outother2.0 KB
scripts/test/corpus/CounterInstance/ChartRun.outother2.8 KB
scripts/test/corpus/CounterInstance/CounterInstance.cfgconfig41 B
scripts/test/corpus/CounterInstance/CounterInstance.tlaother217 B
scripts/test/corpus/CounterInstance/CounterSteps.tlaother113 B
scripts/test/corpus/CounterInstance/chart.mmdother326 B
scripts/test/corpus/CounterInstance/graph.dotother599 B
scripts/test/corpus/CounterInstance/report.mdmarkdown295 B
scripts/test/corpus/CounterInstance/sany.outother73.3 KB
scripts/test/corpus/CounterInstance/verdict-1.exitother3 B
scripts/test/corpus/CounterInstance/verdict-1.outother3.0 KB
scripts/test/corpus/CounterJump/ChartRun.outother2.7 KB
scripts/test/corpus/CounterJump/CounterJump.cfgconfig57 B
scripts/test/corpus/CounterJump/CounterJump.tlaother303 B
scripts/test/corpus/CounterJump/chart.mmdother547 B
scripts/test/corpus/CounterJump/graph.dotother1.0 KB
scripts/test/corpus/CounterJump/report.mdmarkdown374 B
scripts/test/corpus/CounterJump/sany.outother44.9 KB
scripts/test/corpus/CounterJump/verdict-1.exitother3 B
scripts/test/corpus/CounterJump/verdict-1.outother2.1 KB
scripts/test/corpus/CounterJump/verdict-2.outother2.6 KB
scripts/test/corpus/CounterLoop/ChartRun.outother2.6 KB
scripts/test/corpus/CounterLoop/CounterLoop.cfgconfig41 B
scripts/test/corpus/CounterLoop/CounterLoop.tlaother232 B
scripts/test/corpus/CounterLoop/chart.mmdother320 B
scripts/test/corpus/CounterLoop/graph.dotother593 B
scripts/test/corpus/CounterLoop/report.mdmarkdown285 B
scripts/test/corpus/CounterLoop/sany.outother41.5 KB
scripts/test/corpus/CounterLoop/verdict-1.exitother3 B
scripts/test/corpus/CounterLoop/verdict-1.outother2.8 KB
scripts/test/corpus/CounterOrder/ChartRun.outother2.7 KB
scripts/test/corpus/CounterOrder/CounterOrder.cfgconfig51 B
scripts/test/corpus/CounterOrder/CounterOrder.tlaother194 B
scripts/test/corpus/CounterOrder/chart.mmdother406 B
scripts/test/corpus/CounterOrder/graph.dotother299 B
scripts/test/corpus/CounterOrder/report.mdmarkdown606 B
scripts/test/corpus/CounterOrder/sany.outother39.0 KB
scripts/test/corpus/CounterOrder/verdict-1.exitother3 B
scripts/test/corpus/CounterOrder/verdict-1.outother2.1 KB
scripts/test/corpus/CounterOrder/verdict-2.exitother3 B
scripts/test/corpus/CounterOrder/verdict-2.outother2.7 KB
scripts/test/corpus/CounterRules/ChartRun.outother2.9 KB
scripts/test/corpus/CounterRules/CounterRules.cfgconfig79 B
scripts/test/corpus/CounterRules/CounterRules.tlaother265 B
scripts/test/corpus/CounterRules/Moves.tlaother70 B
scripts/test/corpus/CounterRules/chart.mmdother485 B
scripts/test/corpus/CounterRules/graph.dotother302 B
scripts/test/corpus/CounterRules/report.mdmarkdown509 B
scripts/test/corpus/CounterRules/sany.outother60.8 KB
scripts/test/corpus/CounterRules/verdict-1.exitother3 B
scripts/test/corpus/CounterRules/verdict-1.outother2.2 KB
scripts/test/corpus/CounterRules/verdict-2.exitother3 B
scripts/test/corpus/CounterRules/verdict-2.outother2.7 KB
scripts/test/corpus/CounterRules/verdict-3.exitother3 B
scripts/test/corpus/CounterRules/verdict-3.outother3.0 KB
scripts/test/corpus/CounterRules/verdict-4.outother2.8 KB
scripts/test/corpus/CounterStart/ChartRun.outother2.7 KB
scripts/test/corpus/CounterStart/CounterStart.cfgconfig53 B
scripts/test/corpus/CounterStart/CounterStart.tlaother180 B
scripts/test/corpus/CounterStart/chart.mmdother351 B
scripts/test/corpus/CounterStart/graph.dotother466 B
scripts/test/corpus/CounterStart/report.mdmarkdown343 B
scripts/test/corpus/CounterStart/sany.outother36.6 KB
scripts/test/corpus/CounterStart/verdict-1.exitother3 B
scripts/test/corpus/CounterStart/verdict-1.outother1.3 KB
scripts/test/corpus/CounterStart/verdict-2.outother2.6 KB
scripts/test/corpus/CounterStop/ChartRun.outother2.6 KB
scripts/test/corpus/CounterStop/CounterStop.cfgconfig53 B
scripts/test/corpus/CounterStop/CounterStop.tlaother172 B
scripts/test/corpus/CounterStop/chart.mmdother87 B
scripts/test/corpus/CounterStop/graph.dotother466 B
scripts/test/corpus/CounterStop/report.mdmarkdown127 B
scripts/test/corpus/CounterStop/sany.outother36.1 KB
scripts/test/corpus/CounterStop/verdict-1.outother2.2 KB
scripts/test/corpus/CounterTie/ChartRun.outother2.8 KB
scripts/test/corpus/CounterTie/CounterTie.cfgconfig96 B
scripts/test/corpus/CounterTie/CounterTie.tlaother263 B
scripts/test/corpus/CounterTie/chart.mmdother600 B
scripts/test/corpus/CounterTie/graph.dotother798 B
scripts/test/corpus/CounterTie/report.mdmarkdown675 B
scripts/test/corpus/CounterTie/sany.outother44.3 KB
scripts/test/corpus/CounterTie/verdict-1.exitother3 B
scripts/test/corpus/CounterTie/verdict-1.outother2.1 KB
scripts/test/corpus/CounterTie/verdict-2.exitother3 B
scripts/test/corpus/CounterTie/verdict-2.outother2.5 KB
scripts/test/corpus/CounterTie/verdict-3.exitother3 B
scripts/test/corpus/CounterTie/verdict-3.outother3.2 KB
scripts/test/corpus/CounterTie/verdict-4.outother2.6 KB
scripts/test/corpus/CounterTieInGraph/ChartRun.outother2.7 KB
scripts/test/corpus/CounterTieInGraph/CounterTieInGraph.cfgconfig72 B
scripts/test/corpus/CounterTieInGraph/CounterTieInGraph.tlaother283 B
scripts/test/corpus/CounterTieInGraph/chart.mmdother455 B
scripts/test/corpus/CounterTieInGraph/graph.dotother933 B

scripts/run.ts

View on GitHub
import { Buffer } from "node:buffer";
import { type ChildProcess, execFile, type ExecFileException, spawn } from "node:child_process";
import { randomBytes } from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { inspect } from "node:util";
import { boundRun, chartRun, configText } from "./backends/cfg.ts";
import { plainText } from "./backends/plain-text.ts";
import { readConfig } from "./frontends/config.ts";
import { parseDot } from "./frontends/dot.ts";
import { readCounterexample, readMarks } from "./frontends/marks.ts";
import { readMessages } from "./frontends/messages.ts";
import { initialOnlyProperties, readChecks, readSany, readSpec } from "./frontends/spec.ts";
import { readTrace } from "./frontends/trace.ts";
import { type Break, readVerdict } from "./frontends/verdict.ts";
import {
  type Bug,
  CHART_STATES,
  type CheckResult,
  type ConfigSection,
  hasSection,
  type Lasso,
  LEVEL,
  type Mark,
  must,
  type Outcome,
  type Output,
  PART,
  type Part,
  type Property,
  RERUN_STATES,
  type Rule,
  ruleKey,
  type Stop,
  stateRules,
  type Trace,
  type Verdict,
} from "./ir.ts";
import { addPast, deadEnds, finder, lasso, mark } from "./passes/annotate.ts";
import { pathGraph } from "./passes/paths.ts";

export function* fileLines(file: string) {
  const fd = fs.openSync(file, "r");
  try {
    let buf = Buffer.allocUnsafe(PART);
    let kept = 0;
    for (;;) {
      if (kept === buf.length) buf = Buffer.concat([buf], buf.length * 2);
      const n = fs.readSync(fd, buf, kept, buf.length - kept, null);
      if (!n) break;
      const end = kept + n;
      const cut = buf.lastIndexOf(10, end - 1);
      if (cut === -1) {
        kept = end;
        continue;
      }
      yield* buf.toString("utf8", 0, buf[cut - 1] === 13 ? cut - 1 : cut).split(/\r?\n/);
      kept = buf.copy(buf, 0, cut + 1, end);
    }
    yield buf.toString("utf8", 0, kept);
  } finally {
    fs.closeSync(fd);
  }
}

const TLC_TOOL =
  "http:tla2tools[url=https://github.com/tlaplus/tlaplus/releases/download/v1.7.4/tla2tools.jar,checksum=sha256:936a262061c914694dfd669a543be24573c45d5aa0ff20a8b96b23d01e050e88]@1.7.4";
const JAVA_TOOL = "java@temurin-25";
export const PINNED_FLAGS = ["-workers", "1", "-fp", "0", "-lncheck", "final"];
const SANY_ERRORS = /^\*\*\* ?(?:Errors|Abort messages|Parse Error)/m;

type ChildResult = {
  status: number | null;
  stdout: string;
  stderr: string;
  error: ExecFileException | undefined;
};

const runChild = (file: string, args: string[], options: { cwd: string; env: NodeJS.ProcessEnv }) =>
  new Promise<ChildResult>((resolve) => {
    execFile(
      file,
      args,
      { encoding: "utf8", maxBuffer: 1 << 30, ...options },
      (error, stdout, stderr) =>
        resolve({
          status: error ? (typeof error.code === "number" ? error.code : null) : 0,
          stdout,
          stderr,
          error: typeof error?.code === "string" ? error : undefined,
        }),
    );
  });

export const killGroup = (pid: number) =>
  new Promise((resolve) =>
    (process.platform === "win32"
      ? spawn("taskkill", ["/T", "/F", "/PID", String(pid)], { stdio: "ignore", windowsHide: true })
      : spawn("/bin/sh", ["-c", 'kill -s KILL -- "-$1"', "sh", String(pid)], { stdio: "ignore" })
    ).on("exit", resolve),
  );

export const messageOf = (error: unknown) =>
  typeof error === "object" &&
  error !== null &&
  "message" in error &&
  typeof error.message === "string"
    ? error.message
    : inspect(error);

const joined = (files: string[]): Output => {
  const fds: number[] = [];
  const sizes: number[] = [];
  for (const f of files) {
    const fd = fs.openSync(f, "r");
    fds.push(fd);
    sizes.push(fs.fstatSync(fd).size);
  }
  return {
    *[Symbol.iterator]() {
      for (let f = 0; f < fds.length; f++)
        for (let pos = 0; pos < sizes[f];) {
          const buf = Buffer.allocUnsafe(Math.min(PART, sizes[f] - pos));
          const n = fs.readSync(fds[f], buf, 0, buf.length, pos);
          if (!n) break;
          pos += n;
          yield buf.subarray(0, n);
        }
    },
  };
};

const utf8 = new TextDecoder("utf-8", { ignoreBOM: true });

function* outputLines(output: Output) {
  let rest: Uint8Array = new Uint8Array(0);
  for (const part of output) {
    const buf = rest.length ? Buffer.concat([rest, part]) : part;
    const cut = buf.lastIndexOf(10);
    if (cut === -1) {
      rest = buf;
      continue;
    }
    yield* utf8.decode(buf.subarray(0, buf[cut - 1] === 13 ? cut - 1 : cut)).split(/\r?\n/);
    rest = buf.subarray(cut + 1);
  }
  yield utf8.decode(rest);
}

const messagesOf = (output: Output): Iterable<Part> => ({
  [Symbol.iterator]: () => readMessages(outputLines(output)),
});

type FileResult = {
  status: number | null;
  stdout: string;
  output: Output;
  error: Error | undefined;
};

const runToFiles = (
  file: string,
  args: string[],
  { out, ...options }: { out: string; cwd: string; env: NodeJS.ProcessEnv },
) =>
  new Promise<FileResult>((resolve) => {
    const files = [`${out}.stdout`, `${out}.stderr`];
    const fds: number[] = [];
    for (const f of files) fds.push(fs.openSync(f, "w"));
    let child: ChildProcess;
    try {
      child = spawn(file, args, { ...options, stdio: ["ignore", ...fds] });
    } finally {
      for (const fd of fds) fs.closeSync(fd);
    }
    let error: Error | undefined;
    const settle = (code: number | null) =>
      resolve({
        status: error ? null : code,
        stdout: files[0],
        output: joined(files),
        error,
      });
    child.on("error", (e) => {
      error = e;
      if (child.pid === undefined) settle(null);
    });
    child.on("close", (code) => {
      if (child.pid !== undefined) settle(code);
    });
  });

async function findTlc(cwd: string) {
  const { TLA2TOOLS, JAVA } = process.env;
  if (TLA2TOOLS) return { jar: TLA2TOOLS, java: JAVA || "java" };
  const mise = (...args: string[]) =>
    runChild("mise", args, { cwd, env: { ...process.env, MISE_CEILING_PATHS: cwd } });
  const where = () =>
    Promise.all([mise("where", TLC_TOOL), mise("which", "java", "--tool", JAVA_TOOL)]);
  let found = await where();
  if (found.some((r) => r.status !== 0)) {
    const r = await mise("install", JAVA_TOOL, TLC_TOOL);
    if (r.status !== 0)
      throw new Error(
        `Cannot find TLC: mise install failed: ${r.error?.message ?? r.stderr.trim()}\nOr set TLA2TOOLS to the path of tla2tools.jar, and JAVA to a java binary (Java 11 or later).`,
      );
    found = await where();
  }
  return {
    jar: path.join(found[0].stdout.trim(), "tla2tools.jar"),
    java: JAVA || found[1].stdout.trim(),
  };
}

const exitClass = (status: number | null) =>
  status === 0 ? 0 : status === 11 || status === 12 || status === 13 ? 1 : 2;

const tail = (r: FileResult) => {
  if (r.error) return r.error.message;
  const kept: string[] = [];
  let size = 0;
  for (const chunk of plainText(messagesOf(r.output))) {
    kept.push(chunk);
    size += chunk.length;
    while (size - kept[0].length >= 1 << 28) size -= kept.shift()!.length;
  }
  return kept.join("").trim().split("\n").slice(-30).join("\n");
};

const firstLines = (text: string) => {
  const lines = text.trim().split("\n");
  if (lines.length > 40) lines.splice(40, Infinity, `... ${lines.length - 40} more lines`);
  return lines.join("\n");
};

const stopOf = (r: FileResult): Stop => {
  const errors: Part[] = [];
  for (const m of r.error ? [] : messagesOf(r.output))
    if (
      typeof m !== "string" &&
      (m.level === LEVEL.ERROR ||
        m.level === LEVEL.TLCBUG ||
        (errors.length && m.level === LEVEL.STATE))
    )
      errors.push(m);
  if (!errors.length) return { status: r.status, message: tail(r) };
  return { status: r.status, message: firstLines([...plainText(errors)].join("")) };
};

const sanyErrors = (r: FileResult) => {
  if (r.error) return undefined;
  const text = [...plainText(messagesOf(r.output))].join("");
  const at = text.search(SANY_ERRORS);
  return at === -1 ? undefined : firstLines(text.slice(at));
};

const names = (rules: Rule[], kind: Rule["kind"]) => {
  const found: string[] = [];
  for (const rule of rules) if (rule.kind === kind) found.push(rule.name);
  return found;
};

const brokenRule = (rules: Rule[], broke: Break) => {
  if (broke.kind === "temporal") return undefined;
  for (const rule of rules)
    if (rule.kind === broke.kind && (broke.kind === "deadlock" || rule.name === broke.name))
      return rule;
  if (broke.kind === "invariant")
    for (const rule of rules) if (rule.kind === "property" && rule.name === broke.name) return rule;
  return undefined;
};

function* only(marks: Iterable<Mark>, rules: Set<string>) {
  for (const { values, broken } of marks) {
    const kept: string[] = [];
    for (const name of broken) if (rules.has(name)) kept.push(name);
    if (kept.length) yield { values, broken: kept };
  }
}

export async function check(specFile: string, configFile: string): Promise<CheckResult> {
  const cfg = readConfig({ file: configFile, text: fs.readFileSync(configFile, "utf8") });
  const checks = readChecks({ configFile, config: cfg });
  const folder = path.dirname(path.resolve(specFile));
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "statechart-"));
  try {
    const tlc = await findTlc(dir);
    const config = (name: string, sections: ConfigSection[], extra: string[]) => {
      fs.writeFileSync(path.join(dir, `${name}.cfg`), configText(sections, extra));
      return [`-metadir`, path.join(dir, `meta-${name}`), "-config", path.join(dir, `${name}.cfg`)];
    };
    const runJava = (name: string, main: string, args: string[], jvm: string[] = []) => {
      const tmp = path.join(dir, `tmp-${name}`);
      fs.mkdirSync(tmp);
      return runToFiles(
        tlc.java,
        [
          `-Djava.io.tmpdir=${tmp}`,
          `-DTLA-Library=${folder}`,
          ...jvm,
          "-cp",
          tlc.jar,
          main,
          ...args,
        ],
        {
          cwd: dir,
          out: path.join(dir, name),
          env: process.platform === "darwin" ? process.env : { ...process.env, LC_ALL: "C.UTF-8" },
        },
      );
    };
    const xml = await runJava(
      "sany",
      "tla2sany.xml.XMLExporter",
      ["-o", "-I", folder, path.resolve(specFile)],
      ["-XX:TieredStopAtLevel=1"],
    );
    if (xml.status !== 0) {
      const errors = xml.error
        ? undefined
        : sanyErrors(
            await runJava(
              "sany-errors",
              "tla2sany.SANY",
              [path.resolve(specFile)],
              ["-XX:TieredStopAtLevel=1"],
            ),
          );
      throw new Error(
        `SANY could not read ${specFile} (exit ${xml.status}):\n${errors ?? tail(xml)}`,
      );
    }
    const sany = readSany(fs.readFileSync(xml.stdout, "utf8"));
    const module = path.basename(specFile, ".tla");
    const spec = { ...readSpec({ module, sany, checks }), folder };
    const rules = stateRules(spec);
    const all = [...rules];
    for (const name of spec.properties) all.push({ kind: "property", name });
    const verdicts = new Map<string, Verdict>();
    const decide = (rule: Rule, outcome: Outcome, r?: FileResult) =>
      verdicts.set(ruleKey(rule), { rule, outcome, messages: r && messagesOf(r.output) });
    const judge = async () => {
      const groups = [all];
      let first: FileResult | undefined;
      let stop: Stop | undefined;
      const states = { distinct: 0, initial: 0, complete: false, cut: false };
      for (let k = 1; groups.length;) {
        const group: Rule[] = [];
        for (const rule of groups[0]) if (!verdicts.has(ruleKey(rule))) group.push(rule);
        if (first && !group.length) {
          groups.shift();
          continue;
        }
        const name = `verdict-${k++}`;
        const extra: string[] = [];
        const invariants = names(group, "invariant");
        const properties = names(group, "property");
        if (invariants.length) extra.push(`INVARIANT ${invariants.join(" ")}`);
        if (properties.length) extra.push(`PROPERTY ${properties.join(" ")}`);
        const bounded = first !== undefined && !states.complete;
        if (bounded) extra.push("CONSTRAINT ChartBound");
        const r = await runJava(name, "tlc2.TLC", [
          "-tool",
          ...PINNED_FLAGS,
          ...(names(group, "deadlock").length ? [] : ["-deadlock"]),
          ...config(name, spec.written, extra),
          bounded ? "ChartBound" : spec.module,
        ]);
        first ??= r;
        const { broke, distinct, initial } = readVerdict(messagesOf(r.output));
        states.distinct = Math.max(states.distinct, distinct);
        states.initial = Math.max(states.initial, initial);
        const rule = broke && brokenRule(group, broke);
        const cut = bounded && distinct >= RERUN_STATES;
        if (exitClass(r.status) === 2) {
          stop = stopOf(r);
          break;
        } else if (rule) decide(rule, "broken", r);
        else if (cut && (r.status === 0 || broke?.kind === "temporal")) {
          states.cut = true;
          for (const rule of group) decide(rule, "not checked");
        } else if (r.status === 0) {
          states.complete = true;
          for (const rule of group) decide(rule, "holds");
        } else if (broke && properties.length) {
          if (broke.kind === "temporal") {
            states.complete = true;
            for (const rule of group) if (rule.kind !== "property") decide(rule, "holds");
          }
          const pending: Rule[][] = [];
          const rest: Rule[] = [];
          for (const rule of group)
            if (rule.kind === "property") pending.push([rule]);
            else if (!verdicts.has(ruleKey(rule))) rest.push(rule);
          if (pending.length === 1) decide(pending[0][0], "broken", r);
          else groups.splice(0, 1, rest, ...pending);
        } else
          throw new Error(
            `TLC exited ${r.status} on ${specFile}, but its output names no rule that broke:\n${tail(r)}`,
          );
      }
      for (const rule of all) if (!verdicts.has(ruleKey(rule))) decide(rule, "not checked");
      return { first: first!, stop, states };
    };
    fs.writeFileSync(path.join(dir, "ChartBound.tla"), boundRun(spec.module));
    const judged = await judge();
    const exit = exitClass(judged.first.status);
    const failed = (r: FileResult) =>
      new Error(`TLC could not check ${specFile} (exit ${r.status}):\n${stopOf(r).message}`);
    const initialOnly = initialOnlyProperties(sany, spec);
    const propertiesWith = (bugOf: (trace: Trace) => Lasso | undefined) => {
      const properties: Property[] = [];
      for (const rule of all) {
        if (rule.kind !== "property") continue;
        const v = must(verdicts, ruleKey(rule));
        properties.push({
          ...v,
          bug: v.outcome === "broken" && v.messages ? bugOf(readTrace(v.messages)) : undefined,
          initialOnly: initialOnly.has(rule.name),
        });
      }
      return properties;
    };
    if (exit === 2) throw failed(judged.first);
    const checked: Verdict[] = [];
    for (const rule of rules) checked.push(must(verdicts, ruleKey(rule)));
    const common = {
      spec,
      exit,
      stop: judged.stop,
      states: judged.states,
      messages: messagesOf(judged.first.output),
      rules: checked,
    };
    if (judged.states.distinct <= CHART_STATES) {
      const tag = `chart-${randomBytes(16).toString("hex")}`;
      fs.writeFileSync(path.join(dir, "ChartRun.tla"), chartRun(spec, tag));
      const drawn = await runJava("ChartRun", "tlc2.TLC", [
        "-tool",
        ...PINNED_FLAGS,
        "-deadlock",
        ...config("ChartRun", spec.written, ["INVARIANT ChartMark", "CONSTRAINT ChartBudget"]),
        "-dump",
        "dot,actionlabels",
        path.join(dir, "graph"),
        "ChartRun",
      ]);
      const file = path.join(dir, "graph.dot");
      if (!fs.existsSync(file)) throw failed(drawn);
      if (readVerdict(messagesOf(drawn.output)).distinct <= CHART_STATES) {
        const graph = parseDot(fileLines(file));
        const find = finder(graph, spec.variables);
        const drawing = drawn.status === 0 ? undefined : { status: drawn.status };
        const broken = new Set<string>();
        for (const { rule, outcome } of verdicts.values())
          if (rule.kind === "invariant" && outcome === "broken") broken.add(rule.name);
        const marks = only(readMarks(readMessages(fileLines(drawn.stdout)), tag), broken);
        const past = mark(graph, marks, spec.variables, rules, find);
        const excludes = hasSection(spec, "ACTION_CONSTRAINT");
        for (const rule of rules) {
          const { outcome, messages } = must(verdicts, ruleKey(rule));
          if (
            rule.kind === "deadlock" &&
            outcome === "broken" &&
            !drawing &&
            !hasSection(spec, "CONSTRAINT", "ACTION_CONSTRAINT")
          )
            deadEnds(graph, rule);
          if (outcome !== "broken" || !messages) continue;
          const counterexample = readCounterexample(messages);
          if (
            !drawing ||
            lasso(graph, { seq: counterexample, loop: undefined }, spec.variables, find)
          )
            addPast(graph, counterexample, spec.variables, rule, past, excludes, find);
        }
        const properties = propertiesWith((trace) => lasso(graph, trace, spec.variables, find));
        return {
          ...common,
          graph,
          paths: undefined,
          properties,
          drawing,
        };
      }
    }
    const drawn = pathGraph(spec.variables);
    const paths: Bug[] = [];
    for (const rule of rules) {
      const { outcome, messages } = must(verdicts, ruleKey(rule));
      if (outcome === "broken" && messages)
        paths.push(drawn.bug(rule, readCounterexample(messages)));
    }
    return {
      ...common,
      graph: drawn.graph,
      paths,
      properties: propertiesWith(drawn.lasso),
      drawing: undefined,
    };
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
}
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 196kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 192kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 192kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 192kUpdated Claude CodeCodexVerifiedInstall