diff --git a/docs/en/rfcs/2570-knowledge-base.md b/docs/en/rfcs/2570-knowledge-base.md new file mode 100644 index 0000000000..49f7343e20 --- /dev/null +++ b/docs/en/rfcs/2570-knowledge-base.md @@ -0,0 +1,499 @@ +--- +title: "RFC 2570: Scope-based Knowledge Base document plugin" +description: "Plugin-local space Scopes organize knowledge-base Scopes for cross-base retrieval, a Dashboard, three database backends, synchronous editing and native soft deletion." +--- + +- Proposal Name: `knowledge_base_plugin` +- Start Date: 2026-11-10 +- RFC PR: [#2570](https://github.com/oceanbase/powercontext/pull/2570) +- Tracking Issue: [#2440](https://github.com/oceanbase/powercontext/issues/2440) +- Related Issues: [#2441](https://github.com/oceanbase/powercontext/issues/2441), [#2442](https://github.com/oceanbase/powercontext/issues/2442), [#2443](https://github.com/oceanbase/powercontext/issues/2443), [#2444](https://github.com/oceanbase/powercontext/issues/2444) + +# Summary + +One `knowledge-base` business plugin provides local ingestion, parsing, chunking, versioning, and hybrid retrieval. Each +ordinary Scope is one knowledge base. The plugin declares only the `document` family, with one Artifact per complete file. +A plugin-local space Scope groups knowledge-base Scopes. Space identities, membership, and user defaults live only in plugin +storage: core stores neither the space object nor these parent-child relationships. A knowledge base is not an Artifact. The plugin supplies original +and processing-request Source definitions and reuses framework generate, Supervisor, and consumption cursors rather than +implementing an independent task scheduler. + +The first phase supports SQLite, SeekDB, OceanBase, Markdown, standalone-image RapidOCR, rule-based chunking, synchronous +chunk edits/removals, document forget, a Dashboard, browser uploads, and cross-base retrieval within a space. Spaces organize +query scope; they do not introduce tenant isolation or team authorization. This is a target design dependent on framework contracts, not a claim that the +current implementation PR provides every capability. + +# Motivation + +Business descriptions and SOPs need searchable passages, original provenance, and manual changes that preserve history. +Scope provides the authorization and organizational boundary, Artifacts manage whole-document revisions, and plugin storage +holds bodies and indexes. Existing scheduling, revision preconditions, and lifecycle rules reduce domain-specific machinery. + +# Guide-level explanation + +1. Select a plugin-local space; if the UI has no selection, read the caller's default first and pin space_scope_id for creation. + An administrator creates the knowledge-base Scope there and saves parsing/chunking configuration. +2. Upload files through the Dashboard or import a Server-readable directory; select multiple knowledge bases within the space for retrieval. +3. The plugin saves immutable snapshots, registers Sources, and submits processing-request Sources. Acceptance is not parsing success. +4. Framework generate asynchronously processes Markdown/images, prepares complete chunks and indexes, then publishes a document. +5. Changed content and explicit reprocessing retain the document ID. Failures preserve existing revisions. Status distinguishes + pending, latest failure, published, and conflict/skipped. +6. Synchronous chunk edits/removals increment the document revision; core records a lineage_only success Source in the same transaction. +7. Whole-document soft deletion uses generic forget: deprecated, excluded from normal retrieval, with no new revision. Bodies, + originals, and history remain stored. + +The first phase does not promise in-flight cancellation, Source deletion, knowledge-base cascades, physical erasure, an +independent queue, online model switching, cross-space aggregation, generated answers, nested/movable/deletable spaces, team +membership or permission inheritance, continuous directory sync, or remote runners. +PDF/Office, scene-image understanding, OCR of embedded Markdown images, semantic/LLM chunking, and large-corpus benchmarks +are out of scope. Restore/merge methods are not implemented initially; generic routes report unsupported. Import is not restoration. + +# Reference-level explanation + +## 1. Plugin, Scope, and framework dependencies + +Follow [#2549](https://github.com/oceanbase/powercontext/pull/2549) for single-family plugins, Source definitions, writers, +models, and storage, and [#2579](https://github.com/oceanbase/powercontext/pull/2579) for routes and Scope management. +The required framework contract is specified by [#2659](https://github.com/oceanbase/powercontext/pull/2659) and implemented by +[#2660](https://github.com/oceanbase/powercontext/pull/2660). Merge those dependencies before the knowledge-base implementation; +an older master is not sufficient. Framework storage provisioning, original edit-payload evidence, transaction-bound authorization, +governance concurrency checks, and generic Dashboard Artifact forwarding belong to those framework changes, not this plugin PR. +The plugin consumes public contracts and owns its tables/indexes, snapshots, parsing, retrieval, routes, and pages. Initialize through +setup without depending on a framework upgrade hook. [#2617](https://github.com/oceanbase/powercontext/pull/2617) +is a future collection integration, not a prerequisite for explicit local imports. + +| Object | Model | +| --- | --- | +| Space Scope | Plugin-local logical Scope, space_scope_id; no core Scope record | +| Knowledge-base Scope | Ordinary core Scope, scope_id; belongs to exactly one space initially | +| Plugin | knowledge-base, one installed directory, one document family | +| Original | knowledge-base-file Source, pinned snapshot and original path | +| Processing request | knowledge-base-process Source, pinned original reference, configuration, base, and request identity | +| Document | document Artifact, bounded whole-document manifest | +| Chunk | Immutable result in plugin storage, not a separate Artifact | + +```mermaid +flowchart TD + P[Plugin-local space Scope] --> M[Plugin-only membership] + M --> S[Core Scope: knowledge base] + S --> C[Plugin configuration] + S --> O[Original Source and immutable snapshot] + O --> R[Processing request Source] + R --> G[Framework generate and cursor] + G --> I[Immutable chunks and indexes] + I --> D[document Artifact revision] + U[Synchronous chunk edit] --> D + D --> Q[Retrieval and exact chunk reads] +``` + +Create through ctx.scopes.create with server.admin; a key derived from the same full creation identity returns the same Scope. +The framework's plugin external reference identifies creator ownership. Scope fields hold title/summary; plugin tables hold +business configuration. Retry configuration after a successful Scope creation if its separate write fails; reject imports +until configuration is ready. Reuse Scope read/contribute, Artifact write, and framework preconditions. Plugin routes must not +treat Scope contribution as permission to modify every document. + +Plugin inputs subscribe only to processing requests, so originals do not independently trigger this plugin again. Deployment +reserves knowledge-base Scopes for this use and does not enable other content consumers. A Source type name is not framework +enforced exclusive admission; isolation from arbitrary other consumers is not a guarantee of this plugin. + +### 1.1 Space Scopes and default selection + +space_scope_id and core scope_id are separate namespaces. Never pass a space ID to core Scope APIs. The UI calls it a space; +Workspace may be a display label, not another Artifact family or core domain model. Initially spaces are one level deep and +one knowledge base has one parent. Store membership as indexed space_scope_id on the plugin's scope configuration row, never +as core parent_scope_id or context_references. + +Defaults use the authenticated ctx.caller.principal_id. First default resolution idempotently creates a personal space and +its default mapping; concurrent callers resolve the same space. This creates no core Scope or contribution grant. Users may +select a visible space as their default. A space records a personal owner or team kind; team is an organizational label only. +Explicit space creation/renaming and knowledge-base creation/renaming require server.admin. Implicit personal-space registration +and changing one's own default do not require permission to create core Scopes. + +Personal owners can see their spaces; a caller who can read a contained knowledge base can see its space; administrators can +manage empty spaces. Lists/counts include only readable knowledge bases. Membership grants no Scope read/contribute or Artifact +write, permission inheritance, tenant isolation, or cascade deletion. An inaccessible default causes implicit knowledge-base lists +and retrieval to fail closed. GET /me/default-space still returns the caller's own saved ID/version with available=false, and +GET /spaces independently lists accessible spaces. The caller explicitly selects a visible space and uses the saved version to +change the default; neither endpoint silently selects another space. A space without readable bases returns no hits, never a global +fallback. Moving knowledge bases or deleting spaces is not supported initially, avoiding implicit data migrations. + +The knowledge-base creation POST requires an explicit nonempty space_scope_id; missing/empty values return 422. The server +never resolves the default during creation or retry. Dashboard/other callers first read the default or use an explicit selection, +then pin the space ID, request key, and creation parameters. Automatic retries preserve them even if the user's default changes. + +The full creation identity is (ctx.caller.principal_id, space_scope_id, idempotency_key). Derive a key using versioned canonical +encoding and a digest of that tuple for ctx.scopes.create; core adds the plugin-name prefix itself. Never pass only the client's +raw request key or put space membership into core Scope fields. The same full identity returns the same Scope and must match +existing plugin membership. Different callers/spaces with the same raw key are different creations, never reuse or relocation. + +After validating the space and permissions, idempotently create the core Scope, then commit unique membership/configuration +in plugin storage before reporting readiness. Retry incomplete registration using the pinned space and full creation identity; +a Scope missing membership/configuration cannot receive imports or enter space queries. Queries and knowledge-base lists may +still omit the space and resolve the current default; those read rules never change a creation's pinned target. + +## 2. Local storage, identity, and request acceptance + +OpenDAL fs manages snapshot directories accessible to the importer, Server, and Workers. Original paths are provenance; +parsing reads snapshots only. After checking canonical-root containment, open each directory component from the filesystem root +through directory descriptors with O_NOFOLLOW, including ancestors of the allowed root; open the final file relative to that bound +directory. Resolving then reopening an absolute root is insufficient because an ancestor can be replaced by a symlink between +validation and open. Save bytes and compute SHA256 before Source registration; mtime/size cannot replace the content hash. + +```text +file_key = (scope_id, collection_id, normalized_relative_path) +document_id = stable_hash(identity_version, file_key) +source_key = stable_hash(source_schema_version, file_key, raw_content_digest) +request_key = (scope_id, idempotency_key) +result_id = digest(canonical complete chunk result and provenance) +``` + +For Server directories, collection derives from the storage alias and normalized root. Browser uploads use a stable collection +within the knowledge base, never a new collection per batch. Browsers provide filenames/relative paths, not trusted client +absolute paths. Record origin_kind, collection, and relative_path; OpenDAL fs saves uploaded bytes in the shared Server-local +snapshot directory. Equal names from the two origin types are independent by default; the same collection/path retains identity. +Use bounded binary requests and request keys, normalize relative paths, reject traversal, and keep file bodies out of JSON Sources. +Compare full identities on hash matches. Files in different knowledge-base Scopes remain independent; space IDs are not part of +file_key. Same path/bytes reuse the original Source; changed bytes create a new Source +but retain the document ID. Renames are new files; disappearance does not automatically forget a document. + +Each processing request pins original_source_ref, snapshot_ref, processing settings, independent model fingerprint, +base_revision, manual_edits_policy, and request digest. Existing documents pin the accepted revision; first publication has an +explicit no-head precondition. Ordinary duplicate imports reuse available matching content/configuration. Explicit reprocessing +and user retries use new request keys, producing new request Sources even for unchanged originals. Ordinary imports preserve +manual edits; discarding them requires explicit discard authorization. + +First persist bounded immutable acceptance parameters in a plugin resource row, then idempotently register the original and +request Sources, and finally record their actual SourceRefs. Framework generate can start before the plugin acknowledgement +fields are saved: execution uses the committed request Source and pinned acceptance parameters, not another ready flag. +Return accepted only after the request Source is durably confirmed. After a lost response, replay the same key/parameters to +complete registration; a key reused with different parameters conflicts. This register is not a queue: requests without a +successfully registered Source have no background-execution promise and callers can retry. Unreferenced snapshots are not searchable. + +Directory ingestion uses bounded file batches through this same acceptance path and returns per-item receipts. Enumeration +completion does not mean parsing completion. No independent directory-scan job is persisted initially; clients submit large +directories in batches, and repeated enumeration deduplicates by file identities and request keys. + +## 3. Parsing, chunking, and original provenance + +Markdown uses markdown-it-py for headings, blocks, and source mappings. Standalone images use RapidOCR with ONNX Runtime, +retaining boxes, order, confidence, and coordinate transforms; normalized Markdown enters the same chunker. Do not infer +complex tables or flowcharts. Embedded Markdown images remain references. + +Split by heading/content blocks, recursively splitting oversized blocks at paragraphs/sentences. Preserve code, tables, and +SOP steps where possible; split oversized tables with repeated headers. Overlap applies to split long paragraphs. Configure +max_chunk_chars/overlap_chars; 1200/100 is an initial test value. Validate the complete embedding input, including heading text, +against model token limits without silent truncation. Report invalid UTF-8, image dimensions/decoding, and OCR with no text. + +Parsers return Markdown, locators, and warnings, not cursor or Artifact changes. New formats use an internal parser registry, +not additional plugin types. + +A request records the original SourceRef and snapshot. Generate directly references only request Sources in its current window; +a previously consumed original may be outside that window and cannot bypass writer restrictions. The document manifest retains +original provenance, providing a verifiable document → request Source → original Source chain without claiming that the original +is always direct lineage. Synchronous edits preserve current lineage and original locators and add core's success Source. + +## 4. Plugin storage and models + +ctx.storage follows #2660: a dedicated SQLite file or the SeekDB/OceanBase database powercontext_plugin_knowledge_base, precreated by the deployer +when database-creation permission is unavailable. The plugin selects migrations and full-text/vector implementations by backend, +without changing existing core schemas. Originals stay in local OpenDAL directories rather than SQL body columns. + +| Plugin domain table | Fields and constraints | +| --- | --- | +| pc_knowledge_base_spaces | Primary key space_scope_id; title, kind, personal_owner_id, created_by, creation_key, request_digest, version; unique personal owner and creation key | +| pc_knowledge_base_space_defaults | Primary key principal_id; space_scope_id, version; one default mapping per authenticated user | +| pc_knowledge_base_resources | Primary key (scope_id, resource_kind, resource_key); fixed scope/document/request kinds; one scope configuration row with indexed space_scope_id; bounded payload, request_digest, source_ref, document_id, latest observations and timestamps | +| pc_knowledge_base_chunks | Primary key (scope_id, document_id, result_id, chunk_id); ordinal unique within a result; text, text_length, content_digest, heading_path, source_locator, origin, edit_metadata | + +There are four plugin domain tables; spaces/defaults do not invent core scope_id values. Validate space references in plugin +transactions and use version preconditions for default updates. Register the deterministic personal space before its default +mapping; unique keys and idempotent retries repair interruption between these writes without replacing an existing default. Explicit space-creation keys are scoped to their creator; changed parameters conflict. Reuse the scope +resource row for membership without an additional relationship table or core schema changes. + +Resources is a fixed-purpose register, not a generic task framework: scope rows hold configuration/version, document rows stable +file identities, and request rows immutable accepted inputs, receipts, and latest attempt observations. No leases, execution +tokens, claiming queue, or cleanup cursors; do not copy the Artifact publication pointer or lifecycle. Request results may refer +to exact revisions for reconciliation, but core determines success. Full-text/vector auxiliary structures are derived indexes, +not additional domain tables. + +Document content holds only title, original SourceRef, file identity, result_id, chunk count/digest, configuration fingerprints, +manual-edit markers, and applied_request_id. Neither bodies nor unbounded chunk-ID lists belong in the manifest. Result IDs +derive from canonical complete results, never from a predicted uncommitted revision number. + +The plugin independently configures and creates model clients. Deployment supplies credentials; Sources/Artifacts do not store +them. Do not automatically follow ctx.models or Server model changes. Initially one fixed embedding profile serves the plugin, +including model identity/version, dimensions, complete input rules, and normalization version. Equal dimensions do not establish +model compatibility. If existing indexes disagree with deployment configuration, reject vector queries and new publication and +require controlled migration; do not silently replace configuration or mix spaces. Online model switching is not included. +Same-profile index repair reads committed chunks. Parsing/chunking configuration may vary by Scope and is pinned in each request. + +The framework SQL namespace is plugin-name based, not core-database based. Independent deployments sharing a SQL tenant must +isolate their plugin databases/accounts; changing only the core database does not isolate this plugin's tables. + +## 5. Framework consumption, status, and failure handling + +Declare on_sources and processing-request inputs; reuse framework generate/Supervisor, Scope bindings, consumption cursors, +lease fencing, and failure backoff. No independent jobs table or plugin Worker scheduler. Each invocation opens at most one +writer.transaction; core atomically commits Artifacts, the cursor, and invocation acknowledgement. Use a small max_sources and +bounded file sizes to keep OCR/model windows within limits; arbitrary files are not guaranteed to fit framework timeouts. + +| Event | Behavior | +| --- | --- | +| Request submitted without a result | Report pending, without invented percentages | +| Deterministic input error such as invalid format or no OCR text | Persist a bounded failure observation and skip that input; normal window acknowledgement permits subsequent requests | +| Database, model service, or durable index failure | Raise; core rolls back and the cursor does not advance; framework retries with backoff | +| Pinned base changed or document no longer active | Record conflict/skipped, never automatically replace the base; caller submits a new request | +| Core publication completed | Derive published state and exact revision from manifest/history applied_request_id | +| Core succeeded but plugin status acknowledgement failed | Reconcile core, not failure or duplicate publication inferred from a missing status | + +Status reads first reconcile the pinned base+1 revision and current manifest with applied_request_id. If no publication belongs to +the request and the current revision has advanced past its pinned base or the document is inactive, pending/processing resolves to +failed with a conflict reason. This also repairs status when the cursor committed but the later conflict observation failed; it +requires no redelivery, new table, or automatic rebasing. Restart and same-key receipt replay preserve that outcome. + +Display the currently usable revision separately from the latest request status: failed reprocessing does not make an older +committed revision unusable. Failure observations describe the latest attempt, not proof of cursor acknowledgement. Plugin status cannot advance consumption +or justify skipping unproven core writes. Persistent infrastructure failure may block subsequent windows in the Scope; independent +unlimited per-file concurrency and a dead-letter queue are not promised. User retries of deterministic failures use new requests; +old Sources remain unchanged. Processing requests express intent, not lineage_only success. + +Require no head for first publication. Updates validate the pinned base, original, manual-edit policy, and lifecycle. Framework +retries cannot rewrite the request's base or discard policy. If an edit/forget occurs after preparation, publication uses framework +revision/governance concurrency checks and must not reactivate deprecated documents. The framework tracks governance observed by +writer reads and checks it during publication; the plugin passes expected_revision to put and does not invent an +expected_state_version writer argument or a local lock. Lifecycle-race guarantees are an acceptance requirement for #2660. + +## 6. Publication and index consistency + +1. Parse and calculate vectors outside the core transaction; durably store immutable chunks and their full-text/vector indexes, + validating counts, digests, and readability. +2. Validate preconditions and put a manifest referencing result_id and index fingerprints in writer.transaction. Generate commits + its cursor with the revision; user edits commit success evidence with the revision. +3. A failed core commit leaves prepared results invisible and the previous manifest/index usable. Retries do not overwrite old results. +4. Retrieval candidates must match the active current document's result_id/profile. Refill after filtering; unpublished results + must not consume the effective top-k. + +Isolate indexes by result rather than overwriting current candidates under document_id alone. Model/file computation stays outside +short transactions. No additional core Scope-row gate or distributed transaction protocol is introduced. Exact historical chunks +resolve through their revision's manifest, independently of current search indexes. Unreferenced prepared data does not change +visible results; permanent cleanup is a separate contract. + +## 7. Synchronous chunk editing and soft deletion + +Generic Artifact PUT invokes update_artifact synchronously for chunk edits/removals; these are not plugin-route writes or queued +commands. The plugin editing payload is a bounded chunk-change description containing a chunk ID/new text or removal action. +The service reads the base revision and composes the complete result. Only the service creates manifests; reject caller-supplied +arbitrary result IDs or original ownership. Core permits plugin normalization of submitted content; arbitrary manifest replacement +is not exposed. + +Require If-Match, prepare the new result/indexes, then perform exactly one put(expected_revision=...). Core stores the submitted +edit payload as a lineage_only Content Source in the same transaction and increments the document revision. Edits retain logical +chunk identity and original locators, marking non-verbatim manual text. Removing the final chunk permits a zero-chunk revision. +Edit evidence can contain the new text; existing permissions apply, and forget does not erase that evidence. + +Model calls obey synchronous time and edit-size limits; failures publish no partial result. After an uncertain response, read +current revision/lineage before retrying. An old If-Match cannot be reused blindly. Generic PUT does not add an idempotency-key +or exactly-once HTTP guarantee. + +Whole-document soft deletion uses generic forget, invoking forget_artifact with exactly one set_lifecycle(deprecated). This is +only a lifecycle transition: no new content revision, no plugin-specific deletion evidence, and no physical removal of bodies, +indexes, or snapshots. Normal retrieval and Prepared Context exclude deprecated documents. Generic get/history/evidence retain +framework visibility; authorized exact chunk reads and rendering can show retained versions. Do not promise all content becomes +unreadable. Core owns lifecycle; no duplicate document tombstone in plugin storage. Index cleanup is not a prerequisite for forget. + +Reimport, reprocessing, and late work never automatically restore forgotten documents; synchronous edits require active documents. +Before a document exists there is no forget target. In-flight import cancellation is not supported; do not create empty Artifacts +or deletion revisions. No Source deletion or knowledge-base cascade API; physical Scope deletion is not soft deletion. Repeated +forget follows framework preconditions/no-op behavior without scheduling cleanup work. + +## 8. APIs, retrieval, and authorization + +Let `P=/v1/plugins/knowledge-base/api` and `D=/v1/scopes/{s}/artifacts/document/{d}`. Declare plugin routes through PluginApi/Route +using supported response facilities. Accepted below is a business state, not an assumption that custom HTTP 202 is available; +default successful JSON responses may use 200. + +| Operation | API and response | +| --- | --- | +| List/create/rename spaces | GET/POST `P/spaces`, PATCH `P/spaces/{space_scope_id}`; plugin-local spaces, writes require idempotency keys or version preconditions | +| My default space | GET/PUT `P/me/default-space`; current caller's space_scope_id and version; GET adds available; no caller-supplied principal_id for other users | +| List knowledge bases | GET `P/knowledge-bases`; optional space_scope_id, otherwise use the current default | +| Create knowledge base | POST `P/knowledge-bases`; required space_scope_id and idempotency_key; idempotency uses caller + space + request key, returning both IDs | +| Rename/configure knowledge base | PATCH `P/scopes/{s}/settings`; ctx.scopes.update for title/summary, plugin storage for chunking, separate version preconditions | +| Import files | POST `P/scopes/{s}/imports`, bounded paths and idempotency_key; per-file request SourceRefs | +| Browser upload | POST `P/scopes/{s}/uploads`, binary body; collection, relative_path, idempotency_key in query; same acceptance path | +| Document list | GET `P/scopes/{s}/documents`, paginated published summaries and unpublished request observations; core determines revision/lifecycle | +| Reprocess/retry | POST `P/scopes/{s}/documents/{d}/reprocess`, base, pinned configuration, original reference, new request key | +| Processing status | GET `P/scopes/{s}/imports/{request_id}`, acceptance, latest attempt, and verifiable publication | +| Document overview/history | Generic Artifact GET/revisions, bounded manifest | +| Edit/remove chunk | PUT `D`, If-Match, synchronous new document revision | +| Soft-delete document | POST `D/forget`, revision and state_version; framework response | +| Chunk directory/body | GET `P/scopes/{s}/documents/{d}/revisions/{rev}/chunks`; append `/{chunk_id}` for body | +| Batch chunks | POST directory path plus `/batch-get`, bounded chunk_ids | +| Chunk hybrid retrieval | POST `P/scopes/{s}/retrieve`, exact revision, chunk ID, score, provenance | +| Cross-base retrieval within a space | POST `P/retrieve`, optional space_scope_id/scope_ids; resolve as below, hits carry space and knowledge-base IDs | +| Rendering/generic search | render_artifact/search_artifacts; generic search and Prepared Context integration | + +Examples for knowledge-base creation, import, editing, forget, and retrieval: + +```json +{"space_scope_id":"space-team","title":"Operations SOPs","summary":"Runbooks","idempotency_key":"kb-01","chunker":{"max_chunk_chars":1200,"overlap_chars":100}} +``` + +```json +{"root":"/work/docs","paths":["restart.md"],"idempotency_key":"import-01"} +``` + +```json +{"scope_id":"scope-ops","items":[{"document_id":"doc-1","request_ref":{"source_type":"knowledge-base-process","source_id":"request-01"},"status":"accepted"}]} +``` + +```json +{"content":{"operation":"edit_chunk","chunk_id":"chunk-1","text":"Check replica health before restarting."}} +``` + +```json +{"content":{"operation":"remove_chunk","chunk_id":"chunk-2"}} +``` + +```json +{"revision":3,"state_version":0} +``` + +```json +{"query":"How should I restart a follower?","mode":"hybrid","limit":10} +``` + +```json +{"hits":[{"space_scope_id":"space-team","scope_id":"scope-ops","document_ref":{"family":"document","artifact_id":"doc-1","revision":3},"chunk_id":"chunk-1","score":0.82,"source_ref":{"source_type":"knowledge-base-file","source_id":"original-1"},"locator":{"heading_path":["Operations","Restart"]}}]} +``` + +Chunk APIs read the requested exact revision without current-version fallback. Directory pagination omits bodies; single/batch +reads return bounded bodies and provenance. Reject duplicate/missing IDs and excessive responses without silent truncation. +Check Scope/document permissions and ownership first; plugin storage spans Scopes, so chunk IDs alone never authorize reads. + +Generic search retains the framework's Artifact-reference response; plugin retrieve returns chunk structures. Prepared Context +uses SearchHit text/locators within its byte budget. Normal search excludes non-active, stale, and unauthorized candidates before +refilling. Unsupported filter/rerank parameters fail explicitly. Python/HTTP share business semantics. Generic search/Prepared +Context retain their Scope contract; plugin retrieve provides space aggregation. + +### 8.1 Space selection and cross-base retrieval + +1. Omitted space_scope_id resolves the caller's default; an explicit value must be visible. Every explicit scope_id must belong + to that space; selecting bases never implicitly switches the resolved space. +2. Omitted scope_ids selects readable bases in that space; an explicit empty list selects none. An explicitly inaccessible or + out-of-space base fails the whole request rather than being silently omitted. Bound base count, request size, and total hits; + require complete defaults/membership/configuration and never broaden to global search. +3. Retrieve full-text/vector candidates in the selected range and fuse them consistently. Reuse the query vector for the fixed + embedding profile. Use comparable scoring/common candidate ranking, not raw BM25 scores from separate per-base corpora. + Constrain candidates by Scope, then verify core access, active lifecycle, current result_id/profile, and refill after filtering. +4. Hits carry space_scope_id, scope_id, document_ref including revision, chunk_id, score, and provenance. Deduplicate with full + ownership/result identity; importing the same file into different bases does not merge its evidence. Exact chunk reads still + resolve by knowledge-base Scope and revision. + +```json +{"space_scope_id":"space-team","scope_ids":["scope-ops","scope-dev"],"query":"How should I restart after a release?","mode":"hybrid","limit":10} +``` + +A query alone searches readable bases in the default space; space_scope_id alone selects readable bases in that space. +Aggregation reuses single-base authorization, lifecycle, and revision rules without nested HTTP requests. Backend failures are +explicit; partial success and a consistent snapshot across bases are not first-phase promises. + +## 9. Dashboard + +PluginPages provides space/default selection, knowledge-base management, Markdown/standalone-image drag-and-drop upload and +local-directory import, document/request status, retry/reprocess, paginated chunk metadata/bodies, history viewing, synchronous +edit/removal, document forget, and multi-base retrieval within a space. Filter lists by permissions and retain operation-specific +access checks: creation/configuration management requires server.admin, imports require scope.contribute, and edit/forget retain +artifact.write. Pin the selected/default space before submitting creation; retries preserve space_scope_id and the request key. +Changing the creation target is a new user operation. Resolve valid chunking configuration before initial import; no separate +post-upload pending-configuration queue. Capture the space, knowledge-base Scope, document/revision, and view generation for +asynchronous reads; discard stale results and errors after navigation. Status requests must retain their originating Scope, and +old lists/chunks must not replace the newly selected view. An unavailable default leaves the selector usable for explicit recovery. + +Display the usable revision separately from the latest request. A failure does not hide previously committed content. Successful +edits show the new revision; conflicts require re-reading, and uncertain responses require reconciliation. Removing the final +chunk is valid. Label forget as removal from retrieval with history retained; omit cancellation, restoration, whole-base deletion, +and generated-answer controls. + +The sandboxed iframe calls declared plugin routes through the framework client; reads can wrap ctx.reads. Generic document +GET/PUT/forget uses #2660 `client.artifacts(method, path, {scopeId, ifMatch, json})` and the host's +`/dashboard/plugins/{name}/artifacts/{scope_id}/{artifact_id}` route. PluginPages does not declare artifact_actions, and the plugin +must not add an /api/_artifacts proxy. The framework restricts operations to the owning family and retains caller authorization, +If-Match/revision/state_version; plugin hooks retain knowledge-base ownership checks. Do not provide +arbitrary URL forwarding or session credentials. Invoke the generic Artifact operation and its atomic evidence path; an ordinary +plugin writer or asynchronous edit Source is not a substitute. Upload files separately within binary request limits, enforce +server-side path/size bounds, and do not present uploaded-file counts as completed parsing progress. + +## 10. Initialization, implementation, and acceptance + +setup invokes plugin-owned idempotent initialization and schema-version checks. Deployment runs plugin migrations with +coordination in plugin storage across instances. Preserve historical manifest/Source decoding and reject incompatible versions. +The first phase restarts for deployment and does not depend on an upgrade(ctx) hook or hot reload. +Backups must include core, plugin storage, and OpenDAL snapshots; core backups do not automatically cover plugin data. + +#2659/#2660 supply framework storage, synchronous edit evidence, transaction-bound authorization, governance checks, and the +Dashboard Artifact bridge. The knowledge-base PR contains no core runtime, core schema, framework SDK, host, writer, or Dashboard +bridge changes. Repository integration is limited to plugin files, optional dependencies, plugin tests/CI, and usage documentation. +Do not introduce knowledge-base lock tables, read guards, or an independent queue. Verify against the merged framework contracts; +until those dependencies merge, report integration results against the tested framework commit rather than master compatibility. + +Validate on SQLite, SeekDB, and OceanBase: + +- Concurrent plugin-local space/default initialization, default-update conflicts, revoked-default recovery without implicit fallback, unique membership, no core space hierarchy. +- Allowed-root and ancestor symlink replacement after validation; delayed Dashboard list/status responses after switching bases. +- Conflict observation failure after cursor commit, including restart and replay; status must not remain processing forever. +- Default/explicit space queries, empty selections, inaccessible/out-of-space rejection, filtered counts, global top-k, scoped citations. +- Sandboxed Dashboard generic-operation forwarding, preserved If-Match, zero-chunk edits, separate statuses and usable old revisions. +- Administrator-only Scope creation, membership/configuration retry, multi-Scope isolation, snapshots, stable identities/path bounds for both origins. +- Missing creation space returns 422; after core success/plugin failure, changing the default does not redirect the retry; raw keys cannot collide across callers/spaces. +- Retries across import acceptance boundaries, key conflicts, content deduplication, repeated handling, and deterministic failures + that do not block later files. +- Window rollback/redelivery, failure observations versus committed core success, and pinned-base conflicts without automatic rebasing. +- Markdown, real RapidOCR, oversized blocks, complete embedding-input limits, provenance, and manual-text markers. +- Synchronous edit/removal revisions and lineage_only evidence, zero chunks, model failures, and lost responses. +- Edit/forget racing generate without overwriting edits or restoring deprecated documents; retained originals/history. +- Core rollback after index preparation while old retrieval remains usable, candidate filtering, exact reads, generic search, and Prepared Context. +- Explicit model-fingerprint mismatch failures; Server model changes do not alter the plugin vector space. + +Use Markdown from a pinned oceanbase/oceanbase commit and real text images. Mocks cannot establish real OCR or backend support. +#2441 covers spaces/defaults/ingestion/configuration; #2442 framework consumption/parsing/publication; #2443 cross-base retrieval, +editing, and Dashboard; #2444 operation/acceptance; #2440 tracks the whole feature. Implementation may use one PR with staged commits and validation. No new issues. + +# Drawbacks + +Persistent infrastructure failures can block a Scope window; synchronous editing has request time limits. The plugin owns +three-backend indexes, migrations, and backups. Immutable results/history consume storage. Forget removes normal retrieval, +not history access or stored evidence; Sources remain immutable and readable. + +# Rationale and alternatives + +- Plugin-local space Scopes organize knowledge-base Scopes; membership/defaults stay in plugin tables without core hierarchy or implied grants. +- A core Scope is a knowledge base, matching a single-family plugin without another Artifact or cross-family writes. +- Framework generate/cursor atomically commits Artifacts and consumption progress; the plugin records acceptance/observations + rather than duplicating scheduling. +- Generic synchronous PUT supplies preconditions and success evidence; plugin-route writes do not automatically have those semantics. +- Original and request Sources separate immutable facts from reprocessing; direct lineage and original provenance have explicit roles. +- Immutable chunk references preserve history and failure recovery; in-place body replacement cannot implement versioned editing. +- Forget follows framework lifecycle without Source tombstones, deletion revisions, or global read interception. + +# Prior art + +Builds on [plugin interfaces #2549](https://github.com/oceanbase/powercontext/pull/2549), +[plugin APIs #2579](https://github.com/oceanbase/powercontext/pull/2579), +[Source identity](1400_source_definition_and_observation_model.md), +[processing supervision](1515_artifact_processing_supervisor.md), and [Source/Artifact APIs](1437_source_artifact_rest_api.md). +Future collection can integrate [#2617](https://github.com/oceanbase/powercontext/pull/2617). Collection checkpoints describe +acceptance, not completed parsing; deleting a collection target does not delete its Sources or documents. + +# Unresolved questions + +Implementation fixes SQL/index details, OCR model versions, window/file sizes, synchronous editing limits, and API byte bounds. +Validate revision/lifecycle races on real backends. Check gaps between framework RFCs and implementations, including dedicated +storage, edit evidence, and Dashboard forwarding under #2659/#2660; proposals are not released capabilities. These choices do not change append-only Sources, forget without +new revisions, synchronous edits, pinned request bases, or independent models. + +# Future possibilities + +Word/PDF parsing, continuous collection, cross-space queries, generated answers, rerank, adjacent context, in-flight cancellation, and controlled model +migration. Complete Source deletion, revoking historical access, knowledge-base cascades, and independent long tasks require +separate contracts rather than implicit first-phase promises. diff --git a/docs/zh/rfcs/2570-knowledge-base.md b/docs/zh/rfcs/2570-knowledge-base.md new file mode 100644 index 0000000000..ec72e24f3c --- /dev/null +++ b/docs/zh/rfcs/2570-knowledge-base.md @@ -0,0 +1,423 @@ +--- +title: "RFC 2570:基于 Scope 的 Knowledge Base 文档插件" +description: "插件内空间 Scope 组织知识库 Scope,支持空间内跨库检索、Dashboard、三种数据库、同步 chunk 编辑及原生软删除。" +--- + +- Proposal Name: `knowledge_base_plugin` +- Start Date: 2026-11-10 +- RFC PR: [#2570](https://github.com/oceanbase/powercontext/pull/2570) +- Tracking Issue: [#2440](https://github.com/oceanbase/powercontext/issues/2440) +- Related Issues: [#2441](https://github.com/oceanbase/powercontext/issues/2441), [#2442](https://github.com/oceanbase/powercontext/issues/2442), [#2443](https://github.com/oceanbase/powercontext/issues/2443), [#2444](https://github.com/oceanbase/powercontext/issues/2444) + +# 概述 + +一个 `knowledge-base` 业务插件提供本地文档接入、解析、切块、版本管理及混合检索。一个普通 Scope 对应一个知识库, +插件只声明 `document` family,每个 Artifact 表示整篇文档。插件内的空间 Scope 组织多个知识库 Scope;空间身份、父子关系及 +用户默认空间仅存在插件表中,不向 core 创建空间记录或写入 Scope 父子关系。知识库本身不是 Artifact。 +插件附带原文和处理请求 Source 定义,后台复用框架 generate、Supervisor 和消费 cursor,不实现独立任务调度队列。 + +首期支持 SQLite、SeekDB、OceanBase,Markdown、独立图片 RapidOCR、规则切块、同步 chunk 编辑/移除、文档 forget、 +Dashboard、浏览器上传和空间内跨知识库检索。空间是组织与查询范围,不是另建租户或团队权限系统。 +这是依赖框架契约的目标设计,不表示当前实现 PR 已提供全部能力。 + +# 动机 + +业务描述和 SOP 需要可检索片段、原文溯源以及不会覆盖历史的人工修改。Scope 提供知识库的授权和组织边界;Artifact 管理 +整篇文档版本;插件存储保存大块正文和索引。复用现有调度、版本前置条件与生命周期,可以减少知识库专有的运行机制。 + +# 使用层说明 + +1. 用户选择插件内空间;界面未选择时先读取自己的默认空间,再固定 space_scope_id 发起创建。管理员创建知识库 Scope 并保存配置。 +2. 用户通过 Dashboard 上传文件,或导入 Server 可访问的本地目录;可在空间内选择多个知识库联合检索。 +3. 插件保存不可变原文快照、登记 Source,并提交处理请求 Source;导入接受不等于解析完成。 +4. 框架异步调用 generate,处理 Markdown/图片,准备完整 chunks 和索引,再发布 document。 +5. 内容变化或显式重解析沿用 document ID;失败保留已有版本。状态区分待处理、最近失败、已发布及冲突/跳过。 +6. 用户同步编辑或移除 chunk,document revision 增加,core 在同一事务记录 lineage_only 成功操作 Source。 +7. 文档软删除使用通用 forget:变为 deprecated、退出正常检索,不增加 revision。正文、原文和历史保留。 + +首期不承诺运行中撤销,不提供 Source 删除、知识库级联删除、物理擦除、独立任务队列、在线换模型、 +跨空间聚合、生成式问答、空间嵌套/迁移/删除、团队成员或权限继承、持续目录同步或远端 runner。PDF/Office、复杂场景图、 +Markdown 内嵌图片 OCR、语义/LLM 切块和几万篇规模评测不在首期。 +恢复/合并方法首期不实现,对应通用入口按框架返回不支持;不能把导入当作恢复操作。 + +# 参考层说明 + +## 1. 插件、Scope 与框架依赖 + +遵循 [#2549](https://github.com/oceanbase/powercontext/pull/2549) 的单 family、Source 定义、writer、模型和存储契约, +以及 [#2579](https://github.com/oceanbase/powercontext/pull/2579) 的插件路由和 Scope 管理方向。 +依赖 [#2659](https://github.com/oceanbase/powercontext/pull/2659) 的框架契约及 +[#2660](https://github.com/oceanbase/powercontext/pull/2660) 的实现;先合入框架依赖,再合入知识库实现,旧 master 不足以运行全部功能。 +三后端存储供应、原始编辑载荷证据、事务内鉴权、治理并发校验及 Dashboard 通用 Artifact 转发由框架 PR 提供,知识库 PR 不携带这些改动。 +插件只消费公开契约,管理自己的表与索引、快照、解析、检索、业务 API 和页面。初始化使用 setup,首期不依赖 upgrade 钩子。 +[#2617](https://github.com/oceanbase/powercontext/pull/2617) 仅是后续持续采集的接入方向,不是本地显式导入的前置条件。 + +| 对象 | 模型 | +| --- | --- | +| 空间 Scope | 插件内逻辑 Scope,space_scope_id;无 core Scope 记录 | +| 知识库 Scope | 普通 core Scope,scope_id;首期唯一归属于一个空间 | +| 插件 | knowledge-base,单个安装目录,单个 document family | +| 原文 | knowledge-base-file Source,固定快照及原路径 | +| 处理请求 | knowledge-base-process Source,固定原文引用、配置、base 和请求身份 | +| 文档 | document Artifact,整篇文档的有界 manifest | +| chunk | 插件存储中的不可变结果,不是独立 Artifact | + +```mermaid +flowchart TD + P[Plugin-local space Scope] --> M[Plugin-only membership] + M --> S[Core Scope: knowledge base] + S --> C[Plugin configuration] + S --> O[Original Source and immutable snapshot] + O --> R[Processing request Source] + R --> G[Framework generate and cursor] + G --> I[Immutable chunks and indexes] + I --> D[document Artifact revision] + U[Synchronous chunk edit] --> D + D --> Q[Retrieval and exact chunk reads] +``` + +通过 ctx.scopes.create 创建 Scope,沿用 server.admin 要求;同完整创建身份派生的幂等键返回同一 Scope。框架的插件外部引用标识创建归属。 +标题/摘要使用 Scope 字段,业务配置使用插件表。Scope 创建成功但配置失败时,重试补齐配置;配置未就绪时拒绝导入。 +授权沿用 Scope read/contribute、Artifact write 及框架版本校验,插件接口不得将 Scope 贡献权限等同于任意文档修改权限。 + +插件 inputs 只订阅处理请求 Source,原文不会重复触发本插件生成。知识库 Scope 按部署约定专用,不启用其他内容消费者; +不宣称 Source 类型名称提供框架级排他隔离,其他消费者的准入能力不属于本插件的保证。 + +### 1.1 空间 Scope 与默认范围 + +space_scope_id 和 core scope_id 是不同命名空间,接口必须区分,不能把空间 ID 传给 core Scope API。界面称“空间”, +Workspace 可以是英文展示词,不是新增 Artifact family 或另一套 core 领域模型。首期仅一级空间、每库一个归属。 +知识库与空间的归属保存到插件 scope 配置行,带索引的 space_scope_id 不写入 core parent_scope_id 或 context_references。 + +插件按已认证的 ctx.caller.principal_id 保存默认空间。首次解析默认空间时,幂等创建个人空间及默认映射;并发请求返回同一空间, +这不创建 core Scope 或授予贡献权限。用户可以把自己可见的空间设为默认。空间记录个人所有者或团队类型,团队类型仅是组织标签。 +显式空间创建/改名、知识库创建/改名沿用 server.admin;个人默认空间的自动登记与修改自己的默认映射不需要创建 core Scope 的权限。 + +个人空间对所有者可见;有权读取其中知识库的用户可看到包含它的空间;管理员可以管理空空间。列表和计数仅包含调用者可读的知识库。 +归属不授予 Scope read/contribute 或 Artifact write,也不产生权限继承、租户隔离和级联删除。 +默认空间不可见时,隐式知识库列表和检索仍返回错误;GET /me/default-space 允许读取当前用户自己的已保存 ID/version,并返回 available=false, +GET /spaces 独立列出其他可访问空间。用户显式选择有效空间,以已保存的 version 更新默认值;不自动选库或改变查询范围。 +没有可读知识库时返回空结果,不回退全局搜索。首期不支持变更知识库所属空间或删除空间,避免隐含跨库数据迁移。 + +创建知识库的 POST 必须显式携带非空 space_scope_id;缺失或为空返回 422,服务端不在创建或重试时解析默认空间。 +Dashboard/其他调用者先读取默认空间(或使用用户选择),再固定空间 ID、请求键及创建参数;自动重试原样复用,默认空间切换不改变目标。 + +完整创建身份为 (ctx.caller.principal_id, space_scope_id, idempotency_key)。插件对该元组做带版本的规范化编码和摘要,作为 +ctx.scopes.create 的幂等键;core 自己添加插件名前缀。不能只把客户端裸请求键传给 core,也不把空间关系写入 core Scope 字段。 +同完整身份返回同一 Scope,插件已有归属必须匹配;不同调用者或空间即使裸键相同也是不同创建,不得复用或迁移已有 Scope。 + +验证空间及权限后,幂等创建 core Scope,再在插件事务内保存唯一归属和配置;全部完成后才报告可用。 +core 创建成功而插件事务失败时,使用固定空间和完整创建身份补齐;归属/配置不完整的 Scope 不参与空间查询,也不可导入。 +查询和知识库列表仍允许省略空间并使用当前默认值;创建的固定目标不随这些读取规则变化。 + +## 2. 本地存储、身份与请求接受 + +OpenDAL fs 管理 Server、导入进程和 Worker 均可访问的本地快照目录。原路径只作出处,解析只读快照。 +先校验规范化根目录的包含关系,再从文件系统根开始,使用目录描述符和 O_NOFOLLOW 逐级打开所有目录,包括允许根目录的祖先, +最后相对于已绑定目录打开文件。不能只 resolve 后重新打开完整根路径,否则校验后替换根目录或祖先为符号链接仍可越界。 +先写快照,对实际字节计算 SHA256,再登记 Source。mtime/size 不能代替 hash。 + +```text +file_key = (scope_id, collection_id, normalized_relative_path) +document_id = stable_hash(identity_version, file_key) +source_key = stable_hash(source_schema_version, file_key, raw_content_digest) +request_key = (scope_id, idempotency_key) +result_id = digest(canonical complete chunk result and provenance) +``` + +Server 目录的 collection 从存储别名和规范化根目录派生;浏览器上传使用知识库内稳定的上传 collection,不能按批次重新生成。 +浏览器提交文件名或相对路径,不声称掌握客户端绝对路径。记录 origin_kind、collection 和 relative_path,上传字节仍由 OpenDAL fs +落到共享可访问的 Server 本地快照目录。两种来源的同名文件默认独立;同一 collection 同相对路径沿用相同逻辑文件身份。 +上传使用有界二进制请求及请求键,归一化相对路径并拒绝越界;不将文件正文放入 JSON Source。保存完整身份以检查 hash 碰撞。 +同文件跨知识库 Scope 是独立逻辑文档;空间 ID 不进入 file_key,实际数据归属仍是知识库。 +同路径同字节复用原文 Source;变化产生新原文 Source,但 document ID 不变。改名视作新文件,文件消失不自动 forget。 + +每次处理请求固定 original_source_ref、snapshot_ref、处理配置、独立模型指纹、base_revision、manual_edits_policy 及请求摘要。 +已有文档固定接受时的 revision;首次生成使用明确的无 head 前置条件。普通重复导入有可用同内容/配置结果时直接复用; +显式重解析或失败重试使用新的请求键,即使原文相同也产生新的处理请求 Source。人工修改不被普通导入覆盖;丢弃修改必须显式 discard。 + +插件资源行先保存有界、不可变的接受参数,再幂等登记原文和处理请求 Source,最后记录实际 SourceRef。 +框架可能在插件确认字段写入前调用 generate,因此执行依据是已提交请求 Source 和固定接受参数,不等待额外的 ready 标记。 +只在处理请求 Source 确认持久化后返回 accepted。任一步响应丢失,用相同请求键及参数补齐登记;相同键不同参数返回冲突。 +插件登记不是任务队列,未成功登记 Source 的请求不承诺后台执行;调用者可重试原请求。未引用快照不形成可检索内容。 + +目录导入按有限文件批次调用同一逐文件接受流程,返回逐项回执;枚举完成不是全部解析完成。首期不保存独立目录扫描任务, +大目录通过客户端分批提交,失败后重复枚举仍按文件身份及请求键去重。 + +## 3. 解析、切块与原文溯源 + +Markdown 使用 markdown-it-py 提取标题、段落及来源位置。独立图片使用 RapidOCR + ONNX Runtime,保留文本框、顺序、 +置信度及坐标变换,整理成 Markdown 后走相同切块流程;不猜测复杂表格或流程图语义。内嵌图片只保留引用。 + +默认按标题和内容块切分,超长块递归按段落/句子切分。代码、表格和 SOP 步骤尽量完整,超长表格保留表头;overlap 仅用于 +被拆开的长段落。max_chunk_chars/overlap_chars 可配置,1200/100 只是初始验证值。完整 embedding 输入包含标题等附加文本, +必须按模型 token 上限检查,不能静默截断。UTF-8、图片尺寸、解码及 OCR 无文字等错误明确报告。 + +解析器只返回 Markdown、locators 和警告,不管理 cursor 或 Artifact。后续格式通过内部解析器注册表扩展,不新增插件类型。 + +处理请求记录原文 SourceRef 和快照引用。generate 的直接 source_refs 只引用当前窗口的处理请求;已消费原文未必出现在窗口中, +不能绕过 writer 的引用限制。Document manifest 保留原文引用,形成“document → 请求 Source → 原文 Source”的可检查溯源链, +不宣称原文总是每次 revision 的直接 lineage。同步编辑保留当前 lineage 及原文定位,并新增 core 的成功操作 Source。 + +## 4. 插件存储与模型 + +ctx.storage 遵循 #2660:SQLite 为独立文件,SeekDB/OceanBase 的插件专属库为 powercontext_plugin_knowledge_base,建库权限不足时由部署者预建。 +SQL 命名空间按插件名隔离,不按 core 数据库名隔离;同一 SQL tenant 中的独立部署必须隔离插件库/账号,仅更换 core 库名不足以隔离插件数据。 +插件通过 backend 选择迁移及全文/向量实现,不修改现有核心表。原文件仍在 OpenDAL 本地目录,不塞进 SQL 正文列。 + +| 插件业务表 | 字段及约束 | +| --- | --- | +| pc_knowledge_base_spaces | 主键 space_scope_id;title、kind、personal_owner_id、created_by、creation_key、request_digest、version;个人所有者及创建键唯一 | +| pc_knowledge_base_space_defaults | 主键 principal_id;space_scope_id、version;每个已认证用户一个默认映射 | +| pc_knowledge_base_resources | 主键 (scope_id, resource_kind, resource_key);固定 scope/document/request;scope 配置行唯一并带 space_scope_id 索引;有界 payload、request_digest、source_ref、document_id、最近处理观察及时间 | +| pc_knowledge_base_chunks | 主键 (scope_id, document_id, result_id, chunk_id);ordinal 同结果唯一;text、text_length、content_digest、heading_path、source_locator、origin、edit_metadata | + +共四张插件业务表;空间与默认映射不伪造 core scope_id。空间引用在插件事务内验证;默认更新使用版本前置条件,个人空间/默认映射 +初始化先幂等登记确定身份的个人空间,再登记默认映射;唯一键与重试补齐中断,已有默认映射不被初始化覆盖。显式空间创建键按创建者限定,同键不同参数冲突。 +知识库归属复用 resources 的 scope 行,不再增加关系表;不修改核心表。 + +Resources 是固定用途登记,不是通用任务框架:scope 行保存配置/版本;document 行保存稳定文件身份;request 行保存不可变接受参数、 +幂等回执及最近尝试结果。它没有租约、执行 token、领取队列或清理 cursor,也不复制 Artifact 的发布指针与生命周期。 +请求结果可以引用精确 revision 供对账,成功事实仍以 core 为准。全文/向量辅助结构属于派生索引,不计入业务表数。 + +Document content 只保存标题、原文 SourceRef、文件身份、result_id、chunk 数量/摘要、配置指纹、人工修改标记和 applied_request_id。 +正文及无界 chunk ID 列表不进入 manifest。结果 ID 来自规范化完整结果,不能用尚未提交的 revision 号作为存储键。 + +模型由插件独立配置和创建客户端,密钥来自部署配置,不存入 Source/Artifact。不自动跟随 ctx.models 或 Server 模型变化。 +同一插件首期使用一个固定 embedding profile,包含模型身份/版本、维度、完整输入规则与规范化版本;维度相同不等于模型相同。 +已有索引与部署 profile 不一致时拒绝向量查询及新发布,明确要求受控迁移,不自动覆盖配置或混用向量。 +首期不提供在线换模型操作;同 profile 的索引修复从已提交 chunks 重建。解析/切块配置可按 Scope 保存并固定到请求。 + +## 5. 框架消费、状态与失败处理 + +声明 on_sources 和处理请求 inputs,复用框架 generate/Supervisor、Scope binding、消费 cursor、租约 fence 和失败退避。 +没有独立 jobs 表,也没有插件自建 Worker 调度器。每次调用最多一个 writer.transaction,Artifact、cursor 和调用确认在 core 同事务提交。 +配置小的 max_sources 和有界文件大小,避免一次窗口包含过多 OCR/模型工作;不承诺任意大小文件都能在框架时限内完成。 + +| 事件 | 行为 | +| --- | --- | +| 请求已提交、尚无结果 | 展示待处理,不虚构百分比 | +| 文件格式错误、无文字等确定输入错误 | 保存有界失败观察,跳过该输入;窗口正常确认后继续后续请求 | +| 数据库、模型服务、索引持久化故障 | 抛出异常,core 回滚,cursor 不前进,由框架退避重试 | +| 固定 base 已变化或文档已非 active | 记录冲突/跳过,不以新 base 自动覆盖;用户重新提交请求 | +| core 发布完成 | 从 manifest/历史中的 applied_request_id 判定已发布及精确 revision | +| 发布成功但插件状态未更新 | 根据 core 对账,不能仅凭缺少状态把它判为失败或重复发布 | + +状态读取优先核对固定 base+1 revision 及当前 manifest 的 applied_request_id;没有本请求的发布证据,且当前 revision 已越过固定 base +或文档已非 active 时,将 pending/processing 推导为 failed 并返回冲突原因。这样即使 cursor 已提交而随后的冲突观察写入失败, +仍能在查询、同键重放及重启后恢复终态;不依赖重新投递,不新增表,也不自动换 base。 + +文档当前可用 revision 与最新处理请求状态独立展示:重解析失败不会把旧有效版本改成不可用。 +资源表的失败是最近尝试观察,不等于 cursor 已确认;不能仅凭插件状态推进消费或跳过尚未证明完成的 core 写入。 +基础设施持续故障可能阻塞该 Scope 的后续窗口,这是复用框架的边界,不承诺独立逐文件无限制并发或死信队列。 +确定输入失败的人工重试使用新请求;旧 Source 不修改。处理请求是意图,不是 lineage_only 成功证据。 + +首次发布前确认无 head;更新核对固定 base、原文、人工修改策略和生命周期。框架重试也不能改写请求中的 base 或丢弃策略。 +事务外准备后发生编辑/forget 时,发布使用框架的 revision/治理并发校验;不能自动恢复 deprecated 文档。 +框架跟踪 writer 读取时观察到的治理版本,并在发布时校验;插件向 put 传 expected_revision,不扩展 expected_state_version 参数, +也不用插件内存锁替代框架保证。生命周期竞争保护属于 #2660 的验收前提。 + +## 6. 发布与索引一致性 + +1. 在 core 事务外完成解析、向量计算;持久保存不可变 chunks 及其全文/向量索引,校验数量、摘要和可读取性。 +2. 在 writer.transaction 中校验前置条件并 put manifest,引用 result_id 与索引指纹;generate 同时提交 cursor,用户编辑同时提交证据。 +3. core 提交失败时准备结果不可见,旧 manifest 和旧索引保持可用;重试不原地覆盖旧结果。 +4. 检索候选必须匹配当前 active document 的 result_id 和 profile;过滤后补取,未发布结果不能占满 top-k。 + +索引按结果隔离,不以单一 document_id 覆盖旧有效候选。模型和文件计算不进入短事务,不新增 core Scope 行锁协议或跨库事务。 +历史 chunk 通过精确 revision 的 manifest 定位,不依赖当前搜索索引。仅保留未引用的准备数据不会改变可见结果,永久清理另定。 + +## 7. 同步 chunk 编辑与软删除 + +编辑和移除 chunk 通过通用 Artifact PUT 交给 update_artifact,同步完成;不是插件 API 写入,也不排队。 +插件的编辑载荷定义为有界 chunk 变更描述,包含 chunk ID/新文本或移除动作;服务端读取对应基准版本,合成完整结果, +只由服务端产生 manifest,拒绝客户端指定任意 result_id 或原文归属。Core 允许插件规范化提交内容;不开放任意 manifest 替换。 + +带 If-Match,先生成新结果及索引,再恰好一次 put(expected_revision=...)。Core 同事务保存提交的编辑载荷作为 lineage_only +Content Source,document revision 增加。修改保留逻辑 chunk ID 和原定位,标明人工文字并非原文逐字引用;移除最后一块允许零块版本。 +新正文可能出现在编辑证据中,沿用现有权限,不宣称 forget 会抹除这些证据。 + +模型调用受同步时限及编辑大小限制,失败不发布半成品。提交响应未知时重新读取当前 revision/lineage;原 If-Match 不再满足时 +不能盲目重试。通用 PUT 不另承诺幂等键或恰好一次 HTTP 执行。 + +整篇文档使用通用 forget,调用 forget_artifact,恰好一次 set_lifecycle(deprecated)。这只是生命周期变更:不追加内容 revision, +不生成本插件专有的删除证据,也不物理清理正文/索引/快照。正常检索及 Prepared Context 排除 deprecated 文档。 +通用 get、历史和证据沿用框架可见性;授权的精确 chunk 读取与 rendering 可展示保留版本,不返回“所有内容均已删除”的承诺。 +生命周期以 core 为准,不在插件表另建 document 墓碑;物理索引清理不是 forget 生效条件。 + +已忘记文档不因重复导入、重解析或迟到处理自动恢复;同步编辑仅允许 active 文档。尚无 document Artifact 时没有 forget 目标, +首期不支持运行中撤销导入,不创建空 Artifact 或删除 revision。原文 Source 不提供删除接口;不提供知识库级联删除, +不能将物理删除 Scope 当作软删除。重复 forget 使用框架现有前置条件及 no-op 语义,不启动清理任务。 + +## 8. 接口、检索和权限 + +令 `P=/v1/plugins/knowledge-base/api`,`D=/v1/scopes/{s}/artifacts/document/{d}`。插件路由通过 PluginApi/Route 声明, +使用框架支持的响应能力;以下 accepted 是业务状态,不假定插件接口已经支持自定义 HTTP 202。默认成功 JSON 可返回 200。 + +| 操作 | 接口和返回 | +| --- | --- | +| 空间列表/创建/改名 | GET/POST `P/spaces`、PATCH `P/spaces/{space_scope_id}`;插件内逻辑空间,写入带幂等键或版本前置条件 | +| 我的默认空间 | GET/PUT `P/me/default-space`;当前用户的 space_scope_id 和 version;GET 另返回 available;不接受替他人设置的 principal_id | +| 知识库列表 | GET `P/knowledge-bases`,space_scope_id 可省略,使用当前默认空间 | +| 创建知识库 | POST `P/knowledge-bases`,必填 space_scope_id 和 idempotency_key;按调用者+空间+请求键幂等创建,返回两类 ID | +| 知识库改名/配置 | PATCH `P/scopes/{s}/settings`;名称/摘要复用 ctx.scopes.update,切块配置存插件;分别使用版本前置条件 | +| 导入文件 | POST `P/scopes/{s}/imports`,有界 paths、idempotency_key;返回逐文件请求 SourceRef | +| 浏览器上传 | POST `P/scopes/{s}/uploads`,二进制正文,query 携带 collection、relative_path、idempotency_key;复用同一接受流程 | +| 文档列表 | GET `P/scopes/{s}/documents`,分页合并已发布概览和未发布请求观察;从 core 核实 revision/生命周期 | +| 重解析/重试 | POST `P/scopes/{s}/documents/{d}/reprocess`,base、固定配置、原文引用、新请求键 | +| 处理状态 | GET `P/scopes/{s}/imports/{request_id}`,接受记录、最近尝试及可核实的发布结果 | +| 文档概览/历史 | 通用 Artifact GET/revisions,返回有界 manifest | +| 编辑/移除 chunk | PUT `D`,If-Match,同步返回新 document revision | +| 文档软删除 | POST `D/forget`,revision 和 state_version;遵循框架响应 | +| chunk 目录/正文 | GET `P/scopes/{s}/documents/{d}/revisions/{rev}/chunks`,追加 `/{chunk_id}` 获取正文 | +| 批量 chunk | POST 上述目录路径追加 `/batch-get`,有界 chunk_ids | +| chunk 混合检索 | POST `P/scopes/{s}/retrieve`,返回精确 revision、chunk ID、分数及出处 | +| 空间内跨库检索 | POST `P/retrieve`,space_scope_id、scope_ids 均可省略;按下文解析范围,返回带空间和知识库 ID 的命中 | +| 展示与通用检索 | render_artifact/search_artifacts;接入通用 search 和 Prepared Context | + +创建知识库、导入、编辑、forget、检索示例: + +```json +{"space_scope_id":"space-team","title":"Operations SOPs","summary":"Runbooks","idempotency_key":"kb-01","chunker":{"max_chunk_chars":1200,"overlap_chars":100}} +``` + +```json +{"root":"/work/docs","paths":["restart.md"],"idempotency_key":"import-01"} +``` + +```json +{"scope_id":"scope-ops","items":[{"document_id":"doc-1","request_ref":{"source_type":"knowledge-base-process","source_id":"request-01"},"status":"accepted"}]} +``` + +```json +{"content":{"operation":"edit_chunk","chunk_id":"chunk-1","text":"Check replica health before restarting."}} +``` + +```json +{"content":{"operation":"remove_chunk","chunk_id":"chunk-2"}} +``` + +```json +{"revision":3,"state_version":0} +``` + +```json +{"query":"How should I restart a follower?","mode":"hybrid","limit":10} +``` + +```json +{"hits":[{"space_scope_id":"space-team","scope_id":"scope-ops","document_ref":{"family":"document","artifact_id":"doc-1","revision":3},"chunk_id":"chunk-1","score":0.82,"source_ref":{"source_type":"knowledge-base-file","source_id":"original-1"},"locator":{"heading_path":["Operations","Restart"]}}]} +``` + +Chunk API 精确读取指定 revision,不回退当前版本;目录分页不返回正文,单块/批量返回有界正文及出处。Batch-get 拒绝重复 ID、 +缺失 ID 和超限响应,不静默截断;每次先检查 Scope、document 权限和归属。专属存储跨 Scope 共享,不能只凭 chunk ID 读取。 + +通用 search 保持框架的 Artifact 引用格式;插件专用 retrieve 提供 chunk 结构。Prepared Context 使用 SearchHit 的片段与 locator, +并遵守字节预算。正常检索先过滤非 active、旧结果及未授权对象,再补取候选;不支持的 filter/rerank 返回明确错误。 +Python/HTTP 复用相同业务语义。通用 search/Prepared Context 保持原有 Scope 契约;空间聚合通过插件 retrieve 提供。 + +### 8.1 空间范围解析与跨库检索 + +1. 未传 space_scope_id 时,解析当前用户默认空间;有值时校验空间可见性。显式 scope_ids 必须全部属于该空间,不能据此反推另一空间。 +2. 未传 scope_ids 时,取空间中调用者可读的知识库;显式空列表表示不查任何库。显式指定无权访问或空间外的库则整次失败, + 不静默忽略。限制库数、请求大小和总返回数量;默认空间、归属和配置需完整,不扩大为全局搜索。 +3. 选定范围内做全文/向量召回与统一融合。固定 embedding profile 时可复用查询向量;使用可比较的评分/统一候选排序, + 不能直接比较各库独立 BM25 的原始分数。先按 Scope 约束候选,再核对 core 权限、active、当前 result_id 和 profile,并补取。 +4. 每条命中携带 space_scope_id、scope_id、document_ref(含 revision)、chunk_id、分数及出处;去重使用完整归属和结果身份, + 相同文件跨库导入不合并其证据。所有精确 chunk 读取仍以知识库 Scope 和 revision 定位。 + +```json +{"space_scope_id":"space-team","scope_ids":["scope-ops","scope-dev"],"query":"How should I restart after a release?","mode":"hybrid","limit":10} +``` + +仅发送 query 表示查询默认空间内所有可读知识库;传 space_scope_id 不传 scope_ids 表示查询该空间的可读知识库。 +聚合路径复用单库权限、生命周期及版本过滤规则,但不依赖嵌套 HTTP 调用。后端故障明确返回失败,首期不承诺部分成功或跨库一致性快照。 + +## 9. Dashboard + +通过 PluginPages 提供知识库页面。界面包含空间/默认空间选择、知识库管理、Markdown/独立图片拖拽上传和本地目录导入、 +文档与请求状态、重试/重解析、分页 chunk 目录与正文、历史查看、同步编辑/移除、文档遗忘及空间内多库检索。 +空间和知识库列表均权限过滤;创建/配置管理要求 server.admin,内容导入要求 scope.contribute,编辑/forget 保留 artifact.write。 +创建按钮提交前固定所选/默认空间,重试保留原 space_scope_id 和请求键;用户切换创建目标是新的操作。 +首次导入前就确定有效切块配置,不引入上传后的独立待配置任务队列。 +异步读取固定发起时的空间、知识库 Scope、document/revision 和页面代次;切换后丢弃旧响应及错误。 +状态请求不能改用新选中的 Scope,旧列表/chunk 也不能覆盖新页面。默认空间失效时仍可用选择器显式选择其他空间。 + +文档页面同时展示“当前可用 revision”和“最近处理请求”;失败不隐藏旧有效内容。编辑成功显示新 revision,冲突要求重新读取, +响应未知时先核实结果。移除最后一块允许零块版本。Forget 文案为“移出检索”,历史仍可查看;不展示取消、恢复、整库删除或问答入口。 + +iframe 通过框架客户端调用本插件声明的业务路由。通用 document GET/PUT/forget 使用 #2660 的 +`client.artifacts(method, path, {scopeId, ifMatch, json})`,由宿主 `/dashboard/plugins/{name}/artifacts/{scope_id}/{artifact_id}` 转发。 +PluginPages 不声明 artifact_actions,插件不增加 /api/_artifacts 代理。框架限制所属 family 的操作,保持当前用户鉴权及 +If-Match/revision/state_version;插件 hook 继续校验知识库归属。 +不提供任意 URL 转发或会话凭据。最终仍进入通用 Artifact 操作和同一事务证据路径,不能用普通插件 writer 或异步编辑 Source 替代。 +上传按框架二进制上限分文件处理,服务端限制路径和文件大小,前端不把文件数当作解析完成进度。 + +## 10. 初始化、实施与验收 + +setup 调用插件自己的幂等初始化及 schema 版本检查;升级迁移由插件部署流程执行,多实例在插件库协调。 +历史 Artifact manifest 和旧 Source 定义仍需读取;版本不兼容时明确拒绝服务。首期重启部署,不依赖 upgrade(ctx) 钩子或热加载。 +备份必须同时包含 core、插件库和 OpenDAL 快照;core 的备份不自动覆盖插件数据。 + +#2659/#2660 提供存储供应、同步编辑证据、事务内鉴权、治理校验和 Dashboard Artifact 桥接。 +知识库 PR 不修改 core runtime/schema、框架 SDK、host、writer 或 Dashboard 桥接;仓库级集成仅包含插件目录、可选依赖、插件测试/CI 及使用文档。 +不另建知识库锁表、读取 guard 或独立队列。依赖未合入时,明确记录测试所用框架提交,不将隔离集成通过表述成当前 master 已兼容。 + +在 SQLite、SeekDB、OceanBase 分别验证: + +- 默认空间失去权限后仍可读取自己的配置并显式切换;隐式查询不自动回退。 +- 校验后允许根目录/祖先被替换为符号链接时拒绝读取;Dashboard 切库后的旧列表/状态响应不污染新页面。 +- cursor 提交后冲突观察写入失败,查询、重放和重启均可恢复失败终态。 + +- 插件内空间/默认映射并发初始化、默认修改冲突、唯一知识库归属、core 不记录空间父子关系。 +- 空间默认与显式查询、空列表、越权/空间外库拒绝、权限过滤计数、全局 top-k 及带 Scope 的精确引用。 +- Dashboard 沙箱通用操作转发、If-Match 保留、零 chunk 编辑、双状态展示和失败后的旧版本可用性。 +- 创建 Scope 的管理员权限、归属/配置补写、多 Scope 隔离和固定原文快照;两种导入来源的稳定身份及路径约束。 +- 创建缺少空间返回 422;core 成功/插件失败后切换默认空间,重试仍完成原空间归属;不同调用者/空间的裸键不碰撞。 +- 导入请求各提交边界的重试、同键冲突、内容去重、重复处理及确定失败不阻塞后续文件。 +- 框架窗口回滚/重新投递、失败观察与 core 成功对账;固定 base 冲突不能自动换基准。 +- Markdown、真实 RapidOCR、超长块、完整 embedding 输入预算、原文溯源与人工标记。 +- 同步编辑/移除的 revision 和 lineage_only 证据;零块结果、模型失败及响应丢失。 +- 编辑/forget 与 generate 竞争,不覆盖人工编辑、不恢复 deprecated;原文和历史保留。 +- 准备索引后 core 回滚,旧版本仍可检索;候选过滤、精确 chunk 读取、通用 search 和 Prepared Context。 +- 插件模型指纹不匹配明确失败,Server 主模型变化不改变插件向量空间。 + +固定 oceanbase/oceanbase 文档 commit 做 Markdown 小样本,配合真实文字图片;mock 不能代替真实 OCR 和三后端验收。 +#2441 覆盖空间/默认/接入配置,#2442 覆盖框架消费/解析/发布,#2443 覆盖跨库检索/编辑/Dashboard, +#2444 覆盖运行及验收;#2440 总跟踪。 +实现可集中在一个 PR,按模块提交并逐步验收;本文不新增 Issue。 + +# 缺点 + +复用 Source 窗口意味着基础设施持续失败可能阻塞同 Scope;同步编辑受接口时限约束。插件负责三后端索引、表迁移和备份, +不可变结果与保留历史增加存储。Forget 是退出检索而不是擦除或禁止历史读取;Source 保持不可变且可读。 + +# 设计理由与替代方案 + +- 插件内空间 Scope 组织知识库 Scope;关系与默认映射只存插件表,不使用 core 层级或隐含继承权限。 +- core Scope 对应知识库,与单 family 插件一致,无需知识库 Artifact 或跨 family 写入。 +- 复用 generate 和 cursor,使 Artifact 与消费进度在 core 原子提交;插件只记录接受及处理观察,不重复实现调度。 +- 同步通用 PUT 提供版本校验和成功操作证据;插件 API 写入不自动具有这些证据语义。 +- 原文 Source 与处理请求分开,使重解析不修改原始事实;直接 lineage 和原文溯源的职责明确。 +- 不可变 chunk 结果引用保证历史与失败恢复,不能用覆盖正文的方式实现编辑。 +- Forget 遵循框架生命周期,不增加 Source 墓碑、删除 revision 或全局读取拦截。 + +# 既有设计 + +基于 [插件接口 #2549](https://github.com/oceanbase/powercontext/pull/2549)、 +[插件 API #2579](https://github.com/oceanbase/powercontext/pull/2579)、 +[Source 身份](1400_source_definition_and_observation_model.md)、 +[处理监督](1515_artifact_processing_supervisor.md) 和 [Source/Artifact API](1437_source_artifact_rest_api.md)。 +未来持续采集可接入 [#2617](https://github.com/oceanbase/powercontext/pull/2617),采集 checkpoint 只代表接入进度,不能代表解析完成; +删除采集目标不删除已采集 Source 或 document。 + +# 待解决问题 + +实现时确定 SQL 类型与索引细节、OCR 模型版本、窗口和文件大小、同步编辑时限、API 字节上限;用真实后端验证框架的 +revision/生命周期竞争行为。三后端存储、编辑证据、Dashboard 转发依照 #2659/#2660 在合入后的 master 上完成验收,不将提案当成已发布功能。 +这些实现选择不改变 Source 不删除、forget 不增加 revision、同步编辑、固定请求 base 及独立模型的契约。 + +# 后续可能性 + +扩展 Word/PDF 解析、持续采集、跨空间查询、生成式问答、rerank、邻接上下文、运行中撤销及受控模型迁移。 +完整 Source 删除、历史访问撤销、知识库级联删除和独立长任务需要单独确认契约,不作为首期的隐含承诺。