Repository navigation
Commit b4ce310
fix(service-analytics)!: query_dataset refuses measures and dimensions the dataset does not declare (#33958)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} -->
_Requested by **Steedos** · [project
thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbKnsd8ZLsq9hBbRRm5JvJb2)_
Fixes #33745
Clause-②: no (narrowing) — query_dataset stops accepting measures and
dimensions the dataset does not declare; one CONTRACT_REVIEW_TIER review
owed before the queue
Metadata-protocol: none
Skills-docs: none — `skills/**` and `content/docs/**` teach no raw field
or ad-hoc `count` through `query_dataset`; the MCP-served skill text
(`packages/mcp/src/skill-md.ts`) already says `selection` takes members
"by the names `list_datasets` gives", which is what is now enforced.
## 维护者速读
- **改了什么**:AI 的数据集查询工具 `query_dataset` 现在只接受数据集里声明过的指标和维度;传入原始字段(如
`amount`)、临时的 `count` 或未声明的维度时,直接返回 400 `INVALID_FIELD`,并列出该用户可用的指标与维度。
- **为什么改**:之前这类请求会被分析服务临时拼出一个聚合并照常返回,答案的「来源」却把它标成数据集的正式指标(「来源:指标 amount
· 数据集 …」),把非指标口径冒充成治理指标;ADR-0149 规定数据集是唯一的声明定义。
- **风险与回滚**:对 AI/MCP
调用方是收紧(BREAKING):以前能答的未声明成员现在被拒,模型需改选声明成员或退回对象查询(非指标口径)。REST
数据集接口、仪表盘、报表不走此工具,不受影响。回滚 = revert 本 PR。
- **先看**:`packages/services/service-analytics/src/dataset-tools.ts` 的
`assertDeclaredMembers`(判定与报错文案,只列本用户可见成员);同文件 `queryDataset`
中调用它的位置(在数据集解析之后、任何读取之前);测试 `src/__tests__/dataset-tools.test.ts` 新增的
describe 块。
- **两分钟验证**:`pnpm --filter @objectstack/service-analytics exec vitest
run src/__tests__/dataset-tools.test.ts`,预期 43 个用例全部通过,其中「refuses a
member the dataset does not declare」一组 11 个。
- **需要你做的**:无,席位落地(落地前欠一次 CONTRACT_REVIEW_TIER 契约复审记录)。
## What changes
- `createDatasetTools` (`@objectstack/service-analytics`, the one
executor behind the product AI's chat tool and the MCP `query_dataset`
on both bridges) judges the selection against the dataset it resolved,
after the dataset resolves and before anything is read. A name in
`selection.measures`, `selection.dimensions` or a
`selection.timeDimensions` entry's `dimension` that is not one of the
dataset's own `measures[].name` / `dimensions[].name` is refused `400
INVALID_FIELD` with `member`, `param` and `cube` (the dataset name),
through `invalidMemberError`, the envelope the service's member-level
refusals already answer. No new error code.
- The message names the measures and dimensions this caller may use (the
catalogue admission `list_datasets` lists from), never a withheld member
or one over an `internal` field, and points at the D3 fallback (object
query tools, 非指标口径).
- Judged against every declared member, so a declared member the caller
may not read still reaches the service's own field gate (`403
PERMISSION_DENIED`), unchanged. A missing or hidden dataset is still
`404 NOT_FOUND` first. `totals.groupings` and `order` keys are already
judged downstream against the selected members.
- `query_dataset`'s tool description now states the rule.
- Changeset `.changeset/33745-query-dataset-declared-members.md`: patch
(`Metadata-protocol: none`), BREAKING banner, ADR-0087 disposition
`not-required (no-migration-prescription)`.
## Tests
- New top-level `describe('query_dataset refuses a member the dataset
does not declare')` in
`packages/services/service-analytics/src/__tests__/dataset-tools.test.ts`:
a raw field as a measure, the ad-hoc `count`, a suffix spelling
`amount_sum`, a dataset-qualified `pipeline.revenue`, an undeclared
measure beside a declared one, a raw field as a dimension and as a time
dimension — each `INVALID_FIELD` / 400 with `member` / `param` / `cube`,
on both faces (`queryDataset` and `execute`), the service never called,
no read. Plus: the message names only caller-usable members; a missing
or hidden dataset stays `NOT_FOUND`; two CONTROLs (every declared
measure, a derived one included, and dimensions are served; a declared
time dimension reaches the service).
- Red first on the `origin/main` executor: 7 failed / 35 passed (before
the qualified-spelling case was added).
- Ablation 1 (the call to `assertDeclaredMembers` removed, via
`scripts/ablation-replace.mjs`, anchor 1 -> 0): 8 failed / 35 passed;
restored, blob equal to HEAD, `git diff HEAD` empty.
- Ablation 2 (the caller-visible filter on the named members widened to
every declared member): 1 failed (`expected ... not to contain
'margin_total'`); restored the same way.
## Acceptance notes
- Hypothesis readings: H1 held (on `origin/main` the executor served
`measures: ['amount']`, `['count']`, `dimensions: ['amount']`,
`pipeline.revenue` and `pipeline.stage`, each with provenance citing the
raw name as a member). H2: the MCP tool (HTTP and stdio bridges,
`packages/runtime/src/domains/mcp.ts`) and the chat registry face share
this executor, so one refusal covers them; the REST door `POST
/analytics/dataset/query` does NOT go through it (it calls
`AnalyticsService.queryDataset` directly) and is not changed: it serves
dashboards, reports and ad-hoc selections, and the card's ruling names
the tool's executor. H3: a dataset declares no implicit `count`; `count`
is minted by the service's `inferMeasure`, so it is refused here. H4:
`INVALID_FIELD` / 400 (ADR-0149 D2 lists it among the codes the tool may
answer; the tool invents no code).
- A `dataset.member` qualified spelling of a DECLARED member is refused
too: it is not a name `list_datasets` lists, and its provenance used to
cite the raw spelling with no label. Called out in case a consumer
relies on it.
- The REST door's closed-member question and the ADR-0149 D2 line are on
#34005 (maintainer decision).
## Dogfood battery (head `3f717733a13e3d0b4cbd952afa4148c5b35b196c`)
- Local run: NOT MEASURED (the shared verify lock was held by long
sibling suites; five attempts ended queue-timeout 99).
- The battery of record is the merge group's required `Dogfood
Regression Gate`, accepted as such by the seat (15:54Z condition,
reading accepted 15:57Z). Its selector runs the full dogfood suite for
this diff: in `ci.yml` the job's `if:` needs `filter.outputs.core !=
'false'`, and the `filter` job forces `core` to `'true'` on
`merge_group`; `OS_SEED_ONLY` is `queue-verdict.outputs.covered ==
'true'`, and `queue-verdict` runs on `push` only, so on `merge_group`
all three shards run `turbo run test --filter=@objectstack/dogfood` in
full. `@objectstack/dogfood` depends on
`@objectstack/service-analytics`, so its test hash moves with this diff
(no cache hit).
- The tree it judges is this head merged into the queue base. A red
shard ejects the PR from the queue; it is then reported here with its
signature, never re-queued blind.
- No dogfood path selects an undeclared member today (the only
`query_dataset` callers select `row_count` / `task_count`), so the
battery guards against regressions rather than exercising the refusal.
## Verification
Session: https://claude.ai/code/session_01QsR2jUg7BKfRBcTPyMxbLh —
readings at the final head are in the os-dev report comment on the card.
Head `3f717733` is `a711894f` with `origin/main` merged (net diff
byte-identical, same patch-id).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- signed: domain:server#5 · zhuangjianguo ·
session_01QsR2jUg7BKfRBcTPyMxbLh -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 04d4114 commit b4ce310
3 files changed
Lines changed: 183 additions & 6 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 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
535 | 535 | | |
536 | 536 | | |
537 | 537 | | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
538 | 624 | | |
539 | 625 | | |
540 | 626 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
57 | | - | |
58 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| |||
89 | 91 | | |
90 | 92 | | |
91 | 93 | | |
92 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
93 | 97 | | |
94 | 98 | | |
95 | 99 | | |
| |||
472 | 476 | | |
473 | 477 | | |
474 | 478 | | |
| 479 | + | |
475 | 480 | | |
476 | 481 | | |
477 | 482 | | |
478 | 483 | | |
479 | | - | |
480 | | - | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
481 | 488 | | |
482 | 489 | | |
483 | 490 | | |
| |||
504 | 511 | | |
505 | 512 | | |
506 | 513 | | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
507 | 517 | | |
508 | 518 | | |
509 | 519 | | |
| |||
554 | 564 | | |
555 | 565 | | |
556 | 566 | | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
557 | 630 | | |
558 | 631 | | |
559 | 632 | | |
| |||
0 commit comments