@liustack/modlens 3.23.1 → 3.24.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/docs/cli.md CHANGED
@@ -98,6 +98,7 @@ Six providers: `antigravity-cli` (no key), `gemini-api` (fastest free route), `o
98
98
  Other subcommands:
99
99
 
100
100
  - `modlens guard [--model <id>]`: should the engine run for the active model at all? Exit 0 allow, 1 deny, verdict as JSON.
101
- - `modlens config <init|set|show>`: `set` with the value omitted on an `apiKey` field prompts for it with the echo hidden, so the key never enters argv, shell history, or the conversation with an agent driving the terminal. It also accepts one piped line (`pbpaste | modlens config set openai.apiKey`), which keeps the key out of argv, though whatever produced that pipe is still your own command to keep out of history. Keys are `provider`, `proxy` (HTTP/HTTPS proxy for the API providers, `HTTPS_PROXY`/`HTTP_PROXY` also honored), `reuse.<claude|codex|opencode|pi|grok>`, `guards.<denyModels|allowModels|denyWhenUnknown>`, and `<provider>.<apiKey|baseUrl|model|proxy|extraBody>`, plus `openai.structuredOutput` (that route only).
102
- - `modlens doctor`: Node and node:sqlite, provider readiness, the failover chains for this machine, the detected harness, the guard's rules with a live verdict, and the Reuse section with per-harness grant decisions and discovered vision. Spends no quota; `--json` for a machine-readable report.
101
+ - `modlens config <init|set|show>`: `set` with the value omitted on an `apiKey` field prompts for it with the echo hidden, so the key never enters argv, shell history, or the conversation with an agent driving the terminal. It also accepts one piped line (`pbpaste | modlens config set openai.apiKey`), which keeps the key out of argv, though whatever produced that pipe is still your own command to keep out of history. Keys are `provider`, `proxy` (HTTP/HTTPS proxy for the API providers, `HTTPS_PROXY`/`HTTP_PROXY` also honored), `cooldown` (`on` or `off`, on by default), `reuse.<claude|codex|opencode|pi|grok>`, `guards.<denyModels|allowModels|denyWhenUnknown>`, and `<provider>.<apiKey|baseUrl|model|proxy|extraBody>`, plus `openai.structuredOutput` (that route only). `apiKey` accepts a comma-separated list and rotates after authentication, rate-limit, or quota failures.
102
+ - `modlens state clear`: forget every provider cooldown in `~/.modlens/state.json`, so all providers are tried at full priority again.
103
+ - `modlens doctor`: Node and node:sqlite, provider readiness (including API key counts), the failover chains for this machine, the cooldown switch and any cooling keys, the detected harness, the guard's rules with a live verdict, and the Reuse section with per-harness grant decisions and discovered vision. Spends no quota; `--json` for a machine-readable report.
103
104
 
package/docs/cli.zh-CN.md CHANGED
@@ -95,5 +95,6 @@ modlens recover-paste # pull a pasted image into a fil
95
95
  其他子命令:
96
96
 
97
97
  - `modlens guard [--model <id>]`:判断当前激活的模型到底该不该运行引擎。退出码 0 表示放行,1 表示拒绝,判定结果以 JSON 输出。
98
- - `modlens config <init|set|show>`:`set` 在 `apiKey` 字段上省略值会进入不回显的输入提示,密钥不进 argv、不进 shell 历史,也不进你与终端里那个 agent 的对话。它也接受从管道读一行(`pbpaste | modlens config set openai.apiKey`),这样密钥不进 argv,但产生这条管道的命令本身是否留在历史里,由你自己决定。可用的键有 `provider`、`proxy`(API provider 的 HTTP/HTTPS 代理,也认 `HTTPS_PROXY`/`HTTP_PROXY`)、`reuse.<claude|codex|opencode|pi|grok>`、`guards.<denyModels|allowModels|denyWhenUnknown>`,以及 `<provider>.<apiKey|baseUrl|model|proxy|extraBody>`,另有 `openai.structuredOutput`(仅这条路线用得上)。
99
- - `modlens doctor`:报告 Node 与 node:sqlite、各 provider 的就绪状态、本机的故障转移链、检测到的 harness、guard 规则和一次现场判定,以及 Reuse 一节里按 harness 的授权决定与发现的视觉能力。不花任何额度,`--json` 输出机器可读报告。
98
+ - `modlens config <init|set|show>`:`set` 在 `apiKey` 字段上省略值会进入不回显的输入提示,密钥不进 argv、不进 shell 历史,也不进你与终端里那个 agent 的对话。它也接受从管道读一行(`pbpaste | modlens config set openai.apiKey`),这样密钥不进 argv,但产生这条管道的命令本身是否留在历史里,由你自己决定。可用的键有 `provider`、`proxy`(API provider 的 HTTP/HTTPS 代理,也认 `HTTPS_PROXY`/`HTTP_PROXY`)、`cooldown`(`on` 或 `off`,默认打开)、`reuse.<claude|codex|opencode|pi|grok>`、`guards.<denyModels|allowModels|denyWhenUnknown>`,以及 `<provider>.<apiKey|baseUrl|model|proxy|extraBody>`,另有 `openai.structuredOutput`(仅这条路线用得上)。`apiKey` 接受英文逗号分隔的列表,鉴权、限流或配额失败后会轮换。
99
+ - `modlens state clear`:忘掉 `~/.modlens/state.json` 里每一条 provider 冷却,让所有 provider 重新按完整优先级尝试。
100
+ - `modlens doctor`:报告 Node 与 node:sqlite、各 provider 的就绪状态(含密钥数量)、本机的故障转移链、冷却开关和正在冷却的密钥、检测到的 harness、guard 规则和一次现场判定,以及 Reuse 一节里按 harness 的授权决定与发现的视觉能力。不花任何额度,`--json` 输出机器可读报告。
@@ -55,7 +55,7 @@ OpenCode with DeepSeek: `opencode auth login`, pick DeepSeek and paste the key (
55
55
  dsh is different from the other harnesses: modlens plugs in as a native tool, not a prompt-triggered skill. The package itself is a dsh bundle, so one command installs it into a profile:
56
56
 
57
57
  ```sh
