nvidia-nat 1.4.0a20251112__py3-none-any.whl → 1.4.0a20260113__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (492) hide show
  1. aiq/__init__.py +1 -1
  2. nat/{front_ends/mcp → agent/auto_memory_wrapper}/__init__.py +1 -1
  3. nat/agent/auto_memory_wrapper/agent.py +278 -0
  4. nat/agent/auto_memory_wrapper/register.py +227 -0
  5. nat/agent/auto_memory_wrapper/state.py +30 -0
  6. nat/agent/base.py +1 -1
  7. nat/agent/dual_node.py +1 -1
  8. nat/agent/prompt_optimizer/prompt.py +1 -1
  9. nat/agent/prompt_optimizer/register.py +1 -1
  10. nat/agent/react_agent/agent.py +16 -9
  11. nat/agent/react_agent/output_parser.py +2 -2
  12. nat/agent/react_agent/prompt.py +3 -2
  13. nat/agent/react_agent/register.py +2 -2
  14. nat/agent/react_agent/register_per_user_agent.py +104 -0
  15. nat/agent/reasoning_agent/reasoning_agent.py +1 -1
  16. nat/agent/register.py +3 -1
  17. nat/agent/responses_api_agent/__init__.py +1 -1
  18. nat/agent/responses_api_agent/register.py +1 -1
  19. nat/agent/rewoo_agent/agent.py +9 -4
  20. nat/agent/rewoo_agent/prompt.py +1 -1
  21. nat/agent/rewoo_agent/register.py +1 -1
  22. nat/agent/tool_calling_agent/agent.py +5 -4
  23. nat/agent/tool_calling_agent/register.py +1 -1
  24. nat/authentication/__init__.py +1 -1
  25. nat/authentication/api_key/__init__.py +1 -1
  26. nat/authentication/api_key/api_key_auth_provider.py +1 -1
  27. nat/authentication/api_key/api_key_auth_provider_config.py +22 -7
  28. nat/authentication/api_key/register.py +1 -1
  29. nat/authentication/credential_validator/__init__.py +1 -1
  30. nat/authentication/credential_validator/bearer_token_validator.py +1 -1
  31. nat/authentication/exceptions/__init__.py +1 -1
  32. nat/authentication/exceptions/api_key_exceptions.py +1 -1
  33. nat/authentication/http_basic_auth/http_basic_auth_provider.py +1 -1
  34. nat/authentication/http_basic_auth/register.py +1 -1
  35. nat/authentication/interfaces.py +1 -1
  36. nat/authentication/oauth2/__init__.py +1 -1
  37. nat/authentication/oauth2/oauth2_auth_code_flow_provider.py +1 -1
  38. nat/authentication/oauth2/oauth2_auth_code_flow_provider_config.py +1 -1
  39. nat/authentication/oauth2/oauth2_resource_server_config.py +1 -1
  40. nat/authentication/oauth2/register.py +1 -1
  41. nat/authentication/register.py +1 -1
  42. nat/builder/builder.py +563 -1
  43. nat/builder/child_builder.py +385 -0
  44. nat/builder/component_utils.py +34 -4
  45. nat/builder/context.py +34 -1
  46. nat/builder/embedder.py +1 -1
  47. nat/builder/eval_builder.py +19 -7
  48. nat/builder/evaluator.py +1 -1
  49. nat/builder/framework_enum.py +3 -1
  50. nat/builder/front_end.py +1 -1
  51. nat/builder/function.py +113 -5
  52. nat/builder/function_base.py +1 -1
  53. nat/builder/function_info.py +1 -1
  54. nat/builder/intermediate_step_manager.py +1 -1
  55. nat/builder/llm.py +1 -1
  56. nat/builder/per_user_workflow_builder.py +843 -0
  57. nat/builder/retriever.py +1 -1
  58. nat/builder/sync_builder.py +571 -0
  59. nat/builder/user_interaction_manager.py +1 -1
  60. nat/builder/workflow.py +5 -3
  61. nat/builder/workflow_builder.py +619 -378
  62. nat/cli/__init__.py +1 -1
  63. nat/cli/cli_utils/config_override.py +1 -1
  64. nat/cli/cli_utils/validation.py +32 -1
  65. nat/cli/commands/configure/channel/add.py +1 -1
  66. nat/cli/commands/configure/channel/channel.py +1 -1
  67. nat/cli/commands/configure/channel/remove.py +1 -1
  68. nat/cli/commands/configure/channel/update.py +1 -1
  69. nat/cli/commands/configure/configure.py +1 -1
  70. nat/cli/commands/evaluate.py +87 -13
  71. nat/cli/commands/finetune.py +132 -0
  72. nat/cli/commands/info/__init__.py +1 -1
  73. nat/cli/commands/info/info.py +1 -1
  74. nat/cli/commands/info/list_channels.py +1 -1
  75. nat/cli/commands/info/list_components.py +1 -1
  76. nat/cli/commands/object_store/__init__.py +1 -1
  77. nat/cli/commands/object_store/object_store.py +1 -1
  78. nat/cli/commands/optimize.py +1 -1
  79. nat/cli/commands/{mcp → red_teaming}/__init__.py +1 -1
  80. nat/cli/commands/red_teaming/red_teaming.py +138 -0
  81. nat/cli/commands/red_teaming/red_teaming_utils.py +73 -0
  82. nat/cli/commands/registry/__init__.py +1 -1
  83. nat/cli/commands/registry/publish.py +1 -1
  84. nat/cli/commands/registry/pull.py +1 -1
  85. nat/cli/commands/registry/registry.py +1 -1
  86. nat/cli/commands/registry/remove.py +1 -1
  87. nat/cli/commands/registry/search.py +1 -1
  88. nat/cli/commands/sizing/__init__.py +1 -1
  89. nat/cli/commands/sizing/calc.py +1 -1
  90. nat/cli/commands/sizing/sizing.py +1 -1
  91. nat/cli/commands/start.py +1 -1
  92. nat/cli/commands/uninstall.py +1 -1
  93. nat/cli/commands/validate.py +1 -1
  94. nat/cli/commands/workflow/__init__.py +1 -1
  95. nat/cli/commands/workflow/workflow.py +1 -1
  96. nat/cli/commands/workflow/workflow_commands.py +3 -2
  97. nat/cli/entrypoint.py +15 -37
  98. nat/cli/main.py +2 -2
  99. nat/cli/plugin_loader.py +69 -0
  100. nat/cli/register_workflow.py +233 -5
  101. nat/cli/type_registry.py +237 -3
  102. nat/control_flow/register.py +1 -1
  103. nat/control_flow/router_agent/agent.py +1 -1
  104. nat/control_flow/router_agent/prompt.py +1 -1
  105. nat/control_flow/router_agent/register.py +1 -1
  106. nat/control_flow/sequential_executor.py +28 -7
  107. nat/data_models/__init__.py +1 -1
  108. nat/data_models/agent.py +1 -1
  109. nat/data_models/api_server.py +38 -3
  110. nat/data_models/authentication.py +1 -1
  111. nat/data_models/common.py +1 -1
  112. nat/data_models/component.py +9 -1
  113. nat/data_models/component_ref.py +45 -1
  114. nat/data_models/config.py +78 -1
  115. nat/data_models/dataset_handler.py +15 -2
  116. nat/data_models/discovery_metadata.py +1 -1
  117. nat/data_models/embedder.py +1 -1
  118. nat/data_models/evaluate.py +6 -1
  119. nat/data_models/evaluator.py +1 -1
  120. nat/data_models/finetuning.py +260 -0
  121. nat/data_models/front_end.py +1 -1
  122. nat/data_models/function.py +15 -2
  123. nat/data_models/function_dependencies.py +1 -1
  124. nat/data_models/gated_field_mixin.py +1 -1
  125. nat/data_models/interactive.py +1 -1
  126. nat/data_models/intermediate_step.py +29 -2
  127. nat/data_models/invocation_node.py +1 -1
  128. nat/data_models/llm.py +1 -1
  129. nat/data_models/logging.py +1 -1
  130. nat/data_models/memory.py +1 -1
  131. nat/data_models/middleware.py +37 -0
  132. nat/data_models/object_store.py +1 -1
  133. nat/data_models/openai_mcp.py +1 -1
  134. nat/data_models/optimizable.py +1 -1
  135. nat/data_models/optimizer.py +1 -1
  136. nat/data_models/profiler.py +1 -1
  137. nat/data_models/registry_handler.py +1 -1
  138. nat/data_models/retriever.py +1 -1
  139. nat/data_models/retry_mixin.py +1 -1
  140. nat/data_models/runtime_enum.py +26 -0
  141. nat/data_models/span.py +1 -1
  142. nat/data_models/step_adaptor.py +1 -1
  143. nat/data_models/streaming.py +1 -1
  144. nat/data_models/swe_bench_model.py +1 -1
  145. nat/data_models/telemetry_exporter.py +1 -1
  146. nat/data_models/thinking_mixin.py +1 -1
  147. nat/data_models/ttc_strategy.py +1 -1
  148. nat/embedder/azure_openai_embedder.py +1 -1
  149. nat/embedder/nim_embedder.py +1 -1
  150. nat/embedder/openai_embedder.py +1 -1
  151. nat/embedder/register.py +1 -1
  152. nat/eval/__init__.py +1 -1
  153. nat/eval/config.py +8 -1
  154. nat/eval/dataset_handler/dataset_downloader.py +1 -1
  155. nat/eval/dataset_handler/dataset_filter.py +1 -1
  156. nat/eval/dataset_handler/dataset_handler.py +4 -2
  157. nat/eval/evaluate.py +226 -81
  158. nat/eval/evaluator/__init__.py +1 -1
  159. nat/eval/evaluator/base_evaluator.py +2 -2
  160. nat/eval/evaluator/evaluator_model.py +3 -2
  161. nat/eval/intermediate_step_adapter.py +1 -1
  162. nat/eval/llm_validator.py +336 -0
  163. nat/eval/rag_evaluator/evaluate.py +17 -10
  164. nat/eval/rag_evaluator/register.py +1 -1
  165. nat/eval/red_teaming_evaluator/__init__.py +14 -0
  166. nat/eval/red_teaming_evaluator/data_models.py +66 -0
  167. nat/eval/red_teaming_evaluator/evaluate.py +327 -0
  168. nat/eval/red_teaming_evaluator/filter_conditions.py +75 -0
  169. nat/eval/red_teaming_evaluator/register.py +55 -0
  170. nat/eval/register.py +2 -1
  171. nat/eval/remote_workflow.py +1 -1
  172. nat/eval/runners/__init__.py +1 -1
  173. nat/eval/runners/config.py +1 -1
  174. nat/eval/runners/multi_eval_runner.py +1 -1
  175. nat/eval/runners/red_teaming_runner/__init__.py +24 -0
  176. nat/eval/runners/red_teaming_runner/config.py +282 -0
  177. nat/eval/runners/red_teaming_runner/report_utils.py +707 -0
  178. nat/eval/runners/red_teaming_runner/runner.py +867 -0
  179. nat/eval/runtime_evaluator/__init__.py +1 -1
  180. nat/eval/runtime_evaluator/evaluate.py +1 -1
  181. nat/eval/runtime_evaluator/register.py +1 -1
  182. nat/eval/runtime_event_subscriber.py +1 -1
  183. nat/eval/swe_bench_evaluator/evaluate.py +1 -1
  184. nat/eval/swe_bench_evaluator/register.py +1 -1
  185. nat/eval/trajectory_evaluator/evaluate.py +2 -2
  186. nat/eval/trajectory_evaluator/register.py +1 -1
  187. nat/eval/tunable_rag_evaluator/evaluate.py +5 -5
  188. nat/eval/tunable_rag_evaluator/register.py +1 -1
  189. nat/eval/usage_stats.py +1 -1
  190. nat/eval/utils/eval_trace_ctx.py +1 -1
  191. nat/eval/utils/output_uploader.py +1 -1
  192. nat/eval/utils/tqdm_position_registry.py +1 -1
  193. nat/eval/utils/weave_eval.py +1 -1
  194. nat/experimental/decorators/experimental_warning_decorator.py +1 -1
  195. nat/experimental/test_time_compute/editing/iterative_plan_refinement_editor.py +1 -1
  196. nat/experimental/test_time_compute/editing/llm_as_a_judge_editor.py +1 -1
  197. nat/experimental/test_time_compute/editing/motivation_aware_summarization.py +1 -1
  198. nat/experimental/test_time_compute/functions/execute_score_select_function.py +1 -1
  199. nat/experimental/test_time_compute/functions/multi_llm_judge_function.py +88 -0
  200. nat/experimental/test_time_compute/functions/plan_select_execute_function.py +1 -1
  201. nat/experimental/test_time_compute/functions/ttc_tool_orchestration_function.py +1 -1
  202. nat/experimental/test_time_compute/functions/ttc_tool_wrapper_function.py +1 -1
  203. nat/experimental/test_time_compute/models/editor_config.py +1 -1
  204. nat/experimental/test_time_compute/models/scoring_config.py +1 -1
  205. nat/experimental/test_time_compute/models/search_config.py +20 -2
  206. nat/experimental/test_time_compute/models/selection_config.py +33 -2
  207. nat/experimental/test_time_compute/models/stage_enums.py +1 -1
  208. nat/experimental/test_time_compute/models/strategy_base.py +1 -1
  209. nat/experimental/test_time_compute/models/tool_use_config.py +1 -1
  210. nat/experimental/test_time_compute/models/ttc_item.py +1 -1
  211. nat/experimental/test_time_compute/register.py +4 -1
  212. nat/experimental/test_time_compute/scoring/llm_based_agent_scorer.py +1 -1
  213. nat/experimental/test_time_compute/scoring/llm_based_plan_scorer.py +1 -1
  214. nat/experimental/test_time_compute/scoring/motivation_aware_scorer.py +1 -1
  215. nat/experimental/test_time_compute/search/multi_llm_generation.py +115 -0
  216. nat/experimental/test_time_compute/search/multi_llm_planner.py +1 -1
  217. nat/experimental/test_time_compute/search/multi_query_retrieval_search.py +1 -1
  218. nat/experimental/test_time_compute/search/single_shot_multi_plan_planner.py +1 -1
  219. nat/experimental/test_time_compute/selection/best_of_n_selector.py +1 -1
  220. nat/experimental/test_time_compute/selection/llm_based_agent_output_selector.py +1 -1
  221. nat/experimental/test_time_compute/selection/llm_based_output_merging_selector.py +1 -1
  222. nat/experimental/test_time_compute/selection/llm_based_plan_selector.py +1 -1
  223. nat/experimental/test_time_compute/selection/llm_judge_selection.py +127 -0
  224. nat/experimental/test_time_compute/selection/threshold_selector.py +1 -1
  225. nat/finetuning/__init__.py +24 -0
  226. nat/finetuning/finetuning_runtime.py +143 -0
  227. nat/finetuning/interfaces/__init__.py +24 -0
  228. nat/finetuning/interfaces/finetuning_runner.py +261 -0
  229. nat/finetuning/interfaces/trainer_adapter.py +103 -0
  230. nat/finetuning/interfaces/trajectory_builder.py +115 -0
  231. nat/finetuning/utils/__init__.py +15 -0
  232. nat/finetuning/utils/parsers/__init__.py +15 -0
  233. nat/finetuning/utils/parsers/adk_parser.py +141 -0
  234. nat/finetuning/utils/parsers/base_parser.py +238 -0
  235. nat/finetuning/utils/parsers/common.py +91 -0
  236. nat/finetuning/utils/parsers/langchain_parser.py +267 -0
  237. nat/finetuning/utils/parsers/llama_index_parser.py +218 -0
  238. nat/front_ends/__init__.py +1 -1
  239. nat/front_ends/console/__init__.py +1 -1
  240. nat/front_ends/console/authentication_flow_handler.py +1 -1
  241. nat/front_ends/console/console_front_end_config.py +4 -1
  242. nat/front_ends/console/console_front_end_plugin.py +5 -4
  243. nat/front_ends/console/register.py +1 -1
  244. nat/front_ends/cron/__init__.py +1 -1
  245. nat/front_ends/fastapi/__init__.py +1 -1
  246. nat/front_ends/fastapi/async_job.py +128 -0
  247. nat/front_ends/fastapi/auth_flow_handlers/http_flow_handler.py +1 -1
  248. nat/front_ends/fastapi/auth_flow_handlers/websocket_flow_handler.py +13 -9
  249. nat/front_ends/fastapi/dask_client_mixin.py +1 -1
  250. nat/front_ends/fastapi/fastapi_front_end_config.py +23 -1
  251. nat/front_ends/fastapi/fastapi_front_end_controller.py +1 -1
  252. nat/front_ends/fastapi/fastapi_front_end_plugin.py +25 -30
  253. nat/front_ends/fastapi/fastapi_front_end_plugin_worker.py +318 -59
  254. nat/front_ends/fastapi/html_snippets/__init__.py +1 -1
  255. nat/front_ends/fastapi/html_snippets/auth_code_grant_success.py +1 -1
  256. nat/front_ends/fastapi/intermediate_steps_subscriber.py +12 -1
  257. nat/front_ends/fastapi/job_store.py +23 -11
  258. nat/front_ends/fastapi/main.py +1 -1
  259. nat/front_ends/fastapi/message_handler.py +27 -4
  260. nat/front_ends/fastapi/message_validator.py +54 -2
  261. nat/front_ends/fastapi/register.py +1 -1
  262. nat/front_ends/fastapi/response_helpers.py +16 -15
  263. nat/front_ends/fastapi/step_adaptor.py +1 -1
  264. nat/front_ends/fastapi/utils.py +1 -1
  265. nat/front_ends/register.py +1 -2
  266. nat/front_ends/simple_base/__init__.py +1 -1
  267. nat/front_ends/simple_base/simple_front_end_plugin_base.py +6 -4
  268. nat/llm/aws_bedrock_llm.py +1 -1
  269. nat/llm/azure_openai_llm.py +10 -1
  270. nat/llm/dynamo_llm.py +363 -0
  271. nat/llm/huggingface_llm.py +177 -0
  272. nat/llm/litellm_llm.py +1 -1
  273. nat/llm/nim_llm.py +1 -1
  274. nat/llm/openai_llm.py +1 -1
  275. nat/llm/register.py +3 -1
  276. nat/llm/utils/__init__.py +1 -1
  277. nat/llm/utils/env_config_value.py +1 -1
  278. nat/llm/utils/error.py +1 -1
  279. nat/llm/utils/thinking.py +1 -1
  280. nat/memory/__init__.py +1 -1
  281. nat/memory/interfaces.py +1 -1
  282. nat/memory/models.py +1 -1
  283. nat/meta/pypi.md +1 -1
  284. nat/middleware/__init__.py +35 -0
  285. nat/middleware/cache/__init__.py +14 -0
  286. nat/middleware/cache/cache_middleware.py +253 -0
  287. nat/middleware/cache/cache_middleware_config.py +44 -0
  288. nat/middleware/cache/register.py +33 -0
  289. nat/middleware/defense/__init__.py +14 -0
  290. nat/middleware/defense/defense_middleware.py +362 -0
  291. nat/middleware/defense/defense_middleware_content_guard.py +455 -0
  292. nat/middleware/defense/defense_middleware_data_models.py +91 -0
  293. nat/middleware/defense/defense_middleware_output_verifier.py +440 -0
  294. nat/middleware/defense/defense_middleware_pii.py +356 -0
  295. nat/middleware/defense/register.py +82 -0
  296. nat/middleware/dynamic/__init__.py +14 -0
  297. nat/middleware/dynamic/dynamic_function_middleware.py +962 -0
  298. nat/middleware/dynamic/dynamic_middleware_config.py +132 -0
  299. nat/middleware/dynamic/register.py +34 -0
  300. nat/middleware/function_middleware.py +370 -0
  301. nat/middleware/logging/__init__.py +14 -0
  302. nat/middleware/logging/logging_middleware.py +67 -0
  303. nat/middleware/logging/logging_middleware_config.py +28 -0
  304. nat/middleware/logging/register.py +33 -0
  305. nat/middleware/middleware.py +298 -0
  306. nat/middleware/red_teaming/__init__.py +14 -0
  307. nat/middleware/red_teaming/red_teaming_middleware.py +344 -0
  308. nat/middleware/red_teaming/red_teaming_middleware_config.py +112 -0
  309. nat/middleware/red_teaming/register.py +47 -0
  310. nat/middleware/register.py +22 -0
  311. nat/middleware/utils/__init__.py +14 -0
  312. nat/middleware/utils/workflow_inventory.py +155 -0
  313. nat/object_store/__init__.py +1 -1
  314. nat/object_store/in_memory_object_store.py +1 -1
  315. nat/object_store/interfaces.py +1 -1
  316. nat/object_store/models.py +1 -1
  317. nat/object_store/register.py +1 -1
  318. nat/observability/__init__.py +1 -1
  319. nat/observability/exporter/__init__.py +1 -1
  320. nat/observability/exporter/base_exporter.py +1 -1
  321. nat/observability/exporter/exporter.py +1 -1
  322. nat/observability/exporter/file_exporter.py +1 -1
  323. nat/observability/exporter/processing_exporter.py +1 -1
  324. nat/observability/exporter/raw_exporter.py +1 -1
  325. nat/observability/exporter/span_exporter.py +7 -1
  326. nat/observability/exporter_manager.py +1 -1
  327. nat/observability/mixin/__init__.py +1 -1
  328. nat/observability/mixin/batch_config_mixin.py +1 -1
  329. nat/observability/mixin/collector_config_mixin.py +1 -1
  330. nat/observability/mixin/file_mixin.py +1 -1
  331. nat/observability/mixin/file_mode.py +1 -1
  332. nat/observability/mixin/redaction_config_mixin.py +1 -1
  333. nat/observability/mixin/resource_conflict_mixin.py +1 -1
  334. nat/observability/mixin/serialize_mixin.py +1 -1
  335. nat/observability/mixin/tagging_config_mixin.py +1 -1
  336. nat/observability/mixin/type_introspection_mixin.py +1 -1
  337. nat/observability/processor/__init__.py +1 -1
  338. nat/observability/processor/batching_processor.py +1 -1
  339. nat/observability/processor/callback_processor.py +1 -1
  340. nat/observability/processor/falsy_batch_filter_processor.py +1 -1
  341. nat/observability/processor/intermediate_step_serializer.py +1 -1
  342. nat/observability/processor/processor.py +1 -1
  343. nat/observability/processor/processor_factory.py +1 -1
  344. nat/observability/processor/redaction/__init__.py +1 -1
  345. nat/observability/processor/redaction/contextual_redaction_processor.py +1 -1
  346. nat/observability/processor/redaction/contextual_span_redaction_processor.py +1 -1
  347. nat/observability/processor/redaction/redaction_processor.py +1 -1
  348. nat/observability/processor/redaction/span_header_redaction_processor.py +1 -1
  349. nat/observability/processor/span_tagging_processor.py +1 -1
  350. nat/observability/register.py +1 -1
  351. nat/observability/utils/__init__.py +1 -1
  352. nat/observability/utils/dict_utils.py +1 -1
  353. nat/observability/utils/time_utils.py +1 -1
  354. nat/profiler/calc/__init__.py +1 -1
  355. nat/profiler/calc/calc_runner.py +3 -3
  356. nat/profiler/calc/calculations.py +1 -1
  357. nat/profiler/calc/data_models.py +1 -1
  358. nat/profiler/calc/plot.py +30 -3
  359. nat/profiler/callbacks/agno_callback_handler.py +1 -1
  360. nat/profiler/callbacks/base_callback_class.py +1 -1
  361. nat/profiler/callbacks/langchain_callback_handler.py +33 -3
  362. nat/profiler/callbacks/llama_index_callback_handler.py +13 -10
  363. nat/profiler/callbacks/semantic_kernel_callback_handler.py +1 -1
  364. nat/profiler/callbacks/token_usage_base_model.py +1 -1
  365. nat/profiler/data_frame_row.py +1 -1
  366. nat/profiler/data_models.py +1 -1
  367. nat/profiler/decorators/framework_wrapper.py +32 -1
  368. nat/profiler/decorators/function_tracking.py +1 -1
  369. nat/profiler/forecasting/config.py +1 -1
  370. nat/profiler/forecasting/model_trainer.py +1 -1
  371. nat/profiler/forecasting/models/__init__.py +1 -1
  372. nat/profiler/forecasting/models/forecasting_base_model.py +1 -1
  373. nat/profiler/forecasting/models/linear_model.py +1 -1
  374. nat/profiler/forecasting/models/random_forest_regressor.py +1 -1
  375. nat/profiler/inference_metrics_model.py +1 -1
  376. nat/profiler/inference_optimization/bottleneck_analysis/nested_stack_analysis.py +1 -1
  377. nat/profiler/inference_optimization/bottleneck_analysis/simple_stack_analysis.py +1 -1
  378. nat/profiler/inference_optimization/data_models.py +1 -1
  379. nat/profiler/inference_optimization/experimental/concurrency_spike_analysis.py +1 -1
  380. nat/profiler/inference_optimization/experimental/prefix_span_analysis.py +1 -1
  381. nat/profiler/inference_optimization/llm_metrics.py +1 -1
  382. nat/profiler/inference_optimization/prompt_caching.py +1 -1
  383. nat/profiler/inference_optimization/token_uniqueness.py +1 -1
  384. nat/profiler/inference_optimization/workflow_runtimes.py +1 -1
  385. nat/profiler/intermediate_property_adapter.py +1 -1
  386. nat/profiler/parameter_optimization/optimizable_utils.py +1 -1
  387. nat/profiler/parameter_optimization/optimizer_runtime.py +1 -1
  388. nat/profiler/parameter_optimization/parameter_optimizer.py +1 -1
  389. nat/profiler/parameter_optimization/parameter_selection.py +1 -1
  390. nat/profiler/parameter_optimization/pareto_visualizer.py +1 -1
  391. nat/profiler/parameter_optimization/prompt_optimizer.py +1 -1
  392. nat/profiler/parameter_optimization/update_helpers.py +1 -1
  393. nat/profiler/profile_runner.py +1 -1
  394. nat/profiler/utils.py +1 -1
  395. nat/registry_handlers/local/local_handler.py +1 -1
  396. nat/registry_handlers/local/register_local.py +1 -1
  397. nat/registry_handlers/metadata_factory.py +1 -1
  398. nat/registry_handlers/package_utils.py +1 -1
  399. nat/registry_handlers/pypi/pypi_handler.py +1 -1
  400. nat/registry_handlers/pypi/register_pypi.py +1 -1
  401. nat/registry_handlers/register.py +1 -1
  402. nat/registry_handlers/registry_handler_base.py +1 -1
  403. nat/registry_handlers/rest/register_rest.py +1 -1
  404. nat/registry_handlers/rest/rest_handler.py +1 -1
  405. nat/registry_handlers/schemas/headers.py +1 -1
  406. nat/registry_handlers/schemas/package.py +1 -1
  407. nat/registry_handlers/schemas/publish.py +1 -1
  408. nat/registry_handlers/schemas/pull.py +1 -1
  409. nat/registry_handlers/schemas/remove.py +1 -1
  410. nat/registry_handlers/schemas/search.py +1 -1
  411. nat/registry_handlers/schemas/status.py +1 -1
  412. nat/retriever/interface.py +1 -1
  413. nat/retriever/milvus/__init__.py +1 -1
  414. nat/retriever/milvus/register.py +12 -4
  415. nat/retriever/milvus/retriever.py +103 -41
  416. nat/retriever/models.py +1 -1
  417. nat/retriever/nemo_retriever/__init__.py +1 -1
  418. nat/retriever/nemo_retriever/register.py +1 -1
  419. nat/retriever/nemo_retriever/retriever.py +5 -5
  420. nat/retriever/register.py +1 -1
  421. nat/runtime/__init__.py +1 -1
  422. nat/runtime/loader.py +10 -3
  423. nat/runtime/metrics.py +180 -0
  424. nat/runtime/runner.py +13 -6
  425. nat/runtime/session.py +458 -32
  426. nat/runtime/user_metadata.py +1 -1
  427. nat/settings/global_settings.py +1 -1
  428. nat/tool/chat_completion.py +1 -1
  429. nat/tool/code_execution/README.md +1 -1
  430. nat/tool/code_execution/code_sandbox.py +2 -2
  431. nat/tool/code_execution/local_sandbox/Dockerfile.sandbox +1 -1
  432. nat/tool/code_execution/local_sandbox/__init__.py +1 -1
  433. nat/tool/code_execution/local_sandbox/local_sandbox_server.py +1 -1
  434. nat/tool/code_execution/local_sandbox/start_local_sandbox.sh +1 -1
  435. nat/tool/code_execution/register.py +1 -1
  436. nat/tool/code_execution/utils.py +1 -1
  437. nat/tool/datetime_tools.py +1 -1
  438. nat/tool/document_search.py +1 -1
  439. nat/tool/github_tools.py +1 -1
  440. nat/tool/memory_tools/add_memory_tool.py +1 -1
  441. nat/tool/memory_tools/delete_memory_tool.py +1 -1
  442. nat/tool/memory_tools/get_memory_tool.py +1 -1
  443. nat/tool/nvidia_rag.py +2 -2
  444. nat/tool/register.py +1 -1
  445. nat/tool/retriever.py +1 -1
  446. nat/tool/server_tools.py +1 -1
  447. nat/utils/__init__.py +8 -5
  448. nat/utils/callable_utils.py +1 -1
  449. nat/utils/data_models/schema_validator.py +1 -1
  450. nat/utils/debugging_utils.py +1 -1
  451. nat/utils/decorators.py +1 -1
  452. nat/utils/dump_distro_mapping.py +1 -1
  453. nat/utils/exception_handlers/automatic_retries.py +3 -3
  454. nat/utils/exception_handlers/schemas.py +1 -1
  455. nat/utils/io/model_processing.py +1 -1
  456. nat/utils/io/supress_logs.py +33 -0
  457. nat/utils/io/yaml_tools.py +1 -1
  458. nat/utils/log_levels.py +1 -1
  459. nat/utils/log_utils.py +13 -1
  460. nat/utils/metadata_utils.py +1 -1
  461. nat/utils/optional_imports.py +1 -1
  462. nat/utils/producer_consumer_queue.py +1 -1
  463. nat/utils/reactive/base/observable_base.py +1 -1
  464. nat/utils/reactive/base/observer_base.py +1 -1
  465. nat/utils/reactive/base/subject_base.py +1 -1
  466. nat/utils/reactive/observable.py +1 -1
  467. nat/utils/reactive/observer.py +1 -1
  468. nat/utils/reactive/subject.py +1 -1
  469. nat/utils/reactive/subscription.py +1 -1
  470. nat/utils/responses_api.py +1 -1
  471. nat/utils/settings/global_settings.py +1 -1
  472. nat/utils/string_utils.py +1 -1
  473. nat/utils/type_converter.py +18 -5
  474. nat/utils/type_utils.py +1 -1
  475. nat/utils/url_utils.py +1 -1
  476. {nvidia_nat-1.4.0a20251112.dist-info → nvidia_nat-1.4.0a20260113.dist-info}/METADATA +46 -15
  477. nvidia_nat-1.4.0a20260113.dist-info/RECORD +547 -0
  478. nvidia_nat-1.4.0a20260113.dist-info/entry_points.txt +38 -0
  479. nat/cli/commands/mcp/mcp.py +0 -986
  480. nat/front_ends/mcp/introspection_token_verifier.py +0 -73
  481. nat/front_ends/mcp/mcp_front_end_config.py +0 -109
  482. nat/front_ends/mcp/mcp_front_end_plugin.py +0 -151
  483. nat/front_ends/mcp/mcp_front_end_plugin_worker.py +0 -362
  484. nat/front_ends/mcp/memory_profiler.py +0 -320
  485. nat/front_ends/mcp/register.py +0 -27
  486. nat/front_ends/mcp/tool_converter.py +0 -321
  487. nvidia_nat-1.4.0a20251112.dist-info/RECORD +0 -481
  488. nvidia_nat-1.4.0a20251112.dist-info/entry_points.txt +0 -22
  489. {nvidia_nat-1.4.0a20251112.dist-info → nvidia_nat-1.4.0a20260113.dist-info}/WHEEL +0 -0
  490. {nvidia_nat-1.4.0a20251112.dist-info → nvidia_nat-1.4.0a20260113.dist-info}/licenses/LICENSE-3rd-party.txt +0 -0
  491. {nvidia_nat-1.4.0a20251112.dist-info → nvidia_nat-1.4.0a20260113.dist-info}/licenses/LICENSE.md +0 -0
  492. {nvidia_nat-1.4.0a20251112.dist-info → nvidia_nat-1.4.0a20260113.dist-info}/top_level.txt +0 -0
