Repository navigation
Commit 702c562
fix(service-analytics): a truncated query_dataset answer carries drill sidecars for the served rows only (#33146)
<!-- claude-projects-attribution: {"github_login":"os-tesla"} -->
_Requested by **Tesla** · [project
thread](https://claude.ai/code/project/chan_01AXsQkLFKRdQCqQ3RTr6Jao?thread=cmsg_01AXsQkLFKRdQCqQ3RTr6JaoF1nguV8Pdh9hoikrSfbcXx)_
Fixes #32944
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 改了什么:AI 调用 `query_dataset`(MCP 工具和产品内 AI 对话工具共用同一个执行器)在按 `limit`
截断结果时,`drillRawRows` 和 `drillRanges` 这两个按行对齐的钻取附表现在与 `rows` 一起截到同样长度,第 i
项对应第 i 行。
- 为什么改:工具为了判断 `truncated` 会多取一行,过去只把 `rows` 截掉,附表原样返回,于是 `limit: 1` 时有 1
行却有 2 条 `drillRawRows`,客户端按下标钻取会多出一个看不到的分组。
-
风险与回滚:很小。只影响被截断的回答;未截断的回答完全不变(有对照用例);`totals`、`drillRawTotals`、`dimensionFields`、`drillScopes`
不按行对齐,保持原样。回滚 = revert 本 PR,不涉及数据。
- 先看:`packages/services/service-analytics/src/dataset-tools.ts` 新增的
`ROW_ALIGNED_ANSWER_MEMBERS` 常量和 `queryDataset`
中截断后的循环;`src/__tests__/dataset-tools.test.ts` 新增的 `a truncated answer
serves every row-aligned sidecar` 一组用例。
- 两分钟验证:`pnpm --filter @objectstack/service-analytics exec vitest run
src/__tests__/dataset-tools.test.ts`,预期 32/32 通过,其中 4 条 `row-aligned
sidecar` 用例为绿。
- 需要你做的:无,席位落地。
## What changes
- **`@objectstack/service-analytics`**:
`createDatasetTools(...).queryDataset` asks the service for `cap + 1`
rows so `truncated` is a fact, and serves `cap`. It now serves the
answer's row-aligned members to the same length as `rows` when it
truncates. They are listed once, in `ROW_ALIGNED_ANSWER_MEMBERS`, typed
against `keyof AnalyticsResult`.
- **The sidecars, enumerated from `AnalyticsResult`
(`packages/spec/src/contracts/analytics-service.ts`):**
- Aligned to `rows` by index, trimmed: `drillRawRows` ("aligned to
`rows` by index") and `drillRanges` ("aligned to `rows` by index").
- Not row-indexed, served as answered: `fields` (one entry per column),
`totals` (one per requested grouping, computed over the underlying data,
not the capped rows), `drillRawTotals` (aligned to `totals`),
`dimensionFields` and `drillScopes` (keyed by dimension or measure
name), `object`. `sql` is still stripped.
- **One fix, both faces.** The MCP `query_dataset` tool
(`packages/runtime/src/domains/mcp.ts`) and the chat tool (`execute`)
call this executor, so both are fixed here.
- **The REST door has no over-read.** `POST /analytics/dataset/query`
(`packages/rest/src/analytics-routes.ts`) passes the caller's
`selection` to `AnalyticsService.queryDataset` unchanged and does not
truncate, so its `rows` and sidecars come from one query at one limit.
No change there.
## Tests
`packages/services/service-analytics/src/__tests__/dataset-tools.test.ts`
(+4, one `describe` in the `query_dataset` block):
- `limit: 1` and `limit: 2`, through the plugin-wired service:
`truncated: true`, `rowCount` equals the limit, `drillRawRows.length ===
rows.length`, and `drillRawRows[i]` equals `{ stage: rows[i].stage }`
for every i.
- A service answer carrying every sidecar (`limit: 2` over three rows):
`drillRawRows` and `drillRanges` keep their first two entries; `totals`,
`drillRawTotals`, `dimensionFields` and `drillScopes` equal the
service's answer.
- CONTROL: `limit: 3` over three rows: `truncated: false`, and `rows`
and `drillRawRows` equal the service's own answer.
## Acceptance notes
- Ruling grep on the touched surface (`ruling|Ruling-ref|maintainer|⛔`):
`dataset-tools.ts` L295 and its test L472/L513 cite the admission-first
ruling 6766102988 (which holders are candidates). The change runs after
resolution and names no holder, so it does not touch that ruling.
- Not changed: a `limit: 0` call (cap 0) serves no rows, but
`dimensionFields` and `drillScopes` stay on the answer, though their
contract says they are set only on an answer with rows. `drillRawRows` /
`drillRanges` are now `[]` there. This is a narrow edge that nobody has
reported; left as is.
- New row-aligned members: a future `AnalyticsResult` member aligned to
`rows` has to be added to `ROW_ALIGNED_ANSWER_MEMBERS`. The `satisfies`
check catches a misspelled or removed key at compile time, but it cannot
tell whether a new key is row-aligned.
- Live reproduction: the card's showcase MCP run (QA #32940 P-1) was not
repeated here. The unit pins drive the plugin-wired service, whose
`drillRawRows` comes from the same `analytics-service.ts` pass the
showcase uses.
## Verification
All readings at head `bbc366d8` (base `bf02d33b`; `origin/main` merged
before the PR, already up to date).
<details><summary>Measured at origin/main first (the premise
holds)</summary>
New pins over the unfixed executor (`bf02d33b`), `-t 'row-aligned
sidecar'`: **3 failed | 1 passed (4)**.
- `limit 1`: `expected [ { stage: 'draft' }, …(2) ] to have a length of
1 but got 3`
- `limit 2`: `expected [ { stage: 'draft' }, …(2) ] to have a length of
2 but got 3`
- sidecars: `drillRawRows` kept three entries where two rows were
served.
The engine double returns every row it holds, so at `limit: 1` the
measured surplus is two entries, not one. On a SQL driver, which applies
`LIMIT cap + 1`, it is the single probe row the card shows. The CONTROL
passed.
</details>
<details><summary>Package tests and typecheck
(service-analytics)</summary>
- `pnpm --filter @objectstack/service-analytics test`: Test Files 215
passed (215), Tests 5199 passed | 332 skipped (5531)
- `pnpm --filter @objectstack/service-analytics typecheck`: exit 0; `tsc
--listFiles` includes `src/__tests__/dataset-tools.test.ts` (1 hit)
- `dataset-tools.test.ts`: Tests 32 passed (32)
</details>
<details><summary>Ablations (both red, both restored)</summary>
The test imports the source (`../dataset-tools.js`), so there is no
`dist/` leg.
- Trim removed: `scripts/ablation-replace.mjs` on `dataset-tools.ts`,
anchor `if (truncated) {` + the loop line x1 -> x0, replacement `if
(false as boolean) {` x0 -> x1, blob `b32f38f4` -> `a31b8c2b`. Result:
**3 failed | 1 passed**, every truncation case. Restored: blob == HEAD
(`b32f38f4`), `git diff HEAD` empty.
- `drillRanges` dropped from the list: anchor `['drillRawRows',
'drillRanges'] as const` x1 -> x0, replacement `['drillRawRows'] as
const`, blob `b32f38f4` -> `e1acd138`. Result: **1 failed | 3 passed**,
the sidecar case (`drillRanges` kept 3 entries, expected 2). Restored:
blob == HEAD, `git diff HEAD` empty.
</details>
<details><summary>Derived gates and lint</summary>
`node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack` at `bbc366d8`: 62 families, each run with
stdin closed and its exit code recorded before any pipe. `--ran`
reconciles **62 accounted, 61 run, 1 NOT MEASURED**:
- `pnpm check:dual-build-cjs-loads`: exit 3, PREREQUISITE NOT MET. Its
self-test passed (93 cases); the scan needs `dist/` of packages outside
this diff's closure (hono, account, setup, studio, client, client-react,
…). NOT MEASURED; CI runs it.
The other 61 exit 0, `check:nul-bytes`, `check:engine-double-contract`,
`check:test-source-alias`, `check:cross-package-test-inputs`,
`check:published-files` and `check:adr-0087-registration` among them.
`node scripts/check-plugin-teardown-shape.mjs --self-test` first exited
3 (its pinned fixture commit was not in the clone); after `git fetch
origin 621a487`, exit 0, 48 cases. `pnpm lint`: exit 0. No export was
renamed or removed, so `check-platform-checklist.mjs` is not owed.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01L9qibxxaZKwWGECdLG6N1s)_
Co-authored-by: Claude <noreply@anthropic.com>1 parent 796e9d4 commit 702c562
3 files changed
Lines changed: 99 additions & 0 deletions
File tree
- .changeset
- packages/services/service-analytics/src
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
402 | 402 | | |
403 | 403 | | |
404 | 404 | | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 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 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
405 | 477 | | |
406 | 478 | | |
407 | 479 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
66 | 77 | | |
67 | 78 | | |
68 | 79 | | |
| |||
506 | 517 | | |
507 | 518 | | |
508 | 519 | | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
509 | 526 | | |
510 | 527 | | |
511 | 528 | | |
| |||
0 commit comments