diff --git a/.changeset/38538-knowledge-document-unit-skill-text.md b/.changeset/38538-knowledge-document-unit-skill-text.md new file mode 100644 index 00000000000..dbfc3991144 --- /dev/null +++ b/.changeset/38538-knowledge-document-unit-skill-text.md @@ -0,0 +1,12 @@ +--- +'@objectstack/skills': patch +--- + +docs(skills): the objectstack-ai skill teaches a knowledge source's document unit, `source.documentBy`, and that a mounted knowledge service is held until it is enabled + +Clause-②: no +Metadata-protocol: none + +`skills/objectstack-ai/SKILL.md`, Knowledge Sources: the key table lists `source.documentBy`, the single-valued `lookup` / `master_detail` field, listed in `metadataFields`, that links each indexed record to its parent record; setting the key rebuilds nothing, and listing the parent field in `metadataFields` for the first time rebuilds the source in place. One rule is added beside the corpus rules: an app that cuts its own passages writes each one as a child record and indexes the child object with `split: { strategy: 'whole' }` and `documentBy`. The index stays one record = one document, and `documentBy` is the unit a `granularity: 'record'` search counts in `topK` and groups by when no `groupBy` is sent. +The same section no longer reads as "a shipped source is searchable". A shipped item mounts the knowledge service, and the service is held until an embedder provider is saved, the deployment's store is mounted and the server restarts. Held sources are listed and never built or searched: `search_knowledge` is not offered, a search or a run-time save answers `503 SERVICE_UNAVAILABLE` naming the missing half, `os knowledge status` prints the state and why, and `os knowledge rebuild` exits 1 with it. Where an app ships no source, the remedy for a refused run-time save now includes saving an embedder provider and the restart after it. +The file's token ceiling moves from 7247 to 7499: restating clauses of the same file are deleted first, and the row moves by the measured remainder. diff --git a/content/docs/ai/knowledge-rag.mdx b/content/docs/ai/knowledge-rag.mdx index 4723a6d7213..c1f5293df30 100644 --- a/content/docs/ai/knowledge-rag.mdx +++ b/content/docs/ai/knowledge-rag.mdx @@ -167,6 +167,15 @@ const policies = await knowledge.search('parental leave', { }); ``` +A source whose records are parts of a larger document, such as the clauses of +a policy, can name that parent as its **document unit** (`source.documentBy`, +[below](#an-app-that-cuts-its-own-passages)). For such a source +`granularity: 'record'` with no `groupBy` answers one hit per parent record +(the best passage among that parent's records), and `topK` counts parents. The +parent's id is in the hit's `metadata`, under the field `documentBy` names. A +record with no parent is still its own hit, and `granularity: 'chunk'`, an +explicit `groupBy` and every source without `documentBy` answer as before. + A record-level hit is checked against its record as it reads now: a record deleted, out of the source's `where`, or changed since it was indexed is not returned (its index entry is repaired in the background, unless the source @@ -236,8 +245,8 @@ An AI client searches through one tool, `search_knowledge`, defined once by the |:--|:--| | `query` | What to look for, in plain words. Required. | | `sourceIds` | Only these sources, by the ids every answer lists in `sources`. Omit to search them all. | -| `topK` | At most this many hits, 1 to 20, default 5. Counts records or groups in the two modes below. | -| `granularity` | `"chunk"` (the default): one hit per passage. `"record"`: one hit per record, its best passage. | +| `topK` | At most this many hits, 1 to 20, default 5. Counts records (parent records, for a source that declares `documentBy`) or groups in the two modes below. | +| `granularity` | `"chunk"` (the default): one hit per passage. `"record"`: one hit per record, its best passage. For a source that declares a document unit (`documentBy`), `"record"` answers one hit per parent record instead, which the hit carries as `groupValue`; a record with no parent is its own hit. | | `groupBy` | One hit per value of a field the source lists in `metadataFields`, such as `policy`: "which policies", not "which clauses". Implies `"record"`; refused with `"chunk"`. A source of indexed records that does not list the field answers nothing for that call; a file or web source still answers, one hit per document, with no `groupValue`. | | `scope` | Only inside these records: a list of `{ object, ids }`, as `KnowledgeSearchOptions.scope` reads it above. At most 10 entries of at most 500 ids. | @@ -317,7 +326,7 @@ source: { } ``` -Keeping the index text on the same record, rather than in a second object with one row per unit, means a hit names the record people open, the search re-checks that record as the asking user, and `where` and `metadataFields` read the record's own fields. There is no second object to secure, keep in step with the first, or delete with it. The field is indexed like any other content field: its text is sent to the embedder, and a credential or internal field is never indexed. +Keeping the index text on the same record, rather than in a second object with one row per unit, means a hit names the record people open, the search re-checks that record as the asking user, and `where` and `metadataFields` read the record's own fields. There is no second object to secure, keep in step with the first, or delete with it. An app that already keeps one row per unit indexes those rows instead ([below](#an-app-that-cuts-its-own-passages)). The field is indexed like any other content field: its text is sent to the embedder, and a credential or internal field is never indexed. **An optional top heading names the document.** The text the embedder sees is the passage: neither the record's title nor the department that owns it is in it unless the text says so. Starting the index text with one top-level heading that names them, with every unit one level below it, makes each passage's heading path begin with that heading: @@ -340,6 +349,26 @@ Article 7 is then one passage that begins `Group travel policy · Finance depart Moving an existing source to this shape changes its `content` and `split`, so its index is rebuilt once ([below](#when-the-model-or-the-split-changes-rebuild)), and the rebuild sends the whole source to the embedding provider again. +### An app that cuts its own passages + +Some apps already keep each unit as a record of its own: a clause object under a policy, an article under a contract, a section under a manual. Such an app has cut its own passages. Index the child object, one record per passage, and name the link to the parent as the source's **document unit**: + +```ts +source: { + kind: 'object', + object: 'policy_clause', + content: ['body'], + metadataFields: ['policy'], // the lookup from a clause to its policy + documentBy: 'policy', // the document unit: one policy is one document + split: { strategy: 'whole' }, // one clause record is one passage +} +``` + +- **Each passage is a child record, indexed with `split: { strategy: 'whole' }`.** The app's cut is the passage, and the service does not cut a clause again. A clause over the embedder's input budget is removed from the index and reported ([above](#splitting-documents-into-passages)), so keep each one within it. +- **The index stays one record = one document.** `documentBy` changes nothing in it: a hit still names the clause record, and the search re-checks that record as the asking user. Setting or changing `documentBy` rebuilds nothing; listing the parent field in `metadataFields` for the first time rebuilds the source in place, as any new metadata field does. +- **`documentBy` is the unit a search counts and groups by.** `granularity: 'record'` with no `groupBy` answers one hit per parent, the best passage among that policy's clauses, and `topK` counts parents. The parent's id is the hit's `metadata[documentBy]`, and also `groupValue` on a `search_knowledge` hit. A clause with no parent is its own hit, two sources never share a parent, and an explicit `groupBy` takes its place for that search. +- **It must be a single parent link.** `documentBy` names a `lookup` or `master_detail` field of the indexed object that is listed in `metadataFields`, is not `multiple: true`, and is neither a credential nor an `internal` field. Any other field is refused, each time naming the fix: by the parse when `metadataFields` does not list it, at a save in Studio, the metadata API or MCP, and by `os validate` and the publish gate (`knowledge-source-document-by-not-parent-link`). Studio's source form offers the key as **Document unit**. + ## Embedder input limits Every embedding model accepts a text up to some length, counted in tokens. A provider that is sent a longer text truncates it **without an error** — the tail is never embedded, so a search for it finds nothing. `@objectstack/embedder-openai` refuses such an input instead: diff --git a/content/docs/protocol/knowledge.mdx b/content/docs/protocol/knowledge.mdx index 51dad452a8f..05496cdd35a 100644 --- a/content/docs/protocol/knowledge.mdx +++ b/content/docs/protocol/knowledge.mdx @@ -209,6 +209,29 @@ The mode is read by search alone: it enters neither the index fingerprint nor the recipe hash, so changing it rebuilds nothing, and a refused source still builds and syncs its index. +**The document unit: `source.documentBy`.** An app that cuts its own passages +writes each one as a child record (a clause of a policy, an article of a +contract) and indexes the child object with `split: { strategy: 'whole' }` +([§3.5](#35-the-split)), so each record is one passage. `documentBy` names the +field that links each of those records to its parent record, and that parent +is the source's document unit: the unit a record-level search counts and, with +no `groupBy`, groups by ([Records, not passages](#records-not-passages)). The +index does not change. It stays one record = one document (ADR-0161 K1): a +hit names the child record, and the permission re-check and the record sync +read that record. The key enters neither the index fingerprint nor the recipe +hash, so setting it rebuilds nothing; listing the parent field in +`metadataFields` for the first time does, as any new metadata field does. + +The field must be one of `metadataFields`, since grouping reads the value the +index stores beside each passage: the parse refuses one that is not listed. +It must also be a single-valued parent link: a `lookup` or `master_detail` +field the object declares, with a readable `reference`, not `multiple: true`, +and neither credential-class nor `internal`. Any other field is refused at a +save (`VALIDATION_ERROR`, 400, naming the field and the fix), and by +`os validate` and the publish gate +(`knowledge-source-document-by-not-parent-link`), which judge a source a +package ships or code registers. + **What mounts the service.** A stack that ships a `knowledge_source` — any item in its `knowledgeSources`, at the top level or in a package — is served the knowledge service and the deployment's store, exactly as @@ -406,6 +429,16 @@ for `topK × 5` passages, for every caller, and refilled once at `topK × 20` when fewer than `topK` records survive; that is the cap, and fewer records within it means fewer hits, logged at `debug`. +A source that declares a document unit (`source.documentBy`, §3.1) answers +parents instead. Under `granularity: 'record'` with no `groupBy`, its hits are +grouped by the parent each one carries in `metadata[documentBy]`: one hit per +parent, the best-scoring passage among that parent's records, and `topK` +counts parents. A record with no parent (the value is absent, null or empty) +is its own hit, never dropped. The group is keyed with the source, so two +sources never share a parent, however equal their parent ids. A +`search_knowledge` hit carries the parent as `groupValue` too. A source +without `documentBy` answers one hit per record, as above. + `groupBy` names a metadata field and groups by its value instead — "which policies", not "which clauses" — with the best hit of each group and the value in the hit's `metadata`; it implies record granularity, and is refused @@ -413,7 +446,8 @@ with `granularity: 'chunk'` (`VALIDATION_ERROR`, 400, as are an empty `groupBy` and an unknown `granularity`). Hits with the same value are one group whichever source they come from. An `object` source that does not list the field in its `metadataFields` is not searched, and a record with no value for it is -not answered: a hit is never answered ungrouped. +not answered: a hit is never answered ungrouped. An explicit `groupBy` takes +the place of a source's `documentBy` for that search. Each record-level hit is checked against its record as it reads now, one system-context read per source of only the records about to be answered @@ -669,7 +703,10 @@ one passage per unit that fits, and no passage ever holds two units; it needs no `minChars` setting. Text stored in another shape is indexed through a derived field on the same record that carries that rewrite, optionally under one top heading that names the document; the recipe is in -[the knowledge guide](/docs/ai/knowledge-rag#one-passage-per-unit). +[the knowledge guide](/docs/ai/knowledge-rag#one-passage-per-unit). An app +that keeps each unit as a record of its own indexes those records with +`whole`, and names their parent as the source's document unit +(`source.documentBy`, §3.1). **`whole` over the limit.** A `whole` document whose estimate is over the 80% budget is **removed from the index and reported, never truncated**: diff --git a/scripts/check-skills-token-ratchet.mjs b/scripts/check-skills-token-ratchet.mjs index c05e6a5d1fb..7e38540d7a4 100644 --- a/scripts/check-skills-token-ratchet.mjs +++ b/scripts/check-skills-token-ratchet.mjs @@ -570,7 +570,57 @@ export const CEILINGS = new Map([ // (the registry's 409 names both doors), and "Rerank and hybrid weighting" // became one clause. Banked at the measurement. // ceil(($(wc -c < skills/objectstack-ai/SKILL.md) - 27) / 4) == ceil(28987 / 4) - ['skills/objectstack-ai/SKILL.md', 7247], + // 7247 -> 7401 (2026-12-01, the document unit): the Knowledge Sources section + // did not name `source.documentBy`. It gains the key's row (the single-valued + // `lookup` / `master_detail` parent link, listed in `metadataFields`; any + // other field is refused; setting it rebuilds nothing) and one rule beside + // the corpus rules: an app that cuts its own passages writes each as a child + // record indexed with `split: { strategy: 'whole' }`, the index stays one + // record = one document, and `documentBy` is the unit `granularity: 'record'` + // counts in `topK` and groups by. Raise path the standing ruling + // 「A:先削重复,余额按实测提高」 (ruling record 6513306068, letter B). PAID DOWN + // FIRST: the `maxChars` sentence's "the longest passage" (the setting it + // overrides is named **Longest passage** in the same sentence). Arithmetic in + // priced bytes (stamp line excluded), against `main`'s 28987 (the row's 7247, + // headroom 0): the file is 29014 -> 29629 bytes, so the LANDED priced text is + // 29602 -> ceil(29602 / 4) = 7401; the row moves +154, the measured remainder + // only. Pinned AT the measurement. + // ceil(($(wc -c < skills/objectstack-ai/SKILL.md) - 27) / 4) == ceil(29602 / 4) + // 7401 -> 7489 (2026-12-01, mounted is not enabled): the section said a + // shipped item mounts the knowledge service and its store, which read as + // "shipped, so searchable". ADR-0161 K8's dated note on item 12 holds a + // runtime-mounted service until an embedder provider is saved, the + // deployment's store is mounted and the server restarts, and the sentence + // now says so: held sources are listed and never built or searched, + // `search_knowledge` is not offered, a search or a run-time save answers + // `503 SERVICE_UNAVAILABLE` naming the missing half, `os knowledge status` + // prints the state and why, and `os knowledge rebuild` exits 1 with it; the + // remedy where an app ships none gains the embedder save and its restart. + // Raise path the standing ruling 「A:先削重复,余额按实测提高」 (ruling record + // 6513306068, letter B). PAID DOWN FIRST, in the same paragraph: the + // "(`KnowledgeSourceSchema` in `@objectstack/spec/ai`)" aside (the section's + // example imports that schema from that path), the second + // "`SERVICE_UNAVAILABLE`, nothing written" (the held refusal beside it says + // both), and "to the stack" / "the server" of the remedy. Arithmetic in + // priced bytes (stamp line excluded), against 29602 (the row's 7401, + // headroom 0): the file is 29629 -> 29980 bytes, so the LANDED priced text is + // 29953 -> ceil(29953 / 4) = 7489; the row moves +88, the measured remainder + // only. Pinned AT the measurement. + // ceil(($(wc -c < skills/objectstack-ai/SKILL.md) - 27) / 4) == ceil(29953 / 4) + // 7489 -> 7499 (2026-12-01, the same two additions' review round): the + // `source.documentBy` row ended "Setting it rebuilds nothing" right after + // requiring the field in `metadataFields`. The key is in neither hash, but + // `metadataFields` is in the recipe hash, so listing the parent field there + // for the first time rebuilds the source in place; the row now says both + // halves. Raise path the standing ruling 「A:先削重复,余额按实测提高」 (ruling + // record 6513306068, letter B). PAID DOWN FIRST: the held sentence's "and + // its store" (the same sentence names the deployment's store as one of the + // halves the service waits for). Arithmetic in priced bytes (stamp line + // excluded), against 29953 (the row's 7489, headroom 0): the file is 29980 -> + // 30020 bytes, so the LANDED priced text is 29993 -> ceil(29993 / 4) = 7499; + // the row moves +10, the measured remainder only. Pinned AT the measurement. + // ceil(($(wc -c < skills/objectstack-ai/SKILL.md) - 27) / 4) == ceil(29993 / 4) + ['skills/objectstack-ai/SKILL.md', 7499], // -12 (was 6331): the `http.server` decision-table row dropped a trailing // clause that taught `contributes.routes` as a key that "parses but serves // nothing". The key is retired — an authored entry is now REFUSED, carrying diff --git a/skills/objectstack-ai/SKILL.md b/skills/objectstack-ai/SKILL.md index 68c6a062cd8..980ac823e9f 100644 --- a/skills/objectstack-ai/SKILL.md +++ b/skills/objectstack-ai/SKILL.md @@ -233,16 +233,20 @@ names, a skill's `context` over MCP, `os validate` rules: ## Knowledge Sources (RAG) -A RAG source is a **`knowledge_source`** metadata item (`KnowledgeSourceSchema` -in `@objectstack/spec/ai`). Ship it in a package (`knowledgeSources`, -`*.knowledge-source.ts` / `.yml`) or author it in Studio or over MCP; -`KnowledgeServicePlugin({ sources })` and `registerSource` are a second door for -the same schema. A source is identified by its package and `name`; the ☁️ -`search_knowledge` tool lists a package's source to agents as `NAMESPACE-NAME` -(bare for one no package holds). A shipped item mounts the -knowledge service and its store; where an app ships none, a run-time save is -refused (`503 SERVICE_UNAVAILABLE`, nothing written) until you add -`requires: ['knowledge']` to the stack and restart the server. +A RAG source is a **`knowledge_source`** metadata item. Ship it in a package +(`knowledgeSources`, `*.knowledge-source.ts` / `.yml`) or author it in Studio +or over MCP; `KnowledgeServicePlugin({ sources })` and `registerSource` are a +second door for the same schema. A source is identified by its package and +`name`; the ☁️ `search_knowledge` tool lists a package's source to agents as +`NAMESPACE-NAME` (bare for one no package holds). A shipped item mounts the +knowledge service, which does not enable it: the service is +**held** until an embedder provider is saved, the deployment's store is +mounted, and the server restarts. Held sources are listed, never built or +searched: `search_knowledge` is not offered, and a search or a run-time save +answers `503 SERVICE_UNAVAILABLE` naming the missing half (nothing written). +`os knowledge status` prints the state and why; `os knowledge rebuild` exits 1 +with it. Where an app ships none, the save answers that `503` until you add +`requires: ['knowledge']`, restart, save an embedder provider and restart again. **A source is an egress and cost decision.** Its first build sends every listed field of every matching record to the embedding provider and keeps the text (and @@ -259,6 +263,7 @@ object or field, when you may not search that object or query a field it reads. | `source.object` | The object whose records are indexed | | `source.content` | Non-empty list of the fields whose text is indexed, in order. `'*'` is refused: it would also send fields added later. Never ids or system fields | | `source.metadataFields` | Fields kept for search-time filtering (`status`, `owner_id`, `tags`) | +| `source.documentBy` | The document unit: the single-valued `lookup` / `master_detail` field, listed in `metadataFields`, that links each record to its parent (a clause to its policy). Any other field is refused. Setting it rebuilds nothing; newly listing the field rebuilds the source in place | | `source.where` | ObjectQL `where`: keeps drafts and archived rows out | | `source.split` | How documents are cut into passages (below) | | `source.retrieval.mode` | `'vector'` (by meaning), `'keyword'` (by words: codes, clause numbers) or `'hybrid'`; omitted: the store's default (`hybrid` on `turso`, `pgvector`). A mode the store lacks fails a run-time save; shipped, every search (503). `memory` runs none | @@ -321,6 +326,12 @@ export const supportKb: KnowledgeSource = KnowledgeSourceSchema.parse({ - **Attachment text goes through a field.** A `file` field in `content` adds no file text. Have the flow, hook or import that extracts the text write it to a `textarea` field (`contract.attachment_text`), and list that field. +- **An app that cuts its own passages** writes each as a child record and + indexes the child object with `split: { strategy: 'whole' }` and + `documentBy`. The index stays one record = one document; `documentBy` is + the unit `granularity: 'record'` counts in `topK` and groups by unless + `groupBy` is sent (the parent is the hit's `groupValue`; a record with no + parent is its own hit). **Splitting (`split`).** The knowledge service cuts each document into passages for a store declaring `capabilities.chunks`; any other store receives whole @@ -335,7 +346,7 @@ required; an omitted `split` means `recursive`: - `whole`: one passage per document. For short records only: a document over the embedder's input budget is removed from the index and reported. -`maxChars`, the longest passage in characters (`whole` ignores it), overrides +`maxChars`, in characters (`whole` ignores it), overrides the **Longest passage** setting (`knowledge.chunk_target`, else 800); the embedder's input limit can lower it. Adjacent pieces shorter than `minChars` (default 200) are merged in `recursive` text and in a `markdown` section that