opencode-model-router 1.12.0 → 1.12.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.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,41 @@ All notable changes to this project are documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.12.1] - 2026-09-23
9
+
10
+ The delegate instruction filter introduced in 1.12.0 never removed anything in a live
11
+ session, and the change that makes it take effect would, on its own, have deleted
12
+ unrelated system-prompt text. Both are fixed together.
13
+
14
+ ### Fixed
15
+
16
+ - **`delegateInstructions` stripping now reaches the request.** The filter ended with
17
+ `output.system = output.system.flatMap(...)`, which rebinds the hook's output
18
+ property. opencode keeps its own reference to the array it passes to
19
+ `experimental.chat.system.transform` and reads that reference back after the hook
20
+ returns, so every removal was computed and then discarded. Protocol injection had
21
+ always worked only because it used `push`, which mutates the same array. The
22
+ filter now writes its result back with `splice` over the original array and never
23
+ rebinds `output.system`. The 1.12.0 tests passed because they asserted on the
24
+ rebound property rather than on the array the host still holds.
25
+
26
+ - **Removal is bounded to the instruction file's own text.** A block was defined as
27
+ running from its `Instructions from:` marker to the next marker or the end of the
28
+ entry, which was correct only while each instruction file arrived as its own array
29
+ element. The runtime joins the agent prompt, every instruction file and any
30
+ trailing system text into one string, so the last block extended to the end of
31
+ everything: a reproduction with a trailing `<mcp_instructions>` block lost the whole
32
+ MCP section along with `CLAUDE.md`, shrinking the prompt from 839 to 465 characters.
33
+ The filter now reads the file named by the marker (cached per path and revalidated
34
+ against its modification time) and removes exactly the marker line plus the longest
35
+ rendering of the file's contents — as read, LF-to-CRLF, CRLF-to-LF, or any of those
36
+ with trailing whitespace trimmed — that is an exact prefix of the block, keeping
37
+ whatever follows. If the file cannot be read or does not match, the block is kept
38
+ whole: leaving an instruction in place is unhelpful, deleting an unknown span of
39
+ the system prompt is not recoverable. The reader is an optional fourth parameter
40
+ (`InstructionFileReader`), so tests exercise the logic without touching disk; the
41
+ plugin's call site is unchanged.
42
+
8
43
  ## [1.12.0] - 2026-09-23
9
44
 
