Metadata-Version: 2.2
Name: telematics-quality-kit
Version: 0.1.3
Summary: A deterministic testbed for telematics data quality
License:                                  Apache License
                                   Version 2.0, January 2004
                                https://www.apache.org/licenses/
        
        TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
        1. Definitions.
        
           "License" shall mean the terms and conditions for use, reproduction,
           and distribution as defined by Sections 1 through 9 of this document.
        
           "Licensor" shall mean the copyright owner or entity authorized by the
           copyright owner that is granting the License.
        
           "Legal Entity" shall mean the union of the acting entity and all other
           entities that control, are controlled by, or are under common control
           with that entity. For the purposes of this definition, "control" means
           (i) the power, direct or indirect, to cause the direction or management
           of such entity, whether by contract or otherwise, or (ii) ownership of
           fifty percent (50%) or more of the outstanding shares, or (iii)
           beneficial ownership of such entity.
        
           "You" (or "Your") shall mean an individual or Legal Entity exercising
           permissions granted by this License.
        
           "Source" form shall mean the preferred form for making modifications,
           including but not limited to software source code, documentation source,
           and configuration files.
        
           "Object" form shall mean any form resulting from mechanical
           transformation or translation of a Source form, including but not
           limited to compiled object code, generated documentation, and
           conversions to other media types.
        
           "Work" shall mean the work of authorship, whether in Source or Object
           form, made available under the License, as indicated by a copyright
           notice that is included in or attached to the work (an example is
           provided in the Appendix below).
        
           "Derivative Works" shall mean any work, whether in Source or Object
           form, that is based on (or derived from) the Work and for which the
           editorial revisions, annotations, elaborations, or other modifications
           represent, as a whole, an original work of authorship. For the purposes
           of this License, Derivative Works shall not include works that remain
           separable from, or merely link (or bind by name) to the interfaces of,
           the Work and Derivative Works thereof.
        
           "Contribution" shall mean any work of authorship, including the
           original version of the Work and any modifications or additions to that
           Work or Derivative Works thereof, that is intentionally submitted to
           Licensor for inclusion in the Work by the copyright owner or by an
           individual or Legal Entity authorized to submit on behalf of the
           copyright owner. For the purposes of this definition, "submitted"
           means any form of electronic, verbal, or written communication sent to
           the Licensor or its representatives, including but not limited to
           communication on electronic mailing lists, source code control systems,
           and issue tracking systems that are managed by, or on behalf of, the
           Licensor for the purpose of discussing and improving the Work, but
           excluding communication that is conspicuously marked or otherwise
           designated in writing by the copyright owner as "Not a Contribution."
        
           "Contributor" shall mean Licensor and any individual or Legal Entity
           on behalf of whom a Contribution has been received by Licensor and
           subsequently incorporated within the Work.
        
        2. Grant of Copyright License. Subject to the terms and conditions of
           this License, each Contributor hereby grants to You a perpetual,
           worldwide, non-exclusive, no-charge, royalty-free, irrevocable
           copyright license to reproduce, prepare Derivative Works of,
           publicly display, publicly perform, sublicense, and distribute the
           Work and such Derivative Works in Source or Object form.
        
        3. Grant of Patent License. Subject to the terms and conditions of
           this License, each Contributor hereby grants to You a perpetual,
           worldwide, non-exclusive, no-charge, royalty-free, irrevocable
           (except as stated in this section) patent license to make, have made,
           use, offer to sell, sell, import, and otherwise transfer the Work,
           where such license applies only to those patent claims licensable
           by such Contributor that are necessarily infringed by their
           Contribution(s) alone or by combination of their Contribution(s)
           with the Work to which such Contribution(s) was submitted. If You
           institute patent litigation against any entity (including a
           cross-claim or counterclaim in a lawsuit) alleging that the Work
           or a Contribution incorporated within the Work constitutes direct
           or contributory patent infringement, then any patent licenses
           granted to You under this License for that Work shall terminate
           as of the date such litigation is filed.
        
        4. Redistribution. You may reproduce and distribute copies of the
           Work or Derivative Works thereof in any medium, with or without
           modifications, and in Source or Object form, provided that You
           meet the following conditions:
        
           (a) You must give any other recipients of the Work or Derivative
               Works a copy of this License; and
        
           (b) You must cause any modified files to carry prominent notices
               stating that You changed the files; and
        
           (c) You must retain, in the Source form of any Derivative Works
               that You distribute, all copyright, patent, trademark, and
               attribution notices from the Source form of the Work,
               excluding those notices that do not pertain to any part of
               the Derivative Works; and
        
           (d) If the Work includes a "NOTICE" text file as part of its
               distribution, then any Derivative Works that You distribute must
               include a readable copy of the attribution notices contained
               within such NOTICE file, excluding those notices that do not
               pertain to any part of the Derivative Works, in at least one
               of the following places: within a NOTICE text file distributed
               as part of the Derivative Works; within the Source form or
               documentation, if provided along with the Derivative Works; or,
               within a display generated by the Derivative Works, if and
               wherever such third-party notices normally appear. The contents
               of the NOTICE file are for informational purposes only and
               do not modify the License. You may add Your own attribution
               notices within Derivative Works that You distribute, alongside
               or as an addendum to the NOTICE text from the Work, provided
               that such additional attribution notices cannot be construed
               as modifying the License.
        
           You may add Your own copyright statement to Your modifications and
           may provide additional or different license terms and conditions
           for use, reproduction, or distribution of Your modifications, or
           for any such Derivative Works as a whole, provided Your use,
           reproduction, and distribution of the Work otherwise complies with
           the conditions stated in this License.
        
        5. Submission of Contributions. Unless You explicitly state otherwise,
           any Contribution intentionally submitted for inclusion in the Work
           by You to the Licensor shall be under the terms and conditions of
           this License, without any additional terms or conditions.
           Notwithstanding the above, nothing herein shall supersede or modify
           the terms of any separate license agreement you may have executed
           with Licensor regarding such Contributions.
        
        6. Trademarks. This License does not grant permission to use the trade
           names, trademarks, service marks, or product names of the Licensor,
           except as required for reasonable and customary use in describing the
           origin of the Work and reproducing the content of the NOTICE file.
        
        7. Disclaimer of Warranty. Unless required by applicable law or
           agreed to in writing, Licensor provides the Work (and each
           Contributor provides its Contributions) on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
           implied, including, without limitation, any warranties or conditions
           of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
           PARTICULAR PURPOSE. You are solely responsible for determining the
           appropriateness of using or redistributing the Work and assume any
           risks associated with Your exercise of permissions under this
           License.
        
        8. Limitation of Liability. In no event and under no legal theory,
           whether in tort (including negligence), contract, or otherwise,
           unless required by applicable law (such as deliberate and grossly
           negligent acts) or agreed to in writing, shall any Contributor be
           liable to You for damages, including any direct, indirect, special,
           incidental, or consequential damages of any character arising as a
           result of this License or out of the use or inability to use the
           Work (including but not limited to damages for loss of goodwill,
           work stoppage, computer failure or malfunction, or any and all
           other commercial damages or losses), even if such Contributor
           has been advised of the possibility of such damages.
        
        9. Accepting Warranty or Additional Liability. While redistributing
           the Work or Derivative Works thereof, You may choose to offer,
           and charge a fee for, acceptance of support, warranty, indemnity,
           or other liability obligations and/or rights consistent with this
           License. However, in accepting such obligations, You may act only
           on Your own behalf and on Your sole responsibility, not on behalf
           of any other Contributor, and only if You agree to indemnify,
           defend, and hold each Contributor harmless for any liability
           incurred by, or claims asserted against, such Contributor by reason
           of your accepting any such warranty or additional liability.
        
        END OF TERMS AND CONDITIONS
        
        APPENDIX: How to apply the Apache License to your work.
        
           To apply the Apache License to your work, attach the following
           boilerplate notice, with the fields enclosed by brackets "[]"
           replaced with your own identifying information. (Don't include
           the brackets!) The text should be enclosed in the appropriate
           comment syntax for the file format. We also recommend that a
           file or class name and description of purpose be included on the
           same "printed page" as the copyright notice for easier
           identification within third-party archives.
        
           Copyright [yyyy] [name of copyright owner]
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               https://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
        
        
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: mcp<3,>=2.2.0
Requires-Dist: opentelemetry-api>=1.45.1
Requires-Dist: PyYAML<7,>=6.0.3

