Skip to content

The state of a skill is one pure function over its recorded acts - #2212

Merged
zlareb1 merged 2 commits into
mainfrom
feat/skill-state-fold
Dec 5, 2026
Merged

zlareb1 merged 2 commits into
mainfrom
feat/skill-state-fold

Conversation

@zlareb1

@zlareb1 zlareb1 commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Part of: #1143

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

None. No op, field, table or setting changes, and nothing calls the new function.

Why

A skills library keeps a skill as rows of acts: a save stores a revision, a publish or a reject decides one, an archive stops the skill being served, and a delete removes a revision or the whole skill. Every read and every write of such a library asks the same questions of those rows: which revision is served, which is the open draft, and which number comes next. Two callers that each answer with a rule of their own can disagree, and what a reader is served then depends on which caller answered.

This change adds the rule as one pure function with its table tests, before a table, a writer or a read exists. Each of those is then reviewed against a fold that is already on main.

The fold, server/identity/domain/skill_state.py

fold(rows) -> SkillState takes the rows of one skill as mappings with seq, act, revision and content_sha256, plus any column the caller selected. It does no I/O.

  • Void. A delete row voids its revision. A delete row with an empty hash voids every revision up to its number. A void revision is never served, never the latest, never the open draft and has no state, whether its other rows are still present or already removed.
  • Served. Among the rows whose act is not save, reject or delete, the highest seq decides. The skill is served only when every row of that seq is a publish of one revision and one hash, the hash is not empty and the revision is not void. When the deciding rows serve nothing, an older publish row does not take their place. That holds for the rows given: once the publish row of a deleted served revision is removed, the older publish row decides and its revision is served. A writer that deletes the served revision keeps the skill unserved by writing an archive row with the delete row; the fold alone does not.
  • Fail closed. An archive, an act the build does not know, and rows of the deciding seq that differ in act, revision or hash serve nothing. A decision with a higher seq settles them.
  • Latest and open draft. The latest revision is the highest one that is not void and has its save row. The open draft is the latest revision when no publish or reject row names it.
  • Numbers. The next revision and the next seq are one above the highest on any row, the rows of void revisions included, so a number a deleted revision held is never given to another.

SkillState holds:

Field Value
served Served(revision, content_sha256, row), or None when nothing is served
latest_revision the number, 0 for a skill with no revision
open_draft the number, 0 when no draft waits
next_revision, next_seq the numbers a new row takes
revisions number to Revision(revision, content_sha256, state, row)

A revision's state is draft, published, rejected, superseded or withdrawn. row is the deciding publish row on Served and the save row on Revision, the same object the caller passed, so a caller reads its own columns from the fold's answer and does not search the rows a second time.

Choices made where the rule above leaves a case open:

  • A publish or reject row names a revision by number and hash. A revision whose save row has another hash than the row that decides stays a draft, so state == "published" always means the stored bytes have the served hash.
  • An act the build does not know is read as an archive is: it decides, it serves nothing, and it names no revision.
  • A publish row with an empty hash serves nothing, and 0 is not a revision number.
  • The rows are read in one order: by seq, then act, revision and hash. Two reads that return the same rows in different orders give the same answers: what is served, each revision's number, hash and state, the latest revision, the open draft and the next numbers. Of two save rows that hold one number, the later in that order is the revision. Rows equal in all four columns keep the order given, so the carried row of such a tie follows the read's order: Served.row is the first given of the agreeing publish rows and Revision.row is the last given of the equal save rows. Two such folds agree in every answer and can differ in the columns row carries, such as actor and at.
  • The fold returns no state word for the skill as a whole. The first read that answers one adds it to this module.

Scope and risk

Touches:

  • server/identity/domain/skill_state.py (added): fold, SkillState, Served, Revision, and the names of the five acts and the five revision states.
  • tests/unit/test_skill_state_fold.py (added).

Left alone: every other file. This change adds no table, op, wire model, SDK method, command, dashboard view or docs page. The fold is described on the skills architecture page together with its table, when the table is added.

