Skip to content

Commit f0aee74

Browse files
obviyusAlix-007
andcommitted
fix(agents): preserve opaque session key casing
Use the canonical store-key normalizer when subagent reconciliation falls back from an exact lookup. Co-authored-by: Alix-007 <[email protected]>
1 parent 1319e1e commit f0aee74

2 files changed

Lines changed: 62 additions & 15 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { describe, expect, it } from "vitest";
2+
import { resolveSessionStorePathCore, type SessionEntry } from "../../../config/sessions.js";
3+
import type { OpenClawConfig } from "../../../config/types.openclaw.js";
4+
import {
5+
resolveSubagentSessionCompletion,
6+
type SubagentSessionStoreCache,
7+
} from "./subagent-session-reconciliation.js";
8+
9+
const configuredStorePath = "/virtual/openclaw-subagent-reconciliation-sessions.json";
10+
const cfg = {
11+
session: { store: configuredStorePath },
12+
} satisfies OpenClawConfig;
13+
const storePath = resolveSessionStorePathCore(configuredStorePath, { agentId: "main" });
14+
15+
const terminalSession: SessionEntry = {
16+
sessionId: "sibling-session",
17+
status: "done",
18+
startedAt: 1_000,
19+
updatedAt: 2_000,
20+
endedAt: 2_000,
21+
};
22+
23+
function resolveCompletion(childSessionKey: string, storedSessionKey: string) {
24+
const storeCache: SubagentSessionStoreCache = new Map([
25+
[storePath, { [storedSessionKey]: terminalSession }],
26+
]);
27+
return resolveSubagentSessionCompletion({
28+
childSessionKey,
29+
fallbackEndedAt: 3_000,
30+
notBeforeMs: 0,
31+
storeCache,
32+
cfg,
33+
});
34+
}
35+
36+
describe("subagent session reconciliation keys", () => {
37+
it("matches case-insensitive structural session-key segments", () => {
38+
expect(
39+
resolveCompletion("Agent:MAIN:telegram:group:ROOM", "agent:main:telegram:group:room"),
40+
).toMatchObject({ endedAt: 2_000, outcome: { status: "ok" } });
41+
});
42+
43+
it.each([
44+
{
45+
channel: "Matrix",
46+
childSessionKey: "agent:main:matrix:group:!Room:server",
47+
storedSessionKey: "agent:main:matrix:group:!room:server",
48+
},
49+
{
50+
channel: "Signal",
51+
childSessionKey: "agent:main:signal:group:AbCdEf==",
52+
storedSessionKey: "agent:main:signal:group:abcdef==",
53+
},
54+
])(
55+
"does not match a case-distinct $channel opaque peer",
56+
({ childSessionKey, storedSessionKey }) => {
57+
expect(resolveCompletion(childSessionKey, storedSessionKey)).toBeNull();
58+
},
59+
);
60+
});

‎src/agents/subagents/registry/subagent-session-reconciliation.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
listSessionEntriesReadOnly,
1515
loadSessionEntryReadOnly,
1616
} from "../../../config/sessions/session-accessor.js";
17+
import { normalizeStoreSessionKey } from "../../../config/sessions/store-entry.js";
1718
import type { OpenClawConfig } from "../../../config/types.openclaw.js";
1819
import type { SubagentRunOutcome } from "../announce/subagent-announce-output.js";
1920
import {
@@ -69,20 +70,6 @@ function freshSessionStartedAt(
6970
return notBeforeMs === undefined || startedAt >= notBeforeMs ? startedAt : undefined;
7071
}
7172

72-
function findSessionEntryByKey(store: Record<string, SessionEntry>, sessionKey: string) {
73-
const direct = store[sessionKey];
74-
if (direct) {
75-
return direct;
76-
}
77-
const normalized = sessionKey.trim().toLowerCase();
78-
for (const [key, entry] of Object.entries(store)) {
79-
if (key.trim().toLowerCase() === normalized) {
80-
return entry;
81-
}
82-
}
83-
return undefined;
84-
}
85-
8673
/** Load a child session entry using the agent-specific session store path. */
8774
export function loadSubagentSessionEntry(params: {
8875
childSessionKey: string;
@@ -106,7 +93,7 @@ export function loadSubagentSessionEntry(params: {
10693
);
10794
params.storeCache?.set(storePath, store);
10895
}
109-
return findSessionEntryByKey(store, key);
96+
return store[key] ?? store[normalizeStoreSessionKey(key)];
11097
}
11198

11299
/** Resolve a child session entry without depending on the file-backed store shape. */

0 commit comments

Comments
 (0)