a3s-flow 1.1.0

Durable workflow engine and Rust SDK for A3S
Documentation
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
use std::ffi::{OsStr, OsString};
use std::fs;
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, ExitStatus, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::{Duration, SystemTime};

use sha2::{Digest, Sha256};

const BUN_ENV: &str = "A3S_FLOW_BUN";
pub(crate) const BUN_SUPERVISOR_COMMAND: &str = "__run-bun-supervised";
const SUPERVISOR_POLL_INTERVAL: Duration = Duration::from_millis(10);
const BUN_FINGERPRINT_BUFFER_BYTES: usize = 64 * 1024;
const MAX_STABLE_BUN_READ_ATTEMPTS: usize = 3;
const BUN_IDENTITY_DOMAIN: &[u8] = b"a3s.flow.native_ts.compiler.backend.v1";

#[derive(Debug, Clone, PartialEq, Eq)]
struct BunFileMetadata {
    length: u64,
    modified: Option<SystemTime>,
    #[cfg(not(unix))]
    created: Option<SystemTime>,
    #[cfg(unix)]
    device: u64,
    #[cfg(unix)]
    inode: u64,
    #[cfg(unix)]
    changed_seconds: i64,
    #[cfg(unix)]
    changed_nanoseconds: i64,
}

#[derive(Debug)]
pub(super) struct ResolvedBun {
    path: PathBuf,
    fingerprint: String,
    metadata: BunFileMetadata,
}

impl From<&fs::Metadata> for BunFileMetadata {
    fn from(metadata: &fs::Metadata) -> Self {
        #[cfg(unix)]
        use std::os::unix::fs::MetadataExt;

        Self {
            length: metadata.len(),
            modified: metadata.modified().ok(),
            #[cfg(not(unix))]
            created: metadata.created().ok(),
            #[cfg(unix)]
            device: metadata.dev(),
            #[cfg(unix)]
            inode: metadata.ino(),
            #[cfg(unix)]
            changed_seconds: metadata.ctime(),
            #[cfg(unix)]
            changed_nanoseconds: metadata.ctime_nsec(),
        }
    }
}

impl ResolvedBun {
    pub(super) fn resolve() -> Result<Self, String> {
        let configured = std::env::var_os(BUN_ENV).unwrap_or_else(|| OsString::from("bun"));
        let path = resolve_executable(&configured)?;
        let (fingerprint, metadata) = fingerprint_bun(&path)?;
        Ok(Self {
            path,
            fingerprint,
            metadata,
        })
    }

    pub(super) fn compiler_identity(&self) -> String {
        format!("bun-sha256:{}", self.fingerprint)
    }

    pub(super) fn run(
        &self,
        working_directory: &Path,
        arguments: impl IntoIterator<Item = OsString>,
    ) -> Result<(), String> {
        run_bun(working_directory, &self.path, arguments)?;
        self.verify_unchanged()
    }

    fn verify_unchanged(&self) -> Result<(), String> {
        let current = bun_metadata(&self.path)?;
        if current != self.metadata {
            return Err(format!(
                "Bun executable {} changed while the compiler command was running",
                self.path.display()
            ));
        }
        Ok(())
    }
}

fn run_bun(
    working_directory: &Path,
    binary: &Path,
    arguments: impl IntoIterator<Item = OsString>,
) -> Result<(), String> {
    let executable = std::env::current_exe()
        .map_err(|error| format!("could not resolve the compiler executable: {error}"))?;
    let arguments = arguments.into_iter().collect::<Vec<_>>();
    let mut supervisor = Command::new(&executable)
        .arg(BUN_SUPERVISOR_COMMAND)
        .arg(working_directory)
        .arg(binary)
        .args(&arguments)
        .current_dir(working_directory)
        .stdin(Stdio::piped())
        .stdout(Stdio::null())
        .stderr(Stdio::inherit())
        .spawn()
        .map_err(|error| {
            format!(
                "could not start Bun supervisor {}: {error}",
                executable.display()
            )
        })?;
    // Keeping this pipe open is the supervisor's parent-liveness signal. If
    // Flow cancels or kills this compiler process, the operating system closes
    // the handle and the supervisor terminates and reaps Bun.
    let parent_liveness = supervisor.stdin.take().ok_or_else(|| {
        let _ = supervisor.kill();
        let _ = supervisor.wait();
        "Bun supervisor did not expose its parent-liveness pipe".to_string()
    })?;
    let status = supervisor.wait().map_err(|error| {
        let _ = supervisor.kill();
        let _ = supervisor.wait();
        format!("could not wait for Bun supervisor: {error}")
    })?;
    drop(parent_liveness);
    if !status.success() {
        return Err(format!(
            "Bun supervisor exited unsuccessfully with status {status}; Bun executable was {} (override with {BUN_ENV})",
            binary.display()
        ));
    }
    Ok(())
}

