crca 1.4.0__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.
- .github/ISSUE_TEMPLATE/bug_report.md +65 -0
- .github/ISSUE_TEMPLATE/feature_request.md +41 -0
- .github/PULL_REQUEST_TEMPLATE.md +20 -0
- .github/workflows/publish-manual.yml +61 -0
- .github/workflows/publish.yml +64 -0
- .gitignore +214 -0
- CRCA.py +4156 -0
- LICENSE +201 -0
- MANIFEST.in +43 -0
- PKG-INFO +5035 -0
- README.md +4959 -0
- __init__.py +17 -0
- branches/CRCA-Q.py +2728 -0
- branches/crca_cg/corposwarm.py +9065 -0
- branches/crca_cg/fix_rancher_docker_creds.ps1 +155 -0
- branches/crca_cg/package.json +5 -0
- branches/crca_cg/test_bolt_integration.py +446 -0
- branches/crca_cg/test_corposwarm_comprehensive.py +773 -0
- branches/crca_cg/test_new_features.py +163 -0
- branches/crca_sd/__init__.py +149 -0
- branches/crca_sd/crca_sd_core.py +770 -0
- branches/crca_sd/crca_sd_governance.py +1325 -0
- branches/crca_sd/crca_sd_mpc.py +1130 -0
- branches/crca_sd/crca_sd_realtime.py +1844 -0
- branches/crca_sd/crca_sd_tui.py +1133 -0
- crca-1.4.0.dist-info/METADATA +5035 -0
- crca-1.4.0.dist-info/RECORD +501 -0
- crca-1.4.0.dist-info/WHEEL +4 -0
- crca-1.4.0.dist-info/licenses/LICENSE +201 -0
- docs/CRCA-Q.md +2333 -0
- examples/config.yaml.example +25 -0
- examples/crca_sd_example.py +513 -0
- examples/data_broker_example.py +294 -0
- examples/logistics_corporation.py +861 -0
- examples/palantir_example.py +299 -0
- examples/policy_bench.py +934 -0
- examples/pridnestrovia-sd.py +705 -0
- examples/pridnestrovia_realtime.py +1902 -0
- prompts/__init__.py +10 -0
- prompts/default_crca.py +101 -0
- pyproject.toml +151 -0
- requirements.txt +76 -0
- schemas/__init__.py +43 -0
- schemas/mcpSchemas.py +51 -0
- schemas/policy.py +458 -0
- templates/__init__.py +38 -0
- templates/base_specialized_agent.py +195 -0
- templates/drift_detection.py +325 -0
- templates/examples/causal_agent_template.py +309 -0
- templates/examples/drag_drop_example.py +213 -0
- templates/examples/logistics_agent_template.py +207 -0
- templates/examples/trading_agent_template.py +206 -0
- templates/feature_mixins.py +253 -0
- templates/graph_management.py +442 -0
- templates/llm_integration.py +194 -0
- templates/module_registry.py +276 -0
- templates/mpc_planner.py +280 -0
- templates/policy_loop.py +1168 -0
- templates/prediction_framework.py +448 -0
- templates/statistical_methods.py +778 -0
- tests/sanity.yml +31 -0
- tests/sanity_check +406 -0
- tests/test_core.py +47 -0
- tests/test_crca_excel.py +166 -0
- tests/test_crca_sd.py +780 -0
- tests/test_data_broker.py +424 -0
- tests/test_palantir.py +349 -0
- tools/__init__.py +38 -0
- tools/actuators.py +437 -0
- tools/bolt.diy/Dockerfile +103 -0
- tools/bolt.diy/app/components/@settings/core/AvatarDropdown.tsx +175 -0
- tools/bolt.diy/app/components/@settings/core/ControlPanel.tsx +345 -0
- tools/bolt.diy/app/components/@settings/core/constants.tsx +108 -0
- tools/bolt.diy/app/components/@settings/core/types.ts +114 -0
- tools/bolt.diy/app/components/@settings/index.ts +12 -0
- tools/bolt.diy/app/components/@settings/shared/components/TabTile.tsx +151 -0
- tools/bolt.diy/app/components/@settings/shared/service-integration/ConnectionForm.tsx +193 -0
- tools/bolt.diy/app/components/@settings/shared/service-integration/ConnectionTestIndicator.tsx +60 -0
- tools/bolt.diy/app/components/@settings/shared/service-integration/ErrorState.tsx +102 -0
- tools/bolt.diy/app/components/@settings/shared/service-integration/LoadingState.tsx +94 -0
- tools/bolt.diy/app/components/@settings/shared/service-integration/ServiceHeader.tsx +72 -0
- tools/bolt.diy/app/components/@settings/shared/service-integration/index.ts +6 -0
- tools/bolt.diy/app/components/@settings/tabs/data/DataTab.tsx +721 -0
- tools/bolt.diy/app/components/@settings/tabs/data/DataVisualization.tsx +384 -0
- tools/bolt.diy/app/components/@settings/tabs/event-logs/EventLogsTab.tsx +1013 -0
- tools/bolt.diy/app/components/@settings/tabs/features/FeaturesTab.tsx +295 -0
- tools/bolt.diy/app/components/@settings/tabs/github/GitHubTab.tsx +281 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/GitHubAuthDialog.tsx +173 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/GitHubCacheManager.tsx +367 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/GitHubConnection.tsx +233 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/GitHubErrorBoundary.tsx +105 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/GitHubProgressiveLoader.tsx +266 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/GitHubRepositoryCard.tsx +121 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/GitHubRepositorySelector.tsx +312 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/GitHubStats.tsx +291 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/GitHubUserProfile.tsx +46 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/shared/GitHubStateIndicators.tsx +264 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/shared/RepositoryCard.tsx +361 -0
- tools/bolt.diy/app/components/@settings/tabs/github/components/shared/index.ts +11 -0
- tools/bolt.diy/app/components/@settings/tabs/gitlab/GitLabTab.tsx +305 -0
- tools/bolt.diy/app/components/@settings/tabs/gitlab/components/GitLabAuthDialog.tsx +186 -0
- tools/bolt.diy/app/components/@settings/tabs/gitlab/components/GitLabConnection.tsx +253 -0
- tools/bolt.diy/app/components/@settings/tabs/gitlab/components/GitLabRepositorySelector.tsx +358 -0
- tools/bolt.diy/app/components/@settings/tabs/gitlab/components/RepositoryCard.tsx +79 -0
- tools/bolt.diy/app/components/@settings/tabs/gitlab/components/RepositoryList.tsx +142 -0
- tools/bolt.diy/app/components/@settings/tabs/gitlab/components/StatsDisplay.tsx +91 -0
- tools/bolt.diy/app/components/@settings/tabs/gitlab/components/index.ts +4 -0
- tools/bolt.diy/app/components/@settings/tabs/mcp/McpServerList.tsx +99 -0
- tools/bolt.diy/app/components/@settings/tabs/mcp/McpServerListItem.tsx +70 -0
- tools/bolt.diy/app/components/@settings/tabs/mcp/McpStatusBadge.tsx +37 -0
- tools/bolt.diy/app/components/@settings/tabs/mcp/McpTab.tsx +239 -0
- tools/bolt.diy/app/components/@settings/tabs/netlify/NetlifyTab.tsx +1393 -0
- tools/bolt.diy/app/components/@settings/tabs/netlify/components/NetlifyConnection.tsx +990 -0
- tools/bolt.diy/app/components/@settings/tabs/netlify/components/index.ts +1 -0
- tools/bolt.diy/app/components/@settings/tabs/notifications/NotificationsTab.tsx +300 -0
- tools/bolt.diy/app/components/@settings/tabs/profile/ProfileTab.tsx +181 -0
- tools/bolt.diy/app/components/@settings/tabs/providers/cloud/CloudProvidersTab.tsx +308 -0
- tools/bolt.diy/app/components/@settings/tabs/providers/local/ErrorBoundary.tsx +68 -0
- tools/bolt.diy/app/components/@settings/tabs/providers/local/HealthStatusBadge.tsx +64 -0
- tools/bolt.diy/app/components/@settings/tabs/providers/local/LoadingSkeleton.tsx +107 -0
- tools/bolt.diy/app/components/@settings/tabs/providers/local/LocalProvidersTab.tsx +556 -0
- tools/bolt.diy/app/components/@settings/tabs/providers/local/ModelCard.tsx +106 -0
- tools/bolt.diy/app/components/@settings/tabs/providers/local/ProviderCard.tsx +120 -0
- tools/bolt.diy/app/components/@settings/tabs/providers/local/SetupGuide.tsx +671 -0
- tools/bolt.diy/app/components/@settings/tabs/providers/local/StatusDashboard.tsx +91 -0
- tools/bolt.diy/app/components/@settings/tabs/providers/local/types.ts +44 -0
- tools/bolt.diy/app/components/@settings/tabs/settings/SettingsTab.tsx +215 -0
- tools/bolt.diy/app/components/@settings/tabs/supabase/SupabaseTab.tsx +1089 -0
- tools/bolt.diy/app/components/@settings/tabs/vercel/VercelTab.tsx +909 -0
- tools/bolt.diy/app/components/@settings/tabs/vercel/components/VercelConnection.tsx +368 -0
- tools/bolt.diy/app/components/@settings/tabs/vercel/components/index.ts +1 -0
- tools/bolt.diy/app/components/@settings/utils/tab-helpers.ts +54 -0
- tools/bolt.diy/app/components/chat/APIKeyManager.tsx +169 -0
- tools/bolt.diy/app/components/chat/Artifact.tsx +296 -0
- tools/bolt.diy/app/components/chat/AssistantMessage.tsx +192 -0
- tools/bolt.diy/app/components/chat/BaseChat.module.scss +47 -0
- tools/bolt.diy/app/components/chat/BaseChat.tsx +522 -0
- tools/bolt.diy/app/components/chat/Chat.client.tsx +670 -0
- tools/bolt.diy/app/components/chat/ChatAlert.tsx +108 -0
- tools/bolt.diy/app/components/chat/ChatBox.tsx +334 -0
- tools/bolt.diy/app/components/chat/CodeBlock.module.scss +10 -0
- tools/bolt.diy/app/components/chat/CodeBlock.tsx +85 -0
- tools/bolt.diy/app/components/chat/DicussMode.tsx +17 -0
- tools/bolt.diy/app/components/chat/ExamplePrompts.tsx +37 -0
- tools/bolt.diy/app/components/chat/FilePreview.tsx +38 -0
- tools/bolt.diy/app/components/chat/GitCloneButton.tsx +327 -0
- tools/bolt.diy/app/components/chat/ImportFolderButton.tsx +141 -0
- tools/bolt.diy/app/components/chat/LLMApiAlert.tsx +109 -0
- tools/bolt.diy/app/components/chat/MCPTools.tsx +129 -0
- tools/bolt.diy/app/components/chat/Markdown.module.scss +171 -0
- tools/bolt.diy/app/components/chat/Markdown.spec.ts +48 -0
- tools/bolt.diy/app/components/chat/Markdown.tsx +252 -0
- tools/bolt.diy/app/components/chat/Messages.client.tsx +102 -0
- tools/bolt.diy/app/components/chat/ModelSelector.tsx +797 -0
- tools/bolt.diy/app/components/chat/NetlifyDeploymentLink.client.tsx +51 -0
- tools/bolt.diy/app/components/chat/ProgressCompilation.tsx +110 -0
- tools/bolt.diy/app/components/chat/ScreenshotStateManager.tsx +33 -0
- tools/bolt.diy/app/components/chat/SendButton.client.tsx +39 -0
- tools/bolt.diy/app/components/chat/SpeechRecognition.tsx +28 -0
- tools/bolt.diy/app/components/chat/StarterTemplates.tsx +38 -0
- tools/bolt.diy/app/components/chat/SupabaseAlert.tsx +199 -0
- tools/bolt.diy/app/components/chat/SupabaseConnection.tsx +339 -0
- tools/bolt.diy/app/components/chat/ThoughtBox.tsx +43 -0
- tools/bolt.diy/app/components/chat/ToolInvocations.tsx +409 -0
- tools/bolt.diy/app/components/chat/UserMessage.tsx +101 -0
- tools/bolt.diy/app/components/chat/VercelDeploymentLink.client.tsx +158 -0
- tools/bolt.diy/app/components/chat/chatExportAndImport/ExportChatButton.tsx +49 -0
- tools/bolt.diy/app/components/chat/chatExportAndImport/ImportButtons.tsx +96 -0
- tools/bolt.diy/app/components/deploy/DeployAlert.tsx +197 -0
- tools/bolt.diy/app/components/deploy/DeployButton.tsx +277 -0
- tools/bolt.diy/app/components/deploy/GitHubDeploy.client.tsx +171 -0
- tools/bolt.diy/app/components/deploy/GitHubDeploymentDialog.tsx +1041 -0
- tools/bolt.diy/app/components/deploy/GitLabDeploy.client.tsx +171 -0
- tools/bolt.diy/app/components/deploy/GitLabDeploymentDialog.tsx +764 -0
- tools/bolt.diy/app/components/deploy/NetlifyDeploy.client.tsx +246 -0
- tools/bolt.diy/app/components/deploy/VercelDeploy.client.tsx +235 -0
- tools/bolt.diy/app/components/editor/codemirror/BinaryContent.tsx +7 -0
- tools/bolt.diy/app/components/editor/codemirror/CodeMirrorEditor.tsx +555 -0
- tools/bolt.diy/app/components/editor/codemirror/EnvMasking.ts +80 -0
- tools/bolt.diy/app/components/editor/codemirror/cm-theme.ts +192 -0
- tools/bolt.diy/app/components/editor/codemirror/indent.ts +68 -0
- tools/bolt.diy/app/components/editor/codemirror/languages.ts +112 -0
- tools/bolt.diy/app/components/git/GitUrlImport.client.tsx +147 -0
- tools/bolt.diy/app/components/header/Header.tsx +42 -0
- tools/bolt.diy/app/components/header/HeaderActionButtons.client.tsx +54 -0
- tools/bolt.diy/app/components/mandate/MandateSubmission.tsx +167 -0
- tools/bolt.diy/app/components/observability/DeploymentStatus.tsx +168 -0
- tools/bolt.diy/app/components/observability/EventTimeline.tsx +119 -0
- tools/bolt.diy/app/components/observability/FileDiffViewer.tsx +121 -0
- tools/bolt.diy/app/components/observability/GovernanceStatus.tsx +197 -0
- tools/bolt.diy/app/components/observability/GovernorMetrics.tsx +246 -0
- tools/bolt.diy/app/components/observability/LogStream.tsx +244 -0
- tools/bolt.diy/app/components/observability/MandateDetails.tsx +201 -0
- tools/bolt.diy/app/components/observability/ObservabilityDashboard.tsx +200 -0
- tools/bolt.diy/app/components/sidebar/HistoryItem.tsx +187 -0
- tools/bolt.diy/app/components/sidebar/Menu.client.tsx +536 -0
- tools/bolt.diy/app/components/sidebar/date-binning.ts +59 -0
- tools/bolt.diy/app/components/txt +1 -0
- tools/bolt.diy/app/components/ui/BackgroundRays/index.tsx +18 -0
- tools/bolt.diy/app/components/ui/BackgroundRays/styles.module.scss +246 -0
- tools/bolt.diy/app/components/ui/Badge.tsx +53 -0
- tools/bolt.diy/app/components/ui/BranchSelector.tsx +270 -0
- tools/bolt.diy/app/components/ui/Breadcrumbs.tsx +101 -0
- tools/bolt.diy/app/components/ui/Button.tsx +46 -0
- tools/bolt.diy/app/components/ui/Card.tsx +55 -0
- tools/bolt.diy/app/components/ui/Checkbox.tsx +32 -0
- tools/bolt.diy/app/components/ui/CloseButton.tsx +49 -0
- tools/bolt.diy/app/components/ui/CodeBlock.tsx +103 -0
- tools/bolt.diy/app/components/ui/Collapsible.tsx +9 -0
- tools/bolt.diy/app/components/ui/ColorSchemeDialog.tsx +378 -0
- tools/bolt.diy/app/components/ui/Dialog.tsx +449 -0
- tools/bolt.diy/app/components/ui/Dropdown.tsx +63 -0
- tools/bolt.diy/app/components/ui/EmptyState.tsx +154 -0
- tools/bolt.diy/app/components/ui/FileIcon.tsx +346 -0
- tools/bolt.diy/app/components/ui/FilterChip.tsx +92 -0
- tools/bolt.diy/app/components/ui/GlowingEffect.tsx +192 -0
- tools/bolt.diy/app/components/ui/GradientCard.tsx +100 -0
- tools/bolt.diy/app/components/ui/IconButton.tsx +84 -0
- tools/bolt.diy/app/components/ui/Input.tsx +22 -0
- tools/bolt.diy/app/components/ui/Label.tsx +20 -0
- tools/bolt.diy/app/components/ui/LoadingDots.tsx +27 -0
- tools/bolt.diy/app/components/ui/LoadingOverlay.tsx +32 -0
- tools/bolt.diy/app/components/ui/PanelHeader.tsx +20 -0
- tools/bolt.diy/app/components/ui/PanelHeaderButton.tsx +36 -0
- tools/bolt.diy/app/components/ui/Popover.tsx +29 -0
- tools/bolt.diy/app/components/ui/Progress.tsx +22 -0
- tools/bolt.diy/app/components/ui/RepositoryStats.tsx +87 -0
- tools/bolt.diy/app/components/ui/ScrollArea.tsx +41 -0
- tools/bolt.diy/app/components/ui/SearchInput.tsx +80 -0
- tools/bolt.diy/app/components/ui/SearchResultItem.tsx +134 -0
- tools/bolt.diy/app/components/ui/Separator.tsx +22 -0
- tools/bolt.diy/app/components/ui/SettingsButton.tsx +35 -0
- tools/bolt.diy/app/components/ui/Slider.tsx +73 -0
- tools/bolt.diy/app/components/ui/StatusIndicator.tsx +90 -0
- tools/bolt.diy/app/components/ui/Switch.tsx +37 -0
- tools/bolt.diy/app/components/ui/Tabs.tsx +52 -0
- tools/bolt.diy/app/components/ui/TabsWithSlider.tsx +112 -0
- tools/bolt.diy/app/components/ui/ThemeSwitch.tsx +29 -0
- tools/bolt.diy/app/components/ui/Tooltip.tsx +122 -0
- tools/bolt.diy/app/components/ui/index.ts +38 -0
- tools/bolt.diy/app/components/ui/use-toast.ts +66 -0
- tools/bolt.diy/app/components/workbench/DiffView.tsx +796 -0
- tools/bolt.diy/app/components/workbench/EditorPanel.tsx +174 -0
- tools/bolt.diy/app/components/workbench/ExpoQrModal.tsx +55 -0
- tools/bolt.diy/app/components/workbench/FileBreadcrumb.tsx +150 -0
- tools/bolt.diy/app/components/workbench/FileTree.tsx +565 -0
- tools/bolt.diy/app/components/workbench/Inspector.tsx +126 -0
- tools/bolt.diy/app/components/workbench/InspectorPanel.tsx +146 -0
- tools/bolt.diy/app/components/workbench/LockManager.tsx +262 -0
- tools/bolt.diy/app/components/workbench/PortDropdown.tsx +91 -0
- tools/bolt.diy/app/components/workbench/Preview.tsx +1049 -0
- tools/bolt.diy/app/components/workbench/ScreenshotSelector.tsx +293 -0
- tools/bolt.diy/app/components/workbench/Search.tsx +257 -0
- tools/bolt.diy/app/components/workbench/Workbench.client.tsx +506 -0
- tools/bolt.diy/app/components/workbench/terminal/Terminal.tsx +131 -0
- tools/bolt.diy/app/components/workbench/terminal/TerminalManager.tsx +68 -0
- tools/bolt.diy/app/components/workbench/terminal/TerminalTabs.tsx +277 -0
- tools/bolt.diy/app/components/workbench/terminal/theme.ts +36 -0
- tools/bolt.diy/app/components/workflow/WorkflowPhase.tsx +109 -0
- tools/bolt.diy/app/components/workflow/WorkflowStatus.tsx +60 -0
- tools/bolt.diy/app/components/workflow/WorkflowTimeline.tsx +150 -0
- tools/bolt.diy/app/entry.client.tsx +7 -0
- tools/bolt.diy/app/entry.server.tsx +80 -0
- tools/bolt.diy/app/root.tsx +156 -0
- tools/bolt.diy/app/routes/_index.tsx +175 -0
- tools/bolt.diy/app/routes/api.bug-report.ts +254 -0
- tools/bolt.diy/app/routes/api.chat.ts +463 -0
- tools/bolt.diy/app/routes/api.check-env-key.ts +41 -0
- tools/bolt.diy/app/routes/api.configured-providers.ts +110 -0
- tools/bolt.diy/app/routes/api.corporate-swarm-status.ts +55 -0
- tools/bolt.diy/app/routes/api.enhancer.ts +137 -0
- tools/bolt.diy/app/routes/api.export-api-keys.ts +44 -0
- tools/bolt.diy/app/routes/api.git-info.ts +69 -0
- tools/bolt.diy/app/routes/api.git-proxy.$.ts +178 -0
- tools/bolt.diy/app/routes/api.github-branches.ts +166 -0
- tools/bolt.diy/app/routes/api.github-deploy.ts +67 -0
- tools/bolt.diy/app/routes/api.github-stats.ts +198 -0
- tools/bolt.diy/app/routes/api.github-template.ts +242 -0
- tools/bolt.diy/app/routes/api.github-user.ts +287 -0
- tools/bolt.diy/app/routes/api.gitlab-branches.ts +143 -0
- tools/bolt.diy/app/routes/api.gitlab-deploy.ts +67 -0
- tools/bolt.diy/app/routes/api.gitlab-projects.ts +105 -0
- tools/bolt.diy/app/routes/api.health.ts +8 -0
- tools/bolt.diy/app/routes/api.llmcall.ts +298 -0
- tools/bolt.diy/app/routes/api.mandate.ts +351 -0
- tools/bolt.diy/app/routes/api.mcp-check.ts +16 -0
- tools/bolt.diy/app/routes/api.mcp-update-config.ts +23 -0
- tools/bolt.diy/app/routes/api.models.$provider.ts +2 -0
- tools/bolt.diy/app/routes/api.models.ts +90 -0
- tools/bolt.diy/app/routes/api.netlify-deploy.ts +240 -0
- tools/bolt.diy/app/routes/api.netlify-user.ts +142 -0
- tools/bolt.diy/app/routes/api.supabase-user.ts +199 -0
- tools/bolt.diy/app/routes/api.supabase.query.ts +92 -0
- tools/bolt.diy/app/routes/api.supabase.ts +56 -0
- tools/bolt.diy/app/routes/api.supabase.variables.ts +32 -0
- tools/bolt.diy/app/routes/api.system.diagnostics.ts +142 -0
- tools/bolt.diy/app/routes/api.system.disk-info.ts +311 -0
- tools/bolt.diy/app/routes/api.system.git-info.ts +332 -0
- tools/bolt.diy/app/routes/api.update.ts +21 -0
- tools/bolt.diy/app/routes/api.vercel-deploy.ts +497 -0
- tools/bolt.diy/app/routes/api.vercel-user.ts +161 -0
- tools/bolt.diy/app/routes/api.workflow-status.$proposalId.ts +309 -0
- tools/bolt.diy/app/routes/chat.$id.tsx +8 -0
- tools/bolt.diy/app/routes/execute.$mandateId.tsx +432 -0
- tools/bolt.diy/app/routes/git.tsx +25 -0
- tools/bolt.diy/app/routes/observability.$mandateId.tsx +50 -0
- tools/bolt.diy/app/routes/webcontainer.connect.$id.tsx +32 -0
- tools/bolt.diy/app/routes/webcontainer.preview.$id.tsx +97 -0
- tools/bolt.diy/app/routes/workflow.$proposalId.tsx +170 -0
- tools/bolt.diy/app/styles/animations.scss +49 -0
- tools/bolt.diy/app/styles/components/code.scss +9 -0
- tools/bolt.diy/app/styles/components/editor.scss +135 -0
- tools/bolt.diy/app/styles/components/resize-handle.scss +30 -0
- tools/bolt.diy/app/styles/components/terminal.scss +3 -0
- tools/bolt.diy/app/styles/components/toast.scss +23 -0
- tools/bolt.diy/app/styles/diff-view.css +72 -0
- tools/bolt.diy/app/styles/index.scss +73 -0
- tools/bolt.diy/app/styles/variables.scss +255 -0
- tools/bolt.diy/app/styles/z-index.scss +37 -0
- tools/bolt.diy/app/types/GitHub.ts +182 -0
- tools/bolt.diy/app/types/GitLab.ts +103 -0
- tools/bolt.diy/app/types/actions.ts +85 -0
- tools/bolt.diy/app/types/artifact.ts +5 -0
- tools/bolt.diy/app/types/context.ts +26 -0
- tools/bolt.diy/app/types/design-scheme.ts +93 -0
- tools/bolt.diy/app/types/global.d.ts +13 -0
- tools/bolt.diy/app/types/mandate.ts +333 -0
- tools/bolt.diy/app/types/model.ts +25 -0
- tools/bolt.diy/app/types/netlify.ts +94 -0
- tools/bolt.diy/app/types/supabase.ts +54 -0
- tools/bolt.diy/app/types/template.ts +8 -0
- tools/bolt.diy/app/types/terminal.ts +9 -0
- tools/bolt.diy/app/types/theme.ts +1 -0
- tools/bolt.diy/app/types/vercel.ts +67 -0
- tools/bolt.diy/app/utils/buffer.ts +29 -0
- tools/bolt.diy/app/utils/classNames.ts +65 -0
- tools/bolt.diy/app/utils/constants.ts +147 -0
- tools/bolt.diy/app/utils/debounce.ts +13 -0
- tools/bolt.diy/app/utils/debugLogger.ts +1284 -0
- tools/bolt.diy/app/utils/diff.spec.ts +11 -0
- tools/bolt.diy/app/utils/diff.ts +117 -0
- tools/bolt.diy/app/utils/easings.ts +3 -0
- tools/bolt.diy/app/utils/fileLocks.ts +96 -0
- tools/bolt.diy/app/utils/fileUtils.ts +121 -0
- tools/bolt.diy/app/utils/folderImport.ts +73 -0
- tools/bolt.diy/app/utils/formatSize.ts +12 -0
- tools/bolt.diy/app/utils/getLanguageFromExtension.ts +24 -0
- tools/bolt.diy/app/utils/githubStats.ts +9 -0
- tools/bolt.diy/app/utils/gitlabStats.ts +54 -0
- tools/bolt.diy/app/utils/logger.ts +162 -0
- tools/bolt.diy/app/utils/markdown.ts +155 -0
- tools/bolt.diy/app/utils/mobile.ts +4 -0
- tools/bolt.diy/app/utils/os.ts +4 -0
- tools/bolt.diy/app/utils/path.ts +19 -0
- tools/bolt.diy/app/utils/projectCommands.ts +197 -0
- tools/bolt.diy/app/utils/promises.ts +19 -0
- tools/bolt.diy/app/utils/react.ts +6 -0
- tools/bolt.diy/app/utils/sampler.ts +49 -0
- tools/bolt.diy/app/utils/selectStarterTemplate.ts +255 -0
- tools/bolt.diy/app/utils/shell.ts +384 -0
- tools/bolt.diy/app/utils/stacktrace.ts +27 -0
- tools/bolt.diy/app/utils/stripIndent.ts +23 -0
- tools/bolt.diy/app/utils/terminal.ts +11 -0
- tools/bolt.diy/app/utils/unreachable.ts +3 -0
- tools/bolt.diy/app/vite-env.d.ts +2 -0
- tools/bolt.diy/assets/entitlements.mac.plist +25 -0
- tools/bolt.diy/assets/icons/icon.icns +0 -0
- tools/bolt.diy/assets/icons/icon.ico +0 -0
- tools/bolt.diy/assets/icons/icon.png +0 -0
- tools/bolt.diy/bindings.js +78 -0
- tools/bolt.diy/bindings.sh +33 -0
- tools/bolt.diy/docker-compose.yaml +145 -0
- tools/bolt.diy/electron/main/index.ts +201 -0
- tools/bolt.diy/electron/main/tsconfig.json +30 -0
- tools/bolt.diy/electron/main/ui/menu.ts +29 -0
- tools/bolt.diy/electron/main/ui/window.ts +54 -0
- tools/bolt.diy/electron/main/utils/auto-update.ts +110 -0
- tools/bolt.diy/electron/main/utils/constants.ts +4 -0
- tools/bolt.diy/electron/main/utils/cookie.ts +40 -0
- tools/bolt.diy/electron/main/utils/reload.ts +35 -0
- tools/bolt.diy/electron/main/utils/serve.ts +71 -0
- tools/bolt.diy/electron/main/utils/store.ts +3 -0
- tools/bolt.diy/electron/main/utils/vite-server.ts +44 -0
- tools/bolt.diy/electron/main/vite.config.ts +44 -0
- tools/bolt.diy/electron/preload/index.ts +22 -0
- tools/bolt.diy/electron/preload/tsconfig.json +7 -0
- tools/bolt.diy/electron/preload/vite.config.ts +31 -0
- tools/bolt.diy/electron-builder.yml +64 -0
- tools/bolt.diy/electron-update.yml +4 -0
- tools/bolt.diy/eslint.config.mjs +57 -0
- tools/bolt.diy/functions/[[path]].ts +12 -0
- tools/bolt.diy/icons/angular.svg +1 -0
- tools/bolt.diy/icons/astro.svg +8 -0
- tools/bolt.diy/icons/chat.svg +1 -0
- tools/bolt.diy/icons/expo-brand.svg +1 -0
- tools/bolt.diy/icons/expo.svg +4 -0
- tools/bolt.diy/icons/logo-text.svg +1 -0
- tools/bolt.diy/icons/logo.svg +4 -0
- tools/bolt.diy/icons/mcp.svg +1 -0
- tools/bolt.diy/icons/nativescript.svg +1 -0
- tools/bolt.diy/icons/netlify.svg +10 -0
- tools/bolt.diy/icons/nextjs.svg +1 -0
- tools/bolt.diy/icons/nuxt.svg +1 -0
- tools/bolt.diy/icons/qwik.svg +1 -0
- tools/bolt.diy/icons/react.svg +1 -0
- tools/bolt.diy/icons/remix.svg +24 -0
- tools/bolt.diy/icons/remotion.svg +1 -0
- tools/bolt.diy/icons/shadcn.svg +21 -0
- tools/bolt.diy/icons/slidev.svg +60 -0
- tools/bolt.diy/icons/solidjs.svg +1 -0
- tools/bolt.diy/icons/stars.svg +1 -0
- tools/bolt.diy/icons/svelte.svg +1 -0
- tools/bolt.diy/icons/typescript.svg +1 -0
- tools/bolt.diy/icons/vite.svg +1 -0
- tools/bolt.diy/icons/vue.svg +1 -0
- tools/bolt.diy/load-context.ts +9 -0
- tools/bolt.diy/notarize.cjs +31 -0
- tools/bolt.diy/package.json +218 -0
- tools/bolt.diy/playwright.config.preview.ts +35 -0
- tools/bolt.diy/pre-start.cjs +26 -0
- tools/bolt.diy/public/apple-touch-icon-precomposed.png +0 -0
- tools/bolt.diy/public/apple-touch-icon.png +0 -0
- tools/bolt.diy/public/favicon.ico +0 -0
- tools/bolt.diy/public/favicon.svg +4 -0
- tools/bolt.diy/public/icons/AmazonBedrock.svg +1 -0
- tools/bolt.diy/public/icons/Anthropic.svg +4 -0
- tools/bolt.diy/public/icons/Cohere.svg +4 -0
- tools/bolt.diy/public/icons/Deepseek.svg +5 -0
- tools/bolt.diy/public/icons/Default.svg +4 -0
- tools/bolt.diy/public/icons/Google.svg +4 -0
- tools/bolt.diy/public/icons/Groq.svg +4 -0
- tools/bolt.diy/public/icons/HuggingFace.svg +4 -0
- tools/bolt.diy/public/icons/Hyperbolic.svg +3 -0
- tools/bolt.diy/public/icons/LMStudio.svg +5 -0
- tools/bolt.diy/public/icons/Mistral.svg +4 -0
- tools/bolt.diy/public/icons/Ollama.svg +4 -0
- tools/bolt.diy/public/icons/OpenAI.svg +4 -0
- tools/bolt.diy/public/icons/OpenAILike.svg +4 -0
- tools/bolt.diy/public/icons/OpenRouter.svg +4 -0
- tools/bolt.diy/public/icons/Perplexity.svg +4 -0
- tools/bolt.diy/public/icons/Together.svg +4 -0
- tools/bolt.diy/public/icons/xAI.svg +5 -0
- tools/bolt.diy/public/inspector-script.js +292 -0
- tools/bolt.diy/public/logo-dark-styled.png +0 -0
- tools/bolt.diy/public/logo-dark.png +0 -0
- tools/bolt.diy/public/logo-light-styled.png +0 -0
- tools/bolt.diy/public/logo-light.png +0 -0
- tools/bolt.diy/public/logo.svg +15 -0
- tools/bolt.diy/public/social_preview_index.jpg +0 -0
- tools/bolt.diy/scripts/clean.js +45 -0
- tools/bolt.diy/scripts/electron-dev.mjs +181 -0
- tools/bolt.diy/scripts/setup-env.sh +41 -0
- tools/bolt.diy/scripts/update-imports.sh +7 -0
- tools/bolt.diy/scripts/update.sh +52 -0
- tools/bolt.diy/services/execution-governor/Dockerfile +41 -0
- tools/bolt.diy/services/execution-governor/config.ts +42 -0
- tools/bolt.diy/services/execution-governor/index.ts +683 -0
- tools/bolt.diy/services/execution-governor/metrics.ts +141 -0
- tools/bolt.diy/services/execution-governor/package.json +31 -0
- tools/bolt.diy/services/execution-governor/priority-queue.ts +139 -0
- tools/bolt.diy/services/execution-governor/tsconfig.json +21 -0
- tools/bolt.diy/services/execution-governor/types.ts +145 -0
- tools/bolt.diy/services/headless-executor/Dockerfile +43 -0
- tools/bolt.diy/services/headless-executor/executor.ts +210 -0
- tools/bolt.diy/services/headless-executor/index.ts +323 -0
- tools/bolt.diy/services/headless-executor/package.json +27 -0
- tools/bolt.diy/services/headless-executor/tsconfig.json +21 -0
- tools/bolt.diy/services/headless-executor/types.ts +38 -0
- tools/bolt.diy/test-workflows.sh +240 -0
- tools/bolt.diy/tests/integration/corporate-swarm.test.ts +208 -0
- tools/bolt.diy/tests/mandates/budget-limited.json +34 -0
- tools/bolt.diy/tests/mandates/complex.json +53 -0
- tools/bolt.diy/tests/mandates/constraint-enforced.json +36 -0
- tools/bolt.diy/tests/mandates/simple.json +35 -0
- tools/bolt.diy/tsconfig.json +37 -0
- tools/bolt.diy/types/istextorbinary.d.ts +15 -0
- tools/bolt.diy/uno.config.ts +279 -0
- tools/bolt.diy/vite-electron.config.ts +76 -0
- tools/bolt.diy/vite.config.ts +112 -0
- tools/bolt.diy/worker-configuration.d.ts +22 -0
- tools/bolt.diy/wrangler.toml +6 -0
- tools/code_generator.py +461 -0
- tools/file_operations.py +465 -0
- tools/mandate_generator.py +337 -0
- tools/mcpClientUtils.py +1216 -0
- tools/sensors.py +285 -0
- utils/Agent_types.py +15 -0
- utils/AnyToStr.py +0 -0
- utils/HHCS.py +277 -0
- utils/__init__.py +30 -0
- utils/agent.py +3627 -0
- utils/aop.py +2948 -0
- utils/canonical.py +143 -0
- utils/conversation.py +1195 -0
- utils/doctrine_versioning +230 -0
- utils/formatter.py +474 -0
- utils/ledger.py +311 -0
- utils/out_types.py +16 -0
- utils/rollback.py +339 -0
- utils/router.py +929 -0
- utils/tui.py +1908 -0
README.md
ADDED
|
@@ -0,0 +1,4959 @@
|
|
|
1
|
+
# CR-CA: Causal Reasoning and Counterfactual Analysis Framework
|
|
2
|
+
|
|
3
|
+
**Version**: v1.4.0
|
|
4
|
+
**Repository**: [https://github.com/IlumCI/CR-CA](https://github.com/IlumCI/CR-CA)
|
|
5
|
+
**License**: Apache-2.0
|
|
6
|
+
|
|
7
|
+
CR-CA is a comprehensive framework for causal reasoning and counterfactual analysis, combining structural causal models (SCMs) with large language model (LLM) integration. The framework enables sophisticated causal inference, automatic variable extraction from natural language, deterministic causal simulation, and comprehensive counterfactual scenario generation. Designed for both research and practical applications, CR-CA provides a modular architecture supporting specialized agents for quantitative trading, socioeconomic dynamics, and corporate governance.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Quick Reference Tables
|
|
12
|
+
|
|
13
|
+
### Feature Matrix
|
|
14
|
+
|
|
15
|
+
| Feature | Core CRCAAgent | CRCA-SD | CRCA-CG | CRCA-Q |
|
|
16
|
+
|---------|----------------|---------|---------|--------|
|
|
17
|
+
| Automatic Variable Extraction | ✓ | ✓ | ✓ | ✓ |
|
|
18
|
+
| LLM-Based Causal Analysis | ✓ | ✓ | ✓ | ✓ |
|
|
19
|
+
| Deterministic Simulation | ✓ | ✓ | - | ✓ |
|
|
20
|
+
| Counterfactual Generation | ✓ | ✓ | ✓ | ✓ |
|
|
21
|
+
| Batch Prediction | ✓ | - | - | ✓ |
|
|
22
|
+
| Async Operations | ✓ | - | - | ✓ |
|
|
23
|
+
| Optimization Methods | ✓ | ✓ | - | ✓ |
|
|
24
|
+
| Time-Series Analysis | ✓ | - | - | ✓ |
|
|
25
|
+
| Bayesian Inference | ✓ | - | - | ✓ |
|
|
26
|
+
| Data Source Integration | - | - | - | ✓ |
|
|
27
|
+
|
|
28
|
+
### Module Overview
|
|
29
|
+
|
|
30
|
+
| Module | Purpose | Key Components |
|
|
31
|
+
|--------|---------|----------------|
|
|
32
|
+
| `CRCA.py` | Core causal reasoning agent | CRCAAgent, automatic extraction, dual-mode operation |
|
|
33
|
+
| `utils/` | Utility functions and extensions | Formatter, TUI, router, conversation management |
|
|
34
|
+
| `tools/` | External tool integration | MCP client utilities, protocol handling |
|
|
35
|
+
| `templates/` | Agent creation framework | Base agents, feature mixins, module registry |
|
|
36
|
+
| `schemas/` | Type definitions | MCP schemas, data structures |
|
|
37
|
+
| `crca_sd/` | Socioeconomic dynamics | MPC solver, governance, real-time monitoring |
|
|
38
|
+
| `crca_cg/` | Corporate governance | CorporateSwarm, ESG scoring, multi-agent orchestration |
|
|
39
|
+
| `branches/CRCA-Q.py` | Quantitative trading | QuantTradingAgent, causal signals, portfolio optimization |
|
|
40
|
+
|
|
41
|
+
### Dependency Matrix
|
|
42
|
+
|
|
43
|
+
| Dependency | Core | Advanced Analysis | CRCA-SD | CRCA-CG | CRCA-Q |
|
|
44
|
+
|------------|------|-------------------|---------|---------|--------|
|
|
45
|
+
| numpy | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
46
|
+
| rustworkx | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
47
|
+
| swarms | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
48
|
+
| litellm | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
49
|
+
| pandas | - | ✓ | ✓ | ✓ | ✓ |
|
|
50
|
+
| scipy | - | ✓ | ✓ | - | ✓ |
|
|
51
|
+
| loguru | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
52
|
+
| pydantic | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
53
|
+
| mcp | ✓ | ✓ | - | - | - |
|
|
54
|
+
| openai | ✓ | ✓ | - | - | - |
|
|
55
|
+
| ccxt | - | - | - | - | ✓ |
|
|
56
|
+
| web3 | - | - | - | - | ✓ |
|
|
57
|
+
| curses | - | - | ✓ | - | - |
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
## Table of Contents
|
|
62
|
+
|
|
63
|
+
1. [Installation & Setup](#installation--setup)
|
|
64
|
+
2. [Core CRCAAgent](#core-crcaagent)
|
|
65
|
+
3. [Automatic Variable Extraction](#automatic-variable-extraction)
|
|
66
|
+
4. [Module Documentation](#module-documentation)
|
|
67
|
+
- [Utils Module](#utils-module)
|
|
68
|
+
- [Tools Module](#tools-module)
|
|
69
|
+
- [Templates Module](#templates-module)
|
|
70
|
+
- [Schemas Module](#schemas-module)
|
|
71
|
+
- [CRCA-SD Module](#crca-sd-module)
|
|
72
|
+
- [CRCA-CG Module](#crca-cg-module)
|
|
73
|
+
5. [Policy Engine System](#policy-engine-system)
|
|
74
|
+
6. [Branch Integrations](#branch-integrations)
|
|
75
|
+
- [CRCA-Q Integration](#crca-q-integration)
|
|
76
|
+
- [CRCA-SD Integration](#crca-sd-integration)
|
|
77
|
+
- [CRCA-CG Integration](#crca-cg-integration)
|
|
78
|
+
7. [Usage Examples](#usage-examples)
|
|
79
|
+
8. [Advanced Features](#advanced-features)
|
|
80
|
+
9. [Testing](#testing)
|
|
81
|
+
10. [CI/CD Workflows](#cicd-workflows)
|
|
82
|
+
11. [Project Structure](#project-structure)
|
|
83
|
+
12. [Architecture Diagrams](#architecture-diagrams)
|
|
84
|
+
13. [Integration Examples](#integration-examples)
|
|
85
|
+
14. [API Reference](#api-reference)
|
|
86
|
+
15. [Architecture & Design](#architecture--design)
|
|
87
|
+
16. [Research & Theory](#research--theory)
|
|
88
|
+
17. [Configuration & Customization](#configuration--customization)
|
|
89
|
+
18. [Troubleshooting](#troubleshooting)
|
|
90
|
+
19. [Contributing](#contributing)
|
|
91
|
+
20. [Changelog](#changelog)
|
|
92
|
+
21. [References & Resources](#references--resources)
|
|
93
|
+
22. [Bolt.diy Web Application (WIP)](#boltdiy-web-application-wip)
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## Installation & Setup
|
|
98
|
+
|
|
99
|
+
### Prerequisites
|
|
100
|
+
|
|
101
|
+
- Python 3.10 or higher
|
|
102
|
+
- pip package manager
|
|
103
|
+
- (Optional) Git for cloning the repository
|
|
104
|
+
- (Optional) Docker for containerized deployment
|
|
105
|
+
|
|
106
|
+
### Quick Install
|
|
107
|
+
|
|
108
|
+
**Method 1: From PyPI (Recommended)**
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
pip install crca
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Method 2: From Source**
|
|
115
|
+
|
|
116
|
+
Clone the repository:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
git clone https://github.com/IlumCI/CR-CA.git
|
|
120
|
+
cd CR-CA
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Install dependencies:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
pip install -r requirements.txt
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
**Method 3: Development Install**
|
|
130
|
+
|
|
131
|
+
For development with editable install:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
git clone https://github.com/IlumCI/CR-CA.git
|
|
135
|
+
cd CR-CA
|
|
136
|
+
pip install -e ".[dev]"
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Optional Dependencies
|
|
140
|
+
|
|
141
|
+
**For Policy Engine:**
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
# Install policy engine dependencies
|
|
145
|
+
pip install cvxpy ruptures psutil
|
|
146
|
+
|
|
147
|
+
# Or install all optional dependencies
|
|
148
|
+
pip install crca[policy-engine]
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**For CRCA-SD:**
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
# CRCA-SD dependencies are included in requirements.txt
|
|
155
|
+
# Additional dependencies may be needed for specific features
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
**For CRCA-CG:**
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
# Install TUI dependencies (optional)
|
|
162
|
+
pip install rich
|
|
163
|
+
|
|
164
|
+
# Or install all optional dependencies
|
|
165
|
+
pip install crca[tui]
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
**For CRCA-Q:**
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
# Install trading dependencies
|
|
172
|
+
pip install ccxt web3
|
|
173
|
+
|
|
174
|
+
# Or install all optional dependencies
|
|
175
|
+
pip install crca[trading]
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Environment Variables
|
|
179
|
+
|
|
180
|
+
Create a `.env` file in the project root with required API keys:
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
# Required for LLM functionality
|
|
184
|
+
OPENAI_API_KEY=your_openai_api_key_here
|
|
185
|
+
|
|
186
|
+
# Optional: For CRCA-Q trading
|
|
187
|
+
KRAKEN_API_KEY=your_kraken_key_here
|
|
188
|
+
KRAKEN_API_SECRET=your_kraken_secret_here
|
|
189
|
+
|
|
190
|
+
# Optional: For alternative data sources
|
|
191
|
+
TWITTER_BEARER_TOKEN=your_twitter_token_here
|
|
192
|
+
ETHERSCAN_API_KEY=your_etherscan_key_here
|
|
193
|
+
THEGRAPH_API_KEY=your_thegraph_key_here
|
|
194
|
+
|
|
195
|
+
# Optional: For MCP servers
|
|
196
|
+
MCP_SERVER_URL=http://localhost:8000
|
|
197
|
+
MCP_API_KEY=your_mcp_api_key_here
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Docker Setup (Optional)
|
|
201
|
+
|
|
202
|
+
**Example: Dockerfile for Policy Engine**
|
|
203
|
+
|
|
204
|
+
```dockerfile
|
|
205
|
+
FROM python:3.11-slim
|
|
206
|
+
|
|
207
|
+
WORKDIR /app
|
|
208
|
+
|
|
209
|
+
# Install system dependencies
|
|
210
|
+
RUN apt-get update && apt-get install -y \
|
|
211
|
+
gcc \
|
|
212
|
+
g++ \
|
|
213
|
+
&& rm -rf /var/lib/apt/lists/*
|
|
214
|
+
|
|
215
|
+
# Copy requirements
|
|
216
|
+
COPY requirements.txt .
|
|
217
|
+
RUN pip install --no-cache-dir -r requirements.txt
|
|
218
|
+
|
|
219
|
+
# Install policy engine dependencies
|
|
220
|
+
RUN pip install cvxpy ruptures psutil
|
|
221
|
+
|
|
222
|
+
# Copy application
|
|
223
|
+
COPY . .
|
|
224
|
+
|
|
225
|
+
# Set environment variables
|
|
226
|
+
ENV PYTHONUNBUFFERED=1
|
|
227
|
+
|
|
228
|
+
# Run application
|
|
229
|
+
CMD ["python", "-m", "CRCA"]
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
**Example: Docker Compose for Full Stack**
|
|
233
|
+
|
|
234
|
+
```yaml
|
|
235
|
+
version: '3.8'
|
|
236
|
+
|
|
237
|
+
services:
|
|
238
|
+
crca:
|
|
239
|
+
build: .
|
|
240
|
+
environment:
|
|
241
|
+
- OPENAI_API_KEY=${OPENAI_API_KEY}
|
|
242
|
+
volumes:
|
|
243
|
+
- ./policy_ledger.db:/app/policy_ledger.db
|
|
244
|
+
- ./rollback.db:/app/rollback.db
|
|
245
|
+
ports:
|
|
246
|
+
- "8000:8000"
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Verification
|
|
250
|
+
|
|
251
|
+
**Example 1: Basic Verification**
|
|
252
|
+
|
|
253
|
+
```python
|
|
254
|
+
from dotenv import load_dotenv
|
|
255
|
+
from CRCA import CRCAAgent
|
|
256
|
+
|
|
257
|
+
load_dotenv()
|
|
258
|
+
|
|
259
|
+
agent = CRCAAgent(
|
|
260
|
+
model_name="gpt-4o-mini",
|
|
261
|
+
causal_edges=[("funding", "failure"), ("engineers", "failure"), ("project", "failure")],
|
|
262
|
+
variables=["funding", "engineers", "project", "failure", "success"],
|
|
263
|
+
max_loops=3,
|
|
264
|
+
agent_max_loops=3,
|
|
265
|
+
use_crca_tools=True,
|
|
266
|
+
enable_batch_predict=True,
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
task = "Why do large software projects collapse despite adequate funding?"
|
|
270
|
+
result = agent.run(task)
|
|
271
|
+
print(result.get("causal_analysis", ""))
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
**Example 2: Verify Policy Engine**
|
|
275
|
+
|
|
276
|
+
```python
|
|
277
|
+
from schemas.policy import DoctrineV1, EpochConfig, MetricSpec, Objective, RiskBudget
|
|
278
|
+
from utils.ledger import Ledger
|
|
279
|
+
|
|
280
|
+
# Create minimal doctrine
|
|
281
|
+
doctrine = DoctrineV1(
|
|
282
|
+
epoch=EpochConfig(unit="hours", length=1.0),
|
|
283
|
+
metrics={"test": MetricSpec(extractor_key="test", unit="unit")},
|
|
284
|
+
objectives=[Objective(metric_name="test", direction="minimize", weight=1.0)],
|
|
285
|
+
invariants=[],
|
|
286
|
+
levers={},
|
|
287
|
+
risk_budget=RiskBudget()
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
# Test ledger
|
|
291
|
+
ledger = Ledger(db_path=":memory:")
|
|
292
|
+
print("Policy engine components verified!")
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
**Example 3: Verify All Modules**
|
|
296
|
+
|
|
297
|
+
```python
|
|
298
|
+
# Test core
|
|
299
|
+
from CRCA import CRCAAgent
|
|
300
|
+
agent = CRCAAgent(model_name="gpt-4o-mini")
|
|
301
|
+
print("✓ Core CRCA verified")
|
|
302
|
+
|
|
303
|
+
# Test CRCA-SD
|
|
304
|
+
try:
|
|
305
|
+
from crca_sd import StateVector
|
|
306
|
+
print("✓ CRCA-SD verified")
|
|
307
|
+
except ImportError:
|
|
308
|
+
print("✗ CRCA-SD not available")
|
|
309
|
+
|
|
310
|
+
# Test CRCA-CG
|
|
311
|
+
try:
|
|
312
|
+
from crca_cg.corposwarm import CorporateSwarm
|
|
313
|
+
print("✓ CRCA-CG verified")
|
|
314
|
+
except ImportError:
|
|
315
|
+
print("✗ CRCA-CG not available")
|
|
316
|
+
|
|
317
|
+
# Test Policy Engine
|
|
318
|
+
from schemas.policy import DoctrineV1
|
|
319
|
+
from utils.ledger import Ledger
|
|
320
|
+
print("✓ Policy Engine verified")
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
---
|
|
324
|
+
|
|
325
|
+
## Core CRCAAgent
|
|
326
|
+
|
|
327
|
+
### Introduction
|
|
328
|
+
|
|
329
|
+
CRCAAgent is the core component of the CR-CA framework, providing a lightweight causal reasoning engine with LLM integration. It implements structural causal models (SCMs) based on Judea Pearl's causal hierarchy, enabling both LLM-based causal analysis and deterministic causal simulation.
|
|
330
|
+
|
|
331
|
+
### Key Features
|
|
332
|
+
|
|
333
|
+
- **Automatic Variable Extraction**: Extract variables and causal relationships directly from natural language tasks without manual declaration
|
|
334
|
+
- **LLM-Based Causal Reasoning**: Sophisticated multi-loop causal analysis using large language models
|
|
335
|
+
- **Deterministic Causal Simulation**: Fast, deterministic state evolution using linear/non-linear SCMs
|
|
336
|
+
- **Dual-Mode Operation**: Automatically selects LLM mode (task string) or deterministic mode (state dict)
|
|
337
|
+
- **Batch Prediction**: Efficient vectorized predictions for ensemble forecasting
|
|
338
|
+
- **Async/Await Support**: Non-blocking operations for concurrent analysis
|
|
339
|
+
- **Advanced Analysis Methods**: Optimization, time-series analysis, Bayesian inference, root cause analysis
|
|
340
|
+
|
|
341
|
+
### Architecture
|
|
342
|
+
|
|
343
|
+
```mermaid
|
|
344
|
+
flowchart TB
|
|
345
|
+
Input[Input: Task or State] --> ModeCheck{Mode Detection}
|
|
346
|
+
|
|
347
|
+
ModeCheck -->|String Task| LLMMode[LLM Mode]
|
|
348
|
+
ModeCheck -->|State Dict| DetMode[Deterministic Mode]
|
|
349
|
+
|
|
350
|
+
LLMMode --> EmptyCheck{Graph Empty?}
|
|
351
|
+
EmptyCheck -->|Yes| AutoExtract[Automatic Variable Extraction]
|
|
352
|
+
EmptyCheck -->|No| BuildPrompt[Build Causal Prompt]
|
|
353
|
+
|
|
354
|
+
AutoExtract --> LLMAnalysis[LLM Causal Analysis]
|
|
355
|
+
BuildPrompt --> LLMAnalysis
|
|
356
|
+
LLMAnalysis --> GenerateCF[Generate Counterfactuals]
|
|
357
|
+
|
|
358
|
+
DetMode --> Standardize[Standardize to z-space]
|
|
359
|
+
Standardize --> TopoSort[Topological Sort]
|
|
360
|
+
TopoSort --> Predict[SCM Evolution]
|
|
361
|
+
Predict --> DeStandardize[De-standardize]
|
|
362
|
+
DeStandardize --> GenerateCF
|
|
363
|
+
|
|
364
|
+
GenerateCF --> Output[Output: Analysis + Scenarios]
|
|
365
|
+
|
|
366
|
+
Graph[Causal Graph] --> LLMMode
|
|
367
|
+
Graph --> DetMode
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
### Automatic Variable Extraction
|
|
371
|
+
|
|
372
|
+
One of the most powerful features introduced in v1.3.0 is automatic variable extraction. When a task is provided without pre-declared variables, CRCAAgent automatically:
|
|
373
|
+
|
|
374
|
+
1. **Analyzes the task** using LLM-based natural language understanding
|
|
375
|
+
2. **Extracts relevant variables** from the task description
|
|
376
|
+
3. **Identifies causal relationships** between variables
|
|
377
|
+
4. **Builds the causal graph** automatically
|
|
378
|
+
5. **Proceeds with causal analysis** using the extracted graph
|
|
379
|
+
|
|
380
|
+
This eliminates the need to manually declare variables and edges for most use cases, making the framework significantly more accessible.
|
|
381
|
+
|
|
382
|
+
**How it works:**
|
|
383
|
+
|
|
384
|
+
- When `run(task)` is called with a string task and the causal graph is empty, the agent triggers automatic extraction
|
|
385
|
+
- The LLM analyzes the task and generates structured JSON with variables and edges
|
|
386
|
+
- The extraction handler programmatically adds variables and relationships to the graph
|
|
387
|
+
- Subsequent analysis uses the automatically constructed graph
|
|
388
|
+
|
|
389
|
+
**Example:**
|
|
390
|
+
|
|
391
|
+
```python
|
|
392
|
+
from CRCA import CRCAAgent
|
|
393
|
+
|
|
394
|
+
# No variables declared - automatic extraction will occur
|
|
395
|
+
agent = CRCAAgent(model_name="gpt-4o-mini")
|
|
396
|
+
|
|
397
|
+
# Task automatically triggers variable extraction
|
|
398
|
+
task = "Analyze how increasing product price affects demand and revenue"
|
|
399
|
+
result = agent.run(task=task)
|
|
400
|
+
|
|
401
|
+
# Variables and edges were automatically extracted and used
|
|
402
|
+
print(f"Extracted {len(agent.get_nodes())} variables")
|
|
403
|
+
print(f"Created {len(agent.get_edges())} causal relationships")
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
### Basic Usage
|
|
407
|
+
|
|
408
|
+
#### LLM-Based Causal Analysis (Automatic Extraction)
|
|
409
|
+
|
|
410
|
+
```python
|
|
411
|
+
from CRCA import CRCAAgent
|
|
412
|
+
|
|
413
|
+
agent = CRCAAgent(
|
|
414
|
+
model_name="gpt-4o",
|
|
415
|
+
max_loops=3,
|
|
416
|
+
agent_max_loops=3
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
# Automatic variable extraction + causal analysis
|
|
420
|
+
task = "Why do software projects fail despite adequate funding?"
|
|
421
|
+
result = agent.run(task=task)
|
|
422
|
+
|
|
423
|
+
print(result["causal_analysis"])
|
|
424
|
+
for scenario in result["counterfactual_scenarios"][:3]:
|
|
425
|
+
print(f"{scenario.name}: {scenario.reasoning}")
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
#### Deterministic Simulation (Traditional Approach)
|
|
429
|
+
|
|
430
|
+
```python
|
|
431
|
+
from CRCA import CRCAAgent
|
|
432
|
+
|
|
433
|
+
agent = CRCAAgent(variables=["price", "demand", "inventory"])
|
|
434
|
+
agent.add_causal_relationship("price", "demand", strength=-0.5)
|
|
435
|
+
agent.add_causal_relationship("demand", "inventory", strength=-0.2)
|
|
436
|
+
|
|
437
|
+
state = {"price": 100.0, "demand": 1000.0, "inventory": 5000.0}
|
|
438
|
+
result = agent.run(initial_state=state, max_steps=2)
|
|
439
|
+
|
|
440
|
+
print(result["evolved_state"])
|
|
441
|
+
for scenario in result["counterfactual_scenarios"][:3]:
|
|
442
|
+
print(f"{scenario.name}: {scenario.probability:.2f}")
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
#### Hybrid Approach (LLM Extraction + Deterministic Simulation)
|
|
446
|
+
|
|
447
|
+
```python
|
|
448
|
+
from CRCA import CRCAAgent
|
|
449
|
+
|
|
450
|
+
agent = CRCAAgent(model_name="gpt-4o-mini")
|
|
451
|
+
|
|
452
|
+
# Step 1: Automatic extraction via LLM
|
|
453
|
+
task = "Analyze pricing strategy for a new product"
|
|
454
|
+
extraction_result = agent.run(task=task)
|
|
455
|
+
|
|
456
|
+
# Step 2: Use extracted graph for deterministic simulation
|
|
457
|
+
initial_state = {var: 0.0 for var in agent.get_nodes()}
|
|
458
|
+
simulation_result = agent.run(initial_state=initial_state, max_steps=5)
|
|
459
|
+
|
|
460
|
+
print(f"Simulated {len(simulation_result['counterfactual_scenarios'])} scenarios")
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
### Advanced Features
|
|
464
|
+
|
|
465
|
+
#### Batch Prediction
|
|
466
|
+
|
|
467
|
+
```python
|
|
468
|
+
agent = CRCAAgent(
|
|
469
|
+
variables=["price", "demand"],
|
|
470
|
+
enable_batch_predict=True,
|
|
471
|
+
max_batch_size=32
|
|
472
|
+
)
|
|
473
|
+
|
|
474
|
+
factual_states = [{"price": 100.0, "demand": 1000.0} for _ in range(100)]
|
|
475
|
+
interventions_list = [{"price": 100.0 + i} for i in range(100)]
|
|
476
|
+
|
|
477
|
+
predictions = agent._predict_outcomes_batch(factual_states, interventions_list)
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
#### Async Operations
|
|
481
|
+
|
|
482
|
+
```python
|
|
483
|
+
import asyncio
|
|
484
|
+
|
|
485
|
+
async def analyze_async():
|
|
486
|
+
agent = CRCAAgent(variables=["x", "y"])
|
|
487
|
+
result = await agent.run_async(
|
|
488
|
+
initial_state={"x": 1.0, "y": 2.0},
|
|
489
|
+
max_steps=3
|
|
490
|
+
)
|
|
491
|
+
return result
|
|
492
|
+
|
|
493
|
+
result = asyncio.run(analyze_async())
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
#### Optimization Methods
|
|
497
|
+
|
|
498
|
+
```python
|
|
499
|
+
agent = CRCAAgent(variables=["price", "demand", "revenue"])
|
|
500
|
+
agent.add_causal_relationship("price", "demand", strength=-0.5)
|
|
501
|
+
agent.add_causal_relationship("price", "revenue", strength=1.0)
|
|
502
|
+
agent.add_causal_relationship("demand", "revenue", strength=0.8)
|
|
503
|
+
|
|
504
|
+
initial_state = {"price": 100.0, "demand": 1000.0, "revenue": 100000.0}
|
|
505
|
+
|
|
506
|
+
# Gradient-based optimization
|
|
507
|
+
opt_result = agent.gradient_based_intervention_optimization(
|
|
508
|
+
initial_state=initial_state,
|
|
509
|
+
target="revenue",
|
|
510
|
+
intervention_vars=["price"],
|
|
511
|
+
constraints={"price": (80.0, 120.0)}
|
|
512
|
+
)
|
|
513
|
+
|
|
514
|
+
print(f"Optimal price: {opt_result['optimal_intervention']['price']}")
|
|
515
|
+
print(f"Optimal revenue: {opt_result['optimal_target_value']}")
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
---
|
|
519
|
+
|
|
520
|
+
## Module Documentation
|
|
521
|
+
|
|
522
|
+
### Utils Module
|
|
523
|
+
|
|
524
|
+
The `utils/` module provides essential utilities and extensions for the CR-CA framework.
|
|
525
|
+
|
|
526
|
+
#### Formatter (`utils/formatter.py`)
|
|
527
|
+
|
|
528
|
+
Provides markdown output formatting and rich text rendering:
|
|
529
|
+
|
|
530
|
+
```python
|
|
531
|
+
from utils.formatter import Formatter
|
|
532
|
+
|
|
533
|
+
formatter = Formatter(md=True)
|
|
534
|
+
formatter.print_markdown(analysis_text, title="Causal Analysis")
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
#### Agent Types (`utils/Agent_types.py`)
|
|
538
|
+
|
|
539
|
+
Type definitions for agent-related structures.
|
|
540
|
+
|
|
541
|
+
#### Conversation (`utils/conversation.py`)
|
|
542
|
+
|
|
543
|
+
Manages conversation history and message handling for agent interactions.
|
|
544
|
+
|
|
545
|
+
#### Agent Extensions (`utils/agent.py`)
|
|
546
|
+
|
|
547
|
+
Extended agent functionality building on the Swarms Agent base class, including streaming output, error handling, and tool management.
|
|
548
|
+
|
|
549
|
+
#### TUI (`utils/tui.py`)
|
|
550
|
+
|
|
551
|
+
Terminal user interface components for interactive agent interactions, including CorporateSwarmTUI for multi-agent orchestration.
|
|
552
|
+
|
|
553
|
+
#### Router (`utils/router.py`)
|
|
554
|
+
|
|
555
|
+
Task routing and distribution system for multi-agent workflows. Supports multiple swarm architectures including sequential, concurrent, hierarchical, and collaborative patterns.
|
|
556
|
+
|
|
557
|
+
**Example 1: Sequential Workflow**
|
|
558
|
+
|
|
559
|
+
```python
|
|
560
|
+
from utils.router import SwarmRouter, SwarmRouterConfig
|
|
561
|
+
|
|
562
|
+
# Create sequential workflow router
|
|
563
|
+
config = SwarmRouterConfig(
|
|
564
|
+
name="sequential-processor",
|
|
565
|
+
description="Process tasks sequentially",
|
|
566
|
+
swarm_type="SequentialWorkflow",
|
|
567
|
+
task="Process data pipeline: extract, transform, load"
|
|
568
|
+
)
|
|
569
|
+
|
|
570
|
+
router = SwarmRouter(config=config)
|
|
571
|
+
result = router.run()
|
|
572
|
+
|
|
573
|
+
print(f"Sequential workflow completed: {result}")
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
**Example 2: Concurrent Workflow**
|
|
577
|
+
|
|
578
|
+
```python
|
|
579
|
+
from utils.router import SwarmRouter, SwarmRouterConfig
|
|
580
|
+
|
|
581
|
+
# Create concurrent workflow for parallel processing
|
|
582
|
+
config = SwarmRouterConfig(
|
|
583
|
+
name="parallel-processor",
|
|
584
|
+
description="Process multiple tasks in parallel",
|
|
585
|
+
swarm_type="ConcurrentWorkflow",
|
|
586
|
+
task="Analyze multiple datasets concurrently"
|
|
587
|
+
)
|
|
588
|
+
|
|
589
|
+
router = SwarmRouter(config=config)
|
|
590
|
+
result = router.run()
|
|
591
|
+
|
|
592
|
+
print(f"Concurrent workflow completed: {result}")
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
**Example 3: Hierarchical Swarm**
|
|
596
|
+
|
|
597
|
+
```python
|
|
598
|
+
from utils.router import SwarmRouter, SwarmRouterConfig
|
|
599
|
+
|
|
600
|
+
# Create hierarchical swarm for complex task decomposition
|
|
601
|
+
config = SwarmRouterConfig(
|
|
602
|
+
name="hierarchical-manager",
|
|
603
|
+
description="Decompose complex tasks hierarchically",
|
|
604
|
+
swarm_type="HierarchicalSwarm",
|
|
605
|
+
task="Manage multi-level project with dependencies"
|
|
606
|
+
)
|
|
607
|
+
|
|
608
|
+
router = SwarmRouter(config=config)
|
|
609
|
+
result = router.run()
|
|
610
|
+
|
|
611
|
+
print(f"Hierarchical swarm completed: {result}")
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
**Example 4: Collaborative Group Chat**
|
|
615
|
+
|
|
616
|
+
```python
|
|
617
|
+
from utils.router import SwarmRouter, SwarmRouterConfig
|
|
618
|
+
|
|
619
|
+
# Create collaborative swarm for group discussion
|
|
620
|
+
config = SwarmRouterConfig(
|
|
621
|
+
name="collaborative-team",
|
|
622
|
+
description="Collaborative problem solving",
|
|
623
|
+
swarm_type="GroupChat",
|
|
624
|
+
multi_agent_collab_prompt=True,
|
|
625
|
+
task="Design a new feature through team discussion"
|
|
626
|
+
)
|
|
627
|
+
|
|
628
|
+
router = SwarmRouter(config=config)
|
|
629
|
+
result = router.run()
|
|
630
|
+
|
|
631
|
+
print(f"Collaborative discussion completed: {result}")
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
**Example 5: Auto Swarm Selection**
|
|
635
|
+
|
|
636
|
+
```python
|
|
637
|
+
from utils.router import SwarmRouter, SwarmRouterConfig
|
|
638
|
+
|
|
639
|
+
# Let router automatically select best swarm type
|
|
640
|
+
config = SwarmRouterConfig(
|
|
641
|
+
name="auto-router",
|
|
642
|
+
description="Automatic swarm selection",
|
|
643
|
+
swarm_type="auto", # Automatic selection
|
|
644
|
+
task="Complex task requiring intelligent routing"
|
|
645
|
+
)
|
|
646
|
+
|
|
647
|
+
router = SwarmRouter(config=config)
|
|
648
|
+
result = router.run()
|
|
649
|
+
|
|
650
|
+
print(f"Auto-selected swarm type: {router.selected_swarm_type}")
|
|
651
|
+
print(f"Result: {result}")
|
|
652
|
+
```
|
|
653
|
+
|
|
654
|
+
#### HHCS (`utils/HHCS.py`)
|
|
655
|
+
|
|
656
|
+
Hybrid Hierarchical-Cluster Swarm implementation for intelligent task routing with hierarchical decision-making.
|
|
657
|
+
|
|
658
|
+
**Example 6: Basic HHCS Setup**
|
|
659
|
+
|
|
660
|
+
```python
|
|
661
|
+
from utils.HHCS import HybridHierarchicalClusterSwarm
|
|
662
|
+
from swarms.structs.agent import Agent
|
|
663
|
+
|
|
664
|
+
# Create agents for different roles
|
|
665
|
+
analyst = Agent(agent_name="analyst", model_name="gpt-4o-mini")
|
|
666
|
+
researcher = Agent(agent_name="researcher", model_name="gpt-4o-mini")
|
|
667
|
+
writer = Agent(agent_name="writer", model_name="gpt-4o-mini")
|
|
668
|
+
|
|
669
|
+
# Create HHCS with agents
|
|
670
|
+
hhcs = HybridHierarchicalClusterSwarm(
|
|
671
|
+
agents=[analyst, researcher, writer],
|
|
672
|
+
task="Research and write a comprehensive report on causal inference"
|
|
673
|
+
)
|
|
674
|
+
|
|
675
|
+
# Run HHCS
|
|
676
|
+
result = hhcs.run()
|
|
677
|
+
|
|
678
|
+
print(f"HHCS completed: {result}")
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
**Example 7: HHCS with Custom Router**
|
|
682
|
+
|
|
683
|
+
```python
|
|
684
|
+
from utils.HHCS import HybridHierarchicalClusterSwarm
|
|
685
|
+
from swarms.structs.agent import Agent
|
|
686
|
+
|
|
687
|
+
# Create specialized agents
|
|
688
|
+
agents = [
|
|
689
|
+
Agent(agent_name="data_analyst", model_name="gpt-4o-mini"),
|
|
690
|
+
Agent(agent_name="domain_expert", model_name="gpt-4o-mini"),
|
|
691
|
+
Agent(agent_name="technical_writer", model_name="gpt-4o-mini")
|
|
692
|
+
]
|
|
693
|
+
|
|
694
|
+
# Create HHCS with custom routing
|
|
695
|
+
hhcs = HybridHierarchicalClusterSwarm(
|
|
696
|
+
agents=agents,
|
|
697
|
+
task="Analyze market trends and create investment strategy"
|
|
698
|
+
)
|
|
699
|
+
|
|
700
|
+
result = hhcs.run()
|
|
701
|
+
print(f"Task routed to: {hhcs.selected_agent}")
|
|
702
|
+
print(f"Result: {result}")
|
|
703
|
+
```
|
|
704
|
+
|
|
705
|
+
#### AOP (`utils/aop.py`)
|
|
706
|
+
|
|
707
|
+
Agent Orchestration Platform with task queue and MCP server deployment for multi-agent coordination.
|
|
708
|
+
|
|
709
|
+
**Example 8: Task Queue Management**
|
|
710
|
+
|
|
711
|
+
```python
|
|
712
|
+
from utils.aop import TaskQueue, Task
|
|
713
|
+
from swarms.structs.agent import Agent
|
|
714
|
+
|
|
715
|
+
# Create agent
|
|
716
|
+
agent = Agent(agent_name="worker", model_name="gpt-4o-mini")
|
|
717
|
+
|
|
718
|
+
# Create task queue
|
|
719
|
+
queue = TaskQueue(
|
|
720
|
+
agent_name="worker",
|
|
721
|
+
agent=agent,
|
|
722
|
+
max_workers=3, # 3 parallel workers
|
|
723
|
+
max_queue_size=100
|
|
724
|
+
)
|
|
725
|
+
|
|
726
|
+
# Start workers
|
|
727
|
+
queue.start_workers()
|
|
728
|
+
|
|
729
|
+
# Add tasks
|
|
730
|
+
task1 = Task(task="Analyze dataset A", priority=10)
|
|
731
|
+
task2 = Task(task="Analyze dataset B", priority=5)
|
|
732
|
+
task3 = Task(task="Generate report", priority=8)
|
|
733
|
+
|
|
734
|
+
queue.add_task(task1)
|
|
735
|
+
queue.add_task(task2)
|
|
736
|
+
queue.add_task(task3)
|
|
737
|
+
|
|
738
|
+
# Get task status
|
|
739
|
+
task = queue.get_task(task1.task_id)
|
|
740
|
+
print(f"Task status: {task.status}")
|
|
741
|
+
|
|
742
|
+
# Get queue statistics
|
|
743
|
+
stats = queue.get_stats()
|
|
744
|
+
print(f"Total tasks: {stats.total_tasks}")
|
|
745
|
+
print(f"Completed: {stats.completed_tasks}")
|
|
746
|
+
print(f"Pending: {stats.pending_tasks}")
|
|
747
|
+
|
|
748
|
+
# Stop workers when done
|
|
749
|
+
queue.stop_workers()
|
|
750
|
+
```
|
|
751
|
+
|
|
752
|
+
**Example 9: MCP Server Deployment**
|
|
753
|
+
|
|
754
|
+
```python
|
|
755
|
+
from utils.aop import AOP, AgentToolConfig
|
|
756
|
+
from swarms.structs.agent import Agent
|
|
757
|
+
|
|
758
|
+
# Create agents
|
|
759
|
+
agent1 = Agent(agent_name="analyst", model_name="gpt-4o-mini")
|
|
760
|
+
agent2 = Agent(agent_name="researcher", model_name="gpt-4o-mini")
|
|
761
|
+
|
|
762
|
+
# Configure agent tools
|
|
763
|
+
configs = [
|
|
764
|
+
AgentToolConfig(
|
|
765
|
+
agent=agent1,
|
|
766
|
+
tool_name="analyze_data",
|
|
767
|
+
description="Analyze datasets and extract insights"
|
|
768
|
+
),
|
|
769
|
+
AgentToolConfig(
|
|
770
|
+
agent=agent2,
|
|
771
|
+
tool_name="research_topic",
|
|
772
|
+
description="Research topics and gather information"
|
|
773
|
+
)
|
|
774
|
+
]
|
|
775
|
+
|
|
776
|
+
# Create AOP and deploy as MCP server
|
|
777
|
+
aop = AOP(agent_configs=configs)
|
|
778
|
+
|
|
779
|
+
# Start MCP server
|
|
780
|
+
aop.start_mcp_server(port=8000)
|
|
781
|
+
|
|
782
|
+
print("MCP server started on port 8000")
|
|
783
|
+
print(f"Available tools: {aop.get_available_tools()}")
|
|
784
|
+
```
|
|
785
|
+
|
|
786
|
+
**Example 10: AOP with Task Queue and MCP**
|
|
787
|
+
|
|
788
|
+
```python
|
|
789
|
+
from utils.aop import AOP, TaskQueue, AgentToolConfig
|
|
790
|
+
from swarms.structs.agent import Agent
|
|
791
|
+
|
|
792
|
+
# Create agents
|
|
793
|
+
agents = [
|
|
794
|
+
Agent(agent_name="worker1", model_name="gpt-4o-mini"),
|
|
795
|
+
Agent(agent_name="worker2", model_name="gpt-4o-mini")
|
|
796
|
+
]
|
|
797
|
+
|
|
798
|
+
# Configure AOP
|
|
799
|
+
configs = [
|
|
800
|
+
AgentToolConfig(agent=agents[0], tool_name="process_task"),
|
|
801
|
+
AgentToolConfig(agent=agents[1], tool_name="analyze_data")
|
|
802
|
+
]
|
|
803
|
+
|
|
804
|
+
aop = AOP(agent_configs=configs)
|
|
805
|
+
|
|
806
|
+
# Deploy as MCP server
|
|
807
|
+
aop.start_mcp_server(port=8000)
|
|
808
|
+
|
|
809
|
+
# Create task queues for each agent
|
|
810
|
+
for agent in agents:
|
|
811
|
+
queue = TaskQueue(agent_name=agent.agent_name, agent=agent, max_workers=2)
|
|
812
|
+
queue.start_workers()
|
|
813
|
+
|
|
814
|
+
# Add tasks
|
|
815
|
+
queue.add_task(Task(task=f"Task for {agent.agent_name}", priority=5))
|
|
816
|
+
```
|
|
817
|
+
|
|
818
|
+
#### Ledger (`utils/ledger.py`)
|
|
819
|
+
|
|
820
|
+
Event-sourced append-only SQLite ledger for audit trail. See Policy Engine examples above for detailed usage.
|
|
821
|
+
|
|
822
|
+
**Example 11: Ledger Event Recording**
|
|
823
|
+
|
|
824
|
+
```python
|
|
825
|
+
from utils.ledger import Ledger
|
|
826
|
+
from schemas.policy import ObservationEvent, DecisionEvent
|
|
827
|
+
|
|
828
|
+
ledger = Ledger(db_path="audit_ledger.db")
|
|
829
|
+
|
|
830
|
+
# Record observation
|
|
831
|
+
obs = ObservationEvent(epoch=1, metrics={"cpu": 75.0, "memory": 60.0})
|
|
832
|
+
event_id = ledger.append(obs)
|
|
833
|
+
|
|
834
|
+
# Record decision
|
|
835
|
+
dec = DecisionEvent(
|
|
836
|
+
epoch=1,
|
|
837
|
+
interventions=[{"lever_id": "throttle", "params": {"value": 5}}],
|
|
838
|
+
rationale="CPU high, throttling",
|
|
839
|
+
decision_hash="abc123"
|
|
840
|
+
)
|
|
841
|
+
event_id = ledger.append(dec)
|
|
842
|
+
|
|
843
|
+
# Query events
|
|
844
|
+
events = ledger.get_events_by_epoch(epoch=1)
|
|
845
|
+
print(f"Found {len(events)} events in epoch 1")
|
|
846
|
+
```
|
|
847
|
+
|
|
848
|
+
#### Rollback (`utils/rollback.py`)
|
|
849
|
+
|
|
850
|
+
Rollback system for intervention recovery. See Policy Engine examples above for detailed usage.
|
|
851
|
+
|
|
852
|
+
**Example 12: Checkpoint and Rollback**
|
|
853
|
+
|
|
854
|
+
```python
|
|
855
|
+
from utils.rollback import RollbackManager
|
|
856
|
+
|
|
857
|
+
rollback = RollbackManager(db_path="rollback.db")
|
|
858
|
+
|
|
859
|
+
# Create checkpoint
|
|
860
|
+
checkpoint_id = rollback.create_checkpoint(epoch=1, state={"value": 100})
|
|
861
|
+
|
|
862
|
+
# Execute intervention
|
|
863
|
+
# ... intervention code ...
|
|
864
|
+
|
|
865
|
+
# Rollback if needed
|
|
866
|
+
rollback.rollback_n_steps(epoch=1, n_steps=1)
|
|
867
|
+
restored = rollback.restore_checkpoint(checkpoint_id)
|
|
868
|
+
```
|
|
869
|
+
|
|
870
|
+
#### Canonical (`utils/canonical.py`)
|
|
871
|
+
|
|
872
|
+
Canonicalization utilities for deterministic hashing. See Policy Engine examples above for detailed usage.
|
|
873
|
+
|
|
874
|
+
**Example 13: Stable Hashing**
|
|
875
|
+
|
|
876
|
+
```python
|
|
877
|
+
from utils.canonical import stable_hash
|
|
878
|
+
|
|
879
|
+
obj = {"a": 1, "b": 2, "c": 3}
|
|
880
|
+
hash_value = stable_hash(obj)
|
|
881
|
+
print(f"Stable hash: {hash_value}")
|
|
882
|
+
```
|
|
883
|
+
|
|
884
|
+
#### AnyToStr (`utils/AnyToStr.py`)
|
|
885
|
+
|
|
886
|
+
Type conversion utilities for converting various types to strings.
|
|
887
|
+
|
|
888
|
+
**Example 14: Type Conversion**
|
|
889
|
+
|
|
890
|
+
```python
|
|
891
|
+
from utils.AnyToStr import any_to_str
|
|
892
|
+
|
|
893
|
+
# Convert various types to string
|
|
894
|
+
result1 = any_to_str(123)
|
|
895
|
+
result2 = any_to_str([1, 2, 3])
|
|
896
|
+
result3 = any_to_str({"key": "value"})
|
|
897
|
+
|
|
898
|
+
print(f"Int to string: {result1}")
|
|
899
|
+
print(f"List to string: {result2}")
|
|
900
|
+
print(f"Dict to string: {result3}")
|
|
901
|
+
```
|
|
902
|
+
|
|
903
|
+
#### Out Types (`utils/out_types.py`)
|
|
904
|
+
|
|
905
|
+
Output type definitions for history and conversation formatting.
|
|
906
|
+
|
|
907
|
+
**Example 15: Output Type Usage**
|
|
908
|
+
|
|
909
|
+
```python
|
|
910
|
+
from utils.out_types import OutputType, HistoryOutputType
|
|
911
|
+
|
|
912
|
+
# Define output format
|
|
913
|
+
output_format: OutputType = "json" # or "dict", "string", "yaml", etc.
|
|
914
|
+
|
|
915
|
+
# Use in conversation formatting
|
|
916
|
+
from utils.conversation import Conversation
|
|
917
|
+
|
|
918
|
+
conv = Conversation()
|
|
919
|
+
history = conv.get_history(output_type=output_format)
|
|
920
|
+
```
|
|
921
|
+
|
|
922
|
+
### Tools Module
|
|
923
|
+
|
|
924
|
+
#### MCP Client Utils (`tools/mcpClientUtils.py`)
|
|
925
|
+
|
|
926
|
+
Model Context Protocol (MCP) integration for connecting to MCP servers and executing tools:
|
|
927
|
+
|
|
928
|
+
- **Connection Management**: Establish and manage MCP server connections
|
|
929
|
+
- **Tool Execution**: Execute tools via MCP protocol
|
|
930
|
+
- **Protocol Handling**: Transform MCP tools to OpenAI-compatible format
|
|
931
|
+
- **Error Handling**: Comprehensive error handling for MCP operations
|
|
932
|
+
|
|
933
|
+
**Example 1: Basic MCP Connection**
|
|
934
|
+
|
|
935
|
+
```python
|
|
936
|
+
from tools.mcpClientUtils import connect_to_mcp_server, execute_mcp_tool
|
|
937
|
+
|
|
938
|
+
# Connection configuration
|
|
939
|
+
connection_config = {
|
|
940
|
+
"server_url": "http://localhost:8000",
|
|
941
|
+
"api_key": "your_api_key"
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
# Connect to MCP server
|
|
945
|
+
session = await connect_to_mcp_server(connection_config)
|
|
946
|
+
|
|
947
|
+
# Execute tool
|
|
948
|
+
result = await execute_mcp_tool(
|
|
949
|
+
session=session,
|
|
950
|
+
tool_name="analyze_data",
|
|
951
|
+
parameters={"dataset": "sales_data.csv"}
|
|
952
|
+
)
|
|
953
|
+
|
|
954
|
+
print(f"Tool execution result: {result}")
|
|
955
|
+
```
|
|
956
|
+
|
|
957
|
+
**Example 2: Transform MCP Tools to OpenAI Format**
|
|
958
|
+
|
|
959
|
+
```python
|
|
960
|
+
from tools.mcpClientUtils import transform_mcp_tool_to_openai_tool
|
|
961
|
+
|
|
962
|
+
# MCP tool schema
|
|
963
|
+
mcp_tool = {
|
|
964
|
+
"name": "process_file",
|
|
965
|
+
"description": "Process a file",
|
|
966
|
+
"parameters": {
|
|
967
|
+
"type": "object",
|
|
968
|
+
"properties": {
|
|
969
|
+
"file_path": {"type": "string"},
|
|
970
|
+
"operation": {"type": "string"}
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
# Transform to OpenAI format
|
|
976
|
+
openai_tool = transform_mcp_tool_to_openai_tool(mcp_tool)
|
|
977
|
+
|
|
978
|
+
print(f"OpenAI tool format: {openai_tool}")
|
|
979
|
+
```
|
|
980
|
+
|
|
981
|
+
**Example 3: Error Handling in MCP Operations**
|
|
982
|
+
|
|
983
|
+
```python
|
|
984
|
+
from tools.mcpClientUtils import connect_to_mcp_server, execute_mcp_tool
|
|
985
|
+
from loguru import logger
|
|
986
|
+
|
|
987
|
+
try:
|
|
988
|
+
session = await connect_to_mcp_server(connection_config)
|
|
989
|
+
result = await execute_mcp_tool(session, "tool_name", {})
|
|
990
|
+
except ConnectionError as e:
|
|
991
|
+
logger.error(f"MCP connection failed: {e}")
|
|
992
|
+
except Exception as e:
|
|
993
|
+
logger.error(f"Tool execution failed: {e}")
|
|
994
|
+
```
|
|
995
|
+
|
|
996
|
+
#### Actuators (`tools/actuators.py`)
|
|
997
|
+
|
|
998
|
+
Actuator interfaces for executing interventions on system controls, cloud resources, and services. See Policy Engine examples above for detailed usage.
|
|
999
|
+
|
|
1000
|
+
**Example 4: System Control Actuator**
|
|
1001
|
+
|
|
1002
|
+
```python
|
|
1003
|
+
from tools.actuators import SystemControlActuator
|
|
1004
|
+
from schemas.policy import InterventionSpec
|
|
1005
|
+
|
|
1006
|
+
actuator = SystemControlActuator(require_root=False)
|
|
1007
|
+
|
|
1008
|
+
# CPU throttling intervention
|
|
1009
|
+
intervention = InterventionSpec(
|
|
1010
|
+
lever_id="cpu_throttle",
|
|
1011
|
+
parameters={"nice_value": 10, "pid": 12345}
|
|
1012
|
+
)
|
|
1013
|
+
|
|
1014
|
+
result = actuator.execute([intervention])
|
|
1015
|
+
print(f"Intervention result: {result}")
|
|
1016
|
+
```
|
|
1017
|
+
|
|
1018
|
+
**Example 5: Custom Actuator Implementation**
|
|
1019
|
+
|
|
1020
|
+
```python
|
|
1021
|
+
from tools.actuators import BaseActuator
|
|
1022
|
+
from schemas.policy import InterventionSpec
|
|
1023
|
+
from typing import List, Dict, Any
|
|
1024
|
+
|
|
1025
|
+
class CloudResourceActuator(BaseActuator):
|
|
1026
|
+
"""Actuator for cloud resource management."""
|
|
1027
|
+
|
|
1028
|
+
def execute(self, interventions: List[InterventionSpec]) -> Dict[str, Any]:
|
|
1029
|
+
"""Execute cloud resource interventions."""
|
|
1030
|
+
results = []
|
|
1031
|
+
|
|
1032
|
+
for intervention in interventions:
|
|
1033
|
+
if intervention.lever_id == "scale_instance":
|
|
1034
|
+
instance_id = intervention.parameters["instance_id"]
|
|
1035
|
+
instance_type = intervention.parameters["instance_type"]
|
|
1036
|
+
|
|
1037
|
+
# Scale cloud instance (pseudo-code)
|
|
1038
|
+
result = self._scale_instance(instance_id, instance_type)
|
|
1039
|
+
results.append({"intervention": intervention.model_dump(), "result": result})
|
|
1040
|
+
|
|
1041
|
+
return {"executed": len(results), "results": results}
|
|
1042
|
+
|
|
1043
|
+
def _scale_instance(self, instance_id: str, instance_type: str) -> Dict[str, Any]:
|
|
1044
|
+
"""Scale cloud instance."""
|
|
1045
|
+
# Implementation would call cloud API
|
|
1046
|
+
return {"status": "success", "instance_id": instance_id, "type": instance_type}
|
|
1047
|
+
|
|
1048
|
+
# Use custom actuator
|
|
1049
|
+
actuator = CloudResourceActuator()
|
|
1050
|
+
intervention = InterventionSpec(
|
|
1051
|
+
lever_id="scale_instance",
|
|
1052
|
+
parameters={"instance_id": "i-12345", "instance_type": "t3.large"}
|
|
1053
|
+
)
|
|
1054
|
+
result = actuator.execute([intervention])
|
|
1055
|
+
```
|
|
1056
|
+
|
|
1057
|
+
#### Sensors (`tools/sensors.py`)
|
|
1058
|
+
|
|
1059
|
+
Sensor interfaces for reading system metrics, cloud resources, and custom metrics. See Policy Engine examples above for detailed usage.
|
|
1060
|
+
|
|
1061
|
+
**Example 6: System Metrics Sensor**
|
|
1062
|
+
|
|
1063
|
+
```python
|
|
1064
|
+
from tools.sensors import SystemMetricsSensor
|
|
1065
|
+
|
|
1066
|
+
sensor = SystemMetricsSensor()
|
|
1067
|
+
snapshot = sensor.read()
|
|
1068
|
+
|
|
1069
|
+
print(f"CPU: {snapshot['cpu_percent']:.1f}%")
|
|
1070
|
+
print(f"Memory: {snapshot['memory_percent']:.1f}%")
|
|
1071
|
+
```
|
|
1072
|
+
|
|
1073
|
+
**Example 7: Custom Sensor with Registry**
|
|
1074
|
+
|
|
1075
|
+
```python
|
|
1076
|
+
from tools.sensors import BaseSensor, SensorRegistry
|
|
1077
|
+
from typing import Dict
|
|
1078
|
+
|
|
1079
|
+
class ApplicationMetricsSensor(BaseSensor):
|
|
1080
|
+
"""Sensor for application-specific metrics."""
|
|
1081
|
+
|
|
1082
|
+
def read(self) -> Dict[str, float]:
|
|
1083
|
+
"""Read application metrics."""
|
|
1084
|
+
# Read from application monitoring endpoint
|
|
1085
|
+
return {
|
|
1086
|
+
"request_rate": 150.0,
|
|
1087
|
+
"error_rate": 2.5,
|
|
1088
|
+
"response_time": 95.0
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
# Register sensor
|
|
1092
|
+
registry = SensorRegistry()
|
|
1093
|
+
registry.register("app_metrics", ApplicationMetricsSensor())
|
|
1094
|
+
|
|
1095
|
+
# Use registered sensor
|
|
1096
|
+
sensor = registry.get_sensor("app_metrics")
|
|
1097
|
+
snapshot = sensor.read()
|
|
1098
|
+
```
|
|
1099
|
+
|
|
1100
|
+
#### Code Generator (`tools/code_generator.py`)
|
|
1101
|
+
|
|
1102
|
+
Code generation tools with LLM and ML integration for generating, analyzing, and optimizing code.
|
|
1103
|
+
|
|
1104
|
+
**Example 8: Basic Code Generation**
|
|
1105
|
+
|
|
1106
|
+
```python
|
|
1107
|
+
from tools.code_generator import LLMCodeGenerator
|
|
1108
|
+
from swarms.structs.agent import Agent
|
|
1109
|
+
|
|
1110
|
+
# Create agent for code generation
|
|
1111
|
+
agent = Agent(agent_name="code_gen", model_name="gpt-4o-mini")
|
|
1112
|
+
|
|
1113
|
+
# Initialize code generator
|
|
1114
|
+
generator = LLMCodeGenerator(agent=agent)
|
|
1115
|
+
|
|
1116
|
+
# Generate code from specification
|
|
1117
|
+
specification = "Create a Python function that calculates factorial recursively"
|
|
1118
|
+
code = generator.generate(
|
|
1119
|
+
specification=specification,
|
|
1120
|
+
language="python"
|
|
1121
|
+
)
|
|
1122
|
+
|
|
1123
|
+
print(f"Generated code:\n{code}")
|
|
1124
|
+
|
|
1125
|
+
# Validate generated code
|
|
1126
|
+
validation = generator.validate(code, language="python")
|
|
1127
|
+
print(f"Validation result: {validation}")
|
|
1128
|
+
```
|
|
1129
|
+
|
|
1130
|
+
**Example 9: Language-Specific Code Generation**
|
|
1131
|
+
|
|
1132
|
+
```python
|
|
1133
|
+
from tools.code_generator import LLMCodeGenerator
|
|
1134
|
+
|
|
1135
|
+
generator = LLMCodeGenerator(agent=agent)
|
|
1136
|
+
|
|
1137
|
+
# Generate JavaScript code
|
|
1138
|
+
js_code = generator.generate(
|
|
1139
|
+
specification="Create a React component for a todo list",
|
|
1140
|
+
language="javascript"
|
|
1141
|
+
)
|
|
1142
|
+
|
|
1143
|
+
# Generate Python code
|
|
1144
|
+
python_code = generator.generate(
|
|
1145
|
+
specification="Create a data processing pipeline",
|
|
1146
|
+
language="python"
|
|
1147
|
+
)
|
|
1148
|
+
|
|
1149
|
+
# Generate TypeScript code
|
|
1150
|
+
ts_code = generator.generate(
|
|
1151
|
+
specification="Create a type-safe API client",
|
|
1152
|
+
language="typescript"
|
|
1153
|
+
)
|
|
1154
|
+
```
|
|
1155
|
+
|
|
1156
|
+
**Example 10: Code Optimization with ML**
|
|
1157
|
+
|
|
1158
|
+
```python
|
|
1159
|
+
from tools.code_generator import MLCodeOptimizer
|
|
1160
|
+
|
|
1161
|
+
# Initialize ML-based optimizer
|
|
1162
|
+
optimizer = MLCodeOptimizer()
|
|
1163
|
+
|
|
1164
|
+
# Optimize code
|
|
1165
|
+
original_code = """
|
|
1166
|
+
def factorial(n):
|
|
1167
|
+
if n <= 1:
|
|
1168
|
+
return 1
|
|
1169
|
+
return n * factorial(n - 1)
|
|
1170
|
+
"""
|
|
1171
|
+
|
|
1172
|
+
optimized_code = optimizer.optimize(
|
|
1173
|
+
code=original_code,
|
|
1174
|
+
language="python",
|
|
1175
|
+
optimization_target="performance"
|
|
1176
|
+
)
|
|
1177
|
+
|
|
1178
|
+
print(f"Optimized code:\n{optimized_code}")
|
|
1179
|
+
```
|
|
1180
|
+
|
|
1181
|
+
#### Mandate Generator (`tools/mandate_generator.py`)
|
|
1182
|
+
|
|
1183
|
+
Convert CorporateProposal to bolt.diy execution mandate for autonomous code generation.
|
|
1184
|
+
|
|
1185
|
+
**Example 11: Proposal to Mandate Conversion**
|
|
1186
|
+
|
|
1187
|
+
```python
|
|
1188
|
+
from tools.mandate_generator import proposal_to_mandate
|
|
1189
|
+
from crca_cg.corposwarm import CorporateProposal, ProposalType
|
|
1190
|
+
|
|
1191
|
+
# Create corporate proposal
|
|
1192
|
+
proposal = CorporateProposal(
|
|
1193
|
+
proposal_id="prop-001",
|
|
1194
|
+
title="Implement new feature",
|
|
1195
|
+
description="Add user authentication system",
|
|
1196
|
+
proposal_type=ProposalType.TECHNICAL,
|
|
1197
|
+
budget_impact=50000.0,
|
|
1198
|
+
risk_level=0.3
|
|
1199
|
+
)
|
|
1200
|
+
|
|
1201
|
+
# Convert to execution mandate
|
|
1202
|
+
mandate = proposal_to_mandate(
|
|
1203
|
+
proposal=proposal,
|
|
1204
|
+
mandate_id="mandate-001"
|
|
1205
|
+
)
|
|
1206
|
+
|
|
1207
|
+
print(f"Mandate ID: {mandate['mandate_id']}")
|
|
1208
|
+
print(f"Objectives: {mandate['objectives']}")
|
|
1209
|
+
print(f"Budget: {mandate['budget']}")
|
|
1210
|
+
print(f"Constraints: {mandate['constraints']}")
|
|
1211
|
+
```
|
|
1212
|
+
|
|
1213
|
+
**Example 12: Complete Workflow with Mandate Generation**
|
|
1214
|
+
|
|
1215
|
+
```python
|
|
1216
|
+
from tools.mandate_generator import proposal_to_mandate
|
|
1217
|
+
from crca_cg.corposwarm import CorporateSwarm, CorporateProposal
|
|
1218
|
+
|
|
1219
|
+
# Create corporate swarm
|
|
1220
|
+
swarm = CorporateSwarm()
|
|
1221
|
+
|
|
1222
|
+
# Create and submit proposal
|
|
1223
|
+
proposal = swarm.create_proposal(
|
|
1224
|
+
title="Build API gateway",
|
|
1225
|
+
description="Implement microservices API gateway",
|
|
1226
|
+
proposal_type=ProposalType.TECHNICAL
|
|
1227
|
+
)
|
|
1228
|
+
|
|
1229
|
+
# Vote on proposal
|
|
1230
|
+
vote_result = swarm.board.vote_on_proposal(proposal)
|
|
1231
|
+
|
|
1232
|
+
if vote_result.passed:
|
|
1233
|
+
# Convert approved proposal to mandate
|
|
1234
|
+
mandate = proposal_to_mandate(
|
|
1235
|
+
proposal=proposal,
|
|
1236
|
+
corporate_swarm=swarm
|
|
1237
|
+
)
|
|
1238
|
+
|
|
1239
|
+
# Execute mandate (would integrate with bolt.diy)
|
|
1240
|
+
print(f"Mandate created: {mandate['mandate_id']}")
|
|
1241
|
+
print(f"Ready for execution")
|
|
1242
|
+
```
|
|
1243
|
+
|
|
1244
|
+
#### File Operations (`tools/file_operations.py`)
|
|
1245
|
+
|
|
1246
|
+
File manipulation tools with LLM integration for intelligent file operations.
|
|
1247
|
+
|
|
1248
|
+
**Example 13: Basic File Operations**
|
|
1249
|
+
|
|
1250
|
+
```python
|
|
1251
|
+
from tools.file_operations import FileWriter, FileReader
|
|
1252
|
+
|
|
1253
|
+
# Write file
|
|
1254
|
+
writer = FileWriter()
|
|
1255
|
+
result = writer.execute(
|
|
1256
|
+
operation="write",
|
|
1257
|
+
file_path="output.txt",
|
|
1258
|
+
content="Hello, World!"
|
|
1259
|
+
)
|
|
1260
|
+
|
|
1261
|
+
# Read file
|
|
1262
|
+
reader = FileReader()
|
|
1263
|
+
result = reader.execute(
|
|
1264
|
+
operation="read",
|
|
1265
|
+
file_path="output.txt"
|
|
1266
|
+
)
|
|
1267
|
+
|
|
1268
|
+
print(f"File content: {result['content']}")
|
|
1269
|
+
```
|
|
1270
|
+
|
|
1271
|
+
**Example 14: LLM-Powered File Operations**
|
|
1272
|
+
|
|
1273
|
+
```python
|
|
1274
|
+
from tools.file_operations import LLMFileOperator
|
|
1275
|
+
from swarms.structs.agent import Agent
|
|
1276
|
+
|
|
1277
|
+
agent = Agent(agent_name="file_ops", model_name="gpt-4o-mini")
|
|
1278
|
+
operator = LLMFileOperator(agent=agent)
|
|
1279
|
+
|
|
1280
|
+
# Generate file content using LLM
|
|
1281
|
+
result = operator.generate_file(
|
|
1282
|
+
specification="Create a Python class for handling user authentication",
|
|
1283
|
+
file_path="auth.py",
|
|
1284
|
+
language="python"
|
|
1285
|
+
)
|
|
1286
|
+
|
|
1287
|
+
print(f"Generated file: {result['file_path']}")
|
|
1288
|
+
print(f"Content length: {len(result['content'])} characters")
|
|
1289
|
+
```
|
|
1290
|
+
|
|
1291
|
+
**Example 15: File Analysis and Optimization**
|
|
1292
|
+
|
|
1293
|
+
```python
|
|
1294
|
+
from tools.file_operations import FileAnalyzer
|
|
1295
|
+
|
|
1296
|
+
analyzer = FileAnalyzer()
|
|
1297
|
+
|
|
1298
|
+
# Analyze file
|
|
1299
|
+
analysis = analyzer.analyze_file(
|
|
1300
|
+
file_path="code.py",
|
|
1301
|
+
analysis_type="complexity" # or "performance", "security", etc.
|
|
1302
|
+
)
|
|
1303
|
+
|
|
1304
|
+
print(f"Complexity score: {analysis['complexity_score']}")
|
|
1305
|
+
print(f"Issues found: {analysis['issues']}")
|
|
1306
|
+
print(f"Suggestions: {analysis['suggestions']}")
|
|
1307
|
+
```
|
|
1308
|
+
|
|
1309
|
+
### Templates Module
|
|
1310
|
+
|
|
1311
|
+
The templates module provides a framework for creating specialized agents with drag-and-drop feature composition.
|
|
1312
|
+
|
|
1313
|
+
#### Base Specialized Agent (`templates/base_specialized_agent.py`)
|
|
1314
|
+
|
|
1315
|
+
Base class for creating domain-specific agents with common initialization patterns and memory management.
|
|
1316
|
+
|
|
1317
|
+
#### Feature Mixins (`templates/feature_mixins.py`)
|
|
1318
|
+
|
|
1319
|
+
Modular feature components that can be mixed into agent classes:
|
|
1320
|
+
|
|
1321
|
+
- **GraphFeatureMixin**: Graph operations (nodes, edges, topological sort)
|
|
1322
|
+
- **PredictionFeatureMixin**: Prediction methods and standardization
|
|
1323
|
+
- **StatisticsFeatureMixin**: Statistical analysis and data fitting
|
|
1324
|
+
- **LLMFeatureMixin**: LLM integration patterns
|
|
1325
|
+
- **FullFeatureMixin**: All features combined
|
|
1326
|
+
|
|
1327
|
+
**Usage:**
|
|
1328
|
+
|
|
1329
|
+
```python
|
|
1330
|
+
from templates.base_specialized_agent import BaseSpecializedAgent
|
|
1331
|
+
from templates.feature_mixins import GraphFeatureMixin, PredictionFeatureMixin
|
|
1332
|
+
|
|
1333
|
+
class MyAgent(BaseSpecializedAgent, GraphFeatureMixin, PredictionFeatureMixin):
|
|
1334
|
+
def __init__(self, **kwargs):
|
|
1335
|
+
super().__init__(**kwargs)
|
|
1336
|
+
self.init_graph_feature(variables=["A", "B", "C"])
|
|
1337
|
+
self.init_prediction_feature(use_nonlinear=True)
|
|
1338
|
+
```
|
|
1339
|
+
|
|
1340
|
+
#### Module Registry (`templates/module_registry.py`)
|
|
1341
|
+
|
|
1342
|
+
Feature composition system allowing dynamic agent creation:
|
|
1343
|
+
|
|
1344
|
+
```python
|
|
1345
|
+
from templates.module_registry import compose_agent
|
|
1346
|
+
|
|
1347
|
+
agent = compose_agent(
|
|
1348
|
+
features=["graph", "prediction", "statistics"],
|
|
1349
|
+
variables=["x", "y", "z"]
|
|
1350
|
+
)
|
|
1351
|
+
```
|
|
1352
|
+
|
|
1353
|
+
#### Graph Management (`templates/graph_management.py`)
|
|
1354
|
+
|
|
1355
|
+
Graph operations including path finding, cycle detection, and topological sorting.
|
|
1356
|
+
|
|
1357
|
+
**Example 1: Graph Operations**
|
|
1358
|
+
|
|
1359
|
+
```python
|
|
1360
|
+
from templates.graph_management import GraphManager
|
|
1361
|
+
|
|
1362
|
+
# Create graph manager
|
|
1363
|
+
graph = GraphManager()
|
|
1364
|
+
graph.add_node("A")
|
|
1365
|
+
graph.add_node("B")
|
|
1366
|
+
graph.add_node("C")
|
|
1367
|
+
graph.add_edge("A", "B")
|
|
1368
|
+
graph.add_edge("B", "C")
|
|
1369
|
+
|
|
1370
|
+
# Topological sort
|
|
1371
|
+
topo_order = graph.topological_sort()
|
|
1372
|
+
print(f"Topological order: {topo_order}")
|
|
1373
|
+
|
|
1374
|
+
# Find paths
|
|
1375
|
+
paths = graph.find_paths("A", "C")
|
|
1376
|
+
print(f"Paths from A to C: {paths}")
|
|
1377
|
+
|
|
1378
|
+
# Check for cycles
|
|
1379
|
+
has_cycle = graph.has_cycle()
|
|
1380
|
+
print(f"Has cycle: {has_cycle}")
|
|
1381
|
+
```
|
|
1382
|
+
|
|
1383
|
+
#### Prediction Framework (`templates/prediction_framework.py`)
|
|
1384
|
+
|
|
1385
|
+
Prediction methods, standardization, and counterfactual generation patterns.
|
|
1386
|
+
|
|
1387
|
+
**Example 2: Prediction with Standardization**
|
|
1388
|
+
|
|
1389
|
+
```python
|
|
1390
|
+
from templates.prediction_framework import PredictionMixin
|
|
1391
|
+
|
|
1392
|
+
class MyAgent(PredictionMixin):
|
|
1393
|
+
def __init__(self):
|
|
1394
|
+
self.init_prediction_feature(use_nonlinear=True)
|
|
1395
|
+
|
|
1396
|
+
agent = MyAgent()
|
|
1397
|
+
|
|
1398
|
+
# Standardize state
|
|
1399
|
+
state = {"x": 100.0, "y": 200.0}
|
|
1400
|
+
standardized = agent.standardize_state(state)
|
|
1401
|
+
|
|
1402
|
+
# Predict outcome
|
|
1403
|
+
prediction = agent.predict_outcome(
|
|
1404
|
+
state=state,
|
|
1405
|
+
intervention={"x": 110.0}
|
|
1406
|
+
)
|
|
1407
|
+
|
|
1408
|
+
print(f"Predicted outcome: {prediction}")
|
|
1409
|
+
```
|
|
1410
|
+
|
|
1411
|
+
#### Statistical Methods (`templates/statistical_methods.py`)
|
|
1412
|
+
|
|
1413
|
+
Statistical analysis including data fitting, uncertainty quantification, and time-series methods.
|
|
1414
|
+
|
|
1415
|
+
**Example 3: Statistical Analysis**
|
|
1416
|
+
|
|
1417
|
+
```python
|
|
1418
|
+
from templates.statistical_methods import StatisticsMixin
|
|
1419
|
+
import numpy as np
|
|
1420
|
+
|
|
1421
|
+
class StatsAgent(StatisticsMixin):
|
|
1422
|
+
def __init__(self):
|
|
1423
|
+
self.init_statistics_feature()
|
|
1424
|
+
|
|
1425
|
+
agent = StatsAgent()
|
|
1426
|
+
|
|
1427
|
+
# Fit data to model
|
|
1428
|
+
data = np.array([[1, 2], [2, 4], [3, 6], [4, 8]])
|
|
1429
|
+
fitted_model = agent.fit_data(data, model_type="linear")
|
|
1430
|
+
|
|
1431
|
+
# Quantify uncertainty
|
|
1432
|
+
uncertainty = agent.quantify_uncertainty(data, fitted_model)
|
|
1433
|
+
print(f"Uncertainty: {uncertainty}")
|
|
1434
|
+
```
|
|
1435
|
+
|
|
1436
|
+
#### LLM Integration (`templates/llm_integration.py`)
|
|
1437
|
+
|
|
1438
|
+
LLM integration patterns for schema definition, prompt building, and multi-loop reasoning.
|
|
1439
|
+
|
|
1440
|
+
**Example 4: LLM Integration**
|
|
1441
|
+
|
|
1442
|
+
```python
|
|
1443
|
+
from templates.llm_integration import LLMFeatureMixin
|
|
1444
|
+
|
|
1445
|
+
class LLMAgent(LLMFeatureMixin):
|
|
1446
|
+
def __init__(self):
|
|
1447
|
+
self.init_llm_feature(model_name="gpt-4o-mini")
|
|
1448
|
+
|
|
1449
|
+
agent = LLMAgent()
|
|
1450
|
+
|
|
1451
|
+
# Build prompt
|
|
1452
|
+
prompt = agent.build_prompt(
|
|
1453
|
+
task="Analyze causal relationships",
|
|
1454
|
+
context={"variables": ["A", "B", "C"]}
|
|
1455
|
+
)
|
|
1456
|
+
|
|
1457
|
+
# Execute with multi-loop reasoning
|
|
1458
|
+
result = agent.execute_with_loops(
|
|
1459
|
+
prompt=prompt,
|
|
1460
|
+
max_loops=3
|
|
1461
|
+
)
|
|
1462
|
+
```
|
|
1463
|
+
|
|
1464
|
+
#### Policy Loop Integration (`templates/policy_loop.py`)
|
|
1465
|
+
|
|
1466
|
+
Policy loop mixin for temporal policy execution. See Policy Engine examples above for detailed usage.
|
|
1467
|
+
|
|
1468
|
+
**Example 5: Policy Loop with Agent**
|
|
1469
|
+
|
|
1470
|
+
```python
|
|
1471
|
+
from templates.policy_loop import PolicyLoopMixin
|
|
1472
|
+
from schemas.policy import DoctrineV1
|
|
1473
|
+
from utils.ledger import Ledger
|
|
1474
|
+
|
|
1475
|
+
class PolicyAgent(PolicyLoopMixin):
|
|
1476
|
+
def __init__(self, doctrine, ledger):
|
|
1477
|
+
super().__init__(doctrine, ledger, seed=42)
|
|
1478
|
+
|
|
1479
|
+
doctrine = DoctrineV1.from_json("doctrine.json")
|
|
1480
|
+
ledger = Ledger(db_path="ledger.db")
|
|
1481
|
+
|
|
1482
|
+
agent = PolicyAgent(doctrine, ledger)
|
|
1483
|
+
result = agent.run_epoch(epoch=1)
|
|
1484
|
+
```
|
|
1485
|
+
|
|
1486
|
+
#### Drift Detection Integration (`templates/drift_detection.py`)
|
|
1487
|
+
|
|
1488
|
+
Drift detection for change-point detection. See Policy Engine examples above for detailed usage.
|
|
1489
|
+
|
|
1490
|
+
**Example 6: Drift Detection in Policy Loop**
|
|
1491
|
+
|
|
1492
|
+
```python
|
|
1493
|
+
from templates.drift_detection import DriftDetector
|
|
1494
|
+
from templates.policy_loop import PolicyLoopMixin
|
|
1495
|
+
|
|
1496
|
+
class DriftAwarePolicyAgent(PolicyLoopMixin):
|
|
1497
|
+
def __init__(self, doctrine, ledger):
|
|
1498
|
+
super().__init__(doctrine, ledger)
|
|
1499
|
+
self.drift_detector = DriftDetector(algorithm="pelt")
|
|
1500
|
+
|
|
1501
|
+
def check_drift(self, metric_name: str, history: List[float]):
|
|
1502
|
+
"""Check for drift in metric."""
|
|
1503
|
+
change_points, confidence = self.drift_detector.detect_changepoints(
|
|
1504
|
+
metric_name=metric_name,
|
|
1505
|
+
metric_history=history
|
|
1506
|
+
)
|
|
1507
|
+
return len(change_points) > 0
|
|
1508
|
+
|
|
1509
|
+
agent = DriftAwarePolicyAgent(doctrine, ledger)
|
|
1510
|
+
|
|
1511
|
+
# Check for drift
|
|
1512
|
+
has_drift = agent.check_drift("cpu_usage", [50, 55, 60, 65, 70, 75])
|
|
1513
|
+
if has_drift:
|
|
1514
|
+
print("WARNING: Drift detected!")
|
|
1515
|
+
```
|
|
1516
|
+
|
|
1517
|
+
#### MPC Planner Integration (`templates/mpc_planner.py`)
|
|
1518
|
+
|
|
1519
|
+
MPC planner for multi-step optimization. See Policy Engine examples above for detailed usage.
|
|
1520
|
+
|
|
1521
|
+
**Example 7: MPC Planning in Policy Loop**
|
|
1522
|
+
|
|
1523
|
+
```python
|
|
1524
|
+
from templates.mpc_planner import MPCPlanner
|
|
1525
|
+
from templates.policy_loop import PolicyLoopMixin
|
|
1526
|
+
|
|
1527
|
+
class MPCPolicyAgent(PolicyLoopMixin):
|
|
1528
|
+
def __init__(self, doctrine, ledger):
|
|
1529
|
+
super().__init__(doctrine, ledger)
|
|
1530
|
+
self.mpc_planner = MPCPlanner(doctrine, horizon=5)
|
|
1531
|
+
self.use_mpc = True
|
|
1532
|
+
|
|
1533
|
+
def plan_actions(self, epoch: int, state: Dict[str, float]):
|
|
1534
|
+
"""Plan actions using MPC."""
|
|
1535
|
+
if self.use_mpc:
|
|
1536
|
+
interventions, score, rationale = self.mpc_planner.solve_mpc(...)
|
|
1537
|
+
return interventions
|
|
1538
|
+
else:
|
|
1539
|
+
return super().plan_actions(epoch, state)
|
|
1540
|
+
|
|
1541
|
+
agent = MPCPolicyAgent(doctrine, ledger)
|
|
1542
|
+
```
|
|
1543
|
+
|
|
1544
|
+
#### Template Examples (`templates/examples/`)
|
|
1545
|
+
|
|
1546
|
+
Four example templates demonstrating different agent creation patterns.
|
|
1547
|
+
|
|
1548
|
+
**Example 8: Causal Agent Template**
|
|
1549
|
+
|
|
1550
|
+
```python
|
|
1551
|
+
from templates.examples.causal_agent_template import create_causal_agent
|
|
1552
|
+
|
|
1553
|
+
# Create causal agent from template
|
|
1554
|
+
agent = create_causal_agent(
|
|
1555
|
+
variables=["price", "demand", "supply"],
|
|
1556
|
+
model_name="gpt-4o-mini"
|
|
1557
|
+
)
|
|
1558
|
+
|
|
1559
|
+
# Use agent
|
|
1560
|
+
result = agent.run("Analyze price-demand relationship")
|
|
1561
|
+
```
|
|
1562
|
+
|
|
1563
|
+
**Example 9: Drag-Drop Feature Composition**
|
|
1564
|
+
|
|
1565
|
+
```python
|
|
1566
|
+
from templates.examples.drag_drop_example import compose_agent_features
|
|
1567
|
+
|
|
1568
|
+
# Compose agent with selected features
|
|
1569
|
+
agent = compose_agent_features(
|
|
1570
|
+
features=["graph", "prediction", "statistics"],
|
|
1571
|
+
variables=["x", "y", "z"]
|
|
1572
|
+
)
|
|
1573
|
+
|
|
1574
|
+
# Agent now has graph, prediction, and statistics capabilities
|
|
1575
|
+
result = agent.analyze_with_features(data)
|
|
1576
|
+
```
|
|
1577
|
+
|
|
1578
|
+
**Example 10: Logistics Agent Template**
|
|
1579
|
+
|
|
1580
|
+
```python
|
|
1581
|
+
from templates.examples.logistics_agent_template import create_logistics_agent
|
|
1582
|
+
|
|
1583
|
+
# Create logistics-specific agent
|
|
1584
|
+
agent = create_logistics_agent(
|
|
1585
|
+
model_name="gpt-4o-mini",
|
|
1586
|
+
logistics_domain="supply_chain"
|
|
1587
|
+
)
|
|
1588
|
+
|
|
1589
|
+
# Use for logistics tasks
|
|
1590
|
+
result = agent.run("Optimize warehouse inventory levels")
|
|
1591
|
+
```
|
|
1592
|
+
|
|
1593
|
+
**Example 11: Trading Agent Template**
|
|
1594
|
+
|
|
1595
|
+
```python
|
|
1596
|
+
from templates.examples.trading_agent_template import create_trading_agent
|
|
1597
|
+
|
|
1598
|
+
# Create trading agent
|
|
1599
|
+
agent = create_trading_agent(
|
|
1600
|
+
model_name="gpt-4o-mini",
|
|
1601
|
+
trading_strategy="momentum"
|
|
1602
|
+
)
|
|
1603
|
+
|
|
1604
|
+
# Use for trading analysis
|
|
1605
|
+
result = agent.run("Analyze market signals for BTC")
|
|
1606
|
+
```
|
|
1607
|
+
|
|
1608
|
+
**Example 12: Custom Agent from Templates**
|
|
1609
|
+
|
|
1610
|
+
```python
|
|
1611
|
+
from templates.base_specialized_agent import BaseSpecializedAgent
|
|
1612
|
+
from templates.feature_mixins import (
|
|
1613
|
+
GraphFeatureMixin,
|
|
1614
|
+
PredictionFeatureMixin,
|
|
1615
|
+
StatisticsFeatureMixin,
|
|
1616
|
+
LLMFeatureMixin
|
|
1617
|
+
)
|
|
1618
|
+
|
|
1619
|
+
class CustomAgent(
|
|
1620
|
+
BaseSpecializedAgent,
|
|
1621
|
+
GraphFeatureMixin,
|
|
1622
|
+
PredictionFeatureMixin,
|
|
1623
|
+
StatisticsFeatureMixin,
|
|
1624
|
+
LLMFeatureMixin
|
|
1625
|
+
):
|
|
1626
|
+
def __init__(self, **kwargs):
|
|
1627
|
+
super().__init__(**kwargs)
|
|
1628
|
+
# Initialize all features
|
|
1629
|
+
self.init_graph_feature(variables=["A", "B", "C"])
|
|
1630
|
+
self.init_prediction_feature(use_nonlinear=True)
|
|
1631
|
+
self.init_statistics_feature()
|
|
1632
|
+
self.init_llm_feature(model_name="gpt-4o-mini")
|
|
1633
|
+
|
|
1634
|
+
def _get_domain_schema(self):
|
|
1635
|
+
"""Define domain-specific tools."""
|
|
1636
|
+
return {
|
|
1637
|
+
"type": "function",
|
|
1638
|
+
"function": {
|
|
1639
|
+
"name": "custom_domain_tool",
|
|
1640
|
+
"description": "Custom domain tool",
|
|
1641
|
+
"parameters": {
|
|
1642
|
+
"type": "object",
|
|
1643
|
+
"properties": {
|
|
1644
|
+
"param1": {"type": "string"},
|
|
1645
|
+
"param2": {"type": "number"}
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1651
|
+
# Use custom agent
|
|
1652
|
+
agent = CustomAgent(agent_name="custom_agent")
|
|
1653
|
+
result = agent.run("Custom domain task")
|
|
1654
|
+
```
|
|
1655
|
+
|
|
1656
|
+
### Schemas Module
|
|
1657
|
+
|
|
1658
|
+
#### MCP Schemas (`schemas/mcpSchemas.py`)
|
|
1659
|
+
|
|
1660
|
+
Type definitions and schemas for MCP (Model Context Protocol) connections, including connection configurations and tool schemas.
|
|
1661
|
+
|
|
1662
|
+
**Example 1: MCP Connection Schema**
|
|
1663
|
+
|
|
1664
|
+
```python
|
|
1665
|
+
from schemas.mcpSchemas import MCPConnectionConfig, MCPToolSchema
|
|
1666
|
+
|
|
1667
|
+
# Define MCP connection
|
|
1668
|
+
connection = MCPConnectionConfig(
|
|
1669
|
+
server_url="http://localhost:8000",
|
|
1670
|
+
api_key="your_api_key",
|
|
1671
|
+
timeout=30
|
|
1672
|
+
)
|
|
1673
|
+
|
|
1674
|
+
# Define MCP tool schema
|
|
1675
|
+
tool = MCPToolSchema(
|
|
1676
|
+
name="analyze_data",
|
|
1677
|
+
description="Analyze dataset",
|
|
1678
|
+
parameters={
|
|
1679
|
+
"type": "object",
|
|
1680
|
+
"properties": {
|
|
1681
|
+
"dataset": {"type": "string"},
|
|
1682
|
+
"operation": {"type": "string"}
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
)
|
|
1686
|
+
```
|
|
1687
|
+
|
|
1688
|
+
#### Policy Schemas (`schemas/policy.py`)
|
|
1689
|
+
|
|
1690
|
+
Comprehensive policy schema definitions for the policy engine system.
|
|
1691
|
+
|
|
1692
|
+
**Example 2: Policy Schema Usage**
|
|
1693
|
+
|
|
1694
|
+
```python
|
|
1695
|
+
from schemas.policy import (
|
|
1696
|
+
DoctrineV1, EpochConfig, MetricSpec, Objective,
|
|
1697
|
+
Invariant, LeverSpec, RiskBudget, CompiledPolicy
|
|
1698
|
+
)
|
|
1699
|
+
|
|
1700
|
+
# Create complete doctrine
|
|
1701
|
+
doctrine = DoctrineV1(
|
|
1702
|
+
epoch=EpochConfig(unit="hours", length=1.0),
|
|
1703
|
+
metrics={
|
|
1704
|
+
"cpu_usage": MetricSpec(extractor_key="cpu_percent", unit="percent")
|
|
1705
|
+
},
|
|
1706
|
+
objectives=[
|
|
1707
|
+
Objective(metric_name="cpu_usage", direction="minimize", weight=1.0)
|
|
1708
|
+
],
|
|
1709
|
+
invariants=[
|
|
1710
|
+
Invariant(name="cpu_limit", condition="cpu_usage < 90.0")
|
|
1711
|
+
],
|
|
1712
|
+
levers={
|
|
1713
|
+
"throttle": LeverSpec(lever_type="cpu_throttle", bounds={"nice_value": {"min": 0, "max": 19}})
|
|
1714
|
+
},
|
|
1715
|
+
risk_budget=RiskBudget(max_actions_per_epoch=5)
|
|
1716
|
+
)
|
|
1717
|
+
|
|
1718
|
+
# Compile policy
|
|
1719
|
+
compiled = CompiledPolicy.compile(doctrine)
|
|
1720
|
+
print(f"Policy hash: {compiled.policy_hash}")
|
|
1721
|
+
```
|
|
1722
|
+
|
|
1723
|
+
**Example 3: Policy Schema Validation**
|
|
1724
|
+
|
|
1725
|
+
```python
|
|
1726
|
+
from schemas.policy import DoctrineV1, EpochConfig
|
|
1727
|
+
from pydantic import ValidationError
|
|
1728
|
+
|
|
1729
|
+
try:
|
|
1730
|
+
# Invalid doctrine (missing required fields)
|
|
1731
|
+
doctrine = DoctrineV1(
|
|
1732
|
+
epoch=EpochConfig(unit="hours", length=1.0)
|
|
1733
|
+
# Missing metrics, objectives, etc.
|
|
1734
|
+
)
|
|
1735
|
+
except ValidationError as e:
|
|
1736
|
+
print(f"Validation error: {e}")
|
|
1737
|
+
print("Doctrine must include metrics, objectives, invariants, levers, and risk_budget")
|
|
1738
|
+
```
|
|
1739
|
+
|
|
1740
|
+
**Example 4: Policy Schema Serialization**
|
|
1741
|
+
|
|
1742
|
+
```python
|
|
1743
|
+
from schemas.policy import DoctrineV1
|
|
1744
|
+
|
|
1745
|
+
# Create doctrine
|
|
1746
|
+
doctrine = DoctrineV1(...)
|
|
1747
|
+
|
|
1748
|
+
# Serialize to JSON
|
|
1749
|
+
doctrine_json = doctrine.model_dump_json(indent=2)
|
|
1750
|
+
print(doctrine_json)
|
|
1751
|
+
|
|
1752
|
+
# Save to file
|
|
1753
|
+
doctrine.to_json("doctrine.json")
|
|
1754
|
+
|
|
1755
|
+
# Load from file
|
|
1756
|
+
loaded_doctrine = DoctrineV1.from_json("doctrine.json")
|
|
1757
|
+
assert loaded_doctrine.version == doctrine.version
|
|
1758
|
+
```
|
|
1759
|
+
|
|
1760
|
+
### CRCA-SD Module
|
|
1761
|
+
|
|
1762
|
+
CRCA-SD (Socioeconomic Dynamics & Logistics) provides a constrained, stochastic, multi-objective model-predictive control system for socioeconomic dynamics optimization.
|
|
1763
|
+
|
|
1764
|
+
#### Core Components
|
|
1765
|
+
|
|
1766
|
+
- **StateVector**: System state representation
|
|
1767
|
+
- **ControlVector**: Control inputs
|
|
1768
|
+
- **DynamicsModel**: System dynamics modeling
|
|
1769
|
+
- **ConstraintChecker**: Constraint validation
|
|
1770
|
+
- **ForwardSimulator**: Forward simulation engine
|
|
1771
|
+
- **MPCSolver**: Model Predictive Control solver
|
|
1772
|
+
- **ScenarioGenerator**: Scenario generation with CRCA integration
|
|
1773
|
+
|
|
1774
|
+
#### Governance System
|
|
1775
|
+
|
|
1776
|
+
- **Board**: Governance board structure
|
|
1777
|
+
- **Arbitration**: Dispute resolution
|
|
1778
|
+
- **GovernanceSystem**: Complete governance framework
|
|
1779
|
+
|
|
1780
|
+
#### Real-time Components
|
|
1781
|
+
|
|
1782
|
+
- **DataAcquisition**: Real-time data collection
|
|
1783
|
+
- **DataPipeline**: Data processing pipeline
|
|
1784
|
+
- **RealTimeMonitor**: Real-time monitoring
|
|
1785
|
+
- **PolicyExecutor**: Policy execution engine
|
|
1786
|
+
|
|
1787
|
+
#### CRCA Integration
|
|
1788
|
+
|
|
1789
|
+
CRCA-SD integrates CRCAAgent for causal scenario generation:
|
|
1790
|
+
|
|
1791
|
+
```python
|
|
1792
|
+
from crca_sd import get_crca_agent
|
|
1793
|
+
from crca_sd.crca_sd_mpc import ScenarioGenerator
|
|
1794
|
+
|
|
1795
|
+
# Get CRCAAgent instance
|
|
1796
|
+
crca = get_crca_agent(variables=['Y', 'U', 'S', 'P', 'L', 'W', 'K', 'I'])
|
|
1797
|
+
|
|
1798
|
+
# Create scenario generator with CRCA
|
|
1799
|
+
gen = ScenarioGenerator(crca_agent=crca)
|
|
1800
|
+
|
|
1801
|
+
# Generate causal scenarios (not just random)
|
|
1802
|
+
scenarios = gen.generate_causal_scenarios(
|
|
1803
|
+
n_scenarios=10,
|
|
1804
|
+
horizon=12,
|
|
1805
|
+
current_state=state_vector,
|
|
1806
|
+
target_variables=['Y', 'U', 'S']
|
|
1807
|
+
)
|
|
1808
|
+
```
|
|
1809
|
+
|
|
1810
|
+
**Usage Example:**
|
|
1811
|
+
|
|
1812
|
+
```python
|
|
1813
|
+
from crca_sd import StateVector, ControlVector, DynamicsModel
|
|
1814
|
+
from crca_sd.crca_sd_mpc import MPCSolver
|
|
1815
|
+
from crca_sd import get_crca_agent
|
|
1816
|
+
|
|
1817
|
+
# Initialize CRCA agent for causal reasoning
|
|
1818
|
+
crca = get_crca_agent(
|
|
1819
|
+
variables=['GDP', 'unemployment', 'inflation'],
|
|
1820
|
+
model_name="gpt-4o-mini"
|
|
1821
|
+
)
|
|
1822
|
+
|
|
1823
|
+
# Create MPC solver with CRCA integration
|
|
1824
|
+
solver = MPCSolver(
|
|
1825
|
+
dynamics_model=dynamics_model,
|
|
1826
|
+
crca_agent=crca
|
|
1827
|
+
)
|
|
1828
|
+
|
|
1829
|
+
# Solve with causal scenario generation
|
|
1830
|
+
solution = solver.solve(
|
|
1831
|
+
initial_state=state_vector,
|
|
1832
|
+
horizon=12,
|
|
1833
|
+
use_causal_scenarios=True
|
|
1834
|
+
)
|
|
1835
|
+
```
|
|
1836
|
+
|
|
1837
|
+
### CRCA-CG Module
|
|
1838
|
+
|
|
1839
|
+
CRCA-CG (Corporate Governance) provides a multi-agent orchestration system for corporate governance with board oversight, executive leadership, ESG frameworks, and democratic decision-making.
|
|
1840
|
+
|
|
1841
|
+
#### Core Components
|
|
1842
|
+
|
|
1843
|
+
- **CorporateSwarm**: Main multi-agent orchestration system
|
|
1844
|
+
- **ESG Scoring**: Environmental, Social, and Governance metrics
|
|
1845
|
+
- **Risk Assessment**: Comprehensive risk analysis
|
|
1846
|
+
- **Stakeholder Engagement**: Stakeholder interaction management
|
|
1847
|
+
- **Regulatory Compliance**: Compliance monitoring
|
|
1848
|
+
|
|
1849
|
+
#### CRCA Integration
|
|
1850
|
+
|
|
1851
|
+
CorporateSwarm uses CRCAAgent for causal policy analysis:
|
|
1852
|
+
|
|
1853
|
+
```python
|
|
1854
|
+
from crca_cg.corposwarm import CorporateSwarm
|
|
1855
|
+
from CRCA import CRCAAgent
|
|
1856
|
+
|
|
1857
|
+
# Initialize CRCA agent
|
|
1858
|
+
crca = CRCAAgent(
|
|
1859
|
+
variables=['revenue', 'costs', 'employee_satisfaction', 'market_share'],
|
|
1860
|
+
model_name="gpt-4o"
|
|
1861
|
+
)
|
|
1862
|
+
|
|
1863
|
+
# Create CorporateSwarm with CRCA
|
|
1864
|
+
swarm = CorporateSwarm(
|
|
1865
|
+
crca_agent=crca,
|
|
1866
|
+
enable_crca_q=True, # Enable CRCA-Q integration for Investment Committee
|
|
1867
|
+
enable_crca_sd=True # Enable CRCA-SD integration for enhanced voting
|
|
1868
|
+
)
|
|
1869
|
+
|
|
1870
|
+
# Use causal reasoning in governance decisions
|
|
1871
|
+
result = swarm.analyze_policy_impact(
|
|
1872
|
+
policy_description="Increase R&D spending by 20%",
|
|
1873
|
+
use_causal_analysis=True
|
|
1874
|
+
)
|
|
1875
|
+
```
|
|
1876
|
+
|
|
1877
|
+
#### CRCA-Q Integration
|
|
1878
|
+
|
|
1879
|
+
CorporateSwarm can integrate CRCA-Q for Investment Committee decisions:
|
|
1880
|
+
|
|
1881
|
+
```python
|
|
1882
|
+
from crca_cg.corposwarm import CorporateSwarm
|
|
1883
|
+
|
|
1884
|
+
swarm = CorporateSwarm(
|
|
1885
|
+
enable_crca_q=True,
|
|
1886
|
+
crca_q_config={
|
|
1887
|
+
'account_size': 1000000,
|
|
1888
|
+
'conservative_mode': True
|
|
1889
|
+
}
|
|
1890
|
+
)
|
|
1891
|
+
|
|
1892
|
+
# Investment Committee uses CRCA-Q for trading decisions
|
|
1893
|
+
investment_decision = swarm.investment_committee.recommend_allocation()
|
|
1894
|
+
```
|
|
1895
|
+
|
|
1896
|
+
#### CRCA-SD Integration
|
|
1897
|
+
|
|
1898
|
+
Enhanced voting and governance using CRCA-SD:
|
|
1899
|
+
|
|
1900
|
+
```python
|
|
1901
|
+
swarm = CorporateSwarm(
|
|
1902
|
+
enable_crca_sd=True,
|
|
1903
|
+
crca_sd_config={
|
|
1904
|
+
'governance_model': 'democratic',
|
|
1905
|
+
'voting_threshold': 0.6
|
|
1906
|
+
}
|
|
1907
|
+
)
|
|
1908
|
+
|
|
1909
|
+
# Enhanced voting with CRCA-SD governance
|
|
1910
|
+
vote_result = swarm.board.vote_on_proposal(
|
|
1911
|
+
proposal="Approve new product launch",
|
|
1912
|
+
use_enhanced_voting=True
|
|
1913
|
+
)
|
|
1914
|
+
```
|
|
1915
|
+
|
|
1916
|
+
---
|
|
1917
|
+
|
|
1918
|
+
## Policy Engine System
|
|
1919
|
+
|
|
1920
|
+
The Policy Engine System provides a comprehensive temporal policy execution framework with online learning, multi-objective optimization, drift detection, and deterministic decision-making. It enables autonomous policy execution with audit trails, rollback capabilities, and causal scenario generation.
|
|
1921
|
+
|
|
1922
|
+
### Overview
|
|
1923
|
+
|
|
1924
|
+
The Policy Engine implements a doctrine-based policy system where:
|
|
1925
|
+
- **Doctrine**: Defines the policy specification (metrics, objectives, invariants, levers, risk budget)
|
|
1926
|
+
- **Compiled Policy**: Normalized, compiled version with deterministic selectors
|
|
1927
|
+
- **Ledger**: Event-sourced append-only audit trail
|
|
1928
|
+
- **Policy Loop**: Temporal execution with observe → plan → act → update cycle
|
|
1929
|
+
- **Sensors**: Read system metrics and state
|
|
1930
|
+
- **Actuators**: Execute interventions on system controls
|
|
1931
|
+
- **Rollback**: Recovery system for failed interventions
|
|
1932
|
+
- **MPC Planner**: Model Predictive Control for multi-step optimization
|
|
1933
|
+
- **Drift Detection**: Change-point detection for regime shifts
|
|
1934
|
+
|
|
1935
|
+
### Core Components
|
|
1936
|
+
|
|
1937
|
+
#### DoctrineV1: Policy Specification
|
|
1938
|
+
|
|
1939
|
+
A doctrine defines the complete policy plan with epochs, metrics, objectives, invariants, levers, and risk budget.
|
|
1940
|
+
|
|
1941
|
+
**Example 1: Basic Doctrine Creation**
|
|
1942
|
+
|
|
1943
|
+
```python
|
|
1944
|
+
from schemas.policy import (
|
|
1945
|
+
DoctrineV1, EpochConfig, MetricSpec, Objective,
|
|
1946
|
+
Invariant, LeverSpec, RiskBudget
|
|
1947
|
+
)
|
|
1948
|
+
|
|
1949
|
+
# Define epoch configuration (1 hour epochs)
|
|
1950
|
+
epoch = EpochConfig(unit="hours", length=1.0, timezone="UTC")
|
|
1951
|
+
|
|
1952
|
+
# Define metrics to track
|
|
1953
|
+
metrics = {
|
|
1954
|
+
"cpu_usage": MetricSpec(
|
|
1955
|
+
extractor_key="cpu_percent",
|
|
1956
|
+
unit="percent",
|
|
1957
|
+
description="CPU utilization percentage"
|
|
1958
|
+
),
|
|
1959
|
+
"memory_usage": MetricSpec(
|
|
1960
|
+
extractor_key="memory_percent",
|
|
1961
|
+
unit="percent",
|
|
1962
|
+
description="Memory utilization percentage"
|
|
1963
|
+
),
|
|
1964
|
+
"request_latency": MetricSpec(
|
|
1965
|
+
extractor_key="latency_ms",
|
|
1966
|
+
unit="milliseconds",
|
|
1967
|
+
description="Request latency in milliseconds"
|
|
1968
|
+
)
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
# Define objectives (multi-objective optimization)
|
|
1972
|
+
objectives = [
|
|
1973
|
+
Objective(
|
|
1974
|
+
metric_name="request_latency",
|
|
1975
|
+
direction="minimize",
|
|
1976
|
+
target_value=100.0, # Target: <100ms
|
|
1977
|
+
deadline_epoch=24, # Within 24 epochs
|
|
1978
|
+
weight=2.0 # Higher priority
|
|
1979
|
+
),
|
|
1980
|
+
Objective(
|
|
1981
|
+
metric_name="cpu_usage",
|
|
1982
|
+
direction="minimize",
|
|
1983
|
+
target_value=70.0, # Target: <70%
|
|
1984
|
+
weight=1.0
|
|
1985
|
+
)
|
|
1986
|
+
]
|
|
1987
|
+
|
|
1988
|
+
# Define invariants (hard constraints)
|
|
1989
|
+
invariants = [
|
|
1990
|
+
Invariant(
|
|
1991
|
+
name="memory_safety",
|
|
1992
|
+
condition="memory_usage < 90.0",
|
|
1993
|
+
description="Memory must never exceed 90%"
|
|
1994
|
+
),
|
|
1995
|
+
Invariant(
|
|
1996
|
+
name="latency_sla",
|
|
1997
|
+
condition="request_latency < 500.0",
|
|
1998
|
+
description="Latency must never exceed 500ms SLA"
|
|
1999
|
+
)
|
|
2000
|
+
]
|
|
2001
|
+
|
|
2002
|
+
# Define levers (allowed interventions)
|
|
2003
|
+
levers = {
|
|
2004
|
+
"cpu_throttle": LeverSpec(
|
|
2005
|
+
lever_type="cpu_throttle",
|
|
2006
|
+
bounds={"nice_value": {"min": 0, "max": 19}},
|
|
2007
|
+
cost_function="nice_value * 0.1",
|
|
2008
|
+
rollback_required=True,
|
|
2009
|
+
description="CPU throttling via nice values"
|
|
2010
|
+
),
|
|
2011
|
+
"scale_replicas": LeverSpec(
|
|
2012
|
+
lever_type="scale_replicas",
|
|
2013
|
+
bounds={"replicas": {"min": 1, "max": 10}},
|
|
2014
|
+
cost_function="replicas * 10.0",
|
|
2015
|
+
rollback_required=True,
|
|
2016
|
+
description="Scale service replicas"
|
|
2017
|
+
)
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
# Define risk budget
|
|
2021
|
+
risk_budget = RiskBudget(
|
|
2022
|
+
max_actions_per_epoch=5,
|
|
2023
|
+
max_risk_per_epoch=0.5,
|
|
2024
|
+
rollback_required=True
|
|
2025
|
+
)
|
|
2026
|
+
|
|
2027
|
+
# Create doctrine
|
|
2028
|
+
doctrine = DoctrineV1(
|
|
2029
|
+
epoch=epoch,
|
|
2030
|
+
metrics=metrics,
|
|
2031
|
+
objectives=objectives,
|
|
2032
|
+
invariants=invariants,
|
|
2033
|
+
levers=levers,
|
|
2034
|
+
risk_budget=risk_budget,
|
|
2035
|
+
version="1.0"
|
|
2036
|
+
)
|
|
2037
|
+
|
|
2038
|
+
# Save to file
|
|
2039
|
+
doctrine.to_json("policy_doctrine.json")
|
|
2040
|
+
```
|
|
2041
|
+
|
|
2042
|
+
**Example 2: Loading Doctrine from JSON**
|
|
2043
|
+
|
|
2044
|
+
```python
|
|
2045
|
+
from schemas.policy import DoctrineV1
|
|
2046
|
+
|
|
2047
|
+
# Load doctrine from JSON file
|
|
2048
|
+
doctrine = DoctrineV1.from_json("policy_doctrine.json")
|
|
2049
|
+
|
|
2050
|
+
print(f"Doctrine version: {doctrine.version}")
|
|
2051
|
+
print(f"Metrics: {list(doctrine.metrics.keys())}")
|
|
2052
|
+
print(f"Objectives: {len(doctrine.objectives)}")
|
|
2053
|
+
print(f"Levers: {list(doctrine.levers.keys())}")
|
|
2054
|
+
```
|
|
2055
|
+
|
|
2056
|
+
**Example 3: Multi-Objective Policy with Deadlines**
|
|
2057
|
+
|
|
2058
|
+
```python
|
|
2059
|
+
from schemas.policy import DoctrineV1, Objective, EpochConfig, MetricSpec, RiskBudget
|
|
2060
|
+
|
|
2061
|
+
# Complex multi-objective policy
|
|
2062
|
+
objectives = [
|
|
2063
|
+
# Primary objective: minimize latency (high weight, urgent deadline)
|
|
2064
|
+
Objective(
|
|
2065
|
+
metric_name="p95_latency",
|
|
2066
|
+
direction="minimize",
|
|
2067
|
+
target_value=200.0,
|
|
2068
|
+
deadline_epoch=12, # Must achieve within 12 hours
|
|
2069
|
+
weight=3.0
|
|
2070
|
+
),
|
|
2071
|
+
# Secondary objective: minimize cost (medium weight, no deadline)
|
|
2072
|
+
Objective(
|
|
2073
|
+
metric_name="hourly_cost",
|
|
2074
|
+
direction="minimize",
|
|
2075
|
+
weight=1.5
|
|
2076
|
+
),
|
|
2077
|
+
# Tertiary objective: maximize throughput (lower weight)
|
|
2078
|
+
Objective(
|
|
2079
|
+
metric_name="requests_per_second",
|
|
2080
|
+
direction="maximize",
|
|
2081
|
+
target_value=1000.0,
|
|
2082
|
+
weight=1.0
|
|
2083
|
+
)
|
|
2084
|
+
]
|
|
2085
|
+
|
|
2086
|
+
doctrine = DoctrineV1(
|
|
2087
|
+
epoch=EpochConfig(unit="hours", length=1.0),
|
|
2088
|
+
metrics={
|
|
2089
|
+
"p95_latency": MetricSpec(extractor_key="p95_latency_ms", unit="ms"),
|
|
2090
|
+
"hourly_cost": MetricSpec(extractor_key="cost_usd", unit="USD"),
|
|
2091
|
+
"requests_per_second": MetricSpec(extractor_key="rps", unit="req/s")
|
|
2092
|
+
},
|
|
2093
|
+
objectives=objectives,
|
|
2094
|
+
invariants=[],
|
|
2095
|
+
levers={},
|
|
2096
|
+
risk_budget=RiskBudget()
|
|
2097
|
+
)
|
|
2098
|
+
```
|
|
2099
|
+
|
|
2100
|
+
#### CompiledPolicy: Normalized Policy
|
|
2101
|
+
|
|
2102
|
+
The compiled policy normalizes weights, resolves IDs, and creates deterministic selectors.
|
|
2103
|
+
|
|
2104
|
+
**Example 4: Compiling a Doctrine**
|
|
2105
|
+
|
|
2106
|
+
```python
|
|
2107
|
+
from schemas.policy import DoctrineV1, CompiledPolicy
|
|
2108
|
+
|
|
2109
|
+
# Create doctrine
|
|
2110
|
+
doctrine = DoctrineV1(...) # As defined above
|
|
2111
|
+
|
|
2112
|
+
# Compile to normalized policy
|
|
2113
|
+
compiled = CompiledPolicy.compile(doctrine)
|
|
2114
|
+
|
|
2115
|
+
print(f"Policy hash: {compiled.policy_hash}")
|
|
2116
|
+
print(f"Normalized weights: {compiled.normalized_weights}")
|
|
2117
|
+
print(f"Resolved metric IDs: {compiled.resolved_metric_ids}")
|
|
2118
|
+
print(f"Deadline epochs: {compiled.deadline_epochs}")
|
|
2119
|
+
```
|
|
2120
|
+
|
|
2121
|
+
#### Ledger: Event-Sourced Audit Trail
|
|
2122
|
+
|
|
2123
|
+
The ledger provides an append-only event store for all policy decisions and outcomes.
|
|
2124
|
+
|
|
2125
|
+
**Example 5: Recording Events in Ledger**
|
|
2126
|
+
|
|
2127
|
+
```python
|
|
2128
|
+
from utils.ledger import Ledger
|
|
2129
|
+
from schemas.policy import ObservationEvent, DecisionEvent, OutcomeEvent
|
|
2130
|
+
|
|
2131
|
+
# Initialize ledger
|
|
2132
|
+
ledger = Ledger(db_path="policy_ledger.db")
|
|
2133
|
+
|
|
2134
|
+
# Record observation event
|
|
2135
|
+
observation = ObservationEvent(
|
|
2136
|
+
epoch=1,
|
|
2137
|
+
metrics={"cpu_usage": 75.5, "memory_usage": 60.2, "latency_ms": 120.0}
|
|
2138
|
+
)
|
|
2139
|
+
event_id = ledger.append(observation)
|
|
2140
|
+
print(f"Recorded observation event: {event_id}")
|
|
2141
|
+
|
|
2142
|
+
# Record decision event
|
|
2143
|
+
decision = DecisionEvent(
|
|
2144
|
+
epoch=1,
|
|
2145
|
+
interventions=[
|
|
2146
|
+
{"lever_id": "cpu_throttle", "parameters": {"nice_value": 5}}
|
|
2147
|
+
],
|
|
2148
|
+
rationale="CPU usage high, throttling to reduce load",
|
|
2149
|
+
decision_hash="abc123..."
|
|
2150
|
+
)
|
|
2151
|
+
event_id = ledger.append(decision)
|
|
2152
|
+
print(f"Recorded decision event: {event_id}")
|
|
2153
|
+
|
|
2154
|
+
# Record outcome event
|
|
2155
|
+
outcome = OutcomeEvent(
|
|
2156
|
+
epoch=1,
|
|
2157
|
+
metrics={"cpu_usage": 65.0, "memory_usage": 58.0, "latency_ms": 95.0},
|
|
2158
|
+
objectives_met={"request_latency": True, "cpu_usage": True}
|
|
2159
|
+
)
|
|
2160
|
+
event_id = ledger.append(outcome)
|
|
2161
|
+
print(f"Recorded outcome event: {event_id}")
|
|
2162
|
+
```
|
|
2163
|
+
|
|
2164
|
+
**Example 6: Querying Ledger History**
|
|
2165
|
+
|
|
2166
|
+
```python
|
|
2167
|
+
from utils.ledger import Ledger
|
|
2168
|
+
|
|
2169
|
+
ledger = Ledger(db_path="policy_ledger.db")
|
|
2170
|
+
|
|
2171
|
+
# Get all events for a specific epoch
|
|
2172
|
+
epoch_events = ledger.get_events_by_epoch(epoch=1)
|
|
2173
|
+
print(f"Found {len(epoch_events)} events in epoch 1")
|
|
2174
|
+
|
|
2175
|
+
# Get events in a window
|
|
2176
|
+
window_events = ledger.get_events_window(start_epoch=1, end_epoch=10)
|
|
2177
|
+
print(f"Found {len(window_events)} events in window [1, 10]")
|
|
2178
|
+
|
|
2179
|
+
# Get events by type
|
|
2180
|
+
observations = ledger.get_events_by_type("observation", start_epoch=1, end_epoch=24)
|
|
2181
|
+
print(f"Found {len(observations)} observation events")
|
|
2182
|
+
|
|
2183
|
+
# Replay events (for deterministic execution)
|
|
2184
|
+
replay_events = ledger.replay_events(start_epoch=1, end_epoch=10)
|
|
2185
|
+
for event in replay_events:
|
|
2186
|
+
print(f"Event {event['id']}: {event['type']} at epoch {event['epoch']}")
|
|
2187
|
+
```
|
|
2188
|
+
|
|
2189
|
+
#### RollbackManager: Intervention Recovery
|
|
2190
|
+
|
|
2191
|
+
The rollback manager provides checkpoint creation and intervention recovery.
|
|
2192
|
+
|
|
2193
|
+
**Example 7: Creating Checkpoints and Rollback**
|
|
2194
|
+
|
|
2195
|
+
```python
|
|
2196
|
+
from utils.rollback import RollbackManager
|
|
2197
|
+
from schemas.policy import InterventionSpec
|
|
2198
|
+
|
|
2199
|
+
# Initialize rollback manager
|
|
2200
|
+
rollback = RollbackManager(db_path="rollback.db", retention_days=7)
|
|
2201
|
+
|
|
2202
|
+
# Create checkpoint before intervention
|
|
2203
|
+
state = {
|
|
2204
|
+
"cpu_usage": 70.0,
|
|
2205
|
+
"memory_usage": 60.0,
|
|
2206
|
+
"replicas": 3
|
|
2207
|
+
}
|
|
2208
|
+
checkpoint_id = rollback.create_checkpoint(epoch=1, state=state)
|
|
2209
|
+
print(f"Created checkpoint: {checkpoint_id}")
|
|
2210
|
+
|
|
2211
|
+
# Record intervention
|
|
2212
|
+
intervention = InterventionSpec(
|
|
2213
|
+
lever_id="scale_replicas",
|
|
2214
|
+
parameters={"replicas": 5}
|
|
2215
|
+
)
|
|
2216
|
+
intervention_id = rollback.record_intervention(
|
|
2217
|
+
checkpoint_id=checkpoint_id,
|
|
2218
|
+
epoch=1,
|
|
2219
|
+
intervention=intervention,
|
|
2220
|
+
result={"status": "success", "new_replicas": 5}
|
|
2221
|
+
)
|
|
2222
|
+
print(f"Recorded intervention: {intervention_id}")
|
|
2223
|
+
|
|
2224
|
+
# If intervention fails, rollback
|
|
2225
|
+
if intervention_failed:
|
|
2226
|
+
rollback_result = rollback.rollback_n_steps(
|
|
2227
|
+
epoch=1,
|
|
2228
|
+
n_steps=1
|
|
2229
|
+
)
|
|
2230
|
+
print(f"Rolled back {rollback_result['n_rolled_back']} interventions")
|
|
2231
|
+
|
|
2232
|
+
# Restore checkpoint
|
|
2233
|
+
restored_state = rollback.restore_checkpoint(checkpoint_id)
|
|
2234
|
+
print(f"Restored state: {restored_state}")
|
|
2235
|
+
```
|
|
2236
|
+
|
|
2237
|
+
**Example 8: Automatic Rollback on Failure**
|
|
2238
|
+
|
|
2239
|
+
```python
|
|
2240
|
+
from utils.rollback import RollbackManager
|
|
2241
|
+
|
|
2242
|
+
rollback = RollbackManager()
|
|
2243
|
+
|
|
2244
|
+
# Create checkpoint
|
|
2245
|
+
checkpoint_id = rollback.create_checkpoint(epoch=1, state=current_state)
|
|
2246
|
+
|
|
2247
|
+
try:
|
|
2248
|
+
# Execute intervention
|
|
2249
|
+
result = execute_intervention(intervention)
|
|
2250
|
+
rollback.record_intervention(
|
|
2251
|
+
checkpoint_id=checkpoint_id,
|
|
2252
|
+
epoch=1,
|
|
2253
|
+
intervention=intervention,
|
|
2254
|
+
result=result
|
|
2255
|
+
)
|
|
2256
|
+
except Exception as e:
|
|
2257
|
+
# Automatic rollback on failure
|
|
2258
|
+
logger.error(f"Intervention failed: {e}")
|
|
2259
|
+
rollback.rollback_n_steps(epoch=1, n_steps=1)
|
|
2260
|
+
restored_state = rollback.restore_checkpoint(checkpoint_id)
|
|
2261
|
+
logger.info(f"Rolled back to checkpoint: {checkpoint_id}")
|
|
2262
|
+
```
|
|
2263
|
+
|
|
2264
|
+
#### PolicyLoopMixin: Temporal Policy Execution
|
|
2265
|
+
|
|
2266
|
+
The policy loop mixin provides the observe → plan → act → update cycle with online learning.
|
|
2267
|
+
|
|
2268
|
+
**Example 9: Basic Policy Loop Execution**
|
|
2269
|
+
|
|
2270
|
+
```python
|
|
2271
|
+
from templates.policy_loop import PolicyLoopMixin
|
|
2272
|
+
from schemas.policy import DoctrineV1
|
|
2273
|
+
from utils.ledger import Ledger
|
|
2274
|
+
|
|
2275
|
+
# Create doctrine and ledger
|
|
2276
|
+
doctrine = DoctrineV1.from_json("policy_doctrine.json")
|
|
2277
|
+
ledger = Ledger(db_path="policy_ledger.db")
|
|
2278
|
+
|
|
2279
|
+
# Create agent with policy loop mixin
|
|
2280
|
+
class PolicyAgent(PolicyLoopMixin):
|
|
2281
|
+
def __init__(self, doctrine, ledger):
|
|
2282
|
+
super().__init__(doctrine, ledger, seed=42)
|
|
2283
|
+
|
|
2284
|
+
agent = PolicyAgent(doctrine, ledger)
|
|
2285
|
+
|
|
2286
|
+
# Run single epoch
|
|
2287
|
+
result = agent.run_epoch(epoch=1)
|
|
2288
|
+
|
|
2289
|
+
print(f"Epoch {result['epoch']} completed")
|
|
2290
|
+
print(f"Interventions: {result['interventions']}")
|
|
2291
|
+
print(f"Objectives met: {result['objectives_met']}")
|
|
2292
|
+
print(f"Decision hash: {result['decision_hash']}")
|
|
2293
|
+
```
|
|
2294
|
+
|
|
2295
|
+
**Example 10: Policy Loop with Online Learning**
|
|
2296
|
+
|
|
2297
|
+
```python
|
|
2298
|
+
from templates.policy_loop import PolicyLoopMixin
|
|
2299
|
+
from schemas.policy import DoctrineV1
|
|
2300
|
+
from utils.ledger import Ledger
|
|
2301
|
+
|
|
2302
|
+
class LearningPolicyAgent(PolicyLoopMixin):
|
|
2303
|
+
def __init__(self, doctrine, ledger):
|
|
2304
|
+
super().__init__(doctrine, ledger, seed=42)
|
|
2305
|
+
|
|
2306
|
+
agent = LearningPolicyAgent(doctrine, ledger)
|
|
2307
|
+
|
|
2308
|
+
# Run multiple epochs with online learning
|
|
2309
|
+
for epoch in range(1, 25): # 24 hours
|
|
2310
|
+
result = agent.run_epoch(epoch=epoch)
|
|
2311
|
+
|
|
2312
|
+
# Online learning updates model after each epoch
|
|
2313
|
+
agent.update_models(epoch=epoch)
|
|
2314
|
+
|
|
2315
|
+
print(f"Epoch {epoch}: {result['objectives_met']}")
|
|
2316
|
+
print(f"Model updated with {len(result['metrics'])} observations")
|
|
2317
|
+
```
|
|
2318
|
+
|
|
2319
|
+
#### MPCPlanner: Model Predictive Control
|
|
2320
|
+
|
|
2321
|
+
The MPC planner provides multi-step optimization with horizon-based prediction.
|
|
2322
|
+
|
|
2323
|
+
**Example 11: MPC Planning for Multi-Step Optimization**
|
|
2324
|
+
|
|
2325
|
+
```python
|
|
2326
|
+
from templates.mpc_planner import MPCPlanner
|
|
2327
|
+
from schemas.policy import DoctrineV1
|
|
2328
|
+
import numpy as np
|
|
2329
|
+
|
|
2330
|
+
# Create doctrine
|
|
2331
|
+
doctrine = DoctrineV1(...)
|
|
2332
|
+
|
|
2333
|
+
# Initialize MPC planner with 5-step horizon
|
|
2334
|
+
planner = MPCPlanner(
|
|
2335
|
+
doctrine=doctrine,
|
|
2336
|
+
horizon=5, # Plan 5 steps ahead
|
|
2337
|
+
use_robust=False
|
|
2338
|
+
)
|
|
2339
|
+
|
|
2340
|
+
# Current state
|
|
2341
|
+
x_t = np.array([75.0, 60.0, 120.0]) # [cpu, memory, latency]
|
|
2342
|
+
|
|
2343
|
+
# State transition matrices (if available)
|
|
2344
|
+
A = np.array([[0.9, 0.0, 0.0], [0.0, 0.95, 0.0], [0.0, 0.0, 0.85]])
|
|
2345
|
+
B = np.array([[0.1, 0.0], [0.0, 0.05], [0.0, 0.15]])
|
|
2346
|
+
|
|
2347
|
+
# Solve MPC problem
|
|
2348
|
+
interventions, score, rationale = planner.solve_mpc(
|
|
2349
|
+
x_t=x_t,
|
|
2350
|
+
A=A,
|
|
2351
|
+
B=B,
|
|
2352
|
+
objectives=[{"metric_name": "latency_ms", "weight": 2.0, "direction": "minimize"}],
|
|
2353
|
+
constraints=[],
|
|
2354
|
+
lever_bounds={"cpu_throttle": {"nice_value": {"min": 0, "max": 19}}}
|
|
2355
|
+
)
|
|
2356
|
+
|
|
2357
|
+
print(f"MPC solution score: {score}")
|
|
2358
|
+
print(f"Interventions: {interventions}")
|
|
2359
|
+
print(f"Rationale: {rationale}")
|
|
2360
|
+
```
|
|
2361
|
+
|
|
2362
|
+
**Example 12: Robust MPC with Uncertainty**
|
|
2363
|
+
|
|
2364
|
+
```python
|
|
2365
|
+
from templates.mpc_planner import MPCPlanner
|
|
2366
|
+
|
|
2367
|
+
# Initialize robust MPC planner
|
|
2368
|
+
planner = MPCPlanner(
|
|
2369
|
+
doctrine=doctrine,
|
|
2370
|
+
horizon=5,
|
|
2371
|
+
use_robust=True, # Enable robust MPC
|
|
2372
|
+
uncertainty_set_size=0.1 # 10% uncertainty
|
|
2373
|
+
)
|
|
2374
|
+
|
|
2375
|
+
# Solve with uncertainty sets
|
|
2376
|
+
interventions, score, rationale = planner.solve_mpc(...)
|
|
2377
|
+
```
|
|
2378
|
+
|
|
2379
|
+
#### DriftDetector: Change-Point Detection
|
|
2380
|
+
|
|
2381
|
+
The drift detector identifies regime shifts and change points in metrics.
|
|
2382
|
+
|
|
2383
|
+
**Example 13: Detecting Drift in Metrics**
|
|
2384
|
+
|
|
2385
|
+
```python
|
|
2386
|
+
from templates.drift_detection import DriftDetector
|
|
2387
|
+
|
|
2388
|
+
# Initialize drift detector
|
|
2389
|
+
detector = DriftDetector(
|
|
2390
|
+
algorithm="pelt", # Optimal segmentation
|
|
2391
|
+
min_size=2,
|
|
2392
|
+
penalty=10.0,
|
|
2393
|
+
model="rbf"
|
|
2394
|
+
)
|
|
2395
|
+
|
|
2396
|
+
# Metric history (e.g., latency over time)
|
|
2397
|
+
latency_history = [95.0, 98.0, 102.0, 105.0, 110.0, 115.0, 120.0, 125.0, 130.0, 135.0]
|
|
2398
|
+
|
|
2399
|
+
# Detect change points
|
|
2400
|
+
change_points, confidence = detector.detect_changepoints(
|
|
2401
|
+
metric_name="latency",
|
|
2402
|
+
metric_history=latency_history,
|
|
2403
|
+
min_history=5
|
|
2404
|
+
)
|
|
2405
|
+
|
|
2406
|
+
print(f"Change points detected at indices: {change_points}")
|
|
2407
|
+
print(f"Confidence scores: {confidence}")
|
|
2408
|
+
|
|
2409
|
+
# Check if drift detected
|
|
2410
|
+
if len(change_points) > 0:
|
|
2411
|
+
print("WARNING: Drift detected in latency metric!")
|
|
2412
|
+
print(f"Regime change at epoch {change_points[0]}")
|
|
2413
|
+
```
|
|
2414
|
+
|
|
2415
|
+
**Example 14: Multi-Metric Drift Detection**
|
|
2416
|
+
|
|
2417
|
+
```python
|
|
2418
|
+
from templates.drift_detection import DriftDetector
|
|
2419
|
+
|
|
2420
|
+
detector = DriftDetector(algorithm="pelt")
|
|
2421
|
+
|
|
2422
|
+
# Monitor multiple metrics
|
|
2423
|
+
metrics_history = {
|
|
2424
|
+
"cpu_usage": [50.0, 52.0, 55.0, 60.0, 65.0, 70.0, 75.0],
|
|
2425
|
+
"memory_usage": [40.0, 42.0, 45.0, 50.0, 55.0, 60.0, 65.0],
|
|
2426
|
+
"latency": [100.0, 105.0, 110.0, 120.0, 130.0, 140.0, 150.0]
|
|
2427
|
+
}
|
|
2428
|
+
|
|
2429
|
+
# Detect drift in all metrics
|
|
2430
|
+
for metric_name, history in metrics_history.items():
|
|
2431
|
+
change_points, confidence = detector.detect_changepoints(
|
|
2432
|
+
metric_name=metric_name,
|
|
2433
|
+
metric_history=history
|
|
2434
|
+
)
|
|
2435
|
+
|
|
2436
|
+
if len(change_points) > 0:
|
|
2437
|
+
print(f"Drift detected in {metric_name} at epochs: {change_points}")
|
|
2438
|
+
```
|
|
2439
|
+
|
|
2440
|
+
#### Sensors: Metric Collection
|
|
2441
|
+
|
|
2442
|
+
Sensors read system metrics and state snapshots.
|
|
2443
|
+
|
|
2444
|
+
**Example 15: System Metrics Sensor**
|
|
2445
|
+
|
|
2446
|
+
```python
|
|
2447
|
+
from tools.sensors import SystemMetricsSensor
|
|
2448
|
+
|
|
2449
|
+
# Initialize system metrics sensor
|
|
2450
|
+
sensor = SystemMetricsSensor()
|
|
2451
|
+
|
|
2452
|
+
# Read current metrics
|
|
2453
|
+
snapshot = sensor.read()
|
|
2454
|
+
|
|
2455
|
+
print(f"CPU usage: {snapshot['cpu_percent']:.1f}%")
|
|
2456
|
+
print(f"Memory usage: {snapshot['memory_percent']:.1f}%")
|
|
2457
|
+
print(f"Memory available: {snapshot['memory_available_gb']:.2f} GB")
|
|
2458
|
+
print(f"Disk usage: {snapshot['disk_usage_percent']:.1f}%")
|
|
2459
|
+
print(f"Process count: {snapshot['process_count']:.0f}")
|
|
2460
|
+
```
|
|
2461
|
+
|
|
2462
|
+
**Example 16: Custom Sensor Implementation**
|
|
2463
|
+
|
|
2464
|
+
```python
|
|
2465
|
+
from tools.sensors import BaseSensor
|
|
2466
|
+
from typing import Dict
|
|
2467
|
+
|
|
2468
|
+
class CustomMetricsSensor(BaseSensor):
|
|
2469
|
+
"""Custom sensor for application-specific metrics."""
|
|
2470
|
+
|
|
2471
|
+
def read(self) -> Dict[str, float]:
|
|
2472
|
+
"""Read custom metrics."""
|
|
2473
|
+
# Example: Read from application API
|
|
2474
|
+
import requests
|
|
2475
|
+
|
|
2476
|
+
response = requests.get("http://app:8080/metrics")
|
|
2477
|
+
data = response.json()
|
|
2478
|
+
|
|
2479
|
+
return {
|
|
2480
|
+
"request_rate": data.get("requests_per_second", 0.0),
|
|
2481
|
+
"error_rate": data.get("errors_per_second", 0.0),
|
|
2482
|
+
"active_connections": float(data.get("active_connections", 0)),
|
|
2483
|
+
"queue_depth": float(data.get("queue_depth", 0))
|
|
2484
|
+
}
|
|
2485
|
+
|
|
2486
|
+
# Use custom sensor
|
|
2487
|
+
sensor = CustomMetricsSensor()
|
|
2488
|
+
snapshot = sensor.read()
|
|
2489
|
+
print(f"Request rate: {snapshot['request_rate']:.1f} req/s")
|
|
2490
|
+
```
|
|
2491
|
+
|
|
2492
|
+
#### Actuators: Intervention Execution
|
|
2493
|
+
|
|
2494
|
+
Actuators execute interventions on system controls.
|
|
2495
|
+
|
|
2496
|
+
**Example 17: System Control Actuator**
|
|
2497
|
+
|
|
2498
|
+
```python
|
|
2499
|
+
from tools.actuators import SystemControlActuator
|
|
2500
|
+
from schemas.policy import InterventionSpec
|
|
2501
|
+
|
|
2502
|
+
# Initialize actuator
|
|
2503
|
+
actuator = SystemControlActuator(require_root=False)
|
|
2504
|
+
|
|
2505
|
+
# Create intervention
|
|
2506
|
+
intervention = InterventionSpec(
|
|
2507
|
+
lever_id="cpu_throttle",
|
|
2508
|
+
parameters={"nice_value": 10, "pid": 12345}
|
|
2509
|
+
)
|
|
2510
|
+
|
|
2511
|
+
# Execute intervention
|
|
2512
|
+
result = actuator.execute([intervention])
|
|
2513
|
+
|
|
2514
|
+
print(f"Executed {result['executed']} interventions")
|
|
2515
|
+
for r in result['results']:
|
|
2516
|
+
print(f"Status: {r['result']['status']}")
|
|
2517
|
+
```
|
|
2518
|
+
|
|
2519
|
+
**Example 18: Custom Actuator Implementation**
|
|
2520
|
+
|
|
2521
|
+
```python
|
|
2522
|
+
from tools.actuators import BaseActuator
|
|
2523
|
+
from schemas.policy import InterventionSpec
|
|
2524
|
+
from typing import Dict, List, Any
|
|
2525
|
+
|
|
2526
|
+
class KubernetesActuator(BaseActuator):
|
|
2527
|
+
"""Actuator for Kubernetes deployments."""
|
|
2528
|
+
|
|
2529
|
+
def execute(self, interventions: List[InterventionSpec]) -> Dict[str, Any]:
|
|
2530
|
+
"""Execute Kubernetes interventions."""
|
|
2531
|
+
results = []
|
|
2532
|
+
|
|
2533
|
+
for intervention in interventions:
|
|
2534
|
+
if intervention.lever_id == "scale_replicas":
|
|
2535
|
+
replicas = intervention.parameters["replicas"]
|
|
2536
|
+
deployment = intervention.parameters["deployment"]
|
|
2537
|
+
|
|
2538
|
+
# Scale deployment (pseudo-code)
|
|
2539
|
+
# kubectl scale deployment {deployment} --replicas={replicas}
|
|
2540
|
+
result = self._scale_deployment(deployment, replicas)
|
|
2541
|
+
|
|
2542
|
+
results.append({
|
|
2543
|
+
"intervention": intervention.model_dump(),
|
|
2544
|
+
"result": result
|
|
2545
|
+
})
|
|
2546
|
+
|
|
2547
|
+
return {"executed": len(results), "results": results}
|
|
2548
|
+
|
|
2549
|
+
def _scale_deployment(self, deployment: str, replicas: int) -> Dict[str, Any]:
|
|
2550
|
+
"""Scale Kubernetes deployment."""
|
|
2551
|
+
# Implementation would call kubectl or Kubernetes API
|
|
2552
|
+
return {"status": "success", "deployment": deployment, "replicas": replicas}
|
|
2553
|
+
|
|
2554
|
+
# Use custom actuator
|
|
2555
|
+
actuator = KubernetesActuator()
|
|
2556
|
+
intervention = InterventionSpec(
|
|
2557
|
+
lever_id="scale_replicas",
|
|
2558
|
+
parameters={"deployment": "web-app", "replicas": 5}
|
|
2559
|
+
)
|
|
2560
|
+
result = actuator.execute([intervention])
|
|
2561
|
+
```
|
|
2562
|
+
|
|
2563
|
+
#### Canonical: Deterministic Hashing
|
|
2564
|
+
|
|
2565
|
+
The canonical module provides stable hashing for reproducible decisions.
|
|
2566
|
+
|
|
2567
|
+
**Example 19: Stable Hashing for Decision Artifacts**
|
|
2568
|
+
|
|
2569
|
+
```python
|
|
2570
|
+
from utils.canonical import stable_hash, canonical_bytes
|
|
2571
|
+
|
|
2572
|
+
# Hash a decision artifact
|
|
2573
|
+
decision = {
|
|
2574
|
+
"epoch": 1,
|
|
2575
|
+
"interventions": [
|
|
2576
|
+
{"lever_id": "cpu_throttle", "parameters": {"nice_value": 5}}
|
|
2577
|
+
],
|
|
2578
|
+
"metrics": {"cpu_usage": 75.5, "memory_usage": 60.2}
|
|
2579
|
+
}
|
|
2580
|
+
|
|
2581
|
+
# Compute stable hash (same input = same hash)
|
|
2582
|
+
hash1 = stable_hash(decision)
|
|
2583
|
+
hash2 = stable_hash(decision) # Identical input
|
|
2584
|
+
|
|
2585
|
+
assert hash1 == hash2
|
|
2586
|
+
print(f"Decision hash: {hash1}")
|
|
2587
|
+
|
|
2588
|
+
# Hash with quantized floats (for numeric stability)
|
|
2589
|
+
from utils.canonical import quantize_float
|
|
2590
|
+
|
|
2591
|
+
value = 75.5123456789
|
|
2592
|
+
quantized = quantize_float(value, mode="decimal", p=1e-6)
|
|
2593
|
+
print(f"Original: {value}, Quantized: {quantized}")
|
|
2594
|
+
```
|
|
2595
|
+
|
|
2596
|
+
**Example 20: Canonical Bytes for Deterministic Serialization**
|
|
2597
|
+
|
|
2598
|
+
```python
|
|
2599
|
+
from utils.canonical import canonical_bytes, stable_hash
|
|
2600
|
+
|
|
2601
|
+
# Convert object to canonical bytes
|
|
2602
|
+
obj = {
|
|
2603
|
+
"a": 1,
|
|
2604
|
+
"b": [2, 3, 4],
|
|
2605
|
+
"c": {"x": 10.5, "y": 20.3}
|
|
2606
|
+
}
|
|
2607
|
+
|
|
2608
|
+
canonical = canonical_bytes(obj)
|
|
2609
|
+
hash_value = stable_hash(obj)
|
|
2610
|
+
|
|
2611
|
+
print(f"Canonical bytes length: {len(canonical)}")
|
|
2612
|
+
print(f"Stable hash: {hash_value}")
|
|
2613
|
+
|
|
2614
|
+
# Same object in different order produces same hash
|
|
2615
|
+
obj2 = {"c": {"y": 20.3, "x": 10.5}, "a": 1, "b": [2, 3, 4]}
|
|
2616
|
+
hash2 = stable_hash(obj2)
|
|
2617
|
+
|
|
2618
|
+
assert hash_value == hash2 # Same hash despite different key order
|
|
2619
|
+
```
|
|
2620
|
+
|
|
2621
|
+
### Complete End-to-End Example
|
|
2622
|
+
|
|
2623
|
+
**Example 21: Full Policy Engine Workflow**
|
|
2624
|
+
|
|
2625
|
+
```python
|
|
2626
|
+
from schemas.policy import DoctrineV1, EpochConfig, MetricSpec, Objective, RiskBudget
|
|
2627
|
+
from utils.ledger import Ledger
|
|
2628
|
+
from utils.rollback import RollbackManager
|
|
2629
|
+
from templates.policy_loop import PolicyLoopMixin
|
|
2630
|
+
from tools.sensors import SystemMetricsSensor
|
|
2631
|
+
from tools.actuators import SystemControlActuator
|
|
2632
|
+
|
|
2633
|
+
# Step 1: Create doctrine
|
|
2634
|
+
doctrine = DoctrineV1(
|
|
2635
|
+
epoch=EpochConfig(unit="hours", length=1.0),
|
|
2636
|
+
metrics={
|
|
2637
|
+
"cpu_usage": MetricSpec(extractor_key="cpu_percent", unit="percent"),
|
|
2638
|
+
"memory_usage": MetricSpec(extractor_key="memory_percent", unit="percent")
|
|
2639
|
+
},
|
|
2640
|
+
objectives=[
|
|
2641
|
+
Objective(metric_name="cpu_usage", direction="minimize", target_value=70.0, weight=1.0)
|
|
2642
|
+
],
|
|
2643
|
+
invariants=[],
|
|
2644
|
+
levers={
|
|
2645
|
+
"cpu_throttle": LeverSpec(
|
|
2646
|
+
lever_type="cpu_throttle",
|
|
2647
|
+
bounds={"nice_value": {"min": 0, "max": 19}},
|
|
2648
|
+
rollback_required=True
|
|
2649
|
+
)
|
|
2650
|
+
},
|
|
2651
|
+
risk_budget=RiskBudget(max_actions_per_epoch=5)
|
|
2652
|
+
)
|
|
2653
|
+
|
|
2654
|
+
# Step 2: Initialize ledger and rollback
|
|
2655
|
+
ledger = Ledger(db_path="policy_ledger.db")
|
|
2656
|
+
rollback = RollbackManager(db_path="rollback.db")
|
|
2657
|
+
|
|
2658
|
+
# Step 3: Initialize sensors and actuators
|
|
2659
|
+
sensor = SystemMetricsSensor()
|
|
2660
|
+
actuator = SystemControlActuator()
|
|
2661
|
+
|
|
2662
|
+
# Step 4: Create policy agent
|
|
2663
|
+
class FullPolicyAgent(PolicyLoopMixin):
|
|
2664
|
+
def __init__(self, doctrine, ledger, sensor, actuator):
|
|
2665
|
+
super().__init__(doctrine, ledger, seed=42)
|
|
2666
|
+
self.sensor = sensor
|
|
2667
|
+
self.actuator = actuator
|
|
2668
|
+
|
|
2669
|
+
def observe(self, epoch: int):
|
|
2670
|
+
"""Observe current state."""
|
|
2671
|
+
return self.sensor.read()
|
|
2672
|
+
|
|
2673
|
+
def act(self, interventions, epoch: int):
|
|
2674
|
+
"""Execute interventions."""
|
|
2675
|
+
return self.actuator.execute(interventions)
|
|
2676
|
+
|
|
2677
|
+
agent = FullPolicyAgent(doctrine, ledger, sensor, actuator)
|
|
2678
|
+
|
|
2679
|
+
# Step 5: Run policy loop
|
|
2680
|
+
for epoch in range(1, 25):
|
|
2681
|
+
# Create checkpoint
|
|
2682
|
+
state = agent.observe(epoch)
|
|
2683
|
+
checkpoint_id = rollback.create_checkpoint(epoch, state)
|
|
2684
|
+
|
|
2685
|
+
try:
|
|
2686
|
+
# Run epoch
|
|
2687
|
+
result = agent.run_epoch(epoch=epoch)
|
|
2688
|
+
|
|
2689
|
+
# Record intervention
|
|
2690
|
+
for intervention in result['interventions']:
|
|
2691
|
+
rollback.record_intervention(
|
|
2692
|
+
checkpoint_id=checkpoint_id,
|
|
2693
|
+
epoch=epoch,
|
|
2694
|
+
intervention=intervention,
|
|
2695
|
+
result={"status": "success"}
|
|
2696
|
+
)
|
|
2697
|
+
|
|
2698
|
+
print(f"Epoch {epoch}: {result['objectives_met']}")
|
|
2699
|
+
except Exception as e:
|
|
2700
|
+
# Rollback on failure
|
|
2701
|
+
logger.error(f"Epoch {epoch} failed: {e}")
|
|
2702
|
+
rollback.rollback_n_steps(epoch, n_steps=1)
|
|
2703
|
+
```
|
|
2704
|
+
|
|
2705
|
+
---
|
|
2706
|
+
|
|
2707
|
+
## Branch Integrations
|
|
2708
|
+
|
|
2709
|
+
### CRCA-Q Integration
|
|
2710
|
+
|
|
2711
|
+
CRCA-Q (Quantitative Trading) is a single-file quantitative trading system integrating causal reasoning with traditional quantitative finance techniques.
|
|
2712
|
+
|
|
2713
|
+
#### Overview
|
|
2714
|
+
|
|
2715
|
+
CRCA-Q uses causal inference to understand why market movements occur, enabling more robust predictions that remain valid across different market regimes. Unlike correlation-based systems, it implements structural causal models for market analysis.
|
|
2716
|
+
|
|
2717
|
+
#### Key Features
|
|
2718
|
+
|
|
2719
|
+
- **Causal Signal Generation**: Signals based on causal relationships, not correlations
|
|
2720
|
+
- **Portfolio Optimization**: CVaR-based optimization with causal constraints
|
|
2721
|
+
- **Risk Management**: Causal risk modeling and stress testing
|
|
2722
|
+
- **Multi-Asset Support**: Automatic asset discovery and rotation
|
|
2723
|
+
- **Alternative Data Integration**: On-chain, social, news data sources
|
|
2724
|
+
|
|
2725
|
+
#### Integration with CRCAAgent
|
|
2726
|
+
|
|
2727
|
+
```python
|
|
2728
|
+
from branches.CRCA_Q import QuantTradingAgent
|
|
2729
|
+
from CRCA import CRCAAgent
|
|
2730
|
+
|
|
2731
|
+
# QuantTradingAgent uses CRCAAgent internally for causal reasoning
|
|
2732
|
+
# It automatically creates and manages CRCAAgent instances
|
|
2733
|
+
|
|
2734
|
+
agent = QuantTradingAgent(
|
|
2735
|
+
account_size=10000,
|
|
2736
|
+
model_name="gpt-4o-mini",
|
|
2737
|
+
use_crca_agent_heavily=True,
|
|
2738
|
+
crca_max_loops=5
|
|
2739
|
+
)
|
|
2740
|
+
|
|
2741
|
+
# Run trading loop with causal reasoning
|
|
2742
|
+
result = agent.run(
|
|
2743
|
+
task="Analyze market and generate trading signals",
|
|
2744
|
+
use_causal_validation=True
|
|
2745
|
+
)
|
|
2746
|
+
```
|
|
2747
|
+
|
|
2748
|
+
#### Usage in CorporateSwarm
|
|
2749
|
+
|
|
2750
|
+
CRCA-Q can be integrated into CorporateSwarm for Investment Committee decisions:
|
|
2751
|
+
|
|
2752
|
+
```python
|
|
2753
|
+
from crca_cg.corposwarm import CorporateSwarm
|
|
2754
|
+
|
|
2755
|
+
swarm = CorporateSwarm(
|
|
2756
|
+
enable_crca_q=True,
|
|
2757
|
+
crca_q_config={
|
|
2758
|
+
'account_size': 1000000,
|
|
2759
|
+
'conservative_mode': True,
|
|
2760
|
+
'use_crca_agent_heavily': True
|
|
2761
|
+
}
|
|
2762
|
+
)
|
|
2763
|
+
|
|
2764
|
+
# Investment Committee uses CRCA-Q
|
|
2765
|
+
allocation = swarm.investment_committee.recommend_allocation()
|
|
2766
|
+
```
|
|
2767
|
+
|
|
2768
|
+
### CRCA-SD Integration
|
|
2769
|
+
|
|
2770
|
+
CRCA-SD integrates CRCAAgent for causal scenario generation in model-predictive control systems.
|
|
2771
|
+
|
|
2772
|
+
#### Integration Pattern
|
|
2773
|
+
|
|
2774
|
+
```python
|
|
2775
|
+
from crca_sd import get_crca_agent
|
|
2776
|
+
from crca_sd.crca_sd_mpc import ScenarioGenerator
|
|
2777
|
+
|
|
2778
|
+
# Get CRCAAgent instance (returns None if CRCA not available)
|
|
2779
|
+
crca = get_crca_agent(
|
|
2780
|
+
variables=['GDP', 'unemployment', 'inflation', 'stability'],
|
|
2781
|
+
model_name="gpt-4o-mini"
|
|
2782
|
+
)
|
|
2783
|
+
|
|
2784
|
+
if crca:
|
|
2785
|
+
# Create scenario generator with CRCA
|
|
2786
|
+
gen = ScenarioGenerator(crca_agent=crca)
|
|
2787
|
+
|
|
2788
|
+
# Generate causal scenarios instead of random noise
|
|
2789
|
+
scenarios = gen.generate_causal_scenarios(
|
|
2790
|
+
n_scenarios=10,
|
|
2791
|
+
horizon=12,
|
|
2792
|
+
current_state=state_vector,
|
|
2793
|
+
target_variables=['GDP', 'unemployment']
|
|
2794
|
+
)
|
|
2795
|
+
else:
|
|
2796
|
+
# Fallback to Gaussian scenarios
|
|
2797
|
+
scenarios = gen.generate_gaussian(n_scenarios=10, horizon=12)
|
|
2798
|
+
```
|
|
2799
|
+
|
|
2800
|
+
#### Benefits
|
|
2801
|
+
|
|
2802
|
+
- **Causal Understanding**: Scenarios based on causal relationships
|
|
2803
|
+
- **Counterfactual Analysis**: Explores "what-if" scenarios
|
|
2804
|
+
- **Better Policy Optimization**: MPC uses causally-informed scenarios
|
|
2805
|
+
|
|
2806
|
+
### CRCA-CG Integration
|
|
2807
|
+
|
|
2808
|
+
CorporateSwarm integrates both CRCAAgent and CRCA-SD for enhanced governance.
|
|
2809
|
+
|
|
2810
|
+
#### CRCAAgent Integration
|
|
2811
|
+
|
|
2812
|
+
```python
|
|
2813
|
+
from crca_cg.corposwarm import CorporateSwarm
|
|
2814
|
+
from CRCA import CRCAAgent
|
|
2815
|
+
|
|
2816
|
+
# Initialize CRCA agent for causal policy analysis
|
|
2817
|
+
crca = CRCAAgent(
|
|
2818
|
+
variables=['revenue', 'costs', 'employee_satisfaction'],
|
|
2819
|
+
model_name="gpt-4o"
|
|
2820
|
+
)
|
|
2821
|
+
|
|
2822
|
+
swarm = CorporateSwarm(crca_agent=crca)
|
|
2823
|
+
|
|
2824
|
+
# Analyze policy impact using causal reasoning
|
|
2825
|
+
result = swarm.analyze_policy_impact(
|
|
2826
|
+
policy_description="Increase R&D spending by 20%",
|
|
2827
|
+
use_causal_analysis=True
|
|
2828
|
+
)
|
|
2829
|
+
```
|
|
2830
|
+
|
|
2831
|
+
#### CRCA-SD Integration
|
|
2832
|
+
|
|
2833
|
+
Enhanced voting and governance:
|
|
2834
|
+
|
|
2835
|
+
```python
|
|
2836
|
+
swarm = CorporateSwarm(
|
|
2837
|
+
enable_crca_sd=True,
|
|
2838
|
+
crca_sd_config={
|
|
2839
|
+
'governance_model': 'democratic',
|
|
2840
|
+
'voting_threshold': 0.6
|
|
2841
|
+
}
|
|
2842
|
+
)
|
|
2843
|
+
|
|
2844
|
+
# Enhanced voting with CRCA-SD governance system
|
|
2845
|
+
vote_result = swarm.board.vote_on_proposal(
|
|
2846
|
+
proposal="Approve new product launch",
|
|
2847
|
+
use_enhanced_voting=True
|
|
2848
|
+
)
|
|
2849
|
+
```
|
|
2850
|
+
|
|
2851
|
+
---
|
|
2852
|
+
|
|
2853
|
+
## Usage Examples
|
|
2854
|
+
|
|
2855
|
+
### Example 1: Automatic Variable Extraction
|
|
2856
|
+
|
|
2857
|
+
Demonstrates the automatic variable extraction feature:
|
|
2858
|
+
|
|
2859
|
+
```python
|
|
2860
|
+
from CRCA import CRCAAgent
|
|
2861
|
+
|
|
2862
|
+
# No variables declared - automatic extraction
|
|
2863
|
+
agent = CRCAAgent(model_name="gpt-4o-mini")
|
|
2864
|
+
|
|
2865
|
+
# Task automatically triggers extraction and analysis
|
|
2866
|
+
task = "Analyze how increasing marketing budget affects sales and customer acquisition"
|
|
2867
|
+
result = agent.run(task=task)
|
|
2868
|
+
|
|
2869
|
+
# Variables were automatically extracted
|
|
2870
|
+
print(f"Variables: {agent.get_nodes()}")
|
|
2871
|
+
print(f"Edges: {agent.get_edges()}")
|
|
2872
|
+
print(f"Analysis: {result['causal_analysis'][:200]}...")
|
|
2873
|
+
```
|
|
2874
|
+
|
|
2875
|
+
**Variation 1: Multiple Related Variables**
|
|
2876
|
+
|
|
2877
|
+
```python
|
|
2878
|
+
from CRCA import CRCAAgent
|
|
2879
|
+
|
|
2880
|
+
agent = CRCAAgent(model_name="gpt-4o-mini")
|
|
2881
|
+
|
|
2882
|
+
# Complex task with multiple variables
|
|
2883
|
+
task = """
|
|
2884
|
+
Analyze the causal relationships between:
|
|
2885
|
+
- Product quality and customer satisfaction
|
|
2886
|
+
- Customer satisfaction and brand loyalty
|
|
2887
|
+
- Brand loyalty and market share
|
|
2888
|
+
- Market share and revenue growth
|
|
2889
|
+
"""
|
|
2890
|
+
result = agent.run(task=task)
|
|
2891
|
+
|
|
2892
|
+
print(f"Extracted {len(agent.get_nodes())} variables")
|
|
2893
|
+
print(f"Created {len(agent.get_edges())} causal relationships")
|
|
2894
|
+
```
|
|
2895
|
+
|
|
2896
|
+
**Variation 2: Error Handling**
|
|
2897
|
+
|
|
2898
|
+
```python
|
|
2899
|
+
from CRCA import CRCAAgent
|
|
2900
|
+
from loguru import logger
|
|
2901
|
+
|
|
2902
|
+
agent = CRCAAgent(model_name="gpt-4o-mini")
|
|
2903
|
+
|
|
2904
|
+
try:
|
|
2905
|
+
# Task with unclear causal relationships
|
|
2906
|
+
task = "What is the weather today?"
|
|
2907
|
+
result = agent.run(task=task)
|
|
2908
|
+
|
|
2909
|
+
if len(agent.get_nodes()) == 0:
|
|
2910
|
+
logger.warning("No variables extracted - task may not contain causal relationships")
|
|
2911
|
+
# Provide more explicit task
|
|
2912
|
+
task = "Analyze how temperature affects ice cream sales"
|
|
2913
|
+
result = agent.run(task=task)
|
|
2914
|
+
except Exception as e:
|
|
2915
|
+
logger.error(f"Variable extraction failed: {e}")
|
|
2916
|
+
```
|
|
2917
|
+
|
|
2918
|
+
### Example 2: LLM-Based Causal Analysis
|
|
2919
|
+
|
|
2920
|
+
Full LLM-based causal analysis workflow:
|
|
2921
|
+
|
|
2922
|
+
```python
|
|
2923
|
+
from CRCA import CRCAAgent
|
|
2924
|
+
|
|
2925
|
+
agent = CRCAAgent(
|
|
2926
|
+
model_name="gpt-4o",
|
|
2927
|
+
max_loops=3,
|
|
2928
|
+
agent_max_loops=3
|
|
2929
|
+
)
|
|
2930
|
+
|
|
2931
|
+
task = "Why do large software projects fail despite adequate funding and skilled engineers?"
|
|
2932
|
+
result = agent.run(task=task)
|
|
2933
|
+
|
|
2934
|
+
print("Causal Analysis:")
|
|
2935
|
+
print(result["causal_analysis"])
|
|
2936
|
+
|
|
2937
|
+
print("\nCounterfactual Scenarios:")
|
|
2938
|
+
for i, scenario in enumerate(result["counterfactual_scenarios"][:3], 1):
|
|
2939
|
+
print(f"{i}. {scenario.name}")
|
|
2940
|
+
print(f" Interventions: {scenario.interventions}")
|
|
2941
|
+
print(f" Reasoning: {scenario.reasoning[:100]}...")
|
|
2942
|
+
```
|
|
2943
|
+
|
|
2944
|
+
**Variation 1: Extended Analysis with More Loops**
|
|
2945
|
+
|
|
2946
|
+
```python
|
|
2947
|
+
from CRCA import CRCAAgent
|
|
2948
|
+
|
|
2949
|
+
# Use more loops for deeper analysis
|
|
2950
|
+
agent = CRCAAgent(
|
|
2951
|
+
model_name="gpt-4o",
|
|
2952
|
+
max_loops=5, # More causal reasoning loops
|
|
2953
|
+
agent_max_loops=5 # More standard agent loops
|
|
2954
|
+
)
|
|
2955
|
+
|
|
2956
|
+
task = "Analyze the root causes of economic recessions"
|
|
2957
|
+
result = agent.run(task=task)
|
|
2958
|
+
|
|
2959
|
+
# Get detailed analysis
|
|
2960
|
+
print(f"Analysis length: {len(result['causal_analysis'])} characters")
|
|
2961
|
+
print(f"Generated {len(result['counterfactual_scenarios'])} scenarios")
|
|
2962
|
+
```
|
|
2963
|
+
|
|
2964
|
+
**Variation 2: Domain-Specific Analysis**
|
|
2965
|
+
|
|
2966
|
+
```python
|
|
2967
|
+
from CRCA import CRCAAgent
|
|
2968
|
+
|
|
2969
|
+
agent = CRCAAgent(
|
|
2970
|
+
model_name="gpt-4o",
|
|
2971
|
+
system_prompt="You are an expert healthcare analyst specializing in public health policy."
|
|
2972
|
+
)
|
|
2973
|
+
|
|
2974
|
+
task = "Analyze how vaccination rates affect disease spread and healthcare costs"
|
|
2975
|
+
result = agent.run(task=task)
|
|
2976
|
+
|
|
2977
|
+
# Domain-specific analysis
|
|
2978
|
+
print(result["causal_analysis"])
|
|
2979
|
+
```
|
|
2980
|
+
|
|
2981
|
+
### Example 3: Deterministic Simulation
|
|
2982
|
+
|
|
2983
|
+
Traditional deterministic causal simulation:
|
|
2984
|
+
|
|
2985
|
+
```python
|
|
2986
|
+
from CRCA import CRCAAgent
|
|
2987
|
+
|
|
2988
|
+
agent = CRCAAgent(variables=["price", "demand", "revenue"])
|
|
2989
|
+
agent.add_causal_relationship("price", "demand", strength=-0.5)
|
|
2990
|
+
agent.add_causal_relationship("price", "revenue", strength=1.0)
|
|
2991
|
+
agent.add_causal_relationship("demand", "revenue", strength=0.8)
|
|
2992
|
+
|
|
2993
|
+
initial_state = {"price": 100.0, "demand": 1000.0, "revenue": 100000.0}
|
|
2994
|
+
result = agent.run(initial_state=initial_state, max_steps=3)
|
|
2995
|
+
|
|
2996
|
+
print("Evolved State:")
|
|
2997
|
+
for var, value in result["evolved_state"].items():
|
|
2998
|
+
print(f" {var}: {value:.2f}")
|
|
2999
|
+
|
|
3000
|
+
print(f"\nGenerated {len(result['counterfactual_scenarios'])} scenarios")
|
|
3001
|
+
```
|
|
3002
|
+
|
|
3003
|
+
**Variation 1: Multi-Step Simulation**
|
|
3004
|
+
|
|
3005
|
+
```python
|
|
3006
|
+
from CRCA import CRCAAgent
|
|
3007
|
+
|
|
3008
|
+
agent = CRCAAgent(variables=["price", "demand", "inventory", "revenue"])
|
|
3009
|
+
agent.add_causal_relationship("price", "demand", strength=-0.5)
|
|
3010
|
+
agent.add_causal_relationship("demand", "inventory", strength=-0.3)
|
|
3011
|
+
agent.add_causal_relationship("price", "revenue", strength=1.0)
|
|
3012
|
+
agent.add_causal_relationship("demand", "revenue", strength=0.8)
|
|
3013
|
+
|
|
3014
|
+
# Simulate over multiple steps
|
|
3015
|
+
initial_state = {"price": 100.0, "demand": 1000.0, "inventory": 5000.0, "revenue": 100000.0}
|
|
3016
|
+
result = agent.run(initial_state=initial_state, max_steps=10)
|
|
3017
|
+
|
|
3018
|
+
# Track evolution over time
|
|
3019
|
+
print("State Evolution:")
|
|
3020
|
+
for step, state in enumerate(result.get("state_history", []), 1):
|
|
3021
|
+
print(f"Step {step}: price={state.get('price', 0):.2f}, demand={state.get('demand', 0):.2f}")
|
|
3022
|
+
```
|
|
3023
|
+
|
|
3024
|
+
**Variation 2: Non-Linear Relationships**
|
|
3025
|
+
|
|
3026
|
+
```python
|
|
3027
|
+
from CRCA import CRCAAgent
|
|
3028
|
+
|
|
3029
|
+
agent = CRCAAgent(
|
|
3030
|
+
variables=["price", "demand"],
|
|
3031
|
+
use_nonlinear=True # Enable non-linear SCM
|
|
3032
|
+
)
|
|
3033
|
+
|
|
3034
|
+
agent.add_causal_relationship("price", "demand", strength=-0.5)
|
|
3035
|
+
|
|
3036
|
+
# Non-linear simulation
|
|
3037
|
+
initial_state = {"price": 100.0, "demand": 1000.0}
|
|
3038
|
+
result = agent.run(initial_state=initial_state, max_steps=5)
|
|
3039
|
+
|
|
3040
|
+
print("Non-linear evolution:")
|
|
3041
|
+
print(result["evolved_state"])
|
|
3042
|
+
```
|
|
3043
|
+
|
|
3044
|
+
### Example 4: Integration with CRCA-SD
|
|
3045
|
+
|
|
3046
|
+
Using CRCAAgent with CRCA-SD for socioeconomic modeling:
|
|
3047
|
+
|
|
3048
|
+
```python
|
|
3049
|
+
from crca_sd import get_crca_agent, StateVector
|
|
3050
|
+
from crca_sd.crca_sd_mpc import MPCSolver
|
|
3051
|
+
|
|
3052
|
+
# Get CRCAAgent for causal reasoning
|
|
3053
|
+
crca = get_crca_agent(
|
|
3054
|
+
variables=['GDP', 'unemployment', 'inflation'],
|
|
3055
|
+
model_name="gpt-4o-mini"
|
|
3056
|
+
)
|
|
3057
|
+
|
|
3058
|
+
# Create state vector
|
|
3059
|
+
state = StateVector({
|
|
3060
|
+
'GDP': 1000.0,
|
|
3061
|
+
'unemployment': 5.0,
|
|
3062
|
+
'inflation': 2.0
|
|
3063
|
+
})
|
|
3064
|
+
|
|
3065
|
+
# Use CRCA for causal scenario generation in MPC
|
|
3066
|
+
if crca:
|
|
3067
|
+
solver = MPCSolver(crca_agent=crca)
|
|
3068
|
+
solution = solver.solve(initial_state=state, use_causal_scenarios=True)
|
|
3069
|
+
```
|
|
3070
|
+
|
|
3071
|
+
**Variation 1: Complete CRCA-SD Workflow**
|
|
3072
|
+
|
|
3073
|
+
```python
|
|
3074
|
+
from crca_sd import (
|
|
3075
|
+
get_crca_agent, StateVector, ControlVector, DynamicsModel,
|
|
3076
|
+
ConstraintChecker, ForwardSimulator
|
|
3077
|
+
)
|
|
3078
|
+
from crca_sd.crca_sd_mpc import MPCSolver, ScenarioGenerator
|
|
3079
|
+
|
|
3080
|
+
# Initialize CRCA agent
|
|
3081
|
+
crca = get_crca_agent(
|
|
3082
|
+
variables=['Y', 'U', 'S', 'P', 'L', 'W', 'K', 'I'],
|
|
3083
|
+
model_name="gpt-4o-mini"
|
|
3084
|
+
)
|
|
3085
|
+
|
|
3086
|
+
# Create state vector
|
|
3087
|
+
state = StateVector(
|
|
3088
|
+
Y=1000000.0, # GDP
|
|
3089
|
+
U=0.05, # Unemployment
|
|
3090
|
+
S=0.8, # Stability
|
|
3091
|
+
P=1000000.0, # Population
|
|
3092
|
+
L=500000.0, # Labor
|
|
3093
|
+
W=1.0, # Wages
|
|
3094
|
+
K=1000000.0, # Capital
|
|
3095
|
+
I=0.8 # Infrastructure
|
|
3096
|
+
)
|
|
3097
|
+
|
|
3098
|
+
# Create dynamics model
|
|
3099
|
+
dynamics = DynamicsModel()
|
|
3100
|
+
checker = ConstraintChecker()
|
|
3101
|
+
simulator = ForwardSimulator(dynamics, checker)
|
|
3102
|
+
|
|
3103
|
+
# Create MPC solver with CRCA
|
|
3104
|
+
solver = MPCSolver(
|
|
3105
|
+
dynamics_model=dynamics,
|
|
3106
|
+
crca_agent=crca
|
|
3107
|
+
)
|
|
3108
|
+
|
|
3109
|
+
# Solve with causal scenarios
|
|
3110
|
+
solution = solver.solve(
|
|
3111
|
+
initial_state=state,
|
|
3112
|
+
horizon=12,
|
|
3113
|
+
use_causal_scenarios=True
|
|
3114
|
+
)
|
|
3115
|
+
|
|
3116
|
+
print(f"Solution found: {solution['optimal']}")
|
|
3117
|
+
print(f"Control sequence: {solution['control_sequence']}")
|
|
3118
|
+
```
|
|
3119
|
+
|
|
3120
|
+
**Variation 2: Scenario Generation with CRCA**
|
|
3121
|
+
|
|
3122
|
+
```python
|
|
3123
|
+
from crca_sd.crca_sd_mpc import ScenarioGenerator
|
|
3124
|
+
from crca_sd import get_crca_agent, StateVector
|
|
3125
|
+
|
|
3126
|
+
# Get CRCA agent
|
|
3127
|
+
crca = get_crca_agent(
|
|
3128
|
+
variables=['GDP', 'unemployment', 'inflation'],
|
|
3129
|
+
model_name="gpt-4o-mini"
|
|
3130
|
+
)
|
|
3131
|
+
|
|
3132
|
+
# Create scenario generator
|
|
3133
|
+
gen = ScenarioGenerator(crca_agent=crca)
|
|
3134
|
+
|
|
3135
|
+
# Generate causal scenarios
|
|
3136
|
+
state = StateVector(GDP=1000.0, unemployment=5.0, inflation=2.0)
|
|
3137
|
+
scenarios = gen.generate_causal_scenarios(
|
|
3138
|
+
n_scenarios=10,
|
|
3139
|
+
horizon=12,
|
|
3140
|
+
current_state=state,
|
|
3141
|
+
target_variables=['GDP', 'unemployment']
|
|
3142
|
+
)
|
|
3143
|
+
|
|
3144
|
+
print(f"Generated {len(scenarios)} causal scenarios")
|
|
3145
|
+
for i, scenario in enumerate(scenarios[:3], 1):
|
|
3146
|
+
print(f"Scenario {i}: {scenario}")
|
|
3147
|
+
```
|
|
3148
|
+
|
|
3149
|
+
### Example 5: CRCA-SD Complete Workflow
|
|
3150
|
+
|
|
3151
|
+
Complete example from `examples/crca_sd_example.py`:
|
|
3152
|
+
|
|
3153
|
+
```python
|
|
3154
|
+
from crca_sd.crca_sd_core import (
|
|
3155
|
+
StateVector, ControlVector, DynamicsModel,
|
|
3156
|
+
ConstraintChecker, ForwardSimulator
|
|
3157
|
+
)
|
|
3158
|
+
from crca_sd.crca_sd_mpc import MPCSolver, ScenarioGenerator
|
|
3159
|
+
from crca_sd import get_crca_agent
|
|
3160
|
+
|
|
3161
|
+
# Phase A: Basic simulation
|
|
3162
|
+
x_0 = StateVector(
|
|
3163
|
+
P=1000000.0, # Population
|
|
3164
|
+
L=500000.0, # Labor force
|
|
3165
|
+
U=0.05, # Unemployment
|
|
3166
|
+
W=1.0, # Wage proxy
|
|
3167
|
+
S=0.8, # Stability
|
|
3168
|
+
Y=1000000.0 # GDP
|
|
3169
|
+
)
|
|
3170
|
+
|
|
3171
|
+
dynamics = DynamicsModel()
|
|
3172
|
+
checker = ConstraintChecker()
|
|
3173
|
+
simulator = ForwardSimulator(dynamics, checker)
|
|
3174
|
+
|
|
3175
|
+
# Sample budget policies
|
|
3176
|
+
policies = [
|
|
3177
|
+
ControlVector.sample_budget_simplex(["energy", "food", "infrastructure"])
|
|
3178
|
+
for _ in range(5)
|
|
3179
|
+
]
|
|
3180
|
+
|
|
3181
|
+
# Simulate each policy
|
|
3182
|
+
for i, policy in enumerate(policies):
|
|
3183
|
+
trajectory, feasibility, violation = simulator.simulate_scenario(
|
|
3184
|
+
x_0, policy, [{}] * 10, horizon=10
|
|
3185
|
+
)
|
|
3186
|
+
print(f"Policy {i+1}: {sum(feasibility)}/10 steps feasible")
|
|
3187
|
+
|
|
3188
|
+
# Phase B: MPC solver with CRCA
|
|
3189
|
+
crca = get_crca_agent(
|
|
3190
|
+
variables=['Y', 'U', 'S', 'P', 'L', 'W', 'K', 'I'],
|
|
3191
|
+
model_name="gpt-4o-mini"
|
|
3192
|
+
)
|
|
3193
|
+
|
|
3194
|
+
if crca:
|
|
3195
|
+
solver = MPCSolver(dynamics_model=dynamics, crca_agent=crca)
|
|
3196
|
+
solution = solver.solve(initial_state=x_0, horizon=12, use_causal_scenarios=True)
|
|
3197
|
+
print(f"MPC solution: {solution}")
|
|
3198
|
+
```
|
|
3199
|
+
|
|
3200
|
+
### Example 6: CorporateSwarm Complete Example
|
|
3201
|
+
|
|
3202
|
+
Complete example from `examples/logistics_corporation.py`:
|
|
3203
|
+
|
|
3204
|
+
```python
|
|
3205
|
+
from crca_cg.corposwarm import (
|
|
3206
|
+
CorporateSwarm, CorporateRole, DepartmentType,
|
|
3207
|
+
ProposalType, BoardCommitteeType
|
|
3208
|
+
)
|
|
3209
|
+
from utils.tui import CorporateSwarmTUI
|
|
3210
|
+
|
|
3211
|
+
# Create logistics corporation
|
|
3212
|
+
swarm = CorporateSwarm(
|
|
3213
|
+
company_name="UAB Leiliona",
|
|
3214
|
+
industry="Logistics and Supply Chain Management"
|
|
3215
|
+
)
|
|
3216
|
+
|
|
3217
|
+
# Add board members
|
|
3218
|
+
swarm.add_board_member(
|
|
3219
|
+
name="John Doe",
|
|
3220
|
+
role=CorporateRole.CHAIRMAN,
|
|
3221
|
+
expertise=["logistics", "supply_chain"]
|
|
3222
|
+
)
|
|
3223
|
+
|
|
3224
|
+
# Add executives
|
|
3225
|
+
swarm.add_executive(
|
|
3226
|
+
name="Jane Smith",
|
|
3227
|
+
role=CorporateRole.CEO,
|
|
3228
|
+
department=DepartmentType.OPERATIONS
|
|
3229
|
+
)
|
|
3230
|
+
|
|
3231
|
+
# Create department
|
|
3232
|
+
swarm.create_department(
|
|
3233
|
+
name="Operations",
|
|
3234
|
+
dept_type=DepartmentType.OPERATIONS,
|
|
3235
|
+
head="Jane Smith"
|
|
3236
|
+
)
|
|
3237
|
+
|
|
3238
|
+
# Create proposal
|
|
3239
|
+
proposal = swarm.create_proposal(
|
|
3240
|
+
title="Expand warehouse capacity",
|
|
3241
|
+
description="Build new warehouse facility to increase capacity by 50%",
|
|
3242
|
+
proposal_type=ProposalType.STRATEGIC,
|
|
3243
|
+
budget_impact=5000000.0
|
|
3244
|
+
)
|
|
3245
|
+
|
|
3246
|
+
# Vote on proposal
|
|
3247
|
+
vote_result = swarm.board.vote_on_proposal(proposal)
|
|
3248
|
+
print(f"Proposal passed: {vote_result.passed}")
|
|
3249
|
+
print(f"Votes for: {vote_result.votes_for}, against: {vote_result.votes_against}")
|
|
3250
|
+
|
|
3251
|
+
# Use TUI for interactive management
|
|
3252
|
+
if TUI_AVAILABLE:
|
|
3253
|
+
tui = CorporateSwarmTUI(swarm)
|
|
3254
|
+
tui.run()
|
|
3255
|
+
```
|
|
3256
|
+
|
|
3257
|
+
**Variation 1: ESG Scoring and Risk Assessment**
|
|
3258
|
+
|
|
3259
|
+
```python
|
|
3260
|
+
from crca_cg.corposwarm import CorporateSwarm
|
|
3261
|
+
|
|
3262
|
+
swarm = CorporateSwarm(company_name="Example Corp")
|
|
3263
|
+
|
|
3264
|
+
# Calculate ESG score
|
|
3265
|
+
esg_score = swarm.calculate_esg_score()
|
|
3266
|
+
print(f"ESG Score: {esg_score['total_score']:.2f}")
|
|
3267
|
+
print(f"Environmental: {esg_score['environmental']:.2f}")
|
|
3268
|
+
print(f"Social: {esg_score['social']:.2f}")
|
|
3269
|
+
print(f"Governance: {esg_score['governance']:.2f}")
|
|
3270
|
+
|
|
3271
|
+
# Risk assessment
|
|
3272
|
+
risk_assessment = swarm.assess_risks()
|
|
3273
|
+
print(f"Overall risk level: {risk_assessment['overall_risk']}")
|
|
3274
|
+
print(f"Risk factors: {risk_assessment['risk_factors']}")
|
|
3275
|
+
```
|
|
3276
|
+
|
|
3277
|
+
**Variation 2: Integration with CRCA-Q**
|
|
3278
|
+
|
|
3279
|
+
```python
|
|
3280
|
+
from crca_cg.corposwarm import CorporateSwarm
|
|
3281
|
+
|
|
3282
|
+
# Create swarm with CRCA-Q integration
|
|
3283
|
+
swarm = CorporateSwarm(
|
|
3284
|
+
company_name="Trading Corp",
|
|
3285
|
+
enable_crca_q=True,
|
|
3286
|
+
crca_q_config={
|
|
3287
|
+
'account_size': 1000000,
|
|
3288
|
+
'conservative_mode': True
|
|
3289
|
+
}
|
|
3290
|
+
)
|
|
3291
|
+
|
|
3292
|
+
# Investment Committee uses CRCA-Q
|
|
3293
|
+
allocation = swarm.investment_committee.recommend_allocation()
|
|
3294
|
+
print(f"Recommended allocation: {allocation}")
|
|
3295
|
+
```
|
|
3296
|
+
|
|
3297
|
+
### Example 7: Policy Benchmarking
|
|
3298
|
+
|
|
3299
|
+
Example from `examples/policy_bench.py`:
|
|
3300
|
+
|
|
3301
|
+
```python
|
|
3302
|
+
from schemas.policy import DoctrineV1, EpochConfig, MetricSpec, Objective, RiskBudget
|
|
3303
|
+
from templates.policy_loop import PolicyLoopMixin
|
|
3304
|
+
from utils.ledger import Ledger
|
|
3305
|
+
|
|
3306
|
+
# Create multiple policies for benchmarking
|
|
3307
|
+
policies = []
|
|
3308
|
+
|
|
3309
|
+
# Policy 1: Aggressive (high risk, high reward)
|
|
3310
|
+
policy1 = DoctrineV1(
|
|
3311
|
+
epoch=EpochConfig(unit="hours", length=1.0),
|
|
3312
|
+
metrics={"performance": MetricSpec(extractor_key="perf", unit="score")},
|
|
3313
|
+
objectives=[Objective(metric_name="performance", direction="maximize", weight=2.0)],
|
|
3314
|
+
invariants=[],
|
|
3315
|
+
levers={},
|
|
3316
|
+
risk_budget=RiskBudget(max_actions_per_epoch=10, max_risk_per_epoch=1.0)
|
|
3317
|
+
)
|
|
3318
|
+
|
|
3319
|
+
# Policy 2: Conservative (low risk, stable)
|
|
3320
|
+
policy2 = DoctrineV1(
|
|
3321
|
+
epoch=EpochConfig(unit="hours", length=1.0),
|
|
3322
|
+
metrics={"stability": MetricSpec(extractor_key="stab", unit="score")},
|
|
3323
|
+
objectives=[Objective(metric_name="stability", direction="maximize", weight=1.0)],
|
|
3324
|
+
invariants=[],
|
|
3325
|
+
levers={},
|
|
3326
|
+
risk_budget=RiskBudget(max_actions_per_epoch=3, max_risk_per_epoch=0.3)
|
|
3327
|
+
)
|
|
3328
|
+
|
|
3329
|
+
# Benchmark policies
|
|
3330
|
+
ledger = Ledger(db_path="benchmark.db")
|
|
3331
|
+
for i, policy in enumerate([policy1, policy2], 1):
|
|
3332
|
+
agent = PolicyLoopMixin(policy, ledger, seed=42)
|
|
3333
|
+
result = agent.run_epoch(epoch=1)
|
|
3334
|
+
print(f"Policy {i} result: {result['objectives_met']}")
|
|
3335
|
+
```
|
|
3336
|
+
|
|
3337
|
+
### Example 8: Real-Time Monitoring
|
|
3338
|
+
|
|
3339
|
+
Example from `examples/pridnestrovia_realtime.py`:
|
|
3340
|
+
|
|
3341
|
+
```python
|
|
3342
|
+
from crca_sd.crca_sd_realtime import (
|
|
3343
|
+
DataAcquisition, DataPipeline, RealTimeMonitor
|
|
3344
|
+
)
|
|
3345
|
+
|
|
3346
|
+
# Initialize data acquisition
|
|
3347
|
+
data_acq = DataAcquisition(
|
|
3348
|
+
sources=["api", "database", "stream"],
|
|
3349
|
+
update_interval=60 # Update every 60 seconds
|
|
3350
|
+
)
|
|
3351
|
+
|
|
3352
|
+
# Create data pipeline
|
|
3353
|
+
pipeline = DataPipeline(
|
|
3354
|
+
stages=["filter", "validate", "transform", "aggregate"]
|
|
3355
|
+
)
|
|
3356
|
+
|
|
3357
|
+
# Create real-time monitor
|
|
3358
|
+
monitor = RealTimeMonitor(
|
|
3359
|
+
data_acquisition=data_acq,
|
|
3360
|
+
pipeline=pipeline,
|
|
3361
|
+
alert_thresholds={"cpu": 80.0, "memory": 85.0}
|
|
3362
|
+
)
|
|
3363
|
+
|
|
3364
|
+
# Start monitoring
|
|
3365
|
+
monitor.start()
|
|
3366
|
+
|
|
3367
|
+
# Monitor for 1 hour
|
|
3368
|
+
import time
|
|
3369
|
+
time.sleep(3600)
|
|
3370
|
+
|
|
3371
|
+
# Stop monitoring
|
|
3372
|
+
monitor.stop()
|
|
3373
|
+
print(f"Collected {monitor.get_metric_count()} data points")
|
|
3374
|
+
```
|
|
3375
|
+
|
|
3376
|
+
### Example 9: Configuration Example
|
|
3377
|
+
|
|
3378
|
+
Example from `examples/config.yaml.example`:
|
|
3379
|
+
|
|
3380
|
+
```yaml
|
|
3381
|
+
# CRCA Configuration Example
|
|
3382
|
+
agent:
|
|
3383
|
+
model_name: "gpt-4o-mini"
|
|
3384
|
+
max_loops: 3
|
|
3385
|
+
agent_max_loops: 3
|
|
3386
|
+
enable_batch_predict: true
|
|
3387
|
+
max_batch_size: 32
|
|
3388
|
+
|
|
3389
|
+
policy_engine:
|
|
3390
|
+
epoch:
|
|
3391
|
+
unit: "hours"
|
|
3392
|
+
length: 1.0
|
|
3393
|
+
timezone: "UTC"
|
|
3394
|
+
ledger:
|
|
3395
|
+
db_path: "policy_ledger.db"
|
|
3396
|
+
rollback:
|
|
3397
|
+
db_path: "rollback.db"
|
|
3398
|
+
retention_days: 7
|
|
3399
|
+
|
|
3400
|
+
crca_sd:
|
|
3401
|
+
horizon: 12
|
|
3402
|
+
use_causal_scenarios: true
|
|
3403
|
+
|
|
3404
|
+
crca_cg:
|
|
3405
|
+
enable_crca_q: false
|
|
3406
|
+
enable_crca_sd: false
|
|
3407
|
+
```
|
|
3408
|
+
|
|
3409
|
+
**Usage:**
|
|
3410
|
+
|
|
3411
|
+
```python
|
|
3412
|
+
import yaml
|
|
3413
|
+
from schemas.policy import DoctrineV1
|
|
3414
|
+
|
|
3415
|
+
# Load configuration
|
|
3416
|
+
with open("config.yaml", "r") as f:
|
|
3417
|
+
config = yaml.safe_load(f)
|
|
3418
|
+
|
|
3419
|
+
# Use configuration
|
|
3420
|
+
doctrine = DoctrineV1(
|
|
3421
|
+
epoch=EpochConfig(**config["policy_engine"]["epoch"]),
|
|
3422
|
+
# ... other fields from config
|
|
3423
|
+
)
|
|
3424
|
+
```
|
|
3425
|
+
|
|
3426
|
+
---
|
|
3427
|
+
|
|
3428
|
+
## Advanced Features
|
|
3429
|
+
|
|
3430
|
+
### Optimization Methods
|
|
3431
|
+
|
|
3432
|
+
#### Gradient-Based Intervention Optimization
|
|
3433
|
+
|
|
3434
|
+
```python
|
|
3435
|
+
agent = CRCAAgent(variables=["price", "demand", "revenue"])
|
|
3436
|
+
# ... add causal relationships ...
|
|
3437
|
+
|
|
3438
|
+
opt_result = agent.gradient_based_intervention_optimization(
|
|
3439
|
+
initial_state={"price": 100.0, "demand": 1000.0, "revenue": 100000.0},
|
|
3440
|
+
target="revenue",
|
|
3441
|
+
intervention_vars=["price"],
|
|
3442
|
+
constraints={"price": (80.0, 120.0)},
|
|
3443
|
+
method="L-BFGS-B"
|
|
3444
|
+
)
|
|
3445
|
+
|
|
3446
|
+
print(f"Optimal intervention: {opt_result['optimal_intervention']}")
|
|
3447
|
+
print(f"Optimal target value: {opt_result['optimal_target_value']}")
|
|
3448
|
+
```
|
|
3449
|
+
|
|
3450
|
+
#### Bellman Optimal Intervention
|
|
3451
|
+
|
|
3452
|
+
Multi-step dynamic programming optimization:
|
|
3453
|
+
|
|
3454
|
+
```python
|
|
3455
|
+
bellman_result = agent.bellman_optimal_intervention(
|
|
3456
|
+
initial_state={"price": 100.0, "demand": 1000.0},
|
|
3457
|
+
target="revenue",
|
|
3458
|
+
intervention_vars=["price"],
|
|
3459
|
+
horizon=5,
|
|
3460
|
+
discount=0.9
|
|
3461
|
+
)
|
|
3462
|
+
|
|
3463
|
+
print(f"Optimal sequence: {bellman_result['optimal_sequence']}")
|
|
3464
|
+
print(f"Final state: {bellman_result['final_state']}")
|
|
3465
|
+
```
|
|
3466
|
+
|
|
3467
|
+
### Time-Series Analysis
|
|
3468
|
+
|
|
3469
|
+
#### Granger Causality Test
|
|
3470
|
+
|
|
3471
|
+
```python
|
|
3472
|
+
import pandas as pd
|
|
3473
|
+
|
|
3474
|
+
df = pd.read_csv("time_series_data.csv", parse_dates=["date"])
|
|
3475
|
+
|
|
3476
|
+
granger_result = agent.granger_causality_test(
|
|
3477
|
+
df=df,
|
|
3478
|
+
var1="price",
|
|
3479
|
+
var2="demand",
|
|
3480
|
+
max_lag=4
|
|
3481
|
+
)
|
|
3482
|
+
|
|
3483
|
+
print(f"Granger causes: {granger_result['granger_causes']}")
|
|
3484
|
+
print(f"P-value: {granger_result['p_value']:.4f}")
|
|
3485
|
+
```
|
|
3486
|
+
|
|
3487
|
+
#### Vector Autoregression Estimation
|
|
3488
|
+
|
|
3489
|
+
```python
|
|
3490
|
+
var_result = agent.vector_autoregression_estimation(
|
|
3491
|
+
df=df,
|
|
3492
|
+
variables=["price", "demand", "inventory"],
|
|
3493
|
+
max_lag=2
|
|
3494
|
+
)
|
|
3495
|
+
|
|
3496
|
+
print(f"VAR coefficients: {var_result['coefficient_matrices']}")
|
|
3497
|
+
```
|
|
3498
|
+
|
|
3499
|
+
### Bayesian Inference
|
|
3500
|
+
|
|
3501
|
+
```python
|
|
3502
|
+
bayesian_result = agent.bayesian_edge_inference(
|
|
3503
|
+
df=df,
|
|
3504
|
+
parent="price",
|
|
3505
|
+
child="demand",
|
|
3506
|
+
prior_mu=0.0,
|
|
3507
|
+
prior_sigma=1.0
|
|
3508
|
+
)
|
|
3509
|
+
|
|
3510
|
+
print(f"Posterior mean: {bayesian_result['posterior_mean']:.4f}")
|
|
3511
|
+
print(f"95% Credible interval: {bayesian_result['credible_interval_95']}")
|
|
3512
|
+
```
|
|
3513
|
+
|
|
3514
|
+
### Advanced Analysis Methods
|
|
3515
|
+
|
|
3516
|
+
#### Sensitivity Analysis
|
|
3517
|
+
|
|
3518
|
+
```python
|
|
3519
|
+
sensitivity_result = agent.sensitivity_analysis(
|
|
3520
|
+
intervention={"price": 100.0, "demand": 1000.0},
|
|
3521
|
+
target="revenue",
|
|
3522
|
+
perturbation_size=0.01
|
|
3523
|
+
)
|
|
3524
|
+
|
|
3525
|
+
print(f"Sensitivities: {sensitivity_result['sensitivities']}")
|
|
3526
|
+
print(f"Most influential: {sensitivity_result['most_influential_variable']}")
|
|
3527
|
+
```
|
|
3528
|
+
|
|
3529
|
+
#### Root Cause Analysis
|
|
3530
|
+
|
|
3531
|
+
```python
|
|
3532
|
+
rca_result = agent.deep_root_cause_analysis(
|
|
3533
|
+
problem_variable="revenue_decline",
|
|
3534
|
+
max_depth=20,
|
|
3535
|
+
min_path_strength=0.01
|
|
3536
|
+
)
|
|
3537
|
+
|
|
3538
|
+
print(f"Ultimate root causes: {rca_result['ultimate_root_causes']}")
|
|
3539
|
+
```
|
|
3540
|
+
|
|
3541
|
+
#### Shapley Value Attribution
|
|
3542
|
+
|
|
3543
|
+
```python
|
|
3544
|
+
shapley_result = agent.shapley_value_attribution(
|
|
3545
|
+
baseline_state={"price": 0.0, "demand": 0.0},
|
|
3546
|
+
target_state={"price": 100.0, "demand": 1000.0},
|
|
3547
|
+
target="revenue",
|
|
3548
|
+
samples=100
|
|
3549
|
+
)
|
|
3550
|
+
|
|
3551
|
+
print(f"Shapley values: {shapley_result['shapley_values']}")
|
|
3552
|
+
```
|
|
3553
|
+
|
|
3554
|
+
#### Multi-Layer What-If Analysis
|
|
3555
|
+
|
|
3556
|
+
```python
|
|
3557
|
+
scenarios = [
|
|
3558
|
+
{"price": 110.0},
|
|
3559
|
+
{"price": 120.0},
|
|
3560
|
+
{"demand": 1200.0}
|
|
3561
|
+
]
|
|
3562
|
+
|
|
3563
|
+
ml_result = agent.multi_layer_whatif_analysis(
|
|
3564
|
+
scenarios=scenarios,
|
|
3565
|
+
depth=3
|
|
3566
|
+
)
|
|
3567
|
+
|
|
3568
|
+
print(f"Multi-layer analysis: {ml_result['multi_layer_analysis']}")
|
|
3569
|
+
```
|
|
3570
|
+
|
|
3571
|
+
#### Reality Exploration
|
|
3572
|
+
|
|
3573
|
+
```python
|
|
3574
|
+
explore_result = agent.explore_alternate_realities(
|
|
3575
|
+
factual_state={"price": 100.0, "demand": 1000.0},
|
|
3576
|
+
target_outcome="revenue",
|
|
3577
|
+
target_value=150000.0,
|
|
3578
|
+
max_realities=50
|
|
3579
|
+
)
|
|
3580
|
+
|
|
3581
|
+
print(f"Best reality: {explore_result['best_reality']}")
|
|
3582
|
+
print(f"Top 10 realities: {explore_result['top_10_realities']}")
|
|
3583
|
+
```
|
|
3584
|
+
|
|
3585
|
+
### Async Operations
|
|
3586
|
+
|
|
3587
|
+
```python
|
|
3588
|
+
import asyncio
|
|
3589
|
+
|
|
3590
|
+
async def parallel_analysis():
|
|
3591
|
+
agent = CRCAAgent(variables=["x", "y", "z"])
|
|
3592
|
+
|
|
3593
|
+
# Run multiple analyses in parallel
|
|
3594
|
+
results = await asyncio.gather(
|
|
3595
|
+
agent.run_async(initial_state={"x": 1.0}, max_steps=2),
|
|
3596
|
+
agent.run_async(initial_state={"x": 2.0}, max_steps=2),
|
|
3597
|
+
agent.run_async(initial_state={"x": 3.0}, max_steps=2)
|
|
3598
|
+
)
|
|
3599
|
+
|
|
3600
|
+
return results
|
|
3601
|
+
|
|
3602
|
+
results = asyncio.run(parallel_analysis())
|
|
3603
|
+
```
|
|
3604
|
+
|
|
3605
|
+
---
|
|
3606
|
+
|
|
3607
|
+
## Testing
|
|
3608
|
+
|
|
3609
|
+
The CR-CA framework includes a comprehensive test suite located in the `tests/` directory.
|
|
3610
|
+
|
|
3611
|
+
### Test Files
|
|
3612
|
+
|
|
3613
|
+
- **test_core.py**: Core CRCAAgent functionality tests
|
|
3614
|
+
- Variable extraction tests
|
|
3615
|
+
- Causal graph construction tests
|
|
3616
|
+
- Prediction functionality tests
|
|
3617
|
+
- Counterfactual generation tests
|
|
3618
|
+
- Optimization method tests
|
|
3619
|
+
- Time-series analysis tests
|
|
3620
|
+
- Bayesian inference tests
|
|
3621
|
+
- Advanced analysis method tests
|
|
3622
|
+
- Cache behavior tests
|
|
3623
|
+
- Error handling tests
|
|
3624
|
+
|
|
3625
|
+
- **test_crca_sd.py**: CRCA-SD module tests
|
|
3626
|
+
- State vector tests
|
|
3627
|
+
- Control vector tests
|
|
3628
|
+
- Dynamics model tests
|
|
3629
|
+
- Constraint checker tests
|
|
3630
|
+
- Forward simulator tests
|
|
3631
|
+
- MPC solver tests
|
|
3632
|
+
- Scenario generator tests
|
|
3633
|
+
- Governance system tests
|
|
3634
|
+
- Real-time monitoring tests
|
|
3635
|
+
|
|
3636
|
+
- **sanity_check**: Sanity check script for basic functionality
|
|
3637
|
+
- Quick verification of core features
|
|
3638
|
+
- Import tests
|
|
3639
|
+
- Basic agent creation tests
|
|
3640
|
+
|
|
3641
|
+
- **sanity.yml**: GitHub Actions workflow for automated sanity checks
|
|
3642
|
+
- Runs on every push and pull request
|
|
3643
|
+
- Validates basic functionality
|
|
3644
|
+
- Checks import integrity
|
|
3645
|
+
|
|
3646
|
+
### Running Tests
|
|
3647
|
+
|
|
3648
|
+
**Example 1: Run All Tests**
|
|
3649
|
+
|
|
3650
|
+
```bash
|
|
3651
|
+
# Run all tests with verbose output
|
|
3652
|
+
pytest tests/ -v
|
|
3653
|
+
|
|
3654
|
+
# Run with coverage report
|
|
3655
|
+
pytest tests/ --cov=CRCA --cov-report=html
|
|
3656
|
+
|
|
3657
|
+
# Run with coverage and show missing lines
|
|
3658
|
+
pytest tests/ --cov=CRCA --cov-report=term-missing
|
|
3659
|
+
```
|
|
3660
|
+
|
|
3661
|
+
**Example 2: Run Specific Test File**
|
|
3662
|
+
|
|
3663
|
+
```bash
|
|
3664
|
+
# Run core tests only
|
|
3665
|
+
pytest tests/test_core.py -v
|
|
3666
|
+
|
|
3667
|
+
# Run CRCA-SD tests only
|
|
3668
|
+
pytest tests/test_crca_sd.py -v
|
|
3669
|
+
|
|
3670
|
+
# Run specific test class
|
|
3671
|
+
pytest tests/test_core.py::TestCRCAAgent -v
|
|
3672
|
+
|
|
3673
|
+
# Run specific test method
|
|
3674
|
+
pytest tests/test_core.py::TestCRCAAgent::test_variable_extraction -v
|
|
3675
|
+
```
|
|
3676
|
+
|
|
3677
|
+
**Example 3: Run with Markers**
|
|
3678
|
+
|
|
3679
|
+
```bash
|
|
3680
|
+
# Run only fast tests
|
|
3681
|
+
pytest tests/ -m "not slow" -v
|
|
3682
|
+
|
|
3683
|
+
# Run only integration tests
|
|
3684
|
+
pytest tests/ -m "integration" -v
|
|
3685
|
+
|
|
3686
|
+
# Run only unit tests
|
|
3687
|
+
pytest tests/ -m "unit" -v
|
|
3688
|
+
```
|
|
3689
|
+
|
|
3690
|
+
**Example 4: Run Sanity Check**
|
|
3691
|
+
|
|
3692
|
+
```bash
|
|
3693
|
+
# Run sanity check script
|
|
3694
|
+
python tests/sanity_check
|
|
3695
|
+
|
|
3696
|
+
# Or with pytest
|
|
3697
|
+
pytest tests/sanity_check -v
|
|
3698
|
+
```
|
|
3699
|
+
|
|
3700
|
+
**Example 5: Run with Debugging**
|
|
3701
|
+
|
|
3702
|
+
```bash
|
|
3703
|
+
# Run with print statements
|
|
3704
|
+
pytest tests/ -v -s
|
|
3705
|
+
|
|
3706
|
+
# Run with pdb on failure
|
|
3707
|
+
pytest tests/ --pdb
|
|
3708
|
+
|
|
3709
|
+
# Run with detailed traceback
|
|
3710
|
+
pytest tests/ -v --tb=long
|
|
3711
|
+
```
|
|
3712
|
+
|
|
3713
|
+
### Test Structure
|
|
3714
|
+
|
|
3715
|
+
Tests validate:
|
|
3716
|
+
- **Basic Functionality**: Core prediction, graph operations, variable extraction
|
|
3717
|
+
- **Cache Behavior**: Thread-safe caching, cache invalidation
|
|
3718
|
+
- **Graph Utilities**: DAG validation, topological sort, path finding
|
|
3719
|
+
- **Variable Extraction**: Automatic extraction from natural language
|
|
3720
|
+
- **Causal Analysis**: LLM-based analysis generation
|
|
3721
|
+
- **Integration**: Module integration (CRCA-SD, CRCA-CG, CRCA-Q)
|
|
3722
|
+
- **Error Handling**: Exception handling, edge cases
|
|
3723
|
+
- **Performance**: Batch prediction, async operations
|
|
3724
|
+
- **Optimization**: Gradient-based, Bellman optimization
|
|
3725
|
+
- **Time-Series**: Granger causality, VAR estimation
|
|
3726
|
+
- **Bayesian**: Posterior inference, credible intervals
|
|
3727
|
+
- **Advanced Analysis**: Sensitivity, root cause, Shapley values
|
|
3728
|
+
|
|
3729
|
+
### Writing Tests
|
|
3730
|
+
|
|
3731
|
+
**Example 6: Writing a New Test**
|
|
3732
|
+
|
|
3733
|
+
```python
|
|
3734
|
+
import pytest
|
|
3735
|
+
from CRCA import CRCAAgent
|
|
3736
|
+
|
|
3737
|
+
class TestNewFeature:
|
|
3738
|
+
"""Test suite for new feature."""
|
|
3739
|
+
|
|
3740
|
+
def test_basic_functionality(self):
|
|
3741
|
+
"""Test basic functionality of new feature."""
|
|
3742
|
+
agent = CRCAAgent(variables=["x", "y"])
|
|
3743
|
+
result = agent.run(initial_state={"x": 1.0, "y": 2.0})
|
|
3744
|
+
assert "evolved_state" in result
|
|
3745
|
+
|
|
3746
|
+
def test_edge_case(self):
|
|
3747
|
+
"""Test edge case handling."""
|
|
3748
|
+
agent = CRCAAgent(variables=["x"])
|
|
3749
|
+
# Test with empty state
|
|
3750
|
+
result = agent.run(initial_state={})
|
|
3751
|
+
assert result is not None
|
|
3752
|
+
|
|
3753
|
+
@pytest.mark.slow
|
|
3754
|
+
def test_performance(self):
|
|
3755
|
+
"""Test performance (marked as slow)."""
|
|
3756
|
+
agent = CRCAAgent(variables=["x", "y", "z"])
|
|
3757
|
+
# Performance test
|
|
3758
|
+
import time
|
|
3759
|
+
start = time.time()
|
|
3760
|
+
agent.run(initial_state={"x": 1.0, "y": 2.0, "z": 3.0})
|
|
3761
|
+
elapsed = time.time() - start
|
|
3762
|
+
assert elapsed < 1.0 # Should complete in < 1 second
|
|
3763
|
+
```
|
|
3764
|
+
|
|
3765
|
+
### Continuous Integration
|
|
3766
|
+
|
|
3767
|
+
Tests run automatically on:
|
|
3768
|
+
- Every push to main branch
|
|
3769
|
+
- Every pull request
|
|
3770
|
+
- Scheduled nightly builds
|
|
3771
|
+
|
|
3772
|
+
See `.github/workflows/` for CI/CD configuration.
|
|
3773
|
+
|
|
3774
|
+
---
|
|
3775
|
+
|
|
3776
|
+
## API Reference
|
|
3777
|
+
|
|
3778
|
+
### CRCAAgent Core Methods
|
|
3779
|
+
|
|
3780
|
+
#### Initialization
|
|
3781
|
+
|
|
3782
|
+
```python
|
|
3783
|
+
CRCAAgent(
|
|
3784
|
+
variables: Optional[List[str]] = None,
|
|
3785
|
+
causal_edges: Optional[List[Tuple[str, str]]] = None,
|
|
3786
|
+
max_loops: Optional[Union[int, str]] = 3,
|
|
3787
|
+
model_name: str = "gpt-4o",
|
|
3788
|
+
system_prompt: Optional[str] = None,
|
|
3789
|
+
enable_batch_predict: bool = False,
|
|
3790
|
+
max_batch_size: int = 32,
|
|
3791
|
+
bootstrap_workers: int = 0,
|
|
3792
|
+
use_async: bool = False,
|
|
3793
|
+
seed: Optional[int] = None,
|
|
3794
|
+
agent_max_loops: Optional[Union[int, str]] = None,
|
|
3795
|
+
**kwargs
|
|
3796
|
+
)
|
|
3797
|
+
```
|
|
3798
|
+
|
|
3799
|
+
#### Core Methods
|
|
3800
|
+
|
|
3801
|
+
- `run(task=None, initial_state=None, target_variables=None, max_steps=1, **kwargs)`: Main entry point for causal analysis
|
|
3802
|
+
- `add_causal_relationship(source, target, strength=1.0, relation_type=DIRECT, confidence=1.0)`: Add causal edge
|
|
3803
|
+
- `get_nodes()`: Get all nodes in causal graph
|
|
3804
|
+
- `get_edges()`: Get all edges in causal graph
|
|
3805
|
+
- `is_dag()`: Check if graph is acyclic
|
|
3806
|
+
- `generate_counterfactual_scenarios(factual_state, target_variables, max_scenarios=5)`: Generate counterfactuals
|
|
3807
|
+
|
|
3808
|
+
#### Advanced Methods
|
|
3809
|
+
|
|
3810
|
+
- `gradient_based_intervention_optimization(...)`: Gradient optimization
|
|
3811
|
+
- `bellman_optimal_intervention(...)`: Dynamic programming optimization
|
|
3812
|
+
- `granger_causality_test(...)`: Granger causality testing
|
|
3813
|
+
- `vector_autoregression_estimation(...)`: VAR estimation
|
|
3814
|
+
- `bayesian_edge_inference(...)`: Bayesian inference
|
|
3815
|
+
- `sensitivity_analysis(...)`: Sensitivity analysis
|
|
3816
|
+
- `deep_root_cause_analysis(...)`: Root cause analysis
|
|
3817
|
+
- `shapley_value_attribution(...)`: Shapley value calculation
|
|
3818
|
+
- `multi_layer_whatif_analysis(...)`: Multi-layer analysis
|
|
3819
|
+
- `explore_alternate_realities(...)`: Reality exploration
|
|
3820
|
+
|
|
3821
|
+
#### Async Methods
|
|
3822
|
+
|
|
3823
|
+
- `run_async(...)`: Async run wrapper
|
|
3824
|
+
- `quantify_uncertainty_async(...)`: Async uncertainty quantification
|
|
3825
|
+
- `granger_causality_test_async(...)`: Async Granger test
|
|
3826
|
+
- `vector_autoregression_estimation_async(...)`: Async VAR estimation
|
|
3827
|
+
|
|
3828
|
+
### Module APIs
|
|
3829
|
+
|
|
3830
|
+
See individual module documentation for detailed API references:
|
|
3831
|
+
- `utils/`: Formatter, TUI, router utilities
|
|
3832
|
+
- `tools/`: MCP client utilities
|
|
3833
|
+
- `templates/`: Agent creation framework
|
|
3834
|
+
- `crca_sd/`: Socioeconomic dynamics components
|
|
3835
|
+
- `crca_cg/`: Corporate governance components
|
|
3836
|
+
|
|
3837
|
+
---
|
|
3838
|
+
|
|
3839
|
+
## Architecture & Design
|
|
3840
|
+
|
|
3841
|
+
### System Architecture
|
|
3842
|
+
|
|
3843
|
+
The CR-CA framework follows a modular architecture with clear separation of concerns:
|
|
3844
|
+
|
|
3845
|
+
```
|
|
3846
|
+
┌─────────────────────────────────────────────────────────┐
|
|
3847
|
+
│ Application Layer │
|
|
3848
|
+
│ (CRCA-SD, CRCA-CG, CRCA-Q) │
|
|
3849
|
+
└────────────────────┬────────────────────────────────────┘
|
|
3850
|
+
│
|
|
3851
|
+
┌────────────────────▼────────────────────────────────────┐
|
|
3852
|
+
│ Core CRCAAgent │
|
|
3853
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
|
3854
|
+
│ │ LLM Analysis │ │ Deterministic│ │ Auto Extract │ │
|
|
3855
|
+
│ │ Engine │ │ Simulation │ │ Engine │ │
|
|
3856
|
+
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
|
3857
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
|
3858
|
+
│ │ Causal Graph │ │ Counterfact. │ │ Optimization │ │
|
|
3859
|
+
│ │ Management │ │ Generation │ │ Methods │ │
|
|
3860
|
+
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
|
3861
|
+
└────────────────────┬────────────────────────────────────┘
|
|
3862
|
+
│
|
|
3863
|
+
┌────────────────────▼────────────────────────────────────┐
|
|
3864
|
+
│ Supporting Infrastructure │
|
|
3865
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
|
3866
|
+
│ │ Utils │ │ Tools │ │ Templates │ │
|
|
3867
|
+
│ │ (Formatter, │ │ (MCP) │ │ (Agents) │ │
|
|
3868
|
+
│ │ TUI, etc.) │ │ │ │ │ │
|
|
3869
|
+
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
|
3870
|
+
└────────────────────┬────────────────────────────────────┘
|
|
3871
|
+
│
|
|
3872
|
+
┌────────────────────▼────────────────────────────────────┐
|
|
3873
|
+
│ External Dependencies │
|
|
3874
|
+
│ (swarms, litellm, rustworkx, numpy, pandas, scipy) │
|
|
3875
|
+
└─────────────────────────────────────────────────────────┘
|
|
3876
|
+
```
|
|
3877
|
+
|
|
3878
|
+
### Design Principles
|
|
3879
|
+
|
|
3880
|
+
1. **Modularity**: Clear module boundaries with well-defined interfaces
|
|
3881
|
+
2. **Extensibility**: Template system for creating specialized agents
|
|
3882
|
+
3. **Flexibility**: Dual-mode operation (LLM and deterministic)
|
|
3883
|
+
4. **Performance**: Batch prediction and async support for scalability
|
|
3884
|
+
5. **Research-Focused**: Grounded in causal inference theory (Pearl's framework)
|
|
3885
|
+
|
|
3886
|
+
### Data Flow
|
|
3887
|
+
|
|
3888
|
+
1. **Input**: Task string or state dictionary
|
|
3889
|
+
2. **Mode Detection**: Automatic selection of LLM or deterministic mode
|
|
3890
|
+
3. **Variable Extraction**: Automatic extraction if graph is empty
|
|
3891
|
+
4. **Analysis/Simulation**: LLM analysis or deterministic evolution
|
|
3892
|
+
5. **Counterfactual Generation**: Generate scenarios from results
|
|
3893
|
+
6. **Output**: Structured results with analysis and scenarios
|
|
3894
|
+
|
|
3895
|
+
---
|
|
3896
|
+
|
|
3897
|
+
## Research & Theory
|
|
3898
|
+
|
|
3899
|
+
### Theoretical Foundations
|
|
3900
|
+
|
|
3901
|
+
CR-CA is grounded in Judea Pearl's causal hierarchy and structural causal models (SCMs):
|
|
3902
|
+
|
|
3903
|
+
1. **Association**: Observing correlations (Level 1)
|
|
3904
|
+
2. **Intervention**: Understanding effects of actions (Level 2)
|
|
3905
|
+
3. **Counterfactuals**: Reasoning about what would have happened (Level 3)
|
|
3906
|
+
|
|
3907
|
+
### Structural Causal Models
|
|
3908
|
+
|
|
3909
|
+
SCMs represent variables as nodes in a directed acyclic graph (DAG), where edges represent causal relationships. The framework implements:
|
|
3910
|
+
|
|
3911
|
+
- **Linear SCMs**: Standardized z-space evolution with linear relationships
|
|
3912
|
+
- **Non-linear SCMs**: Tanh activation and interaction terms for non-linear dynamics
|
|
3913
|
+
- **Do-Calculus**: Pearl's do-operator for intervention reasoning
|
|
3914
|
+
- **Counterfactual Reasoning**: Abduction-Action-Prediction (AAP) workflow
|
|
3915
|
+
|
|
3916
|
+
### Causal Inference Methods
|
|
3917
|
+
|
|
3918
|
+
The framework implements various causal inference techniques:
|
|
3919
|
+
|
|
3920
|
+
- **Granger Causality**: Time-series causality testing
|
|
3921
|
+
- **Vector Autoregression**: Multi-variable time-series modeling
|
|
3922
|
+
- **Bayesian Inference**: Posterior estimation for edge strengths
|
|
3923
|
+
- **Sensitivity Analysis**: Perturbation-based sensitivity measures
|
|
3924
|
+
- **Shapley Values**: Fair attribution of variable contributions
|
|
3925
|
+
|
|
3926
|
+
### Research Applications
|
|
3927
|
+
|
|
3928
|
+
CR-CA can be used for:
|
|
3929
|
+
|
|
3930
|
+
- **Causal Discovery**: Learning causal structures from data
|
|
3931
|
+
- **Policy Analysis**: Evaluating policy interventions
|
|
3932
|
+
- **Root Cause Analysis**: Identifying ultimate causes of problems
|
|
3933
|
+
- **Scenario Planning**: Exploring alternative futures
|
|
3934
|
+
- **Decision Support**: Causal reasoning for decision-making
|
|
3935
|
+
|
|
3936
|
+
### Extending the Framework
|
|
3937
|
+
|
|
3938
|
+
Researchers can extend CR-CA by:
|
|
3939
|
+
|
|
3940
|
+
1. **Creating Specialized Agents**: Using the template system
|
|
3941
|
+
2. **Adding New Methods**: Implementing custom analysis methods
|
|
3942
|
+
3. **Custom Optimization**: Implementing domain-specific optimizers
|
|
3943
|
+
4. **Theoretical Extensions**: Adding new causal inference methods
|
|
3944
|
+
|
|
3945
|
+
---
|
|
3946
|
+
|
|
3947
|
+
## Prompts Directory
|
|
3948
|
+
|
|
3949
|
+
The `prompts/` directory contains system prompts for guiding LLM behavior in the CR-CA framework.
|
|
3950
|
+
|
|
3951
|
+
### Default CRCA Prompt (`prompts/default_crca.py`)
|
|
3952
|
+
|
|
3953
|
+
The default system prompt for CRCAAgent provides comprehensive guidance on:
|
|
3954
|
+
- Tool usage instructions
|
|
3955
|
+
- Variable extraction guidance
|
|
3956
|
+
- Counterfactual generation guidance
|
|
3957
|
+
- Best practices for causal reasoning
|
|
3958
|
+
|
|
3959
|
+
**Example 1: Using Default Prompt**
|
|
3960
|
+
|
|
3961
|
+
```python
|
|
3962
|
+
from CRCA import CRCAAgent
|
|
3963
|
+
|
|
3964
|
+
# Default prompt is automatically applied
|
|
3965
|
+
agent = CRCAAgent(model_name="gpt-4o-mini")
|
|
3966
|
+
|
|
3967
|
+
# The default prompt guides the LLM on:
|
|
3968
|
+
# - How to use extract_causal_variables tool
|
|
3969
|
+
# - How to use generate_causal_analysis tool
|
|
3970
|
+
# - When to extract variables vs. perform analysis
|
|
3971
|
+
# - Best practices for causal reasoning
|
|
3972
|
+
```
|
|
3973
|
+
|
|
3974
|
+
**Example 2: Custom Prompt Override**
|
|
3975
|
+
|
|
3976
|
+
```python
|
|
3977
|
+
from CRCA import CRCAAgent
|
|
3978
|
+
|
|
3979
|
+
# Custom prompt (appended to default)
|
|
3980
|
+
agent = CRCAAgent(
|
|
3981
|
+
model_name="gpt-4o-mini",
|
|
3982
|
+
system_prompt="You are an expert healthcare analyst. Focus on public health policy implications."
|
|
3983
|
+
)
|
|
3984
|
+
|
|
3985
|
+
# Default CRCA prompt is applied first, then custom prompt
|
|
3986
|
+
```
|
|
3987
|
+
|
|
3988
|
+
**Example 3: Viewing Default Prompt**
|
|
3989
|
+
|
|
3990
|
+
```python
|
|
3991
|
+
from prompts.default_crca import DEFAULT_CRCA_SYSTEM_PROMPT
|
|
3992
|
+
|
|
3993
|
+
# View the default prompt
|
|
3994
|
+
print(DEFAULT_CRCA_SYSTEM_PROMPT)
|
|
3995
|
+
|
|
3996
|
+
# Key sections:
|
|
3997
|
+
# - Tool usage instructions
|
|
3998
|
+
# - extract_causal_variables tool guide
|
|
3999
|
+
# - generate_causal_analysis tool guide
|
|
4000
|
+
# - Workflow guidance
|
|
4001
|
+
# - Best practices
|
|
4002
|
+
# - Common patterns
|
|
4003
|
+
```
|
|
4004
|
+
|
|
4005
|
+
**Example 4: Prompt Customization for Domain**
|
|
4006
|
+
|
|
4007
|
+
```python
|
|
4008
|
+
from CRCA import CRCAAgent
|
|
4009
|
+
|
|
4010
|
+
# Domain-specific prompt
|
|
4011
|
+
domain_prompt = """
|
|
4012
|
+
You are a financial analyst specializing in market causal relationships.
|
|
4013
|
+
When analyzing trading scenarios:
|
|
4014
|
+
- Focus on market microstructure effects
|
|
4015
|
+
- Consider regulatory impacts
|
|
4016
|
+
- Analyze liquidity relationships
|
|
4017
|
+
- Evaluate risk transmission mechanisms
|
|
4018
|
+
"""
|
|
4019
|
+
|
|
4020
|
+
agent = CRCAAgent(
|
|
4021
|
+
model_name="gpt-4o-mini",
|
|
4022
|
+
system_prompt=domain_prompt
|
|
4023
|
+
)
|
|
4024
|
+
|
|
4025
|
+
# Agent now has domain-specific guidance
|
|
4026
|
+
result = agent.run("Analyze how interest rate changes affect stock prices")
|
|
4027
|
+
```
|
|
4028
|
+
|
|
4029
|
+
## Configuration & Customization
|
|
4030
|
+
|
|
4031
|
+
### Agent Configuration
|
|
4032
|
+
|
|
4033
|
+
```python
|
|
4034
|
+
agent = CRCAAgent(
|
|
4035
|
+
# Model configuration
|
|
4036
|
+
model_name="gpt-4o",
|
|
4037
|
+
system_prompt="Custom system prompt",
|
|
4038
|
+
|
|
4039
|
+
# Loop configuration
|
|
4040
|
+
max_loops=3, # Causal reasoning loops
|
|
4041
|
+
agent_max_loops=3, # Standard agent loops
|
|
4042
|
+
|
|
4043
|
+
# Performance configuration
|
|
4044
|
+
enable_batch_predict=True,
|
|
4045
|
+
max_batch_size=32,
|
|
4046
|
+
bootstrap_workers=4, # Parallel bootstrap sampling
|
|
4047
|
+
|
|
4048
|
+
# Reproducibility
|
|
4049
|
+
seed=42,
|
|
4050
|
+
|
|
4051
|
+
# Feature flags
|
|
4052
|
+
use_crca_tools=True, # Enable automatic extraction tools
|
|
4053
|
+
use_async=False
|
|
4054
|
+
)
|
|
4055
|
+
```
|
|
4056
|
+
|
|
4057
|
+
### System Prompt Customization
|
|
4058
|
+
|
|
4059
|
+
The default CRCA system prompt is automatically applied. Custom prompts are appended:
|
|
4060
|
+
|
|
4061
|
+
```python
|
|
4062
|
+
agent = CRCAAgent(
|
|
4063
|
+
system_prompt="Your custom instructions here"
|
|
4064
|
+
# Default CRCA prompt is applied first, then custom prompt
|
|
4065
|
+
)
|
|
4066
|
+
```
|
|
4067
|
+
|
|
4068
|
+
### Feature Flags
|
|
4069
|
+
|
|
4070
|
+
- `use_crca_tools`: Enable/disable automatic variable extraction tools (default: True)
|
|
4071
|
+
- `enable_batch_predict`: Enable batch prediction mode (default: False)
|
|
4072
|
+
- `use_async`: Enable async operations (default: False)
|
|
4073
|
+
|
|
4074
|
+
### Custom Specialized Agents
|
|
4075
|
+
|
|
4076
|
+
Using the template system:
|
|
4077
|
+
|
|
4078
|
+
```python
|
|
4079
|
+
from templates.base_specialized_agent import BaseSpecializedAgent
|
|
4080
|
+
from templates.feature_mixins import GraphFeatureMixin, PredictionFeatureMixin
|
|
4081
|
+
|
|
4082
|
+
class MySpecializedAgent(BaseSpecializedAgent, GraphFeatureMixin, PredictionFeatureMixin):
|
|
4083
|
+
def __init__(self, **kwargs):
|
|
4084
|
+
super().__init__(**kwargs)
|
|
4085
|
+
self.init_graph_feature(variables=["A", "B", "C"])
|
|
4086
|
+
self.init_prediction_feature(use_nonlinear=True)
|
|
4087
|
+
|
|
4088
|
+
def _get_domain_schema(self):
|
|
4089
|
+
return {
|
|
4090
|
+
"type": "function",
|
|
4091
|
+
"function": {
|
|
4092
|
+
"name": "my_domain_tool",
|
|
4093
|
+
"description": "Domain-specific tool",
|
|
4094
|
+
"parameters": {...}
|
|
4095
|
+
}
|
|
4096
|
+
}
|
|
4097
|
+
```
|
|
4098
|
+
|
|
4099
|
+
---
|
|
4100
|
+
|
|
4101
|
+
## Troubleshooting
|
|
4102
|
+
|
|
4103
|
+
### Common Issues
|
|
4104
|
+
|
|
4105
|
+
#### Installation Problems
|
|
4106
|
+
|
|
4107
|
+
**Issue**: Import errors for rustworkx or other dependencies
|
|
4108
|
+
|
|
4109
|
+
**Solution**: Ensure all dependencies are installed:
|
|
4110
|
+
```bash
|
|
4111
|
+
pip install -r requirements.txt
|
|
4112
|
+
```
|
|
4113
|
+
|
|
4114
|
+
**Issue**: MCP-related import errors
|
|
4115
|
+
|
|
4116
|
+
**Solution**: MCP is an optional dependency. If not using MCP features, the framework will work without it.
|
|
4117
|
+
|
|
4118
|
+
#### LLM API Issues
|
|
4119
|
+
|
|
4120
|
+
**Issue**: API key not found
|
|
4121
|
+
|
|
4122
|
+
**Solution**: Set `OPENAI_API_KEY` environment variable or in `.env` file:
|
|
4123
|
+
```bash
|
|
4124
|
+
export OPENAI_API_KEY=your_key_here
|
|
4125
|
+
```
|
|
4126
|
+
|
|
4127
|
+
**Issue**: Rate limiting or timeout errors
|
|
4128
|
+
|
|
4129
|
+
**Solution**: Use a model with higher rate limits or implement retry logic. Consider using `gpt-4o-mini` for development.
|
|
4130
|
+
|
|
4131
|
+
#### Variable Extraction Failures
|
|
4132
|
+
|
|
4133
|
+
**Issue**: Automatic extraction returns empty graph
|
|
4134
|
+
|
|
4135
|
+
**Solution**:
|
|
4136
|
+
- Ensure the task clearly describes causal relationships
|
|
4137
|
+
- Check that `use_crca_tools=True` (default)
|
|
4138
|
+
- Verify LLM API key is set correctly
|
|
4139
|
+
- Try a more explicit task description
|
|
4140
|
+
|
|
4141
|
+
#### Performance Issues
|
|
4142
|
+
|
|
4143
|
+
**Issue**: Slow batch predictions
|
|
4144
|
+
|
|
4145
|
+
**Solution**:
|
|
4146
|
+
- Enable `enable_batch_predict=True`
|
|
4147
|
+
- Adjust `max_batch_size` based on available memory
|
|
4148
|
+
- Use `bootstrap_workers` for parallel operations
|
|
4149
|
+
|
|
4150
|
+
### Debugging
|
|
4151
|
+
|
|
4152
|
+
#### Enable Debug Logging
|
|
4153
|
+
|
|
4154
|
+
```python
|
|
4155
|
+
from loguru import logger
|
|
4156
|
+
|
|
4157
|
+
logger.add("debug.log", level="DEBUG")
|
|
4158
|
+
```
|
|
4159
|
+
|
|
4160
|
+
#### Check Graph State
|
|
4161
|
+
|
|
4162
|
+
```python
|
|
4163
|
+
print(f"Nodes: {agent.get_nodes()}")
|
|
4164
|
+
print(f"Edges: {agent.get_edges()}")
|
|
4165
|
+
print(f"Is DAG: {agent.is_dag()}")
|
|
4166
|
+
```
|
|
4167
|
+
|
|
4168
|
+
#### Verify Tool Execution
|
|
4169
|
+
|
|
4170
|
+
```python
|
|
4171
|
+
# Check if tools are registered
|
|
4172
|
+
print(f"Tools: {agent.tools}")
|
|
4173
|
+
print(f"Tool schemas: {agent.tools_list_dictionary}")
|
|
4174
|
+
```
|
|
4175
|
+
|
|
4176
|
+
---
|
|
4177
|
+
|
|
4178
|
+
## Contributing
|
|
4179
|
+
|
|
4180
|
+
### Development Setup
|
|
4181
|
+
|
|
4182
|
+
1. Clone the repository
|
|
4183
|
+
2. Install development dependencies:
|
|
4184
|
+
```bash
|
|
4185
|
+
pip install -r requirements.txt
|
|
4186
|
+
pip install pytest black mypy
|
|
4187
|
+
```
|
|
4188
|
+
|
|
4189
|
+
3. Create a feature branch
|
|
4190
|
+
4. Make changes following code style guidelines
|
|
4191
|
+
5. Add tests for new functionality
|
|
4192
|
+
6. Run tests: `pytest tests/ -v`
|
|
4193
|
+
7. Submit a pull request
|
|
4194
|
+
|
|
4195
|
+
### Code Style
|
|
4196
|
+
|
|
4197
|
+
- Follow PEP 8 style guidelines
|
|
4198
|
+
- Use type hints for all function signatures
|
|
4199
|
+
- Add docstrings to all classes and methods
|
|
4200
|
+
- Use `loguru` for logging (no `print` statements in production code)
|
|
4201
|
+
- Keep functions focused and modular
|
|
4202
|
+
|
|
4203
|
+
### Testing Requirements
|
|
4204
|
+
|
|
4205
|
+
- All new features must include tests
|
|
4206
|
+
- Maintain or improve test coverage
|
|
4207
|
+
- Tests should be fast and isolated
|
|
4208
|
+
- Use fixtures for common setup
|
|
4209
|
+
|
|
4210
|
+
### Documentation
|
|
4211
|
+
|
|
4212
|
+
- Update README.md for user-facing changes
|
|
4213
|
+
- Add docstrings for all public APIs
|
|
4214
|
+
- Include usage examples for new features
|
|
4215
|
+
- Update changelog for significant changes
|
|
4216
|
+
|
|
4217
|
+
---
|
|
4218
|
+
|
|
4219
|
+
## Changelog
|
|
4220
|
+
|
|
4221
|
+
### v1.4.0 (Current)
|
|
4222
|
+
|
|
4223
|
+
- **Comprehensive Documentation Overhaul**: Complete README rewrite with 100+ code examples
|
|
4224
|
+
- **Policy Engine System Documentation**: Full documentation with 21+ examples covering DoctrineV1, Ledger, Rollback, PolicyLoop, MPC, Drift Detection, Sensors, and Actuators
|
|
4225
|
+
- **Expanded Module Documentation**: Extensive examples for Utils (15+), Tools (15+), Templates (12+), Schemas, and Prompts
|
|
4226
|
+
- **Architecture Diagrams**: Added system architecture, policy engine flow, and data flow diagrams
|
|
4227
|
+
- **Integration Examples**: Added 5 comprehensive integration examples
|
|
4228
|
+
- **CI/CD Documentation**: Added GitHub Actions workflows documentation
|
|
4229
|
+
- **Project Structure**: Added comprehensive directory structure documentation
|
|
4230
|
+
- **Bolt.diy Documentation**: Added WIP section for web application
|
|
4231
|
+
|
|
4232
|
+
### v1.3.0
|
|
4233
|
+
|
|
4234
|
+
- **Automatic Variable Extraction**: Major feature allowing automatic extraction of variables and causal relationships from natural language tasks
|
|
4235
|
+
- **Enhanced LLM Integration**: Improved tool execution and ML-based extraction methods
|
|
4236
|
+
- **Module Expansions**: Comprehensive updates to all modules (utils, tools, templates, schemas)
|
|
4237
|
+
- **Integration Improvements**: Better integration patterns for CRCA-SD, CRCA-CG, and CRCA-Q
|
|
4238
|
+
- **Documentation**: Initial README improvements
|
|
4239
|
+
|
|
4240
|
+
### v1.2.0
|
|
4241
|
+
|
|
4242
|
+
- **Advanced Analysis Suite**:
|
|
4243
|
+
- Batch prediction with vectorized operations
|
|
4244
|
+
- Async/await support for concurrent operations
|
|
4245
|
+
- Optimization methods (gradient-based, Bellman dynamic programming)
|
|
4246
|
+
- Time-series analysis (Granger causality, VAR estimation)
|
|
4247
|
+
- Bayesian inference with credible intervals
|
|
4248
|
+
- Information-theoretic measures
|
|
4249
|
+
- Advanced analysis (sensitivity, root cause, Shapley values, multi-layer what-if, reality exploration)
|
|
4250
|
+
- **Performance Improvements**: Parallel bootstrap sampling, reproducible randomness
|
|
4251
|
+
- **Code Optimization**: Reduced file size while maintaining functionality
|
|
4252
|
+
|
|
4253
|
+
### v1.1.0
|
|
4254
|
+
|
|
4255
|
+
- **Production Hardening**:
|
|
4256
|
+
- Thread-safe prediction cache with lock protection
|
|
4257
|
+
- Requirements.txt pin list for CI
|
|
4258
|
+
- GitHub Actions sanity workflow
|
|
4259
|
+
- Pytest smoke tests
|
|
4260
|
+
- Cache lock documentation
|
|
4261
|
+
|
|
4262
|
+
### v1.0.0
|
|
4263
|
+
|
|
4264
|
+
- **Initial Release**:
|
|
4265
|
+
- Core CRCAAgent with LLM integration
|
|
4266
|
+
- Causal DAG management
|
|
4267
|
+
- Linear SCM evolution
|
|
4268
|
+
- Counterfactual generation
|
|
4269
|
+
- Basic deterministic simulation
|
|
4270
|
+
|
|
4271
|
+
---
|
|
4272
|
+
|
|
4273
|
+
## CI/CD Workflows
|
|
4274
|
+
|
|
4275
|
+
The CR-CA framework includes GitHub Actions workflows for automated testing and publishing.
|
|
4276
|
+
|
|
4277
|
+
### Automated Testing
|
|
4278
|
+
|
|
4279
|
+
**Workflow**: `.github/workflows/sanity.yml`
|
|
4280
|
+
|
|
4281
|
+
- Runs on every push and pull request
|
|
4282
|
+
- Validates basic functionality
|
|
4283
|
+
- Checks import integrity
|
|
4284
|
+
- Fast feedback loop for developers
|
|
4285
|
+
|
|
4286
|
+
**Example: Viewing Test Results**
|
|
4287
|
+
|
|
4288
|
+
```bash
|
|
4289
|
+
# Tests run automatically on GitHub
|
|
4290
|
+
# View results in the "Actions" tab of the repository
|
|
4291
|
+
```
|
|
4292
|
+
|
|
4293
|
+
### PyPI Publishing
|
|
4294
|
+
|
|
4295
|
+
**Workflow 1: Trusted Publishing** (`.github/workflows/publish.yml`)
|
|
4296
|
+
|
|
4297
|
+
Uses PyPI trusted publishing (OIDC) for secure, token-free publishing.
|
|
4298
|
+
|
|
4299
|
+
**Trigger:**
|
|
4300
|
+
- Automatic: On GitHub release publication
|
|
4301
|
+
- Manual: Via workflow_dispatch with version input
|
|
4302
|
+
|
|
4303
|
+
**Example: Manual Publishing**
|
|
4304
|
+
|
|
4305
|
+
1. Go to GitHub Actions → "Publish to PyPI"
|
|
4306
|
+
2. Click "Run workflow"
|
|
4307
|
+
3. Enter version (e.g., "1.4.0")
|
|
4308
|
+
4. Optionally check "Publish to TestPyPI"
|
|
4309
|
+
5. Click "Run workflow"
|
|
4310
|
+
|
|
4311
|
+
**Workflow 2: Manual with API Token** (`.github/workflows/publish-manual.yml`)
|
|
4312
|
+
|
|
4313
|
+
Uses PyPI API token stored in GitHub Secrets.
|
|
4314
|
+
|
|
4315
|
+
**Setup:**
|
|
4316
|
+
1. Create PyPI API token at https://pypi.org/manage/account/token/
|
|
4317
|
+
2. Add token to GitHub Secrets as `PYPI_API_TOKEN`
|
|
4318
|
+
3. For TestPyPI, add `TEST_PYPI_API_TOKEN`
|
|
4319
|
+
|
|
4320
|
+
**Example: Publishing Process**
|
|
4321
|
+
|
|
4322
|
+
```bash
|
|
4323
|
+
# Workflow automatically:
|
|
4324
|
+
# 1. Checks out code
|
|
4325
|
+
# 2. Sets up Python 3.11
|
|
4326
|
+
# 3. Installs build dependencies (build, twine)
|
|
4327
|
+
# 4. Verifies no .env files are present
|
|
4328
|
+
# 5. Builds package (python -m build)
|
|
4329
|
+
# 6. Publishes to PyPI or TestPyPI
|
|
4330
|
+
```
|
|
4331
|
+
|
|
4332
|
+
**Example: Local Publishing (for testing)**
|
|
4333
|
+
|
|
4334
|
+
```bash
|
|
4335
|
+
# Install build tools
|
|
4336
|
+
pip install build twine
|
|
4337
|
+
|
|
4338
|
+
# Build package
|
|
4339
|
+
python -m build
|
|
4340
|
+
|
|
4341
|
+
# Upload to TestPyPI (for testing)
|
|
4342
|
+
twine upload --repository testpypi dist/*
|
|
4343
|
+
|
|
4344
|
+
# Upload to PyPI (production)
|
|
4345
|
+
twine upload dist/*
|
|
4346
|
+
```
|
|
4347
|
+
|
|
4348
|
+
### Workflow Configuration
|
|
4349
|
+
|
|
4350
|
+
**Example: Custom Workflow**
|
|
4351
|
+
|
|
4352
|
+
```yaml
|
|
4353
|
+
name: Custom Workflow
|
|
4354
|
+
|
|
4355
|
+
on:
|
|
4356
|
+
push:
|
|
4357
|
+
branches: [main]
|
|
4358
|
+
pull_request:
|
|
4359
|
+
branches: [main]
|
|
4360
|
+
|
|
4361
|
+
jobs:
|
|
4362
|
+
test:
|
|
4363
|
+
runs-on: ubuntu-latest
|
|
4364
|
+
steps:
|
|
4365
|
+
- uses: actions/checkout@v4
|
|
4366
|
+
- uses: actions/setup-python@v5
|
|
4367
|
+
with:
|
|
4368
|
+
python-version: '3.11'
|
|
4369
|
+
- run: pip install -r requirements.txt
|
|
4370
|
+
- run: pytest tests/ -v
|
|
4371
|
+
```
|
|
4372
|
+
|
|
4373
|
+
## Project Structure
|
|
4374
|
+
|
|
4375
|
+
```
|
|
4376
|
+
CR-CA/
|
|
4377
|
+
├── CRCA.py # Core CRCAAgent implementation
|
|
4378
|
+
├── branches/
|
|
4379
|
+
│ ├── CRCA-Q.py # Quantitative trading agent
|
|
4380
|
+
│ └── crca_cg/ # Corporate governance module
|
|
4381
|
+
│ ├── corposwarm.py # CorporateSwarm implementation
|
|
4382
|
+
│ └── ...
|
|
4383
|
+
│ └── crca_sd/ # Socioeconomic dynamics module
|
|
4384
|
+
│ ├── crca_sd_core.py
|
|
4385
|
+
│ ├── crca_sd_mpc.py
|
|
4386
|
+
│ ├── crca_sd_governance.py
|
|
4387
|
+
│ └── crca_sd_realtime.py
|
|
4388
|
+
├── utils/ # Utility modules
|
|
4389
|
+
│ ├── agent.py # Extended agent functionality
|
|
4390
|
+
│ ├── formatter.py # Markdown formatting
|
|
4391
|
+
│ ├── tui.py # Terminal UI (CorporateSwarmTUI)
|
|
4392
|
+
│ ├── router.py # Swarm router
|
|
4393
|
+
│ ├── HHCS.py # Hybrid Hierarchical-Cluster Swarm
|
|
4394
|
+
│ ├── aop.py # Agent Orchestration Platform
|
|
4395
|
+
│ ├── ledger.py # Event-sourced ledger
|
|
4396
|
+
│ ├── rollback.py # Rollback manager
|
|
4397
|
+
│ ├── canonical.py # Deterministic hashing
|
|
4398
|
+
│ ├── conversation.py # Conversation management
|
|
4399
|
+
│ ├── Agent_types.py # Type definitions
|
|
4400
|
+
│ ├── AnyToStr.py # Type conversion
|
|
4401
|
+
│ └── out_types.py # Output type definitions
|
|
4402
|
+
├── tools/ # External tool integrations
|
|
4403
|
+
│ ├── mcpClientUtils.py # MCP client utilities
|
|
4404
|
+
│ ├── actuators.py # Intervention actuators
|
|
4405
|
+
│ ├── sensors.py # Metric sensors
|
|
4406
|
+
│ ├── code_generator.py # Code generation tools
|
|
4407
|
+
│ ├── mandate_generator.py # Proposal to mandate conversion
|
|
4408
|
+
│ ├── file_operations.py # File manipulation tools
|
|
4409
|
+
│ └── bolt.diy/ # Web application (WIP)
|
|
4410
|
+
├── templates/ # Agent creation framework
|
|
4411
|
+
│ ├── base_specialized_agent.py
|
|
4412
|
+
│ ├── feature_mixins.py
|
|
4413
|
+
│ ├── module_registry.py
|
|
4414
|
+
│ ├── graph_management.py
|
|
4415
|
+
│ ├── prediction_framework.py
|
|
4416
|
+
│ ├── statistical_methods.py
|
|
4417
|
+
│ ├── llm_integration.py
|
|
4418
|
+
│ ├── policy_loop.py # Policy loop mixin
|
|
4419
|
+
│ ├── mpc_planner.py # MPC planner
|
|
4420
|
+
│ ├── drift_detection.py # Drift detection
|
|
4421
|
+
│ └── examples/ # Example templates
|
|
4422
|
+
│ ├── causal_agent_template.py
|
|
4423
|
+
│ ├── drag_drop_example.py
|
|
4424
|
+
│ ├── logistics_agent_template.py
|
|
4425
|
+
│ └── trading_agent_template.py
|
|
4426
|
+
├── schemas/ # Type definitions
|
|
4427
|
+
│ ├── mcpSchemas.py # MCP schemas
|
|
4428
|
+
│ └── policy.py # Policy schemas
|
|
4429
|
+
├── prompts/ # System prompts
|
|
4430
|
+
│ └── default_crca.py # Default CRCA prompt
|
|
4431
|
+
├── examples/ # Usage examples
|
|
4432
|
+
│ ├── crca_sd_example.py
|
|
4433
|
+
│ ├── logistics_corporation.py
|
|
4434
|
+
│ ├── policy_bench.py
|
|
4435
|
+
│ ├── pridnestrovia_realtime.py
|
|
4436
|
+
│ ├── pridnestrovia-sd.py
|
|
4437
|
+
│ └── config.yaml.example
|
|
4438
|
+
├── tests/ # Test suite
|
|
4439
|
+
│ ├── test_core.py
|
|
4440
|
+
│ ├── test_crca_sd.py
|
|
4441
|
+
│ ├── sanity_check
|
|
4442
|
+
│ └── sanity.yml
|
|
4443
|
+
├── docs/ # Documentation
|
|
4444
|
+
│ └── CRCA-Q.md
|
|
4445
|
+
├── .github/
|
|
4446
|
+
│ └── workflows/ # CI/CD workflows
|
|
4447
|
+
│ ├── publish.yml
|
|
4448
|
+
│ └── publish-manual.yml
|
|
4449
|
+
├── requirements.txt # Python dependencies
|
|
4450
|
+
├── pyproject.toml # Project metadata
|
|
4451
|
+
├── LICENSE # Apache-2.0 license
|
|
4452
|
+
└── README.md # This file
|
|
4453
|
+
```
|
|
4454
|
+
|
|
4455
|
+
## Architecture Diagrams
|
|
4456
|
+
|
|
4457
|
+
### System Architecture
|
|
4458
|
+
|
|
4459
|
+
```
|
|
4460
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
4461
|
+
│ Application Layer │
|
|
4462
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
|
4463
|
+
│ │ CRCA-SD │ │ CRCA-CG │ │ CRCA-Q │ │
|
|
4464
|
+
│ │ (Socioecon) │ │ (Corporate) │ │ (Trading) │ │
|
|
4465
|
+
│ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │
|
|
4466
|
+
└─────────┼─────────────────┼─────────────────┼─────────────┘
|
|
4467
|
+
│ │ │
|
|
4468
|
+
┌─────────▼─────────────────▼─────────────────▼─────────────┐
|
|
4469
|
+
│ Core CRCAAgent │
|
|
4470
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
|
4471
|
+
│ │ LLM Analysis │ │ Deterministic│ │ Auto Extract │ │
|
|
4472
|
+
│ │ Engine │ │ Simulation │ │ Engine │ │
|
|
4473
|
+
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
|
4474
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
|
4475
|
+
│ │ Causal Graph │ │ Counterfact. │ │ Optimization │ │
|
|
4476
|
+
│ │ Management │ │ Generation │ │ Methods │ │
|
|
4477
|
+
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
|
4478
|
+
└─────────┬───────────────────────────────────────────────────┘
|
|
4479
|
+
│
|
|
4480
|
+
┌─────────▼───────────────────────────────────────────────────┐
|
|
4481
|
+
│ Policy Engine System │
|
|
4482
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
|
4483
|
+
│ │ Doctrine │ │ Ledger │ │ Rollback │ │
|
|
4484
|
+
│ │ (Policy) │ │ (Audit) │ │ (Recovery) │ │
|
|
4485
|
+
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
|
4486
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
|
4487
|
+
│ │ Policy Loop │ │ MPC Planner │ │ Drift │ │
|
|
4488
|
+
│ │ (Execution) │ │ (Optimization)│ │ Detection │ │
|
|
4489
|
+
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
|
4490
|
+
│ ┌──────────────┐ ┌──────────────┐ │
|
|
4491
|
+
│ │ Sensors │ │ Actuators │ │
|
|
4492
|
+
│ │ (Metrics) │ │(Interventions)│ │
|
|
4493
|
+
│ └──────────────┘ └──────────────┘ │
|
|
4494
|
+
└─────────┬───────────────────────────────────────────────────┘
|
|
4495
|
+
│
|
|
4496
|
+
┌─────────▼───────────────────────────────────────────────────┐
|
|
4497
|
+
│ Supporting Infrastructure │
|
|
4498
|
+
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
|
|
4499
|
+
│ │ Utils │ │ Tools │ │ Templates │ │
|
|
4500
|
+
│ │ (Router, │ │ (MCP, │ │ (Agents, │ │
|
|
4501
|
+
│ │ TUI, etc.) │ │ Actuators) │ │ Mixins) │ │
|
|
4502
|
+
│ └──────────────┘ └──────────────┘ └──────────────┘ │
|
|
4503
|
+
└─────────┬───────────────────────────────────────────────────┘
|
|
4504
|
+
│
|
|
4505
|
+
┌─────────▼───────────────────────────────────────────────────┐
|
|
4506
|
+
│ External Dependencies │
|
|
4507
|
+
│ (swarms, litellm, rustworkx, numpy, pandas, scipy, etc.) │
|
|
4508
|
+
└──────────────────────────────────────────────────────────────┘
|
|
4509
|
+
```
|
|
4510
|
+
|
|
4511
|
+
### Policy Engine Flow
|
|
4512
|
+
|
|
4513
|
+
```
|
|
4514
|
+
┌─────────────┐
|
|
4515
|
+
│ Doctrine │ (Policy Specification)
|
|
4516
|
+
└──────┬──────┘
|
|
4517
|
+
│
|
|
4518
|
+
▼
|
|
4519
|
+
┌─────────────┐
|
|
4520
|
+
│ Compiled │ (Normalized Policy)
|
|
4521
|
+
│ Policy │
|
|
4522
|
+
└──────┬──────┘
|
|
4523
|
+
│
|
|
4524
|
+
▼
|
|
4525
|
+
┌─────────────────────────────────────┐
|
|
4526
|
+
│ Policy Loop (Epoch) │
|
|
4527
|
+
│ ┌──────────┐ │
|
|
4528
|
+
│ │ Observe │ ← Sensors │
|
|
4529
|
+
│ └────┬─────┘ │
|
|
4530
|
+
│ │ │
|
|
4531
|
+
│ ▼ │
|
|
4532
|
+
│ ┌──────────┐ │
|
|
4533
|
+
│ │ Plan │ ← MPC Planner │
|
|
4534
|
+
│ └────┬─────┘ │
|
|
4535
|
+
│ │ │
|
|
4536
|
+
│ ▼ │
|
|
4537
|
+
│ ┌──────────┐ │
|
|
4538
|
+
│ │ Act │ → Actuators │
|
|
4539
|
+
│ └────┬─────┘ │
|
|
4540
|
+
│ │ │
|
|
4541
|
+
│ ▼ │
|
|
4542
|
+
│ ┌──────────┐ │
|
|
4543
|
+
│ │ Update │ → Online Learning │
|
|
4544
|
+
│ └────┬─────┘ │
|
|
4545
|
+
└───────┼─────────────────────────────┘
|
|
4546
|
+
│
|
|
4547
|
+
▼
|
|
4548
|
+
┌─────────────┐
|
|
4549
|
+
│ Ledger │ (Event Store)
|
|
4550
|
+
└─────────────┘
|
|
4551
|
+
```
|
|
4552
|
+
|
|
4553
|
+
### Data Flow
|
|
4554
|
+
|
|
4555
|
+
```
|
|
4556
|
+
Input (Task/State)
|
|
4557
|
+
│
|
|
4558
|
+
▼
|
|
4559
|
+
Mode Detection (LLM vs Deterministic)
|
|
4560
|
+
│
|
|
4561
|
+
├─→ LLM Mode ──→ Auto Extraction ──→ Causal Analysis ──→ Counterfactuals
|
|
4562
|
+
│
|
|
4563
|
+
└─→ Deterministic Mode ──→ Standardize ──→ SCM Evolution ──→ Counterfactuals
|
|
4564
|
+
│
|
|
4565
|
+
▼
|
|
4566
|
+
Output (Results)
|
|
4567
|
+
```
|
|
4568
|
+
|
|
4569
|
+
## Integration Examples
|
|
4570
|
+
|
|
4571
|
+
### Example 1: CRCA + CRCA-SD Integration
|
|
4572
|
+
|
|
4573
|
+
```python
|
|
4574
|
+
from CRCA import CRCAAgent
|
|
4575
|
+
from crca_sd import get_crca_agent, StateVector
|
|
4576
|
+
from crca_sd.crca_sd_mpc import MPCSolver
|
|
4577
|
+
|
|
4578
|
+
# Create CRCA agent
|
|
4579
|
+
crca = get_crca_agent(
|
|
4580
|
+
variables=['GDP', 'unemployment', 'inflation'],
|
|
4581
|
+
model_name="gpt-4o-mini"
|
|
4582
|
+
)
|
|
4583
|
+
|
|
4584
|
+
# Create state vector
|
|
4585
|
+
state = StateVector(GDP=1000.0, unemployment=5.0, inflation=2.0)
|
|
4586
|
+
|
|
4587
|
+
# Use CRCA for causal scenario generation in MPC
|
|
4588
|
+
solver = MPCSolver(crca_agent=crca)
|
|
4589
|
+
solution = solver.solve(initial_state=state, use_causal_scenarios=True)
|
|
4590
|
+
```
|
|
4591
|
+
|
|
4592
|
+
### Example 2: CRCA + CRCA-CG Integration
|
|
4593
|
+
|
|
4594
|
+
```python
|
|
4595
|
+
from CRCA import CRCAAgent
|
|
4596
|
+
from crca_cg.corposwarm import CorporateSwarm
|
|
4597
|
+
|
|
4598
|
+
# Create CRCA agent for policy analysis
|
|
4599
|
+
crca = CRCAAgent(
|
|
4600
|
+
variables=['revenue', 'costs', 'employee_satisfaction'],
|
|
4601
|
+
model_name="gpt-4o"
|
|
4602
|
+
)
|
|
4603
|
+
|
|
4604
|
+
# Create CorporateSwarm with CRCA
|
|
4605
|
+
swarm = CorporateSwarm(crca_agent=crca)
|
|
4606
|
+
|
|
4607
|
+
# Analyze policy impact using causal reasoning
|
|
4608
|
+
result = swarm.analyze_policy_impact(
|
|
4609
|
+
policy_description="Increase R&D spending by 20%",
|
|
4610
|
+
use_causal_analysis=True
|
|
4611
|
+
)
|
|
4612
|
+
```
|
|
4613
|
+
|
|
4614
|
+
### Example 3: CRCA-Q + CorporateSwarm Integration
|
|
4615
|
+
|
|
4616
|
+
```python
|
|
4617
|
+
from crca_cg.corposwarm import CorporateSwarm
|
|
4618
|
+
|
|
4619
|
+
# Create swarm with CRCA-Q integration
|
|
4620
|
+
swarm = CorporateSwarm(
|
|
4621
|
+
enable_crca_q=True,
|
|
4622
|
+
crca_q_config={
|
|
4623
|
+
'account_size': 1000000,
|
|
4624
|
+
'conservative_mode': True
|
|
4625
|
+
}
|
|
4626
|
+
)
|
|
4627
|
+
|
|
4628
|
+
# Investment Committee uses CRCA-Q for trading decisions
|
|
4629
|
+
investment_decision = swarm.investment_committee.recommend_allocation()
|
|
4630
|
+
```
|
|
4631
|
+
|
|
4632
|
+
### Example 4: Policy Engine + CorporateSwarm Integration
|
|
4633
|
+
|
|
4634
|
+
```python
|
|
4635
|
+
from schemas.policy import DoctrineV1
|
|
4636
|
+
from templates.policy_loop import PolicyLoopMixin
|
|
4637
|
+
from utils.ledger import Ledger
|
|
4638
|
+
from crca_cg.corposwarm import CorporateSwarm
|
|
4639
|
+
|
|
4640
|
+
# Create doctrine
|
|
4641
|
+
doctrine = DoctrineV1.from_json("corporate_policy.json")
|
|
4642
|
+
|
|
4643
|
+
# Create ledger
|
|
4644
|
+
ledger = Ledger(db_path="corporate_ledger.db")
|
|
4645
|
+
|
|
4646
|
+
# Create policy agent
|
|
4647
|
+
policy_agent = PolicyLoopMixin(doctrine, ledger, seed=42)
|
|
4648
|
+
|
|
4649
|
+
# Create corporate swarm
|
|
4650
|
+
swarm = CorporateSwarm()
|
|
4651
|
+
|
|
4652
|
+
# Integrate policy engine with corporate governance
|
|
4653
|
+
# Policy decisions can inform corporate proposals
|
|
4654
|
+
result = policy_agent.run_epoch(epoch=1)
|
|
4655
|
+
proposal = swarm.create_proposal(
|
|
4656
|
+
title="Policy-driven initiative",
|
|
4657
|
+
description=f"Based on policy analysis: {result['rationale']}"
|
|
4658
|
+
)
|
|
4659
|
+
```
|
|
4660
|
+
|
|
4661
|
+
### Example 5: Multiple Components Working Together
|
|
4662
|
+
|
|
4663
|
+
```python
|
|
4664
|
+
from CRCA import CRCAAgent
|
|
4665
|
+
from crca_sd import get_crca_agent, StateVector
|
|
4666
|
+
from crca_sd.crca_sd_mpc import MPCSolver
|
|
4667
|
+
from crca_cg.corposwarm import CorporateSwarm
|
|
4668
|
+
from schemas.policy import DoctrineV1
|
|
4669
|
+
from templates.policy_loop import PolicyLoopMixin
|
|
4670
|
+
from utils.ledger import Ledger
|
|
4671
|
+
|
|
4672
|
+
# 1. Create CRCA agent for causal reasoning
|
|
4673
|
+
crca = CRCAAgent(
|
|
4674
|
+
variables=['revenue', 'costs', 'market_share'],
|
|
4675
|
+
model_name="gpt-4o-mini"
|
|
4676
|
+
)
|
|
4677
|
+
|
|
4678
|
+
# 2. Create CRCA-SD solver with CRCA
|
|
4679
|
+
sd_crca = get_crca_agent(variables=['GDP', 'unemployment'], model_name="gpt-4o-mini")
|
|
4680
|
+
solver = MPCSolver(crca_agent=sd_crca)
|
|
4681
|
+
|
|
4682
|
+
# 3. Create CorporateSwarm with CRCA
|
|
4683
|
+
swarm = CorporateSwarm(
|
|
4684
|
+
crca_agent=crca,
|
|
4685
|
+
enable_crca_q=True,
|
|
4686
|
+
enable_crca_sd=True
|
|
4687
|
+
)
|
|
4688
|
+
|
|
4689
|
+
# 4. Create policy engine
|
|
4690
|
+
doctrine = DoctrineV1.from_json("policy.json")
|
|
4691
|
+
ledger = Ledger(db_path="ledger.db")
|
|
4692
|
+
policy_agent = PolicyLoopMixin(doctrine, ledger, seed=42)
|
|
4693
|
+
|
|
4694
|
+
# 5. Integrated workflow
|
|
4695
|
+
# - Policy engine makes decisions
|
|
4696
|
+
policy_result = policy_agent.run_epoch(epoch=1)
|
|
4697
|
+
|
|
4698
|
+
# - CorporateSwarm analyzes impact
|
|
4699
|
+
impact = swarm.analyze_policy_impact(
|
|
4700
|
+
policy_description=policy_result['rationale'],
|
|
4701
|
+
use_causal_analysis=True
|
|
4702
|
+
)
|
|
4703
|
+
|
|
4704
|
+
# - CRCA-SD optimizes scenarios
|
|
4705
|
+
state = StateVector(GDP=1000.0, unemployment=5.0)
|
|
4706
|
+
solution = solver.solve(initial_state=state, use_causal_scenarios=True)
|
|
4707
|
+
|
|
4708
|
+
print(f"Policy decision: {policy_result['interventions']}")
|
|
4709
|
+
print(f"Corporate impact: {impact}")
|
|
4710
|
+
print(f"Optimized solution: {solution}")
|
|
4711
|
+
```
|
|
4712
|
+
|
|
4713
|
+
## References & Resources
|
|
4714
|
+
|
|
4715
|
+
### Academic References
|
|
4716
|
+
|
|
4717
|
+
- Pearl, J. (2009). *Causality: Models, Reasoning, and Inference* (2nd ed.). Cambridge University Press.
|
|
4718
|
+
- Pearl, J., & Mackenzie, D. (2018). *The Book of Why: The New Science of Cause and Effect*. Basic Books.
|
|
4719
|
+
- Spirtes, P., Glymour, C., & Scheines, R. (2000). *Causation, Prediction, and Search* (2nd ed.). MIT Press.
|
|
4720
|
+
- Peters, J., Janzing, D., & Schölkopf, B. (2017). *Elements of Causal Inference: Foundations and Learning Algorithms*. MIT Press.
|
|
4721
|
+
|
|
4722
|
+
### Framework Documentation
|
|
4723
|
+
|
|
4724
|
+
- **Swarms Framework**: [https://github.com/kyegomez/swarms](https://github.com/kyegomez/swarms)
|
|
4725
|
+
- **LiteLLM**: [https://github.com/BerriAI/litellm](https://github.com/BerriAI/litellm)
|
|
4726
|
+
- **RustworkX**: [https://github.com/Qiskit/rustworkx](https://github.com/Qiskit/rustworkx)
|
|
4727
|
+
|
|
4728
|
+
### Additional Resources
|
|
4729
|
+
|
|
4730
|
+
- **CRCA-Q Documentation**: See `docs/CRCA-Q.md` for quantitative trading details
|
|
4731
|
+
- **Template Guide**: See `templates/TEMPLATE_GUIDE.md` for agent creation patterns
|
|
4732
|
+
|
|
4733
|
+
### Community
|
|
4734
|
+
|
|
4735
|
+
- **GitHub Repository**: [https://github.com/IlumCI/CR-CA](https://github.com/IlumCI/CR-CA)
|
|
4736
|
+
- **Issues**: Report bugs and request features via GitHub Issues
|
|
4737
|
+
- **Discussions**: Join discussions about causal reasoning and framework usage
|
|
4738
|
+
|
|
4739
|
+
---
|
|
4740
|
+
|
|
4741
|
+
## Bolt.diy Web Application (WIP)
|
|
4742
|
+
|
|
4743
|
+
**Status**: Work In Progress
|
|
4744
|
+
**Location**: `tools/bolt.diy/`
|
|
4745
|
+
|
|
4746
|
+
Bolt.diy is a full-stack web application and IDE built with Remix, React, TypeScript, and Electron. It provides an in-browser development environment with AI agent integration, code generation, and autonomous execution capabilities.
|
|
4747
|
+
|
|
4748
|
+
### Overview
|
|
4749
|
+
|
|
4750
|
+
Bolt.diy combines:
|
|
4751
|
+
- **WebContainer**: In-browser Node.js runtime for code execution
|
|
4752
|
+
- **AI Agent Integration**: Multiple LLM providers for code generation and assistance
|
|
4753
|
+
- **Code Editor**: CodeMirror-based editor with syntax highlighting
|
|
4754
|
+
- **Git Integration**: Version control within the browser
|
|
4755
|
+
- **Supabase Integration**: Backend services and database
|
|
4756
|
+
- **Deployment**: Netlify/Vercel deployment support
|
|
4757
|
+
- **Execution Services**: Execution Governor and Headless Executor
|
|
4758
|
+
- **MCP Integration**: Model Context Protocol for tool execution
|
|
4759
|
+
- **Electron Support**: Desktop application packaging
|
|
4760
|
+
- **Docker Deployment**: Containerized deployment option
|
|
4761
|
+
- **WebSocket Support**: Real-time communication
|
|
4762
|
+
- **Mandate System**: Execution mandates from corporate proposals
|
|
4763
|
+
- **Observability**: Dashboard for monitoring and metrics
|
|
4764
|
+
|
|
4765
|
+
### Architecture
|
|
4766
|
+
|
|
4767
|
+
**Frontend:**
|
|
4768
|
+
- **Framework**: Remix (React SSR)
|
|
4769
|
+
- **Language**: TypeScript
|
|
4770
|
+
- **Styling**: SCSS/CSS
|
|
4771
|
+
- **UI Components**: Custom components with modern design
|
|
4772
|
+
- **State Management**: React hooks and context
|
|
4773
|
+
|
|
4774
|
+
**Backend:**
|
|
4775
|
+
- **Runtime**: WebContainer API (in-browser Node.js)
|
|
4776
|
+
- **Services**: Execution Governor, Headless Executor
|
|
4777
|
+
- **Database**: Supabase (PostgreSQL)
|
|
4778
|
+
- **Deployment**: Cloudflare Pages, Netlify, Vercel
|
|
4779
|
+
|
|
4780
|
+
**Desktop:**
|
|
4781
|
+
- **Framework**: Electron
|
|
4782
|
+
- **Build**: electron-builder
|
|
4783
|
+
- **Platforms**: Windows, macOS, Linux
|
|
4784
|
+
|
|
4785
|
+
### Key Features
|
|
4786
|
+
|
|
4787
|
+
**1. WebContainer-Based Execution**
|
|
4788
|
+
|
|
4789
|
+
```typescript
|
|
4790
|
+
// Example: Running code in WebContainer
|
|
4791
|
+
import { WebContainer } from '@webcontainer/api';
|
|
4792
|
+
|
|
4793
|
+
const container = await WebContainer.boot();
|
|
4794
|
+
await container.mount(filesystem);
|
|
4795
|
+
|
|
4796
|
+
// Execute code
|
|
4797
|
+
const process = await container.spawn('npm', ['run', 'dev']);
|
|
4798
|
+
```
|
|
4799
|
+
|
|
4800
|
+
**2. AI Agent Integration**
|
|
4801
|
+
|
|
4802
|
+
```typescript
|
|
4803
|
+
// Example: AI agent for code generation
|
|
4804
|
+
import { Agent } from '@bolt/agent';
|
|
4805
|
+
|
|
4806
|
+
const agent = new Agent({
|
|
4807
|
+
provider: 'openai',
|
|
4808
|
+
model: 'gpt-4o-mini'
|
|
4809
|
+
});
|
|
4810
|
+
|
|
4811
|
+
const code = await agent.generateCode({
|
|
4812
|
+
specification: 'Create a React component for a todo list',
|
|
4813
|
+
language: 'typescript'
|
|
4814
|
+
});
|
|
4815
|
+
```
|
|
4816
|
+
|
|
4817
|
+
**3. Mandate-Based Execution**
|
|
4818
|
+
|
|
4819
|
+
```typescript
|
|
4820
|
+
// Example: Converting corporate proposal to execution mandate
|
|
4821
|
+
import { proposalToMandate } from '@bolt/mandate';
|
|
4822
|
+
|
|
4823
|
+
const mandate = proposalToMandate({
|
|
4824
|
+
proposal: corporateProposal,
|
|
4825
|
+
objectives: ['build_api', 'deploy_service'],
|
|
4826
|
+
budget: 50000,
|
|
4827
|
+
constraints: ['use_typescript', 'deploy_to_cloud']
|
|
4828
|
+
});
|
|
4829
|
+
|
|
4830
|
+
// Execute mandate
|
|
4831
|
+
await executeMandate(mandate);
|
|
4832
|
+
```
|
|
4833
|
+
|
|
4834
|
+
**4. MCP Integration**
|
|
4835
|
+
|
|
4836
|
+
```typescript
|
|
4837
|
+
// Example: MCP tool execution
|
|
4838
|
+
import { MCPClient } from '@bolt/mcp';
|
|
4839
|
+
|
|
4840
|
+
const client = new MCPClient({
|
|
4841
|
+
serverUrl: 'http://localhost:8000'
|
|
4842
|
+
});
|
|
4843
|
+
|
|
4844
|
+
const result = await client.executeTool('analyze_code', {
|
|
4845
|
+
code: sourceCode,
|
|
4846
|
+
language: 'typescript'
|
|
4847
|
+
});
|
|
4848
|
+
```
|
|
4849
|
+
|
|
4850
|
+
### Project Structure
|
|
4851
|
+
|
|
4852
|
+
```
|
|
4853
|
+
tools/bolt.diy/
|
|
4854
|
+
├── app/ # Remix application
|
|
4855
|
+
│ ├── components/ # React components
|
|
4856
|
+
│ ├── routes/ # Remix routes
|
|
4857
|
+
│ ├── lib/ # Shared libraries
|
|
4858
|
+
│ ├── utils/ # Utility functions
|
|
4859
|
+
│ └── types/ # TypeScript types
|
|
4860
|
+
├── electron/ # Electron desktop app
|
|
4861
|
+
│ ├── main/ # Main process
|
|
4862
|
+
│ └── preload/ # Preload scripts
|
|
4863
|
+
├── services/ # Backend services
|
|
4864
|
+
│ ├── execution-governor/ # Execution governance
|
|
4865
|
+
│ └── headless-executor/ # Headless execution
|
|
4866
|
+
├── public/ # Static assets
|
|
4867
|
+
├── scripts/ # Build scripts
|
|
4868
|
+
├── tests/ # Test files
|
|
4869
|
+
├── docker-compose.yaml # Docker Compose config
|
|
4870
|
+
├── Dockerfile # Docker build config
|
|
4871
|
+
├── electron-builder.yml # Electron build config
|
|
4872
|
+
└── package.json # Dependencies
|
|
4873
|
+
```
|
|
4874
|
+
|
|
4875
|
+
### Dependencies
|
|
4876
|
+
|
|
4877
|
+
**Core:**
|
|
4878
|
+
- `remix`: React SSR framework
|
|
4879
|
+
- `react`: UI library
|
|
4880
|
+
- `typescript`: Type safety
|
|
4881
|
+
- `@webcontainer/api`: In-browser Node.js runtime
|
|
4882
|
+
- `@modelcontextprotocol/sdk`: MCP SDK
|
|
4883
|
+
- `electron`: Desktop app framework
|
|
4884
|
+
|
|
4885
|
+
**Services:**
|
|
4886
|
+
- `@supabase/supabase-js`: Backend services
|
|
4887
|
+
- `@remix-run/node`: Server-side Remix
|
|
4888
|
+
- `@remix-run/react`: Client-side Remix
|
|
4889
|
+
|
|
4890
|
+
**Development:**
|
|
4891
|
+
- `vite`: Build tool
|
|
4892
|
+
- `eslint`: Linting
|
|
4893
|
+
- `playwright`: Testing
|
|
4894
|
+
- `typescript`: Type checking
|
|
4895
|
+
|
|
4896
|
+
### Setup (WIP)
|
|
4897
|
+
|
|
4898
|
+
**Note**: Bolt.diy is currently under active development. Setup instructions will be provided once stable.
|
|
4899
|
+
|
|
4900
|
+
**Planned Setup Steps:**
|
|
4901
|
+
|
|
4902
|
+
1. Install dependencies:
|
|
4903
|
+
```bash
|
|
4904
|
+
cd tools/bolt.diy
|
|
4905
|
+
pnpm install
|
|
4906
|
+
```
|
|
4907
|
+
|
|
4908
|
+
2. Configure environment variables:
|
|
4909
|
+
```bash
|
|
4910
|
+
cp .env.example .env
|
|
4911
|
+
# Edit .env with your API keys
|
|
4912
|
+
```
|
|
4913
|
+
|
|
4914
|
+
3. Start development server:
|
|
4915
|
+
```bash
|
|
4916
|
+
pnpm dev
|
|
4917
|
+
```
|
|
4918
|
+
|
|
4919
|
+
4. Build for production:
|
|
4920
|
+
```bash
|
|
4921
|
+
pnpm build
|
|
4922
|
+
```
|
|
4923
|
+
|
|
4924
|
+
5. Package Electron app:
|
|
4925
|
+
```bash
|
|
4926
|
+
pnpm electron:build
|
|
4927
|
+
```
|
|
4928
|
+
|
|
4929
|
+
### Current Limitations
|
|
4930
|
+
|
|
4931
|
+
- **Status**: Work In Progress
|
|
4932
|
+
- **Documentation**: Incomplete
|
|
4933
|
+
- **Testing**: Limited test coverage
|
|
4934
|
+
- **Deployment**: Not production-ready
|
|
4935
|
+
- **Features**: Some features may be incomplete or unstable
|
|
4936
|
+
|
|
4937
|
+
### Future Plans
|
|
4938
|
+
|
|
4939
|
+
- Complete WebContainer integration
|
|
4940
|
+
- Full MCP protocol support
|
|
4941
|
+
- Enhanced AI agent capabilities
|
|
4942
|
+
- Improved observability dashboard
|
|
4943
|
+
- Production deployment guides
|
|
4944
|
+
- Comprehensive documentation
|
|
4945
|
+
- Extended test coverage
|
|
4946
|
+
|
|
4947
|
+
### Contributing
|
|
4948
|
+
|
|
4949
|
+
Contributions to Bolt.diy are welcome, but please note:
|
|
4950
|
+
- The project is in active development
|
|
4951
|
+
- APIs may change without notice
|
|
4952
|
+
- Some features may be experimental
|
|
4953
|
+
- Documentation may be incomplete
|
|
4954
|
+
|
|
4955
|
+
For questions or contributions, please open an issue or pull request on GitHub.
|
|
4956
|
+
|
|
4957
|
+
---
|
|
4958
|
+
|
|
4959
|
+
**CR-CA Framework** — Enabling causal reasoning and counterfactual analysis for research and practical applications.
|