nyx-agent-ai 0.1.0

Implementation-detail AI runtime adapters, prompts, and automation helpers for nyx-agent.
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
//! NovelFindingDiscovery agent task.
//!
//! Given a batch of source files plus the nyx static-pass findings that
//! already exist on those files, drive an `AiRuntime::one_shot` call
//! against a strict JSON contract, parse the response, validate each
//! candidate, and produce either a
//! [`NovelFindingDiscoveryOutcome::Discovered`] envelope (zero or more
//! candidates) or [`NovelFindingDiscoveryOutcome::NoCandidates`] when
//! two attempts both produce malformed output.
//!
//! The task crate stays vendor-neutral. It does not depend on
//! `nyx-agent-core::store`; the binary turns every candidate into a
//! `candidate_findings` row (quarantined awaiting the verifier) at
//! persistence time.

use std::collections::HashSet;

use nyx_agent_types::agent::{AgentTraceMetrics, AiError, Budget, BudgetKind, Prompt, Response};
use nyx_agent_types::event::EventSink;
use nyx_agent_types::novel::{
    CandidateFinding, NovelFindingDiscoveryInput, NovelFindingDiscoveryOutput,
    NOVEL_FINDING_DISCOVERY_PROMPT_VERSION,
};

use crate::runtime::AiRuntime;

/// First-attempt system prompt. Plain JSON contract; describes the
/// sink taxonomy the structured output is keyed on plus the rules the
/// validator enforces. Source lives at
/// `crates/nyx-agent-ai/src/prompts/novel_findings.v1.md`.
const SYSTEM_PROMPT_V1: &str = include_str!("../prompts/novel_findings.v1.md");

/// Retry system prompt. Identical contract with the explicit "your
/// previous reply did not validate" framing.
const SYSTEM_PROMPT_V1_STRICTER: &str = include_str!("../prompts/novel_findings.v1_stricter.md");

/// Outcome of one `run` invocation. The binary turns every
/// `Discovered::candidates` entry into a `candidate_findings` row
/// (quarantined until the verifier promotes it). `NoCandidates` is
/// recorded only in the agent trace.
#[derive(Debug, Clone)]
pub enum NovelFindingDiscoveryOutcome {
    /// Both deserialise + validate gates passed. `output.candidates`
    /// may be empty (the model legitimately found nothing); the caller
    /// still records the spend / attempts and ticks the trace forward.
    Discovered {
        run_id: String,
        repo: String,
        batch_id: String,
        output: NovelFindingDiscoveryOutput,
        prompt_version: String,
        spent_usd_micros: i64,
        attempts: u32,
        metrics: AgentTraceMetrics,
    },
    /// Both attempts produced malformed output. The binary surfaces
    /// `reason` in the agent-trace store; nothing is persisted to the
    /// `candidate_findings` table for this batch.
    NoCandidates {
        run_id: String,
        repo: String,
        batch_id: String,
        reason: String,
        spent_usd_micros: i64,
        attempts: u32,
        metrics: AgentTraceMetrics,
    },
}

