Skip to content

M8-S1 — Your engine workbench from source

Section titled “M8-S1 — Your engine workbench from source”
ModuleM8 — Extend the engine from source (source strand)
After this session you canRun the platform from source, make and test a change in one service, and say when an engine change is justified instead of a documented extension point.
Time88 min with a facilitator · about 88 min on your own
Before you startThe strand’s prerequisite (below) met and its self-check passed. A source checkout of the platform’s three code bases that you may build and run, with the development tools installed (source-strand prework). Modules 2, 3 and 6, in particular M6-S4 (the extension map).
Go deeperRun the platform from source · Running locally · How to extend the platform

◆ Orientation (1 min) — The same need, the other way

Section titled “◆ Orientation (1 min) — The same need, the other way”

Larkspur needs condition monitoring. The extension strand meets that need through documented extension points: a parameterised Function, an inbound feed and a webhook (M7-S1). You are on the source strand: you meet it inside the engine, by giving the platform a new kind of value, a Meter reading field type, that every builder can add to any entity, on the web and on the phone, with no code. Over four sessions you add the type to the engine (M8-S2), give it a web control the designer offers (M8-S3), render it on mobile or declare its fallback, and prove that a builder can use it without code (M8-S4).

First you need a workbench: the platform running from source on your machine, and the discipline to change engine code safely. That is this session. You also learn the judgement that matters more than any code: when an engine change is the right answer at all.

▤ Concept (10 min) — Three code bases, one convention, and when to change the engine

Section titled “▤ Concept (10 min) — Three code bases, one convention, and when to change the engine”

The source strand assumes more than the rest of ACM. Before this session you should:

  • read and write TypeScript for Node.js services, including classes, decorators and dependency injection;
  • read and write Angular components, including a form control that implements ControlValueAccessor;
  • read and write Dart with Flutter, including exhaustive switch statements over enums;
  • have a source checkout of the platform that you are allowed to build and run, and the tools its local-development guide lists installed and working: Node.js and pnpm at the versions the workspace declares, Docker, the Flutter SDK with melos, and a code editor.

The self-check. You took it when you chose your strand: the three questions on the course page and the fuller check in M0-S1 (dependency injection, writeValue, Dart’s exhaustive switches). If any of the three ideas was new to you, change to the extension strand now: M7 meets the same need and leads to the same certification. The first practice segment below repeats the check in the context of this session.

The platform you have studied from the outside is built from three code bases that are built, tested and released separately:

flowchart LR
  subgraph BE["Services workspace (TypeScript, Node.js)"]
    SH["Shared package<br/>enums, message classes"]
    DS["Data service<br/>casters, validators"]
    OT["Other services"]
  end
  subgraph WEB["Web front end (Angular)"]
    WT["Shared types<br/>mirrored enums"]
    PL["Plugins<br/>field controls"]
  end
  subgraph MOB["Mobile app (Flutter, Dart)"]
    MD["Domain package<br/>mirrored enums"]
    MW["Field widgets"]
  end
  SH -. "same numbers" .-> WT
  SH -. "same numbers" .-> MD
  DS -- "HTTP API: stored values" --> PL
  DS -- "HTTP API: stored values" --> MW

What to notice: there is no shared code between the three, only shared contracts: the HTTP API’s value shapes, and enum values re-declared with the same numbers on each side. A change to one of those contracts is a change in all three code bases, released together.

  • The services workspace holds the back-end services and their shared packages. It is one workspace driven by a task runner, so shared packages build before the services that use them; the root scripts build everything, start a minimal set of services or all of them, and run each service’s tests. Tests are per service; there is no single command that tests all three code bases.
  • The web front end is one workspace holding the designer and the portal. Its field controls are plugins resolved by name at run time: the plugin system you met in M3-S5.
  • The mobile app is a multi-package Flutter workspace managed with melos. Its domain package is pure Dart and re-declares the platform’s enums so it can read the API’s values without importing anything from the other code bases.

You met the most important of these shared contracts in M2-S3: an enum value that is stored in records is a wire contract and is never renumbered.

M2-S1 named the convention that makes much of the engine pluggable; now you work with it. A service does not keep a central list of its casters, validators, fetchers, exporters or message handlers. Instead, at start-up, a set of booters each scan one folder of the compiled service for files with one suffix, read a static field on each class they find, and register the class in the application’s dependency-injection container under a key made of a prefix and that field.

