Skip to content

Commit dc9b0ae

Browse files
committed
fix(agents): preserve session classification
1 parent 14b0011 commit dc9b0ae

7 files changed

Lines changed: 158 additions & 48 deletions

‎src/agents/openclaw-tools.sessions.test.ts‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -435,17 +435,20 @@ describe("sessions tools", () => {
435435
path: "/tmp/sessions.json",
436436
sessions: [
437437
{
438-
key: "main",
438+
key: "agent:main:main",
439439
kind: "direct",
440+
classification: "main",
440441
sessionId: "s-main",
441442
updatedAt: 10,
442443
lastChannel: "whatsapp",
443444
derivedTitle: "Main mailbox",
444445
lastMessagePreview: "Latest assistant update",
445446
},
446447
{
447-
key: "discord:group:dev",
448+
key: "agent:main:discord:group:dev",
448449
kind: "group",
450+
classification: "group",
451+
peerKind: "group",
449452
sessionId: "s-group",
450453
updatedAt: 11,
451454
channel: "discord",
@@ -461,25 +464,28 @@ describe("sessions tools", () => {
461464
{
462465
key: "agent:main:dashboard:child",
463466
kind: "direct",
467+
classification: "dashboard",
464468
sessionId: "s-dashboard-child",
465469
updatedAt: 12,
466470
parentSessionKey: "agent:main:main",
467471
},
468472
{
469473
key: "agent:main:subagent:worker",
470474
kind: "direct",
475+
classification: "subagent",
471476
sessionId: "s-subagent-worker",
472477
updatedAt: 13,
473478
spawnedBy: "agent:main:main",
474479
},
475480
{
476-
key: "cron:job-1",
481+
key: "agent:main:cron:job-1",
477482
kind: "direct",
483+
classification: "cron",
478484
sessionId: "s-cron",
479485
updatedAt: 9,
480486
},
481-
{ key: "global", kind: "global" },
482-
{ key: "unknown", kind: "unknown" },
487+
{ key: "global", kind: "global", classification: "global", agentId: "main" },
488+
{ key: "unknown", kind: "unknown", classification: "unknown", agentId: "main" },
483489
],
484490
};
485491
}
@@ -537,15 +543,15 @@ describe("sessions tools", () => {
537543
}>;
538544
};
539545
expect(details.sessions).toHaveLength(5);
540-
const main = details.sessions?.find((s) => s.key === "main");
546+
const main = details.sessions?.find((s) => s.key === "agent:main:main");
541547
expect(main?.agentId).toBe("main");
542548
expect(main?.channel).toBe("whatsapp");
543549
expect(main?.derivedTitle).toBe("Main mailbox");
544550
expect(main?.lastMessagePreview).toBe("Latest assistant update");
545551
expect(main?.messages?.length).toBe(1);
546552
expect(main?.messages?.[0]?.role).toBe("assistant");
547553

548-
const group = details.sessions?.find((s) => s.key === "discord:group:dev");
554+
const group = details.sessions?.find((s) => s.key === "agent:main:discord:group:dev");
549555
expect(group?.status).toBe("running");
550556
expect(group?.childSessions).toEqual(["agent:main:subagent:worker"]);
551557
expect(group?.derivedTitle).toBe("Dev room");
@@ -605,12 +611,14 @@ describe("sessions tools", () => {
605611
{
606612
key: "agent:main:main",
607613
kind: "direct",
614+
classification: "main",
608615
sessionId: "visible",
609616
updatedAt: 20,
610617
},
611618
{
612619
key: "agent:other:main",
613620
kind: "direct",
621+
classification: "main",
614622
sessionId: "hidden",
615623
updatedAt: 21,
616624
},
@@ -641,7 +649,7 @@ describe("sessions tools", () => {
641649
key: "agent:main:main",
642650
sessionId: "visible",
643651
agentId: "main",
644-
kind: "other",
652+
kind: "main",
645653
channel: "unknown",
646654
archived: false,
647655
pinned: false,
@@ -664,8 +672,9 @@ describe("sessions tools", () => {
664672
path: "(multiple)",
665673
sessions: [
666674
{
667-
key: "main",
675+
key: "agent:main:main",
668676
kind: "direct",
677+
classification: "main",
669678
sessionId: "sess-main",
670679
updatedAt: 12,
671680
},
@@ -681,7 +690,7 @@ describe("sessions tools", () => {
681690
const details = result.details as {
682691
sessions?: Array<Record<string, unknown>>;
683692
};
684-
const main = details.sessions?.find((session) => session.key === "main");
693+
const main = details.sessions?.find((session) => session.key === "agent:main:main");
685694
expect(main).not.toHaveProperty("transcriptPath");
686695
expect(main).toHaveProperty("sessionId", "sess-main");
687696
});

‎src/agents/tools/sessions-helpers.ts‎

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export {
2121
shouldResolveSessionIdInput,
2222
} from "./sessions-resolution.js";
2323
import { normalizeOptionalString, type FastMode } from "@openclaw/normalization-core/string-coerce";
24-
import type { SessionRunStatus } from "../../../packages/gateway-protocol/src/schema/sessions-row.js";
24+
import type {
25+
SessionRow,
26+
SessionRunStatus,
27+
} from "../../../packages/gateway-protocol/src/schema/sessions-row.js";
2528
import { getRuntimeConfig } from "../../config/config.js";
2629
import type { OpenClawConfig } from "../../config/types.openclaw.js";
2730
import { parseRawSessionConversationRef } from "../../sessions/session-key-utils.js";
@@ -30,6 +33,16 @@ import type { FastModeSource } from "../../shared/fast-mode.js";
3033
/** Coarse session category used by session list/status tools. */
3134
type SessionKind = "main" | "group" | "cron" | "hook" | "node" | "other";
3235

36+
const SESSION_KIND_BY_CLASSIFICATION: Readonly<Record<string, SessionKind>> = {
37+
main: "main",
38+
global: "main",
39+
group: "group",
40+
channel: "group",
41+
cron: "cron",
42+
hook: "hook",
43+
node: "node",
44+
};
45+
3346
/** Delivery target metadata attached to session rows. */
3447
type SessionListDeliveryContext = {
3548
channel?: string;
@@ -42,8 +55,10 @@ type SessionListDeliveryContext = {
4255
export type GatewaySessionListRow = {
4356
key: string;
4457
agentId?: string;
45-
kind: SessionKind;
46-
channel: string;
58+
classification: NonNullable<SessionRow["classification"]>;
59+
peerKind?: SessionRow["peerKind"];
60+
kind: SessionRow["kind"];
61+
channel?: string;
4762
origin?: {
4863
provider?: string;
4964
accountId?: string;
@@ -133,34 +148,15 @@ export function resolveSessionToolContext(opts?: {
133148
};
134149
}
135150

136-
/** Classifies a session key/gateway kind into the row category used by tools. */
151+
/** Projects the Gateway's authoritative classification into the tool's coarse categories. */
137152
export function classifySessionListKind(params: {
138-
key: string;
139-
gatewayKind?: string | null;
140-
alias: string;
141-
mainKey: string;
153+
classification: NonNullable<GatewaySessionListRow["classification"]>;
154+
peerKind?: GatewaySessionListRow["peerKind"];
142155
}): SessionKind {
143-
const key = params.key;
144-
if (key === params.alias || key === params.mainKey) {
145-
return "main";
146-
}
147-
if (key.startsWith("cron:")) {
148-
return "cron";
149-
}
150-
if (key.startsWith("hook:")) {
151-
return "hook";
152-
}
153-
if (key.startsWith("node-") || key.startsWith("node:")) {
154-
return "node";
155-
}
156-
if (params.gatewayKind === "group") {
157-
return "group";
158-
}
159-
if (key.includes(":group:") || key.includes(":channel:")) {
160-
// Gateway-less archived rows still encode group/channel shape in the session key.
161-
return "group";
156+
if (params.classification === "thread") {
157+
return params.peerKind === "group" || params.peerKind === "channel" ? "group" : "other";
162158
}
163-
return "other";
159+
return SESSION_KIND_BY_CLASSIFICATION[params.classification] ?? "other";
164160
}
165161

166162
/** Derives the best channel label for a session row. */

0 commit comments

Comments
 (0)