@@ -90,6 +90,27 @@ export function sanitizeReflectionSliceLines(lines: string[]): string[] {
9090 . filter ( ( line ) => ! isPlaceholderReflectionSliceLine ( line ) ) ;
9191}
9292
93+ const INJECTABLE_REFLECTION_BLOCK_PATTERNS : RegExp [ ] = [
94+ / ^ \s * (?: (?: n e x t | t h i s ) \s + r u n \s + ) ? (?: i g n o r e | d i s r e g a r d | f o r g e t | o v e r r i d e | b y p a s s ) \b [ \s \S ] { 0 , 80 } \b (?: i n s t r u c t i o n s ? | g u a r d r a i l s ? | p o l i c y | d e v e l o p e r | s y s t e m ) \b / i,
95+ / \b (?: r e v e a l | p r i n t | d u m p | s h o w | o u t p u t ) \b [ \s \S ] { 0 , 80 } \b (?: s y s t e m p r o m p t | d e v e l o p e r p r o m p t | h i d d e n p r o m p t | h i d d e n i n s t r u c t i o n s ? | f u l l p r o m p t | p r o m p t v e r b a t i m | s e c r e t s ? | k e y s ? | t o k e n s ? ) \b / i,
96+ / < \s * \/ ? \s * (?: s y s t e m | a s s i s t a n t | u s e r | t o o l | d e v e l o p e r | i n h e r i t e d - r u l e s | d e r i v e d - f o c u s ) \b [ ^ > ] * > / i,
97+ / ^ (?: s y s t e m | a s s i s t a n t | u s e r | d e v e l o p e r | t o o l ) \s * : / i,
98+ ] ;
99+
100+ export function isUnsafeInjectableReflectionLine ( line : string ) : boolean {
101+ const normalized = normalizeReflectionSliceLine ( line ) ;
102+ if ( ! normalized ) return true ;
103+ return INJECTABLE_REFLECTION_BLOCK_PATTERNS . some ( ( pattern ) =>
104+ pattern . test ( normalized ) ,
105+ ) ;
106+ }
107+
108+ export function sanitizeInjectableReflectionLines ( lines : string [ ] ) : string [ ] {
109+ return sanitizeReflectionSliceLines ( lines ) . filter (
110+ ( line ) => ! isUnsafeInjectableReflectionLine ( line ) ,
111+ ) ;
112+ }
113+
93114function isInvariantRuleLike ( line : string ) : boolean {
94115 return / ^ ( a l w a y s | n e v e r | w h e n \b | i f \b | b e f o r e \b | a f t e r \b | p r e f e r \b | a v o i d \b | r e q u i r e \b | o n l y \b | d o n o t \b | m u s t \b | s h o u l d \b ) / i. test ( line ) ||
95116 / \b ( m u s t | s h o u l d | n e v e r | a l w a y s | p r e f e r | a v o i d | r e q u i r e d ? ) \b / i. test ( line ) ;
@@ -172,7 +193,10 @@ export function extractReflectionMappedMemories(reflectionText: string): Reflect
172193 return extractReflectionMappedMemoryItems ( reflectionText ) . map ( ( { text, category, heading } ) => ( { text, category, heading } ) ) ;
173194}
174195
175- export function extractReflectionMappedMemoryItems ( reflectionText : string ) : ReflectionMappedMemoryItem [ ] {
196+ function extractReflectionMappedMemoryItemsWithSanitizer (
197+ reflectionText : string ,
198+ sanitizeLines : ( lines : string [ ] ) => string [ ] ,
199+ ) : ReflectionMappedMemoryItem [ ] {
176200 const mappedSections : Array < {
177201 heading : string ;
178202 category : "preference" | "fact" | "decision" ;
@@ -201,30 +225,45 @@ export function extractReflectionMappedMemoryItems(reflectionText: string): Refl
201225 ] ;
202226
203227 return mappedSections . flatMap ( ( { heading, category, mappedKind } ) => {
204- const lines = sanitizeReflectionSliceLines ( parseSectionBullets ( reflectionText , heading ) ) ;
228+ const lines = sanitizeLines ( parseSectionBullets ( reflectionText , heading ) ) ;
205229 const groupSize = lines . length ;
206230 return lines . map ( ( text , ordinal ) => ( { text, category, heading, mappedKind, ordinal, groupSize } ) ) ;
207231 } ) ;
208232}
209233
210- export function extractReflectionSlices ( reflectionText : string ) : ReflectionSlices {
234+ export function extractReflectionMappedMemoryItems ( reflectionText : string ) : ReflectionMappedMemoryItem [ ] {
235+ return extractReflectionMappedMemoryItemsWithSanitizer ( reflectionText , sanitizeReflectionSliceLines ) ;
236+ }
237+
238+ export function extractInjectableReflectionMappedMemoryItems ( reflectionText : string ) : ReflectionMappedMemoryItem [ ] {
239+ return extractReflectionMappedMemoryItemsWithSanitizer ( reflectionText , sanitizeInjectableReflectionLines ) ;
240+ }
241+
242+ export function extractInjectableReflectionMappedMemories ( reflectionText : string ) : ReflectionMappedMemory [ ] {
243+ return extractInjectableReflectionMappedMemoryItems ( reflectionText ) . map ( ( { text, category, heading } ) => ( { text, category, heading } ) ) ;
244+ }
245+
246+ function extractReflectionSlicesWithSanitizer (
247+ reflectionText : string ,
248+ sanitizeLines : ( lines : string [ ] ) => string [ ] ,
249+ ) : ReflectionSlices {
211250 const invariantSection = parseSectionBullets ( reflectionText , "Invariants" ) ;
212251 const derivedSection = parseSectionBullets ( reflectionText , "Derived" ) ;
213252 const mergedSection = parseSectionBullets ( reflectionText , "Invariants & Reflections" ) ;
214253
215- const invariantsPrimary = sanitizeReflectionSliceLines ( invariantSection ) . filter ( isInvariantRuleLike ) ;
216- const derivedPrimary = sanitizeReflectionSliceLines ( derivedSection ) . filter ( isDerivedDeltaLike ) ;
254+ const invariantsPrimary = sanitizeLines ( invariantSection ) . filter ( isInvariantRuleLike ) ;
255+ const derivedPrimary = sanitizeLines ( derivedSection ) . filter ( isDerivedDeltaLike ) ;
217256
218- const invariantLinesLegacy = sanitizeReflectionSliceLines (
257+ const invariantLinesLegacy = sanitizeLines (
219258 mergedSection . filter ( ( line ) => / i n v a r i a n t | s t a b l e | p o l i c y | r u l e / i. test ( line ) )
220259 ) . filter ( isInvariantRuleLike ) ;
221- const reflectionLinesLegacy = sanitizeReflectionSliceLines (
260+ const reflectionLinesLegacy = sanitizeLines (
222261 mergedSection . filter ( ( line ) => / r e f l e c t | i n h e r i t | d e r i v e | c h a n g e | a p p l y / i. test ( line ) )
223262 ) . filter ( isDerivedDeltaLike ) ;
224- const openLoopLines = sanitizeReflectionSliceLines ( parseSectionBullets ( reflectionText , "Open loops / next actions" ) )
263+ const openLoopLines = sanitizeLines ( parseSectionBullets ( reflectionText , "Open loops / next actions" ) )
225264 . filter ( isOpenLoopAction )
226265 . filter ( isDerivedDeltaLike ) ;
227- const durableDecisionLines = sanitizeReflectionSliceLines ( parseSectionBullets ( reflectionText , "Decisions (durable)" ) )
266+ const durableDecisionLines = sanitizeLines ( parseSectionBullets ( reflectionText , "Decisions (durable)" ) )
228267 . filter ( isInvariantRuleLike ) ;
229268
230269 const invariants = invariantsPrimary . length > 0
@@ -240,8 +279,15 @@ export function extractReflectionSlices(reflectionText: string): ReflectionSlice
240279 } ;
241280}
242281
243- export function extractReflectionSliceItems ( reflectionText : string ) : ReflectionSliceItem [ ] {
244- const slices = extractReflectionSlices ( reflectionText ) ;
282+ export function extractReflectionSlices ( reflectionText : string ) : ReflectionSlices {
283+ return extractReflectionSlicesWithSanitizer ( reflectionText , sanitizeReflectionSliceLines ) ;
284+ }
285+
286+ export function extractInjectableReflectionSlices ( reflectionText : string ) : ReflectionSlices {
287+ return extractReflectionSlicesWithSanitizer ( reflectionText , sanitizeInjectableReflectionLines ) ;
288+ }
289+
290+ function buildReflectionSliceItemsFromSlices ( slices : ReflectionSlices ) : ReflectionSliceItem [ ] {
245291 const invariantGroupSize = slices . invariants . length ;
246292 const derivedGroupSize = slices . derived . length ;
247293
@@ -262,3 +308,11 @@ export function extractReflectionSliceItems(reflectionText: string): ReflectionS
262308
263309 return [ ...invariantItems , ...derivedItems ] ;
264310}
311+
312+ export function extractReflectionSliceItems ( reflectionText : string ) : ReflectionSliceItem [ ] {
313+ return buildReflectionSliceItemsFromSlices ( extractReflectionSlices ( reflectionText ) ) ;
314+ }
315+
316+ export function extractInjectableReflectionSliceItems ( reflectionText : string ) : ReflectionSliceItem [ ] {
317+ return buildReflectionSliceItemsFromSlices ( extractInjectableReflectionSlices ( reflectionText ) ) ;
318+ }
0 commit comments