Index of every page on this site: llms.txt
All skills
kr4chinin avatar

/convert-state-machines-to-actors

@c46b93c
Run it yourself

Convert a database-backed implicit state machine to an actor, the one owner every writer of the persisted state goes through. Use when asked to put writers behind one owner, or to fix a race or lost update across several writers, such as a find-implicit-state-machines finding.

Use this Skill: https://skilld.dev/gh/kr4chinin/skills/convert-state-machines-to-actors

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesguarded-writes.md

≈1.1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Guarded writes

Read before designing the actor's guard; answer each heading that applies.

Guard

The engine accepts the new state only if everything the transition read still holds:

  • Row lock. Read with the locking statement itself (SELECT ... FOR UPDATE) and write in the same transaction.
  • Compare-and-set. UPDATE ... SET state = :next, version = :v + 1 WHERE id = :id AND version = :v; every writer and bypass bumps version. On zero rows, reload, check the receipt and rerun the transition; resending the old next state is an overwrite.
  • Conditional write on a store without interactive transactions: the condition is the guard; on failure, reload and rerun. No lock attribute.
  • Unique key. Guards creation and uniqueness claims only, never an update.

A lock that outlives the transaction is no guard. A session advisory lock (pg_advisory_lock, MySQL GET_LOCK) leaks to the next user of a pooled connection, and a lock row committed on its own survives a crash. In PostgreSQL use pg_advisory_xact_lock (the two-integer form or a wide hash, so unrelated keys do not contend). MySQL has no transaction-scoped named lock: lock the owner row by unique key, inserting it first with an ignore-conflict insert when it may not exist.

Guard inputs and current read

Everything the guard reads is part of it; the entity's version does not cover a parent or quota row. With locks, load after taking them (for a row lock the locking read is the load) and lock every input another writer can change. With compare-and-set or a conditional write, read first and put each input's version or status into the write's condition or transaction, or fold it into the guarded row; on failure, reload from the database. An entity loaded earlier, an ORM identity-map hit or a snapshot read shows the old version.

No row yet

A PostgreSQL row lock covers only existing rows. In MySQL what a locking read locks depends on the index, predicate and isolation level, so lock by unique-key equality. For a first creation or a cross-row invariant ("one default per catalog"), guard its scope: the owner row, a transaction-scoped lock on the owner key, or a unique key plus retry for a uniqueness claim. An aggregate invariant ("allocations within quota") needs the scope row locked.

Atomic bundle

The state with its full context, the receipt and the pending intent or outbox rows commit together. Without multi-item transactions that means one item holding all of them, sized for the redelivery window under the store's item limit, or the store's transactional API. If the bundle cannot be atomic, ask the user.

Contention

  • Take several locks in one fixed order, such as sorted keys.
  • Deadlocks and serialization failures are normal. Roll back the whole transaction (some engines roll back only the statement on a lock-wait timeout), then retry a bounded number of times with jittered backoff: reload, check the receipt, rerun.
  • Retry only failures known to have rolled back; an unknown commit is resolved through the event key. Exhausted retries return contended.

Duplicates

  • Each entry point gives events a key that is unique per event and survives redelivery (a message id, a client request id; not a per-delivery tag), or states that it re-evaluates duplicates against the current state. That works only when a replay is a no-op ("mark paid"); an increment or debit needs a key, and without one an unknown commit is uncertain, not retried.
  • Keep one receipt per entity and event key (a unique row, or an entry in the entity's own item) in the same bundle, for as long as redelivery is possible. A "last event" column cannot answer an older replay.
  • After taking the guard and before the transition, return the recorded outcome for a known key. The same key with different parameters is a conflict.

Identity

Normalize the entity key once at the boundary (case, whitespace, format), consistent with the collation, before locking or comparing.

Outcomes and logs

Keep rejected (do not retry), contended (retry) and uncertain (reconcile) distinct; callers map them to their existing errors. Report a constraint violation as itself. Log one event per outcome (rejected, committed, effect_sent, stale_result_rejected), keyed by entity id, without personal data.

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning7d3 checks · Risk SAFE
  • Gen Agent Trust Hub7d

    The skill provides structured architectural guidance for refactoring database-backed implicit state machines into a robust actor model. It emphasizes data consistency through guarded writes, atomic transactions, and comprehensive scenario-based testing, showing no signs of malicious intent or unauthorized data access.

  • Socket7d

    No alerts

  • Snyk7d

    Risk: MEDIUM · 1 issue

Signed by skilld at c46b93c. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last week.

Activeupdated 2 weeks ago

README badge

README badge for kr4chinin/skills/convert-state-machines-to-actors