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

+19 -31
View File
@@ -9,7 +9,6 @@
* status mapping mirror the official ui-jobs header list.
*/
import type {
SidebarSessionList,
SidebarJobStatus,
SidebarJobView,
} from '../context-types.ts'
@@ -35,42 +34,31 @@ export { treeSessionIds }
/**
* Whether a NEW background job appeared for the current session between two
* consecutive reads of its job list (a job id the previous list lacked).
* Unlike the subagent auto-open (0 → N only), ANY new job id triggers: the
* agent may start several jobs over a session, and each new one should
* surface the Tasks page containing the background-jobs section. A fresh page
* load never triggers — the caller builds its baseline from the first list it
* reads instead of comparing against one (see the poller in
* sidebar/use-host-feeds.ts).
* roster frames (a job id the previous frame lacked). Unlike the subagent
* auto-open (0 → N only), ANY new job id triggers: the agent may start
* several jobs over a session, and each new one should surface the Tasks page
* that contains the background-jobs section.
*
* `since` (epoch ms) is the moment the caller STARTED watching the roster, and
* it is what keeps a page load honest now that the roster arrives as a push
* stream: the host's client jobs model drops a session's key when it sees no
* jobs, so an empty first frame is indistinguishable from "not delivered yet".
* A job that already existed when the watcher opened therefore looks brand new
* to an id-set diff — comparing its start time against `since` filters it out,
* while a job the agent starts afterwards still triggers.
*
* @param prev - the previous frame's rows.
* @param next - the current frame's rows.
* @param since - epoch ms the watch began; omitted skips the age test.
* @returns whether genuinely new work appeared.
*/
export function detectNewJob(
prev: readonly SidebarJobView[],
next: readonly SidebarJobView[],
since?: number,
): boolean {
const prevIds = new Set(prev.map(job => job.id))
return next.some(job => !prevIds.has(job.id))
}
/**
* Collect the background jobs of the whole current tree, owner-labeled. The
* map is keyed by the OWNER session (the fence's unit of access): sessions the
* caller could not read, or that returned nothing, contribute nothing — an
* absent entry is an empty set, never a dropped row of another tree.
*/
export function collectTreeJobs(
byId: SidebarSessionList['byId'],
jobsBySession: Readonly<Record<string, readonly SidebarJobView[]>> | undefined,
rootId: string | undefined,
): TreeJob[] {
const rows: TreeJob[] = []
if (jobsBySession === undefined) return rows
for (const sessionId of treeSessionIds(byId, rootId)) {
const jobs = jobsBySession[sessionId]
if (jobs === undefined || jobs.length === 0) continue
const ownerTitle = byId[sessionId]?.displayTitle ?? sessionId
for (const job of jobs) rows.push({ ownerSessionId: sessionId, ownerTitle, job })
}
return rows
return next.some(job => !prevIds.has(job.id) && (since === undefined || job.startedAt >= since))
}
/**