Skip to main content

kcl_lib/execution/
state.rs

1use std::collections::BTreeMap;
2use std::str::FromStr;
3use std::sync::Arc;
4
5use ahash::AHashMap;
6use anyhow::Result;
7use indexmap::IndexMap;
8use kcl_api::UnitAngle;
9use kcl_api::UnitLength;
10use serde::Deserialize;
11use serde::Serialize;
12use uuid::Uuid;
13
14use crate::CompilationIssue;
15use crate::ExecutorContext;
16use crate::KclErrorWithOutputs;
17use crate::MockConfig;
18use crate::NodePath;
19use crate::SegmentDragAnchor;
20use crate::SourceRange;
21use crate::collections::AhashIndexSet;
22use crate::engine::engine_manager::EngineManager;
23use crate::errors::KclError;
24use crate::errors::KclErrorDetails;
25use crate::errors::Severity;
26use crate::exec::DefaultPlanes;
27use crate::execution::Artifact;
28use crate::execution::ArtifactCommand;
29use crate::execution::ArtifactGraph;
30use crate::execution::ArtifactId;
31use crate::execution::ConstrainableLine2d;
32use crate::execution::EnvironmentRef;
33use crate::execution::ExecOutcome;
34use crate::execution::ExecutorSettings;
35use crate::execution::KclValue;
36use crate::execution::KclValueView;
37use crate::execution::OperationCallbackArgs;
38use crate::execution::OperationsByModule;
39use crate::execution::ProgramLookup;
40use crate::execution::SketchVarId;
41use crate::execution::UnsolvedSegment;
42use crate::execution::annotations;
43use crate::execution::cad_op::Operation;
44use crate::execution::id_generator::IdGenerator;
45#[cfg(test)]
46use crate::execution::memory::MemoryBackendKind;
47use crate::execution::memory::ProgramMemory;
48use crate::execution::memory::Stack;
49use crate::execution::sketch_solve::Solved;
50use crate::execution::types::NumericType;
51use crate::front::Number;
52use crate::front::Object;
53use crate::front::ObjectId;
54use crate::front::ObjectKind;
55use crate::id::IncIdGenerator;
56use crate::modules::ModuleId;
57use crate::modules::ModuleInfo;
58use crate::modules::ModuleLoader;
59use crate::modules::ModulePath;
60use crate::modules::ModuleRepr;
61use crate::modules::ModuleSource;
62use crate::parsing::ast::types::Annotation;
63use crate::parsing::ast::types::NodeRef;
64use crate::parsing::ast::types::TagNode;
65
66/// State for executing a program.
67#[derive(Debug, Clone)]
68pub struct ExecState {
69    pub(super) execution_callbacks: Option<std::sync::Arc<dyn crate::execution::ExecutionCallbacks>>,
70    pub(super) global: GlobalState,
71    pub(super) mod_local: ModuleState,
72}
73
74pub type ModuleInfoMap = IndexMap<ModuleId, ModuleInfo>;
75
76#[derive(Debug, Clone)]
77pub(super) struct GlobalState {
78    /// The deepest machine-executor call depth reached by executions sharing
79    /// this state: the root module, its callbacks, and module bodies executed
80    /// inline on it. Imported modules pre-executed in parallel run on cloned
81    /// state whose counter is dropped, so their depths are not aggregated
82    /// here. Used to survey real-world depth against the runaway guard's
83    /// limit; see `machine::DEFAULT_MACHINE_CALL_DEPTH_LIMIT`.
84    pub(crate) machine_depth_high_water: usize,
85    /// Map from source file absolute path to module ID.
86    pub path_to_source_id: IndexMap<ModulePath, ModuleId>,
87    /// Map from module ID to source file.
88    pub id_to_source: IndexMap<ModuleId, ModuleSource>,
89    /// Map from module ID to module info.
90    pub module_infos: ModuleInfoMap,
91    /// Module loader.
92    pub mod_loader: ModuleLoader,
93    /// Errors and warnings.
94    pub issues: Vec<CompilationIssue>,
95    /// If set, use this version only when deciding whether to emit
96    /// `deprecated_since` warnings. Runtime behavior still uses the version
97    /// declared by the KCL program.
98    pub deprecation_version_override: Option<String>,
99    /// Global artifacts that represent the entire program.
100    pub artifacts: ArtifactState,
101    /// Artifacts for only the root module.
102    pub root_module_artifacts: ModuleArtifactState,
103    /// The segments that were edited that triggered this execution.
104    pub segment_ids_edited: AhashIndexSet<ObjectId>,
105    /// Segment-body drag anchors that temporarily pull a point on a segment toward the cursor.
106    pub drag_anchors: Vec<SegmentDragAnchor>,
107    /// True if this execution is sketch mode execution, executing a single
108    /// sketch block. Unlike [`ModuleState::sketch_mode`], this is constant for
109    /// the entire execution, including while executing the body of the sketch
110    /// block being edited.
111    pub sketch_mode: bool,
112}
113
114impl GlobalState {
115    pub(crate) fn operations_by_module(&self) -> OperationsByModule {
116        let mut operations = OperationsByModule::default();
117        operations.insert(ModuleId::default(), self.root_module_artifacts.operations.clone());
118
119        for (module_id, module_info) in &self.module_infos {
120            match &module_info.repr {
121                ModuleRepr::Root => {}
122                ModuleRepr::Kcl(_, Some(outcome)) => {
123                    operations.insert(*module_id, outcome.artifacts.operations.clone());
124                }
125                ModuleRepr::Foreign(_, Some((_, artifacts))) => {
126                    operations.insert(*module_id, artifacts.operations.clone());
127                }
128                ModuleRepr::Kcl(_, None) | ModuleRepr::Foreign(_, None) | ModuleRepr::Dummy => {}
129            }
130        }
131
132        operations
133    }
134}
135
136#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
137pub(crate) enum ConstraintKey {
138    LineCircle([usize; 10]),
139    CircleCircle([usize; 12]),
140}
141
142#[derive(Debug, Clone, Copy, PartialEq, Eq)]
143pub(crate) enum TangencyMode {
144    LineCircle(ezpz::LineSide),
145    CircleCircle(ezpz::CircleSide),
146}
147
148#[derive(Debug, Clone, Copy, PartialEq, Eq)]
149pub(crate) enum ConstraintState {
150    Tangency(TangencyMode),
151}
152
153#[derive(Debug, Clone, Default)]
154pub(super) struct ArtifactState {
155    /// Internal map of UUIDs to exec artifacts.  This needs to persist across
156    /// executions to allow the graph building to refer to cached artifacts.
157    pub artifacts: IndexMap<ArtifactId, Artifact>,
158    /// Output artifact graph.
159    pub graph: ArtifactGraph,
160}
161
162/// Which stdlib edge function produced this refactor metadata (for lint/code mod).
163#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
164#[ts(export)]
165#[serde(rename_all = "camelCase")]
166pub enum EdgeRefactorStdlibFn {
167    GetOppositeEdge,
168    GetNextAdjacentEdge,
169    GetPreviousAdjacentEdge,
170    GetCommonEdge,
171    EdgeId,
172}
173
174/// Metadata collected when a deprecated edge stdlib function runs, for refactor-to-edgeRefs lint/code mod.
175#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
176#[ts(export)]
177#[serde(rename_all = "camelCase")]
178pub struct EdgeRefactorMeta {
179    pub edge_id: Uuid,
180    pub face_ids: [Uuid; 2],
181    #[serde(default, skip_serializing_if = "Vec::is_empty")]
182    pub end_face_ids: Vec<Uuid>,
183    pub source_range: SourceRange,
184    pub stdlib_fn: EdgeRefactorStdlibFn,
185}
186
187/// Metadata for a deprecated edge stdlib function whose edge ID was resolved,
188/// but whose adjacent face IDs could not be recorded at the helper callsite.
189#[derive(Debug, Clone, PartialEq, Eq)]
190pub(crate) struct PendingEdgeRefactorMeta {
191    pub edge_id: Uuid,
192    pub source_range: SourceRange,
193    pub stdlib_fn: EdgeRefactorStdlibFn,
194}
195
196/// One tag entry in a fillet/chamfer call that used `tags` directly (for refactor to edgeRefs).
197#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
198#[ts(export)]
199#[serde(rename_all = "camelCase")]
200pub struct DirectTagFilletTagEntry {
201    pub tag_identifier: String,
202    pub edge_id: Uuid,
203    pub face_ids: [Uuid; 2],
204}
205
206/// Metadata for one fillet/chamfer call that used `tags` directly (no stdlib call).
207#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
208#[ts(export)]
209#[serde(rename_all = "camelCase")]
210pub struct DirectTagFilletMeta {
211    pub call_source_range: SourceRange,
212    pub tags: Vec<DirectTagFilletTagEntry>,
213}
214
215/// Information needed to rewrite one legacy `angle` call while preserving its
216/// currently solved directed-angle branch.
217#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
218#[ts(export)]
219#[serde(rename_all = "camelCase")]
220pub struct LegacyAngleRefactorMeta {
221    pub source_range: SourceRange,
222    pub sector: u8,
223    pub inverse: bool,
224}
225
226/// Unified metadata stream for Z0006 and future execution-backed refactors.
227#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
228#[ts(export)]
229#[serde(tag = "kind", content = "data", rename_all = "camelCase")]
230pub enum RefactorMetadata {
231    EdgeRefactor(Box<EdgeRefactorMeta>),
232    DirectTagFillet(DirectTagFilletMeta),
233    LegacyAngle(LegacyAngleRefactorMeta),
234}
235
236#[derive(Debug, Clone)]
237pub(crate) struct PendingLegacyAngleRefactorMeta {
238    pub source_range: SourceRange,
239    pub lines: [ConstrainableLine2d; 2],
240    pub desired_angle_radians: f64,
241}
242
243/// Artifact state for a single module.
244#[derive(Debug, Clone, Default, PartialEq, Serialize)]
245pub struct ModuleArtifactState {
246    /// Internal map of UUIDs to exec artifacts.
247    pub artifacts: IndexMap<ArtifactId, Artifact>,
248    /// Outgoing engine commands that have not yet been processed and integrated
249    /// into the artifact graph.
250    #[serde(skip)]
251    pub unprocessed_commands: Vec<ArtifactCommand>,
252    /// Outgoing engine commands.
253    pub commands: Vec<ArtifactCommand>,
254    /// Incoming engine commands.
255    #[cfg(feature = "snapshot-engine-responses")]
256    pub responses: IndexMap<Uuid, kittycad_modeling_cmds::websocket::WebSocketResponse>,
257    /// Operations that have been performed in execution order, for display in
258    /// the Feature Tree.
259    pub operations: Vec<Operation>,
260    /// [`ObjectId`] generator.
261    pub object_id_generator: IncIdGenerator<usize>,
262    /// Objects in the scene, created from execution.
263    pub scene_objects: Vec<Object>,
264    /// Map from source range to object ID for lookup of objects by their source
265    /// range.
266    pub source_range_to_object: BTreeMap<SourceRange, ObjectId>,
267    /// Map from artifact ID to object ID in the scene.
268    pub artifact_id_to_scene_object: IndexMap<ArtifactId, ObjectId>,
269    /// Solutions for sketch variables.
270    pub var_solutions: Vec<(SourceRange, Option<NodePath>, Number)>,
271    /// Metadata collected during execution for refactor lint/code-mod paths (Z0006 and future).
272    pub refactor_metadata: Vec<RefactorMetadata>,
273    /// Deprecated edge helper callsites that may be completed by a downstream
274    /// operation that knows the target solid.
275    #[serde(skip)]
276    pub(crate) pending_edge_refactor_metadata: Vec<PendingEdgeRefactorMeta>,
277}
278
279#[derive(Debug, Clone)]
280pub(super) struct ModuleState {
281    /// The id of this module.
282    pub module_id: ModuleId,
283    /// The id generator for this module.
284    pub id_generator: IdGenerator,
285    pub stack: Stack,
286    /// The size of the call stack. This is used to prevent stack overflows with
287    /// recursive function calls. In general, this doesn't match `stack`'s size
288    /// since it's conservative in reclaiming frames between executions.
289    pub(super) call_stack_size: usize,
290    /// Live call depth of the machine executor within this module, for its
291    /// runaway-recursion guard. The machine's analog of `call_stack_size`.
292    pub(crate) machine_call_depth: usize,
293    /// The current value of the pipe operator returned from the previous
294    /// expression.  If we're not currently in a pipeline, this will be None.
295    pub pipe_value: Option<KclValue>,
296    /// The closest variable declaration being executed in any parent node in the AST.
297    /// This is used to provide better error messages, e.g. noticing when the user is trying
298    /// to use the variable `length` inside the RHS of its own definition, like `length = tan(length)`.
299    pub being_declared: Option<String>,
300    /// Present if we're currently executing inside a sketch block.
301    pub sketch_block: Option<SketchBlockState>,
302    /// Tracks if KCL being executed is currently inside a stdlib function or not.
303    /// This matters because e.g. we shouldn't emit artifacts from declarations declared inside a stdlib function.
304    pub inside_stdlib: bool,
305    /// The source range where we entered the standard library.
306    pub stdlib_entry_source_range: Option<SourceRange>,
307    /// Identifiers that have been exported from the current module.
308    pub module_exports: Vec<String>,
309    /// Settings specified from annotations.
310    pub settings: MetaSettings,
311    /// True if executing in sketch mode. Only a single sketch block will be
312    /// executed. All other code is ignored.
313    pub sketch_mode: bool,
314    /// True to do more costly analysis of whether the sketch block segments are
315    /// under-constrained. The only time we disable this is when a user is
316    /// dragging segments.
317    pub freedom_analysis: bool,
318    pub(super) explicit_length_units: bool,
319    pub(super) path: ModulePath,
320    /// Artifacts for only this module.
321    pub artifacts: ModuleArtifactState,
322    /// Sticky per-constraint state persisted across sketch-mode mock solves.
323    /// Maps from sketch block ID to a map for that sketch.
324    /// Then the inner map is per constraint (in that sketch block) to its state.
325    pub constraint_state: IndexMap<ObjectId, IndexMap<ConstraintKey, ConstraintState>>,
326
327    pub(super) allowed_warnings: Vec<&'static str>,
328    pub(super) denied_warnings: Vec<&'static str>,
329
330    /// Map from consumed solid values to information about the operation that
331    /// consumed them. Populated by operations that destroy their inputs so that
332    /// subsequent attempts to use a consumed solid produce a clear KCL-level
333    /// error rather than a cryptic engine error.
334    pub(super) consumed_solids: AHashMap<ConsumedSolidKey, ConsumedSolidInfo>,
335    /// Defensive map from consumed engine UUID to consumption info.
336    /// Rust code may create a `Solid` with a consumed `engine_id` and a
337    /// different `instance_id` that was not recorded in `consumed_solids`. When
338    /// the exact key lookup misses, this map lets us reject that solid by
339    /// `engine_id`, unless the key is a recorded operation output.
340    pub(super) consumed_solid_ids: AHashMap<Uuid, ConsumedSolidInfo>,
341    /// Region engine UUIDs consumed by successful modeling operations. Regions
342    /// use the KCL `Sketch` representation, so this state keeps stale Region
343    /// values from reaching an engine object that has become something else.
344    pub(super) consumed_regions: AHashMap<Uuid, ConsumedRegionInfo>,
345}
346
347/// Information about the operation that consumed a Region.
348#[derive(Debug, Clone, Copy)]
349pub(crate) struct ConsumedRegionInfo {
350    operation: ConsumedRegionOperation,
351}
352
353impl ConsumedRegionInfo {
354    pub(crate) fn new(operation: ConsumedRegionOperation) -> Self {
355        Self { operation }
356    }
357
358    pub(crate) fn operation(self) -> ConsumedRegionOperation {
359        self.operation
360    }
361}
362
363#[derive(Debug, Clone, Copy, PartialEq, Eq)]
364pub(crate) enum ConsumedRegionOperation {
365    Extrude,
366    Revolve,
367    Sweep,
368    Delete,
369}
370
371impl std::fmt::Display for ConsumedRegionOperation {
372    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
373        match self {
374            Self::Extrude => f.write_str("extrude"),
375            Self::Revolve => f.write_str("revolve"),
376            Self::Sweep => f.write_str("sweep"),
377            Self::Delete => f.write_str("delete"),
378        }
379    }
380}
381
382/// Internal identity for one runtime KCL solid value.
383#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
384pub(crate) struct ConsumedSolidKey {
385    /// The engine body UUID.
386    engine_id: Uuid,
387    /// Distinguishes this KCL runtime instance from other values that may reuse
388    /// the same engine body UUID.
389    instance_id: Uuid,
390}
391
392impl ConsumedSolidKey {
393    pub(crate) fn new(engine_id: Uuid, instance_id: Uuid) -> Self {
394        Self { engine_id, instance_id }
395    }
396
397    pub(crate) fn engine_id(&self) -> Uuid {
398        self.engine_id
399    }
400
401    pub(crate) fn instance_id(&self) -> Uuid {
402        self.instance_id
403    }
404}
405
406/// Information about a solid value that was consumed by an operation.
407/// Stored in `ModuleState.consumed_solids` so subsequent attempts to use the
408/// solid produce a clear error pointing at the operation that consumed it.
409#[derive(Debug, Clone)]
410pub(crate) struct ConsumedSolidInfo {
411    /// The operation that consumed the solid.
412    operation: ConsumedSolidOperation,
413    /// First returned solid value, used only for replacement suggestions in
414    /// error messages. When present, this key is also included in
415    /// `returned_solid_keys`.
416    suggested_replacement_key: Option<ConsumedSolidKey>,
417    /// All solid values returned by that operation. This is used as the
418    /// allow-list for returned solids that reuse a consumed engine UUID.
419    returned_solid_keys: Vec<ConsumedSolidKey>,
420}
421
422impl ConsumedSolidInfo {
423    pub(crate) fn new(operation: ConsumedSolidOperation, returned_solid_keys: Vec<ConsumedSolidKey>) -> Self {
424        Self {
425            operation,
426            suggested_replacement_key: returned_solid_keys.first().copied(),
427            returned_solid_keys,
428        }
429    }
430
431    pub(crate) fn operation(&self) -> ConsumedSolidOperation {
432        self.operation
433    }
434
435    pub(crate) fn suggested_replacement_key(&self) -> Option<ConsumedSolidKey> {
436        self.suggested_replacement_key
437    }
438
439    pub(crate) fn should_report_reused_engine_id_as_consumed(&self, key: ConsumedSolidKey) -> bool {
440        !self.returned_solid_keys.contains(&key)
441    }
442}
443
444#[derive(Debug, Clone, Copy, PartialEq, Eq)]
445pub(crate) enum ConsumedSolidOperation {
446    Union,
447    Intersect,
448    Subtract,
449    Split,
450    JoinSurfaces,
451}
452
453impl ConsumedSolidOperation {
454    pub(crate) fn indefinite_article(self) -> &'static str {
455        match self {
456            Self::Intersect => "an",
457            Self::Union | Self::Subtract | Self::Split | Self::JoinSurfaces => "a",
458        }
459    }
460}
461
462impl std::fmt::Display for ConsumedSolidOperation {
463    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
464        match self {
465            Self::Union => f.write_str("union"),
466            Self::Intersect => f.write_str("intersect"),
467            Self::Subtract => f.write_str("subtract"),
468            Self::Split => f.write_str("split"),
469            Self::JoinSurfaces => f.write_str("joinSurfaces"),
470        }
471    }
472}
473
474#[derive(Debug, Clone, Default)]
475pub(crate) struct SketchBlockState {
476    pub sketch_vars: Vec<KclValue>,
477    pub sketch_id: Option<ObjectId>,
478    pub sketch_constraints: Vec<ObjectId>,
479    pub solver_constraints: Vec<ezpz::Constraint>,
480    pub solver_optional_constraints: Vec<ezpz::Constraint>,
481    pub needed_by_engine: Vec<UnsolvedSegment>,
482    pub segment_tags: IndexMap<ObjectId, TagNode>,
483    pub pending_legacy_angle_refactor_metadata: Vec<PendingLegacyAngleRefactorMeta>,
484}
485
486impl ExecState {
487    pub fn new(exec_context: &super::ExecutorContext) -> Self {
488        ExecState {
489            execution_callbacks: exec_context.execution_callbacks.clone(),
490            global: GlobalState::new(&exec_context.settings, Default::default()),
491            mod_local: ModuleState::new(ModulePath::Main, ProgramMemory::new(), Default::default(), false, true),
492        }
493    }
494
495    #[cfg(test)]
496    pub(crate) fn new_with_memory_backend(exec_context: &super::ExecutorContext, backend: MemoryBackendKind) -> Self {
497        ExecState {
498            execution_callbacks: exec_context.execution_callbacks.clone(),
499            global: GlobalState::new(&exec_context.settings, Default::default()),
500            mod_local: ModuleState::new(
501                ModulePath::Main,
502                ProgramMemory::new_with_backend(backend),
503                Default::default(),
504                false,
505                true,
506            ),
507        }
508    }
509
510    pub fn new_mock(exec_context: &super::ExecutorContext, mock_config: &MockConfig) -> Self {
511        let segment_ids_edited = mock_config.segment_ids_edited.clone();
512        let mut global = GlobalState::new(&exec_context.settings, segment_ids_edited);
513        global.drag_anchors = mock_config.drag_anchors.clone();
514        global.sketch_mode = mock_config.sketch_block_id.is_some();
515        ExecState {
516            execution_callbacks: exec_context.execution_callbacks.clone(),
517            global,
518            mod_local: ModuleState::new(
519                ModulePath::Main,
520                ProgramMemory::new(),
521                Default::default(),
522                mock_config.sketch_block_id.is_some(),
523                mock_config.freedom_analysis,
524            ),
525        }
526    }
527
528    #[cfg(test)]
529    pub(crate) fn new_mock_with_memory_backend(
530        exec_context: &super::ExecutorContext,
531        mock_config: &MockConfig,
532        backend: MemoryBackendKind,
533    ) -> Self {
534        let segment_ids_edited = mock_config.segment_ids_edited.clone();
535        let mut global = GlobalState::new(&exec_context.settings, segment_ids_edited);
536        global.drag_anchors = mock_config.drag_anchors.clone();
537        global.sketch_mode = mock_config.sketch_block_id.is_some();
538        ExecState {
539            execution_callbacks: exec_context.execution_callbacks.clone(),
540            global,
541            mod_local: ModuleState::new(
542                ModulePath::Main,
543                ProgramMemory::new_with_backend(backend),
544                Default::default(),
545                mock_config.sketch_block_id.is_some(),
546                mock_config.freedom_analysis,
547            ),
548        }
549    }
550
551    pub(super) fn reset(&mut self, exec_context: &super::ExecutorContext) {
552        let global = GlobalState::new(&exec_context.settings, Default::default());
553
554        *self = ExecState {
555            execution_callbacks: exec_context.execution_callbacks.clone(),
556            global,
557            mod_local: ModuleState::new(
558                self.mod_local.path.clone(),
559                ProgramMemory::new(),
560                Default::default(),
561                false,
562                true,
563            ),
564        };
565    }
566
567    /// Log a non-fatal error.
568    pub fn err(&mut self, e: CompilationIssue) {
569        self.global.issues.push(e);
570    }
571
572    /// Log a warning.
573    pub fn warn(&mut self, mut e: CompilationIssue, name: &'static str) {
574        debug_assert!(annotations::WARN_VALUES.contains(&name));
575
576        if self.mod_local.allowed_warnings.contains(&name) {
577            return;
578        }
579
580        if self.mod_local.denied_warnings.contains(&name) {
581            e.severity = Severity::Error;
582        } else {
583            e.severity = Severity::Warning;
584        }
585
586        self.global.issues.push(e);
587    }
588
589    pub fn warn_experimental(&mut self, feature_name: &str, source_range: SourceRange) {
590        let Some(severity) = self.mod_local.settings.experimental_features.severity() else {
591            return;
592        };
593        let error = CompilationIssue {
594            source_range,
595            message: format!("Use of {feature_name} is experimental and may change or be removed."),
596            suggestion: None,
597            severity,
598            tag: crate::errors::Tag::None,
599        };
600
601        self.global.issues.push(error);
602    }
603
604    pub fn clear_units_warnings(&mut self, source_range: &SourceRange) {
605        self.global.issues = std::mem::take(&mut self.global.issues)
606            .into_iter()
607            .filter(|e| {
608                e.severity != Severity::Warning
609                    || !source_range.contains_range(&e.source_range)
610                    || e.tag != crate::errors::Tag::UnknownNumericUnits
611            })
612            .collect();
613    }
614
615    pub fn issues(&self) -> &[CompilationIssue] {
616        &self.global.issues
617    }
618
619    pub(crate) fn deprecation_version(&self) -> &str {
620        self.global
621            .deprecation_version_override
622            .as_deref()
623            .unwrap_or(self.mod_local.settings.kcl_version.as_str())
624    }
625
626    #[cfg(test)]
627    pub(crate) fn set_deprecation_version_override(&mut self, version: Option<&str>) {
628        self.global.deprecation_version_override = version.map(str::to_owned);
629    }
630
631    /// Convert to execution outcome when running in WebAssembly.  We want to
632    /// reduce the amount of data that crosses the WASM boundary as much as
633    /// possible.
634    pub async fn into_exec_outcome(
635        self,
636        main_ref: EnvironmentRef,
637        ctx: &ExecutorContext,
638    ) -> Result<ExecOutcome, KclError> {
639        // Fields are opt-in so that we don't accidentally leak private internal
640        // state when we add more to ExecState.
641        let variables = self
642            .mod_local
643            .variables(main_ref)?
644            .into_iter()
645            .map(|(key, value)| (key, KclValueView::from(value)))
646            .collect();
647        Ok(ExecOutcome {
648            variables,
649            filenames: self.global.filenames(),
650            operations: self.global.operations_by_module(),
651            artifact_graph: self.global.artifacts.graph,
652            scene_objects: self.global.root_module_artifacts.scene_objects,
653            source_range_to_object: self.global.root_module_artifacts.source_range_to_object,
654            var_solutions: self.global.root_module_artifacts.var_solutions,
655            refactor_metadata: self.global.root_module_artifacts.refactor_metadata.clone(),
656            issues: self.global.issues,
657            default_planes: ctx.engine.get_default_planes().read().await.clone(),
658        })
659    }
660
661    #[cfg(feature = "snapshot-engine-responses")]
662    pub(crate) fn take_root_module_responses(
663        &mut self,
664    ) -> IndexMap<Uuid, kittycad_modeling_cmds::websocket::WebSocketResponse> {
665        std::mem::take(&mut self.global.root_module_artifacts.responses)
666    }
667
668    pub(crate) fn stack(&self) -> &Stack {
669        &self.mod_local.stack
670    }
671
672    pub(crate) fn mut_stack(&mut self) -> &mut Stack {
673        &mut self.mod_local.stack
674    }
675
676    /// Increment the user-level call stack size, returning an error if it
677    /// exceeds the maximum.
678    pub(super) fn inc_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
679        // If you change this, make sure to test in WebAssembly in the app since
680        // that's the limiting factor.
681        const LIMIT: usize = 50;
682        if self.mod_local.call_stack_size >= LIMIT {
683            return Err(KclError::new_max_call_stack(KclErrorDetails::new(
684                format!(
685                    "Call depth limit ({LIMIT}) exceeded. This usually means a function is recursing without a base case."
686                ),
687                vec![range],
688            )));
689        }
690        self.mod_local.call_stack_size += 1;
691        Ok(())
692    }
693
694    /// Decrement the user-level call stack size, returning an error if it would
695    /// go below zero.
696    pub(super) fn dec_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
697        // Prevent underflow.
698        if self.mod_local.call_stack_size == 0 {
699            let message = "call stack size below zero".to_owned();
700            debug_assert!(false, "{message}");
701            return Err(KclError::new_internal(KclErrorDetails::new(message, vec![range])));
702        }
703        self.mod_local.call_stack_size -= 1;
704        Ok(())
705    }
706
707    /// The deepest machine-executor call depth reached in this execution.
708    /// The machine maintains the counter in all builds; today only the test
709    /// harnesses' depth survey reads it.
710    // Unused outside test builds, but kept available so release diagnostics
711    // can read the counter the machine already maintains.
712    #[allow(dead_code)]
713    pub(crate) fn machine_depth_high_water(&self) -> usize {
714        self.global.machine_depth_high_water
715    }
716
717    /// Returns true if we're executing in sketch mode for the current module.
718    /// In sketch mode, we still want to execute the prelude and other stdlib
719    /// modules as normal, so it can vary per module within a single overall
720    /// execution.
721    pub(crate) fn sketch_mode(&self) -> bool {
722        self.mod_local.sketch_mode
723            && match &self.mod_local.path {
724                ModulePath::Main => true,
725                ModulePath::Local { .. } => true,
726                ModulePath::Std { .. } => false,
727            }
728    }
729
730    /// Returns true if this execution is sketch mode execution, executing a
731    /// single sketch block. Unlike [`Self::sketch_mode`], this doesn't vary
732    /// during the execution.
733    pub(crate) fn is_sketch_mode_execution(&self) -> bool {
734        self.global.sketch_mode
735    }
736
737    pub fn next_object_id(&mut self) -> ObjectId {
738        ObjectId(self.mod_local.artifacts.object_id_generator.next_id())
739    }
740
741    pub fn peek_object_id(&self) -> ObjectId {
742        ObjectId(self.mod_local.artifacts.object_id_generator.peek_id())
743    }
744
745    pub(crate) fn constraint_state(&self, sketch_block_id: ObjectId, key: &ConstraintKey) -> Option<ConstraintState> {
746        let map = self.mod_local.constraint_state.get(&sketch_block_id)?;
747        map.get(key).copied()
748    }
749
750    pub(crate) fn set_constraint_state(
751        &mut self,
752        sketch_block_id: ObjectId,
753        key: ConstraintKey,
754        state: ConstraintState,
755    ) {
756        let map = self.mod_local.constraint_state.entry(sketch_block_id).or_default();
757        map.insert(key, state);
758    }
759
760    pub fn add_scene_object(&mut self, obj: Object, source_range: SourceRange) -> ObjectId {
761        let id = obj.id;
762        debug_assert!(
763            id.0 == self.mod_local.artifacts.scene_objects.len(),
764            "Adding scene object with ID {} but next ID is {}",
765            id.0,
766            self.mod_local.artifacts.scene_objects.len()
767        );
768        let artifact_id = obj.artifact_id;
769        self.mod_local.artifacts.scene_objects.push(obj);
770        self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
771        self.mod_local
772            .artifacts
773            .artifact_id_to_scene_object
774            .insert(artifact_id, id);
775        id
776    }
777
778    /// Add a placeholder scene object. This is useful when we need to reserve
779    /// an ID before we have all the information to create the full object.
780    pub fn add_placeholder_scene_object(
781        &mut self,
782        id: ObjectId,
783        source_range: SourceRange,
784        node_path: Option<NodePath>,
785    ) -> ObjectId {
786        debug_assert!(id.0 == self.mod_local.artifacts.scene_objects.len());
787        self.mod_local
788            .artifacts
789            .scene_objects
790            .push(Object::placeholder(id, source_range, node_path));
791        self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
792        id
793    }
794
795    /// Update a scene object. This is useful to replace a placeholder.
796    pub fn set_scene_object(&mut self, object: Object) {
797        let id = object.id;
798        let artifact_id = object.artifact_id;
799        self.mod_local.artifacts.scene_objects[id.0] = object;
800        self.mod_local
801            .artifacts
802            .artifact_id_to_scene_object
803            .insert(artifact_id, id);
804    }
805
806    pub fn scene_object_id_by_artifact_id(&self, artifact_id: ArtifactId) -> Option<ObjectId> {
807        self.mod_local
808            .artifacts
809            .artifact_id_to_scene_object
810            .get(&artifact_id)
811            .cloned()
812    }
813
814    pub fn segment_ids_edited_contains(&self, object_id: &ObjectId) -> bool {
815        self.global.segment_ids_edited.contains(object_id)
816    }
817
818    pub fn drag_anchor_target(&self, object_id: &ObjectId) -> Option<&crate::front::Point2d<crate::front::Number>> {
819        self.global
820            .drag_anchors
821            .iter()
822            .find(|anchor| &anchor.segment_id == object_id)
823            .map(|anchor| &anchor.target)
824    }
825
826    pub(super) fn is_in_sketch_block(&self) -> bool {
827        self.mod_local.sketch_block.is_some()
828    }
829
830    pub(crate) fn sketch_block_mut(&mut self) -> Option<&mut SketchBlockState> {
831        self.mod_local.sketch_block.as_mut()
832    }
833
834    pub(crate) fn sketch_block(&mut self) -> Option<&SketchBlockState> {
835        self.mod_local.sketch_block.as_ref()
836    }
837
838    pub fn next_uuid(&mut self) -> Uuid {
839        self.mod_local.id_generator.next_uuid()
840    }
841
842    pub fn next_artifact_id(&mut self) -> ArtifactId {
843        self.mod_local.id_generator.next_artifact_id()
844    }
845
846    pub fn id_generator(&mut self) -> &mut IdGenerator {
847        &mut self.mod_local.id_generator
848    }
849
850    /// Record that a solid value has been consumed by a CSG boolean operation.
851    pub(crate) fn mark_solid_consumed(&mut self, consumed_key: ConsumedSolidKey, info: ConsumedSolidInfo) {
852        self.mod_local.consumed_solids.insert(consumed_key, info);
853    }
854
855    /// Record that an engine body UUID has been consumed by a CSG boolean
856    /// operation.
857    pub(crate) fn mark_solid_id_consumed(&mut self, consumed_id: Uuid, info: ConsumedSolidInfo) {
858        self.mod_local.consumed_solid_ids.insert(consumed_id, info);
859    }
860
861    /// Look up whether a solid value was consumed by a previous CSG boolean
862    /// operation.
863    pub(crate) fn check_solid_consumed(&self, key: &ConsumedSolidKey) -> Option<&ConsumedSolidInfo> {
864        self.mod_local.consumed_solids.get(key)
865    }
866
867    /// Look up whether an engine body UUID was consumed by a previous CSG
868    /// boolean operation.
869    pub(crate) fn check_solid_id_consumed(&self, id: &Uuid) -> Option<&ConsumedSolidInfo> {
870        self.mod_local.consumed_solid_ids.get(id)
871    }
872
873    pub(crate) fn mark_region_consumed(&mut self, id: Uuid, info: ConsumedRegionInfo) {
874        self.mod_local.consumed_regions.insert(id, info);
875    }
876
877    pub(crate) fn check_region_consumed(&self, id: &Uuid) -> Option<ConsumedRegionInfo> {
878        self.mod_local.consumed_regions.get(id).copied()
879    }
880
881    /// Find the current variable containing a Region engine UUID. This runs
882    /// only while constructing a diagnostic, so recursively searching arrays
883    /// and objects is preferable to storing variable names in liveness state.
884    pub(crate) fn find_var_name_for_region_id(&self, target_id: Uuid) -> Result<Option<String>, KclError> {
885        fn contains_region_id(value: &KclValue, target_id: Uuid) -> bool {
886            match value {
887                KclValue::Sketch { value } => value.origin_sketch_id.is_some() && value.id == target_id,
888                KclValue::HomArray { value, .. } | KclValue::Tuple { value, .. } => {
889                    value.iter().any(|value| contains_region_id(value, target_id))
890                }
891                KclValue::Object { value, .. } => value.values().any(|value| contains_region_id(value, target_id)),
892                _ => false,
893            }
894        }
895
896        self.mod_local
897            .stack
898            .find_var_name_in_all_envs(|value| contains_region_id(value, target_id))
899    }
900
901    /// Follow direct replacement links until we find the latest known output.
902    /// Used only on error paths so diagnostics can suggest the current solid.
903    pub(crate) fn latest_consumed_output(
904        &self,
905        suggested_replacement_key: Option<ConsumedSolidKey>,
906    ) -> Option<ConsumedSolidKey> {
907        let mut latest = suggested_replacement_key?;
908        let mut seen = AhashIndexSet::default();
909
910        while seen.insert(latest) {
911            let Some(next) = self
912                .mod_local
913                .consumed_solids
914                .get(&latest)
915                .and_then(|info| info.suggested_replacement_key())
916            else {
917                break;
918            };
919            latest = next;
920        }
921
922        Some(latest)
923    }
924
925    /// Search the live environment for the name of a variable holding a Solid
926    /// (or an array of Solids) whose value identity matches `target_key`. Used only on
927    /// error paths to recover variable names for diagnostics.
928    pub(crate) fn find_var_name_for_solid_key(&self, target_key: ConsumedSolidKey) -> Result<Option<String>, KclError> {
929        fn contains_solid_key(value: &KclValue, target_key: ConsumedSolidKey) -> bool {
930            match value {
931                KclValue::Solid { value } => {
932                    value.id == target_key.engine_id() && value.value_id == target_key.instance_id()
933                }
934                KclValue::HomArray { value, .. } => value.iter().any(|v| contains_solid_key(v, target_key)),
935                _ => false,
936            }
937        }
938        self.mod_local
939            .stack
940            .find_var_name_in_all_envs(|value| contains_solid_key(value, target_key))
941    }
942
943    pub(crate) fn add_artifact(&mut self, artifact: Artifact) {
944        let id = artifact.id();
945        self.mod_local.artifacts.artifacts.insert(id, artifact);
946    }
947
948    /// The declaring module and display name of every named view registered so
949    /// far. `view::named` needs these to reject a name that a view declared by
950    /// the same module already uses.
951    ///
952    /// Both artifact maps are scanned, because incremental re-execution divides
953    /// the views between them:
954    /// - a run that clears the scene empties `global.artifacts` beforehand, so
955    ///   every view it can see is one the current run registered into
956    ///   `mod_local.artifacts`;
957    /// - a run that only appends statements to an unchanged prefix does not
958    ///   re-execute that prefix, so the views the prefix declared stay in
959    ///   `global.artifacts` from the previous run while the appended
960    ///   declarations register into `mod_local.artifacts`.
961    ///
962    /// Reading one map alone would accept a duplicate name on one of those
963    /// paths and reject it on the other, which an author would see as the same
964    /// file being accepted while typed and rejected after an unrelated edit.
965    /// Neither path can report a view against its own earlier registration: a
966    /// re-executed declaration is only reached after `global.artifacts` was
967    /// cleared, and an appended declaration has no earlier registration.
968    pub(crate) fn registered_named_views(&self) -> impl Iterator<Item = (ModuleId, &str)> {
969        self.mod_local
970            .artifacts
971            .artifacts
972            .values()
973            .chain(self.global.artifacts.artifacts.values())
974            .filter_map(|artifact| match artifact {
975                Artifact::NamedView(view) => Some((view.code_ref.range.module_id(), view.name.as_str())),
976                _ => None,
977            })
978    }
979
980    pub(crate) fn artifact_mut(&mut self, id: ArtifactId) -> Option<&mut Artifact> {
981        self.mod_local.artifacts.artifacts.get_mut(&id)
982    }
983
984    pub(crate) fn push_op(&mut self, op: Operation) {
985        let index = self.mod_local.artifacts.operations.len();
986        self.mod_local.artifacts.operations.push(op);
987        if let Some(operation) = self.mod_local.artifacts.operations.last().cloned()
988            && let Some(callbacks) = &self.execution_callbacks
989        {
990            callbacks.on_operation(OperationCallbackArgs {
991                module_id: self.mod_local.module_id,
992                operation,
993                index,
994            });
995        }
996    }
997
998    pub(crate) fn push_command(&mut self, command: ArtifactCommand) {
999        self.mod_local.artifacts.unprocessed_commands.push(command);
1000    }
1001
1002    pub(super) fn next_module_id(&self) -> ModuleId {
1003        ModuleId::from_usize(self.global.path_to_source_id.len())
1004    }
1005
1006    pub(super) fn id_for_module(&self, path: &ModulePath) -> Option<ModuleId> {
1007        self.global.path_to_source_id.get(path).cloned()
1008    }
1009
1010    pub(super) fn add_path_to_source_id(&mut self, path: ModulePath, id: ModuleId) {
1011        debug_assert!(!self.global.path_to_source_id.contains_key(&path));
1012        self.global.path_to_source_id.insert(path, id);
1013    }
1014
1015    pub(crate) fn add_root_module_contents(&mut self, program: &crate::Program) {
1016        let root_id = ModuleId::default();
1017        // Get the path for the root module.
1018        let path = self
1019            .global
1020            .path_to_source_id
1021            .iter()
1022            .find(|(_, v)| **v == root_id)
1023            .unwrap()
1024            .0
1025            .clone();
1026        self.add_id_to_source(
1027            root_id,
1028            ModuleSource {
1029                path,
1030                source: program.original_file_contents.to_string(),
1031            },
1032        );
1033    }
1034
1035    pub(super) fn add_id_to_source(&mut self, id: ModuleId, source: ModuleSource) {
1036        self.global.id_to_source.insert(id, source);
1037    }
1038
1039    pub(super) fn add_module(&mut self, id: ModuleId, path: ModulePath, repr: ModuleRepr) {
1040        debug_assert!(self.global.path_to_source_id.contains_key(&path));
1041        let module_info = ModuleInfo { id, repr, path };
1042        self.global.module_infos.insert(id, module_info);
1043    }
1044
1045    pub fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
1046        self.global.module_infos.get(&id)
1047    }
1048
1049    #[cfg(test)]
1050    pub(crate) fn modules(&self) -> &ModuleInfoMap {
1051        &self.global.module_infos
1052    }
1053
1054    #[cfg(test)]
1055    pub(crate) fn root_module_artifact_state(&self) -> &ModuleArtifactState {
1056        &self.global.root_module_artifacts
1057    }
1058
1059    /// Record metadata from a deprecated edge stdlib call for the Z0006 refactor.
1060    ///
1061    /// This is intentionally collected unconditionally when artifact graph support is enabled.
1062    /// The temporary feature flag only controls whether the lint/action is shown in the app.
1063    pub(crate) fn record_edge_refactor_meta(&mut self, meta: EdgeRefactorMeta) {
1064        self.mod_local
1065            .artifacts
1066            .refactor_metadata
1067            .push(RefactorMetadata::EdgeRefactor(Box::new(meta)));
1068    }
1069
1070    pub(crate) fn record_pending_edge_refactor_meta(&mut self, meta: PendingEdgeRefactorMeta) {
1071        self.mod_local.artifacts.pending_edge_refactor_metadata.push(meta);
1072    }
1073
1074    pub(crate) fn pending_edge_refactor_meta(
1075        &self,
1076        edge_id: Uuid,
1077        argument_source_range: SourceRange,
1078    ) -> Option<PendingEdgeRefactorMeta> {
1079        if let Some(pending) = self
1080            .mod_local
1081            .artifacts
1082            .pending_edge_refactor_metadata
1083            .iter()
1084            .find(|meta| meta.edge_id == edge_id && argument_source_range.contains_range(&meta.source_range))
1085        {
1086            return Some(pending.clone());
1087        }
1088
1089        // A helper assigned to a variable is outside the argument's source
1090        // range. Fall back to the edge ID only when it identifies one helper.
1091        let mut matches = self
1092            .mod_local
1093            .artifacts
1094            .pending_edge_refactor_metadata
1095            .iter()
1096            .filter(|meta| meta.edge_id == edge_id);
1097        let pending = matches.next()?.clone();
1098        matches.next().is_none().then_some(pending)
1099    }
1100
1101    pub(crate) fn record_edge_refactor_meta_from_pending(
1102        &mut self,
1103        edge_id: Uuid,
1104        source_range: SourceRange,
1105        face_ids: [Uuid; 2],
1106    ) -> bool {
1107        if self.mod_local.artifacts.refactor_metadata.iter().any(|meta| {
1108            matches!(
1109                meta,
1110                RefactorMetadata::EdgeRefactor(meta)
1111                    if meta.edge_id == edge_id && meta.source_range == source_range
1112            )
1113        }) {
1114            return true;
1115        }
1116
1117        let exact_pending_meta = self
1118            .mod_local
1119            .artifacts
1120            .pending_edge_refactor_metadata
1121            .iter()
1122            .find(|meta| meta.edge_id == edge_id && meta.source_range == source_range)
1123            .cloned();
1124
1125        let edge_pending_meta = || {
1126            let mut matches = self
1127                .mod_local
1128                .artifacts
1129                .pending_edge_refactor_metadata
1130                .iter()
1131                .filter(|meta| meta.edge_id == edge_id);
1132            let pending_meta = matches.next()?.clone();
1133            matches.next().is_none().then_some(pending_meta)
1134        };
1135
1136        let Some(pending_meta) = exact_pending_meta.or_else(edge_pending_meta) else {
1137            return false;
1138        };
1139
1140        self.record_edge_refactor_meta(EdgeRefactorMeta {
1141            edge_id,
1142            face_ids,
1143            end_face_ids: Vec::new(),
1144            source_range: pending_meta.source_range,
1145            stdlib_fn: pending_meta.stdlib_fn,
1146        });
1147
1148        true
1149    }
1150
1151    /// Record metadata from a fillet/chamfer call that used `tags` directly.
1152    ///
1153    /// This is intentionally collected unconditionally when artifact graph support is enabled.
1154    /// The temporary feature flag only controls whether the lint/action is shown in the app.
1155    pub(crate) fn record_direct_tag_fillet_meta(&mut self, meta: DirectTagFilletMeta) {
1156        self.mod_local
1157            .artifacts
1158            .refactor_metadata
1159            .push(RefactorMetadata::DirectTagFillet(meta));
1160    }
1161
1162    /// Refactor metadata collected when deprecated edge stdlib functions run (for tests and lint).
1163    pub fn edge_refactor_metadata(&self) -> Vec<EdgeRefactorMeta> {
1164        self.global
1165            .root_module_artifacts
1166            .refactor_metadata
1167            .iter()
1168            .filter_map(|m| match m {
1169                RefactorMetadata::EdgeRefactor(meta) => Some(meta.as_ref().clone()),
1170                RefactorMetadata::DirectTagFillet(_) | RefactorMetadata::LegacyAngle(_) => None,
1171            })
1172            .collect()
1173    }
1174
1175    /// Direct-tag fillet/chamfer metadata (for Z0006 code mod).
1176    pub fn direct_tag_fillet_metadata(&self) -> Vec<DirectTagFilletMeta> {
1177        self.global
1178            .root_module_artifacts
1179            .refactor_metadata
1180            .iter()
1181            .filter_map(|m| match m {
1182                RefactorMetadata::EdgeRefactor(_) | RefactorMetadata::LegacyAngle(_) => None,
1183                RefactorMetadata::DirectTagFillet(meta) => Some(meta.clone()),
1184            })
1185            .collect()
1186    }
1187
1188    pub fn current_default_units(&self) -> NumericType {
1189        NumericType::Default {
1190            len: self.length_unit(),
1191            angle: self.angle_unit(),
1192        }
1193    }
1194
1195    pub fn length_unit(&self) -> UnitLength {
1196        self.mod_local.settings.default_length_units
1197    }
1198
1199    pub fn angle_unit(&self) -> UnitAngle {
1200        self.mod_local.settings.default_angle_units
1201    }
1202
1203    pub(super) fn circular_import_error(&self, path: &ModulePath, source_range: SourceRange) -> KclError {
1204        KclError::new_import_cycle(KclErrorDetails::new(
1205            format!(
1206                "circular import of modules is not allowed: {} -> {}",
1207                self.global
1208                    .mod_loader
1209                    .import_stack
1210                    .iter()
1211                    .map(|p| p.to_string_lossy())
1212                    .collect::<Vec<_>>()
1213                    .join(" -> "),
1214                path,
1215            ),
1216            vec![source_range],
1217        ))
1218    }
1219
1220    pub(crate) fn pipe_value(&self) -> Option<&KclValue> {
1221        self.mod_local.pipe_value.as_ref()
1222    }
1223
1224    pub(crate) fn error_with_outputs(
1225        &self,
1226        error: KclError,
1227        main_ref: Option<EnvironmentRef>,
1228        default_planes: Option<DefaultPlanes>,
1229    ) -> KclErrorWithOutputs {
1230        let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = self
1231            .global
1232            .path_to_source_id
1233            .iter()
1234            .map(|(k, v)| ((*v), k.clone()))
1235            .collect();
1236
1237        KclErrorWithOutputs::new(
1238            error,
1239            self.issues().to_vec(),
1240            main_ref
1241                .and_then(|main_ref| self.mod_local.variables(main_ref).ok())
1242                .unwrap_or_default(),
1243            self.global.operations_by_module(),
1244            Default::default(),
1245            self.global.artifacts.graph.clone(),
1246            self.global.root_module_artifacts.scene_objects.clone(),
1247            self.global.root_module_artifacts.source_range_to_object.clone(),
1248            self.global.root_module_artifacts.var_solutions.clone(),
1249            self.global.root_module_artifacts.refactor_metadata.clone(),
1250            module_id_to_module_path,
1251            self.global.id_to_source.clone(),
1252            default_planes,
1253        )
1254    }
1255
1256    pub(crate) fn build_program_lookup(
1257        &self,
1258        current: crate::parsing::ast::types::Node<crate::parsing::ast::types::Program>,
1259    ) -> ProgramLookup {
1260        ProgramLookup::new(current, self.global.module_infos.clone())
1261    }
1262
1263    pub(crate) async fn build_artifact_graph(
1264        &mut self,
1265        engine: &Arc<EngineManager>,
1266        program: NodeRef<'_, crate::parsing::ast::types::Program>,
1267    ) -> Result<(), KclError> {
1268        let mut new_commands = Vec::new();
1269        let mut new_exec_artifacts = IndexMap::new();
1270        for module in self.global.module_infos.values_mut() {
1271            match &mut module.repr {
1272                ModuleRepr::Kcl(_, Some(outcome)) => {
1273                    new_commands.extend(outcome.artifacts.process_commands());
1274                    new_exec_artifacts.extend(outcome.artifacts.artifacts.clone());
1275                }
1276                ModuleRepr::Foreign(_, Some((_, module_artifacts))) => {
1277                    new_commands.extend(module_artifacts.process_commands());
1278                    new_exec_artifacts.extend(module_artifacts.artifacts.clone());
1279                }
1280                ModuleRepr::Root | ModuleRepr::Kcl(_, None) | ModuleRepr::Foreign(_, None) | ModuleRepr::Dummy => {}
1281            }
1282        }
1283        // Take from the module artifacts so that we don't try to process them
1284        // again next time due to execution caching.
1285        new_commands.extend(self.global.root_module_artifacts.process_commands());
1286        // Note: These will get re-processed, but since we're just adding them
1287        // to a map, it's fine.
1288        new_exec_artifacts.extend(self.global.root_module_artifacts.artifacts.clone());
1289        let new_responses = engine.take_responses().await;
1290
1291        // Move the artifacts into ExecState global to simplify cache
1292        // management.
1293        for (id, exec_artifact) in new_exec_artifacts {
1294            // Only insert if it wasn't already present. We don't want to
1295            // overwrite what was previously there. We haven't filled in node
1296            // paths yet.
1297            self.global.artifacts.artifacts.entry(id).or_insert(exec_artifact);
1298        }
1299
1300        let initial_graph = self.global.artifacts.graph.clone();
1301
1302        // Build the artifact graph.
1303        let programs = self.build_program_lookup(program.clone());
1304        let graph_result = crate::execution::artifact::build_artifact_graph(
1305            &new_commands,
1306            &new_responses,
1307            program,
1308            &mut self.global.artifacts.artifacts,
1309            initial_graph,
1310            &programs,
1311            &self.global.module_infos,
1312        );
1313
1314        #[cfg(feature = "snapshot-engine-responses")]
1315        {
1316            // Store engine responses for debugging.
1317            self.global.root_module_artifacts.responses.extend(new_responses);
1318        }
1319
1320        let artifact_graph = graph_result?;
1321        self.global.artifacts.graph = artifact_graph;
1322
1323        Ok(())
1324    }
1325
1326    pub(crate) fn kcl_version(&self) -> KclVersion {
1327        self.mod_local.settings.kcl_version
1328    }
1329}
1330
1331#[derive(Debug, Clone, Copy, Default, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS, Ord, PartialOrd)]
1332#[ts(export)]
1333pub enum KclVersion {
1334    #[default]
1335    #[serde(rename = "1.0")]
1336    V1,
1337    #[serde(rename = "2.0")]
1338    V2,
1339    #[serde(rename = "3.0-preview")]
1340    V3Preview,
1341}
1342
1343impl KclVersion {
1344    pub fn as_str(self) -> &'static str {
1345        match self {
1346            Self::V1 => "1.0",
1347            Self::V2 => "2.0",
1348            Self::V3Preview => "3.0-preview",
1349        }
1350    }
1351}
1352
1353impl FromStr for KclVersion {
1354    type Err = KclError;
1355
1356    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
1357        match s {
1358            "1" | "1.0" | "1.0.0" => Ok(Self::V1),
1359            "2" | "2.0" | "2.0.0" => Ok(Self::V2),
1360            "3-preview" | "3.0-preview" | "3.0.0-preview" => Ok(Self::V3Preview),
1361            other => Err(KclError::new_semantic(KclErrorDetails {
1362                source_ranges: Default::default(),
1363                backtrace: Default::default(),
1364                message: format!(
1365                    "Unrecognized version {other}. Valid versions are 1.0, 2.0 and (experimentally) 3.0-preview"
1366                ),
1367            })),
1368        }
1369    }
1370}
1371
1372impl GlobalState {
1373    fn new(settings: &ExecutorSettings, segment_ids_edited: AhashIndexSet<ObjectId>) -> Self {
1374        let mut global = GlobalState {
1375            machine_depth_high_water: 0,
1376            path_to_source_id: Default::default(),
1377            module_infos: Default::default(),
1378            artifacts: Default::default(),
1379            root_module_artifacts: Default::default(),
1380            mod_loader: Default::default(),
1381            issues: Default::default(),
1382            deprecation_version_override: None,
1383            id_to_source: Default::default(),
1384            segment_ids_edited,
1385            drag_anchors: Vec::new(),
1386            sketch_mode: false,
1387        };
1388
1389        let root_id = ModuleId::default();
1390        let root_path = settings.current_file.clone().unwrap_or_default();
1391        global.module_infos.insert(
1392            root_id,
1393            ModuleInfo {
1394                id: root_id,
1395                path: ModulePath::Local {
1396                    value: root_path.clone(),
1397                    original_import_path: None,
1398                },
1399                repr: ModuleRepr::Root,
1400            },
1401        );
1402        global.path_to_source_id.insert(
1403            ModulePath::Local {
1404                value: root_path,
1405                original_import_path: None,
1406            },
1407            root_id,
1408        );
1409        global
1410    }
1411
1412    pub(super) fn filenames(&self) -> IndexMap<ModuleId, ModulePath> {
1413        self.path_to_source_id.iter().map(|(k, v)| ((*v), k.clone())).collect()
1414    }
1415
1416    pub(super) fn get_source(&self, id: ModuleId) -> Option<&ModuleSource> {
1417        self.id_to_source.get(&id)
1418    }
1419}
1420
1421impl ArtifactState {
1422    pub fn cached_body_items(&self) -> usize {
1423        self.graph.item_count()
1424    }
1425
1426    pub(crate) fn clear(&mut self) {
1427        self.artifacts.clear();
1428        self.graph.clear();
1429    }
1430}
1431
1432impl ModuleArtifactState {
1433    pub fn legacy_angle_refactor_metadata(&self) -> Vec<LegacyAngleRefactorMeta> {
1434        self.refactor_metadata
1435            .iter()
1436            .filter_map(|metadata| match metadata {
1437                RefactorMetadata::LegacyAngle(metadata) => Some(*metadata),
1438                RefactorMetadata::EdgeRefactor(_) | RefactorMetadata::DirectTagFillet(_) => None,
1439            })
1440            .collect()
1441    }
1442
1443    pub(crate) fn clear(&mut self) {
1444        self.artifacts.clear();
1445        self.unprocessed_commands.clear();
1446        self.commands.clear();
1447        self.operations.clear();
1448        self.refactor_metadata.clear();
1449    }
1450
1451    pub(crate) fn restore_scene_objects(&mut self, scene_objects: &[Object]) {
1452        self.scene_objects = scene_objects.to_vec();
1453        self.object_id_generator = IncIdGenerator::new(self.scene_objects.len());
1454        self.source_range_to_object.clear();
1455        self.artifact_id_to_scene_object.clear();
1456
1457        for (expected_id, object) in self.scene_objects.iter().enumerate() {
1458            debug_assert_eq!(
1459                object.id.0, expected_id,
1460                "Restored cached scene object ID {} does not match its position {}",
1461                object.id.0, expected_id
1462            );
1463
1464            match &object.kind {
1465                ObjectKind::Wall(wall) => {
1466                    self.source_range_to_object.insert(wall.source.solid.range, object.id);
1467                }
1468                ObjectKind::Cap(cap) => {
1469                    self.source_range_to_object.insert(cap.source.solid.range, object.id);
1470                }
1471                _ => match &object.source {
1472                    crate::front::SourceRef::Simple { range, node_path: _ } => {
1473                        self.source_range_to_object.insert(*range, object.id);
1474                    }
1475                    crate::front::SourceRef::BackTrace { ranges } => {
1476                        // Don't map the entire backtrace, only the most specific
1477                        // range.
1478                        if let Some((range, _)) = ranges.first() {
1479                            self.source_range_to_object.insert(*range, object.id);
1480                        }
1481                    }
1482                },
1483            }
1484
1485            // Ignore placeholder artifacts.
1486            if object.artifact_id != ArtifactId::placeholder() {
1487                self.artifact_id_to_scene_object.insert(object.artifact_id, object.id);
1488            }
1489        }
1490    }
1491
1492    /// When self is a cached state, extend it with new state.
1493    pub(crate) fn extend(&mut self, other: ModuleArtifactState) {
1494        self.artifacts.extend(other.artifacts);
1495        self.unprocessed_commands.extend(other.unprocessed_commands);
1496        self.commands.extend(other.commands);
1497        self.operations.extend(other.operations);
1498        if other.scene_objects.len() > self.scene_objects.len() {
1499            self.scene_objects
1500                .extend(other.scene_objects[self.scene_objects.len()..].iter().cloned());
1501        }
1502        self.source_range_to_object.extend(other.source_range_to_object);
1503        self.artifact_id_to_scene_object
1504            .extend(other.artifact_id_to_scene_object);
1505        self.var_solutions.extend(other.var_solutions);
1506        self.refactor_metadata.extend(other.refactor_metadata);
1507    }
1508
1509    // Move unprocessed artifact commands so that we don't try to process them
1510    // again next time due to execution caching.  Returns a clone of the
1511    // commands that were moved.
1512    pub(crate) fn process_commands(&mut self) -> Vec<ArtifactCommand> {
1513        let unprocessed = std::mem::take(&mut self.unprocessed_commands);
1514        let new_module_commands = unprocessed.clone();
1515        self.commands.extend(unprocessed);
1516        new_module_commands
1517    }
1518
1519    pub(crate) fn scene_object_by_id(&self, id: ObjectId) -> Option<&Object> {
1520        debug_assert!(
1521            id.0 < self.scene_objects.len(),
1522            "Requested object ID {} but only have {} objects",
1523            id.0,
1524            self.scene_objects.len()
1525        );
1526        self.scene_objects.get(id.0)
1527    }
1528
1529    pub(crate) fn scene_object_by_id_mut(&mut self, id: ObjectId) -> Option<&mut Object> {
1530        debug_assert!(
1531            id.0 < self.scene_objects.len(),
1532            "Requested object ID {} but only have {} objects",
1533            id.0,
1534            self.scene_objects.len()
1535        );
1536        self.scene_objects.get_mut(id.0)
1537    }
1538}
1539
1540impl ModuleState {
1541    pub(super) fn new(
1542        path: ModulePath,
1543        memory: Arc<ProgramMemory>,
1544        module_id: Option<ModuleId>,
1545        sketch_mode: bool,
1546        freedom_analysis: bool,
1547    ) -> Self {
1548        let state_module_id = module_id.unwrap_or_default();
1549        ModuleState {
1550            module_id: state_module_id,
1551            id_generator: IdGenerator::new(module_id),
1552            stack: memory.new_stack(),
1553            call_stack_size: 0,
1554            machine_call_depth: 0,
1555            pipe_value: Default::default(),
1556            being_declared: Default::default(),
1557            sketch_block: Default::default(),
1558            stdlib_entry_source_range: Default::default(),
1559            module_exports: Default::default(),
1560            explicit_length_units: false,
1561            path,
1562            settings: Default::default(),
1563            sketch_mode,
1564            freedom_analysis,
1565            artifacts: Default::default(),
1566            constraint_state: Default::default(),
1567            allowed_warnings: Vec::new(),
1568            denied_warnings: Vec::new(),
1569            consumed_solids: AHashMap::default(),
1570            consumed_solid_ids: AHashMap::default(),
1571            consumed_regions: AHashMap::default(),
1572            inside_stdlib: false,
1573        }
1574    }
1575
1576    pub(super) fn variables(&self, main_ref: EnvironmentRef) -> Result<IndexMap<String, KclValue>, KclError> {
1577        self.stack.find_all_in_env_owned(main_ref)
1578    }
1579}
1580
1581impl SketchBlockState {
1582    pub(crate) fn next_sketch_var_id(&self) -> SketchVarId {
1583        SketchVarId(self.sketch_vars.len())
1584    }
1585
1586    /// Given a solve outcome, return the solutions for the sketch variables and
1587    /// enough information to update them in the source.
1588    pub(crate) fn var_solutions(
1589        &self,
1590        solve_outcome: &Solved,
1591        solution_ty: NumericType,
1592        sketch_block_range: SourceRange,
1593    ) -> Result<Vec<(SourceRange, Option<NodePath>, Number)>, KclError> {
1594        self.sketch_vars
1595            .iter()
1596            .map(|v| {
1597                let Some(sketch_var) = v.as_sketch_var() else {
1598                    return Err(KclError::new_internal(KclErrorDetails::new(
1599                        "Expected sketch variable".to_owned(),
1600                        vec![sketch_block_range],
1601                    )));
1602                };
1603                let var_index = sketch_var.id.0;
1604                let solved_n = solve_outcome.final_values.get(var_index).ok_or_else(|| {
1605                    let message = format!("No solution for sketch variable with id {}", var_index);
1606                    debug_assert!(false, "{}", &message);
1607                    KclError::new_internal(KclErrorDetails::new(
1608                        message,
1609                        sketch_var.meta.iter().map(|m| m.source_range).collect(),
1610                    ))
1611                })?;
1612                let solved_value = Number {
1613                    value: *solved_n,
1614                    units: solution_ty.try_into().map_err(|_| {
1615                        KclError::new_internal(KclErrorDetails::new(
1616                            "Failed to convert numeric type to units".to_owned(),
1617                            vec![sketch_block_range],
1618                        ))
1619                    })?,
1620                };
1621                let Some(source_range) = sketch_var.meta.first().map(|m| m.source_range) else {
1622                    return Ok(None);
1623                };
1624                Ok(Some((source_range, sketch_var.node_path.clone(), solved_value)))
1625            })
1626            .filter_map(Result::transpose)
1627            .collect::<Result<Vec<_>, KclError>>()
1628    }
1629}
1630
1631#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS)]
1632#[ts(export)]
1633#[serde(rename_all = "camelCase")]
1634pub struct MetaSettings {
1635    pub default_length_units: UnitLength,
1636    pub default_angle_units: UnitAngle,
1637    pub experimental_features: annotations::WarningLevel,
1638    pub kcl_version: KclVersion,
1639}
1640
1641impl Default for MetaSettings {
1642    fn default() -> Self {
1643        MetaSettings {
1644            default_length_units: UnitLength::Millimeters,
1645            default_angle_units: UnitAngle::Degrees,
1646            experimental_features: annotations::WarningLevel::Deny,
1647            kcl_version: KclVersion::default(),
1648        }
1649    }
1650}
1651
1652impl MetaSettings {
1653    pub(crate) fn update_from_annotation(
1654        &mut self,
1655        annotation: &crate::parsing::ast::types::Node<Annotation>,
1656    ) -> Result<(bool, bool), KclError> {
1657        let properties = annotations::expect_properties(annotations::SETTINGS, annotation)?;
1658
1659        let mut updated_len = false;
1660        let mut updated_angle = false;
1661        for p in properties {
1662            match &*p.inner.key.name {
1663                annotations::SETTINGS_UNIT_LENGTH => {
1664                    let value = annotations::expect_ident(&p.inner.value)?;
1665                    let value = super::types::length_from_str(value, annotation.as_source_range())?;
1666                    self.default_length_units = value;
1667                    updated_len = true;
1668                }
1669                annotations::SETTINGS_UNIT_ANGLE => {
1670                    let value = annotations::expect_ident(&p.inner.value)?;
1671                    let value = super::types::angle_from_str(value, annotation.as_source_range())?;
1672                    self.default_angle_units = value;
1673                    updated_angle = true;
1674                }
1675                annotations::SETTINGS_VERSION => {
1676                    let value = annotations::expect_kcl_version(&p.inner.value)?;
1677                    self.kcl_version = value.parse()?;
1678                }
1679                annotations::SETTINGS_EXPERIMENTAL_FEATURES => {
1680                    let value = annotations::expect_ident(&p.inner.value)?;
1681                    let value = annotations::WarningLevel::from_str(value).map_err(|_| {
1682                        KclError::new_semantic(KclErrorDetails::new(
1683                            format!(
1684                                "Invalid value for {} settings property, expected one of: {}",
1685                                annotations::SETTINGS_EXPERIMENTAL_FEATURES,
1686                                annotations::WARN_LEVELS.join(", ")
1687                            ),
1688                            annotation.as_source_ranges(),
1689                        ))
1690                    })?;
1691                    self.experimental_features = value;
1692                }
1693                name => {
1694                    return Err(KclError::new_semantic(KclErrorDetails::new(
1695                        format!(
1696                            "Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
1697                            annotations::SETTINGS_UNIT_LENGTH,
1698                            annotations::SETTINGS_UNIT_ANGLE
1699                        ),
1700                        vec![annotation.as_source_range()],
1701                    )));
1702                }
1703            }
1704        }
1705
1706        Ok((updated_len, updated_angle))
1707    }
1708}
1709
1710#[cfg(test)]
1711mod tests {
1712    use std::str::FromStr;
1713
1714    use uuid::Uuid;
1715
1716    use super::KclVersion;
1717    use super::ModuleArtifactState;
1718    use crate::NodePath;
1719    use crate::NodePathExt;
1720    use crate::SourceRange;
1721    use crate::execution::ArtifactId;
1722    use crate::front::Object;
1723    use crate::front::ObjectId;
1724    use crate::front::ObjectKind;
1725    use crate::front::Plane;
1726    use crate::front::SourceRef;
1727
1728    #[test]
1729    fn kcl_version_parses_supported_spellings() {
1730        assert_eq!(KclVersion::from_str("1"), Ok(KclVersion::V1));
1731        assert_eq!(KclVersion::from_str("1.0.0"), Ok(KclVersion::V1));
1732        assert_eq!(KclVersion::from_str("2"), Ok(KclVersion::V2));
1733        assert_eq!(KclVersion::from_str("2.0.0"), Ok(KclVersion::V2));
1734        assert_eq!(KclVersion::from_str("3.0-preview"), Ok(KclVersion::V3Preview));
1735        // No such version.
1736        KclVersion::from_str("99.123").unwrap_err();
1737    }
1738
1739    #[test]
1740    fn kcl_version_serializes_as_canonical_setting_value() {
1741        assert_eq!(serde_json::to_string(&KclVersion::V1).unwrap(), r#""1.0""#);
1742        assert_eq!(serde_json::to_string(&KclVersion::V2).unwrap(), r#""2.0""#);
1743        assert_eq!(
1744            serde_json::to_string(&KclVersion::V3Preview).unwrap(),
1745            r#""3.0-preview""#
1746        );
1747    }
1748
1749    #[test]
1750    fn restore_scene_objects_rebuilds_lookup_maps() {
1751        let plane_artifact_id = ArtifactId::new(Uuid::from_u128(1));
1752        let sketch_artifact_id = ArtifactId::new(Uuid::from_u128(2));
1753        let plane_range = SourceRange::from([1, 4, 0]);
1754        let plane_node_path = Some(NodePath::placeholder());
1755        let sketch_ranges = vec![
1756            (SourceRange::from([5, 9, 0]), None),
1757            (SourceRange::from([10, 12, 0]), None),
1758        ];
1759        let cached_objects = vec![
1760            Object {
1761                id: ObjectId(0),
1762                kind: ObjectKind::Plane(Plane::Object(ObjectId(0))),
1763                label: Default::default(),
1764                comments: Default::default(),
1765                artifact_id: plane_artifact_id,
1766                source: SourceRef::new(plane_range, plane_node_path),
1767            },
1768            Object {
1769                id: ObjectId(1),
1770                kind: ObjectKind::Nil,
1771                label: Default::default(),
1772                comments: Default::default(),
1773                artifact_id: sketch_artifact_id,
1774                source: SourceRef::BackTrace {
1775                    ranges: sketch_ranges.clone(),
1776                },
1777            },
1778            Object::placeholder(ObjectId(2), SourceRange::from([13, 14, 0]), None),
1779        ];
1780
1781        let mut artifacts = ModuleArtifactState::default();
1782        artifacts.restore_scene_objects(&cached_objects);
1783
1784        assert_eq!(artifacts.scene_objects, cached_objects);
1785        assert_eq!(
1786            artifacts.artifact_id_to_scene_object.get(&plane_artifact_id),
1787            Some(&ObjectId(0))
1788        );
1789        assert_eq!(
1790            artifacts.artifact_id_to_scene_object.get(&sketch_artifact_id),
1791            Some(&ObjectId(1))
1792        );
1793        assert_eq!(
1794            artifacts.artifact_id_to_scene_object.get(&ArtifactId::placeholder()),
1795            None
1796        );
1797        assert_eq!(artifacts.source_range_to_object.get(&plane_range), Some(&ObjectId(0)));
1798        assert_eq!(
1799            artifacts.source_range_to_object.get(&sketch_ranges[0].0),
1800            Some(&ObjectId(1))
1801        );
1802        // We don't map all the ranges in a backtrace.
1803        assert_eq!(artifacts.source_range_to_object.get(&sketch_ranges[1].0), None);
1804    }
1805}