@@ -150,6 +150,88 @@ Read these files.
150150 expect ( result ) . toContain ( "Read these files" ) ;
151151 } ) ;
152152
153+ it ( "matches H1 headings (top-level section)" , async ( ) => {
154+ // Regression: extractSections previously used /^(#{2,3})/ and silently
155+ // skipped H1 headings, causing post-compaction injection to fail when users
156+ // wrote "# Session Startup" instead of "## Session Startup".
157+ const content = `# Session Startup
158+
159+ Read WORKFLOW_AUTO.md first.
160+
161+ # Other Section
162+
163+ Not relevant.
164+ ` ;
165+ fs . writeFileSync ( path . join ( tmpDir , "AGENTS.md" ) , content ) ;
166+ const result = await readPostCompactionContext ( tmpDir ) ;
167+ expect ( result ) . not . toBeNull ( ) ;
168+ expect ( result ) . toContain ( "Session Startup" ) ;
169+ expect ( result ) . toContain ( "WORKFLOW_AUTO.md" ) ;
170+ expect ( result ) . not . toContain ( "Other Section" ) ;
171+ } ) ;
172+
173+ it ( "matches H4 headings" , async ( ) => {
174+ const content = `#### Session Startup
175+
176+ H4 startup instructions.
177+
178+ #### Other
179+ ` ;
180+ fs . writeFileSync ( path . join ( tmpDir , "AGENTS.md" ) , content ) ;
181+ const result = await readPostCompactionContext ( tmpDir ) ;
182+ expect ( result ) . not . toBeNull ( ) ;
183+ expect ( result ) . toContain ( "H4 startup instructions" ) ;
184+ } ) ;
185+
186+ it ( "H1 section is terminated by the next H1" , async ( ) => {
187+ // A same-level H1 should terminate the current H1 section.
188+ const content = `# Session Startup
189+
190+ Critical startup content.
191+
192+ # Red Lines
193+
194+ Do not break rules.
195+
196+ # Unrelated
197+
198+ Ignore this.
199+ ` ;
200+ fs . writeFileSync ( path . join ( tmpDir , "AGENTS.md" ) , content ) ;
201+ const result = await readPostCompactionContext ( tmpDir ) ;
202+ expect ( result ) . not . toBeNull ( ) ;
203+ expect ( result ) . toContain ( "Critical startup content" ) ;
204+ expect ( result ) . toContain ( "Do not break rules" ) ;
205+ expect ( result ) . not . toContain ( "Unrelated" ) ;
206+ } ) ;
207+
208+ it ( "H1 section includes lower-level sub-headings (H2/H3)" , async ( ) => {
209+ // Sub-headings inside an H1 section must be captured as part of the section.
210+ const content = `# Session Startup
211+
212+ ## Step 1
213+
214+ Do step one.
215+
216+ ### Step 1a
217+
218+ Detail for step 1a.
219+
220+ ## Step 2
221+
222+ Do step two.
223+
224+ # Other
225+ ` ;
226+ fs . writeFileSync ( path . join ( tmpDir , "AGENTS.md" ) , content ) ;
227+ const result = await readPostCompactionContext ( tmpDir ) ;
228+ expect ( result ) . not . toBeNull ( ) ;
229+ expect ( result ) . toContain ( "Step 1" ) ;
230+ expect ( result ) . toContain ( "Step 1a" ) ;
231+ expect ( result ) . toContain ( "Step 2" ) ;
232+ expect ( result ) . not . toContain ( "Other" ) ;
233+ } ) ;
234+
153235 it ( "skips sections inside code blocks" , async ( ) => {
154236 const content = `# Rules
155237
0 commit comments