Commit a871e67
feat(playground): implement task cancellation for Android/iOS playgrounds (#1355)
* feat(playground): implement task cancellation for Android/iOS playgrounds
This PR implements task cancellation functionality for Android and iOS
playgrounds using a singleton + recreation pattern.
When users clicked the "Stop" button in Android/iOS playground, the task
continued to execute and control the device via ADB commands. This was
because:
- Agent instances were global singletons created at server startup
- The /cancel endpoint only deleted progress tips without stopping execution
- There was no mechanism to interrupt ongoing tasks
Implemented a singleton + recreation pattern:
- PlaygroundServer now accepts factory functions instead of instances
- Added task locking mechanism (currentTaskId) to prevent concurrent tasks
- When cancel is triggered, the agent is destroyed and recreated
- Device operations stop immediately as destroyed agents reject new commands
1. **PlaygroundServer** (packages/playground/src/server.ts)
- Added factory function support for page and agent creation
- Added `recreateAgent()` method to destroy and recreate agent
- Added `currentTaskId` to track running tasks
- Enhanced `/execute` endpoint with task conflict detection
- Enhanced `/cancel` endpoint to recreate agent on cancellation
- Backward compatible with existing instance-based usage
2. **Android Playground** (packages/android-playground/src/bin.ts)
- Updated to use factory pattern for server creation
- Each recreation creates fresh AndroidDevice and AndroidAgent instances
3. **iOS Playground** (packages/ios/src/bin.ts)
- Updated to use factory pattern for server creation
- Each recreation creates fresh IOSDevice and IOSAgent instances
- Added test script `test-cancel-android.sh` for automated testing
- Manual testing confirmed device operations stop when cancel is triggered
```
User clicks Stop
↓
Frontend calls /cancel/:requestId
↓
Server checks if current running task
↓
Call recreateAgent()
├─ Destroy old agent (agent.destroy())
├─ Destroy old device (device.destroy())
├─ Create new device (pageFactory())
└─ Create new agent (agentFactory(device))
↓
Clear task lock and progress tips
↓
Device stops operations ✅
```
- ✅ Simple implementation (minimal code changes)
- ✅ Effective cancellation (destroy() immediately sets destroyed flag)
- ✅ Backward compatible (still accepts instances)
- ✅ Natural serialization (one task at a time per device)
```bash
pnpm run android:playground
./test-cancel-android.sh
```
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* fix(page): ensure keyboard actions return promises for better async handling
* refactor(playground): update PlaygroundServer to use agent factories and simplify server creation
* fix(ios): round coordinates for tap and swipe actions to improve accuracy
* fix(android): round coordinates in scrolling and gesture methods for improved accuracy
* refactor(playground): simplify PlaygroundServer instantiation and improve code readability
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent ba3849a commit a871e67
File tree
10 files changed
+226
-102
lines changed- packages
- android-playground/src
- android/src
- ios/src
- playground/src
- web-integration
- src
- puppeteer
- tests
- ai/web/static
- unit-test
10 files changed
+226
-102
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
127 | 132 | | |
128 | | - | |
129 | 133 | | |
130 | 134 | | |
131 | 135 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
882 | 882 | | |
883 | 883 | | |
884 | 884 | | |
885 | | - | |
| 885 | + | |
886 | 886 | | |
887 | 887 | | |
888 | 888 | | |
889 | 889 | | |
890 | | - | |
| 890 | + | |
| 891 | + | |
891 | 892 | | |
892 | | - | |
| 893 | + | |
893 | 894 | | |
894 | 895 | | |
895 | 896 | | |
896 | 897 | | |
897 | | - | |
| 898 | + | |
| 899 | + | |
898 | 900 | | |
899 | 901 | | |
900 | 902 | | |
| |||
1025 | 1027 | | |
1026 | 1028 | | |
1027 | 1029 | | |
1028 | | - | |
1029 | | - | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
1030 | 1035 | | |
1031 | 1036 | | |
1032 | 1037 | | |
| |||
1043 | 1048 | | |
1044 | 1049 | | |
1045 | 1050 | | |
1046 | | - | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
1047 | 1055 | | |
1048 | 1056 | | |
1049 | 1057 | | |
| |||
1062 | 1070 | | |
1063 | 1071 | | |
1064 | 1072 | | |
1065 | | - | |
1066 | | - | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
1067 | 1078 | | |
1068 | 1079 | | |
1069 | 1080 | | |
| |||
1080 | 1091 | | |
1081 | 1092 | | |
1082 | 1093 | | |
1083 | | - | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
1084 | 1098 | | |
1085 | 1099 | | |
1086 | 1100 | | |
| |||
1098 | 1112 | | |
1099 | 1113 | | |
1100 | 1114 | | |
1101 | | - | |
| 1115 | + | |
1102 | 1116 | | |
1103 | 1117 | | |
1104 | | - | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
1105 | 1122 | | |
1106 | 1123 | | |
1107 | 1124 | | |
| |||
1118 | 1135 | | |
1119 | 1136 | | |
1120 | 1137 | | |
1121 | | - | |
| 1138 | + | |
1122 | 1139 | | |
1123 | 1140 | | |
1124 | | - | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
1125 | 1145 | | |
1126 | 1146 | | |
1127 | 1147 | | |
| |||
1138 | 1158 | | |
1139 | 1159 | | |
1140 | 1160 | | |
1141 | | - | |
| 1161 | + | |
1142 | 1162 | | |
1143 | 1163 | | |
1144 | | - | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
1145 | 1168 | | |
1146 | 1169 | | |
1147 | 1170 | | |
| |||
1158 | 1181 | | |
1159 | 1182 | | |
1160 | 1183 | | |
1161 | | - | |
| 1184 | + | |
1162 | 1185 | | |
1163 | 1186 | | |
1164 | | - | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
1165 | 1191 | | |
1166 | 1192 | | |
1167 | 1193 | | |
| |||
1343 | 1369 | | |
1344 | 1370 | | |
1345 | 1371 | | |
1346 | | - | |
1347 | | - | |
| 1372 | + | |
| 1373 | + | |
1348 | 1374 | | |
1349 | 1375 | | |
1350 | 1376 | | |
1351 | | - | |
| 1377 | + | |
1352 | 1378 | | |
1353 | | - | |
| 1379 | + | |
1354 | 1380 | | |
1355 | 1381 | | |
1356 | 1382 | | |
| |||
1360 | 1386 | | |
1361 | 1387 | | |
1362 | 1388 | | |
1363 | | - | |
1364 | | - | |
| 1389 | + | |
| 1390 | + | |
1365 | 1391 | | |
1366 | 1392 | | |
1367 | 1393 | | |
| |||
1435 | 1461 | | |
1436 | 1462 | | |
1437 | 1463 | | |
1438 | | - | |
1439 | | - | |
| 1464 | + | |
| 1465 | + | |
1440 | 1466 | | |
1441 | 1467 | | |
1442 | | - | |
| 1468 | + | |
1443 | 1469 | | |
1444 | 1470 | | |
1445 | 1471 | | |
| |||
1473 | 1499 | | |
1474 | 1500 | | |
1475 | 1501 | | |
1476 | | - | |
1477 | | - | |
| 1502 | + | |
| 1503 | + | |
1478 | 1504 | | |
1479 | 1505 | | |
1480 | | - | |
| 1506 | + | |
1481 | 1507 | | |
1482 | 1508 | | |
1483 | 1509 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
90 | | - | |
91 | 89 | | |
92 | 90 | | |
93 | 91 | | |
94 | 92 | | |
95 | 93 | | |
96 | 94 | | |
97 | | - | |
| 95 | + | |
98 | 96 | | |
99 | 97 | | |
100 | 98 | | |
| |||
105 | 103 | | |
106 | 104 | | |
107 | 105 | | |
108 | | - | |
109 | 106 | | |
110 | 107 | | |
111 | 108 | | |
| |||
165 | 162 | | |
166 | 163 | | |
167 | 164 | | |
168 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
169 | 177 | | |
170 | 178 | | |
171 | 179 | | |
| |||
0 commit comments