ultimate-pi 0.2.7 → 0.3.1

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.
Files changed (53) hide show
  1. package/.agents/skills/harness-eval/SKILL.md +1 -1
  2. package/.agents/skills/harness-governor/SKILL.md +2 -2
  3. package/.agents/skills/harness-spec/SKILL.md +1 -1
  4. package/.pi/PACKAGING.md +3 -2
  5. package/.pi/extensions/custom-header.ts +0 -17
  6. package/.pi/extensions/pi-model-router-harness.ts +42 -0
  7. package/.pi/extensions/policy-gate.ts +18 -0
  8. package/.pi/extensions/provider-payload-sanitize.ts +66 -0
  9. package/.pi/extensions/sentrux-rules-sync.ts +0 -18
  10. package/.pi/harness/README.md +3 -2
  11. package/.pi/harness/docs/adrs/0004-defer-ci-agent-smoke.md +1 -1
  12. package/.pi/harness/docs/adrs/0006-sentrux-dual-layer.md +1 -1
  13. package/.pi/harness/docs/adrs/0009-sentrux-rules-lifecycle.md +2 -2
  14. package/.pi/harness/evals/smoke/README.md +1 -1
  15. package/.pi/harness/evolution/README.md +1 -1
  16. package/.pi/harness/evolution/chaos-drill.md +1 -1
  17. package/.pi/prompts/harness-setup.md +42 -35
  18. package/.pi/scripts/README.md +25 -9
  19. package/.pi/scripts/harness-cli-verify.sh +4 -2
  20. package/.pi/scripts/harness-seed-project-contracts.mjs +49 -0
  21. package/.pi/scripts/harness-sync-model-router.mjs +84 -0
  22. package/.pi/scripts/harness-verify.mjs +5 -3
  23. package/.pi/scripts/sentrux-rules-sync.mjs +2 -2
  24. package/.pi/scripts/vendor-sync-pi-model-router.sh +47 -0
  25. package/.pi/settings.example.json +0 -1
  26. package/.sentrux/rules.toml +1 -1
  27. package/AGENTS.md +1 -1
  28. package/CHANGELOG.md +62 -0
  29. package/README.md +1 -1
  30. package/THIRD_PARTY_NOTICES.md +8 -0
  31. package/biome.json +2 -1
  32. package/package.json +9 -10
  33. package/vendor/pi-model-router/.prettierignore +4 -0
  34. package/vendor/pi-model-router/.prettierrc +5 -0
  35. package/vendor/pi-model-router/AGENTS.md +39 -0
  36. package/vendor/pi-model-router/LICENSE +21 -0
  37. package/vendor/pi-model-router/README.md +99 -0
  38. package/vendor/pi-model-router/UPSTREAM_PIN.md +8 -0
  39. package/vendor/pi-model-router/docs/ARCHITECTURE.md +54 -0
  40. package/vendor/pi-model-router/extensions/commands.ts +720 -0
  41. package/vendor/pi-model-router/extensions/config.ts +348 -0
  42. package/vendor/pi-model-router/extensions/constants.ts +1 -0
  43. package/vendor/pi-model-router/extensions/index.ts +457 -0
  44. package/vendor/pi-model-router/extensions/provider.ts +529 -0
  45. package/vendor/pi-model-router/extensions/routing.ts +416 -0
  46. package/vendor/pi-model-router/extensions/state.ts +49 -0
  47. package/vendor/pi-model-router/extensions/types.ts +86 -0
  48. package/vendor/pi-model-router/extensions/ui.ts +130 -0
  49. package/vendor/pi-model-router/model-router.example.json +48 -0
  50. package/vendor/pi-model-router/package.json +48 -0
  51. package/vendor/pi-model-router/tsconfig.json +16 -0
  52. package/.pi/extensions/model-router-bootstrap.ts +0 -174
  53. package/.sentrux/.harness-rules-meta.json +0 -5
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * After `.pi/model-router.json` exists, set sensible Pi defaults (`router` / `auto`)
4
+ * when the project has no `defaultProvider`. Does **not** add/remove npm packages
5
+ * — model routing ships vendored inside ultimate-pi.
6
+ */
7
+
8
+ import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
9
+ import { join, dirname } from "node:path";
10
+
11
+ function loadSettings(settingsPath) {
12
+ if (!existsSync(settingsPath)) {
13
+ return null;
14
+ }
15
+ try {
16
+ return JSON.parse(readFileSync(settingsPath, "utf8"));
17
+ } catch {
18
+ console.error("[harness-model-router] Invalid JSON:", settingsPath);
19
+ process.exit(1);
20
+ }
21
+ }
22
+
23
+ function saveSettings(settingsPath, data) {
24
+ mkdirSync(dirname(settingsPath), { recursive: true });
25
+ writeFileSync(
26
+ settingsPath,
27
+ `${JSON.stringify(data, null, "\t")}\n`,
28
+ "utf8",
29
+ );
30
+ }
31
+
32
+ function main() {
33
+ const root = process.cwd();
34
+ const configPath = join(root, ".pi", "model-router.json");
35
+ const settingsPath = join(root, ".pi", "settings.json");
36
+ const hasConfig = existsSync(configPath);
37
+
38
+ const settings = loadSettings(settingsPath);
39
+ if (!settings) {
40
+ console.warn(
41
+ "[harness-model-router] No .pi/settings.json — skipping (merge Step 3 first)",
42
+ );
43
+ process.exit(0);
44
+ }
45
+
46
+ let changed = false;
47
+
48
+ if (!hasConfig) {
49
+ if (settings.defaultProvider === "router") {
50
+ delete settings.defaultProvider;
51
+ delete settings.defaultModel;
52
+ changed = true;
53
+ }
54
+ if (changed) {
55
+ saveSettings(settingsPath, settings);
56
+ console.warn(
57
+ "⚠ No .pi/model-router.json — cleared router defaultProvider if present",
58
+ );
59
+ } else {
60
+ console.log("[harness-model-router] No config file; nothing to do");
61
+ }
62
+ process.exit(0);
63
+ }
64
+
65
+ const noProjectDefault =
66
+ settings.defaultProvider == null || settings.defaultProvider === "";
67
+
68
+ if (noProjectDefault) {
69
+ settings.defaultProvider = "router";
70
+ settings.defaultModel = "auto";
71
+ changed = true;
72
+ }
73
+
74
+ if (changed) {
75
+ saveSettings(settingsPath, settings);
76
+ console.log(
77
+ "✓ Router defaults set (`router` / `auto`) — run /reload in pi when ready",
78
+ );
79
+ } else {
80
+ console.log("[harness-model-router] Defaults unchanged (user set defaultProvider)");
81
+ }
82
+ }
83
+
84
+ main();
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * harness:verify — deterministic harness contract checks (no LLM).
3
+ * harness-verify — deterministic harness contract checks (no LLM).
4
4
  */