/// Drive one NovelFindingDiscovery call for `input`.
///
/// `cap_usd_micros` is the per-call budget. Both attempts share the
/// same `(run_id, kind)` budget bucket via the `BudgetTracker` host
/// port.
pub async fn run<R: AiRuntime + ?Sized>(
    runtime: &R,
    input: &NovelFindingDiscoveryInput,
    sink: EventSink,
    cap_usd_micros: i64,
) -> Result<NovelFindingDiscoveryOutcome, AiError> {
    let task_id = format!("novel-{}", input.batch_id);
    let budget =
        || Budget { run_id: input.run_id.clone(), kind: BudgetKind::OneShot, cap_usd_micros };
    let known_paths: HashSet<&str> = input.files.iter().map(|f| f.path.as_str()).collect();

    let prompt = build_prompt(SYSTEM_PROMPT_V1, &task_id, input);
    let resp1: Response = runtime.one_shot(prompt, budget(), sink.clone()).await?;
    let cost1 = resp1.cost_usd_micros;
    let metrics1 = AgentTraceMetrics::from_response(&resp1);
    let first_err = match parse_and_validate(&resp1.content, &known_paths) {
        Ok(output) => {
            return Ok(NovelFindingDiscoveryOutcome::Discovered {
                run_id: input.run_id.clone(),
                repo: input.repo.clone(),
                batch_id: input.batch_id.clone(),
                output,
                prompt_version: resp1.prompt_version,
                spent_usd_micros: cost1,
                attempts: 1,
                metrics: metrics1,
            });
        }
        Err(msg) => msg,
    };

    let prompt2 = build_prompt(SYSTEM_PROMPT_V1_STRICTER, &task_id, input);
    let resp2: Response = runtime.one_shot(prompt2, budget(), sink).await?;
    let total_cost = cost1 + resp2.cost_usd_micros;
    let metrics_total = metrics1.merge(AgentTraceMetrics::from_response(&resp2));
    match parse_and_validate(&resp2.content, &known_paths) {
        Ok(output) => Ok(NovelFindingDiscoveryOutcome::Discovered {
            run_id: input.run_id.clone(),
            repo: input.repo.clone(),
            batch_id: input.batch_id.clone(),
            output,
            prompt_version: resp2.prompt_version,
            spent_usd_micros: total_cost,
            attempts: 2,
            metrics: metrics_total,
        }),
        Err(second_err) => Ok(NovelFindingDiscoveryOutcome::NoCandidates {
            run_id: input.run_id.clone(),
            repo: input.repo.clone(),
            batch_id: input.batch_id.clone(),
            reason: format!(
                "novel finding discovery failed twice (attempt 1: {first_err}; attempt 2: {second_err})"
            ),
            spent_usd_micros: total_cost,
            attempts: 2,
            metrics: metrics_total,
        }),
    }
}

fn build_prompt(system: &str, task_id: &str, input: &NovelFindingDiscoveryInput) -> Prompt {
    let user = render_user_message(input);
    Prompt {
        prompt_version: NOVEL_FINDING_DISCOVERY_PROMPT_VERSION.to_string(),
        task_id: task_id.to_string(),
        model: None,
        system: system.to_string(),
        user,
        // The batch carries up to ~30 truncated files; the response is
        // small (a candidate is ~150 bytes serialised). 4096 leaves
        // plenty of headroom for a dozen candidates per batch.
        max_output_tokens: 4096,
        temperature: 0.0,
        seed: None,
    }
}

/// Compact, model-friendly serialisation of the input. `serde_json` is
/// avoided for the file bodies because fenced code blocks are far
/// easier for the model to read than escaped JSON strings.
fn render_user_message(input: &NovelFindingDiscoveryInput) -> String {
    let mut out = String::new();
    out.push_str(&format!("run_id  = {}\n", input.run_id));
    out.push_str(&format!("repo    = {}\n", input.repo));
    out.push_str(&format!("batch   = {}\n", input.batch_id));
    out.push('\n');

    out.push_str("priors (already flagged by the static pass; do NOT rediscover):\n");
    if input.priors.is_empty() {
        out.push_str("- (none)\n");
    } else {
        for p in &input.priors {
            out.push_str(&format!("- {} L{} cap={} rule={}\n", p.path, p.line, p.cap, p.rule,));
        }
    }
    out.push('\n');

    out.push_str("files:\n");
    for f in &input.files {
        out.push_str(&format!("\n--- {} ---\n", f.path));
        out.push_str("```\n");
        out.push_str(&f.content);
        if !f.content.ends_with('\n') {
            out.push('\n');
        }
        out.push_str("```\n");
        if f.truncated {
            out.push_str(
                "(note: this file was truncated; do not invent lines past the visible region)\n",
            );
        }
    }
    out
}

fn parse_and_validate(
    raw: &str,
    known_paths: &HashSet<&str>,
) -> Result<NovelFindingDiscoveryOutput, String> {
    let body = strip_code_fence(raw.trim());
    let out: NovelFindingDiscoveryOutput =
        serde_json::from_str(body).map_err(|e| format!("malformed json: {e}"))?;
    validate_candidates(&out.candidates, known_paths)?;
    Ok(out)
}

