Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(devices): pet follow-ups after #1035 (#1036)

0xdiego25fb0cd5c3eada
diff --git a/desktop/lib/devices/pet_editor.dart b/desktop/lib/devices/pet_editor.dart
index 832422cc5..79d76570b 100644
--- a/desktop/lib/devices/pet_editor.dart
+++ b/desktop/lib/devices/pet_editor.dart
@@ -361,6 +361,10 @@ class PetEditor extends ChangeNotifier {
if (preview == null) error = controller.petError(deviceKey);
} on PetSourceError catch (e) {
error = e.message;
+ } catch (_) {
+ // An unreadable file, a corrupt zip entry, a full disk: the section must
+ // stay usable, so this ends the attempt like any other refusal.
+ error = 'Couldn’t read this pet';
}
if (_disposed || seq != _seq) {
await source?.cleanup();
@@ -395,6 +399,8 @@ class PetEditor extends ChangeNotifier {
if (_preview == null || _rows[state] == row) return;
_rows = {..._rows, state: row};
_current = false;
+ // A reply still on its way answers the choices before this one.
+ _seq++;
_notify();
_debounce?.cancel();
_debounce = Timer(petRowsDebounce, _refresh);
diff --git a/desktop/test/pet_settings_test.dart b/desktop/test/pet_settings_test.dart
index 80abffe4e..878db5681 100644
--- a/desktop/test/pet_settings_test.dart
+++ b/desktop/test/pet_settings_test.dart
@@ -994,6 +994,7 @@ void main() {
WidgetTester tester,
Map<String, int> cleaned, {
Future<PetSheet?> Function(String path)? loadSheet,
+ Future<PetSource> Function(String path)? resolveSource,
}) async {
final dial = DialState();
final controller = DevicesController(
@@ -1008,11 +1009,13 @@ void main() {
final editor = PetEditor(
controller: controller,
deviceKey: controller.devices.single.key,
- resolveSource: (path) async => PetSource(
- pngPath: path,
- name: 'boba',
- cleanup: () async => cleaned[path] = (cleaned[path] ?? 0) + 1,
- ),
+ resolveSource:
+ resolveSource ??
+ (path) async => PetSource(
+ pngPath: path,
+ name: 'boba',
+ cleanup: () async => cleaned[path] = (cleaned[path] ?? 0) + 1,
+ ),
loadSheet: loadSheet ?? (_) async => null,
);
return editor;
@@ -1096,6 +1099,55 @@ void main() {
pet.dispose();
});
+ testWidgets('a reply landing during the debounce keeps the newer choice', (
+ tester,
+ ) async {
+ daemon.previewReply = sheetPreview('boba', petdexRows);
+ final pet = await editor(tester, {});
+ await pet.use('/tmp/a.png');
+ final replies = <Completer<Map<String, dynamic>>>[];
+ daemon.onPreview = (_) {
+ final reply = Completer<Map<String, dynamic>>();
+ replies.add(reply);
+ return reply.future;
+ };
+ pet.choose('working', 'waving');
+ await tester.pump(const Duration(milliseconds: 300));
+ expect(replies.length, 1);
+ pet.choose('working', 'idle');
+ replies[0].complete(sheetPreview('older', petdexRows));
+ await tester.pump();
+ expect(pet.statesOf('idle'), contains('working'));
+ expect(pet.current, isFalse, reason: 'Apply waits for the newer choice');
+ await tester.pump(const Duration(milliseconds: 300));
+ expect(replies.length, 2);
+ expect(
+ (daemon.payloads['pet_preview']!.last['rows'] as Map)['working'],
+ 'idle',
+ );
+ replies[1].complete(sheetPreview('newest', petdexRows));
+ await tester.pump();
+ expect(pet.preview!.id, 'newest');
+ expect(pet.current, isTrue);
+ pet.dispose();
+ });
+
+ testWidgets('an unreadable file ends the attempt instead of sticking', (
+ tester,
+ ) async {
+ final pet = await editor(
+ tester,
+ {},
+ resolveSource: (path) async =>
+ throw FileSystemException('Permission denied', path),
+ );
+ await pet.use('/tmp/locked.png');
+ expect(pet.busy, isFalse);
+ expect(pet.editing, isFalse);
+ expect(pet.error, 'Couldn’t read this pet');
+ pet.dispose();
+ });
+
testWidgets('cancel, a new pick and dispose clean up the sheet', (
tester,
) async {
diff --git a/devices/harness-device/firmware/test/test_cable_json_parse.py b/devices/harness-device/firmware/test/test_cable_json_parse.py
index 9f3c50c73..8daf8e35f 100644
--- a/devices/harness-device/firmware/test/test_cable_json_parse.py
+++ b/devices/harness-device/firmware/test/test_cable_json_parse.py
@@ -28,7 +28,7 @@ code = r'''
#include <unistd.h>
static int64_t s_last_rx_us, clock_us = 123;
static uint32_t s_bad,s_unknown;
-static unsigned handled, alloc_calls, live, fail_at, fw_slices;
+static unsigned handled, alloc_calls, live, fail_at, fw_slices, pet_slices;
static int last_number;
static size_t allocated_bytes, peak_bytes;
typedef union { max_align_t alignment; size_t bytes; } allocation_header;
@@ -46,6 +46,7 @@ static void deallocate(void *p) {
}
static int64_t esp_timer_get_time(void) { return clock_us; }
static void fw_update_slice(const uint8_t *p,size_t n) { (void)p; (void)n; fw_slices++; }
+static void handle_pet_slice(const uint8_t *p,size_t n) { (void)p; (void)n; pet_slices++; }
static void handle_message(const cJSON *root) {
assert(cJSON_IsObject(root)); handled++;
const cJSON *v = cJSON_GetObjectItemCaseSensitive(root,"n");
@@ -135,6 +136,7 @@ int main(void) {
assert(!munmap(map,3*page));
on_frame(1,9,NULL,0,NULL);assert(s_unknown==1);
on_frame(1,CABLE_TYPE_FW,NULL,0,NULL);assert(fw_slices==1);
+ on_frame(1,CABLE_TYPE_PET,NULL,0,NULL);assert(pet_slices==1 && fw_slices==1 && !live);
puts("USB JSON: real cJSON, guarded/truncated frames, 100000 mutations, every allocation failure PASS");
}
'''