guard: 相邻同角色消息合并为一轮,上下文窗口改按字节
真机联调(A6000 + Qwen3Guard-Gen-0.6B)发现多轮拆分绕过:一条危险消息后面跟 4 条 "你看看可以吗",原实现把每条消息当一轮、只带最近 2 条上下文,危险的首条根本进不了判定, 网关判 Safe 放行;同一模型只看首条时判 Unsafe,说明漏在窗口策略不在模型。 改动: - collect_turns 合并相邻同角色消息成一"轮"(之间没有 assistant 回复,语义上仍是一轮), 当前轮 = 最后一轮 user,上述 payload 的 5 条 user 会作为一整段送检。 - 上下文窗口去掉按条数(multiturn_context_messages),只保留 multiturn_context_bytes, 从最近往前按字节取,预算优先给最近的历史;0 仍表示只判当前轮。 - 旧参数 multiturn_context_messages 传入即报错,避免静默失效。 实测:同一 payload 现在 400 content_policy_violation,stub 仍只有 ping(原文不出网关); 正常 user/assistant 交替多轮不受影响(上下文照常进入判定)。 测试 123→129 项,全绿。
This commit is contained in:
1 parent
a67065955e
commit
d51b69c0df
4 files changed
+92
-45
No files matched your search
+45
-30
@@ -6,9 +6,11 @@
|
||||
-- Safe / Unsafe / Controversial,categories 必须命中宿主固定枚举。
|
||||
-- 所以本地判定不能套 OpenAI 的 messages 结构,对外接口才是 OpenAI 格式。
|
||||
--
|
||||
-- 多轮对话:只判最后一条 user 消息(当前轮),之前的历史按
|
||||
-- multiturn_context_messages / multiturn_context_bytes 压成有界上下文附在
|
||||
-- 当前轮前面。历史不重复送检,避免轮次越多越慢、越长越容易超 token。
|
||||
-- 多轮对话:相邻同角色消息先合并成一"轮"(连续多条 user 之间没有 assistant
|
||||
-- 回复,语义上仍属同一轮),当前轮 = 最后一轮 user,之前的历史按
|
||||
-- multiturn_context_bytes 压成字节有界的上下文附在当前轮前面。
|
||||
-- 历史不重复送检,避免轮次越多越慢、越长越容易超 token;上下文只按字节限量,
|
||||
-- 防止用几句短消息把真正危险的那一轮挤出窗口。
|
||||
--
|
||||
-- 宿主对 {action="block"} 只返回它自己固定的 403 正文,插件改不了;所以请求侧判定
|
||||
-- 不通过时不 block,而是把请求体换成 stub(原文不出网关),再在响应侧 replace 成
|
||||
@@ -25,8 +27,7 @@ local MAX_JUDGE_CHUNKS = 8
|
||||
local MAX_JUDGE_CHUNK_BYTES = 24576
|
||||
-- 带上下文时单段要留出上下文的 token 预算,否则中文容易撞 max_input_tokens
|
||||
local CONTEXT_CHUNK_BYTES = 8192
|
||||
-- 多轮上下文上限:条数与字节都要有硬顶,防止历史把单次判定撑爆
|
||||
local MAX_CONTEXT_MESSAGES = 32
|
||||
-- 多轮上下文字节硬顶,防止历史把单次判定撑爆
|
||||
local MAX_CONTEXT_BYTES = 8192
|
||||
local CONTEXT_LABELS = { user = "User: ", assistant = "Assistant: " }
|
||||
|
||||
@@ -538,14 +539,20 @@ local function collect_turns(decoded)
|
||||
if type(message) ~= "table" or type(message.role) ~= "string" then return nil end
|
||||
local text = message_text(message.content)
|
||||
if text and text ~= "" then
|
||||
turns[#turns + 1] = { role = message.role, text = text }
|
||||
local last = turns[#turns]
|
||||
if last and last.role == message.role then
|
||||
-- 相邻同角色合并:客户端把一轮内容拆成多条消息时,不能只看最后一条
|
||||
last.text = last.text .. "\n" .. text
|
||||
else
|
||||
turns[#turns + 1] = { role = message.role, text = text }
|
||||
end
|
||||
end
|
||||
end
|
||||
if #turns == 0 then return nil end
|
||||
return turns
|
||||
end
|
||||
|
||||
-- 当前轮 = 最后一条 user 消息;它之前的所有轮次都是上下文
|
||||
-- 当前轮 = 最后一轮 user(相邻 user 已合并);它之前的所有轮次都是上下文
|
||||
local function split_turns(turns)
|
||||
local index = nil
|
||||
for i = #turns, 1, -1 do
|
||||
@@ -558,18 +565,34 @@ local function split_turns(turns)
|
||||
return turns[index], turns, index
|
||||
end
|
||||
|
||||
-- 历史压成 "User: ... / Assistant: ..." 的纯文本,保留最近的部分
|
||||
local function render_context(turns, stop, max_messages, max_bytes)
|
||||
if max_messages < 1 or stop <= 1 then return "" end
|
||||
local from = stop - max_messages
|
||||
if from < 1 then from = 1 end
|
||||
-- 历史压成 "User: ... / Assistant: ..." 的纯文本,按字节从最近往前取,
|
||||
-- 保证预算优先留给离当前轮最近的历史,不会被早期短消息占满。
|
||||
local function render_context(turns, stop, max_bytes)
|
||||
if max_bytes <= 0 or stop <= 1 then return "" end
|
||||
local lines = {}
|
||||
for i = from, stop - 1 do
|
||||
local used = 0
|
||||
for i = stop - 1, 1, -1 do
|
||||
local label = CONTEXT_LABELS[turns[i].role]
|
||||
if label then lines[#lines + 1] = label .. turns[i].text end
|
||||
if label then
|
||||
local line = label .. turns[i].text
|
||||
local size = #line + 1 -- 含与前一行之间的换行
|
||||
if used + size > max_bytes then
|
||||
if #lines == 0 then
|
||||
-- 最近一条就超预算:只留它的尾部,上下文仍是"最近的"
|
||||
local room = max_bytes - 1
|
||||
if room > 0 then lines[1] = utf8_tail(line, room) end
|
||||
end
|
||||
break
|
||||
end
|
||||
lines[#lines + 1] = line
|
||||
used = used + size
|
||||
end
|
||||
end
|
||||
if #lines == 0 then return "" end
|
||||
return utf8_tail(table.concat(lines, "\n"), max_bytes)
|
||||
-- 上面是倒序收集,这里恢复时间顺序
|
||||
local ordered = {}
|
||||
for i = #lines, 1, -1 do ordered[#ordered + 1] = lines[i] end
|
||||
return table.concat(ordered, "\n")
|
||||
end
|
||||
|
||||
-- 当前轮必须完整保留(截断只能发生在上下文一侧),否则回答侧会拿错问题
|
||||
@@ -743,15 +766,13 @@ function plugin.init(ctx, options)
|
||||
stub_max_tokens = positive_int(o.stub_max_tokens, 16, "stub_max_tokens")
|
||||
end
|
||||
|
||||
-- 多轮上下文:默认取最近 2 条消息(一组问答)、最多 2048 字节;0 表示只判当前轮
|
||||
local multiturn_messages = non_negative_int(
|
||||
o.multiturn_context_messages, 2, "multiturn_context_messages"
|
||||
)
|
||||
if multiturn_messages > MAX_CONTEXT_MESSAGES then
|
||||
error("response_guard: config.multiturn_context_messages must not exceed "
|
||||
.. MAX_CONTEXT_MESSAGES)
|
||||
-- 多轮上下文:只按字节限量(默认 2048);0 表示只判当前轮,历史不进判定
|
||||
-- 旧的 multiturn_context_messages 已移除:按条数会被几句短填充把那轮关键内容挤出窗口
|
||||
if o.multiturn_context_messages ~= nil then
|
||||
error("response_guard: config.multiturn_context_messages has been removed, "
|
||||
.. "use multiturn_context_bytes instead")
|
||||
end
|
||||
local multiturn_bytes = positive_int(o.multiturn_context_bytes, 2048, "multiturn_context_bytes")
|
||||
local multiturn_bytes = non_negative_int(o.multiturn_context_bytes, 2048, "multiturn_context_bytes")
|
||||
if multiturn_bytes > MAX_CONTEXT_BYTES then
|
||||
error("response_guard: config.multiturn_context_bytes must not exceed " .. MAX_CONTEXT_BYTES)
|
||||
end
|
||||
@@ -776,7 +797,6 @@ function plugin.init(ctx, options)
|
||||
check_input = boolean(o.check_input, true),
|
||||
check_output = boolean(o.check_output, true),
|
||||
block_controversial = boolean(o.block_controversial, true),
|
||||
multiturn_context_messages = multiturn_messages,
|
||||
multiturn_context_bytes = multiturn_bytes,
|
||||
}
|
||||
|
||||
@@ -827,12 +847,7 @@ function plugin.on_request(ctx, request)
|
||||
local current, _, index = split_turns(turns)
|
||||
if current == nil then return { action = "pass" } end
|
||||
|
||||
local context = render_context(
|
||||
turns,
|
||||
index,
|
||||
config.multiturn_context_messages,
|
||||
config.multiturn_context_bytes
|
||||
)
|
||||
local context = render_context(turns, index, config.multiturn_context_bytes)
|
||||
|
||||
-- 输出判定要带上本轮提问(含上文):Qwen3Guard 的 response_moderation 用 user 轮做上下文
|
||||
state.input_text = context_prompt(context, current.text, MAX_INPUT_CONTEXT)
|
||||
|
||||
Reference in new issue
Block a user