Skip to content

Commit a46e3e5

Browse files
committed
fix(gateway): preserve raw external session aliases
Fixes #86743. Implements raw external session alias preservation during gateway session key canonicalization to prevent session splitting after internal agent activity.
1 parent 21000a3 commit a46e3e5

2 files changed

Lines changed: 152 additions & 1 deletion

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import type { SessionEntry } from "../config/sessions.js";
2+
import type { OpenClawConfig } from "../config/types.openclaw.js";
3+
import { normalizeMainKey, parseAgentSessionKey } from "../routing/session-key.js";
4+
import { normalizeSessionKeyPreservingOpaquePeerIds } from "../sessions/session-key-utils.js";
5+
import {
6+
normalizeLowercaseStringOrEmpty,
7+
normalizeOptionalString,
8+
} from "../shared/string-coerce.js";
9+
10+
function resolvePreservedRawExternalStoreKeyCandidate(params: {
11+
cfg: OpenClawConfig;
12+
key: string;
13+
canonicalKey: string;
14+
}): string | undefined {
15+
const key = normalizeSessionKeyPreservingOpaquePeerIds(params.key);
16+
if (!key || key === params.canonicalKey || !key.includes(":")) {
17+
return undefined;
18+
}
19+
const lowered = normalizeLowercaseStringOrEmpty(key);
20+
const mainKey = normalizeMainKey(params.cfg.session?.mainKey);
21+
if (
22+
lowered === "global" ||
23+
lowered === "unknown" ||
24+
lowered === "main" ||
25+
lowered === mainKey ||
26+
lowered.startsWith("agent:") ||
27+
parseAgentSessionKey(key)
28+
) {
29+
return undefined;
30+
}
31+
return key;
32+
}
33+
34+
function resolveAgentSessionRestPreservingSeparators(key: string): string | undefined {
35+
const normalized = normalizeSessionKeyPreservingOpaquePeerIds(key);
36+
const match = /^agent:([^:]+):(.+)$/.exec(normalized);
37+
const rest = match?.[2];
38+
return rest ? rest : undefined;
39+
}
40+
41+
export function resolvePreservedRawExternalStoreKey(params: {
42+
cfg: OpenClawConfig;
43+
key: string;
44+
canonicalKey: string;
45+
}): { key: string; preserveMissingAlias: boolean } | undefined {
46+
const directKey = resolvePreservedRawExternalStoreKeyCandidate(params);
47+
if (directKey) {
48+
return { key: directKey, preserveMissingAlias: true };
49+
}
50+
const canonicalRest = resolveAgentSessionRestPreservingSeparators(params.canonicalKey);
51+
if (!canonicalRest) {
52+
return undefined;
53+
}
54+
const canonicalRawKey = resolvePreservedRawExternalStoreKeyCandidate({
55+
cfg: params.cfg,
56+
key: canonicalRest,
57+
canonicalKey: params.canonicalKey,
58+
});
59+
return canonicalRawKey ? { key: canonicalRawKey, preserveMissingAlias: false } : undefined;
60+
}
61+
62+
export function normalizeSessionStoreKeys(keys: Iterable<string | undefined>): string[] {
63+
const normalized = new Set<string>();
64+
for (const key of keys) {
65+
const trimmed = normalizeOptionalString(key ?? "") ?? "";
66+
if (trimmed) {
67+
normalized.add(trimmed);
68+
}
69+
}
70+
return Array.from(normalized);
71+
}
72+
73+
export function resolvePreservedRawExternalAliasKeys(params: {
74+
preservedRawKey: string | undefined;
75+
preserveMissingAlias: boolean;
76+
storeKeys: Iterable<string>;
77+
}): string[] {
78+
if (!params.preservedRawKey) {
79+
return [];
80+
}
81+
const aliases = Array.from(params.storeKeys).filter(
82+
(key) => normalizeSessionKeyPreservingOpaquePeerIds(key) === params.preservedRawKey,
83+
);
84+
if (!params.preserveMissingAlias && aliases.length === 0) {
85+
return [];
86+
}
87+
aliases.push(params.preservedRawKey);
88+
return normalizeSessionStoreKeys(aliases);
89+
}
90+
91+
export function resolveGatewaySessionStorePreservedAliasKeys(params: {
92+
cfg: OpenClawConfig;
93+
key: string;
94+
canonicalKey: string;
95+
storeKeys: Iterable<string>;
96+
}): string[] {
97+
const preservedRaw = resolvePreservedRawExternalStoreKey({
98+
cfg: params.cfg,
99+
key: params.key,
100+
canonicalKey: params.canonicalKey,
101+
});
102+
if (!preservedRaw) {
103+
return [];
104+
}
105+
return resolvePreservedRawExternalAliasKeys({
106+
preservedRawKey: preservedRaw.key,
107+
preserveMissingAlias: preservedRaw.preserveMissingAlias,
108+
storeKeys: params.storeKeys,
109+
});
110+
}
111+
112+
export function syncSessionStoreAliases(params: {
113+
store: Record<string, SessionEntry>;
114+
canonicalKey: string;
115+
entry: SessionEntry;
116+
aliasKeys: readonly string[];
117+
}): void {
118+
for (const aliasKey of params.aliasKeys) {
119+
if (aliasKey !== params.canonicalKey) {
120+
params.store[aliasKey] = params.entry;
121+
}
122+
}
123+
}
124+
125+
export function deleteSessionStoreAliases(params: {
126+
store: Record<string, SessionEntry>;
127+
canonicalKey: string;
128+
aliasKeys: readonly string[];
129+
}): void {
130+
for (const aliasKey of params.aliasKeys) {
131+
if (aliasKey !== params.canonicalKey) {
132+
delete params.store[aliasKey];
133+
}
134+
}
135+
}

‎src/gateway/session-utils.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ import {
8787
resolveSessionStoreKey,
8888
resolveStoredSessionKeyForAgentStore,
8989
} from "./session-store-key.js";
90+
import {
91+
resolveGatewaySessionStorePreservedAliasKeys,
92+
syncSessionStoreAliases,
93+
} from "./session-store-aliases.js";
9094
import {
9195
readRecentSessionUsageFromTranscript,
9296
readSessionTitleFieldsFromTranscriptAsync,
@@ -910,12 +914,24 @@ export function migrateAndPruneGatewaySessionStoreKey(params: {
910914
params.store[primaryKey] = freshestMatch.entry;
911915
}
912916
}
917+
const preservedAliasKeys = resolveGatewaySessionStorePreservedAliasKeys({
918+
cfg: params.cfg,
919+
key: params.key,
920+
canonicalKey: primaryKey,
921+
storeKeys: target.storeKeys,
922+
});
923+
syncSessionStoreAliases({
924+
store: params.store,
925+
canonicalKey: primaryKey,
926+
entry: params.store[primaryKey],
927+
aliasKeys: preservedAliasKeys,
928+
});
913929
pruneLegacyStoreKeys({
914930
store: params.store,
915931
canonicalKey: primaryKey,
916932
candidates: target.storeKeys,
917933
});
918-
return { target, primaryKey, entry: params.store[primaryKey] };
934+
return { target, primaryKey, entry: params.store[primaryKey], preservedAliasKeys };
919935
}
920936

921937
export function classifySessionKey(key: string, entry?: SessionEntry): GatewaySessionRow["kind"] {

0 commit comments

Comments
 (0)