Skip to content

Commit b93c65e

Browse files
feat: add timeout parameter to Run command
Co-authored-by: aider (openrouter/x-ai/grok-code-fast-1) <[email protected]>
1 parent b7f729e commit b93c65e

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

kernel/src/main.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use mork::{expr, prefix, sexpr};
22
use mork::space::{transitions, unifications, writes, Space};
33
use mork_frontend::bytestring_parser::Parser;
44
use mork_expr::{item_byte, Tag};
5+
use std::time::{Duration, Instant};
56
use pathmap::PathMap;
67
use pathmap::zipper::{Zipper, ZipperAbsolutePath, ZipperIteration, ZipperMoving};
78
use std::collections::{BTreeSet, HashSet};
@@ -2855,6 +2856,8 @@ enum Commands {
28552856
query_pattern: Option<String>,
28562857
#[arg(long, short = 't')]
28572858
query_template: Option<String>,
2859+
#[arg(long, default_value_t = 0)]
2860+
timeout: u64,
28582861
},
28592862
#[command(arg_required_else_help = true)]
28602863
Convert {
@@ -2947,7 +2950,7 @@ fn main() {
29472950
parse_csv();
29482951
parse_json();
29492952
}
2950-
Commands::Run { input_paths, steps, instrumentation, output_path, query_pattern, query_template } => {
2953+
Commands::Run { input_paths, steps, instrumentation, output_path, query_pattern, query_template, timeout } => {
29512954
#[cfg(debug_assertions)]
29522955
println!("WARNING running in debug, if unintentional, build with --release");
29532956
let mut s = Space::new();
@@ -2958,8 +2961,21 @@ fn main() {
29582961
}
29592962
if instrumentation > 0 { println!("loaded {} expressions", s.btm.val_count()) }
29602963
println!("loaded {:?} ; running and outputing to {:?}", &input_paths, output_path.as_ref().or(Some(&"stdout".to_string())));
2964+
let timeout_duration = if timeout > 0 { Some(Duration::from_secs(timeout)) } else { None };
29612965
let t0 = Instant::now();
2962-
let mut performed = s.metta_calculus(steps);
2966+
let mut performed = 0;
2967+
while performed < steps {
2968+
if let Some(td) = timeout_duration {
2969+
if t0.elapsed() >= td {
2970+
break;
2971+
}
2972+
}
2973+
let p = s.metta_calculus(1);
2974+
performed += p;
2975+
if p == 0 {
2976+
break;
2977+
}
2978+
}
29632979
println!("executing {performed} steps took {} ms (unifications {}, writes {}, transitions {})", t0.elapsed().as_millis(), unsafe { unifications }, unsafe { writes }, unsafe { transitions });
29642980
if instrumentation > 0 { println!("dumping {} expressions", s.btm.val_count()) }
29652981
let query_pattern_expr = query_pattern.as_ref().map(|p| expr!(s, p));

0 commit comments

Comments
 (0)