| @@ -17,9 +17,9 @@ use louise_types::{FnParam, Printer, Ty}; |
| 17 | 17 | const MODULE: &str = "+repl"; |
| 18 | 18 | const DISPLAY_PATH: &str = "<repl>"; |
| 19 | 19 | |
| 20 | | −/// Finite source, rendering and currently available ordinary-entry budgets |
| 21 | | −/// (ADR 0083). B1's ordinary `RunOptions` enablement of cumulative |
| 22 | | −/// steps/allocation/output charging is the separate prerequisite for the rest. |
| 20 | +/// Finite source, rendering and fresh ordinary-entry budgets (ADR 0083). Each |
| 21 | +/// evaluation installs one B1 `EvaluationState`; its state never becomes a |
| 22 | +/// persistent session resource. |
| 23 | 23 | #[derive(Clone, Copy, Debug)] |
| 24 | 24 | pub(crate) struct Limits { |
| 25 | 25 | line_bytes: usize, |
| @@ -31,6 +31,8 @@ pub(crate) struct Limits { |
| 31 | 31 | program_output_bytes: usize, |
| 32 | 32 | debug_bytes: usize, |
| 33 | 33 | max_frames: usize, |
| 34 | + max_steps: u64, |
| 35 | + max_allocation_bytes: u64, |
| 34 | 36 | } |
| 35 | 37 | |
| 36 | 38 | impl Default for Limits { |
| @@ -45,6 +47,8 @@ impl Default for Limits { |
| 45 | 47 | program_output_bytes: 64 << 10, |
| 46 | 48 | debug_bytes: 64 << 10, |
| 47 | 49 | max_frames: 4096, |
| 50 | + max_steps: 1_000_000, |
| 51 | + max_allocation_bytes: 16 << 20, |
| 48 | 52 | } |
| 49 | 53 | } |
| 50 | 54 | } |
| @@ -843,6 +847,17 @@ fn evaluate( |
| 843 | 847 | max_frames: Some(limits.max_frames), |
| 844 | 848 | exit_ends_run: true, |
| 845 | 849 | inputs: None, |
| 850 | + evaluation: Some(louise_interp::EvaluationLimits { |
| 851 | + max_frames: limits.max_frames, |
| 852 | + max_steps: limits.max_steps, |
| 853 | + max_allocation_bytes: limits.max_allocation_bytes, |
| 854 | + // Match B1's logical output/generated-content charge to the |
| 855 | + // capture cap. The writer does not add a second charge. |
| 856 | + max_output_bytes: limits.program_output_bytes as u64, |
| 857 | + // Ordinary interpretation never reifies: B1 ignores these. |
| 858 | + max_reified_nodes: 0, |
| 859 | + max_static_bytes: 0, |
| 860 | + }), |
| 846 | 861 | }, |
| 847 | 862 | |value| map_debug_value(value, limits.debug_bytes), |
| 848 | 863 | ); |
| @@ -1480,11 +1495,10 @@ mod tests { |
| 1480 | 1495 | assert!(error.contains("changed between bounded reads"), "{error}"); |
| 1481 | 1496 | } |
| 1482 | 1497 | |
| 1483 | | − #[test] |
| 1484 | | − fn debug_limit_is_structured_and_never_prints_a_partial_result() { |
| 1498 | + fn run_with_limits(input: &[u8], limits: Limits) -> (String, String) { |
| 1485 | 1499 | let mut out = Vec::new(); |
| 1486 | 1500 | let mut err = Vec::new(); |
| 1487 | | − let mut input = io::Cursor::new(b"\"abcdef\"\n1\n".to_vec()); |
| 1501 | + let mut input = io::Cursor::new(input.to_vec()); |
| 1488 | 1502 | run_io( |
| 1489 | 1503 | &mut input, |
| 1490 | 1504 | &mut out, |
| @@ -1492,25 +1506,80 @@ mod tests { |
| 1492 | 1506 | false, |
| 1493 | 1507 | false, |
| 1494 | 1508 | Path::new("."), |
| 1509 | + limits, |
| 1510 | + ) |
| 1511 | + .unwrap(); |
| 1512 | + ( |
| 1513 | + String::from_utf8(out).unwrap(), |
| 1514 | + String::from_utf8(err).unwrap(), |
| 1515 | + ) |
| 1516 | + } |
| 1517 | + |
| 1518 | + #[test] |
| 1519 | + fn fuel_is_cumulative_across_preemption_and_each_entry_starts_fresh() { |
| 1520 | + // The tail calls cross several ordinary preemption slices, but never |
| 1521 | + // execute the CTFE direct protocol and cannot refill the scoped fuel. |
| 1522 | + let (out, err) = run_with_limits( |
| 1523 | + b"fn spin :: n: Int -> () { spin (n + 1) }\nspin 0\n1\n", |
| 1495 | 1524 | Limits { |
| 1496 | | − debug_bytes: 4, |
| 1525 | + max_steps: 12_000, |
| 1497 | 1526 | ..Limits::default() |
| 1498 | 1527 | }, |
| 1499 | | − ) |
| 1500 | | − .unwrap(); |
| 1501 | | − // `Debug "abcdef"` is eight bytes; the cap rejects all of them. |
| 1502 | | − assert_eq!(out, b"1\n"); |
| 1503 | | − let err = String::from_utf8(err).unwrap(); |
| 1504 | | − assert!(err.contains("OutputBytes"), "{err}"); |
| 1505 | | − assert!(err.contains("(8 > 4)"), "{err}"); |
| 1528 | + ); |
| 1529 | + assert_eq!(out, "1\n"); |
| 1530 | + assert!(err.contains("Fuel"), "{err}"); |
| 1506 | 1531 | assert!(!err.contains("interpreter crashed"), "{err}"); |
| 1507 | 1532 | } |
| 1508 | 1533 | |
| 1509 | 1534 | #[test] |
| 1510 | | − fn program_output_limit_is_bounded_and_the_next_expression_still_works() { |
| 1535 | + fn allocation_charges_accumulate_across_fresh_function_calls() { |
| 1536 | + // The growing returned result keeps each recursive call's work live; |
| 1537 | + // an ordinary entry-level budget catches it before a partial Debug run. |
| 1538 | + let (out, err) = run_with_limits( |
| 1539 | + b"fn grow :: n: Int -> List<Int> { [n] <> grow (n + 1) }\ngrow 0\n1\n", |
| 1540 | + Limits { |
| 1541 | + max_allocation_bytes: 1024, |
| 1542 | + ..Limits::default() |
| 1543 | + }, |
| 1544 | + ); |
| 1545 | + assert_eq!(out, "1\n"); |
| 1546 | + assert!(err.contains("AllocationBytes"), "{err}"); |
| 1547 | + assert!(!err.contains("interpreter crashed"), "{err}"); |
| 1548 | + } |
| 1549 | + |
| 1550 | + #[test] |
| 1551 | + fn allocation_budget_is_shared_with_spawned_actor_work() { |
| 1552 | + let (out, err) = run_with_limits( |
| 1553 | + b"import std/process.{type proc}\nfn grow :: n: Int -> List<Int> { [n] <> grow (n + 1) }\n{\n let _ =\n {\n use <- process.spawn\n let _ = grow 0\n ()\n }\n // The deterministic ordinary scheduler gives the child a slice.\n process.sleep 0\n ()\n}\n1\n", |
| 1554 | + Limits { |
| 1555 | + max_allocation_bytes: 1024, |
| 1556 | + ..Limits::default() |
| 1557 | + }, |
| 1558 | + ); |
| 1559 | + assert_eq!(out, "()\n1\n"); |
| 1560 | + assert!(err.contains("AllocationBytes"), "{err}"); |
| 1561 | + assert!(!err.contains("interpreter crashed"), "{err}"); |
| 1562 | + } |
| 1563 | + |
| 1564 | + #[test] |
| 1565 | + fn frame_exhaustion_is_structured_and_does_not_corrupt_the_session() { |
| 1566 | + let (out, err) = run_with_limits( |
| 1567 | + b"fn deep :: n: Int -> Int {\n if n <= 0 { 0 } else { deep (n - 1) + 1 }\n}\ndeep 32\n1\n", |
| 1568 | + Limits { |
| 1569 | + max_frames: 4, |
| 1570 | + ..Limits::default() |
| 1571 | + }, |
| 1572 | + ); |
| 1573 | + assert_eq!(out, "1\n"); |
| 1574 | + assert!(err.contains("Frames"), "{err}"); |
| 1575 | + assert!(!err.contains("stack overflow"), "{err}"); |
| 1576 | + } |
| 1577 | + |
| 1578 | + #[test] |
| 1579 | + fn debug_limit_is_structured_and_never_prints_a_partial_result() { |
| 1511 | 1580 | let mut out = Vec::new(); |
| 1512 | 1581 | let mut err = Vec::new(); |
| 1513 | | − let mut input = io::Cursor::new(b"import std/io\nio.print \"abc\"\n1\n".to_vec()); |
| 1582 | + let mut input = io::Cursor::new(b"\"abcdef\"\n1\n".to_vec()); |
| 1514 | 1583 | run_io( |
| 1515 | 1584 | &mut input, |
| 1516 | 1585 | &mut out, |
| @@ -1519,20 +1588,34 @@ mod tests { |
| 1519 | 1588 | false, |
| 1520 | 1589 | Path::new("."), |
| 1521 | 1590 | Limits { |
| 1522 | | − program_output_bytes: 2, |
| 1591 | + debug_bytes: 4, |
| 1523 | 1592 | ..Limits::default() |
| 1524 | 1593 | }, |
| 1525 | 1594 | ) |
| 1526 | 1595 | .unwrap(); |
| 1527 | | − // The captured prefix is the two configured bytes; there's no partial |
| 1528 | | − // `()` result after the failed output write. Isolation lets `1` then run. |
| 1529 | | − assert_eq!(out, b"ab1\n"); |
| 1596 | + // `Debug "abcdef"` is eight bytes; the cap rejects all of them. |
| 1597 | + assert_eq!(out, b"1\n"); |
| 1530 | 1598 | let err = String::from_utf8(err).unwrap(); |
| 1531 | | − assert!(err.contains("program stdout exceeded 2 bytes"), "{err}"); |
| 1532 | | − assert!( |
| 1533 | | − err.contains("output: program output limit exceeded"), |
| 1534 | | − "{err}" |
| 1599 | + assert!(err.contains("OutputBytes"), "{err}"); |
| 1600 | + assert!(err.contains("(8 > 4)"), "{err}"); |
| 1601 | + assert!(!err.contains("interpreter crashed"), "{err}"); |
| 1602 | + } |
| 1603 | + |
| 1604 | + #[test] |
| 1605 | + fn program_output_limit_is_bounded_and_the_next_expression_still_works() { |
| 1606 | + let (out, err) = run_with_limits( |
| 1607 | + b"import std/io\nio.println \"1234\"\nio.eprintln \"abcde\"\n1\n", |
| 1608 | + Limits { |
| 1609 | + program_output_bytes: 5, |
| 1610 | + ..Limits::default() |
| 1611 | + }, |
| 1535 | 1612 | ); |
| 1613 | + // stdout's five bytes fit exactly. B1 then preflights the six stderr |
| 1614 | + // bytes against the same entry budget; C's writer adds no second charge. |
| 1615 | + // No partial `()` or eprintln value is emitted, and the next entry resets. |
| 1616 | + assert_eq!(out, "1234\n1\n"); |
| 1617 | + assert!(err.contains("OutputBytes"), "{err}"); |
| 1618 | + assert!(!err.contains("abcde"), "{err}"); |
| 1536 | 1619 | } |
| 1537 | 1620 | |
| 1538 | 1621 | #[test] |