Files
agent-safe-middleware/config.example.yaml
T
agcl d51b69c0df 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 项,全绿。
2026-11-27 20:35:37 +08:00

83 lines
2.8 KiB
YAML
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.
upstream: http://127.0.0.1:8000
listen_host: 0.0.0.0
listen_port: 8080
limits:
max_sessions: 4
# 本地模型串行推理(max_concurrency 固定 1),按单段最慢几十毫秒到数秒留
hook_timeout_seconds: 120
upstream_timeout_seconds: 120
max_attachment_files: 8
max_attachment_fields: 128
max_attachment_text_bytes: 1048576
max_attachment_unpacked_bytes: 33554432
max_attachment_zip_entries: 2048
attachment_timeout_seconds: 30
max_attachment_reads: 64
# 本地守卫模型。必须是 Qwen3Guard-Gen(0.6B/4B/8B):宿主的 parse_guard_output 按它的
# "Safety: / Categories: / Refusal:" 格式与十个固定类别名解析,换别的模型会全部 503。
# file 指向 HuggingFace 目录(config.json + 权重 + tokenizer),缺失时宿主启动即失败。
# 注意:该相对路径按进程工作目录解析,不按本文件所在目录解析。
# 部署前跑 tools/check_local_model.py 自检。
llm:
- id: guard_model
type: guard
file: models/guard_model
load:
device: cpu
dtype: auto
limits:
max_input_tokens: 8192
max_new_tokens: 64
timeout_seconds: 60
queue_size: 32
plugins:
- id: response-guard
file: plugins/response_guard.lua
match:
paths: ["/v1/chat/completions"]
# 必须与 llm 段的 id 一致,否则 host.llm.call 一定失败
llm:
allow: [guard_model]
config:
model: guard_model
block_status: 400
# buffered 则整条响应只判一次,但客户端看不到增量
stream_mode: incremental
flush_chars: 512
force_flush_bytes: 4096
# 8 段 × judge_chunk_bytes
max_buffer_bytes: 65536
judge_chunk_bytes: 8192
check_input: true
check_output: true
# Controversial 也按不安全处理;误伤多时设 false
block_controversial: true
# 多轮对话:相邻同角色消息先合并成一"轮",当前轮 = 最后一轮 user,
# 更早的轮次按字节压成有界上下文附在前面,历史不重复送检。
# 只按字节限量(最近的历史优先),避免几句短消息把关键轮次挤出窗口;
# 0 表示只判当前轮,历史完全不进判定。
multiturn_context_bytes: 2048
# 拦截后替换给上游的占位请求:客户端没给 model 时用它兜底
# stub_model: your-upstream-model
# stub_max_tokens: false
- id: stream-redact
enabled: false
file: plugins/stream_redact.lua
match:
paths: ["/v1/chat/completions"]
config:
content_path: choices.*.delta.content
max_buffer_bytes: 262144
- id: request-rewrite
enabled: false
file: plugins/request_rewrite.lua
match:
paths: ["/v1/chat/completions"]
config:
prefix: "[checked] "