CtrlK
BlogDocsLog inGet started
Tessl Logo
PluginPublic

statelyai/stately

Find implicit state machines, review state bugs, and check or visualize statecharts with Stately.

Quality

95%

Does it follow best practices?

Run evals on this skill

Adds up to 20 points to the overall score

View guide
SecuritybySnyk

Medium

Suggest reviewing before use

Overview
Quality
Evals
Security
Files

correlated-regions.mjsscripts/

/**
 * Parallel regions claim that their states combine freely. When one region's
 * state always follows another's (a functional dependency over every checked
 * configuration), drawing them side by side implies combinations the model
 * never reaches. This merges such regions into one region of combined states
 * before projection, and says so.
 */

const isRecord = (value) =>
  value !== null && typeof value === 'object' && !Array.isArray(value);
const labels = (value) => [value].flat().map(String);

/** True when every value of `from` maps to a single value of `to`. */
function determines(rows, from, to) {
  const seen = new Map();
  for (const row of rows) {
    const previous = seen.get(row[from]);
    if (previous === undefined) seen.set(row[from], row[to]);
    else if (previous !== row[to]) return false;
  }
  return true;
}

/**
 * Returns the view to project and a note per merged group. `states` are the
 * raw checked variables, in any order.
 */
export function mergeCorrelatedRegions(view, states) {
  if (!view.state || !states.length) return { view, notes: [] };
  const shapes = states.map((vars) => view.state(vars));
  if (!shapes.every(isRecord)) return { view, notes: [] };
  const names = Object.keys(shapes[0]);
  if (names.length < 2) return { view, notes: [] };
  const rows = shapes.map((shape) =>
    Object.fromEntries(
      names.map((name) => [name, JSON.stringify(labels(shape[name]))]),
    ),
  );
  const distinct = (name) => new Set(rows.map((row) => row[name])).size;
  const parent = new Map(names.map((name) => [name, name]));
  const find = (name) =>
    parent.get(name) === name ? name : find(parent.get(name));
  const followers = [];
  for (const leader of names)
    for (const follower of names)
      if (
        leader !== follower &&
        distinct(leader) > 1 &&
        distinct(follower) > 1 &&
        find(leader) !== find(follower) &&
        determines(rows, leader, follower)
      ) {
        parent.set(find(follower), find(leader));
        followers.push([follower, leader]);
      }
  const groups = new Map();
  for (const name of names) {
    const root = find(name);
    groups.set(root, [...(groups.get(root) ?? []), name]);
  }
  const merged = [...groups.values()].filter((group) => group.length > 1);
  if (!merged.length) return { view, notes: [] };
  const regionOf = (group) => group.join(' + ');
  const notes = merged.map((group) => {
    const follows = followers
      .filter(([follower]) => group.includes(follower))
      .map(([follower, leader]) => `${follower} follows ${leader}`);
    return `Regions ${group.join(' and ')} change together (${follows.join('; ')}), so the chart draws them as one region of combined states.`;
  });
  const state = (vars) => {
    const shape = view.state(vars);
    if (!isRecord(shape)) return shape;
    const out = {};
    for (const group of groups.values()) {
      if (group.length === 1) out[group[0]] = shape[group[0]];
      else
        out[regionOf(group)] = group
          .map((name) => labels(shape[name]).join(' / '))
          .join(' · ');
    }
    const entries = Object.entries(out);
    return entries.length === 1 ? entries[0][1] : out;
  };
  return { view: { ...view, state }, notes };
}

.mcp.json

LICENSE

README.md

SKILL.md

tile.json