diff options
| author | tanaka takashi <isazaka@gmail.com> | 2026-08-11 22:20:25 +0900 |
|---|---|---|
| committer | tanaka takashi <isazaka@gmail.com> | 2026-08-11 22:20:25 +0900 |
| commit | 842e55654db060c84448f3a0deddb24f77fad5a7 (patch) | |
| tree | faafb9e6f26a0eb5a037d8d3ceff4a512785e8f4 /src/Agent.Core | |
| parent | d8cf8947ff93230ae5282fc5058b0e49ddae42aa (diff) | |
Verify task-specific external effect evidence
Diffstat (limited to 'src/Agent.Core')
| -rw-r--r-- | src/Agent.Core/Runtime/AgentRuntime.cs | 5 | ||||
| -rw-r--r-- | src/Agent.Core/Runtime/PlanResult.cs | 7 | ||||
| -rw-r--r-- | src/Agent.Core/Runtime/PlannerService.cs | 23 | ||||
| -rw-r--r-- | src/Agent.Core/Tools/InternalToolCatalog.cs | 2 | ||||
| -rw-r--r-- | src/Agent.Core/Tools/TaskGraphToolHandler.cs | 13 |
5 files changed, 41 insertions, 9 deletions
diff --git a/src/Agent.Core/Runtime/AgentRuntime.cs b/src/Agent.Core/Runtime/AgentRuntime.cs index 894f50e..333d557 100644 --- a/src/Agent.Core/Runtime/AgentRuntime.cs +++ b/src/Agent.Core/Runtime/AgentRuntime.cs @@ -279,7 +279,7 @@ public sealed class AgentRuntime(TaskRegistry registry, IOllamaClient ollama, Ag toolResult = new(false, ex.Message, false, $"{call.Function.Name}:{ex.GetType().Name}:{ex.Message}"); } - externalEffectEvidence |= toolResult.Success && toolResult.ExternalEffect; + externalEffectEvidence |= toolResult.Success && toolResult.ExternalEffect && plannerTask.RequiredEffectTools.Contains(call.Function.Name); // task_update is a metadata tool for graph bookkeeping; its failures should not block completion. bool isMetadataTool = call.Function.Name == "task_update"; if (toolResult.Success) unresolvedToolFailures.Remove(call.Function.Name); @@ -298,6 +298,9 @@ public sealed class AgentRuntime(TaskRegistry registry, IOllamaClient ollama, Ag } ToolUseGuardResult callGuard = guard.Evaluate(requiresExternalEffect, true, externalEffectEvidence, failureFingerprint); if (callGuard.Decision == ToolUseGuardDecision.Fail) throw new InvalidOperationException(callGuard.Observation); + if (callGuard.Decision == ToolUseGuardDecision.Retry) + messages.Add(new OllamaMessage("system", callGuard.Observation + " Required effect Tool(s) for the current Task: " + + string.Join(", ", plannerTask.RequiredEffectTools.Order(StringComparer.Ordinal)) + ".")); continue; } diff --git a/src/Agent.Core/Runtime/PlanResult.cs b/src/Agent.Core/Runtime/PlanResult.cs index bad41fa..2e8dbae 100644 --- a/src/Agent.Core/Runtime/PlanResult.cs +++ b/src/Agent.Core/Runtime/PlanResult.cs @@ -36,6 +36,9 @@ public sealed class PlannedTask [JsonPropertyName("requires_external_effect")] public bool RequiresExternalEffect { get; init; } + + [JsonPropertyName("required_effect_tools")] + public List<string> RequiredEffectTools { get; init; } = []; } /// <summary> @@ -43,9 +46,9 @@ public sealed class PlannedTask /// </summary> public static class PlanSchema { - public static string Json => """{"type":"object","properties":{"tasks":{"type":"array","items":{"type":"object","properties":{"description":{"type":"string"},"dependencies":{"type":"array","items":{"type":"integer"}},"budget_class":{"type":"string","enum":["1K","2K","4K","8K","16K","32K","64K","128K"]},"optimistic_tokens":{"type":"integer","minimum":1},"most_likely_tokens":{"type":"integer","minimum":1},"pessimistic_tokens":{"type":"integer","minimum":1},"requires_external_effect":{"type":"boolean"}},"required":["description","dependencies","budget_class","optimistic_tokens","most_likely_tokens","pessimistic_tokens","requires_external_effect"]}}},"required":["tasks"]}"""; + public static string Json => """{"type":"object","properties":{"tasks":{"type":"array","items":{"type":"object","properties":{"description":{"type":"string"},"dependencies":{"type":"array","items":{"type":"integer"}},"budget_class":{"type":"string","enum":["1K","2K","4K","8K","16K","32K","64K","128K"]},"optimistic_tokens":{"type":"integer","minimum":1},"most_likely_tokens":{"type":"integer","minimum":1},"pessimistic_tokens":{"type":"integer","minimum":1},"requires_external_effect":{"type":"boolean"},"required_effect_tools":{"type":"array","items":{"type":"string","enum":["file_edit","process_run","dir"]}}},"required":["description","dependencies","budget_class","optimistic_tokens","most_likely_tokens","pessimistic_tokens","requires_external_effect","required_effect_tools"]}}},"required":["tasks"]}"""; private static readonly JsonElement _element = JsonDocument.Parse(Json).RootElement.Clone(); public static JsonElement Element => _element; -}
\ No newline at end of file +} diff --git a/src/Agent.Core/Runtime/PlannerService.cs b/src/Agent.Core/Runtime/PlannerService.cs index aa7d3a5..18e4043 100644 --- a/src/Agent.Core/Runtime/PlannerService.cs +++ b/src/Agent.Core/Runtime/PlannerService.cs @@ -93,7 +93,8 @@ public sealed class PlannerService(IOllamaClient ollama, AgentRuntimeSettings se } string description = string.IsNullOrWhiteSpace(pt.Description) ? originalRequest : pt.Description; - PlannerTask task = graph.AddTask(description, budgetClass, estimate, dependencies, pt.RequiresExternalEffect); + PlannerTask task = graph.AddTask(description, budgetClass, estimate, dependencies, pt.RequiresExternalEffect, + requiredEffectTools: ValidateRequiredEffectTools(pt)); idMap.Add(task.Id); } @@ -111,7 +112,7 @@ public sealed class PlannerService(IOllamaClient ollama, AgentRuntimeSettings se foreach (PlannerTask item in completed) { Guid[] dependencies = item.Dependencies.Where(id => completed.Any(candidate => candidate.Id == id)).ToArray(); - PlannerTask copy = graph.AddTask(item.Description, item.BudgetClass, item.Estimate, dependencies, item.RequiresExternalEffect, item.Id); + PlannerTask copy = graph.AddTask(item.Description, item.BudgetClass, item.Estimate, dependencies, item.RequiresExternalEffect, item.Id, item.RequiredEffectTools); graph.AddUsage(copy.Id, item.ActualInputTokens, item.ActualOutputTokens); graph.SetState(copy.Id, PlannerTaskState.Completed); } @@ -140,7 +141,8 @@ public sealed class PlannerService(IOllamaClient ollama, AgentRuntimeSettings se } string description = string.IsNullOrWhiteSpace(pt.Description) ? originalRequest : pt.Description; - PlannerTask task = graph.AddTask(description, bc, est, dependencies, pt.RequiresExternalEffect); + PlannerTask task = graph.AddTask(description, bc, est, dependencies, pt.RequiresExternalEffect, + requiredEffectTools: ValidateRequiredEffectTools(pt)); idMap.Add(task.Id); } @@ -167,6 +169,19 @@ public sealed class PlannerService(IOllamaClient ollama, AgentRuntimeSettings se return new TokenEstimate(o, m, p); } + private static IReadOnlySet<string> ValidateRequiredEffectTools(PlannedTask task) + { + HashSet<string> tools = task.RequiredEffectTools.ToHashSet(StringComparer.Ordinal); + string[] allowed = ["file_edit", "process_run", "dir"]; + if (tools.Any(tool => !allowed.Contains(tool, StringComparer.Ordinal))) + throw new PlannerValidationException("required_effect_tools contains a Tool that cannot provide external-effect evidence."); + if (task.RequiresExternalEffect && tools.Count == 0) + throw new PlannerValidationException("An external-effect Task must provide required_effect_tools."); + if (!task.RequiresExternalEffect && tools.Count != 0) + throw new PlannerValidationException("A non-effect Task must have empty required_effect_tools."); + return tools; + } + private static PlanResult? ParsePlan(string content) { try @@ -285,10 +300,12 @@ public sealed class PlannerService(IOllamaClient ollama, AgentRuntimeSettings se - budget_class: one of 1K, 2K, 4K, 8K, 16K, 32K, 64K, 128K (token budget for this task) - optimistic_tokens, most_likely_tokens, pessimistic_tokens: PERT estimates (0 < O <= M <= P <= budget) - requires_external_effect: true if this task requires a verifiable external effect (file edit, process run, etc.) + - required_effect_tools: the exact effect-producing Internal Tool names whose successful result can complete this task; use only file_edit, process_run, or dir For pure analysis or informational tasks, requires_external_effect should be false. For tasks that modify files, run processes, or produce verifiable side effects, requires_external_effect should be true. Read-only operations (file_read, file_search, and dir list) do not create an external effect and must use requires_external_effect=false. + When requires_external_effect is false, required_effect_tools must be empty. When true, it must contain at least one Tool and only the Tool(s) that prove this task's own expected effect. Preserve every user-supplied absolute path, executable, cwd, argument array, operation name, and literal value exactly in the task that uses it; never abbreviate these values into a shell-like command. Keep each task limited to its own described operation. Do not put later dependent operations into an earlier task. A single task is valid for simple requests. diff --git a/src/Agent.Core/Tools/InternalToolCatalog.cs b/src/Agent.Core/Tools/InternalToolCatalog.cs index d261269..e28673d 100644 --- a/src/Agent.Core/Tools/InternalToolCatalog.cs +++ b/src/Agent.Core/Tools/InternalToolCatalog.cs @@ -19,7 +19,7 @@ public static class InternalToolCatalog Define("file_read", "Read an approved file.", """{"type":"object","additionalProperties":false,"required":["path","mode"],"properties":{"path":{"type":"string"},"mode":{"type":"string","enum":["whole","range","metadata"]},"start":{"type":"integer","minimum":0},"length":{"type":"integer","minimum":0}}}"""), Define("file_edit", "Edit an approved file transactionally.", """{"type":"object","additionalProperties":false,"required":["path","operation"],"properties":{"path":{"type":"string"},"operation":{"type":"string","enum":["overwrite","patch","atomic_replace","delete","rename"]},"content":{"type":["string","null"]},"patch":{"type":["string","null"]},"expected_sha256":{"type":["string","null"]},"new_path":{"type":["string","null"]}}}"""), Define("process_run", "Run one approved executable with separated arguments.", """{"type":"object","additionalProperties":false,"required":["executable","args","cwd"],"properties":{"executable":{"type":"string"},"args":{"type":"array","items":{"type":"string"}},"cwd":{"type":"string"},"timeout_seconds":{"type":"integer","minimum":1},"stdin":{"type":["string","null"]}}}"""), - Define("task_update", "Update the deterministic task graph structure or estimates.", """{"type":"object","additionalProperties":false,"required":["operation"],"properties":{"operation":{"type":"string","enum":["add","add_dependency","set_estimate"]},"task_id":{"type":["string","null"]},"description":{"type":["string","null"]},"depends_on":{"type":"array","items":{"type":"string"}},"budget_class":{"type":["string","null"],"enum":["1K","2K","4K","8K","16K","32K","64K","128K",null]},"optimistic_tokens":{"type":"integer","minimum":0},"most_likely_tokens":{"type":"integer","minimum":0},"pessimistic_tokens":{"type":"integer","minimum":0},"requires_external_effect":{"type":["boolean","null"]}}}"""), + Define("task_update", "Update the deterministic task graph structure or estimates.", """{"type":"object","additionalProperties":false,"required":["operation"],"properties":{"operation":{"type":"string","enum":["add","add_dependency","set_estimate"]},"task_id":{"type":["string","null"]},"description":{"type":["string","null"]},"depends_on":{"type":"array","items":{"type":"string"}},"budget_class":{"type":["string","null"],"enum":["1K","2K","4K","8K","16K","32K","64K","128K",null]},"optimistic_tokens":{"type":"integer","minimum":0},"most_likely_tokens":{"type":"integer","minimum":0},"pessimistic_tokens":{"type":"integer","minimum":0},"requires_external_effect":{"type":["boolean","null"]},"required_effect_tools":{"type":"array","items":{"type":"string","enum":["file_edit","process_run","dir"]}}}}"""), Define("dir", "Create, remove, or list directories under an approved root. Only empty directories can be removed.", """{"type":"object","additionalProperties":false,"required":["path","operation"],"properties":{"path":{"type":"string"},"operation":{"type":"string","enum":["mkdir","rmdir","list"]}}}""") ]; return definitions.ToDictionary(static definition => definition.Name, StringComparer.Ordinal); diff --git a/src/Agent.Core/Tools/TaskGraphToolHandler.cs b/src/Agent.Core/Tools/TaskGraphToolHandler.cs index 26d5a27..fb3979b 100644 --- a/src/Agent.Core/Tools/TaskGraphToolHandler.cs +++ b/src/Agent.Core/Tools/TaskGraphToolHandler.cs @@ -52,7 +52,8 @@ public sealed class TaskGraphToolHandler(int maxTasks = 32, SessionStore? sessio Estimate(arguments), Ids(arguments, "depends_on"), requiresExternalEffect: OptionalBool(arguments, "requires_external_effect"), - id: OptionalId(arguments, "task_id")); + id: OptionalId(arguments, "task_id"), + requiredEffectTools: Strings(arguments, "required_effect_tools")); break; case "add_dependency": Guid taskIdValue = RequiredId(arguments, "task_id"); @@ -79,7 +80,8 @@ public sealed class TaskGraphToolHandler(int maxTasks = 32, SessionStore? sessio depends_on = task.Dependencies, state = task.State.ToString().ToLowerInvariant(), expected_tokens = task.ExpectedTokens, - requires_external_effect = task.RequiresExternalEffect + requires_external_effect = task.RequiresExternalEffect, + required_effect_tools = task.RequiredEffectTools }), critical_path = graph.CriticalPath.TaskIds, critical_path_tokens = graph.CriticalPath.CumulativeTokenCost, @@ -114,6 +116,13 @@ public sealed class TaskGraphToolHandler(int maxTasks = 32, SessionStore? sessio return value.ValueKind == JsonValueKind.True; } + private static string[] Strings(JsonElement arguments, string property) + { + if (!arguments.TryGetProperty(property, out JsonElement value) || value.ValueKind is JsonValueKind.Null or JsonValueKind.Undefined) return []; + if (value.ValueKind != JsonValueKind.Array) throw new ToolValidationException($"'{property}' must be an array."); + return value.EnumerateArray().Select(item => item.GetString() ?? throw new ToolValidationException($"'{property}' entries must be strings.")).ToArray(); + } + private static void DemandStructuralMutation(TaskGraph graph, Guid taskId) { PlannerTaskState state = graph.Get(taskId).State; |
