Skip to content

feat(v2): 协作任务各操作按 #1376 规则表授权,待验收可退回待处理 - #433

Merged
EricWvi merged 5 commits into
ora-space:mainfrom
zhangxinhao:feat/1383-task-action-permissions
Nov 23, 2026
Merged

EricWvi merged 5 commits into
ora-space:mainfrom
zhangxinhao:feat/1383-task-action-permissions

Conversation

@zhangxinhao

Copy link
Copy Markdown
Contributor

Refs ora-space/circus#1383

v2,不涉及 specs。

按决策 circus#1376 的 owner 结论(选项 A 加规则表)落地 circus#1035 的第 2 条:协作任务各项操作由谁来做。服务端判定,不允许时返回 403 forbidden。前端按同一套规则禁用按钮,并写明谁能做,写法沿用确认完成(cloud#339)。

规则

操作 谁能做 状态限制
编辑 创建人、owner、当前负责人 已结束只读(409 task_closed)
改派 创建人、owner、当前负责人(按改派前的负责人算) 已结束不能改(task_closed);Agent 这一轮在执行时不能改(409 run_active)
拆分 创建人、owner、当前负责人 只在待处理、进行中;待验收返回 409 task_in_review
停止运行 运行发起人、创建人、owner 有活动运行
退回待处理(新增) 创建人、owner 待验收,且没有运行在执行
  • 「当前负责人」:人的任务是负责人本人,小队任务是小队的现任队员。Agent 任务没有人类负责人,编辑、改派、拆分只给创建人和 owner;用 Agent 处理、重新执行、追问仍是任何成员都能做(AuthorizeTaskRunner 不变)。
  • 待验收时负责人仍可追问、重新执行(选项 A),这部分不用改。

改了什么

  • 服务端
    • 规则都写在 store/task_access.go:CanManageTask、CanStopTaskRun,以及已有的 canCloseTask。
    • UpdateTask、SetTaskAssignee 改成接收调用方的 Access,在锁住任务的同一事务里判定。
    • CanTransitionTask 加上 待验收 → 待处理,由 canCloseTask 把关。CanRunTransitionTask 不变。
    • 拆分(taskdecomp.Start)和停止(taskrun.Host.Stop)各自补上判定。
  • 前端
    • task-views.ts 新增 canManageTask、manageRefusal、canStopRun、stopRefusal、needsCloser,和服务端一一对应。
    • 任务页:编辑、改派、拆分、停止不允许时禁用,并写明谁能做。待验收多了「退回待处理」,看板拖回待处理列也走同一判断。
    • 创建人或 owner 不能用 Agent 处理别人的任务,但运行时可以在「执行」一节停止它。
    • 新错误码都有对应文案。

