@@ -7,12 +7,22 @@ const EPISTEMIC_PRIORITY = { observation: 3, inferred: 2, subjective: 1 };
77
88/**
99 * Validate a dream decision list against a snapshot of eligible memories.
10- * @param decisions - LLM-produced decision list.
10+ * @param decisions - LLM-produced decision list. In skipInvalid mode, invalid
11+ * entries are spliced out of this array in place (the caller reuses the same
12+ * reference downstream for apply/audit); implicit keeps are appended here too.
1113 * @param snapshot - Map<id, memory> of eligible (non-archived, non-summary) entries.
12- * @returns {{ok: boolean, errors: string[]} }
14+ * @returns {{ok: boolean, errors: string[], skipped?: Array<{index, action, ids, error}> } }
1315 */
1416export function validateDecisions ( decisions , snapshot , options = { } ) {
1517 const errors = [ ] ;
18+ // Issue #89 回归修复(v0.6.9 / Issue #26 的 skipInvalid 路径原样移植,该路径
19+ // 在 v0.7.11 重写中丢失):skipInvalid 开启时,单条非法决策只跳过该条(记录
20+ // 到 skipped、不 claim 任何 id),合法子集照常应用,调用方据此把 run 记为
21+ // degraded。全局信号(update/create 上限、显式覆盖率下限)依旧整单拒绝——
22+ // 刷爆上限或覆盖率不达标的输出是模型坏了,不是轻微 schema 漂移。
23+ const skipped = [ ] ;
24+ const survivors = [ ] ;
25+ const skipInvalid = options . skipInvalid === true ;
1626 const maxUpdatePerRun = options . maxUpdatePerRun ?? 2 ;
1727 const minAgeHours = options . minAgeHours ?? 24 ;
1828 if ( ! Array . isArray ( decisions ) || decisions . length === 0 ) {
@@ -21,106 +31,122 @@ export function validateDecisions(decisions, snapshot, options = {}) {
2131 const claimed = new Set ( ) ;
2232 for ( const [ index , d ] of decisions . entries ( ) ) {
2333 const at = `decision[${ index } ]` ;
34+ // 单条校验错误先进 local:skipInvalid 模式下整条跳过,严格模式下才并入
35+ // 全局 errors(沿用 v0.6.9 的双轨结构)。
36+ const local = [ ] ;
37+ const ids = d && d . action === "conflict" ? [ d . winner , d . loser ] : ( d ?. ids ?? [ ] ) ;
2438 if ( ! d || typeof d !== "object" || ! ACTIONS . has ( d . action ) ) {
25- errors . push ( `${ at } : invalid action ${ JSON . stringify ( d ?. action ) } ` ) ;
26- continue ;
27- }
28- const ids = d . action === "conflict" ? [ d . winner , d . loser ] : ( d . ids ?? [ ] ) ;
29- if ( d . action === "conflict" ) {
39+ local . push ( `${ at } : invalid action ${ JSON . stringify ( d ?. action ) } ` ) ;
40+ } else if ( d . action === "conflict" ) {
3041 if ( ! d . winner || ! d . loser || d . winner === d . loser ) {
31- errors . push ( `${ at } : conflict needs distinct winner and loser` ) ;
32- continue ;
42+ local . push ( `${ at } : conflict needs distinct winner and loser` ) ;
3343 }
3444 } else if ( d . action === "create" ) {
3545 // Mint a fresh memory (sleep pattern discovery). Claims no existing id,
3646 // so it skips the claiming loop below; evidence is optional provenance
3747 // (already filtered to real ids by the caller) and is stored in content.
3848 if ( typeof d . title !== "string" || ! d . title . trim ( ) ) {
39- errors . push ( `${ at } : create needs non-empty title` ) ;
40- continue ;
49+ local . push ( `${ at } : create needs non-empty title` ) ;
4150 }
4251 if ( typeof d . content !== "string" || ! d . content . trim ( ) ) {
43- errors . push ( `${ at } : create needs non-empty content` ) ;
44- continue ;
52+ local . push ( `${ at } : create needs non-empty content` ) ;
4553 }
4654 if ( d . importance !== undefined && ( ! Number . isInteger ( d . importance ) || d . importance < 1 || d . importance > 5 ) ) {
47- errors . push ( `${ at } : create importance must be an integer 1-5 when provided` ) ;
55+ local . push ( `${ at } : create importance must be an integer 1-5 when provided` ) ;
4856 }
4957 if ( typeof d . type !== "string" || ! d . type . trim ( ) ) {
50- errors . push ( `${ at } : create needs non-empty type` ) ;
58+ local . push ( `${ at } : create needs non-empty type` ) ;
5159 }
52- continue ;
5360 } else if ( ! Array . isArray ( d . ids ) || d . ids . length === 0 ) {
54- errors . push ( `${ at } : ${ d . action } needs non-empty ids` ) ;
55- continue ;
61+ local . push ( `${ at } : ${ d . action } needs non-empty ids` ) ;
5662 }
5763 // update-specific field validation runs BEFORE claiming ids, so a failing
5864 // update never pollutes the claimed set (which drives the "every id must
5965 // appear in a decision" check below).
60- if ( d . action === "update" ) {
66+ if ( local . length === 0 && d ? .action === "update" ) {
6167 // 只能更新单条
6268 if ( ! Array . isArray ( d . ids ) || d . ids . length !== 1 ) {
63- errors . push ( `${ at } : update must target exactly one id` ) ;
64- continue ;
65- }
66- // 必须产生实际变化
67- const mem = snapshot . get ( d . ids [ 0 ] ) ;
68- const hasChange = ( d . title !== undefined && d . title !== mem ?. title )
69- || ( d . content !== undefined && d . content !== mem ?. content )
70- || ( d . importance !== undefined && d . importance !== mem ?. importance ) ;
71- if ( ! hasChange ) {
72- errors . push ( `${ at } : update must change at least one field` ) ;
73- continue ;
74- }
75- // 不能更新 summary
76- if ( mem ?. type === "summary" ) {
77- errors . push ( `${ at } : cannot update summary via update action` ) ;
78- continue ;
79- }
80- // 保护期:新建记忆不可立即被 update(可配置)
81- const ageHours = ( Date . now ( ) - new Date ( mem ?. created_at ) . getTime ( ) ) / 3600000 ;
82- if ( ageHours < minAgeHours ) {
83- errors . push ( `${ at } : memory too young (< ${ minAgeHours } h)` ) ;
84- continue ;
69+ local . push ( `${ at } : update must target exactly one id` ) ;
70+ } else {
71+ // 必须产生实际变化
72+ const mem = snapshot . get ( d . ids [ 0 ] ) ;
73+ const hasChange = ( d . title !== undefined && d . title !== mem ?. title )
74+ || ( d . content !== undefined && d . content !== mem ?. content )
75+ || ( d . importance !== undefined && d . importance !== mem ?. importance ) ;
76+ if ( ! hasChange ) {
77+ local . push ( `${ at } : update must change at least one field` ) ;
78+ } else if ( mem ?. type === "summary" ) {
79+ // 不能更新 summary
80+ local . push ( `${ at } : cannot update summary via update action` ) ;
81+ } else {
82+ // 保护期:新建记忆不可立即被 update(可配置)
83+ const ageHours = ( Date . now ( ) - new Date ( mem ?. created_at ) . getTime ( ) ) / 3600000 ;
84+ if ( ageHours < minAgeHours ) {
85+ local . push ( `${ at } : memory too young (< ${ minAgeHours } h)` ) ;
86+ }
87+ }
8588 }
8689 }
87- for ( const id of ids ) {
88- const mem = snapshot . get ( id ) ;
89- if ( ! mem ) {
90- errors . push ( `${ at } : unknown id ${ JSON . stringify ( id ) } ` ) ;
91- } else if ( mem . archived || mem . type === "summary" ) {
92- errors . push ( `${ at } : id ${ JSON . stringify ( id ) } is archived or summary (not eligible)` ) ;
90+ if ( local . length === 0 && d && d . action !== "create" ) {
91+ // seen 捕获同一条决策内的重复 id;claimed 只含先前存活决策的 id
92+ // (被跳过的决策不 claim,其目标留给其它合法决策/隐式 keep)。
93+ const seen = new Set ( ) ;
94+ for ( const id of ids ) {
95+ const mem = snapshot . get ( id ) ;
96+ if ( ! mem ) {
97+ local . push ( `${ at } : unknown id ${ JSON . stringify ( id ) } ` ) ;
98+ } else if ( mem . archived || mem . type === "summary" ) {
99+ local . push ( `${ at } : id ${ JSON . stringify ( id ) } is archived or summary (not eligible)` ) ;
100+ }
101+ if ( claimed . has ( id ) || seen . has ( id ) ) {
102+ local . push ( `${ at } : id ${ JSON . stringify ( id ) } claimed by multiple decisions` ) ;
103+ }
104+ seen . add ( id ) ;
93105 }
94- if ( claimed . has ( id ) ) {
95- errors . push ( `${ at } : id ${ JSON . stringify ( id ) } claimed by multiple decisions` ) ;
106+ if ( local . length === 0 && d . action === "merge" ) {
107+ if ( ! d . keepSource || ! d . ids . includes ( d . keepSource ) ) {
108+ local . push ( `${ at } : merge keepSource must be one of ids` ) ;
109+ }
110+ if ( typeof d . title !== "string" || ! d . title . trim ( ) || typeof d . content !== "string" || ! d . content . trim ( ) ) {
111+ local . push ( `${ at } : merge needs non-empty title and content` ) ;
112+ }
113+ if ( d . importance !== undefined && ( ! Number . isInteger ( d . importance ) || d . importance < 1 || d . importance > 5 ) ) {
114+ local . push ( `${ at } : merge importance must be an integer 1-5 when provided` ) ;
115+ }
116+ // Merging across types would blur preference/project/decision boundaries
117+ // in the injected context; the snapshot carries each entry's type.
118+ // Issue #26 (P1):默认禁止跨类型合并。用户显式开启 allowCrossTypeMerge
119+ // 后放宽该检查,类型边界由用户自行承担。
120+ const mergeTypes = new Set ( d . ids . map ( ( id ) => snapshot . get ( id ) ?. type ) ) ;
121+ if ( mergeTypes . size > 1 && options . allowCrossTypeMerge !== true ) {
122+ local . push ( `${ at } : merge ids span multiple types (${ [ ...mergeTypes ] . join ( ", " ) } )` ) ;
123+ }
96124 }
97- claimed . add ( id ) ;
98125 }
99- if ( d . action === "merge" ) {
100- if ( ! d . keepSource || ! d . ids . includes ( d . keepSource ) ) {
101- errors . push ( `${ at } : merge keepSource must be one of ids` ) ;
102- }
103- if ( typeof d . title !== "string" || ! d . title . trim ( ) || typeof d . content !== "string" || ! d . content . trim ( ) ) {
104- errors . push ( `${ at } : merge needs non-empty title and content` ) ;
105- }
106- if ( d . importance !== undefined && ( ! Number . isInteger ( d . importance ) || d . importance < 1 || d . importance > 5 ) ) {
107- errors . push ( `${ at } : merge importance must be an integer 1-5 when provided` ) ;
108- }
109- // Merging across types would blur preference/project/decision boundaries
110- // in the injected context; the snapshot carries each entry's type.
111- const mergeTypes = new Set ( d . ids . map ( ( id ) => snapshot . get ( id ) ?. type ) ) ;
112- if ( mergeTypes . size > 1 ) {
113- errors . push ( `${ at } : merge ids span multiple types (${ [ ...mergeTypes ] . join ( ", " ) } )` ) ;
126+ if ( local . length > 0 ) {
127+ if ( skipInvalid ) {
128+ // 单条非法 → 跳过该决策,不 claim id(其目标记忆留给其它合法决策/隐式
129+ // keep),并记录到 skipped 供调用方日志/审计。信息性跳过绝不写入全局
130+ // errors,否则会误触发下方的整单拒绝。
131+ skipped . push ( { index, action : d ?. action , ids, error : local . join ( "; " ) } ) ;
132+ } else {
133+ errors . push ( ...local ) ;
114134 }
135+ continue ;
136+ }
137+ if ( d . action !== "create" ) {
138+ for ( const id of ids ) claimed . add ( id ) ;
115139 }
140+ survivors . push ( d ) ;
116141 }
117- // Cap update churn: too many edits in one cycle signals a runaway model
118- const updateCount = decisions . filter ( ( d ) => d . action === "update" ) . length ;
142+ // Cap update churn: too many edits in one cycle signals a runaway model.
143+ // 全局信号——skipInvalid 模式下依旧整单拒绝(见函数头注释)。
144+ const updateCount = survivors . filter ( ( d ) => d . action === "update" ) . length ;
119145 if ( updateCount > maxUpdatePerRun ) {
120146 errors . push ( `too many update decisions: ${ updateCount } > ${ maxUpdatePerRun } ` ) ;
121147 }
122148 // Cap pattern minting per run (sleepMaxPatternPerRun passes through here).
123- const createCount = decisions . filter ( ( d ) => d . action === "create" ) . length ;
149+ const createCount = survivors . filter ( ( d ) => d . action === "create" ) . length ;
124150 const maxCreatePerRun = options . maxCreatePerRun ?? 5 ;
125151 if ( createCount > maxCreatePerRun ) {
126152 errors . push ( `too many create decisions: ${ createCount } > ${ maxCreatePerRun } ` ) ;
@@ -137,25 +163,31 @@ export function validateDecisions(decisions, snapshot, options = {}) {
137163 // snapshot(claimed.size / snapshot.size < dreamMinExplicitCoverage)时整单拒绝,
138164 // 而不是用 keep 把绝大部分 snapshot 全部"通过"。
139165 if ( errors . length > 0 ) {
140- return { ok : false , errors } ;
166+ return { ok : false , errors, skipped } ;
141167 }
142168 const minCoverage = options . dreamMinExplicitCoverage ?? 0.5 ;
143169 if ( options . dreamImplicitKeep !== false ) {
144170 const coverage = snapshot . size > 0 ? claimed . size / snapshot . size : 1 ;
145171 if ( coverage < minCoverage ) {
146172 errors . push ( `explicit decision coverage ${ Math . round ( coverage * 100 ) } % < minimum ${ Math . round ( minCoverage * 100 ) } %` ) ;
147- return { ok : false , errors } ;
173+ return { ok : false , errors, skipped } ;
148174 }
149175 for ( const id of snapshot . keys ( ) ) {
150- if ( ! claimed . has ( id ) ) decisions . push ( { action : "keep" , ids : [ id ] } ) ;
176+ if ( ! claimed . has ( id ) ) survivors . push ( { action : "keep" , ids : [ id ] } ) ;
151177 }
152178 } else {
153179 for ( const id of snapshot . keys ( ) ) {
154180 if ( ! claimed . has ( id ) ) errors . push ( `memory ${ JSON . stringify ( id ) } missing from decisions` ) ;
155181 }
156- if ( errors . length > 0 ) return { ok : false , errors } ;
182+ if ( errors . length > 0 ) return { ok : false , errors, skipped } ;
183+ }
184+ // 调用方下游(apply/audit)复用同一 decisions 引用:就地同步为 survivors——
185+ // 在 skipInvalid 模式下去掉被跳过的非法决策;在隐式 keep 下追加补齐的 keep。
186+ // 内容一致时(无跳过、无补齐)为 no-op。
187+ if ( survivors . length !== decisions . length ) {
188+ decisions . splice ( 0 , decisions . length , ...survivors ) ;
157189 }
158- return { ok : true , errors } ;
190+ return { ok : true , errors, skipped } ;
159191}
160192
161193/** Marker thrown when a decision target changed since the run snapshot. */
0 commit comments