From 7189f33812ffa8d9fa24124cf13840256d0406ad Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 4 Dec 2026 17:58:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=A4=84=E6=96=B9=20AI=20?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E8=AF=8A=E6=96=AD=E8=84=9A=E6=9C=AC=EF=BC=88?= =?UTF-8?q?=E5=8F=AA=E8=AF=BB=EF=BC=8C=E8=BE=93=E5=87=BA=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E9=98=B6=E6=AE=B5=E4=B8=8E=E6=A0=BC=E5=BC=8F=E6=8B=92=E7=BB=9D?= =?UTF-8?q?=E8=A7=84=E5=88=99=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5 <[EMAIL]> --- .../scripts/prescription_ai_task_diagnose.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 server/scripts/prescription_ai_task_diagnose.php diff --git a/server/scripts/prescription_ai_task_diagnose.php b/server/scripts/prescription_ai_task_diagnose.php new file mode 100644 index 000000000..0ad8dda08 --- /dev/null +++ b/server/scripts/prescription_ai_task_diagnose.php @@ -0,0 +1,70 @@ +initialize(); + +$arg = $argv[1] ?? 'recent'; +$query = Db::name('prescription_ai_task'); +if (ctype_digit($arg)) { + $query->where('batch_id', (int) $arg); +} else { + $query->where('model_key', 'openai')->whereIn('status', ['failed', 'retry_wait'])->order('id', 'desc')->limit(max(1, (int) ($argv[2] ?? 20))); +} +$tasks = $query->select()->toArray(); +$cipher = new PrescriptionAiCipher(); +$rules = []; +$errors = []; + +foreach ($tasks as $task) { + printf("task %d batch %d %s %s %s attempts=%d updated=%s\n", $task['id'], $task['batch_id'], $task['model_key'], $task['status'], + $task['error_code'], $task['total_attempts'], date('Y-m-d H:i', (int) $task['updated_at'])); + $errors[$task['error_code'] ?: '-'] = ($errors[$task['error_code'] ?: '-'] ?? 0) + 1; + foreach (Db::name('prescription_ai_attempt')->where('task_id', $task['id'])->order('attempt_no')->select()->toArray() as $attempt) { + printf(" attempt %d %s %s %s\n", $attempt['attempt_no'], $attempt['status'], $attempt['error_code'], date('m-d H:i', (int) $attempt['started_at'])); + } + if (empty($task['progress_cipher'])) { + echo " (无进度记录)\n"; + continue; + } + try { + $progress = $cipher->decrypt((string) $task['progress_cipher'], 'progress:' . $task['id']); + } catch (\Throwable) { + echo " (进度解密失败:本机密钥与写入时不一致)\n"; + continue; + } + printf(" stage=%s prompt=%s calls=%d\n", $progress['stage'] ?? '', $progress['prompt_version'] ?? '', $progress['usage']['total_calls'] ?? 0); + foreach (array_slice($progress['usage']['calls'] ?? [], -8) as $call) { + printf(" call %-26s ok=%d err=%-26s %6.1fs in=%d out=%s\n", $call['stage'] ?? '', (int) !empty($call['ok']), $call['error_code'] ?? '', + ((int) ($call['latency_ms'] ?? 0)) / 1000, (int) ($call['input_token_upper_bound'] ?? 0), + json_encode($call['usage']['completion_tokens'] ?? null)); + } + foreach ($progress['format_rejects'] ?? [] as $reject) { + $rules[$reject['rule'] ?? '?'] = ($rules[$reject['rule'] ?? '?'] ?? 0) + 1; + } + foreach (array_slice($progress['format_rejects'] ?? [], -6) as $reject) { + printf(" reject %-26s rule=%-16s len=%d %s\n", $reject['stage'] ?? '', $reject['rule'] ?? '', (int) ($reject['content_length'] ?? 0), + date('m-d H:i', (int) ($reject['at'] ?? 0))); + } +} + +arsort($errors); +arsort($rules); +echo "\n错误码分布 " . json_encode($errors, JSON_UNESCAPED_UNICODE) . "\n"; +echo "格式拒绝规则分布 " . json_encode($rules, JSON_UNESCAPED_UNICODE) . "\n"; +echo "已部署的拒答解析修复:" . (str_contains((string) file_get_contents(dirname(__DIR__) . '/app/common/service/prescriptionai/PrescriptionAiGenerator.php'), 'CANDIDATE_KEYS') ? '是' : '否') . "\n";