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
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
import json
import hashlib
from typing import Any, Dict, List
from collections import Counter
import argparse
import inspect
import re
from dataclasses import asdict

# 1. Import database classes
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from tools.retail.retail_db import RetailDB
from tools.retail.retail_init import retail_init_data1, retail_init_data2, retail_init_data3, retail_init_data4, retail_init_data5, retail_init_data6, retail_init_data7, retail_init_data8, retail_init_data9, retail_init_data10
from tools.kitchen.kitchen_db import KitchenDB
from tools.kitchen.kitchen_init import kitchen_init_data
from tools.restaurant.restaurant_db import RestaurantDB
from tools.restaurant.restaurant_init import restaurant_init_data, restaurant_init_data5
from tools.restaurant.restaurant6_db import Restaurant6DB
from tools.restaurant.restaurant6_init import restaurant6_init_data
from tools.warehouse.warehouse_db import WarehouseDB
from tools.warehouse import warehouse_init
from tools.household.household_db import HouseholdDB
from tools.household import household_init


SUPPORTED_SCENARIOS = ("retail", "kitchen", "restaurant", "warehouse", "household")
SCENARIO_NUMBER_RANGES = {
    "retail": (1, 10),
    "kitchen": (1, 4),
    "restaurant": (1, 6),
    "warehouse": (1, 25),
    "household": (1, 18),
}
RESULT_FILE_PATTERN = re.compile(r'^([a-z]+)(\d+)_(easy|hard|static)\.json$')
USER_PERFORMANCE_KEYS = (
    "role_consistency",
    "instruction_following",
    "resilience",
    "contextual_robustness",
)


def parse_result_filename(filename):
    """Return ``(scenario, variant, mode)`` for a supported result filename.

    The numeric suffix is always the variant number, so ``restaurant6`` is
    classified as scenario ``restaurant`` with variant ``6``.
    """
    match = RESULT_FILE_PATTERN.fullmatch(filename)
    if not match:
        return None

    scenario = match.group(1)
    scenario_number = int(match.group(2))
    mode = match.group(3)
    if scenario not in SUPPORTED_SCENARIOS:
        return None

    min_number, max_number = SCENARIO_NUMBER_RANGES[scenario]
    if not min_number <= scenario_number <= max_number:
        return None
    return scenario, scenario_number, mode

# ===================== Core Configuration: Fuzzy Match Fields & Scenario Mapping =====================
FUZZY_KEYS = {
    "retail": ["product_name"],
    "kitchen": ["ingredient_name", "recipe_name", "recipes"],
    "restaurant": ["dish_name", "set_meal_name", "restaurant_name"],
    "warehouse": ["equipment"],
    "household": ["item_name", "name"],
}

# Database match method mapping
DB_MATCH_METHOD = {
    "retail": "_find_matching_products",
    "kitchen": None,  # Kitchen uses exact matching, not fuzzy matching
    "restaurant": "_find_matching_dishes",
    "warehouse": None,
    "household": None,
}

# Set meal match method mapping (dish_name may be a set meal name, need to match both dishes and set meals)
DB_SET_MEAL_MATCH_METHOD = {
    "retail": None,
    "kitchen": None,
    "restaurant": "_find_matching_set_meals",
    "warehouse": None,
    "household": None,
}

# Scenario fuzzy match field config (for fuzzy matching within evaluate_interaction.py)
SCENARIO_FUZZY_FIELDS = {
    "retail": ["product_name"],
    "kitchen": ["ingredient_name", "recipe_name"],
    "restaurant": ["dish_name", "set_meal_name", "restaurant_name"],
    "warehouse": ["equipment"],
    "household": ["item_name", "name"],
}

# ===================== Merge Similar Items Configuration =====================
MERGE_NAME_KEYS = [
    "product_name",
    "ingredient_name",
    "equipment",
    "item_name",
    "dish_name",
    "set_meal_name",
    "restaurant_name",
    "recipe_name",
    "name",
]

MERGE_SUM_KEYS = {
    "quantity",
}


# ===================== Numeric Normalization for Hashing =====================
def normalize_for_hash(value):
    """Normalize numeric values so that integer-valued floats (e.g. 12.0) hash identically to ints (12)."""
    if isinstance(value, dict):
        return {k: normalize_for_hash(v) for k, v in value.items()}
    if isinstance(value, list):
        return [normalize_for_hash(v) for v in value]
    if isinstance(value, tuple):
        return [normalize_for_hash(v) for v in value]
    if isinstance(value, bool):
        return value
    if isinstance(value, float):
        return int(value) if value.is_integer() else value
    return value


# ===================== Generic Helpers =====================
def try_parse_number(value):
    try:
        if isinstance(value, bool):
            return None
        if isinstance(value, (int, float)):
            return float(value)
        if isinstance(value, str):
            v = value.strip()
            if v == "":
                return None
            return float(v)
    except Exception:
        return None
    return None


def canonical_string(value: Any) -> str:
    if value is None:
        return ""
    return str(value).strip().lower()


# ===================== Fuzzy Match Function =====================
def fuzzy_match_str(query: str, target: str) -> bool:
    """Generic string fuzzy matching: lowercase containment check"""
    if not query or not target:
        return False
    return query.lower() in target.lower() or target.lower() in query.lower()


# ===================== Scenario-based Fuzzy Matching Core Function =====================
def fuzzy_match_field(gt_name: str, inter_name: str, db_instance: Any, scenario: str) -> bool:
    """
    Perform fuzzy matching based on scenario; dish_name matches both dishes and set meals, either match counts as correct
    """
    # No database instance: fallback to string fuzzy matching
    if not db_instance:
        return fuzzy_match_str(inter_name, gt_name)

    if scenario == "kitchen":
        # Kitchen scenario: use exact matching (KitchenDB has no fuzzy match method)
        return gt_name.lower().strip() == inter_name.lower().strip()

    match_method = DB_MATCH_METHOD.get(scenario)
    set_meal_method = DB_SET_MEAL_MATCH_METHOD.get(scenario)

    def _collect_matching_names(name: str) -> set:
        """Collect all matching names for dishes and set meals"""
        all_names = set()

        # Dish matching
        if match_method and hasattr(db_instance, match_method):
            try:
                match_func = getattr(db_instance, match_method)
                if isinstance(db_instance, Restaurant6DB):
                    # Restaurant6DB needs to iterate over all restaurants.
                    for r_name in db_instance.restaurants:
                        matches = match_func(r_name, name)
                        all_names.update(m.name for m in matches)
                else:
                    matches = match_func(name)
                    all_names.update(m.name for m in matches)
            except Exception:
                pass

        # Set meal matching
        if set_meal_method and hasattr(db_instance, set_meal_method):
            try:
                sm_func = getattr(db_instance, set_meal_method)
                if isinstance(db_instance, Restaurant6DB):
                    for r_name in db_instance.restaurants:
                        matches = sm_func(r_name, name)
                        all_names.update(m.name for m in matches)
                else:
                    matches = sm_func(name)
                    all_names.update(m.name for m in matches)
            except Exception:
                pass

        return all_names

    gt_names = _collect_matching_names(gt_name)
    inter_names = _collect_matching_names(inter_name)

    # Match succeeds if dishes or set meals have intersection
    if len(gt_names) > 0 and len(inter_names) > 0:
        return len(gt_names & inter_names) > 0

    # Database methods all returned empty lists, fallback to string fuzzy matching
    return fuzzy_match_str(inter_name, gt_name)


