refactor: centralize agent timeout defaults

This commit is contained in:
Peter Steinberger
2026-01-06 02:48:44 +00:00
parent d83ca74c18
commit 20a361a3cf
9 changed files with 90 additions and 27 deletions

View File

@@ -10,6 +10,7 @@ import {
resolveModelRefFromString,
resolveThinkingDefault,
} from "../agents/model-selection.js";
import { resolveAgentTimeoutMs } from "../agents/timeout.js";
import {
abortEmbeddedPiRun,
isEmbeddedPiRunActive,
@@ -927,14 +928,10 @@ export function createBridgeHandlers(ctx: BridgeHandlersContext) {
const { cfg, storePath, store, entry } = loadSessionEntry(
p.sessionKey,
);
const defaultTimeoutMs = Math.max(
Math.floor((cfg.agent?.timeoutSeconds ?? 600) * 1000),
0,
);
const timeoutMs =
typeof p.timeoutMs === "number" && Number.isFinite(p.timeoutMs)
? Math.max(0, Math.floor(p.timeoutMs))
: defaultTimeoutMs;
const timeoutMs = resolveAgentTimeoutMs({
cfg,
overrideMs: p.timeoutMs,
});
const now = Date.now();
const sessionId = entry?.sessionId ?? randomUUID();
const sessionEntry: SessionEntry = {

View File

@@ -1,6 +1,7 @@
import { randomUUID } from "node:crypto";
import { resolveThinkingDefault } from "../../agents/model-selection.js";
import { resolveAgentTimeoutMs } from "../../agents/timeout.js";
import { agentCommand } from "../../commands/agent.js";
import { type SessionEntry, saveSessionStore } from "../../config/sessions.js";
import { registerAgentRunContext } from "../../infra/agent-events.js";
@@ -188,14 +189,10 @@ export const chatHandlers: GatewayRequestHandlers = {
}
}
const { cfg, storePath, store, entry } = loadSessionEntry(p.sessionKey);
const defaultTimeoutMs = Math.max(
Math.floor((cfg.agent?.timeoutSeconds ?? 600) * 1000),
0,
);
const timeoutMs =
typeof p.timeoutMs === "number" && Number.isFinite(p.timeoutMs)
? Math.max(0, Math.floor(p.timeoutMs))
: defaultTimeoutMs;
const timeoutMs = resolveAgentTimeoutMs({
cfg,
overrideMs: p.timeoutMs,
});
const now = Date.now();
const sessionId = entry?.sessionId ?? randomUUID();
const sessionEntry: SessionEntry = {

View File

@@ -40,6 +40,27 @@ describe("gateway server chat", () => {
await server.close();
});
test("chat.send defaults to agent timeout config", async () => {
testState.agentConfig = { timeoutSeconds: 123 };
const { server, ws } = await startServerWithClient();
await connectOk(ws);
const res = await rpcReq(ws, "chat.send", {
sessionKey: "main",
message: "hello",
idempotencyKey: "idem-timeout-1",
});
expect(res.ok).toBe(true);
const call = vi.mocked(agentCommand).mock.calls.at(-1)?.[0] as
| { timeout?: string }
| undefined;
expect(call?.timeout).toBe("123");
ws.close();
await server.close();
});
test("chat.send blocked by send policy", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-gw-"));
testState.sessionStorePath = path.join(dir, "sessions.json");

View File

@@ -84,6 +84,7 @@ export const cronIsolatedRun = hoisted.cronIsolatedRun;
export const agentCommand = hoisted.agentCommand;
export const testState = {
agentConfig: undefined as Record<string, unknown> | undefined,
sessionStorePath: undefined as string | undefined,
sessionConfig: undefined as Record<string, unknown> | undefined,
allowFrom: undefined as string[] | undefined,
@@ -243,6 +244,7 @@ vi.mock("../config/config.js", async () => {
agent: {
model: "anthropic/claude-opus-4-5",
workspace: path.join(os.tmpdir(), "clawd-gateway-test"),
...testState.agentConfig,
},
whatsapp: {
allowFrom: testState.allowFrom,
@@ -351,6 +353,7 @@ export function installGatewayTestHooks() {
testState.cronStorePath = undefined;
testState.sessionConfig = undefined;
testState.sessionStorePath = undefined;
testState.agentConfig = undefined;
testState.allowFrom = undefined;
testIsNixMode.value = false;
cronIsolatedRun.mockClear();