Skip to content

Commit 8a68685

Browse files
OmarMcAdamOmar McAdam
andauthored
fix(app): stabilize remote messaging router health (anomalyco#1070)
Keep worker-scoped router health checks and startup wiring aligned so remote workers stop reporting the router as unconfigured. Normalize orchestrator temp-path assertions so router tests stay stable on macOS. Co-authored-by: Omar McAdam <[email protected]>
1 parent da0cd71 commit 8a68685

7 files changed

Lines changed: 67 additions & 12 deletions

File tree

‎apps/app/src/app/app.tsx‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3900,7 +3900,7 @@ export default function App() {
39003900
const items = Array.isArray(response.items) ? response.items : [];
39013901
const activeMatch = response.activeId ? items.find((item) => item.id === response.activeId) : null;
39023902
const match = root ? items.find((item) => normalizeDirectoryPath(item.path) === root) : activeMatch ?? items[0];
3903-
setDevtoolsWorkspaceId(activeMatch?.id ?? match?.id ?? null);
3903+
setDevtoolsWorkspaceId(match?.id ?? activeMatch?.id ?? null);
39043904
} catch {
39053905
if (active) setDevtoolsWorkspaceId(null);
39063906
}
@@ -4010,7 +4010,6 @@ export default function App() {
40104010
(resolvedOpenworkCapabilities()?.config?.write ?? false),
40114011
);
40124012
const devtoolsCapabilities = createMemo(() => openworkServerCapabilities());
4013-
const resolvedDevtoolsWorkspaceId = createMemo(() => devtoolsWorkspaceId() ?? openworkServerWorkspaceId());
40144013

40154014
function updateOpenworkServerSettings(next: OpenworkServerSettings) {
40164015
const stored = writeOpenworkServerSettings(next);
@@ -7098,7 +7097,7 @@ export default function App() {
70987097
openworkServerHostInfo: openworkServerHostInfo(),
70997098
openworkServerCapabilities: devtoolsCapabilities(),
71007099
openworkServerDiagnostics: openworkServerDiagnostics(),
7101-
openworkServerWorkspaceId: resolvedDevtoolsWorkspaceId(),
7100+
openworkServerWorkspaceId: openworkServerWorkspaceId(),
71027101
activeWorkspaceType: workspaceStore.activeWorkspaceDisplay().workspaceType,
71037102
openworkAuditEntries: openworkAuditEntries(),
71047103
openworkAuditStatus: openworkAuditStatus(),

‎apps/app/src/app/lib/openwork-server.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,14 @@ export function createOpenworkServerClient(options: { baseUrl: string; token?: s
11951195
capabilities: () => requestJson<OpenworkServerCapabilities>(baseUrl, "/capabilities", { token, hostToken, timeoutMs: timeouts.capabilities }),
11961196
opencodeRouterHealth: () =>
11971197
requestJsonRaw<OpenworkOpenCodeRouterHealthSnapshot>(baseUrl, "/opencode-router/health", { token, hostToken, timeoutMs: timeouts.opencodeRouter }),
1198+
getOpenCodeRouterHealth: (workspaceId: string, options?: { healthPort?: number | null }) => {
1199+
const query = typeof options?.healthPort === "number" ? `?healthPort=${encodeURIComponent(String(options.healthPort))}` : "";
1200+
return requestJsonRaw<OpenworkOpenCodeRouterHealthSnapshot>(
1201+
baseUrl,
1202+
`/workspace/${encodeURIComponent(workspaceId)}/opencode-router/health${query}`,
1203+
{ token, hostToken, timeoutMs: timeouts.opencodeRouter },
1204+
);
1205+
},
11981206
opencodeRouterBindings: (filters?: { channel?: string; identityId?: string }) => {
11991207
const search = new URLSearchParams();
12001208
if (filters?.channel?.trim()) search.set("channel", filters.channel.trim());

‎apps/app/src/app/pages/identities.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ export default function IdentitiesView(props: IdentitiesViewProps) {
431431
}
432432

433433
const [healthRes, tgRes, slackRes, telegramInfo] = await Promise.all([
434-
client.opencodeRouterHealth(),
434+
client.getOpenCodeRouterHealth(id),
435435
client.getOpenCodeRouterTelegramIdentities(id),
436436
client.getOpenCodeRouterSlackIdentities(id),
437437
client.getOpenCodeRouterTelegram(id).catch(() => null),

‎apps/app/src/app/pages/settings.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,19 +1046,22 @@ export default function SettingsView(props: SettingsViewProps) {
10461046
},
10471047
services: {
10481048
engine: {
1049+
scope: props.startupPreference === "server" ? "local-desktop" : "local-host",
10491050
status: engineStatusLabel(),
10501051
baseUrl: props.engineInfo?.baseUrl ?? null,
10511052
pid: props.engineInfo?.pid ?? null,
10521053
stdout: engineStdout(),
10531054
stderr: engineStderr(),
10541055
},
10551056
orchestrator: {
1057+
scope: props.startupPreference === "server" ? "local-desktop" : "local-host",
10561058
status: orchestratorStatusLabel(),
10571059
dataDir: props.orchestratorStatus?.dataDir ?? null,
10581060
activeWorkspace: props.orchestratorStatus?.activeId ?? null,
10591061
sidecar: orchestratorSidecarSummary(),
10601062
},
10611063
openworkServer: {
1064+
scope: props.startupPreference === "server" ? "connected-worker" : "local-host",
10621065
status: openworkStatusLabel(),
10631066
baseUrl:
10641067
(props.openworkServerHostInfo?.baseUrl ?? props.openworkServerUrl) ||
@@ -1068,6 +1071,11 @@ export default function SettingsView(props: SettingsViewProps) {
10681071
stderr: openworkStderr(),
10691072
},
10701073
opencodeRouter: {
1074+
scope: props.startupPreference === "server" ? "local-desktop" : "local-host",
1075+
note:
1076+
props.startupPreference === "server"
1077+
? "Local desktop router state. Remote worker router state is inferred through the connected OpenWork server."
1078+
: null,
10711079
status: opencodeRouterStatusLabel(),
10721080
healthPort: props.opencodeRouterInfo?.healthPort ?? null,
10731081
pid: props.opencodeRouterInfo?.pid ?? null,

‎apps/orchestrator/scripts/router.mjs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from "node:assert/strict";
22
import { spawn } from "node:child_process";
33
import { once } from "node:events";
4-
import { mkdtemp, mkdir, rm } from "node:fs/promises";
4+
import { mkdtemp, mkdir, realpath, rm } from "node:fs/promises";
55
import { createServer } from "node:net";
66
import { tmpdir } from "node:os";
77
import { dirname, join, resolve } from "node:path";
@@ -72,6 +72,14 @@ async function runCli(args, dataDir) {
7272
return trimmed ? JSON.parse(trimmed) : null;
7373
}
7474

75+
async function canonicalPath(input) {
76+
try {
77+
return await realpath(input);
78+
} catch {
79+
return resolve(input);
80+
}
81+
}
82+
7583
const root = await mkdtemp(join(tmpdir(), "openwork-orchestrator-router-"));
7684
const dataDir = join(root, "data");
7785
const workspaceA = join(root, "ws-a");
@@ -121,8 +129,8 @@ try {
121129
const pathA = await runCli(["workspace", "path", idA, "--json"], dataDir);
122130
const pathB = await runCli(["workspace", "path", idB, "--json"], dataDir);
123131

124-
assert.equal(pathA.path.directory, workspaceA);
125-
assert.equal(pathB.path.directory, workspaceB);
132+
assert.equal(await canonicalPath(pathA.path.directory), await canonicalPath(workspaceA));
133+
assert.equal(await canonicalPath(pathB.path.directory), await canonicalPath(workspaceB));
126134

127135
const status2 = await runCli(["daemon", "status", "--json"], dataDir);
128136
const pid2 = status2.opencode.pid;
@@ -132,7 +140,7 @@ try {
132140
assert.equal(disposed.disposed, true);
133141

134142
const pathA2 = await runCli(["workspace", "path", idA, "--json"], dataDir);
135-
assert.equal(pathA2.path.directory, workspaceA);
143+
assert.equal(await canonicalPath(pathA2.path.directory), await canonicalPath(workspaceA));
136144

137145
await runCli(["daemon", "stop", "--json"], dataDir);
138146
await Promise.race([once(daemon, "exit"), new Promise((resolve) => setTimeout(resolve, 3000))]);

‎apps/orchestrator/src/cli.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6933,10 +6933,10 @@ async function runStart(args: ParsedArgs) {
69336933
opencodeDirectory: resolvedWorkspace,
69346934
opencodeUsername,
69356935
opencodePassword,
6936-
opencodeRouterHealthPort: opencodeRouterChild
6936+
opencodeRouterHealthPort: opencodeRouterEnabled
69376937
? opencodeRouterHealthPort
69386938
: undefined,
6939-
opencodeRouterDataDir: opencodeRouterChild
6939+
opencodeRouterDataDir: opencodeRouterEnabled
69406940
? (opencodeRouterDataDir ?? undefined)
69416941
: undefined,
69426942
logger,
@@ -7854,10 +7854,10 @@ async function runStart(args: ParsedArgs) {
78547854
opencodeDirectory: resolvedWorkspace,
78557855
opencodeUsername,
78567856
opencodePassword,
7857-
opencodeRouterHealthPort: opencodeRouterReady
7857+
opencodeRouterHealthPort: opencodeRouterEnabled
78587858
? opencodeRouterHealthPort
78597859
: undefined,
7860-
opencodeRouterDataDir: opencodeRouterReady
7860+
opencodeRouterDataDir: opencodeRouterEnabled
78617861
? (opencodeRouterDataDir ?? undefined)
78627862
: undefined,
78637863
logger,

‎apps/server/src/server.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,38 @@ function createRoutes(config: ServerConfig, approvals: ApprovalService, tokens:
15841584
return jsonResponse({ updatedAt: Date.now() });
15851585
});
15861586

1587+
addRoute(routes, "GET", "/workspace/:id/opencode-router/health", "client", async (ctx) => {
1588+
requireClientScope(ctx, "collaborator");
1589+
await resolveWorkspace(config, ctx.params.id);
1590+
1591+
const healthPortParam = parseInteger(ctx.url.searchParams.get("healthPort") ?? undefined);
1592+
const port = healthPortParam ?? resolveOpenCodeRouterHealthPort();
1593+
const requestHost = ctx.url.hostname;
1594+
const apply = await tryFetchOpenCodeRouterHealth("GET", "/health", {
1595+
port,
1596+
requestHost,
1597+
timeoutMs: 2_000,
1598+
});
1599+
1600+
if (apply.applied && apply.body && typeof apply.body === "object") {
1601+
return jsonResponse(apply.body, apply.status ?? 200);
1602+
}
1603+
1604+
if (apply.applied) {
1605+
throw new ApiError(502, "opencodeRouter_invalid_health", "OpenCodeRouter returned an invalid health response", {
1606+
port,
1607+
host: apply.host ?? null,
1608+
hosts: apply.hosts,
1609+
});
1610+
}
1611+
1612+
throw new ApiError(apply.status ?? 503, "opencodeRouter_unreachable", apply.error ?? "OpenCodeRouter health unavailable", {
1613+
port,
1614+
host: apply.host ?? null,
1615+
hosts: apply.hosts,
1616+
});
1617+
});
1618+
15871619
addRoute(routes, "POST", "/workspace/:id/opencode-router/telegram-token", "client", async (ctx) => {
15881620
ensureWritable(config);
15891621
requireClientScope(ctx, "collaborator");

0 commit comments

Comments
 (0)