graphcore-kernel 0.1.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 (343) hide show
  1. graphcore/__init__.py +5 -0
  2. graphcore/agent_frontend/__init__.py +8 -0
  3. graphcore/agent_frontend/api.py +69 -0
  4. graphcore/agent_frontend/base/__init__.py +11 -0
  5. graphcore/agent_frontend/base/views.py +167 -0
  6. graphcore/agent_frontend/client.py +31 -0
  7. graphcore/agent_frontend/context.py +157 -0
  8. graphcore/agent_frontend/errors.py +12 -0
  9. graphcore/agent_frontend/loading.py +54 -0
  10. graphcore/agent_frontend/registry.py +42 -0
  11. graphcore/agent_frontend/render.py +207 -0
  12. graphcore/agent_frontend/server.py +103 -0
  13. graphcore/agent_frontend/signatures.py +35 -0
  14. graphcore/agent_frontend/testing.py +45 -0
  15. graphcore/agent_frontend/tests/__init__.py +0 -0
  16. graphcore/agent_frontend/tests/fakes.py +114 -0
  17. graphcore/agent_frontend/tests/test_frontend.py +447 -0
  18. graphcore/agent_frontend/tests/test_loading.py +83 -0
  19. graphcore/agent_frontend/tests/test_server.py +109 -0
  20. graphcore/agent_frontend/tools.py +137 -0
  21. graphcore/api/__init__.py +4 -0
  22. graphcore/api/graph_api.py +378 -0
  23. graphcore/api/predicate_codec.py +81 -0
  24. graphcore/api/tests/__init__.py +0 -0
  25. graphcore/api/tests/test_changed_entities.py +109 -0
  26. graphcore/api/tests/test_graph_api.py +290 -0
  27. graphcore/api/tests/test_graph_api_access.py +94 -0
  28. graphcore/api/tests/test_graph_api_grants.py +90 -0
  29. graphcore/api/tests/test_predicate_codec.py +83 -0
  30. graphcore/api/tests/test_subtree_query.py +79 -0
  31. graphcore/apps.py +71 -0
  32. graphcore/auth/__init__.py +1 -0
  33. graphcore/auth/authn/README.md +178 -0
  34. graphcore/auth/authn/__init__.py +3 -0
  35. graphcore/auth/authn/authenticator.py +120 -0
  36. graphcore/auth/authn/onboarding.py +105 -0
  37. graphcore/auth/authn/principals.py +20 -0
  38. graphcore/auth/authn/resource_metadata.py +43 -0
  39. graphcore/auth/authn/service_accounts.py +96 -0
  40. graphcore/auth/authn/testing.py +96 -0
  41. graphcore/auth/authn/tests/__init__.py +0 -0
  42. graphcore/auth/authn/tests/test_authenticator.py +145 -0
  43. graphcore/auth/authn/tests/test_onboarding.py +145 -0
  44. graphcore/auth/authn/tests/test_service_accounts.py +180 -0
  45. graphcore/auth/authn/users.py +59 -0
  46. graphcore/auth/authz/__init__.py +0 -0
  47. graphcore/auth/authz/access_port.py +120 -0
  48. graphcore/auth/authz/adapter/__init__.py +0 -0
  49. graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
  50. graphcore/auth/authz/adapter/tests/__init__.py +0 -0
  51. graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
  52. graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
  53. graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
  54. graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
  55. graphcore/auth/authz/defaults.py +26 -0
  56. graphcore/auth/authz/kernel_policy.py +191 -0
  57. graphcore/auth/authz/kernel_writes.py +31 -0
  58. graphcore/auth/authz/lookups.py +25 -0
  59. graphcore/auth/authz/policy.py +403 -0
  60. graphcore/auth/authz/tests/__init__.py +0 -0
  61. graphcore/auth/authz/tests/test_can_contain.py +150 -0
  62. graphcore/auth/authz/tests/test_contained_by.py +167 -0
  63. graphcore/auth/authz/tests/test_creation.py +391 -0
  64. graphcore/auth/authz/tests/test_delete_permission.py +82 -0
  65. graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
  66. graphcore/auth/authz/tests/test_integration.py +637 -0
  67. graphcore/auth/authz/tests/test_invites.py +184 -0
  68. graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
  69. graphcore/auth/authz/tests/test_lockout.py +132 -0
  70. graphcore/auth/authz/tests/test_policy.py +329 -0
  71. graphcore/auth/authz/tests/test_primitives.py +73 -0
  72. graphcore/auth/authz/tests/test_principal_creation.py +62 -0
  73. graphcore/bootstrap/__init__.py +0 -0
  74. graphcore/bootstrap/deployment.py +67 -0
  75. graphcore/bootstrap/signals.py +16 -0
  76. graphcore/bootstrap/tests/__init__.py +0 -0
  77. graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
  78. graphcore/broadcast/__init__.py +4 -0
  79. graphcore/broadcast/broadcaster.py +201 -0
  80. graphcore/broadcast/channels.py +56 -0
  81. graphcore/broadcast/tests/__init__.py +0 -0
  82. graphcore/broadcast/tests/test_broadcaster.py +444 -0
  83. graphcore/celery.py +14 -0
  84. graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
  85. graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
  86. graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
  87. graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
  88. graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
  89. graphcore/docs/RUNTIME.md +90 -0
  90. graphcore/docs/SCHEDULING_TDD.md +244 -0
  91. graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
  92. graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
  93. graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
  94. graphcore/docs/__init__.py +0 -0
  95. graphcore/docs/doc_topics.py +97 -0
  96. graphcore/docs/docs_discovery.py +64 -0
  97. graphcore/docs/public_api.py +56 -0
  98. graphcore/domain/__init__.py +0 -0
  99. graphcore/domain/entity/Entity.py +204 -0
  100. graphcore/domain/entity/__init__.py +0 -0
  101. graphcore/domain/entity/deployment/Deployment.py +25 -0
  102. graphcore/domain/entity/deployment/__init__.py +0 -0
  103. graphcore/domain/entity/grant/Grant.py +31 -0
  104. graphcore/domain/entity/grant/__init__.py +0 -0
  105. graphcore/domain/entity/group/Group.py +48 -0
  106. graphcore/domain/entity/group/README.md +88 -0
  107. graphcore/domain/entity/group/__init__.py +0 -0
  108. graphcore/domain/entity/invite/Invite.py +31 -0
  109. graphcore/domain/entity/invite/__init__.py +0 -0
  110. graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
  111. graphcore/domain/entity/request_template/__init__.py +0 -0
  112. graphcore/domain/entity/request_template/tests/__init__.py +0 -0
  113. graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
  114. graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
  115. graphcore/domain/entity/service_account/__init__.py +0 -0
  116. graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
  117. graphcore/domain/entity/strategy_request/__init__.py +0 -0
  118. graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
  119. graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
  120. graphcore/domain/entity/tests/__init__.py +0 -0
  121. graphcore/domain/entity/tests/test_entity.py +227 -0
  122. graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
  123. graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
  124. graphcore/domain/entity/user/User.py +39 -0
  125. graphcore/domain/entity/user/__init__.py +0 -0
  126. graphcore/domain/entity/user/tests/__init__.py +0 -0
  127. graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
  128. graphcore/domain/entity/view/View.py +37 -0
  129. graphcore/domain/entity/view/__init__.py +0 -0
  130. graphcore/domain/entity/view/position.py +78 -0
  131. graphcore/domain/entity/view/tests/__init__.py +0 -0
  132. graphcore/domain/entity/view/tests/test_position.py +71 -0
  133. graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
  134. graphcore/domain/strategy/BaseStrategy.py +759 -0
  135. graphcore/domain/strategy/DeploymentStrategies.py +55 -0
  136. graphcore/domain/strategy/GrantStrategies.py +189 -0
  137. graphcore/domain/strategy/GroupStrategies.py +193 -0
  138. graphcore/domain/strategy/InviteStrategies.py +84 -0
  139. graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
  140. graphcore/domain/strategy/ViewStrategies.py +219 -0
  141. graphcore/domain/strategy/__init__.py +0 -0
  142. graphcore/domain/strategy/runner.py +84 -0
  143. graphcore/domain/strategy/session.py +56 -0
  144. graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
  145. graphcore/domain/strategy/tests/__init__.py +0 -0
  146. graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
  147. graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
  148. graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
  149. graphcore/domain/strategy/tests/test_runner.py +220 -0
  150. graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
  151. graphcore/domain/strategy/tests/test_utils.py +95 -0
  152. graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
  153. graphcore/errors.py +145 -0
  154. graphcore/execution/README.md +97 -0
  155. graphcore/execution/StrategyExecutorService.py +333 -0
  156. graphcore/execution/__init__.py +0 -0
  157. graphcore/execution/dispatcher.py +19 -0
  158. graphcore/execution/environments/__init__.py +0 -0
  159. graphcore/execution/environments/celery.py +59 -0
  160. graphcore/execution/environments/in_process.py +55 -0
  161. graphcore/execution/local_session.py +117 -0
  162. graphcore/execution/redis_run_events.py +74 -0
  163. graphcore/execution/requests.py +22 -0
  164. graphcore/execution/run_events.py +85 -0
  165. graphcore/execution/strategy_context.py +54 -0
  166. graphcore/execution/tasks.py +34 -0
  167. graphcore/execution/tests/__init__.py +5 -0
  168. graphcore/execution/tests/contract.py +171 -0
  169. graphcore/execution/tests/support.py +310 -0
  170. graphcore/execution/tests/test_dispatchers.py +76 -0
  171. graphcore/execution/tests/test_executor.py +368 -0
  172. graphcore/execution/tests/test_local_session.py +95 -0
  173. graphcore/execution/tests/test_reconcile_runs.py +134 -0
  174. graphcore/execution/tests/test_run_events.py +106 -0
  175. graphcore/execution/tests/test_strategy_context.py +62 -0
  176. graphcore/gateway/__init__.py +1 -0
  177. graphcore/gateway/remote.py +35 -0
  178. graphcore/gateway/tests/__init__.py +0 -0
  179. graphcore/gateway/tests/test_remote.py +30 -0
  180. graphcore/graph/EntityService.py +710 -0
  181. graphcore/graph/__init__.py +0 -0
  182. graphcore/graph/entity_cache.py +152 -0
  183. graphcore/graph/tests/__init__.py +4 -0
  184. graphcore/graph/tests/test_EntityService.py +908 -0
  185. graphcore/graph/tests/test_EntityService_list.py +170 -0
  186. graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
  187. graphcore/graph/tests/test_EntityService_referrers.py +236 -0
  188. graphcore/graph/tests/test_EntityService_routing.py +119 -0
  189. graphcore/graph/tests/test_dependent_on.py +141 -0
  190. graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
  191. graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
  192. graphcore/graph/tests/test_entity_cache.py +159 -0
  193. graphcore/graph/tests/test_null_entity_cache.py +12 -0
  194. graphcore/management/__init__.py +0 -0
  195. graphcore/management/commands/__init__.py +0 -0
  196. graphcore/management/commands/dev_reset.py +18 -0
  197. graphcore/management/commands/graphcore_createadmin.py +65 -0
  198. graphcore/management/commands/graphcore_policy.py +59 -0
  199. graphcore/management/commands/graphcore_status.py +98 -0
  200. graphcore/management/tests/__init__.py +0 -0
  201. graphcore/management/tests/test_createadmin.py +78 -0
  202. graphcore/management/tests/test_policy_command.py +46 -0
  203. graphcore/management/tests/test_status_command.py +85 -0
  204. graphcore/migrations/0001_initial.py +186 -0
  205. graphcore/migrations/0002_invite.py +26 -0
  206. graphcore/migrations/__init__.py +0 -0
  207. graphcore/models.py +11 -0
  208. graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
  209. graphcore/persistence/__init__.py +6 -0
  210. graphcore/persistence/django/__init__.py +0 -0
  211. graphcore/persistence/django/backends.py +43 -0
  212. graphcore/persistence/django/bindings.py +28 -0
  213. graphcore/persistence/django/cache.py +38 -0
  214. graphcore/persistence/django/compiler.py +214 -0
  215. graphcore/persistence/django/identity.py +18 -0
  216. graphcore/persistence/django/models.py +252 -0
  217. graphcore/persistence/django/repository.py +525 -0
  218. graphcore/persistence/django/tests/__init__.py +0 -0
  219. graphcore/persistence/django/tests/test_backends.py +68 -0
  220. graphcore/persistence/django/tests/test_compiler.py +269 -0
  221. graphcore/persistence/django/tests/test_repository.py +472 -0
  222. graphcore/persistence/django/tests/test_shared_column.py +120 -0
  223. graphcore/persistence/django/tests/test_threads.py +50 -0
  224. graphcore/persistence/django/tests/test_update_shape.py +175 -0
  225. graphcore/persistence/django/threads.py +38 -0
  226. graphcore/persistence/repository.py +172 -0
  227. graphcore/persistence/tests/__init__.py +0 -0
  228. graphcore/persistence/tests/contract.py +688 -0
  229. graphcore/persistence/tests/evaluate.py +51 -0
  230. graphcore/persistence/tests/fixtures.py +32 -0
  231. graphcore/persistence/tests/memory.py +263 -0
  232. graphcore/persistence/tests/test_memory_repository.py +10 -0
  233. graphcore/persistence/tests/test_query_types.py +127 -0
  234. graphcore/public/__init__.py +9 -0
  235. graphcore/public/api.py +9 -0
  236. graphcore/public/apps.py +10 -0
  237. graphcore/public/auth/__init__.py +1 -0
  238. graphcore/public/auth/grants.py +7 -0
  239. graphcore/public/auth/onboarding.py +8 -0
  240. graphcore/public/auth/perms.py +7 -0
  241. graphcore/public/auth/policy.py +10 -0
  242. graphcore/public/auth/roles.py +8 -0
  243. graphcore/public/auth/subjects.py +7 -0
  244. graphcore/public/entities/__init__.py +32 -0
  245. graphcore/public/errors.py +19 -0
  246. graphcore/public/persistence/__init__.py +44 -0
  247. graphcore/public/persistence/django.py +24 -0
  248. graphcore/public/scheduling.py +23 -0
  249. graphcore/public/strategies/__init__.py +85 -0
  250. graphcore/public/test_settings.py +16 -0
  251. graphcore/public/testing.py +47 -0
  252. graphcore/registry/EntityDirectory.py +246 -0
  253. graphcore/registry/README.md +147 -0
  254. graphcore/registry/StrategyDirectory.py +308 -0
  255. graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
  256. graphcore/registry/__init__.py +0 -0
  257. graphcore/registry/app_discovery.py +108 -0
  258. graphcore/registry/decorators.py +77 -0
  259. graphcore/registry/discovery.py +111 -0
  260. graphcore/registry/registry.py +79 -0
  261. graphcore/registry/tests/__init__.py +0 -0
  262. graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
  263. graphcore/registry/tests/test_app_config.py +36 -0
  264. graphcore/registry/tests/test_discovery.py +147 -0
  265. graphcore/registry/tests/test_entity_directory_storage.py +73 -0
  266. graphcore/registry/tests/test_import_package.py +87 -0
  267. graphcore/registry/tests/test_principal_flag.py +67 -0
  268. graphcore/registry/tests/test_strategy_directory.py +29 -0
  269. graphcore/routes/__init__.py +116 -0
  270. graphcore/routes/tests/__init__.py +0 -0
  271. graphcore/routes/tests/test_routes.py +90 -0
  272. graphcore/runtime.py +243 -0
  273. graphcore/scheduling/__init__.py +0 -0
  274. graphcore/scheduling/beat_scheduler.py +164 -0
  275. graphcore/scheduling/beat_tasks.py +63 -0
  276. graphcore/scheduling/scheduler.py +37 -0
  277. graphcore/scheduling/tests/__init__.py +0 -0
  278. graphcore/scheduling/tests/contract.py +136 -0
  279. graphcore/scheduling/tests/memory.py +42 -0
  280. graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
  281. graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
  282. graphcore/scheduling/tests/test_ticks.py +143 -0
  283. graphcore/tasks.py +9 -0
  284. graphcore/test_settings.py +78 -0
  285. graphcore/testing/__init__.py +0 -0
  286. graphcore/testing/case.py +112 -0
  287. graphcore/testing/runner.py +32 -0
  288. graphcore/testing/settings.py +26 -0
  289. graphcore/testing/tests/__init__.py +0 -0
  290. graphcore/testing/tests/agent_view_fixture.py +19 -0
  291. graphcore/testing/tests/test_case_and_runner.py +219 -0
  292. graphcore/testing/tests/test_settings_for_apps.py +50 -0
  293. graphcore/tests/__init__.py +0 -0
  294. graphcore/tests/ids.py +14 -0
  295. graphcore/tests/public_api_manifest.py +102 -0
  296. graphcore/tests/runs.py +7 -0
  297. graphcore/tests/test_app_discovery.py +90 -0
  298. graphcore/tests/test_composition_boundaries.py +231 -0
  299. graphcore/tests/test_docs_discovery.py +108 -0
  300. graphcore/tests/test_no_print.py +70 -0
  301. graphcore/tests/test_packaging.py +64 -0
  302. graphcore/tests/test_public_api_boundaries.py +229 -0
  303. graphcore/tests/test_public_api_contract.py +62 -0
  304. graphcore/tests/test_public_api_docs.py +28 -0
  305. graphcore/tests/test_runtime.py +42 -0
  306. graphcore/tests/test_shared_type_boundaries.py +128 -0
  307. graphcore/tests/test_shared_types.py +144 -0
  308. graphcore/tests/test_test_settings.py +45 -0
  309. graphcore/transport/__init__.py +0 -0
  310. graphcore/transport/agent_asgi.py +68 -0
  311. graphcore/transport/agent_client.py +85 -0
  312. graphcore/transport/channel_layer.py +68 -0
  313. graphcore/transport/consumers.py +507 -0
  314. graphcore/transport/routing.py +6 -0
  315. graphcore/transport/tests/__init__.py +0 -0
  316. graphcore/transport/tests/test_agent_client.py +31 -0
  317. graphcore/transport/tests/test_api_views.py +733 -0
  318. graphcore/transport/tests/test_channel_layer.py +96 -0
  319. graphcore/transport/tests/test_consumers.py +963 -0
  320. graphcore/transport/tests/test_resource_metadata.py +67 -0
  321. graphcore/transport/urls.py +29 -0
  322. graphcore/transport/views.py +358 -0
  323. graphcore/types/__init__.py +4 -0
  324. graphcore/types/access.py +114 -0
  325. graphcore/types/api.py +76 -0
  326. graphcore/types/entities.py +100 -0
  327. graphcore/types/entity_id.py +85 -0
  328. graphcore/types/execution.py +41 -0
  329. graphcore/types/identity.py +20 -0
  330. graphcore/types/onboarding.py +20 -0
  331. graphcore/types/query.py +116 -0
  332. graphcore/types/schedules.py +91 -0
  333. graphcore/types/strategy.py +73 -0
  334. graphcore/types/tests/__init__.py +0 -0
  335. graphcore/types/tests/test_entity_id.py +50 -0
  336. graphcore/urls.py +5 -0
  337. graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
  338. graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
  339. graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
  340. graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
  341. graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
  342. graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
  343. graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,507 @@
