@@ -213,6 +213,15 @@ describe("stripEnvelopeMetadata", () => {
213213 assert . doesNotMatch ( result , / S u b a g e n t C o n t e x t / ) ;
214214 } ) ;
215215
216+ it ( "preserves inline wrapper payload that only mentions boilerplate later in the sentence" , ( ) => {
217+ const input = [
218+ "[Subagent Context] User quoted the phrase Reply with a brief acknowledgment only. for documentation." ,
219+ ] . join ( "\n" ) ;
220+
221+ const result = stripEnvelopeMetadata ( input ) ;
222+ assert . equal ( result , "User quoted the phrase Reply with a brief acknowledgment only. for documentation." ) ;
223+ } ) ;
224+
216225 // FIX 2 regression: wrapper inline boilerplate should still be stripped
217226 it ( "strips boilerplate-only inline content after wrapper prefix" , ( ) => {
218227 const input = [
@@ -223,6 +232,24 @@ describe("stripEnvelopeMetadata", () => {
223232 assert . equal ( result , "" ) ;
224233 } ) ;
225234
235+ it ( "strips leading inline boilerplate but preserves payload that follows it" , ( ) => {
236+ const input = [
237+ "[Subagent Task] Reply with a brief acknowledgment only. Then summarize the failing test." ,
238+ ] . join ( "\n" ) ;
239+
240+ const result = stripEnvelopeMetadata ( input ) ;
241+ assert . equal ( result , "Then summarize the failing test." ) ;
242+ } ) ;
243+
244+ it ( "strips multiple leading boilerplate phrases before preserving inline payload" , ( ) => {
245+ const input = [
246+ "[Subagent Task] Reply with a brief acknowledgment only. Do not use any memory tools. Actual user content starts here." ,
247+ ] . join ( "\n" ) ;
248+
249+ const result = stripEnvelopeMetadata ( input ) ;
250+ assert . equal ( result , "Actual user content starts here." ) ;
251+ } ) ;
252+
226253 it ( "handles Telegram-style envelope headers" , ( ) => {
227254 const input = [
228255 "System: [2026-03-18 14:21:36 GMT+8] Telegram[bot123] DM | user_456 [msg:12345]" ,
@@ -249,6 +276,22 @@ describe("stripEnvelopeMetadata", () => {
249276 assert . doesNotMatch ( result , / m e s s a g e _ i d / ) ;
250277 } ) ;
251278
279+ it ( "strips standalone JSON blocks when sender_id appears before message_id" , ( ) => {
280+ const input = [
281+ "Some text before" ,
282+ "```json" ,
283+ '{"sender_id": "ou_yyy", "message_id": "om_xxx", "timestamp": "2026-03-18"}' ,
284+ "```" ,
285+ "Some text after" ,
286+ ] . join ( "\n" ) ;
287+
288+ const result = stripEnvelopeMetadata ( input ) ;
289+ assert . match ( result , / S o m e t e x t b e f o r e / ) ;
290+ assert . match ( result , / S o m e t e x t a f t e r / ) ;
291+ assert . doesNotMatch ( result , / m e s s a g e _ i d / ) ;
292+ assert . doesNotMatch ( result , / s e n d e r _ i d / ) ;
293+ } ) ;
294+
252295 it ( "collapses excessive blank lines after stripping" , ( ) => {
253296 const input = [
254297 "System: [2026-03-18 14:21:36 GMT+8] Feishu[default] DM | ou_xxx [msg:om_xxx]" ,
0 commit comments