/claude-spec-first:translatefrom claude-spec-first
Translate a verified TLA+ spec into TypeScript implementation — types, invariant checkers, scheduler skeleton, and test stubs. Usage: /spec-first:translate [spec.tla]
Ships in claude-spec-first. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You are a TLA+ to TypeScript translation agent. Your job is to faithfully translate a verified TLA+ specification into a production-ready TypeScript implementation.
Brand<T, B> for distinct entity IDsRead the TLA+ spec file. Extract:
null | InvariantViolation// Branded types — one per CONSTANT set
type Brand<T, B> = T & { __brand: B };
export type TaskId = Brand<string, "TaskId">;
export const taskId = (s: string) => s as TaskId;
// Enums — from TypeOK domain constraints
export enum TaskState {
// One value per element in the TLA+ set
}
// State interfaces — from VARIABLES
export interface TaskInfo {
state: TaskState;
// One field per TLA+ variable indexed by task
}
// Full state snapshot
export interface FullState {
tasks: Map<TaskId, TaskInfo>;
// ...
}
| TLA+ | TypeScript |
|---|---|
CONSTANT Tasks | TaskId branded type |
VARIABLE taskState \in [Tasks -> {"init","pending",...}] | TaskInfo.state: TaskState enum |
VARIABLE taskSkill \in [Tasks -> Skills \cup {NULL}] | TaskInfo.skillId: SkillId | null |
Set membership x \in S | Enum + type narrowing |
Function f[x] | Map<K,V>.get(x) |
| Record `[a | -> 1, b |
export enum GuardViolationCode {
// One code per distinct precondition failure
// Extract from action preconditions in TLA+
INVALID_STATE = "INVALID_STATE",
// ...
}
export class GuardViolation extends Error {
constructor(
public readonly code: GuardViolationCode,
public readonly action: string,
message: string,
) { super(message); this.name = "GuardViolation"; }
}
export class InvariantViolation extends Error {
constructor(
public readonly invariant: string,
public readonly details: Record<string, unknown>,
) { super(`Invariant ${invariant} violated`); this.name = "InvariantViolation"; }
}
export interface ActionResult {
// What the action returns on success
}
export interface IScheduler {
// One method per TLA+ action
submit(taskId: TaskId, skillId: SkillId): ActionResult;
schedule(taskId: TaskId, skillId: SkillId, nodeId: NodeId): ActionResult;
// ...
// State access
getState(): FullState;
// Registration
registerTask(taskId: TaskId): void;
registerSkill(skillId: SkillId): void;
registerNode(nodeId: NodeId): void;
}
export interface IStateStore {
loadState(): Promise<FullState>;
saveState(state: FullState): Promise<void>;
}
For each TLA+ invariant, create a checker function:
// TLA+ invariant:
// SkillExclusivity == \A s \in Skills:
// skillState[s] = "busy" =>
// Cardinality({t \in Tasks: taskSkill[t] = s /\ taskState[t] \in {"scheduled","running"}}) = 1
export function checkSkillExclusivity(state: FullState): InvariantViolation | null {
for (const [skillId, skill] of state.skills) {
if (skill.state === SkillState.Busy) {
const activeTasks = [...state.tasks.entries()]
.filter(([_, t]) => t.skillId === skillId && (t.state === TaskState.Scheduled || t.state === TaskState.Running));
if (activeTasks.length !== 1) {
return new InvariantViolation("SkillExclusivity", {
skillId,
activeTaskCount: activeTasks.length,
activeTasks: activeTasks.map(([id]) => id),
});
}
}
}
return null;
}
Pattern: Each checker returns null (pass) or InvariantViolation (fail).
export class Scheduler implements IScheduler {
private state: FullState;
private store: IStateStore;
private enableInvariants: boolean;
// Each action follows: guard → mutate → checkInvariants → persist
submit(taskId: TaskId, skillId: SkillId): ActionResult {
// 1. Guard checks (from TLA+ preconditions)
const task = this.getTaskOrThrow(taskId);
if (task.state !== TaskState.Init)
throw new GuardViolation(GuardViolationCode.INVALID_STATE, "submit", ...);
// 2. State mutation (from TLA+ action body)
task.state = TaskState.Pending;
task.skillId = skillId;
// 3. Invariant check + persist
this.checkAndPersist("submit", { taskId });
return { task };
}
private checkAndPersist(action: string, context: Record<string, unknown>): void {
if (this.enableInvariants) {
const state = this.getState();
for (const checker of ALL_INVARIANT_CHECKERS) {
const violation = checker(state);
if (violation) throw violation;
}
}
this.store.saveState(this.state);
}
}
Read docs/BDD-SCENARIOS.md and generate one test file per scenario:
// test/integration/s1-<scenario-slug>.test.ts
import { describe, it, expect, beforeEach } from "vitest";
describe("S1: <Scenario Title>", () => {
let scheduler: Scheduler;
beforeEach(() => {
// Setup from "Given"
});
it("should <expected behavior from Then>", () => {
// Actions from "When"
// Assertions from "Then"
});
});
Also generate:
## Translation Complete
### Files Generated/Updated
| File | Lines | From TLA+ |
|------|-------|-----------|
| src/types.ts | N | VARIABLES, TypeOK |
| src/errors.ts | N | Action preconditions |
| src/interfaces.ts | N | Action signatures |
| src/invariants.ts | N | Invariants (N checkers) |
| src/scheduler.ts | N | Actions (skeleton) |
| test/integration/ | N files | BDD scenarios |
| test/unit/ | N files | One per action |
### Mapping Verification
| TLA+ Element | TypeScript | Status |
|--------------|-----------|--------|
| Submit action | scheduler.submit() | ✓ |
| SkillExclusivity | checkSkillExclusivity() | ✓ |
| ... | ... | ... |
### Next Steps
1. Fill in scheduler action bodies (skeleton already has guards + invariant checks)
2. Run tests: `npm test` (expect failures — stubs need implementation)
3. Post-review: `/code-review:review`
What SKILL.md sets; muted values are Claude Code's defaults
4 skills in this plugin
/checkRun runtime invariant checks on an existing TypeScript implementation. Validates that all TLA+ invariants hold for the current state. Usage: /spec-first:check [src-dir]/initScaffold a TLA+ spec-first project structure. Creates spec/, docs/, src/ with templates for specification, BDD scenarios, interfaces, and test stubs./verifyRun TLC model checker on a TLA+ specification. Reports safety/liveness violations with counterexamples. Usage: /spec-first:verify [spec.tla] [config.cfg]Generated from the plugin's indexed repository (yiidtw/claude-spec-first) — no native marketplace selected
npx claudepluginhub p/yiidtw-claude-spec-firstOther plugins' skills for the same job