feat(attachments): inject reference files into the run + clean up files on delete
TaskRunner appends attached files (absolute paths) to the run prompt as the read-only Reference files section. Task and list deletes now remove the on-disk attachment dir eagerly, and a startup AttachmentOrphanRecovery sweep drops any attachments/<taskId>/ whose task no longer exists (covers list cascade and planning-discard paths).
This commit is contained in:
51
src/ClaudeDo.Worker/Lifecycle/AttachmentOrphanRecovery.cs
Normal file
51
src/ClaudeDo.Worker/Lifecycle/AttachmentOrphanRecovery.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using ClaudeDo.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ClaudeDo.Worker.Lifecycle;
|
||||
|
||||
/// <summary>
|
||||
/// Startup-only sweep: deletes attachment directories whose task no longer exists in the DB.
|
||||
/// </summary>
|
||||
public sealed class AttachmentOrphanRecovery : IHostedService
|
||||
{
|
||||
private readonly IDbContextFactory<ClaudeDoDbContext> _dbFactory;
|
||||
private readonly AttachmentStore _store;
|
||||
private readonly ILogger<AttachmentOrphanRecovery> _logger;
|
||||
|
||||
public AttachmentOrphanRecovery(
|
||||
IDbContextFactory<ClaudeDoDbContext> dbFactory,
|
||||
AttachmentStore store,
|
||||
ILogger<AttachmentOrphanRecovery> logger)
|
||||
{
|
||||
_dbFactory = dbFactory;
|
||||
_store = store;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var taskIds = _store.EnumerateTaskIds();
|
||||
if (taskIds.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("Attachment orphan recovery: no attachment directories found");
|
||||
return;
|
||||
}
|
||||
|
||||
await using var ctx = await _dbFactory.CreateDbContextAsync(cancellationToken);
|
||||
var existingIds = (await ctx.Tasks
|
||||
.Where(t => taskIds.Contains(t.Id))
|
||||
.Select(t => t.Id)
|
||||
.ToListAsync(cancellationToken)).ToHashSet();
|
||||
|
||||
var orphans = taskIds.Where(id => !existingIds.Contains(id)).ToList();
|
||||
foreach (var id in orphans)
|
||||
_store.DeleteTaskDir(id);
|
||||
|
||||
if (orphans.Count > 0)
|
||||
_logger.LogWarning("Attachment orphan recovery: removed {Count} orphaned attachment director(ies)", orphans.Count);
|
||||
else
|
||||
_logger.LogInformation("Attachment orphan recovery: no orphaned attachment directories found");
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
@@ -46,8 +46,10 @@ builder.Services.AddDbContextFactory<ClaudeDoDbContext>(opt =>
|
||||
opt.UseSqlite($"Data Source={cfg.DbPath}"));
|
||||
|
||||
builder.Services.AddSingleton(cfg);
|
||||
builder.Services.AddSingleton<AttachmentStore>();
|
||||
builder.Services.AddHostedService<StaleTaskRecovery>();
|
||||
builder.Services.AddHostedService<OrphanRecovery>();
|
||||
builder.Services.AddHostedService<AttachmentOrphanRecovery>();
|
||||
builder.Services.AddSignalR().AddJsonProtocol(options =>
|
||||
{
|
||||
options.PayloadSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
|
||||
|
||||
@@ -21,6 +21,7 @@ public sealed class TaskRunner
|
||||
private readonly ILogger<TaskRunner> _logger;
|
||||
private readonly ITaskStateService _state;
|
||||
private readonly TaskRunTokenRegistry _tokens;
|
||||
private readonly AttachmentStore _attachments;
|
||||
|
||||
public TaskRunner(
|
||||
IClaudeProcess claude,
|
||||
@@ -31,7 +32,8 @@ public sealed class TaskRunner
|
||||
WorkerConfig cfg,
|
||||
ILogger<TaskRunner> logger,
|
||||
ITaskStateService state,
|
||||
TaskRunTokenRegistry tokens)
|
||||
TaskRunTokenRegistry tokens,
|
||||
AttachmentStore attachments)
|
||||
{
|
||||
_claude = claude;
|
||||
_dbFactory = dbFactory;
|
||||
@@ -42,6 +44,7 @@ public sealed class TaskRunner
|
||||
_logger = logger;
|
||||
_state = state;
|
||||
_tokens = tokens;
|
||||
_attachments = attachments;
|
||||
}
|
||||
|
||||
public async Task RunAsync(TaskEntity task, string slot, CancellationToken ct, bool alreadyClaimed = false)
|
||||
@@ -54,6 +57,7 @@ public sealed class TaskRunner
|
||||
ListConfigEntity? listConfig;
|
||||
List<SubtaskEntity> subtasks;
|
||||
|
||||
List<string>? attachmentPaths = null;
|
||||
using (var context = _dbFactory.CreateDbContext())
|
||||
{
|
||||
var listRepo = new ListRepository(context);
|
||||
@@ -67,6 +71,11 @@ public sealed class TaskRunner
|
||||
|
||||
var subtaskRepo = new SubtaskRepository(context);
|
||||
subtasks = await subtaskRepo.GetByTaskIdAsync(task.Id, ct);
|
||||
|
||||
var attachmentRepo = new TaskAttachmentRepository(context);
|
||||
var attachments = await attachmentRepo.ListByTaskIdAsync(task.Id, ct);
|
||||
if (attachments.Count > 0)
|
||||
attachmentPaths = attachments.Select(a => Path.Combine(_attachments.TaskDir(task.Id), a.FileName)).ToList();
|
||||
}
|
||||
|
||||
// Determine working directory: worktree or sandbox.
|
||||
@@ -114,7 +123,8 @@ public sealed class TaskRunner
|
||||
// Build prompt: title + description + only the OPEN sub-tasks (resolved ones are dropped).
|
||||
var prompt = TaskPromptComposer.Compose(
|
||||
task.Title, task.Description,
|
||||
subtasks.Select(s => (s.Title, s.Completed)));
|
||||
subtasks.Select(s => (s.Title, s.Completed)),
|
||||
attachmentPaths);
|
||||
|
||||
// Run 1.
|
||||
var result = await RunOnceAsync(task.Id, task.Title, slot, runDir, resolvedConfig, 1, false, prompt, ct);
|
||||
|
||||
Reference in New Issue
Block a user