summaryrefslogtreecommitdiff
path: root/src/SomhairlesDream.Modeling/Pipeline.fs
blob: 3f1f1059cc2dd0746465442dbff33f3b481aad56 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
namespace SomhairlesDream.Modeling

open System
open System.IO
open System.Security.Cryptography
open System.Text
open System.Text.Json
open System.Text.Json.Nodes
open SomhairlesDream.Shared

[<CLIMutable>]
type BridgeRequest =
    { Step: DesignStep
      GlbPath: string
      RenderPath: string option
      ResultPath: string
      LogPath: string }

[<CLIMutable>]
type BridgeResult =
    { BlenderVersion: string
      ExportedAt: DateTimeOffset
      ObjectIds: string array
      VerifiedObjectIds: string array
      GlbBytes: int64
      RenderPath: string option
      RenderedAt: DateTimeOffset option }

type IArtifactBridge =
    abstract Export: request: BridgeRequest * heartbeat: (unit -> unit) -> Result<BridgeResult, string>

[<CLIMutable>]
type PipelineOptions =
    { ArtifactRoot: string
      Ids: RunIds
      Render: bool
      Bridge: IArtifactBridge
      Clock: unit -> DateTimeOffset
      OnEvent: RunEvent -> unit }

module private ArtifactFiles =
    let runDirectory (root: string) (ids: RunIds) = Path.Combine(root, ids.ProjectId, ids.RunId)

    let toSystemPath (relativePath: string) =
        relativePath.Replace('/', Path.DirectorySeparatorChar)

    let sha256 path =
        use stream = File.OpenRead(path)
        use sha = SHA256.Create()
        sha.ComputeHash(stream) |> Convert.ToHexString |> fun value -> value.ToLowerInvariant()

    let byteCount path = FileInfo(path).Length

module private ManifestJson =
    let private options () =
        let value = JsonSerializerOptions()
        value.WriteIndented <- true
        value

    let private stringArray (values: string array) =
        let result = JsonArray()

        for value in values do
            result.Add(JsonValue.Create(value))

        result :> JsonNode

    let private change (value: ArtifactChange) =
        let json = JsonObject()
        json["objectId"] <- JsonValue.Create(value.ObjectId)
        json["kind"] <- JsonValue.Create(value.Kind)
        json["summary"] <- JsonValue.Create(value.Summary)
        json :> JsonNode

    let private changes (values: ArtifactChange array) =
        let result = JsonArray()

        for value in values do
            result.Add(change value)

        result :> JsonNode

    let private render (value: RenderArtifact) =
        let json = JsonObject()
        json["artifactPath"] <- JsonValue.Create(value.ArtifactPath)
        json["sha256"] <- JsonValue.Create(value.Sha256)
        json["bytes"] <- JsonValue.Create(value.Bytes)
        json["exportedAt"] <- JsonValue.Create(value.ExportedAt)
        json :> JsonNode

    let private step (value: StepArtifact) =
        let json = JsonObject()
        json["stepId"] <- JsonValue.Create(value.StepId)
        json["stepIndex"] <- JsonValue.Create(value.StepIndex)
        json["artifactPath"] <- JsonValue.Create(value.ArtifactPath)
        json["logPath"] <- JsonValue.Create(value.LogPath)
        json["sha256"] <- JsonValue.Create(value.Sha256)
        json["bytes"] <- JsonValue.Create(value.Bytes)
        json["exportedAt"] <- JsonValue.Create(value.ExportedAt)
        json["objectIds"] <- stringArray value.ObjectIds
        json["verifiedObjectIds"] <- stringArray value.VerifiedObjectIds
        json["changes"] <- changes value.Changes

        match value.Render with
        | Some renderValue -> json["render"] <- render renderValue
        | None -> ()

        json :> JsonNode

    let serialize (manifest: ArtifactManifest) =
        let json = JsonObject()
        let steps = JsonArray()

        for value in manifest.Steps do
            steps.Add(step value)

        json["schemaVersion"] <- JsonValue.Create(manifest.SchemaVersion)
        json["projectId"] <- JsonValue.Create(manifest.ProjectId)
        json["runId"] <- JsonValue.Create(manifest.RunId)
        json["baseId"] <- JsonValue.Create(manifest.BaseId)
        json["targetId"] <- JsonValue.Create(manifest.TargetId)
        json["status"] <- JsonValue.Create(manifest.Status)
        json["startedAt"] <- JsonValue.Create(manifest.StartedAt)
        json["completedAt"] <- JsonValue.Create(manifest.CompletedAt)
        json["blenderVersion"] <- JsonValue.Create(manifest.BlenderVersion)
        json["steps"] <- steps
        json.ToJsonString(options())

