67 lines
3.3 KiB
C#
67 lines
3.3 KiB
C#
using System.Diagnostics;
|
|
using System.Text;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace Strata.Desktop;
|
|
|
|
internal static class CoreClient
|
|
{
|
|
public static async Task<JsonObject> Run(CancellationToken cancellationToken, params string[] args)
|
|
{
|
|
var executable = Environment.GetEnvironmentVariable("STRATA_DESKTOP_CORE")
|
|
?? Path.Combine(AppContext.BaseDirectory, OperatingSystem.IsWindows() ? "strata-desktop.exe" : "strata-desktop");
|
|
if (!Path.IsPathFullyQualified(executable) || !File.Exists(executable))
|
|
throw new InvalidOperationException("Rust core не знайдено. Зберіть desktop-core або вкажіть абсолютний STRATA_DESKTOP_CORE.");
|
|
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
|
timeout.CancelAfter(TimeSpan.FromSeconds(12));
|
|
using var process = new Process { StartInfo = new ProcessStartInfo(executable) {
|
|
UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true
|
|
}};
|
|
foreach (var arg in args) process.StartInfo.ArgumentList.Add(arg);
|
|
process.Start();
|
|
try
|
|
{
|
|
var output = ReadBounded(process.StandardOutput, timeout.Token);
|
|
var error = ReadBounded(process.StandardError, timeout.Token);
|
|
await process.WaitForExitAsync(timeout.Token);
|
|
var text = await output;
|
|
var diagnostic = await error;
|
|
if (process.ExitCode != 0) throw new InvalidOperationException($"Core: {diagnostic.Trim()}");
|
|
var result = JsonNode.Parse(text) as JsonObject ?? throw new InvalidOperationException("Некоректна відповідь core.");
|
|
if (result["schema_version"]?.GetValue<int>() != 1)
|
|
throw new InvalidOperationException("Несумісна версія контракту core.");
|
|
return result;
|
|
}
|
|
catch
|
|
{
|
|
if (!process.HasExited) process.Kill(entireProcessTree: true);
|
|
await process.WaitForExitAsync(CancellationToken.None);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private static async Task<string> ReadBounded(StreamReader reader, CancellationToken token)
|
|
{
|
|
var buffer = new char[4096];
|
|
var result = new StringBuilder();
|
|
while (true)
|
|
{
|
|
var count = await reader.ReadAsync(buffer.AsMemory(), token);
|
|
if (count == 0) return result.ToString();
|
|
if (result.Length + count > 1024 * 1024) throw new InvalidOperationException("Відповідь core завелика.");
|
|
result.Append(buffer, 0, count);
|
|
}
|
|
}
|
|
|
|
public static async Task SelfCheck()
|
|
{
|
|
var h = await Run(CancellationToken.None, "hardware");
|
|
if (h["memory"] is not JsonObject || h["accelerators"] is not JsonArray)
|
|
throw new InvalidOperationException("Missing hardware contract fields.");
|
|
var p = await Run(CancellationToken.None, "plan", Path.Combine(AppContext.BaseDirectory, "example-requirements.json"));
|
|
if (p["evidence"]?.GetValue<string>() != "estimated" || p["candidates"] is not JsonArray { Count: > 0 })
|
|
throw new InvalidOperationException("Missing planning contract fields.");
|
|
Console.WriteLine("PASS: desktop → Rust hardware/plan contract; estimates only, no inference.");
|
|
}
|
|
}
|