agent-bridge-service 0.2.3 → 1.1.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 +122 -140
- package/README.md +526 -36
- package/README.zh-CN.md +457 -34
- package/assets/agent-icons/agy.svg +27 -0
- package/dist/adapters/acp-auth.js +51 -0
- package/dist/adapters/acp-codebuddy.js +507 -0
- package/dist/adapters/agent-adapters.js +470 -46
- package/dist/adapters/claude-stream.js +159 -0
- package/dist/adapters/codex-app-server.js +365 -0
- package/dist/adapters/host-env.js +48 -0
- package/dist/adapters/mcp-elicitation.js +148 -0
- package/dist/adapters/mcp-host.js +70 -0
- package/dist/adapters/step-parsers.js +583 -0
- package/dist/app/bootstrap.js +163 -74
- package/dist/app/claude-resident.js +204 -0
- package/dist/app/conversation.js +639 -0
- package/dist/app/service-gate.js +1 -1
- package/dist/app/session.js +426 -14
- package/dist/app/task-dispatch.js +883 -7
- package/dist/app/task-registry.js +168 -0
- package/dist/cli/dispatch.js +4 -0
- package/dist/cli/flags.js +11 -0
- package/dist/cli/help.js +26 -7
- package/dist/cli/panel.js +334 -0
- package/dist/cli/render.js +2 -0
- package/dist/commands/approvals.js +80 -0
- package/dist/commands/connect.js +124 -4
- package/dist/commands/daemon-service.js +81 -0
- package/dist/commands/daemon.js +269 -6
- package/dist/commands/logout.js +13 -10
- package/dist/commands/serve.js +100 -12
- package/dist/commands/sessions.js +106 -0
- package/dist/core/ansi.js +5 -0
- package/dist/core/artifact-extract.js +158 -0
- package/dist/core/artifact-transfer.js +583 -0
- package/dist/core/device-identity.js +22 -0
- package/dist/core/emitter.js +50 -8
- package/dist/core/errors.js +43 -0
- package/dist/core/form-schema.js +72 -0
- package/dist/core/interaction-broker.js +440 -0
- package/dist/core/interaction-url.js +63 -0
- package/dist/core/partial-messages.js +99 -0
- package/dist/core/permission-ledger.js +146 -0
- package/dist/core/permission-policy.js +88 -0
- package/dist/core/port-fallback.js +22 -0
- package/dist/core/redact.js +13 -0
- package/dist/core/scan.js +12 -7
- package/dist/core/session-store.js +1632 -0
- package/dist/core/task-supervisor.js +968 -30
- package/dist/infra/atomic-file.js +111 -0
- package/dist/infra/audit.js +7 -0
- package/dist/infra/auth.js +143 -40
- package/dist/infra/config.js +511 -72
- package/dist/infra/device-identity-store.js +87 -0
- package/dist/infra/jwt.js +149 -0
- package/dist/infra/profile-lock.js +295 -0
- package/dist/infra/proxy-env.js +81 -0
- package/dist/infra/service-token.js +25 -4
- package/dist/protocol.js +298 -2
- package/dist/transport/a2a-mapping.js +206 -0
- package/dist/transport/acp-gateway-mapping.js +186 -0
- package/dist/transport/bridge-client.js +31 -22
- package/dist/transport/control-plane-page.js +820 -0
- package/dist/transport/control-plane-sessions-page.js +90 -0
- package/dist/transport/control-plane.js +935 -71
- package/dist/transport/gateway.js +1023 -0
- package/dist/transport/office-preview.js +154 -0
- package/package.json +6 -3
- package/assets/agent-icons/gemini.svg +0 -1
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interaction URL vault (1.0).
|
|
3
|
+
*
|
|
4
|
+
* URLs can contain one-time secrets/tokens, so they are intentionally kept out of SQLite and logs.
|
|
5
|
+
* They live in this process only and expire after a short TTL; restart clears them.
|
|
6
|
+
*/
|
|
7
|
+
export class InteractionUrlVault {
|
|
8
|
+
entries = new Map();
|
|
9
|
+
key(sessionId, requestId) {
|
|
10
|
+
return `${sessionId}\u0000${requestId}`;
|
|
11
|
+
}
|
|
12
|
+
register(sessionId, requestId, url, ttlMs = 5 * 60 * 1000, now = Date.now()) {
|
|
13
|
+
// URL 只在内存里;注册时顺手回收过期项,避免长期运行后 Map 只增不减。
|
|
14
|
+
this.cleanup(now);
|
|
15
|
+
const entry = {
|
|
16
|
+
sessionId,
|
|
17
|
+
requestId,
|
|
18
|
+
url,
|
|
19
|
+
expiresAtMs: now + Math.max(1, ttlMs),
|
|
20
|
+
expiresAt: new Date(now + Math.max(1, ttlMs)).toISOString(),
|
|
21
|
+
};
|
|
22
|
+
this.entries.set(this.key(sessionId, requestId), entry);
|
|
23
|
+
return { sessionId, requestId, url, expiresAt: entry.expiresAt };
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 区分「没有」和「过期」:调用方要能回 interaction_url_expired,而不是把过期当不存在。
|
|
27
|
+
* 过期项读取即删除,避免长期占内存。
|
|
28
|
+
*/
|
|
29
|
+
lookup(sessionId, requestId, now = Date.now()) {
|
|
30
|
+
const entry = this.entries.get(this.key(sessionId, requestId));
|
|
31
|
+
if (!entry)
|
|
32
|
+
return { expired: false };
|
|
33
|
+
if (entry.expiresAtMs <= now) {
|
|
34
|
+
this.entries.delete(this.key(sessionId, requestId));
|
|
35
|
+
return { expired: true };
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
entry: {
|
|
39
|
+
sessionId: entry.sessionId,
|
|
40
|
+
requestId: entry.requestId,
|
|
41
|
+
url: entry.url,
|
|
42
|
+
expiresAt: entry.expiresAt,
|
|
43
|
+
},
|
|
44
|
+
expired: false,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
get(sessionId, requestId, now = Date.now()) {
|
|
48
|
+
return this.lookup(sessionId, requestId, now).entry;
|
|
49
|
+
}
|
|
50
|
+
revoke(sessionId, requestId) {
|
|
51
|
+
this.entries.delete(this.key(sessionId, requestId));
|
|
52
|
+
}
|
|
53
|
+
cleanup(now = Date.now()) {
|
|
54
|
+
let removed = 0;
|
|
55
|
+
for (const [key, entry] of this.entries) {
|
|
56
|
+
if (entry.expiresAtMs <= now) {
|
|
57
|
+
this.entries.delete(key);
|
|
58
|
+
removed += 1;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return removed;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 半截消息累积器(1.0 的 `partialMessages`)
|
|
3
|
+
*
|
|
4
|
+
* 为什么要有它:任务跑到一半时平台刷新页面 / 重连,`get_task` 只能给出已落库的事件,
|
|
5
|
+
* 而助手的文本通常在回合结束才作为一条 `complete` 落库 —— 于是重连后屏幕上「什么都没有」,
|
|
6
|
+
* 用户以为任务卡住了。这份累积器让快照能带上「已经吐出来但还没定稿的那半段」。
|
|
7
|
+
*
|
|
8
|
+
* 三条设计取舍(都是刻意的):
|
|
9
|
+
* 1. **只在内存里**,不写 SQLite。流式 chunk 是 token 级的,落库会让 events 表被文本淹掉,
|
|
10
|
+
* retention 也会被 delta 挤爆;而它本来就**不是可恢复状态** —— 进程重启后平台重连拿尾部窗口即可。
|
|
11
|
+
* 2. **一个任务一个桶**(而非一条消息一个桶)。ACP 的 chunk 不带稳定 messageId,硬造 ID 会让平台
|
|
12
|
+
* 误以为是可拼接的多条消息;同一回合内的多段文本直接拼接,语义诚实:这是「本回合累积中的文本」。
|
|
13
|
+
* 3. **有上限**,超了就截断并置 `truncated`。累积器是旁路,不能因为某个模型话痨把内存吃掉。
|
|
14
|
+
*
|
|
15
|
+
* 生命周期:任务终态时由 supervisor 清空 —— 终态之后平台该看事件里的 `complete`,而不是半截文本。
|
|
16
|
+
*/
|
|
17
|
+
/** 单任务半截文本上限(32 KiB):够覆盖绝大多数回合,又不至于把内存吃穿 */
|
|
18
|
+
export const PARTIAL_TEXT_LIMIT = 32 * 1024;
|
|
19
|
+
export class PartialMessageTracker {
|
|
20
|
+
byTask = new Map();
|
|
21
|
+
/**
|
|
22
|
+
* 追加一段流式文本。
|
|
23
|
+
* @returns 追加后是否仍保留该桶(false = 已达上限被截断,后续块直接丢弃)
|
|
24
|
+
*/
|
|
25
|
+
append(taskId, chunk, opts = {}) {
|
|
26
|
+
if (!chunk)
|
|
27
|
+
return this.byTask.has(taskId);
|
|
28
|
+
const existing = this.byTask.get(taskId);
|
|
29
|
+
if (existing?.truncated)
|
|
30
|
+
return false;
|
|
31
|
+
const at = opts.at ?? new Date().toISOString();
|
|
32
|
+
if (!existing) {
|
|
33
|
+
const text = chunk.length > PARTIAL_TEXT_LIMIT ? chunk.slice(0, PARTIAL_TEXT_LIMIT) : chunk;
|
|
34
|
+
this.byTask.set(taskId, {
|
|
35
|
+
messageId: partialMessageId(taskId),
|
|
36
|
+
role: opts.role ?? 'agent',
|
|
37
|
+
text,
|
|
38
|
+
updatedAt: at,
|
|
39
|
+
...(text.length < chunk.length ? { truncated: true } : {}),
|
|
40
|
+
});
|
|
41
|
+
return text.length === chunk.length;
|
|
42
|
+
}
|
|
43
|
+
const room = PARTIAL_TEXT_LIMIT - existing.text.length;
|
|
44
|
+
if (room <= 0) {
|
|
45
|
+
this.byTask.set(taskId, { ...existing, truncated: true, updatedAt: at });
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const piece = chunk.length > room ? chunk.slice(0, room) : chunk;
|
|
49
|
+
this.byTask.set(taskId, {
|
|
50
|
+
...existing,
|
|
51
|
+
text: existing.text + piece,
|
|
52
|
+
updatedAt: at,
|
|
53
|
+
...(piece.length < chunk.length ? { truncated: true } : {}),
|
|
54
|
+
});
|
|
55
|
+
return piece.length === chunk.length;
|
|
56
|
+
}
|
|
57
|
+
list(taskId) {
|
|
58
|
+
const one = this.byTask.get(taskId);
|
|
59
|
+
return one ? [{ ...one }] : [];
|
|
60
|
+
}
|
|
61
|
+
/** 任务终态 / 会话关闭时调用:半截文本不该活过它所属的回合 */
|
|
62
|
+
clear(taskId) {
|
|
63
|
+
this.byTask.delete(taskId);
|
|
64
|
+
}
|
|
65
|
+
/** 当前持有的任务数(面板与测试用;不是 wire 字段) */
|
|
66
|
+
get size() {
|
|
67
|
+
return this.byTask.size;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 半截消息的稳定 ID。
|
|
72
|
+
*
|
|
73
|
+
* 刻意**不**模拟真实 messageId(`msg_<task>_final` 那种)—— 那是落库消息的幂等键,
|
|
74
|
+
* 半截文本随时可能被 complete 顶替,给它一个同构 ID 会诱导平台拿它去重。
|
|
75
|
+
*/
|
|
76
|
+
export function partialMessageId(taskId) {
|
|
77
|
+
return `pm_${taskId}`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 从 ACP `session/update` 的 update 里取助手文本增量。
|
|
81
|
+
* 只认 `agent_message_chunk` + `content.type === 'text'`;其余(tool_call / plan / thought)不是助手正文。
|
|
82
|
+
*/
|
|
83
|
+
export function acpChunkText(update) {
|
|
84
|
+
if (update['sessionUpdate'] !== 'agent_message_chunk')
|
|
85
|
+
return undefined;
|
|
86
|
+
const content = update['content'];
|
|
87
|
+
return content?.['type'] === 'text' && typeof content['text'] === 'string' ? content['text'] : undefined;
|
|
88
|
+
}
|
|
89
|
+
/** 从 codex app-server 通知里取助手文本增量(`item/agentMessage/delta`) */
|
|
90
|
+
export function codexDeltaText(notification) {
|
|
91
|
+
if (notification['method'] !== 'item/agentMessage/delta')
|
|
92
|
+
return undefined;
|
|
93
|
+
const params = (notification['params'] ?? {});
|
|
94
|
+
return typeof params['delta'] === 'string' ? params['delta'] : undefined;
|
|
95
|
+
}
|
|
96
|
+
/** 从 claude stream-json 的 content block 里取助手文本(整条消息,不是 token 增量) */
|
|
97
|
+
export function claudeBlockText(block) {
|
|
98
|
+
return block['type'] === 'text' && typeof block['text'] === 'string' ? block['text'] : undefined;
|
|
99
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 持久授权账本(1.0 approval ledger)—— 纯逻辑,不碰数据库。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独一层:账本的安全性全在「什么时候能自动批准」这一个判断上。
|
|
5
|
+
* 把它写成纯函数,才能用测试把规则钉死;一旦散进 supervisor 的 Promise 回调里,
|
|
6
|
+
* 任何一次重构都可能悄悄放宽条件,而放宽条件 = 无人值守地替用户点「永远允许」。
|
|
7
|
+
*
|
|
8
|
+
* 三条硬规则(来自 openspec specs/session-gateway「持久授权账本」):
|
|
9
|
+
* 1. 自动命中必须**同时**满足:请求本身真实提供了持久允许选项、fingerprint 匹配、permissionMode=default;
|
|
10
|
+
* 2. full 模式永不用账本自动批准(残余权限请求 fail closed);
|
|
11
|
+
* 3. tool title/kind 与模型文本不得创建授权或扩大 scope —— 故 fingerprint 只取
|
|
12
|
+
* 「工具 kind + 归一化资源」,**不取 title、不取模型输出的文字**。
|
|
13
|
+
*/
|
|
14
|
+
import { createHash } from 'node:crypto';
|
|
15
|
+
/** ACP 受控 tool kind;不在表里的一律降级成 other(不按 title 猜) */
|
|
16
|
+
const TOOL_KINDS = new Set(['read', 'edit', 'delete', 'move', 'search', 'execute', 'think', 'fetch', 'other']);
|
|
17
|
+
function asRecord(v) {
|
|
18
|
+
return typeof v === 'object' && v !== null ? v : undefined;
|
|
19
|
+
}
|
|
20
|
+
/** 统一分隔符与大小写,保证同一资源在不同轮次算出同一指纹 */
|
|
21
|
+
function normalizePath(p) {
|
|
22
|
+
return p.replace(/\\/g, '/').replace(/\/+$/, '') || '/';
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 从 ACP toolCall 提取「工具 + 资源」。
|
|
26
|
+
*
|
|
27
|
+
* 只认结构化字段:`kind`、`locations[].path`、`rawInput.file_path|path|command|url`。
|
|
28
|
+
* `title` 是模型/CLI 写的自由文本,**故意不读** —— 否则一句"顺便帮我清空工作区"就可能被
|
|
29
|
+
* 当成已授权的依据。
|
|
30
|
+
*/
|
|
31
|
+
export function permissionTarget(toolCall, cwd) {
|
|
32
|
+
const call = asRecord(toolCall);
|
|
33
|
+
const rawKind = typeof call?.['kind'] === 'string' ? call['kind'].toLowerCase() : '';
|
|
34
|
+
const tool = TOOL_KINDS.has(rawKind) ? rawKind : 'other';
|
|
35
|
+
// 1) locations[].path(ACP 标准字段,最可信)
|
|
36
|
+
const locations = Array.isArray(call?.['locations']) ? call?.['locations'] : [];
|
|
37
|
+
for (const loc of locations) {
|
|
38
|
+
const path = asRecord(loc)?.['path'];
|
|
39
|
+
if (typeof path === 'string' && path.trim()) {
|
|
40
|
+
const full = normalizePath(path.trim());
|
|
41
|
+
return { tool, resourceKey: full, display: baseName(full) };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// 2) rawInput 里的结构化字段
|
|
45
|
+
const input = asRecord(call?.['rawInput']);
|
|
46
|
+
if (input) {
|
|
47
|
+
for (const key of ['file_path', 'filePath', 'path', 'notebook_path']) {
|
|
48
|
+
const v = input[key];
|
|
49
|
+
if (typeof v === 'string' && v.trim()) {
|
|
50
|
+
const full = normalizePath(resolveAgainst(v.trim(), cwd));
|
|
51
|
+
return { tool, resourceKey: full, display: baseName(full) };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const command = input['command'];
|
|
55
|
+
if (typeof command === 'string' && command.trim()) {
|
|
56
|
+
// 只取命令首词:整条命令行每轮都不同,拿它做指纹等于永不命中
|
|
57
|
+
const first = command.trim().split(/\s+/)[0] ?? '';
|
|
58
|
+
return { tool, resourceKey: `cmd:${baseName(normalizePath(first))}`, display: 'command' };
|
|
59
|
+
}
|
|
60
|
+
const url = input['url'];
|
|
61
|
+
if (typeof url === 'string' && url.trim()) {
|
|
62
|
+
// URL 只留 host:查询串可能带 token,绝不进指纹或审计
|
|
63
|
+
try {
|
|
64
|
+
return { tool, resourceKey: `url:${new URL(url.trim()).host}`, display: 'url' };
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return { tool, resourceKey: 'url:-', display: 'url' };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { tool, resourceKey: '-', display: '-' };
|
|
72
|
+
}
|
|
73
|
+
function baseName(p) {
|
|
74
|
+
const parts = p.split('/').filter(Boolean);
|
|
75
|
+
return (parts[parts.length - 1] ?? '-').slice(0, 60);
|
|
76
|
+
}
|
|
77
|
+
function resolveAgainst(p, cwd) {
|
|
78
|
+
if (!cwd)
|
|
79
|
+
return p;
|
|
80
|
+
if (/^([a-zA-Z]:[\\/]|[\\/])/.test(p))
|
|
81
|
+
return p;
|
|
82
|
+
return `${normalizePath(cwd)}/${p}`;
|
|
83
|
+
}
|
|
84
|
+
/** 稳定指纹:sha256(agent | action | tool | resourceKey),截 32 位 hex */
|
|
85
|
+
export function approvalFingerprint(input) {
|
|
86
|
+
const canonical = [
|
|
87
|
+
input.agentType.toLowerCase(),
|
|
88
|
+
(input.action ?? 'permission').toLowerCase(),
|
|
89
|
+
input.tool.toLowerCase(),
|
|
90
|
+
input.resourceKey.toLowerCase(),
|
|
91
|
+
].join('\u0000');
|
|
92
|
+
return createHash('sha256').update(canonical).digest('hex').slice(0, 32);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 「持久允许」类选项的 ACP kind 取值。
|
|
96
|
+
*
|
|
97
|
+
* 不同 CLI 用词不一:ACP 规范是 `allow_always`,CodeBuddy/Grok 用 `allow_persistent`,
|
|
98
|
+
* 少数实现用 `allow_session`。只认这三个 —— 其余(allow_once / reject_*)绝不落账本。
|
|
99
|
+
*/
|
|
100
|
+
export const PERSISTENT_OPTION_KINDS = ['allow_always', 'allow_persistent', 'allow_session'];
|
|
101
|
+
/** 请求里真实存在的持久允许选项(找不到就是找不到,绝不构造 optionId) */
|
|
102
|
+
export function persistentAllowOption(options) {
|
|
103
|
+
if (!Array.isArray(options))
|
|
104
|
+
return undefined;
|
|
105
|
+
for (const option of options) {
|
|
106
|
+
const rec = asRecord(option);
|
|
107
|
+
const optionId = rec?.['optionId'];
|
|
108
|
+
const kind = rec?.['kind'];
|
|
109
|
+
if (typeof optionId !== 'string' || typeof kind !== 'string')
|
|
110
|
+
continue;
|
|
111
|
+
if (PERSISTENT_OPTION_KINDS.includes(kind)) {
|
|
112
|
+
return { optionId, kind };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 账本能否自动批准本次请求。
|
|
119
|
+
*
|
|
120
|
+
* ⚠️ 返回 true 的唯一含义是「可以复用用户此前的人工授权」;
|
|
121
|
+
* 任一条件不满足都返回 false 并给出原因,调用方据此决定是否去问人。
|
|
122
|
+
*/
|
|
123
|
+
export function canAutoApprove(record, input) {
|
|
124
|
+
if (input.permissionMode !== 'default') {
|
|
125
|
+
// full 已由受管参数模式整体放开,残余请求意味着该 CLI 还有自己的审批闸 —— 只能问人
|
|
126
|
+
return { ok: false, reason: 'full_mode_no_ledger' };
|
|
127
|
+
}
|
|
128
|
+
if (!input.hasPersistentOption)
|
|
129
|
+
return { ok: false, reason: 'no_persistent_option' };
|
|
130
|
+
if (!record)
|
|
131
|
+
return { ok: false, reason: 'no_match' };
|
|
132
|
+
if (record.revokedAt)
|
|
133
|
+
return { ok: false, reason: 'revoked' };
|
|
134
|
+
if (record.fingerprint !== input.fingerprint)
|
|
135
|
+
return { ok: false, reason: 'fingerprint_mismatch' };
|
|
136
|
+
const now = input.now ?? Date.now();
|
|
137
|
+
if (record.expiresAt && Date.parse(record.expiresAt) <= now) {
|
|
138
|
+
return { ok: false, reason: 'expired' };
|
|
139
|
+
}
|
|
140
|
+
return { ok: true };
|
|
141
|
+
}
|
|
142
|
+
/** 过期时间:未配置 TTL 时给 7 天(永不过期的授权不该存在) */
|
|
143
|
+
export const DEFAULT_APPROVAL_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
144
|
+
export function approvalExpiry(now, ttlMs = DEFAULT_APPROVAL_TTL_MS) {
|
|
145
|
+
return new Date(now + Math.max(60_000, ttlMs)).toISOString();
|
|
146
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 权限策略决策(1.0 Phase 2)—— 纯函数,不碰文件系统、不 spawn。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独抽出来:这是「平台请求的权限」与「本机允许的权限」交汇处,最容易写出
|
|
5
|
+
* 静默降级(请求 full 却偷偷按 default 跑)。放在 core 里做成纯函数,单测可以直接穷举矩阵。
|
|
6
|
+
*
|
|
7
|
+
* 口径(见 openspec/changes/add-task-execution-control/design.md §6.1):
|
|
8
|
+
* | permission_policy | 未指定 | 请求 default | 请求 full |
|
|
9
|
+
* | default | default| default | 拒绝 |
|
|
10
|
+
* | allow-full | default| default | 满足条件则 full |
|
|
11
|
+
* | full | full | default | 满足条件则 full |
|
|
12
|
+
*
|
|
13
|
+
* ⚠️ full 只是 bridge 选择用哪套 argv,**不是** OS 提权、配置隔离或环境沙箱的证明。
|
|
14
|
+
*/
|
|
15
|
+
/** 只有 CodeBuddy 的 full 需要外部沙箱声明(唯一固定 env 例外) */
|
|
16
|
+
const SANDBOX_REQUIRED_KINDS = new Set(['codebuddy-cli']);
|
|
17
|
+
/**
|
|
18
|
+
* 该工具在当前机器条件下**是否有资格**按 full 运行(不看本机策略)。
|
|
19
|
+
* 与 decidePermissionMode 的第 4/5 步共用同一判据 —— 上报的 executionModes 必须和实际准入一致,
|
|
20
|
+
* 否则平台会看到一个声明了 full 却必然被拒的工具。
|
|
21
|
+
*/
|
|
22
|
+
export function fullModeAdmitted(input) {
|
|
23
|
+
if (input.fullArgs === undefined)
|
|
24
|
+
return false;
|
|
25
|
+
if (SANDBOX_REQUIRED_KINDS.has(input.kind) && input.externalSandbox !== true)
|
|
26
|
+
return false;
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
export function decidePermissionMode(input) {
|
|
30
|
+
const policy = input.policy ?? 'default';
|
|
31
|
+
const requested = input.requested;
|
|
32
|
+
// 1) 本机策略不放行时直接拒绝 —— 不允许以 default 代替执行(不静默降级)
|
|
33
|
+
if (requested === 'full' && policy === 'default') {
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
code: 'permission_policy_denied',
|
|
37
|
+
message: `本机 permission_policy 为 default,不允许请求 full 权限`,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
// 2) 受管参数模式:allow-full / full 下 argv 必须完全由 bridge 决定
|
|
41
|
+
if (policy !== 'default' && input.args.length > 0) {
|
|
42
|
+
return {
|
|
43
|
+
ok: false,
|
|
44
|
+
code: 'permission_policy_denied',
|
|
45
|
+
message: `本机 permission_policy 为 ${policy},任务只能使用适配器默认参数(args 必须为空)`,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
// 3) 计算本次生效模式:未指定时 full 策略才默认 full
|
|
49
|
+
const mode = requested === 'full' ? 'full' : requested === 'default' ? 'default' : policy === 'full' ? 'full' : 'default';
|
|
50
|
+
if (mode === 'default')
|
|
51
|
+
return { ok: true, mode };
|
|
52
|
+
// 4) full 准入:工具必须有已验证的映射(CodeBuddy 还需外部沙箱声明)
|
|
53
|
+
if (!fullModeAdmitted({ kind: input.kind, fullArgs: input.fullArgs, externalSandbox: input.externalSandbox })) {
|
|
54
|
+
const needsSandbox = SANDBOX_REQUIRED_KINDS.has(input.kind) && input.externalSandbox !== true;
|
|
55
|
+
return {
|
|
56
|
+
ok: false,
|
|
57
|
+
code: 'permission_mode_unsupported',
|
|
58
|
+
message: needsSandbox
|
|
59
|
+
? `${input.kind} 的 full 模式需要本机声明 external_sandbox = true`
|
|
60
|
+
: `${input.kind} 没有已验证的 full 权限映射,无法按 full 执行`,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return { ok: true, mode };
|
|
64
|
+
}
|
|
65
|
+
/** full 模式下需要固定注入的环境变量(目前只有 CodeBuddy 一例) */
|
|
66
|
+
export function permissionEnvFor(kind, mode) {
|
|
67
|
+
if (mode !== 'full')
|
|
68
|
+
return undefined;
|
|
69
|
+
return kind === 'codebuddy-cli' ? { CODEBUDDY_IS_SANDBOX: '1' } : undefined;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* full 模式的残余权限请求(spec「权限与问题的组合语义」):
|
|
73
|
+
* full 下权限请求一律 fail closed,不转交人工、不借人工绕过 full 的静态 deny。
|
|
74
|
+
*
|
|
75
|
+
* 只有当前 options **真实提供** reject_once 时,才返回其原 optionId(自动拒绝这一次,
|
|
76
|
+
* CLI 继续回合);否则返回 cancel 语义 —— 调用方以 cancelled outcome 回复 adapter、
|
|
77
|
+
* 取消回合并以 interaction_denied 结束。绝不伪造 reject 选项或 optionId。
|
|
78
|
+
*/
|
|
79
|
+
export function fullResidualPermissionOutcome(options) {
|
|
80
|
+
const list = Array.isArray(options)
|
|
81
|
+
? options.filter((option) => typeof option === 'object' && option !== null)
|
|
82
|
+
: [];
|
|
83
|
+
const rejectOnce = list.find((option) => option['kind'] === 'reject_once');
|
|
84
|
+
if (rejectOnce && typeof rejectOnce['optionId'] === 'string' && rejectOnce['optionId']) {
|
|
85
|
+
return { optionId: rejectOnce['optionId'] };
|
|
86
|
+
}
|
|
87
|
+
return { cancel: true };
|
|
88
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 端口顺延:本机面板默认端口被占用时,从 base 起逐个尝试。
|
|
3
|
+
* 只对 EADDRINUSE 顺延;其它错误立即抛出,避免把权限/配置问题伪装成“端口占用”。
|
|
4
|
+
*/
|
|
5
|
+
export function isAddrInUse(error) {
|
|
6
|
+
return (typeof error === 'object' &&
|
|
7
|
+
error !== null &&
|
|
8
|
+
error.code === 'EADDRINUSE');
|
|
9
|
+
}
|
|
10
|
+
export async function withPortFallback(basePort, attempts, start) {
|
|
11
|
+
for (let offset = 0; offset < attempts; offset += 1) {
|
|
12
|
+
try {
|
|
13
|
+
return await start(basePort + offset);
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
if (isAddrInUse(error))
|
|
17
|
+
continue;
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
package/dist/core/redact.js
CHANGED
|
@@ -32,3 +32,16 @@ export function redact(text, opts = {}) {
|
|
|
32
32
|
}
|
|
33
33
|
return out;
|
|
34
34
|
}
|
|
35
|
+
/** 错误诊断专用:保留 URL origin/path 便于定位,剥离可能含短期 code/token 的 query 与 fragment。 */
|
|
36
|
+
export function redactDiagnostic(text, opts = {}) {
|
|
37
|
+
const withoutSecrets = text.replace(/https?:\/\/[^\s"'<>]+/g, (raw) => {
|
|
38
|
+
try {
|
|
39
|
+
const url = new URL(raw);
|
|
40
|
+
return `${url.origin}${url.pathname}${url.search || url.hash ? '?…' : ''}`;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return raw;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
return redact(withoutSecrets, opts);
|
|
47
|
+
}
|
package/dist/core/scan.js
CHANGED
|
@@ -2,18 +2,21 @@
|
|
|
2
2
|
* 扫描调度:并发探测 12 个工具 + 30s TTL 缓存 + 逐项回调(实时回传)
|
|
3
3
|
*
|
|
4
4
|
* 为什么要并发:单个工具一次 `--version` 子进程(Windows 上还要过 cmd 垫片),串行实测 12.8s(空载)~
|
|
5
|
-
* 25.7s(机器繁忙),期间事件循环被占住、WS
|
|
5
|
+
* 25.7s(机器繁忙),期间事件循环被占住、WS 心跳与入站指令全被推迟。改为并发探测(上限见
|
|
6
|
+
* CONCURRENCY)后落到秒级。
|
|
6
7
|
*
|
|
7
|
-
* 为什么要 onItem:平台侧是卡片式展示,一次性丢
|
|
8
|
+
* 为什么要 onItem:平台侧是卡片式展示,一次性丢 12 条会「一直空着 → 突然全亮」。逐项回调让平台
|
|
8
9
|
* 每完成一个就推一条 `scan_progress`(见 cli.ts),终态再发全量 `agent_list` / `scan_result`。
|
|
9
10
|
*/
|
|
10
|
-
import { AGENTS, probeAgent } from '../adapters/agent-adapters.js';
|
|
11
|
+
import { AGENTS, executionModesFor, probeAgent } from '../adapters/agent-adapters.js';
|
|
11
12
|
const TTL_MS = 30_000;
|
|
12
|
-
/** 并发上限:12 个子进程全开会在低配机器上抢 CPU,
|
|
13
|
+
/** 并发上限:12 个子进程全开会在低配机器上抢 CPU,6 个是实测的平衡点 */
|
|
13
14
|
const CONCURRENCY = 6;
|
|
14
15
|
let cache = null;
|
|
15
16
|
export async function scanAgents(opts = {}) {
|
|
16
|
-
|
|
17
|
+
const sandbox = opts.externalSandbox === true;
|
|
18
|
+
// 缓存必须按 sandbox 区分:否则先以 false 扫过,声明 external_sandbox 后仍报「无 full」
|
|
19
|
+
if (!opts.fresh && cache && cache.sandbox === sandbox && Date.now() - cache.at < TTL_MS) {
|
|
17
20
|
// 命中缓存:不再探测,但仍逐项回调,保证调用方拿到的进度序列是完整的
|
|
18
21
|
if (opts.onItem)
|
|
19
22
|
for (const item of cache.items)
|
|
@@ -31,7 +34,7 @@ export async function scanAgents(opts = {}) {
|
|
|
31
34
|
return;
|
|
32
35
|
let item;
|
|
33
36
|
try {
|
|
34
|
-
item = await probeAgent(def);
|
|
37
|
+
item = await probeAgent(def, sandbox);
|
|
35
38
|
}
|
|
36
39
|
catch {
|
|
37
40
|
// 一个工具探测炸了不能拖垮整次扫描
|
|
@@ -40,6 +43,8 @@ export async function scanAgents(opts = {}) {
|
|
|
40
43
|
display: def.display,
|
|
41
44
|
status: 'not_installed',
|
|
42
45
|
capability: def.capability,
|
|
46
|
+
steps: def.steps !== undefined,
|
|
47
|
+
executionModes: executionModesFor(def, sandbox),
|
|
43
48
|
};
|
|
44
49
|
}
|
|
45
50
|
items[index] = item;
|
|
@@ -47,7 +52,7 @@ export async function scanAgents(opts = {}) {
|
|
|
47
52
|
}
|
|
48
53
|
};
|
|
49
54
|
await Promise.all(Array.from({ length: Math.min(CONCURRENCY, AGENTS.length) }, worker));
|
|
50
|
-
cache = { at: Date.now(), items };
|
|
55
|
+
cache = { at: Date.now(), items, sandbox };
|
|
51
56
|
return items;
|
|
52
57
|
}
|
|
53
58
|
export function invalidateScanCache() {
|