agentops-cockpit 0.5.0__py3-none-any.whl → 0.9.7__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 (38) hide show
  1. agent_ops_cockpit/agent.py +142 -0
  2. agent_ops_cockpit/cli/main.py +104 -11
  3. agent_ops_cockpit/eval/load_test.py +15 -10
  4. agent_ops_cockpit/eval/quality_climber.py +23 -5
  5. agent_ops_cockpit/eval/red_team.py +37 -10
  6. agent_ops_cockpit/mcp_server.py +55 -21
  7. agent_ops_cockpit/ops/arch_review.py +79 -17
  8. agent_ops_cockpit/ops/cost_optimizer.py +0 -1
  9. agent_ops_cockpit/ops/evidence_bridge.py +132 -0
  10. agent_ops_cockpit/ops/frameworks.py +79 -10
  11. agent_ops_cockpit/ops/mcp_hub.py +1 -2
  12. agent_ops_cockpit/ops/orchestrator.py +363 -49
  13. agent_ops_cockpit/ops/pii_scrubber.py +1 -1
  14. agent_ops_cockpit/ops/policies.json +26 -0
  15. agent_ops_cockpit/ops/policy_engine.py +85 -0
  16. agent_ops_cockpit/ops/reliability.py +48 -14
  17. agent_ops_cockpit/ops/secret_scanner.py +10 -3
  18. agent_ops_cockpit/ops/ui_auditor.py +52 -11
  19. agent_ops_cockpit/ops/watcher.py +138 -0
  20. agent_ops_cockpit/ops/watchlist.json +88 -0
  21. agent_ops_cockpit/optimizer.py +393 -58
  22. agent_ops_cockpit/shadow/router.py +7 -8
  23. agent_ops_cockpit/system_prompt.md +13 -0
  24. agent_ops_cockpit/tests/golden_set.json +52 -0
  25. agent_ops_cockpit/tests/test_agent.py +34 -0
  26. agent_ops_cockpit/tests/test_arch_review.py +45 -0
  27. agent_ops_cockpit/tests/test_frameworks.py +100 -0
  28. agent_ops_cockpit/tests/test_optimizer.py +68 -0
  29. agent_ops_cockpit/tests/test_quality_climber.py +18 -0
  30. agent_ops_cockpit/tests/test_red_team.py +35 -0
  31. agent_ops_cockpit/tests/test_secret_scanner.py +24 -0
  32. agentops_cockpit-0.9.7.dist-info/METADATA +246 -0
  33. agentops_cockpit-0.9.7.dist-info/RECORD +47 -0
  34. {agentops_cockpit-0.5.0.dist-info → agentops_cockpit-0.9.7.dist-info}/entry_points.txt +1 -1
  35. agentops_cockpit-0.5.0.dist-info/METADATA +0 -171
  36. agentops_cockpit-0.5.0.dist-info/RECORD +0 -32
  37. {agentops_cockpit-0.5.0.dist-info → agentops_cockpit-0.9.7.dist-info}/WHEEL +0 -0
  38. {agentops_cockpit-0.5.0.dist-info → agentops_cockpit-0.9.7.dist-info}/licenses/LICENSE +0 -0
@@ -1,88 +1,349 @@
1
- import sys
1
+ from __future__ import annotations
2
2
  import os
3
3
  import re
4
- import ast
5
- from typing import List, Dict
4
+ from typing import List, Dict, Any
6
5
  import typer
7
6
  from rich.console import Console
8
7
  from rich.table import Table
9
8
  from rich.panel import Panel
10
9
  from rich.syntax import Syntax
10
+ from packaging import version
11
+
12
+ # Import the evidence bridge
13
+ try:
14
+ from agent_ops_cockpit.ops.evidence_bridge import get_package_evidence, get_compatibility_report
15
+ except ImportError:
16
+ # Fallback for local execution
17
+ try:
18
+ from backend.ops.evidence_bridge import get_package_evidence, get_compatibility_report
19
+ except ImportError:
20
+ # Final fallback
21
+ def get_package_evidence(pkg): return {}
22
+ def get_compatibility_report(imports): return []
11
23
 
12
24
  app = typer.Typer(help="AgentOps Cockpit: The Agent Optimizer CLI")
13
25
  console = Console()
14
26
 
15
27
  class OptimizationIssue:
16
- def __init__(self, id: str, title: str, impact: str, savings: str, description: str, diff: str, fix_pattern: str = None):
28
+ def __init__(self, id: str, title: str, impact: str, savings: str, description: str, diff: str, package: str = None, fix_pattern: str = None):
17
29
  self.id = id
18
30
  self.title = title
19
31
  self.impact = impact
20
32
  self.savings = savings
