@alexgreensh/token-optimizer 0.0.0-stage → 5.13.50
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/.claude-plugin/marketplace.json +76 -0
- package/.claude-plugin/plugin.json +21 -0
- package/LICENSE +130 -0
- package/LICENSE-SMALL-BUSINESS.md +62 -0
- package/PRIVACY.md +200 -0
- package/README.md +13 -2
- package/hooks/codex-hooks.json +4 -0
- package/hooks/hooks.json +182 -0
- package/hooks/module_runner.py +149 -0
- package/hooks/posttooluse_runner.py +1110 -0
- package/hooks/python-launcher.sh +816 -0
- package/hooks/run.py +776 -0
- package/hooks/sessionstart_runner.py +629 -0
- package/hooks/stop_runner.py +408 -0
- package/hooks/userpromptsubmit_runner.py +722 -0
- package/hooks/windows-launcher.py +163 -0
- package/install-cloud.sh +23 -0
- package/package.json +29 -4
- package/skills/fleet-auditor/SKILL.md +150 -0
- package/skills/fleet-auditor/references/fleet-systems.md +167 -0
- package/skills/fleet-auditor/references/report-template.md +50 -0
- package/skills/fleet-auditor/references/waste-patterns.md +126 -0
- package/skills/fleet-auditor/scripts/fleet.py +3612 -0
- package/skills/fleet-auditor/scripts/shared.py +277 -0
- package/skills/resume-checkpoint/SKILL.md +52 -0
- package/skills/resume-checkpoint/scripts/pull_checkpoint.py +188 -0
- package/skills/token-coach/SKILL.md +129 -0
- package/skills/token-coach/assets/coach-dashboard-tab.html +45 -0
- package/skills/token-coach/examples/coaching-session-agentic.md +62 -0
- package/skills/token-coach/examples/coaching-session-heavy-setup.md +46 -0
- package/skills/token-coach/examples/coaching-session-new-project.md +38 -0
- package/skills/token-coach/references/agentic-systems.md +147 -0
- package/skills/token-coach/references/coach-patterns.md +120 -0
- package/skills/token-coach/references/coaching-scripts.md +146 -0
- package/skills/token-coach/references/deterministic-stages.md +102 -0
- package/skills/token-coach/references/keepwarm-consent.md +48 -0
- package/skills/token-coach/references/quick-reference.md +155 -0
- package/skills/token-dashboard/SKILL.md +72 -0
- package/skills/token-optimizer/SKILL.md +150 -0
- package/skills/token-optimizer/assets/dashboard.html +7251 -0
- package/skills/token-optimizer/examples/claude-md-optimized.md +40 -0
- package/skills/token-optimizer/examples/hooks-starter.json +179 -0
- package/skills/token-optimizer/examples/permissions-deny-template.json +24 -0
- package/skills/token-optimizer/pricing/prices.json +695 -0
- package/skills/token-optimizer/references/agent-prompts.md +687 -0
- package/skills/token-optimizer/references/cli-reference.md +92 -0
- package/skills/token-optimizer/references/codex-workflow.md +188 -0
- package/skills/token-optimizer/references/cursor-workflow.md +74 -0
- package/skills/token-optimizer/references/error-recovery.md +37 -0
- package/skills/token-optimizer/references/implementation-playbook.md +416 -0
- package/skills/token-optimizer/references/keepwarm-consent.md +48 -0
- package/skills/token-optimizer/references/opencode-workflow.md +72 -0
- package/skills/token-optimizer/references/optimization-checklist.md +714 -0
- package/skills/token-optimizer/references/phase0-setup.md +142 -0
- package/skills/token-optimizer/references/presentation-workflow.md +72 -0
- package/skills/token-optimizer/references/session-continuity.md +19 -0
- package/skills/token-optimizer/references/token-flow-architecture.md +629 -0
- package/skills/token-optimizer/scripts/activity_tracker.py +143 -0
- package/skills/token-optimizer/scripts/antigravity_doctor.py +548 -0
- package/skills/token-optimizer/scripts/antigravity_hook_bridge.py +511 -0
- package/skills/token-optimizer/scripts/antigravity_install.py +376 -0
- package/skills/token-optimizer/scripts/antigravity_proto.py +413 -0
- package/skills/token-optimizer/scripts/antigravity_session.py +346 -0
- package/skills/token-optimizer/scripts/antigravity_state.py +486 -0
- package/skills/token-optimizer/scripts/archive_result.py +1725 -0
- package/skills/token-optimizer/scripts/bash_compress.py +2490 -0
- package/skills/token-optimizer/scripts/bash_compress_hook.py +766 -0
- package/skills/token-optimizer/scripts/bash_hook.py +61 -0
- package/skills/token-optimizer/scripts/bash_whitelist.py +243 -0
- package/skills/token-optimizer/scripts/benchmark.py +1765 -0
- package/skills/token-optimizer/scripts/build_output_compress.py +776 -0
- package/skills/token-optimizer/scripts/cloud_kit.py +991 -0
- package/skills/token-optimizer/scripts/codex_command_compress.py +434 -0
- package/skills/token-optimizer/scripts/codex_compact_prompt.py +273 -0
- package/skills/token-optimizer/scripts/codex_doctor.py +454 -0
- package/skills/token-optimizer/scripts/codex_hook_bridge.py +311 -0
- package/skills/token-optimizer/scripts/codex_install.py +810 -0
- package/skills/token-optimizer/scripts/codex_io.py +119 -0
- package/skills/token-optimizer/scripts/codex_log_index.py +219 -0
- package/skills/token-optimizer/scripts/codex_models.py +34 -0
- package/skills/token-optimizer/scripts/codex_session.py +1186 -0
- package/skills/token-optimizer/scripts/codex_state.py +707 -0
- package/skills/token-optimizer/scripts/codex_statusline.py +303 -0
- package/skills/token-optimizer/scripts/command_filters.py +386 -0
- package/skills/token-optimizer/scripts/compression_backfill.py +600 -0
- package/skills/token-optimizer/scripts/compression_log.py +230 -0
- package/skills/token-optimizer/scripts/context_intel.py +331 -0
- package/skills/token-optimizer/scripts/context_pressure.py +115 -0
- package/skills/token-optimizer/scripts/copilot_doctor.py +406 -0
- package/skills/token-optimizer/scripts/copilot_hook_bridge.py +709 -0
- package/skills/token-optimizer/scripts/copilot_install.py +277 -0
- package/skills/token-optimizer/scripts/copilot_session.py +496 -0
- package/skills/token-optimizer/scripts/copilot_state.py +569 -0
- package/skills/token-optimizer/scripts/copilot_vscode.py +832 -0
- package/skills/token-optimizer/scripts/cowork_doctor.py +270 -0
- package/skills/token-optimizer/scripts/cowork_install.py +380 -0
- package/skills/token-optimizer/scripts/credential_patterns.py +1514 -0
- package/skills/token-optimizer/scripts/cursor_doctor.py +602 -0
- package/skills/token-optimizer/scripts/cursor_hook_bridge.py +843 -0
- package/skills/token-optimizer/scripts/cursor_install.py +423 -0
- package/skills/token-optimizer/scripts/cursor_session.py +303 -0
- package/skills/token-optimizer/scripts/cursor_state.py +348 -0
- package/skills/token-optimizer/scripts/delta_diff.py +110 -0
- package/skills/token-optimizer/scripts/detectors/__init__.py +2 -0
- package/skills/token-optimizer/scripts/detectors/bad_decomposition.py +78 -0
- package/skills/token-optimizer/scripts/detectors/cache_instability.py +504 -0
- package/skills/token-optimizer/scripts/detectors/looping.py +88 -0
- package/skills/token-optimizer/scripts/detectors/output_waste.py +143 -0
- package/skills/token-optimizer/scripts/detectors/overpowered.py +116 -0
- package/skills/token-optimizer/scripts/detectors/pdf_ingestion.py +52 -0
- package/skills/token-optimizer/scripts/detectors/registry.py +65 -0
- package/skills/token-optimizer/scripts/detectors/respond_to_bash.py +76 -0
- package/skills/token-optimizer/scripts/detectors/retry_churn.py +83 -0
- package/skills/token-optimizer/scripts/detectors/tool_cascade.py +62 -0
- package/skills/token-optimizer/scripts/detectors/wasteful_thinking.py +75 -0
- package/skills/token-optimizer/scripts/detectors/weak_model.py +55 -0
- package/skills/token-optimizer/scripts/detectors/websearch_routing.py +72 -0
- package/skills/token-optimizer/scripts/deterministic_candidates.py +1458 -0
- package/skills/token-optimizer/scripts/devin_compress.py +790 -0
- package/skills/token-optimizer/scripts/devin_doctor.py +403 -0
- package/skills/token-optimizer/scripts/devin_hooks.py +453 -0
- package/skills/token-optimizer/scripts/devin_install.py +1086 -0
- package/skills/token-optimizer/scripts/devin_source.py +119 -0
- package/skills/token-optimizer/scripts/devin_state.py +330 -0
- package/skills/token-optimizer/scripts/devin_verify.py +67 -0
- package/skills/token-optimizer/scripts/grok_doctor.py +560 -0
- package/skills/token-optimizer/scripts/grok_hook_bridge.py +699 -0
- package/skills/token-optimizer/scripts/grok_install.py +397 -0
- package/skills/token-optimizer/scripts/grok_session.py +317 -0
- package/skills/token-optimizer/scripts/grok_state.py +299 -0
- package/skills/token-optimizer/scripts/hermes_doctor.py +479 -0
- package/skills/token-optimizer/scripts/hermes_hook_bridge.py +285 -0
- package/skills/token-optimizer/scripts/hermes_install.py +668 -0
- package/skills/token-optimizer/scripts/hermes_session.py +691 -0
- package/skills/token-optimizer/scripts/hermes_state.py +448 -0
- package/skills/token-optimizer/scripts/hook_io.py +120 -0
- package/skills/token-optimizer/scripts/hook_runtime.py +713 -0
- package/skills/token-optimizer/scripts/injection.py +156 -0
- package/skills/token-optimizer/scripts/install_reconcile.py +952 -0
- package/skills/token-optimizer/scripts/measure.py +58739 -0
- package/skills/token-optimizer/scripts/outline.py +53 -0
- package/skills/token-optimizer/scripts/pipeline_analyzer.py +723 -0
- package/skills/token-optimizer/scripts/plugin_env.py +562 -0
- package/skills/token-optimizer/scripts/py_trust.py +80 -0
- package/skills/token-optimizer/scripts/quality_cache_gate.py +320 -0
- package/skills/token-optimizer/scripts/read_cache.py +2489 -0
- package/skills/token-optimizer/scripts/recovery_output.py +110 -0
- package/skills/token-optimizer/scripts/refetch_fingerprint.py +236 -0
- package/skills/token-optimizer/scripts/refetch_guard.py +244 -0
- package/skills/token-optimizer/scripts/routing_advisor.py +389 -0
- package/skills/token-optimizer/scripts/runtime_env.py +1893 -0
- package/skills/token-optimizer/scripts/session_store.py +979 -0
- package/skills/token-optimizer/scripts/spawn_utils.py +122 -0
- package/skills/token-optimizer/scripts/statusline.js +660 -0
- package/skills/token-optimizer/scripts/structure_map.py +2880 -0
- package/skills/token-optimizer/scripts/structure_map_ts.py +676 -0
- package/skills/token-optimizer/scripts/structure_replay.py +1556 -0
- package/skills/token-optimizer/scripts/thrash_guard.py +604 -0
- package/skills/token-optimizer/scripts/token_estimate.py +80 -0
- package/skills/token-optimizer/scripts/tool_search_diagnostic.py +61 -0
- package/skills/token-optimizer/scripts/utf8_io.py +219 -0
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
"""In-session model + effort routing for the current platform.
|
|
2
|
+
|
|
3
|
+
Given a task, recommend both the model and the reasoning effort to run it with,
|
|
4
|
+
expressed in the terms of the platform Token Optimizer is running on. The goal
|
|
5
|
+
is to stop reflexively spending the top model at max effort on trivial work, and
|
|
6
|
+
to stop under-powering work that actually matters.
|
|
7
|
+
|
|
8
|
+
Two ideas do the work:
|
|
9
|
+
|
|
10
|
+
1. Significance. A task is classified easy / standard / hard from local signals
|
|
11
|
+
only. Significance sets a FLOOR: the cheapest models and the lowest efforts
|
|
12
|
+
are reachable only for easy tasks. Nothing a standard or hard task scores can
|
|
13
|
+
drop it below that floor.
|
|
14
|
+
|
|
15
|
+
2. Native tiers. Each supported platform declares its own model ladder and its
|
|
16
|
+
own effort control. The recommendation is always phrased in the current
|
|
17
|
+
platform's names, and only the current platform's ladder is consulted.
|
|
18
|
+
|
|
19
|
+
Everything here is local, deterministic, and fails open to a safe middle
|
|
20
|
+
default (standard significance, never a cheap model) so a routing hiccup can
|
|
21
|
+
never block or mislead the caller.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
import re
|
|
25
|
+
|
|
26
|
+
# --- effort and tier ordering -------------------------------------------------
|
|
27
|
+
|
|
28
|
+
# Low to high. A floor names the minimum acceptable rung on each axis.
|
|
29
|
+
EFFORT_ORDER = ("minimal", "low", "medium", "high", "xhigh")
|
|
30
|
+
TIER_ORDER = ("budget", "mid", "capable", "frontier")
|
|
31
|
+
|
|
32
|
+
# Models that are only ever appropriate for genuinely easy work, named across
|
|
33
|
+
# platforms. Kept in one place so the floor, the guidance block, and the tests
|
|
34
|
+
# all read the same list.
|
|
35
|
+
VERY_CHEAP_MODELS = frozenset({"haiku", "luna"})
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def is_very_cheap(model):
|
|
39
|
+
"""True if a model name is one of the very-cheap models, matched on name
|
|
40
|
+
parts so a platform-qualified name (e.g. gpt-5.6-luna) is still caught."""
|
|
41
|
+
if not model:
|
|
42
|
+
return False
|
|
43
|
+
parts = re.split(r"[^a-z0-9]+", str(model).lower())
|
|
44
|
+
return any(p in VERY_CHEAP_MODELS for p in parts)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _rank(value, order, default):
|
|
48
|
+
try:
|
|
49
|
+
return order.index(value)
|
|
50
|
+
except ValueError:
|
|
51
|
+
return order.index(default)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _at_least(value, floor, order):
|
|
55
|
+
"""Return whichever of value/floor is higher on `order`."""
|
|
56
|
+
return value if _rank(value, order, order[0]) >= _rank(floor, order, order[0]) else floor
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
# --- per-platform native tables (U2) -----------------------------------------
|
|
60
|
+
|
|
61
|
+
# One row per supported platform. Each row lists its model ladder (tier -> model
|
|
62
|
+
# name, in the platform's own vocabulary), the effort levels the platform
|
|
63
|
+
# accepts, and how effort is applied ("native" = the platform has a real
|
|
64
|
+
# reasoning-effort control; "advisory" = state the effort in the prompt because
|
|
65
|
+
# the platform exposes no native control). Adding a platform is a new row; the
|
|
66
|
+
# engine below does not change.
|
|
67
|
+
ROUTING_TABLES = {
|
|
68
|
+
"claude": {
|
|
69
|
+
"models": {
|
|
70
|
+
"budget": "haiku",
|
|
71
|
+
"mid": "sonnet",
|
|
72
|
+
"capable": "opus",
|
|
73
|
+
"frontier": "opus",
|
|
74
|
+
},
|
|
75
|
+
"efforts": EFFORT_ORDER,
|
|
76
|
+
"effort_kind": "native", # thinking budget
|
|
77
|
+
"effort_knob": "thinking budget",
|
|
78
|
+
},
|
|
79
|
+
"codex": {
|
|
80
|
+
# GPT-5.6 three-tier family (Sol flagship > Terra workhorse > Luna cheap).
|
|
81
|
+
# Verified 2026-07-23 against OpenAI's GPT-5.6 preview + Codex CLI /model list.
|
|
82
|
+
"models": {
|
|
83
|
+
"budget": "gpt-5.6-luna",
|
|
84
|
+
"mid": "gpt-5.6-terra",
|
|
85
|
+
"capable": "gpt-5.6-sol",
|
|
86
|
+
"frontier": "gpt-5.6-sol",
|
|
87
|
+
},
|
|
88
|
+
"efforts": EFFORT_ORDER,
|
|
89
|
+
"effort_kind": "native", # model_reasoning_effort
|
|
90
|
+
"effort_knob": "model_reasoning_effort",
|
|
91
|
+
},
|
|
92
|
+
"opencode": {
|
|
93
|
+
# OpenCode runs user-configured models (any provider). The caller resolves
|
|
94
|
+
# the real ladder from opencode.json (`model` / `small_model`) and passes it
|
|
95
|
+
# to recommend(models=...); this table is the fallback when config is absent
|
|
96
|
+
# or unreadable. No native reasoning-effort knob -> advisory.
|
|
97
|
+
"models": {
|
|
98
|
+
"budget": "haiku",
|
|
99
|
+
"mid": "sonnet",
|
|
100
|
+
"capable": "opus",
|
|
101
|
+
"frontier": "opus",
|
|
102
|
+
},
|
|
103
|
+
"efforts": ("low", "medium", "high"),
|
|
104
|
+
"effort_kind": "advisory",
|
|
105
|
+
"effort_knob": "prompt directive",
|
|
106
|
+
},
|
|
107
|
+
"copilot": {
|
|
108
|
+
# Copilot CLI is a rotating multi-vendor catalog with "Auto" as the GA default and
|
|
109
|
+
# no native reasoning-effort knob (advisory). Ladder anchored to real, priced OpenAI
|
|
110
|
+
# slugs Copilot routes to; exact per-plan availability varies. Verified 2026-07-23.
|
|
111
|
+
"models": {
|
|
112
|
+
"budget": "gpt-5.4-mini",
|
|
113
|
+
"mid": "gpt-5.4",
|
|
114
|
+
"capable": "gpt-5.5",
|
|
115
|
+
"frontier": "gpt-5.6-sol",
|
|
116
|
+
},
|
|
117
|
+
"efforts": ("low", "medium", "high"),
|
|
118
|
+
"effort_kind": "advisory",
|
|
119
|
+
"effort_knob": "prompt directive",
|
|
120
|
+
},
|
|
121
|
+
"hermes": {
|
|
122
|
+
# Hermes is a Claude runtime (reads ~/.claude, models = Claude ladder), so the
|
|
123
|
+
# ladder below is correct, not a placeholder. Hermes exposes no per-tier model
|
|
124
|
+
# config; a single active model can be pinned via HERMES_MODEL, which the caller
|
|
125
|
+
# folds into the ladder. Advisory effort (no native reasoning-effort knob).
|
|
126
|
+
"models": {
|
|
127
|
+
"budget": "haiku",
|
|
128
|
+
"mid": "sonnet",
|
|
129
|
+
"capable": "opus",
|
|
130
|
+
"frontier": "opus",
|
|
131
|
+
},
|
|
132
|
+
"efforts": ("low", "medium", "high"),
|
|
133
|
+
"effort_kind": "advisory",
|
|
134
|
+
"effort_knob": "prompt directive",
|
|
135
|
+
},
|
|
136
|
+
"cowork": {
|
|
137
|
+
# Cowork IS Claude Code (same engine, in a VM), so the ladder mirrors
|
|
138
|
+
# "claude" exactly and effort is native (thinking budget). detect_runtime()
|
|
139
|
+
# returns "claude" inside Cowork; this row is here so a caller that has
|
|
140
|
+
# already resolved is_cowork() can pass runtime="cowork" and get a
|
|
141
|
+
# consistent table rather than falling through to the generic row.
|
|
142
|
+
"models": {
|
|
143
|
+
"budget": "haiku",
|
|
144
|
+
"mid": "sonnet",
|
|
145
|
+
"capable": "opus",
|
|
146
|
+
"frontier": "opus",
|
|
147
|
+
},
|
|
148
|
+
"efforts": EFFORT_ORDER,
|
|
149
|
+
"effort_kind": "native", # thinking budget
|
|
150
|
+
"effort_knob": "thinking budget",
|
|
151
|
+
},
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
# Used when the current platform has no table (keeps the engine total).
|
|
155
|
+
_GENERIC_ROW = {
|
|
156
|
+
"models": {"budget": "small", "mid": "standard", "capable": "large", "frontier": "large"},
|
|
157
|
+
"efforts": ("low", "medium", "high"),
|
|
158
|
+
"effort_kind": "advisory",
|
|
159
|
+
"effort_knob": "prompt directive",
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def platform_row(runtime):
|
|
164
|
+
try:
|
|
165
|
+
return ROUTING_TABLES.get(runtime, _GENERIC_ROW)
|
|
166
|
+
except TypeError:
|
|
167
|
+
# An unhashable runtime can't index the table; treat as unknown.
|
|
168
|
+
return _GENERIC_ROW
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
# --- significance + category classification (U1) -----------------------------
|
|
172
|
+
|
|
173
|
+
# Signals are matched as whole words or whole phrases, not raw substrings, so
|
|
174
|
+
# "author" never triggers "auth" and "reproduction" never triggers "production".
|
|
175
|
+
# Word forms are listed explicitly (authentication, migration, ...) rather than
|
|
176
|
+
# relying on prefixes.
|
|
177
|
+
_HARD_SIGNALS = (
|
|
178
|
+
"security", "auth", "authentication", "authorization", "oauth", "crypto",
|
|
179
|
+
"credential", "credentials", "production", "migrate", "migration", "migrating",
|
|
180
|
+
"schema change", "architecture", "redesign", "concurrent", "concurrency",
|
|
181
|
+
"deadlock", "race condition", "payment", "payments", "billing", "irreversible",
|
|
182
|
+
"data loss", "delete all", "drop table", "refactor across", "multi-file",
|
|
183
|
+
"distributed", "rollout", "backfill",
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
_EASY_SIGNALS = (
|
|
187
|
+
"typo", "rename", "one-liner", "one liner", "single file", "single-file",
|
|
188
|
+
"format", "reformat", "lint", "list", "count", "summarize", "summary",
|
|
189
|
+
"grep", "print", "echo", "comment", "docstring", "readme",
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
_CODE_SIGNALS = ("code", "function", "class", "bug", "refactor", "implement", "test", "compile", "api")
|
|
193
|
+
_REASONING_SIGNALS = ("why", "analyze", "design", "trade-off", "tradeoff", "compare", "evaluate", "plan", "prove")
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _compile(signals):
|
|
197
|
+
return tuple(re.compile(r"\b" + re.escape(s) + r"\b") for s in signals)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
_HARD_RE = _compile(_HARD_SIGNALS)
|
|
201
|
+
_EASY_RE = _compile(_EASY_SIGNALS)
|
|
202
|
+
_CODE_RE = _compile(_CODE_SIGNALS)
|
|
203
|
+
_REASONING_RE = _compile(_REASONING_SIGNALS)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _count_hits(text, patterns):
|
|
207
|
+
return sum(1 for p in patterns if p.search(text))
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def classify_significance(task):
|
|
211
|
+
"""Return (significance, confidence).
|
|
212
|
+
|
|
213
|
+
significance in {"easy","standard","hard"}, confidence in {"high","low"}.
|
|
214
|
+
Local and deterministic. On any error, returns the safe middle: standard/low.
|
|
215
|
+
"""
|
|
216
|
+
try:
|
|
217
|
+
text = (task or "")[:8192].lower()
|
|
218
|
+
hard = _count_hits(text, _HARD_RE)
|
|
219
|
+
easy = _count_hits(text, _EASY_RE)
|
|
220
|
+
|
|
221
|
+
# Structural nudges: very short with no hard signal leans easy; long or
|
|
222
|
+
# multi-step leans away from easy.
|
|
223
|
+
length = len(text)
|
|
224
|
+
steps = text.count("\n") + text.count(". ") + text.count("; ")
|
|
225
|
+
if length < 80 and hard == 0:
|
|
226
|
+
easy += 1
|
|
227
|
+
if length > 400 or steps >= 3:
|
|
228
|
+
hard += 1
|
|
229
|
+
|
|
230
|
+
if hard >= 1 and hard >= easy:
|
|
231
|
+
sig = "hard"
|
|
232
|
+
lead = hard - easy
|
|
233
|
+
elif easy >= 2 and hard == 0:
|
|
234
|
+
sig = "easy"
|
|
235
|
+
lead = easy
|
|
236
|
+
else:
|
|
237
|
+
sig = "standard"
|
|
238
|
+
lead = abs(easy - hard)
|
|
239
|
+
|
|
240
|
+
confidence = "high" if lead >= 2 else "low"
|
|
241
|
+
return sig, confidence
|
|
242
|
+
except Exception:
|
|
243
|
+
return "standard", "low"
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def classify_category(task):
|
|
247
|
+
"""Coarse task kind: 'reasoning' | 'code' | 'simple'. Never raises."""
|
|
248
|
+
try:
|
|
249
|
+
text = (task or "")[:8192].lower()
|
|
250
|
+
r = _count_hits(text, _REASONING_RE)
|
|
251
|
+
c = _count_hits(text, _CODE_RE)
|
|
252
|
+
if r == 0 and c == 0:
|
|
253
|
+
return "simple"
|
|
254
|
+
return "reasoning" if r > c else "code"
|
|
255
|
+
except Exception:
|
|
256
|
+
return "simple"
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
# --- floor + selection (U3) ---------------------------------------------------
|
|
260
|
+
|
|
261
|
+
# significance -> (base tier, base effort). This is both the target and the
|
|
262
|
+
# floor for that level; confidence can promote it, category can nudge effort up.
|
|
263
|
+
_BASE = {
|
|
264
|
+
"easy": ("budget", "low"),
|
|
265
|
+
"standard": ("mid", "medium"),
|
|
266
|
+
"hard": ("capable", "high"),
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
# Low confidence promotes only at the easy boundary, where a wrong call is
|
|
270
|
+
# expensive: a task scored easy but actually significant would otherwise get a
|
|
271
|
+
# cheap model and low effort. Standard and hard already sit above the cheap
|
|
272
|
+
# floor, so an uncertain one stays put rather than over-spending toward the top.
|
|
273
|
+
_PROMOTE = {"easy": "standard", "standard": "standard", "hard": "hard"}
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def baseline(significance, runtime, models=None):
|
|
277
|
+
"""Canonical (model, effort) for a significance level on a platform, with no
|
|
278
|
+
confidence or category adjustment. Used to describe the routing policy.
|
|
279
|
+
|
|
280
|
+
`models` optionally overrides the tier->model ladder (config-resolved, for
|
|
281
|
+
user-configured platforms); a malformed map falls back to the table."""
|
|
282
|
+
row = platform_row(runtime)
|
|
283
|
+
model_map = _coerce_model_map(models, row["models"])
|
|
284
|
+
tier, effort = _BASE.get(significance, _BASE["standard"])
|
|
285
|
+
model = model_map.get(tier, model_map["mid"])
|
|
286
|
+
if effort not in row["efforts"]:
|
|
287
|
+
higher = [e for e in EFFORT_ORDER if e in row["efforts"]
|
|
288
|
+
and _rank(e, EFFORT_ORDER, "medium") >= _rank(effort, EFFORT_ORDER, "medium")]
|
|
289
|
+
effort = higher[0] if higher else row["efforts"][-1]
|
|
290
|
+
return model, effort
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def _coerce_model_map(models, fallback):
|
|
294
|
+
"""Return a complete tier->model map, or `fallback` if `models` is unusable.
|
|
295
|
+
|
|
296
|
+
The engine stays pure: platforms whose models are user-configured (opencode,
|
|
297
|
+
hermes) have the caller read the config and pass the resolved map in. A
|
|
298
|
+
partial or malformed map falls back to the table so the floor never breaks.
|
|
299
|
+
"""
|
|
300
|
+
if not isinstance(models, dict):
|
|
301
|
+
return fallback
|
|
302
|
+
coerced = {}
|
|
303
|
+
for tier in TIER_ORDER:
|
|
304
|
+
value = models.get(tier)
|
|
305
|
+
if not (isinstance(value, str) and value.strip()):
|
|
306
|
+
return fallback
|
|
307
|
+
coerced[tier] = value.strip()
|
|
308
|
+
return coerced
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def recommend(task, runtime, models=None):
|
|
312
|
+
"""Return a recommendation dict for `task` on `runtime`.
|
|
313
|
+
|
|
314
|
+
`models` optionally overrides the platform's tier->model ladder with values
|
|
315
|
+
resolved from the platform's own config (used for user-configured platforms
|
|
316
|
+
like opencode/hermes); a malformed map is ignored and the table is used.
|
|
317
|
+
|
|
318
|
+
Keys: significance, confidence, category, tier, model, effort, effort_kind,
|
|
319
|
+
effort_knob, floor {min_tier, min_effort}, why. Never raises.
|
|
320
|
+
"""
|
|
321
|
+
try:
|
|
322
|
+
sig, conf = classify_significance(task)
|
|
323
|
+
cat = classify_category(task)
|
|
324
|
+
row = platform_row(runtime)
|
|
325
|
+
model_map = _coerce_model_map(models, row["models"])
|
|
326
|
+
|
|
327
|
+
effective = _PROMOTE[sig] if conf == "low" else sig
|
|
328
|
+
min_tier, min_effort = _BASE[effective]
|
|
329
|
+
|
|
330
|
+
# Base pick from the effective significance, then floor it.
|
|
331
|
+
base_tier, base_effort = _BASE[effective]
|
|
332
|
+
tier = _at_least(base_tier, min_tier, TIER_ORDER)
|
|
333
|
+
effort = _at_least(base_effort, min_effort, EFFORT_ORDER)
|
|
334
|
+
|
|
335
|
+
# Category nudge: thinking-heavy work gets one extra effort step, but
|
|
336
|
+
# only for non-easy tasks and never below the floor.
|
|
337
|
+
if effective != "easy" and cat in ("reasoning", "code"):
|
|
338
|
+
idx = min(_rank(effort, EFFORT_ORDER, "medium") + 1, len(EFFORT_ORDER) - 1)
|
|
339
|
+
effort = EFFORT_ORDER[idx]
|
|
340
|
+
|
|
341
|
+
# Clamp effort to what this platform accepts (advisory platforms carry a
|
|
342
|
+
# shorter ladder); keep it at or above the floor after clamping.
|
|
343
|
+
accepted = row["efforts"]
|
|
344
|
+
if effort not in accepted:
|
|
345
|
+
# nearest accepted at or above, else the platform's top.
|
|
346
|
+
higher = [e for e in EFFORT_ORDER if e in accepted
|
|
347
|
+
and _rank(e, EFFORT_ORDER, "medium") >= _rank(effort, EFFORT_ORDER, "medium")]
|
|
348
|
+
effort = higher[0] if higher else accepted[-1]
|
|
349
|
+
|
|
350
|
+
model = model_map.get(tier, model_map["mid"])
|
|
351
|
+
|
|
352
|
+
# Hard floor, enforced last: a very cheap model or a below-floor effort
|
|
353
|
+
# can only stand for an easy task. This cannot be reached by a standard
|
|
354
|
+
# or hard task because their floors already exclude it, but enforce it
|
|
355
|
+
# explicitly so the guarantee does not depend on the tables staying in
|
|
356
|
+
# sync.
|
|
357
|
+
if effective != "easy":
|
|
358
|
+
if is_very_cheap(model) or tier == "budget":
|
|
359
|
+
tier = _at_least(tier, "mid", TIER_ORDER)
|
|
360
|
+
model = model_map.get(tier, model_map["mid"])
|
|
361
|
+
effort = _at_least(effort, min_effort, EFFORT_ORDER)
|
|
362
|
+
if effort in ("minimal", "low"):
|
|
363
|
+
effort = _at_least(effort, "medium", EFFORT_ORDER)
|
|
364
|
+
if effort not in accepted:
|
|
365
|
+
effort = accepted[-1]
|
|
366
|
+
|
|
367
|
+
why = f"{sig} task ({conf} confidence, {cat}) -> {tier} model at {effort} effort"
|
|
368
|
+
return {
|
|
369
|
+
"significance": sig,
|
|
370
|
+
"confidence": conf,
|
|
371
|
+
"category": cat,
|
|
372
|
+
"tier": tier,
|
|
373
|
+
"model": model,
|
|
374
|
+
"effort": effort,
|
|
375
|
+
"effort_kind": row["effort_kind"],
|
|
376
|
+
"effort_knob": row["effort_knob"],
|
|
377
|
+
"floor": {"min_tier": min_tier, "min_effort": min_effort},
|
|
378
|
+
"why": why,
|
|
379
|
+
}
|
|
380
|
+
except Exception:
|
|
381
|
+
# Total fail-open: a safe, non-cheap middle recommendation.
|
|
382
|
+
row = platform_row(runtime)
|
|
383
|
+
return {
|
|
384
|
+
"significance": "standard", "confidence": "low", "category": "simple",
|
|
385
|
+
"tier": "mid", "model": row["models"]["mid"], "effort": "medium",
|
|
386
|
+
"effort_kind": row["effort_kind"], "effort_knob": row["effort_knob"],
|
|
387
|
+
"floor": {"min_tier": "mid", "min_effort": "medium"},
|
|
388
|
+
"why": "fell back to a safe default",
|
|
389
|
+
}
|