Part II — Intermediate Development · Chapter 9 · Lesson 5 of 5 · 6 min read
Chapter 9 Checkpoint: Multi-Module Business-Rules Project
Checkpoint level: Intermediate
Purpose: Integrate module boundaries, cross-module contracts, project layout, validated configuration, and the quality routine in one complete multi-module business-rules project.
Release note: module names, commands, and configuration keys are illustrative. Verify them against the installed v0.63 release before relying on them.
Checkpoint outcomes
- divide a business problem into modules with one responsibility each;
- define exported contracts and keep dependencies pointing downward;
- organize a project with a clear layout, validated configuration, and documentation;
- keep policy rules pure and isolate any capability use in narrow adapters;
- apply formatting, checking, and tests as separate non-mutating gates;
- review the project for cycles, contract drift, and maintainability.
9C.1 Scenario: Order discount and eligibility service
A retailer needs a rules project that decides, for each order, whether it is eligible for a discount, which discount applies, and why. The project uses synthetic data only. It has no network, secret, or process access. Each decision returns a discount amount, a reason code, and a short message.
9C.2 Required modules
| Module | Responsibility | May depend on |
|---|---|---|
| types | Constants, reason codes, and decision shape | Nothing |
| validation | Checks order input and returns clear errors | types |
| rules | Pure discount rules and precedence | types |
| config | Loads and validates thresholds and rates | types, validation |
| service | Combines validation, configuration, and rules into one exported decision function | types, validation, rules, config |
| main | Entry point that prints a labeled report for sample orders | service |
Dependencies point downward. No lower module may import a higher one, and no two modules may import each other.
9C.3 Contracts
The service module exports one function, decide_order, which accepts a string-keyed map and returns a string-keyed map.
| Direction | Field | Type | Rule |
|---|---|---|---|
| Input | order_id | string | Required, nonempty |
| Input | customer_tier | string | One of standard, gold |
| Input | subtotal | number | Zero or greater |
| Input | item_count | integer | One or greater |
| Output | allowed | boolean | True when the input is valid |
| Output | discount | number | Never negative and never above the subtotal |
| Output | reason_code | string | VALIDATION_ERROR, GOLD_BULK, GOLD, BULK, or NONE |
| Output | message | string | Short, labeled, free of secrets |
9C.4 Policy
- Invalid input returns no discount with reason VALIDATION_ERROR.
- A gold-tier order with item count at or above the bulk threshold receives the gold rate plus the bulk rate, reason GOLD_BULK.
- Otherwise a gold-tier order receives the gold rate, reason GOLD.
- Otherwise an order at or above the bulk threshold receives the bulk rate, reason BULK.
- All other orders receive no discount, reason NONE.
- The discount is capped at the subtotal.
9C.5 Project layout
order_rules/
pea.toml # project metadata (illustrative)
config/
rules.defaults # thresholds and rates, no secrets
src/
types.pea
validation.pea
rules.pea
config.pea
service.pea
main.pea
tests/
rules_tests.pea
service_tests.pea
docs/
contracts.md # derived from exported comments
9C.6 Requirements
- Each module states its single responsibility in a header comment.
- Only service and config are exported to main; helpers are internal.
- Policy values come from validated configuration, not unexplained literals.
- The rules module performs no file, network, or process work.
- Every exported function has an intent comment covering inputs, result, and failure behavior.
- Reason codes are stable constants defined once in types.
- Output is labeled and deterministic.
9C.7 Constraints
- Use only constructs covered through Chapter 9.
- Use synthetic data only; include no secrets or personal data.
- Do not enable process execution and request no capability beyond reading the local defaults file in config.
- No import cycles; dependencies follow the table in 9C.2.
- Do not change the exported contract without updating its comment, callers, and tests.
9C.8 Acceptance criteria
- The format check passes in non-mutating mode.
- The checker reports no error.
- All tests in 9C.10 pass.
- The dependency review finds no cycle and no upward import.
- Invalid configuration is rejected with a clear message at load time.
- The documentation matches the exported comments.
- The security statement confirms that no unnecessary capability was used.
9C.9 Reference design
The core is a pure function that takes validated values and returns a decision. Validation and configuration sit beside it and never call upward. The service function is the only place where they are combined.
- types defines the reason-code constants and a helper that builds a decision map.
- validation exports validate_order, returning either a list of error messages or an empty list.
- config exports load_rules, which reads the defaults, checks that rates are between 0 and 1 and that the bulk threshold is at least 1, and returns a map.
- rules exports apply_rules, which uses an ordered chain: gold with bulk, gold, bulk, none. It caps the discount at the subtotal.
- service exports decide_order, which validates first, returns VALIDATION_ERROR on failure, and otherwise calls apply_rules with loaded configuration.
- main loops over a fixed list of sample orders and prints a labeled report for each.
Keeping rules pure means it can be tested without any configuration file, and a different host could reuse it unchanged.
9C.10 Test matrix
| Case | Tier | Subtotal | Items | Expected reason |
|---|---|---|---|---|
| A | standard | 50 | 1 | NONE |
| B | gold | 50 | 1 | GOLD |
| C | standard | 50 | at bulk threshold | BULK |
| D | gold | 50 | at bulk threshold | GOLD_BULK |
| E | standard | 50 | one below bulk threshold | NONE |
| F | gold | 0 | 1 | GOLD with discount 0 |
| G | gold | 10 | at bulk threshold, with combined rate above 100% | GOLD_BULK, discount capped at 10 |
| H | silver | 50 | 1 | VALIDATION_ERROR |
| I | standard | -5 | 1 | VALIDATION_ERROR |
| J | standard | 50 | 0 | VALIDATION_ERROR |
| K | standard | 50 | 1, with an empty order_id | VALIDATION_ERROR |
Cases C, D, and E test the exact bulk boundary. Case G tests the cap. Cases H–K test validation. Add a configuration test that loads a rate of 1.5 and confirms rejection.
9C.11 Validation workflow
- Run the format check, then the checker, then the tests, as three separate steps.
- Predict each case result before running it.
- Record pass or fail with a short note.
- Review dependency direction by listing each module's imports.
- Regenerate documentation and confirm it matches the comments.
9C.12 Debugging requirement
Introduce one temporary defect: swap the gold-with-bulk branch below the gold-only branch. Confirm that case D fails with reason GOLD instead of GOLD_BULK. Restore the correct order and rerun. Then add a temporary upward import from rules to service and confirm that your dependency review catches it. Remove both defects before submission.
9C.13 Submission package
- all project files in canonical formatting;
- a dependency table listing each module's imports;
- the prediction-and-results table for cases A–K and the configuration test;
- format-check, source-check, and test evidence, each recorded separately;
- a note describing the temporary defects and their corrections;
- the generated contracts document;
- a short security statement confirming that no unnecessary capability or process permission was used.
9C.14 Assessment rubric
| Area | Evidence of mastery |
|---|---|
| Module design | Each module has one responsibility and a stated contract. |
| Dependency direction | No cycles and no upward imports. |
| Correctness | All policy branches, boundaries, and the cap behave as specified. |
| Configuration | Values are validated at load time and contain no secrets. |
| Quality routine | Format, check, and tests run separately in non-mutating mode. |
| Maintainability | Names, comments, and exports follow the agreed conventions. |
| Security | No capability beyond the documented local read. |
| Explanation | The learner can justify the layout and trace any decision. |
Completion standard: Every test passes, every acceptance criterion is met, and the learner can explain why the dependency table prevents cycles.
Reflection questions
- Which module boundary was hardest to place, and why?
- What would break if rules imported config?
- How did keeping rules pure simplify testing?
- Which conventions caught a defect before the tests did?
- What would you change in the contract to add a new tier safely?
- Which responsibilities would remain in a host application?
Optional extensions
- Add a platinum tier while preserving existing precedence.
- Add a second configuration profile and a test that selects it.
- Add a reason-code coverage test that fails when a code has no case.
- Add a changelog entry process for contract changes.
Chapter 9 summary
You can now draw module boundaries, control imports and exports, design contracts that dependencies respect, lay out and configure a project safely, and keep it healthy with formatting, checking, review, and a repeatable quality routine. The checkpoint brought these together in a multi-module business-rules project.
Transition to Chapter 10
Chapter 10 turns to testing and quality engineering. The next lesson covers native test syntax and expectations, so the tests used informally here become a first-class part of the project.
Checkpoint status: Ready for review.
n