Skip to content

Commit 0ecdade

Browse files
committed
Refine batch failure handling
Make batch mode continue by default and add --batch-fail-on-error for callers that want fail-fast behavior. Count actual function successes and failures based on FunctionRunResult.success instead of treating every successful runner invocation as a successful function run.\n\nAdd integration coverage for default continue behavior, fail-fast behavior, and accurate batch summaries. Also update integration tests to use assert_cmd::cargo::cargo_bin! instead of the deprecated Command::cargo_bin helper.\n\nVerified with:\n- cargo test batch_\n- cargo test Assisted-By: devx/c659e918-9568-4750-b122-e3890447348a
1 parent 02f146c commit 0ecdade

3 files changed

Lines changed: 143 additions & 49 deletions

File tree

‎src/container.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ mod tests {
148148
let input = BytesContainer::new(BytesContainerType::Input, Codec::Json, raw.clone())
149149
.expect("valid JSON input");
150150

151-
assert_eq!(String::from_utf8(input.raw).unwrap(), String::from_utf8(raw).unwrap());
151+
assert_eq!(
152+
String::from_utf8(input.raw).unwrap(),
153+
String::from_utf8(raw).unwrap()
154+
);
152155
}
153156
}

‎src/main.rs‎

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ struct Opts {
6666
#[clap(short, long)]
6767
batch: bool,
6868

69-
/// In batch mode, continue processing on individual input errors (default: false)
69+
/// In batch mode, fail fast on individual input errors (default: false)
7070
#[clap(long)]
71-
batch_continue_on_error: bool,
71+
batch_fail_on_error: bool,
7272
}
7373

7474
impl Opts {
@@ -227,30 +227,34 @@ fn run_batch_mode(
227227
));
228228
};
229229

230-
// Pre-calculate scale factor (constant across all inputs in batch)
230+
// Load schema/query once; scale factor is computed per input.
231231
let schema_string = opts.read_schema_to_string().transpose()?;
232232
let query_string = opts.read_query_to_string().transpose()?;
233233

234234
// Disable profiling in batch mode for performance
235235
let profile_opts = None;
236236

237237
let mut line_num = 0;
238+
let mut processed_count = 0;
238239
let mut success_count = 0;
239-
let mut error_count = 0;
240+
let mut failed_count = 0;
240241

