/

Ambitwin · source-available, not open source · © 2026 Nicolò Ghignatti

Digital Twin

Keywords on this page
WordMeaningExample
TwinThe stored shape of a building: rooms with position, size, capacityHQ, 12 rooms
UploadOne POST /register, tracked by a handle until ready or failedhandle 5c1e…
HandleThe id the caller polls. It is the new building’s own id5c1e0b7a-…
JobThe stored upload waiting for the worker, in pending_uploadsstatus: pending
LeaseA time during which one worker owns a job. When it runs out, the job can be taken again30 s
WorkerA loop inside the service that takes the next job and provisions itworker.rs
ProvisionStore the twin, announce it to telemetry, ask dashboard for starting columns
PlacementWhere one sensor sits in the building: x, y, zrouter at (4, 0.5, 6)
RouterA Wi-Fi access point. It hears phones and reports how loud each one isap-1
SignalHow loud a router hears a phone, in dBm. Closer means less negative−51 dBm
PortA Rust trait the use cases need, in their own wordsBuildingStore
AdapterThe code that does a port’s job with a real technologyMongoBuildings
CedarThe policy language the access rules are written inRead, Edit
CellA 0.5 m square of floor, used to estimate where a phone is(13.75, 6.25)
TrackOne phone followed from tick to ticktoken 7f3c…
BurstA short flurry of signals from a phone that is not connected to a routera passer-by
DotOne estimated person sent to the browserLobby, floor 0
TickEverything the collector saw in one poll, as one messageone every ~10 s
Upsert“Insert, or replace if it is already there” in one writereplace_one(…).upsert(true)
IdempotentDoing it twice has the same effect as doing it oncere-publishing a registration