pub(crate) fn run_bun_supervised(
    working_directory: &Path,
    binary: &OsStr,
    arguments: &[OsString],
) -> Result<(), String> {
    let child = Command::new(binary)
        .args(arguments)
        .current_dir(working_directory)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::inherit())
        .spawn()
        .map_err(|error| {
            format!(
                "could not start Bun executable {}: {error}; set {BUN_ENV} to an explicit path",
                Path::new(binary).display()
            )
        })?;
    let parent_disconnected = Arc::new(AtomicBool::new(false));
    watch_parent_liveness(Arc::clone(&parent_disconnected));
    let status = wait_for_supervised_child(child, &parent_disconnected)?;
    if !status.success() {
        return Err(format!("Bun exited unsuccessfully with status {status}"));
    }
    Ok(())
}

fn watch_parent_liveness(parent_disconnected: Arc<AtomicBool>) {
    thread::spawn(move || {
        let mut input = io::stdin().lock();
        let mut buffer = [0_u8; 1];
        loop {
            match input.read(&mut buffer) {
                Ok(0) => break,
                Ok(_) => {}
                Err(error) if error.kind() == io::ErrorKind::Interrupted => {}
                Err(_) => break,
            }
        }
        parent_disconnected.store(true, Ordering::Release);
    });
}

fn wait_for_supervised_child(
    mut child: Child,
    parent_disconnected: &AtomicBool,
) -> Result<ExitStatus, String> {
    loop {
        match child.try_wait() {
            Ok(Some(status)) => return Ok(status),
            Ok(None) => {}
            Err(error) => {
                let _ = child.kill();
                let _ = child.wait();
                return Err(format!("could not inspect the Bun process: {error}"));
            }
        }

        if parent_disconnected.load(Ordering::Acquire) {
            let kill_result = child.kill();
            let wait_result = child.wait();
            if let Err(error) = kill_result {
                if wait_result.as_ref().is_err() {
                    return Err(format!(
                        "compiler parent disconnected, but Bun could not be terminated: {error}"
                    ));
                }
            }
            if let Err(error) = wait_result {
                return Err(format!(
                    "compiler parent disconnected, but Bun could not be reaped: {error}"
                ));
            }
            return Err("compiler parent disconnected while Bun was running".to_string());
        }

        thread::sleep(SUPERVISOR_POLL_INTERVAL);
    }
}

fn resolve_executable(configured: &OsStr) -> Result<PathBuf, String> {
    let configured_path = Path::new(configured);
    let has_directory = configured_path.is_absolute()
        || configured_path
            .parent()
            .is_some_and(|parent| !parent.as_os_str().is_empty());
    if has_directory {
        for candidate in executable_candidates(configured_path) {
            if let Ok(path) = canonical_executable(&candidate) {
                return Ok(path);
            }
        }
        return Err(format!(
            "Bun executable {} could not be resolved to an executable file; set {BUN_ENV} to an explicit path",
            configured_path.display()
        ));
    }

    let Some(path_value) = std::env::var_os("PATH") else {
        return Err(format!(
            "Bun executable {} was not found because PATH is unset; set {BUN_ENV} to an explicit path",
            configured_path.display()
        ));
    };
    for directory in std::env::split_paths(&path_value) {
        for candidate in executable_candidates(&directory.join(configured_path)) {
            if let Ok(path) = canonical_executable(&candidate) {
                return Ok(path);
            }
        }
    }

    Err(format!(
        "Bun executable {} was not found on PATH; set {BUN_ENV} to an explicit path",
        configured_path.display()
    ))
}

fn executable_candidates(path: &Path) -> Vec<PathBuf> {
    let candidates = vec![path.to_path_buf()];
    #[cfg(not(windows))]
    return candidates;

    #[cfg(windows)]
    {
        if path.extension().is_some() {
            return candidates;
        }
        let mut candidates = candidates;
        let path_extensions =
            std::env::var_os("PATHEXT").unwrap_or_else(|| OsString::from(".COM;.EXE;.BAT;.CMD"));
        for extension in path_extensions.to_string_lossy().split(';') {
            let extension = extension.trim().trim_start_matches('.');
            if extension.is_empty() {
                continue;
            }
            let mut candidate = path.to_path_buf();
            candidate.set_extension(extension);
            if !candidates.contains(&candidate) {
                candidates.push(candidate);
            }
        }
        candidates
    }
}

fn canonical_executable(path: &Path) -> Result<PathBuf, String> {
    let canonical = fs::canonicalize(path).map_err(|error| {
        format!(
            "could not resolve Bun executable {}: {error}",
            path.display()
        )
    })?;
    let metadata = fs::metadata(&canonical).map_err(|error| {
        format!(
            "could not inspect Bun executable {}: {error}",
            canonical.display()
        )
    })?;
    if !metadata.is_file() {
        return Err(format!(
            "Bun executable {} is not a regular file",
            canonical.display()
        ));
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if metadata.permissions().mode() & 0o111 == 0 {
            return Err(format!(
                "Bun executable {} does not have an execute permission bit",
                canonical.display()
            ));
        }
    }
    Ok(canonical)
}

fn fingerprint_bun(path: &Path) -> Result<(String, BunFileMetadata), String> {
    for _ in 0..MAX_STABLE_BUN_READ_ATTEMPTS {
        let before = bun_metadata(path)?;
        let mut file = fs::File::open(path).map_err(|error| {
            format!("could not read Bun executable {}: {error}", path.display())
        })?;
        let mut hasher = Sha256::new();
        hasher.update((BUN_IDENTITY_DOMAIN.len() as u64).to_le_bytes());
        hasher.update(BUN_IDENTITY_DOMAIN);
        hasher.update(before.length.to_le_bytes());
        let mut bytes_read = 0_u64;
        let mut buffer = vec![0_u8; BUN_FINGERPRINT_BUFFER_BYTES];
        loop {
            let count = file.read(&mut buffer).map_err(|error| {
                format!(
                    "could not fingerprint Bun executable {}: {error}",
                    path.display()
                )
            })?;
            if count == 0 {
                break;
            }
            bytes_read = bytes_read.checked_add(count as u64).ok_or_else(|| {
                format!(
                    "Bun executable {} is too large to fingerprint",
                    path.display()
                )
            })?;
            hasher.update(&buffer[..count]);
        }
        let after = bun_metadata(path)?;
        if before == after && bytes_read == after.length {
            return Ok((hex_lower(&hasher.finalize()), after));
        }
    }

    Err(format!(
        "Bun executable {} changed repeatedly while it was being fingerprinted",
        path.display()
    ))
}

fn bun_metadata(path: &Path) -> Result<BunFileMetadata, String> {
    let metadata = fs::metadata(path).map_err(|error| {
        format!(
            "could not inspect Bun executable {}: {error}",
            path.display()
        )
    })?;
    if !metadata.is_file() {
        return Err(format!(
            "Bun executable {} is not a regular file",
            path.display()
        ));
    }
    Ok(BunFileMetadata::from(&metadata))
}

fn hex_lower(bytes: &[u8]) -> String {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let mut encoded = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        encoded.push(HEX[(byte >> 4) as usize] as char);
        encoded.push(HEX[(byte & 0x0f) as usize] as char);
    }
    encoded
}

#[cfg(test)]
mod tests {
    use super::{fingerprint_bun, wait_for_supervised_child};
    use std::process::{Child, Command, Stdio};
    use std::sync::atomic::AtomicBool;
    use std::time::{Duration, Instant};

    #[test]
    fn backend_fingerprint_changes_with_executable_contents() {
        let directory = tempfile::tempdir().unwrap();
        let executable = directory.path().join("bun-test-binary");
        std::fs::write(&executable, b"first backend").unwrap();
        let first = fingerprint_bun(&executable).unwrap().0;
        std::fs::write(&executable, b"second backend").unwrap();
        let second = fingerprint_bun(&executable).unwrap().0;

        assert_ne!(first, second);
        assert!(first.len() == 64 && second.len() == 64);
    }

    #[cfg(any(unix, windows))]
    #[test]
    fn supervisor_terminates_and_reaps_bun_when_its_parent_disconnects() {
        let child = blocking_test_child();
        let started = Instant::now();

        let error = wait_for_supervised_child(child, &AtomicBool::new(true)).unwrap_err();

        assert!(error.contains("parent disconnected"));
        assert!(
            started.elapsed() < Duration::from_secs(5),
            "supervisor must not wait for the blocking child to exit naturally"
        );
    }

    #[cfg(unix)]
    fn blocking_test_child() -> Child {
        Command::new("sh")
            .args(["-c", "exec sleep 30"])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .unwrap()
    }

    #[cfg(windows)]
    fn blocking_test_child() -> Child {
        Command::new("powershell.exe")
            .args(["-NoProfile", "-Command", "Start-Sleep -Seconds 30"])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .spawn()
            .unwrap()
    }
}