Production patterns, testing & performance
Design reviewable rule functions
A production PeaLang rule should be small, pure and total: inputs arrive as plain data, output is a map or structured error, and no secret is embedded in source.
const MAX_AMOUNT = 10000
function evaluate(tx) {
if tx == null {
return error("ValueError", "missing transaction", "E_NULL")
}
if tx["amount"] == null {
return error("ValueError", "missing amount", "E_AMOUNT", null,
{"field": "amount"})
}
if tx["amount"] > MAX_AMOUNT {
return error("ValueError", "amount above limit", "E_LIMIT", null,
{"amount": tx["amount"], "limit": MAX_AMOUNT})
}
return {"decision": "ALLOW", "score": 0}
}
Return values are data, not exceptions, which makes decisions easy to log, replay and test.
Error contracts
try {
throw error("TypeError", "bad value", "E_TYPE", error("ValueError", "root"))
} catch TypeError e {
print e["kind"]
print e["code"]
print e["cause"]["code"]
} finally {
print "cleanup"
}
Known kinds include Exception, NameError, ValueError, TypeError, IndexError, KeyError, ZeroDivisionError, ScopeError, AssertionError, AttributeError, ImportError, UserError. Unknown kinds raise Unknown error kind, so validate kind strings in shared helpers. The stable value schema is pealang-error-v1; Python hosts should expect ErrorValue; raise PeaError(message, ..., code=..., cause=..., context=...) or build values with build_error(kind, message="", code="", cause=None, context=None). Always include a stable code and a minimal context map; never put tokens or PII there.
Testing strategy
# tests/rules_test.pea
function test_allow() {
let r = evaluate({"amount": 100})
assert r["decision"] == "ALLOW"
}
function test_limit() {
let r = evaluate({"amount": 20000})
assert r["code"] == "E_LIMIT"
}
pea test tests/ --coverage
pea test tests/ --filter test_limit --json
pea check src/ --json && pea typecheck-project
Combine unit-level rule tests with conformance checks. Because type annotations are erased at runtime, run both pea check and pea typecheck-project when contracts matter.
Performance tuning
- Compile hot paths with
pea compileand measure withpea benchmark app.pea --iterations N --save-baseline base.json. - Prefer built-ins (
map,filter,group_by,sort_by) over hand-written loops where semantics match. - Bound long-running programs with
pea vm --max-instructionsand the runtime's execution-time limits. - Avoid repeated interpolation in compiled hot loops; bytecode compilation does not currently support
InterpolatedString. Top-levelawaitis compiled toOp.AWAIT. - Run
pea optimize app.peac --verifyfor conservative bytecode passes andpea fast app.peac --verify --benchmarkfor the direct-handler path in v3.1.39. - Use
stats.benchmark_vm(source, iterations)andstats.benchmark_compare(source, iterations); they report medians and p95, not a regression flag. - Move heavy numeric or model work to the host via
interop.call_pythonor native calls, and keep only policy in PeaLang.
Capability & deployment policy
- Sensitive
std.*modules require explicit permissions (for examplehttp.request,file.read,concurrent,log). - Use
usingscopes for handles; callscopes.dispose()at the host boundary. - Use signed packages (
pea install --verify --public-key ...) and lockfiles in reproducible builds. - Keep plugin manifests under
.pealang/plugin-policy.jsonand review advisory scans before upgrades. The underlying modules areoffline_cache,workspace,registry_auth,dependency_security,plugin_securityandsigning.
Apply it
Work through the industry examples in the code library, starting with FinTech risk rules and microservice integration.