From b07b41cd435991cd254957af79055080c85b346e Mon Sep 17 00:00:00 2001 From: tanaka takashi Date: Sun, 9 Aug 2026 21:08:20 +0900 Subject: feat: AI Agent Sandbox MCP Server v1を実装 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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まで生成可能にした。 --- src/Agent.App/Logging/JsonLinesRuntimeObserver.cs | 37 +++++++++++++++ src/Agent.App/Logging/RollingFileLog.cs | 55 +++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/Agent.App/Logging/JsonLinesRuntimeObserver.cs create mode 100644 src/Agent.App/Logging/RollingFileLog.cs (limited to 'src/Agent.App/Logging') diff --git a/src/Agent.App/Logging/JsonLinesRuntimeObserver.cs b/src/Agent.App/Logging/JsonLinesRuntimeObserver.cs new file mode 100644 index 0000000..b7cbd26 --- /dev/null +++ b/src/Agent.App/Logging/JsonLinesRuntimeObserver.cs @@ -0,0 +1,37 @@ +using System.Text.Json; +using Agent.Core.Ollama; +using Agent.Core.Runtime; +using Agent.Core.Tools; + +namespace Agent.App.Logging; + +public sealed class JsonLinesRuntimeObserver(string logDirectory) : IAgentRuntimeObserver +{ + private readonly object _sync = new(); + private readonly string _path = Path.Combine(logDirectory, "llm.jsonl"); + + public void LlmRequest(Guid taskId, int iteration, IReadOnlyList messages) => Write(new + { + timestamp = DateTimeOffset.UtcNow, task_id = taskId, event_name = "llm.request", iteration, + messages = messages.Select(static message => new { message.Role, message.Content, image_count = message.Images?.Count ?? 0, message.ToolName }) + }); + + public void LlmResponse(Guid taskId, int iteration, OllamaResult result) => Write(new + { + timestamp = DateTimeOffset.UtcNow, task_id = taskId, event_name = "llm.response", iteration, + result.Content, input_tokens = result.InputTokens, output_tokens = result.OutputTokens, + tool_calls = result.ToolCalls?.Select(static call => call.Function.Name).ToArray() ?? [] + }); + + public void ToolResult(Guid taskId, string toolName, ToolExecutionResult result) => Write(new + { + timestamp = DateTimeOffset.UtcNow, task_id = taskId, event_name = "llm.tool_result", tool_name = toolName, + result.Success, result.ExternalEffect, result.Output, result.FailureFingerprint + }); + + private void Write(object value) + { + string line = JsonSerializer.Serialize(value); + lock (_sync) File.AppendAllText(_path, line + Environment.NewLine); + } +} diff --git a/src/Agent.App/Logging/RollingFileLog.cs b/src/Agent.App/Logging/RollingFileLog.cs new file mode 100644 index 0000000..801768d --- /dev/null +++ b/src/Agent.App/Logging/RollingFileLog.cs @@ -0,0 +1,55 @@ +using System.Globalization; + +namespace Agent.App.Logging; + +public sealed class RollingFileLog : IDisposable +{ + private readonly object _sync = new(); + private readonly string _path; + private readonly long _maxBytes; + private readonly int _retainedFiles; + private StreamWriter _writer; + + public RollingFileLog(string directory, long maxBytes, int retainedFiles) + { + Directory.CreateDirectory(directory); + _path = Path.Combine(directory, "application.log"); + _maxBytes = maxBytes; + _retainedFiles = retainedFiles; + _writer = Open(); + } + + public void Information(string message) => Write("INF", message); + public void Error(string message) => Write("ERR", message); + + private void Write(string level, string message) + { + lock (_sync) + { + if (_writer.BaseStream.Length >= _maxBytes) Rotate(); + string line = string.Create(CultureInfo.InvariantCulture, $"{DateTimeOffset.UtcNow:O} [{level}] {message}"); + Console.Error.WriteLine(line); + _writer.WriteLine(line); + _writer.Flush(); + } + } + + private StreamWriter Open() => new(new FileStream(_path, FileMode.Append, FileAccess.Write, FileShare.Read), new System.Text.UTF8Encoding(false)); + + private void Rotate() + { + _writer.Dispose(); + for (int index = _retainedFiles - 1; index >= 1; index--) + { + string source = $"{_path}.{index}"; + if (File.Exists(source)) File.Move(source, $"{_path}.{index + 1}", true); + } + if (File.Exists(_path)) File.Move(_path, $"{_path}.1", true); + _writer = Open(); + } + + public void Dispose() + { + lock (_sync) _writer.Dispose(); + } +} -- cgit v1.2.3