# ===================== Merge Similar Items Before Matching =====================
def get_merge_identity_key(item: dict):
    """
    Return the first available name-like key for grouping similar dict items.
    """
    for key in MERGE_NAME_KEYS:
        if key in item and item[key] is not None:
            return key
    return None


def merge_two_dict_items(base: dict, incoming: dict) -> dict:
    """
    Merge two similar dict items:
    - sum numeric quantity-like fields in MERGE_SUM_KEYS
    - for other fields, keep existing non-empty value; otherwise fill from incoming
    """
    merged = dict(base)

    for k, v in incoming.items():
        if k in MERGE_SUM_KEYS:
            a = try_parse_number(merged.get(k))
            b = try_parse_number(v)
            if a is not None and b is not None:
                summed = a + b
                merged[k] = int(summed) if summed.is_integer() else summed
            elif merged.get(k) is None:
                merged[k] = v
        else:
            existing = merged.get(k)
            if existing is None or (isinstance(existing, str) and existing.strip() == ""):
                merged[k] = v

    return merged


def merge_similar_items_in_list(items, db_instance=None, scenario="retail", current_key=None):
    """
    Merge similar dict items in a list before matching.
    Typical use case:
    [
        {"dish_name": "apple", "quantity": 1},
        {"dish_name": "apple", "quantity": 2}
    ]
    =>
    [
        {"dish_name": "apple", "quantity": 3}
    ]

    注意:
    为保证 list 参数评估时是"无序精准匹配",这里的"similar"也只按严格相等合并,
    不再对 name 类字段使用模糊匹配合并。
    """
    if not isinstance(items, list):
        return items

    normalized_items = [
        merge_similar_items_in_value(item, db_instance=db_instance, scenario=scenario, current_key=current_key)
        for item in items
    ]

    if not normalized_items:
        return normalized_items

    if not all(isinstance(x, dict) for x in normalized_items):
        return normalized_items

    merged_groups = []

    for item in normalized_items:
        name_key = get_merge_identity_key(item)
        if not name_key:
            merged_groups.append(item)
            continue

        current_name = item.get(name_key)
        merged = False

        for idx, existing in enumerate(merged_groups):
            if not isinstance(existing, dict):
                continue
            existing_name_key = get_merge_identity_key(existing)
            if existing_name_key != name_key:
                continue

            existing_name = existing.get(existing_name_key)
            if isinstance(current_name, str) and isinstance(existing_name, str):
                same = canonical_string(existing_name) == canonical_string(current_name)

                if same:
                    merged_groups[idx] = merge_two_dict_items(existing, item)
                    merged = True
                    break
            else:
                if current_name == existing_name:
                    merged_groups[idx] = merge_two_dict_items(existing, item)
                    merged = True
                    break

        if not merged:
            merged_groups.append(item)

    def sort_key(x):
        if isinstance(x, dict):
            name_key = get_merge_identity_key(x)
            if name_key:
                return canonical_string(x.get(name_key))
        return json.dumps(normalize_for_hash(x), ensure_ascii=False, sort_keys=True, default=str)

    return sorted(merged_groups, key=sort_key)


def merge_similar_items_in_value(value, db_instance=None, scenario="retail", current_key=None):
    """
    Recursively merge similar items in complex parameter values.
    """
    if isinstance(value, dict):
        return {
            k: merge_similar_items_in_value(v, db_instance=db_instance, scenario=scenario, current_key=k)
            for k, v in value.items()
        }
    if isinstance(value, list):
        return merge_similar_items_in_list(value, db_instance=db_instance, scenario=scenario, current_key=current_key)
    if isinstance(value, tuple):
        return [
            merge_similar_items_in_value(v, db_instance=db_instance, scenario=scenario, current_key=current_key)
            for v in value
        ]
    return value


def normalize_call_parameters_before_match(params, db_instance=None, scenario="retail"):
    """
    Normalize parameters before comparison:
    1. recursively process nested values
    2. merge similar items in lists
    """
    if not isinstance(params, dict):
        return params
    return merge_similar_items_in_value(params, db_instance=db_instance, scenario=scenario)


# ===================== Strict / Exact Comparison Helpers =====================
def compare_parameters_recursive_exact(
    gt_val: Any,
    inter_val: Any,
    db_instance: Any = None,
    scenario: str = "retail",
    current_key: str = None
) -> bool:
    """
    Exact recursive comparison.
    For list:
    - unordered exact matching only
    For fuzzy keys:
    - exact means normalized string equality, NOT fuzzy db matching
    """
    if type(gt_val) != type(inter_val):
        try:
            if isinstance(gt_val, (str, int, float)) and isinstance(inter_val, (str, int, float)):
                return float(gt_val) == float(inter_val)
        except (ValueError, TypeError):
            pass
        return False

    if isinstance(gt_val, list):
        gt_val = merge_similar_items_in_list(gt_val, db_instance, scenario, current_key)
        inter_val = merge_similar_items_in_list(inter_val, db_instance, scenario, current_key)

        if len(gt_val) != len(inter_val):
            return False

        inter_used = [False] * len(inter_val)
        for gt_item in gt_val:
            found = False
            for j, inter_item in enumerate(inter_val):
                if inter_used[j]:
                    continue
                if compare_parameters_recursive_exact(gt_item, inter_item, db_instance, scenario, current_key):
                    inter_used[j] = True
                    found = True
                    break
            if not found:
                return False
        return True

    if isinstance(gt_val, dict):
        gt_val = merge_similar_items_in_value(gt_val, db_instance, scenario, current_key)
        inter_val = merge_similar_items_in_value(inter_val, db_instance, scenario, current_key)

        if set(gt_val.keys()) != set(inter_val.keys()):
            return False

        for key, g_val in gt_val.items():
            i_val = inter_val[key]
            if not compare_parameters_recursive_exact(g_val, i_val, db_instance, scenario, key):
                return False
        return True

    if isinstance(gt_val, str):
        if current_key in FUZZY_KEYS.get(scenario, []):
            return canonical_string(gt_val) == canonical_string(inter_val)
        return gt_val == inter_val

    return gt_val == inter_val