58
- npx -y @deepseek-ai/dsh plugin --profile web add @liustack/modlens@3.23.1
58
+ npx -y @deepseek-ai/dsh plugin --profile web add @liustack/modlens@3.24.1
59
59
  ```
60
60
 
61
61
  This registers a `modlens_read_image` tool whose schema reaches the model on every request (no trigger heuristics), runs the modlens CLI shipped inside the same package, and returns the structured evidence as the tool's canonical JSON output. Engines, reuse grants, and guard rules stay in `~/.modlens/config.json`, shared with every other harness. dsh is in developer preview and its plugin surface may change; the plugin keeps its touch small (raw tool registration, the llm adapter surface for the vision variants, the attachment reader, and one agent pre-step hook) and degrades loudly if any of them moves.
@@ -87,7 +87,7 @@ modlens ships often, and both install shapes freeze at whatever version they
87
87
  got. On dsh, re-run the install with the version named:
88
88
 
89
89
  ```sh
90
- npx -y @deepseek-ai/dsh plugin --profile <name> add @liustack/modlens@3.23.1
90
+ npx -y @deepseek-ai/dsh plugin --profile <name> add @liustack/modlens@3.24.1
91
91
  ```
92
92
 
93
93
  `npm view @liustack/modlens version` prints the current one, and this page is
@@ -137,6 +137,28 @@ becomes a file path and the session never holds an attachment, so nothing locks
137
137
  the model selector. It only comes up after a paste on a variant or on a vision
138
138
  model, where an attachment is the point.
139
139
 
140
+ ### For other plugin authors: injecting images on a `(modlens vision)` route
141
+
142
+ A wrapper route's declared `inputModalities: ['text', 'image']` is a promise,
143
+ not decoration ([#74](https://github.com/liustack/modlens/issues/74)). Every
144
+ `image` block in the request — pasted by the user or injected by another
145
+ plugin, including blocks nested in tool results — is converted to structured
146
+ evidence text at request time, before the wire reaches the text-only upstream.
147
+ Nothing is silently dropped, so the branch "declared image → inject a native
148
+ image block, otherwise inject a file path" works unchanged whether the
149
+ declaring route is a real vision model or a modlens wrapper.
150
+
151
+ One requirement: the block must carry a host attachment reference, the shape
152
+ `ctx.attachments.saveImage` returns and a Web UI paste produces — the plugin
153
+ reads bytes through `ctx.attachments.readImage(block.attachment)`. A
154
+ hand-built block holding only a path or base64 payload degrades to a constant
155
+ read-failure placeholder. And do not add a file-path text next to an image
156
+ block on an image-declaring route: the block already becomes complete
157
+ evidence there, and the extra path invites a second read of the same image
158
+ through the tool — double quota, and a second wording of the same content,
159
+ which is exactly the prefix-cache churn
160
+ [#68](https://github.com/liustack/modlens/issues/68) removed.
161
+
140
162
  ### Paste-to-path (web profile)
141
163
 
142
164
  Pasting an image into the dsh Web UI under a **text-only model** used to die at
@@ -55,7 +55,7 @@ OpenCode 接 DeepSeek:执行 `opencode auth login`,选择 DeepSeek 并粘贴
55
55
  dsh 与其他 harness 不同:modlens 以原生工具的形式接入,而不是靠提示词触发的 skill。本包自身就是一个 dsh bundle,一条命令即可装进某个 profile:
56
56
 
57
57
  ```sh
58
- npx -y @deepseek-ai/dsh plugin --profile web add @liustack/modlens@3.23.1
58
+ npx -y @deepseek-ai/dsh plugin --profile web add @liustack/modlens@3.24.1
59
59
  ```
60
60
 
61
61
  这会注册一个 `modlens_read_image` 工具,它的 schema 随每次请求抵达模型(不靠触发启发式),运行同一个包里自带的 modlens CLI,并把结构化证据作为工具的标准 JSON 输出返回。引擎、复用授权和 guard 规则仍在 `~/.modlens/config.json` 里,与其他所有 harness 共享。dsh 还在开发者预览阶段,插件接口可能变化。这个插件刻意保持很小的接触面(原生工具注册、视觉变体所用的 llm 适配层、附件读取器,以及一个 agent 执行前钩子),其中任何一处变动,它都会大声报错而不是无声退化。
@@ -73,7 +73,7 @@ dsh 的网页用户面前没有终端,所以引擎设置有一张卡片,在*
73
73
  modlens 发布很频繁,而两种安装形态都会冻结在装进来的那个版本上。dsh 上重跑一遍安装即可,版本号要点名:
74
74
 
75
75
  ```sh