241242
for line_result in input_reader.lines() {
242243
line_num += 1;
243244

244245
let line = match line_result {
245246
Ok(l) => l,
246247
Err(e) => {
247-
error_count += 1;
248-
if opts.batch_continue_on_error {
248+
failed_count += 1;
249+
if opts.batch_fail_on_error {
250+
return Err(e.into());
251+
} else {
249252
eprintln!("Error reading line {}: {}", line_num, e);
250-
println!(r#"{{"success":false,"error":"Error reading input: {}"}}"#, e);
253+
println!(
254+
r#"{{"success":false,"error":"Error reading input: {}"}}"#,
255+
e
256+
);
251257
continue;
252-
} else {
253-
return Err(e.into());
254258
}
255259
}
256260
};
@@ -260,51 +264,53 @@ fn run_batch_mode(
260264
continue;
261265
}
262266

267+
processed_count += 1;
268+
263269
// Parse input
264-
let input = match BytesContainer::new(
265-
BytesContainerType::Input,
266-
codec,
267-
line.into_bytes(),
268-
) {
270+
let input = match BytesContainer::new(BytesContainerType::Input, codec, line.into_bytes()) {
269271
Ok(i) => i,
270272
Err(e) => {
271-
error_count += 1;
272-
if opts.batch_continue_on_error {
273+
failed_count += 1;
274+
if opts.batch_fail_on_error {
275+
return Err(e);
276+
} else {
273277
eprintln!("Error parsing line {}: {}", line_num, e);
274278
println!(r#"{{"success":false,"error":"Invalid JSON input: {}"}}"#, e);
275279
continue;
276-
} else {
277-
return Err(e);
278280
}
279281
}
280282
};
281283

282284
// Calculate scale factor for this input
283-
let scale_factor = if let (Some(ref schema_string), Some(ref query_string), Some(ref json_value)) =
284-
(&schema_string, &query_string, &input.json_value)
285-
{
286-
match BluejaySchemaAnalyzer::analyze_schema_definition(
287-
schema_string,
288-
opts.schema_path.as_ref().and_then(|p| p.to_str()),
289-
query_string,
290-
opts.query_path.as_ref().and_then(|p| p.to_str()),
291-
json_value,
292-
) {
293-
Ok(sf) => sf,
294-
Err(e) => {
295-
error_count += 1;
296-
if opts.batch_continue_on_error {
297-
eprintln!("Error analyzing schema for line {}: {}", line_num, e);
298-
println!(r#"{{"success":false,"error":"Schema analysis failed: {}"}}"#, e);
299-
continue;
300-
} else {
301-
return Err(e);
285+
let scale_factor =
286+
if let (Some(ref schema_string), Some(ref query_string), Some(ref json_value)) =
287+
(&schema_string, &query_string, &input.json_value)
288+
{
289+
match BluejaySchemaAnalyzer::analyze_schema_definition(
290+
schema_string,
291+
opts.schema_path.as_ref().and_then(|p| p.to_str()),
292+
query_string,
293+
opts.query_path.as_ref().and_then(|p| p.to_str()),
294+
json_value,
295+
) {
296+
Ok(sf) => sf,
297+
Err(e) => {
298+
failed_count += 1;
299+
if opts.batch_fail_on_error {
300+
return Err(e);
301+
} else {
302+
eprintln!("Error analyzing schema for line {}: {}", line_num, e);
303+
println!(
304+
r#"{{"success":false,"error":"Schema analysis failed: {}"}}"#,
305+
e
306+
);
307+
continue;
308+
}
302309
}
303310
}
304-
}
305-
} else {
306-
DEFAULT_SCALE_FACTOR
307-
};
311+
} else {
312+
DEFAULT_SCALE_FACTOR
313+
};
308314

309315
// Run function (reusing engine/module!)
310316
let result = run(FunctionRunParams {
@@ -320,27 +326,42 @@ fn run_batch_mode(
320326
// Output result immediately (streaming JSONL - compact format for line-by-line parsing)
321327
match result {
322328
Ok(function_result) => {
323-
success_count += 1;
329+
let function_succeeded = function_result.success;
330+
if function_succeeded {
331+
success_count += 1;
332+
} else {
333+
failed_count += 1;
334+
}
335+
324336
// Use compact JSON (not pretty-printed) for JSONL format
325337
let compact_json = serde_json::to_string(&function_result)
326338
.unwrap_or_else(|error| error.to_string());
327339
println!("{}", compact_json);
340+
341+
if !function_succeeded && opts.batch_fail_on_error {
342+
anyhow::bail!(
343+
"Function execution failed on line {}. Review the logs for more information.",
344+
line_num
345+
);
346+
}
328347
}
329348
Err(e) => {
330-
error_count += 1;
331-
if opts.batch_continue_on_error {
349+
failed_count += 1;
350+
if opts.batch_fail_on_error {
351+
return Err(e);
352+
} else {
332353
eprintln!("Error executing line {}: {}", line_num, e);
333354
println!(r#"{{"success":false,"error":"Execution failed: {}"}}"#, e);
334-
} else {
335-
return Err(e);
336355
}
337356
}
338357
}
339358
}
340359

341360
// Log summary to stderr (so it doesn't interfere with JSONL output on stdout)
342-
eprintln!("Batch complete: {} inputs processed, {} successful, {} errors",
343-
line_num, success_count, error_count);
361+
eprintln!(
362+
"Batch complete: {} inputs processed, {} successful, {} failed",
363+
processed_count, success_count, failed_count
364+
);
344365

345366
Ok(())
346367
}

‎tests/integration_tests.rs‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,69 @@ mod tests {
6464
Ok(())
6565
}
6666

67+
#[test]
68+
fn batch_continues_by_default_and_counts_function_failures() -> Result<()> {
69+
let mut cmd = Command::new(cargo_bin!());
70+
let input_file = temp_batch_input("{\"code\":0}\n{\"code\":1}\n{\"code\":0}\n")?;
71+
72+
cmd.args(["--function", "tests/fixtures/build/exit_code.wasm"])
73+
.arg("--batch")
74+
.arg("--input")
75+
.arg(input_file.as_os_str());
76+
77+
let output = cmd.output()?;
78+
79+
assert!(output.status.success());
80+
81+
let stdout = String::from_utf8(output.stdout)?;
82+
let results = stdout
83+
.lines()
84+
.map(serde_json::from_str::<serde_json::Value>)
85+
.collect::<Result<Vec<_>, _>>()?;
86+
assert_eq!(results.len(), 3);
87+
assert_eq!(results[0]["success"], true);
88+
assert_eq!(results[1]["success"], false);
89+
assert_eq!(results[2]["success"], true);
90+
91+
assert_eq!(
92+
String::from_utf8(output.stderr)?,
93+
"Batch complete: 3 inputs processed, 2 successful, 1 failed\n"
94+
);
95+
96+
Ok(())
97+
}
98+
99+
#[test]
100+
fn batch_fail_on_error_stops_after_function_failure() -> Result<()> {
101+
let mut cmd = Command::new(cargo_bin!());
102+
let input_file = temp_batch_input("{\"code\":0}\n{\"code\":1}\n{\"code\":0}\n")?;
103+
104+
cmd.args(["--function", "tests/fixtures/build/exit_code.wasm"])
105+
.arg("--batch")
106+
.arg("--batch-fail-on-error")
107+
.arg("--input")
108+
.arg(input_file.as_os_str());
109+
110+
let output = cmd.output()?;
111+
112+
assert!(!output.status.success());
113+
114+
let stdout = String::from_utf8(output.stdout)?;
115+
let results = stdout
116+
.lines()
117+
.map(serde_json::from_str::<serde_json::Value>)
118+
.collect::<Result<Vec<_>, _>>()?;
119+
assert_eq!(results.len(), 2);
120+
assert_eq!(results[0]["success"], true);
121+
assert_eq!(results[1]["success"], false);
122+
123+
assert!(String::from_utf8(output.stderr)?.contains(
124+
"Function execution failed on line 2. Review the logs for more information."
125+
));
126+
127+
Ok(())
128+
}
129+
67130
#[test]
68131
fn run_no_opts() -> Result<()> {
69132
let mut cmd = Command::new(cargo_bin!());
@@ -265,6 +328,13 @@ mod tests {
265328
Ok(file)
266329
}
267330

331+
fn temp_batch_input(jsonl: &str) -> Result<assert_fs::NamedTempFile> {
332+
let file = assert_fs::NamedTempFile::new("input.jsonl")?;
333+
file.write_str(jsonl)?;
334+
335+
Ok(file)
336+
}
337+
268338
#[test]
269339
fn test_scale_limits_analyzer_use_defaults_when_query_and_schema_not_provided() -> Result<()> {
270340
let mut cmd = Command::new(cargo_bin!());

0 commit comments

Comments
 (0)