flowvane 0.25.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. apflow/__init__.py +16 -0
  2. flowvane/__init__.py +193 -0
  3. flowvane/_environment.py +20 -0
  4. flowvane/_version.py +8 -0
  5. flowvane/adapters/__init__.py +5 -0
  6. flowvane/adapters/function_executor.py +187 -0
  7. flowvane/api/__init__.py +14 -0
  8. flowvane/api/auth.py +140 -0
  9. flowvane/api/combined.py +252 -0
  10. flowvane/api/rest.py +534 -0
  11. flowvane/api/webhook.py +129 -0
  12. flowvane/app.py +225 -0
  13. flowvane/bridge/__init__.py +5 -0
  14. flowvane/bridge/access.py +212 -0
  15. flowvane/bridge/errors.py +22 -0
  16. flowvane/bridge/execution_modules.py +221 -0
  17. flowvane/bridge/governance_modules.py +255 -0
  18. flowvane/bridge/module_adapter.py +118 -0
  19. flowvane/bridge/output_schema.py +111 -0
  20. flowvane/bridge/protocol.py +242 -0
  21. flowvane/bridge/registry_setup.py +191 -0
  22. flowvane/bridge/scanner_bridge.py +176 -0
  23. flowvane/bridge/schedule_modules.py +432 -0
  24. flowvane/bridge/session.py +105 -0
  25. flowvane/bridge/task_modules.py +1273 -0
  26. flowvane/cli.py +720 -0
  27. flowvane/core/__init__.py +230 -0
  28. flowvane/core/base/__init__.py +10 -0
  29. flowvane/core/base/base_task.py +356 -0
  30. flowvane/core/builders.py +155 -0
  31. flowvane/core/config/__init__.py +69 -0
  32. flowvane/core/config/registry.py +846 -0
  33. flowvane/core/config_manager.py +316 -0
  34. flowvane/core/decorators.py +83 -0
  35. flowvane/core/distributed/__init__.py +25 -0
  36. flowvane/core/distributed/config.py +184 -0
  37. flowvane/core/distributed/events.py +71 -0
  38. flowvane/core/distributed/idempotency.py +132 -0
  39. flowvane/core/distributed/leader_election.py +237 -0
  40. flowvane/core/distributed/lease_manager.py +429 -0
  41. flowvane/core/distributed/node_registry.py +186 -0
  42. flowvane/core/distributed/placement.py +123 -0
  43. flowvane/core/distributed/runtime.py +397 -0
  44. flowvane/core/distributed/types.py +12 -0
  45. flowvane/core/distributed/worker.py +446 -0
  46. flowvane/core/execution/__init__.py +57 -0
  47. flowvane/core/execution/context.py +7 -0
  48. flowvane/core/execution/dispatch.py +459 -0
  49. flowvane/core/execution/error_info.py +196 -0
  50. flowvane/core/execution/errors.py +231 -0
  51. flowvane/core/execution/result_protocol.py +181 -0
  52. flowvane/core/execution/run_retry.py +347 -0
  53. flowvane/core/execution/runs.py +260 -0
  54. flowvane/core/execution/streaming_callbacks.py +143 -0
  55. flowvane/core/execution/submission.py +103 -0
  56. flowvane/core/execution/task_creator.py +1562 -0
  57. flowvane/core/execution/task_executor.py +1286 -0
  58. flowvane/core/execution/task_manager.py +2193 -0
  59. flowvane/core/execution/task_tracker.py +65 -0
  60. flowvane/core/extensions/__init__.py +49 -0
  61. flowvane/core/extensions/base.py +134 -0
  62. flowvane/core/extensions/decorators.py +347 -0
  63. flowvane/core/extensions/executor_metadata.py +208 -0
  64. flowvane/core/extensions/hook.py +71 -0
  65. flowvane/core/extensions/manager.py +375 -0
  66. flowvane/core/extensions/protocol.py +72 -0
  67. flowvane/core/extensions/registry.py +658 -0
  68. flowvane/core/extensions/scanner.py +575 -0
  69. flowvane/core/extensions/storage.py +86 -0
  70. flowvane/core/extensions/types.py +63 -0
  71. flowvane/core/interfaces/__init__.py +26 -0
  72. flowvane/core/interfaces/executable_task.py +150 -0
  73. flowvane/core/storage/__init__.py +41 -0
  74. flowvane/core/storage/context.py +313 -0
  75. flowvane/core/storage/dialects/__init__.py +13 -0
  76. flowvane/core/storage/dialects/postgres.py +76 -0
  77. flowvane/core/storage/dialects/registry.py +71 -0
  78. flowvane/core/storage/dialects/sqlite.py +96 -0
  79. flowvane/core/storage/factory.py +1200 -0
  80. flowvane/core/storage/migrate.py +208 -0
  81. flowvane/core/storage/migrations/001_add_task_tree_fields.py +261 -0
  82. flowvane/core/storage/migrations/002_add_scheduling_fields.py +155 -0
  83. flowvane/core/storage/migrations/003_add_distributed_support.py +223 -0
  84. flowvane/core/storage/migrations/004_add_durability_and_governance.py +180 -0
  85. flowvane/core/storage/migrations/005_add_idempotency_key_unique_index.py +110 -0
  86. flowvane/core/storage/migrations/006_add_service_execution_contracts.py +50 -0
  87. flowvane/core/storage/migrations/007_add_workflow_runs_usage_events.py +99 -0
  88. flowvane/core/storage/migrations/__init__.py +83 -0
  89. flowvane/core/storage/session.py +28 -0
  90. flowvane/core/storage/sqlalchemy/__init__.py +15 -0
  91. flowvane/core/storage/sqlalchemy/invariants.py +199 -0
  92. flowvane/core/storage/sqlalchemy/models.py +856 -0
  93. flowvane/core/storage/sqlalchemy/schedule_calculator.py +466 -0
  94. flowvane/core/storage/sqlalchemy/task_repository.py +1892 -0
  95. flowvane/core/types.py +328 -0
  96. flowvane/core/utils/__init__.py +3 -0
  97. flowvane/core/utils/credentials.py +283 -0
  98. flowvane/core/utils/helpers.py +484 -0
  99. flowvane/core/utils/network_security.py +92 -0
  100. flowvane/core/utils/project_detection.py +81 -0
  101. flowvane/core/validator/__init__.py +20 -0
  102. flowvane/core/validator/dependency_validator.py +479 -0
  103. flowvane/core/validator/user_validator.py +40 -0
  104. flowvane/durability/__init__.py +21 -0
  105. flowvane/durability/attempts.py +266 -0
  106. flowvane/durability/checkpoint.py +168 -0
  107. flowvane/durability/circuit_breaker.py +209 -0
  108. flowvane/durability/retry.py +224 -0
  109. flowvane/extensions/__init__.py +46 -0
  110. flowvane/extensions/core/__init__.py +12 -0
  111. flowvane/extensions/core/aggregate_results_executor.py +232 -0
  112. flowvane/extensions/email/__init__.py +9 -0
  113. flowvane/extensions/email/send_email_executor.py +456 -0
  114. flowvane/extensions/flowvane/__init__.py +10 -0
  115. flowvane/extensions/flowvane/api_executor.py +770 -0
  116. flowvane/extensions/hooks/__init__.py +11 -0
  117. flowvane/extensions/hooks/post_execution_hook.py +71 -0
  118. flowvane/extensions/hooks/pre_execution_hook.py +71 -0
  119. flowvane/extensions/http/__init__.py +10 -0
  120. flowvane/extensions/http/rest_executor.py +458 -0
  121. flowvane/extensions/storage/__init__.py +16 -0
  122. flowvane/extensions/storage/postgres_storage.py +98 -0
  123. flowvane/extensions/storage/sqlite_storage.py +47 -0
  124. flowvane/governance/__init__.py +21 -0
  125. flowvane/governance/budget.py +256 -0
  126. flowvane/governance/ledger.py +368 -0
  127. flowvane/governance/policy.py +159 -0
  128. flowvane/governance/provider_router.py +69 -0
  129. flowvane/governance/reporter.py +222 -0
  130. flowvane/governance/usage.py +310 -0
  131. flowvane/logger.py +77 -0
  132. flowvane/py.typed +0 -0
  133. flowvane/scheduler/__init__.py +27 -0
  134. flowvane/scheduler/base.py +215 -0
  135. flowvane/scheduler/gateway/__init__.py +23 -0
  136. flowvane/scheduler/gateway/ical.py +469 -0
  137. flowvane/scheduler/gateway/webhook.py +608 -0
  138. flowvane/scheduler/internal.py +640 -0
  139. flowvane-0.25.0.dist-info/METADATA +301 -0
  140. flowvane-0.25.0.dist-info/RECORD +144 -0
  141. flowvane-0.25.0.dist-info/WHEEL +5 -0
  142. flowvane-0.25.0.dist-info/entry_points.txt +3 -0
  143. flowvane-0.25.0.dist-info/licenses/LICENSE +18 -0
  144. flowvane-0.25.0.dist-info/top_level.txt +2 -0
