namespace w4c_workflows.Services.Execution;
///
/// The result of executing one task. is always valid JSON
/// (a serialized value) or null when the task produced no output; the
/// control plane stores it directly into task_runs.output jsonb.
///
public sealed record ExecutionResult(
bool Success,
int ExitCode,
string Stdout,
string Stderr,
string? Output,
string? Error,
TimeSpan Duration);
///
/// Serializes an into the flat task.result
/// event fields the worker publishes back to the control plane.
///
public static class TaskResultMessage
{
public const string TypeValue = "task.result";
public static IReadOnlyDictionary ToFields(TaskInvocation invocation, ExecutionResult result)
{
var fields = new Dictionary
{
["type"] = TypeValue,
["run_id"] = invocation.RunId,
["task_id"] = invocation.TaskId,
["attempt"] = invocation.Attempt.ToString(System.Globalization.CultureInfo.InvariantCulture),
["tenant_id"] = invocation.TenantId,
["status"] = result.Success ? "succeeded" : "failed",
["exit_code"] = result.ExitCode.ToString(System.Globalization.CultureInfo.InvariantCulture),
["duration_ms"] = ((long)result.Duration.TotalMilliseconds).ToString(System.Globalization.CultureInfo.InvariantCulture),
};
if (result.Output != null)
fields["output"] = result.Output;
if (result.Error != null)
fields["error"] = result.Error;
return fields;
}
}