5
5
 
6
6
  import { readFile, access } from "node:fs/promises";
@@ -122,12 +122,14 @@ async function checkSentruxRules() {
122
122
  "--check",
123
123
  ]);
124
124
  if (checkCode !== 0) {
125
- fail(checkOut.trim() || "sentrux rules.toml out of date — run harness:sentrux-sync");
125
+ fail(checkOut.trim() || "sentrux rules.toml out of date — run node \"$UP_PKG/.pi/scripts/sentrux-rules-sync.mjs\" --force (see .pi/scripts/README.md for UP_PKG)");
126
126
  }
127
127
  ok("sentrux rules.toml in sync with manifest");
128
128
 
129
129
  if (!(await fileExists(SENTRUX_RULES))) {
130
- fail("missing .sentrux/rules.toml — run npm run harness:sentrux-sync");
130
+ fail(
131
+ "missing .sentrux/rules.toml — run node \"$UP_PKG/.pi/scripts/sentrux-rules-sync.mjs\" --force (resolve UP_PKG via .pi/scripts/README.md)",
132
+ );
131
133
  }
132
134
  ok(".sentrux/rules.toml present");
133
135
  }
@@ -80,7 +80,7 @@ function renderManagedBlock(manifest) {
80
80
  function mergeRules(existing, managedBlock) {
81
81
  const header = `# Sentrux rules — ${new Date().toISOString().slice(0, 10)}
82
82
  # Docs: https://sentrux.dev/docs/rules-engine/
83
- # Sync: npm run harness:sentrux-sync (or /harness-sentrux-sync in pi)
83
+ # Sync: node $UP_PKG/.pi/scripts/sentrux-rules-sync.mjs --force (see .pi/scripts/README.md for UP_PKG) or /harness-sentrux-sync in pi
84
84
  #
85
85
  # Custom rules: add TOML below the managed block; they are preserved on sync.
86
86
 
@@ -168,7 +168,7 @@ async function main() {
168
168
  if (checkOnly) process.exit(0);
169
169
  } else if (checkOnly) {
170
170
  fail(
171
- "rules.toml out of date — run npm run harness:sentrux-sync",
171
+ "rules.toml out of date — run node \"$UP_PKG/.pi/scripts/sentrux-rules-sync.mjs\" --force (see .pi/scripts/README.md for UP_PKG)",
172
172
  );
173
173
  } else {
174
174
  await mkdir(join(ROOT, ".sentrux"), { recursive: true });
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env bash
2
+ # Re-fetch upstream pi-model-router and re-apply ultimate-pi patches.
3
+ set -euo pipefail
4
+ ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
5
+ VEND="$ROOT/vendor/pi-model-router"
6
+
7
+ rm -rf "$VEND"
8
+ git clone --depth 1 https://github.com/yeliu84/pi-model-router.git "$VEND"
9
+ COMMIT="$(git -C "$VEND" rev-parse HEAD)"
10
+ rm -rf "$VEND/.git"
11
+
12
+ for f in "$VEND"/extensions/*.ts; do
13
+ sed -i \
14
+ -e "s|'@earendil-works/pi-agent-core'|'@mariozechner/pi-agent-core'|g" \
15
+ -e "s|'@earendil-works/pi-ai'|'@mariozechner/pi-ai'|g" \
16
+ -e "s|'@earendil-works/pi-coding-agent'|'@mariozechner/pi-coding-agent'|g" \
17
+ -e "s|'@earendil-works/pi-tui'|'@mariozechner/pi-tui'|g" \
18
+ "$f"
19
+ done
20
+
21
+ # Align package.json peers with @mariozechner (upstream lists @earendil-works)
22
+ sed -i \
23
+ -e 's|"@earendil-works/pi-agent-core"|"@mariozechner/pi-agent-core"|g' \
24
+ -e 's|"@earendil-works/pi-ai"|"@mariozechner/pi-ai"|g' \
25
+ -e 's|"@earendil-works/pi-coding-agent"|"@mariozechner/pi-coding-agent"|g' \
26
+ -e 's|"@earendil-works/pi-tui"|"@mariozechner/pi-tui"|g' \
27
+ "$VEND/package.json"
28
+
29
+ python3 -c "
30
+ import re, pathlib
31
+ for p in pathlib.Path('$VEND/extensions').glob('*.ts'):
32
+ t = p.read_text()
33
+ t2 = re.sub(r\"from '\\./([^']+)'\", lambda m: f\"from './{m.group(1)}.js'\" if not m.group(1).endswith('.js') else m.group(0), t)
34
+ p.write_text(t2)
35
+ "
36
+
37
+ cat >"$VEND/UPSTREAM_PIN.md" <<EOF
38
+ # Vendored \`pi-model-router\`
39
+
40
+ - **Repository:** https://github.com/yeliu84/pi-model-router
41
+ - **License:** MIT (\`LICENSE\` in this tree)
42
+ - **Pinned upstream commit:** \`$COMMIT\`
43
+ - **Local changes:** \`extensions/*.ts\` imports use \`@mariozechner/*\` and relative paths end in \`.js\` for TypeScript nodenext.
44
+ EOF
45
+
46
+ rm -f "$VEND/package-lock.json"
47
+ echo "✓ Vendor refreshed at $VEND (commit $COMMIT)"
@@ -8,7 +8,6 @@
8
8
  "npm:@posthog/pi",
9
9
  "npm:context-mode",
10
10
  "npm:@tintinweb/pi-subagents",
11
- "npm:@yeliu84/pi-model-router",
12
11
  "npm:@sting8k/pi-vcc"
13
12
  ]
14
13
  }
@@ -1,6 +1,6 @@
1
1
  # Sentrux rules — 2026-05-15
2
2
  # Docs: https://sentrux.dev/docs/rules-engine/
3
- # Sync: npm run harness:sentrux-sync (or /harness-sentrux-sync in pi)
3
+ # Sync: node $UP_PKG/.pi/scripts/sentrux-rules-sync.mjs --force (see .pi/scripts/README.md for UP_PKG) or /harness-sentrux-sync in pi
4
4
  #
5
5
  # Custom rules: add TOML below the managed block; they are preserved on sync.
6
6
 
package/AGENTS.md CHANGED
@@ -26,7 +26,7 @@ Created: 2026-05-14
26
26
  - Graph before grep — always consult the knowledge graph first
27
27
  - ./raw/ is source storage for graphify
28
28
  - ADRs in docs/adr/ (repo) and .pi/harness/docs/adrs/ (harness) with structured format
29
- - `npm run harness:verify` for deterministic harness contract checks
29
+ - `node "$UP_PKG/.pi/scripts/harness-verify.mjs"` for deterministic harness contract checks (`UP_PKG` — see `.pi/scripts/README.md`)
30
30
  - Harness context: **context-mode only** — never lean-ctx on harness paths (see harness-context skill)
31
31
  - `graphify update .` after significant code changes
32
32
  - ast-grep (`sg`) is the default code search tool — use `sg -p 'pattern'` for structural search, never grep for code
package/CHANGELOG.md CHANGED
@@ -2,6 +2,68 @@
2
2
 
3
3
  All notable changes to this project are documented in this file.
4
4
 
5
+ ## [v0.3.1] — 2026-05-15
6
+
7
+ ### 🐛 Fixes
8
+
9
+ - **External `/harness-setup`**: policy gate no longer forces **plan** phase because the setup doc mentions `harness-plan` (e.g. `gh label create "harness-plan"`).
10
+ - **Harness specs in consumer repos**: copy `*.schema.json` and specs `README` from the package via `harness-seed-project-contracts.mjs` as part of setup (so `plan-packet.schema.json` exists before planning).
11
+ - **Strict LLM gateways**: new `provider-payload-sanitize` extension removes disallowed top-level fields (`reasoning`, etc.) from `messages` before provider requests (avoids 400 “Extra inputs … reasoning” on some OpenAI-compatible APIs).
12
+
13
+ ## [v0.3.0] — 2026-05-15
14
+
15
+ ### 📦 Release
16
+
17
+ - First tag-driven npm / GitHub Packages publish since **v0.2.2**; intermediate history remains under **v0.2.3–v0.2.10** below.
18
+
19
+ ### ✨ Features
20
+
21
+ - Vendored [**pi-model-router**](https://github.com/yeliu84/pi-model-router) with harness gate and `npm run vendor:sync-router`
22
+ - Sentrux **`rules.toml`** sync from `architecture.manifest.json` plus Phase 2 harness telemetry / governance scaffolding (see ADRs)
23
+ - Document **`UP_PKG`** resolution and direct **`$UP_PKG/.pi/scripts/*`** invocation for external installs (no `harness:*` entries in consumer `package.json`)
24
+
25
+ ### 🐛 Fixes
26
+
27
+ - **`pi update`** / global installs: complete `koffi` tree; Node 22–compatible dependency graph
28
+ - Harness-setup: Graphify bootstrap + CLI verification improvements (system deps, installers)
29
+
30
+ ### 🔧 Chores
31
+
32
+ - Drop external **`npm:@yeliu84/pi-model-router`** dependency; add **`THIRD_PARTY_NOTICES.md`**
33
+ - Align publish **`files`** allowlist and **graphify-out** refresh
34
+
35
+ ## [v0.2.10] — 2026-05-15
36
+
37
+ ### 🔧 Chores
38
+
39
+ - Harness scripts must be invoked from `.pi/scripts/` (or `$UP_PKG/.pi/scripts/` for consumers); root `npm run harness:*` scripts removed so `pi install npm:ultimate-pi` works in external repos without mirroring npm script entries
40
+
41
+
42
+ ### ✨ Features
43
+
44
+ - Vendor [`yeliu84/pi-model-router`](https://github.com/yeliu84/pi-model-router) under `vendor/pi-model-router/` with a harness gate in `.pi/extensions/pi-model-router-harness.ts` (no `router/auto` until `.pi/model-router.json` exists after `/harness-setup`)
45
+ - `npm run vendor:sync-router` to refresh upstream + apply import patches (see `vendor/pi-model-router/UPSTREAM_PIN.md`)
46
+
47
+ ### 🔧 Chores
48
+
49
+ - Remove `npm:@yeliu84/pi-model-router` from package dependencies; add `THIRD_PARTY_NOTICES.md`
50
+ - `harness-sync-model-router.mjs` adjusts Pi defaults only (no package toggling)
51
+ - `check:ts` uses ES2023; devDependency on `@mariozechner/pi-ai`, `pi-tui`, `pi-agent-core` for vendored typecheck
52
+
53
+ ### 🐛 Fixes
54
+
55
+ - Avoid npm package conflicts — router always comes from the bundled vendor tree
56
+
57
+ ## [v0.2.8] — 2026-05-15
58
+
59
+ ### ✨ Features
60
+
61
+ - Gate `pi-model-router`: shipped `.pi/settings.example.json` no longer lists `npm:@yeliu84/pi-model-router` until `/harness-setup` Step 3.5 creates `.pi/model-router.json` and runs `harness-sync-model-router.mjs` (adds the package, sets `router` / `auto` when project `defaultProvider` is unset, strips stale router entries when config is missing)
62
+
63
+ ### 🐛 Fixes
64
+
65
+ - Remove extension bootstrap that auto-wrote `.pi/model-router.json` on every start (router config is harness-owned only; avoids `router/auto` + built-in `gpt-5.4-pro` before setup)
66
+
5
67
  ## [v0.2.7] — 2026-05-15
6
68
 
7
69
  ### 🐛 Fixes
package/README.md CHANGED
@@ -66,7 +66,7 @@ Use this when you want each step separate:
66
66
 
67
67
  ## Defaults you should know
68
68
 
69
- - **Model routing is opt-in** — install does not force `router/auto` or `gpt-5.4-pro`. Enable with `/router profile auto` after `/harness-setup` generates `.pi/model-router.json`, or copy [`.pi/model-router.example.json`](.pi/model-router.example.json).
69
+ - **Model routing (vendored + gated)** — [`pi-model-router`](https://github.com/yeliu84/pi-model-router) ships inside this package (`vendor/pi-model-router/`). [`.pi/extensions/pi-model-router-harness.ts`](.pi/extensions/pi-model-router-harness.ts) activates it **only after** `.pi/model-router.json` exists (generation: `/harness-setup` Step 3.5), so **`router/auto` does not appear** beforehand. See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md). [`.pi/scripts/harness-sync-model-router.mjs`](.pi/scripts/harness-sync-model-router.mjs) may set **`defaultProvider`/`defaultModel`** to **`router`/`auto`** when the project sets no default — run **`/reload`** afterward. Do **not** add `npm:@yeliu84/pi-model-router` to `.pi/settings.json`; it duplicates the fork. Maintainer refresh: **`npm run vendor:sync-router`**.
70
70
  - **Plan before mutate** — write/edit/shell that changes the repo is blocked until execute phase.
71
71
  - **No auto-merge** — you decide when to open or merge a PR.
72
72
  - **Structured runs** — each run writes artifacts under `.pi/harness/runs/` for replay and audit.
@@ -0,0 +1,8 @@
1
+ # Third-party notices
2
+
3
+ ## pi-model-router (vendored)
4
+
5
+ - **Project:** https://github.com/yeliu84/pi-model-router
6
+ - **License:** MIT ([vendor/pi-model-router/LICENSE](vendor/pi-model-router/LICENSE))
7
+ - **Pinned revision:** See [vendor/pi-model-router/UPSTREAM_PIN.md](vendor/pi-model-router/UPSTREAM_PIN.md)
8
+ - ultimate-pi loads it from [`vendor/pi-model-router`](vendor/pi-model-router); import specifiers were adapted for `@mariozechner/pi-coding-agent` and related Pi packages.
package/biome.json CHANGED
@@ -11,7 +11,8 @@
11
11
  "includes": [
12
12
  "**/*.{ts,tsx,js,jsx,json,jsonc,css}",
13
13
  "!graphify-out/**/*",
14
- "!graphify-books-out/**/*"
14
+ "!graphify-books-out/**/*",
15
+ "!vendor/**/*"
15
16
  ]
16
17
  },
