diff --git a/PlotLine.Tests/NativeReviewNavigationTests.cs b/PlotLine.Tests/NativeReviewNavigationTests.cs index 9e7f632c..c73c4efe 100644 --- a/PlotLine.Tests/NativeReviewNavigationTests.cs +++ b/PlotLine.Tests/NativeReviewNavigationTests.cs @@ -29,7 +29,7 @@ internal static class NativeReviewNavigationTests var protection = new EphemeralDataProtectionProvider(); var protector = protection.CreateProtector("NativeImport.Source.v1"); var clock = new Clock(); - foreach(var scenario in new[]{"valid", "failed ack", "timeout ack", "new session", "equal revision reopen", "changed prefix reopen", "copied properties", "missing session", "missing revision", "ambiguous session", "missing source", "source session mismatch", "changed content", "changed revision", "unsaved", "wrong manuscript", "wrong book", "wrong author", "wrong instance", "expired", "replaced", "invalid offset"}) + foreach(var scenario in new[]{"valid", "persisted source-only", "failed ack", "timeout ack", "new session", "equal revision reopen", "changed prefix reopen", "copied properties", "missing session", "missing revision", "ambiguous session", "missing source", "source session mismatch", "changed content", "changed revision", "unsaved", "wrong manuscript", "wrong book", "wrong author", "wrong instance", "expired", "replaced", "invalid offset"}) { var store = new NativePresenceStore(clock); var instance = Guid.NewGuid(); var session = Guid.NewGuid(); var manuscript = Guid.NewGuid(); @@ -41,8 +41,9 @@ internal static class NativeReviewNavigationTests if(scenario is "new session" or "equal revision reopen" or "changed prefix reopen" or "copied properties")liveSession=Guid.NewGuid(); if(scenario=="new session")liveRevision=0; if(scenario=="changed prefix reopen")capture.Paragraphs[0].Text="Changed preceding text\r"; - if(scenario=="missing session")row.SourceSession=null; - if(scenario=="missing revision")row.SourceRevision=null; + if(scenario=="missing session"){row.SourceSession=null;capture.SessionId=Guid.Empty;} + if(scenario=="missing revision"){row.SourceRevision=null;capture.Revision=-1;} + if(scenario=="persisted source-only"){row.SourceSession=null;row.SourceRevision=null;} if(scenario=="source session mismatch")capture.SessionId=Guid.NewGuid(); if(scenario=="changed content")capture.Paragraphs[0].Text="Different content\r"; // Retained content no longer agrees with the stored hash. if(scenario=="changed revision")liveRevision++; @@ -64,7 +65,7 @@ internal static class NativeReviewNavigationTests var http=new DefaultHttpContext{User=new ClaimsPrincipal(new ClaimsIdentity([new Claim(ClaimTypes.NameIdentifier,scenario=="wrong author"?"8":"7")],"fixture"))}; var api=new NativeWorkspaceController(access,books,reviews,store,new NativeReviewNavigation(protection)){ControllerContext=new(){HttpContext=http},TempData=new TempDataDictionary(http,new TempData())}; var response=await api.Navigate(row.ReviewID,0,scenario=="wrong instance"?Guid.NewGuid():instance,liveSession); - var valid=scenario is "valid" or "failed ack" or "timeout ack"; + var valid=scenario is "valid" or "persisted source-only" or "failed ack" or "timeout ack"; Check(valid?response is RedirectToActionResult:response is ConflictObjectResult or NotFoundResult,scenario+": navigation outcome"); var command=store.TakeNavigation(7,"grant",instance,liveSession); Check(valid?command?.Chapter?.Start==10:command==null,scenario+": unsafe offsets issue no native command/range selection"); diff --git a/PlotLine/Services/NativeCompanion/NativeReviewNavigation.cs b/PlotLine/Services/NativeCompanion/NativeReviewNavigation.cs index dd5e12f2..87bc7271 100644 --- a/PlotLine/Services/NativeCompanion/NativeReviewNavigation.cs +++ b/PlotLine/Services/NativeCompanion/NativeReviewNavigation.cs @@ -6,40 +6,50 @@ using PlotLine.Data; namespace PlotLine.Services.NativeCompanion; -// Historical offsets are usable only with authenticated source content and its original live session. -// Word revisions are local to an open session; a reopened/copy document cannot inherit this proof. +// Historical offsets require authenticated captured content and its original live session. +// SourceSession/SourceRevision are optional model fields, not columns returned by the review procedure. public sealed class NativeReviewNavigation(IDataProtectionProvider protection) { - public const string Unavailable = "The original Word position can no longer be verified. Review decisions remain available; resubmit the chapter from Word to establish a current position."; + public const string Unavailable = "The original Word position can no longer be verified. Review decisions remain available. Locate this passage directly in Word."; private readonly IDataProtector source = protection.CreateProtector("NativeImport.Source.v1"); public NativeLease[] Candidates(int author, NativeReview row, AiChapterResult result, IEnumerable leases) { - var matching = leases.Where(l => l.Session == row.SourceSession).Take(2).ToArray(); - return matching.Length == 1 && Current(author, row, result, matching[0]) ? matching : []; + var capture = Source(author, row, result); + if (capture == null) return []; + var matching = leases.Where(l => l.Session == capture.SessionId).Take(2).ToArray(); + return matching.Length == 1 && Live(author, row, capture, matching[0]) ? matching : []; } public bool Current(int author, NativeReview row, AiChapterResult result, NativeLease lease) + => Source(author, row, result) is {} capture && Live(author, row, capture, lease); + + private static bool Live(int author, NativeReview row, AiChapterRequest capture, NativeLease lease) + => lease.User == author && lease.Instance != Guid.Empty && lease.Session == capture.SessionId && + lease.Position.Revision == capture.Revision && lease.Position.BookId == row.BookID && + lease.Position.ManuscriptId == row.ManuscriptID && lease.Position.WordAvailable && + lease.Position.WordSaved == true && lease.Position.CanAcknowledgeNavigation; + + private AiChapterRequest? Source(int author, NativeReview row, AiChapterResult result) { - if (result.Scenes == null || result.Scenes.Length == 0 || row.BookID <= 0 || row.UserID != author || lease.User != author || lease.Instance == Guid.Empty || - row.SourceSession is not Guid session || session == Guid.Empty || session != lease.Session || - row.SourceRevision is not long revision || revision < 0 || revision != result.DocumentRevision || - lease.Position.Revision != revision || lease.Position.BookId != row.BookID || - lease.Position.ManuscriptId != row.ManuscriptID || row.ManuscriptID == Guid.Empty || - !lease.Position.WordAvailable || lease.Position.WordSaved != true || !lease.Position.CanAcknowledgeNavigation || - string.IsNullOrEmpty(row.EncryptedSource)) return false; + if (row.UserID != author || row.BookID <= 0 || row.ManuscriptID == Guid.Empty || + result.Scenes == null || result.Scenes.Length == 0 || string.IsNullOrEmpty(row.EncryptedSource)) return null; try { var capture = JsonSerializer.Deserialize(source.Unprotect(row.EncryptedSource)); - if (capture == null || capture.SessionId != session || capture.Revision != revision || + if (capture == null || capture.SessionId == Guid.Empty || capture.Revision < 0 || + capture.Revision != result.DocumentRevision || + row.SourceSession.HasValue && row.SourceSession != capture.SessionId || + row.SourceRevision.HasValue && row.SourceRevision != capture.Revision || capture.BookId != row.BookID || capture.ManuscriptId != row.ManuscriptID || - capture.ChapterStart != row.ChapterStart || capture.ChapterEnd != row.ChapterEnd) return false; - NativeChapterMapping.Build(capture); + capture.ChapterStart != row.ChapterStart || capture.ChapterEnd != row.ChapterEnd) return null; + // Use the existing native API upper bound; a retained valid capture may exceed its default analysis size. + NativeChapterMapping.Build(capture,500000); var hash = Convert.ToHexString(SHA256.HashData(JsonSerializer.SerializeToUtf8Bytes(new { capture.ChapterStart, capture.ChapterEnd, capture.Paragraphs }))); - return hash == row.ContentHash && result.Scenes.All(scene => + return hash == row.ContentHash && result.Scenes.All(scene => scene != null && scene.Start >= capture.ChapterStart && scene.End <= capture.ChapterEnd && scene.End > scene.Start && - capture.Paragraphs.Any(p => p.Start == scene.Start) && capture.Paragraphs.Any(p => p.End == scene.End)); + capture.Paragraphs.Any(p => p.Start == scene.Start) && capture.Paragraphs.Any(p => p.End == scene.End)) ? capture : null; } - catch (Exception e) when (e is CryptographicException or JsonException or ArgumentException) { return false; } + catch (Exception e) when (e is CryptographicException or JsonException or ArgumentException) { return null; } } }