Skip to content

agenticraft_foundation.mpst.patterns

4 communication patterns built from session types: request-response, pipeline, scatter-gather, consensus.

Common multi-agent session patterns.

This module provides reusable session type patterns for common multi-agent communication scenarios.

Patterns: - RequestResponse: Simple client-server request-response - ScatterGather: Coordinator sends to all, gathers responses - Pipeline: Sequential processing through stages - Consensus: Multi-party agreement protocol

Each pattern provides: - Global type constructor - Local type projections - Example usage

ConsensusPattern dataclass

Two-Phase Commit consensus pattern.

ATTRIBUTE DESCRIPTION
coordinator

Coordinator participant ID

TYPE: ParticipantId | str

participants

List of participant IDs

TYPE: Sequence[ParticipantId | str]

prepare_label

Label for prepare messages

TYPE: str

vote_yes_label

Label for affirmative vote

TYPE: str

vote_no_label

Label for negative vote

TYPE: str

commit_label

Label for commit decision

TYPE: str

abort_label

Label for abort decision

TYPE: str

global_type(with_choice=False)

Build the global session type.

PARAMETER DESCRIPTION
with_choice

If True, include explicit choice branches (only practical for 1-2 participants)

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
SessionType

Global type for two-phase commit

participants_set()

Get all participants including coordinator.

PipelinePattern dataclass

Pipeline session pattern.

ATTRIBUTE DESCRIPTION
stages

List of stage participant IDs in order

TYPE: Sequence[ParticipantId | str]

data_label

Label for data messages between stages

TYPE: str

repeatable

If True, pipeline can repeat

TYPE: bool

global_type()

Build the global session type.

RETURNS DESCRIPTION
SessionType

Global type for pipeline

The type is

Stage_1 → Stage_2 : Data. Stage_2 → Stage_3 : Data. ... Stage_{n-1} → Stage_n : Data. end (or X for repeatable)

participants()

Get all participants in the pipeline.

RETURNS DESCRIPTION
set[ParticipantId]

Set of stage participant identifiers.

RequestResponsePattern dataclass

Request-Response session pattern.

ATTRIBUTE DESCRIPTION
client

Client participant ID

TYPE: ParticipantId | str

server

Server participant ID

TYPE: ParticipantId | str

request_label

Label for request message

TYPE: str

response_label

Label for response message

TYPE: str

repeatable

If True, pattern can repeat

TYPE: bool

global_type()

Build the global session type.

RETURNS DESCRIPTION
SessionType

Global type for request-response

participants()

Get all participants in the request-response interaction.

RETURNS DESCRIPTION
set[ParticipantId]

Set containing the client and server identifiers.

ScatterGatherPattern dataclass

Scatter-Gather session pattern.

ATTRIBUTE DESCRIPTION
coordinator

Coordinator participant ID

TYPE: ParticipantId | str

workers

List of worker participant IDs

TYPE: Sequence[ParticipantId | str]

task_label

Label for task messages

TYPE: str

result_label

Label for result messages

TYPE: str

global_type()

Build the global session type.

RETURNS DESCRIPTION
SessionType

Global type for scatter-gather

The type is

Coordinator → Worker_1 : Task. ... Coordinator → Worker_n : Task. Worker_1 → Coordinator : Result. ... Worker_n → Coordinator : Result. end

participants()

Get all participants in the scatter-gather pattern.

RETURNS DESCRIPTION
set[ParticipantId]

Set containing the coordinator and all worker identifiers.

two_phase_commit(coordinator='coordinator', participants=None, with_choice=False)

Create a two-phase commit global type.

PARAMETER DESCRIPTION
coordinator

Coordinator participant name

TYPE: str DEFAULT: 'coordinator'

participants

List of participant names

TYPE: Sequence[str] | None DEFAULT: None

with_choice

Include explicit choice branches (only for 1-2 participants)

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
SessionType

Global session type

Example

Simple 2PC with 2 participants

g = two_phase_commit()

Custom participants

g = two_phase_commit("leader", ["replica1", "replica2", "replica3"])

With explicit branching (for 2 participants)

g = two_phase_commit(with_choice=True)