jev-harness 0.1.6

Zero-overhead System One decision harness and token optimizer for AI coding agents.
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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Semantic Decision Gates powered by TypeSafe Jev System One.
//!
//! Provides ultra-fast micro-decisions (<500µs local, 70-300ms remote) to optimize agentic loops,
//! triage test failures, prevent doomed circular paths, and route model tiers.

use crate::client::JevClient;
use crate::types::*;
use std::collections::HashMap;

/// Truncates a log string preserving the head and tail while guaranteeing valid UTF-8 char boundaries.
pub fn safe_truncate_head_tail(s: &str, max_head: usize, max_tail: usize) -> String {
    if s.len() <= max_head + max_tail {
        return s.to_string();
    }

    let mut head_end = max_head;
    while head_end > 0 && !s.is_char_boundary(head_end) {
        head_end -= 1;
    }

    let mut tail_start = s.len().saturating_sub(max_tail);
    while tail_start < s.len() && !s.is_char_boundary(tail_start) {
        tail_start += 1;
    }

    if head_end >= tail_start {
        return s.to_string();
    }

    let truncated_bytes = tail_start - head_end;
    format!(
        "{}\n\n... [TRUNCATED {} BYTES BY JEV HARNESS] ...\n\n{}",
        &s[..head_end],
        truncated_bytes,
        &s[tail_start..]
    )
}

/// Triages an execution or test failure log.
/// Returns whether expensive frontier LLM calls can be skipped.
pub async fn triage_test_failure(
    raw_error_log: &str,
    client: Option<&JevClient>,
) -> Result<TestTriageResult, JevError> {
    let default_client = JevClient::default();
    let active_client = client.unwrap_or(&default_client);

    let truncated_log = if raw_error_log.len() > 3000 {
        safe_truncate_head_tail(raw_error_log, 1400, 1400)
    } else {
        raw_error_log.to_string()
    };

    let mut questions = HashMap::new();

    let mut cat_criteria = HashMap::new();
    cat_criteria.insert(
        "env_missing".to_string(),
        "Missing dependency, package not found, uninstalled CLI tool, or environment path misconfiguration (e.g. TS2307, ModuleNotFoundError, E0463)".to_string(),
    );
    cat_criteria.insert(
        "flaky_transient".to_string(),
        "Transient network glitch, connection reset, socket timeout, port conflict, or busy worker (e.g. ETIMEDOUT, ECONNRESET)".to_string(),
    );
    cat_criteria.insert(
        "syntax_trivial".to_string(),
        "Simple syntax error, missing bracket, typo in variable, or formatting linter violation"
            .to_string(),
    );
    cat_criteria.insert(
        "deep_logic".to_string(),
        "Real semantic bug, failed unit assertion, invariant violation, panic, or unexpected state"
            .to_string(),
    );
    cat_criteria.insert(
        "test_redundant".to_string(),
        "Failure is due to an obsolete, redundant, or malformed test case rather than system defect".to_string(),
    );

    questions.insert(
        "category".to_string(),
        Question::Choice(ChoiceQuestion {
            instructions: "Categorize the primary root cause of this execution failure".to_string(),
            criteria: cat_criteria,
        }),
    );

    questions.insert(
        "skip_llm".to_string(),
        Question::Noul(NoulQuestion {
            instructions: "Can this error be handled deterministically (e.g. running pip/npm/cargo install, retrying, or fixing a simple typo) without calling an expensive System 2 generative LLM?".to_string(),
        }),
    );

    questions.insert(
        "severity".to_string(),
        Question::Score(ScoreQuestion {
            instructions: "Rate the severity of this defect on application stability".to_string(),
            criteria: vec![
                "trivial_cleanup".to_string(),
                "blocking_env".to_string(),
                "feature_broken".to_string(),
                "systemic_disaster".to_string(),
            ],
        }),
    );

    let resp = active_client.system_one(&truncated_log, questions).await?;

    let cat_ans = resp.answers.get("category").and_then(|a| a.as_choice());
    let skip_ans = resp.answers.get("skip_llm").and_then(|a| a.as_noul());
    let sev_ans = resp.answers.get("severity").and_then(|a| a.as_score());

    let category = cat_ans
        .map(|a| a.choice.clone())
        .unwrap_or_else(|| "deep_logic".to_string());
    let confidence = cat_ans.map(|a| a.confidence).unwrap_or(0.5);
    let sev_score = sev_ans.map(|a| a.score as f64).unwrap_or(3.0);
    let skip_prob = skip_ans.map(|a| a.noul).unwrap_or(0.0);
    let skip_llm = category != "deep_logic"
        && (skip_prob >= 0.65 || category == "env_missing" || category == "flaky_transient");

    let rec = match category.as_str() {
        "env_missing" => "AUTO-ACTION: Install missing dependency or check environment configuration (Do NOT call LLM).",
        "flaky_transient" => "AUTO-ACTION: Retry test once with fresh worker; do not generate code changes.",
        "syntax_trivial" => "LOW-COST: Fix typo locally or route to fastest lightweight tier.",
        "test_redundant" => "PRUNE: Test is redundant or obsolete; prune from test harness.",
        _ => "ESCALATE: Real logic defect; dispatch to System 2 LLM with targeted context.",
    };

    Ok(TestTriageResult {
        category,
        confidence,
        skip_llm,
        skip_llm_prob: skip_prob,
        severity_score: sev_score,
        action_recommendation: rec.to_string(),
        is_mock: resp.is_mock,
    })
}

