Skip to content

Commit a35eda6

Browse files
committed
docs(knowledge): a fitting markdown section is one passage; minChars merges only sections that do not fit
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5mCvGQKiCEBneFwp1j
1 parent c2747f7 commit a35eda6

6 files changed

Lines changed: 52 additions & 19 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
'@objectstack/service-knowledge': minor
3+
'@objectstack/spec': minor
4+
---
5+
6+
feat(service-knowledge): under the `markdown` split, a section that fits is one passage
7+
8+
Clause-②: no
9+
Metadata-protocol: semantic
10+
Skills-docs: follow-up owed — skills/objectstack-ai/SKILL.md
11+
12+
On `split.strategy: 'markdown'`, a section was cut as `recursive` cuts, and its pieces were merged only while the merged piece stayed shorter than `minChars` (default 200). A section that fit `maxChars` but held several paragraphs of 200 characters or more was therefore cut into several passages, so a search hit only part of an article.
13+
14+
Now a section whose text, together with the heading path it is given, fits the effective `maxChars` and the embedder's token budget is one passage, whatever `minChars` is (`minChars: 0` included). Only a section that does not fit is cut as before, with `minChars` merging its short pieces; such a section gives exactly the passages it gave before. The text before the first heading, and a heading's text before its first sub-heading, are sections and follow the same rule. `recursive` and `whole` are unchanged. The `@objectstack/spec` docblocks, the `minChars` description and the knowledge source form's help text now say this; no key, value or default changes.
15+
16+
Which passages change: on `markdown` sources only, the passages of sections that fit but were cut. Each such section becomes one passage, so a table and its note inside a fitting section are always one passage.
17+
18+
The `markdown` split algorithm version stays 2: this change rides the version move to 2 that the body-less heading fix already makes in this release train, so every `markdown` source rebuilds once for both, not twice.
19+
20+
What to do: nothing.

