import { mkdir, readFile, rename, writeFile } from 'node:fs/promises' import { dirname, join } from 'node:path' import { ROLE_IDS } from './roles.js' const VERSION = 3 export const REVIEW_LIMIT = 400 export const DISPATCH_LIMIT = 600 export const REWORK_LIMIT = 200 export const EVENT_LIMIT = 300 export const ARTIFACT_LIMIT = 800 export class SanshengStoreError extends Error { constructor(code, message) { super(message) this.name = 'SanshengStoreError' this.code = code } } export function emptyState() { return { version: VERSION, seq: 0, projects: {}, reviews: [], dispatches: [], reworks: [], artifacts: {}, leases: {}, events: [], } } function isObject(value) { return value !== null && typeof value === 'object' && !Array.isArray(value) } function isNonEmptyString(value) { return typeof value === 'string' && value.trim() !== '' } function projectProblem(project, id) { if (!isObject(project)) return `项目 ${id} 不是对象` if (!isNonEmptyString(project.id)) return `项目 ${id} 缺少 id` if (project.id !== id) return `项目键 ${id} 与 id ${project.id} 不一致` if (!isNonEmptyString(project.name)) return `项目 ${id} 缺少 name` if (!isNonEmptyString(project.roomId)) return `项目 ${id} 缺少 roomId` if (!isObject(project.bots)) return `项目 ${id} 缺少 bots` for (const role of ROLE_IDS) { if (!isNonEmptyString(project.bots[role])) return `项目 ${id} 缺少 ${role} Bot` } if (new Set(ROLE_IDS.map(role => project.bots[role])).size !== ROLE_IDS.length) { return `项目 ${id} 的角色 Bot 重复` } if (project.workspaceId !== undefined && project.workspaceId !== null && typeof project.workspaceId !== 'string') return `项目 ${id} 的 workspaceId 非法` if (project.models !== undefined && !isObject(project.models)) return `项目 ${id} 的 models 非法` if (project.delivery !== undefined && !isObject(project.delivery)) return `项目 ${id} 的 delivery 非法` return undefined } function arrayProblem(value, name) { if (!Array.isArray(value)) return `${name} 不是数组` for (const [index, item] of value.entries()) { if (!isObject(item)) return `${name}[${index}] 不是对象` } return undefined } /** * Q01:只接受明确支持的 v2/v3 schema。合法 JSON 但 schema 错误一律视为损坏, * 不能把 null/[]/错误版本/部分非法结构静默回退成“健康空状态”。 */ export function validateState(raw) { if (!isObject(raw)) return { ok: false, error: '状态顶层不是对象' } if (raw.version !== 2 && raw.version !== 3) return { ok: false, error: `不支持的状态版本 ${String(raw.version)}` } if (!isObject(raw.projects)) return { ok: false, error: 'projects 不是对象' } for (const [id, project] of Object.entries(raw.projects)) { const problem = projectProblem(project, id) if (problem) return { ok: false, error: problem } } for (const name of ['reviews', 'dispatches', 'reworks', 'events']) { const problem = arrayProblem(raw[name] ?? [], name) if (problem) return { ok: false, error: problem } } if (raw.artifacts !== undefined && !isObject(raw.artifacts)) return { ok: false, error: 'artifacts 不是对象' } if (raw.leases !== undefined && !isObject(raw.leases)) return { ok: false, error: 'leases 不是对象' } if (raw.seq !== undefined && !Number.isFinite(raw.seq)) return { ok: false, error: 'seq 不是数字' } return { ok: true, state: raw } } function normalizeProject(project) { if (!isObject(project)) return undefined return { submissionSeq: 0, delivery: { include: [], exclude: [] }, ...project, schemaVersion: VERSION, models: isObject(project.models) ? project.models : {}, bots: isObject(project.bots) ? project.bots : {}, } } function normalize(raw) { const state = isObject(raw) ? raw : {} const projects = {} for (const [id, project] of Object.entries(isObject(state.projects) ? state.projects : {})) { const next = normalizeProject(project) if (next) projects[id] = next } return { version: VERSION, seq: Number.isFinite(state.seq) ? state.seq : 0, projects, reviews: Array.isArray(state.reviews) ? state.reviews : [], dispatches: Array.isArray(state.dispatches) ? state.dispatches : [], reworks: Array.isArray(state.reworks) ? state.reworks : [], artifacts: isObject(state.artifacts) ? state.artifacts : {}, leases: isObject(state.leases) ? state.leases : {}, events: Array.isArray(state.events) ? state.events : [], } } async function readJsonFile(file) { try { return JSON.parse(await readFile(file, 'utf8')) } catch (error) { if (error?.code === 'ENOENT') return undefined throw new Error(`读不了 ${file}:${error?.message ?? error}`) } } async function writeJsonAtomic(file, value) { await mkdir(dirname(file), { recursive: true }) const tmp = `${file}.${process.pid}.${Date.now()}.tmp` await writeFile(tmp, `${JSON.stringify(value, null, 2)}\n`, 'utf8') await rename(tmp, file) } function registryOf(state) { const rooms = {} const bots = {} for (const [projectId, project] of Object.entries(state.projects ?? {})) { if (project.roomId) rooms[project.roomId] = projectId for (const role of ROLE_IDS) { const botId = project.bots?.[role] if (botId) bots[botId] = projectId } } return { version: 1, rooms, bots, updatedAt: Date.now() } } function clone(value) { return structuredClone(value) } /** * 三省六部的补充记录:项目绑定(ProjectBinding)、审查记录(ReviewRecord)、 * 派发记录(DispatchRecord)、成果快照(ArtifactSnapshot)、写租约(WriteLease)与审计事件。 * 原生 group_goal 仍是阶段进度的唯一权威;这里只存它没有的东西。 * * 写入串行化。每次事务先克隆内存状态,落盘失败时回滚内存,避免"内存成功、磁盘失败"。 * 另写一份只含 room/bot → project 的 registry 提示文件:主状态损坏时仍能识别受治理群并拒绝写操作。 */ export class SanshengStore { #file #registryFile #state #registry #ready #queue = Promise.resolve() #health constructor(file) { this.#file = file this.#registryFile = join(dirname(file), 'registry.json') this.#state = emptyState() this.#registry = { version: 1, rooms: {}, bots: {}, updatedAt: 0 } this.#ready = undefined this.#health = { status: 'missing', path: file, error: null } } get file() { return this.#file } get registryFile() { return this.#registryFile } async load() { this.#ready ??= (async () => { let projectError let loadedRaw try { const raw = await readJsonFile(this.#file) if (raw === undefined) { this.#state = emptyState() this.#health = { status: 'missing', path: this.#file, error: null } } else { const checked = validateState(raw) if (!checked.ok) throw new Error(`状态 schema 非法:${checked.error}`) loadedRaw = checked.state this.#state = normalize(loadedRaw) this.#health = { status: 'ok', path: this.#file, error: null } } } catch (error) { projectError = error this.#state = emptyState() this.#health = { status: 'broken', path: this.#file, error: String(error?.message ?? error) } } try { const raw = await readJsonFile(this.#registryFile) if (isObject(raw)) { this.#registry = { version: 1, rooms: isObject(raw.rooms) ? raw.rooms : {}, bots: isObject(raw.bots) ? raw.bots : {}, updatedAt: raw.updatedAt ?? 0, } } } catch { // registry 只是提示文件;读不了时按无提示处理,不影响主状态。 } // 主状态合法但为空、registry 仍记录受治理项目:说明主状态被替换/截断, // 不能当作“项目全都删了”并覆盖最后的治理身份。 if (this.#health.status === 'ok' && Object.keys(this.#state.projects).length === 0 && (Object.keys(this.#registry.rooms ?? {}).length > 0 || Object.keys(this.#registry.bots ?? {}).length > 0)) { this.#health = { status: 'broken', path: this.#file, error: '主状态为空但 registry 仍记录受治理项目' } projectError ??= new Error(`状态 schema 非法:${this.#health.error}`) } if (this.#health.status === 'ok') await this.#writeRegistry(this.#state).catch(() => {}) if (projectError) throw projectError return this.#state })() return this.#ready } health() { return { ...this.#health, registry: this.#registryFile } } /** 同步快照;guard 只能同步读取。 */ peek() { return this.#state } registryPeek() { return this.#registry } async #writeRegistry(state = this.#state) { const registry = registryOf(state) await writeJsonAtomic(this.#registryFile, registry) this.#registry = registry } async transact(mutate) { const run = this.#queue.then(async () => { await this.load() // Q06:在草稿上修改,落盘成功后才发布给同步读取者;任何读都不会看到未提交状态。 const draft = clone(this.#state) try { const result = await mutate(draft) if (draft.reviews.length > REVIEW_LIMIT) draft.reviews = draft.reviews.slice(-REVIEW_LIMIT) if (draft.dispatches.length > DISPATCH_LIMIT) draft.dispatches = draft.dispatches.slice(-DISPATCH_LIMIT) if (draft.reworks.length > REWORK_LIMIT) draft.reworks = draft.reworks.slice(-REWORK_LIMIT) if (draft.events.length > EVENT_LIMIT) draft.events = draft.events.slice(-EVENT_LIMIT) const artifactIds = Object.keys(draft.artifacts) if (artifactIds.length > ARTIFACT_LIMIT) { const keep = new Set(artifactIds.sort((a, b) => (draft.artifacts[a].createdAt ?? 0) - (draft.artifacts[b].createdAt ?? 0)).slice(-ARTIFACT_LIMIT)) for (const id of artifactIds) if (!keep.has(id)) delete draft.artifacts[id] } await writeJsonAtomic(this.#file, draft) // 只有主状态落盘成功后才发布;registry 失败不回滚已提交主状态,下次 load 重建。 this.#state = draft try { await this.#writeRegistry(draft) } catch { // registry 是提示文件,主状态已是权威。 } this.#health = { status: 'ok', path: this.#file, error: null } return result } catch (error) { throw error } }) this.#queue = run.then(() => undefined, () => undefined) return run } nextSeq() { return this.#state.seq + 1 } projectByRoom(roomId) { if (!roomId) return undefined return Object.values(this.#state.projects).find(project => project.roomId === roomId) } projectsByBot(botId) { if (!botId) return [] return Object.values(this.#state.projects).filter(project => ROLE_IDS.some(role => project.bots?.[role] === botId)) } projectByBot(botId) { return this.projectsByBot(botId)[0] } projectById(id) { return id ? this.#state.projects[id] : undefined } resolveProject(ref) { const key = String(ref ?? '').trim() if (key === '') return undefined const projects = Object.values(this.#state.projects) return projects.find(project => project.id === key) ?? projects.find(project => project.name === key) ?? projects.find(project => project.name?.toLocaleLowerCase() === key.toLocaleLowerCase()) } /** 主状态读不了时,registry 仍能说明某群/某 Bot 属于受治理项目。 */ registryProjectForRoom(roomId) { return roomId ? this.#registry.rooms?.[roomId] : undefined } registryProjectForBot(botId) { return botId ? this.#registry.bots?.[botId] : undefined } async #validateProject(project, state = this.#state) { if (!isObject(project) || typeof project.id !== 'string' || project.id === '') { throw new SanshengStoreError('invalid-project', '项目绑定缺少 id。') } const bots = ROLE_IDS.map(role => project.bots?.[role]).filter(Boolean) if (new Set(bots).size !== bots.length) { throw new SanshengStoreError('roles-duplicated', '丞相/尚书/工部必须是三个不同的 Bot。') } for (const other of Object.values(state.projects)) { if (other.id === project.id) continue if (project.roomId && other.roomId === project.roomId) { throw new SanshengStoreError('room-taken', `群 ${project.roomId} 已绑定项目「${other.name}」。`) } for (const role of ROLE_IDS) { const botId = project.bots?.[role] if (botId && ROLE_IDS.some(otherRole => other.bots?.[otherRole] === botId)) { throw new SanshengStoreError('bot-taken', `Bot ${botId} 已属于项目「${other.name}」,一个 Bot 只能属于一个项目。`) } } } } async saveProject(project) { return this.transact(async state => { await this.#validateProject(project, state) const previous = state.projects[project.id] ?? {} const next = { schemaVersion: VERSION, submissionSeq: 0, delivery: { include: [], exclude: [] }, ...previous, ...project, models: { ...(previous.models ?? {}), ...(project.models ?? {}) }, bots: { ...(previous.bots ?? {}), ...(project.bots ?? {}) }, updatedAt: Date.now(), } state.projects[project.id] = next return next }) } async updateProject(id, patch) { return this.transact(async state => { const project = state.projects[id] if (!project) throw new SanshengStoreError('not-found', `项目 ${id} 不存在。`) const next = { ...project, ...patch, updatedAt: Date.now() } await this.#validateProject(next, state) state.projects[id] = next return next }) } async removeProject(id) { return this.transact(state => { const existed = state.projects[id] !== undefined delete state.projects[id] delete state.leases[id] return existed }) } /** 相同 eventKey 的审查只记一次(重试/重复事件幂等)。 */ async addReview(review) { return this.transact(state => { const existing = review.eventKey === undefined ? undefined : state.reviews.find(item => item.eventKey === review.eventKey) if (existing) return { review: existing, duplicate: true } state.seq += 1 const record = { ...review, seq: review.seq ?? state.seq } state.reviews.push(record) return { review: record, duplicate: false } }) } reviewsFor(match = {}) { return this.#state.reviews .filter(item => Object.entries(match).every(([key, value]) => item[key] === value)) .sort((a, b) => (b.seq ?? 0) - (a.seq ?? 0) || (b.at ?? 0) - (a.at ?? 0)) } /** 最新判决:先限定审查对象与合法审查身份,再取 seq/at 最大的一条。 */ latestReviewFor({ projectId, goalId, kind, stageIndex, reviewerRole } = {}) { const matches = this.#state.reviews.filter(review => { if (projectId !== undefined && review.projectId !== projectId) return false if (goalId !== undefined && review.goalId !== goalId) return false if (kind !== undefined && review.kind !== kind) return false if (stageIndex !== undefined && (review.stageIndex ?? null) !== (stageIndex ?? null)) return false if (reviewerRole !== undefined && review.reviewerRole !== reviewerRole) return false return true }) if (matches.length === 0) return undefined return matches.sort((a, b) => (b.seq ?? 0) - (a.seq ?? 0) || (b.at ?? 0) - (a.at ?? 0))[0] } /** 抓取成果后作为一次"提交代次"落库;旧审查因代次推进自动失效。 */ async commitArtifact(projectId, snapshot) { return this.transact(state => { const project = state.projects[projectId] if (!project) throw new SanshengStoreError('not-found', `项目 ${projectId} 不存在。`) state.seq += 1 const submissionSeq = (project.submissionSeq ?? 0) + 1 project.submissionSeq = submissionSeq const record = { ...snapshot, id: snapshot.id, submissionSeq, seq: state.seq } state.artifacts[record.id] = record return { snapshot: record, submissionSeq } }) } artifactById(id) { return id ? this.#state.artifacts[id] : undefined } artifactsFor(projectId, limit = 10) { return Object.values(this.#state.artifacts) .filter(item => projectId === undefined || item.projectId === projectId) .sort((a, b) => (b.createdAt ?? 0) - (a.createdAt ?? 0)) .slice(0, limit) } /** * 派发意图:先落盘再调用原生工具。进程中途消失时它停在 prepared/in_flight, * 下次启动对账会把它标成 unknown(待核实),而不是假装成功或盲目重发。 */ async beginDispatch(record) { return this.transact(state => { if (record.requestId) { const existing = state.dispatches.find(item => item.requestId === record.requestId) if (existing) { if (existing.payloadDigest !== record.payloadDigest) { throw new SanshengStoreError('payload-conflict', `requestId ${record.requestId} 已用于另一份 payload,拒绝重复投递。`) } return { record: existing, replay: true } } } state.seq += 1 const entry = { ...record, status: 'prepared', at: record.at ?? Date.now(), seq: state.seq } state.dispatches.push(entry) return { record: entry, replay: false } }) } async markDispatchInFlight(id) { return this.transact(state => { const record = state.dispatches.find(item => item.id === id) if (!record) return undefined if (record.status === 'prepared') { record.status = 'in_flight' record.sentAt = Date.now() } return record }) } async finishDispatch(id, patch) { return this.transact(state => { const record = state.dispatches.find(item => item.id === id) if (!record) return undefined Object.assign(record, patch, { finishedAt: Date.now() }) return record }) } /** 重启对账:仍停在 prepared/in_flight 的派发视为「结果未知」,保留待人工/尚书核实。 */ async reconcileDispatches(bootId) { return this.transact(state => { let marked = 0 for (const record of state.dispatches) { if (record.status !== 'prepared' && record.status !== 'in_flight' && record.status !== 'intent') continue record.status = 'unknown' record.note = '宿主在派发途中退出;结果未知,需核实后再决定是否重发。' record.reconciledBy = bootId record.reconciledAt = Date.now() marked += 1 } return marked }) } /** 尚书/用户核实一条 unknown 派发:done=确实执行过,not-done=没执行。 */ async resolveDispatch(id, verdict, by, projectId, { withEvent = false } = {}) { return this.transact(state => { const record = state.dispatches.find(item => item.id === id) if (!record) return undefined if (projectId && record.projectId !== projectId) return { record, changed: false, wrongProject: true } if (record.status !== 'unknown') return { record, changed: false } record.status = verdict === 'done' ? 'confirmed' : 'rejected' record.resolvedBy = by record.resolvedAt = Date.now() record.note = verdict === 'done' ? '核实:原生操作已执行。' : '核实:原生操作没有执行。' if (withEvent) { state.seq += 1 state.events.push({ kind: 'reconcile', projectId: record.projectId, summary: `${id} → ${verdict}`, action: record.action, seq: state.seq, at: Date.now(), }) } return { record, changed: true } }) } async addRework(record) { return this.transact(state => { const key = `${record.goalId}:${record.revisionAfter}` const existing = state.reworks.find(item => item.key === key) if (existing) return { rework: existing, duplicate: true } state.reworks.push({ ...record, key }) return { rework: record, duplicate: false } }) } async addEvent(entry) { return this.transact(state => { state.seq += 1 state.events.push({ ...entry, seq: state.seq, at: entry.at ?? Date.now() }) return entry }) } dispatchesFor(projectId, limit = 10) { return this.#state.dispatches .filter(item => projectId === undefined || item.projectId === projectId) .slice(-limit) .reverse() } unknownDispatches(projectId) { return this.#state.dispatches.filter(item => item.status === 'unknown' && (projectId === undefined || item.projectId === projectId)) } reworksFor(projectId, limit = 10) { return this.#state.reworks .filter(item => projectId === undefined || item.projectId === projectId) .slice(-limit) .reverse() } /** 每项目最多一个活动写任务:租约由服务端持有、过期可回收。 */ async acquireLease(projectId, { holder, taskId = null, ttlMs = 30 * 60 * 1000 } = {}) { return this.transact(state => { const project = state.projects[projectId] if (!project) throw new SanshengStoreError('not-found', `项目 ${projectId} 不存在。`) const now = Date.now() const current = state.leases[projectId] if (current && current.expiresAt > now && current.holder !== holder) { return { ok: false, lease: current, reason: `项目正在被 ${current.holder} 写入(任务 ${current.taskId ?? '未命名'}),请等它释放。` } } const lease = { projectId, holder, taskId, acquiredAt: now, expiresAt: now + ttlMs, renewedAt: now } state.leases[projectId] = lease return { ok: true, lease } }) } async releaseLease(projectId, holder) { return this.transact(state => { const current = state.leases[projectId] if (!current || current.holder !== holder) return { ok: false, reason: '租约不存在或不属于该持有者。' } delete state.leases[projectId] return { ok: true } }) } leaseOf(projectId) { const lease = this.#state.leases?.[projectId] if (!lease) return undefined if (lease.expiresAt <= Date.now()) return undefined return lease } /** Q08:真实文件写入口按“规范化工作根”互斥;不同项目指向同一根目录也必须串行。 */ async acquireRootLease(root, { holder, taskId = null, ttlMs = 30 * 60 * 1000 } = {}) { const key = rootLeaseKey(root) if (key === '') throw new SanshengStoreError('invalid-root', '工作目录为空,不能建立写租约。') return this.transact(state => { const now = Date.now() const current = state.leases[key] if (current && current.expiresAt > now && current.holder !== holder) { return { ok: false, lease: current, reason: `工作目录正在被 ${current.holder} 写入(任务 ${current.taskId ?? '未命名'}),同一实际路径不能并发写。`, } } const lease = { root: key.slice(5), holder, taskId, acquiredAt: now, expiresAt: now + ttlMs, renewedAt: now } state.leases[key] = lease return { ok: true, lease } }) } async releaseRootLease(root, holder) { const key = rootLeaseKey(root) return this.transact(state => { const current = state.leases[key] if (!current || current.holder !== holder) return { ok: false, reason: '工作根租约不存在或不属于该持有者。' } delete state.leases[key] return { ok: true } }) } rootLeaseOf(root) { const current = this.#state.leases?.[rootLeaseKey(root)] if (!current) return undefined if (current.expiresAt <= Date.now()) return undefined return current } } function rootLeaseKey(root) { const text = String(root ?? '').trim().replace(/\\/g, '/').replace(/\/+$/, '') if (text === '') return '' return `root:${text}`.toLowerCase() }