From 9ace6b7476879af84bb484849c2e1db4c1bc6fd0 Mon Sep 17 00:00:00 2001 From: davidmonterocrespo24 Date: Fri, 15 May 2026 03:38:34 +0200 Subject: [PATCH] fix(store): addBoard promotes itself to active when none is valid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit addBoard appended the new board to the boards[] array but never touched activeBoardId. The default INITIAL_BOARD_ID points at a board the picker injects on first load — but a fresh anonymous session (or a project that landed in a state without that initial board) can have activeBoardId pointing at nothing. When the agent does add_board('arduino-uno') → compile_sketch, step 2 then fails with "no active board on the canvas" and the model burns a turn on set_active_board. The fix: at addBoard time, if activeBoardId doesn't resolve to any existing board, promote the new board to active. If there IS a valid active board, leave it alone — manual placements of additional boards via the picker still keep focus on whatever the user was working on. --- frontend/src/store/useSimulatorStore.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/frontend/src/store/useSimulatorStore.ts b/frontend/src/store/useSimulatorStore.ts index 84d77368..e8a7ceba 100644 --- a/frontend/src/store/useSimulatorStore.ts +++ b/frontend/src/store/useSimulatorStore.ts @@ -995,7 +995,17 @@ export const useSimulatorStore = create((set, get) => { languageMode: 'arduino', }; - set((s) => ({ boards: [...s.boards, newBoard] })); + set((s) => { + // If there's no current active board (or the stored id doesn't point + // to one that exists), promote the new board to active. Without this, + // an agent that does add_board → compile_sketch fails on step 2 with + // "no active board on the canvas" and has to spend a turn on + // set_active_board. Manual placements via the UI already auto-active + // through the picker; this just closes the API gap. + const stillExists = s.boards.some((b) => b.id === s.activeBoardId); + const nextActive = stillExists ? s.activeBoardId : id; + return { boards: [...s.boards, newBoard], activeBoardId: nextActive }; + }); // Create the editor file group for this board useEditorStore.getState().createFileGroup(`group-${id}`); // Init VFS for Raspberry Pi 3 boards