21
33
  self.description = description
22
34
  self.diff = diff
35
+ self.package = package
23
36
  self.fix_pattern = fix_pattern
37
+ self.evidence = None
24
38
 
25
- def analyze_code(content: str, file_path: str = "agent.py") -> List[OptimizationIssue]:
39
+ def analyze_code(content: str, file_path: str = "agent.py", versions: Dict[str, str] = None) -> List[OptimizationIssue]:
26
40
  issues = []
27
41
  content_lower = content.lower()
42
+ content_no_comments = re.sub(r'#.*', '', content_lower)
43
+ versions = versions or {}
44
+
45
+ # --- SITUATIONAL PLATFORM OPTIMIZATIONS ---
46
+
47
+ v_ai = versions.get("google-cloud-aiplatform", "Not Installed")
48
+ if "google.cloud.aiplatform" in content_lower or "vertexai" in content_lower:
49
+ if v_ai == "Not Installed":
50
+ issues.append(OptimizationIssue(
51
+ "vertex_install", "Install Modern Vertex SDK", "HIGH", "90% cost savings",
52
+ "You appear to be using Vertex AI logic but the SDK is not in your environment. Install v1.70.0+ to unlock context caching.",
53
+ "+ # pip install google-cloud-aiplatform>=1.70.0",
54
+ package="google-cloud-aiplatform"
55
+ ))
56
+ elif v_ai != "Unknown":
57
+ try:
58
+ if version.parse(v_ai) < version.parse("1.70.0"):
59
+ issues.append(OptimizationIssue(
60
+ "vertex_legacy_opt", "Situational Performance (Legacy SDK)", "MEDIUM", "20% cost savings",
61
+ f"Your SDK ({v_ai}) lacks native Context Caching. Optimize by using selective prompt pruning before execution.",
62
+ "+ from agent_ops_cockpit.ops.cost_optimizer import situational_pruning\n+ pruned = situational_pruning(context)",
63
+ package="google-cloud-aiplatform"
64
+ ))
65
+ issues.append(OptimizationIssue(
66
+ "vertex_upgrade_path", "Modernization Path", "HIGH", "90% cost savings",
67
+ "Upgrading to 1.70.0+ enables near-instant token reuse via CachingConfig.",
68
+ "+ # Upgrade to >1.70.0",
69
+ package="google-cloud-aiplatform"
70
+ ))
71
+ elif "cache" not in content_lower:
72
+ issues.append(OptimizationIssue(
73
+ "context_caching", "Enable Context Caching", "HIGH", "90% cost reduction",
74
+ "Large model context detected. Use native CachingConfig.",
75
+ "+ cache = vertexai.preview.CachingConfig(ttl=3600)",
76
+ package="google-cloud-aiplatform"
77
+ ))
78
+ except Exception:
79
+ pass
80
+
81
+ # OpenAI
82
+ openai_v = versions.get("openai", "Not Installed")
83
+ if "openai" in content_lower:
84
+ if openai_v != "Not Installed" and version.parse(openai_v) < version.parse("1.0.0"):
85
+ issues.append(OptimizationIssue(
86
+ "openai_legacy", "Found Legacy OpenAI SDK", "HIGH", "40% latency reduction",
87
+ f"You are on {openai_v}. Transitioning to the v1.0.0+ Client pattern enables modern streaming and improved error handling.",
88
+ "+ from openai import OpenAI\n+ client = OpenAI()",
89
+ package="openai"
90
+ ))
91
+ elif "prompt_cache" not in content_lower:
92
+ issues.append(OptimizationIssue(
93
+ "openai_caching", "OpenAI Prompt Caching", "MEDIUM", "50% latency reduction",
94
+ "OpenAI automatically caches repeated input prefixes. Ensure your system prompt is first.",
95
+ "+ # Ensure system prompt is first\n+ messages = [{'role': 'system', ...}]",
96
+ package="openai"
97
+ ))
98
+
99
+ # Anthropic
100
+ if ("anthropic" in content_lower or "claude" in content_lower) and "orchestra" not in content_lower:
101
+ issues.append(OptimizationIssue(
102
+ "anthropic_orchestration", "Anthropic Orchestration Pattern", "HIGH", "30% reliability boost",
103
+ "Claude performs best with an Orchestrator-Subagent pattern for complex tasks.",
104
+ "+ # Use orchestrator to delegate sub-tasks",
105
+ package="anthropic"
106
+ ))
107
+
108
+ # Microsoft
109
+ if ("autogen" in content_lower or "microsoft" in content_lower) and "workflow" not in content_lower:
110
+ issues.append(OptimizationIssue(
111
+ "ms_workflows", "Microsoft Agent Workflows", "MEDIUM", "40% consistency boost",
112
+ "Using graph-based repeatable workflows ensures enterprise reliability.",
113
+ "+ # Define a repeatable graph-based flow",
114
+ package="pyautogen"
115
+ ))
116
+
117
+ # AWS
118
+ if ("bedrock" in content_lower or "boto3" in content_lower) and "actiongroup" not in content_lower:
119
+ issues.append(OptimizationIssue(
120
+ "aws_action_groups", "AWS Bedrock Action Groups", "HIGH", "50% tool reliability",
121
+ "Standardize tool execution via Bedrock Action Group schemas.",
122
+ "+ # Define Bedrock Action Group",
123
+ package="aws-sdk"
124
+ ))
125
+
126
+ # CopilotKit
127
+ if "copilotkit" in content_lower and "usecopilotstate" not in content_lower:
128
+ issues.append(OptimizationIssue(
129
+ "copilot_state", "CopilotKit Shared State", "MEDIUM", "60% UI responsiveness",
130
+ "Ensure the Face remains aligned with the Engine via shared state sync.",
131
+ "+ # Use shared state for UI alignment",
132
+ package="@copilotkit/react-core"
133
+ ))
28
134
 
