brokkr-command: map every failure to exit 0/1/2, refuse a reused command id
- Any exception now exits 2 with a message. An empty or legacy store fails with a message that names the path instead of aborting with 134. - A --command-id that already has a receipt is refused with exit 2, so a stale receipt is never reported as the answer to a new intent. - The wait treats the editor's "attempted" marker as not yet a verdict. - The action theory passes --output so captureEditorView clears admission now that the harness runs the real policy. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
73f7926ad0
commit
b451cddf1d
2 changed files with 138 additions and 11 deletions
|
|
@ -6,6 +6,8 @@ using GameCult.Brokkr;
|
|||
using GameCult.Caching;
|
||||
using GameCult.Caching.MessagePack;
|
||||
|
||||
// Exit codes: 0 the receipt was accepted (or readHost / an unwaited write succeeded); 1 the receipt says the editor
|
||||
// did not accept it (failed, denied, expired, interrupted); 2 anything else: bad usage, a timeout, an unreadable store.
|
||||
try
|
||||
{
|
||||
return await Cli.RunAsync(args);
|
||||
|
|
@ -15,6 +17,11 @@ catch (Exception error) when (error is ArgumentException or TimeoutException or
|
|||
Console.Error.WriteLine(error.Message);
|
||||
return 2;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
Console.Error.WriteLine($"brokkr-command failed: {error.GetType().Name}: {error.Message}");
|
||||
return 2;
|
||||
}
|
||||
|
||||
internal static class Cli
|
||||
{
|
||||
|
|
@ -47,14 +54,30 @@ internal static class Cli
|
|||
var action = Required(options, "--action");
|
||||
var readHost = string.Equals(action, "readHost", StringComparison.Ordinal);
|
||||
|
||||
using var cache = await CultCacheMessagePack.OpenAsync(cachePath, new CultCacheOpenOptions
|
||||
{
|
||||
UseDirectoryStore = true,
|
||||
ReadOnly = readHost
|
||||
});
|
||||
using var cache = await OpenAsync(cachePath, readHost);
|
||||
return readHost ? await ReadHostAsync(cache) : await WriteIntentAsync(cache, action, options);
|
||||
}
|
||||
|
||||
// An empty file, a legacy single-file store or a damaged shard fails inside CultCache with whatever exception the
|
||||
// decoder throws; the caller gets one message that names the path.
|
||||
private static async Task<CultCache> OpenAsync(string cachePath, bool readOnly)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await CultCacheMessagePack.OpenAsync(cachePath, new CultCacheOpenOptions
|
||||
{
|
||||
UseDirectoryStore = true,
|
||||
ReadOnly = readOnly
|
||||
});
|
||||
}
|
||||
catch (Exception error) when (error is not ArgumentException)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Brokkr store '{cachePath}' is unreadable ({error.GetType().Name}: {error.Message}). It must be a directory store written by the Brokkr editor service.",
|
||||
error);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<int> ReadHostAsync(CultCache cache)
|
||||
{
|
||||
await cache.PullAllBackingStoresAsync();
|
||||
|
|
@ -82,13 +105,21 @@ internal static class Cli
|
|||
|
||||
private static async Task<int> WriteIntentAsync(CultCache cache, string action, Dictionary<string, string> options)
|
||||
{
|
||||
// A command id is single-use, so every invocation is a new act with a new id. --command-id exists for callers
|
||||
// that need to name their own; one that already has a receipt is refused, because that receipt answered an
|
||||
// earlier act and would be reported as the answer to this one.
|
||||
var named = options.TryGetValue("--command-id", out var commandId) && !string.IsNullOrWhiteSpace(commandId);
|
||||
if (named)
|
||||
{
|
||||
await cache.PullAllBackingStoresAsync();
|
||||
if (cache.TryGet(BrokkrCommandLedger.ReceiptKey(commandId!), out BrokkrUnityCommandReceipt? existing) && existing != null)
|
||||
throw new ArgumentException(
|
||||
$"Command id '{commandId}' already has a receipt ({existing.status}). Command ids are single-use; omit --command-id to get a fresh one.");
|
||||
}
|
||||
|
||||
var command = new BrokkrUnityCommand
|
||||
{
|
||||
// A command id is single-use, so every invocation is a new act with a new id. --command-id exists for
|
||||
// callers that need to name their own; reusing an answered id is a caller defect.
|
||||
commandId = options.TryGetValue("--command-id", out var commandId) && !string.IsNullOrWhiteSpace(commandId)
|
||||
? commandId
|
||||
: Guid.NewGuid().ToString("N"),
|
||||
commandId = named ? commandId! : Guid.NewGuid().ToString("N"),
|
||||
action = action
|
||||
};
|
||||
foreach (var (name, value) in options)
|
||||
|
|
@ -107,6 +138,7 @@ internal static class Cli
|
|||
|
||||
var receiptKey = BrokkrCommandLedger.ReceiptKey(command.commandId);
|
||||
var deadline = DateTimeOffset.UtcNow.AddMilliseconds(waitMilliseconds);
|
||||
var started = false;
|
||||
while (DateTimeOffset.UtcNow < deadline)
|
||||
{
|
||||
await Task.Delay(100);
|
||||
|
|
@ -114,6 +146,13 @@ internal static class Cli
|
|||
if (!cache.TryGet(receiptKey, out BrokkrUnityCommandReceipt? receipt) || receipt == null)
|
||||
continue;
|
||||
|
||||
// Attempted is the editor's marker that it has started; the verdict is still to come.
|
||||
if (string.Equals(receipt.status, BrokkrCommandDrain.AttemptedStatus, StringComparison.Ordinal))
|
||||
{
|
||||
started = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
Console.WriteLine($"{receipt.status}: {receipt.message}");
|
||||
if (!string.IsNullOrWhiteSpace(receipt.objectId))
|
||||
Console.WriteLine(receipt.objectId);
|
||||
|
|
@ -125,6 +164,8 @@ internal static class Cli
|
|||
var why = host is { agentCommandsEnabled: false }
|
||||
? " The editor reports agent commands off; the operator enables them in the Brokkr window."
|
||||
: "";
|
||||
if (started)
|
||||
why = " The editor started this intent and has not recorded a result; it will not run it again.";
|
||||
throw new TimeoutException(
|
||||
$"Timed out waiting for Brokkr receipt '{receiptKey.Value}'; the intent stays written.{why}");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -202,7 +202,7 @@ public sealed class BrokkrCommandCliTests
|
|||
}
|
||||
});
|
||||
|
||||
var result = await RunAsync("--unity-cache", project.StorePath, "--action", action, "--wait-ms", "20000");
|
||||
var result = await RunAsync("--unity-cache", project.StorePath, "--action", action, "--output", "probe.png", "--wait-ms", "20000");
|
||||
await Task.Delay(200);
|
||||
stop.Cancel();
|
||||
await loop;
|
||||
|
|
@ -214,4 +214,90 @@ public sealed class BrokkrCommandCliTests
|
|||
var receipt = Assert.Single(audit.AllStoredDocuments.Select(entry => entry.Document).OfType<BrokkrUnityCommandReceipt>());
|
||||
Assert.Equal(executed.commandId, receipt.commandId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReusedCommandIdIsRefusedAndNothingIsWritten()
|
||||
{
|
||||
using var project = new ScratchProject();
|
||||
await project.WriteCommandAsync("used-once", "refreshAssets");
|
||||
using (var editor = project.OpenCache())
|
||||
await BrokkrCommandLedgerTests.Answer(editor, "used-once");
|
||||
|
||||
var result = await RunAsync(
|
||||
"--unity-cache", project.StorePath, "--action", "setEditorPaused", "--command-id", "used-once", "--wait-ms", "2000");
|
||||
|
||||
Assert.Equal(2, result.Exit);
|
||||
Assert.Contains("already has a receipt", result.Error);
|
||||
var intent = Assert.Single(StoredIntents(project));
|
||||
Assert.Equal("refreshAssets", intent.action);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("empty")]
|
||||
[InlineData("legacy")]
|
||||
public async Task UnreadableStoreExitsTwoWithAMessageThatNamesIt(string kind)
|
||||
{
|
||||
using var project = new ScratchProject();
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(project.StorePath)!);
|
||||
// An empty file, or the single-file store the directory store replaced.
|
||||
File.WriteAllBytes(project.StorePath, kind == "empty" ? Array.Empty<byte>() : new byte[] { 0x93, 0x01, 0x02 });
|
||||
|
||||
var write = await RunAsync("--unity-cache", project.StorePath, "--action", "refreshAssets", "--wait-ms", "0");
|
||||
var read = await RunAsync("--unity-cache", project.StorePath, "--action", "readHost");
|
||||
|
||||
Assert.Equal(2, write.Exit);
|
||||
Assert.Contains("unreadable", write.Error);
|
||||
Assert.Contains(project.StorePath, write.Error);
|
||||
Assert.Equal(2, read.Exit);
|
||||
Assert.Contains("unreadable", read.Error);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WaitContinuesPastTheAttemptMarkerToTheVerdict()
|
||||
{
|
||||
using var project = new ScratchProject();
|
||||
using var editor = await EditorProbe.StartAsync(project.StorePath);
|
||||
editor.OnExecute = _ => Thread.Sleep(1500);
|
||||
using var stop = new CancellationTokenSource();
|
||||
var loop = Task.Run(async () =>
|
||||
{
|
||||
while (!stop.IsCancellationRequested)
|
||||
{
|
||||
await editor.TickAsync();
|
||||
await Task.Delay(50);
|
||||
}
|
||||
});
|
||||
|
||||
var result = await RunAsync("--unity-cache", project.StorePath, "--action", "refreshAssets", "--wait-ms", "20000");
|
||||
stop.Cancel();
|
||||
await loop;
|
||||
|
||||
Assert.Equal(0, result.Exit);
|
||||
Assert.StartsWith("accepted:", result.Out);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InterruptedIntentIsReportedAsANonAcceptanceNotAHang()
|
||||
{
|
||||
using var project = new ScratchProject();
|
||||
using var editor = await EditorProbe.StartAsync(project.StorePath);
|
||||
editor.OnExecute = _ => throw new InvalidOperationException("the editor died");
|
||||
using var stop = new CancellationTokenSource();
|
||||
var loop = Task.Run(async () =>
|
||||
{
|
||||
while (!stop.IsCancellationRequested)
|
||||
{
|
||||
try { await editor.TickAsync(); } catch (InvalidOperationException) { }
|
||||
await Task.Delay(50);
|
||||
}
|
||||
});
|
||||
|
||||
var result = await RunAsync("--unity-cache", project.StorePath, "--action", "refreshAssets", "--wait-ms", "20000");
|
||||
stop.Cancel();
|
||||
await loop;
|
||||
|
||||
Assert.Equal(1, result.Exit);
|
||||
Assert.StartsWith("interrupted:", result.Out);
|
||||
Assert.Single(editor.Executed);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue