Repository navigation
Commit 23c311e
fix(service-knowledge): the splitter matches each document line in linear time (#38383)
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用户无感:知识库把文档切成段落时,每一行的标题识别和表格分隔行识别改为只随行长线性增长的读法,普通文档切出的段落前后一字不变。
- 属于知识库检索的切分加固,替维护知识库的管理员把切分耗时控制在随文档长度增长的范围内。
- 若有误,碰到的是知识库管理员:个别标题或表格的切分与以前不同;可直接撤回,已建的索引无需重建。
- 无
## What changes
The knowledge splitter matches each document line in time linear in its
length.
- The `markdown` strategy reads each line outside a code fence for an
ATX heading through `matchHeading`, a scan from both ends of the line.
- The table split decides whether its second row is a separator row
through `isTableSeparatorRow`, one scan from the start of the row.
- Both readings are unchanged for every line: a heading reads to the
same level and text, and a row is a separator row in exactly the same
cases. No passage changes, so `SPLIT_ALGORITHM_VERSION_BY_STRATEGY` is
not bumped and no index needs rebuilding.
- The two helpers are exported from `split/splitter.ts` for their tests
only (marked `@internal`); the package's entry points export nothing
new.
Read first, by risk:
1. `packages/services/service-knowledge/src/split/splitter.ts`,
`matchHeading`: the heading text keeps its first character, and a line
of only `#` and spaces or tabs is a heading only when something follows
the first space or tab. These two edge rules are the readings the old
code gave, pinned by the capture table.
2. `packages/services/service-knowledge/src/split/splitter.ts`,
`isTableSeparatorRow`.
3.
`packages/services/service-knowledge/src/__tests__/split-linear-time.test.ts`,
the two capture tables.
## Tests
New file `src/__tests__/split-linear-time.test.ts`, 18 tests:
- **Captures.** 53 heading lines and 27 table rows (closing `#` runs
with and without spaces, trailing spaces and tabs, CJK headings, `#`
with no text, seven `#`, indented lines, line-ending characters inside a
line; separator rows with and without leading pipes, colons and spaces).
Each table entry is the reading the splitter gave that line on `main`
9b783ee, captured before the edit; the new code gives every one of them
unchanged.
- **Linear time, line match.** Eight shapes of a 100,000-character
heading line and six shapes of a 100,000-character table row: the best
of 5 runs is under 50 ms (measured here: 0.2 ms to 2 ms).
- **Linear time, whole split.** A markdown document carrying each long
heading line, and a table carrying each long second row, split end to
end: the best of 3 runs is under 1,000 ms. The end-to-end time at this
length is about 30 ms on this box, nearly all of it the passage cutting
the splitter already did, which is itself linear; that is why the
end-to-end bound is wider than the line-match bound.
- The existing split suite and its golden files pass byte-identical (no
golden file is in the diff).
Additional, not committed: a seeded differential run of 400,000
generated lines (an alphabet of `#`, spaces, tabs, letters, CJK,
line-ending characters, `:`, `-`, `|`) read identically by the old and
new line matching, 0 mismatches.
Reverse verification, against the committed fix: each of the two line
matchers was put back to its pre-change reading on disk in turn, through
`scripts/ablation-replace.mjs`. Both legs: the 2 capture tests green
(the tables are the pre-change readings), the 100,000-character tests
red; then restored, blob equal to `HEAD` and `git diff HEAD` empty.
## Acceptance notes
- No card: this is a no-card hardening order.
- The end-to-end split of a 100,000-character line is asserted under
1,000 ms rather than 50 ms, for the reason given under Tests; the 50 ms
bound is asserted on the line match itself.
## Verification
Two minutes: `pnpm --filter @objectstack/service-knowledge exec vitest
run src/__tests__/split-linear-time.test.ts`. Expected: 1 file, 18 tests
passed, in a few seconds.
<details><summary>Local readings at 7382828</summary>
- `pnpm --filter @objectstack/service-knowledge test`: Test Files 21
passed (21), Tests 725 passed (725).
- `pnpm --filter @objectstack/service-knowledge typecheck`: exit 0;
`check:test-typecheck: OK`, and `tsc -p tsconfig.test.json --listFiles`
lists the new test file.
- Derived gate list (`dispatch-gates.mjs --commands`, 63 commands): 61
exit 0; 2 exit 3, NOT MEASURED: `check-plugin-teardown-shape.mjs
--self-test` (reason: the shallow checkout cannot reach its pinned
positive-control commit) and `check:dual-build-cjs-loads` (reason: it
reads every package's built output, and only this package's dependency
closure was built). Both run in CI. `--ran` reconciliation: 63 derived,
61 run, 2 NOT-MEASURED, 0 UNRUN.
- `check-platform-checklist.mjs --changed-from origin/main`: exit 0, no
anchored file in the change set.
</details>
<!-- signed: repo:objectstack#8 · sunhaolin ·
session_017xRBcSSm1UGomShRsemA1N -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 61cde6d commit 23c311e
3 files changed
Lines changed: 269 additions & 7 deletions
File tree
- .changeset
- packages/services/service-knowledge/src
- __tests__
- split
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
379 | 379 | | |
380 | 380 | | |
381 | 381 | | |
382 | | - | |
| 382 | + | |
383 | 383 | | |
384 | 384 | | |
385 | 385 | | |
| |||
406 | 406 | | |
407 | 407 | | |
408 | 408 | | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
409 | 434 | | |
410 | 435 | | |
411 | 436 | | |
| |||
502 | 527 | | |
503 | 528 | | |
504 | 529 | | |
505 | | - | |
506 | 530 | | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
507 | 561 | | |
508 | 562 | | |
509 | 563 | | |
| |||
532 | 586 | | |
533 | 587 | | |
534 | 588 | | |
535 | | - | |
536 | | - | |
| 589 | + | |
| 590 | + | |
537 | 591 | | |
538 | 592 | | |
539 | 593 | | |
540 | 594 | | |
541 | | - | |
542 | | - | |
543 | | - | |
| 595 | + | |
| 596 | + | |
544 | 597 | | |
545 | 598 | | |
546 | 599 | | |
| |||
0 commit comments