agent: centralize session and node registry resets

This commit is contained in:
Adrià Arrufat
2026-05-05 08:16:01 +02:00
parent a00161f54c
commit 80075efd6b
3 changed files with 15 additions and 4 deletions

View File

@@ -878,6 +878,12 @@ fn attemptSelfHeal(self: *Self, arena: std.mem.Allocator, failed_command: []cons
return null;
}
/// Tear down the browser session and start fresh. Used by the MCP `task` tool
/// when the caller asks for an isolated run.
pub fn resetSession(self: *Self) !void {
return self.tool_executor.resetSession();
}
/// MCP entry point: run a single user task with a clean LLM context. Browser
/// state (URL, cookies, etc.) is preserved by default; pass a fresh session
/// upstream if isolation is needed. Returns the assistant text on success
@@ -892,7 +898,7 @@ pub fn runOneTask(
_ = self.message_arena.reset(.retain_capacity);
// Each task gets a fresh LLM context; drop registry entries that point
// into the old session so a stray backendNodeId can't survive a navigation.
self.tool_executor.node_registry.reset();
self.tool_executor.resetNodeRegistry();
self.one_shot_attachments = attachments;
return self.processUserMessage(task, null);
}

View File

@@ -111,7 +111,7 @@ pub fn handleToolCall(self: *Self, arena: std.mem.Allocator, req: protocol.Reque
}
if (args.fresh orelse false) {
self.agent.tool_executor.resetSession() catch |err| {
self.agent.resetSession() catch |err| {
log.err(.mcp, "fresh session reset failed", .{ .err = err });
return self.sendErrorResult(id, "Failed to start a fresh browser session");
};

View File

@@ -49,12 +49,17 @@ pub fn deinit(self: *Self) void {
}
/// Tear down the current `Browser` and `Session` and replace them with
/// fresh ones. Caller is responsible for clearing any registry/cache
/// state that depended on the old session.
/// fresh ones. Also clears the node registry, since backendNodeIds from
/// the old session would dangle into the new one.
pub fn resetSession(self: *Self) !void {
self.browser.deinit();
try self.browser.init(self.app, .{}, null);
self.session = try self.browser.newSession(self.notification);
self.node_registry.reset();
}
pub fn resetNodeRegistry(self: *Self) void {
self.node_registry.reset();
}
pub const CallError = browser_tools.ToolError || error{InvalidJsonArguments};