@@ -29,6 +29,10 @@ function fileExists(filePath) {
2929 }
3030}
3131
32+ function isNonEmptyString ( value ) {
33+ return typeof value === 'string' && value . trim ( ) . length > 0 ;
34+ }
35+
3236function createThreshold ( thresholdId , unit , target , summary ) {
3337 return {
3438 thresholdId,
@@ -64,6 +68,14 @@ function evaluateBackendBaselineSufficiency(options = {}) {
6468 repoRoot,
6569 provenance,
6670 } ) ;
71+ const runtimeState = options . runtimeState || {
72+ queryCount : 0 ,
73+ queryP95Ms : undefined ,
74+ queryAverageMs : undefined ,
75+ queryMaxMs : undefined ,
76+ evidenceMode : 'policy_default' ,
77+ } ;
78+ const storeDiagnostics = options . storeDiagnostics || { } ;
6779 const packageJson = readJsonIfExists ( path . join ( repoRoot , 'package.json' ) ) ;
6880 const scripts = packageJson . scripts || { } ;
6981
@@ -116,14 +128,27 @@ function evaluateBackendBaselineSufficiency(options = {}) {
116128 'Local backup, restore, and deterministic rebuild remain acceptable operator tactics.' ,
117129 'Product value is currently gated more by conversation/memory/operator surfaces than by backend distribution.' ,
118130 ] ;
131+ const measuredQueryLatency =
132+ Number ( runtimeState . queryCount || 0 ) > 0
133+ && Number . isFinite ( runtimeState . queryP95Ms )
134+ && Number ( runtimeState . queryP95Ms ) >= 0 ;
135+ const queryLatencyObservationMode = measuredQueryLatency ? 'measured' : 'policy_default' ;
136+ const persistenceObservationMode =
137+ isNonEmptyString ( storeDiagnostics . lastLoadAt ) || isNonEmptyString ( storeDiagnostics . lastSaveAt )
138+ ? 'observed_event'
139+ : 'policy_default' ;
119140
120141 const thresholds = [
121- createThreshold (
122- 'warm_query_p95_ms' ,
123- 'ms' ,
124- 300 ,
125- 'Hold local while warm interactive query latency stays within a 300 ms p95 budget on a reference device.'
126- ) ,
142+ {
143+ ...createThreshold (
144+ 'warm_query_p95_ms' ,
145+ 'ms' ,
146+ 300 ,
147+ 'Hold local while warm interactive query latency stays within a 300 ms p95 budget on a reference device.'
148+ ) ,
149+ observationStatus : queryLatencyObservationMode ,
150+ observedValue : measuredQueryLatency ? Number ( runtimeState . queryP95Ms ) : undefined ,
151+ } ,
127152 createThreshold (
128153 'cold_query_p95_ms' ,
129154 'ms' ,
@@ -149,6 +174,7 @@ function evaluateBackendBaselineSufficiency(options = {}) {
149174 foundationReadiness . status === 'integrated'
150175 && foundationReadiness . decision === 'go'
151176 && foundationReadiness . promotionCriteriaPassed === foundationReadiness . promotionCriteriaTotal ;
177+ const queryLatencyWithinThreshold = ! measuredQueryLatency || Number ( runtimeState . queryP95Ms ) <= 300 ;
152178
153179 const dimensions = [
154180 createDimension (
@@ -159,8 +185,12 @@ function evaluateBackendBaselineSufficiency(options = {}) {
159185 ) ,
160186 createDimension (
161187 'query_latency' ,
162- foundationReadiness . baseline . vectorAdapterIndependent && foundationReadiness . baseline . queryBackendDefaultMode === 'local_hybrid' ,
163- 'Local hybrid query + ANN acceleration is present; no measured repo evidence currently forces backend escalation.' ,
188+ foundationReadiness . baseline . vectorAdapterIndependent
189+ && foundationReadiness . baseline . queryBackendDefaultMode === 'local_hybrid'
190+ && queryLatencyWithinThreshold ,
191+ measuredQueryLatency
192+ ? `Measured runtime query p95 is ${ Number ( runtimeState . queryP95Ms ) . toFixed ( 4 ) } ms across ${ Number ( runtimeState . queryCount || 0 ) } recorded queries.`
193+ : 'Local hybrid query + ANN acceleration is present; no measured repo evidence currently forces backend escalation.' ,
164194 'Escalate when measured warm or cold p95 latency stays above target after local tuning.'
165195 ) ,
166196 createDimension (
@@ -219,6 +249,15 @@ function evaluateBackendBaselineSufficiency(options = {}) {
219249 ) ;
220250 }
221251
252+ if ( measuredQueryLatency && Number ( runtimeState . queryP95Ms ) > 300 ) {
253+ activeTriggers . push (
254+ createTrigger (
255+ 'query_latency_threshold_exceeded' ,
256+ `Measured runtime query p95 ${ Number ( runtimeState . queryP95Ms ) . toFixed ( 4 ) } ms exceeds the current warm-query target of 300 ms.`
257+ )
258+ ) ;
259+ }
260+
222261 const sufficient = activeTriggers . length === 0 ;
223262
224263 if ( sufficient ) {
@@ -268,6 +307,24 @@ function evaluateBackendBaselineSufficiency(options = {}) {
268307 packageScripts : {
269308 sufficiencyVerifierPresent,
270309 } ,
310+ observations : {
311+ queryLatency : {
312+ queryCount : Number ( runtimeState . queryCount || 0 ) ,
313+ queryP95Ms : measuredQueryLatency ? Number ( runtimeState . queryP95Ms ) : undefined ,
314+ queryAverageMs : measuredQueryLatency && Number . isFinite ( runtimeState . queryAverageMs )
315+ ? Number ( runtimeState . queryAverageMs )
316+ : undefined ,
317+ queryMaxMs : measuredQueryLatency && Number . isFinite ( runtimeState . queryMaxMs )
318+ ? Number ( runtimeState . queryMaxMs )
319+ : undefined ,
320+ evidenceMode : queryLatencyObservationMode ,
321+ } ,
322+ persistence : {
323+ lastLoadAt : isNonEmptyString ( storeDiagnostics . lastLoadAt ) ? storeDiagnostics . lastLoadAt : undefined ,
324+ lastSaveAt : isNonEmptyString ( storeDiagnostics . lastSaveAt ) ? storeDiagnostics . lastSaveAt : undefined ,
325+ evidenceMode : persistenceObservationMode ,
326+ } ,
327+ } ,
271328 provenance,
272329 mandatoryChecks : [
273330 {
@@ -357,6 +414,16 @@ function formatBackendBaselineSufficiencyMarkdown(result, reportPaths) {
357414 lines . push ( '' , '## Package Scripts' , '' ) ;
358415 lines . push ( `- Sufficiency verifier present: ${ result . packageScripts . sufficiencyVerifierPresent ? 'yes' : 'no' } ` ) ;
359416
417+ lines . push ( '' , '## Observations' , '' ) ;
418+ lines . push ( `- Query latency evidence mode: ${ result . observations . queryLatency . evidenceMode } ` ) ;
419+ lines . push ( `- Query count: ${ result . observations . queryLatency . queryCount } ` ) ;
420+ lines . push ( `- Query p95 ms: ${ typeof result . observations . queryLatency . queryP95Ms === 'number' ? result . observations . queryLatency . queryP95Ms : 'unmeasured' } ` ) ;
421+ lines . push ( `- Query average ms: ${ typeof result . observations . queryLatency . queryAverageMs === 'number' ? result . observations . queryLatency . queryAverageMs : 'unmeasured' } ` ) ;
422+ lines . push ( `- Query max ms: ${ typeof result . observations . queryLatency . queryMaxMs === 'number' ? result . observations . queryLatency . queryMaxMs : 'unmeasured' } ` ) ;
423+ lines . push ( `- Persistence evidence mode: ${ result . observations . persistence . evidenceMode } ` ) ;
424+ lines . push ( `- Last load at: ${ result . observations . persistence . lastLoadAt || 'unobserved' } ` ) ;
425+ lines . push ( `- Last save at: ${ result . observations . persistence . lastSaveAt || 'unobserved' } ` ) ;
426+
360427 lines . push ( '' , '## Provenance' , '' ) ;
361428 lines . push ( `- Repo root source: ${ result . provenance . repoRootSource } ` ) ;
362429 lines . push ( `- Runtime project root aligned: ${ result . provenance . runtimeProjectRootAligned ? 'yes' : 'no' } ` ) ;
0 commit comments