Grossaufgaben und Sonderbudget (#717, Stufe 2+3)
Eine Aufgabe, deren eigene Schaetzung (gemessen, Geschwister oder Autor) ueber der Haelfte der Tageskostengrenze liegt, bekommt zuerst einen Planungslauf: teilen (Teile unter der Elternaufgabe) oder Sonderbudget beantragen. Danach wartet sie auf ihre Teile bzw. die Freigabe und laeuft einmal zum Zusammenfuehren. Sonderbudget ueber Escalation.request_budget, ausnahmslos freigabepflichtig (auch gegen Werkzeugkonfiguration und Dauerfreigabe). Laeufe im Baum ziehen den Topf statt der Tagesgrenzen, tragen BudgetPot in Verbrauch und Beleg und sind in der Kostenanalyse (nach Budget) und auf der Aufgabenkarte gesondert sichtbar. Aufgebraucht zaehlen sie wieder gegen den Tag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
183e922278
commit
67649559f5
@@ -0,0 +1,133 @@
|
||||
using System.Globalization;
|
||||
using ClawdDotNet.Core.Storage;
|
||||
using ClawdDotNet.Core.Tasks;
|
||||
|
||||
namespace ClawdDotNet.Core.Budget;
|
||||
|
||||
/// <summary>
|
||||
/// #717: A special budget — an amount for exactly one task and everything below it, approved
|
||||
/// by a human.
|
||||
///
|
||||
/// <para><b>The one exception to "budgets stay at the agent" (concept 8a).</b> It is a pot of
|
||||
/// its own: runs in the task tree draw on it instead of the agent's daily limit, so a large
|
||||
/// job neither eats the day's regular work nor fails at the daily limit halfway. It does not
|
||||
/// raise the daily limit — work outside the tree never sees it — and it is never silent: it
|
||||
/// exists only through a human's approval (staging), and its runs are marked in the cost
|
||||
/// analysis. Once it is used up, the tree's runs count against the daily budget again, like
|
||||
/// any other work.</para>
|
||||
/// </summary>
|
||||
public sealed record SpecialBudget
|
||||
{
|
||||
/// <summary>The task at the top of the tree the pot is for.</summary>
|
||||
public string TaskId { get; init; } = "";
|
||||
|
||||
public decimal AmountUsd { get; init; }
|
||||
public string Reason { get; init; } = "";
|
||||
|
||||
/// <summary>The agent that asked for it.</summary>
|
||||
public string RequestedBy { get; init; } = "";
|
||||
|
||||
public DateTime ApprovedAt { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>#717: Where special budgets are kept, and what they have used so far.</summary>
|
||||
public interface ISpecialBudgetRepository
|
||||
{
|
||||
Task<SpecialBudget?> GetAsync(string taskId, CancellationToken ct);
|
||||
|
||||
/// <summary>Sets or replaces the pot of a task — a second approval replaces the amount.</summary>
|
||||
Task SetAsync(SpecialBudget budget, CancellationToken ct);
|
||||
|
||||
Task<IReadOnlyList<SpecialBudget>> ListAsync(CancellationToken ct);
|
||||
|
||||
/// <summary>What runs have drawn on the pot, from the usage records marked with it.</summary>
|
||||
Task<decimal> SpentAsync(string taskId, CancellationToken ct);
|
||||
}
|
||||
|
||||
public sealed class SqliteSpecialBudgetRepository(SqliteStorage storage) : ISpecialBudgetRepository
|
||||
{
|
||||
public async Task<SpecialBudget?> GetAsync(string taskId, CancellationToken ct)
|
||||
{
|
||||
await using var conn = await storage.OpenConnectionAsync(ct);
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = "SELECT TaskId, AmountUsd, Reason, RequestedBy, ApprovedAt FROM SpecialBudgets WHERE TaskId = @id COLLATE NOCASE";
|
||||
cmd.Parameters.AddWithValue("@id", taskId.Trim());
|
||||
await using var reader = await cmd.ExecuteReaderAsync(ct);
|
||||
return await reader.ReadAsync(ct) ? Read(reader) : null;
|
||||
}
|
||||
|
||||
public Task SetAsync(SpecialBudget budget, CancellationToken ct)
|
||||
=> storage.WriteAsync(async conn =>
|
||||
{
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = """
|
||||
INSERT INTO SpecialBudgets (TaskId, AmountUsd, Reason, RequestedBy, ApprovedAt)
|
||||
VALUES (@id, @amount, @reason, @by, @at)
|
||||
ON CONFLICT(TaskId) DO UPDATE SET
|
||||
AmountUsd = excluded.AmountUsd, Reason = excluded.Reason,
|
||||
RequestedBy = excluded.RequestedBy, ApprovedAt = excluded.ApprovedAt
|
||||
""";
|
||||
cmd.Parameters.AddWithValue("@id", budget.TaskId.Trim());
|
||||
cmd.Parameters.AddWithValue("@amount", budget.AmountUsd.ToString(CultureInfo.InvariantCulture));
|
||||
cmd.Parameters.AddWithValue("@reason", budget.Reason);
|
||||
cmd.Parameters.AddWithValue("@by", budget.RequestedBy);
|
||||
cmd.Parameters.AddWithValue("@at", budget.ApprovedAt.ToUniversalTime().ToString("O"));
|
||||
await cmd.ExecuteNonQueryAsync(ct);
|
||||
}, ct);
|
||||
|
||||
public async Task<IReadOnlyList<SpecialBudget>> ListAsync(CancellationToken ct)
|
||||
{
|
||||
await using var conn = await storage.OpenConnectionAsync(ct);
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = "SELECT TaskId, AmountUsd, Reason, RequestedBy, ApprovedAt FROM SpecialBudgets ORDER BY ApprovedAt DESC";
|
||||
var result = new List<SpecialBudget>();
|
||||
await using var reader = await cmd.ExecuteReaderAsync(ct);
|
||||
while (await reader.ReadAsync(ct))
|
||||
result.Add(Read(reader));
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<decimal> SpentAsync(string taskId, CancellationToken ct)
|
||||
{
|
||||
await using var conn = await storage.OpenConnectionAsync(ct);
|
||||
using var cmd = conn.CreateCommand();
|
||||
// Amounts are text (exact decimals); summed here, not in SQL.
|
||||
cmd.CommandText = "SELECT CostUsd FROM RunUsage WHERE BudgetPot = @id COLLATE NOCASE";
|
||||
cmd.Parameters.AddWithValue("@id", taskId.Trim());
|
||||
var spent = 0m;
|
||||
await using var reader = await cmd.ExecuteReaderAsync(ct);
|
||||
while (await reader.ReadAsync(ct))
|
||||
spent += decimal.TryParse(reader.GetString(0), NumberStyles.Float, CultureInfo.InvariantCulture, out var d) ? d : 0m;
|
||||
return spent;
|
||||
}
|
||||
|
||||
private static SpecialBudget Read(Microsoft.Data.Sqlite.SqliteDataReader r) => new()
|
||||
{
|
||||
TaskId = r.GetString(0),
|
||||
AmountUsd = decimal.TryParse(r.GetString(1), NumberStyles.Float, CultureInfo.InvariantCulture, out var d) ? d : 0m,
|
||||
Reason = r.GetString(2),
|
||||
RequestedBy = r.GetString(3),
|
||||
ApprovedAt = DateTime.TryParse(r.GetString(4), null, DateTimeStyles.RoundtripKind, out var at) ? at : DateTime.MinValue
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>#717: Finding the pot a task draws on — its own, or the nearest one above it.</summary>
|
||||
public static class SpecialBudgets
|
||||
{
|
||||
/// <summary>A tree deeper than this is a mistake, not a plan; the walk stops there.</summary>
|
||||
private const int MaxDepth = 32;
|
||||
|
||||
public static async Task<SpecialBudget?> FindAsync(
|
||||
string taskId, ITaskRepository tasks, ISpecialBudgetRepository pots, CancellationToken ct)
|
||||
{
|
||||
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
var current = taskId.Trim();
|
||||
for (var depth = 0; depth < MaxDepth && current.Length > 0 && seen.Add(current); depth++)
|
||||
{
|
||||
if (await pots.GetAsync(current, ct) is { } pot)
|
||||
return pot;
|
||||
current = (await tasks.GetAsync(current, ct))?.ParentId?.Trim() ?? "";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user