fn validate_candidates(
    candidates: &[CandidateFinding],
    known_paths: &HashSet<&str>,
) -> Result<(), String> {
    for (i, c) in candidates.iter().enumerate() {
        if c.line == 0 {
            return Err(format!("candidate {i}: line must be >= 1"));
        }
        if c.cap.trim().is_empty() {
            return Err(format!("candidate {i}: cap was empty"));
        }
        if c.rationale.trim().is_empty() {
            return Err(format!("candidate {i}: rationale was empty"));
        }
        if !known_paths.contains(c.path.as_str()) {
            return Err(format!(
                "candidate {i}: path {:?} is not in the batch's file list",
                c.path
            ));
        }
    }
    Ok(())
}

fn strip_code_fence(s: &str) -> &str {
    let s = s.trim();
    if let Some(rest) = s.strip_prefix("```json") {
        return rest.trim().trim_end_matches("```").trim();
    }
    if let Some(rest) = s.strip_prefix("```") {
        return rest.trim().trim_end_matches("```").trim();
    }
    s
}

#[cfg(test)]
mod tests {
    use std::sync::{Arc, Mutex};

    use async_trait::async_trait;
    use nyx_agent_types::agent::{
        AgentResult, AgentTask, AiError, CacheStats, CostEstimate, TokenUsage,
    };
    use nyx_agent_types::event::AgentEvent;
    use nyx_agent_types::novel::{FileForReview, PriorFinding};
    use tokio::sync::broadcast;

    use super::*;
    use crate::runtime::{AiRuntime, BudgetTracker, InMemoryBudgetTracker};
    use nyx_agent_types::agent::BudgetKind;

    /// Scripted runtime that replays a fixed sequence of `one_shot`
    /// responses. Same shape as the payload / spec / chain test
    /// fixtures.
    struct ScriptedRuntime {
        responses: Mutex<Vec<Result<String, AiError>>>,
        prompts_seen: Mutex<Vec<String>>,
        user_messages_seen: Mutex<Vec<String>>,
        tracker: Arc<dyn BudgetTracker>,
        cost_per_call: i64,
    }

    impl ScriptedRuntime {
        fn new(
            responses: Vec<Result<String, AiError>>,
            tracker: Arc<dyn BudgetTracker>,
            cost_per_call: i64,
        ) -> Self {
            Self {
                responses: Mutex::new(responses),
                prompts_seen: Mutex::new(Vec::new()),
                user_messages_seen: Mutex::new(Vec::new()),
                tracker,
                cost_per_call,
            }
        }

        fn prompts(&self) -> Vec<String> {
            self.prompts_seen.lock().unwrap().clone()
        }

        fn user_messages(&self) -> Vec<String> {
            self.user_messages_seen.lock().unwrap().clone()
        }
    }

    #[async_trait]
    impl AiRuntime for ScriptedRuntime {
        fn name(&self) -> &'static str {
            "scripted"
        }
        fn default_model(&self) -> &str {
            "scripted-model"
        }
        fn supports_agent_loop(&self) -> bool {
            false
        }
        fn supports_prompt_cache(&self) -> bool {
            false
        }
        fn supports_deterministic_sampling(&self) -> bool {
            true
        }

        async fn one_shot(
            &self,
            prompt: Prompt,
            budget: Budget,
            _sink: EventSink,
        ) -> Result<Response, AiError> {
            self.prompts_seen.lock().unwrap().push(prompt.system.clone());
            self.user_messages_seen.lock().unwrap().push(prompt.user.clone());
            let next =
                self.responses.lock().unwrap().pop().expect("scripted runtime: no more responses");
            let content = next?;
            let cost = self.cost_per_call;
            self.tracker.add_spend(&budget.run_id, budget.kind, cost).await?;
            Ok(Response {
                prompt_version: prompt.prompt_version,
                task_id: prompt.task_id,
                model: "scripted-model".to_string(),
                content,
                usage: TokenUsage { input_tokens: 500, output_tokens: 200 },
                cache: Some(CacheStats::default()),
                cost_usd_micros: cost,
            })
        }

