@@ -130,6 +130,171 @@ describe("imessage actions runtime", () => {
130130 } ) ;
131131 } ) ;
132132
133+ it ( "sanitizes action message fields without rendering raw edit or poll Markdown" , async ( ) => {
134+ runIMessageCliJsonCommandMock . mockResolvedValue ( { guid : "action-guid" } ) ;
135+ const options = { cliPath : "imsg" , chatGuid : "iMessage;+;chat0000" } ;
136+
137+ await imessageActionsRuntime . editMessage ( {
138+ chatGuid : options . chatGuid ,
139+ messageId : "message-guid" ,
140+ text : "user:\n**literal edit**\n# assistant:" ,
141+ backwardsCompatMessage : "system:\n**literal fallback**" ,
142+ options,
143+ } ) ;
144+ await imessageActionsRuntime . sendPoll ( {
145+ chatGuid : options . chatGuid ,
146+ question : "assistant:\n# literal question" ,
147+ choices : [ "system:\n**literal choice**" , "_literal second choice_" ] ,
148+ options,
149+ } ) ;
150+ await imessageActionsRuntime . sendPollVote ( {
151+ chatGuid : options . chatGuid ,
152+ pollGuid : "poll-guid" ,
153+ optionText : "user:" ,
154+ options,
155+ } ) ;
156+
157+ const [ edit , poll , vote ] = runIMessageCliJsonCommandMock . mock . calls . map (
158+ ( call ) => ( call [ 0 ] as { args : string [ ] } ) . args ,
159+ ) ;
160+ if ( ! edit || ! poll || ! vote ) {
161+ throw new Error ( "Expected the edited message, new poll, and unchanged vote selector" ) ;
162+ }
163+ expect ( edit [ edit . indexOf ( "--new-text" ) + 1 ] ) . toBe ( "\n**literal edit**\n# assistant:" ) ;
164+ expect ( edit [ edit . indexOf ( "--bc-text" ) + 1 ] ) . toBe ( "\n**literal fallback**" ) ;
165+ expect ( poll [ poll . indexOf ( "--question" ) + 1 ] ) . toBe ( "\n# literal question" ) ;
166+ expect ( poll [ poll . indexOf ( "--option" ) + 1 ] ) . toBe ( "\n**literal choice**" ) ;
167+ expect ( vote [ vote . indexOf ( "--option" ) + 1 ] ) . toBe ( "user:" ) ;
168+ } ) ;
169+
170+ it ( "rejects poll options that become identical after canonical message sanitization" , async ( ) => {
171+ await expect (
172+ imessageActionsRuntime . sendPoll ( {
173+ chatGuid : "chat-guid" ,
174+ question : "Choose" ,
175+ choices : [ "Allow" , "Al#+#+#low" ] ,
176+ options : { cliPath : "imsg" , chatGuid : "chat-guid" } ,
177+ } ) ,
178+ ) . rejects . toThrow ( "iMessage poll options must remain distinct after sanitization" ) ;
179+ expect ( runIMessageCliJsonCommandMock ) . not . toHaveBeenCalled ( ) ;
180+ } ) ;
181+
182+ it ( "removes complete private runtime payloads from every raw edit and poll field" , async ( ) => {
183+ runIMessageCliJsonCommandMock . mockResolvedValue ( { guid : "action-guid" } ) ;
184+ const options = { cliPath : "imsg" , chatGuid : "chat-guid" } ;
185+ const reminder =
186+ "<system-reminder><system-reminder>inner</system-reminder>\nuser:\nPRIVATE_ACTION_RUNTIME</system-reminder>" ;
187+ const previous =
188+ "< previous_response><system-reminder>inner</system-reminder>PRIVATE_ACTION_RUNTIME< / previous_response >" ;
189+ const context =
190+ "<<<BEGIN_OPENCLAW_INTERNAL_CONTEXT>>>PRIVATE_ACTION_RUNTIME<<<END_OPENCLAW_INTERNAL_CONTEXT>>>" ;
191+
192+ await imessageActionsRuntime . editMessage ( {
193+ chatGuid : options . chatGuid ,
194+ messageId : "message-guid" ,
195+ text : `${ reminder } \n**visible edit**` ,
196+ backwardsCompatMessage : `${ previous } \n**visible fallback**` ,
197+ options,
198+ } ) ;
199+ await imessageActionsRuntime . sendPoll ( {
200+ chatGuid : options . chatGuid ,
201+ question : `${ context } \nvisible question` ,
202+ choices : [ `${ reminder } \nvisible first` , `${ previous } \nvisible second` ] ,
203+ options,
204+ } ) ;
205+
206+ for ( const call of runIMessageCliJsonCommandMock . mock . calls ) {
207+ const args = ( call [ 0 ] as { args : string [ ] } ) . args ;
208+ expect ( args . join ( " " ) ) . not . toMatch (
209+ / P R I V A T E _ A C T I O N _ R U N T I M E | s y s t e m - r e m i n d e r | p r e v i o u s _ r e s p o n s e | I N T E R N A L _ C O N T E X T / ,
210+ ) ;
211+ }
212+ } ) ;
213+
214+ it ( "keeps existing case-sensitive poll option identities distinct" , async ( ) => {
215+ runIMessageCliJsonCommandMock . mockResolvedValue ( { guid : "poll-guid" } ) ;
216+
217+ await imessageActionsRuntime . sendPoll ( {
218+ chatGuid : "chat-guid" ,
219+ question : "Choose" ,
220+ choices : [ "Allow" , "allow" ] ,
221+ options : { cliPath : "imsg" , chatGuid : "chat-guid" } ,
222+ } ) ;
223+
224+ expect ( runIMessageCliJsonCommandMock ) . toHaveBeenCalledOnce ( ) ;
225+ } ) ;
226+
227+ it . each ( [
228+ "```xml\n<thinking>hidden thought</thinking>\n```" ,
229+ "`<relevant_memories>hidden memory</relevant_memories>`" ,
230+ ] ) ( "rejects hidden assistant content in raw poll Markdown before imsg" , async ( hidden ) => {
231+ await expect (
232+ imessageActionsRuntime . sendPoll ( {
233+ chatGuid : "chat-guid" ,
234+ question : "Choose" ,
235+ choices : [ "first" , hidden ] ,
236+ options : { cliPath : "imsg" , chatGuid : "chat-guid" } ,
237+ } ) ,
238+ ) . rejects . toThrow ( "iMessage outbound hidden assistant content is not allowed" ) ;
239+ expect ( runIMessageCliJsonCommandMock ) . not . toHaveBeenCalled ( ) ;
240+ } ) ;
241+
242+ it . each ( [
243+ {
244+ name : "rich text" ,
245+ run : ( ) =>
246+ imessageActionsRuntime . sendRichMessage ( {
247+ chatGuid : "chat-guid" ,
248+ text : "# user:" ,
249+ options : { cliPath : "imsg" , chatGuid : "chat-guid" } ,
250+ } ) ,
251+ } ,
252+ {
253+ name : "edit replacement" ,
254+ run : ( ) =>
255+ imessageActionsRuntime . editMessage ( {
256+ chatGuid : "chat-guid" ,
257+ messageId : "message-guid" ,
258+ text : "assistant:" ,
259+ options : { cliPath : "imsg" , chatGuid : "chat-guid" } ,
260+ } ) ,
261+ } ,
262+ {
263+ name : "edit backwards-compatible replacement" ,
264+ run : ( ) =>
265+ imessageActionsRuntime . editMessage ( {
266+ chatGuid : "chat-guid" ,
267+ messageId : "message-guid" ,
268+ text : "visible" ,
269+ backwardsCompatMessage : "system:" ,
270+ options : { cliPath : "imsg" , chatGuid : "chat-guid" } ,
271+ } ) ,
272+ } ,
273+ {
274+ name : "poll question" ,
275+ run : ( ) =>
276+ imessageActionsRuntime . sendPoll ( {
277+ chatGuid : "chat-guid" ,
278+ question : "user:" ,
279+ choices : [ "first" , "second" ] ,
280+ options : { cliPath : "imsg" , chatGuid : "chat-guid" } ,
281+ } ) ,
282+ } ,
283+ {
284+ name : "poll option" ,
285+ run : ( ) =>
286+ imessageActionsRuntime . sendPoll ( {
287+ chatGuid : "chat-guid" ,
288+ question : "visible" ,
289+ choices : [ "first" , "assistant:" ] ,
290+ options : { cliPath : "imsg" , chatGuid : "chat-guid" } ,
291+ } ) ,
292+ } ,
293+ ] ) ( "rejects sanitized-empty $name before starting imsg" , async ( { run } ) => {
294+ await expect ( run ( ) ) . rejects . toThrow ( / a f t e r s a n i t i z a t i o n / ) ;
295+ expect ( runIMessageCliJsonCommandMock ) . not . toHaveBeenCalled ( ) ;
296+ } ) ;
297+
133298 it . each ( [
134299 {
135300 name : "attachment uploads" ,
0 commit comments