29
- # --- PLATFORM SPECIFIC OPTIMIZATIONS ---
135
+ # Routing
136
+ if "pro" in content_lower and "flash" not in content_lower:
137
+ issues.append(OptimizationIssue(
138
+ "model_routing", "Smart Model Routing", "HIGH", "70% cost savings",
139
+ "Route simple queries to Flash models to minimize consumption.",
140
+ "+ if is_simple(q): model = 'gemini-1.5-flash'",
141
+ package="google-cloud-aiplatform"
142
+ ))
143
+
144
+ # Infrastructure (Cloud Run + GKE)
145
+ if "cloud run" in content_lower and "cpu_boost" not in content_lower:
146
+ issues.append(OptimizationIssue(
147
+ "cr_startup_boost", "Cloud Run Startup Boost", "HIGH", "50% latency reduction",
148
+ "Enable Startup CPU Boost to reduce cold-start latency for Python agents.",
149
+ "+ startup_cpu_boost: true",
150
+ package="google-cloud-run"
151
+ ))
152
+ if ("gke" in content_lower or "kubernetes" in content_lower) and "identity" not in content_lower:
153
+ issues.append(OptimizationIssue(
154
+ "gke_identity", "GKE Workload Identity", "HIGH", "100% security baseline",
155
+ "Use Workload Identity for secure service-to-service communication.",
156
+ "+ # Use Workload Identity",
157
+ package="google-cloud-gke"
158
+ ))
30
159
 
31
- # Check for OpenAI Prompt Caching
32
- if "openai" in content_lower and "prompt_cache" not in content_lower:
160
+ # Language Specific (Go + Node)
161
+ if file_path.endswith(".go") and "goroutine" not in content_lower:
162
+ issues.append(OptimizationIssue(
163
+ "go_concurrency", "Go Native Concurrency", "HIGH", "80% throughput boost",
164
+ "Leveraging Goroutines for parallel tool execution is a Go best practice.",
165
+ "+ go func() { tool.execute() }()",
166
+ package="golang"
167
+ ))
168
+ if (file_path.endswith(".ts") or file_path.endswith(".js") or "axios" in content_lower) and "fetch" not in content_lower:
33
169
  issues.append(OptimizationIssue(
34
- "openai_caching",
35
- "OpenAI Prompt Caching",
36
- "MEDIUM",
37
- "50% latency reduction",
38
- "OpenAI automatically caches repeated input prefixes. Ensure your system prompt is at the beginning of the message list.",
39
- "+ # Ensure system prompt is first and static for optimal caching\n+ messages = [{'role': 'system', 'content': SYSTEM_PROMPT}, ...]",
40
- fix_pattern="# [Cockpit Fix] Optimize OpenAI Caching\n"
170
+ "node_native_fetch", "Native Fetch API", "MEDIUM", "20% bundle reduction",
171
+ "Node 20+ supports native fetch, reducing dependency on heavy libraries like axios.",
172
+ "+ const res = await fetch(url);",
173
+ package="nodejs"
41
174
  ))
42
175
 
