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,447 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import json
|
|
3
|
+
import subprocess
|
|
4
|
+
import sys
|
|
5
|
+
import unittest
|
|
6
|
+
|
|
7
|
+
from graphcore.agent_frontend import agent_view, operation, strategy_tool
|
|
8
|
+
from graphcore.agent_frontend.api import AgentView, view_of
|
|
9
|
+
from graphcore.agent_frontend.base.views import MAX_FIELD_CHARS
|
|
10
|
+
from graphcore.agent_frontend.errors import AgentError, RenderTooLarge
|
|
11
|
+
from graphcore.agent_frontend.loading import load_registry
|
|
12
|
+
from graphcore.agent_frontend.registry import AgentRegistry
|
|
13
|
+
from graphcore.agent_frontend.render import Frontend, render
|
|
14
|
+
from graphcore.agent_frontend import tools
|
|
15
|
+
from graphcore.agent_frontend.tests.fakes import PRINCIPAL, FakeEntity, FakeGraphClient
|
|
16
|
+
from graphcore.types.query import ContainsValue
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def run(coro):
|
|
20
|
+
return asyncio.run(coro)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ImportBoundaryTests(unittest.TestCase):
|
|
24
|
+
def test_no_django_or_kernel_internals(self):
|
|
25
|
+
code = (
|
|
26
|
+
"import sys, graphcore.agent_frontend.tools, graphcore.agent_frontend.render, graphcore.agent_frontend.testing;"
|
|
27
|
+
"bad=[m for m in sys.modules if m=='django' or m.startswith('django.')];"
|
|
28
|
+
"allowed=('graphcore.agent_frontend','graphcore.types','graphcore.errors','graphcore.public','graphcore');"
|
|
29
|
+
"bad+=[m for m in sys.modules if m.startswith('graphcore') and not m.startswith(allowed[:4]) and m!='graphcore'];"
|
|
30
|
+
"print(sorted(bad))"
|
|
31
|
+
)
|
|
32
|
+
out = subprocess.run([sys.executable, '-c', code], capture_output=True, text=True)
|
|
33
|
+
self.assertEqual(out.returncode, 0, out.stderr)
|
|
34
|
+
self.assertEqual(out.stdout.strip(), '[]')
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class RegistryTests(unittest.TestCase):
|
|
38
|
+
def test_duplicate_name(self):
|
|
39
|
+
r = AgentRegistry()
|
|
40
|
+
r.add(AgentView('a', lambda c: []))
|
|
41
|
+
with self.assertRaises(ValueError):
|
|
42
|
+
r.add(AgentView('a', lambda c: []))
|
|
43
|
+
|
|
44
|
+
def test_default_for_collision(self):
|
|
45
|
+
r = AgentRegistry()
|
|
46
|
+
r.add(AgentView('a', lambda c: [], default_for=('t.Note',)))
|
|
47
|
+
with self.assertRaisesRegex(ValueError, "'a' and 'b'"):
|
|
48
|
+
r.add(AgentView('b', lambda c: [], default_for=('t.Note',)))
|
|
49
|
+
|
|
50
|
+
def test_app_view_replaces_base_view_of_the_same_name_or_type(self):
|
|
51
|
+
r = AgentRegistry()
|
|
52
|
+
r.add(AgentView('entity', lambda c: [], default_for=('t.Note',)), base=True)
|
|
53
|
+
app = AgentView('entity', lambda c: [], default_for=('t.Note',))
|
|
54
|
+
r.add(app)
|
|
55
|
+
self.assertIs(r.by_name('entity'), app)
|
|
56
|
+
self.assertIs(r.default_for('t.Note'), app)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class FrontendTests(unittest.TestCase):
|
|
60
|
+
def setUp(self):
|
|
61
|
+
self.client = FakeGraphClient()
|
|
62
|
+
self.folder = self.client.add(FakeEntity('gc://b/1', 't.Folder', fields={'name': 'Work'}))
|
|
63
|
+
self.note = self.client.add(
|
|
64
|
+
FakeEntity('gc://b/2', 't.Note', parent_id='gc://b/1', fields={'title': 'Hello'}))
|
|
65
|
+
self.registry = load_registry([])
|
|
66
|
+
self.fe = Frontend(self.client, self.registry, max_chars=5000)
|
|
67
|
+
|
|
68
|
+
def test_default_entity_lists_children_and_tools(self):
|
|
69
|
+
r = run(render(self.fe, PRINCIPAL, '/e/b/1'))
|
|
70
|
+
self.assertIn('# Work · t.Folder', r.text)
|
|
71
|
+
self.assertIn('- Hello · t.Note · /e/b/2', r.text)
|
|
72
|
+
self.assertEqual(len(r.version), 16)
|
|
73
|
+
|
|
74
|
+
def test_default_entity_sections(self):
|
|
75
|
+
text = run(render(self.fe, PRINCIPAL, '/e/b/2')).text
|
|
76
|
+
self.assertIn('## Entity\nid: gc://b/2\ntype: t.Note\nowner: none\n'
|
|
77
|
+
'parent: Work · t.Folder · /e/b/1\nversion: 1', text)
|
|
78
|
+
self.assertIn('## Fields\ntitle: Hello', text)
|
|
79
|
+
self.assertIn('## Children\nnone', text)
|
|
80
|
+
self.assertIn('## Children (1)\n\n- Hello · t.Note · /e/b/2', run(render(self.fe, PRINCIPAL, '/e/b/1')).text)
|
|
81
|
+
|
|
82
|
+
def test_default_entity_top_level_and_owned_by_the_caller(self):
|
|
83
|
+
self.folder.owner = PRINCIPAL.principal_entity_id
|
|
84
|
+
text = run(render(self.fe, PRINCIPAL, '/e/b/1')).text
|
|
85
|
+
self.assertIn(f'owner: {PRINCIPAL.principal_entity_id} (you)', text)
|
|
86
|
+
self.assertIn('parent: none (top level)', text)
|
|
87
|
+
|
|
88
|
+
def test_default_entity_unreadable_parent(self):
|
|
89
|
+
self.client.tombstones.add('gc://b/1')
|
|
90
|
+
self.assertIn('parent: gc://b/1 (not readable)', run(render(self.fe, PRINCIPAL, '/e/b/2')).text)
|
|
91
|
+
|
|
92
|
+
def test_default_entity_prints_every_field(self):
|
|
93
|
+
self.note.fields.update({
|
|
94
|
+
'attributes': {}, 'tags': ['a', 'b'], 'body': 'line one\nline two', 'empty': '',
|
|
95
|
+
'archived': False, 'due': None, 'renders': 'gc://b/1', 'long': 'x' * (MAX_FIELD_CHARS + 1),
|
|
96
|
+
})
|
|
97
|
+
text = run(render(self.fe, PRINCIPAL, '/e/b/2')).text
|
|
98
|
+
for line in ('attributes: {}', 'tags: ["a", "b"]', 'body: "line one\\nline two"', 'empty: ""',
|
|
99
|
+
'archived: false', 'due: null', 'renders: gc://b/1 · /e/b/1',
|
|
100
|
+
f"long: {'x' * MAX_FIELD_CHARS}… (truncated; {MAX_FIELD_CHARS + 1} characters)"):
|
|
101
|
+
self.assertIn(line, text)
|
|
102
|
+
|
|
103
|
+
def test_home_lists_places_then_top_level_entities(self):
|
|
104
|
+
text = run(render(self.fe, PRINCIPAL, '/')).text
|
|
105
|
+
self.assertIn('## Places\n\n- notes.index · /notes', text)
|
|
106
|
+
self.assertNotIn('/notes/{entity}', text)
|
|
107
|
+
self.assertLess(text.index('## Places'), text.index('## Top-level entities'))
|
|
108
|
+
self.assertIn('### t.Folder (1)\n\n- Work · t.Folder · /e/b/1', text)
|
|
109
|
+
|
|
110
|
+
def test_tools_by_policy(self):
|
|
111
|
+
r = run(render(self.fe, PRINCIPAL, '/e/b/2'))
|
|
112
|
+
self.assertEqual([t.name for t in r.tools], ['rename'])
|
|
113
|
+
home = run(render(self.fe, PRINCIPAL, '/'))
|
|
114
|
+
self.assertEqual([t.name for t in home.tools], ['create_note'])
|
|
115
|
+
|
|
116
|
+
def test_view_named_home_renders_at_root(self):
|
|
117
|
+
@agent_view('home')
|
|
118
|
+
async def home(ctx):
|
|
119
|
+
return ['My workspace']
|
|
120
|
+
self.registry.add(view_of(home))
|
|
121
|
+
self.assertIn('My workspace', run(render(self.fe, PRINCIPAL, '/')).text)
|
|
122
|
+
|
|
123
|
+
def test_view_named_entity_replaces_the_fallback(self):
|
|
124
|
+
@agent_view('entity')
|
|
125
|
+
async def entity(ctx, entity):
|
|
126
|
+
return [f'Custom {entity.entity_id}']
|
|
127
|
+
self.registry.add(view_of(entity))
|
|
128
|
+
self.assertIn('Custom gc://b/1', run(render(self.fe, PRINCIPAL, '/e/b/1')).text)
|
|
129
|
+
|
|
130
|
+
def test_default_for_wins_over_the_view_named_entity(self):
|
|
131
|
+
@agent_view('notes.note', default_for=['t.Note'])
|
|
132
|
+
async def note(ctx, entity):
|
|
133
|
+
return ['A note']
|
|
134
|
+
self.registry.add(view_of(note))
|
|
135
|
+
self.assertIn('A note', run(render(self.fe, PRINCIPAL, '/e/b/2')).text)
|
|
136
|
+
self.assertIn('Hello · t.Note', run(render(self.fe, PRINCIPAL, '/e/b/1')).text)
|
|
137
|
+
|
|
138
|
+
def test_route_with_an_entity_and_no_view_renders_the_view_named_entity(self):
|
|
139
|
+
text = run(render(self.fe, PRINCIPAL, '/notes/b/1')).text
|
|
140
|
+
self.assertIn('- Hello · t.Note · /e/b/2', text)
|
|
141
|
+
|
|
142
|
+
def test_without_base_views_an_address_renders_a_placeholder(self):
|
|
143
|
+
fe = Frontend(self.client, AgentRegistry(), max_chars=5000)
|
|
144
|
+
self.assertIn('No agent view renders this address', run(render(fe, PRINCIPAL, '/e/b/1')).text)
|
|
145
|
+
|
|
146
|
+
def test_version_changes_with_child(self):
|
|
147
|
+
v1 = run(render(self.fe, PRINCIPAL, '/e/b/1')).version
|
|
148
|
+
self.client.add(FakeEntity('gc://b/3', 't.Note', parent_id='gc://b/1'))
|
|
149
|
+
self.assertNotEqual(v1, run(render(self.fe, PRINCIPAL, '/e/b/1')).version)
|
|
150
|
+
|
|
151
|
+
def test_unknown_route(self):
|
|
152
|
+
with self.assertRaises(AgentError):
|
|
153
|
+
run(render(self.fe, PRINCIPAL, '/nope'))
|
|
154
|
+
|
|
155
|
+
def test_missing_entity(self):
|
|
156
|
+
with self.assertRaises(AgentError) as cm:
|
|
157
|
+
run(render(self.fe, PRINCIPAL, '/e/b/99'))
|
|
158
|
+
self.assertEqual(cm.exception.code, 'NotFound')
|
|
159
|
+
|
|
160
|
+
def test_label_placement_at_unknown_route(self):
|
|
161
|
+
@agent_view('notes.index')
|
|
162
|
+
async def index(ctx):
|
|
163
|
+
return [ctx.place('/typo')]
|
|
164
|
+
self.registry.add(view_of(index))
|
|
165
|
+
with self.assertRaises(AgentError) as cm:
|
|
166
|
+
run(render(self.fe, PRINCIPAL, '/notes'))
|
|
167
|
+
self.assertEqual(cm.exception.code, 'NotFound')
|
|
168
|
+
|
|
169
|
+
def test_registered_view_and_full_placement(self):
|
|
170
|
+
@agent_view('notes.index')
|
|
171
|
+
async def index(ctx):
|
|
172
|
+
return ['Notes', ctx.place('gc://b/2', full=True)]
|
|
173
|
+
self.registry.add(view_of(index))
|
|
174
|
+
text = run(render(self.fe, PRINCIPAL, '/notes')).text
|
|
175
|
+
self.assertIn('### Hello · t.Note · /e/b/2', text)
|
|
176
|
+
|
|
177
|
+
def test_a_view_lists_what_is_granted_to_the_caller(self):
|
|
178
|
+
self.client.add(FakeEntity('gc://b/3', 't.Note', fields={'title': 'Shared'}))
|
|
179
|
+
self.client.granted = {'gc://b/3'}
|
|
180
|
+
|
|
181
|
+
@agent_view('notes.index')
|
|
182
|
+
async def index(ctx):
|
|
183
|
+
shared = await ctx.find('t.Note', granted_to_me=True)
|
|
184
|
+
return [ctx.place(e.entity_id) for e in shared]
|
|
185
|
+
self.registry.add(view_of(index))
|
|
186
|
+
text = run(render(self.fe, PRINCIPAL, '/notes')).text
|
|
187
|
+
self.assertIn('Shared · t.Note · /e/b/3', text)
|
|
188
|
+
self.assertNotIn('Hello', text)
|
|
189
|
+
|
|
190
|
+
def _tagged_notes(self, count):
|
|
191
|
+
for n in range(count):
|
|
192
|
+
self.client.add(FakeEntity(f'gc://q/{n:03}', 't.Note', fields={'tags': ['x'] if n % 2 else ['x', 'y']}))
|
|
193
|
+
|
|
194
|
+
def _query_view(self, **options):
|
|
195
|
+
@agent_view('notes.index')
|
|
196
|
+
async def index(ctx):
|
|
197
|
+
found = await ctx.query('t.Note', where=ContainsValue('tags', 'x'), **options)
|
|
198
|
+
return [f"{len(found)} more={ctx.more}"]
|
|
199
|
+
self.registry.add(view_of(index))
|
|
200
|
+
|
|
201
|
+
def test_query_reads_every_page_of_a_predicate(self):
|
|
202
|
+
self._tagged_notes(250)
|
|
203
|
+
self._query_view()
|
|
204
|
+
|
|
205
|
+
self.assertIn('250 more=False', run(render(self.fe, PRINCIPAL, '/notes')).text)
|
|
206
|
+
self.assertEqual(self.client.pages_listed, 3)
|
|
207
|
+
|
|
208
|
+
def test_query_stops_at_its_limit_or_the_cap_and_says_there_is_more(self):
|
|
209
|
+
self._tagged_notes(250)
|
|
210
|
+
self._query_view(limit=120)
|
|
211
|
+
self.assertIn('120 more=True', run(render(self.fe, PRINCIPAL, '/notes')).text)
|
|
212
|
+
|
|
213
|
+
self.registry = load_registry([])
|
|
214
|
+
self.fe = Frontend(self.client, self.registry, max_chars=5000, query_max=150)
|
|
215
|
+
self._query_view()
|
|
216
|
+
self.assertIn('150 more=True', run(render(self.fe, PRINCIPAL, '/notes')).text)
|
|
217
|
+
|
|
218
|
+
def test_query_versions_on_every_entity_it_read(self):
|
|
219
|
+
self._tagged_notes(250)
|
|
220
|
+
self._query_view()
|
|
221
|
+
before = run(render(self.fe, PRINCIPAL, '/notes')).version
|
|
222
|
+
|
|
223
|
+
self.client.entities['gc://q/249'].version += 1
|
|
224
|
+
|
|
225
|
+
self.assertNotEqual(run(render(self.fe, PRINCIPAL, '/notes')).version, before)
|
|
226
|
+
|
|
227
|
+
def test_placement_cycle(self):
|
|
228
|
+
@agent_view('notes.index')
|
|
229
|
+
async def index(ctx):
|
|
230
|
+
return [ctx.place('/notes', full=True)]
|
|
231
|
+
self.registry.add(view_of(index))
|
|
232
|
+
with self.assertRaises(RenderTooLarge):
|
|
233
|
+
run(render(self.fe, PRINCIPAL, '/notes'))
|
|
234
|
+
|
|
235
|
+
def test_too_large(self):
|
|
236
|
+
self.fe.max_chars = 50
|
|
237
|
+
with self.assertRaises(RenderTooLarge):
|
|
238
|
+
run(render(self.fe, PRINCIPAL, '/e/b/1'))
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
class ViewEntityTests(unittest.TestCase):
|
|
242
|
+
"""A view entity renders as the agent view its `component` names, over what it `renders`."""
|
|
243
|
+
|
|
244
|
+
def setUp(self):
|
|
245
|
+
self.client = FakeGraphClient()
|
|
246
|
+
self.client.by_type['graphcore.View'] = ['Rename']
|
|
247
|
+
self.note = self.client.add(FakeEntity('gc://b/2', 't.Note', fields={'title': 'Hello'}))
|
|
248
|
+
self.registry = load_registry([])
|
|
249
|
+
|
|
250
|
+
@operation('add_note', 'Add a note', {'type': 'object', 'properties': {}})
|
|
251
|
+
async def add_note(ctx):
|
|
252
|
+
return {}
|
|
253
|
+
|
|
254
|
+
@agent_view('demo.notes', tools=lambda ctx, entity: _async([add_note]))
|
|
255
|
+
async def notes(ctx):
|
|
256
|
+
return ['All the notes']
|
|
257
|
+
|
|
258
|
+
@agent_view('demo.note')
|
|
259
|
+
async def note(ctx, entity):
|
|
260
|
+
return [f"Note {entity.to_dict()['title']}"]
|
|
261
|
+
|
|
262
|
+
self.registry.add(view_of(notes))
|
|
263
|
+
self.registry.add(view_of(note))
|
|
264
|
+
self.fe = Frontend(self.client, self.registry, max_chars=5000)
|
|
265
|
+
|
|
266
|
+
def view(self, component, renders=None):
|
|
267
|
+
return self.client.add(FakeEntity('gc://b/9', 'graphcore.View', fields={
|
|
268
|
+
'component': component, 'renders': renders, 'client': 'web', 'state': {}}))
|
|
269
|
+
|
|
270
|
+
def test_component_without_an_entity(self):
|
|
271
|
+
self.view('demo.notes')
|
|
272
|
+
r = run(render(self.fe, PRINCIPAL, '/e/b/9'))
|
|
273
|
+
self.assertIn('View `demo.notes` of no entity · view entity gc://b/9\n\nAll the notes', r.text)
|
|
274
|
+
self.assertEqual([t.name for t in r.tools], ['add_note', 'rename'])
|
|
275
|
+
|
|
276
|
+
def test_component_reads_the_view_state(self):
|
|
277
|
+
@agent_view('demo.stateful')
|
|
278
|
+
async def stateful(ctx):
|
|
279
|
+
return [f"selected: {ctx.view_state.get('selected')}"]
|
|
280
|
+
self.registry.add(view_of(stateful))
|
|
281
|
+
self.client.add(FakeEntity('gc://b/9', 'graphcore.View', fields={
|
|
282
|
+
'component': 'demo.stateful', 'renders': None, 'state': {'selected': 'gc://b/2'}}))
|
|
283
|
+
self.assertIn('selected: gc://b/2', run(render(self.fe, PRINCIPAL, '/e/b/9')).text)
|
|
284
|
+
|
|
285
|
+
def test_component_over_the_entity_it_renders(self):
|
|
286
|
+
self.view('demo.note', renders='gc://b/2')
|
|
287
|
+
text = run(render(self.fe, PRINCIPAL, '/e/b/9')).text
|
|
288
|
+
self.assertIn('View `demo.note` of /e/b/2', text)
|
|
289
|
+
self.assertIn('Note Hello', text)
|
|
290
|
+
|
|
291
|
+
def test_version_follows_the_entity_it_renders(self):
|
|
292
|
+
self.view('demo.note', renders='gc://b/2')
|
|
293
|
+
before = run(render(self.fe, PRINCIPAL, '/e/b/9')).version
|
|
294
|
+
self.note.version += 1
|
|
295
|
+
self.assertNotEqual(before, run(render(self.fe, PRINCIPAL, '/e/b/9')).version)
|
|
296
|
+
|
|
297
|
+
def test_unknown_component_renders_as_an_entity(self):
|
|
298
|
+
self.view('composite_hbox')
|
|
299
|
+
text = run(render(self.fe, PRINCIPAL, '/e/b/9')).text
|
|
300
|
+
self.assertIn("No agent view is named 'composite_hbox'; showing the view as an entity.", text)
|
|
301
|
+
self.assertIn('component: composite_hbox', text)
|
|
302
|
+
|
|
303
|
+
def test_component_naming_the_view_view_renders_as_an_entity(self):
|
|
304
|
+
self.view('view')
|
|
305
|
+
self.assertIn("No agent view is named 'view'", run(render(self.fe, PRINCIPAL, '/e/b/9')).text)
|
|
306
|
+
|
|
307
|
+
def test_unreadable_target(self):
|
|
308
|
+
self.view('demo.note', renders='gc://b/404')
|
|
309
|
+
self.assertIn('This view renders gc://b/404, which does not exist or is not readable.',
|
|
310
|
+
run(render(self.fe, PRINCIPAL, '/e/b/9')).text)
|
|
311
|
+
|
|
312
|
+
def test_an_app_view_for_view_entities_replaces_the_base_one(self):
|
|
313
|
+
@agent_view('demo.view', default_for=['graphcore.View'])
|
|
314
|
+
async def custom(ctx, entity):
|
|
315
|
+
return ['Custom view rendering']
|
|
316
|
+
self.registry.add(view_of(custom))
|
|
317
|
+
self.view('demo.notes')
|
|
318
|
+
self.assertIn('Custom view rendering', run(render(self.fe, PRINCIPAL, '/e/b/9')).text)
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
async def _async(value):
|
|
322
|
+
return value
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
class ToolTests(unittest.TestCase):
|
|
326
|
+
def setUp(self):
|
|
327
|
+
self.client = FakeGraphClient()
|
|
328
|
+
self.client.add(FakeEntity('gc://b/2', 't.Note', fields={'title': 'Hello'}))
|
|
329
|
+
self.fe = Frontend(self.client, load_registry([]), max_chars=5000)
|
|
330
|
+
|
|
331
|
+
def test_call_runs_strategy_and_returns_new_version(self):
|
|
332
|
+
r = run(render(self.fe, PRINCIPAL, '/e/b/2'))
|
|
333
|
+
out = run(tools.call(self.fe, PRINCIPAL, '/e/b/2', 'rename', {'name': 'X'}, r.version))
|
|
334
|
+
self.assertIn('"ok":true', out)
|
|
335
|
+
self.assertIn(('Rename', 'gc://b/2', {'name': 'X'}), self.client.executed)
|
|
336
|
+
self.assertNotIn(r.version, out)
|
|
337
|
+
|
|
338
|
+
def test_stale(self):
|
|
339
|
+
with self.assertRaises(AgentError) as cm:
|
|
340
|
+
run(tools.call(self.fe, PRINCIPAL, '/e/b/2', 'rename', {'name': 'X'}, 'deadbeef'))
|
|
341
|
+
self.assertEqual(cm.exception.code, 'Stale')
|
|
342
|
+
self.assertEqual(self.client.executed, [])
|
|
343
|
+
|
|
344
|
+
def test_unknown_tool(self):
|
|
345
|
+
with self.assertRaises(AgentError) as cm:
|
|
346
|
+
run(tools.call(self.fe, PRINCIPAL, '/e/b/2', 'bogus', {}))
|
|
347
|
+
self.assertEqual(cm.exception.code, 'UnknownTool')
|
|
348
|
+
|
|
349
|
+
def test_invalid_params_include_schema(self):
|
|
350
|
+
with self.assertRaises(AgentError) as cm:
|
|
351
|
+
run(tools.call(self.fe, PRINCIPAL, '/e/b/2', 'rename', {}))
|
|
352
|
+
self.assertEqual(cm.exception.code, 'InvalidRequest')
|
|
353
|
+
self.assertIn('schema', str(cm.exception))
|
|
354
|
+
|
|
355
|
+
def test_operation_tool(self):
|
|
356
|
+
@operation('ping', 'Ping', {'type': 'object', 'properties': {}})
|
|
357
|
+
async def ping(ctx):
|
|
358
|
+
return {'pong': True}
|
|
359
|
+
|
|
360
|
+
async def view_tools(ctx, entity):
|
|
361
|
+
return [ping]
|
|
362
|
+
|
|
363
|
+
@agent_view('x.view', default_for=['t.Note'], tools=view_tools)
|
|
364
|
+
async def v(ctx, entity):
|
|
365
|
+
return []
|
|
366
|
+
reg = load_registry([])
|
|
367
|
+
reg.add(view_of(v))
|
|
368
|
+
fe = Frontend(self.client, reg)
|
|
369
|
+
out = run(tools.call(fe, PRINCIPAL, '/e/b/2', 'ping', {}))
|
|
370
|
+
self.assertIn('pong', out)
|
|
371
|
+
|
|
372
|
+
def test_a_tool_that_deletes_its_own_place_succeeds(self):
|
|
373
|
+
@operation('discard', 'Discard', {'type': 'object', 'properties': {}})
|
|
374
|
+
async def discard(ctx):
|
|
375
|
+
self.client.tombstones.add(ctx.entity.entity_id)
|
|
376
|
+
return {'deleted': True}
|
|
377
|
+
|
|
378
|
+
async def view_tools(ctx, entity):
|
|
379
|
+
return [discard]
|
|
380
|
+
|
|
381
|
+
@agent_view('x.discard', default_for=['t.Note'], tools=view_tools)
|
|
382
|
+
async def v(ctx, entity):
|
|
383
|
+
return []
|
|
384
|
+
reg = load_registry([])
|
|
385
|
+
reg.add(view_of(v))
|
|
386
|
+
fe = Frontend(self.client, reg)
|
|
387
|
+
out = json.loads(run(tools.call(fe, PRINCIPAL, '/e/b/2', 'discard', {})))
|
|
388
|
+
self.assertEqual(out['result'], {'deleted': True})
|
|
389
|
+
self.assertTrue(out['ok'])
|
|
390
|
+
self.assertIsNone(out['address'])
|
|
391
|
+
self.assertNotIn('version', out)
|
|
392
|
+
self.assertIn('/e/b/2 no longer exists', out['note'])
|
|
393
|
+
|
|
394
|
+
def test_target_is_hidden_unless_the_agent_supplies_it(self):
|
|
395
|
+
r = run(render(self.fe, PRINCIPAL, '/e/b/2'))
|
|
396
|
+
self.assertNotIn('target', r.tools[0].signature)
|
|
397
|
+
home = run(render(self.fe, PRINCIPAL, '/'))
|
|
398
|
+
self.assertNotIn('target', home.tools[0].signature)
|
|
399
|
+
|
|
400
|
+
def test_agent_supplied_target_is_a_required_parameter(self):
|
|
401
|
+
async def ask(ctx, entity):
|
|
402
|
+
return [strategy_tool('Rename', name='rename_any')]
|
|
403
|
+
|
|
404
|
+
@agent_view('x.other', default_for=['t.Note'], tools=ask)
|
|
405
|
+
async def v(ctx, entity):
|
|
406
|
+
return []
|
|
407
|
+
reg = load_registry([])
|
|
408
|
+
reg.add(view_of(v))
|
|
409
|
+
fe = Frontend(self.client, reg)
|
|
410
|
+
r = run(render(fe, PRINCIPAL, '/e/b/2'))
|
|
411
|
+
self.assertIn('target: entity', r.tools[0].signature)
|
|
412
|
+
with self.assertRaises(AgentError) as cm:
|
|
413
|
+
run(tools.call(fe, PRINCIPAL, '/e/b/2', 'rename_any', {'name': 'X'}))
|
|
414
|
+
self.assertEqual(cm.exception.code, 'InvalidRequest')
|
|
415
|
+
run(tools.call(fe, PRINCIPAL, '/e/b/2', 'rename_any', {'target': 'gc://b/2', 'name': 'X'}))
|
|
416
|
+
self.assertIn(('Rename', 'gc://b/2', {'name': 'X'}), self.client.executed)
|
|
417
|
+
|
|
418
|
+
def test_bound_target(self):
|
|
419
|
+
async def bound(ctx, entity):
|
|
420
|
+
return [strategy_tool('Rename', bind=entity.entity_id, name='rename_this')]
|
|
421
|
+
|
|
422
|
+
@agent_view('x.bound', default_for=['t.Note'], tools=bound)
|
|
423
|
+
async def v(ctx, entity):
|
|
424
|
+
return []
|
|
425
|
+
reg = load_registry([])
|
|
426
|
+
reg.add(view_of(v))
|
|
427
|
+
fe = Frontend(self.client, reg)
|
|
428
|
+
r = run(render(fe, PRINCIPAL, '/e/b/2'))
|
|
429
|
+
self.assertNotIn('target', r.tools[0].signature)
|
|
430
|
+
run(tools.call(fe, PRINCIPAL, '/e/b/2', 'rename_this', {'name': 'X'}))
|
|
431
|
+
self.assertIn(('Rename', 'gc://b/2', {'name': 'X'}), self.client.executed)
|
|
432
|
+
|
|
433
|
+
def test_find_short_type_and_predicate(self):
|
|
434
|
+
out = run(tools.find(self.fe, PRINCIPAL, 'Note', {'title': 'Hello'}))
|
|
435
|
+
self.assertIn('Hello · t.Note · /e/b/2', out)
|
|
436
|
+
with self.assertRaises(AgentError):
|
|
437
|
+
run(tools.find(self.fe, PRINCIPAL, 'Nope'))
|
|
438
|
+
|
|
439
|
+
def test_act(self):
|
|
440
|
+
out = run(tools.act(self.fe, PRINCIPAL, 'CreateNote', None, {'title': 'New'}))
|
|
441
|
+
self.assertIn('"ok":true', out)
|
|
442
|
+
with self.assertRaises(AgentError):
|
|
443
|
+
run(tools.act(self.fe, PRINCIPAL, 'Nope', None, {}))
|
|
444
|
+
|
|
445
|
+
def test_open_with_version(self):
|
|
446
|
+
r = run(render(self.fe, PRINCIPAL, '/e/b/2'))
|
|
447
|
+
self.assertTrue(run(tools.open_(self.fe, PRINCIPAL, '/e/b/2', r.version)).startswith('unchanged'))
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import importlib
|
|
2
|
+
import sys
|
|
3
|
+
import tempfile
|
|
4
|
+
import unittest
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from graphcore.agent_frontend.loading import load_registry
|
|
8
|
+
|
|
9
|
+
VIEW = "from graphcore.agent_frontend import agent_view\n\n@agent_view({name!r}{extra})\nasync def v(ctx):\n return []\n"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class LoadRegistryTests(unittest.TestCase):
|
|
13
|
+
def setUp(self):
|
|
14
|
+
self.tmp = tempfile.TemporaryDirectory()
|
|
15
|
+
self.apps = Path(self.tmp.name)
|
|
16
|
+
self.addCleanup(self.tmp.cleanup)
|
|
17
|
+
self.modules: list[str] = []
|
|
18
|
+
self.addCleanup(self._unload)
|
|
19
|
+
|
|
20
|
+
def _unload(self):
|
|
21
|
+
for module in self.modules:
|
|
22
|
+
for name in [m for m in sys.modules if m == module or m.startswith(module + '.')]:
|
|
23
|
+
del sys.modules[name]
|
|
24
|
+
sys.path[:] = [p for p in sys.path if not p.startswith(str(self.apps))]
|
|
25
|
+
|
|
26
|
+
def write_app(self, app: str, name: str, extra: str = '', module: str = 'agent_views') -> tuple[str, Path]:
|
|
27
|
+
"""An in-project agent frontend, importable as `<app>_agent_frontend`."""
|
|
28
|
+
root = self.apps / app / 'agent_frontend'
|
|
29
|
+
package = root / f'{app}_agent_frontend'
|
|
30
|
+
package.mkdir(parents=True)
|
|
31
|
+
(package / '__init__.py').write_text('')
|
|
32
|
+
(package / f'{module}.py').write_text(VIEW.format(name=name, extra=extra))
|
|
33
|
+
sys.path.insert(0, str(root))
|
|
34
|
+
importlib.invalidate_caches()
|
|
35
|
+
self.modules.append(package.name)
|
|
36
|
+
return package.name, package
|
|
37
|
+
|
|
38
|
+
def test_registers_views(self):
|
|
39
|
+
registry = load_registry([self.write_app('notes', 'notes.index')[0]])
|
|
40
|
+
self.assertEqual(registry.by_name('notes.index').name, 'notes.index')
|
|
41
|
+
|
|
42
|
+
def test_views_in_any_module_register_and_tests_are_skipped(self):
|
|
43
|
+
module, package = self.write_app('notes', 'notes.index', module='boards')
|
|
44
|
+
(package / 'tests').mkdir()
|
|
45
|
+
(package / 'tests' / '__init__.py').write_text('')
|
|
46
|
+
(package / 'tests' / 'test_x.py').write_text("raise RuntimeError('imported a test')")
|
|
47
|
+
registry = load_registry([module])
|
|
48
|
+
self.assertIsNotNone(registry.by_name('notes.index'))
|
|
49
|
+
|
|
50
|
+
def test_broken_module_stops_loading(self):
|
|
51
|
+
module, package = self.write_app('notes', 'notes.index')
|
|
52
|
+
(package / 'broken.py').write_text("raise RuntimeError('boom')")
|
|
53
|
+
with self.assertRaisesRegex(ImportError, 'broken'):
|
|
54
|
+
load_registry([module])
|
|
55
|
+
|
|
56
|
+
def test_same_view_name_in_two_apps_fails(self):
|
|
57
|
+
frontends = [self.write_app('a', 'shared.view')[0], self.write_app('b', 'shared.view')[0]]
|
|
58
|
+
with self.assertRaisesRegex(ValueError, 'shared.view'):
|
|
59
|
+
load_registry(frontends)
|
|
60
|
+
|
|
61
|
+
def test_two_apps_default_for_the_same_type_name_both_views(self):
|
|
62
|
+
frontends = [
|
|
63
|
+
self.write_app('a', 'a.view', ", default_for=['t.Note']")[0],
|
|
64
|
+
self.write_app('b', 'b.view', ", default_for=['t.Note']")[0],
|
|
65
|
+
]
|
|
66
|
+
with self.assertRaisesRegex(ValueError, "'a.view' and 'b.view'"):
|
|
67
|
+
load_registry(frontends)
|
|
68
|
+
|
|
69
|
+
def test_base_views_load_into_every_registry(self):
|
|
70
|
+
for _ in range(2):
|
|
71
|
+
registry = load_registry([])
|
|
72
|
+
self.assertEqual(registry.by_name('home').name, 'home')
|
|
73
|
+
self.assertEqual(registry.by_name('entity').name, 'entity')
|
|
74
|
+
|
|
75
|
+
def test_app_view_named_entity_replaces_the_base_view(self):
|
|
76
|
+
registry = load_registry([self.write_app('notes', 'entity')[0]])
|
|
77
|
+
self.assertEqual(registry.by_name('entity').fn.__module__, 'notes_agent_frontend.agent_views')
|
|
78
|
+
|
|
79
|
+
def test_a_base_view_an_app_imports_is_not_the_apps(self):
|
|
80
|
+
module, package = self.write_app('notes', 'notes.index')
|
|
81
|
+
(package / 'reuse.py').write_text('from graphcore.agent_frontend.base import entity_view\n')
|
|
82
|
+
registry = load_registry([module])
|
|
83
|
+
self.assertEqual(registry.by_name('entity').fn.__module__, 'graphcore.agent_frontend.base.views')
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
from contextlib import asynccontextmanager
|
|
2
|
+
|
|
3
|
+
import httpx2
|
|
4
|
+
from django.test import SimpleTestCase
|
|
5
|
+
|
|
6
|
+
from graphcore.agent_frontend.registry import AgentRegistry
|
|
7
|
+
from graphcore.agent_frontend.server import create_app
|
|
8
|
+
from graphcore.agent_frontend.tests.fakes import FakeEntity, FakeGraphClient
|
|
9
|
+
|
|
10
|
+
HEADERS = {'Accept': 'application/json, text/event-stream', 'Content-Type': 'application/json'}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class McpServerTests(SimpleTestCase):
|
|
14
|
+
@asynccontextmanager
|
|
15
|
+
async def serving(self, allowed_origins=(), **kwargs):
|
|
16
|
+
self.graph = FakeGraphClient()
|
|
17
|
+
self.graph.add(FakeEntity('gc://b/1', 't.Note', fields={'title': 'Hello'}))
|
|
18
|
+
app = create_app(self.graph, AgentRegistry(), allowed_origins=allowed_origins, **kwargs)
|
|
19
|
+
self._id = 0
|
|
20
|
+
# The session manager's task group must enter and exit in one task, so no asyncSetUp.
|
|
21
|
+
async with app.mcp_app.router.lifespan_context(app.mcp_app):
|
|
22
|
+
async with httpx2.AsyncClient(transport=httpx2.ASGITransport(app=app), base_url='http://localhost') as http:
|
|
23
|
+
self.http = http
|
|
24
|
+
yield
|
|
25
|
+
|
|
26
|
+
async def rpc(self, method, params=None, token='good'):
|
|
27
|
+
self._id += 1
|
|
28
|
+
headers = dict(HEADERS)
|
|
29
|
+
if token:
|
|
30
|
+
headers['Authorization'] = f'Bearer {token}'
|
|
31
|
+
return await self.http.post(
|
|
32
|
+
'/mcp', headers=headers,
|
|
33
|
+
json={'jsonrpc': '2.0', 'id': self._id, 'method': method, 'params': params or {}},
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
async def call_tool(self, name, arguments):
|
|
37
|
+
response = await self.rpc('tools/call', {'name': name, 'arguments': arguments})
|
|
38
|
+
self.assertEqual(response.status_code, 200, response.text)
|
|
39
|
+
return response.json()['result']
|
|
40
|
+
|
|
41
|
+
async def test_rejects_missing_and_bad_tokens(self):
|
|
42
|
+
async with self.serving():
|
|
43
|
+
self.assertEqual((await self.rpc('tools/list', token=None)).status_code, 401)
|
|
44
|
+
response = await self.rpc('tools/list', token='bad')
|
|
45
|
+
self.assertEqual(response.status_code, 401)
|
|
46
|
+
self.assertEqual(response.headers['www-authenticate'], 'Bearer')
|
|
47
|
+
|
|
48
|
+
async def test_401_challenge_is_built_from_the_request(self):
|
|
49
|
+
async with self.serving(challenge=lambda scope: f'Bearer resource_metadata="x{scope["path"]}"'):
|
|
50
|
+
response = await self.rpc('tools/list', token=None)
|
|
51
|
+
self.assertEqual(response.headers['www-authenticate'], 'Bearer resource_metadata="x/mcp"')
|
|
52
|
+
|
|
53
|
+
async def test_initialize_and_list_tools(self):
|
|
54
|
+
async with self.serving():
|
|
55
|
+
init = await self.rpc('initialize', {
|
|
56
|
+
'protocolVersion': '2025-06-18', 'capabilities': {},
|
|
57
|
+
'clientInfo': {'name': 't', 'version': '0'},
|
|
58
|
+
})
|
|
59
|
+
self.assertEqual(init.status_code, 200, init.text)
|
|
60
|
+
self.assertEqual(init.json()['result']['serverInfo']['name'], 'graphcore')
|
|
61
|
+
tools = (await self.rpc('tools/list')).json()['result']['tools']
|
|
62
|
+
self.assertEqual({t['name'] for t in tools}, {'open', 'find', 'call', 'act'})
|
|
63
|
+
|
|
64
|
+
async def test_open_root(self):
|
|
65
|
+
async with self.serving():
|
|
66
|
+
result = await self.call_tool('open', {'at': '/'})
|
|
67
|
+
self.assertFalse(result.get('isError'))
|
|
68
|
+
self.assertIn('version', result['content'][0]['text'])
|
|
69
|
+
|
|
70
|
+
async def test_find_lists_entities(self):
|
|
71
|
+
async with self.serving():
|
|
72
|
+
result = await self.call_tool('find', {'type': 't.Note'})
|
|
73
|
+
self.assertIn('Hello', result['content'][0]['text'])
|
|
74
|
+
|
|
75
|
+
async def test_stale_write_is_refused(self):
|
|
76
|
+
async with self.serving():
|
|
77
|
+
result = await self.call_tool('call', {
|
|
78
|
+
'at': '/e/b/1', 'tool': 'Rename', 'params': {'name': 'x'}, 'seen_version': 'stale',
|
|
79
|
+
})
|
|
80
|
+
self.assertTrue(result['isError'])
|
|
81
|
+
self.assertIn('stale', result['content'][0]['text'].lower())
|
|
82
|
+
self.assertEqual(self.graph.executed, [])
|
|
83
|
+
|
|
84
|
+
async def test_answers_preflight_from_an_allowed_origin_without_a_token(self):
|
|
85
|
+
async with self.serving(allowed_origins=['http://localhost:3000']):
|
|
86
|
+
response = await self.http.options('/mcp', headers={
|
|
87
|
+
'Origin': 'http://localhost:3000',
|
|
88
|
+
'Access-Control-Request-Method': 'POST',
|
|
89
|
+
'Access-Control-Request-Headers': 'authorization,content-type',
|
|
90
|
+
})
|
|
91
|
+
self.assertEqual(response.status_code, 200, response.text)
|
|
92
|
+
self.assertEqual(response.headers['access-control-allow-origin'], 'http://localhost:3000')
|
|
93
|
+
|
|
94
|
+
async def test_cross_origin_call_carries_allow_origin(self):
|
|
95
|
+
async with self.serving(allowed_origins=['http://localhost:3000']):
|
|
96
|
+
self._id += 1
|
|
97
|
+
response = await self.http.post('/mcp', headers={
|
|
98
|
+
**HEADERS, 'Authorization': 'Bearer good', 'Origin': 'http://localhost:3000',
|
|
99
|
+
}, json={'jsonrpc': '2.0', 'id': self._id, 'method': 'tools/call',
|
|
100
|
+
'params': {'name': 'open', 'arguments': {'at': '/'}}})
|
|
101
|
+
self.assertEqual(response.status_code, 200, response.text)
|
|
102
|
+
self.assertEqual(response.headers['access-control-allow-origin'], 'http://localhost:3000')
|
|
103
|
+
|
|
104
|
+
async def test_other_origins_get_no_allow_origin(self):
|
|
105
|
+
async with self.serving(allowed_origins=['http://localhost:3000']):
|
|
106
|
+
response = await self.http.options('/mcp', headers={
|
|
107
|
+
'Origin': 'http://evil.example', 'Access-Control-Request-Method': 'POST',
|
|
108
|
+
})
|
|
109
|
+
self.assertNotIn('access-control-allow-origin', response.headers)
|