Files
Chronos/src/Titano.Core/Advice/ParameterBindings.cs
T
Claude ea6e1a69e0 fix(sync): keep project positions on their photos when settings cross copies
Settings synced between a PC and its server sequence were stored as they
came, in the other copy's frame order. When the inserted photos arrived
later (a download, an upload), the index moved those already-correct
positions a second time, so exclusions, the hero and camera keyframes
landed on other photos (V-21, CD-2).

- Settings are translated by photo (SHA-256, else size and capture time)
  to the frames of the copy they go to: the server's before they are
  compared or applied here, this PC's to the server's frames as its
  manifest lists them before a push (SyncPositions).
- A position on a photo the receiving copy lacks waits in project.json as
  a pending frame and takes its place when the photo arrives, on an
  insertion or an append in step with the index; the Library's hero
  frame follows. A hero chosen meanwhile wins, and advice does not fill
  a hero while one waits. Old projects without the member are unchanged.
- Pushes send expectedFramesVersion; PATCH /api/v1/sequences/{id} answers
  409 frames_conflict when the frames changed since, and the next
  comparison translates again.
- A pulled payload equal to the local copy is settled without the
  archive: an offline or read-only archive no longer fails the
  comparison (V-10).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 16:20:11 +00:00

135 lines
8.7 KiB
C#

