mirror of
https://github.com/qingchencloud/clawpanel.git
synced 2026-06-08 17:20:01 +08:00
feat(hermes): add tool loop guardrails form
This commit is contained in:
35
tests/hermes-config-page-ui.test.js
Normal file
35
tests/hermes-config-page-ui.test.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { t } from '../src/lib/i18n.js'
|
||||
|
||||
const source = readFileSync(new URL('../src/engines/hermes/pages/config.js', import.meta.url), 'utf8')
|
||||
|
||||
function extractEngineKeys() {
|
||||
return [...source.matchAll(/['"](engine\.[A-Za-z0-9_.-]+)['"]/g)].map(match => match[1])
|
||||
}
|
||||
|
||||
test('Hermes 配置页会暴露工具循环防护结构化配置字段', () => {
|
||||
for (const id of [
|
||||
'hm-tool-guardrails-save',
|
||||
'hm-tool-guardrails-warnings-enabled',
|
||||
'hm-tool-guardrails-hard-stop-enabled',
|
||||
'hm-tool-guardrails-warn-exact-failure',
|
||||
'hm-tool-guardrails-warn-same-tool-failure',
|
||||
'hm-tool-guardrails-warn-no-progress',
|
||||
'hm-tool-guardrails-hard-stop-exact-failure',
|
||||
'hm-tool-guardrails-hard-stop-same-tool-failure',
|
||||
'hm-tool-guardrails-hard-stop-no-progress',
|
||||
]) {
|
||||
assert.match(source, new RegExp(`id="${id}"`), `缺少 ${id}`)
|
||||
}
|
||||
})
|
||||
|
||||
test('Hermes 配置页新增结构化配置不会暴露翻译 key', () => {
|
||||
const keys = new Set(extractEngineKeys().filter(key => key.includes('ToolGuardrails')))
|
||||
|
||||
assert.ok(keys.size > 0, '应能提取工具循环防护用到的 engine 翻译 key')
|
||||
for (const key of keys) {
|
||||
assert.notEqual(t(key), key, `${key} 缺少运行时翻译`)
|
||||
}
|
||||
})
|
||||
106
tests/hermes-tool-loop-guardrails-config.test.js
Normal file
106
tests/hermes-tool-loop-guardrails-config.test.js
Normal file
@@ -0,0 +1,106 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import {
|
||||
buildHermesToolLoopGuardrailsConfigValues,
|
||||
mergeHermesToolLoopGuardrailsConfig,
|
||||
} from '../scripts/dev-api.js'
|
||||
|
||||
test('Hermes 工具循环防护读取会提供上游默认值', () => {
|
||||
const values = buildHermesToolLoopGuardrailsConfigValues({})
|
||||
|
||||
assert.deepEqual(values, {
|
||||
warningsEnabled: true,
|
||||
hardStopEnabled: false,
|
||||
warnExactFailure: 2,
|
||||
warnSameToolFailure: 3,
|
||||
warnNoProgress: 2,
|
||||
hardStopExactFailure: 5,
|
||||
hardStopSameToolFailure: 8,
|
||||
hardStopNoProgress: 5,
|
||||
})
|
||||
})
|
||||
|
||||
test('Hermes 工具循环防护读取会回显嵌套阈值字段', () => {
|
||||
const values = buildHermesToolLoopGuardrailsConfigValues({
|
||||
tool_loop_guardrails: {
|
||||
warnings_enabled: false,
|
||||
hard_stop_enabled: true,
|
||||
warn_after: {
|
||||
exact_failure: 3,
|
||||
same_tool_failure: 4,
|
||||
idempotent_no_progress: 5,
|
||||
},
|
||||
hard_stop_after: {
|
||||
exact_failure: 6,
|
||||
same_tool_failure: 7,
|
||||
idempotent_no_progress: 8,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
assert.equal(values.warningsEnabled, false)
|
||||
assert.equal(values.hardStopEnabled, true)
|
||||
assert.equal(values.warnExactFailure, 3)
|
||||
assert.equal(values.warnSameToolFailure, 4)
|
||||
assert.equal(values.warnNoProgress, 5)
|
||||
assert.equal(values.hardStopExactFailure, 6)
|
||||
assert.equal(values.hardStopSameToolFailure, 7)
|
||||
assert.equal(values.hardStopNoProgress, 8)
|
||||
})
|
||||
|
||||
test('Hermes 工具循环防护保存会保留无关 YAML 并写入上游嵌套结构', () => {
|
||||
const next = mergeHermesToolLoopGuardrailsConfig({
|
||||
model: { provider: 'anthropic' },
|
||||
tool_loop_guardrails: {
|
||||
warnings_enabled: true,
|
||||
custom_flag: 'keep-me',
|
||||
warn_after: {
|
||||
exact_failure: 2,
|
||||
custom_warn: 99,
|
||||
},
|
||||
},
|
||||
streaming: { enabled: true },
|
||||
}, {
|
||||
warningsEnabled: false,
|
||||
hardStopEnabled: true,
|
||||
warnExactFailure: '3',
|
||||
warnSameToolFailure: '4',
|
||||
warnNoProgress: '5',
|
||||
hardStopExactFailure: '6',
|
||||
hardStopSameToolFailure: '7',
|
||||
hardStopNoProgress: '8',
|
||||
})
|
||||
|
||||
assert.deepEqual(next.model, { provider: 'anthropic' })
|
||||
assert.deepEqual(next.streaming, { enabled: true })
|
||||
assert.equal(next.tool_loop_guardrails.warnings_enabled, false)
|
||||
assert.equal(next.tool_loop_guardrails.hard_stop_enabled, true)
|
||||
assert.equal(next.tool_loop_guardrails.custom_flag, 'keep-me')
|
||||
assert.equal(next.tool_loop_guardrails.warn_after.exact_failure, 3)
|
||||
assert.equal(next.tool_loop_guardrails.warn_after.same_tool_failure, 4)
|
||||
assert.equal(next.tool_loop_guardrails.warn_after.idempotent_no_progress, 5)
|
||||
assert.equal(next.tool_loop_guardrails.warn_after.custom_warn, 99)
|
||||
assert.equal(next.tool_loop_guardrails.hard_stop_after.exact_failure, 6)
|
||||
assert.equal(next.tool_loop_guardrails.hard_stop_after.same_tool_failure, 7)
|
||||
assert.equal(next.tool_loop_guardrails.hard_stop_after.idempotent_no_progress, 8)
|
||||
})
|
||||
|
||||
test('Hermes 工具循环防护保存会拒绝越界阈值', () => {
|
||||
assert.throws(
|
||||
() => mergeHermesToolLoopGuardrailsConfig({}, { warnExactFailure: '0' }),
|
||||
/tool_loop_guardrails\.warn_after\.exact_failure/,
|
||||
)
|
||||
assert.throws(
|
||||
() => mergeHermesToolLoopGuardrailsConfig({}, { warnSameToolFailure: '101' }),
|
||||
/tool_loop_guardrails\.warn_after\.same_tool_failure/,
|
||||
)
|
||||
assert.throws(
|
||||
() => mergeHermesToolLoopGuardrailsConfig({}, { hardStopExactFailure: '0' }),
|
||||
/tool_loop_guardrails\.hard_stop_after\.exact_failure/,
|
||||
)
|
||||
assert.throws(
|
||||
() => mergeHermesToolLoopGuardrailsConfig({}, { hardStopNoProgress: '101' }),
|
||||
/tool_loop_guardrails\.hard_stop_after\.idempotent_no_progress/,
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user