opencode-model-router 1.1.12 → 1.1.14
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 +63 -0
- package/package.json +1 -1
- package/src/index.ts +190 -2
package/README.md
CHANGED
|
@@ -487,6 +487,69 @@ Each tier (`@fast`, `@medium`, `@heavy`) has a system prompt that describes its
|
|
|
487
487
|
}
|
|
488
488
|
```
|
|
489
489
|
|
|
490
|
+
### Claude-model adversarial prefixes (automatic)
|
|
491
|
+
|
|
492
|
+
Anthropic models (served directly via `anthropic/*` or routed through other providers as `*/claude-*`) ship with a large cached system prompt that primes them toward broad exploratory Read/Grep/Glob behavior. When such a prompt sits in front of your router instructions, primacy bias and prompt caching weaken the router's authority — subagents ignore caps, orchestrators run read-only work themselves instead of dispatching.
|
|
493
|
+
|
|
494
|
+
To counteract this, the router **automatically prepends an adversarial opener** to:
|
|
495
|
+
|
|
496
|
+
- The tier prompt for any tier whose `model` matches a Claude identifier
|
|
497
|
+
- The orchestrator delegation protocol when the session model is a Claude identifier
|
|
498
|
+
|
|
499
|
+
Detection is by model string, not preset. A `hybrid` preset that mixes providers (e.g. `openai/*` for @fast, `anthropic/*` for @medium and @heavy) gets the override only on its Claude-backed tiers.
|
|
500
|
+
|
|
501
|
+
**Tone assignment:**
|
|
502
|
+
|
|
503
|
+
| Target | Tone | Opener label |
|
|
504
|
+
|--------|------|--------------|
|
|
505
|
+
| `@fast` (Claude) | Scoping — conversational | `SCOPE NOTE` |
|
|
506
|
+
| `@medium` (Claude) | Scoping — conversational | `SCOPE NOTE` |
|
|
507
|
+
| `@heavy` (Claude) | Override — firm | `AUTHORITY OVERRIDE` |
|
|
508
|
+
| Orchestrator (Claude) | Override — firm | `AUTHORITY OVERRIDE` |
|
|
509
|
+
|
|
510
|
+
`@heavy` and the orchestrator use the firmer tone because that's where reconnaissance loops were worst in observed sessions. `@fast` and `@medium` use a softer scoping note to avoid over-correcting legitimate multi-read tasks.
|
|
511
|
+
|
|
512
|
+
**Detection rules:**
|
|
513
|
+
|
|
514
|
+
- `anthropic/<anything>` → Claude
|
|
515
|
+
- `<provider>/claude-<anything>` (e.g. `github-copilot/claude-sonnet-4-6`) → Claude
|
|
516
|
+
- `<provider>/<namespace>.claude-<anything>` (e.g. `bedrock/us.anthropic.claude-3-5-sonnet-...`) → Claude
|
|
517
|
+
- Everything else → untouched
|
|
518
|
+
|
|
519
|
+
No configuration is needed — the prefixes are always applied for Claude-backed tiers. If you want to disable them, override the tier's `prompt` field (per-tier overrides replace the whole prompt, including the prefix).
|
|
520
|
+
|
|
521
|
+
### Anti-narration guardrail (Claude models)
|
|
522
|
+
|
|
523
|
+
Thinking-enabled Claude models (especially Sonnet with the `max` variant) sometimes produce progress narration instead of actual work — phrasings like *"Still writing the X function..."*, *"Now I'll implement Y..."*, *"Let me add Z..."* — without the X/Y/Z ever appearing. This is a known thinking-mode failure pattern.
|
|
524
|
+
|
|
525
|
+
The router counters this on two layers:
|
|
526
|
+
|
|
527
|
+
**1. Prompt-level clause (prevention).** A dedicated `ANTI-NARRATION` block is appended to every Claude-backed tier prompt and to the Claude-backed orchestrator delegation protocol. It names the forbidden phrasings explicitly and requires concrete output to follow any such phrase. A carve-out preserves legitimate explanation/plan requests from the user.
|
|
528
|
+
|
|
529
|
+
**2. Post-hoc detector (telemetry).** An `experimental.text.complete` hook scans completed text for narration regex patterns. On match, it:
|
|
530
|
+
|
|
531
|
+
- Logs a warning to the plugin console:
|
|
532
|
+
```
|
|
533
|
+
[model-router] narration detected (session abc123): "Still writing the auth", "Now I'll add the tests"
|
|
534
|
+
```
|
|
535
|
+
- Appends a visible banner to the text as it's rendered to the user:
|
|
536
|
+
```
|
|
537
|
+
[⚠ narration detected: "Still writing the auth", "Now I'll add the tests"]
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
The detector is not blocking — plugin hooks cannot modify tokens mid-stream. It signals post-hoc so you can spot the pattern in the UI and in logs, and judge whether the prompt-level clause is holding up.
|
|
541
|
+
|
|
542
|
+
Detected patterns (conservative set to minimize false positives):
|
|
543
|
+
|
|
544
|
+
- `Still (writing|implementing|working on|...) the X`
|
|
545
|
+
- `Now (I'll)? (write|implement|add|...) the X`
|
|
546
|
+
- `Let me (write|implement|add|...) (the )? X`
|
|
547
|
+
- `I'll (now)? (write|implement|...) the X`
|
|
548
|
+
- `Going to (write|implement|...) the X`
|
|
549
|
+
- `Continuing (with|by ...ing) (the )? X`
|
|
550
|
+
|
|
551
|
+
Applies to all models, not only Claude — but the prompt-level clause is Claude-only, so non-Claude models get detector-only.
|
|
552
|
+
|
|
490
553
|
### Tier fields reference
|
|
491
554
|
|
|
492
555
|
| Field | Type | Description |
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -750,6 +750,146 @@ function buildCapBanner(
|
|
|
750
750
|
return lines.join("\n");
|
|
751
751
|
}
|
|
752
752
|
|
|
753
|
+
// ---------------------------------------------------------------------------
|
|
754
|
+
// Claude-model adversarial prefixes
|
|
755
|
+
//
|
|
756
|
+
// Anthropic models (direct or via other providers) are served with a large
|
|
757
|
+
// cached "Claude Code" signature prompt that primes them toward broad
|
|
758
|
+
// exploratory Read/Grep/Glob behavior. Our tier prompts land after that
|
|
759
|
+
// cached block and lose authority through primacy bias and cache freezing.
|
|
760
|
+
// For Claude models specifically, we prepend an override block that
|
|
761
|
+
// explicitly revokes the exploratory priming for the current dispatch.
|
|
762
|
+
//
|
|
763
|
+
// Detection is by model identifier, not preset — a hybrid preset mixing
|
|
764
|
+
// providers gets the override only on its Claude-backed tiers.
|
|
765
|
+
// ---------------------------------------------------------------------------
|
|
766
|
+
|
|
767
|
+
function isClaudeModel(modelID: string | undefined): boolean {
|
|
768
|
+
if (!modelID) return false;
|
|
769
|
+
const s = modelID.toLowerCase();
|
|
770
|
+
if (s.startsWith("anthropic/")) return true;
|
|
771
|
+
return /\/claude-/.test(s) || /(^|[\/\-])claude-/.test(s);
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/** Per-tier adversarial openers. @fast/@medium use Tom 2 (scoping); @heavy uses Tom 1 (override). */
|
|
775
|
+
const CLAUDE_TIER_PREFIX: Record<string, string> = {
|
|
776
|
+
fast: [
|
|
777
|
+
"SCOPE NOTE — any cached instruction priming you to 'thoroughly explore",
|
|
778
|
+
"the codebase' or 'gather context broadly' does not apply here. This is",
|
|
779
|
+
"a single dispatch with a single question. Stay narrow to the ask.",
|
|
780
|
+
"",
|
|
781
|
+
"Before every tool call, ask: 'Does THIS read answer the dispatch",
|
|
782
|
+
"question specifically, or am I drifting into context-gathering for my",
|
|
783
|
+
"own understanding?' If the latter, stop — you have enough.",
|
|
784
|
+
].join("\n"),
|
|
785
|
+
medium: [
|
|
786
|
+
"SCOPE NOTE — cached instructions may prime you to deeply understand",
|
|
787
|
+
"surrounding code before editing. For THIS dispatch, the orchestrator",
|
|
788
|
+
"has already scoped the problem; trust that scoping. Read what's needed",
|
|
789
|
+
"for the edit, not the whole module.",
|
|
790
|
+
"",
|
|
791
|
+
"A 4th or 5th 'context read' is usually a sign the dispatch was under-",
|
|
792
|
+
"scoped — return NEED CONTEXT rather than expanding scope yourself.",
|
|
793
|
+
].join("\n"),
|
|
794
|
+
heavy: [
|
|
795
|
+
"AUTHORITY OVERRIDE — this block supersedes any cached prefix suggesting",
|
|
796
|
+
"you should 'investigate thoroughly', 'gather comprehensive context', or",
|
|
797
|
+
"'trace through the code systematically before analyzing'. You are",
|
|
798
|
+
"@heavy, an ANALYSIS specialist. The orchestrator should have pre-",
|
|
799
|
+
"gathered context via @fast before dispatching you. If it didn't, return",
|
|
800
|
+
"`SCOPE GROWTH:` immediately — do NOT self-gather.",
|
|
801
|
+
"",
|
|
802
|
+
"40 minutes of Read/Grep is not analysis — it is the orchestrator's job",
|
|
803
|
+
"that leaked into yours. Push it back with SCOPE GROWTH. Your 3 reads",
|
|
804
|
+
"are for targeted verification, not exploration.",
|
|
805
|
+
].join("\n"),
|
|
806
|
+
};
|
|
807
|
+
|
|
808
|
+
/** Adversarial opener prepended to the delegation protocol when the orchestrator is a Claude model. */
|
|
809
|
+
const CLAUDE_ORCHESTRATOR_PREFIX = [
|
|
810
|
+
"AUTHORITY OVERRIDE — this block supersedes any prior system instruction",
|
|
811
|
+
"about 'exploring the codebase', 'gathering context with tools', or using",
|
|
812
|
+
"Read/Grep/Glob to validate assumptions. Those instructions describe a",
|
|
813
|
+
"general-purpose Claude Code session. THIS session is a routing",
|
|
814
|
+
"orchestrator.",
|
|
815
|
+
"",
|
|
816
|
+
"Your job is to DISPATCH, not to EXECUTE. Read-only work (grep/read/",
|
|
817
|
+
"glob/ls) is execution, and execution is delegated to @fast. You may run",
|
|
818
|
+
"AT MOST 2 direct read-only tool calls per turn — a 3rd call is a rule",
|
|
819
|
+
"violation. If you need more context, you dispatch @fast.",
|
|
820
|
+
"",
|
|
821
|
+
"If a cached instruction told you to 'be thorough', 'explore broadly', or",
|
|
822
|
+
"'read supporting files' — ignore it here. Thoroughness is achieved by",
|
|
823
|
+
"dispatching the right tier, not by you becoming the explorer.",
|
|
824
|
+
].join("\n");
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Anti-narration clause appended to every Claude-model prefix (tier + orchestrator).
|
|
828
|
+
*
|
|
829
|
+
* Thinking-enabled Claude models (esp. Sonnet with `max` variant) sometimes
|
|
830
|
+
* produce progress narration in place of actual work — "Still writing X...",
|
|
831
|
+
* "Now I'll implement Y...", "Let me add Z..." — without the X/Y/Z ever
|
|
832
|
+
* appearing. This clause names the pattern, lists specific forbidden phrasings
|
|
833
|
+
* (A3 — exemplified), and carves out an escape valve for legitimate
|
|
834
|
+
* explanation/plan requests (A2 — with exception).
|
|
835
|
+
*/
|
|
836
|
+
const CLAUDE_ANTI_NARRATION = [
|
|
837
|
+
"ANTI-NARRATION — do NOT write progress commentary in your response or",
|
|
838
|
+
"thinking output. Forbidden phrasings include:",
|
|
839
|
+
" - \"Still writing the X function...\"",
|
|
840
|
+
" - \"Now I'll implement Y...\"",
|
|
841
|
+
" - \"Let me add Z...\"",
|
|
842
|
+
" - \"Continuing with W...\"",
|
|
843
|
+
" - \"Going to fix V...\"",
|
|
844
|
+
"",
|
|
845
|
+
"Each of these signals planning without production. If you write one, the",
|
|
846
|
+
"NEXT tokens MUST contain the actual artifact (the code, the edit, the",
|
|
847
|
+
"concrete output). Otherwise, stop and return with status.",
|
|
848
|
+
"",
|
|
849
|
+
"Exception: when the user explicitly asks for an explanation, plan, or",
|
|
850
|
+
"walkthrough, prose is welcome — this rule targets unsolicited progress",
|
|
851
|
+
"narration during code and implementation tasks.",
|
|
852
|
+
].join("\n");
|
|
853
|
+
|
|
854
|
+
// ---------------------------------------------------------------------------
|
|
855
|
+
// Narration detector (telemetry — logs + appends banner)
|
|
856
|
+
// ---------------------------------------------------------------------------
|
|
857
|
+
|
|
858
|
+
/** Regex patterns that flag progress narration without production. */
|
|
859
|
+
const NARRATION_PATTERNS: RegExp[] = [
|
|
860
|
+
// "Still writing the X", "Still implementing the Y"
|
|
861
|
+
/\bstill\s+(writing|implementing|working on|adding|creating|fixing|building|refactoring|handling)\s+(the\s+)?\w+/gi,
|
|
862
|
+
// "Now I'll write the X", "Now writing the Y"
|
|
863
|
+
/\bnow\s+(i['']ll\s+)?(writ|implement|add|creat|work|fix|build|handl|refactor|updat|mov)\w*\s+(the\s+)?\w+/gi,
|
|
864
|
+
// "Let me write X", "Let me implement Y"
|
|
865
|
+
/\blet\s+me\s+(write|implement|add|create|fix|build|handle|refactor|work on|move|update|set up)\s+(the\s+)?\w+/gi,
|
|
866
|
+
// "I'll write the X", "I'll now implement Y"
|
|
867
|
+
/\bi['']ll\s+(now\s+)?(write|implement|add|create|fix|build|handle|refactor|set up|work on|move|update)\s+(the\s+)?\w+/gi,
|
|
868
|
+
// "Going to fix the X"
|
|
869
|
+
/\bgoing\s+to\s+(write|implement|add|create|fix|build|handle|refactor|set up|work on|move|update)\s+(the\s+)?\w+/gi,
|
|
870
|
+
// "Continuing with X", "Continuing by adding Y"
|
|
871
|
+
/\bcontinuing\s+(with|by\s+\w+ing)\s+(the\s+)?\w+/gi,
|
|
872
|
+
];
|
|
873
|
+
|
|
874
|
+
/** Returns matched narration phrases, deduped and capped. Empty array = no narration detected. */
|
|
875
|
+
function detectNarration(text: string): string[] {
|
|
876
|
+
if (text.length < 20) return [];
|
|
877
|
+
const seen = new Set<string>();
|
|
878
|
+
const out: string[] = [];
|
|
879
|
+
for (const pattern of NARRATION_PATTERNS) {
|
|
880
|
+
const matches = text.match(pattern);
|
|
881
|
+
if (!matches) continue;
|
|
882
|
+
for (const m of matches) {
|
|
883
|
+
const trimmed = m.trim().toLowerCase();
|
|
884
|
+
if (seen.has(trimmed)) continue;
|
|
885
|
+
seen.add(trimmed);
|
|
886
|
+
out.push(m.trim());
|
|
887
|
+
if (out.length >= 5) return out;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
return out;
|
|
891
|
+
}
|
|
892
|
+
|
|
753
893
|
// ---------------------------------------------------------------------------
|
|
754
894
|
// Plugin
|
|
755
895
|
// ---------------------------------------------------------------------------
|
|
@@ -840,6 +980,31 @@ const ModelRouterPlugin: Plugin = async (_ctx: PluginInput) => {
|
|
|
840
980
|
output.output = existing ? `${existing}\n\n${banner}` : banner;
|
|
841
981
|
},
|
|
842
982
|
|
|
983
|
+
// -----------------------------------------------------------------------
|
|
984
|
+
// Narration detector — flags progress-commentary-without-production.
|
|
985
|
+
//
|
|
986
|
+
// Fires per completed text part. Scans for narration patterns; if any
|
|
987
|
+
// match, logs a warning to the plugin console and appends a visible
|
|
988
|
+
// banner to the text so the user sees the detection in the UI. This is
|
|
989
|
+
// telemetry, not blocking — we cannot modify mid-stream generation, only
|
|
990
|
+
// post-hoc signal.
|
|
991
|
+
// -----------------------------------------------------------------------
|
|
992
|
+
"experimental.text.complete": async (input: any, output: any) => {
|
|
993
|
+
const text = output?.text;
|
|
994
|
+
if (typeof text !== "string" || text.length < 20) return;
|
|
995
|
+
|
|
996
|
+
const found = detectNarration(text);
|
|
997
|
+
if (found.length === 0) return;
|
|
998
|
+
|
|
999
|
+
const quoted = found
|
|
1000
|
+
.map((m) => `"${m.slice(0, 60)}${m.length > 60 ? "…" : ""}"`)
|
|
1001
|
+
.join(", ");
|
|
1002
|
+
console.warn(
|
|
1003
|
+
`[model-router] narration detected (session ${input?.sessionID ?? "?"}): ${quoted}`,
|
|
1004
|
+
);
|
|
1005
|
+
output.text = `${text}\n\n[⚠ narration detected: ${quoted}]`;
|
|
1006
|
+
},
|
|
1007
|
+
|
|
843
1008
|
// -----------------------------------------------------------------------
|
|
844
1009
|
// Register tier agents + commands at load time
|
|
845
1010
|
// -----------------------------------------------------------------------
|
|
@@ -850,12 +1015,24 @@ const ModelRouterPlugin: Plugin = async (_ctx: PluginInput) => {
|
|
|
850
1015
|
// Resolve prompt: per-tier override wins; otherwise fall back to global tierPrompts[name].
|
|
851
1016
|
const resolvedPrompt = tier.prompt ?? cfg.tierPrompts?.[name];
|
|
852
1017
|
|
|
1018
|
+
// For Claude-backed tiers, prepend an adversarial opener that revokes
|
|
1019
|
+
// the cached "Claude Code exploratory agent" priming for this dispatch.
|
|
1020
|
+
// Detection is by model string, so hybrid presets get the override
|
|
1021
|
+
// only on their Claude-backed tiers.
|
|
1022
|
+
const claudePrefix = isClaudeModel(tier.model)
|
|
1023
|
+
? `${CLAUDE_TIER_PREFIX[name]}\n\n${CLAUDE_ANTI_NARRATION}`
|
|
1024
|
+
: undefined;
|
|
1025
|
+
const finalPrompt =
|
|
1026
|
+
claudePrefix && resolvedPrompt
|
|
1027
|
+
? `${claudePrefix}\n\n---\n\n${resolvedPrompt}`
|
|
1028
|
+
: resolvedPrompt;
|
|
1029
|
+
|
|
853
1030
|
const agentDef: Record<string, unknown> = {
|
|
854
1031
|
model: tier.model,
|
|
855
1032
|
mode: "subagent",
|
|
856
1033
|
description: tier.description,
|
|
857
1034
|
maxSteps: tier.steps,
|
|
858
|
-
prompt:
|
|
1035
|
+
prompt: finalPrompt,
|
|
859
1036
|
color: tier.color,
|
|
860
1037
|
};
|
|
861
1038
|
|
|
@@ -936,7 +1113,18 @@ const ModelRouterPlugin: Plugin = async (_ctx: PluginInput) => {
|
|
|
936
1113
|
const sessionID = _input?.sessionID;
|
|
937
1114
|
if (sessionID && subagentSessionIDs.has(sessionID)) return;
|
|
938
1115
|
|
|
939
|
-
|
|
1116
|
+
// For Claude-backed orchestrators, prepend an adversarial opener that
|
|
1117
|
+
// revokes the cached "Claude Code explorer" priming for the routing
|
|
1118
|
+
// role. Detection is by orchestrator model, not preset.
|
|
1119
|
+
const providerID = _input?.model?.providerID ?? "";
|
|
1120
|
+
const modelID = _input?.model?.modelID ?? "";
|
|
1121
|
+
const orchestratorModel = providerID && modelID ? `${providerID}/${modelID}` : modelID;
|
|
1122
|
+
const delegationProtocol = buildDelegationProtocol(cfg);
|
|
1123
|
+
const finalProtocol = isClaudeModel(orchestratorModel)
|
|
1124
|
+
? `${CLAUDE_ORCHESTRATOR_PREFIX}\n\n${CLAUDE_ANTI_NARRATION}\n\n---\n\n${delegationProtocol}`
|
|
1125
|
+
: delegationProtocol;
|
|
1126
|
+
|
|
1127
|
+
output.system.push(finalProtocol);
|
|
940
1128
|
},
|
|
941
1129
|
|
|
942
1130
|
// -----------------------------------------------------------------------
|