10
45
  A subagent was refusing dispatched work outright — returning `ESCALATE: The Task tool
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-model-router",
3
- "version": "1.12.0",
3
+ "version": "1.12.1",
4
4
  "description": "OpenCode plugin that routes tasks to tiered subagents (fast/medium/heavy) based on complexity",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -1,7 +1,16 @@
1
+ import { readFileSync, statSync } from "node:fs";
1
2
  import type { RouterConfig } from "./config";
2
3
 
3
4
  export type DelegateInstructionsPolicy = "strip-global" | "strip-all" | "keep";
4
5
 
6
+ /**
7
+ * Returns an instruction file's contents, or `undefined` when the file cannot be
8
+ * read for any reason. Injectable so tests need not touch the filesystem.
9
+ */
10
+ export type InstructionFileReader = (absolutePath: string) => string | undefined;
11
+
12
+ const MARKER = /^Instructions from:[^\S\r\n]*(\S[^\r\n]*)/;
13
+
5
14
  /**
6
15
  * Strip trailing forward slashes without a regex.
7
16
  *
@@ -16,6 +25,74 @@ function stripTrailingSlashes(value: string): string {
16
25
  return value.slice(0, end);
17
26
  }
18
27
 
28
+ const fileCache = new Map<string, { mtimeMs: number; size: number; contents: string }>();
29
+
30
+ /**
31
+ * Real-filesystem reader. The system transform fires on every LLM step, so
32
+ * contents are cached per path and revalidated against mtime and size.
33
+ */
34
+ export const readInstructionFile: InstructionFileReader = (absolutePath) => {
35
+ try {
36
+ const stat = statSync(absolutePath);
37
+ if (!stat.isFile()) return undefined;
38
+ const cached = fileCache.get(absolutePath);
39
+ if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) {
40
+ return cached.contents;
41
+ }
42
+ const contents = readFileSync(absolutePath, "utf8");
43
+ fileCache.set(absolutePath, { mtimeMs: stat.mtimeMs, size: stat.size, contents });
44
+ return contents;
45
+ } catch {
46
+ // Missing, unreadable, or not a file: the caller keeps the section intact.
47
+ fileCache.delete(absolutePath);
48
+ return undefined;
49
+ }
50
+ };
51
+
52
+ /**
53
+ * Length of the longest candidate rendering of `contents` that is an exact
54
+ * prefix of `body`, or `undefined` when none is. Compared literally against the
55
+ * original body so the removal length needs no index mapping.
56
+ */
57
+ function matchedSpan(body: string, contents: string): number | undefined {
58
+ const renderings = [
59
+ contents,
60
+ contents.replace(/\r?\n/g, "\r\n"),
61
+ contents.replace(/\r\n/g, "\n"),
62
+ ];
63
+ let best: number | undefined;
64
+ for (const candidate of [...renderings, ...renderings.map((r) => r.trimEnd())]) {
65
+ if ((best === undefined || candidate.length > best) && body.startsWith(candidate)) {
66
+ best = candidate.length;
67
+ }
68
+ }
69
+ return best;
70
+ }
71
+
72
+ /**
73
+ * Remove the marker line and the named file's own text from `section`, keeping
74
+ * whatever follows. The host joins every instruction file and trailing system
75
+ * text into one string, so the last section's end is not the file's end. If
76
+ * the extent cannot be established from the file, the section is returned
77
+ * unchanged: keeping an instruction is harmless, deleting unknown text is not.
78
+ */
79
+ function removeInstructionBlock(section: string, path: string, read: InstructionFileReader): string {
80
+ let contents: string | undefined;
81
+ try {
82
+ contents = read(path);
83
+ } catch {
84
+ // A throwing reader means the extent is unknown; keep the section.
85
+ contents = undefined;
86
+ }
87
+ if (typeof contents !== "string") return section;
88
+ const newline = section.indexOf("\n");
89
+ const body = newline === -1 ? "" : section.slice(newline + 1);
90
+ const span = matchedSpan(body, contents);
91
+ if (span === undefined) return section;
92
+ const rest = body.slice(span);
93
+ return rest.trim() ? rest : "";
94
+ }
95
+
19
96
  /**
20
97
  * opencode injects instruction files into every session, including children.
21
98
  * A global orchestrator persona can tell delegates to "fire the fast agent via
@@ -23,40 +100,50 @@ function stripTrailingSlashes(value: string): string {
23
100
  * though those delegates have no task tool and must do the dispatched work.
24
101
  * Keep project-local files by default: they usually contain coding conventions
25
102
  * delegates need. With no project directory, every instruction file is global.
26
- * Marker-delimited sections extend to the next marker or the end of the entry.
103
+ *
104
+ * A section runs from its marker to the next marker or the end of the entry;
105
+ * only the marker line plus the named file's contents are removed from it.
106
+ * The host holds its own reference to `output.system` and reads it back after
107
+ * the hook, so the array is mutated in place and never rebound.
27
108
  */
28
109
  export function stripDelegateInstructions(
29
110
  output: { system: string[] },
30
111
  cfg: RouterConfig,
31
112
  projectDirectory: string | undefined,
113
+ readFile: InstructionFileReader = readInstructionFile,
32
114
  ): void {
33
115
  try {
34
116
  const policy = cfg.delegateInstructions ?? "strip-global";
35
117
  if (policy === "keep") return;
118
+ const system = output.system;
119
+ if (!Array.isArray(system)) return;
36
120
 
37
121
  const normalize = (path: string): string =>
38
122
  stripTrailingSlashes(path.trim().replace(/\\/g, "/").toLowerCase());
39
123
  const project = projectDirectory ? normalize(projectDirectory) : undefined;
40
- output.system = output.system.flatMap((entry) => {
124
+ let changed = false;
125
+ const next = system.flatMap((entry) => {
41
126
  if (typeof entry !== "string") return [entry];
42
127
  // Horizontal whitespace only: a pathless marker must not consume the
43
128
  // following line as its path. Preserve original line endings verbatim.
44
129
  const sections = entry.split(/(?=^Instructions from:[^\S\r\n]*\S[^\r\n]*$)/m);
45
130
  let removed = false;
46
- const kept = sections.filter((section) => {
47
- const marker = /^Instructions from:[^\S\r\n]*(\S[^\r\n]*)/.exec(section);
48
- if (!marker) return true;
131
+ const kept = sections.map((section) => {
132
+ const marker = MARKER.exec(section);
133
+ if (!marker) return section;
49
134
  const path = normalize(marker[1]!);
50
135
  const local = project !== undefined &&
51
136
  (path === project || path.startsWith(`${project}/`));
52
- if (policy === "strip-all" || !local) {
53
- removed = true;
54
- return false;
55
- }
56
- return true;
137
+ if (policy !== "strip-all" && local) return section;
138
+ const rest = removeInstructionBlock(section, marker[1]!.trim(), readFile);
139
+ if (rest !== section) removed = true;
140
+ return rest;
57
141
  }).join("");
58
- return removed && !kept.trim() ? [] : [kept];
142
+ if (!removed) return [entry];
143
+ changed = true;
144
+ return kept.trim() ? [kept] : [];
59
145
  });
146
+ if (changed) system.splice(0, system.length, ...next);
60
147
  } catch {
61
148
  // Fail soft: retain the original array if malformed runtime input prevents
62
149
  // computing the replacement. Instruction filtering must not break a turn.