@@ -50,11 +50,18 @@ static int cte_codex_edit(const char *path, cbm_toml_codex_hook_action_t action,
5050 CTE_CODEX_COMMAND , action , check_only );
5151}
5252
53+ static int cte_codex_edit_commands_detailed (const char * path , const char * command ,
54+ const char * command_windows ,
55+ cbm_toml_codex_hook_action_t action , int check_only ,
56+ cbm_toml_codex_hook_failure_t * failure ) {
57+ return cbm_toml_reconcile_codex_hooks_detailed (path , CTE_CODEX_BEGIN , CTE_CODEX_END , command ,
58+ command_windows , action , check_only , failure );
59+ }
60+
5361static int cte_codex_edit_detailed (const char * path , cbm_toml_codex_hook_action_t action ,
5462 int check_only , cbm_toml_codex_hook_failure_t * failure ) {
55- return cbm_toml_reconcile_codex_hooks_detailed (path , CTE_CODEX_BEGIN , CTE_CODEX_END ,
56- CTE_CODEX_COMMAND , CTE_CODEX_COMMAND , action ,
57- check_only , failure );
63+ return cte_codex_edit_commands_detailed (path , CTE_CODEX_COMMAND , CTE_CODEX_COMMAND , action ,
64+ check_only , failure );
5865}
5966
6067static int cte_fixture (char * dir , size_t dir_size , char * path , size_t path_size ) {
@@ -110,6 +117,25 @@ static int cte_occurrences(const char *text, const char *needle) {
110117 return count ;
111118}
112119
120+ static int cte_replace_once (const char * source , const char * needle , const char * replacement ,
121+ char * output , size_t output_size ) {
122+ const char * match = strstr (source , needle );
123+ if (!match ) {
124+ return -1 ;
125+ }
126+ size_t prefix_len = (size_t )(match - source );
127+ size_t needle_len = strlen (needle );
128+ size_t replacement_len = strlen (replacement );
129+ size_t suffix_len = strlen (match + needle_len );
130+ if (prefix_len + replacement_len + suffix_len >= output_size ) {
131+ return -1 ;
132+ }
133+ memcpy (output , source , prefix_len );
134+ memcpy (output + prefix_len , replacement , replacement_len );
135+ memcpy (output + prefix_len + replacement_len , match + needle_len , suffix_len + 1U );
136+ return 0 ;
137+ }
138+
113139static size_t cte_temp_count (const char * dir ) {
114140 cbm_dir_t * directory = cbm_opendir (dir );
115141 if (!directory ) {
@@ -1106,6 +1132,120 @@ TEST(config_toml_codex_reconciles_minimal_owned_forms) {
11061132 PASS ();
11071133}
11081134
1135+ /* #1633: keep this fixture literal. It is the managed AOT block written by the
1136+ * v0.10.2 release, with the release's POSIX and PowerShell command builders. */
1137+ TEST (config_toml_codex_accepts_v0102_managed_windows_crlf ) {
1138+ static const char * command =
1139+ "'C:\\Users\\Example\\AppData\\Local\\Programs\\codebase-memory-mcp\\"
1140+ "codebase-memory-mcp.exe' hook-augment" ;
1141+ static const char * command_windows =
1142+ "& 'C:\\Users\\Example\\AppData\\Local\\Programs\\codebase-memory-mcp\\"
1143+ "codebase-memory-mcp.exe' hook-augment" ;
1144+ static const char * original =
1145+ "\xEF\xBB\xBF[mcp_servers.other]\r\n"
1146+ "command = \"other\"\r\n"
1147+ "keep = true\r\n"
1148+ "# >>> codebase-memory-mcp SessionStart >>>\r\n"
1149+ "[[hooks.SessionStart]]\r\n"
1150+ "matcher = \"startup|resume|clear|compact\"\r\n"
1151+ "\r\n"
1152+ "[[hooks.SessionStart.hooks]]\r\n"
1153+ "type = \"command\"\r\n"
1154+ "command = \"'C:\\\\Users\\\\Example\\\\AppData\\\\Local\\\\Programs\\\\"
1155+ "codebase-memory-mcp\\\\codebase-memory-mcp.exe' hook-augment\"\r\n"
1156+ "command_windows = \"& 'C:\\\\Users\\\\Example\\\\AppData\\\\Local\\\\Programs\\\\"
1157+ "codebase-memory-mcp\\\\codebase-memory-mcp.exe' hook-augment\"\r\n"
1158+ "timeout = 5\r\n"
1159+ "\r\n"
1160+ "[[hooks.SubagentStart]]\r\n"
1161+ "matcher = \"*\"\r\n"
1162+ "\r\n"
1163+ "[[hooks.SubagentStart.hooks]]\r\n"
1164+ "type = \"command\"\r\n"
1165+ "command = \"'C:\\\\Users\\\\Example\\\\AppData\\\\Local\\\\Programs\\\\"
1166+ "codebase-memory-mcp\\\\codebase-memory-mcp.exe' hook-augment\"\r\n"
1167+ "command_windows = \"& 'C:\\\\Users\\\\Example\\\\AppData\\\\Local\\\\Programs\\\\"
1168+ "codebase-memory-mcp\\\\codebase-memory-mcp.exe' hook-augment\"\r\n"
1169+ "timeout = 5\r\n"
1170+ "# <<< codebase-memory-mcp SessionStart <<<\r\n" ;
1171+ char dir [CTE_PATH_CAP ];
1172+ char path [CTE_PATH_CAP ];
1173+ char before [CTE_FILE_CAP ];
1174+ char actual [CTE_FILE_CAP ];
1175+ char invalid [2 ][CTE_FILE_CAP ];
1176+ ASSERT_EQ (cte_fixture (dir , sizeof (dir ), path , sizeof (path )), 0 );
1177+ ASSERT_EQ (th_write_file (path , original ), 0 );
1178+ ASSERT_EQ (cte_read (path , before , sizeof (before )), 0 );
1179+
1180+ cbm_toml_codex_hook_failure_t failure = CBM_TOML_CODEX_HOOK_FAILURE_INVALID_ARGUMENT ;
1181+ int reconcile_rc = cte_codex_edit_commands_detailed (path , command , command_windows ,
1182+ CBM_TOML_CODEX_HOOK_UPSERT , 1 , & failure );
1183+ ASSERT_EQ (failure , CBM_TOML_CODEX_HOOK_FAILURE_NONE );
1184+ ASSERT_EQ (reconcile_rc , 0 );
1185+ ASSERT_EQ (cte_read (path , actual , sizeof (actual )), 0 );
1186+ ASSERT_STR_EQ (actual , before );
1187+
1188+ ASSERT_EQ (cte_codex_edit_commands_detailed (path , command , command_windows ,
1189+ CBM_TOML_CODEX_HOOK_UPSERT , 0 , & failure ),
1190+ 0 );
1191+ ASSERT_EQ (cte_read (path , actual , sizeof (actual )), 0 );
1192+ ASSERT_STR_EQ (actual , before );
1193+ ASSERT_EQ (cte_occurrences (actual , CTE_CODEX_BEGIN ), 1 );
1194+ ASSERT_EQ (cte_occurrences (actual , "[[hooks.SessionStart]]" ), 1 );
1195+ ASSERT_EQ (cte_occurrences (actual , "[[hooks.SubagentStart]]" ), 1 );
1196+ ASSERT_NOT_NULL (strstr (actual , "[mcp_servers.other]" ));
1197+ ASSERT_NOT_NULL (strstr (actual , "keep = true" ));
1198+ for (const char * cursor = actual ; * cursor ; ++ cursor ) {
1199+ if (* cursor == '\n' ) {
1200+ ASSERT (cursor > actual && cursor [-1 ] == '\r' );
1201+ }
1202+ }
1203+
1204+ ASSERT_EQ (cte_codex_edit_commands_detailed (path , command , command_windows ,
1205+ CBM_TOML_CODEX_HOOK_UPSERT , 0 , & failure ),
1206+ 0 );
1207+ ASSERT_EQ (cte_read (path , actual , sizeof (actual )), 0 );
1208+ ASSERT_STR_EQ (actual , before );
1209+
1210+ ASSERT_EQ (cte_codex_edit_commands_detailed (path , command , command_windows ,
1211+ CBM_TOML_CODEX_HOOK_REMOVE , 0 , & failure ),
1212+ 0 );
1213+ ASSERT_EQ (cte_read (path , actual , sizeof (actual )), 0 );
1214+ ASSERT_NOT_NULL (strstr (actual , "[mcp_servers.other]" ));
1215+ ASSERT_NOT_NULL (strstr (actual , "keep = true" ));
1216+ ASSERT_NULL (strstr (actual , CTE_CODEX_BEGIN ));
1217+ ASSERT_NULL (strstr (actual , "hook-augment" ));
1218+
1219+ static const char * owned_line =
1220+ "command = \"'C:\\\\Users\\\\Example\\\\AppData\\\\Local\\\\Programs\\\\"
1221+ "codebase-memory-mcp\\\\codebase-memory-mcp.exe' hook-augment\"\r\n" ;
1222+ ASSERT_EQ (cte_replace_once (original , owned_line , "command = \"foreign\"\r\n" , invalid [0 ],
1223+ sizeof (invalid [0 ])),
1224+ 0 );
1225+ ASSERT_EQ (cte_replace_once (original , "type = \"command\"\r\n" ,
1226+ "type = \"command\"\r\nenabled = true\r\n" , invalid [1 ],
1227+ sizeof (invalid [1 ])),
1228+ 0 );
1229+ for (size_t i = 0U ; i < sizeof (invalid ) / sizeof (invalid [0 ]); ++ i ) {
1230+ ASSERT_EQ (th_write_file (path , invalid [i ]), 0 );
1231+ failure = CBM_TOML_CODEX_HOOK_FAILURE_NONE ;
1232+ ASSERT_EQ (cte_codex_edit_commands_detailed (path , command , command_windows ,
1233+ CBM_TOML_CODEX_HOOK_UPSERT , 1 , & failure ),
1234+ -1 );
1235+ ASSERT_EQ (failure , CBM_TOML_CODEX_HOOK_FAILURE_AMBIGUOUS_OWNERSHIP );
1236+ ASSERT_EQ (cte_read (path , actual , sizeof (actual )), 0 );
1237+ ASSERT_STR_EQ (actual , invalid [i ]);
1238+ ASSERT_EQ (cte_codex_edit_commands_detailed (path , command , command_windows ,
1239+ CBM_TOML_CODEX_HOOK_UPSERT , 0 , & failure ),
1240+ -1 );
1241+ ASSERT_EQ (failure , CBM_TOML_CODEX_HOOK_FAILURE_AMBIGUOUS_OWNERSHIP );
1242+ ASSERT_EQ (cte_read (path , actual , sizeof (actual )), 0 );
1243+ ASSERT_STR_EQ (actual , invalid [i ]);
1244+ }
1245+ th_cleanup (dir );
1246+ PASS ();
1247+ }
1248+
11091249TEST (config_toml_codex_rejects_ambiguous_inline_byte_identically ) {
11101250 char dir [CTE_PATH_CAP ];
11111251 char path [CTE_PATH_CAP ];
@@ -1301,6 +1441,7 @@ SUITE(config_toml_edit) {
13011441 RUN_TEST (config_toml_vibe_ambiguous_target_fail_closed );
13021442 RUN_TEST (config_toml_target_table_rejects_significant_nonassignments_byte_identically );
13031443 RUN_TEST (config_toml_codex_reconciles_minimal_owned_forms );
1444+ RUN_TEST (config_toml_codex_accepts_v0102_managed_windows_crlf );
13041445 RUN_TEST (config_toml_codex_rejects_ambiguous_inline_byte_identically );
13051446 RUN_TEST (config_toml_codex_reports_stable_failure_reasons );
13061447 RUN_TEST (config_toml_codex_preserves_bom_crlf_and_foreign_aot );
0 commit comments