diff --git a/src/mgr/postgres-store.ts b/src/mgr/postgres-store.ts index 2483192..aa76728 100644 --- a/src/mgr/postgres-store.ts +++ b/src/mgr/postgres-store.ts @@ -12,6 +12,7 @@ import { normalizeRunEventPayload, requireEventType } from "../common/events.js" interface PostgresStoreOptions { connectionString: string; + poolMax?: number; } interface MigrationDefinition { @@ -408,7 +409,7 @@ export class PostgresAgentRunStore implements AgentRunStore { private appliedMigrationId: string | null = null; constructor(options: PostgresStoreOptions) { - this.pool = new Pool({ connectionString: options.connectionString, application_name: "agentrun-mgr-v01" }); + this.pool = new Pool({ connectionString: options.connectionString, application_name: "agentrun-mgr-v01", ...(options.poolMax === undefined ? {} : { max: options.poolMax }) }); } async migrate(): Promise { diff --git a/src/mgr/store.ts b/src/mgr/store.ts index 6d91f0b..34decc2 100644 --- a/src/mgr/store.ts +++ b/src/mgr/store.ts @@ -111,13 +111,23 @@ export async function openAgentRunStoreFromEnv(env: NodeJS.ProcessEnv = process. const databaseUrl = env.DATABASE_URL?.trim(); if (databaseUrl) { const { createPostgresAgentRunStore } = await import("./postgres-store.js"); - return createPostgresAgentRunStore({ connectionString: databaseUrl }); + return createPostgresAgentRunStore({ connectionString: databaseUrl, poolMax: optionalPositiveIntegerEnv(env, "AGENTRUN_POSTGRES_POOL_MAX") }); } const storeMode = env.AGENTRUN_STORE ?? env.AGENTRUN_MGR_STORE; if (storeMode === "memory") return new MemoryAgentRunStore(); throw new AgentRunError("infra-failed", "DATABASE_URL is required for agentrun-mgr live runtime; set AGENTRUN_STORE=memory only for explicit self-test/dev mode", { httpStatus: 503, details: { adapter: "postgres", databaseUrl: "missing", memoryFallback: "disabled" } }); } +function optionalPositiveIntegerEnv(env: NodeJS.ProcessEnv, name: string): number | undefined { + const raw = env[name]?.trim(); + if (!raw) return undefined; + const value = Number(raw); + if (!Number.isSafeInteger(value) || value < 1) { + throw new AgentRunError("infra-failed", `${name} must be a positive integer`, { httpStatus: 503, details: { env: name, valuesPrinted: false } }); + } + return value; +} + export class MemoryAgentRunStore implements AgentRunStore { private readonly runs = new Map(); private readonly commands = new Map();