Skip to content

Capped thread copies: count an action's threads as ONE or MANY - #470

Merged
jp-fizzbee merged 13 commits into
mainfrom
claude/project-thread-kfg6jh
Nov 10, 2026
Merged

jp-fizzbee merged 13 commits into
mainfrom
claude/project-thread-kfg6jh

Conversation

@jp-fizzbee

@jp-fizzbee jp-fizzbee commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Builds on #469 (CappedCountStore). Identical threads of an action can now be counted as ONE or MANY copies, so any number of them can be in flight.

  • Opt in:
    • For one action: action_options: <Action>: max_concurrent_actions: -1 (MANY).
    • For all actions without a limit of their own: --experimental_capped_threads, which makes MANY the default when max_concurrent_actions isn't set.
  • Which threads are grouped: role actions are matched against the same action_options keys the concurrency limits use (Role.Action, Role#.Action and the instance key Role#0.Action). A fixed limit under any key keeps ordinary threads.
  • Groups: an opted-in action's threads with identical frames share one slot, with a CardinalityCounter of copies.
    • A new thread starts as ONE.
    • At every yield, groups merge in one pass, using CappedCountStore.AddCount (new here). ONE + ONE is MANY.
    • Running a copy of a MANY group first splits it into "ONE left" and "MANY left", as a fair oneof does. Liveness treats both options as strongly fair. copies.go explains why fairness is sound here.
    • The group keeps its slot, so its thread index stays stable for liveness. The running copy takes a new slot.
  • Crash on yield: one copy of the group that yielded crashes. A group of ONE goes away; a MANY group leaves ONE or MANY copies.
  • Fixed limits: actions with a fixed max_concurrent_actions keep ordinary threads and their limit. Only ordinary threads count toward the global limit.
  • Flag off: with the flag off and no -1, threads, hashing, options and output are unchanged.

Caution: a MANY group can act again and again, like any action that can run forever. State that grows with each step, such as a counter the threads change, has to be bounded by the spec.

Verification

  • New regression specs in tests/specs:
    • capped-thread-copies models the scheduling rule itself as a spec.
    • capped-threads-lock passes and capped-threads-lock-buggy fails MutualExclusion, as an exact limit of 2 does.
    • capped-threads-fair-split passes. A build with an unfair split fails it.
    • capped-threads-loop and capped-threads-crash-loop pass. capped-threads-crash fails liveness, because a crashed busy copy leaves busy set.
    • capped-threads-flag runs with the flag and mixes copies with an action limited to 1.
    • capped-threads-role-limit and capped-threads-role-limit-buggy run with the flag and give a role action a per-role limit of 1 (passes) or 2 (fails).
  • Compared with exact limits: I ran each spec with exact limits of 1 to 4 (crash on and off). Every state reached with exact limits is also reached with copies, and the pass/fail results match.
  • Reference suite: python3 test_references.py passes 172/172, and existing baselines are unchanged.
  • Go tests:
    • go test ./lib includes the new TestCappedCountStoreAddCount.
    • go test ./modelchecker ./lib fails the same 7 tests as on main, which fail there already.

🤖 Generated with Claude Code

https://claude.ai/code/session_013eDw2fcqNcKpnpHXHYFi7n

CountStore keeps, per canonical key, a value and a count, sorted by key
with no zero entries, so equal contents give equal entries whatever the
order of operations. The count policy is per store: exact, or capped at
Many (0, 1, MANY), where removing from Many has two outcomes and the
caller picks one.

It is plain Go with no Starlark and no language change, meant as the
shared core for later users: the checker's own state (such as threads)
and a spec-visible multiset.

Tests: lib/countstore_test.go, random add/remove sequences against
exact counts for both policies, canonical equality, outcomes and errors,
clone and rekey.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The capped policy's increment, removal outcomes and capping now go
through CardinalityCounter (Inc, Dec, NewCardinalityCounter), so the
0/1/MANY rules live in one place. A capped count is the counter's
ValueCode. New test checks the two agree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop the count policy and the exact counts. Each entry's count is a
CardinalityCounter (ONE or MANY; absent is ZERO), so Add is the
counter's Inc, RemoveOutcomes is its Dec, and there are no branches on
a policy. Exact counts can come later as a separate type if needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Remove returns an error for a nil outcome instead of panicking.
- Entries returns a copy, so callers cannot break the sort order or the
  no-ZERO invariant.
- Document the key contract: the key is the element's identity, a
  complete canonical encoding or a collision-resistant hash; Add keeps
  the first value and Equal compares keys and counts.
- Rekey merges with CardinalityCounter.Plus (new: the abstract sum) and
  builds into a temporary store, so a panicking key leaves it unchanged.
- Total returns a shared MANY counter instead of allocating one.
- Tests for each, plus Rekey MANY+MANY, idempotence, clone independence
  and the empty store; Plus is tested against concrete sums.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When max_concurrent_actions is MANY (-1), for an action in its
action_options or globally, an action's identical threads (same frames)
share one slot with a CardinalityCounter of copies. A new thread starts
as ONE; groups with identical frames merge at every yield (ONE + ONE is
MANY); running a copy of a MANY group splits it first into "ONE left" and
"MANY left" forks, a weakly fair choice. The group keeps its slot, and
the running copy takes a new one. Ordinary threads are unchanged and hash
as before. Crash on yield is skipped for copies for now.

--experimental_capped_threads makes MANY the default for an unset global
max_concurrent_actions. Actions with a fixed limit keep ordinary threads
and their limit, and only ordinary threads count towards the global
limit. Without the flag, options and output are unchanged.

Copies stay finite only when the threads' combined effect on the state
is bounded; a counter the threads change grows without bound.

Tests: tests/specs/capped-thread-copies (the rule as a spec) and
capped-threads-{lock,lock-buggy,fair-split,loop,flag}. The reference
suite passes unchanged (168/168).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
crash_on_yield now applies to grouped threads too. A group of ONE crashes
as an ordinary thread. One copy of a MANY group crashing leaves ONE or
MANY copies, so the crash has two successors, as a split does.

Tests: tests/specs/capped-threads-crash (Quiet fails when a busy copy
crashes, as with exact limits 1 to 3) and capped-threads-crash-loop
(passes, as with exact limits). Reachable states include every state of
the exact runs. Reference suite: 170/170.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add copies.go to the modelchecker Bazel rule. A MANY group can act again
and again, so state that grows with each step must be bounded by the
spec, as for any action that can run forever: say so as a caution.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Role actions: decide copies with the same action_options keys as the
  concurrency limits, including the instance key "Role#0.Action". A fixed
  limit under any key keeps ordinary threads. Tests:
  capped-threads-role-limit and capped-threads-role-limit-buggy.
- mergeCopies: one pass, keyed by frames and the thread's fairness, using
  the new CappedCountStore.AddCount instead of calling Add twice for MANY.
- Document why the split is a fair choice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Thread fairness is set only from the action at the root frame, so
identical frames already have the same fairness, and liveness treats
every thread continuation as strongly fair (fairnessLinkName), so a
thread's fairness only marks its action's start link. Keying the merge on
it too can never change a result; drop it and document why.

Also remove the generated .json and out/ files that the role-limit specs
committed by mistake.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jp-fizzbee
jp-fizzbee merged commit 8d3e45d into main Nov 10, 2026
1 check passed
@jp-fizzbee
jp-fizzbee deleted the claude/project-thread-kfg6jh branch November 10, 2026 20:19
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.

2 participants