Files
zyt/server/tests/DifyGatewayOutageClassificationTest.php
T
Your NameandClaude Opus 5 d199d66f8c 处方 AI:Dify 包装的模型网关故障改为可重试,不再按 UPSTREAM_REJECTED 直接失败
OpenAI 分支的 Dify 应用经 gpt.zhenyangtang.cn 网关调用模型,网关间歇返回
503 no_available_provider(无可用账号)。Dify 把工作流内模型节点失败统一包成
400 invalid_param「Run failed: ...」,与真正的参数错误同码,于是任务被判为
UPSTREAM_REJECTED 且不重试。12-05 09:41–10:30 该网关整段不可用,期间任务全部失败。

- upstreamErrorMetadata:带模型插件调用痕迹且属网关无可用账号、5xx/408/429、
  过载、超时的「Run failed」归为 unavailable → UPSTREAM_BUSY(可重试);
  404、鉴权、参数错误不受影响。
- 重试策略:UPSTREAM_BUSY / UPSTREAM_UNAVAILABLE 改为 2 分钟、10 分钟后重试,
  其余错误仍为 30 秒、2 分钟;总次数仍受 max_attempts 限制。
- 新增离线测试 DifyGatewayOutageClassificationTest,补充策略测试。

Co-Authored-By: Claude Opus 5 <[EMAIL]>
2026-12-05 10:57:16 +08:00

60 lines
3.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
/**
* Dify 把工作流内模型节点的失败包成 400 invalid_param「Run failed: ...」返回。
* 模型网关暂时无可用账号、5xx、超时等必须归为可重试的 UPSTREAM_BUSY,
* 配置性 404 与真正的参数错误仍是 UPSTREAM_REJECTED。纯离线,不发请求。
*/
require dirname(__DIR__) . '/vendor/autoload.php';
use app\common\service\DifyChatService;
$checks = 0;
$expect = static function (bool $ok, string $message) use (&$checks): void {
if (!$ok) { throw new RuntimeException($message); }
$checks++;
};
$call = static function (string $method, array $args) {
$reflection = new ReflectionMethod(DifyChatService::class, $method);
$reflection->setAccessible(true);
return $reflection->invokeArgs(null, $args);
};
$secret = 'fixture-only-secret';
$body = static fn (string $message): string => json_encode(['code' => 'invalid_param', 'message' => $message, 'status' => 400]);
$transient = [
// 2026-12-05 生产实际出现的网关故障形态
'Run failed: req_id: 0123456789 PluginInvokeError: {"args":{"description":"[models] Error: API request failed with status code 503: {\"error\":{\"message\":\"The gateway found no eligible account for this request.\",\"type\":\"server_error\",\"code\":\"no_available_provider\"}}"},"error_type":"InvokeError"} ' . $secret,
'Run failed: [openai_api_compatible] Error: req_id: ab12 PluginDaemonInternalServerError: killed by timeout',
"Run failed: req_id: ab12 PluginInvokeError: {\"args\":{},\"error_type\":\"ValueError\",\"message\":\"{'type': 'server_error', 'code': 'upstream_unavailable'}\"}",
"Run failed: req_id: ab12 PluginInvokeError: {\"args\":{},\"error_type\":\"ValueError\",\"message\":\"{'message': 'Our servers are currently overloaded. Please try again later.'}\"}",
'Run failed: [models] Error: API request failed with status code 408: request timeout',
'Run failed: [models] Error: API request failed with status code 502: bad gateway',
];
foreach ($transient as $message) {
$meta = $call('upstreamErrorMetadata', [json_decode($body($message), true)]);
$expect($meta['upstream_code'] === 'invalid_param' && $meta['upstream_reason'] === 'unavailable', 'transient: ' . substr($message, 0, 80));
$result = $call('formatUpstreamRejection', [['http_code' => 400, 'body' => $body($message)], 10]);
$expect($result['error_code'] === 'UPSTREAM_BUSY' && !str_contains(json_encode($result), $secret), 'retryable busy: ' . substr($message, 0, 80));
// 同一载荷以 SSE error 帧到达(HTTP 200)时走同一分类
$stream = $call('formatUpstreamRejection', [['http_code' => 200, 'upstream_error' => true,
'upstream_reason' => $meta['upstream_reason'], 'upstream_code' => $meta['upstream_code']], 10]);
$expect($stream['error_code'] === 'UPSTREAM_BUSY', 'stream frame busy: ' . substr($message, 0, 80));
}
$rejected = [
'Run failed: [models] Error: API request failed with status code 404: 404 page not found',
'query is required',
'Run failed: [models] Error: API request failed with status code 401: invalid api key',
'inputs too long',
];
foreach ($rejected as $message) {
$result = $call('formatUpstreamRejection', [['http_code' => 400, 'body' => $body($message)], 10]);
$expect($result['error_code'] !== 'UPSTREAM_BUSY', 'not an outage: ' . $message);
}
echo "Dify gateway outage classification (no network): {$checks} checks OK\n";