Hoare 1978 csp

From curiositech/windags-skills · View interactive version

Hoare 1978 csp is an AI agent skill from the GitHub repository curiositech/windags-skills. Foundational theory for process-oriented concurrency through synchronous message-passing, applicable to multi-agent coordination and parallel decomposition

How to use this skill

Skill file

Communicating Sequential Processes (Hoare)

Description: Foundational theory for process-oriented concurrency through synchronous message-passing

Activation triggers: Multi-agent coordination protocols, parallel task decomposition, deadlock debugging, coordination failures

Decision Points

1. System Decomposition Strategy

IF task has clear data transformations (input → process → output)
  THEN decompose by data flow (each transformation = process)
  └── Linear dependencies → pipeline topology
  └── Independent branches → parallel processes
  └── Convergence points → guarded input selection

IF components need frequent fine-grained data sharing
  THEN consider shared-memory model instead
  └── Use CSP for coarse-grained coordination only

IF coordination requirements unclear
  THEN map all component interactions first
  └── Draw communication graph before coding

2. Communication Protocol Design

IF request-response pattern needed
  THEN client outputs request, inputs reply
       server inputs request, outputs reply
  └── Symmetric protocol prevents deadlock

IF streaming data flow
  THEN producer: *[output!data]
       consumer: *[input?data → process(data)]
  └── Termination via channel closure

IF multiple clients, one server
  THEN server uses guarded selection:
       *[client1?req → handle1(req)
        []client2?req → handle2(req)]
  └── Built-in fairness via arbitrary choice

3. Deadlock Prevention Strategy

IF communication graph has cycles
  THEN prove resource ordering OR redesign topology
  └── Cycles require careful justification

IF topology is DAG (no cycles)
  THEN deadlock impossible, proceed with design

IF bidirectional communication needed
  THEN alternate input/output carefully
  └── Never have both processes waiting for same direction

4. Termination Design

IF pipeline topology
  THEN source terminates first → propagates downstream
  └── Each stage: *[input?x → process(x)]

IF tree/graph topology
  THEN design explicit join points for synchronization
  └── Avoid premature termination breaking chains

IF long-running services
  THEN use external termination signals
  └── CSP suitable for bounded-lifetime tasks

Failure Modes

1. **Circular Dependency Deadlock**

2. **Protocol Asymmetry**

3. **Premature Termination Cascade**

4. **Guard Condition Race**

5. **Buffering Assumption Violation**

Worked Examples

Example 1: Multi-Agent Negotiation with Guards

Scenario: Auction system where auctioneer coordinates bidder agents

AUCTIONEER = 
  *[bidder1?bid(amount) → record(1, amount); announce!newbid(1, amount)
   []bidder2?bid(amount) → record(2, amount); announce!newbid(2, amount)  
   []bidder3?bid(amount) → record(3, amount); announce!newbid(3, amount)
   []timer?timeout → announce!closed; winner!result
   ]

BIDDER(id) =
  *[announce?newbid(bidder, amount) → 
      [amount < maxprice → auctioneer!bid(amount + increment)
      []amount >= maxprice → skip
      ]
   []winner?result → celebrate
   ]

Decision walkthrough:

  1. Decomposition choice: Request-response pattern → guarded server selection
  2. Protocol design: Auctioneer inputs bids, outputs announcements (symmetric)
  3. Termination: Timer provides bounded execution, winner channel signals end
  4. Nondeterminism: If multiple bidders ready simultaneously, arbitrary selection (fair)

Novice mistake: Making bidders poll for auction state instead of using announcements

Expert insight: Guards encode bidding strategy (amount < maxprice), selection handles concurrency

Example 2: Pipeline Deadlock Recovery

Scenario: Data processing pipeline with validation stage that can reject items

// BROKEN VERSION (deadlock prone)
PRODUCER = *[pipeline!item(data)]
VALIDATOR = *[pipeline?item(data) → 
              [valid(data) → consumer!clean(data)
              []¬valid(data) → skip  // PROBLEM: consumer waits forever
              ]]
CONSUMER = *[validator?clean(data) → process(data)]

Failure analysis:

// FIXED VERSION
VALIDATOR = *[pipeline?item(data) → 
              [valid(data) → consumer!clean(data)
              []¬valid(data) → consumer!error(data)  // Always send something
              ]]
CONSUMER = *[validator?clean(data) → process(data)
            []validator?error(data) → log_error(data)  // Handle both cases
            ]

Key decisions made:

  1. Protocol completeness: Every input must produce some output
  2. Error propagation: Rejections become explicit error messages, not silence
  3. Consumer guards: Handle both success and error cases symmetically

Quality Gates

NOT-FOR Boundaries

Don't use CSP for:

Delegate instead:

CSP sweet spot: Medium-grained coordination between independent processes with clear communication boundaries and well-defined protocols.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.