module Pipeline =
    let private 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 private appendEvent (log: StreamWriter) (onEvent: RunEvent -> unit) event =
        log.WriteLine(RunEvent.toJson event)
        log.Flush()
        log.BaseStream.Flush()
        onEvent event

    let private writeManifest path manifest =
        let temporaryPath = path + ".tmp"
        let bytes = Encoding.UTF8.GetBytes(ManifestJson.serialize manifest)

        use stream = new FileStream(temporaryPath, FileMode.CreateNew, FileAccess.Write, FileShare.None, 4096, FileOptions.WriteThrough)
        stream.Write(bytes, 0, bytes.Length)
        stream.Flush(true)
        File.Move(temporaryPath, path)

    let private verifyBridgeResult (step: DesignStep) (request: BridgeRequest) (result: BridgeResult) =
        if not (File.Exists(request.GlbPath)) || ArtifactFiles.byteCount request.GlbPath = 0L then
            Error $"bridge did not produce a non-empty GLB for {step.StepId}"
        elif result.GlbBytes <> ArtifactFiles.byteCount request.GlbPath then
            Error $"bridge GLB byte count did not match for {step.StepId}"
        elif result.ExportedAt.Offset <> TimeSpan.Zero then
            Error $"bridge export timestamp was not UTC for {step.StepId}"
        elif result.RenderedAt |> Option.exists (fun value -> value.Offset <> TimeSpan.Zero) then
            Error $"bridge render timestamp was not UTC for {step.StepId}"
        elif request.RenderPath.IsSome <> result.RenderPath.IsSome then
            Error $"bridge render result did not match the request for {step.StepId}"
        elif request.RenderPath.IsSome && request.RenderPath <> result.RenderPath then
            Error $"bridge render path did not match the request for {step.StepId}"
        elif result.ObjectIds <> step.ObjectIds || result.VerifiedObjectIds <> step.ObjectIds then
            Error $"object ids did not match expected values for {step.StepId}"
        elif result.ObjectIds |> Array.exists (Ids.validateObjectId >> Result.isError) then
            Error $"bridge returned an invalid object id for {step.StepId}"
        else
            Ok ()

    let private renderArtifact runDirectory (step: DesignStep) path renderedAt =
        if not (File.Exists(path)) || ArtifactFiles.byteCount path = 0L then
            Error $"bridge did not produce a non-empty render for {step.StepId}"
        else
            let relativePath = $"steps/{step.StepId}.png"
            let destination = Path.Combine(runDirectory, ArtifactFiles.toSystemPath relativePath)
            File.Move(path, destination)

            Ok
                { ArtifactPath = relativePath
                  Sha256 = ArtifactFiles.sha256 destination
                  Bytes = ArtifactFiles.byteCount destination
                  ExportedAt = renderedAt }

    let run (options: PipelineOptions) =
        match validateIds options.Ids with
        | Error message -> Error message
        | Ok () ->
            let runDirectory = ArtifactFiles.runDirectory options.ArtifactRoot options.Ids

            if Directory.Exists(runDirectory) then
                Error "run directory already exists"
            else
                let stepsDirectory = Path.Combine(runDirectory, "steps")
                let logsDirectory = Path.Combine(runDirectory, "logs")
                let stagingDirectory = Path.Combine(runDirectory, ".staging")
                let eventsPath = Path.Combine(runDirectory, "events.ndjson")
                let manifestPath = Path.Combine(runDirectory, "manifest.json")
                let startedAt = options.Clock()
                let mutable currentStep: string option = None
                let mutable eventLog: StreamWriter option = None
                let mutable blenderVersion: string option = None

                try
                    Directory.CreateDirectory(runDirectory) |> ignore
                    Directory.CreateDirectory(stepsDirectory) |> ignore
                    Directory.CreateDirectory(logsDirectory) |> ignore
                    Directory.CreateDirectory(stagingDirectory) |> ignore

                    use log = new StreamWriter(File.Open(eventsPath, FileMode.CreateNew, FileAccess.Write, FileShare.Read), UTF8Encoding(false))
                    eventLog <- Some log

                    let emit event = appendEvent log options.OnEvent event
                    let ids = options.Ids

                    emit (RunEvent.Start { Ids = ids; At = startedAt; TotalSteps = Design.steps.Length })

                    let artifacts =
                         [| for step in Design.steps do
                               currentStep <- Some step.StepId
                               emit (RunEvent.Heartbeat { Ids = ids; At = options.Clock(); Message = $"starting {step.StepId}" })

                               let glbStagingPath = Path.GetFullPath(Path.Combine(stagingDirectory, $"{step.StepId}.glb"))
                               let renderStagingPath = Path.GetFullPath(Path.Combine(stagingDirectory, $"{step.StepId}.png"))
                               let resultStagingPath = Path.GetFullPath(Path.Combine(stagingDirectory, $"{step.StepId}.result.json"))
                               let logPath = Path.GetFullPath(Path.Combine(logsDirectory, $"{step.StepId}.log"))
                               let request =
                                   { Step = step
                                     GlbPath = glbStagingPath
                                     RenderPath = if options.Render then Some renderStagingPath else None
                                     ResultPath = resultStagingPath
                                     LogPath = logPath }

                               let bridgeResult =
                                   match options.Bridge.Export(request, fun () -> emit (RunEvent.Heartbeat { Ids = ids; At = options.Clock(); Message = $"heartbeat {step.StepId}" })) with
                                   | Ok value -> value
                                   | Error message -> failwith message

                               blenderVersion <- Some bridgeResult.BlenderVersion

                               match verifyBridgeResult step request bridgeResult with
                               | Ok () -> ()
                               | Error message -> failwith message

                               let relativePath = $"steps/{step.StepId}.glb"
                               let destination = Path.Combine(runDirectory, ArtifactFiles.toSystemPath relativePath)
                               File.Move(glbStagingPath, destination)

                               let render =
                                   if options.Render then
                                       let renderedAt = bridgeResult.RenderedAt |> Option.defaultValue bridgeResult.ExportedAt

                                       match renderArtifact runDirectory step renderStagingPath renderedAt with
                                       | Ok value -> Some value
                                       | Error message -> failwith message
                                   else
                                       None

                               let artifact =
                                   { StepId = step.StepId
                                     StepIndex = step.Index
                                     ArtifactPath = relativePath
                                     LogPath = $"logs/{step.StepId}.log"
                                     Sha256 = ArtifactFiles.sha256 destination
                                     Bytes = ArtifactFiles.byteCount destination
                                     ExportedAt = bridgeResult.ExportedAt
                                     ObjectIds = step.ObjectIds
                                     VerifiedObjectIds = bridgeResult.VerifiedObjectIds
                                     Changes = step.Changes
                                     Render = render }

                               let checkpoint =
                                   { Ids = ids
                                     At = options.Clock()
                                     StepId = step.StepId
                                     StepIndex = step.Index
                                     TotalSteps = Design.steps.Length
                                     Message = "artifact verified"
                                     ArtifactPath = relativePath }

                               emit (RunEvent.Checkpoint checkpoint)
                               yield artifact |]

                    let completedAt = options.Clock()

                    let manifest =
                        { SchemaVersion = 1
                          ProjectId = ids.ProjectId
                          RunId = ids.RunId
                          BaseId = ids.BaseId
                          TargetId = ids.TargetId
                          Status = "complete"
                          StartedAt = startedAt
                          CompletedAt = completedAt
                          BlenderVersion = blenderVersion |> Option.defaultValue "unknown"
                          Steps = artifacts }

                    writeManifest manifestPath manifest
                    emit (RunEvent.Complete { Ids = ids; At = completedAt; ManifestPath = "manifest.json" })
                    currentStep <- None
                    if Directory.Exists(stagingDirectory) then
                        Directory.Delete(stagingDirectory, true)
                    Ok manifest
                with ex ->
                    match eventLog with
                    | Some log ->
                        try
                            appendEvent log options.OnEvent (RunEvent.Fail { Ids = options.Ids; At = options.Clock(); StepId = currentStep; Message = ex.Message })
                        with _ -> ()
                    | None -> ()

                    if Directory.Exists(stagingDirectory) then
                        Directory.Delete(stagingDirectory, true)

                    Error ex.Message

