Skip to content

fix(psa): a task moved out of Completed gives up its 100 % (#2768) - #2771

Draft
sunhaolin wants to merge 3 commits into
mainfrom
claude/fix-2768-reopened-task-percent-30ab3e
Draft

sunhaolin wants to merge 3 commits into
mainfrom
claude/fix-2768-reopened-task-percent-30ab3e

Conversation

@sunhaolin

@sunhaolin sunhaolin commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2768

The problem

Marking a project task Completed writes 100 into its Percent Complete. Moving the task back out of Completed left that 100 in place, so a task nobody had started counted as finished and the project's Percent Complete overstated progress until someone typed the task's percentage back by hand.

The rule chosen (please overrule it if you disagree)

A task moved out of Completed, to any other status, has its Percent Complete set back to 0, unless the same save gives a new percentage.

  • It applies to every status the task can move to: Not Started, In Progress, On Hold and Cancelled. The issue left In Progress / On Hold open; I chose 0 for all of them because the 100 was written by the system and the estimate the task held before it was completed is not stored on the record, so the hook has nothing to restore it from (field history still shows it to a person). One rule for every target status is also the easiest one to explain in the manual.
  • Cancelled is included although a cancelled task is left out of the project figure: otherwise the stale 100 comes back the day the cancelled task is resumed.
  • A percentage given in the same save wins. Reopening with { status: 'in_progress', percent_complete: 70 } keeps 70.
  • "Given" means the payload carries a percent_complete that differs from the stored one. A form that posts every field back sends the old 100 along with the new status; that is not a new estimate, so it is treated as not given. The one case this cannot express is "reopen and deliberately keep 100", which contradicts the status anyway.
  • A task that stays Completed (a rename, a date change) keeps its 100. Moves that do not start from Completed are untouched.
  • Only the 100 the hook wrote is taken back. A Completed task stored at another figure (a row from before PSA phase 2a: project tasks, Gantt, milestones and progress computed from tasks (new crm_project_task) #2545, when Completed did not yet write 100) holds its owner's own number, so it keeps it, also when a full form echoes it back with the new status.
  • A predicate update (multi: true + where) leaves the figure alone (the ADR-0058 Addendum II D3 stand-down, as in forecast.hook.ts and task.hook.ts). One payload serves every matched row, so a reset decided on one row's ctx.previous made the next row see it as given, and the engine refused the batch with MultiUpdateHookKeyDivergenceError. Every writer of a task in this app saves by id, where the reset happens.
  • Rows that were reopened before this change keep the 100 they hold; no data migration is included. Say so if you want one.

The alternative I did not take: remember the pre-completion percentage and restore it. It needs a new field on crm_project_task, and the issue asks only that the 100 does not survive.

What changed

  • src/psa/objects/project_task.hook.ts: project_task_rules (beforeUpdate) writes 0 when the status leaves completed and the save gives no new percentage. project_task_progress is unchanged: it already recomputes the project on every task write, so the project figure follows.
  • src/psa/objects/project_task.object.ts and the four PSA translation files: the Percent Complete help text states both halves of the rule.
  • User docs, in the same change: the in-app manual src/psa/docs/crm_psa_project_plan.md and .zh.md, and the product page content/docs/(product)/projects/project-plan in en, zh-Hans and zh-Hant.
  • .changeset/2768-reopened-task-percent.md (patch).

Tests

test/psa-project-tasks.test.ts gains a block on a booted stack with a two-task project (40 h at 50 % and 60 h at 0 %, project 20 %). Before the fix, three of its cases failed with expected 100 to be 0.

  • Complete the 60 h task: task 100 %, project 80 %. Move it back to Not Started: task 0 %, project 20 %.
  • The same for In Progress and On Hold; for Cancelled the task is 0 % and the project is 50 % (the cancelled task is left out), and resuming it afterwards leaves it at 0 %.
  • Reopening with 70 % in the same save keeps 70 %; the project reads 62 %.
  • A payload that posts the unchanged 100 back with the new status still goes to 0 %.
  • In Progress to On Hold at 50 % stays 50 %, and renaming a Completed task keeps 100 %.
  • A Completed task stored at 80 % (written straight to the driver), reopened by a form that sends 80 back, keeps 80 %. Before the follow-up it went to 0 %.
  • One predicate update moves two Completed tasks to In Progress. Before the follow-up it threw MultiUpdateHookKeyDivergenceError.

Checks

  • pnpm verify (validate, typecheck, lint --strict, hygiene, build, test): green. 332 test files, 6382 tests passed, 58 skipped.
  • After the review follow-up (1d4d8f94, on top of origin/main merged in ecaf7f6d): validate, typecheck, lint --strict, hygiene and build green. The test stage ran twice on a machine at load average 64–117; each run had only Test timed out in 5000ms failures, 6 and 8 tests, in different files outside PSA (campaign, forecast, flow-scheduled, clone, price book, PSA approvals). No assertion failed. Every one of those files passes when rerun alone (7 files, 214 tests, with test/psa-project-tasks.test.ts, 25 tests). CI is the clean run.
  • On a development server with the sample data, signed in as the dev admin, task "Stark pilot: configure site three" (60 planned hours) on project "Stark Medical Pilot Delivery":
    • Project at 36.2 %. Move the task to Completed on the status path: task 100 %, project 59.2 %.
    • Open the edit form, set Status back to Not Started, save: task 0 %, project 36.2 %. The console form sends only the changed field ({"status":"not_started"}).
    • Over the API, Completed to In Progress with percent_complete: 40 in the same request: task 40 %, project 45.4 %.

Metadata-impact: maintainer — a hook changed (project_task_rules now writes Percent Complete 0 when a task leaves Completed)

Marking a project task Completed writes 100 into Percent Complete. Moving
it back out of Completed left the 100 in place, so a task nobody had
started counted as finished and the project's Percent Complete overstated
progress.

project_task_rules now sets Percent Complete back to 0 when the status
leaves Completed, whichever status the task moves to, unless the same
save gives a new percentage. A payload that posts the stored 100 back
with the new status is not a new percentage. The project roll-up already
recomputes on every task write, so the project figure follows.

Tests pin the reopen for every target status, the percentage given in
the same save, and the moves that must stay untouched. The in-app manual,
the product page (en, zh-Hans, zh-Hant) and the field help text state the
rule.
@vercel

vercel Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
hotcrm Ignored Ignored Nov 25, 2026 2:57am UTC

Request Review

@github-actions github-actions Bot added documentation Improvements or additions to documentation ci/cd CI plumbing and the verification pipeline metadata Declarative metadata — schema, security posture, UI surfaces backend Server-side behaviour — hooks, flows, actions labels Nov 24, 2026
@sunhaolin

Copy link
Copy Markdown
Collaborator Author

Seat record · 独立评审 · HOLD · head 975eb1a7 · 2026-11-25

独立评审(只读子代理,xhigh,未参与改动)结论:通过(有 nit)。Metadata-impact 同意 maintainer — hook 变更;「重新打开时取 0 还是恢复原值」本身是产品决定。PR 保持 draft 交维护者合并;本席位不转 ready、不挂 auto-merge。

  • 改了什么:任务从「已完成」改为其他状态、且同一次保存没给出新百分比时,project_task_rules 把完成百分比写为 0(原来只有改为已完成时写 100 这一个方向);四语帮助文字、站点文档 en / zh-Hans / zh-Hant、应用内手册 en / zh、changeset 与测试同步。
  • 为什么改、谁提的:已完成的项目任务改回「未开始」后,完成百分比仍是 100%,项目完成百分比虚高 #2768(本地 UI 测试发现):重新打开的任务仍显示系统写入的 100,项目进度偏高。
  • 最坏情况与能否回退:原本做了一部分的任务重开后显示 0,项目进度偏低,直到有人重填;只回退 hook 即可,无 schema 或数据迁移。已写成的 0 回退后不复原,但对象开了 trackHistory,字段历史可查原值。
  • 评审没有验证的:issue 复现里用阶段条重开,未在浏览器实点;predicate 批量更新的行为从引擎源码推断,未实际运行。

Findings

  1. src/psa/objects/project_task.hook.ts:80-84(低,本 PR,目前不触发):predicate 更新(multi: true + where)一次把两行以上移出已完成时共用一份 payload,第 2 行会把第 1 行写的 0 当成用户给的值,引擎随即抛 MultiUpdateHookKeyDivergenceError,整批不写。目前无入口走到(REST 批量逐 id,无 flow 用 multi 写 crm_project_task)。仓库先例 knowledge_article.hook.ts:148、forecast.hook.ts:66 在非 ctx.dispatch?.mode === 'per-row' 时直接退出,建议照做或至少注释写明。
  2. content/docs/(product)/projects/project-plan.mdx:35 及各语对应段(nit):字段历史里有旧值,"is not kept" 不够准,建议改为「不会自动恢复」。
  3. project_task.hook.ts:82-83(nit):已完成记录存的百分比不是 100(如旧数据)时,完整表单原样回传会被判为「没给出」而写 0;边缘情况,与文档一致。

已核实:导入为 insert 不受影响,按 id upsert 走同一规则;非已完成状态之间切换不变;项目进度随之重算(测试验证 20/80/50/62);四语齐全,文档与手册均已改;与 #2772 共享两个文件,三方合并无冲突、无行为重叠。

- project_task_rules stands down the leaving-Completed reset on a predicate
  update (ADR-0058 Addendum II D3, as in forecast.hook.ts): one shared
  payload made the second row read the first row's 0 as given, and the
  engine refused the batch with MultiUpdateHookKeyDivergenceError.
- Only the 100 the hook wrote is taken back: a Completed task stored at
  another figure (a row from before #2545) keeps it, also when a full form
  echoes it back.
- Docs: the pre-completion estimate is not restored automatically; field
  history still shows it (was "is not kept").

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Server-side behaviour — hooks, flows, actions ci/cd CI plumbing and the verification pipeline documentation Improvements or additions to documentation metadata Declarative metadata — schema, security posture, UI surfaces

Projects

None yet

Development

Successfully merging this pull request may close these issues.

已完成的项目任务改回「未开始」后,完成百分比仍是 100%,项目完成百分比虚高

1 participant