17
18
  "formatter": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ultimate-pi",
3
- "version": "0.2.7",
3
+ "version": "0.3.1",
4
4
  "description": "Ultimate AI coding harness for pi.dev — extensible skills, Obsidian wiki knowledge layer, compressed context, deterministic output",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -12,7 +12,6 @@
12
12
  "knowledge-base",
13
13
  "context-compression",
14
14
  "agent-skills",
15
- "cursor-sdk",
16
15
  "firecrawl",
17
16
  "context-mode",
18
17
  "vcc"
@@ -68,18 +67,16 @@
68
67
  "AGENTS.md",
69
68
  "biome.json",
70
69
  "CHANGELOG.md",
71
- "README.md"
70
+ "README.md",
71
+ "THIRD_PARTY_NOTICES.md",
72
+ "vendor/pi-model-router"
72
73
  ],
73
74
  "peerDependencies": {
74
75
  "@mariozechner/pi-coding-agent": "*"
75
76
  },
76
77
  "scripts": {
77
- "check:ts": "tsc --noEmit --target ES2022 --moduleResolution nodenext --module nodenext --skipLibCheck .pi/extensions/dotenv-loader.ts .pi/extensions/lib/posthog-node.d.ts .pi/extensions/lib/harness-posthog.ts .pi/extensions/lib/harness-paths.ts .pi/extensions/model-router-bootstrap.ts .pi/extensions/harness-telemetry.ts .pi/extensions/trace-recorder.ts .pi/extensions/observation-bus.ts .pi/extensions/drift-monitor.ts .pi/extensions/sentrux-rules-sync.ts .pi/extensions/custom-header.ts",
78
- "harness:graphify-bootstrap": "bash .pi/scripts/harness-graphify-bootstrap.sh",
79
- "harness:cli-verify": "bash .pi/scripts/harness-cli-verify.sh",
80
- "harness:verify": "node .pi/scripts/harness-verify.mjs",
81
- "harness:sentrux-sync": "node .pi/scripts/sentrux-rules-sync.mjs --force",
82
- "harness:meta-optimizer": "node .pi/harness/evolution/meta-optimizer.mjs",
78
+ "check:ts": "tsc --noEmit --target ES2023 --lib ES2023 --moduleResolution nodenext --module nodenext --skipLibCheck .pi/extensions/dotenv-loader.ts .pi/extensions/lib/posthog-node.d.ts .pi/extensions/lib/harness-posthog.ts .pi/extensions/lib/harness-paths.ts .pi/extensions/pi-model-router-harness.ts .pi/extensions/provider-payload-sanitize.ts .pi/extensions/harness-telemetry.ts .pi/extensions/trace-recorder.ts .pi/extensions/observation-bus.ts .pi/extensions/drift-monitor.ts .pi/extensions/sentrux-rules-sync.ts .pi/extensions/custom-header.ts",
79
+ "vendor:sync-router": "bash .pi/scripts/vendor-sync-pi-model-router.sh",
83
80
  "lint": "biome check",
84
81
  "lint:fix": "biome check --fix",
85
82
  "format": "biome format --write",
@@ -90,7 +87,10 @@
90
87
  },
