Repository navigation
fix(approvals): bypass approval for background job control #1817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
95ff02f
test(approvals): reproduce check_background_job approval wedge
Aaronontheweb 04e6b17
fix(approvals): gate only check_background_job cancel, not status que…
Aaronontheweb c03f52a
test(approvals): pipeline-level regression for check_background_job s…
Aaronontheweb 9db6d9e
fix(approvals): align cancel detection with tool binding + TestKit te…
Aaronontheweb d5fd59a
fix(approvals): manage background jobs without second approval
Aaronontheweb 6ce0edc
fix(approvals): bypass approval for background job control
Aaronontheweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/Netclaw.Actors/Jobs/BackgroundJobApprovalMatcher.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <copyright file="BackgroundJobApprovalMatcher.cs" company="Petabridge, LLC"> | ||
| // Copyright (C) 2026 - 2026 Petabridge, LLC <https://petabridge.com> | ||
| // </copyright> | ||
| // ----------------------------------------------------------------------- | ||
| using System.Text.Json; | ||
| using Netclaw.Configuration; | ||
| using Netclaw.Security; | ||
| using Netclaw.Tools; | ||
|
|
||
| namespace Netclaw.Actors.Jobs; | ||
|
|
||
| /// <summary> | ||
| /// Approval matcher for <see cref="CheckBackgroundJobTool"/>. | ||
| /// | ||
| /// <para> | ||
| /// A status query (<c>Cancel=false</c>) is read-only: it asks the | ||
| /// <c>BackgroundJobManagerActor</c> for the caller's own job state and the | ||
| /// manager enforces an exact <c>SessionId + Audience + Boundary</c> match | ||
| /// before answering, so no cross-session data can leak. Status queries must | ||
| /// NOT be fail-closed on the Personal audience — otherwise a non-interactive | ||
| /// turn (reminder, webhook, sub-agent without an approval bridge) wedges | ||
| /// waiting on an approval prompt no one can answer, which stalls the very | ||
| /// background-job workflow the tool exists to serve. | ||
| /// </para> | ||
| /// | ||
| /// <para> | ||
| /// A cancellation (<c>Cancel=true</c>) IS a mutation and stays fail-closed: | ||
| /// it kills a running process, so it keeps requiring interactive approval | ||
| /// exactly like the shell command that launched the job. | ||
| /// </para> | ||
| /// | ||
| /// <para> | ||
| /// Launching a background job is unaffected by this matcher. A background | ||
| /// launch is a <c>shell_execute</c> call with <c>_background: true</c>, which | ||
| /// routes through <see cref="ShellApprovalMatcher"/> and the ordinary shell | ||
| /// approval gate. This matcher only governs the query/cancel follow-up tool. | ||
| /// </para> | ||
| /// </summary> | ||
| public sealed class BackgroundJobApprovalMatcher : IToolApprovalMatcher | ||
| { | ||
| public static readonly BackgroundJobApprovalMatcher Instance = new(); | ||
|
|
||
| /// <summary> | ||
| /// Argument key carrying the cancellation flag on <c>check_background_job</c>. | ||
| /// </summary> | ||
| private const string CancelArgumentKey = "Cancel"; | ||
|
|
||
| public string GetApprovalModeKey(ToolName toolName, IDictionary<string, object?>? arguments) | ||
| => toolName.Value; | ||
|
|
||
| /// <summary> | ||
| /// Only a cancellation request fails closed. A status query is read-only | ||
| /// and job-scoped, so it must not force interactive approval on Personal. | ||
| /// A missing or unparsable <c>Cancel</c> value counts as a status query — | ||
| /// fail toward the read-only path, never toward an approval prompt for a | ||
| /// query. The tool's own argument binding rejects invalid values before | ||
| /// execution, so this cannot mask a real cancellation. | ||
| /// </summary> | ||
| public bool IsFailClosedOnPersonal(ToolName toolName, IDictionary<string, object?>? arguments) | ||
| => IsCancelRequest(arguments); | ||
|
|
||
| public IReadOnlyList<string> ExtractPatterns(ToolName toolName, IDictionary<string, object?>? arguments) | ||
| => [toolName.Value]; | ||
|
|
||
| public IReadOnlyList<string> ExtractCandidateVerbs(ToolName toolName, IDictionary<string, object?>? arguments) | ||
| => [toolName.Value]; | ||
|
|
||
| public IReadOnlyList<ApprovalCandidate> ExtractCandidates(ToolName toolName, IDictionary<string, object?>? arguments) | ||
| => [new ApprovalCandidate(toolName.Value, Directory: null)]; | ||
|
|
||
| public bool IsApproved( | ||
| ToolName toolName, | ||
| IDictionary<string, object?>? arguments, | ||
| IReadOnlyList<ApprovalEntry> approvedEntries, | ||
| string? cwd) | ||
| => ApprovalPatternMatching.MatchesAny(toolName.Value, approvedEntries); | ||
|
|
||
| public bool IsMessy(ToolName toolName, IDictionary<string, object?>? arguments) | ||
| => false; | ||
|
|
||
| public string FormatForDisplay(ToolName toolName, IDictionary<string, object?>? arguments) | ||
| => toolName.Value; | ||
|
|
||
| private static bool IsCancelRequest(IDictionary<string, object?>? arguments) | ||
| { | ||
| if (arguments is null) | ||
| return false; | ||
|
|
||
| if (!arguments.TryGetValue(CancelArgumentKey, out var value) || value is null) | ||
| return false; | ||
|
|
||
| return value switch | ||
| { | ||
| bool b => b, | ||
| JsonElement { ValueKind: JsonValueKind.True } => true, | ||
| JsonElement { ValueKind: JsonValueKind.False } => false, | ||
| string s => bool.TryParse(s, out var parsed) && parsed, | ||
| _ => false | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.