Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions crates/utopia-server/src/api/ontology_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<AppState>,
AuthUser(user): AuthUser,
reader: SessionOrToken,
Path(kb_id): Path<Uuid>,
) -> ApiResult<Json<serde_json::Value>> {
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),
Expand Down Expand Up @@ -84,14 +86,15 @@ fn missing_link(q: &InstancesQuery) -> Result<Option<(&str, LinkEnd)>, AppError>

/// 某个类下的实体实例列表(详情区右侧,分页)。
/// 带 `missing_predicate` 时只列缺那条关系的成员,并回显认作它的属性与短语。
/// 令牌也读得到(0014 决定 7)。
pub async fn list_entity_instances(
State(state): State<AppState>,
AuthUser(user): AuthUser,
reader: SessionOrToken,
Path((kb_id, type_id)): Path<(Uuid, Uuid)>,
Query(q): Query<InstancesQuery>,
) -> ApiResult<Json<serde_json::Value>> {
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 {
Expand Down
12 changes: 8 additions & 4 deletions crates/utopia-server/src/api/token_read_tests.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -15,6 +15,7 @@ struct Fixture {
/// 令牌够不着的另一个库
other_kb: Uuid,
reader: Uuid,
person: Uuid,
entity: Uuid,
fact: Uuid,
_dir: tempfile::TempDir,
Expand Down Expand Up @@ -68,6 +69,7 @@ impl Fixture {
kb,
other_kb,
reader,
person,
entity,
fact,
_dir: dir,
Expand All @@ -81,15 +83,17 @@ 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}"),
format!("/kbs/{kb}/entities/{entity}/history"),
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"),
]
}

Expand Down
2 changes: 1 addition & 1 deletion crates/utopia-server/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl FromRequestParts<AppState> for AuthUser {
/// **不替换 `AuthUser`。** `AuthUser` 把每个 Bearer 都当 JWT 解,令牌在那几百条
/// 路由上一律是 401——那是有意的:哪条路由准令牌进来,要在 0014 里说出理由。
/// 用这个提取器的只有文档原件(0052)、「把一个库搭起来」那几条和读图那几条:搜实体、
/// 实体详情、认知历史、事实证据、图总览、邻域(决定 7)。
/// 实体详情、认知历史、事实证据、图总览、邻域、本体、某类的实例(决定 7)。
///
/// 按推送时的 `external_id` 读回一篇文档(#1276)也是文档原件,开给令牌的理由写在决定 7。
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions docs/design/access-and-audit.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Loading