43
- # Check for large system instructions
44
- large_string_pattern = re.compile(r'"""[\s\S]{200,}"""|\'\'\'[\s\S]{200,}\'\'\'')
45
- if large_string_pattern.search(content) and "cache" not in content_lower:
176
+ lg_v = versions.get("langgraph", "Not Installed")
177
+ if "langgraph" in content_lower:
178
+ if lg_v != "Not Installed" and lg_v != "Unknown":
179
+ try:
180
+ if version.parse(lg_v) < version.parse("0.1.0"):
181
+ issues.append(OptimizationIssue(
182
+ "langgraph_legacy", "Situational Stability (Legacy LangGraph)", "HIGH", "Stability Boost",
183
+ f"You are on {lg_v}. Older versions lack the hardened StateGraph compilation. Upgrade is recommended.",
184
+ "+ # Consider upgrading for better persistence",
185
+ package="langgraph"
186
+ ))
187
+ except Exception:
188
+ pass
189
+
190
+ if "persistence" not in content_lower and "checkpointer" not in content_lower:
191
+ issues.append(OptimizationIssue(
192
+ "langgraph_persistence", "LangGraph Persistence", "HIGH", "100% state recovery",
193
+ "A checkpointer is mandatory for reliable long-running agents.",
194
+ "+ graph.compile(checkpointer=checkpointer)",
195
+ package="langgraph"
196
+ ))
197
+ if "recursion" not in content_lower:
198
+ issues.append(OptimizationIssue(
199
+ "langgraph_recursion", "Recursion Limits", "MEDIUM", "Safety Guardrail",
200
+ "Set recursion limits to prevent expensive infinite loops in cyclic graphs.",
201
+ "+ graph.invoke(..., config={'recursion_limit': 50})",
202
+ package="langgraph"
203
+ ))
204
+
205
+ # --- ARCHITECTURAL OPTIMIZATIONS ---
206
+
207
+ # Large system instructions (individual docstrings > 200 chars)
208
+ docstrings = re.findall(r'"""([\s\S]*?)"""|\'\'\'([\s\S]*?)\'\'\'', content)
209
+ has_large_prompt = any(len(d[0] or d[1]) > 200 for d in docstrings)
210
+
211
+ if has_large_prompt and "cache" not in content_lower:
46
212
  issues.append(OptimizationIssue(
47
- "context_caching",
48
- "Enable Context Caching",
49
- "HIGH",
50
- "90% cost reduction on reuse",
51
- "Large static system instructions detected. Using context caching (Gemini/Anthropic) prevents redundant token processing.",
52
- "+ cache = vertexai.preview.CachingConfig(ttl=3600)\n+ model = GenerativeModel('gemini-1.5-pro', caching_config=cache)",
53
- fix_pattern="# [Cockpit Fix] Vertex AI Context Caching enabled\n"
213
+ "context_caching", "Enable Context Caching", "HIGH", "90% cost reduction",
214
+ "Large static system instructions detected. Use context caching.",
215
+ "+ cache = vertexai.preview.CachingConfig(ttl=3600)",
216
+ package="google-cloud-aiplatform"
54
217
  ))
55
218
 
56
- # Check for missing semantic cache
219
+ # Missing semantic cache
57
220
  if "hive_mind" not in content_lower and "cache" not in content_lower:
58
221
  issues.append(OptimizationIssue(
59
- "semantic_caching",
60
- "Implement Semantic Caching",
61
- "HIGH",
62
- "40-60% cost savings",
63
- "No caching layer detected. Adding a semantic cache (Hive Mind) can significantly reduce LLM calls for repeated queries.",
64
- "+ @hive_mind(cache=global_cache)\n async def chat(q: str): ...",
65
- fix_pattern="# [Cockpit Fix] Hive Mind Semantic Caching integrated\n"
222
+ "semantic_caching", "Implement Semantic Caching", "HIGH", "40-60% savings",
223
+ "No caching layer detected. Adding a semantic cache reduces LLM costs.",
224
+ "+ @hive_mind(cache=global_cache)",
225
+ package="google-adk"
66
226
  ))
67
227
 
