@@ -137,3 +137,77 @@ test("applyHarnessReminder appends reminder as last message", async () => {
137137 assert . equal ( result [ result . length - 1 ] . customType , "gentle-harness-reminder" ) ;
138138 assert . equal ( result . length , 3 ) ;
139139} ) ;
140+
141+ test ( "buildContextReminder with compaction pending includes post-compaction block" , async ( ) => {
142+ __testing . setCompactionPending ( true ) ;
143+ const reminder = __testing . buildContextReminder ( ) ;
144+ assert . equal ( reminder . role , "custom" ) ;
145+ assert . equal ( reminder . customType , "gentle-harness-reminder" ) ;
146+ assert (
147+ reminder . content . includes ( "Context was just compacted" ) ,
148+ "should include post-compaction block when pending"
149+ ) ;
150+ assert (
151+ reminder . content . includes ( "Re-check current SDD/OpenSpec state on disk" ) ,
152+ "should include re-check instruction"
153+ ) ;
154+ assert (
155+ reminder . content . includes ( "restate scope and acceptance criteria" ) ,
156+ "should include restate instruction"
157+ ) ;
158+ // Verify flag is cleared after building
159+ assert . equal (
160+ __testing . getCompactionPending ( ) ,
161+ false ,
162+ "flag should be cleared after one-shot use"
163+ ) ;
164+ } ) ;
165+
166+ test ( "buildContextReminder without compaction pending returns standard reminder" , async ( ) => {
167+ __testing . setCompactionPending ( false ) ;
168+ const reminder = __testing . buildContextReminder ( ) ;
169+ const content = reminder . content ;
170+ assert ( ! content . includes ( "Context was just compacted" ) , "should not include post-compaction block when not pending" ) ;
171+ assert ( content . includes ( "Active discipline" ) , "should include standard discipline content" ) ;
172+ } ) ;
173+
174+ test ( "setCompactionPending and getCompactionPending control flag" , async ( ) => {
175+ __testing . setCompactionPending ( true ) ;
176+ assert . equal ( __testing . getCompactionPending ( ) , true ) ;
177+ __testing . setCompactionPending ( false ) ;
178+ assert . equal ( __testing . getCompactionPending ( ) , false ) ;
179+ } ) ;
180+
181+ test ( "applyHarnessReminder multi-turn scenario: reminder persists and refreshes correctly" , async ( ) => {
182+ // Simulate first context event
183+ __testing . setCompactionPending ( false ) ;
184+ let messages : any [ ] = [ { role : "user" , content : "first message" } ] ;
185+ let result = __testing . applyHarnessReminder ( messages ) ;
186+ assert . equal ( result [ result . length - 1 ] . customType , "gentle-harness-reminder" , "first turn should have reminder" ) ;
187+ const firstReminder = result [ result . length - 1 ] ;
188+
189+ // Simulate second context event with new message appended (tool result)
190+ messages = result ;
191+ messages . push ( { role : "tool" , content : "tool result" } ) ;
192+ result = __testing . applyHarnessReminder ( messages ) ;
193+
194+ // Should have exactly one reminder and it should be fresh (not the old one)
195+ const reminderCount = result . filter ( ( m : any ) => m . customType === "gentle-harness-reminder" ) . length ;
196+ assert . equal ( reminderCount , 1 , "should still have exactly one reminder after multi-turn" ) ;
197+ assert . equal ( result [ result . length - 1 ] . customType , "gentle-harness-reminder" , "reminder should be last" ) ;
198+ // Verify it's a fresh reminder (timestamp should be different)
199+ const secondReminder = result [ result . length - 1 ] ;
200+ assert ( secondReminder . timestamp >= firstReminder . timestamp , "fresh reminder should have equal or newer timestamp" ) ;
201+ } ) ;
202+
203+ test ( "compaction flag resets when applyHarnessReminder builds fresh reminder" , async ( ) => {
204+ __testing . setCompactionPending ( true ) ;
205+ assert . equal ( __testing . getCompactionPending ( ) , true , "flag should be set" ) ;
206+
207+ const messages : any [ ] = [ ] ;
208+ const result = __testing . applyHarnessReminder ( messages ) ;
209+
210+ // Flag should now be reset by buildContextReminder call
211+ assert . equal ( __testing . getCompactionPending ( ) , false , "flag should be reset after applyHarnessReminder" ) ;
212+ assert ( result [ 0 ] . content . includes ( "Context was just compacted" ) , "reminder should include compaction content" ) ;
213+ } ) ;
0 commit comments