Skip to content

feat: add midi-drums device-access layer - #10

Merged
cchacs merged 4 commits into
mainfrom
pullfrog/3-midi-input-device-access
Oct 5, 2026
Merged

cchacs merged 4 commits into
mainfrom
pullfrog/3-midi-input-device-access

Conversation

@pullfrog

@pullfrog pullfrog Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Implements #3 (part of #2): the MIDI drum controller device-access layer.

The plugin scans for input devices through the Host's window.feedBack.midiInput capability, opens several at once, and merges their note-ons into a single stream tagged by logicalSourceKey — the foundation the calibration and kit-profile sub-issues build on.

What it does

  • Device list screen with per-device tick, a rescan control, a saved-device status line, and a bounded hit log.
  • window.midiDrumsDevices — getState, subscribe, discover, setEnabled, setActive, onHit, dispose.
  • Merged stream — onHit yields {logicalSourceKey, note, velocity, channel, at} across every open source, so a consumer never tracks connections itself.
  • Persistence — ticked keys saved server-side via routes.py at /api/plugins/midi-drums/settings.

Decisions worth flagging

  • Availability is gated on the domain, not the browser. The check is mi && mi.version === 1, never navigator.requestMIDIAccess. Gating on the browser API would hide a usable non-Web-MIDI provider (a native adapter) before discover() had ever run, and would claim a Host was capable when its domain is absent.
  • Only declared verbs are called. The manifest declares exactly the five verbs listed above; getSelected() is treated as an optional optimisation, since a Host may implement only what is declared.
  • discover() is the permission boundary, invoked only once the screen is active so the prompt never appears on someone else's screen. A denied result is recorded but never latched — Rescan retries it.
  • Ticks are keys, not labels, and never silently substituted. A device that is unplugged keeps its tick and reopens when it returns; a stale or renamed device must not quietly become a different box's input.
  • Opens and closes for one source are serialised. The domain refcounts an open session by requester name: two concurrent opens add a single reference, while two closes would remove it twice and tear the session out from under whichever open survived. Each source runs its work through a promise chain, and a superseded open releases the reference it just took.
  • Device labels use textContent. A MIDI device name is user-controlled hardware text and is never parsed as markup.

Verification

The repo has no build, lint, or test tooling, so this was verified with throwaway harnesses outside the tree (jsdom + a fake MIDI domain, FastAPI + httpx against routes.py):

  • 89 screen checks: fail-soft with no domain, late domain arrival, denial then retry, two simultaneous sources with tagged/merged hits, note-on filtering (note-off, zero-velocity strikes and other channel traffic ignored), replug with no fallback-switch, close/reopen across screen changes, no discovery while inactive, rescan picking up a later device, idempotent re-hydration, source cap and its visible rejection, bounded hit log, settings failure, navigating away mid-save, a Host without getSelected(), and a bus that appears after the script runs.
  • 27 route checks: defaults, every validation rejection, the 16 KiB cap, persistence, corrupt-file recovery, and salvaging a hand-edited config key-by-key instead of discarding the whole selection.

node --check and py_compile pass. The plugin has not been exercised against a real Host build with hardware attached — that manual pass is still outstanding.

Closes the device-access portion of #2. Settings UI and kit profiles are separate sub-issues and are not in scope here.

🤖 Generated with Pullfrog

Pullfrog  | View workflow run | via Pullfrog | Using big-pickle (free) | 𝕏

Scans for MIDI input devices through the Host's window.feedBack.midiInput
capability, opens several at once, and merges their note-ons into one stream
tagged by logicalSourceKey. Exposes window.midiDrumsDevices and persists the
ticked selection server-side.

Notes on a few decisions:

- Availability is gated on the midi-input domain being present at version 1,
  not on navigator.requestMIDIAccess. A missing or mid-version domain is a
  no-op rather than an error.
- discover() is the permission boundary and runs only once the screen is
  active, so the prompt never appears on someone else's screen. A denied
  result is recorded but never latched, so Rescan can retry it.
- Ticked keys are stored, not labels. A missing device keeps its tick and
  reopens when it returns; no substitute is ever switched in.
- Opens and closes for one source are serialised. The domain refcounts an open
  session by requester name, so two concurrent opens add a single reference
  while two closes would remove it twice and orphan the surviving handle.
- Device labels are written with textContent throughout: a MIDI device name is
  user-controlled hardware text.

Verified with throwaway jsdom and FastAPI harnesses outside the tree; the
repo has no build, lint, or test tooling.
Comment thread midi-drums/screen.js Outdated
// the only thing that re-opens devices: returning to the screen (or
// re-binding after a re-hydration) has to restore them here too.
// restore() skips sources that are already open.
await restore();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Double restore() call on first activation

