back to the school

SageFs.Core/LiveTestFileRouting.fs

   1  namespace SageFs
   2  
   3  /// Pure decision core for "which sessions' live testing does a file change reach".
   4  ///
   5  /// A change to a file belongs to the sessions whose working directory contains it, and it reaches
   6  /// a session's live testing exactly when that session has live testing on. Nothing else decides
   7  /// it: not which session is primary, not which one the dashboard is viewing, not what any other
   8  /// session has switched on. The earlier shape asked the primary cycle's activation first, so a
   9  /// daemon with live testing off on the primary session and on for another one silently dropped
  10  /// every save and every unsaved-buffer change for the other one.
  11  ///
  12  /// Ownership reuses `LiveTestWatcherCore.sessionsForPath`, the same longest-matching-directory
  13  /// resolution the file watcher uses to attribute `FileReloaded`, so the two cannot disagree about
  14  /// who owns a path.
  15  ///
  16  /// Top-level module so the SageFs.Simulation harness and FSI consumers resolve it by name.
  17  module LiveTestFileRouting =
  18  
  19    open WorkerProtocol
  20    open SageFs.Features.LiveTesting
  21  
  22    /// Why a session exists, as far as the live loop is concerned.
  23    [<RequireQualifiedAccess>]
  24    type SessionPurpose =
  25      /// A session a person or an agent works in: a save in its directory and a keystroke in its buffer drive its live loop.
  26      | Working
  27      /// A session the cohort made to verify landings. The landing gate evaluates the rebased files into it and runs its
  28      /// tests itself, so a save in its worktree must not also drive its live loop: that loop confirms with a real build,
  29      /// the build swaps the worker, and a swap in the middle of a verification is a race the gate never asked for.
  30      | Verifying
  31  
  32    /// A session as the router sees it: where it lives on disk, and why it exists.
  33    type SessionRoot =
  34      { SessionId: SessionId
  35        WorkingDirectory: string
  36        Purpose: SessionPurpose }
  37  
  38    /// Where a file change goes.
  39    [<RequireQualifiedAccess>]
  40    type FileRoute =
  41      /// No session's working directory contains the file (it came from the daemon's fallback
  42      /// watch directory). The primary cycle takes it, which is the session-less behaviour.
  43      | SessionLess
  44      /// The sessions that own the file AND have live testing on. May be empty: the owners have
  45      /// live testing off, so the change reaches nobody.
  46      | ToSessions of SessionId list
  47  
  48    /// Every session with live testing on, in the order given. A session that has it off is dropped.
  49    let withLiveTestingOn
  50      (activationOf: SessionId -> LiveTestingActivation)
  51      (sessions: SessionId list)
  52      : SessionId list =
  53      sessions
  54      |> List.filter (fun sid ->
  55        match activationOf sid with
  56        | LiveTestingActivation.Active -> true
  57        | LiveTestingActivation.Inactive -> false)
  58  
  59    /// On when either is on. A file watcher exists for a session when the primary cycle is on (the
  60    /// global enable, which watches every session) or the session's own cycle is on.
  61    let eitherOn (first: LiveTestingActivation) (second: LiveTestingActivation) : LiveTestingActivation =
  62      match first, second with
  63      | LiveTestingActivation.Inactive, LiveTestingActivation.Inactive -> LiveTestingActivation.Inactive
  64      | LiveTestingActivation.Active, LiveTestingActivation.Active
  65      | LiveTestingActivation.Active, LiveTestingActivation.Inactive
  66      | LiveTestingActivation.Inactive, LiveTestingActivation.Active -> LiveTestingActivation.Active
  67  
  68    /// The sessions whose working directory is the most specific one containing `path`. Sessions
  69    /// that share that directory all own the path. A session with no working directory owns nothing.
  70    let owners (roots: SessionRoot list) (path: string) : SessionId list =
  71      let dirSessions =
  72        roots
  73        |> List.filter (fun root -> not (System.String.IsNullOrEmpty root.WorkingDirectory))
  74        |> List.fold
  75          (fun acc root ->
  76            let existing = acc |> Map.tryFind root.WorkingDirectory |> Option.defaultValue []
  77            acc |> Map.add root.WorkingDirectory (existing @ [ root.SessionId ]))
  78          Map.empty
  79      LiveTestWatcherCore.sessionsForPath dirSessions path
  80  
  81    /// The sessions among `sessions` whose live loop a save or a keystroke may drive: every one except a session known to be
  82    /// verifying. A verifying session still OWNS the files in its worktree (so they are never mistaken for session-less ones and
  83    /// handed to the primary), it just is not driven by them. A session the roots do not mention is a working one.
  84    let drivenByEdits (roots: SessionRoot list) (sessions: SessionId list) : SessionId list =
  85      sessions
  86      |> List.filter (fun sid ->
  87        roots
  88        |> List.exists (fun root ->
  89          root.SessionId = sid
  90          && (match root.Purpose with
  91              | SessionPurpose.Verifying -> true
  92              | SessionPurpose.Working -> false))
  93        |> not)
  94  
  95    /// Route one changed file. `activationOf` answers for a session's OWN live-testing cycle.
  96    let route
  97      (activationOf: SessionId -> LiveTestingActivation)
  98      (roots: SessionRoot list)
  99      (path: string)
 100      : FileRoute =
 101      match owners roots path with
 102      | [] -> FileRoute.SessionLess
 103      | owning -> FileRoute.ToSessions (withLiveTestingOn activationOf (drivenByEdits roots owning))
 104