-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix: derive runtime ports from cli port #795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mturac
wants to merge
2
commits into
rohitg00:main
Choose a base branch
from
mturac:fix/issue-750-derived-runtime-ports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+183
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| const DEFAULT_REST_PORT = 3111; | ||
|
|
||
| function parsePort(value: string | undefined): number | null { | ||
| if (!value) return null; | ||
| if (!/^\d+$/.test(value)) return null; | ||
| const port = Number(value); | ||
| if (!Number.isInteger(port) || port < 1 || port > 65535) return null; | ||
| return port; | ||
| } | ||
|
|
||
| function setIfUnset(env: NodeJS.ProcessEnv, key: string, value: number | string): void { | ||
| if (!env[key]) env[key] = String(value); | ||
| } | ||
|
|
||
| export function configuredRuntimePorts(env: NodeJS.ProcessEnv = process.env): { | ||
| restPort: number; | ||
| streamPort: number; | ||
| enginePort: number; | ||
| } { | ||
| const restPort = parsePort(env["III_REST_PORT"]) ?? DEFAULT_REST_PORT; | ||
| return { | ||
| restPort, | ||
| streamPort: | ||
| parsePort(env["III_STREAMS_PORT"]) ?? | ||
| parsePort(env["III_STREAM_PORT"]) ?? | ||
| restPort + 1, | ||
| enginePort: | ||
| parsePort(env["III_ENGINE_PORT"]) ?? | ||
| parsePort(env["III_PORT"]) ?? | ||
| (() => { | ||
| try { | ||
| const port = new URL(env["III_ENGINE_URL"] || "").port; | ||
| return parsePort(port) ?? restPort + 3; | ||
| } catch { | ||
| return restPort + 3; | ||
| } | ||
| })(), | ||
| }; | ||
| } | ||
|
|
||
| export function applyPortFlag(args: string[], env: NodeJS.ProcessEnv = process.env): void { | ||
| const portIdx = args.indexOf("--port"); | ||
| if (portIdx === -1 || !args[portIdx + 1]) return; | ||
|
|
||
| const restPort = parsePort(args[portIdx + 1]); | ||
| if (!restPort) return; | ||
|
|
||
| if (restPort === DEFAULT_REST_PORT) return; | ||
|
|
||
| const streamPort = restPort + 1; | ||
| const viewerPort = restPort + 2; | ||
| const enginePort = restPort + 3; | ||
| if (enginePort > 65535) return; | ||
|
|
||
| env["III_REST_PORT"] = String(restPort); | ||
| setIfUnset(env, "III_STREAMS_PORT", streamPort); | ||
| setIfUnset(env, "III_STREAM_PORT", streamPort); | ||
| setIfUnset(env, "AGENTMEMORY_VIEWER_PORT", viewerPort); | ||
| setIfUnset(env, "III_VIEWER_PORT", viewerPort); | ||
| setIfUnset(env, "III_PORT", enginePort); | ||
| setIfUnset(env, "III_ENGINE_PORT", enginePort); | ||
| setIfUnset(env, "III_ENGINE_URL", `ws://localhost:${enginePort}`); | ||
| } | ||
|
|
||
| export function renderRuntimeIiiConfig( | ||
| config: string, | ||
| env: NodeJS.ProcessEnv = process.env, | ||
| ): string { | ||
| const { restPort, streamPort, enginePort } = configuredRuntimePorts(env); | ||
| let currentWorker = ""; | ||
| let sawTopLevelPort = false; | ||
|
|
||
| const lines = config.split(/\r?\n/).map((line) => { | ||
| const worker = line.match(/^\s*-\s+name:\s*([A-Za-z0-9_-]+)\s*$/); | ||
| if (worker) currentWorker = worker[1]; | ||
|
|
||
| if (/^port:\s*\d+\s*$/.test(line)) { | ||
| sawTopLevelPort = true; | ||
| return `port: ${enginePort}`; | ||
| } | ||
|
|
||
| if (/^\s+port:\s*\d+\s*$/.test(line)) { | ||
| if (currentWorker === "iii-http") { | ||
| return line.replace(/\d+/, String(restPort)); | ||
| } | ||
| if (currentWorker === "iii-stream") { | ||
| return line.replace(/\d+/, String(streamPort)); | ||
| } | ||
| } | ||
| return line; | ||
| }); | ||
|
|
||
| if (!sawTopLevelPort) lines.unshift(`port: ${enginePort}`, ""); | ||
| return lines.join("\n"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { readFileSync } from "node:fs"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { applyPortFlag, renderRuntimeIiiConfig } from "../src/cli/runtime-ports.js"; | ||
|
|
||
| describe("runtime port derivation (#750)", () => { | ||
| it("derives sibling ports when --port targets a non-default instance", () => { | ||
| const env: NodeJS.ProcessEnv = {}; | ||
| applyPortFlag(["--port", "3211"], env); | ||
|
|
||
| expect(env.III_REST_PORT).toBe("3211"); | ||
| expect(env.III_STREAMS_PORT).toBe("3212"); | ||
| expect(env.III_STREAM_PORT).toBe("3212"); | ||
| expect(env.AGENTMEMORY_VIEWER_PORT).toBe("3213"); | ||
| expect(env.III_VIEWER_PORT).toBe("3213"); | ||
| expect(env.III_PORT).toBe("3214"); | ||
| expect(env.III_ENGINE_PORT).toBe("3214"); | ||
| expect(env.III_ENGINE_URL).toBe("ws://localhost:3214"); | ||
| }); | ||
|
|
||
| it("respects explicit sibling port overrides", () => { | ||
| const env: NodeJS.ProcessEnv = { | ||
| III_STREAMS_PORT: "4300", | ||
| III_PORT: "49000", | ||
| III_ENGINE_URL: "ws://127.0.0.1:49000", | ||
| AGENTMEMORY_VIEWER_PORT: "4400", | ||
| }; | ||
| applyPortFlag(["--port", "3211"], env); | ||
|
|
||
| expect(env.III_REST_PORT).toBe("3211"); | ||
| expect(env.III_STREAMS_PORT).toBe("4300"); | ||
| expect(env.III_PORT).toBe("49000"); | ||
| expect(env.III_ENGINE_URL).toBe("ws://127.0.0.1:49000"); | ||
| expect(env.AGENTMEMORY_VIEWER_PORT).toBe("4400"); | ||
| }); | ||
|
|
||
| it("ignores --port values that would overflow derived sibling ports", () => { | ||
| const env: NodeJS.ProcessEnv = {}; | ||
| applyPortFlag(["--port", "65533"], env); | ||
|
|
||
| expect(env.III_REST_PORT).toBeUndefined(); | ||
| expect(env.III_STREAMS_PORT).toBeUndefined(); | ||
| expect(env.AGENTMEMORY_VIEWER_PORT).toBeUndefined(); | ||
| expect(env.III_ENGINE_PORT).toBeUndefined(); | ||
| expect(env.III_ENGINE_URL).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("renders a runtime iii config with derived ports without changing bundled defaults", () => { | ||
| const nativeConfig = readFileSync("iii-config.yaml", "utf-8"); | ||
| const rendered = renderRuntimeIiiConfig(nativeConfig, { | ||
| III_REST_PORT: "3211", | ||
| III_STREAMS_PORT: "3212", | ||
| III_PORT: "3214", | ||
| }); | ||
|
|
||
| expect(nativeConfig).toContain("port: 3111"); | ||
| expect(nativeConfig).toContain("port: 3112"); | ||
| expect(rendered).toContain("port: 3214"); | ||
| expect(rendered).toContain("port: 3211"); | ||
| expect(rendered).toContain("port: 3212"); | ||
| }); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.