trioditis 0.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- trioditis-0.6.0/LICENSE +21 -0
- trioditis-0.6.0/PKG-INFO +212 -0
- trioditis-0.6.0/README.md +195 -0
- trioditis-0.6.0/pyproject.toml +54 -0
- trioditis-0.6.0/setup.cfg +4 -0
- trioditis-0.6.0/tests/test_effort_trial.py +123 -0
- trioditis-0.6.0/tests/test_evalroutes.py +638 -0
- trioditis-0.6.0/tests/test_hardware.py +345 -0
- trioditis-0.6.0/tests/test_localcheck.py +216 -0
- trioditis-0.6.0/tests/test_metrics.py +280 -0
- trioditis-0.6.0/tests/test_plan.py +255 -0
- trioditis-0.6.0/tests/test_planfit.py +204 -0
- trioditis-0.6.0/tests/test_readme.py +20 -0
- trioditis-0.6.0/tests/test_refit.py +191 -0
- trioditis-0.6.0/tests/test_route_study.py +108 -0
- trioditis-0.6.0/tests/test_stats.py +147 -0
- trioditis-0.6.0/tests/test_trioditis.py +1323 -0
- trioditis-0.6.0/trioditis/__init__.py +3 -0
- trioditis-0.6.0/trioditis/__main__.py +514 -0
- trioditis-0.6.0/trioditis/accounts.py +184 -0
- trioditis-0.6.0/trioditis/app.py +633 -0
- trioditis-0.6.0/trioditis/config.py +311 -0
- trioditis-0.6.0/trioditis/envvars.py +9 -0
- trioditis-0.6.0/trioditis/evalroutes.py +626 -0
- trioditis-0.6.0/trioditis/hardware.py +765 -0
- trioditis-0.6.0/trioditis/localcheck.py +121 -0
- trioditis-0.6.0/trioditis/plan.py +327 -0
- trioditis-0.6.0/trioditis/planfit.py +277 -0
- trioditis-0.6.0/trioditis/pricefacts.py +205 -0
- trioditis-0.6.0/trioditis/refit.py +256 -0
- trioditis-0.6.0/trioditis/router.py +475 -0
- trioditis-0.6.0/trioditis/security.py +125 -0
- trioditis-0.6.0/trioditis/templates/config.example.yaml +147 -0
- trioditis-0.6.0/trioditis/templates/env.example +10 -0
- trioditis-0.6.0/trioditis.egg-info/PKG-INFO +212 -0
- trioditis-0.6.0/trioditis.egg-info/SOURCES.txt +46 -0
- trioditis-0.6.0/trioditis.egg-info/dependency_links.txt +1 -0
- trioditis-0.6.0/trioditis.egg-info/entry_points.txt +2 -0
- trioditis-0.6.0/trioditis.egg-info/requires.txt +4 -0
- trioditis-0.6.0/trioditis.egg-info/top_level.txt +2 -0
- trioditis-0.6.0/trioditis_metrics/__init__.py +5 -0
- trioditis-0.6.0/trioditis_metrics/db.py +372 -0
- trioditis-0.6.0/trioditis_metrics/guard.py +151 -0
- trioditis-0.6.0/trioditis_metrics/home.py +16 -0
- trioditis-0.6.0/trioditis_metrics/plan.py +208 -0
- trioditis-0.6.0/trioditis_metrics/prices.py +76 -0
- trioditis-0.6.0/trioditis_metrics/ratelimits.py +78 -0
- trioditis-0.6.0/trioditis_metrics/stats.py +199 -0
trioditis-0.6.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alex
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
trioditis-0.6.0/PKG-INFO
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: trioditis
|
|
3
|
+
Version: 0.6.0
|
|
4
|
+
Summary: Security-first LLM router: one OpenAI-compatible endpoint that picks the right model per prompt.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/alexb-joat/trioditis
|
|
7
|
+
Project-URL: Issues, https://github.com/alexb-joat/trioditis/issues
|
|
8
|
+
Project-URL: Security, https://github.com/alexb-joat/trioditis/security/policy
|
|
9
|
+
Requires-Python: >=3.11
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: fastapi
|
|
13
|
+
Requires-Dist: uvicorn
|
|
14
|
+
Requires-Dist: httpx
|
|
15
|
+
Requires-Dist: pyyaml
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
<div align="center">
|
|
19
|
+
|
|
20
|
+
<picture>
|
|
21
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/alexb-joat/trioditis/main/docs/img/banner-dark.svg">
|
|
22
|
+
<img alt="Trioditis: one endpoint, the right model for every prompt" src="https://raw.githubusercontent.com/alexb-joat/trioditis/main/docs/img/banner-light.svg" width="720">
|
|
23
|
+
</picture>
|
|
24
|
+
|
|
25
|
+
**A security-first router for LLMs.** One OpenAI-compatible endpoint that sends each prompt to the right model, whether local (Ollama), cloud (Claude, GPT, OpenRouter) or legacy, and keeps sensitive prompts on your machine.
|
|
26
|
+
|
|
27
|
+
[](https://github.com/alexb-joat/trioditis/actions/workflows/ci.yml)
|
|
28
|
+
[](https://github.com/alexb-joat/trioditis/actions/workflows/codeql.yml)
|
|
29
|
+

|
|
30
|
+

|
|
31
|
+
[](https://github.com/alexb-joat/trioditis/blob/main/LICENSE)
|
|
32
|
+
|
|
33
|
+
[Quick start](#quick-start) · [How it routes](#how-a-request-is-routed) · [Connect your tools](#connect-your-tools) · [Security](#security-at-a-glance) · [Docs](#documentation)
|
|
34
|
+
|
|
35
|
+
<sub><i>Trioditis: Hecate of the three ways, who stands where the road splits and decides who goes where.<br>Every prompt reaches the same crossroads here: a local model, a cloud model, or no further.</i></sub>
|
|
36
|
+
|
|
37
|
+
</div>
|
|
38
|
+
|
|
39
|
+
<br>
|
|
40
|
+
|
|
41
|
+
<picture>
|
|
42
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/alexb-joat/trioditis/main/docs/img/routing-dark.svg">
|
|
43
|
+
<img alt="Clients send requests to Trioditis on 127.0.0.1:8787. It checks privacy, picks a route, filters models and calls with fallback to local, cloud or legacy models." src="https://raw.githubusercontent.com/alexb-joat/trioditis/main/docs/img/routing-light.svg" width="880">
|
|
44
|
+
</picture>
|
|
45
|
+
|
|
46
|
+
## Why Trioditis
|
|
47
|
+
|
|
48
|
+
| Private by default | Cost-aware | Small and auditable | Explains itself |
|
|
49
|
+
|---|---|---|---|
|
|
50
|
+
| Prompts with emails, cards, keys or passwords go to a local model or are rejected. They are never sent to a cloud API by accident. | Daily and monthly caps with automatic local fallback, plus per-provider limits and live spend. | About 3,400 lines and four hash-pinned dependencies. No LiteLLM, no LangChain, no telemetry. | Every response names its route and model. A log records the reasons, with no prompt text. |
|
|
51
|
+
|
|
52
|
+
## Quick start
|
|
53
|
+
|
|
54
|
+
**1.** Install [Ollama](https://ollama.com/download) and pull a couple of local models (run it natively, not in Docker):
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
ollama pull llama3.2:3b && ollama pull qwen2.5-coder:7b && ollama pull nomic-embed-text
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**2.** Install Trioditis with [pipx](https://pipx.pypa.io/stable/installation/) and give it a folder:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pipx install trioditis
|
|
64
|
+
trioditis init --dir ~/trioditis # writes config.yaml and .env there (owner-only); never overwrites
|
|
65
|
+
cd ~/trioditis # config.yaml, .env and logs/ are read from the current folder
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
<details>
|
|
69
|
+
<summary>Or from a clone, with every package checked against the lockfile's hashes</summary>
|
|
70
|
+
|
|
71
|
+
`pipx` installs the newest release of every dependency on the day you run it. The clone installs the exact versions in `requirements.txt` and refuses any file whose hash differs. From a clone, write `python -m trioditis` wherever these pages say `trioditis`.
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
git clone https://github.com/alexb-joat/trioditis.git && cd trioditis
|
|
75
|
+
python3 -m venv .venv && source .venv/bin/activate # Debian/Ubuntu: sudo apt install python3-venv
|
|
76
|
+
pip install --require-hashes --no-deps -r requirements.txt
|
|
77
|
+
python -m trioditis init
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Windows (PowerShell):
|
|
81
|
+
|
|
82
|
+
```powershell
|
|
83
|
+
git clone https://github.com/alexb-joat/trioditis.git; cd trioditis
|
|
84
|
+
py -3 -m venv .venv; .\.venv\Scripts\Activate.ps1
|
|
85
|
+
pip install --require-hashes --no-deps -r requirements.txt
|
|
86
|
+
python -m trioditis init
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
</details>
|
|
90
|
+
|
|
91
|
+
**3.** Check and run, the same on every system:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
trioditis check # which local models answer, which cloud keys are set
|
|
95
|
+
trioditis serve # http://127.0.0.1:8787/v1, localhost only
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
With the models from step 1 pulled and no cloud keys yet, `check` prints:
|
|
99
|
+
|
|
100
|
+
```text
|
|
101
|
+
config OK: 9 models, 4 routes
|
|
102
|
+
local-general local ready
|
|
103
|
+
local-coder local ready
|
|
104
|
+
local-reasoner local not on this server: ollama pull deepseek-r1:8b
|
|
105
|
+
local-vision local not on this server: ollama pull llava:7b
|
|
106
|
+
legacy-mistral local not on this server: ollama pull mistral:7b
|
|
107
|
+
claude-sonnet cloud missing $ANTHROPIC_API_KEY
|
|
108
|
+
claude-haiku cloud missing $ANTHROPIC_API_KEY
|
|
109
|
+
claude-opus cloud missing $ANTHROPIC_API_KEY
|
|
110
|
+
gpt cloud missing $OPENAI_API_KEY
|
|
111
|
+
2 of 9 models confirmed ready; cloud providers are never contacted
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**4.** Try it:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
trioditis explain "Why does my python function raise KeyError?" # shows the decision, calls no model
|
|
118
|
+
curl -s http://127.0.0.1:8787/v1/chat/completions -H 'Content-Type: application/json' \
|
|
119
|
+
-d '{"model":"auto","messages":[{"role":"user","content":"Write a regex for UK postcodes"}]}'
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Cloud models are optional: add `ANTHROPIC_API_KEY`, `OPENAI_API_KEY` or `OPENROUTER_API_KEY` to `.env` and they join the routes. A cloud provider without a key is simply skipped.
|
|
123
|
+
|
|
124
|
+
## How a request is routed
|
|
125
|
+
|
|
126
|
+
1. **Privacy check.** Built-in detectors (emails, card numbers, IBANs, API keys, private keys, passwords) plus your own keywords and regexes. A hit restricts the request to local models; if none can serve it, it is rejected rather than leaked.
|
|
127
|
+
2. **Pick a route** (`code`, `reasoning`, `writing`, `quick`, or your own): keyword and pattern signals, the names of the tools a coding agent offers (`tool_keywords`), then similarity to each route's example prompts using a *local* embedding model, else the default route. An agent's later turns stay on the route its task started on.
|
|
128
|
+
3. **Filter and order models** by what the request needs: vision, tool calling, context length, and whether the provider's key is present. A route can keep its listed order or `prefer: cheapest` (local first, then by this request's estimated cost) or `fastest` (median latency of recent calls); a provider that just answered "too many requests" is tried last until its wait is over.
|
|
129
|
+
4. **Call the first model and fall back** on outage, rate limit, missing model or spending cap. If no model of the route fits, only another route's model or a local one is used, never a model you call by name only.
|
|
130
|
+
5. **Pin the session.** Requests with the same `X-Session-Id` (or `user`) stay on one route, so agents do not switch models mid-task.
|
|
131
|
+
|
|
132
|
+
Responses carry `X-Trioditis-Route` and `X-Trioditis-Model`. `POST /v1/route` or `trioditis explain "…"` shows the decision and reasons without calling a model. Clients can bypass routing with `model: "route/code"` (force a route), `model: "legacy-mistral"` (call a model directly) or the provider's own id, such as `model: "claude-haiku-5-5"`, which pins an exact version; `X-Trioditis-Upstream-Model` names the id the provider was sent.
|
|
133
|
+
|
|
134
|
+
## Connect your tools
|
|
135
|
+
|
|
136
|
+
| Client | Settings |
|
|
137
|
+
|---|---|
|
|
138
|
+
| **Cline** (VS Code) | Provider *OpenAI Compatible*, base URL `http://127.0.0.1:8787/v1`, model `route/code`. Full recipe, including local-model context: [docs/vscode-cline.md](https://github.com/alexb-joat/trioditis/blob/main/docs/vscode-cline.md) |
|
|
139
|
+
| **Open WebUI, Odysseus, chat UIs** | Add the endpoint `http://127.0.0.1:8787/v1` and pick model `auto` |
|
|
140
|
+
| **OpenAI SDKs** | `OpenAI(base_url="http://127.0.0.1:8787/v1", api_key="<your TRIODITIS_API_KEY>")` |
|
|
141
|
+
|
|
142
|
+
Trioditis speaks the OpenAI chat-completions format, which most tools support. Clients that only speak a provider's native API (for example Claude Code) cannot use it yet.
|
|
143
|
+
|
|
144
|
+
## Spending and limits
|
|
145
|
+
|
|
146
|
+
Every cloud call is checked against your daily and monthly caps first (defaults: $1 a day, $15 a month). Over a cap, the route's local model answers instead, so you get an answer, not a bill. `trioditis usage --since 7d --by client` shows what each tool spent. Per-provider caps, provider rate-limit and balance readouts, and call history are in [docs/spending-and-limits.md](https://github.com/alexb-joat/trioditis/blob/main/docs/spending-and-limits.md). `trioditis hardware` suggests which local models fit your computer ([docs/hardware.md](https://github.com/alexb-joat/trioditis/blob/main/docs/hardware.md)).
|
|
147
|
+
|
|
148
|
+
## Staying current
|
|
149
|
+
|
|
150
|
+
Each call records why its answer ended, retries and cache use, so `trioditis usage --outcomes` shows each model's error, cut-off and retry rates with 95% intervals. A daily `trioditis refit` adds latency and drift charts, and, given a model facts file, names configured models that are deprecated or about to retire and lists new ones worth testing. It changes nothing on its own: [docs/staying-current.md](https://github.com/alexb-joat/trioditis/blob/main/docs/staying-current.md). On a subscription, `trioditis plan import --claude-code` and `usage --plan` show tokens per model and effort next to how far the plan's meters rose, from counts only, never text ([Plan usage](https://github.com/alexb-joat/trioditis/blob/main/docs/spending-and-limits.md#plan-usage)).
|
|
151
|
+
|
|
152
|
+
## Security at a glance
|
|
153
|
+
|
|
154
|
+
| Risk | Control |
|
|
155
|
+
|---|---|
|
|
156
|
+
| Hijacked dependency | Four direct dependencies, hash-pinned (`--require-hashes`), `pip-audit` in CI, Dependabot with a 7-day hold |
|
|
157
|
+
| Router exposed to the network | Binds `127.0.0.1`; refuses any other bind without a 32+ character API key |
|
|
158
|
+
| A web page in your browser reaching the router | Foreign `Host` (DNS rebinding) or `Origin` gets 403 and a log line; POST bodies must be JSON |
|
|
159
|
+
| Sensitive data sent to the cloud | Detectors force local-only routing or reject |
|
|
160
|
+
| Key and prompt leakage | Keys only in environment variables; the client's key is never forwarded; logs hold metadata and a hash, not prompt text |
|
|
161
|
+
| Open proxy and SSRF | Upstream URLs come only from `config.yaml`; no redirects followed |
|
|
162
|
+
| Supply chain | Actions pinned to commit SHAs, read-only workflows, CodeQL and dependency review on every pull request |
|
|
163
|
+
|
|
164
|
+
Report a vulnerability privately via [SECURITY.md](https://github.com/alexb-joat/trioditis/blob/main/SECURITY.md). The full design and its limits are in [docs/security-model.md](https://github.com/alexb-joat/trioditis/blob/main/docs/security-model.md).
|
|
165
|
+
|
|
166
|
+
## Known limits
|
|
167
|
+
|
|
168
|
+
- Fallback happens before an answer starts. If a streamed answer breaks halfway, Trioditis does not retry it on another model.
|
|
169
|
+
- Every provider is called at its OpenAI-style `/chat/completions` address. There are no native Anthropic or Gemini adapters.
|
|
170
|
+
- No prompt caching for Claude models. Trioditis calls Anthropic at its OpenAI-compatible endpoint, and Anthropic's page for it says "Prompt caching is not supported" ([source](https://platform.claude.com/docs/en/cli-sdks-libraries/libraries/openai-sdk), read 13 November 2026). For every provider, Trioditis adds no caching settings of its own and records cached tokens only when the provider reports them.
|
|
171
|
+
- On Windows, Trioditis does not warn about a `.env` that other users can read, and it does not set the mode of the metrics database. Keep the folder in your user profile ([security model](https://github.com/alexb-joat/trioditis/blob/main/docs/security-model.md)).
|
|
172
|
+
- Only the Linux installer sets up the daily `refit` and `plan import`. On macOS and Windows, run them by hand.
|
|
173
|
+
- CI does not build the Docker image.
|
|
174
|
+
- The router picks the right route about two times in three. On 315 held-out prompts it chose the labelled route for 205 (65.1%, 95% interval 59.7–70.1%) on the example config with local embeddings on, and for 116 (36.8%) with keywords alone. Most misses (65 of 110) go to `quick`, the default route, and a miss still gets an answer. The prompts are synthetic and in English; measured 13 November 2026 ([method, data and limits](https://github.com/alexb-joat/trioditis/blob/main/docs/route-accuracy.md)). Your own routes give a different number: `trioditis eval-routes` measures it.
|
|
175
|
+
|
|
176
|
+
## Platforms
|
|
177
|
+
|
|
178
|
+
| Linux (systemd) | macOS (launchd) | Windows 10/11 (Scheduled Task) |
|
|
179
|
+
|---|---|---|
|
|
180
|
+
| Python 3.11+, CI | Python 3.11+, CI | Python 3.11+, CI |
|
|
181
|
+
|
|
182
|
+
CI runs the unit tests and a real-server smoke test on all three, on Python 3.11, 3.12 and 3.13. To run in the background or in a hardened container, see [docs/deployment.md](https://github.com/alexb-joat/trioditis/blob/main/docs/deployment.md).
|
|
183
|
+
|
|
184
|
+
## Documentation
|
|
185
|
+
|
|
186
|
+
- [VS Code with Cline](https://github.com/alexb-joat/trioditis/blob/main/docs/vscode-cline.md) · [Spending caps and provider limits](https://github.com/alexb-joat/trioditis/blob/main/docs/spending-and-limits.md) · [Hardware suggestions](https://github.com/alexb-joat/trioditis/blob/main/docs/hardware.md)
|
|
187
|
+
- [Background service and Docker](https://github.com/alexb-joat/trioditis/blob/main/docs/deployment.md) · [Security model](https://github.com/alexb-joat/trioditis/blob/main/docs/security-model.md) · [`config.example.yaml`](https://github.com/alexb-joat/trioditis/blob/main/config.example.yaml) (providers, models, routes, privacy, limits, logging)
|
|
188
|
+
|
|
189
|
+
**See every decision live:** [Hlidskjalv](https://github.com/alexb-joat/hlidskjalv) reads the decision log (route, model, tokens, never prompts) and shows it next to your files, commands and Git history.
|
|
190
|
+
|
|
191
|
+
<details>
|
|
192
|
+
<summary>Roadmap</summary>
|
|
193
|
+
|
|
194
|
+
- [x] Daily and monthly spending caps with local fallback
|
|
195
|
+
- [x] Hardware detection and local-model suggestions
|
|
196
|
+
- [x] Routes can prefer the cheapest or fastest model
|
|
197
|
+
- [x] Outcome rates per model, a plan-usage ledger and a daily refit
|
|
198
|
+
- [ ] A small local LLM as an optional classifier for ambiguous prompts
|
|
199
|
+
- [ ] Learn from feedback: per-route model scores from thumbs up and down
|
|
200
|
+
- [ ] Cost and latency budgets per route
|
|
201
|
+
- [ ] Native Anthropic and Gemini adapters (today: their OpenAI-compatible endpoints)
|
|
202
|
+
- [ ] Per-user keys and rate limits for team use
|
|
203
|
+
|
|
204
|
+
</details>
|
|
205
|
+
|
|
206
|
+
## Contributing
|
|
207
|
+
|
|
208
|
+
Pull requests are welcome; see [CONTRIBUTING.md](https://github.com/alexb-joat/trioditis/blob/main/CONTRIBUTING.md). Run the checks with `pip install --require-hashes --no-deps -r requirements-dev.txt && python -m pytest -q`. If Trioditis saves you money or time, the **Sponsor** button at the top of the repository supports it.
|
|
209
|
+
|
|
210
|
+
## License
|
|
211
|
+
|
|
212
|
+
[MIT](https://github.com/alexb-joat/trioditis/blob/main/LICENSE). Not affiliated with Anthropic, OpenAI, Ollama, OpenRouter or any model provider; their names refer only to their public APIs.
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<picture>
|
|
4
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/alexb-joat/trioditis/main/docs/img/banner-dark.svg">
|
|
5
|
+
<img alt="Trioditis: one endpoint, the right model for every prompt" src="https://raw.githubusercontent.com/alexb-joat/trioditis/main/docs/img/banner-light.svg" width="720">
|
|
6
|
+
</picture>
|
|
7
|
+
|
|
8
|
+
**A security-first router for LLMs.** One OpenAI-compatible endpoint that sends each prompt to the right model, whether local (Ollama), cloud (Claude, GPT, OpenRouter) or legacy, and keeps sensitive prompts on your machine.
|
|
9
|
+
|
|
10
|
+
[](https://github.com/alexb-joat/trioditis/actions/workflows/ci.yml)
|
|
11
|
+
[](https://github.com/alexb-joat/trioditis/actions/workflows/codeql.yml)
|
|
12
|
+

|
|
13
|
+

|
|
14
|
+
[](https://github.com/alexb-joat/trioditis/blob/main/LICENSE)
|
|
15
|
+
|
|
16
|
+
[Quick start](#quick-start) · [How it routes](#how-a-request-is-routed) · [Connect your tools](#connect-your-tools) · [Security](#security-at-a-glance) · [Docs](#documentation)
|
|
17
|
+
|
|
18
|
+
<sub><i>Trioditis: Hecate of the three ways, who stands where the road splits and decides who goes where.<br>Every prompt reaches the same crossroads here: a local model, a cloud model, or no further.</i></sub>
|
|
19
|
+
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
<br>
|
|
23
|
+
|
|
24
|
+
<picture>
|
|
25
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/alexb-joat/trioditis/main/docs/img/routing-dark.svg">
|
|
26
|
+
<img alt="Clients send requests to Trioditis on 127.0.0.1:8787. It checks privacy, picks a route, filters models and calls with fallback to local, cloud or legacy models." src="https://raw.githubusercontent.com/alexb-joat/trioditis/main/docs/img/routing-light.svg" width="880">
|
|
27
|
+
</picture>
|
|
28
|
+
|
|
29
|
+
## Why Trioditis
|
|
30
|
+
|
|
31
|
+
| Private by default | Cost-aware | Small and auditable | Explains itself |
|
|
32
|
+
|---|---|---|---|
|
|
33
|
+
| Prompts with emails, cards, keys or passwords go to a local model or are rejected. They are never sent to a cloud API by accident. | Daily and monthly caps with automatic local fallback, plus per-provider limits and live spend. | About 3,400 lines and four hash-pinned dependencies. No LiteLLM, no LangChain, no telemetry. | Every response names its route and model. A log records the reasons, with no prompt text. |
|
|
34
|
+
|
|
35
|
+
## Quick start
|
|
36
|
+
|
|
37
|
+
**1.** Install [Ollama](https://ollama.com/download) and pull a couple of local models (run it natively, not in Docker):
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
ollama pull llama3.2:3b && ollama pull qwen2.5-coder:7b && ollama pull nomic-embed-text
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**2.** Install Trioditis with [pipx](https://pipx.pypa.io/stable/installation/) and give it a folder:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pipx install trioditis
|
|
47
|
+
trioditis init --dir ~/trioditis # writes config.yaml and .env there (owner-only); never overwrites
|
|
48
|
+
cd ~/trioditis # config.yaml, .env and logs/ are read from the current folder
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
<details>
|
|
52
|
+
<summary>Or from a clone, with every package checked against the lockfile's hashes</summary>
|
|
53
|
+
|
|
54
|
+
`pipx` installs the newest release of every dependency on the day you run it. The clone installs the exact versions in `requirements.txt` and refuses any file whose hash differs. From a clone, write `python -m trioditis` wherever these pages say `trioditis`.
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
git clone https://github.com/alexb-joat/trioditis.git && cd trioditis
|
|
58
|
+
python3 -m venv .venv && source .venv/bin/activate # Debian/Ubuntu: sudo apt install python3-venv
|
|
59
|
+
pip install --require-hashes --no-deps -r requirements.txt
|
|
60
|
+
python -m trioditis init
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Windows (PowerShell):
|
|
64
|
+
|
|
65
|
+
```powershell
|
|
66
|
+
git clone https://github.com/alexb-joat/trioditis.git; cd trioditis
|
|
67
|
+
py -3 -m venv .venv; .\.venv\Scripts\Activate.ps1
|
|
68
|
+
pip install --require-hashes --no-deps -r requirements.txt
|
|
69
|
+
python -m trioditis init
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
</details>
|
|
73
|
+
|
|
74
|
+
**3.** Check and run, the same on every system:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
trioditis check # which local models answer, which cloud keys are set
|
|
78
|
+
trioditis serve # http://127.0.0.1:8787/v1, localhost only
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
With the models from step 1 pulled and no cloud keys yet, `check` prints:
|
|
82
|
+
|
|
83
|
+
```text
|
|
84
|
+
config OK: 9 models, 4 routes
|
|
85
|
+
local-general local ready
|
|
86
|
+
local-coder local ready
|
|
87
|
+
local-reasoner local not on this server: ollama pull deepseek-r1:8b
|
|
88
|
+
local-vision local not on this server: ollama pull llava:7b
|
|
89
|
+
legacy-mistral local not on this server: ollama pull mistral:7b
|
|
90
|
+
claude-sonnet cloud missing $ANTHROPIC_API_KEY
|
|
91
|
+
claude-haiku cloud missing $ANTHROPIC_API_KEY
|
|
92
|
+
claude-opus cloud missing $ANTHROPIC_API_KEY
|
|
93
|
+
gpt cloud missing $OPENAI_API_KEY
|
|
94
|
+
2 of 9 models confirmed ready; cloud providers are never contacted
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**4.** Try it:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
trioditis explain "Why does my python function raise KeyError?" # shows the decision, calls no model
|
|
101
|
+
curl -s http://127.0.0.1:8787/v1/chat/completions -H 'Content-Type: application/json' \
|
|
102
|
+
-d '{"model":"auto","messages":[{"role":"user","content":"Write a regex for UK postcodes"}]}'
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Cloud models are optional: add `ANTHROPIC_API_KEY`, `OPENAI_API_KEY` or `OPENROUTER_API_KEY` to `.env` and they join the routes. A cloud provider without a key is simply skipped.
|
|
106
|
+
|
|
107
|
+
## How a request is routed
|
|
108
|
+
|
|
109
|
+
1. **Privacy check.** Built-in detectors (emails, card numbers, IBANs, API keys, private keys, passwords) plus your own keywords and regexes. A hit restricts the request to local models; if none can serve it, it is rejected rather than leaked.
|
|
110
|
+
2. **Pick a route** (`code`, `reasoning`, `writing`, `quick`, or your own): keyword and pattern signals, the names of the tools a coding agent offers (`tool_keywords`), then similarity to each route's example prompts using a *local* embedding model, else the default route. An agent's later turns stay on the route its task started on.
|
|
111
|
+
3. **Filter and order models** by what the request needs: vision, tool calling, context length, and whether the provider's key is present. A route can keep its listed order or `prefer: cheapest` (local first, then by this request's estimated cost) or `fastest` (median latency of recent calls); a provider that just answered "too many requests" is tried last until its wait is over.
|
|
112
|
+
4. **Call the first model and fall back** on outage, rate limit, missing model or spending cap. If no model of the route fits, only another route's model or a local one is used, never a model you call by name only.
|
|
113
|
+
5. **Pin the session.** Requests with the same `X-Session-Id` (or `user`) stay on one route, so agents do not switch models mid-task.
|
|
114
|
+
|
|
115
|
+
Responses carry `X-Trioditis-Route` and `X-Trioditis-Model`. `POST /v1/route` or `trioditis explain "…"` shows the decision and reasons without calling a model. Clients can bypass routing with `model: "route/code"` (force a route), `model: "legacy-mistral"` (call a model directly) or the provider's own id, such as `model: "claude-haiku-5-5"`, which pins an exact version; `X-Trioditis-Upstream-Model` names the id the provider was sent.
|
|
116
|
+
|
|
117
|
+
## Connect your tools
|
|
118
|
+
|
|
119
|
+
| Client | Settings |
|
|
120
|
+
|---|---|
|
|
121
|
+
| **Cline** (VS Code) | Provider *OpenAI Compatible*, base URL `http://127.0.0.1:8787/v1`, model `route/code`. Full recipe, including local-model context: [docs/vscode-cline.md](https://github.com/alexb-joat/trioditis/blob/main/docs/vscode-cline.md) |
|
|
122
|
+
| **Open WebUI, Odysseus, chat UIs** | Add the endpoint `http://127.0.0.1:8787/v1` and pick model `auto` |
|
|
123
|
+
| **OpenAI SDKs** | `OpenAI(base_url="http://127.0.0.1:8787/v1", api_key="<your TRIODITIS_API_KEY>")` |
|
|
124
|
+
|
|
125
|
+
Trioditis speaks the OpenAI chat-completions format, which most tools support. Clients that only speak a provider's native API (for example Claude Code) cannot use it yet.
|
|
126
|
+
|
|
127
|
+
## Spending and limits
|
|
128
|
+
|
|
129
|
+
Every cloud call is checked against your daily and monthly caps first (defaults: $1 a day, $15 a month). Over a cap, the route's local model answers instead, so you get an answer, not a bill. `trioditis usage --since 7d --by client` shows what each tool spent. Per-provider caps, provider rate-limit and balance readouts, and call history are in [docs/spending-and-limits.md](https://github.com/alexb-joat/trioditis/blob/main/docs/spending-and-limits.md). `trioditis hardware` suggests which local models fit your computer ([docs/hardware.md](https://github.com/alexb-joat/trioditis/blob/main/docs/hardware.md)).
|
|
130
|
+
|
|
131
|
+
## Staying current
|
|
132
|
+
|
|
133
|
+
Each call records why its answer ended, retries and cache use, so `trioditis usage --outcomes` shows each model's error, cut-off and retry rates with 95% intervals. A daily `trioditis refit` adds latency and drift charts, and, given a model facts file, names configured models that are deprecated or about to retire and lists new ones worth testing. It changes nothing on its own: [docs/staying-current.md](https://github.com/alexb-joat/trioditis/blob/main/docs/staying-current.md). On a subscription, `trioditis plan import --claude-code` and `usage --plan` show tokens per model and effort next to how far the plan's meters rose, from counts only, never text ([Plan usage](https://github.com/alexb-joat/trioditis/blob/main/docs/spending-and-limits.md#plan-usage)).
|
|
134
|
+
|
|
135
|
+
## Security at a glance
|
|
136
|
+
|
|
137
|
+
| Risk | Control |
|
|
138
|
+
|---|---|
|
|
139
|
+
| Hijacked dependency | Four direct dependencies, hash-pinned (`--require-hashes`), `pip-audit` in CI, Dependabot with a 7-day hold |
|
|
140
|
+
| Router exposed to the network | Binds `127.0.0.1`; refuses any other bind without a 32+ character API key |
|
|
141
|
+
| A web page in your browser reaching the router | Foreign `Host` (DNS rebinding) or `Origin` gets 403 and a log line; POST bodies must be JSON |
|
|
142
|
+
| Sensitive data sent to the cloud | Detectors force local-only routing or reject |
|
|
143
|
+
| Key and prompt leakage | Keys only in environment variables; the client's key is never forwarded; logs hold metadata and a hash, not prompt text |
|
|
144
|
+
| Open proxy and SSRF | Upstream URLs come only from `config.yaml`; no redirects followed |
|
|
145
|
+
| Supply chain | Actions pinned to commit SHAs, read-only workflows, CodeQL and dependency review on every pull request |
|
|
146
|
+
|
|
147
|
+
Report a vulnerability privately via [SECURITY.md](https://github.com/alexb-joat/trioditis/blob/main/SECURITY.md). The full design and its limits are in [docs/security-model.md](https://github.com/alexb-joat/trioditis/blob/main/docs/security-model.md).
|
|
148
|
+
|
|
149
|
+
## Known limits
|
|
150
|
+
|
|
151
|
+
- Fallback happens before an answer starts. If a streamed answer breaks halfway, Trioditis does not retry it on another model.
|
|
152
|
+
- Every provider is called at its OpenAI-style `/chat/completions` address. There are no native Anthropic or Gemini adapters.
|
|
153
|
+
- No prompt caching for Claude models. Trioditis calls Anthropic at its OpenAI-compatible endpoint, and Anthropic's page for it says "Prompt caching is not supported" ([source](https://platform.claude.com/docs/en/cli-sdks-libraries/libraries/openai-sdk), read 13 November 2026). For every provider, Trioditis adds no caching settings of its own and records cached tokens only when the provider reports them.
|
|
154
|
+
- On Windows, Trioditis does not warn about a `.env` that other users can read, and it does not set the mode of the metrics database. Keep the folder in your user profile ([security model](https://github.com/alexb-joat/trioditis/blob/main/docs/security-model.md)).
|
|
155
|
+
- Only the Linux installer sets up the daily `refit` and `plan import`. On macOS and Windows, run them by hand.
|
|
156
|
+
- CI does not build the Docker image.
|
|
157
|
+
- The router picks the right route about two times in three. On 315 held-out prompts it chose the labelled route for 205 (65.1%, 95% interval 59.7–70.1%) on the example config with local embeddings on, and for 116 (36.8%) with keywords alone. Most misses (65 of 110) go to `quick`, the default route, and a miss still gets an answer. The prompts are synthetic and in English; measured 13 November 2026 ([method, data and limits](https://github.com/alexb-joat/trioditis/blob/main/docs/route-accuracy.md)). Your own routes give a different number: `trioditis eval-routes` measures it.
|
|
158
|
+
|
|
159
|
+
## Platforms
|
|
160
|
+
|
|
161
|
+
| Linux (systemd) | macOS (launchd) | Windows 10/11 (Scheduled Task) |
|
|
162
|
+
|---|---|---|
|
|
163
|
+
| Python 3.11+, CI | Python 3.11+, CI | Python 3.11+, CI |
|
|
164
|
+
|
|
165
|
+
CI runs the unit tests and a real-server smoke test on all three, on Python 3.11, 3.12 and 3.13. To run in the background or in a hardened container, see [docs/deployment.md](https://github.com/alexb-joat/trioditis/blob/main/docs/deployment.md).
|
|
166
|
+
|
|
167
|
+
## Documentation
|
|
168
|
+
|
|
169
|
+
- [VS Code with Cline](https://github.com/alexb-joat/trioditis/blob/main/docs/vscode-cline.md) · [Spending caps and provider limits](https://github.com/alexb-joat/trioditis/blob/main/docs/spending-and-limits.md) · [Hardware suggestions](https://github.com/alexb-joat/trioditis/blob/main/docs/hardware.md)
|
|
170
|
+
- [Background service and Docker](https://github.com/alexb-joat/trioditis/blob/main/docs/deployment.md) · [Security model](https://github.com/alexb-joat/trioditis/blob/main/docs/security-model.md) · [`config.example.yaml`](https://github.com/alexb-joat/trioditis/blob/main/config.example.yaml) (providers, models, routes, privacy, limits, logging)
|
|
171
|
+
|
|
172
|
+
**See every decision live:** [Hlidskjalv](https://github.com/alexb-joat/hlidskjalv) reads the decision log (route, model, tokens, never prompts) and shows it next to your files, commands and Git history.
|
|
173
|
+
|
|
174
|
+
<details>
|
|
175
|
+
<summary>Roadmap</summary>
|
|
176
|
+
|
|
177
|
+
- [x] Daily and monthly spending caps with local fallback
|
|
178
|
+
- [x] Hardware detection and local-model suggestions
|
|
179
|
+
- [x] Routes can prefer the cheapest or fastest model
|
|
180
|
+
- [x] Outcome rates per model, a plan-usage ledger and a daily refit
|
|
181
|
+
- [ ] A small local LLM as an optional classifier for ambiguous prompts
|
|
182
|
+
- [ ] Learn from feedback: per-route model scores from thumbs up and down
|
|
183
|
+
- [ ] Cost and latency budgets per route
|
|
184
|
+
- [ ] Native Anthropic and Gemini adapters (today: their OpenAI-compatible endpoints)
|
|
185
|
+
- [ ] Per-user keys and rate limits for team use
|
|
186
|
+
|
|
187
|
+
</details>
|
|
188
|
+
|
|
189
|
+
## Contributing
|
|
190
|
+
|
|
191
|
+
Pull requests are welcome; see [CONTRIBUTING.md](https://github.com/alexb-joat/trioditis/blob/main/CONTRIBUTING.md). Run the checks with `pip install --require-hashes --no-deps -r requirements-dev.txt && python -m pytest -q`. If Trioditis saves you money or time, the **Sponsor** button at the top of the repository supports it.
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
[MIT](https://github.com/alexb-joat/trioditis/blob/main/LICENSE). Not affiliated with Anthropic, OpenAI, Ollama, OpenRouter or any model provider; their names refer only to their public APIs.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"] # SPDX license string (PEP 639)
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "trioditis"
|
|
7
|
+
dynamic = ["version"] # from trioditis/__init__.py, the one place it is written
|
|
8
|
+
description = "Security-first LLM router: one OpenAI-compatible endpoint that picks the right model per prompt."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"fastapi",
|
|
14
|
+
"uvicorn",
|
|
15
|
+
"httpx",
|
|
16
|
+
"pyyaml",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Homepage = "https://github.com/alexb-joat/trioditis"
|
|
21
|
+
Issues = "https://github.com/alexb-joat/trioditis/issues"
|
|
22
|
+
Security = "https://github.com/alexb-joat/trioditis/security/policy"
|
|
23
|
+
|
|
24
|
+
[project.scripts]
|
|
25
|
+
trioditis = "trioditis.__main__:main"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools]
|
|
28
|
+
packages = ["trioditis", "trioditis.templates", "trioditis_metrics"]
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.dynamic]
|
|
31
|
+
version = { attr = "trioditis.__version__" }
|
|
32
|
+
|
|
33
|
+
[tool.setuptools.package-data]
|
|
34
|
+
"trioditis.templates" = ["config.example.yaml", "env.example"] # for `trioditis init`
|
|
35
|
+
|
|
36
|
+
[tool.pytest.ini_options]
|
|
37
|
+
testpaths = ["tests"]
|
|
38
|
+
|
|
39
|
+
[tool.ruff]
|
|
40
|
+
line-length = 160 # the code's own width: long lines here are messages and tables, not deep nesting
|
|
41
|
+
extend-exclude = [".venv"]
|
|
42
|
+
|
|
43
|
+
[tool.ruff.lint]
|
|
44
|
+
# pyflakes, pycodestyle, import order, bugbear, pyupgrade and the bandit security rules.
|
|
45
|
+
select = ["E", "F", "W", "I", "B", "UP", "S"]
|
|
46
|
+
# S603 flags every subprocess call; the checks that matter stay on: shell=True (S602, S605)
|
|
47
|
+
# and a program found through PATH (S607). Trioditis runs fixed programs by absolute path.
|
|
48
|
+
ignore = ["S603"]
|
|
49
|
+
|
|
50
|
+
[tool.ruff.lint.per-file-ignores]
|
|
51
|
+
# Tests assert, write temp files, use fake keys and feed the bind check 0.0.0.0 on purpose;
|
|
52
|
+
# short setup lines may share a line; simulations use seeded, non-cryptographic randomness.
|
|
53
|
+
"tests/**" = ["S101", "S104", "S105", "S106", "S108", "S310", "S311", "S607", "E701", "E702"]
|
|
54
|
+
"scripts/**" = ["S101", "S104", "S310", "S607"]
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import subprocess
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
|
|
10
|
+
import effort_trial as t # noqa: E402
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_command_is_plain_chat_on_the_subscription():
|
|
14
|
+
cmd = t.claude_cmd("claude-opus-5-5", "max")
|
|
15
|
+
assert cmd[:2] == ["claude", "-p"] and "--bare" not in cmd # --bare means API-key billing
|
|
16
|
+
assert cmd[cmd.index("--effort") + 1] == "max" and cmd[cmd.index("--tools") + 1] == ""
|
|
17
|
+
assert cmd[cmd.index("--model") + 1] == "claude-opus-5-5"
|
|
18
|
+
with pytest.raises(ValueError):
|
|
19
|
+
t.claude_cmd("claude-opus-5-5", "ultra")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
WORD = {"high": "alpha", "max": "omega"}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _fake_runner(out_by_effort):
|
|
26
|
+
def run(cmd, **kw):
|
|
27
|
+
effort = cmd[cmd.index("--effort") + 1]
|
|
28
|
+
doc = {"session_id": "s-" + effort, "is_error": False, "result": "reply " + WORD[effort],
|
|
29
|
+
"usage": {"input_tokens": 5, "output_tokens": out_by_effort[effort] * len(kw["input"]),
|
|
30
|
+
"output_tokens_details": {"thinking_tokens": 1}}}
|
|
31
|
+
return subprocess.CompletedProcess(cmd, 0, json.dumps(doc), "")
|
|
32
|
+
return run
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _meter(path, weekly):
|
|
36
|
+
path.write_text(json.dumps({"history": [{"at": 1_800_000_000 + i, "limits": [
|
|
37
|
+
{"kind": "weekly_all", "percent": w}, {"kind": "session", "percent": w * 2}]} for i, w in enumerate(weekly)]}))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _args(tmp_path, **kw):
|
|
41
|
+
prompts = tmp_path / "prompts.json"
|
|
42
|
+
prompts.write_text(json.dumps({"prompts": [{"id": "a", "text": "x"}, {"id": "b", "text": "xx"}]}))
|
|
43
|
+
import argparse
|
|
44
|
+
base = dict(prompts=str(prompts), out=str(tmp_path / "out"), efforts="high,max", model="m",
|
|
45
|
+
meter_file=str(tmp_path / "meter.json"), gap=0, max_weekly_rise=3.0, timeout=5, dry_run=False)
|
|
46
|
+
base.update(kw)
|
|
47
|
+
return argparse.Namespace(**base)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_run_report_and_blind(tmp_path):
|
|
51
|
+
_meter(tmp_path / "meter.json", [10])
|
|
52
|
+
a = _args(tmp_path)
|
|
53
|
+
assert t.cmd_run(a, runner=_fake_runner({"high": 100, "max": 300}), sleep=lambda s: None) == 0
|
|
54
|
+
out = Path(a.out)
|
|
55
|
+
s = t.summarise(t.load_runs(out), json.loads((out / "batches.json").read_text()))
|
|
56
|
+
assert s["output_tokens"] == {"a": {"high": 100, "max": 300}, "b": {"high": 200, "max": 600}}
|
|
57
|
+
assert s["ratios"]["max/high"]["ratio"] == pytest.approx(3.0) and s["ratios"]["max/high"]["n"] == 2
|
|
58
|
+
if os.name == "posix": # Windows has no mode bits; the folder's permissions apply there
|
|
59
|
+
assert (out / "runs" / "max-a.json").stat().st_mode & 0o077 == 0 # answers are private
|
|
60
|
+
import argparse
|
|
61
|
+
assert t.cmd_blind(argparse.Namespace(out=a.out, prompts=a.prompts, seed=1)) == 0
|
|
62
|
+
blind = (out / "blind.json").read_text()
|
|
63
|
+
assert "max" not in blind and "high" not in blind and "300" not in blind # nothing gives the effort away
|
|
64
|
+
key = json.loads((out / "key.json").read_text())
|
|
65
|
+
items = json.loads(blind)["items"]
|
|
66
|
+
for it in items:
|
|
67
|
+
for ans in it["answers"]:
|
|
68
|
+
assert ans["text"] == "reply " + WORD[key[it["id"]][ans["letter"]]]
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_the_weekly_guard_stops_before_the_next_batch(tmp_path):
|
|
72
|
+
meter = tmp_path / "meter.json"
|
|
73
|
+
_meter(meter, [10])
|
|
74
|
+
a = _args(tmp_path)
|
|
75
|
+
calls = []
|
|
76
|
+
|
|
77
|
+
def runner(cmd, **kw):
|
|
78
|
+
calls.append(cmd[cmd.index("--effort") + 1])
|
|
79
|
+
_meter(meter, [10, 14]) # the first batch pushed the weekly meter 4 points
|
|
80
|
+
return _fake_runner({"high": 1, "max": 1})(cmd, **kw)
|
|
81
|
+
assert t.cmd_run(a, runner=runner, sleep=lambda s: None) == 3
|
|
82
|
+
assert set(calls) == {"high"}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_a_failed_run_is_kept_as_an_error_and_redone(tmp_path):
|
|
86
|
+
_meter(tmp_path / "meter.json", [10])
|
|
87
|
+
a = _args(tmp_path, efforts="high")
|
|
88
|
+
|
|
89
|
+
def boom(cmd, **kw):
|
|
90
|
+
raise subprocess.TimeoutExpired(cmd, 5)
|
|
91
|
+
t.cmd_run(a, runner=boom, sleep=lambda s: None)
|
|
92
|
+
assert "timed out" in json.loads((Path(a.out) / "runs" / "high-a.json").read_text())["error"]
|
|
93
|
+
t.cmd_run(a, runner=_fake_runner({"high": 7}), sleep=lambda s: None)
|
|
94
|
+
assert json.loads((Path(a.out) / "runs" / "high-a.json").read_text())["error"] is None
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def test_ledger_check_matches_runs_by_salted_session_and_model(tmp_path):
|
|
98
|
+
from trioditis.plan import _hash
|
|
99
|
+
from trioditis_metrics.db import MetricsDB
|
|
100
|
+
from trioditis_metrics.plan import merge_events
|
|
101
|
+
db = MetricsDB(tmp_path / "m.db")
|
|
102
|
+
fp = lambda sid: _hash("salt", "session\0" + sid, 16) # noqa: E731
|
|
103
|
+
merge_events(db.conn, [
|
|
104
|
+
{"id": "1", "at": 1.0, "source": "claude-code", "tokens_measured": 1, "agent": 0, "model": "m", "tokens_out": 300, "thread_fp": fp("s-max")},
|
|
105
|
+
# side call: left out
|
|
106
|
+
{"id": "2", "at": 1.0, "source": "claude-code", "tokens_measured": 1, "agent": 0, "model": "haiku",
|
|
107
|
+
"tokens_out": 7, "thread_fp": fp("s-max")},
|
|
108
|
+
{"id": "3", "at": 1.0, "source": "claude-code", "tokens_measured": 1, "agent": 0, "model": "m", "tokens_out": 90, "thread_fp": fp("s-high")}])
|
|
109
|
+
run = lambda e, sid, out: {"prompt": "a", "effort": e, "model": "m", "session_id": sid, # noqa: E731
|
|
110
|
+
"error": None, "usage": {"output_tokens": out}}
|
|
111
|
+
res = t.ledger_check([run("max", "s-max", 300), run("high", "s-high", 100), run("xhigh", "s-x", 5),
|
|
112
|
+
{**run("low", "s-low", 1), "error": "timed out"}], db.conn, "salt")
|
|
113
|
+
assert res["matched"] == 1 and res["missing"] == ["xhigh-a"]
|
|
114
|
+
assert res["differ"] == [{"run": "high-a", "run_out": 100, "ledger_out": 90}]
|
|
115
|
+
db.close()
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def test_a_meter_reading_older_than_the_last_batch_is_flagged():
|
|
119
|
+
b = lambda e, start, end, at: {"effort": e, "start": start, "end": end, # noqa: E731
|
|
120
|
+
"meter_before": {"session": {"at": at, "percent": 1.0}}}
|
|
121
|
+
s = t.summarise([], [b("high", 100, 200, 90), b("xhigh", 300, 400, 150), b("max", 500, 600, 450)])
|
|
122
|
+
assert [m["stale"] for m in s["meter"]] == [False, True, False]
|
|
123
|
+
assert s["meter"][1]["reading_age_min"] == 2.5
|