228
+ # --- BEST PRACTICE OPTIMIZATIONS ---
229
+
230
+ # Prompt Externalization
231
+ if has_large_prompt:
232
+ issues.append(OptimizationIssue(
233
+ "external_prompts", "Externalize System Prompts", "MEDIUM", "Architectural Debt Reduction",
234
+ "Keeping large system prompts in code makes them hard to version and test. Move them to 'system_prompt.md' and load dynamically.",
235
+ "+ with open('system_prompt.md', 'r') as f:\n+ SYSTEM_PROMPT = f.read()"
236
+ ))
237
+
238
+ # Resiliency / Retries
239
+ if "retry" not in content_lower and "tenacity" not in content_lower:
240
+ issues.append(OptimizationIssue(
241
+ "resiliency_retries", "Implement Exponential Backoff", "HIGH", "99.9% Reliability",
242
+ "Your agent calls external APIs/DBs but has no retry logic. Use 'tenacity' to handle transient failures.",
243
+ "+ @retry(wait=wait_exponential(multiplier=1, min=4, max=10), stop=stop_after_attempt(3))",
244
+ package="tenacity"
245
+ ))
246
+
247
+ # Session Management
248
+ if "session" not in content_lower and "conversation_id" not in content_lower:
249
+ issues.append(OptimizationIssue(
250
+ "session_management", "Add Session Tracking", "MEDIUM", "User Continuity",
251
+ "No session tracking detected. Agents in production need a 'conversation_id' to maintain multi-turn context.",
252
+ "+ def chat(q: str, conversation_id: str = None):"
253
+ ))
254
+
255
+ # Pinecone Optimization
256
+ if "pinecone" in content_lower:
257
+ if "grpc" not in content_lower:
258
+ issues.append(OptimizationIssue(
259
+ "pinecone_grpc", "Pinecone High-Perf (gRPC)", "MEDIUM", "40% latency reduction",
260
+ "You are using the standard Pinecone client. Switching to pinecone[grpc] enables low-latency streaming for large vector retrievals.",
261
+ "+ from pinecone.grpc import PineconeGRPC as Pinecone\n+ pc = Pinecone(api_key='...')"
262
+ ))
263
+ if "namespace" not in content_lower:
264
+ issues.append(OptimizationIssue(
265
+ "pinecone_isolation", "Pinecone Namespace Isolation", "MEDIUM", "RAG Accuracy Boost",
266
+ "No namespaces detected. Use namespaces to isolate user data or document segments for more accurate retrieval.",
267
+ "+ index.query(..., namespace='customer-a')"
268
+ ))
269
+
270
+ # Google Cloud Database Optimizations
271
+
272
+ # AlloyDB
273
+ if "alloydb" in content_no_comments:
274
+ if "columnar" not in content_no_comments:
275
+ issues.append(OptimizationIssue(
276
+ "alloydb_columnar", "AlloyDB Columnar Engine", "HIGH", "100x Query Speedup",
277
+ "AlloyDB detected. Enable the Columnar Engine for analytical and AI-driven vector queries.",
278
+ "+ # Enable AlloyDB Columnar Engine for vector scaling"
279
+ ))
280
+
281
+ # BigQuery
282
+ if "bigquery" in content_no_comments or "bq" in content_no_comments:
283
+ if "vector_search" not in content_no_comments:
284
+ issues.append(OptimizationIssue(
285
+ "bq_vector_search", "BigQuery Vector Search", "HIGH", "FinOps: Serverless RAG",
286
+ "BigQuery detected. Use BQ Vector Search for cost-effective RAG over massive datasets without moving data to a separate DB.",
287
+ "+ SELECT * FROM VECTOR_SEARCH(TABLE my_dataset.embeddings, ...)"
288
+ ))
289
+
290
+ # Cloud SQL
291
+ if "cloudsql" in content_lower or "psycopg2" in content_lower or "sqlalchemy" in content_lower:
292
+ if "cloud-sql-connector" not in content_lower:
293
+ issues.append(OptimizationIssue(
294
+ "cloudsql_connector", "Cloud SQL Python Connector", "MEDIUM", "100% Secure Auth",
295
+ "Using raw drivers detected. Use the official Cloud SQL Python Connector for IAM-based authentication and automatic encryption.",
296
+ "+ from google.cloud.sql.connector import Connector\n+ connector = Connector()"
297
+ ))
298
+
299
+ # Firestore
300
+ if "firestore" in content_lower:
301
+ if "vector" not in content_lower:
302
+ issues.append(OptimizationIssue(
303
+ "firestore_vector", "Firestore Vector Search (Native)", "HIGH", "Real-time RAG",
304
+ "Firestore detected. Use native Vector Search and KNN queries for high-concurrency mobile/web agent retrieval.",
305
+ "+ collection.find_nearest(vector_field='embedding', ...)"
306
+ ))
307
+
308
+ # Oracle OCI Optimizations
309
+ if "oci" in content_lower or "oracle" in content_lower:
310
+ if "resource_principal" not in content_lower:
311
+ issues.append(OptimizationIssue(
312
+ "oci_auth", "OCI Resource Principals", "HIGH", "100% Secure Auth",
313
+ "Using static config/keys detected on OCI. Use Resource Principals for secure, credential-less access from OCI compute.",
314
+ "+ auth = oci.auth.signers.get_resource_principals_signer()"
315
+ ))
316
+
317
+ # CrewAI Optimizations
318
+ if "crewai" in content_lower or "crew(" in content_lower:
319
+ if "manager_agent" not in content_lower and "hierarchical" not in content_lower:
320
+ issues.append(OptimizationIssue(
321
+ "crewai_manager", "Use Hierarchical Manager", "MEDIUM", "30% Coordination Boost",
322
+ "Your crew uses sequential execution. For complex tasks, a Manager Agent improves task handoffs and reasoning.",
323
+ "+ crew = Crew(..., process=Process.hierarchical, manager_agent=manager)"
324
+ ))
325
+
68
326
  return issues