What could break: nothing that runs, because no module imports the new one.

  • Layering: the module imports the standard library only, so domain/ still imports no app, store or api (tests/unit/test_package_contracts.py).
  • Stored data, concurrency, auth and tenant isolation: no statement, row, scope or role changes.
  • Fail closed: the cases the fold cannot read serve nothing, and each has a test below.

Compatibility, rollout and rollback

  • Wire: no op, field, table, column, migration or setting is added, changed or removed.
  • Operator steps: none.
  • Rolling upgrade: no stored or wire shape changes.
  • Rollback: a revert leaves nothing behind.

Tests

The tests were written before the code: tests/unit/test_skill_state_fold.py failed at import while the module did not exist. They build rows by hand and need no ClickHouse.

  • test_a_void_revision_is_never_served_counted_or_answered: fails when a deleted revision is served, is the latest, is the open draft or has a state, with its rows present or removed, for a delete of one revision and of the whole skill. It also fails when an older publish row serves while the publish row of the deleted served revision is still in the rows. Once that row is removed, the rows fold as a deleted draft's do and the older revision is served, which the same test asserts for the draft; keeping the skill unserved after such a delete depends on the archive row written with it.
  • test_an_unknown_act_or_a_disagreeing_tie_serves_nothing: fails when the fold serves under an act it does not know, an act in another case, or deciding rows that differ in revision, hash or act.
  • test_an_agreeing_tie_serves: fails when two publish rows of one seq, revision and hash are refused.
  • test_a_number_is_never_reused_after_a_delete: fails when the next revision or seq is at or below one a delete row holds.
  • test_a_rejected_then_published_revision_is_served: fails when an earlier reject outranks a later publish, for what is served and for the revision's state after an archive.
  • test_a_later_publish_settles_an_unknown_act_or_a_tie: fails when an unread row keeps the skill unserved after a decision with a higher seq.
  • test_a_publish_row_without_a_hash_serves_nothing: fails when a publish row with an empty hash or revision 0 serves.
  • test_a_draft_saved_or_rejected_leaves_the_served_revision_served: fails when a save or reject row decides what is served, or a rejected revision reads as the open draft.
  • test_each_revision_reads_its_state_from_the_last_decision_that_names_it: fails when an earlier published revision reads as served, or an archive leaves a revision published.
  • test_the_latest_revision_is_one_with_its_save_row: fails when a number only a decision row holds is the latest revision or gets a state.
  • test_a_decision_names_a_revision_by_number_and_hash: fails when bytes read as published or rejected under a hash they do not have.
  • test_rows_carry_their_columns_through: fails when the answer holds a copy of a row, or another act's row.
  • test_the_answer_does_not_depend_on_the_order_rows_are_given_in: folds every order of six rows that hold two ties and compares the whole state. No two of the six are equal in seq, act, revision and hash, so it does not cover the carried row of such a tie; test_an_agreeing_tie_serves pins that Served.row is the first given.
  • test_a_skill_with_no_rows_serves_nothing_and_starts_at_one: fails when an empty skill serves, holds a revision, or numbers from 0.

Each of these nineteen one-line defects, applied alone to the module, fails at least one of the tests above: a publish of a void revision serves; a void revision keeps its entry in revisions; an unknown act is skipped when the deciding row is picked; the first row of a disagreeing tie serves; an agreeing tie is refused; the next revision is one above the latest; the next seq skips delete rows; a reject row decides what is served; any earlier reject gives rejected; a reject outranks the served state; rows are ordered by seq alone; a decision names a revision by number alone; a publish row with an empty hash serves; a delete of the whole skill voids one number; the rows of void revisions are dropped before the deciding row is picked; a number with no save row is the latest; superseded reads as withdrawn; the open draft ignores the revision's state; a revision's row is a copy.

