PeaBox Lab › PeaLang › Learn › Chapter 7 › Lesson 7

Part II — Intermediate Development · Chapter 7 · Lesson 7 of 7 · 6 min read

Chapter 7 Checkpoint: Typed Employee-Rules Service

Checkpoint level: Intermediate

Purpose: Integrate optional annotations, inference, generics, structural protocols, Option, Result, Future, and flow-aware semantic checks into one portable employee-rules service.

Checkpoint outcomes

By completing this checkpoint, you will demonstrate that you can:

Project scenario

An HR host submits an employee-rules request. The service validates the request, loads an employee through an approved provider, evaluates eligibility and review-routing rules, and returns a portable decision record. A valid lookup may find no employee; provider operations may fail; remote providers may complete asynchronously.

The checkpoint uses synthetic records only. Do not include real employee data, credentials, tokens, or personal identifiers.

7C.1 Required service boundary

The host request is a string-keyed portable map containing:

FieldContract
request_idNonempty string used for correlation
employee_idNonempty synthetic identifier
actionOne of eligibility, review, or summary
effective_dateValidated date string in the documented format
contextOptional string-keyed portable map

Reject missing, malformed, unsupported, or oversized input before invoking domain logic.

7C.2 Typed internal model

Define typed internal values for the validated request, employee record, decision, validation error, provider error, and service error. Every exported function must have parameter and return annotations.

Use precise domain names and units. Do not represent dates, percentages, status codes, or identifiers as interchangeable values merely because their runtime representation is similar.

7C.3 Provider protocol

Define a focused structural protocol conceptually requiring:

Create conceptual local and remote provider implementations. Keep credentials, network clients, database handles, and host objects outside the protocol.

7C.4 Validation contract

The request validator returns Result<EmployeeRequest, ValidationError>. It must check required keys, portable value shapes, allowed actions, date format, collection sizes, string lengths, and unknown-field policy.

Validation errors must include stable codes and safe field context without echoing sensitive values.

7C.5 Lookup outcome

The provider load contract preserves three independent facts:

Do not collapse no match, provider failure, timeout, and cancellation into one null or status string.

7C.6 Required employee fields

Use synthetic employee values containing:

Validate enumerations, ranges, and collection bounds before rule evaluation.

7C.7 Rules to implement

The service must produce one decision for the requested action:

Each decision includes allowed, outcome, reason_code, explanation, and evidence_codes. Keep policy thresholds as constants.

7C.8 Rule precedence

Apply rules in this order:

Document overlaps and ensure the first matching rule produces a complete, internally consistent decision.

7C.9 Generic utilities

Implement at least two relationship-preserving generic utilities, such as selecting a present value with a fallback policy or transforming a list while retaining its element relationship. Do not use broad types when a type parameter preserves the contract.

Any operation required on a generic type must be justified by a focused protocol constraint.

7C.10 Flow-aware implementation requirements

7C.11 Deadline, cancellation, and cleanup

Apply a documented deadline to provider work. Preserve cancellation as an operational outcome rather than reporting not found. Cleanup must run exactly once for every provider attempt.

Use structured exception handling for exceptional runtime failures and Result for expected typed failures. Do not hide resource or permission failures as business decisions.

7C.12 Portable response contract

Return a string-keyed map containing:

Allowed status values are success, not_found, invalid_request, provider_error, timeout, cancelled, and internal_error. Never return a live Future, typed object, exception, host handle, or secret.

7C.13 Service invariants

7C.14 Required test matrix

CaseConditionExpected status
AValid eligible employeesuccess
BValid employee requiring manual reviewsuccess
CValid ineligible employeesuccess
DValid lookup with no matchnot_found
EMissing required request fieldinvalid_request
FUnsupported actioninvalid_request
GProvider typed errorprovider_error
HDeadline exceededtimeout
ICancellation requestedcancelled
JMalformed provider outputprovider_error

Add boundary tests for every numeric threshold and overlap tests for rule precedence.

7C.15 Deliberate semantic defects

Create temporary variants demonstrating:

Record the diagnostic, root cause, minimal correction, and regression test for each.

7C.16 Validation workflow

Use the exact commands exposed by the installed v0.63 toolchain and record their exit statuses separately.

7C.17 Submission package

7C.18 Assessment rubric

AreaEvidence of mastery
ContractsAnnotations, inference, and domain types are precise and coherent.
GenericsReusable utilities preserve type relationships without broad types.
ProtocolsProviders satisfy a focused structural contract.
OutcomesAbsence, failure, deferred work, timeout, and cancellation remain distinct.
Flow safetyEvery path narrows safely, assigns required values, and returns correctly.
BoundariesInputs and outputs are validated, portable, bounded, and redacted.
TestingAll outcome, boundary, overlap, and defect cases are covered.
WorkflowFormatting, source checks, semantic checks, and tests are separate gates.
SecurityLeast privilege, deadlines, cleanup, and secret handling are explicit.

Completion standard: Every required case passes, every invariant holds, project-wide semantic analysis succeeds, and the learner can explain the type and flow of every service outcome.

7C.19 Reflection questions

Chapter 7 summary

You can now state optional type contracts, use inference deliberately, preserve relationships with generics, define structural protocols, model absence and failure explicitly, represent deferred completion, and apply flow-aware semantic analysis across a project.

Transition to Chapter 8

Chapter 8 develops standard-library mastery. The next lesson introduces the stable std namespace and compatibility conventions for dependable library use.

Checkpoint status: Ready for review.

← Previous Next →