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).
52 lines
1.8 KiB
C#
52 lines
1.8 KiB
C#
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;
|
|
}
|