diff --git a/src/backend/controllers/drivers/DriverController.test.ts b/src/backend/controllers/drivers/DriverController.test.ts index cabf26e6d..edb57a97e 100644 --- a/src/backend/controllers/drivers/DriverController.test.ts +++ b/src/backend/controllers/drivers/DriverController.test.ts @@ -501,6 +501,29 @@ describe('DriverController.#handleCall (via captured router)', () => { expect(body.service.name).toBe('puter-kvstore'); }); + it('treats null `args` as no arguments', async () => { + const actor = await makeUserActor(); + await server.stores.permission.setFlatUserPerm( + actor.user!.id!, + 'service:puter-subdomains:ii:puter-subdomains', + { + permission: 'service:puter-subdomains:ii:puter-subdomains', + deleted: false, + issuer_user_id: actor.user!.id!, + } as never, + ); + + const res = makeRes(); + const req = makeReq( + { interface: 'puter-subdomains', method: 'select', args: null }, + actor, + ); + await runWithContext({ actor }, () => + routes['POST /call'](req, res as unknown as Response, () => {}), + ); + expect((res.body as { success: boolean }).success).toBe(true); + }); + it('pipes a stream-shaped result instead of JSON', async () => { // Use an ephemeral driver registered onto the controller's bag. const streamPayload = Buffer.from('hello-world'); diff --git a/src/backend/controllers/drivers/DriverController.ts b/src/backend/controllers/drivers/DriverController.ts index 17a483283..ed18e7f81 100644 --- a/src/backend/controllers/drivers/DriverController.ts +++ b/src/backend/controllers/drivers/DriverController.ts @@ -276,8 +276,10 @@ export class DriverController extends PuterController { interface: ifaceName, method, driver: driverName, - args = {}, + args: rawArgs, } = (req.body ?? {}) as Record; + // Drivers read fields off `args`, so a null one counts as absent. + const args = rawArgs ?? {}; if (!ifaceName || typeof ifaceName !== 'string') { throw new HttpError(400, 'Missing or invalid `interface`', { diff --git a/src/backend/drivers/ai-chat/ChatCompletionDriver.test.ts b/src/backend/drivers/ai-chat/ChatCompletionDriver.test.ts index 7f4a162b1..4e5e94054 100644 --- a/src/backend/drivers/ai-chat/ChatCompletionDriver.test.ts +++ b/src/backend/drivers/ai-chat/ChatCompletionDriver.test.ts @@ -355,6 +355,17 @@ describe('ChatCompletionDriver.complete auth and model resolution', () => { ).rejects.toMatchObject({ statusCode: 400 }); }); + it('rejects a model that is not a string with 400', async () => { + await expect( + withTestActor(() => + driver.complete({ + messages: [{ role: 'user', content: 'hi' }], + model: { id: 'gpt' }, + } as unknown as ICompleteArguments), + ), + ).rejects.toMatchObject({ statusCode: 400 }); + }); + it('routes by alias when a provider exposes one (puterId is auto-aliased on the bucket)', async () => { // Inject a model that carries a `puterId` so we can verify the // alias bucket resolves back to the canonical id. Spy on diff --git a/src/backend/drivers/ai-chat/ChatCompletionDriver.ts b/src/backend/drivers/ai-chat/ChatCompletionDriver.ts index 5649fe6d1..86c167562 100644 --- a/src/backend/drivers/ai-chat/ChatCompletionDriver.ts +++ b/src/backend/drivers/ai-chat/ChatCompletionDriver.ts @@ -2130,7 +2130,9 @@ export class ChatCompletionDriver extends PuterDriver { } #resolveModel(modelId: string, provider?: string): IChatModel | null { - const models = this.#modelIdMap[normalizeModelKey(modelId ?? '')]; + // `model` comes straight from the caller and may not be a string. + if (typeof modelId !== 'string') return null; + const models = this.#modelIdMap[normalizeModelKey(modelId)]; if (!models || models.length === 0) return null; // An explicitly requested provider is honoured even if its route is // marked — the caller asked for that one, not for the cheapest hop. diff --git a/src/backend/stores/app/AppStore.js b/src/backend/stores/app/AppStore.js index 6c4a79451..8b6d89337 100644 --- a/src/backend/stores/app/AppStore.js +++ b/src/backend/stores/app/AppStore.js @@ -26,6 +26,7 @@ import { PuterStore, } from '../types'; import { HttpError } from '../../core/http/HttpError.js'; +import { runWithConcurrencyLimit } from '../../util/concurrency.js'; import { isUniqueViolation } from '../../util/dbError.js'; import { validateUrl, @@ -89,6 +90,8 @@ const STATS_CACHE_TTL_SECONDS = 30 * 60; // `app-` + a UUID. ClickHouse's `app_opens.app_uid` is FixedString(40) and // fails the whole query on a longer value. const APP_UID_MAX_BYTES = 40; +// ClickHouse caps the size of one query parameter, and the uid list is one. +const CLICKHOUSE_UIDS_PER_QUERY = 1000; // Period helpers for detailed/grouped stats. Ported from v1 // AppInformationService — queries go straight to ClickHouse/MySQL on demand @@ -1727,20 +1730,30 @@ export class AppStore extends PuterStore { const clickhouse = this.clients.clickhouse; if (clickhouse) { - const res = await clickhouse.query({ - query: ` - SELECT app_uid, - uniqExact((user_id, intDiv(ts, ${APP_OPEN_COUNT_PERIOD_SECONDS}))) AS open_count, - count(DISTINCT user_id) AS user_count - FROM app_opens - WHERE app_uid IN {uids:Array(String)} - GROUP BY app_uid - `, - query_params: { uids: uids.map((u) => String(u)) }, - format: 'JSONEachRow', - }); - const rows = await res.json(); - for (const row of rows) { + const chunks = []; + for (let i = 0; i < uids.length; i += CLICKHOUSE_UIDS_PER_QUERY) { + chunks.push(uids.slice(i, i + CLICKHOUSE_UIDS_PER_QUERY)); + } + const results = await runWithConcurrencyLimit( + chunks, + 4, + async (chunk) => { + const res = await clickhouse.query({ + query: ` + SELECT app_uid, + uniqExact((user_id, intDiv(ts, ${APP_OPEN_COUNT_PERIOD_SECONDS}))) AS open_count, + count(DISTINCT user_id) AS user_count + FROM app_opens + WHERE app_uid IN {uids:Array(String)} + GROUP BY app_uid + `, + query_params: { uids: chunk.map((u) => String(u)) }, + format: 'JSONEachRow', + }); + return res.json(); + }, + ); + for (const row of results.flat()) { out.set(row.app_uid, { open_count: parseInt(row.open_count, 10) || 0, user_count: parseInt(row.user_count, 10) || 0, diff --git a/src/backend/stores/app/AppStore.test.js b/src/backend/stores/app/AppStore.test.js index ab37ee9ac..f474eeacf 100644 --- a/src/backend/stores/app/AppStore.test.js +++ b/src/backend/stores/app/AppStore.test.js @@ -200,6 +200,37 @@ describe('AppStore app stats (cache-on-read)', () => { } }); + it('splits a long uid list across ClickHouse queries', async () => { + const uids = Array.from({ length: 2500 }, () => freshUid()); + const queried = []; + + server.clients.clickhouse = { + query: async ({ query_params }) => { + queried.push(query_params.uids.length); + return { + json: async () => + query_params.uids.map((u) => ({ + app_uid: u, + open_count: '2', + user_count: '1', + })), + }; + }, + }; + + try { + const stats = await appStore.getAppsStats(uids); + expect(queried.sort((a, b) => b - a)).toEqual([1000, 1000, 500]); + expect(stats.size).toBe(2500); + expect(stats.get(uids[2499])).toMatchObject({ + open_count: 2, + user_count: 1, + }); + } finally { + delete server.clients.clickhouse; + } + }); + it('keeps ids longer than an app uid out of the ClickHouse query', async () => { const uid = freshUid(); const longId = `iap-${'a'.repeat(36)}-extra`;