Skip to content

Commit 7ed18b5

Browse files
fix(analytics): a grouping or time word with only a near dimension does not stop a matched measure (#35156)
Part of #35005 Clause-②: no Metadata-protocol: none ## 维护者速读 - **改了什么**:`list_datasets` 的 `nextStep.note` 不再为「候选全是维度」的词追问。「按行业看销售额」里 销售额 精确命中、「按行业」只靠近维度 行业,现在 note 直接让模型计算 销售额,并把 行业 作为「可能的分组」点名,明确说不要就它追问;候选里有指标的词(如单独的「销售」)照旧追问、不计算。`list_datasets` 工具说明和 MCP `initialize` 规则用同一句话写明这条限定。 - **为什么改**:#35005 卡面 p1。普通问法(按行业看销售额、本季度营收)因为分组词/时间词碰巧靠近某个维度,被反问一句,用户体验差;只有被度量的那个词才值得追问。 - **风险与回滚**:只改 note 文案与两处说明文字。`decision`、`terms`、`matches`、`candidates` 对任何输入都与改前一致(规范对 `candidates_only` 的定义不动)。回滚即 revert 本 PR。 - **先看**:`packages/services/service-analytics/src/dataset-next-step.ts`(`readTerm` 的 `groupingOnly` 与 `candidates_only` 分支);`packages/services/service-analytics/src/dataset-tools.ts`(说明文字);`packages/mcp/src/mcp-server-runtime.ts`(`DATASET_SELECTION_RULE`)。 - **两分钟验证**:`pnpm --filter @objectstack/service-analytics exec vitest run src/__tests__/dataset-tools.test.ts`,预期 79 个测试全过,其中 `[#35005]` 一组 7 个覆盖「按行业+销售额 不追问」与「单独 销售 仍追问」。 - **需要你做的**:无,席位落地;ADR-0149 D3 修订注与 `skills/objectstack-ask` 的对应句子由单独的 `domain:skills` 卡改(治理面,不在本 PR)。 ## What changes - `dataset-next-step.ts`: `readTerm` keeps the candidate kind (it was already on the member `memberOf` returns; the type dropped it) and marks a `candidates_only` reading whose candidates are all dimensions as `groupingOnly`. The note offers such a word as a possible grouping (`groupingCandidateHit` + `groupingCandidateTail`, four locales) and never emits the ask-first sentence for it, under `one_measure` (left-out words) and under `candidates_only` (reported words). A word with a measure among its candidates is unchanged: asked about beside a matched measure, and `candidates_only` with compute-nothing when nothing matched. When every reported `candidates_only` word is grouping-only (a lone "this quarter"), the note offers the grouping and adds the existing `groupingTail` (ask which number is meant), as for a word that equals a dimension. - Decision values, `terms`, `matches` and `candidates` are unchanged for every input. The spec defines `candidates_only` as "matched no member and has entries in `candidates`", so a lone grouping-only word stays `candidates_only` (see Acceptance notes). - `LIST_DATASETS_DESCRIPTION` and `DATASET_SELECTION_RULE` both now carry the identical sentence "A word whose candidates are all dimensions is at most a possible grouping: never ask about it, and it never stops a matched measure.", and scope the ask-back to "a word with a measure among its candidates". - One `patch` changeset for `@objectstack/service-analytics` and `@objectstack/mcp`. ## Tests - `service-analytics` `dataset-tools.test.ts`: a new `[#35005]` describe over a dataset with one 销售额 / Revenue measure, a Churn Rate measure, and Industry (行业) / Quarter (季度) dimensions. Pins: 「按行业」+销售额 is `one_measure`, no ask-first sentence, grouping offered, `candidates` still lists `dimensions.industry` (four locales); "this quarter"+"revenue" and 本季度+销售额 likewise; bare 销售 still `candidates_only` with compute-nothing (four locales); revenue+churn still asks about churn (four locales); a word near both a measure and a dimension keeps the question; both kinds at once; a lone "this quarter" keeps `candidates_only`, offers the grouping and asks which number. Plus a description pin for the scoped sentence. - `mcp` `initialize-selection-rule-34686.test.ts`: the `initialize` rule and the real `list_datasets` description (from `getDatasetToolDefinitions`) both contain the scoped sentence, and the unscoped form is gone from both. - Ablation (one-time, not kept): the kind check `const groupingOnly = decision === 'candidates_only' && offered.every((m) => m.kind === 'dimensions');` replaced by `const groupingOnly = false;` via `scripts/ablation-replace.mjs` (anchor 1 to 0, blob bfd1844 to df278719790e): 4 of the 7 new pins red (the grouping ones); the bare-measure, churn and mixed-kind controls stayed green as they should. Restored: blob equals HEAD, `git diff HEAD` empty. The suite imports the executor by relative path, so no dist is involved. ## Acceptance notes - Open sub-part, not taken: reading a lone grouping-only word as `no_measure` instead of `candidates_only`. The spec's `DatasetNextStepDecision` describes `no_measure` as "has no entry in `candidates`", so that relabel would change the published meaning of a decision value. This PR keeps the decision and scopes only the note; whether to relabel is a question for the seat. - The ADR-0149 D3 amendment note still says "With candidates, the AI asks in rule 3's form and acts on none", and `skills/objectstack-ask/SKILL.md` step 2 still asks for every word with candidates. Both are governed surfaces left to the follow-up `domain:skills` card named in the claim. - No dataset eval set exists in `service-analytics`. The scenario cards (`docs/qa/scenarios/*.json`) belong to the eval seat and are not touched here. ## Verification <details><summary>Local runs at 9c885eb</summary> - `pnpm --filter '@objectstack/mcp^...' build`: VERDICT command-exit 0. - `pnpm --filter @objectstack/service-analytics test`: Test Files 218 passed (218), Tests 5592 passed, 335 skipped. - `pnpm --filter @objectstack/mcp test`: Test Files 71 passed (71), Tests 1267 passed (1267). - `pnpm --filter @objectstack/service-analytics typecheck` and `pnpm --filter @objectstack/mcp typecheck` (which includes `check:test-typecheck`): exit 0. Both edited test files are in their package's tsc program (`--listFiles`). - `dispatch-gates --commands` derived 70 families; all 70 run, `--ran` reconciliation: 69 exit 0, 1 NOT MEASURED (`check:dual-build-cjs-loads`, PREREQUISITE NOT MET: it reads every package's dist and only the mcp closure was built). CI runs it. - `overlap-check --branch=HEAD`: VERDICT clean exit 0 (origin/main and #35103 merge cleanly). </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01JQwxbWDsbbJizuYnDwBxT8)_ <!-- signed: repo:objectstack#1 · os-warren · session_01JQwxbWDsbbJizuYnDwBxT8 --> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 961f474 commit 7ed18b5

6 files changed

Lines changed: 286 additions & 28 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@objectstack/service-analytics': patch
3+
'@objectstack/mcp': patch
4+
---
5+
6+
fix(analytics): a grouping or time word with only a near dimension no longer stops a matched measure with a question
7+
8+
Clause-②: no
9+
Metadata-protocol: none
10+
11+
A question like 「按行业看销售额」 or "revenue this quarter" sends `list_datasets` a measure word with an exact match (销售额, revenue) and a grouping or time word that matches nothing exactly but is near a dimension (「按行业」 contains 行业, "this quarter" contains `quarter`). The `nextStep` note used to tell the model to ask the user about that word before using it, so an ordinary question got a clarifying question back. Now a word whose candidates are all dimensions is offered as a possible grouping: the note says not to ask about it and that it does not stop the matched measure, which is computed. A word with a measure among its candidates still asks, as before: a bare near-miss measure word (销售 near 销售额) is still `candidates_only` and computes nothing until the user answers, and beside a matched measure it is still asked about.
12+
13+
What changes is the note only. `decision`, `terms`, `matches` and `candidates` are the same as before for every input: a word with only dimension candidates is still `candidates_only` when it is the reported case, and its candidates are still listed. When such a word is the only word sent, the note offers the grouping and asks which number is meant, as it does for a word that equals a dimension.
14+
15+
The `list_datasets` tool description and the MCP `initialize` selection rule now state the scoped rule in the same words: "A word whose candidates are all dimensions is at most a possible grouping: never ask about it, and it never stops a matched measure."

‎packages/mcp/src/initialize-selection-rule-34686.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
MCP_OAUTH_SCOPE_METADATA_WRITE,
2727
QUERY_DATASET_TOOL_NAME,
2828
} from '@objectstack/spec/ai';
29-
import { DATASET_NEXT_STEP_PRECEDENCE } from '@objectstack/service-analytics';
29+
import { DATASET_NEXT_STEP_PRECEDENCE, getDatasetToolDefinitions } from '@objectstack/service-analytics';
3030
import { MCPServerRuntime } from './mcp-server-runtime.js';
3131
import type { McpDataBridge } from './mcp-http-tools.js';
3232
import type { McpAuthoringInjection } from './types.js';
@@ -219,3 +219,28 @@ describe('[#34919] initialize instructions — follow nextStep, cite sourceLine'
219219
expect(text).toContain(DATASET_NEXT_STEP_PRECEDENCE.join(', '));
220220
});
221221
});
222+
223+
/**
224+
* [#35005] The ask-back is owed only for a word with a measure among its
225+
* candidates; a word whose candidates are all dimensions is at most a possible
226+
* grouping. The rule states it in the same words as `list_datasets`' own
227+
* description (the real one, read from the executor's package), so the two
228+
* doors cannot drift apart.
229+
*/
230+
describe('[#35005] initialize instructions — the ask-back is scoped in the same words as list_datasets', () => {
231+
const SCOPED =
232+
'A word whose candidates are all dimensions is at most a possible grouping: never ask about it, and it never '
233+
+ 'stops a matched measure.';
234+
235+
it('both doors carry the scoped sentence, and the question names a measure among the candidates', async () => {
236+
const runtime = new MCPServerRuntime({ name: 't', version: '1.0.0' });
237+
const text = await instructionsOf(runtime, makeBridge());
238+
const list = getDatasetToolDefinitions().find((d) => d.name === LIST_DATASETS_TOOL_NAME)!;
239+
expect(text).toContain(SCOPED);
240+
expect(list.description).toContain(SCOPED);
241+
expect(text).toContain('A word with no match and a measure among its candidates in the answer: ask the same way');
242+
// The unscoped form is gone from both doors.
243+
expect(text).not.toContain('A word with no match but with candidates in the answer');
244+
expect(list.description).not.toContain('possible readings: ask the same way');
245+
});
246+
});

‎packages/mcp/src/mcp-server-runtime.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,10 @@ const DEFAULT_HTTP_INSTRUCTIONS =
18111811
* `list_datasets` `candidates` (the ADR-0149 D3 amendment note): the rule makes
18121812
* that the same question, listing each candidate's label, dataset label and
18131813
* description, never a pick and never a match, even for a single candidate;
1814+
* [#35005] that question is owed only for a word with a measure among its
1815+
* candidates: a word whose candidates are all dimensions is at most a possible
1816+
* grouping, never asked about and never a stop for a matched measure, in the
1817+
* same words as `list_datasets`' description;
18141818
* only a word with no match and no candidate falls back to the object tools,
18151819
* with the answer labelled. [#34919] The answer itself then states the
18161820
* case (`list_datasets` `nextStep`, composed by the service-analytics
@@ -1824,8 +1828,10 @@ const DATASET_SELECTION_RULE =
18241828
+ 'question\'s business words. A word matches only when it equals a measure\'s or dimension\'s name, label or '
18251829
+ 'synonym, never a similar word. Each word that matches exactly one measure: answer with query_dataset; two '
18261830
+ 'such words are two metrics, so compute both. One word matching several measures: ask the user which one '
1827-
+ 'they mean; do not pick. A word with no match but with candidates in the answer: ask the same way, listing '
1828-
+ 'each candidate\'s label, dataset label and description; do not pick one or treat it as a match. No match '
1831+
+ 'they mean; do not pick. A word with no match and a measure among its candidates in the answer: ask the same '
1832+
+ 'way, listing each candidate\'s label, dataset label and description; do not pick one or treat it as a match. '
1833+
+ 'A word whose candidates are all dimensions is at most a possible grouping: never ask about it, and it never '
1834+
+ 'stops a matched measure. No match '
18291835
+ 'and no candidates: fall back to query_records / aggregate_records and say the answer is not from a governed '
18301836
+ 'metric. The list_datasets answer states which of these cases your words are in as nextStep: follow its note, '
18311837
+ 'which reports the first of several_measures, one_measure, candidates_only, no_measure when words differ. Cite '

‎packages/services/service-analytics/src/__tests__/dataset-tools.test.ts‎

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ const OBJECTS: Record<string, { enable?: unknown; fields: Record<string, Record<
6161
crm_opportunity_line_item: {
6262
fields: { id: { type: 'text' }, product: { type: 'text' }, quantity: { type: 'number' }, amount: { type: 'currency' } },
6363
},
64+
// [#35005] one 销售额 measure beside an industry and a quarter grouping.
65+
sales_order: {
66+
fields: { id: { type: 'text' }, industry: { type: 'text' }, quarter: { type: 'text' }, amount: { type: 'number' }, churn: { type: 'number' } },
67+
},
6468
};
6569

6670
const PIPELINE = {
@@ -179,6 +183,15 @@ describe('the two AIToolDefinitions', () => {
179183
expect(defs[1].description).toContain('provenance');
180184
});
181185

186+
it('[#35005] scopes the ask-back to a word with a measure among its candidates', () => {
187+
const [list] = getDatasetToolDefinitions();
188+
expect(list.description).toContain('A word with a measure among its candidates: ask the same way');
189+
expect(list.description).toContain(
190+
'A word whose candidates are all dimensions is at most a possible grouping: never ask about it, and it never '
191+
+ 'stops a matched measure.',
192+
);
193+
});
194+
182195
it('[#34919] says follow nextStep and cite sourceLine, and names the precedence the executor applies', () => {
183196
const [list, query] = getDatasetToolDefinitions();
184197
expect(list.description).toContain('nextStep');
@@ -1103,6 +1116,140 @@ describe('[#34919] list_datasets nextStep — the precedence among mixed cases',
11031116
});
11041117
});
11051118

1119+
/**
1120+
* [#35005] An ask-back is owed only for the word being measured. A word whose
1121+
* candidates are all dimensions (「按行业」 near 行业, "this quarter" near
1122+
* `quarter`) is offered as a possible grouping: no question about it, and no
1123+
* stop for a matched measure. A word with a measure among its candidates keeps
1124+
* the question. The decision and the answer's `candidates` are unchanged: only
1125+
* the sentence is scoped.
1126+
*/
1127+
describe('[#35005] list_datasets nextStep — a grouping or time word with a candidate does not stop a matched measure', () => {
1128+
const SALES_METRICS = {
1129+
name: 'sales_metrics',
1130+
label: 'Sales Metrics',
1131+
object: 'sales_order',
1132+
dimensions: [
1133+
{ name: 'industry', label: { en: 'Industry', 'zh-CN': '行业' }, field: 'industry', type: 'string' },
1134+
{ name: 'quarter', label: { en: 'Quarter', 'zh-CN': '季度' }, field: 'quarter', type: 'string' },
1135+
],
1136+
measures: [
1137+
{ name: 'sales_amount', label: { en: 'Revenue', 'zh-CN': '销售额' }, aggregate: 'sum', field: 'amount' },
1138+
{ name: 'churn_rate', label: { en: 'Churn Rate', 'zh-CN': '流失率' }, aggregate: 'avg', field: 'churn' },
1139+
],
1140+
};
1141+
/** The sentence that offers a grouping and asks nothing about it, by the locale the context names. */
1142+
const GROUPING_OFFER: Record<string, string> = {
1143+
en: 'A possible grouping is not a question: do not ask the user about it',
1144+
'zh-CN': '可能的分组维度不需要追问:不要就它问用户',
1145+
'zh-TW': '可能的分組維度不需要追問:不要就它問使用者',
1146+
'ja-JP': 'グループ分けの候補は確認の対象ではありません',
1147+
};
1148+
/** The reported candidates_only question: compute nothing until the user answers. */
1149+
const ASK_BEFORE_COMPUTING: Record<string, string> = {
1150+
en: 'compute nothing until they answer',
1151+
'zh-CN': '用户回答前不要计算',
1152+
'zh-TW': '使用者回答前不要計算',
1153+
'ja-JP': '回答があるまで計算しないでください',
1154+
};
1155+
const loc = (ctx: ExecutionContext) => String(ctx.locale ?? 'en');
1156+
1157+
async function ask(terms: string[], ctx: ExecutionContext = ADMIN) {
1158+
const { service } = await bootWired();
1159+
const out = await service.datasetTools({ loadDatasets: async () => [SALES_METRICS] }).listDatasets({ terms }, ctx);
1160+
expectNextStepInvariants(out, terms);
1161+
return out;
1162+
}
1163+
1164+
it('「按行业」 + 销售额 ⇒ one_measure, computed with no question back, 行业 offered as a possible grouping', async () => {
1165+
for (const ctx of FOUR_LOCALES) {
1166+
const out = await ask(['按行业', '销售额'], ctx);
1167+
expect(out.nextStep).toMatchObject({ decision: 'one_measure', terms: ['销售额'] });
1168+
// The answer's candidates are what they were: the word is still offered its dimension.
1169+
expect(out.candidates).toEqual([{ term: '按行业', dataset: 'sales_metrics', member: 'dimensions.industry' }]);
1170+
const note = out.nextStep!.note;
1171+
expect(note, loc(ctx)).toContain('"sales_amount"');
1172+
expect(note, loc(ctx)).toContain('按行业');
1173+
expect(note, `${loc(ctx)} ⇒ no ask-first`).not.toContain(ASK_FIRST[loc(ctx)]);
1174+
expect(note, `${loc(ctx)} ⇒ no ask-before-computing`).not.toContain(ASK_BEFORE_COMPUTING[loc(ctx)]);
1175+
expect(note, `${loc(ctx)} ⇒ grouping offered`).toContain(GROUPING_OFFER[loc(ctx)]);
1176+
}
1177+
const en = (await ask(['按行业', '销售额'])).nextStep!.note;
1178+
expect(en).toContain('"按行业" matches no member exactly; it may name a grouping: Industry (dataset: Sales Metrics).');
1179+
expect(en).not.toContain('possible readings');
1180+
});
1181+
1182+
it('"this quarter" + "revenue" ⇒ one_measure, computed with no question back, quarter offered as a possible grouping', async () => {
1183+
const out = await ask(['this quarter', 'revenue']);
1184+
expect(out.nextStep).toMatchObject({ decision: 'one_measure', terms: ['revenue'] });
1185+
expect(out.candidates).toEqual([{ term: 'this quarter', dataset: 'sales_metrics', member: 'dimensions.quarter' }]);
1186+
const note = out.nextStep!.note;
1187+
expect(note).toContain('"this quarter" matches no member exactly; it may name a grouping: Quarter (dataset: Sales Metrics).');
1188+
expect(note).toContain(GROUPING_OFFER.en);
1189+
expect(note).not.toContain(ASK_FIRST.en);
1190+
// 本季度 contains 季度 the same way.
1191+
const zh = await ask(['本季度', '销售额'], { ...ADMIN, locale: 'zh-CN' } as ExecutionContext);
1192+
expect(zh.nextStep).toMatchObject({ decision: 'one_measure', terms: ['销售额'] });
1193+
expect(zh.nextStep!.note).toContain(GROUPING_OFFER['zh-CN']);
1194+
expect(zh.nextStep!.note).not.toContain(ASK_FIRST['zh-CN']);
1195+
});
1196+
1197+
it('a bare near-miss measure word still asks: candidates_only, compute nothing until the user answers', async () => {
1198+
for (const ctx of FOUR_LOCALES) {
1199+
const out = await ask(['销售'], ctx);
1200+
expect(out.nextStep).toMatchObject({ decision: 'candidates_only', terms: ['销售'] });
1201+
expect(out.candidates).toEqual([{ term: '销售', dataset: 'sales_metrics', member: 'measures.sales_amount' }]);
1202+
expect(out.nextStep!.note, loc(ctx)).toContain(ASK_BEFORE_COMPUTING[loc(ctx)]);
1203+
expect(out.nextStep!.note, loc(ctx)).not.toContain(GROUPING_OFFER[loc(ctx)]);
1204+
}
1205+
});
1206+
1207+
it('a matched measure beside a word with a measure candidate still asks about that word (营收 + churn)', async () => {
1208+
for (const ctx of FOUR_LOCALES) {
1209+
const out = await ask(['revenue', 'churn'], ctx);
1210+
expect(out.nextStep).toMatchObject({ decision: 'one_measure', terms: ['revenue'] });
1211+
expect(out.nextStep!.note, `${loc(ctx)} ⇒ ask first`).toContain(ASK_FIRST[loc(ctx)]);
1212+
expect(out.nextStep!.note, loc(ctx)).not.toContain(GROUPING_OFFER[loc(ctx)]);
1213+
}
1214+
});
1215+
1216+
it('a word with a measure AND a dimension among its candidates keeps the question', async () => {
1217+
const out = await ask(['industry revenue', '销售额']);
1218+
expect(out.candidates!.map((c) => c.member)).toEqual(['measures.sales_amount', 'dimensions.industry']);
1219+
expect(out.nextStep).toMatchObject({ decision: 'one_measure', terms: ['销售额'] });
1220+
expect(out.nextStep!.note).toContain(ASK_FIRST.en);
1221+
expect(out.nextStep!.note).not.toContain(GROUPING_OFFER.en);
1222+
const alone = await ask(['industry revenue']);
1223+
expect(alone.nextStep).toMatchObject({ decision: 'candidates_only', terms: ['industry revenue'] });
1224+
expect(alone.nextStep!.note).toContain(ASK_BEFORE_COMPUTING.en);
1225+
});
1226+
1227+
it('both kinds at once: the measure word is asked about and the grouping word is offered', async () => {
1228+
const withMeasure = (await ask(['churn', '按行业', '销售额'])).nextStep!;
1229+
expect(withMeasure).toMatchObject({ decision: 'one_measure', terms: ['销售额'] });
1230+
expect(withMeasure.note).toContain(ASK_FIRST.en);
1231+
expect(withMeasure.note).toContain(GROUPING_OFFER.en);
1232+
// The ask-first sentence is about churn's readings and comes before the grouping offer.
1233+
expect(withMeasure.note.indexOf(ASK_FIRST.en)).toBeLessThan(withMeasure.note.indexOf('"按行业"'));
1234+
const noMeasure = (await ask(['销售', '按行业'])).nextStep!;
1235+
expect(noMeasure).toMatchObject({ decision: 'candidates_only', terms: ['销售', '按行业'] });
1236+
expect(noMeasure.note).toContain(ASK_BEFORE_COMPUTING.en);
1237+
expect(noMeasure.note).toContain(GROUPING_OFFER.en);
1238+
});
1239+
1240+
it('a lone grouping word with a candidate keeps candidates_only and asks which number, never about the grouping', async () => {
1241+
for (const ctx of FOUR_LOCALES) {
1242+
const out = await ask(['this quarter'], ctx);
1243+
// The decision is the spec's: a term with entries in `candidates` is candidates_only.
1244+
expect(out.nextStep).toMatchObject({ decision: 'candidates_only', terms: ['this quarter'] });
1245+
const note = out.nextStep!.note;
1246+
expect(note, loc(ctx)).toContain(GROUPING_OFFER[loc(ctx)]);
1247+
expect(note, loc(ctx)).not.toContain(ASK_BEFORE_COMPUTING[loc(ctx)]);
1248+
}
1249+
expect((await ask(['this quarter'])).nextStep!.note).toContain('ask the user which number they mean');
1250+
});
1251+
});
1252+
11061253
describe('[#34919] list_datasets nextStep — an incomplete catalogue says so (N4)', () => {
11071254
it('a degraded read keeps nextStep over what it lists and ends the note by saying the catalogue was incomplete', async () => {
11081255
const { service } = await bootWired();

0 commit comments

Comments
 (0)