# ===================== Recursive Parameter Comparison =====================
def compare_parameters_recursive(
    gt_val: Any,
    inter_val: Any,
    db_instance: Any = None,
    scenario: str = "retail",
    current_key: str = None
) -> bool:
    """
    Recursively compare two parameter values, supporting:
    1. Array/list: unordered comparison, ignoring order
    2. Dict/object: recursive field-by-field comparison
    3. Specified fields: scenario-based fuzzy matching
    4. Basic types: exact comparison

    关键修改:
    当参数是 list 时,严格按照"无序精准匹配"进行评估,
    不再使用任何模糊匹配。
    """
    # When types differ, try numeric-compatible comparison (str/int/float mix, e.g. "2" vs 2.0)
    if type(gt_val) != type(inter_val):
        try:
            if isinstance(gt_val, (str, int, float)) and isinstance(inter_val, (str, int, float)):
                return float(gt_val) == float(inter_val)
        except (ValueError, TypeError):
            pass
        return False

    # 1. Handle array/list type: unordered exact matching
    if isinstance(gt_val, list):
        return compare_parameters_recursive_exact(gt_val, inter_val, db_instance, scenario, current_key)

    # 2. Handle dict/object type
    if isinstance(gt_val, dict):
        gt_val = merge_similar_items_in_value(gt_val, db_instance, scenario, current_key)
        inter_val = merge_similar_items_in_value(inter_val, db_instance, scenario, current_key)

        if set(gt_val.keys()) != set(inter_val.keys()):
            return False

        for key, g_val in gt_val.items():
            i_val = inter_val[key]
            # Check if current field needs fuzzy matching
            if key in FUZZY_KEYS.get(scenario, []):
                # If value is string, use fuzzy matching directly
                if isinstance(g_val, str):
                    if not fuzzy_match_field(g_val, i_val, db_instance, scenario):
                        return False
                else:
                    # If value is list or other type, continue recursive comparison (keep current key for subsequent string comparison)
                    if not compare_parameters_recursive(g_val, i_val, db_instance, scenario, key):
                        return False
            else:
                if not compare_parameters_recursive(g_val, i_val, db_instance, scenario, key):
                    return False
        return True

    # 3. Basic types: special string handling
    if isinstance(gt_val, str):
        # If current field is in FUZZY_KEYS, use case-insensitive comparison
        if current_key in FUZZY_KEYS.get(scenario, []):
            return gt_val.lower().strip() == inter_val.lower().strip()
        return gt_val == inter_val

    # 4. Other basic types: exact comparison directly
    return gt_val == inter_val


# ===================== Parameter Comparison Wrapper =====================
def compare_parameters_with_fuzzy_match(
    gt_params: Dict[str, Any],
    interaction_params: Dict[str, Any],
    db_instance: Any = None,
    scenario: str = "retail"
) -> bool:
    """Upper-level wrapper: compatible with original function parameters, added scenario parameter"""
    gt_params = normalize_call_parameters_before_match(gt_params, db_instance, scenario)
    interaction_params = normalize_call_parameters_before_match(interaction_params, db_instance, scenario)
    return compare_parameters_recursive(gt_params, interaction_params, db_instance, scenario)


# ===================== Database Hash Calculation =====================
def calculate_db_hash(db_instance):
    """Calculate database state hash, supporting all scenario databases"""
    db_data = {}
    if isinstance(db_instance, KitchenDB):
        db_data = {
            'ingredients': {k: {'name': v.name, 'quantity': v.quantity, 'category': v.category,
                               'storage_location': v.storage_location, 'expiry_date': v.expiry_date,
                               'nutrition': vars(v.nutrition) if v.nutrition else None}
                         for k, v in db_instance.ingredients.items()},
            'recipes': {k: {'name': v.name, 'ingredients': [{'ingredient_name': i.ingredient_name, 'quantity': i.quantity} for i in v.ingredients],
                           'allergens': v.allergens, 'taste': v.taste, 'nutritional_characteristics': v.nutritional_characteristics}
                       for k, v in db_instance.recipes.items()},
            'user_menus': db_instance.user_menus,
            'user_shopping_lists': {k: sorted([{'ingredient_name': item.ingredient_name, 'quantity': item.quantity} for item in v.values()],
                                              key=lambda x: x['ingredient_name'])
                                   for k, v in db_instance.user_shopping_lists.items()}
        }
    elif isinstance(db_instance, RetailDB):
        db_data = {
            'catalog': {k: {'name': v.name, 'category': v.category, 'price': v.price, 'tax_rate': v.tax_rate,
                           'discount': v.discount, 'nutritional_characteristics': v.nutritional_characteristics,
                           'taste': v.taste, 'country_of_origin': v.country_of_origin,
                           'nutrition': vars(v.nutrition) if v.nutrition else None}
                     for k, v in db_instance.catalog.items()},
            'user_carts': {k: sorted([{'product_name': item.product_name, 'quantity': item.quantity,
                                       'category': item.category, 'price': item.price, 'tax_rate': item.tax_rate,
                                       'discount': item.discount} for item in v.values()],
                                      key=lambda x: x['product_name'])
                          for k, v in db_instance.user_carts.items()},
            'user_shopping_lists': db_instance.user_shopping_lists
        }
    elif isinstance(db_instance, RestaurantDB):
        db_data = {
            'catalog': {k: {'name': v.name, 'category': v.category, 'price': v.price, 'tax_rate': v.tax_rate,
                           'discount': v.discount, 'nutritional_characteristics': v.nutritional_characteristics,
                           'taste': v.taste, 'allergens': v.allergens,
                           'nutrition': vars(v.nutrition) if v.nutrition else None}
                     for k, v in db_instance.catalog.items()},
            'set_meals': {k: {'name': v.name, 'included_dishes': v.included_dishes,
                             'set_meal_price': v.set_meal_price, 'set_meal_discount': v.set_meal_discount}
                         for k, v in db_instance.set_meals.items()},
            'user_orders': {k: sorted([{'dish_name': item.dish_name, 'quantity': item.quantity} for item in v.values()],
                                       key=lambda x: x['dish_name'])
                           for k, v in db_instance.user_orders.items()}
        }
    elif isinstance(db_instance, Restaurant6DB):
        db_data = {
            'restaurants': {}
        }
        for r_name, store in db_instance.restaurants.items():
            db_data['restaurants'][r_name] = {
                'catalog': {k: {'name': v.name, 'category': v.category, 'price': v.price, 'tax_rate': v.tax_rate,
                               'discount': v.discount, 'nutritional_characteristics': v.nutritional_characteristics,
                               'taste': v.taste, 'allergens': v.allergens,
                               'nutrition': vars(v.nutrition) if v.nutrition else None}
                         for k, v in store['catalog'].items()},
                'set_meals': {k: {'name': v.name, 'included_dishes': v.included_dishes,
                                 'set_meal_price': v.set_meal_price, 'set_meal_discount': v.set_meal_discount}
                             for k, v in store['set_meals'].items()},
                'user_orders': {k: sorted([{'dish_name': item.dish_name, 'quantity': item.quantity} for item in v.values()],
                                           key=lambda x: x['dish_name'])
                               for k, v in store['user_orders'].items()}
            }
    elif isinstance(db_instance, WarehouseDB):
        db_data = {
            'equipment': {name: asdict(item) for name, item in db_instance.equipment.items()},
            'user_action_lists': {
                user_id: sorted(
                    (asdict(action) for action in actions),
                    key=lambda action: (
                        action['equipment'], action['required_action'], action['due_date']
                    ),
                )
                for user_id, actions in db_instance.user_action_lists.items()
            },
        }
    elif isinstance(db_instance, HouseholdDB):
        db_data = {
            'items': {name: asdict(item) for name, item in db_instance.items.items()},
            'user_action_lists': {
                user_id: sorted(
                    (asdict(action) for action in actions),
                    key=lambda action: (
                        action['name'], action['required_action'], action['due_date']
                    ),
                )
                for user_id, actions in db_instance.user_action_lists.items()
            },
        }
    elif hasattr(db_instance, 'get_all_data'):
        db_data = db_instance.get_all_data()
    else:
        # Generic extraction
        for attr in dir(db_instance):
            if not attr.startswith('_') and not callable(getattr(db_instance, attr)):
                attr_value = getattr(db_instance, attr)
                try:
                    json.dumps(attr_value, sort_keys=True, default=str)
                    db_data[attr] = attr_value
                except Exception:
                    continue

    json_str = json.dumps(normalize_for_hash(db_data), sort_keys=True, ensure_ascii=False, default=str)
    return hashlib.sha256(json_str.encode('utf-8')).hexdigest()


