@capyclaw/capyclaw-workbench 0.1.5 → 0.1.7
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 +23 -32
- package/lib/host.mjs +114 -64
- package/lib/running-tabs.mjs +154 -0
- package/lib/session-port.mjs +13 -17
- package/lib/tab-store.mjs +112 -0
- package/node_modules/@capyclaw/foundation/package.json +2 -2
- package/node_modules/@capyclaw/plugin-kit/package.json +1 -1
- package/node_modules/@capyclaw/service-host/dsh-suite.json +1 -1
- package/node_modules/@capyclaw/service-host/package.json +3 -3
- package/node_modules/@capyclaw/service-host/src/index.mjs +123 -11
- package/node_modules/@capyclaw/service-host/src/tuning.mjs +64 -2
- package/node_modules/@capyclaw/suite-kit/package.json +2 -2
- package/node_modules/@capyclaw/tool-host/dsh-suite.json +1 -1
- package/node_modules/@capyclaw/tool-host/package.json +3 -3
- package/package.json +9 -14
- package/plugin.mjs +4 -5
- package/contract/capabilities.json +0 -178
- package/lib/descriptors.mjs +0 -33
- package/lib/tools.mjs +0 -166
- package/lib/workbench.mjs +0 -133
- package/ui/app.js +0 -114
- package/ui/index.html +0 -64
- package/ui/style.css +0 -54
package/README.md
CHANGED
|
@@ -1,47 +1,37 @@
|
|
|
1
1
|
# @capyclaw/capyclaw-workbench
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
顶栏会话标签宿主(capyclaw 套件剥壳后的唯一运行时)。2026-11-25 用户授权删除了子会话控制面——7 个 `capyclaw_*` 工具、俄罗斯套娃控制台 UI、能力描述符/呈现面、`/capyclaw/capyclaw` 前缀挂载——只保留顶栏标签基础设施(根 `AGENTS.md` 三规则)。本包不再注册任何工具/描述符/portal 活动,选择墙不再出现「CapyClaw 子会话工作台」卡片。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 顶栏标签通道(宿主真源 → 客户端 keeper 只读投影)
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
| --- | --- |
|
|
9
|
-
| `capyclaw_open_session` | 创建子会话并发出第一条指令 |
|
|
10
|
-
| `capyclaw_send_message` | 向运行中/空闲子会话继续发消息 |
|
|
11
|
-
| `capyclaw_list_sessions` | 列出子会话及状态 |
|
|
12
|
-
| `capyclaw_session_transcript` | 读取子会话转录 |
|
|
13
|
-
| `capyclaw_close_session` | 关闭子会话 |
|
|
14
|
-
| `capyclaw_settings_get` | 读取 DSH 设置 |
|
|
15
|
-
| `capyclaw_settings_set` | 写入 DSH 设置 |
|
|
7
|
+
顶栏标签集合的**唯一真源**是 `lib/tab-store.mjs`(持久化在宿主 `work_meta` 的 `session-tabs:v1`)。客户端(keeper/tabs)只拿只读投影,写入全经三个动作端点(webServer exact 路由,与任何挂载前缀无关,`no-store`):
|
|
16
8
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
`
|
|
20
|
-
|
|
21
|
-
|
|
9
|
+
| 端点 | 方法 | 作用 |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| `/capyclaw/session-tabs/state` | GET | 只读投影 `{ids,rev,…}`,所有窗口轮询这一份 |
|
|
12
|
+
| `/capyclaw/session-tabs/admit` | POST | 纳入,来源白名单校验(非法 ⇒ 400) |
|
|
13
|
+
| `/capyclaw/session-tabs/close` | POST | 关闭(只人为) |
|
|
14
|
+
| `/capyclaw/session-tabs/move` | POST | 排序 |
|
|
22
15
|
|
|
23
|
-
|
|
16
|
+
来源白名单(`TAB_SOURCES`):`user-open`/`user-create`/`agent`(客户端可用)+ `running`(**宿主内部专用**)。`admit` 端点**拒绝客户端提交 `running`**。
|
|
24
17
|
|
|
25
|
-
|
|
18
|
+
**规则三(2026-11-25 用户新增):正在运行的会话一定要出现在顶栏。** `lib/running-tabs.mjs` 的 `createRunningReconcile` 每 5s 把 live、非子代理、真实 running(`getSnapshot().running`)的会话 `tabStore.admit(id,'running')`(幂等);冷会话/子代理排除。宿主-only,无客户端轮询通道。
|
|
26
19
|
|
|
27
|
-
|
|
20
|
+
**2026-11-24:`host-pending`/`session-watch`(曾称规则④)已移除**——它「凡 live 就纳入」且经客户端队列中转,违背当时规则(用户投诉「不经过我就冒出来」)。**2026-11-25 的 `running` 来源与之本质不同**:按真实 running 判定、宿主直写真源、无客户端通道、用户当前对话明确授权(见根 `AGENTS.md` 规则三),不得当作复发移除。工作区成员补登记(下节)不加标签,保留。
|
|
28
21
|
|
|
29
|
-
|
|
30
|
-
| --- | --- | --- |
|
|
31
|
-
| `/capyclaw/session-tabs/agent-pending` | `agent` | `session-port.createSession`(AI 派发创建的会话) |
|
|
22
|
+
## 工作区登记
|
|
32
23
|
|
|
33
|
-
|
|
24
|
+
DSH 的 `session.create` 只有传 `workspaceId` 才登记工作区成员;**只传 `cwd` 创建不登记**,会话落「未分组」桶。`lib/workspace-attach.mjs` 补登记:
|
|
34
25
|
|
|
35
|
-
|
|
26
|
+
- **周期补登记**:`createWorkspaceReconcile` 每 5s 把「运行中、非子代理、未被任何工作区认领」的会话按 `cwd` 补登记进匹配的已存在工作区。带 `handled` 集合:已登记/已认领的会话不再重纳(尊重用户手动移出);冷转 live、稍后新建工作区的会话会在后续拍被捕捉。
|
|
36
27
|
|
|
37
|
-
|
|
28
|
+
口径:**只登记进已存在的工作区,绝不擅自新建**;已被任何工作区认领的会话不搬动;子代理不参与。路径匹配用字符串规范化(分隔符/尾斜杠/Windows 不区分大小写),`attachSession` 自身再按 realpath 校验兜底。
|
|
38
29
|
|
|
39
|
-
|
|
40
|
-
- **周期补登记**:`createWorkspaceReconcile` 每 5s 把「运行中、非子代理、未被任何工作区认领」的会话按 `cwd` 补登记进匹配的已存在工作区(修复重启前已存在的远程会话,如 46d7)。带 `handled` 集合:已登记/已认领的会话不再重纳(尊重用户手动移出);冷转 live、稍后新建工作区的会话会在后续拍被捕捉。
|
|
30
|
+
**服务懒解析**:装配可能早于 `sessionController`/`workspaceRegistry` 注册,故端口与补登记运行器都接受零参解析函数、用的时候现取(不被一次性快照定格成缺席);服务晚注册也能用上,未就绪时静默重试。
|
|
41
31
|
|
|
42
|
-
|
|
32
|
+
## 承载形态
|
|
43
33
|
|
|
44
|
-
|
|
34
|
+
宿主进程内插件(foundation ABI raw@1,`inject:['webServer']`):`lib/host.mjs` 的 `apply()` 直接装配 tabStore/sessionPort/两个 reconciler 并在共享 webServer 上注册 4 个 exact 路由;不建 service app、不挂前缀、不注册工具/描述符。公共 `safeGet` 复用 `@capyclaw/suite-kit/host`。`lib/session-port.mjs` 保留会话端口(reconciler 经 `listSessions` 判活;`createSession` 成功即 `admit(id,'agent')` 的 AI 派发接线仍在,供原生子代理/未来接口使用)。
|
|
45
35
|
|
|
46
36
|
## 测试
|
|
47
37
|
|
|
@@ -49,7 +39,8 @@ DSH 的 `session.create` 只有传 `workspaceId` 才登记工作区成员;**
|
|
|
49
39
|
npm test
|
|
50
40
|
```
|
|
51
41
|
|
|
52
|
-
- `test/
|
|
53
|
-
- `test/session-port.test.mjs`:会话端口 + AI 标签队列(入队/去重/上限/drain)+ 创建时工作区登记。
|
|
42
|
+
- `test/session-port.test.mjs`:会话端口 + AI 派发成功 ⇒ 直接 `admit(id,'agent')` 写真源(无队列/drain 中转)+ 创建时工作区登记。
|
|
54
43
|
- `test/workspace-attach.test.mjs`:工作区登记(路径匹配 / 幂等 / 只登记已存在 / 补登记跳过规则 / 周期运行器 + handled / 懒解析)。
|
|
55
|
-
- `test/
|
|
44
|
+
- `test/tab-store.test.mjs`:标签真源(白名单四来源含宿主-only `running` / admit 幂等 / close / move / rev)。
|
|
45
|
+
- `test/running-tabs.test.mjs`:规则三 reconciler(只纳真实 running+live+非子代理 / 冷会话与子代理排除 / 幂等 / 懒解析 / 周期运行器)。
|
|
46
|
+
- `test/host.test.mjs`:宿主装配(state/admit/close/move 端点 / 拒客户端提交 `running` / 无子会话前缀 / 工作区补登记与 running reconciler 接线 / 持久化 / 释放)。
|
package/lib/host.mjs
CHANGED
|
@@ -1,81 +1,131 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* capyclaw-workbench cordis
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* capyclaw-workbench cordis 宿主入口(剥壳后:仅顶栏标签真源宿主)。
|
|
3
|
+
*
|
|
4
|
+
* 2026-11-25 用户授权(当前对话明确):删除 capyclaw 子会话套件的控制面——7 个 capyclaw_*
|
|
5
|
+
* 工具、俄罗斯套娃 UI、能力描述符/呈现面——只保留顶栏标签基础设施:tab-store 真源 +
|
|
6
|
+
* state/admit/close/move 端点 + workspace 补登记 + running reconciler(根 AGENTS.md 三规则)。
|
|
7
|
+
*
|
|
8
|
+
* 不再挂 /capyclaw/capyclaw 前缀、不建 service app、不注册工具/描述符/portal 活动,
|
|
9
|
+
* 故选择墙(portal-hud surfaceRows 三源并集探活)不再出现「CapyClaw 子会话工作台」卡片。
|
|
10
|
+
* 标签端点是 webServer 上的 exact 路由,与任何挂载前缀无关;keeper/tabs 客户端只依赖它们。
|
|
11
|
+
* 公共 safeGet 仍复用 @capyclaw/suite-kit/host(不修改基础设施)。
|
|
6
12
|
*/
|
|
7
|
-
import {
|
|
13
|
+
import { safeGet } from '@capyclaw/suite-kit/host'
|
|
8
14
|
import { createSessionPort } from './session-port.mjs'
|
|
15
|
+
import { createTabStore } from './tab-store.mjs'
|
|
9
16
|
import { createWorkspaceReconcile } from './workspace-attach.mjs'
|
|
10
|
-
import {
|
|
11
|
-
import { CAPYCLAW_DESCRIPTORS } from './descriptors.mjs'
|
|
17
|
+
import { createRunningReconcile } from './running-tabs.mjs'
|
|
12
18
|
|
|
13
19
|
export const name = 'capyclaw-workbench'
|
|
14
20
|
export const inject = ['webServer']
|
|
15
21
|
|
|
16
22
|
export function apply(ctx, config = {}) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
const log = {
|
|
24
|
+
info: (...a) => (ctx.logger?.info ? ctx.logger.info(...a) : console.log('[capyclaw]', ...a)),
|
|
25
|
+
warn: (...a) => (ctx.logger?.warn ? ctx.logger.warn(...a) : console.warn('[capyclaw]', ...a)),
|
|
26
|
+
}
|
|
27
|
+
const disposers = []
|
|
28
|
+
const push = (d) => { if (typeof d === 'function') disposers.push(d) }
|
|
29
|
+
let released = false
|
|
30
|
+
const releaseAll = () => {
|
|
31
|
+
if (released) return
|
|
32
|
+
released = true
|
|
33
|
+
for (const d of disposers.splice(0).reverse()) { try { d() } catch { /* 忽略重复释放 */ } }
|
|
34
|
+
}
|
|
23
35
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
// 服务懒解析:装配可能早于 sessionController/workspaceRegistry 注册,一次性 safeGet
|
|
37
|
+
// 会把缺席定格成永久 false(2026-11-23 重启后补登记/创建静默失效的教训)。传零参解析
|
|
38
|
+
// 函数,端口在每次调用时现取,服务晚注册也能用上。
|
|
39
|
+
// 顶栏标签唯一真源(规则一:只有一份、只有宿主能写)。窗口只读投影 + admit/close/move
|
|
40
|
+
// 三个动作,来源白名单在 tab-store 内校验。
|
|
41
|
+
const tabStore = createTabStore({
|
|
42
|
+
getMeta: (...a) => { const ws = safeGet(ctx, 'workStore'); return ws && typeof ws.getMeta === 'function' ? ws.getMeta(...a) : null },
|
|
43
|
+
setMeta: (...a) => { const ws = safeGet(ctx, 'workStore'); return ws && typeof ws.setMeta === 'function' ? ws.setMeta(...a) : null },
|
|
44
|
+
})
|
|
45
|
+
const sessionPort = createSessionPort({
|
|
46
|
+
// 规则③:AI 派发直接写真源,无「队列 + 客户端轮询」中转。
|
|
47
|
+
admitTab: () => (id, source) => tabStore.admit(id, source),
|
|
48
|
+
sessionController: () => safeGet(ctx, 'sessionController'),
|
|
49
|
+
sessionQuery: () => safeGet(ctx, 'sessionQuery'),
|
|
50
|
+
sessions: () => safeGet(ctx, 'sessions'),
|
|
51
|
+
settings: () => safeGet(ctx, 'settings'),
|
|
52
|
+
workspaceRegistry: () => safeGet(ctx, 'workspaceRegistry'),
|
|
53
|
+
})
|
|
39
54
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
push(() => workspaceReconcile.stop())
|
|
51
|
-
return { sessionPort }
|
|
52
|
-
},
|
|
55
|
+
// 工作区登记(周期补登记):远程「只传 cwd」创建的会话 DSH 不登记工作区成员,落「未分组」。
|
|
56
|
+
// 把运行中、非子代理、未被任何工作区认领的会话按 cwd 补登记进匹配的已存在工作区(不新建)。
|
|
57
|
+
// 带 handled 集合:已登记/已认领的不重纳(尊重用户手动移出);冷转 live 的会话变 live 后被捕捉。
|
|
58
|
+
// 只整理侧栏归属,不加顶栏标签(根 AGENTS.md)。workspaceRegistry 懒解析,晚注册也不丢。
|
|
59
|
+
const workspaceReconcile = createWorkspaceReconcile({
|
|
60
|
+
listSessions: () => sessionPort.listSessions(),
|
|
61
|
+
workspaceRegistry: () => safeGet(ctx, 'workspaceRegistry'),
|
|
62
|
+
})
|
|
63
|
+
workspaceReconcile.start()
|
|
64
|
+
push(() => workspaceReconcile.stop())
|
|
53
65
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
// 运行中会话 ⇒ 顶栏(2026-11-25 用户规则三):宿主-only reconciler,按 live Session 的真实
|
|
67
|
+
// running 判定,admit(id,'running')。子代理/冷会话排除;admit 幂等,停止后不再纳入(标签只人为关)。
|
|
68
|
+
// 2026-11-25 扩展(用户授权):父会话本身空闲但有运行中子代理(上卷)也按 running 纳入;
|
|
69
|
+
// reconciler 另产只读 activity 投影({id:{running,subagents}})随 state 端点附加下发。
|
|
70
|
+
// 与已移除的 session-watch 不同:按真实 running 判定、无客户端轮询通道、HTTP admit 拒客户端 running。
|
|
71
|
+
const runningReconcile = createRunningReconcile({
|
|
72
|
+
listSessions: () => sessionPort.listSessions(),
|
|
73
|
+
sessions: () => safeGet(ctx, 'sessions'),
|
|
74
|
+
admit: (id) => tabStore.admit(id, 'running'),
|
|
75
|
+
})
|
|
76
|
+
runningReconcile.start()
|
|
77
|
+
push(() => runningReconcile.stop())
|
|
58
78
|
|
|
59
|
-
|
|
79
|
+
const webServer = safeGet(ctx, 'webServer')
|
|
80
|
+
if (!webServer || typeof webServer.register !== 'function') {
|
|
81
|
+
log.warn('capyclaw-workbench: 缺少 webServer 服务,顶栏标签端点未注册(reconciler 仍写真源)')
|
|
82
|
+
return releaseAll
|
|
83
|
+
}
|
|
60
84
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
})
|
|
78
|
-
|
|
85
|
+
const json = (res, code, body) => {
|
|
86
|
+
res.writeHead(code, { 'content-type': 'application/json', 'cache-control': 'no-store' })
|
|
87
|
+
res.end(JSON.stringify(body))
|
|
88
|
+
}
|
|
89
|
+
const readBody = (req) => new Promise((resolve) => {
|
|
90
|
+
let text = ''
|
|
91
|
+
req.setEncoding('utf8')
|
|
92
|
+
req.on('data', (c) => { text += c; if (text.length > 65536) { req.destroy(); resolve({}) } })
|
|
93
|
+
req.on('end', () => { try { resolve(JSON.parse(text || '{}')) } catch { resolve({}) } })
|
|
94
|
+
req.on('error', () => resolve({}))
|
|
95
|
+
})
|
|
96
|
+
// 只读投影:所有窗口轮询这一份,客户端不再自存标签集合。
|
|
97
|
+
// activity 为附加只读元数据(运行中子代理上卷),不是第二份标签真源;ids/rev 仍唯一真源。
|
|
98
|
+
const offState = webServer.register({
|
|
99
|
+
kind: 'exact', path: '/capyclaw/session-tabs/state',
|
|
100
|
+
handler: (req, res) => {
|
|
101
|
+
if (req.method !== 'GET') return json(res, 405, { error: 'GET only' })
|
|
102
|
+
json(res, 200, { ...tabStore.list(), activity: runningReconcile.getActivity() })
|
|
79
103
|
},
|
|
80
104
|
})
|
|
105
|
+
if (typeof offState === 'function') push(offState)
|
|
106
|
+
// 三个写入动作(唯一入口)。admit 的来源白名单在 tab-store 内校验;
|
|
107
|
+
// running 是宿主内部专用来源(running-tabs reconciler),客户端不可提交。
|
|
108
|
+
for (const action of ['admit', 'close', 'move']) {
|
|
109
|
+
const off = webServer.register({
|
|
110
|
+
kind: 'exact', path: `/capyclaw/session-tabs/${action}`,
|
|
111
|
+
handler: async (req, res) => {
|
|
112
|
+
if (req.method !== 'POST') return json(res, 405, { error: 'POST only' })
|
|
113
|
+
const body = await readBody(req)
|
|
114
|
+
const out = action === 'admit'
|
|
115
|
+
? (body.source === 'running'
|
|
116
|
+
? { ok: false, error: 'running 来源仅宿主内部使用,客户端不可提交' }
|
|
117
|
+
: tabStore.admit(body.id, body.source))
|
|
118
|
+
: action === 'close'
|
|
119
|
+
? tabStore.close(body.id)
|
|
120
|
+
: tabStore.move(body.id, body.toIndex)
|
|
121
|
+
json(res, out.ok ? 200 : 400, out)
|
|
122
|
+
},
|
|
123
|
+
})
|
|
124
|
+
if (typeof off === 'function') push(off)
|
|
125
|
+
}
|
|
126
|
+
// 注:AI 派发(规则③)不再需要 agent-pending drain 端点——createSession 成功后
|
|
127
|
+
// 宿主直接 tabStore.admit(id,'agent'),窗口下一次拉投影即可看到。
|
|
128
|
+
|
|
129
|
+
log.info('capyclaw-workbench 已挂载顶栏标签真源端点 /capyclaw/session-tabs/{state,admit,close,move}')
|
|
130
|
+
return releaseAll
|
|
81
131
|
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 运行中会话 ⇒ 顶栏标签(2026-11-25 用户新增规则:「正在运行的会话一定要出现在
|
|
3
|
+
* 顶栏标签里面」)。宿主侧周期 reconciler,是 running 来源的唯一写入者。
|
|
4
|
+
*
|
|
5
|
+
* 与历史「第四来源 host / session-watch」事故的区别(见根 AGENTS.md 变更管制):
|
|
6
|
+
* - 宿主-only:直接写 tabStore.admit(id,'running'),没有「客户端轮询队列」中转,
|
|
7
|
+
* 客户端经 HTTP 提交 running 会被 host.mjs 拒绝;
|
|
8
|
+
* - 按【真实 running】判定(live Session 的 getSnapshot().running),不是「凡 live
|
|
9
|
+
* 就纳入」;冷会话(record.live===false)不可能 running,直接跳过;
|
|
10
|
+
* - 子代理(origin==='subagent')嵌套展示,不占顶栏,排除;
|
|
11
|
+
* - 用户当前对话明确授权的新规则,不是 AI 擅自放宽白名单。
|
|
12
|
+
*
|
|
13
|
+
* 2026-11-25 用户再授权(规则三扩展):父会话【本身空闲但有运行中子代理】也按
|
|
14
|
+
* 运行中管理——把运行中子代理沿 parentSession 上卷到最近非子代理祖先,祖先自身
|
|
15
|
+
* running 或上卷子代理数>0 ⇒ admit(祖先,'running')。子代理本身仍不占顶栏。
|
|
16
|
+
* 同时产出只读 activity 投影 {id:{running,subagents:[childIds]}}(仅活跃条目),
|
|
17
|
+
* 经 host.mjs state 端点附加下发,供客户端画「子代理数量徽标 / 关闭确认」,
|
|
18
|
+
* 不构成第二份标签真源(标签集合仍只有 tabStore 一份)。
|
|
19
|
+
*
|
|
20
|
+
* 幂等:admit 已在集合内返回 changed:false(不 bump rev),故无需 handled 集合——
|
|
21
|
+
* 每拍确保所有 running 非子代理会话都在顶栏即可。关闭运行中标签的 UI 流程会先
|
|
22
|
+
* cancel 停止会话再 close,停止后不再 running ⇒ 不会被本 reconciler 重新纳入,
|
|
23
|
+
* 与「标签只人为关」一致。
|
|
24
|
+
*
|
|
25
|
+
* 设计口径:纯函数、服务全部注入(可 fake)、不 import DSH 包。sessions 可传对象
|
|
26
|
+
* 或零参解析函数(懒解析,装配早于服务注册也不丢——2026-11-23 教训)。
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** reconciler 周期。 */
|
|
30
|
+
export const RUNNING_RECONCILE_INTERVAL_MS = 5000
|
|
31
|
+
|
|
32
|
+
/** 上卷 parentSession 链的最大跳数(防环/防异常深链)。 */
|
|
33
|
+
const MAX_PARENT_HOPS = 32
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 把「运行中、非子代理」的会话纳入顶栏(admit 由调用方绑定到 running 来源);
|
|
37
|
+
* 父会话本身空闲但有运行中子代理(上卷)同样纳入。
|
|
38
|
+
* 返回 { admitted, ready, activity };依赖缺失/列表抛错 ⇒ ready=false(下一拍重试)。
|
|
39
|
+
* activity 仅含活跃条目(自身 running 或 subagents 非空),供只读投影。
|
|
40
|
+
* @param {{listSessions:Function, sessions?:object|Function, admit:Function}} deps
|
|
41
|
+
* admit(id) 应返回 tabStore.admit 的结果({ok,changed});只统计 changed 的纳入。
|
|
42
|
+
*/
|
|
43
|
+
export async function reconcileRunningTabs({ listSessions, sessions, admit } = {}) {
|
|
44
|
+
if (typeof listSessions !== 'function' || typeof admit !== 'function') {
|
|
45
|
+
return { admitted: [], ready: false, activity: {} }
|
|
46
|
+
}
|
|
47
|
+
const sess = typeof sessions === 'function' ? sessions() : sessions
|
|
48
|
+
let records
|
|
49
|
+
try {
|
|
50
|
+
records = await listSessions()
|
|
51
|
+
} catch {
|
|
52
|
+
return { admitted: [], ready: false, activity: {} }
|
|
53
|
+
}
|
|
54
|
+
const items = Array.isArray(records) ? records : []
|
|
55
|
+
const byId = new Map()
|
|
56
|
+
for (const record of items) {
|
|
57
|
+
const header = record?.header ?? record
|
|
58
|
+
const id = header?.id ?? header?.sessionId
|
|
59
|
+
if (typeof id === 'string' && id) byId.set(id, { header, live: record?.live })
|
|
60
|
+
}
|
|
61
|
+
const snapshotOf = (id) => {
|
|
62
|
+
const live = sess && typeof sess.get === 'function' ? sess.get(id) : null
|
|
63
|
+
return live && typeof live.getSnapshot === 'function' ? live.getSnapshot() : null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 第一遍:运行中子代理沿 parentSession 上卷到最近非子代理祖先。
|
|
67
|
+
const childrenRunning = new Map()
|
|
68
|
+
for (const [id, entry] of byId) {
|
|
69
|
+
if (entry.live === false) continue // 冷子代理不可能 running
|
|
70
|
+
if (entry.header?.origin !== 'subagent') continue
|
|
71
|
+
const snap = snapshotOf(id)
|
|
72
|
+
if (!snap || !snap.running) continue // 只统计真实 running 的子代理
|
|
73
|
+
let cur = entry.header?.parentSession
|
|
74
|
+
const seen = new Set([id])
|
|
75
|
+
let hops = 0
|
|
76
|
+
while (typeof cur === 'string' && cur && !seen.has(cur) && hops < MAX_PARENT_HOPS) {
|
|
77
|
+
seen.add(cur)
|
|
78
|
+
hops += 1
|
|
79
|
+
const anc = byId.get(cur)
|
|
80
|
+
if (!anc) break
|
|
81
|
+
if (anc.header?.origin !== 'subagent') {
|
|
82
|
+
if (!childrenRunning.has(cur)) childrenRunning.set(cur, [])
|
|
83
|
+
childrenRunning.get(cur).push(id)
|
|
84
|
+
break
|
|
85
|
+
}
|
|
86
|
+
cur = anc.header?.parentSession
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 第二遍:非子代理 live 会话,自身 running 或上卷子代理>0 ⇒ 纳入 + 记 activity。
|
|
91
|
+
const admitted = []
|
|
92
|
+
const activity = {}
|
|
93
|
+
for (const record of items) {
|
|
94
|
+
const header = record?.header ?? record
|
|
95
|
+
const id = header?.id ?? header?.sessionId
|
|
96
|
+
if (typeof id !== 'string' || !id) continue
|
|
97
|
+
if (record?.live === false) continue // 冷会话不可能 running
|
|
98
|
+
if (header?.origin === 'subagent') continue // 子代理不占顶栏
|
|
99
|
+
const snap = snapshotOf(id)
|
|
100
|
+
const ownRunning = !!(snap && snap.running)
|
|
101
|
+
const subagents = childrenRunning.get(id) || []
|
|
102
|
+
if (!ownRunning && subagents.length === 0) continue
|
|
103
|
+
activity[id] = { running: ownRunning, subagents }
|
|
104
|
+
const out = admit(id)
|
|
105
|
+
if (out && out.ok && out.changed) admitted.push(id)
|
|
106
|
+
}
|
|
107
|
+
return { admitted, ready: true, activity }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 周期运行器:每 intervalMs 跑一次 reconcileRunningTabs,持续到 stop()。
|
|
112
|
+
* 服务未就绪(ready=false)或抛错时静默重试。busy 防重入;timer.unref 不挡进程退出。
|
|
113
|
+
* 额外暴露 getActivity():最近一次成功拍的 activity 投影(只读元数据,非标签真源)。
|
|
114
|
+
*/
|
|
115
|
+
export function createRunningReconcile({ listSessions, sessions, admit, intervalMs = RUNNING_RECONCILE_INTERVAL_MS } = {}) {
|
|
116
|
+
if (typeof listSessions !== 'function') throw new Error('createRunningReconcile 需要 listSessions 函数')
|
|
117
|
+
if (typeof admit !== 'function') throw new Error('createRunningReconcile 需要 admit 函数')
|
|
118
|
+
let timer = null
|
|
119
|
+
let stopped = false
|
|
120
|
+
let busy = false
|
|
121
|
+
let activity = {}
|
|
122
|
+
|
|
123
|
+
async function tick() {
|
|
124
|
+
if (stopped || busy) return null
|
|
125
|
+
busy = true
|
|
126
|
+
try {
|
|
127
|
+
const out = await reconcileRunningTabs({ listSessions, sessions, admit })
|
|
128
|
+
if (out && out.ready) activity = out.activity || {}
|
|
129
|
+
return out
|
|
130
|
+
} catch {
|
|
131
|
+
return null
|
|
132
|
+
} finally {
|
|
133
|
+
busy = false
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function start() {
|
|
138
|
+
if (timer || stopped) return
|
|
139
|
+
void tick()
|
|
140
|
+
timer = setInterval(tick, intervalMs)
|
|
141
|
+
timer.unref?.()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function stop() {
|
|
145
|
+
stopped = true
|
|
146
|
+
if (timer) { clearInterval(timer); timer = null }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function getActivity() {
|
|
150
|
+
return activity
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return { start, stop, tick, getActivity }
|
|
154
|
+
}
|
package/lib/session-port.mjs
CHANGED
|
@@ -11,10 +11,13 @@
|
|
|
11
11
|
* - 纯函数端口,服务全部注入(可在测试里用 fake 替换),不 import 任何 DSH 包。
|
|
12
12
|
* - 优雅降级:宿主没有对应服务时返回明确的 unavailable 错误,而不是静默假成功。
|
|
13
13
|
* - GUI 无会话深链(recon 结论):这里只负责「创建/驱动/查询」会话这一控制面;
|
|
14
|
-
* 「呈现哪个会话」是 UI 层的事。经 createSession
|
|
15
|
-
*
|
|
14
|
+
* 「呈现哪个会话」是 UI 层的事。经 createSession 新建的会话由宿主直接
|
|
15
|
+
* tabStore.admit(id,'agent') 纳入顶栏(真源在宿主,无客户端中转)。
|
|
16
16
|
* (2026-11-24:曾短暂加入的 session-watch/host-pending「规则④」已移除——它把每个
|
|
17
17
|
* 运行中的后台/定时/远程会话都自动加成标签,违背「只经用户/AI 特定接口加标签」的规则。)
|
|
18
|
+
* (2026-11-25 用户新规则:正在运行的会话必进顶栏,由宿主 running-tabs.mjs reconciler
|
|
19
|
+
* 按真实 getSnapshot().running 判定直写 tabStore.admit(id,'running'),宿主-only、无客户端
|
|
20
|
+
* 通道——与上面被移除的「凡 live 就纳入」的 session-watch 本质不同,见根 AGENTS.md 规则三。)
|
|
18
21
|
* - 工作区登记:DSH 只给传 workspaceId 的创建登记工作区成员,只传 cwd 不登记
|
|
19
22
|
* (会话落「未分组」)。createSession 成功后按 cwd 补登记进匹配的已存在工作区
|
|
20
23
|
* (workspace-attach,best-effort,不新建工作区)。
|
|
@@ -44,12 +47,10 @@ export function createSessionPort(services = {}) {
|
|
|
44
47
|
const settingsSvc = () => { const s = resolve('settings'); return s && typeof s.describe === 'function' ? s : null }
|
|
45
48
|
const registry = () => { const w = resolve('workspaceRegistry'); return w && typeof w.list === 'function' ? w : null }
|
|
46
49
|
|
|
47
|
-
/* 规则③(AI
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const AGENT_TAB_QUEUE_CAP = 200
|
|
52
|
-
const pendingAgentTabs = []
|
|
50
|
+
/* 规则③(AI 派发标签):createSession 成功后直接调用真源的 admit(id,'agent')。
|
|
51
|
+
真源在宿主(tab-store),所以不再需要「待取队列 + 客户端轮询 drain」这一层中转——
|
|
52
|
+
那层中转是客户端持有集合时代的产物,2026-11-25 收敛真源后删除。 */
|
|
53
|
+
const admitTab = () => { const f = resolve('admitTab'); return typeof f === 'function' ? f : null }
|
|
53
54
|
|
|
54
55
|
return {
|
|
55
56
|
get available() { return !!controller() },
|
|
@@ -72,11 +73,9 @@ export function createSessionPort(services = {}) {
|
|
|
72
73
|
if (agentPreset) request.agentPreset = agentPreset
|
|
73
74
|
const value = await sessionController.create(request)
|
|
74
75
|
const sessionId = value.sessionId
|
|
75
|
-
if (sessionId
|
|
76
|
-
|
|
77
|
-
if (
|
|
78
|
-
pendingAgentTabs.splice(0, pendingAgentTabs.length - AGENT_TAB_QUEUE_CAP)
|
|
79
|
-
}
|
|
76
|
+
if (sessionId) {
|
|
77
|
+
const admit = admitTab()
|
|
78
|
+
if (admit) { try { admit(sessionId, 'agent') } catch { /* 真源不可用:创建结果不受影响 */ } }
|
|
80
79
|
}
|
|
81
80
|
// 工作区登记:只传 cwd 的创建 DSH 不登记成员 ⇒ 会话落「未分组」。这里按 cwd
|
|
82
81
|
// 补登记进匹配的已存在工作区(best-effort:失败不影响创建结果;传 workspaceId
|
|
@@ -88,10 +87,7 @@ export function createSessionPort(services = {}) {
|
|
|
88
87
|
return { sessionId, agentPreset: value.agentPreset ?? agentPreset ?? null }
|
|
89
88
|
},
|
|
90
89
|
|
|
91
|
-
|
|
92
|
-
drainAgentTabs() {
|
|
93
|
-
return { ids: pendingAgentTabs.splice(0, pendingAgentTabs.length) }
|
|
94
|
-
},
|
|
90
|
+
|
|
95
91
|
|
|
96
92
|
/** 向会话发一条用户消息,启动一轮(mode: queue|steer)。 */
|
|
97
93
|
async prompt({ sessionId, text, mode = 'queue' } = {}) {
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 顶栏标签的唯一真源(宿主侧,单一写入者)。
|
|
3
|
+
*
|
|
4
|
+
* 规则先行(2026-11-25 重构,用户要求「用最简的规则,而不是不断加补丁」):
|
|
5
|
+
* 规则一:标签集合只有一份,只有宿主进程能写。窗口全是只读投影。
|
|
6
|
+
* 规则二:只有 admit/close/move 三个动作能改它,admit 的来源必须属于白名单
|
|
7
|
+
* {user-open, user-create, agent, running},其余一律拒绝。
|
|
8
|
+
* 其中 running 是【宿主内部专用】来源(2026-11-25 用户新增规则:正在运行
|
|
9
|
+
* 的会话一定要出现在顶栏):只由宿主 running-tabs reconciler 写入,HTTP
|
|
10
|
+
* admit 端点拒绝客户端提交 running(见 host.mjs),客户端永远只用前三个。
|
|
11
|
+
*
|
|
12
|
+
* 这两条是结构约束,不是检查点:客户端没有集合、没有持久化、没有合并逻辑,
|
|
13
|
+
* 因此「多窗口各存一份互相覆盖」「心跳自动纳入」这些历史事故在结构上写不出来。
|
|
14
|
+
* running 来源与历史「第四来源 host」事故不同:它宿主-only、按真实 running 判定、
|
|
15
|
+
* 无客户端轮询通道,且是用户当前对话明确授权的新规则(见根 AGENTS.md)。
|
|
16
|
+
*
|
|
17
|
+
* 存储:work-store 的 work_meta 单键(进程内唯一写入者,天然原子)。
|
|
18
|
+
* 无 work-store 时降级为进程内内存(仍单一写入者,只是重启不保留)。
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** 加入来源白名单(用户规则)。running 为宿主内部专用(running-tabs reconciler),
|
|
22
|
+
* 客户端经 HTTP 提交 running 会被 host.mjs 拒绝;前三个是客户端可用的人为/派发来源。 */
|
|
23
|
+
export const TAB_SOURCES = ['user-open', 'user-create', 'agent', 'running']
|
|
24
|
+
|
|
25
|
+
const META_KEY = 'session-tabs:v1'
|
|
26
|
+
const MAX_TABS = 500
|
|
27
|
+
|
|
28
|
+
function readIds(getMeta) {
|
|
29
|
+
if (typeof getMeta !== 'function') return null
|
|
30
|
+
const rec = getMeta(META_KEY, null)
|
|
31
|
+
/* 无持久化面/无记录 ⇒ null(表示「别覆盖内存」);有记录但 ids 坏 ⇒ []。 */
|
|
32
|
+
if (rec === null || rec === undefined) return null
|
|
33
|
+
const ids = Array.isArray(rec.ids) ? rec.ids : []
|
|
34
|
+
const out = []
|
|
35
|
+
for (const id of ids) {
|
|
36
|
+
const s = String(id || '')
|
|
37
|
+
if (s && out.indexOf(s) < 0) out.push(s)
|
|
38
|
+
}
|
|
39
|
+
return out
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param {{getMeta?:Function, setMeta?:Function}} store work-store 的 KV 面(可缺席)
|
|
44
|
+
*/
|
|
45
|
+
export function createTabStore(store = {}) {
|
|
46
|
+
const getMeta = typeof store.getMeta === 'function' ? store.getMeta : null
|
|
47
|
+
const setMeta = typeof store.setMeta === 'function' ? store.setMeta : null
|
|
48
|
+
let memory = readIds(getMeta) || []
|
|
49
|
+
let rev = 0
|
|
50
|
+
|
|
51
|
+
const load = () => {
|
|
52
|
+
const persisted = readIds(getMeta)
|
|
53
|
+
if (persisted) memory = persisted
|
|
54
|
+
return memory
|
|
55
|
+
}
|
|
56
|
+
const save = (ids) => {
|
|
57
|
+
memory = ids
|
|
58
|
+
rev += 1
|
|
59
|
+
if (setMeta) setMeta(META_KEY, { v: 1, ids })
|
|
60
|
+
return ids
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
/** 当前标签序(只读投影的唯一来源)。 */
|
|
65
|
+
list() {
|
|
66
|
+
return { ids: load().slice(), rev }
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
/** 纳入:来源必须合法。已在集合内 ⇒ 幂等成功(不改序)。 */
|
|
70
|
+
admit(id, source) {
|
|
71
|
+
const sid = String(id || '')
|
|
72
|
+
const src = String(source || '')
|
|
73
|
+
if (!sid) return { ok: false, error: '需要 sessionId' }
|
|
74
|
+
if (TAB_SOURCES.indexOf(src) < 0) {
|
|
75
|
+
return { ok: false, error: `非法来源:${src || '(缺失)'};只允许 ${TAB_SOURCES.join('/')}` }
|
|
76
|
+
}
|
|
77
|
+
const ids = load()
|
|
78
|
+
if (ids.indexOf(sid) >= 0) return { ok: true, ids: ids.slice(), changed: false }
|
|
79
|
+
const next = ids.concat([sid])
|
|
80
|
+
if (next.length > MAX_TABS) next.splice(0, next.length - MAX_TABS)
|
|
81
|
+
save(next)
|
|
82
|
+
return { ok: true, ids: next.slice(), changed: true }
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
/** 人为关闭:唯一移除路径。 */
|
|
86
|
+
close(id) {
|
|
87
|
+
const sid = String(id || '')
|
|
88
|
+
const ids = load()
|
|
89
|
+
const at = ids.indexOf(sid)
|
|
90
|
+
if (at < 0) return { ok: true, ids: ids.slice(), changed: false }
|
|
91
|
+
const next = ids.slice()
|
|
92
|
+
next.splice(at, 1)
|
|
93
|
+
save(next)
|
|
94
|
+
return { ok: true, ids: next.slice(), changed: true }
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
/** 拖拽排序:序真源在此。 */
|
|
98
|
+
move(id, toIndex) {
|
|
99
|
+
const sid = String(id || '')
|
|
100
|
+
const ids = load()
|
|
101
|
+
const from = ids.indexOf(sid)
|
|
102
|
+
if (from < 0) return { ok: false, error: '未受管的会话' }
|
|
103
|
+
const to = Math.max(0, Math.min(ids.length - 1, Number(toIndex) || 0))
|
|
104
|
+
if (to === from) return { ok: true, ids: ids.slice(), changed: false }
|
|
105
|
+
const next = ids.slice()
|
|
106
|
+
next.splice(from, 1)
|
|
107
|
+
next.splice(to, 0, sid)
|
|
108
|
+
save(next)
|
|
109
|
+
return { ok: true, ids: next.slice(), changed: true }
|
|
110
|
+
},
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capyclaw/foundation",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.15",
|
|
4
4
|
"description": "dsh 插件基石层:接口 ABI、插件加载器与内核服务 ctx.os。应用插件只声明接口,不写装配代码。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/host.mjs",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
},
|
|
44
44
|
"license": "MIT",
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@capyclaw/plugin-kit": "^0.2.
|
|
46
|
+
"@capyclaw/plugin-kit": "^0.2.12"
|
|
47
47
|
},
|
|
48
48
|
"repository": {
|
|
49
49
|
"type": "git",
|