langchain-agentx-python 0.1__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.
- langchain_agentx/__init__.py +46 -0
- langchain_agentx/command/__init__.py +28 -0
- langchain_agentx/command/builtin/__init__.py +25 -0
- langchain_agentx/command/builtin/clear.py +33 -0
- langchain_agentx/command/builtin/compact.py +33 -0
- langchain_agentx/command/builtin/memory.py +37 -0
- langchain_agentx/command/builtin/reload_plugins.py +42 -0
- langchain_agentx/command/context.py +30 -0
- langchain_agentx/command/dispatcher.py +183 -0
- langchain_agentx/command/registry.py +110 -0
- langchain_agentx/command/result.py +25 -0
- langchain_agentx/command/types.py +41 -0
- langchain_agentx/config/__init__.py +14 -0
- langchain_agentx/loop/__init__.py +47 -0
- langchain_agentx/loop/config/__init__.py +20 -0
- langchain_agentx/loop/config/agent_config.py +66 -0
- langchain_agentx/loop/config/agent_loop_config.py +72 -0
- langchain_agentx/loop/config/model_context_resolver.py +105 -0
- langchain_agentx/loop/config/runtime_settings.py +50 -0
- langchain_agentx/loop/config/token_estimator.py +133 -0
- langchain_agentx/loop/context/__init__.py +66 -0
- langchain_agentx/loop/context/blocking_guard.py +97 -0
- langchain_agentx/loop/context/compaction_service.py +60 -0
- langchain_agentx/loop/context/message_utils.py +56 -0
- langchain_agentx/loop/context/pipeline.py +127 -0
- langchain_agentx/loop/context/settings.py +103 -0
- langchain_agentx/loop/context/stages/__init__.py +29 -0
- langchain_agentx/loop/context/stages/autocompact.py +140 -0
- langchain_agentx/loop/context/stages/base.py +32 -0
- langchain_agentx/loop/context/stages/collapse.py +76 -0
- langchain_agentx/loop/context/stages/microcompact.py +76 -0
- langchain_agentx/loop/context/stages/noop.py +33 -0
- langchain_agentx/loop/context/stages/snip.py +71 -0
- langchain_agentx/loop/context/stages/tool_result_budget.py +69 -0
- langchain_agentx/loop/context/types.py +79 -0
- langchain_agentx/loop/exit/__init__.py +1 -0
- langchain_agentx/loop/exit/exit_logic.py +320 -0
- langchain_agentx/loop/exit/reason_codes.py +39 -0
- langchain_agentx/loop/graph/__init__.py +5 -0
- langchain_agentx/loop/graph/builtin_loop_control.py +197 -0
- langchain_agentx/loop/graph/factory.py +1409 -0
- langchain_agentx/loop/graph/graph_edges.py +820 -0
- langchain_agentx/loop/hook/__init__.py +48 -0
- langchain_agentx/loop/hook/async_hook_runner.py +62 -0
- langchain_agentx/loop/hook/config.py +280 -0
- langchain_agentx/loop/hook/engine.py +321 -0
- langchain_agentx/loop/hook/executors/__init__.py +9 -0
- langchain_agentx/loop/hook/executors/agent.py +107 -0
- langchain_agentx/loop/hook/executors/command.py +230 -0
- langchain_agentx/loop/hook/executors/http.py +114 -0
- langchain_agentx/loop/hook/executors/prompt.py +92 -0
- langchain_agentx/loop/hook/graph_wiring.py +134 -0
- langchain_agentx/loop/hook/registry.py +262 -0
- langchain_agentx/loop/hook/trust.py +43 -0
- langchain_agentx/loop/hook/types.py +110 -0
- langchain_agentx/loop/injection/__init__.py +13 -0
- langchain_agentx/loop/injection/dedup.py +74 -0
- langchain_agentx/loop/loop_abort.py +36 -0
- langchain_agentx/loop/model/__init__.py +1 -0
- langchain_agentx/loop/model/model_node.py +648 -0
- langchain_agentx/loop/model/model_nodes.py +661 -0
- langchain_agentx/loop/model/orphan_tool_results.py +38 -0
- langchain_agentx/loop/model/retrier.py +307 -0
- langchain_agentx/loop/model/retry_bridge.py +58 -0
- langchain_agentx/loop/model/retry_events.py +35 -0
- langchain_agentx/loop/model/retry_policy.py +56 -0
- langchain_agentx/loop/model/schema_and_format.py +153 -0
- langchain_agentx/loop/model/tool_and_model_binding.py +227 -0
- langchain_agentx/loop/model/tool_call_degradation_corrector.py +443 -0
- langchain_agentx/loop/model/tool_transcript_guard.py +225 -0
- langchain_agentx/loop/prompt/__init__.py +95 -0
- langchain_agentx/loop/prompt/builder.py +61 -0
- langchain_agentx/loop/prompt/builtin.py +218 -0
- langchain_agentx/loop/prompt/compact.py +408 -0
- langchain_agentx/loop/prompt/sections.py +120 -0
- langchain_agentx/loop/runtime/__init__.py +19 -0
- langchain_agentx/loop/runtime/context.py +34 -0
- langchain_agentx/loop/runtime/context_factory.py +107 -0
- langchain_agentx/loop/runtime/subagent_execution_paths.py +68 -0
- langchain_agentx/loop/subagent/__init__.py +53 -0
- langchain_agentx/loop/subagent/async_runner.py +215 -0
- langchain_agentx/loop/subagent/context.py +209 -0
- langchain_agentx/loop/subagent/fork_worktree_notice.py +25 -0
- langchain_agentx/loop/subagent/graph.py +72 -0
- langchain_agentx/loop/subagent/orchestrator.py +391 -0
- langchain_agentx/loop/subagent/progress.py +30 -0
- langchain_agentx/loop/subagent/prompt.py +52 -0
- langchain_agentx/loop/subagent/runner.py +504 -0
- langchain_agentx/loop/subagent/transcript.py +172 -0
- langchain_agentx/memory/__init__.py +2 -0
- langchain_agentx/memory/instruction/__init__.py +12 -0
- langchain_agentx/memory/instruction/loader.py +325 -0
- langchain_agentx/memory/instruction/resolver.py +24 -0
- langchain_agentx/memory/instruction/runtime.py +83 -0
- langchain_agentx/memory/instruction/sections.py +83 -0
- langchain_agentx/memory/instruction/types.py +59 -0
- langchain_agentx/memory/memdir/__init__.py +77 -0
- langchain_agentx/memory/memdir/age.py +36 -0
- langchain_agentx/memory/memdir/agent_memory.py +380 -0
- langchain_agentx/memory/memdir/extractor.py +309 -0
- langchain_agentx/memory/memdir/loader.py +187 -0
- langchain_agentx/memory/memdir/paths.py +63 -0
- langchain_agentx/memory/memdir/recall.py +45 -0
- langchain_agentx/memory/memdir/runtime.py +43 -0
- langchain_agentx/memory/memdir/scan.py +135 -0
- langchain_agentx/memory/memdir/types.py +104 -0
- langchain_agentx/memory/session/__init__.py +76 -0
- langchain_agentx/memory/session/compact_bridge.py +208 -0
- langchain_agentx/memory/session/prompts.py +172 -0
- langchain_agentx/memory/session/session_memory.py +282 -0
- langchain_agentx/observability/__init__.py +67 -0
- langchain_agentx/observability/evaluation/__init__.py +17 -0
- langchain_agentx/observability/evaluation/checkers/__init__.py +18 -0
- langchain_agentx/observability/evaluation/checkers/base.py +34 -0
- langchain_agentx/observability/evaluation/checkers/compaction.py +38 -0
- langchain_agentx/observability/evaluation/checkers/degradation.py +50 -0
- langchain_agentx/observability/evaluation/checkers/exit_quality.py +42 -0
- langchain_agentx/observability/evaluation/checkers/session_memory.py +45 -0
- langchain_agentx/observability/evaluation/checkers/tool_behavior.py +53 -0
- langchain_agentx/observability/evaluation/retention_scheduler.py +67 -0
- langchain_agentx/observability/evaluation/service.py +102 -0
- langchain_agentx/observability/evaluation/state.py +32 -0
- langchain_agentx/observability/evaluation/store.py +258 -0
- langchain_agentx/observability/events/__init__.py +15 -0
- langchain_agentx/observability/events/langchain_agentx_event_adapter.py +832 -0
- langchain_agentx/observability/logging/__init__.py +15 -0
- langchain_agentx/observability/logging/debug_burst.py +95 -0
- langchain_agentx/observability/logging/logging_config.py +178 -0
- langchain_agentx/observability/logging/logging_contract.py +65 -0
- langchain_agentx/observability/replay/__init__.py +35 -0
- langchain_agentx/observability/replay/cli.py +91 -0
- langchain_agentx/observability/replay/service.py +83 -0
- langchain_agentx/observability/replay/store.py +278 -0
- langchain_agentx/observability/replay/ui.py +47 -0
- langchain_agentx/observability/trace/__init__.py +25 -0
- langchain_agentx/observability/trace/collector.py +560 -0
- langchain_agentx/observability/trace/event_emitter.py +183 -0
- langchain_agentx/observability/trace/hook_event_emitter.py +49 -0
- langchain_agentx/observability/trace/models.py +144 -0
- langchain_agentx/observability/trace/sqlite_store.py +873 -0
- langchain_agentx/observability/trace/trace_callback.py +295 -0
- langchain_agentx/observability/trace/trace_lifecycle_collector.py +114 -0
- langchain_agentx/plugin/__init__.py +26 -0
- langchain_agentx/plugin/builtin.py +53 -0
- langchain_agentx/plugin/config.py +113 -0
- langchain_agentx/plugin/loader.py +386 -0
- langchain_agentx/plugin/manifest.py +154 -0
- langchain_agentx/plugin/registries.py +211 -0
- langchain_agentx/plugin/types.py +142 -0
- langchain_agentx/provider/__init__.py +27 -0
- langchain_agentx/provider/anthropic.py +121 -0
- langchain_agentx/provider/compatible_chat_openai.py +86 -0
- langchain_agentx/provider/env.py +45 -0
- langchain_agentx/provider/model_profile.py +156 -0
- langchain_agentx/provider/openai.py +89 -0
- langchain_agentx/session/__init__.py +17 -0
- langchain_agentx/session/agent_session.py +320 -0
- langchain_agentx/session/conversation_factory.py +87 -0
- langchain_agentx/session/conversation_recovery.py +156 -0
- langchain_agentx/session/conversation_session.py +198 -0
- langchain_agentx/session/factory.py +143 -0
- langchain_agentx/session/protocol.py +25 -0
- langchain_agentx/task_runtime/__init__.py +113 -0
- langchain_agentx/task_runtime/core/__init__.py +51 -0
- langchain_agentx/task_runtime/core/ids.py +33 -0
- langchain_agentx/task_runtime/core/interfaces.py +115 -0
- langchain_agentx/task_runtime/core/notification_priority.py +19 -0
- langchain_agentx/task_runtime/core/types.py +136 -0
- langchain_agentx/task_runtime/integrations/__init__.py +33 -0
- langchain_agentx/task_runtime/integrations/loop_adapter.py +91 -0
- langchain_agentx/task_runtime/integrations/loop_integration.py +61 -0
- langchain_agentx/task_runtime/integrations/prefetch_providers.py +108 -0
- langchain_agentx/task_runtime/integrations/provider_factory.py +103 -0
- langchain_agentx/task_runtime/integrations/queued_command_provider.py +184 -0
- langchain_agentx/task_runtime/integrations/sqlite_queued_command_provider.py +338 -0
- langchain_agentx/task_runtime/integrations/tool_use_summary_provider.py +254 -0
- langchain_agentx/task_runtime/orchestrator/__init__.py +5 -0
- langchain_agentx/task_runtime/orchestrator/runtime.py +386 -0
- langchain_agentx/task_runtime/output/__init__.py +5 -0
- langchain_agentx/task_runtime/output/sink.py +64 -0
- langchain_agentx/task_runtime/policy/__init__.py +11 -0
- langchain_agentx/task_runtime/policy/withhold_visibility.py +32 -0
- langchain_agentx/task_runtime/queue/__init__.py +5 -0
- langchain_agentx/task_runtime/queue/in_memory.py +55 -0
- langchain_agentx/task_runtime/skill_prefetch/__init__.py +4 -0
- langchain_agentx/task_runtime/skill_prefetch/attachments.py +46 -0
- langchain_agentx/task_runtime/skill_prefetch/models.py +37 -0
- langchain_agentx/task_runtime/skill_prefetch/provider.py +344 -0
- langchain_agentx/task_runtime/store/__init__.py +6 -0
- langchain_agentx/task_runtime/store/in_memory.py +81 -0
- langchain_agentx/task_runtime/store/sqlite_store.py +281 -0
- langchain_agentx/task_runtime/tasks/__init__.py +76 -0
- langchain_agentx/task_runtime/tasks/ai_analysis/__init__.py +15 -0
- langchain_agentx/task_runtime/tasks/ai_analysis/base.py +41 -0
- langchain_agentx/task_runtime/tasks/ai_analysis/evaluation.py +67 -0
- langchain_agentx/task_runtime/tasks/ai_analysis/registry.py +36 -0
- langchain_agentx/task_runtime/tasks/ai_analysis/scheduler.py +70 -0
- langchain_agentx/task_runtime/tasks/base/__init__.py +6 -0
- langchain_agentx/task_runtime/tasks/base/contracts.py +24 -0
- langchain_agentx/task_runtime/tasks/custom/__init__.py +7 -0
- langchain_agentx/task_runtime/tasks/custom/executor.py +60 -0
- langchain_agentx/task_runtime/tasks/custom/notification.py +7 -0
- langchain_agentx/task_runtime/tasks/custom/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/custom/spec.py +33 -0
- langchain_agentx/task_runtime/tasks/dream_task/__init__.py +15 -0
- langchain_agentx/task_runtime/tasks/dream_task/executor.py +61 -0
- langchain_agentx/task_runtime/tasks/dream_task/notification.py +19 -0
- langchain_agentx/task_runtime/tasks/dream_task/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/dream_task/spec.py +35 -0
- langchain_agentx/task_runtime/tasks/dream_task/state.py +17 -0
- langchain_agentx/task_runtime/tasks/in_process_teammate/__init__.py +12 -0
- langchain_agentx/task_runtime/tasks/in_process_teammate/executor.py +36 -0
- langchain_agentx/task_runtime/tasks/in_process_teammate/notification.py +25 -0
- langchain_agentx/task_runtime/tasks/in_process_teammate/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/in_process_teammate/spec.py +63 -0
- langchain_agentx/task_runtime/tasks/local_agent/__init__.py +14 -0
- langchain_agentx/task_runtime/tasks/local_agent/executor.py +33 -0
- langchain_agentx/task_runtime/tasks/local_agent/notification.py +21 -0
- langchain_agentx/task_runtime/tasks/local_agent/runner.py +43 -0
- langchain_agentx/task_runtime/tasks/local_agent/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/local_agent/spec.py +31 -0
- langchain_agentx/task_runtime/tasks/local_bash/__init__.py +13 -0
- langchain_agentx/task_runtime/tasks/local_bash/executor.py +95 -0
- langchain_agentx/task_runtime/tasks/local_bash/notification.py +22 -0
- langchain_agentx/task_runtime/tasks/local_bash/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/local_bash/spec.py +55 -0
- langchain_agentx/task_runtime/tasks/remote_agent/__init__.py +19 -0
- langchain_agentx/task_runtime/tasks/remote_agent/backend.py +76 -0
- langchain_agentx/task_runtime/tasks/remote_agent/executor.py +37 -0
- langchain_agentx/task_runtime/tasks/remote_agent/notification.py +22 -0
- langchain_agentx/task_runtime/tasks/remote_agent/semantics.py +13 -0
- langchain_agentx/task_runtime/tasks/remote_agent/spec.py +34 -0
- langchain_agentx/task_runtime/tasks/trace_cleanup/__init__.py +19 -0
- langchain_agentx/task_runtime/tasks/trace_cleanup/bootstrap.py +95 -0
- langchain_agentx/task_runtime/tasks/trace_cleanup/executor.py +66 -0
- langchain_agentx/task_runtime/tasks/trace_cleanup/scheduler.py +169 -0
- langchain_agentx/tool_runtime/__init__.py +90 -0
- langchain_agentx/tool_runtime/adapter.py +365 -0
- langchain_agentx/tool_runtime/base.py +319 -0
- langchain_agentx/tool_runtime/errors.py +190 -0
- langchain_agentx/tool_runtime/identical_call_cache.py +110 -0
- langchain_agentx/tool_runtime/loader.py +195 -0
- langchain_agentx/tool_runtime/models.py +260 -0
- langchain_agentx/tool_runtime/permission_context.py +78 -0
- langchain_agentx/tool_runtime/pipeline.py +621 -0
- langchain_agentx/tool_runtime/policy.py +447 -0
- langchain_agentx/tool_runtime/registry.py +81 -0
- langchain_agentx/tool_runtime/resolvers/__init__.py +27 -0
- langchain_agentx/tool_runtime/resolvers/agent_session.py +125 -0
- langchain_agentx/tool_runtime/resolvers/background.py +32 -0
- langchain_agentx/tool_runtime/resolvers/base.py +20 -0
- langchain_agentx/tool_runtime/resolvers/conversation.py +22 -0
- langchain_agentx/tool_runtime/resolvers/workflow.py +73 -0
- langchain_agentx/tool_runtime/session_store.py +132 -0
- langchain_agentx/tool_runtime/smoke_test_runtime.py +294 -0
- langchain_agentx/tool_runtime/state_bridge.py +164 -0
- langchain_agentx/tools/__init__.py +26 -0
- langchain_agentx/tools/agent/__init__.py +9 -0
- langchain_agentx/tools/agent/backend.py +53 -0
- langchain_agentx/tools/agent/built_in/__init__.py +19 -0
- langchain_agentx/tools/agent/built_in/agentx_guide.py +65 -0
- langchain_agentx/tools/agent/built_in/explore.py +80 -0
- langchain_agentx/tools/agent/built_in/general.py +57 -0
- langchain_agentx/tools/agent/built_in/plan.py +89 -0
- langchain_agentx/tools/agent/built_in/statusline_setup.py +64 -0
- langchain_agentx/tools/agent/built_in/verification.py +120 -0
- langchain_agentx/tools/agent/builtin_subagent_loader.py +89 -0
- langchain_agentx/tools/agent/cwd_resolution.py +119 -0
- langchain_agentx/tools/agent/limits.py +26 -0
- langchain_agentx/tools/agent/loader.py +270 -0
- langchain_agentx/tools/agent/models.py +85 -0
- langchain_agentx/tools/agent/prompt.py +120 -0
- langchain_agentx/tools/agent/registry/__init__.py +18 -0
- langchain_agentx/tools/agent/registry/config.py +29 -0
- langchain_agentx/tools/agent/registry/registry.py +47 -0
- langchain_agentx/tools/agent/scope.py +137 -0
- langchain_agentx/tools/agent/tool.py +256 -0
- langchain_agentx/tools/bash/__init__.py +9 -0
- langchain_agentx/tools/bash/ast_security.py +571 -0
- langchain_agentx/tools/bash/backend.py +1447 -0
- langchain_agentx/tools/bash/bash_hardening.py +734 -0
- langchain_agentx/tools/bash/bash_runtime_contract.py +41 -0
- langchain_agentx/tools/bash/cwd_reporter.py +95 -0
- langchain_agentx/tools/bash/limits.py +71 -0
- langchain_agentx/tools/bash/mode_validation.py +282 -0
- langchain_agentx/tools/bash/models.py +131 -0
- langchain_agentx/tools/bash/observability.py +148 -0
- langchain_agentx/tools/bash/output_utils.py +200 -0
- langchain_agentx/tools/bash/path_security.py +2429 -0
- langchain_agentx/tools/bash/prompt.py +68 -0
- langchain_agentx/tools/bash/read_only_validation.py +589 -0
- langchain_agentx/tools/bash/result_presenter.py +324 -0
- langchain_agentx/tools/bash/sandbox_decision.py +133 -0
- langchain_agentx/tools/bash/security.py +311 -0
- langchain_agentx/tools/bash/sed_edit_parser.py +243 -0
- langchain_agentx/tools/bash/sed_validation.py +163 -0
- langchain_agentx/tools/bash/semantics.py +111 -0
- langchain_agentx/tools/bash/session_manager.py +205 -0
- langchain_agentx/tools/bash/session_runtime.py +290 -0
- langchain_agentx/tools/bash/shell_locator.py +191 -0
- langchain_agentx/tools/bash/task_runtime.py +91 -0
- langchain_agentx/tools/bash/tool.py +939 -0
- langchain_agentx/tools/bash/windows_shell_quoting.py +45 -0
- langchain_agentx/tools/glob/__init__.py +9 -0
- langchain_agentx/tools/glob/models.py +57 -0
- langchain_agentx/tools/glob/pagination.py +30 -0
- langchain_agentx/tools/glob/prompt.py +24 -0
- langchain_agentx/tools/glob/rg_list_backend.py +139 -0
- langchain_agentx/tools/glob/rg_pattern.py +44 -0
- langchain_agentx/tools/glob/tool.py +327 -0
- langchain_agentx/tools/grep/__init__.py +7 -0
- langchain_agentx/tools/grep/backend.py +375 -0
- langchain_agentx/tools/grep/models.py +127 -0
- langchain_agentx/tools/grep/prompt.py +30 -0
- langchain_agentx/tools/grep/rg_subprocess_controller.py +114 -0
- langchain_agentx/tools/grep/tool.py +475 -0
- langchain_agentx/tools/read/__init__.py +9 -0
- langchain_agentx/tools/read/backend.py +415 -0
- langchain_agentx/tools/read/limits.py +67 -0
- langchain_agentx/tools/read/models.py +156 -0
- langchain_agentx/tools/read/prompt.py +73 -0
- langchain_agentx/tools/read/tool.py +494 -0
- langchain_agentx/tools/ripgrep_plugin_exclusions.py +137 -0
- langchain_agentx/tools/skill/__init__.py +4 -0
- langchain_agentx/tools/skill/argument_substitution.py +80 -0
- langchain_agentx/tools/skill/loader.py +196 -0
- langchain_agentx/tools/skill/models.py +88 -0
- langchain_agentx/tools/skill/policy.py +80 -0
- langchain_agentx/tools/skill/prompt.py +35 -0
- langchain_agentx/tools/skill/tool.py +222 -0
- langchain_agentx/utils/__init__.py +0 -0
- langchain_agentx/utils/cwd.py +124 -0
- langchain_agentx/utils/host_platform.py +112 -0
- langchain_agentx/utils/path_hierarchy.py +48 -0
- langchain_agentx/utils/path_user_input.py +66 -0
- langchain_agentx/utils/rg_executable.py +18 -0
- langchain_agentx/utils/subprocess_text.py +101 -0
- langchain_agentx/utils/temp_paths.py +77 -0
- langchain_agentx/utils/unc_path.py +25 -0
- langchain_agentx/utils/win_reserved_paths.py +51 -0
- langchain_agentx/workflow/__init__.py +7 -0
- langchain_agentx/workflow/base.py +97 -0
- langchain_agentx/workflow/batch.py +55 -0
- langchain_agentx/workflow/dag.py +54 -0
- langchain_agentx/workspace/__init__.py +13 -0
- langchain_agentx/workspace/config.py +140 -0
- langchain_agentx/workspace/path_key_normalizer.py +30 -0
- langchain_agentx/workspace/resolver.py +74 -0
- langchain_agentx/workspace/validators.py +41 -0
- langchain_agentx_python-0.1.dist-info/LICENSE +201 -0
- langchain_agentx_python-0.1.dist-info/METADATA +513 -0
- langchain_agentx_python-0.1.dist-info/RECORD +354 -0
- langchain_agentx_python-0.1.dist-info/WHEEL +5 -0
- langchain_agentx_python-0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Reason code constants for loop transitions and termination."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
# ---------------------------------------------------------------------------
|
|
6
|
+
# terminal_reason:对外唯一终态标签(CC 对齐枚举,写入 state)
|
|
7
|
+
# ---------------------------------------------------------------------------
|
|
8
|
+
TERMINAL_REASON_COMPLETED = "completed"
|
|
9
|
+
TERMINAL_REASON_MAX_STEPS = "max_steps"
|
|
10
|
+
TERMINAL_REASON_MAX_TURNS = "max_turns"
|
|
11
|
+
TERMINAL_REASON_MAX_TOKENS = "max_tokens"
|
|
12
|
+
TERMINAL_REASON_CONTENT_FILTER = "content_filter"
|
|
13
|
+
TERMINAL_REASON_BLOCKING_LIMIT = "blocking_limit"
|
|
14
|
+
TERMINAL_REASON_STOP_HOOK_PREVENTED = "stop_hook_prevented"
|
|
15
|
+
TERMINAL_REASON_ERROR = "error"
|
|
16
|
+
# CC queryLoop return.reason:中断 / hook 终局(middleware 或 tools 后写入 abort_terminal_reason)
|
|
17
|
+
TERMINAL_REASON_ABORTED_STREAMING = "aborted_streaming"
|
|
18
|
+
TERMINAL_REASON_ABORTED_TOOLS = "aborted_tools"
|
|
19
|
+
TERMINAL_REASON_HOOK_STOPPED = "hook_stopped"
|
|
20
|
+
|
|
21
|
+
# ---------------------------------------------------------------------------
|
|
22
|
+
# transition.reason(控制流路由观测,不是终态)
|
|
23
|
+
# ---------------------------------------------------------------------------
|
|
24
|
+
TRANSITION_NEXT_TURN = "next_turn"
|
|
25
|
+
TRANSITION_API_ERROR_TERMINAL = "api_error_terminal"
|
|
26
|
+
TRANSITION_WITHHOLD_RECOVERY_RETRY = "withhold_recovery_retry"
|
|
27
|
+
TRANSITION_WITHHOLD_RECOVERY_EXHAUSTED = "withhold_recovery_exhausted"
|
|
28
|
+
TRANSITION_COLLAPSE_DRAIN_RETRY = "collapse_drain_retry"
|
|
29
|
+
TRANSITION_REACTIVE_COMPACT_RETRY = "reactive_compact_retry"
|
|
30
|
+
TRANSITION_MAX_OUTPUT_TOKENS_ESCALATE = "max_output_tokens_escalate"
|
|
31
|
+
TRANSITION_MAX_OUTPUT_TOKENS_RECOVERY = "max_output_tokens_recovery"
|
|
32
|
+
TRANSITION_MAX_OUTPUT_TOKENS_RECOVERY_EXHAUSTED = "max_output_tokens_recovery_exhausted"
|
|
33
|
+
TRANSITION_TASK_NOTIFICATION_ARRIVED = "task_notification_arrived"
|
|
34
|
+
TRANSITION_STOP_HOOK_PREVENTED = "stop_hook_prevented"
|
|
35
|
+
TRANSITION_STOP_HOOK_BLOCKING = "stop_hook_blocking"
|
|
36
|
+
TRANSITION_TOKEN_BUDGET_CONTINUATION = "token_budget_continuation"
|
|
37
|
+
|
|
38
|
+
# token_budget continuation 默认上限(与 CC token budget nudge 次数护栏同向)
|
|
39
|
+
DEFAULT_TOKEN_BUDGET_MAX_CONTINUATIONS = 3
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"""
|
|
2
|
+
内置 loop 控制:捕获 ``LoopAbortSignal``,写入 ``abort_terminal_reason``(及工具侧合成 ToolMessage)。
|
|
3
|
+
|
|
4
|
+
始终由 ``create_loop_agent`` 注入到 ``wrap_model_call`` / ``wrap_tool_call`` 链的最外层,
|
|
5
|
+
用户 middleware 在内层抛出 ``LoopAbortSignal`` 即可,无需手写 Command。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import copy
|
|
11
|
+
import logging
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from langchain.agents.middleware.types import ExtendedModelResponse, ModelResponse
|
|
15
|
+
from langchain_core.messages import AIMessage
|
|
16
|
+
from langgraph.types import Command
|
|
17
|
+
|
|
18
|
+
from ..exit.reason_codes import (
|
|
19
|
+
TERMINAL_REASON_ABORTED_STREAMING,
|
|
20
|
+
TERMINAL_REASON_ABORTED_TOOLS,
|
|
21
|
+
TERMINAL_REASON_HOOK_STOPPED,
|
|
22
|
+
)
|
|
23
|
+
from ..loop_abort import LoopAbortSignal
|
|
24
|
+
from ..model.orphan_tool_results import synthetic_tool_messages_for_orphan_tool_calls
|
|
25
|
+
from ...tool_runtime.permission_context import rotate_auto_approved_window
|
|
26
|
+
|
|
27
|
+
logger = logging.getLogger(__name__)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _abort_model_content(reason: str, detail: str | None) -> str:
|
|
31
|
+
if detail:
|
|
32
|
+
return f"[Agent loop aborted: {reason}] {detail}"
|
|
33
|
+
return f"[Agent loop aborted: {reason}]"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _session_store_from_request(request: Any) -> Any | None:
|
|
37
|
+
try:
|
|
38
|
+
from langgraph.config import get_config
|
|
39
|
+
|
|
40
|
+
config = get_config()
|
|
41
|
+
except Exception as exc:
|
|
42
|
+
logger.debug("failed to fetch langgraph config for session store: %s", exc)
|
|
43
|
+
config = None
|
|
44
|
+
if isinstance(config, dict):
|
|
45
|
+
configurable = config.get("configurable", {}) or {}
|
|
46
|
+
return configurable.get("session_store")
|
|
47
|
+
return None
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class _LoopBuiltinModelControl:
|
|
51
|
+
"""内部单例:捕获模型侧的 ``LoopAbortSignal``。"""
|
|
52
|
+
|
|
53
|
+
def wrap_model_call(self, request: Any, handler: Any) -> Any:
|
|
54
|
+
rotate_auto_approved_window(_session_store_from_request(request))
|
|
55
|
+
try:
|
|
56
|
+
return handler(request)
|
|
57
|
+
except LoopAbortSignal as sig:
|
|
58
|
+
if sig.terminal_reason not in (
|
|
59
|
+
TERMINAL_REASON_ABORTED_STREAMING,
|
|
60
|
+
TERMINAL_REASON_HOOK_STOPPED,
|
|
61
|
+
):
|
|
62
|
+
raise
|
|
63
|
+
ai = AIMessage(
|
|
64
|
+
content=_abort_model_content(sig.terminal_reason, sig.detail),
|
|
65
|
+
response_metadata={"finish_reason": "stop"},
|
|
66
|
+
)
|
|
67
|
+
return ExtendedModelResponse(
|
|
68
|
+
model_response=ModelResponse(result=[ai], structured_response=None),
|
|
69
|
+
command=Command(update={"abort_terminal_reason": sig.terminal_reason}),
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
async def awrap_model_call(self, request: Any, handler: Any) -> Any:
|
|
73
|
+
rotate_auto_approved_window(_session_store_from_request(request))
|
|
74
|
+
try:
|
|
75
|
+
return await handler(request)
|
|
76
|
+
except LoopAbortSignal as sig:
|
|
77
|
+
if sig.terminal_reason not in (
|
|
78
|
+
TERMINAL_REASON_ABORTED_STREAMING,
|
|
79
|
+
TERMINAL_REASON_HOOK_STOPPED,
|
|
80
|
+
):
|
|
81
|
+
raise
|
|
82
|
+
ai = AIMessage(
|
|
83
|
+
content=_abort_model_content(sig.terminal_reason, sig.detail),
|
|
84
|
+
response_metadata={"finish_reason": "stop"},
|
|
85
|
+
)
|
|
86
|
+
return ExtendedModelResponse(
|
|
87
|
+
model_response=ModelResponse(result=[ai], structured_response=None),
|
|
88
|
+
command=Command(update={"abort_terminal_reason": sig.terminal_reason}),
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
_LOOP_BUILTIN_MODEL_SINGLETON = _LoopBuiltinModelControl()
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def get_loop_builtin_model_middleware() -> _LoopBuiltinModelControl:
|
|
96
|
+
"""返回全局单例,避免重复计入用户 middleware 列表。"""
|
|
97
|
+
return _LOOP_BUILTIN_MODEL_SINGLETON
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _model_for_agent_tool(state: Any) -> Any | None:
|
|
101
|
+
config: dict | None = None
|
|
102
|
+
try:
|
|
103
|
+
from langgraph.config import get_config
|
|
104
|
+
|
|
105
|
+
config = get_config()
|
|
106
|
+
except Exception as exc:
|
|
107
|
+
logger.debug("failed to fetch langgraph config for loop model: %s", exc)
|
|
108
|
+
config = None
|
|
109
|
+
loop_config = config.get("configurable", {}).get("loop_config") if config else None
|
|
110
|
+
model = getattr(loop_config, "model", None)
|
|
111
|
+
if isinstance(model, str):
|
|
112
|
+
return model
|
|
113
|
+
if model is None:
|
|
114
|
+
return None
|
|
115
|
+
model_name = getattr(model, "model_name", None)
|
|
116
|
+
return model_name if isinstance(model_name, str) else None
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _with_injected_agent_tool_model(request: Any) -> Any:
|
|
120
|
+
"""在 Agent 工具调用缺失 model 时显式注入父 loop 模型。"""
|
|
121
|
+
tool_call = getattr(request, "tool_call", None)
|
|
122
|
+
if not isinstance(tool_call, dict) or tool_call.get("name") != "Agent":
|
|
123
|
+
return request
|
|
124
|
+
args = tool_call.get("args")
|
|
125
|
+
if not isinstance(args, dict) or args.get("model"):
|
|
126
|
+
return request
|
|
127
|
+
model = _model_for_agent_tool(getattr(request, "state", None))
|
|
128
|
+
if model is None:
|
|
129
|
+
return request
|
|
130
|
+
patched_request = copy.copy(request)
|
|
131
|
+
patched_tool_call = dict(tool_call)
|
|
132
|
+
patched_args = dict(args)
|
|
133
|
+
patched_args["model"] = model
|
|
134
|
+
patched_tool_call["args"] = patched_args
|
|
135
|
+
patched_request.tool_call = patched_tool_call
|
|
136
|
+
return patched_request
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def builtin_sync_tool_wrapper(request: Any, handler: Any) -> Any:
|
|
140
|
+
try:
|
|
141
|
+
return handler(_with_injected_agent_tool_model(request))
|
|
142
|
+
except LoopAbortSignal as sig:
|
|
143
|
+
if sig.terminal_reason != TERMINAL_REASON_ABORTED_TOOLS:
|
|
144
|
+
raise
|
|
145
|
+
tc = request.tool_call
|
|
146
|
+
pending = [dict(tc)] if isinstance(tc, dict) else []
|
|
147
|
+
synth = (
|
|
148
|
+
synthetic_tool_messages_for_orphan_tool_calls(
|
|
149
|
+
pending_tool_calls=pending,
|
|
150
|
+
content_template=(
|
|
151
|
+
"[Tool execution aborted; synthetic result.]"
|
|
152
|
+
+ (f" {sig.detail}" if sig.detail else "")
|
|
153
|
+
),
|
|
154
|
+
)
|
|
155
|
+
if pending
|
|
156
|
+
else []
|
|
157
|
+
)
|
|
158
|
+
return Command(
|
|
159
|
+
update={
|
|
160
|
+
"abort_terminal_reason": TERMINAL_REASON_ABORTED_TOOLS,
|
|
161
|
+
"messages": synth,
|
|
162
|
+
}
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
async def builtin_async_tool_wrapper(request: Any, handler: Any) -> Any:
|
|
167
|
+
try:
|
|
168
|
+
return await handler(_with_injected_agent_tool_model(request))
|
|
169
|
+
except LoopAbortSignal as sig:
|
|
170
|
+
if sig.terminal_reason != TERMINAL_REASON_ABORTED_TOOLS:
|
|
171
|
+
raise
|
|
172
|
+
tc = request.tool_call
|
|
173
|
+
pending = [dict(tc)] if isinstance(tc, dict) else []
|
|
174
|
+
synth = (
|
|
175
|
+
synthetic_tool_messages_for_orphan_tool_calls(
|
|
176
|
+
pending_tool_calls=pending,
|
|
177
|
+
content_template=(
|
|
178
|
+
"[Tool execution aborted; synthetic result.]"
|
|
179
|
+
+ (f" {sig.detail}" if sig.detail else "")
|
|
180
|
+
),
|
|
181
|
+
)
|
|
182
|
+
if pending
|
|
183
|
+
else []
|
|
184
|
+
)
|
|
185
|
+
return Command(
|
|
186
|
+
update={
|
|
187
|
+
"abort_terminal_reason": TERMINAL_REASON_ABORTED_TOOLS,
|
|
188
|
+
"messages": synth,
|
|
189
|
+
}
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
__all__ = [
|
|
194
|
+
"builtin_async_tool_wrapper",
|
|
195
|
+
"builtin_sync_tool_wrapper",
|
|
196
|
+
"get_loop_builtin_model_middleware",
|
|
197
|
+
]
|