Skip to content
Merged
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
28 changes: 17 additions & 11 deletions dotnet/Codexcw/AccountUsage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public static async Task<AccountUsage> GetAccountUsageAsync(
}

var stderr = new TailBuffer(Runner.DefaultStderrLimit);
ProcessException failure;
var stderrPump = stderr.PumpAsync(process.StandardError.BaseStream, CancellationToken.None);
try
{
Expand All @@ -231,11 +232,11 @@ public static async Task<AccountUsage> GetAccountUsageAsync(
},
};
await RequiredRequest(client, "initialize", initParams, InitTimeout,
"initialize codex app-server", stderr, cancellationToken).ConfigureAwait(false);
"initialize codex app-server", cancellationToken).ConfigureAwait(false);
await client.NotifyAsync("initialized", new JsonObject(), cancellationToken).ConfigureAwait(false);

var rawRateLimits = await RequiredRequest(client, "account/rateLimits/read", null, requestTimeout,
"read codex account rate limits", stderr, cancellationToken).ConfigureAwait(false);
"read codex account rate limits", cancellationToken).ConfigureAwait(false);
AccountRateLimits rateLimits;
IReadOnlyDictionary<string, AccountRateLimits> byLimitId;
try
Expand All @@ -260,7 +261,7 @@ await RequiredRequest(client, "initialize", initParams, InitTimeout,
try
{
var rawTokenUsage = await Request(client, "account/usage/read", null, requestTimeout,
"read codex account token usage", stderr, cancellationToken).ConfigureAwait(false);
"read codex account token usage", cancellationToken).ConfigureAwait(false);
try
{
usage = usage with
Expand All @@ -281,7 +282,7 @@ await RequiredRequest(client, "initialize", initParams, InitTimeout,
try
{
var rawAccount = await Request(client, "account/read", new JsonObject(), requestTimeout,
"read codex account", stderr, cancellationToken).ConfigureAwait(false);
"read codex account", cancellationToken).ConfigureAwait(false);
try
{
usage = usage with
Expand All @@ -301,6 +302,10 @@ await RequiredRequest(client, "initialize", initParams, InitTimeout,

return usage;
}
catch (ProcessException ex)
{
failure = ex;
}
finally
{
Session.KillProcessTree(process);
Expand All @@ -314,13 +319,16 @@ await process.WaitForExitAsync(CancellationToken.None)
}
try
{
process.StandardError.BaseStream.Dispose();
await stderrPump.WaitAsync(TimeSpan.FromSeconds(1), CancellationToken.None).ConfigureAwait(false);
}
catch (IOException)
catch (TimeoutException)
{
process.StandardError.BaseStream.Dispose();
}
await stderrPump.ConfigureAwait(false);
}
// Format the failure after teardown so the stderr reader has consumed the final bytes.
throw new ProcessException(failure.Message + StderrSuffix(stderr), failure.InnerException ?? failure);
}

private static async Task<string> RequiredRequest(
Expand All @@ -329,17 +337,16 @@ private static async Task<string> RequiredRequest(
JsonObject? parameters,
TimeSpan timeout,
string context,
TailBuffer stderr,
CancellationToken cancellationToken)
{
try
{
return await Request(client, method, parameters, timeout, context, stderr, cancellationToken)
return await Request(client, method, parameters, timeout, context, cancellationToken)
.ConfigureAwait(false);
}
catch (JsonRpcErrorException ex)
{
throw new ProcessException($"{context}: {ex.Message}{StderrSuffix(stderr)}", ex);
throw new ProcessException($"{context}: {ex.Message}", ex);
}
}

Expand All @@ -349,7 +356,6 @@ private static async Task<string> Request(
JsonObject? parameters,
TimeSpan timeout,
string context,
TailBuffer stderr,
CancellationToken cancellationToken)
{
try
Expand All @@ -366,7 +372,7 @@ private static async Task<string> Request(
}
catch (Exception ex)
{
throw new ProcessException($"{context}: {ex.Message}{StderrSuffix(stderr)}", ex);
throw new ProcessException($"{context}: {ex.Message}", ex);
}
}

Expand Down