fix(sansheng): 返工预算与本地记录取大值,revise 清零绕不过累计上限

This commit is contained in:
qyh15 2026-11-10 17:52:53 +08:00
parent 372c7e8892
commit d037f1ee57
10 changed files with 47 additions and 16 deletions

View File

@ -44,7 +44,7 @@
## 验证
```sh
cd sansheng && npm test # 55 tests:门禁、指纹、存储、派发回执、返工、状态接口、客户端渲染、provision 流程
cd sansheng && npm test # 56 tests:门禁、指纹、存储、派发回执、返工、状态接口、客户端渲染、provision 流程
node --check sansheng/client.js && node --check sansheng/index.js
```

View File

@ -15,7 +15,7 @@
| `sansheng/src/gates.js` | 根 `ctx.tools.guard`,拦截 `group_goal` 变更 |
| `sansheng/src/digest.js` | 目标/阶段指纹,证据随成果版本失效 |
| `sansheng/src/tools.js` | `sansheng_provision` / `sansheng_bind` / `sansheng_status` / `sansheng_review` |
| `sansheng/test/*.test.js` | 22 个纯逻辑测试(P2–P5 后共 55 个,含 provision 流程与客户端渲染) |
| `sansheng/test/*.test.js` | 22 个纯逻辑测试(P2–P5 后共 56 个,含 provision 流程与客户端渲染) |
## 角色与门禁(P1 已生效)

View File

@ -1,12 +1,12 @@
# P2–P4 · 门禁之后的证据、返工与恢复
状态:**代码与单测完成(55 项),宿主侧已在临时实例上跑通 `/sansheng/state`;桌面端需重启后生效。**
状态:**代码与单测完成(56 项),宿主侧已在临时实例上跑通 `/sansheng/state`;桌面端需重启后生效。**
## P2 · 完整开发闭环:门禁 + 派发回执
| 文件 | 作用 |
| --- | --- |
| `sansheng/src/gate.js` | 纯函数:角色、证据版本、返工预算、完成条件 |
| `sansheng/src/gate.js` | 纯函数:角色、证据版本、返工预算、完成条件。预算取「原生阶段 reworks 累计」与「本地返工记录数」的较大值,`revise` 重建阶段清零点数也绕不过去 |
| `sansheng/src/gates.js` | 根 `ctx.tools.guard`,只拦 `group_goal` 的 10 个变更动作 |
| `sansheng/src/digest.js` | 目标/阶段指纹,成果或目标一变,旧审查记录立即失效 |
| `sansheng/src/identity.js` | 由执行会话反查真实调用者;角色按**绑定项目**重算 |
@ -41,7 +41,7 @@
## 测试
```sh
cd sansheng && npm test # 55 tests
cd sansheng && npm test # 56 tests
```
覆盖:角色授权、未绑定放行、跨项目越权、版本过期、重复事件、返工预算、自动派单、重启对账、状态接口、客户端注册。

View File

