Skip to main content

kcl_lib/execution/
state.rs

1#[cfg(feature = "artifact-graph")]
2use std::collections::BTreeMap;
3use std::str::FromStr;
4use std::sync::Arc;
5
6use anyhow::Result;
7use indexmap::IndexMap;
8use kittycad_modeling_cmds::units::UnitAngle;
9use kittycad_modeling_cmds::units::UnitLength;
10use serde::Deserialize;
11use serde::Serialize;
12use uuid::Uuid;
13
14use crate::CompilationIssue;
15use crate::EngineManager;
16use crate::ExecutorContext;
17use crate::KclErrorWithOutputs;
18use crate::MockConfig;
19#[cfg(feature = "artifact-graph")]
20use crate::NodePath;
21use crate::SourceRange;
22use crate::collections::AhashIndexSet;
23use crate::errors::KclError;
24use crate::errors::KclErrorDetails;
25use crate::errors::Severity;
26use crate::exec::DefaultPlanes;
27#[cfg(feature = "artifact-graph")]
28use crate::execution::Artifact;
29#[cfg(feature = "artifact-graph")]
30use crate::execution::ArtifactCommand;
31#[cfg(feature = "artifact-graph")]
32use crate::execution::ArtifactGraph;
33#[cfg(feature = "artifact-graph")]
34use crate::execution::ArtifactId;
35use crate::execution::EnvironmentRef;
36use crate::execution::ExecOutcome;
37use crate::execution::ExecutorSettings;
38use crate::execution::KclValue;
39#[cfg(feature = "artifact-graph")]
40use crate::execution::ProgramLookup;
41use crate::execution::SketchVarId;
42use crate::execution::UnsolvedSegment;
43use crate::execution::annotations;
44use crate::execution::cad_op::Operation;
45use crate::execution::id_generator::IdGenerator;
46use crate::execution::memory::ProgramMemory;
47use crate::execution::memory::Stack;
48#[cfg(feature = "artifact-graph")]
49use crate::execution::sketch_solve::Solved;
50use crate::execution::types::NumericType;
51#[cfg(feature = "artifact-graph")]
52use crate::front::Number;
53use crate::front::Object;
54use crate::front::ObjectId;
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) global: GlobalState,
70    pub(super) mod_local: ModuleState,
71}
72
73pub type ModuleInfoMap = IndexMap<ModuleId, ModuleInfo>;
74
75#[derive(Debug, Clone)]
76pub(super) struct GlobalState {
77    /// Map from source file absolute path to module ID.
78    pub path_to_source_id: IndexMap<ModulePath, ModuleId>,
79    /// Map from module ID to source file.
80    pub id_to_source: IndexMap<ModuleId, ModuleSource>,
81    /// Map from module ID to module info.
82    pub module_infos: ModuleInfoMap,
83    /// Module loader.
84    pub mod_loader: ModuleLoader,
85    /// Errors and warnings.
86    pub issues: Vec<CompilationIssue>,
87    /// Global artifacts that represent the entire program.
88    pub artifacts: ArtifactState,
89    /// Artifacts for only the root module.
90    pub root_module_artifacts: ModuleArtifactState,
91    /// The segments that were edited that triggered this execution.
92    #[cfg(feature = "artifact-graph")]
93    pub segment_ids_edited: AhashIndexSet<ObjectId>,
94}
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
97pub(crate) enum ConstraintKey {
98    LineCircle([usize; 10]),
99    CircleCircle([usize; 12]),
100}
101
102#[derive(Debug, Clone, Copy, PartialEq, Eq)]
103pub(crate) enum TangencyMode {
104    LineCircle(ezpz::LineSide),
105    CircleCircle(ezpz::CircleSide),
106}
107
108#[derive(Debug, Clone, Copy, PartialEq, Eq)]
109pub(crate) enum ConstraintState {
110    Tangency(TangencyMode),
111}
112
113#[cfg(feature = "artifact-graph")]
114#[derive(Debug, Clone, Default)]
115pub(super) struct ArtifactState {
116    /// Internal map of UUIDs to exec artifacts.  This needs to persist across
117    /// executions to allow the graph building to refer to cached artifacts.
118    pub artifacts: IndexMap<ArtifactId, Artifact>,
119    /// Output artifact graph.
120    pub graph: ArtifactGraph,
121}
122
123#[cfg(not(feature = "artifact-graph"))]
124#[derive(Debug, Clone, Default)]
125pub(super) struct ArtifactState {}
126
127/// Artifact state for a single module.
128#[cfg(feature = "artifact-graph")]
129#[derive(Debug, Clone, Default, Serialize)]
130#[cfg_attr(not(feature = "snapshot-engine-responses"), derive(PartialEq))]
131pub struct ModuleArtifactState {
132    /// Internal map of UUIDs to exec artifacts.
133    pub artifacts: IndexMap<ArtifactId, Artifact>,
134    /// Outgoing engine commands that have not yet been processed and integrated
135    /// into the artifact graph.
136    #[serde(skip)]
137    pub unprocessed_commands: Vec<ArtifactCommand>,
138    /// Outgoing engine commands.
139    pub commands: Vec<ArtifactCommand>,
140    /// Incoming engine commands.
141    #[cfg(feature = "snapshot-engine-responses")]
142    pub responses: IndexMap<Uuid, kittycad_modeling_cmds::websocket::WebSocketResponse>,
143    /// Operations that have been performed in execution order, for display in
144    /// the Feature Tree.
145    pub operations: Vec<Operation>,
146    /// [`ObjectId`] generator.
147    pub object_id_generator: IncIdGenerator<usize>,
148    /// Objects in the scene, created from execution.
149    pub scene_objects: Vec<Object>,
150    /// Map from source range to object ID for lookup of objects by their source
151    /// range.
152    pub source_range_to_object: BTreeMap<SourceRange, ObjectId>,
153    /// Map from artifact ID to object ID in the scene.
154    pub artifact_id_to_scene_object: IndexMap<ArtifactId, ObjectId>,
155    /// Solutions for sketch variables.
156    pub var_solutions: Vec<(SourceRange, Number)>,
157}
158
159// It's error-prone to implement this manually. New fields are likely to be
160// forgotten. So we only use this implementation when the feature is enabled.
161#[cfg(feature = "snapshot-engine-responses")]
162impl PartialEq for ModuleArtifactState {
163    fn eq(&self, other: &Self) -> bool {
164        self.artifacts == other.artifacts
165            && self.unprocessed_commands == other.unprocessed_commands
166            && self.commands == other.commands
167            // WebSocketResponse type doesn't implement `PartialEq`.
168            // && self.responses == other.responses
169            && self.operations == other.operations
170            && self.object_id_generator == other.object_id_generator
171            && self.scene_objects == other.scene_objects
172            && self.source_range_to_object == other.source_range_to_object
173            && self.artifact_id_to_scene_object == other.artifact_id_to_scene_object
174            && self.var_solutions == other.var_solutions
175    }
176}
177
178#[cfg(not(feature = "artifact-graph"))]
179#[derive(Debug, Clone, Default, PartialEq, Serialize)]
180pub struct ModuleArtifactState {
181    /// [`ObjectId`] generator.
182    pub object_id_generator: IncIdGenerator<usize>,
183}
184
185#[derive(Debug, Clone)]
186pub(super) struct ModuleState {
187    /// The id generator for this module.
188    pub id_generator: IdGenerator,
189    pub stack: Stack,
190    /// The size of the call stack. This is used to prevent stack overflows with
191    /// recursive function calls. In general, this doesn't match `stack`'s size
192    /// since it's conservative in reclaiming frames between executions.
193    pub(super) call_stack_size: usize,
194    /// The current value of the pipe operator returned from the previous
195    /// expression.  If we're not currently in a pipeline, this will be None.
196    pub pipe_value: Option<KclValue>,
197    /// The closest variable declaration being executed in any parent node in the AST.
198    /// This is used to provide better error messages, e.g. noticing when the user is trying
199    /// to use the variable `length` inside the RHS of its own definition, like `length = tan(length)`.
200    pub being_declared: Option<String>,
201    /// Present if we're currently executing inside a sketch block.
202    pub sketch_block: Option<SketchBlockState>,
203    /// Tracks if KCL being executed is currently inside a stdlib function or not.
204    /// This matters because e.g. we shouldn't emit artifacts from declarations declared inside a stdlib function.
205    pub inside_stdlib: bool,
206    /// The source range where we entered the standard library.
207    pub stdlib_entry_source_range: Option<SourceRange>,
208    /// Identifiers that have been exported from the current module.
209    pub module_exports: Vec<String>,
210    /// Settings specified from annotations.
211    pub settings: MetaSettings,
212    /// True if executing in sketch mode. Only a single sketch block will be
213    /// executed. All other code is ignored.
214    pub sketch_mode: bool,
215    /// True to do more costly analysis of whether the sketch block segments are
216    /// under-constrained. The only time we disable this is when a user is
217    /// dragging segments.
218    pub freedom_analysis: bool,
219    pub(super) explicit_length_units: bool,
220    pub(super) path: ModulePath,
221    /// Artifacts for only this module.
222    pub artifacts: ModuleArtifactState,
223    /// Sticky per-constraint state persisted across sketch-mode mock solves.
224    /// Maps from sketch block ID to a map for that sketch.
225    /// Then the inner map is per constraint (in that sketch block) to its state.
226    pub constraint_state: IndexMap<ObjectId, IndexMap<ConstraintKey, ConstraintState>>,
227
228    pub(super) allowed_warnings: Vec<&'static str>,
229    pub(super) denied_warnings: Vec<&'static str>,
230}
231
232#[derive(Debug, Clone, Default)]
233pub(crate) struct SketchBlockState {
234    pub sketch_vars: Vec<KclValue>,
235    pub sketch_id: Option<ObjectId>,
236    #[cfg(feature = "artifact-graph")]
237    pub sketch_constraints: Vec<ObjectId>,
238    pub solver_constraints: Vec<ezpz::Constraint>,
239    pub solver_optional_constraints: Vec<ezpz::Constraint>,
240    pub needed_by_engine: Vec<UnsolvedSegment>,
241    pub segment_tags: IndexMap<ObjectId, TagNode>,
242}
243
244impl ExecState {
245    pub fn new(exec_context: &super::ExecutorContext) -> Self {
246        ExecState {
247            global: GlobalState::new(&exec_context.settings, Default::default()),
248            mod_local: ModuleState::new(ModulePath::Main, ProgramMemory::new(), Default::default(), false, true),
249        }
250    }
251
252    pub fn new_mock(exec_context: &super::ExecutorContext, mock_config: &MockConfig) -> Self {
253        #[cfg(feature = "artifact-graph")]
254        let segment_ids_edited = mock_config.segment_ids_edited.clone();
255        #[cfg(not(feature = "artifact-graph"))]
256        let segment_ids_edited = Default::default();
257        ExecState {
258            global: GlobalState::new(&exec_context.settings, segment_ids_edited),
259            mod_local: ModuleState::new(
260                ModulePath::Main,
261                ProgramMemory::new(),
262                Default::default(),
263                mock_config.sketch_block_id.is_some(),
264                mock_config.freedom_analysis,
265            ),
266        }
267    }
268
269    pub(super) fn reset(&mut self, exec_context: &super::ExecutorContext) {
270        let global = GlobalState::new(&exec_context.settings, Default::default());
271
272        *self = ExecState {
273            global,
274            mod_local: ModuleState::new(
275                self.mod_local.path.clone(),
276                ProgramMemory::new(),
277                Default::default(),
278                false,
279                true,
280            ),
281        };
282    }
283
284    /// Log a non-fatal error.
285    pub fn err(&mut self, e: CompilationIssue) {
286        self.global.issues.push(e);
287    }
288
289    /// Log a warning.
290    pub fn warn(&mut self, mut e: CompilationIssue, name: &'static str) {
291        debug_assert!(annotations::WARN_VALUES.contains(&name));
292
293        if self.mod_local.allowed_warnings.contains(&name) {
294            return;
295        }
296
297        if self.mod_local.denied_warnings.contains(&name) {
298            e.severity = Severity::Error;
299        } else {
300            e.severity = Severity::Warning;
301        }
302
303        self.global.issues.push(e);
304    }
305
306    pub fn warn_experimental(&mut self, feature_name: &str, source_range: SourceRange) {
307        let Some(severity) = self.mod_local.settings.experimental_features.severity() else {
308            return;
309        };
310        let error = CompilationIssue {
311            source_range,
312            message: format!("Use of {feature_name} is experimental and may change or be removed."),
313            suggestion: None,
314            severity,
315            tag: crate::errors::Tag::None,
316        };
317
318        self.global.issues.push(error);
319    }
320
321    pub fn clear_units_warnings(&mut self, source_range: &SourceRange) {
322        self.global.issues = std::mem::take(&mut self.global.issues)
323            .into_iter()
324            .filter(|e| {
325                e.severity != Severity::Warning
326                    || !source_range.contains_range(&e.source_range)
327                    || e.tag != crate::errors::Tag::UnknownNumericUnits
328            })
329            .collect();
330    }
331
332    pub fn issues(&self) -> &[CompilationIssue] {
333        &self.global.issues
334    }
335
336    /// Convert to execution outcome when running in WebAssembly.  We want to
337    /// reduce the amount of data that crosses the WASM boundary as much as
338    /// possible.
339    pub async fn into_exec_outcome(self, main_ref: EnvironmentRef, ctx: &ExecutorContext) -> ExecOutcome {
340        // Fields are opt-in so that we don't accidentally leak private internal
341        // state when we add more to ExecState.
342        ExecOutcome {
343            variables: self.mod_local.variables(main_ref),
344            filenames: self.global.filenames(),
345            #[cfg(feature = "artifact-graph")]
346            operations: self.global.root_module_artifacts.operations,
347            #[cfg(feature = "artifact-graph")]
348            artifact_graph: self.global.artifacts.graph,
349            #[cfg(feature = "artifact-graph")]
350            scene_objects: self.global.root_module_artifacts.scene_objects,
351            #[cfg(feature = "artifact-graph")]
352            source_range_to_object: self.global.root_module_artifacts.source_range_to_object,
353            #[cfg(feature = "artifact-graph")]
354            var_solutions: self.global.root_module_artifacts.var_solutions,
355            issues: self.global.issues,
356            default_planes: ctx.engine.get_default_planes().read().await.clone(),
357        }
358    }
359
360    #[cfg(all(feature = "artifact-graph", feature = "snapshot-engine-responses"))]
361    pub(crate) fn take_root_module_responses(
362        &mut self,
363    ) -> IndexMap<Uuid, kittycad_modeling_cmds::websocket::WebSocketResponse> {
364        std::mem::take(&mut self.global.root_module_artifacts.responses)
365    }
366
367    pub(crate) fn stack(&self) -> &Stack {
368        &self.mod_local.stack
369    }
370
371    pub(crate) fn mut_stack(&mut self) -> &mut Stack {
372        &mut self.mod_local.stack
373    }
374
375    /// Increment the user-level call stack size, returning an error if it
376    /// exceeds the maximum.
377    pub(super) fn inc_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
378        // If you change this, make sure to test in WebAssembly in the app since
379        // that's the limiting factor.
380        if self.mod_local.call_stack_size >= 50 {
381            return Err(KclError::MaxCallStack {
382                details: KclErrorDetails::new("maximum call stack size exceeded".to_owned(), vec![range]),
383            });
384        }
385        self.mod_local.call_stack_size += 1;
386        Ok(())
387    }
388
389    /// Decrement the user-level call stack size, returning an error if it would
390    /// go below zero.
391    pub(super) fn dec_call_stack_size(&mut self, range: SourceRange) -> Result<(), KclError> {
392        // Prevent underflow.
393        if self.mod_local.call_stack_size == 0 {
394            let message = "call stack size below zero".to_owned();
395            debug_assert!(false, "{message}");
396            return Err(KclError::new_internal(KclErrorDetails::new(message, vec![range])));
397        }
398        self.mod_local.call_stack_size -= 1;
399        Ok(())
400    }
401
402    /// Returns true if we're executing in sketch mode for the current module.
403    /// In sketch mode, we still want to execute the prelude and other stdlib
404    /// modules as normal, so it can vary per module within a single overall
405    /// execution.
406    pub(crate) fn sketch_mode(&self) -> bool {
407        self.mod_local.sketch_mode
408            && match &self.mod_local.path {
409                ModulePath::Main => true,
410                ModulePath::Local { .. } => true,
411                ModulePath::Std { .. } => false,
412            }
413    }
414
415    pub fn next_object_id(&mut self) -> ObjectId {
416        ObjectId(self.mod_local.artifacts.object_id_generator.next_id())
417    }
418
419    pub fn peek_object_id(&self) -> ObjectId {
420        ObjectId(self.mod_local.artifacts.object_id_generator.peek_id())
421    }
422
423    pub(crate) fn constraint_state(&self, sketch_block_id: ObjectId, key: &ConstraintKey) -> Option<ConstraintState> {
424        let map = self.mod_local.constraint_state.get(&sketch_block_id)?;
425        map.get(key).copied()
426    }
427
428    pub(crate) fn set_constraint_state(
429        &mut self,
430        sketch_block_id: ObjectId,
431        key: ConstraintKey,
432        state: ConstraintState,
433    ) {
434        let map = self.mod_local.constraint_state.entry(sketch_block_id).or_default();
435        map.insert(key, state);
436    }
437
438    #[cfg(feature = "artifact-graph")]
439    pub fn add_scene_object(&mut self, obj: Object, source_range: SourceRange) -> ObjectId {
440        let id = obj.id;
441        debug_assert!(
442            id.0 == self.mod_local.artifacts.scene_objects.len(),
443            "Adding scene object with ID {} but next ID is {}",
444            id.0,
445            self.mod_local.artifacts.scene_objects.len()
446        );
447        let artifact_id = obj.artifact_id;
448        self.mod_local.artifacts.scene_objects.push(obj);
449        self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
450        self.mod_local
451            .artifacts
452            .artifact_id_to_scene_object
453            .insert(artifact_id, id);
454        id
455    }
456
457    /// Add a placeholder scene object. This is useful when we need to reserve
458    /// an ID before we have all the information to create the full object.
459    #[cfg(feature = "artifact-graph")]
460    pub fn add_placeholder_scene_object(
461        &mut self,
462        id: ObjectId,
463        source_range: SourceRange,
464        node_path: Option<NodePath>,
465    ) -> ObjectId {
466        debug_assert!(id.0 == self.mod_local.artifacts.scene_objects.len());
467        self.mod_local
468            .artifacts
469            .scene_objects
470            .push(Object::placeholder(id, source_range, node_path));
471        self.mod_local.artifacts.source_range_to_object.insert(source_range, id);
472        id
473    }
474
475    /// Update a scene object. This is useful to replace a placeholder.
476    #[cfg(feature = "artifact-graph")]
477    pub fn set_scene_object(&mut self, object: Object) {
478        let id = object.id;
479        let artifact_id = object.artifact_id;
480        self.mod_local.artifacts.scene_objects[id.0] = object;
481        self.mod_local
482            .artifacts
483            .artifact_id_to_scene_object
484            .insert(artifact_id, id);
485    }
486
487    #[cfg(feature = "artifact-graph")]
488    pub fn scene_object_id_by_artifact_id(&self, artifact_id: ArtifactId) -> Option<ObjectId> {
489        self.mod_local
490            .artifacts
491            .artifact_id_to_scene_object
492            .get(&artifact_id)
493            .cloned()
494    }
495
496    #[cfg(feature = "artifact-graph")]
497    pub fn segment_ids_edited_contains(&self, object_id: &ObjectId) -> bool {
498        self.global.segment_ids_edited.contains(object_id)
499    }
500
501    pub(super) fn is_in_sketch_block(&self) -> bool {
502        self.mod_local.sketch_block.is_some()
503    }
504
505    pub(crate) fn sketch_block_mut(&mut self) -> Option<&mut SketchBlockState> {
506        self.mod_local.sketch_block.as_mut()
507    }
508
509    pub(crate) fn sketch_block(&mut self) -> Option<&SketchBlockState> {
510        self.mod_local.sketch_block.as_ref()
511    }
512
513    pub fn next_uuid(&mut self) -> Uuid {
514        self.mod_local.id_generator.next_uuid()
515    }
516
517    #[cfg(feature = "artifact-graph")]
518    pub fn next_artifact_id(&mut self) -> ArtifactId {
519        self.mod_local.id_generator.next_artifact_id()
520    }
521
522    pub fn id_generator(&mut self) -> &mut IdGenerator {
523        &mut self.mod_local.id_generator
524    }
525
526    #[cfg(feature = "artifact-graph")]
527    pub(crate) fn add_artifact(&mut self, artifact: Artifact) {
528        let id = artifact.id();
529        self.mod_local.artifacts.artifacts.insert(id, artifact);
530    }
531
532    #[cfg(feature = "artifact-graph")]
533    pub(crate) fn artifact_mut(&mut self, id: ArtifactId) -> Option<&mut Artifact> {
534        self.mod_local.artifacts.artifacts.get_mut(&id)
535    }
536
537    pub(crate) fn push_op(&mut self, op: Operation) {
538        #[cfg(feature = "artifact-graph")]
539        self.mod_local.artifacts.operations.push(op);
540        #[cfg(not(feature = "artifact-graph"))]
541        drop(op);
542    }
543
544    #[cfg(feature = "artifact-graph")]
545    pub(crate) fn push_command(&mut self, command: ArtifactCommand) {
546        self.mod_local.artifacts.unprocessed_commands.push(command);
547        #[cfg(not(feature = "artifact-graph"))]
548        drop(command);
549    }
550
551    pub(super) fn next_module_id(&self) -> ModuleId {
552        ModuleId::from_usize(self.global.path_to_source_id.len())
553    }
554
555    pub(super) fn id_for_module(&self, path: &ModulePath) -> Option<ModuleId> {
556        self.global.path_to_source_id.get(path).cloned()
557    }
558
559    pub(super) fn add_path_to_source_id(&mut self, path: ModulePath, id: ModuleId) {
560        debug_assert!(!self.global.path_to_source_id.contains_key(&path));
561        self.global.path_to_source_id.insert(path, id);
562    }
563
564    pub(crate) fn add_root_module_contents(&mut self, program: &crate::Program) {
565        let root_id = ModuleId::default();
566        // Get the path for the root module.
567        let path = self
568            .global
569            .path_to_source_id
570            .iter()
571            .find(|(_, v)| **v == root_id)
572            .unwrap()
573            .0
574            .clone();
575        self.add_id_to_source(
576            root_id,
577            ModuleSource {
578                path,
579                source: program.original_file_contents.to_string(),
580            },
581        );
582    }
583
584    pub(super) fn add_id_to_source(&mut self, id: ModuleId, source: ModuleSource) {
585        self.global.id_to_source.insert(id, source);
586    }
587
588    pub(super) fn add_module(&mut self, id: ModuleId, path: ModulePath, repr: ModuleRepr) {
589        debug_assert!(self.global.path_to_source_id.contains_key(&path));
590        let module_info = ModuleInfo { id, repr, path };
591        self.global.module_infos.insert(id, module_info);
592    }
593
594    pub fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
595        self.global.module_infos.get(&id)
596    }
597
598    #[cfg(all(test, feature = "artifact-graph"))]
599    pub(crate) fn modules(&self) -> &ModuleInfoMap {
600        &self.global.module_infos
601    }
602
603    #[cfg(all(test, feature = "artifact-graph"))]
604    pub(crate) fn root_module_artifact_state(&self) -> &ModuleArtifactState {
605        &self.global.root_module_artifacts
606    }
607
608    pub fn current_default_units(&self) -> NumericType {
609        NumericType::Default {
610            len: self.length_unit(),
611            angle: self.angle_unit(),
612        }
613    }
614
615    pub fn length_unit(&self) -> UnitLength {
616        self.mod_local.settings.default_length_units
617    }
618
619    pub fn angle_unit(&self) -> UnitAngle {
620        self.mod_local.settings.default_angle_units
621    }
622
623    pub(super) fn circular_import_error(&self, path: &ModulePath, source_range: SourceRange) -> KclError {
624        KclError::new_import_cycle(KclErrorDetails::new(
625            format!(
626                "circular import of modules is not allowed: {} -> {}",
627                self.global
628                    .mod_loader
629                    .import_stack
630                    .iter()
631                    .map(|p| p.to_string_lossy())
632                    .collect::<Vec<_>>()
633                    .join(" -> "),
634                path,
635            ),
636            vec![source_range],
637        ))
638    }
639
640    pub(crate) fn pipe_value(&self) -> Option<&KclValue> {
641        self.mod_local.pipe_value.as_ref()
642    }
643
644    pub(crate) fn error_with_outputs(
645        &self,
646        error: KclError,
647        main_ref: Option<EnvironmentRef>,
648        default_planes: Option<DefaultPlanes>,
649    ) -> KclErrorWithOutputs {
650        let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = self
651            .global
652            .path_to_source_id
653            .iter()
654            .map(|(k, v)| ((*v), k.clone()))
655            .collect();
656
657        KclErrorWithOutputs::new(
658            error,
659            self.issues().to_vec(),
660            main_ref
661                .map(|main_ref| self.mod_local.variables(main_ref))
662                .unwrap_or_default(),
663            #[cfg(feature = "artifact-graph")]
664            self.global.root_module_artifacts.operations.clone(),
665            #[cfg(feature = "artifact-graph")]
666            Default::default(),
667            #[cfg(feature = "artifact-graph")]
668            self.global.artifacts.graph.clone(),
669            #[cfg(feature = "artifact-graph")]
670            self.global.root_module_artifacts.scene_objects.clone(),
671            #[cfg(feature = "artifact-graph")]
672            self.global.root_module_artifacts.source_range_to_object.clone(),
673            #[cfg(feature = "artifact-graph")]
674            self.global.root_module_artifacts.var_solutions.clone(),
675            module_id_to_module_path,
676            self.global.id_to_source.clone(),
677            default_planes,
678        )
679    }
680
681    #[cfg(feature = "artifact-graph")]
682    pub(crate) fn build_program_lookup(
683        &self,
684        current: crate::parsing::ast::types::Node<crate::parsing::ast::types::Program>,
685    ) -> ProgramLookup {
686        ProgramLookup::new(current, self.global.module_infos.clone())
687    }
688
689    #[cfg(feature = "artifact-graph")]
690    pub(crate) async fn build_artifact_graph(
691        &mut self,
692        engine: &Arc<Box<dyn EngineManager>>,
693        program: NodeRef<'_, crate::parsing::ast::types::Program>,
694    ) -> Result<(), KclError> {
695        let mut new_commands = Vec::new();
696        let mut new_exec_artifacts = IndexMap::new();
697        for module in self.global.module_infos.values_mut() {
698            match &mut module.repr {
699                ModuleRepr::Kcl(_, Some(outcome)) => {
700                    new_commands.extend(outcome.artifacts.process_commands());
701                    new_exec_artifacts.extend(outcome.artifacts.artifacts.clone());
702                }
703                ModuleRepr::Foreign(_, Some((_, module_artifacts))) => {
704                    new_commands.extend(module_artifacts.process_commands());
705                    new_exec_artifacts.extend(module_artifacts.artifacts.clone());
706                }
707                ModuleRepr::Root | ModuleRepr::Kcl(_, None) | ModuleRepr::Foreign(_, None) | ModuleRepr::Dummy => {}
708            }
709        }
710        // Take from the module artifacts so that we don't try to process them
711        // again next time due to execution caching.
712        new_commands.extend(self.global.root_module_artifacts.process_commands());
713        // Note: These will get re-processed, but since we're just adding them
714        // to a map, it's fine.
715        new_exec_artifacts.extend(self.global.root_module_artifacts.artifacts.clone());
716        let new_responses = engine.take_responses().await;
717
718        // Move the artifacts into ExecState global to simplify cache
719        // management.
720        for (id, exec_artifact) in new_exec_artifacts {
721            // Only insert if it wasn't already present. We don't want to
722            // overwrite what was previously there. We haven't filled in node
723            // paths yet.
724            self.global.artifacts.artifacts.entry(id).or_insert(exec_artifact);
725        }
726
727        let initial_graph = self.global.artifacts.graph.clone();
728
729        // Build the artifact graph.
730        let programs = self.build_program_lookup(program.clone());
731        let graph_result = crate::execution::artifact::build_artifact_graph(
732            &new_commands,
733            &new_responses,
734            program,
735            &mut self.global.artifacts.artifacts,
736            initial_graph,
737            &programs,
738            &self.global.module_infos,
739        );
740
741        #[cfg(feature = "snapshot-engine-responses")]
742        {
743            // Store engine responses for debugging.
744            self.global.root_module_artifacts.responses.extend(new_responses);
745        }
746
747        let artifact_graph = graph_result?;
748        self.global.artifacts.graph = artifact_graph;
749
750        Ok(())
751    }
752
753    #[cfg(not(feature = "artifact-graph"))]
754    pub(crate) async fn build_artifact_graph(
755        &mut self,
756        _engine: &Arc<Box<dyn EngineManager>>,
757        _program: NodeRef<'_, crate::parsing::ast::types::Program>,
758    ) -> Result<(), KclError> {
759        Ok(())
760    }
761}
762
763impl GlobalState {
764    fn new(settings: &ExecutorSettings, segment_ids_edited: AhashIndexSet<ObjectId>) -> Self {
765        #[cfg(not(feature = "artifact-graph"))]
766        drop(segment_ids_edited);
767        let mut global = GlobalState {
768            path_to_source_id: Default::default(),
769            module_infos: Default::default(),
770            artifacts: Default::default(),
771            root_module_artifacts: Default::default(),
772            mod_loader: Default::default(),
773            issues: Default::default(),
774            id_to_source: Default::default(),
775            #[cfg(feature = "artifact-graph")]
776            segment_ids_edited,
777        };
778
779        let root_id = ModuleId::default();
780        let root_path = settings.current_file.clone().unwrap_or_default();
781        global.module_infos.insert(
782            root_id,
783            ModuleInfo {
784                id: root_id,
785                path: ModulePath::Local {
786                    value: root_path.clone(),
787                    original_import_path: None,
788                },
789                repr: ModuleRepr::Root,
790            },
791        );
792        global.path_to_source_id.insert(
793            ModulePath::Local {
794                value: root_path,
795                original_import_path: None,
796            },
797            root_id,
798        );
799        global
800    }
801
802    pub(super) fn filenames(&self) -> IndexMap<ModuleId, ModulePath> {
803        self.path_to_source_id.iter().map(|(k, v)| ((*v), k.clone())).collect()
804    }
805
806    pub(super) fn get_source(&self, id: ModuleId) -> Option<&ModuleSource> {
807        self.id_to_source.get(&id)
808    }
809}
810
811impl ArtifactState {
812    #[cfg(feature = "artifact-graph")]
813    pub fn cached_body_items(&self) -> usize {
814        self.graph.item_count
815    }
816
817    pub(crate) fn clear(&mut self) {
818        #[cfg(feature = "artifact-graph")]
819        {
820            self.artifacts.clear();
821            self.graph.clear();
822        }
823    }
824}
825
826impl ModuleArtifactState {
827    pub(crate) fn clear(&mut self) {
828        #[cfg(feature = "artifact-graph")]
829        {
830            self.artifacts.clear();
831            self.unprocessed_commands.clear();
832            self.commands.clear();
833            self.operations.clear();
834        }
835    }
836
837    #[cfg(feature = "artifact-graph")]
838    pub(crate) fn restore_scene_objects(&mut self, scene_objects: &[Object]) {
839        self.scene_objects = scene_objects.to_vec();
840        self.object_id_generator = IncIdGenerator::new(self.scene_objects.len());
841        self.source_range_to_object.clear();
842        self.artifact_id_to_scene_object.clear();
843
844        for (expected_id, object) in self.scene_objects.iter().enumerate() {
845            debug_assert_eq!(
846                object.id.0, expected_id,
847                "Restored cached scene object ID {} does not match its position {}",
848                object.id.0, expected_id
849            );
850
851            match &object.source {
852                crate::front::SourceRef::Simple { range, node_path: _ } => {
853                    self.source_range_to_object.insert(*range, object.id);
854                }
855                crate::front::SourceRef::BackTrace { ranges } => {
856                    // Don't map the entire backtrace, only the most specific
857                    // range.
858                    if let Some((range, _)) = ranges.first() {
859                        self.source_range_to_object.insert(*range, object.id);
860                    }
861                }
862            }
863
864            // Ignore placeholder artifacts.
865            if object.artifact_id != ArtifactId::placeholder() {
866                self.artifact_id_to_scene_object.insert(object.artifact_id, object.id);
867            }
868        }
869    }
870
871    #[cfg(not(feature = "artifact-graph"))]
872    pub(crate) fn extend(&mut self, _other: ModuleArtifactState) {}
873
874    /// When self is a cached state, extend it with new state.
875    #[cfg(feature = "artifact-graph")]
876    pub(crate) fn extend(&mut self, other: ModuleArtifactState) {
877        self.artifacts.extend(other.artifacts);
878        self.unprocessed_commands.extend(other.unprocessed_commands);
879        self.commands.extend(other.commands);
880        self.operations.extend(other.operations);
881        if other.scene_objects.len() > self.scene_objects.len() {
882            self.scene_objects
883                .extend(other.scene_objects[self.scene_objects.len()..].iter().cloned());
884        }
885        self.source_range_to_object.extend(other.source_range_to_object);
886        self.artifact_id_to_scene_object
887            .extend(other.artifact_id_to_scene_object);
888        self.var_solutions.extend(other.var_solutions);
889    }
890
891    // Move unprocessed artifact commands so that we don't try to process them
892    // again next time due to execution caching.  Returns a clone of the
893    // commands that were moved.
894    #[cfg(feature = "artifact-graph")]
895    pub(crate) fn process_commands(&mut self) -> Vec<ArtifactCommand> {
896        let unprocessed = std::mem::take(&mut self.unprocessed_commands);
897        let new_module_commands = unprocessed.clone();
898        self.commands.extend(unprocessed);
899        new_module_commands
900    }
901
902    #[cfg_attr(not(feature = "artifact-graph"), expect(dead_code))]
903    pub(crate) fn scene_object_by_id(&self, id: ObjectId) -> Option<&Object> {
904        #[cfg(feature = "artifact-graph")]
905        {
906            debug_assert!(
907                id.0 < self.scene_objects.len(),
908                "Requested object ID {} but only have {} objects",
909                id.0,
910                self.scene_objects.len()
911            );
912            self.scene_objects.get(id.0)
913        }
914        #[cfg(not(feature = "artifact-graph"))]
915        {
916            let _ = id;
917            None
918        }
919    }
920
921    #[cfg_attr(not(feature = "artifact-graph"), expect(dead_code))]
922    pub(crate) fn scene_object_by_id_mut(&mut self, id: ObjectId) -> Option<&mut Object> {
923        #[cfg(feature = "artifact-graph")]
924        {
925            debug_assert!(
926                id.0 < self.scene_objects.len(),
927                "Requested object ID {} but only have {} objects",
928                id.0,
929                self.scene_objects.len()
930            );
931            self.scene_objects.get_mut(id.0)
932        }
933        #[cfg(not(feature = "artifact-graph"))]
934        {
935            let _ = id;
936            None
937        }
938    }
939}
940
941impl ModuleState {
942    pub(super) fn new(
943        path: ModulePath,
944        memory: Arc<ProgramMemory>,
945        module_id: Option<ModuleId>,
946        sketch_mode: bool,
947        freedom_analysis: bool,
948    ) -> Self {
949        ModuleState {
950            id_generator: IdGenerator::new(module_id),
951            stack: memory.new_stack(),
952            call_stack_size: 0,
953            pipe_value: Default::default(),
954            being_declared: Default::default(),
955            sketch_block: Default::default(),
956            stdlib_entry_source_range: Default::default(),
957            module_exports: Default::default(),
958            explicit_length_units: false,
959            path,
960            settings: Default::default(),
961            sketch_mode,
962            freedom_analysis,
963            artifacts: Default::default(),
964            constraint_state: Default::default(),
965            allowed_warnings: Vec::new(),
966            denied_warnings: Vec::new(),
967            inside_stdlib: false,
968        }
969    }
970
971    pub(super) fn variables(&self, main_ref: EnvironmentRef) -> IndexMap<String, KclValue> {
972        self.stack
973            .find_all_in_env(main_ref)
974            .map(|(k, v)| (k.clone(), v.clone()))
975            .collect()
976    }
977}
978
979impl SketchBlockState {
980    pub(crate) fn next_sketch_var_id(&self) -> SketchVarId {
981        SketchVarId(self.sketch_vars.len())
982    }
983
984    /// Given a solve outcome, return the solutions for the sketch variables and
985    /// enough information to update them in the source.
986    #[cfg(feature = "artifact-graph")]
987    pub(crate) fn var_solutions(
988        &self,
989        solve_outcome: &Solved,
990        solution_ty: NumericType,
991        range: SourceRange,
992    ) -> Result<Vec<(SourceRange, Number)>, KclError> {
993        self.sketch_vars
994            .iter()
995            .map(|v| {
996                let Some(sketch_var) = v.as_sketch_var() else {
997                    return Err(KclError::new_internal(KclErrorDetails::new(
998                        "Expected sketch variable".to_owned(),
999                        vec![range],
1000                    )));
1001                };
1002                let var_index = sketch_var.id.0;
1003                let solved_n = solve_outcome.final_values.get(var_index).ok_or_else(|| {
1004                    let message = format!("No solution for sketch variable with id {}", var_index);
1005                    debug_assert!(false, "{}", &message);
1006                    KclError::new_internal(KclErrorDetails::new(
1007                        message,
1008                        sketch_var.meta.iter().map(|m| m.source_range).collect(),
1009                    ))
1010                })?;
1011                let solved_value = Number {
1012                    value: *solved_n,
1013                    units: solution_ty.try_into().map_err(|_| {
1014                        KclError::new_internal(KclErrorDetails::new(
1015                            "Failed to convert numeric type to units".to_owned(),
1016                            vec![range],
1017                        ))
1018                    })?,
1019                };
1020                let Some(source_range) = sketch_var.meta.first().map(|m| m.source_range) else {
1021                    return Ok(None);
1022                };
1023                Ok(Some((source_range, solved_value)))
1024            })
1025            .filter_map(Result::transpose)
1026            .collect::<Result<Vec<_>, KclError>>()
1027    }
1028}
1029
1030#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS)]
1031#[ts(export)]
1032#[serde(rename_all = "camelCase")]
1033pub struct MetaSettings {
1034    pub default_length_units: UnitLength,
1035    pub default_angle_units: UnitAngle,
1036    pub experimental_features: annotations::WarningLevel,
1037    pub kcl_version: String,
1038}
1039
1040impl Default for MetaSettings {
1041    fn default() -> Self {
1042        MetaSettings {
1043            default_length_units: UnitLength::Millimeters,
1044            default_angle_units: UnitAngle::Degrees,
1045            experimental_features: annotations::WarningLevel::Deny,
1046            kcl_version: "1.0".to_owned(),
1047        }
1048    }
1049}
1050
1051impl MetaSettings {
1052    pub(crate) fn update_from_annotation(
1053        &mut self,
1054        annotation: &crate::parsing::ast::types::Node<Annotation>,
1055    ) -> Result<(bool, bool), KclError> {
1056        let properties = annotations::expect_properties(annotations::SETTINGS, annotation)?;
1057
1058        let mut updated_len = false;
1059        let mut updated_angle = false;
1060        for p in properties {
1061            match &*p.inner.key.name {
1062                annotations::SETTINGS_UNIT_LENGTH => {
1063                    let value = annotations::expect_ident(&p.inner.value)?;
1064                    let value = super::types::length_from_str(value, annotation.as_source_range())?;
1065                    self.default_length_units = value;
1066                    updated_len = true;
1067                }
1068                annotations::SETTINGS_UNIT_ANGLE => {
1069                    let value = annotations::expect_ident(&p.inner.value)?;
1070                    let value = super::types::angle_from_str(value, annotation.as_source_range())?;
1071                    self.default_angle_units = value;
1072                    updated_angle = true;
1073                }
1074                annotations::SETTINGS_VERSION => {
1075                    let value = annotations::expect_number(&p.inner.value)?;
1076                    self.kcl_version = value;
1077                }
1078                annotations::SETTINGS_EXPERIMENTAL_FEATURES => {
1079                    let value = annotations::expect_ident(&p.inner.value)?;
1080                    let value = annotations::WarningLevel::from_str(value).map_err(|_| {
1081                        KclError::new_semantic(KclErrorDetails::new(
1082                            format!(
1083                                "Invalid value for {} settings property, expected one of: {}",
1084                                annotations::SETTINGS_EXPERIMENTAL_FEATURES,
1085                                annotations::WARN_LEVELS.join(", ")
1086                            ),
1087                            annotation.as_source_ranges(),
1088                        ))
1089                    })?;
1090                    self.experimental_features = value;
1091                }
1092                name => {
1093                    return Err(KclError::new_semantic(KclErrorDetails::new(
1094                        format!(
1095                            "Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
1096                            annotations::SETTINGS_UNIT_LENGTH,
1097                            annotations::SETTINGS_UNIT_ANGLE
1098                        ),
1099                        vec![annotation.as_source_range()],
1100                    )));
1101                }
1102            }
1103        }
1104
1105        Ok((updated_len, updated_angle))
1106    }
1107}
1108
1109#[cfg(all(feature = "artifact-graph", test))]
1110mod tests {
1111    use uuid::Uuid;
1112
1113    use super::ModuleArtifactState;
1114    use crate::NodePath;
1115    use crate::SourceRange;
1116    use crate::execution::ArtifactId;
1117    use crate::front::Object;
1118    use crate::front::ObjectId;
1119    use crate::front::ObjectKind;
1120    use crate::front::Plane;
1121    use crate::front::SourceRef;
1122
1123    #[test]
1124    fn restore_scene_objects_rebuilds_lookup_maps() {
1125        let plane_artifact_id = ArtifactId::new(Uuid::from_u128(1));
1126        let sketch_artifact_id = ArtifactId::new(Uuid::from_u128(2));
1127        let plane_range = SourceRange::from([1, 4, 0]);
1128        let plane_node_path = Some(NodePath::placeholder());
1129        let sketch_ranges = vec![
1130            (SourceRange::from([5, 9, 0]), None),
1131            (SourceRange::from([10, 12, 0]), None),
1132        ];
1133        let cached_objects = vec![
1134            Object {
1135                id: ObjectId(0),
1136                kind: ObjectKind::Plane(Plane::Object(ObjectId(0))),
1137                label: Default::default(),
1138                comments: Default::default(),
1139                artifact_id: plane_artifact_id,
1140                source: SourceRef::new(plane_range, plane_node_path),
1141            },
1142            Object {
1143                id: ObjectId(1),
1144                kind: ObjectKind::Nil,
1145                label: Default::default(),
1146                comments: Default::default(),
1147                artifact_id: sketch_artifact_id,
1148                source: SourceRef::BackTrace {
1149                    ranges: sketch_ranges.clone(),
1150                },
1151            },
1152            Object::placeholder(ObjectId(2), SourceRange::from([13, 14, 0]), None),
1153        ];
1154
1155        let mut artifacts = ModuleArtifactState::default();
1156        artifacts.restore_scene_objects(&cached_objects);
1157
1158        assert_eq!(artifacts.scene_objects, cached_objects);
1159        assert_eq!(
1160            artifacts.artifact_id_to_scene_object.get(&plane_artifact_id),
1161            Some(&ObjectId(0))
1162        );
1163        assert_eq!(
1164            artifacts.artifact_id_to_scene_object.get(&sketch_artifact_id),
1165            Some(&ObjectId(1))
1166        );
1167        assert_eq!(
1168            artifacts.artifact_id_to_scene_object.get(&ArtifactId::placeholder()),
1169            None
1170        );
1171        assert_eq!(artifacts.source_range_to_object.get(&plane_range), Some(&ObjectId(0)));
1172        assert_eq!(
1173            artifacts.source_range_to_object.get(&sketch_ranges[0].0),
1174            Some(&ObjectId(1))
1175        );
1176        // We don't map all the ranges in a backtrace.
1177        assert_eq!(artifacts.source_range_to_object.get(&sketch_ranges[1].0), None);
1178    }
1179}