module ArtifactVerifier =
    let private stringProperty (value: JsonElement) (name: string) =
        let result = value.GetProperty(name).GetString()

        if isNull result then
            failwith $"manifest property '{name}' is null"
        else
            result

    let private parseRender (value: JsonElement) =
        let mutable renderValue = Unchecked.defaultof<JsonElement>

        if value.TryGetProperty("render", &renderValue) then
            Some
                { ArtifactPath = stringProperty renderValue "artifactPath"
                  Sha256 = stringProperty renderValue "sha256"
                  Bytes = renderValue.GetProperty("bytes").GetInt64()
                  ExportedAt = renderValue.GetProperty("exportedAt").GetDateTimeOffset() }
        else
            None

    let private parseStep (value: JsonElement) : StepArtifact =
        { StepId = stringProperty value "stepId"
          StepIndex = value.GetProperty("stepIndex").GetInt32()
          ArtifactPath = stringProperty value "artifactPath"
          LogPath = stringProperty value "logPath"
          Sha256 = stringProperty value "sha256"
          Bytes = value.GetProperty("bytes").GetInt64()
          ExportedAt = value.GetProperty("exportedAt").GetDateTimeOffset()
          ObjectIds = value.GetProperty("objectIds").EnumerateArray() |> Seq.map (fun item -> item.GetString()) |> Seq.toArray
          VerifiedObjectIds = value.GetProperty("verifiedObjectIds").EnumerateArray() |> Seq.map (fun item -> item.GetString()) |> Seq.toArray
          Changes =
              value.GetProperty("changes").EnumerateArray()
              |> Seq.map (fun item ->
                  { ObjectId = stringProperty item "objectId"
                    Kind = stringProperty item "kind"
                    Summary = stringProperty item "summary" })
              |> Seq.toArray
          Render = parseRender value }

    let private parseManifest (value: JsonElement) : ArtifactManifest =
        { SchemaVersion = value.GetProperty("schemaVersion").GetInt32()
          ProjectId = stringProperty value "projectId"
          RunId = stringProperty value "runId"
          BaseId = stringProperty value "baseId"
          TargetId = stringProperty value "targetId"
          Status = stringProperty value "status"
          StartedAt = value.GetProperty("startedAt").GetDateTimeOffset()
          CompletedAt = value.GetProperty("completedAt").GetDateTimeOffset()
          BlenderVersion = stringProperty value "blenderVersion"
          Steps = value.GetProperty("steps").EnumerateArray() |> Seq.map parseStep |> Seq.toArray }

    let private safePath runDirectory relativePath =
        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, ArtifactFiles.toSystemPath normalized))
                let prefix = Path.GetFullPath(runDirectory).TrimEnd(Path.DirectorySeparatorChar) + string Path.DirectorySeparatorChar

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

    let private verifyFile runDirectory expectedPath expectedHash expectedBytes =
        match safePath runDirectory expectedPath with
        | Error message -> Error message
        | Ok path when not (File.Exists(path)) -> Error $"artifact is missing: {expectedPath}"
        | Ok path when FileInfo(path).Length <> expectedBytes -> Error $"artifact byte count mismatch: {expectedPath}"
        | Ok path when ArtifactFiles.sha256 path <> expectedHash -> Error $"artifact hash mismatch: {expectedPath}"
        | Ok _ -> Ok ()

    let private verifyLogFile runDirectory expectedPath =
        match safePath runDirectory expectedPath with
        | Error message -> Error message
        | Ok path when not (File.Exists(path)) -> Error $"log is missing: {expectedPath}"
        | Ok path when FileInfo(path).Length = 0L -> Error $"log is empty: {expectedPath}"
        | Ok _ -> Ok ()

    let verify manifestPath =
        try
            if not (File.Exists(manifestPath)) then
                Error "manifest is missing"
            elif Path.GetFileName(manifestPath) <> "manifest.json" then
                Error "manifest path must be manifest.json"
            else
                use document = JsonDocument.Parse(File.ReadAllText(manifestPath))
                let manifest = parseManifest document.RootElement
                let runDirectory = Path.GetDirectoryName(Path.GetFullPath(manifestPath))
                let projectDirectory = Directory.GetParent(runDirectory).Name
                let runDirectoryName = DirectoryInfo(runDirectory).Name

                if manifest.SchemaVersion <> 1 then
                    Error "unsupported manifest schema"
                elif manifest.Status <> "complete" then
                    Error "manifest is not complete"
                elif manifest.ProjectId <> projectDirectory || manifest.RunId <> runDirectoryName then
                    Error "manifest identity does not match its directory"
                elif Ids.validateResourceId manifest.ProjectId |> Result.isError
                     || Ids.validateResourceId manifest.RunId |> Result.isError
                     || Ids.validateResourceId manifest.BaseId |> Result.isError
                     || Ids.validateResourceId manifest.TargetId |> Result.isError then
                    Error "manifest contains an invalid resource id"
                elif manifest.Steps.Length <> Design.steps.Length then
                    Error "manifest step count does not match design"
                else
                    let mutable failure: string option = None

                    for expected, actual in Array.zip Design.steps manifest.Steps do
                        if failure.IsNone && actual.StepId <> expected.StepId then
                            failure <- Some $"manifest step id mismatch: {expected.StepId}"
                        elif failure.IsNone && actual.StepIndex <> expected.Index then
                            failure <- Some $"manifest step index mismatch: {expected.StepId}"
                        elif failure.IsNone && actual.ObjectIds <> expected.ObjectIds then
                            failure <- Some $"manifest source object ids mismatch: {expected.StepId}"
                        elif failure.IsNone && actual.VerifiedObjectIds <> expected.ObjectIds then
                            failure <- Some $"manifest verified object ids mismatch: {expected.StepId}"
                        elif failure.IsNone && actual.Changes <> expected.Changes then
                            failure <- Some $"manifest changes mismatch: {expected.StepId}"
                        elif failure.IsNone then
                            let expectedPath = $"steps/{expected.StepId}.glb"
                            let expectedLogPath = $"logs/{expected.StepId}.log"

                            if actual.ArtifactPath <> expectedPath then
                                failure <- Some $"manifest artifact path mismatch: {expected.StepId}"
                            elif actual.LogPath <> expectedLogPath then
                                failure <- Some $"manifest log path mismatch: {expected.StepId}"
                            else
                                match verifyLogFile runDirectory actual.LogPath with
                                | Error message -> failure <- Some message
                                | Ok () ->
                                    match verifyFile runDirectory actual.ArtifactPath actual.Sha256 actual.Bytes with
                                    | Error message -> failure <- Some message
                                    | Ok () ->
                                        match actual.Render with
                                        | Some render ->
                                            match verifyFile runDirectory render.ArtifactPath render.Sha256 render.Bytes with
                                            | Error message -> failure <- Some message
                                            | Ok () -> ()
                                        | None -> ()

                    match failure with
                    | Some message -> Error message
                    | None -> Ok manifest
        with ex ->
            Error $"invalid manifest: {ex.Message}"