State Machine Debugging for Multi-Room 3D Scanning Interactions
This article walks through a real-world debugging case in a multi-room spatial scanning application, where a seemingly linear capture flow broke down into a multi-state interaction problem. It covers four common failure patterns, a code-first workflow that uses AI coding tools to produce an interaction diagram before implementation, an explicit finite state machine with transition guards, an idempotency fix for a hidden retry defect that split rooms apart, background-sleep recovery strategies for cross-room relocalization, and a layered display approach to reduce visual noise on the floor plan result page. Readers will find a practical checklist for evaluating state completeness, guard conditions, retry idempotency, state persistence, and result-page readability.
1. Background: Why Continuous Multi-Room Scanning Breaks Down
In indoor spatial digitization workflows, users typically carry a handheld device and move continuously between rooms to complete a full chain: start scanning, scan, save a single room, relocalize across spaces, and generate a 2D floor plan. The chain looks linear, but it is in fact a multi-state, multi-branch state machine. If the conditions at any single step are not defined rigorously, the flaw gets amplified downstream into an obvious experience defect.
While testing a multi-room site, I captured five key screens in a row, which exposed four high-frequency problems:
- Incomplete results misjudged as success: a room with only about 0.3 square meters of fragmented data still received a green pass indicator, prompting the user to move on to the next room.
- Context loss caused by background sleep: the capture camera went to sleep near a doorway, and after recovery the system asked the user to go back to the original room, leaving the state inconsistent with the real position.
- Data fragmentation triggered by misoperation: a misjudged tap on a highlighted button created a separate new space that could not be stitched with the existing data.
- Information overload on the result page: more than twenty wall IDs were overlaid on the final floor plan, obscuring the core geometry.
Each issue looks minor on its own, but together they point to three main threads that were never straightened out: state definitions, transition conditions, and view noise reduction.
2. Methodology: Let the Model Read the Code and Propose a Plan First
Do not start editing code on a complex page right away. A safer workflow is to feed the project source code as context, together with on-site screenshots and a one-sentence verbal requirement, into an AI coding tool, and ask it to first output a problem breakdown and an interaction diagram. Only after a human review passes should the coding phase begin. This brings three benefits:
- The plan aligns with the code structure, avoiding designs that look good on a diagram but cannot land in the code.
- Defect localization happens earlier, reducing repeated rework.
- The diagram serves as a baseline for team communication and acceptance.
An interesting pattern emerged when comparing several mainstream AI coding tools (each set to the highest reasoning tier available in its client): for presentation-layer tasks such as page noise reduction and generating comparison images, the gap between tools was very small; but for deep logic debugging, three of the four top-tier configurations missed the hidden defect that “tapping retry splits the room apart”, and only one traced it out by following the code call chain. This shows that when complex interactions and state machines are involved, the decisive factor is how deeply the model understands the code’s real execution path.
3. State Machine Modeling: Making Implicit Transitions Explicit
Start by abstracting the business into a finite set of states, then define the transition table. An example:
IDLE -> SCANNING : user taps start scanningSCANNING -> ROOM_CANDIDATE : single-room scan endsROOM_CANDIDATE -> SAVED : area/completeness check passesROOM_CANDIDATE -> SCANNING : check fails, prompt to rescanSAVED -> RELOCALIZING : move across spaces, trigger relocalizationRELOCALIZING -> SAVED : relocalization succeeds, coordinate systems alignedRELOCALIZING -> IDLE : relocalization fails, fall back to a safe stateSAVED -> RESULT : user ends capture
The corresponding guard in pseudocode can be written as:
def on_scan_end(room):if room.area < MIN_AREA or room.coverage < MIN_COVERAGE:return transition("SCANNING", reason="INCOMPLETE")return transition("ROOM_CANDIDATE")
The key point: when validation fails, never enter the “saved” branch, which eliminates the “green checkmark nudging users to scan the next room” problem at the root.
4. Locating the Hidden Defect: Why the Retry Path Splits Rooms
“Retrying creates a new space” is a classic missing idempotency issue. Semantically, a retry should reuse the previous session context rather than reinitialize. The fix is to introduce a stable session identifier and an idempotency key:
def retry(session_id):ctx = store.get(session_id) # reuse existing contextif ctx is None:ctx = store.create() # create only on first attemptreturn resume(ctx, from_state="ROOM_CANDIDATE")
At the same time, give “create new space” and “retry” different entry points with a secondary confirmation to prevent accidental taps. This kind of defect is extremely hard to reproduce in manual testing; it can only be found by reasoning layer by layer along the code call chain, which is exactly where deep code understanding proves its value.
5. Cross-Space Relocalization and Sleep Fallback
Background camera sleep creates gaps in device pose and position data. Engineering-wise, three things should be done:
- Listen for foreground/background switch events and actively persist the current state before sleep.
- After recovery, first evaluate relocalization confidence; when confidence is insufficient, prompt the user to “realign” instead of blindly asking them to return to the original room.
- Set a timeout and failure fallback for coordinate system alignment, ensuring the state machine always converges to a safe state.
6. Result Page Noise Reduction: Layered Display on Demand
More than twenty wall IDs covering the floor plan is typical visual noise. A layer-grading strategy can be applied: show only the outline and area by default, and load IDs only when zooming or tapping; merge or collapse fragment walls whose area is too small; and provide an annotation toggle. This preserves information completeness without sacrificing readability.
7. Evaluation Takeaways and a Delivery Checklist
- Presentation-layer tasks are about “output speed”, while logic-layer tasks are about “depth of code understanding”; weight them differently by task type when selecting tools.
- For complex interactions, always produce a plan diagram before coding.
- Delivery checklist: is the state set complete, does every transition have a guard, is retry idempotent, is state persisted before sleep, and can the result page be denoised.
Solidifying this method noticeably improves both the stability and the experience of multi-room scanning applications.
