|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#35138] A `query_records` page that hit its row cap says so with |
| 5 | + * `truncated: true`, on both doors, so a model does not sum or rank one page |
| 6 | + * as if it were the whole set. |
| 7 | + * |
| 8 | + * The flag is decided by a probe: the handler asks the bridge for one row |
| 9 | + * more than the page, serves the page, and flags it when the extra row came |
| 10 | + * back. A page holding exactly `limit` rows and no more carries no flag. The |
| 11 | + * flag is present only when true. |
| 12 | + * |
| 13 | + * The two doors are the two bridges the tool runs over: the HTTP door's |
| 14 | + * (`packages/runtime` `domains/mcp.ts`, `callData('query')` into the data |
| 15 | + * door's `findData`, which answers a counted `total` and `hasMore`; the stand |
| 16 | + * in below answers the same page facts) and the stdio door's |
| 17 | + * (`createStdioDataBridge`, over a stand-in engine). The stdio bridge used to |
| 18 | + * answer `total: records.length` with no `hasMore`, so a capped stdio page |
| 19 | + * claimed to be whole; it now counts, as the data door does, and never states |
| 20 | + * a total it did not count. |
| 21 | + * |
| 22 | + * Where the host's read window (`OS_DATA_MAX_PAGE_SIZE` / |
| 23 | + * `OS_DATA_QUERY_ROW_BUDGET`) would refuse the probe row, the page is read as |
| 24 | + * asked and the door's counted `hasMore` decides. |
| 25 | + */ |
| 26 | + |
| 27 | +import { afterEach, describe, it, expect, vi } from 'vitest'; |
| 28 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 29 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 30 | +import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'; |
| 31 | +import type { ExecutionContext } from '@objectstack/spec/kernel'; |
| 32 | +import type { IDataEngine, IMetadataService } from '@objectstack/spec/contracts'; |
| 33 | +import { createStdioDataBridge } from './stdio-data-bridge.js'; |
| 34 | +import { registerObjectTools, type McpDataBridge } from './mcp-http-tools.js'; |
| 35 | + |
| 36 | +const OBJECT = 'cap_deal'; |
| 37 | +const DEF = { name: OBJECT, fields: { name: { type: 'text' } } }; |
| 38 | +const PRINCIPAL = { userId: 'u1', isSystem: false } as unknown as ExecutionContext; |
| 39 | + |
| 40 | +const rowsOf = (n: number) => Array.from({ length: n }, (_, i) => ({ id: `d${i + 1}`, name: `deal ${i + 1}` })); |
| 41 | + |
| 42 | +type QueryOpts = { limit?: number; offset?: number }; |
| 43 | + |
| 44 | +/** The HTTP door's bridge: the data door's `findData` page facts — a counted total and `hasMore`. */ |
| 45 | +function httpDoor(stored: number) { |
| 46 | + const rows = rowsOf(stored); |
| 47 | + const asked: QueryOpts[] = []; |
| 48 | + const bridge = { |
| 49 | + async listObjects() { return [{ name: OBJECT, label: 'Deal' }]; }, |
| 50 | + async describeObject(name: string) { return { name }; }, |
| 51 | + async query(object: string, opts: QueryOpts) { |
| 52 | + asked.push({ limit: opts.limit, offset: opts.offset }); |
| 53 | + const offset = opts.offset ?? 0; |
| 54 | + const records = rows.slice(offset, opts.limit === undefined ? undefined : offset + opts.limit); |
| 55 | + return { object, records, total: rows.length, hasMore: offset + records.length < rows.length }; |
| 56 | + }, |
| 57 | + async get(object: string, id: string) { return { object, id }; }, |
| 58 | + async create(object: string, data: unknown) { return { object, data }; }, |
| 59 | + async update(object: string, id: string) { return { object, id }; }, |
| 60 | + async remove(object: string, id: string) { return { object, id, success: true }; }, |
| 61 | + } as unknown as McpDataBridge; |
| 62 | + return { bridge, asked }; |
| 63 | +} |
| 64 | + |
| 65 | +/** The stdio door's bridge, over a stand-in engine that honours `limit` / `offset` and counts. */ |
| 66 | +function stdioDoor(stored: number, opts: { countThrows?: boolean } = {}) { |
| 67 | + const rows = rowsOf(stored); |
| 68 | + const asked: QueryOpts[] = []; |
| 69 | + const engine = { |
| 70 | + find: vi.fn(async (_object: string, query: QueryOpts) => { |
| 71 | + asked.push({ limit: query.limit, offset: query.offset }); |
| 72 | + const offset = query.offset ?? 0; |
| 73 | + return rows.slice(offset, query.limit === undefined ? undefined : offset + query.limit); |
| 74 | + }), |
| 75 | + count: vi.fn(async () => { |
| 76 | + if (opts.countThrows) throw new Error('count unavailable'); |
| 77 | + return rows.length; |
| 78 | + }), |
| 79 | + }; |
| 80 | + const metadataService = { getObject: vi.fn(async (name: string) => (name === OBJECT ? DEF : undefined)) }; |
| 81 | + const bridge = createStdioDataBridge({ |
| 82 | + engine: engine as unknown as IDataEngine, |
| 83 | + metadataService: metadataService as unknown as IMetadataService, |
| 84 | + resolvePrincipal: async () => PRINCIPAL, |
| 85 | + }); |
| 86 | + return { bridge, asked, engine }; |
| 87 | +} |
| 88 | + |
| 89 | +async function queryRecords(bridge: McpDataBridge, args: Record<string, unknown>): Promise<any> { |
| 90 | + const server = new McpServer({ name: 'pin', version: '0' }); |
| 91 | + registerObjectTools(server, bridge); |
| 92 | + const [clientSide, serverSide] = InMemoryTransport.createLinkedPair(); |
| 93 | + await server.connect(serverSide); |
| 94 | + const client = new Client({ name: 'pin', version: '0' }); |
| 95 | + await client.connect(clientSide); |
| 96 | + try { |
| 97 | + const res = (await client.callTool({ |
| 98 | + name: 'query_records', |
| 99 | + arguments: { objectName: OBJECT, ...args }, |
| 100 | + })) as { isError?: boolean; content: Array<{ text: string }> }; |
| 101 | + expect(res.isError, res.content[0]?.text).toBeFalsy(); |
| 102 | + return JSON.parse(res.content[0]!.text); |
| 103 | + } finally { |
| 104 | + await client.close(); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +const DOORS = [ |
| 109 | + ['HTTP', httpDoor], |
| 110 | + ['stdio', stdioDoor], |
| 111 | +] as const; |
| 112 | + |
| 113 | +afterEach(() => { |
| 114 | + vi.unstubAllEnvs(); |
| 115 | +}); |
| 116 | + |
| 117 | +describe('query_records — a capped page says `truncated: true` (#35138)', () => { |
| 118 | + for (const [door, make] of DOORS) { |
| 119 | + it(`${door} door: a page that hit its cap answers truncated: true, with the page only`, async () => { |
| 120 | + const { bridge, asked } = make(5); |
| 121 | + const payload = await queryRecords(bridge, { limit: 3 }); |
| 122 | + expect(payload.truncated).toBe(true); |
| 123 | + expect(payload.records.map((r: { id: string }) => r.id)).toEqual(['d1', 'd2', 'd3']); |
| 124 | + expect(payload.total).toBe(5); |
| 125 | + expect(payload.hasMore).toBe(true); |
| 126 | + // The probe: one row past the page was asked for. |
| 127 | + expect(asked).toEqual([{ limit: 4, offset: undefined }]); |
| 128 | + }); |
| 129 | + |
| 130 | + it(`${door} door: a page with exactly limit rows and no more answers no truncated`, async () => { |
| 131 | + const { bridge } = make(3); |
| 132 | + const payload = await queryRecords(bridge, { limit: 3 }); |
| 133 | + expect('truncated' in payload).toBe(false); |
| 134 | + expect(payload.records).toHaveLength(3); |
| 135 | + expect(payload.total).toBe(3); |
| 136 | + expect(payload.hasMore).toBe(false); |
| 137 | + }); |
| 138 | + |
| 139 | + it(`${door} door: the last page of a paged read is not capped`, async () => { |
| 140 | + const { bridge } = make(5); |
| 141 | + const payload = await queryRecords(bridge, { limit: 3, offset: 3 }); |
| 142 | + expect('truncated' in payload).toBe(false); |
| 143 | + expect(payload.records.map((r: { id: string }) => r.id)).toEqual(['d4', 'd5']); |
| 144 | + expect(payload.hasMore).toBe(false); |
| 145 | + }); |
| 146 | + |
| 147 | + it(`${door} door: where the read window would refuse the probe row, the page is read as asked and hasMore decides`, async () => { |
| 148 | + vi.stubEnv('OS_DATA_MAX_PAGE_SIZE', '3'); |
| 149 | + const capped = make(5); |
| 150 | + const payload = await queryRecords(capped.bridge, { limit: 3 }); |
| 151 | + expect(capped.asked).toEqual([{ limit: 3, offset: undefined }]); |
| 152 | + expect(payload.truncated).toBe(true); |
| 153 | + expect(payload.records).toHaveLength(3); |
| 154 | + |
| 155 | + const whole = make(3); |
| 156 | + expect('truncated' in (await queryRecords(whole.bridge, { limit: 3 }))).toBe(false); |
| 157 | + }); |
| 158 | + } |
| 159 | +}); |
| 160 | + |
| 161 | +describe('stdio query: the page facts are counted, never the page length (#35138)', () => { |
| 162 | + it('a capped page answers the match total and hasMore, not total: records.length', async () => { |
| 163 | + const { bridge, engine } = stdioDoor(5); |
| 164 | + const page: any = await bridge.query(OBJECT, { limit: 3 }); |
| 165 | + expect(page.records).toHaveLength(3); |
| 166 | + expect(page.total).toBe(5); |
| 167 | + expect(page.hasMore).toBe(true); |
| 168 | + // Counted as the caller, under the caller's filter. |
| 169 | + expect(engine.count).toHaveBeenCalledWith(OBJECT, {}, { context: PRINCIPAL }); |
| 170 | + }); |
| 171 | + |
| 172 | + it('a count that fails states no total at all', async () => { |
| 173 | + const { bridge } = stdioDoor(5, { countThrows: true }); |
| 174 | + const page: any = await bridge.query(OBJECT, { limit: 3 }); |
| 175 | + expect(page.records).toHaveLength(3); |
| 176 | + expect('total' in page).toBe(false); |
| 177 | + expect(page.hasMore).toBe(true); |
| 178 | + }); |
| 179 | + |
| 180 | + it('an unpaged read is the whole set: its length is the total, and no count runs', async () => { |
| 181 | + const { bridge, engine } = stdioDoor(4); |
| 182 | + const page: any = await bridge.query(OBJECT, {}); |
| 183 | + expect(page.total).toBe(4); |
| 184 | + expect(page.hasMore).toBe(false); |
| 185 | + expect(engine.count).not.toHaveBeenCalled(); |
| 186 | + }); |
| 187 | +}); |
0 commit comments