﻿# telematics-quality-kit

**A deterministic, synthetic testbed for telematics data quality.** Generate and decode GT06 and Teltonika device frames, exercise a real parser-in-the-loop ingestion path, inject reversible simulator faults, and inspect every result with an explicit trust verdict.

This is a developer tool for building and testing telematics data systems. It is not a fleet management product, does not connect to real vehicles, and does not issue commands to devices. The current implementation is **v0.1** and uses synthetic data only.

## Contents

- [What you can do](#what-you-can-do)
- [Requirements and install](#requirements-and-install)
- [Configuration](#configuration)
- [60-second walkthrough](#60-second-walkthrough)
- [How quality results work](#how-quality-results-work)
- [Synthetic fleet and protocols](#synthetic-fleet-and-protocols)
- [Command reference](#command-reference)
  - [demo](#demo)
  - [serve: MCP server](#serve-mcp-server)
  - [shell: interactive and scripted sessions](#shell-interactive-and-scripted-sessions)
  - [inject: fault drills against a server](#inject-fault-drills-against-a-server)
  - [stream: pipeline events](#stream-pipeline-events)
  - [init: starter files](#init-starter-files)
  - [doctor: environment check](#doctor-environment-check)
  - [report: static HTML report](#report-static-html-report)
- [MCP tools, resources, and prompts](#mcp-tools-resources-and-prompts)
- [Development and verification](#development-and-verification)
- [Project layout](#project-layout)
- [Determinism, safety, and limitations](#determinism-safety-and-limitations)
- [License](#license)

## What you can do

- Generate deterministic GT06 and Teltonika frames from one seed, then decode them through the same codecs used by the ingestion pipeline.
- Run a synthetic fleet in-process, over MCP stdio, or over Streamable HTTP bound to loopback by default.
- Inspect fleet and vehicle quality through the developer shell or MCP tools.
- Inject reversible faults into the simulator and check that the reported quality changes as expected.
- Follow raw frame, decode, store, check, fault, and verdict events through a bounded event tap.
- Create a self-contained, reproducible HTML report with inline CSS and SVG, no JavaScript, and no external requests.
- Run unit, integration, property, and CLI tests, along with lint, formatting, typing, and dependency-audit checks.

## Requirements and install

- Python **3.12 or newer**
- [`uv`](https://docs.astral.sh/uv/)
- Git, if cloning the repository

Clone the repository and install the project and its development dependencies:

```bash
git clone https://github.com/benkivuva/telematics-quality-kit.git
cd telematics-quality-kit
uv sync
```

Check the local environment and see the available commands:

```bash
uv run telematics doctor
uv run telematics --help
```

All examples below use `uv run telematics`, which runs the checked-out project in its managed environment. The package also installs `telematics-quality-kit` as an alias for the same CLI. Run `uv run telematics COMMAND --help` for the exact options of any command.

Choose the installation path for how you plan to use the toolkit:

- **Develop from a checkout:** use `uv sync` above; this installs the locked runtime and development dependencies.
- **Run the demo without a checkout:** after v0.1.2 is published to PyPI, use `uvx telematics-quality-kit demo`. Until then, run `uv run telematics demo` from a checkout.
- **Install into an existing Python environment:** after v0.1.2 is published, use `python -m pip install telematics-quality-kit`, then run `telematics demo`. The CLI package is not available on PyPI before release.
- **Run an MCP HTTP server in a container:** use Docker Compose as described under [Run the container](#run-the-container).

After publication, the no-clone command is:

```bash
uvx telematics-quality-kit demo
```

## Configuration

Drift thresholds are heuristics. The CLI reads these optional environment variables; unset values keep the defaults shown below:

| Variable | Default | Meaning |
| --- | ---: | --- |
| `TELEMATICS_DRIFT_WARN_FRACTION` | `0.01` | Fraction of points that may be flagged before drift becomes WARN. |
| `TELEMATICS_DRIFT_BLOCK_FRACTION` | `0.05` | Fraction of points that may be flagged before drift becomes BLOCK; must be greater than the WARN fraction. |
| `TELEMATICS_DRIFT_STATIONARY_JITTER_METERS` | `25` | Stationary movement tolerance in meters. |

Set these in the environment before starting `demo`, `serve`, `shell`, `stream`, or `report`. For example, in PowerShell:

```powershell
$env:TELEMATICS_DRIFT_WARN_FRACTION = "0.02"
$env:TELEMATICS_DRIFT_BLOCK_FRACTION = "0.08"
uv run telematics shell
```

The same variables can be exported in Bash. Values must be finite numbers, the BLOCK fraction must exceed WARN, and the jitter threshold must be positive.

## 60-second walkthrough

Run the deterministic demo:

```bash
uv run telematics demo
```

Open the embedded shell and inspect the simulated fleet:

```bash
uv run telematics shell
```

At the `telematics>` prompt, try:

```text
fleet
ls --limit 5
get TRK-0001
report TRK-0001 --verbose
explain TRK-0001 freshness
```

The shell starts with an embedded synthetic fleet. Begin with `help` or `fleet`; `help` lists every command available in the current v0.1 shell.

Exercise a fault and advance simulated time without waiting on the wall clock:

```text
inject blackout TRK-0001 --duration 20m
advance 6m
get TRK-0001
clear
exit
```

Generate a shareable report in the current directory:

```bash
uv run telematics report --out fleet-report.html
```

The report uses deterministic simulated time. Repeating the same command with the same project version and inputs produces the same report bytes.

## How quality results work

Each vehicle receives six checks. Their thresholds are configurable heuristics; they are not ground truth or a replacement for domain-specific validation.

| Check | What it evaluates |
| --- | --- |
| `freshness` | Age of the latest report relative to the profile's expected reporting interval. |
| `drift` | Implausible movement, such as a jump exceeding the profile's maximum speed or excessive stationary jitter. |
| `duplicates` | Repeated telemetry records and excessive heartbeat rates. |
| `ignition` | Excessive or implausible ignition transitions. |
| `plausibility` | Invalid coordinates, future timestamps, clock skew, and profile-inconsistent speeds. |
| `fuel` | Fuel-level changes that cannot be explained by distance and operating context. It flags an unexplained change; it does not claim theft. |

The v0.1 vehicle trust score is the weighted mean of check scores, capped by the worst check verdict:

| Check | Weight |
| --- | ---: |
| Freshness | 0.30 |
| Drift | 0.25 |
| Duplicates | 0.15 |
| Ignition | 0.10 |
| Plausibility | 0.15 |
| Fuel | 0.05 |

The verdict caps are `PASS = 1.0`, `WARN = 0.79`, and `BLOCK = 0.39`. The fleet score is the mean of vehicle scores; the accompanying verdict counts keep a high fleet average from hiding individual problems.

| Verdict | Trust label | Meaning |
| --- | --- | --- |
| `PASS` âœ“ | **trust it** | Values are usable for decisions under the configured heuristics. |
| `WARN` âš  | **be careful** | Values are returned with caveats. |
| `BLOCK` â›” | **don't trust it** | A BLOCK is a normal result, not a tool error. Last-known location values may still be present and clearly labelled, but `usable_for_decision` is `false`; affected derived values are withheld and listed. |

Every quality-bearing result includes a `data_quality` object with a plain-language label, symbol, verdict, score, `usable_for_decision`, caveats, withheld values, and the evaluation window. Detailed per-check metrics are available in verbose quality reports. Verdicts are always conveyed using text and a symbol, not color alone.

## Synthetic fleet and protocols

The simulator generates only synthetic identifiers such as `TRK-0001`; its IMEIs are synthetic and zero-prefixed. The default fleet size is 12, the default seed is 42, and supported fleet sizes are 1 through 10,000. That maximum is a validation guard, not a throughput claim. The implemented wire codecs are GT06 and standard Teltonika Codec 8 (`0x08`); Codec 8 Extended (`0x8E`) is not implemented.

The default profile mix is `trk=40,lry=30,car=30`. Profiles are allocated deterministically by largest remainder. Protocol assignment and profile defaults are:

| Profile | Vehicle type | Maximum speed | Reporting interval | Protocol |
| --- | --- | ---: | ---: | --- |
| `trk` | Long-haul truck | 90 km/h | 30 s | Teltonika Codec 8 |
| `lry` | Delivery lorry | 60 km/h | 60 s | Teltonika Codec 8 |
| `car` | Ride-hail / PSV car | 70 km/h | 20 s | GT06 |
| `pik` | Mixed pickup | 80 km/h | 45 s | GT06 |

Set a specific mix with comma-separated percentages that sum to 100:

```bash
uv run telematics serve --vehicles 100 --profile-mix trk=40,lry=30,car=15,pik=15
uv run telematics report --vehicles 40 --profile-mix trk=50,lry=25,car=15,pik=10 --out mixed.html
```

The simulator derives its per-vehicle and per-tick pseudo-random state from the master seed. The same seed, clock, fleet options, and inputs produce the same generated wire bytes and results. Time-sensitive behavior uses an injected simulator clock, not the current wall clock.

## Command reference

The installed command is `telematics`. Its current v0.1 subcommands are:

```text
demo  serve  shell  inject  stream  init  doctor  report
```

Use `uv run telematics --help` or `uv run telematics COMMAND --help` for up-to-date usage. The architecture document also describes future v0.2 commands; they are not part of the current CLI.

### `demo`

Print a deterministic before-and-after fleet table for the vertical-slice quality demo:

```bash
uv run telematics demo
uv run telematics demo --vehicles 20 --seed 7
```

Options:

- `--vehicles N`: fleet size from 1 to 10,000; default `3` for the demo.
- `--seed S`: deterministic integer seed; default `42`.

### `serve`: MCP server

Start an in-memory synthetic fleet and expose the MCP surface. The default transport is Streamable HTTP at `http://127.0.0.1:8000/mcp`:

```bash
uv run telematics serve
```

Options:

- `--vehicles N`: fleet size, default `12`.
- `--seed S`: seed, default `42`.
- `--profile-mix trk=40,lry=30,car=15,pik=15`: profile percentages; values must be positive and sum to 100. Omit a profile to exclude it.
- `--transport streamable-http|stdio`: transport, default `streamable-http`.
- `--host HOST`: HTTP bind host, default `127.0.0.1` (loopback).
- `--port PORT`: HTTP port, default `8000`, valid range 1â€“65,535.
- `--speed Nx`: simulated clock rate, default `1x`. For example, `60x` advances roughly an hour of simulated time per minute of elapsed time.

Examples:

```bash
uv run telematics serve --vehicles 50 --seed 23 --profile-mix trk=40,lry=30,car=15,pik=15
uv run telematics serve --transport streamable-http --host 127.0.0.1 --port 8010 --speed 60x
uv run telematics serve --transport stdio
```

The HTTP server is local-only by default. Use `--host` to change the bind address deliberately; avoid exposing a simulator with destructive fault tools to an untrusted network. The stdio transport reserves stdout for MCP protocol messages.

### `shell`: interactive and scripted sessions

Start an embedded simulator shell (no server required):

```bash
uv run telematics shell
uv run telematics shell --vehicles 200 --seed 42
uv run telematics shell --profile-mix trk=50,lry=20,car=20,pik=10
```

Run one command non-interactively with `-c`:

```bash
uv run telematics shell -c 'fleet'
uv run telematics shell -c 'get TRK-0001'
```

Attach the shell to a running Streamable HTTP MCP server instead of creating an embedded fleet:

```bash
uv run telematics shell --attach
uv run telematics shell --attach http://127.0.0.1:8010/mcp -c 'tools'
```

Available shell commands:

| Command | Behavior |
| --- | --- |
| `fleet` | Show vehicle count, fleet trust score, and PASS/WARN/BLOCK counts. |
| `ls [--limit N]` | List vehicles, lower trust scores first; default limit 50. |
| `get VEHICLE_ID` | Show the last location, age, trust signal, and caveats. |
| `report [VEHICLE_ID] [--verbose]` | Print a structured quality report for a vehicle or the fleet. `--verbose` includes check metrics. |
| `explain VEHICLE_ID CHECK` | Show a check's verdict, score, and metrics. Checks: `freshness`, `drift`, `duplicates`, `ignition`, `plausibility`, `fuel`. |
| `inject FAULT VEHICLE_ID [--severity low\|medium\|high] [--duration 15m]` | Inject a reversible fault into an embedded simulator. |
| `clear` | Clear simulator faults and restore seeded frames. |
| `faults` | List active fault IDs in embedded mode. |
| `advance DURATION` | Advance embedded simulated time by a duration such as `30s`, `6m`, or `1h`. |
| `pause` / `resume` | Pause or resume automatic simulator time in embedded mode. Explicit `advance` still works while paused. |
| `speed [Nx]` | Show or set the automatic simulator clock rate, such as `speed 10x`. |
| `reset` | Clear active simulator faults and restore the seeded frames. |
| `seed INTEGER` | Rebuild the embedded fleet using a new seed. |
| `call TOOL [JSON_ARGUMENTS]` | Call a public MCP tool and print its structured response. |
| `tools` | List tools exposed by the active MCP server. |
| `expect verdict VEHICLE_ID PASS\|WARN\|BLOCK` | Assert a vehicle verdict in a script or interactive session. |
| `expect field VEHICLE_ID PATH VALUE` | Assert a field in the location response, using dotted paths such as `data_quality.usable_for_decision`; `true` and `false` are parsed as booleans. |
| `exit`, `quit`, `Ctrl-D` | Leave the shell. |

Fault names accept aliases: `stale`, `drift`, `blackout`, `dupes`, `flicker`, `skew`, `fuel-spike`, and `burst`; canonical names are also accepted. Severity scales the fault's default magnitude or duration: low `0.5x`, medium `1.0x`, high `2.0x`.

`advance`, `pause`, `resume`, `speed`, `reset`, and `seed` are available only for embedded sessions. An attached session talks only to the server's public MCP tools. The `events` shell command is planned for v0.2 and is not available in v0.1. Tab completion depends on platform readline support and is not available on every Windows Python installation.

Script files are plain text, one shell command per line; blank lines and `#` comments are ignored. Any command error or failed `expect` stops the script with exit code 1:

```bash
uv run telematics init script blackout-drill.tqs
```

Edit the file to contain, for example:

```text
inject blackout TRK-0001 --duration 20m
advance 6m
expect verdict TRK-0001 BLOCK
expect field TRK-0001 data_quality.usable_for_decision false
```

Then run it:

```bash
uv run telematics shell --script blackout-drill.tqs
```

The shell script runs against a new embedded fleet by default. Attach scripts with `--attach URL` only if their commands should use a running MCP server; `advance` will not work in attached mode.

### `inject`: fault drills against a server

The standalone `inject` command calls the simulator-only MCP fault tools on a running server. In one terminal, start the server:

```bash
uv run telematics serve --speed 60x
```

In another terminal, inject a fault:

```bash
uv run telematics inject blackout --vehicle TRK-0001 --severity high --duration 20m
```

Wait for a selected verdict and use the exit code in automation:

```bash
uv run telematics inject blackout --vehicle TRK-0001 --duration 20m --wait 30s --expect BLOCK
# Bash / macOS / Linux / WSL:
echo $?
# PowerShell:
$LASTEXITCODE
```

Options:

- Positional `fault`: canonical names or aliases: `stale_device`/`stale`, `gps_drift`/`drift`, `rural_blackout`/`blackout`, `duplicate_heartbeats`/`dupes`, `ignition_flicker`/`flicker`, `clock_skew`/`skew`, `fuel_sensor_spike`/`fuel-spike`, `buffered_burst`/`burst`.
- `--vehicle ID`: required synthetic vehicle ID.
- `--severity low|medium|high`: default `medium`; scales default magnitude/duration by 0.5x, 1x, or 2x.
- `--duration 15m`: optional simulated fault duration. Supported units are integer seconds (`30s`), minutes (`15m`), and hours (`1h`).
- `--url URL`: MCP endpoint, default `http://127.0.0.1:8000/mcp`.
- `--wait [DURATION]`: poll for a verdict after injection; omitting the duration means `30s`.
- `--expect PASS|WARN|BLOCK`: expected verdict while waiting; default `BLOCK`.

Exit code is 0 if the injection succeeds or the requested verdict is observed, 1 if the wait expires without observing it, 2 for invalid command input, and 3 if the server cannot be reached or does not expose the simulator fault capability. `serve --speed 60x` lets simulated freshness age faster while `inject --wait` uses real elapsed time for its polling timeout. The default vehicle fleet includes `TRK-0001`; IDs depend on selected profile mix and fleet size.

### `stream`: pipeline events

Stream follows the event tap of an embedded fleet. It is intentionally a continuous command; press `Ctrl-C` to stop. Each subscriber uses a bounded queue so stream consumers do not block frame ingestion; older queued events may be dropped when a consumer falls behind.

```bash
uv run telematics stream
uv run telematics stream --vehicles 50 --rate 20
uv run telematics stream --vehicle TRK-0001 --stage issue --stage verdict
uv run telematics stream --issues-only
uv run telematics stream --stage rx --hex
uv run telematics stream --json | jq .
```

Options:

- `--vehicles N`: fleet size, default `12`.
- `--seed S`: deterministic seed, default `42`.
- `--vehicle ID`: follow one vehicle.
- `--stage STAGE`: filter by one or more stages: `rx`, `decode`, `store`, `check`, `issue`, `parse`, `verdict`, or `fault`.
- `--issues-only`: retain priority issues, parse failures, fault events, and verdict changes.
- `--hex`: show full frame hex instead of the short default preview.
- `--json`: emit one schema-validated JSON object per line (summary goes to stderr).
- `--rate N`: maximum normal event output rate in lines per second, default `20`; must be a positive integer. Priority events bypass sampling.

### `init`: starter files

Print a starter template to the terminal or create a new file. Existing paths are never overwritten:

```bash
uv run telematics init fleet
uv run telematics init fleet fleet.yaml
uv run telematics init scenario blackout.yaml
uv run telematics init script drill.tqs
```

Templates:

- `fleet`: seed, vehicle count, and `profile_mix` percentages.
- `scenario`: name, seed, vehicle count, duration, optional profile mix, and a list of fault/vehicle/severity entries.
- `script`: commands for `telematics shell --script`.

Scenario files are YAML and are loaded with PyYAML `safe_load`. Example:

```yaml
name: blackout-drill
seed: 42
vehicles: 12
duration: 1h
profile_mix:
  trk: 40
  lry: 30
  car: 20
  pik: 10
faults:
  - fault: rural_blackout
    vehicle_id: TRK-0001
    severity: high
```

### `doctor`: environment check

Check for Python 3.12+, `uv`, and an installed project package:

```bash
uv run telematics doctor
```

The command prints one status or repair hint per check and returns non-zero if a requirement is missing.

### `report`: static HTML report

Generate one self-contained synthetic fleet report without starting a server:

```bash
uv run telematics report
uv run telematics report --clean --out clean.html
uv run telematics report --scenario blackout.yaml --out blackout-report.html
uv run telematics report --inject drift:CAR-0001:high --duration 1h --fault-duration 20m --open
```

With no `--clean`, `--scenario`, or `--inject`, the default is a labelled **showcase** scenario with faults applied to sample vehicles. Use `--clean` for a baseline, `--inject` for explicit faults, or `--scenario` for a YAML scenario. A scenario can be combined with additional `--inject` flags; `--clean` cannot be combined with `--inject`.

Options:

- `--vehicles N`: number of synthetic vehicles; defaults to 12 or the scenario's `vehicles` value.
- `--seed S`: seed; defaults to 42 or the scenario's seed.
- `--scenario FILE`: YAML scenario file.
- `--clean`: produce a fault-free baseline.
- `--inject FAULT:VEHICLE[:SEVERITY]`: add a fault; repeat the option for multiple faults. Severity defaults to `medium`. Example: `--inject blackout:TRK-0001:high`.
- `--duration 1h`: total simulated report window; defaults to 1 hour or the scenario's duration.
- `--fault-duration 20m`: how long faults are active at the end of the window; default `20m`.
- `--detail N`: number of worst vehicles to include in detailed cards, from 1 to 200; default `50`.
- `--profile-mix trk=40,lry=30,car=20,pik=10`: profile distribution; defaults to scenario mix if supplied.
- `--out FILE`: output path; default `fleet-report.html`.
- `--open`: open the resulting file in the default browser.

The report writes to `--out` and silently replaces an existing file at that path. Choose a new path or back up the existing report if you need to keep it. The report includes a synthetic-data notice, scenario and seed, simulated range in East Africa Time, injected faults, fleet score and verdict counts, a worst-first vehicle table, per-check metrics and caveats for detailed vehicles, freshness timelines, and local SVG tracks. A BLOCKed vehicle's location track is withheld when the quality rules prohibit presenting it as decision-ready. HTML strings are escaped. The document contains inline CSS and SVG, a restrictive CSP meta tag, no JavaScript, no external fonts or requests, and a black-and-white-friendly print stylesheet. Reports larger than 5 MB produce a warning suggesting a lower `--detail` value.

## MCP tools, resources, and prompts

`telematics serve` exposes 11 v0.1 tools over either stdio or Streamable HTTP. `inject_fault` and `reset_simulator` are registered only when the active backend exposes the simulator fault capability; they are not general device-control commands.

| Tool | Purpose |
| --- | --- |
| `list_vehicles` | Paginated vehicle list with compact quality signals. |
| `get_vehicle_location` | Last location, age, and quality. |
| `get_trip_history` | Trip history for a vehicle and time window; affected distance may be withheld. |
| `get_fuel_events` | Unexplained fuel changes for a vehicle or fleet and window. |
| `get_quality_report` | Vehicle or fleet quality report; optional `verbose` includes check metrics. |
| `check_fleet_freshness` | Fleet report age and freshness verdicts. |
| `check_gps_drift` | GPS drift results for one vehicle and time window. |
| `check_duplicates` | Repeated records and heartbeat-rate checks for a time window. |
| `list_quality_issues` | Ranked WARN/BLOCK findings and suggested follow-up tools. |
| `inject_fault` | Simulator-only reversible fault injection. |
| `reset_simulator` | Simulator-only reset to seeded frames. |

Every output has a `schema_version`; input and output models reject undeclared fields. JSON Schema files for each tool are in [`docs/schemas/`](docs/schemas/). `BLOCK` responses are successful tool responses with `usable_for_decision=false`, not JSON-RPC errors.

The server also exposes:

- Resources: `quality://checks` and `fleet://profiles`.
- Prompts: `fleet_health_briefing`, `investigate_vehicle`, and `fault_drill`.

For an MCP client supporting stdio, configure its server entry with the command below and replace the directory with your checkout's absolute path. A stdio client launches `telematics serve` as a child process, so there is no second terminal to start and keep open. `stdout` is reserved for MCP protocol messages; diagnostics go to `stderr`.

```json
{
  "mcpServers": {
    "telematics-quality-kit": {
      "command": "uv",
      "args": [
        "run",
        "--directory",
        "/absolute/path/to/telematics-quality-kit",
        "telematics",
        "serve",
        "--transport",
        "stdio"
      ]
    }
  }
}
```

On Windows, use an absolute path and JSON-escape each backslash (or use forward slashes), for example:

```json
{
  "mcpServers": {
    "telematics-quality-kit": {
      "command": "uv",
      "args": ["run", "--directory", "C:/Users/you/projects/telematics-quality-kit", "telematics", "serve", "--transport", "stdio"]
    }
  }
}
```

For an HTTP-capable client, open two terminals. Keep the server running in the first:

```bash
uv run telematics serve
```

Configure/connect the client in the second terminal or its settings to `http://127.0.0.1:8000/mcp`. Stop the server with `Ctrl-C` when finished. HTTP clients do not launch the server process for you.

## Development and verification

### Run the container

The published GHCR image runs the Streamable HTTP MCP server. Docker Compose pulls the prebuilt image and publishes it on host loopback only:

```bash
docker compose up -d
```

Connect an MCP client to `http://127.0.0.1:8000/mcp`. Stop the service with `docker compose down`. The image runs as a non-root user with a read-only root filesystem and dropped capabilities. See [the threat model](docs/threat-model.md) and [privacy notes](docs/privacy.md).

The container filesystem is read-only and Compose does not mount a persistent report directory by default. To create a report inside its temporary `/tmp` filesystem and copy it to the host (PowerShell example):

```powershell
docker compose run --name tqk-report --no-deps telematics-quality-kit report --out /tmp/report.html
docker cp tqk-report:/tmp/report.html .\report.html
docker rm tqk-report
```

The report disappears with the temporary container unless copied out. A bind mount can also be used, but the host directory must be writable by the container's non-root user; on Windows, Docker Desktop handles the file sharing while Linux hosts may need to adjust directory ownership or permissions.

Install all project and development dependencies:

```bash
uv sync
```

Run all local quality gates:

```bash
make verify
```

`make verify` runs Ruff lint, Ruff format check, strict mypy for `src/telematics`, and the full pytest suite. Additional useful commands:

```bash
# All test tiers
uv run pytest

# Individual test tiers
uv run pytest -m unit
uv run pytest -m integration
uv run pytest -m property

# CLI tests
uv run pytest tests/cli

# Lint, formatting, typing
uv run ruff check .
uv run ruff format --check .
uv run mypy src/telematics

# Dependency vulnerability audit
make security
# equivalent
uv run pip-audit

# Deterministic demo
make demo
# equivalent
uv run telematics demo
```

GitHub Actions runs lint, format, typing, the full test suite, and CLI smoke tests on Linux, macOS, and Windows. The security workflow runs repository and dependency scanning. Protocol fixture sources and independent CRC verification notes are documented in [`tests/fixtures/README.md`](tests/fixtures/README.md).

Useful project references:
- [`docs/schemas/`](docs/schemas/): generated MCP tool schemas.

## Project layout

```text
src/telematics/
  backends/    FleetBackend and simulator implementation
  cli/         demo, serve, shell, inject, stream, init, doctor, report commands
  codecs/      GT06, Teltonika Codec 8, and CRC implementations
  ingest/      framing, parser-in-the-loop pipeline, and bounded event tap
  mcp/         strict schemas, server tools, resources, and prompts
  models/      telemetry and quality result models
  quality/     six pure quality checks and weighted scoring
  report/      deterministic report data, HTML rendering, and SVG generation
  service/     backend-independent application logic
  simulator/   deterministic generator, Kenya profiles, and real-time clock driver
  store/       bounded in-memory telemetry store
tests/
  cli/         command-line and report behavior
  fixtures/    copied protocol vectors and provenance
  integration/ in-process MCP and serve behavior
  property/    parser, scoring, and determinism invariants
  unit/        focused codec, pipeline, simulator, and quality tests
```

## Determinism, safety, and limitations

- **Synthetic only:** example IDs, frames, and locations are generated test data, not captured device data or production fleet information.
- **No device commands:** the toolkit does not issue commands to GPS trackers or vehicles. Fault injection mutates the local simulator only.
- **Loopback default:** the HTTP server binds to `127.0.0.1` unless `--host` is explicitly changed.
- **Parser in the data path:** simulator frames are encoded to protocol bytes and decoded before telemetry reaches storage and quality checks.
- **Deterministic time and randomness:** fixed seeds and injected clocks make simulations, tests, and reports repeatable. The long-running `stream` command sleeps to drive a live display; tests do not rely on wall-clock sleeps.
- **Bounded event consumers:** event tap subscriptions have bounded queues; slow consumers do not block the ingestion pipeline and can miss older queued events.
- **Heuristic checks:** scores and thresholds describe configurable defaults. They are not a guarantee that a value is correct.
- **v0.1 scope:** current commands are listed in the [command reference](#command-reference). Features marked v0.2 in `ARCHITECTURE.md`â€”including a TCP listener, real Traccar backend, capture/replay, dashboard, metrics intervals, and external device simulatorâ€”are not available in this release.

## License

This project is distributed under the [Apache-2.0 License](LICENSE).
