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";