# ===================== Ground Truth Tool Call Simplification =====================
def simplify_tool_calls(db_instance, tool_calls, scenario="retail"):
    """
    Simplify ground truth tool calls, keeping only parameters needed by database methods.
    Also normalize parameters by merging similar items before matching/execution.
    """
    simplified_calls = []
    for tool_call in tool_calls:
        try:
            method_name = tool_call.get("tool_name") or tool_call.get("name")
            params = tool_call.get("parameters", {})

            if hasattr(db_instance, method_name):
                method = getattr(db_instance, method_name)
                sig = inspect.signature(method)

                # Keep only parameters accepted by the method signature
                valid_params = {
                    k: v for k, v in params.items()
                    if k in sig.parameters
                }
                valid_params = normalize_call_parameters_before_match(valid_params, db_instance, scenario)

                simplified_calls.append({
                    "tool_name": method_name,
                    "parameters": valid_params
                })
            else:
                # If method does not exist, keep original call (for subsequent error statistics)
                tool_call_copy = dict(tool_call)
                if "parameters" in tool_call_copy:
                    tool_call_copy["parameters"] = normalize_call_parameters_before_match(
                        tool_call_copy.get("parameters", {}), db_instance, scenario
                    )
                simplified_calls.append(tool_call_copy)
        except Exception:
            # Keep original call on error
            simplified_calls.append(tool_call)

    return simplified_calls


# ===================== Tool Execution Function =====================
def execute_tool_chain(db_instance, tool_calls, scenario="retail"):
    """Execute tool call chain (generic adapter) - with parameter filtering and pre-merge normalization."""
    results = []
    for tool_call in tool_calls:
        try:
            method_name = tool_call.get("tool_name") or tool_call.get("name")
            params = tool_call.get("parameters", {})

            if hasattr(db_instance, method_name):
                method = getattr(db_instance, method_name)

                # Filter parameters, keeping only those in the method signature
                sig = inspect.signature(method)
                valid_params = {
                    k: v for k, v in params.items()
                    if k in sig.parameters
                }
                valid_params = normalize_call_parameters_before_match(valid_params, db_instance, scenario)

                result = method(**valid_params)
                results.append({
                    "tool_name": method_name,
                    "parameters": valid_params,
                    "result": result,
                    "status": "success"
                })
            else:
                results.append({
                    "tool_name": method_name,
                    "parameters": params,
                    "result": f"Tool '{method_name}' not found",
                    "status": "error"
                })
        except Exception as e:
            results.append({
                "tool_name": tool_call.get("tool_name") or tool_call.get("name"),
                "parameters": tool_call.get("parameters", {}),
                "result": str(e),
                "status": "error"
            })
    return results


# ===================== Tool Call Comparison =====================
def compare_tool_calls(ground_truth_calls, interaction_calls, db_instance=None, scenario="retail"):
    """
    Compare tool calls:
    1. Tool name exact match
    2. Parameters: unordered arrays + scenario-based fuzzy matching
    3. Overall call sequence unordered comparison
    4. Parameter filtering: only compare parameters needed by database methods
    """
    def extract_call_info(call):
        if isinstance(call, dict):
            return {
                "tool_name": call.get("tool_name") or call.get("name"),  # Support both field names
                "parameters": call.get("parameters", {})
            }
        return call

    def filter_params_by_method(tool_name, params, db):
        """Filter parameters based on database method signature"""
        if db and hasattr(db, tool_name):
            try:
                method = getattr(db, tool_name)
                sig = inspect.signature(method)
                params = {k: v for k, v in params.items() if k in sig.parameters}
            except Exception:
                pass
        return normalize_call_parameters_before_match(params, db, scenario)

    try:
        # Ground truth calls: normalize parameters
        gt_calls = [extract_call_info(call) for call in ground_truth_calls]
        for call in gt_calls:
            call["parameters"] = normalize_call_parameters_before_match(
                call.get("parameters", {}), db_instance, scenario
            )

        # Extract model calls and filter parameters
        # New format: {"turn": 0, "calls": [...], "results": [...]}
        # Old format: {"turn": 0, "call": {...}, "result": "..."}
        interaction_only_calls = []
        for entry in interaction_calls:
            if isinstance(entry, dict):
                # New format: use "calls" list
                if "calls" in entry and isinstance(entry["calls"], list):
                    for call in entry["calls"]:
                        call_info = extract_call_info(call)
                        call_info["parameters"] = filter_params_by_method(
                            call_info["tool_name"],
                            call_info["parameters"],
                            db_instance
                        )
                        interaction_only_calls.append(call_info)
                # Old format: use "call" single object (backward compatible)
                elif "call" in entry:
                    call_info = extract_call_info(entry["call"])
                    call_info["parameters"] = filter_params_by_method(
                        call_info["tool_name"],
                        call_info["parameters"],
                        db_instance
                    )
                    interaction_only_calls.append(call_info)

        matches = 0
        matched_interaction_indices = set()

        for gt_call in gt_calls:
            for idx, interaction_call in enumerate(interaction_only_calls):
                if idx in matched_interaction_indices:
                    continue

                if gt_call.get("tool_name") == interaction_call.get("tool_name"):
                    if compare_parameters_with_fuzzy_match(
                        gt_call.get("parameters", {}),
                        interaction_call.get("parameters", {}),
                        db_instance,
                        scenario
                    ):
                        matches += 1
                        matched_interaction_indices.add(idx)
                        break

        return matches, len(gt_calls), len(interaction_only_calls)
    except Exception:
        return 0, 0, 0


# ===================== Database Initialization =====================
def get_init_db(scenario, scenario_number):
    import io
    import sys

    # Temporarily redirect print output to avoid database initialization messages
    old_stdout = sys.stdout
    sys.stdout = io.StringIO()

    try:
        db = None
        if scenario == "retail":
            db = RetailDB()
            init_data = [
                retail_init_data1, retail_init_data2, retail_init_data3, retail_init_data4,
                retail_init_data5, retail_init_data6, retail_init_data7, retail_init_data8,
                retail_init_data9, retail_init_data10
            ]
            if 1 <= scenario_number <= 10:
                db.init_from_json(init_data[scenario_number-1])
        elif scenario == "kitchen":
            db = KitchenDB()
            db.init_from_json(kitchen_init_data)
        elif scenario == "restaurant":
            if scenario_number == 6:
                db = Restaurant6DB()
                db.init_from_json(restaurant6_init_data)
            else:
                db = RestaurantDB()
            if 1 <= scenario_number <= 4:
                db.init_from_json(restaurant_init_data)
            elif scenario_number == 5:
                db.init_from_json(restaurant_init_data5)
        elif scenario == "warehouse":
            db = WarehouseDB()
            init_data = getattr(warehouse_init, f"warehouse_init_data{scenario_number}", None)
            if init_data is None:
                raise ValueError(f"warehouse initialization data {scenario_number} not found")
            db.init_from_json(init_data)
        elif scenario == "household":
            db = HouseholdDB()
            init_data = getattr(household_init, f"household_init_data{scenario_number}", None)
            if init_data is None:
                raise ValueError(f"household initialization data {scenario_number} not found")
            db.init_from_json(init_data)
        else:
            raise ValueError(f"unsupported scenario: {scenario}")
        return db
    finally:
        sys.stdout = old_stdout


