Skip to content

Commit 6a12c6f

Browse files
authored
fix(gateway): scope session data lookups by agent [AI] (#81386)
* fix: scope gateway session lookups by agent * addressing review-skill * addressing review-skill * addressing review-skill * addressing review-skill * addressing codex review * addressing codex review * addressing codex review * addressing codex review * addressing codex review * addressing review-skill * addressing review-skill * addressing review-skill * addressing review-skill * addressing codex review * addressing codex review * addressing codex review * addressing codex review * addressing codex review * addressing codex review * addressing codex review * addressing ci * addressing ci * fix: complete root-cause handling * addressing review-skill * addressing codex review * addressing codex review * addressing codex review * addressing codex review * addressing codex review * Fix Swift protocol optional initializer defaults * Stabilize node command lookup in approval test * Fix browser proxy approval test node lookup * Trim unrelated changes from issue 642 fix * Remove unrelated formatting churn from issue 642 fix * Fix Swift protocol generator lint * docs: add changelog entry for PR merge
1 parent 9a20400 commit 6a12c6f

23 files changed

Lines changed: 2099 additions & 127 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Docs: https://docs.openclaw.ai
1818

1919
### Fixes
2020

21+
- fix(gateway): scope session data lookups by agent [AI]. (#81386) Thanks @pgondhi987.
2122
- Agents/media: accept generated media attachments on internal completion events and report delivery-loss failures as errors, so completed background music/video tasks do not disappear after provider success.
2223
- Matrix/approvals: release in-flight reaction bindings when the channel approval handler stops mid-delivery, preventing stale approval targets after restart. Fixes #82485. (#82482) Thanks @Feelw00.
2324
- Matrix/E2EE: stop requesting MSC4222 `state_after` sync responses so homeservers with incomplete state-after data do not leave fresh encrypted rooms without outbound room encryptors. Fixes #82515. Thanks @nickdecooman.

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,18 +2109,22 @@ public struct SessionsMessagesUnsubscribeParams: Codable, Sendable {
21092109
public struct SessionsAbortParams: Codable, Sendable {
21102110
public let key: String?
21112111
public let runid: String?
2112+
public let agentid: String?
21122113

21132114
public init(
21142115
key: String?,
2115-
runid: String?)
2116+
runid: String?,
2117+
agentid: String? = nil)
21162118
{
21172119
self.key = key
21182120
self.runid = runid
2121+
self.agentid = agentid
21192122
}
21202123

21212124
private enum CodingKeys: String, CodingKey {
21222125
case key
21232126
case runid = "runId"
2127+
case agentid = "agentId"
21242128
}
21252129
}
21262130

@@ -2338,6 +2342,7 @@ public struct SessionsCompactParams: Codable, Sendable {
23382342

23392343
public struct SessionsUsageParams: Codable, Sendable {
23402344
public let key: String?
2345+
public let agentid: String?
23412346
public let startdate: String?
23422347
public let enddate: String?
23432348
public let mode: AnyCodable?
@@ -2350,6 +2355,7 @@ public struct SessionsUsageParams: Codable, Sendable {
23502355

23512356
public init(
23522357
key: String?,
2358+
agentid: String? = nil,
23532359
startdate: String?,
23542360
enddate: String?,
23552361
mode: AnyCodable?,
@@ -2361,6 +2367,7 @@ public struct SessionsUsageParams: Codable, Sendable {
23612367
includecontextweight: Bool?)
23622368
{
23632369
self.key = key
2370+
self.agentid = agentid
23642371
self.startdate = startdate
23652372
self.enddate = enddate
23662373
self.mode = mode
@@ -2374,6 +2381,7 @@ public struct SessionsUsageParams: Codable, Sendable {
23742381

23752382
private enum CodingKeys: String, CodingKey {
23762383
case key
2384+
case agentid = "agentId"
23772385
case startdate = "startDate"
23782386
case enddate = "endDate"
23792387
case mode
@@ -4274,21 +4282,25 @@ public struct ArtifactsListParams: Codable, Sendable {
42744282
public let sessionkey: String?
42754283
public let runid: String?
42764284
public let taskid: String?
4285+
public let agentid: String?
42774286

42784287
public init(
42794288
sessionkey: String?,
42804289
runid: String?,
4281-
taskid: String?)
4290+
taskid: String?,
4291+
agentid: String? = nil)
42824292
{
42834293
self.sessionkey = sessionkey
42844294
self.runid = runid
42854295
self.taskid = taskid
4296+
self.agentid = agentid
42864297
}
42874298

42884299
private enum CodingKeys: String, CodingKey {
42894300
case sessionkey = "sessionKey"
42904301
case runid = "runId"
42914302
case taskid = "taskId"
4303+
case agentid = "agentId"
42924304
}
42934305
}
42944306

@@ -4310,24 +4322,28 @@ public struct ArtifactsGetParams: Codable, Sendable {
43104322
public let sessionkey: String?
43114323
public let runid: String?
43124324
public let taskid: String?
4325+
public let agentid: String?
43134326
public let artifactid: String
43144327

43154328
public init(
43164329
sessionkey: String?,
43174330
runid: String?,
43184331
taskid: String?,
4332+
agentid: String? = nil,
43194333
artifactid: String)
43204334
{
43214335
self.sessionkey = sessionkey
43224336
self.runid = runid
43234337
self.taskid = taskid
4338+
self.agentid = agentid
43244339
self.artifactid = artifactid
43254340
}
43264341

43274342
private enum CodingKeys: String, CodingKey {
43284343
case sessionkey = "sessionKey"
43294344
case runid = "runId"
43304345
case taskid = "taskId"
4346+
case agentid = "agentId"
43314347
case artifactid = "artifactId"
43324348
}
43334349
}
@@ -4350,24 +4366,28 @@ public struct ArtifactsDownloadParams: Codable, Sendable {
43504366
public let sessionkey: String?
43514367
public let runid: String?
43524368
public let taskid: String?
4369+
public let agentid: String?
43534370
public let artifactid: String
43544371

43554372
public init(
43564373
sessionkey: String?,
43574374
runid: String?,
43584375
taskid: String?,
4376+
agentid: String? = nil,
43594377
artifactid: String)
43604378
{
43614379
self.sessionkey = sessionkey
43624380
self.runid = runid
43634381
self.taskid = taskid
4382+
self.agentid = agentid
43644383
self.artifactid = artifactid
43654384
}
43664385

43674386
private enum CodingKeys: String, CodingKey {
43684387
case sessionkey = "sessionKey"
43694388
case runid = "runId"
43704389
case taskid = "taskId"
4390+
case agentid = "agentId"
43714391
case artifactid = "artifactId"
43724392
}
43734393
}

‎extensions/memory-core/src/session-search-visibility.test.ts‎

Lines changed: 160 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ import { afterEach, describe, expect, it, vi } from "vitest";
44
import { filterMemorySearchHitsBySessionVisibility } from "./session-search-visibility.js";
55
import { asOpenClawConfig } from "./tools.test-helpers.js";
66

7-
const crossAgentStore = {
7+
type TestSessionEntry = {
8+
sessionId: string;
9+
updatedAt: number;
10+
sessionFile: string;
11+
};
12+
13+
const crossAgentStore: Record<string, TestSessionEntry> = {
814
"agent:peer:only": {
915
sessionId: "w1",
1016
updatedAt: 1,
1117
sessionFile: "/tmp/sessions/w1.jsonl",
1218
},
1319
};
14-
let combinedSessionStore: typeof crossAgentStore | Record<string, never> = crossAgentStore;
20+
let combinedSessionStore: Record<string, TestSessionEntry> = crossAgentStore;
1521

1622
vi.mock("openclaw/plugin-sdk/session-transcript-hit", async (importOriginal) => {
1723
const actual =
@@ -100,10 +106,19 @@ describe("filterMemorySearchHitsBySessionVisibility", () => {
100106
hits,
101107
});
102108
expect(sessionTranscriptHit.loadCombinedSessionStoreForGateway).toHaveBeenCalledTimes(1);
103-
expect(sessionTranscriptHit.loadCombinedSessionStoreForGateway).toHaveBeenCalledWith(cfg);
109+
expect(sessionTranscriptHit.loadCombinedSessionStoreForGateway).toHaveBeenCalledWith(cfg, {
110+
agentId: "main",
111+
});
104112
});
105113

106-
it("allows cross-agent session hits when visibility=all and agent-to-agent is enabled", async () => {
114+
it("keeps same-agent session hits when visibility=all and agent-to-agent is enabled", async () => {
115+
combinedSessionStore = {
116+
"agent:main:only": {
117+
sessionId: "w1",
118+
updatedAt: 1,
119+
sessionFile: "/tmp/sessions/w1.jsonl",
120+
},
121+
};
107122
const hit: MemorySearchResult = {
108123
path: "sessions/w1.jsonl",
109124
source: "sessions",
@@ -127,6 +142,120 @@ describe("filterMemorySearchHitsBySessionVisibility", () => {
127142
expect(filtered).toEqual([hit]);
128143
});
129144

145+
it("keeps global-scope session hits for non-default agents", async () => {
146+
combinedSessionStore = {
147+
global: {
148+
sessionId: "w1",
149+
updatedAt: 1,
150+
sessionFile: "/tmp/sessions/w1.jsonl",
151+
},
152+
};
153+
const hit: MemorySearchResult = {
154+
path: "sessions/w1.jsonl",
155+
source: "sessions",
156+
score: 1,
157+
snippet: "x",
158+
startLine: 1,
159+
endLine: 2,
160+
};
161+
const cfg = asOpenClawConfig({
162+
session: { scope: "global" },
163+
tools: {
164+
sessions: { visibility: "all" },
165+
agentToAgent: { enabled: true, allow: ["*"] },
166+
},
167+
});
168+
const filtered = await filterMemorySearchHitsBySessionVisibility({
169+
cfg,
170+
agentId: "secondary",
171+
requesterSessionKey: "agent:secondary:main",
172+
sandboxed: false,
173+
hits: [hit],
174+
});
175+
expect(filtered).toEqual([hit]);
176+
});
177+
178+
it("does not keep cross-agent session hits outside the scoped store", async () => {
179+
combinedSessionStore = {};
180+
const hit: MemorySearchResult = {
181+
path: "sessions/w1.jsonl",
182+
source: "sessions",
183+
score: 1,
184+
snippet: "x",
185+
startLine: 1,
186+
endLine: 2,
187+
};
188+
const cfg = asOpenClawConfig({
189+
tools: {
190+
sessions: { visibility: "all" },
191+
agentToAgent: { enabled: true, allow: ["*"] },
192+
},
193+
});
194+
const filtered = await filterMemorySearchHitsBySessionVisibility({
195+
cfg,
196+
requesterSessionKey: "agent:main:main",
197+
sandboxed: false,
198+
hits: [hit],
199+
});
200+
expect(filtered).toStrictEqual([]);
201+
});
202+
203+
it("does not keep cross-agent session hits when a shared store returns out-of-scope keys", async () => {
204+
combinedSessionStore = crossAgentStore;
205+
const hit: MemorySearchResult = {
206+
path: "sessions/w1.jsonl",
207+
source: "sessions",
208+
score: 1,
209+
snippet: "x",
210+
startLine: 1,
211+
endLine: 2,
212+
};
213+
const cfg = asOpenClawConfig({
214+
tools: {
215+
sessions: { visibility: "all" },
216+
agentToAgent: { enabled: true, allow: ["*"] },
217+
},
218+
});
219+
const filtered = await filterMemorySearchHitsBySessionVisibility({
220+
cfg,
221+
requesterSessionKey: "agent:main:main",
222+
sandboxed: false,
223+
hits: [hit],
224+
});
225+
expect(filtered).toStrictEqual([]);
226+
});
227+
228+
it("does not keep owner-qualified cross-agent hits that collide with a scoped stem", async () => {
229+
combinedSessionStore = {
230+
"agent:main:main": {
231+
sessionId: "main",
232+
updatedAt: 1,
233+
sessionFile: "/tmp/sessions/main.jsonl",
234+
},
235+
};
236+
const hit: MemorySearchResult = {
237+
path: "sessions/peer/main.jsonl",
238+
source: "sessions",
239+
score: 1,
240+
snippet: "x",
241+
startLine: 1,
242+
endLine: 2,
243+
};
244+
const cfg = asOpenClawConfig({
245+
tools: {
246+
sessions: { visibility: "all" },
247+
agentToAgent: { enabled: true, allow: ["*"] },
248+
},
249+
});
250+
const filtered = await filterMemorySearchHitsBySessionVisibility({
251+
cfg,
252+
requesterSessionKey: "agent:main:main",
253+
sandboxed: false,
254+
hits: [hit],
255+
});
256+
expect(filtered).toStrictEqual([]);
257+
});
258+
130259
it("denies cross-agent session hits when agent-to-agent is disabled", async () => {
131260
const hit: MemorySearchResult = {
132261
path: "sessions/w1.jsonl",
@@ -203,4 +332,31 @@ describe("filterMemorySearchHitsBySessionVisibility", () => {
203332

204333
expect(filtered).toStrictEqual([]);
205334
});
335+
336+
it("does not keep cross-agent deleted archive hits outside the scoped store when a2a is allowed", async () => {
337+
combinedSessionStore = {};
338+
const hit: MemorySearchResult = {
339+
path: "sessions/peer/deleted-stem.jsonl.deleted.2026-02-16T22-27-33.000Z",
340+
source: "sessions",
341+
score: 1,
342+
snippet: "x",
343+
startLine: 1,
344+
endLine: 2,
345+
};
346+
const cfg = asOpenClawConfig({
347+
tools: {
348+
sessions: { visibility: "all" },
349+
agentToAgent: { enabled: true, allow: ["*"] },
350+
},
351+
});
352+
353+
const filtered = await filterMemorySearchHitsBySessionVisibility({
354+
cfg,
355+
requesterSessionKey: "agent:main:main",
356+
sandboxed: false,
357+
hits: [hit],
358+
});
359+
360+
expect(filtered).toStrictEqual([]);
361+
});
206362
});

0 commit comments

Comments
 (0)