76
- npx -y @deepseek-ai/dsh plugin --profile <name> add @liustack/modlens@3.23.1
76
+ npx -y @deepseek-ai/dsh plugin --profile <name> add @liustack/modlens@3.24.1
77
77
  ```
78
78
 
79
79
  `npm view @liustack/modlens version` 可以查到当前版本号,本页的版本号则由发布流程自动写入。
@@ -96,6 +96,12 @@ skill 类 harness 上,skill 是一个拷贝出来的文件夹,拷贝会保
96
96
 
97
97
  走上面第一条粘贴路线就不会遇到:图片变成文件路径,会话里从不存在附件,模型选择器也就不会被锁住。只有在变体或视觉模型上粘贴时才会产生附件,而那种场景下附件本来就是目的。
98
98
 
99
+ ### 给其他插件作者:向 `(modlens vision)` 路由注入图片
100
+
101
+ 包装路由声明的 `inputModalities: ['text', 'image']` 是承诺,不是装饰([#74](https://github.com/liustack/modlens/issues/74))。请求里的每一个 `image` 块,无论来自用户粘贴还是其他插件注入(包括嵌在 tool result 里的),都会在请求时转成结构化证据文本,再发往 text-only 上游。没有任何内容被静默丢弃,所以「声明 image 就注入原生图片块,否则注入文件路径」这条分支不用区分对面是真视觉模型还是 modlens 包装。
102
+
103
+ 唯一前提:块必须带宿主附件引用,即 `ctx.attachments.saveImage` 返回、Web UI 粘贴产生的那种形状,插件靠 `ctx.attachments.readImage(block.attachment)` 取字节。手搓的只含路径或 base64 的块会落进固定的读取失败占位。另外不要在声明 image 的路由上给图片块旁边再附路径文本:块在这里已经变成完整证据,多出的路径会诱导模型用工具把同一张图再读一遍,双倍配额,还产生同一内容的第二种措辞,正是 [#68](https://github.com/liustack/modlens/issues/68) 清除过的前缀缓存抖动。
104
+
99
105
  ### 粘贴转路径(paste-to-path,web profile)
100
106
 
101
107
  过去在 dsh Web UI 里,**纯文本模型**下粘贴图片会死在图片准入检查这一步。插件现在带了一个浏览器端半边(由 dsh 的客户端插件系统自动加载),恰好在这种情况下接管粘贴:图片字节发到插件在 dsh web 服务器上的 `/modlens/paste` 路由(仅回环地址,校验 magic byte,上限 25 MB),落成一个私有临时文件,输入框收到的则是纯文本的文件路径。这与 Pi、OpenCode、Claude Code 递给模型的形态一致,也正是 modlens skill 和 `modlens_read_image` 工具的首要触发条件。消息里不带图片附件,准入检查根本不会触发。
package/docs/security.md CHANGED
@@ -38,6 +38,10 @@ So the private-address guards, the magic-byte check, and the size cap protect ex
38
38
 
39
39
  Text inside an image is untrusted, the same as a web page. A screenshot can contain instructions aimed at whatever reads it. The prompt says so explicitly, but that is mitigation, not a guarantee: analyze images you are willing to open, and prefer a sandboxed working directory when they came from elsewhere.
40
40
 
41
+ ## Credentials in errors
42
+
43
+ Gateway bodies, subprocess stderr, per-attempt records, warnings, and persisted cooldown reasons all go through redaction before they travel. When a provider has several API keys, every sibling key is registered as a secret, not the joined `'k1,k2'` string as one. Truncation happens after that pass, so a cut cannot hide a key from the exact-match scrub.
44
+
41
45
  ## Evidence, not invention
42
46
 
43
47
  What the engine cannot read goes into `uncertainty` rather than being filled in. v2 dropped pixel coordinates and confidence scores entirely, because those are the two fields models fabricate most convincingly.
@@ -38,6 +38,10 @@ ModLens 调用 `agy` 时带上 `--dangerously-skip-permissions`,因为某些
38
38
 
39
39
  图片里的文字是不可信的,和网页一样。一张截图可能带着写给任何读取者的指令。prompt 里明确说了这一点,但那是缓解措施,不是保证:只分析你愿意打开的图片,图片来自别处时优先用沙箱化的工作目录。
40
40
 
41
+ ## 报错里的凭据
42
+
43
+ 网关响应体、子进程 stderr、每次尝试记录、警告,以及写入冷却状态的原因,都会先脱敏再往外走。一个 provider 配了多把密钥时,每一把都会登记为秘密,而不是把 `'k1,k2'` 整串当成一把。截断发生在脱敏之后,避免切到密钥中间让精确匹配找不到。
44
+
41
45
  ## 证据,而非编造
42
46
 
43
47
  引擎读不出来的内容会进入 `uncertainty`,而不是被脑补出来。v2 彻底去掉了像素坐标和置信度分数,因为这两个字段正是模型编造得最像真的。
@@ -10,7 +10,7 @@ read_when:
10
10
 
11
11
  English | [中文](troubleshooting.zh-CN.md)
12
12
 
13
- Start with `modlens doctor`: it checks your Node version, which providers are ready, which one will be selected and why, and the detected harness, all without spending quota or making a network request. It catches most setup problems before you read any further.
13
+ Start with `modlens doctor`: it checks your Node version, which providers are ready (including how many API keys each one has), which one will be selected and why, the cooldown switch and any cooling keys, and the detected harness, all without spending quota or making a network request. It catches most setup problems before you read any further. A spent key rotates to the next one, then cools, so the next run tries a healthy key first.
14
14
 
15
15
  Every message below is one modlens actually prints. Search this file for the words you saw.
16
16
 
@@ -163,7 +163,7 @@ simply lands on an older one. Name the exact version instead, which pnpm treats
163
163
  as a deliberate request rather than a resolution:
164
164
 
165
165
  ```sh
166
- npx -y @deepseek-ai/dsh plugin --profile <name> add @liustack/modlens@3.23.1
166
+ npx -y @deepseek-ai/dsh plugin --profile <name> add @liustack/modlens@3.24.1
167
167
  ```
168
168
 
169
169
  `npm view @liustack/modlens version` prints the current one. pnpm 11 installs a named
@@ -178,7 +178,7 @@ file:
178
178
 
179
179
  ```yaml
180
180
  minimumReleaseAgeExclude:
181
- - '@liustack/modlens@3.23.1'
181
+ - '@liustack/modlens@3.24.1'
182
182
  ```
183
183
 
184
184
  Or lift the gate for a single command, which lifts it for everything that
@@ -10,7 +10,7 @@ read_when:
10
10
 
11
11
  [English](troubleshooting.md) | 中文
12
12
 
13
- 先跑 `modlens doctor`:它会检查你的 Node 版本、哪些 provider 已就绪、将选中哪一个及其原因,以及检测到的 harness,全程不消耗额度,也不发网络请求。大多数配置问题在你继续往下读之前就能被它查出来。
13
+ 先跑 `modlens doctor`:它会检查你的 Node 版本、哪些 provider 已就绪(含各家有几把密钥)、将选中哪一个及其原因、冷却开关和正在冷却的密钥,以及检测到的 harness,全程不消耗额度,也不发网络请求。大多数配置问题在你继续往下读之前就能被它查出来。一把密钥用尽会先轮换到下一把,再进入冷却,下次运行会优先试还健康的密钥。
14
14
 
15
15
  下面每条消息都是 modlens 实际会打印的。拿你看到的字眼在本文里搜索即可。
16
16
 
@@ -144,7 +144,7 @@ dsh profile 装到的是旧版 modlens。`dsh.bundle` 声明从 3.9.0 起才存
144
144
  `@latest` 绕不开这一层,本页早先的说法是错的。冷静期先把候选版本过滤掉,dist-tag 才在剩下的里面解析,于是它直接落到了更旧的那个上。改成写死精确版本号,pnpm 会把它当作一次明确的指定,而不是一次解析:
145
145
 
146
146
  ```sh
