@@ -41,7 +41,7 @@ def is_schema_drift_enabled() -> bool:
4141
4242
4343def schema_drift_sample_limit () -> int :
44- """Max JSONL records per session to fingerprint (default 3). Set 0 to disable sampling cap."""
44+ """Max JSONL records per session to fingerprint (default 3). 0 means no cap (all records) ."""
4545 raw = os .environ .get ("CLAUDE_CODE_CHAT_BROWSER_SCHEMA_DRIFT_SAMPLE" , "3" ).strip ()
4646 try :
4747 return max (0 , int (raw ))
@@ -75,6 +75,8 @@ def _load_baseline() -> tuple[frozenset[str], frozenset[str]]:
7575 return _baseline_cache
7676 try :
7777 raw = json .loads (BASELINE_PATH .read_text (encoding = "utf-8" ))
78+ if not isinstance (raw , dict ):
79+ raise ValueError ("schema_baseline.json: root must be an object" )
7880 fields = raw .get ("fields" , {})
7981 if not isinstance (fields , dict ):
8082 raise ValueError ("schema_baseline.json: 'fields' must be an object" )
@@ -122,30 +124,31 @@ def record_parse_drift(observed_paths: set[str]) -> SchemaDriftReport | None:
122124 except (OSError , json .JSONDecodeError , ValueError , TypeError ):
123125 return None
124126
127+ genuinely_new : list [str ] = []
128+ missing_fields : list [str ] = []
125129 with _lock :
126130 global _last_report
127131 prior_new = set (_last_report ["new_fields" ])
128132 genuinely_new = sorted (set (report ["new_fields" ]) - prior_new )
129133 merged_new = sorted (prior_new | set (report ["new_fields" ]))
134+ missing_fields = list (report ["missing_fields" ])
135+ _last_report = {
136+ "known_fields" : report ["known_fields" ],
137+ "new_fields" : merged_new ,
138+ "missing_fields" : missing_fields ,
139+ "has_drift" : bool (merged_new or missing_fields ),
140+ }
130141
131142 if genuinely_new :
132143 _log .warning (
133144 "schema drift: new JSONL field paths not in baseline: %s" ,
134145 genuinely_new ,
135146 )
136- if report [ " missing_fields" ] :
147+ if missing_fields :
137148 _log .warning (
138149 "schema drift: missing required JSONL field paths in sampled records: %s" ,
139- report [ " missing_fields" ] ,
150+ missing_fields ,
140151 )
141-
142- with _lock :
143- _last_report = {
144- "known_fields" : report ["known_fields" ],
145- "new_fields" : merged_new ,
146- "missing_fields" : list (report ["missing_fields" ]),
147- "has_drift" : bool (merged_new or report ["missing_fields" ]),
148- }
149152 return report
150153
151154
0 commit comments