diff --git a/.changeset/33028-query-dataset-empty-measures.md b/.changeset/33028-query-dataset-empty-measures.md new file mode 100644 index 00000000000..48ddf5a4951 --- /dev/null +++ b/.changeset/33028-query-dataset-empty-measures.md @@ -0,0 +1,17 @@ +--- +'@objectstack/service-analytics': patch +--- + +fix(service-analytics)!: `query_dataset` refuses a selection that names no measure, as the REST dataset door does + +Clause-②: no (narrowing) — a `query_dataset` call whose `selection.measures` is empty, which answered 500 `DATABASE_ERROR` (or, with dimensions, group rows carrying no measure), is refused 400 `VALIDATION_FAILED` before anything is read, on the MCP tool and on the product AI's chat tool alike. +Metadata-protocol: none + + + +**BREAKING** for a caller that sent `query_dataset` a selection with `measures: []`. + +FROM: the call reached the dataset query with nothing to project. The SQL driver answered `500 DATABASE_ERROR` with its own statement error, and with `dimensions` the tool served group rows carrying no measure. +TO: the call is refused `400 VALIDATION_FAILED`, `body.selection.measures must be a non-empty array of measure names.`, before the dataset is resolved or any row is read: the answer `POST /api/v1/analytics/dataset/query` gives the same selection. The fix for a caller: name at least one measure from the dataset, as `list_datasets` lists them. + +The check sits in the dataset-tools executor that both faces call, the MCP `query_dataset` tool and the chat tool, so one guard covers both. A selection naming a measure is served exactly as before. diff --git a/packages/qa/dogfood/test/dataset-package-scoped-readers.dogfood.test.ts b/packages/qa/dogfood/test/dataset-package-scoped-readers.dogfood.test.ts index 360e5a12c50..c86d1713dd7 100644 --- a/packages/qa/dogfood/test/dataset-package-scoped-readers.dogfood.test.ts +++ b/packages/qa/dogfood/test/dataset-package-scoped-readers.dogfood.test.ts @@ -204,6 +204,30 @@ describe('[#32072] the dataset readers resolve a dataset by package (ADR-0150 D3 expect(viaTool.isError, viaTool.text).toBe(false); expect(JSON.parse(viaTool.text).provenance.dataset.name).toBe(SOLO); }); + + // A selection naming no measure has nothing to project. The REST door + // refused it at its entry; the MCP tool reached the SQL driver and answered + // 500 DATABASE_ERROR with the driver's text (with dimensions, group rows + // carrying no measure). The two doors live in packages that do not depend on + // each other, so this is where one caller holds them to one answer: the same + // code, status and sentence. + it.each([ + ['without dimensions', { measures: [] }], + ['with dimensions', { measures: [], dimensions: ['status'] }], + ])('measures: [] %s is refused 400 VALIDATION_FAILED, in the same words, on the REST door and the MCP tool', async (_case, selection) => { + const res = await stack.apiAs(admin, 'POST', '/analytics/dataset/query', { datasetName: SOLO, selection }); + const restText = await res.text(); + const rest = JSON.parse(restText); + expect(res.status, restText).toBe(400); + expect(codeOf(rest)).toBe('VALIDATION_FAILED'); + expect(messageOf(rest)).toBe('body.selection.measures must be a non-empty array of measure names.'); + + const viaTool = await tool('query_dataset', { datasetName: SOLO, selection }); + expect(viaTool.isError, viaTool.text).toBe(true); + const { error } = JSON.parse(viaTool.text); + expect({ code: error.code, status: error.status, message: error.message }) + .toEqual({ code: codeOf(rest), status: res.status, message: messageOf(rest) }); + }); }); // ── [#32310, ADR-0150 D3] the caller arm: `?callerPackage=` ───────────────── diff --git a/packages/services/service-analytics/src/__tests__/dataset-tools.test.ts b/packages/services/service-analytics/src/__tests__/dataset-tools.test.ts index f36d333280e..fd561b17b81 100644 --- a/packages/services/service-analytics/src/__tests__/dataset-tools.test.ts +++ b/packages/services/service-analytics/src/__tests__/dataset-tools.test.ts @@ -410,6 +410,47 @@ describe('query_dataset', () => { ).rejects.toMatchObject({ code: 'SERVICE_UNAVAILABLE', status: 503 }); }); + // A selection naming no measure has nothing to project. The spec selection + // accepts `measures: []`, so it used to reach `service.queryDataset`, whose + // SELECT had an empty projection: the SQL driver answered 500 + // `DATABASE_ERROR` with its own text, and with dimensions the tool served + // group rows carrying no measure. The tool now refuses it before anything is + // read, with the REST door's answer to the same selection + // (`POST /analytics/dataset/query`: 400 `VALIDATION_FAILED`, the same + // sentence). Both faces this executor serves are pinned: the direct member + // the MCP bridge calls, and `execute`, the registry face the product AI's + // chat tool calls. The dogfood suite holds the REST door and the MCP tool to + // one answer for one caller. + it.each([ + ['without dimensions', { measures: [] }], + ['with dimensions', { measures: [], dimensions: ['stage'] }], + ])('refuses measures: [] %s 400 VALIDATION_FAILED in the REST door\'s words, on both faces, reading nothing', async (_case, selection) => { + const { service, reads } = await bootWired(); + const spy = vi.spyOn(service, 'queryDataset'); + const tools = service.datasetTools({ loadDatasets: loadSaved }); + const input = { datasetName: 'pipeline', selection }; + const direct = await tools.queryDataset(input, ME).catch((e) => e); + const chat = await tools.execute({ toolName: 'query_dataset', input }, ME).catch((e) => e); + for (const e of [direct, chat]) { + expect(e).toMatchObject({ code: 'VALIDATION_FAILED', status: 400 }); + expect(messageOf(e)).toBe('body.selection.measures must be a non-empty array of measure names.'); + } + expect(spy).not.toHaveBeenCalled(); + expect(reads).toEqual([]); + }); + + it('CONTROL: a selection naming a measure, with dimensions, is served on both faces as before', async () => { + const { service } = await bootWired([{ stage: 'won', revenue: 100 }]); + const tools = service.datasetTools({ loadDatasets: loadSaved }); + const input = { datasetName: 'pipeline', selection: { measures: ['revenue'], dimensions: ['stage'] } }; + const direct = await tools.queryDataset(input, ME); + const chat = await tools.execute({ toolName: 'query_dataset', input }, ME); + for (const out of [direct, chat] as Array<{ rows: unknown[]; rowCount: number }>) { + expect(out.rows).toEqual([{ stage: 'won', revenue: 100 }]); + expect(out.rowCount).toBe(1); + } + }); + it('dispatches by tool name through execute, and refuses an unknown one', async () => { const { service } = await bootWired(); const tools = service.datasetTools({ loadDatasets: loadSaved }); diff --git a/packages/services/service-analytics/src/dataset-tools.ts b/packages/services/service-analytics/src/dataset-tools.ts index d4372aa5499..788afd5bf24 100644 --- a/packages/services/service-analytics/src/dataset-tools.ts +++ b/packages/services/service-analytics/src/dataset-tools.ts @@ -192,6 +192,18 @@ function refusal(code: string, status: number, message: string, details?: unknow return err; } +/** + * The sentence `query_dataset` refuses a selection naming no measure with: the + * REST door's answer to the same selection (`POST /analytics/dataset/query`, + * `answerDatasetQuery` in `@objectstack/rest`), with its code and status. The + * spec selection accepts `measures: []`, and a query with nothing to project + * reached the SQL driver as a SELECT with an empty projection (500 + * `DATABASE_ERROR`, the driver's own text) or, with dimensions, served group + * rows carrying no measure. Neither package depends on the other, so the + * dogfood suite holds the two doors to one answer for one caller. + */ +const SELECTION_MEASURES_REQUIRED = 'body.selection.measures must be a non-empty array of measure names.'; + /** * [ADR-0149 D4] The plugin-wired service, or the refusal. `SERVICE_UNAVAILABLE` * / 503: nothing about the caller's request is wrong — the deployment cannot @@ -426,6 +438,10 @@ export function createDatasetTools(deps: DatasetToolsDeps): DatasetTools { // caller's own selection, never a parse output. const raw = input as { datasetName: string; selection: DatasetSelection }; const datasetName = raw.datasetName; + // Judged before anything is read, as the REST door judges it before it + // resolves the dataset: a missing dataset with no measure is this 400 on + // both doors, never a 404 on one of them. + if (raw.selection.measures.length === 0) throw refusal('VALIDATION_FAILED', 400, SELECTION_MEASURES_REQUIRED); let loaded: LoadedDatasets; try {