feat: found copies foundation (contracts, migration 0012, store, wire, trash and job hooks, keys)

Owner review 6 asks Chronos to recognise the same time-lapse held under different ids on the
server and on this PC, compare the copies, and keep them apart or integrate and sync them. This
lays the shared foundation the later parcels build on, without any UI or detection yet.

- Migration 0012 adds found_copies (local-server and local-local pairs with evidence, scores,
  answer, policy and the 404 record) and copy_probes; every existing link with a local sequence
  becomes a Linked pair, so a server deletion and the per-link policy have a place.
- FoundCopyStore keeps the pairs: upserts that never touch the answer, a compare-and-set of the
  state with an Integrating backstop (a failed or cancelled job reads as the previous state),
  link promotion and demotion, the once-per-refresh 404 count, the merge's links stage
  (session keys, links reset, pairs moved, refused when both are linked on one server), the
  local overlap queries and the probes. SyncLinkStore.RekeyAsync moves pairs and probes with
  the links in the same transaction.
- Contracts for detection, review, plans, the Integrate and MergeSequences jobs (kinds 25/26,
  High level, new detail steps) and the Upload all rule that keeps suspected, kept-apart,
  deleted, busy and server-born sequences here.
- Wire: POST photos/lookup, uploads into a target (IntoTarget/Copied, trailing and optional),
  frames/delete on the client, capabilities photo-lookup and upload-into, cached per remote.
- The trash plans and deletes accept the job that holds the sequence (its own hold is not
  Busy) and a frames version checked under the lease; a Background job never interrupts a
  running one.
- Settings for linking, the photo policy and projects; the en and it keys of design section 9.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-16 19:39:04 +01:00
1 parent 43719bb5c5
commit 283d04d9cd
57 files changed
+3302 -26

No files matched your search

