1. Dependency direction rule for shared platform modules
Use case. Platform crates import product modules and invert architecture. Enforce layer direction mechanically so engineers see violations before review, not after integration builds fail.
# imports contain scoped package path.
const LAYERS = ["platform", "domain", "app", "ui"]
let import_path = "platform/auth"
let allowed = true
if import_path == "app" {
allowed = false
}
print "DEPENDENCY={allowed}"
Save as platform-architecture_01.pea; check pea fmt --check platform-architecture_01.pea && pea check platform-architecture_01.pea; run pea run platform-architecture_01.pea.
2. Feature flag progressive exposure gate
Use case. A flag can be enabled globally by accident. Gate exposure by rollout percentage hash and allowlist; the rule is code so audits can explain every user who saw the variant.
# user_id converted to stable int by host.
const ROLLOUT_PERCENT = 10
const ALLOWLIST = ["alpha-001"]
function flag_exposed(user_id, allowlisted) {
if allowlisted { return {"exposed": true, "reason": "ALLOWLIST"} }
if (user_id % 100) < ROLLOUT_PERCENT {
return {"exposed": true, "reason": "ROLLOUT"}
}
return {"exposed": false, "reason": "OUTSIDE_ROLLOUT"}
}
print "FLAG={flag_exposed(8421, false).exposed}"
Save as platform-architecture_02.pea; check pea fmt --check platform-architecture_02.pea && pea check platform-architecture_02.pea; run pea run platform-architecture_02.pea.
3. Config drift detection between envs
Use case. Staging and production drift silently and breaks deploy weekends. Compare resolved config checksum and flag differences before promotion; ignore volatile fields but not secrets.
# checksums of resolved config excluding vault tokens.
const DRIFT_ALERT = 1
function config_status(resolved_a, resolved_b) {
if resolved_a == resolved_b {
return {"status": "IN_SYNC", "reason": "MATCH"}
}
return {"status": "DRIFT", "reason": "MISMATCH"}
}
print "CONFIG={config_status("sha-aaa", "sha-bbb").status}"
Save as platform-architecture_03.pea; check pea fmt --check platform-architecture_03.pea && pea check platform-architecture_03.pea; run pea run platform-architecture_03.pea.
4. Tenant SLA tier serialization boundary
Use case. Gold tenants expect dedicated queue partitions; free tenants must not consume them. Enforce contract route mapping in code, not spreadsheets, before accepting support tickets.
# tier from billing subscription; lane from router config.
const LANE = {"enterprise": "isolated", "pro": "shared-plus", "free": "shared"}
function route_lane(tier) {
if tier in LANE { return {"lane": LANE[tier], "reason": "TIER_MAPPED"} }
return {"lane": "shared", "reason": "DEFAULT_TIER"}
}
print "LANE={route_lane("enterprise").lane}"
Save as platform-architecture_04.pea; check pea fmt --check platform-architecture_04.pea && pea check platform-architecture_04.pea; run pea run platform-architecture_04.pea.
5. Adapter migration stage gate
Use case. A legacy adapter must coexist with the new one during migration. Track percentages and require synthetic comparisons before switching traffic, or rollout becomes an untested leap.
# stage integers 0..3; mismatch_rate is relative.
const MAX_MISMATCH = 0.005
function migration_gate(stage, mismatch_rate, synthetic_ok) {
if not synthetic_ok { return {"action": "HOLD", "reason": "SYNTHETIC_FAIL"} }
if mismatch_rate > MAX_MISMATCH { return {"action": "ROLLBACK_NEW", "reason": "OUTPUT_DIVERGENT"} }
return {"action": "ADVANCE", "reason": "CONSISTENT"}
}
print "MIGRATION={migration_gate(2, 0.001, true).action}"
Save as platform-architecture_05.pea; check pea fmt --check platform-architecture_05.pea && pea check platform-architecture_05.pea; run pea run platform-architecture_05.pea.
6. DTO serializer version compatibility
Use case. A field rename in a published event breaks consumers ignoring unknown fields. Enforce additive-only changes and keep versioned serializers behind a compatibility window.
# old_required and new_required lists from CI diff.
function dto_backwards_compatible(new_required, old_required) {
let i = 0
while i < len(old_required) {
let field = old_required[i]
let found = false
let j = 0
while j < len(new_required) {
if new_required[j] == field { found = true }
j += 1
}
if not found { return {"ok": false, "removed": field} }
i += 1
}
return {"ok": true, "removed": null}
}
print "DTO={dto_backwards_compatible(["id","email","created_at"], ["id", "email"]).ok}"
Save as platform-architecture_06.pea; check pea fmt --check platform-architecture_06.pea && pea check platform-architecture_06.pea; run pea run platform-architecture_06.pea.
7. Webhook idempotency key lifecycle
Use case. Stripe/GitHub webhooks are retried indefinitely. Require a provider idempotency key before handing to downstream queues; reject missing keys with structured reason so providers can retry.
# key present; seen_keys from distributed cache.
const WINDOW_S = 86400
function webhook_decision(key, seen) {
if key == "" { return {"action": "REJECT", "reason": "MISSING_KEY"} }
if seen { return {"action": "ACK", "reason": "REPLAYED"} }
return {"action": "ENQUEUE", "reason": "NEW"}
}
print "WEBHOOK={webhook_decision("evt-9821", true).action}"
Save as platform-architecture_07.pea; check pea fmt --check platform-architecture_07.pea && pea check platform-architecture_07.pea; run pea run platform-architecture_07.pea.
8. Pub/sub consumer concurrency governor
Use case. A bad message deploy spins millions of threads reprocessing same event. Limit concurrent consumers per message class; shed surplus instead of starving healthy tenants.
# inflight is per topic+group; limit configured per class.
const LIMITS = {"billing.event": 8, "analytics.event": 32}
function can_consume(message_class, inflight) {
if message_class in LIMITS {
let limit = LIMITS[message_class]
if inflight >= limit { return {"action": "WAIT", "reason": "SHED"} }
return {"action": "CLAIM", "reason": "OK"}
}
return {"action": "REJECT", "reason": "UNKNOWN_CLASS"}
}
print "PUBSUB={can_consume("billing.event", 8).action}"
Save as platform-architecture_08.pea; check pea fmt --check platform-architecture_08.pea && pea check platform-architecture_08.pea; run pea run platform-architecture_08.pea.
9. Tenant noisy neighbour throttle preflight
Use case. One tenant’s cron burst can starve others behind shared queues. Admit only if estimated work fits per-tenant allowance; queue otherwise with a fair ETA.
# estimated_ms comes from job payload; allowance is remaining hourly budget.
const PER_TENANT_MS = 60000
function admit_job(estimated_ms, allowance_ms) {
if estimated_ms > allowance_ms {
return {"action": "QUEUE", "reason": "OVER_ALLOWANCE"}
}
return {"action": "RUN_NOW", "reason": "WITHIN_BUDGET", "remaining": allowance_ms - estimated_ms}
}
print "ADMIT={admit_job(70000, 60000).action}"
Save as platform-architecture_09.pea; check pea fmt --check platform-architecture_09.pea && pea check platform-architecture_09.pea; run pea run platform-architecture_09.pea.
10. Backend model override precedence for tenants
Use case. Support staff choose models but enterprise contracts demand pinned SKUs. Resolve overrides by contract then by persona; never let a UI dropdown bypass contractual model choice.
# contract_model from billing; user_override from UI.
function model_choice(contract_model, user_override) {
if contract_model != "" {
return {"model": contract_model, "reason": "CONTRACT"}
}
if user_override != "" {
return {"model": user_override, "reason": "USER"}
}
return {"model": "standard", "reason": "DEFAULT"}
}
print "MODEL={model_choice("gpt-5.6-enterprise", "free-tier").model}"
Save as platform-architecture_10.pea; check pea fmt --check platform-architecture_10.pea && pea check platform-architecture_10.pea; run pea run platform-architecture_10.pea.
← Back to the Code Library