mex-memory/mex / skills/mex-statechart
mex-statechart
VerifiedExplain complicated existing behavior using checked state diagrams. Use when asked for a state diagram, to diagram the previous explanation, show a bug or recovery path, compare a proposed fix or existing TLA+ spec, or derive a model from an agreed chart. Generates native Mermaid and a readable terminal fallback; optionally view and explicitly save in MEX Hub.
upstream/scripts/rule-book.mjs
View on GitHubimport fs from "node:fs";
import path from "node:path";
import {
blankStrings,
definitions,
ruleSentences,
splitCommentsKeepingOffsets,
tlaVariables,
} from "./lib.mjs";
const EXTENDS_STATEMENT = /\bEXTENDS\s+(\w+(?:\s*,\s*\w+)*)/g;
const CONSTANT_STATEMENT = /\bCONSTANTS?\s+(\w+(?:\([^)]*\))?(?:\s*,\s*\w+(?:\([^)]*\))?)*)/g;
const DECLARED_NAME = /(\w+)(?:\([^)]*\))?/g;
const LOCAL_DEFINITION = /^LOCAL\s/;
const INSTANCE_STATEMENT =
/^(LOCAL[ \t]+)?(?:([A-Za-z_]\w*)[ \t]*==\s*)?INSTANCE\s+(\w+)((?:[^\n]|\n(?=\s))*)/gm;
const SUBSTITUTION = /(\w+)\s*<-([\s\S]*?)(?=\w+\s*<-|$)/g;
const ALIAS = /^\s*([A-Za-z_]\w*(?:![A-Za-z_]\w*)*)\s*$/;
export function ruleBook(spec) {
const modules = new Map();
const moduleOf = (file) => {
if (modules.has(file)) return modules.get(file);
const module = { rules: new Map(), parameters: [] };
modules.set(file, module);
const text = fs.readFileSync(file, "utf8");
const code = blankStrings(splitCommentsKeepingOffsets(text).code);
const fileOf = (name) => path.join(path.dirname(file), `${name}.tla`);
const inherited = new Map();
const instancedReads = new Map();
const localImports = new Set();
const take = (imported, { name, local, substitutions = new Map() }) => {
for (const [rule, entry] of moduleOf(imported).rules) {
if (entry.local) continue;
const key = name ? `${name}!${rule}` : rule;
if (local) localImports.add(key);
if (entry.sentence) inherited.set(key, entry.sentence);
instancedReads.set(
key,
[...entry.reads].map((v) => substitutions.get(v) ?? v),
);
}
};
const parameters = [];
for (const [, names] of code.matchAll(EXTENDS_STATEMENT))
for (const extended of names
.split(/\s*,\s*/)
.map(fileOf)
.filter(fs.existsSync)) {
parameters.push(...moduleOf(extended).parameters);
take(extended, {});
}
for (const [, names] of code.matchAll(CONSTANT_STATEMENT))
parameters.push(...[...names.matchAll(DECLARED_NAME)].map(([, constant]) => constant));
for (const [, local, name, instanced, clause] of code.matchAll(INSTANCE_STATEMENT))
if (fs.existsSync(fileOf(instanced)))
take(fileOf(instanced), {
name,
local,
substitutions: new Map([...clause.matchAll(SUBSTITUTION)].map(([, v, e]) => [v, e])),
});
const written = ruleSentences(text);
const { bare, defs, own, through } = definitions(text, instancedReads, parameters);
module.parameters.push(...parameters, ...tlaVariables(bare));
const sentenceOf = (rule, seen = new Set()) => {
if (written.has(rule)) return written.get(rule);
if (seen.has(rule)) return undefined;
const d = defs.get(rule);
if (!d) return inherited.get(rule);
const alias = ALIAS.exec(bare.slice(d.from, d.to))?.[1];
return alias ? sentenceOf(alias, seen.add(rule)) : undefined;
};
for (const [rule, entry] of own) {
const d = defs.get(rule);
module.rules.set(rule, {
sentence: sentenceOf(rule),
reads: through(entry).reads,
local: d ? LOCAL_DEFINITION.test(bare.slice(d.at, d.from)) : localImports.has(rule),
});
}
return module;
};
return moduleOf(path.resolve(spec)).rules;
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| 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: 197k | Updated | Claude CodeCodex | Verified | Install |
| 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: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| 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: 194k | Updated | Claude CodeCodex | Verified | Install |