@@ -0,0 +1,252 @@
1
+ """
2
+ Unified REST + A2A + MCP server for FlowVane.
3
+
4
+ Mounts the registry-driven REST app (flowvane.api.rest), the apcore-a2a ASGI app,
5
+ and the apcore-mcp ASGI app under a single Starlette application served by one
6
+ uvicorn process:
7
+
8
+ / -> REST/JSON + OpenAPI docs (flowvane.api.rest)
9
+ /a2a -> A2A agent (apcore-a2a)
10
+ /mcp -> MCP streamable-http server (apcore-mcp)
11
+
12
+ Follows the embedding pattern documented by ``apcore_surfaces.mcp.async_serve``: the MCP
13
+ app is an async context manager whose transport session manager must stay alive
14
+ for the lifetime of the server, so uvicorn runs *inside* the ``async with`` block.
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import asyncio
20
+ from typing import Any, Optional
21
+
22
+ from apcore import Registry
23
+ from apcore.approval import ApprovalHandler
24
+ from starlette.applications import Starlette
25
+ from starlette.middleware import Middleware
26
+ from starlette.routing import Mount
27
+
28
+ from flowvane.api.rest import build_rest_app
29
+ from flowvane.logger import get_logger
30
+
31
+ logger = get_logger(__name__)
32
+
33
+
34
+ def assemble_combined(
35
+ rest_app: Starlette,
36
+ a2a_app: Starlette,
37
+ mcp_app: Starlette,
38
+ *,
39
+ cors_origins: Optional[list[str]] = None,
40
+ ) -> Starlette:
41
+ """Mount A2A, REST, and MCP under one Starlette with clean top-level paths.
42
+
43
+ apcore-mcp hardcodes its streamable-http endpoint at ``/mcp`` inside the MCP
44
+ app (alongside ``/health`` and ``/metrics``), so mounting that app under a
45
+ ``/mcp`` prefix would bury the endpoint at ``/mcp/mcp``. Instead the MCP app
46
+ is mounted at the root as a catch-all and the lightweight REST routes are
47
+ lifted ahead of it, so the surface is::
48
+
49
+ /, /docs, /openapi.json, /modules* -> REST (lifted routes)
50
+ /a2a/* -> A2A (mounted, middleware kept)
51
+ /mcp, /health, /metrics -> MCP (root mount, middleware kept)
52
+
53
+ A2A and MCP keep their own middleware via mounting; REST carries none of note
54
+ (optional CORS is applied here at the parent instead).
55
+ """
56
+ routes = [
57
+ Mount("/a2a", app=a2a_app),
58
+ *rest_app.routes,
59
+ Mount("/", app=mcp_app),
60
+ ]
61
+ middleware: list[Middleware] = []
62
+ if cors_origins:
63
+ from starlette.middleware.cors import CORSMiddleware
64
+
65
+ middleware.append(
66
+ Middleware(
67
+ CORSMiddleware,
68
+ allow_origins=cors_origins,
69
+ allow_methods=["*"],
70
+ allow_headers=["*"],
71
+ )
72
+ )
73
+ return Starlette(routes=routes, middleware=middleware)
74
+
75
+
76
+ async def _serve_all_async(
77
+ registry: Registry,
78
+ *,
79
+ host: str,
80
+ port: int,
81
+ title: str,
82
+ version: str,
83
+ description: str,
84
+ cors_origins: Optional[list[str]],
85
+ log_level: Optional[str],
86
+ explorer: bool,
87
+ metrics: bool,
88
+ sys_modules: bool,
89
+ push_notifications: bool,
90
+ webhook: bool,
91
+ webhook_secret: Optional[str],
92
+ auth: bool,
93
+ scheduler: bool,
94
+ approval_handler: ApprovalHandler | None = None,
95
+ ) -> None:
96
+ """Assemble the combined app and run uvicorn inside the MCP async context.
97
+
98
+ When ``auth`` is set, every face validates the same JWT: A2A and MCP each
99
+ carry their own authenticator (so their sub-apps self-authenticate, keeping
100
+ A2A agent-card discovery exempt), while a parent AuthMiddleware guards the
101
+ REST module routes. The parent exempts the /a2a and /mcp prefixes (handled
102
+ by the sub-apps), the HMAC webhook, and the public REST metadata paths.
103
+ Raises RuntimeError if auth is requested without ``api.jwt_secret`` set.
104
+ """
105
+ import uvicorn
106
+ from apcore_surfaces.a2a import async_serve as a2a_async_serve
107
+ from apcore import Executor
108
+ from flowvane.bridge.protocol import FlowVaneMCP, PublicExecutor
109
+
110
+ base_url = f"http://{host}:{port}"
111
+ # The webhook route is lifted to the top level alongside the other REST routes
112
+ # by assemble_combined (*rest_app.routes), so it serves at /webhook/trigger/...
113
+ extra_routes = None
114
+ if webhook:
115
+ from flowvane.api.webhook import build_webhook_routes
116
+
117
+ factory = getattr(registry, "apflow_session_factory", None)
118
+ extra_routes = (
119
+ build_webhook_routes(secret_key=webhook_secret, session_factory=factory)
120
+ if factory
121
+ else build_webhook_routes(secret_key=webhook_secret)
122
+ )
123
+
124
+ a2a_auth = None
125
+ mcp_auth = None
126
+ if auth:
127
+ from flowvane.api.auth import build_a2a_authenticator, build_mcp_authenticator
128
+
129
+ a2a_auth = build_a2a_authenticator()
130
+ mcp_auth = build_mcp_authenticator()
131
+ if a2a_auth is None or mcp_auth is None:
132
+ # Both builders return None when api.jwt_secret is not configured.
133
+ # Pressing on here would silently leave A2A/MCP unauthenticated
134
+ # (auth=None/require_auth with no authenticator) and wrap REST in
135
+ # an AuthMiddleware backed by a None authenticator, crashing every
136
+ # request with a 500 — fail loudly instead, matching serve_rest.
137
+ raise RuntimeError("auth requested but api.jwt_secret is not configured")
138
+
139
+ # CORS is applied once at the combined parent (assemble_combined), so the REST
140
+ # sub-app is built without its own CORS layer here.
141
+ rest_app = build_rest_app(
142
+ registry,
143
+ title=title,
144
+ version=version,
145
+ extra_routes=extra_routes,
146
+ approval_handler=approval_handler,
147
+ )
148
+ a2a_app = await a2a_async_serve(
149
+ PublicExecutor(Executor.from_registry(registry, approval_handler=approval_handler)),
150
+ name=title,
151
+ version=version,
152
+ description=description,
153
+ url=f"{base_url}/a2a",
154
+ explorer=explorer,
155
+ metrics=metrics,
156
+ sys_modules=sys_modules,
157
+ cors_origins=cors_origins,
158
+ push_notifications=push_notifications,
159
+ auth=a2a_auth,
160
+ )
161
+
162
+ # The MCP transport session manager lives for the duration of this block, so
163
+ # uvicorn must run inside it (per apcore_surfaces.mcp.async_serve's documented pattern).
164
+ mcp_server = FlowVaneMCP(
165
+ (
166
+ Executor.from_registry(registry, approval_handler=approval_handler)
167
+ if approval_handler is not None
168
+ else registry
169
+ ),
170
+ name=title,
171
+ version=version,
172
+ observability=metrics,
173
+ authenticator=mcp_auth,
174
+ require_auth=auth,
175
+ )
176
+ async with mcp_server.async_serve(explorer=explorer, allow_execute=explorer) as mcp_app:
177
+ combined = assemble_combined(rest_app, a2a_app, mcp_app, cors_origins=cors_origins)
178
+ app: Any = combined
179
+ if auth:
180
+ from flowvane.api.auth import apply_rest_auth
181
+
182
+ # A2A and MCP self-authenticate inside their mounted sub-apps, so the
183
+ # parent only needs to guard the lifted REST module routes; exempt the
184
+ # sub-app prefixes (and MCP's health/metrics) to avoid double-gating.
185
+ app = apply_rest_auth(
186
+ combined,
187
+ mcp_auth,
188
+ extra_exempt_prefixes={"/a2a", "/mcp", "/health", "/metrics", "/.well-known"},
189
+ )
190
+ config = uvicorn.Config(app, host=host, port=port, log_level=(log_level or "info").lower())
191
+
192
+ # Run the scheduler poll loop in this same process/event loop (single
193
+ # SQLite writer) for the lifetime of the server; clusters use worker.
194
+ scheduler_obj = None
195
+ if scheduler:
196
+ from flowvane.scheduler.internal import InternalScheduler
197
+
198
+ factory = getattr(registry, "apflow_session_factory", None)
199
+ scheduler_obj = (
200
+ InternalScheduler(session_factory=factory) if factory else InternalScheduler()
201
+ )
202
+ await scheduler_obj.start()
203
+ try:
204
+ async with rest_app.router.lifespan_context(rest_app):
205
+ await uvicorn.Server(config).serve()
206
+ finally:
207
+ if scheduler_obj is not None:
208
+ await scheduler_obj.stop()
209
+
210
+
211
+ def serve_all(
212
+ registry: Registry,
213
+ *,
214
+ host: str = "0.0.0.0",
215
+ port: int = 8000,
216
+ title: str = "FlowVane",
217
+ version: str = "",
218
+ description: str = "FlowVane AI-Perceivable Distributed Orchestration",
219
+ cors_origins: Optional[list[str]] = None,
220
+ log_level: Optional[str] = None,
221
+ explorer: bool = False,
222
+ metrics: bool = False,
223
+ sys_modules: bool = False,
224
+ push_notifications: bool = False,
225
+ webhook: bool = False,
226
+ webhook_secret: Optional[str] = None,
227
+ auth: bool = False,
228
+ scheduler: bool = False,
229
+ approval_handler: ApprovalHandler | None = None,
230
+ ) -> None:
231
+ """Build and run the unified REST + A2A + MCP server on one port (blocking)."""
232
+ asyncio.run(
233
+ _serve_all_async(
234
+ registry,
235
+ host=host,
236
+ port=port,
237
+ title=title,
238
+ version=version,
239
+ description=description,
240
+ cors_origins=cors_origins,
241
+ log_level=log_level,
242
+ explorer=explorer,
243
+ metrics=metrics,
244
+ sys_modules=sys_modules,
245
+ push_notifications=push_notifications,
246
+ webhook=webhook,
247
+ webhook_secret=webhook_secret,
248
+ auth=auth,
249
+ scheduler=scheduler,
250
+ approval_handler=approval_handler,
251
+ )
252
+ )