This commit is contained in:
Your Name
2026-10-23 13:57:11 +08:00
parent ff98014fa2
commit e86ee4ae66
28 changed files with 3310 additions and 66 deletions
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
// Explicit local opt-in: private Python runtime and one pre-existing synthetic PNG.
require dirname(__DIR__) . '/app/common/service/ClinicalImageTextService.php';
use app\common\service\ClinicalImageTextService;
if ($argc !== 3) throw new RuntimeException('usage: php test.php private-python synthetic-fixture');
$python = realpath($argv[1]); $fixture = realpath($argv[2]);
if ($python === false || $fixture === false) throw new RuntimeException('local fixture/runtime missing');
$sources = [['index' => 77, 'path' => $fixture]];
$config = ['enabled' => true, 'python' => $python, 'timeout_seconds' => 20,
'threads' => 1, 'detection_limit' => 640, 'max_text_bytes' => 4096];
$before = glob(sys_get_temp_dir() . '/clinical-ocr-*') ?: [];
$result = ClinicalImageTextService::read($sources, dirname($fixture), microtime(true) + 20, null, $config);
if ($result['status'] !== 'completed' || $result['images'][0]['index'] !== 77 || !$result['images'][0]['lines']) {
throw new RuntimeException('synthetic OCR bridge unavailable; safe status=' . $result['status'] . ' reason=' . ($result['reason'] ?? 'none'));
}
// Deliberately persist/print neither recognized values nor boxes.
$lines = count($result['images'][0]['lines']); unset($result);
$started = microtime(true);
$timeout = ClinicalImageTextService::read($sources, dirname($fixture), microtime(true) + 0.06, null, $config);
if (microtime(true) - $started > 1.0 || ($timeout['reason'] ?? '') !== 'deadline') throw new RuntimeException('real child deadline was not bounded');
$checks = 0;
$cancel = ClinicalImageTextService::read($sources, dirname($fixture), microtime(true) + 20,
static function () use (&$checks): bool { return ++$checks > 1; }, $config);
if (($cancel['reason'] ?? '') !== 'cancelled') throw new RuntimeException('real child cancellation not observed');
$after = glob(sys_get_temp_dir() . '/clinical-ocr-*') ?: [];
if (array_diff($after, $before)) throw new RuntimeException('new request-private OCR files remain');
echo 'Local synthetic OCR bridge: processed=1, lines=' . $lines . ", deadline+cancel+cleanup OK; raw values/boxes saved=0\n";
@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
namespace app\common\service {
function proc_open($command, $descriptors, &$pipes, $cwd, $env, $options) {
$GLOBALS['ocrCommands'][] = $command;
if (count($command) !== 2 || !str_ends_with($command[1], 'clinical_image_ocr.py') || !$options['bypass_shell']) throw new \RuntimeException('unsafe command');
if (isset($env['PYTHONPATH']) || isset($env['SYNTHETIC_PRIVATE_SECRET'])) throw new \RuntimeException('unfiltered environment');
foreach ($descriptors as $descriptor) {
if (!is_resource($descriptor)) throw new \RuntimeException('must use private file descriptors');
$GLOBALS['ocrTemporaryPaths'][] = stream_get_meta_data($descriptor)['uri'];
if (PHP_OS_FAMILY !== 'Windows' && (fstat($descriptor)['mode'] & 0777) !== 0600) throw new \RuntimeException('private file mode');
}
rewind($descriptors[0]);
$request = json_decode(stream_get_contents($descriptors[0]), true);
$GLOBALS['ocrLastRequest'] = $request;
if (count($request['images']) !== 2 || $request['images'][0]['index'] !== 7 || $request['images'][1]['index'] !== 19) throw new \RuntimeException('one process must cover every global source');
$scenario = $GLOBALS['ocrScenario'];
if ($scenario === 'spawn') return false;
$rows = array_map(static fn (array $source): array => ['index' => $source['index'], 'status' => 'processed',
'lines' => [['text' => 'SYNTHETIC_TEXT', 'confidence' => 0.9, 'box' => [[0, 0], [1, 0], [1, 1], [0, 1]]]]], $request['images']);
$result = ['status' => 'completed', 'images' => $rows];
if ($scenario === 'missing') array_pop($result['images']);
if ($scenario === 'duplicate') $result['images'][1]['index'] = 7;
if ($scenario === 'unknown') $result['images'][1]['index'] = 999;
if ($scenario === 'confidence') $result['images'][0]['lines'][0]['confidence'] = 2;
if ($scenario === 'box') $result['images'][0]['lines'][0]['box'][0][0] = 'bad';
if ($scenario === 'control') $result['images'][0]['lines'][0]['text'] = "bad\0text";
if ($scenario === 'no-lines') $result['images'][0]['lines'] = [];
if ($scenario === 'unavailable') $result['status'] = 'unavailable';
if ($scenario === 'partial') $result['images'][1] = ['index' => 19, 'status' => 'unreadable', 'lines' => []];
fwrite($descriptors[1], $scenario === 'malformed' ? '{' : json_encode($result));
if ($scenario === 'overflow') fwrite($descriptors[1], str_repeat('x', 524289));
fwrite($descriptors[2], 'SYNTHETIC_PRIVATE_STDERR');
$GLOBALS['ocrRunning'] = in_array($scenario, ['timeout', 'cancel'], true);
return fopen('php://temp', 'w+b');
}
function proc_get_status($process): array { return ['running' => $GLOBALS['ocrRunning'], 'exitcode' => $GLOBALS['ocrScenario'] === 'exit' ? 1 : 0]; }
function proc_terminate($process, $signal = 15): bool { $GLOBALS['ocrRunning'] = false; $GLOBALS['ocrTerminated']++; return true; }
function proc_close($process): int {
if ($GLOBALS['ocrRunning']) throw new \RuntimeException('must never close a running process');
$GLOBALS['ocrClosed']++; fclose($process); return 0;
}
}
namespace {
require dirname(__DIR__) . '/app/common/service/ClinicalImageTextService.php';
use app\common\service\ClinicalImageTextService;
function ocrExpect(bool $condition, string $message): void { if (!$condition) throw new RuntimeException($message); }
$directory = sys_get_temp_dir() . '/clinical-ocr-fixture-' . bin2hex(random_bytes(8));
mkdir($directory, 0700);
$image = $directory . '/image.png';
file_put_contents($image, base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII='));
$images = [['index' => 7, 'path' => $image], ['index' => 19, 'path' => $image]];
$config = ['enabled' => true, 'python' => PHP_BINARY, 'timeout_seconds' => 1, 'threads' => 1, 'detection_limit' => 640, 'max_text_bytes' => 4096];
$ocrCommands = []; $ocrTemporaryPaths = []; $ocrClosed = 0; $ocrTerminated = 0; $ocrRunning = false;
putenv('SYNTHETIC_PRIVATE_SECRET=not-for-child');
try {
$disabled = ClinicalImageTextService::read($images, $directory, microtime(true) + 1, null, ['enabled' => false]);
ocrExpect($disabled === ['status' => 'disabled', 'images' => []] && $ocrCommands === [], 'disabled feature performs no subprocess work');
foreach (['success', 'partial', 'missing', 'duplicate', 'unknown', 'confidence', 'box', 'control', 'no-lines',
'malformed', 'overflow', 'exit', 'spawn', 'timeout', 'cancel', 'unavailable'] as $ocrScenario) {
$ocrRunning = false;
$before = count($ocrCommands); $checks = 0;
$abort = $ocrScenario === 'cancel' ? static function () use (&$checks): bool { return ++$checks > 1; } : null;
$result = ClinicalImageTextService::read($images, $directory, microtime(true) + ($ocrScenario === 'timeout' ? 0.06 : 1), $abort, $config);
ocrExpect(count($ocrCommands) === $before + 1, 'one subprocess per complete source set');
ocrExpect(count($result['images']) === 2 && array_column($result['images'], 'index') === [7, 19], 'every input index explicitly covered on every outcome');
if ($ocrScenario === 'success') ocrExpect($result['status'] === 'completed', 'valid complete output');
elseif (in_array($ocrScenario, ['partial', 'missing', 'unavailable', 'timeout', 'cancel'], true)) ocrExpect($result['status'] === 'partial', 'checkpoint/missing rows never claim a fully completed set');
else ocrExpect($result['status'] === 'unavailable', 'invalid child output is safe optional unavailability');
ocrExpect(!str_contains(json_encode($result), 'SYNTHETIC_PRIVATE_STDERR'), 'stderr never reaches callers');
foreach ($ocrTemporaryPaths as $path) ocrExpect(!file_exists($path), 'private request descriptors are cleaned after every outcome');
}
foreach (['duplicate', 'outside', 'url', 'unc', 'relative', 'index', 'pixel', 'bytes'] as $invalid) {
$selected = $images;
if ($invalid === 'duplicate') $selected[1]['index'] = 7;
elseif ($invalid === 'index') $selected[0]['index'] = true;
elseif ($invalid === 'outside') $selected[0]['path'] = __FILE__;
elseif ($invalid === 'url') $selected[0]['path'] = 'https://synthetic.invalid/image.png';
elseif ($invalid === 'unc') $selected[0]['path'] = '\\\\synthetic.invalid\\share\\image.png';
elseif ($invalid === 'relative') $selected[0]['path'] = 'image.png';
elseif ($invalid === 'bytes') {
$huge = $directory . '/large.png'; $handle = fopen($huge, 'w+b'); ftruncate($handle, 20971521); fclose($handle); $selected[0]['path'] = $huge;
} else {
$huge = $directory . '/pixels.bmp';
file_put_contents($huge, 'BM' . pack('VvvV', 54, 0, 0, 54) . pack('VllvvVVllVV', 40, 10000, 10000, 1, 24, 0, 0, 0, 0, 0, 0));
$selected[0]['path'] = $huge;
}
$before = count($ocrCommands);
$result = ClinicalImageTextService::read($selected, $directory, microtime(true) + 1, null, $config);
ocrExpect($result['status'] === 'unavailable' && count($ocrCommands) === $before, 'invalid source rejected before process creation: ' . $invalid);
}
ocrExpect($ocrTerminated >= 2 && $ocrClosed >= 2, 'deadline/cancel terminate and reap owned subprocesses');
echo "ClinicalImageTextService offline descriptors/coverage/limits/cancellation: 25 scenarios OK\n";
} finally {
putenv('SYNTHETIC_PRIVATE_SECRET');
foreach (glob($directory . '/*') ?: [] as $path) unlink($path);
rmdir($directory);
}
}
@@ -62,6 +62,21 @@ errorReceiptExpect($safeSkipped['items'][0]['status'] === 'skipped' && $safeSkip
'final errors preserve safe missing-source facts and separate counters');
errorReceiptExpect(!str_contains(json_encode($safeSkipped), 'private.invalid')
&& !str_contains(json_encode($safeSkipped), 'private-error-body'), 'skipped error metadata never includes resource URLs or private reasons');
$imageEvidence = $result;
$imageEvidence['items'] = [
['kind' => 'image', 'status' => 'submitted', 'text_check_status' => 'withheld',
'evidence_status' => 'unverified', 'evidence_reason' => 'context_budget', 'raw_text' => 'PRIVATE_OCR_TEXT'],
['kind' => 'image', 'status' => 'submitted', 'text_check_status' => 'private-runtime-error',
'evidence_status' => 'unverified', 'evidence_reason' => 'private-provider-error'],
];
$format->invoke(null, [], $imageEvidence);
$safeEvidence = DiagnosisAiLogic::getAssistantErrorMeta();
errorReceiptExpect($safeEvidence['items'][0]['text_check_status'] === 'withheld'
&& $safeEvidence['items'][0]['evidence_reason'] === 'context_budget'
&& !isset($safeEvidence['items'][1]['text_check_status'], $safeEvidence['items'][1]['evidence_reason'])
&& !str_contains(json_encode($safeEvidence), 'PRIVATE_OCR_TEXT')
&& !str_contains(json_encode($safeEvidence), 'private-provider-error'),
'only fixed image text and synthesis-gap enums survive the public receipt, never raw OCR or provider errors');
$success = $format->invoke(null, [], ['ok' => true, 'content' => '合成回复', 'transfer_manifest_final' => true,
'required_sections' => AiRequiredSource::manifest([]), 'items' => [['kind' => 'image', 'status' => 'skipped', 'reason' => 'not_found']],
'attachment_count' => 1, 'submitted_attachment_count' => 0, 'skipped_attachment_count' => 1,
@@ -0,0 +1,287 @@
<?php
declare(strict_types=1);
// Offline transport fixtures only: no bootstrap, .env, credentials, PHI or network.
namespace think\facade {
class Log { public static function warning(string $message): void { $GLOBALS['batchLogs'][] = $message; } }
}
namespace app\common\service {
require __DIR__ . '/fixtures/CancellableCurlMultiStub.php';
class ClinicalKnowledgeReference {
public static function ancientBookSources(string $query): array { $GLOBALS['batchReferenceQueries'][] = $query; return []; }
public static function augmentQueryWithAncientBookSources(string $query, array $sources): string { return $query . "\nSYNTHETIC_REFERENCE"; }
}
class DifyRequiredAttachmentUpload {
public static function prepare(string $endpoint, string $key, string $user, array $files, int $timeout,
?callable $shouldAbort = null, array $options = []): array {
$GLOBALS['batchUploads'][] = compact('endpoint', 'user', 'files', 'timeout');
$wire = []; $indices = []; $skipped = [];
foreach ($files as $index => $file) {
$id = (int) pathinfo(parse_url($file['url'], PHP_URL_PATH), PATHINFO_FILENAME);
if ($GLOBALS['batchScenario'] === 'upload-ambiguous') return ['ok' => false, 'error_code' => 'REQUIRED_FILES_UPLOAD_FAILED'];
if ($GLOBALS['batchScenario'] === 'upload-size' && $id === 4) return ['ok' => false, 'error_code' => 'REQUIRED_FILES_TOO_LARGE'];
if ($GLOBALS['batchScenario'] === 'all-missing' || ($GLOBALS['batchScenario'] === 'missing' && in_array($id, [1, 4, 7], true))
|| in_array($id, $GLOBALS['batchMissingIds'] ?? [], true)) {
$skipped[] = ['index' => $index + 1, 'kind' => $file['type'], 'reason' => 'not_found'];
if (is_callable($options['on_upload'] ?? null)) ($options['on_upload'])(['index' => $index + 1, 'status' => 'skipped', 'reason' => 'not_found']);
continue;
}
foreach (['uploading', 'uploaded'] as $status) if (is_callable($options['on_upload'] ?? null)) {
($options['on_upload'])(['index' => $index + 1, 'status' => $status]);
}
$wire[] = ['type' => $file['type'], 'transfer_method' => 'local_file', 'upload_file_id' => 'synthetic-upload-' . $id];
$indices[] = $index + 1;
}
return ['ok' => true, 'files' => $wire, 'uploaded_indices' => $indices, 'skipped' => $skipped];
}
}
function config(string $name): array { return $GLOBALS['batchConfig']; }
function batchFixtureQuery(array $payload): string {
if (isset($payload['query'])) return $payload['query'];
$texts = [];
foreach ($payload['messages'] ?? [] as $message) {
if (is_string($message['content'] ?? null)) $texts[] = $message['content'];
elseif (is_array($message['content'] ?? null)) foreach ($message['content'] as $part) {
if (($part['type'] ?? '') === 'text') $texts[] = $part['text'];
}
}
return implode("\n", $texts);
}
function batchFixtureWireIds(array $payload): array {
if (isset($payload['files'])) return array_column($payload['files'], 'upload_file_id');
$ids = [];
foreach ($payload['messages'] ?? [] as $message) if (is_array($message['content'] ?? null)) foreach ($message['content'] as $part) {
if (($part['type'] ?? '') === 'image_url') $ids[] = 'synthetic-upload-'
. (int) pathinfo(parse_url($part['image_url']['url'], PHP_URL_PATH), PATHINFO_FILENAME);
}
return $ids;
}
function curl_init(): \stdClass { return (object) ['errno' => 0]; }
function curl_setopt_array(\stdClass $handle, array $options): bool { $handle->options = $options; return true; }
function curl_exec(\stdClass $handle): string|bool {
$options = $handle->options;
$payload = json_decode($options[CURLOPT_POSTFIELDS], true);
if (str_ends_with($options[CURLOPT_URL], '/stop')) { $handle->code = 200; return '{"result":"success"}'; }
$GLOBALS['batchRequests'][] = ['payload' => $payload, 'timeout' => $options[CURLOPT_TIMEOUT], 'url' => $options[CURLOPT_URL]];
$scenario = $GLOBALS['batchScenario'];
$extract = str_starts_with(batchFixtureQuery($payload), '【附件分批证据提取】');
$wire = $payload['files'] ?? [];
$ids = batchFixtureWireIds($payload);
$dify = str_ends_with($options[CURLOPT_URL], '/chat-messages');
$error = null;
$handle->code = 200;
if ($scenario === 'mapping-fallback' && $dify) {
$handle->code = 404; $error = ['code' => 'not_found'];
} elseif ($extract && (($scenario === 'count-two' && count($wire) > 2)
|| ($scenario === 'mapping-count-one' && count($ids) > 1))) {
$handle->code = 400; $error = ['code' => 'too_many_files', 'message' => 'Number of files exceeds the limit'];
} elseif ($extract && in_array($scenario, ['reject', 'reject-sse'], true) && in_array('synthetic-upload-4', $ids, true)) {
$handle->code = $scenario === 'reject-sse' ? 200 : 415;
$error = ['code' => 'unsupported_file_type', 'message' => 'SYNTHETIC_PRIVATE_ERROR https://private.invalid/file'];
} elseif ($extract && in_array($scenario, ['ambiguous', 'authentication', 'quota', 'server'], true)) {
$handle->code = ['ambiguous' => 413, 'authentication' => 401, 'quota' => 429, 'server' => 503][$scenario];
$error = ['code' => 'invalid_param', 'message' => 'SYNTHETIC_PRIVATE_ERROR'];
} elseif ($scenario === 'partial' && !$extract) {
$error = ['code' => 'unsupported_file_type'];
}
if ($extract && $scenario === 'deadline') usleep(1100000);
if ($extract && $scenario === 'cancel') $GLOBALS['batchCancelled'] = true;
if (isset($options[CURLOPT_HEADERFUNCTION])) ($options[CURLOPT_HEADERFUNCTION])($handle, 'HTTP/1.1 ' . $handle->code . " Synthetic\r\n");
$stream = ($payload['response_mode'] ?? '') === 'streaming' || !empty($payload['stream']);
if ($error !== null && (!$stream || $handle->code !== 200)) {
$body = json_encode($error);
if ($stream) { ($options[CURLOPT_WRITEFUNCTION])($handle, $body); return true; }
return $body;
}
$answer = 'SYNTHETIC_FINAL_ANSWER';
if ($extract) {
preg_match_all('/实际附件\d+ => 原始附件#(\d+)/u', batchFixtureQuery($payload), $map);
$rows = [];
foreach ($ids as $position => $id) $rows[] = ['index' => (int) ($map[1][$position] ?? 0),
'status' => 'read', 'findings' => 'EVIDENCE_' . $id];
$answer = json_encode(['files' => $rows]);
}
if (!$stream) return json_encode($dify ? ['answer' => $answer] : ['choices' => [['message' => ['content' => $answer]]]]);
$write = $options[CURLOPT_WRITEFUNCTION];
if (!$dify) {
$write($handle, 'data: ' . json_encode(['choices' => [['delta' => ['content' => $answer]]]]) . "\n\n");
$write($handle, "data: [DONE]\n\n");
return true;
}
$write($handle, 'data: ' . json_encode(['event' => 'message', 'task_id' => 'synthetic-task-' . count($GLOBALS['batchRequests']), 'answer' => $answer]) . "\n\n");
if ($error !== null) $write($handle, 'data: ' . json_encode(['event' => 'error'] + $error) . "\n\n");
else $write($handle, "data: {\"event\":\"message_end\"}\n\n");
return true;
}
function curl_errno(\stdClass $handle): int { return $handle->errno; }
function curl_getinfo(\stdClass $handle, int $option): int { return $handle->code; }
function curl_close(\stdClass $handle): void {}
}
namespace {
require dirname(__DIR__) . '/app/common/service/DifyChatService.php';
use app\common\service\DifyChatService;
function batchExpect(bool $condition, string $message): void { if (!$condition) throw new RuntimeException($message); }
$batchConfig = ['enable' => true, 'base_url' => 'https://synthetic.invalid/v1/chat-messages', 'timeout' => 20,
'required_attachment_transport' => 'upload', 'max_files' => 3,
'models' => ['qwen' => ['name' => 'synthetic', 'api_key' => 'SYNTHETIC_KEY', 'max_files' => 3]]];
$files = array_map(static fn (int $index): array => ['type' => 'image', 'url' => 'https://synthetic.invalid/' . $index . '.png'], range(1, 7));
$sections = array_map(static fn (int $index): array => ['key' => 'source_' . $index, 'label' => '栏目' . $index,
'count' => 1, 'availability' => 'present', 'status' => 'prepared'], range(1, 13));
$query = implode("\n", array_map(static fn (int $index): string => 'ORIGINAL_SECTION_' . $index . ':' . str_repeat('合成正文', 120), range(1, 13))) . "\nORIGINAL_TASK_JSON_SCHEMA";
$inputs = ['original_task' => 'synthetic_task', 'full_context' => $query];
$reset = static function (string $scenario): void {
$GLOBALS['batchScenario'] = $scenario; $GLOBALS['batchRequests'] = []; $GLOBALS['batchUploads'] = [];
$GLOBALS['batchLogs'] = []; $GLOBALS['batchCancelled'] = false; $GLOBALS['batchReferenceQueries'] = [];
$GLOBALS['batchMissingIds'] = [];
};
foreach ([false, true] as $streaming) {
foreach (array_merge(['success', 'missing', 'all-missing', 'reject', 'count-two', 'upload-size'], $streaming ? ['reject-sse'] : []) as $scenario) {
$reset($scenario); $deltas = []; $events = []; $tasks = []; $ends = [];
$options = ['required_files' => true, 'required_sections' => $sections,
'on_progress' => static function (array $event) use (&$events): void { $events[] = $event; },
'on_task' => static function (...$args) use (&$tasks): void { $tasks[] = $args; },
'on_transfer_end' => static function (...$args) use (&$ends): void { $ends[] = $args; }];
$result = $streaming ? DifyChatService::streamChat('qwen', $inputs, $query, 'same-synthetic-user',
static function (string $delta) use (&$deltas): bool { $deltas[] = $delta; return true; }, null, $files, $options)
: DifyChatService::chat('qwen', $inputs, $query, 'same-synthetic-user', $files, $options);
batchExpect(!empty($result['ok']), 'every known attachment gap still permits final original task: ' . $scenario);
batchExpect($result['attachment_count'] === 7 && $result['attachment_mode'] === 'batch_evidence'
&& $result['attachment_transfer'] === 'uploaded_files' && count($result['items']) === 7, 'full seven-item safe receipt survives synthesis');
$missing = $scenario === 'all-missing' ? 7 : ($scenario === 'missing' ? 3 : 0);
$rejected = in_array($scenario, ['reject', 'reject-sse', 'upload-size'], true) ? 1 : 0;
batchExpect($result['submitted_attachment_count'] === 7 - $missing - $rejected
&& $result['skipped_attachment_count'] === $missing && $result['rejected_attachment_count'] === $rejected,
'accepted, missing and rejected sources are disjoint and complete');
batchExpect(array_column($result['items'], 'index') === range(1, 7)
&& array_column($result['items'], 'label') === array_map(static fn (int $id): string => '图片附件' . $id, range(1, 7)), 'original labels and positions survive each batch');
batchExpect(count($result['required_sections']) === 13 && array_unique(array_column($result['required_sections'], 'status')) === ['submitted'], 'all thirteen original body sections finally submitted');
$final = $batchRequests[count($batchRequests) - 1]['payload'];
batchExpect(str_starts_with($final['query'], $query) && $final['inputs'] === $inputs && ($final['files'] ?? []) === [], 'final original full text/schema/inputs are preserved without falsely retransmitting original files');
batchExpect(str_contains($final['query'], '最终单次请求未重新携带原始附件') && str_contains($result['attachment_notice'], '分批'), 'final task and envelope truthfully identify batch evidence');
if ($scenario === 'all-missing') batchExpect(!str_contains($final['query'], 'EVIDENCE_'), 'a response without any available source is never promoted into attachment evidence');
$covered = [];
foreach ($batchRequests as $request) {
batchExpect($request['url'] === 'https://synthetic.invalid/v1/chat-messages' && $request['payload']['user'] === 'same-synthetic-user', 'all calls retain same app and user');
batchExpect(count($request['payload']['files'] ?? []) <= 3, 'no request exceeds configured app cap');
if (str_starts_with($request['payload']['query'], '【附件分批证据提取】')) {
batchExpect(!str_contains($request['payload']['query'], 'SYNTHETIC_REFERENCE'), 'intermediate evidence receives no ancient-book augmentation');
foreach ($request['payload']['files'] ?? [] as $wire) $covered[$wire['upload_file_id']] = true;
}
}
if ($scenario === 'success') {
batchExpect(count($batchRequests) === 4 && array_map(static fn (array $request): int => count($request['payload']['files'] ?? []), $batchRequests) === [3, 3, 1, 0], 'seven sources use exactly 3+3+1 and one synthesis');
batchExpect(array_keys($covered) === array_map(static fn (int $id): string => 'synthetic-upload-' . $id, range(1, 7)), 'every original source is covered exactly once');
foreach (range(1, 7) as $id) batchExpect(str_contains($final['query'], 'synthetic-upload-' . $id), 'each batch evidence is retained in full final query');
batchExpect(count($batchReferenceQueries) === 1, 'clinical reference is added to final synthesis only');
}
if (in_array($scenario, ['reject', 'reject-sse', 'count-two'], true)) batchExpect(count($batchUploads) === 3, 'bisection reuses already uploaded app/user IDs');
if ($streaming) batchExpect($deltas === ['SYNTHETIC_FINAL_ANSWER'] && count($ends) === count($batchRequests)
&& count($tasks) >= 1, 'only final clinical delta is delivered and every child transfer ends with task callbacks preserved');
foreach ($events as $event) if (!str_contains($event['message'] ?? '', '分批提交提取证据')) {
$isFinal = ($event['phase'] ?? '') === 'upstream' || str_contains($event['message'] ?? '', '资料正文与附件引用已提交');
if (!$isFinal && ($event['attachment_mode'] ?? '') === 'batch_evidence' && ($event['status'] ?? '') === 'uploading') {
batchExpect(array_unique(array_column($event['required_sections'], 'status')) === ['prepared'], 'body sections stay prepared during attachment extraction uploads');
}
}
batchExpect(!str_contains(json_encode([$result, $events, $batchLogs]), 'SYNTHETIC_PRIVATE_ERROR')
&& !str_contains(json_encode([$result, $events, $batchLogs]), 'SYNTHETIC_KEY'), 'safe callbacks/results/logs never expose provider message, URL or key');
}
foreach (['ambiguous', 'authentication', 'quota', 'server', 'upload-ambiguous'] as $scenario) {
$reset($scenario);
$options = ['required_files' => true, 'knowledge_mode' => 'evidence', 'required_sections' => $sections];
$result = $streaming ? DifyChatService::streamChat('qwen', $inputs, $query, 'same-synthetic-user', static fn (): bool => true, null, $files, $options)
: DifyChatService::chat('qwen', $inputs, $query, 'same-synthetic-user', $files, $options);
batchExpect(empty($result['ok']) && count($batchRequests) === ($scenario === 'upload-ambiguous' ? 0 : 1), 'ambiguous/auth/quota/server failure never removes files and synthesizes');
}
foreach (['cancel', 'deadline'] as $scenario) {
$reset($scenario); $stops = []; $ends = 0;
$options = ['required_files' => true, 'timeout' => $scenario === 'deadline' ? 1 : 20, 'knowledge_mode' => 'evidence',
'should_abort' => static fn (): bool => $GLOBALS['batchCancelled'],
'on_stop' => static function (...$args) use (&$stops): void { $stops[] = $args; },
'on_transfer_end' => static function (...$args) use (&$ends): void { $ends++; }];
$result = $streaming ? DifyChatService::streamChat('qwen', [], $query, 'same-synthetic-user', static fn (): bool => true,
$options['should_abort'], $files, $options) : DifyChatService::chat('qwen', [], $query, 'same-synthetic-user', $files, $options);
batchExpect($result['error_code'] === ($scenario === 'deadline' ? 'UPSTREAM_TIMEOUT' : 'AI_ASSISTANT_CANCELLED')
&& count($batchRequests) === 1 && count($batchUploads) === 1, 'shared cancellation/deadline prevents every later upload, extraction and synthesis');
batchExpect($ends === 1 && ($scenario !== 'cancel' || $stops !== []), 'cancel/end callbacks survive extraction transport');
}
}
$reset('success');
$duplicateFiles = $files; $duplicateFiles[1] = $files[0];
$result = DifyChatService::chat('qwen', [], $query, 'same-synthetic-user', $duplicateFiles,
['required_files' => true, 'knowledge_mode' => 'evidence']);
batchExpect($result['submitted_attachment_count'] === 7
&& array_column($batchRequests[0]['payload']['files'], 'upload_file_id') === ['synthetic-upload-1', 'synthetic-upload-1', 'synthetic-upload-3'],
'duplicate logical original positions remain independently submitted without deduplication');
DifyChatService::chat('qwen', [], $query, 'another-synthetic-user', $duplicateFiles,
['required_files' => true, 'knowledge_mode' => 'evidence']);
batchExpect(count($batchUploads) === 6 && $batchUploads[3]['user'] === 'another-synthetic-user', 'upload IDs never persist into another request/user');
// Assert the model input's identity map, not only the final transport receipt.
// The second batch carries global 4/5/6; filtering must not renumber a missing
// source into "图片附件1", including when that set is later bisected.
foreach ([false, true] as $streaming) foreach (['mapping', 'mapping-count-one', 'mapping-fallback', 'mapping-openai'] as $scenario) {
foreach ([[1], [4], [5], [6], [7], [4, 5], [4, 5, 6]] as $missingIds) {
foreach ([false, true] as $duplicates) {
$reset($scenario); $batchMissingIds = $missingIds;
$selectedFiles = $files;
if ($duplicates) $selectedFiles[4] = $files[3];
$batchConfig['base_url'] = $scenario === 'mapping-fallback' ? 'https://synthetic.invalid/v1'
: ($scenario === 'mapping-openai' ? 'https://synthetic.invalid/v1/chat/completions' : 'https://synthetic.invalid/v1/chat-messages');
// Direct OpenAI has no source availability/upload stage, so its
// original references all remain physically present.
$physicalMissing = $scenario === 'mapping-openai' ? [] : $missingIds;
$opts = ['required_files' => true, 'knowledge_mode' => 'evidence'];
$result = $streaming ? DifyChatService::streamChat('qwen', [], $query, 'same-synthetic-user', static fn (): bool => true,
null, $selectedFiles, $opts) : DifyChatService::chat('qwen', [], $query, 'same-synthetic-user', $selectedFiles, $opts);
batchExpect(!empty($result['ok']), 'physical mapping cases complete without clinical identity loss');
$seen = [];
foreach ($batchRequests as $request) {
$prompt = \app\common\service\batchFixtureQuery($request['payload']);
if (!str_starts_with($prompt, '【附件分批证据提取】')) continue;
$wireIds = \app\common\service\batchFixtureWireIds($request['payload']);
preg_match_all('/实际附件(\d+) => 原始附件#(\d+)(([^)]+))/u', $prompt, $map, PREG_SET_ORDER);
batchExpect(count($map) === count($wireIds), 'every actual physical wire entry has exactly one stable source mapping');
$mapped = [];
foreach ($map as $position => $entry) {
$global = (int) $entry[2];
$sourceId = (int) pathinfo(parse_url($selectedFiles[$global - 1]['url'], PHP_URL_PATH), PATHINFO_FILENAME);
batchExpect((int) $entry[1] === $position + 1 && $entry[3] === '图片附件' . $global
&& $wireIds[$position] === 'synthetic-upload-' . $sourceId && !isset($mapped[$global]),
'wire order maps to correct original source/label even for duplicate URLs');
batchExpect(!in_array($sourceId, $physicalMissing, true), 'confirmed missing sources cannot appear in actual attachment map');
$mapped[$global] = true; $seen[$global] = true;
}
preg_match_all('/^原始附件#(\d+)(图片附件\d+)$/mu', $prompt, $listed, PREG_SET_ORDER);
foreach ($listed as $entry) {
$global = (int) $entry[1];
$sourceId = (int) pathinfo(parse_url($selectedFiles[$global - 1]['url'], PHP_URL_PATH), PATHINFO_FILENAME);
if (in_array($sourceId, $physicalMissing, true)) batchExpect(str_contains($prompt,
'原始附件#' . $global . '(图片附件' . $global . ')源文件不存在,已跳过,未提供给模型'),
'missing notice uses the same stable original identity as the extraction list');
}
batchExpect(!preg_match('/(?<!()图片附件\d+源文件不存在/u', $prompt), 'no batch-local missing label can conflict with original source identity');
}
foreach ($selectedFiles as $global => $file) {
$sourceId = (int) pathinfo(parse_url($file['url'], PHP_URL_PATH), PATHINFO_FILENAME);
batchExpect(isset($seen[$global + 1]) === !in_array($sourceId, $physicalMissing, true),
'every available original logical position has a physical identity map');
}
}
}
}
$batchConfig['base_url'] = 'https://synthetic.invalid/v1/chat-messages';
$reset('success'); $batchMissingIds = [1];
$result = DifyChatService::chat('qwen', [], $query, 'same-synthetic-user', array_slice($files, 0, 2),
['required_files' => true, 'knowledge_mode' => 'evidence']);
batchExpect(!empty($result['ok']) && count($batchRequests) === 1
&& !str_contains($batchRequests[0]['payload']['query'], '【附件原始序号与实际顺序】')
&& str_contains($batchRequests[0]['payload']['query'], '图片附件1源文件不存在'), 'direct non-batch missing-source prompt keeps its prior contract');
// A rejection after a visible original clinical delta can never replay the task.
$reset('partial'); $deltas = [];
$result = DifyChatService::streamChat('qwen', [], $query, 'same-synthetic-user',
static function (string $delta) use (&$deltas): bool { $deltas[] = $delta; return true; }, null, array_slice($files, 0, 2),
['required_files' => true, 'knowledge_mode' => 'evidence']);
batchExpect(empty($result['ok']) && count($batchRequests) === 1 && count($deltas) === 1, 'no batch recovery/replay after a delivered original clinical delta');
echo "Clinical required attachment batching / 7 sources / stable physical source maps / gaps / shared identity and upload reuse / stream isolation / cancellation and deadline: OK\n";
}
+143
View File
@@ -0,0 +1,143 @@
<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use app\common\service\DifyChatService;
function imageTextExpect(bool $ok, string $message): void { if (!$ok) throw new RuntimeException($message); }
$service = new ReflectionClass(DifyChatService::class);
$receiptMethod = $service->getMethod('uploadedAttachmentReceipt');
$append = $service->getMethod('addImageTextCheck');
$files = array_fill(0, 3, ['type' => 'image', 'url' => 'https://example.invalid/private-source']);
$uploaded = ['files' => [
['type' => 'image', 'transfer_method' => 'local_file', 'upload_file_id' => 'private-id-2'],
['type' => 'image', 'transfer_method' => 'local_file', 'upload_file_id' => 'private-id-3'],
], 'uploaded_indices' => [2, 3], 'skipped' => [['index' => 1, 'kind' => 'image', 'reason' => 'not_found']],
'image_text_check' => ['status' => 'completed', 'images' => [
['index' => 2, 'status' => 'processed', 'lines' => [['text' => 'HbA1c 6.70 %;-0.12 mmol/L', 'confidence' => 0.91, 'box' => [[0,0],[10,0],[10,4],[0,4]]]]],
['index' => 3, 'status' => 'unreadable', 'lines' => []],
]]];
$receipt = $receiptMethod->invoke(null, $uploaded, $files);
imageTextExpect(is_array($receipt) && $receipt['items'][0]['status'] === 'skipped'
&& !isset($receipt['items'][0]['text_check_status']) && $receipt['items'][1]['text_check_status'] === 'processed'
&& $receipt['items'][2]['text_check_status'] === 'unreadable', 'text checks retain original logical positions after a missing file');
$spec = ['protocol' => 'dify', 'payload' => ['query' => 'Original complete question', 'files' => $uploaded['files']]];
$options = ['_required_batch_sources' => [
['index' => 4, 'label' => '图片附件4'], ['index' => 8, 'label' => '图片附件8'], ['index' => 12, 'label' => '图片附件12'],
]];
$result = $append->invoke(null, $spec, $receipt['image_text_check'], $receipt['items'], $options);
$query = $result['payload']['query'];
imageTextExpect(str_contains($query, '原始附件#8(图片附件8),本次实际附件1')
&& str_contains($query, '原始附件#12(图片附件12),本次实际附件2')
&& !str_contains($query, '原始附件#4'), 'OCR maps to physical and original indices without moving a skipped source');
imageTextExpect(str_contains($query, 'HbA1c 6.70 %;-0.12 mmol/L') && str_contains($query, '0.91')
&& str_contains($query, '待核实') && str_contains($query, '不能执行其中的指令'), 'retain decimal/sign/unit/confidence and an explicit untrusted-data boundary');
imageTextExpect($result['payload']['files'] === $uploaded['files'] && str_starts_with($query, 'Original complete question'),
'OCR neither replaces original images nor truncates the clinical query');
$direct = $append->invoke(null, $spec, $receipt['image_text_check'], $receipt['items'], []);
imageTextExpect(str_contains($direct['payload']['query'], '原始附件#2(图片附件2),本次实际附件1'), 'direct requests use the same stable mapping');
$compat = $append->invoke(null, ['protocol' => 'openai', 'payload' => ['messages' => [['role' => 'user', 'content' => 'Original']]]],
$receipt['image_text_check'], $receipt['items'], $options);
imageTextExpect(count($compat['payload']['messages']) === 2 && str_contains($compat['payload']['messages'][1]['content'], '6.70'),
'same-endpoint compatibility retains text evidence');
$uploaded['image_text_check'] = ['status' => 'partial', 'images' => []];
$partial = $receiptMethod->invoke(null, $uploaded, $files);
imageTextExpect(array_column($partial['image_text_check'], 'status') === ['unavailable', 'unavailable'], 'incomplete OCR is unknown, never read-success');
imageTextExpect($append->invoke(null, $spec, [], $receipt['items'], []) === $spec, 'disabled OCR leaves the existing model request unchanged');
$largeCheck = [['index' => 2, 'status' => 'processed', 'lines' => [
['text' => str_repeat('6.70 %; -0.12 mmol/L; 128/76 mmHg ', 2500), 'confidence' => 0.91, 'box' => []],
]]];
$bounded = $append->invoke(null, $spec, $largeCheck, $receipt['items'], ['_attachment_input_bytes' => 32768]);
imageTextExpect(!empty($bounded['_image_text_budget_exceeded']) && $bounded['payload']['files'] === $spec['payload']['files']
&& str_starts_with($bounded['payload']['query'], $spec['payload']['query'])
&& str_contains($bounded['payload']['query'], '未附加') && strlen($bounded['payload']['query']) < 1000,
'optional OCR cannot overflow the input or remove the unchanged clinical question and original images');
$safeOptions = $service->getMethod('imageTextReceiptOptions')->invoke(null, $bounded, ['attachment_items' => $receipt['items']]);
imageTextExpect($safeOptions['attachment_items'][1]['text_check_status'] === 'withheld'
&& $safeOptions['attachment_items'][2]['text_check_status'] === 'unreadable',
'only processed-but-not-transmitted OCR is marked withheld; no false evidence-transmitted claim');
$escapeCheck = [['index' => 2, 'status' => 'processed', 'lines' => [
['text' => str_repeat('"', 8000), 'confidence' => 0.91, 'box' => []],
]]];
$escaped = $append->invoke(null, $spec, $escapeCheck, $receipt['items'], ['_attachment_input_bytes' => 32768]);
imageTextExpect(!empty($escaped['_image_text_budget_exceeded'])
&& strlen(json_encode($escaped['payload'], JSON_UNESCAPED_UNICODE)) <= 32768,
'nested OCR quotation escaping is measured from the actual serialized candidate payload');
foreach (['dify', 'openai'] as $protocol) {
$payload = $protocol === 'dify' ? ['query' => '', 'files' => $uploaded['files']]
: ['messages' => [['role' => 'user', 'content' => '']], 'files' => $uploaded['files']];
$padding = str_repeat('A', 32640 - strlen(json_encode($payload, JSON_UNESCAPED_UNICODE)));
if ($protocol === 'dify') $payload['query'] = $padding;
else $payload['messages'][0]['content'] = $padding;
$nearLimit = $append->invoke(null, ['protocol' => $protocol, 'payload' => $payload],
$receipt['image_text_check'], $receipt['items'], ['_attachment_input_bytes' => 32768]);
imageTextExpect(!empty($nearLimit['_image_text_budget_exceeded']) && $nearLimit['payload'] === $payload
&& strlen(json_encode($nearLimit['payload'], JSON_UNESCAPED_UNICODE)) === 32640,
'withheld notice also respects capacity; unchanged original payload wins over an optional warning in ' . $protocol);
}
$fit = $service->getMethod('fitAttachmentEvidence');
$items = [['status' => 'submitted'], ['status' => 'submitted']];
$repeated = [['index' => 1, 'status' => 'read', 'findings' => str_repeat('HbA1c 6.70%; signed -0.12 mmol/L; BP 128/76 mmHg. ', 1000)],
['index' => 2, 'status' => 'read', 'findings' => 'Different source: HbA1c 7.80%, signed +0.34 mmol/L, BP 142/81 mmHg.']];
$packed = $fit->invokeArgs(null, [$repeated, &$items, 2000]);
imageTextExpect(array_column($packed, 'index') === [1, 2] && array_column($packed, 'status') === ['read', 'read']
&& str_contains($packed[0]['findings'], '6.70%') && str_contains($packed[0]['findings'], '-0.12 mmol/L')
&& str_contains($packed[0]['findings'], '128/76 mmHg') && str_contains($packed[0]['findings'], '重复1000次')
&& $packed[1]['findings'] === $repeated[1]['findings'],
'consecutive identical sentence coding preserves source, decimal, sign, unit and repetition count without touching independent observations');
$items = [['status' => 'submitted'], ['status' => 'submitted']];
$unique = [['index' => 1, 'status' => 'read', 'findings' => implode(' ', range(10000, 16000))], $repeated[1]];
$boundedRows = $fit->invokeArgs(null, [$unique, &$items, 1500]);
imageTextExpect($boundedRows === [$unique[1]] && $items[0]['evidence_status'] === 'unverified'
&& $items[0]['evidence_reason'] === 'context_budget' && $items[0]['status'] === 'submitted'
&& strlen(json_encode(['files' => $boundedRows], JSON_UNESCAPED_UNICODE)) <= 1500,
'oversized observation is omitted from facts and becomes a shared source gap, never substring truncation or an erased receipt');
$shortRows = array_map(static fn (int $index): array => ['index' => $index, 'status' => 'read',
'findings' => 'HbA1c 6.70%.'], range(1, 40));
$shortBytes = strlen(json_encode(['files' => $shortRows], JSON_UNESCAPED_UNICODE));
$shortItems = array_fill(0, 40, ['status' => 'submitted', 'evidence_status' => 'read']);
$allShort = $fit->invokeArgs(null, [$shortRows, &$shortItems, $shortBytes + 100]);
imageTextExpect($allShort === $shortRows && array_unique(array_column($shortItems, 'evidence_status')) === ['read'],
'all forty original observations fit before constructing any larger fallback metadata');
foreach ([0, 1, 11, $shortBytes - 1] as $limit) {
$selectedItems = array_fill(0, 40, ['status' => 'submitted', 'evidence_status' => 'read']);
$selected = $fit->invokeArgs(null, [$shortRows, &$selectedItems, $limit]);
imageTextExpect($selected === [] || strlen(json_encode(['files' => $selected], JSON_UNESCAPED_UNICODE)) <= $limit,
'zero or below-fallback-floor budget never expands evidence');
$included = array_column($selected, 'index');
foreach ($shortRows as $position => $row) {
imageTextExpect($selectedItems[$position]['status'] === 'submitted', 'capacity never rewrites actual transport');
if (in_array($row['index'], $included, true)) imageTextExpect(in_array($row, $selected, true), 'retained facts remain complete');
else imageTextExpect($selectedItems[$position]['evidence_status'] === 'unverified', 'each excluded source remains a visible gap');
}
}
$supplement = $service->getMethod('attachmentSynthesisSupplement');
$escapedRows = array_map(static fn (int $index): array => ['index' => $index, 'status' => 'read',
'findings' => str_repeat('SOURCE_' . $index . ': "quoted" \\ path mmol/L / unit' . "\n", 150)], range(1, 40));
$escapedItems = array_map(static fn (int $index): array => ['label' => '图片附件' . $index,
'status' => 'submitted', 'evidence_status' => 'read'], range(1, 40));
$limited = $supplement->invokeArgs(null, [$escapedRows, &$escapedItems, 32768]);
$addedBytes = strlen(json_encode($limited['text'], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE)) - 2;
imageTextExpect($addedBytes <= 32768 && count($limited['gaps']) > 0 && count($escapedItems) === 40,
'actual JSON-escaped additions including gap metadata fit while all client source receipts remain');
$manyItems = array_map(static fn (int $index): array => ['label' => '图片附件' . $index,
'status' => $index % 2 ? 'skipped' : 'rejected', 'reason' => $index % 2 ? 'not_found' : 'file_type'], range(1, 20000));
$global = $supplement->invokeArgs(null, [[], &$manyItems, 32768]);
imageTextExpect(strlen(json_encode($global['text'], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE)) - 2 <= 32768
&& str_contains($global['text'], '原始附件#1-20000全批尚未可靠综合') && count($global['gaps']) === 20000
&& $manyItems[0]['status'] === 'skipped' && $manyItems[1]['status'] === 'rejected',
'an oversized identity list becomes a bounded whole-range gap without erasing the full truthful client manifest');
$observe = $service->getMethod('checkedAttachmentEvidence');
$rows = $observe->invoke(null, json_encode(['files' => [
['index' => 8, 'status' => 'read', 'findings' => '6.70 mmol/L'],
['index' => 12, 'status' => 'unreadable', 'findings' => 'Small blurred text'],
['index' => 999, 'status' => 'read', 'findings' => 'FOREIGN_SOURCE'],
]]), [4, 8, 12]);
imageTextExpect(array_column($rows, 'status') === ['unverified', 'read', 'unreadable']
&& $rows[1]['findings'] === '6.70 mmol/L' && !str_contains(json_encode($rows), 'FOREIGN_SOURCE'),
'missing/unreadable/foreign sources are separated while exact numeric observations survive');
$duplicate = $observe->invoke(null, '{"files":[{"index":8,"status":"read","findings":"6.7"},{"index":8,"status":"read","findings":"8.7"}]}', [8]);
imageTextExpect($duplicate[0]['status'] === 'unverified' && !str_contains($duplicate[0]['findings'], '6.7'), 'duplicate conflicting source observations cannot silently overwrite one another');
imageTextExpect($observe->invoke(null, 'Unstructured claim that all files were read', [8])[0]['status'] === 'unverified',
'a generic success sentence does not certify every attachment was observed');
echo "Local image text evidence / exact numbers / stable source mapping / original images / incomplete status: OK\n";
@@ -0,0 +1,308 @@
<?php
declare(strict_types=1);
// Standalone offline fault injection. No application bootstrap, credentials or I/O.
namespace think\facade {
class Log { public static function warning(string $message): void { $GLOBALS['largeLogs'][] = $message; } }
}
namespace app\common\service {
require __DIR__ . '/fixtures/CancellableCurlMultiStub.php';
class ClinicalKnowledgeReference {
public static function ancientBookSources(string $query): array { return []; }
public static function augmentQueryWithAncientBookSources(string $query, array $sources): string { return $query; }
}
class DifyRequiredAttachmentUpload {
public static function prepare(string $endpoint, string $key, string $user, array $files, int $timeout,
?callable $shouldAbort = null, array $options = []): array {
$GLOBALS['largeUploads'][] = compact('user', 'files', 'timeout');
$wire = []; $indices = []; $skipped = [];
foreach ($files as $index => $file) {
if ($shouldAbort !== null && $shouldAbort()) return ['ok' => false, 'error_code' => 'AI_ASSISTANT_CANCELLED'];
$id = (int) pathinfo(parse_url($file['url'], PHP_URL_PATH), PATHINFO_FILENAME);
$missing = in_array($id, $GLOBALS['largeMissing'], true);
if ($missing) {
$skipped[] = ['index' => $index + 1, 'kind' => $file['type'], 'reason' => 'not_found'];
if (is_callable($options['on_upload'] ?? null)) ($options['on_upload'])([
'index' => $index + 1, 'status' => 'skipped', 'reason' => 'not_found',
]);
continue;
}
foreach (['uploading', 'uploaded'] as $status) if (is_callable($options['on_upload'] ?? null)) {
($options['on_upload'])(['index' => $index + 1, 'status' => $status]);
}
$wire[] = ['type' => $file['type'], 'transfer_method' => 'local_file', 'upload_file_id' => 'SOURCE_' . $id];
$indices[] = $index + 1;
}
return ['ok' => true, 'files' => $wire, 'uploaded_indices' => $indices, 'skipped' => $skipped];
}
}
function config(string $name): array { return $GLOBALS['largeConfig']; }
function microtime(bool $asFloat = false): float { return $GLOBALS['largeClock']; }
function curl_init(): \stdClass { return (object) ['errno' => 0, 'code' => 200]; }
function curl_setopt_array(\stdClass $handle, array $options): bool { $handle->options = $options; return true; }
function curl_exec(\stdClass $handle): string|bool {
$options = $handle->options;
if (str_ends_with($options[CURLOPT_URL], '/stop')) {
$GLOBALS['largeStops']++;
return '{"result":"success"}';
}
$payload = json_decode($options[CURLOPT_POSTFIELDS], true);
$query = $payload['query'];
$extract = str_starts_with($query, '【附件分批证据提取】');
$synthesis = str_contains($query, '【最终任务】');
if ($extract) $GLOBALS['largeExtracts']++;
$ids = array_column($payload['files'] ?? [], 'upload_file_id');
$limitMs = $options[CURLOPT_TIMEOUT_MS] ?? $options[CURLOPT_TIMEOUT] * 1000;
$GLOBALS['largeRequests'][] = compact('payload', 'extract', 'synthesis', 'ids', 'limitMs');
$scenario = $GLOBALS['largeScenario'];
$error = null;
if ($extract && $scenario === 'provider-cap-three' && count($ids) > 3) {
$handle->code = 400; $error = ['code' => 'too_many_files', 'message' => 'Number of files exceeds the limit'];
} elseif ($extract && $scenario === 'reject-documents'
&& in_array('document', array_column($payload['files'] ?? [], 'type'), true)) {
$handle->code = 415; $error = ['code' => 'unsupported_file_type', 'message' => 'PRIVATE_SYNTHETIC_ERROR'];
} elseif ($extract && $GLOBALS['largeExtracts'] === 2 && in_array($scenario, ['auth', 'quota', 'server', 'ambiguous', 'network'], true)) {
if ($scenario === 'network') { $handle->errno = CURLE_COULDNT_CONNECT; $handle->code = 0; return false; }
$handle->code = ['auth' => 401, 'quota' => 429, 'server' => 503, 'ambiguous' => 413][$scenario];
$error = ['code' => 'invalid_param', 'message' => 'PRIVATE_SYNTHETIC_ERROR'];
} elseif ($synthesis && in_array($scenario, ['context-limit', 'context-unique'], true) && strlen($query) > 120000) {
$handle->code = 400; $error = ['code' => 'context_length_exceeded', 'message' => 'maximum context length exceeded'];
}
$duration = $scenario === 'extraction-deadline' && $extract ? 31.0
: ($scenario === 'synthesis-deadline' ? 20.0 : 0.001);
if ($duration * 1000 >= $limitMs) {
$GLOBALS['largeClock'] += $limitMs / 1000;
$handle->errno = CURLE_OPERATION_TIMEDOUT; $handle->code = 0;
return false;
}
$GLOBALS['largeClock'] += $duration;
if (isset($options[CURLOPT_HEADERFUNCTION])) {
($options[CURLOPT_HEADERFUNCTION])($handle, 'HTTP/1.1 ' . $handle->code . " Synthetic\r\n");
}
$streaming = ($payload['response_mode'] ?? '') === 'streaming';
if ($error !== null) {
$body = json_encode($error);
if ($streaming) { ($options[CURLOPT_WRITEFUNCTION])($handle, $body); return true; }
return $body;
}
$visibleIds = $scenario === 'sparse-evidence' && $extract ? array_slice($ids, 0, 1) : $ids;
$answer = 'SYNTHETIC_FINAL_ANSWER';
if ($extract) {
preg_match_all('/实际附件\d+ => 原始附件#(\d+)/u', $query, $map);
$rows = [];
foreach ($visibleIds as $position => $id) $rows[] = ['index' => (int) ($map[1][$position] ?? 0),
'status' => 'read', 'findings' => 'OBSERVATION_' . $id
. ($scenario === 'context-limit' ? str_repeat('Long synthetic extracted text. ', 1000) : '')
. ($scenario === 'context-unique' ? implode(' ', array_map(static fn (int $n): string => 'unique_' . $id . '_' . $n, range(1, 1000))) : '')];
$answer = json_encode(['files' => $rows]);
}
if (!$streaming) return json_encode(['answer' => $answer]);
$write = $options[CURLOPT_WRITEFUNCTION];
$write($handle, 'data: ' . json_encode(['event' => 'message', 'task_id' => 'large-task-' . count($GLOBALS['largeRequests']), 'answer' => $answer]) . "\n\n");
if ($extract && $scenario === 'cancel-second' && $GLOBALS['largeExtracts'] === 2) $GLOBALS['largeCancelled'] = true;
$write($handle, "data: {\"event\":\"message_end\"}\n\n");
return true;
}
function curl_errno(\stdClass $handle): int { return $handle->errno; }
function curl_getinfo(\stdClass $handle, int $option): int { return $handle->code; }
function curl_close(\stdClass $handle): void {}
}
namespace {
require dirname(__DIR__) . '/app/common/service/DifyChatService.php';
use app\common\service\DifyChatService;
function largeExpect(bool $condition, string $message): void { if (!$condition) throw new RuntimeException($message); }
function largeHasObservation(string $query, int $id): bool { return preg_match('/OBSERVATION_SOURCE_' . $id . '(?![0-9])/', $query) === 1; }
function largeHasGap(string $query, int $id): bool {
preg_match_all('/原始附件#序号\/范围\[([0-9,-]+)\]/u', $query, $matches);
foreach ($matches[1] as $list) foreach (explode(',', $list) as $range) {
$ends = array_map('intval', explode('-', $range));
if ($id >= $ends[0] && $id <= ($ends[1] ?? $ends[0])) return true;
}
return false;
}
function largeAddedPayloadBytes(array $payload, string $original): int {
$baseline = $payload; $baseline['query'] = $original;
$flags = JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE;
return strlen(json_encode($payload, $flags)) - strlen(json_encode($baseline, $flags));
}
function largeFiles(int $count, bool $mixed = false): array {
return array_map(static function (int $id) use ($mixed): array {
$document = $mixed && $id % 5 === 0;
return ['type' => $document ? 'document' : 'image',
'url' => 'https://synthetic.invalid/' . $id . ($document ? '.pdf' : '.png')];
}, range(1, $count));
}
function largeReset(string $scenario, int $cap = 10): void {
$GLOBALS['largeScenario'] = $scenario; $GLOBALS['largeClock'] = 1000.0;
$GLOBALS['largeRequests'] = []; $GLOBALS['largeUploads'] = []; $GLOBALS['largeLogs'] = [];
$GLOBALS['largeExtracts'] = 0; $GLOBALS['largeStops'] = 0; $GLOBALS['largeCancelled'] = false;
$GLOBALS['largeMissing'] = [];
$GLOBALS['largeConfig'] = ['enable' => true, 'base_url' => 'https://synthetic.invalid/v1/chat-messages',
'timeout' => 90, 'required_attachment_transport' => 'upload', 'max_files' => 3,
'models' => ['qwen' => ['name' => 'synthetic-model', 'api_key' => 'SYNTHETIC_ONLY_KEY', 'max_files' => $cap]]];
}
function largeRun(array $files, bool $streaming, string $query = 'COMPLETE_ORIGINAL_CASE_AND_OUTPUT_SCHEMA'): array {
$deltas = []; $events = []; $tasks = []; $ends = [];
$options = ['required_files' => true, 'required_sections' => [
['key' => 'basic_info', 'status' => 'prepared', 'availability' => 'present'],
['key' => 'chat', 'status' => 'prepared', 'availability' => 'present'],
], 'on_progress' => static function (array $event) use (&$events): void { $events[] = $event; },
'on_task' => static function (...$args) use (&$tasks): void { $tasks[] = $args; },
'on_transfer_end' => static function (...$args) use (&$ends): void { $ends[] = $args; }];
$inputs = ['full_original_inputs' => $query];
if ($streaming) {
$result = DifyChatService::streamChat('qwen', $inputs, $query, 'same-synthetic-user',
static function (string $delta) use (&$deltas): bool { $deltas[] = $delta; return true; },
static fn (): bool => $GLOBALS['largeCancelled'], $files, $options);
} else $result = DifyChatService::chat('qwen', $inputs, $query, 'same-synthetic-user', $files, $options);
return compact('result', 'deltas', 'events', 'tasks', 'ends');
}
function largeSafe(array $result, array $events): void {
$encoded = json_encode([$result, $events, $GLOBALS['largeLogs']]);
largeExpect(!str_contains($encoded, 'PRIVATE_SYNTHETIC_ERROR') && !str_contains($encoded, 'SYNTHETIC_ONLY_KEY')
&& !str_contains($encoded, 'synthetic.invalid'), 'safe result/progress/log receipt must not expose source URL/key/provider message');
}
$cases = 0;
foreach ([false, true] as $streaming) foreach ([false, true] as $mixed) foreach ([10, 11, 20, 40] as $count) {
largeReset('success');
['result' => $result, 'deltas' => $deltas, 'events' => $events] = largeRun(largeFiles($count, $mixed), $streaming);
largeExpect(!empty($result['ok']) && count($result['items']) === $count && $result['submitted_attachment_count'] === $count,
'10/11/20/40 image/mixed sources must all remain accounted for');
$expectedCalls = $count <= 10 ? 1 : (int) ceil($count / 10) + 1;
largeExpect(count($largeRequests) === $expectedCalls, 'use configured cap and at most one final synthesis');
foreach ($largeRequests as $request) largeExpect(count($request['ids']) <= 10
&& $request['payload']['user'] === 'same-synthetic-user', 'same app/user and cap throughout pipeline');
if ($count > 10) {
$final = $largeRequests[count($largeRequests) - 1]['payload'];
largeExpect(str_starts_with($final['query'], 'COMPLETE_ORIGINAL_CASE_AND_OUTPUT_SCHEMA')
&& $final['inputs'] === ['full_original_inputs' => 'COMPLETE_ORIGINAL_CASE_AND_OUTPUT_SCHEMA']
&& ($final['files'] ?? []) === [], 'final task preserves complete original text/schema/inputs and truthfully uses extracted evidence');
foreach (range(1, $count) as $id) largeExpect(largeHasObservation($final['query'], $id), 'every source observation survives final query');
largeExpect($result['attachment_batch_count'] === (int) ceil($count / 10), 'receipt exposes actual extraction attempts');
}
if ($streaming) largeExpect($deltas === ['SYNTHETIC_FINAL_ANSWER'], 'private extraction text never reaches clinical answer');
largeSafe($result, $events); $cases++;
}
foreach ([false, true] as $streaming) foreach (['provider-cap-three', 'reject-documents', 'missing'] as $scenario) {
largeReset($scenario);
if ($scenario === 'missing') $largeMissing = [2, 17, 39];
['result' => $result, 'deltas' => $deltas, 'events' => $events] = largeRun(largeFiles(40, true), $streaming);
largeExpect(!empty($result['ok']) && count($result['items']) === 40, 'known source gaps must not erase full task or its complete manifest');
$rejected = $scenario === 'reject-documents' ? 8 : 0;
$missing = $scenario === 'missing' ? 3 : 0;
largeExpect($result['submitted_attachment_count'] === 40 - $rejected - $missing
&& $result['rejected_attachment_count'] === $rejected && $result['skipped_attachment_count'] === $missing,
'accepted, rejected and missing sources partition original forty sources');
$final = $largeRequests[count($largeRequests) - 1]['payload']['query'];
foreach ($result['items'] as $index => $item) {
$id = $index + 1;
if ($item['status'] === 'submitted') largeExpect(largeHasObservation($final, $id), 'available source evidence reaches final synthesis');
else largeExpect(largeHasGap($final, $id), 'every known unread source is covered by explicit compact source-range gaps');
}
largeExpect(count($largeRequests) <= 80, 'bisection stays below binary-tree source bound plus synthesis');
if ($streaming) largeExpect($deltas === ['SYNTHETIC_FINAL_ANSWER'], 'failed child batch content is never displayed');
largeSafe($result, $events); $cases++;
}
foreach ([false, true] as $streaming) foreach (['auth', 'quota', 'server', 'ambiguous', 'network'] as $scenario) {
largeReset($scenario);
['result' => $result, 'deltas' => $deltas, 'events' => $events] = largeRun(largeFiles(40), $streaming);
largeExpect(empty($result['ok']) && count($largeRequests) === 2 && count($result['items']) === 40,
'unclassified second-batch failure stops entire task and preserves complete source manifest');
largeExpect(!array_filter($largeRequests, static fn (array $request): bool => $request['synthesis']),
'unknown/auth/network faults must not silently become permission to drop sources and synthesize');
largeExpect(count(array_filter($result['items'], static fn (array $item): bool => $item['status'] === 'prepared')) >= 20,
'sources never attempted after failure remain unsubmitted');
largeExpect($deltas === [] && !isset($result['attachment_gaps']),
'early whole-task failure has no fabricated final answer or completed gap synthesis');
largeSafe($result, $events); $cases++;
}
foreach ([false, true] as $streaming) foreach (['extraction-deadline', 'synthesis-deadline'] as $scenario) {
largeReset($scenario);
['result' => $result, 'deltas' => $deltas, 'events' => $events] = largeRun(largeFiles(40), $streaming);
largeExpect(empty($result['ok']) && $result['error_code'] === 'UPSTREAM_TIMEOUT', 'whole shared 90-second budget also constrains final synthesis');
$expected = $scenario === 'extraction-deadline' ? 3 : 5;
largeExpect(count($largeRequests) === $expected && abs($largeClock - 1090.0) < 0.01,
'later batches do not receive fresh ninety-second budgets');
largeExpect($largeRequests[count($largeRequests) - 1]['limitMs'] <= ($scenario === 'extraction-deadline' ? 28000 : 10000),
'real curl millisecond option receives remaining aggregate budget');
largeExpect($deltas === [], 'no extracted intermediate evidence is promoted to partial clinical answer on timeout');
largeSafe($result, $events); $cases++;
}
largeReset('cancel-second');
['result' => $result, 'deltas' => $deltas, 'events' => $events, 'tasks' => $tasks] = largeRun(largeFiles(40), true);
largeExpect(empty($result['ok']) && $result['error_code'] === 'AI_ASSISTANT_CANCELLED'
&& count($largeRequests) === 2 && $deltas === [] && $tasks !== [], 'stop during child extraction prevents every later extraction/synthesis and preserves real task IDs');
largeSafe($result, $events); $cases++;
foreach ([false, true] as $streaming) {
largeReset('context-limit');
['result' => $result, 'deltas' => $deltas, 'events' => $events] = largeRun(largeFiles(40), $streaming,
'COMPLETE_ORIGINAL_CASE_' . str_repeat('Long original case. ', 4000));
$final = $largeRequests[count($largeRequests) - 1];
largeExpect(!empty($result['ok']) && $final['synthesis'] && strlen($final['payload']['query']) < 120000
&& count($largeRequests) === 5 && $result['submitted_attachment_count'] === 40,
'lossless repeated-text encoding prevents final context overflow after all forty attachments were extracted');
foreach (range(1, 40) as $id) largeExpect(largeHasObservation($final['payload']['query'], $id), 'all forty independent observations survive repeated-text encoding');
largeExpect($result['attachment_gaps'] === [], 'reversible repetition coding does not invent a missing source');
largeSafe($result, $events); $cases++;
}
largeReset('context-unique');
$GLOBALS['largeConfig']['required_attachment_input_bytes'] = 100000;
['result' => $result] = largeRun(largeFiles(40), false);
$final = $largeRequests[count($largeRequests) - 1]['payload']['query'];
largeExpect(!empty($result['ok']) && strlen($final) < 120000 && $result['submitted_attachment_count'] === 40
&& count($result['attachment_gaps']) > 0 && str_contains($final, '本轮综合容量不足'),
'unique oversized evidence becomes explicit per-source synthesis gaps and a bounded answer, never silent truncation or an oversized final request');
foreach ($result['items'] as $item) largeExpect(($item['status'] ?? '') === 'submitted', 'budget gaps do not rewrite the fact every original source was transmitted');
largeExpect(largeAddedPayloadBytes($largeRequests[count($largeRequests) - 1]['payload'], 'COMPLETE_ORIGINAL_CASE_AND_OUTPUT_SCHEMA') <= 100000,
'unique evidence plus its gap metadata obey the actual serialized additional byte budget');
$cases++;
foreach ([false, true] as $streaming) {
largeReset('success');
['result' => $result, 'deltas' => $deltas] = largeRun(largeFiles(700), $streaming);
$final = $largeRequests[count($largeRequests) - 1]['payload'];
largeExpect(!empty($result['ok']) && count($result['items']) === 700
&& $result['submitted_attachment_count'] === 700 && $result['attachment_gaps'] === []
&& count($largeRequests) === 71, 'seven hundred short observations remain complete without extra inference requests');
foreach (range(1, 700) as $id) largeExpect(largeHasObservation($final['query'], $id), 'all seven hundred independent source facts fit and survive');
largeExpect(largeAddedPayloadBytes($final, 'COMPLETE_ORIGINAL_CASE_AND_OUTPUT_SCHEMA') <= 196608,
'default 192KiB bounds actual serialized additions instead of multiplying per-source fallback prose');
if ($streaming) largeExpect($deltas === ['SYNTHETIC_FINAL_ANSWER'], 'large source count still emits only final clinical text');
$cases++;
}
largeReset('success');
$largeConfig['required_attachment_input_bytes'] = 32768;
$original = 'COMPLETE_ORIGINAL_CASE_' . str_repeat('original "quoted" \\ value' . "\n", 10000);
['result' => $result] = largeRun(largeFiles(11), false, $original);
$final = $largeRequests[count($largeRequests) - 1]['payload'];
largeExpect(!empty($result['ok']) && str_starts_with($final['query'], $original)
&& $final['inputs'] === ['full_original_inputs' => $original] && $result['attachment_gaps'] === []
&& largeAddedPayloadBytes($final, $original) <= 32768 && count($largeRequests) === 3,
'a large original query and inputs remain exact; only new serialized attachment evidence is bounded, with no hard failure or extra calls');
$cases++;
largeReset('sparse-evidence');
['result' => $result] = largeRun(largeFiles(40), false);
$final = $largeRequests[count($largeRequests) - 1]['payload']['query'];
largeExpect(!empty($result['ok']) && $result['submitted_attachment_count'] === 40
&& count($result['attachment_gaps']) === 36 && !largeHasObservation($final, 2)
&& $result['items'][1]['evidence_status'] === 'unverified'
&& largeHasGap($final, 2) && str_contains($final, '已提交,但未取得逐来源可核对观察'),
'a missing observation becomes an explicit source gap without failing the whole known-case answer');
$cases++;
echo "Large attachment offline contract: {$cases} scenarios OK (10/11/20/40; mixed documents; known gaps; second-batch faults; 90s budget; stop; context and extraction-completeness risks)\n";
}
@@ -76,9 +76,14 @@ namespace {
missingFallbackExpect($result['items'][0]['status'] === 'skipped' && $result['items'][0]['reason'] === 'not_found',
'known missing item survives every fallback outcome');
if ($missingFallbackScenario === 'missing-image-available-document') {
missingFallbackExpect(empty($result['ok']) && $result['error_code'] === 'REQUIRED_FILES_UNSUPPORTED'
&& count($missingFallbackRequests) === 1 && $result['submitted_attachment_count'] === 0
&& $result['attachment_transfer'] === 'uploaded_files', 'unsupported fallback retains the prior staged receipt');
missingFallbackExpect(!empty($result['ok']) && $result['rejected_attachment_count'] === 1
&& count($missingFallbackRequests) === 6 && $result['submitted_attachment_count'] === 0
&& $result['attachment_transfer'] === 'uploaded_files'
&& $result['items'][1]['status'] === 'rejected' && $result['items'][1]['reason'] === 'file_type',
'unsupported document is isolated as a truthful unread gap while the confirmed missing source stays skipped');
$last = $missingFallbackRequests[count($missingFallbackRequests) - 1];
missingFallbackExpect(str_contains(json_encode($last), 'complete synthetic clinical text')
&& !str_contains(json_encode($last), 'https://fixture.invalid/missing'), 'final original body never resurrects known missing URLs');
continue;
}
missingFallbackExpect(!empty($result['ok']) && count($missingFallbackRequests) === 2
+183
View File
@@ -0,0 +1,183 @@
<?php
declare(strict_types=1);
// Real uploader + real model orchestration, fake OCR/cURL/clock. No live I/O.
namespace think\facade {
class Log { public static function warning(string $message): void {} }
}
namespace app\common\service {
require __DIR__ . '/fixtures/CancellableCurlMultiStub.php';
function config(string $name): array { return $GLOBALS['ocrBudgetConfig']; }
function microtime(bool $asFloat = false): float { return $GLOBALS['ocrBudgetClock']; }
class ClinicalKnowledgeReference {
public static function ancientBookSources(string $query): array { return []; }
public static function augmentQueryWithAncientBookSources(string $query, array $sources): string { return $query; }
}
class ClinicalImageTextService {
public static function read(array $images, string $root, float $deadline, ?callable $abort, array $settings): array {
foreach ($images as $image) if (!is_file($image['path'])) throw new \RuntimeException('expected authorized temporary image');
$GLOBALS['ocrBudgetReads'][] = ['start' => microtime(true), 'deadline' => $deadline,
'indices' => array_column($images, 'index')];
$scenario = $GLOBALS['ocrBudgetScenario'];
if ($scenario === 'exhaust') $GLOBALS['ocrBudgetClock'] = $deadline + 0.8;
elseif ($scenario === 'cancel') { $GLOBALS['ocrBudgetClock'] += 0.1; $GLOBALS['ocrBudgetCancelled'] = true; }
else $GLOBALS['ocrBudgetClock'] += min(1.25, max(0.0, $deadline - microtime(true)));
if ($scenario === 'throw') throw new \RuntimeException('PRIVATE_SYNTHETIC_OCR_FAILURE');
return ['status' => 'unavailable', 'images' => []];
}
}
function curl_init(): \stdClass { return (object) ['errno' => 0, 'code' => 200]; }
function curl_setopt_array(\stdClass $handle, array $options): bool { $handle->options = $options; return true; }
function curl_exec(\stdClass $handle): string|bool {
$options = $handle->options;
if (is_array($options[CURLOPT_POSTFIELDS])) {
if (!str_ends_with($options[CURLOPT_URL], '/files/upload')) throw new \RuntimeException('only synthetic upload endpoint');
$GLOBALS['ocrBudgetUploads']++;
$GLOBALS['ocrBudgetClock'] += 0.2;
$body = json_encode(['id' => 'synthetic-upload-' . $GLOBALS['ocrBudgetUploads']]);
($options[CURLOPT_WRITEFUNCTION])($handle, $body);
return $body;
}
$payload = json_decode($options[CURLOPT_POSTFIELDS], true);
$GLOBALS['ocrBudgetModels'][] = ['payload' => $payload, 'at' => microtime(true),
'timeout_ms' => $options[CURLOPT_TIMEOUT_MS] ?? $options[CURLOPT_TIMEOUT] * 1000];
if ($GLOBALS['ocrBudgetCancelled']) throw new \RuntimeException('model must not start after cancellation');
$GLOBALS['ocrBudgetClock'] += 0.1;
if (isset($options[CURLOPT_HEADERFUNCTION])) ($options[CURLOPT_HEADERFUNCTION])($handle, "HTTP/1.1 200 Synthetic\r\n");
if (($payload['response_mode'] ?? '') === 'streaming') {
($options[CURLOPT_WRITEFUNCTION])($handle, "data: {\"event\":\"message\",\"answer\":\"SYNTHETIC_VISUAL_ANSWER\"}\n\ndata: {\"event\":\"message_end\"}\n\n");
return true;
}
return '{"answer":"SYNTHETIC_VISUAL_ANSWER"}';
}
function curl_errno(\stdClass $handle): int { return $handle->errno; }
function curl_getinfo(\stdClass $handle, int $option): int { return $handle->code; }
function curl_close(\stdClass $handle): void {}
}
namespace {
require dirname(__DIR__) . '/app/common/service/DifyRequiredAttachmentUpload.php';
require dirname(__DIR__) . '/app/common/service/DifyChatService.php';
use app\common\service\DifyChatService;
use app\common\service\DifyRequiredAttachmentUpload;
function budgetExpect(bool $condition, string $message): void { if (!$condition) throw new RuntimeException($message); }
function budgetReset(string $scenario): void {
$GLOBALS['ocrBudgetScenario'] = $scenario; $GLOBALS['ocrBudgetClock'] = 1000.0;
$GLOBALS['ocrBudgetReads'] = []; $GLOBALS['ocrBudgetModels'] = [];
$GLOBALS['ocrBudgetUploads'] = 0; $GLOBALS['ocrBudgetCancelled'] = false;
}
$root = sys_get_temp_dir() . '/ocr-budget-test-' . bin2hex(random_bytes(8));
$public = $root . '/public'; $temporary = $root . '/private';
mkdir($public . '/uploads', 0700, true); mkdir($temporary, 0700);
$source = $public . '/uploads/synthetic.png';
file_put_contents($source, base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII='));
$files = [['type' => 'image', 'url' => 'https://app.synthetic.test/uploads/synthetic.png']];
$uploadOptions = ['public_root' => $public, 'temp_root' => $temporary,
'local_origin' => 'https://app.synthetic.test',
'ocr' => ['enabled' => true, 'timeout_seconds' => 60]];
$ocrBudgetConfig = ['enable' => true, 'base_url' => 'https://model.synthetic.test/v1/chat-messages',
'timeout' => 20, 'required_attachment_transport' => 'upload',
'required_attachment_upload' => $uploadOptions, 'required_attachment_ocr' => $uploadOptions['ocr'],
'models' => ['qwen' => ['name' => 'synthetic', 'api_key' => 'SYNTHETIC_ONLY_KEY', 'max_files' => 10]]];
$cases = 0;
try {
foreach ([false, true] as $streaming) {
budgetReset('exhaust');
$result = $streaming
? DifyChatService::streamChat('qwen', [], 'SYNTHETIC_ORIGINAL_QUESTION', 'synthetic-user', static fn (): bool => true,
null, $files, ['required_files' => true])
: DifyChatService::chat('qwen', [], 'SYNTHETIC_ORIGINAL_QUESTION', 'synthetic-user', $files, ['required_files' => true]);
budgetExpect(!empty($result['ok']) && count($ocrBudgetReads) === 1 && count($ocrBudgetModels) === 1,
'OCR reaching its child deadline plus cleanup must still permit the original visual model request');
budgetExpect(1020.0 - $ocrBudgetModels[0]['at'] >= 8.0 - 0.00001 && $ocrBudgetModels[0]['timeout_ms'] >= 7999,
'reserve at least eight seconds after worst-case 0.8 second OCR cleanup');
budgetExpect(($ocrBudgetModels[0]['payload']['files'][0]['upload_file_id'] ?? '') === 'synthetic-upload-1'
&& str_contains($ocrBudgetModels[0]['payload']['query'], 'SYNTHETIC_ORIGINAL_QUESTION'), 'original uploaded image and original clinical task survive optional OCR timeout');
budgetExpect(($result['items'][0]['text_check_status'] ?? '') === 'unavailable', 'skipped/unfinished optional OCR remains explicit in receipt');
$cases++;
}
foreach ([1, 9] as $limit) {
budgetReset('exhaust'); $ocrBudgetConfig['timeout'] = $limit;
$result = DifyChatService::chat('qwen', [], 'SYNTHETIC_ORIGINAL_QUESTION', 'synthetic-user', $files, ['required_files' => true]);
budgetExpect(!empty($result['ok']) && $ocrBudgetReads === [] && count($ocrBudgetModels) === 1,
'insufficient optional OCR budget skips OCR while the original model can still start');
budgetExpect(($result['items'][0]['text_check_status'] ?? '') === 'unavailable', 'low-budget text-check status must not claim OCR processing');
$cases++;
}
$ocrBudgetConfig['timeout'] = 90;
budgetReset('fixed');
$context = (object) ['ocr_remaining_seconds' => 4.0];
$options = $uploadOptions + ['ocr_context' => $context];
foreach (range(1, 3) as $batch) {
$result = DifyRequiredAttachmentUpload::prepare($ocrBudgetConfig['base_url'], 'SYNTHETIC_ONLY_KEY', 'synthetic-user', $files, 90, null, $options);
budgetExpect(!empty($result['ok']) && count($result['files']) === 1, 'each uploaded batch remains usable with exhausted OCR allocation');
}
budgetExpect(count($ocrBudgetReads) === 2 && abs($context->ocr_remaining_seconds - 1.5) < 0.00001,
'actual OCR time is deducted across batches, and insufficient work plus cleanup balance does not restart OCR');
budgetExpect(abs($ocrBudgetReads[0]['deadline'] - $ocrBudgetReads[0]['start'] - 3.2) < 0.00001
&& abs($ocrBudgetReads[1]['deadline'] - $ocrBudgetReads[1]['start'] - 1.95) < 0.00001,
'each later batch receives only the shared balance after reserving cleanup');
$cases++;
budgetReset('exhaust');
$context = (object) ['ocr_remaining_seconds' => 3.0];
foreach ([1, 2] as $batch) {
$result = DifyRequiredAttachmentUpload::prepare($ocrBudgetConfig['base_url'], 'SYNTHETIC_ONLY_KEY', 'synthetic-user', $files, 90, null,
$uploadOptions + ['ocr_context' => $context]);
budgetExpect(!empty($result['ok']), 'shared OCR exhaustion never removes original images');
}
budgetExpect(count($ocrBudgetReads) === 1 && abs($context->ocr_remaining_seconds) < 0.00001
&& abs($ocrBudgetReads[0]['deadline'] - $ocrBudgetReads[0]['start'] - 2.2) < 0.00001,
'last OCR call plus worst-case cleanup stays within the cumulative shared allocation');
$cases++;
budgetReset('throw');
$context = (object) ['ocr_remaining_seconds' => 5.0];
$result = DifyRequiredAttachmentUpload::prepare($ocrBudgetConfig['base_url'], 'SYNTHETIC_ONLY_KEY', 'synthetic-user', $files, 90, null,
$uploadOptions + ['ocr_context' => $context]);
budgetExpect(!empty($result['ok']) && $result['image_text_check']['status'] === 'unavailable'
&& abs($context->ocr_remaining_seconds - 3.75) < 0.00001, 'failed OCR also spends its actual elapsed time without removing the original image');
$cases++;
budgetReset('fixed');
$local = $uploadOptions; $local['ocr']['timeout_seconds'] = 2.0;
foreach ([1, 2] as $batch) budgetExpect(!empty(DifyRequiredAttachmentUpload::prepare($ocrBudgetConfig['base_url'], 'SYNTHETIC_ONLY_KEY',
'synthetic-user', $files, 90, null, $local)['ok']), 'absence of a shared context uses local timeout');
budgetExpect(count($ocrBudgetReads) === 2 && abs($ocrBudgetReads[1]['deadline'] - $ocrBudgetReads[1]['start'] - 2.0) < 0.00001,
'legacy callers retain configured per-call optional timeout');
$cases++;
budgetReset('exhaust');
$context = (object) ['deadline' => 1012.0, 'ocr_remaining_seconds' => 30.0];
$result = DifyRequiredAttachmentUpload::prepare($ocrBudgetConfig['base_url'], 'SYNTHETIC_ONLY_KEY', 'synthetic-user', $files, 90, null,
$uploadOptions + ['ocr_context' => $context]);
budgetExpect(!empty($result['ok']) && 1012.0 - $ocrBudgetClock >= 8.0 - 0.00001,
'optional original absolute deadline clamps OCR even if integer upload timeout is larger');
$cases++;
budgetReset('cancel');
$result = DifyChatService::streamChat('qwen', [], 'SYNTHETIC_ORIGINAL_QUESTION', 'synthetic-user', static fn (): bool => true,
static fn (): bool => $GLOBALS['ocrBudgetCancelled'], $files, ['required_files' => true]);
budgetExpect(empty($result['ok']) && $result['error_code'] === 'AI_ASSISTANT_CANCELLED' && $ocrBudgetModels === [],
'cancellation during optional OCR stops the whole request, never falls through to model answering');
$cases++;
foreach ([0.0, 0.9, NAN] as $remaining) {
budgetReset('exhaust');
$context = (object) ['ocr_remaining_seconds' => $remaining];
$result = DifyRequiredAttachmentUpload::prepare($ocrBudgetConfig['base_url'], 'SYNTHETIC_ONLY_KEY', 'synthetic-user', $files, 90, null,
$uploadOptions + ['ocr_context' => $context]);
budgetExpect(!empty($result['ok']) && $ocrBudgetReads === [] && $result['image_text_check']['status'] === 'unavailable',
'empty/sub-second/invalid shared OCR budget never prevents the original uploaded image');
$cases++;
}
budgetExpect((glob($temporary . '/*') ?: []) === [], 'authorized request-private image copies are cleaned');
echo "Dify OCR optional budget: {$cases} offline scenarios OK; eight-second model reserve, cleanup reserve, shared elapsed accounting and cancellation\n";
} finally {
foreach (glob($temporary . '/*') ?: [] as $path) unlink($path);
unlink($source); rmdir($public . '/uploads'); rmdir($public); rmdir($temporary); rmdir($root);
}
}
@@ -76,6 +76,7 @@ namespace {
$root = dirname(__DIR__, 2);
require $root . '/server/app/common/logic/BaseLogic.php';
require $root . '/server/app/common/service/DifyChatService.php';
require $root . '/server/app/common/service/DifyStreamTiming.php';
require $root . '/server/app/adminapi/service/AssistantSseTiming.php';
require $root . '/server/app/adminapi/logic/tcm/DiagnosisAiLogic.php';
use app\common\service\DifyChatService;
@@ -91,7 +92,9 @@ namespace {
$files = array_map(static fn (int $id): array => ['type' => 'image', 'url' => 'https://synthetic.invalid/' . $id . '.png'], [1, 2]);
$sections = array_map(static fn (int $id): array => ['key' => 'source_' . $id, 'label' => 'synthetic',
'count' => $id === 13 ? 0 : 1, 'availability' => $id === 13 ? 'empty' : 'present'], range(1, 13));
$options = ['required_files' => true, 'knowledge_mode' => 'evidence', 'required_sections' => $sections];
// Test the one-attempt classification boundary in isolation. Public-entry
// clinical batching/recovery is covered by DifyClinicalAttachmentBatchTest.
$options = ['required_files' => true, '_required_batch_internal' => true, 'knowledge_mode' => 'evidence', 'required_sections' => $sections];
$reset = static function (array $fixtures): void {
$GLOBALS['classificationFixtures'] = $fixtures;
$GLOBALS['classificationRequests'] = [];
@@ -62,7 +62,9 @@ namespace {
loopbackRequiredExpect(is_string($ready['address'] ?? null), 'loopback peer supplies ephemeral port');
$loopbackRequiredConfig = ['enable' => true, 'base_url' => 'http://' . $ready['address'] . '/v1/chat-messages',
'timeout' => 5, 'max_files' => 3, 'models' => ['qwen' => ['name' => 'synthetic', 'api_key' => 'loopback-fixture-only']]];
$options = ['required_files' => true, 'required_sections' => $sections, 'knowledge_mode' => 'evidence'];
// A one-connection peer verifies the physical transport boundary; the
// public multi-call coordinator has separate offline batching coverage.
$options = ['required_files' => true, '_required_batch_internal' => true, 'required_sections' => $sections, 'knowledge_mode' => 'evidence'];
$result = $scenario === 'streaming'
? DifyChatService::streamChat('qwen', [], '合成资料;聊天:无', 'synthetic-user', static fn (string $delta): bool => true, null, $files, $options)
: DifyChatService::chat('qwen', [], '合成资料;聊天:无', 'synthetic-user', $files, $options);
@@ -21,7 +21,7 @@ namespace app\common\service {
$stream = ($payload['response_mode'] ?? '') === 'streaming' || !empty($payload['stream']);
$GLOBALS['requiredRequests'][] = ['url' => $handle->url, 'payload' => $payload];
$scenario = $GLOBALS['requiredScenario'];
$handle->code = $scenario === 'reject' ? 413 : ($scenario === 'input_rejected' && !empty($payload['inputs']) ? 400
$handle->code = $scenario === 'reject' && !empty($payload['files']) ? 413 : ($scenario === 'input_rejected' && !empty($payload['inputs']) ? 400
: ($scenario === 'fallback' && str_ends_with($handle->url, '/chat-messages') ? 404 : 200));
if (isset($handle->options[CURLOPT_HEADERFUNCTION])) {
($handle->options[CURLOPT_HEADERFUNCTION])($handle, 'HTTP/1.1 ' . $handle->code . " Synthetic\r\n");
@@ -39,13 +39,16 @@ namespace app\common\service {
}
return $errorBody;
}
if (!$stream) return json_encode(['answer' => '合成回答']);
$answer = ($payload['inputs']['synthetic_task'] ?? '') === 'prescription_generate'
? json_encode(['clinical_diagnosis' => '合成测试证候', 'herbs' => [['name' => '合成药名', 'dosage' => 1]],
'rationale' => '合成草稿,需人工复核'], JSON_UNESCAPED_UNICODE) : '合成回答';
if (!$stream) return json_encode(['answer' => $answer]);
$write = $handle->options[CURLOPT_WRITEFUNCTION];
if (str_ends_with($handle->url, '/chat-messages')) {
$write($handle, 'data: ' . json_encode(['event' => 'message', 'answer' => '合成回答']) . "\n\n");
$write($handle, 'data: ' . json_encode(['event' => 'message', 'answer' => $answer]) . "\n\n");
$write($handle, "data: {\"event\":\"message_end\"}\n\n");
} else {
$write($handle, 'data: ' . json_encode(['choices' => [['delta' => ['content' => '合成回答']]]]) . "\n\n");
$write($handle, 'data: ' . json_encode(['choices' => [['delta' => ['content' => $answer]]]]) . "\n\n");
$write($handle, "data: [DONE]\n\n");
}
return true;
@@ -87,23 +90,25 @@ namespace {
$result = $streaming
? DifyChatService::streamChat('qwen', [], '合成资料;聊天:无', 'synthetic-user', static fn (string $text): bool => true, null, $withVideos, $withProgress)
: DifyChatService::chat('qwen', [], '合成资料;聊天:无', 'synthetic-user', $withVideos, $withProgress);
requiredExpect(!empty($result['ok']) && count($requiredRequests) === 1, 'required request succeeds exactly once');
requiredExpect(count($requiredRequests[0]['payload']['files']) === 8 && $result['transmitted_file_count'] === 8,
'all eight images/documents reach serialized wire payload despite default max_files=3');
requiredExpect(array_column($requiredRequests[0]['payload']['files'], 'url') === array_column($files, 'url'), 'video references never enter payload');
requiredExpect(!empty($result['ok']) && count($requiredRequests) === 4, 'three evidence batches finish with one original clinical task');
$wireFiles = array_merge(...array_map(static fn (array $request): array => $request['payload']['files'] ?? [], $requiredRequests));
requiredExpect(count($wireFiles) === 8 && $result['transmitted_file_count'] === 8,
'all eight images/documents reach bounded batches with max_files=3');
requiredExpect(array_column($wireFiles, 'url') === array_column($files, 'url'), 'video references never enter payload and original positions remain complete');
requiredExpect($result['transfer_manifest_final'] === true && $result['attachment_count'] === 8
&& array_unique(array_column($result['items'], 'status')) === ['submitted'], 'final attachment manifest reflects actual complete payload');
requiredExpect(count($result['required_sections']) === 13
&& array_unique(array_column($result['required_sections'], 'status')) === ['submitted']
&& $result['required_sections'][12]['availability'] === 'empty', 'empty required text section is explicitly submitted, not omitted');
requiredExpect($events[0]['required_sections'][0]['status'] === 'submitting'
&& $events[1]['source_summary']['required_sections'][0]['status'] === 'submitted', 'progress moves actual source manifest from submitting to submitted');
requiredExpect($events[0]['required_sections'][0]['status'] === 'prepared'
&& $events[count($events) - 1]['source_summary']['required_sections'][0]['status'] === 'submitted', 'full clinical body stays prepared until final original task submission');
$reset('reject');
$result = $streaming
? DifyChatService::streamChat('qwen', [], '合成资料', 'synthetic-user', static fn (string $text): bool => true, null, $files, $options)
: DifyChatService::chat('qwen', [], '合成资料', 'synthetic-user', $files, $options);
requiredExpect(($result['error_code'] ?? '') === 'REQUIRED_FILES_REJECTED' && count($requiredRequests) === 1
&& count($requiredRequests[0]['payload']['files']) === 8, 'hard provider limit fails explicitly without stripping files');
requiredExpect(!empty($result['ok']) && $result['rejected_attachment_count'] === 8
&& $result['submitted_attachment_count'] === 0 && count($requiredRequests) <= 17
&& empty($requiredRequests[count($requiredRequests) - 1]['payload']['files']), 'confirmed per-file rejection finishes original task with eight explicit unread gaps');
}
foreach ([['type' => 'unknown', 'url' => 'https://synthetic.invalid/file'], ['type' => 'image', 'url' => 'invalid'],
['type' => 'custom', 'url' => 'https://synthetic.invalid/file'], ['type' => 'image', 'url' => 'https://synthetic.invalid/a.png', 'transfer_method' => 'local_file']] as $invalid) {
@@ -115,9 +120,10 @@ namespace {
$result = $streaming
? DifyChatService::streamChat('qwen', ['synthetic_undeclared' => 'value'], '合成正文', 'synthetic-user', static fn (string $text): bool => true, null, $files, $options)
: DifyChatService::chat('qwen', ['synthetic_undeclared' => 'value'], '合成正文', 'synthetic-user', $files, $options);
requiredExpect(!empty($result['ok']) && count($requiredRequests) === 2
&& count($requiredRequests[0]['payload']['files']) === 8 && count($requiredRequests[1]['payload']['files']) === 8,
'undeclared Dify inputs retry preserves complete required files and query');
requiredExpect(!empty($result['ok']) && count($requiredRequests) === 5
&& $result['submitted_attachment_count'] === 8
&& $requiredRequests[3]['payload']['query'] === $requiredRequests[4]['payload']['query'],
'undeclared Dify inputs retry preserves final complete original body and all batch evidence');
}
$reset();
$fallbackOptions = array_replace($options, ['required_sections' => []]);
@@ -126,17 +132,22 @@ namespace {
'source_summary manifest fallback keeps all text sections when attachment list is empty');
$requiredConfig['base_url'] = 'https://synthetic.invalid/v1/chat/completions';
$reset(); $result = DifyChatService::chat('qwen', [], '合成资料', 'synthetic-user', $files, $options);
requiredExpect(($result['error_code'] ?? '') === 'REQUIRED_FILES_UNSUPPORTED' && $requiredRequests === [], 'OpenAI documents fail before unsupported transmission');
requiredExpect(!empty($result['ok']) && $result['submitted_attachment_count'] === 4
&& $result['rejected_attachment_count'] === 4, 'OpenAI documents become explicit unread gaps while every supported image and original body continue');
$reset(); $result = DifyChatService::chat('qwen', [], '合成资料', 'synthetic-user', array_slice($files, 0, 4), $options);
requiredExpect(!empty($result['ok']) && count($requiredRequests[0]['payload']['messages'][0]['content']) === 5, 'all four OpenAI image_url parts are serialized');
requiredExpect(!empty($result['ok']) && $result['submitted_attachment_count'] === 4
&& count($requiredRequests) === 3 && count($requiredRequests[0]['payload']['messages'][0]['content']) === 4
&& count($requiredRequests[1]['payload']['messages'][0]['content']) === 2, 'all four OpenAI image_url parts are serialized within the configured cap');
$requiredConfig['base_url'] = 'https://synthetic.invalid/v1';
$reset('fallback'); $result = DifyChatService::chat('qwen', [], '合成资料', 'synthetic-user', $files, $options);
requiredExpect(($result['error_code'] ?? '') === 'REQUIRED_FILES_UNSUPPORTED' && count($requiredRequests) === 1, 'protocol fallback cannot fake document submission');
requiredExpect(!empty($result['ok']) && $result['submitted_attachment_count'] === 4
&& $result['rejected_attachment_count'] === 4, 'protocol fallback cannot fake document submission and preserves explicit gaps');
$requiredConfig['base_url'] = 'https://synthetic.invalid/v1/chat-messages';
$prepared = ['profile' => 'qwen', 'task' => 'summary', 'query' => '合成资料;聊天:无', 'user' => 'synthetic-user',
'files' => $files, 'required_sections' => $sections, 'source_summary' => ['required_sections' => $sections], 'upstream_timeout' => 5];
$run = static function () use ($prepared): array {
$run = static function (array $overrides = []) use ($prepared): array {
$prepared = array_replace($prepared, $overrides);
$deltas = []; $events = [];
$result = DiagnosisAiLogic::streamPreparedAssistant($prepared,
static function (string $delta) use (&$deltas): bool { $deltas[] = $delta; return true; },
@@ -148,9 +159,25 @@ namespace {
requiredExpect($test['result']['transfer_manifest_final'] === true && $test['result']['attachment_count'] === 8
&& $test['result']['required_sections'][0]['status'] === 'submitted'
&& $test['result']['source_summary']['required_sections'][0]['status'] === 'submitted', 'assistant done preserves actual final manifest instead of prepared status');
foreach (['summary', 'tcm_pattern', 'prescription_review', 'prescription_generate', 'medication_review',
'exam_review', 'complication_risk', 'guideline_review', 'custom'] as $task) {
$reset();
$taskQuery = '完整合成病历;TASK=' . $task . ';保留原始任务输出结构';
$taskInputs = ['synthetic_task' => $task, 'clinical_context' => $taskQuery];
$test = $run(['task' => $task, 'query' => $taskQuery, 'inputs' => $taskInputs,
'files' => array_slice($files, 0, 7)]);
requiredExpect(is_array($test['result']) && $test['result']['task'] === $task
&& $test['result']['attachment_count'] === 7 && $test['result']['submitted_attachment_count'] === 7
&& count($requiredRequests) === 4 && count($test['deltas']) === 1, 'all nine real assistant task routes retain all seven attachments and emit only final answer: ' . $task);
$final = $requiredRequests[3]['payload'];
requiredExpect(str_starts_with($final['query'], $taskQuery) && $final['inputs'] === $taskInputs,
'each original assistant task and all original inputs survive evidence synthesis');
if ($task === 'prescription_generate') requiredExpect(is_array($test['result']['prescription_draft'] ?? null),
'JSON prescription schema stays parseable without a prose notice prefix');
}
$reset('reject'); $test = $run();
requiredExpect($test['result'] === null && DiagnosisAiLogic::getAssistantErrorCode() === 'REQUIRED_FILES_REJECTED'
&& count($requiredRequests) === 1 && $test['deltas'] === [], 'assistant mandatory rejection does not trigger blocking compatibility retry');
requiredExpect(is_array($test['result']) && $test['result']['submitted_attachment_count'] === 0
&& count($requiredRequests) <= 17 && $test['deltas'] === ['合成回答'], 'assistant receives only final clinical answer after bounded affirmative file isolation');
$reset('cancel'); $test = $run();
requiredExpect($test['result'] === null && DiagnosisAiLogic::getAssistantErrorCode() === 'AI_ASSISTANT_CANCELLED'
&& count($requiredRequests) === 1 && $requiredClosed === 1 && $test['deltas'] === [], 'cancellation still closes transport without late answer or retry');
+19
View File
@@ -46,6 +46,25 @@ $unknown = new DifyStreamTiming($clock);
$unknown->frame(['event' => 'private-secret-event', 'data' => ['node_id' => 'private-id']], 'dify');
streamTimingExpect(isset($unknown->snapshot()['events']['other']) && !str_contains(json_encode($unknown->snapshot()), 'private-'),
'unknown provider strings never become log labels');
$vision = new DifyStreamTiming($clock);
$vision->frame(['event' => 'node_finished', 'data' => ['id' => 'private-vision-node', 'node_type' => 'llm',
'inputs' => ['#files#' => [['type' => 'image', 'url' => 'private-source'], ['type' => 'document']]],
'process_data' => ['prompts' => [['role' => 'user', 'text' => 'private-prompt', 'files' => [
['type' => 'image', 'data' => 'private-image', 'detail' => 'high'],
['type' => 'image', 'data' => 'private-image', 'detail' => 'high'],
]]]]]], 'dify');
$visionNode = $vision->snapshot()['nodes'][0];
streamTimingExpect($visionNode['input_image_count'] === 1 && $visionNode['model_image_part_count'] === 2
&& $visionNode['model_high_detail_image_part_count'] === 2, 'detect expanded image input without retaining images');
streamTimingExpect(!str_contains(json_encode($vision->snapshot()), 'private-'), 'vision counts never retain prompts or sources');
$notExposed = new DifyStreamTiming($clock);
$notExposed->frame(['event' => 'node_finished', 'data' => ['id' => 'opaque', 'node_type' => 'llm']], 'dify');
streamTimingExpect(!isset($notExposed->snapshot()['nodes'][0]['model_image_part_count']), 'unexposed processing is unknown, not zero images');
foreach (['list-operator', 'document-extractor'] as $type) {
$notExposed->frame(['event' => 'node_finished', 'data' => ['id' => $type, 'node_type' => $type]], 'dify');
}
streamTimingExpect(array_column($notExposed->snapshot()['nodes'], 'type') === ['llm', 'list-operator', 'document-extractor'],
'document preparation timings have fixed safe node labels');
$openai = new DifyStreamTiming($clock);
$now += 10; $openai->frame(['choices' => [['delta' => ['reasoning_content' => 'private-thought']]]], 'openai');
streamTimingExpect(!isset($openai->snapshot()['first_answer_ms']), 'reasoning content is not a visible first token');
+163
View File
@@ -0,0 +1,163 @@
"""Offline worker tests with fake inference; no model or source network access."""
import importlib.util
import math
import socket
import struct
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "clinical_image_ocr.py"
SPEC = importlib.util.spec_from_file_location("clinical_image_ocr", SCRIPT)
ocr = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(ocr)
class Image:
def close(self):
pass
def detected(text="SYNTHETIC", confidence=0.9, box=None):
return SimpleNamespace(txts=[text], scores=[confidence], boxes=[box or [[0, 0], [1, 0], [1, 1], [0, 1]]])
class WorkerTests(unittest.TestCase):
def setUp(self):
self.private = tempfile.TemporaryDirectory(prefix="ocr-worker-test-")
self.root = Path(self.private.name)
self.image = self.root / "image.png"
self.image.write_bytes(b"synthetic fake image for injected decoder")
self.request = {"allowed_root": str(self.root), "images": [
{"index": 7, "path": str(self.image)}, {"index": 19, "path": str(self.image)},
], "deadline_ms": 1000, "threads": 1, "detection_limit": 640, "max_text_bytes": 4096}
def tearDown(self):
self.private.cleanup()
def run_worker(self, request=None, factory=None, loader=None, checkpoint=None):
return ocr.run(request or self.request, engine_factory=factory or (lambda *_: lambda _image: detected()),
image_loader=loader or (lambda _: Image()), checkpoint=checkpoint)
def test_complete_global_indices_and_one_engine(self):
calls = []
def factory(threads, detection):
calls.append((threads, detection))
return lambda _image: detected()
result = self.run_worker(factory=factory)
self.assertEqual(result["status"], "completed")
self.assertEqual([row["index"] for row in result["images"]], [7, 19])
self.assertEqual(calls, [(1, 640)])
def test_invalid_indices_limits_paths_and_no_model_load(self):
variants = [
{"images": [{"index": 7, "path": str(self.image)}] * 2},
{"images": [{"index": True, "path": str(self.image)}]},
{"images": [{"index": 7, "path": str(SCRIPT)}]},
{"images": [{"index": 7, "path": "https://synthetic.invalid/image.png"}]},
{"images": [{"index": 7, "path": "\\\\synthetic.invalid\\share\\image.png"}]},
{"allowed_root": "//synthetic.invalid/share"},
{"threads": 3}, {"detection_limit": 10000}, {"max_text_bytes": 9999999},
{"deadline_ms": -1}, {"deadline_ms": 0},
]
for changed in variants:
with self.subTest(changed=list(changed)):
result = self.run_worker({**self.request, **changed}, factory=lambda *_: self.fail("must not load model"))
self.assertEqual(result["status"], "unavailable")
def test_too_many_bytes_and_symlink_outside(self):
with self.image.open("wb") as handle:
handle.truncate(ocr.MAX_BYTES + 1)
self.assertEqual(self.run_worker()["status"], "unavailable")
self.image.write_bytes(b"synthetic")
link = self.root / "outside.png"
try:
link.symlink_to(SCRIPT)
except OSError:
return
result = self.run_worker({**self.request, "images": [{"index": 7, "path": str(link)}]})
self.assertEqual(result["status"], "unavailable")
def test_pixel_header_guard_before_large_decode(self):
from PIL import Image as PillowImage
image = self.root / "large.bmp"
image.write_bytes(b"BM" + struct.pack("<IHHI", 54, 0, 0, 54)
+ struct.pack("<IiiHHIIiiII", 40, 10000, 10000, 1, 24, 0, 0, 0, 0, 0, 0))
with self.assertRaises((ocr.Unavailable, PillowImage.DecompressionBombError, PillowImage.DecompressionBombWarning)):
ocr.load_image(image)
def test_network_denied_and_socket_functions_restored(self):
original = socket.socket.connect
def factory(*_):
with self.assertRaises(OSError):
socket.create_connection(("example.test", 443))
return lambda _image: detected()
self.assertEqual(self.run_worker(factory=factory)["status"], "completed")
self.assertIs(socket.socket.connect, original)
def test_first_unreadable_does_not_block_other_source(self):
calls = []
def loader(_):
calls.append(True)
if len(calls) == 1:
raise OSError("private decoder error")
return Image()
result = self.run_worker(loader=loader)
self.assertEqual(result["status"], "partial")
self.assertEqual([row["status"] for row in result["images"]], ["unreadable", "processed"])
self.assertNotIn("private decoder error", str(result))
def test_deadline_checkpoint_preserves_completed_and_unfinished_rows(self):
clock, checkpoints = [0.0], []
def checkpoint(value):
checkpoints.append([row["status"] for row in value["images"]])
clock[0] = 2.0
with patch.object(ocr.time, "monotonic", side_effect=lambda: clock[0]):
result = self.run_worker(checkpoint=checkpoint)
self.assertEqual(result["status"], "partial")
self.assertEqual(result["reason"], "deadline")
self.assertEqual(checkpoints, [["processed", "unreadable"]])
def test_invalid_confidence_boxes_and_text_never_become_processed(self):
for output in [detected(confidence=math.nan), detected(confidence=2), detected(text="bad\0text"),
detected(box=[[math.inf, 0]] * 4), detected(text="x" * 9000)]:
with self.subTest():
result = self.run_worker(factory=lambda *_, value=output: lambda _image: value)
self.assertEqual(result["status"], "unavailable")
self.assertTrue(all(not row["lines"] for row in result["images"]))
def test_shared_text_budget_keeps_first_source_and_marks_later_unreadable(self):
result = self.run_worker({**self.request, "max_text_bytes": len("SYNTHETIC")})
self.assertEqual(result["status"], "partial")
self.assertEqual([row["status"] for row in result["images"]], ["processed", "unreadable"])
def test_dependency_failure_is_safe_and_covers_every_index(self):
def factory(*_):
raise RuntimeError("private model path and stderr")
result = self.run_worker(factory=factory)
self.assertEqual(result["status"], "unavailable")
self.assertEqual(len(result["images"]), 2)
self.assertNotIn("private", str(result))
def test_missing_local_models_fail_before_import_or_download(self):
with patch.object(ocr.importlib.metadata, "version", side_effect=lambda name: {"rapidocr": "3.10.0", "onnxruntime": "1.31.0"}[name]), \
patch.object(ocr.importlib.util, "find_spec", return_value=SimpleNamespace(origin=str(self.root / "__init__.py"))), \
self.assertRaises(FileNotFoundError):
ocr.load_engine(1, 640)
if __name__ == "__main__":
unittest.main()