digital-twin is a Rust service (Axum, MongoDB, Kafka, Redis) in backend/digital-twin. It implements the Digital Twin architecture. Route /twin/*, checked at the edge.

It owns the spatial model of a building, and it has five jobs:

JobEntry
Register a buildingPOST /register, the worker, the Kafka answer
Read and change buildingsGET and PATCH /building/{id}, lists, counts
Tell other services who owns a buildingGET /domain/{building}
Store where sensors sitGET and PUT /building/{id}/placements
Estimate where phones areKafka wifi-observations

The idea in one minute

Think of an architect’s office that also keeps a live floor plan.

flowchart LR
    B["browser"] -- "register, read, edit" --> D[("digital-twin<br/>the architect's office")]
    D -- "building-registration-requested" --> T["telemetry"]
    T -- "building-registration-completed" --> D
    D -- "init columns" --> Da["dashboard"]
    D -- "failed: /trigger" --> N["notification"]
    C["collector"] -- "wifi-observations" --> D
    D -- "twin:devices:id (dots)" --> R["Redis, then socket"]
    D -- "estimated-occupancy" --> T
    D --- M[("MongoDB<br/>buildings, uploads, placements")]

The files

Ports and adapters. Arrows point inward: only main.rs knows the store is MongoDB.

PathWhat it holdsMay not import
src/domain/Building, Room, Coordinates, Dimensions, the input checks, Placement, PlacementChanges, AcceptedUpload, UploadStatus, GatewayClaims, DomainErroraxum, mongodb, crate::adapters, crate::service
src/service/The use cases buildings, provisioning, placements, tracking, tracks, estimator. authz (Cedar). ports.rs. fakes.rs (test doubles)axum, mongodb, crate::adapters
src/adapters/driving/http_api/ (handlers, claims extractor, error mapping), worker.rs, kafka_consumer.rs, wifi_consumer.rscrate::adapters::driven
src/adapters/driven/persistence/ (db.rs, jobs.rs, placements.rs), kafka_producer.rs, redis_devices.rs, outbound.rs
src/adapters/metrics.rs, ratelimit.rs, topics.rs: shared by every route
src/lib.rsbuild_router, and /contracts
src/main.rsReads env, builds adapters, starts everything

tests/architecture_fitness.rs reads every file under each folder and fails when one names a forbidden import.

Seven ports sit between the use cases and the outside:

Port (service/ports.rs)What it asks forAdapter
BuildingStorefind_by_id, find_by_domain, find_by_name, upsert, delete, counts_by_domainMongoBuildings
UploadQueueenqueue, claim, mark_ready, mark_failed, statusMongoUploadQueue
PlacementStoreload, applyMongoPlacements
RegistrationEventspublish_building_registration_requestKafkaEventProducer
OccupancyEventspublish_occupancyKafkaEventProducer
DevicesFeedpublishRedisDevicesFeed
DownstreamSyncclone_thresholds, init_preferences, notify_provisioning_failedOutboundConfig

Ports return anyhow::Result. A use case turns that into DomainError::Internal, which the HTTP layer answers as 500.

flowchart TD
    H["driving: http_api, worker,<br/>kafka_consumer, wifi_consumer"] --> S["service: Buildings, Provisioning,<br/>Placements, Tracking"]
    S --> D["domain: Building, Placement,<br/>DomainError"]
    S --> P{{"ports"}}
    P -.-> A["driven: Mongo, Kafka,<br/>Redis, HTTP"]

main.rs: starting up

The order matters, so here it is step by step.

let buildings = db::connect(&mongo_uri, "ambitwin")
    .await
    .expect("Failed to connect to MongoDB");
  1. Connect to MongoDB: database ambitwin, collection buildings. This also creates the indexes (id unique, domains).
let sync_enabled = env::var("NODE_ENV").map(|v| v != "test").unwrap_or(true);
let outbound = OutboundConfig {
    telemetry_url: env::var("TELEMETRY_URL").unwrap_or_else(|_| "http://localhost:3000".to_string()),
    dashboard_url: env::var("DASHBOARD_URL").unwrap_or_else(|_| "http://localhost:3001".to_string()),
    notification_url: env::var("NOTIFICATION_URL").unwrap_or_else(|_| "http://localhost:3004".to_string()),
    sync_enabled,
    client: client(),
};
  1. The addresses of the three services it calls. NODE_ENV=test turns the three HTTP calls off (sync_enabled = false). One HTTP client for all of them, with a 5 s timeout.
let events = Arc::new(
    KafkaEventProducer::new(&kafka_brokers).await.expect("Failed to configure Kafka producer"),
);
  1. The Kafka producer. Creating it first makes sure the topics building-registration-requested, building-registration-completed and estimated-occupancy exist.
let placements = Arc::new(MongoPlacements::new(placements::connect(&mongo_uri, "ambitwin").await …));
let store = Arc::new(MongoBuildings::new(buildings.clone()));
let queue = Arc::new(MongoUploadQueue::from_building_collection(&buildings).await.expect("create upload queue indexes"));
  1. Open the placements and pending_uploads collections and create their indexes.
let provisioning = Arc::new(Provisioning::new(store.clone(), queue, downstream.clone(), events.clone()));
worker::spawn(provisioning.clone());
kafka_consumer::spawn(&kafka_brokers, provisioning.clone());
let feed = Arc::new(RedisDevicesFeed::new(&redis_url).expect("REDIS_URL is a Redis URL"));
wifi_consumer::spawn(&kafka_brokers, Arc::new(Tracking::new(store.clone(), placements.clone(), feed, events)));
  1. Start the worker loop.
  2. Start the registration consumer (building-registration-completed).
  3. Build the Redis feed (it connects only on its first publish) and start the Wi-Fi consumer.
  4. Build the router with the rate limiter on, and serve on PORT until SIGTERM or Ctrl+C.

A failure in steps 1 to 4 stops the process (expect): the service never runs half wired. The three background loops are started with spawn and are never waited on.

VariableDefaultWhat it is
MONGO_URImongodb://localhost:27017All three collections
KAFKA_BROKERSlocalhost:9092Registration, Wi-Fi ticks, room counts
REDIS_URLredis://localhost:6379Dots on twin:devices:{id}
TELEMETRY_URLhttp://localhost:3000Room limits after an edit
DASHBOARD_URLhttp://localhost:3001Starting columns
NOTIFICATION_URLhttp://localhost:3004The “provisioning failed” alert
NODE_ENVtest turns off the three HTTP calls above
PORT3000HTTP

What it stores

MongoDB, database ambitwin:

CollectionOne document perShapeIndexes
buildingsbuilding{id, name, rooms: [{id, name, capacity, position, dimensions, color?}], domains}id unique. domains
pending_uploadsuploadthe building, the caller’s raw claims, status, attempts, leased_until, accepted_at, error, finished_atid unique. status + leased_until. finished_at with expiry 1 h
placementsbuilding{_id: buildingId, sensors: {"<sensorId>": {x, y, z}}}_id

domain/building.rs: the shapes and their checks

pub struct Room {
    pub id: String,
    pub name: String,
    pub capacity: f64,
    pub position: Coordinates,
    pub dimensions: Dimensions,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color: Option<String>,
}

pub struct Building {
    pub id: String,
    pub name: String,
    pub rooms: Vec<Room>,
    pub domains: Vec<String>,
}

impl PartialEq for Building {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

A room is a box: position is its centre, dimensions its width, height and depth. A building is its rooms plus the list of domains that own it. Two buildings are “equal” when their ids match, whatever else differs.

Building and Room are not shared shapes: no other Rust service parses them. The cross-language readers (agent, frontend) are kept in line by schemas/fixtures/building.json, checked in tests/building_conformance.rs.

Checking raw input

pub struct PositionInput {
    pub x: Option<f64>,
    pub y: Option<f64>,
    pub z: Option<f64>,
}

impl PositionInput {
    pub fn to_coordinates(&self) -> Result<Coordinates, DomainError> {
        match (self.x, self.y, self.z) {
            (Some(x), Some(y), Some(z)) if x.is_finite() && y.is_finite() && z.is_finite() => {
                Ok(Coordinates { x, y, z })
            }
            _ => Err(DomainError::Validation(
                "position.x, position.y and position.z must be finite numbers".to_string(),
            )),
        }
    }
}

The wire types have Option everywhere, so a missing field does not crash the JSON reader. to_coordinates is the gate: all three present, and is_finite (not NaN, not infinity). Only then do the strict Coordinates exist. DimensionsInput::to_dimensions does the same with one more rule: each must be above 0. validate_capacity wants a finite number of 0 or more.

Names never fail

pub fn normalize_building_name(name: Option<&str>, id: Option<&str>) -> String {
    let trimmed = name.map(str::trim).filter(|s| !s.is_empty());
    trimmed.or(id).unwrap_or("Building").to_string()
}

pub fn normalize_room_name(name: Option<&str>, id: &str) -> String {
    name.map(str::trim).filter(|s| !s.is_empty()).unwrap_or(id).to_string()
}
CaseResult
Building name " HQ "HQ (trimmed)
Blank building name, at registrationBuilding (there is no id to fall back on yet)
Blank building name, found storedthe building’s id
Blank room namethe room’s id

domain/placement.rs: a sensor’s position

pub const MAX_BATCH_PLACEMENTS: usize = 200;

pub fn checked(upsert: Vec<Placement>, delete: Vec<String>) -> Result<Self, DomainError> {
    if upsert.len() + delete.len() > MAX_BATCH_PLACEMENTS { … }

    let mut seen = HashSet::new();
    let mut once = |sensor_id: &str| -> Result<String, DomainError> {
        let sensor_id = sensor_id.trim();
        if sensor_id.is_empty() { … "sensorId must not be blank" }
        // Stored as a field name keyed by sensor id, and `.`/`$` would be read as a path there.
        if !sensor_id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') { … }
        if !seen.insert(sensor_id.to_owned()) { … "appears more than once in this batch" }
        Ok(sensor_id.to_owned())
    };
    // … each upsert also needs finite x, y, z
}

PlacementChanges::checked is the only way to build a PlacementChanges, so no layer can skip its rules:

RuleWhy
At most 200 items in totalOne request cannot hold the service up
Sensor id trimmed, not blankAn empty key is meaningless
Only letters, digits, -, _The id is a field name in MongoDB. A . or $ would be read as a path or an operator
Each sensor once, across upsert and delete“Move and remove” in one batch has no clear meaning
Coordinates finiteNaN would poison the distance maths later

Registering a building

The HTTP side: add_building

pub async fn add_building(State(state): State<AppState>, claims: GatewayClaims, Json(body): Json<RegisterBuildingRequest>)
    -> Result<(StatusCode, Json<serde_json::Value>), DomainError>
{
    let rooms = body.rooms.into_iter().map(RoomWireInput::into_room).collect::<Result<Vec<_>, _>>()?;

    let building = Building {
        id: Uuid::new_v4().to_string(),
        name: normalize_building_name(body.name.as_deref(), None),
        rooms,
        domains: body.domains,
    };
    let handle = state.provisioning.accept(building, &claims).await?;

    Ok((StatusCode::ACCEPTED, Json(serde_json::json!({ "buildingId": handle }))))
}
  1. Turn every wire room into a checked Room (position, dimensions, capacity, name). The first bad room fails the whole request with 400.
  2. Make the building. The client chooses the room ids, the service chooses the building id (a fresh UUID).
  3. Hand it to accept. Answer 202 Accepted with the handle: “received, not done yet”.

accept: the checks before 202

pub async fn accept(&self, building: Building, claims: &GatewayClaims) -> Result<String, DomainError> {
    if building.domains.is_empty() {
        return Err(DomainError::Validation("A building needs at least one domain".to_string()));
    }
    if !authz::can_edit_every(claims, &building.domains) {
        return Err(DomainError::Forbidden(
            "Requires an editing role in every domain the building is registered to".to_string(),
        ));
    }
    let upload = AcceptedUpload { id: building.id.clone(), building, claims: claims.raw.clone() };
    self.queue.enqueue(&upload).await?;
    Ok(upload.id)
}

Every check runs before the answer, so the service never accepts a building it would refuse later.

RuleError
position has finite x, y, z400
dimensions has finite width, height, depth, all above 0400
capacity present, finite, 0 or more400 capacity must be a non-negative number
domains not empty400 A building needs at least one domain
Editor in every domain403. One foreign domain refuses the whole upload

Then the upload is saved together with the caller’s raw claims header (claims.raw). The worker has no HTTP request to take claims from, and later calls to dashboard need them.

The queue: UploadQueue on MongoDB

A job is a document in pending_uploads. Three queries do all the work:

fn claimable_filter(now: DateTime) -> Document {
    doc! {
        "status": "pending",
        "$or": [
            { "leased_until": null },
            { "leased_until": { "$lte": now } },
        ],
    }
}

fn claim_update(expires_at: DateTime) -> Document {
    doc! {
        "$set": { "leased_until": expires_at },
        "$inc": { "attempts": 1 },
    }
}

fn resolve_filter(id: &str) -> Document {
    doc! { "id": { "$eq": id }, "status": "pending" }
}

A lease is like borrowing a library book for 30 seconds. If the borrower (the worker) crashes, the book comes back by itself when the time runs out and someone else can take it. That is why a crash in the middle of provisioning loses nothing.

Indexes on pending_uploads:

// unique id; (status, leased_until) for claims; finished_at with expire_after(1 h)
col.update_many(
    doc! { "status": { "$in": ["ready", "failed"] }, "finished_at": null },
    doc! { "$set": { "finished_at": DateTime::now() } },
).await?;

The last statement runs at every start. A TTL index never removes a record that lacks its field, so uploads finished before expiry existed would otherwise stay forever. This gives them a finished_at now.

The worker

const LEASE: Duration = Duration::from_secs(30);
const IDLE_POLL: Duration = Duration::from_millis(50);

pub fn spawn(provisioning: Arc<Provisioning>) -> tokio::task::JoinHandle<()> {
    tokio::spawn(async move {
        loop {
            match provisioning.provision_next(LEASE).await {
                Ok(true) => {}
                Ok(false) => tokio::time::sleep(IDLE_POLL).await,
                Err(e) => {
                    log::error!("provisioning loop stalled: {e:?}");
                    tokio::time::sleep(IDLE_POLL).await;
                }
            }
        }
    })
}

A loop that never ends:

provision_next and provision

pub async fn provision_next(&self, lease: Duration) -> Result<bool, DomainError> {
    let Some(upload) = self.queue.claim(lease).await? else {
        return Ok(false);
    };

    if let Err(e) = self.provision(&upload).await {
        log::error!("provisioning {} failed: {e:?}", upload.id);
        self.fail(&upload.id, &format!("{e:?}")).await?;
    }
    Ok(true)
}

async fn provision(&self, upload: &AcceptedUpload) -> Result<(), DomainError> {
    self.buildings.upsert(&upload.building).await?;
    self.events.publish_building_registration_request(&upload.building).await?;
    self.downstream.init_preferences(&upload.building.id, &upload.claims).await;
    Ok(())
}
  1. Claim a job. None: return false.
  2. Store the twin (an upsert, so doing it twice converges on one twin).
  3. Publish building-registration-requested to Kafka. The key is the building id.
  4. Ask dashboard for starting columns. This one returns nothing: a failure is only logged.
  5. If step 2 or 3 failed, the upload is failed on the spot (see fail below).

The worker does not mark the job done. The job stays pending with its lease running until telemetry answers.

flowchart TD
    A["provision_next: claim a job"] --> N{"job found?"}
    N -- no --> Sleep["sleep 50 ms"]
    N -- yes --> Pr["provision: upsert, publish, init columns"]
    Pr --> Ok{"upsert and publish worked?"}
    Ok -- no --> Fail["fail(id, error)"]
    Ok -- yes --> Wait["job stays pending, lease 30 s"]
    Wait --> Ans{"telemetry answers within 30 s?"}
    Ans -- yes --> Res["resolve"]
    Ans -- no --> Again["lease runs out, the worker claims it again and republishes"]
    Again --> Pr

Example. telemetry is down for 2 minutes. The job is claimed at 0 s, 30 s, 60 s, 90 s, 120 s: 5 attempts, 5 identical requests on Kafka. When telemetry comes back it registers the building 5 times (each an upsert) and answers 5 times. The first ready resolves the job. The other 4 match nothing.

sequenceDiagram
    autonumber
    participant B as Browser
    participant H as add_building
    participant P as Provisioning
    participant Q as MongoUploadQueue
    participant W as worker.rs
    participant S as MongoBuildings
    participant K as KafkaEventProducer
    participant D as OutboundConfig
    B->>H: POST /register {name, rooms, domains}
    H->>H: RoomWireInput::into_room for each room
    H->>H: new UUID, normalize_building_name
    H->>P: accept(building, claims)
    P->>P: domains not empty, can_edit_every(claims, domains)
    P->>Q: enqueue: status pending, attempts 0
    H-->>B: 202 {buildingId}
    W->>P: provision_next(30 s)
    P->>Q: claim: a pending job, lease free or run out
    Q-->>P: AcceptedUpload, lease until now + 30 s, attempts + 1
    P->>S: upsert(building)
    P->>K: publish_building_registration_request
    P->>D: init_preferences: POST dashboard /preferences/init/{id}
    B->>H: GET /building/{id}/status, every few seconds
    H-->>B: pending

Example data: one room, editor in eng.

StepData
1{"name":" HQ ","domains":["eng"],"rooms":[{"id":"r1","name":"","capacity":20,"position":{"x":5,"y":1.5,"z":4},"dimensions":{"width":10,"height":3,"depth":8}}]}
2Room {id: r1, name: r1, capacity: 20.0, …}. The blank name becomes the id
3Building {id: 5c1e…, name: HQ, domains: [eng]}. The name is trimmed
6pending_uploads: {id: 5c1e…, building, claims: "<raw header>", status: pending, attempts: 0}
7202 {"buildingId":"5c1e…"}
10The same job, leased_until now + 30 s, attempts 1
12{"buildingId":"5c1e…","name":"HQ","rooms":[{"id":"r1","name":"r1"}]} on Kafka, key 5c1e…
13POST …/preferences/init/5c1e… with the uploader’s claims

(The building id is made up for the example.)

What goes to the other services

pub async fn sync_building_clone(config: &OutboundConfig, building: &Building, max_temperature: Option<f64>, claims_header: Option<&str>) -> anyhow::Result<()> {
    if !config.sync_enabled { return Ok(()); }
    // PUT {telemetry}/thresholds/buildings/{id}  { name, rooms: [{id, name}], maxTemperature? }
    // non-2xx: bail! "Failed to sync sensor threshold clone: {status} {details}"
}

pub async fn init_building_preferences(config: &OutboundConfig, building_id: &str, claims_header: Option<&str>) {
    // POST {dashboard}/preferences/init/{id}; an Err is only logged
}

pub async fn notify_provisioning_failed(config: &OutboundConfig, building_id: &str, error: &str) {
    // POST {notification}/trigger with the system identity; an Err is only logged
}

Telemetry’s answer

adapters/driving/kafka_consumer.rs, group building-registrations-completed.

let consumer: StreamConsumer = ClientConfig::new()
    .set("bootstrap.servers", brokers)
    .set("group.id", GROUP_ID)
    .set("auto.offset.reset", "earliest")
    // A crash between reading an answer and resolving it must leave the answer unread.
    .set("enable.auto.commit", "false")
    .create()?;

Read from the oldest unread message, and commit by hand, only after the answer is resolved.

async fn handle_payload(payload: &[u8], provisioning: &Provisioning) -> Disposition {
    let event: RegistrationCompleted = match serde_json::from_slice(payload) {
        Ok(e) => e,
        Err(e) => { log::error!("malformed registration-completed event: {e:?}"); return Disposition::Settled; }
    };

    let error = event.failure();
    match provisioning.resolve(&event.building_id, error.as_deref()).await {
        Ok(Some(elapsed)) => { /* record the duration, ready or failed */ Disposition::Settled }
        Ok(None) => Disposition::Settled,
        Err(e @ DomainError::Internal(_)) => { /* log */ Disposition::Retry }
        Err(e) => { /* log */ Disposition::Settled }
    }
}
Result of resolveWhat the consumer does
Malformed messageLog it and commit. Reading it again gives the same answer
Ok(Some(time))The job changed. Record the time since accept, commit
Ok(None)Nothing changed: a repeat. Commit
Internal error (the store failed)Retry: stop, reconnect after 5 s, read the same message again
Any other errorLog it and commit
if handle_payload(payload, provisioning).await == Disposition::Retry {
    anyhow::bail!("answer at offset {} unresolved, rereading", message.offset());
}

