重构(半自动履约): 收敛状态同步并拆分任务服务

This commit is contained in:
yml
2026-11-18 14:35:15 +08:00
parent aa3743f39f
commit f76b9c452d
29 changed files with 3362 additions and 2570 deletions
@@ -36,6 +36,8 @@ export type SemiAutoTraceEntry = {
durationMs?: number
requestSummary?: Record<string, unknown>
requestBody?: unknown
/** 请求体里的通用 `code` 字段按验证码处理;响应体中的业务 code 保持原样。 */
maskRequestCode?: boolean
responseSummary?: Record<string, unknown>
responseBody?: unknown
errorMessage?: string
@@ -86,6 +88,7 @@ const SENSITIVE_KEYS = new Set([
'ticket',
'credential_payload_json',
])
const REQUEST_ONLY_SENSITIVE_KEYS = new Set(['code', 'rid', 'mfa_id'])
export function runWithSemiAutoTrace<T>(context: SemiAutoTraceContext, fn: () => T): T {
return traceStorage.run(context, fn)
@@ -106,29 +109,39 @@ function normalizeKey(key: string): string {
}
/** 深度脱敏:Cookie / Token / 密钥字段直接替换为 ******。 */
export function redactTraceValue(value: unknown, depth = 0): unknown {
export function redactTraceValue(
value: unknown,
depth = 0,
options: { maskRequestCode?: boolean } = {},
): unknown {
if (depth > MAX_REDACT_DEPTH) return '[max-depth]'
if (value === null || value === undefined) return value
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
return value
}
if (Array.isArray(value)) {
return value.slice(0, 50).map((item) => redactTraceValue(item, depth + 1))
return value.slice(0, 50).map((item) => redactTraceValue(item, depth + 1, options))
}
if (typeof value === 'object') {
const source = value as Record<string, unknown>
const result: Record<string, unknown> = {}
for (const [key, current] of Object.entries(source)) {
result[key] = SENSITIVE_KEYS.has(normalizeKey(key))
? maskValue(current)
: redactTraceValue(current, depth + 1)
const normalizedKey = normalizeKey(key)
result[key] =
SENSITIVE_KEYS.has(normalizedKey) ||
(options.maskRequestCode === true && REQUEST_ONLY_SENSITIVE_KEYS.has(normalizedKey))
? maskValue(current)
: redactTraceValue(current, depth + 1, options)
}
return result
}
return String(value)
}
export function stringifyTraceBody(value: unknown): string {
export function stringifyTraceBody(
value: unknown,
options: { maskRequestCode?: boolean } = {},
): string {
if (value === undefined || value === null) return ''
if (typeof value === 'string') {
return value.length > MAX_BODY_CHARS
@@ -136,7 +149,7 @@ export function stringifyTraceBody(value: unknown): string {
: value
}
try {
const text = JSON.stringify(redactTraceValue(value), null, 2)
const text = JSON.stringify(redactTraceValue(value, 0, options), null, 2)
return text.length > MAX_BODY_CHARS ? `${text.slice(0, MAX_BODY_CHARS)}\n...[truncated]` : text
} catch {
return String(value).slice(0, MAX_BODY_CHARS)
@@ -172,7 +185,9 @@ export function recordSemiAutoTrace(entry: SemiAutoTraceEntry): void {
const orderNo = entry.orderNo || context?.orderNo || ''
const requestId = entry.requestId || context?.requestId || ''
const status = entry.status === 'error' ? 'error' : 'ok'
const requestBody = stringifyTraceBody(entry.requestBody)
const requestBody = stringifyTraceBody(entry.requestBody, {
maskRequestCode: entry.maskRequestCode === true,
})
const responseBody = stringifyTraceBody(entry.responseBody)
void query(
`INSERT INTO semi_auto_trace_logs (