summaryrefslogtreecommitdiff
path: root/src/SomhairlesDream.Server/ArtifactRunCoordinator.fs
blob: 51a4fcd61a72da0addedd5b064a4644e15069309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
namespace SomhairlesDream.Server

open System
open System.Collections.Concurrent
open System.Collections.Generic
open System.IO
open System.Security.Cryptography
open System.Threading.Tasks
open SomhairlesDream.Modeling
open SomhairlesDream.Shared

type RunStartOutcome =
    | Accepted of RunSnapshot
    | Conflict of RunSnapshot

type ArtifactLookupError =
    | RunNotFound
    | RunNotComplete of RunSnapshot
    | InvalidArtifactPath of string
    | ArtifactNotFound
    | InvalidManifest of string
    | ArtifactMismatch of string

type ArtifactFile =
    { Stream: Stream
      RelativePath: string }

type ArtifactRunCoordinator(
    artifactRoot: string,
    bridgeFactory: unit -> IArtifactBridge,
    clock: unit -> DateTimeOffset,
    staleAfter: TimeSpan
) =
    let root = Path.GetFullPath(artifactRoot)
    let registry = ArtifactRunRegistry(staleAfter, clock)

    let validateIds (ids: RunIds) =
        [| ids.ProjectId; ids.RunId; ids.BaseId; ids.TargetId |]
        |> Array.tryFind (Ids.validateResourceId >> Result.isError)
        |> function
            | Some invalid -> Error $"invalid resource id: {invalid}"
            | None -> Ok ()

    let runDirectory (projectId: string) (runId: string) =
        Path.Combine(root, projectId, runId)

    let failIfNeeded (store: ArtifactRunStore) (ids: RunIds) message =
        let snapshot = store.Snapshot()

        if snapshot.Status = "idle" || snapshot.Status = "running" then
            store.Apply(
                RunEvent.Fail
                    { Ids = ids
                      At = clock ()
                      StepId = snapshot.CurrentStepId
                      Message = message }
            )
            |> ignore

    let safeArtifactPath (runDirectory: string) (relativePath: string) =
        if String.IsNullOrWhiteSpace(relativePath) || Path.IsPathRooted(relativePath) then
            Error "artifact path must be relative"
        else
            let normalized = relativePath.Replace('\\', '/')
            let segments = normalized.Split('/', StringSplitOptions.RemoveEmptyEntries)

            if segments |> Array.exists (fun segment -> segment = ".." || segment = ".") then
                Error "artifact path contains traversal"
            else
                let fullPath = Path.GetFullPath(Path.Combine(runDirectory, normalized.Replace('/', Path.DirectorySeparatorChar)))
                let prefix = Path.GetFullPath(runDirectory).TrimEnd(Path.DirectorySeparatorChar) + string Path.DirectorySeparatorChar

                if fullPath.StartsWith(prefix, StringComparison.Ordinal) then
                    Ok(fullPath, String.Join("/", segments))
                else
                    Error "artifact path escapes run directory"

    let rejectLinks (fullPath: string) =
        let rootPath = Path.GetFullPath(root)
        let relative = fullPath.Substring(rootPath.Length + 1)
        let mutable current = rootPath
        let mutable outcome = Ok ()

        for segment in
            relative.Split(
                [| Path.DirectorySeparatorChar; Path.AltDirectorySeparatorChar |],
                StringSplitOptions.RemoveEmptyEntries
            ) do
            match outcome with
            | Error _ -> ()
            | Ok () ->
                current <- Path.Combine(current, segment)

                let info: FileSystemInfo =
                    if Directory.Exists(current) && not (File.Exists(current)) then
                        DirectoryInfo(current) :> FileSystemInfo
                    else
                        FileInfo(current) :> FileSystemInfo

                if not (isNull info.LinkTarget) then
                    outcome <- Error "artifact path crosses a filesystem link"

        outcome

    let checkpointDigests =
        ConcurrentDictionary<string * string, ConcurrentDictionary<string, string * int64>>()

    let recordCheckpointDigest (ids: RunIds) (relativePath: string) =
        try
            match safeArtifactPath (runDirectory ids.ProjectId ids.RunId) relativePath with
            | Ok(fullPath, normalized) when File.Exists(fullPath) ->
                match rejectLinks fullPath with
                | Error _ -> ()
                | Ok () ->
                    use stream = File.OpenRead(fullPath)
                    use sha = SHA256.Create()

                    let digest =
                        (Convert.ToHexString(sha.ComputeHash(stream)).ToLowerInvariant(), stream.Length)

                    let digests =
                        checkpointDigests.GetOrAdd(
                            (ids.ProjectId, ids.RunId),
                            fun _ -> ConcurrentDictionary<string, string * int64>()
                        )

                    digests[normalized] <- digest
            | _ -> ()
        with _ ->
            ()

    let openValidated
        (fullPath: string)
        (normalized: string)
        (expectedSha256: string)
        (expectedBytes: int64)
        : Result<ArtifactFile, ArtifactLookupError> =
        match rejectLinks fullPath with
        | Error message -> Error(InvalidArtifactPath message)
        | Ok () ->
            try
                if not (File.Exists(fullPath)) then
                    Error ArtifactNotFound
                else
                    let stream =
                        File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read)

                    let outcome =
                        try
                            if stream.Length <> expectedBytes then
                                Error(ArtifactMismatch $"artifact byte count mismatch: {normalized}")
                            else
                                use sha = SHA256.Create()

                                let sha256 =
                                    Convert.ToHexString(sha.ComputeHash(stream)).ToLowerInvariant()

                                if sha256 <> expectedSha256 then
                                    Error(ArtifactMismatch $"artifact hash mismatch: {normalized}")
                                else
                                    stream.Seek(0L, SeekOrigin.Begin) |> ignore
                                    Ok { Stream = stream :> Stream; RelativePath = normalized }
                        with _ ->
                            stream.Dispose()
                            reraise ()

                    match outcome with
                    | Ok file -> Ok file
                    | Error lookupError ->
                        stream.Dispose()
                        Error lookupError
            with
            | :? FileNotFoundException
            | :? DirectoryNotFoundException -> Error ArtifactNotFound
            | :? IOException as ex -> Error(InvalidArtifactPath $"artifact unavailable: {ex.Message}")

    let verifiedManifest (projectId: string) (runId: string) =
        let path = Path.Combine(runDirectory projectId runId, "manifest.json")

        match rejectLinks path with
        | Error message -> Error(InvalidArtifactPath message)
        | Ok () ->
            match ArtifactVerifier.verify path with
            | Ok manifest -> Ok manifest
            | Error message -> Error(InvalidManifest message)

    let memberDigests (manifest: ArtifactManifest) =
        let map = Dictionary<string, string * int64>()

        let add (relativePath: string) (sha256: string) (bytes: int64) =
            map[relativePath] <- (sha256, bytes)

        for step in manifest.Steps do
            add step.ArtifactPath step.Sha256 step.Bytes
            step.Render |> Option.iter (fun render -> add render.ArtifactPath render.Sha256 render.Bytes)

        map

    let completedDigest (projectId: string) (runId: string) (normalized: string) =
        match verifiedManifest projectId runId with
        | Error lookupError -> Error lookupError
        | Ok manifest ->
            match (memberDigests manifest).TryGetValue(normalized) with
            | true, digest -> Ok digest
            | false, _ -> Error ArtifactNotFound

    let checkpointDigest (projectId: string) (runId: string) (normalized: string) =
        match checkpointDigests.TryGetValue((projectId, runId)) with
        | true, digests ->
            match digests.TryGetValue(normalized) with
            | true, digest -> Ok digest
            | false, _ -> Error ArtifactNotFound
        | false, _ -> Error ArtifactNotFound

    let runPipeline (store: ArtifactRunStore) (ids: RunIds) render =
        try
            let options : PipelineOptions =
                { ArtifactRoot = root
                  Ids = ids
                  Steps = Design.forProject ids.ProjectId
                  Render = render
                  Bridge = bridgeFactory ()
                  Clock = clock
                  OnEvent =
                      fun event ->
                          (match event with
                           | Checkpoint value -> recordCheckpointDigest value.Ids value.ArtifactPath
                           | _ -> ())

                          match store.Apply event with
                          | Ok _ -> ()
                          | Error message -> invalidOp message }

            match Pipeline.run options with
            | Ok _ -> ()
            | Error message -> failIfNeeded store ids message
        with ex ->
            failIfNeeded store ids ex.Message

    member _.ArtifactRoot = root

    member _.Start(ids: RunIds, render: bool) : Result<RunStartOutcome, string> =
        match validateIds ids with
        | Error message -> Error message
        | Ok () ->
            match registry.TryCreate(ids) with
            | Some store ->
                Task.Run(fun () -> runPipeline store ids render) |> ignore
                Ok(Accepted(store.Snapshot()))
            | None ->
                match registry.TryFind(ids.ProjectId, ids.RunId) with
                | Some store -> Ok(Conflict(store.Observe(clock ())))
                | None -> Error "run registry entry disappeared"

    member _.TryFind(projectId: string, runId: string) = registry.TryFind(projectId, runId)

    member _.Subscribe(projectId: string, runId: string) =
        registry.TryFind(projectId, runId) |> Option.map (fun store -> store.Subscribe())

    member _.ObserveAll() = registry.ObserveAll(clock ())

    member _.Manifest(projectId: string, runId: string) : Result<ArtifactManifest, ArtifactLookupError> =
        match registry.TryFind(projectId, runId) with
        // Pre-published historical runs persist on disk after restarts; serve
        // them from the verified manifest when no live registry entry exists.
        | None -> verifiedManifest projectId runId
        | Some store ->
            let snapshot = store.Observe(clock ())

            if snapshot.Status <> "complete" then
                Error(RunNotComplete snapshot)
            else
                verifiedManifest projectId runId

    member _.Artifact(projectId: string, runId: string, relativePath: string) : Result<ArtifactFile, ArtifactLookupError> =
        match registry.TryFind(projectId, runId) with
        | None -> Error RunNotFound
        | Some store ->
            let snapshot = store.Observe(clock ())

            if snapshot.Status <> "complete" then
                Error(RunNotComplete snapshot)
            else
                let directory = runDirectory projectId runId

                match safeArtifactPath directory relativePath with
                | Error message -> Error(InvalidArtifactPath message)
                | Ok(fullPath, normalized) ->
                    match rejectLinks fullPath with
                    | Error message -> Error(InvalidArtifactPath message)
                    | Ok () ->
                        match completedDigest projectId runId normalized with
                        | Error lookupError -> Error lookupError
                        | Ok(sha256, bytes) -> openValidated fullPath normalized sha256 bytes

    member _.StepArtifact(projectId: string, runId: string, relativePath: string) : Result<ArtifactFile, ArtifactLookupError> =
        match registry.TryFind(projectId, runId) with
        // Disk fallback keeps pre-published historical checkpoints reachable
        // for boot replay after a service restart; only .glb step artifacts
        // under steps/ are ever served, digest-verified against the manifest.
        | None ->
            match safeArtifactPath (runDirectory projectId runId) relativePath with
            | Error message -> Error(InvalidArtifactPath message)
            | Ok(_, normalized) when not (normalized.EndsWith(".glb", StringComparison.OrdinalIgnoreCase)) ->
                Error(InvalidArtifactPath "step artifacts must be .glb files")
            | Ok(_, normalized) when not (normalized.StartsWith("steps/", StringComparison.Ordinal)) ->
                Error(InvalidArtifactPath "step artifacts must live under steps/")
            | Ok(fullPath, normalized) ->
                let digest =
                    match rejectLinks fullPath with
                    | Error message -> Error(InvalidArtifactPath message)
                    | Ok () -> completedDigest projectId runId normalized

                match digest with
                | Error lookupError -> Error lookupError
                | Ok(sha256, bytes) -> openValidated fullPath normalized sha256 bytes
        | Some store ->
            let directory = runDirectory projectId runId

            match safeArtifactPath directory relativePath with
            | Error message -> Error(InvalidArtifactPath message)
            | Ok(_, normalized) when not (normalized.EndsWith(".glb", StringComparison.OrdinalIgnoreCase)) ->
                Error(InvalidArtifactPath "step artifacts must be .glb files")
            | Ok(_, normalized) when not (normalized.StartsWith("steps/", StringComparison.Ordinal)) ->
                Error(InvalidArtifactPath "step artifacts must live under steps/")
            | Ok(fullPath, normalized) ->
                let snapshot = store.Observe(clock ())

                let digest =
                    match rejectLinks fullPath with
                    | Error message -> Error(InvalidArtifactPath message)
                    | Ok () ->
                        if snapshot.Status = "complete" then
                            completedDigest projectId runId normalized
                        elif snapshot.CompletedSteps |> Array.exists (fun recorded -> recorded = normalized) then
                            checkpointDigest projectId runId normalized
                        else
                            Error ArtifactNotFound

                match digest with
                | Error lookupError -> Error lookupError
                | Ok(sha256, bytes) -> openValidated fullPath normalized sha256 bytes