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
17 changes: 17 additions & 0 deletions .changeset/33028-query-dataset-empty-measures.md
Original file line number Diff line number Diff line change
@@ -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

<!-- adr-0087: not-required (no-migration-prescription) No key of any schema is added, removed, renamed or re-shaped, and no stored shape changes. The refusal is the code, status and sentence the REST door POST /analytics/dataset/query already answers the same selection with, so there is nothing for a migration to rewrite and no tombstone. -->

**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.
Original file line number Diff line number Diff line change
Expand Up @@ -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=` ─────────────────
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
16 changes: 16 additions & 0 deletions packages/services/service-analytics/src/dataset-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Loading