/// Evaluates if current agent trajectory is trapped in a circular loop or unviable path.
pub async fn should_abort_trajectory(
    proposed_step: &str,
    recent_attempts_summary: &str,
    client: Option<&JevClient>,
) -> Result<AbortGateResult, JevError> {
    let default_client = JevClient::default();
    let active_client = client.unwrap_or(&default_client);

    let state = format!(
        "RECENT ATTEMPTS & CONTEXT:\n{}\n\nPROPOSED NEXT STEP:\n{}",
        recent_attempts_summary, proposed_step
    );

    let mut questions = HashMap::new();

    questions.insert(
        "dead_end".to_string(),
        Question::Noul(NoulQuestion {
            instructions: "Does this proposed step indicate a dead end, repeating a previously failed approach, or proposing an unviable/destructive path?".to_string(),
        }),
    );

    let mut action_criteria = HashMap::new();
    action_criteria.insert(
        "proceed".to_string(),
        "The step is logical, progress-oriented, and grounded in evidence".to_string(),
    );
    action_criteria.insert(
        "replan".to_string(),
        "The step is doubtful or weak; reconsider alternatives".to_string(),
    );
    action_criteria.insert(
        "abort_and_ask".to_string(),
        "The trajectory is circular or contradictory; stop and ask user for clarification"
            .to_string(),
    );

    questions.insert(
        "action".to_string(),
        Question::Choice(ChoiceQuestion {
            instructions: "What should the orchestrator do with this proposed trajectory?"
                .to_string(),
            criteria: action_criteria,
        }),
    );

    questions.insert(
        "viability".to_string(),
        Question::Score(ScoreQuestion {
            instructions: "Evaluate the technical viability of this step".to_string(),
            criteria: vec![
                "hopeless_circular".to_string(),
                "doubtful".to_string(),
                "plausible".to_string(),
                "highly_viable".to_string(),
            ],
        }),
    );

    let resp = active_client.system_one(&state, questions).await?;

    let dead_end_ans = resp.answers.get("dead_end").and_then(|a| a.as_noul());
    let action_ans = resp.answers.get("action").and_then(|a| a.as_choice());
    let viability_ans = resp.answers.get("viability").and_then(|a| a.as_score());

    let dead_end_prob = dead_end_ans.map(|a| a.noul).unwrap_or(0.0);
    let action = action_ans
        .map(|a| a.choice.clone())
        .unwrap_or_else(|| "proceed".to_string());
    let viability = viability_ans.map(|a| a.score as f64).unwrap_or(3.0);

    let should_abort = dead_end_prob >= 0.70 || action == "abort_and_ask" || viability <= 1.5;
    let effective_action = if should_abort && action == "proceed" {
        "abort_and_ask".to_string()
    } else {
        action
    };

    let summary = if should_abort {
        format!("Abort recommended (prob={:.2})", dead_end_prob)
    } else {
        format!(
            "Safe to proceed (viability={:.1}, action={})",
            viability, effective_action
        )
    };

    Ok(AbortGateResult {
        should_abort,
        abort_probability: dead_end_prob,
        action: effective_action,
        viability_score: viability,
        reasoning_summary: summary,
        is_mock: resp.is_mock,
    })
}

