fix(prompts): stop on-disk prompt overrides from freezing forever

EnsureExists blindly seeded ~/.todo-app/prompts/*.md with the bundled
default and never revisited it, so any file created by opening the
Files settings tab shadowed every later default change permanently
(SuggestImprovement/AskUser sections never reached real runs since
2026-06-04). PromptFiles now hashes what a file was seeded/saved with
in prompts/.defaults.json: Classify() tells missing/current-default/
known-past-default/edited apart, ReconcileStaleDefaults() drops files
that only ever matched a superseded default, and real edits are left
alone and surfaced in the Files tab with a diff + reset-to-default
action. QuarantineOrphans() moves stale-named leftovers (agent.md,
planning.md) into prompts/_orphans instead of silently deleting them.
Wired as a Worker startup sweep (PromptFileRecovery) alongside the
existing OrphanRecovery/AttachmentOrphanRecovery services.
This commit is contained in:
mika kuns
2026-08-05 15:59:03 +02:00
parent 83ea429b8a
commit b153869216
11 changed files with 483 additions and 27 deletions
@@ -0,0 +1,49 @@
using System.Text.Json;
using ClaudeDo.Data;
using ClaudeDo.Worker.Lifecycle;
using Microsoft.Extensions.Logging.Abstractions;
namespace ClaudeDo.Worker.Tests.Lifecycle;
public sealed class PromptFileRecoveryTests
{
[Fact]
public async Task StartAsync_ReconcilesStaleDefaultAndQuarantinesOrphan_WithoutTouchingRealEdit()
{
var root = Path.Combine(Path.GetTempPath(), $"claudedo_prompts_{Guid.NewGuid():N}");
try
{
// A file that only ever matched a now-superseded default should get reconciled away.
Directory.CreateDirectory(root);
const string oldDefaultText = "This used to be the bundled retry default.";
File.WriteAllText(PromptFiles.PathFor(PromptKind.Retry, root), oldDefaultText);
var hashes = new Dictionary<string, string>
{
[PromptKind.Retry.ToString()] = PromptFiles.HashOf(PromptFiles.Normalize(oldDefaultText))
};
File.WriteAllText(Path.Combine(root, ".defaults.json"), JsonSerializer.Serialize(hashes));
// A genuinely edited file should survive untouched.
PromptFiles.Save(PromptKind.System, "My real customization.", root);
// A leftover file from a retired naming scheme should be quarantined, not deleted.
var orphanPath = Path.Combine(root, "agent.md");
File.WriteAllText(orphanPath, "leftover");
var sut = new PromptFileRecovery(NullLogger<PromptFileRecovery>.Instance, root);
await sut.StartAsync(CancellationToken.None);
Assert.False(File.Exists(PromptFiles.PathFor(PromptKind.Retry, root)), "Stale unedited default must be reconciled away");
Assert.True(File.Exists(PromptFiles.PathFor(PromptKind.System, root)), "Real edit must survive");
Assert.False(File.Exists(orphanPath), "Orphan must be moved out of the prompts root");
Assert.True(File.Exists(Path.Combine(root, "_orphans", "agent.md")), "Orphan must be quarantined, not deleted");
await sut.StopAsync(CancellationToken.None); // must not throw
}
finally
{
if (Directory.Exists(root)) Directory.Delete(root, recursive: true);
}
}
}