Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/tests/package-size-budgets.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"bytes": 2475000
},
"fx-term.wasm": {
"bytes": 4254000
"bytes": 4270000
},
"engine.js": {
"gzip": 34600
Expand Down
89 changes: 89 additions & 0 deletions src/core/agent/worker_runtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ pub const StateSnapshot = struct {
}
};

/// The file review a pending request shows. `review` is an identity, not a
/// lifetime: the tool call that built it frees it as soon as the request is
/// answered, and the answer can come from another thread, so only
/// `copyPendingReview`, which copies under the worker's lock, may read it.
pub const PendingPermissionReview = struct {
request_id: u64,
review: *const diff_mod.FileReview,
Expand Down Expand Up @@ -2785,6 +2789,26 @@ pub const WorkerRuntime = struct {
};
}

/// Returns a copy of the file review the pending request `request_id` waits
/// on, which the caller owns and frees with `OwnedReview.deinit`, or null
/// when no request with that id still waits for a decision or it has no
/// review. The copy is made under `worker_mutex`: the tool call that owns
/// the review waits on this worker, so it cannot end and free the review
/// while the lock is held (`tests/tla/subagent/TuiParentApproval.tla`
/// NoStalePrompt).
pub fn copyPendingReview(
self: *WorkerRuntime,
alloc: std.mem.Allocator,
request_id: u64,
) std.mem.Allocator.Error!?diff_mod.OwnedReview {
self.worker_mutex.lockUncancelable(io_mod.getIo());
defer self.worker_mutex.unlock(io_mod.getIo());
if (!self.permissionRequestAwaitingDecisionLocked()) return null;
if (self.pending_permission_request_shared.?.id != request_id) return null;
const review = self.pending_permission_review orelse return null;
return try diff_mod.OwnedReview.init(alloc, review);
}

pub fn activeTurnId(self: *WorkerRuntime) u64 {
self.worker_mutex.lockUncancelable(io_mod.getIo());
defer self.worker_mutex.unlock(io_mod.getIo());
Expand Down Expand Up @@ -7761,6 +7785,71 @@ test "permission snapshot keeps the full file review borrowed and request-bound"
try std.testing.expectEqual(@as(usize, 7), snapshot.pending_permission_review.?.review.rowCount());
}

test "worker copies a pending review under its lock" {
const alloc = std.testing.allocator;
var runtime = WorkerRuntime{};
defer runtime.deinit(alloc);
var review = try diff_mod.FileReview.init(alloc, "one\ntwo\n", "one\nTWO\n");
var review_freed = false;
defer if (!review_freed) review.deinit(alloc);

runtime.worker_processing = true;
runtime.pending_permission_waiting = true;
runtime.pending_permission_request_shared = try permission_request.OwnedPermissionRequest.dupe(
alloc,
.{
.id = 44,
.label = "file_mutation",
.file = .{
.kind = .write,
.intent = .mutation,
.preview = .{
.path = "note.txt",
.lines = &.{
.{ .op = .deletion, .old_line = 2, .text = "two" },
.{ .op = .addition, .new_line = 2, .text = "TWO" },
},
.additions = 1,
.deletions = 1,
.truncated = false,
},
.scope = .workspace_files,
},
},
);
runtime.pending_permission_review = &review;

// A request with another id, waiting or not, has no copy to give.
try std.testing.expect(try runtime.copyPendingReview(alloc, 45) == null);

var copied = (try runtime.copyPendingReview(alloc, 44)).?;
defer copied.deinit(alloc);
try std.testing.expect(copied.view() != &review);
const rows = review.rowCount();
try std.testing.expectEqual(rows, copied.view().rowCount());
const before = try review.viewport(alloc, 0, rows);
defer alloc.free(before.lines);
const first_row = try alloc.dupe(u8, before.lines[0].text);
defer alloc.free(first_row);

// The copy keeps the rows after the tool call that built the review frees it.
review.deinit(alloc);
review_freed = true;
const after = try copied.view().viewport(alloc, 0, rows);
defer alloc.free(after.lines);
try std.testing.expectEqualStrings(first_row, after.lines[0].text);

// Once the request is answered, it waits on nothing, so it has no copy.
try std.testing.expectEqual(
PermissionSubmissionResult.accepted,
runtime.submitPermissionResponse(
44,
permission_request.OwnedPermissionResponse.init(alloc, .once, null),
),
);
try std.testing.expect(try runtime.copyPendingReview(alloc, 44) == null);
}