/// Dynamically selects minimal sufficient model tier.
pub async fn route_model_tier(
    task_description: &str,
    client: Option<&JevClient>,
) -> Result<ModelRouteResult, JevError> {
    let default_client = JevClient::default();
    let active_client = client.unwrap_or(&default_client);

    let mut questions = HashMap::new();

    let mut tier_criteria = HashMap::new();
    tier_criteria.insert(
        "deterministic".to_string(),
        "Can be solved with bash, regex, deterministic script, or pure Jev classification"
            .to_string(),
    );
    tier_criteria.insert(
        "lightweight_system2".to_string(),
        "Simple coding edit, formatting, documentation, or trivial unit test (e.g. Gemini 3.8 Flash)".to_string(),
    );
    tier_criteria.insert(
        "heavy_system2".to_string(),
        "Complex architecture, deep reasoning, multi-file refactoring, or difficult debugging (e.g. GPT-6 Astra, Claude Fable 5.1)".to_string(),
    );

    questions.insert(
        "tier".to_string(),
        Question::Choice(ChoiceQuestion {
            instructions: "Select the minimal sufficient model tier to solve this programming task"
                .to_string(),
            criteria: tier_criteria,
        }),
    );

    questions.insert(
        "complexity".to_string(),
        Question::Score(ScoreQuestion {
            instructions: "Rate the cognitive complexity of this task".to_string(),
            criteria: vec![
                "trivial".to_string(),
                "straightforward".to_string(),
                "moderate".to_string(),
                "highly_complex".to_string(),
            ],
        }),
    );

    let resp = active_client
        .system_one(task_description, questions)
        .await?;

    let tier_ans = resp.answers.get("tier").and_then(|a| a.as_choice());
    let comp_ans = resp.answers.get("complexity").and_then(|a| a.as_score());

    let selected_tier = tier_ans
        .map(|a| a.choice.clone())
        .unwrap_or_else(|| "lightweight_system2".to_string());
    let confidence = tier_ans.map(|a| a.confidence).unwrap_or(0.5);
    let complexity_score = comp_ans.map(|a| a.score as f64).unwrap_or(2.0);

    let (rec_model, rationale) = match selected_tier.as_str() {
        "deterministic" => (
            "Direct Python/Bash Script (0 LLM Tokens)",
            "Task does not require generative reasoning; execute mechanically.",
        ),
        "heavy_system2" => (
            "Claude Fable 5.1 / GPT-6 Astra (~$10.00 in / $50.00 out per 1M tokens)",
            "Task requires deep architectural synthesis or multi-file reasoning.",
        ),
        _ => (
            "Gemini 3.8 Flash (~$0.75 in / $3.75 out per 1M tokens)",
            "Straightforward generative task; lightweight fast agent tier is optimal.",
        ),
    };

    Ok(ModelRouteResult {
        selected_tier,
        confidence,
        complexity_score,
        recommended_model: rec_model.to_string(),
        rationale: rationale.to_string(),
        is_mock: resp.is_mock,
    })
}

/// Verifies whether step output meets criteria before concluding work.
pub async fn verify_step_completion(
    criteria: &str,
    output: &str,
    client: Option<&JevClient>,
) -> Result<VerificationResult, JevError> {
    let default_client = JevClient::default();
    let active_client = client.unwrap_or(&default_client);

    let state = format!(
        "CRITERIA TO SATISFY:\n{}\n\nACTUAL STEP OUTPUT:\n{}",
        criteria, output
    );

    let mut questions = HashMap::new();

    questions.insert(
        "satisfied".to_string(),
        Question::Noul(NoulQuestion {
            instructions: "Does the output fully satisfy all required acceptance criteria without missing details?".to_string(),
        }),
    );

    questions.insert(
        "rigor".to_string(),
        Question::Score(ScoreQuestion {
            instructions: "Score the completeness and rigor of verification evidence".to_string(),
            criteria: vec![
                "unsupported_claim".to_string(),
                "partial_evidence".to_string(),
                "well_verified".to_string(),
                "exhaustively_proven".to_string(),
            ],
        }),
    );

    let resp = active_client.system_one(&state, questions).await?;

    let sat_ans = resp.answers.get("satisfied").and_then(|a| a.as_noul());
    let rig_ans = resp.answers.get("rigor").and_then(|a| a.as_score());

    let sat_prob = sat_ans.map(|a| a.noul).unwrap_or(0.0);
    let rig_score = rig_ans.map(|a| a.score as f64).unwrap_or(2.0);

    let is_verified = sat_prob >= 0.80 && rig_score >= 2.5;

    Ok(VerificationResult {
        is_verified,
        satisfaction_probability: sat_prob,
        rigor_score: rig_score,
        confidence: sat_prob,
        needs_rework: !is_verified,
        is_mock: resp.is_mock,
    })
}