69
327
 
70
328
  def estimate_savings(token_count: int, issues: List[OptimizationIssue]) -> Dict[str, Any]:
71
- """
72
- Step 5: FinOps Integration. Calculate literal dollar-amount projection.
73
- """
74
- # Baseline: $10 per 1M tokens (mixed input/output)
75
329
  baseline_cost_per_m = 10.0
76
330
  monthly_requests = 10000
77
331
  current_cost = (token_count / 1_000_000) * baseline_cost_per_m * monthly_requests
78
332
 
79
333
  total_savings_pct = 0.0
80
334
  for issue in issues:
81
- if "90%" in issue.savings: total_savings_pct += 0.4 # Cumulative weighted
82
- if "50%" in issue.savings: total_savings_pct += 0.2
83
- if "40-60%" in issue.savings: total_savings_pct += 0.25
335
+ if "90%" in issue.savings:
336
+ total_savings_pct += 0.45 # Context Caching / Modern SDK
337
+ elif "70%" in issue.savings:
338
+ total_savings_pct += 0.35 # Smart Routing (Pro -> Flash)
339
+ elif "50%" in issue.savings:
340
+ total_savings_pct += 0.20 # Infrastructure / Startup Boost
341
+ elif "40-60%" in issue.savings:
342
+ total_savings_pct += 0.25 # Semantic Caching (Hive Mind)
343
+ else:
344
+ total_savings_pct += 0.05 # Standard Best Practices
84
345
 
85
- projected_savings = current_cost * min(total_savings_pct, 0.8) # Cap at 80%
346
+ projected_savings = current_cost * min(total_savings_pct, 0.85)
86
347
 
