Skip to content

Commit 726ef48

Browse files
committed
fix(tui): accept canonical session-key aliases in chat event routing
1 parent 0e2bc58 commit 726ef48

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Docs: https://docs.openclaw.ai
4141
- Gateway/chat streaming tool-boundary text retention: merge assistant delta segments into per-run chat buffers so pre-tool text is preserved in live chat deltas/finals when providers emit post-tool assistant segments as non-prefix snapshots. (#36957) Thanks @Datyedyeguy.
4242
- TUI/model indicator freshness: prevent stale session snapshots from overwriting freshly patched model selection (and reset per-session freshness when switching session keys) so `/model` updates reflect immediately instead of lagging by one or more commands. (#21255) Thanks @kowza.
4343
- TUI/final-error rendering fallback: when a chat `final` event has no renderable assistant content but includes envelope `errorMessage`, render the formatted error text instead of collapsing to `"(no output)"`, preserving actionable failure context in-session. (#14687) Thanks @Mquarmoc.
44+
- TUI/session-key alias event matching: treat chat events whose session keys are canonical aliases (for example `agent:<id>:main` vs `main`) as the same session while preserving cross-agent isolation, so assistant replies no longer disappear or surface in another terminal window due to strict key-form mismatch. (#33937) Thanks @yjh1412.
4445
- OpenAI Codex OAuth/login hardening: fail OAuth completion early when the returned token is missing `api.responses.write`, and allow `openclaw models auth login --provider openai-codex` to use the built-in OAuth path even when no provider plugins are installed. (#36660) Thanks @driesvints.
4546
- OpenAI Codex OAuth/scope request parity: augment the OAuth authorize URL with required API scopes (`api.responses.write`, `model.request`, `api.model.read`) before browser handoff so OAuth tokens include runtime model/request permissions expected by OpenAI API calls. (#24720) Thanks @Skippy-Gunboat.
4647
- Onboarding/API key input hardening: strip non-Latin1 Unicode artifacts from normalized secret input (while preserving Latin-1 content and internal spaces) so malformed copied API keys cannot trigger HTTP header `ByteString` construction crashes; adds regression coverage for shared normalization and MiniMax auth header usage. (#24496) Thanks @fa6maalassaf.

src/tui/tui-event-handlers.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,44 @@ describe("tui-event-handlers: handleAgentEvent", () => {
193193
expect(chatLog.startTool).toHaveBeenCalledWith("tc1", "exec", undefined);
194194
});
195195

196+
it("accepts chat events when session key is an alias of the active canonical key", () => {
197+
const { state, chatLog, handleChatEvent } = createHandlersHarness({
198+
state: {
199+
currentSessionKey: "agent:main:main",
200+
activeChatRunId: null,
201+
},
202+
});
203+
204+
handleChatEvent({
205+
runId: "run-alias",
206+
sessionKey: "main",
207+
state: "delta",
208+
message: { content: "hello" },
209+
});
210+
211+
expect(state.activeChatRunId).toBe("run-alias");
212+
expect(chatLog.updateAssistant).toHaveBeenCalledWith("hello", "run-alias");
213+
});
214+
215+
it("does not cross-match canonical session keys from different agents", () => {
216+
const { chatLog, handleChatEvent } = createHandlersHarness({
217+
state: {
218+
currentAgentId: "alpha",
219+
currentSessionKey: "agent:alpha:main",
220+
activeChatRunId: null,
221+
},
222+
});
223+
224+
handleChatEvent({
225+
runId: "run-other-agent",
226+
sessionKey: "agent:beta:main",
227+
state: "delta",
228+
message: { content: "should be ignored" },
229+
});
230+
231+
expect(chatLog.updateAssistant).not.toHaveBeenCalled();
232+
});
233+
196234
it("clears run mapping when the session changes", () => {
197235
const { state, chatLog, tui, handleChatEvent, handleAgentEvent } = createHandlersHarness({
198236
state: { activeChatRunId: null },

src/tui/tui-event-handlers.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseAgentSessionKey } from "../sessions/session-key-utils.js";
12
import { asString, extractTextFromMessage, isCommandMessage } from "./tui-formatters.js";
23
import { TuiStreamAssembler } from "./tui-stream-assembler.js";
34
import type { AgentEvent, ChatEvent, TuiStateAccess } from "./tui-types.js";
@@ -146,13 +147,36 @@ export function createEventHandlers(context: EventHandlerContext) {
146147
void loadHistory?.();
147148
};
148149

150+
const isSameSessionKey = (left: string | undefined, right: string | undefined): boolean => {
151+
const normalizedLeft = (left ?? "").trim().toLowerCase();
152+
const normalizedRight = (right ?? "").trim().toLowerCase();
153+
if (!normalizedLeft || !normalizedRight) {
154+
return false;
155+
}
156+
if (normalizedLeft === normalizedRight) {
157+
return true;
158+
}
159+
const parsedLeft = parseAgentSessionKey(normalizedLeft);
160+
const parsedRight = parseAgentSessionKey(normalizedRight);
161+
if (parsedLeft && parsedRight) {
162+
return parsedLeft.agentId === parsedRight.agentId && parsedLeft.rest === parsedRight.rest;
163+
}
164+
if (parsedLeft) {
165+
return parsedLeft.rest === normalizedRight;
166+
}
167+
if (parsedRight) {
168+
return normalizedLeft === parsedRight.rest;
169+
}
170+
return false;
171+
};
172+
149173
const handleChatEvent = (payload: unknown) => {
150174
if (!payload || typeof payload !== "object") {
151175
return;
152176
}
153177
const evt = payload as ChatEvent;
154178
syncSessionKey();
155-
if (evt.sessionKey !== state.currentSessionKey) {
179+
if (!isSameSessionKey(evt.sessionKey, state.currentSessionKey)) {
156180
return;
157181
}
158182
if (finalizedRuns.has(evt.runId)) {

0 commit comments

Comments
 (0)