Retry ends the loop with an error without committing. The outer spawn loop waits 5 s and starts again. Reconnecting rewinds to the last commit, so the unresolved answer is read again.

resolve and fail

pub async fn resolve(&self, id: &str, error: Option<&str>) -> Result<Option<Duration>, DomainError> {
    match error {
        None => Ok(self.queue.mark_ready(id).await?),
        Some(e) => self.fail(id, e).await,
    }
}

// A failed upload must not leave an orphaned twin behind -- whether provisioning
// failed before telemetry ever heard about it, or telemetry rejected it later.
// Notify before deleting: notification resolves the building's domains by
// calling back into digital-twin, so the twin must still exist when it does.
async fn fail(&self, id: &str, error: &str) -> Result<Option<Duration>, DomainError> {
    let Some(elapsed) = self.queue.mark_failed(id, error).await? else {
        // Already failed means a retry: notify went out, only the delete may be outstanding.
        if self.queue.status(id).await? == Some(UploadStatus::Failed) {
            self.buildings.delete(id).await?;
        } else {
            log::warn!("registration of {id} failed after the upload was resolved: {error}");
        }
        return Ok(None);
    };
    self.downstream.notify_provisioning_failed(id, error).await;
    self.buildings.delete(id).await?;
    Ok(Some(elapsed))
}
sequenceDiagram
    autonumber
    participant K as Kafka
    participant C as handle_payload
    participant P as Provisioning
    participant Q as MongoUploadQueue
    participant N as notification
    participant S as MongoBuildings
    K->>C: RegistrationCompleted
    C->>C: failure()
    C->>P: resolve(id, error)
    alt ready
        P->>Q: mark_ready: only if still pending
    else failed
        P->>Q: mark_failed: only if still pending
        P->>N: POST /trigger, as system:digital-twin
        P->>S: delete(id)
    end
    C->>K: commit offset