87
348
  return {
88
349
  "current_monthly": current_cost,
@@ -94,34 +355,80 @@ def estimate_savings(token_count: int, issues: List[OptimizationIssue]) -> Dict[
94
355
  def audit(
95
356
  file_path: str = typer.Argument("agent.py", help="Path to the agent code to audit"),
96
357
  interactive: bool = typer.Option(True, "--interactive/--no-interactive", "-i", help="Run in interactive mode"),
97
- apply_fix: bool = typer.Option(False, "--apply", "--fix", help="Automatically apply recommended fixes")
358
+ apply_fix: bool = typer.Option(False, "--apply", "--fix", help="Automatically apply recommended fixes"),
359
+ quick: bool = typer.Option(False, "--quick", "-q", help="Skip live evidence fetching for faster execution")
98
360
  ):
99
- """
100
- Audits agent code and proposes cost/perf/FinOps optimizations.
101
- """
102
361
  console.print(Panel.fit("🔍 [bold blue]GCP AGENT OPS: OPTIMIZER AUDIT[/bold blue]", border_style="blue"))
103
- console.print(f"Target: [yellow]{file_path}[/yellow]")
104
-
105
362
  if not os.path.exists(file_path):
106
- console.print(f"❌ [red]Error: File {file_path} not found.[/red]")
363
+ console.print(f"❌ [red]Error: Path {file_path} not found.[/red]")
107
364
  raise typer.Exit(1)
108
365
 
366
+ # If it's a directory, try to find the agent entry point
367
+ if os.path.isdir(file_path):
368
+ found = False
369
+ for entry in ["agent.py", "main.py", "app.py"]:
370
+ candidate = os.path.join(file_path, entry)
371
+ if os.path.exists(candidate):
372
+ file_path = candidate
373
+ found = True
374
+ break
375
+ if not found:
376
+ # Look for any .py file if common names aren't found
377
+ for root, dirs, files in os.walk(file_path):
378
+ # Prune excluded directories for performance
379
+ dirs[:] = [d for d in dirs if d not in [".venv", "node_modules", ".git", "__pycache__", "dist", "build"]]
380
+
381
+ for f in files:
382
+ if f.endswith(".py") and f != "__init__.py":
383
+ file_path = os.path.join(root, f)
384
+ found = True
385
+ break
386
+ if found: break
387
+
388
+ if not found:
389
+ console.print(f"❌ [red]Error: No python entry point found in {file_path}[/red]")
390
+ raise typer.Exit(1)
391
+
392
+ console.print(f"Target: [yellow]{file_path}[/yellow]")
393
+
109
394
  with open(file_path, 'r') as f:
110
395
  content = f.read()
111
396
 
397
+ # Heuristic: Find all imported packages
398
+ imports = re.findall(r"(?:from|import)\s+([\w\.-]+)", content)
399
+
400
+ from agent_ops_cockpit.ops.evidence_bridge import get_installed_version
401
+ package_versions = { pkg: get_installed_version(pkg) for pkg in ["google-cloud-aiplatform", "openai", "anthropic", "langgraph", "crewai"] }
402
+
112
403
  token_estimate = len(content.split()) * 1.5
113
404
  console.print(f"📊 Token Metrics: ~[bold]{token_estimate:.0f}[/bold] prompt tokens detected.")
114
405
 
115
- with console.status("[bold green]Running heuristic analysis..."):
116
- issues = analyze_code(content, file_path)
117
- import time
118
- time.sleep(1)
406
+ issues = analyze_code(content, file_path, versions=package_versions)
407
+ # Inject live evidence (skip in quick mode)
408
+ if not quick:
409
+ for issue in issues:
410
+ if issue.package:
411
+ issue.evidence = get_package_evidence(issue.package)
412
+
413
+ # --- CROSS-PACKAGE VALIDATION ---
414
+ comp_reports = get_compatibility_report(imports)
415
+
416
+ if comp_reports:
417
+ console.print("\n[bold yellow]🧩 Cross-Package Validation:[/bold yellow]")
418
+ for report in comp_reports:
419
+ if report["type"] == "INCOMPATIBLE":
420
+ console.print(f"❌ [bold red]Conflict Detected:[/bold red] {report['component']} + {report['conflict_with']}")
421
+ console.print(f" [dim]{report['reason']}[/dim]")
422
+ elif report["type"] == "SYNERGY":
423
+ console.print(f"✅ [bold green]Synergy Verified:[/bold green] {report['component']} is optimally paired.")
119
424
 
120
425
  if not issues:
121
- console.print("\n[bold green]✅ No immediate optimization opportunities found. Your agent is lean![/bold green]")
122
- return
426
+ console.print("\n[bold green]✅ No immediate code-level optimizations found. Your agent is lean![/bold green]")
427
+ if not comp_reports:
428
+ return
429
+ else:
430
+ raise typer.Exit(0)
123
431
 
124
- # Step 5: FinOps Report
125
432
  savings = estimate_savings(token_estimate, issues)
126
433
  finops_panel = Panel(
127
434
  f"💰 [bold]FinOps Projection (Est. 10k req/mo)[/bold]\n"
@@ -141,15 +448,39 @@ def audit(
141
448
  console.print(f"\n[bold white on blue] --- [{opt.impact} IMPACT] {opt.title} --- [/bold white on blue]")
142
449
  console.print(f"Benefit: [green]{opt.savings}[/green]")
143
450
  console.print(f"Reason: {opt.description}")
144
- console.print("\nProposed Change:")
451
+
452
+ if opt.evidence and "error" not in opt.evidence:
453
+ ev = opt.evidence
454
+ ev_title = "[dim]SDK Citation & Evidence[/dim]"
455
+
456
+ # Highlight if an upgrade is required for maximum efficiency
457
+ if ev.get("upgrade_required"):
458
+ console.print("🚨 [bold yellow]URGENT UPGRADE RECOMMENDED[/bold yellow]")
459
+ console.print(f" Current: {ev['installed_version']} | Required for optimization: >={ev['min_optimized_version']}")
460
+ ev_title = "[bold red]UPGRADE REQUIRED Evidence[/bold red]"
461
+
462
+ ev_panel = Panel(
463
+ f"🔗 [bold]Source[/bold]: {ev['source_url']}\n"
464
+ f"📅 [bold]Latest Release[/bold]: {ev['release_date'][:10]}\n"
465
+ f"📝 [bold]Note[/bold]: {ev['best_practice_context']}",
466
+ title=ev_title,
467
+ border_style="red" if ev.get("upgrade_required") else "dim"
468
+ )
469
+ console.print(ev_panel)
470
+ # Orchestrator parsing
471
+ console.print(f"SOURCE: {opt.title} | {ev['source_url']} | {ev['best_practice_context'].replace('\\n', ' ')}")
472
+
145
473
  syntax = Syntax(opt.diff, "python", theme="monokai", line_numbers=False)
146
474
  console.print(syntax)
147
475
 
476
+ # Output ACTION: for report generation
477
+ console.print(f"ACTION: {file_path}:1 | Optimization: {opt.title} | {opt.description} (Est. {opt.savings})")
478
+
148
479
  do_apply = False
149
480
  if apply_fix:
150
481
  do_apply = True
151
482
  elif interactive:
152
- do_apply = typer.confirm("\nDo you want to apply this optimization?", default=True)
483
+ do_apply = typer.confirm("\nDo you want to apply this code-level optimization?", default=True)
153
484
 
154
485
  if do_apply:
155
486
  console.print("✅ [APPROVED] applying fix...")
@@ -164,7 +495,6 @@ def audit(
164
495
  with open(file_path, 'w') as f:
165
496
  f.write(fixed_content)
166
497
  console.print(f"\n✨ [bold green]Applied {applied} optimizations to {file_path}![/bold green]")
167
- console.print("🚀 Run 'agent-ops report' to verify the new architecture score.")
168
498
 
169
499
  summary_table = Table(title="🎯 AUDIT SUMMARY")
170
500
  summary_table.add_column("Category", style="cyan")
@@ -173,5 +503,10 @@ def audit(
173
503
  summary_table.add_row("Optimizations Rejected", str(rejected))
174
504
  console.print(summary_table)
175
505
 
506
+ # CI/CD Enforcement: Fail if high-impact issues remain in non-interactive mode
507
+ if not interactive and any(opt.impact == "HIGH" for opt in issues):
508
+ console.print("\n[bold red]❌ HIGH IMPACT issues detected. Optimization required for production.[/bold red]")
509
+ raise typer.Exit(code=1)
510
+
176
511
  if __name__ == "__main__":
177
512
  app()
@@ -1,7 +1,8 @@
1
1
  import asyncio
2
2
  import json
3
3
  import uuid
4
- from typing import Dict, Any, Callable
4
+ import os
5
+ from typing import Any, Callable
5
6
  from datetime import datetime
6
7
 
7
8
  class ShadowRouter:
@@ -13,17 +14,17 @@ class ShadowRouter:
13
14
  self.v1 = v1_func
14
15
  self.v2 = v2_func
15
16
 
16
- async def route(self, query: str, context: Dict[str, Any] = None):
17
+ async def route(self, query: str, **kwargs):
17
18
  trace_id = str(uuid.uuid4())
18
19
 
19
20
  # 1. Primary Call (Production v1) - Sequential/Blocking
20
21
  start_v1 = datetime.now()
21
- v1_resp = await self.v1(query, context)
22
+ v1_resp = await self.v1(query, **kwargs)
22
23
  v1_latency = (datetime.now() - start_v1).total_seconds()
23
24
 
24
25
  # 2. Shadow Call (Experimental v2) - Asynchronous/Non-blocking
25
26
  # We fire and forget this, or use a background task
26
- asyncio.create_task(self._run_shadow(trace_id, query, context, v1_resp, v1_latency))
27
+ asyncio.create_task(self._run_shadow(trace_id, query, v1_resp, v1_latency, **kwargs))
27
28
 
28
29
  return {
29
30
  "response": v1_resp,
@@ -31,14 +32,14 @@ class ShadowRouter:
31
32
  "latency": v1_latency
32
33
  }
33
34
 
34
- async def _run_shadow(self, trace_id: str, query: str, context: Dict[str, Any], v1_resp: Any, v1_latency: float):
35
+ async def _run_shadow(self, trace_id: str, query: str, v1_resp: Any, v1_latency: float, **kwargs):
35
36
  """
36
37
  Runs the v2 agent in the 'shadow' without user impact.
37
38
  Logs the comparison to BigQuery/Cloud Logging.
38
39
  """
39
40
  try:
40
41
  start_v2 = datetime.now()
41
- v2_resp = await self.v2(query, context)
42
+ v2_resp = await self.v2(query, **kwargs)
42
43
  v2_latency = (datetime.now() - start_v2).total_seconds()
43
44
 
44
45
  comparison = {
@@ -71,5 +72,3 @@ class ShadowRouter:
71
72
  os.makedirs("traces", exist_ok=True)
72
73
  with open(f"traces/{data['traceId']}.json", "w") as f:
73
74
  json.dump(data, f)
74
-
75
- import os
@@ -0,0 +1,13 @@
1
+ # 🕹️ AgentOps Cockpit: System Persona
2
+
3
+ You are a professional **Google Well-Architected Agent Orchestrator**.
4
+ Your primary goal is to assist users in building, optimizing, and securing AI agents on Google Cloud.
5
+
6
+ ## 🛡️ Core Directives:
7
+ 1. **Safety First**: Always check for PII leakage and prompt injection before executing logic.
8
+ 2. **Operations-Aware**: Frame your responses within the context of the Engine, Face, and Cockpit.
9
+ 3. **Structured Recovery**: If a tool fails, suggest a retry strategy with exponential backoff.
10
+ 4. **Efficiency**: Use semantic caching whenever possible to reduce token overhead.
11
+
12
+ ## 📡 Output Standard:
13
+ Follow the **A2UI Protocol**. Always return structured JSON that the Face can render.