pub fn build_provider_params(
    provider: &str,
    effort: &str,
    model: Option<&str>,
) -> (serde_json::Value, bool, String, String) {
    let norm_provider = provider.trim().to_lowercase();
    let norm_model = model.unwrap_or("").trim().to_lowercase();

    let direct_models = [
        "gpt-5.6-luna",
        "gpt-5.5",
        "gpt-4o",
        "gpt-4o-mini",
        "gemini-3.8-live",
        "gemini-2.5-flash",
        "gemini-2.0-flash",
        "gemini-1.5-flash",
        "claude-3-5-haiku",
        "qwen-3.8-flash-standard",
        "qwen-2.5-coder",
        "llama-3.3",
        "llama-3.1",
    ];
    if direct_models.iter().any(|dm| norm_model.contains(dm)) {
        return (
            serde_json::json!({}),
            false,
            format!("Model '{:?}' is a direct single-pass model without internal reasoning CoT. Do NOT inject reasoning parameters.", model),
            "Cache unaffected. Model runs in direct generation mode.".to_string(),
        );
    }

    let cache_rec = if effort != "low" {
        "Keep reasoning effort stable across related sub-steps to preserve Prompt Cache (KV Cache)."
    } else {
        "Low reasoning effort saves ~7,000 reasoning tokens. Safe to use for mechanical tool calls."
    };

    if norm_provider == "openai" || norm_provider == "codex" {
        (
            serde_json::json!({ "reasoning_effort": effort }),
            true,
            format!("Configured OpenAI reasoning_effort='{}' for target model. Note: Ensure temperature=1.0 or omitted to prevent HTTP 400.", effort),
            cache_rec.to_string(),
        )
    } else if norm_provider == "deepseek" || norm_provider == "deepseek-ai" {
        let effort_val = if effort == "low" { "low" } else { "high" };
        (
            serde_json::json!({
                "extra_body": { "thinking": { "type": "enabled" } },
                "reasoning_effort": effort_val
            }),
            true,
            format!("DeepSeek Thinking mode configured with effort='{}'. Preserves reasoning_content in multi-turn tool calling.", effort_val),
            if effort == "low" { "Cuts latency by ~200s in mechanical steps when set to low.".to_string() } else { cache_rec.to_string() },
        )
    } else if norm_provider == "qwen" || norm_provider == "alibaba" || norm_provider == "dashscope"
    {
        if effort == "low" {
            (
                serde_json::json!({ "enable_thinking": false }),
                true,
                "Disabled Qwen thinking CoT for mechanical/terminal step to minimize latency. Wrap in extra_body={'enable_thinking': false} when using OpenAI client.".to_string(),
                "Zero tokens spent on reasoning trace.".to_string(),
            )
        } else {
            let budget = if effort == "medium" { 4096 } else { 16384 };
            (
                serde_json::json!({ "enable_thinking": true, "thinking_budget": budget }),
                true,
                format!("Enabled Qwen thinking budget ({} tokens). Wrap in extra_body when using OpenAI client.", budget),
                cache_rec.to_string(),
            )
        }
    } else if norm_provider == "anthropic" || norm_provider == "claude" {
        let chosen = match effort {
            "low" => "low",
            "medium" => "medium",
            _ => "max",
        };
        (
            serde_json::json!({
                "thinking": { "type": "adaptive" },
                "output_config": { "effort": chosen }
            }),
            true,
            format!(
                "Configured Anthropic Adaptive Thinking with effort='{}'.",
                chosen
            ),
            cache_rec.to_string(),
        )
    } else if norm_provider == "gemini" || norm_provider == "google" {
        let chosen = match effort {
            "low" => "minimal",
            "medium" => "medium",
            _ => "high",
        };
        (
            serde_json::json!({
                "thinking_config": { "thinking_level": chosen }
            }),
            true,
            format!("Configured Gemini thinking_level='{}'.", chosen),
            cache_rec.to_string(),
        )
    } else if norm_provider == "kimi" || norm_provider == "moonshot" {
        if effort == "low" {
            (
                serde_json::json!({ "extra_body": { "thinking": false } }),
                true,
                "Enabled Kimi Instant Mode (thinking disabled) for zero-latency execution."
                    .to_string(),
                "Eliminates internal CoT overhead.".to_string(),
            )
        } else {
            let k_effort = if effort == "high" { "high" } else { "low" };
            (
                serde_json::json!({ "reasoning_effort": k_effort }),
                true,
                format!("Configured Kimi reasoning_effort='{}'.", k_effort),
                cache_rec.to_string(),
            )
        }
    } else if norm_provider == "mimo" || norm_provider == "xiaomi" {
        if effort == "low" {
            (
                serde_json::json!({ "thinking": { "type": "disabled" } }),
                true,
                "Disabled MiMo CoT for terminal command to free GPU inference.".to_string(),
                "Immediate generation without scratchpad.".to_string(),
            )
        } else {
            (
                serde_json::json!({
                    "thinking": { "type": "enabled" },
                    "reasoning": { "effort": effort }
                }),
                true,
                format!("Enabled MiMo deep reasoning with effort='{}'.", effort),
                cache_rec.to_string(),
            )
        }
    } else {
        (
            serde_json::json!({ "reasoning_effort": effort }),
            true,
            format!("Generic reasoning effort='{}'.", effort),
            cache_rec.to_string(),
        )
    }
}

