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