Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.

## Unreleased

### An empty Bot `PORT` is unset, so NaN never reaches Bun.serve

`PORT=` on `agent-bot` and `agent-langgraph` used to parse as `NaN` (`??` does not treat empty as absent) and `Bun.serve` bound an ephemeral port while compose still published 4200/4201. A prefix typo (`42o0`) started on 42. Empty now means the shipped default; anything that is not a whole port number refuses to start.

### The server connects to Postgres on Windows, and `localhost` is no longer a coin toss

Two separate faults, both of which stop a deployment reaching its own database and neither of which
Expand Down
8 changes: 7 additions & 1 deletion agent-bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EventEncoder } from "@ag-ui/encoder";
import { serve } from "bun";
import OpenAI from "openai";
import { hasManagedAgentToken } from "../../shared/agent-authorisation";
import { listenPort } from "../../shared/listen-port";
import { toProviderMessages } from "./history";

/**
Expand All @@ -16,7 +17,12 @@ import { toProviderMessages } from "./history";
* running where its effects are visible to the person watching.
*/

const PORT = Number.parseInt(process.env.PORT ?? "4200", 10);
const resolvedPort = listenPort(process.env.PORT, 4200);
if (!resolvedPort.ok) {
console.error(resolvedPort.reason);
process.exit(1);
}
const PORT = resolvedPort.port;
const MANAGED_AGENT_TOKEN = process.env.MANAGED_AGENT_TOKEN?.trim();
if (!MANAGED_AGENT_TOKEN) {
console.error(
Expand Down
8 changes: 7 additions & 1 deletion agent-langgraph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { ChatOpenAI } from "@langchain/openai";
import { serve } from "bun";
import { hasManagedAgentToken } from "../../shared/agent-authorisation";
import { listenPort } from "../../shared/listen-port";
import { toLangChainMessages } from "./history";
import { readReasoningEffort } from "./model-options";
import { streamRun } from "./stream";
Expand All @@ -36,7 +37,12 @@ import { streamRun } from "./stream";
* The graph provides model orchestration without changing that contract.
*/

const PORT = Number.parseInt(process.env.PORT ?? "4201", 10);
const resolvedPort = listenPort(process.env.PORT, 4201);
if (!resolvedPort.ok) {
console.error(resolvedPort.reason);
process.exit(1);
}
const PORT = resolvedPort.port;
const MANAGED_AGENT_TOKEN = process.env.MANAGED_AGENT_TOKEN?.trim();
if (!MANAGED_AGENT_TOKEN) {
console.error(
Expand Down
28 changes: 28 additions & 0 deletions shared/listen-port.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, test } from "bun:test";
import { listenPort } from "./listen-port";

/**
* Empty PORT must not become NaN / an ephemeral bind. Same empty-string trap as the supervisor.
*/
describe("Bot listen port", () => {
test("unset and empty string fall back", () => {
expect(listenPort(undefined, 4200)).toEqual({ ok: true, port: 4200 });
expect(listenPort("", 4201)).toEqual({ ok: true, port: 4201 });
expect(listenPort(" ", 4200)).toEqual({ ok: true, port: 4200 });
});

test("a whole number in range is accepted", () => {
expect(listenPort("4200", 4200)).toEqual({ ok: true, port: 4200 });
expect(listenPort("4500", 4200)).toEqual({ ok: true, port: 4500 });
expect(listenPort("1", 4200)).toEqual({ ok: true, port: 1 });
expect(listenPort("65535", 4200)).toEqual({ ok: true, port: 65535 });
});

test("prefix typos and out-of-range values are refused", () => {
expect(listenPort("42o0", 4200).ok).toBe(false);
expect(listenPort("0", 4200).ok).toBe(false);
expect(listenPort("65536", 4200).ok).toBe(false);
expect(listenPort("-1", 4200).ok).toBe(false);
expect(listenPort("1.5", 4200).ok).toBe(false);
});
});
30 changes: 30 additions & 0 deletions shared/listen-port.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Listen port for a Bot process.
*
* An empty `PORT=` (compose blank, leftover `.env` line) is unset, not zero — the same empty-string
* trap #96/#114/#312/#343 found for the server, computer, and supervisor. `??` only fires on
* undefined, so `Number.parseInt("", 10)` used to be `NaN` and `Bun.serve({ port: NaN })` bound an
* ephemeral port while compose still published 4200/4201. Prefix typos (`42o0`) also used to start
* on 42 via parseInt.
*/
export function listenPort(
raw: string | undefined,
fallback: number,
): { ok: true; port: number } | { ok: false; reason: string } {
const trimmed = raw?.trim();
if (!trimmed) return { ok: true, port: fallback };
if (!/^\d+$/.test(trimmed)) {
return {
ok: false,
reason: `PORT must be a whole number from 1 to 65535 (got ${JSON.stringify(raw)}).`,
};
}
const value = Number.parseInt(trimmed, 10);
if (value < 1 || value > 65535) {
return {
ok: false,
reason: `PORT must be a whole number from 1 to 65535 (got ${JSON.stringify(raw)}).`,
};
}
return { ok: true, port: value };
}