@@ -1,5 +1,6 @@
using Chronos.Core.IO;
using Chronos.Core.Project;
using Chronos.Data.Copies;
using Chronos.Data.Homes;
using Chronos.Data.Projects;
using Chronos.Data.Sync;
@@ -202,6 +203,105 @@ public abstract class HomeRekeyLinkTests(TestContext context) : RepositoryTest(c
Assert.Count(1, await store.ListAsync(HomeFixtures.Other, Ct));
Assert.Empty(await store.ListAsync(HomeFixtures.Nas, Ct));
}
[Test]
public async Task TheMatchesAndProbesOfAnAliasMoveWithItsLinks()
{
var db = await OpenChronosAsync();
var (linked, found) = await TwoSequencesAsync(db);
var store = new SyncLinkStore(db);
var copies = new FoundCopyStore(db);
var (r1, r2) = (Guid.CreateVersion7(), Guid.CreateVersion7());
await store.UpsertAsync(Link(HomeFixtures.Nas, r1, linked, TestData.Now), Ct);
await copies.EnsureLinkedAsync(HomeFixtures.Nas, r1, linked, Ct);
await copies.UpsertFoundAsync(Candidate(HomeFixtures.Nas, r2, found), Ct);
await copies.SetProbeAsync(new CopyProbe(found, HomeFixtures.Nas, 3, 3, TestData.Now), Ct);
var migration = new HomeLayoutMigration(new HomeLayoutOptions { DataDirectory = Context.CreateTempDirectory("data"), Database = db });
var result = await migration.RekeyAsync(Id, [HomeFixtures.Nas, HomeFixtures.NasByIp], Ct);
Assert.Equal((1, 0), (result.LinksMoved, result.LinksDropped));
var key = ServerIdentity.IdKey(Id);
Assert.Empty(await copies.ListByServerAsync(HomeFixtures.Nas, Ct));
var moved = await copies.ListByServerAsync(key, Ct);
Assert.Count(2, moved);
Assert.Equal(CopyState.Linked, moved.Single(c => c.RemoteId == r1).State);
Assert.Equal(CopyState.Candidate, moved.Single(c => c.RemoteId == r2).State);
Assert.Null(await copies.GetProbeAsync(found, HomeFixtures.Nas, Ct));
Assert.Equal(3, (await copies.GetProbeAsync(found, key, Ct))!.FramesVersion);
}
[Test]
public async Task OnAClashThePairOfTheSurvivingLinkWinsElseTheHigherState()
{
var db = await OpenChronosAsync();
var (linked, found) = await TwoSequencesAsync(db);
var key = ServerIdentity.IdKey(Id);
var store = new SyncLinkStore(db);
var copies = new FoundCopyStore(db);
var (r1, r2) = (Guid.CreateVersion7(), Guid.CreateVersion7());
// The same link under both keys: the one compared last (the alias's) survives, and so does its pair, though the pair
// under the id is further on.
await store.UpsertAsync(Link(HomeFixtures.Nas, r1, linked, TestData.Now), Ct);
await store.UpsertAsync(Link(key, r1, linked, TestData.Now.AddHours(-1)), Ct);
var aliasPair = await copies.EnsureLinkedAsync(HomeFixtures.Nas, r1, linked, Ct);
var idPair = await copies.EnsureLinkedAsync(key, r1, linked, Ct);
Assert.True(await copies.SetStateAsync(idPair.Id, CopyState.Linked, CopyState.Integrated, new CopyAnswer(null, null, null, true, TestData.Now), Ct));
// A match without a link under both keys: the answered one wins over the newer candidate.
var keptApart = await copies.UpsertFoundAsync(Candidate(HomeFixtures.Nas, r2, found), Ct);
Assert.True(await copies.SetStateAsync(keptApart.Id, CopyState.Candidate, CopyState.KeptApart, new CopyAnswer("f", 4, null, null, TestData.Now), Ct));
var candidate = await copies.UpsertFoundAsync(Candidate(key, r2, found), Ct);
await copies.SetProbeAsync(new CopyProbe(found, HomeFixtures.Nas, 1, 1, TestData.Now), Ct);
await copies.SetProbeAsync(new CopyProbe(found, key, 2, 2, TestData.Now), Ct);
var (moved, dropped) = await store.RekeyAsync(HomeFixtures.Nas, key, Ct);
Assert.Equal((1, 1), (moved, dropped));
var rows = await copies.ListByServerAsync(key, Ct);
Assert.Count(2, rows);
var pair = rows.Single(c => c.RemoteId == r1);
Assert.Equal(aliasPair.Id, pair.Id);
Assert.Equal(CopyState.Linked, pair.State);
var match = rows.Single(c => c.RemoteId == r2);
Assert.Equal(keptApart.Id, match.Id);
Assert.NotEqual(candidate.Id, match.Id);
Assert.Equal(CopyState.KeptApart, match.State);
Assert.Empty(await copies.ListByServerAsync(HomeFixtures.Nas, Ct));
Assert.Equal(2, (await copies.GetProbeAsync(found, key, Ct))!.FramesVersion);
Assert.Null(await copies.GetProbeAsync(found, HomeFixtures.Nas, Ct));
}
private async Task<(Guid Linked, Guid Found)> TwoSequencesAsync(ChronosDatabase db)
{
var archive = TestData.Archive();
await db.Archives.UpsertAsync(archive, Ct);
var linked = TestData.Sequence(archive.Id);
var found = TestData.Sequence(archive.Id);
await db.Sequences.UpsertAsync(linked, Ct);
await db.Sequences.UpsertAsync(found, Ct);
return (linked.Id, found.Id);
}
private static SyncLink Link(string server, Guid remoteId, Guid localId, DateTimeOffset synced) => new()
{
Server = server,
RemoteId = remoteId,
LocalId = localId,
Title = "Harbour",
RemoteUpdatedAt = TestData.Now,
SyncedAt = synced,
};
private static FoundCopy Candidate(string server, Guid remoteId, Guid localId) => new()
{
Id = Guid.NewGuid(),
Kind = CopyKind.LocalServer,
LocalId = localId,
Server = server,
RemoteId = remoteId,
Evidence = CopyEvidence.Hashes,
Fingerprint = "f",
};
}
public sealed class SqliteHomeRekeyLinkTests(TestContext context) : HomeRekeyLinkTests(context)