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,436 @@
1
+ """`SetViewState`, `CreateView` and the `View`
2
+ policy, through the executor - see docs/VIEW_ENTITY_TDD.md."""
3
+ from unittest.mock import patch
4
+
5
+ from asgiref.sync import async_to_sync
6
+ from django.test import TransactionTestCase
7
+
8
+ from graphcore.bootstrap.deployment import ensure_deployment
9
+ from graphcore.domain.entity.user.User import User
10
+ from graphcore.domain.entity.view.View import View
11
+ from graphcore.domain.entity.view.position import position_key
12
+ from graphcore.domain.strategy.BaseStrategy import (
13
+ CreateEntity, DeleteEntity, ReparentChild,
14
+ )
15
+ from graphcore.domain.strategy.ViewStrategies import CreateView, SetViewState
16
+ from graphcore.errors import Forbidden, InvalidRequest
17
+ from graphcore.execution.strategy_context import kernel_internal_context
18
+ from graphcore.registry.registry import DEFAULT_REGISTRY
19
+ from graphcore.runtime import django_runtime
20
+ from graphcore.types.entities import EntityFilter
21
+ from graphcore.types.identity import Principal
22
+ from graphcore.types.query import AllOf, ContainsValue, Equal
23
+ from graphcore.tests.runs import run
24
+
25
+ VIEW_TYPES = frozenset({View.get_entity_type_id()})
26
+
27
+
28
+ def _address(entity_id):
29
+ return '/e/' + entity_id[len('gc://'):]
30
+
31
+
32
+ class _ViewFixture(TransactionTestCase):
33
+ def setUp(self):
34
+ self.runtime = django_runtime(DEFAULT_REGISTRY)
35
+ self.service = self.runtime.entity_service
36
+ self.executor = self.runtime.executor
37
+
38
+ async def _user(self, username):
39
+ user = self.service.create(User, username=username)
40
+ with kernel_internal_context():
41
+ await self.service.save_entity(user, Principal(principal_entity_id=user.entity_id))
42
+ return Principal(principal_entity_id=user.entity_id)
43
+
44
+ async def _seed(self):
45
+ await ensure_deployment(self.service, DEFAULT_REGISTRY)
46
+ self.alice = await self._user('view-alice')
47
+ self.bob = await self._user('view-bob')
48
+ self.a = self.alice.principal_entity_id
49
+
50
+ async def _run(self, payload, principal):
51
+ request, _ = await run(self.executor, payload, principal=principal)
52
+ return request.ret_val
53
+
54
+ async def _set(self, principal, state, address=None, placement=None, view=None, target=None,
55
+ client=None, **params):
56
+ if target is None and client is None:
57
+ client = 'web'
58
+ payload = SetViewState.request_constructor(
59
+ target, state=state, client=client, address=address, view=view,
60
+ placement=[{'slot': slot, 'renders': renders} for slot, renders in placement or []] or None,
61
+ **params,
62
+ )
63
+ return (await self._run(payload, principal))['view_entity_id']
64
+
65
+ async def _entity(self, principal):
66
+ payload = CreateEntity.request_constructor(None, entity_type_id='graphcore.Entity')
67
+ return (await self._run(payload, principal))['child_entity']
68
+
69
+ async def _explicit_view(self, principal, parent=None, renders=None):
70
+ payload = CreateView.request_constructor(parent, renders=renders, component='composite_hbox')
71
+ return (await self._run(payload, principal))['view_entity_id']
72
+
73
+ async def _load(self, entity_id):
74
+ return await self.service.repository.load(entity_id)
75
+
76
+ async def _views(self):
77
+ return await self.service.find_entities(Equal('entity_type_id', View.get_entity_type_id()))
78
+
79
+ async def _query(self, principal, where):
80
+ page = await self.runtime.graph_api.list_entities(
81
+ principal, EntityFilter(type_ids=VIEW_TYPES, where=where), limit=100,
82
+ )
83
+ return {e.entity_id for e in page.entities}
84
+
85
+
86
+ class PageTests(_ViewFixture):
87
+ @async_to_sync
88
+ async def test_a_first_change_creates_one_top_level_view_of_the_page(self):
89
+ await self._seed()
90
+ d = await self._entity(self.alice)
91
+ address = _address(d)
92
+
93
+ view = await self._load(await self._set(self.alice, {'collapsed': ['h1']}, address=address))
94
+
95
+ self.assertEqual((view.parent_id, view.owner, view.client), (None, self.a, 'web'))
96
+ self.assertEqual((view.slot, view.address, view.renders), (address, address, d))
97
+ self.assertEqual(view.position_key, position_key(self.a, 'web', address, d))
98
+ self.assertEqual(view.state, {'collapsed': ['h1']})
99
+ self.assertIsNone(view.component)
100
+ self.assertEqual(len(await self._views()), 1)
101
+
102
+ @async_to_sync
103
+ async def test_the_root_and_client_routes_render_nothing(self):
104
+ await self._seed()
105
+ for address in ('/', '/settings'):
106
+ view = await self._load(await self._set(self.alice, {'a': 1}, address=address))
107
+ self.assertIsNone(view.renders)
108
+ self.assertEqual(view.state, {'a': 1})
109
+
110
+ @async_to_sync
111
+ async def test_a_nested_change_creates_the_pages_view_first(self):
112
+ await self._seed()
113
+ p, d = await self._entity(self.alice), await self._entity(self.alice)
114
+ address = _address(p)
115
+
116
+ inner = await self._load(await self._set(self.alice, {'open': True}, address=address,
117
+ placement=[('outline', d), ('section', None)]))
118
+ middle = await self._load(inner.parent_id)
119
+ outer = await self._load(middle.parent_id)
120
+
121
+ self.assertEqual([outer.renders, middle.renders, inner.renders], [p, d, None])
122
+ self.assertEqual([outer.slot, middle.slot, inner.slot], [address, 'outline', 'section'])
123
+ self.assertEqual({outer.address, middle.address, inner.address}, {address})
124
+ self.assertIsNone(outer.parent_id)
125
+ self.assertEqual((outer.state, middle.state, inner.state), ({}, {}, {'open': True}))
126
+ self.assertEqual(len(await self._views()), 3)
127
+
128
+ @async_to_sync
129
+ async def test_repeating_creates_nothing_and_merges_state(self):
130
+ await self._seed()
131
+ address = _address(await self._entity(self.alice))
132
+ first = await self._set(self.alice, {'a': 1, 'b': 2}, address=address, placement=[('outline', None)])
133
+
134
+ second = await self._set(self.alice, {'b': 3, 'c': 4}, address=address, placement=[('outline', None)])
135
+
136
+ self.assertEqual(first, second)
137
+ self.assertEqual((await self._load(first)).state, {'a': 1, 'b': 3, 'c': 4})
138
+ self.assertEqual(len(await self._views()), 2)
139
+
140
+ @async_to_sync
141
+ async def test_siblings_differ_by_slot_and_renders(self):
142
+ await self._seed()
143
+ d = await self._entity(self.alice)
144
+ ids = {
145
+ await self._set(self.alice, {}, address='/', placement=[(slot, renders)])
146
+ for slot, renders in (('a', None), ('b', None), ('a', d))
147
+ }
148
+ self.assertEqual(len(ids), 3)
149
+
150
+ @async_to_sync
151
+ async def test_a_null_value_removes_the_key(self):
152
+ await self._seed()
153
+ view_id = await self._set(self.alice, {'a': 1, 'b': 2}, address='/')
154
+
155
+ await self._set(self.alice, {'a': None}, address='/')
156
+
157
+ self.assertEqual((await self._load(view_id)).state, {'b': 2})
158
+
159
+ @async_to_sync
160
+ async def test_component_is_set_when_given(self):
161
+ await self._seed()
162
+
163
+ view_id = await self._set(self.alice, {}, address='/', component='list_view')
164
+
165
+ self.assertEqual((await self._load(view_id)).component, 'list_view')
166
+
167
+ @async_to_sync
168
+ async def test_two_users_at_one_address_get_separate_views(self):
169
+ await self._seed()
170
+
171
+ mine = await self._set(self.alice, {'a': 1}, address='/')
172
+ theirs = await self._set(self.bob, {'a': 2}, address='/')
173
+
174
+ self.assertNotEqual(mine, theirs)
175
+ self.assertEqual((await self._load(theirs)).owner, self.bob.principal_entity_id)
176
+
177
+ @async_to_sync
178
+ async def test_clients_keep_separate_views(self):
179
+ await self._seed()
180
+ self.assertNotEqual(
181
+ await self._set(self.alice, {}, address='/', client='web'),
182
+ await self._set(self.alice, {}, address='/', client='mobile'),
183
+ )
184
+
185
+ @async_to_sync
186
+ async def test_bad_positions_are_refused(self):
187
+ await self._seed()
188
+ for params in ({'address': '/a/'}, {'address': '/a?x=1'}, {},
189
+ {'address': '/', 'view': 'gc://0000000001/aaaaaaaaaaaa'}):
190
+ with self.assertRaises(InvalidRequest, msg=params):
191
+ await self._set(self.alice, {'x': 1}, **params)
192
+ self.assertEqual(await self._views(), {})
193
+
194
+ @async_to_sync
195
+ async def test_a_concurrent_first_change_reuses_the_view_it_created(self):
196
+ await self._seed()
197
+ winner = await self._set(self.alice, {'a': 1}, address='/')
198
+ real_find = self.service.find_entities
199
+ calls = []
200
+
201
+ async def find_after_a_miss(predicate):
202
+ calls.append(predicate)
203
+ return {} if len(calls) == 1 else await real_find(predicate)
204
+
205
+ with patch.object(self.service, 'find_entities', side_effect=find_after_a_miss):
206
+ loser = await self._set(self.alice, {'b': 2}, address='/')
207
+
208
+ self.assertEqual(loser, winner)
209
+ self.assertEqual((await self._load(winner)).state, {'a': 1, 'b': 2})
210
+ self.assertEqual(len(await self._views()), 1)
211
+
212
+
213
+ class ViewStartTests(_ViewFixture):
214
+ async def _shared(self, role):
215
+ view = await self._explicit_view(self.alice)
216
+ await self.runtime.graph_api.grant(self.alice, view, self.bob.principal_entity_id, role)
217
+ return view
218
+
219
+ @async_to_sync
220
+ async def test_a_step_is_a_child_of_the_view_with_no_address(self):
221
+ await self._seed()
222
+ view = await self._explicit_view(self.alice)
223
+ d = await self._entity(self.alice)
224
+
225
+ child = await self._load(await self._set(self.alice, {'x': 1}, view=view, placement=[('kanban', d)]))
226
+
227
+ self.assertEqual((child.parent_id, child.slot, child.renders), (view, 'kanban', d))
228
+ self.assertIsNone(child.address)
229
+ self.assertEqual(child.position_key, position_key(view, 'web', 'kanban', d))
230
+
231
+ @async_to_sync
232
+ async def test_any_view_may_be_the_start(self):
233
+ await self._seed()
234
+ page = await self._set(self.alice, {}, address='/')
235
+
236
+ child = await self._load(await self._set(self.alice, {}, view=page, placement=[('a', None)]))
237
+
238
+ self.assertEqual((child.parent_id, child.address), (page, '/'))
239
+
240
+ @async_to_sync
241
+ async def test_an_editor_stores_state_inside_a_shared_view(self):
242
+ await self._seed()
243
+ view = await self._shared('editor')
244
+
245
+ child = await self._load(await self._set(self.bob, {'x': 1}, view=view, placement=[('kanban', None)]))
246
+
247
+ self.assertEqual(child.parent_id, view)
248
+
249
+ @async_to_sync
250
+ async def test_a_viewer_is_refused(self):
251
+ await self._seed()
252
+ view = await self._shared('viewer')
253
+
254
+ with self.assertRaises(Forbidden):
255
+ await self._set(self.bob, {'x': 1}, view=view, placement=[('kanban', None)])
256
+
257
+ @async_to_sync
258
+ async def test_a_non_view_start_is_refused(self):
259
+ await self._seed()
260
+ d = await self._entity(self.alice)
261
+
262
+ with self.assertRaises(InvalidRequest):
263
+ await self._set(self.alice, {'x': 1}, view=d, placement=[('kanban', None)])
264
+
265
+ @async_to_sync
266
+ async def test_a_view_start_needs_a_step(self):
267
+ await self._seed()
268
+ view = await self._explicit_view(self.alice)
269
+
270
+ with self.assertRaises(InvalidRequest):
271
+ await self._set(self.alice, {'x': 1}, view=view)
272
+
273
+
274
+ class LookupTests(_ViewFixture):
275
+ @async_to_sync
276
+ async def test_a_page_query_returns_the_pages_views_only(self):
277
+ await self._seed()
278
+ p = await self._entity(self.alice)
279
+ address = _address(p)
280
+ inner = await self._set(self.alice, {}, address=address, placement=[('outline', None), ('section', None)])
281
+ page = (await self._load((await self._load(inner)).parent_id)).parent_id
282
+ await self._set(self.alice, {}, address='/')
283
+ await self._set(self.bob, {}, address=address)
284
+
285
+ found = await self._query(self.alice, AllOf((
286
+ Equal('owner', self.a), Equal('client', 'web'), Equal('address', address),
287
+ )))
288
+
289
+ self.assertEqual(len(found), 3)
290
+ self.assertIn(inner, found)
291
+ self.assertIn(page, found)
292
+
293
+ @async_to_sync
294
+ async def test_a_view_query_returns_the_views_inside_it(self):
295
+ await self._seed()
296
+ view = await self._explicit_view(self.alice)
297
+ inner = await self._set(self.alice, {}, view=view, placement=[('a', None), ('b', None)])
298
+ await self._set(self.alice, {}, address='/')
299
+
300
+ found = await self._query(self.alice, AllOf((ContainsValue('ancestor_ids', view), Equal('client', 'web'))))
301
+
302
+ self.assertEqual(len(found), 2)
303
+ self.assertIn(inner, found)
304
+
305
+
306
+ class TargetedTests(_ViewFixture):
307
+ @async_to_sync
308
+ async def test_the_owner_merges_state(self):
309
+ await self._seed()
310
+ view_id = await self._set(self.alice, {'a': 1}, address='/')
311
+
312
+ await self._set(self.alice, {'b': 2}, target=view_id)
313
+
314
+ self.assertEqual((await self._load(view_id)).state, {'a': 1, 'b': 2})
315
+
316
+ @async_to_sync
317
+ async def test_a_non_owner_without_a_grant_is_refused(self):
318
+ await self._seed()
319
+ view_id = await self._set(self.alice, {'a': 1}, address='/')
320
+
321
+ with self.assertRaises(Forbidden):
322
+ await self._set(self.bob, {'b': 2}, target=view_id)
323
+
324
+ @async_to_sync
325
+ async def test_a_targeted_change_takes_no_position(self):
326
+ await self._seed()
327
+ view = await self._explicit_view(self.alice)
328
+
329
+ with self.assertRaises(InvalidRequest):
330
+ await self._set(self.alice, {}, target=view, client='web', address='/')
331
+ self.assertIsNone((await self._load(view)).address)
332
+
333
+
334
+ class CreateViewTests(_ViewFixture):
335
+ @async_to_sync
336
+ async def test_a_top_level_view(self):
337
+ await self._seed()
338
+ d = await self._entity(self.alice)
339
+
340
+ view = await self._load(await self._explicit_view(self.alice, renders=d))
341
+
342
+ self.assertEqual((view.parent_id, view.owner, view.renders), (None, self.a, d))
343
+ self.assertEqual((view.client, view.slot, view.address, view.position_key), (None, None, None, None))
344
+ self.assertEqual(view.component, 'composite_hbox')
345
+
346
+ @async_to_sync
347
+ async def test_two_explicit_views_of_one_entity_coexist(self):
348
+ await self._seed()
349
+ d = await self._entity(self.alice)
350
+ self.assertNotEqual(await self._explicit_view(self.alice, renders=d),
351
+ await self._explicit_view(self.alice, renders=d))
352
+
353
+ @async_to_sync
354
+ async def test_a_view_placed_in_a_data_entity(self):
355
+ await self._seed()
356
+ container = await self._entity(self.alice)
357
+
358
+ view = await self._load(await self._explicit_view(self.alice, parent=container))
359
+
360
+ self.assertEqual(view.parent_id, container)
361
+
362
+ @async_to_sync
363
+ async def test_a_view_in_an_entity_the_caller_cannot_create_in_is_refused(self):
364
+ await self._seed()
365
+ container = await self._entity(self.alice)
366
+
367
+ with self.assertRaises(Forbidden):
368
+ await self._explicit_view(self.bob, parent=container)
369
+
370
+ def test_it_takes_no_position(self):
371
+ self.assertEqual(
372
+ set(CreateView.get_request_schema()['properties']), {'renders', 'component', 'state'},
373
+ )
374
+
375
+
376
+ class PolicyTests(_ViewFixture):
377
+ @async_to_sync
378
+ async def test_generic_create_and_reparent_cannot_target_a_view(self):
379
+ await self._seed()
380
+ view_id = await self._explicit_view(self.alice)
381
+ other = await self._explicit_view(self.alice)
382
+
383
+ with self.assertRaises(Forbidden):
384
+ await self._run(
385
+ CreateEntity.request_constructor(view_id, entity_type_id='graphcore.Entity'), self.alice,
386
+ )
387
+ with self.assertRaises(Forbidden):
388
+ await self._run(ReparentChild.request_constructor(view_id, parent_id=other), self.alice)
389
+
390
+ @async_to_sync
391
+ async def test_nothing_is_created_under_a_user(self):
392
+ await self._seed()
393
+
394
+ with self.assertRaises(InvalidRequest):
395
+ await self._run(
396
+ CreateEntity.request_constructor(self.a, entity_type_id='graphcore.Entity'), self.alice,
397
+ )
398
+
399
+
400
+ class DeletionTests(_ViewFixture):
401
+ async def _delete(self, entity_id, principal):
402
+ await self._run(DeleteEntity.request_constructor(entity_id), principal)
403
+
404
+ @async_to_sync
405
+ async def test_deleting_the_shown_entity_deletes_its_views_and_their_children(self):
406
+ await self._seed()
407
+ d, x = await self._entity(self.alice), await self._entity(self.alice)
408
+ inner = await self._set(self.alice, {'a': 1}, address=_address(d), placement=[('outline', x)])
409
+ explicit = await self._explicit_view(self.alice, renders=d)
410
+ survivor = await self._set(self.alice, {'a': 1}, address=_address(x))
411
+
412
+ await self._delete(d, self.alice)
413
+
414
+ self.assertEqual(set(await self._views()), {survivor})
415
+ self.assertIsNone(await self._load(inner))
416
+ self.assertIsNone(await self._load(explicit))
417
+ self.assertIsNotNone(await self._load(x))
418
+
419
+ @async_to_sync
420
+ async def test_deleting_a_pages_view_deletes_its_nested_views(self):
421
+ await self._seed()
422
+ inner = await self._load(await self._set(self.alice, {'a': 1}, address='/', placement=[('outline', None)]))
423
+
424
+ await self._delete(inner.parent_id, self.alice)
425
+
426
+ self.assertEqual(await self._views(), {})
427
+
428
+ @async_to_sync
429
+ async def test_another_users_view_of_a_deleted_entity_is_deleted(self):
430
+ await self._seed()
431
+ d = await self._entity(self.alice)
432
+ bobs = await self._set(self.bob, {'a': 1}, address='/', placement=[('outline', d)])
433
+
434
+ await self._delete(d, self.alice)
435
+
436
+ self.assertIsNone(await self._load(bobs))
graphcore/errors.py ADDED
@@ -0,0 +1,145 @@
1
+ """Shared kernel errors.
2
+
3
+ Used by GraphAPI, EntityService, and the executor. Transports map these to
4
+ wire status codes; they never import this module to invent new types.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import logging
9
+ import uuid
10
+ from typing import Any
11
+
12
+
13
+ class KernelError(Exception):
14
+ """Expected kernel failure. Unexpected bugs use log_and_correlate instead."""
15
+
16
+ code = 'error'
17
+
18
+ def __init__(self, message: str, **details: Any):
19
+ self.message = message
20
+ self.details = details
21
+ super().__init__(message)
22
+
23
+ def __str__(self) -> str:
24
+ return self.message
25
+
26
+ def to_dict(self) -> dict:
27
+ out = {'code': self.code, 'error': self.message}
28
+ out.update(self.details)
29
+ return out
30
+
31
+ @staticmethod
32
+ def from_dict(data: dict) -> 'KernelError':
33
+ """The error a `to_dict()` form describes, as the class that defines
34
+ its `code`; `StrategyFailed` for a code no class defines."""
35
+ details = {k: v for k, v in data.items() if k not in ('code', 'error')}
36
+ return _class_for(data.get('code'))(data.get('error') or 'strategy failed', **details)
37
+
38
+ @staticmethod
39
+ def of(exc: BaseException) -> 'KernelError':
40
+ """`exc` itself, or an unexpected exception as `StrategyFailed`."""
41
+ if isinstance(exc, KernelError):
42
+ return exc
43
+ return StrategyFailed(str(exc) or type(exc).__name__)
44
+
45
+
46
+ def _class_for(code) -> type:
47
+ pending = [KernelError]
48
+ while pending:
49
+ cls = pending.pop()
50
+ if vars(cls).get('code') == code:
51
+ return cls
52
+ pending.extend(cls.__subclasses__())
53
+ return StrategyFailed
54
+
55
+
56
+ class InvalidRequest(KernelError):
57
+ """Bad request shape, over-cap batch, schema violation, unknown strategy."""
58
+
59
+ code = 'invalid_request'
60
+
61
+
62
+ class Forbidden(KernelError):
63
+ """Caller lacks permission for a write."""
64
+
65
+ code = 'forbidden'
66
+
67
+
68
+ class NotFound(KernelError):
69
+ """Write target (or other required resource) does not exist."""
70
+
71
+ code = 'not_found'
72
+
73
+
74
+ class Conflict(KernelError):
75
+ """Optimistic-lock / concurrent modification conflict."""
76
+
77
+ code = 'conflict'
78
+
79
+
80
+ class PreconditionFailed(Conflict):
81
+ """A conditional write was refused: its precondition entity no longer
82
+ matches."""
83
+
84
+ code = 'precondition_failed'
85
+
86
+
87
+ class ConcurrentModificationError(Conflict):
88
+ """Version mismatch on save — another process wrote the entity first."""
89
+
90
+
91
+ class DuplicateEntity(Conflict):
92
+ """Create refused: another entity already holds a value the store keeps
93
+ unique, e.g. a User's ``(issuer, subject)``."""
94
+
95
+
96
+ class RemoteUnavailable(KernelError):
97
+ """A remote read could not be delivered. `cause` is the `TombstoneCause`
98
+ the gateway reported, so a transport can say which."""
99
+
100
+ code = 'remote_unavailable'
101
+
102
+ def __init__(self, message: str, cause: str, **details: Any):
103
+ super().__init__(message, cause=cause, **details)
104
+ self.cause = cause
105
+
106
+
107
+ class StrategyFailed(KernelError):
108
+ """A run ended `failed`; the message is its request's `error`."""
109
+
110
+ code = 'strategy_failed'
111
+
112
+
113
+ class StaleAttempt(KernelError):
114
+ """A write or result from an attempt the kernel no longer runs: the
115
+ request is not `running`, or holds a later attempt."""
116
+
117
+ code = 'stale_attempt'
118
+
119
+
120
+ class InvalidQuery(KernelError):
121
+ """A query is malformed: a literal of a type no backend can carry, a
122
+ non-positive page limit."""
123
+
124
+ code = 'invalid_query'
125
+
126
+
127
+ class UnsupportedQuery(KernelError):
128
+ """A query is well-formed but names something this backend refuses: an
129
+ unknown predicate node, or a field outside the filterable allowlist.
130
+ Raised before execution — nothing is ever silently dropped."""
131
+
132
+ code = 'unsupported_query'
133
+
134
+
135
+ class ConfigurationError(Exception):
136
+ """The deployment's composition is invalid. Raised while a runtime is
137
+ built, never for a request, so it is not a `KernelError`."""
138
+
139
+
140
+ def log_and_correlate(logger: logging.Logger, message: str) -> str:
141
+ """Log an unexpected error with its traceback and return a correlation id."""
142
+ correlation_id = str(uuid.uuid4())
143
+ logger.error(f"[{correlation_id}] {message}", exc_info=True)
144
+ return correlation_id
145
+
@@ -0,0 +1,97 @@
1
+ # Strategy execution
2
+
3
+ The design is `docs/STRATEGY_EXECUTION_TDD.md`; this is the map of the code.
4
+
5
+ | Module | Role |
6
+ |---|---|
7
+ | `StrategyExecutorService.py` | Front door, attempts, retries, reporting, run reconciliation |
8
+ | `requests.py` | `new_strategy_request`, the one way a `StrategyRequest` is built |
9
+ | `dispatcher.py` | The `Dispatcher` port: hands one attempt to an environment |
10
+ | `environments/in_process.py` | `InProcessDispatcher`: the attempt runs on this process's event loop |
11
+ | `environments/celery.py` | `CeleryDispatcher` and the `graphcore.tasks.run_attempt` task |
12
+ | `local_session.py` | `LocalStrategySession`: the strategy's view of the graph |
13
+ | `strategy_context.py` | The strategy and kernel-internal contexts the chokepoint trusts |
14
+ | `tasks.py` | `graphcore.tasks.reconcile_runs` |
15
+
16
+ The runner (`domain/strategy/runner.py`) and the session port
17
+ (`domain/strategy/session.py`) are domain code: they import nothing from here.
18
+
19
+ ## A run
20
+
21
+ 1. **Front door.** `execute_request(request, principal, wait, callback_channel)`
22
+ refuses an id that already exists, an unregistered strategy, a missing
23
+ target, and a principal the policy refuses (`_can_execute`). A refused
24
+ request is never saved. Otherwise it is saved `pending` with a dispatch
25
+ deadline, owned by the principal. `submit(payload, ...)` builds the request first.
26
+ 2. **Attempt.** `start_attempt` re-authorizes the owner against current
27
+ state, saves `pending` with `attempt + 1` and a dispatch deadline
28
+ (`GRAPHCORE_EXECUTION_DISPATCH_TIMEOUT`), and dispatches by
29
+ `strategy.workload`. A refusal or a dispatch error fails the run. In a
30
+ Celery worker, CPU dispatches to the worker's own loop.
31
+ 3. **Run.** The entry point claims the attempt (`claim_attempt`), saving
32
+ `running` with `attempt_deadline` (`strategy.timeout` or
33
+ `GRAPHCORE_EXECUTION_DEFAULT_TIMEOUT`) counted from now; a delivery that
34
+ loses the claim runs nothing. It then builds a `LocalStrategySession` and calls
35
+ `StrategyRunner.run`. The runner checks the schema and
36
+ `verify_executable()`, runs `apply()`, saves the returned target if it
37
+ changed, always runs `cleanup()`, and returns an `AttemptResult`.
38
+ 4. **Result.** `finish_attempt` ignores a result for an attempt the request
39
+ no longer holds. `succeeded` saves `ret_val`; `conflict` backs off
40
+ (`0.1 * 2**attempt` seconds plus jitter) and starts the next attempt, up
41
+ to `GRAPHCORE_EXECUTION_MAX_ATTEMPTS`; anything else saves `failed` with
42
+ the error's message, `code`, and details.
43
+
44
+ With `wait=True` the front door returns once the request has succeeded,
45
+ with the target reloaded as the caller, or raises the error the run failed
46
+ with (`request.failure()`), whoever the caller is. The waiter subscribes
47
+ to the request through the `RunEvents` port and re-reads it whenever the
48
+ process that finished it publishes; at the attempt deadline it reconciles
49
+ the run itself. `RedisRunEvents` carries the event across processes.
50
+
51
+ ## Sessions and fencing
52
+
53
+ A session reads and writes as the request's owner. Each write carries a
54
+ repository `Precondition` that the request is still `running` at this
55
+ attempt, checked in the write's own transaction, and raises `StaleAttempt`
56
+ otherwise. It runs inside `strategy_context(strategy_cls)`, so the
57
+ persistence chokepoint authorizes it against the strategy's policy. Nothing
58
+ else runs in a strategy context, and `kernel_internal_context()` refuses to
59
+ open inside one. The executor's own request writes use
60
+ `kernel_internal_context()`.
61
+
62
+ ## Reporting
63
+
64
+ With a `callback_channel`, the terminal request is reported on it:
65
+
66
+ ```python
67
+ {"type": "strategy_complete", "request_id": ..., "target_entity_id": ...,
68
+ "client_request_id": ..., "status": "succeeded", "success": True, "ret_val": {...}}
69
+ # failed: "status": "failed", "success": False, "error": "<message>", "code": "<code>" instead of ret_val
70
+ ```
71
+
72
+ On success each entry in `callbacks` (a strategy payload) is submitted as
73
+ the owner with the finished request's `ret_val` added to its params as
74
+ `source_ret_val`.
75
+
76
+ ## Run reconciliation
77
+
78
+ `reconcile_runs` finds `pending` and `running` requests past their
79
+ deadline. A `pending` one was never claimed, or never started: its attempt
80
+ is sent again under the same number. A `running` one is started again, or
81
+ failed with `attempt timed out` once out of attempts. The superseded
82
+ attempt's later writes are fenced out and its result discarded.
83
+ The Celery task runs when a worker starts and every
84
+ `GRAPHCORE_RECONCILE_INTERVAL` seconds (`BeatScheduler` keeps the periodic
85
+ task).
86
+
87
+ ## Debugging
88
+
89
+ - A request stuck in `running` past its deadline means nothing is running
90
+ reconciliation: check the worker and Beat.
91
+ - A request that stays `pending` while its attempt is sent again and again
92
+ means no environment is consuming that workload's queue.
93
+ - `Discarding stale result for attempt ...` is a superseded attempt finishing
94
+ late, which is expected after a reconcile.
95
+ - A target saved on every run despite no change usually means the strategy
96
+ replaced it with an entity the session never returned.
97
+ - Cleanup errors are logged and swallowed so they never mask the run's result.