'application/json'], json_encode(['error' => [ 'code' => $code, 'message' => 'Traceback: /root/private/key secret=never-expose', ]], JSON_THROW_ON_ERROR)); } $client = new CloudAgentClient( '1', 'test-secret-which-is-at-least-32-bytes', new Client(['handler' => HandlerStack::create(new MockHandler($responses))]), 20.0, 'http://127.0.0.1:8001/internal/agent/request', 'test-gateway-secret-at-least-32-bytes' ); $actor = [ 'user_id' => 1, 'is_admin' => true, 'is_super_admin' => true, 'can_manage' => true, ]; foreach ($cases as [$status, $code, $panelStatus, $retryable, $message]) { try { $client->request('POST', '/v1/vms/test-operation', $actor, []); throw new RuntimeException('Agent error response was accepted: ' . $status); } catch (CloudAgentError $error) { $assert($error->agentCode === $code && $error->agentHttpStatus === $status && $error->httpStatus === $panelStatus && $error->retryable === $retryable && $error->getMessage() === $message && !str_contains($error->getMessage(), '/root') && !str_contains($error->getMessage(), 'never-expose'), 'Agent status mapping or error redaction changed: ' . $status); } } $fallback = new CloudAgentClient( '1', 'test-secret-which-is-at-least-32-bytes', new Client(['handler' => HandlerStack::create(new MockHandler([ new Response(400, ['Content-Type' => 'application/json'], json_encode([ 'error' => ['code' => '../unsafe', 'message' => 'secret'], ], JSON_THROW_ON_ERROR)), ]))]), 20.0, 'http://127.0.0.1:8001/internal/agent/request', 'test-gateway-secret-at-least-32-bytes' ); try { $fallback->request('POST', '/v1/vms/test-operation', $actor, []); throw new RuntimeException('Invalid Agent error code was accepted.'); } catch (CloudAgentError $error) { $assert($error->agentCode === 'agent_http_error' && $error->httpStatus === 422, 'Invalid Agent error code did not fall back to a stable panel code.'); } echo "Cloud Agent error-code and redaction contract passed.\n";