dsh-chrome-use 0.9.4 → 0.12.0
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 +46 -0
- package/README.md +4 -0
- package/docs/chrome-session-debugging.md +1 -1
- package/extension/background.js +133 -19
- package/extension/controller.js +441 -68
- package/extension/failures.js +188 -0
- package/extension/manifest.json +1 -1
- package/extension/page-content.js +74 -5
- package/extension/protocol.js +8 -1
- package/package.json +1 -1
- package/src/bridge.js +5 -1
- package/src/client.js +1 -1
- package/src/index.js +67 -4
- package/src/skill.js +19 -13
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,52 @@
|
|
|
3
3
|
版本号遵循语义化版本。格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/)。
|
|
4
4
|
扩展版本和 Harness 插件版本始终一起发布,数字保持一致。
|
|
5
5
|
|
|
6
|
+
## [0.12.0] - 2026-11-18
|
|
7
|
+
|
|
8
|
+
### 新增
|
|
9
|
+
|
|
10
|
+
- chrome_cleanup:关闭本会话用 chrome_open 打开的标签页,避免一个任务结束后留下几十个标签页。dryRun=true 可以先看会关哪些,keep=[tabId] 保留指定的页面;只关本会话自己打开的页面,不会关用户自己打开的页面、只是 claim 的页面或其他会话的页面。任务结束时显式调用一次即可。
|
|
11
|
+
- WebSocket 断开时自动做同样的清理:只关闭本会话用 chrome_open 打开的标签页,用户自己打开的页面和其他会话的页面保持原样;重连不会误关。
|
|
12
|
+
- chrome_tabs 的输出增加 windowId,方便区分不同 Chrome 窗口里的标签页。
|
|
13
|
+
|
|
14
|
+
### 文档
|
|
15
|
+
|
|
16
|
+
- 运行期 skill 指南补充 chrome_cleanup、断线自动清理和「只清理自己打开的页面」的边界。
|
|
17
|
+
|
|
18
|
+
## [0.11.0] - 2026-11-18
|
|
19
|
+
|
|
20
|
+
### 新增
|
|
21
|
+
|
|
22
|
+
- chrome_emulate:不改 DOM,只改当前标签页的渲染条件。支持视口尺寸和 mobile(width/height 必须一起给)、deviceScaleFactor、colorScheme(light/dark)、reducedMotion、forcedColors、network(offline/slow-3g/fast-3g/online)、cpuThrottlingRate(1-20)、timezone 和 locale。用于响应式、深色模式、弱网/离线、慢设备和本地化检查;布局、媒体查询、页面脚本和截图都会跟着变。
|
|
23
|
+
- 每次调用只更新传入的字段,其余保持;chrome_emulate reset=true 清除全部。覆盖只在当前占用期间有效,chrome_release 或断开时会自动恢复,避免把用户的标签页留在手机尺寸或离线下。chrome_doctor 会显示当前覆盖。
|
|
24
|
+
- 部分 CDP 调用失败时会先清掉已经生效的覆盖,再返回 EMULATION_FAILED,避免留下半套设置。部分页面只在加载时读取设置,需要 chrome_history action=reload 后再生效。
|
|
25
|
+
|
|
26
|
+
### 文档
|
|
27
|
+
|
|
28
|
+
- 运行期 skill 指南补充 chrome_emulate 的用法、reset 和重载说明。
|
|
29
|
+
|
|
30
|
+
## [0.10.0] - 2026-11-13
|
|
31
|
+
|
|
32
|
+
### 新增
|
|
33
|
+
|
|
34
|
+
- chrome_doctor:页面健康检查。不传 tabId 时报告连接、端口、扩展版本、插件版本和部署允许的工具;传 tabId 时再报告页面 URL、文档就绪状态、弹窗、验证码/人机校验、旧 ref、已执行动作数、控制台错误和未完成请求。不能确定为什么点不动时先查它。
|
|
35
|
+
- chrome_wait_response:等待某次网络响应(urlContains、method、status 组合),可读取响应体,替代「反复轮询 chrome_network」。
|
|
36
|
+
- chrome_assert:确认页面满足某个条件(元素可见/消失、选择器、文字、网址、predicate、networkIdle),默认立即返回,可短暂轮询;不满足时返回 ASSERT_FAILED,避免下一步靠猜。
|
|
37
|
+
- chrome_content:按 selector 读取一块页面正文,输出 markdown、纯文本、HTML 或 JSON 大纲,maxChars 限制长度。
|
|
38
|
+
- chrome_scrape:用 `name: selector` / `name: selector@attr` 字段表静态抽取结构化数据;给容器 selector 时逐容器重复。
|
|
39
|
+
- 同源 iframe 与 open shadow DOM 支持:快照/查找会给子框架元素标 frame=true,点击和填写的坐标会自动换算到顶层视口;content、scrape、wait、assert 的选择器能穿透 open shadow DOM。
|
|
40
|
+
- 快照和 chrome_doctor 增加 challenge 字段,识别 Cloudflare、reCAPTCHA、hCaptcha、Turnstile 等验证码/人机校验,提示交给用户处理。
|
|
41
|
+
- 动作历史:chrome_history 不传 action 时列出本会话在该标签页执行过的工具、目标、是否可能已发送输入和失败码;不记录输入文字、表达式、predicate 和文件路径。
|
|
42
|
+
|
|
43
|
+
### 变更
|
|
44
|
+
|
|
45
|
+
- 所有失败都带结构化字段:错误消息第二行为 `Failure: {"code","actionSent","retry","hint"}`。actionSent 说明输入是否可能已经生效(null 表示不确定),retry 给出下一步建议(snapshot/claim/other-tab/wait/inspect/user/fix-args/fix-page/none),hint 是一句可执行的下一步说明。扩展、bridge 和工具结果三层都会保留这组字段。
|
|
46
|
+
- 部署可以用 DSH_CHROME_USE_ALLOWED_ACTIONS 限制可用工具(逗号分隔短名,如 snapshot,tabs,click);设置后未列出的工具返回 ACTION_NOT_ALLOWED;诊断类工具(chrome_setup/status/pair/doctor/report)始终可用,chrome_status 和 chrome_doctor 显示允许清单。
|
|
47
|
+
|
|
48
|
+
### 文档
|
|
49
|
+
|
|
50
|
+
- 运行期 skill 指南补充新工具、结构化失败字段、同源 iframe/shadow DOM 说明和动作开关。
|
|
51
|
+
|
|
6
52
|
## [0.9.4] - 2026-11-13
|
|
7
53
|
|
|
8
54
|
### 变更
|
package/README.md
CHANGED
|
@@ -68,6 +68,8 @@ DSH_CHROME_USE_PORT=18793 DSH_CHROME_USE_NAME=Codex dsh
|
|
|
68
68
|
|
|
69
69
|
端口 18792 留给 `scripts/fixture.mjs` 测试页。自动选择会跳过它。
|
|
70
70
|
|
|
71
|
+
需要收窄权限时,用 `DSH_CHROME_USE_ALLOWED_ACTIONS` 限定这个部署能用的工具,逗号分隔短名,例如 `DSH_CHROME_USE_ALLOWED_ACTIONS=snapshot,tabs,click`。设置后没列出的工具会返回 `ACTION_NOT_ALLOWED`(fail-closed,写错名字等于禁用);`chrome_setup`、`chrome_status`、`chrome_pair`、`chrome_doctor`、`chrome_report` 这几个诊断工具始终可用。不设置则全部可用。`chrome_status` 和 `chrome_doctor` 会显示实际允许的清单。
|
|
72
|
+
|
|
71
73
|
自动模式共用同一个 `port.json`。同时运行多个 Harness 时,建议各自显式设置 `DSH_CHROME_USE_PORT`;否则端口可能随启动顺序变化,导致旧令牌失效、需要重新配对。
|
|
72
74
|
|
|
73
75
|
多个 Harness 可以同时操作不同的标签页,同一个标签页仍只归一个 Harness。另一个 Harness 抢先占用时返回 `TAB_OWNED_BY_ANOTHER_HARNESS`,不会排队等待。原来的单 Harness 安装不需要改配置。
|
|
@@ -154,6 +156,8 @@ Port 18792 is reserved for the `scripts/fixture.mjs` test page; automatic select
|
|
|
154
156
|
|
|
155
157
|
Automatic mode shares one `port.json`. When running several harnesses, set `DSH_CHROME_USE_PORT` explicitly for each; otherwise the port a harness gets can depend on startup order, which invalidates the old token and forces a new pairing.
|
|
156
158
|
|
|
159
|
+
To narrow the surface, set `DSH_CHROME_USE_ALLOWED_ACTIONS` to a comma-separated list of short tool names, for example `DSH_CHROME_USE_ALLOWED_ACTIONS=snapshot,tabs,click`. Tools outside the list fail with `ACTION_NOT_ALLOWED` (fail-closed: a typo disables the tool); the diagnostics `chrome_setup`, `chrome_status`, `chrome_pair`, `chrome_doctor` and `chrome_report` always stay available. Unset means every tool is available. `chrome_status` and `chrome_doctor` report the effective list.
|
|
160
|
+
|
|
157
161
|
Several harnesses can work on different tabs at the same time; a tab still belongs to only one harness. When another harness got there first you get `TAB_OWNED_BY_ANOTHER_HARNESS`, and there is no queueing. Existing single-harness setups need no configuration change.
|
|
158
162
|
|
|
159
163
|
Harnesses can also leave each other messages in the background: `chrome_send_message` broadcasts or targets a specific port, `chrome_messages` reads them, and the receiver sees an unread hint in the result of its next tool call. This is an asynchronous message board: it does not interrupt a running operation and is not shown in the extension popup. Details and limits: [Messages between harnesses](docs/agent-messaging.md).
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
|
|
22
22
|
## 读取结构和验证按钮
|
|
23
23
|
|
|
24
|
-
`chrome_snapshot` 默认返回页面的可访问性结构,包括链接、按钮等可操作元素,最多 500 个条目,结果中的 `truncated` 表示是否截断。读文章正文用 `format: "text"` 或 `format: "markdown"`,这两种模式不返回 ref,也不会让旧 ref
|
|
24
|
+
`chrome_snapshot` 默认返回页面的可访问性结构,包括链接、按钮等可操作元素,最多 500 个条目,结果中的 `truncated` 表示是否截断。读文章正文用 `format: "text"` 或 `format: "markdown"`,这两种模式不返回 ref,也不会让旧 ref 失效;也可以改用 `chrome_content` 按 selector 取一块区域,或 `chrome_scrape` 批量抽字段。元素太多时用 `chrome_find` 按名称和角色查找。快照不返回完整 HTML,也不返回输入框的值。同源子框架里的元素会带 `frame: true`;`challenge` 字段非空表示页面出现验证码/人机校验,需要用户处理。点不动或页面看起来不对时,先 `chrome_doctor` 看页面状态和未完成请求。
|
|
25
25
|
|
|
26
26
|
要检查目标区域,可用 `chrome_evaluate` 执行下面的表达式。先根据快照确认选择器,再替换示例中的 `main`。
|
|
27
27
|
|
package/extension/background.js
CHANGED
|
@@ -2,6 +2,7 @@ import { BrowserController } from './controller.js';
|
|
|
2
2
|
import { TabLeases } from './leases.js';
|
|
3
3
|
import { PORT, MAX_MESSAGE, PARALLEL_TABS, MESSAGE_UNREAD_MAX, validateArgs } from './protocol.js';
|
|
4
4
|
import { classifyPairingCode } from './pairing-code.js';
|
|
5
|
+
import { classifyFailure } from './failures.js';
|
|
5
6
|
|
|
6
7
|
// One Chrome extension can drive several harnesses at once. Each connection
|
|
7
8
|
// keeps its own socket, pairing token, command map and cleanup generation. The
|
|
@@ -35,7 +36,7 @@ let restoring;
|
|
|
35
36
|
// delivers synthesized input to the current tab. Queue them across tabs so two
|
|
36
37
|
// commands cannot switch the same window underneath each other. They do not
|
|
37
38
|
// touch window focus, so the user keeps typing in whichever app is in front.
|
|
38
|
-
const inputActions = new Set(['activate', 'close', 'click', 'fill', 'key', 'hover', 'select', 'upload', 'drag', 'scroll_to', 'scroll']);
|
|
39
|
+
const inputActions = new Set(['activate', 'cleanup', 'close', 'click', 'fill', 'key', 'hover', 'select', 'upload', 'drag', 'scroll_to', 'scroll']);
|
|
39
40
|
const inputQueue = [];
|
|
40
41
|
let inputBusy = false;
|
|
41
42
|
function acquireInput(signal) {
|
|
@@ -202,6 +203,7 @@ function createClient(port) {
|
|
|
202
203
|
name: null,
|
|
203
204
|
lastError: '',
|
|
204
205
|
tasks: new Map(),
|
|
206
|
+
opened: new Set(),
|
|
205
207
|
drain: undefined,
|
|
206
208
|
revoked: false,
|
|
207
209
|
abortStart: undefined,
|
|
@@ -231,7 +233,7 @@ async function saveToken(client, port, token, name) {
|
|
|
231
233
|
});
|
|
232
234
|
}
|
|
233
235
|
|
|
234
|
-
async function drainClient(client) {
|
|
236
|
+
async function drainClient(client, { closeOpened = false } = {}) {
|
|
235
237
|
if (client.drain) return client.drain;
|
|
236
238
|
client.drain = (async () => {
|
|
237
239
|
// Block commands as soon as cleanup starts, before the first await.
|
|
@@ -243,11 +245,12 @@ async function drainClient(client) {
|
|
|
243
245
|
// new one. Re-enumerate; never trust the pre-abort snapshot.
|
|
244
246
|
for (let pass = 0; pass < 2; pass++) {
|
|
245
247
|
const tabs = leases.ownedBy(client);
|
|
246
|
-
if (!tabs.length)
|
|
248
|
+
if (!tabs.length) break;
|
|
247
249
|
for (const tabId of tabs) leases.beginClose(tabId, client);
|
|
248
250
|
for (const tabId of tabs) {
|
|
249
251
|
if (leases.owner(tabId) !== client) continue;
|
|
250
252
|
if (leases.get(tabId)?.state === 'quarantined') continue;
|
|
253
|
+
const wasOpened = closeOpened && client.opened.has(tabId);
|
|
251
254
|
try {
|
|
252
255
|
await controller.detachTab(tabId);
|
|
253
256
|
} catch (error) {
|
|
@@ -259,6 +262,20 @@ async function drainClient(client) {
|
|
|
259
262
|
}
|
|
260
263
|
continue;
|
|
261
264
|
}
|
|
265
|
+
// Detach releases the lease through the controller callback, so remove
|
|
266
|
+
// the tab only after native cleanup; a failed remove leaves it open.
|
|
267
|
+
if (wasOpened) {
|
|
268
|
+
try { await chrome.tabs.remove(tabId); }
|
|
269
|
+
catch (error) {
|
|
270
|
+
const message = String(error?.message ?? error).slice(0, 2000);
|
|
271
|
+
if (!/No tab with id|Invalid tab ID/i.test(message)) {
|
|
272
|
+
client.lastError = message;
|
|
273
|
+
lastError = message;
|
|
274
|
+
portErrors.set(client.port, message);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
client.opened.delete(tabId);
|
|
278
|
+
}
|
|
262
279
|
// A task or a late callback may have released/reassigned the lease while
|
|
263
280
|
// native detach was running; do not clean up someone else's tab.
|
|
264
281
|
if (leases.owner(tabId) !== client) continue;
|
|
@@ -266,6 +283,26 @@ async function drainClient(client) {
|
|
|
266
283
|
else leases.release(tabId, client);
|
|
267
284
|
}
|
|
268
285
|
}
|
|
286
|
+
// A released tab still belongs to the task that opened it. Close those too
|
|
287
|
+
// on disconnect, but never a tab another Harness has claimed meanwhile.
|
|
288
|
+
if (closeOpened) {
|
|
289
|
+
for (const tabId of [...client.opened]) {
|
|
290
|
+
const owner = leases.owner(tabId);
|
|
291
|
+
if (owner && owner !== client) { client.opened.delete(tabId); continue; }
|
|
292
|
+
// A detach failure is not fatal here: the tab is already gone from our
|
|
293
|
+
// control, and remove() below is what actually closes it.
|
|
294
|
+
try { await controller.detachTab(tabId); } catch {}
|
|
295
|
+
try { await chrome.tabs.remove(tabId); }
|
|
296
|
+
catch (error) {
|
|
297
|
+
const message = String(error?.message ?? error).slice(0, 2000);
|
|
298
|
+
if (!/No tab with id|Invalid tab ID/i.test(message)) {
|
|
299
|
+
lastError = message;
|
|
300
|
+
portErrors.set(client.port, message);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
client.opened.delete(tabId);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
269
306
|
})();
|
|
270
307
|
return client.drain;
|
|
271
308
|
}
|
|
@@ -368,7 +405,7 @@ async function startClient(client, { code, restoreEpoch } = {}) {
|
|
|
368
405
|
client.connected = false;
|
|
369
406
|
clients.delete(port);
|
|
370
407
|
updateBadge();
|
|
371
|
-
drainClient(client).catch(error => { lastError = String(error.message); });
|
|
408
|
+
drainClient(client, { closeOpened: true }).catch(error => { lastError = String(error.message); });
|
|
372
409
|
finish(reject, new Error(client.lastError || `Connection to port ${port} closed. Reconnect to continue.`));
|
|
373
410
|
};
|
|
374
411
|
});
|
|
@@ -410,10 +447,20 @@ function connectPort(port, options = {}) {
|
|
|
410
447
|
return ready;
|
|
411
448
|
}
|
|
412
449
|
|
|
450
|
+
// Every failure keeps the plain CODE: message for older plugins and adds the
|
|
451
|
+
// structured parts (code, actionSent, retry, hint) for newer ones.
|
|
452
|
+
function failResult(action, message, error) {
|
|
453
|
+
const text = String(error?.message ?? error).slice(0, 2000);
|
|
454
|
+
// The controller already classified failures that crossed the page boundary
|
|
455
|
+
// (it knows the action); keep that object instead of re-deriving it from text.
|
|
456
|
+
const failure = error?.failure && typeof error.failure.code === 'string' ? error.failure : classifyFailure(text, action);
|
|
457
|
+
return { error: text, failure };
|
|
458
|
+
}
|
|
459
|
+
|
|
413
460
|
function sendResult(client, message, response) {
|
|
414
461
|
if (!isCurrent(client) || !client.connected || client.socket?.readyState !== WebSocket.OPEN) return;
|
|
415
462
|
const body = JSON.stringify({ type: 'result', id: message.id, ...response });
|
|
416
|
-
client.socket.send(body.length <= MAX_MESSAGE ? body : JSON.stringify({ type: 'result', id: message.id, error: 'RESULT_TOO_LARGE' }));
|
|
463
|
+
client.socket.send(body.length <= MAX_MESSAGE ? body : JSON.stringify({ type: 'result', id: message.id, error: 'RESULT_TOO_LARGE', failure: classifyFailure('RESULT_TOO_LARGE', message.action) }));
|
|
417
464
|
}
|
|
418
465
|
|
|
419
466
|
// Only the count and the senders reach the bridge; message text stays in
|
|
@@ -511,13 +558,79 @@ async function settleFailedLease(client, tabId, action) {
|
|
|
511
558
|
leases.release(tabId, client);
|
|
512
559
|
}
|
|
513
560
|
|
|
561
|
+
async function tabBrief(tabId) {
|
|
562
|
+
try {
|
|
563
|
+
const tab = await chrome.tabs.get(tabId);
|
|
564
|
+
return { tabId, title: String(tab.title ?? '').slice(0, 300), url: String(tab.url ?? '').slice(0, 2000) };
|
|
565
|
+
} catch {
|
|
566
|
+
return { tabId };
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// Close only tabs this connection created with chrome_open. A tab the user
|
|
571
|
+
// already had stays open even when this connection claimed it, because closing
|
|
572
|
+
// user tabs is never a cleanup side effect.
|
|
573
|
+
async function cleanupOpenedTabs(client, args, signal) {
|
|
574
|
+
const keep = new Set(args.keep ?? []);
|
|
575
|
+
const opened = [...client.opened];
|
|
576
|
+
const targets = opened.filter(tabId => !keep.has(tabId));
|
|
577
|
+
const kept = await Promise.all(opened.filter(tabId => keep.has(tabId)).map(tabBrief));
|
|
578
|
+
if (args.dryRun) {
|
|
579
|
+
const wouldClose = [];
|
|
580
|
+
const skipped = [];
|
|
581
|
+
for (const tabId of targets) {
|
|
582
|
+
const owner = leases.owner(tabId);
|
|
583
|
+
if (owner && owner !== client) skipped.push({ tabId, error: 'TAB_OWNED_BY_ANOTHER_HARNESS' });
|
|
584
|
+
else wouldClose.push(await tabBrief(tabId));
|
|
585
|
+
}
|
|
586
|
+
return { dryRun: true, wouldClose, kept, closed: [], failed: skipped, note: 'Dry run: nothing was closed.' };
|
|
587
|
+
}
|
|
588
|
+
const closed = [];
|
|
589
|
+
const failed = [];
|
|
590
|
+
for (const tabId of targets) {
|
|
591
|
+
signal.throwIfAborted();
|
|
592
|
+
const owner = leases.owner(tabId);
|
|
593
|
+
if (owner && owner !== client) {
|
|
594
|
+
client.opened.delete(tabId);
|
|
595
|
+
failed.push({ tabId, error: 'TAB_OWNED_BY_ANOTHER_HARNESS: another Harness claimed this tab; cleanup left it alone' });
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
const brief = await tabBrief(tabId);
|
|
599
|
+
try {
|
|
600
|
+
// Detach first so emulation and debugger state are restored before the
|
|
601
|
+
// tab disappears. A detach failure must not stop the tab from closing.
|
|
602
|
+
await controller.detachTab(tabId).catch(() => {});
|
|
603
|
+
await chrome.tabs.remove(tabId);
|
|
604
|
+
leases.release(tabId, client);
|
|
605
|
+
client.opened.delete(tabId);
|
|
606
|
+
closed.push(brief);
|
|
607
|
+
} catch (error) {
|
|
608
|
+
const message = String(error?.message ?? error).slice(0, 500);
|
|
609
|
+
if (/No tab with id|Invalid tab ID/i.test(message)) {
|
|
610
|
+
leases.release(tabId, client);
|
|
611
|
+
client.opened.delete(tabId);
|
|
612
|
+
closed.push(brief);
|
|
613
|
+
} else {
|
|
614
|
+
failed.push({ ...brief, error: message });
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return {
|
|
619
|
+
dryRun: false,
|
|
620
|
+
closed,
|
|
621
|
+
kept,
|
|
622
|
+
failed,
|
|
623
|
+
note: closed.length ? 'Closed tabs opened by this Harness. User-opened tabs and other Harnesses are untouched.' : 'No tabs opened by this Harness were open.',
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
|
|
514
627
|
async function handleCommand(client, message) {
|
|
515
628
|
const id = message.id;
|
|
516
629
|
if (typeof id !== 'string' || id.length > 200 || !Number.isFinite(message.deadline) || message.deadline <= Date.now() || message.deadline > Date.now() + 20000) {
|
|
517
630
|
throw new Error('Invalid command deadline');
|
|
518
631
|
}
|
|
519
632
|
if (client.tasks.has(id)) {
|
|
520
|
-
sendResult(client, message,
|
|
633
|
+
sendResult(client, message, failResult(message.action, message, 'DUPLICATE_COMMAND_ID: this command id is already running'));
|
|
521
634
|
return;
|
|
522
635
|
}
|
|
523
636
|
const action = message.action;
|
|
@@ -525,7 +638,7 @@ async function handleCommand(client, message) {
|
|
|
525
638
|
try {
|
|
526
639
|
args = validateArgs(`chrome_${action}`, message.args);
|
|
527
640
|
} catch (error) {
|
|
528
|
-
sendResult(client, message,
|
|
641
|
+
sendResult(client, message, failResult(action, message, error));
|
|
529
642
|
return;
|
|
530
643
|
}
|
|
531
644
|
// Inbox tools only touch extension storage: they need no tab, input lock,
|
|
@@ -535,35 +648,35 @@ async function handleCommand(client, message) {
|
|
|
535
648
|
if (action === 'send_message') await sendAgentMessage(client, message, args);
|
|
536
649
|
else await readAgentMessages(client, message, args);
|
|
537
650
|
} catch (error) {
|
|
538
|
-
sendResult(client, message,
|
|
651
|
+
sendResult(client, message, failResult(action, message, error));
|
|
539
652
|
}
|
|
540
653
|
return;
|
|
541
654
|
}
|
|
542
655
|
const tabId = Number.isSafeInteger(args.tabId) ? args.tabId : undefined;
|
|
543
656
|
const lease = tabId === undefined ? undefined : leases.get(tabId);
|
|
544
657
|
if (lease?.state === 'quarantined') {
|
|
545
|
-
sendResult(client, message,
|
|
658
|
+
sendResult(client, message, failResult(action, message, 'TAB_LEASE_QUARANTINED: this tab did not finish native cleanup; reload the extension to clear it'));
|
|
546
659
|
return;
|
|
547
660
|
}
|
|
548
661
|
if (lease?.state === 'closing') {
|
|
549
|
-
sendResult(client, message,
|
|
662
|
+
sendResult(client, message, failResult(action, message, 'TAB_LEASE_CLOSING: this tab is being cleaned up; retry after cleanup settles'));
|
|
550
663
|
return;
|
|
551
664
|
}
|
|
552
665
|
if (tabId !== undefined && busyTabs.has(tabId)) {
|
|
553
|
-
sendResult(client, message,
|
|
666
|
+
sendResult(client, message, failResult(action, message, 'TAB_BUSY: another command is already running on this tab'));
|
|
554
667
|
return;
|
|
555
668
|
}
|
|
556
669
|
if (action === 'claim' && tabId !== undefined) {
|
|
557
670
|
try { leases.acquire(tabId, client); }
|
|
558
|
-
catch (error) { sendResult(client, message,
|
|
671
|
+
catch (error) { sendResult(client, message, failResult(action, message, error)); return; }
|
|
559
672
|
} else if (action === 'release' || action === 'close') {
|
|
560
673
|
const owner = tabId === undefined ? undefined : leases.owner(tabId);
|
|
561
|
-
if (!owner) { sendResult(client, message,
|
|
562
|
-
if (owner !== client) { sendResult(client, message,
|
|
674
|
+
if (!owner) { sendResult(client, message, failResult(action, message, 'TAB_NOT_LEASED: claim the tab first')); return; }
|
|
675
|
+
if (owner !== client) { sendResult(client, message, failResult(action, message, 'TAB_OWNED_BY_ANOTHER_HARNESS: another Harness has claimed this tab; use another tab or release it there')); return; }
|
|
563
676
|
} else if (tabId !== undefined) {
|
|
564
677
|
const owner = leases.owner(tabId);
|
|
565
|
-
if (!owner) { sendResult(client, message,
|
|
566
|
-
if (owner !== client) { sendResult(client, message,
|
|
678
|
+
if (!owner) { sendResult(client, message, failResult(action, message, 'TAB_NOT_LEASED: claim the tab first')); return; }
|
|
679
|
+
if (owner !== client) { sendResult(client, message, failResult(action, message, 'TAB_OWNED_BY_ANOTHER_HARNESS: another Harness has claimed this tab; use another tab or release it there')); return; }
|
|
567
680
|
}
|
|
568
681
|
|
|
569
682
|
const cancellation = new AbortController();
|
|
@@ -584,11 +697,12 @@ async function handleCommand(client, message) {
|
|
|
584
697
|
releaseInput = await acquireInput(cancellation.signal);
|
|
585
698
|
}
|
|
586
699
|
const options = action === 'open'
|
|
587
|
-
? { onTabCreated: tabId => { openedTabId = tabId;
|
|
700
|
+
? { onTabCreated: tabId => { openedTabId = tabId; const reserved = leases.reserve(tabId, client); if (reserved) client.opened.add(tabId); return reserved; } }
|
|
588
701
|
: undefined;
|
|
589
|
-
response = { result: await controller.execute(action, args, cancellation.signal, options) };
|
|
702
|
+
response = { result: action === 'cleanup' ? await cleanupOpenedTabs(client, args, cancellation.signal) : await controller.execute(action, args, cancellation.signal, options) };
|
|
703
|
+
if (action === 'close' && tabId !== undefined) client.opened.delete(tabId);
|
|
590
704
|
} catch (error) {
|
|
591
|
-
response =
|
|
705
|
+
response = failResult(action, message, error);
|
|
592
706
|
} finally {
|
|
593
707
|
clearTimeout(deadline);
|
|
594
708
|
try {
|