golem-cli 1.3.1

Command line interface for Golem.
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright 2024-2025 Golem Cloud
//
// Licensed under the Golem Source License v1.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://license.golem.cloud/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::app::build::task_result_marker::TaskResultMarkerHashSourceKind::{Hash, HashFromString};
use crate::fs;
use crate::log::log_warn_action;
use crate::model::app::{AppComponentName, DependentComponent};
use crate::model::app_raw::{
    ComposeAgentWrapper, GenerateAgentWrapper, GenerateQuickJSCrate, GenerateQuickJSDTS,
    InjectToPrebuiltQuickJs,
};
use crate::model::ProjectId;
use crate::model::{app_raw, ComponentName};
use anyhow::{anyhow, bail, Context};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use wit_parser::PackageName;

pub enum TaskResultMarkerHashSourceKind {
    // The string will be hashed
    HashFromString(String),
    // The string will be used as the hash, expected to be in hex format
    Hash(String),
}

pub trait TaskResultMarkerHashSource {
    fn kind() -> &'static str;

    /// The hashed value of id will be used as the task result marker filename.
    ///
    /// If id() returns None, then the source will be used as id.
    ///
    /// Specifying the id is optional, as some tasks are their own identity, like external commands.
    /// In those cases we can skip calculating values and hashes twice.
    ///
    /// The main difference between id and hash is that it should not include
    /// generic "task properties", only ids for the task. E.g.: the hash_input for rpc linking
    /// should contain all the main and dependency component names and types, while the id should
    /// only contain the main component name which the dependencies are linked into.
    fn id(&self) -> anyhow::Result<Option<String>>;

    /// The source will be used for calculating the hash value for the task result marker.
    /// It should contain all the properties of the task which should trigger re-runs.
    /// Note that currently we usually do not include file sources in these, as for those
    /// we use mod-time based checks together with task markers.
    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind>;
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TaskResult {
    // NOTE: kind is optional, only used for debugging
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
    // NOTE: id is optional, only used for debugging
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    // NOTE: hash_input is optional, only used for debugging
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub hash_input: Option<String>,

    pub hash_hex: String,
    pub success: bool,
}

#[derive(Serialize)]
pub struct ResolvedExternalCommandMarkerHash<'a> {
    pub build_dir: &'a Path,
    pub command: &'a app_raw::ExternalCommand,
}

impl TaskResultMarkerHashSource for ResolvedExternalCommandMarkerHash<'_> {
    fn kind() -> &'static str {
        "ResolvedExternalCommandMarkerHash"
    }

    fn id(&self) -> anyhow::Result<Option<String>> {
        Ok(None)
    }

    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind> {
        Ok(HashFromString(serde_json::to_string(self)?))
    }
}

#[derive(Serialize)]
pub struct GenerateQuickJSCrateCommandMarkerHash<'a> {
    pub build_dir: &'a Path,
    pub command: &'a GenerateQuickJSCrate,
}

impl TaskResultMarkerHashSource for GenerateQuickJSCrateCommandMarkerHash<'_> {
    fn kind() -> &'static str {
        "GenerateQuickJSCrateCommandMarkerHash"
    }

    fn id(&self) -> anyhow::Result<Option<String>> {
        Ok(None)
    }

    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind> {
        Ok(HashFromString(serde_json::to_string(self)?))
    }
}

#[derive(Serialize)]
pub struct GenerateQuickJSDTSCommandMarkerHash<'a> {
    pub build_dir: &'a Path,
    pub command: &'a GenerateQuickJSDTS,
}

impl TaskResultMarkerHashSource for GenerateQuickJSDTSCommandMarkerHash<'_> {
    fn kind() -> &'static str {
        "GenerateQuickJSDTSCommandMarkerHash"
    }

    fn id(&self) -> anyhow::Result<Option<String>> {
        Ok(None)
    }

    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind> {
        Ok(HashFromString(serde_json::to_string(self)?))
    }
}

#[derive(Serialize)]
pub struct AgentWrapperCommandMarkerHash<'a> {
    pub build_dir: &'a Path,
    pub command: &'a GenerateAgentWrapper,
}

impl TaskResultMarkerHashSource for AgentWrapperCommandMarkerHash<'_> {
    fn kind() -> &'static str {
        "AgentWrapperCommandMarkerHash"
    }

    fn id(&self) -> anyhow::Result<Option<String>> {
        Ok(None)
    }

    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind> {
        Ok(HashFromString(serde_json::to_string(self)?))
    }
}

#[derive(Serialize)]
pub struct ComposeAgentWrapperCommandMarkerHash<'a> {
    pub build_dir: &'a Path,
    pub command: &'a ComposeAgentWrapper,
}

