fix(store): addBoard promotes itself to active when none is valid

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.
This commit is contained in:
davidmonterocrespo24 2026-05-15 03:38:34 +02:00
parent f0953fcf45
commit 9ace6b7476
1 changed files with 11 additions and 1 deletions

View File

@ -995,7 +995,17 @@ export const useSimulatorStore = create<SimulatorState>((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