refactor(tasks): 后台任务改读宿主客户端 jobs 服务,删除自建三条路由

- 新增 src/client/jobs-client.ts:结构探测 ctx.jobs(watchRows / observe /
  state / kill),引用计数的按会话 roster 订阅、只观测展开的那一行、
  跨树去重(无主任务归到承载它的被观测会话,树外 owner 丢弃)。
- 抽屉与输出浮窗改读该服务:行来自推送 roster(状态 / 时长 / owner / 进度),
  输出面换 TerminalBlock 显示宿主为观察者流的保留输出(gapBefore → 截断提示、
  error → 失败提示),kill 走 jobs.kill(owner, jobId),两击确认不变。
- 自动激活(use-host-feeds)改骑同一条推送 roster;detectNewJob 增加
  「观测起点时钟」参数,避免「页面打开时已在跑的任务」被当成新工作。
- 删除 src/jobs-routes.ts(+ tests/jobs-routes.spec.ts)、index.ts 的三条路由、
  api.jobsList/jobOutput/jobKill 与 JobOutputResult、context-types 的
  SidebarJobsService(换成 SidebarClientJobsService/SidebarJobsSnapshot/
  SidebarObservedJob 镜像,SidebarJobView 补 owner/progress)、
  页面里的 jobs.list 轮询与 collectTreeJobs。
- 三个 spec 改用假 ctx.jobs 服务(jobs-view 14 例 / 自动激活 12 例 /
  页面 20 例):观测只在展开行、切换与关闭释放观测、watch 隐藏时不建立、
  卸载时全部释放、无服务时不渲染任务面。

验证:typecheck / lint 通过;pnpm test 1240 passed / 9 skipped;pnpm build + pack 通过。

(cherry picked from commit 7d9b82e73190218f05910433ed520cb90e4d4ab2)
This commit is contained in:
Menghuan1918 committed 2026-09-27 22:18:35 +08:00
1 parent f584b67813
commit 48294d336e
16 files changed
+763 -1050

No files matched your search

+50 -12
View File
@@ -12,7 +12,8 @@ import { renderRoot } from './test-utils.ts'
import { SubagentView } from '../src/client/SubagentView.tsx'
import type {
Context,
SidebarJobView,
SidebarClientJobsService,
SidebarJobsSnapshot,
SidebarProjectionSnapshot,
SidebarSessionList,
SidebarSessionSummary,
@@ -44,6 +45,41 @@ interface NavigationSpy {
opened: Array<SidebarSubagentAddress | string>
}
/**
* A structurally faithful double of the host's client jobs service: a
* subscribable snapshot, a reference-counted `watchRows`, an `observe` that
* records its calls, and a `kill` that records its arguments.
*/
function makeJobsService() {
let snapshot: SidebarJobsSnapshot = { rows: {}, observed: {} }
const listeners = new Set<() => void>()
const observations: Array<{ sessionId: string | undefined; jobId: string }> = []
const kills: Array<{ sessionId: string; jobId: string }> = []
const service: SidebarClientJobsService = {
state: {
getSnapshot: () => snapshot,
subscribe: (listener: () => void) => {
listeners.add(listener)
return () => { listeners.delete(listener) }
},
},
watchRows: () => () => {},
observe: (sessionId: string | undefined, jobId: string) => {
observations.push({ sessionId, jobId })
return () => {}
},
kill: async (sessionId: string, jobId: string) => { kills.push({ sessionId, jobId }) },
}
const emit = (next: Partial<SidebarJobsSnapshot>): void => {
snapshot = { ...snapshot, ...next }
for (const listener of [...listeners]) listener()
}
return { service, emit, observations, kills }
}
/** The jobs service the page reads (rebuilt per test in beforeEach). */
let jobsFake: ReturnType<typeof makeJobsService>
/** The client context face SubagentView touches. */
function makeCtx(store: Store, navigation?: NavigationSpy): Context {
return {
@@ -51,9 +87,13 @@ function makeCtx(store: Store, navigation?: NavigationSpy): Context {
list: store,
refreshProjections: async () => {},
},
get: (name: string) => (name === 'uiWorkspace' && navigation !== undefined
? { openSession: (target: SidebarSubagentAddress | string) => { navigation.opened.push(target) } }
: undefined),
get: (name: string) => {
if (name === 'jobs') return jobsFake.service
if (name === 'uiWorkspace' && navigation !== undefined) {
return { openSession: (target: SidebarSubagentAddress | string) => { navigation.opened.push(target) } }
}
return undefined
},
} as unknown as Context
}
@@ -64,8 +104,6 @@ function jsonResponse(value: unknown): Response {
let teamPayload: unknown = { available: false }
const teamMutations: Array<{ method: string; body: Record<string, unknown> }> = []
const fetchedMethods: string[] = []
/** Job lists the stubbed `jobs.list` route answers with, keyed by OWNER session. */
let jobsByOwner: Record<string, SidebarJobView[]> = {}
/** The `subagents.live` payload the stub answers with (session id → live view). */
let livePayload: Record<string, unknown> = {}
/** The folded workflow runs the stub answers `workflows.list` with. */
@@ -77,9 +115,12 @@ beforeEach(() => {
runsPayload = []
teamMutations.length = 0
fetchedMethods.length = 0
jobsByOwner = {
root: [{ id: 'bash-1', kind: 'bash', label: 'sleep 300', status: 'running', startedAt: 1_000 }],
}
jobsFake = makeJobsService()
jobsFake.emit({
rows: {
root: [{ id: 'bash-1', kind: 'bash', label: 'sleep 300', status: 'running', startedAt: 1_000 }],
},
})
vi.stubGlobal('fetch', async (url: string | URL | Request, init?: RequestInit) => {
const method = String(url).split('/').pop()
fetchedMethods.push(method ?? '')
@@ -87,9 +128,6 @@ beforeEach(() => {
if (method === 'subagents.live') return jsonResponse({ ok: true, value: { live: livePayload } })
if (method === 'workflows.list') return jsonResponse({ ok: true, value: { runs: runsPayload } })
if (method === 'teams.view') return jsonResponse({ ok: true, value: teamPayload })
if (method === 'jobs.list') {
return jsonResponse({ ok: true, value: { jobs: jobsByOwner[String(body.sessionId ?? '')] ?? [] } })
}
if (method === 'teams.taskCreate' || method === 'teams.taskUpdate') {
teamMutations.push({ method: method ?? '', body })
return jsonResponse({