fix(tasks): 会话「驻留」不再当作「agent 在跑」——崩溃重启后的子代理状态(#800)

宿主目录行的 `activity` 是**会话驻留**而非运行态(宿主自己的字段注释:
"recursive catalog listing observed a resident Session … does not encode a
durable outcome or guarantee continuation delivery")。崩溃重启后,DSH 打开
被中断子会话的历史流会把它 prepare → retain → attach 成驻留 Session,但不会
启动 agent,于是 `activity === 'running'` 成立、卡片显示「运行中」并附带实时
预览行,而实际上没有任何东西在跑。

权威运行位其实早就在手边:会话摘要的 `running`(根卡片已经在用,
`subagent-lineage` 统计后代也在用,DSH 官方子代理下拉同样用它)。改为
「摘要的 running 优先、目录标志兜底」:

- `src/client/tasks-model.ts`:子节点 activity/state 由
  `summary.running ?? (view.running === true)` 决定。摘要没有该字段的老宿主
  继续走目录标志,行为不变。
- `src/client/team-projection.ts`:同一规则(原先恰好相反,优先了驻留标志)。

测试:
- `tests/tasks-page.spec.tsx` 新增两条:驻留但未运行 → 不是 running 卡片
  (旧代码下断言 `data-running` 为 null 会红);摘要无 running 字段 → 回落到
  目录标志(旧行为保留)。同时把两条既有用例的 fixture 修正为两路一致
  (原先 live 说 running、摘要说 false,本身自相矛盾)。
- `tests/team-projection.spec.ts` 新增两条:摘要 false + live true → idle;
  摘要 true + live false → running。
This commit is contained in:
ZinChl committed 2026-09-30 02:24:36 +08:00
1 parent 492661a3ea
commit e38aa56fec
4 files changed
+84 -4

No files matched your search

+14 -1
View File
@@ -364,7 +364,20 @@ export function buildTasksModel(input: TasksModelInput): TasksNode[] {
// reports EVERY child with an explicit `running` flag, and a row keeps
// its disclosure unless its own catalog is known-empty.
const view = childLive(live, entry.id)
const activity: 'running' | 'inactive' = view?.running === true ? 'running' : 'inactive'
// …but that flag is SESSION RESIDENCY, not "an agent is running": the
// host's own field doc says it means "recursive catalog listing observed
// a resident Session … does not encode a durable outcome or guarantee
// continuation delivery". After a crash-restart, opening a child's
// history retains its session WITHOUT starting an agent, so residency
// reads `running` for a child nothing is driving (issue #800: the card
// showed 运行中 with a live preview line for an interrupted subagent).
// The session summary's `running` is the authoritative run bit — the root
// card below reads it, so does DSH's own subagent dropdown, and
// ./subagent-lineage.ts already counts descendants from it — so it wins
// whenever the host reports the field; the catalog flag stays the
// fallback for a deployment whose summaries do not carry it.
const running = summary?.running ?? (view?.running === true)
const activity: 'running' | 'inactive' = running ? 'running' : 'inactive'
const state: TasksNodeState = activity === 'running'
? 'running'
: team !== undefined ? teamState(team.status) : 'done'
+6 -1
View File
@@ -117,6 +117,11 @@ function memberStatus(
): TeamMemberRow['status'] {
if (member.phase === 'failed') return 'failed'
if (member.phase === 'provisioning') return 'provisioning'
const running = live[member.id]?.running ?? byId[member.id]?.running
// The summary's `running` is the authoritative run bit; the live channel's
// flag is the catalog's RESIDENCY, which reads `running` for a retained
// session that no agent is driving (a crash-restart — issue #800). Same rule
// as the task tree: authoritative wins, the live flag only stands in when the
// host's summaries do not carry the field.
const running = byId[member.id]?.running ?? live[member.id]?.running
return running === true ? 'running' : 'idle'
}
+49 -2
View File
@@ -805,7 +805,12 @@ describe('Tasks page: the shared task window', () => {
summary: { counts: [{ kind: 'read', count: 2 }], runningDetail: '' },
},
}
const store = makeStore(snapshotWithChildren(2))
// A running child says so in BOTH channels: the live flag is the catalog's
// session RESIDENCY, the summary's `running` is the authoritative run bit
// (see the #800 case below for what residency alone means after a restart).
const snapshot = snapshotWithChildren(2)
snapshot.byId['child-0']!.running = true
const store = makeStore(snapshot)
const { container, unmount } = renderRoot(
createElement(SubagentView, { sessionId: 'root', active: true, ctx: makeCtx(store) }),
)
@@ -825,6 +830,46 @@ describe('Tasks page: the shared task window', () => {
unmount()
})
it('a retained-but-not-running child is NOT a running card (issue #800)', async () => {
// After a crash-restart, opening an interrupted child's history RETAINS its
// session without starting an agent: the catalog's `activity` — which the
// live channel reports as `running` — flips to running while the
// authoritative summary still says `running: false`. The card must follow
// the summary; the old reading showed 运行中 (with a live preview line) for a
// subagent nothing was driving.
livePayload = { 'child-0': { running: true, text: 'half-finished line' } }
const store = makeStore(snapshotWithChildren(2))
const { container, unmount } = renderRoot(
createElement(SubagentView, { sessionId: 'root', active: true, ctx: makeCtx(store) }),
)
await flushJobs()
// Both children are settled now, so the global fold holds them: unfold.
const foldToggle = container.querySelector('button[aria-label="展开已完成的节点"]') as HTMLButtonElement
await act(async () => { foldToggle.click() })
const child = container.querySelector('[data-graph-node="child-0"]') as HTMLElement
expect(child).not.toBeNull()
const bar = child.querySelector('[data-card-bar]') as HTMLElement
expect(bar.getAttribute('data-running')).toBeNull()
expect(bar.textContent).toContain('已完成')
unmount()
})
it('falls back to the catalog flag when the host carries no summary running bit', async () => {
// Older hosts: the summary has no `running` field, so the live channel's
// flag is all there is — the pre-#800 reading must survive.
livePayload = { 'child-0': { running: true, summary: { counts: [], runningDetail: '' } } }
const snapshot = snapshotWithChildren(2)
delete snapshot.byId['child-0']!.running
const store = makeStore(snapshot)
const { container, unmount } = renderRoot(
createElement(SubagentView, { sessionId: 'root', active: true, ctx: makeCtx(store) }),
)
await flushJobs()
const bar = container.querySelector('[data-graph-node="child-0"] [data-card-bar]') as HTMLElement
expect(bar.getAttribute('data-running')).toBe('true')
unmount()
})
it('folds one settled card into the aggregate from its bar, and expands it back', async () => {
const store = makeStore(snapshotWithChildren(3))
const { container, unmount } = renderRoot(
@@ -936,7 +981,9 @@ describe('Tasks page: the shared task window', () => {
it('offers no fold chevron on a running card or on the current session', async () => {
livePayload = { 'child-0': { running: true, summary: { counts: [], runningDetail: '' } } }
const store = makeStore(snapshotWithChildren(2))
const snapshot = snapshotWithChildren(2)
snapshot.byId['child-0']!.running = true
const store = makeStore(snapshot)
const { container, unmount } = renderRoot(
createElement(SubagentView, { sessionId: 'root', active: true, ctx: makeCtx(store) }),
)
+15
View File
@@ -104,6 +104,21 @@ describe('teamMembersOf', () => {
expect(failed[0]?.status).toBe('failed')
})
it('ignores the live channel residency when the summary says the agent is not running (issue #800)', () => {
// The live flag is the catalog's SESSION RESIDENCY: a retained session
// whose agent died in a crash-restart reads `running` there while the
// authoritative summary stays false. The summary wins.
const live: Record<string, SidebarChildLiveView | undefined> = { w: { running: true } }
const byId = { w: summary('w', { running: false }) }
expect(teamMembersOf(team(), live, byId)[0]?.status).toBe('idle')
})
it('takes the Summary running bit even when the live channel says otherwise', () => {
const live: Record<string, SidebarChildLiveView | undefined> = { w: { running: false } }
const byId = { w: summary('w', { running: true }) }
expect(teamMembersOf(team(), live, byId)[0]?.status).toBe('running')
})
it('exposes the durable member error as the diagnostics list', () => {
const rows = teamMembersOf(
team({ members: [member({ phase: 'failed', error: 'spawn rejected' })] }),