# ===================== Main Evaluation Function =====================
def evaluate_interaction_success(ground_truth_file, interaction_log_file, scenario="kitchen", args=None, silent=False, num_samples=0):
    """
    Evaluate interaction success rate

    Args:
        ground_truth_file: Ground truth file path
        interaction_log_file: Interaction log file path
        scenario: Scenario type (kitchen, retail, restaurant, warehouse, household)
        args: Argument object containing scenario_number
        silent: Whether to run in silent mode
        num_samples: Number of samples per scenario to test, 0 means test all samples
    """
    with open(ground_truth_file, 'r', encoding='utf-8') as f:
        ground_truth_data = json.load(f)
    with open(interaction_log_file, 'r', encoding='utf-8') as f:
        interaction_data = json.load(f)

    # Limit sample count based on num_samples
    if num_samples > 0:
        ground_truth_data = ground_truth_data[:num_samples]
        interaction_data = interaction_data[:num_samples]

    results = {
        "total_scenarios": len(ground_truth_data),
        "valid_scenarios": 0,
        "invalid_scenarios": [],
        "tool_based": {"success_count": 0, "partial_matches": [], "success_rate": 0.0},
        "result_based": {"success_count": 0, "success_rate": 0.0},
        "joint_success": {"success_count": 0, "success_rate": 0.0},  # Joint success rate
        "detailed_results": [],
        "micro_tool_stats": {
            "total_correct_calls": 0, "total_ground_truth_calls": 0,
            "total_interaction_calls": 0, "micro_accuracy": 0.0,
            "task_count": 0  # Actual number of evaluated tasks (denominator)
        },
        "performance_metrics": {
            "avg_user_response_time": 0.0,
            "avg_agent_response_time": 0.0,
            "avg_tokens_consumed": 0.0,
            "avg_rounds_count": 0.0,
            "avg_input_tokens": 0.0,
            "avg_output_tokens": 0.0,
            "avg_tool_calls_count": 0.0,
            "avg_user_performance": {key: 0.0 for key in USER_PERFORMANCE_KEYS},
            "user_performance_sample_counts": {key: 0 for key in USER_PERFORMANCE_KEYS},
        },
    }

    total_correct_calls = total_gt_calls = total_interaction_calls = 0
    total_user_response_time = 0.0
    total_agent_response_time = 0.0
    total_tokens_consumed = 0
    total_rounds_count = 0
    total_input_tokens = 0
    total_output_tokens = 0
    total_tool_calls_count = 0
    total_user_performance = {key: 0.0 for key in USER_PERFORMANCE_KEYS}
    user_performance_counts = {key: 0 for key in USER_PERFORMANCE_KEYS}
    valid_interaction_count = 0

    # Match interaction results to ground truth by scenario_id (not by sequential
    # index). Reruns write back only the tasks they executed, so the interaction
    # file may contain a non-contiguous, sparse subset of scenario_ids (e.g. only
    # the reran tasks), which index-aligned matching would mis-pair. Falling back
    # to positional scenario_id when an entry lacks the field keeps behavior
    # identical for fully-run files (where ids are 1..N and position-stable).
    interaction_by_id = {}
    for _i, _entry in enumerate(interaction_data):
        if isinstance(_entry, dict):
            _sid = _entry.get("scenario_id", _i + 1)
        else:
            _sid = _i + 1
        interaction_by_id[_sid] = _entry

    for _gt_idx, gt_scenario in enumerate(ground_truth_data):
        scenario_id = gt_scenario.get("scenario_id", _gt_idx + 1) if isinstance(gt_scenario, dict) else _gt_idx + 1
        interaction_scenario = interaction_by_id.get(scenario_id)
        if interaction_scenario is None:
            # This ground-truth task was not (re)run, so no interaction result exists.
            if not silent:
                print(f"Warning: interaction log missing scenario {scenario_id} data, skipped")
            results["invalid_scenarios"].append({
                "scenario_id": scenario_id,
                "reason": "Missing interaction data"
            })
            continue

        # Data format validation
        gt_tool_calls_raw = gt_scenario.get("ground_truth", [])
        interaction_tool_calls = interaction_scenario.get("tool_calls", [])
        if not isinstance(gt_tool_calls_raw, list) or not isinstance(interaction_tool_calls, list):
            results["invalid_scenarios"].append({
                "scenario_id": scenario_id,
                "reason": "Data format error (not a list)"
            })
            continue

        # Validation passed: include in valid evaluation
        valid_interaction_count += 1
        results["valid_scenarios"] += 1

        # Performance metrics statistics
        total_tokens_consumed += interaction_scenario.get("tokens_consumed", 0)
        total_user_response_time += interaction_scenario.get("user_response_time_seconds", 0.0)
        total_agent_response_time += interaction_scenario.get("agent_response_time_seconds", 0.0)
        total_rounds_count += interaction_scenario.get("rounds_count", 0)
        total_input_tokens += interaction_scenario.get("input_tokens", 0)
        total_output_tokens += interaction_scenario.get("output_tokens", 0)
        total_tool_calls_count += interaction_scenario.get("tool_calls_count", 0)

        user_perf = interaction_scenario.get("user_performance", {})
        if user_perf:
            for key in USER_PERFORMANCE_KEYS:
                value = user_perf.get(f"original_{key}_avg")
                if value is None:
                    value = user_perf.get(key)
                if isinstance(value, (int, float)) and not isinstance(value, bool):
                    total_user_performance[key] += float(value)
                    user_performance_counts[key] += 1

        detailed_result = {
            "scenario_id": scenario_id,
            "tool_based": {"success": False, "matches": 0, "total_gt_calls": 0, "total_interaction_calls": 0},
            "result_based": {"success": False, "gt_hash": None, "interaction_hash": None},
            "joint_success": False  # Joint success flag
        }

        # Get database instance for ground truth simplification and comparison
        db_instance_for_matching = get_init_db(scenario, args.scenario_number)

        # Simplify ground truth tool calls: keep only parameters needed by database methods
        gt_tool_calls = simplify_tool_calls(db_instance_for_matching, gt_tool_calls_raw, scenario=scenario)

        # Tool call evaluation (using simplified ground truth)
        matches, total_gt, total_interactions = compare_tool_calls(
            gt_tool_calls, interaction_tool_calls, db_instance_for_matching, scenario
        )
        detailed_result["tool_based"].update({"matches": matches, "total_gt_calls": total_gt, "total_interaction_calls": total_interactions})

        total_correct_calls += matches
        total_gt_calls += total_gt
        total_interaction_calls += total_interactions

        tool_success = False
        if matches == total_gt and total_gt > 0:
            results["tool_based"]["success_count"] += 1
            detailed_result["tool_based"]["success"] = True
            tool_success = True
        elif matches > 0:
            results["tool_based"]["partial_matches"].append({"scenario_id": scenario_id, "matches": matches, "total": total_gt})

        # Final result evaluation
        result_success = False
        try:
            gt_db = get_init_db(scenario, args.scenario_number)
            gt_tool_calls = execute_tool_chain(gt_db, gt_tool_calls, scenario=scenario)
            gt_hash = calculate_db_hash(gt_db)

            interaction_db = get_init_db(scenario, args.scenario_number)
            # New format: {"turn": 0, "calls": [...], "results": [...]}
            # Old format: {"turn": 0, "call": {...}, "result": "..."}
            interaction_only_calls = []
            for entry in interaction_tool_calls:
                if isinstance(entry, dict):
                    if "calls" in entry and isinstance(entry["calls"], list):
                        # New format: expand all calls
                        interaction_only_calls.extend(entry["calls"])
                    elif "call" in entry:
                        # Old format: backward compatible
                        interaction_only_calls.append(entry["call"])
            interaction_only_calls = execute_tool_chain(interaction_db, interaction_only_calls, scenario=scenario)
            interaction_hash = calculate_db_hash(interaction_db)

            detailed_result["result_based"].update({"gt_hash": gt_hash, "interaction_hash": interaction_hash})
            if gt_hash == interaction_hash:
                results["result_based"]["success_count"] += 1
                detailed_result["result_based"]["success"] = True
                result_success = True
        except Exception as e:
            if not silent:
                print(f"Scenario {scenario_id} database operation failed, skipped result evaluation: {e}")

        # Joint success: only when both process-based and result-based evaluations succeed
        if tool_success and result_success:
            results["joint_success"]["success_count"] += 1
            detailed_result["joint_success"] = True

        results["detailed_results"].append(detailed_result)

    # Calculate rates
    if results["valid_scenarios"] > 0:
        results["tool_based"]["success_rate"] = results["tool_based"]["success_count"] / results["valid_scenarios"]
        results["result_based"]["success_rate"] = results["result_based"]["success_count"] / results["valid_scenarios"]
        results["joint_success"]["success_rate"] = results["joint_success"]["success_count"] / results["valid_scenarios"]

    # Micro tool call success rate: use actual evaluated task count as denominator
    task_count = len(results["detailed_results"])  # Actual number of evaluated tasks
    results["micro_tool_stats"].update({
        "total_correct_calls": total_correct_calls,
        "total_ground_truth_calls": total_gt_calls,
        "total_interaction_calls": total_interaction_calls,
        "task_count": task_count,  # Actual number of evaluated tasks
        "micro_accuracy": total_correct_calls / total_gt_calls if total_gt_calls > 0 else 0.0,
        # Per-task tool call accuracy (average)
        "avg_task_accuracy": sum(d["tool_based"]["matches"] / d["tool_based"]["total_gt_calls"]
                                  for d in results["detailed_results"]
                                  if d["tool_based"]["total_gt_calls"] > 0) / task_count if task_count > 0 else 0.0
    })

    if valid_interaction_count > 0:
        results["performance_metrics"]["avg_user_response_time"] = total_user_response_time / valid_interaction_count
        results["performance_metrics"]["avg_agent_response_time"] = total_agent_response_time / valid_interaction_count
        results["performance_metrics"]["avg_tokens_consumed"] = total_tokens_consumed / valid_interaction_count
        results["performance_metrics"]["avg_rounds_count"] = total_rounds_count / valid_interaction_count
        results["performance_metrics"]["avg_input_tokens"] = total_input_tokens / valid_interaction_count
        results["performance_metrics"]["avg_output_tokens"] = total_output_tokens / valid_interaction_count
        results["performance_metrics"]["avg_tool_calls_count"] = total_tool_calls_count / valid_interaction_count
    for key in USER_PERFORMANCE_KEYS:
        count = user_performance_counts[key]
        results["performance_metrics"]["user_performance_sample_counts"][key] = count
        if count > 0:
            results["performance_metrics"]["avg_user_performance"][key] = (
                total_user_performance[key] / count
            )

    # A result file is complete only when every selected ground-truth task has a
    # valid interaction entry. Aggregate accuracy must not include partial files.
    results["is_complete"] = (
        results["total_scenarios"] > 0
        and results["valid_scenarios"] == results["total_scenarios"]
        and not results["invalid_scenarios"]
    )

    return results


