dsh-sansheng-liubu/sansheng/src/gates.js

106 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { captureArtifactSnapshotSync } from './artifact.js'
import { evaluateGoalAction, GOAL_MUTATIONS } from './gate.js'
import { resolveActor } from './identity.js'
import { roleOfBot } from './roles.js'
/**
* 根级 tools.guard:原生 group_goal 执行前同步判定。
*
* 与旧实现的区别:
* - 群解析使用与 DS Bot 原生相同的规范化(去引号、Main 可见全部群);
* - 解析失败时,若调用者属于某个项目,拒绝而不是放行(F01);
* - 先确认调用者是可解析的 Bot 身份;会话过期/状态损坏返回 unknown,绝不按用户放行;
* - 项目状态不可读(registry 能证明该群受治理)时停止写操作,避免"空状态=不受治理"。
*/
export function installGates({ ctx, store, dsbot, isBotSession }) {
return ctx.tools.guard(exec => {
try {
if (!exec || exec.name !== 'group_goal') return undefined
const action = String(exec.arguments?.action ?? '')
if (!GOAL_MUTATIONS.has(action)) return undefined
const actor = resolveActor({ dsbot, store, sessionId: exec.agent?.id, isBotSession })
if (actor.kind === 'unknown') {
return `三省六部:${actor.reason}(action=${action})`
}
if (actor.kind !== 'bot') return undefined
if (actor.projectConflict) {
return `三省六部:Bot「${actor.botName ?? actor.botId}」同时属于多个项目,拒绝修改目标;请先清理绑定。`
}
let roomId = dsbot.roomOfSession(exec.agent?.id)
if (!roomId) {
const ref = String(exec.arguments?.group ?? '').trim()
if (ref === '') {
return actor.project
? '三省六部:无法确定当前群属于哪个项目(没有群上下文也没有 group 参数),本次目标修改未执行。'
: undefined
}
const picked = dsbot.pickRoom(actor.botId, ref)
if (picked.error !== undefined) {
// 项目 Bot 指名一个解析不到的群:宁可拒绝,也不让原生命令落到未治理群或别的项目。
if (actor.project || store.registryProjectForBot?.(actor.botId)) {
return `三省六部:无法解析群「${ref}」到当前项目(${picked.error}),本次目标修改未执行。`
}
return undefined
}
roomId = picked.room.id
}
const health = typeof store.health === 'function' ? store.health() : { status: 'ok' }
// Q02:主状态与 registry 双故障时门禁无法判断该群是否受治理,直接拒绝。
if (health.status !== 'ok' && health.registryStatus === 'broken') {
return '三省六部:主状态与治理 registry 均不可读,无法判定该群是否受治理;本次目标修改未执行。'
}
let project = store.projectByRoom(roomId)
if (!project) {
const hinted = store.registryProjectForRoom?.(roomId)
if (hinted) {
project = { id: hinted, name: '(治理状态不可读)', degraded: true, bots: {}, maxReworks: 3 }
}
}
if (!project) return undefined
const room = dsbot.roomOf(roomId)
const members = (room?.members ?? []).map(id => ({
id,
name: dsbot.botOf(id)?.name ?? id,
role: roleOfBot(project, id),
}))
let artifact
if (project.workDir && (action === 'pass' || action === 'rework' || action === 'complete')) {
try {
artifact = captureArtifactSnapshotSync({
project,
goal: room?.goal,
stageIndex: action === 'complete' ? null : (room?.goal?.current ?? -1) + 1,
scope: action === 'complete' ? 'goal' : 'stage',
})
} catch (error) {
return `三省六部:无法生成当前成果快照(${error?.message ?? error}),本次目标修改未执行。`
}
}
const verdict = evaluateGoalAction({
action,
actor: { ...actor, role: roleOfBot(project, actor.botId) },
project,
goal: room?.goal,
reviews: store.peek().reviews,
reworks: store.peek().reworks,
members,
args: exec.arguments ?? {},
artifact,
projectFreshness: health.status,
})
if (!verdict.allow) return `三省六部:${verdict.reason}`
// 给 observer 一个标记:真正执行前还要在异步边界后复核一次最新审批/成果。
try { exec.__sanshengGate = true } catch { /* 冻结的 exec 对象则跳过最终复核 */ }
return undefined
} catch (error) {
// 校验内部出错时拒绝而不是放行:目标状态比一次调用重要。
return `三省六部:目标校验失败(${error?.message ?? error}),本次修改未执行。`
}
})
}