Skip to content

feat(policy-guard): stop resending rejected conversations upstream - #876

Open
nieyuanhong wants to merge 1 commit into
icebear0828:devfrom
nieyuanhong:feat/policy-guard
Open

nieyuanhong wants to merge 1 commit into
icebear0828:devfrom
nieyuanhong:feat/policy-guard

Conversation

@nieyuanhong

Copy link
Copy Markdown
Contributor

Summary

上游安全检测拒绝某段对话后,重试(同账号/换账号/去掉 previous_response_id 重发)会复现同样拒绝并再次上报违规,这正是账号升级为封禁的路径。本 PR 引入可开关的「安全策略熔断」:记住被拒绝的对话,后续轮次在本地直接 400 拒绝,使同一拒绝对话对上游的提交恒为 1 次。

默认关闭(policy_guard.enabled = false);开启后的行为变化:命中的策略 403 不再按账号封禁处理。

Changes

  • 判定与隔离:src/proxy/policy-rejection.ts(结构化错误码优先 → 文案关键字兜底 → 状态码仅作收窄)、src/proxy/conversation-quarantine.ts(会话身份 + 输入前缀包含判定;编辑/TTL/手动解除放行)
  • 拦截点:账号路径(src/routes/shared/proxy-handler.ts)、直连路径(direct-request-handler.ts)、流式终态帧记录(streaming-handler.ts + response-processor.ts / responses-passthrough.ts);403 策略拒绝不再进入封号/限流重试分支(proxy-error-handler.ts、proxy-retry-classifier.ts)
  • 配置与 schema:src/config-schema.ts、config/default.yaml,并以测试锁定三处默认值一致
  • 管理端与 UI:GET/POST /admin/policy-guard-settings、GET /admin/policy-guard-quarantines、POST .../reset(src/routes/admin/policy-guard.ts)、设置页 Pane(PolicyGuardSettings.tsx、shared/hooks/use-policy-guard.ts、i18n)
  • 文档与 CI:README.md、API.md;Linux backend job 新增端到端烟测步骤 npm run netlab:policy-guard

Test Plan

  • npx tsc --noEmit
  • npm test — 3513 passed / 38 skipped(含新增:三条拦截路径、隔离表 TTL/前缀、判定矩阵、管理端鉴权、默认值一致性;曾出现 1 次未定位偶发失败,随后两次复跑全绿)
  • npm run build
  • npm run netlab:policy-guard — 本地 12/12:turn1 上游恰好 1 次 → turn2 本地 400 且上游计数不变 → 解除后 turn3 重新放行
  • cd web && npx vitest run — PolicyGuardSettings 组件 7 用例通过(web 全量 tsc 存在既有的 AddAccount.tsx i18n key 报错,与本次无关)
  • fork CI 预验证(nieyuanhong#17):Quality gate 4/4 job 全绿,其中 backend-tests 的新增步骤 Run policy-guard end-to-end smoke 已在 Linux 上通过;Docker smoke ✅

Notes

  • 隔离表为内存态(重启清空),TTL 默认 1 小时;判定每次请求读配置,管理端开关即时生效
  • 分支基线 = dev f824033,与上游当前 dev 一致

A safety-gate rejection is a function of the conversation content, so
retrying it - on the same account, another account, or after stripping
previous_response_id - reproduces the rejection and files another
violation report, which is what escalates to an account ban. Remember the
rejected conversation and answer follow-up turns locally with a 400, so a
rejected conversation reaches the upstream exactly once.

Detection prefers the structured error codes (cyber_policy,
session_blocked_by_cyber_policy), falls back to message keywords, and lets
HTTP status codes only narrow the match, so an ordinary 403 ban or 429
rate limit keeps its existing handling. Quarantine is conversation-scoped:
it holds while the new input still contains the recorded prefix, and is
released on edit, TTL expiry, or an admin reset.

The guard is off by default (policy_guard.enabled = false); enabling it
also stops treating a policy 403 as an account ban. The netlab smoke
(npm run netlab:policy-guard) now runs in the Linux backend CI job and
asserts the one-submission contract end to end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant