Files
apes-sidebar/src/team-routes.ts
T
Menghuan1918 d8bc6566df feat(tasks): 适配 DSH 0.1.7 的 Agent Teams(读 projection + 写路由对接真实服务)
团队功能在 0.1.7 上整条失效:team-routes.ts 当初镜像的是 0.1.6 的 Remote 词汇
(remoteView / remoteCreateTask / remoteUpdateTask),而 0.1.7 把这三个方法删掉了,
读路径改到 Lead Session 的 `agentTeam` Session projection。真机 3080 复现:
teams.view 对带团队的会话返回 400 "membership.svc.remoteView is not a function",
于是 TeamBoard 从不渲染、节点富化与任务窗连带失效。

- 读:删掉 teams.view 与 5s 轮询,改读 SessionListState.projectionsBySession[root]
  .values.agentTeam(与 subagentCatalog 同源,推送式);新增 client/team-projection.ts
  做纯适配器,成员运行状态由 subagents.live 的 running 叠加、Lead 回退 session list
  (官方 README 的 "overlays activity from Session status")。projection 缺席 =
  这里没有团队,与旧的 {available:false}/{team:null} 合并成同一静默降级。
- 写:taskCreate/taskUpdate 改调 0.1.7 的 createTask/updateTask(返回已提交的 view,
  拒绝改为抛 TeamError),TEAM_TASK_STALE_REVISION → 409 team-conflict,其余 → 400
  team-error(wire 新增 team-conflict)。api.ts 的 writeTask 把失败信封还原成
  {ok:false,code,message} —— 原先窗口永远走不到该分支(readEnvelope 是抛)。
- 任务窗:外壳换成上一轮的 FloatingWindow(可拖拽/可拉伸/只在关闭或 Esc 时消失),
  动作行下沉到 shell 的 footer,长描述不再把动作推出视野。
- 真机抓到的两个单测抓不到的问题:
  1. 队列中任务的「完成」被宿主状态机拒绝(pending → claim → in_progress →
     complete → completed → reopen)→ 动作行跟随状态,pending 出「认领」;
  2. 认领后 Tag 变「阻塞」—— ready 的语义只是「pending 且无未完成 blocker」,
     对所有非 pending 任务都是 false → 抽出唯一规则 taskBlocked/taskStatusLabel/
     taskTone/taskDotState,四处展示点全部改走它。
- i18n:新增 teamTaskClaim / taskWindowCreate / taskWindowDetail /
  tasksStateProvisioning 词条 ×20 词典。

验证:typecheck / lint 通过;pnpm test 1276 passed / 9 skipped;
pnpm test:mount 7/7(真实 DSH 0.1.7-rc.2 挂载 + tab 全扫无崩溃)。
真机 3080(0.1.7-rc.1 + Agent Teams 实验层):看板从 projection 渲染、
队列任务「认领」→「完成」全链路写通(负责人 lead / 进行中 / 已完成)、
外部点击不关窗、Escape 关窗,截图见 docs/plans/2026-09-27-agent-teams-dsh-0.1.7-adaptation.md。

(cherry picked from commit dbc524e0b350c5d3564843f4776cd15d797506d8)
2026-09-27 22:19:05 +08:00

181 lines
8.1 KiB
TypeScript

