From 4ffc7c6807b4f7f932fe9863b1a845b6f681d965 Mon Sep 17 00:00:00 2001 From: James Ross Date: Wed, 18 Nov 2026 23:05:12 -0800 Subject: [PATCH] fix(cost): charge v2 allocation for values built by prelude calls Under the shared-read schedule every Call node was charged one step and no allocation, and byte, text, integer and Option operations added steps only. So core.bytes.concat, core.bytes.slice and the Option operations built values for zero charged bytes, contradicting the README and maxAllocatedBytes as cumulative logical allocation (#328). A prelude call without a shared-read rule is now charged its result's full size, as under v1. Source and native Target share the meter. The reference evaluator gains byte values, concat, slice, length and the Option constructors. RED: the evaluator metered 13,333 bytes against a 4,309-byte v2 minimum, and eight extra rounds added only 3,088 / 2,600 bytes. GREEN: 4/4; existing schedule tests sound; cargo xtask verify exit 0. Flat ReplaceRange (jedit#410 @ 67a85dc): 261,523,656 -> 261,826,990 steps, 175,260,814 -> 213,473,665 bytes; Target lowers at the new minimum. Agent-Session: AFC0A5E0-B032-49DC-8B18-CB932831DCC2 Agent-Tool: Claude Code --- CHANGELOG.md | 9 + .../src/target_ir/fold_work/shared_reads.rs | 9 + .../tests/core_cost_schedules/reference.rs | 37 ++++ .../tests/shared_read_result_allocation.rs | 183 ++++++++++++++++++ docs/topics/core-cost-schedules/README.md | 8 +- docs/topics/core-cost-schedules/test-plan.md | 2 + 6 files changed, 245 insertions(+), 3 deletions(-) create mode 100644 crates/amblicit-syntax/tests/shared_read_result_allocation.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f6e7e58..37661bb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,15 @@ versions still track specification maturity rather than a released product. ## [Unreleased] +- Fix the shared-read cost schedule charging no allocation for values built by + prelude calls. `core.bytes.concat`, `core.bytes.slice`, text concatenation, + integer operations and the `Option` operations now charge their result's + full size in steps and allocated bytes, as the documentation stated and as + `maxAllocatedBytes` (cumulative logical allocation) requires. Source + admission and native Target share the meter. Jedit's flat ReplaceRange + minimum moves from 261,523,656 steps and 175,260,814 bytes to 261,826,990 + steps and 213,473,665 bytes (203.6 MiB) (issue #339). + - Fix native Target refusing shared-read modules that call a target pure intrinsic from a bounded `for` body, directly or through a helper, with "target pure-intrinsic cost is unestablished or exceeds the step budget". diff --git a/crates/amblicit-syntax/src/target_ir/fold_work/shared_reads.rs b/crates/amblicit-syntax/src/target_ir/fold_work/shared_reads.rs index e36001be..982823d8 100644 --- a/crates/amblicit-syntax/src/target_ir/fold_work/shared_reads.rs +++ b/crates/amblicit-syntax/src/target_ir/fold_work/shared_reads.rs @@ -49,6 +49,15 @@ impl<'a> Meter<'a> { .checked_add(u64::try_from(case.len()).ok()?)?, })? .evaluation(), + // A prelude call without a shared-read rule builds a new value + // (bytes, text, integers, Options): charge it as under v1. + CoreExpr::Call { callee, .. } + if (super::is_typed_prelude_operation(callee) + && !Self::has_shared_rule(callee)) + || callee == "core.string.concat" => + { + self.value(expr, available, depth)?.evaluation() + } CoreExpr::Local { .. } | CoreExpr::Field { .. } | CoreExpr::If { .. } diff --git a/crates/amblicit-syntax/tests/core_cost_schedules/reference.rs b/crates/amblicit-syntax/tests/core_cost_schedules/reference.rs index a52866a8..a1306e50 100644 --- a/crates/amblicit-syntax/tests/core_cost_schedules/reference.rs +++ b/crates/amblicit-syntax/tests/core_cost_schedules/reference.rs @@ -19,6 +19,8 @@ pub enum V { Rec(BTreeMap), List(Vec), Var(String, String, Option>), + Bytes(Vec), + Opt(Option>), } #[derive(Debug, Default, Clone, Copy)] @@ -49,6 +51,11 @@ pub fn full(value: &V) -> (u64, u64) { let (pc, pb) = payload.as_deref().map_or((1, 64), full); (6 + pc, 399 + ty.len() as u64 + case.len() as u64 + pb) } + V::Bytes(bytes) => (1, 64 + bytes.len() as u64), + V::Opt(item) => { + let (ic, ib) = item.as_deref().map_or((0, 0), full); + (1 + ic, 64 + ib) + } } } @@ -61,6 +68,8 @@ fn shell(value: &V) -> (u64, u64) { .fold((1, 64), |(c, b), key| (c + 1, b + 64 + key.len() as u64)), V::List(items) => (1 + items.len() as u64, 64 * (1 + items.len() as u64)), V::Var(ty, case, _) => (7, 64 + 399 + ty.len() as u64 + case.len() as u64), + // Built by a prelude call: charged at their full actual size. + V::Bytes(_) | V::Opt(_) => full(value), } } @@ -281,6 +290,34 @@ impl Evaluator<'_> { left.extend(right); self.build(V::List(left)) } + "core.bytes.concat" => { + let (V::Bytes(mut left), V::Bytes(right)) = (args[0].clone(), args[1].clone()) + else { + panic!("byte operands") + }; + left.extend(right); + self.build(V::Bytes(left)) + } + "core.bytes.slice" => { + let V::Bytes(bytes) = &args[0] else { + panic!("byte operand") + }; + let (from, to) = (int(&args[1]) as usize, int(&args[2]) as usize); + self.build(V::Bytes(bytes[from..to].to_vec())) + } + "core.bytes.length" => match &args[0] { + V::Bytes(bytes) => self.build(V::Int(bytes.len() as u128)), + other => panic!("byte operand, got {other:?}"), + }, + "core.option.some" => { + let item = args.pop().unwrap(); + self.build(V::Opt(Some(Box::new(item)))) + } + "core.option.none" => self.build(V::Opt(None)), + "core.option.isSome" => match &args[0] { + V::Opt(item) => self.build(V::Bool(item.is_some())), + other => panic!("option operand, got {other:?}"), + }, "core.integer.add" => { let sum = int(&args[0]) + int(&args[1]); self.build(V::Int(sum)) diff --git a/crates/amblicit-syntax/tests/shared_read_result_allocation.rs b/crates/amblicit-syntax/tests/shared_read_result_allocation.rs new file mode 100644 index 00000000..52f0f8f5 --- /dev/null +++ b/crates/amblicit-syntax/tests/shared_read_result_allocation.rs @@ -0,0 +1,183 @@ +//! Shared-read schedule: prelude calls that build a new value charge its +//! allocation (#339). +//! +//! Oracles: `maxAllocatedBytes` is cumulative logical allocation (#328), so a +//! fold that builds a byte or Option value every round allocates at least that +//! value's size every round; and the reference evaluator meters every built +//! value at its actual size and must stay within the minimum budget. +#[path = "core_cost_schedules/reference.rs"] +mod reference; + +use amblicit_syntax::{ + compile_to_core, lower_to_target_ir, parse_module, CompilerContext, CoreBudget, + CoreCostSchedule, CoreModule, ResourceRef, TargetIrLoweringFacts, TargetLoweringStatus, +}; +use reference::{Evaluator, V}; +use std::collections::BTreeMap; + +const CEILING: u64 = u64::MAX / 4; +const ROUNDS: u64 = 8; + +fn budget(steps: u64, bytes: u64) -> CoreBudget { + CoreBudget { + max_steps: steps, + max_allocated_bytes: bytes, + max_output_bytes: CEILING, + } +} + +fn compile( + source: &str, + budget: CoreBudget, +) -> Result> { + compile_to_core( + &parse_module(source).expect("fixture parses"), + &CompilerContext::new() + .with_core_cost_schedule(CoreCostSchedule::SharedReads) + .with_operation_profile("p.read", "continuum.profile.read-only/v1") + .with_budget("p.b", budget), + ) +} + +fn minimum(mut accepts: impl FnMut(u64) -> bool) -> u64 { + assert!(accepts(CEILING), "generous-budget control"); + let (mut low, mut high) = (0, CEILING); + while low < high { + let middle = low + (high - low) / 2; + if accepts(middle) { + high = middle; + } else { + low = middle + 1; + } + } + high +} + +fn minimums(source: &str) -> (u64, u64) { + if let Err(errors) = compile(source, budget(CEILING, CEILING)) { + panic!("fixture compiles: {errors:#?}"); + } + ( + minimum(|v| compile(source, budget(v, CEILING)).is_ok()), + minimum(|v| compile(source, budget(CEILING, v)).is_ok()), + ) +} + +/// Each round concatenates two 256-byte values and slices the result. +const BYTES: &str = "package cost.alloc@1; +type State = { a: Bytes, n: U64 }; +fn step(s: State) -> State { + let joined = s.a + s.a; + let part = slice(joined, 0u64, len(joined)); + return { a: s.a, n: len(part) }; +} +type In = { a: Bytes }; +type Out = { n: U64 }; +intent run(input: In) returns Out profile p.read basis none budget <= p.b { + let initial: State = { a: input.a, n: 0u64 }; + let final = fold(initial, step, ROUNDSu64); + return { n: final.n }; +}"; + +/// Each round builds a present Option of a 256-byte value and reads it back. +const OPTIONS: &str = "package cost.alloc@1; +type State = { a: Bytes, kept: Option> }; +fn step(s: State) -> State { return { a: s.a, kept: some(s.a) }; } +type In = { a: Bytes }; +type Out = { present: Bool }; +intent run(input: In) returns Out profile p.read basis none budget <= p.b { + let initial: State = { a: input.a, kept: none() }; + let final = fold(initial, step, ROUNDSu64); + return { present: isSome(final.kept) }; +}"; + +/// The source with its fold bound set to `rounds`. +fn rounds(source: &str, rounds: u64) -> String { + source.replace("ROUNDSu64", &format!("{rounds}u64")) +} + +/// Allocation the minimum budget adds for `ROUNDS` more fold rounds. +fn added_bytes_per_extra_rounds(source: &str) -> u64 { + minimums(&rounds(source, 2 * ROUNDS)).1 - minimums(&rounds(source, ROUNDS)).1 +} + +#[test] +fn byte_results_built_each_round_are_charged_their_allocation() { + // Each round, concat and slice each build a value of up to 512 bytes, + // one cell (64 bytes) plus its payload. + let added = added_bytes_per_extra_rounds(BYTES); + assert!(added >= ROUNDS * 2 * (64 + 512), "added bytes {added}"); +} + +#[test] +fn option_results_built_each_round_are_charged_their_allocation() { + // A present Option is one cell plus its 256-byte payload value. + let added = added_bytes_per_extra_rounds(OPTIONS); + assert!(added >= ROUNDS * (64 + 64 + 256), "added bytes {added}"); +} + +fn facts() -> TargetIrLoweringFacts { + TargetIrLoweringFacts { + target_profile: ResourceRef { + coordinate: "echo.dpo@1".into(), + digest: Some(format!("sha256:{}", "1".repeat(64))), + }, + target_ir_domain: "echo.span-ir/v3".into(), + operation_profiles: vec!["continuum.profile.read-only/v1".into()], + obstruction_coordinates: Vec::new(), + effect_lowerings: Vec::new(), + effect_signatures: Vec::new(), + pure_functions: Vec::new(), + } +} + +#[test] +fn native_target_refuses_one_byte_below_the_source_minimum() { + for source in [rounds(BYTES, ROUNDS), rounds(OPTIONS, ROUNDS)] { + let source = source.as_str(); + let (steps, bytes) = minimums(source); + let core = compile(source, budget(steps, bytes)).expect("source minimum compiles"); + assert_eq!( + lower_to_target_ir(&core, &facts()).status, + TargetLoweringStatus::Lowered + ); + let mut below = core.clone(); + below.intents.get_mut("run").unwrap().core_evaluation_budget = budget(steps, bytes - 1); + assert_ne!( + lower_to_target_ir(&below, &facts()).status, + TargetLoweringStatus::Lowered + ); + } +} + +#[test] +fn built_byte_and_option_values_stay_within_budget() { + for (name, source) in [ + ("bytes", rounds(BYTES, ROUNDS)), + ("options", rounds(OPTIONS, ROUNDS)), + ] { + let source = source.as_str(); + let (steps, bytes) = minimums(source); + let core = compile(source, budget(steps, bytes)).expect("accepted at its minimums"); + let mut evaluator = Evaluator { + core: &core, + meter: reference::Meter::default(), + foreign: BTreeMap::new(), + }; + let input = V::Rec(BTreeMap::from([("a".to_owned(), V::Bytes(vec![7; 256]))])); + let outcome = evaluator.intent("run", input, Clone::clone); + let metered = evaluator.meter; + println!("{name}: static steps={steps} bytes={bytes}; metered {metered:?}; {outcome:?}"); + assert!(outcome.is_ok(), "{name}: {outcome:?}"); + assert!( + metered.steps <= steps, + "{name}: metered {} > {steps}", + metered.steps + ); + assert!( + metered.bytes <= bytes, + "{name}: metered {} > {bytes}", + metered.bytes + ); + } +} diff --git a/docs/topics/core-cost-schedules/README.md b/docs/topics/core-cost-schedules/README.md index f77e8b19..104aefaf 100644 --- a/docs/topics/core-cost-schedules/README.md +++ b/docs/topics/core-cost-schedules/README.md @@ -36,9 +36,11 @@ Values are immutable and shared by reference. - A match payload binder costs one step; dispatch costs one step per arm plus one. - Unchanged from the copying schedule: comparisons scan both operands in full, - intent input and output are charged at full size, and every other - operation (byte, text, integer, Option, effect and target intrinsic) keeps - its copying charge. + intent input and output are charged at full size, and every prelude call + that builds a new value (byte and text operations, integer operations, + `Option` constructors and readers) is charged that value's full size as + steps and allocated bytes, plus its copying-schedule call work. Effects and + target intrinsics keep their own rules below. A new list shell (`listAppend`, `listConcat`) is sized by the largest length the list can provably have, when one is known, instead of its declared diff --git a/docs/topics/core-cost-schedules/test-plan.md b/docs/topics/core-cost-schedules/test-plan.md index 01ca39c4..d8aac9ca 100644 --- a/docs/topics/core-cost-schedules/test-plan.md +++ b/docs/topics/core-cost-schedules/test-plan.md @@ -19,6 +19,7 @@ each consumer. | COST-REQ-007 | implemented | Under the shared-read schedule, lawpack helper and target intrinsic calls are charged full argument and result copies plus their declared templates, and the source checker uses the same authenticated facts as Target. | EDICT-LANG-COST-SCHEDULE-001 | | COST-REQ-008 | implemented | Under the shared-read schedule, list shells and comparison scans inside folds and calls are charged by the abstract length a value can provably have reached, never above the declared maximum. | EDICT-LANG-COST-SCHEDULE-001 | | COST-REQ-009 | implemented | Under the shared-read schedule, native Target's intrinsic-cost check charges literal-bound loop bodies per iteration, so a module calling intrinsics from loops, helpers and fold steps lowers at its source minimum and refuses one step below it. | EDICT-LANG-COST-SCHEDULE-001 | +| COST-REQ-010 | implemented | Under the shared-read schedule, every prelude call that builds a new value charges that value's full size in steps and allocated bytes. | EDICT-LANG-COST-SCHEDULE-001 | | COST-REQ-005 | implemented | No program accepted under a shared-read budget does more metered work than the budget on a reference evaluator that implements the schedule. | EDICT-LANG-COST-SCHEDULE-001 | ## Test Cases @@ -36,3 +37,4 @@ each consumer. | COST-TP-009 | implemented | Refusal | COST-REQ-007 | An external-action application selecting the shared-read schedule refuses with `InvalidBound`. | `shared_read_build_refuses_unmetered_external_action_requests` | crates/amblicit-cli/tests/lawpack_authoring_cli.rs | Documented limit. | | COST-TP-010 | implemented | Precision | COST-REQ-008 | A declared capacity the fold cannot reach leaves the minimum unchanged, flat and nested; native Target admits the source minimum and refuses one step less; the reference evaluator never exceeds the minimum on growing, saturating, nested and replaced-list programs. | `unreachable_list_capacity_does_not_change_the_fold_minimum`, `nested_fold_growth_is_charged_by_reachable_length`, `native_target_recomputes_the_reachable_minimum`, `fold_growth_to_and_past_capacity_stays_within_budget`, `replaced_and_concatenated_lists_stay_within_budget` | crates/amblicit-syntax/tests/fold_reachable_bounds.rs | Calibrated: an append that adds no length, and a fixed point that charges one remaining round, each fail the soundness tests. | | COST-TP-011 | implemented | Target agreement | COST-REQ-009 | Intrinsic calls in a loop body, through a helper and in a fold step lower at the source minimum and refuse one step below; the reference evaluator stays within budget on a full input list. | `native_target_lowers_loop_intrinsics_at_the_shared_read_source_minimum`, `native_target_still_refuses_one_step_below_the_loop_minimum`, `loop_and_helper_intrinsic_calls_stay_within_budget` | crates/amblicit-syntax/tests/shared_read_foreign_calls.rs | RED before #337: Target refused with the pure-intrinsic cost diagnostic. | +| COST-TP-012 | implemented | Result allocation | COST-REQ-010 | Eight extra fold rounds add at least each round's byte concat and slice results, or present Option, to the minimum allocation; native Target refuses one byte below the source minimum; the reference evaluator stays within budget. | `byte_results_built_each_round_are_charged_their_allocation`, `option_results_built_each_round_are_charged_their_allocation`, `native_target_refuses_one_byte_below_the_source_minimum`, `built_byte_and_option_values_stay_within_budget` | crates/amblicit-syntax/tests/shared_read_result_allocation.rs | RED before #339: the evaluator metered 13,333 bytes against a 4,309-byte minimum. |