@@ -240,6 +240,69 @@ test("#534: 全部合法的轮次 verdict=ok,metadata 不因补注长出多余
240240 store . close ( ) ;
241241} ) ;
242242
243+ test ( "#534: 覆盖率不足(没有逐条跳过)也判 degraded,与 dream_runs.status 同源" , async ( ) => {
244+ // CodeRabbit on #535:coverageShortfall 同样是校验层降级。此前 verdict 只认
245+ // skippedInvalid,于是「模型给了合法子集、但覆盖不到半数」的轮次会落成
246+ // dream_runs.status="degraded" 而 metadata 写 verdict="ok"——两套账在最该
247+ // 对齐的一格上又对不上。这条锁把它们绑死在同一个判据上。
248+ const { store, service, dream } = setup ( ) ;
249+ const ids = [ ] ;
250+ for ( let i = 0 ; i < 5 ; i += 1 ) {
251+ ids . push ( service . saveWithDedupe ( { type : "project" , title : `覆盖-${ i } ` , content : `覆盖正文-${ i } ` , importance : 3 } ) . memory . id ) ;
252+ }
253+ const result = await dream . runDream ( mockCtx ( {
254+ // 2/5 = 40% 覆盖 < 下限 50%,且没有一条被逐条跳过——降级理由只能是 coverage。
255+ // 必须让一条真落地(archive 掉一条):全 keep 轮会先被 noChange 分支截走
256+ // (那里 status 只由 summaryStored 决定),走不到 coverage 这一格。
257+ onConsolidation : ( ) => JSON . stringify ( [
258+ { action : "keep" , ids : [ ids [ 0 ] ] } ,
259+ { action : "archive" , ids : [ ids [ 1 ] ] , reason : "重复条目" }
260+ ] )
261+ } ) , service , { dreamImplicitKeep : true , dreamMinExplicitCoverage : 0.5 } ) ;
262+ const run = store . getDreamRun ( result . runId ) ;
263+ assert . equal ( run . applied , 1 , "先确认这条路径真的落地了改动(否则测的还是 noChange 分支)" ) ;
264+ assert . equal ( run . status , "degraded" , "覆盖率不足在 run 行上一直是 degraded(#104 方向 1)" ) ;
265+ const meta = metaOf ( consolidateAudits ( service ) [ 0 ] ) ;
266+ assert . equal ( meta . decision_validation . verdict , "degraded" , "审计 metadata 必须跟 run 行说同一件事" ) ;
267+ assert . equal ( meta . decision_validation . skipped_count , 0 , "这条路径没有逐条跳过,判据只能是 coverage" ) ;
268+ assert . equal ( meta . decision_validation . survivor_count , 5 , "未 claim 的 4 条被隐式 keep 补齐,全部存活" ) ;
269+ store . close ( ) ;
270+ } ) ;
271+
272+ test ( "#534: 审计行的 id 来自本次插入,不是「当前最大 id」" , async ( ) => {
273+ // CodeRabbit on #535(Major):saveLlmAudit 原来先 INSERT 再
274+ // `SELECT * ORDER BY id DESC LIMIT 1`。库支持多进程共用,另一个写者插在
275+ // 中间就会把这行读成别人的调用,随后 annotate 把本轮判决写到别人头上。
276+ // 这里用「连续两次写入各自拿回自己那行」锁住回读口径。
277+ const { store, service } = setup ( ) ;
278+ const first = service . saveLlmAudit ( { trigger_source : "t" , operation_type : "o1" , model_id : "m" , status : "success" } ) ;
279+ service . saveLlmAudit ( { trigger_source : "t" , operation_type : "o2" , model_id : "m" , status : "success" } ) ;
280+ const second = service . saveLlmAudit ( { trigger_source : "t" , operation_type : "o3" , model_id : "m" , status : "success" } ) ;
281+ assert . equal ( first . operation_type , "o1" , "回读的必须是自己插入的那一行" ) ;
282+ assert . equal ( second . operation_type , "o3" ) ;
283+ assert . ok ( second . id > first . id , "id 单调,但取值只认本次插入" ) ;
284+
285+ // 判决补注同样按 id 定位:插一行噪声在后面,也不能改变目标行。
286+ service . saveLlmAudit ( { trigger_source : "t" , operation_type : "noise" , model_id : "m" , status : "success" } ) ;
287+ const updated = service . annotateLlmAudit ( first . id , { decision_validation : { verdict : "rejected" } } ) ;
288+ assert . equal ( updated . operation_type , "o1" , "补注不许漂到别的行" ) ;
289+ const rows = service . listLlmAudits ( { limit : 20 } ) ;
290+ assert . equal ( rows . find ( ( r ) => r . operation_type === "noise" ) . metadata , undefined , "噪声行不许被写上本轮判决" ) ;
291+ store . close ( ) ;
292+ } ) ;
293+
294+ test ( "#534: saveLlmAudit 不再用「取最大 id」回读(源码锁)" , ( ) => {
295+ const src = readFileSync ( new URL ( "../src/store.js" , import . meta. url ) , "utf8" ) ;
296+ const body = src . slice ( src . indexOf ( "function saveLlmAudit(entry)" ) ) ;
297+ const head = body . slice ( 0 , body . indexOf ( "\n }\n" ) ) ;
298+ // 先去注释再判:解释这条坑的注释本身会引述旧写法,不剥掉就永远命中自己。
299+ const code = head . replace ( / ^ \s * \/ \/ .* $ / gm, "" ) ;
300+ assert . equal ( code . includes ( "ORDER BY id DESC LIMIT 1" ) , false ,
301+ "多进程共用同一库时「取最大 id」会读成别人的行——必须用 lastInsertRowid" ) ;
302+ assert . match ( code , / l a s t I n s e r t R o w i d / , "回读口径锁在本次插入返回的 rowid 上" ) ;
303+ assert . match ( code , / S E L E C T \* F R O M l l m _ a u d i t _ l o g s W H E R E i d = \? / , "回读用主键定位,不用排序取首行" ) ;
304+ } ) ;
305+
243306// ---------------------------------------------------------------- 改动面守卫
244307
245308test ( "#534: decisions 列之外没有别的写入口往那一列塞非决策对象" , ( ) => {
0 commit comments