dsh-sansheng-liubu/test/policy.test.js

139 lines
5.8 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import {
COURT_ROLES,
DEFAULT_ROUTES,
EXECUTION_TOOLSET,
inject,
roleFromMembership,
routeForRole,
toolsetForRole,
evaluateToolPolicy,
apply,
} from '../index.js'
const approvedTask = ownerName => ({ id: 'exec-1', status: 'in_progress', ownerName, blockedBy: [] })
function mockRuntime(role = 'lead') {
const sections = new Map()
const tools = new Map()
const agent = {
ctx: {
effect: callback => callback(),
systemPrompt: {
getSectionOrder: () => 0,
section: section => {
if (sections.has(section.name)) throw new Error(`duplicate prompt ${section.name}`)
sections.set(section.name, section)
return () => sections.delete(section.name)
},
},
on: () => () => {},
tools: {
register: definition => { tools.set(definition.name, definition); return () => tools.delete(definition.name) },
guard: callback => { agent.guard = callback; return () => { delete agent.guard } },
},
},
}
const ctx = {
agents: { list: () => [agent] },
agentPresets: { composedPreset: () => 'sansheng-liubu' },
agentTeams: {
tryMembership: () => role === 'unassigned' ? undefined : role === 'lead' ? { role: 'lead', name: 'lead' } : { role: 'teammate', name: role },
listTasks: () => [],
},
logger: { info: () => {}, warn: () => {} },
on: () => () => {},
}
return { ctx, agent, sections, tools }
}
test('maps roles and preserves model routes', () => {
assert.equal(roleFromMembership(undefined), 'lead')
assert.equal(roleFromMembership({ role: 'lead', name: 'lead' }), 'lead')
assert.equal(roleFromMembership({ role: 'teammate', name: 'menxia' }), 'menxia')
assert.equal(routeForRole('lead', DEFAULT_ROUTES), undefined, '默认不强制模型,避免宿主缺少适配器')
assert.deepEqual(routeForRole('lead', { lead: { provider: 'p', model: 'm', reasoningEffort: 'high' } }), { provider: 'p', model: 'm', reasoningEffort: 'high' })
assert.equal(routeForRole('gongbu', { minister: { provider: 'p', model: 'm' } }).model, 'm', '未列出的角色回退到 minister 路由')
assert.equal(inject.includes('sanshengLiubuGeneration'), false)
assert.equal(inject.includes('credentials'), false)
})
test('maps the normal unassigned primary agent to Chancellor automatically', () => {
const runtime = mockRuntime('unassigned')
const dispose = apply(runtime.ctx)
assert.match(runtime.sections.get('sansheng-liubu:role').text, /三省六部模式:丞相/)
dispose()
})
test('keeps execution file tools and planning roles read-only', () => {
for (const role of ['gongbu', 'libu', 'hubu', 'lipu', 'bingbu', 'xingbu']) {
assert.deepEqual(toolsetForRole(role), EXECUTION_TOOLSET)
}
for (const role of ['gongbu', 'lipu', 'bingbu', 'xingbu']) {
assert.equal(evaluateToolPolicy(role, 'bash', [approvedTask(role)]), undefined)
}
assert.match(evaluateToolPolicy('menxia', 'write', []), /只读/)
})
test('keeps clerk and revenue ministries off the command line', () => {
for (const role of ['libu', 'hubu']) {
assert.equal(evaluateToolPolicy(role, 'write', [approvedTask(role)]), undefined)
assert.match(evaluateToolPolicy(role, 'bash', [approvedTask(role)]), /不属于/)
}
assert.match(evaluateToolPolicy('libu', 'pwsh', [approvedTask('libu')]), /不属于/)
})
test('keeps the court at ten seats', () => {
assert.equal(COURT_ROLES.length, 10)
assert.deepEqual([...COURT_ROLES].sort(), ['bingbu', 'gongbu', 'hubu', 'lead', 'libu', 'lipu', 'menxia', 'shangshu', 'xingbu', 'zhongshu'])
for (const role of COURT_ROLES.filter((entry) => entry !== 'lead')) {
assert.equal(roleFromMembership({ role: 'teammate', name: role }), role, `${role} 是可识别的朝堂角色`)
}
assert.equal(roleFromMembership({ role: 'lead', name: 'lead' }), 'lead', '丞相由主会话担任')
})
test('allows Image-gen generation, batch, and edit only for approved Gongbu work', () => {
for (const tool of ['generate_image', 'generate_images', 'edit_image']) {
assert.match(evaluateToolPolicy('gongbu', tool, []), /尚未准奏/)
assert.equal(evaluateToolPolicy('gongbu', tool, [approvedTask('gongbu')]), undefined)
assert.match(evaluateToolPolicy('libu', tool, [approvedTask('libu')]), /不属于/)
assert.match(evaluateToolPolicy('lead', tool, []), /只读/)
}
})
test('blocks non-Image-gen image tools', () => {
for (const tool of ['image_generate', 'codex_image_generate']) {
assert.match(evaluateToolPolicy('gongbu', tool, [approvedTask('gongbu')]), /只允许使用 Image-gen/)
}
})
test('does not register duplicate image tools', () => {
const runtime = mockRuntime('gongbu')
const dispose = apply(runtime.ctx)
assert.equal(runtime.tools.size, 0)
assert.deepEqual([...runtime.sections.keys()], ['sansheng-liubu:role'])
const prompt = runtime.sections.get('sansheng-liubu:role').text
assert.ok(prompt.includes('工部职责') || prompt.includes('你负责代码实现'), '工部提示词已注册且不重复')
dispose()
})
test('role prompt registration remains idempotent', () => {
const runtime = mockRuntime('gongbu')
const disposeFirst = apply(runtime.ctx)
const disposeSecond = apply(runtime.ctx)
assert.deepEqual([...runtime.sections.keys()], ['sansheng-liubu:role'])
assert.equal(runtime.tools.size, 0)
disposeFirst()
assert.ok(runtime.sections.has('sansheng-liubu:role'))
disposeSecond()
assert.equal(runtime.sections.size, 0)
})
test('keeps management tool audit and approval rules', () => {
const audit = []
assert.equal(evaluateToolPolicy('lead', 'task_board_create', [], { audit: item => audit.push(item) }), undefined)
assert.equal(audit[0].authority, 'chancellor')
assert.match(evaluateToolPolicy('bingbu', 'automation_create', [approvedTask('bingbu')]), /门下省准奏记录/)
})