# ===================== Report Printing =====================
def print_evaluation_report(results):
    print("=" * 60)
    print("Interaction Success Rate Evaluation Report")
    print("=" * 60)
    print(f"Total scenarios: {results['total_scenarios']}")
    print(f"Valid evaluation scenarios: {results['valid_scenarios']}")
    print(f"Invalid skipped scenarios: {len(results['invalid_scenarios'])}")
    if results["invalid_scenarios"]:
        print("Skipped invalid samples:")
        for item in results["invalid_scenarios"]:
            print(f"  - Scenario {item['scenario_id']}: {item['reason']}")
    print()

    print("1. Tool-based evaluation:")
    print(f"   Successful scenarios: {results['tool_based']['success_count']}")
    print(f"   Success rate: {results['tool_based']['success_rate']:.2%}")
    if results['tool_based']['partial_matches']:
        print("   Partially matched scenarios:")
        for m in results['tool_based']['partial_matches']:
            print(f"     Scenario {m['scenario_id']}: {m['matches']}/{m['total']}")
    print()

    print("2. Result-based evaluation:")
    print(f"   Successful scenarios: {results['result_based']['success_count']}")
    print(f"   Success rate: {results['result_based']['success_rate']:.2%}\n")

    print("3. Joint success rate (process + result dual evaluation):")
    print(f"   Successful scenarios: {results['joint_success']['success_count']}")
    print(f"   Success rate: {results['joint_success']['success_rate']:.2%}\n")

    print("4. Per-task tool call detailed statistics:")
    for d in results['detailed_results']:
        print(f"   Scenario {d['scenario_id']}: correct {d['tool_based']['matches']}/{d['tool_based']['total_gt_calls']}")

    print("\n5. Micro tool call success rate:")
    print(f"   Evaluated tasks: {results['micro_tool_stats']['task_count']}")
    print(f"   Total correct calls: {results['micro_tool_stats']['total_correct_calls']}")
    print(f"   Total ground truth calls: {results['micro_tool_stats']['total_ground_truth_calls']}")
    print(f"   Overall accuracy: {results['micro_tool_stats']['micro_accuracy']:.2%}")
    print(f"   Average task accuracy: {results['micro_tool_stats']['avg_task_accuracy']:.2%}")

    print("\n6. Overall performance metrics:")
    perf = results.get('performance_metrics', {})
    print(f"   Avg user response time: {perf.get('avg_user_response_time', 0):.2f} seconds")
    print(f"   Avg agent response time: {perf.get('avg_agent_response_time', 0):.2f} seconds")
    print(f"   Avg tokens consumed: {perf.get('avg_tokens_consumed', 0):.0f}")
    print(f"   Avg conversation rounds: {perf.get('avg_rounds_count', 0):.2f}")
    print(f"   Avg input tokens: {perf.get('avg_input_tokens', 0):.0f}")
    print(f"   Avg output tokens: {perf.get('avg_output_tokens', 0):.0f}")
    print(f"   Avg tool calls: {perf.get('avg_tool_calls_count', 0):.2f}")
    print("   Simulated-user metric averages:")
    user_perf_avg = perf.get("avg_user_performance", {})
    user_perf_counts = perf.get("user_performance_sample_counts", {})
    for key in USER_PERFORMANCE_KEYS:
        print(
            f"     {key}: {user_perf_avg.get(key, 0):.2f} "
            f"(n={user_perf_counts.get(key, 0)})"
        )