@@ -1,362 +0,0 @@
1
- # SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
- # SPDX-License-Identifier: Apache-2.0
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
-
16
- import logging
17
- from abc import ABC
18
- from abc import abstractmethod
19
- from collections.abc import Mapping
20
- from typing import Any
21
-
22
- from mcp.server.fastmcp import FastMCP
23
- from starlette.exceptions import HTTPException
24
- from starlette.requests import Request
25
-
26
- from nat.builder.function import Function
27
- from nat.builder.function_base import FunctionBase
28
- from nat.builder.workflow import Workflow
29
- from nat.builder.workflow_builder import WorkflowBuilder
30
- from nat.data_models.config import Config
31
- from nat.front_ends.mcp.mcp_front_end_config import MCPFrontEndConfig
32
- from nat.front_ends.mcp.memory_profiler import MemoryProfiler
33
-
34
- logger = logging.getLogger(__name__)
35
-
36
-
37
- class MCPFrontEndPluginWorkerBase(ABC):
38
- """Base class for MCP front end plugin workers.
39
-
40
- This abstract base class provides shared utilities and defines the contract
41
- for MCP worker implementations. Most users should inherit from
42
- MCPFrontEndPluginWorker instead of this class directly.
43
- """
44
-
45
- def __init__(self, config: Config):
46
- """Initialize the MCP worker with configuration.
47
-
48
- Args:
49
- config: The full NAT configuration
50
- """
51
- self.full_config = config
52
- self.front_end_config: MCPFrontEndConfig = config.general.front_end
53
-
54
- # Initialize memory profiler if enabled
55
- self.memory_profiler = MemoryProfiler(enabled=self.front_end_config.enable_memory_profiling,
56
- log_interval=self.front_end_config.memory_profile_interval,
57
- top_n=self.front_end_config.memory_profile_top_n,
58
- log_level=self.front_end_config.memory_profile_log_level)
59
-
60
- def _setup_health_endpoint(self, mcp: FastMCP):
61
- """Set up the HTTP health endpoint that exercises MCP ping handler."""
62
-
63
- @mcp.custom_route("/health", methods=["GET"])
64
- async def health_check(_request: Request):
65
- """HTTP health check using server's internal ping handler"""
66
- from starlette.responses import JSONResponse
67
-
68
- try:
69
- from mcp.types import PingRequest
70
-
71
- # Create a ping request
72
- ping_request = PingRequest(method="ping")
73
-
74
- # Call the ping handler directly (same one that responds to MCP pings)
75
- await mcp._mcp_server.request_handlers[PingRequest](ping_request)
76
-
77
- return JSONResponse({
78
- "status": "healthy",
79
- "error": None,
80
- "server_name": mcp.name,
81
- })
82
-
83
- except Exception as e:
84
- return JSONResponse({
85
- "status": "unhealthy",
86
- "error": str(e),
87
- "server_name": mcp.name,
88
- },
89
- status_code=503)
90
-
91
- @abstractmethod
92
- async def create_mcp_server(self) -> FastMCP:
93
- """Create and configure the MCP server instance.
94
-
95
- This is the main extension point. Plugins can return FastMCP or any subclass
96
- to customize server behavior (for example, add authentication, custom transports).
97
-
98
- Returns:
99
- FastMCP instance or a subclass with custom behavior
100
- """
101
- ...
102
-
103
- @abstractmethod
104
- async def add_routes(self, mcp: FastMCP, builder: WorkflowBuilder):
105
- """Add routes to the MCP server.
106
-
107
- Plugins must implement this method. Most plugins can call
108
- _default_add_routes() for standard behavior and then add
109
- custom enhancements.
110
-
111
- Args:
112
- mcp: The FastMCP server instance
113
- builder: The workflow builder instance
114
- """
115
- ...
116
-
117
- async def _default_add_routes(self, mcp: FastMCP, builder: WorkflowBuilder):
118
- """Default route registration logic - reusable by subclasses.
119
-
120
- This is a protected helper method that plugins can call to get
121
- standard route registration behavior. Plugins typically call this
122
- from their add_routes() implementation and then add custom features.
123
-
124
- This method:
125
- - Sets up the health endpoint
126
- - Builds the workflow and extracts all functions
127
- - Filters functions based on tool_names config
128
- - Registers each function as an MCP tool
129
- - Sets up debug endpoints for tool introspection
130
-
131
- Args:
132
- mcp: The FastMCP server instance
133
- builder: The workflow builder instance
134
- """
135
- from nat.front_ends.mcp.tool_converter import register_function_with_mcp
136
-
137
- # Set up the health endpoint
138
- self._setup_health_endpoint(mcp)
139
-
140
- # Build the workflow and register all functions with MCP
141
- workflow = await builder.build()
142
-
143
- # Get all functions from the workflow
144
- functions = await self._get_all_functions(workflow)
145
-
146
- # Filter functions based on tool_names if provided
147
- if self.front_end_config.tool_names:
148
- logger.info("Filtering functions based on tool_names: %s", self.front_end_config.tool_names)
149
- filtered_functions: dict[str, Function] = {}
150
- for function_name, function in functions.items():
151
- if function_name in self.front_end_config.tool_names:
152
- # Treat current tool_names as function names, so check if the function name is in the list
153
- filtered_functions[function_name] = function
154
- elif any(function_name.startswith(f"{group_name}.") for group_name in self.front_end_config.tool_names):
155
- # Treat tool_names as function group names, so check if the function name starts with the group name
156
- filtered_functions[function_name] = function
157
- else:
158
- logger.debug("Skipping function %s as it's not in tool_names", function_name)
159
- functions = filtered_functions
160
-
161
- # Register each function with MCP, passing workflow context for observability
162
- for function_name, function in functions.items():
163
- register_function_with_mcp(mcp, function_name, function, workflow, self.memory_profiler)
164
-
165
- # Add a simple fallback function if no functions were found
166
- if not functions:
167
- raise RuntimeError("No functions found in workflow. Please check your configuration.")
168
-
169
- # After registration, expose debug endpoints for tool/schema inspection
170
- self._setup_debug_endpoints(mcp, functions)
171
-
172
- async def _get_all_functions(self, workflow: Workflow) -> dict[str, Function]:
173
- """Get all functions from the workflow.
174
-
175
- Args:
176
- workflow: The NAT workflow.
177
-
178
- Returns:
179
- Dict mapping function names to Function objects.
180
- """
181
- functions: dict[str, Function] = {}
182
-
183
- # Extract all functions from the workflow
184
- functions.update(workflow.functions)
185
- for function_group in workflow.function_groups.values():
186
- functions.update(await function_group.get_accessible_functions())
187
-
188
- if workflow.config.workflow.workflow_alias:
189
- functions[workflow.config.workflow.workflow_alias] = workflow
190
- else:
191
- functions[workflow.config.workflow.type] = workflow
192
-
193
- return functions
194
-
195
- def _setup_debug_endpoints(self, mcp: FastMCP, functions: Mapping[str, FunctionBase]) -> None:
196
- """Set up HTTP debug endpoints for introspecting tools and schemas.
197
-
198
- Exposes:
199
- - GET /debug/tools/list: List tools. Optional query param `name` (one or more, repeatable or comma separated)
200
- selects a subset and returns details for those tools.
201
- - GET /debug/memory/stats: Get current memory profiling statistics (read-only)
202
- """
203
-
204
- @mcp.custom_route("/debug/tools/list", methods=["GET"])
205
- async def list_tools(request: Request):
206
- """HTTP list tools endpoint."""
207
-
208
- from starlette.responses import JSONResponse
209
-
210
- from nat.front_ends.mcp.tool_converter import get_function_description
211
-
212
- # Query params
213
- # Support repeated names and comma-separated lists
214
- names_param_list = set(request.query_params.getlist("name"))
215
- names: list[str] = []
216
- for raw in names_param_list:
217
- # if p.strip() is empty, it won't be included in the list!
218
- parts = [p.strip() for p in raw.split(",") if p.strip()]
219
- names.extend(parts)
220
- detail_raw = request.query_params.get("detail")
221
-
222
- def _parse_detail_param(detail_param: str | None, has_names: bool) -> bool:
223
- if detail_param is None:
224
- if has_names:
225
- return True
226
- return False
227
- v = detail_param.strip().lower()
228
- if v in ("0", "false", "no", "off"):
229
- return False
230
- if v in ("1", "true", "yes", "on"):
231
- return True
232
- # For invalid values, default based on whether names are present
233
- return has_names
234
-
235
- # Helper function to build the input schema info
236
- def _build_schema_info(fn: FunctionBase) -> dict[str, Any] | None:
237
- schema = getattr(fn, "input_schema", None)
238
- if schema is None:
239
- return None
240
-
241
- # check if schema is a ChatRequest
242
- schema_name = getattr(schema, "__name__", "")
243
- schema_qualname = getattr(schema, "__qualname__", "")
244
- if "ChatRequest" in schema_name or "ChatRequest" in schema_qualname:
245
- # Simplified interface used by MCP wrapper for ChatRequest
246
- return {
247
- "type": "object",
248
- "properties": {
249
- "query": {
250
- "type": "string", "description": "User query string"
251
- }
252
- },
253
- "required": ["query"],
254
- "title": "ChatRequestQuery",
255
- }
256
-
257
- # Pydantic models provide model_json_schema
258
- if schema is not None and hasattr(schema, "model_json_schema"):
259
- return schema.model_json_schema()
260
-
261
- return None
262
-
263
- def _build_final_json(functions_to_include: Mapping[str, FunctionBase],
264
- include_schemas: bool = False) -> dict[str, Any]:
265
- tools = []
266
- for name, fn in functions_to_include.items():
267
- list_entry: dict[str, Any] = {
268
- "name": name, "description": get_function_description(fn), "is_workflow": hasattr(fn, "run")
269
- }
270
- if include_schemas:
271
- list_entry["schema"] = _build_schema_info(fn)
272
- tools.append(list_entry)
273
-
274
- return {
275
- "count": len(tools),
276
- "tools": tools,
277
- "server_name": mcp.name,
278
- }
279
-
280
- if names:
281
- # Return selected tools
282
- try:
283
- functions_to_include = {n: functions[n] for n in names}
284
- except KeyError as e:
285
- raise HTTPException(status_code=404, detail=f"Tool \"{e.args[0]}\" not found.") from e
286
- else:
287
- functions_to_include = functions
288
-
289
- # Default for listing all: detail defaults to False unless explicitly set true
290
- return JSONResponse(
291
- _build_final_json(functions_to_include, _parse_detail_param(detail_raw, has_names=bool(names))))
292
-
293
- # Memory profiling endpoint (read-only)
294
- @mcp.custom_route("/debug/memory/stats", methods=["GET"])
295
- async def get_memory_stats(_request: Request):
296
- """Get current memory profiling statistics."""
297
- from starlette.responses import JSONResponse
298
-
299
- stats = self.memory_profiler.get_stats()
300
- return JSONResponse(stats)
301
-
302
-
303
- class MCPFrontEndPluginWorker(MCPFrontEndPluginWorkerBase):
304
- """Default MCP server worker implementation.
305
-
306
- Inherit from this class to create custom MCP workers that extend or modify
307
- server behavior. Override create_mcp_server() to use a different server type,
308
- and override add_routes() to add custom functionality.
309
-
310
- Example:
311
- class CustomWorker(MCPFrontEndPluginWorker):
312
- async def create_mcp_server(self):
313
- # Return custom MCP server instance
314
- return MyCustomFastMCP(...)
315
-
316
- async def add_routes(self, mcp, builder):
317
- # Get default routes
318
- await super().add_routes(mcp, builder)
319
- # Add custom features
320
- self._add_my_custom_features(mcp)
321
- """
322
-
323
- async def create_mcp_server(self) -> FastMCP:
324
- """Create default MCP server with optional authentication.
325
-
326
- Returns:
327
- FastMCP instance configured with settings from NAT config
328
- """
329
- # Handle auth if configured
330
- auth_settings = None
331
- token_verifier = None
332
-
333
- if self.front_end_config.server_auth:
334
- from mcp.server.auth.settings import AuthSettings
335
- from pydantic import AnyHttpUrl
336
-
337
- server_url = f"http://{self.front_end_config.host}:{self.front_end_config.port}"
338
- auth_settings = AuthSettings(issuer_url=AnyHttpUrl(self.front_end_config.server_auth.issuer_url),
339
- required_scopes=self.front_end_config.server_auth.scopes,
340
- resource_server_url=AnyHttpUrl(server_url))
341
-
342
- # Create token verifier
343
- from nat.front_ends.mcp.introspection_token_verifier import IntrospectionTokenVerifier
344
-
345
- token_verifier = IntrospectionTokenVerifier(self.front_end_config.server_auth)
346
-
347
- return FastMCP(name=self.front_end_config.name,
348
- host=self.front_end_config.host,
349
- port=self.front_end_config.port,
350
- debug=self.front_end_config.debug,
351
- auth=auth_settings,
352
- token_verifier=token_verifier)
353
-
354
- async def add_routes(self, mcp: FastMCP, builder: WorkflowBuilder):
355
- """Add default routes to the MCP server.
356
-
357
- Args:
358
- mcp: The FastMCP server instance
359
- builder: The workflow builder instance
360
- """
361
- # Use the default implementation from base class to add the tools to the MCP server
362
- await self._default_add_routes(mcp, builder)
@@ -1,320 +0,0 @@
1
- # SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
- # SPDX-License-Identifier: Apache-2.0
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- """Memory profiling utilities for MCP frontend."""
16
-
17
- import gc
18
- import logging
19
- import tracemalloc
20
- from typing import Any
21
-
22
- logger = logging.getLogger(__name__)
23
-
24
-
25
- class MemoryProfiler:
26
- """Memory profiler for tracking memory usage and potential leaks."""
27
-
28
- def __init__(self, enabled: bool = False, log_interval: int = 50, top_n: int = 10, log_level: str = "DEBUG"):
29
- """Initialize the memory profiler.
30
-
31
- Args:
32
- enabled: Whether memory profiling is enabled
33
- log_interval: Log stats every N requests
34
- top_n: Number of top allocations to log
35
- log_level: Log level for memory profiling output (e.g., "DEBUG", "INFO")
36
- """
37
- self.enabled = enabled
38
- # normalize interval to avoid modulo-by-zero
39
- self.log_interval = max(1, int(log_interval))
40
- self.top_n = top_n
41
- self.log_level = getattr(logging, log_level.upper(), logging.DEBUG)
42
- self.request_count = 0
43
- self.baseline_snapshot = None
44
-
45
- # Track whether this instance started tracemalloc (to avoid resetting external tracing)
46
- self._we_started_tracemalloc = False
47
-
48
- if self.enabled:
49
- logger.info("Memory profiling ENABLED (interval=%d, top_n=%d, log_level=%s)",
50
- self.log_interval,
51
- top_n,
52
- log_level)
53
- try:
54
- if not tracemalloc.is_tracing():
55
- tracemalloc.start()
56
- self._we_started_tracemalloc = True
57
- # Take baseline snapshot
58
- gc.collect()
59
- self.baseline_snapshot = tracemalloc.take_snapshot()
60
- except RuntimeError as e:
61
- logger.warning("tracemalloc unavailable or not tracing: %s", e)
62
- else:
63
- logger.info("Memory profiling DISABLED")
64
-
65
- def _log(self, message: str, *args: Any) -> None:
66
- """Log a message at the configured log level.
67
-
68
- Args:
69
- message: Log message format string
70
- args: Arguments for the format string
71
- """
72
- logger.log(self.log_level, message, *args)
73
-
74
- def on_request_complete(self) -> None:
75
- """Called after each request completes."""
76
- if not self.enabled:
77
- return
78
- self.request_count += 1
79
- if self.request_count % self.log_interval == 0:
80
- self.log_memory_stats()
81
-
82
- def _ensure_tracing(self) -> bool:
83
- """Ensure tracemalloc is running if we started it originally.
84
-
85
- Returns:
86
- True if tracemalloc is active, False otherwise
87
- """
88
- if tracemalloc.is_tracing():
89
- return True
90
-
91
- # Only restart if we started it originally (respect external control)
92
- if not self._we_started_tracemalloc:
93
- return False
94
-
95
- # Attempt to restart
96
- try:
97
- logger.warning("tracemalloc was stopped externally; restarting (we started it originally)")
98
- tracemalloc.start()
99
-
100
- # Reset baseline since old tracking data is lost
101
- gc.collect()
102
- self.baseline_snapshot = tracemalloc.take_snapshot()
103
- logger.info("Baseline snapshot reset after tracemalloc restart")
104
-
105
- return True
106
- except RuntimeError as e:
107
- logger.error("Failed to restart tracemalloc: %s", e)
108
- return False
109
-
110
- def _safe_traced_memory(self) -> tuple[float, float] | None:
111
- """Return (current, peak usage in MB) if tracemalloc is active, else None."""
112
- if not self._ensure_tracing():
113
- return None
114
-
115
- try:
116
- current, peak = tracemalloc.get_traced_memory()
117
- megabyte = (1 << 20)
118
- return (current / megabyte, peak / megabyte)
119
- except RuntimeError:
120
- return None
121
-
122
- def _safe_snapshot(self) -> tracemalloc.Snapshot | None:
123
- """Return a tracemalloc Snapshot if available, else None."""
124
- if not self._ensure_tracing():
125
- return None
126
-
127
- try:
128
- return tracemalloc.take_snapshot()
129
- except RuntimeError:
130
- return None
131
-
132
- def log_memory_stats(self) -> dict[str, Any]:
133
- """Log current memory statistics and return them."""
134
- if not self.enabled:
135
- return {}
136
-
137
- # Force garbage collection first
138
- gc.collect()
139
-
140
- # Get current memory usage
141
- mem = self._safe_traced_memory()
142
- if mem is None:
143
- logger.info("tracemalloc is not active; cannot collect memory stats.")
144
- # still return structural fields
145
- stats = {
146
- "request_count": self.request_count,
147
- "current_memory_mb": None,
148
- "peak_memory_mb": None,
149
- "active_intermediate_managers": self._safe_intermediate_step_manager_count(),
150
- "outstanding_steps": self._safe_outstanding_step_count(),
151
- "active_exporters": self._safe_exporter_count(),
152
- "isolated_exporters": self._safe_isolated_exporter_count(),
153
- "subject_instances": self._count_instances_of_type("Subject"),
154
- }
155
- return stats
156
-
157
- current_mb, peak_mb = mem
158
-
159
- # Take snapshot and compare to baseline
160
- snapshot = self._safe_snapshot()
161
-
162
- # Track BaseExporter instances (observability layer)
163
- exporter_count = self._safe_exporter_count()
164
- isolated_exporter_count = self._safe_isolated_exporter_count()
165
-
166
- # Track Subject instances (event streams)
167
- subject_count = self._count_instances_of_type("Subject")
168
-
169
- stats = {
170
- "request_count": self.request_count,
171
- "current_memory_mb": round(current_mb, 2),
172
- "peak_memory_mb": round(peak_mb, 2),
173
- "active_intermediate_managers": self._safe_intermediate_step_manager_count(),
174
- "outstanding_steps": self._safe_outstanding_step_count(),
175
- "active_exporters": exporter_count,
176
- "isolated_exporters": isolated_exporter_count,
177
- "subject_instances": subject_count,
178
- }
179
-
180
- self._log("=" * 80)
181
- self._log("MEMORY PROFILE AFTER %d REQUESTS:", self.request_count)
182
- self._log(" Current Memory: %.2f MB", current_mb)
183
- self._log(" Peak Memory: %.2f MB", peak_mb)
184
- self._log("")
185
- self._log("NAT COMPONENT INSTANCES:")
186
- self._log(" IntermediateStepManagers: %d active (%d outstanding steps)",
187
- stats["active_intermediate_managers"],
188
- stats["outstanding_steps"])
189
- self._log(" BaseExporters: %d active (%d isolated)", stats["active_exporters"], stats["isolated_exporters"])
190
- self._log(" Subject (event streams): %d instances", stats["subject_instances"])
191
-
192
- # Show top allocations
193
- if snapshot is None:
194
- self._log("tracemalloc snapshot unavailable.")
195
- else:
196
- if self.baseline_snapshot:
197
- self._log("TOP %d MEMORY GROWTH SINCE BASELINE:", self.top_n)
198
- top_stats = snapshot.compare_to(self.baseline_snapshot, 'lineno')
199
- else:
200
- self._log("TOP %d MEMORY ALLOCATIONS:", self.top_n)
201
- top_stats = snapshot.statistics('lineno')
202
-
203
- for i, stat in enumerate(top_stats[:self.top_n], 1):
204
- self._log(" #%d: %s", i, stat)
205
-
206
- self._log("=" * 80)
207
-
208
- return stats
209
-
210
- def _count_instances_of_type(self, type_name: str) -> int:
211
- """Count instances of a specific type in memory."""
212
- count = 0
213
- for obj in gc.get_objects():
214
- try:
215
- if type(obj).__name__ == type_name:
216
- count += 1
217
- except Exception:
218
- pass
219
- return count
220
-
221
- def _safe_exporter_count(self) -> int:
222
- try:
223
- from nat.observability.exporter.base_exporter import BaseExporter
224
- return BaseExporter.get_active_instance_count()
225
- except Exception as e:
226
- logger.debug("Could not get BaseExporter stats: %s", e)
227
- return 0
228
-
229
- def _safe_isolated_exporter_count(self) -> int:
230
- try:
231
- from nat.observability.exporter.base_exporter import BaseExporter
232
- return BaseExporter.get_isolated_instance_count()
233
- except Exception:
234
- return 0
235
-
236
- def _safe_intermediate_step_manager_count(self) -> int:
237
- try:
238
- from nat.builder.intermediate_step_manager import IntermediateStepManager
239
- # len() is atomic in CPython, but catch RuntimeError just in case
240
- try:
241
- return IntermediateStepManager.get_active_instance_count()
242
- except RuntimeError:
243
- # Set was modified during len() - very rare
244
- logger.debug("Set changed during count, returning 0")
245
- return 0
246
- except Exception as e:
247
- logger.debug("Could not get IntermediateStepManager stats: %s", e)
248
- return 0
249
-
250
- def _safe_outstanding_step_count(self) -> int:
251
- """Get total outstanding steps across all active IntermediateStepManager instances."""
252
- try:
253
- from nat.builder.intermediate_step_manager import IntermediateStepManager
254
-
255
- # Make a snapshot to avoid "Set changed size during iteration" if GC runs
256
- try:
257
- instances_snapshot = list(IntermediateStepManager._active_instances)
258
- except RuntimeError:
259
- # Set changed during list() call - rare but possible
260
- logger.debug("Set changed during snapshot, returning 0 for outstanding steps")
261
- return 0
262
-
263
- total_outstanding = 0
264
- # Iterate through snapshot safely
265
- for ref in instances_snapshot:
266
- try:
267
- manager = ref()
268
- if manager is not None:
269
- total_outstanding += manager.get_outstanding_step_count()
270
- except (ReferenceError, AttributeError):
271
- # Manager was GC'd or in invalid state - skip it
272
- continue
273
- return total_outstanding
274
- except Exception as e:
275
- logger.debug("Could not get outstanding step count: %s", e)
276
- return 0
277
-
278
- def get_stats(self) -> dict[str, Any]:
279
- """Get current memory statistics without logging."""
280
- if not self.enabled:
281
- return {"enabled": False}
282
-
283
- mem = self._safe_traced_memory()
284
- if mem is None:
285
- return {
286
- "enabled": True,
287
- "request_count": self.request_count,
288
- "current_memory_mb": None,
289
- "peak_memory_mb": None,
290
- "active_intermediate_managers": self._safe_intermediate_step_manager_count(),
291
- "outstanding_steps": self._safe_outstanding_step_count(),
292
- "active_exporters": self._safe_exporter_count(),
293
- "isolated_exporters": self._safe_isolated_exporter_count(),
294
- "subject_instances": self._count_instances_of_type("Subject"),
295
- }
296
-
297
- current_mb, peak_mb = mem
298
- return {
299
- "enabled": True,
300
- "request_count": self.request_count,
301
- "current_memory_mb": round(current_mb, 2),
302
- "peak_memory_mb": round(peak_mb, 2),
303
- "active_intermediate_managers": self._safe_intermediate_step_manager_count(),
304
- "outstanding_steps": self._safe_outstanding_step_count(),
305
- "active_exporters": self._safe_exporter_count(),
306
- "isolated_exporters": self._safe_isolated_exporter_count(),
307
- "subject_instances": self._count_instances_of_type("Subject"),
308
- }
309
-
310
- def reset_baseline(self) -> None:
311
- """Reset the baseline snapshot to current state."""
312
- if not self.enabled:
313
- return
314
- gc.collect()
315
- snap = self._safe_snapshot()
316
- if snap is None:
317
- logger.info("Cannot reset baseline: tracemalloc is not active.")
318
- return
319
- self.baseline_snapshot = snap
320
- logger.info("Memory profiling baseline reset at request %d", self.request_count)