Polling the status and resync

GET /building/{id}/status reads pending_uploads and answers {"status": "pending" | "ready" | "failed"}. 404 No upload with handle when the job is unknown, or more than 1 hour after it finished. It needs only a signed-in caller.

POST /building/{id}/sync is resync: read the stored building, check the caller edits one of its domains, and publish the same registration request the worker would. Use it when telemetry lost a registration. It writes nothing here.


Reading buildings: Buildings

pub async fn get(&self, id: &str, claims: &GatewayClaims) -> Result<Building, DomainError> {
    let building = self.find(id).await?;
    if !authz::can_read_building(claims, &building.domains) {
        return Err(DomainError::Forbidden("Requires membership in one of this building's domains".to_string()));
    }
    self.normalize(building).await
}

Find (or 404), check the caller may read it (or 403), then repair names. list_for_domain checks the caller is a member of the asked domain, then lists and repairs each building.

pub async fn counts_per_domain(&self, requested: &[String], claims: &GatewayClaims) -> Result<HashMap<String, i64>, DomainError> {
    if requested.len() > MAX_DOMAIN_NAMES {      // 500
        return Err(DomainError::Validation(format!("Too many domains requested (max {MAX_DOMAIN_NAMES})")));
    }
    let scoped = authz::filter_readable_domains_from_memberships(requested, claims);
    Ok(self.store.counts_by_domain(&scoped).await?)
}

