@@ -98,8 +98,7 @@ def discovery_classes_with_document(self, input_bucket: str, input_prefix: str):
9898 # No need to transform - it's already in the right format
9999 current_class = model_response
100100
101- # Merge the new class with existing Default + Custom classes
102- # and save to Custom config
101+ # Add the discovered class to the target version's config
103102 self ._merge_and_save_class (current_class )
104103
105104 return {"status" : "SUCCESS" }
@@ -151,8 +150,7 @@ def discovery_classes_with_document_and_ground_truth(
151150 # No need to transform - it's already in the right format
152151 current_class = model_response
153152
154- # Merge the new class with existing Default + Custom classes
155- # and save to Custom config
153+ # Add the discovered class to the target version's config
156154 self ._merge_and_save_class (current_class )
157155
158156 return {"status" : "SUCCESS" }
@@ -166,18 +164,17 @@ def discovery_classes_with_document_and_ground_truth(
166164
167165 def _merge_and_save_class (self , new_class : Dict [str , Any ]) -> None :
168166 """
169- Merge a new discovered class with existing Default + Custom classes and save to Custom .
167+ Merge a new discovered class into the target version's configuration .
170168
171- This method ensures that discovered classes are ADDITIVE to existing classes:
172- 1. Read Default classes (base classes from deployment)
173- 2. Read existing Custom classes (previous user customizations)
174- 3. Build a merged list starting from Default, overriding with Custom
175- 4. Add/update the new discovered class
176- 5. Save the complete merged list to Custom
169+ This method only adds/updates the discovered class within the target version's
170+ own class list. It does NOT pull in classes from the default configuration version,
171+ keeping the target version's classes exactly as the user curated them, plus the
172+ newly discovered class.
177173
178- This is necessary because the Default+Custom merge uses array replacement,
179- not array concatenation. By saving the complete class list to Custom,
180- we ensure no classes are lost during the merge.
174+ Steps:
175+ 1. Read existing classes from the target version
176+ 2. Add/update the new discovered class (deduplicate by $id)
177+ 3. Save back to the target version
181178
182179 Args:
183180 new_class: The newly discovered class schema to add/update
@@ -186,65 +183,36 @@ def _merge_and_save_class(self, new_class: Dict[str, Any]) -> None:
186183 new_class_id = new_class .get ("$id" ) or new_class .get ("x-aws-idp-document-type" )
187184 logger .info (f"Merging discovered class: { new_class_id } " )
188185
189- # Step 1: Read Default classes (base classes from deployment)
190- default_config = self .config_manager .get_configuration ("Config" , "default" )
191- default_classes : list = []
192- if (
193- default_config
194- and isinstance (default_config , IDPConfig )
195- and default_config .classes
196- ):
197- # Convert to list of dicts for easier manipulation
198- default_classes = [
199- cls
200- if isinstance (cls , dict )
201- else cls .model_dump ()
202- if hasattr (cls , "model_dump" )
203- else dict (cls )
204- for cls in default_config .classes
205- ]
206- logger .info (f"Found { len (default_classes )} classes in Default config" )
207-
208- # Step 2: Read existing Custom config (raw, no Pydantic defaults)
186+ # Read existing config for the target version (raw, no Pydantic defaults)
209187 existing_custom = (
210188 self .config_manager .get_raw_configuration ("Config" , version = self .version )
211189 or {}
212190 )
213191 custom_classes = list (existing_custom .get ("classes" , []))
214- logger .info (f"Found { len (custom_classes )} classes in Custom config" )
215-
216- # Step 3: Build merged class list - start with Default, override with Custom
217- # Use a dict keyed by class ID for efficient deduplication
218- merged_classes_by_id : Dict [str , Dict [str , Any ]] = {}
219-
220- # Add Default classes first
221- for cls in default_classes :
222- cls_id = cls .get ("$id" ) or cls .get ("x-aws-idp-document-type" )
223- if cls_id :
224- merged_classes_by_id [cls_id ] = cls
192+ logger .info (
193+ f"Found { len (custom_classes )} existing classes in version '{ self .version } '"
194+ )
225195
226- # Override/add Custom classes
196+ # Build class list from existing version classes, deduplicating by ID
197+ classes_by_id : Dict [str , Dict [str , Any ]] = {}
227198 for cls in custom_classes :
228199 cls_id = cls .get ("$id" ) or cls .get ("x-aws-idp-document-type" )
229200 if cls_id :
230- merged_classes_by_id [cls_id ] = cls
201+ classes_by_id [cls_id ] = cls
231202
232- # Step 4: Add/update the new discovered class
203+ # Add/update the new discovered class
233204 if new_class_id :
234- merged_classes_by_id [new_class_id ] = new_class
205+ classes_by_id [new_class_id ] = new_class
235206
236207 # Convert back to list
237- merged_classes = list (merged_classes_by_id .values ())
238- logger .info (f"Merged class list has { len (merged_classes )} classes" )
208+ merged_classes = list (classes_by_id .values ())
209+ logger .info (f"Saving { len (merged_classes )} classes to version ' { self . version } ' " )
239210
240- # Step 5: Save to Custom config
241- # The merged list will replace Default.classes during runtime merge,
242- # ensuring all classes (Default + Custom + new) are preserved
211+ # Save to version config
243212 existing_custom ["classes" ] = merged_classes
244213 self .config_manager .save_raw_configuration (
245214 "Config" , existing_custom , version = self .version
246215 )
247- logger .info (f"Saved { len (merged_classes )} classes to Custom config" )
248216
249217 def _validate_json_schema (self , schema : Dict [str , Any ]) -> tuple [bool , str ]:
250218 """
0 commit comments