test "held turn propagation refreshes queued prompt trusted reviewer context" {
const alloc = std.testing.allocator;
var runtime = WorkerRuntime{};
Expand Down
12 changes: 9 additions & 3 deletions src/core/app/app_agent_runtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,15 @@ pub fn Runtime(comptime App: type) type {
.disabled;
// A child's `subagent` calls go to its own host, which the tool
// context already holds, as main's go to main's.
// A child's approvals and questions go to its own worker, as
// main's do, for main's runs and the user's alike; main's screen
// borrows them while the child is hidden.
// A run main started asks through its turn, so the request is
// registered and main is told; it still waits on this child's own
// worker, whose screen shows it too. A turn the person typed has no
// turn of main's, so it keeps the worker's prompter and stays with
// the person (`tests/tla/subagent/TuiParentApproval.tla`
// TypedStaysWithPerson).
if (run.turn) |turn| {
if (run.startedByParent()) ctx.permission_prompter = turn.permissionPrompter();
}
ctx.interactive = false;
ctx.ask_user_questions = true;
ctx.provider = run.admission.provider;
Expand Down
31 changes: 28 additions & 3 deletions src/core/app/app_agents_runtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ pub fn Runtime(comptime App: type) type {
for (app.child_agents.items) |child| {
if (!lendsTo(app, child, borrower)) continue;
const approval = &child.agent.approval_prompt;
if (approval.isActive() and approval.rule_management == null) {
if (approval.isActive() and approval.rule_management == null and !parentRoutedApproval(child)) {
if (oldest == null or child.approval_asked < oldest.?.asked) oldest = .{ .child = child, .kind = .approval, .asked = child.approval_asked };
}
if (child.agent.question_prompt.isActive()) {
Expand Down Expand Up @@ -1267,8 +1267,10 @@ pub fn Runtime(comptime App: type) type {
/// Lends `child`'s approval to `borrower`'s prompt. Its request comes
/// from the subagent's own worker and stays labeled with its name.
/// It goes back when the subagent is shown, the instance gets its
/// own, or the subagent stops waiting (`keepLentApproval`).
/// own, or the subagent stops waiting (`keepLentApproval`). A request
/// main answers is never lent: main's screen does not show it.
fn lendApproval(app: *App, borrower: *AppAgent, child: *ChildAgent) void {
if (parentRoutedApproval(child)) return;
std.mem.swap(@TypeOf(child.agent.approval_prompt), &borrower.approval_prompt, &child.agent.approval_prompt);
app.approval_lender = child;
debug_trace.eventf("host", "approval_lent", .{}, "agent={s}", .{child.id});
Expand Down Expand Up @@ -1591,7 +1593,9 @@ pub fn Runtime(comptime App: type) type {
}
for (app.child_agents.items) |child| {
if (child == app.agent_on_screen) continue;
const approval = child.agent.approval_prompt.isActive();
// A request main answers is not a wait for the person: the
// screen shows no prompt for it and the footer stays quiet.
const approval = child.agent.approval_prompt.isActive() and !parentRoutedApproval(child);
const asks = child.agent.question_prompt.isActive();
if (!approval and !asks and !requestWaitsForUser(app, child)) continue;
if (!found) {
Expand Down Expand Up @@ -2401,11 +2405,29 @@ pub fn Runtime(comptime App: type) type {
return null;
}

/// The person answered a subagent's request on its own screen, so the
/// registry binding goes with it. An agent that is not a hosted child
/// does nothing.
pub fn withdrawAnsweredApproval(app: *App, agent: *AppAgent) void {
const child = hostedOf(app, agent) orelse return;
const host = hostOf(app, child) orelse return;
_ = host.withdrawAnsweredApproval(child.id);
}

fn hostedOf(app: *App, agent: *const AppAgent) ?*ChildAgent {
for (app.child_agents.items) |child| if (&child.agent == agent) return child;
return null;
}

/// Whether `child`'s waiting approval came from a run main started. Main
/// answers those, so no instance's screen lends one and the footer and
/// the agent list never say the child needs the person
/// (`tests/tla/subagent/TuiParentApproval.tla` NoPersonPromptForParentRouted).
fn parentRoutedApproval(child: *const ChildAgent) bool {
const run = if (child.agent.child_run) |*value| value else return false;
return run.turn != null and run.startedByParent();
}

/// A subagent's approval or question is lent to an instance's screen
/// only when that instance's tree holds it: an instance's are its
/// own, and a subagent of another instance belongs to that one.
Expand Down Expand Up @@ -2606,6 +2628,9 @@ pub fn Runtime(comptime App: type) type {
if (target) |child| {
if (app.approval_lender == child) return .needs_approval;
if (app.question_lender == child) return .has_question;
// A request main answers is main's work, not a wait for the
// person, so the list says working, not "needs approval".
if (parentRoutedApproval(child) and child.agent.approval_prompt.isActive()) return .working;
}
const last_end: LastEnd = if (target) |child| (if (child.agent.role == .subagent) child.last_end else .done) else .done;
const state = instanceState(targetAgent(app, target), requestWaitsForUser(app, target), last_end);
Expand Down
12 changes: 6 additions & 6 deletions src/core/app/app_render_runtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6104,7 +6104,7 @@ test "core.app_render_runtime file approval returns to the preserved inline skil
try std.testing.expect(!app.terminal.catalogMenuScreenActive());

try std.testing.expect(try app.agent().approval_prompt.syncRequest(alloc, request));
try std.testing.expect(app.agent().approval_prompt.syncReview(&review));
try std.testing.expect(app.agent().approval_prompt.adoptReview(alloc, app.agent().approval_prompt.request.?.id, &review, try diff_mod.OwnedReview.init(alloc, &review)));
app.shell.render_requests.request(.modal);
try Runtime(CoordinatorTestApp).flushRequestedFrame(&app, (&app).agent());
try Runtime(CoordinatorTestApp).flushRequestedFrame(&app, (&app).agent());
Expand Down Expand Up @@ -6197,7 +6197,7 @@ test "core.app_render_runtime file approvals commit the alternate screen for eac
alloc,
request_a,
));
try std.testing.expect(app.agent().approval_prompt.syncReview(&review));
try std.testing.expect(app.agent().approval_prompt.adoptReview(alloc, app.agent().approval_prompt.request.?.id, &review, try diff_mod.OwnedReview.init(alloc, &review)));

app.shell.render_requests.request(.first_frame);
try Runtime(CoordinatorTestApp).flushRequestedFrame(&app, (&app).agent());
Expand Down Expand Up @@ -6233,7 +6233,7 @@ test "core.app_render_runtime file approvals commit the alternate screen for eac
alloc,
request_b,
));
try std.testing.expect(app.agent().approval_prompt.syncReview(&review));
try std.testing.expect(app.agent().approval_prompt.adoptReview(alloc, app.agent().approval_prompt.request.?.id, &review, try diff_mod.OwnedReview.init(alloc, &review)));
app.shell.render_requests.request(.modal);
try std.testing.expect(!approval_screen.fileApprovalAffirmativeReady(
&app.shell,
Expand Down Expand Up @@ -6325,7 +6325,7 @@ test "core.app_render_runtime restores file review before rendering a generic ap
try app.shell.initBacking(alloc);
try app.shell.enableShadowVt(alloc);
try std.testing.expect(try app.agent().approval_prompt.syncRequest(alloc, file_request));
try std.testing.expect(app.agent().approval_prompt.syncReview(&review));
try std.testing.expect(app.agent().approval_prompt.adoptReview(alloc, app.agent().approval_prompt.request.?.id, &review, try diff_mod.OwnedReview.init(alloc, &review)));

app.shell.render_requests.request(.first_frame);
try Runtime(CoordinatorTestApp).flushRequestedFrame(&app, (&app).agent());
Expand Down Expand Up @@ -6543,7 +6543,7 @@ test "core.app_render_runtime file approval takes over the full transcript alter
try app.shell.writeTranscript(alloc, &app.metrics, "prior transcript\n", true);
try app_lifecycle.openFullTranscript(app.alloc, &app.terminal, &app.shell, &app.metrics);
try std.testing.expect(try app.agent().approval_prompt.syncRequest(alloc, request));
try std.testing.expect(app.agent().approval_prompt.syncReview(&review));
try std.testing.expect(app.agent().approval_prompt.adoptReview(alloc, app.agent().approval_prompt.request.?.id, &review, try diff_mod.OwnedReview.init(alloc, &review)));

app.shell.render_requests.request(.modal);
try Runtime(CoordinatorTestApp).flushRequestedFrame(&app, (&app).agent());
Expand Down Expand Up @@ -6667,7 +6667,7 @@ test "core.app_render_runtime lifecycle rewrite recovers normal buffer after fil
var review = try diff_mod.FileReview.init(alloc, "", "pending\n");
defer review.deinit(alloc);
try std.testing.expect(try app.agent().approval_prompt.syncRequest(alloc, request));
try std.testing.expect(app.agent().approval_prompt.syncReview(&review));
try std.testing.expect(app.agent().approval_prompt.adoptReview(alloc, app.agent().approval_prompt.request.?.id, &review, try diff_mod.OwnedReview.init(alloc, &review)));
app.shell.render_requests.request(.modal);
try Runtime(CoordinatorTestApp).flushRequestedFrame(&app, (&app).agent());
try std.testing.expect(app.terminal.fileApprovalScreenActive());
Expand Down
Loading
Loading