取舍

  • 改派只拦 Agent 执行中,不拦工作流处理中。 工作流处理期间改派是已有的、测过的行为(task_workflows_edits_test.go),工作流结束时按处理链接结算,不受负责人变化影响。界面原来也只在 Agent 执行时禁用改派。
  • 停止只管任务的 Agent 运行。 工作流运行的停止(workflow-runs/.../stop、任务页「工作流」标签里的停止)不在 #1376 的规则表里,这次不动。
  • 退回待处理不另发通知。 退回后任务回到负责人的「待我处理」(cloud#431 的口径),负责人在那里就能看到。
  • #1376 补充的「我参与的」。 前端没有叫「我参与的」的视图。最接近的是「我的任务」(指派给我和我所在小队的任务),它本来就不按状态过滤,负责人跑完一轮进入待验收的任务一直列在里面。所以这里只改了提示文字,写明包括跑完交给创建人验收的任务。如果 owner 指的是另一个视图,请在运行 issue 里说。

验证

  • go test -race 跑了 store、server、taskdecomp、taskrun、workflowhost、taskworkflow,全部通过。
    • 新增 TestTaskManageNeedsCreatorOwnerOrAssignee、TestTaskSendBackNeedsCreatorOrOwner、TestDecompositionNeedsCreatorOwnerOrAssignee,以及停止和待验收拆分的断言。
    • 去掉判定后,这些新断言都会失败。
    • 原来的停止测试由另一个成员停止 owner 发起的运行,现在改成由创建人停止成员发起的运行,仍然覆盖「停止的人不是发起人」。
  • vitest:collaboration 和 squads 下 12 个文件,134 条全部通过。typecheck、lint 通过。
  • 界面:本机栈,owner、Alice、Bob 三个账号。owner 建了任务派给 Alice,Alice 推到待验收。
    • 经 HTTP:Bob 编辑、改派,Alice 退回待处理,都返回 403 forbidden。
    • 页面上:Bob 的编辑、改派、确认完成、退回、取消都禁用,各自写明谁能做。Alice 能编辑,但不能退回。owner 点「退回待处理」后,任务回到待处理。

… task

- Edit, reassign and split: the creator, the workspace owner and whoever
  does the work now (the assignee, or a member of the assigned squad).
  An Agent task has no person doing it, so only its creator and the owner
  manage it. A finished task is read-only (409 task_closed).
- Reassigning waits for a running Agent turn (409 run_active).
- Splitting only applies to a task still being worked; a task in review
  answers 409 task_in_review.
- Stopping a run: whoever started it, the creator and the owner.
- The creator and the owner may send a task in review back to pending
  while no run executes it.

Anyone else gets 403 forbidden.
The task page follows the server's rules: editing, reassigning and
splitting name the creator, assignee or squad; stopping names the run's
starter and the creator; the creator gets 退回待处理 in review, also on
the board. A creator or owner who cannot run the task's Agent can still
stop its run. 「我的任务」 says it keeps work handed in for review.

@wanglongan587 wanglongan587 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审查版本:ora-space/cloud#433 head 7e1dbab81fe02cd1a7df0e365dc736528b2af4ef(base upstream/main c3b62695a5c8f016dfa0dc3249cd47a32df9b9e6),对照决策 ora-space/circus#1376 与父事项 ora-space/circus#1035。
审查方式:静态代码与契约核对 + 运行 #1383 界面截图人工检视(PR CI server、frontend、e2e 在 7e1dbab8 全绿;本轮未在本地重跑测试)。

阻塞项(需修改后重新送审)

  1. v2-server/tasks.openapi.json 漏了 stopTaskRun 的 403 响应声明(Medium)

    • 位置:v2-server/tasks.openapi.json(POST /api/v2/tenants/{tid}/tasks/{taskid}/runs/{rid}/stop,L952–L1030)、v2-server/internal/server/task_runs.go:115-116,186-197。
    • 问题:taskrun.Host.Stop 新增返回 store.ErrForbidden 并经 taskRunFault 映射为 403 forbidden,但 tasks.openapi.json 的 stopTaskRun 仍只声明了 200/401/404/409/500/503,缺 403(其余几个被加权的接口都有 403)。server/tasks_test.go 的 taskClient 会用 openapi3filter.ValidateResponse 按该文件校验 HTTP 响应,经 HTTP 测试旁观者停止运行返回 403 会直接在契约校验阶段失败。
    • 建议:在 v2-server/tasks.openapi.json 的 stopTaskRun 补上 403(TaskError),如前端生成的客户端有变化一并同步,并在 v2-server/internal/server/task_runs_test.go 补一条经 taskClient 的旁观者停止 403 forbidden HTTP 测试。
  2. 前端 TaskFamily 的「重新拆分」按钮漏了 canDecompose 和 canManage 门禁(Medium)

    • 位置:frontend-v2/src/features/collaboration/task-page.tsx:252-253,265、frontend-v2/src/features/collaboration/task-decomposition.tsx:101,116,131-143。
    • 问题:顶栏「拆分」按钮已改为仅在 canDecompose(task) 时显示,且 !canManage 时禁用并给出 manageRefusal(task, '拆分')。但正文下方 TaskFamily → DecompositionStatus 在上一次拆分失败(latest.status === 'failed')时仍无条件渲染可点击的「重新拆分」按钮(L141)。这导致:
      • 旁观者(!canManage)在拆分失败提示里仍能点开弹窗提交拆分,直到服务端返回 403;
      • 任务进入 待验收(review)或 已结束(done/cancelled)后,顶栏「拆分」已隐藏,但「重新拆分」仍可点开提交,直到服务端返回 409 task_in_review / task_closed。
    • 建议:把 canDecompose(task)、canManage 和 manageRefusal(task, '拆分') 传给 TaskFamily / DecompositionStatus:非 canDecompose(task) 时不渲染「重新拆分」,!canManage 时禁用并加上 title={manageRefusal(task, '拆分')},并在 task-page.test.tsx 补对应单测。
  3. taskdecomp.Host.Start 在 h.lock(taskID) 之前读取 task 并判权,锁内未重读(Medium)

    • 位置:v2-server/internal/taskdecomp/taskdecomp.go:133-159。
    • 问题:taskrun.Host.Start(taskrun.go:265)、taskrun.Host.Stop(taskrun.go:430)与 taskworkflow.Host.Start(taskworkflow/host.go:61)都是在获取 h.lock(taskID) 之后再读 task 并判权;而 taskdecomp.Host.Start 在 L135 读 task、L139 调 store.CanManageTask,L145 才加锁且锁内不重读 task。若请求在 h.lock(taskID) 排队等待前一次操作,后续锁内的 TaskFinished/TaskReview 校验和 input 拼装都会继续使用锁前旧快照。
    • 建议:将 defer h.lock(taskID)() 挪到 h.store.Task(ctx, tenant, taskID) 之前(跨路由 CreateTaskDecomposition 未锁 v2_tasks 行的小窗口在 phase1 可接受,无需扩改事务边界)。

非阻塞建议

  1. taskrun.Host.Stop 测试覆盖边界与陈旧注释(Low)
    • v2-server/internal/taskrun/taskrun_test.go:416-447 的 TestStopEndsTheRunAndReturnsTheTask 用 f.owner 测停止,但 f.owner 同时是 RoleOwner 和任务创建人(删掉 task.CreatedBy == a.UserID 条件该用例也能过)。建议补全三个独立身份的测试:非 owner 的创建人、非创建人的 owner、以及非创建人/非 owner 的运行发起人本人。
    • v2-server/internal/taskrun/taskrun_test.go:716 残留旧注释 // Anyone may still stop the run: it spends nobody's identity.,建议一并更新。

已核对通过的重点项(回应作者指定审查内容)

  • taskrun.Host.Stop 逻辑:在 h.lock(taskID) 内重读 task、run 与最新 TenantAccess,并在 !run.Live() 与 seen 幂等判断之前校验 CanStopTaskRun(旁观者无法绕过),停止时仍以 run.StartedBy scope 调执行服务——逻辑严密。
  • taskdecomp.Host.Start 鉴权与幂等顺序:CanManageTask 在 TaskDecompositionByKey 之前、TaskDecompositionByKey 在 TaskFinished/TaskReview 之前,与 taskworkflow.Host.Start 口径一致;ArchiveSquad 会写 ended_at,已归档小队与已移除队员都能被 InSquad 拦住。
  • 改派在 Agent 执行中返回 run_active、在工作流处理中仍放行的取舍:接受。
    • 与 circus#1376 原文及结论(「服务端补上前端已有的限制」)一致;
    • 核对 store/tasks.go:254-258、store/task_workflows.go 及 store/task_workflows_edits_test.go:改派不会递增 v2_task_workflow_runs.status_revision,工作流结算的 status_revision = 1 栅栏不受影响;工作流处理中时,CreateTaskRun / CreateTaskWorkflowRun 都会检查运行中的 v2_task_workflow_runs、SetTaskStatus 返回 409 workflow_active,新负责人无法在工作流结束前并发启动或手动改状态;
    • Agent idle 会话期间改派后,原发起人仍可通过 !runsAgent && task.activeRun 区块结束自己的空闲会话,新负责人「重新执行」也会以 TaskRunReplaced 结束旧会话。
  • 截图核对:已下载并检视 #1383 所附两张截图(1383-bystander-bob.png 与 1383-creator-owner.png):旁观者 Bob 视角下编辑/改派/确认完成/退回待处理/取消均已禁用并写明原因,owner 创建人视角下待验收状态可点「退回待处理」并显示返工提示,与说明一致(两张截图未涵盖拆分失败与运行中停止区块,改完以上阻塞项后请按 v2 规则附新 commit 截图重新送审)。

…he task lock

- tasks.openapi.json: stopTaskRun answers 403 forbidden to anyone but the
  run's starter, the task's creator and the workspace owner; an HTTP test
  stops as a bystander through the contract-checking client.
- taskdecomp.Start reads and judges the task after taking its lock, like
  the run and workflow hosts, so a start that waited sees the task as the
  previous one left it.
- Stop tests cover the starter, a creator who is not the owner and an
  owner who did not create the task, each on their own standing.
The failed-split notice follows the header's 拆分 rule: no retry once the
task is in review or finished, and a disabled retry naming who can for a
member who may not split it. The tasks client picks up the stop route's
description.

@wanglongan587 wanglongan587 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上一轮的三个阻塞项都已改好,剩 v2-server/README.md 还写着旧规则

审过 903199fb7705f19eec4d85fc9be79e3b415fd373(在 7e1dbab8 之上两个 commit)。规则本身、两处新增判定的位置、以及 run_active 只拦 Agent 执行中这个取舍,都与 #1376 的 owner 结论一致,不用再改。剩下的是一条文档:这次改动让 v2-server/README.md 里几句关于任务权限的话直接变成假的,而 v2-server/AGENTS.md 要求把 v2 的行为写进这份 README。

已复核通过

  1. stopTaskRun 补 403:tasks.openapi.json 的该路由现在声明 403(TaskError),描述写明谁能停;TestTaskRunHTTP 用独立的 taskClient 以旁观者身份走契约校验拿到 403 forbidden,再以发起人身份停下。code 是自由字符串、403 也是新加的状态码,不会破坏既有断言。
  2. 「重新拆分」按顶栏规则门禁:TaskFamily 的 onRetry 只在 canDecompose(task) 时传入,retryRefusal 用 manageRefusal(task, '拆分'),待验收/已结束时按钮整体不渲染,无权限时禁用并在 title 写明谁能拆。
  3. taskdecomp.Host.Start 先加锁再读任务:defer h.lock(taskID)() 移到 h.store.Task 之前,锁内读到的 task 同时用于判权和后续 ErrTaskClosed/ErrTaskInReview,与 taskrun.Host.Start/Stop、taskworkflow.Host.Start 一致。
  4. 建议 4:TestStopIsForTheStarterTheCreatorAndTheOwner 覆盖发起人本人、非 owner 的创建人、非创建人的 owner 三种各自成立的身份;taskrun_test.go 的旧注释也已改成「创建人虽然不能发起,但能停」。

我本地复核的证据:

  • npm ci 后在 frontend-v2 跑 vitest run src/features/collaboration:7 个文件 102 条通过。对新用例做了两次变异——把 onRetry 换回无条件传入、把 retryRefusal 恒设为 ''——两次都只有 offers to split again after a failure only while the caller may split the task 失败,说明它确实守着这两个条件,不是空断言。
  • Go 侧本机没有可用的 PostgreSQL(TEST_DATABASE_URL 未设、55432 拒绝连接、无 docker),所以没有本地跑;以 CI 在同一个 head 上的 server(Linux + Postgres 17.11,go test -race -count=1 ./...)、frontend(npm run check,含 api:check:tasks)、e2e(8m55s)三个 job 全绿为准,另 gofmt/go vet 也在该 job 内。
  • 截图:1383-bystander-bob-903199f.png 里 Bob 的「编辑」「确认完成」「退回待处理」「取消任务」都是禁用态(与 1383-creator-sent-back-903199f.png 的黑色可用态对比明显),指派下拉是禁用态,两处文案各自写明谁能做;owner 退回后状态回到待处理、顶栏「拆分」重新出现、编辑 恢复可用,和说明一致。

阻塞项:README 与新规则相反

这几句现在与代码矛盾(行号按 903199fb):

  1. 281 路由表 stop:「任何成员都可以停」→ 现在是运行发起人、任务创建人、工作区 owner,其他人 403 forbidden。
  2. 305:「当前工作区任意成员都可读写、指派与推进任务」→ 编辑、改派只给创建人、owner、当前负责人;确认完成、取消、退回待处理只给创建人和 owner。
  3. 324(派给人的任务):「别人仍能看过程、停止」→ 别人能看,但停不了,除非是创建人或 owner。
  4. 297、302:生命周期漏了 review → pending;「后两种人不能手动做」也不再准确——运行造成的回退人不能手动做,但创建人/owner 可以手动把待验收退回待处理。

同一节里顺带补齐(这几处本来只是没写全,一起补省一轮):

  • 274(PUT tasks):补谁能改(创建人、owner、当前负责人)与已结束 409 task_closed。
  • 275(status):确认完成、取消之外,退回待处理也只限创建人和 owner。
  • 277(assignment):补谁能改派(按改派前的负责人算)、已结束 409 task_closed、Agent 执行中 409 run_active。
  • 453(任务拆分):补谁能拆(创建人、owner、当前负责人;其他人 403 forbidden)与待验收 409 task_in_review。

非阻塞

  • taskdecomp.Host.Start 的判权在 TaskDecompositionByKey 之前:同一次拆分(同一个 key)如果在途期间调用方失去了管理权(任务被改派走、或不再是队员),重试会拿到 403 而不是那次拆分本身。范围很窄,TaskDecompositionByKey 本来就按人查,把它挪到判权之前也说得通,随你判断。
  • #1376 结论里的「我参与的」:你在 issue 里问 EricWvi 的那条还没有回应。我的任务 不按状态过滤、负责人跑完一轮进入待验收的任务确实在里面,实质满足;但这个视图名并不存在,等 EricWvi/owner 给了口径再关 #1035 那一条,这轮先不要当成已确认。

v2-server/README.md and the collaboration README still said any member
may edit, reassign, split and stop. They now state the #1376 rules, the
review → pending send-back, and the 403, task_closed, run_active and
task_in_review answers of each route.

@wanglongan587 wanglongan587 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

审过 commit 2ad85c1e(相对 903199fb 只改文档:v2-server/README.md、frontend-v2/src/features/collaboration/README.md,+21/−12)。上轮的阻塞项已修好,规则与代码一致,同意合并。

复核(对照 2ad85c1e 的代码)

  • 路由表:编辑、状态、改派、停止四条都补上了 403 forbidden,编辑/改派补上 task_closed,改派补上 run_active。与 store/tasks.go 的 requireManage(先 ErrForbidden,再 ErrTaskClosed)、SetTaskAssignee 的 ErrRunActive 一致。
  • 生命周期:review → pending 已补,并写明退回只有人工能做。「人不能手动从 running 进入 review/pending」成立:SettleDeliveredTaskTurn 在同一事务里锁任务行并要求 turn_active,交付期间 ActiveRun 仍是 running,SetTaskStatus 会先返回 409 run_active;CanRunTransitionTask 不含 review → pending。
  • 谁不能改:canCloseTask / CanManageTask / CanStopTaskRun 三个函数与文中四条一一对应;「改派按改派前的负责人算」对得上 requireManage(ctx, tx, a, before)。
  • 拆分:taskdecomp.Start 的判定顺序(CanManageTask → task_closed → task_in_review → subtask_not_decomposable)与新写的状态限制一致。
  • 前端 README:新增的「谁能改任务」与 task-views.ts 的 canManageTask / canStopRun 一致;「重新拆分」的禁用与原因也和 task-page.tsx 对齐。
  • 保留的旧说法是对的:工作流页签里「停止不限发起人」不属于 #1376 规则表,workflowhost.CancelRun 确实只按租户查运行、不校验发起人。

证据

  • gh pr checks 433 在同一 head 上 server、frontend、e2e 全绿(run 45102165912)。
  • 两条 README 的改动只描述已有行为,没有引入与 store/task_access.go、task-views.ts 不一致的规则。

非阻塞(一行措辞,可改可不改)

frontend-v2/src/features/collaboration/README.md 的「创建人和 owner 看不到「用 Agent 处理」」在创建人同时是负责人时不成立:runsAgent 只看指派人,自己建给自己的人仍能开运行。写成「不是负责人的创建人和 owner 看不到」更准确。

@EricWvi
EricWvi merged commit bb53fbe into ora-space:main Nov 23, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants