Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/officecli/BatchTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ public class BatchResult
public string? Code { get; set; }
/// <summary>The original batch item, included when the command fails so the agent can inspect/retry.</summary>
public BatchItem? Item { get; set; }
/// <summary>Advisory diagnostics produced while executing this item.</summary>
internal List<OfficeCli.Core.CliWarning>? Warnings { get; set; }
}

/// <summary>
Expand All @@ -244,6 +246,8 @@ internal class BatchResultConverter : JsonConverter<BatchResult>
if (root.TryGetProperty("error", out var err)) result.Error = err.GetString();
if (root.TryGetProperty("code", out var cod)) result.Code = cod.GetString();
if (root.TryGetProperty("item", out var itm)) result.Item = JsonSerializer.Deserialize(itm.GetRawText(), BatchJsonContext.Default.BatchItem);
if (root.TryGetProperty("warnings", out var wrn))
result.Warnings = JsonSerializer.Deserialize(wrn.GetRawText(), OfficeCli.Core.AppJsonContext.Default.ListCliWarning);
return result;
}

Expand Down Expand Up @@ -277,6 +281,11 @@ public override void Write(Utf8JsonWriter writer, BatchResult value, JsonSeriali
JsonSerializer.Serialize(writer, value.Item, BatchJsonContext.Default.BatchItem);
}
}
if (value.Warnings is { Count: > 0 })
{
writer.WritePropertyName("warnings");
JsonSerializer.Serialize(writer, value.Warnings, OfficeCli.Core.AppJsonContext.Default.ListCliWarning);
}
writer.WriteEndObject();
}

Expand Down
37 changes: 35 additions & 2 deletions src/officecli/CommandBuilder.Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,47 @@ internal static List<BatchResult> ApplyBatchItems(
continue;
}
}
// Batch dispatches straight to the shared handler methods, bypassing
// the standalone add/set wrappers that normally drain warnings into
// their output envelope. Capture a fresh warning scope per item so
// diagnostics retain the step that produced them.
OfficeCli.Core.WarningContext.Begin();
try
{
var output = ExecuteBatchItem(handler, item, json);
results.Add(new BatchResult { Index = bi, Success = true, Output = output });
var warnings = OfficeCli.Core.WarningContext.End();
if (handler is OfficeCli.Handlers.WordHandler word)
{
// Word advisories live on the handler rather than in
// WarningContext; standalone add/set merge these explicitly.
var advisory = string.Equals(item.Command, "add", StringComparison.OrdinalIgnoreCase)
? word.LastAddWarnings
: string.Equals(item.Command, "set", StringComparison.OrdinalIgnoreCase)
? word.LastSetWarnings
: null;
if (advisory is { Count: > 0 })
{
warnings ??= new List<OfficeCli.Core.CliWarning>();
warnings.AddRange(advisory.Select(message => new OfficeCli.Core.CliWarning
{
Message = message,
Code = "advisory",
}));
}
}
results.Add(new BatchResult { Index = bi, Success = true, Output = output, Warnings = warnings });
}
catch (Exception ex)
{
results.Add(new BatchResult { Index = bi, Success = false, Item = item, Error = ex.Message, Code = OfficeCli.Core.OutputFormatter.InferErrorCode(ex) });
results.Add(new BatchResult
{
Index = bi,
Success = false,
Item = item,
Error = ex.Message,
Code = OfficeCli.Core.OutputFormatter.InferErrorCode(ex),
Warnings = OfficeCli.Core.WarningContext.End(),
});
if (stopOnError) break;
}
// BUG-BT2: per-item unrecognized-LaTeX diagnostics. The handler
Expand Down
8 changes: 8 additions & 0 deletions src/officecli/CommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,11 @@ internal static void PrintBatchResults(List<BatchResult> results, bool json, int
System.Text.Json.JsonSerializer.Serialize(slimWriter, r.Item, BatchJsonContext.Default.BatchItem);
}
}
if (r.Warnings is { Count: > 0 })
{
slimWriter.WritePropertyName("warnings");
System.Text.Json.JsonSerializer.Serialize(slimWriter, r.Warnings, OfficeCli.Core.AppJsonContext.Default.ListCliWarning);
}
slimWriter.WriteEndObject();
}
slimWriter.WriteEndArray();
Expand Down Expand Up @@ -1452,6 +1457,9 @@ internal static void PrintBatchResults(List<BatchResult> results, bool json, int
{
@out.WriteLine($"{prefix}ERROR: {r.Error}");
}
if (r.Warnings is { Count: > 0 })
foreach (var warning in r.Warnings)
@out.WriteLine($" WARNING: {warning.Message}");
}

var succeeded = results.Count(r => r.Success);
Expand Down
2 changes: 1 addition & 1 deletion src/officecli/Core/OutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal class CliWarning
}

/// <summary>
/// Thread-static context for capturing warnings during command execution in JSON mode.
/// Thread-static context for capturing warnings during command execution.
/// </summary>
internal static class WarningContext
{
Expand Down