147
- npx -y @deepseek-ai/dsh plugin --profile <name> add @liustack/modlens@3.23.1
147
+ npx -y @deepseek-ai/dsh plugin --profile <name> add @liustack/modlens@3.24.1
148
148
  ```
149
149
 
150
150
  `npm view @liustack/modlens version` 可以查到当前版本号。pnpm 11 会装上被点名的版本,11.1.3 起还会把它作为一条已批准的例外写进该 profile 的 `pnpm-workspace.yaml`,其余所有包和 modlens 以后的版本仍然留在窗口后面。
@@ -153,7 +153,7 @@ npx -y @deepseek-ai/dsh plugin --profile <name> add @liustack/modlens@3.23.1
153
153
 
154
154
  ```yaml
155
155
  minimumReleaseAgeExclude:
156
- - '@liustack/modlens@3.23.1'
156
+ - '@liustack/modlens@3.24.1'
157
157
  ```
158
158
 
159
159
  或者只为这一条命令解除冷静期,注意它解除的是这条命令解析到的所有包,不只 modlens:
package/dsh/client.js CHANGED
@@ -192,6 +192,8 @@ window.__ModuleLoader__.load({
192
192
  pickToConfigure: 'Pick an engine above to configure its key and endpoint.',
193
193
  engine: 'Engine',
194
194
  apiKey: 'API key',
195
+ apiKeyHint:
196
+ 'Separate multiple keys with commas. ModLens rotates to the next key after authentication, rate-limit, or quota failures.',
195
197
  baseUrl: 'Base URL',
196
198
  model: 'Model',
197
199
  stored: 'stored, leave empty to keep it',
@@ -219,6 +221,7 @@ window.__ModuleLoader__.load({
219
221
  pickToConfigure: '在上面选一个引擎,才能配置它的密钥和地址。',
220
222
  engine: '引擎',
221
223
  apiKey: 'API 密钥',
224
+ apiKeyHint: '多个密钥用英文逗号分隔。鉴权、限流或配额失败时会自动轮换到下一个密钥。',
222
225
  baseUrl: '接口地址',
223
226
  model: '模型',
224
227
  stored: '已保存,留空即不改动',
@@ -644,6 +647,22 @@ window.__ModuleLoader__.load({
644
647
  'clinote',
645
648
  )
646
649
  : secretField(t.apiKey, 'apiKey', current.hasKey ? t.stored : t.unset),
650
+ draft.provider === '' || keyless
651
+ ? null
652
+ : fieldRow(
653
+ '',
654
+ h(
655
+ 'div',
656
+ {
657
+ style: {
658
+ fontSize: '13px',
659
+ color: 'var(--dsw-alias-label-tertiary, rgba(127,127,127,0.8))',
660
+ },
661
+ },
662
+ t.apiKeyHint,
663
+ ),
664
+ 'api-key-rotation',
665
+ ),
647
666
  draft.provider === '' || keyless ? null : textField(t.baseUrl, 'baseUrl', 'text', t.fallback),
648
667
  draft.provider === '' ? null : textField(t.model, 'model', 'text', t.fallback),
649
668
  // Where these values are coming from, said once, because the
package/dsh/index.js CHANGED
@@ -55,7 +55,20 @@ export function apply(ctx, config = {}) {
55
55
  // wrappers land, including the later sweeps, and read by the verdict.
56
56
  const ownProviders = new Set()
57
57
  if (config.visionProvider !== false) {
58
- registerVisionProvider(ctx, config, ownProviders, evidenceCache)
58
+ // Bundle loaders can call apply while this outer context is still waiting
59
+ // for its required services. Reading ctx.llm here then throws "inactive
60
+ // context" before the first discovery sweep can register any lifecycle
61
+ // work (#79). Put the whole provider registry inside an injected child
62
+ // scope: Cordis starts it only while llm is active, and tears its listeners
63
+ // and registrations down with that service. Preview hosts without inject
64
+ // keep the dependency-free plugin's former feature-detected path.
65
+ if (typeof ctx.inject === 'function') {
66
+ ctx.inject(['llm'], (scope) => {
67
+ return registerVisionProvider(scope, config, ownProviders, evidenceCache)
68
+ })
69
+ } else {
70
+ registerVisionProvider(ctx, config, ownProviders, evidenceCache)
71
+ }
59
72
  }
60
73
  // Paste-to-path: the browser half (dsh/client.js) intercepts image pastes
61
74
  // and POSTs the bytes here; the file lands in a private temp dir and the
@@ -569,10 +582,50 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
569
582
  if (Array.isArray(info?.inputModalities) && info.inputModalities.includes('image')) return false
570
583
  return true
571
584
  }
572
- if (typeof ctx.llm?.registerAdapter !== 'function' || typeof ctx.llm?.stream !== 'function') {
585
+ // Keep this activation bound to the exact service implementation that made
586
+ // it runnable. Cordis reuses the child context when llm is replaced, so
587
+ // looking the service up again after an await could otherwise move an old
588
+ // topology result into the new registry.
589
+ const llm = ctx.llm
590
+ if (typeof llm?.registerAdapter !== 'function' || typeof llm?.stream !== 'function') {
573
591
  return
574
592
  }
575
593
 
594
+ // Discovery promises are ordinary JavaScript work, not Cordis effects.
595
+ // The disposer invalidates this activation before the injected child is
596
+ // re-run, and clears the ownership facts whose actual adapter effects the
597
+ // framework tears down independently.
598
+ let active = true
599
+ const claimedProviders = new Set()
600
+ const deactivate = () => {
601
+ active = false
602
+ for (const providerId of claimedProviders) ownProviders?.delete(providerId)
603
+ claimedProviders.clear()
604
+ }
605
+ // Cordis marks a fiber UNLOADING before it runs activation disposers. A
606
+ // promise continuation already in the microtask queue can therefore see
607
+ // `active` before the disposer flips it. Creating and immediately releasing
608
+ // a zero-work effect is the framework's atomic liveness boundary: once it
609
+ // succeeds, the following synchronous registry mutation cannot race an
610
+ // unload. A lifecycle refusal cancels this activation without turning an
611
+ // expected teardown into a terminal diagnostic.
612
+ const activationCanCommit = () => {
613
+ if (!active) return false
614
+ if (typeof ctx.effect !== 'function') return true
615
+ try {
616
+ const release = ctx.effect(() => {})
617
+ if (typeof release === 'function') release()
618
+ return active
619
+ } catch (error) {
620
+ const inactive =
621
+ error?.code === 'INACTIVE_EFFECT' ||
622
+ /cannot create effect on inactive context/i.test(String(error?.message ?? error))
623
+ if (!inactive) throw error
624
+ deactivate()
625
+ return false
626
+ }
627
+ }
628
+
576
629
  // dsh snapshots providerInfo and providerRetryPolicy at registration time.
577
630
  // Keep the state and registration handle for each wrapper so an upstream
578
631
  // replacement can refresh those snapshots instead of leaving a synthetic
@@ -582,6 +635,7 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
582
635
  const policyKey = (policy) => (policy === undefined ? undefined : JSON.stringify(policy))
583
636
 
584
637
  const registerWrapper = (upstream, providerId, displayName) => {
638
+ if (!activationCanCommit()) return false
585
639
  const state = { displayName, retryPolicyKey: undefined }
586
640
  const withVision = (info) => {
587
641
  const inputModalities = Array.isArray(info?.inputModalities) ? [...info.inputModalities] : []
@@ -590,7 +644,7 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
590
644
  return { ...info, provider: providerId, inputModalities }
591
645
  }
592
646
  try {
593
- const registration = ctx.llm.registerAdapter([providerId], {
647
+ const registration = llm.registerAdapter([providerId], {
594
648
  // Duck-typing LlmAdapter: providerInfo/providerRetryPolicy are
595
649
  // base-class defaults a plain object must supply itself (their
596
650
  // absence is exactly the silent registration failure this catch
@@ -608,20 +662,20 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
608
662
  // and the registration boundary below fails closed instead; the
609
663
  // ordinary not-mounted-yet case never reaches this method, because
610
664
  // reconcile waits for the upstream before registering (#66).
611
- if (typeof ctx.llm.providerRetryPolicy !== 'function') return undefined
612
- const policy = ctx.llm.providerRetryPolicy(upstream)
665
+ if (typeof llm.providerRetryPolicy !== 'function') return undefined
666
+ const policy = llm.providerRetryPolicy(upstream)
613
667
  state.retryPolicyKey = policyKey(policy)
614
668
  return policy
615
669
  },
616
670
  async listModels(_provider, signal) {
617
- const models = await ctx.llm.listModels(upstream, signal)
671
+ const models = await llm.listModels(upstream, signal)
618
672
  return models.filter(shouldWrap).map((model) => ({
619
673
  ...withVision(model),
620
674
  name: `${model.name ?? model.id} (modlens vision)`,
621
675
  }))
622
676
  },
623
677
  async resolveModel(_provider, model, signal) {
624
- const info = await ctx.llm.resolveModelInfo(upstream, model, signal)
678
+ const info = await llm.resolveModelInfo(upstream, model, signal)
625
679
  if (!shouldWrap(info)) {
626
680
  // Refusing is right: wrapping a model that reads images itself
627
681
  // would claim a bridge it does not need, hand it text evidence
@@ -662,7 +716,7 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
662
716
  return (async function* () {
663
717
  const converted = await convertImagesToEvidence(ctx, options.messages, options.signal, self)
664
718
  const messages = restoreUpstreamSource(converted, providerId, upstream)
665
- yield* ctx.llm.stream({ ...options, provider: upstream, messages })
719
+ yield* llm.stream({ ...options, provider: upstream, messages })
666
720
  })()
667
721
  },
668
722
  evidenceCache,
@@ -672,6 +726,7 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
672
726
  // duplicate below means someone else holds that id, and skipping a
673
727
  // provider we do not own would let a real vision model's paste be
674
728
  // taken over, which is the bug the verdict exists to prevent.
729
+ claimedProviders.add(providerId)
675
730
  ownProviders?.add(providerId)
676
731
  return true
677
732
  } catch (error) {
@@ -695,6 +750,7 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
695
750
  const dropWrapper = (upstream, current) => {
696
751
  registrations.delete(upstream)
697
752
  wrapped.delete(upstream)
753
+ claimedProviders.delete(current.providerId)
698
754
  ownProviders?.delete(current.providerId)
699
755
  if (typeof current.registration === 'function') current.registration()
700
756
  }
@@ -704,9 +760,9 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
704
760
  // failed and what to do next depends on whether it failed before or after
705
761
  // the host committed.
706
762
  const routed = (providerId) => {
707
- if (typeof ctx.llm.listProviders !== 'function') return false
763
+ if (typeof llm.listProviders !== 'function') return false
708
764
  try {
709
- return ctx.llm.listProviders().some((info) => (typeof info === 'string' ? info : info?.id) === providerId)
765
+ return llm.listProviders().some((info) => (typeof info === 'string' ? info : info?.id) === providerId)
710
766
  } catch {
711
767
  return false
712
768
  }
@@ -724,7 +780,7 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
724
780
  let nextPolicyKey
725
781
  try {
726
782
  nextPolicyKey =
727
- typeof ctx.llm.providerRetryPolicy === 'function' ? policyKey(ctx.llm.providerRetryPolicy(upstream)) : undefined
783
+ typeof llm.providerRetryPolicy === 'function' ? policyKey(llm.providerRetryPolicy(upstream)) : undefined
728
784
  } catch (error) {
729
785
  dropWrapper(upstream, current)
730
786
  console.error(`[modlens] vision provider refresh removed (${current.providerId}): ${error}`)
@@ -772,9 +828,9 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
772
828
  // plumbing below could never correct it, because the name it compared
773
829
  // against was a constant.
774
830
  const upstreamName = () => {
775
- if (typeof ctx.llm.listProviders !== 'function') return upstream
831
+ if (typeof llm.listProviders !== 'function') return upstream
776
832
  try {
777
- const found = ctx.llm.listProviders().find((entry) => entry.id === upstream)
833
+ const found = llm.listProviders().find((entry) => entry.id === upstream)
778
834
  return found?.name ?? upstream
779
835
  } catch {
780
836
  return upstream
@@ -789,6 +845,7 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
789
845
  // re-examined when the holder's route disappears.
790
846
  let claimedElsewhere = false
791
847
  const reconcile = () => {
848
+ if (!activationCanCommit()) return
792
849
  if (reconciling) {
793
850
  // dropWrapper's disposer makes the host emit adapters-updated while
794
851
  // this very run is on the stack, and whatever that event announced
@@ -800,8 +857,8 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
800
857
  try {
801
858
  const current = registrations.get(upstream)
802
859
  const available =
803
- typeof ctx.llm.listProviders !== 'function' ||
804
- ctx.llm.listProviders().some((info) => (typeof info === 'string' ? info : info?.id) === upstream)
860
+ typeof llm.listProviders !== 'function' ||
861
+ llm.listProviders().some((info) => (typeof info === 'string' ? info : info?.id) === upstream)
805
862
  if (!current) {
806
863
  if (claimedElsewhere) {
807
864
  if (routed(providerId)) return
@@ -848,7 +905,7 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
848
905
  }
849
906
  reconcile()
850
907
  if (typeof ctx.on === 'function') ctx.on('llm/adapters-updated', reconcile)
851
- return
908
+ return deactivate
852
909
  }
853
910
 
854
911
  // Auto-discovery. `wrapped` guards duplicates across sweeps and the
@@ -860,16 +917,19 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
860
917
  // one promise chain so two can never interleave their probes at all.
861
918
  const discover = Array.isArray(config.discover) ? new Set(config.discover) : null
862
919
  const sweepOnce = async () => {
920
+ if (!activationCanCommit()) return
863
921
  try {
864
922
  await sweepBody()
865
923
  } catch (error) {
924
+ if (!active) return
866
925
  // A sweep failure must never become an unhandled rejection inside the
867
926
  // host process; the next topology notification simply tries again.
868
927
  console.error(`[modlens] vision provider discovery sweep failed: ${error}`)
869
928
  }
870
929
  }
871
930
  const sweepBody = async () => {
872
- if (typeof ctx.llm.listProviders !== 'function') {
931
+ if (!active) return
932
+ if (typeof llm.listProviders !== 'function') {
873
933
  // Older registry surface: fall back to the single legacy wrap once.
874
934
  if (!wrapped.has('__legacy_fallback__')) {
875
935
  wrapped.add('__legacy_fallback__')
@@ -877,15 +937,18 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
877
937
  }
878
938
  return
879
939
  }
880
- const providers = ctx.llm.listProviders()
940
+ const providers = llm.listProviders()
941
+ if (!active) return
881
942
  // Same tolerance as the pinned path: an entry may be a bare id string.
882
943
  const idOf = (info) => (typeof info === 'string' ? info : info?.id)
883
944
  const available = new Set(providers.map(idOf).filter(Boolean))
884
945
  for (const [upstream, current] of registrations) {
946
+ if (!active) return
885
947
  if (available.has(upstream)) continue
886
948
  dropWrapper(upstream, current)
887
949
  }
888
950
  for (const info of providers) {
951
+ if (!active) return
889
952
  const id = idOf(info)
890
953
  if (!id || String(id).startsWith('modlens-')) continue
891
954
  if (discover && !discover.has(id)) continue
@@ -900,13 +963,19 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
900
963
  wrapped.add(id)
901
964
  let models = []
902
965
  try {
903
- models = await ctx.llm.listModels(id)
966
+ models = await llm.listModels(id)
904
967
  } catch {
968
+ if (!activationCanCommit()) return
905
969
  // Unreachable route today; release the claim so a later topology
906
970
  // change retries it.
907
971
  wrapped.delete(id)
908
972
  continue
909
973
  }
974
+ // The promise can settle just before Cordis marks this fiber UNLOADING,
975
+ // while the activation disposer is still one microtask away. Re-enter
976
+ // the atomic lifecycle boundary before either continuing to refresh a
977
+ // later registration or committing this provider's wrapper.
978
+ if (!activationCanCommit()) return
910
979
  if (!models.some(shouldWrap)) {
911
980
  // No eligible models yet: release, the route may gain some later.
912
981
  wrapped.delete(id)
@@ -931,6 +1000,7 @@ function registerVisionProvider(ctx, config, ownProviders, evidenceCache) {
931
1000
  void sweep()
932
1001
  })
933
1002
  }
1003
+ return deactivate
934
1004
  }
935
1005
 
936
1006
  // The same pasted attachment rides every later step of its session, and the
@@ -1397,16 +1467,27 @@ const REUSE_HARNESSES = ['claude', 'codex', 'opencode', 'pi', 'grok']
1397
1467
  // one source whole, so the card has to read the same two places a read does or
1398
1468
  // it shows an empty form for an engine that works.
1399
1469
  const ENGINE_ENV_BINDINGS = {
1400
- 'gemini-api': { apiKey: 'GEMINI_API_KEY' },
1470
+ 'gemini-api': { apiKey: 'GEMINI_API_KEY', baseUrl: 'GEMINI_BASE_URL' },
1401
1471
  openai: { apiKey: 'OPENAI_API_KEY', baseUrl: 'OPENAI_BASE_URL' },
1402
1472
  anthropic: { apiKey: 'ANTHROPIC_API_KEY', baseUrl: 'ANTHROPIC_BASE_URL' },
1403
1473
  }
1404
1474
 
1475
+ /** Whether a comma-separated API-key value contains at least one real key. */
1476
+ function hasApiKeys(value) {
1477
+ return (
1478
+ typeof value === 'string' &&
1479
+ value
1480
+ .split(',')
1481
+ .map((key) => key.trim())
1482
+ .some((key) => key !== '')
1483
+ )
1484
+ }
1485
+
1405
1486
  function engineEnvSettings(engine, env = process.env) {
1406
1487
  const settings = {}
1407
1488
  for (const [field, variable] of Object.entries(ENGINE_ENV_BINDINGS[engine] ?? {})) {
1408
1489
  const value = typeof env[variable] === 'string' ? env[variable].trim() : ''
1409
- if (value !== '') settings[field] = value
1490
+ if (value !== '' && (field !== 'apiKey' || hasApiKeys(value))) settings[field] = value
1410
1491
  }
1411
1492
  return settings
1412
1493
  }
@@ -1472,7 +1553,7 @@ function engineSummary(config = readModlensConfig()) {
1472
1553
  engines[name] = {
1473
1554
  baseUrl: typeof settings.baseUrl === 'string' ? settings.baseUrl : '',
1474
1555
  model: typeof settings.model === 'string' ? settings.model : '',
1475
- hasKey: typeof settings.apiKey === 'string' && settings.apiKey !== '',
1556
+ hasKey: hasApiKeys(settings.apiKey),
1476
1557
  // '' means neither source holds anything, which is not the same as the
1477
1558
  // file holding an empty entry: that one is already off its variables.
1478
1559
  source: inFile ? 'file' : Object.keys(settings).length > 0 ? 'env' : '',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liustack/modlens",
3
- "version": "3.23.1",
3
+ "version": "3.24.1",
4
4
  "description": "Plug-in vision for text-only LLMs, powered by the free Antigravity CLI",
5
5
  "type": "module",
6
6
  "bin": {
@@ -20,11 +20,11 @@ powershell -ExecutionPolicy Bypass -File <skill-dir>\scripts\run.ps1 <args>
20
20
 
21
21
  It resolves a working runtime (PATH `modlens`, then `npx`, then `bunx`) and forwards your arguments unchanged. Exit 78 means no runtime: relay the `nextSteps` from its stderr JSON instead of retrying.
22
22
 
23
- If your harness forbids running scripts, reason through the same order by hand and run the first line that works (the pinned version is 3.23.1):
23
+ If your harness forbids running scripts, reason through the same order by hand and run the first line that works (the pinned version is 3.24.1):
24
24
 
25
- 1. A `modlens` on `PATH` whose major version is 3 and is at least 3.23.1: `modlens <args>`.
26
- 2. Otherwise, if `npx` exists: `npx --yes --package @liustack/modlens@3.23.1 modlens <args>`.
27
- 3. Otherwise, if `bunx` exists: `bunx --bun @liustack/modlens@3.23.1 <args>`.
25
+ 1. A `modlens` on `PATH` whose major version is 3 and is at least 3.24.1: `modlens <args>`.
26
+ 2. Otherwise, if `npx` exists: `npx --yes --package @liustack/modlens@3.24.1 modlens <args>`.
27
+ 3. Otherwise, if `bunx` exists: `bunx --bun @liustack/modlens@3.24.1 <args>`.
28
28
  4. Otherwise tell the user no JavaScript runtime was found and that installing Node 22.19+ (https://nodejs.org) or Bun (https://bun.sh) is the next step. Do not claim modlens itself failed.
29
29
 
30
30
  `references/runtime.md` documents the pin and the diagnostic fields.
@@ -19,11 +19,12 @@ modlens config set <provider>.<field> <value> # fields: apiKey, baseUrl, model
19
19
 
20
20
  ## The file's exact shape
21
21
 
22
- Everything lives under six top-level keys, all optional. This example shows every supported key and field at once (a real file only needs what you use). A missing file means all defaults. Provider settings sit under `providers.<name>`, not at the top level, which is the mistake hand-editors make most.
22
+ Everything lives under seven top-level keys, all optional. This example shows every supported key and field at once (a real file only needs what you use). A missing file means all defaults. Provider settings sit under `providers.<name>`, not at the top level, which is the mistake hand-editors make most.
23
23
 
24
24
  ```json