Counts are scoped by silently dropping the domains the caller cannot read, not by an error. A caller cannot learn which domains exist by trying names.

Example. POST /buildings/counts {"domains":["eng","ops"]} from a member of eng only. ops is dropped. Answer: {"counts":{"eng":3}}.

The count itself is one MongoDB aggregation:

vec![
    doc! { "$unwind": "$domains" },
    doc! { "$match": { "domains": { "$in": domains } } },
    doc! { "$group": { "_id": "$domains", "count": { "$sum": 1 } } },
]

$unwind turns a building with two domains into two rows, $match keeps the wanted domains, $group counts per domain.

normalize: repairing on read

async fn normalize(&self, mut building: Building) -> Result<Building, DomainError> {
    let mut changed = false;
    let normalized = normalize_building_name(Some(&building.name), Some(&building.id));
    if normalized != building.name { building.name = normalized; changed = true; }
    for room in &mut building.rooms {
        let normalized = normalize_room_name(Some(&room.name), &room.id);
        if normalized != room.name { room.name = normalized; changed = true; }
    }
    if changed { self.store.upsert(&building).await?; }
    Ok(building)
}

A stored blank name is replaced by the id. If anything changed, the building is written back, so the repair happens once. Reading can write.

Changing a building

pub async fn update(&self, id: &str, patch: BuildingPatch, claims: &GatewayClaims) -> Result<Building, DomainError> {
    let mut building = self.get_building_by_id_for_edit(id, claims).await?;

    if let Some(name) = patch.name { building.name = name; }
    if let Some(domains) = patch.domains { building.domains = domains; }
    self.store.upsert(&building).await?;
    self.downstream.clone_thresholds(&building, patch.max_temperature, &claims.raw).await?;

    Ok(building)
}
  1. Load the building and check the caller edits one of its current domains.
  2. Apply name and domains if present.
  3. Save.
  4. Tell telemetry (PUT /thresholds/buildings/{id}) with the caller’s own claims.
sequenceDiagram
    autonumber
    participant B as Browser
    participant U as Buildings::update
    participant S as MongoBuildings
    participant T as telemetry
    B->>U: PATCH /building/b1 {name, domains, maxTemperature}
    U->>S: find, normalize
    U->>U: can_edit_domains(claims, current domains)
    U->>U: apply name and domains when present
    U->>S: upsert(building)
    U->>T: clone_thresholds: PUT /thresholds/buildings/b1 {name, rooms, maxTemperature?}
    T-->>U: 2xx
    U-->>B: Building

Ownership lookup

pub async fn domains_of(&self, building: &str) -> Result<Vec<String>, DomainError> {
    Ok(match self.store.find_by_id(building).await? {
        Some(found) => found.domains,
        None => self.store.find_by_name(building).await?.into_iter().flat_map(|b| b.domains).collect(),
    })
}

GET /domain/{building} answers which domains own a building: first by id, then, if there is none, by exact name (all buildings with that name, domains joined). Three services ask it:

CallerWhyIdentity sentIts own limits
notificationWho receives a breach alertsystem:notification-service, no memberships2 s timeout, 15 min cache
socketMay this browser subscribe to this building?The browser’s claims2 s timeout, 60 s cache
telemetryMay this caller read this building’s data?The caller’s claims5 s timeout, 60 s cache

The route checks only that a caller is signed in. It does not scope the answer to the caller: system: callers have no memberships and would always get [].

Example. Two buildings named Main Hall, one in eng, one in ops. GET /domain/Main%20Hall answers ["eng","ops"]. Asking by id answers only that building’s domain.


Placements

pub async fn apply(&self, building_id: &str, changes: PlacementChanges, claims: &GatewayClaims) -> Result<(), DomainError> {
    let building = self.building(building_id).await?;
    if !authz::can_edit_domains(claims, &building.domains) {
        return Err(DomainError::Forbidden("Requires an editing role in one of this building's domains".to_string()));
    }
    Ok(self.store.apply(building_id, &changes).await?)
}

list checks can_read_building and sorts by sensor id. apply checks can_edit_domains. The store does the write:

let mut update = Document::new();
if !changes.upsert.is_empty() {
    let mut set = Document::new();
    for placement in &changes.upsert {
        set.insert(format!("{SENSORS}.{}", placement.sensor_id), mongodb::bson::to_bson(&placement.position)?);
    }
    update.insert("$set", set);
}
if !changes.delete.is_empty() {
    let unset: Document = changes.delete.iter().map(|sensor_id| (format!("{SENSORS}.{sensor_id}"), "".into())).collect();
    update.insert("$unset", unset);
}
self.col.update_one(doc! { "_id": building_id }, update).upsert(true).await?;

Example. {"upsert":[{"sensorId":"s1","position":{"x":4,"y":0.5,"z":6}}],"delete":["s2"]} becomes one MongoDB update:

{"$set": {"sensors.s1": {"x":4,"y":0.5,"z":6}}, "$unset": {"sensors.s2": ""}}

One document per building makes the batch all or nothing. This MongoDB runs alone, with no transactions, and one update_one on one document is atomic. An empty batch writes nothing. This is also why sensor ids can only be letters, digits, - and _.

Only the position lives here. The sensor itself (type, room, name) belongs to telemetry, and the sensor id is never checked against it.

sequenceDiagram
    autonumber
    participant B as Browser
    participant H as save_placements
    participant C as PlacementChanges::checked
    participant P as Placements::apply
    participant S as MongoPlacements
    B->>H: {upsert: [{sensorId, position}], delete: [sensorId]}
    H->>H: position.to_coordinates for each upsert
    H->>C: checked(upsert, delete)
    C-->>H: PlacementChanges or 400
    H->>P: apply(b1, changes, claims)
    P->>P: building exists, can_edit_domains
    P->>S: one update_one with $set and $unset, upsert
    H-->>B: 204

Access rules: authz.rs

Cedar runs in process, with the shared bundle from backend/libs/auth-policy embedded at build time (include_str!).

static ROLE_WEIGHTS: LazyLock<HashMap<String, i64>> = LazyLock::new(|| {
    serde_json::from_str(include_str!("../../../libs/auth-contracts/roles.json")).expect("roles.json parses")
});

LazyLock builds the value the first time it is used. The role ladder comes from the same roles.json as every other service.

From claims to a Cedar caller

