Part III — Advanced Systems · Chapter 13 · Lesson 5 of 5 · 3 min read
Chapter 13 Checkpoint: Bounded Concurrent Workflow
Checkpoint level: Advanced
Purpose: Build a concurrent order-pricing workflow that overlaps host lookups, bounds concurrency and time, cancels cleanly, and produces deterministic results.
Release note: async APIs and host registration calls are illustrative. Verify them against your installed v0.63 release.
Checkpoint outcomes
By completing this checkpoint, you will demonstrate that you can:
- write async functions that await host lookups;
- define a safe async host boundary;
- run children in a bounded task group;
- apply per-call and overall deadlines;
- cancel cleanly with guaranteed cleanup; and
- prove deterministic output under concurrency.
Scenario
Each order needs a customer tier (host database) and a stock check (host inventory service). The workflow must price a batch of synthetic orders using the familiar reasons GOLD_BULK, GOLD, BULK, NONE, and VALIDATION_ERROR, with a bulk threshold of 10.
13C.1 Requirements
| Area | Requirement |
|---|---|
| Concurrency | At most 10 orders in flight |
| Per-call deadline | 1 second for tier and stock lookups |
| Batch deadline | 60 seconds |
| Tier fallback | "standard" on timeout |
| Stock failure | Order gets VALIDATION_ERROR |
| Failure policy | Collect-all; one bad order never discards the batch |
| Cancellation | Release stock holds; write no partial result for cancelled orders |
| Output | Same order and content as input, every run |
13C.2 Reference structure
async function price_one(order, token) {
let tier = "standard"
try {
with std.async.deadline(seconds=1.0) {
tier = await host.lookup_tier(order["customer_id"], token)
}
} catch err {
if err.kind == "Cancelled" { throw err }
}
let hold = null
try {
with std.async.deadline(seconds=1.0) {
hold = await host.reserve_stock(order["sku"], order["qty"], token)
}
} catch err {
if err.kind == "Cancelled" { throw err }
return {"id": order["id"], "reason": "VALIDATION_ERROR"}
}
try {
return {"id": order["id"], "reason": reason_for(tier, order["qty"])}
} finally {
await host.release_if_unconfirmed(hold)
}
}
export async function price_batch(orders, token) {
with std.async.deadline(seconds=60.0) {
return await std.async.gather(
[price_one(o, token) for o in orders],
limit=10, return_errors=true)
}
}
reason_for is the synchronous rule function from earlier chapters: GOLD_BULK for gold with quantity of at least 10, GOLD for gold, BULK for quantity of at least 10, otherwise NONE.
13C.3 Required test matrix
| Case | Fake host behaviour | Expected result |
|---|---|---|
| A | All lookups fast and successful | Correct reasons; output in input order |
| B | Tier lookup exceeds 1 s for one gold order | That order uses "standard" (BULK or NONE) |
| C | Stock service fails for one order | That order is VALIDATION_ERROR; others unaffected |
| D | 50 orders, observe in-flight count | Never more than 10 concurrent |
| E | Batch cancelled after half the orders | No partial results for cancelled orders; all holds released |
| F | All lookups slow so 60 s deadline fires | Timeout raised; holds released |
| G | Run case A five times | Byte-identical output each time |
| H | Run case A on interpreter and VM | Identical output |
13C.4 Acceptance criteria
- Every case passes.
- No task outlives price_batch.
- No stock hold remains after any run.
- Cancelled errors are never swallowed.
- Host values crossing the boundary are portable and validated.
- The capability list for the host functions is minimal and documented.
13C.5 Submission package
- the script and host registration code;
- the fake host with its configurable behaviours;
- results for cases A to H;
- a timing comparison for limits 1, 10, and 50; and
- a short design note explaining deadlines, limits, and failure policy.
13C.6 Assessment rubric
| Area | Evidence of mastery |
|---|---|
| Async design | Waits overlap; no blocking calls in async code. |
| Boundary safety | Portable values, safe errors, named capabilities. |
| Structure | All tasks owned by groups; none leak. |
| Time control | Per-call and batch deadlines behave as specified. |
| Cancellation | Cleanup always runs; Cancelled is never swallowed. |
| Determinism | Output order and content are stable across runs and engines. |
Completion standard: Cases A to H pass, no holds leak, and the design note justifies every limit and deadline.
13C.7 Reflection questions
- Which deadline caught the most problems during testing?
- What would change if partial batches were unacceptable?
- How would you choose the concurrency limit in production?
- Where could a blocking call still slip in?
Chapter 13 summary
You can now write async PeaLang that overlaps waiting work, cross the async host boundary safely, bound work with cancellation and deadlines, and own every task through task groups. The checkpoint showed that these tools produce concurrent programs that are still predictable and testable.
Transition to Chapter 14
Concurrency multiplies what a script can do in a given time. Chapter 14 turns to limiting what it may do at all, beginning with capability-safe execution and least privilege.
Checkpoint status: Ready for review.