Skip to content

Commit f089cf4

Browse files
committed
fix(gateway): stop fabricating assistant agent id "main" before roster resolution
Three fabrication points told clients the assistant agent was "main" before any roster/config resolution: DEFAULT_ASSISTANT_IDENTITY in src/gateway/assistant-identity.ts, the Control UI store's initial snapshot (ui/src/app/gateway-store.ts:65), and its hello fallback (:336). On installs whose implicit main agent is retired, every page reload fired sessions.catalog.list for the nonexistent agent and flashed 'unknown agent id "main"' in the sidebar. The no-roster state now carries no agent id: DEFAULT_ASSISTANT_IDENTITY drops agentId, resolveAssistantIdentity returns ResolvedAssistantIdentity (agentId required) for config-backed paths, the bootstrap omits the optional assistantAgentId field without config, the UI store starts null, and agent selection adopts the roster default once agents.list arrives.
1 parent c59d381 commit f089cf4

8 files changed

Lines changed: 87 additions & 33 deletions

‎src/gateway/assistant-identity.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ const ASSISTANT_IDENTITY_LIMITS = {
2424
} as const;
2525
type AssistantIdentityField = keyof typeof ASSISTANT_IDENTITY_LIMITS;
2626

27-
export const DEFAULT_ASSISTANT_IDENTITY: AssistantIdentity = {
28-
agentId: "main",
29-
name: "Assistant",
30-
avatar: "A",
31-
};
32-
3327
type AssistantIdentity = {
34-
agentId: string;
3528
name: string;
3629
avatar: string;
3730
emoji?: string;
3831
};
3932