To addYou createThe class declaresIt is registered under
A field-type castera file ending .caster.ts in the data service’s casters folderstatic castType (the field type’s enum value)caster.<value>
An entity validatora file ending .validator.ts in its validators folderthe entity name and phasesvalidator.…
A message handlera file ending .handler.ts in a service’s handlers folderthe message it handlesits class name

What to notice: adding most engine parts means creating one file with the right suffix and the right static field. There is no registry to edit, and a missing file fails quietly, which is why tests matter.

Two consequences shape how you work. First, the booters scan the compiled output, so a new file does nothing until the service is rebuilt. Second, a lookup for a type with nothing registered returns nothing rather than failing: for casters, the engine then stores the submitted value unchanged. A forgotten caster is not an error you will see; it is a value that silently skips its normalisation.

Running the platform from source is the same topology you operated in ACDE, rearranged for one machine: the datastores and the message bus run as containers, the services run as native processes from the workspace, the web front end runs from its development server, and the gateway runs as a container in front of them. Four facts save hours:

  1. Configuration is generated, not copied. Installing the workspace creates your local configuration files from their examples and never overwrites them. Where the guide asks you to edit a tracked file to make a single-machine setup behave, that edit is local-only: it never goes into a commit.
  2. Order matters. The datastores first; then the services, with their bus queues bound; only then the first-time setup. The setup provisions the first tenant over the bus, and a message published before anyone is listening is dropped, which shows up later as endless space or domain errors.
  3. The host name is load-bearing. The platform finds the tenant and space from the address you use, exactly as in ACDE. Use the address the local configuration names, consistently, in the browser and in your API calls.
  4. Read the failure as progress. On the way up you typically see “connection refused”, then a space error, then a domain error, then a real answer. Each is a different layer answering, and each means the layer before it now works.

The first administrator is the one you name in the local migration configuration before the first run. The setup gives that account a one-time password-reset link instead of a password; on a workstation there is no mail server, so you read the link from your local identity database, as the lab shows. A practice environment also needs a valid license that includes administration; your platform team supplies the license values with the checkout, as an operator supplies them in ACDE M2-S1.

The engine has documented recipes for its own extension: a new field type, a new view type, a new reaction action, a new bus message with its handler, a new service, a new migration, and a family of rules for keeping new code tenant-aware (carry the space on every message, never build a cache key by hand, never default to the root space). M8 uses the first and the tenant rules; you will recognise the others when you review someone’s engine change.

Knowing the recipes is the easy part. The judgement is whether to use one. An engine change is justified only when all four of these hold:

TestThe questionCondition Monitoring as a field type
No documented point will doCan configuration, a Function, the API, events or a webhook meet the need? (M6-S4)Partly: M7 meets the alarm need without engine code. A value kind with its unit, checked on save and offered in the designer, is not something a Function can add.
It is generalWould every tenant benefit, not just one customer?Yes: any maintenance, utility or laboratory solution records meter readings.
It will be carriedWill the platform’s engineering own it in every future release, or will you maintain a fork?It must be accepted upstream; otherwise every platform upgrade becomes a merge.
You can change all three code basesCan the team change, test and release the services, the web front end and the mobile app together?Yes, on this strand; the mobile half may be a declared fallback.

What to notice: the first test is the extension map from M6-S4, applied honestly; the other three are about ownership. A change that passes the first test but fails “it will be carried” is a fork, and a fork costs more at every upgrade than it saved at the start.

If any test fails, take the documented point, even if the engine change would be quicker today. That is why both strands exist: they are not two ways to do the same job, but two answers to two different situations.

▶ Guided demo (20 min) — The bring-up, step by step

Section titled “▶ Guided demo (20 min) — The bring-up, step by step”

