Skip to content

Commit 8f24392

Browse files
bitfroeststeipete
andauthored
fix(browser): preserve agent identity for screenshot vision (#121941)
Fixes #121919. Preserve screenshot session ownership through the focused agent-scope SDK before media runtime resolves provider credentials. Co-authored-by: Peter Steinberger <[email protected]>
1 parent 1901dda commit 8f24392

5 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎extensions/browser/index.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ describe("browser plugin", () => {
261261
await tool.execute("call-1", { action: "status" });
262262
expect(runtimeApiMocks.createBrowserTool).toHaveBeenCalledWith({
263263
agentSessionKey: "agent:main:webchat:direct:123",
264+
agentId: "main",
264265
agentDir: "/tmp/agent",
265266
workspaceDir: "/tmp/workspace",
266267
activeModel: { provider: "openai", model: "gpt-5.5" },

‎extensions/browser/plugin-registration.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function createLazyBrowserTool(
7777
sandboxBridgeUrl?: string;
7878
allowHostControl?: boolean;
7979
agentSessionKey?: string;
80+
agentId?: string;
8081
agentDir?: string;
8182
workspaceDir?: string;
8283
activeModel?: {
@@ -138,6 +139,7 @@ function createBrowserToolOptions(ctx: OpenClawPluginToolContext): {
138139
sandboxBridgeUrl?: string;
139140
allowHostControl?: boolean;
140141
agentSessionKey?: string;
142+
agentId?: string;
141143
agentDir?: string;
142144
workspaceDir?: string;
143145
activeModel?: {
@@ -159,6 +161,7 @@ function createBrowserToolOptions(ctx: OpenClawPluginToolContext): {
159161
? { allowHostControl: ctx.browser.allowHostControl }
160162
: {}),
161163
...(ctx.sessionKey ? { agentSessionKey: ctx.sessionKey } : {}),
164+
...(ctx.agentId ? { agentId: ctx.agentId } : {}),
162165
...(ctx.agentDir ? { agentDir: ctx.agentDir } : {}),
163166
...(ctx.workspaceDir ? { workspaceDir: ctx.workspaceDir } : {}),
164167
...(ctx.activeModel?.provider || ctx.activeModel?.modelId

‎extensions/browser/src/browser-tool.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ export function createBrowserTool(opts?: {
364364
sandboxBridgeUrl?: string;
365365
allowHostControl?: boolean;
366366
agentSessionKey?: string;
367+
agentId?: string;
367368
agentDir?: string;
368369
workspaceDir?: string;
369370
activeModel?: {
@@ -835,6 +836,7 @@ export function createBrowserTool(opts?: {
835836
cfg: screenshotCfg,
836837
filePath: screenshotPath,
837838
agentDir: opts?.agentDir,
839+
agentId: opts?.agentId,
838840
workspaceDir: opts?.workspaceDir,
839841
activeModel: opts?.activeModel,
840842
mediaScope: opts?.mediaScope,

‎extensions/browser/src/browser/vision.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,48 @@ describe("describeBrowserScreenshot", () => {
9292
});
9393
});
9494

95+
it.each([
96+
{ name: "session-owned default agent", agentId: undefined, sessionAgentId: "main" },
97+
{ name: "explicit matching worker agent", agentId: "worker", sessionAgentId: "worker" },
98+
])(
99+
"passes the $name identity to image understanding when its directory is absent",
100+
async (testCase) => {
101+
const describeEntry = vi.fn().mockResolvedValue({ text: "A dashboard." });
102+
103+
await describeBrowserScreenshot(
104+
{
105+
cfg: {},
106+
filePath: "/tmp/screenshot.png",
107+
...(testCase.agentId ? { agentId: testCase.agentId } : {}),
108+
mediaScope: { sessionKey: `agent:${testCase.sessionAgentId}:webchat:direct:123` },
109+
},
110+
makeDeps(describeEntry),
111+
);
112+
113+
expect(describeEntry).toHaveBeenCalledWith(
114+
expect.objectContaining({ agentId: testCase.sessionAgentId, agentDir: undefined }),
115+
);
116+
},
117+
);
118+
119+
it("rejects an agent identity that conflicts with its session before image understanding", async () => {
120+
const describeEntry = vi.fn().mockResolvedValue({ text: "A dashboard." });
121+
122+
await expect(
123+
describeBrowserScreenshot(
124+
{
125+
cfg: {},
126+
filePath: "/tmp/screenshot.png",
127+
agentId: "worker",
128+
mediaScope: { sessionKey: "agent:main:webchat:direct:123" },
129+
},
130+
makeDeps(describeEntry),
131+
),
132+
).rejects.toThrow(/belongs to "main", not "worker"/);
133+
134+
expect(describeEntry).not.toHaveBeenCalled();
135+
});
136+
95137
it("resizes screenshots before image understanding when image sanitization is configured", async () => {
96138
const describeResult = vi.fn().mockResolvedValue({ text: "Small screenshot." });
97139
const normalizeBrowserScreenshot = vi.fn(async () => ({

‎extensions/browser/src/browser/vision.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,18 @@ export async function describeBrowserScreenshot(
9090
deps: BrowserScreenshotDescriptionDeps,
9191
): Promise<BrowserScreenshotDescriptionResult | null> {
9292
const filePath = await resolveImageUnderstandingFilePath(ctx, deps);
93+
const agentId = ctx.agentDir
94+
? undefined
95+
: (await import("openclaw/plugin-sdk/agent-scope-runtime")).resolveSessionAgentId({
96+
agentId: ctx.agentId,
97+
sessionKey: ctx.mediaScope?.sessionKey,
98+
config: ctx.cfg,
99+
});
93100
const described = await deps.describeImageFile({
94101
filePath,
95102
cfg: ctx.cfg,
96103
prompt: DEFAULT_BROWSER_SCREENSHOT_DESCRIPTION_PROMPT,
104+
...(agentId ? { agentId } : {}),
97105
agentDir: ctx.agentDir,
98106
workspaceDir: ctx.workspaceDir,
99107
activeModel: normalizeActiveModel(ctx.activeModel),

0 commit comments

Comments
 (0)