Skip to main content
← Back to list
01Issue
BugShippedSwamp CLIPublic
Assigneesskunk-ape

Relationships

#4992 context.runModel() runs the target method without the target's per-behavior lock

Opened by skunk-ape · 11/22/2026· Shipped 11/22/2026

What happens

A method run takes a per-behavior lock, so two runs on the same behavior wait for each other. A nested run started with context.runModel() does not take that lock on its target. It can run at the same time as a swamp behavior run (or another nested run) on the same behavior.

When both runs read the behavior's data and then write it, one update is lost. The concurrency docs say runs on the same behavior serialize. Nested runs break that.

Steps to reproduce

  1. swamp init a fresh repo with the default filesystem datastore.
  2. Add an extension behavior @repro/counter with a method bump. It reads its state resource, sleeps 4 seconds, then writes { count: previous + 1 }.
  3. Add an extension behavior @repro/caller with a method go that calls context.runModel({ definition: "ctr", method: "bump" }).
  4. swamp behavior create @repro/counter ctr and swamp behavior create @repro/caller clr.
  5. Control: start swamp behavior run ctr bump twice, one second apart. The second waits for the first. Count goes 1, 2, 3.
  6. Race: start swamp behavior run clr go, then one second later swamp behavior run ctr bump.

Log from step 6 (count was 3 before):

16:23:01.665Z pid=2246243 nested START read count=3
16:23:02.660Z pid=2248304 hand   START read count=3
16:23:05.671Z pid=2246243 nested END   wrote count=4
16:23:06.666Z pid=2248304 hand   END   wrote count=4

Both commands exit 0. Data versions 4 and 5 both hold count: 4. The same happens with the order reversed: the hand run holds the lock and the nested run goes straight in.

Expected vs actual

Expected: the nested run waits for the target's lock, as a second swamp behavior run does. The two bumps run one after the other and the count ends at 5.

Actual: both run at once on the same behavior. The count ends at 4. One update is lost and nothing reports it.

What the lock protects

Version numbers stay unique without the lock, because each version directory is created with an atomic mkdir (src/infrastructure/persistence/unified_data_repository.ts:3352). So no version is overwritten. What breaks is everything a method assumes about owning the behavior while it runs:

  • Read, then write: two runs read the same latest version, and the later write silently drops the earlier one (shown above).
  • "Latest" is whichever run finished last, not whichever started last.
  • Lifecycle checks such as "create only if no resource exists" both pass, so external side effects (create a cloud resource, send a message) can happen twice.
  • In a team or custom datastore the target's data is pulled and pushed under the target's lock. A nested run writes the target with no lock or lease on it.

Cause

On origin/main (3f2b72312):

  • A top-level run takes the lock in src/cli/commands/model_method_run.ts:646-683 (methodRunLockMode, then acquireModelLocks for the one behavior being run). acquireModelLocks is at src/cli/repo_context.ts:3678. Workflow steps lock their own step's behavior the same way.
  • context.runModel is wired in src/domain/models/in_process_executor.ts:315 to ModelInvocationService.invoke (src/domain/models/model_invocation_service.ts:243). invoke resolves the target, checks depth, cycles and authorization, builds a child context (line 374) and calls executionService.executeWorkflow (line 412). Nothing on that path takes a lock on the target.
  • design/enablers/datastores.md:10662 says runModel() "carries the outer run's locks". Those are the caller's locks, not the target's. No code comment or design note says leaving the target unlocked is deliberate.
  • .claude/skills/swamp/references/behavior/references/concurrency.md:5 tells users runs on the same behavior serialize.

Suggested fix

In ModelInvocationService.invoke, take the target's per-behavior lock (acquireModelLocks or the same lock hook workflow steps use) before executeWorkflow, run the child under runUnderModelLocks so its own children see the lock, and release it in a finally.

Caveats:

  • Self-call: cycle detection is by (type, method), so a method may runModel another method on its own behavior. The caller already holds that lock. The lock must be reentrant for the current run scope (the lock-holder marker already knows which locks the scope holds), or this deadlocks.
  • Lock order: A's method calling B while B's method calls A can wait on each other. The lock timeout and the existing lock_wait_cycle check should turn that into an error, not a hang. A test should pin it.
  • The nested run's writes to the target should be pushed under the target's lock in team and custom datastores.
  • Nested runs are not available on workers, so remote dispatch needs no change.

Version

Reproduced with swamp 20261120.161258.0-sha.7b758e05, filesystem datastore, Linux. The code is the same on origin/main 3f2b72312.

02Bog Flow
✓OPEN✓TRIAGED✓IN PROGRESS✓SHIPPED+ 1 MOREASSIGNED+ 2 MOREREVIEW+ 10 MOREPR_LINKED+ 2 MORESESSION_SUMMARIZED

Shipped

11/22/2026, 6:13:58 PM

Click a lifecycle step above to view its details.

03Sludge Pulse
skunk-ape assigned skunk-ape11/22/2026, 5:22:16 PM

Sign in to post a ripple.