pub async fn modulate_reasoning_effort_with_tokens(
    context: &str,
    provider: &str,
    model: Option<&str>,
    session_context_tokens: usize,
    client: Option<&JevClient>,
) -> Result<ReasoningEffortResult, JevError> {
    let default_client = JevClient::default();
    let active_client = client.unwrap_or(&default_client);

    let mut questions = HashMap::new();

    let mut effort_criteria = HashMap::new();
    effort_criteria.insert(
        "low".to_string(),
        "Mechanical action: run bash command, check git status, view file, format code, linter check, simple import, or trivial syntax edit".to_string(),
    );
    effort_criteria.insert(
        "medium".to_string(),
        "Standard code modification: implement bounded function, write standard unit test, add parameter, or localized refactoring".to_string(),
    );
    effort_criteria.insert(
        "high".to_string(),
        "Deep cognitive task: architectural design, race condition, distributed deadlock, concurrency kernel bug, or complex multi-file debugging".to_string(),
    );

    questions.insert(
        "effort".to_string(),
        Question::Choice(ChoiceQuestion {
            instructions: "Select the minimal sufficient reasoning effort needed for this immediate agent step".to_string(),
            criteria: effort_criteria,
        }),
    );

    questions.insert(
        "complexity".to_string(),
        Question::Score(ScoreQuestion {
            instructions: "Rate the cognitive depth required for this next step".to_string(),
            criteria: vec![
                "trivial_mechanical".to_string(),
                "standard_implementation".to_string(),
                "complex_logic".to_string(),
                "exceptional_architecture".to_string(),
            ],
        }),
    );

    let clean_context = if context.len() > 4000 {
        &context[..4000]
    } else {
        context
    };

    let resp = active_client.system_one(clean_context, questions).await?;

    let effort_ans = resp.answers.get("effort").and_then(|a| a.as_choice());
    let comp_ans = resp.answers.get("complexity").and_then(|a| a.as_score());

    let effort = effort_ans
        .map(|a| a.choice.clone())
        .unwrap_or_else(|| "medium".to_string());
    let confidence = effort_ans.map(|a| a.confidence).unwrap_or(0.85);
    let complexity_score = comp_ans.map(|a| a.score as f64).unwrap_or(2.0);

    let (provider_params, is_supported, rationale, mut cache_rec) =
        build_provider_params(provider, &effort, model);

    if session_context_tokens > 30000 && is_supported {
        cache_rec = format!(
            "HIGH CACHE RISK ({} tokens active): Modulating reasoning effort across turns may invalidate prefix KV cache. Hysteresis recommended: preserve stable reasoning effort across active sub-steps.",
            session_context_tokens
        );
    }

    Ok(ReasoningEffortResult {
        effort,
        confidence,
        complexity_score,
        rationale,
        provider: provider.to_string(),
        provider_params,
        is_reasoning_supported: is_supported,
        cache_safe_recommendation: cache_rec,
        is_mock: resp.is_mock,
    })
}

pub async fn modulate_reasoning_effort(
    context: &str,
    provider: &str,
    model: Option<&str>,
    client: Option<&JevClient>,
) -> Result<ReasoningEffortResult, JevError> {
    modulate_reasoning_effort_with_tokens(context, provider, model, 0, client).await
}