Skip to content
English

Add a feature without breaking deployed code

Imagine the mug shop from Test shop behavior has been deployed. Customers can order its final mug through PlaceOrder. Other programs may already call that command, and its accepted OrderPlaced events are part of the shop’s history. Those callers and facts now depend on the behavior we shipped.

The shop needs a new online policy: keep one mug for customers buying in person. We will add that behavior alongside the deployed command. In NeoHaskell, a deployed command, event, or query keeps its identity and implementation. New requirements get a new slice rather than a rewrite of deployed code, as Model the whole process explains.

At one mug remaining, rewriting PlaceOrder would turn an existing caller’s accepted order into a rejection. A separate online command gives the new policy its own route. Each path below starts with its own example shop at one mug:

Rewrite the deployed command

CallerPlaceOrderChanged ruleRejected1 mugExisting caller's behavior breaks

Add a separate slice

CallerPlaceOrder0 mugsOnlinePlaceOnlineOrder1 mugReserved

The saved events also depend on a compatible reader. Imagine renaming the required entityId field to shopId under the same event identity. Existing records would still contain entityId; a reader that requires only the new field could no longer decode them. That incompatible rewrite can stop rebuilding the shop from its history:

Keep the original event reader

entityIdentityIdRequired fieldReplay continues

Rewrite the event reader

entityIdshopIdRequired fieldReplay stops: field does not match

Freezing OrderPlaced keeps its reader available for those records. If a new requirement needs a different event shape, introduce a new event identity and keep the old reader for retained history.

Start from the tested shop before the reserve policy exists. Save and review the current work first. From the project root, freeze its domain files:

Terminal
neo lock --all

Neo discovers files beneath Commands, Events, and Queries, records their paths in .locked-files, and creates a local Git commit containing the manifest and those files. Review that commit as the deployed baseline. Keep the manifest in version control. Locking is an explicit preparation step; imagining a deployment does not run it for you.

The build now protects that baseline. If an agent changes the acceptance condition in the locked src/Shop/Commands/PlaceOrder.hs, running:

Terminal
neo build

refuses the build before compilation. The diagnostic identifies src/Shop/Commands/PlaceOrder.hs as a modified locked file. Restore the original contents, keeping unrelated work. Do not remove its lock or bypass the check to introduce the reserve policy. The protection catches a changed deployed file even when that edit would otherwise compile and pass newly edited tests.

This check is part of neo build. Running tests alone does not establish that the frozen files are unchanged. The manifest covers the discovered domain files; it does not automatically freeze every application configuration or feature-composition file.

The existing PlaceOrder continues to accept the final mug. The new command, PlaceOnlineOrder, uses the same shop and records the same fact when an order succeeds. Their one-mug boundary differs. The new command returns Reserved for in-person sales when it rejects the reserved mug:

Stock Deployed command Online command
3 or 2 Accept Accept
1 Accept Reject reserve
0 Sold out Sold out
No shop Shop not found Shop not found

Ask your agent to add the feature, after reading the project’s active guidance:

The tested mug shop has been deployed and its Commands, Events and Queries are locked. Preserve every locked file byte-for-byte, including PlaceOrder and its existing tests and public behavior.
Create src/Shop/Commands/PlaceOnlineOrder.hs with a distinct PlaceOnlineOrder command. Copy the relevant existing command structure into this new module. It acts on ShopEntity and accepts exactly one existing OrderPlaced event at stock 3 or 2. At stock 1 reject with Reserved for in-person sales; preserve Sold out at zero and Shop not found for a missing shop. Permit WebTransport for this new command.
Create a separate Shop.OnlineFeature with an onlineShopFlow and a new command slice. Register it alongside counterFlow and shopFlow in App.hs. Reuse the existing event and query unchanged. Add separate tests for the new command; retain checks that deployed PlaceOrder still accepts the final mug. Preserve historical replay 0,3,2,1,0 and unchanged history after rejection.
Verify the new /commands/place-online-order route and asynchronous stock 3,2,1, followed by reserve rejection without another event. Keep /commands/place-order unchanged. Run neo build and neo --ci test. Show the added code, unchanged locked files, actual results and any skipped checks.

Copying a small feature gives the new behavior its own place to evolve. We deliberately accept this duplication instead of adding a condition to the deployed command or changing a shared helper beneath it. Model the whole process explains the aim: comparable additions should require comparable work without reopening earlier slices.

Inspect src/Shop/Commands/PlaceOnlineOrder.hs, not a patch to PlaceOrder.hs. The new command has its own type and filename. Its EntityOf still names ShopEntity, and its accepted branch still emits the existing OrderPlaced. Inside the Just shop branch, the new decision is:

if shop.availableMugs <= 0
then Decider.reject "Sold out"
else if shop.availableMugs > 1
then Decider.acceptExisting [
OrderPlaced OrderPlaced.Event {
entityId = shop.shopId
}
]
else Decider.reject
"Reserved for in-person sales"

The two conditions distinguish an empty shop from its reserved final mug. A rejection records no event, so we need no new event shape and no change to stock replay or AvailableStock. The omitted Nothing branch still returns Shop not found.

The new feature connects that command to its existing event. This excerpt belongs in src/Shop/OnlineFeature.hs, with the appropriate module declaration and imports:

onlineShopFlow :: FeatureAction
onlineShopFlow = feature "Online mug shop" do
chapter "Online ordering" do
slice "Place an online order" do
entity @ShopEntity do
command @PlaceOnlineOrder do
produces @OrderPlaced.Event

Import onlineShopFlow into App.hs and add it alongside the existing flows:

EventModel.features do
counterFlow
shopFlow
onlineShopFlow

Run neo inspect sync, then open neo ide. In Ungrouped, choose Fit View and find both commands. Trace each to OrderPlaced, then follow that event into AvailableStock. The new route joins the existing stock calculation; the deployed route remains separately identifiable.

Adding the new route does not move callers automatically. Existing callers of /commands/place-order keep the deployed behavior and can still buy the final mug. Move online callers to /commands/place-online-order when they are ready. Until that migration is complete, the old route remains a way to buy the last mug. An application-wide reserve policy requires retiring or restricting that entry point after its consumers have moved; locking source alone does not change access to it.

A shop that already sold three mugs has a real history ending at zero. Replay still gives:

Initial state 0
ShopOpened 3
First OrderPlaced 2
Second OrderPlaced 1
Third OrderPlaced 0

Replay applies recorded facts without asking the new command to approve them again. Neither the event nor the query should be rewritten to pretend the third order never happened.

Run neo build to check the locked files and compile the addition, then neo --ci test for behavior. Keep the old command’s tests and add the new policy’s cases beside them. At stock one, the old command must accept while the new one rejects. Both should preserve the original sold-out and missing-shop replies. Check exact accepted event payloads and verify that rejection adds no event.

Use the running application’s built-in documentation, introduced in Place an order over HTTP, to open a fresh shop with an unused identifier. Send two requests to place-online-order, using that shop identifier. Its stock query should settle at one. A third online request returns Reserved for in-person sales; repeating it must leave both history and stock unchanged. Verify the old route’s last-mug acceptance on a separate shop so the two checks do not consume each other’s stock.

When the new feature is ready for deployment, review its checks and run neo lock --all again to include its new domain file. Future changes to this deployed behavior will get another identity and slice of their own.