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,219 @@
1
+ """`View` state and explicit views. See docs/VIEW_ENTITY_TDD.md."""
2
+ from typing import Optional
3
+
4
+ from graphcore.domain.entity.Entity import Entity
5
+ from graphcore.domain.entity.view.View import View
6
+ from graphcore.domain.entity.view.position import Position, position_key
7
+ from graphcore.domain.strategy.BaseStrategy import Strategy
8
+ from graphcore.errors import DuplicateEntity, InvalidRequest, NotFound
9
+ from graphcore.registry.decorators import register_strategy
10
+ from graphcore.types.entity_id import is_entity_id
11
+ from graphcore.types.entities import EntityFilter
12
+ from graphcore.types.query import Equal
13
+
14
+
15
+ def _state_param(config: dict) -> dict:
16
+ state = config.get('state') or {}
17
+ if not isinstance(state, dict):
18
+ raise InvalidRequest("state must be an object")
19
+ return state
20
+
21
+
22
+ def _component_param(config: dict) -> Optional[str]:
23
+ component = config.get('component')
24
+ if component is not None and not isinstance(component, str):
25
+ raise InvalidRequest("component must be a string or null")
26
+ return component
27
+
28
+
29
+ def _apply_change(view: View, config: dict) -> None:
30
+ state = dict(view.state)
31
+ for key, value in _state_param(config).items():
32
+ if value is None:
33
+ state.pop(key, None)
34
+ else:
35
+ state[key] = value
36
+ view.state = state
37
+ if 'component' in config:
38
+ view.component = _component_param(config)
39
+
40
+
41
+ class _Positions:
42
+ """Finds or creates the view at a position, with any views above it
43
+ that do not exist yet."""
44
+
45
+ async def _ensure_position(self, position: Position, change: dict) -> View:
46
+ if position.address is not None:
47
+ view = await self._ensure(
48
+ self.principal_entity_id, None, position.client, position.address,
49
+ self._address_entity(position.address), position.address,
50
+ change if not position.steps else None,
51
+ )
52
+ else:
53
+ view = await self.get_entity(position.view)
54
+ if not isinstance(view, View):
55
+ raise InvalidRequest(f"{position.view} is not a View")
56
+ for i, step in enumerate(position.steps):
57
+ last = i == len(position.steps) - 1
58
+ view = await self._ensure(
59
+ view.entity_id, view.entity_id, position.client, step.slot, step.renders,
60
+ view.address, change if last else None,
61
+ )
62
+ return view
63
+
64
+ def _address_entity(self, address: str) -> Optional[str]:
65
+ """The route's entity; none for a client route outside the table."""
66
+ try:
67
+ return self.registry.routes.resolve(address).entity_id
68
+ except NotFound:
69
+ return None
70
+
71
+ async def _find(self, key: str) -> Optional[View]:
72
+ page = await self.session.list(EntityFilter(
73
+ type_ids=frozenset({View.get_entity_type_id()}), where=Equal('position_key', key),
74
+ ), limit=1)
75
+ return next(iter(page.entities), None)
76
+
77
+ async def _ensure(self, scope_id: str, parent_id: Optional[str], client: str, slot: str,
78
+ renders: Optional[str], address: Optional[str],
79
+ change: Optional[dict]) -> View:
80
+ key = position_key(scope_id, client, slot, renders)
81
+ view = await self._find(key)
82
+ if view is None:
83
+ view = self.session.create(View)
84
+ view.client, view.slot, view.renders, view.address = client, slot, renders, address
85
+ view.position_key = key
86
+ if parent_id:
87
+ view.set_parent(parent_id)
88
+ if change is not None:
89
+ _apply_change(view, change)
90
+ try:
91
+ return await self.save_entity(view)
92
+ except DuplicateEntity:
93
+ view = await self._find(key)
94
+ if view is None:
95
+ raise
96
+ if change is not None:
97
+ _apply_change(view, change)
98
+ view = await self.save_entity(view)
99
+ return view
100
+
101
+
102
+ POSITION_PARAMS = ('client', 'address', 'view', 'placement')
103
+
104
+
105
+ @register_strategy(View)
106
+ class SetViewState(_Positions, Strategy):
107
+ """Merge `state` into a view. Targeted, the target is the view.
108
+ Untargeted, `client`, `address` or `view`, and `placement` name where
109
+ it sits; it is created, with any views above it that do not exist yet,
110
+ on its first change."""
111
+ strategy_description = "Merges state into a view, creating it and the views above it when they do not exist"
112
+
113
+ async def apply(self) -> tuple:
114
+ config = self.strategy_request.param_config
115
+ if self.entity is not None:
116
+ if not isinstance(self.entity, View):
117
+ raise InvalidRequest(f"{self.entity.entity_id} is not a View")
118
+ if any(config.get(name) is not None for name in POSITION_PARAMS):
119
+ raise InvalidRequest("a targeted change takes no position")
120
+ _apply_change(self.entity, config)
121
+ self.entity = await self.save_entity(self.entity)
122
+ view = self.entity
123
+ else:
124
+ view = await self._ensure_position(Position.from_params(config), config)
125
+
126
+ self.strategy_request.ret_val['view_entity_id'] = view.entity_id
127
+ return self.strategy_request, self.entity
128
+
129
+ @staticmethod
130
+ def get_request_schema():
131
+ return {
132
+ "type": "object",
133
+ "properties": {
134
+ "client": {"type": ["string", "null"], "description": "Without a target, the client storing the view"},
135
+ "address": {"type": ["string", "null"], "description": "Without a target, start at this page's view"},
136
+ "view": {"type": ["string", "null"], "description": "Without a target, start at this view"},
137
+ "placement": {
138
+ "type": ["array", "null"],
139
+ "description": "Steps below the start, each a slot and the entity shown there",
140
+ "items": {
141
+ "type": "object",
142
+ "properties": {"slot": {"type": "string"}, "renders": {"type": ["string", "null"]}},
143
+ "required": ["slot"],
144
+ },
145
+ },
146
+ "state": {"type": "object", "description": "Shallow-merged into the view's state; a null value removes the key"},
147
+ "component": {"type": ["string", "null"], "description": "Replaces the view's component when given"},
148
+ },
149
+ "required": ["state"],
150
+ }
151
+
152
+ @classmethod
153
+ def request_constructor(
154
+ cls,
155
+ target_entity_id: Optional[str],
156
+ state: dict,
157
+ client: Optional[str] = None,
158
+ address: Optional[str] = None,
159
+ view: Optional[str] = None,
160
+ placement: Optional[list] = None,
161
+ component: Optional[str] = None,
162
+ ):
163
+ param_config = {"state": state}
164
+ for name, value in (('client', client), ('address', address), ('view', view),
165
+ ('placement', placement), ('component', component)):
166
+ if value is not None:
167
+ param_config[name] = value
168
+ return cls._build_request(
169
+ target_entity_id=target_entity_id, param_config=param_config,
170
+ )
171
+
172
+
173
+ @register_strategy(Entity)
174
+ class CreateView(Strategy):
175
+ """Create an explicit view in the target, or at the top level, owned by
176
+ the caller, without one."""
177
+ strategy_description = "Creates a view in the target, or at the top level"
178
+
179
+ async def apply(self) -> tuple:
180
+ config = self.strategy_request.param_config
181
+ renders = config.get('renders')
182
+ if renders is not None and not is_entity_id(renders):
183
+ raise InvalidRequest(f"renders must be an entity identifier, got {renders!r}")
184
+
185
+ view = self.session.create(View)
186
+ view.renders = renders
187
+ view.component = _component_param(config)
188
+ view.state = dict(_state_param(config))
189
+ if self.entity is not None:
190
+ view.set_parent(self.entity.entity_id)
191
+ view = await self.save_entity(view)
192
+
193
+ self.strategy_request.ret_val['view_entity_id'] = view.entity_id
194
+ return self.strategy_request, self.entity
195
+
196
+ @staticmethod
197
+ def get_request_schema():
198
+ return {
199
+ "type": "object",
200
+ "properties": {
201
+ "renders": {"type": ["string", "null"], "description": "The entity the view shows"},
202
+ "component": {"type": ["string", "null"], "description": "The view name, or null for the type's default"},
203
+ "state": {"type": ["object", "null"], "description": "Initial view state"},
204
+ },
205
+ "required": [],
206
+ }
207
+
208
+ @classmethod
209
+ def request_constructor(
210
+ cls,
211
+ target_entity_id: Optional[str],
212
+ renders: Optional[str] = None,
213
+ component: Optional[str] = None,
214
+ state: Optional[dict] = None,
215
+ ):
216
+ return cls._build_request(
217
+ target_entity_id=target_entity_id,
218
+ param_config={"renders": renders, "component": component, "state": state or {}},
219
+ )
File without changes
@@ -0,0 +1,84 @@
1
+ """One attempt, run through a session (docs/STRATEGY_EXECUTION_TDD.md, The
2
+ runner). Domain code: no kernel service is imported here."""
3
+ from __future__ import annotations
4
+
5
+ import copy
6
+ import logging
7
+ from typing import Optional
8
+
9
+ import jsonschema
10
+
11
+ from graphcore.domain.entity.Entity import Entity
12
+ from graphcore.domain.strategy.session import StrategySession
13
+ from graphcore.errors import ConcurrentModificationError, InvalidRequest, KernelError
14
+ from graphcore.types.execution import AttemptResult, ExecutionAttempt
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+
19
+ class ChangeTracker:
20
+ """The last persisted state of every entity an attempt's session
21
+ returned or saved."""
22
+
23
+ def __init__(self) -> None:
24
+ self._persisted: dict[str, dict] = {}
25
+
26
+ def record(self, entity: Optional[Entity]) -> None:
27
+ if entity is not None:
28
+ self._persisted[entity.entity_id] = copy.deepcopy(entity.to_dict())
29
+
30
+ def is_unsaved(self, entity: Entity) -> bool:
31
+ return entity.to_dict() != self._persisted.get(entity.entity_id)
32
+
33
+
34
+ class StrategyRunner:
35
+ """Performs one attempt. Writes only through the session, performs no
36
+ authorization and no retry, and never raises: every outcome is an
37
+ `AttemptResult`."""
38
+
39
+ def __init__(self, registry) -> None:
40
+ self.registry = registry
41
+
42
+ async def run(self, attempt: ExecutionAttempt, session: StrategySession,
43
+ tracker: ChangeTracker) -> AttemptResult:
44
+ def result(outcome, ret_val=None, error=None):
45
+ return AttemptResult(request_id=attempt.request_id, attempt=attempt.attempt, outcome=outcome,
46
+ ret_val=ret_val, error=KernelError.of(error).to_dict() if error else None)
47
+
48
+ strategy = None
49
+ try:
50
+ strategy_cls = self.registry.strategies.require(attempt.strategy_name)
51
+ request = await session.get(attempt.request_id)
52
+ target = await session.get(attempt.target_entity_id) if attempt.target_entity_id else None
53
+ strategy = strategy_cls(request, target, session, self.registry)
54
+ _verify(strategy)
55
+ _, target = await strategy.apply()
56
+ if target is not None and not strategy_cls.read_only_strategy and tracker.is_unsaved(target):
57
+ await session.save(target)
58
+ return result('succeeded', ret_val=dict(request.ret_val or {}))
59
+ except ConcurrentModificationError as exc:
60
+ return result('conflict', error=exc)
61
+ except Exception as exc:
62
+ if not isinstance(exc, KernelError):
63
+ logger.exception("Attempt %s of %s failed", attempt.attempt, attempt.request_id)
64
+ return result('failed', error=exc)
65
+ finally:
66
+ if strategy is not None:
67
+ try:
68
+ await strategy.cleanup()
69
+ except Exception:
70
+ logger.exception("Cleanup failed for %s", attempt.strategy_name)
71
+
72
+
73
+ def _verify(strategy) -> None:
74
+ """The boundary checks: `param_config` against the schema, then
75
+ `verify_executable()` for what a schema cannot express."""
76
+ name = type(strategy).__name__
77
+ try:
78
+ jsonschema.validate(instance=strategy.strategy_request.param_config,
79
+ schema=strategy.get_request_schema())
80
+ except jsonschema.ValidationError as e:
81
+ where = '.'.join(str(part) for part in e.absolute_path)
82
+ raise InvalidRequest(f"Invalid param_config for {name}: {where + ': ' if where else ''}{e.message}") from e
83
+ if strategy.verify_executable() is False:
84
+ raise InvalidRequest(f"Strategy {name} is not executable for this request")
@@ -0,0 +1,56 @@
1
+ """The strategy's view of the graph (docs/STRATEGY_EXECUTION_TDD.md, The
2
+ strategy session). Owned by the domain; adapters implement it over a
3
+ runtime's services or over the execution transport."""
4
+ from __future__ import annotations
5
+
6
+ from abc import ABC, abstractmethod
7
+ from typing import Optional, TypeVar
8
+
9
+ from graphcore.domain.entity.Entity import Entity
10
+ from graphcore.domain.entity.strategy_request.StrategyRequest import StrategyRequest
11
+ from graphcore.types.entities import EntityFilter, EntityPage
12
+ from graphcore.types.identity import Principal
13
+ from graphcore.types.strategy import StrategyRequestPayload
14
+
15
+ E = TypeVar('E', bound=Entity)
16
+
17
+
18
+ class StrategySession(ABC):
19
+ """Bound to one attempt, acting as its request's owner. Every write is
20
+ fenced: it is refused with `StaleAttempt` unless the request is
21
+ `running` with this attempt's number."""
22
+
23
+ principal: Principal
24
+
25
+ @abstractmethod
26
+ def create(self, entity_cls: type[E], **fields) -> E:
27
+ """A new entity with an identifier minted for this deployment, the
28
+ same on every attempt of the request for the same call order. Not
29
+ persisted."""
30
+
31
+ @abstractmethod
32
+ async def get(self, entity_id: str) -> Entity:
33
+ """Raises `NotFound` or `Forbidden`."""
34
+
35
+ @abstractmethod
36
+ async def get_many(self, entity_ids: list[str]) -> dict[str, Entity]:
37
+ """Only the entities the principal may read."""
38
+
39
+ @abstractmethod
40
+ async def list(self, entity_filter: EntityFilter, cursor: Optional[str] = None,
41
+ limit: int = 25) -> EntityPage[Entity]:
42
+ """Principal-scoped, with `EntityService.list_entities` semantics."""
43
+
44
+ @abstractmethod
45
+ async def save(self, entity: E) -> E:
46
+ """Commits on return; raises `ConcurrentModificationError` on a stale version."""
47
+
48
+ @abstractmethod
49
+ async def delete_tree(self, root_id: str) -> list[str]:
50
+ """Deletes through the deletion chokepoint; returns the deleted ids."""
51
+
52
+ @abstractmethod
53
+ async def execute(self, payload: StrategyRequestPayload, *, wait: bool = True,
54
+ callback_channel: Optional[str] = None,
55
+ ) -> tuple[StrategyRequest, Optional[Entity]]:
56
+ """Submit a nested request as this session's principal."""
@@ -0,0 +1,167 @@
1
+ """
2
+ Base test class for testing strategies.
3
+
4
+ Provides common setup, teardown, and helper methods for strategy testing.
5
+ """
6
+ from asgiref.sync import async_to_sync
7
+ from django.test import TransactionTestCase
8
+ from graphcore.domain.entity.Entity import Entity
9
+ from graphcore.execution.strategy_context import kernel_internal_context
10
+ from graphcore.types.identity import Principal
11
+ from graphcore.types.strategy import StrategyRequestPayload
12
+ from graphcore.registry.registry import DEFAULT_REGISTRY
13
+ from graphcore.runtime import django_runtime
14
+ from graphcore.tests.runs import run
15
+ import logging
16
+
17
+ logger = logging.getLogger(__name__)
18
+
19
+
20
+ class BaseStrategyTest(TransactionTestCase):
21
+ """
22
+ Base test class for strategy tests.
23
+
24
+ Provides common functionality for testing async strategies.
25
+ Uses TransactionTestCase to support async test methods.
26
+ """
27
+ TEST_USER_ENTITY_ID = "test-user-entity-id"
28
+ def setUp(self):
29
+ """Set up test fixtures"""
30
+ self.registry = DEFAULT_REGISTRY.fork_open()
31
+ for entity_class, strategy_class in getattr(type(self), '_test_strategy_mappings', ()):
32
+ self.registry.strategies.register_strategy(entity_class.__name__, strategy_class)
33
+ rt = django_runtime(self.registry)
34
+ self.entity_service = rt.entity_service
35
+ self.executor_service = rt.executor
36
+ self.entities_created = []
37
+ self.test_principal = Principal(principal_entity_id=self.TEST_USER_ENTITY_ID)
38
+
39
+ def tearDown(self):
40
+ """Clean up test fixtures."""
41
+ async_to_sync(self._async_tear_down)()
42
+ super().tearDown()
43
+
44
+ async def _async_tear_down(self):
45
+ """Run async cleanup for entities created during the test."""
46
+ # Clean up all entities created during the test
47
+ for entity_id in self.entities_created:
48
+ try:
49
+ await self.entity_service.delete_entities(
50
+ [entity_id],
51
+ principal=self.test_principal,
52
+ )
53
+ except Exception as e:
54
+ logger.warning(f"Failed to clean up entity {entity_id}: {e}")
55
+
56
+ async def create_test_entity(self, entity_class=Entity, **attributes):
57
+ """
58
+ Create a test entity with given attributes.
59
+
60
+ Args:
61
+ entity_class: The entity class to instantiate (default: Entity)
62
+ **attributes: Attributes to set on the entity
63
+
64
+ Returns:
65
+ Created entity
66
+ """
67
+ entity = self.entity_service.create(entity_class)
68
+ for key, value in attributes.items():
69
+ entity.set_attribute(key, value)
70
+
71
+ # Grant write permission to test user
72
+
73
+ # Fixture setup is a documented no-strategy write - see
74
+ # docs/AUTHORIZATION_TECHNICAL_TDD.md (Enforcement).
75
+ with kernel_internal_context():
76
+ entity = await self.entity_service.save_entity(entity, self.test_principal)
77
+ self.entities_created.append(entity.entity_id)
78
+
79
+ return entity
80
+
81
+ async def save_test_entity(self, entity, principal=None):
82
+ """Persist a fixture entity outside of any strategy - see
83
+ docs/AUTHORIZATION_TECHNICAL_TDD.md (Enforcement)."""
84
+ with kernel_internal_context():
85
+ return await self.entity_service.save_entity(
86
+ entity, principal or self.test_principal
87
+ )
88
+
89
+ def create_strategy_request(self, strategy_name, target_entity_id, param_config=None):
90
+ """
91
+ Create a strategy request for testing.
92
+
93
+ Args:
94
+ strategy_name: Name of the strategy class
95
+ target_entity_id: ID of the target entity
96
+ param_config: Parameters for the strategy (default: {})
97
+
98
+ Returns:
99
+ StrategyRequestPayload
100
+ """
101
+ return StrategyRequestPayload(
102
+ strategy_name=strategy_name,
103
+ target_entity_id=target_entity_id,
104
+ param_config=param_config or {},
105
+ )
106
+
107
+ async def execute_strategy(self, payload):
108
+ """Run the payload through the front door as the test principal and
109
+ return its succeeded request; a failed run raises `StrategyFailed`."""
110
+ request, _ = await run(self.executor_service, payload, self.test_principal)
111
+ return request
112
+
113
+ def assert_strategy_success(self, strategy_request, entity=None):
114
+ """
115
+ Assert that a strategy executed successfully.
116
+
117
+ Args:
118
+ strategy_request: The executed strategy request
119
+ entity: Optional entity to check for modifications
120
+ """
121
+ # Reaching here without an exception already means apply() succeeded;
122
+ # there is no separate is_applied flag to check (Step B4 removed it,
123
+ # since it was never persisted and always read False after a reload).
124
+ self.assertIsNotNone(strategy_request, "Strategy request should not be None")
125
+
126
+ def assert_strategy_failure(self, strategy_request, expected_error=None):
127
+ """
128
+ Assert that a strategy failed with an expected error.
129
+
130
+ Args:
131
+ strategy_request: The strategy request
132
+ expected_error: Optional expected error message substring
133
+ """
134
+ self.assertIsNotNone(strategy_request, "Strategy request should not be None")
135
+
136
+ if expected_error:
137
+ self.assertIn(expected_error, str(strategy_request.ret_val.get('error', '')),
138
+ f"Expected error message containing '{expected_error}'")
139
+
140
+ def assert_entity_has_attribute(self, entity, attribute_name, expected_value=None):
141
+ """
142
+ Assert that an entity has a specific attribute.
143
+
144
+ Args:
145
+ entity: The entity to check
146
+ attribute_name: Name of the attribute
147
+ expected_value: Optional expected value
148
+ """
149
+ self.assertTrue(entity.has_attribute(attribute_name),
150
+ f"Entity should have attribute '{attribute_name}'")
151
+
152
+ if expected_value is not None:
153
+ actual_value = entity.get_attribute(attribute_name)
154
+ self.assertEqual(actual_value, expected_value,
155
+ f"Attribute '{attribute_name}' should have value {expected_value}, got {actual_value}")
156
+
157
+ def assert_entity_has_child(self, parent_entity, child_entity_id):
158
+ """
159
+ Assert that an entity has a specific child.
160
+
161
+ Args:
162
+ parent_entity: The parent entity
163
+ child_entity_id: ID of the expected child
164
+ """
165
+ self.assertIn(child_entity_id, parent_entity.children_ids,
166
+ f"Entity {parent_entity.entity_id} should have child {child_entity_id}")
167
+
File without changes