tiiny-router-pi 0.0.0-stage → 0.0.2

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.
package/README.md CHANGED
@@ -1,3 +1,745 @@
1
- # Temporary Holding Version
1
+ # tiiny router
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **`tiiny` + `pi`**
4
+
5
+ Forked from
6
+ [garciacionet-alt/AI_Articles](https://github.com/garciacionet-alt/AI_Articles/tree/main/tiiny-model-routing),
7
+ but with `pi` harness instead of `hermes`.
8
+
9
+ This is a [pi](https://pi.dev) extension that sends each task to the best model
10
+ on a Tiiny device. For example, a survey goes to a research model, a bulk
11
+ classification goes to a small fast model, a refactor goes to a coding
12
+ model, and a task whose shape is unclear goes to the generalist.
13
+
14
+
15
+ <details><summary><h2>Contents</h2></summary>
16
+
17
+ <!-- toc -->
18
+
19
+ - [Install](#install)
20
+ * [Dry Run](#dry-run)
21
+ - [Uninstall](#uninstall)
22
+ - [Loops](#loops)
23
+ - [Use](#use)
24
+ * [Interactive](#interactive)
25
+ * [One-off](#one-off)
26
+ * [With Redeye](#with-redeye)
27
+ * [Delegating with subagents](#delegating-with-subagents)
28
+ * [Batches: plan, reserve, load, release](#batches-plan-reserve-load-release)
29
+ - [Commands](#commands)
30
+ * [`npx pirouter`](#npx-pirouter)
31
+ * [`pirouter install`](#pirouter-install)
32
+ * [`pirouter uninstall`](#pirouter-uninstall)
33
+ * [`pirouter check`](#pirouter-check)
34
+ * [`pirouter status`](#pirouter-status)
35
+ * [`pirouter plan`](#pirouter-plan)
36
+ * [`pirouter reserve`](#pirouter-reserve)
37
+ * [`pirouter load`](#pirouter-load)
38
+ * [`pirouter ensure`](#pirouter-ensure)
39
+ * [`pirouter release`](#pirouter-release)
40
+ * [`pirouter catalog`](#pirouter-catalog)
41
+ * [`pirouter image`](#pirouter-image)
42
+ - [Reasoning mode](#reasoning-mode)
43
+ - [Checking a route before spending tokens](#checking-a-route-before-spending-tokens)
44
+ - [The tier table](#the-tier-table)
45
+ - [Configuration](#configuration)
46
+ - [Operating notes](#operating-notes)
47
+ * [One task at a time](#one-task-at-a-time)
48
+ * [A 404 is residency, not a bad id](#a-404-is-residency-not-a-bad-id)
49
+ * [Changes need a reload](#changes-need-a-reload)
50
+ - [Layout](#layout)
51
+ - [Development](#development)
52
+ * [Tests](#tests)
53
+ - [Credits](#credits)
54
+
55
+ <!-- tocstop -->
56
+
57
+ </details>
58
+
59
+
60
+ ## Install
61
+
62
+ Install locally with npm, then run the setup script.
63
+
64
+ ```bash
65
+ npm i -D tiiny-router
66
+ ```
67
+
68
+ Installing is local to your current repo only, not global or user-scope.
69
+
70
+ After installing via `npm`, run the install script, `npx pirouter install`.
71
+ The script adds symbolic links, and copies a folder
72
+ to the project's `.pi/` directory, so pi can load the router's extension,
73
+ the router's agents, and router skills in the project.
74
+
75
+ To remove it, run `npx pirouter uninstall`, then `npm rm tiiny-router`.
76
+ See [Uninstall](#uninstall). Uninstall leaves `~/.pi/agent/tiiny` in
77
+ place. Delete it yourself if you no longer use the controller.
78
+
79
+ ### Dry Run
80
+
81
+ Print what would change without changing anything:
82
+
83
+ ```bash
84
+ npx pirouter install --dry-run
85
+ ```
86
+
87
+ Then install the extension, agents, and skill:
88
+
89
+ ```bash
90
+ npx pirouter install
91
+ ```
92
+
93
+ Run these from the project root. `npx pirouter install` stops with an error
94
+ if the current directory has no `package.json`. It runs `scripts/install.sh`,
95
+ which writes inside the project's `.pi/` directory. It makes these changes,
96
+ in this order:
97
+
98
+ 1. Links the package as a pi extension:
99
+ `.pi/extensions/tiiny-router -> node_modules/tiiny-router`. pi reads
100
+ the `pi.extensions` field of the package's `package.json` to find
101
+ `index.ts`, which registers the `tiiny-router/*` virtual models.
102
+ 2. Links each agent definition into `.pi/agents/`: `tiiny-browser.md`,
103
+ `tiiny-coder.md`, `tiiny-drafter.md`, `tiiny-indexer.md`,
104
+ `tiiny-researcher.md` and `tiiny-worker.md`. These are for the
105
+ subagent tool (see [Delegating with subagents](#delegating-with-subagents)).
106
+ 3. Links the skill folder:
107
+ `.pi/skills/tiiny-routing -> node_modules/tiiny-router/skill`.
108
+ 4. Copies `index.ts` and `agents.ts` from pi's example `subagent`
109
+ extension into `.pi/extensions/subagent`, then edits the copy to set
110
+ `MAX_CONCURRENCY = 1` (see [One task at a time](#one-task-at-a-time)).
111
+ It looks for pi's package in the project's `node_modules`, then in
112
+ this package's own `node_modules`, then in the global npm root. This is
113
+ a copy, not a link, so later pi updates do not change it.
114
+
115
+ Steps 1 to 3 create symbolic links. Because they point into
116
+ `node_modules`, upgrading `tiiny-router` with npm updates them too.
117
+
118
+ It doesn't overwrite files or directories. If a destination exists
119
+ and is not a symbolic link, it prints `skip:` and moves on. If it is a
120
+ symbolic link, it is replaced, so running `install` again is safe and
121
+ re-points old links at the current package. Step 4 is skipped if
122
+ `.pi/extensions/subagent` exists in any form. In that case, set
123
+ `MAX_CONCURRENCY = 1` in your own subagent extension. Step 4 is also
124
+ skipped, with a message, if pi's example cannot be found.
125
+
126
+ Each change prints one line: `link:`, `copy:` or `skip:`. With
127
+ `--dry-run`, it changes nothing, but prints each command it would run
128
+ (`would: ...`) before the same `link:` and `copy:` lines.
129
+
130
+ It does not edit `.pi/settings.json`, `~/.pi/agent/models.json` or any
131
+ file outside `.pi/`.
132
+
133
+ >
134
+ > [!NOTE]
135
+ > **The project folder must be "trusted" in `pi` before starting**.
136
+ >
137
+
138
+ The `tiiny-*` agents are project agents, which the subagent tool does
139
+ not read by default. See
140
+ [Project agents and `agentScope`](#project-agents-and-agentscope).
141
+
142
+ Everything else, the controller and the route checker, is a
143
+ `pirouter` subcommand too. Run `npx pirouter` with no arguments for
144
+ the list.
145
+
146
+ To try the router for one run without installing anything:
147
+
148
+ ```bash
149
+ pi -e npm:tiiny-router --model tiiny-router/auto
150
+ ```
151
+
152
+ ## Uninstall
153
+
154
+ From the project directory, preview the removal, then uninstall:
155
+
156
+ ```bash
157
+ npx pirouter uninstall --dry-run
158
+ npx pirouter uninstall
159
+ ```
160
+
161
+ This removes router entries from global `~/.pi/agent/settings.json` and
162
+ local `.pi/settings.json`, including npm sources and local package paths.
163
+ It also removes links to the router's extension, agents and skill from
164
+ both scopes. If `PI_CODING_AGENT_DIR` is set, that directory is used for
165
+ global config instead. Other projects' local config is unchanged.
166
+
167
+ Unrelated settings and real files are preserved. The copied subagent
168
+ files are removed only when they match pi's installed example with
169
+ `MAX_CONCURRENCY = 1`. Modified copies, or copies whose source example is
170
+ unavailable, are skipped; remove those manually if you no longer need
171
+ them. Running uninstall again is safe, and no `package.json` is required.
172
+
173
+ Restart pi or run `/reload`. To also remove the npm dependency, run:
174
+
175
+ ```bash
176
+ npm rm tiiny-router
177
+ ```
178
+
179
+
180
+ ## Loops
181
+
182
+ For an orchestrator such as
183
+ [redeye](https://github.com/nichoth/redeye) that runs `pi` in fresh `git`
184
+ worktrees, add the package to the project's `pi` settings instead:
185
+
186
+ ```bash
187
+ npm i -D tiiny-router
188
+ ```
189
+
190
+ Commit `.pi/settings.json`:
191
+
192
+ ```json
193
+ { "packages": ["../node_modules/tiiny-router"] }
194
+ ```
195
+
196
+ The path is relative to `.pi/`, so each worktree loads its own installed
197
+ copy. Then use the `auto` model, which picks a tier for each message:
198
+
199
+ ```sh
200
+ npx redeye pi -m tiiny-router/auto --concurrency 1
201
+ ```
202
+
203
+ To pin every task to one tier instead, name it, as in
204
+ `-m tiiny-router/code`.
205
+
206
+ ## Use
207
+
208
+ ### Interactive
209
+
210
+ ```bash
211
+ pi --model tiiny-router/auto
212
+ ```
213
+
214
+ The footer shows the selection and the routed model (for example
215
+ `auto -> Qwen3.6-35B-A3B-turbo`), and the status line shows the route
216
+ label, such as `tiiny: draft->generalist [think:off]`. In `auto`, every
217
+ new message is routed again. If the tier and model have not changed, the
218
+ session stays on the same model.
219
+
220
+ ### One-off
221
+
222
+ ```bash
223
+ pi --model tiiny-router/index:off -p "Classify every file under ./notes by topic"
224
+ ```
225
+
226
+ ### With Redeye
227
+
228
+ Use this with the [`redeye` CLI](https://github.com/nichoth/redeye) tool.
229
+ See [the redeye docs](https://github.com/nichoth/redeye#local-model-with-pi)
230
+ for more examples.
231
+
232
+ ### Delegating with subagents
233
+
234
+ The installed agents each pin a tier:
235
+
236
+ | Agent | Model |
237
+ |---|---|
238
+ | `tiiny-researcher` | `tiiny-router/research:high` |
239
+ | `tiiny-indexer` | `tiiny-router/index:off` |
240
+ | `tiiny-coder` | `tiiny-router/code` |
241
+ | `tiiny-browser` | `tiiny-router/browser` |
242
+ | `tiiny-drafter` | `tiiny-router/draft:off` |
243
+ | `tiiny-worker` | `tiiny-router/auto` |
244
+
245
+ Ask for them by name from any pi session, including one running on a
246
+ cloud model:
247
+
248
+ ```
249
+ Use tiiny-indexer to classify every file under ./vault by topic,
250
+ then use tiiny-drafter to summarise the result in five bullets.
251
+ ```
252
+
253
+ Choosing an agent is how you override the inferred tier. Choose by what
254
+ the task *does*: "read these logs and tell me what went wrong" is a
255
+ drafting task, and a survey *about* code is research. A mistyped tier
256
+ fails (`Virtual model tiiny-router/indx is not registered`) and is
257
+ never quietly sent to the generalist.
258
+
259
+ #### Project agents and `agentScope`
260
+
261
+ The subagent tool looks for agents in two places:
262
+
263
+ - user agents, in `~/.pi/agent/agents/`, shared by every project
264
+ - project agents, in the nearest `.pi/agents/` above the working
265
+ directory
266
+
267
+ `pirouter install` puts the `tiiny-*` agents in the project's
268
+ `.pi/agents/`, so they are project agents. The tool's `agentScope`
269
+ parameter chooses where it looks:
270
+
271
+ | `agentScope` | Agents it can run |
272
+ |---|---|
273
+ | `"user"` (default) | user agents only |
274
+ | `"project"` | project agents only |
275
+ | `"both"` | both, and a project agent wins over a user agent with the same name |
276
+
277
+ With the default, the `tiiny-*` agents cannot be found. The model sets
278
+ the tool's parameters, so say in your request that it should use project
279
+ agents, for example:
280
+
281
+ ```
282
+ Use the subagent tool with agentScope "both" to run tiiny-coder on
283
+ the failing test in test/route.test.ts.
284
+ ```
285
+
286
+ Project agents come from the repository, so a cloned repo could add
287
+ its own `.pi/agents/` with any instructions. For that reason, pi asks
288
+ before it runs one (`Run project-local agents?`), and lists each agent
289
+ and its file. Approve to run it, or decline to cancel the call.
290
+
291
+ pi does not ask if the project is trusted and the agents are in its own
292
+ `.pi/agents/`, which is the case after a normal install. It also does
293
+ not ask in a run without a UI, such as `pi -p`. A caller can turn the
294
+ prompt off with `confirmProjectAgents: false`.
295
+
296
+ ### Batches: plan, reserve, load, release
297
+
298
+ The router uses whatever is loaded. To actually run a batch on its
299
+ specialists, have the controller load them first:
300
+
301
+ ```bash
302
+ npx pirouter plan --owner "$USER" --task "vault cleanup" \
303
+ --goal "index every file under the vault" \
304
+ --goal "summarise the meeting notes into five bullets"
305
+ ```
306
+
307
+ ```bash
308
+ npx pirouter reserve --owner "$USER" --task "vault cleanup"
309
+ ```
310
+
311
+ ```bash
312
+ npx pirouter load --wave 1
313
+ ```
314
+
315
+ Then delegate the wave's goals, using **the same goal text** you
316
+ planned, one at a time. Then restore the device and drop the lock:
317
+
318
+ ```bash
319
+ npx pirouter release
320
+ ```
321
+
322
+ `plan` packs the batch into waves that each fit in 100% NPU and writes
323
+ `~/.pi/agent/tiiny/tiiny-plan.json`. The router reads it: a planned goal
324
+ runs on its planned model, but only if that model is loaded. Until
325
+ `load` runs, the planned model is not loaded, so the router falls back
326
+ as usual. `release` reloads the `required` models from `tiers.json` and
327
+ checks that it worked.
328
+
329
+ See [Commands](#commands) for every option.
330
+ The `tiiny-routing` skill walks a pi agent through this sequence.
331
+
332
+ ## Commands
333
+
334
+ ```sh
335
+ pirouter [--device NAME] <command> [options]
336
+ ```
337
+
338
+ ### `npx pirouter`
339
+
340
+ Run `npx pirouter` with no arguments for a list of commands.
341
+
342
+ `install`, `uninstall` and `check` are handled by `pirouter` itself.
343
+ Every other command runs
344
+ the controller, `controller/tiiny_ctl.py`, with `python3`.
345
+ `npx pirouter check -h` and `npx pirouter <controller command> -h`
346
+ show a command's options.
347
+
348
+ `--device NAME` goes before the command, and targets a device by name
349
+ (for example `tiiny-ant`) or IP address. With a named device, the lock
350
+ and plan files get the device name in them
351
+ (`tiiny-lock-<name>.json`, `tiiny-plan-<name>.json`).
352
+
353
+ Several controller commands take `--owner`. If it is not given, the
354
+ owner is `$TIINY_OWNER`, or else `$USER`. The lock is between owners:
355
+ a command that changes the device refuses to run while a different
356
+ owner holds the lock, unless you pass `--force`.
357
+
358
+ ### `pirouter install`
359
+
360
+ ```bash
361
+ npx pirouter install [--dry-run]
362
+ ```
363
+
364
+ Link the extension, agents and skill into `./.pi`, and copy the
365
+ subagent extension. See [Install](#install).
366
+ Flag `--dry-run` prints the changes without making them, and `-h`
367
+ prints help.
368
+
369
+
370
+ ### `pirouter uninstall`
371
+
372
+ ```bash
373
+ npx pirouter uninstall [--dry-run]
374
+ ```
375
+
376
+ Remove the router from global pi config and the current directory's
377
+ `.pi/`. See [Uninstall](#uninstall) for the files removed and preserved.
378
+ Use `--dry-run` to preview changes, or `-h` for help.
379
+
380
+ ### `pirouter check`
381
+
382
+ ```bash
383
+ npx pirouter check [--tier TIER] [--thinking auto|off|on] "goal"
384
+ npx pirouter check --all
385
+ ```
386
+
387
+ Shows what the router would do with a goal: the tier, the model, the
388
+ budget, the reasoning settings it would send, and whether the model is
389
+ loaded. It only reads. See
390
+ [Checking a route](#checking-a-route-before-spending-tokens).
391
+
392
+ | Option | Meaning |
393
+ |---|---|
394
+ | `--tier TIER` | use this tier instead of inferring one from the goal |
395
+ | `--thinking MODE` | `auto` (default), `off` or `on` |
396
+ | `--all` | list every tier's candidates and which are loaded |
397
+
398
+ ### `pirouter status`
399
+
400
+ ```bash
401
+ npx pirouter status
402
+ ```
403
+
404
+ Shows the device, the loaded models, the lock, the hold (what the
405
+ current batch loaded) and the plan. It takes no options.
406
+
407
+ ### `pirouter plan`
408
+
409
+ ```bash
410
+ npx pirouter plan --goal "GOAL" [--goal "GOAL" ...] [options]
411
+ ```
412
+
413
+ Picks a model for each goal, packs the models into waves that each fit
414
+ in 100% NPU, and writes the plan file. It needs at least one `--goal`.
415
+ Only models downloaded to the device are chosen.
416
+
417
+ | Option | Meaning |
418
+ |---|---|
419
+ | `--goal GOAL` | a task goal; repeat it for a batch |
420
+ | `--tier TIER` | use this tier for every goal |
421
+ | `--model MODEL` | use this model for every goal |
422
+ | `--owner OWNER` | who is planning |
423
+ | `--task TASK` | what the batch is for |
424
+
425
+ ### `pirouter reserve`
426
+
427
+ ```bash
428
+ npx pirouter reserve --owner OWNER [options]
429
+ ```
430
+
431
+ Takes the lock, so other agents do not load or unload models while
432
+ your batch runs. The lock expires on its own, so a crashed agent does
433
+ not hold the device forever.
434
+
435
+ | Option | Meaning |
436
+ |---|---|
437
+ | `--owner OWNER` | who is taking the device (required) |
438
+ | `--task TASK` | what it is for |
439
+ | `--model MODEL` | a model the batch needs; repeat for several |
440
+ | `--ttl SECONDS` | how long the lock lasts (default 1800) |
441
+
442
+ ### `pirouter load`
443
+
444
+ ```bash
445
+ npx pirouter load [--wave N | --model MODEL ...] [options]
446
+ ```
447
+
448
+ Loads models and records them as the hold. With no `--model`, it reads
449
+ the plan: `--wave N` loads that wave, and without it, every wave's
450
+ models are loaded. If a model does not fit, it unloads models to make
451
+ room itself, so the device does not choose what to evict.
452
+
453
+ | Option | Meaning |
454
+ |---|---|
455
+ | `--model MODEL` | a model to load; repeat for several |
456
+ | `--wave N` | load only wave `N` of the plan |
457
+ | `--owner OWNER` | who is loading |
458
+ | `--force` | load even if another owner holds the lock |
459
+
460
+ ### `pirouter ensure`
461
+
462
+ ```bash
463
+ npx pirouter ensure --model MODEL [--model MODEL ...] [options]
464
+ ```
465
+
466
+ Makes sure the given models are loaded, for example after the device
467
+ reboots. A model that is already loaded is left alone, so it is cheap
468
+ to run before every batch. It reports a model that did not load as a
469
+ failure.
470
+
471
+ | Option | Meaning |
472
+ |---|---|
473
+ | `--model MODEL` | a model that must be loaded (required; repeatable) |
474
+ | `--owner OWNER` | who is asking |
475
+ | `--force` | load even if another owner holds the lock |
476
+
477
+ ### `pirouter release`
478
+
479
+ ```bash
480
+ npx pirouter release [--owner OWNER] [--force]
481
+ ```
482
+
483
+ Clears the hold, reloads the `required` models from `tiers.json`,
484
+ checks that they loaded, and clears the lock. It checks who holds the
485
+ lock before it changes anything.
486
+
487
+ | Option | Meaning |
488
+ |---|---|
489
+ | `--owner OWNER` | who is releasing |
490
+ | `--force` | release a lock held by another owner |
491
+
492
+ ### `pirouter catalog`
493
+
494
+ ```bash
495
+ npx pirouter catalog [--update | --refresh]
496
+ ```
497
+
498
+ Compares the device's model store with the last saved snapshot, and
499
+ lists models that were added, removed or changed. It names any changed
500
+ model that `tiers.json` lists, since its NPU and context figures may
501
+ now be wrong.
502
+
503
+ | Option | Meaning |
504
+ |---|---|
505
+ | `--update` | save the current store as the new snapshot |
506
+ | `--refresh` | the same as `--update` |
507
+
508
+ ### `pirouter image`
509
+
510
+ ```bash
511
+ npx pirouter image [--kind image|music|ocr] [--load] "description"
512
+ ```
513
+
514
+ Picks a model for an image, music or OCR job from its description, and
515
+ shows its NPU share, whether it is loaded, and the other candidates.
516
+
517
+ | Option | Meaning |
518
+ |---|---|
519
+ | `--kind KIND` | `image` (default), `music` or `ocr` |
520
+ | `--load` | load the chosen model and record it as the hold; free it with `release` |
521
+
522
+
523
+
524
+ ## Reasoning mode
525
+
526
+ The thinking level on the virtual model sets the reasoning mode:
527
+
528
+ | Suffix | Mode | Meaning |
529
+ |---|---|---|
530
+ | `:off` | off | as little reasoning as the model allows |
531
+ | `:medium` (default) | auto | the model's own default; nothing is sent |
532
+ | `:high` | on | reasoning on, highest level |
533
+
534
+ For `off` and `on`, the router reads the routed model's `thinking` block
535
+ from the device and rewrites the request:
536
+
537
+ - `chat_template_kwargs.enable_thinking` is always sent, because the
538
+ device's `toggleable` flag is not always right.
539
+ - If the model has levels but no switch (for example `gpt-oss-20b`),
540
+ `reasoning_effort` is set to its cheapest level for `off` and its
541
+ highest for `on`. The label then says `off->low`, since the model still
542
+ reasons, only less.
543
+ - `max_tokens` is raised to at least 4096. A reasoning model with a
544
+ small budget spends it all on the trace and returns an empty answer
545
+ with HTTP 200.
546
+
547
+ The achieved state, not the requested one, is shown in the label:
548
+ `off`, `on`, `off->low`, `on->high`, `unsupported` (the model does not
549
+ reason), or `unknown` (the device would not say, so nothing was changed).
550
+
551
+ ## Checking a route before spending tokens
552
+
553
+ ```bash
554
+ npx pirouter check --thinking off "index every file under the vault"
555
+ ```
556
+
557
+ ```
558
+ goal: index every file under the vault
559
+ tier: index (inferred)
560
+ model: Qwen/Qwen3.6-35B-A3B-turbo
561
+ label: index->generalist
562
+ budget: 55% NPU, 262144 context
563
+ reasoning: on/off switch
564
+ thinking: off -> off
565
+ sent: enable_thinking=false, max_tokens>=4096
566
+ residency: loaded
567
+ ```
568
+
569
+ `--tier <tier>` forces a tier. `--all` lists every tier's candidates and
570
+ whether each is loaded. It only reads.
571
+
572
+ To log every live routing decision, set `TIINY_ROUTER_TRACE` to a file.
573
+ It gets one JSON line per request:
574
+
575
+ ```bash
576
+ TIINY_ROUTER_TRACE=/tmp/tiiny-routes.jsonl pi --model tiiny-router/auto
577
+ ```
578
+
579
+ ```json
580
+ {"reason":"user","virtual":"auto","tier":"index","model":"Qwen/Qwen3.6-35B-A3B-turbo","label":"index->generalist","thinking":"off->off"}
581
+ ```
582
+
583
+ ## The tier table
584
+
585
+ `tiers.json` is the single tier table. The extension reads it, and so
586
+ does the controller, through `controller/tiers.py`. It holds:
587
+
588
+ - `generalist`: the model the device keeps loaded, used as the fallback.
589
+ - `required`: what `pirouter release` restores.
590
+ - `tiers`: per tier, candidates in order of preference, each with
591
+ `npu` (share of the pool) and `context` (window).
592
+ - `shapeSignals`: phrases that identify each tier in `auto`. The first
593
+ tier with a match wins, so more specific tiers are listed first.
594
+
595
+ The figures come from this device on 2026-11-20, and only models that
596
+ were downloaded are listed. The model store changes with TiinyOS
597
+ updates, and the catalogue is not always right: it lists the generalist
598
+ at 49% NPU, while `tiiny top` shows 55%. After an update, check what
599
+ changed:
600
+
601
+ ```bash
602
+ npx pirouter catalog --refresh
603
+ ```
604
+
605
+ ```bash
606
+ tiiny top
607
+ ```
608
+
609
+ Then edit `tiers.json`, and add any new ids to `~/.pi/agent/models.json`.
610
+ Two models whose shares add up to more than 100% cannot be loaded at the
611
+ same time.
612
+
613
+ ## Configuration
614
+
615
+ | Variable | Default | Purpose |
616
+ |---|---|---|
617
+ | `TIINY_API_KEY` | (required) | device bearer token |
618
+ | `TIINY_BASE_URL` | `http://openai.api.tiiny/v1` | inference endpoint |
619
+ | `TIINY_HOST` | `http://tiinyhost.local` | model store catalogue |
620
+ | `TIINY_PI_PROVIDER` | `tiiny` | provider id in `models.json` |
621
+ | `TIINY_HOME` | `~/.pi/agent/tiiny` | plan, lock and hold files |
622
+ | `TIINY_OWNER` | `$USER` | lock owner for controller commands |
623
+ | `TIINY_PLAN_FILE` | `$TIINY_HOME/tiiny-plan.json` | plan the router reads |
624
+ | `TIINY_TIERS_FILE` | `./tiers.json` | tier table |
625
+ | `TIINY_THINKING_MAX_TOKENS` | `4096` | output floor when reasoning is set |
626
+ | `TIINY_ROUTER_TRACE` | (off) | JSON-lines route log |
627
+
628
+ ## Operating notes
629
+
630
+ ### One task at a time
631
+
632
+ The device serves one generation-heavy stream at a time. Two heavy
633
+ requests do not fail; they **hang**, and the device keeps reporting
634
+ healthy. This applies to the device as a whole, not just to pi. Killing
635
+ a client does not seem to stop its request on the device, so later
636
+ requests can hang behind it until it finishes. To clear stuck work, run `tiiny interrupt --all`, but note
637
+ that it stops every active request on the device, not just yours.
638
+
639
+ ### A 404 is residency, not a bad id
640
+
641
+ `404 "is not loaded"` means the model is not resident. `GET /v1/models`
642
+ and `tiiny top` list loaded models. `tiiny ls` lists the whole store.
643
+
644
+ ### Changes need a reload
645
+
646
+ A running pi session keeps the extension it loaded. Run `/reload` after
647
+ editing. Subagent children are new processes and always load the
648
+ current code.
649
+
650
+ ## Layout
651
+
652
+ ```
653
+ index.ts the pi extension: virtual models + payload rewrite
654
+ src/tiers.ts tier table loading and shape detection (pure)
655
+ src/route.ts model choice (pure)
656
+ src/thinking.ts reasoning-mode resolution (pure)
657
+ src/device.ts read-only device and plan-file access
658
+ src/cli/route-check.ts the route checker (`pirouter check`)
659
+ dist/ src/ compiled to JS, built on pack
660
+ tiers.json the tier table, shared with the controller
661
+ agents/ tiiny-* subagent definitions
662
+ skill/SKILL.md the tiiny-routing skill
663
+ controller/ tiiny_ctl.py and tiiny-swap.sh, adapted for pi
664
+ bin/pirouter the `pirouter` command line
665
+ scripts/install.sh `pirouter install`: links everything into ./.pi
666
+ ```
667
+
668
+ ## Development
669
+
670
+ ```bash
671
+ npm test
672
+ ```
673
+
674
+ ```bash
675
+ npm run typecheck
676
+ ```
677
+
678
+ ```bash
679
+ npm run build
680
+ ```
681
+
682
+ `build` compiles `src/` to `dist/`. It runs on `npm pack` and
683
+ `npm publish`, because Node will not run TypeScript from inside
684
+ `node_modules`, so an installed `pirouter check` runs the compiled copy.
685
+ In a checkout, `bin/pirouter` runs the source directly, and
686
+ `npm run route-check -- "<goal>"` does the same.
687
+
688
+ ### Tests
689
+
690
+ There are three suites. `npm test` runs the first two and skips the
691
+ third.
692
+
693
+ ```bash
694
+ npm run test:unit
695
+ ```
696
+
697
+ Unit tests for the pure modules: tier detection, model choice and
698
+ reasoning resolution.
699
+
700
+ ```bash
701
+ npm run test:e2e
702
+ ```
703
+
704
+ End-to-end tests against a fake device. A local HTTP server
705
+ (`test/support/fake-tiiny.ts`) serves `/v1/models`, the store catalogue
706
+ and chat completions, and returns `404 "is not loaded"` for a model that
707
+ is not resident. A fake `tiiny` CLI (`test/support/fake-tiiny-cli`)
708
+ loads and unloads models on it. The tests run the pinned pi binary and
709
+ the real controller against it, using a throwaway pi agent directory, so
710
+ they never touch `~/.pi` or a real device. They cover:
711
+
712
+ - routing to a loaded specialist, falling through a tier, falling back
713
+ to the generalist, inferring the tier in `auto`, and planned goals
714
+ - the reasoning controls on the wire for switch, levels-only and
715
+ non-reasoning models
716
+ - follow-ups staying on one model, and a model evicted mid-task being
717
+ replaced
718
+ - a parent delegating through the subagent tool, with the child routing
719
+ itself
720
+ - the controller: waves that fit, the lock, explicit eviction before a
721
+ load, and release restoring the required models
722
+ - the whole cycle: plan, reserve, load, then a routed child on the loaded
723
+ specialist, then release
724
+
725
+ ```bash
726
+ npm run test:live
727
+ ```
728
+
729
+ Live tests against the real device, in `test/live/`. **They change the
730
+ device.** They plan and load the `index` tier's first model
731
+ (`openai/gpt-oss-20b` in the shipped table), send one routed request
732
+ with thinking off and check that it reached the device as
733
+ `reasoning_effort: "low"`, then release. Afterwards they unload anything
734
+ they loaded, so the device ends as it started. They use your
735
+ `~/.pi/agent/models.json` and `TIINY_API_KEY`, and stop if another owner
736
+ holds the lock. Run them only when nothing else is using the device.
737
+
738
+ ## Credits
739
+
740
+ The routing design, the controller (`controller/tiiny_ctl.py`,
741
+ `controller/tiiny-swap.sh`) and the special-model table come from
742
+ [AI_Articles/tiiny-model-routing](https://github.com/garciacionet-alt/AI_Articles/tree/main/tiiny-model-routing)
743
+ by Gary Garcia, under the MIT License. The controller was changed to keep
744
+ its state in `TIINY_HOME` and to read its tier table and restore set from
745
+ `tiers.json`.