From cfeb70e25dbccef8910938a69e545b64f08e00a0 Mon Sep 17 00:00:00 2001 From: Bogdan Szabo Date: Mon, 19 Oct 2026 22:30:50 +0200 Subject: [PATCH] A token reads a base's ontology and a class's instances Co-Authored-By: Claude Opus 5.5 Signed-off-by: Bogdan Szabo --- .../utopia-server/src/api/ontology_routes.rs | 11 +++++--- .../utopia-server/src/api/token_read_tests.rs | 12 ++++++--- crates/utopia-server/src/auth.rs | 2 +- ...ty-from-the-person-scope-from-the-token.md | 27 +++++++++++-------- docs/design/access-and-audit.md | 6 ++--- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/crates/utopia-server/src/api/ontology_routes.rs b/crates/utopia-server/src/api/ontology_routes.rs index ee944bb0a..795e30c4a 100644 --- a/crates/utopia-server/src/api/ontology_routes.rs +++ b/crates/utopia-server/src/api/ontology_routes.rs @@ -10,6 +10,7 @@ use utopia_core::AppError; use utopia_store::ontology::{Lacking, LinkEnd}; use uuid::Uuid; +use crate::api::documents_routes::require_reader_kb; use crate::auth::{AuthUser, SessionOrToken}; use crate::error::ApiResult; use crate::state::AppState; @@ -23,12 +24,13 @@ async fn require_kb( utopia_store::access::require_kb(&state.pool, user, kb_id, min).await } +/// 令牌也读得到(0014 决定 7):类别 key 要先换成类型 id,才能去查实例 pub async fn get( State(state): State, - AuthUser(user): AuthUser, + reader: SessionOrToken, Path(kb_id): Path, ) -> ApiResult> { - require_kb(&state, &user, kb_id, Role::Viewer).await?; + require_reader_kb(&state, &reader, kb_id).await?; let (entity_types, relation_types) = tokio::try_join!( utopia_store::ontology::entity_type_views(&state.pool, kb_id), utopia_store::ontology::relation_type_views(&state.pool, kb_id), @@ -84,14 +86,15 @@ fn missing_link(q: &InstancesQuery) -> Result, AppError> /// 某个类下的实体实例列表(详情区右侧,分页)。 /// 带 `missing_predicate` 时只列缺那条关系的成员,并回显认作它的属性与短语。 +/// 令牌也读得到(0014 决定 7)。 pub async fn list_entity_instances( State(state): State, - AuthUser(user): AuthUser, + reader: SessionOrToken, Path((kb_id, type_id)): Path<(Uuid, Uuid)>, Query(q): Query, ) -> ApiResult> { let missing = missing_link(&q)?; - require_kb(&state, &user, kb_id, Role::Viewer).await?; + require_reader_kb(&state, &reader, kb_id).await?; let per = q.per.clamp(1, 100); let page = q.page.max(0); let predicate = match missing { diff --git a/crates/utopia-server/src/api/token_read_tests.rs b/crates/utopia-server/src/api/token_read_tests.rs index b9d52d525..289b35100 100644 --- a/crates/utopia-server/src/api/token_read_tests.rs +++ b/crates/utopia-server/src/api/token_read_tests.rs @@ -1,5 +1,5 @@ -//! 个人令牌读图(0014 决定 7):搜实体、实体详情、认知历史、事实证据、图总览、邻域六条读路由 -//! 收任何一枚活着的令牌。角色照旧是上限,`kb_ids` 排除了的库答 404;改实体仍只认会话。 +//! 个人令牌读图(0014 决定 7):搜实体、实体详情、认知历史、事实证据、图总览、邻域、本体、 +//! 某类的实例这些读路由收任何一枚活着的令牌。角色照旧是上限,`kb_ids` 排除了的库答 404;改实体仍只认会话。 use crate::state::AppState; use axum::body::Body; use axum::http::{Request, StatusCode}; @@ -15,6 +15,7 @@ struct Fixture { /// 令牌够不着的另一个库 other_kb: Uuid, reader: Uuid, + person: Uuid, entity: Uuid, fact: Uuid, _dir: tempfile::TempDir, @@ -68,6 +69,7 @@ impl Fixture { kb, other_kb, reader, + person, entity, fact, _dir: dir, @@ -81,8 +83,8 @@ impl Fixture { .1 } - fn read_paths(&self) -> [String; 6] { - let (kb, entity, fact) = (self.kb, self.entity, self.fact); + fn read_paths(&self) -> [String; 8] { + let (kb, person, entity, fact) = (self.kb, self.person, self.entity, self.fact); [ format!("/kbs/{kb}/entities?q=Zhang"), format!("/kbs/{kb}/entities/{entity}"), @@ -90,6 +92,8 @@ impl Fixture { format!("/kbs/{kb}/facts/{fact}/evidence"), format!("/kbs/{kb}/graph/overview"), format!("/kbs/{kb}/graph/neighborhood?entity={entity}"), + format!("/kbs/{kb}/ontology"), + format!("/kbs/{kb}/ontology/entity-types/{person}/entities"), ] } diff --git a/crates/utopia-server/src/auth.rs b/crates/utopia-server/src/auth.rs index f8a9ff910..e45f46416 100644 --- a/crates/utopia-server/src/auth.rs +++ b/crates/utopia-server/src/auth.rs @@ -177,7 +177,7 @@ impl FromRequestParts for AuthUser { /// **不替换 `AuthUser`。** `AuthUser` 把每个 Bearer 都当 JWT 解,令牌在那几百条 /// 路由上一律是 401——那是有意的:哪条路由准令牌进来,要在 0014 里说出理由。 /// 用这个提取器的只有文档原件(0052)、「把一个库搭起来」那几条和读图那几条:搜实体、 -/// 实体详情、认知历史、事实证据、图总览、邻域(决定 7)。 +/// 实体详情、认知历史、事实证据、图总览、邻域、本体、某类的实例(决定 7)。 /// /// 按推送时的 `external_id` 读回一篇文档(#1276)也是文档原件,开给令牌的理由写在决定 7。 /// diff --git a/docs/decisions/0014-identity-from-the-person-scope-from-the-token.md b/docs/decisions/0014-identity-from-the-person-scope-from-the-token.md index f005c3589..747d41e69 100644 --- a/docs/decisions/0014-identity-from-the-person-scope-from-the-token.md +++ b/docs/decisions/0014-identity-from-the-person-scope-from-the-token.md @@ -87,18 +87,22 @@ push documents in. 7. **A token reads the graph** (added 2026-10-16, #1262). A service that mirrors into a base reads the base back to check what landed, and a session JWT is the wrong credential for a backend - (decision 6). Six reads now also take any live token, acting as its owner: searching a base's + (decision 6). Eight reads now also take any live token, acting as its owner: searching a base's entities (`GET /kbs/{id}/entities`), an entity's detail (`GET /kbs/{id}/entities/{eid}`), its record-axis history (`…/history`), a fact's evidence (`GET /kbs/{id}/facts/{fid}/evidence`), - the graph overview (`GET /kbs/{id}/graph/overview`) and an entity's neighbourhood - (`GET /kbs/{id}/graph/neighborhood`). Search finds the entity by name; the detail gives its - facts, names and derivations; the history says when we changed our mind; the evidence ties a - fact to its sentence; the overview gives the most connected entities with the base's totals, - so a client sees the shape of what it pushed; the neighbourhood gives the entities around one, - so it can follow what a statement touches, and one started from another base's entity comes - back empty. Read is the lowest scope, so any token qualifies; the owner's role is still the - ceiling and Viewer suffices; a token whose `kb_ids` exclude the base gets 404, as for document - content (0052). Changing an entity on the same path (`PATCH`) still takes a session. + the graph overview (`GET /kbs/{id}/graph/overview`), an entity's neighbourhood + (`GET /kbs/{id}/graph/neighborhood`), the ontology (`GET /kbs/{id}/ontology`) and a class's + instances (`GET /kbs/{id}/ontology/entity-types/{tid}/entities`). Search finds the entity by + name; the detail gives its facts, names and derivations; the history says when we changed our + mind; the evidence ties a fact to its sentence; the overview gives the most connected entities + with the base's totals, so a client sees the shape of what it pushed; the neighbourhood gives + the entities around one, so it can follow what a statement touches, and one started from + another base's entity comes back empty; the ontology turns a class key into the type id the + instances read takes; the instances list that class's members, or only those lacking a + relation, and a class id from another base gives an empty page. Read is the lowest scope, so + any token qualifies; the owner's role is still the ceiling and Viewer suffices; a token whose + `kb_ids` exclude the base gets 404, as for document content (0052). Changing an entity + (`PATCH`) or the ontology on the same paths still takes a session. Reading a pushed document back by the `external_id` it was pushed under (`GET /kbs/{id}/sources/{sid}/documents/by-external-id`, added 2026-10-19, #1276) takes a @@ -133,7 +137,8 @@ push documents in. token now creates `api` sources too. - 2026-10-16: decision 7 opens four graph reads to any token; decision 6's "only these routes and document content" now also counts them. 2026-10-19: the graph overview and neighbourhood - join them, and so does reading a pushed document back by its `external_id` (#1276). + join them, and so does reading a pushed document back by its `external_id` (#1276). The + ontology and a class's instances follow (#1265). ## Open questions diff --git a/docs/design/access-and-audit.md b/docs/design/access-and-audit.md index 2ad0ab6c2..372ee8be0 100644 --- a/docs/design/access-and-audit.md +++ b/docs/design/access-and-audit.md @@ -19,9 +19,9 @@ writes an audit row `mcp.tool_called`; the tokens page shows the plaintext once snippet [0014]. A `write` token also sets up a base as its owner: creating a base and a `statements` or `api` source, reading the source's push key, importing an ontology and deciding a kind word, with the owner's role unchanged as the ceiling; any token lists a workspace's bases; a token limited by `kb_ids` -creates no base. These routes, document content and six graph reads (entity search, detail, -history, fact evidence, overview, neighbourhood; Viewer suffices, `kb_ids` answers 404) take -`SessionOrToken`; apart from MCP, every other route is session-only [0014 d6, d7]. `ingest_token` on a source is a different kind: plaintext, push-only [0014]. +creates no base. These routes, document content and eight graph reads (entity search, detail, +history, fact evidence, overview, neighbourhood, ontology, a class's instances; Viewer suffices, +`kb_ids` answers 404) take `SessionOrToken`; apart from MCP, every other route is session-only [0014 d6, d7]. `ingest_token` on a source is a different kind: plaintext, push-only [0014]. **A client reads a pushed document back by its `external_id`.** `GET /kbs/{id}/sources/{sid}/documents/by-external-id?external_id=…` finds an `api` or `statements`