for m in &claims.payload.memberships {
    let Some(role) = &m.role else { continue };
    let Some(&weight) = ROLE_WEIGHTS.get(role) else { continue };   // unknown role: skipped
    if weight > max_weight { max_weight = weight; }
    standard_customer.insert(m.domain.clone());
    if weight >= staff_weight { business_staff.insert(m.domain.clone()); }
    if weight >= business_admin_weight { business_admin.insert(m.domain.clone()); }
    if weight >= admin_weight { admin.insert(m.domain.clone()); }
}

For each membership, the domain is put in every set the role’s weight reaches: domainsAsStandardCustomer, domainsAsBusinessStaff, domainsAsBusinessAdmin, domainsAsAdmin. A business_admin in eng is in the first three sets. A role the ladder does not know is skipped, never treated as a wildcard.

The resource is the domain being asked about. Cedar then decides Allow or not:

HelperCedar actionTrue when
is_member_of(d)ReadAny known role in d
can_read_building(ds)ReadMember of one of ds, or the caller is system:ap-simulator
can_edit_domains(ds)Editbusiness_staff or higher in one of ds
can_edit_every(ds)Editds not empty and editor in all
filter_readable_domains_from_membershipsReadKeeps the readable ones

Any failure while building the Cedar request is logged and answers false: deny by default.

Example. Ada is admin in other. A building in eng and ops. can_read_building is false: a platform admin is not a member of every domain.


One request, layer by layer

sequenceDiagram
    autonumber
    participant E as Edge
    participant RL as ratelimit::rate_limit
    participant M as metrics::track_metrics
    participant X as GatewayClaims extractor
    participant H as handler
    participant U as use case
    participant Er as DomainError into_response
    E->>M: request
    M->>RL: request
    RL->>RL: client_ip: last x-forwarded-for entry, else peer
    RL->>RL: count for this IP in a 60 s window
    alt over 300
        RL-->>E: 429
    else
        RL->>X: request
        X->>X: header present, base64, JSON, has a user id?
        X->>H: GatewayClaims
        H->>H: wire input into domain types
        H->>U: call
        U-->>H: Ok or DomainError
        H-->>Er: on error
        Er-->>E: status and {type, message}
    end

Every field and status code: Twin API (OpenAPI).

The edge strips /twin. Every route except the public ones needs x-gateway-claims.

MethodPathWho may callWhat it doesAnswers
POST/registerEditor (business_staff or higher) in every listed domainCheck the building, queue it202 {buildingId}
GET/building/{id}/statusAny signed-in callerUpload status{status}
GET/building/{id}Member of one of its domains, or system:ap-simulatorOne buildingBuilding
PATCH/building/{id}Editor in one of its current domainsChange name, domains. Send room limits to telemetryBuilding
POST/building/{id}/syncEditor in one of its domainsAnnounce it to telemetry again202
GET/buildings/{domain}Member of that domainAll its buildings[Building]
POST/buildings/countsAny signed-in callerCount per domain, unreadable domains dropped{counts: {domain: n}}
GET/domain/{building}Any signed-in callerDomains owning the building, by id then by name["eng"]
GET/building/{id}/placementsMember, or system:ap-simulatorSensor positions, sorted by id[{sensorId, position}]
PUT/building/{id}/placementsEditor in one of its domainsAdd, move, remove positions in one batch204
GET/contractsPublicThe metric catalog: roomName, roomMaxOccupancyServiceMetricsContract
GET/health, /metricsPublicLiveness, Prometheus

/contracts is how dashboard learns which columns this service can fill. It is built from the shared telemetry_schema types, because a hand-written json! once drifted from the reader and emptied the catalog at runtime.

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
    let invalid = || DomainError::Unauthorized("Invalid authentication token".to_string());
    let raw = parts.headers.get(CLAIMS_HEADER)
        .ok_or_else(|| DomainError::Unauthorized("Missing authentication token".to_string()))?
        .to_str().map_err(|_| invalid())?.to_string();
    let decoded = decode_claims_header(&raw).ok_or_else(invalid)?;
    let payload: ClaimsPayload = serde_json::from_slice(&decoded).map_err(|_| invalid())?;
    if payload.user_id().is_none() {
        return Err(invalid());
    }
    Ok(GatewayClaims { payload, raw })
}

The claims extractor, same as in the other Rust services. The raw header is kept because the use cases forward it.

DomainErrorStatustype
Validation400Validation Error
Unauthorized401Unauthorized Error. A missing or unreadable header, or no user id
Forbidden403Forbidden Error
NotFound404Not Found Error
Internal500Internal Server Error. A generic text, the cause only logged

Rate limit. 300 requests per IP per 60 s, counted in memory. The IP is the last x-forwarded-for entry, the one the edge added, so a client writing its own value does not get a fresh counter. Idle IPs are dropped once per window.


Estimating phones from Wi-Fi

This is the most original part of the service. In plain words: the louder a router hears a phone, the closer the phone is. With three or more routers, there is usually one spot on the floor that explains all the loudness numbers at once.

A tick enters

wifi_consumer.rs:

// A missed tick is replaced ten seconds later, so start at the newest and never reread.
let consumer: StreamConsumer = ClientConfig::new()
    .set("bootstrap.servers", brokers)
    .set("group.id", GROUP_ID)                       // "wifi-observations"
    .set("auto.offset.reset", "latest")
    .set("enable.auto.commit", "true")
    .set("topic.metadata.refresh.interval.ms", "10000")
    .create()?;

The opposite of the registration consumer: start at the newest message, commit automatically, never reread. The collector polls about every 10 seconds and Kafka keeps 10 minutes. Replayed after an outage, old ticks would move the dots backwards. The topic belongs to telemetry: twin never creates it, so its 10-minute retention holds. The metadata refresh every 10 s lets the consumer find the topic once telemetry creates it.

async fn handle_payload(payload: &[u8], tracking: &Tracking, now_ms: u64) -> Observed {
    let tick: WifiObservations = match serde_json::from_slice(payload) { … Invalid };
    let lag = Duration::from_millis(now_ms.saturating_sub(tick.tick));
    let observed = tracking.observe(tick, now_ms).await;
    // log if not Accepted, record the metric with the lag
    observed
}

Parse the tick. A tick that cannot be read is dropped, and counted invalid. Otherwise hand it to Tracking::observe.

observe: accept or drop

pub const MAX_TICK_AGE_MS: u64 = 30_000;