Starting state. Your checkout builds; Docker is running; nothing of the platform is started. Follow along in your own terminal, with a facilitator or alone: the lab starts from the platform this demo brings up. Check the expected result after each step before you go on.

  1. Install the services workspace from its root. The install runs the local-configuration script.

    Terminal window
    pnpm install

    Expected: for each local configuration file it creates, the install prints one [setup:local] created … line; for files that already exist it prints nothing.

  2. Start the datastores and the bus as containers.

    Terminal window
    docker compose up -d database rabbitmq redis

    Expected: three containers running (docker compose ps).

  3. Build and start the minimal set of services, in their own terminal, and leave it running.

    Terminal window
    pnpm build && pnpm start:main

    Expected: each service logs that it is listening; the data service and the identity service report their bus connection.

  4. Check that the bus queues have consumers before anything is provisioned.

    Terminal window
    docker compose exec rabbitmq rabbitmqctl list_queues name consumers | grep -E "data-service-queue|users-service-queue"

    Expected: both queues listed with 1 consumer. If either shows 0, the first-time setup would be lost; wait for the service and check again.

  5. Run the first-time setup once the queues are bound.

    Terminal window
    pnpm migrate

    Expected: the migration ends without errors, and the gateway answers a real response instead of a space error.

  6. Start the web front end from its own workspace, in a third terminal.

    Terminal window
    pnpm start

    Expected: the development server compiles and prints the local address of the portal.

Screenshot pending Three terminals side by side: the datastore containers running, the services' start-up logs, and the web development server's compiled message What to notice: the services and the web front end each own a terminal and keep running; the containers run in the background. Closing a terminal stops what it started.

✓ Practice (4 min) — Ready for the strand?

Section titled “✓ Practice (4 min) — Ready for the strand?”
PracticeChoose one answer.

You add a new caster file to the data service’s casters folder and restart the service without rebuilding. A record saved afterwards stores the raw value unchanged. Why?

  1. Explanation for A

    ✗ Incorrect

    Not this one: the caster never ran; the booters scan the compiled output, which does not yet contain your file. See the booter convention.

  2. Explanation for B

    ✓ Correct

    Correct: without a rebuild there is no compiled file to discover, and a missing caster fails quietly by storing the value as sent. See the booter convention.

  3. Explanation for C

    ✗ Incorrect

    Not this one: there is no central list; the file, its suffix and its static field are all the registration there is. See the booter convention.

Check your answer

The correct answer is B. Open the explanation under each option to see why.

PracticeChoose one answer.

On a fresh machine, you run the first-time setup before starting the services. Everything seems to succeed, but the portal keeps answering with a space error. What happened?

  1. Explanation for A

    ✓ Correct

    Correct: provisioning travels over the bus, and a message nobody is bound to receive is lost; start the services, confirm consumers, then set up. See the bring-up.

  2. Explanation for B

    ✗ Incorrect

    Not this one: the space error comes from the platform’s tenancy, which was never provisioned; the browser is not the cause. See the bring-up.

  3. Explanation for C

    ✗ Incorrect

    Not this one: the error names the space, which the lost provisioning would have created; it is an ordering problem. See the bring-up.

Check your answer

The correct answer is A. Open the explanation under each option to see why.

PracticeChoose one answer.

In Dart 3, the mobile app’s domain package gains a new value in an enum that several switch statements cover, none with a default. What does that give you when you add a field type?

  1. Explanation for A

    ✗ Incorrect

    Not this one: exhaustive switches are checked when you build, long before any user opens a record. See the prerequisite.

  2. Explanation for B

    ✓ Correct

    Correct: without a default, the compiler refuses an incomplete switch, so it lists every place the new value must be handled. See M0-S1: the source check.

  3. Explanation for C

    ✗ Incorrect

    Not this one: Dart does not fall through to another case; an unhandled enum value in an exhaustive switch is an error. See M0-S1: the source check.

Check your answer

The correct answer is B. Open the explanation under each option to see why.

⛏ Lab (47 min) — Bring it up, change it, catch it, revert it

Section titled “⛏ Lab (47 min) — Bring it up, change it, catch it, revert it”

Goal. Your source workbench running with the Larkspur suite in it, and one complete change cycle in the data service: a test that states today’s behaviour, a change the test catches, and a clean revert.

Before you start. Your checkout on a clean working tree (git status shows nothing to commit) in all three code bases. The Larkspur pack from the workbench. Work in the services workspace unless a step says otherwise.