# ===================== Main Function =====================
def main():
    import os

    parser = argparse.ArgumentParser(description="evaluation script")
    parser.add_argument("--model_name", type=str, default="gemini-3.1-pro-preview", help="Model name (subdirectory under results folder)")
    parser.add_argument("--num_samples", type=int, default=0, help="Number of samples per scenario to test, 0 means test all samples")
    parser.add_argument(
        "--include_partial",
        action="store_true",
        help=(
            "Include every valid trajectory from incomplete result files in "
            "aggregate metrics. Per-file completeness is still reported."
        ),
    )
    # Configurable I/O roots. Defaults preserve the original behaviour
    # (reading ../results/{model} and writing ../eval_result/{model}).
    # Pass --results_root ../GPT_user_results --output_root ../GPT_user_eval_result
    # to evaluate the GPT-user run instead.
    parser.add_argument("--results_root", type=str, default="../results",
                        help="Root directory holding per-model result subdirs (default: ../results)")
    parser.add_argument("--output_root", type=str, default="../eval_result",
                        help="Root directory for per-model eval output (default: ../eval_result)")
    args = parser.parse_args()

    model_name = args.model_name
    num_samples = args.num_samples
    results_dir = os.path.join(args.results_root, model_name)

    if not os.path.exists(results_dir):
        print(f"Error: result directory '{results_dir}' does not exist")
        return

    # Ensure output directory exists
    output_dir = os.path.join(args.output_root, model_name)
    os.makedirs(output_dir, exist_ok=True)

    # Get all JSON files
    json_files = [f for f in os.listdir(results_dir) if f.endswith('.json')]

    if not json_files:
        print(f"Error: no JSON files found in directory '{results_dir}'")
        return

    supported_result_files = {
        filename for filename in json_files
        if parse_result_filename(filename) is not None
    }
    unsupported_result_files = sorted(set(json_files) - supported_result_files)
    expected_result_files = {
        f"{scenario}{number}_{mode}.json"
        for scenario, (minimum, maximum) in SCENARIO_NUMBER_RANGES.items()
        for number in range(minimum, maximum + 1)
        for mode in ("easy", "hard", "static")
    }
    missing_result_files = sorted(expected_result_files - supported_result_files)
    benchmark_total_tasks = 0
    for scenario, (minimum, maximum) in SCENARIO_NUMBER_RANGES.items():
        for number in range(minimum, maximum + 1):
            ground_truth_path = os.path.join(
                os.path.dirname(__file__), '..', 'scenarios', 'final',
                f'{scenario}{number}.json'
            )
            with open(ground_truth_path, 'r', encoding='utf-8') as stream:
                benchmark_total_tasks += len(json.load(stream)) * 3

    print(f"Found {len(json_files)} result files, starting evaluation...\n")

    # Aggregate all results
    all_results = []

    for json_file in sorted(json_files):
        parsed_filename = parse_result_filename(json_file)
        if parsed_filename is None:
            print(f"Skipping unsupported result filename: {json_file}")
            continue
        scenario_prefix, scenario_number, user_mode = parsed_filename

        ground_truth_file = os.path.join(
            os.path.dirname(__file__), '..', 'scenarios', 'final',
            f'{scenario_prefix}{scenario_number}.json'
        )
        interaction_log_file = f"{results_dir}/{json_file}"

        # Check if ground truth file exists
        if not os.path.exists(ground_truth_file):
            print(f"Skipped: ground truth file not found '{ground_truth_file}'")
            continue

        print(f"Evaluating: {json_file}...", end=" ")

        try:
            results = evaluate_interaction_success(
                ground_truth_file,
                interaction_log_file,
                scenario=scenario_prefix,
                args=argparse.Namespace(scenario_number=scenario_number),
                silent=True,
                num_samples=num_samples
            )

            # Save per-file evaluation results
            output_file = f"{output_dir}/{json_file.replace('.json', '_eval.json')}"
            with open(output_file, 'w', encoding='utf-8') as f:
                json.dump(results, f, ensure_ascii=False, indent=2)

            print(f"Evaluation results saved to {output_file}")

            # Print brief report (success rates only)
            print(f"Valid samples: {results['valid_scenarios']}/{results['total_scenarios']}, "
                  f"Tool success rate: {results['tool_based']['success_rate']:.2%}, "
                  f"Result success rate: {results['result_based']['success_rate']:.2%}, "
                  f"Joint success rate: {results['joint_success']['success_rate']:.2%}")
            included_in_summary = results["is_complete"] or (
                args.include_partial and results["valid_scenarios"] > 0
            )
            if not included_in_summary:
                print("Excluded from aggregate accuracy: result file is incomplete")
            elif not results["is_complete"]:
                print("Included available trajectories from incomplete result file")

            performance = results["performance_metrics"]

            all_results.append({
                "file": json_file,
                "scenario": scenario_prefix,
                "scenario_number": scenario_number,
                "mode": user_mode,
                "total_scenarios": results["total_scenarios"],
                "valid_scenarios": results["valid_scenarios"],
                "is_complete": results["is_complete"],
                "included_in_summary": included_in_summary,
                "tool_based_success_rate": results["tool_based"]["success_rate"],
                "result_based_success_rate": results["result_based"]["success_rate"],
                "joint_success_rate": results["joint_success"]["success_rate"],
                "micro_accuracy": results["micro_tool_stats"]["micro_accuracy"],
                "avg_task_accuracy": results["micro_tool_stats"]["avg_task_accuracy"],
                "avg_user_response_time": performance["avg_user_response_time"],
                "avg_agent_response_time": performance["avg_agent_response_time"],
                "avg_tokens_consumed": performance["avg_tokens_consumed"],
                "avg_rounds_count": performance["avg_rounds_count"],
                "avg_input_tokens": performance["avg_input_tokens"],
                "avg_output_tokens": performance["avg_output_tokens"],
                "avg_tool_calls_count": performance["avg_tool_calls_count"],
                "avg_user_performance": performance["avg_user_performance"],
                "user_performance_sample_counts": performance["user_performance_sample_counts"],
            })

        except Exception as e:
            print(f"Evaluation failed {json_file}: {e}")
            all_results.append({
                "file": json_file,
                "error": str(e)
            })

    # Print summary report
    print("\n" + "="*120)
    print("Evaluation Summary Report")
    print("="*120)
    if num_samples > 0:
        print(f"Sample limit: first {num_samples} samples per scenario")
    else:
        print("Sample limit: test all samples")
    print(f"{'File':<40} {'Scenario':<12} {'Num':<6} {'Mode':<8} {'Valid':<10} {'Tool Rate':<10} {'Result Rate':<10} {'Joint Rate':<10}")
    print("-"*120)

    for r in all_results:
        if "error" in r:
            print(f"{r['file']:<40} Error: {r['error']}")
        else:
            valid_str = f"{r.get('valid_scenarios', '?')}/{r.get('total_scenarios', '?')}"
            print(f"{r['file']:<40} {r['scenario']:<12} {r['scenario_number']:<6} {r['mode']:<8} "
                  f"{valid_str:<10} "
                  f"{r['tool_based_success_rate']:>8.2%}   {r['result_based_success_rate']:>8.2%}   "
                  f"{r['joint_success_rate']:>8.2%}")

    # Calculate average success rates
    valid_results = [
        r for r in all_results
        if "error" not in r and r.get("included_in_summary", False)
    ]
    incomplete_results = [
        r for r in all_results
        if "error" not in r and not r.get("is_complete", False)
    ]
    excluded_incomplete_results = [
        r for r in incomplete_results
        if not r.get("included_in_summary", False)
    ]
    included_partial_results = [
        r for r in incomplete_results
        if r.get("included_in_summary", False)
    ]
    if excluded_incomplete_results:
        print(
            "Excluded incomplete files from aggregate accuracy: "
            + ", ".join(r["file"] for r in excluded_incomplete_results)
        )
    if included_partial_results:
        print(
            "Included valid trajectories from incomplete files: "
            + ", ".join(r["file"] for r in included_partial_results)
        )
    if valid_results:
        total_valid = sum(r["valid_scenarios"] for r in valid_results)
        total_tasks = sum(r["total_scenarios"] for r in valid_results)

        def task_weighted_average(field):
            return sum(
                r[field] * r["valid_scenarios"] for r in valid_results
            ) / total_valid

        avg_tool_success = task_weighted_average("tool_based_success_rate")
        avg_result_success = task_weighted_average("result_based_success_rate")
        avg_joint_success = task_weighted_average("joint_success_rate")
        avg_micro_accuracy = task_weighted_average("micro_accuracy")
        avg_task_accuracy = task_weighted_average("avg_task_accuracy")
        avg_user_response_time = task_weighted_average("avg_user_response_time")
        avg_agent_response_time = task_weighted_average("avg_agent_response_time")
        avg_tokens_consumed = task_weighted_average("avg_tokens_consumed")
        avg_rounds_count = task_weighted_average("avg_rounds_count")
        avg_input_tokens = task_weighted_average("avg_input_tokens")
        avg_output_tokens = task_weighted_average("avg_output_tokens")
        avg_tool_calls_count = task_weighted_average("avg_tool_calls_count")

        avg_user_performance = {}
        user_performance_sample_counts = {}
        for key in USER_PERFORMANCE_KEYS:
            count = sum(
                r["user_performance_sample_counts"].get(key, 0)
                for r in valid_results
            )
            user_performance_sample_counts[key] = count
            avg_user_performance[key] = (
                sum(
                    r["avg_user_performance"].get(key, 0.0)
                    * r["user_performance_sample_counts"].get(key, 0)
                    for r in valid_results
                ) / count
                if count > 0 else 0.0
            )

        print("-"*120)
        print(f"Average success rates: tool-based={avg_tool_success:.2%}, result-based={avg_result_success:.2%}, joint={avg_joint_success:.2%}")
        print(f"Micro tool calls: overall accuracy={avg_micro_accuracy:.2%}, avg task accuracy={avg_task_accuracy:.2%}")
        weighting = (
            "all available valid trajectories"
            if args.include_partial else "complete files only"
        )
        print(f"   [weighting] {weighting}: tasks={total_valid}/{total_tasks}")
        print(
            f"   [benchmark coverage] tasks={total_valid}/{benchmark_total_tasks} "
            f"({total_valid / benchmark_total_tasks:.2%})"
        )
        print()
        print("Overall performance averages:")
        print(f"   Avg user response time: {avg_user_response_time:.2f}s")
        print(f"   Avg agent response time: {avg_agent_response_time:.2f}s")
        print(f"   Avg tokens consumed: {avg_tokens_consumed:.0f}")
        print(f"   Avg conversation rounds: {avg_rounds_count:.2f}")
        print(f"   Avg input tokens: {avg_input_tokens:.0f}")
        print(f"   Avg output tokens: {avg_output_tokens:.0f}")
        print(f"   Avg tool calls: {avg_tool_calls_count:.2f}")
        print()
        print("Simulated-user metric averages:")
        for key in USER_PERFORMANCE_KEYS:
            print(
                f"   {key}: {avg_user_performance[key]:.4f} "
                f"(n={user_performance_sample_counts[key]})"
            )

    # Save summary results
    summary_file = f"{output_dir}/summary.json"
    with open(summary_file, 'w', encoding='utf-8') as f:
        json.dump({"all_results": all_results, "summary": {
            "total_files": len(all_results),
            "valid_files": len(valid_results),
            "incomplete_files": len(incomplete_results),
            "included_partial_files": [r["file"] for r in included_partial_results],
            "excluded_incomplete_files": [r["file"] for r in excluded_incomplete_results],
            "num_samples": num_samples,
            "include_partial": args.include_partial,
            "overall_weighting": (
                "all_available_valid_trajectories_task_weighted"
                if args.include_partial else "complete_files_only_task_weighted"
            ),
            "total_valid_scenarios": total_valid if valid_results else 0,
            "total_original_tasks": total_tasks if valid_results else 0,
            "trajectory_coverage_rate": (
                total_valid / total_tasks
                if valid_results and total_tasks > 0 else 0
            ),
            "benchmark_total_tasks": benchmark_total_tasks,
            "benchmark_coverage_rate": (
                total_valid / benchmark_total_tasks
                if valid_results and benchmark_total_tasks > 0 else 0
            ),
            "missing_result_files": missing_result_files,
            "unsupported_result_files": unsupported_result_files,
            "avg_tool_based_success_rate": avg_tool_success if valid_results else 0,
            "avg_result_based_success_rate": avg_result_success if valid_results else 0,
            "avg_joint_success_rate": avg_joint_success if valid_results else 0,
            "micro_accuracy": avg_micro_accuracy if valid_results else 0,
            "avg_task_accuracy": avg_task_accuracy if valid_results else 0,
            "avg_user_response_time": avg_user_response_time if valid_results else 0,
            "avg_agent_response_time": avg_agent_response_time if valid_results else 0,
            "avg_tokens_consumed": avg_tokens_consumed if valid_results else 0,
            "avg_rounds_count": avg_rounds_count if valid_results else 0,
            "avg_input_tokens": avg_input_tokens if valid_results else 0,
            "avg_output_tokens": avg_output_tokens if valid_results else 0,
            "avg_tool_calls_count": avg_tool_calls_count if valid_results else 0,
            "avg_user_performance": avg_user_performance if valid_results else {
                key: 0.0 for key in USER_PERFORMANCE_KEYS
            },
            "user_performance_sample_counts": user_performance_sample_counts if valid_results else {
                key: 0 for key in USER_PERFORMANCE_KEYS
            },
        }}, f, ensure_ascii=False, indent=2)
    print(f"\nSummary results saved to {summary_file}")


if __name__ == "__main__":
    main()