dsh-subagent-default-model 2.1.1 → 2.1.4
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 +73 -0
- package/README.en.md +7 -2
- package/README.md +7 -2
- package/lib/index.js +106 -39
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,78 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 2.1.4 (2026-10-02)
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
|
|
7
|
+
- **P0:注入的路由滞后一步,导致模型上下文里出现互相矛盾的两行**。2.1.2 修掉「每步重复注入」之后,实测发现注入的**内容**仍可能错:真实日志显示,一个子代理实际在 `ds41` 上运行,却收到一行写着 `glm` 的注入,随后又收到一行 `ds41` —— 模型同时看到两行、彼此矛盾。
|
|
8
|
+
|
|
9
|
+
**根因**:取值优先级里有「派发时种子」这一档。宿主的循环顺序是 `preStep()` → `buildRequest()`,于是**第一步的 preStep 执行时,`request/context` 帧尚未产生**,插件只能回落到 `agent.options` —— 而那是**派发时的意图值**,一旦路由在派发后被切换(故障转移 / 配置变更 / 父会话选择),它就永远不再更新。第一步注入的这个「猜测值」会排队进 inbox,在**第二步**才被领取,此时真实路由早已不同。
|
|
10
|
+
|
|
11
|
+
**修复**:只报告**已知为真**的路由,优先级为
|
|
12
|
+
1. `session.requestContext()` —— 宿主自己对最新 `request/context` 帧的折叠,文档原文即「the latest resolved route metadata」,也是宿主 `nextFailoverIndex` 读同一处的用法;
|
|
13
|
+
2. 本插件在 `agent/request` 瀑布里捕获的**结算后**种子(对该次请求权威);
|
|
14
|
+
3. **不回落**到 `agent.options`。
|
|
15
|
+
|
|
16
|
+
第 3 条是关键取舍:注入行只是**对现实的陈述**,一个立刻被推翻的猜测比不说更糟。因此路由已知前**保持沉默**,下一步(首次请求提交后)再注入。副作用是单步子代理不再收到该行 —— 这是**刻意的**,因为那时确实无从得知真实路由;把它当作「信息缺失」而非「错误信息」更诚实。
|
|
17
|
+
|
|
18
|
+
- **同步移除死代码 `routeOfAgent()`**:修复后它不再被任何路径调用,留着会误导下一个读者以为 `agent.options` 仍是有效来源。
|
|
19
|
+
|
|
20
|
+
### Tests
|
|
21
|
+
|
|
22
|
+
- 新增回归用例 **「路由在下一步之前被切换时,必须报告新路由、绝不陈旧」**:把 `agent.options` 故意留在派发种子(`glm`),而会话折叠推进到 `ds41`,断言**恰好一行**且内容为 `ds41`、且**绝不含 `glm`**。变异验证:把优先级改回「种子优先」,该用例连同另 2 项立即失败。
|
|
23
|
+
- 重写首步用例为**「未提交路由时保持沉默」**:断言首步不注入任何猜测行,并在路由已知后的下一注入一次。旧用例断言的是本次刻意反转的行为,故按新契约重写而非删除。
|
|
24
|
+
- **夹具修正(本轮缺陷得以漏过的原因)**:`makeAgent()` 原先把 `session.requestContext()` 固化为一个**常量**,而宿主语义是「折叠最新 `request/context` 的可变值」。夹具不模拟路由变化,注入器读到什么都会被当成「正确」。现改为可变状态,并由 `dispatchRequest()` 在瀑布结算后 `commitRoute()`,与宿主一致。
|
|
25
|
+
|
|
26
|
+
### Note
|
|
27
|
+
|
|
28
|
+
- 当前版本序列:2.1.2 修「每步重复注入」(1228 步注入 1227 次 → 11 步注入 2 次),2.1.4 修「内容滞后一步」。两者是**独立缺陷**,均已由真实运行验证。
|
|
29
|
+
|
|
30
|
+
## 2.1.3 (2026-10-02)
|
|
31
|
+
|
|
32
|
+
### Changed
|
|
33
|
+
|
|
34
|
+
- **认证类失败(密钥无效)现在也触发跨供应商切换,并附带明确的 warn 日志**。此前 `AUTH` 被刻意排除在切换范围外,理由是「不要把密钥配错误当成换一家就好而掩盖真实问题」。该顾虑本身成立,但它有一个代价被忽略了:当 `models` 列表里各家供应商**密钥各自独立**时,只要其中一家的密钥失效,整个子代理就会直接失败,而**健康的其它供应商完全闲置**——实测中确实遇到了这种情况(任务因一家配额/密钥问题终止,而列表里还有可用路由)。
|
|
35
|
+
|
|
36
|
+
现改为:
|
|
37
|
+
- **触发码分两类**:连接类(`RATE_LIMIT`/`QUOTA`/`SERVER`/`TIMEOUT`/`TRANSPORT`/`EMPTY_RESPONSE`)与**认证类**(`AUTH`、`INVALID_CREDENTIAL`,即宿主对 HTTP 401/403 的映射)。
|
|
38
|
+
- **认证类切换时必打一条 warn**,点名「失败的供应商凭据被拒绝、该供应商 API key 可能无效/过期/无权限,故障转移正在切到 X/Y,请核实该密钥,否则问题会持续复现」。
|
|
39
|
+
- 设计意图:**用可观测性替代抑制**。原先靠「不切换」来暴露密钥问题,现在改为「切换 + 日志」,两者兼得——任务能继续,问题也不会被静默掩盖。
|
|
40
|
+
- **不改变**:池耗尽仍放行真实错误(不做无限重试);每个候选每个 run 最多试一次;非上述类别的错误码(`NO_ADAPTER`、`CONTEXT_WINDOW_EXCEEDED`、`IMAGE_OFFLOAD_REQUIRED` 等)仍不切换——换一家会同样失败,切换只会掩盖真实错误。
|
|
41
|
+
|
|
42
|
+
测试:`failover.test.mjs` 的触发码用例重写,断言 AUTH / INVALID_CREDENTIAL **会**切换、AUTH 必须产出含 `API key is likely invalid` 且点名失败供应商的 warn、且三类非路由型错误码仍**不**切换。夹具新增 `warnings` 捕获(否则「日志必须存在」这条契约无法断言)。变异验证:把 AUTH 退回排除状态,该用例立即失败。
|
|
43
|
+
|
|
44
|
+
### Note
|
|
45
|
+
|
|
46
|
+
- 本次改动源于一次真实排障:日志显示某供应商返回 `429 soft_rate`(配额打满,供应商自身建议切换模型)后任务终止。注意**该 429 属于连接类、原本就在切换范围内**,因此上述 AUTH 改动与它无因果关系;配额场景另行调查(见下一条)。
|
|
47
|
+
- 同期确认了一个易混淆现象:宿主 LLM 层有自己的 `llm/retry` 策略,配额类错误会**先被它重试若干次**,之后才交由 `agent/request-error` 瀑布(即本插件的介入点)。因此插件观察到切换请求的时机晚于错误首次发生。这属于宿主分层行为,不是本插件的重试循环。
|
|
48
|
+
|
|
49
|
+
## 2.1.2 (2026-10-02)
|
|
50
|
+
|
|
51
|
+
### Fixed
|
|
52
|
+
|
|
53
|
+
- **P0:每个 step 都重复注入一次路由行(实测 387 步注入 479 次)**。用 8 个真实子代理实测时,探针报告「同一行反复出现,且 provider 会在轮次之间变来变去」。核对会话日志(`session.v4.jsonl.zstd`)确认:
|
|
54
|
+
|
|
55
|
+
| 探针 | step 数 | 注入次数 |
|
|
56
|
+
| --- | --- | --- |
|
|
57
|
+
| d43cab8a | 81 | 80 |
|
|
58
|
+
| 62ef8523 | 78 | 77 |
|
|
59
|
+
| 13952b6f | 24 | 23 |
|
|
60
|
+
| 6fb5b5d4 | 20 | 19 |
|
|
61
|
+
| d9474a33 | 84 | 83 |
|
|
62
|
+
| 31bfeacc | 79 | 78 |
|
|
63
|
+
| ceacf07f | 20 | 19 |
|
|
64
|
+
|
|
65
|
+
**根因**:2.1.1 的去重写成「`agent.inbox.nextStep` 里是否已有一条同样的路由行」。但宿主在每个 `preStep()` 的**开头**先调用 `inbox.claim()`,而 `claim()` 会把 `next-step` **整表抽干**(`mutate("next-step", 0, this.nextStep.length, [], false)`)——且这一步发生在 `agent/pre-step` 瀑布**之前**。于是轮到我这个监听器时,待处理列表**必然是空的**,那个判断**结构上恒为 false**,等于没有去重,每步都重新注入。
|
|
66
|
+
**为什么之前的两轮修复都没碰到它**:2.1.0 的问题在「首步读不到缓存」(已修),本问题是「去重依据失效」——两者独立。
|
|
67
|
+
**修复**:去重改为**带外状态**(`injectedRoute` 按 `agent.id` 记录上次注入的文本),不再依赖 inbox 内容。路由未变 ⇒ 不注入;路由变化 ⇒ 先清掉未认领的旧行再写新行。
|
|
68
|
+
**回归护栏**:新增 3 项用例——① 同路由连续 30 步必须只注入 1 次;② 中途切换 provider 时「每条路由各 1 次」而非「每步 1 次」;③ 同路由相邻两步不重复。**测试夹具同步修正**:`makeInbox()` 增加 `claim()` 并让 `dispatchPreStep()` 在瀑布前先抽干 inbox——正是这个夹具缺失让缺陷逃过了 2.1.0/2.1.1 两轮共 142 项测试。变异验证:把去重退回 inbox 方案,这 3 项立即全部失败。
|
|
69
|
+
|
|
70
|
+
- **测试记录方式修正:改为在「注入发生时」记录,而非事后读 inbox**。事后读 `inbox.nextStep` 只能看到当前这一步的那一条,因此「28 条重复」与「正好 1 条」在断言上长得一模一样——这是缺陷能藏住的第二个原因。现由 `recordInjections()` 在 `prepend` 时记录全部历史。
|
|
71
|
+
|
|
72
|
+
### Changed
|
|
73
|
+
|
|
74
|
+
- 上述修正同时解释了实测中观察到的「provider 在轮次间跳变」现象:稳定路由被反复重新注入时,日志里会呈现为路由在变。
|
|
75
|
+
|
|
3
76
|
## 2.1.1 (2026-10-01)
|
|
4
77
|
|
|
5
78
|
### Fixed
|
package/README.en.md
CHANGED
|
@@ -24,7 +24,12 @@ Note this decides **which route serves the next subagent**, chosen at **delegati
|
|
|
24
24
|
With `failoverEnabled` (on by default), when a subagent hits a **connection-class failure** mid-run, the plugin retries on another route from its own `models` list:
|
|
25
25
|
|
|
26
26
|
- **Provider-agnostic** — candidates come from the whole `models` list, so failover works **across providers**. If `deepseek-official/deepseek-v4-pro` dies, the subagent can fail over to a *different vendor's* model in the list — not merely another model ID from the same vendor.
|
|
27
|
-
- **
|
|
27
|
+
- **Two classes of trigger code** —
|
|
28
|
+
- **Connection-class**: `RATE_LIMIT`, `QUOTA`, `SERVER`, `TIMEOUT`, `TRANSPORT`, `EMPTY_RESPONSE`.
|
|
29
|
+
- **Auth-class**: `AUTH`, `INVALID_CREDENTIAL` (HTTP 401/403 — an invalid, expired, or unauthorised key).
|
|
30
|
+
- Auth-class failures **do** switch: when each provider holds its own credentials, one bad key must not kill the whole delegation while healthy providers sit unused.
|
|
31
|
+
- **But an auth switch always emits a warning** naming the provider whose key was refused. That is deliberate: switching buys resilience, and the **log line** is what keeps the misconfiguration from being silently hidden.
|
|
32
|
+
- Other codes (e.g. `NO_ADAPTER`, `CONTEXT_WINDOW_EXCEEDED`, `IMAGE_OFFLOAD_REQUIRED`) do **not** switch: another provider would fail identically, so switching would only bury the real error.
|
|
28
33
|
- **Inherited reasoning strength is dropped** — after switching, the new provider/model is not sent the previous route's `reasoningEffort`; it resolves its own default. Otherwise a target that rejects that level can fail every request and burn through all remaining candidates.
|
|
29
34
|
- **Sticky within a run** — after a switch, later steps of that same subagent stay on the new model instead of flip-flopping every step.
|
|
30
35
|
- **Exhaustion passes the real error through** — once every candidate in the list has been tried, the real error surfaces; there is no infinite retry.
|
|
@@ -231,7 +236,7 @@ CI runs the same suite on `ubuntu-latest`, `windows-latest`, and `macos-latest`,
|
|
|
231
236
|
With `failoverEnabled` on (the default), when a subagent's own loop hits a connection-class failure, the plugin automatically switches models inside the `models` list and retries, following `strategy`. See [Capability 2](#capability-2--cross-provider-failover-if-one-provider-is-down-run-on-another-providers-model) above for the full account; the key points:
|
|
232
237
|
|
|
233
238
|
- **Cross-provider** — candidates are the whole `models` list, so it can fail over to a model from another provider, not just another model ID from the same vendor.
|
|
234
|
-
- **
|
|
239
|
+
- **Trigger codes** — connection-class: `RATE_LIMIT`, `QUOTA`, `SERVER`, `TIMEOUT`, `TRANSPORT`, `EMPTY_RESPONSE`; auth-class: `AUTH`, `INVALID_CREDENTIAL` (invalid/expired key — switches, with a warning). Other codes do not switch.
|
|
235
240
|
- `round-robin`: advance to the next model in the list (the queue).
|
|
236
241
|
- `random`: pick any model (without checking whether it was used before).
|
|
237
242
|
- **Needs ≥ 2 models** — with fewer than 2 entries in `models`, the feature is inactive.
|
package/README.md
CHANGED
|
@@ -24,7 +24,12 @@
|
|
|
24
24
|
开启 `failoverEnabled`(默认开启)后,子代理在运行中遇到**连接类失败**时,会自动在本插件的 `models` 列表内换一条路由重试:
|
|
25
25
|
|
|
26
26
|
- **不区分供应商** —— 候选来自整个 `models` 列表,**跨 provider 生效**。主选 `deepseek-official/deepseek-v4-pro` 挂了,可以切到列表里另一个供应商的模型,而不只是在同一家换个型号。
|
|
27
|
-
-
|
|
27
|
+
- **触发码分两类** ——
|
|
28
|
+
- **连接类**:`RATE_LIMIT`、`QUOTA`、`SERVER`、`TIMEOUT`、`TRANSPORT`、`EMPTY_RESPONSE`。
|
|
29
|
+
- **认证类**:`AUTH`、`INVALID_CREDENTIAL`(HTTP 401/403,密钥无效/过期/无权限)。
|
|
30
|
+
- 认证类**也会切换**:当各家供应商密钥各自独立时,其中一家密钥失效不应该让整个任务死掉、而健康的供应商却在闲置。
|
|
31
|
+
- **但切换时一定会打 warn 日志**,明确指出「该供应商的密钥可能无效」——这是刻意设计:允许切换是为了可用性,而**日志**保证这个配置问题不会被静默掩盖。
|
|
32
|
+
- 其余失败码(如 `NO_ADAPTER`、`CONTEXT_WINDOW_EXCEEDED`、`IMAGE_OFFLOAD_REQUIRED`)**不切换**:换一家也会同样失败,切换只会掩盖真实错误。
|
|
28
33
|
- **切换时丢弃继承的推理强度** —— 换到新 provider/model 后不沿用旧路由的 `reasoningEffort`,按目标自己的默认档位请求;否则目标模型可能因「不支持该强度」而直接拒绝请求,导致把剩余候选全部烧光。
|
|
29
34
|
- **同一次运行内粘住** —— 切换后该子代理后续步骤继续跑在新模型上,不会每步来回跳。
|
|
30
35
|
- **耗尽即放行** —— 列表内全部候选都试过仍失败,就抛出真实错误,不做无限重试。
|
|
@@ -231,7 +236,7 @@ CI 在 `ubuntu-latest`、`windows-latest`、`macos-latest` 三个平台上同时
|
|
|
231
236
|
开启 `failoverEnabled`(默认开启)后,子代理自身的循环遇到连接类失败时,插件会在 `models` 列表内按 `strategy` 自动切换模型并重试。完整说明见上文「[功能二:跨供应商故障转移](#功能二跨供应商故障转移--这条通了那条不通就换另一个供应商的模型)」,要点复述:
|
|
232
237
|
|
|
233
238
|
- **跨供应商** —— 候选是整个 `models` 列表,可以切到另一个 provider 的模型,不限于同一家换型号。
|
|
234
|
-
-
|
|
239
|
+
- **触发码** —— 连接类:`RATE_LIMIT`、`QUOTA`、`SERVER`、`TIMEOUT`、`TRANSPORT`、`EMPTY_RESPONSE`;认证类:`AUTH`、`INVALID_CREDENTIAL`(密钥无效/过期,会切换并打 warn 日志)。其余码不切换。
|
|
235
240
|
- `round-robin`:按列表顺序切到下一个模型(队列)。
|
|
236
241
|
- `random`:随机挑一个模型(不判断之前是否用过)。
|
|
237
242
|
- **需要 ≥ 2 个模型** —— 当 `models` 少于 2 项时本功能不生效。
|
package/lib/index.js
CHANGED
|
@@ -156,6 +156,35 @@ const WRAPPED = Symbol.for("dsh-subagent-default-model.wrapped");
|
|
|
156
156
|
*/
|
|
157
157
|
const FAILOVER_TRIGGER_CODES = ["RATE_LIMIT", "QUOTA", "SERVER", "TIMEOUT", "TRANSPORT", "EMPTY_RESPONSE"];
|
|
158
158
|
|
|
159
|
+
/**
|
|
160
|
+
* Auth failures, handled SEPARATELY from the connection codes above.
|
|
161
|
+
*
|
|
162
|
+
* `AUTH` (HTTP 401/403 — a bad, expired, or unauthorised key) is not a
|
|
163
|
+
* connectivity problem, so switching on it is only right when the pool entries
|
|
164
|
+
* hold INDEPENDENT credentials. With per-provider keys — the normal setup — one
|
|
165
|
+
* provider's key going bad must not kill the whole delegation while healthy
|
|
166
|
+
* providers sit unused, which is what excluding AUTH entirely used to do.
|
|
167
|
+
*
|
|
168
|
+
* The reason AUTH was originally excluded still stands, though: silently
|
|
169
|
+
* rerouting around a broken key HIDES a misconfiguration. So an AUTH switch is
|
|
170
|
+
* always accompanied by a loud warning naming the provider whose credentials
|
|
171
|
+
* were refused, and pool exhaustion still passes the real error through.
|
|
172
|
+
* Observability replaces suppression.
|
|
173
|
+
*
|
|
174
|
+
* When several entries share ONE credential (e.g. the same env var), switching
|
|
175
|
+
* cannot help — but it also cannot loop: every candidate is tried at most once
|
|
176
|
+
* per run, and exhaustion surfaces the real error.
|
|
177
|
+
*/
|
|
178
|
+
const AUTH_FAILURE_CODES = ["AUTH", "INVALID_CREDENTIAL"];
|
|
179
|
+
|
|
180
|
+
/** Every failure code that may trigger a switch. */
|
|
181
|
+
const FAILOVER_ELIGIBLE_CODES = [...FAILOVER_TRIGGER_CODES, ...AUTH_FAILURE_CODES];
|
|
182
|
+
|
|
183
|
+
/** True when a failure code is an auth-class failure (picks the log wording). */
|
|
184
|
+
function isAuthFailure(code) {
|
|
185
|
+
return AUTH_FAILURE_CODES.includes(code);
|
|
186
|
+
}
|
|
187
|
+
|
|
159
188
|
/** Resolve one model entry to `{provider, model}`, or undefined. */
|
|
160
189
|
function resolveEntry(section, entry) {
|
|
161
190
|
if (typeof entry === "string") {
|
|
@@ -288,7 +317,7 @@ function installFailover(ctx, state) {
|
|
|
288
317
|
const { agent, turn, step, failure, signal } = payload;
|
|
289
318
|
if (signal?.aborted) return next();
|
|
290
319
|
if (!isSubagentAgent(agent)) return next();
|
|
291
|
-
if (failure === void 0 || !
|
|
320
|
+
if (failure === void 0 || !FAILOVER_ELIGIBLE_CODES.includes(failure.code)) return next();
|
|
292
321
|
const current = pending.get(agent.id);
|
|
293
322
|
// The primary already failed (that triggered the first switch), so the
|
|
294
323
|
// remaining candidates are the other pool entries: at most
|
|
@@ -316,6 +345,18 @@ function installFailover(ctx, state) {
|
|
|
316
345
|
view.entries.length,
|
|
317
346
|
view.strategy
|
|
318
347
|
);
|
|
348
|
+
// An auth switch is called out explicitly: the point of allowing it is
|
|
349
|
+
// resilience, and the risk it introduces is that a broken credential
|
|
350
|
+
// becomes invisible. This line is what keeps it visible — without it,
|
|
351
|
+
// silently rerouting around a bad key would hide a real misconfiguration.
|
|
352
|
+
if (isAuthFailure(failure.code)) {
|
|
353
|
+
ctx.logger.warn(
|
|
354
|
+
`[dsh-subagent-default-model] the credentials for the failing provider ${payload.provider ?? "(unknown)"} were refused (%s). This is NOT a connection problem: that provider's API key is likely invalid, expired, or unauthorised. Failover is switching the subagent to %s/%s — verify that provider's key, because the failure will keep recurring until it is fixed.`,
|
|
355
|
+
failure.code,
|
|
356
|
+
view.entries[index].provider,
|
|
357
|
+
view.entries[index].model
|
|
358
|
+
);
|
|
359
|
+
}
|
|
319
360
|
return { kind: "retry" };
|
|
320
361
|
});
|
|
321
362
|
|
|
@@ -404,30 +445,46 @@ function isRouteContextMessage(message) {
|
|
|
404
445
|
}
|
|
405
446
|
|
|
406
447
|
/**
|
|
407
|
-
*
|
|
448
|
+
* Resolve the route the agent is ACTUALLY using, or `undefined` when that is
|
|
449
|
+
* not yet knowable.
|
|
408
450
|
*
|
|
409
|
-
*
|
|
410
|
-
*
|
|
411
|
-
*
|
|
412
|
-
*
|
|
451
|
+
* The FIRST step is the hard case. `preStep()` runs BEFORE `buildRequest()`, so
|
|
452
|
+
* on step 1 no `request/context` frame exists and no `agent/request` waterfall
|
|
453
|
+
* has settled. The only value available is `agent.options` — the DISPATCH-TIME
|
|
454
|
+
* seed, which the parent may already have superseded (a switch during dispatch,
|
|
455
|
+
* or a route chosen after this agent was created).
|
|
413
456
|
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
419
|
-
* `agent.options` here is earlier and just as authoritative.
|
|
457
|
+
* Guessing from `agent.options` is what produced the reported contradiction: a
|
|
458
|
+
* subagent whose real route was `ds41` got a step-1 line saying `glm` (the
|
|
459
|
+
* dispatch seed), queued into the inbox, claimed on step 2 — so the model saw
|
|
460
|
+
* `glm` and `ds41` side by side. Since the line is only a report about reality,
|
|
461
|
+
* a guess that is immediately contradicted is worse than saying nothing.
|
|
420
462
|
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
463
|
+
* Therefore: report only what is KNOWN, and stay silent otherwise. The route
|
|
464
|
+
* becomes knowable one step later (the session fold updates as soon as the
|
|
465
|
+
* first request commits), so the line still lands early in the run.
|
|
466
|
+
*
|
|
467
|
+
* @param {object} agent - the loop agent.
|
|
468
|
+
* @param {Map} settled - per-agent settled-seed cache.
|
|
469
|
+
* @returns {{provider: string, model: string}|undefined} the known route.
|
|
423
470
|
*/
|
|
424
|
-
function
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
const
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
471
|
+
function routeForInjection(agent, settled) {
|
|
472
|
+
// The session's fold of the latest `request/context` — authoritative and
|
|
473
|
+
// current, and what the host itself treats as "the latest resolved route".
|
|
474
|
+
const live = agent?.session?.requestContext?.();
|
|
475
|
+
if (live !== void 0 && live !== null) {
|
|
476
|
+
const provider = typeof live.provider === "string" ? live.provider : "";
|
|
477
|
+
const model = typeof live.model === "string" ? live.model : "";
|
|
478
|
+
if (provider !== "" || model !== "") return { provider, model };
|
|
479
|
+
}
|
|
480
|
+
// A request already built by this plugin's own waterfall: still authoritative
|
|
481
|
+
// for that request, and normally identical to the fold above.
|
|
482
|
+
const captured = settled.get(agent.id);
|
|
483
|
+
if (captured !== void 0) return captured;
|
|
484
|
+
// Nothing has committed yet. Deliberately do NOT fall back to
|
|
485
|
+
// `agent.options`: that is the dispatch seed and is exactly the stale guess
|
|
486
|
+
// this function exists to avoid.
|
|
487
|
+
return void 0;
|
|
431
488
|
}
|
|
432
489
|
|
|
433
490
|
/**
|
|
@@ -440,13 +497,7 @@ function routeOfAgent(agent) {
|
|
|
440
497
|
* conversation. This injects one line stating the route the child is ACTUALLY
|
|
441
498
|
* using.
|
|
442
499
|
*
|
|
443
|
-
* SOURCE OF TRUTH:
|
|
444
|
-
* 1. `agent.options` at pre-step time (see `routeOfAgent`) — always available,
|
|
445
|
-
* including the very first step.
|
|
446
|
-
* 2. The SETTLED seed from the `agent/request` waterfall, once a request has
|
|
447
|
-
* been built. It reflects every listener's rewrite (including this
|
|
448
|
-
* plugin's own failover swap), so after a mid-run switch the injected line
|
|
449
|
-
* follows the new route rather than the one the agent started on.
|
|
500
|
+
* SOURCE OF TRUTH: see `routeForInjection` — the session's own live route first.
|
|
450
501
|
*
|
|
451
502
|
* Injected on every step whose route differs from the line already present, so
|
|
452
503
|
* a failover switch or a route change is reflected immediately; an unchanged
|
|
@@ -476,41 +527,56 @@ function installRouteContextInjector(ctx, state) {
|
|
|
476
527
|
|
|
477
528
|
// Inject on the pre-step, where the message list for the step about to run
|
|
478
529
|
// is still mutable and the agent's own route is already known.
|
|
530
|
+
//
|
|
531
|
+
// DEDUPE MUST BE OUT-OF-BAND, NOT "is a copy still pending?".
|
|
532
|
+
// The loop calls `inbox.claim()` at the TOP of `preStep()` — BEFORE this
|
|
533
|
+
// waterfall — and `claim()` DRAINS `next-step`
|
|
534
|
+
// (`mutate("next-step", 0, this.nextStep.length, [], false)`). So by the time
|
|
535
|
+
// this listener runs, the pending list is always empty and a "does a route
|
|
536
|
+
// line already exist in the inbox?" test is structurally always false. An
|
|
537
|
+
// earlier build did exactly that and re-injected on EVERY step: a real
|
|
538
|
+
// 29-step subagent ended up with 28 identical route lines, which also made
|
|
539
|
+
// the reported route churn between turns. Track what was last injected for
|
|
540
|
+
// this agent instead.
|
|
541
|
+
const injectedRoute = new Map();
|
|
542
|
+
|
|
479
543
|
const disposePreStep = ctx.on("agent/pre-step", async ({ agent, messages }, next) => {
|
|
480
544
|
const decision = await next();
|
|
481
545
|
if (decision === void 0 || decision === null || decision.kind === "reject") return decision;
|
|
482
546
|
if (state.getSection()?.injectRouteContext === false) return decision;
|
|
483
547
|
if (!isSubagentAgent(agent)) return decision;
|
|
484
548
|
|
|
485
|
-
//
|
|
486
|
-
// agent
|
|
487
|
-
const route =
|
|
549
|
+
// The live session route wins; see `routeForInjection` for why the
|
|
550
|
+
// dispatch-time `agent.options` must NOT be preferred.
|
|
551
|
+
const route = routeForInjection(agent, resolvedRoute);
|
|
488
552
|
if (route === void 0) return decision;
|
|
489
553
|
// Defensive: only the loop's live Agent exposes an inbox. A test double
|
|
490
554
|
// or a future agent shape without one must not break the step.
|
|
491
555
|
if (agent.inbox === void 0 || !Array.isArray(agent.inbox.nextStep)) return decision;
|
|
492
556
|
|
|
493
|
-
// Reuse the pending message when the route is unchanged; replace it when
|
|
494
|
-
// the route moved (failover / config change). Without the remove step a
|
|
495
|
-
// stale line would linger alongside the fresh one.
|
|
496
|
-
const pending = agent.inbox.nextStep.filter(isRouteContextMessage);
|
|
497
557
|
const wanted = routeContextText(route.provider, route.model);
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
558
|
+
// Same route as last time => the line already placed in this session is
|
|
559
|
+
// still accurate; inject nothing (no duplicate, no churn).
|
|
560
|
+
if (injectedRoute.get(agent.id)?.text === wanted) return decision;
|
|
561
|
+
|
|
562
|
+
// Route moved (failover / config change): drop any not-yet-claimed copy so
|
|
563
|
+
// the stale route does not linger beside the fresh one.
|
|
564
|
+
for (const message of agent.inbox.nextStep.filter(isRouteContextMessage)) {
|
|
565
|
+
agent.inbox.remove(message.id);
|
|
501
566
|
}
|
|
502
|
-
if (existing !== void 0) return decision;
|
|
503
567
|
|
|
504
568
|
const message = createUserMessage({
|
|
505
569
|
content: [{ type: "text", text: wanted }],
|
|
506
570
|
source: { kind: ROUTE_CONTEXT_SOURCE, form: ROUTE_CONTEXT_FORM }
|
|
507
571
|
});
|
|
508
572
|
agent.inbox.prepend("next-step", message);
|
|
573
|
+
injectedRoute.set(agent.id, { text: wanted });
|
|
509
574
|
return decision;
|
|
510
575
|
});
|
|
511
576
|
|
|
512
577
|
const disposeDisposed = ctx.on("agent/disposed", ({ agent }) => {
|
|
513
578
|
resolvedRoute.delete(agent.id);
|
|
579
|
+
injectedRoute.delete(agent.id);
|
|
514
580
|
});
|
|
515
581
|
|
|
516
582
|
ctx.effect(() => () => {
|
|
@@ -518,6 +584,7 @@ function installRouteContextInjector(ctx, state) {
|
|
|
518
584
|
disposePreStep();
|
|
519
585
|
disposeDisposed();
|
|
520
586
|
resolvedRoute.clear();
|
|
587
|
+
injectedRoute.clear();
|
|
521
588
|
}, "dsh-subagent-default-model: route context injector");
|
|
522
589
|
}
|
|
523
590
|
|
package/package.json
CHANGED