impl TaskResultMarkerHashSource for ComposeAgentWrapperCommandMarkerHash<'_> {
    fn kind() -> &'static str {
        "ComposeAgentWrapperCommandMarkerHash"
    }

    fn id(&self) -> anyhow::Result<Option<String>> {
        Ok(None)
    }

    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind> {
        Ok(HashFromString(serde_json::to_string(self)?))
    }
}

#[derive(Serialize)]
pub struct InjectToPrebuiltQuickJsCommandMarkerHash<'a> {
    pub build_dir: &'a Path,
    pub command: &'a InjectToPrebuiltQuickJs,
}

impl TaskResultMarkerHashSource for InjectToPrebuiltQuickJsCommandMarkerHash<'_> {
    fn kind() -> &'static str {
        "InjectToPrebuiltQuickJsCommandMarkerHash"
    }

    fn id(&self) -> anyhow::Result<Option<String>> {
        Ok(None)
    }

    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind> {
        Ok(HashFromString(serde_json::to_string(self)?))
    }
}

pub struct ComponentGeneratorMarkerHash<'a> {
    pub component_name: &'a AppComponentName,
    pub generator_kind: &'a str,
}

impl TaskResultMarkerHashSource for ComponentGeneratorMarkerHash<'_> {
    fn kind() -> &'static str {
        "ComponentGeneratorMarkerHash"
    }

    fn id(&self) -> anyhow::Result<Option<String>> {
        Ok(None)
    }

    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind> {
        Ok(HashFromString(format!(
            "{}-{}",
            self.component_name, self.generator_kind
        )))
    }
}

pub struct LinkRpcMarkerHash<'a> {
    pub component_name: &'a AppComponentName,
    pub static_wasm_rpc_dependencies: &'a BTreeSet<&'a DependentComponent>,
    pub dynamic_wasm_rpc_dependencies: &'a BTreeSet<&'a DependentComponent>,
    pub library_dependencies: &'a BTreeSet<&'a DependentComponent>,
}

impl TaskResultMarkerHashSource for LinkRpcMarkerHash<'_> {
    fn kind() -> &'static str {
        "RpcLinkMarkerHash"
    }

    fn id(&self) -> anyhow::Result<Option<String>> {
        Ok(Some(self.component_name.to_string()))
    }

    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind> {
        #[derive(Serialize)]
        struct SerializedMarker<'a> {
            component_name: &'a str,
            static_wasm_rpc_deps: Vec<String>,
            dynamic_wasm_rpc_deps: Vec<String>,
            library_deps: Vec<String>,
        }

        Ok(HashFromString(serde_json::to_string(&SerializedMarker {
            component_name: self.component_name.as_str(),
            static_wasm_rpc_deps: self
                .static_wasm_rpc_dependencies
                .iter()
                .map(|dep| dep.source.to_string())
                .collect(),
            dynamic_wasm_rpc_deps: self
                .dynamic_wasm_rpc_dependencies
                .iter()
                .map(|dep| dep.source.to_string())
                .collect(),
            library_deps: self
                .library_dependencies
                .iter()
                .map(|dep| dep.source.to_string())
                .collect(),
        })?))
    }
}

pub struct AddMetadataMarkerHash<'a> {
    pub component_name: &'a AppComponentName,
    pub root_package_name: PackageName,
}

impl TaskResultMarkerHashSource for AddMetadataMarkerHash<'_> {
    fn kind() -> &'static str {
        "AddMetadataMarkerHash"
    }

    fn id(&self) -> anyhow::Result<Option<String>> {
        Ok(Some(self.component_name.to_string()))
    }

    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind> {
        Ok(HashFromString(self.root_package_name.to_string()))
    }
}

pub struct GetServerComponentHash<'a> {
    pub project_id: Option<&'a ProjectId>,
    pub component_name: &'a ComponentName,
    pub component_version: u64,
    // NOTE: use None for querying
    pub component_hash: Option<&'a str>,
}

impl TaskResultMarkerHashSource for GetServerComponentHash<'_> {
    fn kind() -> &'static str {
        "GetServerComponentHash"
    }

    fn id(&self) -> anyhow::Result<Option<String>> {
        Ok(Some(format!(
            "{:?}#{}#{}",
            self.project_id, self.component_name, self.component_version
        )))
    }

    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind> {
        match self.component_hash {
            Some(hash) => Ok(Hash(hash.to_string())),
            None => bail!("Missing precalculated hash for {}", self.component_name),
        }
    }
}

pub struct GetServerIfsFileHash<'a> {
    pub project_id: Option<&'a ProjectId>,
    pub component_name: &'a ComponentName,
    pub component_version: u64,
    pub target_path: &'a str,
    // NOTE: use None for querying
    pub file_hash: Option<&'a str>,
}

