dsh-cothread-mcp 0.1.6 → 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/CHANGELOG.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.7
4
+ - Fix the automation task creating a new session on every save, which left a pile of sessions in the workspace and moved the conversation away from the one that had the actual content. The host only reuses a session that has not run a turn yet, so an automation session that had already run was replaced by a fresh one each time. The session is now reused as long as it exists, and the task is bound to the same-named session that already has turns, so the conversation stays where it was.
3
5
  ## 0.1.6
4
6
  - Fix the automation session still showing up outside the selected workspace. The session was created against the workspace's directory, so it only owned that directory and never joined the workspace's session list; it is now created against the workspace itself, which makes the host attach it there. Sessions left outside every workspace by earlier versions are adopted into the workspace they belong to the next time the project is saved, and the task binds to the session the host actually created.
5
7
  ## 0.1.5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-cothread-mcp",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "连接共序 CoThread MCP,支持浏览器授权、项目设置及消息提醒。",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/automation.js CHANGED
@@ -237,12 +237,18 @@ async function ensureSession(ctx, sessionId, cwd, title, workspaceId = '') {
237
237
  const registry = serviceOf(ctx, 'workspaceRegistry')
238
238
  const archived = registry && Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
239
239
  if (archived.includes(sessionId)) return { ok: false, sessionId, reason: '会话已归档' }
240
+ // 会话已经存在就直接用它,不再请宿主创建。
241
+ // 宿主在 session.create 里只复用「没跑过轮次」的空白会话,自动化会话跑过一轮之后就不可复用了,
242
+ // 每保存一次宿主就另建一个会话,任务改绑到新会话上,之前那个还在工作区里挂着,一次攒一堆。
243
+ // 自动化任务要的是「一直往同一个会话里投递」,宿主在 schedule 那边对 session 目标也是这么做的:
244
+ // 会话存在就能投递,不需要是空白会话。所以这里只确认它在,不重建。
245
+ if (await sessionExists(ctx, sessionId)) {
246
+ // 会话早于本次改动建成、还没挂进工作区时,宿主不会替它补挂,这里补上。
247
+ await attachToWorkspace(ctx, workspaceId, sessionId)
248
+ nameSession(ctx, sessionId, title)
249
+ return { ok: true, sessionId }
250
+ }
240
251
  let createdId = sessionId
241
- // 先挂载,再创建。
242
- // 宿主在 session.create 里带上 workspaceId 时,只复用工作区已经记着的会话;会话没挂上去
243
- // 就会被另建一个,于是记住的还是旧 id,旧会话继续留在「未分组」里。先挂载,宿主就复用它。
244
- // 会话还不存在时挂载会失败(读不到它的 header),忽略即可,宿主创建时会自己挂上。
245
- await attachToWorkspace(ctx, workspaceId, sessionId)
246
252
  try {
247
253
  // 宿主的 session.create 里 workspaceId 与 cwd 互斥:只给 cwd 时,会话只是「拥有」这个
248
254
  // 目录,并不会被加进工作区的会话列表,界面上打开会话就看不到它挂在选中的工作区下。
@@ -255,12 +261,106 @@ async function ensureSession(ctx, sessionId, cwd, title, workspaceId = '') {
255
261
  // 会话已经存在时不算失败,接着往下走命名。
256
262
  if (!/exist|already/i.test(message)) return { ok: false, sessionId, reason: message }
257
263
  }
258
- // 宿主另建了会话时,它自己会挂进工作区;这里再确认一次,幂等。
259
264
  await attachToWorkspace(ctx, workspaceId, createdId)
260
265
  nameSession(ctx, createdId, title)
261
266
  return { ok: true, sessionId: createdId }
262
267
  }
263
268
 
269
+ /**
270
+ * 找一个已经有内容的同名会话。
271
+ *
272
+ * 自动化任务要的是「一直往同一个会话里投递」。宿主只在会话是空白(没跑过轮次)时才复用,
273
+ * 所以每保存一次就会另建一个会话,之前那个被孤立在工作区里。这里从存储的会话里找出标题相同、
274
+ * 且已经跑过轮次的那个,让任务绑回它。
275
+ */
276
+ async function findActiveSession(ctx, workspaceId, cwd, title) {
277
+ if (!workspaceId || !cwd || !title) return ''
278
+ const registry = serviceOf(ctx, 'workspaceRegistry')
279
+ if (!registry || typeof registry.get !== 'function') return ''
280
+ const workspace = registry.get(workspaceId)
281
+ if (!workspace) return ''
282
+ const query = serviceOf(ctx, 'sessionQuery')
283
+ if (!query || typeof query.observeSession !== 'function') return ''
284
+ const members = Array.isArray(workspace.sessionIds) ? workspace.sessionIds : []
285
+ const archived = Array.isArray(registry.archivedSessionIds) ? registry.archivedSessionIds : []
286
+ let best = ''
287
+ for (const id of members) {
288
+ if (!id || archived.includes(id)) continue
289
+ const session = serviceOf(ctx, 'sessions')
290
+ const live = session && typeof session.get === 'function' ? session.get(id) : undefined
291
+ if (live) {
292
+ // 在跑的会话直接看它的标题;跑过轮次的才算——空白会话不算,它没有内容可看。
293
+ const name = await sessionTitleOf(ctx, live)
294
+ if (name !== title) continue
295
+ if (!await hasTurn(ctx, live)) continue
296
+ if (best === '' || id < best) best = id
297
+ continue
298
+ }
299
+ let observation
300
+ try {
301
+ observation = await query.observeSession(id)
302
+ } catch {
303
+ continue
304
+ }
305
+ if (!observation) continue
306
+ const events = observation.events || []
307
+ let name = ''
308
+ let started = false
309
+ for (const event of events) {
310
+ if (event && event.type === 'turn/start') started = true
311
+ if (event && event.type === 'session/title' && event.title) name = String(event.title)
312
+ }
313
+ if (!started || name !== title) continue
314
+ if (best === '' || id < best) best = id
315
+ }
316
+ return best
317
+ }
318
+
319
+ /** 这个会话跑过轮次吗。空白会话没有内容,不算。 */
320
+ async function hasTurn(ctx, session) {
321
+ const agents = serviceOf(ctx, 'agents')
322
+ if (agents && typeof agents.turnBoundaryOf === 'function') {
323
+ try {
324
+ const boundary = agents.turnBoundaryOf(session)
325
+ if (boundary && (boundary.lastTurn > 0 || boundary.openTurnStartSeq !== null)) return true
326
+ } catch {
327
+ // 读不到就按下面的事件判断。
328
+ }
329
+ }
330
+ const events = session && typeof session.snapshotEvents === 'function' ? session.snapshotEvents() : []
331
+ for (const event of events) if (event && event.type === 'turn/start') return true
332
+ return false
333
+ }
334
+ /** 读一个会话的标题,读不到就当作没有。 */
335
+ async function sessionTitleOf(ctx, session) {
336
+ const titles = serviceOf(ctx, 'sessionTitle')
337
+ if (!titles || typeof titles.get !== 'function') return ''
338
+ try {
339
+ const current = titles.get(session)
340
+ return current && current.title ? String(current.title) : ''
341
+ } catch {
342
+ return ''
343
+ }
344
+ }
345
+ /**
346
+ * 这个会话还在不在。
347
+ *
348
+ * 宿主里分两种:在跑的(live)与存着的(已存储)。两者都算会话还在——自动化任务要往同一个
349
+ * 会话里持续投递,会话跑过一轮也没关系。宿主给自动化任务投递时对 session 目标也是这么判的。
350
+ */
351
+ async function sessionExists(ctx, sessionId) {
352
+ if (!sessionId) return false
353
+ const sessions = serviceOf(ctx, 'sessions')
354
+ if (sessions && typeof sessions.get === 'function' && sessions.get(sessionId)) return true
355
+ const persistence = serviceOf(ctx, 'sessionPersistence')
356
+ if (!persistence || typeof persistence.stat !== 'function') return false
357
+ try {
358
+ const stored = await persistence.stat(sessionId)
359
+ return stored !== undefined && stored !== null
360
+ } catch {
361
+ return false
362
+ }
363
+ }
264
364
  /**
265
365
  * 把会话挂进它该在的工作区。
266
366
  *
@@ -418,7 +518,10 @@ export async function syncAutomations(ctx, options) {
418
518
  if (await deleteTask(schedule, extra.id)) { deleted++; deletedIds.add(extra.id) }
419
519
  }
420
520
  if (!task) {
421
- const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, workspaceId: row.workspaceId, sessionId: reusableSession(mapping[key], registry) })
521
+ // 优先绑回那个已经跑过轮次、有内容的同名会话;没有才新建。
522
+ const active = await findActiveSession(ctx, row.workspaceId, cwd, title)
523
+ const rememberedSession = active || reusableSession(mapping[key], registry)
524
+ const made = await createTask(ctx, schedule, { title, prompt, seconds, cwd, workspaceId: row.workspaceId, sessionId: rememberedSession })
422
525
  task = made.task
423
526
  created++
424
527
  current = seconds
@@ -578,6 +681,15 @@ export async function saveAutomation(ctx, options) {
578
681
  const rebound = await rebindTask(schedule, task, remembered)
579
682
  if (rebound !== sessionId) { sessionId = rebound; updated++ }
580
683
  }
684
+ // 任务现在绑着的是个空白会话,而有内容的是另一个会话:改绑到有内容的那个,
685
+ // 免得巡检跑在一个空会话里,之前的内容留在旁边没人看。
686
+ if (sessionId) {
687
+ const active = await findActiveSession(ctx, row.workspaceId, cwd, title)
688
+ if (active && active !== sessionId) {
689
+ const rebound = await rebindTask(schedule, task, active)
690
+ if (rebound === active) { sessionId = active; updated++ }
691
+ }
692
+ }
581
693
  // 早期版本留下的会话还在「未分组」里,先把它们归位到选中的工作区。
582
694
  await adoptOrphanSessions(ctx, row.workspaceId, cwd)
583
695
  // 会话当场建出来:保存完就能打开它,任务也已经绑在它上面。