Skip to content

Commit 79722d2

Browse files
authored
fix(agents): prevent invalid session filters from listing every session (#129198)
* fix(agents): preserve explicit session kind filtering * test(agents): refresh session kind prompt snapshots
1 parent 8160c6e commit 79722d2

7 files changed

Lines changed: 67 additions & 33 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export {
3232
} from "./sessions-resolution.js";
3333

3434
/** Coarse session category used by session list/status tools. */
35-
type SessionKind = "main" | "group" | "cron" | "hook" | "node" | "other";
35+
export const SESSION_LIST_KINDS = ["main", "group", "cron", "hook", "node", "other"] as const;
36+
type SessionKind = (typeof SESSION_LIST_KINDS)[number];
3637

3738
const SESSION_KIND_BY_CLASSIFICATION: Readonly<Record<string, SessionKind>> = {
3839
main: "main",

‎src/agents/tools/sessions-list-tool.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,46 @@ describe("sessions-list-tool", () => {
182182
});
183183
});
184184

185+
it("limits session kind arguments to the documented classification values", () => {
186+
const tool = createSessionsListTool({ config: VALID_CONFIG });
187+
188+
expect(
189+
Value.Check(tool.parameters, { kinds: ["main", "group", "cron", "hook", "node", "other"] }),
190+
).toBe(true);
191+
expect(Value.Check(tool.parameters, { kinds: ["unknown"] })).toBe(false);
192+
expect(Value.Check(tool.parameters, { kinds: [" "] })).toBe(false);
193+
});
194+
195+
it.each([
196+
{ name: "unknown-only", kinds: ["unknown"], expected: [] },
197+
{ name: "whitespace-only", kinds: [" "], expected: [] },
198+
{ name: "unknown scalar", kinds: "unknown", expected: [] },
199+
{ name: "whitespace scalar", kinds: " ", expected: [] },
200+
{ name: "known scalar", kinds: "MAIN", expected: ["agent:main:main"] },
201+
{ name: "mixed known and unknown", kinds: ["unknown", "MAIN"], expected: ["agent:main:main"] },
202+
{
203+
name: "empty",
204+
kinds: [],
205+
expected: ["agent:main:main", "agent:main:slack:channel:team-room"],
206+
},
207+
])("never broadens an explicit $name session kind filter", async ({ kinds, expected }) => {
208+
mocks.gatewayCall.mockResolvedValue({
209+
sessions: [
210+
sessionRow("agent:main:main", "main"),
211+
sessionRow("agent:main:slack:channel:team-room", "channel"),
212+
sessionRow("agent:other:main", "main", "other"),
213+
],
214+
});
215+
216+
const result = await createSessionsListTool({ config: VALID_CONFIG }).execute("filter-kinds", {
217+
kinds,
218+
});
219+
220+
expect(getSessionsListDetails(result).sessions?.map((session) => session.key)).toEqual(
221+
expected,
222+
);
223+
});
224+
185225
it("lists unspawned same-agent sessions from the canonical main session under tree visibility", async () => {
186226
mocks.resolveEffectiveSessionToolsVisibility.mockReturnValue("tree");
187227
mocks.resolveSandboxedSessionToolContext.mockReturnValue({

‎src/agents/tools/sessions-list-tool.ts‎

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
*
44
* Lists visible sessions and optionally hydrates titles, last messages, and transcript-derived metadata.
55
*/
6-
import {
7-
normalizeOptionalLowercaseString,
8-
readStringValue,
9-
} from "@openclaw/normalization-core/string-coerce";
6+
import { readStringValue } from "@openclaw/normalization-core/string-coerce";
107
import pMap from "p-map";
118
import { Type } from "typebox";
129
import type { SessionRunStatus } from "../../../packages/gateway-protocol/src/schema/sessions-row.js";
@@ -21,6 +18,7 @@ import { resolveSessionAgentIds } from "../agent-scope.js";
2118
import {
2219
optionalNonNegativeIntegerSchema,
2320
optionalPositiveIntegerSchema,
21+
stringEnum,
2422
} from "../schema/typebox.js";
2523
import {
2624
describeSessionLinkRule,
@@ -51,12 +49,13 @@ import {
5149
resolveEffectiveSessionToolsVisibility,
5250
resolveInternalSessionKey,
5351
resolveSandboxedSessionToolContext,
52+
SESSION_LIST_KINDS,
5453
type GatewaySessionListRow,
5554
type SessionListRow,
5655
} from "./sessions-helpers.js";
5756

5857
const SessionsListToolSchema = Type.Object({
59-
kinds: Type.Optional(Type.Array(Type.String())),
58+
kinds: Type.Optional(Type.Array(stringEnum(SESSION_LIST_KINDS))),
6059
limit: optionalPositiveIntegerSchema(),
6160
activeMinutes: optionalPositiveIntegerSchema(),
6261
messageLimit: optionalNonNegativeIntegerSchema(),
@@ -73,14 +72,7 @@ const SessionListRowOutputSchema = Type.Object(
7372
key: Type.String(),
7473
sessionId: Type.Optional(Type.String()),
7574
agentId: Type.String(),
76-
kind: Type.Union([
77-
Type.Literal("main"),
78-
Type.Literal("group"),
79-
Type.Literal("cron"),
80-
Type.Literal("hook"),
81-
Type.Literal("node"),
82-
Type.Literal("other"),
83-
]),
75+
kind: stringEnum(SESSION_LIST_KINDS),
8476
channel: Type.String(),
8577
archived: Type.Boolean(),
8678
pinned: Type.Boolean(),
@@ -185,13 +177,13 @@ export function createSessionsListTool(opts?: {
185177
sandboxed: opts?.sandboxed === true,
186178
});
187179

188-
const kindsRaw = readStringArrayParam(params, "kinds")
189-
?.map((value) => normalizeOptionalLowercaseString(value))
190-
.filter((value): value is string => Boolean(value));
191-
const allowedKindsList = (kindsRaw ?? []).filter((value) =>
192-
["main", "group", "cron", "hook", "node", "other"].includes(value),
193-
);
194-
const allowedKinds = allowedKindsList.length ? new Set(allowedKindsList) : undefined;
180+
const kindsRaw = readStringArrayParam(params, "kinds")?.map((value) => value.toLowerCase());
181+
const requestedKinds = params.kinds;
182+
const allowedKinds =
183+
(Array.isArray(requestedKinds) || typeof requestedKinds === "string") &&
184+
requestedKinds.length > 0
185+
? new Set(kindsRaw)
186+
: undefined;
195187

196188
const limit = readPositiveIntegerParam(params, "limit");
197189
const activeMinutes = readPositiveIntegerParam(params, "activeMinutes");

‎test/fixtures/agents/prompt-snapshots/codex-runtime-happy-path/codex-dynamic-tools.telegram-direct.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,7 @@
11191119
},
11201120
"kinds": {
11211121
"items": {
1122+
"enum": ["main", "group", "cron", "hook", "node", "other"],
11221123
"type": "string"
11231124
},
11241125
"type": "array"

‎test/fixtures/agents/prompt-snapshots/codex-runtime-happy-path/discord-group-codex-message-tool.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ This is the deterministic model-bound layer stack OpenClaw can snapshot for the
231231
"roughTokens": 0
232232
},
233233
"dynamicToolsJson": {
234-
"chars": 54999,
235-
"roughTokens": 13750
234+
"chars": 55201,
235+
"roughTokens": 13801
236236
},
237237
"openClawDeveloperInstructions": {
238238
"chars": 4499,
@@ -243,8 +243,8 @@ This is the deterministic model-bound layer stack OpenClaw can snapshot for the
243243
"roughTokens": 7221
244244
},
245245
"totalWithDynamicToolsJson": {
246-
"chars": 83883,
247-
"roughTokens": 20971
246+
"chars": 84085,
247+
"roughTokens": 21022
248248
},
249249
"userInputText": {
250250
"chars": 1300,

‎test/fixtures/agents/prompt-snapshots/codex-runtime-happy-path/telegram-direct-codex-message-tool.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ This is the deterministic model-bound layer stack OpenClaw can snapshot for the
231231
"roughTokens": 0
232232
},
233233
"dynamicToolsJson": {
234-
"chars": 54691,
235-
"roughTokens": 13673
234+
"chars": 54893,
235+
"roughTokens": 13724
236236
},
237237
"openClawDeveloperInstructions": {
238238
"chars": 3390,
@@ -243,8 +243,8 @@ This is the deterministic model-bound layer stack OpenClaw can snapshot for the
243243
"roughTokens": 6851
244244
},
245245
"totalWithDynamicToolsJson": {
246-
"chars": 82095,
247-
"roughTokens": 20524
246+
"chars": 82297,
247+
"roughTokens": 20575
248248
},
249249
"userInputText": {
250250
"chars": 929,

‎test/fixtures/agents/prompt-snapshots/codex-runtime-happy-path/telegram-heartbeat-codex-tool.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ This is the deterministic model-bound layer stack OpenClaw can snapshot for the
226226
"roughTokens": 0
227227
},
228228
"dynamicToolsJson": {
229-
"chars": 56248,
230-
"roughTokens": 14062
229+
"chars": 56450,
230+
"roughTokens": 14113
231231
},
232232
"openClawDeveloperInstructions": {
233233
"chars": 3390,
@@ -238,8 +238,8 @@ This is the deterministic model-bound layer stack OpenClaw can snapshot for the
238238
"roughTokens": 6955
239239
},
240240
"totalWithDynamicToolsJson": {
241-
"chars": 84068,
242-
"roughTokens": 21017
241+
"chars": 84270,
242+
"roughTokens": 21068
243243
},
244244
"userInputText": {
245245
"chars": 1271,

0 commit comments

Comments
 (0)