Repository navigation
Commit 2776e88
feat(record-context): citation verifier reports failures apart from refusals (#37438)
<!-- claude-projects-attribution: {"github_login":"os-bill"} -->
_Requested by **Bill** · [project
thread](https://claude.ai/code/project/chan_016iMXWSZDzqWRWvWzdNo8wg?thread=cmsg_016iMXWSZDzqWRWvWzdNo8wgToQGw4qwp7m57qUh2sLcjt)_
Fixes #37418
Clause-②: yes (widening) — new optional third argument of the exported
verifyRecordCitations and two new exported types; existing calls
unchanged
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:用户无感;AI 回答里的引用核对时,数据库故障不再被当成「无权查看」悄悄丢掉,而是报给后台。
- 为什么做:AI 回答引用专题的后续,替云端产生引用的那一步和运维排障的人解决「一次故障让引用永久消失且无人知晓」。
-
出错会怎样:故障时引用照样不显示(宁缺勿错),只是多报一条错误;无权查看和记录不存在对后台仍是同一个结果,不泄露记录是否存在;可直接撤回本改动。
- 要你做什么:无。
## What changes
`verifyRecordCitations(citations, door, options?)` in
`packages/services/service-record-context/src/verify-citations.ts`:
- **Refusal vs failure.** A throw carrying an ADR-0112 `code` with a
declared 4xx status is a refusal. The status is read as ADR-0112's
reader (`resolveThrownHttpError`'s `declaredStatus`) reads it, `.status`
then `.statusCode`, mirrored locally because the package does not depend
on `@objectstack/types`. A refusal is the only shape
`get_record_context` refuses with: its citations are dropped and nothing
is reported, as before. Any other throw (no code, a driver code with no
status, a 5xx) is a failure: its citations are dropped too, so the
result stays fail-closed, and the failure goes to the new optional
`options.onError(error, call)`, once per verifying call. A callback that
throws, or returns a promise that rejects, is ignored, so the verifier
still never throws for a citation and leaves no unhandled rejection. The
served-definition read behind the HTML-body check reports too; there the
call answered and only the tag-removed reading is lost, which the
option's JSDoc says.
- **Why status, not the code string.** ADR-0112's 2026-08-27 amendment
names the status channel as the one structural signal between a refusal
and a fault: a driver errno (`ECONNRESET`, `SQLITE_ERROR`) also arrives
on `.code`, so the string alone cannot tell.
- **Design choice: a callback, not a new result shape.** It is the
smaller change. The return type stays `RecordCitationParsed[]`, existing
two-argument callers see nothing, and the result carries nothing that
could tell a caller a hidden record from a missing one. A `{ verified,
unverifiable }` result would put the failure next to data the caller may
see, and would change the signature every caller reads.
- New exported types `VerifyRecordCitationsOptions` and `VerifyingCall`
(`src/index.ts`).
- **Nits from the PR #37396 review:**
- The module doc says which list cap a producer gets: the verifier reads
the first 20 (`RECORD_CITATIONS_MAX`) and drops the rest, where
`RecordCitationsSchema` refuses a list of 21.
- The module doc corrects the "tag-stripping only makes verification
stricter" note: the tag-removed reading is a second string, and removing
a tag joins adjacent text (`<p>Fire</p><p>cover</p>` reads `Firecover`).
A test pins it.
- `stripBodyTags` (`packages/spec/src/ai/record-body-anchor.ts`) says it
is not a sanitizer. JSDoc only.
## Tests
`src/__tests__/verify-citations.test.ts`, new block "a refusal and a
failure":
- A door whose root read throws an uncoded `Error`: citations dropped,
failure reported once per distinct call (two calls, three citations →
two reports). Control: the same citations verify through a door that
answers.
- A driver code with no status and a coded 500: dropped and reported.
- Control: coded 403, 404 and 400 refusals: dropped, not reported.
- A refused read (`unreadableRows`) and a missing record (`p404`) give
the host the identical `{ quotes: [], reported: [] }`, with a control
leg that verifies the readable record.
- A throwing callback is ignored; no callback still drops the failed
call.
- Rework (contract review 7184346569, seat REWORK 7184355118): a coded
refusal declared with `statusCode` only is not reported (control: coded
503 `statusCode` is); a 4xx with no code, a thrown string and a thrown
`undefined` are reported; a failing definition read behind an HTML body
is reported once while the field quote and the as-stored quote still
verify; an async callback that rejects leaves no unhandled rejection.
- The adjacent-text join of the tag-removed reading.
Ablation (one, on the coded-vs-uncoded split): `isRefusal` reduced to
"has a string code" → "reports a driver code with no status, and a coded
5xx, as failures" goes red (1 failed, 20 passed); restored → green.
Rework ablations: dropping the `.statusCode` limb → the statusCode-only
test red (1 failed | 24 passed); dropping the returned-promise handler →
the async-callback test red (1 failed | 24 passed); both restored →
green.
## Acceptance notes
- `record-context-tools.ts` (fenced, sibling #37361) turns some door
failures into coded refusals before they reach the verifier: a failing
related-object definition read or published-definition read becomes
`PERMISSION_DENIED` 403 so the refusal names nothing. Those failures are
therefore not reported, and the module doc now says so. The follow-up
card is #37463.
- The whole 4xx band is a refusal, so a door declaring a coded 408 or
429 would drop a transient condition without a report. No door on the
read path declares one today; the module doc records it (review N4).
- `Clause-②: yes (widening)` (seat ruling, same as the #37412
precedent): the new parameter is optional and the return type is
unchanged, so no existing caller changes; there is no caller outside
this package today. Bump stays patch.
## Verification
Two-minute check: `pnpm --filter @objectstack/service-record-context
exec vitest run src/__tests__/verify-citations.test.ts` — 25 tests pass.
Read first: `verify-citations.ts` `isRefusal`, `declaredStatusOf` and
`report` (the split and the async guard), then the `haystackOf` catch.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01Utp6CZnFSe9yjMwBFr33Rk)_
<!-- signed: repo:objectstack#5 · os-bill ·
session_01Utp6CZnFSe9yjMwBFr33Rk -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent cc38a91 commit 2776e88
5 files changed
Lines changed: 296 additions & 14 deletions
File tree
- .changeset
- packages
- services/service-record-context/src
- __tests__
- spec/src/ai
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | | - | |
| 15 | + | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
88 | 93 | | |
89 | 94 | | |
90 | 95 | | |
| |||
150 | 155 | | |
151 | 156 | | |
152 | 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 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
153 | 295 | | |
154 | 296 | | |
155 | 297 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
0 commit comments