pub async fn observe(&self, tick: WifiObservations, now_ms: u64) -> Observed {
    if !tick.problems().is_empty() {
        return Observed::Invalid;
    }
    if now_ms.saturating_sub(tick.tick) > MAX_TICK_AGE_MS {
        return Observed::Stale;
    }
    if self.held_tick(&tick.building_id) > Some(tick.tick) {
        return Observed::Stale;
    }
    // …
}
flowchart TD
    In["tick"] --> P{"problems() empty?"}
    P -- no --> Inv["invalid, nothing published"]
    P -- yes --> A{"now minus tick over 30 s?"}
    A -- yes --> St["stale, nothing published"]
    A -- no --> H{"older than the building's held tick?"}
    H -- yes --> St
    H -- no --> Acc["accepted"]

Three gates: the tick must follow the shared contract (problems()), it must be under 30 s old (three collector polls), and it must not be older than the newest one already held for that building.

let (geometry, moved) = self.geometry(&tick.building_id, now_ms).await;
let devices = {
    let mut sites = self.sites.lock().expect("tracking lock");
    let site = sites.entry(tick.building_id.clone()).or_default();
    if moved {
        site.tracks = Tracks::default();
    }
    site.tick = tick.tick;
    site.dots = geometry.as_ref().map_or_else(Vec::new, |geometry| site.tracks.step(&tick, geometry));
    site.dots.clone()
};
if let Some(geometry) = geometry.filter(|_| !tick.answered.is_empty()) {
    self.occupancy.publish_occupancy(&EstimatedOccupancy {
        building_id: tick.building_id.clone(), tick: tick.tick, rooms: room_counts(&geometry, &devices),
    }).await;
}
self.feed.publish(&Devices { building_id: tick.building_id, tick: tick.tick, devices }).await;
Observed::Accepted
  1. Get the building’s floor plan (geometry), and whether a router moved since last time.
  2. Step the tracks. If a router moved, all tracks of the building start again.
  3. If at least one router answered (tick.answered not empty), publish the room counts to Kafka.
  4. Always publish the dots to Redis, even an empty list, so the browser clears the dots of people who left.
  5. A building twin does not know gets no dots (geometry is None).

The floor plan is cached

pub const GEOMETRY_TTL_MS: u64 = 30_000;

Tracking::geometry keeps one Geometry per building and reloads it when older than 30 s. The reload reads the building and its placements, and compares the new placements with the old ones. If they differ, a router moved: moved = true.

A failed reload keeps the last good plan. A database blip must not wipe every dot.

Geometry::of: cutting the building into cells

  1. Floors. Take each room’s bottom, y − height / 2. Sort the bottoms. Bottoms within 0.5 m of each other are one floor.
  2. Cells. On each floor, cover the rooms’ outline with 0.5 m squares, and keep only the cells that fall inside a room.
  3. Routers. Each placement goes on the floor its y falls on. Every placed sensor is used; only routers ever appear in a tick.
  4. Expected signal. For every cell and every router on its floor, work out how loud that router should hear a phone standing there.
pub fn expected_signal(metres: f64, walls: usize) -> f64 {
    SIGNAL_AT_1M_DBM                                    // -40
        - 10.0 * PATH_LOSS_EXPONENT * metres.max(1.0).log10()   // 22 * log10(distance)
        - WALL_LOSS_DB * walls as f64                   // 5 per wall
}

The rule: −40 dBm at 1 m, 22 × log10(distance) lost with distance, and 5 dB lost per wall. The distance is never taken below 1 m. This is the same radio rule ap-simulator uses.

Example.

RouterDistanceWallsExpected
A3 m0−40 − 22 × 0.477 = −50.5 dBm
B8 m1−40 − 22 × 0.903 − 5 = −64.9 dBm
C12 m2−40 − 22 × 1.079 − 10 = −73.7 dBm

Counting walls

fn walls_between(a: (f64, f64), b: (f64, f64), outlines: &[Bounds]) -> usize { … }
fn crossings((min_x, min_z, max_x, max_z): Bounds, a: (f64, f64), b: (f64, f64)) -> Vec<f64> { … }   // Liang-Barsky clipping

For each room outline, find where the straight line from the cell to the router crosses its edges (an old line-clipping method called Liang-Barsky). Sort all crossings along the line. Crossings closer than 0.3 m to each other count as one wall: rooms drawn edge to edge rarely share a coordinate exactly, so two outlines meeting at a wall would otherwise count twice.

Scoring the cells: posterior

let misfit = heard.iter().map(|&(c, s)| (s - row[c]).powi(2)).sum::<f64>();
let mut cost = misfit / (2.0 * SIGNAL_SD_DB.powi(2));        // SIGNAL_SD_DB = 4.5
if let Some(prior) = prior {
    let walked = (cell.x - prior.x).powi(2) + (cell.z - prior.z).powi(2);
    if walked > prior.reach.powi(2) { return None; }
    cost += walked / (2.0 * prior.variance);
}
// …
let weighed = costs.iter().map(|&(index, cost)| (&plan.cells[index], (low - cost).exp()));

For every cell on the floor:

  1. Misfit = the sum of (heard − expected)², divided by 2 × 4.5². The 4.5 is the noise of the signal (the simulator’s 4 dB shadowing and 2 dB fading, combined).
  2. With a prior (where the phone was last time): drop cells farther than its reach, and add distance² / (2 × prior variance), a pull toward the old place.
  3. The best (lowest) cost is low. Each cell’s weight is e^(low − cost). The best cell weighs 1.
  4. The position is the weighted mean of the cell centres. The spread comes from the weighted distances, at least 0.5 m.

Example. A phone is heard at −51, −66, −75 dBm.

CellExpected A, B, CMisfitCostWeight
X−50.5, −64.9, −73.70.25 + 1.21 + 1.69 = 3.150.081
Y−60, −55, −7081 + 121 + 25 = 2275.600.004

Cell X wins by far.

A lone fix: fix

const MIN_ROUTERS: usize = 3;

pub fn fix(&self, heard: &BTreeMap<String, i32>) -> Option<Fix> {
    let floor = self.floor_of_loudest(&heard)?;
    let placed = heard.iter().filter(|(router, _)| plan.columns.contains_key(*router)).count();
    if placed < MIN_ROUTERS { return None; }
    let posterior = self.posterior(floor, &heard, None)?;
    // … the best cell becomes the Fix
}

For a burst with no prior, pick the floor of the loudest router (a floor slab costs about 18 dB, so the loudest one is on the right floor). Then at least 3 routers placed on that floor must have heard it: two circles cross at two points, and the third router picks one. Fewer than 3: no fix.

Following phones: Tracks::step

pub const MAX_SPEED_M_S: f64 = 2.0;
const DRIFT_M_S: f64 = 0.5;
pub const FORGET_AFTER_TICKS: u64 = 3;
const RING_TOLERANCE_DB: f64 = 9.0;

