Repository navigation
Commit f782a12
fix(knowledge-pgvector): a failed search is a coded refusal, its statement has a time limit, and its pool size is a setting (#38726)
<!-- claude-projects-attribution: {"github_login":"os-warren"} -->
_Requested by **Warren** · [project
thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ8VZtjriPRDBMFWChF2eLFkE)_
Fixes #38718
Clause-②: yes
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:私有部署企业版在 PostgreSQL 上检索知识时,数据库忙、连接等不到或语句超时,助手会收到明确的「超时 /
暂不可用」拒绝,不再拿到「0 条命中」;检索语句默认最多跑 10 秒;连接池大小可以由运维通过设置调整。
- 为什么做:属于「私有部署企业版知识检索 10 人同时提问不超时」专题第一期,目标之一是出错不能变成空结果,首个用户是 BrightRule
制度问询。
- 出错会怎样:最坏情况是本来能跑完的超长检索(超过 10 秒)现在被拒绝为超时,运维可调大时限设置;改动只在知识检索的 PostgreSQL
适配器内,撤回即恢复原样。
- 要你做什么:无。
## What changes
Topic #38703 row B2, in `@objectstack/knowledge-pgvector` only (no
change to `service-knowledge` or `core`).
1. **Coded refusals.** `search()` now answers every failure of the
store's database as an ADR-0112 refusal, judged by SQLSTATE (never by
the server's message, which `lc_messages` translates): `57014`
(statement past its limit, or cancelled) is `TIMEOUT` 504; class `53`
(`too_many_connections`, out of memory, disk full) is
`SERVICE_UNAVAILABLE` 503; no connection (the pool's wait timed out,
class `08`, `57P01`–`57P05`, a socket error) is `SERVICE_UNAVAILABLE`
503; any other SQLSTATE is `DATABASE_ERROR` 500. The message is one
sentence naming the source; the SQLSTATE, the driver text and the
setting to change go to `remedy` (operator text the service logs). An
already-coded error (the adapter's own refusals) and a non-database
error (the embedder's) pass through unchanged. New module
`src/errors.ts`.
- The pool's wait timeout carries no code at all, so `poolFromPg` now
marks every error its pool and connections throw (a non-enumerable
symbol; message, code and keys untouched, so write paths read exactly
what `pg` threw), and the classifier treats a marked uncoded error as
"no connection".
- Placement: the translation is one `try/catch` in `search()` around the
attempts loop (moved into a private `searchAttempts`); the ranking
statement's body is untouched (the #38692 phase-1a pgvector PR follows
this one).
2. **Statement time limit.** The ranking statement runs through a new
`searchStatement` helper: its own transaction, `SET LOCAL
statement_timeout = N` first. Chosen form: `SET LOCAL` in a transaction,
so the limit lasts exactly one statement, touches no session state and
holds behind a transaction-mode pooler (the builder lock still needs
session mode; README says so). Default 10000 ms, the same bound the
Turso store uses for a remote request (#38693); `statementTimeoutMs`
plugin/adapter option, else the setting
`OS_KNOWLEDGE_STATEMENT_TIMEOUT_MS`. Only the ranking statement is
limited; catalog reads, writes and builds are not.
3. **Pool size from a named setting.** `OS_KNOWLEDGE_POOL_MAX` (plugin
option `poolMax` wins), default **20** (raised from 4 in Rework 2 below,
on os-bob's measurement). Strict like `OS_DATABASE_POOL_MAX`: a value
that is not a positive integer stops the boot naming the setting; blank
reads as unset. The plugin reads it (as it already reads the URL from
the environment), so a composed boot (`os serve` hands `process.env`)
and a host that mounts the plugin directly read the same setting;
`resolveKnowledgeStore` in `core` needed no change. The boot line now
says the pool size, the limit and where each was read from.
- **Ratio to `max_connections = 200`** (README, "Pool size and time
limit"): `replicas × (OS_DATABASE_POOL_MAX + OS_KNOWLEDGE_POOL_MAX +
builds running at once) + headroom ≤ max_connections`. With 20 kept as
headroom, platform pool at its default 5 and one build running, the
default of 20 fits up to 6 replicas (3 replicas = 60 store connections);
the ceiling is 84 with 2 replicas, 54 with 3, 39 with 4. A larger pool
moves waiting from the pool into the database; it does not make one
search faster. This is the setting key and ratio B12 (the delivery note)
carries.
## Why
Premise re-read on `origin/main` d2caaec before code: `codedRefusalOf`
(`knowledge-service.ts:4974`) accepts only a SCREAMING_SNAKE `code` with
an integer `status` 400–599; `searchRound` (`:3237-3249`) logs an
uncoded throw at `error` and `continue`s, and `search()` throws
`wholeRefusal` only when `refused` is non-empty (`:2561`). A `pg`
error's `code` is a SQLSTATE (`57014`) or absent, so a single-source
search answered `{ hits: [] }`. The ablation below reproduces exactly
that.
## Tests
- `src/__tests__/search-refusals.test.ts` (unit, runs everywhere, 14
cases): the statement shape (`BEGIN`, `SET LOCAL statement_timeout =
10000`, the ranking, `COMMIT`; the ranking never on a bare pooled query;
a given limit; a bad limit refused at construction); one case per error
class (statement timeout `57014`, cancel `57014` with a translated
server message, pool wait through `poolFromPg` at the ranking's connect
and at a pooled read, `53300`, `57P01`, `42P01`), each pinned on `code`
+ `status`; the embedder's uncoded error passed through by identity;
**through `KnowledgeService.search`: the caller is refused `TIMEOUT` 504
instead of an empty list**; the settings reader (precedence, blank,
strict refusals).
- `src/__tests__/search-refusals.live-postgres.test.ts` (live,
`OS_TEST_POSTGRES_URL`, no pgvector needed): a real `pg_sleep` past `SET
LOCAL statement_timeout` answers `57014` and classifies `TIMEOUT` 504,
and `SHOW statement_timeout` on the same pooled connection after the
transaction reads the session's own value; a real full pool (`max: 1`)
times out through `poolFromPg` and classifies `SERVICE_UNAVAILABLE` 503
on both `query` and `connect`. Pins codes, never durations.
<details><summary>Local readings at head 9a39db7</summary>
- `pnpm --filter @objectstack/knowledge-pgvector test`: exit 0, Test
Files 6 passed / 9 skipped (live files, no URL), Tests 38 passed / 95
skipped.
- `vitest run src/__tests__/search-refusals.live-postgres.test.ts` with
`OS_TEST_POSTGRES_URL` at a local PostgreSQL 16.15: exit 0, 2/2.
- `pnpm --filter @objectstack/knowledge-pgvector typecheck`: exit 0
(incl. `check:test-typecheck`, 0 debt).
- `eslint --no-inline-config` on the five changed source/test files:
exit 0. `scripts/check-nul-bytes.mjs`: exit 0.
- `dispatch-gates.mjs --commands` (7 paths, 694 changed lines): 26 of 27
commands exit 0; `check-plugin-teardown-shape.mjs --self-test` exit 3 —
NOT MEASURED locally, reason: shallow clone cannot reach its pinned
fixture commit (the script says so); CI runs it.
- **Ablation** (fix committed first, restored with `git checkout HEAD
--`): with `search()` rethrowing the raw error, 8 of 14 cases go red,
and the `KnowledgeService` case receives `{ hits: [] }` — the defect as
filed. With `poolFromPg`'s marking removed, the two pool-wait cases go
red.
</details>
## Acceptance notes
- `Clause-②: yes` is copied from the claim. Behaviour that changes for a
caller: a database failure that used to read as zero hits from that
source is now a refusal (`refused[]`, or a whole-search refusal when it
was the only source); a search statement over 10 s is now stopped where
it used to run to completion.
- Out of scope, noted, not filed: an uncoded throw from something other
than the database (the embedder failing, a mis-sized query vector) is
still logged and skipped by `searchRound`, so it still reads as zero
hits for that source. The general fix sits in `service-knowledge`
(`searchRound` treating any uncoded throw as a refusal), which this card
was told not to touch (held by #38700). carrier: #38703 (topic owner) ·
noted, not filed.
- `connectTimeoutMs` (also the pool's wait) stays a plugin option only,
default 5000; the card asked for the pool size and the time limit, not
this.
## Verification
Two-minute check: `pnpm --filter @objectstack/knowledge-pgvector exec
vitest run src/__tests__/search-refusals.test.ts` — expect 14 passed,
among them "through KnowledgeService: the caller is refused TIMEOUT, not
answered an empty hit list".
Read first, by risk: `src/errors.ts` (the SQLSTATE table),
`src/pgvector-adapter.ts` `search()` / `searchStatement()`,
`src/plugin.ts` `poolFromPg` and `resolveStoreSettings`.
## Rework (L1 7272964718, docs only)
Head `4340f85d` adds the two should-fix doc edits and nothing else; nits
N1–N3 not taken this round.
- `content/docs/deployment/environment-variables.mdx` (Database &
Storage, beside `OS_DATABASE_POOL_MAX`): `OS_KNOWLEDGE_POOL_MAX`
(integer, `4`, per replica, plugin option wins, the sizing formula with
a link to the README section, a non-integer refuses the boot) and
`OS_KNOWLEDGE_STATEMENT_TIMEOUT_MS` (integer, `10000`, per search
statement, `SET LOCAL`, refused `TIMEOUT` past it, plugin option wins, a
non-integer refuses the boot).
- `content/docs/protocol/knowledge.mdx` §4.1: one sentence each for the
pool precedence (`poolMax`, else `OS_KNOWLEDGE_POOL_MAX`, else 4), the
ranking statement under `SET LOCAL statement_timeout` (default 10000
ms), and the three refusals by SQLSTATE.
Regen-provenance: 7272956301 · 9a39db7 → 4340f85 · `git diff
--name-only 9a39db7 4340f85` →
content/docs/deployment/environment-variables.mdx,
content/docs/protocol/knowledge.mdx (prose faces only, no code path
moved)
## Rework 2 (pool default 4 → 20, on new data)
Head `4f6b7d85`. The only behaviour change is `DEFAULT_POOL_MAX` 4 → 20
in `src/plugin.ts`; the rest is its pins and prose. Nits N1–N3 not
taken. `statement_timeout` stays 10000 ms.
Evidence (not this PR's bench reading): os-bob's measurement on the
card, #38718 comment 7273601824 — local PostgreSQL 17 + pgvector 0.8.7,
59,949 indexed BrightRule clauses, end to end over MCP
`search_knowledge`, scoped, 15 questions in rotation:
> | 配置 | 20 并发:空结果 | 40 并发:空结果 |
> |---|---|---|
> | 混合检索 · 连接池 4(现默认) | 15 / 45 | 52 / 90 |
> | 混合检索 · 连接池 20 | 0 / 45 | 8 / 90 |
> | 只用向量 · 连接池 20 | 0 / 45 | 0 / 90 |
>
> 空结果全部来自等连接超时:平台日志里有 225 次 `timeout exceeded when trying to connect` …
**建议默认值从 4 改为 20**(不只是可配置)。… 语句时限 10 秒:单人串行时,只用向量的主语句 234–445 ms,混合检索
1.1–1.7 s,都远低于 10 秒。
(Those empty results are what this PR turns into `SERVICE_UNAVAILABLE`
refusals; the larger pool is what keeps them from happening at 20
concurrent callers.)
- `src/plugin.ts`: `DEFAULT_POOL_MAX = 20`; its docblock rewritten to
justify the move (the measurement, a lazy ceiling rather than a count
opened at boot, fits up to 6 replicas at `max_connections=200`, class 53
refusals past that), replacing the old "not moved" note. The two option
docblocks say 20.
- **Upgrade effect, stated in the changeset and README:** a deployment
with `OS_KNOWLEDGE_POOL_MAX` unset may now open up to 20 store
connections per replica under load instead of 4. With the README's
formula (platform pool 5, one build, 20 kept as headroom,
`max_connections=200`) a replica reaches up to 26, so up to 6 replicas
fit (6 × 26 = 156 ≤ 180; 7 × 26 = 182 does not); 3 replicas take 60
store connections. More replicas or a smaller server must set it lower
(`4` keeps the old size) or raise `max_connections`, otherwise the
server refuses connections (`too_many_connections`, class 53) and those
searches are refused `SERVICE_UNAVAILABLE`.
- Every pin of 4 moved together: `search-refusals.test.ts`
(`DEFAULT_POOL_MAX` and the blank-setting case), the README options and
settings tables, `environment-variables.mdx`'s `OS_KNOWLEDGE_POOL_MAX`
row, `knowledge.mdx` §4.1, the changeset.
- Local at `4f6b7d85`: `search-refusals.test.ts` 14/14, `tsc --noEmit`
exit 0.
Regen-provenance: 7272956301 · 4340f85 → 4f6b7d8 · `git diff
--name-only 4340f85 4f6b7d8` →
.changeset/knowledge-pgvector-coded-search-refusals.md,
content/docs/deployment/environment-variables.mdx,
content/docs/protocol/knowledge.mdx,
packages/plugins/knowledge-pgvector/README.md,
packages/plugins/knowledge-pgvector/src/__tests__/search-refusals.test.ts,
packages/plugins/knowledge-pgvector/src/plugin.ts (NOT prose-only: one
constant and its docblock in `plugin.ts`, one test pin; the record does
not carry across this hop by itself)
## Human Minimum
- [x] 1. Schemas start as Zod; `packages/spec` holds no runtime logic.
(not touched)
- [x] 2. Every name I added uses the casing for its kind.
- [x] 3. No formatter ran; eslint passes on the changed files.
- [x] 4. One issue, one branch off an up-to-date `main`, one pull
request.
- [x] 5. A change that publishes carries a changeset, and both
declaration lines are filled in here and in the changeset.
- [x] 6. `packages/spec` touched: n/a.
- [x] 7. No hand edits to `content/docs/references/`,
`packages/*/CHANGELOG.md` or `content/docs/releases/`.
- [x] 8. Title, body and comments are in English (the 维护者速读 block
excepted by AGENTS.md's Communication rule).
---
_Generated by [Claude
Code](https://claude.ai/code/session_01JQwxbYVKM5Jf2J5JbHJ8Aj)_
<!-- signed: repo:objectstack@os-warren · os-warren ·
session_01JQwxbYVKM5Jf2J5JbHJ8Aj -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 855c3e1 commit f782a12
9 files changed
Lines changed: 699 additions & 20 deletions
File tree
- .changeset
- content/docs
- deployment
- protocol
- packages/plugins/knowledge-pgvector
- src
- __tests__
| 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 | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
| 57 | + | |
56 | 58 | | |
57 | 59 | | |
58 | 60 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
736 | 736 | | |
737 | 737 | | |
738 | 738 | | |
739 | | - | |
740 | | - | |
741 | | - | |
742 | | - | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
743 | 743 | | |
744 | 744 | | |
745 | 745 | | |
| |||
773 | 773 | | |
774 | 774 | | |
775 | 775 | | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
776 | 784 | | |
777 | 785 | | |
778 | 786 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
28 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
62 | 63 | | |
63 | 64 | | |
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 | + | |
65 | 100 | | |
66 | 101 | | |
67 | 102 | | |
| |||
| 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 | + | |
0 commit comments