91
88
  "devDependencies": {
92
89
  "@biomejs/biome": "2.4.14",
90
+ "@mariozechner/pi-agent-core": "0.73.0",
91
+ "@mariozechner/pi-ai": "0.73.0",
93
92
  "@mariozechner/pi-coding-agent": "0.73.0",
93
+ "@mariozechner/pi-tui": "0.73.0",
94
94
  "@sinclair/typebox": "^0.34.49",
95
95
  "lefthook": "2.1.6",
96
96
  "typescript": "^6.0.3"
@@ -99,7 +99,6 @@
99
99
  "@posthog/pi": "latest",
100
100
  "@sting8k/pi-vcc": "^0.3.12",
101
101
  "@tintinweb/pi-subagents": "latest",
102
- "@yeliu84/pi-model-router": "latest",
103
102
  "asciify-image": "^0.1.10",
104
103
  "jimp": "^1.6.1",
105
104
  "posthog-node": "^5.30.6"
@@ -0,0 +1,4 @@
1
+ node_modules
2
+ dist
3
+ .git
4
+ .pi
@@ -0,0 +1,5 @@
1
+ {
2
+ "tabWidth": 2,
3
+ "useTabs": false,
4
+ "singleQuote": true
5
+ }
@@ -0,0 +1,39 @@
1
+ # Pi Model Router: Core Mandates
2
+
3
+ ## Project Overview
4
+ The `pi-model-router` is an extension-first model router for the `pi` coding agent. It registers a custom logical provider (`router`) that exposes "profiles" as models (e.g., `router/auto`). For every turn, the router intelligently selects an underlying concrete model based on task complexity, conversation phase, and user-defined rules.
5
+
6
+ ## Architectural Principles
7
+ - **Extension-First**: All functionality must be implemented as a `pi` extension without modifying `pi` core.
8
+ - **Custom Provider**: Use `pi.registerProvider` to hook into the model lifecycle. The logical model (e.g., `router/auto`) should remain stable while the underlying model changes transparently.
9
+ - **Modularized Design**: Strictly follow the modular structure defined in Phase 3:
10
+ - `extensions/types.ts`: All interfaces and type definitions.
11
+ - `extensions/config.ts`: Configuration loading, normalization, and merging.
12
+ - `extensions/routing.ts`: Core routing logic (heuristics, classifier, rule matching).
13
+ - `extensions/provider.ts`: Custom `router` provider registration and delegation stream.
14
+ - `extensions/state.ts`: Session-persisted state management and snapshotting.
15
+ - `extensions/ui.ts`: UI status line and widget rendering logic.
16
+ - `extensions/commands.ts`: CLI command registrations and completions.
17
+ - `extensions/index.ts`: Main entry point (orchestrator).
18
+
19
+ ## Routing Decision Logic
20
+ Routing follows a tiered system (`high`, `medium`, `low`) and an ordered decision flow:
21
+ 1. **Budget Check**: Downgrade to `medium` if `maxSessionBudget` is exceeded.
22
+ 2. **Context Trigger**: Upgrade to `high` if `largeContextThreshold` is reached.
23
+ 3. **Manual Pin**: Use tier pinned via `/router pin` or `/router fix`.
24
+ 4. **Custom Rules**: Check keyword-based rules against the user prompt.
25
+ 5. **LLM Classifier (Optional)**: Call `classifierModel` for intent categorization.
26
+ 6. **Heuristics (Fallback)**: Use local heuristics if the classifier is off/fails.
27
+ 7. **Phase Bias**: Apply stickiness to maintain a consistent tier during multi-turn tasks.
28
+
29
+ ## Coding Standards
30
+ - **TypeScript**: Strictly adhere to TypeScript. NEVER use the `any` type; prefer specific types or `unknown`.
31
+ - **Functions**: Always use arrow functions (`const myFunc = () => ...`) instead of function statements (`function myFunc() ...`) for consistency and lexical scoping.
32
+ - **Imports**: Use top-level static imports over inline `import()` or `require()` calls for consistency and cleaner ESM code.
33
+ - **State Management**: Persist router state via `pi.appendEntry` with a custom `router-state` entry type to ensure branch-safe behavior.
34
+ - **Error Handling**: Implement robust fallback chains for model failures (retrying with alternative models).
35
+
36
+ ## Documentation Reference
37
+ - `docs/ARCHITECTURE.md`: Detailed architectural deep dive.
38
+ - `README.md`: Usage and installation guide.
39
+ - `model-router.example.json`: Reference for configuration structure.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ye Liu
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.
@@ -0,0 +1,99 @@
1
+ # pi-model-router
2
+
3
+ Intelligent per-turn model router extension for the [pi-coding-agent](https://github.com/earendil-works/pi/tree/main/packages/coding-agent). Automatically selects between high, medium, and low-tier LLMs based on task intent, session budget, context size, and custom rules — with automatic fallbacks and phase awareness.
4
+
5
+ ## What it does
6
+
7
+ - **Logical Router Provider**: Registers a `router` provider that exposes stable profiles (e.g., `router/auto`) as models.
8
+ - **Per-Turn Routing**: Intelligently chooses between `high`, `medium`, and `low` tiers for every turn based on task intent and complexity.
9
+ - **Task-Aware Heuristics**: Detects planning vs. implementation vs. lightweight tasks using keyword analysis, word count, and conversation history.
10
+ - **Advanced Controls**: Includes built-in support for:
11
+ - **LLM Intent Classifier**: Optionally use a fast model to categorize intent (overrides heuristics).
12
+ - **Custom Rules**: Define keyword-based tier overrides for specific patterns (e.g., `deploy` → `high`).
13
+ - **Context Trigger**: Automatically upgrade to high-tier when token usage exceeds a threshold.
14
+ - **Cost Budgeting**: Set a session spend limit; high tier downgrades to medium once exceeded.
15
+ - **Fallback Chains**: Automatic retry with alternative models if the primary choice fails.
16
+ - **Phase Memory**: Biased stickiness to keep you in the same tier during multi-turn planning or implementation work.
17
+ - **Thinking Control**: Full control over reasoning/thinking levels per tier and profile.
18
+ - **Persistent State**: Pins, profiles, costs, and debug history are remembered across agent restarts and conversation branches.
19
+
20
+ ## Installation
21
+
22
+ ### As a user
23
+
24
+ Install from npm:
25
+
26
+ ```bash
27
+ pi install npm:@yeliu84/pi-model-router
28
+ ```
29
+
30
+ ### For development
31
+
32
+ Clone this repo and install from source:
33
+
34
+ ```bash
35
+ pi install .
36
+ ```
37
+
38
+ Or load directly for one run:
39
+
40
+ ```bash
41
+ pi -e ./extensions/index.ts
42
+ ```
43
+
44
+ ## Configuration
45
+
46
+ Copy the example config to one of:
47
+
48
+ - `~/.pi/agent/model-router.json` (Global)
49
+ - `.pi/model-router.json` (Project-specific)
50
+
51
+ ### Basic Config Shape
52
+
53
+ ```json
54
+ {
55
+ "defaultProfile": "auto",
56
+ "classifierModel": "google/gemini-flash-latest",
57
+ "maxSessionBudget": 1.0,
58
+ "profiles": {
59
+ "auto": {
60
+ "high": { "model": "openai/gpt-5.4-pro", "thinking": "high" },
61
+ "medium": { "model": "google/gemini-flash-latest", "thinking": "medium" },
62
+ "low": { "model": "openai/gpt-5.4-nano", "thinking": "low" }
63
+ }
64
+ }
65
+ }
66
+ ```
67
+
68
+ ### Configuration Fields
69
+
70
+ | Field | Description |
71
+ | ----------------------- | --------------------------------------------------------------------------------- |
72
+ | `defaultProfile` | The profile to use when starting a new session. |
73
+ | `classifierModel` | (Optional) Model used to categorize intent. If omitted, fast heuristics are used. |
74
+ | `maxSessionBudget` | (Optional) USD budget for the session. Forces `medium` tier once exceeded. |
75
+ | `largeContextThreshold` | (Optional) Token count trigger to force `high` tier for large contexts. |
76
+ | `phaseBias` | (0.0 - 1.0) Stickiness of the current phase. Higher = more stable. Default `0.5`. |
77
+ | `rules` | List of custom keyword rules (e.g. `{ "matches": "deploy", "tier": "high" }`). |
78
+ | `profiles` | Map of profile definitions, each containing `high`, `medium`, and `low` tiers. |
79
+
80
+ ## Commands
81
+
82
+ | Command | Description |
83
+ | --------------------------- | ------------------------------------------------------------------------------- |
84
+ | `/router` | Show detailed status, current profile, spend, and settings. |
85
+ | `/router status` | Alias for `/router` (show current status). |
86
+ | `/router profile [name]` | Switch to a profile or list available ones (enables router if off). |
87
+ | `/router pin [prof] <t\|a>` | Pin a tier (high/medium/low/auto) for the current or specified profile. |
88
+ | `/router fix <tier>` | Correct the _last_ decision and pin that tier for the current profile. |
89
+ | `/router thinking ...` | Override thinking levels (e.g. `/router thinking low xhigh`). |
90
+ | `/router disable` | Disable the router and switch back to the last non-router model. |
91
+ | `/router widget <on\|off>` | Toggle the persistent state widget (supports `toggle`). |
92
+ | `/router debug <on\|off>` | Toggle turn-by-turn routing notifications (supports `toggle`, `clear`, `show`). |
93
+ | `/router reload` | Hot-reload the configuration JSON. |
94
+ | `/router help` | Show usage help for all subcommands. |
95
+
96
+ ## Documentation
97
+
98
+ - [Architecture Guide](docs/ARCHITECTURE.md): Deep dive into the routing logic and modular design.
99
+ - [Sample Configuration](model-router.example.json): Diverse profile examples (`cheap`, `deep`, `balanced`).
@@ -0,0 +1,8 @@
1
+ # Vendored `pi-model-router`
2
+
3
+ - **Repository:** https://github.com/yeliu84/pi-model-router
4
+ - **License:** MIT (`LICENSE` in this tree)
5
+ - **Pinned upstream commit:** `8c60095da0e753c242c4be9bb617b85f4dd3255c`
6
+ - **Local changes:** TypeScript imports in `extensions/*.ts` use `@mariozechner/*`; relative imports end in `.js` for NodeNext; `package.json` peerDependencies list `@mariozechner/*`.
7
+
8
+ **Refresh upstream:** run `npm run vendor:sync-router` from ultimate-pi root (updates this file with the latest commit SHA).
@@ -0,0 +1,54 @@
1
+ # Architecture: Pi Model Router Extension
2
+
3
+ The `pi-model-router` is an extension-first model router for the `pi` coding agent. It registers a custom logical provider (`router`) that exposes "profiles" as models (e.g., `router/auto`). For every turn, the router intelligently selects an underlying concrete model based on task complexity, conversation phase, and user-defined rules.
4
+
5
+ ## Core Concepts
6
+
7
+ ### 1. Profiles & Tiers
8
+
9
+ The router is organized into **Profiles** (e.g., `auto`, `cheap`, `deep`). Each profile defines three **Tiers**:
10
+
11
+ - **High**: Reserved for architecture, design, complex debugging, and planning. Uses high-reasoning models.
12
+ - **Medium**: The default for standard implementation, multi-file edits, and focused fixes.
13
+ - **Low**: Used for summaries, changelogs, formatting, and simple read-only lookups.
14
+
15
+ ### 2. Custom Provider Implementation
16
+
17
+ The extension uses `pi.registerProvider` to hook into the `pi` model lifecycle. This ensures that the selected model in the `pi` footer remains stable (e.g., `router/auto`) while the underlying model changes transparently turn-by-turn via the `streamSimple` interception.
18
+
19
+ ## Routing Decision Flow
20
+
21
+ For every request sent to a `router/*` model, the following logic is executed:
22
+
23
+ 1. **Budget Check**: If a `maxSessionBudget` is configured and the session spend exceeds it, the router automatically downgrades `high` tier requests to `medium`.
24
+ 2. **Context Trigger**: If `largeContextThreshold` is exceeded (measured in tokens), the router forces the `high` tier to ensure the model can handle the large context.
25
+ 3. **Manual Pin**: If the user has pinned a tier via `/router pin` or `/router fix`, that tier is used.
26
+ 4. **Custom Rules**: Keyword-based rules defined in the config are checked against the user prompt.
27
+ 5. **LLM Classifier (Optional)**: If `classifierModel` is configured, a fast LLM is called to categorize the user's intent.
28
+ 6. **Heuristics (Fallback)**: If the classifier is off or fails, a fast local heuristic (keyword/length/tool-use analysis) is used.
29
+ 7. **Biased Stickiness**: The `phaseBias` setting modulates thresholds to keep the router in a consistent phase (e.g., staying in `high` tier during a multi-turn planning session).
30
+
31
+ ## Module Architecture
32
+
33
+ The extension is modularized for maintainability:
34
+
35
+ - `extensions/index.ts`: Orchestrator. Manages state, hooks into `pi` events, and wires modules together.
36
+ - `extensions/provider.ts`: Implements the `router` provider and the delegation/retry loop.
37
+ - `extensions/routing.ts`: Core decision logic, heuristics, and the LLM classifier.
38
+ - `extensions/config.ts`: Loads, merges, and normalizes the JSON configuration.
39
+ - `extensions/commands.ts`: Registers all `/router` subcommands and their autocompletions.
40
+ - `extensions/ui.ts`: Manages the status line and the optional state widget.
41
+ - `extensions/state.ts`: Handles session-persisted state and snapshots.
42
+ - `extensions/types.ts`: Centralized interface and type definitions.
43
+
44
+ ## State & Persistence
45
+
46
+ The router state is persisted using `pi.appendEntry` with a custom type `router-state`. This allows the router to:
47
+
48
+ - Restore the active profile and pins across agent relaunches.
49
+ - Maintain independent pins and state for different conversation branches.
50
+ - Track accumulated session costs safely.
51
+
52
+ ## Reliability: Fallback Chains
53
+
54
+ Each tier in a profile can define an optional `fallbacks` list. If the primary model fails (e.g., due to rate limits or provider downtime), the router automatically retries the next model in the chain before surfacing an error to the user.