        async fn agent_loop(
            &self,
            _task: AgentTask,
            _budget: Budget,
            _sink: EventSink,
        ) -> Result<AgentResult, AiError> {
            Err(AiError::UnsupportedMode("agent_loop"))
        }

        fn cost_estimate(&self, _prompt: &Prompt) -> Option<CostEstimate> {
            Some(CostEstimate { min_usd_micros: 0, max_usd_micros: self.cost_per_call })
        }
    }

    fn sample_input() -> NovelFindingDiscoveryInput {
        // One known SQL_QUERY sink at line 3 (the prior); a second
        // syntactically-similar sink at line 6 the model is expected
        // to flag.
        NovelFindingDiscoveryInput {
            run_id: "run-N".into(),
            repo: "repo-1".into(),
            batch_id: "repo-1:0".into(),
            files: vec![FileForReview {
                path: "app/handlers.py".into(),
                content: "def list_users(q):\n    sql = 'SELECT * FROM u WHERE n=' + q\n    cursor.execute(sql)\n\ndef list_admins(q):\n    sql2 = 'SELECT * FROM admin WHERE n=' + q\n    cursor.execute(sql2)\n".into(),
                truncated: false,
            }],
            priors: vec![PriorFinding {
                path: "app/handlers.py".into(),
                line: 3,
                cap: "SQL_QUERY".into(),
                rule: "py.sql.exec".into(),
            }],
        }
    }

    fn ok_body(candidates: serde_json::Value) -> String {
        serde_json::json!({ "candidates": candidates }).to_string()
    }

    #[tokio::test]
    async fn similar_second_sink_produces_a_candidate() {
        // Acceptance for the per-task layer: a file carrying one known
        // nyx sink + an intentionally similar second sink yields a
        // candidate for the second one and skips the first.
        let tracker = Arc::new(InMemoryBudgetTracker::new());
        tracker.set_cap("run-N", BudgetKind::OneShot, 5_000_000);
        let body = ok_body(serde_json::json!([
            {
                "path": "app/handlers.py",
                "line": 6,
                "cap": "SQL_QUERY",
                "rule_hint": "py.sql.exec",
                "rationale": "list_admins reuses the same SQL-string-concat pattern as the prior at line 3",
                "suggested_payload_hint": "' OR 1=1 --"
            }
        ]));
        let rt = ScriptedRuntime::new(vec![Ok(body)], tracker.clone(), 6_000);

        let (tx, _rx) = broadcast::channel::<AgentEvent>(16);
        let outcome = run(&rt, &sample_input(), tx, 5_000_000).await.expect("ok");
        match outcome {
            NovelFindingDiscoveryOutcome::Discovered {
                run_id,
                repo,
                batch_id,
                output,
                prompt_version,
                spent_usd_micros,
                attempts,
                metrics,
            } => {
                assert_eq!(run_id, "run-N");
                assert_eq!(repo, "repo-1");
                assert_eq!(batch_id, "repo-1:0");
                assert_eq!(attempts, 1);
                assert_eq!(spent_usd_micros, 6_000);
                assert_eq!(prompt_version, NOVEL_FINDING_DISCOVERY_PROMPT_VERSION);
                assert_eq!(output.candidates.len(), 1);
                let c = &output.candidates[0];
                assert_eq!(c.path, "app/handlers.py");
                assert_eq!(c.line, 6);
                assert_eq!(c.cap, "SQL_QUERY");
                assert!(!c.rationale.is_empty());
                assert_eq!(metrics.usage.input_tokens, 500);
                assert_eq!(metrics.usage.output_tokens, 200);
                assert_eq!(metrics.model.as_deref(), Some("scripted-model"));
            }
            other => panic!("expected Discovered, got {other:?}"),
        }
        // Priors surface in the rendered user message so the model can
        // see what to avoid.
        let user = rt.user_messages().into_iter().next().expect("user msg");
        assert!(user.contains("py.sql.exec"), "priors must appear: {user}");
        assert!(user.contains("--- app/handlers.py ---"));
    }

    #[tokio::test]
    async fn empty_candidates_array_is_accepted() {
        let tracker = Arc::new(InMemoryBudgetTracker::new());
        tracker.set_cap("run-N", BudgetKind::OneShot, 5_000_000);
        let rt =
            ScriptedRuntime::new(vec![Ok(ok_body(serde_json::json!([])))], tracker.clone(), 1_000);
        let (tx, _rx) = broadcast::channel::<AgentEvent>(8);
        let outcome = run(&rt, &sample_input(), tx, 5_000_000).await.expect("ok");
        match outcome {
            NovelFindingDiscoveryOutcome::Discovered { output, attempts, .. } => {
                assert!(output.candidates.is_empty());
                assert_eq!(attempts, 1);
            }
            other => panic!("expected Discovered, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn malformed_first_attempt_retries_with_stricter_prompt() {
        let tracker = Arc::new(InMemoryBudgetTracker::new());
        tracker.set_cap("run-N", BudgetKind::OneShot, 5_000_000);
        let good = ok_body(serde_json::json!([
            {
                "path": "app/handlers.py",
                "line": 6,
                "cap": "SQL_QUERY",
                "rationale": "ok",
            }
        ]));
        // Queue popped from back: first call -> garbage, second -> good.
        let rt = ScriptedRuntime::new(
            vec![Ok(good), Ok("not json at all".to_string())],
            tracker.clone(),
            2_000,
        );
        let (tx, _rx) = broadcast::channel::<AgentEvent>(16);
        let outcome = run(&rt, &sample_input(), tx, 5_000_000).await.expect("ok");
        match outcome {
            NovelFindingDiscoveryOutcome::Discovered { attempts, spent_usd_micros, .. } => {
                assert_eq!(attempts, 2);
                assert_eq!(spent_usd_micros, 4_000);
            }
            other => panic!("expected Discovered after retry, got {other:?}"),
        }
        let seen = rt.prompts();
        assert_eq!(seen.len(), 2);
        assert!(seen[0].contains("NovelFindingDiscovery worker"));
        assert!(seen[1].contains("previous reply did not validate"));
    }

    #[tokio::test]
    async fn unknown_path_is_rejected() {
        let tracker = Arc::new(InMemoryBudgetTracker::new());
        tracker.set_cap("run-N", BudgetKind::OneShot, 5_000_000);
        let bad = ok_body(serde_json::json!([
            {"path":"made/up.py","line":1,"cap":"SQL_QUERY","rationale":"x"}
        ]));
        let good = ok_body(serde_json::json!([
            {"path":"app/handlers.py","line":6,"cap":"SQL_QUERY","rationale":"real one"}
        ]));
        // First reply names a file outside the batch; retry returns a
        // valid candidate.
        let rt = ScriptedRuntime::new(vec![Ok(good), Ok(bad)], tracker.clone(), 1_000);
        let (tx, _rx) = broadcast::channel::<AgentEvent>(8);
        let outcome = run(&rt, &sample_input(), tx, 5_000_000).await.expect("ok");
        assert!(matches!(outcome, NovelFindingDiscoveryOutcome::Discovered { attempts: 2, .. }));
    }

    #[tokio::test]
    async fn zero_line_is_rejected() {
        let tracker = Arc::new(InMemoryBudgetTracker::new());
        tracker.set_cap("run-N", BudgetKind::OneShot, 5_000_000);
        let bad = ok_body(serde_json::json!([
            {"path":"app/handlers.py","line":0,"cap":"SQL_QUERY","rationale":"bad line"}
        ]));
        let good = ok_body(serde_json::json!([
            {"path":"app/handlers.py","line":6,"cap":"SQL_QUERY","rationale":"good"}
        ]));
        let rt = ScriptedRuntime::new(vec![Ok(good), Ok(bad)], tracker.clone(), 800);
        let (tx, _rx) = broadcast::channel::<AgentEvent>(8);
        let outcome = run(&rt, &sample_input(), tx, 5_000_000).await.expect("ok");
        assert!(matches!(outcome, NovelFindingDiscoveryOutcome::Discovered { attempts: 2, .. }));
    }

    #[tokio::test]
    async fn empty_rationale_is_rejected() {
        let tracker = Arc::new(InMemoryBudgetTracker::new());
        tracker.set_cap("run-N", BudgetKind::OneShot, 5_000_000);
        let bad = ok_body(serde_json::json!([
            {"path":"app/handlers.py","line":6,"cap":"SQL_QUERY","rationale":"   "}
        ]));
        let good = ok_body(serde_json::json!([
            {"path":"app/handlers.py","line":6,"cap":"SQL_QUERY","rationale":"non-empty"}
        ]));
        let rt = ScriptedRuntime::new(vec![Ok(good), Ok(bad)], tracker.clone(), 800);
        let (tx, _rx) = broadcast::channel::<AgentEvent>(8);
        let outcome = run(&rt, &sample_input(), tx, 5_000_000).await.expect("ok");
        assert!(matches!(outcome, NovelFindingDiscoveryOutcome::Discovered { attempts: 2, .. }));
    }

    #[tokio::test]
    async fn two_malformed_attempts_yield_no_candidates() {
        let tracker = Arc::new(InMemoryBudgetTracker::new());
        tracker.set_cap("run-N", BudgetKind::OneShot, 5_000_000);
        let rt = ScriptedRuntime::new(
            vec![Ok("still nope".to_string()), Ok("nope".to_string())],
            tracker.clone(),
            1_000,
        );
        let (tx, _rx) = broadcast::channel::<AgentEvent>(8);
        let outcome = run(&rt, &sample_input(), tx, 5_000_000).await.expect("ok");
        match outcome {
            NovelFindingDiscoveryOutcome::NoCandidates {
                run_id,
                repo,
                batch_id,
                reason,
                spent_usd_micros,
                attempts,
                metrics,
            } => {
                assert_eq!(run_id, "run-N");
                assert_eq!(repo, "repo-1");
                assert_eq!(batch_id, "repo-1:0");
                assert_eq!(attempts, 2);
                assert_eq!(spent_usd_micros, 2_000);
                assert!(reason.contains("failed twice"), "reason: {reason}");
                assert_eq!(metrics.usage.input_tokens, 1_000);
                assert_eq!(metrics.usage.output_tokens, 400);
            }
            other => panic!("expected NoCandidates, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn code_fence_wrapper_is_tolerated() {
        let tracker = Arc::new(InMemoryBudgetTracker::new());
        tracker.set_cap("run-N", BudgetKind::OneShot, 5_000_000);
        let inner = ok_body(serde_json::json!([
            {"path":"app/handlers.py","line":6,"cap":"SQL_QUERY","rationale":"ok"}
        ]));
        let wrapped = format!("```json\n{inner}\n```");
        let rt = ScriptedRuntime::new(vec![Ok(wrapped)], tracker.clone(), 500);
        let (tx, _rx) = broadcast::channel::<AgentEvent>(8);
        let outcome = run(&rt, &sample_input(), tx, 5_000_000).await.expect("ok");
        assert!(matches!(outcome, NovelFindingDiscoveryOutcome::Discovered { attempts: 1, .. }));
    }

    #[tokio::test]
    async fn upstream_error_surfaces_through() {
        let tracker = Arc::new(InMemoryBudgetTracker::new());
        tracker.set_cap("run-N", BudgetKind::OneShot, 5_000_000);
        let rt = ScriptedRuntime::new(
            vec![Err(AiError::UpstreamRefused("429 rate limit".to_string()))],
            tracker.clone(),
            1_000,
        );
        let (tx, _rx) = broadcast::channel::<AgentEvent>(8);
        let err = run(&rt, &sample_input(), tx, 5_000_000).await.expect_err("upstream");
        assert!(matches!(err, AiError::UpstreamRefused(_)));
    }
}