agenticraft_foundation.algebra.patterns¶
6 coordination patterns built from CSP primitives: request-response, pipeline, scatter-gather, barrier, mutex, producer-consumer.
Coordination patterns for multi-agent systems in CSP.
This module provides: - Request-Response: Client-server interaction - Pipeline: Sequential processing stages - Scatter-Gather: Parallel task distribution and collection - Barrier: Synchronization point for multiple agents - Mutex: Mutual exclusion pattern - Producer-Consumer: Buffered communication
BarrierPattern
dataclass
¶
Bases: CoordinationPattern
Barrier pattern for multi-party synchronization.
All participants must reach the barrier before any can proceed.
CSP Model
PARTICIPANT_i = arrive → depart → SKIP BARRIER = arrive_1 → arrive_2 → ... → depart_1 → depart_2 → ... → SKIP
MutexPattern
dataclass
¶
Bases: CoordinationPattern
Mutual exclusion pattern.
Only one process can be in the critical section at a time.
CSP Model
MUTEX = acquire → release → MUTEX PROCESS_i = acquire → critical_i → release → SKIP
PipelinePattern
dataclass
¶
Bases: CoordinationPattern
Pipeline pattern for sequential multi-stage processing.
Interaction
Stage1 ─data→ Stage2 ─data→ Stage3 → ...
CSP Model
STAGE_1 = data_1_2 → SKIP STAGE_2 = data_1_2 → data_2_3 → SKIP STAGE_N = data_{n-1}_n → SKIP SYSTEM = STAGE_1 ||| STAGE_2 ||| ... ||| STAGE_N (synchronized on data events)
ProducerConsumerPattern
dataclass
¶
Bases: CoordinationPattern
Producer-Consumer pattern with bounded buffer.
CSP Model
PRODUCER = produce → put → PRODUCER CONSUMER = get → consume → CONSUMER BUFFER = put → get → BUFFER (simplified unbounded)
global_process()
¶
Simplified: produce → put → get → consume → recurse.
RequestResponsePattern
dataclass
¶
Bases: CoordinationPattern
Request-Response pattern for client-server interaction.
Interaction
Client ─request→ Server Server ─response→ Client
CSP Model
CLIENT = request → response → SKIP SERVER = request → response → SKIP SYSTEM = CLIENT |[{request, response}]| SERVER
ScatterGatherPattern
dataclass
¶
Bases: CoordinationPattern
Scatter-Gather pattern for parallel task distribution.
Interaction
Coordinator ─task→ Worker1, Worker2, ... Worker1, Worker2, ... ─result→ Coordinator
CSP Model
COORD = task_w1 → task_w2 → result_w1 → result_w2 → SKIP WORKER_i = task_wi → result_wi → SKIP
barrier(participants, arrive_event='arrive', depart_event='depart', repeatable=False)
¶
Create a barrier synchronization pattern.
| PARAMETER | DESCRIPTION |
|---|---|
participants
|
List of participant names
TYPE:
|
arrive_event
|
Prefix for arrival events
TYPE:
|
depart_event
|
Prefix for departure events
TYPE:
|
repeatable
|
Whether the pattern repeats
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The global process representing the pattern |
compose_agents(agents, sync_events=None)
¶
mutex(processes, acquire_event='acquire', release_event='release', critical_event_prefix='critical')
¶
Create a mutual exclusion pattern.
| PARAMETER | DESCRIPTION |
|---|---|
processes
|
List of process names
TYPE:
|
acquire_event
|
Event to acquire mutex
TYPE:
|
release_event
|
Event to release mutex
TYPE:
|
critical_event_prefix
|
Prefix for critical section events
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The mutex resource process |
pipeline(stages, data_event_prefix='data', repeatable=False)
¶
Create a pipeline pattern process.
| PARAMETER | DESCRIPTION |
|---|---|
stages
|
Ordered list of stage names
TYPE:
|
data_event_prefix
|
Prefix for data events
TYPE:
|
repeatable
|
Whether the pattern repeats
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The global process representing the pattern |
producer_consumer(producer='producer', consumer='consumer', produce_event='produce', consume_event='consume', put_event='put', get_event='get')
¶
Create a producer-consumer pattern.
| PARAMETER | DESCRIPTION |
|---|---|
producer
|
Producer name
TYPE:
|
consumer
|
Consumer name
TYPE:
|
produce_event
|
Event when producer creates item
TYPE:
|
consume_event
|
Event when consumer uses item
TYPE:
|
put_event
|
Event to put item in buffer
TYPE:
|
get_event
|
Event to get item from buffer
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The global process representing the pattern |
request_response(client='client', server='server', request_event='request', response_event='response', repeatable=False)
¶
Create a request-response pattern process.
| PARAMETER | DESCRIPTION |
|---|---|
client
|
Client participant name
TYPE:
|
server
|
Server participant name
TYPE:
|
request_event
|
Request event name
TYPE:
|
response_event
|
Response event name
TYPE:
|
repeatable
|
Whether the pattern repeats
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The global process representing the pattern |
scatter_gather(coordinator='coordinator', workers=(), task_event_prefix='task', result_event_prefix='result', repeatable=False)
¶
Create a scatter-gather pattern process.
| PARAMETER | DESCRIPTION |
|---|---|
coordinator
|
Coordinator name
TYPE:
|
workers
|
List of worker names
TYPE:
|
task_event_prefix
|
Prefix for task events
TYPE:
|
result_event_prefix
|
Prefix for result events
TYPE:
|
repeatable
|
Whether the pattern repeats
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The global process representing the pattern |
verify_pattern(pattern, spec=None)
¶
Verify properties of a coordination pattern.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern to verify
TYPE:
|
spec
|
Optional specification to check refinement against
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PatternVerification
|
PatternVerification with results |
agenticraft_foundation.algebra.patterns.coordination
¶
Coordination patterns for multi-agent systems.
These patterns express common coordination scenarios in CSP, enabling formal verification of multi-agent interactions.
CoordinationPattern
¶
Bases: ABC
Base class for coordination patterns.
participants()
abstractmethod
¶
Return set of participant names.
events()
abstractmethod
¶
Return set of events in this pattern.
global_process()
abstractmethod
¶
Return the global (system) process representing the pattern.
local_process(participant)
abstractmethod
¶
Return the local process for a specific participant.
compose(sync_events=None)
¶
Compose all local processes into the global system.
| PARAMETER | DESCRIPTION |
|---|---|
sync_events
|
Events to synchronize on. Defaults to pattern events.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
Composed parallel process |
is_deadlock_free()
¶
Check if the pattern is deadlock-free.
RequestResponsePattern
dataclass
¶
Bases: CoordinationPattern
Request-Response pattern for client-server interaction.
Interaction
Client ─request→ Server Server ─response→ Client
CSP Model
CLIENT = request → response → SKIP SERVER = request → response → SKIP SYSTEM = CLIENT |[{request, response}]| SERVER
PipelinePattern
dataclass
¶
Bases: CoordinationPattern
Pipeline pattern for sequential multi-stage processing.
Interaction
Stage1 ─data→ Stage2 ─data→ Stage3 → ...
CSP Model
STAGE_1 = data_1_2 → SKIP STAGE_2 = data_1_2 → data_2_3 → SKIP STAGE_N = data_{n-1}_n → SKIP SYSTEM = STAGE_1 ||| STAGE_2 ||| ... ||| STAGE_N (synchronized on data events)
ScatterGatherPattern
dataclass
¶
Bases: CoordinationPattern
Scatter-Gather pattern for parallel task distribution.
Interaction
Coordinator ─task→ Worker1, Worker2, ... Worker1, Worker2, ... ─result→ Coordinator
CSP Model
COORD = task_w1 → task_w2 → result_w1 → result_w2 → SKIP WORKER_i = task_wi → result_wi → SKIP
BarrierPattern
dataclass
¶
Bases: CoordinationPattern
Barrier pattern for multi-party synchronization.
All participants must reach the barrier before any can proceed.
CSP Model
PARTICIPANT_i = arrive → depart → SKIP BARRIER = arrive_1 → arrive_2 → ... → depart_1 → depart_2 → ... → SKIP
MutexPattern
dataclass
¶
Bases: CoordinationPattern
Mutual exclusion pattern.
Only one process can be in the critical section at a time.
CSP Model
MUTEX = acquire → release → MUTEX PROCESS_i = acquire → critical_i → release → SKIP
ProducerConsumerPattern
dataclass
¶
Bases: CoordinationPattern
Producer-Consumer pattern with bounded buffer.
CSP Model
PRODUCER = produce → put → PRODUCER CONSUMER = get → consume → CONSUMER BUFFER = put → get → BUFFER (simplified unbounded)
global_process()
¶
Simplified: produce → put → get → consume → recurse.
PatternVerification
dataclass
¶
Result of pattern verification.
request_response(client='client', server='server', request_event='request', response_event='response', repeatable=False)
¶
Create a request-response pattern process.
| PARAMETER | DESCRIPTION |
|---|---|
client
|
Client participant name
TYPE:
|
server
|
Server participant name
TYPE:
|
request_event
|
Request event name
TYPE:
|
response_event
|
Response event name
TYPE:
|
repeatable
|
Whether the pattern repeats
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The global process representing the pattern |
pipeline(stages, data_event_prefix='data', repeatable=False)
¶
Create a pipeline pattern process.
| PARAMETER | DESCRIPTION |
|---|---|
stages
|
Ordered list of stage names
TYPE:
|
data_event_prefix
|
Prefix for data events
TYPE:
|
repeatable
|
Whether the pattern repeats
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The global process representing the pattern |
scatter_gather(coordinator='coordinator', workers=(), task_event_prefix='task', result_event_prefix='result', repeatable=False)
¶
Create a scatter-gather pattern process.
| PARAMETER | DESCRIPTION |
|---|---|
coordinator
|
Coordinator name
TYPE:
|
workers
|
List of worker names
TYPE:
|
task_event_prefix
|
Prefix for task events
TYPE:
|
result_event_prefix
|
Prefix for result events
TYPE:
|
repeatable
|
Whether the pattern repeats
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The global process representing the pattern |
barrier(participants, arrive_event='arrive', depart_event='depart', repeatable=False)
¶
Create a barrier synchronization pattern.
| PARAMETER | DESCRIPTION |
|---|---|
participants
|
List of participant names
TYPE:
|
arrive_event
|
Prefix for arrival events
TYPE:
|
depart_event
|
Prefix for departure events
TYPE:
|
repeatable
|
Whether the pattern repeats
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The global process representing the pattern |
mutex(processes, acquire_event='acquire', release_event='release', critical_event_prefix='critical')
¶
Create a mutual exclusion pattern.
| PARAMETER | DESCRIPTION |
|---|---|
processes
|
List of process names
TYPE:
|
acquire_event
|
Event to acquire mutex
TYPE:
|
release_event
|
Event to release mutex
TYPE:
|
critical_event_prefix
|
Prefix for critical section events
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The mutex resource process |
producer_consumer(producer='producer', consumer='consumer', produce_event='produce', consume_event='consume', put_event='put', get_event='get')
¶
Create a producer-consumer pattern.
| PARAMETER | DESCRIPTION |
|---|---|
producer
|
Producer name
TYPE:
|
consumer
|
Consumer name
TYPE:
|
produce_event
|
Event when producer creates item
TYPE:
|
consume_event
|
Event when consumer uses item
TYPE:
|
put_event
|
Event to put item in buffer
TYPE:
|
get_event
|
Event to get item from buffer
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Process
|
The global process representing the pattern |
compose_agents(agents, sync_events=None)
¶
verify_pattern(pattern, spec=None)
¶
Verify properties of a coordination pattern.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern to verify
TYPE:
|
spec
|
Optional specification to check refinement against
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PatternVerification
|
PatternVerification with results |