advisers 0.2.0-preview.71 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +130 -201
- package/NOTICE +29 -4
- package/README-zh.md +35 -8
- package/README.md +37 -8
- package/THIRD-PARTY-NOTICES.md +277 -0
- package/bin/advisers.js +2264 -350
- package/index.js +1 -1
- package/lib/attachments.js +158 -29
- package/lib/capture.js +82 -2
- package/lib/evidence.js +1180 -26
- package/lib/ical.js +542 -0
- package/lib/imap.js +82 -24
- package/lib/mcp-server.js +869 -0
- package/lib/model.js +151 -2
- package/lib/notify.js +156 -4
- package/lib/ocr.js +26 -1
- package/lib/pdf.js +52 -6
- package/lib/pet-brief.js +78 -15
- package/lib/receipt-verify.js +644 -0
- package/lib/remind.js +261 -11
- package/lib/research.js +148 -18
- package/lib/safety/classifier.js +444 -0
- package/lib/safety/explain.js +157 -0
- package/lib/safety/index.js +333 -0
- package/lib/safety/redact.js +212 -0
- package/lib/safety/rules.js +824 -0
- package/lib/scheduler.js +399 -0
- package/lib/source.js +294 -0
- package/lib/sqlite.js +702 -42
- package/lib/sync.js +141 -20
- package/lib/timestamp.js +26 -1
- package/lib/tos/index.js +332 -0
- package/lib/tos/manifest.json +32 -0
- package/lib/tos/privacy.zh-CN.md +136 -0
- package/lib/tos/safety.zh-CN.md +101 -0
- package/lib/tos/summary.zh-CN.md +12 -0
- package/lib/tos/terms.zh-CN.md +439 -0
- package/package.json +10 -8
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local content-safety classifier.
|
|
3
|
+
*
|
|
4
|
+
* Pure function over text: no network, no filesystem, no third-party deps.
|
|
5
|
+
* It normalizes (NFKC / zero-width / traditional-to-simplified / whitespace),
|
|
6
|
+
* runs the 14-category rule matrix from rules.js, applies the narrow block
|
|
7
|
+
* allowlist and surface/context downgrades, and returns a structured decision
|
|
8
|
+
* that NEVER contains the original text.
|
|
9
|
+
*
|
|
10
|
+
* Result shape:
|
|
11
|
+
* {
|
|
12
|
+
* action: 'allow' | 'warn' | 'confirm' | 'redact' | 'block',
|
|
13
|
+
* categories: string[], // category ids, highest priority first
|
|
14
|
+
* ruleIds: string[], // stable rule ids, highest priority first
|
|
15
|
+
* confidence: number, // 0..1 (0 for allow)
|
|
16
|
+
* evidenceHash: string | null, // HMAC-SHA256(key, hit snippets)[0..16]
|
|
17
|
+
* code: string, // stable audit code, 'NONE' for allow
|
|
18
|
+
* rulesetVersion: string,
|
|
19
|
+
* rulesetDigest: string,
|
|
20
|
+
* surface: string,
|
|
21
|
+
* severity: 'none' | 'low' | 'medium' | 'high' | 'critical',
|
|
22
|
+
* crisis: boolean, // self-harm: show crisis hint, never block
|
|
23
|
+
* downgraded: boolean,
|
|
24
|
+
* truncated: boolean, // true when only the first maxChars were scanned
|
|
25
|
+
* hits: [{ ruleId, category, subtype, code, severity, action, confidence,
|
|
26
|
+
* hardBlock, crisis, downgraded }]
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* Truncation fails CLOSED (content-safety audit SEC2-02, 2026-11-17): when the
|
|
30
|
+
* input is longer than maxChars the tail is unscannable, so a benign-looking
|
|
31
|
+
* prefix is never returned as 'allow'. Egress surfaces (text that can leave the
|
|
32
|
+
* machine, see SURFACES) return 'block' with code INPUT_TRUNCATED; local-file
|
|
33
|
+
* surfaces return 'confirm' and let the caller decide. The escalation only ever
|
|
34
|
+
* raises the action of an existing decision.
|
|
35
|
+
*
|
|
36
|
+
* The caller owns the audit key: pass { hmacKey } to get an evidenceHash.
|
|
37
|
+
* Without a key evidenceHash is null (never a plaintext or unkeyed fallback).
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
import { createHmac } from 'node:crypto'
|
|
41
|
+
import { DEFAULT_RULESET, actionRank, resolveRuleset } from './rules.js'
|
|
42
|
+
import { detectPiiInternal } from './redact.js'
|
|
43
|
+
|
|
44
|
+
export const DEFAULT_SURFACE = 'chat.input'
|
|
45
|
+
export const MAX_SCAN_CHARS = 100_000
|
|
46
|
+
export const MAX_EVIDENCE_SNIPPETS = 20
|
|
47
|
+
export const MAX_EVIDENCE_SNIPPET_CHARS = 64
|
|
48
|
+
|
|
49
|
+
/** Surface policy table. localFile surfaces downgrade non-CSAM blocks. */
|
|
50
|
+
export const SURFACES = Object.freeze({
|
|
51
|
+
'safety.check': { egress: true, localFile: false },
|
|
52
|
+
'chat.input': { egress: true, localFile: false },
|
|
53
|
+
'chat.output': { egress: true, localFile: false },
|
|
54
|
+
'agent.input': { egress: true, localFile: false },
|
|
55
|
+
'agent.output': { egress: true, localFile: false },
|
|
56
|
+
'model.output': { egress: true, localFile: false },
|
|
57
|
+
'research.query': { egress: true, localFile: false },
|
|
58
|
+
'research.output': { egress: true, localFile: false },
|
|
59
|
+
'capture.input': { egress: false, localFile: true },
|
|
60
|
+
'attachments.text': { egress: false, localFile: true },
|
|
61
|
+
'doc-review.text': { egress: false, localFile: true },
|
|
62
|
+
'office.spec': { egress: false, localFile: true },
|
|
63
|
+
'office.output': { egress: true, localFile: false },
|
|
64
|
+
deliverable: { egress: true, localFile: false },
|
|
65
|
+
'pet-brief': { egress: true, localFile: false },
|
|
66
|
+
'mcp.tool': { egress: true, localFile: false },
|
|
67
|
+
'dsh.tool': { egress: true, localFile: false },
|
|
68
|
+
unknown: { egress: true, localFile: false },
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
const SURFACE_ALIASES = Object.freeze({
|
|
72
|
+
chat: 'chat.input',
|
|
73
|
+
agent: 'agent.input',
|
|
74
|
+
capture: 'capture.input',
|
|
75
|
+
attachments: 'attachments.text',
|
|
76
|
+
attachment: 'attachments.text',
|
|
77
|
+
docreview: 'doc-review.text',
|
|
78
|
+
review: 'doc-review.text',
|
|
79
|
+
office: 'office.spec',
|
|
80
|
+
research: 'research.query',
|
|
81
|
+
pet: 'pet-brief',
|
|
82
|
+
mcp: 'mcp.tool',
|
|
83
|
+
dsh: 'dsh.tool',
|
|
84
|
+
safety: 'safety.check',
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
/** Normalize a surface name; unknown names fail closed to the generic surface. */
|
|
88
|
+
export function normalizeSurface(surface) {
|
|
89
|
+
if (surface == null || surface === '') return DEFAULT_SURFACE
|
|
90
|
+
const raw = String(surface).trim().toLowerCase()
|
|
91
|
+
const key = SURFACE_ALIASES[raw] || raw
|
|
92
|
+
return SURFACES[key] ? key : 'unknown'
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Policy for a normalized surface (local files are reviewed, not hard-blocked). */
|
|
96
|
+
export function surfacePolicy(surface) {
|
|
97
|
+
const key = normalizeSurface(surface)
|
|
98
|
+
return SURFACES[key] || SURFACES.unknown
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ---------------------------------------------------------------------------
|
|
102
|
+
// normalization: NFKC, zero-width, traditional -> simplified, whitespace
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
|
|
105
|
+
const ZERO_WIDTH_RE = /[\u00AD\u034F\u061C\u115F\u1160\u17B4\u17B5\u180B-\u180E\u200B-\u200F\u202A-\u202E\u2060-\u2064\u206A-\u206F\uFE00-\uFE0F\uFEFF\u{E0100}-\u{E01EF}]/gu
|
|
106
|
+
|
|
107
|
+
// Only characters needed by the rule matrix. This is normalization for
|
|
108
|
+
// MATCHING only; the original text is never rewritten for the caller.
|
|
109
|
+
const TRAD_TO_SIMP = Object.freeze({
|
|
110
|
+
'兒': '儿', '槍': '枪', '彈': '弹', '藥': '药', '殺': '杀', '竊': '窃',
|
|
111
|
+
'騙': '骗', '賭': '赌', '錢': '钱', '貸': '贷', '證': '证', '駕': '驾',
|
|
112
|
+
'齡': '龄', '賣': '卖', '買': '买', '傷': '伤', '襲': '袭', '擊': '击',
|
|
113
|
+
'軍': '军', '獨': '独', '國': '国', '愛': '爱', '亂': '乱', '華': '华',
|
|
114
|
+
'極': '极', '網': '网', '絡': '络', '組': '组', '織': '织', '揚': '扬',
|
|
115
|
+
'動': '动', '顛': '颠', '製': '制', '販': '贩', '穢': '秽', '強': '强',
|
|
116
|
+
'姦': '奸', '褻': '亵', '視': '视', '殘': '残', '廢': '废', '碼': '码',
|
|
117
|
+
'鑰': '钥', '賬': '账', '帳': '账', '戶': '户', '轉': '转', '詐': '诈',
|
|
118
|
+
'單': '单', '發': '发', '個': '个', '訊': '讯', '錄': '录', '惡': '恶',
|
|
119
|
+
'馬': '马', '驟': '骤', '換': '换', '臉': '脸', '擬': '拟', '聲': '声',
|
|
120
|
+
'偽': '伪', '學': '学', '約': '约', '見': '见', '戀': '恋', '無': '无',
|
|
121
|
+
'統': '统', '開': '开', '戲': '戏', '誘': '诱', '脅': '胁', '嚇': '吓',
|
|
122
|
+
'搶': '抢', '綁': '绑', '撥': '拨', '號': '号', '兇': '凶', '屍': '尸',
|
|
123
|
+
'體': '体', '腦': '脑', '炰': '炮', '菸': '烟', '紋': '纹', '輟': '辍',
|
|
124
|
+
'複': '复', '鍊': '炼', '燒': '烧', '慫': '怂', '慾': '欲', '蕩': '荡',
|
|
125
|
+
'艷': '艳', '親': '亲', '權': '权', '員': '员', '會': '会', '區': '区',
|
|
126
|
+
'縣': '县', '鄉': '乡', '鎮': '镇', '門': '门', '車': '车', '團': '团',
|
|
127
|
+
'夥': '伙', '僱': '雇', '傭': '佣',
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
const HAN_RE = /[\u4e00-\u9fff]/g
|
|
131
|
+
|
|
132
|
+
function toSimplified(text) {
|
|
133
|
+
return text.replace(HAN_RE, (char) => TRAD_TO_SIMP[char] || char)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Whitespace/punctuation insensitive view for CJK combo rules. Removing
|
|
138
|
+
* separators defeats "制 作 炸 弹" / "制作,炸弹" style evasions.
|
|
139
|
+
*/
|
|
140
|
+
const COMPACT_STRIP_RE = /[\s\u3000\u00A0,。、;:!?…—~·“”‘’「」『』()()《》〈〉【】〔〕\[\]{}<>,.!?;:'"`~!@#$%^&*_+=|\\/\-]/gu
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Normalize without changing semantics: NFKC (full/half width), zero-width
|
|
144
|
+
* removal, traditional -> simplified for matching, lowercase, collapsed spaces.
|
|
145
|
+
*/
|
|
146
|
+
export function normalizeText(input) {
|
|
147
|
+
const original = String(input == null ? '' : input)
|
|
148
|
+
const base = original.normalize('NFKC').replace(ZERO_WIDTH_RE, '')
|
|
149
|
+
const normalized = toSimplified(base).toLowerCase().replace(/\s+/gu, ' ').trim()
|
|
150
|
+
const compact = normalized.replace(COMPACT_STRIP_RE, '')
|
|
151
|
+
return { original, normalized, compact }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Negation window check, compact view only (e.g. "禁止传播儿童裸照"). */
|
|
155
|
+
function isNegated(compactText, index) {
|
|
156
|
+
const window = compactText.slice(Math.max(0, index - 6), index)
|
|
157
|
+
return /(不要|不得|严禁|禁止|防止|避免|警惕|防范|预防|拒绝|切勿|勿|反对|反诈|反家暴|反暴力|打击|查处|严惩)$/u.test(window)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// ---------------------------------------------------------------------------
|
|
161
|
+
// policy heuristics
|
|
162
|
+
// ---------------------------------------------------------------------------
|
|
163
|
+
|
|
164
|
+
// Quoted/legal/news/academic contexts downgrade a non-hard block one step.
|
|
165
|
+
const CONTEXT_MARKERS = /(普法|法律|刑法|民法典|法规|法条|条文|判刑|判决|案例|新闻|报道|通报|公告|依据|根据|引用|学术|论文|研究|教学|教材|课堂|培训|防范|预防|应对|自救|求助|支持|志愿者|干预|保护|反诈|维权|举报|打击|查处|严惩|治理|宣传|警示|警示教育|安全研究|渗透测试|授权|应急响应|样本分析|加固|修复|漏洞|通知|声明)/u
|
|
166
|
+
// First-person solicitation cancels the context downgrade (anti-bypass).
|
|
167
|
+
const HARD_SOLICIT = /(教我|告诉我|指导我|帮我|给我|我要|我想|我需要|求|请给|麻烦给)/u
|
|
168
|
+
|
|
169
|
+
function hasContextDowngrade(views) {
|
|
170
|
+
return CONTEXT_MARKERS.test(views.compact) && !HARD_SOLICIT.test(views.compact)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function rankOf(action) {
|
|
174
|
+
return actionRank(action)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function hmacEvidence(snippets, hmacKey) {
|
|
178
|
+
if (!hmacKey || (typeof hmacKey !== 'string' && !Buffer.isBuffer(hmacKey) && !(hmacKey instanceof Uint8Array))) {
|
|
179
|
+
return null
|
|
180
|
+
}
|
|
181
|
+
const unique = [...new Set(snippets)].slice(0, MAX_EVIDENCE_SNIPPETS)
|
|
182
|
+
if (!unique.length) return null
|
|
183
|
+
const material = unique.map((snippet) => String(snippet).slice(0, MAX_EVIDENCE_SNIPPET_CHARS)).sort().join('\u0000')
|
|
184
|
+
try {
|
|
185
|
+
return createHmac('sha256', hmacKey).update(material, 'utf8').digest('hex').slice(0, 16)
|
|
186
|
+
} catch {
|
|
187
|
+
return null
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
// core scan
|
|
193
|
+
// ---------------------------------------------------------------------------
|
|
194
|
+
|
|
195
|
+
function scanRule(rule, views) {
|
|
196
|
+
for (const view of rule.views) {
|
|
197
|
+
const text = view === 'normalized' ? views.normalized : views.compact
|
|
198
|
+
if (!text) continue
|
|
199
|
+
for (const pattern of rule.patterns) {
|
|
200
|
+
const match = pattern.exec(text)
|
|
201
|
+
if (!match) continue
|
|
202
|
+
if (rule.negationSensitive && view === 'compact' && isNegated(text, match.index)) continue
|
|
203
|
+
return { view, snippet: match[0] }
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return null
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function buildHit(rule, snippet, actionOverride) {
|
|
210
|
+
return {
|
|
211
|
+
ruleId: rule.id,
|
|
212
|
+
category: rule.category,
|
|
213
|
+
subtype: rule.subtype,
|
|
214
|
+
code: rule.code,
|
|
215
|
+
severity: rule.severity,
|
|
216
|
+
action: actionOverride || rule.action,
|
|
217
|
+
confidence: rule.confidence,
|
|
218
|
+
hardBlock: Boolean(rule.hardBlock),
|
|
219
|
+
crisis: Boolean(rule.crisis),
|
|
220
|
+
blockable: Boolean(rule.blockable),
|
|
221
|
+
downgraded: null,
|
|
222
|
+
snippet: snippet || rule.id,
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const SEVERITY_ORDER = { critical: 4, high: 3, medium: 2, low: 1 }
|
|
227
|
+
|
|
228
|
+
function allowResult(views, surface, ruleset, truncated) {
|
|
229
|
+
return {
|
|
230
|
+
action: 'allow',
|
|
231
|
+
categories: [],
|
|
232
|
+
ruleIds: [],
|
|
233
|
+
confidence: 0,
|
|
234
|
+
evidenceHash: null,
|
|
235
|
+
code: 'NONE',
|
|
236
|
+
rulesetVersion: ruleset.version,
|
|
237
|
+
rulesetDigest: ruleset.digest,
|
|
238
|
+
surface,
|
|
239
|
+
severity: 'none',
|
|
240
|
+
crisis: false,
|
|
241
|
+
downgraded: false,
|
|
242
|
+
truncated,
|
|
243
|
+
hits: [],
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** The stable fail-closed decision for a truncated payload on an egress surface. */
|
|
248
|
+
function truncationBlockResult(surface, ruleset) {
|
|
249
|
+
return {
|
|
250
|
+
action: 'block',
|
|
251
|
+
categories: [],
|
|
252
|
+
ruleIds: [],
|
|
253
|
+
confidence: 0.5,
|
|
254
|
+
evidenceHash: null,
|
|
255
|
+
code: 'INPUT_TRUNCATED',
|
|
256
|
+
rulesetVersion: ruleset.version,
|
|
257
|
+
rulesetDigest: ruleset.digest,
|
|
258
|
+
surface,
|
|
259
|
+
severity: 'medium',
|
|
260
|
+
crisis: false,
|
|
261
|
+
downgraded: false,
|
|
262
|
+
truncated: true,
|
|
263
|
+
hits: [],
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Fail closed when the text was truncated: the unscanned tail could contain a
|
|
269
|
+
* hard-block rule hit, so the prefix result cannot be trusted as 'allow'.
|
|
270
|
+
* Egress surfaces refuse the payload outright (and skip the expensive prefix
|
|
271
|
+
* scan: the verdict does not depend on it); local-file surfaces keep scanning
|
|
272
|
+
* so a hard block in the scanned prefix still wins, then ask for confirmation.
|
|
273
|
+
* Escalation only raises an action, never lowers one.
|
|
274
|
+
*/
|
|
275
|
+
function applyTruncationFailClosed(result, policy) {
|
|
276
|
+
if (!result.truncated) return result
|
|
277
|
+
const floor = policy.egress ? 'block' : 'confirm'
|
|
278
|
+
if (rankOf(result.action) >= rankOf(floor)) return result
|
|
279
|
+
return {
|
|
280
|
+
...result,
|
|
281
|
+
action: floor,
|
|
282
|
+
code: 'INPUT_TRUNCATED',
|
|
283
|
+
severity: result.severity === 'none' ? 'medium' : result.severity,
|
|
284
|
+
confidence: Math.max(result.confidence, 0.5),
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function runClassify(ruleset, text, options = {}) {
|
|
289
|
+
const surface = normalizeSurface(options.surface)
|
|
290
|
+
const policy = SURFACES[surface] || SURFACES.unknown
|
|
291
|
+
const maxChars = Number.isInteger(options.maxChars) && options.maxChars > 0 ? options.maxChars : MAX_SCAN_CHARS
|
|
292
|
+
const raw = String(text == null ? '' : text)
|
|
293
|
+
const truncated = raw.length > maxChars
|
|
294
|
+
// Egress + truncation can only end in block, so settle it before the rule
|
|
295
|
+
// scan (deterministic and bounded even for a multi-megabyte paste).
|
|
296
|
+
if (truncated && policy.egress) return truncationBlockResult(surface, ruleset)
|
|
297
|
+
const views = normalizeText(truncated ? raw.slice(0, maxChars) : raw)
|
|
298
|
+
if (!views.compact) return applyTruncationFailClosed(allowResult(views, surface, ruleset, truncated), policy)
|
|
299
|
+
|
|
300
|
+
const contextDowngrade = hasContextDowngrade(views)
|
|
301
|
+
const hits = []
|
|
302
|
+
const piiHits = detectPiiInternal(views.original, { ruleset })
|
|
303
|
+
|
|
304
|
+
for (const rule of ruleset.rules) {
|
|
305
|
+
if (rule.detector === 'pii') {
|
|
306
|
+
const first = piiHits.filter((hit) => hit.ruleId === rule.id)
|
|
307
|
+
if (!first.length) continue
|
|
308
|
+
const hit = buildHit(rule, first.map((item) => item.raw).join(' '))
|
|
309
|
+
hit.pii = true
|
|
310
|
+
hits.push(hit)
|
|
311
|
+
continue
|
|
312
|
+
}
|
|
313
|
+
if (rule.detector === 'pii-bulk') {
|
|
314
|
+
const enough = piiHits.length >= (rule.minPiiHits || 6)
|
|
315
|
+
const exfil = rule.exfilPattern ? rule.exfilPattern.exec(views.compact) : null
|
|
316
|
+
const quantity = rule.quantityPattern ? rule.quantityPattern.exec(views.compact) : null
|
|
317
|
+
if (!enough || !exfil || !quantity) continue
|
|
318
|
+
hits.push(buildHit(rule, exfil[0] + quantity[0]))
|
|
319
|
+
continue
|
|
320
|
+
}
|
|
321
|
+
const found = scanRule(rule, views)
|
|
322
|
+
if (!found) continue
|
|
323
|
+
hits.push(buildHit(rule, found.snippet))
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Block is narrow: only explicit block rules can block; the CSAM hard block
|
|
327
|
+
// is never downgraded. Everything else may be softened by context/surface.
|
|
328
|
+
for (const hit of hits) {
|
|
329
|
+
if (hit.action !== 'block' || hit.hardBlock) continue
|
|
330
|
+
if (contextDowngrade) {
|
|
331
|
+
hit.action = 'confirm'
|
|
332
|
+
hit.downgraded = 'context'
|
|
333
|
+
}
|
|
334
|
+
if (policy.localFile && hit.action === 'block') {
|
|
335
|
+
hit.action = 'confirm'
|
|
336
|
+
hit.downgraded = 'surface'
|
|
337
|
+
} else if (policy.localFile && hit.action === 'confirm') {
|
|
338
|
+
hit.action = 'warn'
|
|
339
|
+
hit.downgraded = 'surface'
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const crisis = hits.some((hit) => hit.crisis)
|
|
344
|
+
if (crisis) {
|
|
345
|
+
for (const hit of hits) {
|
|
346
|
+
if (hit.crisis && rankOf(hit.action) < rankOf('warn')) hit.action = 'warn'
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Deterministic ordering: action rank desc, then ruleset order (id sorted).
|
|
351
|
+
// Array.prototype.sort is stable, so equal-rank hits keep ruleset order.
|
|
352
|
+
const ordered = [...hits].sort((a, b) => rankOf(b.action) - rankOf(a.action))
|
|
353
|
+
const categories = [...new Set(ordered.map((hit) => hit.category))]
|
|
354
|
+
const ruleIds = [...new Set(ordered.map((hit) => hit.ruleId))]
|
|
355
|
+
const action = ordered.length ? ordered[0].action : 'allow'
|
|
356
|
+
const primary = ordered[0]
|
|
357
|
+
const baseConfidence = ordered.length
|
|
358
|
+
? Math.min(0.99, Math.max(...ordered.map((hit) => hit.confidence)) + 0.02 * Math.max(0, categories.length - 1))
|
|
359
|
+
: 0
|
|
360
|
+
const evidenceHash = hmacEvidence(ordered.map((hit) => hit.snippet), options.hmacKey)
|
|
361
|
+
|
|
362
|
+
const result = {
|
|
363
|
+
action,
|
|
364
|
+
categories,
|
|
365
|
+
ruleIds,
|
|
366
|
+
confidence: Math.round(baseConfidence * 100) / 100,
|
|
367
|
+
evidenceHash,
|
|
368
|
+
code: ordered.length ? ordered[0].code : 'NONE',
|
|
369
|
+
rulesetVersion: ruleset.version,
|
|
370
|
+
rulesetDigest: ruleset.digest,
|
|
371
|
+
surface,
|
|
372
|
+
severity: ordered.length ? ordered[0].severity : 'none',
|
|
373
|
+
crisis,
|
|
374
|
+
downgraded: ordered.some((hit) => Boolean(hit.downgraded)),
|
|
375
|
+
truncated,
|
|
376
|
+
hits: ordered.map((hit) => ({
|
|
377
|
+
ruleId: hit.ruleId,
|
|
378
|
+
category: hit.category,
|
|
379
|
+
subtype: hit.subtype,
|
|
380
|
+
code: hit.code,
|
|
381
|
+
severity: hit.severity,
|
|
382
|
+
action: hit.action,
|
|
383
|
+
confidence: hit.confidence,
|
|
384
|
+
hardBlock: hit.hardBlock,
|
|
385
|
+
crisis: hit.crisis,
|
|
386
|
+
downgraded: hit.downgraded,
|
|
387
|
+
})),
|
|
388
|
+
}
|
|
389
|
+
return applyTruncationFailClosed(result, policy)
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Classify text locally. Never throws on caller text; invalid options degrade
|
|
394
|
+
* safely (no hmacKey means no evidenceHash; unknown surface means generic).
|
|
395
|
+
*/
|
|
396
|
+
export function classify(text, options = {}) {
|
|
397
|
+
let ruleset = DEFAULT_RULESET
|
|
398
|
+
if (options.ruleset) {
|
|
399
|
+
try {
|
|
400
|
+
ruleset = options.ruleset.__compiled ? options.ruleset : resolveRuleset(options.ruleset)
|
|
401
|
+
} catch {
|
|
402
|
+
ruleset = DEFAULT_RULESET
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
try {
|
|
406
|
+
return runClassify(ruleset, text, options)
|
|
407
|
+
} catch {
|
|
408
|
+
// Fail closed on an internal bug: never let ordinary text slip past as allow.
|
|
409
|
+
return {
|
|
410
|
+
action: 'confirm',
|
|
411
|
+
categories: [],
|
|
412
|
+
ruleIds: [],
|
|
413
|
+
confidence: 0.5,
|
|
414
|
+
evidenceHash: null,
|
|
415
|
+
code: 'SAFETY_INTERNAL_ERROR',
|
|
416
|
+
rulesetVersion: ruleset.version,
|
|
417
|
+
rulesetDigest: ruleset.digest,
|
|
418
|
+
surface: normalizeSurface(options.surface),
|
|
419
|
+
severity: 'medium',
|
|
420
|
+
crisis: false,
|
|
421
|
+
downgraded: false,
|
|
422
|
+
truncated: false,
|
|
423
|
+
hits: [],
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/** Bind a ruleset (and default options such as hmacKey) to a classify function. */
|
|
429
|
+
export function createClassifier(config = {}) {
|
|
430
|
+
let ruleset = DEFAULT_RULESET
|
|
431
|
+
if (config.ruleset != null) ruleset = resolveRuleset(config.ruleset)
|
|
432
|
+
const defaults = {
|
|
433
|
+
surface: config.surface,
|
|
434
|
+
hmacKey: config.hmacKey,
|
|
435
|
+
maxChars: config.maxChars,
|
|
436
|
+
}
|
|
437
|
+
return (text, options = {}) => {
|
|
438
|
+
const merged = { ...defaults, ...options, ruleset }
|
|
439
|
+
return runClassify(ruleset, text, merged)
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// Kept for tests and for callers that want the severity ordering.
|
|
444
|
+
export const SEVERITY_RANK = Object.freeze(SEVERITY_ORDER)
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Human-readable safety explanations (the copy behind `advisers safety explain`).
|
|
3
|
+
*
|
|
4
|
+
* Contract: the public explanation may reveal the CATEGORY, action, ruleset
|
|
5
|
+
* version and evidence-hash prefix, but never the matched text or the rule
|
|
6
|
+
* keywords. Full rule tables stay private to reduce evasion.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { CATEGORY_BY_ID, RULESET_VERSION } from './rules.js'
|
|
10
|
+
|
|
11
|
+
export const EXPLAIN_FORMAT = 'advisers-safety-explain'
|
|
12
|
+
export const NOTICE_VERSION = 1
|
|
13
|
+
|
|
14
|
+
/** Crisis resources; self-harm is never blocked, only escalated. */
|
|
15
|
+
export const CRISIS_RESOURCES = Object.freeze({
|
|
16
|
+
mainlandHotline: '12356',
|
|
17
|
+
emergency: '120',
|
|
18
|
+
police: '110',
|
|
19
|
+
international: Object.freeze(['US 988', 'UK 116 123']),
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
const CODE_HINTS = Object.freeze({
|
|
23
|
+
CSAM: '检测到涉及未成年人的性剥削内容,已阻止处理。',
|
|
24
|
+
TERRORISM_OPERATIONS: '检测到恐怖主义操作指引,已阻止处理。',
|
|
25
|
+
ILLEGAL_INSTRUCTION: '检测到直接违法教唆(制毒、制爆、武器改装等),已阻止处理。',
|
|
26
|
+
MALWARE_WEAPONIZED: '检测到恶意代码或网络攻击武器化内容,已阻止处理。',
|
|
27
|
+
PII_BULK_EXFIL: '检测到批量个人信息外泄内容,已阻止处理。',
|
|
28
|
+
SELFHARM_CRISIS: '检测到自伤风险,已触发危机提示(内容未被阻断)。',
|
|
29
|
+
PII: '检测到个人信息或密钥,已按脱敏处理(原文不落盘)。',
|
|
30
|
+
PROMPT_INJECTION: '检测到提示注入或越权指令,已提示风险并加固系统边界。',
|
|
31
|
+
SAFETY_INTERNAL_ERROR: '本地审核出现异常,已按保守策略处理。',
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
const ACTION_LABELS = Object.freeze({
|
|
35
|
+
allow: '允许',
|
|
36
|
+
warn: '提醒',
|
|
37
|
+
confirm: '需确认',
|
|
38
|
+
redact: '脱敏',
|
|
39
|
+
block: '已拦截',
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
/** Public category label; no rule keywords are exposed. */
|
|
43
|
+
export function categoryLabel(categoryId) {
|
|
44
|
+
const category = CATEGORY_BY_ID[categoryId]
|
|
45
|
+
return category ? category.labelZh : '未分类风险'
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Evidence hashes are keyed one-way digests; show only a short prefix. */
|
|
49
|
+
export function formatEvidenceHash(hash) {
|
|
50
|
+
if (!hash) return '不可用(未配置审计密钥)'
|
|
51
|
+
return String(hash).slice(0, 8) + '…'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function categoriesText(result) {
|
|
55
|
+
const list = (result && result.categories) || []
|
|
56
|
+
if (!list.length) return '未分类风险'
|
|
57
|
+
return list.map(categoryLabel).join('、')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function seqText(seq) {
|
|
61
|
+
return seq == null ? '当前事件' : '事件 #' + String(seq)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function reportHint(seq) {
|
|
65
|
+
return seq == null ? 'advisers safety report' : 'advisers safety report ' + String(seq)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function explainHint(seq) {
|
|
69
|
+
return seq == null ? 'advisers safety explain' : 'advisers safety explain ' + String(seq)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Why did this happen? Used by `advisers safety explain <seq>`.
|
|
74
|
+
* Accepts the audit-row shape; never includes the original text.
|
|
75
|
+
*/
|
|
76
|
+
export function explainAudit(event = {}) {
|
|
77
|
+
const seq = event.seq
|
|
78
|
+
const surface = event.surface || 'unknown'
|
|
79
|
+
const action = event.action || 'warn'
|
|
80
|
+
const code = event.code || 'NONE'
|
|
81
|
+
const categories = event.categories && event.categories.length
|
|
82
|
+
? event.categories
|
|
83
|
+
: (event.category ? [event.category] : [])
|
|
84
|
+
const ruleIds = event.ruleIds || []
|
|
85
|
+
const evidenceHash = event.evidenceHash || null
|
|
86
|
+
const rulesetVersion = event.rulesetVersion || RULESET_VERSION
|
|
87
|
+
const at = event.at || ''
|
|
88
|
+
const hint = CODE_HINTS[code]
|
|
89
|
+
const lines = []
|
|
90
|
+
lines.push(seqText(seq) + (at ? ' · ' + at : ''))
|
|
91
|
+
lines.push('surface=' + surface + ' · 动作=' + (ACTION_LABELS[action] || action) + ' · 类别=' + categories.map(categoryLabel).join('、'))
|
|
92
|
+
lines.push('规则版本=' + rulesetVersion + ' · 规则=' + (ruleIds.length ? ruleIds.join(',') : '—'))
|
|
93
|
+
lines.push('证据哈希=' + formatEvidenceHash(evidenceHash) + '(不可逆)')
|
|
94
|
+
if (hint) lines.push('说明:' + hint)
|
|
95
|
+
lines.push('误判?' + reportHint(seq) + '。' + explainHint(seq) + '。完整规则表不公开,以避免被规避。')
|
|
96
|
+
return lines.join('\n')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Copy shown when a block decision stops a request. */
|
|
100
|
+
export function blockNotice(result = {}, options = {}) {
|
|
101
|
+
const seq = options.seq
|
|
102
|
+
const lines = []
|
|
103
|
+
lines.push('已拦截:这段内容可能涉及' + categoriesText(result) + ',Advisers 不能生成或代为处理。')
|
|
104
|
+
lines.push('你可以:改写需求(' + explainHint(seq) + ' 查看原因);如果是误判,' + reportHint(seq) + ' 反馈。')
|
|
105
|
+
if (result.crisis) lines.push(crisisNotice())
|
|
106
|
+
else lines.push('涉及未成年人或即时危险,请拨 110;紧急医疗请拨 120。')
|
|
107
|
+
return lines.join('\n')
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Copy shown when a warn decision lets the request continue with a notice. */
|
|
111
|
+
export function warnNotice(result = {}, options = {}) {
|
|
112
|
+
const seq = options.seq
|
|
113
|
+
const lines = []
|
|
114
|
+
lines.push('提醒:检测到可能的风险(' + categoriesText(result) + ')。内容已保留,请自行判断是否继续。')
|
|
115
|
+
lines.push('继续使用可能违反法律或平台规则;误判可执行 ' + reportHint(seq) + ' 反馈。')
|
|
116
|
+
return lines.join('\n')
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Copy shown when the user must explicitly confirm a risk (confirm action). */
|
|
120
|
+
export function confirmNotice(result = {}, options = {}) {
|
|
121
|
+
const seq = options.seq
|
|
122
|
+
const lines = []
|
|
123
|
+
lines.push('需要确认:检测到可能的风险(' + categoriesText(result) + ')。')
|
|
124
|
+
lines.push('继续意味着你自行承担相应责任;输入 y 继续,输入 n 取消。')
|
|
125
|
+
lines.push('如需了解原因:' + explainHint(seq) + ';误判反馈:' + reportHint(seq) + '。')
|
|
126
|
+
return lines.join('\n')
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Self-harm: never blocked, always escalate to crisis resources. */
|
|
130
|
+
export function crisisNotice() {
|
|
131
|
+
return [
|
|
132
|
+
'我很在意你现在的安全。先不要一个人扛着:请联系身边可信任的人,或拨打当地急救/心理援助热线。',
|
|
133
|
+
'中国大陆可拨 ' + CRISIS_RESOURCES.mainlandHotline + ';紧急医疗 ' + CRISIS_RESOURCES.emergency + ';报警 ' + CRISIS_RESOURCES.police + '。',
|
|
134
|
+
'如果你现在有立即伤害自己的打算,请直接拨打 ' + CRISIS_RESOURCES.emergency + '/' + CRISIS_RESOURCES.police + '。',
|
|
135
|
+
'(国际:' + CRISIS_RESOURCES.international.join(' / ') + '。)',
|
|
136
|
+
].join('\n')
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Pick the notice matching a classify() result. */
|
|
140
|
+
export function explainResult(result = {}, options = {}) {
|
|
141
|
+
switch (result.action) {
|
|
142
|
+
case 'block':
|
|
143
|
+
return blockNotice(result, options)
|
|
144
|
+
case 'confirm':
|
|
145
|
+
return confirmNotice(result, options)
|
|
146
|
+
case 'warn':
|
|
147
|
+
return result.crisis ? crisisNotice() : warnNotice(result, options)
|
|
148
|
+
case 'redact': {
|
|
149
|
+
const labels = (result.categories || []).map(categoryLabel).filter(Boolean)
|
|
150
|
+
return labels.length
|
|
151
|
+
? '已按个人信息保护要求脱敏(' + labels.join('、') + ')。'
|
|
152
|
+
: '已按个人信息保护要求脱敏。'
|
|
153
|
+
}
|
|
154
|
+
default:
|
|
155
|
+
return ''
|
|
156
|
+
}
|
|
157
|
+
}
|