25
25
  {
26
26
  "provider": "gemini-api",
27
+ "cooldown": "on",
27
28
  "proxy": "http://127.0.0.1:7890",
28
29
  "reuse": { "claude": true, "codex": true, "opencode": false, "pi": true, "grok": true },
29
30
  "saved": {
@@ -64,7 +65,8 @@ Everything lives under six top-level keys, all optional. This example shows ever
64
65
  Field semantics:
65
66
 
66
67
  - `provider`: which provider runs when `-p` is not given. Canonical names or aliases both work (`agy`/`antigravity` for `antigravity-cli`, `gemini` for `gemini-api`, `openai-compat` for `openai`, `claude` for `anthropic`, `kimi`/`kimi-code` for `kimi-cli`, `claude-code` for `claude-cli`). Empty or absent pins nothing: the failover chain decides, trying configured API providers before the agent CLIs.
67
- - `providers.<name>.<field>`: six fields exist, `apiKey`, `baseUrl`, `model`, `proxy`, `extraBody`, and `structuredOutput` (the openai route only). Every provider entry is optional, and every field inside it is optional. Alias keys are read too (settings saved under `gemini` are found when `gemini-api` resolves), with the canonical key winning on conflict.
68
+ - `cooldown`: `'on'` (default) or `'off'`. On, a quota-spent key is remembered in `~/.modlens/state.json` and tried last until it recovers (45 minutes by default, 24 hours for monthly HTTP 432/433, or the engine-reported `Resets in` clause). Off, that file is neither read nor written. `modlens state clear` forgets every cooldown.
69
+ - `providers.<name>.<field>`: six fields exist, `apiKey`, `baseUrl`, `model`, `proxy`, `extraBody`, and `structuredOutput` (the openai route only). Every provider entry is optional, and every field inside it is optional. Alias keys are read too (settings saved under `gemini` are found when `gemini-api` resolves), with the canonical key winning on conflict. `apiKey` accepts a comma-separated list. Requests use the configured order and rotate only after authentication, rate-limit, or quota failures. Other failures skip remaining keys and keep provider failover.
68
70
  - `providers.<name>.extraBody`: a JSON object merged into the request body of the API providers (`gemini-api`, `openai`, `anthropic`), for whatever knobs that vendor has and modlens has no flag for. Turning thinking off is the usual reason, see the section below. Nested objects merge key by key, so adding one knob leaves the rest of that block alone. The fields carrying the image, the prompt, and each route's own enforcement machinery are refused with an error naming the field. `response_format` on the `openai` route is not one of them: setting it there deliberately replaces the schema modlens would otherwise send. The three CLI providers take no request body, so a run on `antigravity-cli`, `claude-cli` or `kimi-cli` ignores it and says so in `meta.warnings`.
69
71
  - `providers.openai.structuredOutput`: `true` asks an OpenAI-compatible gateway to enforce the vision contract itself, as `response_format: json_schema` in the strict form those endpoints require. Off by default, since a gateway without structured-output support answers 400 for the field. A `response_format` you set in `extraBody` wins over it.
70
72
  - `saved.openai.<label>`: named saved copies of the openai slot, written only by `modlens config save openai <label>` and swapped in whole by `modlens config use openai <label>`. Switching gateways used to mean overwriting `providers.openai` and losing the previous key; a saved copy is where it survives. `use` refuses to overwrite an active slot that no label holds (pass `--discard` to drop it deliberately), and nothing in resolution, guards, or the env bindings reads this section: the active slot stays the only openai route in any run.
@@ -73,7 +75,7 @@ Field semantics:
73
75
  - `allowModels` non-empty (allowlist mode): only the listed models run the engine, every other identified model is denied. Right for the actual 2026 landscape, where text-only models are the short list. A deny pattern still wins over an allow match, so a broad allow can have its vision variants carved out, as in the example above: `glm-5.*` allows the text line while `glm-*v*` catches `glm-5v-turbo`. Anchor allow patterns tightly (`deepseek-v4-*`, not `deepseek*`) so a vendor's next multimodal generation falls off the list and steps aside until you have checked it.
74
76
  - List a model by what actually reaches it, not by what it could see: a multimodal model behind a gateway that strips images still needs modlens, and your session transcript records the model name the gateway reports. `modlens doctor`'s Guard section shows the rules and a live verdict for checking the result.
75
77
  - `denyWhenUnknown` (default `false`) decides what happens when no signal identifies the active model, in either mode: `false` proceeds, `true` denies. The active model is detected from, strongest first: the `MODLENS_MODEL` env var (`none` means "treat as unknown"), the harness's session storage, the `--model` self-report.
76
- - `GEMINI_API_KEY`, `OPENAI_API_KEY`, `OPENAI_BASE_URL`, `ANTHROPIC_API_KEY` and `ANTHROPIC_BASE_URL` configure a provider this file says nothing about, and are ignored entirely for one it does. They used to merge field by field, which built pairings that existed nowhere: a baseUrl and an apiKey are one credential. modlens still reads `MODLENS_HARNESS` (paste-recovery and guard scope), `MODLENS_MODEL` (guard override, see `guards`), and the fingerprints harnesses inject themselves, which pin the guard's storage lookup to the current session: `CLAUDE_CODE_SESSION_ID`, `CODEX_THREAD_ID`, plus the presence markers harness detection relies on (`CLAUDECODE`, `PI_CODING_AGENT`, `CODEX_SANDBOX`).
78
+ - `GEMINI_API_KEY`, `GEMINI_BASE_URL`, `OPENAI_API_KEY`, `OPENAI_BASE_URL`, `ANTHROPIC_API_KEY` and `ANTHROPIC_BASE_URL` configure a provider this file says nothing about, and are ignored entirely for one it does. They used to merge field by field, which built pairings that existed nowhere: a baseUrl and an apiKey are one credential. The key variables accept a comma-separated list the same way the file field does. modlens still reads `MODLENS_HARNESS` (paste-recovery and guard scope), `MODLENS_MODEL` (guard override, see `guards`), and the fingerprints harnesses inject themselves, which pin the guard's storage lookup to the current session: `CLAUDE_CODE_SESSION_ID`, `CODEX_THREAD_ID`, plus the presence markers harness detection relies on (`CLAUDECODE`, `PI_CODING_AGENT`, `CODEX_SANDBOX`).
77
79
  - `reuse.<claude|codex|opencode|pi|grok>`: per-harness grants for spending other local logins, written by the onboarding conversation (`references/onboard.md`). `true` lets reads reuse that harness (pi credentials join the inline region with every guard intact; a signed-in Codex, an OpenCode vision model, or pi driven directly join the agent region before `claude-cli`), `false` records a refusal so the user is never re-asked, absent means never asked and nothing runs. `claude` absent counts as granted: `claude-cli` predates this model as a built-in provider, and `reuse.claude false` removes it from the chain (`-p claude-cli` still pins). Reused engines get no priority over the user's own: regions order by speed class only. Every reused answer adds a `meta.warnings` line naming whose quota it spent, and `modlens doctor`'s Reuse section shows each harness's decision plus what discovery found (probe results cache for 6 hours in `~/.modlens/auto-cache.json`; doctor always re-probes). Set with `modlens config set reuse.codex true` (empty clears back to never-asked).
78
80
  - Unknown top-level keys and unknown provider names are ignored rather than rejected, so a typo fails quiet: run `modlens doctor` after hand-editing, it shows which file and env values are actually in effect.
79
81