‎content/docs/ai/knowledge-rag.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ source: {
255255
```
256256

257257
- `recursive` (the default) cuts at blank lines, then line breaks, then sentence ends, then semicolons, then characters. It reads Chinese and English sentence ends — 「3.2 条」 and 「400.50 元」 stay whole — and never cuts inside a Markdown table or list item that fits.
258-
- `markdown` cuts one section per heading; each passage begins with its heading path, which is embedded with it.
258+
- `markdown` cuts one section per heading; each passage begins with its heading path, which is embedded with it. A section that fits in one passage (its text with its heading path, within the length below) is one passage; only a section that does not fit is cut as `recursive` cuts, and `minChars` (default 200) merges the short pieces of that cut.
259259
- `whole` keeps each document as one passage.
260260

261261
A passage is at most `split.maxChars` characters — else the **Knowledge → Target chunk size** setting, else 800 — and, when the adapter reports its embedder's input limit, within 80% of that limit by the shared token estimate below. A `whole` document over that budget is **removed from the index and reported** (`failed` and `failures` on the reindex result, an `error` log on the event sync), never truncated; because the estimate runs high, a `whole` document can use about 60% of the model's limit. The full rules are in [the protocol's split section](/docs/protocol/knowledge#35-the-split).

‎content/docs/protocol/knowledge.mdx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,15 +564,16 @@ them. The object source's `split` key chooses how:
564564
split: {
565565
strategy: 'recursive', // 'whole' | 'recursive' | 'markdown'
566566
maxChars: 600, // optional: longest passage, in characters
567-
minChars: 200, // optional: merge adjacent pieces shorter than this (default 200)
567+
minChars: 200, // optional: merge adjacent pieces shorter than this (default 200);
568+
// under 'markdown', only in a section that does not fit
568569
}
569570
```
570571

571572
| Strategy | How it cuts |
572573
|----------|-------------|
573574
| `whole` | One passage per document. |
574575
| `recursive` (the default) | At blank lines, then line breaks, then sentence ends, then semicolons, then characters — each next level only for a piece still too long. A Markdown table or list item is one unit while it fits; a table that does not fit is cut between rows, each piece repeating the header row. Adjacent pieces are merged while the merged piece is shorter than `minChars`; a short title block leads the text after it. |
575-
| `markdown` | One section per heading (a `#` inside a code fence is no heading), each cut as `recursive` cuts. Every passage begins with its heading path (`Policy > Transport > Air`), which counts toward its length and is embedded with it, and carries it as `headingPath`. A heading with no text of its own joins the next passage. |
576+
| `markdown` | One section per heading (a `#` inside a code fence is no heading); the text before the first heading, and a heading's text before its first sub-heading, are sections too. Every passage begins with its heading path (`Policy > Transport > Air`), which counts toward its length and is embedded with it, and carries it as `headingPath`. A section that fits — its text with its heading path, within the effective `maxChars` and the token budget — is **one passage, whatever `minChars` is**; only a section that does not fit is cut as `recursive` cuts, its pieces merged up to `minChars`. A heading with no text of its own is named by the path of the section after it when it is that section's ancestor, and is a passage of its own otherwise. |
576577

577578
**Sentence ends, for Chinese and English text.** `。?!` end a sentence
578579
wherever they stand. `. ? !` end one only before whitespace or the end of the
@@ -588,7 +589,9 @@ embedder's `maxInputTokens`, every passage's token estimate (the shared
588589
`estimateTokens` from `@objectstack/spec/shared`) also stays within **80%** of
589590
that limit. A passage keeps both bounds, so the effective length is the
590591
smaller one; with no limit known, only `maxChars` applies. A `minChars` above
591-
the effective `maxChars` reads as `maxChars`. The splitter never overlaps
592+
the effective `maxChars` reads as `maxChars`; under `markdown` it merges only
593+
the pieces of a section that does not fit, so `minChars: 0` still leaves a
594+
fitting section whole. The splitter never overlaps
592595
passages.
593596

594597
**`whole` over the limit.** A `whole` document whose estimate is over the 80%

‎packages/services/service-knowledge/src/split/splitter.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
* character, the next level only for a piece the level above left too long.
1717
* A Markdown table or list item is one unit while it fits. Adjacent pieces
1818
* are merged while the merged piece is shorter than `minChars`.
19-
* - `markdown` — one section per ATX heading (outside code fences); each
20-
* section is cut as `recursive` cuts, and every passage begins with its
21-
* heading path, which counts toward its length and is embedded with it. A
22-
* heading with no text of its own is named by the path of the section after
23-
* it when it is that section's ancestor, and is a passage of its own
24-
* otherwise — never a line of another heading's section.
19+
* - `markdown` — one section per ATX heading (outside code fences); every
20+
* passage begins with its heading path, which counts toward its length and
21+
* is embedded with it. A section that fits — its text together with the
22+
* heading path it is given, within `maxChars` and the token budget — is one
23+
* passage, whatever `minChars` is; only a section that does not fit is cut
24+
* as `recursive` cuts, its pieces merged up to `minChars`. A parent heading's
25+
* text before its first child, and the text before the first heading, are
26+
* sections of their own. A heading with no text of its own is named by the
27+
* path of the section after it when it is that section's ancestor, and is a
28+
* passage of its own otherwise — never a line of another heading's section.
2529
*
2630
* ## The sentence end (the Chinese-aware part)
2731
*
@@ -97,7 +101,7 @@ export interface ResolvedSplit {
97101
strategy: KnowledgeSplitStrategy;
98102
/** Longest passage, in characters. Not read by `whole`. */
99103
maxChars: number;
100-
/** Adjacent pieces shorter than this are merged. Never above `maxChars`. */
104+
/** Adjacent pieces shorter than this are merged; under `markdown`, only in a section that does not fit. Never above `maxChars`. */
101105
minChars: number;
102106
/** Largest `estimateTokens` of a passage; absent when no limit is known. */
103107
maxTokens?: number;
@@ -566,9 +570,10 @@ export function matchHeading(line: string): { level: number; text: string } | nu
566570
* section's ancestor; otherwise it is a passage of its own, under its own path
567571
* (its ancestors and itself). It never joins a section that is not its
568572
* descendant, so no heading is filed under one that is not its ancestor, and
569-
* every heading is in some passage's path. Each section is cut by
570-
* {@link splitRecursive} within the bounds left after its heading path, and
571-
* every passage begins with that path.
573+
* every heading is in some passage's path. A section whose text, with its
574+
* heading path, fits the bounds is one passage; any other is cut by
575+
* {@link splitRecursive} within the bounds left after its heading path. Every
576+
* passage begins with that path.
572577
*/
573578
export function splitMarkdown(
574579
text: string,

‎packages/spec/src/ai/knowledge-source.metadata-form.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const knowledgeSourceForm = defineMetadataForm({
9393
helpText: 'Whole: one passage per document. Recursive: cut at blank lines, then line breaks, then sentence ends. Markdown: one passage per heading, carrying its heading path.',
9494
},
9595
{ field: 'maxChars', label: 'Longest passage', helpText: 'In characters. Needs a strategy. Wins over the settings page\'s chunk size; the embedding model\'s input limit can lower it. Not read by Whole document.' },
96-
{ field: 'minChars', label: 'Shortest passage', helpText: 'In characters. Needs a strategy. Adjacent pieces shorter than this are merged, up to this length (default 200). Recursive and Markdown only.' },
96+
{ field: 'minChars', label: 'Shortest passage', helpText: 'In characters. Needs a strategy. Adjacent pieces shorter than this are merged, up to this length (default 200). Recursive and Markdown only; under Markdown a section that fits is one passage, so this merges only the pieces of a section that does not.' },
9797
],
9898
},
9999
],

‎packages/spec/src/ai/knowledge-source.zod.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ export const KnowledgeSplitStrategySchema = lazySchema(() => z.enum(['whole', 'r
9494
* semicolons, then characters, never inside a Markdown table or list item
9595
* while it fits; adjacent short pieces are merged up to `minChars`.
9696
* - `markdown` — one section per heading; each passage carries its heading
97-
* path, which counts toward its length and is embedded with it.
97+
* path, which counts toward its length and is embedded with it. A section
98+
* that fits — its text with its heading path, within the effective
99+
* `maxChars` — is one passage, whatever `minChars` is; a section that does
100+
* not fit is cut as `recursive` cuts. The text before the first heading,
101+
* and a heading's text before its first sub-heading, are sections too.
98102
*
99103
* The effective `maxChars` is the smaller of this value (or the settings
100104
* page's `knowledge.chunk_target`, or 800) and what fits in 80% of the
@@ -114,11 +118,12 @@ export const KnowledgeSplitSchema = lazySchema(() => strictObject(
114118
.describe('Longest passage in characters; wins over the settings default, the embedder limit can lower it'),
115119
/**
116120
* Adjacent pieces shorter than this are merged, up to this length
117-
* (`recursive` and `markdown`). A value above the effective `maxChars`
118-
* reads as `maxChars`. @default 200
121+
* (`recursive`, and `markdown` within a section that does not fit; a
122+
* `markdown` section that fits is one passage whatever this is). A value
123+
* above the effective `maxChars` reads as `maxChars`. @default 200
119124
*/
120125
minChars: z.number().int().nonnegative().optional()
121-
.describe('Adjacent pieces shorter than this are merged up to it (default 200); a value above the effective maxChars reads as maxChars'),
126+
.describe('Adjacent pieces shorter than this are merged up to it (default 200); under markdown, only in a section that does not fit, since a section that fits is one passage; a value above the effective maxChars reads as maxChars'),
122127
},
123128
));
124129

0 commit comments

Comments
 (0)