@ -124,7 +124,7 @@ export function buildStatePayload({ store, dsbot, now = Date.now() }) {
const projects = Object.values(state.projects ?? {}).map(project => {
const room = dsbot.roomOf(project.roomId)
const goal = room?.goal
const budget = budgetOf(goal, project)
const budget = budgetOf(goal, project, state.reworks)
const unknown = store.unknownDispatches(project.id).length
const dispatches = store.dispatchesFor(project.id, 3)
const goalReviews = store.reviewsFor({ projectId: project.id, kind: 'goal' })

View File

@ -23,9 +23,14 @@ function findReview(reviews, match) {
return undefined
}
/** 返工预算直接取原生目标里的阶段累计,revise/换阶段都不清零。 */
export function budgetOf(goal, project) {
const used = (goal?.stages ?? []).reduce((sum, stage) => sum + (Number(stage.reworks) || 0), 0)
/**
* 返工预算:原生阶段里的 reworks 累计是权威,revise 重建阶段会把它清零,
* 所以再与本地返工记录数取较大值,保证"按整个目标累计"不被改写目标绕过。
*/
export function budgetOf(goal, project, recorded = []) {
const native = (goal?.stages ?? []).reduce((sum, stage) => sum + (Number(stage.reworks) || 0), 0)
const logged = (recorded ?? []).filter(record => goal === undefined || record?.goalId === goal.id).length
const used = Math.max(native, logged)
const max = Number.isInteger(project?.maxReworks) && project.maxReworks > 0 ? project.maxReworks : 3
return { used, max, left: Math.max(0, max - used) }
}
@ -34,7 +39,7 @@ export function budgetOf(goal, project) {
* 纯函数门禁:尚书是唯一目标操作者;阶段通过/驳回要自己的审查记录,
* 目标完成要丞相的验收记录;返工受预算限制;工部与丞相不得改目标。
*/
export function evaluateGoalAction({ action, actor, project, goal, reviews, members, args }) {
export function evaluateGoalAction({ action, actor, project, goal, reviews, members, args, reworks = [] }) {
if (!project) return { allow: true }
if (!GOAL_MUTATIONS.has(action)) return { allow: true }
// 用户从页面/会话直接操作时放行;Bot 才需要角色约束。
@ -100,7 +105,7 @@ export function evaluateGoalAction({ action, actor, project, goal, reviews, memb
if (action === 'rework') {
const stageIndex = goal.current + 1
const budget = budgetOf(goal, project)
const budget = budgetOf(goal, project, reworks)
if (budget.used >= budget.max) {
return deny('budget', `返工预算已用尽(${budget.used}/${budget.max})。请与用户确认后用 revise 调整目标,或 stop 结束。`)
}

View File

@ -38,6 +38,7 @@ export function installGates({ ctx, store, dsbot }) {
project,
goal: room?.goal,
reviews: store.peek().reviews,
reworks: store.peek().reworks,
members,
args: exec.arguments ?? {},
})

View File

@ -92,7 +92,7 @@ async function settle({ ctx, store, dsbot, project, record, action, result, befo
const detected = detectRework({ before, after, reason: record.reason })
if (!detected) return
const budget = budgetOf(after, project)
const budget = budgetOf(after, project, store.peek().reworks)
const rework = {
id: newId('rwk'),
projectId: project.id,

View File

@ -65,6 +65,6 @@ export function describeDispatch(record) {
return `${record.id} ${record.action}${stage} → ${status}`
}
export function budgetAfter(goal, project) {
return budgetOf(goal, project)
export function budgetAfter(goal, project, recorded = []) {
return budgetOf(goal, project, recorded)
}

View File

@ -77,7 +77,7 @@ function projectLines(dsbot, store, project) {
lines.push(' 目标:还没有(尚书用 group_goal draft/start 起草)')
return lines
}
const budget = budgetOf(goal, project)
const budget = budgetOf(goal, project, store.peek().reworks)
const stage = goal.stages?.[goal.current]
const passed = (goal.stages ?? []).filter(item => item.status === 'passed').length
lines.push(` 目标:${goal.status} rev ${goal.revision} · 阶段 ${passed}/${goal.stages?.length ?? 0}${stage ? ` · 当前「${stage.title}」` : ''} · 返工 ${budget.used}/${budget.max}`)
@ -361,7 +361,7 @@ async function review({ store, dsbot, args, exec }) {
if (record.verdict === 'pass') {
return `已记录尚书阶段审查 ${record.id}:阶段 ${stageIndex}「${stage.title}」通过,目标版本 ${goal.revision}。现在可以 group_goal pass(evidence 用同一份证据)。`
}
const budget = budgetOf(goal, project)
const budget = budgetOf(goal, project, store.peek().reworks)
return `已记录尚书阶段审查 ${record.id}:阶段 ${stageIndex}「${stage.title}」驳回,目标版本 ${goal.revision}。可以 group_goal rework;返工将计为 ${budget.used + 1}/${budget.max}。`
}

View File

@ -166,6 +166,31 @@ test('驳回需要尚书 reject 记录,且受返工预算限制', () => {
assert.equal(spent.code, 'budget')
})
test('revise 重建阶段把 reworks 清零后,本地返工记录仍占预算', () => {
const goal = makeGoal()
// 模拟 group_goal revise 之后:新阶段 reworks 归零,但已经返工过 3 轮。
goal.stages[0].reworks = 0
const recorded = [
{ projectId: project.id, goalId: goal.id, stageIndex: 1 },
{ projectId: project.id, goalId: goal.id, stageIndex: 1 },
{ projectId: project.id, goalId: goal.id, stageIndex: 1 },
]
assert.deepEqual(budgetOf(goal, project, recorded), { used: 3, max: 3, left: 0 })
const spent = decide({
action: 'rework',
goal,
reviews: [stageReview(goal, 1, 'reject')],
reworks: recorded,
args: { reason: '第四轮' },
})
assert.equal(spent.allow, false)
assert.equal(spent.code, 'budget')
const otherGoal = [{ projectId: project.id, goalId: 'goal-other', stageIndex: 1 }]
assert.equal(budgetOf(goal, project, otherGoal).used, 0, '别的目标的返工不算进本目标')
})
test('完成目标需要丞相 goal_pass 记录', () => {
const goal = makeGoal()
goal.current = 2