summaryrefslogtreecommitdiff
path: root/src/Agent.Core/Tools/ToolPolicy.cs
blob: c9f216a51a7709061969dc41f70c988a71a41d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System.Text.Json;

namespace Agent.Core.Tools;

public sealed record ToolPolicyDecision(bool Allowed, string Reason)
{
    public static ToolPolicyDecision Allow() => new(true, "allowed");
    public static ToolPolicyDecision Deny(string reason) => new(false, reason);
}

public interface IToolPolicyEngine
{
    ToolPolicyDecision Evaluate(string toolName, JsonElement arguments);
}

public interface IInternalToolBackend
{
    Task BeginTaskAsync(Guid taskId, CancellationToken cancellationToken) => Task.CompletedTask;
    Task<ToolExecutionResult> ExecuteAsync(Guid taskId, string toolName, JsonElement arguments, CancellationToken cancellationToken);
    Task EndTaskAsync(Guid taskId, bool success, CancellationToken cancellationToken) => Task.CompletedTask;
}

public sealed record ToolExecutionResult(bool Success, string Output, bool ExternalEffect, string? FailureFingerprint = null);

public sealed class DenyByDefaultPolicy(IReadOnlySet<string>? allowedTools = null) : IToolPolicyEngine
{
    private readonly IReadOnlySet<string> _allowedTools = allowedTools ?? new HashSet<string>(StringComparer.Ordinal);
    public ToolPolicyDecision Evaluate(string toolName, JsonElement arguments) => _allowedTools.Contains(toolName)
        ? ToolPolicyDecision.Allow()
        : ToolPolicyDecision.Deny("Capability is not allowed by policy.");
}