File size: 28,828 Bytes
6c50d1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
import copy
import random
import threading
import time
from collections import defaultdict
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass
from json import JSONDecodeError
from typing import Any, Callable, Dict, List, Optional, Set, TYPE_CHECKING

import numpy as np

import requests
import pickle
import os

from autoresttest.config import get_config
from autoresttest.models import (
    OperationProperties,
    ParameterKey,
    ParameterProperties,
    RequestData,
    RequestRequirements,
    RequestResponse,
    SchemaProperties,
)

from autoresttest.utils import (
    remove_nulls,
    split_parameter_values,
    get_params,
    get_request_body_params,
    get_param_combinations,
    get_required_params,
    get_body_object_combinations,
    dispatch_request,
    get_accept_header,
)
from autoresttest.llm import NaiveValueGenerator, SmartValueGenerator

if TYPE_CHECKING:
    from .generate_graph import OperationGraph, OperationNode, OperationEdge

CONFIG = get_config()


@dataclass
class StatusCode:
    status_code: int
    count: int
    requests_and_responses: List[RequestResponse]


class RequestGenerator:
    def __init__(self, operation_graph: "OperationGraph", api_url: str, is_naive=True):
        self.operation_graph: "OperationGraph" = operation_graph
        self.api_url = api_url
        self.status_codes: Dict[int, StatusCode] = (
            {}
        )  # dictionary to track status code occurrences
        self.requests_generated = 0  # Initialize the request count
        self.successful_query_data = []  # List to store successful query data
        self.is_naive = is_naive
        self.responses: Dict[str, RequestResponse] = (
            {}
        )  # Dictionary to store responses for each operation_id
        self.allowed_retries = 1

    @staticmethod
    def generate_naive_values(
        parameters: Dict[ParameterKey, ParameterProperties],
        request_body: Dict[str, SchemaProperties] | None,
    ):
        value_generator = NaiveValueGenerator(
            parameters=parameters, request_body=request_body
        )
        return value_generator.generate_parameters() if parameters else None, (
            value_generator.generate_request_body() if request_body else None
        )

    @staticmethod
    def generate_smart_values(
        operation_properties: OperationProperties,
        requirements: RequestRequirements | None = None,
    ):
        """
        Generate smart values for parameters and request body using LLMs
        :param operation_properties: Dictionary mapping of operation properties
        :param requirements: RequestRequirements object that contains any parameters or request body requirements
        :return: a tuple of the generated parameters and request body
        """
        value_generator = SmartValueGenerator(
            operation_properties=operation_properties, requirements=requirements
        )
        return (
            value_generator.generate_parameters(),
            value_generator.generate_request_body(),
        )

    def make_request_data(
        self,
        operation_properties: OperationProperties,
        requirements: RequestRequirements | None = None,
    ) -> RequestData:
        """
        Process the operation properties by preparing request data for queries with mapping values to parameters and request body
        """
        endpoint_path = operation_properties.endpoint_path
        http_method = operation_properties.http_method.lower()

        if self.is_naive:
            parameters, request_body = self.generate_naive_values(
                operation_properties.parameters, operation_properties.request_body
            )
        else:
            parameters, request_body = self.generate_smart_values(
                operation_properties=operation_properties, requirements=requirements
            )

        return RequestData(
            endpoint_path=endpoint_path,
            http_method=http_method,
            parameters=parameters,
            request_body=request_body,
            operation_properties=operation_properties,
            requirements=requirements,
        )

    def make_request_retry_data(
        self, request_data: RequestData, response: requests.Response
    ) -> RequestData:
        if self.is_naive:
            return request_data
        else:
            requirements = request_data.requirements
            value_generator = SmartValueGenerator(
                operation_properties=request_data.operation_properties,
                requirements=requirements,
                temperature=CONFIG.strict_temperature,
            )
            parameters, request_body = value_generator.generate_retry_parameters(
                request_data, response
            ), value_generator.generate_retry_request_body(request_data, response)
            return RequestData(
                endpoint_path=request_data.endpoint_path,
                http_method=request_data.http_method,
                parameters=parameters,
                request_body=request_body,
                operation_properties=request_data.operation_properties,
                requirements=requirements,
            )

    def _handle_retry(
        self,
        request_data: RequestData,
        response: requests.Response,
        curr_retry: int,
        permitted_retries: int = 1,
    ) -> Optional[RequestResponse]:
        retry_data = self.make_request_retry_data(request_data, response)
        retry_response = self.send_operation_request(
            retry_data,
            retry_nums=curr_retry + 1,
            allow_retry=True,
            permitted_retries=permitted_retries,
        )
        return retry_response

    def _determine_auth_mappings(
        self, request_val: Any, auth_parameters: Dict, auth_mappings
    ):
        if isinstance(request_val, dict):
            for item, properties in request_val.items():
                item_name = item[0] if isinstance(item, tuple) else item
                if auth_parameters.get("username") == item_name:
                    auth_mappings["username"] = properties
                elif auth_parameters.get("password") == item_name:
                    auth_mappings["password"] = properties
                else:
                    self._determine_auth_mappings(
                        properties, auth_parameters, auth_mappings
                    )
        elif isinstance(request_val, list):
            for item in request_val:
                self._determine_auth_mappings(item, auth_parameters, auth_mappings)

    def _determine_if_valid_auth(self, param_auth):
        if not param_auth or not isinstance(param_auth, dict):
            return False
        return (
            param_auth.get("username")
            and param_auth.get("username") != "None"
            and param_auth.get("password")
            and param_auth.get("password") != "None"
        )

    def get_auth_info(self, operation_node: "OperationNode", auth_attempts: int = 3):
        operation_properties = operation_node.operation_properties
        value_generator = SmartValueGenerator(operation_properties=operation_properties)

        auth_parameters = value_generator.determine_auth_params()
        # print(f"Auth parameters for operation {operation_node.operation_id}: {auth_parameters}")
        if not auth_parameters or auth_parameters == "None":
            return []
        query_param_auth = auth_parameters.get("query_parameters")
        body_param_auth = auth_parameters.get("body_parameters")

        print(
            "Attempting to get auth info for operation: ", operation_node.operation_id
        )

        token_info = []
        if query_param_auth and self._determine_if_valid_auth(query_param_auth):
            self.determine_tokens(operation_node, query_param_auth, token_info)
        elif body_param_auth and self._determine_if_valid_auth(body_param_auth):
            self.determine_tokens(operation_node, body_param_auth, token_info, True)
        return token_info

    def _decompose_body_prop_mappings(self, bodies: List):
        body_mappings = {}
        for body in bodies:
            if isinstance(body, dict):
                for key, value in body.items():
                    if key not in body_mappings:
                        body_mappings[key] = []
                    body_mappings[key].append(value)
        return body_mappings

    def determine_tokens(
        self,
        operation_node: "OperationNode",
        param_auth_info,
        token_info,
        is_body=False,
    ):
        param_q_table = {"params": {}, "body": {}}
        params = get_param_combinations(operation_node.operation_properties.parameters)
        if not is_body:
            for param in params:
                # param is a tuple of ParameterKey tuples; p[0] extracts the parameter name
                param_names = {p[0] for p in param}
                if set(param_auth_info.values()).issubset(param_names):
                    param_q_table["params"][param] = 0
        else:
            param_q_table["params"] = {param: 0 for param in params}
        select_mime = (
            list(operation_node.operation_properties.request_body.keys())[0]
            if operation_node.operation_properties.request_body
            else None
        )
        body_properties: List = []
        if select_mime and operation_node.operation_properties.request_body:
            body_properties = get_body_object_combinations(
                operation_node.operation_properties.request_body[select_mime]
            )
        if is_body:
            for body in body_properties:
                if set(param_auth_info.values()).issubset(set(body)):
                    param_q_table["body"][body] = 0
        else:
            param_q_table["body"] = {key: 0 for key in body_properties}

        value_generator = SmartValueGenerator(
            operation_properties=operation_node.operation_properties
        )
        failed_responses = []
        for i in range(3):
            response = self.create_and_send_request(
                operation_node, allow_retry=True, permitted_retries=1
            )
            if response is not None and response.response and not response.response.ok:
                failed_responses.append(response)
        if failed_responses:
            parameter_mappings, request_body_mappings = (
                value_generator.generate_informed_value_agent_params(
                    num_values=10, responses=failed_responses
                ),
                value_generator.generate_informed_value_agent_body(
                    num_values=10, responses=failed_responses
                ),
            )
        else:
            parameter_mappings, request_body_mappings = (
                value_generator.generate_value_agent_params(num_values=10),
                value_generator.generate_value_agent_body(num_values=10),
            )

        request_body_mappings = (
            self._decompose_body_prop_mappings(request_body_mappings[select_mime])
            if select_mime and select_mime in request_body_mappings
            else {}
        )

        param_q_table["params"]["None"] = 0
        param_q_table["body"]["None"] = 0

        curr_tokens = []
        start_time = time.time()
        while time.time() - start_time < 30 and len(curr_tokens) < 5:
            # get best action
            required_params = get_required_params(
                operation_node.operation_properties.parameters
            )
            best_params = (None, -np.inf)
            if param_q_table["params"] and required_params:
                for params, score in param_q_table["params"].items():
                    if (
                        params
                        and required_params.issubset(set(params))
                        and score > best_params[1]
                    ):
                        best_params = (params, score)
                best_params = best_params[0]
            else:
                best_params = (
                    max(param_q_table["params"].items(), key=lambda x: x[1])[0]
                    if param_q_table["params"]
                    else None
                )

            # required_body = get_required_params(operation_node.operation_properties.request_body[select_mime].properties)
            best_body = (
                max(param_q_table["body"].items(), key=lambda x: x[1])[0]
                if param_q_table["body"]
                else None
            )

            if best_params == "None":
                best_params = None
            if best_body == "None":
                best_body = None

            parameters = {}
            request_body = {}
            if best_params:
                for param in best_params:
                    parameters[param] = (
                        random.choice(parameter_mappings[param])
                        if param in parameter_mappings
                        else None
                    )
            if best_body and select_mime:
                request_body[select_mime] = {}
                for prop in best_body:
                    request_body[select_mime][prop] = (
                        random.choice(request_body_mappings[prop])
                        if prop in request_body_mappings
                        else None
                    )

            response = self.send_operation_request(
                RequestData(
                    endpoint_path=operation_node.operation_properties.endpoint_path,
                    http_method=operation_node.operation_properties.http_method,
                    parameters=parameters,
                    request_body=request_body,
                    operation_properties=operation_node.operation_properties,
                ),
                allow_retry=False,
                permitted_retries=0,
            )

            if best_params is None:
                best_params = "None"
            if best_body is None:
                best_body = "None"

            if response and response.response.ok:
                auth_mappings = {}
                self._determine_auth_mappings(
                    response.request.parameters, param_auth_info, auth_mappings
                )
                if len(auth_mappings) == 2:
                    curr_tokens.append(auth_mappings)
                auth_mappings = {}
                self._determine_auth_mappings(
                    response.request.request_body, param_auth_info, auth_mappings
                )
                if len(auth_mappings) == 2:
                    curr_tokens.append(auth_mappings)
                param_q_table["params"][best_params] += 2
                param_q_table["body"][best_body] += 2
            else:
                param_q_table["params"][best_params] -= 1
                param_q_table["body"][best_body] -= 1

        if curr_tokens:
            for token in curr_tokens:
                if token not in token_info:
                    token_info.append(token)

    def send_operation_request(
        self,
        request_data: RequestData,
        retry_nums: int = 0,
        allow_retry: bool = False,
        permitted_retries: int = 1,
    ) -> Optional[RequestResponse]:
        """
        Send the operation request to the API
        :param request_data:
        :param retry_nums:
        :param allow_retry:
        :return:
        """
        endpoint_path = request_data.endpoint_path
        http_method = request_data.http_method
        parameters = request_data.parameters
        request_body = request_data.request_body
        processed_parameters = copy.deepcopy(parameters) or {}

        path_params, query_params, header_params, cookie_params = (
            split_parameter_values(
                request_data.operation_properties.parameters, processed_parameters
            )
        )

        for name, value in path_params.items():
            endpoint_path = endpoint_path.replace("{" + name + "}", str(value))

        try:
            select_method = getattr(
                requests, http_method
            )  # selects correct http method
            full_url = f"{self.api_url}{endpoint_path}"

            # Merge custom headers from config
            merged_headers = header_params.copy()
            merged_headers.update(get_config().static_headers)

            accept_header = get_accept_header(
                request_data.operation_properties.responses
            )
            response = dispatch_request(
                select_method=select_method,
                full_url=full_url,
                params=query_params,
                body=request_body,
                header=merged_headers,
                cookies=cookie_params,
                accept=accept_header,
            )
            if response is not None:
                if not response.ok and retry_nums < permitted_retries and allow_retry:
                    return self._handle_retry(
                        request_data, response, retry_nums, permitted_retries
                    )
                else:
                    return RequestResponse(
                        request=request_data,
                        response=response,
                        response_text=response.text,
                    )
            return None
        except requests.exceptions.RequestException as err:
            print(f"Request exception due to error: {err}")
            print(f"Endpoint Path: {endpoint_path}")
            print(f"Params: {parameters}")
            print(f"Request Body: {request_body}")
            return None
        except Exception as err:
            print(f"Unexpected error due to: {err}")
            print(f"Endpoint Path: {endpoint_path}")
            print(f"Params: {parameters}")
            print(f"Request Body: {request_body}")
            return None

    def _validate_value_mappings(
        self,
        curr_node: "OperationNode",
        parameter_mappings: Dict,
        req_param_mappings: Dict[ParameterKey, List[Any]],
        req_body_mappings: Dict[str, List[Any]],
        occurrences: Dict,
    ):
        operation_id = curr_node.operation_id
        if operation_id not in parameter_mappings:
            parameter_mappings[operation_id] = {"params": {}, "body": {}}

        operation_params = get_params(curr_node.operation_properties.parameters)
        # operation_body_params = get_request_body_params(curr_node.operation_properties.request_body)

        # take the reverse of mappings since API outputs least likely values first
        req_param_mappings = (
            {k: v for k, v in reversed(list(req_param_mappings.items()))}
            if req_param_mappings
            else {}
        )
        req_body_mappings = (
            {k: v for k, v in reversed(list(req_body_mappings.items()))}
            if req_body_mappings
            else {}
        )

        if req_param_mappings:
            for parameter, values in req_param_mappings.items():
                for value in values:
                    if parameter not in parameter_mappings[operation_id]["params"]:
                        parameter_mappings[operation_id]["params"][parameter] = []
                    if (
                        parameter in operation_params
                        and len(parameter_mappings[operation_id]["params"][parameter])
                        < 10
                    ):
                        parameter_mappings[operation_id]["params"][parameter].append(
                            [value, 0]
                        )
                        occurrences[parameter] = occurrences.get(parameter, 0) + 1

        if req_body_mappings:
            for mime, mime_params in req_body_mappings.items():
                if mime not in parameter_mappings[operation_id]["body"]:
                    parameter_mappings[operation_id]["body"][mime] = []
                for mime_param in mime_params:
                    num_bodies = len(parameter_mappings[operation_id]["body"][mime])
                    if num_bodies < 10:
                        parameter_mappings[operation_id]["body"][mime].append(
                            [mime_param, 0]
                        )
                        occurrences[mime] = occurrences.get(mime, 0) + 1

                # body_params = get_nested_obj_mappings(mime_params) # handler works for getting first obj values same as get_request_body_params
                # for body_param, value in body_params.items():
                #    if body_param not in parameter_mappings[operation_id]['body'][mime]:
                #        parameter_mappings[operation_id]['body'][mime][body_param] = {}
                #    if body_param in operation_body_params[mime] and len(parameter_mappings[operation_id]['body'][mime][body_param]) < 10:
                #        parameter_mappings[operation_id]['body'][mime][body_param][value] = 0
                #        occurrences[body_param] = occurrences.get(body_param, 0) + 1

    def value_depth_traversal(
        self,
        curr_node: "OperationNode",
        parameter_mappings: Dict,
        responses: Dict[str, List],
        visited: Set,
    ):
        visited.add(curr_node.operation_id)

        # Pre-initialize Q-table entry to ensure it exists even if later steps fail
        if curr_node.operation_id not in parameter_mappings:
            parameter_mappings[curr_node.operation_id] = {"params": {}, "body": {}}

        for edge in curr_node.outgoing_edges:
            if edge.destination.operation_id not in visited:
                self.value_depth_traversal(
                    edge.destination, parameter_mappings, responses, visited
                )

        print("Building value table generation for operation: ", curr_node.operation_id)

        occurrences = {}
        # self.handle_dependent_values(curr_node, dependent_responses, occurrences, parameter_mappings, responses)

        desired_size = 10
        lowest_occurrences = min(occurrences.values()) if occurrences else 0
        if lowest_occurrences < desired_size:
            possible_responses = []
            # Use two samples with two retries each to gather server responses for augmenting value generation
            for _ in range(2):
                response = self.create_and_send_request(
                    curr_node, allow_retry=True, permitted_retries=2
                )
                if response is not None:
                    possible_responses.append(response)

            value_generator = SmartValueGenerator(
                operation_properties=curr_node.operation_properties
            )
            if possible_responses:
                parameters, request_body = (
                    value_generator.generate_informed_value_agent_params(
                        num_values=desired_size - lowest_occurrences,
                        responses=possible_responses,
                    ),
                    value_generator.generate_informed_value_agent_body(
                        num_values=desired_size - lowest_occurrences,
                        responses=possible_responses,
                    ),
                )
            else:
                parameters, request_body = value_generator.generate_value_agent_params(
                    num_values=desired_size - lowest_occurrences
                ), value_generator.generate_value_agent_body(
                    num_values=desired_size - lowest_occurrences
                )
            self._validate_value_mappings(
                curr_node, parameter_mappings, parameters, request_body, occurrences
            )

        print(
            "Completed value table generation for operation: ", curr_node.operation_id
        )

    def generate_value_tables_parallel(
        self,
        operation_nodes: Dict[str, "OperationNode"],
        parameter_mappings: Dict,
        max_workers: int = 4,
        progress_callback: Optional[Callable[[str, int], None]] = None,
    ) -> None:
        """Generate value tables for all operations in parallel using a thread pool."""
        # parameter_mappings corresponds to q_table
        visited_lock = threading.Lock()
        mappings_lock = threading.Lock()
        progress_lock = threading.Lock()
        visited: Set[str] = set()
        completed_count = 0

        def process_operation(operation_node: "OperationNode") -> str:
            nonlocal completed_count
            operation_id = operation_node.operation_id

            # Check if already processed (with lock)
            with visited_lock:
                if operation_id in visited:
                    return operation_id
                visited.add(operation_id)

            # Pre-initialize Q-table entry to ensure it exists even if later steps fail
            with mappings_lock:
                if operation_id not in parameter_mappings:
                    parameter_mappings[operation_id] = {"params": {}, "body": {}}

            # Generate values (I/O bound - HTTP + LLM calls)
            occurrences: Dict[str, int] = {}
            desired_size = 10
            lowest_occurrences = min(occurrences.values()) if occurrences else 0

            if lowest_occurrences < desired_size:
                possible_responses: List[RequestResponse] = []
                for _ in range(2):
                    response = self.create_and_send_request(
                        operation_node, allow_retry=True, permitted_retries=2
                    )
                    if response is not None:
                        possible_responses.append(response)

                value_generator = SmartValueGenerator(
                    operation_properties=operation_node.operation_properties
                )
                if possible_responses:
                    parameters, request_body = (
                        value_generator.generate_informed_value_agent_params(
                            num_values=desired_size - lowest_occurrences,
                            responses=possible_responses,
                        ),
                        value_generator.generate_informed_value_agent_body(
                            num_values=desired_size - lowest_occurrences,
                            responses=possible_responses,
                        ),
                    )
                else:
                    parameters, request_body = (
                        value_generator.generate_value_agent_params(
                            num_values=desired_size - lowest_occurrences
                        ),
                        value_generator.generate_value_agent_body(
                            num_values=desired_size - lowest_occurrences
                        ),
                    )

                # Thread-safe update to parameter_mappings
                with mappings_lock:
                    self._validate_value_mappings(
                        operation_node,
                        parameter_mappings,
                        parameters,
                        request_body,
                        occurrences,
                    )

            return operation_id

        # Submit all operations to thread pool
        with ThreadPoolExecutor(max_workers=max_workers) as executor:
            futures = {
                executor.submit(process_operation, node): op_id
                for op_id, node in operation_nodes.items()
            }

            for future in as_completed(futures):
                op_id = futures[future]
                try:
                    result_op_id = future.result()
                    with progress_lock:
                        completed_count += 1
                        if progress_callback:
                            progress_callback(result_op_id, completed_count)
                except Exception:
                    # Still increment count on error to avoid stuck progress
                    with progress_lock:
                        completed_count += 1
                        if progress_callback:
                            progress_callback(op_id, completed_count)

    def create_and_send_request(
        self,
        curr_node: "OperationNode",
        requirement: RequestRequirements | None = None,
        allow_retry: bool = False,
        permitted_retries: int = 1,
    ) -> Optional[RequestResponse]:
        request_data: RequestData = self.make_request_data(
            curr_node.operation_properties, requirement
        )
        response: Optional[RequestResponse] = self.send_operation_request(
            request_data, allow_retry=allow_retry, permitted_retries=permitted_retries
        )
        return response


if __name__ == "__main__":
    pass