/**
* The Agent Teams WRITE routes of the /sidebar JSON API ('teams.taskCreate' /
* 'teams.taskUpdate').
*
* The READ path is deliberately absent. DSH 0.1.7 replaced the 0.1.6 Remote
* trio (`remoteView` / `remoteCreateTask` / `remoteUpdateTask`, which this
* plugin's `teams.view` route mirrored) with the Lead Session's `agentTeam`
* Session projection: the browser already receives it in
* `SessionListState.projectionsBySession`, so the Tasks page reads the board
* straight off the shared snapshot (see ./client/team-projection.ts) and no
* host route, poller or observation handshake is involved. That also removed
* the only reason this file needed `tryMembership` for reads.
*
* What remains cannot be done in the browser: `createTask` / `updateTask`
* demand the exact live Lead Agent as their authority credential, which only
* the host holds. The experimental `dsh-experimental-agent-team` service
* (`ctx.agentTeams`) exists only when the deployment opts in, so the routes
* degrade structurally:
*
* - service absent (`ctx.get('agentTeams')` undefined) → 503 `team-error`
* (a mutation can never be accepted there; the page silently hides the
* whole Teams block because the projection is absent too, so this is the
* belt to that pair of braces);
* - the tree's root agent is not live in this process (cold session / another
* harness) → 404 `team-error`; teams are live-led by definition;
* - the root leads no team (`tryMembership` miss) → 404 `team-error`.
*
* Rejections are THROWN by 0.1.7 (0.1.6 returned a `TeamTaskMutationResult`
* union), so the CAS conflict has to be translated back into a wire code the
* client can route on: `TEAM_TASK_STALE_REVISION` → `team-conflict` (409),
* every other `TeamError` → `team-error` (400). The message is never parsed.
*
* Teams are implicit (TeamId ≡ the lead's session id) and every service
* method demands the live member Agent as its authority token, so the routes
* re-derive the caller per request (`ctx.agents.get(rootSessionId)`) exactly
* like the jobs.kill fence. Zero DSH source changes; the plugin never imports
* the experimental package (structural mirrors only).
*/
import type {
Context,
SidebarAgentTeamsService,
SidebarAgentsService,
SidebarCreateTeamTaskRequest,
SidebarTeamTaskMutationResult,
SidebarUpdateTeamTaskRequest,
} from './context-types.ts'
import { requireString, SidebarError } from './wire.ts'
/** The Agent Teams write routes of the sidebar API. */
export interface SidebarTeamsRoutes {
/** Create one shared task (payload = `{ rootSessionId, ...request }`). */
taskCreate(payload: unknown): Promise<SidebarTeamTaskMutationResult>
/** CAS-mutate one shared task (payload = `{ rootSessionId, ...request }`). */
taskUpdate(payload: unknown): Promise<SidebarTeamTaskMutationResult>
}
/** The CAS actions the wire accepts (mirrors the domain union). */
const TASK_ACTIONS = new Set([
'claim', 'release', 'edit', 'set_dependencies', 'complete', 'reopen', 'reassign', 'delete',
])
/** The service's stale-revision code (0.1.6 returned it as a result union). */
const STALE_REVISION_CODE = 'TEAM_TASK_STALE_REVISION'
/** Narrow an unknown payload value to a string array (else undefined). */
function stringArrayOf(value: unknown): string[] | undefined {
if (!Array.isArray(value)) return undefined
const out = value.filter((item): item is string => typeof item === 'string')
return out.length === value.length ? out : undefined
}
/**
* The thrown value's `HarnessError` code, when it carries one. Read
* structurally: the plugin does not import `@deepseek-ai/dsh-llm` for a
* string, and a non-TeamError rejection simply has no code to route on.
*/
function errorCodeOf(error: unknown): string | undefined {
if (typeof error !== 'object' || error === null) return undefined
const code = (error as { code?: unknown }).code
return typeof code === 'string' ? code : undefined
}
/**
* Build the Agent Teams write routes bound to the plugin context.
* @param ctx - host plugin context.
*/
export function buildTeamsApi(ctx: Context): SidebarTeamsRoutes {
const teams = ctx.get('agentTeams') as SidebarAgentTeamsService | undefined
const agents = ctx.get('agents') as SidebarAgentsService | undefined
/**
* The live lead agent of the requested tree plus its membership check.
* Throws 503 when the experimental layer is absent and 404 when the root
* simply leads no team (a normal page state, but never for a mutation).
*/
const membershipOf = async (
payload: unknown,
): Promise<{ svc: SidebarAgentTeamsService; agent: unknown }> => {
if (teams === undefined) {
throw new SidebarError('team-error', 'the agent-teams layer is not mounted in this deployment', 503)
}
const rootSessionId = requireString(payload, 'rootSessionId')
const agent = agents?.get(rootSessionId)
if (agent === undefined) {
// The root is not live in this process (cold session / other harness):
// teams are live-led by definition, so there is nothing to mutate.
throw new SidebarError('team-error', 'the tree root is not live in this process', 404)
}
try {
if (teams.tryMembership(agent) === undefined) {
throw new SidebarError('team-error', 'the tree root leads no team', 404)
}
} catch (error) {
if (error instanceof SidebarError) throw error
throw new SidebarError('team-error', error instanceof Error ? error.message : String(error), 404)
}
return { svc: teams, agent }
}
/**
* Translate one service rejection: a stale revision keeps its own wire code
* so the client's task window can say "someone else changed this task"
* instead of showing a generic failure.
*/
const serviceError = (error: unknown): SidebarError => {
const message = error instanceof Error ? error.message : String(error)
return errorCodeOf(error) === STALE_REVISION_CODE
? new SidebarError('team-conflict', message, 409)
: new SidebarError('team-error', message, 400)
}
return {
async taskCreate(payload) {
const membership = await membershipOf(payload)
const record = payload as Record<string, unknown>
const blockedBy = stringArrayOf(record.blockedBy)
const writeScopes = stringArrayOf(record.writeScopes)
const req: SidebarCreateTeamTaskRequest = {
subject: requireString(payload, 'subject'),
description: typeof record.description === 'string' ? record.description : '',
...(blockedBy !== undefined ? { blockedBy } : {}),
...(writeScopes !== undefined ? { writeScopes } : {}),
}
try {
return { ok: true, value: await membership.svc.createTask(membership.agent, req) }
} catch (error) {
throw serviceError(error)
}
},
async taskUpdate(payload) {
const membership = await membershipOf(payload)
const record = payload as Record<string, unknown>
const expectedRevision = record.expectedRevision
if (typeof expectedRevision !== 'number' || !Number.isSafeInteger(expectedRevision) || expectedRevision < 0) {
throw new SidebarError('bad-request', 'missing or invalid "expectedRevision"')
}
const action = record.action
if (typeof action !== 'string' || !TASK_ACTIONS.has(action)) {
throw new SidebarError('bad-request', 'missing or invalid "action"')
}
const blockedBy = stringArrayOf(record.blockedBy)
const writeScopes = stringArrayOf(record.writeScopes)
const req: SidebarUpdateTeamTaskRequest = {
taskId: requireString(payload, 'taskId'),
expectedRevision,
action: action as SidebarUpdateTeamTaskRequest['action'],
...(typeof record.subject === 'string' ? { subject: record.subject } : {}),
...(typeof record.description === 'string' ? { description: record.description } : {}),
...(blockedBy !== undefined ? { blockedBy } : {}),
...(writeScopes !== undefined ? { writeScopes } : {}),
...(typeof record.owner === 'string' ? { owner: record.owner } : {}),
}
try {
return { ok: true, value: await membership.svc.updateTask(membership.agent, req) }
} catch (error) {
throw serviceError(error)
}
},
}
}