activate() calls discover() (line 455) which internally calls restore() (line 141), then activate() calls restore() again here. While idempotent, this does unnecessary work on the first activation. Remove this line since discover() already restores after a successful discovery.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not redundant, and removing it would regress a behaviour. discover() short-circuits with if (discovered && !force) return before it reaches its internal restore(), so on every activation after the first it opens nothing. This call is the only thing that re-opens devices when the user returns to the screen, or after the Host re-hydrates the layer — without it, leaving and re-entering the screen would leave every ticked device closed. The duplicate pass on the first activation is a loop over the saved keys that openSource() skips for anything already open, so it costs nothing.

Pullfrog  | View workflow run | via Pullfrog | Using big-pickle (free) | 𝕏

@cchacs cchacs Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in bc99dda. The extra restore now only runs when the devices were already found, so returning to the screen still reopens them.

Comment thread midi-drums/screen.js Resolved
Comment thread midi-drums/routes.py Outdated
to stream the request body above."""
with write_lock:
merged = {**_read(), **incoming}
config_dir.mkdir(parents=True, exist_ok=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Redundant config_dir.mkdir() call

config_dir.mkdir(parents=True, exist_ok=True) is already called in setup() at line 117 before routes are registered. The call here in _merge_and_persist() is redundant (though harmless).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping this one. The mkdir() in setup() does a different job from this one: it is the fail-fast validation that surfaces an unusable config_dir at load time, before any route is registered (see the comment at routes.py:114). This one makes the write path self-sufficient, so a config_dir that disappears or is rotated mid-session degrades to the existing 500 failed to save settings rather than raising FileNotFoundError out of the tmp-file write. It is also the convention the template sets (my-plugin/routes.py:144 does the same, and there is no setup-time mkdir() there to make it redundant).

Pullfrog  | View workflow run | via Pullfrog | Using big-pickle (free) | 𝕏

@cchacs cchacs Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9ddf2fc. setup() already makes the config folder before the routes exist.

@pullfrog

pullfrog Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Addressed all three threads from the review on #10.

Fixed — midi-drums/screen.js: removed the bindSourcesChanged() call in createDeviceLayer(). setActive() binds the same handler before its own no-op early-return and runs unconditionally at the end of the boot block, so the construction-time call only repeated work. Verified with a jsdom + fake-midi-input-domain harness (18 checks covering the single bus subscription at boot, replug recovery, screen leave/return, note-on filtering and re-hydration) — identical results before and after. node --check passes. Pushed as 151fb8c.

Kept, with reasoning — the other two:

  • routes.py:187 — the mkdir() in _merge_and_persist() is not redundant with the one in setup(). The setup-time call is the fail-fast config validation (it runs before any route is registered); this one keeps the write path self-sufficient, matching the template at my-plugin/routes.py:144. A config_dir that vanishes mid-session then degrades to the handled 500 rather than an exception out of the tmp-file write.
  • screen.js:460 — the second restore() in activate() is the only thing that re-opens devices on re-entry: discover() short-circuits via if (discovered && !force) return before reaching its internal restore(). Removing it would leave every ticked device closed after leaving and returning to the screen.

Both are explained in-thread and left open. The re-hydration thread is resolved; a fresh read of the review turned up no new comments.

Task list (7/7 completed)
  • Checkout PR branch
  • Fetch and read review comments
  • Verify each finding against code
  • Implement justified fixes
  • Test and review diff
  • Commit and push branch
  • Reply per thread and report progress

Pullfrog  | View workflow run | via Pullfrog | Using big-pickle (free) | 𝕏

@kilo-code-bot

kilo-code-bot Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • midi-drums/screen.js
  • midi-drums/routes.py
Previous Review Summary (commit 78e184a)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 78e184a)

Status: 3 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 3
SUGGESTION 0
Issue Details (click to expand)

WARNING

File Line Issue
midi-drums/screen.js 460 Double restore() call on first activation
midi-drums/screen.js 480 Redundant bindSourcesChanged() call in createDeviceLayer()
midi-drums/routes.py 187 Redundant config_dir.mkdir() call
Files Reviewed (4 files)
  • midi-drums/screen.js - 2 issues
  • midi-drums/routes.py - 1 issue
  • midi-drums/screen.html - 0 issues
  • midi-drums/plugin.json - 0 issues
  • midi-drums/assets/plugin.css - 0 issues

Fix these issues in Kilo Cloud


Reviewed by free · Input: 42.4K · Output: 8K · Cached: 219.4K

pullfrog Bot and others added 3 commits October 5, 2026 04:24
setActive() already binds midi-input:sources-changed on the first boot
call, and no session can exist without setActive(true), so the extra
call at layer construction only repeated work.
@cchacs
cchacs merged commit 8504e2e into main Oct 5, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant