harn-hostlib 0.10.130

Opt-in code-intelligence and deterministic-tool host builtins for the Harn VM
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
//! Event-driven output matching for one background command.

use std::time::Duration;

use harn_vm::{VmDictExt, VmValue};
use regex_automata::{hybrid::dfa::DFA, hybrid::LazyStateID, Input};

use crate::error::HostlibError;
use crate::tools::payload::{
    optional_bool, optional_string, optional_u64, require_dict_arg, require_string,
};

pub(crate) const NAME: &str = "hostlib_tools_wait_command_output";

#[derive(Clone, Copy)]
enum Source {
    Stdout,
    Stderr,
    Combined,
}

impl Source {
    fn parse(value: Option<String>) -> Result<Self, HostlibError> {
        match value.as_deref().unwrap_or("combined") {
            "stdout" => Ok(Self::Stdout),
            "stderr" => Ok(Self::Stderr),
            "combined" => Ok(Self::Combined),
            _ => Err(HostlibError::InvalidParameter {
                builtin: NAME,
                param: "source",
                message: "expected stdout, stderr, or combined".to_string(),
            }),
        }
    }

    fn as_str(self) -> &'static str {
        match self {
            Self::Stdout => "stdout",
            Self::Stderr => "stderr",
            Self::Combined => "combined",
        }
    }

    fn bytes(self, state: &super::long_running::OutputState) -> &[u8] {
        match self {
            Self::Stdout => &state.stdout,
            Self::Stderr => &state.stderr,
            Self::Combined => &state.combined,
        }
    }
}

enum Matcher {
    Literal(LiteralMatcher),
    Regex(Box<RegexMatcher>),
}

impl Matcher {
    fn new(pattern: &str, regex: bool, from_offset: usize) -> Result<Self, HostlibError> {
        if pattern.is_empty() {
            return Err(HostlibError::InvalidParameter {
                builtin: NAME,
                param: "pattern",
                message: "must not be empty".to_string(),
            });
        }
        if regex {
            RegexMatcher::new(pattern, from_offset).map(|matcher| Self::Regex(Box::new(matcher)))
        } else {
            Ok(Self::Literal(LiteralMatcher::new(
                pattern.as_bytes(),
                from_offset,
            )))
        }
    }

    fn find(&mut self, bytes: &[u8]) -> Result<Option<(usize, usize)>, HostlibError> {
        match self {
            Self::Literal(matcher) => Ok(matcher.find(bytes)),
            Self::Regex(matcher) => matcher.find(bytes),
        }
    }
}

struct LiteralMatcher {
    pattern: Vec<u8>,
    prefix: Vec<usize>,
    matched: usize,
    processed: usize,
}

impl LiteralMatcher {
    fn new(pattern: &[u8], from_offset: usize) -> Self {
        let mut prefix = vec![0; pattern.len()];
        let mut matched = 0;
        for index in 1..pattern.len() {
            while matched > 0 && pattern[index] != pattern[matched] {
                matched = prefix[matched - 1];
            }
            if pattern[index] == pattern[matched] {
                matched += 1;
            }
            prefix[index] = matched;
        }
        Self {
            pattern: pattern.to_vec(),
            prefix,
            matched: 0,
            processed: from_offset,
        }
    }

    fn find(&mut self, bytes: &[u8]) -> Option<(usize, usize)> {
        while self.processed < bytes.len() {
            let byte = bytes[self.processed];
            while self.matched > 0 && byte != self.pattern[self.matched] {
                self.matched = self.prefix[self.matched - 1];
            }
            if byte == self.pattern[self.matched] {
                self.matched += 1;
            }
            self.processed += 1;
            if self.matched == self.pattern.len() {
                return Some((self.processed - self.pattern.len(), self.processed));
            }
        }
        None
    }
}

struct RegexMatcher {
    dfa: DFA,
    cache: regex_automata::hybrid::dfa::Cache,
    state: Option<LazyStateID>,
    finder: regex::bytes::Regex,
    from_offset: usize,
    processed: usize,
}

impl RegexMatcher {
    fn new(pattern: &str, from_offset: usize) -> Result<Self, HostlibError> {
        let dfa = DFA::new(pattern).map_err(|error| HostlibError::InvalidParameter {
            builtin: NAME,
            param: "pattern",
            message: error.to_string(),
        })?;
        let finder =
            regex::bytes::Regex::new(pattern).map_err(|error| HostlibError::InvalidParameter {
                builtin: NAME,
                param: "pattern",
                message: error.to_string(),
            })?;
        let cache = dfa.create_cache();
        Ok(Self {
            dfa,
            cache,
            state: None,
            finder,
            from_offset,
            processed: from_offset,
        })
    }

    fn find(&mut self, bytes: &[u8]) -> Result<Option<(usize, usize)>, HostlibError> {
        if self.from_offset > bytes.len() {
            return Ok(None);
        }
        if self.state.is_none() {
            self.state = Some(
                self.dfa
                    .start_state_forward(
                        &mut self.cache,
                        &Input::new(bytes).span(self.from_offset..bytes.len()),
                    )
                    .map_err(|error| backend(error.to_string()))?,
            );
        }
        let mut state = self.state.expect("regex state initialized");
        while self.processed < bytes.len() {
            let index = self.processed;
            state = self
                .dfa
                .next_state(&mut self.cache, state, bytes[index])
                .map_err(|error| backend(error.to_string()))?;
            self.processed += 1;
            if state.is_match() {
                self.state = Some(state);
                return Ok(self.exact_match(bytes, index));
            }
        }
        self.state = Some(state);
        let end_state = self
            .dfa
            .next_eoi_state(&mut self.cache, state)
            .map_err(|error| backend(error.to_string()))?;
        if end_state.is_match() {
            return Ok(self.exact_match(bytes, bytes.len()));
        }
        Ok(None)
    }

    fn exact_match(&self, bytes: &[u8], end: usize) -> Option<(usize, usize)> {
        self.finder
            .find(&bytes[self.from_offset..end])
            .map(|found| {
                (
                    self.from_offset + found.start(),
                    self.from_offset + found.end(),
                )
            })
    }
}

/// The bytes a finished command left in its own receipt, for the requested
/// source.
///
/// `combined` needs the fallback. The live output feed keeps an interleaved
/// combined buffer, but the terminal receipt does not populate that field, so
/// asking a completed handle for combined output reads as empty while `stdout`
/// holds the bytes. Concatenating is not the original interleaving, and cannot
/// be: the receipt does not record the order the two streams arrived in. It is
/// still sound for matching, because each stream is searched as itself, so a
/// match means those bytes really were printed on that stream and no match can
/// straddle a seam that never existed.
fn terminal_output(source: Source, result: &VmValue) -> Vec<u8> {
    let Some(map) = result.as_dict() else {
        return Vec::new();
    };
    let field = |key: &str| -> Vec<u8> {
        match map.get(key) {
            Some(VmValue::String(text)) => text.as_bytes().to_vec(),
            _ => Vec::new(),
        }
    };
    match source {
        Source::Stdout => field("stdout"),
        Source::Stderr => field("stderr"),
        Source::Combined => {
            let combined = field("combined");
            if combined.is_empty() {
                let mut merged = field("stdout");
                merged.extend_from_slice(&field("stderr"));
                merged
            } else {
                combined
            }
        }
    }
}

fn backend(message: String) -> HostlibError {
    HostlibError::Backend {
        builtin: NAME,
        message,
    }
}

pub(crate) async fn handle(args: Vec<VmValue>) -> Result<VmValue, HostlibError> {
    let map = require_dict_arg(NAME, &args)?;
    let handle_id = require_string(NAME, &map, "handle_id")?;
    let pattern = require_string(NAME, &map, "pattern")?;
    let source = Source::parse(optional_string(NAME, &map, "source")?)?;
    let regex = optional_bool(NAME, &map, "regex")?.unwrap_or(false);
    let from_offset = usize::try_from(optional_u64(NAME, &map, "from_offset")?.unwrap_or(0))
        .map_err(|_| HostlibError::InvalidParameter {
            builtin: NAME,
            param: "from_offset",
            message: "offset exceeds this platform's addressable output size".to_string(),
        })?;
    let timeout_ms = optional_u64(NAME, &map, "timeout_ms")?;
    let session_id = optional_string(NAME, &map, "session_id")?
        .or_else(harn_vm::current_agent_session_id)
        .unwrap_or_default();
    let mut matcher = Matcher::new(&pattern, regex, from_offset)?;

    if let Some((feed, cwd)) = super::long_running::output_context_for_handle(&handle_id) {
        let timeout = async move {
            match timeout_ms {
                Some(ms) => tokio::time::sleep(Duration::from_millis(ms)).await,
                None => std::future::pending::<()>().await,
            }
        };
        tokio::pin!(timeout);
        loop {
            let notified = feed.notified();
            tokio::pin!(notified);
            // `notify_waiters` does not retain a permit for an unregistered
            // future. Register before inspecting state so publication cannot
            // land in the check/select gap.
            notified.as_mut().enable();
            let ready = {
                let state = feed
                    .state
                    .lock()
                    .unwrap_or_else(|poison| poison.into_inner());
                let bytes = source.bytes(&state);
                if let Some((start, end)) = matcher.find(bytes)? {
                    Some((
                        response(
                            "matched",
                            &handle_id,
                            source,
                            &pattern,
                            Some((start, end, &bytes[start..end])),
                            running_result(&handle_id, &cwd, &state),
                        ),
                        false,
                    ))
                } else {
                    state.terminal.clone().map(|result| {
                        (
                            response("exited", &handle_id, source, &pattern, None, result),
                            true,
                        )
                    })
                }
            };
            if let Some((result, drain_terminal)) = ready {
                if drain_terminal {
                    let _ = super::wait_command::drain_matching_result(&session_id, &handle_id);
                }
                return Ok(result);
            }
            tokio::select! {
                biased;
                () = &mut notified => {}
                () = &mut timeout => {
                    return Ok(response(
                        "timed_out",
                        &handle_id,
                        source,
                        &pattern,
                        None,
                        running_result_from_feed(&handle_id, &cwd, &feed),
                    ));
                }
            }
        }
    }

    if let Some(result) = super::wait_command::drain_matching_result(&session_id, &handle_id) {
        // The command has already finished and its live output feed is gone, so
        // the loop above never ran. Search what it left behind instead of
        // answering without looking.
        //
        // Returning `exited` here unconditionally was the defect: a command that
        // printed the pattern and then finished before the wait attached gave
        // exactly the answer a command that never printed it gives. An agent
        // that starts a build, thinks for a moment, and then waits on it lost
        // that build's output, and no field in the result said so.
        let searched = terminal_output(source, &result);
        let found = matcher.find(&searched)?;
        let status = if found.is_some() { "matched" } else { "exited" };
        return Ok(response(
            status,
            &handle_id,
            source,
            &pattern,
            found.map(|(start, end)| (start, end, &searched[start..end])),
            result,
        ));
    }
    Err(HostlibError::InvalidParameter {
        builtin: NAME,
        param: "handle_id",
        message: format!("unknown background command handle `{handle_id}`"),
    })
}

fn running_result_from_feed(
    handle_id: &str,
    cwd: &std::path::Path,
    feed: &super::long_running::OutputFeed,
) -> VmValue {
    let state = feed
        .state
        .lock()
        .unwrap_or_else(|poison| poison.into_inner());
    running_result(handle_id, cwd, &state)
}

fn running_result(
    handle_id: &str,
    cwd: &std::path::Path,
    state: &super::long_running::OutputState,
) -> VmValue {
    let mut result = harn_vm::value::DictMap::new();
    result.put_str("handle_id", handle_id);
    result.put_str("status", "running");
    result.insert("completed".into(), VmValue::Bool(false));
    result.insert("timed_out".into(), VmValue::Bool(false));
    result.insert("exit_code".into(), VmValue::Nil);
    result.put_str("cwd", crate::tools::args::to_agent_path(cwd));
    result.put_str("stdout", String::from_utf8_lossy(&state.stdout));
    result.put_str("stderr", String::from_utf8_lossy(&state.stderr));
    result.put_str("combined", String::from_utf8_lossy(&state.combined));
    result.insert(
        "byte_count".into(),
        VmValue::Int(state.combined.len() as i64),
    );
    VmValue::dict(result)
}

fn response(
    status: &str,
    handle_id: &str,
    source: Source,
    pattern: &str,
    found: Option<(usize, usize, &[u8])>,
    result: VmValue,
) -> VmValue {
    let mut response = harn_vm::value::DictMap::new();
    response.put_str("status", status);
    response.put_str("handle_id", handle_id);
    response.put_str("source", source.as_str());
    response.put_str("pattern", pattern);
    response.insert("matched".into(), VmValue::Bool(found.is_some()));
    response.insert("timed_out".into(), VmValue::Bool(status == "timed_out"));
    if let Some((start, end, bytes)) = found {
        response.insert("match_start".into(), VmValue::Int(start as i64));
        response.insert("match_end".into(), VmValue::Int(end as i64));
        response.insert("next_offset".into(), VmValue::Int(end as i64));
        response.put_str("match", String::from_utf8_lossy(bytes));
    } else {
        for key in ["match_start", "match_end", "next_offset", "match"] {
            response.insert(key.into(), VmValue::Nil);
        }
    }
    response.insert("result".into(), result);
    VmValue::dict(response)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn literal_matcher_carries_state_across_chunks() {
        let mut matcher = Matcher::new("ready", false, 2).unwrap();
        assert_eq!(matcher.find(b"xxre").unwrap(), None);
        assert_eq!(matcher.find(b"xxready now").unwrap(), Some((2, 7)));
    }

    #[test]
    fn regex_matcher_carries_state_across_chunks() {
        let mut matcher = Matcher::new(r"ready\s+on\s+\d+", true, 0).unwrap();
        assert_eq!(matcher.find(b"ready on").unwrap(), None);
        assert_eq!(matcher.find(b"ready on 4312").unwrap(), Some((0, 10)));
    }
}