impl TaskResultMarkerHashSource for GetServerIfsFileHash<'_> {
    fn kind() -> &'static str {
        "GetServerIfsFileHash"
    }

    fn id(&self) -> anyhow::Result<Option<String>> {
        Ok(Some(format!(
            "{:?}#{}#{}#{}",
            self.project_id, self.component_name, self.component_version, self.target_path
        )))
    }

    fn source(&self) -> anyhow::Result<TaskResultMarkerHashSourceKind> {
        match self.file_hash {
            Some(hash) => Ok(Hash(hash.to_string())),
            None => bail!(
                "Missing precalculated hash for {} - {}",
                self.component_name,
                self.target_path
            ),
        }
    }
}

pub struct TaskResultMarker {
    kind: &'static str,
    id: String,
    hash_input: String,
    marker_file_path: PathBuf,
    hash_hex: String,
    previous_result: Option<TaskResult>,
}

impl TaskResultMarker {
    pub fn new<T: TaskResultMarkerHashSource>(dir: &Path, task: T) -> anyhow::Result<Self> {
        let (hash_input, hash_hex) = match task.source()? {
            HashFromString(hash_input) => {
                let mut hasher = blake3::Hasher::new();
                hasher.update(hash_input.as_bytes());
                (hash_input, hasher.finalize().to_hex().to_string())
            }
            Hash(hash) => (hash.clone(), hash),
        };

        let (id_hash_hex, id) = {
            match task.id()? {
                Some(id) => (Self::id_hash_hex::<T>(&id), id),
                None => (hash_hex.clone(), hash_input.clone()),
            }
        };

        let (marker_file_path, marker_file_exists, previous_result) =
            Self::load_previous_result(dir, &id_hash_hex)?;

        let task_result_marker = Self {
            kind: T::kind(),
            id,
            hash_input,
            marker_file_path,
            hash_hex,
            previous_result,
        };

        if marker_file_exists && !task_result_marker.is_up_to_date() {
            fs::remove(&task_result_marker.marker_file_path)?;
        }

        Ok(task_result_marker)
    }

    pub fn get_hash<T: TaskResultMarkerHashSource>(
        dir: &Path,
        task: T,
    ) -> anyhow::Result<Option<String>> {
        let id_hash_hex = {
            match task.id()? {
                Some(id) => Self::id_hash_hex::<T>(&id),
                None => bail!("missing id for get_hash, task kind: {}", T::kind()),
            }
        };

        let (_marker_file_path, _marker_file_exists, previous_result) =
            Self::load_previous_result(dir, &id_hash_hex)?;

        Ok(previous_result.map(|previous_result| previous_result.hash_hex))
    }

    fn id_hash_hex<T: TaskResultMarkerHashSource>(id: &str) -> String {
        let mut hasher = blake3::Hasher::new();
        hasher.update(T::kind().as_bytes());
        hasher.update(id.as_bytes());
        hasher.finalize().to_hex().to_string()
    }

    fn load_previous_result(
        dir: &Path,
        id_hash_hex: &str,
    ) -> anyhow::Result<(PathBuf, bool, Option<TaskResult>)> {
        let marker_file_path = dir.join(id_hash_hex);
        let marker_file_exists = marker_file_path.exists();

        let previous_result = {
            if marker_file_exists {
                match serde_json::from_str::<TaskResult>(&fs::read_to_string(&marker_file_path)?) {
                    Ok(result) => Some(result),
                    Err(err) => {
                        log_warn_action(
                            "Ignoring",
                            format!(
                                "invalid task marker {}: {}",
                                marker_file_path.display(),
                                err
                            ),
                        );
                        None
                    }
                }
            } else {
                None
            }
        };

        Ok((marker_file_path, marker_file_exists, previous_result))
    }

    pub fn is_up_to_date(&self) -> bool {
        match &self.previous_result {
            Some(previous_result) => {
                previous_result.hash_hex == self.hash_hex && previous_result.success
            }
            None => false,
        }
    }

    pub fn success(self) -> anyhow::Result<()> {
        self.save_marker_file(true)
    }

    pub fn failure(self) -> anyhow::Result<()> {
        self.save_marker_file(false)
    }

    fn save_marker_file(self, success: bool) -> anyhow::Result<()> {
        fs::write_str(
            &self.marker_file_path,
            &serde_json::to_string_pretty(&TaskResult {
                // TODO: setting kind, id and hash_input could be driven by a debug flag, env or build
                kind: Some(self.kind.to_string()),
                id: Some(self.id),
                hash_input: Some(self.hash_input),
                hash_hex: self.hash_hex,
                success,
            })?,
        )
    }

    pub fn result<T>(self, result: anyhow::Result<T>) -> anyhow::Result<T> {
        match result {
            Ok(result) => {
                self.success()?;
                Ok(result)
            }
            Err(source_err) => {
                self.failure().with_context(|| {
                    anyhow!(
                        "Failed to save failure marker for source error: {:?}",
                        source_err,
                    )
                })?;
                Err(source_err)
            }
        }
    }
}