talkops-opscloud 0.3.0b1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (309) hide show
  1. opscloud/__init__.py +5 -0
  2. opscloud/__main__.py +6 -0
  3. opscloud/_constants.py +53 -0
  4. opscloud/_debug.py +145 -0
  5. opscloud/_debug_buffer.py +250 -0
  6. opscloud/_textual_patches.py +367 -0
  7. opscloud/_version.py +15 -0
  8. opscloud/agent/__init__.py +27 -0
  9. opscloud/agent/config.py +168 -0
  10. opscloud/agent/factory.py +1103 -0
  11. opscloud/approval_mode.py +44 -0
  12. opscloud/backend/__init__.py +49 -0
  13. opscloud/backend/composite.py +89 -0
  14. opscloud/backend/local.py +183 -0
  15. opscloud/backend/registry.py +79 -0
  16. opscloud/backend/sandbox_config.py +107 -0
  17. opscloud/backend/sandbox_factory.py +432 -0
  18. opscloud/built_in_skills/remember/SKILL.md +124 -0
  19. opscloud/built_in_skills/skill-creator/SKILL.md +348 -0
  20. opscloud/built_in_skills/skill-creator/scripts/init_skill.py +371 -0
  21. opscloud/built_in_skills/skill-creator/scripts/quick_validate.py +165 -0
  22. opscloud/cli/__init__.py +1 -0
  23. opscloud/cli/commands/__init__.py +15 -0
  24. opscloud/cli/commands/auth.py +45 -0
  25. opscloud/cli/commands/doctor.py +100 -0
  26. opscloud/cli/commands/mcp.py +137 -0
  27. opscloud/cli/commands/threads.py +51 -0
  28. opscloud/cli/interactive.py +154 -0
  29. opscloud/cli/main.py +903 -0
  30. opscloud/cli/non_interactive.py +425 -0
  31. opscloud/cli/server_manager.py +58 -0
  32. opscloud/commands/__init__.py +19 -0
  33. opscloud/commands/_base.py +110 -0
  34. opscloud/commands/_guards.py +52 -0
  35. opscloud/commands/_router.py +146 -0
  36. opscloud/commands/_types.py +36 -0
  37. opscloud/commands/core/__init__.py +47 -0
  38. opscloud/commands/core/auth.py +94 -0
  39. opscloud/commands/core/bug.py +46 -0
  40. opscloud/commands/core/clear.py +106 -0
  41. opscloud/commands/core/cloud.py +185 -0
  42. opscloud/commands/core/compact.py +128 -0
  43. opscloud/commands/core/config_cmd.py +120 -0
  44. opscloud/commands/core/context.py +220 -0
  45. opscloud/commands/core/cost.py +44 -0
  46. opscloud/commands/core/doctor.py +535 -0
  47. opscloud/commands/core/effort.py +108 -0
  48. opscloud/commands/core/exit_cmd.py +41 -0
  49. opscloud/commands/core/fast.py +148 -0
  50. opscloud/commands/core/help_cmd.py +94 -0
  51. opscloud/commands/core/mcp.py +134 -0
  52. opscloud/commands/core/model.py +109 -0
  53. opscloud/commands/core/permissions.py +196 -0
  54. opscloud/commands/core/plugins.py +92 -0
  55. opscloud/commands/core/pool.py +108 -0
  56. opscloud/commands/core/resume.py +97 -0
  57. opscloud/commands/core/skills.py +178 -0
  58. opscloud/commands/power/__init__.py +51 -0
  59. opscloud/commands/power/agents.py +98 -0
  60. opscloud/commands/power/btw.py +85 -0
  61. opscloud/commands/power/copy.py +97 -0
  62. opscloud/commands/power/goal.py +548 -0
  63. opscloud/commands/power/loop.py +295 -0
  64. opscloud/commands/power/memory.py +287 -0
  65. opscloud/commands/power/review.py +152 -0
  66. opscloud/commands/power/rubric.py +287 -0
  67. opscloud/commands/power/runtime.py +321 -0
  68. opscloud/commands/power/skill_creator.py +70 -0
  69. opscloud/commands/power/skill_invoke.py +165 -0
  70. opscloud/commands/power/tasks.py +209 -0
  71. opscloud/commands/power/trace.py +117 -0
  72. opscloud/commands/power/ui_toggles.py +135 -0
  73. opscloud/commands/power/version.py +52 -0
  74. opscloud/commands/registry.py +176 -0
  75. opscloud/config/__init__.py +114 -0
  76. opscloud/config/adapters/__init__.py +5 -0
  77. opscloud/config/adapters/postgres.py +776 -0
  78. opscloud/config/adapters/sqlite.py +769 -0
  79. opscloud/config/aws.py +248 -0
  80. opscloud/config/cloud_profiles.py +437 -0
  81. opscloud/config/env_vars.py +81 -0
  82. opscloud/config/langsmith.py +319 -0
  83. opscloud/config/manifest.py +817 -0
  84. opscloud/config/metadata.py +205 -0
  85. opscloud/config/paths.py +604 -0
  86. opscloud/config/plugins.py +130 -0
  87. opscloud/config/settings.py +834 -0
  88. opscloud/config/store.py +453 -0
  89. opscloud/config/store_factory.py +97 -0
  90. opscloud/config/toml_config.py +562 -0
  91. opscloud/editor.py +173 -0
  92. opscloud/exceptions.py +65 -0
  93. opscloud/file_ops.py +217 -0
  94. opscloud/hooks/__init__.py +139 -0
  95. opscloud/hooks/env.py +44 -0
  96. opscloud/hooks/interrupt.py +121 -0
  97. opscloud/hooks/models/__init__.py +109 -0
  98. opscloud/hooks/models/adapters.py +19 -0
  99. opscloud/hooks/models/config.py +79 -0
  100. opscloud/hooks/models/domain.py +434 -0
  101. opscloud/hooks/models/transport.py +37 -0
  102. opscloud/hooks/tools.py +231 -0
  103. opscloud/input.py +48 -0
  104. opscloud/integrations/__init__.py +80 -0
  105. opscloud/integrations/base.py +153 -0
  106. opscloud/integrations/content_filter.py +119 -0
  107. opscloud/integrations/event_bus.py +480 -0
  108. opscloud/integrations/hooks.py +238 -0
  109. opscloud/integrations/identity.py +181 -0
  110. opscloud/integrations/notifications.py +134 -0
  111. opscloud/integrations/stream_bridge.py +397 -0
  112. opscloud/integrations/thread_store.py +81 -0
  113. opscloud/json_types.py +20 -0
  114. opscloud/mcp/__init__.py +57 -0
  115. opscloud/mcp/config.py +203 -0
  116. opscloud/mcp/discovery.py +229 -0
  117. opscloud/mcp/mcp_info.py +109 -0
  118. opscloud/mcp/middleware.py +102 -0
  119. opscloud/mcp/preload.py +505 -0
  120. opscloud/mcp/raw_config.py +149 -0
  121. opscloud/mcp/semantic_profiler.py +305 -0
  122. opscloud/mcp/session_manager.py +727 -0
  123. opscloud/mcp/trust.py +105 -0
  124. opscloud/memory/__init__.py +27 -0
  125. opscloud/memory/branch.py +61 -0
  126. opscloud/memory/guard.py +19 -0
  127. opscloud/memory/onboarding.py +120 -0
  128. opscloud/memory/registry.py +134 -0
  129. opscloud/memory/store.py +223 -0
  130. opscloud/middleware/__init__.py +106 -0
  131. opscloud/middleware/_repository_bounds.py +459 -0
  132. opscloud/middleware/ask_user.py +152 -0
  133. opscloud/middleware/auto_mode.py +60 -0
  134. opscloud/middleware/auto_mode_hitl.py +2434 -0
  135. opscloud/middleware/compaction.py +566 -0
  136. opscloud/middleware/configurable_model.py +233 -0
  137. opscloud/middleware/cost_tracking.py +1021 -0
  138. opscloud/middleware/glm_stall_recovery.py +109 -0
  139. opscloud/middleware/goal_criteria.py +1234 -0
  140. opscloud/middleware/goal_state_notice.py +712 -0
  141. opscloud/middleware/goal_tools.py +407 -0
  142. opscloud/middleware/headless_mcp_guard.py +88 -0
  143. opscloud/middleware/jev_model_router.py +1018 -0
  144. opscloud/middleware/local_context.py +460 -0
  145. opscloud/middleware/mcp_context.py +151 -0
  146. opscloud/middleware/mcp_middleware.py +146 -0
  147. opscloud/middleware/memory_guard.py +393 -0
  148. opscloud/middleware/model_retry.py +205 -0
  149. opscloud/middleware/registry.py +137 -0
  150. opscloud/middleware/reliable_rubric.py +709 -0
  151. opscloud/middleware/resume_state.py +91 -0
  152. opscloud/middleware/server_hooks.py +1189 -0
  153. opscloud/middleware/shell_allow_list.py +90 -0
  154. opscloud/middleware/skills.py +382 -0
  155. opscloud/middleware/subagent_artifacts.py +84 -0
  156. opscloud/middleware/subagent_telemetry.py +261 -0
  157. opscloud/middleware/subagents.py +243 -0
  158. opscloud/middleware/tool_filter.py +384 -0
  159. opscloud/middleware/unified_system_message.py +62 -0
  160. opscloud/model/__init__.py +37 -0
  161. opscloud/model/config.py +2992 -0
  162. opscloud/model/factory.py +477 -0
  163. opscloud/model/pool.py +459 -0
  164. opscloud/model/reasoning.py +282 -0
  165. opscloud/offload.py +173 -0
  166. opscloud/output.py +64 -0
  167. opscloud/plugins/__init__.py +189 -0
  168. opscloud/plugins/_json.py +22 -0
  169. opscloud/plugins/adapters/__init__.py +13 -0
  170. opscloud/plugins/adapters/agents.py +112 -0
  171. opscloud/plugins/adapters/commands.py +190 -0
  172. opscloud/plugins/adapters/mcp.py +363 -0
  173. opscloud/plugins/adapters/skills.py +132 -0
  174. opscloud/plugins/commands_cli.py +541 -0
  175. opscloud/plugins/discovery.py +512 -0
  176. opscloud/plugins/manifest.py +335 -0
  177. opscloud/plugins/marketplace.py +893 -0
  178. opscloud/plugins/models.py +334 -0
  179. opscloud/plugins/project_plugins.py +317 -0
  180. opscloud/plugins/protocol.py +31 -0
  181. opscloud/plugins/store.py +905 -0
  182. opscloud/plugins/substitution.py +122 -0
  183. opscloud/project_utils.py +145 -0
  184. opscloud/prompts/__init__.py +37 -0
  185. opscloud/prompts/system.py +483 -0
  186. opscloud/prompts/templates/system_prompt.md +166 -0
  187. opscloud/prompts/types.py +54 -0
  188. opscloud/rubrics/__init__.py +42 -0
  189. opscloud/rubrics/evaluator.py +404 -0
  190. opscloud/rubrics/evidence_extractor.py +229 -0
  191. opscloud/rubrics/generator.py +239 -0
  192. opscloud/rubrics/jev_compiler.py +175 -0
  193. opscloud/rubrics/jev_grader.py +297 -0
  194. opscloud/rubrics/middleware.py +8 -0
  195. opscloud/schema/__init__.py +107 -0
  196. opscloud/schema/interrupts.py +981 -0
  197. opscloud/security/__init__.py +91 -0
  198. opscloud/security/approval_mode.py +528 -0
  199. opscloud/security/approval_mode_source.py +230 -0
  200. opscloud/security/cli_ast_evaluator.py +552 -0
  201. opscloud/security/jev_classifier.py +651 -0
  202. opscloud/security/shell_safety.py +340 -0
  203. opscloud/security/unicode_security.py +439 -0
  204. opscloud/security/url_validation.py +160 -0
  205. opscloud/server/__init__.py +13 -0
  206. opscloud/server/_server_config.py +158 -0
  207. opscloud/server/server.py +242 -0
  208. opscloud/server/server_graph.py +93 -0
  209. opscloud/skills/__init__.py +91 -0
  210. opscloud/skills/commands.py +1099 -0
  211. opscloud/skills/invocation.py +200 -0
  212. opscloud/skills/load.py +388 -0
  213. opscloud/skills/loader.py +23 -0
  214. opscloud/skills/merge.py +65 -0
  215. opscloud/skills/registry.py +263 -0
  216. opscloud/skills/sources.py +174 -0
  217. opscloud/skills/trust.py +417 -0
  218. opscloud/state/__init__.py +142 -0
  219. opscloud/state/base.py +154 -0
  220. opscloud/state/cloud_context.py +262 -0
  221. opscloud/state/goal_channels.py +124 -0
  222. opscloud/state/goal_state_limits.py +175 -0
  223. opscloud/state/resume_state.py +58 -0
  224. opscloud/state/service_context.py +281 -0
  225. opscloud/state/session.py +957 -0
  226. opscloud/state/state_migration.py +86 -0
  227. opscloud/subagents/__init__.py +30 -0
  228. opscloud/subagents/loader.py +331 -0
  229. opscloud/subagents/subagents_parser.py +117 -0
  230. opscloud/subagents/types.py +25 -0
  231. opscloud/tools/__init__.py +36 -0
  232. opscloud/tools/catalog.py +37 -0
  233. opscloud/tools/display.py +24 -0
  234. opscloud/tools/fetch_url.py +176 -0
  235. opscloud/tools/goal_tools.py +423 -0
  236. opscloud/tools/registry.py +105 -0
  237. opscloud/tools/thread.py +32 -0
  238. opscloud/tools/web_search.py +215 -0
  239. opscloud/ui/__init__.py +119 -0
  240. opscloud/ui/app.py +5400 -0
  241. opscloud/ui/app.tcss +479 -0
  242. opscloud/ui/clipboard.py +147 -0
  243. opscloud/ui/command_registry.py +482 -0
  244. opscloud/ui/message_store.py +152 -0
  245. opscloud/ui/permission_store.py +360 -0
  246. opscloud/ui/preferences.py +113 -0
  247. opscloud/ui/prompt_manager.py +96 -0
  248. opscloud/ui/remote_client.py +505 -0
  249. opscloud/ui/textual_adapter.py +1269 -0
  250. opscloud/ui/theme.py +728 -0
  251. opscloud/ui/ui_help.py +415 -0
  252. opscloud/ui/widgets/__init__.py +383 -0
  253. opscloud/ui/widgets/_ask_user_types.py +81 -0
  254. opscloud/ui/widgets/_copy_spans.py +52 -0
  255. opscloud/ui/widgets/_inline_prompt.py +290 -0
  256. opscloud/ui/widgets/_js_eval_display.py +139 -0
  257. opscloud/ui/widgets/_links.py +175 -0
  258. opscloud/ui/widgets/_tool_stream.py +308 -0
  259. opscloud/ui/widgets/agent_selector.py +297 -0
  260. opscloud/ui/widgets/approval.py +741 -0
  261. opscloud/ui/widgets/ask_user.py +539 -0
  262. opscloud/ui/widgets/auth.py +695 -0
  263. opscloud/ui/widgets/auth_manager.py +214 -0
  264. opscloud/ui/widgets/auto_mode_notice.py +146 -0
  265. opscloud/ui/widgets/autocomplete.py +709 -0
  266. opscloud/ui/widgets/chat_input.py +805 -0
  267. opscloud/ui/widgets/cloud_selector.py +528 -0
  268. opscloud/ui/widgets/config_manager.py +652 -0
  269. opscloud/ui/widgets/debug_console.py +1269 -0
  270. opscloud/ui/widgets/devops_renderers.py +184 -0
  271. opscloud/ui/widgets/diff.py +237 -0
  272. opscloud/ui/widgets/effort_selector.py +166 -0
  273. opscloud/ui/widgets/goal_review.py +612 -0
  274. opscloud/ui/widgets/goal_status.py +57 -0
  275. opscloud/ui/widgets/infra_panel.py +141 -0
  276. opscloud/ui/widgets/install_confirm.py +114 -0
  277. opscloud/ui/widgets/loading.py +170 -0
  278. opscloud/ui/widgets/mcp_viewer.py +1204 -0
  279. opscloud/ui/widgets/messages.py +4461 -0
  280. opscloud/ui/widgets/model_selector.py +863 -0
  281. opscloud/ui/widgets/notification_center.py +108 -0
  282. opscloud/ui/widgets/notification_settings.py +119 -0
  283. opscloud/ui/widgets/operation_card.py +106 -0
  284. opscloud/ui/widgets/permissions_manager.py +667 -0
  285. opscloud/ui/widgets/plugin_manager.py +1212 -0
  286. opscloud/ui/widgets/pool_selector.py +376 -0
  287. opscloud/ui/widgets/preamble.py +53 -0
  288. opscloud/ui/widgets/skills_viewer.py +432 -0
  289. opscloud/ui/widgets/status.py +1037 -0
  290. opscloud/ui/widgets/subagent_panel.py +927 -0
  291. opscloud/ui/widgets/theme_selector.py +232 -0
  292. opscloud/ui/widgets/thread_selector.py +358 -0
  293. opscloud/ui/widgets/toast.py +31 -0
  294. opscloud/ui/widgets/tool_display.py +325 -0
  295. opscloud/ui/widgets/tool_renderers.py +234 -0
  296. opscloud/ui/widgets/tool_widgets.py +278 -0
  297. opscloud/ui/widgets/welcome.py +421 -0
  298. opscloud/ui/widgets/welcome_popup.py +102 -0
  299. opscloud/utils/__init__.py +24 -0
  300. opscloud/utils/cost_estimation.py +169 -0
  301. opscloud/utils/git.py +390 -0
  302. opscloud/utils/logger.py +571 -0
  303. opscloud/utils/session_stats.py +115 -0
  304. opscloud/utils/startup_error.py +37 -0
  305. talkops_opscloud-0.3.0b1.dist-info/METADATA +449 -0
  306. talkops_opscloud-0.3.0b1.dist-info/RECORD +309 -0
  307. talkops_opscloud-0.3.0b1.dist-info/WHEEL +4 -0
  308. talkops_opscloud-0.3.0b1.dist-info/entry_points.txt +2 -0
  309. talkops_opscloud-0.3.0b1.dist-info/licenses/LICENSE +175 -0