Bring it up

  1. Name your own first administrator: in the data service’s local migration configuration (the .env.migration file the install created), set the administrator’s email to your own address at example.com and choose a user name. Save. This file is local and ignored by version control.

  2. Confirm the platform you brought up in the demo is still running: the portal’s sign-in page opens at the local address the web server printed. If you skipped the demo, do its six steps now.

  3. Read your one-time password-reset link from the local identity database:

    Terminal window
    docker compose exec database mongosh --quiet --eval \
    'const t=db.getSiblingDB("identity-users").resettokens.findOne(); print("/identity/reset-password/"+t.resetToken)'

    Open the printed path on your local platform address, set a password, and sign in. Expected: the portal’s welcome page.

  4. Load the Larkspur suite into your workbench with the seed from the Larkspur pack, pointed at your local address:

    Terminal window
    export BASE=<your local platform address>
    node larkspur-pack/seed/seed.mjs --base-url "$BASE"

    Expected: the seed reports the four modules and the people created. Open Administration › Applications › Modules and check that Asset Maintenance is there.

Run one service’s tests

  1. Find where the data service keeps its unit tests and its casters:

    Terminal window
    find services/data-service/src -path '*__tests__*' -name '*.ts' | head
    ls services/data-service/src/casters

    Expected: a tests folder with unit and integration tests, and one caster file per field type that has one (number, text, date and others).

  2. Run the data service’s tests once, to see a green baseline:

    Terminal window
    pnpm --filter data-service test:unit

    Expected: the run ends with passing tests and no failures. Note how long it took. This is the unit suite: it needs the built service and no database. The plain test script also runs the integration tests, which start a MongoDB container and need Docker.

Make a change that a test catches

  1. Write a unit test beside the others that states the Number caster’s current behaviour for garbage input: it returns NaN. Name it for what it documents.

    import {expect} from '@loopback/testlab';
    import {NumberCaster} from '../../../casters/number.caster';
    describe('NumberCaster (documents current behaviour)', () => {
    it('turns unparseable input into NaN', () => {
    expect(Number.isNaN(new NumberCaster().cast('twelve'))).to.be.true();
    });
    });
  2. Rebuild and run the tests. Expected: your new test passes, with the others.

  3. Now change the behaviour: in the Number caster, return null when the result is not a finite number.

  4. Rebuild and run the tests again. Expected: your test fails, naming the case: the change was caught.

  5. Read what you have learned: the caster’s silent NaN is real behaviour that stored records may already contain, so changing it is a behaviour change for every tenant, not a fix. Write one sentence in your notes on what a change like this would need (a decision, a migration of stored values, release notes).

Revert cleanly

  1. Restore the caster to its committed state:

    Terminal window
    git restore services/data-service/src/casters/number.caster.ts
  2. Decide what to do with your test: keep it in a local branch as documentation, or delete it. Do not leave it untracked in the working tree.

  3. Rebuild and run the tests a last time. Expected: green, as in step 6.

  4. Confirm the working tree is clean, or contains only your deliberate local branch:

    Terminal window
    git status
  5. Write two lines in your notes for M8-S2: the field type you will add must not repeat the Number caster’s silent failure, and the decision is visible in a test from the start.

Check.

  • The portal opens on your local address and you are signed in as your own administrator.
  • Asset Maintenance and the other Larkspur modules are in Modules.
  • You ran the data service’s tests and saw them pass.
  • Your test of the Number caster passed before your change and failed after it.
  • The caster is restored and the tests pass again.
  • git status shows no stray changes in the services workspace.
  • Your notes say what changing an existing caster’s behaviour would need.

Pass condition. A from-source platform with the Larkspur suite, and a recorded change cycle: a test passed, the change made it fail, the revert made it pass, and the working tree is clean.

If something goes wrong

What you seeWhat to do
EADDRINUSE for a metrics port when services startTwo services chose the same metrics port. Give each a distinct METRICS_PORT, as the local-development guide’s launcher does.
Endless space or domain errors after the setupThe setup ran before the queues had consumers. Restart the services, confirm both queues show 1, and run the setup again (it is safe to re-run).
The reset link opens a page that says the link is invalidUse the same host as the portal, and the newest token: run the step 3 command again after any new setup run.
The seed answers 401You are not signed in to the API: the seed signs in with LK_ADMIN_EMAIL and LK_ADMIN_PASSWORD; export them for your local administrator.
Tests fail before you changed anythingYour checkout is not on a released line, or a dependency is missing. Run the build again and record the failing test; do not start M8-S2 on a red baseline.
git restore leaves the caster changedYou edited the compiled file in dist. Edit and restore the source under src, then rebuild.
Model answer

