File size: 9,009 Bytes
f6213fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# solverforge-deliveries WIREFRAME

This file is the architectural map for the deliveries example.

`README.md` explains how to run and use the app. This document explains how the
pieces fit together and where each responsibility lives.

## Documentation Roles

- `README.md`
  Quick start, dependency shape, route list, and user-facing orientation.
- `WIREFRAME.md`
  Architecture, execution flow, and file-map walkthrough.
- `AGENTS.md`
  Repo-specific contribution, validation, and documentation rules.
- `Makefile`
  Local development, validation, and Space/Docker command surface.
- `Dockerfile`
  Hugging Face Docker Space image definition.
- `docs/screenshot.png`
  Current browser screenshot embedded by the README.

## What This Repo Is Teaching

This repo is a complete `solverforge-deliveries` `1.0.1` list-variable
SolverForge app for delivery routing.

It shows how to combine:

- a `Plan` solution with a list planning variable
- route-specific score rules
- SolverForge CVRP construction and local-search hooks
- `solverforge-maps` road-network preparation and route geometry
- retained jobs with snapshots, analysis, cancel, pause, resume, and SSE
- a browser plan viewer built on stock `solverforge-ui` assets

## SolverForge Concepts In Plain Language

- `Delivery`
  Input stop data. The solver assigns delivery IDs into vehicle routes.
- `Vehicle`
  Planning entity. Each vehicle owns one ordered `delivery_order` list.
- `Plan`
  Planning solution. It holds deliveries, vehicles, score, routing mode, view
  state, and prepared routing data.
- hard score
  Missing assignments, capacity overage, late delivery seconds, and unreachable
  route legs.
- soft score
  Total travel seconds.
- retained job
  A solve that lives in memory so the UI can stream events, fetch snapshots,
  pause/resume, cancel, analyze, and delete terminal jobs.

## Runtime Flow

1. The browser loads `static/index.html`.
2. `static/app/main.mjs` loads `static/sf-config.json`.
3. The app fetches `/demo-data/PHILADELPHIA`.
4. `PlanDto::from_plan()` serializes a refreshed transport plan with preview
   state.
5. The browser normalizes the plan, renders summary cards, tables, timelines,
   and a map.
6. When the user clicks Solve, the browser sends the current plan to
   `POST /jobs`.
7. `src/api/routes.rs` deserializes the `PlanDto`, rebuilds a `Plan`, and calls
   `prepare_plan()`.
8. `prepare_plan()` builds straight-line or road-network matrices and attaches
   `PreparedVehicleRouting` to each vehicle.
9. `SolverService` starts a retained solve through `SolverManager<Plan>`.
10. Solver events are converted by `src/solver/service/runtime_payload.rs` into
    UI-facing JSON.
11. The browser consumes `/jobs/{id}/events` and fetches snapshots, analysis,
    and route geometry for exact snapshot revisions.
12. Road-network route geometry follows the `solverforge-maps` graph route,
    projects each leg endpoint onto the nearest road segment, and stitches the
    exact depot or delivery coordinate back in before encoding so each displayed
    leg reaches the visible markers.

## File Map

