dsh-team-control 0.0.0-stage → 0.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/README.md CHANGED
@@ -1,3 +1,128 @@
1
- # Temporary Holding Version
1
+ # @local/dsh-team-plugin
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 把本仓的 **dsh-team sidecar** 暴露为 DSH 模型可调用工具的 Host 插件(bundle)。
4
+
5
+ - 编排内核仍在 sidecar 进程里([`../src/`](../src/)),本插件只做**适配 + 生命周期**,不重写调度/权限/事件流。
6
+ - 零第三方依赖:只用 Node 内置模块;工具以普通 `ToolDefinition` 对象注册(JSON Schema 参数),不依赖 `defineTool`。
7
+ - 11 个工具:`team_create` `team_spawn` `team_models` `team_status` `team_task` `team_send` `team_report_task` `team_dispatch` `team_scope` `team_control` `team_report`。
8
+
9
+ ## 调用顺序写在哪(别让模型自己摸)
10
+
11
+ - 11 个工具里**只有 `team_create` 标了 `START HERE`**:它的 description 直接给出 5 步顺序、`runtime` 的语义与后果
12
+ (不传 `runtime:"agent"` 就是 worker,成员不会思考)、以及 `teamId` 取自 `result.team.id`;`team_spawn` 写明返回的
13
+ `member.id` 就是后面 `assignee` 要的值;`team_task` 写明简报必须自包含(成员看不到 Lead 的对话)。模型只看工具面就能按序走。
14
+ - 完整配方(字段陷阱、拆任务与并发、症状→修法表)在 skill **`dsh-agent-team`**:[`../skills/dsh-agent-team/SKILL.md`](../skills/dsh-agent-team/SKILL.md),
15
+ 安装到 `~/.dsh/skills/dsh-agent-team/SKILL.md` 后出现在会话技能目录;`team_create` 描述末尾也指向它。
16
+
17
+ ## 两种成员运行时
18
+
19
+ `team_create` 的 `runtime` 决定「成员」是什么:
20
+
21
+ | `runtime` | 成员实体 | 谁执行任务 | sidecar 的角色 |
22
+ |---|---|---|---|
23
+ | `worker`(默认) | sidecar 拉起的沙箱 Node 进程 | `plan` 的每个 step 是一个经 broker 代理的动作 | 派发 + 四域审批 + 事件真相源 |
24
+ | `agent` | **真 DSH sub-agent session**(`subagents.startActivation`) | 成员用自己的模型与工具干活 | 只做任务/DAG/审批/看板(不启动任何进程) |
25
+
26
+ > 依赖宿主的 `subagents` 服务(`dsh-subagent` bundle,几乎总在)。插件用 `ctx.get('subagents')` 探测而不是
27
+ > `inject` 硬依赖:没有它时只有 `runtime: "agent"` 相关调用会报「subagents 服务不可用」,worker 团队照常。
28
+
29
+ `agent` 运行时的闭环(全部在插件内,无轮询工具调用):
30
+
31
+ 1. `team_spawn` → `subagents.startActivation({delivery:'parent', childId, provider, label, request:{prompt, parent, cwd: team.workspace, agentOptions?}})` → 把子会话 id 作为 `sessionId` `POST /api/agents/register`,成员出现在看板上并置 `idle`。`agentOptions` 只在显式指定 provider/model/reasoningEffort 时出现(见下节)。
32
+ 2. 建任务/依赖满足 → 插件调度器(`agentDispatchMs`,默认 1.5s 一跳)调 `POST /api/agents/dispatch` **原子认领**一条 ready 任务 → `subagents.sendMessage(lead, sessionId, [{type:'text', text: 任务简报}])` 把任务发进该成员的信箱。
33
+ 3. 成员干完 → 自己调 `team_report_task`(精确摘要/成败,`/api/agents/report`),或由 `agent/status` 事件在回合结束时兜底收尾(`/api/agents/status`)。
34
+ 4. 成员报 `blocked` 时任务退回 `blocked`(**不消耗重试**),阻塞原因进 `blockers` 与 `/api/blockers`。
35
+ 5. 成员的 session 工具调用由 `tools/execute` 上报为 span;sidecar 用 `member.agent_session_id` 把这些 span 挂到成员节点下,瀑布里就是 `成员 → 工具` 两层。
36
+
37
+ `team_control`:`agent` 团队的 `interrupt` 调 `subagents.interrupt` 中止当前回合并同步落库 `interrupted`;`resume` 重新武装并按需再派发。
38
+
39
+ ## 模型路由(每个成员不同的 provider / model / effort)
40
+
41
+ 目录的**唯一来源**是 Host 进程的 `ctx.llm`:`listProviders()`(`{id,name}`)+ `listModels(provider)`
42
+ (`{provider,id,name,description?,inputModalities?}`)。只上报**能列出模型**的 provider(空列表或未配置的直接跳过),
43
+ 所以目录和面板里不会出现空分组。插件把这份映射快照 `POST /api/llm/catalog` 给 sidecar(30s 节流,
44
+ `llm/adapters-updated` 事件会重置节流);sidecar 只存 provider/model 映射、启用开关和备注,**不代理 LLM 请求、
45
+ 不保存凭据**,所以面板独立打开(`:4317/panel`)也能用。
46
+
47
+ - 面板「模型路由」页:按 provider 分组,逐条启用/停用、写备注;停用项不会出现在 `team_models` 里。
48
+ - `team_models`:只列**启用**的路由(`provider`/`model`/`name`/`description`/`note`/`modalities`),并带一条用法提示。
49
+ - `team_spawn`:`agentProvider` + `model` 指定 LLM route(与 subagent 后端的 `provider` 参数无关);
50
+ `reasoningEffort` 是 adapter 定义的不透明 id。实际生效的 route(显式指定或继承 Lead)必须在启用集合里,
51
+ 否则报「模型路由未启用 / 所有模型路由都已停用」;目录里查不到的 route 不拦。
52
+ - 实际生效值写进 `request.agentOptions = {provider?, model?, reasoningEffort?}`(只放有值的字段),
53
+ 返回值 `agent.route` 与 sidecar 成员记录的 `model` 用的也是真实 model id。
54
+ - 继承规则与 DSH 一致:route 变了又没显式给 effort → 丢弃继承的 effort(返回值里是 `null`);
55
+ route 不变 → 沿用 Lead 的 effort。`model` 在 worker 团队只是展示标签。
56
+
57
+ ## 安装
58
+
59
+ 在 DSH 里用 `plugin_manager` 安装本目录(绝对路径),它会写 profile 的 `dependencies`(`link:`)与 `dsh.profile.bundles`:
60
+
61
+ ```text
62
+ plugin_manager(action: "install_bundle", target: "C:\\...\\agent-team-plugin\\dsh-plugin")
63
+ ```
64
+
65
+ 期望结果:`application: "applied"`、`warnings: []`,随后工具面出现 11 个 `team_*` 工具。
66
+ 安装后可用 `cordis_inspect_query` 的 `Tool.listTools` / `Config.listConfigs` 复核(无需审批)。
67
+ 卸载:`plugin_manager(action: "remove_bundle", target: "@local/dsh-team-plugin")`。
68
+
69
+ ## 配置(`cordis.patch.yml`)
70
+
71
+ ```yaml
72
+ - insert:
73
+ - id: dsh-team
74
+ name: '@local/dsh-team-plugin'
75
+ config:
76
+ baseUrl: http://127.0.0.1:4317
77
+ autoStart: true
78
+ reportToolCalls: true
79
+ ```
80
+
81
+ | 字段 | 默认 | 说明 |
82
+ |---|---|---|
83
+ | `baseUrl` | `http://127.0.0.1:4317` | sidecar 地址;端口从这个 URL 推导 |
84
+ | `autoStart` | `true` | sidecar 不可达时,首次工具调用自动拉起 `dsh-team up` |
85
+ | `home` | `~/.dsh/team` | 数据目录(`team.db` / `token`);必须与 sidecar 一致 |
86
+ | `sidecarEntry` | `../bin/dsh-team.mjs` | sidecar 入口;默认按包位置推导,无需配置 |
87
+ | `startTimeoutMs` | `20000` | 自动拉起的等待上限 |
88
+ | `requestTimeoutMs` | `60000` | 单次 API 调用超时 |
89
+ | `reportToolCalls` | `true` | 监听 Host 的 `tools/execute`,把每次工具调用上报为 `POST /api/spans` 的 span(先 `running` 后 `done/failed`),面板 Trace 瀑布实时展示 |
90
+ | `traceInputMax` | `1200` | 上报参数/结果预览的截断长度(字符);只截断,不脱敏其他字段 |
91
+ | `agentAutoDispatch` | `true` | `agent` 团队:调度器自动把 ready 任务派给 idle 成员 |
92
+ | `agentDispatchMs` | `1500` | 调度器跳间隔;每次只认领少量任务,避免同时唤醒全部成员 |
93
+ | `agentDispatchMax` | `16` | 每跳最多认领的任务数 |
94
+ | `agentSettleMs` | `250` | 建任务后的「声明依赖」窗口(sidecar 侧是 200ms) |
95
+ | `freshProvider` | `spawn` | `subagents.startActivation` 的 provider(`context: fresh`) |
96
+ | `forkProvider` | `fork` | `subagents.startActivation` 的 provider(`context: fork`) |
97
+
98
+ > 上报采用「会话 → 团队」绑定:任意一次带 `teamId` 的 `team_*` 调用、或 `team_create` 成功后,该会话后续的
99
+ > 工具调用都会挂到对应团队;未绑定团队的工具调用直接丢弃(不阻塞、不等待,1.2s 超时静默失败)。
100
+
101
+ > 本插件**不导出 `Config` schema**,因此 `Config.listConfigs` 对它的 `status` 是 `absent` ——
102
+ > 这是「无 schema」而不是「配置未生效」;`dsh --profile <p> --dump-config` 可看到 row 的
103
+ > `config: {baseUrl, autoStart}` 已合成。代价是拼错的字段不会被校验,代码侧已做默认值兜底。
104
+
105
+ ## 生命周期
106
+
107
+ - 插件**只回收自己拉起的** sidecar 子进程(`ctx.effect` 的 disposer 里 kill);用户手工起的服务不受影响。
108
+ - sidecar 是 DSH Host 的子进程:Host 退出后它可能存活,下次工具调用会用健康检查复用;
109
+ 要彻底停:`node bin/dsh-team.mjs down`,或把 `autoStart` 设为 `false`。
110
+ - 工具不受 DSH 的沙箱审批约束(它们是纯 HTTP 客户端);真正的权限边界仍由 sidecar 的四域
111
+ PermBroker 执行,且 Worker 进程没有原生 IO 能力(见 [ADR-002](../docs/adr/ADR-002-worker-confinement.md))。
112
+
113
+ ## 面板位置
114
+
115
+ 本包只提供**工具面**。界面在配套的 [`@local/dsh-team-ui`](../dsh-plugin-ui/):一个注册在
116
+ `settings.plugins.tab` 的 React 页面(设置 → 插件 → dsh-team 控制台),经 Host 同源代理读取
117
+ sidecar。独立的 `{baseUrl}/panel` 仍然保留,适合不打开 DSH 时使用。
118
+
119
+ 不把独立面板塞进 iframe 的原因:DSH 插件规范要求 UI 用 React 组件渲染在 Slot 里,
120
+ iframe 文档拿不到主题 token / 明暗切换 / `ctx.locale`。
121
+
122
+ ## 自测
123
+
124
+ ```bash
125
+ node dsh-plugin/test-adapter.mjs # 临时 sidecar 上跑完建队→加成员→阻塞→审批→报告→卸载
126
+ ```
127
+
128
+ 该脚本不依赖演示实例,覆盖注册契约、真实 API 调用、错误文案与卸载清理。
package/changes.js ADDED
@@ -0,0 +1,168 @@
1
+ // dsh-team-control — 成员改动采集:把成员的 write/edit 落盘差异算成 unified diff。
2
+ //
3
+ // 只做三件事,不碰 sidecar 之外的东西:
4
+ // 1. 读改前/改后文本(读不到/超过大小上限就放弃,宁缺毋滥);
5
+ // 2. 算 unified diff(带 3 行上下文、`@@ -a,b +c,d @@` 头、行号可直接被面板渲染);
6
+ // 3. 统计新增/删除行数。
7
+ // 零第三方依赖:只用 Node 内置模块。
8
+ import fs from 'node:fs';
9
+ import path from 'node:path';
10
+
11
+ /** 超过这个大小不读(二进制约文件、超大日志没必要进审阅页)。 */
12
+ const MAX_FILE_BYTES = 2 * 1024 * 1024;
13
+ /** 上下文行数:与 git diff 默认一致。 */
14
+ const CONTEXT = 3;
15
+ /** 中间段 LCS 的 DP 上限(n*m 超过就退化成整段替换,避免大文件卡死 host)。 */
16
+ const LCS_CELL_LIMIT = 4_000_000;
17
+
18
+ /** 读文本;不存在 / 目录 / 太大 / 读失败都返回 null。 */
19
+ export function readTextSafe(file) {
20
+ try {
21
+ const st = fs.statSync(file);
22
+ if (!st.isFile() || st.size > MAX_FILE_BYTES) return null;
23
+ return fs.readFileSync(file, 'utf8');
24
+ } catch {
25
+ return null;
26
+ }
27
+ }
28
+
29
+ /** 归一化换行后按行切;空文本是 0 行。 */
30
+ export function splitLines(text) {
31
+ if (typeof text !== 'string' || text === '') return [];
32
+ return text.replace(/\r\n/g, '\n').replace(/\r/g, '\n').split('\n');
33
+ }
34
+
35
+ /**
36
+ * 行级 LCS 编辑脚本:返回 [{ type: ' ' | '-' | '+', text }]。
37
+ * 中间段过大时退化为「全删 + 全加」,diff 仍然正确,只是不精细。
38
+ */
39
+ export function lineOps(before, after) {
40
+ const a = splitLines(before);
41
+ const b = splitLines(after);
42
+ // 共同前缀/后缀先剥掉,DP 只跑中间段(改动通常集中在一小段)
43
+ let head = 0;
44
+ while (head < a.length && head < b.length && a[head] === b[head]) head += 1;
45
+ let tail = 0;
46
+ while (tail < a.length - head && tail < b.length - head && a[a.length - 1 - tail] === b[b.length - 1 - tail]) tail += 1;
47
+ const midA = a.slice(head, a.length - tail);
48
+ const midB = b.slice(head, b.length - tail);
49
+ const ops = [];
50
+ for (let i = 0; i < head; i += 1) ops.push({ type: ' ', text: a[i] });
51
+ const n = midA.length;
52
+ const m = midB.length;
53
+ if (n * m > LCS_CELL_LIMIT) {
54
+ for (const line of midA) ops.push({ type: '-', text: line });
55
+ for (const line of midB) ops.push({ type: '+', text: line });
56
+ } else if (n || m) {
57
+ // dp[i][j] = midA[i..] 与 midB[j..] 的最长公共子序列长度
58
+ const width = m + 1;
59
+ const dp = new Int32Array((n + 1) * width);
60
+ for (let i = n - 1; i >= 0; i -= 1) {
61
+ for (let j = m - 1; j >= 0; j -= 1) {
62
+ dp[i * width + j] = midA[i] === midB[j]
63
+ ? dp[(i + 1) * width + j + 1] + 1
64
+ : Math.max(dp[(i + 1) * width + j], dp[i * width + j + 1]);
65
+ }
66
+ }
67
+ let i = 0;
68
+ let j = 0;
69
+ while (i < n && j < m) {
70
+ if (midA[i] === midB[j]) { ops.push({ type: ' ', text: midA[i] }); i += 1; j += 1; }
71
+ else if (dp[(i + 1) * width + j] >= dp[i * width + j + 1]) { ops.push({ type: '-', text: midA[i] }); i += 1; }
72
+ else { ops.push({ type: '+', text: midB[j] }); j += 1; }
73
+ }
74
+ while (i < n) { ops.push({ type: '-', text: midA[i] }); i += 1; }
75
+ while (j < m) { ops.push({ type: '+', text: midB[j] }); j += 1; }
76
+ }
77
+ for (let i = a.length - tail; i < a.length; i += 1) ops.push({ type: ' ', text: a[i] });
78
+ return ops;
79
+ }
80
+
81
+ /**
82
+ * 算 unified diff。`file` 只用于 `--- a/<file>` / `+++ b/<file>` 头(相对路径更好读)。
83
+ * 没有差异时返回空串。
84
+ */
85
+ export function unifiedDiff(file, before, after) {
86
+ const a = splitLines(before);
87
+ const b = splitLines(after);
88
+ const ops = lineOps(before, after);
89
+ const hasChange = ops.some((op) => op.type !== ' ');
90
+ if (!hasChange) return '';
91
+ const label = String(file || 'file').replace(/\\/g, '/');
92
+ const out = [`--- a/${label}`, `+++ b/${label}`];
93
+ // 每个 op 在旧/新文件里的起始行号(1 基)
94
+ let oldNo = 1;
95
+ let newNo = 1;
96
+ const at = ops.map((op) => {
97
+ const here = { old: oldNo, new: newNo };
98
+ if (op.type === ' ') { oldNo += 1; newNo += 1; }
99
+ else if (op.type === '-') oldNo += 1;
100
+ else newNo += 1;
101
+ return here;
102
+ });
103
+ // 改动块:连续改动之间的上下文空档不超过 2*CONTEXT 就并进同一段
104
+ const changed = [];
105
+ for (let i = 0; i < ops.length; i += 1) if (ops[i].type !== ' ') changed.push(i);
106
+ let cursor = 0;
107
+ while (cursor < changed.length) {
108
+ const first = changed[cursor];
109
+ let last = first;
110
+ while (cursor + 1 < changed.length && changed[cursor + 1] - last <= CONTEXT * 2 + 1) {
111
+ cursor += 1;
112
+ last = changed[cursor];
113
+ }
114
+ cursor += 1;
115
+ const start = Math.max(0, first - CONTEXT);
116
+ const end = Math.min(ops.length, last + CONTEXT + 1);
117
+ const body = ops.slice(start, end);
118
+ const oldCount = body.filter((op) => op.type !== '+').length;
119
+ const newCount = body.filter((op) => op.type !== '-').length;
120
+ // git 惯例:侧边计数为 0 时,起始行写「插入点前一行」
121
+ const oldStart = oldCount === 0 ? at[start].old - 1 : at[start].old;
122
+ const newStart = newCount === 0 ? at[start].new - 1 : at[start].new;
123
+ out.push(`@@ -${oldStart},${oldCount} +${newStart},${newCount} @@`);
124
+ for (const op of body) out.push(op.type + op.text);
125
+ }
126
+ return out.join('\n') + '\n';
127
+ }
128
+
129
+ /** 统计 diff 里的新增/删除行数(只看 `+`/`-` 前缀行,跳过 `---`/`+++` 文件头)。 */
130
+ export function countDiff(diff) {
131
+ let added = 0;
132
+ let deleted = 0;
133
+ for (const line of String(diff || '').split('\n')) {
134
+ if (line.startsWith('+++') || line.startsWith('---')) continue;
135
+ if (line.startsWith('+')) added += 1;
136
+ else if (line.startsWith('-')) deleted += 1;
137
+ }
138
+ return { added, deleted };
139
+ }
140
+
141
+ /** 成员的写改工具名(DSH 内置工具)。 */
142
+ export const FILE_TOOLS = new Set(['write', 'edit', 'apply_patch', 'str_replace_editor', 'multi_edit']);
143
+
144
+ /** 从工具参数里取目标文件(不同工具的参数名不同)。 */
145
+ export function targetFile(name, args) {
146
+ if (!args || typeof args !== 'object') return '';
147
+ const raw = args.file_path ?? args.path ?? args.filePath ?? args.filename;
148
+ return typeof raw === 'string' ? raw : '';
149
+ }
150
+
151
+ /** 相对路径:在 baseDir 下时保留相对路径(面板更好读),否则用绝对路径。
152
+ * 统一成正斜杠:Windows 上 path.relative 给反斜杠,面板按 / 过滤与展示,diff 头也用 /。 */
153
+ export function displayPath(abs, baseDir) {
154
+ const posix = (p) => String(p).replace(/\\/g, '/');
155
+ if (!baseDir) return posix(abs);
156
+ try {
157
+ const rel = path.relative(baseDir, abs);
158
+ return posix(rel && !rel.startsWith('..') && !path.isAbsolute(rel) ? rel : abs);
159
+ } catch {
160
+ return posix(abs);
161
+ }
162
+ }
163
+
164
+ /** 工具名 → 变更类型(新文件 = add;其余 = edit)。 */
165
+ export function changeKind(before, after) {
166
+ if (before === null && after !== null) return 'add';
167
+ return 'edit';
168
+ }
@@ -0,0 +1,8 @@
1
+ # dsh-team:把本地 sidecar 暴露为模型可调用的编排工具
2
+ # baseUrl/home 必须与实际运行的 sidecar 一致;autoStart=true 时首次调用会自动拉起 `dsh-team up`。
3
+ - insert:
4
+ - id: dsh-team
5
+ name: 'dsh-team-control'
6
+ config:
7
+ baseUrl: http://127.0.0.1:4317
8
+ autoStart: true
package/icon.svg ADDED
@@ -0,0 +1,17 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="dsh-team">
2
+ <rect x="2" y="2" width="60" height="60" rx="14" fill="#1b2430"/>
3
+ <g stroke="#5ad1c8" stroke-width="3" stroke-linecap="round" fill="none">
4
+ <path d="M32 15v10"/>
5
+ <path d="M32 25 17 34"/>
6
+ <path d="M32 25l15 9"/>
7
+ <path d="M17 42h30"/>
8
+ <path d="M17 34v8"/>
9
+ <path d="M47 34v8"/>
10
+ </g>
11
+ <g fill="#5ad1c8">
12
+ <circle cx="32" cy="13" r="5"/>
13
+ <circle cx="17" cy="34" r="4.5"/>
14
+ <circle cx="47" cy="34" r="4.5"/>
15
+ </g>
16
+ <circle cx="32" cy="50" r="5" fill="#f0b429"/>
17
+ </svg>