What ran at this head:

  • pytest -n 2 -q tests/unit/test_skill_state_fold.py tests/unit/test_package_contracts.py tests/unit/test_op_transport_contract.py tests/unit/test_tunable_constants.py tests/unit/test_store_names_refused.py tests/unit/test_degraded_unnoted.py tests/unit/test_public_copy.py tests/unit/test_doc_links.py tests/unit/test_agent_instructions.py tests/analyzer: 615 passed, 12 skipped. Ten skipped cases are in test_store_names_refused.py and need the clickhouse binary, which this machine does not have; the other two are the analyzer cases that need a ClickHouse server.
  • ruff format and ruff check on the two files: unchanged, no finding.
  • make typecheck: the server ratchet reports 0 errors and mypy --strict sdk/python/loom reports no issues. mypy --strict on the two files reports no issues.
  • scripts/gates/function_length_gate.py: at its baseline.
  • No generator was run: the change adds no op, field, setting, migration, command or plugin file, so none has new output.
  • No ClickHouse and no server were started: the change has no statement, migration or integration test.
  • Stress or load run: none. The fold is a pure function over the rows of one skill.
  • Left to CI: the full unit suite, the integration suite and the generator checks of make lint.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code wrote the diff and ran the checks listed under Tests.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • Local format, lint, type checks and focused tests pass. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump), the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py) and the standalone skill bundles (python3 scripts/codegen/standalone_skills.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.
  • I signed the Contributor License Agreement (CLA) if CLA assistant requested a signature.

Unticked: this change has no generated file, no migration, no plugin file and no frontend file, so those four checks did not apply; the CLA line is the committer's to confirm. No page under docs/ changes: nothing a user or an operator can reach is added.

🤖 Generated with Claude Code


Note

Low Risk
Isolated domain module with no production callers, no I/O, and no schema or API surface changes.

Overview
Introduces fold in server/identity/domain/skill_state.py as the single rule for turning a skill’s act rows (save, publish, reject, archive, delete) into a SkillState: which revision is served, latest/open draft, next revision/seq, and per-revision states (draft through withdrawn). The logic is pure (stdlib only), order-independent, and fails closed on unknown acts, archive, disagreeing same-seq rows, empty publish hashes, and void revisions from delete.

Adds tests/unit/test_skill_state_fold.py with hand-built row cases (void deletes, ties, supersede/withdraw, number reuse, column passthrough, permutations). Nothing else in the repo imports this module yet—no table, API, or wire changes.

Reviewed by Cursor Bugbot for commit 492844e. Bugbot is set up for automated code reviews on this repo. Configure here.

`server/identity/domain/skill_state.py` adds `fold`. It reads the rows of one
skill (`seq`, `act`, `revision`, `content_sha256`) and answers the served
revision and hash, the latest revision, the open draft, the next revision and
`seq`, and the state of each revision.

A `delete` row voids its revision, or every revision up to its number when its
hash is empty. Among the rows that are not a `save`, a `reject` or a `delete`
the highest `seq` decides, and it serves only when every row of that `seq` is a
`publish` of one revision and one hash that is not void. An act the fold does
not know and rows of the deciding `seq` that disagree serve nothing.

The fold does no I/O and has no caller. The rule is tested on hand-built rows
before a table, a writer or a read exists, so each of those is reviewed against
it.

Signed-off-by: Mohammad Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@vercel

vercel Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Dec 5, 2026 6:16am UTC
loom-telemetry Ready Ready Preview Dec 5, 2026 6:16am UTC

Request Review

After a delete of the served revision with no archive row, nothing is
served only while that revision's publish row is in the rows. Once it is
removed, the older publish row decides and its revision is served. The
comment in the void-revision test says so, and that a delete of the
served revision depends on its archive row to keep the skill unserved.

The fold sorts by seq, act, revision and hash, and the sort is stable, so
rows equal in all four keep the order given. Two reads of the same rows
give the same answers, and the row carried for such a tie follows the
read's order: Served.row is the first given, Revision.row the last. The
two comments that promise one order say so.

Comments only; no behaviour changes.

Signed-off-by: Mohammad Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@zlareb1
zlareb1 merged commit b60ad07 into main Dec 5, 2026
72 of 80 checks passed
@zlareb1
zlareb1 deleted the feat/skill-state-fold branch December 5, 2026 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant