Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/35005-grouping-candidate-no-ask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@objectstack/service-analytics': patch
'@objectstack/mcp': patch
---

fix(analytics): a grouping or time word with only a near dimension no longer stops a matched measure with a question

Clause-②: no
Metadata-protocol: none

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.

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.

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."
27 changes: 26 additions & 1 deletion packages/mcp/src/initialize-selection-rule-34686.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
MCP_OAUTH_SCOPE_METADATA_WRITE,
QUERY_DATASET_TOOL_NAME,
} from '@objectstack/spec/ai';
import { DATASET_NEXT_STEP_PRECEDENCE } from '@objectstack/service-analytics';
import { DATASET_NEXT_STEP_PRECEDENCE, getDatasetToolDefinitions } from '@objectstack/service-analytics';
import { MCPServerRuntime } from './mcp-server-runtime.js';
import type { McpDataBridge } from './mcp-http-tools.js';
import type { McpAuthoringInjection } from './types.js';
Expand Down Expand Up @@ -219,3 +219,28 @@ describe('[#34919] initialize instructions — follow nextStep, cite sourceLine'
expect(text).toContain(DATASET_NEXT_STEP_PRECEDENCE.join(', '));
});
});

/**
* [#35005] The ask-back is owed only for a word with a measure among its
* candidates; a word whose candidates are all dimensions is at most a possible
* grouping. The rule states it in the same words as `list_datasets`' own
* description (the real one, read from the executor's package), so the two
* doors cannot drift apart.
*/
describe('[#35005] initialize instructions — the ask-back is scoped in the same words as list_datasets', () => {
const SCOPED =
'A word whose candidates are all dimensions is at most a possible grouping: never ask about it, and it never '
+ 'stops a matched measure.';

it('both doors carry the scoped sentence, and the question names a measure among the candidates', async () => {
const runtime = new MCPServerRuntime({ name: 't', version: '1.0.0' });
const text = await instructionsOf(runtime, makeBridge());
const list = getDatasetToolDefinitions().find((d) => d.name === LIST_DATASETS_TOOL_NAME)!;
expect(text).toContain(SCOPED);
expect(list.description).toContain(SCOPED);
expect(text).toContain('A word with no match and a measure among its candidates in the answer: ask the same way');
// The unscoped form is gone from both doors.
expect(text).not.toContain('A word with no match but with candidates in the answer');
expect(list.description).not.toContain('possible readings: ask the same way');
});
});
10 changes: 8 additions & 2 deletions packages/mcp/src/mcp-server-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,10 @@ const DEFAULT_HTTP_INSTRUCTIONS =
* `list_datasets` `candidates` (the ADR-0149 D3 amendment note): the rule makes
* that the same question, listing each candidate's label, dataset label and
* description, never a pick and never a match, even for a single candidate;
* [#35005] that question is owed only for a word with a measure among its
* candidates: a word whose candidates are all dimensions is at most a possible
* grouping, never asked about and never a stop for a matched measure, in the
* same words as `list_datasets`' description;
* only a word with no match and no candidate falls back to the object tools,
* with the answer labelled. [#34919] The answer itself then states the
* case (`list_datasets` `nextStep`, composed by the service-analytics
Expand All @@ -1824,8 +1828,10 @@ const DATASET_SELECTION_RULE =
+ 'question\'s business words. A word matches only when it equals a measure\'s or dimension\'s name, label or '
+ 'synonym, never a similar word. Each word that matches exactly one measure: answer with query_dataset; two '
+ 'such words are two metrics, so compute both. One word matching several measures: ask the user which one '
+ 'they mean; do not pick. A word with no match but with candidates in the answer: ask the same way, listing '
+ 'each candidate\'s label, dataset label and description; do not pick one or treat it as a match. No match '
+ 'they mean; do not pick. A word with no match and a measure among its candidates in the answer: ask the same '
+ 'way, listing each candidate\'s label, dataset label and description; do not pick one or treat it as a match. '
+ 'A word whose candidates are all dimensions is at most a possible grouping: never ask about it, and it never '
+ 'stops a matched measure. No match '
+ 'and no candidates: fall back to query_records / aggregate_records and say the answer is not from a governed '
+ 'metric. The list_datasets answer states which of these cases your words are in as nextStep: follow its note, '
+ 'which reports the first of several_measures, one_measure, candidates_only, no_measure when words differ. Cite '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const OBJECTS: Record<string, { enable?: unknown; fields: Record<string, Record<
crm_opportunity_line_item: {
fields: { id: { type: 'text' }, product: { type: 'text' }, quantity: { type: 'number' }, amount: { type: 'currency' } },
},
// [#35005] one 销售额 measure beside an industry and a quarter grouping.
sales_order: {
fields: { id: { type: 'text' }, industry: { type: 'text' }, quarter: { type: 'text' }, amount: { type: 'number' }, churn: { type: 'number' } },
},
};

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

it('[#35005] scopes the ask-back to a word with a measure among its candidates', () => {
const [list] = getDatasetToolDefinitions();
expect(list.description).toContain('A word with a measure among its candidates: ask the same way');
expect(list.description).toContain(
'A word whose candidates are all dimensions is at most a possible grouping: never ask about it, and it never '
+ 'stops a matched measure.',
);
});

it('[#34919] says follow nextStep and cite sourceLine, and names the precedence the executor applies', () => {
const [list, query] = getDatasetToolDefinitions();
expect(list.description).toContain('nextStep');
Expand Down Expand Up @@ -1103,6 +1116,140 @@ describe('[#34919] list_datasets nextStep — the precedence among mixed cases',
});
});

/**
* [#35005] An ask-back is owed only for the word being measured. A word whose
* candidates are all dimensions (「按行业」 near 行业, "this quarter" near
* `quarter`) is offered as a possible grouping: no question about it, and no
* stop for a matched measure. A word with a measure among its candidates keeps
* the question. The decision and the answer's `candidates` are unchanged: only
* the sentence is scoped.
*/
describe('[#35005] list_datasets nextStep — a grouping or time word with a candidate does not stop a matched measure', () => {
const SALES_METRICS = {
name: 'sales_metrics',
label: 'Sales Metrics',
object: 'sales_order',
dimensions: [
{ name: 'industry', label: { en: 'Industry', 'zh-CN': '行业' }, field: 'industry', type: 'string' },
{ name: 'quarter', label: { en: 'Quarter', 'zh-CN': '季度' }, field: 'quarter', type: 'string' },
],
measures: [
{ name: 'sales_amount', label: { en: 'Revenue', 'zh-CN': '销售额' }, aggregate: 'sum', field: 'amount' },
{ name: 'churn_rate', label: { en: 'Churn Rate', 'zh-CN': '流失率' }, aggregate: 'avg', field: 'churn' },
],
};
/** The sentence that offers a grouping and asks nothing about it, by the locale the context names. */
const GROUPING_OFFER: Record<string, string> = {
en: 'A possible grouping is not a question: do not ask the user about it',
'zh-CN': '可能的分组维度不需要追问:不要就它问用户',
'zh-TW': '可能的分組維度不需要追問:不要就它問使用者',
'ja-JP': 'グループ分けの候補は確認の対象ではありません',
};
/** The reported candidates_only question: compute nothing until the user answers. */
const ASK_BEFORE_COMPUTING: Record<string, string> = {
en: 'compute nothing until they answer',
'zh-CN': '用户回答前不要计算',
'zh-TW': '使用者回答前不要計算',
'ja-JP': '回答があるまで計算しないでください',
};
const loc = (ctx: ExecutionContext) => String(ctx.locale ?? 'en');

async function ask(terms: string[], ctx: ExecutionContext = ADMIN) {
const { service } = await bootWired();
const out = await service.datasetTools({ loadDatasets: async () => [SALES_METRICS] }).listDatasets({ terms }, ctx);
expectNextStepInvariants(out, terms);
return out;
}

it('「按行业」 + 销售额 ⇒ one_measure, computed with no question back, 行业 offered as a possible grouping', async () => {
for (const ctx of FOUR_LOCALES) {
const out = await ask(['按行业', '销售额'], ctx);
expect(out.nextStep).toMatchObject({ decision: 'one_measure', terms: ['销售额'] });
// The answer's candidates are what they were: the word is still offered its dimension.
expect(out.candidates).toEqual([{ term: '按行业', dataset: 'sales_metrics', member: 'dimensions.industry' }]);
const note = out.nextStep!.note;
expect(note, loc(ctx)).toContain('"sales_amount"');
expect(note, loc(ctx)).toContain('按行业');
expect(note, `${loc(ctx)} ⇒ no ask-first`).not.toContain(ASK_FIRST[loc(ctx)]);
expect(note, `${loc(ctx)} ⇒ no ask-before-computing`).not.toContain(ASK_BEFORE_COMPUTING[loc(ctx)]);
expect(note, `${loc(ctx)} ⇒ grouping offered`).toContain(GROUPING_OFFER[loc(ctx)]);
}
const en = (await ask(['按行业', '销售额'])).nextStep!.note;
expect(en).toContain('"按行业" matches no member exactly; it may name a grouping: Industry (dataset: Sales Metrics).');
expect(en).not.toContain('possible readings');
});

it('"this quarter" + "revenue" ⇒ one_measure, computed with no question back, quarter offered as a possible grouping', async () => {
const out = await ask(['this quarter', 'revenue']);
expect(out.nextStep).toMatchObject({ decision: 'one_measure', terms: ['revenue'] });
expect(out.candidates).toEqual([{ term: 'this quarter', dataset: 'sales_metrics', member: 'dimensions.quarter' }]);
const note = out.nextStep!.note;
expect(note).toContain('"this quarter" matches no member exactly; it may name a grouping: Quarter (dataset: Sales Metrics).');
expect(note).toContain(GROUPING_OFFER.en);
expect(note).not.toContain(ASK_FIRST.en);
// 本季度 contains 季度 the same way.
const zh = await ask(['本季度', '销售额'], { ...ADMIN, locale: 'zh-CN' } as ExecutionContext);
expect(zh.nextStep).toMatchObject({ decision: 'one_measure', terms: ['销售额'] });
expect(zh.nextStep!.note).toContain(GROUPING_OFFER['zh-CN']);
expect(zh.nextStep!.note).not.toContain(ASK_FIRST['zh-CN']);
});

it('a bare near-miss measure word still asks: candidates_only, compute nothing until the user answers', async () => {
for (const ctx of FOUR_LOCALES) {
const out = await ask(['销售'], ctx);
expect(out.nextStep).toMatchObject({ decision: 'candidates_only', terms: ['销售'] });
expect(out.candidates).toEqual([{ term: '销售', dataset: 'sales_metrics', member: 'measures.sales_amount' }]);
expect(out.nextStep!.note, loc(ctx)).toContain(ASK_BEFORE_COMPUTING[loc(ctx)]);
expect(out.nextStep!.note, loc(ctx)).not.toContain(GROUPING_OFFER[loc(ctx)]);
}
});

it('a matched measure beside a word with a measure candidate still asks about that word (营收 + churn)', async () => {
for (const ctx of FOUR_LOCALES) {
const out = await ask(['revenue', 'churn'], ctx);
expect(out.nextStep).toMatchObject({ decision: 'one_measure', terms: ['revenue'] });
expect(out.nextStep!.note, `${loc(ctx)} ⇒ ask first`).toContain(ASK_FIRST[loc(ctx)]);
expect(out.nextStep!.note, loc(ctx)).not.toContain(GROUPING_OFFER[loc(ctx)]);
}
});

it('a word with a measure AND a dimension among its candidates keeps the question', async () => {
const out = await ask(['industry revenue', '销售额']);
expect(out.candidates!.map((c) => c.member)).toEqual(['measures.sales_amount', 'dimensions.industry']);
expect(out.nextStep).toMatchObject({ decision: 'one_measure', terms: ['销售额'] });
expect(out.nextStep!.note).toContain(ASK_FIRST.en);
expect(out.nextStep!.note).not.toContain(GROUPING_OFFER.en);
const alone = await ask(['industry revenue']);
expect(alone.nextStep).toMatchObject({ decision: 'candidates_only', terms: ['industry revenue'] });
expect(alone.nextStep!.note).toContain(ASK_BEFORE_COMPUTING.en);
});

it('both kinds at once: the measure word is asked about and the grouping word is offered', async () => {
const withMeasure = (await ask(['churn', '按行业', '销售额'])).nextStep!;
expect(withMeasure).toMatchObject({ decision: 'one_measure', terms: ['销售额'] });
expect(withMeasure.note).toContain(ASK_FIRST.en);
expect(withMeasure.note).toContain(GROUPING_OFFER.en);
// The ask-first sentence is about churn's readings and comes before the grouping offer.
expect(withMeasure.note.indexOf(ASK_FIRST.en)).toBeLessThan(withMeasure.note.indexOf('"按行业"'));
const noMeasure = (await ask(['销售', '按行业'])).nextStep!;
expect(noMeasure).toMatchObject({ decision: 'candidates_only', terms: ['销售', '按行业'] });
expect(noMeasure.note).toContain(ASK_BEFORE_COMPUTING.en);
expect(noMeasure.note).toContain(GROUPING_OFFER.en);
});

it('a lone grouping word with a candidate keeps candidates_only and asks which number, never about the grouping', async () => {
for (const ctx of FOUR_LOCALES) {
const out = await ask(['this quarter'], ctx);
// The decision is the spec's: a term with entries in `candidates` is candidates_only.
expect(out.nextStep).toMatchObject({ decision: 'candidates_only', terms: ['this quarter'] });
const note = out.nextStep!.note;
expect(note, loc(ctx)).toContain(GROUPING_OFFER[loc(ctx)]);
expect(note, loc(ctx)).not.toContain(ASK_BEFORE_COMPUTING[loc(ctx)]);
}
expect((await ask(['this quarter'])).nextStep!.note).toContain('ask the user which number they mean');
});
});

describe('[#34919] list_datasets nextStep — an incomplete catalogue says so (N4)', () => {
it('a degraded read keeps nextStep over what it lists and ends the note by saying the catalogue was incomplete', async () => {
const { service } = await bootWired();
Expand Down
Loading
Loading