Sessions: account a restored Session to its Workspace

Ensure() only registered a Workspace when the Session was new, so Sessions restored from disk never entered the Workspace record and stayed invisible in the panel. Always create (idempotent) and attach an unaccounted Session; warn on attach failure. Tests gain a shared Workspace fixture.
This commit is contained in:
2026-11-02 04:28:26 +08:00
parent 3bee231da6
commit d527ad8045
4 changed files with 40 additions and 7 deletions
+11 -2
View File
@@ -139,12 +139,21 @@ export class Sessions {
async ensure(id: string, cwd: string, title: string, role: 'orchestrator' | 'worker'): Promise<Agent> {
const ctx = this.ctx
let result = await ctx.sessionController.resolveAgent(id as SessionId)
if ('error' in result && result.error.code === 'session/not-found') {
const workspace = await ctx.workspaceRegistry.create(cwd)
const created = 'error' in result && result.error.code === 'session/not-found'
// Resolve the Workspace even for a Session that already exists: a restored Session, and one adopted from another DSH
// home, must end up accounted to the Workspace its directory names, or the panel lists it nowhere.
const workspace = await ctx.workspaceRegistry.create(cwd)
if (created) {
// A Session keeps the preset it was created with, one of the bundle's own (cordis.patch.yml).
await ctx.sessionController.create({ sessionId: id as SessionId, workspaceId: workspace.id,
agentPreset: role === 'worker' ? WORKER_PRESET : ROUTER_PRESET })
result = await ctx.sessionController.resolveAgent(id as SessionId)
} else if (!workspace.sessionIds.includes(id as SessionId)) {
// A failed attach leaves the Session usable but unlistable in its Workspace, which the panel shows and an operator can
// fix there, so it warns instead of failing the launch.
await workspace.attachSession(id as SessionId).catch((error: unknown) => {
ctx.logger.warn('dsh-superagent could not attach Session %s to Workspace %s: %s', id, workspace.id, String(error))
})
}
if ('error' in result) throw result.error
const settings = this.host.store.settings()