Skip to content

Lightpanda: the headless browser designed for AI and automation

agent: --trace-results writes each tool result's text to the trace

Adrià Arrufat596d05baf2c5
diff --git a/src/Config.zig b/src/Config.zig
index 227a044fb..966100946 100644
--- a/src/Config.zig
+++ b/src/Config.zig
@@ -470,6 +470,7 @@ const Commands = cli.Builder(.{
.{ .name = "system_prompt", .type = ?[:0]const u8 },
.{ .name = "task", .type = ?[]const u8 },
.{ .name = "trace", .type = ?[]const u8 },
+ .{ .name = "trace_results", .type = bool },
.{ .name = "save", .type = ?[]const u8 },
.{ .name = "attach", .short = 'a', .type = []const u8, .multiple = true },
.{ .name = "verbosity", .type = ?AgentVerbosity },
diff --git a/src/agent/Agent.zig b/src/agent/Agent.zig
index e669afcb0..fd11da445 100644
--- a/src/agent/Agent.zig
+++ b/src/agent/Agent.zig
@@ -246,6 +246,12 @@ pub fn init(allocator: std.mem.Allocator, app: *App, opts: Config.Agent) !*Agent
return error.InvalidFilename;
}
}
+ if (opts.trace_results and opts.trace == null) {
+ log.fatal(.app, "conflicting flags", .{
+ .hint = "--trace-results adds result text to the --trace file; pass --trace too",
+ });
+ return error.ConflictingFlags;
+ }
if (opts.no_llm and opts.provider != null) {
log.warn(.app, "ignoring --provider", .{ .reason = "--no-llm takes precedence" });
}
@@ -382,7 +388,7 @@ pub fn init(allocator: std.mem.Allocator, app: *App, opts: Config.Agent) !*Agent
errdefer if (self.ai_client) |c| c.deinit(allocator);
if (opts.trace) |path| {
- self.trace = Trace.create(allocator, path) catch |err| {
+ self.trace = Trace.create(allocator, path, opts.trace_results) catch |err| {
log.fatal(.app, "cannot write trace", .{ .path = path, .err = err });
return err;
};
diff --git a/src/agent/Trace.zig b/src/agent/Trace.zig
index 7966f47a1..b3130b6be 100644
--- a/src/agent/Trace.zig
+++ b/src/agent/Trace.zig
@@ -35,10 +35,11 @@ started_ms: u64,
model_started_ms: u64 = 0,
model_calls: u32 = 0,
tool_calls: u32 = 0,
+with_results: bool,
-pub fn create(allocator: std.mem.Allocator, path: []const u8) !Trace {
+pub fn create(allocator: std.mem.Allocator, path: []const u8, with_results: bool) !Trace {
const file = try std.Io.Dir.cwd().createFile(lp.io, path, .{ .truncate = true });
- return .{ .file = file, .line = .init(allocator), .started_ms = now() };
+ return .{ .file = file, .line = .init(allocator), .started_ms = now(), .with_results = with_results };
}
pub fn deinit(self: *Trace) void {
@@ -78,6 +79,7 @@ pub fn toolCall(self: *Trace, name: []const u8, arguments: ?std.json.Value, resu
.args = arguments,
.ms = ms,
.result_bytes = result.text.len,
+ .result = if (self.with_results) result.text else null,
.@"error" = result.is_error,
.navigated = result.navigated,
.page = if (frame) |f| lp.tools.pageState(f) else null,
@@ -137,7 +139,7 @@ test "Trace writes one JSON line per step, arguments as given" {
defer tmp.cleanup();
const path = try testing.tmpPath(&tmp, "trace.jsonl");
- var trace: Trace = try .create(std.testing.allocator, path);
+ var trace: Trace = try .create(std.testing.allocator, path, false);
trace.run("openai", "gpt-test", "find courts");
var response: zenai.provider.GenerateResult = .init(std.testing.allocator);
@@ -174,7 +176,24 @@ test "Trace writes one JSON line per step, arguments as given" {
const tool = records[2].value.object;
try std.testing.expectEqualStrings("$LP_PASSWORD", tool.get("args").?.object.get("value").?.string);
try std.testing.expectEqual(7, tool.get("result_bytes").?.integer);
+ try std.testing.expect(tool.get("result") == null);
try std.testing.expect(tool.get("page") == null);
try std.testing.expectEqual(1, records[3].value.object.get("model_calls").?.integer);
}
+
+test "Trace writes result text only when asked" {
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ const path = try testing.tmpPath(&tmp, "trace.jsonl");
+
+ var trace: Trace = try .create(std.testing.allocator, path, true);
+ trace.toolCall("markdown", null, &.{ .text = "# Title" }, 3, null);
+ trace.deinit();
+
+ const written = try std.Io.Dir.cwd().readFileAlloc(lp.io, path, std.testing.allocator, .limited(4096));
+ defer std.testing.allocator.free(written);
+ const record = try std.json.parseFromSlice(std.json.Value, std.testing.allocator, std.mem.trimEnd(u8, written, "\n"), .{});
+ defer record.deinit();
+ try std.testing.expectEqualStrings("# Title", record.value.object.get("result").?.string);
+}
diff --git a/src/help.zon b/src/help.zon
index 5fa765d8a..2a71f4026 100644
--- a/src/help.zon
+++ b/src/help.zon
@@ -316,8 +316,12 @@
\\ Write one JSON line per step to PATH: each model call (latency,
\\ input, cached and output tokens) and each tool call (arguments,
\\ latency, result size, page URL and HTTP status), then a summary.
- \\ Arguments keep their $LP_* placeholders; results are not written.
- \\ Overwrites PATH if it exists.
+ \\ Arguments keep their $LP_* placeholders; results are not written
+ \\ unless --trace-results is set. Overwrites PATH if it exists.
+ \\ --trace-results
+ \\ Also write each tool result's text to the --trace file, as the
+ \\ model received it. Pages can be large and may hold personal
+ \\ data, so this is off by default. Requires --trace.
\\ --url <URL>
\\ Open this page before the first turn. With --task it saves the
\\ model a turn spent navigating; with --save the opening