task_runs.tokens_in only ever held the API's uncached "input_tokens" field (off by a factor of ~400,000 on a resumed session), and tokens_out summed only the last result event instead of the whole session. TaskRunner now reads each run's cache-read/cache-write/input/output totals from the session transcript via a new ITranscriptUsageReader.ReadSessionTotalsAsync, storing the delta against prior runs on the same session so a --resume doesn't double-count. New task_runs.cache_read_tokens/cache_write_tokens columns; the Session tab now shows the raw total (what actually counts against the 5h/7d usage limit) with a breakdown tooltip.
100 lines
4.1 KiB
C#
100 lines
4.1 KiB
C#
using ClaudeDo.Data;
|
|
using ClaudeDo.Data.Models;
|
|
using ClaudeDo.Data.Repositories;
|
|
using ClaudeDo.Worker.Config;
|
|
using ClaudeDo.Worker.Hub;
|
|
using ClaudeDo.Worker.Runner;
|
|
using ClaudeDo.Worker.Tests.Infrastructure;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
|
|
|
|
namespace ClaudeDo.Worker.Tests.Runner;
|
|
|
|
/// Verifies that RunAsync and ContinueAsync abort cleanly when StartRunningAsync fails
|
|
/// (e.g. the task is already Running due to a concurrent RunNow / QueuePicker race).
|
|
public sealed class StartRunningGuardTests : IDisposable
|
|
{
|
|
private readonly DbFixture _db = new();
|
|
private readonly string _tempDir;
|
|
private readonly WorkerConfig _cfg;
|
|
|
|
public StartRunningGuardTests()
|
|
{
|
|
_tempDir = Path.Combine(Path.GetTempPath(), $"cd_guard_{Guid.NewGuid():N}");
|
|
Directory.CreateDirectory(_tempDir);
|
|
_cfg = new WorkerConfig { SandboxRoot = _tempDir, LogRoot = _tempDir };
|
|
}
|
|
|
|
public void Dispose() { _db.Dispose(); try { Directory.Delete(_tempDir, true); } catch { } }
|
|
|
|
private TaskRunner BuildRunner(IClaudeProcess claude)
|
|
{
|
|
var dbFactory = _db.CreateFactory();
|
|
var state = TaskStateServiceBuilder.Build(dbFactory).State;
|
|
var wt = new WorktreeManager(new ClaudeDo.Data.Git.GitService(), dbFactory, _cfg, NullLogger<WorktreeManager>.Instance);
|
|
return new TaskRunner(claude, dbFactory, new HubBroadcaster(new CapturingHubContext()), wt,
|
|
new ClaudeArgsBuilder(), _cfg, NullLogger<TaskRunner>.Instance, state, new TaskRunTokenRegistry(), new AttachmentStore(), new FakeSessionSkillSeeder(), new FakeTranscriptUsageReader());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RunAsync_TaskAlreadyRunning_NoProcessStarted_NoRunRecord()
|
|
{
|
|
string listId = Guid.NewGuid().ToString(), taskId = Guid.NewGuid().ToString();
|
|
using (var ctx = _db.CreateContext())
|
|
{
|
|
ctx.Lists.Add(new ListEntity { Id = listId, Name = "L", WorkingDir = null, CreatedAt = DateTime.UtcNow });
|
|
ctx.Tasks.Add(new TaskEntity
|
|
{
|
|
Id = taskId, ListId = listId, Title = "Already running",
|
|
Status = TaskStatus.Running, CreatedAt = DateTime.UtcNow,
|
|
});
|
|
await ctx.SaveChangesAsync();
|
|
}
|
|
|
|
var fake = new FakeClaudeProcess();
|
|
var runner = BuildRunner(fake);
|
|
|
|
using (var ctx = _db.CreateContext())
|
|
await runner.RunAsync((await new TaskRepository(ctx).GetByIdAsync(taskId))!, "slot-1", CancellationToken.None);
|
|
|
|
Assert.Equal(0, fake.CallCount);
|
|
|
|
using var verify = _db.CreateContext();
|
|
Assert.Empty(await new TaskRunRepository(verify).GetByTaskIdAsync(taskId));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ContinueAsync_TaskAlreadyRunning_NoProcessStarted_NoNewRunRecord()
|
|
{
|
|
string listId = Guid.NewGuid().ToString(), taskId = Guid.NewGuid().ToString();
|
|
using (var ctx = _db.CreateContext())
|
|
{
|
|
ctx.Lists.Add(new ListEntity { Id = listId, Name = "L", WorkingDir = null, CreatedAt = DateTime.UtcNow });
|
|
ctx.Tasks.Add(new TaskEntity
|
|
{
|
|
Id = taskId, ListId = listId, Title = "Already running",
|
|
Status = TaskStatus.Running, CreatedAt = DateTime.UtcNow,
|
|
});
|
|
await ctx.SaveChangesAsync();
|
|
|
|
await new TaskRunRepository(ctx).AddAsync(new TaskRunEntity
|
|
{
|
|
Id = Guid.NewGuid().ToString(), TaskId = taskId, RunNumber = 1,
|
|
IsRetry = false, Prompt = "p", SessionId = "sess-guard-test",
|
|
StartedAt = DateTime.UtcNow.AddMinutes(-5), FinishedAt = DateTime.UtcNow.AddMinutes(-1),
|
|
ExitCode = 0, ResultMarkdown = "ok",
|
|
});
|
|
}
|
|
|
|
var fake = new FakeClaudeProcess();
|
|
var runner = BuildRunner(fake);
|
|
|
|
await runner.ContinueAsync(taskId, "follow up", "slot-1", CancellationToken.None);
|
|
|
|
Assert.Equal(0, fake.CallCount);
|
|
|
|
using var verify = _db.CreateContext();
|
|
Assert.Single(await new TaskRunRepository(verify).GetByTaskIdAsync(taskId));
|
|
}
|
|
}
|