Proposal: make flua faster to troubleshoot for agents

Proposals are ordered by time saved, not by implementation cost. P1 and P2 are the big ones; P3–P5 are cheap and mostly docs.


P1 — Bridge error telemetry: full traceback + automatic payload dump

Why. The aliasing bug surfaced as the single line Callback error: lost reference in the QA log. The Python traceback (lua54.pyx frames + the failing flua call site) was invisible without patching bindings.py. Diagnosis required bisecting the payload by hand and pickling it for offline replay — several hours of monkey-patching that a flag would have collapsed into one run.

Spec.

Where. The table_from call sites in src/flua/bindings.py (api_call, api_hc3, device_for, …). Wrap once behind a helper so all sites share the behavior.

Agent value. Run once → load the dump → ddmin the structure → root cause. No manual instrumentation, no backups.

Acceptance criteria.


P2 — Byte-level websocket client trace

Why. The handshake bug ("connects, no events") was invisible from outside: flua's client buffered the auth_required frame that arrived in the same TCP segment as the HTTP 101, then waited for a recv that never came. The QA-side debug channel could not see the client's buffer or frame boundaries; diagnosis required reading websocket.py and building a custom test server.

Spec.

Where. src/flua/websocket.py, gated on the same debug-flag plumbing as --debug:http.

Acceptance criteria.


P3 — Sim-state introspection tool (flua --tool state)

Why. "Where did this object get aliased into the store?" required identity scans over state.devices and route replay via undocumented Python imports (flua.api.state.SimState, flua.api.request.ApiRequest, route functions). Agents will keep needing this entry point; it should be a tool, not folklore.

Spec.

Where. New tool under src/flua/tools/ (or extend an existing state tool); docs in USAGE.md.

Acceptance criteria.


P4 — Bridge-error cheatsheet in USAGE.md + flua-troubleshooting skill

Why. "lost reference" is opaque: the installed lupa ships no .pyx source, and the root cause required fetching lupa 2.6 from GitHub and reading py_to_lua_table. A mapping from lupa error → likely cause → flua invariant violated would have pointed at aliasing immediately.

Spec.

lupa error cause flua invariant
lost reference payload contains a shared (aliased) or cyclic structure; or a freed/lapsed lupa object crossed the bridge engine state and bridge data are JSON trees; no lupa proxies in engine data
cannot mix objects from different Lua runtimes object passed between runtimes one runtime per QA
TypeError: cannot convert … non-plain type reached table_from lua_to_python output is plain JSON types

Acceptance criteria.


P5 — Machine-readable bridge errors in --log json

Why. --log json already exists and is the agent-friendliest interface, but bridge errors arrive as a one-line message with no structured fields.

Spec.

Where. Logging layer for Lua callback errors; ties into P1.


P6 — Runtime tree-invariant check (--verify-tree)

Why. The aliasing bug would have been self-reporting at write time: plugin_update_view stored one list object into two state paths, and nothing noticed until table_from failed ~20 minutes later in QA terms. The fix (0.1.87) adds a copy.deepcopy at one site, but the class of bug is "state stopped being a JSON tree" — worth an invariant.

Spec.


Suggested rollout

  1. P1 — one helper in bindings.py, one flag; highest value per line.
  2. P4 — docs/skill only; makes P1 discoverable and self-serve.
  3. P2 — websocket trace; completes the "network bugs are visible" story next to the existing --debug:http.
  4. P3/P6 — the state tool and invariant; small, developer-facing.
  5. P5 — rides on P1; do together if touching the logging layer anyway.

Non-goals