agentleFS
arxdsilvaarxdsilva/ai-loops/skills/jobs-and-state-machines/SKILL.md

jobs-and-state-machines

Skill0 starsChanged 7 days agoMITView on GitHub

Use when implementing or reviewing background jobs, workflows, sagas, retries, schedulers, leases or locks, or any entity with a lifecycle (orders, payments, subscriptions) - explicit state machines, typed outcomes, hysteresis and indexed work discovery.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Never: for every worker, scan every possible task.

    line 48
  2. Distinctive:

    Use explicit priority layers (for example forced, critical, scheduled, normal, background).

    line 58

≈ “Common” is an estimate: at least 1 in 5 of the 364,980 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Jobs and state machines

Load

  • Your language: loops:lang-<id>. Go: specs/languages/go/RULES.md §43 state transitions, §34 ordering, §53 escalation.
  • specs/ARCHITECTURE.md §17 state machines, §12 ownership.

State machines

Model lifecycles as explicit states with one transition function that checks the source state (an enum, sealed type or tagged union in the pack's language). Go example:

type PaymentState uint8
const (Pending PaymentState = iota; Authorised; Captured; Refunded; Failed)

Each state defines:

  • entry condition;
  • action;
  • completion;
  • failure (with a typed reason);
  • resource lifecycle (leases and locks held).

Transitions are visible functions or events, not ad hoc field assignments from several packages. Illegal transitions return a typed error and are tested.

Typed outcomes

Expected failures (declined, out of stock, not eligible, lease lost) are typed results, not generic errors or exceptions. Infrastructure errors are wrapped with context (Go: specs/languages/go/RULES.md §8).

Leases and locks

Support explicit lease keys (entity, row, queue partition, external resource).

A lease must release on: successful completion; cancellation; owner crash (TTL expiry); target deletion; replacement by another job; invalidation.

  • Leases have a TTL and a heartbeat or fencing token; a stale owner cannot commit.
  • Exactly one owner may hold an exclusive lease.
  • Quantity moves (stock, balance) are atomic with respect to the location/ownership invariant: a unit exists in exactly one place at a time.

Work discovery

Never: for every worker, scan every possible task. Use indexed candidates by type, partition, availability and urgency, then:

filter -> cheap score -> small finalist set -> exact check -> acquire lease

Deterministic tie-break among candidates (priority, then sequence, then ID).

Priority

Use explicit priority layers (for example forced, critical, scheduled, normal, background). Do not bury priority in callback order.

Hysteresis and flapping

Any behaviour driven by a threshold or signal (autoscaling, circuit breakers, retries, alerts, throttles) uses different start and stop thresholds, or a minimum duration or cooldown, and ships a no-flap test: feed a signal oscillating around the threshold and assert the state changes at most once per cooldown.

Expensive searches run bucketed or invalidation-driven; emergency checks may run more often.

Retries

Capped exponential backoff with jitter from an injected RNG and clock; retry only idempotent or keyed operations; a dead-letter path for exhausted retries.

Tests

Cover: priority ordering; conflicting leases; cancellation cleanup; target deletion cleanup; no duplicate quantities; no two exclusive owners; unreachable-candidate fallback; deterministic tie-break; no-flap; illegal transitions; large-N discovery benchmark.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.