using System.Collections;
using System.Globalization;
using Titano.Core.Export;
using Titano.Core.Project;
namespace Titano.Core.Advice;
/// <summary>How one advisor parameter reads and writes <see cref="ProjectSettings"/>.</summary>
internal sealed class ParameterBinding
{
public required string Parameter { get; init; }
public required AdvisorSection Section { get; init; }
/// <summary>A module on/off decision: <see cref="Advisor.Optimise"/> returns it to Auto when the advice differs.</summary>
public bool IsModuleSwitch { get; init; }
/// <summary>The value in force (manual, else advised), null when neither exists.</summary>
public required Func<ProjectSettings, object?> Effective { get; init; }
/// <summary>Auto fields: stores the advice and its reason, never touching a manual value.</summary>
public Func<ProjectSettings, Recommendation, ProjectSettings>? Advise { get; init; }
/// <summary>Auto fields: drops the manual value so the advice applies.</summary>
public Func<ProjectSettings, ProjectSettings>? ResetToAuto { get; init; }
/// <summary>Plain fields: the heavier decision <see cref="Advisor.Optimise"/> takes (each guards user choices).</summary>
public Func<ProjectSettings, Recommendation, ProjectSettings>? Set { get; init; }
}
/// <summary>The binding of every <see cref="AdvisorParameters"/> id, in section order.</summary>
internal static class ParameterBindings
{
public static IReadOnlyList<ParameterBinding> All { get; } =
[
new()
{
Parameter = AdvisorParameters.Hero, Section = AdvisorSection.Frames,
Effective = s => s.HeroFrame,
// A hero waiting for its photo (taken from another copy) is the user's choice too.
Set = (s, r) => s.HeroFrame is null && !s.HeroPending ? s with { HeroFrame = (int)r.Value! } : s,
},
new()
{
Parameter = AdvisorParameters.Excluded, Section = AdvisorSection.Frames,
Effective = s => s.ExcludedFrames,
Set = (s, r) => s with { ExcludedFrames = [.. s.ExcludedFrames.Union((int[])r.Value!).Order()] },
},
AutoField(AdvisorParameters.TimingMode, AdvisorSection.Timing, true, s => s.Timing.Mode, (s, a) => s with { Timing = s.Timing with { Mode = a } }),
AutoField(AdvisorParameters.FrameRate, AdvisorSection.Timing, false, s => s.Timing.FrameRate, (s, a) => s with { Timing = s.Timing with { FrameRate = a } }),
new()
{
Parameter = AdvisorParameters.FitToSeconds, Section = AdvisorSection.Timing,
Effective = s => s.Timing.FitToSeconds,
Set = (s, r) => s with { Timing = s.Timing with { FitToSeconds = (double)r.Value! } },
},
AutoField(AdvisorParameters.Deflicker, AdvisorSection.Exposure, true, s => s.Exposure.Deflicker, (s, a) => s with { Exposure = s.Exposure with { Deflicker = a } }),
AutoField(AdvisorParameters.DeflickerWindow, AdvisorSection.Exposure, false, s => s.Exposure.DeflickerWindow, (s, a) => s with { Exposure = s.Exposure with { DeflickerWindow = a } }),
AutoField(AdvisorParameters.DeflickerStrength, AdvisorSection.Exposure, false, s => s.Exposure.DeflickerStrength, (s, a) => s with { Exposure = s.Exposure with { DeflickerStrength = a } }),
AutoField(AdvisorParameters.Regions, AdvisorSection.Exposure, true, s => s.Exposure.Regions, (s, a) => s with { Exposure = s.Exposure with { Regions = a } }),
AutoField(AdvisorParameters.HolyGrail, AdvisorSection.Exposure, true, s => s.Exposure.HolyGrail, (s, a) => s with { Exposure = s.Exposure with { HolyGrail = a } }),
AutoField(AdvisorParameters.TransitionLength, AdvisorSection.Exposure, false, s => s.Exposure.TransitionLength, (s, a) => s with { Exposure = s.Exposure with { TransitionLength = a } }),
AutoField(AdvisorParameters.WhiteBalanceSmoothing, AdvisorSection.Exposure, true, s => s.Exposure.WhiteBalanceSmoothing, (s, a) => s with { Exposure = s.Exposure with { WhiteBalanceSmoothing = a } }),
AutoField(AdvisorParameters.MotionBlur, AdvisorSection.Motion, true, s => s.Motion.MotionBlur, (s, a) => s with { Motion = s.Motion with { MotionBlur = a } }),
AutoField(AdvisorParameters.TargetShutterAngle, AdvisorSection.Motion, false, s => s.Motion.TargetShutterAngle, (s, a) => s with { Motion = s.Motion with { TargetShutterAngle = a } }),
AutoField(AdvisorParameters.FlowQuality, AdvisorSection.Motion, false, s => s.Motion.FlowQuality, (s, a) => s with { Motion = s.Motion with { FlowQuality = a } }),
AutoField(AdvisorParameters.Stabilize, AdvisorSection.Motion, true, s => s.Motion.Stabilize, (s, a) => s with { Motion = s.Motion with { Stabilize = a } }),
AutoField(AdvisorParameters.StabilizeSmoothing, AdvisorSection.Motion, false, s => s.Motion.StabilizeSmoothing, (s, a) => s with { Motion = s.Motion with { StabilizeSmoothing = a } }),
AutoField(AdvisorParameters.Aspect, AdvisorSection.Camera, false, s => s.Camera.Aspect, (s, a) => s with { Camera = s.Camera with { Aspect = a } }),
new()
{
Parameter = AdvisorParameters.OffsetY, Section = AdvisorSection.Camera,
Effective = s => s.Camera.OffsetY,
Set = (s, r) => s.Camera.OffsetY == 0 ? s with { Camera = s.Camera with { OffsetY = (double)r.Value! } } : s,
},
AutoField(AdvisorParameters.ExportPreset, AdvisorSection.Export, true, s => s.Export.Preset, (s, a) => s with { Export = s.Export with { Preset = a } }),
new()
{
Parameter = AdvisorParameters.Resolution, Section = AdvisorSection.Export,
Effective = s => s.Export.Overrides is { Width: { } w, Height: { } h } ? Size(w, h) : null,
Set = CapResolution,
},
];
public static ParameterBinding? Find(string parameter) => All.FirstOrDefault(b => b.Parameter == parameter);
/// <summary>Value equality that compares lists element by element.</summary>
public static bool SameValue(object? a, object? b)
{
if (a is IEnumerable x && b is IEnumerable y && a is not string && b is not string) return x.Cast<object>().SequenceEqual(y.Cast<object>());
return Equals(a, b);
}
/// <summary>Recommendation arguments as stored in <see cref="Auto{T}.ReasonArgs"/> (culture-neutral text).</summary>
public static string[] Strings(IReadOnlyList<object> args) => [.. args.Select(a => Convert.ToString(a, CultureInfo.InvariantCulture) ?? string.Empty)];
public static string Size(int width, int height) => string.Create(CultureInfo.InvariantCulture, $"{width}x{height}");
private static ParameterBinding AutoField<T>(string parameter, AdvisorSection section, bool module, Func<ProjectSettings, Auto<T>> get, Func<ProjectSettings, Auto<T>, ProjectSettings> set) => new()
{
Parameter = parameter,
Section = section,
IsModuleSwitch = module,
Effective = s => EffectiveOf(get(s)),
Advise = (s, r) => set(s, Advise(get(s), r)),
ResetToAuto = s => set(s, get(s).Reset()),
};
private static object? EffectiveOf<T>(Auto<T> auto) => !auto.IsAuto && auto.Manual is not null ? auto.Manual : auto.HasAdvice ? auto.AutoValue : null;
/// <summary>
/// Stores a value with its reason. Missing data changes no value: the missing-data reason is stored only when the
/// parameter has no advice yet, so the Auto tooltip can say why it is empty.
/// </summary>
private static Auto<T> Advise<T>(Auto<T> auto, Recommendation recommendation)
{
var args = Strings(recommendation.ReasonArgs);
if (recommendation.Value is null)
return auto.HasAdvice ? auto : auto with { ReasonKey = recommendation.ReasonKey, ReasonArgs = args };
if (recommendation.Value is not T value)
throw new InvalidOperationException($"{recommendation.Parameter} expects {typeof(T).Name}, got {recommendation.Value.GetType().Name}.");
return auto.WithAdvice(value, recommendation.ReasonKey, args);
}
/// <summary>Overrides the output size when the preset in force is larger than the recommended cap.</summary>
private static ProjectSettings CapResolution(ProjectSettings settings, Recommendation recommendation)
{
var parts = ((string)recommendation.Value!).Split('x');
var width = int.Parse(parts[0], CultureInfo.InvariantCulture);
var height = int.Parse(parts[1], CultureInfo.InvariantCulture);
var id = EffectiveOf(settings.Export.Preset) as string;
if (id is null || ExportPresets.Find(id) is not { Width: > 0 } preset || (preset.Width <= width && preset.Height <= height)) return settings;
return settings with { Export = settings.Export with { Overrides = settings.Export.Overrides with { Width = width, Height = height } } };
}
}