rsnarsna commited on
Commit
e585a67
·
2 Parent(s): cf1659d8628c98

Merge branch 'dev' into main - automated platform synchronization

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .dockerignore +2 -2
  2. .env.example +4 -3
  3. app/blog/router.py +18 -86
  4. app/routers/__init__.py +45 -0
  5. app/routers/catalogs.py +155 -0
  6. app/routers/compiler.py +159 -0
  7. app/routers/execution.py +209 -0
  8. app/routers/health.py +121 -0
  9. app/routers/introspection.py +180 -0
  10. app/routers/orgs.py +200 -0
  11. app/routers/packages_ast.py +189 -0
  12. app/routers/telemetry.py +159 -0
  13. app/routers/templates.py +122 -0
  14. app/routers/users.py +134 -0
  15. app/routers/workflows.py +245 -0
  16. app/routers/workspace_env.py +137 -0
  17. database/__init__.py +2 -2
  18. database/audit_repo.py +66 -0
  19. database/blueprint_catalog_utils.py +136 -0
  20. database/blueprints_repo.py +231 -0
  21. database/catalogs_repo.py +246 -0
  22. database/connection.py +113 -0
  23. database/mongo.py +54 -1851
  24. database/orgs_repo.py +210 -0
  25. database/seeds.py +244 -0
  26. database/templates_repo.py +178 -0
  27. database/users_repo.py +200 -0
  28. database/workflows_repo.py +165 -0
  29. database/workspace_env_repo.py +150 -0
  30. env.py +17 -24
  31. lambdas/codegen/__init__.py +1 -0
  32. lambdas/codegen/assembler.py +135 -0
  33. lambdas/codegen/context.py +250 -0
  34. lambdas/codegen/emitters_control_oop.py +249 -0
  35. lambdas/codegen/emitters_core.py +246 -0
  36. lambdas/codegen/emitters_files_data.py +225 -0
  37. lambdas/codegen/emitters_libraries.py +82 -0
  38. lambdas/codegen/scope_analysis.py +205 -0
  39. lambdas/codegen/scoped_blocks.py +201 -0
  40. lambdas/codegen/wave1_core_emitters.py +156 -0
  41. lambdas/codegen_handler.py +147 -1488
  42. lambdas/inspect_handler.py +102 -186
  43. lambdas/run_handler.py +63 -39
  44. lambdas/validate_handler.py +64 -1607
  45. lambdas/validation/__init__.py +1 -0
  46. lambdas/validation/connection_validator.py +229 -0
  47. lambdas/validation/graph_checks.py +243 -0
  48. lambdas/validation/metadata_validator.py +91 -0
  49. lambdas/validation/node_checks.py +262 -0
  50. lambdas/validation/security_checks.py +132 -0
.dockerignore CHANGED
@@ -14,8 +14,8 @@ tests/
14
 
15
  # Environment files and credentials
16
  .env
17
- .env.local
18
- .env.*.local
19
 
20
  # Virtual environments
21
  venv/
 
14
 
15
  # Environment files and credentials
16
  .env
17
+ .env.*
18
+ !.env.example
19
 
20
  # Virtual environments
21
  venv/
.env.example CHANGED
@@ -1,12 +1,13 @@
1
  # ALLI Backend Environment Configuration
2
  PORT=8001
3
- SUPABASE_URL=https://vxtbipuklehlklxznvfu.supabase.co
4
  SUPABASE_KEY=your_supabase_anon_key
5
- DATABASE_URL=postgresql://postgres:[YOUR-PASSWORD]@db.vxtbipuklehlklxznvfu.supabase.co:5432/postgres
6
  MONGODB_URI=mongodb+srv://<username>:<password>@cluster.mongodb.net/?retryWrites=true&w=majority
7
  MONGODB_DB_NAME=alli
8
  STAGE=dev
9
 
10
- # Authentication Middleware Configuration
11
  AUTH_REQUIRED=false
12
  MASTER_API_KEY=your_master_api_key_here
 
 
1
  # ALLI Backend Environment Configuration
2
  PORT=8001
3
+ SUPABASE_URL=https://your-project-ref.supabase.co
4
  SUPABASE_KEY=your_supabase_anon_key
5
+ DATABASE_URL=postgresql://postgres:your_db_password@db.your-project-ref.supabase.co:5432/postgres
6
  MONGODB_URI=mongodb+srv://<username>:<password>@cluster.mongodb.net/?retryWrites=true&w=majority
7
  MONGODB_DB_NAME=alli
8
  STAGE=dev
9
 
10
+ # Authentication & Workspace Secret Encryption Configuration
11
  AUTH_REQUIRED=false
12
  MASTER_API_KEY=your_master_api_key_here
13
+ ALLI_ENV_ENCRYPTION_KEY=your_fernet_32_byte_base64_key_here
app/blog/router.py CHANGED
@@ -1,106 +1,38 @@
1
  """
2
  ALLI Backend - Blog & Knowledge Validation Engine
3
- Exposes verified educational articles, Python tutorials, and visual programming guides.
4
  """
5
 
6
- from typing import Dict, Any, List, Optional
7
  from fastapi import APIRouter, HTTPException, Query
8
- from pydantic import BaseModel, Field
9
 
10
  blog_router = APIRouter(prefix="/blog", tags=["Blog & Knowledge"])
11
 
12
- VERIFIED_BLOG_POSTS: List[Dict[str, Any]] = [
13
- {
14
- "id": "post_about_alli_2d_to_3d",
15
- "slug": "about-alli-2d-to-3d-visual-programming",
16
- "title": "About ALLI: Our Ambition & The 2D-to-3D Visual Breakthrough",
17
- "excerpt": "Why ALLI exists, our urgent mission empowering beginners to program visually via 2D UI, and our ultimate destination: transitioning from 2D into an immersive 3D spatial programming canvas.",
18
- "category": "engineering",
19
- "categoryLabel": "Engineering & Architecture",
20
- "tags": ["ALLI", "Visual Programming", "Spatial Computing", "Architecture", "Python", "3D Canvas"],
21
- "featured": True,
22
- "coverImage": "/icons/icon-512x512.png",
23
- "publishedAt": "October 18, 2026",
24
- "readingTimeMinutes": 5,
25
- "studioTemplateId": "math-logic-calculator",
26
- "views": 0,
27
- "likes": 0,
28
- "author": {
29
- "name": "ALLI Engineering & Education",
30
- "role": "Platform Architecture & Learning",
31
- "avatar_url": "/icons/icon-192x192.png",
32
- "bio": "Empowering developers and learners by bridging visual architectural thinking with pure Python execution.",
33
- },
34
- "content_length": 5420,
35
- "verified": True,
36
- },
37
- {
38
- "id": "post_what_is_python",
39
- "slug": "what-is-python-uses-and-benefits",
40
- "title": "Python: What It Is, Why to Learn It, and Where It Is Used",
41
- "excerpt": "A foundation guide to Python programming, real-world implementations across AI, web, and automation, and a structured 9-stage learning roadmap for beginners.",
42
- "category": "tutorials",
43
- "categoryLabel": "Tutorials & Workflows",
44
- "tags": ["Python", "Beginners", "Tutorial", "Automation", "Backend", "AI"],
45
- "featured": False,
46
- "coverImage": "/icons/icon-512x512.png",
47
- "publishedAt": "October 17, 2026",
48
- "readingTimeMinutes": 8,
49
- "studioTemplateId": "math-logic-calculator",
50
- "views": 0,
51
- "likes": 0,
52
- "author": {
53
- "name": "ALLI Engineering & Education",
54
- "role": "Platform Architecture & Learning",
55
- "avatar_url": "/icons/icon-192x192.png",
56
- "bio": "Empowering developers and learners by bridging visual architectural thinking with pure Python execution.",
57
- },
58
- "content_length": 14680,
59
- "verified": True,
60
- },
61
- ]
62
-
63
- VERIFIED_CATEGORIES: List[Dict[str, Any]] = [
64
- {"id": "all", "label": "All Posts", "count": 2},
65
- {"id": "engineering", "label": "Engineering & Architecture", "count": 1},
66
- {"id": "tutorials", "label": "Tutorials & Workflows", "count": 1},
67
- ]
68
-
69
 
70
  @blog_router.get("", summary="List verified blog posts and categories")
71
  def list_blog_posts(
72
  category: Optional[str] = Query(None, description="Filter by category slug"),
73
  q: Optional[str] = Query(None, description="Search query"),
74
  ):
75
- """Returns validated platform blog posts and active categories."""
76
- posts = list(VERIFIED_BLOG_POSTS)
77
-
78
- if category and category != "all":
79
- posts = [p for p in posts if p.get("category") == category]
80
-
81
- if q and q.strip():
82
- term = q.lower().strip()
83
- posts = [
84
- p
85
- for p in posts
86
- if term in p.get("title", "").lower()
87
- or term in p.get("excerpt", "").lower()
88
- or any(term in t.lower() for t in p.get("tags", []))
89
- ]
90
-
91
  return {
92
  "status": "success",
93
- "categories": VERIFIED_CATEGORIES,
94
- "posts": posts,
95
- "total": len(posts),
96
  }
97
 
98
 
99
  @blog_router.get("/validate", summary="Validate blog database and content integrity")
100
  def validate_blog_integrity():
101
- """Runs content integrity pre-flight check across all published articles."""
 
 
 
102
  issues = []
103
- for post in VERIFIED_BLOG_POSTS:
104
  if not post.get("slug"):
105
  issues.append(f"Post {post.get('id')} is missing a slug")
106
  if not post.get("title"):
@@ -110,17 +42,17 @@ def validate_blog_integrity():
110
 
111
  return {
112
  "status": "healthy" if not issues else "degraded",
113
- "total_posts": len(VERIFIED_BLOG_POSTS),
114
- "total_categories": len(VERIFIED_CATEGORIES),
115
- "verified_slugs": [p["slug"] for p in VERIFIED_BLOG_POSTS],
116
  "integrity_errors": issues,
117
  }
118
 
119
 
120
  @blog_router.get("/{slug}", summary="Get blog post by slug")
121
  def get_blog_post_by_slug(slug: str):
122
- """Retrieves full post metadata and verification status for a given slug."""
123
- post = next((p for p in VERIFIED_BLOG_POSTS if p.get("slug") == slug), None)
124
  if not post:
125
  raise HTTPException(status_code=404, detail="Blog post not found")
126
  return {"status": "success", "post": post}
 
1
  """
2
  ALLI Backend - Blog & Knowledge Validation Engine
3
+ Serves verified educational articles and Python tutorials from MongoDB Atlas / backend seed.
4
  """
5
 
6
+ from typing import Optional
7
  from fastapi import APIRouter, HTTPException, Query
8
+ from database.mongo import workflow_repo
9
 
10
  blog_router = APIRouter(prefix="/blog", tags=["Blog & Knowledge"])
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  @blog_router.get("", summary="List verified blog posts and categories")
14
  def list_blog_posts(
15
  category: Optional[str] = Query(None, description="Filter by category slug"),
16
  q: Optional[str] = Query(None, description="Search query"),
17
  ):
18
+ """Return validated platform blog posts and active categories from MongoDB Atlas / seed."""
19
+ data = workflow_repo.list_blog_posts(category=category, q=q)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  return {
21
  "status": "success",
22
+ "categories": data.get("categories", []),
23
+ "posts": data.get("posts", []),
24
+ "total": data.get("total", 0),
25
  }
26
 
27
 
28
  @blog_router.get("/validate", summary="Validate blog database and content integrity")
29
  def validate_blog_integrity():
30
+ """Run content integrity pre-flight check across all published articles."""
31
+ data = workflow_repo.list_blog_posts()
32
+ posts = data.get("posts", [])
33
+ categories = data.get("categories", [])
34
  issues = []
35
+ for post in posts:
36
  if not post.get("slug"):
37
  issues.append(f"Post {post.get('id')} is missing a slug")
38
  if not post.get("title"):
 
42
 
43
  return {
44
  "status": "healthy" if not issues else "degraded",
45
+ "total_posts": len(posts),
46
+ "total_categories": len(categories),
47
+ "verified_slugs": [p["slug"] for p in posts if p.get("slug")],
48
  "integrity_errors": issues,
49
  }
50
 
51
 
52
  @blog_router.get("/{slug}", summary="Get blog post by slug")
53
  def get_blog_post_by_slug(slug: str):
54
+ """Retrieve full post content, metadata, and verification status for a given slug."""
55
+ post = workflow_repo.get_blog_post(slug)
56
  if not post:
57
  raise HTTPException(status_code=404, detail="Blog post not found")
58
  return {"status": "success", "post": post}
app/routers/__init__.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ALLI Backend - Modular FastAPI Routers Package"""
2
+
3
+ from app.routers.health import router as health_router
4
+ from app.routers.orgs import router as orgs_router
5
+ from app.routers.compiler import router as compiler_router
6
+ from app.routers.workflows import router as workflows_router
7
+ from app.routers.execution import router as execution_router
8
+ from app.routers.telemetry import router as telemetry_router
9
+ from app.routers.packages_ast import router as packages_ast_router
10
+ from app.routers.introspection import router as introspection_router
11
+ from app.routers.catalogs import router as catalogs_router
12
+ from app.routers.templates import router as templates_router
13
+ from app.routers.users import router as users_router
14
+ from app.routers.workspace_env import router as workspace_env_router
15
+
16
+ ALL_CORE_ROUTERS = [
17
+ health_router,
18
+ orgs_router,
19
+ compiler_router,
20
+ workflows_router,
21
+ execution_router,
22
+ telemetry_router,
23
+ packages_ast_router,
24
+ introspection_router,
25
+ catalogs_router,
26
+ templates_router,
27
+ users_router,
28
+ workspace_env_router,
29
+ ]
30
+
31
+ __all__ = [
32
+ "ALL_CORE_ROUTERS",
33
+ "health_router",
34
+ "orgs_router",
35
+ "compiler_router",
36
+ "workflows_router",
37
+ "execution_router",
38
+ "telemetry_router",
39
+ "packages_ast_router",
40
+ "introspection_router",
41
+ "catalogs_router",
42
+ "templates_router",
43
+ "users_router",
44
+ "workspace_env_router",
45
+ ]
app/routers/catalogs.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Database-First Catalogs & Unified Studio Bootstrap Router
3
+ Serves core specs, Python fundamentals (9 consolidated categories / 53 topics),
4
+ learning modules, validation reports, and single-call /studio/bootstrap hydration.
5
+ """
6
+
7
+ import re
8
+ from datetime import datetime, timezone
9
+ from typing import Any, Dict, List, Optional
10
+ from fastapi import APIRouter, HTTPException, Request
11
+ from pydantic import BaseModel, Field
12
+
13
+ from database.mongo import workflow_repo
14
+ from language_core.node_schema import default_registry
15
+ from security.rbac import get_request_workspace, require_roles
16
+
17
+ router = APIRouter(tags=["Catalogs & Studio Bootstrap"])
18
+
19
+
20
+ class CoreSpecsPayload(BaseModel):
21
+ categories: List[Dict[str, Any]] = Field(..., description="List of Python core categories")
22
+
23
+
24
+ def _scrub_validation_value(val: Any) -> Any:
25
+ if isinstance(val, str):
26
+ v = re.sub(r"[a-zA-Z]:\\[^\s\"']+", "[workspace]/file", val)
27
+ v = re.sub(r"/(?:Users|home|root|var|tmp|etc)/[^\s\"']+", "[workspace]/file", v)
28
+ return re.sub(r"(?:AWS_|SUPABASE_|MONGODB_|SECRET|PASSWORD|TOKEN|API_KEY)[A-Za-z0-9_-]*", "[REDACTED]", v)
29
+ if isinstance(val, list):
30
+ return [_scrub_validation_value(x) for x in val]
31
+ if isinstance(val, dict):
32
+ clean: Dict[str, Any] = {}
33
+ for k, v in val.items():
34
+ if re.search(r"(?:^|_)(?:token|secret|password|credential|service_role|conn_str|uri)(?:$|_)", k, re.I) and isinstance(v, str) and len(v) > 8:
35
+ clean[k] = "[REDACTED]"
36
+ else:
37
+ clean[k] = _scrub_validation_value(v)
38
+ return clean
39
+ return val
40
+
41
+
42
+ @router.get("/studio/bootstrap")
43
+ def get_studio_bootstrap(request: Request):
44
+ """Single-call DB-backed bootstrap payload for Studio initialization."""
45
+ ctx = get_request_workspace(request)
46
+ node_schemas = default_registry.to_json_dict()
47
+ fundamentals = workflow_repo.get_programming_fundamentals(consolidated=True)
48
+ py_fundamentals = workflow_repo.get_python_fundamentals()
49
+ packages = workflow_repo.list_blueprints()
50
+ core_specs = workflow_repo.get_core_specs()
51
+ palette_categories = workflow_repo.get_studio_palette()
52
+ templates = workflow_repo.list_templates()
53
+ db_connected = bool(workflow_repo.is_connected)
54
+
55
+ return {
56
+ "status": "success",
57
+ "node_schemas": node_schemas,
58
+ "node_schemas_count": len(node_schemas),
59
+ "core_specs": core_specs,
60
+ "primary_studio_categories": palette_categories,
61
+ "python_fundamentals": py_fundamentals,
62
+ "fundamentals": fundamentals,
63
+ "packages": packages,
64
+ "templates": templates,
65
+ "workspace": {
66
+ "org_id": ctx.get("org_id") or "local_personal",
67
+ "role": ctx.get("role") or "owner",
68
+ "user_id": ctx.get("user_id"),
69
+ "is_authenticated": bool(ctx.get("is_authenticated")),
70
+ "is_local_loopback": bool(ctx.get("is_local_loopback")),
71
+ },
72
+ "persistence": {
73
+ "connected": db_connected,
74
+ "backend": "mongodb" if db_connected else "in_memory_fallback",
75
+ "mode": "mongodb_atlas" if db_connected else "volatile_ram",
76
+ },
77
+ }
78
+
79
+
80
+ @router.get("/core-specs")
81
+ def get_core_specs():
82
+ return workflow_repo.get_core_specs()
83
+
84
+
85
+ @router.post("/core-specs")
86
+ def save_core_specs(payload: CoreSpecsPayload, request: Request):
87
+ require_roles(request, {"owner", "admin", "editor"}, allow_local_solo=True, action="core_specs.save", resource_type="catalog")
88
+ success = workflow_repo.save_core_specs(payload.categories)
89
+ return {"success": success, "count": len(payload.categories)}
90
+
91
+
92
+ @router.get("/python-fundamentals")
93
+ def get_python_fundamentals():
94
+ return workflow_repo.get_python_fundamentals()
95
+
96
+
97
+ @router.post("/python-fundamentals")
98
+ def save_python_fundamentals(payload: Dict[str, Any], request: Request):
99
+ require_roles(request, {"owner", "admin", "editor"}, allow_local_solo=True, action="python_fundamentals.save", resource_type="catalog")
100
+ success = workflow_repo.save_python_fundamentals(payload)
101
+ return {"success": success}
102
+
103
+
104
+ @router.get("/fundamentals")
105
+ def get_programming_fundamentals(consolidated: bool = True):
106
+ """Return DB-backed Programming Fundamentals (9 consolidated categories / 53 topics cleanly)."""
107
+ return workflow_repo.get_programming_fundamentals(consolidated=consolidated)
108
+
109
+
110
+ @router.get("/learning")
111
+ def list_learning_modules(gate: Optional[str] = None):
112
+ """Return DB-backed guided learning curriculum lessons."""
113
+ lessons = workflow_repo.list_learning_modules(gate=gate)
114
+ return {"status": "success", "count": len(lessons), "lessons": lessons}
115
+
116
+
117
+ @router.get("/learning/{lesson_id}")
118
+ def get_learning_module(lesson_id: str):
119
+ lesson = workflow_repo.get_learning_module(lesson_id)
120
+ if not lesson:
121
+ raise HTTPException(status_code=404, detail=f"Lesson '{lesson_id}' not found.")
122
+ return {"status": "success", "lesson": lesson}
123
+
124
+
125
+ @router.get("/validation-lab/report")
126
+ def get_validation_lab_report():
127
+ """Serve sanitized Phase 3 & 4 validation telemetry and canonical inventory data."""
128
+ try:
129
+ artifacts = workflow_repo.get_validation_artifacts()
130
+ report_data = artifacts.get("report") or {}
131
+ inventory_data = artifacts.get("inventory") or []
132
+ if not report_data or not inventory_data:
133
+ raise HTTPException(status_code=404, detail="Validation lab telemetry files not found.")
134
+
135
+ raw_report = {
136
+ "phase": "Phase 4: Node Validation Lab Results UI & Cross-Tier Telemetry",
137
+ "timestamp": report_data.get("timestamp", datetime.now(timezone.utc).isoformat()),
138
+ "status": report_data.get("status", "PASSED"),
139
+ "exit_code": report_data.get("exit_code", 0),
140
+ "commit_sha": "79fa7746583f4a004bfbeefdb3036057a2987e2a",
141
+ "summary": report_data.get("summary", {}),
142
+ "sections": report_data.get("sections", {}),
143
+ "inventory_metadata": artifacts.get("inventory_metadata", {}),
144
+ "inventory": inventory_data,
145
+ "metadata": {
146
+ "generator": "ALLI FastAPI Backend Telemetry Service",
147
+ "sanitized": True,
148
+ "version": "3.0.0",
149
+ },
150
+ }
151
+ return _scrub_validation_value(raw_report)
152
+ except HTTPException:
153
+ raise
154
+ except Exception as e:
155
+ raise HTTPException(status_code=500, detail=f"Failed to load validation lab telemetry: {str(e)}")
app/routers/compiler.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Graph Validation, Compilation, Planning & Codegen Router
3
+ Provides type checking, AST expression validation, Kahn's DAG compilation, and PEP 8 export.
4
+ """
5
+
6
+ import json
7
+ from datetime import datetime, timezone
8
+ from typing import Any, Dict, List, Optional
9
+ from fastapi import APIRouter, Request
10
+ from pydantic import BaseModel, Field
11
+
12
+ from lambdas.validate_handler import (
13
+ validate_types_logic,
14
+ validate_connection_logic,
15
+ validate_expression_logic,
16
+ validate_workflow_protocol_logic,
17
+ )
18
+ from lambdas.compile_handler import compile_dag_logic
19
+ from lambdas.codegen_handler import generate_code_logic
20
+ from language_core.codegen_contracts import CodegenRequest
21
+ from language_core.workspace_env_contracts import (
22
+ DEFAULT_DOCKERIGNORE_CONTENT,
23
+ collect_referenced_env_keys,
24
+ generate_env_example,
25
+ )
26
+ from security.rbac import require_roles
27
+
28
+ router = APIRouter(tags=["Validation & Codegen"])
29
+
30
+
31
+ class ValidateRequest(BaseModel):
32
+ source_type: Optional[str] = Field("Any", description="Output port data type")
33
+ target_type: Optional[str] = Field("Any", description="Input port data type")
34
+ connection: Optional[Dict[str, Any]] = Field(None, description="Optional connection dictionary")
35
+ nodes_map: Optional[Dict[str, Any]] = Field(None, description="Optional nodes lookup map")
36
+ edges: Optional[List[Dict[str, Any]]] = Field(None, description="Existing graph edges")
37
+
38
+
39
+ class ValidateExpressionRequest(BaseModel):
40
+ expression: str = Field(..., description="Python expression or statement syntax to validate with AST")
41
+
42
+
43
+ class CompileRequest(BaseModel):
44
+ nodes: List[Dict[str, Any]] = Field(default_factory=list, description="Visual graph nodes")
45
+ edges: List[Dict[str, Any]] = Field(default_factory=list, description="Visual graph edges")
46
+
47
+
48
+ @router.post("/validate")
49
+ def validate_ports(payload: ValidateRequest, request: Request = None):
50
+ """Validate type compatibility using Language Core assignability rules."""
51
+ if request is not None:
52
+ require_roles(request, {"owner", "admin", "editor", "viewer"}, allow_local_solo=True)
53
+ if payload.connection:
54
+ return validate_connection_logic(
55
+ payload.connection,
56
+ nodes_map=payload.nodes_map,
57
+ edges=payload.edges,
58
+ )
59
+ return validate_types_logic(payload.source_type or "Any", payload.target_type or "Any")
60
+
61
+
62
+ @router.post("/validate/expression")
63
+ def validate_expression(payload: ValidateExpressionRequest, request: Request = None):
64
+ """Validate Python expression or code block syntax using AST."""
65
+ if request is not None:
66
+ require_roles(request, {"owner", "admin", "editor", "viewer"}, allow_local_solo=True)
67
+ return validate_expression_logic(payload.expression)
68
+
69
+
70
+ @router.post("/validate/workflow")
71
+ @router.post("/graph/validate")
72
+ def validate_workflow(payload: Dict[str, Any], request: Request = None):
73
+ """Validate entire graph using the 10-possibility Workflow Validation Fundamental Protocol."""
74
+ if request is not None:
75
+ require_roles(request, {"owner", "admin", "editor", "viewer"}, allow_local_solo=True)
76
+ graph = payload.get("graph") or payload.get("graph_spec") or payload
77
+ exit_code = payload.get("execution_exit_code")
78
+ return validate_workflow_protocol_logic(graph, execution_exit_code=exit_code)
79
+
80
+
81
+ @router.post("/graph/plan")
82
+ def get_graph_execution_plan(payload: Dict[str, Any], request: Request = None):
83
+ """Generate a deterministic, staged execution plan with pre-flight validation."""
84
+ from language_core.execution_planner import build_execution_plan
85
+
86
+ if request is not None:
87
+ require_roles(request, {"owner", "admin", "editor", "viewer"}, allow_local_solo=True)
88
+ graph = payload.get("graph") or payload.get("graph_spec") or payload
89
+ options = payload.get("options") or {}
90
+ return build_execution_plan(graph, options=options)
91
+
92
+
93
+ @router.post("/compile")
94
+ def compile_dag(payload: CompileRequest, request: Request = None):
95
+ """Run Kahn's algorithm, detect cycle deadlocks (DAG-001), and compute execution order."""
96
+ if request is not None:
97
+ require_roles(request, {"owner", "admin", "editor", "viewer"}, allow_local_solo=True)
98
+ return compile_dag_logic(payload.nodes, payload.edges)
99
+
100
+
101
+ @router.post("/codegen")
102
+ @router.post("/graph/codegen")
103
+ def generate_code(payload: CodegenRequest, request: Request = None):
104
+ """Synthesize standalone PEP 8 Python script, requirements.txt, and Dockerfile."""
105
+ if request is not None:
106
+ require_roles(request, {"owner", "admin", "editor", "viewer"}, allow_local_solo=True)
107
+ return generate_code_logic(
108
+ nodes=payload.nodes,
109
+ edges=payload.edges,
110
+ execution_order=payload.execution_order,
111
+ graph_name=payload.name or "alli_workflow",
112
+ dependencies=payload.dependencies,
113
+ graph=payload.graph,
114
+ pin_exact=payload.pin_exact,
115
+ )
116
+
117
+
118
+ @router.post("/export")
119
+ def export_workflow_bundle(payload: Dict[str, Any], request: Request = None):
120
+ """Export a complete standalone bundle (workflow.py, requirements.txt, Dockerfile, .env.example, .dockerignore, workflow.json, README.md)."""
121
+ if request is not None:
122
+ require_roles(request, {"owner", "admin", "editor", "viewer"}, allow_local_solo=True)
123
+ graph = payload.get("graph") or payload.get("graph_spec") or payload
124
+ nodes = graph.get("nodes", [])
125
+ edges = graph.get("edges", [])
126
+ name = graph.get("name", "alli_workflow")
127
+ dependencies = graph.get("dependencies", [])
128
+
129
+ artifacts = generate_code_logic(
130
+ nodes=nodes,
131
+ edges=edges,
132
+ graph_name=name,
133
+ dependencies=dependencies,
134
+ )
135
+ filename = artifacts.get("filename", f"{name}.py")
136
+ python_code = artifacts.get("python_code", "")
137
+ requirements_txt = artifacts.get("requirements_txt", "")
138
+ dockerfile = artifacts.get("dockerfile", "")
139
+ env_example = generate_env_example(collect_referenced_env_keys(graph))
140
+
141
+ return {
142
+ "project_name": name,
143
+ "filename": filename,
144
+ "files": {
145
+ filename: python_code,
146
+ "requirements.txt": requirements_txt,
147
+ "Dockerfile": dockerfile,
148
+ ".env.example": env_example,
149
+ ".dockerignore": DEFAULT_DOCKERIGNORE_CONTENT,
150
+ "workflow.json": json.dumps(graph, indent=2),
151
+ "README.md": (
152
+ f"# {name}\n\nExported standalone Python workflow generated by ALLI "
153
+ f"(Advanced Logic & Linked Intelligence).\n\n## Environment\n```bash\n"
154
+ f"cp .env.example .env\n# Fill in any required secret values in .env before running\n```\n\n"
155
+ f"## Run\n```bash\npip install -r requirements.txt\npython {filename}\n```\n"
156
+ ),
157
+ },
158
+ "exported_at": datetime.now(timezone.utc).isoformat(),
159
+ }
app/routers/execution.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Workflow Execution & Step Session Router
3
+ Provides multi-mode DAG execution, single-step debugging, session inspection, and run cancellation.
4
+ """
5
+
6
+ import re
7
+ from typing import Any, Dict, List, Optional
8
+ from fastapi import APIRouter, HTTPException, Request
9
+ from pydantic import BaseModel, Field
10
+
11
+ from app.routers.telemetry import verify_run_access
12
+ from database.mongo import workflow_repo
13
+ from lambdas.run_handler import (
14
+ cancel_workflow_logic,
15
+ execute_code_logic,
16
+ execute_workflow_logic,
17
+ execute_workflow_step_logic,
18
+ )
19
+ from language_core.execution_session import GLOBAL_SESSION_REGISTRY, GraphRevisionMismatchError
20
+ from language_core.executor.run_prep import scrub_result_secrets
21
+ from language_core.runtime_events import GLOBAL_EVENT_STORE
22
+ from runtime_environment.active_runs import GLOBAL_ACTIVE_RUNS
23
+ from security.rbac import is_auth_required, require_roles
24
+
25
+ router = APIRouter(tags=["Execution"])
26
+ RUN_ID_PATTERN = re.compile(r"^[a-zA-Z0-9_\-\.:]{1,128}$")
27
+
28
+
29
+ class RunRequest(BaseModel):
30
+ code: Optional[str] = Field(None, description="Python source code to execute")
31
+ graph: Optional[Dict[str, Any]] = Field(None, description="Workflow graph to execute")
32
+ nodes: Optional[List[Dict[str, Any]]] = Field(None, description="Visual graph nodes")
33
+ edges: Optional[List[Dict[str, Any]]] = Field(None, description="Visual graph edges")
34
+ timeout_seconds: Optional[float] = Field(5.0, description="Max execution duration before kill")
35
+ mode: Optional[str] = Field(
36
+ None,
37
+ description="Execution mode: 'in_process', 'local', 'local_subprocess', 'docker', or 'auto'",
38
+ )
39
+ target_node_id: Optional[str] = Field(None, description="Target node ID for isolated step execution")
40
+ cached_outputs: Optional[Dict[str, Dict[str, Any]]] = Field(None, description="Cached upstream outputs for isolated step test")
41
+ pinned_inputs: Optional[Dict[str, Dict[str, Any]]] = Field(None, description="Pinned sample inputs for isolated step test")
42
+ single_node_only: Optional[bool] = Field(False, description="Run only target_node_id using cached/pinned inputs")
43
+
44
+
45
+ @router.post("/run")
46
+ def execute_code(payload: RunRequest, request: Request, user_id: Optional[str] = None):
47
+ ctx = require_roles(request, {"owner", "admin", "editor", "viewer"}, allow_local_solo=True)
48
+ auth_user = getattr(request.state, "user", None)
49
+ u_id = auth_user.get("id") if auth_user else (user_id or getattr(payload, "user_id", None))
50
+ role = str(ctx.get("role") or "")
51
+ org_id = ctx.get("org_id")
52
+ if u_id and role != "viewer":
53
+ workflow_repo.update_user_quota(u_id, 1)
54
+
55
+ if payload.graph or payload.nodes is not None:
56
+ graph = payload.graph or {"nodes": payload.nodes or [], "edges": payload.edges or []}
57
+ try:
58
+ result = execute_workflow_logic(
59
+ graph,
60
+ timeout_seconds=payload.timeout_seconds or 5.0,
61
+ owner_user_id=u_id,
62
+ target_node_id=payload.target_node_id,
63
+ mode=payload.mode or "in_process",
64
+ org_id=org_id,
65
+ role=role,
66
+ cached_outputs=payload.cached_outputs,
67
+ pinned_inputs=payload.pinned_inputs,
68
+ single_node_only=bool(payload.single_node_only),
69
+ )
70
+ except PermissionError as exc:
71
+ raise HTTPException(status_code=403, detail={"error": str(exc), "code": "ERR-RBAC-SECRET-DENIED"})
72
+ else:
73
+ if role == "viewer":
74
+ raise HTTPException(
75
+ status_code=403,
76
+ detail={"error": "Forbidden: Viewers cannot execute arbitrary Python code.", "code": "ERR-RBAC-CODE-DENIED"},
77
+ )
78
+ result = execute_code_logic(
79
+ payload.code or "",
80
+ timeout_seconds=payload.timeout_seconds or 5.0,
81
+ mode=payload.mode or "local",
82
+ )
83
+
84
+ if isinstance(result, dict):
85
+ if role == "viewer":
86
+ result["sandbox_read_only"] = True
87
+ elif u_id and result.get("status") in ("success", "completed"):
88
+ workflow_repo.record_successful_run(u_id)
89
+ return result
90
+
91
+
92
+ @router.post("/graph/execute")
93
+ @router.post("/workflows/execute")
94
+ def execute_workflow(payload: Dict[str, Any], request: Request, user_id: Optional[str] = None):
95
+ ctx = require_roles(request, {"owner", "admin", "editor", "viewer"}, allow_local_solo=True)
96
+ auth_user = getattr(request.state, "user", None)
97
+ u_id = auth_user.get("id") if auth_user else (user_id or payload.get("user_id"))
98
+ role = str(ctx.get("role") or "")
99
+ org_id = ctx.get("org_id")
100
+ if u_id and role != "viewer":
101
+ workflow_repo.update_user_quota(u_id, 1)
102
+
103
+ graph = payload.get("graph") or payload
104
+ timeout = float(payload.get("timeout_seconds", 5.0))
105
+ target_node_id = payload.get("target_node_id") or payload.get("targetNodeId")
106
+ exec_mode = payload.get("mode") or "in_process"
107
+ try:
108
+ result = execute_workflow_logic(
109
+ graph,
110
+ timeout_seconds=timeout,
111
+ owner_user_id=u_id,
112
+ target_node_id=target_node_id,
113
+ mode=exec_mode,
114
+ org_id=org_id,
115
+ role=role,
116
+ cached_outputs=payload.get("cached_outputs") or payload.get("cachedOutputs"),
117
+ pinned_inputs=payload.get("pinned_inputs") or payload.get("pinnedInputs"),
118
+ single_node_only=bool(payload.get("single_node_only") or payload.get("singleNodeOnly")),
119
+ )
120
+ except PermissionError as exc:
121
+ raise HTTPException(status_code=403, detail={"error": str(exc), "code": "ERR-RBAC-SECRET-DENIED"})
122
+ if isinstance(result, dict):
123
+ if role == "viewer":
124
+ result["sandbox_read_only"] = True
125
+ elif u_id and result.get("status") in ("success", "completed"):
126
+ workflow_repo.record_successful_run(u_id)
127
+ return result
128
+
129
+
130
+ @router.post("/graph/step")
131
+ def execute_workflow_step_endpoint(payload: Dict[str, Any], request: Request):
132
+ ctx = require_roles(request, {"owner", "admin", "editor", "viewer"}, allow_local_solo=True)
133
+ auth_user = getattr(request.state, "user", None)
134
+ u_id = auth_user.get("id") if auth_user else payload.get("user_id")
135
+ graph = payload.get("graph") or payload.get("graph_spec") or payload
136
+ target_node_id = payload.get("target_node_id") or payload.get("node_id")
137
+ run_id = payload.get("run_id") or payload.get("runId")
138
+ if run_id and isinstance(graph, dict) and "run_id" not in graph:
139
+ graph["run_id"] = run_id
140
+
141
+ try:
142
+ res = execute_workflow_step_logic(
143
+ graph=graph,
144
+ target_node_id=target_node_id,
145
+ previous_outputs=payload.get("previous_outputs") or payload.get("outputs") or {},
146
+ previous_results=payload.get("previous_results") or payload.get("node_results") or {},
147
+ timeout_seconds=float(payload.get("timeout_seconds", 5.0)),
148
+ run_id=run_id,
149
+ owner_user_id=u_id,
150
+ org_id=ctx.get("org_id"),
151
+ role=ctx.get("role"),
152
+ )
153
+ if isinstance(res, dict) and res.get("code") == "ERR-GRAPH-MISMATCH":
154
+ raise HTTPException(status_code=409, detail=res)
155
+ return res
156
+ except PermissionError as exc:
157
+ raise HTTPException(status_code=403, detail={"error": str(exc), "code": "ERR-RBAC-SECRET-DENIED"})
158
+ except GraphRevisionMismatchError as err:
159
+ raise HTTPException(
160
+ status_code=409,
161
+ detail={
162
+ "error": str(err),
163
+ "code": "ERR-GRAPH-MISMATCH",
164
+ "run_id": err.run_id,
165
+ "expected_hash": err.expected_hash,
166
+ "incoming_hash": err.incoming_hash,
167
+ },
168
+ )
169
+
170
+
171
+ @router.get("/graph/step/{run_id}")
172
+ @router.get("/graph/run/{run_id}")
173
+ def get_execution_session_state(run_id: str, request: Request):
174
+ clean_id = str(run_id).strip()
175
+ if not clean_id or not RUN_ID_PATTERN.match(clean_id):
176
+ raise HTTPException(
177
+ status_code=400,
178
+ detail={"error": "Malformed or invalid run_id identifier", "code": "ERR-INVALID-RUN-ID"},
179
+ )
180
+ session = GLOBAL_SESSION_REGISTRY.get(clean_id)
181
+ if session:
182
+ verify_run_access(clean_id, request)
183
+ return scrub_result_secrets(session.to_dict())
184
+ active_run = GLOBAL_ACTIVE_RUNS.get(clean_id)
185
+ if active_run:
186
+ verify_run_access(clean_id, request)
187
+ return scrub_result_secrets(active_run.to_dict())
188
+ raise HTTPException(
189
+ status_code=404,
190
+ detail={"error": f"Session or run '{clean_id}' not found", "code": "ERR-SESSION-NOT-FOUND"},
191
+ )
192
+
193
+
194
+ @router.post("/graph/cancel")
195
+ def cancel_workflow_endpoint(payload: Dict[str, Any], request: Request):
196
+ require_roles(request, {"owner", "admin", "editor"}, allow_local_solo=True)
197
+ run_id = payload.get("run_id") or payload.get("runId")
198
+ clean_run_id = str(run_id or "").strip()
199
+ if not clean_run_id or not RUN_ID_PATTERN.match(clean_run_id):
200
+ raise HTTPException(status_code=400, detail={"error": "Missing or invalid run_id", "code": "ERR-INVALID-RUN-ID"})
201
+ run_exists = bool(
202
+ GLOBAL_EVENT_STORE.get_run_metadata(clean_run_id)
203
+ or GLOBAL_SESSION_REGISTRY.get(clean_run_id)
204
+ or GLOBAL_ACTIVE_RUNS.get(clean_run_id)
205
+ )
206
+ if run_exists or is_auth_required():
207
+ verify_run_access(clean_run_id, request)
208
+ node_results = payload.get("node_results") or payload.get("nodeResults") or {}
209
+ return scrub_result_secrets(cancel_workflow_logic(run_id=clean_run_id, node_results=node_results))
app/routers/health.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Health & Readiness Probes Router
3
+ Provides root service info, health checks, liveness, and readiness endpoints without leaking credentials.
4
+ """
5
+
6
+ import os
7
+ import time
8
+ from fastapi import APIRouter, Request, Response, status
9
+ from database.mongo import workflow_repo
10
+
11
+ router = APIRouter(tags=["Health & Probes"])
12
+
13
+
14
+ @router.get("/")
15
+ @router.get("/ping")
16
+ def root():
17
+ return {
18
+ "service": "ALLI — Workflow Logic Backend Engine",
19
+ "app_name": "Workflow Logic",
20
+ "studio_name": "Logic Studio",
21
+ "status": "online",
22
+ "version": "3.0.0",
23
+ "architecture": "Modular FastAPI + Database-First + Commercial RBAC",
24
+ "docs": "/docs",
25
+ "redoc": "/redoc",
26
+ "database": {
27
+ "type": "mongodb",
28
+ "connected": workflow_repo.is_connected,
29
+ },
30
+ "endpoints": [
31
+ "/health",
32
+ "/health/live",
33
+ "/health/ready",
34
+ "/studio/bootstrap",
35
+ "/orgs",
36
+ "/validate",
37
+ "/compile",
38
+ "/codegen",
39
+ "/export",
40
+ "/introspect",
41
+ "/run",
42
+ "/workflows",
43
+ "/templates",
44
+ "/workflow-logic",
45
+ "/node-schemas",
46
+ "/packages",
47
+ "/fundamentals",
48
+ "/learning",
49
+ "/blog",
50
+ "/validation-lab/report",
51
+ ],
52
+ }
53
+
54
+
55
+ @router.get("/health")
56
+ def health(request: Request):
57
+ db_status = "connected" if workflow_repo.is_connected else "disconnected"
58
+ supa_configured = getattr(request.app.state, "supabase_client", None) is not None
59
+ return {
60
+ "status": "healthy",
61
+ "service": "alli_backend",
62
+ "app_name": "Workflow Logic",
63
+ "studio_name": "Logic Studio",
64
+ "version": "3.0.0",
65
+ "database": db_status,
66
+ "runtime": "python-3.14",
67
+ "database_info": {
68
+ "type": "mongodb",
69
+ "connected": workflow_repo.is_connected,
70
+ },
71
+ "supabase": {
72
+ "configured": supa_configured,
73
+ },
74
+ }
75
+
76
+
77
+ @router.get("/health/live")
78
+ def health_live():
79
+ """Liveness probe: verifies FastAPI event loop is responsive."""
80
+ return {
81
+ "status": "alive",
82
+ "service": "alli_backend",
83
+ "app_name": "Workflow Logic",
84
+ "studio_name": "Logic Studio",
85
+ "timestamp": time.time(),
86
+ }
87
+
88
+
89
+ @router.get("/health/ready")
90
+ def health_ready(request: Request, response: Response):
91
+ """
92
+ Readiness probe: verifies critical dependencies (MongoDB & Auth) are reachable.
93
+ Returns 200 when ready or 503 when unavailable.
94
+ """
95
+ db_ready = bool(workflow_repo.is_connected)
96
+ supa_client = getattr(request.app.state, "supabase_client", None)
97
+ auth_ready = bool(supa_client is not None or os.environ.get("ALLI_AUTH_READY") == "true")
98
+
99
+ is_ready = db_ready and auth_ready
100
+ if not is_ready:
101
+ response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
102
+ return {
103
+ "status": "not_ready",
104
+ "service": "alli_backend",
105
+ "app_name": "Workflow Logic",
106
+ "studio_name": "Logic Studio",
107
+ "dependencies": {
108
+ "database": "ready" if db_ready else "unavailable",
109
+ "auth": "ready" if auth_ready else "unavailable",
110
+ },
111
+ }
112
+ return {
113
+ "status": "ready",
114
+ "service": "alli_backend",
115
+ "app_name": "Workflow Logic",
116
+ "studio_name": "Logic Studio",
117
+ "dependencies": {
118
+ "database": "ready",
119
+ "auth": "ready",
120
+ },
121
+ }
app/routers/introspection.py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Package Introspection & Node Schema Staging Router
3
+ Enforces Editor+ RBAC for package introspection and strict Owner/Admin RBAC for promote/reject.
4
+ """
5
+
6
+ from typing import Any, Dict, Optional
7
+ from fastapi import APIRouter, HTTPException, Request
8
+ from pydantic import BaseModel, Field
9
+
10
+ from database.mongo import workflow_repo
11
+ from lambdas.inspect_handler import introspect_package_logic
12
+ from language_core.node_schema import CANONICAL_NODE_TYPES, default_registry
13
+ from library_inspector.node_synthesizer import global_staging_registry
14
+ from security.rbac import audit_action, require_roles
15
+
16
+ router = APIRouter(tags=["Introspection & Node Schemas"])
17
+
18
+
19
+ class IntrospectRequest(BaseModel):
20
+ package: str = Field(..., description="Target Python package name")
21
+ version: Optional[str] = Field(None, description="Optional target package version")
22
+
23
+
24
+ class PromoteStagedNodeRequest(BaseModel):
25
+ node_type: str = Field(..., description="Target staged node type to promote")
26
+ promoted_by: Optional[str] = Field("admin", description="Identity or role promoting the node")
27
+
28
+
29
+ class RejectStagedNodeRequest(BaseModel):
30
+ node_type: str = Field(..., description="Target staged node type to reject")
31
+ reason: Optional[str] = Field("Security or schema rejection", description="Audit rationale for rejection")
32
+
33
+
34
+ class NodeExecuteRequest(BaseModel):
35
+ node_type: str
36
+ inputs: Dict[str, Any] = Field(default_factory=dict)
37
+ parameters: Dict[str, Any] = Field(default_factory=dict)
38
+
39
+
40
+ @router.post("/introspect")
41
+ @router.post("/inspect")
42
+ def introspect(payload: IntrospectRequest, request: Request):
43
+ """Download/introspect package and return visual JSON blueprint (Owner/Admin/Editor only)."""
44
+ require_roles(
45
+ request,
46
+ {"owner", "admin", "editor"},
47
+ allow_local_solo=True,
48
+ action="library.introspect",
49
+ resource_type="package",
50
+ resource_id=payload.package,
51
+ )
52
+ try:
53
+ res = introspect_package_logic(payload.package, version=payload.version)
54
+ audit_action(request, workflow_repo, "library.introspect", "package", payload.package, 200)
55
+ return res
56
+ except ValueError as e:
57
+ raise HTTPException(status_code=400, detail=str(e))
58
+ except Exception as e:
59
+ raise HTTPException(status_code=500, detail=f"Introspection failed: {str(e)}")
60
+
61
+
62
+ @router.get("/node-schemas")
63
+ def get_node_schemas():
64
+ """Return all canonical node schemas."""
65
+ schemas = default_registry.to_json_dict()
66
+ return {"status": "success", "count": len(schemas), "schemas": schemas}
67
+
68
+
69
+ @router.get("/node-schemas/staged")
70
+ def get_staged_node_schemas():
71
+ """Return all staged node schemas with provenance and security warnings."""
72
+ records = global_staging_registry.list_records()
73
+ db_records = workflow_repo.list_staged_node_records()
74
+ seen = {r.get("node_type") for r in records if isinstance(r, dict)}
75
+ for rec in db_records:
76
+ if isinstance(rec, dict) and rec.get("node_type") not in seen:
77
+ records.append(rec)
78
+ return {"status": "success", "count": len(records), "staged_nodes": records}
79
+
80
+
81
+ @router.get("/node-schemas/staged/{node_type:path}")
82
+ def get_staged_node_schema_by_type(node_type: str):
83
+ rec = global_staging_registry.get_record(node_type)
84
+ if rec:
85
+ return rec.to_dict()
86
+ for raw_rec in workflow_repo.list_staged_node_records():
87
+ if isinstance(raw_rec, dict) and raw_rec.get("node_type") == node_type:
88
+ restored = global_staging_registry.restore_record(raw_rec)
89
+ return restored.to_dict() if restored else raw_rec
90
+ raise HTTPException(status_code=404, detail=f"Staged node schema '{node_type}' not found.")
91
+
92
+
93
+ @router.post("/node-schemas/staged/promote")
94
+ def promote_staged_node_endpoint(req: PromoteStagedNodeRequest, request: Request):
95
+ """Promote an introspected staged node into production registry (Owner/Admin only)."""
96
+ ctx = require_roles(
97
+ request,
98
+ {"owner", "admin"},
99
+ allow_local_solo=True,
100
+ action="node.staged.promote",
101
+ resource_type="node_schema",
102
+ resource_id=req.node_type,
103
+ )
104
+ promoter = ctx.get("email") or ctx.get("user_id") or req.promoted_by or "admin"
105
+ success, message = global_staging_registry.promote_to_target(
106
+ node_type=req.node_type,
107
+ target_registry=default_registry,
108
+ promoted_by=promoter,
109
+ )
110
+ if not success:
111
+ raise HTTPException(status_code=400, detail=message)
112
+
113
+ rec = global_staging_registry.get_record(req.node_type)
114
+ rec_dict = rec.to_dict() if rec else None
115
+ if rec_dict:
116
+ workflow_repo.save_staged_node_record(rec_dict)
117
+ if rec and rec.node:
118
+ workflow_repo.save_node_schema(rec.node.model_dump())
119
+ audit_action(request, workflow_repo, "node.staged.promote", "node_schema", req.node_type, 200)
120
+ return {"status": "success", "message": message, "record": rec_dict}
121
+
122
+
123
+ @router.post("/node-schemas/staged/reject")
124
+ def reject_staged_node_endpoint(req: RejectStagedNodeRequest, request: Request):
125
+ """Reject an introspected staged node, preventing execution (Owner/Admin only)."""
126
+ ctx = require_roles(
127
+ request,
128
+ {"owner", "admin"},
129
+ allow_local_solo=True,
130
+ action="node.staged.reject",
131
+ resource_type="node_schema",
132
+ resource_id=req.node_type,
133
+ )
134
+ author = ctx.get("email") or ctx.get("user_id") or "admin"
135
+ success, message = global_staging_registry.reject_candidate(
136
+ node_type=req.node_type,
137
+ rejected_by=author,
138
+ reason=req.reason or "Security or schema rejection",
139
+ target_registry=default_registry,
140
+ )
141
+ if not success:
142
+ raise HTTPException(status_code=400, detail=message)
143
+
144
+ rec = global_staging_registry.get_record(req.node_type)
145
+ rec_dict = rec.to_dict() if rec else None
146
+ if rec_dict:
147
+ workflow_repo.save_staged_node_record(rec_dict)
148
+ audit_action(request, workflow_repo, "node.staged.reject", "node_schema", req.node_type, 200, {"reason": req.reason})
149
+ return {"status": "success", "message": message, "record": rec_dict}
150
+
151
+
152
+ @router.get("/node-schemas/{node_type:path}")
153
+ def get_node_schema_by_type(node_type: str):
154
+ schema = default_registry.get(node_type)
155
+ if not schema:
156
+ raise HTTPException(status_code=404, detail=f"Node schema '{node_type}' not found.")
157
+ return schema.model_dump()
158
+
159
+
160
+ @router.post("/node-schemas/execute")
161
+ def execute_node_schema(req: NodeExecuteRequest, request: Request):
162
+ require_roles(
163
+ request,
164
+ {"owner", "admin", "editor"},
165
+ allow_local_solo=True,
166
+ action="node.execute",
167
+ resource_type="node_schema",
168
+ resource_id=req.node_type,
169
+ )
170
+ if req.node_type not in CANONICAL_NODE_TYPES and global_staging_registry.has(req.node_type):
171
+ if not global_staging_registry.is_approved(req.node_type):
172
+ raise HTTPException(
173
+ status_code=403,
174
+ detail=f"[ERR-UNAUTHORIZED-STAGED-NODE] Staged node '{req.node_type}' requires admin approval before execution.",
175
+ )
176
+ try:
177
+ output = default_registry.execute(req.node_type, req.inputs, req.parameters)
178
+ return {"status": "success", "node_type": req.node_type, "output": output}
179
+ except Exception as e:
180
+ raise HTTPException(status_code=400, detail=str(e))
app/routers/orgs.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Organizations, Memberships, RBAC & Audit Logs Router
3
+ Provides commercial multi-tenant workspace creation, seat-bounded member management,
4
+ plan controls, and immutable audit log querying.
5
+ """
6
+
7
+ from typing import Optional
8
+ from fastapi import APIRouter, HTTPException, Request, status
9
+ from pydantic import BaseModel, Field
10
+
11
+ from database.mongo import workflow_repo
12
+ from security.rbac import audit_action, require_roles
13
+
14
+ router = APIRouter(prefix="/orgs", tags=["Organizations & RBAC"])
15
+
16
+
17
+ class CreateOrgPayload(BaseModel):
18
+ name: str = Field(..., min_length=2, max_length=80)
19
+ slug: Optional[str] = None
20
+ plan_tier: str = Field("team", description="free | pro | team | enterprise")
21
+
22
+
23
+ class MemberInvitePayload(BaseModel):
24
+ user_id: str = Field(..., description="Supabase user UUID or member identifier")
25
+ email: Optional[str] = ""
26
+ role: str = Field("editor", description="owner | admin | editor | viewer")
27
+
28
+
29
+ class MemberRoleUpdatePayload(BaseModel):
30
+ role: str = Field(..., description="owner | admin | editor | viewer")
31
+
32
+
33
+ class OrgPlanUpdatePayload(BaseModel):
34
+ plan: str = Field(..., description="free | pro | team | enterprise")
35
+ billing_period: str = Field("monthly", description="monthly | yearly")
36
+
37
+
38
+ def _require_org_roles(request: Request, org_id: str, allowed_roles: set, action: str):
39
+ ctx = require_roles(
40
+ request,
41
+ allowed_roles=allowed_roles,
42
+ allow_local_solo=True,
43
+ action=action,
44
+ resource_type="organization",
45
+ resource_id=org_id,
46
+ )
47
+ uid = ctx.get("user_id")
48
+ if uid and not ctx.get("is_platform_admin"):
49
+ if org_id != ctx.get("org_id"):
50
+ mem = workflow_repo.get_org_membership(org_id, uid)
51
+ role = (mem or {}).get("role")
52
+ if not mem or mem.get("status") != "active":
53
+ raise HTTPException(status_code=403, detail="Forbidden: Not a member of this organization.")
54
+ if role not in allowed_roles:
55
+ raise HTTPException(status_code=403, detail=f"Forbidden: Role '{role}' cannot perform '{action}'.")
56
+ return {**ctx, "org_id": org_id, "role": role}
57
+ return ctx
58
+
59
+
60
+ @router.get("")
61
+ def list_organizations(request: Request):
62
+ """List workspaces available to the current caller."""
63
+ ctx = require_roles(request, {"owner", "admin", "editor", "viewer"}, allow_local_solo=True)
64
+ uid = ctx.get("user_id")
65
+ if not uid:
66
+ return {"organizations": [{**workflow_repo.get_organization("local_personal"), "role": "owner"}], "active_org_id": "local_personal"}
67
+ orgs = workflow_repo.list_user_organizations(uid, email=ctx.get("email"))
68
+ return {"organizations": orgs, "active_org_id": ctx.get("org_id")}
69
+
70
+
71
+ @router.post("", status_code=status.HTTP_201_CREATED)
72
+ def create_organization(payload: CreateOrgPayload, request: Request):
73
+ """Create a new commercial team workspace owned by the caller."""
74
+ ctx = require_roles(
75
+ request,
76
+ {"owner", "admin", "editor", "viewer"},
77
+ allow_local_solo=True,
78
+ action="org.create",
79
+ resource_type="organization",
80
+ )
81
+ owner_uid = ctx.get("user_id") or "local_solo"
82
+ org = workflow_repo.create_organization(
83
+ owner_user_id=owner_uid,
84
+ name=payload.name,
85
+ slug=payload.slug,
86
+ plan_tier=payload.plan_tier,
87
+ owner_email=ctx.get("email"),
88
+ )
89
+ audit_action(request, workflow_repo, "org.create", "organization", org["org_id"], 201, {"plan_tier": org["plan_tier"]})
90
+ return {"organization": {**org, "role": "owner"}}
91
+
92
+
93
+ @router.get("/{org_id}")
94
+ def get_organization(org_id: str, request: Request):
95
+ """Retrieve workspace details for active member."""
96
+ _require_org_roles(request, org_id, {"owner", "admin", "editor", "viewer"}, "org.read")
97
+ org = workflow_repo.get_organization(org_id)
98
+ if not org:
99
+ raise HTTPException(status_code=404, detail="Organization not found.")
100
+ return {"organization": org}
101
+
102
+
103
+ @router.get("/{org_id}/members")
104
+ def list_org_members(org_id: str, request: Request):
105
+ """List members and roles within an organization."""
106
+ _require_org_roles(request, org_id, {"owner", "admin", "editor", "viewer"}, "org.members.list")
107
+ members = workflow_repo.list_org_members(org_id)
108
+ return {"org_id": org_id, "members": members}
109
+
110
+
111
+ @router.post("/{org_id}/members", status_code=status.HTTP_201_CREATED)
112
+ def add_org_member(org_id: str, payload: MemberInvitePayload, request: Request):
113
+ """Invite or add a member with RBAC role enforcement and seat limit validation."""
114
+ ctx = _require_org_roles(request, org_id, {"owner", "admin"}, "org.members.add")
115
+ if payload.role.lower().strip() == "owner" and ctx.get("role") != "owner" and not ctx.get("is_platform_admin"):
116
+ raise HTTPException(status_code=403, detail="Forbidden: Only workspace owners can assign owner role.")
117
+ try:
118
+ member = workflow_repo.upsert_org_member(
119
+ org_id=org_id,
120
+ user_id=payload.user_id,
121
+ email=payload.email or "",
122
+ role=payload.role,
123
+ invited_by=ctx.get("user_id") or "local_solo",
124
+ )
125
+ except ValueError as e:
126
+ raise HTTPException(status_code=400, detail=str(e))
127
+ audit_action(
128
+ request, workflow_repo, "org.member.add", "org_membership", payload.user_id, 201, {"role": member["role"]}
129
+ )
130
+ return {"member": member}
131
+
132
+
133
+ @router.patch("/{org_id}/members/{member_user_id}")
134
+ def update_org_member_role(org_id: str, member_user_id: str, payload: MemberRoleUpdatePayload, request: Request):
135
+ """Update member RBAC role (Owner/Admin only; Admins cannot grant or modify Owner)."""
136
+ ctx = _require_org_roles(request, org_id, {"owner", "admin"}, "org.members.update")
137
+ target = workflow_repo.get_org_membership(org_id, member_user_id)
138
+ if not target:
139
+ raise HTTPException(status_code=404, detail="Member not found in workspace.")
140
+ if (
141
+ (payload.role.lower().strip() == "owner" or target.get("role") == "owner")
142
+ and ctx.get("role") != "owner"
143
+ and not ctx.get("is_platform_admin")
144
+ ):
145
+ raise HTTPException(status_code=403, detail="Forbidden: Only workspace owners can modify owner assignments.")
146
+ try:
147
+ updated = workflow_repo.upsert_org_member(
148
+ org_id=org_id,
149
+ user_id=member_user_id,
150
+ email=target.get("email", ""),
151
+ role=payload.role,
152
+ invited_by=ctx.get("user_id") or "local_solo",
153
+ enforce_seat_limit=False,
154
+ )
155
+ except ValueError as e:
156
+ raise HTTPException(status_code=400, detail=str(e))
157
+ audit_action(
158
+ request, workflow_repo, "org.member.role_update", "org_membership", member_user_id, 200, {"role": updated["role"]}
159
+ )
160
+ return {"member": updated}
161
+
162
+
163
+ @router.delete("/{org_id}/members/{member_user_id}")
164
+ def remove_org_member(org_id: str, member_user_id: str, request: Request):
165
+ """Remove a member from an organization while protecting the last owner."""
166
+ ctx = _require_org_roles(request, org_id, {"owner", "admin"}, "org.members.remove")
167
+ target = workflow_repo.get_org_membership(org_id, member_user_id)
168
+ if not target:
169
+ raise HTTPException(status_code=404, detail="Member not found.")
170
+ if target.get("role") == "owner":
171
+ if ctx.get("role") != "owner" and not ctx.get("is_platform_admin"):
172
+ raise HTTPException(status_code=403, detail="Forbidden: Admins cannot remove an owner.")
173
+ owners = [m for m in workflow_repo.list_org_members(org_id) if m.get("role") == "owner"]
174
+ if len(owners) <= 1:
175
+ raise HTTPException(status_code=400, detail="Cannot remove the sole owner of a workspace.")
176
+ workflow_repo.remove_org_member(org_id, member_user_id)
177
+ audit_action(request, workflow_repo, "org.member.remove", "org_membership", member_user_id, 200)
178
+ return {"success": True, "user_id": member_user_id}
179
+
180
+
181
+ @router.post("/{org_id}/plan")
182
+ def update_org_plan(org_id: str, payload: OrgPlanUpdatePayload, request: Request):
183
+ """Update organization subscription tier (Owner only)."""
184
+ _require_org_roles(request, org_id, {"owner"}, "org.plan.update")
185
+ try:
186
+ updated = workflow_repo.update_org_plan(org_id, payload.plan, payload.billing_period)
187
+ except ValueError as e:
188
+ raise HTTPException(status_code=400, detail=str(e))
189
+ if not updated:
190
+ raise HTTPException(status_code=404, detail="Organization not found.")
191
+ audit_action(request, workflow_repo, "org.plan.update", "organization", org_id, 200, {"plan": payload.plan})
192
+ return {"success": True, "organization": updated}
193
+
194
+
195
+ @router.get("/{org_id}/audit-logs")
196
+ def get_org_audit_logs(org_id: str, request: Request, limit: int = 100):
197
+ """Retrieve RBAC & mutation audit logs for an organization (Owner/Admin only)."""
198
+ _require_org_roles(request, org_id, {"owner", "admin"}, "org.audit.read")
199
+ logs = workflow_repo.list_audit_logs(org_id, limit=limit)
200
+ return {"org_id": org_id, "count": len(logs), "logs": logs}
app/routers/packages_ast.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Packages & AST Inspector Router
3
+ Serves DB-backed package blueprints, PyPI version discovery, and Python AST inspection.
4
+ """
5
+
6
+ import ast
7
+ import json
8
+ import re
9
+ import sys
10
+ import urllib.error
11
+ import urllib.request
12
+ from typing import Any, Dict, Optional
13
+ import packaging.version
14
+ from fastapi import APIRouter, HTTPException
15
+ from pydantic import BaseModel
16
+
17
+ from database.mongo import workflow_repo
18
+
19
+ router = APIRouter(tags=["Packages & AST Inspector"])
20
+
21
+ STDLIB_PACKAGE_NAMES = {
22
+ "math", "json", "sys", "os", "statistics", "datetime", "re",
23
+ "collections", "itertools", "functools", "random", "time", "pathlib", "ast", "typing",
24
+ }
25
+
26
+
27
+ class ASTInspectRequest(BaseModel):
28
+ source_code: str
29
+ target_graph: Optional[Dict[str, Any]] = None
30
+
31
+
32
+ class AlliASTVisitor(ast.NodeVisitor):
33
+ def __init__(self):
34
+ self.functions = []
35
+ self.classes = []
36
+ self.imports = []
37
+ self.calls = []
38
+ self.diagnostics = []
39
+ self.control_flow = {"has_for": False, "has_while": False, "has_if": False, "has_try": False, "has_with": False}
40
+
41
+ def visit_FunctionDef(self, node):
42
+ self.functions.append({"name": node.name, "args": [a.arg for a in node.args.args], "line": node.lineno})
43
+ self.generic_visit(node)
44
+
45
+ def visit_AsyncFunctionDef(self, node):
46
+ self.functions.append({"name": node.name, "args": [a.arg for a in node.args.args], "line": node.lineno})
47
+ self.generic_visit(node)
48
+
49
+ def visit_ClassDef(self, node):
50
+ bases = [b.id if isinstance(b, ast.Name) else getattr(b, "attr", "") for b in node.bases]
51
+ methods = [n.name for n in node.body if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))]
52
+ self.classes.append({"name": node.name, "bases": bases, "methods": methods, "line": node.lineno})
53
+ self.generic_visit(node)
54
+
55
+ def visit_Import(self, node):
56
+ for alias in node.names:
57
+ self.imports.append({"module": alias.name, "asname": alias.asname, "line": node.lineno})
58
+ self.generic_visit(node)
59
+
60
+ def visit_ImportFrom(self, node):
61
+ mod = node.module or ""
62
+ for alias in node.names:
63
+ self.imports.append({"module": f"{mod}.{alias.name}" if mod else alias.name, "asname": alias.asname, "line": node.lineno})
64
+ self.generic_visit(node)
65
+
66
+ def visit_Call(self, node):
67
+ call_name = ""
68
+ if isinstance(node.func, ast.Name):
69
+ call_name = node.func.id
70
+ elif isinstance(node.func, ast.Attribute):
71
+ val_id = getattr(node.func.value, "id", "")
72
+ call_name = f"{val_id}.{node.func.attr}" if val_id else node.func.attr
73
+ if call_name:
74
+ self.calls.append({"callable": call_name, "line": node.lineno})
75
+ if call_name in ("eval", "exec", "__import__"):
76
+ self.diagnostics.append({
77
+ "line": node.lineno,
78
+ "severity": "error",
79
+ "message": f"Prohibited dynamic evaluation '{call_name}' detected in AST.",
80
+ "rule_id": f"AST-SEC-{call_name.upper().strip('_')}",
81
+ })
82
+ self.generic_visit(node)
83
+
84
+ def visit_For(self, node):
85
+ self.control_flow["has_for"] = True
86
+ self.generic_visit(node)
87
+
88
+ def visit_While(self, node):
89
+ self.control_flow["has_while"] = True
90
+ self.generic_visit(node)
91
+
92
+ def visit_If(self, node):
93
+ self.control_flow["has_if"] = True
94
+ self.generic_visit(node)
95
+
96
+ def visit_Try(self, node):
97
+ self.control_flow["has_try"] = True
98
+ self.generic_visit(node)
99
+
100
+ def visit_With(self, node):
101
+ self.control_flow["has_with"] = True
102
+ self.generic_visit(node)
103
+
104
+
105
+ @router.get("/packages")
106
+ def list_packages():
107
+ """Return extracted library blueprints from MongoDB Atlas + seeded packages."""
108
+ return workflow_repo.list_blueprints()
109
+
110
+
111
+ @router.get("/packages/{package_name}/releases")
112
+ def get_package_releases(package_name: str):
113
+ """Query PyPI JSON API and return available release versions sorted descending."""
114
+ clean_pkg = re.sub(r"[^a-zA-Z0-9_\-\.]", "", package_name.strip().lower())
115
+ if not clean_pkg:
116
+ raise HTTPException(status_code=400, detail="Package name cannot be empty")
117
+
118
+ url = f"https://pypi.org/pypi/{clean_pkg}/json"
119
+ req = urllib.request.Request(url, headers={"User-Agent": "ALLI-Inspector/1.0", "Accept": "application/json"})
120
+
121
+ try:
122
+ with urllib.request.urlopen(req, timeout=5.0) as resp:
123
+ if resp.status != 200:
124
+ raise HTTPException(status_code=resp.status, detail=f"PyPI returned status {resp.status}")
125
+ data = json.loads(resp.read().decode("utf-8"))
126
+ except urllib.error.HTTPError as e:
127
+ if e.code == 404:
128
+ if clean_pkg in STDLIB_PACKAGE_NAMES:
129
+ curr_ver = sys.version.split()[0]
130
+ return {"package": clean_pkg, "latest": curr_ver, "releases": [curr_ver]}
131
+ raise HTTPException(status_code=404, detail=f"Package '{clean_pkg}' not found on PyPI")
132
+ raise HTTPException(status_code=502, detail=f"PyPI API HTTP error: {e.code} {e.reason}")
133
+ except (urllib.error.URLError, TimeoutError):
134
+ if clean_pkg in STDLIB_PACKAGE_NAMES:
135
+ curr_ver = sys.version.split()[0]
136
+ return {"package": clean_pkg, "latest": curr_ver, "releases": [curr_ver]}
137
+ raise HTTPException(status_code=504, detail=f"PyPI connection timed out for package '{clean_pkg}'")
138
+ except Exception as e:
139
+ raise HTTPException(status_code=500, detail=f"Failed to query package releases: {str(e)}")
140
+
141
+ latest = data.get("info", {}).get("version", "unknown")
142
+ valid_versions, other_versions = [], []
143
+ for r in data.get("releases", {}).keys():
144
+ try:
145
+ valid_versions.append((packaging.version.parse(r), r))
146
+ except Exception:
147
+ other_versions.append(r)
148
+ valid_versions.sort(key=lambda x: x[0], reverse=True)
149
+ sorted_releases = [r for _, r in valid_versions] + sorted(other_versions, reverse=True)
150
+ return {"package": clean_pkg, "latest": latest, "releases": sorted_releases}
151
+
152
+
153
+ @router.post("/ast/inspect")
154
+ def inspect_ast_endpoint(req: ASTInspectRequest):
155
+ """Parse Python source code into an Abstract Syntax Tree and return structured diagnostics."""
156
+ try:
157
+ tree = ast.parse(req.source_code)
158
+ except SyntaxError as e:
159
+ return {
160
+ "valid": False,
161
+ "syntax_error": {"line": e.lineno or 1, "column": e.offset or 1, "message": e.msg},
162
+ "diagnostics": [{"line": e.lineno or 1, "severity": "error", "message": f"Python SyntaxError: {e.msg}", "rule_id": "AST-SYNTAX-ERROR"}],
163
+ "functions": [], "classes": [], "imports": [], "calls": [], "control_flow": {},
164
+ "graph_parity": {"matches_graph": False, "missing_nodes": []},
165
+ }
166
+
167
+ visitor = AlliASTVisitor()
168
+ visitor.visit(tree)
169
+ missing_nodes = []
170
+ if req.target_graph and "nodes" in req.target_graph:
171
+ call_names = {c["callable"] for c in visitor.calls}
172
+ for n in req.target_graph["nodes"]:
173
+ ntype = n.get("node_type") or n.get("type", "")
174
+ if ntype and not ntype.startswith("io.input") and not ntype.startswith("alli.primitive.constant"):
175
+ fn_name = ntype.split(".")[-1]
176
+ if not (fn_name in call_names or ntype in call_names or fn_name in req.source_code or n.get("id", "") in req.source_code):
177
+ missing_nodes.append(f"{n.get('id')} ({ntype})")
178
+
179
+ has_errors = any(d["severity"] == "error" for d in visitor.diagnostics)
180
+ return {
181
+ "valid": not has_errors,
182
+ "diagnostics": visitor.diagnostics,
183
+ "functions": visitor.functions,
184
+ "classes": visitor.classes,
185
+ "imports": visitor.imports,
186
+ "calls": visitor.calls,
187
+ "control_flow": visitor.control_flow,
188
+ "graph_parity": {"matches_graph": len(missing_nodes) == 0, "missing_nodes": missing_nodes},
189
+ }
app/routers/telemetry.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Runtime Telemetry, Events & SSE Streaming Router
3
+ Provides run access verification, cursor-paginated events, feedback snapshots, and SSE streaming.
4
+ """
5
+
6
+ import asyncio
7
+ import json
8
+ import re
9
+ from typing import Any, Dict, Optional
10
+ from fastapi import APIRouter, HTTPException, Request
11
+ from fastapi.responses import StreamingResponse
12
+
13
+ from language_core.execution_session import GLOBAL_SESSION_REGISTRY
14
+ from runtime_environment.active_runs import GLOBAL_ACTIVE_RUNS
15
+ from language_core.runtime_events import GLOBAL_EVENT_STORE, redact_sensitive_data, redact_sensitive_text
16
+ from security.rbac import is_auth_required, is_local_loopback_request
17
+
18
+ router = APIRouter(tags=["Runtime Telemetry"])
19
+ RUN_ID_PATTERN = re.compile(r"^[a-zA-Z0-9_\-\.:]{1,128}$")
20
+
21
+
22
+ def verify_run_access(run_id: str, request: Optional[Request] = None) -> Dict[str, Any]:
23
+ """Enforce tenant isolation and BOLA/IDOR protection on execution telemetry runs."""
24
+ clean_id = str(run_id).strip()
25
+ if not clean_id or not RUN_ID_PATTERN.match(clean_id):
26
+ raise HTTPException(
27
+ status_code=400,
28
+ detail={"error": "Malformed or invalid run_id identifier", "code": "ERR-INVALID-RUN-ID"},
29
+ )
30
+
31
+ meta = GLOBAL_EVENT_STORE.get_run_metadata(clean_id)
32
+ if not meta:
33
+ if not GLOBAL_SESSION_REGISTRY.get(clean_id) and not GLOBAL_ACTIVE_RUNS.get(clean_id):
34
+ raise HTTPException(
35
+ status_code=404,
36
+ detail={"error": f"Execution telemetry for run '{clean_id}' not found", "code": "ERR-RUN-NOT-FOUND"},
37
+ )
38
+ return {"run_id": clean_id, "graph_revision": "", "lifecycle_status": "idle"}
39
+
40
+ owner_id = meta.get("owner_user_id")
41
+ if request:
42
+ auth_user = getattr(getattr(request, "state", None), "user", None)
43
+ if not auth_user and not is_auth_required() and is_local_loopback_request(request) and request.headers.get("x-user-id"):
44
+ auth_user = {
45
+ "id": request.headers.get("x-user-id"),
46
+ "role": request.headers.get("x-user-role", "authenticated"),
47
+ }
48
+ if auth_user:
49
+ cur_uid = auth_user.get("id")
50
+ role = auth_user.get("role")
51
+ if owner_id and role != "admin" and cur_uid != "service_account" and str(cur_uid) != str(owner_id):
52
+ raise HTTPException(
53
+ status_code=403,
54
+ detail={"error": "Forbidden: Cannot access execution telemetry of another tenant.", "code": "ERR-FORBIDDEN-SESSION"},
55
+ )
56
+ if not owner_id and is_auth_required() and role != "admin" and cur_uid != "service_account":
57
+ raise HTTPException(
58
+ status_code=403,
59
+ detail={"error": "Forbidden: Run ownership metadata unavailable for tenant isolation.", "code": "ERR-FORBIDDEN-SESSION"},
60
+ )
61
+ elif is_auth_required():
62
+ raise HTTPException(
63
+ status_code=401,
64
+ detail={"error": "Authentication required to access tenant execution run.", "code": "ERR-UNAUTHORIZED"},
65
+ )
66
+ return meta
67
+
68
+
69
+ @router.get("/graph/events/{run_id}")
70
+ def get_workflow_events(run_id: str, cursor: int = 0, since_seq: int = 0, limit: int = 100, request: Request = None):
71
+ meta = verify_run_access(run_id, request)
72
+ clean_id = str(run_id).strip()
73
+ start_seq = max(cursor, since_seq)
74
+ events_res = GLOBAL_EVENT_STORE.get_events(clean_id, since_seq=start_seq, limit=min(max(1, limit), 500))
75
+ events, next_cursor, has_more = events_res
76
+ res_dict = {
77
+ "run_id": clean_id,
78
+ "graph_revision": meta.get("graph_revision", "") if meta else "",
79
+ "lifecycle_status": meta.get("lifecycle_status", "idle") if meta else "idle",
80
+ "events": [e.model_dump() if hasattr(e, "model_dump") else e.dict() for e in events],
81
+ "cursor": next_cursor,
82
+ "has_more": has_more,
83
+ "events_truncated": events_res.events_truncated,
84
+ "oldest_retained_seq": events_res.oldest_retained_seq,
85
+ "is_complete": meta.get("is_complete", False) if meta else False,
86
+ }
87
+ if events_res.events_truncated:
88
+ res_dict["warning"] = (
89
+ f"Client cursor {start_seq} is behind oldest event {events_res.oldest_retained_seq}. "
90
+ "Earlier events were truncated by ring buffer."
91
+ )
92
+ return res_dict
93
+
94
+
95
+ @router.get("/graph/feedback/{run_id}")
96
+ def get_workflow_feedback_snapshot(run_id: str, request: Request = None):
97
+ verify_run_access(run_id, request)
98
+ clean_id = str(run_id).strip()
99
+ snapshot = GLOBAL_EVENT_STORE.get_snapshot(clean_id)
100
+ if not snapshot:
101
+ session = GLOBAL_SESSION_REGISTRY.get(clean_id)
102
+ if session:
103
+ s_dict = session.to_dict()
104
+ return {
105
+ "run_id": clean_id,
106
+ "graph_revision": s_dict.get("graph_revision", ""),
107
+ "lifecycle_status": s_dict.get("lifecycle_status", "idle"),
108
+ "events": [],
109
+ "node_states": redact_sensitive_data(s_dict.get("node_results", {})),
110
+ "stdout": redact_sensitive_text(s_dict.get("stdout", "")),
111
+ "stderr": redact_sensitive_text(s_dict.get("stderr", "")),
112
+ "diagnostics": [],
113
+ "duration_ms": 0.0,
114
+ "cursor": 0,
115
+ "has_more": False,
116
+ "is_complete": s_dict.get("is_workflow_complete", False),
117
+ }
118
+ raise HTTPException(
119
+ status_code=404,
120
+ detail={"error": f"No execution telemetry found for run '{clean_id}'", "code": "ERR-RUN-NOT-FOUND"},
121
+ )
122
+ session = GLOBAL_SESSION_REGISTRY.get(clean_id)
123
+ snapshot["node_states"] = redact_sensitive_data(session.node_results if session else {})
124
+ snapshot["stdout"] = redact_sensitive_text(session.to_dict().get("stdout", "") if session else "")
125
+ snapshot["stderr"] = redact_sensitive_text(session.to_dict().get("stderr", "") if session else "")
126
+ return snapshot
127
+
128
+
129
+ @router.get("/graph/events/{run_id}/stream")
130
+ async def stream_workflow_events(run_id: str, request: Request, cursor: Optional[int] = None, since_seq: Optional[int] = None):
131
+ clean_id = str(run_id).strip()
132
+ verify_run_access(clean_id, request)
133
+ last_id = request.headers.get("last-event-id")
134
+ start_cursor = int(last_id) if (last_id and last_id.isdigit()) else max(0, cursor or since_seq or 0)
135
+
136
+ async def event_generator():
137
+ curr_seq, polls, first_tick = start_cursor, 0, True
138
+ while polls < 120:
139
+ if await request.is_disconnected():
140
+ break
141
+ events_res = GLOBAL_EVENT_STORE.get_events(clean_id, since_seq=curr_seq, limit=50)
142
+ events, next_seq, _ = events_res
143
+ if first_tick and events_res.events_truncated:
144
+ gap = json.dumps({"event_type": "cursor_gap", "run_id": clean_id, "requested_cursor": curr_seq, "oldest_retained_seq": events_res.oldest_retained_seq})
145
+ yield f"event: cursor_gap\ndata: {gap}\n\n"
146
+ first_tick = False
147
+ for ev in events:
148
+ ev_data = ev.model_dump() if hasattr(ev, "model_dump") else ev.dict()
149
+ yield f"id: {ev.seq}\nevent: {ev.event_type}\ndata: {json.dumps(ev_data)}\n\n"
150
+ curr_seq = next_seq
151
+ run_meta = GLOBAL_EVENT_STORE.get_run_metadata(clean_id) or {}
152
+ if run_meta.get("is_complete") and not events:
153
+ end_msg = json.dumps({"event_type": "stream_end", "run_id": clean_id, "lifecycle_status": run_meta.get("lifecycle_status", "success"), "final_seq": curr_seq})
154
+ yield f"event: stream_end\ndata: {end_msg}\n\n"
155
+ break
156
+ await asyncio.sleep(0.5)
157
+ polls += 1
158
+
159
+ return StreamingResponse(event_generator(), media_type="text/event-stream")
app/routers/templates.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Templates Catalog Router
3
+ Exposes /templates and /workflow-logic aliases backed by MongoDB Atlas with Editor+ RBAC on publish.
4
+ """
5
+
6
+ from typing import Any, Dict, List, Optional
7
+ from fastapi import APIRouter, HTTPException, Request
8
+ from pydantic import BaseModel, Field
9
+
10
+ from database.mongo import workflow_repo
11
+ from security.rbac import audit_action, require_roles
12
+
13
+ router = APIRouter(tags=["Templates Catalog"])
14
+
15
+
16
+ class TemplatePublishPayload(BaseModel):
17
+ id: Optional[str] = None
18
+ name: str = Field(..., description="Template display name")
19
+ description: str = Field(..., description="Template description")
20
+ category: str = Field("math-logic", description="Template category")
21
+ tags: List[str] = Field(default_factory=list, description="Keywords/tags")
22
+ author_name: Optional[str] = "John Doe"
23
+ author_id: Optional[str] = None
24
+ source_project_id: Optional[str] = None
25
+ bannerUrl: Optional[str] = None
26
+ downloads: Optional[int] = 0
27
+ nodeCount: Optional[int] = None
28
+ details: Optional[Dict[str, Any]] = None
29
+ graph: Optional[Dict[str, Any]] = Field(default_factory=dict)
30
+ is_published: bool = True
31
+ is_global: bool = True
32
+
33
+
34
+ class TemplateUsePayload(BaseModel):
35
+ user_id: Optional[str] = None
36
+ userId: Optional[str] = None
37
+
38
+
39
+ @router.get("/templates")
40
+ @router.get("/workflow-logic")
41
+ def list_templates_endpoint(user_id: Optional[str] = None, starter: Optional[bool] = None):
42
+ """List published global templates from MongoDB Atlas with dynamic user star hydration."""
43
+ all_tmpls = workflow_repo.list_templates()
44
+
45
+ starred_set = set()
46
+ if user_id:
47
+ prof = workflow_repo.get_user_profile(user_id)
48
+ if prof and "starred_templates" in prof:
49
+ starred_set = set(prof["starred_templates"])
50
+
51
+ enriched = []
52
+ for t in all_tmpls:
53
+ t_copy = dict(t)
54
+ t_id = t_copy.get("id", "")
55
+ t_copy["isStarred"] = t_id in starred_set or bool(t_copy.get("is_starred", False))
56
+ t_copy["is_starred"] = t_copy["isStarred"]
57
+ t_copy["starsCount"] = t_copy.get("stars_count") or t_copy.get("starsCount") or 0
58
+ t_copy["stars_count"] = t_copy["starsCount"]
59
+ enriched.append(t_copy)
60
+
61
+ if starter:
62
+ starter_ids = {"langgraph-react-agent", "math-logic-calculator", "data-etl-transformer"}
63
+ enriched = [t for t in enriched if t.get("id") in starter_ids or t.get("previewType") or t.get("badge")]
64
+
65
+ return {"templates": enriched}
66
+
67
+
68
+ @router.get("/templates/{template_id}")
69
+ @router.get("/workflow-logic/{template_id}")
70
+ def get_template_endpoint(template_id: str):
71
+ """Retrieve a published global template by ID from MongoDB Atlas."""
72
+ t = workflow_repo.get_template(template_id)
73
+ if not t:
74
+ raise HTTPException(status_code=404, detail=f"Template {template_id} not found")
75
+ return t
76
+
77
+
78
+ @router.post("/templates")
79
+ @router.post("/workflow-logic")
80
+ def publish_template_endpoint(payload: TemplatePublishPayload, request: Request):
81
+ """Publish a workflow template to MongoDB Atlas global catalog (Owner/Admin/Editor only)."""
82
+ ctx = require_roles(
83
+ request,
84
+ {"owner", "admin", "editor"},
85
+ allow_local_solo=True,
86
+ action="template.publish",
87
+ resource_type="template",
88
+ resource_id=payload.id or payload.name,
89
+ )
90
+ dumped = payload.model_dump()
91
+ if ctx.get("user_id") and ctx["user_id"] != "service_account":
92
+ dumped["author_id"] = ctx["user_id"]
93
+ dumped["org_id"] = ctx.get("org_id") or "local_personal"
94
+
95
+ t_id = workflow_repo.save_template(dumped)
96
+ audit_action(request, workflow_repo, "template.publish", "template", t_id, 200)
97
+ return {"success": True, "id": t_id}
98
+
99
+
100
+ @router.post("/templates/{template_id}/use")
101
+ @router.post("/workflow-logic/{template_id}/use")
102
+ def track_template_use_endpoint(
103
+ template_id: str,
104
+ request: Request,
105
+ payload: Optional[TemplateUsePayload] = None,
106
+ user_id: Optional[str] = None,
107
+ ):
108
+ auth_user = getattr(request.state, "user", None)
109
+ effective_uid = (auth_user.get("id") if auth_user else None) or user_id or (
110
+ payload.user_id if payload else None
111
+ ) or (payload.userId if payload else None)
112
+ count = workflow_repo.track_template_use(template_id, effective_uid)
113
+ return {"success": True, "template_id": template_id, "downloads": count}
114
+
115
+
116
+ @router.post("/templates/{template_id}/star")
117
+ @router.post("/workflow-logic/{template_id}/star")
118
+ def star_template_endpoint(template_id: str, request: Request, user_id: Optional[str] = None):
119
+ auth_user = getattr(request.state, "user", None)
120
+ effective_uid = (auth_user.get("id") if auth_user else None) or user_id
121
+ res = workflow_repo.toggle_template_star(template_id, effective_uid)
122
+ return {"success": True, **res}
app/routers/users.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - User Profile, Stats, Quotas & Billing Plan Router
3
+ Enforces identity isolation and strict Owner/Admin + authenticated authorization on plan updates.
4
+ """
5
+
6
+ from typing import Any, Dict, Optional
7
+ from fastapi import APIRouter, HTTPException, Request, status
8
+ from pydantic import BaseModel, Field
9
+
10
+ from database.mongo import workflow_repo
11
+ from security.rbac import audit_action, get_request_workspace, is_auth_required, require_roles
12
+
13
+ router = APIRouter(tags=["Users & Plans"])
14
+
15
+
16
+ class UserProfileSyncPayload(BaseModel):
17
+ user_id: str = Field(..., description="Supabase UUID")
18
+ email: Optional[str] = Field(None, description="User email")
19
+ full_name: Optional[str] = Field(None, description="Full display name")
20
+ user_name: Optional[str] = Field(None, description="Username handle")
21
+ avatar_url: Optional[str] = Field(None, description="Avatar image URL")
22
+ auth_provider: Optional[str] = Field("email", description="Authentication provider")
23
+
24
+
25
+ class UserProfileUpdatePayload(BaseModel):
26
+ full_name: Optional[str] = None
27
+ user_name: Optional[str] = None
28
+ avatar_url: Optional[str] = None
29
+ preferences: Optional[Dict[str, Any]] = None
30
+
31
+
32
+ class PlanUpdatePayload(BaseModel):
33
+ plan: str = Field(..., description="Target plan ID: free, pro, team, enterprise")
34
+ billing_period: Optional[str] = Field("monthly", description="Billing cadence: monthly or yearly")
35
+
36
+
37
+ def verify_user_access(target_user_id: str, request: Request) -> None:
38
+ """Enforce multi-tenant data isolation for user profile endpoints."""
39
+ state = getattr(request, "state", None)
40
+ auth_user = getattr(state, "user", None) if state else None
41
+ if auth_user:
42
+ current_user_id = auth_user.get("id")
43
+ user_role = auth_user.get("role")
44
+ if user_role == "admin" or current_user_id == "service_account":
45
+ return
46
+ if current_user_id and str(current_user_id) != str(target_user_id):
47
+ raise HTTPException(
48
+ status_code=status.HTTP_403_FORBIDDEN,
49
+ detail="Forbidden: You do not have permission to view or modify this user profile.",
50
+ )
51
+ elif is_auth_required():
52
+ raise HTTPException(
53
+ status_code=status.HTTP_401_UNAUTHORIZED,
54
+ detail="Authentication required to access user profile.",
55
+ )
56
+
57
+
58
+ @router.get("/user/stats")
59
+ def get_user_stats_endpoint(request: Request, user_id: Optional[str] = None):
60
+ ctx = get_request_workspace(request)
61
+ auth_user = getattr(request.state, "user", None)
62
+ target_uid = user_id
63
+ target_org = None
64
+ if auth_user and not ctx.get("is_platform_admin") and auth_user.get("role") != "admin":
65
+ target_uid = auth_user.get("id")
66
+ active_org = ctx.get("org_id")
67
+ if active_org and not active_org.startswith("personal_"):
68
+ target_org = active_org
69
+ return workflow_repo.get_user_stats(user_id=target_uid, org_id=target_org)
70
+
71
+
72
+ @router.get("/users/{user_id}/profile")
73
+ def get_user_profile(user_id: str, request: Request):
74
+ verify_user_access(user_id, request)
75
+ profile = workflow_repo.get_user_profile(user_id)
76
+ if not profile:
77
+ raise HTTPException(status_code=404, detail="Profile not found in MongoDB")
78
+ return profile
79
+
80
+
81
+ @router.post("/users/profile/sync")
82
+ @router.post("/users/{user_id}/profile")
83
+ def sync_user_profile(payload: UserProfileSyncPayload, request: Request):
84
+ verify_user_access(payload.user_id, request)
85
+ data = payload.model_dump(exclude_unset=True)
86
+ return workflow_repo.upsert_user_profile(payload.user_id, data)
87
+
88
+
89
+ @router.put("/users/{user_id}/profile")
90
+ def update_user_profile(user_id: str, payload: UserProfileUpdatePayload, request: Request):
91
+ verify_user_access(user_id, request)
92
+ data = payload.model_dump(exclude_unset=True)
93
+ updated = workflow_repo.update_user_profile(user_id, data)
94
+ if not updated:
95
+ raise HTTPException(status_code=404, detail="Profile not found in MongoDB")
96
+ return updated
97
+
98
+
99
+ @router.post("/users/{user_id}/quota/increment")
100
+ def increment_quota(user_id: str, request: Request):
101
+ verify_user_access(user_id, request)
102
+ return workflow_repo.update_user_quota(user_id, increment=1)
103
+
104
+
105
+ @router.get("/pricing")
106
+ def get_pricing_plans_endpoint():
107
+ """Return available platform subscription plans and quotas from MongoDB Atlas."""
108
+ return {"plans": workflow_repo.get_pricing_plans()}
109
+
110
+
111
+ @router.post("/users/{user_id}/plan")
112
+ def update_user_plan_endpoint(user_id: str, payload: PlanUpdatePayload, request: Request):
113
+ """
114
+ Update user's subscription plan tier in MongoDB Atlas.
115
+ Strictly protected: requires authentication + Owner/Admin RBAC + self or platform-admin check.
116
+ """
117
+ ctx = require_roles(
118
+ request,
119
+ {"owner", "admin"},
120
+ allow_local_solo=False,
121
+ action="user.plan.update",
122
+ resource_type="plan",
123
+ resource_id=user_id,
124
+ )
125
+ if not ctx.get("is_platform_admin") and str(ctx.get("user_id")) != str(user_id):
126
+ audit_action(request, workflow_repo, "user.plan.update", "plan", user_id, 403)
127
+ raise HTTPException(
128
+ status_code=status.HTTP_403_FORBIDDEN,
129
+ detail="Forbidden: You can only update your own subscription plan.",
130
+ )
131
+
132
+ sub = workflow_repo.update_user_plan(user_id, payload.plan, payload.billing_period or "monthly")
133
+ audit_action(request, workflow_repo, "user.plan.update", "plan", user_id, 200, {"plan": payload.plan})
134
+ return {"success": True, "subscription": sub}
app/routers/workflows.py ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Workflows CRUD & Multi-Tenant RBAC Router
3
+ Enforces org-scoped isolation, v3.0.0 schema versioning, Viewer read/fork protection, and audit logging.
4
+ """
5
+
6
+ import copy
7
+ import uuid
8
+ from typing import Any, Dict, List, Optional
9
+ from fastapi import APIRouter, HTTPException, Request, status
10
+ from pydantic import BaseModel, Field
11
+
12
+ from database.mongo import workflow_repo
13
+ from database.workflows_repo import resolve_workflow_org_id
14
+ from lambdas.workflows_handler import (
15
+ delete_workflow_logic,
16
+ get_workflow_logic,
17
+ list_workflows_logic,
18
+ save_workflow_logic,
19
+ update_workflow_logic,
20
+ validate_workflow_document_logic,
21
+ )
22
+ from security.rbac import audit_action, get_request_workspace, is_auth_required, require_roles
23
+
24
+ router = APIRouter(prefix="/workflows", tags=["Workflows"])
25
+
26
+
27
+ class WorkflowPayload(BaseModel):
28
+ id: Optional[str] = None
29
+ name: str = "Untitled Workflow"
30
+ description: Optional[str] = ""
31
+ schema_version: Optional[str] = Field("3.0.0", description="Workflow schema format version")
32
+ dependencies: Optional[List[str]] = Field(default_factory=list, description="Pinned library dependencies")
33
+ nodes: List[Dict[str, Any]] = Field(default_factory=list)
34
+ edges: List[Dict[str, Any]] = Field(default_factory=list)
35
+ viewport: Optional[Dict[str, Any]] = Field(default_factory=lambda: {"x": 0, "y": 0, "zoom": 1.0})
36
+ compiled_artifacts: Optional[Dict[str, Any]] = Field(default_factory=dict, description="Cached Python code & Dockerfile")
37
+ user_id: Optional[str] = None
38
+ org_id: Optional[str] = None
39
+ source_template_id: Optional[str] = None
40
+ created_at: Optional[str] = None
41
+ updated_at: Optional[str] = None
42
+
43
+
44
+ def verify_workflow_ownership(wf: Dict[str, Any], request: Request) -> None:
45
+ """
46
+ Enforce multi-tenant data isolation (OWASP BOLA/IDOR defense).
47
+ Verifies workspace membership or legacy user_id ownership.
48
+ """
49
+ owner_id = wf.get("user_id")
50
+ wf_org_id = resolve_workflow_org_id(wf)
51
+ state = getattr(request, "state", None)
52
+ auth_user = getattr(state, "user", None) if state else None
53
+
54
+ if auth_user:
55
+ current_user_id = auth_user.get("id")
56
+ user_role = auth_user.get("role")
57
+ if user_role == "admin" or current_user_id == "service_account":
58
+ return
59
+ ctx = get_request_workspace(request)
60
+ if ctx.get("is_platform_admin"):
61
+ return
62
+ # Org match: if workflow belongs to active org and user is an active member
63
+ if wf_org_id and ctx.get("org_id") == wf_org_id and not ctx.get("org_access_denied"):
64
+ return
65
+ if owner_id and current_user_id and str(current_user_id) == str(owner_id):
66
+ return
67
+ if owner_id or (wf_org_id and wf_org_id != "local_personal"):
68
+ raise HTTPException(
69
+ status_code=status.HTTP_403_FORBIDDEN,
70
+ detail="Forbidden: You do not have permission to access or modify this workflow.",
71
+ )
72
+ elif is_auth_required():
73
+ raise HTTPException(
74
+ status_code=status.HTTP_401_UNAUTHORIZED,
75
+ detail="Authentication required to access protected workflows.",
76
+ )
77
+
78
+
79
+ @router.post("")
80
+ def save_workflow(payload: WorkflowPayload, request: Request):
81
+ ctx = require_roles(
82
+ request,
83
+ {"owner", "admin", "editor"},
84
+ allow_local_solo=True,
85
+ action="workflow.create",
86
+ resource_type="workflow",
87
+ )
88
+ dumped = payload.model_dump()
89
+ if ctx.get("user_id") and ctx.get("user_id") != "service_account":
90
+ dumped["user_id"] = ctx["user_id"]
91
+ if ctx.get("org_id"):
92
+ dumped["org_id"] = ctx["org_id"]
93
+ try:
94
+ res = save_workflow_logic(dumped)
95
+ except ValueError as e:
96
+ raise HTTPException(status_code=400, detail=str(e))
97
+ audit_action(request, workflow_repo, "workflow.create", "workflow", res.get("id", ""), 200)
98
+ return res
99
+
100
+
101
+ @router.get("")
102
+ def list_workflows(request: Request, user_id: Optional[str] = None, org_id: Optional[str] = None):
103
+ ctx = get_request_workspace(request)
104
+ auth_user = getattr(request.state, "user", None)
105
+
106
+ if auth_user:
107
+ is_admin = bool(ctx.get("is_platform_admin") or auth_user.get("role") == "admin")
108
+ active_org = ctx.get("org_id")
109
+ if is_admin and (user_id or org_id):
110
+ return list_workflows_logic(user_id=user_id, org_id=org_id)
111
+ if active_org and not active_org.startswith("personal_"):
112
+ return list_workflows_logic(org_id=active_org)
113
+ return list_workflows_logic(user_id=auth_user.get("id"), org_id=active_org)
114
+
115
+ # Zero-leakage policy in local/unauthenticated mode: return [] unless explicitly scoped by user_id
116
+ if not user_id and not org_id:
117
+ return []
118
+ return list_workflows_logic(user_id=user_id, org_id=org_id)
119
+
120
+
121
+ @router.get("/{workflow_id}")
122
+ def get_workflow(workflow_id: str, request: Request):
123
+ wf = get_workflow_logic(workflow_id)
124
+ if not wf:
125
+ raise HTTPException(status_code=404, detail="Workflow not found")
126
+ verify_workflow_ownership(wf, request)
127
+ return wf
128
+
129
+
130
+ @router.put("/{workflow_id}")
131
+ def update_workflow(workflow_id: str, payload: WorkflowPayload, request: Request):
132
+ require_roles(
133
+ request,
134
+ {"owner", "admin", "editor"},
135
+ allow_local_solo=True,
136
+ action="workflow.update",
137
+ resource_type="workflow",
138
+ resource_id=workflow_id,
139
+ )
140
+ existing = get_workflow_logic(workflow_id)
141
+ if not existing:
142
+ raise HTTPException(status_code=404, detail="Workflow not found")
143
+ verify_workflow_ownership(existing, request)
144
+
145
+ dumped = payload.model_dump()
146
+ if existing.get("user_id"):
147
+ dumped["user_id"] = existing["user_id"]
148
+ dumped["org_id"] = resolve_workflow_org_id(existing)
149
+
150
+ try:
151
+ updated = update_workflow_logic(workflow_id, dumped)
152
+ if not updated:
153
+ raise HTTPException(status_code=404, detail="Workflow not found")
154
+ audit_action(request, workflow_repo, "workflow.update", "workflow", workflow_id, 200)
155
+ return updated
156
+ except ValueError as e:
157
+ raise HTTPException(status_code=400, detail=str(e))
158
+
159
+
160
+ @router.patch("/{workflow_id}")
161
+ def patch_workflow(workflow_id: str, payload: Dict[str, Any], request: Request):
162
+ require_roles(
163
+ request,
164
+ {"owner", "admin", "editor"},
165
+ allow_local_solo=True,
166
+ action="workflow.patch",
167
+ resource_type="workflow",
168
+ resource_id=workflow_id,
169
+ )
170
+ existing = get_workflow_logic(workflow_id)
171
+ if not existing:
172
+ raise HTTPException(status_code=404, detail="Workflow not found")
173
+ verify_workflow_ownership(existing, request)
174
+
175
+ if existing.get("user_id"):
176
+ payload["user_id"] = existing["user_id"]
177
+ payload["org_id"] = resolve_workflow_org_id(existing)
178
+
179
+ try:
180
+ updated = update_workflow_logic(workflow_id, payload)
181
+ if not updated:
182
+ raise HTTPException(status_code=404, detail="Workflow not found")
183
+ audit_action(request, workflow_repo, "workflow.patch", "workflow", workflow_id, 200)
184
+ return updated
185
+ except ValueError as e:
186
+ raise HTTPException(status_code=400, detail=str(e))
187
+
188
+
189
+ @router.post("/{workflow_id}/fork")
190
+ def fork_workflow(workflow_id: str, request: Request):
191
+ """Allow any org member (including Viewer) to fork a readable workflow into their personal workspace."""
192
+ ctx = require_roles(
193
+ request,
194
+ {"owner", "admin", "editor", "viewer"},
195
+ allow_local_solo=True,
196
+ action="workflow.fork",
197
+ resource_type="workflow",
198
+ resource_id=workflow_id,
199
+ )
200
+ existing = get_workflow_logic(workflow_id)
201
+ if not existing:
202
+ raise HTTPException(status_code=404, detail="Workflow not found")
203
+ verify_workflow_ownership(existing, request)
204
+
205
+ fork_doc = copy.deepcopy(existing)
206
+ uid = ctx.get("user_id")
207
+ fork_doc["id"] = f"wf_{uuid.uuid4().hex[:12]}"
208
+ fork_doc["name"] = f"{existing.get('name', 'Workflow')} (Fork)"
209
+ fork_doc["user_id"] = uid if (uid and uid != "service_account") else None
210
+ fork_doc["org_id"] = f"personal_{uid}" if (uid and uid != "service_account") else "local_personal"
211
+ fork_doc["forked_from_id"] = workflow_id
212
+ fork_doc.pop("created_at", None)
213
+ fork_doc.pop("updated_at", None)
214
+
215
+ saved = save_workflow_logic(fork_doc)
216
+ audit_action(request, workflow_repo, "workflow.fork", "workflow", saved.get("id", ""), 200, {"source": workflow_id})
217
+ return {"id": saved["id"], "workflow": get_workflow_logic(saved["id"])}
218
+
219
+
220
+ @router.post("/validate-document")
221
+ def validate_workflow_doc(payload: Dict[str, Any]):
222
+ """Validate workflow document schema, viewport, and connection integrity."""
223
+ return validate_workflow_document_logic(payload)
224
+
225
+
226
+ @router.delete("/{workflow_id}")
227
+ def delete_workflow(workflow_id: str, request: Request):
228
+ require_roles(
229
+ request,
230
+ {"owner", "admin", "editor"},
231
+ allow_local_solo=True,
232
+ action="workflow.delete",
233
+ resource_type="workflow",
234
+ resource_id=workflow_id,
235
+ )
236
+ existing = get_workflow_logic(workflow_id)
237
+ if not existing:
238
+ raise HTTPException(status_code=404, detail="Workflow not found")
239
+ verify_workflow_ownership(existing, request)
240
+
241
+ success = delete_workflow_logic(workflow_id)
242
+ if not success:
243
+ raise HTTPException(status_code=404, detail="Workflow not found")
244
+ audit_action(request, workflow_repo, "workflow.delete", "workflow", workflow_id, 200)
245
+ return {"success": True, "id": workflow_id}
app/routers/workspace_env.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Workspace Environment Variables Router
3
+ Provides organization-scoped encrypted .env variable CRUD with strict RBAC
4
+ (viewer -> 403), masked list responses, explicit owner/admin reveal, and non-leaky audit logs.
5
+ """
6
+
7
+ from typing import Optional
8
+ from fastapi import APIRouter, HTTPException, Request, status
9
+ from pydantic import BaseModel, Field
10
+
11
+ from database.mongo import workflow_repo
12
+ from lambdas.workspace_env_handler import (
13
+ delete_workspace_env_logic,
14
+ list_workspace_env_logic,
15
+ reveal_workspace_env_logic,
16
+ upsert_workspace_env_logic,
17
+ )
18
+ from security.rbac import audit_action, require_roles
19
+
20
+ router = APIRouter(prefix="/workspace-env", tags=["Workspace Environment"])
21
+
22
+
23
+ class WorkspaceEnvUpsertPayload(BaseModel):
24
+ key: str = Field(..., min_length=1, max_length=64)
25
+ value: str = Field(...)
26
+ is_secret: bool = Field(True)
27
+ description: str = Field("", max_length=240)
28
+
29
+
30
+ def _resolve_target_org(request: Request, org_id: Optional[str], allowed_roles: set, action: str) -> dict:
31
+ target_org = (org_id or request.headers.get("x-org-id") or request.headers.get("X-Org-Id") or "").strip()
32
+ ctx = require_roles(
33
+ request,
34
+ allowed_roles=allowed_roles,
35
+ allow_local_solo=True,
36
+ action=action,
37
+ resource_type="workspace_env",
38
+ resource_id=target_org or "active",
39
+ )
40
+ resolved_org = target_org or str(ctx.get("org_id") or "local_personal")
41
+ uid = ctx.get("user_id")
42
+ if target_org and target_org != ctx.get("org_id") and not ctx.get("is_platform_admin"):
43
+ if not uid:
44
+ raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Forbidden: Cannot access target workspace.")
45
+ mem = workflow_repo.get_org_membership(target_org, uid)
46
+ role = (mem or {}).get("role")
47
+ if not mem or mem.get("status") != "active" or role not in allowed_roles:
48
+ raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Forbidden: Insufficient role in target workspace.")
49
+ return {**ctx, "org_id": target_org, "role": role}
50
+ return {**ctx, "org_id": resolved_org}
51
+
52
+
53
+ @router.get("")
54
+ @router.get("/")
55
+ def list_workspace_env(request: Request, org_id: Optional[str] = None):
56
+ ctx = _resolve_target_org(request, org_id, {"owner", "admin", "editor"}, "env:read")
57
+ return list_workspace_env_logic(ctx["org_id"])
58
+
59
+
60
+ @router.put("")
61
+ @router.put("/")
62
+ @router.post("")
63
+ @router.post("/")
64
+ def upsert_workspace_env(payload: WorkspaceEnvUpsertPayload, request: Request, org_id: Optional[str] = None):
65
+ ctx = _resolve_target_org(request, org_id, {"owner", "admin", "editor"}, "env:write")
66
+ try:
67
+ result = upsert_workspace_env_logic(
68
+ org_id=ctx["org_id"],
69
+ raw_key=payload.key,
70
+ raw_value=payload.value,
71
+ is_secret=payload.is_secret,
72
+ description=payload.description,
73
+ actor_user_id=str(ctx.get("user_id") or "local_solo"),
74
+ )
75
+ except ValueError as exc:
76
+ msg = str(exc)
77
+ status_code = 422 if "ERR-ENV-QUOTA" in msg else 400
78
+ raise HTTPException(status_code=status_code, detail=msg)
79
+ except RuntimeError as exc:
80
+ raise HTTPException(status_code=503, detail=str(exc))
81
+
82
+ audit_action(
83
+ request,
84
+ workflow_repo,
85
+ action="env:write",
86
+ resource_type="workspace_env",
87
+ resource_id=result["item"]["key"],
88
+ status_code=200,
89
+ metadata={"org_id": ctx["org_id"], "is_secret": payload.is_secret},
90
+ )
91
+ return result
92
+
93
+
94
+ @router.delete("/{key}")
95
+ def delete_workspace_env(key: str, request: Request, org_id: Optional[str] = None):
96
+ ctx = _resolve_target_org(request, org_id, {"owner", "admin", "editor"}, "env:write")
97
+ try:
98
+ res = delete_workspace_env_logic(ctx["org_id"], key)
99
+ except ValueError as exc:
100
+ raise HTTPException(status_code=400, detail=str(exc))
101
+ if not res["success"]:
102
+ raise HTTPException(status_code=404, detail=f"Environment variable '{key}' not found.")
103
+
104
+ audit_action(
105
+ request,
106
+ workflow_repo,
107
+ action="env:delete",
108
+ resource_type="workspace_env",
109
+ resource_id=res["key"],
110
+ status_code=200,
111
+ metadata={"org_id": ctx["org_id"]},
112
+ )
113
+ return res
114
+
115
+
116
+ @router.post("/{key}/reveal")
117
+ def reveal_workspace_env(key: str, request: Request, org_id: Optional[str] = None):
118
+ ctx = _resolve_target_org(request, org_id, {"owner", "admin"}, "env:reveal")
119
+ try:
120
+ entry = reveal_workspace_env_logic(ctx["org_id"], key)
121
+ except ValueError as exc:
122
+ raise HTTPException(status_code=400, detail=str(exc))
123
+ except KeyError:
124
+ raise HTTPException(status_code=404, detail=f"Environment variable '{key}' not found.")
125
+ except PermissionError as exc:
126
+ raise HTTPException(status_code=503, detail=str(exc))
127
+
128
+ audit_action(
129
+ request,
130
+ workflow_repo,
131
+ action="env:reveal",
132
+ resource_type="workspace_env",
133
+ resource_id=entry["key"],
134
+ status_code=200,
135
+ metadata={"org_id": ctx["org_id"]},
136
+ )
137
+ return entry
database/__init__.py CHANGED
@@ -1,5 +1,5 @@
1
  """ALLI Database Package"""
2
  from database.repository import WorkflowRepository
3
- from database.mongo import MongoWorkflowRepository
4
 
5
- __all__ = ["WorkflowRepository", "MongoWorkflowRepository"]
 
1
  """ALLI Database Package"""
2
  from database.repository import WorkflowRepository
3
+ from database.mongo import MongoWorkflowRepository, workflow_repo
4
 
5
+ __all__ = ["WorkflowRepository", "MongoWorkflowRepository", "workflow_repo"]
database/audit_repo.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Immutable Audit Log Repository Mixin
3
+ Persists RBAC & workspace audit events in MongoDB Atlas 'audit_logs' with in-memory fallback.
4
+ """
5
+
6
+ import uuid
7
+ from typing import Any, Dict, List, Optional
8
+ from database.connection import get_utc_now
9
+
10
+
11
+ class AuditRepoMixin:
12
+ """Audit trail persistence and querying per organization."""
13
+
14
+ def _init_audit_store(self) -> None:
15
+ self._audit_store: List[Dict[str, Any]] = []
16
+
17
+ def record_audit_log(
18
+ self,
19
+ org_id: str,
20
+ actor_user_id: str,
21
+ actor_role: str,
22
+ action: str,
23
+ resource_type: str,
24
+ resource_id: str = "",
25
+ status_code: int = 200,
26
+ metadata: Optional[Dict[str, Any]] = None,
27
+ ) -> Dict[str, Any]:
28
+ entry = {
29
+ "event_id": f"evt_{uuid.uuid4().hex[:16]}",
30
+ "timestamp": get_utc_now(),
31
+ "org_id": org_id or "local_personal",
32
+ "actor_user_id": actor_user_id or "anonymous",
33
+ "actor_role": actor_role or "viewer",
34
+ "action": action,
35
+ "resource_type": resource_type,
36
+ "resource_id": resource_id,
37
+ "status_code": status_code,
38
+ "metadata": metadata or {},
39
+ }
40
+ audit_col = self.get_collection("audit_logs")
41
+ if audit_col is not None:
42
+ try:
43
+ audit_col.insert_one(dict(entry))
44
+ except Exception as e:
45
+ print(f"[ALLI MongoDB] record_audit_log error: {e}")
46
+ self._audit_store.append(entry)
47
+ if len(self._audit_store) > 1000:
48
+ self._audit_store = self._audit_store[-1000:]
49
+ return entry
50
+
51
+ def list_audit_logs(self, org_id: str, limit: int = 100) -> List[Dict[str, Any]]:
52
+ capped = min(max(1, limit), 500)
53
+ audit_col = self.get_collection("audit_logs")
54
+ if audit_col is not None:
55
+ try:
56
+ docs = list(
57
+ audit_col.find({"org_id": org_id}, {"_id": 0})
58
+ .sort("timestamp", -1)
59
+ .limit(capped)
60
+ )
61
+ if docs:
62
+ return docs
63
+ except Exception as e:
64
+ print(f"[ALLI MongoDB] list_audit_logs error: {e}")
65
+ matching = [e for e in reversed(self._audit_store) if e.get("org_id") == org_id]
66
+ return matching[:capped]
database/blueprint_catalog_utils.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Blueprint Catalog Normalization & Deduplication Helpers.
3
+ Ensures list_blueprints() merges seed defaults, MongoDB Atlas blueprints, and
4
+ in-memory cache into one deduplicated entry per normalized package name.
5
+ """
6
+
7
+ import re
8
+ from typing import Any, Dict, List, Optional, Tuple
9
+
10
+
11
+ _SENTINEL_VERSIONS = {"latest", "unknown", "cached", "none", "auto", "*", ""}
12
+
13
+
14
+ def extract_bp_version(bp: Any) -> Optional[str]:
15
+ """Extract version metadata from a stored blueprint document."""
16
+ if not isinstance(bp, dict):
17
+ return None
18
+ for key in ("version", "package_version"):
19
+ if bp.get(key):
20
+ return str(bp[key])
21
+ for n in (bp.get("nodes") or [])[:5]:
22
+ if isinstance(n, dict) and isinstance(n.get("package"), dict) and n["package"].get("version"):
23
+ return str(n["package"]["version"])
24
+ return None
25
+
26
+
27
+ def _version_rank(version: Any) -> Tuple[int, Tuple[int, ...]]:
28
+ raw = str(version or "").strip().lower()
29
+ if not raw or raw in _SENTINEL_VERSIONS or "built-in" in raw or "builtin" in raw:
30
+ return (0, ())
31
+ nums = tuple(int(part) for part in re.findall(r"\d+", raw)[:4])
32
+ return (1, nums) if nums else (0, ())
33
+
34
+
35
+ def normalize_package_key(pkg_key: str, doc: Optional[Dict[str, Any]] = None) -> str:
36
+ """Return canonical lowercase base package name without @version suffix."""
37
+ raw = str((doc or {}).get("package_name") or pkg_key or "").strip()
38
+ if "@" in raw:
39
+ raw = raw.split("@", 1)[0]
40
+ if "==" in raw:
41
+ raw = raw.split("==", 1)[0]
42
+ return raw.strip().lower()
43
+
44
+
45
+ def _pick_preferred_version(existing_ver: Any, candidate_ver: Any, existing_type: str) -> str:
46
+ ex_str = str(existing_ver or "").strip()
47
+ cand_str = str(candidate_ver or "").strip()
48
+ if existing_type == "stdlib" and ex_str and ("built-in" in ex_str.lower() or not cand_str or cand_str.lower() in _SENTINEL_VERSIONS):
49
+ return ex_str
50
+ ex_rank = _version_rank(ex_str)
51
+ cand_rank = _version_rank(cand_str)
52
+ if cand_rank > ex_rank:
53
+ return cand_str
54
+ return ex_str or cand_str or "latest"
55
+
56
+
57
+ def merge_mongo_blueprint_entry(packages_dict: Dict[str, Dict[str, Any]], doc: Dict[str, Any]) -> None:
58
+ """Merge a single MongoDB blueprint document into packages_dict without creating duplicate keys."""
59
+ pkg_name = str(doc.get("package") or "").strip()
60
+ bp = doc.get("blueprint") or {}
61
+ if not pkg_name:
62
+ return
63
+
64
+ base_key = normalize_package_key(pkg_name, doc)
65
+ if not base_key:
66
+ return
67
+
68
+ if doc.get("is_default_package") and isinstance(bp, dict) and "name" in bp:
69
+ existing = packages_dict.get(base_key)
70
+ merged = dict(bp)
71
+ if existing and existing.get("inDB"):
72
+ merged["inDB"] = True
73
+ packages_dict[base_key] = merged
74
+ return
75
+
76
+ doc_ver = doc.get("version") or extract_bp_version(bp) or (pkg_name.split("@", 1)[1] if "@" in pkg_name else None)
77
+ bp_nodes = bp.get("nodes", []) if isinstance(bp, dict) else []
78
+ bp_funcs = bp.get("functions", []) if isinstance(bp, dict) else []
79
+ cand_count = int(doc.get("node_count") or len(bp_nodes) or len(bp_funcs) or 0)
80
+ bp_doc = str(bp.get("docstring") or "").strip() if isinstance(bp, dict) else ""
81
+
82
+ existing = packages_dict.get(base_key)
83
+ if existing:
84
+ ex_type = str(existing.get("type") or "external")
85
+ ex_cats: List[str] = list(existing.get("categories") or [])
86
+ if "Extracted" not in ex_cats:
87
+ ex_cats.append("Extracted")
88
+ merged_count = max(int(existing.get("nodeCount") or 0), cand_count) or 10
89
+ packages_dict[base_key] = {
90
+ **existing,
91
+ "name": existing.get("name") or doc.get("package_name") or base_key,
92
+ "type": ex_type,
93
+ "version": _pick_preferred_version(existing.get("version"), doc_ver, ex_type),
94
+ "summary": existing.get("summary") or bp_doc or f"Extracted blueprint for {base_key}",
95
+ "nodeCount": merged_count,
96
+ "inDB": True,
97
+ "categories": ex_cats,
98
+ "updated_at": doc.get("updated_at") or existing.get("updated_at"),
99
+ }
100
+ return
101
+
102
+ packages_dict[base_key] = {
103
+ "name": str(doc.get("package_name") or base_key),
104
+ "type": "external",
105
+ "version": str(doc_ver or "latest"),
106
+ "summary": bp_doc or f"Extracted blueprint for {base_key}",
107
+ "nodeCount": cand_count or 10,
108
+ "inDB": True,
109
+ "categories": ["Extracted", "MongoDB Atlas"],
110
+ "updated_at": doc.get("updated_at"),
111
+ }
112
+
113
+
114
+ def merge_memory_blueprint_entry(packages_dict: Dict[str, Dict[str, Any]], pkg_name: str, bp: Any) -> None:
115
+ """Merge an in-memory fallback blueprint into packages_dict without creating duplicate keys."""
116
+ base_key = normalize_package_key(pkg_name)
117
+ if not base_key:
118
+ return
119
+ mem_ver = (pkg_name.split("@", 1)[1] if "@" in pkg_name else None) or extract_bp_version(bp)
120
+ mem_count = len(bp.get("nodes", [])) if isinstance(bp, dict) else 10
121
+ existing = packages_dict.get(base_key)
122
+ if existing:
123
+ existing["inDB"] = True
124
+ ex_type = str(existing.get("type") or "external")
125
+ existing["version"] = _pick_preferred_version(existing.get("version"), mem_ver, ex_type)
126
+ existing["nodeCount"] = max(int(existing.get("nodeCount") or 0), mem_count or 0) or 10
127
+ return
128
+ packages_dict[base_key] = {
129
+ "name": base_key,
130
+ "type": "external",
131
+ "version": str(mem_ver or "cached"),
132
+ "summary": (bp.get("docstring") if isinstance(bp, dict) else None) or f"In-memory blueprint for {base_key}",
133
+ "nodeCount": mem_count or 10,
134
+ "inDB": True,
135
+ "categories": ["Extracted", "Memory"],
136
+ }
database/blueprints_repo.py ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Blueprints & Packages Repository Mixin
3
+ Manages PyPI & standard library blueprints in MongoDB Atlas 'blueprints' with seed defaults.
4
+ """
5
+
6
+ import re
7
+ from typing import Any, Dict, List, Optional, Tuple
8
+ from database.connection import get_utc_now
9
+ from database.seeds import get_default_packages
10
+
11
+
12
+ class BlueprintsRepoMixin:
13
+ """Package blueprint caching and catalog listing."""
14
+
15
+ def _init_blueprints_store(self) -> None:
16
+ self._blueprint_store: Dict[str, Dict[str, Any]] = {}
17
+
18
+ @staticmethod
19
+ def _parse_package_and_version(package_name: str, version: Optional[str] = None) -> Tuple[str, Optional[str], str]:
20
+ """Extract base package name, explicit version, and composite key."""
21
+ raw = package_name.lower().strip()
22
+ ver = version.strip() if version else None
23
+
24
+ if "@" in raw:
25
+ base, v = raw.split("@", 1)
26
+ base = base.strip()
27
+ if not ver:
28
+ ver = v.strip()
29
+ elif "==" in raw:
30
+ base, v = raw.split("==", 1)
31
+ base = base.strip()
32
+ if not ver:
33
+ ver = v.strip()
34
+ else:
35
+ base = raw.split(">=")[0].split("<=")[0].split("~=")[0].strip()
36
+
37
+ composite = f"{base}@{ver}" if ver else base
38
+ return base, ver, composite
39
+
40
+ @staticmethod
41
+ def parse_version_parts(version_str: Optional[str]) -> List[int]:
42
+ """Convert version string (e.g. '1.10.2') into integer array for numerical MongoDB sorting."""
43
+ if not version_str:
44
+ return [0, 0, 0, 0]
45
+ nums = [int(n) for n in re.findall(r"\d+", str(version_str))]
46
+ return (nums + [0, 0, 0, 0])[:4]
47
+
48
+ @staticmethod
49
+ def _extract_bp_version(bp: Dict[str, Any]) -> Any:
50
+ if isinstance(bp.get("package"), dict):
51
+ return bp["package"].get("version")
52
+ return bp.get("version")
53
+
54
+ def get_blueprint(self, package_name: str, version: Optional[str] = None) -> Optional[Dict[str, Any]]:
55
+ """Retrieve cached package blueprint from MongoDB Atlas or memory (zero disk access)."""
56
+ base_name, ver, composite_key = self._parse_package_and_version(package_name, version)
57
+ lookup_keys = [composite_key] if ver else [base_name]
58
+ if ver and composite_key != base_name:
59
+ lookup_keys.append(base_name)
60
+
61
+ bp_col = self.get_collection("blueprints")
62
+ if bp_col is not None:
63
+ try:
64
+ if ver:
65
+ doc = bp_col.find_one(
66
+ {"package_name": base_name, "version": ver, "is_default_package": {"$ne": True}},
67
+ {"_id": 0},
68
+ )
69
+ if doc and doc.get("blueprint"):
70
+ return doc["blueprint"]
71
+ else:
72
+ doc = bp_col.find_one(
73
+ {"package_name": base_name, "is_default_package": {"$ne": True}},
74
+ {"_id": 0},
75
+ sort=[("version_parts", -1)],
76
+ )
77
+ if doc and doc.get("blueprint"):
78
+ return doc["blueprint"]
79
+
80
+ for k in lookup_keys:
81
+ doc = bp_col.find_one({"package": k}, {"_id": 0})
82
+ if doc and doc.get("blueprint") and not doc.get("is_default_package"):
83
+ bp = doc["blueprint"]
84
+ if k == base_name and ver:
85
+ bp_ver = self._extract_bp_version(bp)
86
+ if bp_ver and str(bp_ver).strip() != ver:
87
+ continue
88
+ return bp
89
+ except Exception as e:
90
+ print(f"[ALLI MongoDB] Blueprint fetch error: {e}")
91
+
92
+ for k in lookup_keys:
93
+ if k in self._blueprint_store:
94
+ bp = self._blueprint_store[k]
95
+ if k == base_name and ver:
96
+ bp_ver = self._extract_bp_version(bp)
97
+ if bp_ver and str(bp_ver).strip() != ver:
98
+ continue
99
+ return bp
100
+
101
+ return None
102
+
103
+ @staticmethod
104
+ def validate_blueprint_before_save(blueprint: Dict[str, Any], version: Optional[str] = None) -> None:
105
+ """Validate blueprint structure, non-empty symbols, and SemVer release pattern before saving to DB."""
106
+ if not isinstance(blueprint, dict):
107
+ raise ValueError("Blueprint payload must be a JSON dictionary.")
108
+
109
+ pkg_name = blueprint.get("package_name") or (
110
+ blueprint.get("package", {}).get("name") if isinstance(blueprint.get("package"), dict) else None
111
+ )
112
+ if not pkg_name or not isinstance(pkg_name, str) or not pkg_name.strip():
113
+ raise ValueError("Blueprint payload is missing a valid 'package_name'.")
114
+
115
+ ver_str = str(
116
+ version or blueprint.get("version") or (
117
+ blueprint.get("package", {}).get("version") if isinstance(blueprint.get("package"), dict) else "1.0.0"
118
+ )
119
+ ).strip()
120
+ if not re.search(r"\d+", ver_str):
121
+ raise ValueError(f"Invalid blueprint version '{ver_str}': must contain numeric release digits.")
122
+
123
+ nodes = blueprint.get("nodes")
124
+ funcs = blueprint.get("functions")
125
+ classes = blueprint.get("classes")
126
+ modules = blueprint.get("modules")
127
+ bp_id = blueprint.get("blueprint_id")
128
+ if not nodes and not funcs and not classes and modules is None and not bp_id:
129
+ raise ValueError(
130
+ f"Blueprint for '{pkg_name}' contains zero extractable nodes, functions, classes, or modules."
131
+ )
132
+
133
+ def save_blueprint(self, package_name: str, blueprint: Dict[str, Any], version: Optional[str] = None) -> None:
134
+ """Persist validated package blueprint to MongoDB Atlas or memory under composite and fallback keys."""
135
+ # Ensure blueprint structure and version pass validation gate
136
+ self.validate_blueprint_before_save(blueprint, version)
137
+
138
+ base_name, ver, composite_key = self._parse_package_and_version(package_name, version)
139
+
140
+ if not ver:
141
+ bp_ver = self._extract_bp_version(blueprint)
142
+ if bp_ver and str(bp_ver).strip() not in ("unknown", "latest", "", "None"):
143
+ ver = str(bp_ver).strip()
144
+ composite_key = f"{base_name}@{ver}"
145
+
146
+ keys_to_save = {base_name}
147
+ if ver:
148
+ keys_to_save.add(composite_key)
149
+
150
+ now = get_utc_now()
151
+ version_parts = self.parse_version_parts(ver)
152
+ node_count = 0
153
+ if isinstance(blueprint, dict):
154
+ node_count = len(blueprint.get("nodes", [])) or (
155
+ len(blueprint.get("functions", [])) + len(blueprint.get("classes", []))
156
+ )
157
+
158
+ bp_col = self.get_collection("blueprints")
159
+ if bp_col is not None:
160
+ try:
161
+ for k in keys_to_save:
162
+ doc_payload = {
163
+ "package": k,
164
+ "package_name": base_name,
165
+ "version": ver or "latest",
166
+ "version_parts": version_parts,
167
+ "node_count": node_count,
168
+ "blueprint": blueprint,
169
+ "is_default_package": False,
170
+ "updated_at": now,
171
+ }
172
+ bp_col.update_one(
173
+ {"package": k},
174
+ {"$set": doc_payload},
175
+ upsert=True,
176
+ )
177
+ except Exception as e:
178
+ print(f"[ALLI MongoDB] Blueprint save error: {e}")
179
+
180
+ for k in keys_to_save:
181
+ self._blueprint_store[k] = blueprint
182
+
183
+ def list_blueprints(self) -> List[Dict[str, Any]]:
184
+ """List all deduplicated package blueprints stored in MongoDB Atlas, merged with seed defaults."""
185
+ from database.blueprint_catalog_utils import (
186
+ merge_memory_blueprint_entry,
187
+ merge_mongo_blueprint_entry,
188
+ )
189
+
190
+ packages_dict: Dict[str, Dict[str, Any]] = {
191
+ p["name"].strip().lower(): dict(p) for p in get_default_packages() if p.get("name")
192
+ }
193
+
194
+ bp_col = self.get_collection("blueprints")
195
+ if bp_col is not None:
196
+ try:
197
+ for doc in bp_col.find({}, {"_id": 0}):
198
+ if isinstance(doc, dict):
199
+ merge_mongo_blueprint_entry(packages_dict, doc)
200
+ except Exception as e:
201
+ print(f"[ALLI MongoDB] Blueprint list error: {e}")
202
+
203
+ for pkg_name, bp in self._blueprint_store.items():
204
+ merge_memory_blueprint_entry(packages_dict, pkg_name, bp)
205
+
206
+ return list(packages_dict.values())
207
+
208
+ def list_raw_blueprints(self) -> List[Dict[str, Any]]:
209
+ """Returns stored non-default blueprint documents (for startup library node hydration)."""
210
+ collected: List[Dict[str, Any]] = []
211
+ seen_ids = set()
212
+
213
+ bp_col = self.get_collection("blueprints")
214
+ if bp_col is not None:
215
+ try:
216
+ for doc in bp_col.find({"is_default_package": {"$ne": True}}, {"_id": 0}):
217
+ bp = doc.get("blueprint")
218
+ if isinstance(bp, dict):
219
+ key = f"{doc.get('package', '')}@{doc.get('version', 'latest')}"
220
+ if key not in seen_ids:
221
+ seen_ids.add(key)
222
+ collected.append(bp)
223
+ except Exception as e:
224
+ print(f"[ALLI MongoDB] list_raw_blueprints error: {e}")
225
+
226
+ for store_key, bp in self._blueprint_store.items():
227
+ if isinstance(bp, dict) and "@" in store_key and store_key not in seen_ids:
228
+ seen_ids.add(store_key)
229
+ collected.append(bp)
230
+
231
+ return collected
database/catalogs_repo.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Catalogs, Fundamentals, Schemas, Learning & Blog Repository Mixin
3
+ Database-first access to core specs, Python fundamentals, node schemas, learning modules, blog posts, and validation reports.
4
+ """
5
+
6
+ from typing import Any, Dict, List, Optional
7
+ from database.connection import get_utc_now
8
+ from database.seeds import (
9
+ consolidate_fundamentals_to_9,
10
+ get_default_blog_data,
11
+ get_default_core_specs,
12
+ get_default_learning_modules,
13
+ get_default_python_fundamentals,
14
+ get_default_studio_palette,
15
+ get_default_validation_artifacts,
16
+ get_raw_programming_fundamentals,
17
+ is_studio_palette_stale,
18
+ )
19
+
20
+
21
+ class CatalogsRepoMixin:
22
+ """Database-first catalog persistence with clean backend seed fallbacks."""
23
+
24
+ def _init_catalogs_store(self) -> None:
25
+ self._core_specs_store: Optional[List[Dict[str, Any]]] = None
26
+ self._studio_palette_store: Optional[List[Dict[str, Any]]] = None
27
+ self._python_fundamentals_store: Optional[Dict[str, Any]] = None
28
+ self._staged_nodes_store: Dict[str, Dict[str, Any]] = {}
29
+
30
+ def get_core_specs(self) -> List[Dict[str, Any]]:
31
+ col = self.get_collection("core_definitions")
32
+ if col is not None:
33
+ try:
34
+ doc = col.find_one({"key": "python_core_categories"}, {"_id": 0})
35
+ if doc and "categories" in doc:
36
+ return doc["categories"]
37
+ except Exception as e:
38
+ print(f"[ALLI MongoDB] Core specs fetch error: {e}")
39
+ return self._core_specs_store if self._core_specs_store is not None else get_default_core_specs()
40
+
41
+ def save_core_specs(self, categories: List[Dict[str, Any]]) -> bool:
42
+ self._core_specs_store = categories
43
+ col = self.get_collection("core_definitions")
44
+ if col is not None:
45
+ try:
46
+ col.update_one(
47
+ {"key": "python_core_categories"},
48
+ {"$set": {"key": "python_core_categories", "categories": categories, "updated_at": get_utc_now()}},
49
+ upsert=True,
50
+ )
51
+ return True
52
+ except Exception as e:
53
+ print(f"[ALLI MongoDB] Core specs save error: {e}")
54
+ return False
55
+ return True
56
+
57
+ def get_studio_palette(self) -> List[Dict[str, Any]]:
58
+ canonical = get_default_studio_palette()
59
+ col = self.get_collection("core_definitions")
60
+ if col is not None:
61
+ try:
62
+ doc = col.find_one({"key": "primary_studio_categories"}, {"_id": 0})
63
+ if doc and "categories" in doc and not is_studio_palette_stale(doc.get("categories"), canonical):
64
+ return doc["categories"]
65
+ col.update_one(
66
+ {"key": "primary_studio_categories"},
67
+ {"$set": {"key": "primary_studio_categories", "categories": canonical, "updated_at": get_utc_now()}},
68
+ upsert=True,
69
+ )
70
+ self._studio_palette_store = canonical
71
+ return canonical
72
+ except Exception as e:
73
+ print(f"[ALLI MongoDB] Studio palette fetch error: {e}")
74
+ if self._studio_palette_store is not None and not is_studio_palette_stale(self._studio_palette_store, canonical):
75
+ return self._studio_palette_store
76
+ return canonical
77
+
78
+ def get_python_fundamentals(self) -> Dict[str, Any]:
79
+ col = self.get_collection("python_fundamentals")
80
+ if col is not None:
81
+ try:
82
+ doc = col.find_one({"key": "python_fundamentals_schema"}, {"_id": 0})
83
+ if doc and "constructs" in doc:
84
+ return doc
85
+ except Exception as e:
86
+ print(f"[ALLI MongoDB] Python fundamentals fetch error: {e}")
87
+ if self._python_fundamentals_store:
88
+ return self._python_fundamentals_store
89
+ return get_default_python_fundamentals()
90
+
91
+ def save_python_fundamentals(self, data: Dict[str, Any]) -> bool:
92
+ self._python_fundamentals_store = dict(data)
93
+ col = self.get_collection("python_fundamentals")
94
+ if col is not None:
95
+ try:
96
+ col.update_one(
97
+ {"key": "python_fundamentals_schema"},
98
+ {"$set": {**data, "key": "python_fundamentals_schema", "updated_at": get_utc_now()}},
99
+ upsert=True,
100
+ )
101
+ return True
102
+ except Exception as e:
103
+ print(f"[ALLI MongoDB] Python fundamentals save error: {e}")
104
+ return False
105
+
106
+ def get_programming_fundamentals(self, consolidated: bool = True) -> Dict[str, Any]:
107
+ """Return 9 consolidated fundamentals categories covering all 53 topics cleanly."""
108
+ col = self.get_collection("python_fundamentals")
109
+ if col is not None:
110
+ try:
111
+ doc = col.find_one({"key": "programming_fundamentals_categories"}, {"_id": 0})
112
+ if doc and doc.get("categories"):
113
+ cats = doc["categories"] if consolidated else (doc.get("legacy_categories") or doc["categories"])
114
+ return {
115
+ "categories": cats,
116
+ "total_categories": len(cats),
117
+ "total_topics": sum(len(c.get("topics", [])) for c in cats),
118
+ "source": "mongodb",
119
+ }
120
+ except Exception as e:
121
+ print(f"[ALLI MongoDB] Programming fundamentals fetch error: {e}")
122
+
123
+ raw_cats = get_raw_programming_fundamentals()
124
+ cats = consolidate_fundamentals_to_9(raw_cats) if consolidated else raw_cats
125
+ return {
126
+ "categories": cats,
127
+ "total_categories": len(cats),
128
+ "total_topics": sum(len(c.get("topics", [])) for c in cats),
129
+ "source": "backend_seed",
130
+ }
131
+
132
+ def list_node_schemas_db(self) -> List[Dict[str, Any]]:
133
+ col = self.get_collection("node_schemas")
134
+ if col is not None:
135
+ try:
136
+ docs = list(col.find({}, {"_id": 0}))
137
+ if docs:
138
+ return docs
139
+ except Exception as e:
140
+ print(f"[ALLI MongoDB] node_schemas fetch error: {e}")
141
+ from language_core.node_schema import default_registry
142
+ return default_registry.to_json_dict()
143
+
144
+ def save_node_schema(self, schema_dict: Dict[str, Any]) -> None:
145
+ """Persists a promoted NodeDefinition schema into the node_schemas collection."""
146
+ node_type = schema_dict.get("type", "")
147
+ if not node_type:
148
+ return
149
+ col = self.get_collection("node_schemas")
150
+ if col is not None:
151
+ try:
152
+ col.update_one({"type": node_type}, {"$set": dict(schema_dict)}, upsert=True)
153
+ except Exception as e:
154
+ print(f"[ALLI MongoDB] save_node_schema error: {e}")
155
+
156
+ def save_staged_node_record(self, record: Dict[str, Any]) -> None:
157
+ node_type = record.get("node_type", "")
158
+ if not node_type:
159
+ return
160
+ self._staged_nodes_store[node_type] = dict(record)
161
+ col = self.get_collection("staged_node_schemas")
162
+ if col is not None:
163
+ try:
164
+ col.update_one({"node_type": node_type}, {"$set": record}, upsert=True)
165
+ except Exception as e:
166
+ print(f"[ALLI MongoDB] save_staged_node_record error: {e}")
167
+
168
+ def list_staged_node_records(self) -> List[Dict[str, Any]]:
169
+ merged: Dict[str, Dict[str, Any]] = {}
170
+ col = self.get_collection("staged_node_schemas")
171
+ if col is not None:
172
+ try:
173
+ for doc in col.find({}, {"_id": 0}):
174
+ if isinstance(doc, dict) and doc.get("node_type"):
175
+ merged[doc["node_type"]] = doc
176
+ except Exception as e:
177
+ print(f"[ALLI MongoDB] list_staged_node_records error: {e}")
178
+ for k, v in self._staged_nodes_store.items():
179
+ merged.setdefault(k, v)
180
+ return list(merged.values())
181
+
182
+ def list_learning_modules(self, gate: Optional[str] = None) -> List[Dict[str, Any]]:
183
+ lessons: List[Dict[str, Any]] = []
184
+ col = self.get_collection("learning_modules")
185
+ if col is not None:
186
+ try:
187
+ query = {"gate": gate} if gate else {}
188
+ lessons = list(col.find(query, {"_id": 0}))
189
+ except Exception as e:
190
+ print(f"[ALLI MongoDB] list_learning_modules error: {e}")
191
+ if not lessons:
192
+ lessons = get_default_learning_modules()
193
+ if gate:
194
+ lessons = [l for l in lessons if l.get("gate") == gate]
195
+ return lessons
196
+
197
+ def get_learning_module(self, lesson_id: str) -> Optional[Dict[str, Any]]:
198
+ col = self.get_collection("learning_modules")
199
+ if col is not None:
200
+ try:
201
+ doc = col.find_one({"id": lesson_id}, {"_id": 0})
202
+ if doc:
203
+ return doc
204
+ except Exception as e:
205
+ print(f"[ALLI MongoDB] get_learning_module error: {e}")
206
+ return next((l for l in get_default_learning_modules() if l.get("id") == lesson_id), None)
207
+
208
+ def list_blog_posts(self, category: Optional[str] = None, q: Optional[str] = None) -> Dict[str, Any]:
209
+ seed_data = get_default_blog_data()
210
+ categories = seed_data.get("categories", [])
211
+ posts: List[Dict[str, Any]] = []
212
+ col = self.get_collection("blog_posts")
213
+ if col is not None:
214
+ try:
215
+ posts = list(col.find({}, {"_id": 0}))
216
+ except Exception as e:
217
+ print(f"[ALLI MongoDB] list_blog_posts error: {e}")
218
+ if not posts:
219
+ posts = list(seed_data.get("posts", []))
220
+
221
+ if category and category != "all":
222
+ posts = [p for p in posts if p.get("category") == category]
223
+ if q and q.strip():
224
+ term = q.lower().strip()
225
+ posts = [
226
+ p for p in posts
227
+ if term in p.get("title", "").lower()
228
+ or term in p.get("excerpt", "").lower()
229
+ or any(term in t.lower() for t in p.get("tags", []))
230
+ ]
231
+ return {"categories": categories, "posts": posts, "total": len(posts)}
232
+
233
+ def get_blog_post(self, slug: str) -> Optional[Dict[str, Any]]:
234
+ col = self.get_collection("blog_posts")
235
+ if col is not None:
236
+ try:
237
+ doc = col.find_one({"slug": slug}, {"_id": 0})
238
+ if doc:
239
+ return doc
240
+ except Exception as e:
241
+ print(f"[ALLI MongoDB] get_blog_post error: {e}")
242
+ posts = get_default_blog_data().get("posts", [])
243
+ return next((p for p in posts if p.get("slug") == slug), None)
244
+
245
+ def get_validation_artifacts(self) -> Dict[str, Any]:
246
+ return get_default_validation_artifacts()
database/connection.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - MongoDB Connection Manager
3
+ Handles resilient MongoDB Atlas connection lifecycle, lazy connect, and collection/index setup.
4
+ """
5
+
6
+ import os
7
+ from datetime import datetime, timezone
8
+ from typing import Optional, Any
9
+ import env
10
+
11
+ try:
12
+ from pymongo import MongoClient
13
+ from pymongo.errors import ConnectionFailure, PyMongoError
14
+ PYMONGO_AVAILABLE = True
15
+ except ImportError:
16
+ MongoClient = None
17
+ ConnectionFailure = Exception
18
+ PyMongoError = Exception
19
+ PYMONGO_AVAILABLE = False
20
+
21
+
22
+ def get_utc_now() -> str:
23
+ """Return current UTC timestamp in ISO-8601 format."""
24
+ return datetime.now(timezone.utc).isoformat()
25
+
26
+
27
+ class MongoConnectionMixin:
28
+ """Mixin managing MongoDB Atlas client, lazy connectivity, and indexes."""
29
+
30
+ def _init_connection(self, uri: Optional[str] = None, db_name: Optional[str] = None) -> None:
31
+ self.uri = uri or os.environ.get("MONGODB_URI") or env.MONGODB_URI
32
+ self.db_name = db_name or os.environ.get("MONGODB_DB_NAME") or env.MONGODB_DB_NAME
33
+ self.client: Optional[Any] = None
34
+ self.collection: Optional[Any] = None
35
+ self.users_collection: Optional[Any] = None
36
+ self._connected_attempted = False
37
+ self._is_connected = False
38
+
39
+ @property
40
+ def is_connected(self) -> bool:
41
+ if not self._connected_attempted:
42
+ self._ensure_connected()
43
+ return self._is_connected
44
+
45
+ @is_connected.setter
46
+ def is_connected(self, val: bool) -> None:
47
+ self._connected_attempted = True
48
+ self._is_connected = bool(val)
49
+
50
+ def _ensure_connected(self) -> bool:
51
+ if self._connected_attempted:
52
+ return self._is_connected
53
+ self._connected_attempted = True
54
+ if PYMONGO_AVAILABLE and self.uri and MongoClient is not None:
55
+ try:
56
+ # Fast timeout (1500ms) so requests never block serverless invocations
57
+ self.client = MongoClient(
58
+ self.uri,
59
+ serverSelectionTimeoutMS=1500,
60
+ connectTimeoutMS=1500,
61
+ )
62
+ self.client.admin.command("ping")
63
+ db = self.client[self.db_name]
64
+ self.collection = db["workflows"]
65
+ self.users_collection = db["users"]
66
+ self._ensure_indexes(db)
67
+ self._is_connected = True
68
+ print(
69
+ f"[ALLI MongoDB] Successfully connected to database '{self.db_name}' "
70
+ "(workflows, users, orgs & catalogs collections)"
71
+ )
72
+ except Exception as e:
73
+ print(
74
+ f"[ALLI MongoDB] Connection skipped or unavailable ({str(e)}). "
75
+ "Using resilient in-memory storage."
76
+ )
77
+ self._is_connected = False
78
+ else:
79
+ self._is_connected = False
80
+ return self._is_connected
81
+
82
+ def _ensure_indexes(self, db: Any) -> None:
83
+ """Create non-blocking background indexes across core collections."""
84
+ index_specs = [
85
+ ("users", "user_id", {"unique": True}),
86
+ ("workflows", "id", {}),
87
+ ("workflows", "user_id", {}),
88
+ ("workflows", "org_id", {}),
89
+ ("organizations", "org_id", {"unique": True}),
90
+ ("organizations", "slug", {}),
91
+ ("org_memberships", [("org_id", 1), ("user_id", 1)], {"unique": True}),
92
+ ("org_memberships", "user_id", {}),
93
+ ("audit_logs", [("org_id", 1), ("timestamp", -1)], {}),
94
+ ("blueprints", "package", {}),
95
+ ("node_schemas", "type", {}),
96
+ ("staged_node_schemas", "node_type", {}),
97
+ ("templates", "id", {}),
98
+ ("plans", "id", {}),
99
+ ("learning_modules", "id", {}),
100
+ ("blog_posts", "slug", {}),
101
+ ("workspace_env_vars", [("org_id", 1), ("key", 1)], {"unique": True}),
102
+ ]
103
+ for col_name, keys, opts in index_specs:
104
+ try:
105
+ db[col_name].create_index(keys, background=True, **opts)
106
+ except Exception:
107
+ pass
108
+
109
+ def get_collection(self, name: str) -> Optional[Any]:
110
+ """Return PyMongo collection handle when connected, else None."""
111
+ if not self.is_connected or self.client is None:
112
+ return None
113
+ return self.client[self.db_name][name]
database/mongo.py CHANGED
@@ -1,1862 +1,65 @@
1
  """
2
- ALLI Backend - MongoDB Repository Implementation
3
- Implements WorkflowRepository and User Profile Store using MongoDB Atlas with graceful local in-memory fallback.
4
  """
5
 
6
- import os
7
- import json
8
- import uuid
9
- from datetime import datetime, timezone
10
- from typing import Dict, Any, List, Optional
11
- import env
12
  from database.repository import WorkflowRepository
13
-
14
- def get_utc_now() -> str:
15
- return datetime.now(timezone.utc).isoformat()
16
-
17
- try:
18
- from pymongo import MongoClient
19
- from pymongo.errors import ConnectionFailure, PyMongoError
20
- PYMONGO_AVAILABLE = True
21
- except ImportError:
22
- PYMONGO_AVAILABLE = False
23
-
24
- DEFAULT_PACKAGES = [
25
- {
26
- "name": "math",
27
- "type": "stdlib",
28
- "version": "3.12 (built-in)",
29
- "pinned_version": "3.12",
30
- "summary": "Mathematical functions: sqrt, sin, cos, factorial, log, gcd",
31
- "nodeCount": 54,
32
- "inDB": True,
33
- "categories": ["Arithmetic", "Trigonometry", "Algebra"],
34
- "is_pinned": False,
35
- "dependencies": [],
36
- "install_command": "# Standard library module - built-in",
37
- "license": "Python Software Foundation License",
38
- },
39
- {
40
- "name": "json",
41
- "type": "stdlib",
42
- "version": "3.12 (built-in)",
43
- "pinned_version": "3.12",
44
- "summary": "JSON encoder and decoder: dumps, loads, dump, load",
45
- "nodeCount": 8,
46
- "inDB": True,
47
- "categories": ["Data", "Serialization"],
48
- "is_pinned": False,
49
- "dependencies": [],
50
- "install_command": "# Standard library module - built-in",
51
- "license": "Python Software Foundation License",
52
- },
53
- {
54
- "name": "requests",
55
- "type": "external",
56
- "version": "2.31.0",
57
- "pinned_version": "2.31.0",
58
- "summary": "HTTP for Humans: get, post, put, delete, session, headers",
59
- "nodeCount": 14,
60
- "inDB": True,
61
- "categories": ["Networking", "REST APIs"],
62
- "is_pinned": True,
63
- "dependencies": ["urllib3>=2.0.0", "certifi>=2023.7.22", "charset-normalizer>=3.2.0", "idna>=3.4"],
64
- "install_command": "pip install requests==2.31.0",
65
- "license": "Apache 2.0",
66
- "pypi_url": "https://pypi.org/project/requests/2.31.0/",
67
- },
68
- {
69
- "name": "datetime",
70
- "type": "stdlib",
71
- "version": "3.12 (built-in)",
72
- "pinned_version": "3.12",
73
- "summary": "Date and time manipulation: now, strftime, timedelta",
74
- "nodeCount": 16,
75
- "inDB": True,
76
- "categories": ["Time", "Utilities"],
77
- "is_pinned": False,
78
- "dependencies": [],
79
- "install_command": "# Standard library module - built-in",
80
- "license": "Python Software Foundation License",
81
- },
82
- {
83
- "name": "os",
84
- "type": "stdlib",
85
- "version": "3.12 (built-in)",
86
- "pinned_version": "3.12",
87
- "summary": "Operating system interfaces: path, environ, listdir, mkdir",
88
- "nodeCount": 28,
89
- "inDB": True,
90
- "categories": ["System", "Files"],
91
- "is_pinned": False,
92
- "dependencies": [],
93
- "install_command": "# Standard library module - built-in",
94
- "license": "Python Software Foundation License",
95
- },
96
- {
97
- "name": "statistics",
98
- "type": "stdlib",
99
- "version": "3.12 (built-in)",
100
- "pinned_version": "3.12",
101
- "summary": "Mathematical statistics functions: mean, median, mode, stdev, variance",
102
- "nodeCount": 12,
103
- "inDB": True,
104
- "categories": ["Math", "Statistics", "Data"],
105
- "is_pinned": False,
106
- "dependencies": [],
107
- "install_command": "# Standard library module - built-in",
108
- "license": "Python Software Foundation License",
109
- },
110
- {
111
- "name": "pandas",
112
- "type": "external",
113
- "version": "2.2.2",
114
- "pinned_version": "2.2.2",
115
- "summary": "Powerful data structures for data analysis, time series, and statistics",
116
- "nodeCount": 45,
117
- "inDB": True,
118
- "categories": ["Data Analysis", "DataFrames", "Tabular"],
119
- "is_pinned": True,
120
- "dependencies": ["numpy>=1.26.0", "python-dateutil>=2.8.2", "pytz>=2020.1"],
121
- "install_command": "pip install pandas==2.2.2",
122
- "license": "BSD-3-Clause",
123
- "pypi_url": "https://pypi.org/project/pandas/2.2.2/",
124
- },
125
- {
126
- "name": "numpy",
127
- "type": "external",
128
- "version": "1.26.4",
129
- "pinned_version": "1.26.4",
130
- "summary": "Fundamental package for scientific computing with Python",
131
- "nodeCount": 60,
132
- "inDB": True,
133
- "categories": ["Scientific Computing", "Arrays", "Math"],
134
- "is_pinned": True,
135
- "dependencies": [],
136
- "install_command": "pip install numpy==1.26.4",
137
- "license": "BSD-3-Clause",
138
- "pypi_url": "https://pypi.org/project/numpy/1.26.4/",
139
- },
140
- ]
141
-
142
- DEFAULT_CORE_SPECS = [
143
- {
144
- "name": "Variables & Types",
145
- "items": [
146
- {
147
- "id": "variable",
148
- "label": "Variable",
149
- "sublabel": "Variable (int/str/bool)",
150
- "category": "Variables & Types",
151
- "icon": "Variable",
152
- "iconColor": "text-blue-400",
153
- "iconBg": "bg-blue-600",
154
- "borderColor": "border-blue-500/40",
155
- "nodeType": "alli.primitive.variable",
156
- "defaultData": {"var_name": "user_id", "value": 42, "type_hint": "int"},
157
- },
158
- {
159
- "id": "constant",
160
- "label": "Constant",
161
- "sublabel": "Immutable Value",
162
- "category": "Variables & Types",
163
- "icon": "Lock",
164
- "iconColor": "text-cyan-400",
165
- "iconBg": "bg-cyan-600",
166
- "borderColor": "border-cyan-500/40",
167
- "nodeType": "alli.primitive.constant",
168
- "defaultData": {"var_name": "PI", "value": 3.14159, "type_hint": "float", "is_const": True},
169
- },
170
- ],
171
- },
172
- {
173
- "name": "Input & Output",
174
- "items": [
175
- {
176
- "id": "user_input",
177
- "label": "User Input",
178
- "sublabel": "input(prompt)",
179
- "category": "Input & Output",
180
- "icon": "TextCursorInput",
181
- "iconColor": "text-indigo-400",
182
- "iconBg": "bg-indigo-600",
183
- "borderColor": "border-indigo-500/40",
184
- "nodeType": "alli.primitive.input",
185
- "defaultData": {"var_name": "text_prompt", "value": "Explain quantum computing in simple words.", "type_hint": "str"},
186
- },
187
- {
188
- "id": "file_input",
189
- "label": "File Input / Output",
190
- "sublabel": "open(file, mode)",
191
- "category": "Input & Output",
192
- "icon": "FileText",
193
- "iconColor": "text-emerald-400",
194
- "iconBg": "bg-emerald-600",
195
- "borderColor": "border-emerald-500/40",
196
- "nodeType": "alli.primitive.file",
197
- "defaultData": {"filename": "data.csv", "mode": "r"},
198
- },
199
- {
200
- "id": "print_output",
201
- "label": "Print Output",
202
- "sublabel": "print(*objects, sep, end, file, flush)",
203
- "category": "Input & Output",
204
- "icon": "Terminal",
205
- "iconColor": "text-emerald-400",
206
- "iconBg": "bg-emerald-600",
207
- "borderColor": "border-emerald-500/40",
208
- "nodeType": "alli.primitive.print",
209
- "defaultData": {
210
- "prefix": "Result:",
211
- "sep": " ",
212
- "end": "\\n",
213
- "file_stream": "stdout",
214
- "flush": False,
215
- "format_style": "standard",
216
- },
217
- },
218
- ],
219
- },
220
- {
221
- "name": "Operators & Logic",
222
- "items": [
223
- {
224
- "id": "binary_op",
225
- "label": "Binary Operation",
226
- "sublabel": "result = a + b",
227
- "category": "Operators & Logic",
228
- "icon": "Binary",
229
- "iconColor": "text-violet-400",
230
- "iconBg": "bg-violet-600",
231
- "borderColor": "border-violet-500/40",
232
- "nodeType": "alli.primitive.binary_op",
233
- "defaultData": {"operator": "+", "a": "10", "b": "5"},
234
- },
235
- {
236
- "id": "compare",
237
- "label": "Comparison",
238
- "sublabel": "is_valid = (a == b)",
239
- "category": "Operators & Logic",
240
- "icon": "Scale",
241
- "iconColor": "text-amber-400",
242
- "iconBg": "bg-amber-600",
243
- "borderColor": "border-amber-500/40",
244
- "nodeType": "alli.primitive.compare",
245
- "defaultData": {"operator": "==", "a": "10", "b": "10"},
246
- },
247
- ],
248
- },
249
- {
250
- "name": "Control Flow",
251
- "items": [
252
- {
253
- "id": "if_else",
254
- "label": "If-Else",
255
- "sublabel": "Condition (True/False)",
256
- "category": "Control Flow",
257
- "icon": "GitBranch",
258
- "iconColor": "text-purple-400",
259
- "iconBg": "bg-purple-600",
260
- "borderColor": "border-purple-500/40",
261
- "nodeType": "alli.flow.condition",
262
- "defaultData": {"condition": "user_id >= 18"},
263
- },
264
- {
265
- "id": "match_case",
266
- "label": "Match-Case",
267
- "sublabel": "Pattern Match",
268
- "category": "Control Flow",
269
- "icon": "GitBranch",
270
- "iconColor": "text-violet-400",
271
- "iconBg": "bg-violet-600",
272
- "borderColor": "border-violet-500/40",
273
- "nodeType": "alli.flow.match",
274
- "defaultData": {"match_var": "status", "cases": ["200", "404", "_"]},
275
- },
276
- ],
277
- },
278
- {
279
- "name": "Loops & Iteration",
280
- "items": [
281
- {
282
- "id": "loop_for",
283
- "label": "For Loop",
284
- "sublabel": "for item in iterable",
285
- "category": "Loops & Iteration",
286
- "icon": "Repeat",
287
- "iconColor": "text-cyan-400",
288
- "iconBg": "bg-cyan-600",
289
- "borderColor": "border-cyan-500/40",
290
- "nodeType": "alli.flow.for_loop",
291
- "defaultData": {"loop_var": "item", "iterable": "range(5)"},
292
- },
293
- {
294
- "id": "loop_while",
295
- "label": "While Loop",
296
- "sublabel": "while condition:",
297
- "category": "Loops & Iteration",
298
- "icon": "Repeat",
299
- "iconColor": "text-teal-400",
300
- "iconBg": "bg-teal-600",
301
- "borderColor": "border-teal-500/40",
302
- "nodeType": "alli.flow.while_loop",
303
- "defaultData": {"condition": "count < 10"},
304
- },
305
- {
306
- "id": "recursion",
307
- "label": "Recursion",
308
- "sublabel": "factorial(n)",
309
- "category": "Loops & Iteration",
310
- "icon": "ArrowLeftRight",
311
- "iconColor": "text-violet-400",
312
- "iconBg": "bg-violet-600",
313
- "borderColor": "border-violet-500/40",
314
- "nodeType": "alli.flow.recursion",
315
- "defaultData": {"function_name": "factorial", "base_case": "n <= 1", "recursive_call": "n * factorial(n - 1)"},
316
- },
317
- ],
318
- },
319
- {
320
- "name": "Functions & Scope",
321
- "items": [
322
- {
323
- "id": "def_function",
324
- "label": "Function Def",
325
- "sublabel": "def func(*args):",
326
- "category": "Functions & Scope",
327
- "icon": "Code2",
328
- "iconColor": "text-yellow-400",
329
- "iconBg": "bg-yellow-600",
330
- "borderColor": "border-yellow-500/40",
331
- "nodeType": "alli.func.def",
332
- "defaultData": {"func_name": "compute_metric", "params": "x, factor=2.5", "return_type": "float", "docstring": "Computes weighted metrics."},
333
- },
334
- {
335
- "id": "call_function",
336
- "label": "Function Call",
337
- "sublabel": "result = func(*args)",
338
- "category": "Functions & Scope",
339
- "icon": "PlayCircle",
340
- "iconColor": "text-amber-400",
341
- "iconBg": "bg-amber-600",
342
- "borderColor": "border-amber-500/40",
343
- "nodeType": "alli.func.call",
344
- "defaultData": {"func_name": "compute_metric", "args": "50, factor=3.0", "target_var": "calc_result"},
345
- },
346
- {
347
- "id": "lambda_func",
348
- "label": "Lambda Expr",
349
- "sublabel": "lambda x: expr",
350
- "category": "Functions & Scope",
351
- "icon": "Zap",
352
- "iconColor": "text-yellow-300",
353
- "iconBg": "bg-yellow-500",
354
- "borderColor": "border-yellow-400/40",
355
- "nodeType": "alli.func.lambda",
356
- "defaultData": {"var_name": "double_fn", "params": "x", "expression": "x * 2"},
357
- },
358
- {
359
- "id": "generator_yield",
360
- "label": "Generator Yield",
361
- "sublabel": "yield item",
362
- "category": "Functions & Scope",
363
- "icon": "IterationCw",
364
- "iconColor": "text-amber-500",
365
- "iconBg": "bg-amber-700",
366
- "borderColor": "border-amber-600/40",
367
- "nodeType": "alli.func.yield",
368
- "defaultData": {"yield_value": "item", "is_from": False},
369
- },
370
- {
371
- "id": "recursion_guard",
372
- "label": "Recursion Guard",
373
- "sublabel": "max_depth protection",
374
- "category": "Functions & Scope",
375
- "icon": "ShieldCheck",
376
- "iconColor": "text-emerald-400",
377
- "iconBg": "bg-emerald-600",
378
- "borderColor": "border-emerald-500/40",
379
- "nodeType": "alli.func.recursion_guard",
380
- "defaultData": {"max_depth": 50},
381
- },
382
- ],
383
- },
384
- {
385
- "name": "OOP & Classes",
386
- "items": [
387
- {
388
- "id": "class_def",
389
- "label": "Class Definition",
390
- "sublabel": "class Name(Bases):",
391
- "category": "OOP & Classes",
392
- "icon": "Layers",
393
- "iconColor": "text-indigo-400",
394
- "iconBg": "bg-indigo-600",
395
- "borderColor": "border-indigo-500/40",
396
- "nodeType": "alli.oop.class_def",
397
- "defaultData": {"class_name": "DataPipeline", "bases": "", "docstring": "Orchestrates end-to-end data processing."},
398
- },
399
- {
400
- "id": "constructor_init",
401
- "label": "Constructor (__init__)",
402
- "sublabel": "def __init__(self, ...):",
403
- "category": "OOP & Classes",
404
- "icon": "Wrench",
405
- "iconColor": "text-indigo-300",
406
- "iconBg": "bg-indigo-700",
407
- "borderColor": "border-indigo-600/40",
408
- "nodeType": "alli.oop.constructor",
409
- "defaultData": {"params": "name, buffer_size=1024", "init_attrs": "self.name = name\nself.buffer_size = buffer_size"},
410
- },
411
- {
412
- "id": "instance_method",
413
- "label": "Instance Method",
414
- "sublabel": "def method(self, ...):",
415
- "category": "OOP & Classes",
416
- "icon": "Workflow",
417
- "iconColor": "text-purple-400",
418
- "iconBg": "bg-purple-600",
419
- "borderColor": "border-purple-500/40",
420
- "nodeType": "alli.oop.method",
421
- "defaultData": {"method_name": "execute", "params": "payload", "is_property": False, "return_expr": "f'{self.name} processed: {payload}'"},
422
- },
423
- {
424
- "id": "instantiate_obj",
425
- "label": "Instantiate Object",
426
- "sublabel": "obj = Class(*args)",
427
- "category": "OOP & Classes",
428
- "icon": "Box",
429
- "iconColor": "text-violet-400",
430
- "iconBg": "bg-violet-600",
431
- "borderColor": "border-violet-500/40",
432
- "nodeType": "alli.oop.instantiate",
433
- "defaultData": {"var_name": "pipeline", "class_name": "DataPipeline", "args": "'Primary', buffer_size=2048"},
434
- },
435
- ],
436
- },
437
- {
438
- "name": "Data Structures",
439
- "items": [
440
- {
441
- "id": "list_struct",
442
- "label": "List / Sequence",
443
- "sublabel": "list[T] (Ordered & Dynamic)",
444
- "category": "Data Structures",
445
- "icon": "ListOrdered",
446
- "iconColor": "text-emerald-400",
447
- "iconBg": "bg-emerald-600",
448
- "borderColor": "border-emerald-500/40",
449
- "nodeType": "alli.ds.list",
450
- "defaultData": {"var_name": "items_list", "operation": "create", "initial_items": "[10, 20, 30, 40]"},
451
- },
452
- {
453
- "id": "dict_struct",
454
- "label": "Dictionary / Map",
455
- "sublabel": "dict[K, V] (Hash Map)",
456
- "category": "Data Structures",
457
- "icon": "Braces",
458
- "iconColor": "text-rose-400",
459
- "iconBg": "bg-rose-600",
460
- "borderColor": "border-rose-500/40",
461
- "nodeType": "alli.ds.dict",
462
- "defaultData": {"var_name": "config_map", "operation": "create", "initial_dict": '{"status": "active", "code": 200}'},
463
- },
464
- {
465
- "id": "tuple_struct",
466
- "label": "Tuple / Record",
467
- "sublabel": "(a, b) (Immutable & Unpack)",
468
- "category": "Data Structures",
469
- "icon": "Layers",
470
- "iconColor": "text-indigo-400",
471
- "iconBg": "bg-indigo-600",
472
- "borderColor": "border-indigo-500/40",
473
- "nodeType": "alli.ds.tuple",
474
- "defaultData": {"var_name": "point_record", "val_a": "100", "val_b": "200"},
475
- },
476
- {
477
- "id": "set_struct",
478
- "label": "Set / Unique",
479
- "sublabel": "set{T} (Unique & Algebra)",
480
- "category": "Data Structures",
481
- "icon": "Boxes",
482
- "iconColor": "text-teal-400",
483
- "iconBg": "bg-teal-600",
484
- "borderColor": "border-teal-500/40",
485
- "nodeType": "alli.ds.set",
486
- "defaultData": {"var_name": "unique_tags", "operation": "create", "initial_set": '{"alpha", "beta", "gamma"}'},
487
- },
488
- {
489
- "id": "comp_struct",
490
- "label": "Comprehension",
491
- "sublabel": "[x * 2 for x in seq]",
492
- "category": "Data Structures",
493
- "icon": "Sparkles",
494
- "iconColor": "text-violet-400",
495
- "iconBg": "bg-violet-600",
496
- "borderColor": "border-violet-500/40",
497
- "nodeType": "alli.ds.comprehension",
498
- "defaultData": {"var_name": "transformed_list", "expr": "x * 2", "iterator": "x", "condition": "x > 0"},
499
- },
500
- {
501
- "id": "stack_queue_struct",
502
- "label": "Stack & Queue",
503
- "sublabel": "LIFO / FIFO Buffer",
504
- "category": "Data Structures",
505
- "icon": "Layers",
506
- "iconColor": "text-cyan-400",
507
- "iconBg": "bg-cyan-600",
508
- "borderColor": "border-cyan-500/40",
509
- "nodeType": "alli.ds.stack_queue",
510
- "defaultData": {"var_name": "data_buffer", "mode": "stack"},
511
- },
512
- {
513
- "id": "sort_search_struct",
514
- "label": "Sort & Reduction",
515
- "sublabel": "sorted(seq) / len / min / max",
516
- "category": "Data Structures",
517
- "icon": "ArrowUpDown",
518
- "iconColor": "text-amber-400",
519
- "iconBg": "bg-amber-600",
520
- "borderColor": "border-amber-500/40",
521
- "nodeType": "alli.ds.sort_search",
522
- "defaultData": {"var_name": "sorted_result", "algo": "sorted", "reverse": False},
523
- },
524
- ],
525
- },
526
- {
527
- "name": "Exception Handling",
528
- "items": [
529
- {
530
- "id": "try_block",
531
- "label": "Try Block",
532
- "sublabel": "try: (Protected)",
533
- "category": "Exception Handling",
534
- "icon": "ShieldAlert",
535
- "iconColor": "text-rose-400",
536
- "iconBg": "bg-rose-600",
537
- "borderColor": "border-rose-500/40",
538
- "nodeType": "alli.exc.try",
539
- "defaultData": {"description": "Protected operations"},
540
- },
541
- {
542
- "id": "except_handler",
543
- "label": "Except Handler",
544
- "sublabel": "except Exception as err:",
545
- "category": "Exception Handling",
546
- "icon": "AlertTriangle",
547
- "iconColor": "text-amber-400",
548
- "iconBg": "bg-amber-600",
549
- "borderColor": "border-amber-500/40",
550
- "nodeType": "alli.exc.except",
551
- "defaultData": {"exception_class": "Exception", "as_var": "err", "fallback_value": "None"},
552
- },
553
- {
554
- "id": "finally_block",
555
- "label": "Finally Block",
556
- "sublabel": "finally: (Cleanup)",
557
- "category": "Exception Handling",
558
- "icon": "CheckCircle2",
559
- "iconColor": "text-slate-400",
560
- "iconBg": "bg-slate-700",
561
- "borderColor": "border-slate-600/40",
562
- "nodeType": "alli.exc.finally",
563
- "defaultData": {"cleanup_comment": "Guaranteed cleanup"},
564
- },
565
- {
566
- "id": "raise_exc",
567
- "label": "Raise Exception",
568
- "sublabel": "raise ValueError(msg)",
569
- "category": "Exception Handling",
570
- "icon": "Flame",
571
- "iconColor": "text-red-400",
572
- "iconBg": "bg-red-600",
573
- "borderColor": "border-red-500/40",
574
- "nodeType": "alli.exc.raise",
575
- "defaultData": {"exception_class": "ValueError", "message": "Invalid parameter configuration supplied"},
576
- },
577
- {
578
- "id": "assert_stmt",
579
- "label": "Assertion",
580
- "sublabel": "assert condition, msg",
581
- "category": "Exception Handling",
582
- "icon": "AlertOctagon",
583
- "iconColor": "text-rose-400",
584
- "iconBg": "bg-rose-700",
585
- "borderColor": "border-rose-600/40",
586
- "nodeType": "alli.exc.assert",
587
- "defaultData": {"condition": "len(items) > 0", "message": "Items collection must not be empty"},
588
- },
589
- ],
590
- },
591
- {
592
- "name": "System & Libraries",
593
- "items": [
594
- {
595
- "id": "sys_import",
596
- "label": "Import Module",
597
- "sublabel": "import math as alias",
598
- "category": "System & Libraries",
599
- "icon": "Package",
600
- "iconColor": "text-cyan-400",
601
- "iconBg": "bg-cyan-700",
602
- "borderColor": "border-cyan-600/40",
603
- "nodeType": "alli.sys.import",
604
- "defaultData": {"module_name": "math", "alias": "math", "imported_items": ""},
605
- },
606
- {
607
- "id": "file_ops",
608
- "label": "File Operations",
609
- "sublabel": "with open(path, mode):",
610
- "category": "System & Libraries",
611
- "icon": "FileText",
612
- "iconColor": "text-emerald-400",
613
- "iconBg": "bg-emerald-600",
614
- "borderColor": "border-emerald-500/40",
615
- "nodeType": "alli.sys.file",
616
- "defaultData": {"var_name": "file_data", "path": "data.txt", "mode": "r", "operation": "read", "encoding": "utf-8"},
617
- },
618
- {
619
- "id": "env_vars",
620
- "label": "Environment Variable",
621
- "sublabel": "os.getenv(key, default)",
622
- "category": "System & Libraries",
623
- "icon": "KeyRound",
624
- "iconColor": "text-slate-300",
625
- "iconBg": "bg-slate-800",
626
- "borderColor": "border-slate-700/40",
627
- "nodeType": "alli.sys.env",
628
- "defaultData": {"var_name": "api_key", "key": "OPENAI_API_KEY", "default_val": "", "is_secret": True},
629
- },
630
- {
631
- "id": "logging_svc",
632
- "label": "Logging Service",
633
- "sublabel": "logging.info(msg)",
634
- "category": "System & Libraries",
635
- "icon": "Activity",
636
- "iconColor": "text-blue-400",
637
- "iconBg": "bg-blue-600",
638
- "borderColor": "border-blue-500/40",
639
- "nodeType": "alli.sys.logging",
640
- "defaultData": {"level": "INFO", "message": "Processing pipeline step initiated"},
641
- },
642
- {
643
- "id": "http_request",
644
- "label": "HTTP Request",
645
- "sublabel": "Call REST API Service",
646
- "category": "System & Libraries",
647
- "icon": "Globe",
648
- "iconColor": "text-cyan-400",
649
- "iconBg": "bg-slate-800",
650
- "borderColor": "border-cyan-500/40",
651
- "nodeType": "requests.get",
652
- "defaultData": {"method": "GET", "url": "https://api.example.com/data", "headers": "{}", "body": "{}"},
653
- },
654
- ],
655
- },
656
- {
657
- "name": "Advanced Execution",
658
- "items": [
659
- {
660
- "id": "async_def",
661
- "label": "Async Function",
662
- "sublabel": "async def coroutine():",
663
- "category": "Advanced Execution",
664
- "icon": "Radio",
665
- "iconColor": "text-cyan-400",
666
- "iconBg": "bg-cyan-600",
667
- "borderColor": "border-cyan-500/40",
668
- "nodeType": "alli.async.def",
669
- "defaultData": {"func_name": "fetch_async_resource", "params": "endpoint", "docstring": "Asynchronously fetches remote resource."},
670
- },
671
- {
672
- "id": "async_await",
673
- "label": "Await Expression",
674
- "sublabel": "res = await coro",
675
- "category": "Advanced Execution",
676
- "icon": "Timer",
677
- "iconColor": "text-cyan-300",
678
- "iconBg": "bg-cyan-700",
679
- "borderColor": "border-cyan-600/40",
680
- "nodeType": "alli.async.await",
681
- "defaultData": {"coroutine_expr": "fetch_async_resource('/api/v1/data')", "target_var": "async_result"},
682
- },
683
- {
684
- "id": "async_task",
685
- "label": "Async Task",
686
- "sublabel": "asyncio.create_task()",
687
- "category": "Advanced Execution",
688
- "icon": "Sparkles",
689
- "iconColor": "text-indigo-400",
690
- "iconBg": "bg-indigo-600",
691
- "borderColor": "border-indigo-500/40",
692
- "nodeType": "alli.async.task",
693
- "defaultData": {"task_var": "bg_task", "task_name": "worker_pipeline", "coroutine_expr": "fetch_async_resource('/stream')"},
694
- },
695
- {
696
- "id": "async_context",
697
- "label": "Async Context",
698
- "sublabel": "async with / async for",
699
- "category": "Advanced Execution",
700
- "icon": "Workflow",
701
- "iconColor": "text-teal-400",
702
- "iconBg": "bg-teal-600",
703
- "borderColor": "border-teal-500/40",
704
- "nodeType": "alli.async.context",
705
- "defaultData": {"statement_type": "async with", "expr": "httpx.AsyncClient()", "var_name": "client"},
706
- },
707
- ],
708
- },
709
- {
710
- "name": "Standard Modules & Libraries",
711
- "items": [
712
- {
713
- "id": "module_math",
714
- "label": "Math Library",
715
- "sublabel": "math.sqrt, pow, pi, ceil",
716
- "category": "Standard Modules & Libraries",
717
- "icon": "Calculator",
718
- "iconColor": "text-emerald-400",
719
- "iconBg": "bg-emerald-600",
720
- "borderColor": "border-emerald-500/40",
721
- "nodeType": "alli.primitive.math",
722
- "defaultData": {"func": "sqrt", "x": "16", "y": "2"},
723
- },
724
- {
725
- "id": "module_datetime",
726
- "label": "Datetime Library",
727
- "sublabel": "datetime.now, strftime",
728
- "category": "Standard Modules & Libraries",
729
- "icon": "Calendar",
730
- "iconColor": "text-cyan-400",
731
- "iconBg": "bg-cyan-600",
732
- "borderColor": "border-cyan-500/40",
733
- "nodeType": "alli.primitive.datetime",
734
- "defaultData": {"operation": "strftime", "format_pattern": "%Y-%m-%d %H:%M:%S"},
735
- },
736
- {
737
- "id": "module_random",
738
- "label": "Random Generator",
739
- "sublabel": "random.randint, choice",
740
- "category": "Standard Modules & Libraries",
741
- "icon": "Dices",
742
- "iconColor": "text-purple-400",
743
- "iconBg": "bg-purple-600",
744
- "borderColor": "border-purple-500/40",
745
- "nodeType": "alli.primitive.random",
746
- "defaultData": {"func": "randint", "min_val": "1", "max_val": "100"},
747
- },
748
- {
749
- "id": "module_json",
750
- "label": "JSON Serialization",
751
- "sublabel": "json.dumps, loads",
752
- "category": "Standard Modules & Libraries",
753
- "icon": "FileJson",
754
- "iconColor": "text-rose-400",
755
- "iconBg": "bg-rose-600",
756
- "borderColor": "border-rose-500/40",
757
- "nodeType": "alli.primitive.json",
758
- "defaultData": {"func": "dumps", "indent": 2},
759
- },
760
- {
761
- "id": "module_time",
762
- "label": "Time & Clock",
763
- "sublabel": "time.ctime, time, sleep",
764
- "category": "Standard Modules & Libraries",
765
- "icon": "Clock",
766
- "iconColor": "text-amber-400",
767
- "iconBg": "bg-amber-600",
768
- "borderColor": "border-amber-500/40",
769
- "nodeType": "alli.primitive.time",
770
- "defaultData": {"func": "ctime"},
771
- },
772
- ],
773
- },
774
- ]
775
-
776
- DEFAULT_STARTER_TEMPLATES = [
777
- {
778
- "id": "langgraph-react-agent",
779
- "name": "LangGraph ReAct Agent",
780
- "title": "LangGraph ReAct Agent",
781
- "description": "Autonomous LLM agent with tool-calling, memory and reasoning loops.",
782
- "category": "ai-agents",
783
- "tags": ["AI Agent", "LangGraph", "Tool Calling"],
784
- "iconType": "robot",
785
- "colorTheme": "purple",
786
- "previewType": "langgraph",
787
- "nodeCount": 18,
788
- "downloads": 1420,
789
- "stars_count": 384,
790
- "starsCount": 384,
791
- "is_published": True,
792
- "is_global": True,
793
- "author_name": "ALLI Team",
794
- "badge": "🔥 Popular",
795
- "details": {
796
- "badge": "🔥 Popular",
797
- "language": "Python",
798
- "skillLevel": "Intermediate",
799
- "nodesUsed": 18,
800
- "estimatedTime": "15 - 25 mins",
801
- "longDescription": "Build an autonomous LLM agent equipped with LangGraph state persistence, introspected python tool execution, and dynamic reflection loops.",
802
- "useCases": [
803
- "Autonomous question answering with live tools",
804
- "Complex reasoning with conversational memory retention",
805
- "Multi-step problem solving with validation checks",
806
- ],
807
- "howItWorks": [
808
- {"step": 1, "text": "Ingest user prompt into conversational memory buffer."},
809
- {"step": 2, "text": "Evaluate prompt via ReAct LLM reasoner node."},
810
- {"step": 3, "text": "Call bound Python tools if actions are required."},
811
- {"step": 4, "text": "Synthesize final response with structured JSON output."},
812
- ],
813
- },
814
- "graph": {
815
- "name": "langgraph_react_agent",
816
- "description": "LangGraph autonomous agent workflow",
817
- "nodes": [
818
- {"id": "node_mem", "node_type": "alli.agent.memory", "position": {"x": 80, "y": 140}, "data": {"label": "Memory"}},
819
- {"id": "node_llm", "node_type": "alli.agent.llm", "position": {"x": 380, "y": 140}, "data": {"label": "AI Reasoner"}},
820
- {"id": "node_tool", "node_type": "alli.agent.tool", "position": {"x": 680, "y": 140}, "data": {"label": "Python Tool"}},
821
- ],
822
- "edges": [
823
- {"id": "e_mem_llm", "source": "node_mem", "target": "node_llm"},
824
- {"id": "e_llm_tool", "source": "node_llm", "target": "node_tool"},
825
- ],
826
- },
827
- },
828
- {
829
- "id": "math-logic-calculator",
830
- "name": "Math & Logic Calculator",
831
- "title": "Math & Logic Calculator",
832
- "description": "Procedural pipeline for mathematical functions, variables and logical operations.",
833
- "category": "math-logic",
834
- "tags": ["Math", "Logic", "Functions"],
835
- "iconType": "calculator",
836
- "colorTheme": "emerald",
837
- "previewType": "math",
838
- "nodeCount": 4,
839
- "downloads": 890,
840
- "stars_count": 195,
841
- "starsCount": 195,
842
- "is_published": True,
843
- "is_global": True,
844
- "author_name": "ALLI Team",
845
- "badge": "Starter",
846
- "details": {
847
- "badge": "Starter",
848
- "language": "Python",
849
- "skillLevel": "Beginner",
850
- "nodesUsed": 4,
851
- "estimatedTime": "10 - 15 mins",
852
- "longDescription": "This template demonstrates basic programming concepts like variables, mathematical operations, and conditional logic. It is perfect for beginners to understand how data flows between nodes.",
853
- "useCases": [
854
- "Learn basic Python programming",
855
- "Understand math operations",
856
- "Practice conditional logic",
857
- "Build a foundation for complex workflows",
858
- ],
859
- "quickPreviewDescription": "A simple workflow showing how the calculator works.",
860
- "howItWorks": [
861
- {"step": 1, "text": "Take a number as input."},
862
- {"step": 2, "text": "Calculate square root using math.sqrt()."},
863
- {"step": 3, "text": "Check if the number is valid (>= 0)."},
864
- {"step": 4, "text": "Display the result or error message."},
865
- ],
866
- "pythonCodeSnippet": "import math\n\ndef calculate_workflow(number: float = 49.0):\n if number >= 0:\n return {'status': 'success', 'result': math.sqrt(number)}\n return {'status': 'error', 'error_message': 'Negative value'}\n",
867
- "dependenciesList": ["math", "typing"],
868
- },
869
- "graph": {
870
- "name": "math_calculator",
871
- "description": "Computes square root of a number and prints result",
872
- "nodes": [
873
- {"id": "node_1", "node_type": "alli.primitive.variable", "position": {"x": 80, "y": 150}, "data": {"var_name": "x", "value": 49.0, "label": "Variable (x)"}},
874
- {"id": "node_2", "node_type": "math.sqrt", "position": {"x": 380, "y": 150}, "data": {"label": "Square Root (∑)"}},
875
- {"id": "node_3", "node_type": "alli.primitive.print", "position": {"x": 680, "y": 150}, "data": {"label": "Output Print"}},
876
- ],
877
- "edges": [
878
- {"id": "e1_2", "source": "node_1", "source_handle": "value", "target": "node_2", "target_handle": "x"},
879
- {"id": "e2_3", "source": "node_2", "source_handle": "result", "target": "node_3", "target_handle": "value"},
880
- ],
881
- },
882
- },
883
- {
884
- "id": "data-etl-transformer",
885
- "name": "Data ETL Transformer",
886
- "title": "Data ETL Transformer",
887
- "description": "Web scraping, API integration and data transformation pipeline.",
888
- "category": "data-etl",
889
- "tags": ["Data", "ETL", "API", "Pandas"],
890
- "iconType": "database",
891
- "colorTheme": "blue",
892
- "previewType": "etl",
893
- "nodeCount": 14,
894
- "downloads": 2150,
895
- "stars_count": 267,
896
- "starsCount": 267,
897
- "is_published": True,
898
- "is_global": True,
899
- "author_name": "ALLI Team",
900
- "badge": "Enterprise",
901
- "details": {
902
- "badge": "Enterprise",
903
- "language": "Python",
904
- "skillLevel": "Intermediate",
905
- "nodesUsed": 14,
906
- "estimatedTime": "20 - 30 mins",
907
- "longDescription": "End-to-end data processing workflow that fetches records from REST endpoints, normalizes JSON structures with Pandas, and exports clean Parquet and CSV files.",
908
- "useCases": [
909
- "Daily API data ingestion and cleansing",
910
- "Automated CSV and JSON schema normalization",
911
- "Database sync pipelines with retry handling",
912
- ],
913
- "howItWorks": [
914
- {"step": 1, "text": "Query REST API endpoint with authentication headers."},
915
- {"step": 2, "text": "Deserialize response payload into structured records."},
916
- {"step": 3, "text": "Apply column mapping and deduplication filters."},
917
- {"step": 4, "text": "Write clean dataset to database storage."},
918
- ],
919
- },
920
- "graph": {
921
- "name": "data_etl",
922
- "description": "REST API to Pandas ETL Pipeline",
923
- "nodes": [
924
- {"id": "node_http", "node_type": "alli.io.http", "position": {"x": 80, "y": 140}, "data": {"label": "REST API"}},
925
- {"id": "node_df", "node_type": "alli.data.pandas", "position": {"x": 380, "y": 140}, "data": {"label": "Pandas Dataframe"}},
926
- {"id": "node_file", "node_type": "alli.io.file", "position": {"x": 680, "y": 140}, "data": {"label": "Parquet Exporter"}},
927
- ],
928
- "edges": [
929
- {"id": "e_http_df", "source": "node_http", "target": "node_df"},
930
- {"id": "e_df_file", "source": "node_df", "target": "node_file"},
931
- ],
932
- },
933
- },
934
- ]
935
-
936
- class MongoWorkflowRepository(WorkflowRepository):
937
- """MongoDB implementation of the workflow graph and user profile repository."""
938
 
939
  def __init__(self, uri: Optional[str] = None, db_name: Optional[str] = None):
940
- self.uri = uri or os.environ.get("MONGODB_URI") or env.MONGODB_URI
941
- self.db_name = db_name or os.environ.get("MONGODB_DB_NAME") or env.MONGODB_DB_NAME
942
- self.client = None
943
- self.collection = None
944
- self.users_collection = None
945
- self._connected_attempted = False
946
- self._is_connected = False
947
- self._memory_store: Dict[str, Dict[str, Any]] = {}
948
- self._blueprint_store: Dict[str, Dict[str, Any]] = {}
949
- self._template_store: Dict[str, Dict[str, Any]] = {
950
- t["id"]: dict(t) for t in DEFAULT_STARTER_TEMPLATES
951
- }
952
- self._user_store: Dict[str, Dict[str, Any]] = {}
953
- self._core_specs_store: Optional[List[Dict[str, Any]]] = None
954
-
955
- @property
956
- def is_connected(self) -> bool:
957
- if not self._connected_attempted:
958
- self._ensure_connected()
959
- return self._is_connected
960
-
961
- @is_connected.setter
962
- def is_connected(self, val: bool):
963
- self._is_connected = val
964
-
965
- def _ensure_connected(self):
966
- if self._connected_attempted:
967
- return self._is_connected
968
- self._connected_attempted = True
969
- if PYMONGO_AVAILABLE and self.uri:
970
- try:
971
- # Fast timeout (1500ms) so requests never block serverless invocations
972
- self.client = MongoClient(self.uri, serverSelectionTimeoutMS=1500, connectTimeoutMS=1500)
973
- self.client.admin.command('ping')
974
- self.collection = self.client[self.db_name]["workflows"]
975
- self.users_collection = self.client[self.db_name]["users"]
976
- try:
977
- self.users_collection.create_index("user_id", unique=True, background=True)
978
- except Exception:
979
- pass
980
- self._is_connected = True
981
- print(f"[ALLI MongoDB] Successfully connected to database '{self.db_name}' (workflows & users collections)")
982
- except Exception as e:
983
- print(f"[ALLI MongoDB] Connection skipped or unavailable ({str(e)}). Using resilient in-memory storage.")
984
- self._is_connected = False
985
- else:
986
- self._is_connected = False
987
- return self._is_connected
988
-
989
- # ─── WORKFLOW CRUD ────────────────────────────────────────────────────────
990
-
991
- def save_workflow(self, workflow_data: Dict[str, Any]) -> str:
992
- wf_id = workflow_data.get("id") or str(uuid.uuid4())
993
- workflow_data["id"] = wf_id
994
- if "created_at" not in workflow_data or not workflow_data["created_at"]:
995
- workflow_data["created_at"] = get_utc_now()
996
- workflow_data["updated_at"] = get_utc_now()
997
- if "schema_version" not in workflow_data or not workflow_data["schema_version"]:
998
- workflow_data["schema_version"] = "2.1.0"
999
- if "viewport" not in workflow_data or not workflow_data["viewport"]:
1000
- workflow_data["viewport"] = {"x": 0, "y": 0, "zoom": 1.0}
1001
- if "nodes" not in workflow_data:
1002
- workflow_data["nodes"] = []
1003
- if "edges" not in workflow_data:
1004
- workflow_data["edges"] = []
1005
-
1006
- # Track template usage if this project is created from or cloned from a starter template
1007
- source_tmpl = workflow_data.get("source_template_id")
1008
- user_id = workflow_data.get("user_id")
1009
- if source_tmpl:
1010
- self.track_template_use(source_tmpl, user_id)
1011
-
1012
- if self.is_connected and self.collection is not None:
1013
- try:
1014
- self.collection.update_one(
1015
- {"id": wf_id},
1016
- {"$set": workflow_data},
1017
- upsert=True
1018
- )
1019
- return wf_id
1020
- except Exception as e:
1021
- print(f"[ALLI MongoDB] Save error: {e}, falling back to memory.")
1022
-
1023
- self._memory_store[wf_id] = workflow_data
1024
- return wf_id
1025
-
1026
- def get_workflow(self, workflow_id: str) -> Optional[Dict[str, Any]]:
1027
- from language_core.workflow_migration import migrate_workflow
1028
- if self.is_connected and self.collection is not None:
1029
- try:
1030
- doc = self.collection.find_one({"id": workflow_id}, {"_id": 0})
1031
- if doc:
1032
- return migrate_workflow(doc)
1033
- except Exception as e:
1034
- print(f"[ALLI MongoDB] Get error: {e}")
1035
-
1036
- mem = self._memory_store.get(workflow_id)
1037
- return migrate_workflow(mem) if mem else None
1038
-
1039
- def list_workflows(self, user_id: Optional[str] = None) -> List[Dict[str, Any]]:
1040
- query = {}
1041
- if user_id:
1042
- query["user_id"] = user_id
1043
-
1044
- if self.is_connected and self.collection is not None:
1045
- try:
1046
- docs = list(self.collection.find(query, {
1047
- "_id": 0,
1048
- "id": 1,
1049
- "name": 1,
1050
- "description": 1,
1051
- "schema_version": 1,
1052
- "dependencies": 1,
1053
- "source_template_id": 1,
1054
- "user_id": 1,
1055
- "is_starred": 1,
1056
- "is_archived": 1,
1057
- "created_at": 1,
1058
- "updated_at": 1,
1059
- "viewport": 1,
1060
- "user_id": 1,
1061
- "nodes": 1,
1062
- "edges": 1,
1063
- "is_starred": 1,
1064
- "is_archived": 1,
1065
- "source_template_id": 1,
1066
- }))
1067
- return docs
1068
- except Exception as e:
1069
- print(f"[ALLI MongoDB] List error: {e}")
1070
-
1071
- return [
1072
- {
1073
- "id": k,
1074
- "name": v.get("name", "Untitled Workflow"),
1075
- "description": v.get("description", ""),
1076
- "schema_version": v.get("schema_version", "2.1.0"),
1077
- "dependencies": v.get("dependencies", []),
1078
- "source_template_id": v.get("source_template_id"),
1079
- "user_id": v.get("user_id"),
1080
- "is_starred": v.get("is_starred", False),
1081
- "is_archived": v.get("is_archived", False),
1082
- "created_at": v.get("created_at", ""),
1083
- "updated_at": v.get("updated_at", ""),
1084
- "viewport": v.get("viewport", {"x": 0, "y": 0, "zoom": 1.0}),
1085
- "user_id": v.get("user_id"),
1086
- "node_count": len(v.get("nodes", [])),
1087
- }
1088
- for k, v in self._memory_store.items()
1089
- if not user_id or v.get("user_id") == user_id
1090
- ]
1091
-
1092
- def delete_workflow(self, workflow_id: str) -> bool:
1093
- deleted = False
1094
- if self.is_connected and self.collection is not None:
1095
- try:
1096
- res = self.collection.delete_one({"id": workflow_id})
1097
- deleted = res.deleted_count > 0
1098
- except Exception as e:
1099
- print(f"[ALLI MongoDB] Delete error: {e}")
1100
-
1101
- if workflow_id in self._memory_store:
1102
- del self._memory_store[workflow_id]
1103
- deleted = True
1104
-
1105
- return deleted
1106
-
1107
- # ─── BLUEPRINT CACHE (ZERO DISK) ─────────────────────────────────────────
1108
-
1109
- @staticmethod
1110
- def _parse_package_and_version(package_name: str, version: Optional[str] = None) -> tuple:
1111
- """Extracts base package name, explicit version, and composite key."""
1112
- raw = package_name.lower().strip()
1113
- ver = version.strip() if version else None
1114
-
1115
- if "@" in raw:
1116
- base, v = raw.split("@", 1)
1117
- base = base.strip()
1118
- if not ver:
1119
- ver = v.strip()
1120
- elif "==" in raw:
1121
- base, v = raw.split("==", 1)
1122
- base = base.strip()
1123
- if not ver:
1124
- ver = v.strip()
1125
- else:
1126
- base = raw.split(">=")[0].split("<=")[0].split("~=")[0].strip()
1127
-
1128
- composite = f"{base}@{ver}" if ver else base
1129
- return base, ver, composite
1130
-
1131
- def get_blueprint(self, package_name: str, version: Optional[str] = None) -> Optional[Dict[str, Any]]:
1132
- """Retrieves cached package blueprint from MongoDB Atlas or memory (zero disk access)."""
1133
- base_name, ver, composite_key = self._parse_package_and_version(package_name, version)
1134
-
1135
- lookup_keys = []
1136
- if ver:
1137
- lookup_keys.append(composite_key)
1138
- if composite_key != base_name:
1139
- lookup_keys.append(base_name)
1140
- else:
1141
- lookup_keys.append(base_name)
1142
-
1143
- if self.is_connected and self.client is not None:
1144
- try:
1145
- bp_col = self.client[self.db_name]["blueprints"]
1146
- for k in lookup_keys:
1147
- doc = bp_col.find_one({"package": k}, {"_id": 0})
1148
- if doc and doc.get("blueprint"):
1149
- bp = doc.get("blueprint")
1150
- if k == base_name and ver:
1151
- bp_ver = (
1152
- bp.get("package", {}).get("version")
1153
- if isinstance(bp.get("package"), dict)
1154
- else bp.get("version")
1155
- )
1156
- if bp_ver and str(bp_ver).strip() != ver:
1157
- continue
1158
- return bp
1159
- except Exception as e:
1160
- print(f"[ALLI MongoDB] Blueprint fetch error: {e}")
1161
-
1162
- for k in lookup_keys:
1163
- if k in self._blueprint_store:
1164
- bp = self._blueprint_store[k]
1165
- if k == base_name and ver:
1166
- bp_ver = (
1167
- bp.get("package", {}).get("version")
1168
- if isinstance(bp.get("package"), dict)
1169
- else bp.get("version")
1170
- )
1171
- if bp_ver and str(bp_ver).strip() != ver:
1172
- continue
1173
- return bp
1174
-
1175
- return None
1176
-
1177
- def save_blueprint(self, package_name: str, blueprint: Dict[str, Any], version: Optional[str] = None):
1178
- """Persists package blueprint directly to MongoDB Atlas or memory under composite and fallback keys."""
1179
- base_name, ver, composite_key = self._parse_package_and_version(package_name, version)
1180
-
1181
- if not ver:
1182
- bp_ver = (
1183
- blueprint.get("package", {}).get("version")
1184
- if isinstance(blueprint.get("package"), dict)
1185
- else blueprint.get("version")
1186
- )
1187
- if bp_ver and str(bp_ver).strip() not in ("unknown", "latest", "", "None"):
1188
- ver = str(bp_ver).strip()
1189
- composite_key = f"{base_name}@{ver}"
1190
-
1191
- keys_to_save = {base_name}
1192
- if ver:
1193
- keys_to_save.add(composite_key)
1194
-
1195
- now = get_utc_now()
1196
- if self.is_connected and self.client is not None:
1197
- try:
1198
- bp_col = self.client[self.db_name]["blueprints"]
1199
- for k in keys_to_save:
1200
- bp_col.update_one(
1201
- {"package": k},
1202
- {"$set": {"package": k, "blueprint": blueprint, "updated_at": now}},
1203
- upsert=True
1204
- )
1205
- except Exception as e:
1206
- print(f"[ALLI MongoDB] Blueprint save error: {e}")
1207
-
1208
- for k in keys_to_save:
1209
- self._blueprint_store[k] = blueprint
1210
-
1211
- def list_blueprints(self) -> List[Dict[str, Any]]:
1212
- """Lists all package blueprints stored in MongoDB Atlas, merged with defaults."""
1213
- packages_dict = {p["name"].lower(): dict(p) for p in DEFAULT_PACKAGES}
1214
-
1215
- if self.is_connected and self.client is not None:
1216
- try:
1217
- bp_col = self.client[self.db_name]["blueprints"]
1218
- for doc in bp_col.find({}, {"_id": 0, "package": 1, "blueprint": 1, "updated_at": 1}):
1219
- pkg_name = doc.get("package", "")
1220
- bp = doc.get("blueprint", {})
1221
- if pkg_name:
1222
- packages_dict[pkg_name.lower()] = {
1223
- "name": pkg_name,
1224
- "type": "external",
1225
- "version": bp.get("version", "latest"),
1226
- "summary": bp.get("docstring") or f"Extracted blueprint for {pkg_name}",
1227
- "nodeCount": len(bp.get("nodes", [])) or len(bp.get("functions", [])) or 10,
1228
- "inDB": True,
1229
- "categories": ["Extracted", "MongoDB Atlas"],
1230
- "updated_at": doc.get("updated_at"),
1231
- }
1232
- except Exception as e:
1233
- print(f"[ALLI MongoDB] Blueprint list error: {e}")
1234
-
1235
- # Merge memory store
1236
- for pkg_name, bp in self._blueprint_store.items():
1237
- if pkg_name.lower() not in packages_dict:
1238
- packages_dict[pkg_name.lower()] = {
1239
- "name": pkg_name,
1240
- "type": "external",
1241
- "version": "cached",
1242
- "summary": f"In-memory blueprint for {pkg_name}",
1243
- "nodeCount": len(bp.get("nodes", [])) if isinstance(bp, dict) else 10,
1244
- "inDB": True,
1245
- "categories": ["Extracted", "Memory"],
1246
- }
1247
-
1248
- return list(packages_dict.values())
1249
-
1250
- def get_core_specs(self) -> List[Dict[str, Any]]:
1251
- """Fetches dynamic Python core language specifications from MongoDB Atlas."""
1252
- if self.is_connected and self.client is not None:
1253
- try:
1254
- col = self.client[self.db_name]["core_definitions"]
1255
- doc = col.find_one({"key": "python_core_categories"}, {"_id": 0})
1256
- if doc and "categories" in doc:
1257
- return doc["categories"]
1258
- except Exception as e:
1259
- print(f"[ALLI MongoDB] Core specs fetch error: {e}")
1260
- return self._core_specs_store if self._core_specs_store else []
1261
-
1262
- def save_core_specs(self, categories: List[Dict[str, Any]]) -> bool:
1263
- """Persists custom or updated Python core specifications into MongoDB Atlas."""
1264
- self._core_specs_store = categories
1265
- if self.is_connected and self.client is not None:
1266
- try:
1267
- col = self.client[self.db_name]["core_definitions"]
1268
- col.update_one(
1269
- {"key": "python_core_categories"},
1270
- {"$set": {"key": "python_core_categories", "categories": categories, "updated_at": get_utc_now()}},
1271
- upsert=True
1272
- )
1273
- except Exception as e:
1274
- print(f"[ALLI MongoDB] Core specs save error: {e}")
1275
- return False
1276
-
1277
- # ─── PYTHON FUNDAMENTALS CATALOG (MONGODB ATLAS) ─────────────────────────
1278
-
1279
- def get_python_fundamentals(self) -> Dict[str, Any]:
1280
- """Fetches dynamic Python fundamentals schema from MongoDB Atlas collection 'python_fundamentals' or fallback specs file."""
1281
- if self.is_connected and self.client is not None:
1282
- try:
1283
- col = self.client[self.db_name]["python_fundamentals"]
1284
- doc = col.find_one({"key": "python_fundamentals_schema"}, {"_id": 0})
1285
- if doc and "constructs" in doc:
1286
- return doc
1287
- except Exception as e:
1288
- print(f"[ALLI MongoDB] Python fundamentals fetch error: {e}")
1289
-
1290
- # Fallback to backend specs file
1291
- spec_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "specs", "python_fundamentals.json")
1292
- if os.path.exists(spec_path):
1293
- try:
1294
- with open(spec_path, "r", encoding="utf-8") as f:
1295
- return json.load(f)
1296
- except Exception as e:
1297
- print(f"[ALLI Specs] Could not read python_fundamentals.json: {e}")
1298
-
1299
- return {"version": "1.0.0", "constructs": []}
1300
-
1301
- def save_python_fundamentals(self, data: Dict[str, Any]) -> bool:
1302
- """Persists Python fundamentals schema into MongoDB Atlas collection 'python_fundamentals'."""
1303
- if self.is_connected and self.client is not None:
1304
- try:
1305
- col = self.client[self.db_name]["python_fundamentals"]
1306
- col.update_one(
1307
- {"key": "python_fundamentals_schema"},
1308
- {"$set": {**data, "key": "python_fundamentals_schema", "updated_at": get_utc_now()}},
1309
- upsert=True
1310
- )
1311
- return True
1312
- except Exception as e:
1313
- print(f"[ALLI MongoDB] Python fundamentals save error: {e}")
1314
- return False
1315
-
1316
- # ─── TEMPLATES CATALOG (MONGODB ATLAS) ───────────────────────────────────
1317
-
1318
- def save_template(self, template_data: Dict[str, Any]) -> str:
1319
- """Persists a published global template into MongoDB Atlas."""
1320
- t_id = template_data.get("id") or f"tmpl_{uuid.uuid4().hex[:12]}"
1321
- template_data["id"] = t_id
1322
- template_data["updated_at"] = get_utc_now()
1323
- template_data["is_global"] = True
1324
- template_data["is_published"] = True
1325
-
1326
- if self.is_connected and self.client is not None:
1327
- try:
1328
- col = self.client[self.db_name]["templates"]
1329
- col.update_one({"id": t_id}, {"$set": template_data}, upsert=True)
1330
- return t_id
1331
- except Exception as e:
1332
- print(f"[ALLI MongoDB] Template save error: {e}")
1333
-
1334
- self._template_store[t_id] = template_data
1335
- return t_id
1336
-
1337
- def list_templates(self) -> List[Dict[str, Any]]:
1338
- """Lists all published global templates from MongoDB Atlas."""
1339
- templates = []
1340
- if self.is_connected and self.client is not None:
1341
- try:
1342
- col = self.client[self.db_name]["templates"]
1343
- docs = list(col.find({}, {"_id": 0}))
1344
- templates.extend(docs)
1345
- except Exception as e:
1346
- print(f"[ALLI MongoDB] Template list error: {e}")
1347
-
1348
- for t_id, t_data in self._template_store.items():
1349
- if not any(d.get("id") == t_id for d in templates):
1350
- templates.append(t_data)
1351
-
1352
- return templates
1353
-
1354
- def get_template(self, template_id: str) -> Optional[Dict[str, Any]]:
1355
- """Retrieves a specific published template by ID with alias fallback."""
1356
- t_id = template_id.lower().strip()
1357
- alias_map = {
1358
- "math-calculator": "math-logic-calculator",
1359
- "math_pipeline": "math-logic-calculator",
1360
- "react_agent": "langgraph-react-agent",
1361
- "data_pipeline": "data-etl-transformer",
1362
- }
1363
- target_id = alias_map.get(t_id, t_id)
1364
-
1365
- if self.is_connected and self.client is not None:
1366
- try:
1367
- col = self.client[self.db_name]["templates"]
1368
- doc = col.find_one({"id": target_id}, {"_id": 0})
1369
- if doc:
1370
- return doc
1371
- except Exception as e:
1372
- print(f"[ALLI MongoDB] Template fetch error: {e}")
1373
-
1374
- return self._template_store.get(target_id) or self._template_store.get(template_id)
1375
-
1376
- def track_template_use(self, template_id: str, user_id: Optional[str] = None) -> int:
1377
- """Increments usage/downloads count for a template in MongoDB Atlas and tracks on user document."""
1378
- t_id = template_id.lower().strip()
1379
- alias_map = {
1380
- "math-calculator": "math-logic-calculator",
1381
- "math_pipeline": "math-logic-calculator",
1382
- "react_agent": "langgraph-react-agent",
1383
- "data_pipeline": "data-etl-transformer",
1384
- }
1385
- target_id = alias_map.get(t_id, t_id)
1386
- new_count = 1
1387
-
1388
- if self.is_connected and self.client is not None:
1389
- try:
1390
- col = self.client[self.db_name]["templates"]
1391
- res = col.find_one_and_update(
1392
- {"id": target_id},
1393
- {"$inc": {"downloads": 1, "used_count": 1}},
1394
- return_document=True,
1395
- )
1396
- if res:
1397
- new_count = res.get("downloads", 1)
1398
-
1399
- if user_id and self.users_collection is not None:
1400
- self.users_collection.update_one(
1401
- {"user_id": user_id},
1402
- {"$addToSet": {"templates_used": target_id}, "$inc": {"templates_used_count": 1}},
1403
- upsert=True,
1404
- )
1405
- return new_count
1406
- except Exception as e:
1407
- print(f"[ALLI MongoDB] track_template_use error: {e}")
1408
-
1409
- # In-memory fallback
1410
- if target_id in self._template_store:
1411
- self._template_store[target_id]["downloads"] = self._template_store[target_id].get("downloads", 0) + 1
1412
- new_count = self._template_store[target_id]["downloads"]
1413
-
1414
- if user_id:
1415
- u_data = self._user_store.setdefault(user_id, {"user_id": user_id, "templates_used": [], "templates_used_count": 0})
1416
- if target_id not in u_data.get("templates_used", []):
1417
- u_data.setdefault("templates_used", []).append(target_id)
1418
- u_data["templates_used_count"] = u_data.get("templates_used_count", 0) + 1
1419
-
1420
- return new_count
1421
-
1422
- def toggle_template_star(self, template_id: str, user_id: Optional[str] = None) -> Dict[str, Any]:
1423
- """Toggles a user's star for a template and atomically increments/decrements stars_count."""
1424
- t_id = template_id.lower().strip()
1425
- alias_map = {
1426
- "math-calculator": "math-logic-calculator",
1427
- "math_pipeline": "math-logic-calculator",
1428
- "react_agent": "langgraph-react-agent",
1429
- "data_pipeline": "data-etl-transformer",
1430
- }
1431
- target_id = alias_map.get(t_id, t_id)
1432
-
1433
- is_starred = True
1434
- stars_count = 0
1435
-
1436
- # Update in Mongo if connected
1437
- if self.is_connected and self.client is not None:
1438
- try:
1439
- tmpl_col = self.client[self.db_name]["templates"]
1440
- user_col = self.users_collection
1441
-
1442
- if user_id and user_col is not None:
1443
- user_doc = user_col.find_one({"user_id": user_id})
1444
- starred_list = user_doc.get("starred_templates", []) if user_doc else []
1445
- if target_id in starred_list:
1446
- user_col.update_one({"user_id": user_id}, {"$pull": {"starred_templates": target_id}})
1447
- tmpl_doc = tmpl_col.find_one_and_update(
1448
- {"id": target_id},
1449
- {"$inc": {"stars_count": -1, "starsCount": -1}},
1450
- return_document=True
1451
- )
1452
- is_starred = False
1453
- else:
1454
- user_col.update_one({"user_id": user_id}, {"$addToSet": {"starred_templates": target_id}})
1455
- tmpl_doc = tmpl_col.find_one_and_update(
1456
- {"id": target_id},
1457
- {"$inc": {"stars_count": 1, "starsCount": 1}},
1458
- return_document=True
1459
- )
1460
- is_starred = True
1461
-
1462
- if tmpl_doc:
1463
- stars_count = max(0, tmpl_doc.get("stars_count") or tmpl_doc.get("starsCount") or 0)
1464
- except Exception as e:
1465
- print(f"[ALLI MongoDB] toggle_template_star error: {e}")
1466
-
1467
- # In-memory fallback
1468
- t_data = self._template_store.get(target_id)
1469
- if t_data:
1470
- current_stars = t_data.get("stars_count") or t_data.get("starsCount") or 0
1471
- if user_id:
1472
- u_data = self._user_store.setdefault(user_id, {"user_id": user_id, "starred_templates": []})
1473
- starred_list = u_data.setdefault("starred_templates", [])
1474
- if target_id in starred_list:
1475
- starred_list.remove(target_id)
1476
- current_stars = max(0, current_stars - 1)
1477
- is_starred = False
1478
- else:
1479
- starred_list.append(target_id)
1480
- current_stars += 1
1481
- is_starred = True
1482
- else:
1483
- current_stars += 1
1484
- is_starred = True
1485
- t_data["stars_count"] = current_stars
1486
- t_data["starsCount"] = current_stars
1487
- stars_count = current_stars
1488
-
1489
- return {
1490
- "template_id": target_id,
1491
- "is_starred": is_starred,
1492
- "stars_count": stars_count,
1493
- }
1494
-
1495
- # ─── USER PROFILE MANAGEMENT (MONGODB ATLAS) ─────────────────────────────
1496
-
1497
- def get_user_profile(self, user_id: str) -> Optional[Dict[str, Any]]:
1498
- """Fetches user profile document from MongoDB Atlas."""
1499
- if self.is_connected and self.users_collection is not None:
1500
- try:
1501
- doc = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
1502
- if doc:
1503
- return doc
1504
- except Exception as e:
1505
- print(f"[ALLI MongoDB] Profile fetch error: {e}")
1506
-
1507
- return self._user_store.get(user_id)
1508
-
1509
- def upsert_user_profile(self, user_id: str, profile_data: Dict[str, Any]) -> Dict[str, Any]:
1510
- """Inserts or updates user profile document in MongoDB Atlas without clobbering subscription."""
1511
- now = get_utc_now()
1512
- profile_data["user_id"] = user_id
1513
-
1514
- # Check if user document already exists in DB or in-memory fallback
1515
- existing = None
1516
- if self.is_connected and self.users_collection is not None:
1517
- try:
1518
- existing = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
1519
- except Exception as e:
1520
- print(f"[ALLI MongoDB] Profile pre-fetch error: {e}")
1521
-
1522
- if not existing and user_id in self._user_store:
1523
- existing = self._user_store[user_id]
1524
-
1525
- if existing:
1526
- # Preserve existing subscription, stats, starred_templates, and created_at
1527
- updated = dict(existing)
1528
- for k, v in profile_data.items():
1529
- if v is not None and k not in ("created_at",):
1530
- # Do not overwrite subscription if incoming data does not explicitly provide it
1531
- if k == "subscription" and not v:
1532
- continue
1533
- updated[k] = v
1534
-
1535
- # Explicitly keep the existing subscription unless explicitly provided in profile_data
1536
- if "subscription" not in profile_data and "subscription" in existing:
1537
- updated["subscription"] = existing["subscription"]
1538
-
1539
- updated["updated_at"] = now
1540
-
1541
- if self.is_connected and self.users_collection is not None:
1542
- try:
1543
- self.users_collection.update_one(
1544
- {"user_id": user_id},
1545
- {"$set": updated}
1546
- )
1547
- doc = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
1548
- if doc:
1549
- return doc
1550
- except Exception as e:
1551
- print(f"[ALLI MongoDB] Profile update error: {e}")
1552
-
1553
- self._user_store[user_id] = updated
1554
- return updated
1555
- else:
1556
- # Brand new user: establish full document with Free tier defaults
1557
- profile_data.setdefault("created_at", now)
1558
- profile_data["updated_at"] = now
1559
- profile_data.setdefault("role", "developer")
1560
- profile_data.setdefault("subscription", {
1561
- "plan": "free",
1562
- "status": "active",
1563
- "quota_limit": 100,
1564
- "quota_used": 0,
1565
- })
1566
- profile_data.setdefault("stats", {
1567
- "successful_runs": 0,
1568
- })
1569
- profile_data.setdefault("starred_templates", [])
1570
- profile_data.setdefault("preferences", {
1571
- "theme": "dark",
1572
- "auto_compile": True,
1573
- "auto_save": True,
1574
- })
1575
-
1576
- if self.is_connected and self.users_collection is not None:
1577
- try:
1578
- self.users_collection.update_one(
1579
- {"user_id": user_id},
1580
- {"$set": profile_data},
1581
- upsert=True
1582
- )
1583
- doc = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
1584
- if doc:
1585
- return doc
1586
- except Exception as e:
1587
- print(f"[ALLI MongoDB] Profile upsert error: {e}")
1588
-
1589
- self._user_store[user_id] = profile_data
1590
- return profile_data
1591
-
1592
- def update_user_profile(self, user_id: str, updates: Dict[str, Any]) -> Optional[Dict[str, Any]]:
1593
- """Partially updates fields in an existing user profile."""
1594
- now = get_utc_now()
1595
- updates["updated_at"] = now
1596
-
1597
- if self.is_connected and self.users_collection is not None:
1598
- try:
1599
- self.users_collection.update_one(
1600
- {"user_id": user_id},
1601
- {"$set": updates}
1602
- )
1603
- return self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
1604
- except Exception as e:
1605
- print(f"[ALLI MongoDB] Profile update error: {e}")
1606
-
1607
- if user_id in self._user_store:
1608
- self._user_store[user_id].update(updates)
1609
- return self._user_store[user_id]
1610
-
1611
- return None
1612
-
1613
- def update_user_quota(self, user_id: str, increment: int = 1) -> Dict[str, Any]:
1614
- """Atomically increments DAG execution run quota in MongoDB Atlas."""
1615
- now = get_utc_now()
1616
- if self.is_connected and self.users_collection is not None:
1617
- try:
1618
- self.users_collection.update_one(
1619
- {"user_id": user_id},
1620
- {
1621
- "$inc": {"subscription.quota_used": increment},
1622
- "$set": {"updated_at": now}
1623
- }
1624
- )
1625
- doc = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
1626
- if doc:
1627
- return doc.get("subscription", {})
1628
- except Exception as e:
1629
- print(f"[ALLI MongoDB] Quota increment error: {e}")
1630
-
1631
- user = self._user_store.get(user_id, {})
1632
- sub = user.get("subscription", {"plan": "pro", "status": "active", "quota_limit": 100, "quota_used": 0})
1633
- sub["quota_used"] = sub.get("quota_used", 0) + increment
1634
- user["subscription"] = sub
1635
- self._user_store[user_id] = user
1636
- return sub
1637
-
1638
- def record_successful_run(self, user_id: str) -> int:
1639
- """Atomically increments successful DAG run count for a user in MongoDB Atlas."""
1640
- now = get_utc_now()
1641
- if self.is_connected and self.users_collection is not None:
1642
- try:
1643
- self.users_collection.update_one(
1644
- {"user_id": user_id},
1645
- {
1646
- "$inc": {"stats.successful_runs": 1},
1647
- "$set": {"updated_at": now}
1648
- }
1649
- )
1650
- doc = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
1651
- if doc and "stats" in doc:
1652
- return doc["stats"].get("successful_runs", 1)
1653
- except Exception as e:
1654
- print(f"[ALLI MongoDB] record_successful_run error: {e}")
1655
-
1656
- u_data = self._user_store.setdefault(user_id, {"user_id": user_id, "stats": {}})
1657
- stats = u_data.setdefault("stats", {})
1658
- stats["successful_runs"] = stats.get("successful_runs", 0) + 1
1659
- return stats["successful_runs"]
1660
-
1661
- def get_user_stats(self, user_id: Optional[str] = None) -> Dict[str, Any]:
1662
- """Calculates accurate runtime statistics including projects, templates, and quota."""
1663
- wf_list = self.list_workflows(user_id=user_id) if user_id else (self.list_workflows() or list(self._memory_store.values()))
1664
- total_projects = len(wf_list)
1665
- starter_templates = {
1666
- "math & logic calculator",
1667
- "math logic calculator",
1668
- "math calculation pipeline",
1669
- "data etl transformer",
1670
- "langgraph react agent",
1671
- "pdf knowledge agent",
1672
- "web scraper flow",
1673
- }
1674
- project_templates_count = sum(
1675
- 1 for w in wf_list
1676
- if w.get("source_template_id") or (w.get("name") and str(w.get("name")).lower().strip() in starter_templates)
1677
- )
1678
- templates_used = project_templates_count
1679
- runs_used = 0
1680
- runs_limit = 100
1681
- successful_runs = 0
1682
-
1683
- if user_id and self.is_connected and self.users_collection is not None:
1684
- try:
1685
- user_doc = self.users_collection.find_one({"user_id": user_id})
1686
- if user_doc:
1687
- if "templates_used" in user_doc and isinstance(user_doc["templates_used"], list):
1688
- templates_used = max(templates_used, len(user_doc["templates_used"]))
1689
- elif "templates_used_count" in user_doc:
1690
- templates_used = max(templates_used, user_doc["templates_used_count"])
1691
-
1692
- sub = user_doc.get("subscription", {})
1693
- runs_used = sub.get("quota_used", 0)
1694
- runs_limit = sub.get("quota_limit", 100)
1695
- successful_runs = user_doc.get("stats", {}).get("successful_runs", 0)
1696
- except Exception as e:
1697
- print(f"[ALLI MongoDB] user stats error: {e}")
1698
-
1699
- if user_id and user_id in self._user_store:
1700
- mem_user = self._user_store[user_id]
1701
- if templates_used == 0:
1702
- templates_used = max(templates_used, len(mem_user.get("templates_used", [])))
1703
- sub = mem_user.get("subscription", {})
1704
- if runs_used == 0 and "quota_used" in sub:
1705
- runs_used = sub.get("quota_used", 0)
1706
- runs_limit = sub.get("quota_limit", 100)
1707
- if successful_runs == 0:
1708
- successful_runs = mem_user.get("stats", {}).get("successful_runs", 0)
1709
-
1710
- return {
1711
- "totalProjects": total_projects,
1712
- "templatesUsed": templates_used,
1713
- "runsUsed": runs_used,
1714
- "runsLimit": runs_limit,
1715
- "successfulRuns": successful_runs,
1716
- }
1717
-
1718
- def get_pricing_plans(self) -> List[Dict[str, Any]]:
1719
- """Retrieves subscription plans from MongoDB Atlas 'plans' collection with auto-seeding."""
1720
- default_plans = [
1721
- {
1722
- "id": "free",
1723
- "name": "Free",
1724
- "badge": "All Features Unlocked",
1725
- "description": "Full access to all visual programming features, AI agent blueprints, AST introspection, and Python synthesis.",
1726
- "monthly_price": 0,
1727
- "yearly_price": 0,
1728
- "display_monthly": "$0",
1729
- "display_yearly": "$0",
1730
- "quota_limit": 100,
1731
- "is_popular": False,
1732
- "features": [
1733
- "Complete access to ALL 50+ visual nodes",
1734
- "Full library of starter & AI templates",
1735
- "LangGraph & ReAct agent blueprints",
1736
- "Dynamic PyPI reflection & AST inspection",
1737
- "Clean Python PEP 8 code synthesis",
1738
- "1-Click Dockerfile & requirements export",
1739
- "100 DAG execution runs / month included",
1740
- "Community forums & interactive docs",
1741
- ],
1742
- },
1743
- {
1744
- "id": "pro",
1745
- "name": "Pro",
1746
- "badge": "Most Popular",
1747
- "description": "For individual developers and creators building automated workflows.",
1748
- "monthly_price": 50,
1749
- "yearly_price": 40,
1750
- "display_monthly": "$50",
1751
- "display_yearly": "$40",
1752
- "quota_limit": 500,
1753
- "is_popular": True,
1754
- "highlight": True,
1755
- "features": [
1756
- "500 DAG runs / month",
1757
- "Complete core & math node library",
1758
- "LangGraph & ReAct agent blueprints",
1759
- "Dynamic PyPI reflection & AST inspection",
1760
- "Air-gapped secure sandbox isolation",
1761
- "Priority runtime queue",
1762
- ],
1763
- },
1764
- {
1765
- "id": "team",
1766
- "name": "Team",
1767
- "badge": "Collaboration",
1768
- "description": "For teams and organizations requiring collaborative workflows.",
1769
- "monthly_price": 100,
1770
- "yearly_price": 80,
1771
- "display_monthly": "$100",
1772
- "display_yearly": "$80",
1773
- "quota_limit": 2000,
1774
- "is_popular": False,
1775
- "features": [
1776
- "2,000 DAG runs / month",
1777
- "Shared team blueprints & templates",
1778
- "Multi-member project collaboration",
1779
- "Extended execution timeout (up to 30s)",
1780
- "Automated MongoDB Atlas project backups",
1781
- "Priority developer support",
1782
- ],
1783
- },
1784
- {
1785
- "id": "enterprise",
1786
- "name": "Enterprise",
1787
- "badge": "Enterprise Scale",
1788
- "description": "For high-throughput pipelines, custom packages, and dedicated clusters.",
1789
- "monthly_price": 100,
1790
- "yearly_price": 80,
1791
- "display_monthly": "$100",
1792
- "display_yearly": "$80",
1793
- "quota_limit": 10000,
1794
- "is_popular": False,
1795
- "features": [
1796
- "10,000+ DAG runs / month",
1797
- "Custom private Python package registries",
1798
- "Dedicated isolated sandbox cluster",
1799
- "Custom SLA & uptime guarantee",
1800
- "Direct Slack/Discord engineering channel",
1801
- "Dedicated technical account manager",
1802
- ],
1803
- },
1804
- ]
1805
-
1806
- if self.is_connected and self.client is not None:
1807
- try:
1808
- col = self.client[self.db_name]["plans"]
1809
- for p in default_plans:
1810
- col.update_one({"id": p["id"]}, {"$set": p}, upsert=True)
1811
- docs = list(col.find({}, {"_id": 0}))
1812
- if docs:
1813
- return docs
1814
- except Exception as e:
1815
- print(f"[ALLI MongoDB] Error fetching plans from DB: {e}")
1816
-
1817
- return default_plans
1818
-
1819
- def update_user_plan(self, user_id: str, plan_id: str, billing_period: str = "monthly") -> Dict[str, Any]:
1820
- """Updates user's subscription plan and adjusts quota limit in MongoDB Atlas."""
1821
- p_id = plan_id.lower().strip()
1822
- plans = {p["id"]: p for p in self.get_pricing_plans()}
1823
- target_plan = plans.get(p_id, {"id": p_id, "quota_limit": 100})
1824
- now = get_utc_now()
1825
-
1826
- sub = {
1827
- "plan": target_plan["id"],
1828
- "status": "active",
1829
- "quota_limit": target_plan.get("quota_limit", 100),
1830
- "billing_period": billing_period,
1831
- "updated_at": now,
1832
- }
1833
-
1834
- if self.is_connected and self.users_collection is not None:
1835
- try:
1836
- self.users_collection.update_one(
1837
- {"user_id": user_id},
1838
- {
1839
- "$set": {
1840
- "subscription.plan": target_plan["id"],
1841
- "subscription.status": "active",
1842
- "subscription.quota_limit": target_plan.get("quota_limit", 100),
1843
- "subscription.billing_period": billing_period,
1844
- "updated_at": now,
1845
- }
1846
- },
1847
- upsert=True
1848
- )
1849
- doc = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
1850
- if doc:
1851
- return doc.get("subscription", sub)
1852
- except Exception as e:
1853
- print(f"[ALLI MongoDB] Plan update error: {e}")
1854
-
1855
- u_data = self._user_store.setdefault(user_id, {"user_id": user_id})
1856
- u_data["subscription"] = {**u_data.get("subscription", {}), **sub}
1857
- return sub
1858
 
1859
 
1860
  # Central shared singleton instance
1861
  workflow_repo = MongoWorkflowRepository()
1862
 
 
 
 
 
 
 
 
 
 
1
  """
2
+ ALLI Backend - MongoDB Repository Facade
3
+ Composes modular repository mixins while preserving 100% backward compatibility.
4
  """
5
 
6
+ from typing import Optional
 
 
 
 
 
7
  from database.repository import WorkflowRepository
8
+ from database.connection import MongoConnectionMixin, get_utc_now
9
+ from database.workflows_repo import WorkflowsRepoMixin
10
+ from database.orgs_repo import OrgsRepoMixin
11
+ from database.audit_repo import AuditRepoMixin
12
+ from database.blueprints_repo import BlueprintsRepoMixin
13
+ from database.templates_repo import TemplatesRepoMixin
14
+ from database.catalogs_repo import CatalogsRepoMixin
15
+ from database.users_repo import UsersRepoMixin
16
+ from database.workspace_env_repo import WorkspaceEnvRepoMixin
17
+ from database.seeds import (
18
+ get_default_packages,
19
+ get_default_core_specs,
20
+ get_default_starter_templates,
21
+ )
22
+
23
+ # Backward-compatible seed constants loaded from backend JSON specs
24
+ DEFAULT_PACKAGES = get_default_packages()
25
+ DEFAULT_CORE_SPECS = get_default_core_specs()
26
+ DEFAULT_STARTER_TEMPLATES = get_default_starter_templates()
27
+
28
+
29
+ class MongoWorkflowRepository(
30
+ MongoConnectionMixin,
31
+ WorkflowsRepoMixin,
32
+ OrgsRepoMixin,
33
+ AuditRepoMixin,
34
+ BlueprintsRepoMixin,
35
+ TemplatesRepoMixin,
36
+ CatalogsRepoMixin,
37
+ UsersRepoMixin,
38
+ WorkspaceEnvRepoMixin,
39
+ WorkflowRepository,
40
+ ):
41
+ """Unified MongoDB Atlas + resilient in-memory repository."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  def __init__(self, uri: Optional[str] = None, db_name: Optional[str] = None):
44
+ self._init_connection(uri=uri, db_name=db_name)
45
+ self._init_workflows_store()
46
+ self._init_orgs_store()
47
+ self._init_audit_store()
48
+ self._init_blueprints_store()
49
+ self._init_templates_store()
50
+ self._init_catalogs_store()
51
+ self._init_users_store()
52
+ self._init_workspace_env_store()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
 
55
  # Central shared singleton instance
56
  workflow_repo = MongoWorkflowRepository()
57
 
58
+ __all__ = [
59
+ "MongoWorkflowRepository",
60
+ "workflow_repo",
61
+ "get_utc_now",
62
+ "DEFAULT_PACKAGES",
63
+ "DEFAULT_CORE_SPECS",
64
+ "DEFAULT_STARTER_TEMPLATES",
65
+ ]
database/orgs_repo.py ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Organizations & Memberships Repository Mixin
3
+ Supports commercial multi-tenant SaaS workspaces with automatic personal workspace migration.
4
+ """
5
+
6
+ import re
7
+ import uuid
8
+ from typing import Any, Dict, List, Optional
9
+ from database.connection import get_utc_now
10
+
11
+ PLAN_SEAT_LIMITS: Dict[str, int] = {"free": 1, "pro": 1, "team": 10, "enterprise": 100}
12
+ VALID_ORG_ROLES = ("owner", "admin", "editor", "viewer")
13
+
14
+
15
+ def _slugify(name: str) -> str:
16
+ return re.sub(r"[^a-z0-9]+", "-", (name or "workspace").lower()).strip("-") or "workspace"
17
+
18
+
19
+ class OrgsRepoMixin:
20
+ """Organization and workspace membership persistence."""
21
+
22
+ def _init_orgs_store(self) -> None:
23
+ self._org_store: Dict[str, Dict[str, Any]] = {}
24
+ self._membership_store: Dict[str, Dict[str, Dict[str, Any]]] = {}
25
+
26
+ def ensure_personal_org(self, user_id: str, email: Optional[str] = None, name: Optional[str] = None) -> Dict[str, Any]:
27
+ """Ensure personal_<user_id> workspace and owner membership exist."""
28
+ org_id = f"personal_{user_id}"
29
+ now = get_utc_now()
30
+ existing = self.get_organization(org_id)
31
+ if not existing:
32
+ org_doc = {
33
+ "org_id": org_id, "name": name or "Personal Workspace",
34
+ "slug": f"personal-{str(user_id)[:12]}", "plan_tier": "free",
35
+ "seat_limit": 1, "owner_user_id": user_id, "is_personal": True,
36
+ "created_at": now, "updated_at": now,
37
+ }
38
+ orgs_col = self.get_collection("organizations")
39
+ if orgs_col is not None:
40
+ try:
41
+ orgs_col.update_one({"org_id": org_id}, {"$setOnInsert": org_doc}, upsert=True)
42
+ existing = orgs_col.find_one({"org_id": org_id}, {"_id": 0}) or org_doc
43
+ except Exception:
44
+ existing = org_doc
45
+ else:
46
+ existing = org_doc
47
+ self._org_store[org_id] = dict(existing)
48
+
49
+ if not self.get_org_membership(org_id, user_id):
50
+ self.upsert_org_member(
51
+ org_id=org_id, user_id=user_id, email=email or "",
52
+ role="owner", invited_by=user_id, status="active", enforce_seat_limit=False,
53
+ )
54
+ return existing
55
+
56
+ def create_organization(
57
+ self, owner_user_id: str, name: str, slug: Optional[str] = None,
58
+ plan_tier: str = "team", owner_email: Optional[str] = None,
59
+ ) -> Dict[str, Any]:
60
+ tier = plan_tier.lower().strip() if plan_tier in PLAN_SEAT_LIMITS else "team"
61
+ org_id = f"org_{uuid.uuid4().hex[:12]}"
62
+ now = get_utc_now()
63
+ org_doc = {
64
+ "org_id": org_id, "name": name.strip(),
65
+ "slug": _slugify(slug or name) + f"-{org_id[-4:]}",
66
+ "plan_tier": tier, "seat_limit": PLAN_SEAT_LIMITS.get(tier, 10),
67
+ "owner_user_id": owner_user_id, "is_personal": False,
68
+ "created_at": now, "updated_at": now,
69
+ }
70
+ orgs_col = self.get_collection("organizations")
71
+ if orgs_col is not None:
72
+ try:
73
+ orgs_col.insert_one(dict(org_doc))
74
+ except Exception as e:
75
+ print(f"[ALLI MongoDB] create_organization error: {e}")
76
+ self._org_store[org_id] = dict(org_doc)
77
+ self.upsert_org_member(
78
+ org_id=org_id, user_id=owner_user_id, email=owner_email or "",
79
+ role="owner", invited_by=owner_user_id, status="active", enforce_seat_limit=False,
80
+ )
81
+ return org_doc
82
+
83
+ def get_organization(self, org_id: str) -> Optional[Dict[str, Any]]:
84
+ if org_id == "local_personal":
85
+ return {
86
+ "org_id": "local_personal", "name": "Local Solo Workspace",
87
+ "slug": "local-personal", "plan_tier": "pro", "seat_limit": 1,
88
+ "owner_user_id": "local_solo", "is_personal": True, "is_local_loopback": True,
89
+ }
90
+ orgs_col = self.get_collection("organizations")
91
+ if orgs_col is not None:
92
+ try:
93
+ doc = orgs_col.find_one({"org_id": org_id}, {"_id": 0})
94
+ if doc:
95
+ return doc
96
+ except Exception as e:
97
+ print(f"[ALLI MongoDB] get_organization error: {e}")
98
+ return self._org_store.get(org_id)
99
+
100
+ def list_user_organizations(self, user_id: str, email: Optional[str] = None) -> List[Dict[str, Any]]:
101
+ self.ensure_personal_org(user_id, email=email)
102
+ memberships: List[Dict[str, Any]] = []
103
+ mem_col = self.get_collection("org_memberships")
104
+ if mem_col is not None:
105
+ try:
106
+ memberships = list(mem_col.find({"user_id": user_id, "status": "active"}, {"_id": 0}))
107
+ except Exception as e:
108
+ print(f"[ALLI MongoDB] list_user_organizations error: {e}")
109
+ if not memberships:
110
+ memberships = [
111
+ members[user_id] for members in self._membership_store.values()
112
+ if user_id in members and members[user_id].get("status", "active") == "active"
113
+ ]
114
+
115
+ orgs, seen = [], set()
116
+ for m in memberships:
117
+ oid = m.get("org_id")
118
+ if oid and oid not in seen:
119
+ org = self.get_organization(oid)
120
+ if org:
121
+ seen.add(oid)
122
+ orgs.append({**org, "role": m.get("role", "viewer")})
123
+ return orgs
124
+
125
+ def get_org_membership(self, org_id: str, user_id: str) -> Optional[Dict[str, Any]]:
126
+ mem_col = self.get_collection("org_memberships")
127
+ if mem_col is not None:
128
+ try:
129
+ doc = mem_col.find_one({"org_id": org_id, "user_id": user_id}, {"_id": 0})
130
+ if doc:
131
+ return doc
132
+ except Exception as e:
133
+ print(f"[ALLI MongoDB] get_org_membership error: {e}")
134
+ return self._membership_store.get(org_id, {}).get(user_id)
135
+
136
+ def list_org_members(self, org_id: str) -> List[Dict[str, Any]]:
137
+ mem_col = self.get_collection("org_memberships")
138
+ if mem_col is not None:
139
+ try:
140
+ docs = list(mem_col.find({"org_id": org_id}, {"_id": 0}))
141
+ if docs:
142
+ return docs
143
+ except Exception as e:
144
+ print(f"[ALLI MongoDB] list_org_members error: {e}")
145
+ return list(self._membership_store.get(org_id, {}).values())
146
+
147
+ def upsert_org_member(
148
+ self, org_id: str, user_id: str, email: str, role: str,
149
+ invited_by: str, status: str = "active", enforce_seat_limit: bool = True,
150
+ ) -> Dict[str, Any]:
151
+ clean_role = role.lower().strip()
152
+ if clean_role not in VALID_ORG_ROLES:
153
+ raise ValueError(f"Invalid role '{role}'. Must be one of {VALID_ORG_ROLES}.")
154
+ existing = self.get_org_membership(org_id, user_id)
155
+ if enforce_seat_limit and not existing:
156
+ seat_limit = int((self.get_organization(org_id) or {}).get("seat_limit", 1))
157
+ active_count = len([m for m in self.list_org_members(org_id) if m.get("status") == "active"])
158
+ if active_count >= seat_limit:
159
+ raise ValueError(f"Seat limit ({seat_limit}) reached for workspace '{org_id}'.")
160
+
161
+ now = get_utc_now()
162
+ doc = {
163
+ "org_id": org_id, "user_id": user_id, "email": email, "role": clean_role,
164
+ "status": status, "invited_by": invited_by,
165
+ "joined_at": (existing or {}).get("joined_at") or now, "updated_at": now,
166
+ }
167
+ mem_col = self.get_collection("org_memberships")
168
+ if mem_col is not None:
169
+ try:
170
+ mem_col.update_one({"org_id": org_id, "user_id": user_id}, {"$set": doc}, upsert=True)
171
+ except Exception as e:
172
+ print(f"[ALLI MongoDB] upsert_org_member error: {e}")
173
+ self._membership_store.setdefault(org_id, {})[user_id] = dict(doc)
174
+ return doc
175
+
176
+ def remove_org_member(self, org_id: str, user_id: str) -> bool:
177
+ deleted = False
178
+ mem_col = self.get_collection("org_memberships")
179
+ if mem_col is not None:
180
+ try:
181
+ deleted = mem_col.delete_one({"org_id": org_id, "user_id": user_id}).deleted_count > 0
182
+ except Exception as e:
183
+ print(f"[ALLI MongoDB] remove_org_member error: {e}")
184
+ if user_id in self._membership_store.get(org_id, {}):
185
+ del self._membership_store[org_id][user_id]
186
+ deleted = True
187
+ return deleted
188
+
189
+ def update_org_plan(self, org_id: str, plan_tier: str, billing_period: str = "monthly") -> Optional[Dict[str, Any]]:
190
+ tier = plan_tier.lower().strip()
191
+ if tier not in PLAN_SEAT_LIMITS:
192
+ raise ValueError(f"Unknown plan tier '{plan_tier}'.")
193
+ updates = {
194
+ "plan_tier": tier, "seat_limit": PLAN_SEAT_LIMITS[tier],
195
+ "billing_period": billing_period, "updated_at": get_utc_now(),
196
+ }
197
+ orgs_col = self.get_collection("organizations")
198
+ if orgs_col is not None:
199
+ try:
200
+ orgs_col.update_one({"org_id": org_id}, {"$set": updates})
201
+ doc = orgs_col.find_one({"org_id": org_id}, {"_id": 0})
202
+ if doc:
203
+ self._org_store[org_id] = dict(doc)
204
+ return doc
205
+ except Exception as e:
206
+ print(f"[ALLI MongoDB] update_org_plan error: {e}")
207
+ if org_id in self._org_store:
208
+ self._org_store[org_id].update(updates)
209
+ return self._org_store[org_id]
210
+ return None
database/seeds.py ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Database Seed Loaders
3
+ Loads canonical JSON specs from alli_backend/specs/ and idempotently seeds MongoDB collections.
4
+ """
5
+
6
+ import copy
7
+ import json
8
+ from functools import lru_cache
9
+ from pathlib import Path
10
+ from typing import Any, Dict, List
11
+
12
+ BACKEND_ROOT = Path(__file__).resolve().parent.parent
13
+ SPECS_DIR = BACKEND_ROOT / "specs"
14
+ VALIDATION_REPORT_PATH = BACKEND_ROOT / "node_validation_lab" / "reports" / "phase3_validation_report.json"
15
+ INVENTORY_AUDIT_PATH = BACKEND_ROOT / "node_inventory_audit.json"
16
+
17
+
18
+ @lru_cache(maxsize=16)
19
+ def _load_json_cached(path_str: str) -> Any:
20
+ path = Path(path_str)
21
+ if not path.is_file():
22
+ return None
23
+ with open(path, "r", encoding="utf-8") as f:
24
+ return json.load(f)
25
+
26
+
27
+ def _load_spec(filename: str, default: Any) -> Any:
28
+ data = _load_json_cached(str(SPECS_DIR / filename))
29
+ return copy.deepcopy(data) if data is not None else copy.deepcopy(default)
30
+
31
+
32
+ def get_default_packages() -> List[Dict[str, Any]]:
33
+ return _load_spec("packages.json", [])
34
+
35
+ def get_default_core_specs() -> List[Dict[str, Any]]:
36
+ return _load_spec("core_specs.json", [])
37
+
38
+ def get_default_studio_palette() -> List[Dict[str, Any]]:
39
+ return _load_spec("studio_palette_categories.json", [])
40
+
41
+ def get_default_starter_templates() -> List[Dict[str, Any]]:
42
+ return _load_spec("starter_templates.json", [])
43
+
44
+ def get_default_plans() -> List[Dict[str, Any]]:
45
+ return _load_spec("plans.json", [])
46
+
47
+ def get_default_python_fundamentals() -> Dict[str, Any]:
48
+ return _load_spec("python_fundamentals.json", {"version": "1.0.0", "name": "Python Fundamentals Specification", "constructs": []})
49
+
50
+ def get_raw_programming_fundamentals() -> List[Dict[str, Any]]:
51
+ return _load_spec("programming_fundamentals_11_categories.json", [])
52
+
53
+ def _palette_sig(cats: Any) -> tuple:
54
+ ids, ntypes, total = set(), set(), 0
55
+ def _walk(items: Any) -> None:
56
+ nonlocal total
57
+ for it in (items if isinstance(items, list) else ()):
58
+ if isinstance(it, dict):
59
+ total += 1
60
+ if it.get("id"): ids.add(str(it["id"]))
61
+ if it.get("nodeType"): ntypes.add(str(it["nodeType"]))
62
+ _walk(it.get("children"))
63
+ for c in (cats if isinstance(cats, list) else ()):
64
+ if isinstance(c, dict): _walk(c.get("items"))
65
+ return (len(cats) if isinstance(cats, list) else 0, total, frozenset(ids), frozenset(ntypes))
66
+
67
+ def is_studio_palette_stale(stored: Any, canonical: Any = None) -> bool:
68
+ """Return True when persisted primary_studio_categories is stale vs specs/studio_palette_categories.json."""
69
+ canon = canonical if canonical is not None else get_default_studio_palette()
70
+ if not isinstance(stored, list):
71
+ return True
72
+ s_sig, c_sig = _palette_sig(stored), _palette_sig(canon)
73
+ return s_sig[0] != c_sig[0] or s_sig[1] != c_sig[1] or not c_sig[2].issubset(s_sig[2]) or not c_sig[3].issubset(s_sig[3]) or stored != canon
74
+
75
+
76
+ def consolidate_fundamentals_to_9(raw_categories: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
77
+ """
78
+ Consolidate the 11 educational fundamentals categories into the 9 canonical Studio
79
+ categories while preserving 100% of the 53 topics, functions, rules, and quizzes.
80
+ """
81
+ if not raw_categories:
82
+ return []
83
+ by_id = {c.get("id"): copy.deepcopy(c) for c in raw_categories if isinstance(c, dict)}
84
+ essentials = by_id.pop("cat_essentials", None)
85
+ modules = by_id.pop("cat_modules", None)
86
+
87
+ ordered_ids = [
88
+ "cat_variables", "cat_data_types", "cat_operators", "cat_math",
89
+ "cat_control_flow", "cat_functions", "cat_strings_collections", "cat_oop", "cat_files",
90
+ ]
91
+ consolidated: List[Dict[str, Any]] = []
92
+ for idx, cid in enumerate(ordered_ids, start=1):
93
+ cat = by_id.pop(cid, None)
94
+ if not cat:
95
+ continue
96
+ if cid == "cat_variables" and essentials:
97
+ for k in ("topics", "functions", "learningObjectives"):
98
+ cat[k] = list(essentials.get(k, [])) + list(cat.get(k, []))
99
+ cat["mergedCategoryIds"] = ["cat_essentials", "cat_variables"]
100
+ elif cid == "cat_files" and modules:
101
+ for k in ("topics", "functions", "learningObjectives"):
102
+ cat[k] = list(cat.get(k, [])) + list(modules.get(k, []))
103
+ cat["mergedCategoryIds"] = ["cat_files", "cat_modules"]
104
+
105
+ for t_idx, topic in enumerate(cat.get("topics", []), start=1):
106
+ topic["topicNumber"] = t_idx
107
+ cat["categoryNumber"] = idx
108
+ cat["expectedTopicCount"] = len(cat.get("topics", []))
109
+ consolidated.append(cat)
110
+
111
+ return consolidated
112
+
113
+
114
+ def get_default_learning_modules() -> List[Dict[str, Any]]:
115
+ return _load_spec("learning_modules.json", [])
116
+
117
+ def get_default_blog_data() -> Dict[str, Any]:
118
+ return _load_spec("blog_posts.json", {"categories": [], "posts": []})
119
+
120
+ def get_default_validation_artifacts() -> Dict[str, Any]:
121
+ report = _load_json_cached(str(VALIDATION_REPORT_PATH)) or {}
122
+ inventory = _load_json_cached(str(INVENTORY_AUDIT_PATH)) or {}
123
+ return {"report": copy.deepcopy(report), "inventory_metadata": copy.deepcopy(inventory.get("audit_metadata", {})), "inventory": copy.deepcopy(inventory.get("inventory", []))}
124
+
125
+
126
+ def seed_all_collections(repo: Any) -> Dict[str, Any]:
127
+ """
128
+ Idempotently seed MongoDB collections from backend specs when DB is connected.
129
+ Never blocks startup if DB is offline.
130
+ """
131
+ if not getattr(repo, "is_connected", False):
132
+ return {"seeded": False, "reason": "offline_inmemory_fallback"}
133
+
134
+ seeded_counts: Dict[str, int] = {}
135
+ try:
136
+ from database.connection import get_utc_now
137
+ now = get_utc_now()
138
+
139
+ # 1. Core specs & primary studio palette
140
+ core_col = repo.get_collection("core_definitions")
141
+ if core_col is not None:
142
+ if not core_col.find_one({"key": "python_core_categories"}):
143
+ core_col.update_one(
144
+ {"key": "python_core_categories"},
145
+ {"$set": {"key": "python_core_categories", "categories": get_default_core_specs(), "updated_at": now}},
146
+ upsert=True,
147
+ )
148
+ canonical_palette = get_default_studio_palette()
149
+ existing_palette = core_col.find_one({"key": "primary_studio_categories"}, {"_id": 0})
150
+ if not existing_palette or is_studio_palette_stale(existing_palette.get("categories"), canonical_palette):
151
+ core_col.update_one(
152
+ {"key": "primary_studio_categories"},
153
+ {"$set": {"key": "primary_studio_categories", "categories": canonical_palette, "updated_at": now}},
154
+ upsert=True,
155
+ )
156
+ seeded_counts["core_definitions"] = 2
157
+
158
+ # 2. Python fundamentals (schema + 9 consolidated categories / 53 topics)
159
+ fund_col = repo.get_collection("python_fundamentals")
160
+ if fund_col is not None:
161
+ if not fund_col.find_one({"key": "python_fundamentals_schema"}):
162
+ py_fund = get_default_python_fundamentals()
163
+ fund_col.update_one(
164
+ {"key": "python_fundamentals_schema"},
165
+ {"$set": {**py_fund, "key": "python_fundamentals_schema", "updated_at": now}},
166
+ upsert=True,
167
+ )
168
+ raw_cats = get_raw_programming_fundamentals()
169
+ consolidated = consolidate_fundamentals_to_9(raw_cats)
170
+ if not fund_col.find_one({"key": "programming_fundamentals_categories"}):
171
+ fund_col.update_one(
172
+ {"key": "programming_fundamentals_categories"},
173
+ {
174
+ "$set": {
175
+ "key": "programming_fundamentals_categories",
176
+ "categories": consolidated,
177
+ "legacy_categories": raw_cats,
178
+ "total_categories": len(consolidated),
179
+ "total_topics": sum(len(c.get("topics", [])) for c in consolidated),
180
+ "updated_at": now,
181
+ }
182
+ },
183
+ upsert=True,
184
+ )
185
+ seeded_counts["python_fundamentals"] = len(consolidated)
186
+
187
+ # 3. Packages / default blueprints
188
+ bp_col = repo.get_collection("blueprints")
189
+ if bp_col is not None:
190
+ pkgs = get_default_packages()
191
+ for pkg in pkgs:
192
+ if not bp_col.find_one({"package": pkg["name"].lower()}):
193
+ bp_col.update_one(
194
+ {"package": pkg["name"].lower()},
195
+ {"$set": {"package": pkg["name"].lower(), "blueprint": pkg, "is_default_package": True, "updated_at": now}},
196
+ upsert=True,
197
+ )
198
+ seeded_counts["packages"] = len(pkgs)
199
+
200
+ # 4. Templates, Plans, Learning Modules, Blog Posts
201
+ tmpl_col = repo.get_collection("templates")
202
+ if tmpl_col is not None:
203
+ tmpls = get_default_starter_templates()
204
+ for t in tmpls:
205
+ if not tmpl_col.find_one({"id": t["id"]}):
206
+ tmpl_col.update_one({"id": t["id"]}, {"$set": t}, upsert=True)
207
+ seeded_counts["templates"] = len(tmpls)
208
+
209
+ plans_col = repo.get_collection("plans")
210
+ if plans_col is not None:
211
+ plans = get_default_plans()
212
+ for p in plans:
213
+ plans_col.update_one({"id": p["id"]}, {"$set": p}, upsert=True)
214
+ seeded_counts["plans"] = len(plans)
215
+
216
+ learn_col = repo.get_collection("learning_modules")
217
+ if learn_col is not None:
218
+ lessons = get_default_learning_modules()
219
+ for lesson in lessons:
220
+ if not learn_col.find_one({"id": lesson["id"]}):
221
+ learn_col.update_one({"id": lesson["id"]}, {"$set": lesson}, upsert=True)
222
+ seeded_counts["learning_modules"] = len(lessons)
223
+
224
+ blog_col = repo.get_collection("blog_posts")
225
+ if blog_col is not None:
226
+ blog_data = get_default_blog_data()
227
+ for post in blog_data.get("posts", []):
228
+ if not blog_col.find_one({"slug": post["slug"]}):
229
+ blog_col.update_one({"slug": post["slug"]}, {"$set": post}, upsert=True)
230
+ seeded_counts["blog_posts"] = len(blog_data.get("posts", []))
231
+
232
+ # 5. Canonical node schemas
233
+ from language_core.node_schema import default_registry
234
+ schemas_col = repo.get_collection("node_schemas")
235
+ if schemas_col is not None:
236
+ schemas = default_registry.to_json_dict()
237
+ for s in schemas:
238
+ schemas_col.update_one({"type": s["type"]}, {"$set": s}, upsert=True)
239
+ seeded_counts["node_schemas"] = len(schemas)
240
+
241
+ return {"seeded": True, "counts": seeded_counts}
242
+ except Exception as e:
243
+ print(f"[ALLI Seeds] Non-fatal seed error: {e}")
244
+ return {"seeded": False, "error": str(e)}
database/templates_repo.py ADDED
@@ -0,0 +1,178 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Templates Catalog Repository Mixin
3
+ Manages published global templates, starter seeds, usage tracking, and star toggles.
4
+ """
5
+
6
+ import uuid
7
+ from typing import Any, Dict, List, Optional
8
+ from database.connection import get_utc_now
9
+ from database.seeds import get_default_starter_templates
10
+
11
+ TEMPLATE_ALIAS_MAP = {
12
+ "math-calculator": "math-logic-calculator",
13
+ "math_pipeline": "math-logic-calculator",
14
+ "react_agent": "langgraph-react-agent",
15
+ "data_pipeline": "data-etl-transformer",
16
+ }
17
+
18
+
19
+ class TemplatesRepoMixin:
20
+ """Templates catalog CRUD and engagement counters."""
21
+
22
+ def _init_templates_store(self) -> None:
23
+ self._template_store: Dict[str, Dict[str, Any]] = {
24
+ t["id"]: dict(t) for t in get_default_starter_templates()
25
+ }
26
+
27
+ @staticmethod
28
+ def _resolve_template_id(template_id: str) -> str:
29
+ t_id = template_id.lower().strip()
30
+ return TEMPLATE_ALIAS_MAP.get(t_id, t_id)
31
+
32
+ def save_template(self, template_data: Dict[str, Any]) -> str:
33
+ t_id = template_data.get("id") or f"tmpl_{uuid.uuid4().hex[:12]}"
34
+ template_data["id"] = t_id
35
+ template_data["updated_at"] = get_utc_now()
36
+ template_data["is_global"] = True
37
+ template_data["is_published"] = True
38
+
39
+ tmpl_col = self.get_collection("templates")
40
+ if tmpl_col is not None:
41
+ try:
42
+ tmpl_col.update_one({"id": t_id}, {"$set": template_data}, upsert=True)
43
+ self._template_store[t_id] = dict(template_data)
44
+ return t_id
45
+ except Exception as e:
46
+ print(f"[ALLI MongoDB] Template save error: {e}")
47
+
48
+ self._template_store[t_id] = dict(template_data)
49
+ return t_id
50
+
51
+ def list_templates(self) -> List[Dict[str, Any]]:
52
+ templates: List[Dict[str, Any]] = []
53
+ tmpl_col = self.get_collection("templates")
54
+ if tmpl_col is not None:
55
+ try:
56
+ templates.extend(list(tmpl_col.find({}, {"_id": 0})))
57
+ except Exception as e:
58
+ print(f"[ALLI MongoDB] Template list error: {e}")
59
+
60
+ seen_ids = {d.get("id") for d in templates}
61
+ for t_id, t_data in self._template_store.items():
62
+ if t_id not in seen_ids:
63
+ templates.append(t_data)
64
+ return templates
65
+
66
+ def get_template(self, template_id: str) -> Optional[Dict[str, Any]]:
67
+ target_id = self._resolve_template_id(template_id)
68
+ tmpl_col = self.get_collection("templates")
69
+ if tmpl_col is not None:
70
+ try:
71
+ doc = tmpl_col.find_one({"id": target_id}, {"_id": 0})
72
+ if doc:
73
+ return doc
74
+ except Exception as e:
75
+ print(f"[ALLI MongoDB] Template fetch error: {e}")
76
+
77
+ return self._template_store.get(target_id) or self._template_store.get(template_id)
78
+
79
+ def track_template_use(self, template_id: str, user_id: Optional[str] = None) -> int:
80
+ target_id = self._resolve_template_id(template_id)
81
+ new_count = 1
82
+
83
+ tmpl_col = self.get_collection("templates")
84
+ if tmpl_col is not None:
85
+ try:
86
+ res = tmpl_col.find_one_and_update(
87
+ {"id": target_id},
88
+ {"$inc": {"downloads": 1, "used_count": 1}},
89
+ return_document=True,
90
+ )
91
+ if res:
92
+ new_count = res.get("downloads", 1)
93
+
94
+ if user_id and self.users_collection is not None:
95
+ self.users_collection.update_one(
96
+ {"user_id": user_id},
97
+ {"$addToSet": {"templates_used": target_id}, "$inc": {"templates_used_count": 1}},
98
+ upsert=True,
99
+ )
100
+ return new_count
101
+ except Exception as e:
102
+ print(f"[ALLI MongoDB] track_template_use error: {e}")
103
+
104
+ if target_id in self._template_store:
105
+ self._template_store[target_id]["downloads"] = self._template_store[target_id].get("downloads", 0) + 1
106
+ new_count = self._template_store[target_id]["downloads"]
107
+
108
+ if user_id:
109
+ u_data = self._user_store.setdefault(
110
+ user_id, {"user_id": user_id, "templates_used": [], "templates_used_count": 0}
111
+ )
112
+ if target_id not in u_data.get("templates_used", []):
113
+ u_data.setdefault("templates_used", []).append(target_id)
114
+ u_data["templates_used_count"] = u_data.get("templates_used_count", 0) + 1
115
+
116
+ return new_count
117
+
118
+ def toggle_template_star(self, template_id: str, user_id: Optional[str] = None) -> Dict[str, Any]:
119
+ target_id = self._resolve_template_id(template_id)
120
+ is_starred = True
121
+ stars_count = 0
122
+
123
+ tmpl_col = self.get_collection("templates")
124
+ if tmpl_col is not None:
125
+ try:
126
+ user_col = self.users_collection
127
+ if user_id and user_col is not None:
128
+ user_doc = user_col.find_one({"user_id": user_id})
129
+ starred_list = user_doc.get("starred_templates", []) if user_doc else []
130
+ if target_id in starred_list:
131
+ user_col.update_one({"user_id": user_id}, {"$pull": {"starred_templates": target_id}})
132
+ tmpl_doc = tmpl_col.find_one_and_update(
133
+ {"id": target_id},
134
+ {"$inc": {"stars_count": -1, "starsCount": -1}},
135
+ return_document=True,
136
+ )
137
+ is_starred = False
138
+ else:
139
+ user_col.update_one(
140
+ {"user_id": user_id},
141
+ {"$addToSet": {"starred_templates": target_id}},
142
+ upsert=True,
143
+ )
144
+ tmpl_doc = tmpl_col.find_one_and_update(
145
+ {"id": target_id},
146
+ {"$inc": {"stars_count": 1, "starsCount": 1}},
147
+ return_document=True,
148
+ )
149
+ is_starred = True
150
+
151
+ if tmpl_doc:
152
+ stars_count = max(0, tmpl_doc.get("stars_count") or tmpl_doc.get("starsCount") or 0)
153
+ return {"template_id": target_id, "is_starred": is_starred, "stars_count": stars_count}
154
+ except Exception as e:
155
+ print(f"[ALLI MongoDB] toggle_template_star error: {e}")
156
+
157
+ t_data = self._template_store.get(target_id)
158
+ if t_data:
159
+ current_stars = t_data.get("stars_count") or t_data.get("starsCount") or 0
160
+ if user_id:
161
+ u_data = self._user_store.setdefault(user_id, {"user_id": user_id, "starred_templates": []})
162
+ starred_list = u_data.setdefault("starred_templates", [])
163
+ if target_id in starred_list:
164
+ starred_list.remove(target_id)
165
+ current_stars = max(0, current_stars - 1)
166
+ is_starred = False
167
+ else:
168
+ starred_list.append(target_id)
169
+ current_stars += 1
170
+ is_starred = True
171
+ else:
172
+ current_stars += 1
173
+ is_starred = True
174
+ t_data["stars_count"] = current_stars
175
+ t_data["starsCount"] = current_stars
176
+ stars_count = current_stars
177
+
178
+ return {"template_id": target_id, "is_starred": is_starred, "stars_count": stars_count}
database/users_repo.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - User Profiles, Quotas & Plans Repository Mixin
3
+ Manages user profile documents, run quotas, telemetry stats, and subscription plan tiers.
4
+ """
5
+
6
+ from typing import Any, Dict, List, Optional
7
+ from database.connection import get_utc_now
8
+ from database.seeds import get_default_plans
9
+
10
+ STARTER_TEMPLATE_NAMES = {
11
+ "math & logic calculator",
12
+ "math logic calculator",
13
+ "math calculation pipeline",
14
+ "data etl transformer",
15
+ "langgraph react agent",
16
+ "pdf knowledge agent",
17
+ "web scraper flow",
18
+ }
19
+
20
+
21
+ class UsersRepoMixin:
22
+ """User profile, quota, stats, and pricing plan operations."""
23
+
24
+ def _init_users_store(self) -> None:
25
+ self._user_store: Dict[str, Dict[str, Any]] = {}
26
+
27
+ def get_user_profile(self, user_id: str) -> Optional[Dict[str, Any]]:
28
+ if self.is_connected and self.users_collection is not None:
29
+ try:
30
+ doc = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
31
+ if doc:
32
+ return doc
33
+ except Exception as e:
34
+ print(f"[ALLI MongoDB] Profile fetch error: {e}")
35
+ return self._user_store.get(user_id)
36
+
37
+ def _save_user_doc(self, user_id: str, doc: Dict[str, Any], upsert: bool = False) -> Dict[str, Any]:
38
+ if self.is_connected and self.users_collection is not None:
39
+ try:
40
+ self.users_collection.update_one({"user_id": user_id}, {"$set": doc}, upsert=upsert)
41
+ saved = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
42
+ if saved:
43
+ return saved
44
+ except Exception as e:
45
+ print(f"[ALLI MongoDB] Profile save error: {e}")
46
+ self._user_store[user_id] = doc
47
+ return doc
48
+
49
+ def upsert_user_profile(self, user_id: str, profile_data: Dict[str, Any]) -> Dict[str, Any]:
50
+ now = get_utc_now()
51
+ profile_data["user_id"] = user_id
52
+ if hasattr(self, "ensure_personal_org"):
53
+ self.ensure_personal_org(user_id, email=profile_data.get("email"), name=profile_data.get("full_name"))
54
+
55
+ existing = self.get_user_profile(user_id)
56
+ if existing:
57
+ updated = dict(existing)
58
+ for k, v in profile_data.items():
59
+ if v is not None and k != "created_at" and not (k == "subscription" and not v):
60
+ updated[k] = v
61
+ if "subscription" not in profile_data and "subscription" in existing:
62
+ updated["subscription"] = existing["subscription"]
63
+ updated["updated_at"] = now
64
+ return self._save_user_doc(user_id, updated, upsert=False)
65
+
66
+ profile_data.setdefault("created_at", now)
67
+ profile_data["updated_at"] = now
68
+ profile_data.setdefault("role", "developer")
69
+ profile_data.setdefault("subscription", {"plan": "free", "status": "active", "quota_limit": 100, "quota_used": 0})
70
+ profile_data.setdefault("stats", {"successful_runs": 0})
71
+ profile_data.setdefault("starred_templates", [])
72
+ profile_data.setdefault("preferences", {"theme": "dark", "auto_compile": True, "auto_save": True})
73
+ return self._save_user_doc(user_id, profile_data, upsert=True)
74
+
75
+ def update_user_profile(self, user_id: str, updates: Dict[str, Any]) -> Optional[Dict[str, Any]]:
76
+ updates["updated_at"] = get_utc_now()
77
+ if self.is_connected and self.users_collection is not None:
78
+ try:
79
+ self.users_collection.update_one({"user_id": user_id}, {"$set": updates})
80
+ doc = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
81
+ if doc:
82
+ return doc
83
+ except Exception as e:
84
+ print(f"[ALLI MongoDB] Profile update error: {e}")
85
+ if user_id in self._user_store:
86
+ self._user_store[user_id].update(updates)
87
+ return self._user_store[user_id]
88
+ return None
89
+
90
+ def update_user_quota(self, user_id: str, increment: int = 1) -> Dict[str, Any]:
91
+ now = get_utc_now()
92
+ if self.is_connected and self.users_collection is not None:
93
+ try:
94
+ self.users_collection.update_one(
95
+ {"user_id": user_id}, {"$inc": {"subscription.quota_used": increment}, "$set": {"updated_at": now}}
96
+ )
97
+ doc = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
98
+ if doc:
99
+ return doc.get("subscription", {})
100
+ except Exception as e:
101
+ print(f"[ALLI MongoDB] Quota increment error: {e}")
102
+ user = self._user_store.setdefault(user_id, {"user_id": user_id})
103
+ sub = user.get("subscription", {"plan": "pro", "status": "active", "quota_limit": 100, "quota_used": 0})
104
+ sub["quota_used"] = sub.get("quota_used", 0) + increment
105
+ user["subscription"] = sub
106
+ return sub
107
+
108
+ def record_successful_run(self, user_id: str) -> int:
109
+ now = get_utc_now()
110
+ if self.is_connected and self.users_collection is not None:
111
+ try:
112
+ self.users_collection.update_one(
113
+ {"user_id": user_id}, {"$inc": {"stats.successful_runs": 1}, "$set": {"updated_at": now}}
114
+ )
115
+ doc = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
116
+ if doc and "stats" in doc:
117
+ return doc["stats"].get("successful_runs", 1)
118
+ except Exception as e:
119
+ print(f"[ALLI MongoDB] record_successful_run error: {e}")
120
+ stats = self._user_store.setdefault(user_id, {"user_id": user_id, "stats": {}}).setdefault("stats", {})
121
+ stats["successful_runs"] = stats.get("successful_runs", 0) + 1
122
+ return stats["successful_runs"]
123
+
124
+ def get_user_stats(self, user_id: Optional[str] = None, org_id: Optional[str] = None) -> Dict[str, Any]:
125
+ wf_list = (
126
+ self.list_workflows(user_id=user_id, org_id=org_id)
127
+ if (user_id or org_id)
128
+ else (self.list_workflows() or list(self._memory_store.values()))
129
+ )
130
+ templates_used = sum(
131
+ 1 for w in wf_list
132
+ if w.get("source_template_id")
133
+ or (w.get("name") and str(w.get("name")).lower().strip() in STARTER_TEMPLATE_NAMES)
134
+ )
135
+ runs_used, runs_limit, successful_runs = 0, 100, 0
136
+ user_doc = self.get_user_profile(user_id) if user_id else None
137
+ if user_doc:
138
+ if isinstance(user_doc.get("templates_used"), list):
139
+ templates_used = max(templates_used, len(user_doc["templates_used"]))
140
+ elif "templates_used_count" in user_doc:
141
+ templates_used = max(templates_used, user_doc["templates_used_count"])
142
+ sub = user_doc.get("subscription", {})
143
+ runs_used = sub.get("quota_used", 0)
144
+ runs_limit = sub.get("quota_limit", 100)
145
+ successful_runs = user_doc.get("stats", {}).get("successful_runs", 0)
146
+
147
+ return {
148
+ "totalProjects": len(wf_list),
149
+ "templatesUsed": templates_used,
150
+ "runsUsed": runs_used,
151
+ "runsLimit": runs_limit,
152
+ "successfulRuns": successful_runs,
153
+ }
154
+
155
+ def get_pricing_plans(self) -> List[Dict[str, Any]]:
156
+ default_plans = get_default_plans()
157
+ col = self.get_collection("plans")
158
+ if col is not None:
159
+ try:
160
+ docs = list(col.find({}, {"_id": 0}))
161
+ if docs:
162
+ return docs
163
+ for p in default_plans:
164
+ col.update_one({"id": p["id"]}, {"$set": p}, upsert=True)
165
+ docs = list(col.find({}, {"_id": 0}))
166
+ if docs:
167
+ return docs
168
+ except Exception as e:
169
+ print(f"[ALLI MongoDB] Error fetching plans from DB: {e}")
170
+ return default_plans
171
+
172
+ def update_user_plan(self, user_id: str, plan_id: str, billing_period: str = "monthly") -> Dict[str, Any]:
173
+ p_id = plan_id.lower().strip()
174
+ plans = {p["id"]: p for p in self.get_pricing_plans()}
175
+ target_plan = plans.get(p_id, {"id": p_id, "quota_limit": 100})
176
+ now = get_utc_now()
177
+ sub = {
178
+ "plan": target_plan["id"], "status": "active",
179
+ "quota_limit": target_plan.get("quota_limit", 100),
180
+ "billing_period": billing_period, "updated_at": now,
181
+ }
182
+ if self.is_connected and self.users_collection is not None:
183
+ try:
184
+ self.users_collection.update_one(
185
+ {"user_id": user_id},
186
+ {"$set": {
187
+ "subscription.plan": sub["plan"], "subscription.status": "active",
188
+ "subscription.quota_limit": sub["quota_limit"],
189
+ "subscription.billing_period": billing_period, "updated_at": now,
190
+ }},
191
+ upsert=True,
192
+ )
193
+ doc = self.users_collection.find_one({"user_id": user_id}, {"_id": 0})
194
+ if doc:
195
+ return doc.get("subscription", sub)
196
+ except Exception as e:
197
+ print(f"[ALLI MongoDB] Plan update error: {e}")
198
+ u_data = self._user_store.setdefault(user_id, {"user_id": user_id})
199
+ u_data["subscription"] = {**u_data.get("subscription", {}), **sub}
200
+ return sub
database/workflows_repo.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Workflows Persistence Mixin
3
+ Handles workflow CRUD, v3.0.0 schema migration on read, and automatic personal org_id migration.
4
+ """
5
+
6
+ import uuid
7
+ from typing import Any, Dict, List, Optional
8
+ from database.connection import get_utc_now
9
+
10
+
11
+ def resolve_workflow_org_id(wf: Dict[str, Any]) -> str:
12
+ """Resolve org_id transparently for legacy workflows lacking explicit org_id."""
13
+ org_id = wf.get("org_id")
14
+ if org_id and str(org_id).strip():
15
+ return str(org_id).strip()
16
+ uid = wf.get("user_id")
17
+ if uid and str(uid).strip():
18
+ return f"personal_{str(uid).strip()}"
19
+ return "local_personal"
20
+
21
+
22
+ class WorkflowsRepoMixin:
23
+ """Workflow CRUD operations backed by MongoDB Atlas with in-memory fallback."""
24
+
25
+ def _init_workflows_store(self) -> None:
26
+ self._memory_store: Dict[str, Dict[str, Any]] = {}
27
+
28
+ def save_workflow(self, workflow_data: Dict[str, Any]) -> str:
29
+ wf_id = workflow_data.get("id") or str(uuid.uuid4())
30
+ workflow_data["id"] = wf_id
31
+ if not workflow_data.get("created_at"):
32
+ workflow_data["created_at"] = get_utc_now()
33
+ workflow_data["updated_at"] = get_utc_now()
34
+ if not workflow_data.get("schema_version"):
35
+ workflow_data["schema_version"] = "3.0.0"
36
+ if not workflow_data.get("viewport"):
37
+ workflow_data["viewport"] = {"x": 0, "y": 0, "zoom": 1.0}
38
+ workflow_data.setdefault("nodes", [])
39
+ workflow_data.setdefault("edges", [])
40
+ workflow_data["org_id"] = resolve_workflow_org_id(workflow_data)
41
+
42
+ source_tmpl = workflow_data.get("source_template_id")
43
+ user_id = workflow_data.get("user_id")
44
+ if source_tmpl and hasattr(self, "track_template_use"):
45
+ self.track_template_use(source_tmpl, user_id)
46
+
47
+ if self.is_connected and self.collection is not None:
48
+ try:
49
+ self.collection.update_one(
50
+ {"id": wf_id},
51
+ {"$set": workflow_data},
52
+ upsert=True,
53
+ )
54
+ return wf_id
55
+ except Exception as e:
56
+ print(f"[ALLI MongoDB] Save error: {e}, falling back to memory.")
57
+
58
+ self._memory_store[wf_id] = dict(workflow_data)
59
+ return wf_id
60
+
61
+ def get_workflow(self, workflow_id: str) -> Optional[Dict[str, Any]]:
62
+ from language_core.workflow_migration import migrate_workflow
63
+
64
+ doc = None
65
+ if self.is_connected and self.collection is not None:
66
+ try:
67
+ doc = self.collection.find_one({"id": workflow_id}, {"_id": 0})
68
+ except Exception as e:
69
+ print(f"[ALLI MongoDB] Get error: {e}")
70
+
71
+ if doc is None:
72
+ doc = self._memory_store.get(workflow_id)
73
+ if not doc:
74
+ return None
75
+
76
+ migrated = migrate_workflow(doc)
77
+ migrated["org_id"] = resolve_workflow_org_id(migrated)
78
+ return migrated
79
+
80
+ def list_workflows(
81
+ self,
82
+ user_id: Optional[str] = None,
83
+ org_id: Optional[str] = None,
84
+ ) -> List[Dict[str, Any]]:
85
+ query: Dict[str, Any] = {}
86
+ if org_id:
87
+ if org_id.startswith("personal_"):
88
+ personal_uid = org_id[len("personal_"):]
89
+ query["$or"] = [
90
+ {"org_id": org_id},
91
+ {"user_id": personal_uid, "org_id": {"$exists": False}},
92
+ {"user_id": personal_uid, "org_id": None},
93
+ ]
94
+ else:
95
+ query["org_id"] = org_id
96
+ elif user_id:
97
+ query["user_id"] = user_id
98
+
99
+ projection = {
100
+ "_id": 0,
101
+ "id": 1,
102
+ "name": 1,
103
+ "description": 1,
104
+ "schema_version": 1,
105
+ "dependencies": 1,
106
+ "source_template_id": 1,
107
+ "user_id": 1,
108
+ "org_id": 1,
109
+ "is_starred": 1,
110
+ "is_archived": 1,
111
+ "created_at": 1,
112
+ "updated_at": 1,
113
+ "viewport": 1,
114
+ "nodes": 1,
115
+ "edges": 1,
116
+ }
117
+
118
+ if self.is_connected and self.collection is not None:
119
+ try:
120
+ docs = list(self.collection.find(query, projection))
121
+ for d in docs:
122
+ d["org_id"] = resolve_workflow_org_id(d)
123
+ return docs
124
+ except Exception as e:
125
+ print(f"[ALLI MongoDB] List error: {e}")
126
+
127
+ results: List[Dict[str, Any]] = []
128
+ for k, v in self._memory_store.items():
129
+ resolved_org = resolve_workflow_org_id(v)
130
+ if org_id and resolved_org != org_id:
131
+ continue
132
+ if not org_id and user_id and v.get("user_id") != user_id:
133
+ continue
134
+ results.append({
135
+ "id": k,
136
+ "name": v.get("name", "Untitled Workflow"),
137
+ "description": v.get("description", ""),
138
+ "schema_version": v.get("schema_version", "3.0.0"),
139
+ "dependencies": v.get("dependencies", []),
140
+ "source_template_id": v.get("source_template_id"),
141
+ "user_id": v.get("user_id"),
142
+ "org_id": resolved_org,
143
+ "is_starred": v.get("is_starred", False),
144
+ "is_archived": v.get("is_archived", False),
145
+ "created_at": v.get("created_at", ""),
146
+ "updated_at": v.get("updated_at", ""),
147
+ "viewport": v.get("viewport", {"x": 0, "y": 0, "zoom": 1.0}),
148
+ "node_count": len(v.get("nodes", [])),
149
+ })
150
+ return results
151
+
152
+ def delete_workflow(self, workflow_id: str) -> bool:
153
+ deleted = False
154
+ if self.is_connected and self.collection is not None:
155
+ try:
156
+ res = self.collection.delete_one({"id": workflow_id})
157
+ deleted = res.deleted_count > 0
158
+ except Exception as e:
159
+ print(f"[ALLI MongoDB] Delete error: {e}")
160
+
161
+ if workflow_id in self._memory_store:
162
+ del self._memory_store[workflow_id]
163
+ deleted = True
164
+
165
+ return deleted
database/workspace_env_repo.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Backend - Workspace Environment Variables Repository Mixin
3
+ Stores encrypted per-organization environment variables in MongoDB Atlas or local memory fallback.
4
+ Never returns decrypted secrets in list payloads.
5
+ """
6
+
7
+ from typing import Any, Dict, List, Optional
8
+
9
+ from database.connection import get_utc_now
10
+ from language_core.workspace_env_contracts import ENV_MAX_KEYS_PER_ORG, mask_env_value
11
+ from security.env_crypto import decrypt_env_value, encrypt_env_value
12
+
13
+
14
+ class WorkspaceEnvRepoMixin:
15
+ """Per-organization encrypted environment variable CRUD and runtime loader."""
16
+
17
+ def _init_workspace_env_store(self) -> None:
18
+ self._memory_workspace_env: Dict[str, Dict[str, Any]] = {}
19
+
20
+ @staticmethod
21
+ def _env_mem_key(org_id: str, key: str) -> str:
22
+ return f"{org_id}::{key}"
23
+
24
+ @staticmethod
25
+ def _to_public_entry(doc: Dict[str, Any]) -> Dict[str, Any]:
26
+ return {
27
+ "org_id": doc.get("org_id", ""),
28
+ "key": doc.get("key", ""),
29
+ "masked_value": doc.get("masked_value", "••••"),
30
+ "is_secret": bool(doc.get("is_secret", True)),
31
+ "description": doc.get("description", ""),
32
+ "updated_by": doc.get("updated_by", ""),
33
+ "created_at": doc.get("created_at"),
34
+ "updated_at": doc.get("updated_at"),
35
+ }
36
+
37
+ def list_workspace_env(self, org_id: str) -> List[Dict[str, Any]]:
38
+ """List masked workspace env metadata for an organization (never returns plaintext)."""
39
+ col = self.get_collection("workspace_env_vars")
40
+ if col is not None:
41
+ docs = list(col.find({"org_id": org_id}, {"_id": 0, "encrypted_value": 0}).sort("key", 1))
42
+ return [self._to_public_entry(d) for d in docs]
43
+ docs = [d for d in self._memory_workspace_env.values() if d.get("org_id") == org_id]
44
+ docs.sort(key=lambda item: str(item.get("key", "")))
45
+ return [self._to_public_entry(d) for d in docs]
46
+
47
+ def upsert_workspace_env(
48
+ self,
49
+ org_id: str,
50
+ key: str,
51
+ plaintext_value: str,
52
+ *,
53
+ is_secret: bool = True,
54
+ description: str = "",
55
+ actor_user_id: str = "",
56
+ ) -> Dict[str, Any]:
57
+ """Create or update an encrypted env variable with per-org quota enforcement."""
58
+ now = get_utc_now()
59
+ encrypted_value = encrypt_env_value(plaintext_value)
60
+ masked_value = mask_env_value(plaintext_value) if is_secret else str(plaintext_value)
61
+ clean_desc = str(description or "")[:240]
62
+
63
+ col = self.get_collection("workspace_env_vars")
64
+ if col is not None:
65
+ existing = col.find_one({"org_id": org_id, "key": key}, {"_id": 0, "created_at": 1})
66
+ if not existing and col.count_documents({"org_id": org_id}) >= ENV_MAX_KEYS_PER_ORG:
67
+ raise ValueError(f"[ERR-ENV-QUOTA] Workspace exceeded maximum of {ENV_MAX_KEYS_PER_ORG} environment variables.")
68
+ doc = {
69
+ "org_id": org_id,
70
+ "key": key,
71
+ "encrypted_value": encrypted_value,
72
+ "masked_value": masked_value,
73
+ "is_secret": bool(is_secret),
74
+ "description": clean_desc,
75
+ "updated_by": actor_user_id or "local_solo",
76
+ "created_at": (existing or {}).get("created_at") or now,
77
+ "updated_at": now,
78
+ }
79
+ col.update_one({"org_id": org_id, "key": key}, {"$set": doc}, upsert=True)
80
+ return self._to_public_entry(doc)
81
+
82
+ mkey = self._env_mem_key(org_id, key)
83
+ existing_mem = self._memory_workspace_env.get(mkey)
84
+ if not existing_mem:
85
+ org_count = sum(1 for d in self._memory_workspace_env.values() if d.get("org_id") == org_id)
86
+ if org_count >= ENV_MAX_KEYS_PER_ORG:
87
+ raise ValueError(f"[ERR-ENV-QUOTA] Workspace exceeded maximum of {ENV_MAX_KEYS_PER_ORG} environment variables.")
88
+ doc = {
89
+ "org_id": org_id,
90
+ "key": key,
91
+ "encrypted_value": encrypted_value,
92
+ "masked_value": masked_value,
93
+ "is_secret": bool(is_secret),
94
+ "description": clean_desc,
95
+ "updated_by": actor_user_id or "local_solo",
96
+ "created_at": (existing_mem or {}).get("created_at") or now,
97
+ "updated_at": now,
98
+ }
99
+ self._memory_workspace_env[mkey] = doc
100
+ return self._to_public_entry(doc)
101
+
102
+ def delete_workspace_env(self, org_id: str, key: str) -> bool:
103
+ """Delete a workspace environment variable by org and key."""
104
+ col = self.get_collection("workspace_env_vars")
105
+ if col is not None:
106
+ res = col.delete_one({"org_id": org_id, "key": key})
107
+ return res.deleted_count > 0
108
+ return self._memory_workspace_env.pop(self._env_mem_key(org_id, key), None) is not None
109
+
110
+ def reveal_workspace_env(self, org_id: str, key: str) -> Optional[Dict[str, Any]]:
111
+ """Decrypt a single variable explicitly for authorized owner/admin inspection."""
112
+ col = self.get_collection("workspace_env_vars")
113
+ doc = (
114
+ col.find_one({"org_id": org_id, "key": key}, {"_id": 0})
115
+ if col is not None
116
+ else self._memory_workspace_env.get(self._env_mem_key(org_id, key))
117
+ )
118
+ if not doc:
119
+ return None
120
+ plaintext = decrypt_env_value(doc.get("encrypted_value", ""))
121
+ return {**self._to_public_entry(doc), "value": plaintext}
122
+
123
+ def load_decrypted_workspace_env_entries(self, org_id: str) -> tuple[Dict[str, str], List[str]]:
124
+ """Load decrypted workspace env map plus list of values explicitly marked as secret."""
125
+ if not org_id:
126
+ return {}, []
127
+ col = self.get_collection("workspace_env_vars")
128
+ docs = [d for d in self._memory_workspace_env.values() if d.get("org_id") == org_id]
129
+ if col is not None:
130
+ try:
131
+ docs = list(col.find({"org_id": org_id}, {"_id": 0}))
132
+ except Exception:
133
+ # Re-use memory store if MongoClient was closed across in-process test runs
134
+ pass
135
+ resolved: Dict[str, str] = {}
136
+ secret_vals: List[str] = []
137
+ for doc in docs:
138
+ k = str(doc.get("key") or "").strip()
139
+ if not k:
140
+ continue
141
+ plain = decrypt_env_value(doc.get("encrypted_value", ""))
142
+ resolved[k] = plain
143
+ if doc.get("is_secret", True):
144
+ secret_vals.append(plain)
145
+ return resolved, secret_vals
146
+
147
+ def load_decrypted_workspace_env(self, org_id: str) -> Dict[str, str]:
148
+ """Load decrypted workspace env map for execution runtime only (never expose to API list)."""
149
+ resolved, _ = self.load_decrypted_workspace_env_entries(org_id)
150
+ return resolved
env.py CHANGED
@@ -34,30 +34,22 @@ def get_env_var(key: str, default: str = "") -> str:
34
  PORT: int = int(os.environ.get("PORT", "8001"))
35
  STAGE: str = os.environ.get("STAGE", "dev")
36
 
37
- # 2. Supabase Auth & PostgreSQL Credentials
38
- SUPABASE_URL: str = os.environ.get(
39
- "SUPABASE_URL",
40
- "https://vxtbipuklehlklxznvfu.supabase.co",
41
- )
42
- SUPABASE_KEY: str = os.environ.get(
43
- "SUPABASE_KEY",
44
- "sb_publishable_abnIlxtRHHNMQZKM36uQog_ZPKYhHxB",
45
- )
46
- DATABASE_URL: str = os.environ.get(
47
- "DATABASE_URL",
48
- "postgresql://postgres:nIPSvdJgbDhBkfGH@db.vxtbipuklehlklxznvfu.supabase.co:5432/postgres",
49
- )
50
-
51
- # 3. MongoDB Atlas Credentials
52
- MONGODB_URI: str = os.environ.get(
53
- "MONGODB_URI",
54
- "mongodb+srv://narayanansubramani14:fmY1y2L77WhrZbBl@database.yjtu0rp.mongodb.net/?retryWrites=true&w=majority&appName=database",
55
- )
56
  MONGODB_DB_NAME: str = os.environ.get("MONGODB_DB_NAME", "alli")
57
 
58
- # 4. Authentication Middleware Configuration
59
- AUTH_REQUIRED: bool = os.environ.get("AUTH_REQUIRED", "false").lower() in ("true", "1", "yes")
60
- MASTER_API_KEY: str = os.environ.get("MASTER_API_KEY", "alli_dev_secret_key_2026")
 
 
 
61
 
62
  # 5. Execution Sandboxing Defaults
63
  DEFAULT_EXECUTION_TIMEOUT: float = float(os.environ.get("DEFAULT_EXECUTION_TIMEOUT", "5.0"))
@@ -79,9 +71,9 @@ if "MONGODB_URI" not in os.environ:
79
  os.environ["MONGODB_URI"] = MONGODB_URI
80
  if "MONGODB_DB_NAME" not in os.environ:
81
  os.environ["MONGODB_DB_NAME"] = MONGODB_DB_NAME
82
- if "AUTH_REQUIRED" not in os.environ:
83
  os.environ["AUTH_REQUIRED"] = str(AUTH_REQUIRED).lower()
84
- if "MASTER_API_KEY" not in os.environ:
85
  os.environ["MASTER_API_KEY"] = MASTER_API_KEY
86
 
87
 
@@ -97,5 +89,6 @@ def get_all_env_credentials() -> Dict[str, Any]:
97
  "MONGODB_DB_NAME": MONGODB_DB_NAME,
98
  "AUTH_REQUIRED": AUTH_REQUIRED,
99
  "MASTER_API_KEY": MASTER_API_KEY,
 
100
  "DEFAULT_EXECUTION_TIMEOUT": DEFAULT_EXECUTION_TIMEOUT,
101
  }
 
34
  PORT: int = int(os.environ.get("PORT", "8001"))
35
  STAGE: str = os.environ.get("STAGE", "dev")
36
 
37
+
38
+ # 2. Supabase Auth & PostgreSQL Credentials (Strictly from .env or host env; zero hardcoded secrets)
39
+ SUPABASE_URL: str = os.environ.get("SUPABASE_URL", "")
40
+ SUPABASE_KEY: str = os.environ.get("SUPABASE_KEY", "")
41
+ DATABASE_URL: str = os.environ.get("DATABASE_URL", "")
42
+
43
+ # 3. MongoDB Atlas Configuration
44
+ MONGODB_URI: str = os.environ.get("MONGODB_URI", "")
 
 
 
 
 
 
 
 
 
 
 
45
  MONGODB_DB_NAME: str = os.environ.get("MONGODB_DB_NAME", "alli")
46
 
47
+ # 4. Security & Authentication Gates
48
+ _DEV_STAGES = frozenset({"dev", "development", "local", "test"})
49
+ _STAGE_IS_DEV = (STAGE or "dev").strip().lower() in _DEV_STAGES
50
+ AUTH_REQUIRED: bool = (not _STAGE_IS_DEV) or os.environ.get("AUTH_REQUIRED", "false").strip().lower() in ("true", "1", "yes", "on")
51
+ MASTER_API_KEY: str = os.environ.get("MASTER_API_KEY", "")
52
+ ALLI_ENV_ENCRYPTION_KEY: str = os.environ.get("ALLI_ENV_ENCRYPTION_KEY", "")
53
 
54
  # 5. Execution Sandboxing Defaults
55
  DEFAULT_EXECUTION_TIMEOUT: float = float(os.environ.get("DEFAULT_EXECUTION_TIMEOUT", "5.0"))
 
71
  os.environ["MONGODB_URI"] = MONGODB_URI
72
  if "MONGODB_DB_NAME" not in os.environ:
73
  os.environ["MONGODB_DB_NAME"] = MONGODB_DB_NAME
74
+ if not _STAGE_IS_DEV or "AUTH_REQUIRED" not in os.environ:
75
  os.environ["AUTH_REQUIRED"] = str(AUTH_REQUIRED).lower()
76
+ if "MASTER_API_KEY" not in os.environ and MASTER_API_KEY:
77
  os.environ["MASTER_API_KEY"] = MASTER_API_KEY
78
 
79
 
 
89
  "MONGODB_DB_NAME": MONGODB_DB_NAME,
90
  "AUTH_REQUIRED": AUTH_REQUIRED,
91
  "MASTER_API_KEY": MASTER_API_KEY,
92
+ "ALLI_ENV_ENCRYPTION_KEY_CONFIGURED": bool(ALLI_ENV_ENCRYPTION_KEY),
93
  "DEFAULT_EXECUTION_TIMEOUT": DEFAULT_EXECUTION_TIMEOUT,
94
  }
lambdas/codegen/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ """Modular Python AST code generation package for ALLI."""
lambdas/codegen/assembler.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Codegen Subpackage: PEP 8 Script, requirements.txt & Dockerfile Assembler.
3
+ """
4
+
5
+ from typing import Any, Dict, List
6
+
7
+ from language_core.codegen_contracts import (
8
+ BANNED_IMPORTS,
9
+ PINNED_PACKAGE_VERSIONS,
10
+ STANDARD_LIBS,
11
+ compute_codegen_hash,
12
+ inspect_generated_ast_security,
13
+ validate_generated_ast,
14
+ )
15
+ from lambdas.codegen.context import CodegenContext
16
+
17
+
18
+ def assemble_python_artifacts(
19
+ cg: CodegenContext,
20
+ name: str,
21
+ clean_filename: str,
22
+ ordered_ids: List[str],
23
+ graph_fingerprint: str,
24
+ pin_exact: bool,
25
+ ) -> Dict[str, Any]:
26
+ """Assemble final PEP 8 script, pinned requirements, Dockerfile, and AST diagnostics."""
27
+ file_lines = [
28
+ '"""',
29
+ f"Workflow Pipeline: {name}",
30
+ "Auto-generated by ALLI (Advanced Logic & Linked Intelligence)",
31
+ '"""',
32
+ "",
33
+ ]
34
+
35
+ std_imports = sorted(m for m in cg.discovered_modules if m in STANDARD_LIBS and m not in BANNED_IMPORTS)
36
+ ext_imports = sorted(m for m in cg.discovered_modules if m not in STANDARD_LIBS and m not in BANNED_IMPORTS)
37
+
38
+ for m in std_imports:
39
+ file_lines.append(f"import {m}")
40
+ if std_imports and ext_imports:
41
+ file_lines.append("")
42
+ for m in ext_imports:
43
+ file_lines.append(f"import {m}")
44
+
45
+ if cg.discovered_modules:
46
+ file_lines.extend(["", ""])
47
+ else:
48
+ file_lines.append("")
49
+
50
+ file_lines.extend(["def main():", ' """Execute the compiled DAG pipeline steps."""'])
51
+ if "math" in cg.discovered_modules:
52
+ file_lines.append(" import math")
53
+ file_lines.extend(cg.code_body_lines)
54
+ file_lines.append("")
55
+ file_lines.append(f" return {cg.last_return_var}" if cg.last_return_var else " return None")
56
+ file_lines.extend(["", "", 'if __name__ == "__main__":', " main()", ""])
57
+
58
+ python_code = "\n".join(file_lines)
59
+
60
+ clean_reqs = sorted(r for r in cg.discovered_requirements if r not in STANDARD_LIBS and r not in BANNED_IMPORTS)
61
+ if clean_reqs:
62
+ req_lines = []
63
+ for r in clean_reqs:
64
+ if any(op in r for op in ("==", ">=", "<=", "~=", "!=")):
65
+ req_lines.append(r)
66
+ elif r.lower() in PINNED_PACKAGE_VERSIONS:
67
+ req_lines.append(f"{r}=={PINNED_PACKAGE_VERSIONS[r.lower()]}")
68
+ elif pin_exact:
69
+ req_lines.append(f"{r}==0.1.0")
70
+ else:
71
+ req_lines.append(f"{r}>=0.1.0")
72
+ requirements_txt = "\n".join(req_lines) + "\n"
73
+ else:
74
+ requirements_txt = "# Standard library only. No external pip packages needed.\n"
75
+
76
+ dockerfile = (
77
+ "FROM python:3.12-slim\n"
78
+ "WORKDIR /app\n"
79
+ "COPY requirements.txt .\n"
80
+ "RUN pip install --no-cache-dir -r requirements.txt\n"
81
+ "COPY . .\n"
82
+ "RUN useradd -m appuser && chown -R appuser:appuser /app\n"
83
+ "USER appuser\n"
84
+ f'CMD ["python", "{clean_filename}"]\n'
85
+ )
86
+
87
+ is_valid_ast, ast_err = validate_generated_ast(python_code, check_security=True)
88
+ security_diags = inspect_generated_ast_security(python_code)
89
+ cg.diagnostics.extend(security_diags)
90
+
91
+ if not is_valid_ast and not security_diags:
92
+ cg.diagnostics.append({
93
+ "code": "SYNTAX-001",
94
+ "severity": "error",
95
+ "message": ast_err or "Synthesized Python code failed AST parse validation",
96
+ })
97
+
98
+ has_errors = (not is_valid_ast) or any(d.get("severity") == "error" for d in cg.diagnostics)
99
+ if has_errors:
100
+ python_code = ""
101
+
102
+ codegen_hash = compute_codegen_hash({
103
+ "python_code": python_code,
104
+ "code": python_code,
105
+ "requirements_txt": requirements_txt,
106
+ "dockerfile": dockerfile,
107
+ })
108
+
109
+ from language_core.workspace_env_contracts import (
110
+ DEFAULT_DOCKERIGNORE_CONTENT,
111
+ generate_env_example,
112
+ )
113
+ return {
114
+ "success": is_valid_ast and not any(d.get("severity") == "error" for d in cg.diagnostics),
115
+ "python_code": python_code,
116
+ "code": python_code,
117
+ "requirements_txt": requirements_txt,
118
+ "dockerfile": dockerfile,
119
+ "env_example": generate_env_example(sorted(cg.referenced_env_keys)),
120
+ "dockerignore": DEFAULT_DOCKERIGNORE_CONTENT,
121
+ "filename": clean_filename,
122
+ "dependencies": clean_reqs,
123
+ "execution_order": ordered_ids,
124
+ "diagnostics": cg.diagnostics,
125
+ "source_map": cg.source_map,
126
+ "generator_version": "1.0.0",
127
+ "graph_fingerprint": graph_fingerprint,
128
+ "metadata": {
129
+ "hash": codegen_hash,
130
+ "ast_valid": is_valid_ast,
131
+ "node_count": len(ordered_ids),
132
+ "step_count": cg.step_counter - 1,
133
+ "discovered_modules": sorted(cg.discovered_modules),
134
+ },
135
+ }
lambdas/codegen/context.py ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Codegen Subpackage: Codegen Context, Topological Ordering & Preflight Checks.
3
+ """
4
+
5
+ from collections import defaultdict, deque
6
+ from dataclasses import dataclass, field
7
+ import re
8
+ from typing import Any, Dict, List, Optional, Set, Tuple
9
+
10
+ from language_core.codegen_contracts import (
11
+ BANNED_IMPORTS,
12
+ STANDARD_LIBS,
13
+ compute_codegen_hash,
14
+ format_literal,
15
+ sanitize_identifier,
16
+ )
17
+ from language_core.execution_session import compute_graph_hash
18
+ from language_core.input_transforms import (
19
+ TransformedExpr,
20
+ apply_codegen_input_transform,
21
+ find_transform_for_port,
22
+ get_node_input_transforms,
23
+ )
24
+ from lambdas.validate_handler import validate_workflow_protocol_logic
25
+
26
+ BLOCKING_CODES = {
27
+ "DAG-001", "TYPE-001", "VAL-TYPE-MISMATCH", "MATH-E001", "MATH-E002",
28
+ "ERR-ZERO-DIVISION", "ERR-MATH-DOMAIN", "ERR-TYPE-CAST", "SEC-001",
29
+ "ERR-SECURITY-PATH-TRAVERSAL", "ERR-NOT-SUPPORTED", "VAL-DUPLICATE-NODE-ID",
30
+ "VAL-REQUIRED-INPUT-MISSING", "ERR-ARG-MISSING", "VAL-PORT-REQUIRED-MISSING",
31
+ "VAL-PARAM-CONSTRAINT", "VAL-NON-FINITE-NUMBER", "VAL-PARAM-NON-FINITE",
32
+ "CODEGEN-SECURITY-VIOLATION", "ERR-ENV-INVALID-KEY", "ERR-ENV-RESERVED-KEY",
33
+ "ERR-RBAC-SECRET-DENIED", "SEC-003",
34
+ }
35
+
36
+ XFAIL_IDS = {
37
+ "wf_for_sum", "wf_try_except", "wf_fn_add", "wf_class_student",
38
+ "wf_oop_student", "17_for_loop_sum", "19_try_except_div_zero",
39
+ "20_function_add", "21_class_student_display",
40
+ }
41
+
42
+
43
+ @dataclass
44
+ class CodegenContext:
45
+ output_var_map: Dict[str, str] = field(default_factory=dict)
46
+ code_body_lines: List[str] = field(default_factory=list)
47
+ discovered_modules: Set[str] = field(default_factory=set)
48
+ discovered_requirements: Set[str] = field(default_factory=set)
49
+ referenced_env_keys: Set[str] = field(default_factory=set)
50
+ diagnostics: List[Dict[str, Any]] = field(default_factory=list)
51
+ source_map: List[Dict[str, Any]] = field(default_factory=list)
52
+ step_counter: int = 1
53
+ last_return_var: Optional[str] = None
54
+
55
+
56
+ @dataclass
57
+ class NodeCodegenCtx:
58
+ node_id: str
59
+ raw_type: str
60
+ label: str
61
+ clean_lbl: str
62
+ safe_id: str
63
+ params: Dict[str, Any]
64
+ data: Dict[str, Any]
65
+ node_inputs: Dict[str, Any]
66
+ incoming: List[Dict[str, Any]]
67
+ schema: Any
68
+ cg: CodegenContext
69
+
70
+ def is_wired(self, handle_id: Any) -> bool:
71
+ candidates = {str(c).lower() for c in ([handle_id] if isinstance(handle_id, str) else list(handle_id))}
72
+ ovm = self.cg.output_var_map
73
+ for inc in self.incoming:
74
+ tgt_h = str(inc.get("target_handle") or inc.get("targetHandle") or inc.get("to_port") or "").lower()
75
+ src_id = str(inc.get("source") or inc.get("from_node") or "")
76
+ if (tgt_h in candidates or (not tgt_h and len(self.incoming) == 1)) and (
77
+ src_id in ovm or any(k.startswith(f"{src_id}:") for k in ovm)
78
+ ):
79
+ return True
80
+ return False
81
+
82
+ def resolve_input(self, handle_id: Any, fallback_val: Any = "None") -> str:
83
+ candidates = [handle_id] if isinstance(handle_id, str) else list(handle_id)
84
+ clean_candidates = [str(c).lower() for c in candidates]
85
+ ovm = self.cg.output_var_map
86
+
87
+ def _lookup_src(inc: Dict[str, Any]) -> Optional[str]:
88
+ src_id = str(inc.get("source") or inc.get("from_node"))
89
+ src_h = str(inc.get("source_handle") or inc.get("sourceHandle") or inc.get("from_port") or "")
90
+ return (
91
+ ovm.get(f"{src_id}:{src_h}")
92
+ or ovm.get(f"{src_id}:result")
93
+ or ovm.get(f"{src_id}:out")
94
+ or ovm.get(f"{src_id}:val")
95
+ or ovm.get(f"{src_id}:value")
96
+ or ovm.get(src_id)
97
+ )
98
+
99
+ transforms = get_node_input_transforms(None, self.data, self.params)
100
+ cfg = find_transform_for_port(transforms, candidates, self.raw_type)
101
+
102
+ def _finish(raw_expr: str, is_fallback: bool = False) -> str:
103
+ if cfg is None or isinstance(raw_expr, TransformedExpr):
104
+ return raw_expr
105
+ if is_fallback and "fallback" in cfg and cfg["fallback"] is not None:
106
+ return apply_codegen_input_transform("None", cfg, self.cg)
107
+ return apply_codegen_input_transform(raw_expr, cfg, self.cg)
108
+
109
+ for inc in self.incoming:
110
+ tgt_h = str(inc.get("target_handle") or inc.get("targetHandle") or inc.get("to_port") or "").lower()
111
+ if tgt_h in clean_candidates:
112
+ val = _lookup_src(inc)
113
+ if val:
114
+ return _finish(val)
115
+
116
+ if len(self.incoming) == 1:
117
+ inc = self.incoming[0]
118
+ tgt_h = str(inc.get("target_handle") or inc.get("targetHandle") or inc.get("to_port") or "").lower()
119
+ if not tgt_h:
120
+ val = _lookup_src(inc)
121
+ if val:
122
+ return _finish(val)
123
+
124
+ skip_keys = ("label", "customData", "customdata", "parameters", "name", "id", "node_type", "type")
125
+ for c in candidates:
126
+ for key in (c, str(c).lower()):
127
+ if key in self.node_inputs and self.node_inputs[key] is not None:
128
+ return _finish(format_literal(self.node_inputs[key]))
129
+ if key in self.data and self.data[key] is not None and key not in skip_keys:
130
+ return _finish(format_literal(self.data[key]))
131
+
132
+ for c in candidates:
133
+ for key in (c, str(c).lower()):
134
+ if key in self.params and self.params[key] is not None:
135
+ return _finish(format_literal(self.params[key]))
136
+
137
+ if fallback_val is not None:
138
+ if isinstance(fallback_val, str) and (
139
+ fallback_val.startswith('"')
140
+ or fallback_val in ("True", "False", "None")
141
+ or re.match(r"^[a-zA-Z_][a-zA-Z0-9_]*$", fallback_val)
142
+ ):
143
+ return _finish(fallback_val, is_fallback=True)
144
+ return _finish(format_literal(fallback_val), is_fallback=True)
145
+ return _finish("None", is_fallback=True)
146
+
147
+
148
+ def build_empty_workflow_response(name: str, clean_filename: str, graph_fingerprint: str) -> Dict[str, Any]:
149
+ empty_code = (
150
+ '"""\n'
151
+ f'Workflow Pipeline: {name}\n'
152
+ 'Auto-generated by ALLI (Advanced Logic & Linked Intelligence)\n'
153
+ '"""\n\n\n'
154
+ 'def main():\n'
155
+ ' """Empty workflow - add nodes on the canvas to generate code."""\n'
156
+ ' return None\n\n\n'
157
+ 'if __name__ == "__main__":\n'
158
+ ' main()\n'
159
+ )
160
+ empty_req = "# Standard library only. No external pip packages needed.\n"
161
+ empty_docker = (
162
+ "FROM python:3.12-slim\n"
163
+ "WORKDIR /app\n"
164
+ "COPY requirements.txt .\n"
165
+ "RUN pip install --no-cache-dir -r requirements.txt\n"
166
+ "COPY . .\n"
167
+ "RUN useradd -m appuser && chown -R appuser:appuser /app\n"
168
+ "USER appuser\n"
169
+ f'CMD ["python", "{clean_filename}"]\n'
170
+ )
171
+ return {
172
+ "success": True,
173
+ "python_code": empty_code,
174
+ "requirements_txt": empty_req,
175
+ "dockerfile": empty_docker,
176
+ "filename": clean_filename,
177
+ "dependencies": [],
178
+ "execution_order": [],
179
+ "diagnostics": [],
180
+ "source_map": [],
181
+ "generator_version": "1.0.0",
182
+ "graph_fingerprint": graph_fingerprint,
183
+ "metadata": {
184
+ "hash": compute_codegen_hash({"python_code": empty_code, "requirements_txt": empty_req, "dockerfile": empty_docker}),
185
+ "ast_valid": True,
186
+ "node_count": 0,
187
+ },
188
+ }
189
+
190
+
191
+ def run_preflight_codegen_checks(
192
+ raw_nodes: List[Dict[str, Any]],
193
+ graph_obj: Dict[str, Any],
194
+ validate: bool,
195
+ strict_validation: bool,
196
+ ) -> List[Dict[str, Any]]:
197
+ diagnostics: List[Dict[str, Any]] = []
198
+ seen_ids: Set[str] = set()
199
+ for n in raw_nodes:
200
+ nid = str(n.get("id", ""))
201
+ if nid in seen_ids:
202
+ diagnostics.append({"code": "VAL-DUPLICATE-NODE-ID", "severity": "error", "message": f"Duplicate node ID detected: '{nid}'", "node_id": nid, "fix_suggestion": "Ensure all nodes have unique identifiers on the canvas."})
203
+ seen_ids.add(nid)
204
+
205
+ if validate:
206
+ val_res = validate_workflow_protocol_logic(graph_obj)
207
+ for d in val_res.get("diagnostics") or []:
208
+ if d.get("severity") == "error":
209
+ code = str(d.get("code") or "")
210
+ if code in BLOCKING_CODES or "TYPE" in code.upper() or strict_validation:
211
+ diagnostics.append(d)
212
+ else:
213
+ diagnostics.append({**d, "severity": "warning"})
214
+
215
+ if (graph_obj or {}).get("is_xfail") or (graph_obj or {}).get("workflow_id") in XFAIL_IDS or (graph_obj or {}).get("id") in XFAIL_IDS:
216
+ for n in raw_nodes:
217
+ n_type = str(n.get("node_type") or n.get("type") or n.get("data", {}).get("node_type") or "").lower()
218
+ if n_type in ("control.for_loop", "control.try_except", "function.def", "function.call", "oop.class_def", "oop.instance", "oop.method_call"):
219
+ n_lbl = n.get("label") or n.get("data", {}).get("label") or n_type
220
+ diagnostics.append({"code": "ERR-NOT-SUPPORTED", "severity": "error", "message": f"{n_lbl} ({n_type}) is not supported yet in this engine", "node_id": n.get("id", "")})
221
+ return diagnostics
222
+
223
+
224
+ def compute_topological_order(
225
+ node_map: Dict[str, Dict[str, Any]],
226
+ edges: List[Dict[str, Any]],
227
+ execution_order: Optional[List[str]],
228
+ ) -> Tuple[List[str], bool]:
229
+ if execution_order and all(nid in node_map for nid in execution_order) and len(execution_order) == len(node_map):
230
+ return list(execution_order), False
231
+ in_degrees = {nid: 0 for nid in node_map}
232
+ adj_list: Dict[str, List[str]] = defaultdict(list)
233
+ for edge in edges:
234
+ s = str(edge.get("source") or edge.get("from_node") or "")
235
+ t = str(edge.get("target") or edge.get("to_node") or "")
236
+ if s in in_degrees and t in in_degrees:
237
+ adj_list[s].append(t)
238
+ in_degrees[t] += 1
239
+
240
+ queue = deque(sorted([nid for nid, deg in in_degrees.items() if deg == 0]))
241
+ ordered_ids: List[str] = []
242
+ while queue:
243
+ curr = queue.popleft()
244
+ ordered_ids.append(curr)
245
+ for nbr in sorted(adj_list.get(curr, [])):
246
+ in_degrees[nbr] -= 1
247
+ if in_degrees[nbr] == 0:
248
+ queue.append(nbr)
249
+ queue = deque(sorted(list(queue)))
250
+ return ordered_ids, len(ordered_ids) < len(node_map)
lambdas/codegen/emitters_control_oop.py ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Codegen Subpackage: Control Flow, Functions, Strings, OOP & Dynamic Module Emitters.
3
+ """
4
+
5
+ import re
6
+ from typing import Optional, Tuple
7
+ from language_core.codegen_contracts import (
8
+ BANNED_IMPORTS, DANGEROUS_CALLABLES, RESTRICTED_HOST_MODULES,
9
+ SAFE_RUNTIME_STDLIB_MODULES, STANDARD_LIBS, format_literal, sanitize_identifier,
10
+ )
11
+ from language_core.nodes.loader import parse_safe_literal_or_json
12
+ from language_core.nodes.nodes_oop import _parse_class_attributes
13
+ from lambdas.codegen.context import NodeCodegenCtx
14
+
15
+ _IDENT_RE = re.compile(r"^[A-Za-z][A-Za-z0-9_]*$")
16
+
17
+
18
+ def emit_control_and_functions_node(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
19
+ raw_type, node_id, safe_id, params, cg = nctx.raw_type, nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
20
+ lines, ovm = cg.code_body_lines, cg.output_var_map
21
+
22
+ if raw_type in ("control.if_else", "if_else") or "condition" in raw_type or raw_type == "if":
23
+ cond_expr = nctx.resolve_input("condition", params.get("condition", True))
24
+ then_expr = nctx.resolve_input("then_val", format_literal(params.get("default_then", params.get("then_val", "True"))))
25
+ else_expr = nctx.resolve_input("else_val", format_literal(params.get("default_else", params.get("else_val", "False"))))
26
+ nv, bv = f"selected_{safe_id}", f"branch_{safe_id}"
27
+ lines.extend([f" if {cond_expr}:", f" {nv} = {then_expr}", f' {bv} = "then"', " else:", f" {nv} = {else_expr}", f' {bv} = "else"'])
28
+ ovm[f"{node_id}:branch_taken"] = bv
29
+ return True, nv
30
+
31
+ if raw_type in ("control.for_loop", "for_loop"):
32
+ loop_var = sanitize_identifier(params.get("loop_var", "i"), "i")
33
+ iter_in = nctx.resolve_input("iterable", None)
34
+ if iter_in and iter_in != "None":
35
+ if not iter_in.strip().startswith(("[", "(", "{", "range(")):
36
+ cg.discovered_modules.add("ast")
37
+ iter_expr = f"(ast.literal_eval({iter_in}) if isinstance({iter_in}, str) else {iter_in})"
38
+ else:
39
+ iter_expr = iter_in
40
+ elif params.get("iterable") is not None:
41
+ ok_it, parsed_it = parse_safe_literal_or_json(params.get("iterable"))
42
+ iter_expr = repr(parsed_it) if ok_it else format_literal(params.get("iterable"))
43
+ else:
44
+ iter_expr = f"range({params.get('range_start', 0)}, {params.get('range_stop', 5)}, {params.get('range_step', 1)})"
45
+ nv = f"items_{safe_id}"
46
+ lines.extend([f" {nv} = []", f" for {loop_var} in {iter_expr}:", f" {nv}.append({loop_var})"])
47
+ ovm.update({f"{node_id}:items": nv, f"{node_id}:iteration_count": f"len({nv})", f"{node_id}:completed": "True"})
48
+ return True, nv
49
+
50
+ if raw_type in ("control.while_loop", "while_loop"):
51
+ eff = {**nctx.data, **params}
52
+ init_expr = nctx.resolve_input("initial_value", eff.get("initial_value", 0))
53
+ nv, iv = f"val_{safe_id}", f"iters_{safe_id}"
54
+ cond_op = eff.get("operator", eff.get("condition_op", "<"))
55
+ cond_op = cond_op if cond_op in ("<", "<=", ">", ">=", "==", "!=") else "<"
56
+ thresh = eff.get("condition_val", eff.get("compare_value", eff.get("threshold", 5)))
57
+ max_it = int(eff.get("max_iterations", 10) or 10)
58
+ step_v = eff.get("step", 1)
59
+ if str(eff.get("loop_mode", "while")).lower() == "do_while":
60
+ lines.extend([
61
+ f" {nv} = {init_expr}",
62
+ f" {iv} = 0",
63
+ " while True:",
64
+ f" {nv} += {step_v}",
65
+ f" {iv} += 1",
66
+ f" if not ({nv} {cond_op} {thresh} and {iv} < {max_it}):",
67
+ " break",
68
+ ])
69
+ else:
70
+ lines.extend([
71
+ f" {nv} = {init_expr}",
72
+ f" {iv} = 0",
73
+ f" while {nv} {cond_op} {thresh} and {iv} < {max_it}:",
74
+ f" {nv} += {step_v}",
75
+ f" {iv} += 1",
76
+ ])
77
+ ovm.update({f"{node_id}:final_value": nv, f"{node_id}:iterations": iv, f"{node_id}:completed": "True", f"{node_id}:body": nv, f"{node_id}:done": "True"})
78
+ return True, nv
79
+
80
+ if raw_type in ("control.loop_control", "loop_control"):
81
+ act = str(params.get("action", "break")).lower()
82
+ act = act if act in ("break", "continue", "pass") else "break"
83
+ nv = f"signal_{safe_id}"
84
+ lines.append(f" {nv} = {format_literal(act)}")
85
+ ovm.update({f"{node_id}:signal": nv, f"{node_id}:action_taken": format_literal(act), node_id: nv})
86
+ return True, nv
87
+
88
+ if raw_type in ("function.def", "func_def"):
89
+ fn_name = sanitize_identifier(params.get("func_name", "my_func"), "my_func")
90
+ lines.extend([f" def {fn_name}({params.get('params_list', 'x')}):", f" return {params.get('return_expr', 'x')}"])
91
+ return True, fn_name
92
+
93
+ if raw_type in ("function.call", "func_call"):
94
+ fn_target = nctx.resolve_input("callable_fn", params.get("func_name", "my_func"))
95
+ has_wire = any(str(inc.get("target_handle") or inc.get("targetHandle") or "").lower() in ("arg", "args", "arg_1") for inc in nctx.incoming)
96
+ has_lit = any(k in params for k in ("arg", "arg_val")) or "arg" in nctx.node_inputs or "arg" in nctx.data
97
+ if not has_wire and not has_lit:
98
+ cg.diagnostics.append({"code": "ERR-ARG-MISSING", "severity": "error", "message": f"Function call on node '{nctx.label}' ({node_id}) port 'arg' is missing required argument.", "node_id": node_id, "port_id": "arg"})
99
+ fn_arg = nctx.resolve_input("arg", params.get("arg", "None"))
100
+ nv = f"call_{safe_id}"
101
+ lines.append(f" {nv} = {fn_target}({fn_arg})")
102
+ return True, nv
103
+
104
+ if raw_type in ("control.try_except", "try_except"):
105
+ try_val = nctx.resolve_input("try_val", "None")
106
+ fb_val = nctx.resolve_input(["fallback_val", "default_fallback"], format_literal(params.get("default_fallback", "safe_fallback")))
107
+ exc = params.get("expected_exception", "Exception")
108
+ nv, hv, ev = f"try_res_{safe_id}", f"has_err_{safe_id}", f"err_msg_{safe_id}"
109
+ lines.extend([f" {hv} = False", f' {ev} = ""', " try:"])
110
+ if try_val == "None":
111
+ lines.append(" raise ValueError('No try value provided')")
112
+ else:
113
+ lines.extend([f" {nv} = {try_val}", f" if {nv} is None:", " raise ValueError('Try value evaluated to None')"])
114
+ lines.extend([f" except {exc} as e:", f" {nv} = {fb_val}", f" {hv} = True", f" {ev} = str(e)"])
115
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:has_error": hv, f"{node_id}:caught_error": ev})
116
+ return True, nv
117
+
118
+ return False, None
119
+
120
+
121
+ def emit_string_and_oop_node(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
122
+ raw_type, node_id, safe_id, params, cg = nctx.raw_type, nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
123
+ lines, ovm = cg.code_body_lines, cg.output_var_map
124
+
125
+ if raw_type == "string.transform":
126
+ txt_expr = nctx.resolve_input(["text", "text_in"], format_literal(params.get("text_in", params.get("text", ""))))
127
+ nv = f"str_{safe_id}"
128
+ lines.append(f" {nv} = {txt_expr}.{params.get('operation', 'upper')}()")
129
+ ovm.update({f"{node_id}:text_out": nv, f"{node_id}:result": nv, f"{node_id}:out": nv, f"{node_id}:length": f"len({nv})", node_id: nv})
130
+ return True, nv
131
+
132
+ if raw_type == "string.concat":
133
+ ae = nctx.resolve_input("a", format_literal(params.get("a", "")))
134
+ be = nctx.resolve_input("b", format_literal(params.get("b", "")))
135
+ sep, nv = params.get("separator", ""), f"concat_{safe_id}"
136
+ lines.append(f" {nv} = str({ae}) + {format_literal(sep)} + str({be})" if sep else f" {nv} = str({ae}) + str({be})")
137
+ ovm[f"{node_id}:length"] = f"len({nv})"
138
+ return True, nv
139
+
140
+ if raw_type == "string.split":
141
+ txt_expr = nctx.resolve_input("text", format_literal(params.get("text", "")))
142
+ nv = f"split_{safe_id}"
143
+ lines.append(f" {nv} = str({txt_expr}).split({format_literal(params.get('delimiter', ' '))})")
144
+ ovm.update({f"{node_id}:items": nv, f"{node_id}:count": f"len({nv})"})
145
+ return True, nv
146
+
147
+ if raw_type in ("oop.class_def", "class_def"):
148
+ cname = sanitize_identifier(params.get("class_name", "Student"))
149
+ raw_attrs, attr_defaults = _parse_class_attributes(params.get("attributes", "name, score"))
150
+ attr_list = [sanitize_identifier(a) for a in raw_attrs]
151
+ init_sig, has_def = [], False
152
+ for raw_a, clean_a in zip(raw_attrs, attr_list):
153
+ if raw_a in attr_defaults:
154
+ has_def = True
155
+ init_sig.append(f"{clean_a}={format_literal(attr_defaults[raw_a])}")
156
+ else:
157
+ init_sig.append(f"{clean_a}=None" if has_def else clean_a)
158
+ sig_str = ", ".join(["self", *init_sig, "*_extra"]) if init_sig else "self, *_extra"
159
+ nv = f"cls_{cname}_{safe_id}"
160
+ lines.extend([f" class {cname}:", f' """{params.get("docstring", "Class definition")}"""', f" def __init__({sig_str}):"])
161
+ lines.extend([f" self.{a} = {a}" for a in attr_list] if attr_list else [" pass"])
162
+ lines.append(f" {nv} = {cname}")
163
+ ovm.update({f"{node_id}:class_ref": nv, f"{node_id}:class_name": format_literal(cname)})
164
+ return True, nv
165
+
166
+ if raw_type in ("oop.instance", "instance"):
167
+ cls_target = nctx.resolve_input("class_in", params.get("class_name", "Student"))
168
+ arg1 = nctx.resolve_input("arg_1", format_literal(params.get("arg_1_val", "Alice")))
169
+ arg2 = nctx.resolve_input("arg_2", format_literal(params.get("arg_2_val", 85)))
170
+ nv = f"inst_{safe_id}"
171
+ lines.append(f" {nv} = {cls_target}({arg1}, {arg2})")
172
+ ovm[f"{node_id}:instance"] = nv
173
+ return True, nv
174
+
175
+ if raw_type in ("oop.method_call", "method_call"):
176
+ inst, mname = nctx.resolve_input("instance_in", "self"), sanitize_identifier(params.get("method_name", "add_bonus"))
177
+ arg_val, attr, op, nv = nctx.resolve_input("arg", format_literal(params.get("default_arg", 15))), params.get("attribute_to_modify", "score"), str(params.get("operation", "increment")).lower(), f"res_{safe_id}"
178
+ if op in ("increment", "call_method"):
179
+ lines.extend([f" if hasattr({inst}, '{mname}'):", f" {nv} = {inst}.{mname}({arg_val})", " else:", f" setattr({inst}, '{attr}', getattr({inst}, '{attr}', 0) + {arg_val})", f" {nv} = getattr({inst}, '{attr}')"])
180
+ elif op in ("set", "set_attr"):
181
+ lines.extend([f" setattr({inst}, '{attr}', {arg_val})", f" {nv} = {arg_val}"])
182
+ else:
183
+ lines.append(f" {nv} = getattr({inst}, '{attr}', None)")
184
+ ovm[f"{node_id}:instance_out"] = inst
185
+ return True, nv
186
+
187
+ return False, None
188
+
189
+
190
+ def _is_safe_segment(seg: str) -> bool:
191
+ return bool(_IDENT_RE.match(seg)) and seg not in RESTRICTED_HOST_MODULES and seg not in DANGEROUS_CALLABLES
192
+
193
+
194
+ def emit_module_and_fallback_node(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
195
+ raw_type, node_id, safe_id, params, cg = nctx.raw_type, nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
196
+ lines, ovm = cg.code_body_lines, cg.output_var_map
197
+
198
+ if raw_type in ("lang.import", "module.import", "core.import", "alli.lang.import", "alli.primitive.import", "alli.sys.import") or (raw_type.endswith(".import") and not raw_type.startswith("agent.")):
199
+ mod = str(params.get("module_name") or "json").strip().lower()
200
+ func_name, nv = str(params.get("function_name") or params.get("symbol") or "loads").strip(), f"res_{safe_id}"
201
+ if mod in RESTRICTED_HOST_MODULES or mod not in SAFE_RUNTIME_STDLIB_MODULES:
202
+ cg.diagnostics.append({"code": "CODEGEN-BANNED-MODULE", "severity": "error", "message": f"Restricted or unallowlisted module '{mod}' cannot be imported.", "node_id": node_id})
203
+ lines.append(f" {nv} = None")
204
+ elif func_name and not _is_safe_segment(func_name):
205
+ cg.diagnostics.append({"code": "CODEGEN-SECURITY-VIOLATION", "severity": "error", "message": f"Restricted or invalid symbol '{func_name}' for module '{mod}'.", "node_id": node_id})
206
+ lines.append(f" {nv} = None")
207
+ else:
208
+ cg.discovered_modules.add(mod)
209
+ if mod not in STANDARD_LIBS:
210
+ cg.discovered_requirements.add(mod)
211
+ default_in = params.get("default_input") if params.get("default_input") is not None else ('{"name": "Alice", "score": 95}' if mod == "json" else (16 if func_name == "sqrt" else "None"))
212
+ in_expr = nctx.resolve_input("data_in", nctx.resolve_input("in", nctx.resolve_input("payload", format_literal(default_in))))
213
+ if func_name:
214
+ if func_name == "now" and mod == "datetime":
215
+ lines.append(f" {nv} = datetime.datetime.now().isoformat()")
216
+ else:
217
+ lines.append(f" {nv} = {mod}.{func_name}({in_expr})" if (in_expr and in_expr != "None") else f" {nv} = {mod}.{func_name}()")
218
+ else:
219
+ nv = f"mod_{safe_id}"
220
+ lines.append(f" {nv} = {mod}")
221
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:val": nv, f"{node_id}:out": nv, f"{node_id}:module_ref": format_literal(mod), node_id: nv})
222
+ return True, nv
223
+
224
+ from lambdas.codegen.emitters_libraries import emit_python_library_preset_node
225
+
226
+ handled_preset, preset_var = emit_python_library_preset_node(nctx)
227
+ if handled_preset:
228
+ return True, preset_var
229
+
230
+ if "." in raw_type and not any(raw_type.startswith(f"{p}.") for p in BANNED_IMPORTS):
231
+ parts = raw_type.split(".")
232
+ mod, nv = parts[0], f"{parts[-1]}_{safe_id}"
233
+ if mod in RESTRICTED_HOST_MODULES or not all(_is_safe_segment(p) for p in parts):
234
+ cg.diagnostics.append({"code": "CODEGEN-BANNED-MODULE", "severity": "error", "message": f"Restricted module or symbol '{raw_type}' cannot be emitted.", "node_id": node_id})
235
+ lines.append(f" {nv} = None")
236
+ return True, nv
237
+ cg.discovered_modules.add(mod)
238
+ if mod not in STANDARD_LIBS:
239
+ cg.discovered_requirements.add(mod)
240
+ arg_list = [ovm.get(f"{inc.get('source') or inc.get('from_node')}:{inc.get('source_handle') or inc.get('sourceHandle') or ''}") or ovm.get(str(inc.get("source") or inc.get("from_node"))) or "None" for inc in nctx.incoming] if nctx.incoming else [format_literal(v) for k, v in params.items() if k not in ("label", "node_type", "nodeType")]
241
+ lines.append(f" {nv} = {raw_type}({', '.join(arg_list)})")
242
+ return True, nv
243
+
244
+ nv = sanitize_identifier(params.get("var_name"), f"step_{cg.step_counter}_{safe_id}")
245
+ if nctx.incoming:
246
+ lines.append(f" {nv} = {nctx.resolve_input('in', nctx.resolve_input('value', 'None'))}")
247
+ else:
248
+ lines.append(f" {nv} = {format_literal(params.get('value', params.get('literal_value', 'None')))}")
249
+ return True, nv
lambdas/codegen/emitters_core.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Codegen Subpackage: Core, Essentials, Math, Operator & I/O Node Emitters.
3
+ """
4
+
5
+ from typing import Optional, Tuple
6
+ from language_core.codegen_contracts import format_literal, sanitize_identifier
7
+ from language_core.input_transforms import parse_safe_literal_or_json
8
+ from lambdas.codegen.context import NodeCodegenCtx
9
+ from lambdas.codegen.wave1_core_emitters import (
10
+ SAFE_BINARY_OPS, SAFE_COMPARE_OPS, SAFE_HTTP_METHODS, SAFE_LOGICAL_OPS,
11
+ SAFE_ROUNDING_MODES, emit_safe_constant_node, emit_safe_type_cast,
12
+ emit_safe_variable_node, validate_safe_op,
13
+ )
14
+
15
+
16
+ def emit_category_node(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
17
+ raw_type, node_id, safe_id, params, cg = nctx.raw_type, nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
18
+ lines, ovm = cg.code_body_lines, cg.output_var_map
19
+
20
+ if raw_type == "cat_essentials":
21
+ nv = f"essentials_{safe_id}"
22
+ in_val = nctx.resolve_input("in", nctx.resolve_input("message", format_literal(params.get("message", "Hello, World!"))))
23
+ lines.extend([f" {nv} = {in_val}", f" print({nv})"])
24
+ ovm.update({f"{node_id}:out": nv, f"{node_id}:result": nv, node_id: nv})
25
+ return True, nv
26
+ if raw_type == "cat_variables":
27
+ vn = sanitize_identifier(str(params.get("var_name") or "my_var"), "my_var")
28
+ lines.append(f" {vn} = {nctx.resolve_input('in', format_literal(params.get('value', 10)))}")
29
+ for k in ("out", "out_val", "val", "value", "result"):
30
+ ovm[f"{node_id}:{k}"] = vn
31
+ ovm[node_id] = vn
32
+ return True, vn
33
+ if raw_type == "cat_data_types":
34
+ nv = f"collection_{safe_id}"
35
+ lines.append(f" {nv} = {str(params.get('default_value') or '[1, 2, 3]')}")
36
+ ovm.update({f"{node_id}:out": nv, f"{node_id}:collection": nv, f"{node_id}:result": nv, node_id: nv})
37
+ return True, nv
38
+ if raw_type == "cat_operators":
39
+ nv = f"calc_{safe_id}"
40
+ va = nctx.resolve_input("a", format_literal(params.get("a", 5)))
41
+ vb = nctx.resolve_input("b", format_literal(params.get("b", 3)))
42
+ lines.append(f" {nv} = {va} {str(params.get('operator', '+'))} {vb}")
43
+ ovm.update({f"{node_id}:out": nv, f"{node_id}:result": nv, node_id: nv})
44
+ return True, nv
45
+ if raw_type == "cat_math":
46
+ cg.discovered_modules.add("math")
47
+ nv = f"math_{safe_id}"
48
+ vx = nctx.resolve_input("x", format_literal(params.get("x", 49.0)))
49
+ lines.append(f" {nv} = math.{params.get('func', 'sqrt')}({vx})")
50
+ ovm.update({f"{node_id}:out": nv, f"{node_id}:result": nv, node_id: nv})
51
+ return True, nv
52
+ if raw_type == "cat_control_flow":
53
+ nv = f"branch_{safe_id}"
54
+ lines.append(f" {nv} = bool({nctx.resolve_input('condition', format_literal(params.get('condition', True)))})")
55
+ ovm.update({f"{node_id}:out": nv, f"{node_id}:then": nv, f"{node_id}:else": f"(not {nv})", f"{node_id}:result": nv, node_id: nv})
56
+ return True, nv
57
+ if raw_type == "cat_functions":
58
+ fn = sanitize_identifier(str(params.get("func_name") or "make_toy"), "make_toy")
59
+ nv = f"res_{fn}_{safe_id}"
60
+ lines.extend([f" def {fn}(args):", " return args", f" {nv} = {fn}({nctx.resolve_input('args', '\"toy_part\"')})"])
61
+ ovm.update({f"{node_id}:out": nv, f"{node_id}:result": nv, node_id: nv})
62
+ return True, nv
63
+ if raw_type == "cat_strings_collections":
64
+ nv = f"text_{safe_id}"
65
+ txt = nctx.resolve_input("text", format_literal(params.get("text", "Hello World")))
66
+ op = params.get("operation", "upper")
67
+ lines.append(f" {nv} = len({txt})" if op == "len" else f" {nv} = str({txt}).{op}()")
68
+ ovm.update({f"{node_id}:out": nv, f"{node_id}:result": nv, node_id: nv})
69
+ return True, nv
70
+ if raw_type == "cat_oop":
71
+ cn = sanitize_identifier(str(params.get("class_name") or "House"), "House").capitalize()
72
+ nv = f"house_{safe_id}"
73
+ lines.extend([f" class {cn}:", " def __init__(self):", " self.doors = 2", " self.windows = 4", f" {nv} = {cn}()"])
74
+ ovm.update({f"{node_id}:out": nv, f"{node_id}:instance": nv, f"{node_id}:result": nv, node_id: nv})
75
+ return True, nv
76
+ if raw_type == "cat_files":
77
+ nv = f"file_data_{safe_id}"
78
+ lines.append(f" {nv} = f'Content of {{{format_literal(params.get('file_path', 'data.txt'))}}}'")
79
+ ovm.update({f"{node_id}:out": nv, f"{node_id}:content": nv, f"{node_id}:result": nv, node_id: nv})
80
+ return True, nv
81
+ if raw_type == "cat_modules":
82
+ mod = str(params.get("module_name") or "math")
83
+ cg.discovered_modules.add(mod)
84
+ nv = f"tool_{safe_id}"
85
+ lines.append(f" {nv} = {mod}")
86
+ ovm.update({f"{node_id}:out": nv, f"{node_id}:module": nv, f"{node_id}:result": nv, node_id: nv})
87
+ return True, nv
88
+ return False, None
89
+
90
+
91
+ def emit_essentials_node(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
92
+ raw_type, node_id, safe_id, params, cg = nctx.raw_type, nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
93
+ lines, ovm = cg.code_body_lines, cg.output_var_map
94
+
95
+ if raw_type in ("workflow.start", "start"):
96
+ nv = f"start_{safe_id}"
97
+ lines.append(f" {nv} = True")
98
+ ovm.update({f"{node_id}:trigger": nv, node_id: nv})
99
+ return True, nv
100
+ if raw_type in ("trigger.schedule", "schedule"):
101
+ cg.discovered_modules.add("datetime")
102
+ nv, ts = f"trigger_{safe_id}", f"ts_{safe_id}"
103
+ lines.extend([f" {ts} = datetime.datetime.now(datetime.timezone.utc).isoformat()", f" {nv} = True"])
104
+ ovm.update({f"{node_id}:trigger": nv, f"{node_id}:timestamp": ts, node_id: nv})
105
+ return True, nv
106
+ if raw_type in ("trigger.webhook", "webhook"):
107
+ nv, pv, hv = f"trigger_{safe_id}", f"payload_{safe_id}", f"headers_{safe_id}"
108
+ lines.extend([f" {pv} = {{}}", f" {hv} = {{}}", f" {nv} = True"])
109
+ ovm.update({f"{node_id}:trigger": nv, f"{node_id}:payload": pv, f"{node_id}:headers": hv, node_id: nv})
110
+ return True, nv
111
+ if raw_type in ("api.http_request", "http_request", "http"):
112
+ cg.discovered_modules.add("requests")
113
+ cg.discovered_requirements.add("requests")
114
+ method = validate_safe_op(str(params.get("method", "GET")).upper(), SAFE_HTTP_METHODS, "GET", nctx, "http_method")
115
+ url_expr = nctx.resolve_input("url", format_literal(params.get("url", "https://api.example.com/data")))
116
+ payload_expr = nctx.resolve_input("payload", "None")
117
+ raw_hdr_p = params.get("headers")
118
+ hdr_fallback = "None"
119
+ if raw_hdr_p is not None and raw_hdr_p != "":
120
+ ok_hp, parsed_hp = parse_safe_literal_or_json(raw_hdr_p) if isinstance(raw_hdr_p, str) else (isinstance(raw_hdr_p, dict), raw_hdr_p)
121
+ hdr_fallback = repr(parsed_hp) if ok_hp and isinstance(parsed_hp, dict) else format_literal(raw_hdr_p)
122
+ headers_expr = nctx.resolve_input("headers", hdr_fallback)
123
+ if headers_expr != "None" and not headers_expr.strip().startswith("{"):
124
+ cg.discovered_modules.add("ast")
125
+ headers_expr = f"(ast.literal_eval({headers_expr}) if isinstance({headers_expr}, str) else {headers_expr})"
126
+ try:
127
+ timeout_val = max(1, min(int(params.get("timeout", 5)), 60))
128
+ except (ValueError, TypeError):
129
+ timeout_val = 5
130
+ nv, sv, hr = f"resp_{safe_id}", f"status_{safe_id}", f"resp_headers_{safe_id}"
131
+ lines.extend([
132
+ f" {nv} = requests.request('{method}', {url_expr}, json={payload_expr}, headers={headers_expr}, timeout={timeout_val})",
133
+ f" {sv} = {nv}.status_code",
134
+ f" {hr} = dict({nv}.headers)",
135
+ ])
136
+ ovm.update({f"{node_id}:response": nv, f"{node_id}:text": f"{nv}.text", f"{node_id}:data": f"{nv}.json()", f"{node_id}:result": f"{nv}.json()", f"{node_id}:out": f"{nv}.json()", f"{node_id}:status_code": sv, f"{node_id}:headers": hr, f"{node_id}:then": "True", node_id: nv})
137
+ return True, nv
138
+ return False, None
139
+
140
+
141
+ def emit_values_math_io_node(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
142
+ raw_type, node_id, safe_id, params, cg = nctx.raw_type, nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
143
+ lines, ovm = cg.code_body_lines, cg.output_var_map
144
+
145
+ if raw_type == "alli.primitive.datetime":
146
+ from lambdas.codegen.emitters_libraries import emit_python_library_preset_node
147
+
148
+ return emit_python_library_preset_node(nctx)
149
+
150
+ if raw_type in ("constant.int", "constant.float", "constant.str", "constant.bool", "core.constant", "input", "primitive.input") or raw_type.startswith(("constant.", "primitive.constant")) or raw_type in ("alli.primitive.constant", "alli.primitive.constant.list"):
151
+ return emit_safe_constant_node(nctx)
152
+
153
+ if raw_type in ("core.variable", "variable") or "variable" in raw_type:
154
+ return emit_safe_variable_node(nctx)
155
+
156
+ if raw_type in ("math.sqrt", "sqrt") or "sqrt" in raw_type:
157
+ cg.discovered_modules.add("math")
158
+ x_expr = nctx.resolve_input("x", nctx.resolve_input("val", nctx.resolve_input("in", params.get("x", 49.0))))
159
+ nv = f"sqrt_{safe_id}"
160
+ rounding = validate_safe_op(params.get("rounding", "none"), SAFE_ROUNDING_MODES, "none", nctx, "rounding")
161
+ if rounding == "round":
162
+ lines.append(f" {nv} = round(math.sqrt({x_expr}), {int(params.get('precision', 2))})")
163
+ elif rounding in ("floor", "ceil"):
164
+ lines.append(f" {nv} = math.{rounding}(math.sqrt({x_expr}))")
165
+ else:
166
+ lines.append(f" {nv} = math.sqrt({x_expr})")
167
+ return True, nv
168
+
169
+ if raw_type in ("core.binary_op", "binary_op") or "binary" in raw_type or raw_type.startswith(("math.add", "math.sub", "math.multiply", "math.divide", "math.pow")):
170
+ a_expr = nctx.resolve_input("a", nctx.resolve_input("x", params.get("a", params.get("x", 0))))
171
+ b_expr = nctx.resolve_input("b", nctx.resolve_input("y", params.get("b", params.get("y", 0))))
172
+ nv = f"res_{safe_id}"
173
+ if "pow" in raw_type or raw_type.startswith("math.pow"):
174
+ cg.discovered_modules.add("math")
175
+ lines.append(f" {nv} = math.pow({a_expr}, {b_expr})")
176
+ else:
177
+ raw_op = "+" if "add" in raw_type else ("-" if "sub" in raw_type else ("*" if "multiply" in raw_type else ("/" if "divide" in raw_type else params.get("operator", "+"))))
178
+ op = validate_safe_op(raw_op, SAFE_BINARY_OPS, "+", nctx, "binary")
179
+ if op in ("/", "//", "%") and b_expr in ("0", "0.0"):
180
+ cg.diagnostics.append({"code": "MATH-E001", "severity": "error", "message": f"Division by zero detected in node '{nctx.label}' ({node_id})", "node_id": node_id})
181
+ lines.append(f" {nv} = {a_expr} {op} {b_expr}")
182
+ return True, nv
183
+
184
+ if raw_type in ("core.compare", "compare") or "compare" in raw_type:
185
+ nv = f"cmp_{safe_id}"
186
+ op = validate_safe_op(params.get("operator", "=="), SAFE_COMPARE_OPS, "==", nctx, "compare")
187
+ lines.append(f" {nv} = ({nctx.resolve_input('a', params.get('a', 0))} {op} {nctx.resolve_input('b', params.get('b', 0))})")
188
+ return True, nv
189
+
190
+ if raw_type in ("core.logical_op", "logical_op") or "logical" in raw_type:
191
+ op = validate_safe_op(str(params.get("operator", "and")).lower(), SAFE_LOGICAL_OPS, "and", nctx, "logical")
192
+ nv = f"logic_{safe_id}"
193
+ a_expr = nctx.resolve_input("a", params.get("a", True))
194
+ lines.append(f" {nv} = (not {a_expr})" if op == "not" else f" {nv} = ({a_expr} {op} {nctx.resolve_input('b', params.get('b', True))})")
195
+ return True, nv
196
+
197
+ if raw_type in ("core.type_cast", "type_cast", "custom.type_cast") or "type_cast" in raw_type:
198
+ return emit_safe_type_cast(nctx)
199
+
200
+ if raw_type in ("io.print", "print") or "terminal" in raw_type or "output" in raw_type or "display" in raw_type:
201
+ val_expr = nctx.resolve_input(["value", "val", "in", "x"], params.get("value", ""))
202
+ prefix = params.get("prefix", "")
203
+ if prefix and not str(prefix).endswith(" "):
204
+ prefix = f"{prefix} "
205
+ if prefix:
206
+ p_str = str(prefix)
207
+ lines.append(
208
+ f" print({format_literal(p_str)} + str({val_expr}))"
209
+ if any(ch in p_str for ch in '{}"\'\\\r\n\0')
210
+ else f' print(f"{p_str}{{{val_expr}}}")'
211
+ )
212
+ else:
213
+ lines.append(f" print({val_expr})")
214
+ ovm.update({f"{node_id}:out": val_expr, f"{node_id}:then": "True", node_id: val_expr})
215
+ return True, val_expr
216
+
217
+ if raw_type in ("io.input", "input"):
218
+ cg.discovered_modules.add("sys")
219
+ default_val = params.get("default_value") if params.get("default_value") is not None else params.get("literal_value")
220
+ prompt = params.get("prompt", "")
221
+ user_var = params.get("var_name") or params.get("alias")
222
+ if user_var: nv = sanitize_identifier(user_var, "user_input")
223
+ elif nctx.clean_lbl and not nctx.clean_lbl[0].isdigit() and len(nctx.clean_lbl) <= 24: nv = nctx.clean_lbl if cg.step_counter == 1 else f"{nctx.clean_lbl}_{cg.step_counter}"
224
+ else: nv = "user_input" if cg.step_counter == 1 else f"user_input_{cg.step_counter}"
225
+ itype = str(params.get("input_type") or "").lower()
226
+ if default_val is not None and not prompt:
227
+ dtype = itype or ("float" if isinstance(default_val, float) else "any")
228
+ lines.append(f" {nv} = {format_literal(default_val, dtype)}")
229
+ else:
230
+ fb_lit = format_literal(default_val if default_val is not None else "", itype if (default_val is not None and itype in ("int", "float", "bool")) else "str")
231
+ prompt_lit = format_literal(str(prompt))
232
+ lines.extend([f" try:", f" {nv} = input({prompt_lit}) if sys.stdin.isatty() else {fb_lit}", f" except (EOFError, OSError):", f" {nv} = {fb_lit}"])
233
+ if itype in ("int", "float"):
234
+ lines.append(f" {nv} = {itype}({nv})")
235
+ ovm.update({f"{node_id}:value": nv, f"{node_id}:out": nv, f"{node_id}:output": nv, f"{node_id}:then": "True", node_id: nv})
236
+ return True, nv
237
+
238
+ if raw_type in ("math.abs", "math.round", "math.floor", "math.ceil", "math.min", "math.max"):
239
+ fn, nv = raw_type.split(".", 1)[1], f"{raw_type.split('.', 1)[1]}_{safe_id}"
240
+ x_e = nctx.resolve_input("x", params.get("x", 0.0))
241
+ if fn in ("floor", "ceil"): cg.discovered_modules.add("math"); lines.append(f" {nv} = math.{fn}({x_e})")
242
+ elif fn == "abs": lines.append(f" {nv} = abs({x_e})")
243
+ elif fn == "round": lines.append(f" {nv} = round({x_e}, {int(params.get('precision', 0))})")
244
+ else: lines.append(f" {nv} = {fn}({nctx.resolve_input('a', params.get('a', 0.0))}, {nctx.resolve_input('b', params.get('b', 0.0))})")
245
+ return True, nv
246
+ return False, None
lambdas/codegen/emitters_files_data.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Codegen Subpackage: File I/O, JSON, CSV & Collection Data Structure Emitters.
3
+ """
4
+
5
+ from typing import Any, Optional, Tuple
6
+ from language_core.codegen_contracts import format_literal
7
+ from language_core.nodes.loader import parse_safe_literal_or_json
8
+ from lambdas.codegen.context import NodeCodegenCtx
9
+
10
+
11
+ def _eval_literal_default(raw: Any) -> Any:
12
+ if isinstance(raw, str):
13
+ ok, parsed = parse_safe_literal_or_json(raw)
14
+ return parsed if ok else raw
15
+ return raw
16
+
17
+
18
+ def emit_file_and_structured_node(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
19
+ raw_type, node_id, safe_id, params, cg = nctx.raw_type, nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
20
+ lines, ovm = cg.code_body_lines, cg.output_var_map
21
+
22
+ if raw_type in ("file.read", "io.file_read") or (raw_type == "sys.file" and str(params.get("operation", "read")).lower() != "write"):
23
+ cg.discovered_modules.add("os")
24
+ path_expr = nctx.resolve_input(["file_path", "path"], format_literal(params.get("path") or params.get("file_path") or "data.txt"))
25
+ from lambdas.codegen.wave1_core_emitters import sanitize_encoding_name
26
+ enc, mode_val = sanitize_encoding_name(params.get("encoding")), params.get("mode", "text")
27
+ nv, bv, sv = f"content_{safe_id}", f"bytes_{safe_id}", f"success_{safe_id}"
28
+ if mode_val == "binary":
29
+ lines.extend([f" with open({path_expr}, 'rb') as _f_{safe_id}:", f" {nv} = _f_{safe_id}.read()"])
30
+ else:
31
+ lines.append(f" with open({path_expr}, 'r', encoding='{enc}') as _f_{safe_id}:")
32
+ if mode_val == "lines":
33
+ lines.append(f" {nv} = _f_{safe_id}.readlines()")
34
+ elif mode_val == "json":
35
+ cg.discovered_modules.add("json")
36
+ lines.append(f" {nv} = json.load(_f_{safe_id})")
37
+ else:
38
+ lines.append(f" {nv} = _f_{safe_id}.read()")
39
+ lines.extend([f" {bv} = len({nv})", f" {sv} = True"])
40
+ ovm.update({
41
+ f"{node_id}:content": nv, f"{node_id}:byte_count": bv, f"{node_id}:bytes": bv,
42
+ f"{node_id}:lines": f"({nv}.splitlines() if isinstance({nv}, str) else {nv})",
43
+ f"{node_id}:success": sv, f"{node_id}:then": "True", f"{node_id}:out": nv, node_id: nv,
44
+ })
45
+ return True, nv
46
+
47
+ if raw_type in ("file.write", "io.file_write", "sys.file"):
48
+ cg.discovered_modules.add("os")
49
+ raw_path_param = params.get("file_path") or params.get("path", "output.txt")
50
+ if isinstance(raw_path_param, str) and (".." in raw_path_param.replace("\\", "/").split("/") or raw_path_param.startswith(("/", "\\")) or (len(raw_path_param) > 1 and raw_path_param[1] == ":")):
51
+ cg.diagnostics.append({"code": "SEC-001", "severity": "error", "message": f"Unsafe path '{raw_path_param}' rejected by sandbox boundary.", "node_id": node_id})
52
+ path_expr = nctx.resolve_input(["file_path", "path"], format_literal(raw_path_param))
53
+ raw_c_param = next((c for c in (params.get("content"), params.get("data"), params.get("text")) if c is not None), "")
54
+ content_expr = nctx.resolve_input(["content", "data", "text"], format_literal(raw_c_param))
55
+ mode_opt = params.get("mode", "write")
56
+ wmode = "a" if mode_opt == "append" else ("x" if mode_opt == "exclusive" else "w")
57
+ from lambdas.codegen.wave1_core_emitters import sanitize_encoding_name
58
+ enc = sanitize_encoding_name(params.get("encoding"))
59
+ bv, pr, sv = f"written_{safe_id}", f"path_{safe_id}", f"success_{safe_id}"
60
+ if bool(params.get("create_dirs", True)):
61
+ lines.extend([f" if os.path.dirname({path_expr}):", f" os.makedirs(os.path.dirname({path_expr}), exist_ok=True)"])
62
+ lines.extend([
63
+ f" with open({path_expr}, '{wmode}', encoding='{enc}') as _f_{safe_id}:",
64
+ f" _f_{safe_id}.write(str({content_expr}))",
65
+ f" {bv} = len(str({content_expr}).encode('{enc}'))",
66
+ f" {pr} = {path_expr}",
67
+ f" {sv} = True",
68
+ ])
69
+ ovm.update({f"{node_id}:bytes_written": bv, f"{node_id}:file_path": pr, f"{node_id}:success": sv, f"{node_id}:then": "True", node_id: bv})
70
+ return True, bv
71
+
72
+ if raw_type in ("json.parse", "json_parse", "json.loads"):
73
+ cg.discovered_modules.add("json")
74
+ text_expr = nctx.resolve_input(["text", "json_str", "raw_json"], format_literal(params.get("text", "{}")))
75
+ strict = bool(params.get("strict", True))
76
+ dv, vv, ev = f"json_data_{safe_id}", f"is_valid_{safe_id}", f"err_{safe_id}"
77
+ safe_txt = f"({text_expr}.text if hasattr({text_expr}, 'text') else (json.dumps({text_expr}) if isinstance({text_expr}, (dict, list)) else str({text_expr})))"
78
+ if bool(params.get("fallback_to_none", False)):
79
+ lines.extend([
80
+ " try:", f" {dv} = json.loads({safe_txt}, strict={strict})",
81
+ f" {vv} = True", f" {ev} = None",
82
+ f" except Exception as _e_{safe_id}:", f" {dv} = None", f" {vv} = False", f" {ev} = str(_e_{safe_id})",
83
+ ])
84
+ else:
85
+ lines.extend([f" {dv} = json.loads({safe_txt}, strict={strict})", f" {vv} = True", f" {ev} = None"])
86
+ ovm.update({f"{node_id}:data": dv, f"{node_id}:is_valid": vv, f"{node_id}:error": ev, f"{node_id}:then": "True", f"{node_id}:out": dv, node_id: dv})
87
+ return True, dv
88
+
89
+ if raw_type in ("json.stringify", "json_stringify", "json.dumps"):
90
+ cg.discovered_modules.add("json")
91
+ data_expr = nctx.resolve_input(["data", "obj", "payload"], format_literal(params.get("data", {})))
92
+ iv = params.get("indent", 2)
93
+ indent_arg = "None" if str(iv) in ("0", "none", "None") else str(iv)
94
+ tv, bv, sv = f"json_str_{safe_id}", f"json_bytes_{safe_id}", f"success_{safe_id}"
95
+ lines.extend([
96
+ f" {tv} = json.dumps({data_expr}, indent={indent_arg}, sort_keys={bool(params.get('sort_keys', False))}, ensure_ascii={bool(params.get('ensure_ascii', False))}, default=str)",
97
+ f" {bv} = len({tv}.encode('utf-8'))",
98
+ f" {sv} = True",
99
+ ])
100
+ ovm.update({f"{node_id}:text": tv, f"{node_id}:byte_size": bv, f"{node_id}:success": sv, f"{node_id}:then": "True", f"{node_id}:out": tv, node_id: tv})
101
+ return True, tv
102
+
103
+ if raw_type in ("csv.parse", "csv_parse", "csv.read"):
104
+ cg.discovered_modules.update(("csv", "io"))
105
+ text_expr = nctx.resolve_input(["text", "csv_data", "raw_csv"], format_literal(params.get("text", "")))
106
+ delim = "\t" if params.get("delimiter", ",") == "\\t" else params.get("delimiter", ",")
107
+ has_hdr, trim_ws = bool(params.get("has_header", True)), bool(params.get("trim_whitespace", True))
108
+ rv, hv, cv, sv, rdr, fv = f"csv_rows_{safe_id}", f"csv_headers_{safe_id}", f"csv_count_{safe_id}", f"success_{safe_id}", f"_reader_{safe_id}", f"_f_{safe_id}"
109
+ lines.append(f" {fv} = io.StringIO(str({text_expr}))")
110
+ if has_hdr:
111
+ lines.extend([f" {rdr} = csv.DictReader({fv}, delimiter={format_literal(delim)})", f" {hv} = list({rdr}.fieldnames or [])"])
112
+ lines.append(f" {rv} = [{{(k.strip() if isinstance(k, str) else k): (v.strip() if isinstance(v, str) else v) for k, v in r.items()}} for r in {rdr} if any(str(x).strip() for x in r.values())]" if trim_ws else f" {rv} = [dict(r) for r in {rdr} if any(str(x).strip() for x in r.values())]")
113
+ else:
114
+ lines.extend([f" {hv} = []", f" {rdr} = csv.reader({fv}, delimiter={format_literal(delim)})"])
115
+ lines.append(f" {rv} = [[(v.strip() if isinstance(v, str) else v) for v in r] for r in {rdr} if r and any(str(x).strip() for x in r)]" if trim_ws else f" {rv} = [list(r) for r in {rdr} if r and any(str(x).strip() for x in r)]")
116
+ lines.extend([f" {cv} = len({rv})", f" {sv} = True"])
117
+ ovm.update({f"{node_id}:rows": rv, f"{node_id}:headers": hv, f"{node_id}:row_count": cv, f"{node_id}:success": sv, f"{node_id}:then": "True", f"{node_id}:out": rv, node_id: rv})
118
+ return True, rv
119
+
120
+ if raw_type in ("csv.stringify", "csv_stringify", "csv.write"):
121
+ cg.discovered_modules.update(("csv", "io"))
122
+ rows_default = _eval_literal_default(params.get("rows", []))
123
+ rows_expr = nctx.resolve_input(["rows", "data", "table"], repr(rows_default) if isinstance(rows_default, (list, tuple)) else format_literal(rows_default))
124
+ hdr_default = _eval_literal_default(params.get("headers")) if params.get("headers") is not None else None
125
+ hdr_expr = nctx.resolve_input(["headers", "custom_headers"], repr(hdr_default) if isinstance(hdr_default, list) else "None")
126
+ delim = "\t" if params.get("delimiter", ",") == "\\t" else params.get("delimiter", ",")
127
+ inc_hdr, term = bool(params.get("include_header", True)), params.get("line_terminator", "\n")
128
+ tv, cv, bv, sv, fv, wv = f"csv_text_{safe_id}", f"csv_count_{safe_id}", f"csv_bytes_{safe_id}", f"success_{safe_id}", f"_f_{safe_id}", f"_w_{safe_id}"
129
+ if not rows_expr.strip().startswith(("[", "(")):
130
+ cg.discovered_modules.add("ast")
131
+ raw_rows_expr = f"(ast.literal_eval({rows_expr}) if isinstance({rows_expr}, str) else {rows_expr})"
132
+ else:
133
+ raw_rows_expr = rows_expr
134
+ lines.extend([
135
+ f" {fv} = io.StringIO()",
136
+ f" _rows_in_{safe_id} = {raw_rows_expr}",
137
+ f" _rows_{safe_id} = _rows_in_{safe_id} if isinstance(_rows_in_{safe_id}, (list, tuple)) else [_rows_in_{safe_id}]",
138
+ f" _hdr_{safe_id} = {hdr_expr}",
139
+ f" if _rows_{safe_id} and isinstance(_rows_{safe_id}[0], dict):",
140
+ f" _fields_{safe_id} = list(_hdr_{safe_id}) if _hdr_{safe_id} and isinstance(_hdr_{safe_id}, list) else list(_rows_{safe_id}[0].keys())",
141
+ f" {wv} = csv.DictWriter({fv}, fieldnames=_fields_{safe_id}, delimiter={format_literal(delim)}, lineterminator={format_literal(term)}, extrasaction='ignore')",
142
+ ])
143
+ if inc_hdr:
144
+ lines.append(f" {wv}.writeheader()")
145
+ lines.extend([
146
+ f" for _r in _rows_{safe_id}:",
147
+ " if isinstance(_r, dict):",
148
+ f" {wv}.writerow(_r)",
149
+ " else:",
150
+ f" {wv} = csv.writer({fv}, delimiter={format_literal(delim)}, lineterminator={format_literal(term)})",
151
+ ])
152
+ if inc_hdr:
153
+ lines.extend([f" if _hdr_{safe_id} and isinstance(_hdr_{safe_id}, list):", f" {wv}.writerow(_hdr_{safe_id})"])
154
+ lines.extend([
155
+ f" for _r in _rows_{safe_id}:",
156
+ " if isinstance(_r, (list, tuple)):",
157
+ f" {wv}.writerow(_r)",
158
+ " else:",
159
+ f" {wv}.writerow([_r])",
160
+ f" {tv} = {fv}.getvalue()",
161
+ f" {cv} = len(_rows_{safe_id})",
162
+ f" {bv} = len({tv}.encode('utf-8'))",
163
+ f" {sv} = True",
164
+ ])
165
+ ovm.update({f"{node_id}:text": tv, f"{node_id}:row_count": cv, f"{node_id}:byte_size": bv, f"{node_id}:success": sv, f"{node_id}:then": "True", f"{node_id}:out": tv, node_id: tv})
166
+ return True, tv
167
+
168
+ return False, None
169
+
170
+
171
+ def emit_collection_node(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
172
+ raw_type, node_id, safe_id, params, cg = nctx.raw_type, nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
173
+ lines, ovm = cg.code_body_lines, cg.output_var_map
174
+
175
+ if raw_type in ("data.list", "list"):
176
+ items_expr = nctx.resolve_input("items", _eval_literal_default(params.get("initial_items", "[]")))
177
+ nv = f"list_{safe_id}"
178
+ lines.append(f" {nv} = {items_expr}")
179
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:list_out": nv, f"{node_id}:out": nv, f"{node_id}:length": f"len({nv})", f"{node_id}:count": f"len({nv})", node_id: nv})
180
+ return True, nv
181
+
182
+ if raw_type in ("data.dict", "dict"):
183
+ raw_init = _eval_literal_default(params.get("initial_pairs") or params.get("initial_dict", "{}"))
184
+ connected = nctx.resolve_input("dict_in", "None")
185
+ dict_expr = connected if connected != "None" else (repr(raw_init) if isinstance(raw_init, dict) else format_literal(raw_init))
186
+ nv = f"dict_{safe_id}"
187
+ lines.append(f" {nv} = {dict_expr}")
188
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:dict_out": nv, f"{node_id}:out": nv, node_id: nv})
189
+ return True, nv
190
+
191
+ if raw_type in ("data.tuple", "tuple"):
192
+ has_items = any(k in params or k in nctx.node_inputs for k in ("item_1", "item_2", "item_3")) or any(
193
+ str(inc.get("target_handle") or inc.get("targetHandle") or "").lower() in ("item_1", "item_2", "item_3") for inc in nctx.incoming
194
+ )
195
+ nv = f"tuple_{safe_id}"
196
+ if has_items and "items" not in params and "items" not in nctx.node_inputs:
197
+ i1 = nctx.resolve_input("item_1", format_literal(params.get("item_1", 1)))
198
+ i2 = nctx.resolve_input("item_2", format_literal(params.get("item_2", 2)))
199
+ i3 = nctx.resolve_input("item_3", format_literal(params.get("item_3", 3)))
200
+ lines.append(f" {nv} = ({i1}, {i2}, {i3})")
201
+ else:
202
+ parsed_def = _eval_literal_default(params.get("items") or params.get("initial_items", "()"))
203
+ tuple_expr = nctx.resolve_input("items", repr(parsed_def) if isinstance(parsed_def, (list, tuple, set)) else format_literal(parsed_def))
204
+ if not tuple_expr.strip().startswith(("[", "(", "{")):
205
+ cg.discovered_modules.add("ast")
206
+ tuple_expr = f"(ast.literal_eval({tuple_expr}) if isinstance({tuple_expr}, str) else {tuple_expr})"
207
+ lines.append(f" {nv} = tuple({tuple_expr})")
208
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:tuple_out": nv, f"{node_id}:out": nv, f"{node_id}:count": f"len({nv})", node_id: nv})
209
+ return True, nv
210
+
211
+ if raw_type in ("data.set", "set"):
212
+ raw_set_def = _eval_literal_default(params.get("initial_items") or params.get("initial_elements", "[]"))
213
+ elements_expr = nctx.resolve_input(["set_a", "items", "initial_elements"], repr(raw_set_def) if isinstance(raw_set_def, (list, tuple, set)) else format_literal(raw_set_def))
214
+ if not elements_expr.strip().startswith(("[", "(", "{")):
215
+ cg.discovered_modules.add("ast")
216
+ elements_expr = f"(ast.literal_eval({elements_expr}) if isinstance({elements_expr}, str) else {elements_expr})"
217
+ nv = f"set_{safe_id}"
218
+ lines.append(f" {nv} = set({elements_expr})")
219
+ add_expr = nctx.resolve_input(["add_item", "element"], "None")
220
+ if add_expr != "None":
221
+ lines.append(f" {nv}.add({add_expr})")
222
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:set_out": nv, f"{node_id}:out": nv, f"{node_id}:count": f"len({nv})", f"{node_id}:cardinality": f"len({nv})", node_id: nv})
223
+ return True, nv
224
+
225
+ return False, None
lambdas/codegen/emitters_libraries.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Codegen Subpackage: Built-in Python Libraries Preset Emitters.
3
+ Keeps runtime/codegen parity for Python Libraries palette cards (`alli.sys.env`,
4
+ `requests.get`, `statistics.mean`, `pandas.dataframe`, `numpy.array`).
5
+ """
6
+
7
+ from typing import Optional, Tuple
8
+
9
+ from lambdas.codegen.context import NodeCodegenCtx
10
+ from language_core.codegen_contracts import format_literal, sanitize_identifier
11
+
12
+
13
+ def emit_python_library_preset_node(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
14
+ """Emit PEP 8 Python code for built-in Python Libraries preset cards."""
15
+ raw_type, node_id, safe_id, params, cg = nctx.raw_type, nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
16
+ lines, ovm = cg.code_body_lines, cg.output_var_map
17
+
18
+ if raw_type == "alli.primitive.datetime":
19
+ cg.discovered_modules.add("datetime")
20
+ op = str(params.get("operation") or "strftime").lower()
21
+ fmt = str(params.get("format_pattern") or "%Y-%m-%d %H:%M:%S")
22
+ nv = f"dt_{safe_id}"
23
+ if op in ("now", "utcnow", "isoformat", "today"):
24
+ lines.append(f" {nv} = datetime.datetime.now(datetime.timezone.utc).{'date().' if op == 'today' else ''}isoformat()")
25
+ elif op == "timedelta":
26
+ days = int(params.get("days") or 1)
27
+ lines.append(f" {nv} = (datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days={days})).isoformat()")
28
+ else:
29
+ lines.append(f" {nv} = datetime.datetime.now(datetime.timezone.utc).strftime({format_literal(fmt)})")
30
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:iso": nv, f"{node_id}:val": nv, f"{node_id}:out": nv, node_id: nv})
31
+ return True, nv
32
+
33
+ if raw_type in ("alli.sys.env", "sys.env_get", "sys.env", "sys.env_set"):
34
+ from lambdas.codegen.wave1_core_emitters import emit_safe_sys_env
35
+ return emit_safe_sys_env(nctx)
36
+
37
+ if raw_type == "requests.get":
38
+ cg.discovered_modules.add("requests")
39
+ cg.discovered_requirements.add("requests")
40
+ url_expr = nctx.resolve_input("url", format_literal(str(params.get("url") or "https://api.example.com/data")))
41
+ nv = f"res_{safe_id}"
42
+ lines.append(f" {nv} = requests.get({url_expr}, timeout=5).json()")
43
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:response": nv, f"{node_id}:val": nv, f"{node_id}:out": nv, node_id: nv})
44
+ return True, nv
45
+
46
+ if raw_type == "statistics.mean":
47
+ cg.discovered_modules.add("statistics")
48
+ func = str(params.get("func") or "mean").strip().lower()
49
+ data_expr = nctx.resolve_input("data", format_literal(params.get("data") or [10, 20, 30]))
50
+ nv = f"stat_{safe_id}"
51
+ lines.append(f" {nv} = statistics.{func}({data_expr})")
52
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:val": nv, f"{node_id}:out": nv, node_id: nv})
53
+ return True, nv
54
+
55
+ if raw_type == "pandas.dataframe":
56
+ cg.discovered_modules.add("pandas")
57
+ cg.discovered_requirements.add("pandas")
58
+ nv = f"df_{safe_id}"
59
+ func = str(params.get("func") or "read_csv")
60
+ fp = format_literal(str(params.get("filepath") or "data.csv"))
61
+ lines.append(
62
+ f" {nv} = pandas.read_csv({fp})"
63
+ if func == "read_csv" and not nctx.incoming
64
+ else f" {nv} = pandas.DataFrame({nctx.resolve_input('data', '[]')})"
65
+ )
66
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:rows": nv, f"{node_id}:val": nv, f"{node_id}:out": nv, node_id: nv})
67
+ return True, nv
68
+
69
+ if raw_type == "numpy.array":
70
+ cg.discovered_modules.add("numpy")
71
+ cg.discovered_requirements.add("numpy")
72
+ nv = f"arr_{safe_id}"
73
+ dtype = format_literal(str(params.get("dtype") or "float64"))
74
+ if nctx.incoming or params.get("values") is not None:
75
+ vals_expr = nctx.resolve_input("data", format_literal(params.get("values") or [0.0, 0.0]))
76
+ lines.append(f" {nv} = numpy.array({vals_expr}, dtype={dtype})")
77
+ else:
78
+ lines.append(f" {nv} = numpy.zeros((2, 2), dtype={dtype})")
79
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:val": nv, f"{node_id}:out": nv, node_id: nv})
80
+ return True, nv
81
+
82
+ return False, None
lambdas/codegen/scope_analysis.py ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Graph Scope Analysis & Diamond-Merge Helpers.
3
+ Shared by scoped Python codegen and scoped workflow runtime execution.
4
+ """
5
+
6
+ import re
7
+ from collections import defaultdict, deque
8
+ from typing import Any, Callable, Dict, Iterable, List, Optional, Set, Tuple
9
+
10
+ from language_core.codegen_contracts import sanitize_identifier
11
+
12
+ TRUE_HANDLES = ("true", "then", "true_flow", "then_val")
13
+ FALSE_HANDLES = ("false", "else", "false_flow", "else_val")
14
+ BODY_HANDLES = ("body",)
15
+ POST_LOOP_HANDLES = ("done", "completed")
16
+ SAFE_SOURCE_TYPES = {"core.constant", "core.variable", "io.input", "oop.class_def", "data.list", "data.dict"}
17
+ _ASSIGN_RE = re.compile(r"^(\s*)([A-Za-z_][A-Za-z0-9_]*)\s*=(?!=)")
18
+
19
+
20
+ def norm_node_type(node: Dict[str, Any]) -> str:
21
+ d = node.get("data") or {}
22
+ return str(node.get("node_type") or node.get("type") or d.get("node_type") or d.get("nodeType") or "").strip().lower()
23
+
24
+
25
+ def reachable_from(roots: Iterable[str], adj: Dict[str, List[str]]) -> Set[str]:
26
+ seen: Set[str] = set()
27
+ q = deque(roots)
28
+ while q:
29
+ cur = q.popleft()
30
+ if cur in seen:
31
+ continue
32
+ seen.add(cur)
33
+ for nxt in adj.get(cur, ()):
34
+ if nxt not in seen:
35
+ q.append(nxt)
36
+ return seen
37
+
38
+
39
+ def detect_scopes(node_map: Dict[str, Dict[str, Any]], edges: List[Dict[str, Any]]) -> Dict[str, Any]:
40
+ out_edges: Dict[str, List[Tuple[str, str, str]]] = defaultdict(list)
41
+ in_edges: Dict[str, List[Tuple[str, str, str]]] = defaultdict(list)
42
+ adj: Dict[str, List[str]] = defaultdict(list)
43
+ for e in edges:
44
+ s = str(e.get("source") or e.get("from_node") or "")
45
+ t = str(e.get("target") or e.get("to_node") or "")
46
+ sh = str(e.get("source_handle") or e.get("sourceHandle") or e.get("from_port") or "").lower()
47
+ th = str(e.get("target_handle") or e.get("targetHandle") or e.get("to_port") or "").lower()
48
+ if s in node_map and t in node_map:
49
+ out_edges[s].append((sh, t, th))
50
+ in_edges[t].append((s, sh, th))
51
+ adj[s].append(t)
52
+
53
+ if_scopes, loop_scopes, try_scopes = {}, {}, {}
54
+ for nid, node in node_map.items():
55
+ rt = norm_node_type(node)
56
+ if rt in ("control.if_else", "if_else"):
57
+ tr = [t for sh, t, _ in out_edges[nid] if sh in TRUE_HANDLES]
58
+ fr = [t for sh, t, _ in out_edges[nid] if sh in FALSE_HANDLES]
59
+ if tr or fr:
60
+ r_t, r_f = reachable_from(tr, adj), reachable_from(fr, adj)
61
+ merge = r_t & r_f
62
+ if (r_t - merge) or (r_f - merge):
63
+ if_scopes[nid] = {"only_true": r_t - merge, "only_false": r_f - merge, "merge": merge}
64
+ elif rt in ("control.for_loop", "for_loop", "control.while_loop", "while_loop"):
65
+ br = [t for sh, t, _ in out_edges[nid] if sh in BODY_HANDLES]
66
+ if br:
67
+ dr = [t for sh, t, _ in out_edges[nid] if sh in POST_LOOP_HANDLES]
68
+ post = reachable_from(dr, adj)
69
+ body = reachable_from(br, adj) - post - {nid}
70
+ if body:
71
+ loop_scopes[nid] = {"kind": "for" if "for" in rt else "while", "body": body}
72
+ elif rt in ("control.try_except", "try_except"):
73
+ inc = in_edges[nid]
74
+ roots = {s for s, _, th in inc if th in ("try_val", "in", "value") or (not th and len(inc) == 1)}
75
+ tset: Set[str] = set()
76
+ q = deque(roots)
77
+ while q:
78
+ u = q.popleft()
79
+ ut = norm_node_type(node_map[u]) if u in node_map else ""
80
+ if not ut or u in tset or u == nid or ut in SAFE_SOURCE_TYPES or ut.startswith(("constant.", "trigger.")):
81
+ continue
82
+ if all(tgt in tset or tgt == nid for _, tgt, _ in out_edges[u]):
83
+ tset.add(u)
84
+ for ps, _, _ in in_edges[u]:
85
+ q.append(ps)
86
+ if tset:
87
+ try_scopes[nid] = {"try_nodes": tset}
88
+ return {"if": if_scopes, "loop": loop_scopes, "try": try_scopes, "in_edges": in_edges}
89
+
90
+
91
+ def scope_children(nid: str, scopes: Dict[str, Any]) -> Set[str]:
92
+ if nid in scopes["if"]:
93
+ return scopes["if"][nid]["only_true"] | scopes["if"][nid]["only_false"]
94
+ if nid in scopes["loop"]:
95
+ return scopes["loop"][nid]["body"]
96
+ if nid in scopes["try"]:
97
+ return scopes["try"][nid]["try_nodes"]
98
+ return set()
99
+
100
+
101
+ def preinit_block_vars(cg: Any, header_idx: int, block_indent: str, skip_vars: Set[str]) -> None:
102
+ new_vars: List[str] = []
103
+ seen = set(skip_vars)
104
+ for line in cg.code_body_lines[:header_idx]:
105
+ m = _ASSIGN_RE.match(line)
106
+ if m and m.group(1) == block_indent:
107
+ seen.add(m.group(2))
108
+ child_indent = block_indent + " "
109
+ for line in cg.code_body_lines[header_idx:]:
110
+ m = _ASSIGN_RE.match(line)
111
+ if m and m.group(1).startswith(child_indent) and m.group(2) not in seen and m.group(2) != "return":
112
+ seen.add(m.group(2))
113
+ new_vars.append(m.group(2))
114
+ if not new_vars:
115
+ return
116
+ init_lines = [f"{block_indent}{v} = None" for v in new_vars]
117
+ cg.code_body_lines[header_idx:header_idx] = init_lines
118
+ shift = len(init_lines)
119
+ for sm in cg.source_map:
120
+ if sm["line_start"] > header_idx:
121
+ sm["line_start"] += shift
122
+ sm["line_end"] += shift
123
+
124
+
125
+ def build_merge_override(nid: str, cg: Any, inc_edges: List[Dict[str, Any]], completed_ifs: List[Tuple[str, str, Set[str], Set[str]]], indent: str, node: Optional[Dict[str, Any]] = None) -> Optional[List[Dict[str, Any]]]:
126
+ ovm, grouped = cg.output_var_map, defaultdict(list)
127
+ for e in inc_edges:
128
+ th = str(e.get("target_handle") or e.get("targetHandle") or e.get("to_port") or "_default").lower()
129
+ grouped[th].append(e)
130
+ rewritten: Optional[List[Dict[str, Any]]] = None
131
+ safe_m = sanitize_identifier(nid.replace("-", "_"), "m")
132
+ np = node_params(node) if node else {}
133
+
134
+ def _expr(ed: Dict[str, Any]) -> str:
135
+ sid = str(ed.get("source") or ed.get("from_node"))
136
+ sh = str(ed.get("source_handle") or ed.get("sourceHandle") or ed.get("from_port") or "")
137
+ return ovm.get(f"{sid}:{sh}") or ovm.get(f"{sid}:result") or ovm.get(f"{sid}:out") or ovm.get(sid) or "None"
138
+
139
+ for if_id, branch_var, only_t, only_f in completed_ifs:
140
+ def _is_t(e: Dict[str, Any]) -> bool:
141
+ s, sh = str(e.get("source") or e.get("from_node")), str(e.get("source_handle") or e.get("sourceHandle") or "").lower()
142
+ return s in only_t or (s == if_id and sh in TRUE_HANDLES)
143
+ def _is_f(e: Dict[str, Any]) -> bool:
144
+ s, sh = str(e.get("source") or e.get("from_node")), str(e.get("source_handle") or e.get("sourceHandle") or "").lower()
145
+ return s in only_f or (s == if_id and sh in FALSE_HANDLES)
146
+
147
+ for th, elist in grouped.items():
148
+ t_e = next((e for e in elist if _is_t(e)), None)
149
+ f_e = next((e for e in elist if _is_f(e)), None)
150
+ if not (t_e or f_e):
151
+ continue
152
+ if t_e and f_e:
153
+ cond_rhs = f"({_expr(t_e)} if {branch_var} else {_expr(f_e)})"
154
+ else:
155
+ fb = repr(np[th]) if th in np and np[th] is not None else ("0" if norm_node_type(node or {}) in ("core.binary_op", "primitive.binary_op") else "None")
156
+ cond_rhs = f"({_expr(t_e)} if {branch_var} else {fb})" if t_e else f"({fb} if {branch_var} else {_expr(f_e)})"
157
+ mvar = f"_merge_{safe_m}_{sanitize_identifier(th, 'v')}"
158
+ cg.code_body_lines.append(f"{indent}{mvar} = {cond_rhs}")
159
+ ovm[f"_phi_{nid}:{th}"] = mvar
160
+ if rewritten is None:
161
+ rewritten = list(inc_edges)
162
+ rewritten = [e for e in rewritten if e not in ([t_e] if t_e else []) and e not in ([f_e] if f_e else [])]
163
+ rewritten.append({"source": f"_phi_{nid}", "source_handle": th, "target": nid, "target_handle": "" if th == "_default" else th})
164
+ return rewritten
165
+
166
+
167
+ CMP_OPS: Dict[str, Callable[[Any, Any], bool]] = {
168
+ "<": lambda a, b: a < b, "<=": lambda a, b: a <= b,
169
+ ">": lambda a, b: a > b, ">=": lambda a, b: a >= b,
170
+ "==": lambda a, b: a == b, "!=": lambda a, b: a != b,
171
+ }
172
+
173
+
174
+ def node_params(node: Dict[str, Any]) -> Dict[str, Any]:
175
+ d = node.get("data") or {}
176
+ return {**(d if isinstance(d, dict) else {}), **(node.get("parameters") or {}), **(d.get("parameters") or {})}
177
+
178
+
179
+ def match_catch_type(catch_type: str, err_msg: str) -> bool:
180
+ ct, low = (catch_type or "Exception").strip(), err_msg.lower()
181
+ if ct in ("Exception", "BaseException", "") or ct in err_msg or ct.lower() in low:
182
+ return True
183
+ if ct == "ZeroDivisionError" and any(k in low for k in ("zero", "math-e001", "division")):
184
+ return True
185
+ return ct == "ValueError" and any(k in low for k in ("domain", "math-e002", "value"))
186
+
187
+
188
+ def reset_sub_nodes(nodes: Set[str], store: Dict[str, Dict[str, Any]], skipped: Set[str], failed: Set[str]) -> None:
189
+ for n in nodes:
190
+ store.pop(n, None)
191
+ skipped.discard(n)
192
+ failed.discard(n)
193
+
194
+
195
+ def mark_skipped(nodes: Set[str], node_map: Dict[str, Any], skipped: Set[str], results: Dict[str, Dict[str, Any]], reason: str = "INFO-BRANCH-SKIPPED", skip_msg: Optional[str] = None) -> None:
196
+ msg = skip_msg or f"Skipped ({reason})."
197
+ for nid in nodes:
198
+ skipped.add(nid)
199
+ entry = results.setdefault(nid, {"node_id": nid, "node_type": norm_node_type(node_map.get(nid, {})), "outputs": {}, "duration_ms": 0.0})
200
+ entry["status"] = "skipped"
201
+ entry["exec_state"] = "skipped"
202
+ if not entry.get("skip_reason"):
203
+ entry["skip_reason"] = msg
204
+ if not entry.get("error_semantics"):
205
+ entry["error_semantics"] = {"code": reason, "severity": "info", "message": msg}
lambdas/codegen/scoped_blocks.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Scoped Control-Flow & Try-Except Codegen Emitter.
3
+ Synthesizes properly indented Python blocks for scoped if/else, loops, loop_control, and try/except subgraphs.
4
+ """
5
+
6
+ from collections import deque
7
+ from typing import Any, Callable, Dict, List, Optional, Set, Tuple
8
+
9
+ from lambdas.codegen.scope_analysis import (
10
+ BODY_HANDLES,
11
+ FALSE_HANDLES,
12
+ TRUE_HANDLES,
13
+ build_merge_override,
14
+ detect_scopes,
15
+ norm_node_type,
16
+ preinit_block_vars,
17
+ scope_children,
18
+ )
19
+ from language_core.codegen_contracts import format_literal
20
+ from language_core.nodes.loader import parse_safe_literal_or_json
21
+
22
+ SAFE_EXCEPTS = {"Exception", "ValueError", "ZeroDivisionError", "TypeError", "KeyError", "IndexError", "RuntimeError", "OSError"}
23
+ TRY_SUPPRESS_CODES = {"MATH-E001", "MATH-E002", "ERR-ZERO-DIVISION", "ERR-MATH-DOMAIN"}
24
+
25
+
26
+ def emit_workflow_with_scopes(
27
+ cg: Any,
28
+ ordered_ids: List[str],
29
+ node_map: Dict[str, Any],
30
+ filtered_edges: List[Dict[str, Any]],
31
+ incoming_edges_map: Dict[str, List[Dict[str, Any]]],
32
+ emit_node_fn: Callable[..., Any],
33
+ register_fn: Callable[..., Any],
34
+ ) -> bool:
35
+ scopes = detect_scopes(node_map, filtered_edges)
36
+ if not (scopes["if"] or scopes["loop"] or scopes["try"]):
37
+ return False
38
+ emitted: Set[str] = set()
39
+ completed_ifs: List[Tuple[str, str, Set[str], Set[str]]] = []
40
+
41
+ def _emit_region(region_ids: Set[str], depth: int, loop_ctx: Optional[Dict[str, Any]] = None) -> None:
42
+ inner_res: Set[str] = set()
43
+ for r in region_ids:
44
+ inner_res |= scope_children(r, scopes)
45
+ pool = region_ids - inner_res
46
+
47
+ def _flush_prereqs(targets: Set[str]) -> None:
48
+ anc, q = set(), deque(targets)
49
+ while q:
50
+ cur = q.popleft()
51
+ for ps, _, _ in scopes["in_edges"].get(cur, ()):
52
+ if ps in pool and ps not in emitted and ps not in targets and ps not in anc:
53
+ anc.add(ps)
54
+ q.append(ps)
55
+ for pid in ordered_ids:
56
+ if pid in anc and pid not in emitted:
57
+ _dispatch(pid)
58
+
59
+ def _dispatch(nid: str) -> None:
60
+ if nid in emitted:
61
+ return
62
+ indent = " " * (4 + depth * 4)
63
+ if nid in scopes["if"]:
64
+ sc = scopes["if"][nid]
65
+ _flush_prereqs({nid} | sc["only_true"] | sc["only_false"])
66
+ emitted.add(nid)
67
+ nctx, _ = emit_node_fn(nid, extra_indent=0, build_only=True)
68
+ p = {**nctx.data, **nctx.params}
69
+ cond_e = nctx.resolve_input(["condition", "cond"], p.get("condition", True))
70
+ then_fb = p.get("then_val") if p.get("then_val") is not None else p.get("default_then", True)
71
+ else_fb = p.get("else_val") if p.get("else_val") is not None else p.get("default_else", False)
72
+ then_e = nctx.resolve_input(["then_val", "true_val", "default_then", "a"], format_literal(then_fb))
73
+ else_e = nctx.resolve_input(["else_val", "false_val", "default_else", "b"], format_literal(else_fb))
74
+ bv, sv = f"branch_{nctx.safe_id}", f"selected_{nctx.safe_id}"
75
+ s_line = len(cg.code_body_lines) + 1
76
+ cg.code_body_lines.extend([f"{indent}# Step {cg.step_counter}: [{nctx.label}]", f"{indent}{bv} = bool({cond_e})", f"{indent}{sv} = None"])
77
+ hdr_idx = len(cg.code_body_lines)
78
+ cg.output_var_map.update({f"{nid}:result": sv, f"{nid}:out": sv, f"{nid}:val": sv, f"{nid}:then": sv, f"{nid}:true": sv, f"{nid}:else": sv, f"{nid}:false": sv, f"{nid}:branch_taken": bv, nid: sv})
79
+ register_fn(nctx, sv)
80
+ cg.source_map.append({"node_id": nid, "line_start": s_line, "line_end": hdr_idx, "target_var": sv, "statement_label": nctx.label})
81
+ cg.step_counter += 1
82
+ cg.code_body_lines.extend([f"{indent}if {bv}:", f"{indent} {sv} = {then_e}"])
83
+ _emit_region(sc["only_true"] - emitted, depth + 1, loop_ctx)
84
+ cg.code_body_lines.extend([f"{indent}else:", f"{indent} {sv} = {else_e}"])
85
+ _emit_region(sc["only_false"] - emitted, depth + 1, loop_ctx)
86
+ preinit_block_vars(cg, hdr_idx, indent, {bv, sv})
87
+ completed_ifs.append((nid, bv, sc["only_true"], sc["only_false"]))
88
+ return
89
+
90
+ if nid in scopes["loop"]:
91
+ sc = scopes["loop"][nid]
92
+ _flush_prereqs({nid} | sc["body"])
93
+ emitted.add(nid)
94
+ nctx, _ = emit_node_fn(nid, extra_indent=0, build_only=True)
95
+ p, ovm, safe = {**nctx.data, **nctx.params}, cg.output_var_map, nctx.safe_id
96
+ s_line = len(cg.code_body_lines) + 1
97
+ cg.code_body_lines.append(f"{indent}# Step {cg.step_counter}: [{nctx.label}]")
98
+ if sc["kind"] == "for":
99
+ rc = p.get("range_count", nctx.data.get("range_count", p.get("count", nctx.data.get("count"))))
100
+ raw_it = p.get("iterable") or nctx.data.get("iterable") or nctx.node_inputs.get("iterable") or p.get("items") or nctx.data.get("items") or ""
101
+ has_it_edge = any(str(e.get("target_handle") or e.get("targetHandle") or "").lower() in ("iterable", "items", "") for e in nctx.incoming)
102
+ if isinstance(raw_it, str) and raw_it.strip():
103
+ ok_it, parsed_it = parse_safe_literal_or_json(raw_it)
104
+ fb_it = repr(list(parsed_it)) if ok_it and isinstance(parsed_it, (list, tuple, set, range)) else format_literal(raw_it)
105
+ else:
106
+ fb_it = repr(list(raw_it)) if isinstance(raw_it, (list, tuple, set, range)) else f"list(range(int({format_literal(rc)})))" if rc is not None else "[1, 2, 3]"
107
+ it_expr = nctx.resolve_input(["iterable", "items"], fb_it) if has_it_edge else fb_it
108
+ max_it = int(p.get("max_iterations", 1000) or 1000)
109
+ src_v, items_v, idx_v, item_v, cnt_v = f"loop_src_{safe}", f"items_{safe}", f"idx_{safe}", f"item_{safe}", f"loop_iters_{safe}"
110
+ cg.code_body_lines.extend([f"{indent}{src_v} = list({it_expr})", f"{indent}if len({src_v}) > {max_it}:", f'{indent} raise ValueError("[ERR-LOOP-OVERFLOW] For loop exceeded max_iterations.")', f"{indent}{items_v}, {cnt_v}, {item_v} = [], 0, None"])
111
+ ovm.update({f"{nid}:item": item_v, f"{nid}:body": item_v, f"{nid}:index": idx_v, f"{nid}:items": items_v, f"{nid}:result": items_v, f"{nid}:out": items_v, f"{nid}:val": items_v, f"{nid}:iteration_count": cnt_v, f"{nid}:iterations": cnt_v, f"{nid}:done": "True", f"{nid}:completed": "True", nid: items_v})
112
+ register_fn(nctx, items_v)
113
+ cg.source_map.append({"node_id": nid, "line_start": s_line, "line_end": len(cg.code_body_lines), "target_var": items_v, "statement_label": nctx.label})
114
+ cg.step_counter += 1
115
+ hdr_idx = len(cg.code_body_lines)
116
+ cg.code_body_lines.extend([f"{indent}for {idx_v}, {item_v} in enumerate({src_v}):", f"{indent} {cnt_v} += 1", f"{indent} {items_v}.append({item_v})"])
117
+ _emit_region(sc["body"] - emitted, depth + 1, {"kind": "for", "continue_stmts": ["continue"]})
118
+ preinit_block_vars(cg, hdr_idx, indent, {src_v, items_v, idx_v, item_v, cnt_v})
119
+ else:
120
+ init_e = nctx.resolve_input("initial_value", format_literal(p.get("initial_value", 0)))
121
+ op = p.get("operator", "<") if p.get("operator", "<") in ("<", "<=", ">", ">=", "==", "!=") else "<"
122
+ cmp_e = nctx.resolve_input(["compare_value", "condition_val"], format_literal(p.get("condition_val", p.get("compare_value", 5))))
123
+ step_e = nctx.resolve_input("step", format_literal(p.get("step", 1)))
124
+ max_it = int(p.get("max_iterations", 100) or 100)
125
+ mode = str(p.get("loop_mode") or p.get("loop_variant") or "while").strip().lower()
126
+ cur_v, cnt_v = f"loop_val_{safe}", f"loop_iters_{safe}"
127
+ cg.code_body_lines.extend([f"{indent}{cur_v} = {init_e}", f"{indent}{cnt_v} = 0"])
128
+ ovm.update({f"{nid}:body": cur_v, f"{nid}:val": cur_v, f"{nid}:final_value": cur_v, f"{nid}:result": cur_v, f"{nid}:out": cur_v, f"{nid}:iteration_count": cnt_v, f"{nid}:iterations": cnt_v, f"{nid}:step_count": cnt_v, f"{nid}:done": "True", f"{nid}:completed": "True", nid: cur_v})
129
+ register_fn(nctx, cur_v)
130
+ cg.source_map.append({"node_id": nid, "line_start": s_line, "line_end": len(cg.code_body_lines), "target_var": cur_v, "statement_label": nctx.label})
131
+ cg.step_counter += 1
132
+ hdr_idx = len(cg.code_body_lines)
133
+ c_stmts = [f"{cur_v} += {step_e}"] + ([f"if not ({cur_v} {op} {cmp_e}): break"] if mode == "do_while" else []) + ["continue"]
134
+ if mode == "do_while":
135
+ cg.code_body_lines.extend([f"{indent}while True:", f"{indent} if {cnt_v} >= {max_it}: break", f"{indent} {cnt_v} += 1"])
136
+ else:
137
+ cg.code_body_lines.extend([f"{indent}while ({cur_v} {op} {cmp_e}) and {cnt_v} < {max_it}:", f"{indent} {cnt_v} += 1"])
138
+ _emit_region(sc["body"] - emitted, depth + 1, {"kind": mode, "continue_stmts": c_stmts})
139
+ cg.code_body_lines.append(f"{indent} {cur_v} += {step_e}")
140
+ if mode == "do_while":
141
+ cg.code_body_lines.append(f"{indent} if not ({cur_v} {op} {cmp_e}): break")
142
+ preinit_block_vars(cg, hdr_idx, indent, {cur_v, cnt_v})
143
+ return
144
+
145
+ if nid in scopes["try"]:
146
+ sc = scopes["try"][nid]
147
+ _flush_prereqs({nid} | sc["try_nodes"])
148
+ emitted.add(nid)
149
+ nctx, _ = emit_node_fn(nid, extra_indent=0, build_only=True)
150
+ p, safe = {**nctx.data, **nctx.params}, nctx.safe_id
151
+ fb_e = nctx.resolve_input("fallback_val", format_literal(p.get("default_fallback", "safe_fallback")))
152
+ ctype = str(p.get("catch_type", "Exception") or "Exception")
153
+ if ctype not in SAFE_EXCEPTS:
154
+ ctype = "Exception"
155
+ nv, ev, hv = f"try_res_{safe}", f"caught_err_{safe}", f"has_err_{safe}"
156
+ s_line = len(cg.code_body_lines) + 1
157
+ cg.code_body_lines.extend([f"{indent}# Step {cg.step_counter}: [{nctx.label}]", f'{indent}{ev}, {hv}, {nv} = "", False, None'])
158
+ hdr_idx = len(cg.code_body_lines)
159
+ cg.code_body_lines.append(f"{indent}try:")
160
+ prev_diags = len(cg.diagnostics)
161
+ _emit_region(sc["try_nodes"] - emitted, depth + 1, loop_ctx)
162
+ cg.diagnostics[prev_diags:] = [d for d in cg.diagnostics[prev_diags:] if d.get("code") not in TRY_SUPPRESS_CODES]
163
+ val_e = nctx.resolve_input("try_val", format_literal(p.get("default_try", None)))
164
+ cg.code_body_lines.extend([f"{indent} {nv} = {val_e}", f"{indent} if {nv} is None: {nv} = {fb_e}", f"{indent}except {ctype} as _err_{safe}:", f"{indent} {ev}, {hv}, {nv} = str(_err_{safe}), True, {fb_e}"])
165
+ preinit_block_vars(cg, hdr_idx, indent, {nv, ev, hv})
166
+ cg.output_var_map.update({f"{nid}:result": nv, f"{nid}:val": nv, f"{nid}:out": nv, f"{nid}:caught_error": ev, f"{nid}:has_error": hv, nid: nv})
167
+ register_fn(nctx, nv)
168
+ cg.source_map.append({"node_id": nid, "line_start": s_line, "line_end": len(cg.code_body_lines), "target_var": nv, "statement_label": nctx.label})
169
+ cg.step_counter += 1
170
+ return
171
+
172
+ if loop_ctx is not None and norm_node_type(node_map[nid]) in ("control.loop_control", "loop_control"):
173
+ emitted.add(nid)
174
+ nctx, _ = emit_node_fn(nid, extra_indent=0, build_only=True)
175
+ pe = {**nctx.data, **nctx.params}
176
+ act = str(pe.get("action", "break")).strip().lower()
177
+ sig_v = f"signal_{nctx.safe_id}"
178
+ s_line = len(cg.code_body_lines) + 1
179
+ cg.code_body_lines.extend([f"{indent}# Step {cg.step_counter}: [{nctx.label}]", f"{indent}{sig_v} = {format_literal(act)}"])
180
+ cond_inc = [e for e in incoming_edges_map.get(nid, []) if str(e.get("source_handle") or e.get("sourceHandle") or "").lower() not in TRUE_HANDLES + FALSE_HANDLES + BODY_HANDLES]
181
+ stmt_body = list(loop_ctx["continue_stmts"]) if act == "continue" else ([act] if act == "break" else ["pass"])
182
+ if cond_inc:
183
+ ce = nctx.resolve_input("condition", True)
184
+ cg.code_body_lines.extend([f"{indent}if {ce}:"] + [f"{indent} {st}" for st in stmt_body])
185
+ elif pe.get("condition", True) is not False:
186
+ cg.code_body_lines.extend([f"{indent}{st}" for st in stmt_body])
187
+ cg.output_var_map.update({f"{nid}:signal": sig_v, f"{nid}:action_taken": sig_v, nid: sig_v})
188
+ cg.source_map.append({"node_id": nid, "line_start": s_line, "line_end": len(cg.code_body_lines), "target_var": sig_v, "statement_label": nctx.label})
189
+ cg.step_counter += 1
190
+ return
191
+
192
+ emitted.add(nid)
193
+ ovr = build_merge_override(nid, cg, incoming_edges_map.get(nid, []), completed_ifs, indent, node_map.get(nid))
194
+ emit_node_fn(nid, extra_indent=depth * 4, incoming_override=ovr)
195
+
196
+ for nid in ordered_ids:
197
+ if nid in pool:
198
+ _dispatch(nid)
199
+
200
+ _emit_region(set(node_map.keys()), 0, None)
201
+ return True
lambdas/codegen/wave1_core_emitters.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Codegen Subpackage: Safe Wave-1 Core & Workspace Env Emitters
3
+ Eliminates raw code-string interpolation for type_cast, variable/constant literals,
4
+ and emits AST-validated sys.env_get / sys.env_set statements.
5
+ """
6
+
7
+ import re
8
+ from typing import Any, Optional, Tuple
9
+
10
+ from lambdas.codegen.context import NodeCodegenCtx
11
+ from language_core.codegen_contracts import format_literal, sanitize_identifier
12
+ from language_core.executor.wave1_handlers import normalize_cast_target
13
+ from language_core.workspace_env_contracts import validate_env_key
14
+
15
+ SAFE_HTTP_METHODS = frozenset({"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"})
16
+ SAFE_BINARY_OPS = frozenset({"+", "-", "*", "/", "//", "%", "**"})
17
+ SAFE_COMPARE_OPS = frozenset({"==", "!=", "<", "<=", ">", ">="})
18
+ SAFE_LOGICAL_OPS = frozenset({"and", "or", "not"})
19
+ SAFE_ROUNDING_MODES = frozenset({"none", "round", "floor", "ceil"})
20
+ _ENCODING_RE = re.compile(r"^[a-zA-Z0-9_-]{2,24}$")
21
+
22
+
23
+ def sanitize_encoding_name(raw_enc: Any, default: str = "utf-8") -> str:
24
+ """Validate text encoding identifier strictly to prevent string interpolation escape."""
25
+ candidate = str(raw_enc or default).strip()
26
+ return candidate if _ENCODING_RE.match(candidate) else default
27
+
28
+
29
+ def validate_safe_op(raw_op: Any, allowed: frozenset, default: str, nctx: NodeCodegenCtx, kind: str) -> str:
30
+ """Allowlist operator string cleanly before code interpolation."""
31
+ op = str(raw_op or default).strip()
32
+ if op not in allowed:
33
+ nctx.cg.diagnostics.append({
34
+ "code": "CODEGEN-SECURITY-VIOLATION",
35
+ "severity": "error",
36
+ "message": f"Unsafe {kind} operator '{op}' rejected on node '{nctx.label}' ({nctx.node_id}).",
37
+ "node_id": nctx.node_id,
38
+ })
39
+ return default
40
+ return op
41
+
42
+
43
+ def emit_safe_constant_node(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
44
+ """Emit literal constant assignment supporting both literal_value and value aliases."""
45
+ raw_type, node_id, safe_id, params, cg = nctx.raw_type, nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
46
+ lines = cg.code_body_lines
47
+ raw_val = params.get("literal_value")
48
+ if raw_val is None:
49
+ raw_val = params.get("value", 0)
50
+ ctype = str(params.get("const_type") or "").strip().lower()
51
+ _tmap = (
52
+ ("int", "int"), ("integer", "int"), ("float", "float"), ("number", "float"),
53
+ ("str", "str"), ("string", "str"), ("text", "str"), ("bool", "bool"),
54
+ ("boolean", "bool"), ("list", "list"), ("dict", "dict"),
55
+ )
56
+ dtype = ctype if ctype in ("int", "float", "str", "bool", "list", "dict", "any") else next((v for k, v in _tmap if k in raw_type), "any")
57
+ try:
58
+ formatted_val = format_literal(raw_val, dtype)
59
+ except ValueError as ve:
60
+ cg.diagnostics.append({"code": "VAL-NON-FINITE-NUMBER", "severity": "error", "message": f"Node '{nctx.label}' ({node_id}): {ve}", "node_id": node_id})
61
+ formatted_val = "0.0"
62
+ nv = sanitize_identifier(params.get("const_name"), f"val_{safe_id}")
63
+ lines.append(f" {nv} = {formatted_val}")
64
+ return True, nv
65
+
66
+
67
+ def emit_safe_variable_node(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
68
+ """Emit variable assignment preserving explicit type annotations and upstream input overrides."""
69
+ params, safe_id, cg = nctx.params, nctx.safe_id, nctx.cg
70
+ nv = sanitize_identifier(params.get("var_name", "my_var"), f"var_{safe_id}")
71
+ default_val = params.get("literal_value") if params.get("literal_value") is not None else params.get("value", 0)
72
+ vtype = str(params.get("var_type") or ("str" if isinstance(default_val, str) and not default_val.lstrip("-").isdigit() else "int")).lower()
73
+ if nctx.incoming:
74
+ assigned = nctx.resolve_input(["in_val", "in", "val", "value"], default_val)
75
+ else:
76
+ assigned = format_literal(default_val, vtype)
77
+ cg.code_body_lines.append(f" {nv} = {assigned}")
78
+ return True, nv
79
+
80
+
81
+ def emit_safe_type_cast(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
82
+ """Emit AST-safe explicit type conversion with strict target_type allowlisting (zero code injection)."""
83
+ node_id, safe_id, params, cg = nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
84
+ lines, nv = cg.code_body_lines, f"cast_{safe_id}"
85
+ raw_target = params.get("target_type", "str")
86
+ try:
87
+ ttype = normalize_cast_target(raw_target)
88
+ except ValueError as exc:
89
+ cg.diagnostics.append({
90
+ "code": "CODEGEN-SECURITY-VIOLATION",
91
+ "severity": "error",
92
+ "message": str(exc),
93
+ "node_id": node_id,
94
+ "port_id": "target_type",
95
+ })
96
+ lines.append(f" {nv} = None")
97
+ return True, nv
98
+
99
+ val_expr = nctx.resolve_input(["value", "in_val", "val", "raw"], params.get("value", 0))
100
+ if ttype == "bool":
101
+ lines.append(f" {nv} = (({val_expr}).strip().lower() in ('true', '1', 'yes', 'on') if isinstance({val_expr}, str) else bool({val_expr}))")
102
+ elif ttype == "json":
103
+ cg.discovered_modules.add("json")
104
+ lines.append(f" {nv} = (json.loads({val_expr}) if isinstance({val_expr}, str) else json.dumps({val_expr}))")
105
+ else:
106
+ lines.append(f" {nv} = {ttype}({val_expr})")
107
+ return True, nv
108
+
109
+
110
+ def emit_safe_sys_env(nctx: NodeCodegenCtx) -> Tuple[bool, Optional[str]]:
111
+ """Emit clean Python os.getenv / os.environ[KEY] code for workspace env nodes."""
112
+ raw_type, node_id, safe_id, params, cg = nctx.raw_type, nctx.node_id, nctx.safe_id, nctx.params, nctx.cg
113
+ lines, ovm = cg.code_body_lines, cg.output_var_map
114
+
115
+ if raw_type not in ("alli.sys.env", "sys.env_get", "sys.env", "sys.env_set"):
116
+ return False, None
117
+
118
+ is_set_mode = raw_type == "sys.env_set" or (raw_type in ("sys.env", "alli.sys.env") and str(params.get("mode") or "get").strip().lower() == "set")
119
+ raw_key = str(params.get("key") or ("APP_CONFIG_VAR" if is_set_mode else "ENV_VAR")).strip()
120
+ ok_key, clean_key_or_code, key_err = validate_env_key(raw_key)
121
+ if not ok_key:
122
+ cg.diagnostics.append({
123
+ "code": clean_key_or_code,
124
+ "severity": "error",
125
+ "message": key_err,
126
+ "node_id": node_id,
127
+ "port_id": "key",
128
+ })
129
+ return True, f"env_err_{safe_id}"
130
+
131
+ cg.discovered_modules.add("os")
132
+ cg.referenced_env_keys.add(clean_key_or_code)
133
+ key_lit = format_literal(clean_key_or_code)
134
+
135
+ is_secret = bool(params.get("is_secret"))
136
+ if is_set_mode:
137
+ if nctx.is_wired(["value", "val", "in"]):
138
+ val_expr = nctx.resolve_input(["value", "val", "in"], "")
139
+ lines.append(f" os.environ[{key_lit}] = str({val_expr})")
140
+ elif is_secret:
141
+ lines.append(f" os.environ[{key_lit}] = os.getenv({key_lit}, \"\")")
142
+ else:
143
+ raw_val = nctx.node_inputs.get("value") if "value" in nctx.node_inputs else params.get("value")
144
+ val_expr = format_literal(raw_val if raw_val is not None else "")
145
+ lines.append(f" os.environ[{key_lit}] = str({val_expr})")
146
+ nv = f"env_set_{safe_id}"
147
+ lines.append(f" {nv} = {key_lit}")
148
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:key": nv, f"{node_id}:out": nv, node_id: nv})
149
+ return True, nv
150
+
151
+ def_val = "" if is_secret else (params.get("default_val") if params.get("default_val") is not None else "")
152
+ def_expr = format_literal(str(def_val))
153
+ nv = sanitize_identifier(params.get("var_name"), f"env_{safe_id}")
154
+ lines.append(f" {nv} = os.getenv({key_lit}, {def_expr})")
155
+ ovm.update({f"{node_id}:result": nv, f"{node_id}:val": nv, f"{node_id}:out": nv, node_id: nv})
156
+ return True, nv
lambdas/codegen_handler.py CHANGED
@@ -1,49 +1,77 @@
1
  """
2
- ALLI Code Generator Service (alli-codegen) -- Phase 7 Implementation
3
  Synthesizes deterministic, standalone, PEP 8-compliant Python scripts,
4
  requirements.txt, and Dockerfiles directly from validated visual graphs.
5
-
6
- Key Guarantees:
7
- 1. Authoritative Backend Service with mandatory preflight validation gating.
8
- 2. Deterministic topological step sequencing using Kahn's algorithm.
9
- 3. Clean PEP 8 formatting with standard library imports separated from external packages.
10
- 4. Exclusion of standard libraries and internal namespaces from requirements.txt.
11
- 5. Non-root secure Dockerfile generation.
12
- 6. Syntactic AST verification (ast.parse) with SYNTAX-001 diagnostic on error.
13
- 7. 64-character SHA-256 fingerprinting and source-mapping for studio telemetry.
14
  """
15
 
16
- import ast
17
  import json
18
- import math
19
  import re
20
- from collections import defaultdict, deque
21
- from typing import Dict, Any, List, Optional, Set, Tuple
22
-
23
- from language_core.node_schema import default_registry
24
- from library_inspector.node_synthesizer import global_staging_registry
25
- from language_core.execution_session import compute_graph_hash
26
- from lambdas.validate_handler import validate_workflow_protocol_logic
27
 
28
  from language_core.codegen_contracts import (
29
- STANDARD_LIBS,
30
  BANNED_IMPORTS,
31
  PINNED_PACKAGE_VERSIONS,
32
- CodegenRequest,
33
- CodegenArtifacts,
34
- CodegenDiagnostic,
35
- SourceMapEntry,
36
- sanitize_identifier,
37
  format_literal,
38
- compute_codegen_hash,
39
- validate_generated_ast,
40
- inspect_generated_ast_security,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  )
42
 
43
  # Backwards compatible internal aliases
44
  _sanitize_identifier = sanitize_identifier
45
  _format_literal = format_literal
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  def generate_code_logic(
49
  nodes: Optional[List[Dict[str, Any]]] = None,
@@ -56,16 +84,11 @@ def generate_code_logic(
56
  strict_validation: bool = False,
57
  pin_exact: bool = True,
58
  ) -> Dict[str, Any]:
59
- """
60
- Authoritative Python code generation engine (Phase 7).
61
- Transforms a validated ALLI graph into standalone PEP 8 Python, requirements.txt,
62
- and Dockerfile container artifacts with execution parity and AST validation.
63
- """
64
- # ─── 1. EXTRACT & NORMALIZE INPUTS ────────────────────────────────────────
65
  from language_core.workflow_migration import migrate_workflow
 
66
  if isinstance(nodes, dict) and "nodes" in nodes and graph is None:
67
- graph = nodes
68
- nodes = None
69
 
70
  if graph and isinstance(graph, dict):
71
  graph = migrate_workflow(graph)
@@ -80,1469 +103,130 @@ def generate_code_logic(
80
  raw_edges = migrated.get("edges") or []
81
  name = graph_name or "alli_workflow"
82
  deps = dependencies or []
83
- graph_obj = {
84
- "name": name,
85
- "nodes": raw_nodes,
86
- "edges": raw_edges,
87
- "dependencies": deps,
88
- }
89
 
90
- clean_graph_name = sanitize_identifier(name.lower().replace(" ", "_"), "alli_workflow")
91
- clean_filename = f"{clean_graph_name}.py"
92
  graph_fingerprint = compute_graph_hash(graph_obj)
93
 
94
- # ─── 2. EMPTY WORKFLOW HANDLING ───────────────────────────────────────────
95
  if not raw_nodes:
96
- empty_code = (
97
- '"""\n'
98
- f'Workflow Pipeline: {name}\n'
99
- 'Auto-generated by ALLI (Advanced Logic & Linked Intelligence)\n'
100
- '"""\n\n\n'
101
- 'def main():\n'
102
- ' """Empty workflow - add nodes on the canvas to generate code."""\n'
103
- ' return None\n\n\n'
104
- 'if __name__ == "__main__":\n'
105
- ' main()\n'
106
- )
107
- empty_req = "# Standard library only. No external pip packages needed.\n"
108
- empty_docker = (
109
- "FROM python:3.12-slim\n"
110
- "WORKDIR /app\n"
111
- "COPY requirements.txt .\n"
112
- "RUN pip install --no-cache-dir -r requirements.txt\n"
113
- "COPY . .\n"
114
- "RUN useradd -m appuser && chown -R appuser:appuser /app\n"
115
- "USER appuser\n"
116
- f'CMD ["python", "{clean_filename}"]\n'
117
- )
118
- empty_res = {
119
- "success": True,
120
- "python_code": empty_code,
121
- "requirements_txt": empty_req,
122
- "dockerfile": empty_docker,
123
- "filename": clean_filename,
124
- "dependencies": [],
125
- "execution_order": [],
126
- "diagnostics": [],
127
- "source_map": [],
128
- "generator_version": "1.0.0",
129
- "graph_fingerprint": graph_fingerprint,
130
- "metadata": {
131
- "hash": compute_codegen_hash({
132
- "python_code": empty_code,
133
- "requirements_txt": empty_req,
134
- "dockerfile": empty_docker,
135
- }),
136
- "ast_valid": True,
137
- "node_count": 0,
138
- },
139
- }
140
- return empty_res
141
-
142
- # ─── 3. PREFLIGHT GRAPH VALIDATION & FAIL-CLOSED GATING ───────────────────
143
- diagnostics: List[Dict[str, Any]] = []
144
 
145
- # Check for duplicate node IDs
146
- seen_ids: Set[str] = set()
147
- for n in raw_nodes:
148
- nid = str(n.get("id", ""))
149
- if nid in seen_ids:
150
- diagnostics.append({
151
- "code": "VAL-DUPLICATE-NODE-ID",
152
- "severity": "error",
153
- "message": f"Duplicate node ID detected: '{nid}'",
154
- "node_id": nid,
155
- "fix_suggestion": "Ensure all nodes have unique identifiers on the canvas.",
156
- })
157
- seen_ids.add(nid)
158
-
159
- if validate:
160
- val_res = validate_workflow_protocol_logic(graph_obj)
161
- val_diags = val_res.get("diagnostics") or []
162
- for d in val_diags:
163
- code = d.get("code")
164
- sev = d.get("severity")
165
- # Always fail closed on cycle (DAG-001), type assignability (TYPE-001),
166
- # unapproved staging nodes, math errors, or if strict_validation is active
167
- if sev == "error":
168
- if (
169
- code in (
170
- "DAG-001",
171
- "TYPE-001",
172
- "VAL-TYPE-MISMATCH",
173
- "MATH-E001",
174
- "MATH-E002",
175
- "ERR-ZERO-DIVISION",
176
- "ERR-MATH-DOMAIN",
177
- "ERR-TYPE-CAST",
178
- "SEC-001",
179
- "ERR-SECURITY-PATH-TRAVERSAL",
180
- "ERR-NOT-SUPPORTED",
181
- "VAL-DUPLICATE-NODE-ID",
182
- "VAL-REQUIRED-INPUT-MISSING",
183
- "ERR-ARG-MISSING",
184
- "VAL-PORT-REQUIRED-MISSING",
185
- )
186
- or "TYPE" in str(code).upper()
187
- or strict_validation
188
- ):
189
- diagnostics.append(d)
190
- else:
191
- # Downgrade non-fatal schema warnings when generating code in permissive mode
192
- diagnostics.append({**d, "severity": "warning"})
193
-
194
- xfail_ids = {"wf_for_sum", "wf_try_except", "wf_fn_add", "wf_class_student", "wf_oop_student", "17_for_loop_sum", "19_try_except_div_zero", "20_function_add", "21_class_student_display"}
195
- if (graph_obj or {}).get("is_xfail") or (graph_obj or {}).get("workflow_id") in xfail_ids or (graph_obj or {}).get("id") in xfail_ids:
196
- for n in raw_nodes:
197
- n_type = str(n.get("node_type") or n.get("type") or n.get("data", {}).get("node_type") or "").lower()
198
- if n_type in ("control.for_loop", "control.try_except", "function.def", "function.call", "oop.class_def", "oop.instance", "oop.method_call"):
199
- n_lbl = n.get("label") or n.get("data", {}).get("label") or n_type
200
- diagnostics.append({
201
- "code": "ERR-NOT-SUPPORTED",
202
- "severity": "error",
203
- "message": f"{n_lbl} ({n_type}) is not supported yet in this engine",
204
- "node_id": n.get("id", ""),
205
- })
206
-
207
- blocking_errors = [d for d in diagnostics if d.get("severity") == "error"]
208
- if blocking_errors:
209
  return {
210
- "success": False,
211
- "python_code": "",
212
- "code": "",
213
- "requirements_txt": "",
214
- "dockerfile": "",
215
- "filename": clean_filename,
216
- "dependencies": [],
217
- "execution_order": [],
218
- "diagnostics": diagnostics,
219
- "source_map": [],
220
- "generator_version": "1.0.0",
221
  "graph_fingerprint": graph_fingerprint,
222
- "metadata": {
223
- "valid": False,
224
- "error_count": len(blocking_errors),
225
- "ast_valid": False,
226
- "node_count": len(raw_nodes),
227
- },
228
  }
229
 
230
- # ─── 4. FILTER VISUAL GROUPS & RESOLVE EXECUTION ORDER ────────────────────
231
- nodes = [
232
  n for n in raw_nodes
233
  if str(n.get("node_type") or n.get("type") or n.get("data", {}).get("node_type") or "")
234
  not in ("system.workflow_group", "workflow_group", "workflowGroup")
235
  ]
236
- edges = [
237
  e for e in raw_edges
238
  if not str(e.get("source", "")).startswith("group_") and not str(e.get("target", "")).startswith("group_")
239
  ]
240
- node_map = {str(n["id"]): n for n in nodes if n.get("id")}
241
-
242
- # Deterministic Kahn's Topological Sort with tie-breaking
243
- if execution_order and all(nid in node_map for nid in execution_order) and len(execution_order) == len(node_map):
244
- ordered_ids = list(execution_order)
245
- else:
246
- in_degrees = {nid: 0 for nid in node_map}
247
- adj_list = defaultdict(list)
248
- for edge in edges:
249
- s = str(edge.get("source") or edge.get("from_node") or "")
250
- t = str(edge.get("target") or edge.get("to_node") or "")
251
- if s in in_degrees and t in in_degrees:
252
- adj_list[s].append(t)
253
- in_degrees[t] += 1
254
-
255
- queue = deque(sorted([nid for nid, deg in in_degrees.items() if deg == 0]))
256
- ordered_ids = []
257
- while queue:
258
- curr = queue.popleft()
259
- ordered_ids.append(curr)
260
- for nbr in sorted(adj_list.get(curr, [])):
261
- in_degrees[nbr] -= 1
262
- if in_degrees[nbr] == 0:
263
- queue.append(nbr)
264
- # Maintain deterministic queue ordering
265
- queue = deque(sorted(list(queue)))
266
-
267
- # Cycle detection guard
268
- if len(ordered_ids) < len(node_map):
269
- cycle_diag = {
270
- "code": "DAG-001",
271
- "severity": "error",
272
- "message": "Cycle detected during code generation topological sequencing.",
273
- "fix_suggestion": "Remove circular feedback wires between procedural nodes.",
274
- }
275
- return {
276
- "success": False,
277
- "python_code": "",
278
- "requirements_txt": "",
279
- "dockerfile": "",
280
- "filename": clean_filename,
281
- "dependencies": [],
282
- "execution_order": [],
283
- "diagnostics": [cycle_diag],
284
- "source_map": [],
285
- "generator_version": "1.0.0",
286
- "graph_fingerprint": graph_fingerprint,
287
- "metadata": {"valid": False, "has_cycles": True},
288
- }
289
-
290
- # ─── 5. INCOMING EDGE MAPPING & DEPENDENCY TRACKING ───────────────────────
291
- incoming_edges_map = defaultdict(list)
292
- for edge in edges:
293
- t = str(edge.get("target") or edge.get("to_node") or "")
294
- incoming_edges_map[t].append(edge)
295
 
296
- discovered_modules: Set[str] = set()
297
- discovered_requirements: Set[str] = set()
 
298
 
 
299
  for dep in deps:
300
  clean_dep = str(dep).strip()
301
  if clean_dep and clean_dep not in STANDARD_LIBS and clean_dep not in BANNED_IMPORTS:
302
- discovered_requirements.add(clean_dep)
303
-
304
- output_var_map: Dict[str, str] = {}
305
- code_body_lines: List[str] = []
306
- source_map: List[Dict[str, Any]] = []
307
- step_counter = 1
308
- last_return_var: Optional[str] = None
309
-
310
- # Base line offset: 2 lines for docstring + imports + 2 blank lines + def main():
311
- current_line_number = 7
312
-
313
- # ─── 6. STEP SYNTHESIS LOOP ───────────────────────────────────────────────
314
- for node_id in ordered_ids:
315
  node = node_map.get(node_id)
316
  if not node:
317
- continue
318
-
319
- raw_type = str(
320
- node.get("node_type")
321
- or node.get("type")
322
- or node.get("data", {}).get("node_type")
323
- or node.get("data", {}).get("nodeType")
324
- or ""
325
- ).strip().lower()
326
-
327
  data = node.get("data", {}) or {}
328
- top_params = node.get("parameters") if isinstance(node.get("parameters"), dict) else {}
329
- params = dict(top_params)
330
  node_inputs = node.get("inputs") if isinstance(node.get("inputs"), dict) else (data.get("inputs") if isinstance(data.get("inputs"), dict) else {})
331
-
332
  label = str(data.get("label") or node.get("label") or raw_type or node_id)
333
- # Clean, human-readable safe_id (avoids ugly raw UI UUIDs like alli_primitive_input_mvumq1nh_kvh9c)
334
- clean_lbl = re.sub(r"[^a-zA-Z0-9_]", "_", label.lower()).strip("_")
335
- clean_lbl = re.sub(r"_+", "_", clean_lbl)
336
  if str(node_id).startswith("alli_primitive_") or len(str(node_id)) > 20:
337
- if clean_lbl and not clean_lbl[0].isdigit() and len(clean_lbl) <= 24:
338
- safe_id = f"{clean_lbl}_{step_counter}"
339
- else:
340
- safe_id = f"step_{step_counter}"
341
  else:
342
- safe_id = sanitize_identifier(node_id.replace("-", "_"), f"node_{step_counter}")
343
- incoming = incoming_edges_map.get(node_id, [])
344
-
345
- # Schema inspection from registry
346
- schema = None
347
- if default_registry.has(raw_type):
348
- schema = default_registry.get(raw_type)
349
- elif global_staging_registry.has(raw_type):
350
- schema = global_staging_registry.get(raw_type)
351
 
352
- # Check capability states for codegen support
353
  cap_states = (
354
  (schema.capability_states if schema and hasattr(schema, "capability_states") else None)
355
- or (schema.verification.capability_states if schema and hasattr(schema, "verification") and schema.verification and hasattr(schema.verification, "capability_states") else None)
356
- or data.get("capability_states")
357
- or params.get("capability_states")
358
- or node.get("capability_states")
359
  or (data.get("verification") or {}).get("capability_states")
360
  )
361
  if cap_states and cap_states.get("codegen_supported") is False:
362
- diagnostics.append({
363
- "code": "CODEGEN_UNSUPPORTED",
364
- "severity": "error",
365
- "message": f"Code generation is unsupported for node '{label}' ({node_id}) of type '{raw_type}'.",
366
- "node_id": node_id,
367
- })
368
 
369
  if schema and getattr(schema, "codegen", None):
370
  for imp in schema.codegen.imports:
371
  clean_imp = str(imp).strip()
372
  if clean_imp not in BANNED_IMPORTS:
373
- discovered_modules.add(clean_imp)
374
  if clean_imp not in STANDARD_LIBS:
375
- discovered_requirements.add(clean_imp)
376
  for req in schema.codegen.requirements:
377
  clean_req = str(req).strip()
378
  if clean_req not in STANDARD_LIBS and clean_req not in BANNED_IMPORTS:
379
- discovered_requirements.add(clean_req)
380
-
381
- # Wire and parameter input resolver
382
- def resolve_input(handle_id: Any, fallback_val: Any = "None") -> str:
383
- candidates = [handle_id] if isinstance(handle_id, str) else list(handle_id)
384
- clean_candidates = [str(c).lower() for c in candidates]
385
-
386
- # 1. Match specific target handle from incoming edges
387
- for inc in incoming:
388
- tgt_h = str(inc.get("target_handle") or inc.get("targetHandle") or inc.get("to_port") or "").lower()
389
- if tgt_h in clean_candidates:
390
- src_id = str(inc.get("source") or inc.get("from_node"))
391
- src_h = str(inc.get("source_handle") or inc.get("sourceHandle") or inc.get("from_port") or "")
392
- val = (
393
- output_var_map.get(f"{src_id}:{src_h}")
394
- or output_var_map.get(f"{src_id}:result")
395
- or output_var_map.get(f"{src_id}:out")
396
- or output_var_map.get(f"{src_id}:val")
397
- or output_var_map.get(f"{src_id}:value")
398
- or output_var_map.get(src_id)
399
- )
400
- if val:
401
- return val
402
-
403
- # 2. Single incoming connection fallback (only when target handle is unspecified)
404
- if len(incoming) == 1:
405
- inc = incoming[0]
406
- tgt_h = str(inc.get("target_handle") or inc.get("targetHandle") or inc.get("to_port") or "").lower()
407
- if not tgt_h:
408
- src_id = str(inc.get("source") or inc.get("from_node"))
409
- src_h = str(inc.get("source_handle") or inc.get("sourceHandle") or inc.get("from_port") or "")
410
- val = (
411
- output_var_map.get(f"{src_id}:{src_h}")
412
- or output_var_map.get(f"{src_id}:result")
413
- or output_var_map.get(f"{src_id}:out")
414
- or output_var_map.get(f"{src_id}:val")
415
- or output_var_map.get(f"{src_id}:value")
416
- or output_var_map.get(src_id)
417
- )
418
- if val:
419
- return val
420
-
421
- # 2.5 Direct unconnected input port values
422
- for c in candidates:
423
- if c in node_inputs and node_inputs[c] is not None:
424
- return format_literal(node_inputs[c])
425
- if str(c).lower() in node_inputs and node_inputs[str(c).lower()] is not None:
426
- return format_literal(node_inputs[str(c).lower()])
427
- if c in data and data[c] is not None and c not in ("label", "customData", "parameters", "name", "id", "node_type", "type"):
428
- return format_literal(data[c])
429
- if str(c).lower() in data and data[str(c).lower()] is not None and str(c).lower() not in ("label", "customdata", "parameters", "name", "id", "node_type", "type"):
430
- return format_literal(data[str(c).lower()])
431
 
432
- # 3. Direct configuration parameter from node data
433
- for c in candidates:
434
- if c in params and params[c] is not None:
435
- return format_literal(params[c])
436
- if str(c).lower() in params and params[str(c).lower()] is not None:
437
- return format_literal(params[str(c).lower()])
438
-
439
- # 4. Fallback literal
440
- if fallback_val is not None:
441
- if isinstance(fallback_val, str) and (
442
- fallback_val.startswith('"')
443
- or fallback_val in ("True", "False", "None")
444
- or re.match(r"^[a-zA-Z_][a-zA-Z0-9_]*$", fallback_val)
445
- ):
446
- return fallback_val
447
- return format_literal(fallback_val)
448
- return "None"
449
-
450
- # ─── DISPATCH NODE HANDLERS ───────────────────────────────────────────
451
  node_var: Optional[str] = None
452
- start_step_line = len(code_body_lines) + 1
453
- code_body_lines.append(f" # Step {step_counter}: [{label}]")
454
-
455
- # ─── 0. PROGRAMMING FUNDAMENTALS 11 CATEGORY NODES ───────────────────
456
- if raw_type == "cat_essentials":
457
- node_var = f"essentials_{safe_id}"
458
- in_val = resolve_input("in", resolve_input("message", format_literal(params.get("message", "Hello, World!"))))
459
- code_body_lines.append(f" {node_var} = {in_val}")
460
- code_body_lines.append(f" print({node_var})")
461
- output_var_map[f"{node_id}:out"] = node_var
462
- output_var_map[f"{node_id}:result"] = node_var
463
- output_var_map[node_id] = node_var
464
- last_return_var = node_var
465
-
466
- elif raw_type == "cat_variables":
467
- var_name = sanitize_identifier(str(params.get("var_name") or "my_var"), "my_var")
468
- val_in = resolve_input("in", format_literal(params.get("value", 10)))
469
- code_body_lines.append(f" {var_name} = {val_in}")
470
- output_var_map[f"{node_id}:out"] = var_name
471
- output_var_map[f"{node_id}:out_val"] = var_name
472
- output_var_map[f"{node_id}:val"] = var_name
473
- output_var_map[f"{node_id}:value"] = var_name
474
- output_var_map[f"{node_id}:result"] = var_name
475
- output_var_map[node_id] = var_name
476
- last_return_var = var_name
477
-
478
- elif raw_type == "cat_data_types":
479
- node_var = f"collection_{safe_id}"
480
- dtype = params.get("data_type", "list")
481
- raw_def = str(params.get("default_value") or "[1, 2, 3]")
482
- code_body_lines.append(f" {node_var} = {raw_def}")
483
- output_var_map[f"{node_id}:out"] = node_var
484
- output_var_map[f"{node_id}:collection"] = node_var
485
- output_var_map[f"{node_id}:result"] = node_var
486
- output_var_map[node_id] = node_var
487
- last_return_var = node_var
488
-
489
- elif raw_type == "cat_operators":
490
- node_var = f"calc_{safe_id}"
491
- val_a = resolve_input("a", format_literal(params.get("a", 5)))
492
- val_b = resolve_input("b", format_literal(params.get("b", 3)))
493
- op = str(params.get("operator", "+"))
494
- code_body_lines.append(f" {node_var} = {val_a} {op} {val_b}")
495
- output_var_map[f"{node_id}:out"] = node_var
496
- output_var_map[f"{node_id}:result"] = node_var
497
- output_var_map[node_id] = node_var
498
- last_return_var = node_var
499
-
500
- elif raw_type == "cat_math":
501
- discovered_modules.add("math")
502
- node_var = f"math_{safe_id}"
503
- val_x = resolve_input("x", format_literal(params.get("x", 49.0)))
504
- fn = params.get("func", "sqrt")
505
- code_body_lines.append(f" {node_var} = math.{fn}({val_x})")
506
- output_var_map[f"{node_id}:out"] = node_var
507
- output_var_map[f"{node_id}:result"] = node_var
508
- output_var_map[node_id] = node_var
509
- last_return_var = node_var
510
-
511
- elif raw_type == "cat_control_flow":
512
- node_var = f"branch_{safe_id}"
513
- cond_val = resolve_input("condition", format_literal(params.get("condition", True)))
514
- code_body_lines.append(f" {node_var} = bool({cond_val})")
515
- output_var_map[f"{node_id}:out"] = node_var
516
- output_var_map[f"{node_id}:then"] = node_var
517
- output_var_map[f"{node_id}:else"] = f"(not {node_var})"
518
- output_var_map[f"{node_id}:result"] = node_var
519
- output_var_map[node_id] = node_var
520
- last_return_var = node_var
521
-
522
- elif raw_type == "cat_functions":
523
- fn_name = sanitize_identifier(str(params.get("func_name") or "make_toy"), "make_toy")
524
- node_var = f"res_{fn_name}_{safe_id}"
525
- code_body_lines.append(f" def {fn_name}(args):")
526
- code_body_lines.append(f" return args")
527
- in_arg = resolve_input("args", '"toy_part"')
528
- code_body_lines.append(f" {node_var} = {fn_name}({in_arg})")
529
- output_var_map[f"{node_id}:out"] = node_var
530
- output_var_map[f"{node_id}:result"] = node_var
531
- output_var_map[node_id] = node_var
532
- last_return_var = node_var
533
-
534
- elif raw_type == "cat_strings_collections":
535
- node_var = f"text_{safe_id}"
536
- txt_val = resolve_input("text", format_literal(params.get("text", "Hello World")))
537
- op = params.get("operation", "upper")
538
- if op == "len":
539
- code_body_lines.append(f" {node_var} = len({txt_val})")
540
- else:
541
- code_body_lines.append(f" {node_var} = str({txt_val}).{op}()")
542
- output_var_map[f"{node_id}:out"] = node_var
543
- output_var_map[f"{node_id}:result"] = node_var
544
- output_var_map[node_id] = node_var
545
- last_return_var = node_var
546
-
547
- elif raw_type == "cat_oop":
548
- cname = sanitize_identifier(str(params.get("class_name") or "House"), "House").capitalize()
549
- node_var = f"house_{safe_id}"
550
- code_body_lines.append(f" class {cname}:")
551
- code_body_lines.append(f" def __init__(self):")
552
- code_body_lines.append(f" self.doors = 2")
553
- code_body_lines.append(f" self.windows = 4")
554
- code_body_lines.append(f" {node_var} = {cname}()")
555
- output_var_map[f"{node_id}:out"] = node_var
556
- output_var_map[f"{node_id}:instance"] = node_var
557
- output_var_map[f"{node_id}:result"] = node_var
558
- output_var_map[node_id] = node_var
559
- last_return_var = node_var
560
-
561
- elif raw_type == "cat_files":
562
- node_var = f"file_data_{safe_id}"
563
- fpath = format_literal(params.get("file_path", "data.txt"))
564
- code_body_lines.append(f" {node_var} = f'Content of {{{fpath}}}'")
565
- output_var_map[f"{node_id}:out"] = node_var
566
- output_var_map[f"{node_id}:content"] = node_var
567
- output_var_map[f"{node_id}:result"] = node_var
568
- output_var_map[node_id] = node_var
569
- last_return_var = node_var
570
-
571
- elif raw_type == "cat_modules":
572
- mod_name = str(params.get("module_name") or "math")
573
- discovered_modules.add(mod_name)
574
- node_var = f"tool_{safe_id}"
575
- code_body_lines.append(f" {node_var} = {mod_name}")
576
- output_var_map[f"{node_id}:out"] = node_var
577
- output_var_map[f"{node_id}:module"] = node_var
578
- output_var_map[f"{node_id}:result"] = node_var
579
- output_var_map[node_id] = node_var
580
- last_return_var = node_var
581
-
582
- # 0A. Workflow Start Node
583
- if raw_type in ("workflow.start", "start"):
584
- node_var = f"start_{safe_id}"
585
- code_body_lines.append(f" {node_var} = True")
586
- output_var_map[f"{node_id}:trigger"] = node_var
587
- output_var_map[node_id] = node_var
588
-
589
- # 0B. Schedule Trigger Node
590
- elif raw_type in ("trigger.schedule", "schedule"):
591
- discovered_modules.add("datetime")
592
- node_var = f"trigger_{safe_id}"
593
- ts_var = f"ts_{safe_id}"
594
- code_body_lines.append(f" {ts_var} = datetime.datetime.now(datetime.timezone.utc).isoformat()")
595
- code_body_lines.append(f" {node_var} = True")
596
- output_var_map[f"{node_id}:trigger"] = node_var
597
- output_var_map[f"{node_id}:timestamp"] = ts_var
598
- output_var_map[node_id] = node_var
599
-
600
- # 0C. Webhook Trigger Node
601
- elif raw_type in ("trigger.webhook", "webhook"):
602
- node_var = f"trigger_{safe_id}"
603
- payload_var = f"payload_{safe_id}"
604
- headers_var = f"headers_{safe_id}"
605
- code_body_lines.append(f" {payload_var} = {{}}")
606
- code_body_lines.append(f" {headers_var} = {{}}")
607
- code_body_lines.append(f" {node_var} = True")
608
- output_var_map[f"{node_id}:trigger"] = node_var
609
- output_var_map[f"{node_id}:payload"] = payload_var
610
- output_var_map[f"{node_id}:headers"] = headers_var
611
- output_var_map[node_id] = node_var
612
-
613
- # 0D. API HTTP Request Node
614
- elif raw_type in ("api.http_request", "http_request", "http"):
615
- discovered_modules.add("requests")
616
- discovered_requirements.add("requests")
617
- method = str(params.get("method", "GET")).upper()
618
- default_url = params.get("url", "https://api.example.com/data")
619
- url_expr = resolve_input("url", format_literal(default_url))
620
- payload_expr = resolve_input("payload", "None")
621
- headers_expr = resolve_input("headers", "None")
622
- timeout_val = params.get("timeout", 5)
623
- node_var = f"resp_{safe_id}"
624
- status_var = f"status_{safe_id}"
625
- then_var = f"then_{safe_id}"
626
- headers_res_var = f"resp_headers_{safe_id}"
627
- code_body_lines.append(f" {node_var} = requests.request('{method}', {url_expr}, json={payload_expr}, headers={headers_expr}, timeout={timeout_val})")
628
- code_body_lines.append(f" {status_var} = {node_var}.status_code")
629
- code_body_lines.append(f" {headers_res_var} = dict({node_var}.headers)")
630
- output_var_map[f"{node_id}:response"] = node_var
631
- output_var_map[f"{node_id}:text"] = f"{node_var}.text"
632
- output_var_map[f"{node_id}:data"] = f"{node_var}.json()"
633
- output_var_map[f"{node_id}:result"] = f"{node_var}.json()"
634
- output_var_map[f"{node_id}:out"] = f"{node_var}.json()"
635
- output_var_map[f"{node_id}:status_code"] = status_var
636
- output_var_map[f"{node_id}:headers"] = headers_res_var
637
- output_var_map[f"{node_id}:then"] = "True"
638
- output_var_map[node_id] = node_var
639
-
640
- # 1. Primitives & Constants
641
- elif (
642
- raw_type in ("constant.int", "constant.float", "constant.str", "constant.bool", "core.constant")
643
- or raw_type.startswith("constant.")
644
- or "primitive" in raw_type
645
- or raw_type in ("input", "primitive.input")
646
- ):
647
- raw_val = params.get("literal_value", 0)
648
-
649
- dtype = "any"
650
- if "int" in raw_type or "integer" in raw_type:
651
- dtype = "int"
652
- elif "float" in raw_type or "number" in raw_type:
653
- dtype = "float"
654
- elif "str" in raw_type or "string" in raw_type or "text" in raw_type:
655
- dtype = "str"
656
- elif "bool" in raw_type or "boolean" in raw_type:
657
- dtype = "bool"
658
- elif "list" in raw_type:
659
- dtype = "list"
660
- elif "dict" in raw_type:
661
- dtype = "dict"
662
-
663
- try:
664
- formatted_val = format_literal(raw_val, dtype)
665
- except ValueError as ve:
666
- diagnostics.append({
667
- "code": "VAL-NON-FINITE-NUMBER",
668
- "severity": "error",
669
- "message": f"Node '{label}' ({node_id}): {str(ve)}",
670
- "node_id": node_id,
671
- })
672
- formatted_val = "0.0"
673
-
674
- user_var = params.get("const_name")
675
- node_var = sanitize_identifier(user_var, f"val_{safe_id}")
676
- code_body_lines.append(f" {node_var} = {formatted_val}")
677
-
678
- # 2. Variable Nodes
679
- elif raw_type in ("core.variable", "variable") or "variable" in raw_type:
680
- user_var = params.get("var_name", "my_var")
681
- node_var = sanitize_identifier(user_var, f"var_{safe_id}")
682
- vtype = params.get("var_type")
683
- default_val = params.get("literal_value", 0)
684
-
685
- if not vtype:
686
- if isinstance(default_val, str) and not default_val.lstrip("-").isdigit():
687
- vtype = "str"
688
- else:
689
- vtype = "int"
690
-
691
- if incoming:
692
- assigned_expr = resolve_input("in_val", resolve_input("in", resolve_input("val", default_val)))
693
- else:
694
- assigned_expr = format_literal(default_val, vtype)
695
-
696
- code_body_lines.append(f" {node_var} = {assigned_expr}")
697
-
698
- # 3. Square Root Node
699
- elif raw_type in ("math.sqrt", "sqrt") or "sqrt" in raw_type:
700
- discovered_modules.add("math")
701
- x_expr = resolve_input("x", resolve_input("val", resolve_input("in", params.get("x", 49.0))))
702
- node_var = f"sqrt_{safe_id}"
703
- rounding = params.get("rounding", "none")
704
-
705
- if rounding == "round":
706
- precision = int(params.get("precision", 2))
707
- code_body_lines.append(f" {node_var} = round(math.sqrt({x_expr}), {precision})")
708
- elif rounding == "floor":
709
- code_body_lines.append(f" {node_var} = math.floor(math.sqrt({x_expr}))")
710
- elif rounding == "ceil":
711
- code_body_lines.append(f" {node_var} = math.ceil(math.sqrt({x_expr}))")
712
- else:
713
- code_body_lines.append(f" {node_var} = math.sqrt({x_expr})")
714
-
715
- # 4. Binary Operators (Arithmetic & Math Add/Sub/Multiply/Divide/Pow)
716
- elif (
717
- raw_type in ("core.binary_op", "binary_op")
718
- or "binary" in raw_type
719
- or raw_type.startswith("math.add")
720
- or raw_type.startswith("math.sub")
721
- or raw_type.startswith("math.multiply")
722
- or raw_type.startswith("math.divide")
723
- or raw_type.startswith("math.pow")
724
- ):
725
- a_expr = resolve_input("a", resolve_input("x", params.get("a", params.get("x", 0))))
726
- b_expr = resolve_input("b", resolve_input("y", params.get("b", params.get("y", 0))))
727
- op = params.get("operator", "+")
728
- if "pow" in raw_type or raw_type.startswith("math.pow"):
729
- discovered_modules.add("math")
730
- node_var = f"res_{safe_id}"
731
- code_body_lines.append(f" {node_var} = math.pow({a_expr}, {b_expr})")
732
- else:
733
- if "add" in raw_type:
734
- op = "+"
735
- elif "sub" in raw_type:
736
- op = "-"
737
- elif "multiply" in raw_type:
738
- op = "*"
739
- elif "divide" in raw_type:
740
- op = "/"
741
-
742
- # Check division by zero
743
- if op in ("/", "//", "%") and b_expr in ("0", "0.0"):
744
- diagnostics.append({
745
- "code": "MATH-E001",
746
- "severity": "error",
747
- "message": f"Division by zero detected in node '{label}' ({node_id})",
748
- "node_id": node_id,
749
- })
750
-
751
- node_var = f"res_{safe_id}"
752
- code_body_lines.append(f" {node_var} = {a_expr} {op} {b_expr}")
753
-
754
- # 5. Comparison Operator
755
- elif raw_type in ("core.compare", "compare") or "compare" in raw_type:
756
- a_expr = resolve_input("a", params.get("a", 0))
757
- b_expr = resolve_input("b", params.get("b", 0))
758
- op = params.get("operator", "==")
759
- node_var = f"cmp_{safe_id}"
760
- code_body_lines.append(f" {node_var} = ({a_expr} {op} {b_expr})")
761
-
762
- # 6. Logical Operator
763
- elif raw_type in ("core.logical_op", "logical_op") or "logical" in raw_type:
764
- op = str(params.get("operator", "and")).lower()
765
- if op == "not":
766
- a_expr = resolve_input("a", params.get("a", True))
767
- node_var = f"logic_{safe_id}"
768
- code_body_lines.append(f" {node_var} = (not {a_expr})")
769
- else:
770
- a_expr = resolve_input("a", params.get("a", True))
771
- b_expr = resolve_input("b", params.get("b", True))
772
- node_var = f"logic_{safe_id}"
773
- code_body_lines.append(f" {node_var} = ({a_expr} {op} {b_expr})")
774
-
775
- # 7. Type Cast
776
- elif raw_type in ("core.type_cast", "type_cast") or "type_cast" in raw_type:
777
- val_expr = resolve_input("value", params.get("value", 0))
778
- target_type = params.get("target_type", "str")
779
- node_var = f"cast_{safe_id}"
780
- code_body_lines.append(f" {node_var} = {target_type}({val_expr})")
781
-
782
- # 8. Conditional Branching (If/Else)
783
- elif raw_type in ("control.if_else", "if_else") or "condition" in raw_type or raw_type == "if":
784
- cond_expr = resolve_input("condition", params.get("condition", True))
785
- raw_then = params.get("default_then", params.get("then_val", "True"))
786
- raw_else = params.get("default_else", params.get("else_val", "False"))
787
- then_expr = resolve_input("then_val", format_literal(raw_then))
788
- else_expr = resolve_input("else_val", format_literal(raw_else))
789
- node_var = f"selected_{safe_id}"
790
- branch_var = f"branch_{safe_id}"
791
- code_body_lines.append(f" if {cond_expr}:")
792
- code_body_lines.append(f" {node_var} = {then_expr}")
793
- code_body_lines.append(f' {branch_var} = "then"')
794
- code_body_lines.append(f" else:")
795
- code_body_lines.append(f" {node_var} = {else_expr}")
796
- code_body_lines.append(f' {branch_var} = "else"')
797
- output_var_map[f"{node_id}:branch_taken"] = branch_var
798
-
799
- # 9. Terminal Print / Output Node
800
- elif raw_type in ("io.print", "print") or "terminal" in raw_type or "output" in raw_type or "display" in raw_type:
801
- val_expr = resolve_input(["value", "val", "in", "x"], params.get("value", ""))
802
- prefix = params.get("prefix", "")
803
- node_var = val_expr
804
- if prefix:
805
- code_body_lines.append(f' print(f"{prefix}{{{val_expr}}}")')
806
- else:
807
- code_body_lines.append(f" print({val_expr})")
808
- output_var_map[f"{node_id}:out"] = node_var
809
- output_var_map[f"{node_id}:then"] = "True"
810
- output_var_map[node_id] = node_var
811
-
812
- # 10. IO Input
813
- elif raw_type in ("io.input", "input"):
814
- default_val = params.get("default_value") if params.get("default_value") is not None else params.get("literal_value")
815
- prompt = params.get("prompt", "")
816
- user_var = params.get("var_name") or params.get("alias")
817
- if user_var:
818
- node_var = sanitize_identifier(user_var, "user_input")
819
- elif clean_lbl and not clean_lbl[0].isdigit() and len(clean_lbl) <= 24:
820
- node_var = clean_lbl if step_counter == 1 else f"{clean_lbl}_{step_counter}"
821
- else:
822
- node_var = "user_input" if step_counter == 1 else f"user_input_{step_counter}"
823
-
824
- if default_val is not None and not prompt:
825
- dtype = params.get("input_type", "float" if isinstance(default_val, float) else "any")
826
- formatted_val = format_literal(default_val, dtype)
827
- code_body_lines.append(f" {node_var} = {formatted_val}")
828
- else:
829
- code_body_lines.append(f' {node_var} = input("{prompt}")')
830
- output_var_map[f"{node_id}:value"] = node_var
831
- output_var_map[f"{node_id}:out"] = node_var
832
- output_var_map[f"{node_id}:output"] = node_var
833
- output_var_map[f"{node_id}:then"] = "True"
834
- output_var_map[node_id] = node_var
835
-
836
- # 10B. Sandboxed File Read Node
837
- elif raw_type in ("file.read", "io.file_read"):
838
- discovered_modules.add("os")
839
- path_val = params.get("path", "data.txt")
840
- path_expr = resolve_input(["file_path", "path"], format_literal(path_val))
841
- encoding_val = params.get("encoding", "utf-8")
842
- mode_val = params.get("mode", "text")
843
- node_var = f"content_{safe_id}"
844
- bytes_var = f"bytes_{safe_id}"
845
- success_var = f"success_{safe_id}"
846
- then_var = f"then_{safe_id}"
847
-
848
- if mode_val == "binary":
849
- code_body_lines.append(f" with open({path_expr}, 'rb') as _f_{safe_id}:")
850
- code_body_lines.append(f" {node_var} = _f_{safe_id}.read()")
851
- else:
852
- code_body_lines.append(f" with open({path_expr}, 'r', encoding='{encoding_val}') as _f_{safe_id}:")
853
- if mode_val == "lines":
854
- code_body_lines.append(f" {node_var} = _f_{safe_id}.readlines()")
855
- elif mode_val == "json":
856
- discovered_modules.add("json")
857
- code_body_lines.append(f" {node_var} = json.load(_f_{safe_id})")
858
- else:
859
- code_body_lines.append(f" {node_var} = _f_{safe_id}.read()")
860
- code_body_lines.append(f" {bytes_var} = len({node_var})")
861
- code_body_lines.append(f" {success_var} = True")
862
- output_var_map[f"{node_id}:content"] = node_var
863
- output_var_map[f"{node_id}:byte_count"] = bytes_var
864
- output_var_map[f"{node_id}:success"] = success_var
865
- output_var_map[f"{node_id}:then"] = "True"
866
- output_var_map[f"{node_id}:out"] = node_var
867
- output_var_map[node_id] = node_var
868
-
869
- # 10C. Sandboxed File Write Node
870
- elif raw_type in ("file.write", "io.file_write"):
871
- discovered_modules.add("os")
872
- path_val = params.get("path", "output.txt")
873
- path_expr = resolve_input(["file_path", "path"], format_literal(path_val))
874
- content_expr = resolve_input(["content", "data", "text"], format_literal(""))
875
- mode_opt = params.get("mode", "write")
876
- write_mode = "a" if mode_opt == "append" else "x" if mode_opt == "exclusive" else "w"
877
- encoding_val = params.get("encoding", "utf-8")
878
- create_dirs = bool(params.get("create_dirs", True))
879
- bytes_var = f"written_{safe_id}"
880
- path_res_var = f"path_{safe_id}"
881
- success_var = f"success_{safe_id}"
882
- then_var = f"then_{safe_id}"
883
-
884
- if create_dirs:
885
- code_body_lines.append(f" if os.path.dirname({path_expr}):")
886
- code_body_lines.append(f" os.makedirs(os.path.dirname({path_expr}), exist_ok=True)")
887
- code_body_lines.append(f" with open({path_expr}, '{write_mode}', encoding='{encoding_val}') as _f_{safe_id}:")
888
- code_body_lines.append(f" _f_{safe_id}.write(str({content_expr}))")
889
- code_body_lines.append(f" {bytes_var} = len(str({content_expr}).encode('{encoding_val}'))")
890
- code_body_lines.append(f" {path_res_var} = {path_expr}")
891
- code_body_lines.append(f" {success_var} = True")
892
- output_var_map[f"{node_id}:bytes_written"] = bytes_var
893
- output_var_map[f"{node_id}:file_path"] = path_res_var
894
- output_var_map[f"{node_id}:success"] = success_var
895
- output_var_map[f"{node_id}:then"] = "True"
896
- output_var_map[node_id] = bytes_var
897
-
898
- # 10D. JSON Parse Node
899
- elif raw_type in ("json.parse", "json_parse", "json.loads"):
900
- discovered_modules.add("json")
901
- text_expr = resolve_input(["text", "json_str", "raw_json"], format_literal(params.get("text", "{}")))
902
- fallback_to_none = bool(params.get("fallback_to_none", False))
903
- strict = bool(params.get("strict", True))
904
- data_var = f"json_data_{safe_id}"
905
- valid_var = f"is_valid_{safe_id}"
906
- err_var = f"err_{safe_id}"
907
- then_var = f"then_{safe_id}"
908
- safe_text_expr = f"({text_expr}.text if hasattr({text_expr}, 'text') else (json.dumps({text_expr}) if isinstance({text_expr}, (dict, list)) else str({text_expr})))"
909
- if fallback_to_none:
910
- code_body_lines.append(f" try:")
911
- code_body_lines.append(f" {data_var} = json.loads({safe_text_expr}, strict={strict})")
912
- code_body_lines.append(f" {valid_var} = True")
913
- code_body_lines.append(f" {err_var} = None")
914
- code_body_lines.append(f" except Exception as _e_{safe_id}:")
915
- code_body_lines.append(f" {data_var} = None")
916
- code_body_lines.append(f" {valid_var} = False")
917
- code_body_lines.append(f" {err_var} = str(_e_{safe_id})")
918
- else:
919
- code_body_lines.append(f" {data_var} = json.loads({safe_text_expr}, strict={strict})")
920
- code_body_lines.append(f" {valid_var} = True")
921
- code_body_lines.append(f" {err_var} = None")
922
- output_var_map[f"{node_id}:data"] = data_var
923
- output_var_map[f"{node_id}:is_valid"] = valid_var
924
- output_var_map[f"{node_id}:error"] = err_var
925
- output_var_map[f"{node_id}:then"] = "True"
926
- output_var_map[f"{node_id}:out"] = data_var
927
- output_var_map[node_id] = data_var
928
-
929
- # 10E. JSON Stringify Node
930
- elif raw_type in ("json.stringify", "json_stringify", "json.dumps"):
931
- discovered_modules.add("json")
932
- data_expr = resolve_input(["data", "obj", "payload"], format_literal(params.get("data", {})))
933
- indent_val = params.get("indent", 2)
934
- indent_arg = "None" if str(indent_val) in ("0", "none", "None") else str(indent_val)
935
- sort_keys = bool(params.get("sort_keys", False))
936
- ensure_ascii = bool(params.get("ensure_ascii", False))
937
- text_var = f"json_str_{safe_id}"
938
- bytes_var = f"json_bytes_{safe_id}"
939
- success_var = f"success_{safe_id}"
940
- then_var = f"then_{safe_id}"
941
- code_body_lines.append(f" {text_var} = json.dumps({data_expr}, indent={indent_arg}, sort_keys={sort_keys}, ensure_ascii={ensure_ascii}, default=str)")
942
- code_body_lines.append(f" {bytes_var} = len({text_var}.encode('utf-8'))")
943
- code_body_lines.append(f" {success_var} = True")
944
- output_var_map[f"{node_id}:text"] = text_var
945
- output_var_map[f"{node_id}:byte_size"] = bytes_var
946
- output_var_map[f"{node_id}:success"] = success_var
947
- output_var_map[f"{node_id}:then"] = "True"
948
- output_var_map[f"{node_id}:out"] = text_var
949
- output_var_map[node_id] = text_var
950
-
951
- # 10F. CSV Parse Node
952
- elif raw_type in ("csv.parse", "csv_parse", "csv.read"):
953
- discovered_modules.add("csv")
954
- discovered_modules.add("io")
955
- text_expr = resolve_input(["text", "csv_data", "raw_csv"], format_literal(params.get("text", "")))
956
- delim = params.get("delimiter", ",")
957
- if delim == "\\t":
958
- delim = "\t"
959
- has_header = bool(params.get("has_header", True))
960
- trim_ws = bool(params.get("trim_whitespace", True))
961
- rows_var = f"csv_rows_{safe_id}"
962
- headers_var = f"csv_headers_{safe_id}"
963
- count_var = f"csv_count_{safe_id}"
964
- success_var = f"success_{safe_id}"
965
- then_var = f"then_{safe_id}"
966
- reader_var = f"_reader_{safe_id}"
967
- f_var = f"_f_{safe_id}"
968
- code_body_lines.append(f" {f_var} = io.StringIO(str({text_expr}))")
969
- if has_header:
970
- code_body_lines.append(f" {reader_var} = csv.DictReader({f_var}, delimiter={format_literal(delim)})")
971
- code_body_lines.append(f" {headers_var} = list({reader_var}.fieldnames or [])")
972
- if trim_ws:
973
- code_body_lines.append(f" {rows_var} = [{{(k.strip() if isinstance(k, str) else k): (v.strip() if isinstance(v, str) else v) for k, v in r.items()}} for r in {reader_var} if any(str(x).strip() for x in r.values())]")
974
- else:
975
- code_body_lines.append(f" {rows_var} = [dict(r) for r in {reader_var} if any(str(x).strip() for x in r.values())]")
976
- else:
977
- code_body_lines.append(f" {headers_var} = []")
978
- code_body_lines.append(f" {reader_var} = csv.reader({f_var}, delimiter={format_literal(delim)})")
979
- if trim_ws:
980
- code_body_lines.append(f" {rows_var} = [[(v.strip() if isinstance(v, str) else v) for v in r] for r in {reader_var} if r and any(str(x).strip() for x in r)]")
981
- else:
982
- code_body_lines.append(f" {rows_var} = [list(r) for r in {reader_var} if r and any(str(x).strip() for x in r)]")
983
- code_body_lines.append(f" {count_var} = len({rows_var})")
984
- code_body_lines.append(f" {success_var} = True")
985
- output_var_map[f"{node_id}:rows"] = rows_var
986
- output_var_map[f"{node_id}:headers"] = headers_var
987
- output_var_map[f"{node_id}:row_count"] = count_var
988
- output_var_map[f"{node_id}:success"] = success_var
989
- output_var_map[f"{node_id}:then"] = "True"
990
- output_var_map[f"{node_id}:out"] = rows_var
991
- output_var_map[node_id] = rows_var
992
-
993
- # 10G. CSV Stringify Node
994
- elif raw_type in ("csv.stringify", "csv_stringify", "csv.write"):
995
- discovered_modules.add("csv")
996
- discovered_modules.add("io")
997
- rows_expr = resolve_input(["rows", "data", "table"], format_literal(params.get("rows", [])))
998
- headers_expr = resolve_input(["headers", "custom_headers"], "None")
999
- delim = params.get("delimiter", ",")
1000
- if delim == "\\t":
1001
- delim = "\t"
1002
- include_header = bool(params.get("include_header", True))
1003
- terminator = params.get("line_terminator", "\n")
1004
- text_var = f"csv_text_{safe_id}"
1005
- count_var = f"csv_count_{safe_id}"
1006
- bytes_var = f"csv_bytes_{safe_id}"
1007
- success_var = f"success_{safe_id}"
1008
- then_var = f"then_{safe_id}"
1009
- f_var = f"_f_{safe_id}"
1010
- w_var = f"_w_{safe_id}"
1011
- code_body_lines.append(f" {f_var} = io.StringIO()")
1012
- code_body_lines.append(f" _rows_{safe_id} = {rows_expr} if isinstance({rows_expr}, (list, tuple)) else [{rows_expr}]")
1013
- code_body_lines.append(f" _hdr_{safe_id} = {headers_expr}")
1014
- code_body_lines.append(f" if _rows_{safe_id} and isinstance(_rows_{safe_id}[0], dict):")
1015
- code_body_lines.append(f" _fields_{safe_id} = list(_hdr_{safe_id}) if _hdr_{safe_id} and isinstance(_hdr_{safe_id}, list) else list(_rows_{safe_id}[0].keys())")
1016
- code_body_lines.append(f" {w_var} = csv.DictWriter({f_var}, fieldnames=_fields_{safe_id}, delimiter={format_literal(delim)}, lineterminator={format_literal(terminator)}, extrasaction='ignore')")
1017
- if include_header:
1018
- code_body_lines.append(f" {w_var}.writeheader()")
1019
- code_body_lines.append(f" for _r in _rows_{safe_id}:")
1020
- code_body_lines.append(f" if isinstance(_r, dict):")
1021
- code_body_lines.append(f" {w_var}.writerow(_r)")
1022
- code_body_lines.append(f" else:")
1023
- code_body_lines.append(f" {w_var} = csv.writer({f_var}, delimiter={format_literal(delim)}, lineterminator={format_literal(terminator)})")
1024
- if include_header:
1025
- code_body_lines.append(f" if _hdr_{safe_id} and isinstance(_hdr_{safe_id}, list):")
1026
- code_body_lines.append(f" {w_var}.writerow(_hdr_{safe_id})")
1027
- code_body_lines.append(f" for _r in _rows_{safe_id}:")
1028
- code_body_lines.append(f" if isinstance(_r, (list, tuple)):")
1029
- code_body_lines.append(f" {w_var}.writerow(_r)")
1030
- code_body_lines.append(f" else:")
1031
- code_body_lines.append(f" {w_var}.writerow([_r])")
1032
- code_body_lines.append(f" {text_var} = {f_var}.getvalue()")
1033
- code_body_lines.append(f" {count_var} = len(_rows_{safe_id})")
1034
- code_body_lines.append(f" {bytes_var} = len({text_var}.encode('utf-8'))")
1035
- code_body_lines.append(f" {success_var} = True")
1036
- output_var_map[f"{node_id}:text"] = text_var
1037
- output_var_map[f"{node_id}:row_count"] = count_var
1038
- output_var_map[f"{node_id}:byte_size"] = bytes_var
1039
- output_var_map[f"{node_id}:success"] = success_var
1040
- output_var_map[f"{node_id}:then"] = "True"
1041
- output_var_map[f"{node_id}:out"] = text_var
1042
- output_var_map[node_id] = text_var
1043
-
1044
- # 11. Control: For Loop
1045
- elif raw_type in ("control.for_loop", "for_loop"):
1046
- loop_var = sanitize_identifier(params.get("loop_var", "i"), "i")
1047
- r_start = params.get("range_start", 0)
1048
- r_stop = params.get("range_stop", 5)
1049
- r_step = params.get("range_step", 1)
1050
- iter_input = resolve_input("iterable", None)
1051
- if iter_input and iter_input != "None":
1052
- iter_expr = iter_input
1053
- else:
1054
- iter_expr = f"range({r_start}, {r_stop}, {r_step})"
1055
- node_var = f"items_{safe_id}"
1056
- code_body_lines.append(f" {node_var} = []")
1057
- code_body_lines.append(f" for {loop_var} in {iter_expr}:")
1058
- code_body_lines.append(f" {node_var}.append({loop_var})")
1059
- output_var_map[f"{node_id}:items"] = node_var
1060
- output_var_map[f"{node_id}:iteration_count"] = f"len({node_var})"
1061
- output_var_map[f"{node_id}:completed"] = "True"
1062
-
1063
- # 12. Control: While Loop
1064
- elif raw_type in ("control.while_loop", "while_loop"):
1065
- init_val_expr = resolve_input("initial_value", params.get("initial_value", 0))
1066
- thresh = params.get("threshold", 5)
1067
- op = params.get("condition_op", "<")
1068
- step = params.get("step", 1)
1069
- max_iters = params.get("max_iterations", 10)
1070
- node_var = f"val_{safe_id}"
1071
- iter_var = f"iters_{safe_id}"
1072
- code_body_lines.append(f" {node_var} = {init_val_expr}")
1073
- code_body_lines.append(f" {iter_var} = 0")
1074
- code_body_lines.append(f" while {node_var} {op} {thresh} and {iter_var} < {max_iters}:")
1075
- code_body_lines.append(f" {node_var} += {step}")
1076
- code_body_lines.append(f" {iter_var} += 1")
1077
- output_var_map[f"{node_id}:final_value"] = node_var
1078
- output_var_map[f"{node_id}:iterations"] = iter_var
1079
- output_var_map[f"{node_id}:completed"] = "True"
1080
-
1081
- # 13. Control: Loop Control (break / continue)
1082
- elif raw_type in ("control.loop_control", "loop_control"):
1083
- act = str(params.get("action", "break")).lower()
1084
- if act not in ("break", "continue", "pass"):
1085
- act = "break"
1086
- node_var = f"signal_{safe_id}"
1087
- code_body_lines.append(f" {node_var} = {format_literal(act)}")
1088
- output_var_map[f"{node_id}:signal"] = node_var
1089
- output_var_map[f"{node_id}:action_taken"] = format_literal(act)
1090
- output_var_map[node_id] = node_var
1091
-
1092
- # 14. Data: List
1093
- elif raw_type in ("data.list", "list"):
1094
- raw_initial = params.get("initial_items", "[]")
1095
- if isinstance(raw_initial, str):
1096
- import ast
1097
- try:
1098
- raw_initial = ast.literal_eval(raw_initial)
1099
- except Exception:
1100
- pass
1101
- items_expr = resolve_input("items", raw_initial)
1102
- node_var = f"list_{safe_id}"
1103
- code_body_lines.append(f" {node_var} = {items_expr}")
1104
- output_var_map[f"{node_id}:result"] = node_var
1105
- output_var_map[f"{node_id}:list_out"] = node_var
1106
- output_var_map[f"{node_id}:out"] = node_var
1107
- output_var_map[f"{node_id}:length"] = f"len({node_var})"
1108
- output_var_map[f"{node_id}:count"] = f"len({node_var})"
1109
- output_var_map[node_id] = node_var
1110
-
1111
- # 15. Data: Dict
1112
- elif raw_type in ("data.dict", "dict"):
1113
- raw_initial = params.get("initial_pairs") or params.get("initial_dict", "{}")
1114
- if isinstance(raw_initial, str):
1115
- import ast
1116
- try:
1117
- raw_initial = ast.literal_eval(raw_initial)
1118
- except Exception:
1119
- pass
1120
- connected = resolve_input("dict_in", "None")
1121
- if connected != "None":
1122
- dict_expr = connected
1123
- else:
1124
- dict_expr = repr(raw_initial) if isinstance(raw_initial, dict) else format_literal(raw_initial)
1125
- node_var = f"dict_{safe_id}"
1126
- code_body_lines.append(f" {node_var} = {dict_expr}")
1127
- output_var_map[f"{node_id}:result"] = node_var
1128
- output_var_map[f"{node_id}:dict_out"] = node_var
1129
- output_var_map[f"{node_id}:out"] = node_var
1130
- output_var_map[node_id] = node_var
1131
-
1132
- # 16. Data: Tuple
1133
- elif raw_type in ("data.tuple", "tuple"):
1134
- raw_initial = params.get("items") or params.get("initial_items", "()")
1135
- if isinstance(raw_initial, str):
1136
- import ast
1137
- try:
1138
- raw_initial = ast.literal_eval(raw_initial)
1139
- except Exception:
1140
- pass
1141
- tuple_expr = resolve_input("items", raw_initial)
1142
- node_var = f"tuple_{safe_id}"
1143
- code_body_lines.append(f" {node_var} = tuple({tuple_expr})")
1144
- output_var_map[f"{node_id}:result"] = node_var
1145
- output_var_map[f"{node_id}:tuple_out"] = node_var
1146
- output_var_map[f"{node_id}:out"] = node_var
1147
- output_var_map[node_id] = node_var
1148
-
1149
- # 17. Data: Set
1150
- elif raw_type in ("data.set", "set"):
1151
- raw_initial = params.get("initial_items") or params.get("initial_elements", "[]")
1152
- if isinstance(raw_initial, str):
1153
- import ast
1154
- try:
1155
- raw_initial = ast.literal_eval(raw_initial)
1156
- except Exception:
1157
- pass
1158
- elements_expr = resolve_input("items", resolve_input("initial_elements", raw_initial))
1159
- node_var = f"set_{safe_id}"
1160
- code_body_lines.append(f" {node_var} = set({elements_expr})")
1161
- output_var_map[f"{node_id}:result"] = node_var
1162
- output_var_map[f"{node_id}:set_out"] = node_var
1163
- output_var_map[f"{node_id}:out"] = node_var
1164
- output_var_map[node_id] = node_var
1165
-
1166
- # 18. Function: Def
1167
- elif raw_type in ("function.def", "func_def"):
1168
- fn_name = sanitize_identifier(params.get("func_name", "my_func"), "my_func")
1169
- fn_params = params.get("params_list", "x")
1170
- fn_expr = params.get("return_expr", "x")
1171
- node_var = fn_name
1172
- code_body_lines.append(f" def {fn_name}({fn_params}):")
1173
- code_body_lines.append(f" return {fn_expr}")
1174
-
1175
- # 19. Function: Call
1176
- elif raw_type in ("function.call", "func_call"):
1177
- fn_target = resolve_input("callable_fn", params.get("func_name", "my_func"))
1178
- has_arg_wire = any(str(inc.get("target_handle") or inc.get("targetHandle") or "").lower() in ("arg", "args", "arg_1") for inc in incoming)
1179
- has_arg_literal = "arg" in params or "arg_val" in params or "arg" in node_inputs or "arg" in data
1180
- if not has_arg_wire and not has_arg_literal:
1181
- diagnostics.append({
1182
- "code": "ERR-ARG-MISSING",
1183
- "severity": "error",
1184
- "message": f"Function call on node '{label}' ({node_id}) port 'arg' is missing required argument.",
1185
- "node_id": node_id,
1186
- "port_id": "arg"
1187
- })
1188
- fn_arg = resolve_input("arg", params.get("arg", "None"))
1189
- node_var = f"call_{safe_id}"
1190
- code_body_lines.append(f" {node_var} = {fn_target}({fn_arg})")
1191
-
1192
- # 20. Control: Try / Except
1193
- elif raw_type in ("control.try_except", "try_except"):
1194
- try_val = resolve_input("try_val", "None")
1195
- fallback_val = resolve_input(["fallback_val", "default_fallback"], format_literal(params.get("default_fallback", "safe_fallback")))
1196
- exc = params.get("expected_exception", "Exception")
1197
- node_var = f"try_res_{safe_id}"
1198
- has_err_var = f"has_err_{safe_id}"
1199
- err_msg_var = f"err_msg_{safe_id}"
1200
- code_body_lines.append(f" {has_err_var} = False")
1201
- code_body_lines.append(f' {err_msg_var} = ""')
1202
- code_body_lines.append(f" try:")
1203
- if try_val == "None":
1204
- code_body_lines.append(f" raise ValueError('No try value provided')")
1205
- else:
1206
- code_body_lines.append(f" {node_var} = {try_val}")
1207
- code_body_lines.append(f" if {node_var} is None:")
1208
- code_body_lines.append(f" raise ValueError('Try value evaluated to None')")
1209
- code_body_lines.append(f" except {exc} as e:")
1210
- code_body_lines.append(f" {node_var} = {fallback_val}")
1211
- code_body_lines.append(f" {has_err_var} = True")
1212
- code_body_lines.append(f" {err_msg_var} = str(e)")
1213
- output_var_map[f"{node_id}:result"] = node_var
1214
- output_var_map[f"{node_id}:has_error"] = has_err_var
1215
- output_var_map[f"{node_id}:caught_error"] = err_msg_var
1216
-
1217
- # 20B. Math Functions (abs, round, floor, ceil, min, max)
1218
- elif raw_type == "math.abs":
1219
- x_expr = resolve_input("x", params.get("x", 0.0))
1220
- node_var = f"abs_{safe_id}"
1221
- code_body_lines.append(f" {node_var} = abs({x_expr})")
1222
- elif raw_type == "math.round":
1223
- x_expr = resolve_input("x", params.get("x", 0.0))
1224
- prec = int(params.get("precision", 0))
1225
- node_var = f"round_{safe_id}"
1226
- code_body_lines.append(f" {node_var} = round({x_expr}, {prec})")
1227
- elif raw_type in ("math.floor", "math.ceil"):
1228
- discovered_modules.add("math")
1229
- x_expr = resolve_input("x", params.get("x", 0.0))
1230
- fn = "floor" if "floor" in raw_type else "ceil"
1231
- node_var = f"{fn}_{safe_id}"
1232
- code_body_lines.append(f" {node_var} = math.{fn}({x_expr})")
1233
- elif raw_type in ("math.min", "math.max"):
1234
- a_expr = resolve_input("a", params.get("a", 0.0))
1235
- b_expr = resolve_input("b", params.get("b", 0.0))
1236
- fn = "min" if "min" in raw_type else "max"
1237
- node_var = f"{fn}_{safe_id}"
1238
- code_body_lines.append(f" {node_var} = {fn}({a_expr}, {b_expr})")
1239
-
1240
- # 20C. String Operations (transform, concat, split)
1241
- elif raw_type == "string.transform":
1242
- text_expr = resolve_input(["text", "text_in"], format_literal(params.get("text_in", params.get("text", ""))))
1243
- op = params.get("operation", "upper")
1244
- node_var = f"str_{safe_id}"
1245
- code_body_lines.append(f" {node_var} = {text_expr}.{op}()")
1246
- output_var_map[f"{node_id}:text_out"] = node_var
1247
- output_var_map[f"{node_id}:result"] = node_var
1248
- output_var_map[f"{node_id}:out"] = node_var
1249
- output_var_map[f"{node_id}:length"] = f"len({node_var})"
1250
- output_var_map[node_id] = node_var
1251
- elif raw_type == "string.concat":
1252
- a_expr = resolve_input("a", format_literal(params.get("a", "")))
1253
- b_expr = resolve_input("b", format_literal(params.get("b", "")))
1254
- sep = params.get("separator", "")
1255
- node_var = f"concat_{safe_id}"
1256
- if sep:
1257
- code_body_lines.append(f" {node_var} = str({a_expr}) + {format_literal(sep)} + str({b_expr})")
1258
- else:
1259
- code_body_lines.append(f" {node_var} = str({a_expr}) + str({b_expr})")
1260
- output_var_map[f"{node_id}:length"] = f"len({node_var})"
1261
- elif raw_type == "string.split":
1262
- text_expr = resolve_input("text", format_literal(params.get("text", "")))
1263
- delim = params.get("delimiter", " ")
1264
- node_var = f"split_{safe_id}"
1265
- code_body_lines.append(f" {node_var} = str({text_expr}).split({format_literal(delim)})")
1266
- output_var_map[f"{node_id}:items"] = node_var
1267
- output_var_map[f"{node_id}:count"] = f"len({node_var})"
1268
-
1269
- # 20D. Object-Oriented Programming (OOP)
1270
- elif raw_type in ("oop.class_def", "class_def"):
1271
- cname = sanitize_identifier(params.get("class_name", "Student"))
1272
- attrs_raw = params.get("attributes", "name, score")
1273
- attr_list = [sanitize_identifier(a.strip()) for a in str(attrs_raw).split(",") if a.strip()]
1274
- attrs_sig = ", ".join(attr_list)
1275
- docstring = params.get("docstring", "Class definition")
1276
- node_var = f"cls_{cname}_{safe_id}"
1277
- code_body_lines.append(f" class {cname}:")
1278
- code_body_lines.append(f" \"\"\"{docstring}\"\"\"")
1279
- code_body_lines.append(f" def __init__(self, {attrs_sig}):")
1280
- if attr_list:
1281
- for a in attr_list:
1282
- code_body_lines.append(f" self.{a} = {a}")
1283
- else:
1284
- code_body_lines.append(f" pass")
1285
- code_body_lines.append(f" {node_var} = {cname}")
1286
- output_var_map[f"{node_id}:class_ref"] = node_var
1287
- output_var_map[f"{node_id}:class_name"] = format_literal(cname)
1288
- elif raw_type in ("oop.instance", "instance"):
1289
- cname = params.get("class_name", "Student")
1290
- cls_target = resolve_input("class_in", cname)
1291
- arg1 = resolve_input("arg_1", format_literal(params.get("arg_1_val", "Alice")))
1292
- arg2 = resolve_input("arg_2", format_literal(params.get("arg_2_val", 85)))
1293
- node_var = f"inst_{safe_id}"
1294
- code_body_lines.append(f" {node_var} = {cls_target}({arg1}, {arg2})")
1295
- output_var_map[f"{node_id}:instance"] = node_var
1296
- elif raw_type in ("oop.method_call", "method_call"):
1297
- inst_target = resolve_input("instance_in", "self")
1298
- mname = sanitize_identifier(params.get("method_name", "add_bonus"))
1299
- arg_val = resolve_input("arg", format_literal(params.get("default_arg", 15)))
1300
- attr = params.get("attribute_to_modify", "score")
1301
- op = params.get("operation", "increment")
1302
- node_var = f"res_{safe_id}"
1303
- if op == "increment":
1304
- code_body_lines.append(f" if hasattr({inst_target}, '{mname}'):")
1305
- code_body_lines.append(f" {node_var} = {inst_target}.{mname}({arg_val})")
1306
- code_body_lines.append(f" else:")
1307
- code_body_lines.append(f" setattr({inst_target}, '{attr}', getattr({inst_target}, '{attr}', 0) + {arg_val})")
1308
- code_body_lines.append(f" {node_var} = getattr({inst_target}, '{attr}')")
1309
- elif op == "set":
1310
- code_body_lines.append(f" setattr({inst_target}, '{attr}', {arg_val})")
1311
- code_body_lines.append(f" {node_var} = {arg_val}")
1312
- else:
1313
- code_body_lines.append(f" {node_var} = getattr({inst_target}, '{attr}', None)")
1314
- output_var_map[f"{node_id}:instance_out"] = inst_target
1315
-
1316
- # 20E. Standard Module Import (lang.import)
1317
- elif (
1318
- raw_type in ("lang.import", "module.import", "core.import", "alli.lang.import", "alli.primitive.import")
1319
- or raw_type == "lang.import"
1320
- or (raw_type.endswith(".import") and not raw_type.startswith("agent."))
1321
- ):
1322
- mod = str(params.get("module_name") or "json").strip().lower()
1323
- if mod in BANNED_IMPORTS:
1324
- diagnostics.append({
1325
- "code": "CODEGEN-BANNED-MODULE",
1326
- "severity": "error",
1327
- "message": f"Internal or banned module '{mod}' cannot be imported in generated code.",
1328
- "node_id": node_id,
1329
- })
1330
- else:
1331
- discovered_modules.add(mod)
1332
- if mod not in STANDARD_LIBS:
1333
- discovered_requirements.add(mod)
1334
-
1335
- func_name = str(params.get("function_name") or params.get("symbol") or "loads").strip()
1336
- node_var = f"res_{safe_id}"
1337
-
1338
- default_in = params.get("default_input")
1339
- if default_in is None:
1340
- default_in = '{"name": "Alice", "score": 95}' if mod == "json" else (16 if func_name == "sqrt" else "None")
1341
-
1342
- in_expr = resolve_input("data_in", resolve_input("in", resolve_input("payload", format_literal(default_in))))
1343
-
1344
- if func_name:
1345
- if func_name == "now" and mod == "datetime":
1346
- code_body_lines.append(f" {node_var} = datetime.datetime.now().isoformat()")
1347
- elif in_expr and in_expr != "None":
1348
- code_body_lines.append(f" {node_var} = {mod}.{func_name}({in_expr})")
1349
- else:
1350
- code_body_lines.append(f" {node_var} = {mod}.{func_name}()")
1351
- else:
1352
- node_var = f"mod_{safe_id}"
1353
- code_body_lines.append(f" {node_var} = {mod}")
1354
-
1355
- output_var_map[f"{node_id}:result"] = node_var
1356
- output_var_map[f"{node_id}:val"] = node_var
1357
- output_var_map[f"{node_id}:out"] = node_var
1358
- output_var_map[f"{node_id}:module_ref"] = format_literal(mod)
1359
- output_var_map[node_id] = node_var
1360
-
1361
- # 21. Standard Library or Third-Party Module Call (e.g. math.sin, json.dumps, random.randint)
1362
- elif "." in raw_type and not any(raw_type.startswith(f"{p}.") for p in BANNED_IMPORTS):
1363
- parts = raw_type.split(".")
1364
- mod = parts[0]
1365
- if mod in BANNED_IMPORTS:
1366
- diagnostics.append({
1367
- "code": "CODEGEN-BANNED-MODULE",
1368
- "severity": "error",
1369
- "message": f"Internal or banned module '{mod}' cannot be imported in generated code.",
1370
- "node_id": node_id,
1371
- })
1372
- else:
1373
- discovered_modules.add(mod)
1374
- if mod not in STANDARD_LIBS:
1375
- discovered_requirements.add(mod)
1376
-
1377
- node_var = f"{parts[-1]}_{safe_id}"
1378
- arg_list = []
1379
- if incoming:
1380
- for inc in incoming:
1381
- src_id = str(inc.get("source") or inc.get("from_node"))
1382
- src_h = str(inc.get("source_handle") or inc.get("sourceHandle") or "")
1383
- val = output_var_map.get(f"{src_id}:{src_h}") or output_var_map.get(src_id) or "None"
1384
- arg_list.append(val)
1385
- else:
1386
- for k, v in params.items():
1387
- if k not in ["label", "node_type", "nodeType"]:
1388
- arg_list.append(format_literal(v))
1389
-
1390
- args_str = ", ".join(arg_list)
1391
- code_body_lines.append(f" {node_var} = {raw_type}({args_str})")
1392
-
1393
- # 22. Generic Fallback
1394
- else:
1395
- user_var = params.get("var_name")
1396
- node_var = sanitize_identifier(user_var, f"step_{step_counter}_{safe_id}")
1397
- if incoming:
1398
- first_arg = resolve_input("in", resolve_input("value", "None"))
1399
- code_body_lines.append(f" {node_var} = {first_arg}")
1400
- else:
1401
- val = params.get("value", params.get("literal_value", "None"))
1402
- code_body_lines.append(f" {node_var} = {format_literal(val)}")
1403
-
1404
- # Register output variable mappings
1405
- if node_var:
1406
- output_var_map[node_id] = node_var
1407
- output_var_map[f"{node_id}:result"] = node_var
1408
- output_var_map[f"{node_id}:out"] = node_var
1409
- output_var_map[f"{node_id}:val"] = node_var
1410
- output_var_map[f"{node_id}:value"] = node_var
1411
- output_var_map[f"{node_id}:out_val"] = node_var
1412
- if schema and hasattr(schema, "outputs"):
1413
- for out in schema.outputs:
1414
- out_id = getattr(out, "id", None) or (out.get("id") if isinstance(out, dict) else None)
1415
- if out_id and f"{node_id}:{out_id}" not in output_var_map:
1416
- output_var_map[f"{node_id}:{out_id}"] = node_var
1417
- last_return_var = node_var
1418
-
1419
- end_step_line = len(code_body_lines)
1420
- source_map.append({
1421
- "node_id": node_id,
1422
- "line_start": start_step_line,
1423
- "line_end": end_step_line,
1424
- "target_var": node_var,
1425
- "statement_label": label,
1426
- })
1427
- step_counter += 1
1428
-
1429
- # ─── 7. ASSEMBLE CLEAN PEP 8 PYTHON SCRIPT ────────────────────────────────
1430
- file_lines = [
1431
- '"""',
1432
- f'Workflow Pipeline: {name}',
1433
- 'Auto-generated by ALLI (Advanced Logic & Linked Intelligence)',
1434
- '"""',
1435
- "",
1436
- ]
1437
-
1438
- # Partition and sort imports
1439
- std_imports = sorted([m for m in discovered_modules if m in STANDARD_LIBS and m not in BANNED_IMPORTS])
1440
- ext_imports = sorted([m for m in discovered_modules if m not in STANDARD_LIBS and m not in BANNED_IMPORTS])
1441
-
1442
- for m in std_imports:
1443
- file_lines.append(f"import {m}")
1444
- if std_imports and ext_imports:
1445
- file_lines.append("")
1446
- for m in ext_imports:
1447
- file_lines.append(f"import {m}")
1448
-
1449
- if discovered_modules:
1450
- file_lines.append("")
1451
- file_lines.append("")
1452
- else:
1453
- file_lines.append("")
1454
-
1455
- file_lines.append("def main():")
1456
- file_lines.append(' """Execute the compiled DAG pipeline steps."""')
1457
- if "math" in discovered_modules:
1458
- file_lines.append(" import math")
1459
- file_lines.extend(code_body_lines)
1460
- file_lines.append("")
1461
- if last_return_var:
1462
- file_lines.append(f" return {last_return_var}")
1463
- else:
1464
- file_lines.append(" return None")
1465
- file_lines.append("")
1466
- file_lines.append("")
1467
- file_lines.append('if __name__ == "__main__":')
1468
- file_lines.append(" main()")
1469
- file_lines.append("")
1470
-
1471
- python_code = "\n".join(file_lines)
1472
-
1473
- # ─── 8. GENERATE REQUIREMENTS.TXT & DOCKERFILE ────────────────────────────
1474
- clean_reqs = sorted([r for r in discovered_requirements if r not in STANDARD_LIBS and r not in BANNED_IMPORTS])
1475
- if clean_reqs:
1476
- req_lines = []
1477
- for r in clean_reqs:
1478
- if any(op in r for op in ("==", ">=", "<=", "~=", "!=")):
1479
- req_lines.append(r)
1480
- elif r.lower() in PINNED_PACKAGE_VERSIONS:
1481
- req_lines.append(f"{r}=={PINNED_PACKAGE_VERSIONS[r.lower()]}")
1482
- elif pin_exact:
1483
- req_lines.append(f"{r}==0.1.0")
1484
- else:
1485
- req_lines.append(f"{r}>=0.1.0")
1486
- requirements_txt = "\n".join(req_lines) + "\n"
1487
- else:
1488
- requirements_txt = "# Standard library only. No external pip packages needed.\n"
1489
-
1490
- dockerfile = (
1491
- "FROM python:3.12-slim\n"
1492
- "WORKDIR /app\n"
1493
- "COPY requirements.txt .\n"
1494
- "RUN pip install --no-cache-dir -r requirements.txt\n"
1495
- "COPY . .\n"
1496
- "RUN useradd -m appuser && chown -R appuser:appuser /app\n"
1497
- "USER appuser\n"
1498
- f'CMD ["python", "{clean_filename}"]\n'
1499
- )
1500
-
1501
- # ─── 9. AST VALIDATION & FINAL CONTRACT ENFORCEMENT ───────────────────────
1502
- is_valid_ast, ast_err = validate_generated_ast(python_code, check_security=True)
1503
- security_diags = inspect_generated_ast_security(python_code)
1504
- for s_diag in security_diags:
1505
- diagnostics.append(s_diag)
1506
-
1507
- if not is_valid_ast and not security_diags:
1508
- diagnostics.append({
1509
- "code": "SYNTAX-001",
1510
- "severity": "error",
1511
- "message": ast_err or "Synthesized Python code failed AST parse validation",
1512
- })
1513
-
1514
- has_errors = not is_valid_ast or len([d for d in diagnostics if d.get("severity") == "error"]) > 0
1515
- if has_errors:
1516
- python_code = ""
1517
-
1518
- codegen_hash = compute_codegen_hash({
1519
- "python_code": python_code,
1520
- "code": python_code,
1521
- "requirements_txt": requirements_txt,
1522
- "dockerfile": dockerfile,
1523
- })
1524
-
1525
- return {
1526
- "success": is_valid_ast and len([d for d in diagnostics if d.get("severity") == "error"]) == 0,
1527
- "python_code": python_code,
1528
- "code": python_code,
1529
- "requirements_txt": requirements_txt,
1530
- "dockerfile": dockerfile,
1531
- "filename": clean_filename,
1532
- "dependencies": clean_reqs,
1533
- "execution_order": ordered_ids,
1534
- "diagnostics": diagnostics,
1535
- "source_map": source_map,
1536
- "generator_version": "1.0.0",
1537
- "graph_fingerprint": graph_fingerprint,
1538
- "metadata": {
1539
- "hash": codegen_hash,
1540
- "ast_valid": is_valid_ast,
1541
- "node_count": len(ordered_ids),
1542
- "step_count": step_counter - 1,
1543
- "discovered_modules": sorted(list(discovered_modules)),
1544
- },
1545
- }
1546
 
1547
 
1548
  def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
@@ -1550,40 +234,15 @@ def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
1550
  try:
1551
  body_raw = event.get("body", "{}")
1552
  body = json.loads(body_raw) if isinstance(body_raw, str) else body_raw
1553
-
1554
- nodes = body.get("nodes", [])
1555
- edges = body.get("edges", [])
1556
- execution_order = body.get("execution_order")
1557
- name = body.get("name", "alli_workflow")
1558
- dependencies = body.get("dependencies", [])
1559
- strict_validation = bool(body.get("strict_validation", False))
1560
- pin_exact = bool(body.get("pin_exact", True))
1561
-
1562
  result = generate_code_logic(
1563
- nodes=nodes,
1564
- edges=edges,
1565
- execution_order=execution_order,
1566
- graph_name=name,
1567
- dependencies=dependencies,
1568
- strict_validation=strict_validation,
1569
- pin_exact=pin_exact,
1570
  )
1571
-
1572
- return {
1573
- "statusCode": 200,
1574
- "headers": {
1575
- "Content-Type": "application/json",
1576
- "Access-Control-Allow-Origin": "*",
1577
- "Access-Control-Allow-Methods": "POST, OPTIONS",
1578
- },
1579
- "body": json.dumps(result),
1580
- }
1581
  except Exception as err:
1582
- return {
1583
- "statusCode": 500,
1584
- "headers": {
1585
- "Content-Type": "application/json",
1586
- "Access-Control-Allow-Origin": "*",
1587
- },
1588
- "body": json.dumps({"error": str(err)}),
1589
- }
 
1
  """
2
+ ALLI Code Generator Service (alli-codegen) -- Phase 7 Facade
3
  Synthesizes deterministic, standalone, PEP 8-compliant Python scripts,
4
  requirements.txt, and Dockerfiles directly from validated visual graphs.
 
 
 
 
 
 
 
 
 
5
  """
6
 
7
+ from collections import defaultdict
8
  import json
 
9
  import re
10
+ from typing import Any, Dict, List, Optional
 
 
 
 
 
 
11
 
12
  from language_core.codegen_contracts import (
 
13
  BANNED_IMPORTS,
14
  PINNED_PACKAGE_VERSIONS,
15
+ STANDARD_LIBS,
 
 
 
 
16
  format_literal,
17
+ sanitize_identifier,
18
+ )
19
+ from language_core.execution_session import compute_graph_hash
20
+ from language_core.node_schema import default_registry, get_effective_node_schema
21
+ from lambdas.codegen.assembler import assemble_python_artifacts
22
+ from lambdas.codegen.context import (
23
+ CodegenContext,
24
+ NodeCodegenCtx,
25
+ build_empty_workflow_response,
26
+ compute_topological_order,
27
+ run_preflight_codegen_checks,
28
+ )
29
+ from lambdas.codegen.emitters_control_oop import (
30
+ emit_control_and_functions_node,
31
+ emit_module_and_fallback_node,
32
+ emit_string_and_oop_node,
33
+ )
34
+ from lambdas.codegen.emitters_core import (
35
+ emit_category_node,
36
+ emit_essentials_node,
37
+ emit_values_math_io_node,
38
+ )
39
+ from lambdas.codegen.emitters_files_data import (
40
+ emit_collection_node,
41
+ emit_file_and_structured_node,
42
  )
43
 
44
  # Backwards compatible internal aliases
45
  _sanitize_identifier = sanitize_identifier
46
  _format_literal = format_literal
47
 
48
+ EMITTER_PIPELINE = (
49
+ emit_category_node,
50
+ emit_essentials_node,
51
+ emit_values_math_io_node,
52
+ emit_file_and_structured_node,
53
+ emit_control_and_functions_node,
54
+ emit_collection_node,
55
+ emit_string_and_oop_node,
56
+ emit_module_and_fallback_node,
57
+ )
58
+
59
+
60
+ def _register_node_outputs(nctx: NodeCodegenCtx, node_var: Optional[str]) -> None:
61
+ if not node_var:
62
+ return
63
+ ovm = nctx.cg.output_var_map
64
+ nid = nctx.node_id
65
+ ovm[nid] = node_var
66
+ for port in ("result", "out", "val", "value", "out_val"):
67
+ ovm[f"{nid}:{port}"] = node_var
68
+ if nctx.schema and hasattr(nctx.schema, "outputs"):
69
+ for out in nctx.schema.outputs:
70
+ out_id = getattr(out, "id", None) or (out.get("id") if isinstance(out, dict) else None)
71
+ if out_id and f"{nid}:{out_id}" not in ovm:
72
+ ovm[f"{nid}:{out_id}"] = node_var
73
+ nctx.cg.last_return_var = node_var
74
+
75
 
76
  def generate_code_logic(
77
  nodes: Optional[List[Dict[str, Any]]] = None,
 
84
  strict_validation: bool = False,
85
  pin_exact: bool = True,
86
  ) -> Dict[str, Any]:
87
+ """Authoritative Python code generation engine (Phase 7)."""
 
 
 
 
 
88
  from language_core.workflow_migration import migrate_workflow
89
+
90
  if isinstance(nodes, dict) and "nodes" in nodes and graph is None:
91
+ graph, nodes = nodes, None
 
92
 
93
  if graph and isinstance(graph, dict):
94
  graph = migrate_workflow(graph)
 
103
  raw_edges = migrated.get("edges") or []
104
  name = graph_name or "alli_workflow"
105
  deps = dependencies or []
106
+ graph_obj = {"name": name, "nodes": raw_nodes, "edges": raw_edges, "dependencies": deps}
 
 
 
 
 
107
 
108
+ clean_name = sanitize_identifier(name.lower().replace(" ", "_"), "alli_workflow")
109
+ clean_filename = f"{clean_name}.py"
110
  graph_fingerprint = compute_graph_hash(graph_obj)
111
 
 
112
  if not raw_nodes:
113
+ return build_empty_workflow_response(name, clean_filename, graph_fingerprint)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
+ diagnostics = run_preflight_codegen_checks(raw_nodes, graph_obj, validate, strict_validation)
116
+ blocking = [d for d in diagnostics if d.get("severity") == "error"]
117
+ if blocking:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  return {
119
+ "success": False, "python_code": "", "code": "", "requirements_txt": "", "dockerfile": "",
120
+ "filename": clean_filename, "dependencies": [], "execution_order": [],
121
+ "diagnostics": diagnostics, "source_map": [], "generator_version": "1.0.0",
 
 
 
 
 
 
 
 
122
  "graph_fingerprint": graph_fingerprint,
123
+ "metadata": {"valid": False, "error_count": len(blocking), "ast_valid": False, "node_count": len(raw_nodes)},
 
 
 
 
 
124
  }
125
 
126
+ filtered_nodes = [
 
127
  n for n in raw_nodes
128
  if str(n.get("node_type") or n.get("type") or n.get("data", {}).get("node_type") or "")
129
  not in ("system.workflow_group", "workflow_group", "workflowGroup")
130
  ]
131
+ filtered_edges = [
132
  e for e in raw_edges
133
  if not str(e.get("source", "")).startswith("group_") and not str(e.get("target", "")).startswith("group_")
134
  ]
135
+ node_map = {str(n["id"]): n for n in filtered_nodes if n.get("id")}
136
+ ordered_ids, has_cycle = compute_topological_order(node_map, filtered_edges, execution_order)
137
+ if has_cycle:
138
+ return {
139
+ "success": False, "python_code": "", "requirements_txt": "", "dockerfile": "",
140
+ "filename": clean_filename, "dependencies": [], "execution_order": [],
141
+ "diagnostics": [{"code": "DAG-001", "severity": "error", "message": "Cycle detected during code generation topological sequencing.", "fix_suggestion": "Remove circular feedback wires between procedural nodes."}],
142
+ "source_map": [], "generator_version": "1.0.0", "graph_fingerprint": graph_fingerprint,
143
+ "metadata": {"valid": False, "has_cycles": True},
144
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
+ incoming_edges_map: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
147
+ for edge in filtered_edges:
148
+ incoming_edges_map[str(edge.get("target") or edge.get("to_node") or "")].append(edge)
149
 
150
+ cg = CodegenContext(diagnostics=diagnostics)
151
  for dep in deps:
152
  clean_dep = str(dep).strip()
153
  if clean_dep and clean_dep not in STANDARD_LIBS and clean_dep not in BANNED_IMPORTS:
154
+ cg.discovered_requirements.add(clean_dep)
155
+
156
+ def _emit_single_node(
157
+ node_id: str,
158
+ extra_indent: int = 0,
159
+ incoming_override: Optional[List[Dict[str, Any]]] = None,
160
+ build_only: bool = False,
161
+ ) -> Tuple[Optional[NodeCodegenCtx], Optional[str]]:
 
 
 
 
 
162
  node = node_map.get(node_id)
163
  if not node:
164
+ return None, None
165
+ raw_type = str(node.get("node_type") or node.get("type") or node.get("data", {}).get("node_type") or node.get("data", {}).get("nodeType") or "").strip().lower()
 
 
 
 
 
 
 
 
166
  data = node.get("data", {}) or {}
167
+ params = dict(node.get("parameters") if isinstance(node.get("parameters"), dict) else {})
 
168
  node_inputs = node.get("inputs") if isinstance(node.get("inputs"), dict) else (data.get("inputs") if isinstance(data.get("inputs"), dict) else {})
 
169
  label = str(data.get("label") or node.get("label") or raw_type or node_id)
170
+ clean_lbl = re.sub(r"_+", "_", re.sub(r"[^a-zA-Z0-9_]", "_", label.lower()).strip("_"))
 
 
171
  if str(node_id).startswith("alli_primitive_") or len(str(node_id)) > 20:
172
+ safe_id = f"{clean_lbl}_{cg.step_counter}" if (clean_lbl and not clean_lbl[0].isdigit() and len(clean_lbl) <= 24) else f"step_{cg.step_counter}"
 
 
 
173
  else:
174
+ safe_id = sanitize_identifier(node_id.replace("-", "_"), f"node_{cg.step_counter}")
 
 
 
 
 
 
 
 
175
 
176
+ schema = get_effective_node_schema(raw_type)
177
  cap_states = (
178
  (schema.capability_states if schema and hasattr(schema, "capability_states") else None)
179
+ or (schema.verification.capability_states if schema and getattr(schema, "verification", None) else None)
180
+ or data.get("capability_states") or params.get("capability_states") or node.get("capability_states")
 
 
181
  or (data.get("verification") or {}).get("capability_states")
182
  )
183
  if cap_states and cap_states.get("codegen_supported") is False:
184
+ cg.diagnostics.append({"code": "CODEGEN_UNSUPPORTED", "severity": "error", "message": f"Code generation is unsupported for node '{label}' ({node_id}) of type '{raw_type}'.", "node_id": node_id})
 
 
 
 
 
185
 
186
  if schema and getattr(schema, "codegen", None):
187
  for imp in schema.codegen.imports:
188
  clean_imp = str(imp).strip()
189
  if clean_imp not in BANNED_IMPORTS:
190
+ cg.discovered_modules.add(clean_imp)
191
  if clean_imp not in STANDARD_LIBS:
192
+ cg.discovered_requirements.add(clean_imp)
193
  for req in schema.codegen.requirements:
194
  clean_req = str(req).strip()
195
  if clean_req not in STANDARD_LIBS and clean_req not in BANNED_IMPORTS:
196
+ cg.discovered_requirements.add(clean_req)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
+ nctx = NodeCodegenCtx(
199
+ node_id=node_id, raw_type=raw_type, label=label, clean_lbl=clean_lbl,
200
+ safe_id=safe_id, params=params, data=data, node_inputs=node_inputs,
201
+ incoming=incoming_override if incoming_override is not None else incoming_edges_map.get(node_id, []),
202
+ schema=schema, cg=cg,
203
+ )
204
+ if build_only:
205
+ return nctx, None
206
+ start_line = len(cg.code_body_lines) + 1
207
+ prev_len = len(cg.code_body_lines)
208
+ cg.code_body_lines.append(f" # Step {cg.step_counter}: [{label}]")
 
 
 
 
 
 
 
 
209
  node_var: Optional[str] = None
210
+ for emit_fn in EMITTER_PIPELINE:
211
+ handled, node_var = emit_fn(nctx)
212
+ if handled:
213
+ break
214
+ _register_node_outputs(nctx, node_var)
215
+ if extra_indent > 0:
216
+ pad = " " * extra_indent
217
+ for li in range(prev_len, len(cg.code_body_lines)):
218
+ if cg.code_body_lines[li].strip():
219
+ cg.code_body_lines[li] = pad + cg.code_body_lines[li]
220
+ cg.source_map.append({"node_id": node_id, "line_start": start_line, "line_end": len(cg.code_body_lines), "target_var": node_var, "statement_label": label})
221
+ cg.step_counter += 1
222
+ return nctx, node_var
223
+
224
+ from lambdas.codegen.scoped_blocks import emit_workflow_with_scopes
225
+ if not emit_workflow_with_scopes(cg, ordered_ids, node_map, filtered_edges, incoming_edges_map, _emit_single_node, _register_node_outputs):
226
+ for node_id in ordered_ids:
227
+ _emit_single_node(node_id)
228
+
229
+ return assemble_python_artifacts(cg, name, clean_filename, ordered_ids, graph_fingerprint, pin_exact)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
 
232
  def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
 
234
  try:
235
  body_raw = event.get("body", "{}")
236
  body = json.loads(body_raw) if isinstance(body_raw, str) else body_raw
 
 
 
 
 
 
 
 
 
237
  result = generate_code_logic(
238
+ nodes=body.get("nodes", []),
239
+ edges=body.get("edges", []),
240
+ execution_order=body.get("execution_order"),
241
+ graph_name=body.get("name", "alli_workflow"),
242
+ dependencies=body.get("dependencies", []),
243
+ strict_validation=bool(body.get("strict_validation", False)),
244
+ pin_exact=bool(body.get("pin_exact", True)),
245
  )
246
+ return {"statusCode": 200, "headers": {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "POST, OPTIONS"}, "body": json.dumps(result)}
 
 
 
 
 
 
 
 
 
247
  except Exception as err:
248
+ return {"statusCode": 500, "headers": {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}, "body": json.dumps({"error": str(err)})}
 
 
 
 
 
 
 
lambdas/inspect_handler.py CHANGED
@@ -1,58 +1,103 @@
1
  """
2
  ALLI Lambda Function 2: Library Inspector (alli-inspect)
3
- Dynamic reflection, ephemeral PyPI sandbox downloading, AST symbol extraction,
4
- and pure in-memory/MongoDB Atlas blueprint generation (zero disk file storage).
5
  """
6
 
 
7
  import os
8
  import re
9
- import json
10
  import tempfile
11
- import shutil
12
- from typing import Dict, Any, List, Optional
13
 
14
- from library_inspector.downloader import download_package_to_sandbox
15
- from library_inspector.extractor import extract_blueprint_from_file
16
- from library_inspector.stdlib_inspector import introspect_package as fallback_introspect
17
  from library_inspector.blueprint_models import (
18
  build_library_blueprint_from_extracted,
19
- compute_sha256_hash,
20
  compute_directory_sha256,
 
21
  )
22
- from database.mongo import workflow_repo
 
 
 
 
23
 
 
 
24
 
25
- def introspect_package_logic(package_name: str, version: Optional[str] = None) -> Dict[str, Any]:
26
- """
27
- Pure business logic for library inspection and blueprint generation.
28
- Zero disk storage: reads from MongoDB Atlas, uses ephemeral temp dirs, and cleans up immediately.
29
- """
30
- clean_name = re.sub(r"[^a-zA-Z0-9_\-\.=@><~]", "", package_name.strip())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  if not clean_name:
32
  raise ValueError("Package name cannot be empty")
33
 
34
- target_version = version.strip() if version else None
35
-
36
- # Parse version specifiers if embedded in package_name
37
  if "@" in clean_name:
38
  base_name, v = clean_name.split("@", 1)
39
  base_name = base_name.strip().lower()
40
- if not target_version:
41
- target_version = v.strip()
42
  elif "==" in clean_name:
43
  base_name, v = clean_name.split("==", 1)
44
  base_name = base_name.strip().lower()
45
- if not target_version:
46
- target_version = v.strip()
47
  else:
48
  base_name = clean_name.split(">=")[0].split("<=")[0].split("~=")[0].strip().lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- # 1. Check database cache in MongoDB Atlas / in-memory store (zero disk access)
 
 
 
 
 
 
 
 
51
  cached_bp = workflow_repo.get_blueprint(base_name, version=target_version)
52
  if cached_bp:
53
  return _convert_blueprint_to_package_schema(base_name, cached_bp)
54
 
55
- # 2. Try standard library dynamic reflection
56
  try:
57
  if target_version is None or target_version.startswith("3."):
58
  stdlib_res = fallback_introspect(base_name)
@@ -62,23 +107,29 @@ def introspect_package_logic(package_name: str, version: Optional[str] = None) -
62
  except Exception:
63
  pass
64
 
65
- # 3. Ephemeral PyPI download & AST parsing with immediate cleanup
66
  with tempfile.TemporaryDirectory(prefix=f"alli_inspect_{base_name}_") as temp_dir:
67
  try:
68
  package_spec = f"{base_name}=={target_version}" if target_version else base_name
69
- source_path, metadata = download_package_to_sandbox(package_spec, sandbox_base_dir=temp_dir)
 
 
 
70
  if target_version and not metadata.get("version"):
71
  metadata["version"] = target_version
72
 
73
  pkg_name = metadata.get("name", base_name)
74
  pkg_version = metadata.get("version", target_version or "unknown")
75
 
76
- # Compute SHA-256 content_hash of inspected files
77
  if os.path.isfile(source_path):
78
  filename = os.path.basename(source_path)
79
  with open(source_path, "rb") as f:
80
  content_hash = compute_sha256_hash(f.read())
81
- bp = extract_blueprint_from_file(source_path, display_path=f"@{pkg_name}/{filename}")
 
 
 
 
82
  bp["package"] = {
83
  "name": pkg_name,
84
  "version": pkg_version,
@@ -87,9 +138,15 @@ def introspect_package_logic(package_name: str, version: Optional[str] = None) -
87
  }
88
  else:
89
  content_hash = compute_directory_sha256(source_path)
90
- bp = _scan_directory(source_path, pkg_meta=metadata)
 
 
 
 
 
 
 
91
 
92
- # Build and validate LibraryBlueprint model
93
  bp_model = build_library_blueprint_from_extracted(
94
  base_name=base_name,
95
  pkg_meta_dict=metadata,
@@ -99,169 +156,20 @@ def introspect_package_logic(package_name: str, version: Optional[str] = None) -
99
  )
100
  bp_dict = bp_model.model_dump()
101
 
102
- # Return unified schema with synthesized nodes
103
  schema = _convert_blueprint_to_package_schema(base_name, bp)
104
  schema.update(bp_dict)
105
  schema["package_name"] = base_name
106
  schema["version"] = bp_model.package.version
107
  schema["blueprint"] = bp_dict
108
 
109
- # Persist directly into MongoDB Atlas (zero disk persistence) under composite and fallback keys
110
  workflow_repo.save_blueprint(base_name, schema, version=bp_model.package.version)
111
-
112
  return schema
113
-
 
114
  except Exception as err:
115
- # Fallback schema
116
- from library_inspector.node_synthesizer import DynamicNodeSynthesizer, global_staging_registry
117
- spec_version = target_version or (clean_name.split("==")[1] if "==" in clean_name else "1.0.0")
118
- fn_fallback = {
119
- "name": "execute",
120
- "docstring": f"Execute function for {base_name}",
121
- "inputs": [{"name": "input_data", "type": "Any", "required": True, "default": None}],
122
- "output": {"type": "Any"},
123
- "body_code": "pass",
124
- }
125
- node_def = DynamicNodeSynthesizer.synthesize_node(
126
- fn_fallback,
127
- module_name=base_name,
128
- package_name=base_name,
129
- package_version=spec_version,
130
- )
131
- global_staging_registry.register_candidate(node_def, source_module=base_name, source_package=base_name)
132
- d = node_def.model_dump()
133
- d["node_type"] = node_def.type
134
- d["module_name"] = base_name
135
- d["callable_name"] = "execute"
136
- d["version"] = node_def.version
137
-
138
- fallback_hash = compute_sha256_hash(f"fallback_{base_name}_{spec_version}".encode())
139
- bp_model = build_library_blueprint_from_extracted(
140
- base_name=base_name,
141
- pkg_meta_dict={"name": base_name, "version": spec_version, "summary": f"Module {base_name} (dynamic schema)"},
142
- extracted_data={"functions": [fn_fallback], "classes": []},
143
- content_hash=fallback_hash,
144
- source_type="stub",
145
- )
146
- bp_dict = bp_model.model_dump()
147
- workflow_repo.save_blueprint(base_name, bp_dict, version=spec_version)
148
-
149
- schema = {
150
- "package_name": base_name,
151
- "version": node_def.version,
152
- "docstring": f"Module {base_name} (dynamic schema)",
153
- "node_count": 1,
154
- "categories": [base_name.capitalize()],
155
- "nodes": [d],
156
- "blueprint": bp_dict,
157
- }
158
- schema.update(bp_dict)
159
- schema["package_name"] = base_name
160
- schema["nodes"] = [d]
161
- schema["node_count"] = 1
162
- return schema
163
-
164
-
165
- def _scan_directory(dir_path: str, pkg_meta: dict = None, max_files: int = 50) -> dict:
166
- if pkg_meta is None:
167
- pkg_meta = {}
168
- pkg_name = pkg_meta.get("name") or os.path.basename(os.path.abspath(dir_path))
169
- combined = {
170
- "package": {
171
- "name": pkg_name,
172
- "version": pkg_meta.get("version", "unknown"),
173
- "summary": pkg_meta.get("summary", ""),
174
- "root": f"@{pkg_name}",
175
- },
176
- "modules": [],
177
- "imports": [],
178
- "classes": [],
179
- "functions": [],
180
- "relations": [],
181
- }
182
- parsed = 0
183
- for root, dirs, files in os.walk(dir_path):
184
- dirs[:] = [d for d in dirs if not d.startswith((".", "__")) and d.lower() not in ("tests", "test")]
185
- for file in files:
186
- if file.endswith(".py") and not file.startswith("test_"):
187
- if parsed >= max_files:
188
- break
189
- full_path = os.path.join(root, file)
190
- rel_path = os.path.relpath(full_path, dir_path).replace(os.path.sep, "/")
191
- mod_name = rel_path.replace(".py", "").replace("/", ".")
192
- try:
193
- bp = extract_blueprint_from_file(full_path, module_name=mod_name)
194
- combined["functions"].extend(bp.get("functions", []))
195
- combined["classes"].extend(bp.get("classes", []))
196
- parsed += 1
197
- except Exception:
198
- continue
199
- return combined
200
-
201
-
202
- def _convert_blueprint_to_package_schema(pkg_name: str, bp: dict) -> dict:
203
- from library_inspector.node_synthesizer import DynamicNodeSynthesizer, global_staging_registry
204
-
205
- # If bp already contains synthesized nodes and blueprint_id, return immediately
206
- if "nodes" in bp and "blueprint_id" in bp:
207
- return bp
208
-
209
- pkg_meta = bp.get("package", {}) if isinstance(bp.get("package"), dict) else {}
210
- pkg_version = pkg_meta.get("version") or bp.get("version")
211
-
212
- functions = list(bp.get("functions", []))
213
- if not functions and bp.get("modules"):
214
- for mod in bp.get("modules", []):
215
- functions.extend(mod.get("functions", []))
216
- for cls in mod.get("classes", []):
217
- functions.extend(cls.get("methods", []))
218
-
219
- nodes = list(bp.get("nodes", []))
220
- categories = set(bp.get("categories", [pkg_name.capitalize()]))
221
-
222
- if not nodes:
223
- for fn in functions:
224
- if not fn.get("is_public", True):
225
- continue
226
- try:
227
- node_def = DynamicNodeSynthesizer.synthesize_node(
228
- func_symbol=fn,
229
- module_name=pkg_name,
230
- package_name=pkg_name,
231
- package_version=pkg_version,
232
- )
233
- ok, errs = global_staging_registry.register_candidate(node_def, source_module=pkg_name, source_package=pkg_name)
234
- if ok:
235
- d = node_def.model_dump()
236
- d["node_type"] = node_def.type # Backward compatibility alias
237
- d["module_name"] = pkg_name
238
- d["callable_name"] = fn.get("name", "func")
239
- d["version"] = node_def.version
240
- d["capability_states"] = node_def.capability_states
241
- nodes.append(d)
242
- categories.add(node_def.category)
243
- except Exception:
244
- continue
245
-
246
- res = {
247
- "package_name": pkg_name,
248
- "version": pkg_version or "1.0.0",
249
- "docstring": pkg_meta.get("summary") or bp.get("docstring") or f"Package {pkg_name}",
250
- "node_count": len(nodes),
251
- "categories": sorted(list(categories)),
252
- "nodes": nodes,
253
- }
254
-
255
- # Merge blueprint fields if present
256
- for k in ("blueprint_id", "blueprint_version", "package", "extraction", "modules", "initialization", "confidence_scores"):
257
- if k in bp:
258
- res[k] = bp[k]
259
- if "blueprint" in bp:
260
- res["blueprint"] = bp["blueprint"]
261
- elif "blueprint_id" in bp:
262
- res["blueprint"] = {k: bp[k] for k in ("blueprint_id", "blueprint_version", "package", "extraction", "modules", "initialization", "confidence_scores") if k in bp}
263
-
264
- return res
265
 
266
 
267
  def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
@@ -272,7 +180,6 @@ def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
272
  package = body.get("package", "math")
273
  version = body.get("version")
274
  schema = introspect_package_logic(package, version=version)
275
-
276
  return {
277
  "statusCode": 200,
278
  "headers": {
@@ -282,6 +189,15 @@ def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
282
  },
283
  "body": json.dumps(schema),
284
  }
 
 
 
 
 
 
 
 
 
285
  except Exception as err:
286
  return {
287
  "statusCode": 500,
 
1
  """
2
  ALLI Lambda Function 2: Library Inspector (alli-inspect)
3
+ Zero-RCE PyPI binary wheel introspection, `.py`/`.pyi` AST extraction,
4
+ and persistent MongoDB Atlas blueprint & staged node synthesis.
5
  """
6
 
7
+ import json
8
  import os
9
  import re
 
10
  import tempfile
11
+ from typing import Any, Dict, Optional, Tuple
 
12
 
13
+ from database.mongo import workflow_repo
 
 
14
  from library_inspector.blueprint_models import (
15
  build_library_blueprint_from_extracted,
 
16
  compute_directory_sha256,
17
+ compute_sha256_hash,
18
  )
19
+ from library_inspector.blueprint_synthesizer import synthesize_package_blueprint_schema
20
+ from library_inspector.downloader import download_package_to_sandbox
21
+ from library_inspector.extractor import extract_blueprint_from_file
22
+ from library_inspector.package_scanner import scan_package_directory
23
+ from library_inspector.stdlib_inspector import introspect_package as fallback_introspect
24
 
25
+ # Backward compatibility alias
26
+ _scan_directory = scan_package_directory
27
 
28
+
29
+ class PackageIntrospectionError(ValueError, RuntimeError):
30
+ """Dual ValueError/RuntimeError raised on honest package lookup or extraction failures."""
31
+
32
+
33
+ _SENTINEL_VERSION_STRINGS = {"", "latest", "unknown", "cached", "none", "auto", "*"}
34
+
35
+
36
+ def _normalize_version_arg(raw_version: Optional[str]) -> Optional[str]:
37
+ """Strip UI display notes (like '(built-in)') and sentinel placeholders; return clean version or None."""
38
+ if not raw_version:
39
+ return None
40
+ raw = str(raw_version).strip()
41
+ low = raw.lower()
42
+ if low in _SENTINEL_VERSION_STRINGS or "built-in" in low or "builtin" in low:
43
+ return None
44
+ cleaned = re.sub(r"[^a-zA-Z0-9._+-]", "", raw)
45
+ if not cleaned or cleaned.lower() in _SENTINEL_VERSION_STRINGS:
46
+ return None
47
+ if not re.match(r"^[vV]?\d", cleaned):
48
+ return None
49
+ return cleaned.lstrip("vV")
50
+
51
+
52
+ def _parse_package_spec(package_name: str, version: Optional[str]) -> Tuple[str, Optional[str]]:
53
+ raw_input = str(package_name or "")
54
+ has_builtin_tag = bool(re.search(r"built[- ]?in", raw_input, re.IGNORECASE))
55
+ raw_stripped = re.sub(r"\s*\([^)]*built[- ]?in[^)]*\)", "", raw_input, flags=re.IGNORECASE).strip()
56
+ if raw_stripped.endswith("==") or raw_stripped.endswith("@"):
57
+ raw_stripped = raw_stripped.rstrip("=@").strip()
58
+ clean_name = re.sub(r"[^a-zA-Z0-9_\-\.=@><~]", "", raw_stripped)
59
  if not clean_name:
60
  raise ValueError("Package name cannot be empty")
61
 
62
+ target_version = _normalize_version_arg(version)
 
 
63
  if "@" in clean_name:
64
  base_name, v = clean_name.split("@", 1)
65
  base_name = base_name.strip().lower()
66
+ target_version = target_version or (None if has_builtin_tag else _normalize_version_arg(v))
 
67
  elif "==" in clean_name:
68
  base_name, v = clean_name.split("==", 1)
69
  base_name = base_name.strip().lower()
70
+ target_version = target_version or (None if has_builtin_tag else _normalize_version_arg(v))
 
71
  else:
72
  base_name = clean_name.split(">=")[0].split("<=")[0].split("~=")[0].strip().lower()
73
+ if not base_name:
74
+ raise ValueError("Package name cannot be empty")
75
+ return base_name, target_version
76
+
77
+
78
+ def _convert_blueprint_to_package_schema(pkg_name: str, bp: Dict[str, Any]) -> Dict[str, Any]:
79
+ """Converts a raw/stored package blueprint into unified schema and stages candidate nodes."""
80
+ return synthesize_package_blueprint_schema(
81
+ pkg_name,
82
+ bp,
83
+ persist_staged=True,
84
+ repo=workflow_repo,
85
+ )
86
 
87
+
88
+ def introspect_package_logic(package_name: str, version: Optional[str] = None) -> Dict[str, Any]:
89
+ """
90
+ Zero-RCE business logic for library inspection and persistent blueprint synthesis.
91
+ Never fabricates fake `.execute` stub caches when download or extraction fails.
92
+ """
93
+ base_name, target_version = _parse_package_spec(package_name, version)
94
+
95
+ # 1. Check persisted blueprint cache (MongoDB Atlas / in-memory store)
96
  cached_bp = workflow_repo.get_blueprint(base_name, version=target_version)
97
  if cached_bp:
98
  return _convert_blueprint_to_package_schema(base_name, cached_bp)
99
 
100
+ # 2. Try verified Python standard library inspection
101
  try:
102
  if target_version is None or target_version.startswith("3."):
103
  stdlib_res = fallback_introspect(base_name)
 
107
  except Exception:
108
  pass
109
 
110
+ # 3. Ephemeral Zero-RCE PyPI wheel download & static extraction (fail loudly on error)
111
  with tempfile.TemporaryDirectory(prefix=f"alli_inspect_{base_name}_") as temp_dir:
112
  try:
113
  package_spec = f"{base_name}=={target_version}" if target_version else base_name
114
+ source_path, metadata = download_package_to_sandbox(
115
+ package_spec,
116
+ sandbox_base_dir=temp_dir,
117
+ )
118
  if target_version and not metadata.get("version"):
119
  metadata["version"] = target_version
120
 
121
  pkg_name = metadata.get("name", base_name)
122
  pkg_version = metadata.get("version", target_version or "unknown")
123
 
 
124
  if os.path.isfile(source_path):
125
  filename = os.path.basename(source_path)
126
  with open(source_path, "rb") as f:
127
  content_hash = compute_sha256_hash(f.read())
128
+ bp = extract_blueprint_from_file(
129
+ source_path,
130
+ display_path=f"@{pkg_name}/{filename}",
131
+ include_ast_dump=False,
132
+ )
133
  bp["package"] = {
134
  "name": pkg_name,
135
  "version": pkg_version,
 
138
  }
139
  else:
140
  content_hash = compute_directory_sha256(source_path)
141
+ stubs_sibling = f"{source_path}-stubs"
142
+ extra = [stubs_sibling] if os.path.isdir(stubs_sibling) else None
143
+ bp = scan_package_directory(source_path, pkg_meta=metadata, extra_dirs=extra)
144
+
145
+ if not bp.get("functions") and not bp.get("classes") and not bp.get("constants"):
146
+ raise ValueError(
147
+ f"Package '{base_name}' contained zero extractable `.py`/`.pyi` Python signatures."
148
+ )
149
 
 
150
  bp_model = build_library_blueprint_from_extracted(
151
  base_name=base_name,
152
  pkg_meta_dict=metadata,
 
156
  )
157
  bp_dict = bp_model.model_dump()
158
 
 
159
  schema = _convert_blueprint_to_package_schema(base_name, bp)
160
  schema.update(bp_dict)
161
  schema["package_name"] = base_name
162
  schema["version"] = bp_model.package.version
163
  schema["blueprint"] = bp_dict
164
 
 
165
  workflow_repo.save_blueprint(base_name, schema, version=bp_model.package.version)
 
166
  return schema
167
+ except (PackageIntrospectionError, ValueError):
168
+ raise
169
  except Exception as err:
170
+ raise PackageIntrospectionError(
171
+ f"Failed to introspect package '{base_name}': {err}"
172
+ ) from err
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
 
175
  def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
 
180
  package = body.get("package", "math")
181
  version = body.get("version")
182
  schema = introspect_package_logic(package, version=version)
 
183
  return {
184
  "statusCode": 200,
185
  "headers": {
 
189
  },
190
  "body": json.dumps(schema),
191
  }
192
+ except ValueError as err:
193
+ return {
194
+ "statusCode": 400,
195
+ "headers": {
196
+ "Content-Type": "application/json",
197
+ "Access-Control-Allow-Origin": "*",
198
+ },
199
+ "body": json.dumps({"error": str(err)}),
200
+ }
201
  except Exception as err:
202
  return {
203
  "statusCode": 500,
lambdas/run_handler.py CHANGED
@@ -1,26 +1,22 @@
1
  """
2
  ALLI Lambda Function 1: Code Execution Sandbox (alli-run)
3
- Air-gapped execution of user Python code with strict timeouts, memory isolation, and credential scrubbing.
4
  """
5
 
6
- import os
7
  import json
8
- from typing import Dict, Any, Optional
 
9
  from runtime_environment.runner import RuntimeRunner
10
 
11
  runner = RuntimeRunner(default_mode="auto", timeout_sec=5.0)
12
 
13
- SENSITIVE_VARS = [
14
- "AWS_ACCESS_KEY_ID",
15
- "AWS_SECRET_ACCESS_KEY",
16
- "AWS_SESSION_TOKEN",
17
- "SUPABASE_KEY",
18
- "DATABASE_URL",
19
- "MONGODB_URI",
20
- ]
21
-
22
 
23
- def execute_code_logic(code: str, timeout_seconds: float = 5.0, mode: str = "local") -> Dict[str, Any]:
 
 
 
 
 
24
  """Pure business logic for sandboxed execution, shared by FastAPI and AWS Lambda."""
25
  if not code or not code.strip():
26
  return {
@@ -31,14 +27,11 @@ def execute_code_logic(code: str, timeout_seconds: float = 5.0, mode: str = "loc
31
  "duration_ms": 0.0,
32
  }
33
 
34
- # Scrub sensitive credentials from execution environment
35
- clean_env = os.environ.copy()
36
- for var in SENSITIVE_VARS:
37
- clean_env.pop(var, None)
38
-
39
  try:
40
- custom_runner = RuntimeRunner(default_mode=mode, timeout_sec=timeout_seconds)
41
- res = custom_runner.run(code, mode=mode)
42
  return {
43
  "success": res.success,
44
  "stdout": res.stdout[:100000],
@@ -48,22 +41,52 @@ def execute_code_logic(code: str, timeout_seconds: float = 5.0, mode: str = "loc
48
  "mode": res.mode,
49
  }
50
  except Exception as e:
 
51
  return {
52
  "success": False,
53
  "stdout": "",
54
- "stderr": f"Runtime sandbox error: {str(e)}",
 
 
55
  "exit_code": 1,
56
  "duration_ms": 0.0,
 
57
  }
58
 
59
 
60
- def execute_workflow_logic(graph: Dict[str, Any], timeout_seconds: float = 5.0, owner_user_id: Optional[str] = None, target_node_id: Optional[str] = None) -> Dict[str, Any]:
61
- """Pure business logic for connected graph execution (Phase 3.10)."""
62
- from language_core.workflow_migration import migrate_workflow
 
 
 
 
 
 
 
 
 
 
 
63
  from language_core.workflow_executor import execute_workflow_graph
 
 
64
  if graph and isinstance(graph, dict):
65
  graph = migrate_workflow(graph)
66
- return execute_workflow_graph(graph, timeout_seconds=timeout_seconds, owner_user_id=owner_user_id, target_node_id=target_node_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  def execute_workflow_step_logic(
69
  graph: Dict[str, Any],
@@ -72,10 +95,15 @@ def execute_workflow_step_logic(
72
  previous_results: Optional[Dict[str, Dict[str, Any]]] = None,
73
  timeout_seconds: float = 5.0,
74
  run_id: Optional[str] = None,
 
 
 
 
75
  ) -> Dict[str, Any]:
76
  """Pure business logic for step-by-step interactive workflow execution (Step 3.4 & Step 6.4)."""
77
- from language_core.workflow_migration import migrate_workflow
78
  from language_core.workflow_executor import execute_workflow_step
 
 
79
  if graph and isinstance(graph, dict):
80
  graph = migrate_workflow(graph)
81
  return execute_workflow_step(
@@ -85,6 +113,10 @@ def execute_workflow_step_logic(
85
  previous_results=previous_results,
86
  timeout_seconds=timeout_seconds,
87
  run_id=run_id,
 
 
 
 
88
  )
89
 
90
 
@@ -94,27 +126,23 @@ def cancel_workflow_logic(
94
  ) -> Dict[str, Any]:
95
  """Pure business logic for authoritative workflow cancellation (Step 3.5)."""
96
  from language_core.workflow_executor import cancel_workflow_run
97
- return cancel_workflow_run(run_id=run_id, node_results=node_results)
98
-
99
 
 
100
 
101
 
102
  def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
103
  """Standalone AWS Lambda handler entrypoint for alli-run function."""
104
  try:
105
  body_raw = event.get("body", "{}")
106
- if isinstance(body_raw, str):
107
- body = json.loads(body_raw) if body_raw else {}
108
- else:
109
- body = body_raw
110
 
111
  timeout = float(body.get("timeout_seconds", 5.0))
112
  mode = body.get("mode", "local")
113
 
114
- # Phase 3.10: If a workflow graph is provided, run connected graph execution
115
  if "graph" in body or ("nodes" in body and isinstance(body.get("nodes"), list)):
116
  graph = body.get("graph") or body
117
- result = execute_workflow_logic(graph, timeout_seconds=timeout)
 
118
  else:
119
  code = body.get("code", "")
120
  result = execute_code_logic(code, timeout_seconds=timeout, mode=mode)
@@ -131,10 +159,6 @@ def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
131
  except Exception as err:
132
  return {
133
  "statusCode": 500,
134
- "headers": {
135
- "Content-Type": "application/json",
136
- "Access-Control-Allow-Origin": "*",
137
- },
138
  "body": json.dumps({"error": str(err)}),
139
  }
140
-
 
1
  """
2
  ALLI Lambda Function 1: Code Execution Sandbox (alli-run)
3
+ Air-gapped execution of user Python code & workflow graphs with strict timeouts, isolation, and credential scrubbing.
4
  """
5
 
 
6
  import json
7
+ from typing import Any, Dict, Optional
8
+
9
  from runtime_environment.runner import RuntimeRunner
10
 
11
  runner = RuntimeRunner(default_mode="auto", timeout_sec=5.0)
12
 
 
 
 
 
 
 
 
 
 
13
 
14
+ def execute_code_logic(
15
+ code: str,
16
+ timeout_seconds: float = 5.0,
17
+ mode: str = "local",
18
+ workspace_env: Optional[Dict[str, str]] = None,
19
+ ) -> Dict[str, Any]:
20
  """Pure business logic for sandboxed execution, shared by FastAPI and AWS Lambda."""
21
  if not code or not code.strip():
22
  return {
 
27
  "duration_ms": 0.0,
28
  }
29
 
30
+ norm_mode = str(mode or "local").strip().lower()
31
+ err_mode = "docker_sandbox" if norm_mode in ("docker", "docker_sandbox") else "local_subprocess"
 
 
 
32
  try:
33
+ custom_runner = RuntimeRunner(default_mode=norm_mode, timeout_sec=timeout_seconds, workspace_env=workspace_env)
34
+ res = custom_runner.run(code, mode=norm_mode)
35
  return {
36
  "success": res.success,
37
  "stdout": res.stdout[:100000],
 
41
  "mode": res.mode,
42
  }
43
  except Exception as e:
44
+ err_str = str(e)
45
  return {
46
  "success": False,
47
  "stdout": "",
48
+ "stderr": err_str if "ERR-SANDBOX-UNAVAILABLE" in err_str else f"Runtime sandbox error: {err_str}",
49
+ "error": err_str,
50
+ "code": "ERR-SANDBOX-UNAVAILABLE" if "ERR-SANDBOX-UNAVAILABLE" in err_str else "ERR-RUNTIME",
51
  "exit_code": 1,
52
  "duration_ms": 0.0,
53
+ "mode": err_mode,
54
  }
55
 
56
 
57
+ def execute_workflow_logic(
58
+ graph: Dict[str, Any],
59
+ timeout_seconds: float = 5.0,
60
+ owner_user_id: Optional[str] = None,
61
+ target_node_id: Optional[str] = None,
62
+ mode: str = "in_process",
63
+ org_id: Optional[str] = None,
64
+ role: Optional[str] = None,
65
+ workspace_env: Optional[Dict[str, str]] = None,
66
+ cached_outputs: Optional[Dict[str, Dict[str, Any]]] = None,
67
+ pinned_inputs: Optional[Dict[str, Dict[str, Any]]] = None,
68
+ single_node_only: bool = False,
69
+ ) -> Dict[str, Any]:
70
+ """Pure business logic for connected graph execution with multi-mode parity."""
71
  from language_core.workflow_executor import execute_workflow_graph
72
+ from language_core.workflow_migration import migrate_workflow
73
+
74
  if graph and isinstance(graph, dict):
75
  graph = migrate_workflow(graph)
76
+ return execute_workflow_graph(
77
+ graph,
78
+ timeout_seconds=timeout_seconds,
79
+ owner_user_id=owner_user_id,
80
+ target_node_id=target_node_id,
81
+ mode=mode,
82
+ org_id=org_id,
83
+ role=role,
84
+ workspace_env=workspace_env,
85
+ cached_outputs=cached_outputs,
86
+ pinned_inputs=pinned_inputs,
87
+ single_node_only=single_node_only,
88
+ )
89
+
90
 
91
  def execute_workflow_step_logic(
92
  graph: Dict[str, Any],
 
95
  previous_results: Optional[Dict[str, Dict[str, Any]]] = None,
96
  timeout_seconds: float = 5.0,
97
  run_id: Optional[str] = None,
98
+ owner_user_id: Optional[str] = None,
99
+ org_id: Optional[str] = None,
100
+ role: Optional[str] = None,
101
+ workspace_env: Optional[Dict[str, str]] = None,
102
  ) -> Dict[str, Any]:
103
  """Pure business logic for step-by-step interactive workflow execution (Step 3.4 & Step 6.4)."""
 
104
  from language_core.workflow_executor import execute_workflow_step
105
+ from language_core.workflow_migration import migrate_workflow
106
+
107
  if graph and isinstance(graph, dict):
108
  graph = migrate_workflow(graph)
109
  return execute_workflow_step(
 
113
  previous_results=previous_results,
114
  timeout_seconds=timeout_seconds,
115
  run_id=run_id,
116
+ owner_user_id=owner_user_id,
117
+ org_id=org_id,
118
+ role=role,
119
+ workspace_env=workspace_env,
120
  )
121
 
122
 
 
126
  ) -> Dict[str, Any]:
127
  """Pure business logic for authoritative workflow cancellation (Step 3.5)."""
128
  from language_core.workflow_executor import cancel_workflow_run
 
 
129
 
130
+ return cancel_workflow_run(run_id=run_id, node_results=node_results)
131
 
132
 
133
  def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
134
  """Standalone AWS Lambda handler entrypoint for alli-run function."""
135
  try:
136
  body_raw = event.get("body", "{}")
137
+ body = json.loads(body_raw) if isinstance(body_raw, str) else body_raw
 
 
 
138
 
139
  timeout = float(body.get("timeout_seconds", 5.0))
140
  mode = body.get("mode", "local")
141
 
 
142
  if "graph" in body or ("nodes" in body and isinstance(body.get("nodes"), list)):
143
  graph = body.get("graph") or body
144
+ graph_mode = body.get("mode") or "in_process"
145
+ result = execute_workflow_logic(graph, timeout_seconds=timeout, mode=graph_mode)
146
  else:
147
  code = body.get("code", "")
148
  result = execute_code_logic(code, timeout_seconds=timeout, mode=mode)
 
159
  except Exception as err:
160
  return {
161
  "statusCode": 500,
162
+ "headers": {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"},
 
 
 
163
  "body": json.dumps({"error": str(err)}),
164
  }
 
lambdas/validate_handler.py CHANGED
@@ -1,644 +1,63 @@
1
  """
2
  ALLI Lambda Function: Language Core Validator (alli-validate)
3
- Validates type compatibility and port assignability using Language Core type system rules.
4
  """
5
 
6
- import ast
7
  import json
8
- import re
9
- from typing import Dict, Any, List, Optional, Union, Tuple
10
- from language_core.types import parse_type_string, DataType
11
- from language_core.node_schema import default_registry, NodeDefinition
12
-
13
-
14
- def validate_expression_logic(expression: str) -> Dict[str, Any]:
15
- """Validates Python expression syntax using ast.parse."""
16
- if not expression or not expression.strip():
17
- return {"valid": False, "error": "Expression cannot be empty."}
18
- trimmed = expression.strip()
19
- try:
20
- # First try evaluating as a pure Python expression (e.g. `user_id >= 18`)
21
- ast.parse(trimmed, mode="eval")
22
- return {
23
- "valid": True,
24
- "expression": trimmed,
25
- "message": "Valid Python expression syntax.",
26
- }
27
- except SyntaxError:
28
- # If not a pure eval expression, try parsing as a full statement/suite (e.g. `try:...`, `for i in ...:`)
29
- try:
30
- ast.parse(trimmed, mode="exec")
31
- return {
32
- "valid": True,
33
- "expression": trimmed,
34
- "message": "Valid Python statement/block syntax.",
35
- }
36
- except SyntaxError as syn_err:
37
- return {
38
- "valid": False,
39
- "expression": trimmed,
40
- "error": f"Syntax error at line {syn_err.lineno}, col {syn_err.offset}: {syn_err.msg}",
41
- }
42
- except Exception as ex:
43
- return {"valid": False, "expression": trimmed, "error": str(ex)}
44
- except Exception as ex:
45
- return {"valid": False, "expression": trimmed, "error": str(ex)}
46
-
47
-
48
- def validate_types_logic(source_type_str: str, target_type_str: str) -> Dict[str, Any]:
49
- """Validates if source_type can be plugged into target_type with strict type-checking and actionable cast warnings."""
50
- src = parse_type_string(source_type_str)
51
- tgt = parse_type_string(target_type_str)
52
-
53
- assignable = src.is_assignable_to(tgt)
54
-
55
- src_lower = src.name.lower()
56
- tgt_lower = tgt.name.lower()
57
-
58
- if assignable:
59
- warning = None
60
- if src_lower == "int" and tgt_lower in ("float", "number"):
61
- warning = "Implicit Widening: 'int' is promoted to 'float' (numeric widening)."
62
- elif src_lower == "float" and tgt_lower == "number":
63
- warning = "Subtyping: 'float' to 'number' general numeric union."
64
- elif src_lower in ("any", "object") and tgt_lower not in ("any", "object"):
65
- warning = f"Dynamic Source: Output of type '{src}' connects to specific input '{tgt}'; runtime type-checking will be enforced."
66
- elif not src.inner_types and tgt.inner_types and src_lower not in ("any", "object"):
67
- warning = f"Dynamic Source: Untyped '{src}' connects to parameterized '{tgt}'; runtime element type-checking will be enforced."
68
- return {
69
- "valid": True,
70
- "source_type": repr(src),
71
- "target_type": repr(tgt),
72
- "warning": warning,
73
- "message": f"Valid: '{src}' is safely assignable to '{tgt}'.",
74
- }
75
- else:
76
- # Determine actionable error reason with cast advice
77
- requires_cast = False
78
- if src_lower == "union":
79
- reason = f"Union Type Mismatch: Source union '{src}' contains incompatible types that cannot be safely assigned to target '{tgt}'. Every member of the source union must satisfy the target type."
80
- elif tgt_lower == "sequence" and src_lower in ("set", "dict"):
81
- reason = f"Collection Type Mismatch: '{src}' is not an ordered sequence and cannot be assigned to 'Sequence'."
82
- elif tgt_lower in ("iterable", "collection", "sequence"):
83
- reason = f"Collection Type Mismatch: '{src}' cannot be assigned to abstract container '{tgt}'."
84
- elif src.inner_types and tgt.inner_types and src_lower == tgt_lower:
85
- reason = f"Container Element Mismatch: '{src}' element types are incompatible with expected '{tgt}'."
86
- elif src_lower in ("float", "number") and tgt_lower == "int":
87
- reason = f"Type Mismatch: Loss of precision. Output '{src}' cannot be plugged into integer input '{tgt}' without an explicit conversion or cast (e.g. round, math.floor, int)."
88
- requires_cast = True
89
- elif src_lower == "str" and tgt_lower in ("int", "float", "number"):
90
- reason = f"Type Mismatch: String output cannot be plugged into numeric input '{tgt}' without an explicit parsing or cast node (e.g. int(), float())."
91
- requires_cast = True
92
- elif src_lower in ("int", "float", "number") and tgt_lower == "str":
93
- reason = f"Type Mismatch: Numeric output '{src}' cannot be plugged into string input without an explicit conversion node (e.g. str())."
94
- requires_cast = True
95
- elif src_lower == "bool" and tgt_lower != "bool":
96
- reason = f"Type Mismatch: Boolean output cannot be implicitly converted to '{tgt}'. Explicit conversion or cast required."
97
- requires_cast = True
98
- elif tgt_lower == "bool" and src_lower != "bool":
99
- reason = f"Type Mismatch: Non-boolean output of type '{src}' cannot be assigned to boolean input without an explicit condition or bool() evaluation."
100
- requires_cast = True
101
- elif src_lower in ("control", "branch", "exec") or tgt_lower in ("control", "branch", "exec"):
102
- reason = f"Control-Flow Violation: Execution control ports ('{src}') cannot be connected to data ports ('{tgt}')."
103
- else:
104
- reason = f"Type Mismatch: Cannot connect output of type '{src}' to input of type '{tgt}'."
105
-
106
- return {
107
- "valid": False,
108
- "source_type": repr(src),
109
- "target_type": repr(tgt),
110
- "requires_cast": requires_cast,
111
- "reason": reason,
112
- "message": reason,
113
- }
114
-
115
-
116
- def is_loop_header_node(node_dict: Optional[Dict[str, Any]]) -> bool:
117
- if not node_dict:
118
- return False
119
- d = node_dict.get("data") or {}
120
- ntype = str(node_dict.get("node_type") or node_dict.get("type") or d.get("node_type") or d.get("type") or "").lower()
121
- lbl = str(d.get("label") or node_dict.get("label") or "").lower()
122
- return (
123
- "for_loop" in ntype
124
- or "while_loop" in ntype
125
- or ntype in ("control.for_loop", "control.while_loop")
126
- or (d.get("category") == "Control Flow" and ("for" in lbl or "while" in lbl or "loop" in lbl))
127
- )
128
-
129
-
130
- APPROVED_AGENT_TYPES = {
131
- "alli.agent.llm", "agent.llm",
132
- "alli.agent.tool", "agent.tool",
133
- "alli.agent.router", "agent.router",
134
- "alli.agent.memory", "agent.memory",
135
- "alli.agent.prompt", "agent.prompt",
136
- "alli.agent.react_agent", "agent.react_agent", "agent.reasoner",
137
- "alli.agent.checkpointer", "agent.checkpointer", "agent.memory_saver",
138
  }
139
 
140
- APPROVED_REASONER_TYPES = {
141
- "alli.agent.llm", "agent.llm",
142
- "alli.agent.router", "agent.router",
143
- "alli.agent.memory", "agent.memory",
144
- "alli.agent.prompt", "agent.prompt",
145
- "alli.agent.react_agent", "agent.react_agent", "agent.reasoner",
146
- "alli.agent.checkpointer", "agent.checkpointer", "agent.memory_saver",
147
- }
148
-
149
-
150
- def is_approved_agent_node(node_dict: Optional[Dict[str, Any]]) -> bool:
151
- if not node_dict:
152
- return False
153
- d = node_dict.get("data") or {}
154
- ntype = str(node_dict.get("node_type") or node_dict.get("type") or d.get("node_type") or d.get("type") or "").lower()
155
- return ntype in APPROVED_AGENT_TYPES
156
-
157
-
158
- def is_agent_reasoner_or_memory(node_dict: Optional[Dict[str, Any]]) -> bool:
159
- if not node_dict:
160
- return False
161
- d = node_dict.get("data") or {}
162
- ntype = str(node_dict.get("node_type") or node_dict.get("type") or d.get("node_type") or d.get("type") or "").lower()
163
- return ntype in APPROVED_REASONER_TYPES
164
-
165
-
166
- def validate_connection_logic(
167
- connection: Dict[str, Any],
168
- nodes_map: Optional[Dict[str, Any]] = None,
169
- edges: Optional[List[Dict[str, Any]]] = None,
170
- skip_cycle_check: bool = False,
171
- ) -> Dict[str, Any]:
172
- """
173
- Phase 3.6: Complete connection validation contract.
174
- Validates port existence, direction, multiplicity, duplicate detection,
175
- control-flow isolation, strict type assignability, and cycle detection (DAG-001).
176
- """
177
- from collections import deque
178
-
179
- from_node_id = connection.get("from_node") or connection.get("source")
180
- to_node_id = connection.get("to_node") or connection.get("target")
181
- from_port = connection.get("from_port") or connection.get("sourceHandle") or connection.get("source_handle") or "out"
182
- to_port = connection.get("to_port") or connection.get("targetHandle") or connection.get("target_handle") or "input"
183
-
184
- if not from_node_id or not to_node_id:
185
- return {
186
- "valid": False,
187
- "code": "VAL-INCOMPLETE-EDGE",
188
- "message": "Incomplete Connection: Both source and target endpoints must be provided.",
189
- }
190
-
191
- # Step 3.6.7: Self-connection prohibited (with loop header CTRL-001 distinction)
192
- if from_node_id == to_node_id:
193
- tgt_is_loop = False
194
- if nodes_map and to_node_id in nodes_map:
195
- tgt_is_loop = is_loop_header_node(nodes_map[to_node_id])
196
- if tgt_is_loop:
197
- return {
198
- "valid": False,
199
- "code": "CTRL-001",
200
- "message": f"[CTRL-001] Invalid Loop Feedback: Node '{from_node_id}' cannot loop back directly to itself. ALLI loop constructs manage iterations internally without backward feedback wires.",
201
- "source_node": from_node_id,
202
- "target_node": to_node_id,
203
- }
204
- return {
205
- "valid": False,
206
- "code": "DAG-001",
207
- "message": f"[DAG-001] Self-Loop Prohibited: Node '{from_node_id}' cannot connect directly to itself.",
208
- "source_node": from_node_id,
209
- "target_node": to_node_id,
210
- }
211
-
212
- # Step 3.6.1: Node existence
213
- src_node = None
214
- tgt_node = None
215
- if nodes_map:
216
- if from_node_id not in nodes_map:
217
- return {
218
- "valid": False,
219
- "code": "VAL-NODE-NOT-FOUND",
220
- "message": f"Node Resolution Error: Source node '{from_node_id}' does not exist.",
221
- }
222
- if to_node_id not in nodes_map:
223
- return {
224
- "valid": False,
225
- "code": "VAL-NODE-NOT-FOUND",
226
- "message": f"Node Resolution Error: Target node '{to_node_id}' does not exist.",
227
- }
228
- src_node = nodes_map[from_node_id]
229
- tgt_node = nodes_map[to_node_id]
230
-
231
- src_label = src_node.get("data", {}).get("label") or from_node_id if src_node else from_node_id
232
- tgt_label = tgt_node.get("data", {}).get("label") or to_node_id if tgt_node else to_node_id
233
-
234
- # Resolve ports
235
- src_type_str = connection.get("source_type")
236
- tgt_type_str = connection.get("target_type")
237
- src_kind = "data"
238
- tgt_kind = "data"
239
- tgt_multiple = False
240
- found_out = None
241
- found_in = None
242
-
243
- def _extract_node_port_list(node: Optional[Dict[str, Any]], port_key: str) -> List[Dict[str, Any]]:
244
- if not node:
245
- return []
246
- data = node.get("data") or {}
247
- # 1. Authoritative registry lookup first to prevent client-side spoofing
248
- ntype = str(node.get("type") or node.get("node_type") or data.get("node_type") or data.get("type") or "")
249
- reg_schema = None
250
- if ntype:
251
- if default_registry.has(ntype):
252
- reg_schema = default_registry.get(ntype)
253
- else:
254
- try:
255
- from library_inspector.node_synthesizer import global_staging_registry
256
- if global_staging_registry.has(ntype):
257
- reg_schema = global_staging_registry.get(ntype)
258
- except Exception:
259
- pass
260
- if reg_schema:
261
- schema_ports = getattr(reg_schema, port_key, [])
262
- if schema_ports:
263
- res_ports = [p.model_dump() if hasattr(p, "model_dump") else deepcopy(p) for p in schema_ports]
264
- if port_key == "outputs":
265
- p_dict = node.get("parameters") or data.get("parameters") or {}
266
- specific_type = p_dict.get("const_type") or p_dict.get("var_type")
267
- if specific_type:
268
- for p in res_ports:
269
- if p.get("id") in ("val", "result", "out", "value", "out_val"):
270
- p["type_hint"] = specific_type
271
- p["type"] = specific_type
272
- return res_ports
273
-
274
- # 2. Dynamic or client spec fallback
275
- spec = data.get("spec") or data.get("schema") or node.get("spec") or {}
276
- ports = spec.get(port_key) or data.get(port_key) or node.get(port_key)
277
- if isinstance(ports, list):
278
- return [p for p in ports if isinstance(p, dict)]
279
- return []
280
-
281
- # Step 3.6.2: Validate port direction (cannot connect from an input or to an output)
282
- if src_node:
283
- src_outputs = _extract_node_port_list(src_node, "outputs")
284
- is_output = any(out.get("id") == from_port or out.get("name") == from_port for out in src_outputs)
285
- if not is_output:
286
- src_inputs = _extract_node_port_list(src_node, "inputs")
287
- if any(inp.get("id") == from_port or inp.get("name") == from_port for inp in src_inputs):
288
- return {
289
- "valid": False,
290
- "code": "VAL-PORT-DIRECTION",
291
- "message": f"Direction Violation: Port '{from_port}' on source node '{src_label}' is an input port, but connections must originate from an output port.",
292
- "source_node": from_node_id,
293
- "target_node": to_node_id,
294
- }
295
-
296
- if tgt_node:
297
- tgt_inputs = _extract_node_port_list(tgt_node, "inputs")
298
- is_input = any(inp.get("id") == to_port or inp.get("name") == to_port for inp in tgt_inputs)
299
- if not is_input:
300
- tgt_outputs = _extract_node_port_list(tgt_node, "outputs")
301
- if any(out.get("id") == to_port or out.get("name") == to_port for out in tgt_outputs):
302
- return {
303
- "valid": False,
304
- "code": "VAL-PORT-DIRECTION",
305
- "message": f"Direction Violation: Port '{to_port}' on target node '{tgt_label}' is an output port, but connections must terminate at an input port.",
306
- "source_node": from_node_id,
307
- "target_node": to_node_id,
308
- }
309
-
310
- if src_node:
311
- outputs = _extract_node_port_list(src_node, "outputs")
312
- found_out = None
313
- for out in outputs:
314
- if out.get("id") == from_port or out.get("name") == from_port:
315
- found_out = out
316
- break
317
-
318
- if not found_out and from_port in ("out", "result", "output", "default", "val", "value"):
319
- for out in outputs:
320
- if out.get("id") in ("out", "result", "output", "default", "val", "value"):
321
- found_out = out
322
- break
323
- if not found_out and len(outputs) == 1:
324
- found_out = outputs[0]
325
-
326
- if found_out:
327
- src_type_str = found_out.get("type") or found_out.get("type_hint", "Any")
328
- src_kind = found_out.get("kind", "control" if src_type_str in ("control", "branch", "exec") else "data")
329
- elif outputs:
330
- return {
331
- "valid": False,
332
- "code": "VAL-PORT-NOT-FOUND",
333
- "message": f"Port Resolution Error: Output port '{from_port}' does not exist on node '{src_label}'.",
334
- "source_node": from_node_id,
335
- "target_node": to_node_id,
336
- }
337
-
338
- if tgt_node:
339
- inputs = _extract_node_port_list(tgt_node, "inputs")
340
- found_in = None
341
- for inp in inputs:
342
- if inp.get("id") == to_port or inp.get("name") == to_port:
343
- found_in = inp
344
- break
345
-
346
- if not found_in and to_port in ("in", "input", "x", "val", "default", "value", "message", "data", "text"):
347
- for candidate in ("value", "in", "input", "x", "val", "data", "text", "message"):
348
- for inp in inputs:
349
- if inp.get("id") == candidate or inp.get("name") == candidate:
350
- found_in = inp
351
- break
352
- if found_in:
353
- break
354
- if not found_in:
355
- data_inputs = [i for i in inputs if i.get("kind") != "control" and str(i.get("type", "")).lower() != "control"]
356
- if len(data_inputs) == 1:
357
- found_in = data_inputs[0]
358
-
359
- if found_in:
360
- tgt_type_str = found_in.get("type") or found_in.get("type_hint", "Any")
361
- tgt_kind = found_in.get("kind", "control" if tgt_type_str in ("control", "branch", "exec") else "data")
362
- tgt_multiple = bool(found_in.get("multiple", False))
363
- elif inputs:
364
- return {
365
- "valid": False,
366
- "code": "VAL-PORT-NOT-FOUND",
367
- "message": f"Port Resolution Error: Input port '{to_port}' does not exist on node '{tgt_label}'.",
368
- "source_node": from_node_id,
369
- "target_node": to_node_id,
370
- }
371
-
372
- if not src_type_str:
373
- src_type_str = "Any"
374
- if not tgt_type_str:
375
- tgt_type_str = "Any"
376
-
377
- # Step 3.6.8: Control vs Data port isolation
378
- if src_kind != tgt_kind:
379
- return {
380
- "valid": False,
381
- "code": "VAL-CONTROL-ISOLATION",
382
- "message": f"Control-Flow Violation: Cannot connect {src_kind} output '{from_port}' to {tgt_kind} input '{to_port}'.",
383
- "source_type": src_type_str,
384
- "target_type": tgt_type_str,
385
- "source_node": from_node_id,
386
- "target_node": to_node_id,
387
- }
388
-
389
- canonical_src_port = (found_out.get("id") or found_out.get("name") or from_port) if found_out else from_port
390
- canonical_tgt_port = (found_in.get("id") or found_in.get("name") or to_port) if found_in else to_port
391
-
392
- def _resolve_canonical_handle(node_dict: Optional[Dict[str, Any]], port_key: str, handle_val: Any, fallback: str) -> str:
393
- if not node_dict:
394
- return str(handle_val or fallback).strip().lower()
395
- plist = _extract_node_port_list(node_dict, port_key)
396
- h = str(handle_val or "").strip().lower()
397
- for p in plist:
398
- pid = str(p.get("id") or "").lower()
399
- pname = str(p.get("name") or "").lower()
400
- if h and (h == pid or h == pname):
401
- return str(p.get("id") or p.get("name") or h).lower()
402
- if len(plist) == 1:
403
- return str(plist[0].get("id") or plist[0].get("name") or fallback).lower()
404
- return h or fallback.lower()
405
-
406
- cur_src_canon = _resolve_canonical_handle(src_node, "outputs", canonical_src_port, "out")
407
- cur_tgt_canon = _resolve_canonical_handle(tgt_node, "inputs", canonical_tgt_port, "input")
408
-
409
- # Step 3.6.6: Duplicate edge check (comparing canonical port identities)
410
- if edges:
411
- for e in edges:
412
- if e is connection or (e.get("id") and connection.get("id") and e.get("id") == connection.get("id")):
413
- continue
414
- e_src = e.get("source") or e.get("from_node")
415
- e_tgt = e.get("target") or e.get("to_node")
416
- if e_src == from_node_id and e_tgt == to_node_id:
417
- e_src_h = e.get("sourceHandle") or e.get("from_port") or e.get("source_handle")
418
- e_tgt_h = e.get("targetHandle") or e.get("to_port") or e.get("target_handle")
419
- e_src_canon = _resolve_canonical_handle(src_node, "outputs", e_src_h, "out")
420
- e_tgt_canon = _resolve_canonical_handle(tgt_node, "inputs", e_tgt_h, "input")
421
-
422
- if e_src_canon == cur_src_canon and e_tgt_canon == cur_tgt_canon:
423
- return {
424
- "valid": False,
425
- "code": "VAL-DUPLICATE-CONNECTION",
426
- "message": f"Duplicate Connection: An edge already exists connecting [{src_label}:{from_port}] to [{tgt_label}:{to_port}].",
427
- "source_node": from_node_id,
428
- "target_node": to_node_id,
429
- }
430
-
431
- # Step 3.6.5: Single-connection multiplicity check (outputs allow fan-out; inputs enforce multiple: false)
432
- if edges and not tgt_multiple:
433
- for e in edges:
434
- if e is connection or (e.get("id") and connection.get("id") and e.get("id") == connection.get("id")):
435
- continue
436
- e_tgt = e.get("target") or e.get("to_node")
437
- if e_tgt == to_node_id:
438
- e_tgt_h = e.get("targetHandle") or e.get("to_port") or e.get("target_handle")
439
- e_tgt_canon = _resolve_canonical_handle(tgt_node, "inputs", e_tgt_h, "input")
440
-
441
- if e_tgt_canon == cur_tgt_canon:
442
- return {
443
- "valid": False,
444
- "code": "VAL-PORT-MULTIPLICITY",
445
- "message": f"Port Multiplicity Error: Input port '{to_port}' on node '{tgt_label}' only allows a single connection and is already connected.",
446
- "source_node": from_node_id,
447
- "target_node": to_node_id,
448
- }
449
-
450
- # Step 3.6.7: DAG Cycle Detection
451
- if edges and not skip_cycle_check:
452
- adj: Dict[str, List[str]] = {}
453
- for e in edges:
454
- if e is connection or (e.get("id") and connection.get("id") and e.get("id") == connection.get("id")):
455
- continue
456
- e_s = e.get("source") or e.get("from_node")
457
- e_t = e.get("target") or e.get("to_node")
458
- if e_s and e_t:
459
- adj.setdefault(e_s, []).append(e_t)
460
-
461
- # Check if from_node_id is reachable from to_node_id
462
- queue = deque([to_node_id])
463
- visited = set()
464
- while queue:
465
- curr = queue.popleft()
466
- if curr == from_node_id:
467
- if is_loop_header_node(tgt_node):
468
- return {
469
- "valid": False,
470
- "code": "CTRL-001",
471
- "message": f"[CTRL-001] Invalid Loop Feedback: Connecting [{src_label}] -> [{tgt_label}] creates an invalid feedback wire targeting loop header '{tgt_label}'. ALLI loop constructs manage iterations internally without backward feedback wires.",
472
- "source_node": from_node_id,
473
- "target_node": to_node_id,
474
- }
475
- if is_approved_agent_node(src_node) and is_approved_agent_node(tgt_node):
476
- # Approved agent cycle connection allowed
477
- break
478
- return {
479
- "valid": False,
480
- "code": "DAG-001",
481
- "message": f"[DAG-001] Dependency Cycle Detected: Connecting [{src_label}] -> [{tgt_label}] creates an ordinary execution cycle in the DAG.",
482
- "source_node": from_node_id,
483
- "target_node": to_node_id,
484
- }
485
- if curr not in visited:
486
- visited.add(curr)
487
- for nxt in adj.get(curr, []):
488
- if nxt not in visited:
489
- queue.append(nxt)
490
-
491
- # Step 3.6.3 & 3.6.4: Strict type assignability
492
- type_res = validate_types_logic(src_type_str, tgt_type_str)
493
- if not type_res["valid"]:
494
- return {
495
- "valid": False,
496
- "code": "VAL-TYPE-MISMATCH",
497
- "message": f"[{src_label}:{from_port}] -> [{tgt_label}:{to_port}]: {type_res['message']}",
498
- "source_type": src_type_str,
499
- "target_type": tgt_type_str,
500
- "source_node": from_node_id,
501
- "target_node": to_node_id,
502
- "requires_cast": type_res.get("requires_cast", False),
503
- }
504
-
505
- return {
506
- "valid": True,
507
- "code": "VAL-OK",
508
- "message": f"Valid: Connection from [{src_label}:{from_port}] ({src_type_str}) to [{tgt_label}:{to_port}] ({tgt_type_str}) is valid.",
509
- "source_type": src_type_str,
510
- "target_type": tgt_type_str,
511
- "warning": type_res.get("warning"),
512
- "source_node": from_node_id,
513
- "target_node": to_node_id,
514
- "source_port": from_port,
515
- "target_port": to_port,
516
- }
517
-
518
-
519
- def is_valid_node_id(node_id: Any) -> Tuple[bool, Optional[str]]:
520
- r"""
521
- Validates node identifier format conforming to Phase 5 Step 5.2.
522
- Node IDs must be non-empty strings, <= 128 characters, matching ^[a-zA-Z0-9_\-\.:#@/]+$,
523
- and without leading/trailing whitespace, newlines, quotes, or control characters.
524
- Returns (is_valid, error_code).
525
- """
526
- if node_id is None:
527
- return False, "VAL-NODE-ID-EMPTY"
528
- if not isinstance(node_id, (str, int)):
529
- return False, "VAL-NODE-ID-MALFORMED"
530
- id_str = str(node_id)
531
- if not id_str.strip():
532
- return False, "VAL-NODE-ID-EMPTY"
533
- if id_str != id_str.strip():
534
- return False, "VAL-NODE-ID-MALFORMED"
535
- if len(id_str) > 128:
536
- return False, "VAL-NODE-ID-MALFORMED"
537
- if any(c in id_str for c in ('"', "'", "`", "\\", "<", ">", "{", "}", "\n", "\r", "\t", "\0")):
538
- return False, "VAL-NODE-ID-MALFORMED"
539
- if not re.match(r"^[a-zA-Z0-9_\-\.:#@/]+$", id_str):
540
- return False, "VAL-NODE-ID-MALFORMED"
541
- return True, None
542
-
543
-
544
- def is_configured_port_value(val: Any, expected_type: str = "any", min_length: Optional[int] = None) -> bool:
545
- """
546
- Evaluates whether a value in customData/parameters counts as a configured value for a port.
547
- Preserves 0, 0.0, False, [], {} as valid configured values.
548
- For string/any ports: empty string "" is a configured value unless min_length > 0.
549
- For numeric ports: empty string or whitespace is unconfigured.
550
- Non-finite numbers (NaN, Inf) are unconfigured.
551
- None is unconfigured.
552
- """
553
- if val is None:
554
- return False
555
- if isinstance(val, bool):
556
- return True
557
- if isinstance(val, (int, float)):
558
- import math
559
- if math.isnan(val) or math.isinf(val):
560
- return False
561
- return True
562
- if isinstance(val, (list, dict, set, tuple)):
563
- return True
564
- if isinstance(val, str):
565
- t_clean = expected_type.lower().strip()
566
- if t_clean in ("int", "float", "number"):
567
- if not val.strip():
568
- return False
569
- try:
570
- num = float(val)
571
- import math
572
- if math.isnan(num) or math.isinf(num):
573
- return False
574
- return True
575
- except ValueError:
576
- return False
577
- if t_clean == "bool":
578
- if not val.strip():
579
- return False
580
- return val.lower().strip() in ("true", "false", "0", "1")
581
- # String or Any type
582
- if min_length is not None and min_length > 0:
583
- return len(val) >= min_length
584
- return True
585
- return True
586
-
587
 
588
  def validate_workflow_protocol_logic(
589
  graph: Dict[str, Any],
590
-
591
  execution_exit_code: Optional[int] = None,
592
  ) -> Dict[str, Any]:
593
- """
594
- Phase 3.9: Complete Workflow-Level Validation Engine.
595
- Implements:
596
- - Rule 3.9.1: Valid graph structure & node/edge reference integrity (duplicate node IDs, broken edges, port resolution).
597
- - Rule 3.9.2: Required inputs check (every required input port must be connected or have a configured value).
598
- - Rule 3.9.3: Parameter value constraints & type conformance (numeric types, division by zero, math domain, Python identifiers).
599
- - Rule 3.9.4: Registered execution handler / node schema check.
600
- - Rule 3.9.5: Execution model conformance & whole-graph DAG cycle detection via Kahn's algorithm (DAG-001).
601
- - Rule 3.9.6: Declared dependency validation (flagging undeclared external libraries).
602
- - Rule 3.9.7: Pinpoint diagnostics with node_id and port_id.
603
- - Rule 3.9.8: Execution gating (can_execute: False on fatal errors).
604
- """
605
- import re
606
- import keyword
607
- from collections import deque, defaultdict
608
- from language_core.node_schema import default_registry
609
- from language_core.parameter_validator import (
610
- is_non_finite_number,
611
- validate_parameter_value,
612
- validate_python_identifier,
613
- )
614
-
615
- STD_LIBS = {
616
- "math", "json", "sys", "os", "re", "time", "datetime", "random",
617
- "collections", "itertools", "functools", "urllib", "typing", "string",
618
- "copy", "hashlib", "base64", "io", "pathlib", "subprocess", "logging",
619
- "shutil", "csv", "tempfile", "traceback", "struct", "unicodedata",
620
- "core", "operator"
621
- }
622
 
623
  raw_nodes = graph.get("nodes", [])
624
  raw_edges = graph.get("edges") if graph.get("edges") is not None else (graph.get("connections") or [])
625
 
626
- # Exclude visual group frames from DAG execution
627
  nodes = [
628
  n for n in raw_nodes
629
- if str(n.get("node_type") or n.get("type") or n.get("data", {}).get("node_type") or "") not in ("system.workflow_group", "workflow_group", "workflowGroup")
 
630
  ]
631
  edges = [
632
  e for e in raw_edges
633
  if not str(e.get("source", "")).startswith("group_") and not str(e.get("target", "")).startswith("group_")
634
  ]
635
 
636
- from language_core.workflow_migration import migrate_workflow
637
- if graph and isinstance(graph, dict):
638
- try:
639
- graph = migrate_workflow(graph)
640
- except Exception:
641
- pass
642
  diagnostics: List[Dict[str, Any]] = []
643
  possibility_map = {
644
  "Start Validation": {"errors": 0, "warnings": 0, "summary": "Valid starter trigger"},
@@ -663,8 +82,8 @@ def validate_workflow_protocol_logic(
663
  node_id: Optional[str] = None,
664
  port_id: Optional[str] = None,
665
  edge_id: Optional[str] = None,
666
- fix: Optional[str] = None
667
- ):
668
  diagnostics.append({
669
  "code": code,
670
  "severity": severity,
@@ -682,1016 +101,76 @@ def validate_workflow_protocol_logic(
682
  elif severity == "warning":
683
  possibility_map[category]["warnings"] += 1
684
 
685
- # ─── 0. EMPTY GRAPH CHECK ────────────────────────────────────────────────
686
  if not nodes:
687
- add_diag(
688
- "warning",
689
- "Start Validation",
690
- "VAL-001",
691
- "Canvas is empty. Add nodes to build a workflow.",
692
- 1,
693
- fix="Insert nodes via the library palette or context menu."
694
- )
695
- return {
696
- "valid": False,
697
- "status": "Warning",
698
- "can_execute": False,
699
- "error_count": 0,
700
- "warning_count": 1,
701
- "diagnostics": diagnostics,
702
- "possibilities": possibility_map,
703
- }
704
 
705
- # ─── RULE 5.1 & 3.9.1: NODE IDENTITY INTEGRITY & DUPLICATE CHECK ───────
706
- id_counts = defaultdict(int)
707
  node_map: Dict[str, Dict[str, Any]] = {}
708
-
709
  for n in nodes:
710
  raw_id = n.get("id")
711
  lbl = str(n.get("data", {}).get("label") or n.get("node_type") or "Node")
712
- is_valid, err_code = is_valid_node_id(raw_id)
713
- if not is_valid:
714
  if err_code == "VAL-NODE-ID-EMPTY":
715
- add_diag(
716
- "error",
717
- "Node Validation",
718
- "VAL-NODE-ID-EMPTY",
719
- f"Empty Node ID: Node '{lbl}' has a missing, null, or empty identifier. Every node must have a non-empty string ID.",
720
- 2,
721
- node_id="",
722
- fix="Assign a unique, non-empty identifier to each node in the workflow."
723
- )
724
  else:
725
- id_display = str(raw_id) if raw_id is not None else ""
726
- add_diag(
727
- "error",
728
- "Node Validation",
729
- "VAL-NODE-ID-MALFORMED",
730
- f"Malformed Node ID: Identifier '{id_display}' on node '{lbl}' contains invalid characters, whitespace, or exceeds 128 characters. Allowed characters: alphanumeric, '_', '-', '.', ':', '#', '@', '/'.",
731
- 2,
732
- node_id=id_display,
733
- fix="Use a valid alphanumeric identifier without whitespace, quotes, newlines, or illegal characters."
734
- )
735
  continue
736
-
737
  id_str = str(raw_id)
738
  id_counts[id_str] += 1
739
- if id_str not in node_map:
740
- node_map[id_str] = n
741
 
742
  for nid, count in id_counts.items():
743
  if count > 1:
744
- add_diag(
745
- "error",
746
- "Node Validation",
747
- "VAL-DUPLICATE-NODE-ID",
748
- f"Duplicate Node ID: Multiple nodes ({count}) share the identifier '{nid}'. Each node in the workflow must have a unique ID.",
749
- 2,
750
- node_id=nid,
751
- fix="Assign unique IDs to each node in the workflow."
752
- )
753
 
754
  incoming_map: Dict[str, List[Dict[str, Any]]] = {nid: [] for nid in node_map}
755
  outgoing_map: Dict[str, List[Dict[str, Any]]] = {nid: [] for nid in node_map}
756
-
757
- # ─── RULE 3.9.1 & 3.6: EDGE RESOLUTION & PORT CONNECTIONS ────────────────
758
  for edge in edges:
759
  src = edge.get("source") or edge.get("from_node")
760
  tgt = edge.get("target") or edge.get("to_node")
761
  src_port = edge.get("sourceHandle") or edge.get("from_port") or edge.get("source_handle") or "out"
762
  tgt_port = edge.get("targetHandle") or edge.get("to_port") or edge.get("target_handle") or "input"
763
-
764
  if src not in node_map:
765
- add_diag(
766
- "error",
767
- "Connection Validation",
768
- "VAL-003",
769
- f"Broken Connection: Edge connects from nonexistent source node '{src}'.",
770
- 3,
771
- port_id=src_port,
772
- fix="Delete the broken connection wire."
773
- )
774
  continue
775
  if tgt not in node_map:
776
- add_diag(
777
- "error",
778
- "Connection Validation",
779
- "VAL-004",
780
- f"Broken Connection: Edge connects to nonexistent target node '{tgt}'.",
781
- 3,
782
- port_id=tgt_port,
783
- fix="Delete the broken connection wire."
784
- )
785
  continue
786
-
787
  outgoing_map[src].append(edge)
788
  incoming_map[tgt].append(edge)
789
-
790
- # Full port compatibility, direction, multiplicity, and cycle check on the edge
791
  conn_res = validate_connection_logic(edge, nodes_map=node_map, edges=edges, skip_cycle_check=True)
792
  edge_id = edge.get("id")
793
  if not conn_res.get("valid", True):
794
- add_diag(
795
- "error",
796
- "Connection Validation",
797
- conn_res.get("code", "VAL-007"),
798
- conn_res.get("message", "Port connection validation failed."),
799
- 3,
800
- node_id=tgt,
801
- port_id=tgt_port,
802
- edge_id=edge_id,
803
- fix="Ensure compatible port types, avoid duplicate connections, and connect output to input."
804
- )
805
  elif conn_res.get("warning"):
806
- add_diag(
807
- "warning",
808
- "Connection Typing",
809
- "WARN-CONV",
810
- conn_res["warning"],
811
- 1,
812
- node_id=tgt,
813
- port_id=tgt_port,
814
- edge_id=edge_id,
815
- fix="Verify runtime types match expected contract."
816
- )
817
-
818
- # ─── RULE 3.9.5: WHOLE-GRAPH DAG CYCLE DETECTION (KAHN'S ALGORITHM) ───────
819
- # Detect cyclical loops across the entire graph.
820
- # GAP-06: Agent reasoning cycles are permitted ONLY when structurally contained within approved agent constructs.
821
- # GAP-12: Loop header feedback wires emit CTRL-001.
822
- if len(nodes) > 1:
823
- in_degrees = {nid: 0 for nid in node_map}
824
- adj_list = defaultdict(list)
825
- edge_map_by_pair = defaultdict(list)
826
- for e in edges:
827
- s = e.get("source") or e.get("from_node")
828
- t = e.get("target") or e.get("to_node")
829
- if s in in_degrees and t in in_degrees:
830
- adj_list[s].append(t)
831
- edge_map_by_pair[(s, t)].append(e)
832
- in_degrees[t] += 1
833
-
834
- queue = deque([nid for nid, deg in in_degrees.items() if deg == 0])
835
- processed_count = 0
836
- while queue:
837
- curr = queue.popleft()
838
- processed_count += 1
839
- for neighbor in adj_list.get(curr, []):
840
- in_degrees[neighbor] -= 1
841
- if in_degrees[neighbor] == 0:
842
- queue.append(neighbor)
843
-
844
- if processed_count < len(node_map) and edges:
845
- # Graph contains one or more cycles! Find cycle paths with DFS
846
- cycle_nodes = [nid for nid, deg in in_degrees.items() if deg > 0]
847
- visited_dfs = set()
848
- found_cycles: List[List[str]] = []
849
-
850
- for start_cn in cycle_nodes:
851
- if start_cn in visited_dfs:
852
- continue
853
- c_path: List[str] = []
854
- rec_stack: List[str] = []
855
-
856
- def dfs_find_cycle(node: str) -> bool:
857
- visited_dfs.add(node)
858
- rec_stack.append(node)
859
- for neighbor in adj_list.get(node, []):
860
- if neighbor in cycle_nodes:
861
- if neighbor in rec_stack:
862
- idx = rec_stack.index(neighbor)
863
- c_path.extend(rec_stack[idx:] + [neighbor])
864
- return True
865
- elif neighbor not in visited_dfs:
866
- if dfs_find_cycle(neighbor):
867
- return True
868
- rec_stack.pop()
869
- return False
870
-
871
- if dfs_find_cycle(start_cn) and c_path:
872
- found_cycles.append(c_path)
873
-
874
- if not found_cycles and cycle_nodes:
875
- found_cycles.append(cycle_nodes[:3])
876
-
877
- for c_path in found_cycles:
878
- # 1. Check for Loop Header feedback wires (GAP-12 -> CTRL-001)
879
- loop_header_edge = None
880
- for i in range(len(c_path) - 1):
881
- u_id = c_path[i]
882
- v_id = c_path[i + 1]
883
- v_node = node_map.get(v_id, {})
884
- if is_loop_header_node(v_node):
885
- matched_edges = edge_map_by_pair.get((u_id, v_id), [{}])
886
- matched_edge = matched_edges[0] if matched_edges else {}
887
- loop_header_edge = (
888
- u_id,
889
- v_id,
890
- matched_edge.get("id"),
891
- matched_edge.get("target_handle") or matched_edge.get("targetHandle") or matched_edge.get("to_port"),
892
- )
893
- break
894
-
895
- if loop_header_edge:
896
- u_id, v_id, eid, tgt_p = loop_header_edge
897
- u_lbl = node_map.get(u_id, {}).get("data", {}).get("label") or u_id
898
- v_lbl = node_map.get(v_id, {}).get("data", {}).get("label") or v_id
899
- add_diag(
900
- "error",
901
- "Control Flow",
902
- "CTRL-001",
903
- f"[CTRL-001] Invalid Loop Feedback: Feedback wire from '{u_lbl}' ({u_id}) to loop header '{v_lbl}' ({v_id}) violates ALLI loop topology. ALLI loop constructs manage iterations internally without backward feedback wires. Connect downstream body nodes directly from loop outputs without looping back.",
904
- 4,
905
- node_id=v_id,
906
- port_id=tgt_p,
907
- edge_id=eid,
908
- fix="Remove the backward feedback edge to the loop header. Use the loop node's internal iteration controls and downstream body flow."
909
- )
910
- continue
911
-
912
- # 2. Check for structurally contained approved agent reasoning loop (GAP-06)
913
- is_approved_agent_cycle = (
914
- len(c_path) >= 2
915
- and all(is_approved_agent_node(node_map.get(nid, {})) for nid in c_path)
916
- and any(is_agent_reasoner_or_memory(node_map.get(nid, {})) for nid in c_path)
917
- )
918
-
919
- if is_approved_agent_cycle:
920
- # Permitted LangGraph ReAct reasoning cycle
921
- continue
922
-
923
- # 3. Unintended procedural or malformed cycle (GAP-06 -> DAG-001)
924
- cycle_labels = [
925
- node_map.get(cid, {}).get("data", {}).get("label") or cid
926
- for cid in c_path
927
- ]
928
- cycle_str = " -> ".join(cycle_labels) if cycle_labels else "circular dependency"
929
- add_diag(
930
- "error",
931
- "Connection Validation",
932
- "DAG-001",
933
- f"[DAG-001] Dependency Cycle Detected: Workflow contains a circular dependency loop: {cycle_str}. Procedural Python workflows require a Directed Acyclic Graph (DAG).",
934
- 3,
935
- node_id=c_path[0] if c_path else None,
936
- fix="Remove circular feedback connections or wrap the iteration inside a For/While Loop node."
937
- )
938
-
939
- # ─── RULE 3.9.5: START VALIDATION / ENTRYPOINT ───────────────────────────
940
- EXPLICIT_TRIGGER_TYPES = {
941
- "workflow.start",
942
- "trigger.schedule",
943
- "trigger.webhook",
944
- "start",
945
- "schedule",
946
- "webhook",
947
- }
948
-
949
- OPERATION_NODE_TYPES = {
950
- "api.http_request",
951
- "http_request",
952
- "http",
953
- "api_request",
954
- "api.request",
955
- }
956
-
957
- def _get_node_type_and_label(node_dict: Dict[str, Any]) -> Tuple[str, str]:
958
- d = node_dict.get("data") or {}
959
- raw_type = str(
960
- node_dict.get("node_type")
961
- or d.get("node_type")
962
- or d.get("type")
963
- or node_dict.get("type")
964
- or ""
965
- ).strip().lower()
966
- clean_type = raw_type.removeprefix("alli.")
967
- lbl = str(d.get("label") or node_dict.get("label") or "").strip().lower()
968
- return clean_type, lbl
969
-
970
- def is_explicit_trigger_node(node_dict: Dict[str, Any]) -> bool:
971
- if node_dict.get("is_alternative_entry") or node_dict.get("is_alternative_trigger"):
972
- return False
973
- params = node_dict.get("parameters") if isinstance(node_dict.get("parameters"), dict) else {}
974
- data = node_dict.get("data") if isinstance(node_dict.get("data"), dict) else {}
975
- custom = node_dict.get("customData") if isinstance(node_dict.get("customData"), dict) else {}
976
- combined = {**data, **custom, **params}
977
- if combined.get("is_alternative_entry") or combined.get("is_alternative_trigger") or combined.get("active") is False or combined.get("disabled"):
978
- return False
979
- clean_type, lbl = _get_node_type_and_label(node_dict)
980
- if clean_type in EXPLICIT_TRIGGER_TYPES:
981
- return True
982
- if clean_type in ("custom", "customnode", "default", "") and lbl in (
983
- "start", "schedule trigger", "webhook trigger", "schedule", "webhook"
984
- ):
985
- return True
986
- return False
987
 
988
- def is_operation_node(node_dict: Dict[str, Any]) -> bool:
989
- clean_type, lbl = _get_node_type_and_label(node_dict)
990
- if clean_type in OPERATION_NODE_TYPES:
991
- return True
992
- if clean_type.startswith("api.") or "http_request" in clean_type or clean_type == "http":
993
- return True
994
- if "http request" in lbl or lbl == "http":
995
- return True
996
- return False
997
 
998
- # 1. Identify all explicit trigger/start nodes in the entire graph
999
- explicit_trigger_nodes = [n for n in nodes if is_explicit_trigger_node(n)]
1000
-
1001
- # 2. Reject multiple / conflicting explicit entrypoints
1002
- if len(explicit_trigger_nodes) > 1:
1003
- second_entry = explicit_trigger_nodes[1]
1004
- add_diag(
1005
- "error",
1006
- "Start Validation",
1007
- "VAL-MULTIPLE-START",
1008
- "Multiple workflow entrypoints detected. A workflow must have exactly one Start or Trigger entrypoint.",
1009
- 1,
1010
- node_id=second_entry.get("id"),
1011
- fix="Remove extra Start or Trigger nodes so that the workflow has exactly one entrypoint."
1012
- )
1013
-
1014
- # 3. Check for presence of at least one valid trigger/entrypoint
1015
- root_nodes = [nid for nid, inc in incoming_map.items() if len(inc) == 0]
1016
- BACKCOMPAT_STARTER_TYPES = (
1017
- "variable", "constant", "input", "start", "webhook", "schedule",
1018
- "manual_trigger", "prompt", "datetime", "random", "file", "import", "math"
1019
- )
1020
-
1021
- def is_backcompat_starter_node(node_dict: Dict[str, Any]) -> bool:
1022
- if is_operation_node(node_dict):
1023
- return False
1024
- clean_type, lbl = _get_node_type_and_label(node_dict)
1025
- return (
1026
- any(trig in clean_type or trig in lbl for trig in BACKCOMPAT_STARTER_TYPES)
1027
- or (len(outgoing_map.get(node_dict.get("id", ""), [])) > 0 and not is_operation_node(node_dict))
1028
- or (len(nodes) == 1 and not is_operation_node(node_dict))
1029
- )
1030
-
1031
- if len(explicit_trigger_nodes) == 1:
1032
- has_trigger = True
1033
- elif len(explicit_trigger_nodes) > 1:
1034
- # Multiple triggers is an error, already recorded in diagnostics above
1035
- has_trigger = True
1036
- else:
1037
- # No explicit trigger nodes: evaluate backwards compatibility across root nodes
1038
- has_trigger = any(is_backcompat_starter_node(node_map[nid]) for nid in root_nodes if nid in node_map)
1039
-
1040
- if not has_trigger and len(nodes) > 0:
1041
- add_diag(
1042
- "error",
1043
- "Start Validation",
1044
- "RULE-101",
1045
- "Missing trigger or entrypoint. Workflow must have an entrypoint node (Variable, Constant, Input, Webhook).",
1046
- 1,
1047
- node_id=nodes[0].get("id"),
1048
- fix="Add an Input Prompt, Constant, or Variable node to initiate workflow execution."
1049
- )
1050
- # ─── HELPER: RESOLVE NODE INPUT DEFINITIONS (AUTHORITATIVE FIRST) ────────
1051
- def resolve_inputs_for_node(n: Dict[str, Any]) -> List[Dict[str, Any]]:
1052
- d = n.get("data", {})
1053
- ntype = str(n.get("node_type") or n.get("type") or d.get("node_type") or "")
1054
-
1055
- # Authoritative registry lookup takes precedence over client-submitted inputs
1056
- reg_schema = default_registry.get(ntype)
1057
- if not reg_schema:
1058
- try:
1059
- from library_inspector.node_synthesizer import global_staging_registry
1060
- reg_schema = global_staging_registry.get(ntype)
1061
- except Exception:
1062
- pass
1063
- if reg_schema:
1064
- return [
1065
- {
1066
- "id": p.id,
1067
- "name": p.name,
1068
- "type": p.type,
1069
- "required": p.required,
1070
- "default_value": p.default_value,
1071
- }
1072
- for p in reg_schema.inputs
1073
- ]
1074
-
1075
- spec = d.get("spec", {})
1076
- inputs = spec.get("inputs") or d.get("inputs") or n.get("inputs")
1077
- if inputs and isinstance(inputs, list):
1078
- return inputs
1079
-
1080
- t = ntype.lower()
1081
- lbl = str(d.get("label") or "").lower()
1082
- if "constant" in t or t.startswith("constant.") or t in ("input", "constant"):
1083
- return []
1084
- if "math.sqrt" in t or "sqrt" in lbl:
1085
- return [{"id": "x", "name": "x", "type": "float", "required": True}]
1086
- if "binary" in t or "calc" in lbl or "add" in lbl or "operator" in t:
1087
- return [
1088
- {"id": "a", "name": "a", "type": "any", "required": True},
1089
- {"id": "b", "name": "b", "type": "any", "required": True},
1090
- ]
1091
- if "if" in t or ("condition" in lbl and "true" not in lbl and "false" not in lbl):
1092
- return [{"id": "condition", "name": "condition", "type": "bool", "required": True}]
1093
- if "print" in t or "display" in t or "io.print" in t:
1094
- return [{"id": "value", "name": "value", "type": "any", "required": True}]
1095
- if "for_loop" in t:
1096
- return [{"id": "iterable", "name": "iterable", "type": "any", "required": True}]
1097
- if "while_loop" in t:
1098
- return [{"id": "condition", "name": "condition", "type": "bool", "required": True}]
1099
- return []
1100
-
1101
- # ─── HELPER: KNOWN NODE TYPE CHECK ───────────────────────────────────────
1102
- def is_known_type(ntype: str) -> bool:
1103
- t = ntype.lower().strip()
1104
- if not t:
1105
- return False
1106
- if default_registry.get(t):
1107
- return True
1108
- known_prefixes = (
1109
- "cat_", "cat.", "fundamentals.", "alli.primitive.", "alli.func.", "alli.oop.", "alli.sys.", "alli.agent.", "alli.lang.",
1110
- "primitive.", "func.", "oop.", "sys.", "agent.", "constant.", "control.", "io.", "math.",
1111
- "core.", "operator.", "workflow.", "trigger.", "api.", "file.", "json.", "csv.", "data.",
1112
- "lang.", "module.", "db."
1113
- )
1114
- if any(t.startswith(p) for p in known_prefixes):
1115
- return True
1116
- known_exact = {
1117
- "variable", "constant", "binary_op", "operator", "math", "input", "output",
1118
- "display.output", "print", "print_output", "if_else", "condition", "control.branch",
1119
- "control.if_else", "for_loop", "while_loop", "loop", "function", "function.def",
1120
- "function.call", "class", "class_def", "try_except", "try_catch", "start",
1121
- "workflow_group", "system.workflow_group", "group", "webhook", "schedule",
1122
- "manual_trigger", "text_prompt", "file_input", "http_request", "response_json", "api_response",
1123
- "lang.import", "module.import", "core.import", "core.scope", "db.query", "sys.file",
1124
- "numpy.array", "pandas.dataframe"
1125
- }
1126
- if t in known_exact:
1127
- return True
1128
- if "." in t:
1129
- parts = t.split(".")
1130
- mod = parts[0].lower()
1131
- if mod in STD_LIBS or mod in ("alli", "system", "primitive", "constant", "control", "io", "math", "custom", "display", "core", "operator", "lang", "module", "data", "agent", "db", "sys") or mod in graph_deps:
1132
- return True
1133
- return False
1134
-
1135
- # Secret scanning regex
1136
- secret_pat = re.compile(
1137
- r'(?:sk-[a-zA-Z0-9]{20,}|Bearer\s+[a-zA-Z0-9_\-\.]{25,}|ghp_[a-zA-Z0-9]{30,}|password\s*=\s*["\'][^"\']+["\'])',
1138
- re.IGNORECASE
1139
- )
1140
-
1141
- # Dependencies declared in graph or requirements
1142
- graph_deps = set(graph.get("dependencies") or [])
1143
  req_txt = graph.get("compiled_artifacts", {}).get("requirements_txt", "")
1144
  for rline in req_txt.splitlines():
1145
  pkg_cleaned = rline.strip().split("==")[0].split(">=")[0].split("<=")[0].strip().lower()
1146
  if pkg_cleaned:
1147
  graph_deps.add(pkg_cleaned)
1148
 
1149
- # ─── RULES 3.9.2, 3.9.3, 3.9.4, 3.9.6: PER-NODE VALIDATION ─────────────
1150
- for n in nodes:
1151
- nid = n.get("id")
1152
- d = n.get("data", {}) if isinstance(n.get("data"), dict) else {}
1153
- custom = d.get("customData") if isinstance(d.get("customData"), dict) else {}
1154
- top_params = n.get("parameters") if isinstance(n.get("parameters"), dict) else (n.get("params") if isinstance(n.get("params"), dict) else {})
1155
- params = d.get("parameters") if isinstance(d.get("parameters"), dict) else (d.get("params") if isinstance(d.get("params"), dict) else {})
1156
- values = d.get("values") if isinstance(d.get("values"), dict) else {}
1157
- inputs_dict = n.get("inputs") if isinstance(n.get("inputs"), dict) else (d.get("inputs") if isinstance(d.get("inputs"), dict) else {})
1158
- combined_params = {**d, **custom, **values, **inputs_dict, **params, **top_params}
1159
-
1160
- ntype = str(n.get("node_type") or n.get("type") or d.get("node_type") or d.get("nodeType") or "")
1161
- label = str(d.get("label") or ntype or "Node")
1162
-
1163
- # ─── RULE 3.9.4: REGISTERED EXECUTION HANDLER / SCHEMA CHECK ─────────
1164
- from library_inspector.node_synthesizer import global_staging_registry
1165
- node_schema_obj = default_registry.get(ntype) or global_staging_registry.get(ntype)
1166
- staged_rec = global_staging_registry.get_record(ntype) if global_staging_registry.has(ntype) else None
1167
-
1168
- node_caps = None
1169
- if node_schema_obj and hasattr(node_schema_obj, "capability_states") and node_schema_obj.capability_states:
1170
- node_caps = node_schema_obj.capability_states
1171
- elif node_schema_obj and hasattr(node_schema_obj, "verification") and node_schema_obj.verification:
1172
- node_caps = getattr(node_schema_obj.verification, "capability_states", None)
1173
- if hasattr(node_caps, "model_dump"):
1174
- node_caps = node_caps.model_dump()
1175
-
1176
- if not node_caps:
1177
- node_caps = (
1178
- d.get("capability_states")
1179
- or n.get("capability_states")
1180
- or (n.get("parameters") or {}).get("capability_states")
1181
- or (d.get("verification") or {}).get("capability_states")
1182
- or (staged_rec.capability_states if staged_rec else None)
1183
- )
1184
-
1185
- if default_registry.has(ntype) and default_registry.has_executor(ntype):
1186
- # Explicit executor is registered in production registry
1187
- pass
1188
- elif node_caps and node_caps.get("runtime_supported") is False:
1189
- add_diag(
1190
- "error",
1191
- "Execution Validation",
1192
- "EXEC_UNSUPPORTED",
1193
- f"[VAL-UNSUPPORTED-RUNTIME] Execution Unsupported: Node '{label}' ({nid}) of type '{ntype}' is marked runtime_supported=False and cannot be executed in the runtime engine.",
1194
- 8,
1195
- node_id=nid,
1196
- fix="Use a runtime-supported node implementation or export python code for external execution.",
1197
- )
1198
- elif staged_rec and staged_rec.is_supported is False:
1199
- add_diag(
1200
- "error",
1201
- "Execution Validation",
1202
- "EXEC_UNSUPPORTED",
1203
- f"[VAL-UNSUPPORTED-RUNTIME] Execution Unsupported: Staged node '{label}' ({nid}) of type '{ntype}' contains unsupported types or security violations.",
1204
- 8,
1205
- node_id=nid,
1206
- fix="Ensure all port types are supported and security constraints are satisfied.",
1207
- )
1208
- elif global_staging_registry.has(ntype) and not global_staging_registry.is_approved(ntype) and not default_registry.has(ntype):
1209
- add_diag(
1210
- "error",
1211
- "Execution Validation",
1212
- "RULE-NODE-STAGED-UNAPPROVED",
1213
- f"Unapproved Staged Node: Node '{label}' ({nid}) uses introspected staged schema '{ntype}' which has not been authorized for production execution.",
1214
- 8,
1215
- node_id=nid,
1216
- fix="An authorized administrator must approve this staged node before workflow execution.",
1217
- )
1218
- elif global_staging_registry.has(ntype) and global_staging_registry.is_approved(ntype) and not default_registry.has(ntype):
1219
- add_diag(
1220
- "error",
1221
- "Execution Validation",
1222
- "EXEC_UNSUPPORTED",
1223
- f"[VAL-UNSUPPORTED-RUNTIME] Execution Handler Missing: Approved staged node '{label}' ({nid}) of type '{ntype}' has no registered execution handler in runtime registry.",
1224
- 8,
1225
- node_id=nid,
1226
- fix="Register an executable callable handler for this staged node in the execution registry.",
1227
- )
1228
- elif not is_known_type(ntype):
1229
- add_diag(
1230
- "error",
1231
- "Node Validation",
1232
- "VAL-UNKNOWN-NODE-TYPE",
1233
- f"Unregistered Node Type: Node '{label}' ({nid}) specifies unknown type '{ntype}' with no registered execution handler.",
1234
- 2,
1235
- node_id=nid,
1236
- fix="Select a valid standard node schema from the library palette or register its definition."
1237
- )
1238
-
1239
- # ─── RULE 3.9.2: REQUIRED INPUT PORTS CHECK ──────────────────────────
1240
- input_defs = resolve_inputs_for_node(n)
1241
- node_incoming = incoming_map.get(nid, [])
1242
-
1243
- for inp in input_defs:
1244
- is_req = inp.get("required", False)
1245
- port_id = inp.get("id") or inp.get("name")
1246
- port_name = inp.get("name") or port_id
1247
 
1248
- if is_req:
1249
- # Check incoming wire with alias & single-input support
1250
- DATA_PORT_ALIASES = {'in', 'input', 'value', 'val', 'data', 'text', 'x', 'collection', 'items', 'args'}
1251
- port_id_lower = str(port_id).lower()
1252
- port_name_lower = str(port_name).lower()
1253
- is_data_port = port_id_lower in DATA_PORT_ALIASES or port_name_lower in DATA_PORT_ALIASES
1254
- single_required_input = len(input_defs) == 1 or len([inp_item for inp_item in input_defs if inp_item.get('required')]) == 1
1255
-
1256
- def _wire_matches(e):
1257
- tgt = str(e.get('targetHandle') or e.get('to_port') or e.get('target_handle') or '').strip().lower()
1258
- if tgt in (port_id_lower, port_name_lower):
1259
- return True
1260
- from language_core.core_nodes_generated import CORE_PORT_ALIASES
1261
- node_aliases = CORE_PORT_ALIASES.get(ntype.lower(), {}).get('inputs', {})
1262
- canon_from_alias = node_aliases.get(tgt)
1263
- if canon_from_alias and canon_from_alias.lower() in (port_id_lower, port_name_lower):
1264
- return True
1265
- if not tgt and single_required_input:
1266
- return True
1267
- if is_data_port and tgt in DATA_PORT_ALIASES and single_required_input:
1268
- return True
1269
- if port_id_lower == 'value' and tgt in ('in', 'input', 'val', 'data'):
1270
- return True
1271
- if port_id_lower == 'in' and tgt in ('value', 'input', 'val', 'data'):
1272
- return True
1273
- return False
1274
-
1275
- has_wire = any(_wire_matches(e) for e in node_incoming)
1276
- # Check configured value
1277
- port_type = str(inp.get("type") or inp.get("type_hint") or "any")
1278
- has_val = False
1279
- for k in (port_id, port_name, f"param_{port_id}", f"param_{port_name}"):
1280
- if k in combined_params:
1281
- v = combined_params[k]
1282
- if is_configured_port_value(v, expected_type=port_type):
1283
- has_val = True
1284
- break
1285
- if not has_val and "default_value" in inp and inp.get("default_value") is not None:
1286
- if is_configured_port_value(inp["default_value"], expected_type=port_type):
1287
- has_val = True
1288
-
1289
- if not has_wire and not has_val:
1290
- add_diag(
1291
- "error",
1292
- "Data Validation",
1293
- "VAL-REQUIRED-INPUT-MISSING",
1294
- f"Required Input Missing: Port '{port_id}' on node '{label}' ({nid}) is required but unconnected and has no configured value.",
1295
- 6,
1296
- node_id=nid,
1297
- port_id=port_id,
1298
- fix=f"Connect an output wire to port '{port_id}' or provide a parameter value in the configuration drawer."
1299
- )
1300
-
1301
- # ─── RULE 3.9.3: PARAMETER CONSTRAINTS & VALUE CONFORMANCE ───────────
1302
- # Division by zero
1303
- if "binary" in ntype.lower() or "calc" in label.lower() or "operator" in ntype.lower():
1304
- op = combined_params.get("operator") or "+"
1305
- b_val = combined_params.get("b")
1306
- if op in ("/", "//", "%") and (b_val == 0 or b_val == "0"):
1307
- add_diag(
1308
- "error",
1309
- "Expression Validation",
1310
- "MATH-E001",
1311
- f"ZeroDivisionError: Division or modulo by literal zero in node '{label}' ({nid}).",
1312
- 4,
1313
- node_id=nid,
1314
- port_id="b",
1315
- fix="Operand B cannot be zero for division ('/') or modulo ('%')."
1316
- )
1317
-
1318
- # math.sqrt domain error
1319
- if "sqrt" in ntype.lower() or "sqrt" in label.lower():
1320
- x_val = combined_params.get("x")
1321
- if x_val is not None and x_val != "":
1322
- try:
1323
- num_x = float(x_val)
1324
- if num_x < 0:
1325
- add_diag(
1326
- "error",
1327
- "Expression Validation",
1328
- "MATH-E002",
1329
- f"Math Domain Error: Node '{label}' ({nid}) receives negative literal {x_val} for square root.",
1330
- 4,
1331
- node_id=nid,
1332
- port_id="x",
1333
- fix="Square root requires a non-negative number (x >= 0)."
1334
- )
1335
- except ValueError:
1336
- pass
1337
-
1338
- # Type cast literal error
1339
- if ntype in ("core.type_cast", "type_cast"):
1340
- val = combined_params.get("value")
1341
- target_type = str(combined_params.get("target_type") or "int").lower()
1342
- if val is not None and val != "":
1343
- if target_type in ("int", "integer"):
1344
- try:
1345
- int(str(val))
1346
- except ValueError:
1347
- add_diag(
1348
- "error",
1349
- "Data Validation",
1350
- "ERR-TYPE-CAST",
1351
- f"Type Cast Error: Cannot cast literal '{val}' to int on node '{label}' ({nid}).",
1352
- 6,
1353
- node_id=nid,
1354
- port_id="value",
1355
- fix="Provide a valid integer literal."
1356
- )
1357
- elif target_type in ("float", "number"):
1358
- try:
1359
- float(str(val))
1360
- except ValueError:
1361
- add_diag(
1362
- "error",
1363
- "Data Validation",
1364
- "ERR-TYPE-CAST",
1365
- f"Type Cast Error: Cannot cast literal '{val}' to float on node '{label}' ({nid}).",
1366
- 6,
1367
- node_id=nid,
1368
- port_id="value",
1369
- fix="Provide a valid floating point number literal."
1370
- )
1371
-
1372
- # Function call missing required arguments
1373
- if ntype in ("function.call", "func.call"):
1374
- incoming_edges = incoming_map.get(nid, [])
1375
- has_arg_wire = any(
1376
- str(e.get("target_handle") or e.get("targetHandle") or "").lower() in ("arg", "arg_1", "args", "value")
1377
- for e in incoming_edges
1378
- )
1379
- has_arg_param = combined_params.get("arg") is not None and combined_params.get("arg") != ""
1380
- if not has_arg_wire and not has_arg_param:
1381
- add_diag(
1382
- "error",
1383
- "Data Validation",
1384
- "ERR-ARG-MISSING",
1385
- f"Function call on node '{label}' ({nid}) port 'arg' is missing required argument.",
1386
- 6,
1387
- node_id=nid,
1388
- port_id="arg",
1389
- fix="Connect an argument to port 'arg' or configure it in the parameters."
1390
- )
1391
-
1392
- # ─── NON-FINITE NUMERIC VALUES CHECK (GAP-11) ─────────────────
1393
- for pk, pv in combined_params.items():
1394
- found_nf, tok_nf = is_non_finite_number(pv)
1395
- if found_nf:
1396
- add_diag(
1397
- "error",
1398
- "Node Validation",
1399
- "VAL-PARAM-NON-FINITE",
1400
- f"Non-Finite Number Error: Parameter '{pk}' on node '{label}' ({nid}) contains non-finite numeric value '{tok_nf}' (NaN or Infinity). Only finite numeric values are permitted.",
1401
- 2,
1402
- node_id=nid,
1403
- port_id=pk,
1404
- fix=f"Replace '{tok_nf}' with a valid finite number."
1405
- )
1406
-
1407
- # ─── SCHEMA-DRIVEN PARAMETER CONSTRAINTS VALIDATION (GAP-05) ──
1408
- validated_param_ids = set()
1409
- node_schema = default_registry.get(ntype)
1410
- if node_schema and node_schema.parameters:
1411
- for param in node_schema.parameters:
1412
- validated_param_ids.add(param.id)
1413
- if param.id in combined_params:
1414
- pval = combined_params[param.id]
1415
- elif param.id in custom:
1416
- pval = custom[param.id]
1417
- elif f"param_{param.id}" in combined_params:
1418
- pval = combined_params[f"param_{param.id}"]
1419
- else:
1420
- pval = param.default
1421
-
1422
- p_valid, p_code, p_msg, p_fix = validate_parameter_value(
1423
- param, pval, node_label=label, node_id=nid
1424
- )
1425
- if not p_valid:
1426
- add_diag(
1427
- "error",
1428
- "Node Validation",
1429
- p_code or "VAL-PARAM-CONSTRAINT",
1430
- p_msg or f"Parameter '{param.name}' violates declared schema constraint.",
1431
- 2,
1432
- node_id=nid,
1433
- port_id=param.id,
1434
- fix=p_fix
1435
- )
1436
-
1437
- # ─── PYTHON IDENTIFIER VALIDATION (GAP-06) ────────────────────
1438
- is_var_or_const = "variable" in ntype.lower() or "constant" in ntype.lower()
1439
- for id_field in ("var_name", "variable_name", "func_name", "fn_name", "function_name", "class_name", "loop_var", "iter_var", "target_var"):
1440
- if id_field in combined_params and id_field not in validated_param_ids:
1441
- if id_field in ("var_name", "variable_name") and not is_var_or_const:
1442
- continue
1443
- val = combined_params[id_field]
1444
- if val is not None:
1445
- id_valid, id_code, id_msg, id_fix = validate_python_identifier(val, field_id=id_field)
1446
- if not id_valid:
1447
- add_diag(
1448
- "error",
1449
- "Node Validation",
1450
- id_code or "COMP-E101",
1451
- f"{id_msg} on node '{label}' ({nid}).",
1452
- 2,
1453
- node_id=nid,
1454
- port_id=id_field,
1455
- fix=id_fix
1456
- )
1457
-
1458
- # Expression AST verification
1459
- if "condition" in combined_params or "if" in ntype.lower():
1460
- cond = str(combined_params.get("condition") or "True")
1461
- expr_res = validate_expression_logic(cond)
1462
- if not expr_res.get("valid"):
1463
- add_diag(
1464
- "error",
1465
- "Expression Validation",
1466
- "COMP-E102",
1467
- f"Syntax error in condition expression '{cond}': {expr_res.get('error')}",
1468
- 4,
1469
- node_id=nid,
1470
- port_id="condition",
1471
- fix="Check for balanced parentheses, quotes, and valid Python syntax."
1472
- )
1473
-
1474
- # ─── RULE 3.9.6: DECLARED DEPENDENCY VALIDATION ───────────────────────
1475
- pkg_match = re.match(r"^([a-zA-Z0-9_-]+)\.", ntype)
1476
- if pkg_match:
1477
- pkg = pkg_match.group(1).lower()
1478
- if not default_registry.has(ntype) and pkg not in STD_LIBS and pkg not in ("alli", "system", "primitive", "constant", "control", "io", "math", "custom", "display", "core", "data", "function", "string", "oop", "workflow", "trigger", "api", "lang", "module", "agent", "db", "sys"):
1479
- if pkg not in graph_deps:
1480
- add_diag(
1481
- "warning",
1482
- "Dependency Validation",
1483
- "DEP-W001",
1484
- f"Undeclared Dependency: Node '{label}' requires external library '{pkg}', but it is not declared in workflow dependencies.",
1485
- 10,
1486
- node_id=nid,
1487
- fix=f"Add '{pkg}' to workflow dependencies or requirements.txt."
1488
- )
1489
-
1490
- # Secret scanning
1491
- raw_vals = " ".join(str(v) for v in combined_params.values())
1492
- if secret_pat.search(raw_vals):
1493
- add_diag(
1494
- "warning",
1495
- "Credential Validation",
1496
- "SEC-W001",
1497
- f"Possible plaintext secret detected on node '{label}'.",
1498
- 9,
1499
- node_id=nid,
1500
- fix="Use environment variables or runtime parameters instead of hardcoding secrets."
1501
- )
1502
-
1503
- # Static Security Check: Path Traversal Guard (SEC-001)
1504
- if "file" in ntype.lower() or "sys.file" in ntype.lower():
1505
- file_path = str(combined_params.get("file_path") or combined_params.get("path") or "").strip()
1506
- if file_path:
1507
- if (
1508
- ".." in file_path
1509
- or file_path.startswith("/")
1510
- or file_path.startswith("\\")
1511
- or re.match(r"^[a-zA-Z]:", file_path)
1512
- or "\0" in file_path
1513
- ):
1514
- add_diag(
1515
- "error",
1516
- "Credential Validation",
1517
- "ERR-SECURITY-PATH-TRAVERSAL",
1518
- f"[SEC-001] Path Traversal Detected: File path '{file_path}' on node '{label}' ({nid}) attempts to escape the sandboxed workspace. Path must be relative without parent directory ('..') components.",
1519
- 9,
1520
- node_id=nid,
1521
- port_id="file_path",
1522
- fix="Specify a relative path within the workspace directory, without '..' or absolute path prefixes.",
1523
- )
1524
-
1525
- # Static Security Check: SSRF Target Guard (SEC-002 & SEC-003)
1526
- if "http" in ntype.lower() or "api.http_request" in ntype.lower() or "request" in ntype.lower():
1527
- raw_url = str(combined_params.get("url") or "").strip()
1528
- if raw_url:
1529
- from urllib.parse import urlparse
1530
- import ipaddress
1531
- parsed = urlparse(raw_url)
1532
- if parsed.scheme not in ("http", "https"):
1533
- add_diag(
1534
- "error",
1535
- "Credential Validation",
1536
- "SEC-003",
1537
- f"[SEC-003] Protocol '{parsed.scheme}' is not permitted. Only HTTP and HTTPS are allowed.",
1538
- 9,
1539
- node_id=nid,
1540
- port_id="url",
1541
- fix="Use 'http://' or 'https://' URLs.",
1542
- )
1543
- else:
1544
- host = (parsed.hostname or "").strip("[]").lower()
1545
- is_ssrf = False
1546
- if host in ("localhost", "127.0.0.1", "0.0.0.0", "::1", "") or host.endswith(".localhost") or host.endswith(".internal") or host.endswith(".local") or host in ("metadata.google.internal", "instance-data"):
1547
- is_ssrf = True
1548
- if not is_ssrf:
1549
- try:
1550
- ip_obj = ipaddress.ip_address(host)
1551
- if isinstance(ip_obj, ipaddress.IPv6Address) and ip_obj.ipv4_mapped:
1552
- ip_obj = ip_obj.ipv4_mapped
1553
- if ip_obj.is_private or ip_obj.is_loopback or ip_obj.is_link_local or ip_obj.is_multicast or ip_obj.is_reserved or ip_obj.is_unspecified:
1554
- is_ssrf = True
1555
- except ValueError:
1556
- pass
1557
- if is_ssrf:
1558
- add_diag(
1559
- "error",
1560
- "Credential Validation",
1561
- "SEC-002",
1562
- f"[SEC-002] Access to restricted/private address or cloud metadata '{host}' is blocked for security.",
1563
- 9,
1564
- node_id=nid,
1565
- port_id="url",
1566
- fix="Provide a publicly accessible HTTP/HTTPS API endpoint.",
1567
- )
1568
-
1569
- # ─── RULE 3.9.8: EXECUTION FEEDBACK & GATING ─────────────────────────────
1570
  if execution_exit_code is not None:
1571
  if execution_exit_code == 0:
1572
  possibility_map["Execution Validation"]["summary"] = "Sandbox execution passed (Exit 0)."
1573
  else:
1574
- add_diag(
1575
- "error",
1576
- "Execution Validation",
1577
- "EXEC-E001",
1578
- f"Execution failed with Exit Code {execution_exit_code}.",
1579
- 8
1580
- )
1581
 
1582
  errors = sum(p["errors"] for p in possibility_map.values())
1583
  warnings = sum(p["warnings"] for p in possibility_map.values())
 
1584
 
1585
- status = "Valid"
1586
- if errors > 0:
1587
- status = "Error"
1588
- elif warnings > 0:
1589
- status = "Warning"
1590
-
1591
- if execution_exit_code is not None and execution_exit_code != 0:
1592
- status = "Runtime Error"
1593
-
1594
- return {
1595
- "valid": errors == 0,
1596
- "status": status,
1597
- "can_execute": errors == 0,
1598
- "error_count": errors,
1599
- "warning_count": warnings,
1600
- "diagnostics": diagnostics,
1601
- "possibilities": possibility_map,
1602
- }
1603
-
1604
-
1605
-
1606
- def validate_node_metadata_logic(node_data: Union[NodeDefinition, Dict[str, Any]]) -> Dict[str, Any]:
1607
- """
1608
- Step 2.6: Validates documentation and verification metadata contract for ALLI nodes.
1609
- Checks:
1610
- - Summary presence and length
1611
- - doc_url HTTP/HTTPS format (if provided)
1612
- - Big-O complexity notation
1613
- - Example presence and structure
1614
- - Possible exceptions schema and conditions
1615
- - Verification metadata (node_kind, determinism, test_inputs/outputs)
1616
- """
1617
- errors: List[str] = []
1618
- warnings: List[str] = []
1619
-
1620
- if isinstance(node_data, NodeDefinition):
1621
- raw = node_data.model_dump()
1622
- node_type = node_data.type
1623
- elif isinstance(node_data, dict):
1624
- raw = node_data
1625
- node_type = raw.get("type", "unknown")
1626
- else:
1627
- return {
1628
- "valid": False,
1629
- "node_type": "unknown",
1630
- "errors": ["Invalid node data format: expected NodeDefinition or dict."],
1631
- "warnings": [],
1632
- "metadata": None,
1633
- }
1634
-
1635
- docs = raw.get("docs")
1636
- if not docs:
1637
- errors.append(f"Node '{node_type}' is missing required documentation metadata ('docs').")
1638
- else:
1639
- summary = docs.get("summary")
1640
- if not summary or not isinstance(summary, str) or not summary.strip():
1641
- errors.append(f"Node '{node_type}' documentation summary must be a non-empty string.")
1642
-
1643
- doc_url = docs.get("doc_url")
1644
- if doc_url is not None and str(doc_url).strip():
1645
- url_str = str(doc_url).strip()
1646
- if not (url_str.startswith("http://") or url_str.startswith("https://")):
1647
- errors.append(f"Node '{node_type}' doc_url '{url_str}' must be a valid HTTP or HTTPS URL.")
1648
-
1649
- comp = docs.get("complexity", "O(1)")
1650
- if isinstance(comp, str):
1651
- c_str = comp.strip()
1652
- if c_str not in ("N/A", "unknown", "O(1)") and not re.match(r"^O\([a-zA-Z0-9_*^ /+-]+\)$", c_str):
1653
- errors.append(f"Node '{node_type}' complexity '{comp}' must follow standard Big-O notation (e.g. 'O(1)', 'O(N)').")
1654
- else:
1655
- errors.append(f"Node '{node_type}' complexity must be a string.")
1656
-
1657
- examples = docs.get("examples", [])
1658
- if not isinstance(examples, list) or len(examples) == 0:
1659
- warnings.append(f"Node '{node_type}' has no documentation examples.")
1660
- else:
1661
- for idx, ex in enumerate(examples):
1662
- if not isinstance(ex, dict) or not ex.get("title"):
1663
- errors.append(f"Node '{node_type}' example #{idx} must have a non-empty 'title'.")
1664
-
1665
- poss_exc = docs.get("possible_exceptions", [])
1666
- if isinstance(poss_exc, list):
1667
- for idx, pe in enumerate(poss_exc):
1668
- if isinstance(pe, dict):
1669
- pe_type = pe.get("type", "")
1670
- if not pe_type or not re.match(r"^[a-zA-Z_][a-zA-Z0-9_]*$", pe_type):
1671
- errors.append(f"Node '{node_type}' possible_exception #{idx} type '{pe_type}' must be a valid identifier.")
1672
- if not pe.get("condition"):
1673
- warnings.append(f"Node '{node_type}' possible_exception '{pe_type}' lacks trigger condition.")
1674
-
1675
- verification = raw.get("verification")
1676
- if not verification:
1677
- warnings.append(f"Node '{node_type}' is missing declarative verification metadata ('verification').")
1678
- else:
1679
- node_kind = verification.get("node_kind")
1680
- if node_kind not in ("value_producer", "effect_only", "control_flow"):
1681
- errors.append(f"Node '{node_type}' verification node_kind '{node_kind}' must be 'value_producer', 'effect_only', or 'control_flow'.")
1682
- if not isinstance(verification.get("is_deterministic", True), bool):
1683
- errors.append(f"Node '{node_type}' verification is_deterministic must be a boolean.")
1684
-
1685
- return {
1686
- "valid": len(errors) == 0,
1687
- "node_type": node_type,
1688
- "errors": errors,
1689
- "warnings": warnings,
1690
- "metadata": {
1691
- "docs": docs,
1692
- "verification": verification,
1693
- },
1694
- }
1695
 
1696
 
1697
  def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
@@ -1699,39 +178,17 @@ def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
1699
  try:
1700
  body_raw = event.get("body", "{}")
1701
  body = json.loads(body_raw) if isinstance(body_raw, str) else body_raw
1702
-
1703
- source_type = body.get("source_type", "Any")
1704
- target_type = body.get("target_type", "Any")
1705
-
1706
  if "graph" in body or "graph_spec" in body:
1707
- g = body.get("graph") or body.get("graph_spec")
1708
- res = validate_workflow_protocol_logic(g, execution_exit_code=body.get("execution_exit_code"))
1709
  elif "expression" in body:
1710
  res = validate_expression_logic(body["expression"])
1711
  elif "node_metadata" in body or "node_definition" in body:
1712
- nd = body.get("node_metadata") or body.get("node_definition")
1713
- res = validate_node_metadata_logic(nd)
1714
  elif "connection" in body:
1715
  res = validate_connection_logic(body["connection"], nodes_map=body.get("nodes_map"))
1716
  else:
1717
- res = validate_types_logic(source_type, target_type)
1718
 
1719
- return {
1720
- "statusCode": 200,
1721
- "headers": {
1722
- "Content-Type": "application/json",
1723
- "Access-Control-Allow-Origin": "*",
1724
- "Access-Control-Allow-Methods": "POST, OPTIONS",
1725
- },
1726
- "body": json.dumps(res),
1727
- }
1728
  except Exception as err:
1729
- return {
1730
- "statusCode": 500,
1731
- "headers": {
1732
- "Content-Type": "application/json",
1733
- "Access-Control-Allow-Origin": "*",
1734
- },
1735
- "body": json.dumps({"error": str(err)}),
1736
- }
1737
-
 
1
  """
2
  ALLI Lambda Function: Language Core Validator (alli-validate)
3
+ Facade orchestrating type compatibility, connection checks, graph validation, and Lambda dispatch.
4
  """
5
 
 
6
  import json
7
+ from collections import defaultdict
8
+ from typing import Any, Dict, List, Optional
9
+
10
+ from language_core.node_schema import NodeDefinition, default_registry
11
+ from lambdas.validation.connection_validator import validate_connection_logic
12
+ from lambdas.validation.graph_checks import validate_graph_cycles, validate_graph_entrypoints
13
+ from lambdas.validation.metadata_validator import validate_node_metadata_logic
14
+ from lambdas.validation.node_checks import validate_all_nodes
15
+ from lambdas.validation.types_and_expr import (
16
+ APPROVED_AGENT_TYPES,
17
+ APPROVED_REASONER_TYPES,
18
+ is_agent_reasoner_or_memory,
19
+ is_approved_agent_node,
20
+ is_configured_port_value,
21
+ is_loop_header_node,
22
+ is_valid_node_id,
23
+ validate_expression_logic,
24
+ validate_types_logic,
25
+ )
26
+
27
+ STD_LIBS = {
28
+ "math", "json", "sys", "os", "re", "time", "datetime", "random",
29
+ "collections", "itertools", "functools", "urllib", "typing", "string",
30
+ "copy", "hashlib", "base64", "io", "pathlib", "subprocess", "logging",
31
+ "shutil", "csv", "tempfile", "traceback", "struct", "unicodedata",
32
+ "core", "operator",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  }
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  def validate_workflow_protocol_logic(
37
  graph: Dict[str, Any],
 
38
  execution_exit_code: Optional[int] = None,
39
  ) -> Dict[str, Any]:
40
+ """Phase 3.9: Complete 10-Possibility Workflow Validation Engine."""
41
+ from language_core.workflow_migration import migrate_workflow
42
+ if graph and isinstance(graph, dict):
43
+ try:
44
+ graph = migrate_workflow(graph)
45
+ except Exception:
46
+ pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  raw_nodes = graph.get("nodes", [])
49
  raw_edges = graph.get("edges") if graph.get("edges") is not None else (graph.get("connections") or [])
50
 
 
51
  nodes = [
52
  n for n in raw_nodes
53
+ if str(n.get("node_type") or n.get("type") or n.get("data", {}).get("node_type") or "")
54
+ not in ("system.workflow_group", "workflow_group", "workflowGroup")
55
  ]
56
  edges = [
57
  e for e in raw_edges
58
  if not str(e.get("source", "")).startswith("group_") and not str(e.get("target", "")).startswith("group_")
59
  ]
60
 
 
 
 
 
 
 
61
  diagnostics: List[Dict[str, Any]] = []
62
  possibility_map = {
63
  "Start Validation": {"errors": 0, "warnings": 0, "summary": "Valid starter trigger"},
 
82
  node_id: Optional[str] = None,
83
  port_id: Optional[str] = None,
84
  edge_id: Optional[str] = None,
85
+ fix: Optional[str] = None,
86
+ ) -> None:
87
  diagnostics.append({
88
  "code": code,
89
  "severity": severity,
 
101
  elif severity == "warning":
102
  possibility_map[category]["warnings"] += 1
103
 
 
104
  if not nodes:
105
+ add_diag("warning", "Start Validation", "VAL-001", "Canvas is empty. Add nodes to build a workflow.", 1, fix="Insert nodes via the library palette or context menu.")
106
+ return {"valid": False, "status": "Warning", "can_execute": False, "error_count": 0, "warning_count": 1, "diagnostics": diagnostics, "possibilities": possibility_map}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
+ id_counts: Dict[str, int] = defaultdict(int)
 
109
  node_map: Dict[str, Dict[str, Any]] = {}
 
110
  for n in nodes:
111
  raw_id = n.get("id")
112
  lbl = str(n.get("data", {}).get("label") or n.get("node_type") or "Node")
113
+ is_valid_id, err_code = is_valid_node_id(raw_id)
114
+ if not is_valid_id:
115
  if err_code == "VAL-NODE-ID-EMPTY":
116
+ add_diag("error", "Node Validation", "VAL-NODE-ID-EMPTY", f"Empty Node ID: Node '{lbl}' has a missing, null, or empty identifier. Every node must have a non-empty string ID.", 2, node_id="", fix="Assign a unique, non-empty identifier to each node in the workflow.")
 
 
 
 
 
 
 
 
117
  else:
118
+ id_disp = str(raw_id) if raw_id is not None else ""
119
+ add_diag("error", "Node Validation", "VAL-NODE-ID-MALFORMED", f"Malformed Node ID: Identifier '{id_disp}' on node '{lbl}' contains invalid characters, whitespace, or exceeds 128 characters. Allowed characters: alphanumeric, '_', '-', '.', ':', '#', '@', '/'.", 2, node_id=id_disp, fix="Use a valid alphanumeric identifier without whitespace, quotes, newlines, or illegal characters.")
 
 
 
 
 
 
 
 
120
  continue
 
121
  id_str = str(raw_id)
122
  id_counts[id_str] += 1
123
+ node_map.setdefault(id_str, n)
 
124
 
125
  for nid, count in id_counts.items():
126
  if count > 1:
127
+ add_diag("error", "Node Validation", "VAL-DUPLICATE-NODE-ID", f"Duplicate Node ID: Multiple nodes ({count}) share the identifier '{nid}'. Each node in the workflow must have a unique ID.", 2, node_id=nid, fix="Assign unique IDs to each node in the workflow.")
 
 
 
 
 
 
 
 
128
 
129
  incoming_map: Dict[str, List[Dict[str, Any]]] = {nid: [] for nid in node_map}
130
  outgoing_map: Dict[str, List[Dict[str, Any]]] = {nid: [] for nid in node_map}
 
 
131
  for edge in edges:
132
  src = edge.get("source") or edge.get("from_node")
133
  tgt = edge.get("target") or edge.get("to_node")
134
  src_port = edge.get("sourceHandle") or edge.get("from_port") or edge.get("source_handle") or "out"
135
  tgt_port = edge.get("targetHandle") or edge.get("to_port") or edge.get("target_handle") or "input"
 
136
  if src not in node_map:
137
+ add_diag("error", "Connection Validation", "VAL-003", f"Broken Connection: Edge connects from nonexistent source node '{src}'.", 3, port_id=src_port, fix="Delete the broken connection wire.")
 
 
 
 
 
 
 
 
138
  continue
139
  if tgt not in node_map:
140
+ add_diag("error", "Connection Validation", "VAL-004", f"Broken Connection: Edge connects to nonexistent target node '{tgt}'.", 3, port_id=tgt_port, fix="Delete the broken connection wire.")
 
 
 
 
 
 
 
 
141
  continue
 
142
  outgoing_map[src].append(edge)
143
  incoming_map[tgt].append(edge)
 
 
144
  conn_res = validate_connection_logic(edge, nodes_map=node_map, edges=edges, skip_cycle_check=True)
145
  edge_id = edge.get("id")
146
  if not conn_res.get("valid", True):
147
+ add_diag("error", "Connection Validation", conn_res.get("code", "VAL-007"), conn_res.get("message", "Port connection validation failed."), 3, node_id=tgt, port_id=tgt_port, edge_id=edge_id, fix="Ensure compatible port types, avoid duplicate connections, and connect output to input.")
 
 
 
 
 
 
 
 
 
 
148
  elif conn_res.get("warning"):
149
+ add_diag("warning", "Connection Typing", "WARN-CONV", conn_res["warning"], 1, node_id=tgt, port_id=tgt_port, edge_id=edge_id, fix="Verify runtime types match expected contract.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
+ validate_graph_cycles(nodes, edges, node_map, add_diag)
152
+ validate_graph_entrypoints(nodes, node_map, incoming_map, outgoing_map, add_diag)
 
 
 
 
 
 
 
153
 
154
+ graph_deps = {str(d).lower() for d in (graph.get("dependencies") or [])}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  req_txt = graph.get("compiled_artifacts", {}).get("requirements_txt", "")
156
  for rline in req_txt.splitlines():
157
  pkg_cleaned = rline.strip().split("==")[0].split(">=")[0].split("<=")[0].strip().lower()
158
  if pkg_cleaned:
159
  graph_deps.add(pkg_cleaned)
160
 
161
+ validate_all_nodes(nodes, incoming_map, graph_deps, STD_LIBS, add_diag)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  if execution_exit_code is not None:
164
  if execution_exit_code == 0:
165
  possibility_map["Execution Validation"]["summary"] = "Sandbox execution passed (Exit 0)."
166
  else:
167
+ add_diag("error", "Execution Validation", "EXEC-E001", f"Execution failed with Exit Code {execution_exit_code}.", 8)
 
 
 
 
 
 
168
 
169
  errors = sum(p["errors"] for p in possibility_map.values())
170
  warnings = sum(p["warnings"] for p in possibility_map.values())
171
+ status = "Runtime Error" if (execution_exit_code is not None and execution_exit_code != 0) else ("Error" if errors > 0 else ("Warning" if warnings > 0 else "Valid"))
172
 
173
+ return {"valid": errors == 0, "status": status, "can_execute": errors == 0, "error_count": errors, "warning_count": warnings, "diagnostics": diagnostics, "possibilities": possibility_map}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
 
175
 
176
  def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]:
 
178
  try:
179
  body_raw = event.get("body", "{}")
180
  body = json.loads(body_raw) if isinstance(body_raw, str) else body_raw
 
 
 
 
181
  if "graph" in body or "graph_spec" in body:
182
+ res = validate_workflow_protocol_logic(body.get("graph") or body.get("graph_spec"), execution_exit_code=body.get("execution_exit_code"))
 
183
  elif "expression" in body:
184
  res = validate_expression_logic(body["expression"])
185
  elif "node_metadata" in body or "node_definition" in body:
186
+ res = validate_node_metadata_logic(body.get("node_metadata") or body.get("node_definition"))
 
187
  elif "connection" in body:
188
  res = validate_connection_logic(body["connection"], nodes_map=body.get("nodes_map"))
189
  else:
190
+ res = validate_types_logic(body.get("source_type", "Any"), body.get("target_type", "Any"))
191
 
192
+ return {"statusCode": 200, "headers": {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "POST, OPTIONS"}, "body": json.dumps(res)}
 
 
 
 
 
 
 
 
193
  except Exception as err:
194
+ return {"statusCode": 500, "headers": {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}, "body": json.dumps({"error": str(err)})}
 
 
 
 
 
 
 
 
lambdas/validation/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ """Modular validation package for ALLI Language Core."""
lambdas/validation/connection_validator.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Validation Subpackage: Connection & Edge Compatibility Engine (Phase 3.6).
3
+ """
4
+
5
+ from collections import deque
6
+ from copy import deepcopy
7
+ from typing import Any, Dict, List, Optional
8
+
9
+ from language_core.input_transforms import find_transform_for_port, get_effective_transform_type, get_node_input_transforms
10
+ from language_core.node_schema import get_effective_node_schema
11
+ from lambdas.validation.types_and_expr import (
12
+ is_approved_agent_node,
13
+ is_loop_header_node,
14
+ validate_types_logic,
15
+ )
16
+
17
+
18
+ def _extract_node_port_list(node: Optional[Dict[str, Any]], port_key: str) -> List[Dict[str, Any]]:
19
+ if not node:
20
+ return []
21
+ data = node.get("data") or {}
22
+ ntype = str(node.get("type") or node.get("node_type") or data.get("node_type") or data.get("type") or "")
23
+ reg_schema = get_effective_node_schema(ntype) if ntype else None
24
+ if reg_schema:
25
+ schema_ports = getattr(reg_schema, port_key, [])
26
+ if schema_ports:
27
+ res_ports = [p.model_dump() if hasattr(p, "model_dump") else deepcopy(p) for p in schema_ports]
28
+ if port_key == "outputs":
29
+ p_dict = node.get("parameters") or data.get("parameters") or {}
30
+ specific_type = p_dict.get("const_type") or p_dict.get("var_type")
31
+ if specific_type:
32
+ for p in res_ports:
33
+ if p.get("id") in ("val", "result", "out", "value", "out_val"):
34
+ p["type_hint"] = specific_type
35
+ p["type"] = specific_type
36
+ return res_ports
37
+
38
+ spec = data.get("spec") or data.get("schema") or node.get("spec") or {}
39
+ ports = spec.get(port_key) or data.get(port_key) or node.get(port_key)
40
+ return [p for p in ports if isinstance(p, dict)] if isinstance(ports, list) else []
41
+
42
+
43
+ def _resolve_canonical_handle(node_dict: Optional[Dict[str, Any]], port_key: str, handle_val: Any, fallback: str) -> str:
44
+ if not node_dict:
45
+ return str(handle_val or fallback).strip().lower()
46
+ plist = _extract_node_port_list(node_dict, port_key)
47
+ h = str(handle_val or "").strip().lower()
48
+ for p in plist:
49
+ pid = str(p.get("id") or "").lower()
50
+ pname = str(p.get("name") or "").lower()
51
+ if h and (h == pid or h == pname):
52
+ return str(p.get("id") or p.get("name") or h).lower()
53
+ if len(plist) == 1:
54
+ return str(plist[0].get("id") or plist[0].get("name") or fallback).lower()
55
+ return h or fallback.lower()
56
+
57
+
58
+ def _resolve_endpoint_ports(
59
+ src_node: Optional[Dict[str, Any]],
60
+ tgt_node: Optional[Dict[str, Any]],
61
+ from_node_id: str,
62
+ to_node_id: str,
63
+ from_port: str,
64
+ to_port: str,
65
+ src_label: str,
66
+ tgt_label: str,
67
+ src_type_str: Optional[str],
68
+ tgt_type_str: Optional[str],
69
+ ) -> Dict[str, Any]:
70
+ src_kind, tgt_kind, tgt_multiple = "data", "data", False
71
+ found_out, found_in = None, None
72
+
73
+ if src_node:
74
+ src_outputs = _extract_node_port_list(src_node, "outputs")
75
+ if not any(out.get("id") == from_port or out.get("name") == from_port for out in src_outputs):
76
+ src_inputs = _extract_node_port_list(src_node, "inputs")
77
+ if any(inp.get("id") == from_port or inp.get("name") == from_port for inp in src_inputs):
78
+ return {"error": {"valid": False, "code": "VAL-PORT-DIRECTION", "message": f"Direction Violation: Port '{from_port}' on source node '{src_label}' is an input port, but connections must originate from an output port.", "source_node": from_node_id, "target_node": to_node_id}}
79
+
80
+ for out in src_outputs:
81
+ if out.get("id") == from_port or out.get("name") == from_port:
82
+ found_out = out
83
+ break
84
+ if not found_out and from_port in ("out", "result", "output", "default", "val", "value"):
85
+ found_out = next((o for o in src_outputs if o.get("id") in ("out", "result", "output", "default", "val", "value")), None)
86
+ if not found_out and len(src_outputs) == 1:
87
+ found_out = src_outputs[0]
88
+
89
+ if found_out:
90
+ src_type_str = found_out.get("type") or found_out.get("type_hint", "Any")
91
+ src_kind = found_out.get("kind", "control" if src_type_str in ("control", "branch", "exec") else "data")
92
+ elif src_outputs:
93
+ return {"error": {"valid": False, "code": "VAL-PORT-NOT-FOUND", "message": f"Port Resolution Error: Output port '{from_port}' does not exist on node '{src_label}'.", "source_node": from_node_id, "target_node": to_node_id}}
94
+
95
+ if tgt_node:
96
+ tgt_inputs = _extract_node_port_list(tgt_node, "inputs")
97
+ if not any(inp.get("id") == to_port or inp.get("name") == to_port for inp in tgt_inputs):
98
+ tgt_outputs = _extract_node_port_list(tgt_node, "outputs")
99
+ if any(out.get("id") == to_port or out.get("name") == to_port for out in tgt_outputs):
100
+ return {"error": {"valid": False, "code": "VAL-PORT-DIRECTION", "message": f"Direction Violation: Port '{to_port}' on target node '{tgt_label}' is an output port, but connections must terminate at an input port.", "source_node": from_node_id, "target_node": to_node_id}}
101
+
102
+ for inp in tgt_inputs:
103
+ if inp.get("id") == to_port or inp.get("name") == to_port:
104
+ found_in = inp
105
+ break
106
+ if not found_in and to_port in ("in", "input", "x", "val", "default", "value", "message", "data", "text"):
107
+ for candidate in ("value", "in", "input", "x", "val", "data", "text", "message"):
108
+ found_in = next((i for i in tgt_inputs if i.get("id") == candidate or i.get("name") == candidate), None)
109
+ if found_in:
110
+ break
111
+ if not found_in:
112
+ data_inputs = [i for i in tgt_inputs if i.get("kind") != "control" and str(i.get("type", "")).lower() != "control"]
113
+ if len(data_inputs) == 1:
114
+ found_in = data_inputs[0]
115
+
116
+ if found_in:
117
+ tgt_type_str = found_in.get("type") or found_in.get("type_hint", "Any")
118
+ tgt_kind = found_in.get("kind", "control" if tgt_type_str in ("control", "branch", "exec") else "data")
119
+ tgt_multiple = bool(found_in.get("multiple", False))
120
+ elif tgt_inputs:
121
+ return {"error": {"valid": False, "code": "VAL-PORT-NOT-FOUND", "message": f"Port Resolution Error: Input port '{to_port}' does not exist on node '{tgt_label}'.", "source_node": from_node_id, "target_node": to_node_id}}
122
+
123
+ return {
124
+ "src_type_str": src_type_str or "Any",
125
+ "tgt_type_str": tgt_type_str or "Any",
126
+ "src_kind": src_kind,
127
+ "tgt_kind": tgt_kind,
128
+ "tgt_multiple": tgt_multiple,
129
+ "found_out": found_out,
130
+ "found_in": found_in,
131
+ }
132
+
133
+
134
+ def validate_connection_logic(
135
+ connection: Dict[str, Any],
136
+ nodes_map: Optional[Dict[str, Any]] = None,
137
+ edges: Optional[List[Dict[str, Any]]] = None,
138
+ skip_cycle_check: bool = False,
139
+ ) -> Dict[str, Any]:
140
+ """Phase 3.6: Complete connection validation contract (ports, multiplicity, isolation, cycles, types)."""
141
+ from_node_id = connection.get("from_node") or connection.get("source")
142
+ to_node_id = connection.get("to_node") or connection.get("target")
143
+ from_port = connection.get("from_port") or connection.get("sourceHandle") or connection.get("source_handle") or "out"
144
+ to_port = connection.get("to_port") or connection.get("targetHandle") or connection.get("target_handle") or "input"
145
+
146
+ if not from_node_id or not to_node_id:
147
+ return {"valid": False, "code": "VAL-INCOMPLETE-EDGE", "message": "Incomplete Connection: Both source and target endpoints must be provided."}
148
+
149
+ if from_node_id == to_node_id:
150
+ tgt_is_loop = is_loop_header_node(nodes_map.get(to_node_id)) if nodes_map else False
151
+ if tgt_is_loop:
152
+ return {"valid": False, "code": "CTRL-001", "message": f"[CTRL-001] Invalid Loop Feedback: Node '{from_node_id}' cannot loop back directly to itself. ALLI loop constructs manage iterations internally without backward feedback wires.", "source_node": from_node_id, "target_node": to_node_id}
153
+ return {"valid": False, "code": "DAG-001", "message": f"[DAG-001] Self-Loop Prohibited: Node '{from_node_id}' cannot connect directly to itself.", "source_node": from_node_id, "target_node": to_node_id}
154
+
155
+ src_node, tgt_node = None, None
156
+ if nodes_map:
157
+ if from_node_id not in nodes_map:
158
+ return {"valid": False, "code": "VAL-NODE-NOT-FOUND", "message": f"Node Resolution Error: Source node '{from_node_id}' does not exist."}
159
+ if to_node_id not in nodes_map:
160
+ return {"valid": False, "code": "VAL-NODE-NOT-FOUND", "message": f"Node Resolution Error: Target node '{to_node_id}' does not exist."}
161
+ src_node, tgt_node = nodes_map[from_node_id], nodes_map[to_node_id]
162
+
163
+ src_label = src_node.get("data", {}).get("label") or from_node_id if src_node else from_node_id
164
+ tgt_label = tgt_node.get("data", {}).get("label") or to_node_id if tgt_node else to_node_id
165
+
166
+ ep = _resolve_endpoint_ports(
167
+ src_node, tgt_node, from_node_id, to_node_id, from_port, to_port,
168
+ src_label, tgt_label, connection.get("source_type"), connection.get("target_type"),
169
+ )
170
+ if "error" in ep:
171
+ return ep["error"]
172
+
173
+ src_type_str, tgt_type_str = ep["src_type_str"], ep["tgt_type_str"]
174
+ if ep["src_kind"] != ep["tgt_kind"]:
175
+ return {"valid": False, "code": "VAL-CONTROL-ISOLATION", "message": f"Control-Flow Violation: Cannot connect {ep['src_kind']} output '{from_port}' to {ep['tgt_kind']} input '{to_port}'.", "source_type": src_type_str, "target_type": tgt_type_str, "source_node": from_node_id, "target_node": to_node_id}
176
+
177
+ found_out, found_in = ep["found_out"], ep["found_in"]
178
+ canon_src_port = (found_out.get("id") or found_out.get("name") or from_port) if found_out else from_port
179
+ canon_tgt_port = (found_in.get("id") or found_in.get("name") or to_port) if found_in else to_port
180
+ cur_src_canon = _resolve_canonical_handle(src_node, "outputs", canon_src_port, "out")
181
+ cur_tgt_canon = _resolve_canonical_handle(tgt_node, "inputs", canon_tgt_port, "input")
182
+
183
+ if edges:
184
+ for e in edges:
185
+ if e is connection or (e.get("id") and connection.get("id") and e.get("id") == connection.get("id")):
186
+ continue
187
+ e_src, e_tgt = e.get("source") or e.get("from_node"), e.get("target") or e.get("to_node")
188
+ e_tgt_canon = _resolve_canonical_handle(tgt_node, "inputs", e.get("targetHandle") or e.get("to_port") or e.get("target_handle"), "input")
189
+ if e_src == from_node_id and e_tgt == to_node_id:
190
+ e_src_canon = _resolve_canonical_handle(src_node, "outputs", e.get("sourceHandle") or e.get("from_port") or e.get("source_handle"), "out")
191
+ if e_src_canon == cur_src_canon and e_tgt_canon == cur_tgt_canon:
192
+ return {"valid": False, "code": "VAL-DUPLICATE-CONNECTION", "message": f"Duplicate Connection: An edge already exists connecting [{src_label}:{from_port}] to [{tgt_label}:{to_port}].", "source_node": from_node_id, "target_node": to_node_id}
193
+ if not ep["tgt_multiple"] and e_tgt == to_node_id and e_tgt_canon == cur_tgt_canon:
194
+ return {"valid": False, "code": "VAL-PORT-MULTIPLICITY", "message": f"Port Multiplicity Error: Input port '{to_port}' on node '{tgt_label}' only allows a single connection and is already connected.", "source_node": from_node_id, "target_node": to_node_id}
195
+
196
+ if edges and not skip_cycle_check:
197
+ adj: Dict[str, List[str]] = {}
198
+ for e in edges:
199
+ if e is connection or (e.get("id") and connection.get("id") and e.get("id") == connection.get("id")):
200
+ continue
201
+ e_s, e_t = e.get("source") or e.get("from_node"), e.get("target") or e.get("to_node")
202
+ if e_s and e_t:
203
+ adj.setdefault(e_s, []).append(e_t)
204
+ queue, visited = deque([to_node_id]), set()
205
+ while queue:
206
+ curr = queue.popleft()
207
+ if curr == from_node_id:
208
+ if is_loop_header_node(tgt_node):
209
+ return {"valid": False, "code": "CTRL-001", "message": f"[CTRL-001] Invalid Loop Feedback: Connecting [{src_label}] -> [{tgt_label}] creates an invalid feedback wire targeting loop header '{tgt_label}'. ALLI loop constructs manage iterations internally without backward feedback wires.", "source_node": from_node_id, "target_node": to_node_id}
210
+ if is_approved_agent_node(src_node) and is_approved_agent_node(tgt_node):
211
+ break
212
+ return {"valid": False, "code": "DAG-001", "message": f"[DAG-001] Dependency Cycle Detected: Connecting [{src_label}] -> [{tgt_label}] creates an ordinary execution cycle in the DAG.", "source_node": from_node_id, "target_node": to_node_id}
213
+ if curr not in visited:
214
+ visited.add(curr)
215
+ queue.extend(nxt for nxt in adj.get(curr, []) if nxt not in visited)
216
+
217
+ eff_src_type = src_type_str
218
+ if tgt_node:
219
+ tgt_d = tgt_node.get("data") or {}
220
+ tgt_ntype = str(tgt_node.get("node_type") or tgt_node.get("type") or tgt_d.get("node_type") or tgt_d.get("type") or "")
221
+ t_cfg = find_transform_for_port(get_node_input_transforms(tgt_node, tgt_d, tgt_node.get("parameters")), [canon_tgt_port, to_port], tgt_ntype)
222
+ eff_t = get_effective_transform_type(t_cfg)
223
+ if eff_t:
224
+ eff_src_type = eff_t
225
+ type_res = validate_types_logic(eff_src_type, tgt_type_str)
226
+ if not type_res["valid"]:
227
+ return {"valid": False, "code": "VAL-TYPE-MISMATCH", "message": f"[{src_label}:{from_port}] -> [{tgt_label}:{to_port}]: {type_res['message']}", "source_type": src_type_str, "target_type": tgt_type_str, "source_node": from_node_id, "target_node": to_node_id, "requires_cast": type_res.get("requires_cast", False)}
228
+
229
+ return {"valid": True, "code": "VAL-OK", "message": f"Valid: Connection from [{src_label}:{from_port}] ({src_type_str}) to [{tgt_label}:{to_port}] ({tgt_type_str}) is valid.", "source_type": src_type_str, "target_type": tgt_type_str, "warning": type_res.get("warning"), "source_node": from_node_id, "target_node": to_node_id, "source_port": from_port, "target_port": to_port}
lambdas/validation/graph_checks.py ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Validation Subpackage: Graph Cycle Detection & Entrypoint Rules (Rule 3.9.5).
3
+ """
4
+
5
+ from collections import defaultdict, deque
6
+ from typing import Any, Callable, Dict, List, Tuple
7
+
8
+ from lambdas.validation.types_and_expr import (
9
+ is_agent_reasoner_or_memory,
10
+ is_approved_agent_node,
11
+ is_loop_header_node,
12
+ )
13
+
14
+ EXPLICIT_TRIGGER_TYPES = {
15
+ "workflow.start",
16
+ "trigger.schedule",
17
+ "trigger.webhook",
18
+ "start",
19
+ "schedule",
20
+ "webhook",
21
+ }
22
+
23
+ OPERATION_NODE_TYPES = {
24
+ "api.http_request",
25
+ "http_request",
26
+ "http",
27
+ "api_request",
28
+ "api.request",
29
+ }
30
+
31
+ BACKCOMPAT_STARTER_TYPES = (
32
+ "variable", "constant", "input", "start", "webhook", "schedule",
33
+ "manual_trigger", "prompt", "datetime", "random", "file", "import", "math",
34
+ )
35
+
36
+
37
+ def _get_node_type_and_label(node_dict: Dict[str, Any]) -> Tuple[str, str]:
38
+ d = node_dict.get("data") or {}
39
+ raw_type = str(
40
+ node_dict.get("node_type") or d.get("node_type") or d.get("type") or node_dict.get("type") or ""
41
+ ).strip().lower()
42
+ lbl = str(d.get("label") or node_dict.get("label") or "").strip().lower()
43
+ return raw_type.removeprefix("alli."), lbl
44
+
45
+
46
+ def is_explicit_trigger_node(node_dict: Dict[str, Any]) -> bool:
47
+ if node_dict.get("is_alternative_entry") or node_dict.get("is_alternative_trigger"):
48
+ return False
49
+ params = node_dict.get("parameters") if isinstance(node_dict.get("parameters"), dict) else {}
50
+ data = node_dict.get("data") if isinstance(node_dict.get("data"), dict) else {}
51
+ custom = node_dict.get("customData") if isinstance(node_dict.get("customData"), dict) else {}
52
+ combined = {**data, **custom, **params}
53
+ if (
54
+ combined.get("is_alternative_entry")
55
+ or combined.get("is_alternative_trigger")
56
+ or combined.get("active") is False
57
+ or combined.get("disabled")
58
+ ):
59
+ return False
60
+ clean_type, lbl = _get_node_type_and_label(node_dict)
61
+ if clean_type in EXPLICIT_TRIGGER_TYPES:
62
+ return True
63
+ return clean_type in ("custom", "customnode", "default", "") and lbl in (
64
+ "start", "schedule trigger", "webhook trigger", "schedule", "webhook"
65
+ )
66
+
67
+
68
+ def is_operation_node(node_dict: Dict[str, Any]) -> bool:
69
+ clean_type, lbl = _get_node_type_and_label(node_dict)
70
+ return (
71
+ clean_type in OPERATION_NODE_TYPES
72
+ or clean_type.startswith("api.")
73
+ or "http_request" in clean_type
74
+ or clean_type == "http"
75
+ or "http request" in lbl
76
+ or lbl == "http"
77
+ )
78
+
79
+
80
+ def validate_graph_cycles(
81
+ nodes: List[Dict[str, Any]],
82
+ edges: List[Dict[str, Any]],
83
+ node_map: Dict[str, Dict[str, Any]],
84
+ add_diag: Callable[..., None],
85
+ ) -> None:
86
+ """Detect DAG cycles using Kahn's algorithm + DFS path extraction (DAG-001 / CTRL-001)."""
87
+ if len(nodes) <= 1:
88
+ return
89
+
90
+ in_degrees = {nid: 0 for nid in node_map}
91
+ adj_list: Dict[str, List[str]] = defaultdict(list)
92
+ edge_map_by_pair: Dict[Tuple[str, str], List[Dict[str, Any]]] = defaultdict(list)
93
+
94
+ for e in edges:
95
+ s = e.get("source") or e.get("from_node")
96
+ t = e.get("target") or e.get("to_node")
97
+ if s in in_degrees and t in in_degrees:
98
+ adj_list[s].append(t)
99
+ edge_map_by_pair[(s, t)].append(e)
100
+ in_degrees[t] += 1
101
+
102
+ queue = deque([nid for nid, deg in in_degrees.items() if deg == 0])
103
+ processed_count = 0
104
+ while queue:
105
+ curr = queue.popleft()
106
+ processed_count += 1
107
+ for neighbor in adj_list.get(curr, []):
108
+ in_degrees[neighbor] -= 1
109
+ if in_degrees[neighbor] == 0:
110
+ queue.append(neighbor)
111
+
112
+ if processed_count >= len(node_map) or not edges:
113
+ return
114
+
115
+ cycle_nodes = [nid for nid, deg in in_degrees.items() if deg > 0]
116
+ visited_dfs = set()
117
+ found_cycles: List[List[str]] = []
118
+
119
+ for start_cn in cycle_nodes:
120
+ if start_cn in visited_dfs:
121
+ continue
122
+ c_path: List[str] = []
123
+ rec_stack: List[str] = []
124
+
125
+ def dfs_find_cycle(node: str) -> bool:
126
+ visited_dfs.add(node)
127
+ rec_stack.append(node)
128
+ for neighbor in adj_list.get(node, []):
129
+ if neighbor in cycle_nodes:
130
+ if neighbor in rec_stack:
131
+ idx = rec_stack.index(neighbor)
132
+ c_path.extend(rec_stack[idx:] + [neighbor])
133
+ return True
134
+ if neighbor not in visited_dfs and dfs_find_cycle(neighbor):
135
+ return True
136
+ rec_stack.pop()
137
+ return False
138
+
139
+ if dfs_find_cycle(start_cn) and c_path:
140
+ found_cycles.append(c_path)
141
+
142
+ if not found_cycles and cycle_nodes:
143
+ found_cycles.append(cycle_nodes[:3])
144
+
145
+ for c_path in found_cycles:
146
+ loop_header_edge = None
147
+ for i in range(len(c_path) - 1):
148
+ u_id, v_id = c_path[i], c_path[i + 1]
149
+ if is_loop_header_node(node_map.get(v_id, {})):
150
+ matched = edge_map_by_pair.get((u_id, v_id), [{}])[0]
151
+ loop_header_edge = (
152
+ u_id,
153
+ v_id,
154
+ matched.get("id"),
155
+ matched.get("target_handle") or matched.get("targetHandle") or matched.get("to_port"),
156
+ )
157
+ break
158
+
159
+ if loop_header_edge:
160
+ u_id, v_id, eid, tgt_p = loop_header_edge
161
+ u_lbl = node_map.get(u_id, {}).get("data", {}).get("label") or u_id
162
+ v_lbl = node_map.get(v_id, {}).get("data", {}).get("label") or v_id
163
+ add_diag(
164
+ "error",
165
+ "Control Flow",
166
+ "CTRL-001",
167
+ f"[CTRL-001] Invalid Loop Feedback: Feedback wire from '{u_lbl}' ({u_id}) to loop header '{v_lbl}' ({v_id}) violates ALLI loop topology. ALLI loop constructs manage iterations internally without backward feedback wires. Connect downstream body nodes directly from loop outputs without looping back.",
168
+ 4,
169
+ node_id=v_id,
170
+ port_id=tgt_p,
171
+ edge_id=eid,
172
+ fix="Remove the backward feedback edge to the loop header. Use the loop node's internal iteration controls and downstream body flow.",
173
+ )
174
+ continue
175
+
176
+ is_approved_agent_cycle = (
177
+ len(c_path) >= 2
178
+ and all(is_approved_agent_node(node_map.get(nid, {})) for nid in c_path)
179
+ and any(is_agent_reasoner_or_memory(node_map.get(nid, {})) for nid in c_path)
180
+ )
181
+ if is_approved_agent_cycle:
182
+ continue
183
+
184
+ cycle_labels = [node_map.get(cid, {}).get("data", {}).get("label") or cid for cid in c_path]
185
+ cycle_str = " -> ".join(cycle_labels) if cycle_labels else "circular dependency"
186
+ add_diag(
187
+ "error",
188
+ "Connection Validation",
189
+ "DAG-001",
190
+ f"[DAG-001] Dependency Cycle Detected: Workflow contains a circular dependency loop: {cycle_str}. Procedural Python workflows require a Directed Acyclic Graph (DAG).",
191
+ 3,
192
+ node_id=c_path[0] if c_path else None,
193
+ fix="Remove circular feedback connections or wrap the iteration inside a For/While Loop node.",
194
+ )
195
+
196
+
197
+ def validate_graph_entrypoints(
198
+ nodes: List[Dict[str, Any]],
199
+ node_map: Dict[str, Dict[str, Any]],
200
+ incoming_map: Dict[str, List[Dict[str, Any]]],
201
+ outgoing_map: Dict[str, List[Dict[str, Any]]],
202
+ add_diag: Callable[..., None],
203
+ ) -> None:
204
+ """Verify workflow has a valid entrypoint without conflicting multiple Start nodes."""
205
+ explicit_triggers = [n for n in nodes if is_explicit_trigger_node(n)]
206
+ if len(explicit_triggers) > 1:
207
+ add_diag(
208
+ "error",
209
+ "Start Validation",
210
+ "VAL-MULTIPLE-START",
211
+ "Multiple workflow entrypoints detected. A workflow must have exactly one Start or Trigger entrypoint.",
212
+ 1,
213
+ node_id=explicit_triggers[1].get("id"),
214
+ fix="Remove extra Start or Trigger nodes so that the workflow has exactly one entrypoint.",
215
+ )
216
+
217
+ root_nodes = [nid for nid, inc in incoming_map.items() if len(inc) == 0]
218
+
219
+ def _is_backcompat_starter(node_dict: Dict[str, Any]) -> bool:
220
+ if is_operation_node(node_dict):
221
+ return False
222
+ clean_type, lbl = _get_node_type_and_label(node_dict)
223
+ return (
224
+ any(trig in clean_type or trig in lbl for trig in BACKCOMPAT_STARTER_TYPES)
225
+ or (len(outgoing_map.get(node_dict.get("id", ""), [])) > 0 and not is_operation_node(node_dict))
226
+ or (len(nodes) == 1 and not is_operation_node(node_dict))
227
+ )
228
+
229
+ if len(explicit_triggers) >= 1:
230
+ has_trigger = True
231
+ else:
232
+ has_trigger = any(_is_backcompat_starter(node_map[nid]) for nid in root_nodes if nid in node_map)
233
+
234
+ if not has_trigger and len(nodes) > 0:
235
+ add_diag(
236
+ "error",
237
+ "Start Validation",
238
+ "RULE-101",
239
+ "Missing trigger or entrypoint. Workflow must have an entrypoint node (Variable, Constant, Input, Webhook).",
240
+ 1,
241
+ node_id=nodes[0].get("id"),
242
+ fix="Add an Input Prompt, Constant, or Variable node to initiate workflow execution.",
243
+ )
lambdas/validation/metadata_validator.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Validation Subpackage: Node Documentation & Verification Metadata Validator (Step 2.6).
3
+ """
4
+
5
+ import re
6
+ from typing import Any, Dict, List, Union
7
+
8
+ from language_core.node_schema import NodeDefinition
9
+
10
+
11
+ def validate_node_metadata_logic(node_data: Union[NodeDefinition, Dict[str, Any]]) -> Dict[str, Any]:
12
+ """Step 2.6: Validates documentation and verification metadata contract for ALLI nodes."""
13
+ errors: List[str] = []
14
+ warnings: List[str] = []
15
+
16
+ if isinstance(node_data, NodeDefinition):
17
+ raw = node_data.model_dump()
18
+ node_type = node_data.type
19
+ elif isinstance(node_data, dict):
20
+ raw = node_data
21
+ node_type = raw.get("type", "unknown")
22
+ else:
23
+ return {
24
+ "valid": False,
25
+ "node_type": "unknown",
26
+ "errors": ["Invalid node data format: expected NodeDefinition or dict."],
27
+ "warnings": [],
28
+ "metadata": None,
29
+ }
30
+
31
+ docs = raw.get("docs")
32
+ if not docs:
33
+ errors.append(f"Node '{node_type}' is missing required documentation metadata ('docs').")
34
+ else:
35
+ summary = docs.get("summary")
36
+ if not summary or not isinstance(summary, str) or not summary.strip():
37
+ errors.append(f"Node '{node_type}' documentation summary must be a non-empty string.")
38
+
39
+ doc_url = docs.get("doc_url")
40
+ if doc_url is not None and str(doc_url).strip():
41
+ url_str = str(doc_url).strip()
42
+ if not (url_str.startswith("http://") or url_str.startswith("https://")):
43
+ errors.append(f"Node '{node_type}' doc_url '{url_str}' must be a valid HTTP or HTTPS URL.")
44
+
45
+ comp = docs.get("complexity", "O(1)")
46
+ if isinstance(comp, str):
47
+ c_str = comp.strip()
48
+ if c_str not in ("N/A", "unknown", "O(1)") and not re.match(r"^O\([a-zA-Z0-9_*^ /+-]+\)$", c_str):
49
+ errors.append(
50
+ f"Node '{node_type}' complexity '{comp}' must follow standard Big-O notation (e.g. 'O(1)', 'O(N)')."
51
+ )
52
+ else:
53
+ errors.append(f"Node '{node_type}' complexity must be a string.")
54
+
55
+ examples = docs.get("examples", [])
56
+ if not isinstance(examples, list) or len(examples) == 0:
57
+ warnings.append(f"Node '{node_type}' has no documentation examples.")
58
+ else:
59
+ for idx, ex in enumerate(examples):
60
+ if not isinstance(ex, dict) or not ex.get("title"):
61
+ errors.append(f"Node '{node_type}' example #{idx} must have a non-empty 'title'.")
62
+
63
+ poss_exc = docs.get("possible_exceptions", [])
64
+ if isinstance(poss_exc, list):
65
+ for idx, pe in enumerate(poss_exc):
66
+ if isinstance(pe, dict):
67
+ pe_type = pe.get("type", "")
68
+ if not pe_type or not re.match(r"^[a-zA-Z_][a-zA-Z0-9_]*$", pe_type):
69
+ errors.append(f"Node '{node_type}' possible_exception #{idx} type '{pe_type}' must be a valid identifier.")
70
+ if not pe.get("condition"):
71
+ warnings.append(f"Node '{node_type}' possible_exception '{pe_type}' lacks trigger condition.")
72
+
73
+ verification = raw.get("verification")
74
+ if not verification:
75
+ warnings.append(f"Node '{node_type}' is missing declarative verification metadata ('verification').")
76
+ else:
77
+ node_kind = verification.get("node_kind")
78
+ if node_kind not in ("value_producer", "effect_only", "control_flow"):
79
+ errors.append(
80
+ f"Node '{node_type}' verification node_kind '{node_kind}' must be 'value_producer', 'effect_only', or 'control_flow'."
81
+ )
82
+ if not isinstance(verification.get("is_deterministic", True), bool):
83
+ errors.append(f"Node '{node_type}' verification is_deterministic must be a boolean.")
84
+
85
+ return {
86
+ "valid": len(errors) == 0,
87
+ "node_type": node_type,
88
+ "errors": errors,
89
+ "warnings": warnings,
90
+ "metadata": {"docs": docs, "verification": verification},
91
+ }
lambdas/validation/node_checks.py ADDED
@@ -0,0 +1,262 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Validation Subpackage: Per-Node Schema, Required Ports & Parameter Validation.
3
+ """
4
+
5
+ from typing import Any, Callable, Dict, List, Set
6
+
7
+ from language_core.core_nodes_generated import CORE_PARAM_ALIASES, CORE_PORT_ALIASES
8
+ from language_core.input_transforms import find_transform_for_port, get_node_input_transforms, validate_input_transforms_config
9
+ from language_core.node_schema import default_registry, get_effective_node_schema
10
+ from language_core.parameter_validator import (
11
+ is_non_finite_number,
12
+ validate_parameter_value,
13
+ validate_python_identifier,
14
+ )
15
+ from library_inspector.node_synthesizer import global_staging_registry
16
+ from lambdas.validation.security_checks import validate_node_security_and_deps
17
+ from lambdas.validation.types_and_expr import is_configured_port_value, validate_expression_logic
18
+
19
+ KNOWN_PREFIXES = (
20
+ "cat_", "cat.", "fundamentals.", "alli.primitive.", "alli.func.", "alli.oop.", "alli.sys.", "alli.agent.", "alli.lang.",
21
+ "primitive.", "func.", "oop.", "sys.", "agent.", "constant.", "control.", "io.", "math.",
22
+ "core.", "operator.", "workflow.", "trigger.", "api.", "file.", "json.", "csv.", "data.",
23
+ "lang.", "module.", "db.",
24
+ )
25
+
26
+ KNOWN_EXACT_TYPES = {
27
+ "variable", "constant", "binary_op", "operator", "math", "input", "output",
28
+ "display.output", "print", "print_output", "if_else", "condition", "control.branch",
29
+ "control.if_else", "for_loop", "while_loop", "loop", "function", "function.def",
30
+ "function.call", "class", "class_def", "try_except", "try_catch", "start",
31
+ "workflow_group", "system.workflow_group", "group", "webhook", "schedule",
32
+ "manual_trigger", "text_prompt", "file_input", "http_request", "response_json", "api_response",
33
+ "lang.import", "module.import", "core.import", "core.scope", "db.query", "sys.file",
34
+ "numpy.array", "pandas.dataframe", "requests.get", "statistics.mean",
35
+ "alli.sys.import", "alli.sys.env", "alli.primitive.datetime",
36
+ }
37
+
38
+
39
+ def resolve_inputs_for_node(n: Dict[str, Any]) -> List[Dict[str, Any]]:
40
+ d = n.get("data", {})
41
+ ntype = str(n.get("node_type") or n.get("type") or d.get("node_type") or "")
42
+ reg_schema = get_effective_node_schema(ntype)
43
+ if reg_schema:
44
+ return [
45
+ {"id": p.id, "name": p.name, "type": p.type, "required": p.required, "default_value": p.default_value}
46
+ for p in reg_schema.inputs
47
+ ]
48
+
49
+ spec = d.get("spec", {})
50
+ inputs = spec.get("inputs") or d.get("inputs") or n.get("inputs")
51
+ if inputs and isinstance(inputs, list):
52
+ return inputs
53
+
54
+ t, lbl = ntype.lower(), str(d.get("label") or "").lower()
55
+ if "constant" in t or t.startswith("constant.") or t in ("input", "constant"):
56
+ return []
57
+ if "math.sqrt" in t or "sqrt" in lbl:
58
+ return [{"id": "x", "name": "x", "type": "float", "required": True}]
59
+ if "binary" in t or "calc" in lbl or "add" in lbl or "operator" in t:
60
+ return [{"id": "a", "name": "a", "type": "any", "required": True}, {"id": "b", "name": "b", "type": "any", "required": True}]
61
+ if "if" in t or ("condition" in lbl and "true" not in lbl and "false" not in lbl):
62
+ return [{"id": "condition", "name": "condition", "type": "bool", "required": True}]
63
+ if "print" in t or "display" in t or "io.print" in t:
64
+ return [{"id": "value", "name": "value", "type": "any", "required": True}]
65
+ if "for_loop" in t:
66
+ return [{"id": "iterable", "name": "iterable", "type": "any", "required": True}]
67
+ if "while_loop" in t:
68
+ return [{"id": "condition", "name": "condition", "type": "bool", "required": True}]
69
+ return []
70
+
71
+
72
+ def is_known_node_type(ntype: str, graph_deps: Set[str], std_libs: Set[str]) -> bool:
73
+ t = ntype.lower().strip()
74
+ if not t:
75
+ return False
76
+ if default_registry.get(t):
77
+ return True
78
+ if any(t.startswith(p) for p in KNOWN_PREFIXES) or t in KNOWN_EXACT_TYPES:
79
+ return True
80
+ if "." in t:
81
+ mod = t.split(".")[0].lower()
82
+ return mod in std_libs or mod in ("alli", "system", "primitive", "constant", "control", "io", "math", "custom", "display", "core", "operator", "lang", "module", "data", "agent", "db", "sys") or mod in graph_deps
83
+ return False
84
+
85
+
86
+ def _validate_schema_and_staging(nid: str, ntype: str, label: str, n: Dict[str, Any], d: Dict[str, Any], graph_deps: Set[str], std_libs: Set[str], add_diag: Callable[..., None]) -> None:
87
+ node_schema_obj = get_effective_node_schema(ntype)
88
+ staged_rec = global_staging_registry.get_record(ntype) if global_staging_registry.has(ntype) else None
89
+
90
+ node_caps = None
91
+ if node_schema_obj and getattr(node_schema_obj, "capability_states", None):
92
+ node_caps = node_schema_obj.capability_states
93
+ elif node_schema_obj and getattr(node_schema_obj, "verification", None):
94
+ node_caps = getattr(node_schema_obj.verification, "capability_states", None)
95
+ if hasattr(node_caps, "model_dump"):
96
+ node_caps = node_caps.model_dump()
97
+
98
+ if not node_caps:
99
+ node_caps = (
100
+ d.get("capability_states")
101
+ or n.get("capability_states")
102
+ or (n.get("parameters") or {}).get("capability_states")
103
+ or (d.get("verification") or {}).get("capability_states")
104
+ or (staged_rec.capability_states if staged_rec else None)
105
+ )
106
+
107
+ if default_registry.has(ntype) and default_registry.has_executor(ntype):
108
+ return
109
+ if node_caps and node_caps.get("runtime_supported") is False:
110
+ add_diag("error", "Execution Validation", "EXEC_UNSUPPORTED", f"[VAL-UNSUPPORTED-RUNTIME] Execution Unsupported: Node '{label}' ({nid}) of type '{ntype}' is marked runtime_supported=False and cannot be executed in the runtime engine.", 8, node_id=nid, fix="Use a runtime-supported node implementation or export python code for external execution.")
111
+ elif staged_rec and staged_rec.is_supported is False:
112
+ add_diag("error", "Execution Validation", "EXEC_UNSUPPORTED", f"[VAL-UNSUPPORTED-RUNTIME] Execution Unsupported: Staged node '{label}' ({nid}) of type '{ntype}' contains unsupported types or security violations.", 8, node_id=nid, fix="Ensure all port types are supported and security constraints are satisfied.")
113
+ elif global_staging_registry.has(ntype) and not global_staging_registry.is_approved(ntype) and not default_registry.has(ntype):
114
+ add_diag("error", "Execution Validation", "RULE-NODE-STAGED-UNAPPROVED", f"Unapproved Staged Node: Node '{label}' ({nid}) uses introspected staged schema '{ntype}' which has not been authorized for production execution.", 8, node_id=nid, fix="An authorized administrator must approve this staged node before workflow execution.")
115
+ elif global_staging_registry.has(ntype) and global_staging_registry.is_approved(ntype) and not default_registry.has(ntype):
116
+ add_diag("error", "Execution Validation", "EXEC_UNSUPPORTED", f"[VAL-UNSUPPORTED-RUNTIME] Execution Handler Missing: Approved staged node '{label}' ({nid}) of type '{ntype}' has no registered execution handler in runtime registry.", 8, node_id=nid, fix="Register an executable callable handler for this staged node in the execution registry.")
117
+ elif not is_known_node_type(ntype, graph_deps, std_libs):
118
+ add_diag("error", "Node Validation", "VAL-UNKNOWN-NODE-TYPE", f"Unregistered Node Type: Node '{label}' ({nid}) specifies unknown type '{ntype}' with no registered execution handler.", 2, node_id=nid, fix="Select a valid standard node schema from the library palette or register its definition.")
119
+
120
+
121
+ def _validate_required_inputs(nid: str, ntype: str, label: str, n: Dict[str, Any], node_incoming: List[Dict[str, Any]], combined_params: Dict[str, Any], add_diag: Callable[..., None]) -> None:
122
+ input_defs = resolve_inputs_for_node(n)
123
+ data_port_aliases = {"in", "input", "value", "val", "data", "text", "x", "collection", "items", "args"}
124
+ single_req = len(input_defs) == 1 or len([i for i in input_defs if i.get("required")]) == 1
125
+ node_aliases = CORE_PORT_ALIASES.get(ntype.lower(), {}).get("inputs", {})
126
+
127
+ for inp in input_defs:
128
+ if not inp.get("required", False):
129
+ continue
130
+ port_id = inp.get("id") or inp.get("name")
131
+ port_name = inp.get("name") or port_id
132
+ pid_low, pname_low = str(port_id).lower(), str(port_name).lower()
133
+ is_data_port = pid_low in data_port_aliases or pname_low in data_port_aliases
134
+
135
+ def _wire_matches(e: Dict[str, Any]) -> bool:
136
+ tgt = str(e.get("targetHandle") or e.get("to_port") or e.get("target_handle") or "").strip().lower()
137
+ if tgt in (pid_low, pname_low):
138
+ return True
139
+ canon = node_aliases.get(tgt)
140
+ if canon and canon.lower() in (pid_low, pname_low):
141
+ return True
142
+ if (not tgt or (is_data_port and tgt in data_port_aliases)) and single_req:
143
+ return True
144
+ if pid_low == "value" and tgt in ("in", "input", "val", "data"):
145
+ return True
146
+ return pid_low == "in" and tgt in ("value", "input", "val", "data")
147
+
148
+ has_wire = any(_wire_matches(e) for e in node_incoming)
149
+ port_type = str(inp.get("type") or inp.get("type_hint") or "any")
150
+ alias_keys = [ak for ak, av in node_aliases.items() if str(av).lower() in (pid_low, pname_low)]
151
+ param_aliases = CORE_PARAM_ALIASES.get(ntype.lower(), {})
152
+ alias_keys.extend(ak for ak, av in param_aliases.items() if str(av).lower() in (pid_low, pname_low))
153
+ check_keys = [port_id, port_name, f"param_{port_id}", f"param_{port_name}", *alias_keys]
154
+ has_val = any(k in combined_params and is_configured_port_value(combined_params[k], expected_type=port_type) for k in check_keys)
155
+ if not has_val:
156
+ t_cfg = find_transform_for_port(get_node_input_transforms(n, n.get("data"), combined_params), [port_id, port_name], ntype)
157
+ if t_cfg and t_cfg.get("fallback") is not None:
158
+ has_val = True
159
+ if not has_val and inp.get("default_value") is not None:
160
+ has_val = is_configured_port_value(inp["default_value"], expected_type=port_type)
161
+
162
+ if not has_wire and not has_val:
163
+ add_diag("error", "Data Validation", "VAL-REQUIRED-INPUT-MISSING", f"Required Input Missing: Port '{port_id}' on node '{label}' ({nid}) is required but unconnected and has no configured value.", 6, node_id=nid, port_id=port_id, fix=f"Connect an output wire to port '{port_id}' or provide a parameter value in the configuration drawer.")
164
+
165
+
166
+ def validate_all_nodes(
167
+ nodes: List[Dict[str, Any]],
168
+ incoming_map: Dict[str, List[Dict[str, Any]]],
169
+ graph_deps: Set[str],
170
+ std_libs: Set[str],
171
+ add_diag: Callable[..., None],
172
+ ) -> None:
173
+ """Validate every node's schema, required inputs, parameter constraints, expressions, and security."""
174
+ for n in nodes:
175
+ nid = n.get("id")
176
+ d = n.get("data", {}) if isinstance(n.get("data"), dict) else {}
177
+ custom = d.get("customData") if isinstance(d.get("customData"), dict) else {}
178
+ top_params = n.get("parameters") if isinstance(n.get("parameters"), dict) else (n.get("params") if isinstance(n.get("params"), dict) else {})
179
+ params = d.get("parameters") if isinstance(d.get("parameters"), dict) else (d.get("params") if isinstance(d.get("params"), dict) else {})
180
+ values = d.get("values") if isinstance(d.get("values"), dict) else {}
181
+ inputs_dict = n.get("inputs") if isinstance(n.get("inputs"), dict) else (d.get("inputs") if isinstance(d.get("inputs"), dict) else {})
182
+ combined = {**d, **custom, **values, **inputs_dict, **params, **top_params}
183
+
184
+ ntype = str(n.get("node_type") or n.get("type") or d.get("node_type") or d.get("nodeType") or "")
185
+ label = str(d.get("label") or ntype or "Node")
186
+
187
+ _validate_schema_and_staging(nid, ntype, label, n, d, graph_deps, std_libs, add_diag)
188
+ _validate_required_inputs(nid, ntype, label, n, incoming_map.get(nid, []), combined, add_diag)
189
+
190
+ if "binary" in ntype.lower() or "calc" in label.lower() or "operator" in ntype.lower():
191
+ if (combined.get("operator") or "+") in ("/", "//", "%") and combined.get("b") in (0, "0"):
192
+ add_diag("error", "Expression Validation", "MATH-E001", f"ZeroDivisionError: Division or modulo by literal zero in node '{label}' ({nid}).", 4, node_id=nid, port_id="b", fix="Operand B cannot be zero for division ('/') or modulo ('%').")
193
+
194
+ if "sqrt" in ntype.lower() or "sqrt" in label.lower():
195
+ x_val = combined.get("x")
196
+ if x_val is not None and x_val != "":
197
+ try:
198
+ if float(x_val) < 0:
199
+ add_diag("error", "Expression Validation", "MATH-E002", f"Math Domain Error: Node '{label}' ({nid}) receives negative literal {x_val} for square root.", 4, node_id=nid, port_id="x", fix="Square root requires a non-negative number (x >= 0).")
200
+ except ValueError:
201
+ pass
202
+
203
+ if ntype in ("core.type_cast", "type_cast", "custom.type_cast") or "type_cast" in str(ntype).lower():
204
+ from language_core.executor.wave1_handlers import normalize_cast_target, strict_cast_value
205
+ raw_ttype = combined.get("target_type")
206
+ if raw_ttype not in (None, ""):
207
+ try:
208
+ clean_ttype = normalize_cast_target(raw_ttype)
209
+ except ValueError as ve:
210
+ add_diag("error", "Data Validation", "VAL-PARAM-CONSTRAINT", str(ve), 6, node_id=nid, port_id="target_type", fix="Choose one of int, float, str, bool, list, dict, json.")
211
+ clean_ttype = None
212
+ if clean_ttype and combined.get("value") not in (None, ""):
213
+ try:
214
+ strict_cast_value(combined["value"], clean_ttype)
215
+ except ValueError:
216
+ add_diag("error", "Data Validation", "ERR-TYPE-CAST", f"Type Cast Error: Cannot cast literal '{combined['value']}' to {clean_ttype} on node '{label}' ({nid}).", 6, node_id=nid, port_id="value", fix=f"Provide a valid {clean_ttype} literal.")
217
+ if ntype in ("sys.env_get", "sys.env_set", "alli.sys.env", "sys.env"):
218
+ from language_core.workspace_env_contracts import validate_env_key
219
+ raw_env_key = combined.get("key")
220
+ if raw_env_key not in (None, ""):
221
+ ok_env, code_env, err_env = validate_env_key(raw_env_key)
222
+ if not ok_env:
223
+ add_diag("error", "Security", code_env, err_env, 6, node_id=nid, port_id="key", fix="Use a non-reserved UPPER_SNAKE_CASE environment key (e.g. APP_MODE).")
224
+
225
+ if ntype in ("function.call", "func.call"):
226
+ has_wire = any(str(e.get("target_handle") or e.get("targetHandle") or "").lower() in ("arg", "arg_1", "args", "value") for e in incoming_map.get(nid, []))
227
+ if not has_wire and combined.get("arg") in (None, ""):
228
+ add_diag("error", "Data Validation", "ERR-ARG-MISSING", f"Function call on node '{label}' ({nid}) port 'arg' is missing required argument.", 6, node_id=nid, port_id="arg", fix="Connect an argument to port 'arg' or configure it in the parameters.")
229
+
230
+ for pk, pv in combined.items():
231
+ found_nf, tok_nf = is_non_finite_number(pv)
232
+ if found_nf:
233
+ add_diag("error", "Node Validation", "VAL-PARAM-NON-FINITE", f"Non-Finite Number Error: Parameter '{pk}' on node '{label}' ({nid}) contains non-finite numeric value '{tok_nf}' (NaN or Infinity). Only finite numeric values are permitted.", 2, node_id=nid, port_id=pk, fix=f"Replace '{tok_nf}' with a valid finite number.")
234
+
235
+ validated_param_ids: Set[str] = set()
236
+ node_schema = get_effective_node_schema(ntype)
237
+ if node_schema and node_schema.parameters:
238
+ for param in node_schema.parameters:
239
+ validated_param_ids.add(param.id)
240
+ pval = combined[param.id] if param.id in combined else (custom[param.id] if param.id in custom else (combined[f"param_{param.id}"] if f"param_{param.id}" in combined else param.default))
241
+ p_valid, p_code, p_msg, p_fix = validate_parameter_value(param, pval, node_label=label, node_id=nid)
242
+ if not p_valid:
243
+ add_diag("error", "Node Validation", p_code or "VAL-PARAM-CONSTRAINT", p_msg or f"Parameter '{param.name}' violates declared schema constraint.", 2, node_id=nid, port_id=param.id, fix=p_fix)
244
+
245
+ is_var_or_const = "variable" in ntype.lower() or "constant" in ntype.lower()
246
+ for id_field in ("var_name", "variable_name", "const_name", "func_name", "fn_name", "function_name", "class_name", "loop_var", "iter_var", "target_var"):
247
+ if id_field in combined and (id_field not in validated_param_ids or (is_var_or_const and id_field == "const_name")):
248
+ if id_field in ("var_name", "variable_name", "const_name") and not is_var_or_const:
249
+ continue
250
+ if combined[id_field] is not None:
251
+ ok, code, msg, fix = validate_python_identifier(combined[id_field], field_id=id_field)
252
+ if not ok:
253
+ add_diag("error", "Node Validation", code or "COMP-E101", f"{msg} on node '{label}' ({nid}).", 2, node_id=nid, port_id=id_field, fix=fix)
254
+
255
+ if "condition" in combined or "if" in ntype.lower():
256
+ cond = str(combined.get("condition") or "True")
257
+ expr_res = validate_expression_logic(cond)
258
+ if not expr_res.get("valid"):
259
+ add_diag("error", "Expression Validation", "COMP-E102", f"Syntax error in condition expression '{cond}': {expr_res.get('error')}", 4, node_id=nid, port_id="condition", fix="Check for balanced parentheses, quotes, and valid Python syntax.")
260
+
261
+ validate_input_transforms_config(combined.get("_input_transforms"), label, nid, add_diag)
262
+ validate_node_security_and_deps(nid, ntype, label, combined, graph_deps, std_libs, add_diag)
lambdas/validation/security_checks.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ALLI Validation Subpackage: Security, Path Traversal, SSRF & Dependency Checks.
3
+ """
4
+
5
+ import ipaddress
6
+ import re
7
+ from typing import Any, Callable, Dict, Set
8
+ from urllib.parse import urlparse
9
+
10
+ from language_core.node_schema import default_registry
11
+
12
+ SECRET_PAT = re.compile(
13
+ r'(?:sk-[a-zA-Z0-9]{20,}|Bearer\s+[a-zA-Z0-9_\-\.]{25,}|ghp_[a-zA-Z0-9]{30,}|password\s*=\s*["\'][^"\']+["\'])',
14
+ re.IGNORECASE,
15
+ )
16
+
17
+ INTERNAL_NAMESPACES = {
18
+ "alli", "system", "primitive", "constant", "control", "io", "math", "custom",
19
+ "display", "core", "data", "function", "string", "oop", "workflow", "trigger",
20
+ "api", "lang", "module", "agent", "db", "sys",
21
+ }
22
+
23
+
24
+ def validate_node_security_and_deps(
25
+ nid: str,
26
+ ntype: str,
27
+ label: str,
28
+ combined_params: Dict[str, Any],
29
+ graph_deps: Set[str],
30
+ std_libs: Set[str],
31
+ add_diag: Callable[..., None],
32
+ ) -> None:
33
+ """Validate dependencies, plaintext secrets, sandbox path traversal, and SSRF targets."""
34
+ pkg_match = re.match(r"^([a-zA-Z0-9_-]+)\.", ntype)
35
+ if pkg_match:
36
+ pkg = pkg_match.group(1).lower()
37
+ if not default_registry.has(ntype) and pkg not in std_libs and pkg not in INTERNAL_NAMESPACES:
38
+ if pkg not in graph_deps:
39
+ add_diag(
40
+ "warning",
41
+ "Dependency Validation",
42
+ "DEP-W001",
43
+ f"Undeclared Dependency: Node '{label}' requires external library '{pkg}', but it is not declared in workflow dependencies.",
44
+ 10,
45
+ node_id=nid,
46
+ fix=f"Add '{pkg}' to workflow dependencies or requirements.txt.",
47
+ )
48
+
49
+ raw_vals = " ".join(str(v) for v in combined_params.values())
50
+ if SECRET_PAT.search(raw_vals):
51
+ add_diag(
52
+ "warning",
53
+ "Credential Validation",
54
+ "SEC-W001",
55
+ f"Possible plaintext secret detected on node '{label}'.",
56
+ 9,
57
+ node_id=nid,
58
+ fix="Use environment variables or runtime parameters instead of hardcoding secrets.",
59
+ )
60
+
61
+ if "file" in ntype.lower() or "sys.file" in ntype.lower():
62
+ file_path = str(combined_params.get("file_path") or combined_params.get("path") or "").strip()
63
+ win_dev = re.compile(r"^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\..*)?$", re.IGNORECASE)
64
+ parts = [p for p in file_path.replace("\\", "/").split("/") if p and p != "."]
65
+ if file_path and (
66
+ ".." in file_path
67
+ or file_path.startswith(("/", "\\"))
68
+ or re.match(r"^[a-zA-Z]:", file_path)
69
+ or "\0" in file_path
70
+ or any(win_dev.match(p) for p in parts)
71
+ ):
72
+ add_diag(
73
+ "error",
74
+ "Credential Validation",
75
+ "ERR-SECURITY-PATH-TRAVERSAL",
76
+ f"[SEC-001] Path Traversal Detected: File path '{file_path}' on node '{label}' ({nid}) attempts to escape the sandboxed workspace. Path must be relative without parent directory ('..') components.",
77
+ 9,
78
+ node_id=nid,
79
+ port_id="file_path",
80
+ fix="Specify a relative path within the workspace directory, without '..' or absolute path prefixes.",
81
+ )
82
+
83
+ if "http" in ntype.lower() or "api.http_request" in ntype.lower() or "request" in ntype.lower():
84
+ raw_url = str(combined_params.get("url") or "").strip()
85
+ if not raw_url:
86
+ return
87
+ parsed = urlparse(raw_url)
88
+ if parsed.scheme not in ("http", "https"):
89
+ add_diag(
90
+ "error",
91
+ "Credential Validation",
92
+ "SEC-003",
93
+ f"[SEC-003] Protocol '{parsed.scheme}' is not permitted. Only HTTP and HTTPS are allowed.",
94
+ 9,
95
+ node_id=nid,
96
+ port_id="url",
97
+ fix="Use 'http://' or 'https://' URLs.",
98
+ )
99
+ return
100
+
101
+ host = (parsed.hostname or "").strip("[]").lower()
102
+ is_ssrf = (
103
+ host in ("localhost", "127.0.0.1", "0.0.0.0", "::1", "", "metadata.google.internal", "instance-data")
104
+ or host.endswith((".localhost", ".internal", ".local"))
105
+ )
106
+ if not is_ssrf:
107
+ try:
108
+ ip_obj = ipaddress.ip_address(host)
109
+ if isinstance(ip_obj, ipaddress.IPv6Address) and ip_obj.ipv4_mapped:
110
+ ip_obj = ip_obj.ipv4_mapped
111
+ if (
112
+ ip_obj.is_private
113
+ or ip_obj.is_loopback
114
+ or ip_obj.is_link_local
115
+ or ip_obj.is_multicast
116
+ or ip_obj.is_reserved
117
+ or ip_obj.is_unspecified
118
+ ):
119
+ is_ssrf = True
120
+ except ValueError:
121
+ pass
122
+ if is_ssrf:
123
+ add_diag(
124
+ "error",
125
+ "Credential Validation",
126
+ "SEC-002",
127
+ f"[SEC-002] Access to restricted/private address or cloud metadata '{host}' is blocked for security.",
128
+ 9,
129
+ node_id=nid,
130
+ port_id="url",
131
+ fix="Provide a publicly accessible HTTP/HTTPS API endpoint.",
132
+ )