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
@@ -31,10 +31,10 @@ public sealed class SqliteUsageRepository : IUsageRepository
|
||||
cmd.CommandText = """
|
||||
INSERT INTO RunUsage
|
||||
(AgentId, Model, PromptTokens, CompletionTokens, CachedTokens,
|
||||
CostUsd, CostIsKnown, Status, StepCount, DurationMs, OccurredAt, UsageDate)
|
||||
CostUsd, CostIsKnown, Status, StepCount, DurationMs, OccurredAt, UsageDate, BudgetPot)
|
||||
VALUES
|
||||
(@agentId, @model, @prompt, @completion, @cached,
|
||||
@cost, @costKnown, @status, @steps, @duration, @occurredAt, @usageDate)
|
||||
@cost, @costKnown, @status, @steps, @duration, @occurredAt, @usageDate, @pot)
|
||||
""";
|
||||
|
||||
cmd.Parameters.AddWithValue("@agentId", usage.AgentId);
|
||||
@@ -49,16 +49,24 @@ public sealed class SqliteUsageRepository : IUsageRepository
|
||||
cmd.Parameters.AddWithValue("@duration", usage.DurationMs);
|
||||
cmd.Parameters.AddWithValue("@occurredAt", occurredAt.ToString("O"));
|
||||
cmd.Parameters.AddWithValue("@usageDate", FormatDate(DateOnly.FromDateTime(occurredAt)));
|
||||
cmd.Parameters.AddWithValue("@pot", usage.BudgetPot ?? "");
|
||||
|
||||
await cmd.ExecuteNonQueryAsync(ct);
|
||||
}, ct);
|
||||
|
||||
public async Task<DailyUsage> GetDailyAsync(DateOnly date, string agentId, CancellationToken ct)
|
||||
public Task<DailyUsage> GetDailyAsync(DateOnly date, string agentId, CancellationToken ct)
|
||||
=> GetDailyCoreAsync(date, agentId, withoutPots: false, ct);
|
||||
|
||||
public Task<DailyUsage> GetDailyForBudgetAsync(DateOnly date, string agentId, CancellationToken ct)
|
||||
=> GetDailyCoreAsync(date, agentId, withoutPots: true, ct);
|
||||
|
||||
private async Task<DailyUsage> GetDailyCoreAsync(DateOnly date, string agentId, bool withoutPots, CancellationToken ct)
|
||||
{
|
||||
await using var conn = await _storage.OpenConnectionAsync(ct);
|
||||
using var cmd = conn.CreateCommand();
|
||||
|
||||
var filterAgent = !string.IsNullOrWhiteSpace(agentId);
|
||||
var filterPots = withoutPots ? " AND BudgetPot = ''" : "";
|
||||
|
||||
cmd.CommandText = $"""
|
||||
SELECT COALESCE(SUM(PromptTokens), 0),
|
||||
@@ -66,7 +74,7 @@ public sealed class SqliteUsageRepository : IUsageRepository
|
||||
COUNT(*),
|
||||
COALESCE(SUM(CASE WHEN CostIsKnown = 0 THEN 1 ELSE 0 END), 0)
|
||||
FROM RunUsage
|
||||
WHERE UsageDate = @date {(filterAgent ? "AND AgentId = @agentId" : "")}
|
||||
WHERE UsageDate = @date {(filterAgent ? "AND AgentId = @agentId" : "")}{filterPots}
|
||||
""";
|
||||
cmd.Parameters.AddWithValue("@date", FormatDate(date));
|
||||
if (filterAgent)
|
||||
@@ -88,7 +96,7 @@ public sealed class SqliteUsageRepository : IUsageRepository
|
||||
using var costCmd = conn.CreateCommand();
|
||||
costCmd.CommandText = $"""
|
||||
SELECT CostUsd FROM RunUsage
|
||||
WHERE UsageDate = @date {(filterAgent ? "AND AgentId = @agentId" : "")}
|
||||
WHERE UsageDate = @date {(filterAgent ? "AND AgentId = @agentId" : "")}{filterPots}
|
||||
""";
|
||||
costCmd.Parameters.AddWithValue("@date", FormatDate(date));
|
||||
if (filterAgent)
|
||||
@@ -134,6 +142,9 @@ public sealed class SqliteUsageRepository : IUsageRepository
|
||||
CostUsd, CostIsKnown, Status, StepCount, DurationMs, OccurredAt
|
||||
FROM RunUsage
|
||||
WHERE AgentId = @agent COLLATE NOCASE
|
||||
-- #717: a special budget's runs are the large kind; they would inflate the
|
||||
-- estimate of the agent's ordinary run.
|
||||
AND BudgetPot = ''
|
||||
ORDER BY OccurredAt DESC
|
||||
LIMIT @limit
|
||||
""";
|
||||
|
||||
@@ -28,6 +28,12 @@ public sealed record RunUsage
|
||||
public long DurationMs { get; init; }
|
||||
|
||||
public DateTime OccurredAt { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// #717: The task whose special budget the run drew on; empty for the daily budget. Such
|
||||
/// runs do not count against the daily limits — that is what the pot is for.
|
||||
/// </summary>
|
||||
public string BudgetPot { get; init; } = "";
|
||||
}
|
||||
|
||||
/// <summary>Verbrauch eines Tages, aggregiert.</summary>
|
||||
@@ -51,6 +57,13 @@ public interface IUsageRepository
|
||||
/// <summary>Tagesverbrauch eines Agenten. <paramref name="agentId"/> leer = ganze Instanz.</summary>
|
||||
Task<DailyUsage> GetDailyAsync(DateOnly date, string agentId, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// #717: What counts against the daily limits — the day's usage without runs that drew on
|
||||
/// a special budget. <see cref="GetDailyAsync"/> keeps showing everything that was used.
|
||||
/// </summary>
|
||||
Task<DailyUsage> GetDailyForBudgetAsync(DateOnly date, string agentId, CancellationToken ct)
|
||||
=> GetDailyAsync(date, agentId, ct);
|
||||
|
||||
Task<IReadOnlyList<RunUsage>> GetRecentAsync(int limit, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -88,7 +88,7 @@ public sealed class UsageAnalytics(
|
||||
}
|
||||
|
||||
/// <param name="bucket"><c>hour</c> or <c>day</c>.</param>
|
||||
/// <param name="groupBy"><c>agent</c>, <c>model</c> or <c>source</c>.</param>
|
||||
/// <param name="groupBy"><c>agent</c>, <c>model</c>, <c>source</c> or <c>budget</c> (#717: daily or a special budget).</param>
|
||||
/// <param name="utcOffset">Offset of the viewer's local time - a "day" is the viewer's day.</param>
|
||||
public async Task<IReadOnlyList<SeriesPoint>> SeriesAsync(
|
||||
RunQuery q, string bucket, string groupBy, TimeSpan utcOffset, CancellationToken ct)
|
||||
@@ -216,6 +216,8 @@ public sealed class UsageAnalytics(
|
||||
{
|
||||
"model" => string.IsNullOrWhiteSpace(r.Model) ? "(unknown)" : r.Model,
|
||||
"source" => r.Source,
|
||||
// #717: A special budget is visible apart — it is outside the daily limits.
|
||||
"budget" => string.IsNullOrWhiteSpace(r.BudgetPot) ? "daily" : "special:" + r.BudgetPot,
|
||||
_ => r.AgentId
|
||||
};
|
||||
|
||||
|
||||
@@ -127,6 +127,12 @@ public sealed record RunReceipt
|
||||
/// chain. With it, a result can be followed back to the run that asked for it.
|
||||
/// </summary>
|
||||
public string? CallerRunId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// #717: The task whose special budget the run drew on; <c>null</c> for a run on the daily
|
||||
/// budget. Keeps the pot visible apart in the cost analysis.
|
||||
/// </summary>
|
||||
public string? BudgetPot { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -46,11 +46,11 @@ public sealed class SqliteAuditRepository : IAuditRepository
|
||||
INSERT INTO RunReceipts
|
||||
(RunId, AgentId, Model, Source, TaskId, Status, StepCount,
|
||||
PromptTokens, CompletionTokens, CachedTokens, CostUsd, CostIsKnown,
|
||||
DurationMs, ResultRef, OccurredAt, CallerRunId)
|
||||
DurationMs, ResultRef, OccurredAt, CallerRunId, BudgetPot)
|
||||
VALUES
|
||||
(@runId, @agentId, @model, @source, @taskId, @status, @stepCount,
|
||||
@prompt, @completion, @cached, @cost, @costKnown,
|
||||
@durationMs, @resultRef, @occurredAt, @callerRunId)
|
||||
@durationMs, @resultRef, @occurredAt, @callerRunId, @budgetPot)
|
||||
""";
|
||||
cmd.Parameters.AddWithValue("@runId", receipt.RunId);
|
||||
cmd.Parameters.AddWithValue("@agentId", receipt.AgentId);
|
||||
@@ -68,6 +68,7 @@ public sealed class SqliteAuditRepository : IAuditRepository
|
||||
cmd.Parameters.AddWithValue("@resultRef", receipt.ResultRef);
|
||||
cmd.Parameters.AddWithValue("@occurredAt", Format(receipt.OccurredAt));
|
||||
cmd.Parameters.AddWithValue("@callerRunId", (object?)receipt.CallerRunId ?? DBNull.Value);
|
||||
cmd.Parameters.AddWithValue("@budgetPot", (object?)receipt.BudgetPot ?? DBNull.Value);
|
||||
await cmd.ExecuteNonQueryAsync(ct);
|
||||
}, ct);
|
||||
|
||||
@@ -264,7 +265,7 @@ public sealed class SqliteAuditRepository : IAuditRepository
|
||||
|
||||
private const string ReceiptColumns =
|
||||
"Id, RunId, AgentId, Model, Source, TaskId, Status, StepCount, PromptTokens, " +
|
||||
"CompletionTokens, CachedTokens, CostUsd, CostIsKnown, DurationMs, ResultRef, OccurredAt, CallerRunId";
|
||||
"CompletionTokens, CachedTokens, CostUsd, CostIsKnown, DurationMs, ResultRef, OccurredAt, CallerRunId, BudgetPot";
|
||||
|
||||
private static async Task<IReadOnlyList<AuditEntry>> ReadEntriesAsync(SqliteCommand cmd, CancellationToken ct)
|
||||
{
|
||||
@@ -309,7 +310,8 @@ public sealed class SqliteAuditRepository : IAuditRepository
|
||||
DurationMs = r.GetInt64(13),
|
||||
ResultRef = r.GetString(14),
|
||||
OccurredAt = Parse(r.GetString(15)),
|
||||
CallerRunId = r.IsDBNull(16) ? null : r.GetString(16)
|
||||
CallerRunId = r.IsDBNull(16) ? null : r.GetString(16),
|
||||
BudgetPot = r.IsDBNull(17) ? null : r.GetString(17)
|
||||
};
|
||||
|
||||
private static string Format(DateTime value) => value.ToUniversalTime().ToString("O");
|
||||
|
||||
@@ -54,6 +54,9 @@ public sealed class BudgetGuard
|
||||
private readonly Dictionary<string, RunEstimate> _reservedByAgent = new(StringComparer.OrdinalIgnoreCase);
|
||||
private RunEstimate _reservedInstance;
|
||||
|
||||
/// <summary>#717: What runs in flight have reserved of each special budget.</summary>
|
||||
private readonly Dictionary<string, RunEstimate> _reservedByPot = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public BudgetGuard(IUsageRepository usage, Func<DateTime>? now = null)
|
||||
{
|
||||
_usage = usage;
|
||||
@@ -109,7 +112,7 @@ public sealed class BudgetGuard
|
||||
_reservedInstance += estimate;
|
||||
}
|
||||
|
||||
return (status, new Reservation(this, agent.AgentId, estimate));
|
||||
return (status, new Reservation(() => Release(agent.AgentId, estimate)));
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -117,9 +120,49 @@ public sealed class BudgetGuard
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #717: Reserves a run on a special budget instead of the daily limits. Refused once what
|
||||
/// the pot's runs used, plus what runs in flight reserved, has reached the amount — the
|
||||
/// same rule as for the daily limit: a run is not refused for its own estimate.
|
||||
/// </summary>
|
||||
public async Task<IDisposable?> ReservePotAsync(SpecialBudget pot, decimal spent, string agentId, CancellationToken ct)
|
||||
{
|
||||
var estimate = await EstimateRunAsync(agentId, ct);
|
||||
|
||||
await _reserveGate.WaitAsync(ct);
|
||||
try
|
||||
{
|
||||
lock (_reservedLock)
|
||||
{
|
||||
var reserved = _reservedByPot.GetValueOrDefault(pot.TaskId);
|
||||
if (spent + reserved.CostUsd >= pot.AmountUsd)
|
||||
return null;
|
||||
_reservedByPot[pot.TaskId] = reserved + estimate;
|
||||
}
|
||||
|
||||
return new Reservation(() => ReleasePot(pot.TaskId, estimate));
|
||||
}
|
||||
finally
|
||||
{
|
||||
_reserveGate.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private void ReleasePot(string potId, RunEstimate estimate)
|
||||
{
|
||||
lock (_reservedLock)
|
||||
{
|
||||
var left = _reservedByPot.GetValueOrDefault(potId) - estimate;
|
||||
if (left.CostUsd <= 0 && left.Tokens <= 0)
|
||||
_reservedByPot.Remove(potId);
|
||||
else
|
||||
_reservedByPot[potId] = left;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Today's usage of an agent, or with an empty id of the whole instance.</summary>
|
||||
public Task<DailyUsage> GetUsedTodayAsync(string agentId, CancellationToken ct)
|
||||
=> _usage.GetDailyAsync(DateOnly.FromDateTime(_now()), agentId, ct);
|
||||
=> _usage.GetDailyForBudgetAsync(DateOnly.FromDateTime(_now()), agentId, ct);
|
||||
|
||||
/// <summary>What is currently reserved by runs in flight — for the agent, or with an
|
||||
/// empty id for the whole instance.</summary>
|
||||
@@ -159,14 +202,14 @@ public sealed class BudgetGuard
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Reservation(BudgetGuard guard, string agentId, RunEstimate estimate) : IDisposable
|
||||
private sealed class Reservation(Action release) : IDisposable
|
||||
{
|
||||
private int _disposed;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _disposed, 1) == 0)
|
||||
guard.Release(agentId, estimate);
|
||||
release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,7 +223,7 @@ public sealed class BudgetGuard
|
||||
var agentBudget = agent.Budget;
|
||||
if (agentBudget.HasAnyLimit)
|
||||
{
|
||||
var used = await _usage.GetDailyAsync(today, agent.AgentId, ct);
|
||||
var used = await _usage.GetDailyForBudgetAsync(today, agent.AgentId, ct);
|
||||
|
||||
if (Exceeds(agentBudget.DailyCostUsd, used.CostUsd + agentReserved.CostUsd))
|
||||
{
|
||||
@@ -203,7 +246,7 @@ public sealed class BudgetGuard
|
||||
// Instanzgrenze — gilt für alle Agenten zusammen
|
||||
if (instanceBudget.HasAnyLimit)
|
||||
{
|
||||
var used = await _usage.GetDailyAsync(today, agentId: "", ct);
|
||||
var used = await _usage.GetDailyForBudgetAsync(today, agentId: "", ct);
|
||||
|
||||
if (Exceeds(instanceBudget.DailyCostUsd, used.CostUsd + instanceReserved.CostUsd))
|
||||
{
|
||||
@@ -232,7 +275,7 @@ public sealed class BudgetGuard
|
||||
if (agent.Budget.DailyCostUsd <= 0)
|
||||
return null;
|
||||
|
||||
var used = await _usage.GetDailyAsync(DateOnly.FromDateTime(_now()), agent.AgentId, ct);
|
||||
var used = await _usage.GetDailyForBudgetAsync(DateOnly.FromDateTime(_now()), agent.AgentId, ct);
|
||||
return Math.Max(0m, agent.Budget.DailyCostUsd - used.CostUsd);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
using System.Globalization;
|
||||
using ClawdDotNet.Core.Config;
|
||||
using ClawdDotNet.Core.Tasks;
|
||||
|
||||
namespace ClawdDotNet.Core.Budget;
|
||||
|
||||
/// <summary>
|
||||
/// #717: A task whose estimate exceeds a large share of the agent's daily cost limit (default
|
||||
/// half) is not simply started. Its run plans instead: split it into parts, or ask a human for
|
||||
/// a special budget. Which way, the agent proposes; a special budget is always a human's call.
|
||||
///
|
||||
/// <para><b>Only on an estimate of this task.</b> Measured runs, its siblings' runs, or its
|
||||
/// author's estimate. The agent's average run says nothing about one task — were it enough, an
|
||||
/// agent with expensive runs would have every task stopped for planning.</para>
|
||||
///
|
||||
/// <para>Not for a task already split (it has parts), nor for one in a funded tree, nor for a
|
||||
/// recurring one — its runs are the day's regular work and have their own limit.</para>
|
||||
/// </summary>
|
||||
public sealed class LargeTaskCheck(
|
||||
TaskCostEstimator estimator,
|
||||
ITaskRepository tasks,
|
||||
ISpecialBudgetRepository? pots,
|
||||
Func<IReadOnlyList<AgentConfig>> agents,
|
||||
Func<FillWorkSettings> settings) : ILargeTaskCheck
|
||||
{
|
||||
public async Task<string?> PlanningInstructionAsync(TaskItem task, CancellationToken ct)
|
||||
{
|
||||
var agentId = TaskAssignee.AgentId(task.Assignee);
|
||||
var agent = agents().FirstOrDefault(a => string.Equals(a.AgentId, agentId, StringComparison.OrdinalIgnoreCase));
|
||||
var share = settings().LargeTaskShare;
|
||||
if (agent is not { Budget.DailyCostUsd: > 0 } || share <= 0)
|
||||
return null;
|
||||
|
||||
if (await IsFundedAsync(task, ct) || (await tasks.ListChildrenAsync(task.Id, ct)).Count > 0)
|
||||
return null;
|
||||
|
||||
var estimate = await estimator.EstimateAsync(task, ct);
|
||||
if (estimate.Source is EstimateSource.AgentAverage or EstimateSource.None)
|
||||
return null;
|
||||
|
||||
var threshold = agent.Budget.DailyCostUsd * share;
|
||||
return estimate.Run.CostUsd > threshold
|
||||
? Instruction(task, estimate, agent.Budget.DailyCostUsd, share)
|
||||
: null;
|
||||
}
|
||||
|
||||
public async Task<bool> IsFundedAsync(TaskItem task, CancellationToken ct)
|
||||
=> pots is not null && await SpecialBudgets.FindAsync(task.Id, tasks, pots, ct) is not null;
|
||||
|
||||
/// <summary>What the planning run is told — ahead of the task itself.</summary>
|
||||
public static string Instruction(TaskItem task, TaskEstimate estimate, decimal dailyLimit, decimal share)
|
||||
{
|
||||
var limit = dailyLimit.ToString("0.00", CultureInfo.InvariantCulture);
|
||||
var percent = (share * 100).ToString("0", CultureInfo.InvariantCulture);
|
||||
return $"""
|
||||
[Planning run — from the system, not from the task's author]
|
||||
Task {task.Id} is large: one run is expected to cost {FillWorkNotice.Describe(estimate)}, more than {percent} % of your daily budget of {limit} USD.
|
||||
Do not start the work in this run. Propose one of two ways:
|
||||
1. Split it: create parts with the taskboard (task_create, parent='{task.Id}'), each able to run on its own and small enough for a day. Parts that are not urgent can be fill work (fill_by). Task {task.Id} waits until its parts are done and then runs once more to bring them together.
|
||||
2. Ask for a special budget: Escalation, action request_budget, with task='{task.Id}', amount_usd and reasoning. A human decides. The budget is for this task and its parts only.
|
||||
End with one or two sentences: which way, and why.
|
||||
""";
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,12 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
/// <summary>#689: Escalations, conflicts and decisions; set by the host. Tools reach it through their context.</summary>
|
||||
public Cases.ICaseDesk? CaseDesk { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// #717: Special budgets. A run on a task in a funded tree draws on the pot instead of the
|
||||
/// daily limits; without this, every run counts against the day.
|
||||
/// </summary>
|
||||
public Budget.ISpecialBudgetRepository? SpecialBudgets { get; set; }
|
||||
|
||||
private Organisation.OrgChart CurrentOrganisation()
|
||||
=> OrganisationProvider?.Invoke() ?? new Organisation.OrgChart([], _agentConfigProvider?.Invoke() ?? []);
|
||||
|
||||
@@ -192,7 +198,7 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
|
||||
// Vor der ersten Anfrage prüfen — ein erschöpftes Budget soll gar nichts kosten.
|
||||
// #716: Die Reservierung gilt, bis der Verbrauch erfasst ist (Ende der Methode).
|
||||
var (denied, reservation) = await CheckBudgetAsync(agentConfig, source ?? AuditSource.Direct, taskId, externalCt);
|
||||
var (denied, reservation, pot) = await CheckBudgetAsync(agentConfig, source ?? AuditSource.Direct, taskId, externalCt);
|
||||
if (denied is not null)
|
||||
return denied;
|
||||
using var budgetReservation = reservation;
|
||||
@@ -205,8 +211,8 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
var recorder = CreateRecorder(agentConfig);
|
||||
var result = await RunCoreAsync(agentConfig, userMessage, instanceId, externalCt, runId, source, chain, recorder);
|
||||
activity.Complete(result.Status.ToString());
|
||||
await RecordUsageAsync(agentConfig, result);
|
||||
await RecordReceiptAsync(runId, agentConfig, result, source ?? AuditSource.Direct, taskId, chain.CallerRunId);
|
||||
await RecordUsageAsync(agentConfig, result, pot);
|
||||
await RecordReceiptAsync(runId, agentConfig, result, source ?? AuditSource.Direct, taskId, chain.CallerRunId, pot);
|
||||
await SaveTranscriptAsync(recorder, runId, agentConfig, result, source ?? AuditSource.Direct, taskId);
|
||||
return result;
|
||||
}
|
||||
@@ -478,7 +484,7 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
if (RefuseWhileShuttingDown(agentConfig) is { } zu)
|
||||
return zu;
|
||||
|
||||
var (denied, reservation) = await CheckBudgetAsync(agentConfig, source, taskId, externalCt);
|
||||
var (denied, reservation, pot) = await CheckBudgetAsync(agentConfig, source, taskId, externalCt);
|
||||
if (denied is not null)
|
||||
return denied;
|
||||
using var budgetReservation = reservation;
|
||||
@@ -528,8 +534,8 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
var recorder = CreateRecorder(agentConfig);
|
||||
var result = await ChatCoreAsync(agentConfig, userMessage, instanceId, runCts.Token, source, runId, chain, recorder);
|
||||
activity.Complete(result.Status.ToString());
|
||||
await RecordUsageAsync(agentConfig, result);
|
||||
await RecordReceiptAsync(runId, agentConfig, result, source, taskId, chain.CallerRunId);
|
||||
await RecordUsageAsync(agentConfig, result, pot);
|
||||
await RecordReceiptAsync(runId, agentConfig, result, source, taskId, chain.CallerRunId, pot);
|
||||
await SaveTranscriptAsync(recorder, runId, agentConfig, result, source ?? AuditSource.Direct, taskId);
|
||||
return result;
|
||||
}
|
||||
@@ -1693,6 +1699,7 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
Organisation = CurrentOrganisation(),
|
||||
Trust = ReportingLine.TrustOf(agentConfig, _toolRegistry.Get),
|
||||
Cases = CaseDesk,
|
||||
SpecialBudgets = SpecialBudgets,
|
||||
// #697: Where something an agent hands on came from — known to the engine, not claimed by the agent.
|
||||
RunId = runId,
|
||||
RunTaskId = runId is null ? null : Activity.Find(runId)?.TaskId
|
||||
@@ -1748,16 +1755,21 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
/// <summary>
|
||||
/// Prüft das Tagesbudget. Gibt ein Ergebnis zurück, wenn der Lauf nicht stattfinden
|
||||
/// darf — sonst null und, bei gesetzter Grenze, die Reservierung des Laufs (#716).
|
||||
/// #717: Liegt die Aufgabe in einem Baum mit Sonderbudget, zieht der Lauf den Topf statt
|
||||
/// der Tagesgrenzen; dessen Kennung kommt als <c>Pot</c> zurück.
|
||||
/// </summary>
|
||||
private async Task<(AgentRunResult? Denied, IDisposable? Reservation)> CheckBudgetAsync(
|
||||
private async Task<(AgentRunResult? Denied, IDisposable? Reservation, string? Pot)> CheckBudgetAsync(
|
||||
AgentConfig agentConfig, string? source, string? taskId, CancellationToken ct)
|
||||
{
|
||||
if (_budgetGuard is null)
|
||||
return (null, null);
|
||||
return (null, null, null);
|
||||
|
||||
if (await ReservePotAsync(agentConfig, taskId, ct) is { } potRun)
|
||||
return (null, potRun.Reservation, potRun.Pot);
|
||||
|
||||
var (status, reservation) = await _budgetGuard.ReserveAsync(agentConfig, InstanceBudget, ct);
|
||||
if (status.IsAllowed)
|
||||
return (null, reservation);
|
||||
return (null, reservation, null);
|
||||
|
||||
// #608: Ist ein Budget erschoepft, lehnt es jeden weiteren Lauf des Tages ab. Einmal je
|
||||
// Agent, Grenze und Tag als Warnung genuegt - danach steht jede Ablehnung als Beleg da.
|
||||
@@ -1785,9 +1797,47 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
await RecordReceiptAsync(Guid.NewGuid().ToString("N"), agentConfig, result, source, taskId);
|
||||
|
||||
OnRunCompleted?.Invoke(agentConfig.Model, result);
|
||||
return (result, null);
|
||||
return (result, null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #717: The special budget of the task's tree, if there is one and it has something left.
|
||||
/// A used-up pot is not a refusal — the tree's runs count against the day again, like any
|
||||
/// other work, and that is said once in the log.
|
||||
/// </summary>
|
||||
private async Task<(IDisposable Reservation, string Pot)?> ReservePotAsync(
|
||||
AgentConfig agentConfig, string? taskId, CancellationToken ct)
|
||||
{
|
||||
if (_budgetGuard is null || SpecialBudgets is not { } pots || _taskRepository is null || string.IsNullOrWhiteSpace(taskId))
|
||||
return null;
|
||||
|
||||
try
|
||||
{
|
||||
if (await Budget.SpecialBudgets.FindAsync(taskId, _taskRepository, pots, ct) is not { } pot)
|
||||
return null;
|
||||
|
||||
var spent = await pots.SpentAsync(pot.TaskId, ct);
|
||||
if (await _budgetGuard.ReservePotAsync(pot, spent, agentConfig.AgentId, ct) is { } reservation)
|
||||
return (reservation, pot.TaskId);
|
||||
|
||||
if (_potUsedUp.TryAdd(pot.TaskId, 0))
|
||||
_loggerFactory.CreateLogger("ClawdDotNet.Core.Engine.Budget").LogWarning(
|
||||
"Special budget of {TaskId} is used up ({Spent:F2} of {Amount:F2} USD) — runs in its tree count against the daily budget again",
|
||||
pot.TaskId, spent, pot.AmountUsd);
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
// Without the pot the run is checked like any other — never unchecked.
|
||||
_loggerFactory.CreateLogger("ClawdDotNet.Core.Engine.Budget")
|
||||
.LogWarning(ex, "Special budget lookup failed for task {TaskId}", taskId);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>#717: Pots already reported as used up — said once, not at every run.</summary>
|
||||
private readonly System.Collections.Concurrent.ConcurrentDictionary<string, byte> _potUsedUp = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>Welche Budgetgrenze je Agent zuletzt an welchem Tag als Warnung gemeldet wurde.</summary>
|
||||
private readonly System.Collections.Concurrent.ConcurrentDictionary<string, DateOnly> _budgetGewarnt = new();
|
||||
|
||||
@@ -1795,7 +1845,7 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
/// Schreibt den Verbrauch eines Laufs fort. Fehler hierbei dürfen den Lauf nicht
|
||||
/// nachträglich scheitern lassen — die eigentliche Arbeit ist bereits getan.
|
||||
/// </summary>
|
||||
private async Task RecordUsageAsync(AgentConfig agentConfig, AgentRunResult result)
|
||||
private async Task RecordUsageAsync(AgentConfig agentConfig, AgentRunResult result, string? budgetPot = null)
|
||||
{
|
||||
if (_usageRepository is null)
|
||||
return;
|
||||
@@ -1822,7 +1872,8 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
Status = result.Status.ToString(),
|
||||
StepCount = result.StepCount,
|
||||
DurationMs = (long)result.Duration.TotalMilliseconds,
|
||||
OccurredAt = DateTime.Now
|
||||
OccurredAt = DateTime.Now,
|
||||
BudgetPot = budgetPot ?? ""
|
||||
}, CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -1920,7 +1971,7 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
|
||||
private async Task RecordReceiptAsync(
|
||||
string runId, AgentConfig agentConfig, AgentRunResult result, string? source, string? taskId,
|
||||
string? callerRunId = null)
|
||||
string? callerRunId = null, string? budgetPot = null)
|
||||
{
|
||||
if (_auditRepository is null)
|
||||
return;
|
||||
@@ -1951,7 +2002,8 @@ public sealed class AgentEngine : IAgentMessageRouter, Staging.IFrozenCallExecut
|
||||
DurationMs = (long)result.Duration.TotalMilliseconds,
|
||||
ResultRef = Cap(result.FinalMessage ?? "", 500),
|
||||
OccurredAt = DateTime.Now,
|
||||
CallerRunId = callerRunId
|
||||
CallerRunId = callerRunId,
|
||||
BudgetPot = budgetPot
|
||||
}, CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -93,12 +93,17 @@ public readonly record struct Freigabeeinstufung(StagingDecision Entscheidung, s
|
||||
/// </summary>
|
||||
public static class FreigabeRegeln
|
||||
{
|
||||
/// <summary>Was einen Agenten verändert. Bleibt ausnahmslos freigabepflichtig.</summary>
|
||||
/// <summary>
|
||||
/// Was einen Agenten verändert — und #717 ein Sonderbudget: Geld über die Tagesgrenze
|
||||
/// hinaus entscheidet immer ein Mensch. Bleibt ausnahmslos freigabepflichtig, auch gegen
|
||||
/// Werkzeugkonfiguration und Dauerfreigabe.
|
||||
/// </summary>
|
||||
public static IReadOnlySet<string> ImmerFreigabe { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
"AgentEditor.update_identity",
|
||||
"AgentEditor.update_soul",
|
||||
"AgentEditor.create_agent"
|
||||
"AgentEditor.create_agent",
|
||||
"Escalation.request_budget"
|
||||
};
|
||||
|
||||
public static bool IstImmerFreigabe(string tool, string? operation)
|
||||
@@ -109,7 +114,9 @@ public static class FreigabeRegeln
|
||||
StagingDecision? werkzeugVorgabe, StagingPolicy policy, bool dauerfreigabe = false)
|
||||
{
|
||||
if (IstImmerFreigabe(tool, operation))
|
||||
return new(StagingDecision.Approve, "Änderungen an Agenten brauchen immer eine Freigabe.");
|
||||
return new(StagingDecision.Approve, string.Equals(tool, "Escalation", StringComparison.OrdinalIgnoreCase)
|
||||
? "Ein Sonderbudget entscheidet immer ein Mensch."
|
||||
: "Änderungen an Agenten brauchen immer eine Freigabe.");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(operation))
|
||||
return Nach(policy.Decide(tool, null), "der Standardregel");
|
||||
|
||||
@@ -373,6 +373,15 @@ public sealed class SqliteStorage
|
||||
DecidedAt TEXT NOT NULL,
|
||||
Json TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- #717: Special budgets — one pot per task tree, approved by a human.
|
||||
CREATE TABLE IF NOT EXISTS SpecialBudgets (
|
||||
TaskId TEXT PRIMARY KEY COLLATE NOCASE,
|
||||
AmountUsd TEXT NOT NULL,
|
||||
Reason TEXT NOT NULL DEFAULT '',
|
||||
RequestedBy TEXT NOT NULL DEFAULT '',
|
||||
ApprovedAt TEXT NOT NULL
|
||||
);
|
||||
""";
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
@@ -418,6 +427,13 @@ public sealed class SqliteStorage
|
||||
SpalteErgaenzen(connection, "Tasks", "EstimateUsd", "TEXT NULL");
|
||||
SpalteErgaenzen(connection, "Tasks", "FillAlertedAt", "TEXT NULL");
|
||||
|
||||
// #717: A large task got its planning run — split or special budget, the agent proposed.
|
||||
SpalteErgaenzen(connection, "Tasks", "LargePlannedAt", "TEXT NULL");
|
||||
|
||||
// #717: Runs that drew on a special budget — outside the daily limits, visible apart.
|
||||
SpalteErgaenzen(connection, "RunUsage", "BudgetPot", "TEXT NOT NULL DEFAULT ''");
|
||||
SpalteErgaenzen(connection, "RunReceipts", "BudgetPot", "TEXT NULL");
|
||||
|
||||
// Findet offene Aufrufe beim Start, ohne die ganze Tabelle zu lesen.
|
||||
using var index = connection.CreateCommand();
|
||||
index.CommandText = """
|
||||
|
||||
@@ -53,7 +53,14 @@ public sealed class EngineTaskDispatcher : ITaskDispatcher
|
||||
_clock = clock ?? TimeProvider.System;
|
||||
}
|
||||
|
||||
public async Task<TaskLauf> DispatchAsync(TaskItem task, CancellationToken ct)
|
||||
public Task<TaskLauf> DispatchAsync(TaskItem task, CancellationToken ct)
|
||||
=> DispatchAsync(task, null, ct);
|
||||
|
||||
/// <summary>
|
||||
/// #717: With an instruction from the code ahead of the task — for a large task, that the
|
||||
/// run plans instead of starting the work.
|
||||
/// </summary>
|
||||
public async Task<TaskLauf> DispatchAsync(TaskItem task, string? instruction, CancellationToken ct)
|
||||
{
|
||||
// Poll-Tasks (tool_job) ticken ein Tool statt einen Agenten direkt zu starten.
|
||||
if (task.Type == TaskItemType.ToolJob)
|
||||
@@ -61,6 +68,8 @@ public sealed class EngineTaskDispatcher : ITaskDispatcher
|
||||
|
||||
var agents = _agents();
|
||||
var message = BuildMessage(task, await WhyAsync(task, ct));
|
||||
if (!string.IsNullOrWhiteSpace(instruction))
|
||||
message = instruction.Trim() + "\n\n---\n\n" + message;
|
||||
var kind = TaskAssignee.KindOf(task.Assignee);
|
||||
|
||||
AgentRunResult result;
|
||||
|
||||
@@ -100,6 +100,9 @@ public interface ITaskRepository
|
||||
/// </summary>
|
||||
Task<bool> MarkFillAlertedAsync(string id, DateTime now, CancellationToken ct);
|
||||
|
||||
/// <summary>#717: Records that a large task had its planning run.</summary>
|
||||
Task<bool> MarkLargePlannedAsync(string id, DateTime now, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// Reconciliation beim Start: verwaiste Claims (älter als <paramref name="leaseCutoff"/>)
|
||||
/// lösen und die betroffenen Aufgaben von <see cref="TaskItemStatus.InProgress"/> zurück
|
||||
|
||||
@@ -271,6 +271,17 @@ public sealed class SqliteTaskRepository : ITaskRepository
|
||||
return await cmd.ExecuteNonQueryAsync(ct) > 0;
|
||||
}, ct);
|
||||
|
||||
public Task<bool> MarkLargePlannedAsync(string id, DateTime now, CancellationToken ct)
|
||||
=> _storage.WriteAsync(async conn =>
|
||||
{
|
||||
using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = "UPDATE Tasks SET LargePlannedAt = @now, UpdatedAt = @now WHERE Id = @id";
|
||||
cmd.Parameters.AddWithValue("@now", Format(now));
|
||||
cmd.Parameters.AddWithValue("@id", id);
|
||||
|
||||
return await cmd.ExecuteNonQueryAsync(ct) > 0;
|
||||
}, ct);
|
||||
|
||||
public Task<int> ReleaseStaleClaimsAsync(DateTime leaseCutoff, DateTime now, CancellationToken ct)
|
||||
=> _storage.WriteAsync(async conn =>
|
||||
{
|
||||
@@ -363,7 +374,7 @@ public sealed class SqliteTaskRepository : ITaskRepository
|
||||
"RequireApproval, Acceptance, BlockedBy, OnlyWhenMarketOpen, Body, FileName, " +
|
||||
"ToolName, JobTypeId, LastOccurrence, ClaimToken, ClaimedAt, CreatedAt, UpdatedAt, "
|
||||
+ "BudgetMissCount, LastBudgetMissAt, DeferredUntil, CatchUpPending, CreatedBy, ParentId, OriginRunId, OriginTaskId, "
|
||||
+ "FillBy, EstimateUsd, FillAlertedAt";
|
||||
+ "FillBy, EstimateUsd, FillAlertedAt, LargePlannedAt";
|
||||
|
||||
private static void BindDefinition(SqliteCommand cmd, TaskItem task)
|
||||
{
|
||||
@@ -451,7 +462,8 @@ public sealed class SqliteTaskRepository : ITaskRepository
|
||||
FillBy = r.IsDBNull(30) ? null : Parse(r.GetString(30)),
|
||||
EstimateUsd = r.IsDBNull(31) ? null
|
||||
: decimal.Parse(r.GetString(31), System.Globalization.CultureInfo.InvariantCulture),
|
||||
FillAlertedAt = r.IsDBNull(32) ? null : Parse(r.GetString(32))
|
||||
FillAlertedAt = r.IsDBNull(32) ? null : Parse(r.GetString(32)),
|
||||
LargePlannedAt = r.IsDBNull(33) ? null : Parse(r.GetString(33))
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -274,6 +274,12 @@ public sealed record TaskItem
|
||||
/// </summary>
|
||||
public DateTime? FillAlertedAt { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// #717: When a large task had its planning run — split or special budget, the agent
|
||||
/// proposed. Planned once; afterwards it runs like any other task.
|
||||
/// </summary>
|
||||
public DateTime? LargePlannedAt { get; init; }
|
||||
|
||||
/// <summary>#717: One-time work with a deadline instead of an appointment.</summary>
|
||||
public bool IsFillWork => FillBy is not null && !IsRecurring;
|
||||
|
||||
|
||||
@@ -33,6 +33,26 @@ public interface ITaskDispatcher
|
||||
{
|
||||
/// <summary>Führt die Aufgabe aus und meldet, wie der Lauf ausging.</summary>
|
||||
Task<TaskLauf> DispatchAsync(TaskItem task, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// #717: Runs the task with an instruction from the code ahead of it. Without an
|
||||
/// implementation the instruction is dropped and the task runs as usual.
|
||||
/// </summary>
|
||||
Task<TaskLauf> DispatchAsync(TaskItem task, string? instruction, CancellationToken ct)
|
||||
=> DispatchAsync(task, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #717: Whether a task is too large to simply start — its estimate is a large share of the
|
||||
/// agent's daily budget. Then its run plans instead: split it, or ask for a special budget.
|
||||
/// </summary>
|
||||
public interface ILargeTaskCheck
|
||||
{
|
||||
/// <summary>The instruction for the planning run, or <c>null</c> if the task can simply run.</summary>
|
||||
Task<string?> PlanningInstructionAsync(TaskItem task, CancellationToken ct);
|
||||
|
||||
/// <summary>Whether the task's tree has a special budget — then, after planning, it runs.</summary>
|
||||
Task<bool> IsFundedAsync(TaskItem task, CancellationToken ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -170,6 +190,12 @@ public sealed class TaskScanner : IAsyncDisposable
|
||||
|
||||
private readonly Func<TimeSpan> _fillWarnBefore;
|
||||
|
||||
/// <summary>#717: Whether a task is too large to simply start.</summary>
|
||||
private readonly ILargeTaskCheck? _largeCheck;
|
||||
|
||||
/// <summary>#717: Leaves a note on a task — why the scanner holds it.</summary>
|
||||
private readonly Func<string, string, CancellationToken, Task>? _noteOnTask;
|
||||
|
||||
/// <summary>#717: Fill work already reported as past its deadline — once per task.</summary>
|
||||
private readonly HashSet<string> _warnedOverdueFill = [];
|
||||
|
||||
@@ -205,8 +231,12 @@ public sealed class TaskScanner : IAsyncDisposable
|
||||
ITaskTreeWatch? treeWatch = null,
|
||||
IFillGate? fillGate = null,
|
||||
Func<TaskItem, CancellationToken, Task>? askAboutFill = null,
|
||||
Func<TimeSpan>? fillWarnBefore = null)
|
||||
Func<TimeSpan>? fillWarnBefore = null,
|
||||
ILargeTaskCheck? largeTaskCheck = null,
|
||||
Func<string, string, CancellationToken, Task>? noteOnTask = null)
|
||||
{
|
||||
_largeCheck = largeTaskCheck;
|
||||
_noteOnTask = noteOnTask;
|
||||
_treeWatch = treeWatch;
|
||||
_fillGate = fillGate;
|
||||
_askAboutFill = askAboutFill;
|
||||
@@ -616,10 +646,15 @@ public sealed class TaskScanner : IAsyncDisposable
|
||||
|
||||
private async Task ProcessCoreAsync(TaskItem task, string token, CancellationToken ct)
|
||||
{
|
||||
// #717: A large task is not simply started — its run plans first.
|
||||
var planning = await PlanningInstructionAsync(task, ct);
|
||||
|
||||
TaskLauf lauf;
|
||||
try
|
||||
{
|
||||
lauf = await _dispatcher.DispatchAsync(task, ct);
|
||||
lauf = planning is null
|
||||
? await _dispatcher.DispatchAsync(task, ct)
|
||||
: await _dispatcher.DispatchAsync(task, planning, ct);
|
||||
}
|
||||
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
||||
{
|
||||
@@ -656,6 +691,12 @@ public sealed class TaskScanner : IAsyncDisposable
|
||||
|
||||
_zurueckgestellt.TryRemove(task.Id, out _);
|
||||
|
||||
if (planning is not null)
|
||||
{
|
||||
await SettlePlanningAsync(task, token, lauf, now, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Endstatus:
|
||||
// - Fehlschlag → zurück auf todo (der Marker steht schon beim Claim, derselbe Termin
|
||||
// wird nicht wiederholt).
|
||||
@@ -682,9 +723,99 @@ public sealed class TaskScanner : IAsyncDisposable
|
||||
await _repo.ClearBudgetMissAsync(task.Id, now, ct);
|
||||
|
||||
if (ok && final == TaskItemStatus.Done)
|
||||
{
|
||||
await UnblockDependentsAsync(task.Id, now, ct);
|
||||
await ResumeLargeParentAsync(task, now, ct);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #717: The planning instruction for a large one-time task that has not been planned yet.
|
||||
/// A failing check costs the check, not the run.
|
||||
/// </summary>
|
||||
private async Task<string?> PlanningInstructionAsync(TaskItem task, CancellationToken ct)
|
||||
{
|
||||
if (_largeCheck is null || task.IsRecurring || task.LargePlannedAt is not null || task.Type == TaskItemType.ToolJob)
|
||||
return null;
|
||||
try
|
||||
{
|
||||
return await _largeCheck.PlanningInstructionAsync(task, ct);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogWarning(ex, "Large-task check failed for {TaskId} — it runs as usual", task.Id);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #717: After the planning run of a large task. Split into parts → it waits for them.
|
||||
/// A special budget approved meanwhile → it runs. Otherwise it waits, blocked, for the
|
||||
/// human's decision on the budget — never silently: the task says why.
|
||||
///
|
||||
/// <para>The occurrence is given back first: the planning run did not do the work, and a
|
||||
/// one-time task whose marker is used up would never run again.</para>
|
||||
/// </summary>
|
||||
private async Task SettlePlanningAsync(TaskItem task, string token, TaskLauf lauf, DateTime now, CancellationToken ct)
|
||||
{
|
||||
if (!await _repo.ReleaseClaimAsync(task.Id, token, task.LastOccurrence, now, ct))
|
||||
return;
|
||||
if (lauf != TaskLauf.Erledigt)
|
||||
return; // the planning did not take place or failed — the next due tick plans again
|
||||
|
||||
await _repo.MarkLargePlannedAsync(task.Id, now, ct);
|
||||
|
||||
var parts = (await _repo.ListChildrenAsync(task.Id, ct)).Count(IsOpen);
|
||||
string? note = null;
|
||||
if (parts > 0)
|
||||
{
|
||||
await _repo.SetStatusAsync(task.Id, TaskItemStatus.Blocked, now, ct);
|
||||
note = $"Large task: split into {parts} part(s). It runs again once they are done.";
|
||||
}
|
||||
else if (_largeCheck is not null && await _largeCheck.IsFundedAsync(task, ct))
|
||||
{
|
||||
_logger.LogInformation("Large task {TaskId} has a special budget — it runs on it", task.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _repo.SetStatusAsync(task.Id, TaskItemStatus.Blocked, now, ct);
|
||||
note = "Large task: not started. It waits for a special budget to be approved (staging), "
|
||||
+ "or for a human to split it or set it back to todo.";
|
||||
}
|
||||
|
||||
if (note is not null)
|
||||
{
|
||||
_logger.LogInformation("Task {TaskId}: {Note}", task.Id, note);
|
||||
if (_noteOnTask is not null)
|
||||
{
|
||||
try { await _noteOnTask(task.Id, note, ct); }
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogWarning(ex, "Could not note on task {TaskId}", task.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #717: A part of a split large task is done. When it was the last open one, the large
|
||||
/// task is taken up again — to bring the parts together.
|
||||
/// </summary>
|
||||
private async Task ResumeLargeParentAsync(TaskItem part, DateTime now, CancellationToken ct)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(part.ParentId)
|
||||
|| await _repo.GetAsync(part.ParentId, ct) is not { Status: TaskItemStatus.Blocked, LargePlannedAt: not null } parent)
|
||||
return;
|
||||
if ((await _repo.ListChildrenAsync(parent.Id, ct)).Any(IsOpen))
|
||||
return;
|
||||
|
||||
await _repo.SetStatusAsync(parent.Id, TaskItemStatus.Todo, now, ct);
|
||||
_logger.LogInformation("Large task {TaskId}: all parts done — it runs again to bring them together", parent.Id);
|
||||
}
|
||||
|
||||
private static bool IsOpen(TaskItem task)
|
||||
=> task.Status is not (TaskItemStatus.Done or TaskItemStatus.Canceled or TaskItemStatus.Archived);
|
||||
|
||||
/// <summary>
|
||||
/// #716: The budget refused the run. A one-time task is carried over to the next day
|
||||
/// with its occurrence intact; a recurring one follows the agent's policy — the
|
||||
|
||||
@@ -49,6 +49,9 @@ public sealed record AgentToolContext(
|
||||
/// <summary>#689: The desk for escalations, conflicts and decisions. <c>null</c> outside the engine.</summary>
|
||||
public Cases.ICaseDesk? Cases { get; init; }
|
||||
|
||||
/// <summary>#717: Special budgets — set up by an approved request_budget.</summary>
|
||||
public Budget.ISpecialBudgetRepository? SpecialBudgets { get; init; }
|
||||
|
||||
/// <summary>#697: The run this call belongs to — for stamping where something came from.</summary>
|
||||
public string? RunId { get; init; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user