A tick has two kinds of rows: connected (phones joined to a router, each with a stable token) and bursts (phones only passing, heard by several routers). step does this:

  1. Group the connected rows by token: each phone’s own signals.
  2. For each burst:
    • its token is a connected phone: add its signals to that phone;
    • otherwise compute a fix, then find the phones that fit: one of the phone’s own signals is within 9 dB of what that router should hear at the fix, and the phone could have walked there;
    • exactly one fits: add the burst to that phone;
    • none fits: someone passing, a dot with connected: false;
    • two or more fit: drop it. Guessing would give one phone the other’s path.
  3. Update each phone’s track with posterior, using its last place as the prior. If nothing is within reach, start again from the signals alone.
  4. Forget a token unseen for 3 ticks. One failed poll must not end a track.
  5. Each track becomes a dot: floor, nearest room, x, y = floor height, z, radius = the spread, connected: true.
fn prior(&self, now_ms: u64) -> Prior {
    let seconds = now_ms.saturating_sub(self.at_ms) as f64 / 1000.0;
    Prior {
        x: self.x,
        z: self.z,
        variance: self.variance + (DRIFT_M_S * seconds).powi(2),
        reach: MAX_SPEED_M_S * seconds + 2.0 * self.variance.sqrt(),
    }
}

The older a track, the less it is trusted: its variance grows with time, and its reach (how far the phone may have walked) grows at 2 m/s, which is faster than anybody walks.

Prior example. A track was last updated 10 s ago with variance 1 m².

ValueFormulaResult
Variance now1 + (0.5 m/s × 10 s)²26 m²
Reach2 m/s × 10 s + 2 × √122 m

Cells more than 22 m away are not even scored.

Room counts

fn room_counts(geometry: &Geometry, devices: &[Device]) -> BTreeMap<String, u64> {
    let mut counts: BTreeMap<String, u64> = geometry.covered_rooms().into_iter().map(|room| (room.to_owned(), 0)).collect();
    for device in devices.iter().filter(|device| device.connected) {
        if let Some(count) = counts.get_mut(&device.room) { *count += 1; }
    }
    counts
}

When at least one router answered: every room on a floor with a placed router starts at 0, then each connected dot adds 1 to its room. Passing dots do not count. A room with a router and nobody in it reports an honest 0.

OutputWhereLimit
DotsRedis twin:devices:{id}Gives up after 2 s. Counted as timeout
Room countsKafka estimated-occupancy, key = building, kept 1 hGives up after 2 s
// RedisDevicesFeed
let outcome = match tokio::time::timeout(PUBLISH_TIMEOUT, self.send(&channel, payload)).await {
    Ok(Ok(())) => "ok",
    Ok(Err(e)) => { log::warn!("devices of {} not published: {e:?}", devices.building_id); "error" }
    Err(_) => { log::warn!("devices of {} not published: timed out", devices.building_id); *self.connection.lock().await = None; "timeout" }
};

Both limits keep the Wi-Fi consumer from stalling: the next tick comes 10 s later anyway. The Redis connection is made lazily on the first publish and made again after any failure, so the service starts without Redis, and a hung Redis never blocks anything for long.

sequenceDiagram
    autonumber
    participant K as Kafka wifi-observations
    participant C as wifi_consumer.rs
    participant T as Tracking::observe
    participant G as Tracking::geometry
    participant Tk as Tracks::step
    participant O as KafkaEventProducer
    participant F as RedisDevicesFeed
    K->>C: tick, newest offset, auto-commit
    C->>C: handle_payload: parse WifiObservations
    C->>T: observe(tick, now)
    T->>T: problems() empty? tick under 30 s old? not older than the held one?
    T->>G: floor plan for this building
    G-->>T: Geometry, and whether a router moved
    T->>Tk: step(tick, geometry), tracks reset if a router moved
    Tk-->>T: dots
    T->>O: publish_occupancy, only if a router answered
    T->>F: publish(Devices), always, empty list included

Failure handling

What failsEffect
MongoDB on a request500, cause logged
MongoDB in the workerThe loop logs, sleeps 50 ms, tries again
Kafka publish in provisionfail: job failed, alert, twin deleted
telemetry silentThe job is republished every 30 s
Store error while resolvingThe answer is read again after 5 s
dashboard initLogged only
notification alertLogged only
telemetry on PATCH500, building already saved
Redis or Kafka on a Wi-Fi tickDropped after 2 s, counted
A consumer’s stream breaksReconnect after 5 s

Metrics

NameLabelsWhat it means
http_requests_total, http_error_requests_total, http_request_duration_secondsmethod, route, statusEvery route, by route template
twin_provisioning_duration_secondsready, failedFrom accept to telemetry’s answer
twin_wifi_ticks_totalaccepted, invalid, staleTicks read
twin_wifi_tick_lag_secondsFrom the collector’s snapshot to reading it
twin_devices_published_totalok, error, timeoutDot lists to Redis
twin_occupancy_published_totalok, error, timeoutRoom counts to Kafka

Tests

just test twin               # unit tests in src/, no infrastructure
just test twin-integration   # tests/*.rs against a throwaway MongoDB
WhereWhat it covers
src/service/*.rsEvery use case against the fakes in fakes.rs: no database, no network
src/adapters/driving/kafka_consumer.rsready, failed with and without error, malformed, repeat, retry on store error, unknown building
src/adapters/driving/wifi_consumer.rsEvery fixture tick accepted, every rejected one dropped, a stale tick dropped
src/adapters/ratelimit.rsThe limit, per-IP counts, forged addresses, window reset
src/service/tracks.rsWalking speed, the 3-tick forget, shared bursts, passers. A replay of tests/data/wifi-recording.jsonl
tests/api.rs, tests/api/*.rsRoutes through the real router: auth, buildings, update, placements, registration, rooms, tenant scoping
tests/persistence_*.rsThe MongoDB adapters
tests/architecture_fitness.rsLayer imports
tests/building_conformance.rsschemas/fixtures/building.json round trip
tests/cedar_conformance.rsThe 24 golden cases of auth-policy/fixtures/conformance.json
tests/cucumber.rs, tests/features/A malformed upload refused. Prompt completion. A repeated ready. Scoping to one organization

tests/data/wifi-recording.jsonl is a recorded run with where every phone really stood. The test passes only if the dots place people better than counting each phone in its router’s room. Regenerate it with tests/data/record_wifi.py.


Gaps