Files
apes-sidebar/tests/team-projection.spec.ts
T
ZinChl e38aa56fec 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。
2026-09-30 02:24:36 +08:00

136 lines
5.6 KiB
TypeScript

/**
* Unit tests for the `agentTeam` projection adapter: reading the Lead
* Session's board off the client snapshot, and overlaying runtime activity
* onto the durable roster.
*
* The shape under test is DSH 0.1.7's `TeamProjection` (see the experimental
* package's `lib/types/types.d.ts`): `{ members: {id,name,role,phase,error?},
* tasks: TeamTaskView[], failure? }`. There is no `available` flag and no
* `model` field — a projection that is absent means "this session leads no
* team" (or the deployment never mounted the layer), which is the same silent
* degradation either way.
*/
import { describe, expect, it } from 'vitest'
import { teamMembersOf, teamProjectionOf } from '../src/client/team-projection.ts'
import type {
SidebarChildLiveView,
SidebarSessionList,
SidebarSessionSummary,
SidebarTeamMemberProjection,
SidebarTeamProjection,
} from '../src/context-types.ts'
/** One durable roster row. */
function member(over: Partial<SidebarTeamMemberProjection> = {}): SidebarTeamMemberProjection {
return { id: 'w', name: 'writer', role: 'teammate', phase: 'active', ...over }
}
/** One Lead Session projection. */
function team(over: Partial<SidebarTeamProjection> = {}): SidebarTeamProjection {
return { members: [member()], tasks: [], ...over }
}
/** The snapshot's projection map with one session's team installed. */
function projections(
rootId: string,
teamValue: SidebarTeamProjection,
): SidebarSessionList['projectionsBySession'] {
return { [rootId]: { values: { agentTeam: teamValue }, state: 'ready', error: null } }
}
/** A summary row. */
function summary(id: string, over: Partial<SidebarSessionSummary> = {}): SidebarSessionSummary {
return { id, displayTitle: id, ...over }
}
describe('teamProjectionOf', () => {
it('reads the root session own projection', () => {
const value = team()
expect(teamProjectionOf(projections('root', value), 'root')).toBe(value)
})
it('answers undefined without a root, without a snapshot, or without a team', () => {
expect(teamProjectionOf(projections('root', team()), undefined)).toBeUndefined()
expect(teamProjectionOf(undefined, 'root')).toBeUndefined()
// A session the projection store knows, but which leads no team (the
// deployment without the experimental layer, or a plain subagent tree).
const other: SidebarSessionList['projectionsBySession'] = {
root: { values: {}, state: 'ready', error: null },
}
expect(teamProjectionOf(other, 'root')).toBeUndefined()
expect(teamProjectionOf(projections('root', team()), 'someone-else')).toBeUndefined()
})
})
describe('teamMembersOf', () => {
const noLive: Record<string, SidebarChildLiveView | undefined> = {}
it('answers an empty roster without a team', () => {
expect(teamMembersOf(undefined, noLive, {})).toEqual([])
})
it('carries the durable phase and the member identity through', () => {
const rows = teamMembersOf(
team({ members: [member({ id: 'lead', name: 'lead', role: 'lead' }), member({ id: 'w' })] }),
noLive,
{},
)
expect(rows).toEqual([
{ id: 'lead', name: 'lead', role: 'lead', phase: 'active', status: 'idle', diagnostics: [] },
{ id: 'w', name: 'writer', role: 'teammate', phase: 'active', status: 'idle', diagnostics: [] },
])
})
it('reads an active member activity from the live channel', () => {
const live: Record<string, SidebarChildLiveView | undefined> = { w: { running: true } }
expect(teamMembersOf(team(), live, {})[0]?.status).toBe('running')
})
it('falls back to the session list running flag (the Lead is no live child)', () => {
const byId = { lead: summary('lead', { running: true }) }
const rows = teamMembersOf(
team({ members: [member({ id: 'lead', name: 'lead', role: 'lead' })] }),
noLive,
byId,
)
expect(rows[0]?.status).toBe('running')
})
it('never claims a running turn while the durable phase disagrees', () => {
const live: Record<string, SidebarChildLiveView | undefined> = { w: { running: true } }
const provisioning = teamMembersOf(team({ members: [member({ phase: 'provisioning' })] }), live, {})
expect(provisioning[0]?.status).toBe('provisioning')
const failed = teamMembersOf(team({ members: [member({ phase: 'failed' })] }), live, {})
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' })] }),
noLive,
{},
)
expect(rows[0]?.diagnostics).toEqual(['spawn rejected'])
})
it('treats a member the live channel has not mentioned as idle', () => {
const live: Record<string, SidebarChildLiveView | undefined> = { other: { running: true } }
expect(teamMembersOf(team(), live, {})[0]?.status).toBe('idle')
})
})