```text
.
β”œβ”€β”€ Cargo.toml
β”‚   Rust 1.95 crate metadata for app version 1.0.1 and registry dependency
β”‚   requests.
β”œβ”€β”€ solver.toml
β”‚   Embedded search policy for construction heuristics and local search.
β”œβ”€β”€ solverforge.app.toml
β”‚   App metadata, demo IDs, model facts/entities, registry dependency sources,
β”‚   and the `solverforge 0.13.1` runtime target.
β”œβ”€β”€ Makefile
β”‚   Hospital-style local build, validation, and Space/Docker commands.
β”œβ”€β”€ Dockerfile
β”‚   Multi-stage Rust 1.95 Docker image for Hugging Face Spaces.
β”œβ”€β”€ .dockerignore
β”‚   Keeps build artifacts, git metadata, route cache, and Playwright output out
β”‚   of the Docker context.
β”œβ”€β”€ README.md
β”‚   Run guide, dependency shape, API list, and learning path.
β”œβ”€β”€ AGENTS.md
β”‚   Repo-specific rules for future edits.
β”œβ”€β”€ WIREFRAME.md
β”‚   This architectural walkthrough.
β”œβ”€β”€ docs/screenshot.png
β”‚   Current browser screenshot used by the README.
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ domain/
β”‚   β”‚   `planning_model!` manifest, `Plan`, entities, facts, preview structs,
β”‚   β”‚   and route metrics.
β”‚   β”œβ”€β”€ constraints/
β”‚   β”‚   Assignment, capacity, time-window, and travel-time score rules.
β”‚   β”œβ”€β”€ data/
β”‚   β”‚   Deterministic city seed data and generator entrypoints.
β”‚   β”œβ”€β”€ solver/
β”‚   β”‚   Retained-job service and runtime event payload formatting.
β”‚   └── api/
β”‚       Axum routes, DTOs, and SSE endpoint.
β”œβ”€β”€ static/
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ sf-config.json
β”‚   β”œβ”€β”€ generated/ui-model.json
β”‚   └── app/
β”‚       Browser controller, plan models, and UI renderers.
└── tests/
    β”œβ”€β”€ api_contract.rs
    β”‚   Single integration-test crate composed from `tests/api_contract/*.rs`.
    β”œβ”€β”€ api_contract/
    β”‚   Catalog, job, lifecycle, SSE, and live-road modules.
    β”œβ”€β”€ support/
    β”‚   Shared integration-test helpers used by that single crate.
    β”œβ”€β”€ e2e/
    β”‚   Playwright browser tests for the served app.
    └── frontend_models.test.mjs
        Frontend model tests.
```

## Domain And Route Metrics

`src/domain/plan.rs` stays small and macro-facing. It owns the `Plan` struct,
normalization, list shadow refresh, transport refresh, and the
`VrpSolution` implementation.

Route-specific behavior lives under `src/domain/route_metrics/`:

- `preparation.rs`
  Builds matrices and per-vehicle prepared routing data.
- `cvrp_hooks.rs`
  Supplies Clarke-Wright, k-opt, load, capacity, and route replacement hooks.
- `metrics.rs`
  Computes per-vehicle route metrics.
- `scoring.rs`
  Builds preview DTOs and aggregate hard/soft score components.
- `routes.rs`
  Builds straight-line or road-network route geometry snapshots, including
  edge-projected visual endpoints for road legs.
- `insertions.rs`
  Ranks candidate insertion positions for one delivery.
- `types.rs`
  Shared route metrics, snapshots, candidates, and routing trait types.

This split keeps the public domain API stable while avoiding oversized files.

## Demo Data

`src/data/data_seed/entrypoints.rs` exposes three demo IDs:

- `PHILADELPHIA`
- `HARTFORD`
- `FIRENZE`

Each city has a small module with separate depot and grouped visit files. The
generator is deterministic. Every demo has ten vehicle depots, enough scaled
deliveries for those vehicles, reachable road-network coordinates, and enough
aggregate capacity before route ordering.

## API And Retained Runtime

The REST API handles job control and snapshot reads:

- `/jobs` creates a retained solver job.
- `/jobs/{id}` and `/jobs/{id}/status` expose summary state.
- `/jobs/{id}/snapshot` returns an exact or latest snapshot.
- `/jobs/{id}/analysis` runs constraint analysis for a snapshot.
- `/jobs/{id}/routes` returns route geometry for a snapshot.
- `/jobs/{id}/events` streams typed lifecycle events.

The insertion endpoint, `/recommendations/delivery-insertions`, is app-specific.
It prepares the submitted plan, removes the requested delivery from any
existing route, evaluates candidate insert positions, and returns preview plans.

## Frontend Layout

`static/app/main.mjs` is the controller. It owns current plan state, retained
job state, route identity tracking, and event handlers.

Supporting modules are split by responsibility:

- `static/app/models/core.mjs`
  Clone and normalize incoming plans.
- `static/app/models/preview.mjs`
  Draft straight-line preview scoring.
- `static/app/models/timeline.mjs`
  Vehicle and delivery rail models.
- `static/app/models/formatters.mjs`
  Labels, icons, tones, clocks, and durations.
- `static/app/ui/layout.mjs`
  Page shell and stock SolverForge UI component composition.
- `static/app/ui/overview.mjs`
  Summary, route list, vehicle-id keyed route highlighting, map, and timeline
  rendering. The tutorial uses ten distinct map colors for its ten fixed
  vehicles.
- `static/app/ui/data-tables.mjs`
  Read-only vehicle and delivery tables with delivery insertion recommendations.
- `static/app/ui/modals.mjs`
  Analysis and insertion recommendation bodies.
- `static/app/ui/api-guide.mjs`
  Visible API guide content.
- `static/app/ui/lifecycle.mjs`
  Dataset markers and route-identity helpers.

## Validation Surfaces

Use the Makefile as the repo-local workflow:

- `make fmt-check`
- `make clippy`
- `make build-release`
- `make test`
- `make test-e2e`
- `make space-build`
- `make test-live-road`
- `make ci-local`
- `make pre-release`

`make ci-local` includes the Docker image build used by the Hugging Face Space.
The Playwright command uses the publication bundle's root Node dev dependency;
runtime UI assets are served from the declared `solverforge-ui` Cargo crate.

The file-size rule is part of the architecture: keep files below 300 lines and
split by responsibility before they become broad catch-all modules.