From bc22089e9dc9fcefe6cc1d26d56a2a0c677bbd68 Mon Sep 17 00:00:00 2001 From: jasper-yl Date: Tue, 17 Nov 2026 23:14:50 -0800 Subject: [PATCH 1/2] feat(scopes): leave plugin-created Scopes out of Scope listings Co-Authored-By: Claude --- .../src-tauri/src/transport/operations.json | 2 +- desktop/ui/src/generated/api.d.ts | 2 +- desktop/ui/src/generated/operations.ts | 2 +- docs/en/docs/develop/plugins.md | 4 +++ docs/en/rfcs/2579-plugin-reload-and-apis.md | 3 ++- docs/zh/docs/develop/plugins.md | 3 +++ docs/zh/rfcs/2579-plugin-reload-and-apis.md | 3 ++- openapi/powercontext.yaml | 4 ++- src/powercontext/builtin/plugins/host.py | 3 ++- src/powercontext/builtin/scope/application.py | 5 +++- src/powercontext/builtin/scope/models.py | 6 +++++ src/powercontext/builtin/scope/repository.py | 25 ++++++++++++++++--- src/powercontext/http/_generated/schema.py | 8 ++++-- src/powercontext/server/app.py | 8 +++++- tests/e2e/test_plugin_access.py | 11 ++++++++ 15 files changed, 75 insertions(+), 14 deletions(-) diff --git a/desktop/src-tauri/src/transport/operations.json b/desktop/src-tauri/src/transport/operations.json index 7c195d19ad..9edfcaf2a8 100644 --- a/desktop/src-tauri/src/transport/operations.json +++ b/desktop/src-tauri/src/transport/operations.json @@ -1,5 +1,5 @@ { - "contractSha256": "d133a4c382a734d1e6fd194b1dbf4d90f7ffabc37fe335fc27d93f59fa4e636f", + "contractSha256": "eaa14b1581221ea89706140c5181c9ca701d685caaa9ebcbfdbcf5758db3d713", "operations": { "get_liveness": { "method": "GET", diff --git a/desktop/ui/src/generated/api.d.ts b/desktop/ui/src/generated/api.d.ts index 435c578cb6..c876d2b33c 100644 --- a/desktop/ui/src/generated/api.d.ts +++ b/desktop/ui/src/generated/api.d.ts @@ -287,7 +287,7 @@ export interface paths { }; /** * List observable Scopes - * @description Inspect available Scopes for an explicit navigation or organization request. Do not enumerate Scopes to bypass the current session binding or search another work context implicitly. Reuse the host-selected Scope for ordinary data-plane operations. Discover Scope descriptors with an explicit literal-substring field and exact relationship filters. Query matches the selected original field using the database's native substring operation. Regular expressions and wildcard syntax are not supported. Case, accent, and full-width/half-width matching follow the underlying database collation; results need not be identical across backends. Requests without query parameters preserve the existing complete-list behavior. + * @description Inspect available Scopes for an explicit navigation or organization request. Do not enumerate Scopes to bypass the current session binding or search another work context implicitly. Reuse the host-selected Scope for ordinary data-plane operations. Discover Scope descriptors with an explicit literal-substring field and exact relationship filters. Query matches the selected original field using the database's native substring operation. Regular expressions and wildcard syntax are not supported. Case, accent, and full-width/half-width matching follow the underlying database collation; results need not be identical across backends. Requests without query parameters return the complete list in one response. Scopes created by a plugin (external reference kind `powercontext.plugin`) are used through that plugin and are left out of every listing, unless `external_reference_kind` is `powercontext.plugin`. */ get: operations["list_scopes"]; put?: never; diff --git a/desktop/ui/src/generated/operations.ts b/desktop/ui/src/generated/operations.ts index 6b843ff2e5..d00ae0137d 100644 --- a/desktop/ui/src/generated/operations.ts +++ b/desktop/ui/src/generated/operations.ts @@ -15,7 +15,7 @@ */ // Generated from openapi/powercontext.yaml. Do not edit. -export const contractSha256 = "d133a4c382a734d1e6fd194b1dbf4d90f7ffabc37fe335fc27d93f59fa4e636f"; +export const contractSha256 = "eaa14b1581221ea89706140c5181c9ca701d685caaa9ebcbfdbcf5758db3d713"; export const operations = { "get_liveness": { "method": "GET", diff --git a/docs/en/docs/develop/plugins.md b/docs/en/docs/develop/plugins.md index 79b89bbce9..ad4533e0c6 100644 --- a/docs/en/docs/develop/plugins.md +++ b/docs/en/docs/develop/plugins.md @@ -390,6 +390,10 @@ plugin = ArtifactPlugin( `ctx.scopes.update(scope_id, expected_version=..., title=..., summary=...)` changes a Scope this plugin created and also needs `server.admin`; the API returns 404 when the Scope does not exist and 403 when another plugin or user created it. +- Scopes a plugin creates are used through the plugin: the `GET /v1/scopes` listing leaves them out, so the Dashboard's + Scope selector, the desktop app, and an agent's `list_scopes` do not show them, also after the plugin is removed. A + request with `external_reference_kind=powercontext.plugin` lists exactly these Scopes. Only listings change: reading a + Scope by ID, access to its data, and authorization stay as they are. - `request.body` is the validated `request_model` instance, `bytes`, or `None`, according to `body`; `request.path_params`, `request.query`, and `request.caller` are the path parameters, query parameters, and caller. - Return a Pydantic model or a dict for JSON, `BinaryResponse(content=..., content_type=..., filename=...)` for a file, diff --git a/docs/en/rfcs/2579-plugin-reload-and-apis.md b/docs/en/rfcs/2579-plugin-reload-and-apis.md index a41d6184eb..4be8edbd66 100644 --- a/docs/en/rfcs/2579-plugin-reload-and-apis.md +++ b/docs/en/rfcs/2579-plugin-reload-and-apis.md @@ -511,7 +511,8 @@ Scopes created by plugins: - At creation, core adds the external reference `{"kind": "powercontext.plugin", "value": ""}` to the Scope; `ctx.scopes.list()` and `ctx.scopes.update()` use it to decide ownership, and deployers can see which plugin created a - Scope. + Scope. The `GET /v1/scopes` listing leaves out Scopes with a reference of this kind; a request with + `external_reference_kind=powercontext.plugin` lists them. - The permission to create Scopes is not relaxed for plugins. Letting non-administrators create Scopes (for example, child Scopes under a parent) requires changing core's authorization model and is out of scope for this proposal. - A plugin's business configuration (for example, a knowledge base's chunking strategy) is not written into the Scope; diff --git a/docs/zh/docs/develop/plugins.md b/docs/zh/docs/develop/plugins.md index d4bcc37840..609f226b7c 100644 --- a/docs/zh/docs/develop/plugins.md +++ b/docs/zh/docs/develop/plugins.md @@ -360,6 +360,9 @@ plugin = ArtifactPlugin( - 需要创建 Scope 时用 `ctx.scopes.create(title=..., summary=..., idempotency_key=...)`,要求调用者具备 `server.admin`。 `ctx.scopes.list()` 返回本插件创建、且调用者可读的 Scope。`ctx.scopes.update(scope_id, expected_version=..., title=..., summary=...)` 修改本插件创建的 Scope,同样要求 `server.admin`;Scope 不存在时接口返回 404,不是本插件创建的返回 403。 +- 插件创建的 Scope 只通过插件使用:`GET /v1/scopes` 的列表不包含它们,所以 Dashboard 的 Scope 选择器、桌面端和 Agent 的 + `list_scopes` 都看不到,插件卸载后也一样。带上 `external_reference_kind=powercontext.plugin` 时列出的就是这些 Scope。这只 + 影响列表:按 ID 读取 Scope、访问其中的数据和权限判定都不变。 - `request.body` 按 `body` 声明是校验后的 `request_model` 实例、`bytes` 或 `None`;`request.path_params`、`request.query`、 `request.caller` 分别是路径参数、查询参数和调用者。 - 返回值:Pydantic 模型或 dict 返回 JSON;`BinaryResponse(content=..., content_type=..., filename=...)` 返回文件; diff --git a/docs/zh/rfcs/2579-plugin-reload-and-apis.md b/docs/zh/rfcs/2579-plugin-reload-and-apis.md index 9dcb5bfe29..1cfb7affda 100644 --- a/docs/zh/rfcs/2579-plugin-reload-and-apis.md +++ b/docs/zh/rfcs/2579-plugin-reload-and-apis.md @@ -430,7 +430,8 @@ RFC #2549 的上下文(专属存储、模型、日志),自定义接口调 插件创建的 Scope: - core 在创建时给 Scope 加上外部引用 `{"kind": "powercontext.plugin", "value": "<插件名>"}`,`ctx.scopes.list()` 和 - `ctx.scopes.update()` 据此判断归属,部署者也能看出 Scope 由哪个插件创建。 + `ctx.scopes.update()` 据此判断归属,部署者也能看出 Scope 由哪个插件创建。`GET /v1/scopes` 的列表不包含带有这类引用的 + Scope,请求带上 `external_reference_kind=powercontext.plugin` 时才列出它们。 - 创建 Scope 的权限要求不因插件而放宽。允许非管理员创建 Scope(例如在父 Scope 下创建子 Scope)需要修改 core 的授权 模型,不在本提案范围内。 - 插件的业务配置(例如知识库的分块策略)不写入 Scope,由插件以 `scope_id` 为键存进自己的存储。先创建 Scope,再写配置; diff --git a/openapi/powercontext.yaml b/openapi/powercontext.yaml index cc1a218814..f1c9cd8458 100644 --- a/openapi/powercontext.yaml +++ b/openapi/powercontext.yaml @@ -617,7 +617,9 @@ paths: Query matches the selected original field using the database's native substring operation. Regular expressions and wildcard syntax are not supported. Case, accent, and full-width/half-width matching follow the underlying database collation; results need not be identical across backends. - Requests without query parameters preserve the existing complete-list behavior. + Requests without query parameters return the complete list in one response. + Scopes created by a plugin (external reference kind `powercontext.plugin`) are used through that plugin and + are left out of every listing, unless `external_reference_kind` is `powercontext.plugin`. operationId: list_scopes x-powercontext-feature-contracts: [scope.selection] x-powercontext-access: {action: server.observe, resource: {type: server}} diff --git a/src/powercontext/builtin/plugins/host.py b/src/powercontext/builtin/plugins/host.py index cfb2e2b929..9405837ebd 100644 --- a/src/powercontext/builtin/plugins/host.py +++ b/src/powercontext/builtin/plugins/host.py @@ -59,6 +59,7 @@ from powercontext.builtin.plugins.writer import PluginArtifactWriter, PluginContractError, PluginWriteError, UserChange from powercontext.builtin.records import ArtifactRevisionPreconditionError, BaseValueConflictError from powercontext.builtin.runtime.artifact_merge import ArtifactMergeAccess +from powercontext.builtin.scope.models import PLUGIN_SCOPE_REFERENCE_KIND from powercontext.builtin.sources import BUILTIN_SOURCE_REGISTRY from powercontext.errors import RevisionConflictError from powercontext.plugins import ( @@ -738,7 +739,7 @@ async def submit(self, scope_id: str, definition: str, value: BaseModel, /) -> S class _Scopes: - _KIND = "powercontext.plugin" + _KIND = PLUGIN_SCOPE_REFERENCE_KIND def __init__(self, host: _Host) -> None: self._host = host diff --git a/src/powercontext/builtin/scope/application.py b/src/powercontext/builtin/scope/application.py index c21a6d760c..386248751a 100644 --- a/src/powercontext/builtin/scope/application.py +++ b/src/powercontext/builtin/scope/application.py @@ -140,13 +140,16 @@ async def list( *, scope_ids: Sequence[str] | None = None, external_reference: ScopeExternalReference | None = None, + exclude_external_reference_kind: str | None = None, ) -> tuple[ScopeDescriptor, ...]: async with self._database.transaction() as connection: if scope_ids is not None: return await self._repository.get_many(connection, tuple(scope_ids)) if external_reference is not None: return await self._repository.with_external_reference(connection, external_reference) - return await self._repository.list(connection) + return await self._repository.list( + connection, exclude_external_reference_kind=exclude_external_reference_kind + ) async def navigation(self, scope_ids): """Resolve ancestor names without discovering unrelated Scopes or their contents.""" diff --git a/src/powercontext/builtin/scope/models.py b/src/powercontext/builtin/scope/models.py index 8d04cd217d..bee8951a30 100644 --- a/src/powercontext/builtin/scope/models.py +++ b/src/powercontext/builtin/scope/models.py @@ -196,11 +196,17 @@ def validate_scope_id(cls, value: str) -> str: ] +# Scopes a plugin creates carry this external reference kind, with the plugin's name as the value. +PLUGIN_SCOPE_REFERENCE_KIND = "powercontext.plugin" + + class ScopeDiscovery(_ScopeValue): query: str | None = Field(default=None, max_length=256) query_field: ScopeQueryField | None = None parent_scope_id: str | None = None external_reference_kind: str | None = None + # Scopes with a reference of this kind are left out; not part of the cursor's query identity. + exclude_external_reference_kind: str | None = None binding_integration: str | None = None binding_kind: str | None = None limit: StrictInt = Field(default=50, ge=1, le=100) diff --git a/src/powercontext/builtin/scope/repository.py b/src/powercontext/builtin/scope/repository.py index 53a66e43e4..53231239bf 100644 --- a/src/powercontext/builtin/scope/repository.py +++ b/src/powercontext/builtin/scope/repository.py @@ -20,7 +20,7 @@ from functools import lru_cache from hashlib import sha256 -from sqlalchemy import bindparam, delete, false, insert, literal, null, select, union_all, update +from sqlalchemy import bindparam, delete, false, insert, literal, null, select, true, union_all, update from sqlalchemy.ext.asyncio import AsyncConnection from powercontext.builtin.persistence.tables import ( @@ -45,6 +45,20 @@ _DEFAULT_SETTING = "default" +def _without_reference_kind(kind: str | None): + """Scopes that carry no external reference of this kind; every Scope when no kind is given.""" + + if kind is None: + return true() + external = SCOPE_EXTERNAL_REFERENCES_TABLE.alias("scope_excluded_external_reference") + return ~( + select(1) + .where(external.c.scope_id == SCOPES_TABLE.c.scope_id, external.c.kind == kind) + .correlate(SCOPES_TABLE) + .exists() + ) + + def _scope_projection(statement): """Read Scope metadata and its relationships in one ordered result.""" metadata = ("scope_id", "title", "summary", "parent_scope_id", "version") @@ -115,8 +129,11 @@ async def navigation(self, connection, scope_ids): ).mappings() return tuple(dict(row) for row in rows) - async def list(self, connection: AsyncConnection, /) -> tuple[ScopeDescriptor, ...]: - return await self._load(connection, _scope_projection(select(SCOPES_TABLE).order_by(SCOPES_TABLE.c.scope_id))) + async def list( + self, connection: AsyncConnection, /, *, exclude_external_reference_kind: str | None = None + ) -> tuple[ScopeDescriptor, ...]: + statement = select(SCOPES_TABLE).where(_without_reference_kind(exclude_external_reference_kind)) + return await self._load(connection, _scope_projection(statement.order_by(SCOPES_TABLE.c.scope_id))) async def with_external_reference( self, connection: AsyncConnection, reference: ScopeExternalReference, / @@ -174,6 +191,8 @@ async def discover( if len(external_conditions) > 1: statement = statement.where(select(1).where(*external_conditions).correlate(SCOPES_TABLE).exists()) + statement = statement.where(_without_reference_kind(discovery.exclude_external_reference_kind)) + binding = SCOPE_BINDINGS_TABLE.alias("scope_discovery_binding") binding_conditions = [binding.c.scope_id == SCOPES_TABLE.c.scope_id] if discovery.binding_integration is not None: diff --git a/src/powercontext/http/_generated/schema.py b/src/powercontext/http/_generated/schema.py index caf1c13a97..0e14832356 100644 --- a/src/powercontext/http/_generated/schema.py +++ b/src/powercontext/http/_generated/schema.py @@ -536,8 +536,12 @@ "full-width/half-width matching follow the " "underlying database collation; results need not " "be identical across backends. Requests without " - "query parameters preserve the existing " - "complete-list behavior.", + "query parameters return the complete list in one " + "response. Scopes created by a plugin (external " + "reference kind `powercontext.plugin`) are used " + "through that plugin and are left out of every " + "listing, unless `external_reference_kind` is " + "`powercontext.plugin`.", "operationId": "list_scopes", "x-powercontext-feature-contracts": ["scope.selection"], "x-powercontext-access": {"action": "server.observe", "resource": {"type": "server"}}, diff --git a/src/powercontext/server/app.py b/src/powercontext/server/app.py index e6022b85d7..586f67957e 100644 --- a/src/powercontext/server/app.py +++ b/src/powercontext/server/app.py @@ -421,6 +421,7 @@ from powercontext.builtin.scope import ( ScopeSelection as DomainScopeSelection, ) +from powercontext.builtin.scope.models import PLUGIN_SCOPE_REFERENCE_KIND from powercontext.builtin.source_eligibility import SourceNotEligibleError from powercontext.builtin.sources import ( ObservedInvocation, @@ -2610,8 +2611,12 @@ async def list_scopes( ) or "limit" in http_request.query_params ) + # A plugin's Scopes hold what the plugin manages and are reached through the plugin. Listings leave + # them out unless the request asks for that reference kind. + hidden = None if request.external_reference_kind == PLUGIN_SCOPE_REFERENCE_KIND else PLUGIN_SCOPE_REFERENCE_KIND if not pagination_requested: - return ScopePage(items=[_scope_descriptor_response(scope) for scope in await scopes.list()]) + listed = await scopes.list(exclude_external_reference_kind=hidden) + return ScopePage(items=[_scope_descriptor_response(scope) for scope in listed]) principal = current_principal() caller = "anonymous" if principal is None else f"{principal.type}:{principal.id}" @@ -2621,6 +2626,7 @@ async def list_scopes( query_field=None if request.query_field is None else request.query_field.value, parent_scope_id=request.parent_scope_id, external_reference_kind=request.external_reference_kind, + exclude_external_reference_kind=hidden, binding_integration=request.binding_integration, binding_kind=request.binding_kind, limit=request.limit, diff --git a/tests/e2e/test_plugin_access.py b/tests/e2e/test_plugin_access.py index 77e6e9bcbd..7065612a36 100644 --- a/tests/e2e/test_plugin_access.py +++ b/tests/e2e/test_plugin_access.py @@ -315,4 +315,15 @@ async def scenario(): assert renamed.json()["title"] == "renamed" assert west["scope_id"] not in listed.json()["scope_ids"] + # Scope listings leave a plugin's Scopes out, as one complete list and as pages with any filter. + rooms = {east["scope_id"], west["scope_id"]} + for query in ("", "?limit=100", "?query=e&query_field=title"): + page = await admin.get(f"/v1/scopes{query}") + assert page.status_code == 200, page.text + assert not rooms & {item["scope_id"] for item in page.json()["items"]} + assert other in {item["scope_id"] for item in (await admin.get("/v1/scopes")).json()["items"]} + # Asking for the plugin reference kind lists exactly them. + asked = await admin.get("/v1/scopes?external_reference_kind=powercontext.plugin") + assert {item["scope_id"] for item in asked.json()["items"]} == rooms + asyncio.run(scenario()) From 549f7ae8ab80b8f6594c6ea9e490705a0adcbd3b Mon Sep 17 00:00:00 2001 From: jasper-yl Date: Wed, 18 Nov 2026 00:59:49 -0800 Subject: [PATCH 2/2] test(desktop): qualify the plugin Scope listing contract Co-Authored-By: Claude --- desktop/VALIDATION.md | 31 +++++++++++++++++++ .../src/connections/compatibility.json | 22 +++++++++++++ 2 files changed, 53 insertions(+) diff --git a/desktop/VALIDATION.md b/desktop/VALIDATION.md index 81a0b0b93d..30be326ef5 100644 --- a/desktop/VALIDATION.md +++ b/desktop/VALIDATION.md @@ -3586,3 +3586,34 @@ uv run --no-sync python desktop/tests/real_server.py \ --product-qualification sqlite-source-plugins-9f113a72-v1 \ --wheel desktop/.artifacts/source-plugins/powercontext-1.5.2.dev248+g9f113a729-py3-none-any.whl ``` + + +## Server bc22089e plugin Scope listing protocol qualification + +`sqlite-plugin-scope-listing-bc22089e-v1` qualifies all thirteen Desktop protocol operations against +Server source `bc22089e9dc9fcefe6cc1d26d56a2a0c677bbd68`, which leaves plugin-created Scopes out of Scope listings and states +that rule in the `list_scopes` description. + +- Normalized OpenAPI SHA-256: `eaa14b1581221ea89706140c5181c9ca701d685caaa9ebcbfdbcf5758db3d713`. +- Wheel: `powercontext-1.5.2.dev238+gbc22089e9-py3-none-any.whl`. +- Wheel SHA-256: `d7c7644cb32bc54bba8e9a2c5dd64d88d7db58d8c46a96008bbe4c4134b43fde`. +- Native debug probe SHA-256 (built without debug info): `0feb207b3fdb9be2293c67f6a0cdd4d01ea51c6685f71b20ac6af6c4fa2770f2`. + +On 2026-11-18, Linux 7.0 x86_64 (Ubuntu 24.04, glibc 2.39) and Python 3.12.3 passed anonymous loopback, +static Bearer, HTTPS with explicit CA/base path, and enforced Provider modes of the unchanged +`desktop/tests/real_server.py` through the ordinary product ConnectionManager. The probe was built with +the product Cargo lock without `ci-fixtures` and selected this bundled qualification; the client and +qualified contract digests are identical. `bundled_product_qualification_covers_every_desktop_operation` +passed against the same records. The wheel and report are retained under ignored +`desktop/.artifacts/plugin-scope-listing/` and `desktop/.artifacts/real-server.json`. +This qualifies the observed local SQLite protocol; it does not establish installed WebView, native +credential persistence, other operating systems, native database upgrades or release acceptance. + +Reproduce against the recorded artifact: + +```sh +cargo build --locked --manifest-path desktop/src-tauri/Cargo.toml --example server_probe +uv run --no-sync python desktop/tests/real_server.py \ + --product-qualification sqlite-plugin-scope-listing-bc22089e-v1 \ + --wheel desktop/.artifacts/plugin-scope-listing/powercontext-1.5.2.dev238+gbc22089e9-py3-none-any.whl +``` diff --git a/desktop/src-tauri/src/connections/compatibility.json b/desktop/src-tauri/src/connections/compatibility.json index 464661d2f7..12a2ba8156 100644 --- a/desktop/src-tauri/src/connections/compatibility.json +++ b/desktop/src-tauri/src/connections/compatibility.json @@ -1772,5 +1772,27 @@ "search_memory" ], "evidence": "desktop/VALIDATION.md#server-9f113a72-source-plugins-protocol-qualification" + }, + { + "id": "sqlite-plugin-scope-listing-bc22089e-v1", + "serverCommit": "bc22089e9dc9fcefe6cc1d26d56a2a0c677bbd68", + "contractSha256": "eaa14b1581221ea89706140c5181c9ca701d685caaa9ebcbfdbcf5758db3d713", + "artifactSha256": "d7c7644cb32bc54bba8e9a2c5dd64d88d7db58d8c46a96008bbe4c4134b43fde", + "operations": [ + "get_access_principal", + "get_artifact_revision", + "get_capabilities", + "get_connection_identity", + "get_default_scope", + "get_liveness", + "get_readiness", + "get_scope", + "get_server_info", + "get_stats", + "list_scopes", + "remember_memory", + "search_memory" + ], + "evidence": "desktop/VALIDATION.md#server-bc22089e-plugin-scope-listing-protocol-qualification" } ]