dsh-seal-scripts 0.5.2 → 0.6.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/README.en.md +220 -167
- package/README.md +214 -186
- package/lib/client.js +322 -17
- package/lib/core/index.js +11 -1
- package/lib/host/capabilities.js +4 -0
- package/lib/host/compact-delete.js +188 -24
- package/lib/host/delete.js +32 -4
- package/lib/host/import-session.js +498 -0
- package/lib/host/routes/sessions.js +47 -1
- package/package.json +4 -3
|
@@ -3,12 +3,47 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 任何一步失败都不删,所以最坏情况是「压缩成功、拆失败、会话还在」——
|
|
5
5
|
* 代价是 compactNow 已经改写了历史,这一步不可逆。
|
|
6
|
+
*
|
|
7
|
+
* 0.5.3 本地修复(三处,都是 0.2.2 web profile 的部署形状逼出来的):
|
|
8
|
+
* 1. 压缩后端按预设隔离:profile 根上下文没有 compaction,「delete」以前必抛。
|
|
9
|
+
* 现在先问 agent 自己的上下文(预设插件是挂进 agent 作用域的)。
|
|
10
|
+
* 2. 会话不在内存里时要 resume;裸 resume 不会挂预设插件,也不带模型,
|
|
11
|
+
* 压缩会失败。优先走宿主自己的组合入口 sessionController.agents.resume(),
|
|
12
|
+
* 它会把预设(含压缩)挂进 agent 作用域并装上模型选择。
|
|
13
|
+
* 3. 每一步失败都留一行到 session-memory/_compact-delete.log,
|
|
14
|
+
* 否则设置页只有一句英文,谁也看不出卡在哪。
|
|
6
15
|
*/
|
|
7
16
|
|
|
17
|
+
import { appendFile, mkdir, readFile, stat, writeFile } from 'node:fs/promises'
|
|
18
|
+
import { join } from 'node:path'
|
|
8
19
|
import { listMemories } from '../core/tool.js'
|
|
20
|
+
import { captureCompaction } from '../core/store.js'
|
|
21
|
+
import { readLocaleSync } from '../core/locale.js'
|
|
9
22
|
import { deleteSessionPermanently } from './delete.js'
|
|
10
23
|
|
|
11
24
|
const TIMEOUT_MS = 120_000
|
|
25
|
+
const LOG_NAME = '_compact-delete.log'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 找出会话里最新的一条压缩事件(0.5.3 本地修复)。
|
|
29
|
+
*
|
|
30
|
+
* 事件监听挂在 profile 根上,压缩却在预设的隔离作用域里发生——那边发出来的
|
|
31
|
+
* session/event 未必传到根插件,于是「等拆解」会一直等到超时、什么都拆不出来。
|
|
32
|
+
* 与其猜监听收不收得到,不如压缩完自己回会话里把那条事件捞出来,直接拆。
|
|
33
|
+
* @param {object|undefined} session
|
|
34
|
+
* @returns {object|undefined}
|
|
35
|
+
*/
|
|
36
|
+
function latestCompactionEvent(session) {
|
|
37
|
+
try {
|
|
38
|
+
const end = typeof session?.seq === 'number' ? session.seq : undefined
|
|
39
|
+
if (end === undefined || typeof session.eventAt !== 'function') return undefined
|
|
40
|
+
for (let seq = end - 1; seq >= 0 && seq > end - 2000; seq--) {
|
|
41
|
+
const event = session.eventAt(seq)
|
|
42
|
+
if (event?.type === 'compaction/summary') return event
|
|
43
|
+
}
|
|
44
|
+
} catch { /* 读不到就不拆,交给后面「没有新回顾」的报错 */ }
|
|
45
|
+
return undefined
|
|
46
|
+
}
|
|
12
47
|
|
|
13
48
|
function withTimeout(promise, ms, text) {
|
|
14
49
|
return new Promise((yes, no) => {
|
|
@@ -17,8 +52,63 @@ function withTimeout(promise, ms, text) {
|
|
|
17
52
|
})
|
|
18
53
|
}
|
|
19
54
|
|
|
55
|
+
const message = (e) => (e instanceof Error ? e.message : String(e))
|
|
56
|
+
|
|
57
|
+
/** 一行诊断,失败也不遮住真正的报错。 */
|
|
58
|
+
async function note(dshHome, sessionId, text) {
|
|
59
|
+
try {
|
|
60
|
+
const dir = join(dshHome, 'session-memory')
|
|
61
|
+
await mkdir(dir, { recursive: true })
|
|
62
|
+
const file = join(dir, LOG_NAME)
|
|
63
|
+
// 日志不能无限长:超过 256 KB 就只留最后 64 KB(按整行切)。
|
|
64
|
+
try {
|
|
65
|
+
const info = await stat(file)
|
|
66
|
+
if (info.size > 256 * 1024) {
|
|
67
|
+
const kept = (await readFile(file, 'utf8')).slice(-64 * 1024)
|
|
68
|
+
const cut = kept.indexOf('\n')
|
|
69
|
+
await writeFile(file, cut >= 0 ? kept.slice(cut + 1) : kept)
|
|
70
|
+
}
|
|
71
|
+
} catch { /* 文件还没有就算了 */ }
|
|
72
|
+
await appendFile(file, new Date().toISOString() + ' ' + sessionId + ' ' + text + '\n')
|
|
73
|
+
} catch { /* 诊断写不出来就算了 */ }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 关掉我们自己拉起来的 agent;宿主没钩子就用拿到的句柄。 */
|
|
77
|
+
async function release(agent, handle) {
|
|
78
|
+
// 1) 插件自己抓到的句柄
|
|
79
|
+
try { if (handle !== undefined) return await handle.dispose() } catch { /* 落到自造 */ }
|
|
80
|
+
// 2) 没有句柄(例如经 sessionController 恢复的 agent):自己造一把,和删除路径同一套
|
|
81
|
+
try {
|
|
82
|
+
if (typeof agent?.cancel === 'function' && typeof agent.whenIdle === 'function'
|
|
83
|
+
&& typeof agent.scope?.dispose === 'function') {
|
|
84
|
+
agent.cancel({ kind: 'disposed' }, { keepInbox: true })
|
|
85
|
+
await agent.whenIdle()
|
|
86
|
+
await agent.scope.dispose()
|
|
87
|
+
}
|
|
88
|
+
} catch { /* 关不掉也不遮住真正的报错 */ }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 找一个能用的压缩后端。宿主把它挂在预设作用域里,根上下文看不见。
|
|
93
|
+
* @param {object} deps
|
|
94
|
+
* @param {object|undefined} agent
|
|
95
|
+
* @returns {{source:string, service:object}|null}
|
|
96
|
+
*/
|
|
97
|
+
function findCompaction(deps, agent) {
|
|
98
|
+
const seen = []
|
|
99
|
+
const tryOne = (source, service) => {
|
|
100
|
+
const usable = typeof service?.compactNow === 'function'
|
|
101
|
+
seen.push(source + (usable ? '' : '(no compactNow)'))
|
|
102
|
+
return usable ? { source, service } : null
|
|
103
|
+
}
|
|
104
|
+
const found = tryOne('deps.compaction', deps.compaction)
|
|
105
|
+
?? tryOne('agent.ctx', agent?.ctx?.get?.('compaction'))
|
|
106
|
+
?? tryOne('agent.loopCtx', agent?.loopCtx?.get?.('compaction'))
|
|
107
|
+
return found === null ? { seen } : found
|
|
108
|
+
}
|
|
109
|
+
|
|
20
110
|
/**
|
|
21
|
-
* @param {object} deps 含 persistence / sessions / agents / workspaceRegistry / compaction
|
|
111
|
+
* @param {object} deps 含 persistence / sessions / agents / workspaceRegistry / compaction / sessionController
|
|
22
112
|
* @param {string} dshHome
|
|
23
113
|
* @param {string} sessionId
|
|
24
114
|
* @param {(id:string, ms:number) => Promise<object>} waitForSplit 等这一轮拆解落盘
|
|
@@ -26,7 +116,6 @@ function withTimeout(promise, ms, text) {
|
|
|
26
116
|
*/
|
|
27
117
|
export async function compactThenDelete(deps, dshHome, sessionId, waitForSplit) {
|
|
28
118
|
if (typeof sessionId !== 'string' || sessionId === '') throw new Error('sessionId must be a non-empty string')
|
|
29
|
-
if (deps.compaction === undefined) throw new Error('the compaction service is unavailable; cannot delete')
|
|
30
119
|
|
|
31
120
|
const header = (await deps.persistence.list()).map((r) => r.header ?? r).find((h) => h.id === sessionId)
|
|
32
121
|
if (header === undefined) throw new Error('no such session: ' + sessionId)
|
|
@@ -38,39 +127,114 @@ export async function compactThenDelete(deps, dshHome, sessionId, waitForSplit)
|
|
|
38
127
|
let handle
|
|
39
128
|
let agent = deps.agents?.get(sessionId)
|
|
40
129
|
if (agent === undefined) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (
|
|
130
|
+
// 宿主自己的组合入口:预设插件(含压缩)与模型选择都挂进 agent 作用域。
|
|
131
|
+
const controller = deps.sessionController?.agents
|
|
132
|
+
if (typeof controller?.resume === 'function') {
|
|
133
|
+
try {
|
|
134
|
+
agent = await controller.resume(sessionId)
|
|
135
|
+
await note(dshHome, sessionId, 'resumed via sessionController')
|
|
136
|
+
} catch (error) {
|
|
137
|
+
await note(dshHome, sessionId, 'sessionController.resume failed: ' + message(error))
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (agent === undefined || agent === null) {
|
|
141
|
+
handle = await deps.agents.resume({ resumeSessionId: sessionId })
|
|
142
|
+
agent = handle?.agent
|
|
143
|
+
await note(dshHome, sessionId, 'resumed via agents.resume (bare, no preset mounted)')
|
|
144
|
+
if (agent === undefined) throw new Error('failed to resume the session; cannot delete')
|
|
145
|
+
}
|
|
146
|
+
} else {
|
|
147
|
+
await note(dshHome, sessionId, 'agent was already live')
|
|
44
148
|
}
|
|
45
149
|
|
|
150
|
+
// 压缩后端在预设的隔离作用域里,外面任何 ctx.get 都看不到。
|
|
151
|
+
// 但预设里那份 command-compact 是挂在全局命令表上的:让它替我们调,
|
|
152
|
+
// 它闭包里的 ctx 就在隔离作用域内,能看到 compaction。
|
|
153
|
+
void waitForSplit // 不再依赖事件监听:压缩完自己拆(见 latestCompactionEvent)
|
|
46
154
|
const controller = new AbortController()
|
|
47
|
-
const
|
|
155
|
+
const commands = deps.commands
|
|
48
156
|
|
|
49
|
-
let
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
157
|
+
let compacted = null
|
|
158
|
+
let result = null
|
|
159
|
+
if (typeof commands?.execute === 'function') {
|
|
160
|
+
try {
|
|
161
|
+
compacted = await withTimeout(
|
|
162
|
+
Promise.resolve(commands.execute(agent, '/compact', [], controller.signal)),
|
|
163
|
+
TIMEOUT_MS,
|
|
164
|
+
'compaction timed out'
|
|
165
|
+
)
|
|
166
|
+
} catch (error) {
|
|
167
|
+
controller.abort()
|
|
168
|
+
await note(dshHome, sessionId, '/compact threw: ' + message(error))
|
|
169
|
+
throw new Error('compaction failed, session not deleted: ' + message(error))
|
|
170
|
+
}
|
|
171
|
+
if (compacted === undefined) {
|
|
172
|
+
await note(dshHome, sessionId, '/compact is not registered for this session')
|
|
173
|
+
throw new Error('compaction is unavailable for this session (/compact is not registered), session not deleted')
|
|
174
|
+
}
|
|
175
|
+
const outcome = compacted.result ?? {}
|
|
176
|
+
await note(dshHome, sessionId, '/compact -> ' + outcome.kind + ': ' + (outcome.text ?? ''))
|
|
177
|
+
if (outcome.kind !== 'success') {
|
|
178
|
+
throw new Error('compaction failed, session not deleted: ' + (outcome.text ?? 'unknown error'))
|
|
179
|
+
}
|
|
180
|
+
if (/no compactable history/i.test(outcome.text ?? '')) {
|
|
181
|
+
throw new Error('no compactable history left, so there is nothing to review; use plain delete instead (session not deleted)')
|
|
182
|
+
}
|
|
183
|
+
} else {
|
|
184
|
+
const found = findCompaction(deps, agent)
|
|
185
|
+
if (found.service === undefined) {
|
|
186
|
+
await note(dshHome, sessionId, 'no compaction backend; looked at: ' + (found.seen ?? []).join(', '))
|
|
187
|
+
await release(agent, handle)
|
|
188
|
+
throw new Error('the compaction service is unavailable; cannot delete (no compaction backend is reachable from this session)')
|
|
189
|
+
}
|
|
190
|
+
await note(dshHome, sessionId, 'compaction from ' + found.source)
|
|
191
|
+
try {
|
|
192
|
+
result = await withTimeout(
|
|
193
|
+
Promise.resolve(found.service.compactNow(agent, controller.signal, 'session-seal-scripts-delete')),
|
|
194
|
+
TIMEOUT_MS,
|
|
195
|
+
'compaction timed out'
|
|
196
|
+
)
|
|
197
|
+
} catch (error) {
|
|
198
|
+
controller.abort()
|
|
199
|
+
await note(dshHome, sessionId, 'compactNow threw: ' + message(error))
|
|
200
|
+
throw new Error('compaction failed, session not deleted: ' + message(error))
|
|
201
|
+
}
|
|
202
|
+
if (result === null) {
|
|
203
|
+
await note(dshHome, sessionId, 'compactNow returned null (nothing to compact)')
|
|
204
|
+
throw new Error('no compactable history left, so there is nothing to review; use plain delete instead (session not deleted)')
|
|
205
|
+
}
|
|
59
206
|
}
|
|
60
|
-
|
|
207
|
+
await note(dshHome, sessionId, 'compaction finished')
|
|
61
208
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
209
|
+
// 自己拆:会话里那条 compaction/summary 就是回顾的原料。
|
|
210
|
+
const session = agent?.session
|
|
211
|
+
const event = latestCompactionEvent(session)
|
|
212
|
+
if (event === undefined) {
|
|
213
|
+
await note(dshHome, sessionId, 'no compaction/summary event on the session after compaction')
|
|
214
|
+
} else {
|
|
215
|
+
try {
|
|
216
|
+
const captured = await captureCompaction({ dshHome, cwd, session, event, lang: readLocaleSync(dshHome) })
|
|
217
|
+
await note(dshHome, sessionId, captured?.file
|
|
218
|
+
? 'captured review ourselves: ' + captured.file
|
|
219
|
+
: 'capture skipped: ' + (captured?.reason ?? 'no file'))
|
|
220
|
+
} catch (error) {
|
|
221
|
+
await note(dshHome, sessionId, 'self capture failed: ' + message(error))
|
|
222
|
+
}
|
|
66
223
|
}
|
|
67
224
|
|
|
68
225
|
const after = await listMemories(dshHome, cwd)
|
|
69
226
|
const fresh = after.entries.filter((e) => !before.has(e.file))
|
|
70
|
-
if (fresh.length === 0)
|
|
227
|
+
if (fresh.length === 0) {
|
|
228
|
+
await note(dshHome, sessionId, 'no new review file appeared')
|
|
229
|
+
throw new Error('compaction finished but produced no new review, session not deleted (nothing was removed)')
|
|
230
|
+
}
|
|
71
231
|
|
|
72
232
|
const target = fresh[fresh.length - 1]
|
|
73
233
|
const deleted = await deleteSessionPermanently(deps, sessionId)
|
|
74
|
-
if (!deleted.ok)
|
|
234
|
+
if (!deleted.ok) {
|
|
235
|
+
await note(dshHome, sessionId, 'delete after review failed: ' + deleted.error.message)
|
|
236
|
+
throw new Error('review was captured (' + target.file + ') but the delete failed: ' + deleted.error.message)
|
|
237
|
+
}
|
|
238
|
+
await note(dshHome, sessionId, 'deleted, review ' + target.file)
|
|
75
239
|
return { ok: true, deleted: sessionId, memory: target.file }
|
|
76
|
-
}
|
|
240
|
+
}
|
package/lib/host/delete.js
CHANGED
|
@@ -58,15 +58,38 @@ async function plainDirectory(dir) {
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
|
-
*
|
|
61
|
+
* 关掉活会话。只用宿主公开的能力:agents.create/resume 返回的句柄 + cancel/whenIdle/flush。
|
|
62
|
+
* 拿不到句柄就拒绝,不猜——这是插件原本的语义,也是上游 DSH 唯一能保证的路径。
|
|
62
63
|
* @returns {Promise<{ok:true} | {ok:false, code:string, message:string}>}
|
|
63
64
|
*/
|
|
64
65
|
async function closeLive(deps, sessionId) {
|
|
65
66
|
const live = deps.sessions?.get(sessionId)
|
|
66
67
|
const agent = deps.agents?.get(sessionId)
|
|
67
68
|
if (live === undefined && agent === undefined) return { ok: true }
|
|
68
|
-
|
|
69
|
-
|
|
69
|
+
if (agent === undefined) {
|
|
70
|
+
return { ok: false, code: 'session-busy', message: 'this session is live in the engine but has no agent attached; close it in the app first, then delete' }
|
|
71
|
+
}
|
|
72
|
+
// 1) 宿主自带 teardown:够用时一步到底(cancel → whenIdle → 关作用域 → 落盘句柄 → detach)。
|
|
73
|
+
// 1) 插件在 agents.create/resume 上抓到的句柄。
|
|
74
|
+
const handle = ownedHandlesOf(agent);
|
|
75
|
+
if (handle === undefined) {
|
|
76
|
+
// 2) 没句柄就自己造一把:cancel → whenIdle → 关掉 agent 自己的作用域,全用 agent 上公开的成员。
|
|
77
|
+
// 摘注册表是宿主闭包里的动作,外面拿不到,所以删完可能留一条残留,用 leftover 标记回给调用方。
|
|
78
|
+
if (typeof agent.cancel !== 'function' || typeof agent.whenIdle !== 'function'
|
|
79
|
+
|| typeof agent.scope?.dispose !== 'function') {
|
|
80
|
+
return { ok: false, code: 'session-busy', message: 'the live session has no public handle available; let its owner stop it first, then delete' }
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
agent.cancel({ kind: 'disposed' }, { keepInbox: true })
|
|
84
|
+
await withTimeout(agent.whenIdle(), 'the agent did not become idle')
|
|
85
|
+
await withTimeout(agent.scope.dispose(), 'the agent scope did not close')
|
|
86
|
+
} catch (error) {
|
|
87
|
+
return { ok: false, code: 'session-busy', message: 'cannot shut this session down: ' + message(error) + '; the original logs are kept' }
|
|
88
|
+
}
|
|
89
|
+
const leftover = deps.agents.get(sessionId) !== undefined || deps.sessions.get(sessionId) !== undefined
|
|
90
|
+
return { ok: true, leftover }
|
|
91
|
+
}
|
|
92
|
+
if (agent.session !== live || typeof agent.cancel !== 'function'
|
|
70
93
|
|| typeof agent.whenIdle !== 'function' || typeof deps.sessions?.flush !== 'function') {
|
|
71
94
|
return { ok: false, code: 'session-busy', message: 'the live session has no public handle available; let its owner stop it first, then delete' }
|
|
72
95
|
}
|
|
@@ -112,6 +135,10 @@ export async function deleteSessionPermanently(deps, sessionId) {
|
|
|
112
135
|
|
|
113
136
|
const closed = await closeLive(deps, sessionId)
|
|
114
137
|
if (!closed.ok) return failure(409, closed.code, closed.message)
|
|
138
|
+
// 自己造的那把钥匙只能停住 agent;注册表摘除是宿主闭包里的动作,外面做不到。
|
|
139
|
+
const stopNote = closed.leftover === true
|
|
140
|
+
? 'the engine still holds a live reference to this session until it restarts'
|
|
141
|
+
: ''
|
|
115
142
|
|
|
116
143
|
const dir = sessionDirOf(root, header.cwd, sessionId)
|
|
117
144
|
if (!inside(resolve(root), dir)) return failure(500, 'delete-failed', 'the session directory escaped the storage root')
|
|
@@ -124,7 +151,8 @@ export async function deleteSessionPermanently(deps, sessionId) {
|
|
|
124
151
|
return failure(500, 'delete-failed', 'cannot delete the session log: ' + message(error))
|
|
125
152
|
}
|
|
126
153
|
|
|
127
|
-
const warnings = []
|
|
154
|
+
const warnings = stopNote === '' ? [] : [stopNote]
|
|
155
|
+
// 目录已删,让工作区把它从列表里摘掉(公开接口只有这一条)。
|
|
128
156
|
for (const workspace of workspaces) {
|
|
129
157
|
try { await workspace.detachSession(sessionId) }
|
|
130
158
|
catch (error) { warnings.push('workspace ' + workspace.id + ' still holds this session: ' + message(error)) }
|