Commit 993f676
UN-2946 [MISC] Flush embedding usage records on indexing path (#1962)
* UN-2946 [FIX] Flush embedding usage records on indexing path
The deferred-batch usage refactor stopped flushing embedding-callback
records: ``UsageHandler.on_event_end`` now appends to ``_pending_usage``
instead of pushing directly, but ``_handle_index`` (and
``_run_pipeline_index``) never drained the list. Embedding rows fell
off ``Usage`` for every workflow / API-deployment run, so the API
response's ``metadata.usage.embedding_tokens`` reported 0 despite
indexing actually happening.
- Add ``EmbeddingCompat.flush_pending_usage()`` mirroring the LLM shim.
- ``_handle_index`` flushes embedding into
``ExecutionResult.metadata["usage_records"]`` on all success exits
and attaches partial rows via ``LegacyExecutorError.partial_usage_records``
on the error path.
- ``_run_pipeline_index`` now returns ``(metrics, records)`` so
``_handle_structure_pipeline`` can absorb embedding rows into
``pipeline_records``. Existing IDE-index path already absorbs via
``metadata["usage_records"]`` and starts working automatically.
- Fix the ``_run_pipeline_index`` mock in test_phase5d to return the
new tuple shape.
* UN-2946 [FIX] Preserve mid-loop index records on LegacyExecutorError
If ``_handle_index`` raises in iteration N of the per-output loop,
records accumulated from iterations 1…N-1 were dropped because the
exception escaped ``_run_pipeline_index`` unmodified and
``_handle_structure_pipeline``'s ``except`` branch only inherits
``e.partial_usage_records`` (the N-th iteration's partial rows) and
``pipeline_records`` (which never received the in-flight tuple).
Mirror the ``_handle_ide_index`` pattern: wrap the ``_handle_index``
call in a try/except and prepend ``index_records`` to
``e.partial_usage_records`` before re-raising so the outer handler
sees every row the worker had collected so far.
* Update workers/executor/executors/legacy_executor.py
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Chandrasekharan M <117059509+chandrasekharan-zipstack@users.noreply.github.com>
* UN-2946 [REFACTOR] Split _run_pipeline_index / _handle_index helpers
Extract structure-pipeline indexing loop body into _index_pipeline_output
and missing-param validation into _missing_index_params to drop both
functions below SonarCloud's cognitive complexity threshold.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* UN-2946 [FIX] Guard embedding handler flush and propagate index failure
- EmbeddingCompat.flush_pending_usage now wraps each handler call in
try/except so one bad handler doesn't drop usage rows from the rest
and doesn't escape into the indexing success path.
- _index_pipeline_output now raises LegacyExecutorError when
_handle_index returns a failure result, so the structure pipeline
aborts instead of running downstream steps against an incomplete
vector store.
- Added regression tests asserting embedding usage rows propagate
through the structure pipeline and that an indexing returned-failure
short-circuits before answer_prompt.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Signed-off-by: Chandrasekharan M <117059509+chandrasekharan-zipstack@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Athul <89829560+athul-rs@users.noreply.github.com>1 parent 8fdef4e commit 993f676
3 files changed
Lines changed: 260 additions & 88 deletions
File tree
- unstract/sdk1/src/unstract/sdk1
- workers
- executor/executors
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
281 | 281 | | |
282 | 282 | | |
283 | 283 | | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
659 | 659 | | |
660 | 660 | | |
661 | 661 | | |
662 | | - | |
| 662 | + | |
663 | 663 | | |
664 | 664 | | |
665 | 665 | | |
666 | 666 | | |
667 | 667 | | |
668 | 668 | | |
| 669 | + | |
| 670 | + | |
669 | 671 | | |
670 | 672 | | |
671 | 673 | | |
| |||
891 | 893 | | |
892 | 894 | | |
893 | 895 | | |
894 | | - | |
| 896 | + | |
895 | 897 | | |
896 | 898 | | |
897 | 899 | | |
898 | 900 | | |
899 | 901 | | |
900 | 902 | | |
901 | | - | |
902 | | - | |
903 | | - | |
| 903 | + | |
904 | 904 | | |
905 | 905 | | |
906 | | - | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
907 | 909 | | |
908 | | - | |
909 | | - | |
910 | 910 | | |
911 | 911 | | |
912 | | - | |
913 | | - | |
914 | | - | |
915 | | - | |
916 | | - | |
917 | | - | |
918 | | - | |
919 | 912 | | |
| 913 | + | |
920 | 914 | | |
921 | 915 | | |
922 | 916 | | |
923 | | - | |
924 | | - | |
925 | | - | |
926 | | - | |
927 | | - | |
928 | | - | |
929 | | - | |
930 | | - | |
931 | | - | |
932 | | - | |
933 | | - | |
934 | | - | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
935 | 927 | | |
936 | 928 | | |
937 | | - | |
938 | | - | |
| 929 | + | |
939 | 930 | | |
940 | | - | |
941 | | - | |
942 | | - | |
943 | | - | |
944 | | - | |
945 | | - | |
946 | | - | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
947 | 946 | | |
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 | | - | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
981 | 950 | | |
982 | | - | |
983 | | - | |
984 | | - | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
985 | 966 | | |
986 | | - | |
| 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 | + | |
987 | 1022 | | |
988 | 1023 | | |
989 | 1024 | | |
| |||
1008 | 1043 | | |
1009 | 1044 | | |
1010 | 1045 | | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
1011 | 1063 | | |
1012 | 1064 | | |
1013 | 1065 | | |
| |||
1027 | 1079 | | |
1028 | 1080 | | |
1029 | 1081 | | |
1030 | | - | |
1031 | | - | |
1032 | | - | |
1033 | | - | |
1034 | | - | |
1035 | | - | |
1036 | | - | |
1037 | | - | |
1038 | | - | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
1039 | 1088 | | |
1040 | 1089 | | |
1041 | 1090 | | |
| |||
1110 | 1159 | | |
1111 | 1160 | | |
1112 | 1161 | | |
| 1162 | + | |
1113 | 1163 | | |
1114 | 1164 | | |
1115 | 1165 | | |
| |||
1152 | 1202 | | |
1153 | 1203 | | |
1154 | 1204 | | |
1155 | | - | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
1156 | 1210 | | |
1157 | 1211 | | |
1158 | 1212 | | |
| |||
1169 | 1223 | | |
1170 | 1224 | | |
1171 | 1225 | | |
1172 | | - | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
1173 | 1231 | | |
1174 | 1232 | | |
1175 | 1233 | | |
1176 | 1234 | | |
1177 | 1235 | | |
1178 | 1236 | | |
1179 | 1237 | | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
1180 | 1247 | | |
1181 | | - | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
1182 | 1251 | | |
1183 | 1252 | | |
1184 | 1253 | | |
| |||
0 commit comments