diff options
| author | tanaka takashi <isazaka@gmail.com> | 2026-08-09 21:08:20 +0900 |
|---|---|---|
| committer | tanaka takashi <isazaka@gmail.com> | 2026-08-09 21:08:20 +0900 |
| commit | b07b41cd435991cd254957af79055080c85b346e (patch) | |
| tree | 27720d3ce14e4d0b84670a55ac26a2f877ce3a20 /src/Agent.Core/Tasks/TaskRegistry.cs | |
| parent | 2fefd29e86c3b926d0cf4a362251d3f119688612 (diff) | |
feat: AI Agent Sandbox MCP Server v1を実装
Specifications、Design、ImplementationPlanの優先順位を整理し、単一Task実行モデル、typed option優先、設定可能なresource上限、local Ollamaを必須とするRelease Gateを文書化。
.NET 10/C# 14の5 production project、3 test project、E2E Clientを構築。typed YAML、en/ja locale、fixed/user prompt、stdioおよびloopback限定Streamable HTTP、agent_run/agent_status、Session、Ollama text/tool/Vision、TaskGraphとtoken accountingを実装。
Windows sandboxとしてfixed AppContainer、canonical/reparse検査、能力別ACL、path+SHA-256 executable approval、child-process restriction、Job Object、task journal、atomic file transaction、crash recoveryを実装。WFPのIPv4/IPv6 default denyとexact allow、dynamic session、temporary loopback exemption、管理者/BFE診断を追加。
Security/Recovery test、MCP/Ollama integration test、stdio/HTTP Operational E2E、Release Bundle生成・checksum・manifest・fresh extraction検査を追加。Requirement TraceabilityとPhase 0〜11の進捗、初回セットアップ可能なREADMEを整備。
Debug/Release/publish/objをrepository rootのbuild/へ集約し、旧project-local bin/objへの依存を除去。artifacts/とbuild/をGit管理外に設定し、clean checkoutでもsolution buildからE2E Clientまで生成可能にした。
Diffstat (limited to 'src/Agent.Core/Tasks/TaskRegistry.cs')
| -rw-r--r-- | src/Agent.Core/Tasks/TaskRegistry.cs | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/Agent.Core/Tasks/TaskRegistry.cs b/src/Agent.Core/Tasks/TaskRegistry.cs new file mode 100644 index 0000000..b61e525 --- /dev/null +++ b/src/Agent.Core/Tasks/TaskRegistry.cs @@ -0,0 +1,115 @@ +using System.Collections.Concurrent; +using System.Threading.Channels; +using Agent.Core.Sessions; + +namespace Agent.Core.Tasks; + +public sealed class TaskRegistry +{ + private readonly ConcurrentDictionary<Guid, MutableTask> _tasks = new(); + private readonly SessionStore _sessions; + private readonly Channel<Guid> _queue = Channel.CreateUnbounded<Guid>(new UnboundedChannelOptions { SingleReader = true, SingleWriter = false }); + + public TaskRegistry(SessionStore? sessions = null) => _sessions = sessions ?? new SessionStore(); + + public SessionStore Sessions => _sessions; + + public TaskSnapshot Enqueue(string request, Guid? requestedSessionId, double? temperature, IReadOnlyList<string>? images = null) + { + if (string.IsNullOrWhiteSpace(request)) throw new ArgumentException("Request cannot be empty.", nameof(request)); + if (temperature is < 0 or > 2) throw new ArgumentOutOfRangeException(nameof(temperature)); + Guid sessionId; + if (requestedSessionId is { } existing) + { + if (!_sessions.TryGet(existing, out _)) throw new KeyNotFoundException($"Session '{existing}' was not found."); + sessionId = existing; + } + else + { + sessionId = _sessions.Create().SessionId; + } + + var task = new MutableTask(Guid.NewGuid(), sessionId, request, temperature, images ?? []); + if (!_tasks.TryAdd(task.TaskId, task)) throw new InvalidOperationException("Could not register task."); + if (!_queue.Writer.TryWrite(task.TaskId)) + { + _tasks.TryRemove(task.TaskId, out _); + throw new InvalidOperationException("Task queue is not accepting work."); + } + return task.Snapshot(); + } + + public bool TryGet(Guid taskId, Guid? sessionId, out TaskSnapshot? snapshot) + { + if (_tasks.TryGetValue(taskId, out MutableTask? task) && (sessionId is null || task.SessionId == sessionId)) + { + snapshot = task.Snapshot(); + return true; + } + snapshot = null; + return false; + } + + public IAsyncEnumerable<Guid> ReadQueuedAsync(CancellationToken cancellationToken) => _queue.Reader.ReadAllAsync(cancellationToken); + + public bool Transition(Guid taskId, AgentTaskState state, string? currentTask = null, string? result = null) + { + return _tasks.TryGetValue(taskId, out MutableTask? task) && task.Transition(state, currentTask, result); + } + + public bool AddUsage(Guid taskId, long inputTokens, long outputTokens) => + _tasks.TryGetValue(taskId, out MutableTask? task) && task.AddUsage(inputTokens, outputTokens); + + private sealed class MutableTask(Guid taskId, Guid sessionId, string request, double? temperature, IReadOnlyList<string> images) + { + private readonly object _sync = new(); + private AgentTaskState _state = AgentTaskState.Queued; + private string? _currentTask; + private string? _result; + private long _inputTokens; + private long _outputTokens; + + public Guid TaskId { get; } = taskId; + public Guid SessionId { get; } = sessionId; + + public TaskSnapshot Snapshot() + { + lock (_sync) + { + int completed = _state == AgentTaskState.Completed ? 1 : 0; + return new(TaskId, SessionId, _state, request, temperature, _currentTask, _result, completed, 1, _inputTokens, _outputTokens, 0, images); + } + } + + public bool AddUsage(long inputTokens, long outputTokens) + { + if (inputTokens < 0 || outputTokens < 0) return false; + lock (_sync) + { + _inputTokens += inputTokens; + _outputTokens += outputTokens; + return true; + } + } + + public bool Transition(AgentTaskState next, string? currentTask, string? result) + { + lock (_sync) + { + if (!Allowed(_state, next)) return false; + _state = next; + _currentTask = currentTask; + _result = result; + return true; + } + } + + private static bool Allowed(AgentTaskState current, AgentTaskState next) => current switch + { + AgentTaskState.Queued => next is AgentTaskState.Planning or AgentTaskState.Failed or AgentTaskState.Cancelled, + AgentTaskState.Planning => next is AgentTaskState.Running or AgentTaskState.Failed or AgentTaskState.Cancelled, + AgentTaskState.Running => next is AgentTaskState.Planning or AgentTaskState.Completed or AgentTaskState.Failed or AgentTaskState.Cancelled, + _ => false + }; + } +} |
