PR #172 fixed the jsonb double-encoding for `tool_allowlist` but the same class of bug, and the same re-derived workaround, remained elsewhere. 1. model_config (agent roles): jsonbObject still used the buggy `::jsonb` bind, so `ai_agent_roles.model_config` round-tripped as a jsonb STRING SCALAR. The read-path `typeof === 'object'` check then failed and the model override was SILENTLY dropped (role fell back to the default model). Fixed to `::text::jsonb` and added `parseModelConfig` + `normalizeRow` so every read self-heals already-corrupted rows (no migration). 2. Centralized the write workaround as `jsonbBind()` in database/utils.ts — one implementation with one explanation of the quirk — replacing the per-repo `jsonbArray` (mcp) and `jsonbObject` (roles). 3. Integration coverage (the fix is a DB round-trip a unit test cannot see; the read-side parser MASKS a write regression): new ai-mcp-server-repo.int-spec asserts `jsonb_typeof(tool_allowlist)='array'` after insert + heals a seeded string-scalar row; ai-agent-roles-repo int-spec gains the same for `model_config` (`'object'` + heal). 4. Updated the stale `ai-mcp-servers.types.ts` comment (the driver returns a JSON string for legacy rows; the repo normalizes every read). 5. Fail-open logging: a corrupt tool_allowlist degrades to "no restriction" (agent gets ALL tools) — normalizeRow now warns (server id only, never contents) so the silent widening leaves a trace. 6. Simplified parseToolAllowlist (normalize the string once, then a single array-of-strings check) — identical behaviour, all 12 cases still pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
95 lines
3.5 KiB
TypeScript
95 lines
3.5 KiB
TypeScript
import { Kysely, sql } from 'kysely';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { AiMcpServerRepo } from '@docmost/db/repos/ai-chat/ai-mcp-server.repo';
|
|
import { getTestDb, destroyTestDb, createWorkspace } from './db';
|
|
|
|
/**
|
|
* AiMcpServerRepo `tool_allowlist` jsonb round-trip (PR #172 / issue #173 §3).
|
|
*
|
|
* The fix under test is a DB round-trip, so a unit test cannot observe it: the
|
|
* write must land as a real jsonb ARRAY (not a double-encoded string scalar),
|
|
* and the read must repair any legacy string-scalar rows. The read-side
|
|
* `parseToolAllowlist` MASKS a write regression (it parses the string back), so
|
|
* without this integration check, reverting `::text::jsonb` to `::jsonb` would
|
|
* keep every unit test green while silently corrupting the column again.
|
|
*/
|
|
describe('AiMcpServerRepo tool_allowlist jsonb round-trip [integration]', () => {
|
|
let db: Kysely<any>;
|
|
let repo: AiMcpServerRepo;
|
|
let ws: string;
|
|
|
|
beforeAll(async () => {
|
|
db = getTestDb();
|
|
repo = new AiMcpServerRepo(db as any);
|
|
ws = (await createWorkspace(db)).id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await destroyTestDb();
|
|
});
|
|
|
|
const jsonbTypeof = async (id: string): Promise<string | null> => {
|
|
const res = await sql<{ t: string | null }>`
|
|
SELECT jsonb_typeof(tool_allowlist) AS t
|
|
FROM ai_mcp_servers WHERE id = ${id}
|
|
`.execute(db);
|
|
return res.rows[0]?.t ?? null;
|
|
};
|
|
|
|
it('insert stores the allowlist as a jsonb ARRAY (not a string scalar)', async () => {
|
|
const row = await repo.insert({
|
|
workspaceId: ws,
|
|
name: `srv-${randomUUID()}`,
|
|
transport: 'http',
|
|
url: 'https://example.com/mcp',
|
|
toolAllowlist: ['search', 'crawl'],
|
|
});
|
|
|
|
// The column holds a real jsonb array — the whole point of ::text::jsonb.
|
|
expect(await jsonbTypeof(row.id)).toBe('array');
|
|
|
|
// And the read returns a genuine string[], not a JSON string.
|
|
const found = await repo.findById(row.id, ws);
|
|
expect(found?.toolAllowlist).toEqual(['search', 'crawl']);
|
|
expect(Array.isArray(found?.toolAllowlist)).toBe(true);
|
|
});
|
|
|
|
it('an empty allowlist is normalized to null (no restriction), not []', async () => {
|
|
const row = await repo.insert({
|
|
workspaceId: ws,
|
|
name: `srv-${randomUUID()}`,
|
|
transport: 'http',
|
|
url: 'https://example.com/mcp',
|
|
toolAllowlist: [],
|
|
});
|
|
// The column is SQL NULL, so jsonb_typeof returns SQL NULL (JS null).
|
|
expect(await jsonbTypeof(row.id)).toBeNull();
|
|
expect((await repo.findById(row.id, ws))?.toolAllowlist).toBeNull();
|
|
});
|
|
|
|
it('repairs a legacy double-encoded (string scalar) row on read (self-heal)', async () => {
|
|
// Seed a row whose tool_allowlist is a jsonb STRING SCALAR holding the JSON
|
|
// text — exactly what the old `::jsonb` double-encoding produced.
|
|
const id = randomUUID();
|
|
await sql`
|
|
INSERT INTO ai_mcp_servers (id, workspace_id, name, transport, url, tool_allowlist)
|
|
VALUES (
|
|
${id}, ${ws}, ${`srv-${id}`}, 'http', 'https://example.com/mcp',
|
|
to_jsonb(${'["alpha","beta"]'}::text)
|
|
)
|
|
`.execute(db);
|
|
|
|
// Sanity: the seeded column really IS the corrupt string-scalar shape.
|
|
expect(await jsonbTypeof(id)).toBe('string');
|
|
|
|
// The repo read heals it back to a real string[].
|
|
expect((await repo.findById(id, ws))?.toolAllowlist).toEqual([
|
|
'alpha',
|
|
'beta',
|
|
]);
|
|
const enabled = await repo.listEnabled(ws);
|
|
const healed = enabled.find((r) => r.id === id);
|
|
expect(healed?.toolAllowlist).toEqual(['alpha', 'beta']);
|
|
});
|
|
});
|