What a finished lab looks like. Your terminal history shows the bring-up order: containers, then services with both queues at 1 consumer, then the setup, then the web server. The seed’s summary lists Supplier Management, Procurement, Asset Maintenance and Quality and Compliance.

The change cycle in your notes:

  • Baseline: data service tests green.
  • Test added: “NumberCaster turns unparseable input into NaN” — passes.
  • Change: return null for non-finite results — the new test fails, as it should.
  • Reverted with git restore; rebuilt; green again; test kept on the local branch doc/number-caster.

What a real change to an existing caster would need: a recorded decision (records may already hold NaN), a migration or a read-time rule for the stored values, a note in the release, and agreement from the platform’s engineering, because the behaviour belongs to every tenant.

For M8-S2: the Meter reading caster returns null for anything it cannot read as a finite value with a unit, and its first test says so.

✓ Practice (5 min) — Engine change or not?

Section titled “✓ Practice (5 min) — Engine change or not?”
PracticeChoose one answer.

Larkspur’s quality team wants supplier audit scores to be recalculated every night from three entities, with a formula they can change themselves. Is an engine change justified?

  1. Explanation for A

    ✗ Incorrect

    Not this one: scheduled work, reactions and formulas already do this; the first test, “no documented point will do”, fails. See when an engine change is justified.

  2. Explanation for B

    ✓ Correct

    Correct: a scheduled job with a formula the team can edit needs no engine code; changing the engine would add upgrade cost for nothing. See when an engine change is justified.

  3. Explanation for C

    ✗ Incorrect

    Not this one: being able to change the engine is one test of four; the need must first be out of reach of documented points. See when an engine change is justified.

Check your answer

The correct answer is B. Open the explanation under each option to see why.

PracticeChoose one answer.

A partner proposes a new reaction action that sends work orders to one customer’s own maintenance system in its private format. The platform’s engineering team will not take it upstream. What does the four-test rule say?

  1. Explanation for A

    ✓ Correct

    Correct: a customer-specific need fails “it is general” and “it will be carried”, and documented points can reach the customer’s system. See when an engine change is justified.

  2. Explanation for B

    ✗ Incorrect

    Not this one: a recipe tells you how to change the engine, not whether to; the ownership tests fail here. See when an engine change is justified.

  3. Explanation for C

    ✗ Incorrect

    Not this one: the ownership problem remains whatever the scope; and an action without its editor in the designer cannot be configured. See when an engine change is justified.

Check your answer

The correct answer is A. Open the explanation under each option to see why.

PracticeChoose one answer.

During the bring-up you edit a tracked configuration file so the gateway reaches your native services. What do you do with that edit?

  1. Explanation for A

    ✗ Incorrect

    Not this one: the edit makes one machine behave like a deployment; on a shared branch it would break other setups. See the bring-up.

  2. Explanation for B

    ✓ Correct

    Correct: local-only edits stay local; generated configuration and ignored files carry the rest. See the bring-up.

  3. Explanation for C

    ✗ Incorrect

    Not this one: reverting is safe but wasteful; a local-only commit keeps it out of shared history without the daily rework. See the bring-up.

Check your answer

The correct answer is B. Open the explanation under each option to see why.

PracticeChoose one answer.

Why did the lab ask you to write a test of the Number caster’s current behaviour before changing it?

  1. Explanation for A

    ✗ Incorrect

    Not this one: nothing enforces a test per caster; the test was your own safeguard. See the lab.

  2. Explanation for B

    ✗ Incorrect

    Not this one: the silent result is current behaviour that stored records may rely on; changing it is a decision, not a fix. See the lab.

  3. Explanation for C

    ✓ Correct

    Correct: a test of today’s behaviour turns an engine change into a visible, reviewable difference instead of a silent one. See the lab.

Check your answer

The correct answer is C. Open the explanation under each option to see why.

You can run the platform from source: datastores and bus as containers, services native with their queues bound before the first-time setup, the web front end from its development server, your own first administrator, and the Larkspur suite seeded in. You have changed engine code under a test and reverted it cleanly. And you can apply the four tests that decide whether an engine change is justified at all.

Next. In M8-S2 you add the Meter reading field type to the engine: its value in the shared contract, its caster with tests, and the places downstream that must know it, in a tenant-safe way. You will use the change cycle from this lab for every step.