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