33+
type ResolvedAssistantIdentity = AssistantIdentity & { agentId: string };
34+
35+
export const DEFAULT_ASSISTANT_IDENTITY: AssistantIdentity = {
36+
name: "Assistant",
37+
avatar: "A",
38+
};
39+
4040
function normalizeIdentityValue(
4141
field: AssistantIdentityField,
4242
value: string | undefined,
@@ -100,7 +100,7 @@ export function resolveAssistantIdentity(params: {
100100
cfg: OpenClawConfig;
101101
agentId?: string | null;
102102
workspaceDir?: string | null;
103-
}): AssistantIdentity {
103+
}): ResolvedAssistantIdentity {
104104
const defaultAgentId = normalizeAgentId(resolveDefaultAgentId(params.cfg));
105105
const agentId = normalizeAgentId(params.agentId ?? defaultAgentId);
106106
const isDefaultAgent = agentId === defaultAgentId;

‎src/gateway/control-ui-contract.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export type ControlUiBootstrapConfig = {
137137
assistantAvatarSource?: string | null;
138138
assistantAvatarStatus?: "none" | "local" | "remote" | "data" | null;
139139
assistantAvatarReason?: string | null;
140-
assistantAgentId: string;
140+
assistantAgentId?: string;
141141
serverVersion?: string;
142142
/**
143143
* Git branch of a source-checkout (non-release) gateway install. Omitted for

‎src/gateway/control-ui.http.test.ts‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe("handleControlUiHttpRequest", () => {
9696
assistantAvatarSource?: string | null;
9797
assistantAvatarStatus?: "none" | "local" | "remote" | "data" | null;
9898
assistantAvatarReason?: string | null;
99-
assistantAgentId: string;
99+
assistantAgentId?: string;
100100
devGitBranch?: string;
101101
localMediaPreviewRoots?: string[];
102102
seamColor?: string;
@@ -1150,7 +1150,10 @@ describe("handleControlUiHttpRequest", () => {
11501150
{
11511151
root: { kind: "resolved", path: tmp },
11521152
config: {
1153-
agents: { defaults: { workspace: tmp } },
1153+
agents: {
1154+
defaults: { workspace: tmp },
1155+
list: [{ id: "roboclaw", default: true, workspace: tmp }],
1156+
},
11541157
ui: {
11551158
seamColor: "#1A2b3C",
11561159
assistant: { name: "</script><script>alert(1)//", avatar: "</script>.png" },
@@ -1165,7 +1168,7 @@ describe("handleControlUiHttpRequest", () => {
11651168
expect(parsed.assistantAvatar).toBe("A");
11661169
expect(parsed.assistantAvatarStatus).toBe("none");
11671170
expect(parsed.assistantAvatarReason).toBe("missing");
1168-
expect(parsed.assistantAgentId).toBe("main");
1171+
expect(parsed.assistantAgentId).toBe("roboclaw");
11691172
expect(parsed.seamColor).toBe("#1A2b3C");
11701173
expect(parsed.timeFormat).toBe("auto");
11711174
expect(parsed.terminalEnabled).toBe(true);
@@ -1175,6 +1178,22 @@ describe("handleControlUiHttpRequest", () => {
11751178
});
11761179
});
11771180

1181+
it("omits the assistant agent id without a config snapshot", async () => {
1182+
await withControlUiRoot({
1183+
fn: async (tmp) => {
1184+
const { res, end } = makeMockHttpResponse();
1185+
const handled = await handleControlUiHttpRequest(
1186+
{ url: CONTROL_UI_BOOTSTRAP_CONFIG_PATH, method: "GET" } as IncomingMessage,
1187+
res,
1188+
{ root: { kind: "resolved", path: tmp } },
1189+
);
1190+
1191+
expect(handled).toBe(true);
1192+
expect(parseBootstrapPayload(end)).not.toHaveProperty("assistantAgentId");
1193+
},
1194+
});
1195+
});
1196+
11781197
it("includes the dev checkout branch in bootstrap config", async () => {
11791198
devInstallBranchMock.branch = "feat/dev-branch-badge";
11801199
try {
@@ -1669,7 +1688,7 @@ describe("handleControlUiHttpRequest", () => {
16691688
expect(handled).toBe(true);
16701689
expect(res.statusCode).toBe(200);
16711690
const parsed = parseBootstrapPayload(end);
1672-
expect(parsed.assistantAgentId).toBe("main");
1691+
expect(parsed.assistantAgentId).toBeUndefined();
16731692
},
16741693
});
16751694
},
@@ -1823,7 +1842,7 @@ describe("handleControlUiHttpRequest", () => {
18231842
expect(res.statusCode).not.toBe(404);
18241843
const parsed = parseBootstrapPayload(end);
18251844
expect(parsed.basePath).toBe("");
1826-
expect(parsed.assistantAgentId).toBe("main");
1845+
expect(parsed.assistantAgentId).toBeUndefined();
18271846
},
18281847
});
18291848
});

‎src/gateway/control-ui.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -966,12 +966,15 @@ export async function handleControlUiHttpRequest(
966966
return true;
967967
}
968968
const config = opts?.config;
969-
const identity = config
969+
const resolvedIdentity = config
970970
? resolveAssistantIdentity({ cfg: config, agentId: opts?.agentId })
971-
: DEFAULT_ASSISTANT_IDENTITY;
972-
const avatarProjection = config
973-
? resolveGatewayAssistantAvatar({ cfg: config, identity })
974-
: { avatar: identity.avatar, resolution: null };
971+
: undefined;
972+
const identity = resolvedIdentity ?? DEFAULT_ASSISTANT_IDENTITY;
973+
const assistantAgentId = resolvedIdentity?.agentId;
974+
const avatarProjection =
975+
config && resolvedIdentity
976+
? resolveGatewayAssistantAvatar({ cfg: config, identity: resolvedIdentity })
977+
: { avatar: identity.avatar, resolution: null };
975978
const avatarMeta = controlUiAvatarResolutionMeta(avatarProjection.resolution);
976979
sendJson(res, 200, {
977980
basePath,
@@ -980,10 +983,10 @@ export async function handleControlUiHttpRequest(
980983
assistantAvatarSource: avatarMeta.avatarSource,
981984
assistantAvatarStatus: avatarMeta.avatarStatus,
982985
assistantAvatarReason: avatarMeta.avatarReason,
983-
assistantAgentId: identity.agentId,
986+
...(assistantAgentId ? { assistantAgentId } : {}),
984987
serverVersion: resolveRuntimeServiceVersion(process.env),
985988
devGitBranch: (await resolveDevInstallGitBranch()) ?? undefined,
986-
localMediaPreviewRoots: [...getAgentScopedMediaLocalRoots(config ?? {}, identity.agentId)],
989+
localMediaPreviewRoots: [...getAgentScopedMediaLocalRoots(config ?? {}, assistantAgentId)],
987990
embedSandbox:
988991
config?.gateway?.controlUi?.embedSandbox === "trusted"
989992
? "trusted"

‎ui/src/app/agent-selection.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { GatewayBrowserClient } from "../api/gateway.ts";
33
import type { AgentsListResult } from "../api/types.ts";
44
import { createAgentSelectionCapability } from "./agent-selection.ts";
55

6-
function createGateway(assistantAgentId = "Main") {
6+
function createGateway(assistantAgentId: string | null = "Main") {
77
let snapshot = { client: null as GatewayBrowserClient | null, assistantAgentId };
88
const listeners = new Set<(next: typeof snapshot) => void>();
99
return {
@@ -115,4 +115,20 @@ describe("agent selection", () => {
115115
selection.set("main");
116116
expect(selection.state).toEqual({ selectedId: "roboclaw", scopeId: "roboclaw" });
117117
});
118+
119+
it("waits for the roster when the gateway has no assistant agent id", () => {
120+
const gateway = createGateway(null);
121+
const roster = createRoster();
122+
const selection = createAgentSelectionCapability(gateway.gateway, roster.roster);
123+
124+
expect(selection.state).toEqual({ selectedId: null, scopeId: null });
125+
roster.publish({
126+
defaultId: "Roboclaw",
127+
mainKey: "main",
128+
scope: "per-sender",
129+
agents: [{ id: "roboclaw", kind: "agent" }],
130+
});
131+
132+
expect(selection.state).toEqual({ selectedId: "roboclaw", scopeId: "roboclaw" });
133+
});
118134
});

‎ui/src/app/agent-selection.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ export function createAgentSelectionCapability(
3535
const reconcileSelectedId = (value: string | null): string | null => {
3636
const selectedId = value?.trim() ? normalizeAgentId(value) : null;
3737
const agentsList = roster.state.agentsList;
38-
if (
39-
!selectedId ||
40-
!agentsList ||
41-
agentsList.agents.length === 0 ||
42-
agentsList.agents.some((agent) => normalizeAgentId(agent.id) === selectedId)
43-
) {
38+
if (!agentsList || agentsList.agents.length === 0) {
4439
return selectedId;
4540
}
46-
return normalizeAgentId(agentsList.defaultId);
41+
const defaultId = normalizeAgentId(agentsList.defaultId);
42+
return !selectedId ||
43+
!agentsList.agents.some((agent) => normalizeAgentId(agent.id) === selectedId)
44+
? defaultId
45+
: selectedId;
4746
};
4847
const resolveScopeId = (value: string | null): string | null => {
4948
const scopeId = value?.trim() ? normalizeAgentId(value) : null;
@@ -56,9 +55,10 @@ export function createAgentSelectionCapability(
5655
const initialId = gateway.snapshot.assistantAgentId
5756
? normalizeAgentId(gateway.snapshot.assistantAgentId)
5857
: null;
58+
const initialSelectedId = reconcileSelectedId(initialId);
5959
let state: AgentSelectionState = {
60-
selectedId: initialId,
61-
scopeId: resolveScopeId(initialId),
60+
selectedId: initialSelectedId,
61+
scopeId: resolveScopeId(initialSelectedId),
6262
};
6363
let client = gateway.snapshot.client;
6464
const listeners = new Set<(next: AgentSelectionState) => void>();

‎ui/src/app/gateway-store.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ describe("createApplicationGateway connection phase", () => {
121121
expect(gateway.snapshot.phase).toBe("offline");
122122
});
123123

124+
it("does not invent an assistant agent id before the gateway advertises one", () => {
125+
const { gateway, current } = createStore();
126+
127+
expect(gateway.snapshot.assistantAgentId).toBeNull();
128+
gateway.start();
129+
current().opts.onHello?.(HELLO);
130+
expect(gateway.snapshot.assistantAgentId).toBeNull();
131+
132+
gateway.connect();
133+
current().opts.onHello?.({
134+
...HELLO,
135+
snapshot: { sessionDefaults: { defaultAgentId: "roboclaw" } },
136+
});
137+
expect(gateway.snapshot.assistantAgentId).toBe("roboclaw");
138+
});
139+
124140
it("publishes the hello canvas URL synchronously and clears it on disconnect", () => {
125141
const { gateway, current } = createStore();
126142
gateway.start();

‎ui/src/app/gateway-store.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function createApplicationGateway(
6262
offlineStable: false,
6363
hello: null,
6464
canvasPluginSurfaceUrl: null,
65-
assistantAgentId: "main",
65+
assistantAgentId: null,
6666
sessionKey: settings.sessionKey,
6767
lastError: null,
6868
lastErrorCode: null,
@@ -333,7 +333,7 @@ export function createApplicationGateway(
333333
phase: "connected",
334334
hello,
335335
canvasPluginSurfaceUrl,
336-
assistantAgentId: sessionDefaults?.defaultAgentId ?? "main",
336+
assistantAgentId: sessionDefaults?.defaultAgentId ?? null,
337337
sessionKey,
338338
lastError: null,
339339
lastErrorCode: null,

0 commit comments

Comments
 (0)