fix: stabilize gateway config tests + tool schema

This commit is contained in:
Peter Steinberger
2026-01-15 05:11:54 +00:00
parent 4fb114dcb3
commit 757243993c
7 changed files with 43 additions and 26 deletions

View File

@@ -2,20 +2,19 @@ import type { AgentElevatedAllowFromConfig } from "./types.base.js";
export type ToolProfileId = "minimal" | "coding" | "messaging" | "full"; export type ToolProfileId = "minimal" | "coding" | "messaging" | "full";
export type ToolPolicyConfig = {
allow?: string[];
deny?: string[];
profile?: ToolProfileId;
};
export type AgentToolsConfig = { export type AgentToolsConfig = {
/** Base tool profile applied before allow/deny lists. */ /** Base tool profile applied before allow/deny lists. */
profile?: ToolProfileId; profile?: ToolProfileId;
allow?: string[]; allow?: string[];
deny?: string[]; deny?: string[];
/** Optional tool policy overrides keyed by provider id or "provider/model". */ /** Optional tool policy overrides keyed by provider id or "provider/model". */
byProvider?: Record< byProvider?: Record<string, ToolPolicyConfig>;
string,
{
profile?: ToolProfileId;
allow?: string[];
deny?: string[];
}
>;
/** Per-agent elevated exec gate (can only further restrict global tools.elevated). */ /** Per-agent elevated exec gate (can only further restrict global tools.elevated). */
elevated?: { elevated?: {
/** Enable or disable elevated mode for this agent (default: true). */ /** Enable or disable elevated mode for this agent (default: true). */
@@ -83,14 +82,7 @@ export type ToolsConfig = {
allow?: string[]; allow?: string[];
deny?: string[]; deny?: string[];
/** Optional tool policy overrides keyed by provider id or "provider/model". */ /** Optional tool policy overrides keyed by provider id or "provider/model". */
byProvider?: Record< byProvider?: Record<string, ToolPolicyConfig>;
string,
{
profile?: ToolProfileId;
allow?: string[];
deny?: string[];
}
>;
web?: { web?: {
search?: { search?: {
/** Enable web search tool (default: true when API key is present). */ /** Enable web search tool (default: true when API key is present). */

View File

@@ -146,10 +146,10 @@ export const ToolProfileSchema = z
.union([z.literal("minimal"), z.literal("coding"), z.literal("messaging"), z.literal("full")]) .union([z.literal("minimal"), z.literal("coding"), z.literal("messaging"), z.literal("full")])
.optional(); .optional();
const ByProviderPolicySchema = z.object({ export const ToolPolicyWithProfileSchema = z.object({
profile: ToolProfileSchema,
allow: z.array(z.string()).optional(), allow: z.array(z.string()).optional(),
deny: z.array(z.string()).optional(), deny: z.array(z.string()).optional(),
profile: ToolProfileSchema,
}); });
// Provider docking: allowlists keyed by provider id (no schema updates when adding providers). // Provider docking: allowlists keyed by provider id (no schema updates when adding providers).
@@ -176,7 +176,7 @@ export const AgentToolsSchema = z
profile: ToolProfileSchema, profile: ToolProfileSchema,
allow: z.array(z.string()).optional(), allow: z.array(z.string()).optional(),
deny: z.array(z.string()).optional(), deny: z.array(z.string()).optional(),
byProvider: z.record(z.string(), ByProviderPolicySchema).optional(), byProvider: z.record(z.string(), ToolPolicyWithProfileSchema).optional(),
elevated: z elevated: z
.object({ .object({
enabled: z.boolean().optional(), enabled: z.boolean().optional(),
@@ -280,7 +280,7 @@ export const ToolsSchema = z
profile: ToolProfileSchema, profile: ToolProfileSchema,
allow: z.array(z.string()).optional(), allow: z.array(z.string()).optional(),
deny: z.array(z.string()).optional(), deny: z.array(z.string()).optional(),
byProvider: z.record(z.string(), ByProviderPolicySchema).optional(), byProvider: z.record(z.string(), ToolPolicyWithProfileSchema).optional(),
web: ToolsWebSchema, web: ToolsWebSchema,
audio: z audio: z
.object({ .object({

View File

@@ -119,7 +119,7 @@ export const agentHandlers: GatewayRequestHandlers = {
undefined, undefined,
errorShape( errorShape(
ErrorCodes.INVALID_REQUEST, ErrorCodes.INVALID_REQUEST,
`invalid agent params: unknown channel: ${normalized}`, `invalid agent params: unknown channel: ${String(normalized)}`,
), ),
); );
return; return;

View File

@@ -1,6 +1,11 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import { connectOk, installGatewayTestHooks, onceMessage, startServerWithClient } from "./test-helpers.js"; import {
connectOk,
installGatewayTestHooks,
onceMessage,
startServerWithClient,
} from "./test-helpers.js";
installGatewayTestHooks(); installGatewayTestHooks();

View File

@@ -68,6 +68,17 @@ const hoisted = vi.hoisted(() => ({
}, },
})); }));
const testConfigRoot = {
value: path.join(
os.tmpdir(),
`clawdbot-gateway-test-${process.pid}-${crypto.randomUUID()}`,
),
};
export const setTestConfigRoot = (root: string) => {
testConfigRoot.value = root;
};
export const bridgeStartCalls = hoisted.bridgeStartCalls; export const bridgeStartCalls = hoisted.bridgeStartCalls;
export const bridgeInvoke = hoisted.bridgeInvoke; export const bridgeInvoke = hoisted.bridgeInvoke;
export const bridgeListConnected = hoisted.bridgeListConnected; export const bridgeListConnected = hoisted.bridgeListConnected;
@@ -157,7 +168,7 @@ vi.mock("../config/sessions.js", async () => {
vi.mock("../config/config.js", async () => { vi.mock("../config/config.js", async () => {
const actual = await vi.importActual<typeof import("../config/config.js")>("../config/config.js"); const actual = await vi.importActual<typeof import("../config/config.js")>("../config/config.js");
const resolveConfigPath = () => path.join(os.homedir(), ".clawdbot", "clawdbot.json"); const resolveConfigPath = () => path.join(testConfigRoot.value, "clawdbot.json");
const hashConfigRaw = (raw: string | null) => const hashConfigRaw = (raw: string | null) =>
crypto.createHash("sha256").update(raw ?? "").digest("hex"); crypto.createHash("sha256").update(raw ?? "").digest("hex");
@@ -233,8 +244,12 @@ vi.mock("../config/config.js", async () => {
return { return {
...actual, ...actual,
CONFIG_PATH_CLAWDBOT: resolveConfigPath(), get CONFIG_PATH_CLAWDBOT() {
STATE_DIR_CLAWDBOT: path.dirname(resolveConfigPath()), return resolveConfigPath();
},
get STATE_DIR_CLAWDBOT() {
return path.dirname(resolveConfigPath());
},
get isNixMode() { get isNixMode() {
return testIsNixMode.value; return testIsNixMode.value;
}, },

View File

@@ -21,6 +21,7 @@ import {
embeddedRunMock, embeddedRunMock,
piSdkMock, piSdkMock,
sessionStoreSaveDelayMs, sessionStoreSaveDelayMs,
setTestConfigRoot,
testIsNixMode, testIsNixMode,
testState, testState,
testTailnetIPv4, testTailnetIPv4,
@@ -28,6 +29,7 @@ import {
let previousHome: string | undefined; let previousHome: string | undefined;
let tempHome: string | undefined; let tempHome: string | undefined;
let tempConfigRoot: string | undefined;
export function installGatewayTestHooks() { export function installGatewayTestHooks() {
beforeEach(async () => { beforeEach(async () => {
@@ -35,6 +37,8 @@ export function installGatewayTestHooks() {
previousHome = process.env.HOME; previousHome = process.env.HOME;
tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-gateway-home-")); tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-gateway-home-"));
process.env.HOME = tempHome; process.env.HOME = tempHome;
tempConfigRoot = path.join(tempHome, ".clawdbot-test");
setTestConfigRoot(tempConfigRoot);
sessionStoreSaveDelayMs.value = 0; sessionStoreSaveDelayMs.value = 0;
testTailnetIPv4.value = undefined; testTailnetIPv4.value = undefined;
testState.gatewayBind = undefined; testState.gatewayBind = undefined;
@@ -81,6 +85,7 @@ export function installGatewayTestHooks() {
}); });
tempHome = undefined; tempHome = undefined;
} }
tempConfigRoot = undefined;
}); });
} }

View File

@@ -59,7 +59,7 @@ export async function resolveMessageChannelSelection(params: {
const normalized = normalizeMessageChannel(params.channel); const normalized = normalizeMessageChannel(params.channel);
if (normalized) { if (normalized) {
if (!isKnownChannel(normalized)) { if (!isKnownChannel(normalized)) {
throw new Error(`Unknown channel: ${normalized}`); throw new Error(`Unknown channel: ${String(normalized)}`);
} }
return { return {
channel: normalized as MessageChannelId, channel: normalized as MessageChannelId,