dandy 3.0.0a1__tar.gz → 3.0.0a4__tar.gz
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.
- {dandy-3.0.0a1 → dandy-3.0.0a4}/PKG-INFO +1 -1
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/__init__.py +6 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/agent.py +6 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/decision/decision.py +5 -1
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/llm/service.py +70 -3
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/tool/service.py +39 -29
- dandy-3.0.0a4/dandy/domain/guard/__init__.py +20 -0
- dandy-3.0.0a4/dandy/domain/guard/context_guard.py +53 -0
- dandy-3.0.0a4/dandy/domain/guard/guard.py +136 -0
- dandy-3.0.0a4/dandy/domain/guard/repeat_guard.py +104 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/intel/intel.py +2 -1
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/message/message.py +7 -3
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/request/options.py +2 -1
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/request/request.py +7 -1
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/skill/skill.py +14 -2
- dandy-3.0.0a4/dandy/domain/tool/__init__.py +4 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/tool/tool.py +16 -2
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/key.py +3 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/llm/config.py +42 -1
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/llm/connector.py +10 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/cli.py +83 -31
- dandy-3.0.0a4/dandy/interfaces/cli/commands.py +82 -0
- dandy-3.0.0a4/dandy/interfaces/cli/main.py +136 -0
- dandy-3.0.0a4/dandy/interfaces/cli/one_shot.py +110 -0
- dandy-3.0.0a4/dandy/interfaces/cli/tui/tui.py +226 -0
- dandy-3.0.0a4/dandy/interfaces/cli/usage.py +60 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/constants.py +1 -1
- dandy-3.0.0a4/dandy/shared/hashing.py +125 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/settings/load.py +2 -1
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy.egg-info/PKG-INFO +1 -1
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy.egg-info/SOURCES.txt +8 -1
- {dandy-3.0.0a1 → dandy-3.0.0a4}/pyproject.toml +1 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/tests/test_dandy.py +2 -0
- dandy-3.0.0a1/dandy/domain/tool/__init__.py +0 -5
- dandy-3.0.0a1/dandy/domain/tool/repeated_call.py +0 -145
- dandy-3.0.0a1/dandy/interfaces/cli/main.py +0 -130
- dandy-3.0.0a1/dandy/interfaces/cli/tui/tui.py +0 -131
- {dandy-3.0.0a1 → dandy-3.0.0a4}/LICENSE.md +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/README.md +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/cli/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/cli/agents_md.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/cli/cli_agent.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/cli/intelligence/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/cli/intelligence/bots/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/cli/intelligence/bots/coding_bot.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/cli/intelligence/bots/init_bot.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/cli/intelligence/bots/session_naming_bot.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/cli/intelligence/prompts.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intel.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/command/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/command/command_tool.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/file/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/file/create_directory_tool.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/file/delete_file_tool.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/file/edit_file_tool.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/file/list_directory_tool.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/file/read_file_tool.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/file/search_tool.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/file/write_file_tool.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/git/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/git/git_diff_tool.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/git/git_status_tool.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/paths.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/question/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/question/question_tool.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/intelligence/tools/tool_errors.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/prompts.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/session.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/agent/session_store.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/bot/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/bot/bot.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/bot/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/decision/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/decision/mixin.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/decision/service.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/diligence.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/handler.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/mixin.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/second_pass/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/second_pass/diligence.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/service.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/stop_word_removal/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/stop_word_removal/constants.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/stop_word_removal/diligence.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/vowel_removal/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/vowel_removal/constants.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/diligence/vowel_removal/diligence.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/llm/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/llm/mixin.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/skill/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/skill/mixin.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/skill/service.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/skill/tools.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/tool/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/application/tool/mixin.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/decision/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/decision/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/decision/intel.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/decision/prompts.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/intel/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/intel/annotation.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/intel/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/intel/factory.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/intel/mixin.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/intel/service.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/intel/typing.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/message/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/message/compaction.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/prompt/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/prompt/prompt.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/prompt/snippet.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/request/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/request/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/request/prompts.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/skill/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/skill/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/skill/prompts.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/tokens/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/tokens/estimate.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/tool/beat.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/tool/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/domain/tool/intel.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/cache.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/decorators.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/memory/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/memory/cache.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/memory/decorators.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/sqlite/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/sqlite/cache.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/sqlite/connection.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/cache/sqlite/decorators.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/file/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/file/mixin.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/file/service.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/future/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/future/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/future/future.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/future/process.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/http/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/http/connector.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/http/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/http/intel.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/http/mixin.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/http/service.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/http/url.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/llm/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/llm/fenced_json.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/bot_events.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/decision_events.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/decorators.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/diligence_events.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/events.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/llm_events.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/recorder.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/recording.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/renderer/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/renderer/html.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/renderer/html_templates/base_event_template.html +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/renderer/html_templates/base_recording_output_template.html +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/renderer/json.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/renderer/markdown.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/renderer/renderer.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/tool_events.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/infrastructure/recorder/utils.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/error_log.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/thinking_phrases.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/tui/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/tui/ascii.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/tui/banner.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/tui/markdown.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/tui/printer.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/tui/story_progress.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/tui/text.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/interfaces/cli/utils.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/cancellation.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/connector/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/connector/connector.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/connector/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/debug.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/files.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/interaction.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/media.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/service/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/service/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/service/mixin.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/service/service.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/settings/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/settings/default_settings.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/settings/settings.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/singleton.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/subprocess.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/typing/__init__.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/typing/constants.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/typing/exceptions.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/typing/registry.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/typing/signature.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/typing/typed_kwargs.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/typing/typing.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy/shared/utils.py +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy.egg-info/dependency_links.txt +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy.egg-info/entry_points.txt +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy.egg-info/requires.txt +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/dandy.egg-info/top_level.txt +0 -0
- {dandy-3.0.0a1 → dandy-3.0.0a4}/setup.cfg +0 -0
|
@@ -7,11 +7,13 @@ from dandy.infrastructure.cache.sqlite.cache import SqliteCache
|
|
|
7
7
|
from dandy.infrastructure.cache.sqlite.decorators import cache_to_sqlite
|
|
8
8
|
from dandy.infrastructure.cache.key import generate_cache_key
|
|
9
9
|
from dandy.shared.exceptions import DandyCriticalError, DandyError, DandyRecoverableError
|
|
10
|
+
from dandy.shared.hashing import HashMixin
|
|
10
11
|
from dandy.infrastructure.future.process import process_to_future
|
|
11
12
|
from dandy.domain.intel.intel import BaseIntel, BaseListIntel, DefaultIntel
|
|
12
13
|
from dandy.domain.message.message import MessageHistory
|
|
13
14
|
from dandy.domain.prompt.prompt import Prompt
|
|
14
15
|
from dandy.domain.request.options import LlmOptions
|
|
16
|
+
from dandy.infrastructure.llm.config import LlmConfig
|
|
15
17
|
from dandy.infrastructure.recorder.decorators import (
|
|
16
18
|
recorder_to_html_file,
|
|
17
19
|
recorder_to_json_file,
|
|
@@ -34,6 +36,8 @@ __all__ = [
|
|
|
34
36
|
'DandyRecoverableError',
|
|
35
37
|
'Decision',
|
|
36
38
|
'DefaultIntel',
|
|
39
|
+
'HashMixin',
|
|
40
|
+
'LlmConfig',
|
|
37
41
|
'LlmOptions',
|
|
38
42
|
'MemoryCache',
|
|
39
43
|
'MessageHistory',
|
|
@@ -71,6 +75,8 @@ class Dandy:
|
|
|
71
75
|
DandyRecoverableError = DandyRecoverableError
|
|
72
76
|
Decision = Decision
|
|
73
77
|
DefaultIntel = DefaultIntel
|
|
78
|
+
HashMixin = HashMixin
|
|
79
|
+
LlmConfig = LlmConfig
|
|
74
80
|
LlmOptions = LlmOptions
|
|
75
81
|
MemoryCache = MemoryCache
|
|
76
82
|
MessageHistory = MessageHistory
|
|
@@ -24,6 +24,7 @@ from dandy.domain.tool.beat import StoryBeat
|
|
|
24
24
|
|
|
25
25
|
if TYPE_CHECKING:
|
|
26
26
|
from dandy.application.bot.bot import Bot
|
|
27
|
+
from dandy.application.llm.service import LlmService
|
|
27
28
|
from dandy.domain.intel.typing import IntelType
|
|
28
29
|
from dandy.domain.skill.skill import SkillType
|
|
29
30
|
from dandy.domain.tool.tool import ToolType
|
|
@@ -74,6 +75,10 @@ class Agent:
|
|
|
74
75
|
context_size = self.bot.llm.config.context_size
|
|
75
76
|
return context_size if context_size > 0 else DEFAULT_CONTEXT_WINDOW
|
|
76
77
|
|
|
78
|
+
@property
|
|
79
|
+
def llm(self) -> LlmService:
|
|
80
|
+
return self.bot.llm
|
|
81
|
+
|
|
77
82
|
def turn(
|
|
78
83
|
self,
|
|
79
84
|
message: Prompt | str | None = None,
|
|
@@ -91,6 +96,7 @@ class Agent:
|
|
|
91
96
|
raise RunStoppedError(RUN_STOPPED_MESSAGE)
|
|
92
97
|
|
|
93
98
|
self.last_request = self._message_as_str(message)
|
|
99
|
+
self.max_context_tokens = self._resolve_context_size()
|
|
94
100
|
message = self._prepare_message(message, progress_callback, verbose_callback)
|
|
95
101
|
self._compact_history_for_next_message(message, progress_callback)
|
|
96
102
|
self._ensure_history_fits(message)
|
|
@@ -30,18 +30,22 @@ from dandy.infrastructure.recorder.llm_events import recorder_add_llm_failure_ev
|
|
|
30
30
|
|
|
31
31
|
if TYPE_CHECKING:
|
|
32
32
|
from dandy.application.llm.mixin import LlmServiceMixin
|
|
33
|
+
from dandy.infrastructure.llm.config import LlmConfig
|
|
33
34
|
|
|
34
35
|
|
|
35
36
|
class Decision:
|
|
36
37
|
def __init__(self, recorder_event_id: str, llm_service_mixin: LlmServiceMixin) -> None:
|
|
37
38
|
self.recorder_event_id = recorder_event_id
|
|
38
39
|
self._llm_service_mixin = llm_service_mixin
|
|
39
|
-
self.llm_config = self._llm_service_mixin.get_llm_config()
|
|
40
40
|
self._llm_connector = None
|
|
41
41
|
|
|
42
42
|
self.keys_description = None
|
|
43
43
|
self.keys_values = None
|
|
44
44
|
|
|
45
|
+
@property
|
|
46
|
+
def llm_config(self) -> LlmConfig:
|
|
47
|
+
return self._llm_service_mixin.llm.config
|
|
48
|
+
|
|
45
49
|
def __getitem__(self, item: str) -> Any:
|
|
46
50
|
return self.keys_values[item]
|
|
47
51
|
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
from pathlib import Path
|
|
4
|
-
from typing import TYPE_CHECKING
|
|
4
|
+
from typing import TYPE_CHECKING, Any
|
|
5
5
|
|
|
6
6
|
from dandy.infrastructure.future.process import process_to_future
|
|
7
7
|
from dandy.shared.service.service import BaseService
|
|
8
8
|
from dandy.infrastructure.llm.connector import LlmConnector
|
|
9
|
+
from dandy.infrastructure.llm.config import LlmConfig
|
|
9
10
|
from dandy.application.decision.mixin import DecisionServiceMixin
|
|
10
11
|
from dandy.application.diligence.mixin import DiligenceServiceMixin
|
|
12
|
+
from dandy.domain.request.exceptions import LlmCriticalError
|
|
13
|
+
from dandy.domain.request.options import LlmOptions
|
|
11
14
|
from dandy.domain.request.prompts import service_system_prompt
|
|
12
15
|
from dandy.application.tool.mixin import ToolServiceMixin
|
|
13
16
|
from dandy.application.skill.mixin import SkillServiceMixin
|
|
@@ -17,13 +20,21 @@ if TYPE_CHECKING:
|
|
|
17
20
|
|
|
18
21
|
from dandy.infrastructure.future.future import AsyncFuture
|
|
19
22
|
from dandy.domain.intel.typing import IntelType
|
|
20
|
-
from dandy.infrastructure.llm.config import LlmConfig
|
|
21
|
-
from dandy.domain.request.options import LlmOptions
|
|
22
23
|
from dandy.domain.prompt.prompt import Prompt
|
|
23
24
|
from dandy.domain.message.message import MessageHistory
|
|
24
25
|
from dandy.domain.tool.tool import ToolType
|
|
25
26
|
|
|
26
27
|
|
|
28
|
+
_CONFIG_IDENTITY_OVERRIDE_TYPES = {
|
|
29
|
+
'model': str,
|
|
30
|
+
'host': str,
|
|
31
|
+
'port': int,
|
|
32
|
+
'api_key': str,
|
|
33
|
+
'context_size': int,
|
|
34
|
+
'prompt_retry_count': int,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
27
38
|
class LlmService(
|
|
28
39
|
BaseService['dandy.application.llm.mixin.LlmServiceMixin'],
|
|
29
40
|
DecisionServiceMixin,
|
|
@@ -66,6 +77,62 @@ class LlmService(
|
|
|
66
77
|
def config(self) -> LlmConfig:
|
|
67
78
|
return self._llm_connector.llm_config
|
|
68
79
|
|
|
80
|
+
@config.setter
|
|
81
|
+
def config(self, llm_config: LlmConfig) -> None:
|
|
82
|
+
if not isinstance(llm_config, LlmConfig):
|
|
83
|
+
message = f'config expects an LlmConfig instance, got {type(llm_config).__name__}.'
|
|
84
|
+
raise LlmCriticalError(message)
|
|
85
|
+
|
|
86
|
+
self._llm_connector.set_llm_config(llm_config)
|
|
87
|
+
self.obj.llm_config = llm_config.name
|
|
88
|
+
|
|
89
|
+
def set_config(self, name: str, **overrides: Any) -> LlmConfig:
|
|
90
|
+
"""Swap this bot's LLM config mid-conversation, keeping the history.
|
|
91
|
+
|
|
92
|
+
`name` selects a settings `LLM_CONFIGS` entry; keyword overrides set
|
|
93
|
+
individual fields on the resolved config (model, host, port, api_key,
|
|
94
|
+
context_size, prompt_retry_count, or any LlmOptions field). The new
|
|
95
|
+
config applies to the next request and survives `reset()`.
|
|
96
|
+
"""
|
|
97
|
+
identity_overrides: dict[str, Any] = {}
|
|
98
|
+
option_overrides: dict[str, Any] = {}
|
|
99
|
+
|
|
100
|
+
for key, value in overrides.items():
|
|
101
|
+
expected_type = _CONFIG_IDENTITY_OVERRIDE_TYPES.get(key)
|
|
102
|
+
|
|
103
|
+
if expected_type is not None:
|
|
104
|
+
if isinstance(value, bool) or not isinstance(value, expected_type):
|
|
105
|
+
message = f'Invalid override for "{key}": expected {expected_type.__name__}.'
|
|
106
|
+
raise LlmCriticalError(message)
|
|
107
|
+
|
|
108
|
+
identity_overrides[key] = value
|
|
109
|
+
|
|
110
|
+
elif key in LlmOptions.model_fields:
|
|
111
|
+
option_overrides[key] = value
|
|
112
|
+
|
|
113
|
+
else:
|
|
114
|
+
message = (
|
|
115
|
+
f'Unknown LLM config override "{key}". Use model, host, port, api_key, '
|
|
116
|
+
'context_size, prompt_retry_count or an LlmOptions field.'
|
|
117
|
+
)
|
|
118
|
+
raise LlmCriticalError(message)
|
|
119
|
+
|
|
120
|
+
llm_config = LlmConfig(name)
|
|
121
|
+
|
|
122
|
+
for key, value in identity_overrides.items():
|
|
123
|
+
setattr(llm_config, key, value)
|
|
124
|
+
|
|
125
|
+
if option_overrides:
|
|
126
|
+
base_options = llm_config._settings_values.get('options') or {}
|
|
127
|
+
merged_options = {**base_options, **option_overrides}
|
|
128
|
+
llm_config.options = LlmOptions(**merged_options)
|
|
129
|
+
llm_config._settings_values['options'] = merged_options
|
|
130
|
+
|
|
131
|
+
self._llm_connector.set_llm_config(llm_config)
|
|
132
|
+
self.obj.llm_config = name
|
|
133
|
+
|
|
134
|
+
return llm_config
|
|
135
|
+
|
|
69
136
|
@property
|
|
70
137
|
def options(self) -> LlmOptions:
|
|
71
138
|
return self._llm_connector.llm_config.options
|
|
@@ -12,17 +12,18 @@ from dandy.shared.utils import (
|
|
|
12
12
|
pydantic_validation_error_to_str,
|
|
13
13
|
truncate_output,
|
|
14
14
|
)
|
|
15
|
+
from dandy.domain.guard.context_guard import ContextPressureGuard
|
|
16
|
+
from dandy.domain.guard.guard import GuardChain, GuardEvent
|
|
17
|
+
from dandy.domain.guard.repeat_guard import RepeatGuard
|
|
15
18
|
from dandy.domain.intel.factory import IntelFactory
|
|
16
19
|
from dandy.domain.request.exceptions import LlmCriticalError, LlmRecoverableError
|
|
17
20
|
from dandy.domain.message.compaction import (
|
|
18
21
|
COMPACTION_TARGET_RATIO,
|
|
19
|
-
COMPACTION_TRIGGER_RATIO,
|
|
20
22
|
DEFAULT_CONTEXT_WINDOW,
|
|
21
23
|
compact_message_history,
|
|
22
24
|
)
|
|
23
25
|
from dandy.domain.tool.beat import StoryBeat
|
|
24
26
|
from dandy.domain.tool.intel import ToolCallIntel, ToolCallsIntel
|
|
25
|
-
from dandy.domain.tool.repeated_call import RepeatedCallGuard
|
|
26
27
|
from dandy.infrastructure.recorder.tool_events import recorder_add_tool_result_event
|
|
27
28
|
from dandy.domain.tool.tool import BaseTool, ToolType, ToolHandler, to_tool_instances
|
|
28
29
|
|
|
@@ -58,17 +59,25 @@ class ToolService(BaseService['dandy.application.tool.mixin.ToolServiceMixin']):
|
|
|
58
59
|
tool_functions = tool_functions or {}
|
|
59
60
|
|
|
60
61
|
iteration_count = 0
|
|
61
|
-
#
|
|
62
|
-
#
|
|
63
|
-
#
|
|
64
|
-
#
|
|
65
|
-
|
|
62
|
+
# The guards watch the model's behavior live: the repeat guard steers
|
|
63
|
+
# the model out of degenerate repeat loops, and the context-pressure
|
|
64
|
+
# guard keeps the conversation inside the window, failing the loop
|
|
65
|
+
# fast when a correction stops working instead of letting the
|
|
66
|
+
# provider reject the request.
|
|
67
|
+
guard_chain = GuardChain([RepeatGuard(), ContextPressureGuard()])
|
|
66
68
|
|
|
67
69
|
while True:
|
|
68
70
|
if stop_signal.is_set():
|
|
69
71
|
raise RunStoppedError(RUN_STOPPED_MESSAGE)
|
|
70
72
|
|
|
71
|
-
self.
|
|
73
|
+
context_size = getattr(self.obj.config, 'context_size', 0) or DEFAULT_CONTEXT_WINDOW
|
|
74
|
+
|
|
75
|
+
round_outcome = guard_chain.on_round_start(
|
|
76
|
+
self.obj.messages.estimated_token_count, context_size
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
if round_outcome.compact:
|
|
80
|
+
self._compact_history(progress_callback=progress_callback)
|
|
72
81
|
|
|
73
82
|
skill_tools = skill_service.active_tools()
|
|
74
83
|
round_tools = user_tools + skill_tools
|
|
@@ -122,10 +131,12 @@ class ToolService(BaseService['dandy.application.tool.mixin.ToolServiceMixin']):
|
|
|
122
131
|
tool_call=tool_call,
|
|
123
132
|
tools_by_name=tools_by_name,
|
|
124
133
|
tool_functions=tool_functions,
|
|
134
|
+
guard_chain=guard_chain,
|
|
135
|
+
round_number=round_number,
|
|
136
|
+
context_size=context_size,
|
|
125
137
|
progress_callback=progress_callback,
|
|
126
138
|
verbose_callback=verbose_callback,
|
|
127
139
|
tool_call_callback=tool_call_callback,
|
|
128
|
-
repeated_call_guard=repeated_call_guard,
|
|
129
140
|
)
|
|
130
141
|
|
|
131
142
|
iteration_count += 1
|
|
@@ -143,29 +154,21 @@ class ToolService(BaseService['dandy.application.tool.mixin.ToolServiceMixin']):
|
|
|
143
154
|
)
|
|
144
155
|
raise LlmCriticalError(message)
|
|
145
156
|
|
|
146
|
-
def
|
|
157
|
+
def _compact_history(
|
|
147
158
|
self, progress_callback: Callable[[StoryBeat], None] | None = None
|
|
148
159
|
) -> None:
|
|
149
160
|
"""Trim the accumulated conversation before the next LLM round.
|
|
150
161
|
|
|
151
162
|
Long-running tool loops append a tool-call request plus its results per
|
|
152
|
-
round, which can overflow the provider's context window when the loop
|
|
153
|
-
unbounded.
|
|
154
|
-
|
|
155
|
-
and the newest exchange.
|
|
156
|
-
|
|
157
|
-
|
|
163
|
+
round, which can overflow the provider's context window when the loop
|
|
164
|
+
is unbounded. Compacts to a fraction of the resolved config's
|
|
165
|
+
``CONTEXT_SIZE`` (a default window when unset), keeping the system
|
|
166
|
+
message and the newest exchange. The context-pressure guard decides
|
|
167
|
+
when this runs, so a steady state where each round re-adds about what
|
|
168
|
+
compaction just dropped does not compact every round.
|
|
158
169
|
"""
|
|
159
170
|
context_size = getattr(self.obj.config, 'context_size', 0) or DEFAULT_CONTEXT_WINDOW
|
|
160
171
|
target_token_count = max(1, int(context_size * COMPACTION_TARGET_RATIO))
|
|
161
|
-
trigger_token_count = max(
|
|
162
|
-
target_token_count + 1, int(context_size * COMPACTION_TRIGGER_RATIO)
|
|
163
|
-
)
|
|
164
|
-
|
|
165
|
-
current_token_count = self.obj.messages.estimated_token_count
|
|
166
|
-
|
|
167
|
-
if current_token_count <= trigger_token_count:
|
|
168
|
-
return
|
|
169
172
|
|
|
170
173
|
if progress_callback is not None:
|
|
171
174
|
progress_callback(
|
|
@@ -183,10 +186,12 @@ class ToolService(BaseService['dandy.application.tool.mixin.ToolServiceMixin']):
|
|
|
183
186
|
tools_by_name: dict[str, BaseTool],
|
|
184
187
|
tool_functions: dict[str, ToolHandler],
|
|
185
188
|
*,
|
|
189
|
+
guard_chain: GuardChain,
|
|
190
|
+
round_number: int,
|
|
191
|
+
context_size: int,
|
|
186
192
|
progress_callback: Callable[[StoryBeat], None] | None = None,
|
|
187
193
|
verbose_callback: Callable[[str], None] | None = None,
|
|
188
194
|
tool_call_callback: Callable[[str, dict[str, object]], None] | None = None,
|
|
189
|
-
repeated_call_guard: RepeatedCallGuard,
|
|
190
195
|
) -> None:
|
|
191
196
|
tool = tools_by_name.get(tool_call.name)
|
|
192
197
|
|
|
@@ -248,10 +253,15 @@ class ToolService(BaseService['dandy.application.tool.mixin.ToolServiceMixin']):
|
|
|
248
253
|
tool_name=tool_call.name, result=result_text, event_id=self.recorder_event_id
|
|
249
254
|
)
|
|
250
255
|
|
|
251
|
-
feedback_text =
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
256
|
+
feedback_text = guard_chain.on_tool_call(
|
|
257
|
+
GuardEvent(
|
|
258
|
+
tool_name=tool_call.name,
|
|
259
|
+
arguments=arguments_kwargs,
|
|
260
|
+
result_text=result_text,
|
|
261
|
+
round_number=round_number,
|
|
262
|
+
history_token_count=self.obj.messages.estimated_token_count,
|
|
263
|
+
context_window=context_size,
|
|
264
|
+
),
|
|
255
265
|
verbose_callback=verbose_callback,
|
|
256
266
|
)
|
|
257
267
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from dandy.domain.guard.context_guard import ContextPressureGuard
|
|
2
|
+
from dandy.domain.guard.guard import Guard, GuardChain, GuardEvent, GuardOutcome
|
|
3
|
+
from dandy.domain.guard.repeat_guard import (
|
|
4
|
+
REPEATED_CALL_STOP_THRESHOLD,
|
|
5
|
+
REPEATED_CALL_WARNING_THRESHOLD,
|
|
6
|
+
RepeatGuard,
|
|
7
|
+
canonical_tool_arguments,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
'REPEATED_CALL_STOP_THRESHOLD',
|
|
12
|
+
'REPEATED_CALL_WARNING_THRESHOLD',
|
|
13
|
+
'ContextPressureGuard',
|
|
14
|
+
'Guard',
|
|
15
|
+
'GuardChain',
|
|
16
|
+
'GuardEvent',
|
|
17
|
+
'GuardOutcome',
|
|
18
|
+
'RepeatGuard',
|
|
19
|
+
'canonical_tool_arguments',
|
|
20
|
+
]
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dandy.domain.message.compaction import COMPACTION_TRIGGER_RATIO
|
|
4
|
+
from dandy.domain.guard.guard import Guard, GuardOutcome
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ContextPressureGuard(Guard):
|
|
8
|
+
"""Watch the conversation's context pressure and keep the loop on the ground.
|
|
9
|
+
|
|
10
|
+
Requests compaction (the loop's course correction) once the conversation's
|
|
11
|
+
estimated token count passes the trigger share of the context window. If
|
|
12
|
+
the pressure re-asserts itself after a compaction was already issued, that
|
|
13
|
+
is an identical failure: the correction could not relieve the pressure.
|
|
14
|
+
When what remains still exceeds the whole window, no valid request can be
|
|
15
|
+
sent, so the guard fails the loop instead of letting the provider reject
|
|
16
|
+
it. Pressure that settles back under the trigger clears the guard.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
name = 'context pressure'
|
|
20
|
+
|
|
21
|
+
def __init__(self, trigger_ratio: float = COMPACTION_TRIGGER_RATIO) -> None:
|
|
22
|
+
super().__init__()
|
|
23
|
+
self.trigger_ratio = trigger_ratio
|
|
24
|
+
self._compaction_issued = False
|
|
25
|
+
|
|
26
|
+
def on_round_start(self, history_token_count: int, context_window: int) -> GuardOutcome:
|
|
27
|
+
if context_window <= 0:
|
|
28
|
+
return GuardOutcome()
|
|
29
|
+
|
|
30
|
+
trigger_token_count = max(1, int(context_window * self.trigger_ratio))
|
|
31
|
+
|
|
32
|
+
if history_token_count <= trigger_token_count:
|
|
33
|
+
self._compaction_issued = False
|
|
34
|
+
return GuardOutcome()
|
|
35
|
+
|
|
36
|
+
if self._compaction_issued:
|
|
37
|
+
self.record_correction(self.name)
|
|
38
|
+
|
|
39
|
+
if history_token_count > context_window:
|
|
40
|
+
message = (
|
|
41
|
+
f'LLM Tool Service stopped the loop: compaction was '
|
|
42
|
+
f'requested but the conversation ({history_token_count} '
|
|
43
|
+
f'tokens) still exceeds the {context_window} token '
|
|
44
|
+
'context window, so no valid request can be sent. '
|
|
45
|
+
'Clear the conversation and retry, with a shorter '
|
|
46
|
+
'request if possible.'
|
|
47
|
+
)
|
|
48
|
+
return GuardOutcome(failure_message=message)
|
|
49
|
+
|
|
50
|
+
return GuardOutcome()
|
|
51
|
+
|
|
52
|
+
self._compaction_issued = True
|
|
53
|
+
return GuardOutcome(compact=True)
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Callable, ClassVar, Sequence
|
|
5
|
+
|
|
6
|
+
from dandy.domain.request.exceptions import LlmRecoverableError
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass(frozen=True)
|
|
10
|
+
class GuardEvent:
|
|
11
|
+
"""One observed moment in a tool loop, everything a guard may need.
|
|
12
|
+
|
|
13
|
+
Tool call events carry the executed call (name, validated arguments,
|
|
14
|
+
result text, round number) plus the loop's context pressure (the
|
|
15
|
+
conversation's estimated token count against the resolved context
|
|
16
|
+
window), so guards that watch the conversation shape can run off the
|
|
17
|
+
same event stream as guards that watch the calls themselves.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
tool_name: str
|
|
21
|
+
arguments: dict
|
|
22
|
+
result_text: str
|
|
23
|
+
round_number: int
|
|
24
|
+
history_token_count: int
|
|
25
|
+
context_window: int
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass
|
|
29
|
+
class GuardOutcome:
|
|
30
|
+
"""A guard's verdict on one event.
|
|
31
|
+
|
|
32
|
+
``note`` is a steering text to append to the tool result fed back to the
|
|
33
|
+
model (the live course correction), ``compact`` asks the loop to compact
|
|
34
|
+
the conversation before the next LLM round, ``failure_message`` stops the
|
|
35
|
+
loop (raised by the chain as ``LlmRecoverableError``), and ``verbose_line``
|
|
36
|
+
is an optional debug line for the loop's verbose callback.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
note: str | None = None
|
|
40
|
+
compact: bool = False
|
|
41
|
+
failure_message: str | None = None
|
|
42
|
+
verbose_line: str | None = None
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class Guard:
|
|
46
|
+
"""A watcher over a tool loop that course-corrects the model's behavior.
|
|
47
|
+
|
|
48
|
+
Guards are middleware for the loop: each one observes the same events
|
|
49
|
+
(round starts and executed tool calls) and returns a verdict. A guard
|
|
50
|
+
that sees the same signature trip it again after it already issued a
|
|
51
|
+
correction for that signature counts an identical failure: the
|
|
52
|
+
correction did not change the behavior. Identical failures piling up is
|
|
53
|
+
what fails the loop, so a stuck model costs a bounded number of rounds
|
|
54
|
+
instead of burning the whole tool-iteration cap.
|
|
55
|
+
|
|
56
|
+
Subclasses set ``name`` (used in loop error messages) and implement
|
|
57
|
+
``observe`` and/or ``on_round_start``; a hook that is not implemented
|
|
58
|
+
stays a no-op, so a guard can watch only one kind of event.
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
name: ClassVar[str] = 'guard'
|
|
62
|
+
|
|
63
|
+
def __init__(self) -> None:
|
|
64
|
+
self._corrections_issued: dict[str, int] = {}
|
|
65
|
+
|
|
66
|
+
def observe(self, event: GuardEvent) -> GuardOutcome: # noqa: ARG002
|
|
67
|
+
"""Watch one executed tool call and return the correction to apply."""
|
|
68
|
+
return GuardOutcome()
|
|
69
|
+
|
|
70
|
+
def on_round_start(self, history_token_count: int, context_window: int) -> GuardOutcome: # noqa: ARG002
|
|
71
|
+
"""Watch the loop's context pressure at the start of a round."""
|
|
72
|
+
return GuardOutcome()
|
|
73
|
+
|
|
74
|
+
def record_correction(self, signature: str) -> int:
|
|
75
|
+
"""Count a correction issued for a signature and return the new count.
|
|
76
|
+
|
|
77
|
+
A signature that re-trips a guard after a correction was already
|
|
78
|
+
issued for it is an identical failure: the correction did not change
|
|
79
|
+
the behavior, and repeated identical failures are what fail the loop.
|
|
80
|
+
"""
|
|
81
|
+
count = self._corrections_issued.get(signature, 0) + 1
|
|
82
|
+
self._corrections_issued[signature] = count
|
|
83
|
+
return count
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class GuardChain:
|
|
87
|
+
"""Run an ordered set of guards over one tool loop.
|
|
88
|
+
|
|
89
|
+
Every guard sees every event; the chain raises ``LlmRecoverableError``
|
|
90
|
+
the moment any guard reports a failure (before the next HTTP call),
|
|
91
|
+
appends every guard's steering notes to the tool result fed back to the
|
|
92
|
+
model, and forwards any guard's compaction request to the loop.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
def __init__(self, guards: Sequence[Guard]) -> None:
|
|
96
|
+
self._guards = list(guards)
|
|
97
|
+
|
|
98
|
+
def on_round_start(self, history_token_count: int, context_window: int) -> GuardOutcome:
|
|
99
|
+
"""Watch a round start through every guard; returns the combined verdict."""
|
|
100
|
+
outcome = GuardOutcome()
|
|
101
|
+
|
|
102
|
+
for guard in self._guards:
|
|
103
|
+
guard_outcome = guard.on_round_start(history_token_count, context_window)
|
|
104
|
+
|
|
105
|
+
if guard_outcome.failure_message:
|
|
106
|
+
raise LlmRecoverableError(guard_outcome.failure_message)
|
|
107
|
+
|
|
108
|
+
if guard_outcome.compact:
|
|
109
|
+
outcome.compact = True
|
|
110
|
+
|
|
111
|
+
return outcome
|
|
112
|
+
|
|
113
|
+
def on_tool_call(
|
|
114
|
+
self, event: GuardEvent, verbose_callback: Callable[[str], None] | None = None
|
|
115
|
+
) -> str:
|
|
116
|
+
"""Watch an executed tool call through every guard.
|
|
117
|
+
|
|
118
|
+
Returns the result text to feed back to the model with any steering
|
|
119
|
+
notes appended, and raises ``LlmRecoverableError`` when a guard fails
|
|
120
|
+
the loop.
|
|
121
|
+
"""
|
|
122
|
+
result_text = event.result_text
|
|
123
|
+
|
|
124
|
+
for guard in self._guards:
|
|
125
|
+
guard_outcome = guard.observe(event)
|
|
126
|
+
|
|
127
|
+
if guard_outcome.failure_message:
|
|
128
|
+
raise LlmRecoverableError(guard_outcome.failure_message)
|
|
129
|
+
|
|
130
|
+
if guard_outcome.verbose_line and verbose_callback is not None:
|
|
131
|
+
verbose_callback(guard_outcome.verbose_line)
|
|
132
|
+
|
|
133
|
+
if guard_outcome.note:
|
|
134
|
+
result_text += guard_outcome.note
|
|
135
|
+
|
|
136
|
+
return result_text
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
from dandy.shared.hashing import canonicalize
|
|
7
|
+
from dandy.domain.guard.guard import Guard, GuardEvent, GuardOutcome
|
|
8
|
+
|
|
9
|
+
# From the warning threshold on, the tool result fed back to the model carries
|
|
10
|
+
# a steering note; at the stop threshold the loop fails fast, so a degenerate
|
|
11
|
+
# run costs a bounded number of rounds instead of burning the whole
|
|
12
|
+
# tool-iteration cap.
|
|
13
|
+
REPEATED_CALL_WARNING_THRESHOLD = 3
|
|
14
|
+
REPEATED_CALL_STOP_THRESHOLD = 10
|
|
15
|
+
|
|
16
|
+
# Steering note appended to the tool result fed back to the model once a
|
|
17
|
+
# repeated call crosses the warning threshold. It tells the model that the
|
|
18
|
+
# repetition is not producing new information and to change approach or stop.
|
|
19
|
+
|
|
20
|
+
_SIGNATURE_STEERING_NOTE = (
|
|
21
|
+
'\n\n[Note: you have now run this exact same call '
|
|
22
|
+
'{count} times in this conversation. Running it again will not '
|
|
23
|
+
'change anything. Either change your approach (for example, edit the '
|
|
24
|
+
'code or run a different command) or stop calling tools and give '
|
|
25
|
+
'your final answer.]'
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def canonical_tool_arguments(kwargs: dict, shallow: bool = False) -> str:
|
|
30
|
+
"""Canonical form of validated tool arguments for repeat-call signatures.
|
|
31
|
+
|
|
32
|
+
Whitespace runs in every string value fold to a single space and the dict
|
|
33
|
+
dumps as sorted-key compact JSON, so re-typed calls that differ only in
|
|
34
|
+
line wraps, indentation, or key order share one signature. In the shallow
|
|
35
|
+
level, long string values additionally fold to a length plus head and
|
|
36
|
+
tail fingerprint, keeping the signature cheap at volume.
|
|
37
|
+
"""
|
|
38
|
+
return json.dumps(canonicalize(kwargs, shallow=shallow), sort_keys=True, separators=(',', ':'))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class RepeatGuard(Guard):
|
|
42
|
+
"""Steer a model out of degenerate repeat loops, or fail the loop fast.
|
|
43
|
+
|
|
44
|
+
Counts every executed call, per conversation, against a full signature
|
|
45
|
+
(tool name plus a digest of the exact canonical arguments): arguments are
|
|
46
|
+
canonicalized (whitespace runs folded, sorted-key compact JSON), so a
|
|
47
|
+
re-typed call that differs only in line wraps, indentation, or key order
|
|
48
|
+
shares one signature and accumulates, while different arguments are
|
|
49
|
+
different calls that never accumulate against each other no matter how
|
|
50
|
+
often they happen to produce the same result. A model that keeps
|
|
51
|
+
re-running the same call gets a steering note at the warning threshold
|
|
52
|
+
and stops the loop at the stop threshold.
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
name = 'repeated call'
|
|
56
|
+
|
|
57
|
+
def __init__(
|
|
58
|
+
self,
|
|
59
|
+
warning_threshold: int = REPEATED_CALL_WARNING_THRESHOLD,
|
|
60
|
+
stop_threshold: int = REPEATED_CALL_STOP_THRESHOLD,
|
|
61
|
+
) -> None:
|
|
62
|
+
super().__init__()
|
|
63
|
+
self.warning_threshold = warning_threshold
|
|
64
|
+
self.stop_threshold = stop_threshold
|
|
65
|
+
self._full_counts: dict[str, int] = {}
|
|
66
|
+
|
|
67
|
+
def observe(self, event: GuardEvent) -> GuardOutcome:
|
|
68
|
+
"""Count one executed call and return the correction to apply.
|
|
69
|
+
|
|
70
|
+
The verdict is unchanged below the warning threshold, carries a
|
|
71
|
+
steering note from the warning threshold on, and fails the loop at
|
|
72
|
+
the stop threshold.
|
|
73
|
+
"""
|
|
74
|
+
full_count = self._count_full(event)
|
|
75
|
+
|
|
76
|
+
if full_count >= self.stop_threshold:
|
|
77
|
+
message = (
|
|
78
|
+
f'LLM Tool Service stopped the loop: the model repeated the '
|
|
79
|
+
f'identical tool call "{event.tool_name}" {full_count} times '
|
|
80
|
+
'in this conversation. Running it again will not change '
|
|
81
|
+
'anything, so the loop stopped.'
|
|
82
|
+
)
|
|
83
|
+
return GuardOutcome(failure_message=message)
|
|
84
|
+
|
|
85
|
+
if full_count >= self.warning_threshold:
|
|
86
|
+
self.record_correction(self._full_signature(event))
|
|
87
|
+
return GuardOutcome(
|
|
88
|
+
note=_SIGNATURE_STEERING_NOTE.format(count=full_count),
|
|
89
|
+
verbose_line=f' - {event.tool_name} repeated {full_count}x: steering note added',
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
return GuardOutcome()
|
|
93
|
+
|
|
94
|
+
def _count_full(self, event: GuardEvent) -> int:
|
|
95
|
+
signature = self._full_signature(event)
|
|
96
|
+
count = self._full_counts.get(signature, 0) + 1
|
|
97
|
+
self._full_counts[signature] = count
|
|
98
|
+
return count
|
|
99
|
+
|
|
100
|
+
@staticmethod
|
|
101
|
+
def _full_signature(event: GuardEvent) -> str:
|
|
102
|
+
canonical = canonical_tool_arguments(event.arguments)
|
|
103
|
+
digest = hashlib.sha256(canonical.encode('utf-8')).hexdigest()[:32]
|
|
104
|
+
return f'{event.tool_name}::{digest}'
|
|
@@ -9,6 +9,7 @@ from pydantic.main import IncEx, create_model
|
|
|
9
9
|
from pydantic_core import from_json
|
|
10
10
|
|
|
11
11
|
from dandy.shared.files import read_from_file, write_to_file
|
|
12
|
+
from dandy.shared.hashing import HashMixin
|
|
12
13
|
from dandy.domain.intel.exceptions import IntelCriticalError
|
|
13
14
|
from dandy.domain.intel.annotation import FieldAnnotation
|
|
14
15
|
|
|
@@ -16,7 +17,7 @@ if TYPE_CHECKING:
|
|
|
16
17
|
from pathlib import Path
|
|
17
18
|
|
|
18
19
|
|
|
19
|
-
class BaseIntel(BaseModel, ABC):
|
|
20
|
+
class BaseIntel(HashMixin, BaseModel, ABC):
|
|
20
21
|
@classmethod
|
|
21
22
|
def create_from_file(cls, file_path: Path | str) -> Self:
|
|
22
23
|
return cls.model_validate_json(json_data=read_from_file(file_path=file_path))
|