@@ -0,0 +1,146 @@
1
+ """Agent middleware for MCP tools execution, argument normalization, and auth resilience.
2
+
3
+ Normalizes model-generated arguments before they hit MCP tools (e.g., dropping empty
4
+ strings for optional params) and provides clean error surfacing for expired tokens or
5
+ unauthenticated MCP sessions.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from collections.abc import Awaitable, Callable
11
+ import logging
12
+ from typing import TYPE_CHECKING, Any
13
+
14
+ from langchain.agents.middleware.types import AgentMiddleware
15
+ from langchain_core.messages import ToolMessage
16
+ from langgraph.prebuilt.tool_node import ToolCallRequest
17
+
18
+ from opscloud.middleware.registry import register_middleware
19
+ from opscloud.utils.logger import get_logger
20
+
21
+ logger = get_logger(__name__)
22
+
23
+ _MCP_AUTH_ERROR_SIGNALS = (
24
+ "unauthorized",
25
+ "not authorized",
26
+ "unauthenticated",
27
+ "authentication failed",
28
+ "token expired",
29
+ "token is expired",
30
+ "is expired",
31
+ "session expired",
32
+ "invalid credentials",
33
+ "access denied",
34
+ "credentials have expired",
35
+ "expiredtoken",
36
+ )
37
+
38
+
39
+ def normalize_mcp_arguments(
40
+ arguments: dict[str, Any],
41
+ input_schema: Any = None,
42
+ ) -> dict[str, Any]:
43
+ """Drop empty-string values for optional parameters to avoid MCP server schema rejections.
44
+
45
+ Many MCP servers (AWS MCP, Slack, GitHub, K8s) validate optional string/ID parameters
46
+ strictly and reject empty strings `""` when the parameter should have been omitted.
47
+ """
48
+ if not isinstance(arguments, dict):
49
+ return arguments
50
+
51
+ required_set: set[str] = set()
52
+ properties: dict[str, Any] = {}
53
+ if isinstance(input_schema, dict):
54
+ required_set = set(input_schema.get("required") or ())
55
+ properties = input_schema.get("properties") or {}
56
+
57
+ cleaned: dict[str, Any] = {}
58
+ for key, value in arguments.items():
59
+ if value != "" or key in required_set:
60
+ cleaned[key] = value
61
+ continue
62
+ # Drop empty strings for non-required fields
63
+ logger.debug("Normalized MCP tool arg: dropped empty-string for key %r", key)
64
+
65
+ return cleaned
66
+
67
+
68
+ @register_middleware(name="mcp_tool")
69
+ class MCPToolMiddleware(AgentMiddleware):
70
+ """Wraps MCP tool calls to normalize arguments and handle authentication failures."""
71
+
72
+ def _is_mcp_call(self, request: ToolCallRequest) -> bool:
73
+ tool_name = (
74
+ request.tool_call.get("name", "")
75
+ if getattr(request, "tool_call", None) and isinstance(request.tool_call, dict)
76
+ else ""
77
+ )
78
+ return ":" in tool_name or tool_name.startswith("mcp__")
79
+
80
+ def _preprocess_request(self, request: ToolCallRequest) -> ToolCallRequest:
81
+ if not self._is_mcp_call(request):
82
+ return request
83
+
84
+ args = request.tool_call.get("args") or {}
85
+ if isinstance(args, dict):
86
+ input_schema = getattr(request.tool, "args_schema", None)
87
+ schema_dict = input_schema.schema() if input_schema is not None and hasattr(input_schema, "schema") else None
88
+ cleaned = normalize_mcp_arguments(args, schema_dict)
89
+ request.tool_call["args"] = cleaned
90
+
91
+ return request
92
+
93
+ def _handle_error(self, request: ToolCallRequest, exc: Exception) -> ToolMessage | None:
94
+ err_str = str(exc).lower()
95
+ is_auth_error = any(sig in err_str for sig in _MCP_AUTH_ERROR_SIGNALS) or (
96
+ "expired" in err_str and any(k in err_str for k in ("token", "credential", "auth", "session"))
97
+ )
98
+ if is_auth_error:
99
+ tool_name = request.tool_call.get("name", "mcp_tool")
100
+ tool_id = request.tool_call.get("id", "")
101
+ logger.warning("MCP tool %r encountered authentication error: %s", tool_name, exc)
102
+ return ToolMessage(
103
+ content=(
104
+ f"MCP tool `{tool_name}` failed due to authentication: {exc}. "
105
+ "The cloud credentials or session token for this MCP integration may have expired. "
106
+ "Please verify your AWS / cloud authentication or refresh your credentials."
107
+ ),
108
+ name=tool_name,
109
+ tool_call_id=tool_id,
110
+ status="error",
111
+ )
112
+ return None
113
+
114
+ def wrap_tool_call(
115
+ self,
116
+ request: ToolCallRequest,
117
+ handler: Callable[[ToolCallRequest], Any],
118
+ ) -> Any:
119
+ preprocessed = self._preprocess_request(request)
120
+ try:
121
+ return handler(preprocessed)
122
+ except Exception as exc:
123
+ auth_msg = self._handle_error(request, exc)
124
+ if auth_msg is not None:
125
+ return auth_msg
126
+ raise
127
+
128
+ async def awrap_tool_call(
129
+ self,
130
+ request: ToolCallRequest,
131
+ handler: Callable[[ToolCallRequest], Awaitable[Any]],
132
+ ) -> Any:
133
+ preprocessed = self._preprocess_request(request)
134
+ try:
135
+ return await handler(preprocessed)
136
+ except Exception as exc:
137
+ auth_msg = self._handle_error(request, exc)
138
+ if auth_msg is not None:
139
+ return auth_msg
140
+ raise
141
+
142
+
143
+ __all__ = [
144
+ "MCPToolMiddleware",
145
+ "normalize_mcp_arguments",
146
+ ]
@@ -0,0 +1,393 @@
1
+ """Protect machine-managed memory blocks from agent edits while allowing user memory updates."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ from difflib import SequenceMatcher
7
+ from enum import Enum
8
+ import os
9
+ from pathlib import Path
10
+ from typing import TYPE_CHECKING, Any
11
+
12
+ from langchain.agents.middleware.types import AgentMiddleware, ToolCallRequest
13
+ from langchain_core.messages import ToolMessage
14
+ from langgraph.types import Command
15
+
16
+ from opscloud.middleware.registry import register_middleware
17
+ from opscloud.utils.logger import get_logger
18
+
19
+ if TYPE_CHECKING:
20
+ from collections.abc import Awaitable, Callable, Iterable
21
+
22
+ logger = get_logger(__name__)
23
+
24
+ _GUARDED_TOOLS: frozenset[str] = frozenset(
25
+ {
26
+ "write_file",
27
+ "edit_file",
28
+ "delete",
29
+ "delete_file",
30
+ "write_to_file",
31
+ "replace_file_content",
32
+ "multi_replace_file_content",
33
+ "rm",
34
+ }
35
+ )
36
+
37
+ ONBOARDING_NAME_MEMORY_START = "<!-- opscloud:onboarding-name:start -->"
38
+ ONBOARDING_NAME_MEMORY_END = "<!-- opscloud:onboarding-name:end -->"
39
+
40
+ _MANAGED_START_MARKERS = (
41
+ "<!-- opscloud:onboarding-name:start -->",
42
+ "<!-- opscloud:managed-memory:start -->",
43
+ "<!-- k8s_autopilot:onboarding-name:start -->",
44
+ "<!-- k8s_autopilot:managed-memory:start -->",
45
+ "<!-- deepagents:onboarding-name:start -->",
46
+ "<!-- opscode:onboarding-name:start -->",
47
+ "<!-- managed:start -->",
48
+ )
49
+ _MANAGED_END_MARKERS = (
50
+ "<!-- opscloud:onboarding-name:end -->",
51
+ "<!-- opscloud:managed-memory:end -->",
52
+ "<!-- k8s_autopilot:onboarding-name:end -->",
53
+ "<!-- k8s_autopilot:managed-memory:end -->",
54
+ "<!-- deepagents:onboarding-name:end -->",
55
+ "<!-- opscode:onboarding-name:end -->",
56
+ "<!-- managed:end -->",
57
+ )
58
+
59
+ _REJECTION_MESSAGE = (
60
+ "The region between the `opscloud:onboarding-name:start` and "
61
+ "`opscloud:onboarding-name:end` markers in {path} is machine-managed and "
62
+ "must not be edited. Your other changes to the file were kept, but the "
63
+ "managed block was restored to its previous content. Do not modify content "
64
+ "between those markers."
65
+ )
66
+
67
+ _RESTORE_FAILED_MESSAGE = (
68
+ "The region between the `opscloud:onboarding-name:start` and "
69
+ "`opscloud:onboarding-name:end` markers in {path} is machine-managed and "
70
+ "must not be edited. Your edit changed it and the previous content could "
71
+ "not be restored, so the managed block may now be corrupted. Do not modify "
72
+ "content between those markers, and do not rely on this edit having "
73
+ "succeeded."
74
+ )
75
+
76
+ _DELETE_REJECTION_MESSAGE = (
77
+ "The guarded memory file {path} contains a machine-managed region or is protected "
78
+ "and must not be deleted. Do not delete this file or a parent directory that contains it."
79
+ )
80
+
81
+
82
+ class _RestoreOutcome(Enum):
83
+ UNCHANGED = "unchanged"
84
+ RESTORED = "restored"
85
+ FAILED = "failed"
86
+
87
+
88
+ def extract_onboarding_name_block(text: str) -> str | None:
89
+ """Return the managed onboarding name block (markers included) if present."""
90
+ for start_marker, end_marker in zip(_MANAGED_START_MARKERS, _MANAGED_END_MARKERS, strict=False):
91
+ start = text.find(start_marker)
92
+ end = text.find(end_marker)
93
+ if start != -1 and end != -1 and start < end:
94
+ return text[start : end + len(end_marker)]
95
+ return None
96
+
97
+
98
+ def strip_onboarding_name_markers(text: str) -> str:
99
+ """Remove every onboarding-name marker occurrence from text."""
100
+ res = text
101
+ for m in _MANAGED_START_MARKERS + _MANAGED_END_MARKERS:
102
+ res = res.replace(m, "")
103
+ return res
104
+
105
+
106
+ def _upsert_onboarding_name_memory(existing: str, block: str) -> str:
107
+ """Insert or replace the managed onboarding name memory block."""
108
+ for start_marker, end_marker in zip(_MANAGED_START_MARKERS, _MANAGED_END_MARKERS, strict=False):
109
+ start = existing.find(start_marker)
110
+ end = existing.find(end_marker)
111
+ if start != -1 and end != -1 and start < end:
112
+ end += len(end_marker)
113
+ prefix = existing[:start].rstrip()
114
+ suffix = existing[end:].strip()
115
+ parts = [part for part in (prefix, block, suffix) if part]
116
+ return "\n\n".join(parts).rstrip() + "\n"
117
+
118
+ base = existing.rstrip()
119
+ if not base:
120
+ return f"## User Preferences\n\n{block}\n"
121
+ if "## User Preferences" in base:
122
+ return f"{base}\n\n{block}\n"
123
+ return f"{base}\n\n## User Preferences\n\n{block}\n"
124
+
125
+
126
+ @register_middleware(name="memory_guard")
127
+ class ManagedMemoryGuardMiddleware(AgentMiddleware[Any, Any]):
128
+ """Protects managed onboarding blocks and memory files from deletion while allowing user edits."""
129
+
130
+ def __init__(self, guarded_paths: Iterable[str | Path] = ()) -> None:
131
+ super().__init__()
132
+ requested = list(guarded_paths)
133
+ resolved: set[Path] = set()
134
+ for raw in requested:
135
+ try:
136
+ resolved.add(Path(raw).expanduser().resolve())
137
+ except (OSError, RuntimeError, ValueError):
138
+ logger.warning("Could not resolve guarded memory path %r", raw, exc_info=True)
139
+ self._guarded: frozenset[Path] = frozenset(resolved)
140
+ if requested and not self._guarded:
141
+ logger.warning(
142
+ "ManagedMemoryGuardMiddleware resolved no guarded paths from %r",
143
+ requested,
144
+ )
145
+
146
+ def _guarded_path(self, request: ToolCallRequest) -> Path | None:
147
+ tool_call = getattr(request, "tool_call", None)
148
+ if not isinstance(tool_call, dict):
149
+ return None
150
+ tool_name = tool_call.get("name", "")
151
+ if tool_name not in _GUARDED_TOOLS:
152
+ return None
153
+ args = tool_call.get("args") or {}
154
+ file_path = (
155
+ args.get("file_path")
156
+ or args.get("path")
157
+ or args.get("target")
158
+ or args.get("TargetFile")
159
+ or args.get("target_file")
160
+ or args.get("file")
161
+ or ""
162
+ )
163
+ if not isinstance(file_path, str) or not file_path:
164
+ return None
165
+ try:
166
+ resolved = Path(file_path).expanduser().resolve()
167
+ except (OSError, RuntimeError, ValueError):
168
+ logger.warning(
169
+ "Could not resolve target path %r for %s",
170
+ file_path,
171
+ tool_name,
172
+ exc_info=True,
173
+ )
174
+ return None
175
+ if tool_name in ("delete", "delete_file", "rm"):
176
+ for guarded in self._guarded:
177
+ try:
178
+ if guarded.is_relative_to(resolved) or resolved == guarded:
179
+ return guarded
180
+ except AttributeError:
181
+ if str(guarded).startswith(str(resolved)):
182
+ return guarded
183
+ return None
184
+ return resolved if resolved in self._guarded else None
185
+
186
+ @staticmethod
187
+ def _read(path: Path) -> str | None:
188
+ try:
189
+ fd = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0))
190
+ with os.fdopen(fd, "r", encoding="utf-8") as f:
191
+ return f.read()
192
+ except FileNotFoundError:
193
+ return None
194
+ except (OSError, UnicodeDecodeError):
195
+ logger.warning("Could not read guarded memory file %s", path, exc_info=True)
196
+ return None
197
+
198
+ @staticmethod
199
+ def _write(path: Path, content: str) -> None:
200
+ flags = os.O_WRONLY | os.O_TRUNC
201
+ if hasattr(os, "O_NOFOLLOW"):
202
+ flags |= os.O_NOFOLLOW
203
+ fd = os.open(path, flags)
204
+ with os.fdopen(fd, "w", encoding="utf-8", newline="") as f:
205
+ f.write(content)
206
+
207
+ @staticmethod
208
+ def _line_range_for_block(before: str, before_block: str) -> tuple[int, int] | None:
209
+ block_start = before.find(before_block)
210
+ if block_start == -1:
211
+ return None
212
+ block_end = block_start + len(before_block)
213
+ start_line: int | None = None
214
+ end_line: int | None = None
215
+ offset = 0
216
+ for line_number, line in enumerate(before.splitlines(keepends=True)):
217
+ line_end = offset + len(line)
218
+ if start_line is None and offset <= block_start < line_end:
219
+ start_line = line_number
220
+ if offset < block_end <= line_end:
221
+ end_line = line_number + 1
222
+ break
223
+ offset = line_end
224
+ if start_line is None or end_line is None:
225
+ return None
226
+ return start_line, end_line
227
+
228
+ @staticmethod
229
+ def _without_managed_block_edits(before: str, after: str, before_block: str) -> str | None:
230
+ block_range = ManagedMemoryGuardMiddleware._line_range_for_block(before, before_block)
231
+ if block_range is None:
232
+ return None
233
+ block_start, block_end = block_range
234
+ before_lines = before.splitlines(keepends=True)
235
+ after_lines = after.splitlines(keepends=True)
236
+ ranges: list[tuple[int, int]] = []
237
+ matcher = SequenceMatcher(None, before_lines, after_lines, autojunk=False)
238
+ for (
239
+ tag,
240
+ before_start,
241
+ before_end,
242
+ after_start,
243
+ after_end,
244
+ ) in matcher.get_opcodes():
245
+ overlaps = before_start < block_end and block_start < before_end
246
+ if tag == "insert":
247
+ if block_start < before_start < block_end:
248
+ ranges.append((after_start, after_end))
249
+ continue
250
+ if not overlaps or tag == "delete":
251
+ continue
252
+ if tag == "equal":
253
+ start = max(before_start, block_start)
254
+ end = min(before_end, block_end)
255
+ ranges.append(
256
+ (
257
+ after_start + start - before_start,
258
+ after_start + end - before_start,
259
+ )
260
+ )
261
+ else:
262
+ ranges.append((after_start, after_end))
263
+
264
+ if not ranges:
265
+ return after
266
+ parts: list[str] = []
267
+ cursor = 0
268
+ for start, end in sorted(ranges):
269
+ range_start = start
270
+ range_end = end
271
+ if range_start < cursor:
272
+ range_end = max(range_end, cursor)
273
+ range_start = cursor
274
+ parts.extend(after_lines[cursor:range_start])
275
+ cursor = range_end
276
+ parts.extend(after_lines[cursor:])
277
+ return "".join(parts)
278
+
279
+ def _restore(self, path: Path, before: str, before_block: str) -> _RestoreOutcome:
280
+ after = self._read(path)
281
+ if after is None:
282
+ logger.warning(
283
+ "Guarded memory file %s is unreadable after edit; cannot restore managed block",
284
+ path,
285
+ )
286
+ return _RestoreOutcome.FAILED
287
+ block_after = extract_onboarding_name_block(after)
288
+ if block_after == before_block:
289
+ return _RestoreOutcome.UNCHANGED
290
+ if block_after is not None:
291
+ source = after
292
+ else:
293
+ maybe_source = self._without_managed_block_edits(before, after, before_block)
294
+ if maybe_source is None:
295
+ logger.error(
296
+ "Could not locate previous managed block in %s; leaving the edited file untouched",
297
+ path,
298
+ )
299
+ return _RestoreOutcome.FAILED
300
+ source = strip_onboarding_name_markers(maybe_source)
301
+ restored = _upsert_onboarding_name_memory(source, before_block)
302
+ if extract_onboarding_name_block(restored) != before_block:
303
+ logger.error(
304
+ "Restored content for %s did not reproduce the managed block; leaving the edited file untouched",
305
+ path,
306
+ )
307
+ return _RestoreOutcome.FAILED
308
+ try:
309
+ self._write(path, restored)
310
+ except (OSError, UnicodeEncodeError):
311
+ logger.warning("Could not restore managed memory block at %s", path, exc_info=True)
312
+ return _RestoreOutcome.FAILED
313
+ return _RestoreOutcome.RESTORED
314
+
315
+ @staticmethod
316
+ def _error(request: ToolCallRequest, path: Path, *, restore_failed: bool) -> ToolMessage:
317
+ template = _RESTORE_FAILED_MESSAGE if restore_failed else _REJECTION_MESSAGE
318
+ tool_call = getattr(request, "tool_call", {})
319
+ return ToolMessage(
320
+ content=template.format(path=path),
321
+ name=tool_call.get("name", "edit_file"),
322
+ tool_call_id=tool_call.get("id", ""),
323
+ status="error",
324
+ )
325
+
326
+ @staticmethod
327
+ def _delete_error(request: ToolCallRequest, path: Path) -> ToolMessage:
328
+ tool_call = getattr(request, "tool_call", {})
329
+ return ToolMessage(
330
+ content=_DELETE_REJECTION_MESSAGE.format(path=path),
331
+ name=tool_call.get("name", "delete"),
332
+ tool_call_id=tool_call.get("id", ""),
333
+ status="error",
334
+ )
335
+
336
+ @staticmethod
337
+ def _reject_delete(path: Path, before: str | None) -> bool:
338
+ if before is not None:
339
+ return extract_onboarding_name_block(before) is not None or path.exists()
340
+ return path.exists()
341
+
342
+ def _result_after_restore(
343
+ self,
344
+ request: ToolCallRequest,
345
+ path: Path,
346
+ before: str,
347
+ before_block: str,
348
+ result: ToolMessage | Command[Any],
349
+ ) -> ToolMessage | Command[Any]:
350
+ outcome = self._restore(path, before, before_block)
351
+ if outcome is _RestoreOutcome.UNCHANGED:
352
+ return result
353
+ return self._error(request, path, restore_failed=outcome is _RestoreOutcome.FAILED)
354
+
355
+ def wrap_tool_call(
356
+ self,
357
+ request: ToolCallRequest,
358
+ handler: Callable[[ToolCallRequest], ToolMessage | Command[Any]],
359
+ ) -> ToolMessage | Command[Any]:
360
+ path = self._guarded_path(request)
361
+ if path is None:
362
+ return handler(request)
363
+ before = self._read(path)
364
+ tool_name = request.tool_call.get("name", "")
365
+ if tool_name in ("delete", "delete_file", "rm"):
366
+ if self._reject_delete(path, before):
367
+ return self._delete_error(request, path)
368
+ return handler(request)
369
+ before_block = extract_onboarding_name_block(before) if before is not None else None
370
+ if before is None or before_block is None:
371
+ return handler(request)
372
+ result = handler(request)
373
+ return self._result_after_restore(request, path, before, before_block, result)
374
+
375
+ async def awrap_tool_call(
376
+ self,
377
+ request: ToolCallRequest,
378
+ handler: Callable[[ToolCallRequest], Awaitable[ToolMessage | Command[Any]]],
379
+ ) -> ToolMessage | Command[Any]:
380
+ path = await asyncio.to_thread(self._guarded_path, request)
381
+ if path is None:
382
+ return await handler(request)
383
+ before = await asyncio.to_thread(self._read, path)
384
+ tool_name = request.tool_call.get("name", "")
385
+ if tool_name in ("delete", "delete_file", "rm"):
386
+ if await asyncio.to_thread(self._reject_delete, path, before):
387
+ return self._delete_error(request, path)
388
+ return await handler(request)
389
+ before_block = extract_onboarding_name_block(before) if before is not None else None
390
+ if before is None or before_block is None:
391
+ return await handler(request)
392
+ result = await handler(request)
393
+ return await asyncio.to_thread(self._result_after_restore, request, path, before, before_block, result)