Skip to content

Lightpanda: the headless browser designed for AI and automation

agent: tighten the backported fixes

Adrià Arrufat2f0d037a5358
diff --git a/src/NodeRegistry.zig b/src/NodeRegistry.zig
index 40f5a81fd..a9347e36b 100644
--- a/src/NodeRegistry.zig
+++ b/src/NodeRegistry.zig
@@ -205,6 +205,5 @@ test "NodeRegistry: reset never reuses an id" {
registry.reset();
try testing.expectEqual(null, registry.lookup_by_id.get(first_id));
- // A stale id stays a miss rather than resolving to whatever registers next.
try testing.expect((try registry.register(dom_node)).id != first_id);
}
diff --git a/src/SemanticTree.zig b/src/SemanticTree.zig
index 01bf01e6a..74794c1ff 100644
--- a/src/SemanticTree.zig
+++ b/src/SemanticTree.zig
@@ -69,7 +69,7 @@ pub fn init(arena: std.mem.Allocator, node: *Node, registry: *NodeRegistry, fram
/// Walk the pruned tree with `visitor`: `visit(*Node, *NodeData) !bool`
/// returns whether to descend into the children, `leave() !void` closes a
/// visited node.
-pub fn visitAll(self: @This(), visitor: anytype) error{WriteFailed}!void {
+fn visitAll(self: @This(), visitor: anytype) error{WriteFailed}!void {
var xpath_buffer: std.ArrayList(u8) = .empty;
const listener_targets = interactive.buildListenerTargetMap(self.frame, self.arena) catch |err| {
log.err(.app, "listener map failed", .{ .err = err });
diff --git a/src/agent/Agent.zig b/src/agent/Agent.zig
index eb13cebb9..33cfbc88e 100644
--- a/src/agent/Agent.zig
+++ b/src/agent/Agent.zig
@@ -159,8 +159,8 @@ model: []u8,
/// Per-turn reasoning budget for LLM turns. Mutable at runtime via `/effort`.
effort: Config.Effort,
script_file: ?[]const u8,
-/// `--url`: opened before the first turn, in every mode. A `--task` run that
-/// starts on its page does not spend a model turn navigating to it.
+/// `--url`: opened before the first turn, so a `--task` run does not spend a
+/// model turn navigating to its own start page.
start_url: ?[:0]const u8,
one_shot_task: ?[]const u8,
one_shot_save: ?[]const u8,
@@ -172,7 +172,6 @@ http_interrupt: zenai.http.Interrupt = .{},
synthetic_tool_call_id: u32 = 0,
/// Per-turn CSS selector for each tool call the model made, in call order, so
/// `--save` can record a call that addressed its element by `backendNodeId`.
-/// Only filled while `capturing_for_save`.
save_selectors: std.ArrayListUnmanaged(?[]const u8) = .empty,
capturing_for_save: bool = false,
/// Aggregate Anthropic/OpenAI/Gemini token usage across every model call.
@@ -293,9 +292,8 @@ pub fn init(allocator: std.mem.Allocator, app: *App, opts: Config.Agent) !*Agent
const verbosity = settings.resolveVerbosity(opts, remembered);
const stream_enabled = settings.resolveStream(remembered);
browser_tools.search_engine = opts.search_engine orelse settings.resolveSearchEngine(remembered);
- // Only the REPL's `/searchEngine` used to say this. A keyless engine that
- // has hit its cap fails every search, and without a word here that reads
- // as a bad agent rather than a missing key.
+ // A keyless engine over its cap fails every search; silence reads as a bad
+ // agent rather than a missing key.
if (browser_tools.searchKeyStatus(browser_tools.search_engine)) |key| switch (key.state) {
.set => {},
.keyless => log.info(.app, "keyless search endpoint", .{ .env_var = key.env_var, .limit = "rate-limited per client IP" }),
@@ -518,10 +516,10 @@ const TurnInput = struct {
/// Returns true on success.
pub fn run(self: *Agent) bool {
if (self.start_url) |url| {
- if (self.gotoStart(url, self.one_shot_save != null)) |err| {
+ self.gotoStart(url, self.one_shot_save != null) catch |err| {
self.terminal.printError("could not open {s}: {s}", .{ url, browser_tools.errorMessage(err) });
return false;
- }
+ };
}
if (self.one_shot_task) |task| {
const saving = self.one_shot_save != null;
@@ -545,27 +543,23 @@ pub fn run(self: *Agent) bool {
return true;
}
-/// Print single-line cumulative token usage to stderr, so wrappers driving
-/// `lightpanda agent --task ...` can capture per-task cost by `grep`-ing the
-/// `$usage` prefix. Stable key=value format:
-/// $usage prompt=N completion=N total=N cached=N cache_creation=N
-/// Fields emit 0 when the provider didn't report them.
-/// Goes through the tool layer so a failed start reads like any other tool
-/// failure.
-fn gotoStart(self: *Agent, url: [:0]const u8, record: bool) ?browser_tools.ToolError {
+fn gotoStart(self: *Agent, url: [:0]const u8, record: bool) browser_tools.ToolError!void {
var arena: std.heap.ArenaAllocator = .init(self.allocator);
defer arena.deinit();
const a = arena.allocator();
var object: std.json.ObjectMap = .empty;
- object.put(a, "url", .{ .string = url }) catch return browser_tools.ToolError.OutOfMemory;
+ try object.put(a, "url", .{ .string = url });
const args: std.json.Value = .{ .object = object };
- _ = browser_tools.call(a, self.ts.session, &self.ts.registry, "goto", args, .{}) catch |err| return err;
- // The opening navigation is the first line of any replayable script.
+ _ = try browser_tools.call(a, self.ts.session, &self.ts.registry, "goto", args, .{});
if (record) self.recordSaveCommand(Command.fromToolCall(.goto, args));
- return null;
}
+/// Print single-line cumulative token usage to stderr, so wrappers driving
+/// `lightpanda agent --task ...` can capture per-task cost by `grep`-ing the
+/// `$usage` prefix. Stable key=value format:
+/// $usage prompt=N completion=N total=N cached=N cache_creation=N
+/// Fields emit 0 when the provider didn't report them.
fn printUsageSummary(self: *Agent) void {
const u = self.total_usage;
std.debug.print(
@@ -1311,9 +1305,8 @@ fn synthesizeSaveTo(self: *Agent, arena: std.mem.Allocator, path: []const u8, mo
return;
}
- // `stripCodeFence` accepts a block with no closing fence, so a script cut
- // off mid-statement is indistinguishable from a complete one once it is on
- // disk. Refuse rather than save something that will not replay.
+ // `stripCodeFence` accepts an unclosed block, so a truncated script is
+ // indistinguishable from a complete one once it is on disk.
if (result.finish_reason == .max_tokens) {
return self.abortSave(baseline, "the model ran out of output tokens mid-script");
}
@@ -1376,8 +1369,7 @@ fn logSaveBufferError(self: *Agent, err: anyerror) void {
}
/// Swap a call's ephemeral `backendNodeId` for the selector the tool layer
-/// resolved, so the call can be replayed. Returns `args` untouched when there
-/// is nothing to swap.
+/// resolved, so the call can be replayed.
fn withSelector(arena: std.mem.Allocator, args: ?std.json.Value, selector: ?[]const u8) ?std.json.Value {
const sel = selector orelse return args;
const original = args orelse return args;
@@ -1797,9 +1789,6 @@ fn processUserMessage(self: *Agent, input: TurnInput) !?[]const u8 {
const args = browser_tools.normalizeArgKeys(ca, tool, tc.arguments) catch tc.arguments;
// Fall back to the navigation a read tool performed, so a
// markdown/tree-driven turn isn't lost from `/save`.
- // A call that named its element by id is unreplayable as-is; the
- // tool layer resolved a selector for it while the node still
- // existed.
const replayable = withSelector(ca, args, if (i < self.save_selectors.items.len) self.save_selectors.items[i] else null);
const cmd = Command.fromToolCall(tool, replayable);
const to_record = if (cmd.isRecorded())
@@ -1965,8 +1954,10 @@ fn handleToolCall(ctx: *anyopaque, allocator: std.mem.Allocator, tool_name: []co
};
if (self.capturing_for_save) {
// One entry per call, errors included, so the index lines up with
- // `RunToolsResult.tool_calls_made`.
- const kept = if (selector) |sel| self.allocator.dupe(u8, sel) catch null else null;
+ // `RunToolsResult.tool_calls_made`. The conversation arena outlives the
+ // turn that reads them; `allocator` here is zenai's per-call arena.
+ const ca = self.conversation.arena.allocator();
+ const kept = if (selector) |sel| ca.dupe(u8, sel) catch null else null;
self.save_selectors.append(self.allocator, kept) catch {};
}
diff --git a/src/browser/tools.zig b/src/browser/tools.zig
index 5b597dfdc..9ea7f983f 100644
--- a/src/browser/tools.zig
+++ b/src/browser/tools.zig
@@ -825,9 +825,8 @@ pub const ToolResult = struct {
is_error: bool = false,
/// Only set when the caller passed `CallOpts.inline_image`.
image: ?lp.screenshot.Prepared = null,
- /// Only set when the caller passed `CallOpts.record` and the call named its
- /// element by `backendNodeId`. Resolved before the action runs, because a
- /// navigation takes the node with it.
+ /// Resolved before the action runs, because a navigation takes the node
+ /// with it.
selector: ?[]const u8 = null,
};
@@ -860,10 +859,8 @@ const NodeAndPage = struct { node: *DOMNode, page: *lp.Frame, target: ActionTarg
pub const CallOpts = struct {
/// The caller can hand an image to a model.
inline_image: bool = false,
- /// The caller is recording for `--save`/`/save`. A call that addresses its
- /// element by `backendNodeId` gets `ToolResult.selector` filled in, since
- /// a registry id means nothing in a later session and the node may be gone
- /// by the time the caller wants to record it.
+ /// Fill in `ToolResult.selector`: a registry id means nothing in a later
+ /// session, so `--save` cannot replay a call that used one.
record: bool = false,
};
@@ -897,8 +894,14 @@ pub fn call(
};
const substituted = try substituteStringArgs(arena, tool, normalized);
- // Before dispatch: after a navigation the node is gone.
- const selector = if (opts.record) selectorForArgs(arena, session, registry, substituted) else null;
+ // Before dispatch, because a navigation takes the node with it. Gated on
+ // `isRecorded` because `SelectorPath.build` is the expensive part of a tool
+ // call and the read-only tools that take a `backendNodeId` -- tree,
+ // markdown, html, nodeDetails -- would only have it thrown away.
+ const selector = if (opts.record and tool.isRecorded())
+ selectorForArgs(arena, session, registry, substituted)
+ else
+ null;
var result = dispatch(arena, session, registry, tool, substituted, opts) catch |err| {
if (err == error.NavigationFailed) {
@@ -912,8 +915,7 @@ pub fn call(
}
/// The CSS selector for a call's `backendNodeId`, so the call can be recorded
-/// in a form that still resolves in a later session. Null when the arguments
-/// name no node, already carry a selector, or the node cannot be named.
+/// in a form that still resolves in a later session.
fn selectorForArgs(
arena: std.mem.Allocator,
session: *lp.Session,
@@ -1168,13 +1170,24 @@ const KeyStatus = struct {
state: enum { set, keyless, missing },
};
-/// `null` for `.auto`, which has no key of its own.
+fn keyStatusOf(comptime e: anytype) KeyStatus {
+ return .{
+ .env_var = e.env_var,
+ .state = if (engineKey(e)) |key| (if (key != null) .set else .keyless) else |_| .missing,
+ };
+}
+
+/// For `.auto`, the rung the cascade would actually land on -- it has no key of
+/// its own, but "which engine is about to serve, and on what terms" is the
+/// question worth answering, and `.auto` is the default.
pub fn searchKeyStatus(engine: SearchEngine) ?KeyStatus {
inline for (api_engines) |e| {
- if (engine == e.tag) return .{
- .env_var = e.env_var,
- .state = if (engineKey(e)) |key| (if (key != null) .set else .keyless) else |_| .missing,
- };
+ if (engine == e.tag) return keyStatusOf(e);
+ }
+ if (engine != .auto) return null;
+ inline for (api_engines) |e| {
+ const status = keyStatusOf(e);
+ if (status.state != .missing) return status;
}
return null;
}
@@ -1204,8 +1217,7 @@ fn execSearch(arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError!To
}
} else |_| {}
}
- // The last rung's reason, not a generic one: every engine having
- // failed is usually one cause, and the model can act on it.
+ // The last engine's reason, not a generic one -- the model can act on it.
return searchFailed(arena, last_label, last_err.?, last_detail);
},
inline else => |tag| {
@@ -1230,34 +1242,33 @@ fn searchExplicit(arena: std.mem.Allocator, comptime engine: anytype, timeout_ms
return .{ .text = markdown_ };
}
-/// What the provider said, duped out of the client before `deinit` takes it.
-/// Without this the status and body survive only in a log line, and every
-/// failure reaches the model as the word `InternalError`.
+/// Duped out of the client before `deinit` takes it; otherwise the model sees
+/// only the error name.
const Failure = struct {
status: ?u10 = null,
message: []const u8 = "",
};
-/// A rate limit and a bad key need different reactions, so the model is told
-/// which it hit rather than just that the search failed.
fn searchFailed(arena: std.mem.Allocator, label: []const u8, err: anyerror, detail: Failure) ToolError!ToolResult {
var aw: std.Io.Writer.Allocating = .init(arena);
- const w = &aw.writer;
- w.print("{s} search failed: {s}", .{ label, @errorName(err) }) catch return ToolError.OutOfMemory;
- if (detail.status) |status| w.print(" (HTTP {d})", .{status}) catch return ToolError.OutOfMemory;
+ writeFailure(&aw.writer, label, err, detail) catch return ToolError.OutOfMemory;
+ return .{ .text = aw.written(), .is_error = true };
+}
+
+fn writeFailure(w: *std.Io.Writer, label: []const u8, err: anyerror, detail: Failure) !void {
+ try w.print("{s} search failed: {s}", .{ label, @errorName(err) });
+ if (detail.status) |status| try w.print(" (HTTP {d})", .{status});
if (detail.message.len > 0) {
- w.writeAll(": ") catch return ToolError.OutOfMemory;
- writeSingleLine(w, detail.message) catch return ToolError.OutOfMemory;
+ try w.writeAll(": ");
+ try writeSingleLine(w, detail.message);
}
+ // The one failure where the right move is not "try another query".
if (detail.status == 429) {
- w.writeAll(". This engine is rate-limited right now; wait before retrying, or read the answer from a page instead.") catch
- return ToolError.OutOfMemory;
+ try w.writeAll(". This engine is rate-limited right now; wait before retrying, or read the answer from a page instead.");
}
- return .{ .text = aw.written(), .is_error = true };
}
-/// `arena` owns the returned slice. `detail` is filled on a non-2xx so the
-/// caller can say what actually happened.
+/// `arena` owns the returned slice.
fn apiSearch(
comptime engine: anytype,
arena: std.mem.Allocator,
@@ -1285,7 +1296,6 @@ fn apiSearch(
.status = status,
.body = client.last_error.body,
});
- // `client.last_error` dies with the client on the deferred deinit.
detail.* = .{
.status = status,
.message = if (client.last_error.body) |b| (arena.dupe(u8, b) catch "") else "",
@@ -1310,17 +1320,21 @@ pub const SearchResults = struct {
hits: []const Hit = &.{},
};
+/// The engines agree on title and url and disagree only on which field holds
+/// the snippet.
+fn collectHits(arena: std.mem.Allocator, results: anytype, comptime snippet: []const u8) ![]Hit {
+ const hits = try arena.alloc(Hit, results.len);
+ for (results, hits) |r, *hit| hit.* = .{ .title = r.title, .url = r.url, .snippet = @field(r, snippet) };
+ return hits;
+}
+
fn collectTavily(arena: std.mem.Allocator, resp: tavily.types.SearchResponse) !SearchResults {
- const hits = try arena.alloc(Hit, resp.results.len);
- for (resp.results, hits) |r, *hit| hit.* = .{ .title = r.title, .url = r.url, .snippet = r.content };
- return .{ .answer = resp.answer orelse "", .hits = hits };
+ return .{ .answer = resp.answer orelse "", .hits = try collectHits(arena, resp.results, "content") };
}
fn collectBrave(arena: std.mem.Allocator, resp: brave.types.SearchResponse) !SearchResults {
const results: []const brave.types.Result = if (resp.web) |web| web.results else &.{};
- const hits = try arena.alloc(Hit, results.len);
- for (results, hits) |r, *hit| hit.* = .{ .title = r.title, .url = r.url, .snippet = r.description };
- return .{ .hits = hits };
+ return .{ .hits = try collectHits(arena, results, "description") };
}
fn collectExa(arena: std.mem.Allocator, resp: exa.types.SearchResponse) !SearchResults {
@@ -1337,22 +1351,19 @@ fn collectExa(arena: std.mem.Allocator, resp: exa.types.SearchResponse) !SearchR
}
fn collectKeenable(arena: std.mem.Allocator, resp: keenable.types.SearchResponse) !SearchResults {
- const hits = try arena.alloc(Hit, resp.results.len);
- // snippet carries the page text (the wire format's always-empty
- // `description` is deliberately not even mapped by the client).
- for (resp.results, hits) |r, *hit| hit.* = .{ .title = r.title, .url = r.url, .snippet = r.snippet };
- return .{ .hits = hits };
+ // `snippet` carries the page text; the wire format's always-empty
+ // `description` is deliberately not even mapped by the client.
+ return .{ .hits = try collectHits(arena, resp.results, "snippet") };
}
fn renderResults(arena: std.mem.Allocator, results: SearchResults) ToolError![]const u8 {
if (results.answer.len == 0 and results.hits.len == 0) return "No results.";
var aw: std.Io.Writer.Allocating = .init(arena);
- const w = &aw.writer;
- renderInto(w, results) catch return ToolError.OutOfMemory;
+ writeResults(&aw.writer, results) catch return ToolError.OutOfMemory;
return aw.written();
}
-fn renderInto(w: *std.Io.Writer, results: SearchResults) !void {
+fn writeResults(w: *std.Io.Writer, results: SearchResults) !void {
if (results.answer.len > 0) try w.print("**Answer:** {s}\n\n", .{results.answer});
for (results.hits, 0..) |hit, i| try writeResultItem(w, i, hit.title, hit.url, hit.snippet);
}
@@ -2828,7 +2839,7 @@ test "formatLpEnvNames reports empty when no names" {
try std.testing.expectEqualStrings("No LP_* environment variables are set.", r);
}
-test "collectTavily renders answer and results" {
+test "tavily results render as markdown" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const aa = arena.allocator();
@@ -2850,7 +2861,7 @@ test "collectTavily renders answer and results" {
try std.testing.expect(std.mem.indexOf(u8, md, "2. **France**") != null);
}
-test "collectTavily handles empty results" {
+test "tavily: no results render as a notice" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const aa = arena.allocator();
@@ -2858,7 +2869,7 @@ test "collectTavily handles empty results" {
try std.testing.expectEqualStrings("No results.", try renderResults(aa, try collectTavily(aa, .{})));
}
-test "collectBrave renders web results" {
+test "brave results render as markdown" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const aa = arena.allocator();
@@ -2879,7 +2890,7 @@ test "collectBrave renders web results" {
try std.testing.expect(std.mem.indexOf(u8, md, "2. **France**") != null);
}
-test "collectBrave handles empty results" {
+test "brave: no results render as a notice" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const aa = arena.allocator();
@@ -2889,7 +2900,7 @@ test "collectBrave handles empty results" {
try std.testing.expectEqualStrings("No results.", try renderResults(aa, try collectBrave(aa, .{ .web = .{} })));
}
-test "collectKeenable reads snippet" {
+test "keenable results render the snippet as the body" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const aa = arena.allocator();
@@ -2917,14 +2928,14 @@ test "writeResultItem uses the URL as title when the title is empty" {
try std.testing.expectEqualStrings("1. **https://example.org/x.pdf** — https://example.org/x.pdf\n snippet\n\n", aw.written());
}
-test "collectKeenable handles empty results" {
+test "keenable: no results render as a notice" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const aa = arena.allocator();
try std.testing.expectEqualStrings("No results.", try renderResults(aa, try collectKeenable(aa, .{})));
}
-test "collectBrave flattens newlines in titles and descriptions" {
+test "brave titles and descriptions render on one line" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const aa = arena.allocator();
@@ -2953,12 +2964,9 @@ test "searchFailed: a rate limit says so, a bare failure stays short" {
.message = "Public API hourly limit reached.\nWait 2 minutes to continue.",
});
try std.testing.expect(limited.is_error);
- // The status and the provider's own words, which previously reached the
- // model only as the word "ApiError".
try std.testing.expect(std.mem.indexOf(u8, limited.text, "(HTTP 429)") != null);
try std.testing.expect(std.mem.indexOf(u8, limited.text, "Public API hourly limit reached.") != null);
try std.testing.expect(std.mem.indexOf(u8, limited.text, "rate-limited right now") != null);
- // Flattened: a newline would break the numbered-list markdown around it.
try std.testing.expect(std.mem.indexOf(u8, limited.text, "\n") == null);
const bare = try searchFailed(aa, "web", error.ConnectionRefused, .{});