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.
- graphcore/__init__.py +5 -0
- graphcore/agent_frontend/__init__.py +8 -0
- graphcore/agent_frontend/api.py +69 -0
- graphcore/agent_frontend/base/__init__.py +11 -0
- graphcore/agent_frontend/base/views.py +167 -0
- graphcore/agent_frontend/client.py +31 -0
- graphcore/agent_frontend/context.py +157 -0
- graphcore/agent_frontend/errors.py +12 -0
- graphcore/agent_frontend/loading.py +54 -0
- graphcore/agent_frontend/registry.py +42 -0
- graphcore/agent_frontend/render.py +207 -0
- graphcore/agent_frontend/server.py +103 -0
- graphcore/agent_frontend/signatures.py +35 -0
- graphcore/agent_frontend/testing.py +45 -0
- graphcore/agent_frontend/tests/__init__.py +0 -0
- graphcore/agent_frontend/tests/fakes.py +114 -0
- graphcore/agent_frontend/tests/test_frontend.py +447 -0
- graphcore/agent_frontend/tests/test_loading.py +83 -0
- graphcore/agent_frontend/tests/test_server.py +109 -0
- graphcore/agent_frontend/tools.py +137 -0
- graphcore/api/__init__.py +4 -0
- graphcore/api/graph_api.py +378 -0
- graphcore/api/predicate_codec.py +81 -0
- graphcore/api/tests/__init__.py +0 -0
- graphcore/api/tests/test_changed_entities.py +109 -0
- graphcore/api/tests/test_graph_api.py +290 -0
- graphcore/api/tests/test_graph_api_access.py +94 -0
- graphcore/api/tests/test_graph_api_grants.py +90 -0
- graphcore/api/tests/test_predicate_codec.py +83 -0
- graphcore/api/tests/test_subtree_query.py +79 -0
- graphcore/apps.py +71 -0
- graphcore/auth/__init__.py +1 -0
- graphcore/auth/authn/README.md +178 -0
- graphcore/auth/authn/__init__.py +3 -0
- graphcore/auth/authn/authenticator.py +120 -0
- graphcore/auth/authn/onboarding.py +105 -0
- graphcore/auth/authn/principals.py +20 -0
- graphcore/auth/authn/resource_metadata.py +43 -0
- graphcore/auth/authn/service_accounts.py +96 -0
- graphcore/auth/authn/testing.py +96 -0
- graphcore/auth/authn/tests/__init__.py +0 -0
- graphcore/auth/authn/tests/test_authenticator.py +145 -0
- graphcore/auth/authn/tests/test_onboarding.py +145 -0
- graphcore/auth/authn/tests/test_service_accounts.py +180 -0
- graphcore/auth/authn/users.py +59 -0
- graphcore/auth/authz/__init__.py +0 -0
- graphcore/auth/authz/access_port.py +120 -0
- graphcore/auth/authz/adapter/__init__.py +0 -0
- graphcore/auth/authz/adapter/default_access_adapter.py +556 -0
- graphcore/auth/authz/adapter/tests/__init__.py +0 -0
- graphcore/auth/authz/adapter/tests/test_default_adapter.py +543 -0
- graphcore/auth/authz/adapter/tests/test_entity_write_hooks.py +207 -0
- graphcore/auth/authz/adapter/tests/test_group_types.py +70 -0
- graphcore/auth/authz/adapter/tests/test_sync_support.py +209 -0
- graphcore/auth/authz/defaults.py +26 -0
- graphcore/auth/authz/kernel_policy.py +191 -0
- graphcore/auth/authz/kernel_writes.py +31 -0
- graphcore/auth/authz/lookups.py +25 -0
- graphcore/auth/authz/policy.py +403 -0
- graphcore/auth/authz/tests/__init__.py +0 -0
- graphcore/auth/authz/tests/test_can_contain.py +150 -0
- graphcore/auth/authz/tests/test_contained_by.py +167 -0
- graphcore/auth/authz/tests/test_creation.py +391 -0
- graphcore/auth/authz/tests/test_delete_permission.py +82 -0
- graphcore/auth/authz/tests/test_granted_to_me.py +160 -0
- graphcore/auth/authz/tests/test_integration.py +637 -0
- graphcore/auth/authz/tests/test_invites.py +184 -0
- graphcore/auth/authz/tests/test_kernel_policy.py +140 -0
- graphcore/auth/authz/tests/test_lockout.py +132 -0
- graphcore/auth/authz/tests/test_policy.py +329 -0
- graphcore/auth/authz/tests/test_primitives.py +73 -0
- graphcore/auth/authz/tests/test_principal_creation.py +62 -0
- graphcore/bootstrap/__init__.py +0 -0
- graphcore/bootstrap/deployment.py +67 -0
- graphcore/bootstrap/signals.py +16 -0
- graphcore/bootstrap/tests/__init__.py +0 -0
- graphcore/bootstrap/tests/test_deployment_seed.py +154 -0
- graphcore/broadcast/__init__.py +4 -0
- graphcore/broadcast/broadcaster.py +201 -0
- graphcore/broadcast/channels.py +56 -0
- graphcore/broadcast/tests/__init__.py +0 -0
- graphcore/broadcast/tests/test_broadcaster.py +444 -0
- graphcore/celery.py +14 -0
- graphcore/docs/AGENT_FRONTEND_TDD.md +389 -0
- graphcore/docs/ARCHITECTURE_PRINCIPLES.md +152 -0
- graphcore/docs/AUTHORIZATION_TECHNICAL_TDD.md +1379 -0
- graphcore/docs/ENTITY_REFERENCE_TDD.md +561 -0
- graphcore/docs/GRAPHAPI_FRONT_DOOR.md +173 -0
- graphcore/docs/RUNTIME.md +90 -0
- graphcore/docs/SCHEDULING_TDD.md +244 -0
- graphcore/docs/STRATEGY_EXECUTION_TDD.md +1043 -0
- graphcore/docs/VIEW_ENTITY_TDD.md +522 -0
- graphcore/docs/VIEW_SYSTEM_TDD.md +307 -0
- graphcore/docs/__init__.py +0 -0
- graphcore/docs/doc_topics.py +97 -0
- graphcore/docs/docs_discovery.py +64 -0
- graphcore/docs/public_api.py +56 -0
- graphcore/domain/__init__.py +0 -0
- graphcore/domain/entity/Entity.py +204 -0
- graphcore/domain/entity/__init__.py +0 -0
- graphcore/domain/entity/deployment/Deployment.py +25 -0
- graphcore/domain/entity/deployment/__init__.py +0 -0
- graphcore/domain/entity/grant/Grant.py +31 -0
- graphcore/domain/entity/grant/__init__.py +0 -0
- graphcore/domain/entity/group/Group.py +48 -0
- graphcore/domain/entity/group/README.md +88 -0
- graphcore/domain/entity/group/__init__.py +0 -0
- graphcore/domain/entity/invite/Invite.py +31 -0
- graphcore/domain/entity/invite/__init__.py +0 -0
- graphcore/domain/entity/request_template/RequestTemplate.py +48 -0
- graphcore/domain/entity/request_template/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/__init__.py +0 -0
- graphcore/domain/entity/request_template/tests/test_request_template.py +52 -0
- graphcore/domain/entity/service_account/ServiceAccount.py +41 -0
- graphcore/domain/entity/service_account/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/StrategyRequest.py +61 -0
- graphcore/domain/entity/strategy_request/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/__init__.py +0 -0
- graphcore/domain/entity/strategy_request/tests/test_strategy_request_entity.py +35 -0
- graphcore/domain/entity/tests/__init__.py +0 -0
- graphcore/domain/entity/tests/test_entity.py +227 -0
- graphcore/domain/entity/tests/test_grant_group_entities.py +36 -0
- graphcore/domain/entity/tests/test_write_once_fields.py +98 -0
- graphcore/domain/entity/user/User.py +39 -0
- graphcore/domain/entity/user/__init__.py +0 -0
- graphcore/domain/entity/user/tests/__init__.py +0 -0
- graphcore/domain/entity/user/tests/test_user_entity.py +33 -0
- graphcore/domain/entity/view/View.py +37 -0
- graphcore/domain/entity/view/__init__.py +0 -0
- graphcore/domain/entity/view/position.py +78 -0
- graphcore/domain/entity/view/tests/__init__.py +0 -0
- graphcore/domain/entity/view/tests/test_position.py +71 -0
- graphcore/domain/entity/view/tests/test_view_entity.py +134 -0
- graphcore/domain/strategy/BaseStrategy.py +759 -0
- graphcore/domain/strategy/DeploymentStrategies.py +55 -0
- graphcore/domain/strategy/GrantStrategies.py +189 -0
- graphcore/domain/strategy/GroupStrategies.py +193 -0
- graphcore/domain/strategy/InviteStrategies.py +84 -0
- graphcore/domain/strategy/RequestTemplateStrategies.py +126 -0
- graphcore/domain/strategy/ViewStrategies.py +219 -0
- graphcore/domain/strategy/__init__.py +0 -0
- graphcore/domain/strategy/runner.py +84 -0
- graphcore/domain/strategy/session.py +56 -0
- graphcore/domain/strategy/tests/BaseStrategyTest.py +167 -0
- graphcore/domain/strategy/tests/__init__.py +0 -0
- graphcore/domain/strategy/tests/test_BaseStrategies.py +870 -0
- graphcore/domain/strategy/tests/test_grant_strategies.py +245 -0
- graphcore/domain/strategy/tests/test_group_strategies.py +99 -0
- graphcore/domain/strategy/tests/test_runner.py +220 -0
- graphcore/domain/strategy/tests/test_strategy_locks.py +446 -0
- graphcore/domain/strategy/tests/test_utils.py +95 -0
- graphcore/domain/strategy/tests/test_view_strategies.py +436 -0
- graphcore/errors.py +145 -0
- graphcore/execution/README.md +97 -0
- graphcore/execution/StrategyExecutorService.py +333 -0
- graphcore/execution/__init__.py +0 -0
- graphcore/execution/dispatcher.py +19 -0
- graphcore/execution/environments/__init__.py +0 -0
- graphcore/execution/environments/celery.py +59 -0
- graphcore/execution/environments/in_process.py +55 -0
- graphcore/execution/local_session.py +117 -0
- graphcore/execution/redis_run_events.py +74 -0
- graphcore/execution/requests.py +22 -0
- graphcore/execution/run_events.py +85 -0
- graphcore/execution/strategy_context.py +54 -0
- graphcore/execution/tasks.py +34 -0
- graphcore/execution/tests/__init__.py +5 -0
- graphcore/execution/tests/contract.py +171 -0
- graphcore/execution/tests/support.py +310 -0
- graphcore/execution/tests/test_dispatchers.py +76 -0
- graphcore/execution/tests/test_executor.py +368 -0
- graphcore/execution/tests/test_local_session.py +95 -0
- graphcore/execution/tests/test_reconcile_runs.py +134 -0
- graphcore/execution/tests/test_run_events.py +106 -0
- graphcore/execution/tests/test_strategy_context.py +62 -0
- graphcore/gateway/__init__.py +1 -0
- graphcore/gateway/remote.py +35 -0
- graphcore/gateway/tests/__init__.py +0 -0
- graphcore/gateway/tests/test_remote.py +30 -0
- graphcore/graph/EntityService.py +710 -0
- graphcore/graph/__init__.py +0 -0
- graphcore/graph/entity_cache.py +152 -0
- graphcore/graph/tests/__init__.py +4 -0
- graphcore/graph/tests/test_EntityService.py +908 -0
- graphcore/graph/tests/test_EntityService_list.py +170 -0
- graphcore/graph/tests/test_EntityService_model_resolution.py +339 -0
- graphcore/graph/tests/test_EntityService_referrers.py +236 -0
- graphcore/graph/tests/test_EntityService_routing.py +119 -0
- graphcore/graph/tests/test_dependent_on.py +141 -0
- graphcore/graph/tests/test_engine_is_type_agnostic.py +25 -0
- graphcore/graph/tests/test_engine_resolves_read_permission.py +93 -0
- graphcore/graph/tests/test_entity_cache.py +159 -0
- graphcore/graph/tests/test_null_entity_cache.py +12 -0
- graphcore/management/__init__.py +0 -0
- graphcore/management/commands/__init__.py +0 -0
- graphcore/management/commands/dev_reset.py +18 -0
- graphcore/management/commands/graphcore_createadmin.py +65 -0
- graphcore/management/commands/graphcore_policy.py +59 -0
- graphcore/management/commands/graphcore_status.py +98 -0
- graphcore/management/tests/__init__.py +0 -0
- graphcore/management/tests/test_createadmin.py +78 -0
- graphcore/management/tests/test_policy_command.py +46 -0
- graphcore/management/tests/test_status_command.py +85 -0
- graphcore/migrations/0001_initial.py +186 -0
- graphcore/migrations/0002_invite.py +26 -0
- graphcore/migrations/__init__.py +0 -0
- graphcore/models.py +11 -0
- graphcore/persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md +396 -0
- graphcore/persistence/__init__.py +6 -0
- graphcore/persistence/django/__init__.py +0 -0
- graphcore/persistence/django/backends.py +43 -0
- graphcore/persistence/django/bindings.py +28 -0
- graphcore/persistence/django/cache.py +38 -0
- graphcore/persistence/django/compiler.py +214 -0
- graphcore/persistence/django/identity.py +18 -0
- graphcore/persistence/django/models.py +252 -0
- graphcore/persistence/django/repository.py +525 -0
- graphcore/persistence/django/tests/__init__.py +0 -0
- graphcore/persistence/django/tests/test_backends.py +68 -0
- graphcore/persistence/django/tests/test_compiler.py +269 -0
- graphcore/persistence/django/tests/test_repository.py +472 -0
- graphcore/persistence/django/tests/test_shared_column.py +120 -0
- graphcore/persistence/django/tests/test_threads.py +50 -0
- graphcore/persistence/django/tests/test_update_shape.py +175 -0
- graphcore/persistence/django/threads.py +38 -0
- graphcore/persistence/repository.py +172 -0
- graphcore/persistence/tests/__init__.py +0 -0
- graphcore/persistence/tests/contract.py +688 -0
- graphcore/persistence/tests/evaluate.py +51 -0
- graphcore/persistence/tests/fixtures.py +32 -0
- graphcore/persistence/tests/memory.py +263 -0
- graphcore/persistence/tests/test_memory_repository.py +10 -0
- graphcore/persistence/tests/test_query_types.py +127 -0
- graphcore/public/__init__.py +9 -0
- graphcore/public/api.py +9 -0
- graphcore/public/apps.py +10 -0
- graphcore/public/auth/__init__.py +1 -0
- graphcore/public/auth/grants.py +7 -0
- graphcore/public/auth/onboarding.py +8 -0
- graphcore/public/auth/perms.py +7 -0
- graphcore/public/auth/policy.py +10 -0
- graphcore/public/auth/roles.py +8 -0
- graphcore/public/auth/subjects.py +7 -0
- graphcore/public/entities/__init__.py +32 -0
- graphcore/public/errors.py +19 -0
- graphcore/public/persistence/__init__.py +44 -0
- graphcore/public/persistence/django.py +24 -0
- graphcore/public/scheduling.py +23 -0
- graphcore/public/strategies/__init__.py +85 -0
- graphcore/public/test_settings.py +16 -0
- graphcore/public/testing.py +47 -0
- graphcore/registry/EntityDirectory.py +246 -0
- graphcore/registry/README.md +147 -0
- graphcore/registry/StrategyDirectory.py +308 -0
- graphcore/registry/TYPE_METADATA_PLACEMENT.md +66 -0
- graphcore/registry/__init__.py +0 -0
- graphcore/registry/app_discovery.py +108 -0
- graphcore/registry/decorators.py +77 -0
- graphcore/registry/discovery.py +111 -0
- graphcore/registry/registry.py +79 -0
- graphcore/registry/tests/__init__.py +0 -0
- graphcore/registry/tests/test_agent_frontend_discovery.py +56 -0
- graphcore/registry/tests/test_app_config.py +36 -0
- graphcore/registry/tests/test_discovery.py +147 -0
- graphcore/registry/tests/test_entity_directory_storage.py +73 -0
- graphcore/registry/tests/test_import_package.py +87 -0
- graphcore/registry/tests/test_principal_flag.py +67 -0
- graphcore/registry/tests/test_strategy_directory.py +29 -0
- graphcore/routes/__init__.py +116 -0
- graphcore/routes/tests/__init__.py +0 -0
- graphcore/routes/tests/test_routes.py +90 -0
- graphcore/runtime.py +243 -0
- graphcore/scheduling/__init__.py +0 -0
- graphcore/scheduling/beat_scheduler.py +164 -0
- graphcore/scheduling/beat_tasks.py +63 -0
- graphcore/scheduling/scheduler.py +37 -0
- graphcore/scheduling/tests/__init__.py +0 -0
- graphcore/scheduling/tests/contract.py +136 -0
- graphcore/scheduling/tests/memory.py +42 -0
- graphcore/scheduling/tests/test_beat_scheduler.py +87 -0
- graphcore/scheduling/tests/test_memory_scheduler.py +15 -0
- graphcore/scheduling/tests/test_ticks.py +143 -0
- graphcore/tasks.py +9 -0
- graphcore/test_settings.py +78 -0
- graphcore/testing/__init__.py +0 -0
- graphcore/testing/case.py +112 -0
- graphcore/testing/runner.py +32 -0
- graphcore/testing/settings.py +26 -0
- graphcore/testing/tests/__init__.py +0 -0
- graphcore/testing/tests/agent_view_fixture.py +19 -0
- graphcore/testing/tests/test_case_and_runner.py +219 -0
- graphcore/testing/tests/test_settings_for_apps.py +50 -0
- graphcore/tests/__init__.py +0 -0
- graphcore/tests/ids.py +14 -0
- graphcore/tests/public_api_manifest.py +102 -0
- graphcore/tests/runs.py +7 -0
- graphcore/tests/test_app_discovery.py +90 -0
- graphcore/tests/test_composition_boundaries.py +231 -0
- graphcore/tests/test_docs_discovery.py +108 -0
- graphcore/tests/test_no_print.py +70 -0
- graphcore/tests/test_packaging.py +64 -0
- graphcore/tests/test_public_api_boundaries.py +229 -0
- graphcore/tests/test_public_api_contract.py +62 -0
- graphcore/tests/test_public_api_docs.py +28 -0
- graphcore/tests/test_runtime.py +42 -0
- graphcore/tests/test_shared_type_boundaries.py +128 -0
- graphcore/tests/test_shared_types.py +144 -0
- graphcore/tests/test_test_settings.py +45 -0
- graphcore/transport/__init__.py +0 -0
- graphcore/transport/agent_asgi.py +68 -0
- graphcore/transport/agent_client.py +85 -0
- graphcore/transport/channel_layer.py +68 -0
- graphcore/transport/consumers.py +507 -0
- graphcore/transport/routing.py +6 -0
- graphcore/transport/tests/__init__.py +0 -0
- graphcore/transport/tests/test_agent_client.py +31 -0
- graphcore/transport/tests/test_api_views.py +733 -0
- graphcore/transport/tests/test_channel_layer.py +96 -0
- graphcore/transport/tests/test_consumers.py +963 -0
- graphcore/transport/tests/test_resource_metadata.py +67 -0
- graphcore/transport/urls.py +29 -0
- graphcore/transport/views.py +358 -0
- graphcore/types/__init__.py +4 -0
- graphcore/types/access.py +114 -0
- graphcore/types/api.py +76 -0
- graphcore/types/entities.py +100 -0
- graphcore/types/entity_id.py +85 -0
- graphcore/types/execution.py +41 -0
- graphcore/types/identity.py +20 -0
- graphcore/types/onboarding.py +20 -0
- graphcore/types/query.py +116 -0
- graphcore/types/schedules.py +91 -0
- graphcore/types/strategy.py +73 -0
- graphcore/types/tests/__init__.py +0 -0
- graphcore/types/tests/test_entity_id.py +50 -0
- graphcore/urls.py +5 -0
- graphcore_kernel-0.1.0.dist-info/METADATA +94 -0
- graphcore_kernel-0.1.0.dist-info/RECORD +343 -0
- graphcore_kernel-0.1.0.dist-info/WHEEL +5 -0
- graphcore_kernel-0.1.0.dist-info/entry_points.txt +2 -0
- graphcore_kernel-0.1.0.dist-info/licenses/LICENSE +202 -0
- graphcore_kernel-0.1.0.dist-info/licenses/NOTICE +2 -0
- graphcore_kernel-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""The four generic tools, as plain async functions over a `Frontend`."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
from typing import Any, Optional
|
|
6
|
+
|
|
7
|
+
import jsonschema
|
|
8
|
+
|
|
9
|
+
from graphcore.agent_frontend.context import Context, Reads, label_of, to_address
|
|
10
|
+
from graphcore.agent_frontend.errors import AgentError
|
|
11
|
+
from graphcore.agent_frontend.render import Frontend, ResolvedTool, render
|
|
12
|
+
from graphcore.errors import InvalidRequest, NotFound
|
|
13
|
+
from graphcore.types.identity import Principal
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _json(value: Any) -> str:
|
|
17
|
+
return json.dumps(value, default=str, separators=(',', ':'))
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
async def open_(fe: Frontend, principal: Principal, at: str, version: Optional[str] = None) -> str:
|
|
21
|
+
rendered = await render(fe, principal, at)
|
|
22
|
+
if version is None:
|
|
23
|
+
return rendered.text
|
|
24
|
+
state = 'unchanged' if version == rendered.version else 'changed'
|
|
25
|
+
return f"{state} since {version}\n{rendered.text}"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _resolve_type(fe: Frontend, type_id: str) -> str:
|
|
29
|
+
known = fe.client.entity_types()
|
|
30
|
+
if type_id in known:
|
|
31
|
+
return type_id
|
|
32
|
+
matches = [t for t in known if t.endswith('.' + type_id)]
|
|
33
|
+
if len(matches) == 1:
|
|
34
|
+
return matches[0]
|
|
35
|
+
detail = f"; did you mean {', '.join(sorted(matches))}?" if matches else ''
|
|
36
|
+
raise AgentError('UnknownType', f"{type_id!r} is not an entity type{detail}")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
async def find(
|
|
40
|
+
fe: Frontend, principal: Principal, type: str, predicate: Optional[dict] = None, limit: int = 25,
|
|
41
|
+
) -> str:
|
|
42
|
+
type_id = _resolve_type(fe, type)
|
|
43
|
+
try:
|
|
44
|
+
page = await fe.client.list_entities(
|
|
45
|
+
principal, frozenset([type_id]), where=predicate or None, limit=limit,
|
|
46
|
+
)
|
|
47
|
+
except InvalidRequest as exc:
|
|
48
|
+
raise AgentError('InvalidRequest', str(exc))
|
|
49
|
+
lines = [
|
|
50
|
+
f"- {label_of(e)} · {e.entity_type_id} · {to_address(e.entity_id)}" for e in page.entities
|
|
51
|
+
]
|
|
52
|
+
head = f"{len(lines)} {type_id}" + (' (more exist; narrow the predicate)' if page.next_cursor else '')
|
|
53
|
+
return '\n'.join([head, *lines])
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _strategy_params(tool: ResolvedTool, params: dict) -> dict:
|
|
57
|
+
return {k: v for k, v in params.items() if not (tool.takes_target and k == 'target')}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
async def _target_id(fe: Frontend, target: str) -> str:
|
|
61
|
+
if target.startswith('/'):
|
|
62
|
+
resolved = fe.client.resolve(target)
|
|
63
|
+
if resolved['entity_id'] is None:
|
|
64
|
+
raise AgentError('InvalidRequest', f"{target} is not an entity address")
|
|
65
|
+
return resolved['entity_id']
|
|
66
|
+
return target
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
async def call(
|
|
70
|
+
fe: Frontend, principal: Principal, at: str, tool: str, params: Optional[dict] = None,
|
|
71
|
+
seen_version: Optional[str] = None,
|
|
72
|
+
) -> str:
|
|
73
|
+
params = params or {}
|
|
74
|
+
rendered = await render(fe, principal, at)
|
|
75
|
+
if seen_version is not None and seen_version != rendered.version:
|
|
76
|
+
raise AgentError(
|
|
77
|
+
'Stale',
|
|
78
|
+
f"{rendered.address} changed since version {seen_version}; current version "
|
|
79
|
+
f"{rendered.version}. Open it again, then retry.",
|
|
80
|
+
)
|
|
81
|
+
chosen = next((t for t in rendered.tools if t.name == tool), None)
|
|
82
|
+
if chosen is None:
|
|
83
|
+
names = ', '.join(t.name for t in rendered.tools) or 'none'
|
|
84
|
+
raise AgentError('UnknownTool', f"{tool!r} is not a tool at {rendered.address}; tools: {names}")
|
|
85
|
+
schema = chosen.full_schema()
|
|
86
|
+
try:
|
|
87
|
+
jsonschema.validate(params, schema)
|
|
88
|
+
except jsonschema.ValidationError as exc:
|
|
89
|
+
raise AgentError('InvalidRequest', f"{exc.message}; schema: {_json(schema)}")
|
|
90
|
+
|
|
91
|
+
try:
|
|
92
|
+
if chosen.spec.fn is not None:
|
|
93
|
+
ctx = Context(principal, rendered.address, rendered.entity, fe.client, Reads(), fe.registry, fe.query_max)
|
|
94
|
+
result = await chosen.spec.fn(ctx, **params)
|
|
95
|
+
else:
|
|
96
|
+
target = await _target_id(fe, params['target']) if chosen.takes_target else chosen.spec.bind
|
|
97
|
+
ctx = Context(principal, rendered.address, rendered.entity, fe.client, Reads(), fe.registry, fe.query_max)
|
|
98
|
+
result = await ctx.execute(chosen.spec.strategy, target, _strategy_params(chosen, params))
|
|
99
|
+
except NotFound as exc:
|
|
100
|
+
raise AgentError('NotFound', str(exc))
|
|
101
|
+
|
|
102
|
+
try:
|
|
103
|
+
after = await render(fe, principal, at)
|
|
104
|
+
except AgentError as exc:
|
|
105
|
+
# The tool deleted the place or revoked the caller's access to it.
|
|
106
|
+
if exc.code != 'NotFound':
|
|
107
|
+
raise
|
|
108
|
+
return _json({
|
|
109
|
+
'ok': True, 'tool': chosen.name, 'result': result, 'address': None,
|
|
110
|
+
'note': f"ran {chosen.name}; {rendered.address} no longer exists or is not readable",
|
|
111
|
+
})
|
|
112
|
+
return _json({
|
|
113
|
+
'ok': True, 'tool': chosen.name, 'result': result, 'address': after.address,
|
|
114
|
+
'version': after.version,
|
|
115
|
+
'note': f"ran {chosen.name} at {after.address}; open it to see the change",
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
async def act(
|
|
120
|
+
fe: Frontend, principal: Principal, strategy: str, target: Optional[str] = None,
|
|
121
|
+
params: Optional[dict] = None,
|
|
122
|
+
) -> str:
|
|
123
|
+
params = params or {}
|
|
124
|
+
info = fe.client.strategy_info().get(strategy)
|
|
125
|
+
if info is None:
|
|
126
|
+
raise AgentError('UnknownStrategy', f"{strategy!r} is not a registered strategy")
|
|
127
|
+
try:
|
|
128
|
+
jsonschema.validate(params, info.get('schema') or {})
|
|
129
|
+
except jsonschema.ValidationError as exc:
|
|
130
|
+
raise AgentError('InvalidRequest', f"{exc.message}; schema: {_json(info.get('schema'))}")
|
|
131
|
+
try:
|
|
132
|
+
target_id = await _target_id(fe, target) if target else None
|
|
133
|
+
ctx = Context(principal, '/', None, fe.client, Reads(), fe.registry, fe.query_max)
|
|
134
|
+
result = await ctx.execute(strategy, target_id, params)
|
|
135
|
+
except NotFound as exc:
|
|
136
|
+
raise AgentError('NotFound', str(exc))
|
|
137
|
+
return _json({'ok': True, 'strategy': strategy, 'result': result})
|
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
"""Transport-agnostic GraphAPI — the in-process Tier 1 surface.
|
|
2
|
+
|
|
3
|
+
HTTP, WebSocket, and (later) MCP adapters call this; they do wire format and
|
|
4
|
+
error-code mapping only.
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import hashlib
|
|
9
|
+
import hmac
|
|
10
|
+
import logging
|
|
11
|
+
import secrets
|
|
12
|
+
import time
|
|
13
|
+
from typing import Optional
|
|
14
|
+
|
|
15
|
+
from graphcore.types.access import GrantInfo, ManageAccess, Role
|
|
16
|
+
from graphcore.errors import (
|
|
17
|
+
ConcurrentModificationError,
|
|
18
|
+
Forbidden,
|
|
19
|
+
InvalidRequest,
|
|
20
|
+
KernelError,
|
|
21
|
+
NotFound,
|
|
22
|
+
RemoteUnavailable,
|
|
23
|
+
StrategyFailed,
|
|
24
|
+
log_and_correlate,
|
|
25
|
+
)
|
|
26
|
+
from graphcore.types.identity import Principal
|
|
27
|
+
from graphcore.types.api import AccessSummary, StrategyOutcome, StrategyRequestPayload
|
|
28
|
+
from graphcore.types.entities import EntityFilter, EntityPage, EntityResult, Tombstone, view_etag
|
|
29
|
+
from graphcore.domain.entity.Entity import Entity
|
|
30
|
+
from graphcore.domain.entity.group.Group import Group
|
|
31
|
+
from graphcore.execution.StrategyExecutorService import StrategyExecutorService
|
|
32
|
+
from graphcore.graph.EntityService import EntityService
|
|
33
|
+
from graphcore.registry.registry import Registry
|
|
34
|
+
from graphcore.types.query import Predicate
|
|
35
|
+
|
|
36
|
+
logger = logging.getLogger(__name__)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class GraphAPI:
|
|
40
|
+
"""Application-service facade over the entity graph and strategy executor."""
|
|
41
|
+
|
|
42
|
+
def __init__(
|
|
43
|
+
self,
|
|
44
|
+
registry: Registry,
|
|
45
|
+
entity_service: EntityService,
|
|
46
|
+
strategy_executor: StrategyExecutorService,
|
|
47
|
+
max_batch_size: int,
|
|
48
|
+
):
|
|
49
|
+
self.registry = registry
|
|
50
|
+
self.entity_service = entity_service
|
|
51
|
+
self.strategy_executor = strategy_executor
|
|
52
|
+
self.max_batch_size = max_batch_size
|
|
53
|
+
self.broadcaster = self.entity_service.broadcaster
|
|
54
|
+
|
|
55
|
+
async def get_entity(self, principal: Principal, entity_id: str) -> EntityResult[Entity]:
|
|
56
|
+
try:
|
|
57
|
+
return await self.entity_service.get_entity(entity_id, principal)
|
|
58
|
+
except Forbidden:
|
|
59
|
+
return Tombstone(entity_id=str(entity_id), cause='forbidden')
|
|
60
|
+
except NotFound:
|
|
61
|
+
return Tombstone(entity_id=str(entity_id), cause='missing')
|
|
62
|
+
except RemoteUnavailable as exc:
|
|
63
|
+
return Tombstone(entity_id=str(entity_id), cause=exc.cause)
|
|
64
|
+
|
|
65
|
+
async def get_entities(
|
|
66
|
+
self, principal: Principal, entity_ids: list
|
|
67
|
+
) -> dict[str, EntityResult[Entity]]:
|
|
68
|
+
if len(entity_ids) > self.max_batch_size:
|
|
69
|
+
raise InvalidRequest(f'entity_ids exceeds the maximum of {self.max_batch_size}')
|
|
70
|
+
return await self.entity_service.get_entities(entity_ids, principal)
|
|
71
|
+
|
|
72
|
+
async def changed_entities(
|
|
73
|
+
self, principal: Principal, held: dict[str, str]
|
|
74
|
+
) -> dict[str, EntityResult[Entity]]:
|
|
75
|
+
"""The entities whose client view differs from what a client holds.
|
|
76
|
+
`held` maps each id to the `etag` the client has; an id gone from
|
|
77
|
+
storage comes back as a `deleted` tombstone. A remote entity that
|
|
78
|
+
can't be read right now is left out: what the client holds stands."""
|
|
79
|
+
if len(held) > self.max_batch_size:
|
|
80
|
+
raise InvalidRequest(f'entities exceeds the maximum of {self.max_batch_size}')
|
|
81
|
+
current = await self.entity_service.get_entities(list(held), principal)
|
|
82
|
+
changed = {}
|
|
83
|
+
for entity_id, result in current.items():
|
|
84
|
+
if isinstance(result, Tombstone):
|
|
85
|
+
if result.cause == 'missing':
|
|
86
|
+
result = Tombstone(entity_id=entity_id, cause='deleted')
|
|
87
|
+
elif result.cause != 'forbidden':
|
|
88
|
+
continue
|
|
89
|
+
if view_etag(result.to_client()) != held.get(entity_id):
|
|
90
|
+
changed[entity_id] = result
|
|
91
|
+
return changed
|
|
92
|
+
|
|
93
|
+
async def get_referrers(
|
|
94
|
+
self, principal: Principal, entity_id: str, where: Optional[Predicate] = None,
|
|
95
|
+
cursor: Optional[str] = None, limit: int = 25,
|
|
96
|
+
) -> EntityPage[Entity]:
|
|
97
|
+
"""Reverse lookup: the entities the caller can read that reference
|
|
98
|
+
`entity_id`, narrowed by `where`."""
|
|
99
|
+
limit = min(max(int(limit), 1), self.max_batch_size)
|
|
100
|
+
return await self.entity_service.referrers(
|
|
101
|
+
entity_id, principal, where=where, cursor=cursor, limit=limit,
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
async def execute_strategies(
|
|
105
|
+
self,
|
|
106
|
+
principal: Principal,
|
|
107
|
+
payloads: list[StrategyRequestPayload],
|
|
108
|
+
wait: bool = False,
|
|
109
|
+
callback_channel: Optional[str] = None,
|
|
110
|
+
) -> list[StrategyOutcome]:
|
|
111
|
+
if len(payloads) == 0:
|
|
112
|
+
raise InvalidRequest('strategy list cannot be empty')
|
|
113
|
+
if len(payloads) > self.max_batch_size:
|
|
114
|
+
raise InvalidRequest(f'strategy list exceeds the maximum of {self.max_batch_size}')
|
|
115
|
+
|
|
116
|
+
outcomes: list[StrategyOutcome] = []
|
|
117
|
+
for payload in payloads:
|
|
118
|
+
try:
|
|
119
|
+
request, _ = await self.strategy_executor.submit(
|
|
120
|
+
payload, principal, wait=wait, callback_channel=callback_channel,
|
|
121
|
+
)
|
|
122
|
+
outcomes.append(StrategyOutcome(
|
|
123
|
+
status=request.status,
|
|
124
|
+
strategy_request_id=request.entity_id,
|
|
125
|
+
target_entity_id=request.target_entity_id,
|
|
126
|
+
ret_val=request.ret_val or {},
|
|
127
|
+
error=request.error,
|
|
128
|
+
error_code=request.error_code,
|
|
129
|
+
error_details=request.error_details or {},
|
|
130
|
+
client_request_id=payload.client_request_id,
|
|
131
|
+
))
|
|
132
|
+
except KernelError as exc:
|
|
133
|
+
logger.warning(
|
|
134
|
+
f"Strategy {payload.strategy_name} on {payload.target_entity_id} failed "
|
|
135
|
+
f"for {principal.principal_entity_id}: {exc}"
|
|
136
|
+
)
|
|
137
|
+
details = dict(exc.details)
|
|
138
|
+
outcomes.append(StrategyOutcome(
|
|
139
|
+
status='failed',
|
|
140
|
+
strategy_request_id=details.pop('strategy_request_id', None),
|
|
141
|
+
target_entity_id=payload.target_entity_id,
|
|
142
|
+
error=str(exc),
|
|
143
|
+
error_code=exc.code,
|
|
144
|
+
error_details=details,
|
|
145
|
+
client_request_id=payload.client_request_id,
|
|
146
|
+
))
|
|
147
|
+
except Exception:
|
|
148
|
+
log_and_correlate(
|
|
149
|
+
logger, f"Error executing strategy request for entity {payload.target_entity_id}"
|
|
150
|
+
)
|
|
151
|
+
outcomes.append(StrategyOutcome(
|
|
152
|
+
status='failed',
|
|
153
|
+
target_entity_id=payload.target_entity_id,
|
|
154
|
+
error='Internal server error',
|
|
155
|
+
error_code=StrategyFailed.code,
|
|
156
|
+
client_request_id=payload.client_request_id,
|
|
157
|
+
))
|
|
158
|
+
return outcomes
|
|
159
|
+
|
|
160
|
+
def describe_entities(self) -> dict:
|
|
161
|
+
"""The entity directory, plus per-type access facts (roles,
|
|
162
|
+
public_permissions) - identical for every instance of a type, so
|
|
163
|
+
computed here once rather than per-resource in get_access."""
|
|
164
|
+
directory = self.registry.entities.get_entity_directory()
|
|
165
|
+
policy = self.registry.policy
|
|
166
|
+
for entity_type_id, entity_info in directory.items():
|
|
167
|
+
entity_cls = self.registry.entities.get_entity_class(entity_type_id)
|
|
168
|
+
entity_info['group'] = issubclass(entity_cls, Group)
|
|
169
|
+
entity_info['shareable'] = policy.is_shareable(entity_cls)
|
|
170
|
+
entity_info['roles'] = [
|
|
171
|
+
{
|
|
172
|
+
'name': role.name,
|
|
173
|
+
'description': role.description,
|
|
174
|
+
'permissions': sorted(type(p).__name__ for p in role.perms),
|
|
175
|
+
'permission_details': sorted(
|
|
176
|
+
({'name': type(p).__name__, 'description': p.description} for p in role.perms),
|
|
177
|
+
key=lambda detail: detail['name'],
|
|
178
|
+
),
|
|
179
|
+
}
|
|
180
|
+
for role in policy.available_roles(entity_cls)
|
|
181
|
+
]
|
|
182
|
+
entity_info['public_permissions'] = sorted(
|
|
183
|
+
type(perm).__name__ for perm in policy.type_public_perms(entity_cls)
|
|
184
|
+
)
|
|
185
|
+
return directory
|
|
186
|
+
|
|
187
|
+
def describe_strategies(self) -> dict:
|
|
188
|
+
return self.registry.strategies.get_strategy_directory()
|
|
189
|
+
|
|
190
|
+
def routes(self) -> list[dict]:
|
|
191
|
+
return [
|
|
192
|
+
{'pattern': r.pattern, 'view_name': r.view_name, 'app_id': r.app_id}
|
|
193
|
+
for r in self.registry.routes.all()
|
|
194
|
+
]
|
|
195
|
+
|
|
196
|
+
def resolve(self, address: str) -> dict:
|
|
197
|
+
resolved = self.registry.routes.resolve(address)
|
|
198
|
+
return {'view_name': resolved.view_name, 'entity_id': resolved.entity_id}
|
|
199
|
+
|
|
200
|
+
# --- authorization: grant/revoke/list, sugar over the grant strategies ---
|
|
201
|
+
|
|
202
|
+
async def grant(
|
|
203
|
+
self, principal: Principal, resource_id: str, subject_id: str, role_name: str,
|
|
204
|
+
) -> str:
|
|
205
|
+
"""Grant `role_name` to `subject_id` on `resource_id`, creating or
|
|
206
|
+
replacing the grant. Returns the Grant id."""
|
|
207
|
+
from graphcore.domain.strategy.GrantStrategies import CreateGrant
|
|
208
|
+
|
|
209
|
+
request = await self._run(principal, CreateGrant.request_constructor(
|
|
210
|
+
target_entity_id=resource_id, subject_id=subject_id, role_name=role_name,
|
|
211
|
+
))
|
|
212
|
+
return request.ret_val['grant_entity_id']
|
|
213
|
+
|
|
214
|
+
async def revoke(self, principal: Principal, resource_id: str, subject_id: str) -> None:
|
|
215
|
+
"""Revoke `subject_id`'s grant on `resource_id`, if any."""
|
|
216
|
+
from graphcore.domain.entity.grant.Grant import Grant
|
|
217
|
+
from graphcore.domain.strategy.GrantStrategies import RevokeGrant
|
|
218
|
+
|
|
219
|
+
resource = await self.entity_service.get_entity(resource_id, principal)
|
|
220
|
+
for child_id in resource.children_ids:
|
|
221
|
+
try:
|
|
222
|
+
child = await self.entity_service.get_entity(child_id, principal)
|
|
223
|
+
except NotFound:
|
|
224
|
+
continue
|
|
225
|
+
if isinstance(child, Grant) and child.subject_id == subject_id:
|
|
226
|
+
await self._run(principal, RevokeGrant.request_constructor(
|
|
227
|
+
target_entity_id=child.entity_id,
|
|
228
|
+
))
|
|
229
|
+
return
|
|
230
|
+
|
|
231
|
+
# --- invites: see docs/AUTHORIZATION_TECHNICAL_TDD.md (Invites) ---
|
|
232
|
+
|
|
233
|
+
async def create_invite(
|
|
234
|
+
self, principal: Principal, resource_id: str, role_name: str, **options,
|
|
235
|
+
) -> tuple[str, str]:
|
|
236
|
+
"""Create an invite to `resource_id` and return `(invite_id, token)`.
|
|
237
|
+
The token is the only copy of the secret; the strategy sees its hash."""
|
|
238
|
+
from graphcore.domain.strategy.InviteStrategies import CreateInvite
|
|
239
|
+
|
|
240
|
+
secret = secrets.token_urlsafe(32)
|
|
241
|
+
request = await self._run(principal, CreateInvite.request_constructor(
|
|
242
|
+
target_entity_id=resource_id, role_name=role_name,
|
|
243
|
+
token_hash=_sha256(secret), **options,
|
|
244
|
+
))
|
|
245
|
+
invite_id = request.ret_val['invite_entity_id']
|
|
246
|
+
return invite_id, f'{invite_id}.{secret}'
|
|
247
|
+
|
|
248
|
+
async def redeem_invite(self, principal: Principal, token: str) -> dict:
|
|
249
|
+
"""Grant the invite's role on its resource to `principal`. Every
|
|
250
|
+
refusal is the same `NotFound`, so callers learn nothing about which
|
|
251
|
+
invites exist."""
|
|
252
|
+
from graphcore.auth.authz.kernel_writes import grant
|
|
253
|
+
from graphcore.auth.authz.lookups import find_grant
|
|
254
|
+
from graphcore.domain.entity.invite.Invite import Invite
|
|
255
|
+
from graphcore.execution.strategy_context import kernel_internal_context
|
|
256
|
+
|
|
257
|
+
invite_id, _, secret = str(token).rpartition('.')
|
|
258
|
+
refused = NotFound('No such invite')
|
|
259
|
+
repository = self.entity_service.repository
|
|
260
|
+
subject_id = principal.principal_entity_id
|
|
261
|
+
for _attempt in range(3):
|
|
262
|
+
invite = await repository.load(invite_id) if invite_id and secret else None
|
|
263
|
+
if not isinstance(invite, Invite) or not hmac.compare_digest(invite.token_hash or '', _sha256(secret)):
|
|
264
|
+
raise refused
|
|
265
|
+
resource = await repository.load(invite.parent_id)
|
|
266
|
+
if resource is None:
|
|
267
|
+
raise refused
|
|
268
|
+
result = {'resource_id': resource.entity_id, 'entity_type_id': resource.entity_type_id}
|
|
269
|
+
# Already admitted: redeeming changes nothing and uses nothing up.
|
|
270
|
+
if subject_id in invite.redeemed_by:
|
|
271
|
+
return result
|
|
272
|
+
if (await find_grant(self.entity_service, resource.entity_id, subject_id) is not None
|
|
273
|
+
or await self.entity_service.access_adapter.allows(principal, ManageAccess(), resource.entity_id)):
|
|
274
|
+
return result
|
|
275
|
+
if (invite.expires_at is not None and invite.expires_at <= time.time()) \
|
|
276
|
+
or len(invite.redeemed_by) >= invite.max_uses:
|
|
277
|
+
raise refused
|
|
278
|
+
# Claim the use first: the version check makes concurrent
|
|
279
|
+
# redemptions of the last use lose here, before any grant.
|
|
280
|
+
invite.redeemed_by = [*invite.redeemed_by, subject_id]
|
|
281
|
+
with kernel_internal_context():
|
|
282
|
+
try:
|
|
283
|
+
await self.entity_service.save_entity(invite, principal)
|
|
284
|
+
except ConcurrentModificationError:
|
|
285
|
+
continue
|
|
286
|
+
await grant(self.entity_service, resource.entity_id, subject_id, invite.role_name, principal)
|
|
287
|
+
return result
|
|
288
|
+
raise refused
|
|
289
|
+
|
|
290
|
+
async def _run(self, principal: Principal, payload: StrategyRequestPayload):
|
|
291
|
+
request, _ = await self.strategy_executor.submit(payload, principal)
|
|
292
|
+
return request
|
|
293
|
+
|
|
294
|
+
async def list_grants(self, principal: Principal, resource_id: str) -> list[GrantInfo]:
|
|
295
|
+
"""Requires ManageAccess on the resource: only those who can change
|
|
296
|
+
access see who has it."""
|
|
297
|
+
adapter = self.entity_service.access_adapter
|
|
298
|
+
if not await adapter.allows(principal, ManageAccess(), resource_id):
|
|
299
|
+
raise Forbidden(
|
|
300
|
+
f"User {principal.principal_entity_id} cannot manage access to {resource_id}"
|
|
301
|
+
)
|
|
302
|
+
return await adapter.list_grants(principal, resource_id)
|
|
303
|
+
|
|
304
|
+
async def available_roles(self, resource_id: str) -> list[Role]:
|
|
305
|
+
return await self.entity_service.access_adapter.available_roles(resource_id)
|
|
306
|
+
|
|
307
|
+
async def get_access(self, principal: Principal, resource_id: str) -> AccessSummary:
|
|
308
|
+
"""The share surface's instance-level facts for one resource: whether
|
|
309
|
+
the caller can manage it and, only if so, its effective grants.
|
|
310
|
+
Requires Read (get_entity raises otherwise). Type-level facts (roles, public_permissions) are on the
|
|
311
|
+
entity directory - identical for every instance of a type, so they
|
|
312
|
+
don't belong in a per-resource call."""
|
|
313
|
+
await self.entity_service.get_entity(resource_id, principal)
|
|
314
|
+
adapter = self.entity_service.access_adapter
|
|
315
|
+
can_manage = await adapter.allows(principal, ManageAccess(), resource_id)
|
|
316
|
+
grants = await adapter.list_grants(principal, resource_id) if can_manage else []
|
|
317
|
+
return AccessSummary(resource_id=resource_id, can_manage=can_manage, grants=grants)
|
|
318
|
+
|
|
319
|
+
async def list_entities(
|
|
320
|
+
self, principal: Principal, entity_filter: EntityFilter,
|
|
321
|
+
cursor: Optional[str] = None, limit: int = 25,
|
|
322
|
+
) -> EntityPage[Entity]:
|
|
323
|
+
"""The share surface's subject picker, and any future type browser:
|
|
324
|
+
an enforced, paginated listing over `entity_filter`."""
|
|
325
|
+
limit = min(max(int(limit), 1), self.max_batch_size)
|
|
326
|
+
return await self.entity_service.list_entities(
|
|
327
|
+
principal, entity_filter, cursor=cursor, limit=limit,
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
async def list_children(
|
|
331
|
+
self, principal: Principal, parent_id: str, type_ids: Optional[frozenset[str]] = None,
|
|
332
|
+
cursor: Optional[str] = None, limit: int = 25,
|
|
333
|
+
) -> EntityPage[Entity]:
|
|
334
|
+
"""The children of `parent_id` the caller can read, of `type_ids` or
|
|
335
|
+
of every type the parent's policy lets it contain."""
|
|
336
|
+
if type_ids is None:
|
|
337
|
+
parent = await self.entity_service.get_entity(parent_id, principal)
|
|
338
|
+
entities = self.registry.entities
|
|
339
|
+
contained = self.registry.policy.can_contain(type(parent))
|
|
340
|
+
type_ids = frozenset(
|
|
341
|
+
type_id for type_id, cls in entities.get_all_entities().items()
|
|
342
|
+
if contained is None or issubclass(cls, contained)
|
|
343
|
+
)
|
|
344
|
+
return await self.list_entities(
|
|
345
|
+
principal, EntityFilter(type_ids=type_ids, parent_id=parent_id),
|
|
346
|
+
cursor=cursor, limit=limit,
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
def type_strategies(self, entity_type_id: str) -> list[dict]:
|
|
350
|
+
"""The strategies the policy allows on an entity type, serialized."""
|
|
351
|
+
entity_cls = self.registry.entities.get_entity_class(entity_type_id)
|
|
352
|
+
if entity_cls is None:
|
|
353
|
+
raise NotFound(f"Entity type {entity_type_id} is not registered")
|
|
354
|
+
return self._allowed_strategies(entity_cls)
|
|
355
|
+
|
|
356
|
+
def deployment_strategies(self) -> list[dict]:
|
|
357
|
+
"""The strategies that may run with no target."""
|
|
358
|
+
from graphcore.domain.entity.deployment.Deployment import Deployment
|
|
359
|
+
|
|
360
|
+
allowed = self.registry.policy.allowed_strategies(Deployment)
|
|
361
|
+
return [cls.serialize() for cls in sorted(allowed or (), key=lambda c: c.__name__)]
|
|
362
|
+
|
|
363
|
+
def _allowed_strategies(self, entity_cls: type) -> list[dict]:
|
|
364
|
+
allowed = self.registry.policy.allowed_strategies(entity_cls)
|
|
365
|
+
if allowed is None:
|
|
366
|
+
classes: dict[str, type] = {}
|
|
367
|
+
for base in entity_cls.__mro__:
|
|
368
|
+
for strategy_cls in self.registry.strategies.get_strategies(base.__name__):
|
|
369
|
+
classes.setdefault(strategy_cls.__name__, strategy_cls)
|
|
370
|
+
allowed = classes.values()
|
|
371
|
+
return [cls.serialize() for cls in sorted(allowed, key=lambda c: c.__name__)]
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
def _sha256(secret: str) -> str:
|
|
375
|
+
return hashlib.sha256(secret.encode()).hexdigest()
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
__all__ = ['GraphAPI']
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""The predicate wire encoding: one JSON object per node, as specified in
|
|
2
|
+
`persistence/ENTITY_REPOSITORY_TECHNICAL_TDD.md` (Wire encoding)."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from graphcore.errors import InvalidRequest
|
|
6
|
+
from graphcore.types.query import AllOf, AnyOf, ContainsAny, ContainsValue, Equal, InValues, Predicate
|
|
7
|
+
|
|
8
|
+
MAX_DEPTH = 8
|
|
9
|
+
MAX_VALUES = 1000
|
|
10
|
+
|
|
11
|
+
_SCALARS = (str, int, float, bool, type(None))
|
|
12
|
+
_LEAVES = {'equal': Equal, 'contains_value': ContainsValue}
|
|
13
|
+
_SETS = {'in_values': InValues, 'contains_any': ContainsAny}
|
|
14
|
+
_BRANCHES = {'all_of': AllOf, 'any_of': AnyOf}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _scalar(value):
|
|
18
|
+
if not isinstance(value, _SCALARS):
|
|
19
|
+
raise InvalidRequest(f"predicate value {value!r} is not a JSON scalar")
|
|
20
|
+
return value
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _field(body: dict, name: str) -> str:
|
|
24
|
+
field = body.get('field')
|
|
25
|
+
if not isinstance(field, str) or not field:
|
|
26
|
+
raise InvalidRequest(f"{name} needs a non-empty string 'field'")
|
|
27
|
+
return field
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _body(body, name: str, keys: set) -> dict:
|
|
31
|
+
if not isinstance(body, dict) or set(body) != keys:
|
|
32
|
+
raise InvalidRequest(f"{name} must be an object with exactly {sorted(keys)}")
|
|
33
|
+
return body
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def decode_predicate(obj) -> Predicate:
|
|
37
|
+
"""Raises `InvalidRequest` for anything outside the encoding."""
|
|
38
|
+
budget = [MAX_VALUES]
|
|
39
|
+
|
|
40
|
+
def spend(n: int) -> None:
|
|
41
|
+
budget[0] -= n
|
|
42
|
+
if budget[0] < 0:
|
|
43
|
+
raise InvalidRequest(f"predicate carries more than {MAX_VALUES} values")
|
|
44
|
+
|
|
45
|
+
def decode(node, depth: int) -> Predicate:
|
|
46
|
+
if depth > MAX_DEPTH:
|
|
47
|
+
raise InvalidRequest(f"predicate is more than {MAX_DEPTH} levels deep")
|
|
48
|
+
if not isinstance(node, dict) or len(node) != 1:
|
|
49
|
+
raise InvalidRequest("a predicate node is an object with exactly one key")
|
|
50
|
+
((name, body),) = node.items()
|
|
51
|
+
if name in _LEAVES:
|
|
52
|
+
body = _body(body, name, {'field', 'value'})
|
|
53
|
+
spend(1)
|
|
54
|
+
return _LEAVES[name](_field(body, name), _scalar(body['value']))
|
|
55
|
+
if name in _SETS:
|
|
56
|
+
body = _body(body, name, {'field', 'values'})
|
|
57
|
+
values = body['values']
|
|
58
|
+
if not isinstance(values, list):
|
|
59
|
+
raise InvalidRequest(f"{name} 'values' must be a list")
|
|
60
|
+
spend(len(values))
|
|
61
|
+
return _SETS[name](_field(body, name), frozenset(_scalar(v) for v in values))
|
|
62
|
+
if name in _BRANCHES:
|
|
63
|
+
if not isinstance(body, list):
|
|
64
|
+
raise InvalidRequest(f"{name} must be a list of nodes")
|
|
65
|
+
return _BRANCHES[name](tuple(decode(child, depth + 1) for child in body))
|
|
66
|
+
raise InvalidRequest(f"unknown predicate node {name!r}")
|
|
67
|
+
|
|
68
|
+
return decode(obj, 1)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def encode_predicate(predicate: Predicate) -> dict:
|
|
72
|
+
for name, cls in _LEAVES.items():
|
|
73
|
+
if type(predicate) is cls:
|
|
74
|
+
return {name: {'field': predicate.field, 'value': predicate.value}}
|
|
75
|
+
for name, cls in _SETS.items():
|
|
76
|
+
if type(predicate) is cls:
|
|
77
|
+
return {name: {'field': predicate.field, 'values': sorted(predicate.values, key=repr)}}
|
|
78
|
+
for name, cls in _BRANCHES.items():
|
|
79
|
+
if type(predicate) is cls:
|
|
80
|
+
return {name: [encode_predicate(child) for child in predicate.predicates]}
|
|
81
|
+
raise InvalidRequest(f"{type(predicate).__name__} has no wire encoding")
|
|
File without changes
|