1
+ """WebSocket adapter for the GraphAPI. Push/subscription lifecycle stays here."""
2
+ import asyncio
3
+ import json
4
+ import logging
5
+ import time
6
+ from typing import Optional
7
+
8
+ from channels.generic.websocket import AsyncWebsocketConsumer
9
+
10
+ from graphcore.runtime import get_runtime
11
+ from graphcore.types.identity import Principal
12
+ from graphcore.types.api import StrategyRequestPayload
13
+ from graphcore.types.entities import Tombstone, client_view
14
+ from graphcore.auth.authn import authenticate
15
+ from graphcore.errors import InvalidRequest, log_and_correlate
16
+ from graphcore.broadcast.channels import owner_channel, scope_channel
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+ BEARER_SUBPROTOCOL = 'graphcore.bearer'
21
+
22
+
23
+ class GlobalEntityConsumer(AsyncWebsocketConsumer):
24
+ """Handles authenticated WebSocket connections with user-scoped entity updates"""
25
+
26
+ def _extract_token(self) -> Optional[str]:
27
+ """The token offered as ``Sec-WebSocket-Protocol: graphcore.bearer, <token>``.
28
+ Not the query string: servers and proxies log URLs."""
29
+ offered = self.scope.get('subprotocols') or []
30
+ if len(offered) == 2 and offered[0] == BEARER_SUBPROTOCOL:
31
+ return offered[1]
32
+ return None
33
+
34
+ async def accept(self, subprotocol=None):
35
+ if BEARER_SUBPROTOCOL in (self.scope.get('subprotocols') or []):
36
+ subprotocol = BEARER_SUBPROTOCOL
37
+ await super().accept(subprotocol)
38
+
39
+ def _arm_expiry(self, principal: Principal) -> None:
40
+ """Close the socket when the token it authenticated with expires, unless
41
+ a ``reauth`` replaces it first."""
42
+ self._cancel_expiry()
43
+ if principal.expires_at is not None:
44
+ self._expiry_task = asyncio.create_task(self._close_at(principal.expires_at))
45
+
46
+ def _cancel_expiry(self) -> None:
47
+ task = getattr(self, '_expiry_task', None)
48
+ if task is not None and task is not asyncio.current_task():
49
+ task.cancel()
50
+ self._expiry_task = None
51
+
52
+ def _token_expired(self) -> bool:
53
+ """Wall clock: the expiry timer's monotonic clock stops while the host sleeps."""
54
+ principal = self.scope.get('principal')
55
+ return principal is not None and principal.expires_at is not None and time.time() >= principal.expires_at
56
+
57
+ async def _close_at(self, expires_at: int) -> None:
58
+ await asyncio.sleep(max(0, expires_at - time.time()))
59
+ await self._reject('Authentication token expired', 'TOKEN_EXPIRED')
60
+
61
+ async def _reject(self, message: str, code: str) -> None:
62
+ await self.send(json.dumps({'type': 'auth_error', 'message': message, 'code': code}))
63
+ await self.close(code=4002)
64
+
65
+ async def handle_reauth(self, token) -> None:
66
+ """Swap in a renewed token for the same identity without dropping the connection."""
67
+ principal = await authenticate(token) if isinstance(token, str) else None
68
+ if principal is None or principal.principal_entity_id != self.get_principal_entity_id():
69
+ await self._reject('Authentication token is invalid or expired', 'INVALID_TOKEN')
70
+ return
71
+ self.scope['principal'] = principal
72
+ self._arm_expiry(principal)
73
+ await self.send(json.dumps({'type': 'reauthenticated', 'expires_at': principal.expires_at}))
74
+
75
+ def get_principal_entity_id(self) -> str:
76
+ """Get current user's entity ID from connection scope."""
77
+ return self.scope.get('principal_entity_id')
78
+
79
+ def _principal(self) -> Principal:
80
+ return self.scope.get('principal') or Principal(principal_entity_id=self.get_principal_entity_id())
81
+
82
+ async def connect(self):
83
+ """Accept connection after JWT validation and join user-specific group"""
84
+ logger.info("Connecting to WebSocket")
85
+ # Set once: every later message of this connection runs in this task.
86
+ get_runtime().repository.use_shared_threads()
87
+ try:
88
+ token = self._extract_token()
89
+
90
+ if not token:
91
+ logger.warning("WebSocket connection rejected: no token provided")
92
+ await self.accept()
93
+ await self.send(json.dumps({
94
+ 'type': 'auth_error',
95
+ 'message': 'No authentication token provided',
96
+ 'code': 'NO_TOKEN'
97
+ }))
98
+ await self.close(code=4001)
99
+ return
100
+
101
+ principal = await authenticate(token)
102
+
103
+ if not principal:
104
+ logger.warning("WebSocket connection rejected: invalid token")
105
+ await self.accept()
106
+ await self.send(json.dumps({
107
+ 'type': 'auth_error',
108
+ 'message': 'Authentication token is invalid or expired',
109
+ 'code': 'INVALID_TOKEN'
110
+ }))
111
+ await self.close(code=4002)
112
+ return
113
+
114
+ self.scope['principal'] = principal
115
+ self.scope['principal_entity_id'] = principal.principal_entity_id
116
+
117
+ await self.accept()
118
+ self._arm_expiry(principal)
119
+
120
+ principal_entity_id = principal.principal_entity_id
121
+ broadcaster = get_runtime().broadcaster
122
+ await self.channel_layer.group_add(
123
+ owner_channel(principal_entity_id), self.channel_name
124
+ )
125
+ await self.channel_layer.group_add("public", self.channel_name)
126
+
127
+ broadcaster.register_connection(principal_entity_id, self.channel_name)
128
+
129
+ group_subscriptions_degraded = False
130
+ try:
131
+ scope_ids = list(await broadcaster.access.granted_resources(principal))
132
+ except Exception:
133
+ log_and_correlate(
134
+ logger,
135
+ f"Granted-resource lookup failed for user {principal_entity_id}; "
136
+ f"scope subscriptions degraded for this connection",
137
+ )
138
+ scope_ids = []
139
+ group_subscriptions_degraded = True
140
+ self.scope['scope_ids'] = scope_ids
141
+ for scope_id in scope_ids:
142
+ await self.channel_layer.group_add(scope_channel(scope_id), self.channel_name)
143
+
144
+ await self.send(json.dumps({
145
+ 'type': 'connected',
146
+ 'message': 'Connected to user-scoped entity updates',
147
+ 'principal_entity_id': principal_entity_id,
148
+ 'group_subscriptions_degraded': group_subscriptions_degraded,
149
+ 'max_batch_size': get_runtime().graph_api.max_batch_size,
150
+ }))
151
+ logger.info(f"WebSocket connection established for user {principal_entity_id}")
152
+
153
+ except Exception as e:
154
+ logger.error(f"Error in connect: {str(e)}")
155
+ await self.close(code=4003)
156
+
157
+ async def disconnect(self, close_code):
158
+ """Remove from user-specific and scope groups on disconnect"""
159
+ logger.info(f"Disconnecting from WebSocket with code: {close_code}")
160
+ self._cancel_expiry()
161
+ try:
162
+ principal_entity_id = self.get_principal_entity_id()
163
+ if principal_entity_id:
164
+ await self.channel_layer.group_discard(
165
+ owner_channel(principal_entity_id), self.channel_name
166
+ )
167
+ get_runtime().broadcaster.deregister_connection(principal_entity_id, self.channel_name)
168
+ await self.channel_layer.group_discard("public", self.channel_name)
169
+ for scope_id in self.scope.get('scope_ids', []):
170
+ await self.channel_layer.group_discard(scope_channel(scope_id), self.channel_name)
171
+ except Exception as e:
172
+ logger.error(f"Error in disconnect: {str(e)}")
173
+
174
+ async def receive(self, text_data):
175
+ """Handle incoming messages from the client"""
176
+ try:
177
+ data = json.loads(text_data)
178
+ command = data.get('command')
179
+ logger.debug(f"Received WebSocket command: {command}")
180
+
181
+ if command != 'reauth' and self._token_expired():
182
+ await self._reject('Authentication token expired', 'TOKEN_EXPIRED')
183
+ return
184
+
185
+ if command == 'subscribe_entities':
186
+ entity_ids = data.get('entity_ids', [])
187
+ if len(entity_ids) > get_runtime().graph_api.max_batch_size:
188
+ await self.send(json.dumps({
189
+ 'type': 'error',
190
+ 'message': f'entity_ids exceeds the maximum of {get_runtime().graph_api.max_batch_size}'
191
+ }))
192
+ else:
193
+ await self.handle_entity_subscriptions(entity_ids)
194
+ elif command == 'execute_strategy':
195
+ await_response = data.get('await_response', False)
196
+ strategy_data = data.get('strategy') or []
197
+ if len(strategy_data) > get_runtime().graph_api.max_batch_size:
198
+ await self.send(json.dumps({
199
+ 'type': 'error',
200
+ 'message': f'strategy list exceeds the maximum of {get_runtime().graph_api.max_batch_size}'
201
+ }))
202
+ else:
203
+ await self.handle_execute_strategy(strategy_data, await_response=await_response)
204
+ elif command == 'get_entity':
205
+ entity_id = data.get('entity_id')
206
+ await self.handle_get_entity(entity_id)
207
+ elif command == 'get_entities':
208
+ entity_ids = data.get('entity_ids', [])
209
+ if len(entity_ids) > get_runtime().graph_api.max_batch_size:
210
+ await self.send(json.dumps({
211
+ 'type': 'error',
212
+ 'message': f'entity_ids exceeds the maximum of {get_runtime().graph_api.max_batch_size}'
213
+ }))
214
+ else:
215
+ await self.handle_get_entities(entity_ids)
216
+ elif command == 'resync':
217
+ await self.handle_resync(data.get('resync_id'), data.get('entities'))
218
+ elif command == 'reauth':
219
+ await self.handle_reauth(data.get('token'))
220
+ elif command == 'ping':
221
+ await self.send(json.dumps({
222
+ 'type': 'pong',
223
+ 'message': 'Pong'
224
+ }))
225
+ else:
226
+ logger.warning(f"Unknown command received: {command}")
227
+ await self.send(json.dumps({
228
+ 'type': 'error',
229
+ 'message': f'Unknown command: {command}'
230
+ }))
231
+ except Exception:
232
+ correlation_id = log_and_correlate(logger, "Error in receive")
233
+ await self.send(json.dumps({
234
+ 'type': 'error',
235
+ 'message': 'Internal server error',
236
+ 'correlation_id': correlation_id
237
+ }))
238
+
239
+ async def handle_entity_subscriptions(self, entity_ids):
240
+ """Handle initial entity subscriptions with authorization"""
241
+ try:
242
+ logger.info(f"Setting up subscriptions for entities: {entity_ids}")
243
+ entities = await get_runtime().graph_api.get_entities(self._principal(), entity_ids)
244
+ entities_data = {
245
+ eid: client_view(e)
246
+ for eid, e in entities.items()
247
+ }
248
+
249
+ if entities_data:
250
+ await self.send(json.dumps({
251
+ 'type': 'entity_update',
252
+ 'entities': entities_data
253
+ }))
254
+
255
+ await self.send(json.dumps({
256
+ 'type': 'subscriptions_complete',
257
+ 'message': 'Entity subscriptions processed'
258
+ }))
259
+
260
+ except Exception:
261
+ correlation_id = log_and_correlate(logger, "Error setting up subscriptions")
262
+ await self.send(json.dumps({
263
+ 'type': 'error',
264
+ 'message': 'Failed to set up subscriptions',
265
+ 'correlation_id': correlation_id
266
+ }))
267
+
268
+ async def handle_get_entity(self, entity_id):
269
+ """Handle request for a single entity by ID with authorization"""
270
+ try:
271
+ if not entity_id:
272
+ await self.send(json.dumps({
273
+ 'type': 'error',
274
+ 'message': 'No entity_id provided'
275
+ }))
276
+ return
277
+
278
+ logger.debug(f"Fetching entity: {entity_id}")
279
+ result = await get_runtime().graph_api.get_entity(self._principal(), entity_id)
280
+
281
+ if isinstance(result, Tombstone):
282
+ if result.cause == 'forbidden':
283
+ # Same contract as the subscribe_entities path: send it
284
+ # through entity_update/client_view() so the client's
285
+ # entity store gets a `forbidden: true` record, not a
286
+ # dead end that leaves it "loading" forever.
287
+ await self.send(json.dumps({
288
+ 'type': 'entity_update',
289
+ 'entities': {entity_id: client_view(result)}
290
+ }))
291
+ else:
292
+ await self.send(json.dumps({
293
+ 'type': 'entity_not_found',
294
+ 'entity_id': entity_id,
295
+ 'message': f'Entity {entity_id} not found'
296
+ }))
297
+ return
298
+
299
+ await self.send(json.dumps({
300
+ 'type': 'entity_update',
301
+ 'entities': {entity_id: client_view(result)}
302
+ }))
303
+
304
+ except Exception:
305
+ correlation_id = log_and_correlate(logger, f"Error fetching entity {entity_id}")
306
+ await self.send(json.dumps({
307
+ 'type': 'error',
308
+ 'message': f'Failed to fetch entity {entity_id}',
309
+ 'correlation_id': correlation_id
310
+ }))
311
+
312
+ async def handle_get_entities(self, entity_ids):
313
+ """Bulk-fetch entities by ID. Returns a single entity_update with all found entities."""
314
+ try:
315
+ if not entity_ids:
316
+ await self.send(json.dumps({
317
+ 'type': 'error',
318
+ 'message': 'No entity_ids provided'
319
+ }))
320
+ return
321
+
322
+ entities = await get_runtime().graph_api.get_entities(self._principal(), entity_ids)
323
+
324
+ if entities:
325
+ await self.send(json.dumps({
326
+ 'type': 'entity_update',
327
+ 'entities': {
328
+ eid: client_view(e)
329
+ for eid, e in entities.items()
330
+ }
331
+ }))
332
+
333
+ except Exception:
334
+ correlation_id = log_and_correlate(logger, "Error in handle_get_entities")
335
+ await self.send(json.dumps({
336
+ 'type': 'error',
337
+ 'message': 'Failed to fetch entities',
338
+ 'correlation_id': correlation_id
339
+ }))
340
+
341
+ async def handle_resync(self, resync_id, held):
342
+ """Send what changed among the entities the client holds, then
343
+ `resync_done`, which ends the client's resync even on failure."""
344
+ error = None
345
+ try:
346
+ if not isinstance(held, dict):
347
+ raise InvalidRequest('entities must map entity ids to etags')
348
+ changed = await get_runtime().graph_api.changed_entities(self._principal(), held)
349
+ if changed:
350
+ await self.send(json.dumps({
351
+ 'type': 'entity_update',
352
+ 'entities': {eid: client_view(e) for eid, e in changed.items()},
353
+ }))
354
+ except InvalidRequest as exc:
355
+ error = str(exc)
356
+ except Exception:
357
+ error = f'Resync failed (correlation_id={log_and_correlate(logger, "Error in resync")})'
358
+ await self.send(json.dumps({'type': 'resync_done', 'resync_id': resync_id, 'error': error}))
359
+
360
+ async def handle_execute_strategy(self, strategy_data, await_response=False):
361
+ """Handle strategy execution via WebSocket with authorization. Wire
362
+ format and error mapping only; execution goes through `GraphAPI`."""
363
+ try:
364
+ logger.info(f"Executing strategy (await_response={await_response})")
365
+ principal = self._principal()
366
+
367
+ payloads = []
368
+ for strategy in strategy_data:
369
+ try:
370
+ payloads.append(StrategyRequestPayload.from_json(strategy))
371
+ except InvalidRequest as exc:
372
+ await self.send(json.dumps({
373
+ 'type': 'error',
374
+ 'message': str(exc)
375
+ }))
376
+ if not payloads:
377
+ return
378
+
379
+ outcomes = await get_runtime().graph_api.execute_strategies(
380
+ principal, payloads, wait=False,
381
+ callback_channel=self.channel_name if await_response else None,
382
+ )
383
+
384
+ for outcome in outcomes:
385
+ if outcome.strategy_request_id is not None:
386
+ if await_response:
387
+ await self.send(json.dumps({
388
+ 'type': 'strategy_queued',
389
+ 'request_id': outcome.strategy_request_id,
390
+ 'target_entity_id': outcome.target_entity_id,
391
+ 'client_request_id': outcome.client_request_id,
392
+ 'status': outcome.status,
393
+ }))
394
+ else:
395
+ await self.send(json.dumps({
396
+ 'type': 'strategy_executed',
397
+ 'request_id': outcome.strategy_request_id,
398
+ 'status': outcome.status,
399
+ }))
400
+ elif await_response:
401
+ await self.send(json.dumps({
402
+ 'type': 'strategy_result',
403
+ 'request_id': None,
404
+ 'target_entity_id': outcome.target_entity_id,
405
+ 'client_request_id': outcome.client_request_id,
406
+ 'status': 'failed',
407
+ 'success': False,
408
+ 'error': outcome.error,
409
+ 'error_code': outcome.error_code,
410
+ 'error_details': outcome.error_details,
411
+ }))
412
+ else:
413
+ await self.send(json.dumps({
414
+ 'type': 'error',
415
+ 'message': outcome.error
416
+ }))
417
+
418
+ except Exception:
419
+ correlation_id = log_and_correlate(logger, "Error executing strategy")
420
+ await self.send(json.dumps({
421
+ 'type': 'error',
422
+ 'message': f'Internal server error (correlation_id={correlation_id})'
423
+ }))
424
+
425
+ async def strategy_complete(self, event):
426
+ """A run this connection submitted with `await_response` ended
427
+ (sent by the executor through the channel layer)."""
428
+ try:
429
+ await self.send(json.dumps({
430
+ 'type': 'strategy_result',
431
+ 'request_id': event.get('request_id'),
432
+ 'target_entity_id': event.get('target_entity_id'),
433
+ 'client_request_id': event.get('client_request_id'),
434
+ 'status': event.get('status'),
435
+ 'success': event['success'],
436
+ 'ret_val': event.get('ret_val', {}),
437
+ 'error': event.get('error'),
438
+ 'error_code': event.get('code'),
439
+ 'error_details': event.get('details') or {},
440
+ }))
441
+ except Exception as e:
442
+ logger.error(f"Error in strategy_complete callback: {str(e)}")
443
+
444
+ async def resync_requested(self, event):
445
+ """This process's Redis subscription reconnected, so updates may have
446
+ been lost (sent by `PubSubChannelLayer`)."""
447
+ await self.send(json.dumps({'type': 'resync'}))
448
+
449
+ async def entity_update(self, event):
450
+ """Handle entity updates and send to clients (called by channel layer)"""
451
+ try:
452
+ await self.send(text_data=json.dumps({
453
+ 'type': 'entity_update',
454
+ 'entities': event['entities']
455
+ }))
456
+ except Exception as e:
457
+ logger.error(f"Error in entity_update: {str(e)}")
458
+
459
+ async def reference_update(self, event):
460
+ """A reference to some target was added or removed by an entity this
461
+ connection can read (called by the channel layer)."""
462
+ try:
463
+ await self.send(text_data=json.dumps({
464
+ 'type': 'reference_update',
465
+ 'target_entity_id': event['target_entity_id'],
466
+ 'source_entity_id': event['source_entity_id'],
467
+ 'change': event['change'],
468
+ }))
469
+ except Exception as e:
470
+ logger.error(f"Error in reference_update: {str(e)}")
471
+
472
+ async def access_granted(self, event):
473
+ """A fresh grant on `resource_id` - the broadcaster already joined
474
+ this channel to `scope_{resource_id}`; track it for disconnect
475
+ cleanup and notify the client (no data backfill - see
476
+ docs/GRAPHAPI_FRONT_DOOR.md)."""
477
+ resource_id = event.get('resource_id')
478
+ scope_ids = self.scope.setdefault('scope_ids', [])
479
+ if resource_id and resource_id not in scope_ids:
480
+ scope_ids.append(resource_id)
481
+ await self.send(json.dumps({
482
+ 'type': 'access_granted',
483
+ 'resource_id': resource_id,
484
+ 'role': event.get('role'),
485
+ }))
486
+
487
+ async def access_revoked(self, event):
488
+ """A grant on `resource_id` was revoked - the broadcaster already
489
+ left `scope_{resource_id}`; drop it from disconnect bookkeeping and
490
+ notify the client."""
491
+ resource_id = event.get('resource_id')
492
+ scope_ids = self.scope.get('scope_ids', [])
493
+ if resource_id in scope_ids:
494
+ scope_ids.remove(resource_id)
495
+ await self.send(json.dumps({
496
+ 'type': 'access_revoked',
497
+ 'resource_id': resource_id,
498
+ }))
499
+
500
+ async def access_changed(self, event):
501
+ """A role change with the same read reach - no channel join/leave,
502
+ just a client-visible notification."""
503
+ await self.send(json.dumps({
504
+ 'type': 'access_changed',
505
+ 'resource_id': event.get('resource_id'),
506
+ 'role': event.get('role'),
507
+ }))
@@ -0,0 +1,6 @@
1
+ from django.urls import re_path
2
+ from graphcore.transport import consumers
3
+
4
+ websocket_urlpatterns = [
5
+ re_path(r'ws/entities/$', consumers.GlobalEntityConsumer.as_asgi()),
6
+ ]
File without changes
@@ -0,0 +1,31 @@
1
+ """`InProcessGraphClient` over a given `GraphAPI`."""
2
+ from graphcore.public.strategies import SetAttributes
3
+ from graphcore.public.testing import GraphCoreTestCase
4
+ from graphcore.runtime import django_runtime
5
+ from graphcore.testing.tests.test_case_and_runner import TEST_REGISTRY, _Scope
6
+ from graphcore.transport.agent_client import InProcessGraphClient
7
+
8
+
9
+ class InProcessGraphClientTests(GraphCoreTestCase):
10
+ def setUp(self):
11
+ super().setUp()
12
+ self.runtime = django_runtime(TEST_REGISTRY)
13
+ self.api = self.runtime.graph_api
14
+ self.client = InProcessGraphClient(self.api)
15
+
16
+ async def test_reads_through_the_given_api(self):
17
+ owner = await self.create_user('owner')
18
+ scope = await self.create_entity(_Scope, owner)
19
+
20
+ self.assertEqual((await self.client.get_entity(owner, scope.entity_id)).entity_id, scope.entity_id)
21
+
22
+ async def test_a_refused_run_is_a_failed_outcome(self):
23
+ owner = await self.create_user('owner')
24
+ stranger = await self.create_user('stranger')
25
+ scope = await self.create_entity(_Scope, owner)
26
+
27
+ outcome = await self.client.execute(
28
+ stranger, SetAttributes.__name__, scope.entity_id, {'attribute_map': {'proof': 1}},
29
+ )
30
+
31
+ self.assertEqual(outcome['status'], 'failed')