From c2dbd33e6d54f8d4ba1f43961e1fb420e6bca585 Mon Sep 17 00:00:00 2001 From: JMO Date: Tue, 22 Sep 2026 16:28:16 -0700 Subject: [PATCH 01/15] Add a secret-scanning workflow Runs gitleaks on pushes to main and on pull requests. The settings file pull_config.sh produces holds API keys in plain text, and .gitignore covers the default filename, but nothing catches a rename or a key in a test fixture. .gitleaks.toml allowlists one path: the constant in app/apps/evenhub/even-api.ts that matches the generic-api-key rule. Scoped by path rather than disabling the rule, so everything else is still scanned. --- .github/workflows/secret-scan.yml | 22 ++++++++++++++++++++++ .gitleaks.toml | 9 +++++++++ 2 files changed, 31 insertions(+) create mode 100644 .github/workflows/secret-scan.yml create mode 100644 .gitleaks.toml diff --git a/.github/workflows/secret-scan.yml b/.github/workflows/secret-scan.yml new file mode 100644 index 00000000..e07f47b0 --- /dev/null +++ b/.github/workflows/secret-scan.yml @@ -0,0 +1,22 @@ +name: Secret scan + +# Scans for committed secrets on pushes to main and on pull requests. + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + gitleaks: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: gitleaks/gitleaks-action@v2 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitleaks.toml b/.gitleaks.toml new file mode 100644 index 00000000..e87ce09b --- /dev/null +++ b/.gitleaks.toml @@ -0,0 +1,9 @@ +# Secret-scanning config for the workflow in .github/workflows/secret-scan.yml. +[extend] +useDefault = true + +[[rules]] +id = "generic-api-key" + [rules.allowlist] + description = "SIGN_KEY in even-api.ts is a client-side constant the app needs to sign EvenHub requests, not a credential." + paths = ['''app/apps/evenhub/even-api\.ts'''] From a7916a54790f2408cc433225314ebf3e533cf4fe Mon Sep 17 00:00:00 2001 From: JMO Date: Tue, 22 Sep 2026 22:09:11 -0700 Subject: [PATCH 02/15] Make the firmware upgrade reachable from older custom firmware Glasses running an older Faceclaw firmware revision cannot be upgraded at all. The flash asks for confirmation on the lens first, and that firmware never shows the prompt, so the one path off it is closed. Measured on revision 13 (base 2.2.9.22). showPrompt writes the EvenHub create-prompt page to the right arm and waits for an ack. The lens accepts the write at the GATT layer and then answers nothing and displays nothing: four attempts, four onWriteCharacteristic status=0 length=165, no reply. buildCreatePromptPage documents its geometry as stock-firmware-safe "so this works before flashing", and that does not hold there. Three faults, each only visible once the previous one is fixed. An unacked prompt page was fatal. Flashing is the only way off that firmware, so failing here leaves the glasses permanently unupgradable. An unacked page now falls back to the phone-side confirmation that the low-battery retry already uses, on a screen that says what is being given up: the on-lens step is what proves the glasses are the wearer's. connectArm made a single attempt. The lens is routinely unavailable for a moment just after a previous link to it closed, which is exactly the state a retry is in, so the fallback failed instantly with "could not connect to the right arm". FaceclawFirmwareFlasher.connectLensResilient already retries within a window for this reason; connectArmResilient mirrors it and reports each attempt. That retry then tripped the lost-connection latch. Disconnecting between attempts fires onConnectionStateChange, which set rightLost AND finished and counted down the selection latch, so a successful second attempt still aborted with "Lost connection to the glasses". The callback now ignores drops while the arms are still being established. Logging, because a failed flash used to leave almost nothing to read. emitLog reached logcat, but emitState, emitProgress and emitComplete on the flasher, and emitState and emitResult on the prompt communicator, went only to the in-app listener, so the line carrying the reason existed only on the phone screen. Seven `catch (Exception ignored)` blocks across the two classes now log with the Throwable, matching the call sites elsewhere that already do. tests/flash-prompt-fallback.test.cjs covers the decision at its seam through the existing load-typescript loader. The first test fails against the current view model and passes with this change; the second is a regression guard and passes either way, so an unrelated failure cannot quietly skip the on-lens confirmation. Verified on hardware: both lenses flashed through this path after it had been a dead end. --- .../faceclaw/app/FaceclawFirmwareFlasher.java | 40 +++++++-- .../app/FaceclawFlashPromptCommunicator.java | 85 +++++++++++++++++-- app/phone-ui/onboarding-flash-view-model.ts | 31 ++++++- tests/flash-prompt-fallback.test.cjs | 62 ++++++++++++++ 4 files changed, 202 insertions(+), 16 deletions(-) create mode 100644 tests/flash-prompt-fallback.test.cjs diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java index 3a941ac6..91f5bcaa 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFirmwareFlasher.java @@ -133,7 +133,8 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener { cancel(); try { bleManager.close(); - } catch (Exception ignored) { + } catch (Exception e) { + Log.w(TAG, "close: ble manager close failed", e); } } @@ -207,7 +208,8 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener { emitLog(lens + " lens: all components verified"); try { bleManager.disconnect(address); - } catch (Exception ignored) { + } catch (Exception e) { + Log.w(TAG, "disconnect after " + lens + " lens failed: address=" + address, e); } } @@ -349,7 +351,9 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener { emitLog(lens + " connect attempt " + attempt + " failed (" + lastError + "); retrying..."); try { bleManager.disconnect(address); - } catch (Exception ignored) { + } catch (Exception e) { + // Routine between retries: the link is usually already gone. + Log.d(TAG, "disconnect before retry failed: address=" + address, e); } sleepInterruptibly(RETRY_DELAY_MS); } @@ -545,6 +549,8 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener { } BleProtocol.ParsedFrame frame = BleProtocol.parseFrame(data); if (!frame.ok || frame.pb.length < 2) { + // Dropped acks surface upstream as an unexplained timeout, so say so here. + Log.d(TAG, "ignoring ack frame: ok=" + frame.ok + " len=" + frame.pb.length); return; } dataAcks.add(Arrays.copyOf(frame.pb, Math.min(frame.pb.length, 2))); @@ -552,9 +558,7 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener { @Override public void onConnectionStateChange(String address, boolean connected) { - if (!connected) { - Log.i(TAG, "disconnected: " + address); - } + Log.i(TAG, (connected ? "connected: " : "disconnected: ") + address); } // ---- helpers ------------------------------------------------------------- @@ -562,7 +566,8 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener { private void teardown() { try { bleManager.close(); - } catch (Exception ignored) { + } catch (Exception e) { + Log.w(TAG, "teardown: ble manager close failed", e); } } @@ -665,6 +670,13 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener { private void emitProgress(String lens, int componentIndex, int componentCount, int blockIndex, int blockCount, long bytesSent, long bytesTotal) { + // Per-block, so DEBUG and behind isLoggable: thousands of these per flash, and + // without the guard the message is built every time even when nobody reads it. + // The component boundaries are the interesting ones and emitLog reports those. + if (Log.isLoggable(TAG, Log.DEBUG)) { + Log.d(TAG, "progress " + lens + " component " + (componentIndex + 1) + "/" + componentCount + + " block " + (blockIndex + 1) + "/" + blockCount + " " + bytesSent + "/" + bytesTotal + " bytes"); + } mainHandler.post(() -> { FaceclawFirmwareFlasherListener current = listener; if (current != null) { @@ -675,6 +687,13 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener { private void emitState(String state, String detail) { final String safeDetail = detail == null ? "" : detail; + // These went only to the in-app listener, so a failed flash left its reason on + // the phone screen and nowhere a log could be read from afterwards. + if ("error".equals(state)) { + Log.e(TAG, "state=" + state + (safeDetail.isEmpty() ? "" : " -- " + safeDetail)); + } else { + Log.i(TAG, "state=" + state + (safeDetail.isEmpty() ? "" : " -- " + safeDetail)); + } mainHandler.post(() -> { FaceclawFirmwareFlasherListener current = listener; if (current != null) { @@ -685,6 +704,13 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener { private void emitComplete(boolean success, String detail) { final String safeDetail = detail == null ? "" : detail; + // The single most important line of a flash, and it used to be invisible to + // logcat: on failure this carries the reason. + if (success) { + Log.i(TAG, "complete success -- " + safeDetail); + } else { + Log.e(TAG, "complete FAILED -- " + safeDetail); + } mainHandler.post(() -> { FaceclawFirmwareFlasherListener current = listener; if (current != null) { diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java index ef342706..833a34b2 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java @@ -4,6 +4,7 @@ import android.bluetooth.BluetoothGatt; import android.content.Context; import android.os.Handler; import android.os.Looper; +import android.os.SystemClock; import android.util.Log; import java.util.List; @@ -54,6 +55,15 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { private static final int SELECTION_TIMEOUT_MS = 120_000; private static final int CREATE_ACK_TIMEOUT_MS = 3_000; private static final int BATTERY_ACK_TIMEOUT_MS = 3_000; + /** + * A single connect attempt is not enough. The lens is routinely unavailable for a + * moment right after a previous link to it closed, which is exactly the situation + * this class is in whenever the user retries. FaceclawFirmwareFlasher already + * retries within a window for the same reason; this matches it rather than + * failing on the first refusal. + */ + private static final int ARM_CONNECT_WINDOW_MS = 30_000; + private static final int ARM_RETRY_DELAY_MS = 2_500; private final Context context; private final String rightAddress; @@ -75,6 +85,14 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { private volatile boolean rightConnected = false; private volatile boolean leftConnected = false; private volatile boolean rightLost = false; + /** + * True while the arms are still being brought up. connectArmResilient disconnects + * between retries, which fires onConnectionStateChange, and that callback latches + * rightLost AND finished -- so without this guard a deliberate retry-disconnect + * ends the whole run, even when the very next attempt connects. Only a drop AFTER + * both arms are up is a lost connection. + */ + private volatile boolean establishing = true; private volatile Boolean approved = null; private volatile ScheduledExecutorService heartbeatExecutor; @@ -121,7 +139,8 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { stopHeartbeat(); try { bleManager.close(); - } catch (Exception ignored) { + } catch (Exception e) { + Log.w(TAG, "close: ble manager close failed", e); } } @@ -148,6 +167,8 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { } } + // Both arms are up: from here a disconnect really is a lost connection. + establishing = false; emitState("connected", ""); // Auth both arms now (pairing prompts, if any, happen here) before // anything else, so both bonds exist by the time flashing starts. @@ -185,7 +206,10 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { } try { sendShutdown(); - } catch (Exception ignored) { + } catch (Exception e) { + // The lens is about to be torn down either way, but a shutdown that + // never lands is why it can come back up in an odd state. + Log.w(TAG, "sendShutdown failed", e); } if (!Boolean.TRUE.equals(approved)) { emitResult(false); @@ -224,9 +248,7 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { } private void connectArm(String address, String arm) { - if (!bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) { - throw new IllegalStateException("could not connect to the " + arm + " arm (" + address + ")"); - } + connectArmResilient(address, arm); bleManager.requestConnectionPriority(address, BluetoothGatt.CONNECTION_PRIORITY_HIGH); bleManager.requestMtu(address, ConnectionOptions.DESIRED_MTU, ConnectionOptions.CONNECT_TIMEOUT_MS); if (!bleManager.discoverServices(address, ConnectionOptions.SERVICES_TIMEOUT_MS)) { @@ -238,6 +260,51 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { emitLog("connected " + arm + " arm"); } + private void sleepInterruptibly(int ms) { + try { + Thread.sleep(ms); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + /** + * Keep trying to open the link until the window closes, reporting each attempt. + * Mirrors FaceclawFirmwareFlasher.connectLensResilient. + */ + private void connectArmResilient(String address, String arm) { + long deadline = SystemClock.elapsedRealtime() + ARM_CONNECT_WINDOW_MS; + String lastError = "unknown"; + int attempt = 0; + while (SystemClock.elapsedRealtime() < deadline && !cancelled) { + attempt++; + try { + if (bleManager.connect(address, ConnectionOptions.CONNECT_TIMEOUT_MS)) { + if (attempt > 1) { + emitLog("connected " + arm + " arm (attempt " + attempt + ")"); + } + return; + } + lastError = "connect refused"; + } catch (Exception e) { + lastError = e.getMessage() == null ? e.toString() : e.getMessage(); + } + emitLog(arm + " arm connect attempt " + attempt + " failed (" + lastError + "); retrying..."); + try { + bleManager.disconnect(address); + } catch (Exception e) { + // Expected between retries: the link is usually already gone. + Log.d(TAG, "disconnect before retry failed: address=" + address, e); + } + sleepInterruptibly(ARM_RETRY_DELAY_MS); + } + if (cancelled) { + throw new IllegalStateException("Cancelled."); + } + throw new IllegalStateException( + "could not connect to the " + arm + " arm after " + attempt + " attempts: " + lastError); + } + /** * Security-auth exchange on sid=0x80: firmware 2.2.9 will not run a * session (and closes the link after ~30 s) without it, and on an @@ -412,7 +479,8 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { stopHeartbeat(); try { bleManager.close(); - } catch (Exception ignored) { + } catch (Exception e) { + Log.w(TAG, "teardown: ble manager close failed", e); } rightConnected = false; leftConnected = false; @@ -485,7 +553,7 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { if (address.equalsIgnoreCase(rightAddress)) { rightConnected = false; synchronized (lock) { - if (!finished && !cancelled) { + if (!establishing && !finished && !cancelled) { rightLost = true; finished = true; selectionLatch.countDown(); @@ -518,6 +586,8 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { private void emitState(String state, String detail) { final String safeDetail = detail == null ? "" : detail; + // Listener-only until now, so a prompt that never appeared left no trace in a log. + Log.i(TAG, "state=" + state + (safeDetail.isEmpty() ? "" : " -- " + safeDetail)); mainHandler.post(() -> { FaceclawFlashPromptListener current = listener; if (current != null) { @@ -536,6 +606,7 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { } private void emitResult(boolean approvedResult) { + Log.i(TAG, "result=" + (approvedResult ? "approved" : "declined")); mainHandler.post(() -> { FaceclawFlashPromptListener current = listener; if (current != null) { diff --git a/app/phone-ui/onboarding-flash-view-model.ts b/app/phone-ui/onboarding-flash-view-model.ts index 3249c97c..a99e4880 100644 --- a/app/phone-ui/onboarding-flash-view-model.ts +++ b/app/phone-ui/onboarding-flash-view-model.ts @@ -56,6 +56,8 @@ export class OnboardingFlashViewModel extends Observable { private flasher: FirmwareFlasher | null = null; private flasherUnsubscribers: Array<() => void> = []; private retryAction: () => void = () => this.beginPrompt(); + /** Overrides the error screen's "Retry" when the action is not a retry. */ + private primaryLabelOverride: string | null = null; constructor(options?: { mode?: FlashMode; fromOnboarding?: boolean; autoStart?: boolean }) { super(); @@ -174,7 +176,7 @@ export class OnboardingFlashViewModel extends Observable { case "flashed": return "Finish"; case "error": - return "Retry"; + return this.primaryLabelOverride ?? "Retry"; default: return ""; } @@ -329,6 +331,25 @@ export class OnboardingFlashViewModel extends Observable { break; case "error": this.busy = false; + // An unacked prompt page is not a connection failure. The lens accepts the + // write at the GATT layer and then never answers, which is what firmware + // older than the revision this build needs does -- measured on revision 13, + // four writes accepted with status 0 and no reply, nothing shown on the lens. + // Flashing is the only way OFF that firmware, so failing here leaves the + // glasses permanently unupgradable. Fall back to the phone-side confirmation + // that the low-battery retry already uses, and say plainly what is being + // given up: the on-lens step is what proves these are the wearer's glasses. + if (/prompt page not acked/i.test(detail)) { + this.toError( + "Your glasses are on older firmware that can't show the confirmation prompt, so it can't be " + + "confirmed on the lens. You can confirm here instead. Only continue if these are your glasses " + + "and you want to install Faceclaw's custom firmware.", + () => this.beginPrompt({ skipPrompt: true }), + "Confirm On Your Phone", + "Confirm & Install", + ); + break; + } this.toError(detail || "Connection failed.", () => this.beginPrompt({ skipPrompt })); break; } @@ -569,7 +590,13 @@ export class OnboardingFlashViewModel extends Observable { }); } - private toError(message: string, retry: () => void, headline = "Something Went Wrong"): void { + private toError( + message: string, + retry: () => void, + headline = "Something Went Wrong", + primaryLabel: string | null = null, + ): void { + this.primaryLabelOverride = primaryLabel; this.retryAction = retry; this.status = message; this.setPhase("error"); diff --git a/tests/flash-prompt-fallback.test.cjs b/tests/flash-prompt-fallback.test.cjs new file mode 100644 index 00000000..a82151a8 --- /dev/null +++ b/tests/flash-prompt-fallback.test.cjs @@ -0,0 +1,62 @@ +// The flash cannot always ask for confirmation on the lens: firmware older than +// the revision this build needs accepts the create-prompt write and then never +// answers it. Flashing is the only way off that firmware, so an unacked prompt +// must not be a dead end. These cover the decision at that seam. +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { loader } = require('./helpers/load-typescript.cjs'); + +function fixture() { + class Observable { notifyPropertyChange() {} } + const load = loader({}, { + '@nativescript/core': { Observable, Frame: { topmost: () => null } }, + './onboarding-navigation': { finishOnboardingNavigation() {} }, + './onboarding-state': { setOnboardingCompleted() {}, setPreviewOnlyMode() {} }, + '../native/ble-permissions': { ensureBlePermissions: async () => {} }, + '../g2/device-addresses': { + isValidMacAddress: () => true, + loadDeviceAddresses: () => ({ right: 'R', left: 'L' }), + saveDeviceAddresses() {}, + }, + '../g2/firmware-builder': { + buildCustomFirmware: async () => '', buildStockFirmware: async () => '', + FirmwareBuildError: class extends Error {}, FirmwareProgress: {}, + }, + '../native/device-discovery': { buildAddressSet: () => new Set(), DeviceDiscoveryBridge: class {} }, + '../native/firmware-flasher': { FirmwareFlasher: class {} }, + '../native/flash-prompt-communicator': { FlashPromptCommunicator: class {} }, + '../g2/reconnect-policy': { resumeAutoReconnect() {}, suppressAutoReconnect() {} }, + '../util/format-error': { formatErrorMessage: (e) => String(e) }, + }); + const mod = load('app/phone-ui/onboarding-flash-view-model.ts'); + const Model = mod.OnboardingFlashViewModel ?? Object.values(mod).find((v) => typeof v === 'function'); + const model = new Model(); + const beginArgs = []; + model.beginPrompt = async (options) => { beginArgs.push(options); }; + return { model, beginArgs }; +} + +test('an unacked prompt page offers the phone-side confirmation instead of dead-ending', () => { + const { model, beginArgs } = fixture(); + model.handlePromptState('error', 'prompt page not acked: AA:BB:CC:DD:EE:FF', false); + assert.equal(model.phase, 'error'); + assert.equal(model.headline, 'Confirm On Your Phone'); + assert.equal(model.primaryLabel, 'Confirm & Install', 'the action is a confirmation, not a retry'); + assert.match(model.status, /can't show the confirmation prompt/i); + model.retryAction(); + // The loader runs the module in its own vm realm, so the options object has that + // realm's prototype and deepStrictEqual would reject it. Compare the field. + assert.equal(beginArgs.length, 1); + assert.equal(beginArgs[0].skipPrompt, true, 'confirming must skip the lens prompt, not re-ask for it'); +}); + +test('any other prompt failure still reports as an ordinary retry', () => { + const { model, beginArgs } = fixture(); + model.handlePromptState('error', 'discoverServices failed: right arm', false); + assert.equal(model.phase, 'error'); + assert.equal(model.headline, 'Something Went Wrong'); + assert.equal(model.primaryLabel, 'Retry'); + model.retryAction(); + assert.equal(beginArgs.length, 1); + assert.equal(beginArgs[0].skipPrompt, false, 'an ordinary failure must not silently skip the lens prompt'); +}); From b361cdd934fb3ced507acfed6f7b7c9a11c0e2ce Mon Sep 17 00:00:00 2001 From: JMO Date: Tue, 22 Sep 2026 22:09:40 -0700 Subject: [PATCH 03/15] Say how to leave silent mode, and drop the belief when it is contradicted Silent mode is entered and exited only by long-pressing both touchpads. While it is on the firmware ignores all input and blanks the display, so from the phone the glasses look connected and simply answer nothing. The app cannot turn it off either: the communicator exposes onSilentMode and a listener set, and no setter. The status read "Connected (Silent mode enabled)", which names the state but not the way out of it, and someone who triggered the gesture by accident has no reason to know the gesture exists. It now names it. The belief can also go stale. silentMode is set by the firmware's push event and cleared only when the phase goes to disconnected, on the assumption that it is re-reported on the way back up. Toggle silent mode on the glasses without the link ever dropping and that assumption does not hold: a missed "off" leaves the phone claiming silent mode for as long as the connection lasts. Observed with the status reading silent while the glasses were granting framebuffer leases and rendering frames, which cannot happen in a mode whose whole behaviour is to blank the display and ignore input. So the belief is dropped whenever the glasses do something that mode makes impossible: input from them, since the firmware discards input while silent, or a shell render that gets past the display-target check, since silent mode does not leave a display target standing. The second matters because it self-heals without the user touching anything. Not unit-tested. Both halves are in dashboard-controller.ts, which imports 56 modules, so a test there would mostly exercise mocks, and the stale-clear is state management across events rather than a pure function. The refactor that would make it testable is moving the silent-mode belief into a small state holder taking the firmware's push events plus the two kinds of contradicting evidence; glasses-display-state.ts next door is already exactly that shape. --- app/g2/dashboard-controller.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index caa893c8..220a4465 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -1007,12 +1007,30 @@ class DashboardController { * charging and silent mode both make the display unavailable while BLE stays * up, and a battery that just ran out simply stops answering. */ + /** + * Drop a stale silent-mode belief when the glasses do something they could + * not do in silent mode. The firmware blanks the display and ignores input + * while silent, so input or a presented frame is direct evidence it ended. + */ + private clearSilentModeOnEvidence(reason: string): void { + if (!this.silentMode) return; + this.silentMode = false; + this.appendLog(`silent mode cleared: ${reason}`); + this.emit(); + } + private displayPreviewMessage(): string { if (this.phase === "charging") { return this.glassesDisplayLabel(); } if (this.silentMode && this.phase === "connected") { - return "Connected (Silent mode enabled)"; + // Say how to leave it. Silent mode is entered and exited only by the + // same gesture on the glasses, the firmware ignores all input and blanks + // the display while it is on, and nothing here can turn it off -- there + // is no setter, only onSilentMode. So a user who triggered it by + // accident sees a connected pair of glasses that answers nothing, with + // no way to find out why. The instruction belongs where they are looking. + return "Connected (Silent mode — long-press both touchpads on the glasses to exit)"; } // The preview compositor keeps the mirror live (including as a black // frame while the simulated screen is off), so never cover it. @@ -1399,6 +1417,12 @@ class DashboardController { } }); this.offRing = communicator.onRingEvent((event) => { + // Input is proof silent mode ended: the firmware ignores all input while + // it is on, so one cannot arrive in that state. silentMode is otherwise + // cleared only on a disconnect, on the assumption that the firmware + // re-reports it, so a toggle made on the glasses without the link ever + // dropping leaves the phone claiming silent mode indefinitely. + this.clearSilentModeOnEvidence("input from the glasses"); void this.handleInputEvent(event).catch((error) => { const message = this.formatError(error); this.appendLog(`input handler failed: ${message}`); @@ -2503,6 +2527,9 @@ class DashboardController { frameTimings.finishFrame(frameId, "discarded: shell render with no display target"); return; } + // A display target is standing, which silent mode does not leave in place. + // Evidence enough to drop the flag even if the user never touches the glasses. + this.clearSilentModeOnEvidence("the glasses are presenting frames"); // A shell overlay that dims what it covers (a context menu) must dim the // window surfaces too, which live below the shell surface in the // compositor: forward the factor before this frame composites. From 423f9fbcd1f9ec484def08dff9b42c6afb774d72 Mon Sep 17 00:00:00 2001 From: JMO Date: Wed, 23 Sep 2026 08:12:19 -0700 Subject: [PATCH 04/15] Refuse the flash with a reason when the glasses are in silent mode Silent mode blanks the display and stops the firmware dispatching input while BLE and GATT stay up, so the create-prompt page is written, accepted, and never rendered. There is no touchpad event available to answer it with, and the flash ends after four attempts on "prompt page not acked", which tells the wearer nothing about what is wrong or what to do about it. readBattery was already performing a sid-0x09 settings read whose ack carries silentMode next to the battery, and the field was being discarded. readSettings is factored out so one read serves both callers, and requireNotSilent runs it before the prompt page is written. A positive report throws, and the existing error path turns the message into what the wearer sees: that the glasses are in silent mode, and that long-pressing both touchpads leaves it. It refuses only on a positive report. An arm that does not answer, and firmware whose ack omits the field, both fall through to the prompt exactly as before, so older firmware is unaffected and a missing answer can never block a flash. This replaces the phone-side confirmation fallback that was here before. An unacked page is not by itself evidence that the prompt was never displayed, and the on-lens step is what proves the glasses being flashed are the ones in front of you, so overriding it on a timeout gave up more than it was worth. --- .../app/FaceclawFlashPromptCommunicator.java | 57 +++++++++++++++-- app/phone-ui/onboarding-flash-view-model.ts | 31 +--------- tests/flash-prompt-fallback.test.cjs | 62 ------------------- 3 files changed, 55 insertions(+), 95 deletions(-) delete mode 100644 tests/flash-prompt-fallback.test.cjs diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java index 833a34b2..a0707a19 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java @@ -51,6 +51,15 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { // Index 0 = decline, index 1 = approve. Kept short for the ~50-col grid. private static final String[] ITEMS = new String[] {"No, cancel", "Yes, flash"}; + /** + * Shown instead of a bare "prompt page not acked" timeout. Silent mode is + * entered and left by the same gesture on the glasses and nothing here can + * clear it, so the instruction has to travel with the error. + */ + private static final String SILENT_MODE_MESSAGE = + "Your glasses are in silent mode, so they cannot show the confirmation prompt. " + + "Long-press both touchpads on the glasses to leave silent mode, then try again."; + private static final int HEARTBEAT_INTERVAL_MS = 4_000; private static final int SELECTION_TIMEOUT_MS = 120_000; private static final int CREATE_ACK_TIMEOUT_MS = 3_000; @@ -183,6 +192,12 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { sendPrelude(rightAddress); if (!skipPrompt) { + // Ask before writing a page that cannot be answered: silent mode + // blanks the display and stops the firmware dispatching input while + // BLE stays up, so the create-prompt write is still accepted and the + // flash would spend four attempts timing out on an error that says + // nothing about the reason. + requireNotSilent(rightAddress); showPrompt(rightAddress); startHeartbeat(); emitState("prompting", ""); @@ -376,6 +391,18 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { } private int readBattery(String address, String arm) throws InterruptedException { + BleProtocol.BatterySnapshot snapshot = readSettings(address, arm); + return snapshot == null ? -1 : snapshot.battery; + } + + /** + * The sid-0x09 settings read behind both the battery gate and the + * silent-mode check. One ack carries both fields, so neither caller needs + * a query of its own. Returns null when the arm does not answer or the ack + * carries no settings payload. + */ + private BleProtocol.BatterySnapshot readSettings(String address, String arm) + throws InterruptedException { for (int attempt = 0; attempt < 2 && !cancelled; attempt++) { int magic = allocMagic(); byte[] ack = writeAndAwaitAck( @@ -386,16 +413,38 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { BleProtocol.buildSettingsQuery(magic), BATTERY_ACK_TIMEOUT_MS); if (ack == null) { - emitLog("battery read attempt " + (attempt + 1) + " unacked: " + arm + " arm"); + emitLog("settings read attempt " + (attempt + 1) + " unacked: " + arm + " arm"); continue; } BleProtocol.BatterySnapshot snapshot = BleProtocol.parseSettingsBattery(ack); if (snapshot != null) { - return snapshot.battery; + return snapshot; } - emitLog("battery read ack had no battery field: " + arm + " arm"); + emitLog("settings read ack had no settings payload: " + arm + " arm"); + } + return null; + } + + /** + * Refuse the prompt only when the arm POSITIVELY reports silent mode. An + * arm that does not answer, and firmware whose ack omits the field + * (silentMode -1), both fall through to the prompt, so behavior on older + * firmware is unchanged and a missing answer never blocks a flash. + */ + private void requireNotSilent(String address) throws InterruptedException { + BleProtocol.BatterySnapshot snapshot = readSettings(address, "right"); + String state; + if (snapshot == null) { + state = "unknown (arm did not answer)"; + } else if (snapshot.silentMode < 0) { + state = "unknown (ack omits the field)"; + } else { + state = snapshot.silentMode > 0 ? "on" : "off"; + } + emitLog("silent mode before prompt: " + state); + if (snapshot != null && snapshot.silentMode > 0) { + throw new IllegalStateException(SILENT_MODE_MESSAGE); } - return -1; } /** Write and wait for the matching ack; returns the ack's protobuf (may be empty), or null on timeout/write failure. */ diff --git a/app/phone-ui/onboarding-flash-view-model.ts b/app/phone-ui/onboarding-flash-view-model.ts index a99e4880..3249c97c 100644 --- a/app/phone-ui/onboarding-flash-view-model.ts +++ b/app/phone-ui/onboarding-flash-view-model.ts @@ -56,8 +56,6 @@ export class OnboardingFlashViewModel extends Observable { private flasher: FirmwareFlasher | null = null; private flasherUnsubscribers: Array<() => void> = []; private retryAction: () => void = () => this.beginPrompt(); - /** Overrides the error screen's "Retry" when the action is not a retry. */ - private primaryLabelOverride: string | null = null; constructor(options?: { mode?: FlashMode; fromOnboarding?: boolean; autoStart?: boolean }) { super(); @@ -176,7 +174,7 @@ export class OnboardingFlashViewModel extends Observable { case "flashed": return "Finish"; case "error": - return this.primaryLabelOverride ?? "Retry"; + return "Retry"; default: return ""; } @@ -331,25 +329,6 @@ export class OnboardingFlashViewModel extends Observable { break; case "error": this.busy = false; - // An unacked prompt page is not a connection failure. The lens accepts the - // write at the GATT layer and then never answers, which is what firmware - // older than the revision this build needs does -- measured on revision 13, - // four writes accepted with status 0 and no reply, nothing shown on the lens. - // Flashing is the only way OFF that firmware, so failing here leaves the - // glasses permanently unupgradable. Fall back to the phone-side confirmation - // that the low-battery retry already uses, and say plainly what is being - // given up: the on-lens step is what proves these are the wearer's glasses. - if (/prompt page not acked/i.test(detail)) { - this.toError( - "Your glasses are on older firmware that can't show the confirmation prompt, so it can't be " + - "confirmed on the lens. You can confirm here instead. Only continue if these are your glasses " + - "and you want to install Faceclaw's custom firmware.", - () => this.beginPrompt({ skipPrompt: true }), - "Confirm On Your Phone", - "Confirm & Install", - ); - break; - } this.toError(detail || "Connection failed.", () => this.beginPrompt({ skipPrompt })); break; } @@ -590,13 +569,7 @@ export class OnboardingFlashViewModel extends Observable { }); } - private toError( - message: string, - retry: () => void, - headline = "Something Went Wrong", - primaryLabel: string | null = null, - ): void { - this.primaryLabelOverride = primaryLabel; + private toError(message: string, retry: () => void, headline = "Something Went Wrong"): void { this.retryAction = retry; this.status = message; this.setPhase("error"); diff --git a/tests/flash-prompt-fallback.test.cjs b/tests/flash-prompt-fallback.test.cjs deleted file mode 100644 index a82151a8..00000000 --- a/tests/flash-prompt-fallback.test.cjs +++ /dev/null @@ -1,62 +0,0 @@ -// The flash cannot always ask for confirmation on the lens: firmware older than -// the revision this build needs accepts the create-prompt write and then never -// answers it. Flashing is the only way off that firmware, so an unacked prompt -// must not be a dead end. These cover the decision at that seam. -const test = require('node:test'); -const assert = require('node:assert/strict'); -const { loader } = require('./helpers/load-typescript.cjs'); - -function fixture() { - class Observable { notifyPropertyChange() {} } - const load = loader({}, { - '@nativescript/core': { Observable, Frame: { topmost: () => null } }, - './onboarding-navigation': { finishOnboardingNavigation() {} }, - './onboarding-state': { setOnboardingCompleted() {}, setPreviewOnlyMode() {} }, - '../native/ble-permissions': { ensureBlePermissions: async () => {} }, - '../g2/device-addresses': { - isValidMacAddress: () => true, - loadDeviceAddresses: () => ({ right: 'R', left: 'L' }), - saveDeviceAddresses() {}, - }, - '../g2/firmware-builder': { - buildCustomFirmware: async () => '', buildStockFirmware: async () => '', - FirmwareBuildError: class extends Error {}, FirmwareProgress: {}, - }, - '../native/device-discovery': { buildAddressSet: () => new Set(), DeviceDiscoveryBridge: class {} }, - '../native/firmware-flasher': { FirmwareFlasher: class {} }, - '../native/flash-prompt-communicator': { FlashPromptCommunicator: class {} }, - '../g2/reconnect-policy': { resumeAutoReconnect() {}, suppressAutoReconnect() {} }, - '../util/format-error': { formatErrorMessage: (e) => String(e) }, - }); - const mod = load('app/phone-ui/onboarding-flash-view-model.ts'); - const Model = mod.OnboardingFlashViewModel ?? Object.values(mod).find((v) => typeof v === 'function'); - const model = new Model(); - const beginArgs = []; - model.beginPrompt = async (options) => { beginArgs.push(options); }; - return { model, beginArgs }; -} - -test('an unacked prompt page offers the phone-side confirmation instead of dead-ending', () => { - const { model, beginArgs } = fixture(); - model.handlePromptState('error', 'prompt page not acked: AA:BB:CC:DD:EE:FF', false); - assert.equal(model.phase, 'error'); - assert.equal(model.headline, 'Confirm On Your Phone'); - assert.equal(model.primaryLabel, 'Confirm & Install', 'the action is a confirmation, not a retry'); - assert.match(model.status, /can't show the confirmation prompt/i); - model.retryAction(); - // The loader runs the module in its own vm realm, so the options object has that - // realm's prototype and deepStrictEqual would reject it. Compare the field. - assert.equal(beginArgs.length, 1); - assert.equal(beginArgs[0].skipPrompt, true, 'confirming must skip the lens prompt, not re-ask for it'); -}); - -test('any other prompt failure still reports as an ordinary retry', () => { - const { model, beginArgs } = fixture(); - model.handlePromptState('error', 'discoverServices failed: right arm', false); - assert.equal(model.phase, 'error'); - assert.equal(model.headline, 'Something Went Wrong'); - assert.equal(model.primaryLabel, 'Retry'); - model.retryAction(); - assert.equal(beginArgs.length, 1); - assert.equal(beginArgs[0].skipPrompt, false, 'an ordinary failure must not silently skip the lens prompt'); -}); From 67c0671b9976aa73994386dbeff446db7f2b8b39 Mon Sep 17 00:00:00 2001 From: JMO Date: Wed, 23 Sep 2026 08:46:56 -0700 Subject: [PATCH 05/15] Log what the lens answers the prompt page with An ack is not a render: the lens can accept the create-prompt page and display nothing, so a null check on the reply throws away the only thing that can say what it did. The page size going out and the ack payload coming back are both logged now. --- .../app/FaceclawFlashPromptCommunicator.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java index a0707a19..8e9657f1 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawFlashPromptCommunicator.java @@ -366,9 +366,15 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { int magic = allocMagic(); byte[] page = BleProtocol.buildCreatePromptPage( magic, TEXT_NAME, TEXT_CONTAINER_ID, warningText, LIST_NAME, LIST_CONTAINER_ID, ITEMS); + emitLog("prompt page attempt " + (attempt + 1) + " writing " + page.length + " bytes to " + address); pageAck = writeAndAwaitAck(address, BleProtocol.SID_EVENHUB, BleProtocol.FLAG_REQUEST, magic, page, CREATE_ACK_TIMEOUT_MS); if (pageAck == null) { emitLog("prompt page attempt " + (attempt + 1) + " unacked: " + address); + } else { + // An ack is not a render. The lens can accept the page and show + // nothing, so the payload it answers with is the only thing that + // can say why; log it rather than only null-checking it. + emitLog("prompt page ack payload (" + pageAck.length + " bytes): " + toHex(pageAck)); } } if (pageAck == null) { @@ -633,6 +639,15 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener { }); } + /** Ack payloads are short; hex is the only honest way to log an opaque reply. */ + private static String toHex(byte[] bytes) { + StringBuilder out = new StringBuilder(bytes.length * 2); + for (byte b : bytes) { + out.append(String.format(java.util.Locale.ROOT, "%02x", b & 255)); + } + return out.toString(); + } + private void emitState(String state, String detail) { final String safeDetail = detail == null ? "" : detail; // Listener-only until now, so a prompt that never appeared left no trace in a log. From cd32aed849657122c0767a0addda6cad77c0353d Mon Sep 17 00:00:00 2001 From: JMO Date: Fri, 25 Sep 2026 08:09:56 -0700 Subject: [PATCH 06/15] Compile events.ts for the tests tests/ios-glanceboard.test.cjs requires .test-build/app/g2/events.js, but events.ts was not in tests/tsconfig.json, so a clean build never produced it. --- tests/tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/tsconfig.json b/tests/tsconfig.json index b88019f9..c24890c2 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -47,6 +47,7 @@ "../app/apps/timer/timer-model.ts", "../app/apps/teleprompter/script-tracker.ts", "../app/apps/nightscout/nightscout-alerts.ts", - "../app/g2/brightness-curve.ts" + "../app/g2/brightness-curve.ts", + "../app/g2/events.ts" ] } From 0459f34e5b3e34d9214d436a53cd98d3c859a770 Mon Sep 17 00:00:00 2001 From: JMO Date: Fri, 25 Sep 2026 08:11:31 -0700 Subject: [PATCH 07/15] Drop the silent-mode clearing, keep only the exit instruction The native session already clears silent mode on touchpad input and re-reads it on every battery poll, so the TypeScript-side clearing duplicated that. The display-target trigger was also wrong: the communicator stays up in silent mode, so it cleared the flag on the next shell render. --- app/g2/dashboard-controller.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index de643e1f..cd6fb470 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -1021,7 +1021,13 @@ class DashboardController { return this.glassesDisplayLabel(); } if (this.silentMode && this.phase === "connected") { - return "Connected (Silent mode enabled)"; + // Say how to leave it. Silent mode is entered and exited only by the + // same gesture on the glasses, the firmware ignores all input and blanks + // the display while it is on, and nothing here can turn it off -- there + // is no setter, only onSilentMode. So a user who triggered it by + // accident sees a connected pair of glasses that answers nothing, with + // no way to find out why. The instruction belongs where they are looking. + return "Connected (Silent mode — long-press both touchpads on the glasses to exit)"; } // The preview compositor keeps the mirror live (including as a black // frame while the simulated screen is off), so never cover it. From 727a3c0378889dc0534ee9bddff6c2bf9796bbe9 Mon Sep 17 00:00:00 2001 From: JMO Date: Fri, 25 Sep 2026 08:16:55 -0700 Subject: [PATCH 08/15] Refuse the flash prompt with a reason when the glasses are in silent mode Ported to the shared Kotlin FlashPromptFlow after the Java was converted. The settings ack the battery read already parses carries silentMode; read it before writing the prompt page, and refuse only on a positive report. An arm that does not answer, or an ack without the field, falls through to the prompt as before. --- .../app/g2protocol/FlashPromptFlow.kt | 54 ++++++++++++++++--- .../kotlin/com/faceclaw/app/StockFlowsTest.kt | 44 +++++++++++++-- 2 files changed, 89 insertions(+), 9 deletions(-) diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FlashPromptFlow.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FlashPromptFlow.kt index d1b5a3f8..9fb44fa5 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FlashPromptFlow.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FlashPromptFlow.kt @@ -13,7 +13,9 @@ import kotlin.concurrent.Volatile * pairing prompt, and doing both here means both prompts appear at the start rather than one * popping up half way through flashing the second lens. While connected, each arm's battery * level is read (they are independent batteries) so the caller can refuse to flash on a low - * charge. + * charge. Before the prompt is shown, the right arm's settings are read once more so a lens + * that reports silent mode (which blanks the display and ignores input) is refused with the + * way out instead of a prompt nobody can see. * * With `skipPrompt` the on-glasses confirmation is not shown: the flow is just connect + auth + * battery read + result(approved). Used to re-check the battery after the user has already @@ -38,6 +40,15 @@ class FlashPromptFlow( const val LIST_CONTAINER_ID = 2 // Index 0 = decline, index 1 = approve. Kept short for the ~50-col grid. val ITEMS = arrayOf("No, cancel", "Yes, flash") + + /** + * Shown instead of a bare "prompt page not acked" error. Silent mode is entered and left + * by the same gesture on the glasses and nothing here can clear it, so the instruction + * has to travel with the error. + */ + const val SILENT_MODE_MESSAGE = + "Your glasses are in silent mode, so they cannot show the confirmation prompt. " + + "Long-press both touchpads on the glasses to leave silent mode, then try again." } private val rightAddress = rightAddress.trim() @@ -121,6 +132,11 @@ class FlashPromptFlow( session.sendPrelude(rightAddress, ": $rightAddress") if (!skipPrompt) { + // Ask before writing a page that cannot be answered: silent mode blanks the + // display and stops the firmware dispatching input while BLE stays up, so the + // create-prompt write is still acked and the user is left waiting on a prompt + // that never appears. + requireNotSilent(rightAddress) showPrompt(rightAddress) startHeartbeat() listener.onState("prompting", "") @@ -238,23 +254,49 @@ class FlashPromptFlow( listener.onBattery(right, left) } - private fun readBattery(address: String, arm: String): Int { + private fun readBattery(address: String, arm: String): Int = readSettingsSnapshot(address, arm)?.battery ?: -1 + + /** + * The sid-0x09 settings read behind both the battery gate and the silent-mode check; one + * ack carries both fields. Two attempts, since on 2.2.9 a request sent right after the + * prelude can be dropped. Null when the arm does not answer or the ack has no battery field. + */ + private fun readSettingsSnapshot(address: String, arm: String): BleProtocol.BatterySnapshot? { var attempt = 0 while (attempt < 2 && !cancelled) { val ack = session.readSettings(address, timings.batteryAckTimeoutMs) if (ack == null) { - emitLog("battery read attempt " + (attempt + 1) + " unacked: " + arm + " arm") + emitLog("settings read attempt " + (attempt + 1) + " unacked: " + arm + " arm") attempt++ continue } val snapshot = BleProtocol.parseSettingsBattery(ack) if (snapshot != null) { - return snapshot.battery + return snapshot } - emitLog("battery read ack had no battery field: $arm arm") + emitLog("settings read ack had no battery field: $arm arm") attempt++ } - return -1 + return null + } + + /** + * Refuse the prompt only when the arm POSITIVELY reports silent mode. An arm that does not + * answer, and firmware whose ack omits the field, both fall through to the prompt exactly as + * before, so a missing answer can never block a flash. + */ + private fun requireNotSilent(address: String) { + val snapshot = readSettingsSnapshot(address, "right") + val state = when { + snapshot == null -> "unknown (arm did not answer)" + snapshot.silentMode < 0 -> "unknown (ack omits the field)" + snapshot.silentMode > 0 -> "on" + else -> "off" + } + emitLog("silent mode before prompt: $state") + if (snapshot != null && snapshot.silentMode > 0) { + throw IllegalStateException(SILENT_MODE_MESSAGE) + } } private fun startHeartbeat() { diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/StockFlowsTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/StockFlowsTest.kt index 9573d705..1506f7e1 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/StockFlowsTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/StockFlowsTest.kt @@ -60,11 +60,12 @@ private fun field(number: Int, text: String): ByteArray = field(number, text.enc private fun authSuccess(magic: Int) = BleProtocol.encodeVarintField(1, 4) + BleProtocol.encodeVarintField(2, magic) + field(3, ByteArray(0)) private fun authPending(magic: Int) = BleProtocol.encodeVarintField(1, 4) + BleProtocol.encodeVarintField(2, magic) + field(3, BleProtocol.encodeVarintField(1, 1)) private fun ack(magic: Int) = BleProtocol.encodeVarintField(1, 1) + BleProtocol.encodeVarintField(2, magic) -private fun settingsAck(magic: Int, battery: Int = -1, left: String = "", right: String = "", extension: String = ""): ByteArray { +private fun settingsAck(magic: Int, battery: Int = -1, left: String = "", right: String = "", extension: String = "", silentMode: Int = -1): ByteArray { var request = ByteArray(0) if (left.isNotEmpty()) request += field(5, left) if (right.isNotEmpty()) request += field(6, right) if (battery >= 0) request += BleProtocol.encodeVarintField(12, battery) + if (silentMode >= 0) request += BleProtocol.encodeVarintField(14, silentMode) var pb = BleProtocol.encodeVarintField(1, 2) + BleProtocol.encodeVarintField(2, magic) + field(4, request) if (extension.isNotEmpty()) pb += field(100, extension) return pb @@ -205,11 +206,11 @@ class StockFlowsTest { override fun onResult(approved: Boolean) { result = approved } } - private fun promptResponder(link: FakeStockLink, selection: Int?, batteries: Map): (FakeStockLink.Written) -> Unit = { w -> + private fun promptResponder(link: FakeStockLink, selection: Int?, batteries: Map, silentMode: Int = -1): (FakeStockLink.Written) -> Unit = { w -> when (w.sid) { BleProtocol.SID_SECURITY_AUTH -> link.notify(w.address, authSuccess(w.magic), w.sid) BleProtocol.PRELUDE_ACK_SID -> link.notify(w.address, ack(BleProtocol.PRELUDE_ACK_MAGIC), w.sid) - BleProtocol.SID_UI_SETTING -> link.notify(w.address, settingsAck(w.magic, batteries[w.address] ?: -1), w.sid) + BleProtocol.SID_UI_SETTING -> link.notify(w.address, settingsAck(w.magic, batteries[w.address] ?: -1, silentMode = silentMode), w.sid) BleProtocol.SID_EVENHUB -> { val cmd = BleProtocol.readVarintFieldValue(BleProtocol.stripTrailingCrc(w.pb), 1, -1) link.notify(w.address, ack(w.magic), w.sid) @@ -275,6 +276,43 @@ class StockFlowsTest { assertEquals("disconnected:Lost connection to the glasses.", events.states.last()) } + @Test + fun promptRefusesOnlyWhenTheLensReportsSilentMode() { + // Silent mode on: refused with the way out, before any page is written. + val silentOn = FakeStockLink() + silentOn.responder = promptResponder(silentOn, 1, mapOf(R to 85), silentMode = 1) + var events = PromptEvents() + FlashPromptFlow(silentOn, R, "", "w", false, events, fast, testPlatform()).run() + assertEquals(null, events.result) + assertEquals("error:${FlashPromptFlow.SILENT_MODE_MESSAGE}", events.states.last()) + assertTrue(events.logs.any { it == "silent mode before prompt: on" }) + assertFalse(silentOn.writes.any { it.sid == BleProtocol.SID_EVENHUB }) + assertTrue(silentOn.closed) + + // Silent mode off: the prompt is shown and answered as usual. + val silentOff = FakeStockLink() + silentOff.responder = promptResponder(silentOff, 1, mapOf(R to 85), silentMode = 0) + events = PromptEvents() + FlashPromptFlow(silentOff, R, "", "w", false, events, fast, testPlatform()).run() + assertEquals(true, events.result, events.logs.joinToString("\n")) + assertTrue(events.logs.any { it == "silent mode before prompt: off" }) + + // Firmware whose ack omits the field: unknown is not a refusal. + val omitted = FakeStockLink() + omitted.responder = promptResponder(omitted, 1, mapOf(R to 85)) + events = PromptEvents() + FlashPromptFlow(omitted, R, "", "w", false, events, fast, testPlatform()).run() + assertEquals(true, events.result, events.logs.joinToString("\n")) + assertTrue(events.logs.any { it == "silent mode before prompt: unknown (ack omits the field)" }) + + // skipPrompt re-checks the battery only; silent mode does not block it. + val skipped = FakeStockLink() + skipped.responder = promptResponder(skipped, null, mapOf(R to 85), silentMode = 1) + events = PromptEvents() + FlashPromptFlow(skipped, R, "", "w", true, events, fast, testPlatform()).run() + assertEquals(true, events.result) + } + // ---- OTA --------------------------------------------------------------- private fun putU32(buf: ByteArray, offset: Int, value: Long) { for (i in 0 until 4) buf[offset + i] = ((value ushr (8 * i)) and 0xff).toByte() } From 9de46878b07a32cdefd445195e0470a96b4debeb Mon Sep 17 00:00:00 2001 From: JMO Date: Fri, 25 Sep 2026 08:22:25 -0700 Subject: [PATCH 09/15] Run the Kotlin tests in CI npm run test:kotlin covers the shared Kotlin (protocol, flash prompt, OTA, compositor) and was not run by any workflow. --- .github/workflows/tests.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6f0f2ce7..dce2bfd9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,3 +17,12 @@ jobs: - uses: actions/checkout@v4 - run: npm ci - run: npm test + + # The shared Kotlin tests (tests/kotlin, on the JVM host). The runner image + # already carries the JDK 17 and Android SDK the build needs. + test-kotlin: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: npm ci + - run: npm run test:kotlin From 668e454fbb619b9fc5c14d89533e8be43613e766 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 25 Sep 2026 14:11:20 -0700 Subject: [PATCH 10/15] Terminal app: Tap host to launch a shell (alternative to app context menu) --- app/apps/terminal/terminal-app.worker.ts | 53 ++++++++++++++++++++---- app/ui/window-menu.ts | 8 ++-- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/app/apps/terminal/terminal-app.worker.ts b/app/apps/terminal/terminal-app.worker.ts index fd110ad5..4dfefaf6 100644 --- a/app/apps/terminal/terminal-app.worker.ts +++ b/app/apps/terminal/terminal-app.worker.ts @@ -2,7 +2,8 @@ * Terminal app, hosted in its own worker thread. Window model: * - "terminal:hub": the window opened from the launcher; shows the list of * live g2mirror sessions across every connected host (grouped by host when - * more than one is connected), and hosts the Manage Connections section + * more than one is connected; selecting a host's heading launches a shell + * there), and hosts the Manage Connections section * where g2mirror:// connections are added, removed, and toggled. * - "terminal:view:N": opened by selecting a session in the hub; each has * its own websocket connection (the protocol allows one attached session @@ -1047,10 +1048,7 @@ function windowMenuItems(window: TerminalWindow): MenuItem[] { label, onSelect: (ctx) => { ctx.stack.pop(); - launchAndOpenView(control, preset).catch((error) => { - // The hub status line also shows the server's error message. - console.warn(`terminal launch ${preset} failed: ${error}`); - }); + launchFromUi(control, preset); }, }); } @@ -1180,7 +1178,10 @@ function applyHistoryReply(window: ViewWindow, reply: { start: number; oldest: n type HubItem = { label: string; - /** Group heading (host name): drawn differently and skipped by selection. */ + /** + * Group heading (host name): drawn unindented and dimmer. Selectable only + * when it has an onSelect (launch a shell on that host). + */ heading?: boolean; /** Session with recent output: an animated indicator marks the row. */ active?: boolean; @@ -1210,7 +1211,7 @@ function hubMenu(window: HubWindow): Menu { function drawHubRow({ image, item, x, y, width, selected }: MenuDrawArgs): void { const font = chromeFont(); if (item.heading) { - image.drawText(font, x, y + HUB_ROW_TEXT_INSET, item.label, 140); + image.drawText(font, x, y + HUB_ROW_TEXT_INSET, item.label, selected ? 255 : 140); return; } // Activity gutter, open-window number column, then the label, truncated @@ -1282,9 +1283,14 @@ function hubSessionItems(window: HubWindow): HubItem[] { ]; const connected = connectedControls(); const multiHost = connected.length > 1; + const canLaunch = launchPresetNames().length > 0; for (const control of connected) { if (multiHost) { - items.push({ label: connectionDisplayName(control.config), heading: true }); + items.push({ + label: connectionDisplayName(control.config), + heading: true, + onSelect: canLaunch ? () => launchOnHost(window, control) : undefined, + }); } const sessions = orderedSessions(window, control); for (const session of sessions) { @@ -1568,6 +1574,37 @@ function launchPresetNames(): string[] { return names; } +/** + * A host heading in the hub was selected: launch a shell there. With a single + * launch preset it starts right away; with several, a submenu titled with the + * host's name picks which. + */ +function launchOnHost(window: HubWindow, control: ControlConnection): void { + const presets = launchPresetNames(); + if (presets.length === 1) { + launchFromUi(control, presets[0]!); + return; + } + windowMenu(window).open( + presets.map((preset) => ({ + label: `Launch ${preset}`, + onSelect: (ctx) => { + ctx.stack.pop(); + launchFromUi(control, preset); + }, + })), + connectionDisplayName(control.config), + ); +} + +/** Launch started from a menu or list row; failures only get logged. */ +function launchFromUi(control: ControlConnection, preset: string): void { + launchAndOpenView(control, preset).catch((error) => { + // The hub status line also shows the server's error message. + console.warn(`terminal launch ${preset} failed: ${error}`); + }); +} + /** Launch a preset on a host and open a view window on the new session. */ async function launchAndOpenView(control: ControlConnection, preset: string): Promise { if (!control.client) throw new Error("Not connected to the g2mirror server."); diff --git a/app/ui/window-menu.ts b/app/ui/window-menu.ts index 599691e9..a06d4165 100644 --- a/app/ui/window-menu.ts +++ b/app/ui/window-menu.ts @@ -92,10 +92,10 @@ export class WindowMenu { /** * Open the menu, or the shell's system menu when this window has no - * entries. `items` overrides the window's own entries for an app that - * reuses this host for another list (a per-row action menu). + * entries. `items` (and optionally `title`) override the window's own for + * an app that reuses this host for another list (a per-row action menu). */ - open(items: MenuItem[] = this.options.items()): void { + open(items: MenuItem[] = this.options.items(), title: string = this.options.title()): void { if (this.stack) return; if (!items.length) { this.options.post({ type: "open-system-menu", windowId: this.options.windowId }); @@ -106,7 +106,7 @@ export class WindowMenu { handleInput: () => {}, }; const stack = new LayerStack(base, { ...noopLayerActions }, this.options.size, this.options.isFocused); - stack.push(new WindowMenuLayer(this.options.title(), items)); + stack.push(new WindowMenuLayer(title, items)); this.stack = stack; } From 459560cd723732d2c29950381a9c66ce0df2fc6b Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 25 Sep 2026 22:06:44 -0700 Subject: [PATCH 11/15] Fix a frozen menu in the notification modal --- app/ui/shell/modal-layer.ts | 12 ++++++++++-- tests/menu-display-list.test.cjs | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/ui/shell/modal-layer.ts b/app/ui/shell/modal-layer.ts index 0e200e7d..9001d0ae 100644 --- a/app/ui/shell/modal-layer.ts +++ b/app/ui/shell/modal-layer.ts @@ -52,11 +52,19 @@ export class ShellModalLayer implements Layer { this.stack.setBaseSize(interior); // Flattening bakes the inner stack's planes (glyphs included) so the blit // below transplants the finished modal content into this layer's plane. - const inner = flattenPlanes(this.stack.paint(), interior); + // The top plane's retained presentations (a menu's selection highlight + // and scroll strip) stay unbaked: they are display lists the glasses + // animate, and baking one freezes it on its first frame. Lower planes' + // are baked so the top plane's raster still covers them. + const planes = this.stack.paint(); + const top = planes[planes.length - 1]!; + const inner = flattenPlanes(planes.map((plane) => plane === top ? { ...plane, image: plane.image.withDrawsBaked(false) } : plane), interior); const rect = modalRect(); + const innerX = rect.x + MODAL_PADDING, innerY = rect.y + MODAL_PADDING; image.fillRect(rect.x, rect.y, rect.width, rect.height, SHELL_OPAQUE_BLACK); image.drawRect(rect.x, rect.y, rect.width, rect.height, 110); - image.bitBlt(inner, rect.x + MODAL_PADDING, rect.y + MODAL_PADDING, { transparentZero: true }); + image.bitBlt(inner, innerX, innerY, { transparentZero: true }); + top.image.copyPresentationsInto(image, innerX + top.x, innerY + top.y); return image; } diff --git a/tests/menu-display-list.test.cjs b/tests/menu-display-list.test.cjs index 2a176bea..fdfe12df 100644 --- a/tests/menu-display-list.test.cjs +++ b/tests/menu-display-list.test.cjs @@ -112,3 +112,21 @@ test('unselected sidebar icons retain -2 depth through shell cropping, including state.selectedIndex=1;const next=new graphics.GrayImage(640,480);chrome.drawSidebar(next,state); assert.equal(next.draws.filter(d=>d.presentation).length,2); }); +test('shell modal carries its inner menu highlight animation instead of baking its first frame',async()=>{ + const {ShellModalLayer}=load('app/ui/shell/modal-layer.ts',{ + '../../graphics/image':graphics,'../../graphics/plane':planes,'../gestures':{},'../layers':load('app/ui/layers.ts',{ + '../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()}, + './gestures':{isDirectionalInput:()=>false}, + }), + './geometry':{appViewportSize:()=>({width:120,height:110}),appViewportRect:()=>({x:10,y:20,width:120,height:110}),SHELL_OPAQUE_BLACK:1}, + }); + const modal=new ShellModalLayer(menu().layer,noopLayerActions),paint=()=>modal.paint({},()=>new graphics.GrayImage(200,200)); + const bare=menu().paint().draws.find(d=>d.presentation),before=paint().draws.find(d=>d.presentation); + assert.equal(before.x,bare.x+28);assert.equal(before.y,bare.y+38); // translated into the box interior + await modal.handleInput({type:'scroll-down'},{}); + const image=paint(),row=image.draws.find(d=>d.presentation); + assert.ok(row.y>before.y);assert.ok(row.presentation.displayList.timeline,'the slide stays a timed display list'); + assert.ok(!image.withDrawsBaked(false).pixels.some(v=>v===255),'selected text is not baked into the modal surface'); + const c=new SurfaceCompositor(200,200);c.setShellScene(encodeShellScene([{image,x:0,y:0,shellKey:9}])); + assert.ok(c.composite().some(v=>v===240)); +}); From efafcb0f0cb81efbd55031d873da06913096692e Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 25 Sep 2026 22:13:02 -0700 Subject: [PATCH 12/15] Fix unit tests --- ...ebug.test.cjs => compass-readout.test.cjs} | 87 +++++++++---------- tests/compass-texture-cache.test.cjs | 1 - 2 files changed, 40 insertions(+), 48 deletions(-) rename tests/{compass-debug.test.cjs => compass-readout.test.cjs} (57%) diff --git a/tests/compass-debug.test.cjs b/tests/compass-readout.test.cjs similarity index 57% rename from tests/compass-debug.test.cjs rename to tests/compass-readout.test.cjs index 215dcc88..7a801134 100644 --- a/tests/compass-debug.test.cjs +++ b/tests/compass-readout.test.cjs @@ -24,12 +24,7 @@ const small = font(18), large = font(32); const diagnostics = { magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 0x8c, sampleTimeMs: 1234 }; function compass(width = 576, height = 260) { - const settings = new Map(); - const debug = load('app/apps/compass/debug.ts', { '../../native/settings-store': { - getBooleanSetting: (key, fallback) => settings.get(key) ?? fallback, - setBooleanSetting: (key, value) => settings.set(key, value), - } }); - let listener, window, renders = 0, pops = 0; + let listener, window; class RecordingImage extends graphics.GrayImage { texts = []; drawText(font, x, y, text, value) { @@ -56,64 +51,62 @@ function compass(width = 576, height = 260) { '../../ui/shell/geometry': { screenCenterInViewportX: () => width / 2 - 32 }, '../../ui/shell/in-process-window': { YieldAtRootLayer: class { constructor(layer) { return layer; } }, - createInProcessWindow: (options) => { window = options; return { requestRender: () => ++renders }; }, + createInProcessWindow: (options) => { window = options; return { requestRender() {} }; }, }, '../../ui/shell/shell': { shell: { isWindowVisible: () => true } }, '../../native/location-permissions': { hasLocationPermission: () => false }, './calibration': calibration, './calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} }, './heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) }, - './debug': debug, }, { setInterval: () => 1, clearInterval() {} }); - const ctx = { stack: { getBaseSize: () => ({ width, height }), pop: () => ++pops } }; - const open = () => app.createCompassAppWindow({ onClosed() {} }); open(); - return { open, debug, settings, menu: () => window.menuItems(), - toggle: () => window.menuItems().find((item) => item.label.startsWith('Debug information:')).onSelect(ctx), + const ctx = { stack: { getBaseSize: () => ({ width, height }), pop() {} } }; + app.createCompassAppWindow({ onClosed() {} }); + return { menu: () => window.menuItems(), paint: () => window.baseLayer.paint(ctx), - heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }), - renders: () => renders, pops: () => pops }; + heading: (info) => listener({ command: 15, headingDegrees: 359, diagnostics: info }) }; } -test('debug toggle defaults off, repaints, closes menu and survives reopening', () => { - const app = compass(); app.heading(diagnostics); - assert.equal(app.debug.isCompassDebugEnabled(), false); - assert.ok(!app.paint().texts.some((t) => t.text.startsWith('Mag:'))); - const before = app.renders(); app.toggle(); - assert.equal(app.debug.isCompassDebugEnabled(), true); - assert.ok(app.renders() > before && app.pops() === 1); - app.open(); assert.equal(app.debug.isCompassDebugEnabled(), true); - app.toggle(); assert.equal(app.debug.isCompassDebugEnabled(), false); +const calibrationText = (image) => image.texts.find((t) => t.text.startsWith('Calibration:')); +const overlaps = (a, b) => a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height; + +test('calibration readiness is always shown and the debug toggle is gone', () => { + const app = compass(); + assert.ok(!app.menu().some((item) => item.label.startsWith('Debug information'))); + assert.equal(calibrationText(app.paint()).text, 'Calibration: --'); + app.heading(diagnostics); + assert.equal(calibrationText(app.paint()).text, 'Calibration: ●●●'); + assert.ok(!app.paint().texts.some((t) => /^(Mag|Anom|Src):/.test(t.text))); }); -test('diagnostics sit to the right of the heading without overlap or clipping', () => { - for (const width of [240, 320, 576, 640]) { - const app = compass(width); app.toggle(); app.heading(diagnostics); +test('calibration dots track firmware magnetic accuracy and never go stale', () => { + const app = compass(); + for (const [accuracy, dots] of [[0, '○○○'], [1, '●○○'], [2, '●●○'], [3, '●●●'], [4, '●●●'], [-1, '--']]) { + app.heading({ ...diagnostics, magneticAccuracy: accuracy }); + assert.equal(calibrationText(app.paint()).text, `Calibration: ${dots}`, `accuracy ${accuracy}`); + } + app.heading(diagnostics); + // Legacy samples (no 0x80 flag) and missing diagnostics must replace the old value. + app.heading({ ...diagnostics, flags: 0 }); + assert.equal(calibrationText(app.paint()).text, 'Calibration: --'); + app.heading(diagnostics); + app.heading(undefined); + assert.equal(calibrationText(app.paint()).text, 'Calibration: --'); +}); + +test('calibration readout fits the viewport without overlapping the heading or status', () => { + for (const [width, height] of [[240, 260], [320, 260], [576, 260], [640, 260], [576, 288], [576, 140]]) { + const app = compass(width, height); app.heading({ ...diagnostics, magneticAccuracy: 2 }); const image = app.paint(); - const heading = image.texts.find((t) => t.text === '359° N'); - for (const text of ['Mag: 3/3', 'Anom: 2', 'Src: RV']) { - const label = image.texts.find((t) => t.text === text); - assert.ok(label, text); - assert.ok(label.x > heading.x + heading.width); - assert.ok(label.x + label.width <= width && label.y + label.height <= image.height); + const label = calibrationText(image); + assert.ok(label.x >= 0 && label.y >= 0, `${width}x${height}`); + assert.ok(label.x + label.width <= width && label.y + label.height <= image.height, `${width}x${height}`); + for (const other of image.texts.filter((t) => t !== label)) { + assert.ok(!overlaps(label, other), `${width}x${height}: overlaps "${other.text}"`); } - const status = image.texts.find((t) => t.text === 'Magnetic heading'); - assert.ok(image.texts.filter((t) => t.text.startsWith('Src:')).every((t) => t.y + t.height < status.y)); - if (process.env.COMPASS_PREVIEW && width === 576) { + if (process.env.COMPASS_PREVIEW && width === 576 && height === 260) { fs.writeFileSync(process.env.COMPASS_PREVIEW, Buffer.concat([ Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels), ])); } } }); - -test('legacy and missing samples replace diagnostics instead of leaving stale values', () => { - const app = compass(); app.toggle(); app.heading(diagnostics); - assert.ok(app.paint().texts.some((t) => t.text === 'Mag: 3/3')); - app.heading(undefined); - assert.ok(app.paint().texts.some((t) => t.text === 'unavailable')); - assert.ok(!app.paint().texts.some((t) => t.text === 'Mag: 3/3')); - assert.deepEqual(Array.from(app.debug.compassDebugLines({ ...diagnostics, flags: 0 })), ['Sample data', 'unavailable']); - for (const [source, name] of [[1, 'GRV'], [2, 'GMRV'], [3, 'RV']]) { - assert.equal(app.debug.compassDebugLines({ ...diagnostics, orientationSource: source })[2], `Src: ${name}`); - } -}); diff --git a/tests/compass-texture-cache.test.cjs b/tests/compass-texture-cache.test.cjs index 8cc9670f..f4c6a3eb 100644 --- a/tests/compass-texture-cache.test.cjs +++ b/tests/compass-texture-cache.test.cjs @@ -47,7 +47,6 @@ const compass = load('app/apps/compass/compass-app.ts', { './calibration': calibration, './calibration-layer': {}, './compass-rose': rose, - './debug': { compassDebugLines: () => [], isCompassDebugEnabled: () => false, setCompassDebugEnabled: () => {} }, './declination': {}, './heading': { getNorthReference: () => 'magnetic', resolveHeading: (n) => ({ displayDegrees: n }) }, }, ` From 54eae0c6b9f870ea37d8eda52912fd1d5c5f653d Mon Sep 17 00:00:00 2001 From: Chris Lee Date: Thu, 17 Sep 2026 19:04:37 -0400 Subject: [PATCH 13/15] Stop leaking every submitted frame's pixel buffer in native memory Passing a JS ArrayBuffer to a Java ByteBuffer parameter leaks it in @nativescript/android 9.0.4 through 9.1.1: JsArgConverter wraps the backing store with NewDirectByteBuffer and takes a JNI global ref that it never deletes. The ref keeps the ByteBuffer reachable, so makeInstanceWeakAndCheckIfAlive always answers "alive", the JS finalizer re-arms, and V8 never frees the ArrayBuffer. submitSurfaceFrame passed a fresh copy of each frame this way, so every frame leaked a full surface (576x260, 640x452 or 640x480 bytes) plus one global ref, including frames that Java then discards as unchanged. The draw-list buffer and buzzer payloads leaked the same way. Each payload now goes through a reused Java direct ByteBuffer (app/native/java-direct-buffer.ts): JS writes into it through an ArrayBuffer.from view, and the Java object itself is passed, which takes NativeScript's ordinary object path. Every consumer (SurfaceCompositor applyAndComposite, playBuzzerSequence) copies before returning, so one buffer per JS context is safe. Covered: the communicator bridge (frames, draw lists, buzzer, which also carries EvenHub tones), the preview-only display, and all eight worker apps. Workers submit through the Android getActiveDisplay() (frames) and worker-buzzer (tones), so the reused buffers live there rather than in each worker; the worker sources are shared with iOS, whose active display takes the ArrayBuffer itself. The smaller one-off crossings (glyph and image atlas registration, the gray preview helper, firmware and file writes) are left as they are. tests/java-direct-buffer.test.cjs covers the helper against a fake ByteBuffer, and the Android worker-buzzer test stubs it. (Rebased onto main by James Babcock: worker changes moved into active-display.ts and worker-buzzer.ts.) Co-Authored-By: Claude Opus 5 Co-Authored-By: Claude Opus 5.5 (1M context) --- app/native/active-display.ts | 42 ++++++++++- app/native/faceclaw-communicator.ts | 21 ++++-- app/native/java-direct-buffer.ts | 95 ++++++++++++++++++++++++ app/native/preview-display.ts | 15 ++-- app/native/worker-buzzer.ts | 7 +- tests/buzzer.test.cjs | 8 +- tests/java-direct-buffer.test.cjs | 111 ++++++++++++++++++++++++++++ tests/tsconfig.json | 1 + 8 files changed, 283 insertions(+), 17 deletions(-) create mode 100644 app/native/java-direct-buffer.ts create mode 100644 tests/java-direct-buffer.test.cjs diff --git a/app/native/active-display.ts b/app/native/active-display.ts index 6ca761e9..104073c7 100644 --- a/app/native/active-display.ts +++ b/app/native/active-display.ts @@ -1,18 +1,54 @@ +import { JavaDirectBuffer } from "./java-direct-buffer"; + declare const com: any; +// Reused Java-side buffers for this isolate's frames: passing a JS ArrayBuffer +// to Java leaks it (java-direct-buffer.ts). Module state is per isolate, so +// each worker gets its own pair. +const framePixelsBuffer = new JavaDirectBuffer(640 * 480); +const frameDrawsBuffer = new JavaDirectBuffer(); + /** - * The Java object a worker isolate submits surface frames to: the live BLE + * The display a worker isolate submits surface frames to: the live BLE * communicator, or the preview-only compositor standing in when no glasses * are paired (both expose the same submitSurfaceFrame signature). Located * through Java statics because those are shared across isolates, unlike any - * JS-side state. + * JS-side state. Null when neither exists. * * Frame submission only. Buzzer effects use worker-buzzer, which routes to * the platform BLE session and skips playback when disconnected. */ -export function getActiveDisplay(): any { +export function getActiveDisplay(): typeof display | null { + return activeJavaDisplay() ? display : null; +} + +function activeJavaDisplay(): any { return ( com.faceclaw.app.FaceclawBleCommunicator.getActive() ?? com.faceclaw.app.FaceclawPreviewCompositor.getActive() ); } + +/** + * Takes the same ArrayBuffer arguments as the iOS active display and copies + * them into the reused Java buffers right before the synchronous Java call. + */ +const display = { + submitSurfaceFrame(buffer: ArrayBuffer, surfaceId: string, x: number, y: number, width: number, height: number, + fingerprint: string, paintMs: number, frameId: number, draws: ArrayBuffer | null = null): void { + const target = activeJavaDisplay(); + if (!target) return; + target.submitSurfaceFrame( + framePixelsBuffer.load(new Uint8Array(buffer)), + surfaceId, + x, + y, + width, + height, + fingerprint, + paintMs, + frameId, + frameDrawsBuffer.loadOptional(draws), + ); + }, +}; diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index 2663195e..878243fc 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -1,6 +1,7 @@ import type { RingInput } from "../g2/ring-input"; import { ImageSource, Utils } from "@nativescript/core"; import * as frameTimings from "./frame-timings"; +import { JavaDirectBuffer } from "./java-direct-buffer"; declare const com: any; @@ -132,6 +133,12 @@ export class FaceclawCommunicatorBridge { private javaCallQueue: Promise = Promise.resolve(); /** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */ private queuedJavaCalls = 0; + // Reused Java-side buffers for byte payloads; passing a JS ArrayBuffer to + // Java leaks it (see java-direct-buffer.ts). Loaded inside the queued call, + // immediately before the synchronous Java call that consumes them. + private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480); + private readonly frameDrawsBuffer = new JavaDirectBuffer(); + private readonly buzzerBuffer = new JavaDirectBuffer(); private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>(); // Recent frame-finished outcomes from the Java side, so waitForFrameFinished // does not race against finishes that land before the wait starts. @@ -594,15 +601,17 @@ export class FaceclawCommunicatorBridge { */ glyphs: ArrayBuffer | null = null, ): Promise { - // Snapshot because the Java call is deferred; the buffer is passed as an - // ArrayBuffer, which NativeScript marshals to a ByteBuffer without the - // ~150ms per-element copy a byte[] parameter would need. + // Snapshot because the Java call is deferred. The bytes reach Java as a + // ByteBuffer (no ~150ms per-element copy, as a byte[] parameter would + // need), but through a reused Java direct buffer rather than by passing + // the ArrayBuffer itself, which NativeScript never frees + // (java-direct-buffer.ts). const snapshot = new Uint8Array(pixels8bpp); // inlineWhenIdle: this is the frame path, the Java side of it measures // ~5ms (composite + pack), and the caller awaits it either way. await this.enqueueJavaCall(() => { this.communicator.submitSurfaceFrame( - snapshot.buffer, + this.framePixelsBuffer.load(snapshot), surfaceId, Math.round(rect.x), Math.round(rect.y), @@ -611,7 +620,7 @@ export class FaceclawCommunicatorBridge { fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), - glyphs, + this.frameDrawsBuffer.loadOptional(glyphs), ); }, true); } @@ -673,7 +682,7 @@ export class FaceclawCommunicatorBridge { async playBuzzerSequence(payload: Uint8Array): Promise { const snapshot = new Uint8Array(payload); await this.enqueueJavaCall(() => { - this.communicator.playBuzzerSequence(snapshot.buffer); + this.communicator.playBuzzerSequence(this.buzzerBuffer.load(snapshot)); }); } diff --git a/app/native/java-direct-buffer.ts b/app/native/java-direct-buffer.ts new file mode 100644 index 00000000..220164de --- /dev/null +++ b/app/native/java-direct-buffer.ts @@ -0,0 +1,95 @@ +/** + * Hands bytes to a Java `java.nio.ByteBuffer` parameter without leaking them. + * + * Why this exists: NativeScript Android (9.0.4 through at least 9.1.1) + * converts a JS ArrayBuffer or typed array passed to a Java method by wrapping + * its backing store in `NewDirectByteBuffer` and taking a JNI global ref that is + * never deleted (JsArgConverter.cpp, `buffer = env.NewGlobalRef(buffer)`). The + * ByteBuffer is also linked to the JS ArrayBuffer as its native counterpart. + * The leaked global ref keeps the ByteBuffer reachable forever, so the + * runtime's GC handshake (Runtime.makeInstanceWeakAndCheckIfAlive) always says + * "alive", the JS finalizer re-arms, and V8 never frees the ArrayBuffer. Every + * call leaked its whole buffer plus one global ref. + * + * On the frame path that meant a full surface (576x260, 640x452 or 640x480 + * bytes) per submitted frame, including frames the Java side then discards as + * unchanged. Measured on a Galaxy Z Fold7, native-heap growth matched frame + * bytes submitted to within a few percent, and Samsung's Heimdall memory + * manager killed the app at about 6.4 GB. ART also aborts the process once + * 51,200 global refs are held, so the ref leak is a second ceiling behind the + * memory one. + * + * The workaround keeps the conversion out of the call: the bytes are copied + * into a Java-allocated direct ByteBuffer that is reused, through an + * ArrayBuffer view made once with `ArrayBuffer.from` (an external backing + * store over the Java memory, no global ref), and the Java ByteBuffer object + * itself is passed. A JS proxy for an existing Java object takes the ordinary + * object path, which does not leak. + * + * Contract: the buffer returned by load() is only valid until the next load() + * on the same instance, so the Java callee must consume it synchronously and + * not keep a reference. The Kotlin consumers (SurfaceCompositor via + * AndroidByteReader, playBuzzerSequence, ShellScene.decode) copy or parse + * before returning, which satisfies this. One instance belongs to one JS + * context (main thread or one worker); instances are not shared across + * isolates. + */ + +declare const java: any; + +/** Minimal surface of java.nio.ByteBuffer this module uses. */ +export interface JavaByteBufferLike { + clear(): unknown; + limit(newLimit: number): unknown; +} + +/** How to allocate a direct buffer and view its memory; injectable for tests. */ +export interface DirectBufferBackend { + allocateDirect(capacity: number): JavaByteBufferLike; + viewOf(buffer: JavaByteBufferLike): Uint8Array; +} + +const nativeScriptBackend: DirectBufferBackend = { + allocateDirect: (capacity) => java.nio.ByteBuffer.allocateDirect(capacity), + viewOf: (buffer) => new Uint8Array((ArrayBuffer as any).from(buffer)), +}; + +/** Capacity grows in steps of this many bytes, so a slowly growing payload does not reallocate every call. */ +const CAPACITY_STEP_BYTES = 4096; + +export class JavaDirectBuffer { + private javaBuffer: JavaByteBufferLike | null = null; + private view: Uint8Array | null = null; + /** Number of Java buffers allocated so far (a test and diagnostics hook). */ + allocations = 0; + + constructor( + private readonly initialCapacity = 0, + private readonly backend: DirectBufferBackend = nativeScriptBackend, + ) {} + + /** + * Copy `bytes` into the reused Java buffer and return it with position 0 and + * limit `bytes.length`, so `remaining()` equals the byte count. Valid until + * the next load() on this instance. + */ + load(bytes: Uint8Array): JavaByteBufferLike { + if (!this.javaBuffer || !this.view || this.view.length < bytes.length) { + const wanted = Math.max(bytes.length, this.initialCapacity, 1); + const capacity = Math.ceil(wanted / CAPACITY_STEP_BYTES) * CAPACITY_STEP_BYTES; + this.javaBuffer = this.backend.allocateDirect(capacity); + this.view = this.backend.viewOf(this.javaBuffer); + this.allocations++; + } + this.view.set(bytes); + this.javaBuffer.clear(); + this.javaBuffer.limit(bytes.length); + return this.javaBuffer; + } + + /** As load(), for an optional ArrayBuffer (e.g. a frame's draw list); null stays null. */ + loadOptional(buffer: ArrayBuffer | null | undefined): JavaByteBufferLike | null { + if (!buffer || buffer.byteLength === 0) return null; + return this.load(new Uint8Array(buffer)); + } +} diff --git a/app/native/preview-display.ts b/app/native/preview-display.ts index 33a226de..7ef1b618 100644 --- a/app/native/preview-display.ts +++ b/app/native/preview-display.ts @@ -1,5 +1,6 @@ import { ImageSource, Utils } from "@nativescript/core"; import { dimFactor256, type FaceclawCommunicatorBridge, type SurfaceOptions } from "./faceclaw-communicator"; +import { JavaDirectBuffer } from "./java-direct-buffer"; declare const com: any; declare const java: any; @@ -49,6 +50,10 @@ function nonNegativeNumber(value: number): number { */ export class PreviewDisplayTarget implements DisplayTarget { private readonly compositor: any; + // Reused Java-side buffers: passing a JS ArrayBuffer to Java leaks it + // (java-direct-buffer.ts). + private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480); + private readonly frameDrawsBuffer = new JavaDirectBuffer(); constructor() { const context = Utils.android.getApplicationContext(); @@ -124,11 +129,11 @@ export class PreviewDisplayTarget implements DisplayTarget { frameId = 0, glyphs: ArrayBuffer | null = null, ): Promise { - // Copy for the same reason the bridge does: the source may be a view on a - // larger buffer, and Java receives the backing ArrayBuffer. - const snapshot = new Uint8Array(pixels8bpp); + // Copied into a reused Java direct buffer, sized to exactly this view's + // bytes; passing the ArrayBuffer itself would leak it + // (java-direct-buffer.ts). this.compositor.submitSurfaceFrame( - snapshot.buffer, + this.framePixelsBuffer.load(pixels8bpp), surfaceId, Math.round(rect.x), Math.round(rect.y), @@ -137,7 +142,7 @@ export class PreviewDisplayTarget implements DisplayTarget { fingerprint, Math.round(nonNegativeNumber(paintMs)), Math.round(nonNegativeNumber(frameId)), - glyphs, + this.frameDrawsBuffer.loadOptional(glyphs), ); // The Java side finishes the frame: the composite is the end of the line, // and worker isolates submit to the same object without coming through here. diff --git a/app/native/worker-buzzer.ts b/app/native/worker-buzzer.ts index d3d3d385..b6730f6a 100644 --- a/app/native/worker-buzzer.ts +++ b/app/native/worker-buzzer.ts @@ -1,12 +1,17 @@ import type { WorkerAppReply } from "../ui/shell/worker-window"; +import { JavaDirectBuffer } from "./java-direct-buffer"; declare const com: any; +// Reused Java-side buffer for this isolate's tone payloads: passing a JS +// ArrayBuffer to Java leaks it (java-direct-buffer.ts). +const buzzerBuffer = new JavaDirectBuffer(); + /** Workers use the main-thread BLE session on iOS and the native queue on Android. */ export function playWorkerBuzzerSequence(payload: Uint8Array): void { if (global.isIOS) { global.postMessage({ type: "buzzer-sequence", payload: Array.from(payload) } satisfies WorkerAppReply); } else { - com.faceclaw.app.FaceclawBleCommunicator.getActive()?.playBuzzerSequence(new Uint8Array(payload).buffer); + com.faceclaw.app.FaceclawBleCommunicator.getActive()?.playBuzzerSequence(buzzerBuffer.load(payload)); } } diff --git a/tests/buzzer.test.cjs b/tests/buzzer.test.cjs index b9d9b20d..ea8e1e91 100644 --- a/tests/buzzer.test.cjs +++ b/tests/buzzer.test.cjs @@ -30,10 +30,14 @@ test('iOS game worker forwards an immutable byte payload through its host and ig test('Android game bridge retains direct native playback and skips a missing communicator', () => { let active = null; const played = []; + // Stands in for the reused Java direct buffer: the payload reaches Java as a + // copy, never as the caller's own ArrayBuffer. + const directBuffer = { JavaDirectBuffer: class { load(bytes) { return { bytes: bytes.slice() }; } } }; const bridge = loader({ global: { isIOS: false }, - com: { faceclaw: { app: { FaceclawBleCommunicator: { getActive: () => active } } } } })('app/native/worker-buzzer.ts'); + com: { faceclaw: { app: { FaceclawBleCommunicator: { getActive: () => active } } } } }, + { './java-direct-buffer': directBuffer })('app/native/worker-buzzer.ts'); bridge.playWorkerBuzzerSequence(payload); - active = { playBuzzerSequence: buffer => played.push(new Uint8Array(buffer)) }; + active = { playBuzzerSequence: buffer => played.push(buffer.bytes) }; const backing = new Uint8Array(payload.length + 2); backing.set(payload, 1); bridge.playWorkerBuzzerSequence(backing.subarray(1, -1)); backing.fill(0); assert.deepEqual(played, [payload]); diff --git a/tests/java-direct-buffer.test.cjs b/tests/java-direct-buffer.test.cjs new file mode 100644 index 00000000..e6112c7c --- /dev/null +++ b/tests/java-direct-buffer.test.cjs @@ -0,0 +1,111 @@ +const test = require("node:test"); +const assert = require("node:assert/strict"); + +const { JavaDirectBuffer } = require("../.test-build/app/native/java-direct-buffer.js"); + +/** + * A stand-in for java.nio.ByteBuffer.allocateDirect: fixed capacity, a + * position and a limit, and the relative bulk get() SurfaceCompositor uses. + * viewOf() plays ArrayBuffer.from: a view over the same memory, not a copy. + */ +function fakeBackend() { + const allocated = []; + return { + allocated, + allocateDirect(capacity) { + const memory = new Uint8Array(capacity); + const buffer = { + memory, + position: 0, + limitValue: capacity, + clear() { + this.position = 0; + this.limitValue = capacity; + return this; + }, + limit(newLimit) { + if (newLimit > capacity) throw new Error("limit past capacity"); + this.limitValue = newLimit; + if (this.position > newLimit) this.position = newLimit; + return this; + }, + remaining() { + return this.limitValue - this.position; + }, + get(dst, offset, length) { + if (length > this.remaining()) throw new Error("BufferUnderflowException"); + dst.set(memory.subarray(this.position, this.position + length), offset); + this.position += length; + return this; + }, + }; + allocated.push(buffer); + return buffer; + }, + viewOf(buffer) { + return buffer.memory; + }, + }; +} + +/** Consume the way SurfaceCompositor.applyAndComposite does: exact remaining(), then drain. */ +function consume(buffer, expectedBytes) { + assert.equal(buffer.remaining(), expectedBytes); + const out = new Uint8Array(expectedBytes); + buffer.get(out, 0, expectedBytes); + return out; +} + +function frame(length, seed) { + const bytes = new Uint8Array(length); + for (let i = 0; i < length; i++) bytes[i] = (i * 31 + seed) & 0xff; + return bytes; +} + +test("a loaded buffer exposes exactly the payload bytes to a Java-style reader", () => { + const backend = fakeBackend(); + const pool = new JavaDirectBuffer(640 * 480, backend); + const shell = frame(640 * 480, 1); + assert.deepEqual(consume(pool.load(shell), shell.length), shell); +}); + +test("successive frames reuse one Java buffer, including smaller ones", () => { + const backend = fakeBackend(); + const pool = new JavaDirectBuffer(640 * 480, backend); + for (let i = 0; i < 50; i++) { + const size = i % 2 === 0 ? 640 * 480 : 640 * 452; + const bytes = frame(size, i); + assert.deepEqual(consume(pool.load(bytes), size), bytes); + } + assert.equal(pool.allocations, 1); + assert.equal(backend.allocated.length, 1); +}); + +test("a payload larger than the buffer allocates once more, then reuses", () => { + const backend = fakeBackend(); + const pool = new JavaDirectBuffer(0, backend); + consume(pool.load(frame(10, 0)), 10); + consume(pool.load(frame(9000, 0)), 9000); + consume(pool.load(frame(8000, 0)), 8000); + consume(pool.load(frame(9000, 0)), 9000); + assert.equal(pool.allocations, 2); +}); + +test("loadOptional passes null through and loads a draw list", () => { + const backend = fakeBackend(); + const pool = new JavaDirectBuffer(0, backend); + assert.equal(pool.loadOptional(null), null); + assert.equal(pool.loadOptional(undefined), null); + assert.equal(pool.loadOptional(new ArrayBuffer(0)), null); + assert.equal(backend.allocated.length, 0); + const draws = frame(36, 7); + assert.deepEqual(consume(pool.loadOptional(draws.buffer), 36), draws); +}); + +test("a view on a larger buffer contributes only its own bytes", () => { + const backend = fakeBackend(); + const pool = new JavaDirectBuffer(0, backend); + const whole = frame(100, 3); + const view = whole.subarray(10, 30); + assert.deepEqual(consume(pool.load(view), 20), whole.slice(10, 30)); +}); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index c24890c2..8ec54367 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -27,6 +27,7 @@ "../app/ui/metrics.ts", "../app/ui/menu-core.ts", "../app/phone-ui/phone-gestures.ts", + "../app/native/java-direct-buffer.ts", "../app/g2/firmware-compat.ts", "../app/g2/glasses-hardware-identity.ts", "../app/g2/ble-proximity.ts", From 8cdee83ff44e52e4f6b8fc89a31c6118055cc8fd Mon Sep 17 00:00:00 2001 From: Chris Lee Date: Thu, 17 Sep 2026 19:04:37 -0400 Subject: [PATCH 14/15] Windows: decide a surface's visibility when the call is queued, not before connect() registers window surfaces in a loop that reads the foreground window once, before the loop, then awaits each surface's configure call before queuing its visibility. On a fresh process restoreOpenApps runs at the same time and focuses windows as it relaunches them. The bridge runs Java calls in order, so a visibility decided before the await lands after the focus change's own calls and undoes them: the old foreground can stay visible and cover the new one, and a later window switch doesn't clear it, because a switch only hides the window being left. configureWindowSurface now also accepts visibility as a function, asked after the configure call, when the visibility call is queued. The display- attach loops (glasses and preview), the display-mode, per-app layout and vertical-position changes, and an in-process window's height-mode change pass one. Launch paths that pass a literal false are unchanged. Co-Authored-By: Claude Opus 5 --- app/g2/dashboard-controller.ts | 38 ++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index cd6fb470..ad7b6137 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -510,7 +510,7 @@ class DashboardController { window.relayout(); await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, ); } else if (window.closeable) { @@ -548,7 +548,7 @@ class DashboardController { void (async () => { for (const window of Array.from(shell.getWindows())) { if (!changed.includes(window.appId)) continue; - await this.configureWindowSurface(window.surfaceId, window.windowId === shell.foregroundWindow()?.windowId, window.heightMode); + await this.configureWindowSurface(window.surfaceId, this.isForegroundWindow(window.windowId), window.heightMode); window.relayout?.(); } shell.foregroundWindow()?.requestRender(); @@ -560,12 +560,11 @@ class DashboardController { const position = verticalPositionSetting.get(); if (position === this.lastVerticalPosition) return; this.lastVerticalPosition = position; - const foregroundWindowId = shell.foregroundWindow()?.windowId; void (async () => { for (const window of shell.getWindows()) { await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, ); } @@ -1249,11 +1248,10 @@ class DashboardController { // loop re-applies the current dim from here. await target.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1); this.appliedUnderlayDim = 1; - const foregroundWindowId = shell.foregroundWindow()?.windowId; for (const window of shell.getWindows()) { await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, target, ); @@ -1538,11 +1536,10 @@ class DashboardController { await communicator.setUnderlayDim(SHELL_SURFACE_Z_ORDER, 1); this.appliedUnderlayDim = 1; await this.configureLockSurface(communicator); - const foregroundWindowId = shell.foregroundWindow()?.windowId; for (const window of shell.getWindows()) { await this.configureWindowSurface( window.surfaceId, - window.windowId === foregroundWindowId, + this.isForegroundWindow(window.windowId), window.heightMode, ); } @@ -2276,8 +2273,7 @@ class DashboardController { reconfigureSurface: (heightMode) => { // Resize the surface rect to the new band; a foreground window stays // visible. The shell re-renders so the chrome (top bar) follows. - const visible = shell.foregroundWindow()?.windowId === windowId; - void this.configureWindowSurface(surfaceId, visible, heightMode); + void this.configureWindowSurface(surfaceId, this.isForegroundWindow(windowId), heightMode); this.requestShellRender(); }, onClosed: () => { @@ -2430,10 +2426,21 @@ class DashboardController { } } - /** Create/refresh a window surface on the compositor, if a display target exists. */ + /** + * Create/refresh a window surface on the compositor, if a display target exists. + * + * `visible` may be a function, asked only after the surface is configured, + * right when the visibility call is queued. Callers that loop over windows + * or resize one in place must pass one (isForegroundWindow): the configure + * call yields, the foreground can change meanwhile, and a visibility decided + * before the await then lands AFTER the focus change's own calls, since the + * bridge runs Java calls in order. At startup, restoreOpenApps focuses + * windows while connect() registers surfaces, so a foreground read before + * that loop left the old window visible and the new foreground hidden. + */ private async configureWindowSurface( surfaceId: string, - visible: boolean, + visible: boolean | (() => boolean), heightMode: WindowHeightMode = "min", // ensurePreviewDisplay passes its not-yet-published target explicitly. target: DisplayTarget | null = this.display, @@ -2444,7 +2451,12 @@ class DashboardController { zOrder: 0, transparency: "opaque", }); - await target.setSurfaceVisible(surfaceId, visible); + await target.setSurfaceVisible(surfaceId, typeof visible === "function" ? visible() : visible); + } + + /** A late answer to "is this window in front?", for configureWindowSurface. */ + private isForegroundWindow(windowId: string): () => boolean { + return () => shell.foregroundWindow()?.windowId === windowId; } private removeWindowSurface(surfaceId: string): void { From 1812818ae2e0515e4dde64667dc52075f1a6d1c5 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 25 Sep 2026 22:28:45 -0700 Subject: [PATCH 15/15] Stop leaking the remaining JS ArrayBuffers passed to Java The same NativeScript conversion leak as the frame path (see java-direct-buffer.ts) applied to every other ArrayBuffer handed to a Java ByteBuffer parameter: - Shell scenes (submitShellScene on the bridge and the preview display), submitted on every shell render, which the frame-path fix predates. These now go through a reused JavaDirectBuffer like frames do. - Glyph, firmware-glyph and image atlas registration, Mapbox image decode, the preview placeholder bitmap, firmware hash/write, binary file writes and the EvenHub privacy-policy download, plus the Kotlin protocol smoke test. These are rare or large, so they cross through a new copyToJavaByteBuffer: a fresh Java direct buffer per call that is freed normally, rather than one pinned for the life of the isolate. Co-Authored-By: Claude Opus 5.5 (1M context) --- app/apps/evenhub/privacy-policy.ts | 4 ++-- app/native/faceclaw-communicator.ts | 6 +++++- app/native/file-access.ts | 8 ++++---- app/native/firmware-files.ts | 9 +++++++-- app/native/gray-image-preview.ts | 11 ++++++----- app/native/image-bytes.ts | 4 +++- app/native/java-direct-buffer.ts | 17 +++++++++++++++++ app/native/kotlin-protocol-smoke.ts | 8 +++++--- app/native/preview-display.ts | 3 ++- app/native/texture-atlas.ts | 12 ++++++++---- tests/compass-texture-cache.test.cjs | 2 +- tests/java-direct-buffer.test.cjs | 13 ++++++++++++- 12 files changed, 72 insertions(+), 25 deletions(-) diff --git a/app/apps/evenhub/privacy-policy.ts b/app/apps/evenhub/privacy-policy.ts index 53fc2681..e5e0c0be 100644 --- a/app/apps/evenhub/privacy-policy.ts +++ b/app/apps/evenhub/privacy-policy.ts @@ -1,5 +1,6 @@ import { Application } from "@nativescript/core"; import { fetchWithUserAgent } from "../../util/http"; +import { copyToJavaByteBuffer } from "../../native/java-direct-buffer"; declare const android: any; declare const androidx: any; @@ -93,8 +94,7 @@ async function downloadAndOpenPdf(url: string, appName: string, activity: any): const file = new java.io.File(directory, `policy-${hash.toString(16)}.pdf`); const stream = new java.io.FileOutputStream(file); try { - const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); - stream.getChannel().write(buffer); + stream.getChannel().write(copyToJavaByteBuffer(bytes)); } finally { stream.close(); } diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index 878243fc..37b5f479 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -138,6 +138,7 @@ export class FaceclawCommunicatorBridge { // immediately before the synchronous Java call that consumes them. private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480); private readonly frameDrawsBuffer = new JavaDirectBuffer(); + private readonly shellSceneBuffer = new JavaDirectBuffer(); private readonly buzzerBuffer = new JavaDirectBuffer(); private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>(); // Recent frame-finished outcomes from the Java side, so waitForFrameFinished @@ -545,7 +546,10 @@ export class FaceclawCommunicatorBridge { */ async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { const snapshot = new Uint8Array(bytes); - await this.enqueueJavaCall(() => this.communicator.submitShellScene(snapshot.buffer, paintMs, frameId), true); + await this.enqueueJavaCall( + () => this.communicator.submitShellScene(this.shellSceneBuffer.load(snapshot), paintMs, frameId), + true, + ); } async setUnderlayDim(belowZOrder: number, factor: number): Promise { diff --git a/app/native/file-access.ts b/app/native/file-access.ts index b79f3d5f..5cf818e5 100644 --- a/app/native/file-access.ts +++ b/app/native/file-access.ts @@ -5,6 +5,7 @@ * reasonable for a sideloaded personal tool). */ import { Utils } from "@nativescript/core"; +import { copyToJavaByteBuffer } from "./java-direct-buffer"; declare const android: any; declare const java: any; @@ -150,10 +151,9 @@ export function writeBinaryFile(path: string, bytes: Uint8Array): boolean { if (parent) parent.mkdirs(); const stream = new java.io.FileOutputStream(file); try { - // An ArrayBuffer marshals to java.nio.ByteBuffer (the same conversion - // submitSurfaceFrame relies on), so write through the channel. - const buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength); - stream.getChannel().write(buffer); + // Write through the channel from a Java direct buffer: a bulk copy, and + // passing the ArrayBuffer itself would leak it (java-direct-buffer.ts). + stream.getChannel().write(copyToJavaByteBuffer(bytes)); } finally { stream.close(); } diff --git a/app/native/firmware-files.ts b/app/native/firmware-files.ts index 59236354..5517af25 100644 --- a/app/native/firmware-files.ts +++ b/app/native/firmware-files.ts @@ -1,3 +1,8 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + declare const com: any -export function firmwareSha256(buffer: ArrayBuffer): string { return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(buffer)) } -export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, buffer) } + +// The image crosses as a fresh Java buffer: passing a JS ArrayBuffer to Java +// leaks it (java-direct-buffer.ts). +export function firmwareSha256(buffer: ArrayBuffer): string { return String(com.faceclaw.app.FaceclawFirmwareUtil.sha256Hex(copyToJavaByteBuffer(new Uint8Array(buffer)))) } +export function writeFirmwareFile(path: string, buffer: ArrayBuffer): void { com.faceclaw.app.FaceclawFirmwareUtil.writeFile(path, copyToJavaByteBuffer(new Uint8Array(buffer))) } diff --git a/app/native/gray-image-preview.ts b/app/native/gray-image-preview.ts index 690c1e4b..fc8b1656 100644 --- a/app/native/gray-image-preview.ts +++ b/app/native/gray-image-preview.ts @@ -1,16 +1,17 @@ import { ImageSource } from "@nativescript/core"; import { type GrayImage } from "../graphics/image"; +import { copyToJavaByteBuffer } from "./java-direct-buffer"; declare const com: any; const PREVIEW_BRIGHTEN_GAMMA = 0.7; /** - * Build the phone-UI preview for a frame. The pixel buffer is passed to Java - * as an ArrayBuffer (marshalled to a ByteBuffer) and expanded to ARGB there; - * doing the per-pixel work in JS or copying element-by-element across the - * bridge costs ~150ms per frame. + * Build the phone-UI preview for a frame. The pixels are copied into a Java + * direct ByteBuffer and expanded to ARGB there; doing the per-pixel work in JS + * or copying element-by-element across the bridge costs ~150ms per frame, and + * passing the ArrayBuffer itself would leak it (java-direct-buffer.ts). */ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { if (!global.isAndroid) { @@ -19,7 +20,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null { const baked = image.withDrawsBaked(); const bitmap = com.faceclaw.app.PreviewBitmapUtil.fromGray( - baked.pixels.buffer, + copyToJavaByteBuffer(baked.pixels), baked.width, baked.height, PREVIEW_BRIGHTEN_GAMMA, diff --git a/app/native/image-bytes.ts b/app/native/image-bytes.ts index 12c7fd5e..cb3f5a0b 100644 --- a/app/native/image-bytes.ts +++ b/app/native/image-bytes.ts @@ -1,7 +1,9 @@ import { grayImageFromPacket } from './image-files' import { type GrayImage } from '../graphics/image' +import { copyToJavaByteBuffer } from './java-direct-buffer' declare const com: any +/** The encoded bytes cross as a fresh Java buffer; passing the ArrayBuffer leaks it (java-direct-buffer.ts). */ export function decodeImageBytes(bytes: ArrayBuffer, width: number, height: number): GrayImage | null { - return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(bytes, width, height)) + return grayImageFromPacket(com.faceclaw.app.ImageFileLoader.loadGrayFromBytes(copyToJavaByteBuffer(new Uint8Array(bytes)), width, height)) } diff --git a/app/native/java-direct-buffer.ts b/app/native/java-direct-buffer.ts index 220164de..b882fd71 100644 --- a/app/native/java-direct-buffer.ts +++ b/app/native/java-direct-buffer.ts @@ -93,3 +93,20 @@ export class JavaDirectBuffer { return this.load(new Uint8Array(buffer)); } } + +/** + * Copy `bytes` into a fresh Java direct buffer (position 0, limit + * `bytes.length`) for a one-off crossing such as an atlas registration or a + * file write. Unlike a JavaDirectBuffer nothing is pinned between calls: the + * Java buffer is an ordinary object, freed once the JS side drops it. + */ +export function copyToJavaByteBuffer( + bytes: Uint8Array, + backend: DirectBufferBackend = nativeScriptBackend, +): JavaByteBufferLike { + // A zero-capacity direct buffer may have no address to view. + const javaBuffer = backend.allocateDirect(Math.max(bytes.length, 1)); + backend.viewOf(javaBuffer).set(bytes); + javaBuffer.limit(bytes.length); + return javaBuffer; +} diff --git a/app/native/kotlin-protocol-smoke.ts b/app/native/kotlin-protocol-smoke.ts index a99cc1e2..062ae21d 100644 --- a/app/native/kotlin-protocol-smoke.ts +++ b/app/native/kotlin-protocol-smoke.ts @@ -1,3 +1,5 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + declare const com: any /** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */ @@ -10,11 +12,11 @@ export function runKotlinProtocolSmokeTest(): void { const compositor = new native.SurfaceCompositor() compositor.configureScreen(3, 2) compositor.configureSurface('test', 0, 0, 3, 2, 0, 0) - const pixels = new Uint8Array([0, 16, 255, 32, 48, 64]) - compositor.submitSurface('test', new native.AndroidByteReader(pixels.buffer), 0, 0, 3, 2, '1') + const pixels = copyToJavaByteBuffer(new Uint8Array([0, 16, 255, 32, 48, 64])) + compositor.submitSurface('test', new native.AndroidByteReader(pixels), 0, 0, 3, 2, '1') assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer') assert(hex(native.BmpUtil.pack4bppFromGray8(compositor.composite().gray, 3, 2)) === '01f02340', 'packing') - const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels.buffer)) + const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels)) assert(id > 0 && native.ImageAtlas.get(id).width === 3, 'image atlas/reused buffer') console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android') } diff --git a/app/native/preview-display.ts b/app/native/preview-display.ts index 7ef1b618..53e3e927 100644 --- a/app/native/preview-display.ts +++ b/app/native/preview-display.ts @@ -54,6 +54,7 @@ export class PreviewDisplayTarget implements DisplayTarget { // (java-direct-buffer.ts). private readonly framePixelsBuffer = new JavaDirectBuffer(640 * 480); private readonly frameDrawsBuffer = new JavaDirectBuffer(); + private readonly shellSceneBuffer = new JavaDirectBuffer(); constructor() { const context = Utils.android.getApplicationContext(); @@ -109,7 +110,7 @@ export class PreviewDisplayTarget implements DisplayTarget { } async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise { - this.compositor.submitShellScene(new Uint8Array(bytes).buffer, paintMs, frameId); + this.compositor.submitShellScene(this.shellSceneBuffer.load(bytes), paintMs, frameId); } async setUnderlayDim(belowZOrder: number, factor: number): Promise { diff --git a/app/native/texture-atlas.ts b/app/native/texture-atlas.ts index f39d97d7..a934ed40 100644 --- a/app/native/texture-atlas.ts +++ b/app/native/texture-atlas.ts @@ -1,17 +1,21 @@ +import { copyToJavaByteBuffer } from './java-direct-buffer' + declare const com: any +// Byte payloads cross as fresh Java buffers: passing a JS ArrayBuffer to Java +// leaks it (java-direct-buffer.ts). + /** Android bridge to the process-wide shared Kotlin atlases. */ export function textureAtlasAvailable(): boolean { return !!global.isAndroid } export function textureFontId(key: string): number { return com.faceclaw.app.GlyphAtlas.fontId(key) } export function registerTextureGlyphs(buffer: ArrayBuffer, aa: boolean): void { - const reader = new com.faceclaw.app.AndroidByteReader(buffer) + const reader = new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer))) if (aa) com.faceclaw.app.GlyphAtlas.registerAa(reader) else com.faceclaw.app.GlyphAtlas.register(reader) } export function registerFirmwareGlyphs(buffer: ArrayBuffer): void { - com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(buffer)) + com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(new Uint8Array(buffer)))) } export function textureImageId(key: string, width: number, height: number, pixels: Uint8Array): number { - const buffer = pixels.byteOffset === 0 && pixels.byteLength === pixels.buffer.byteLength ? pixels.buffer : pixels.slice().buffer - return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(buffer)) + return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(copyToJavaByteBuffer(pixels))) } diff --git a/tests/compass-texture-cache.test.cjs b/tests/compass-texture-cache.test.cjs index f4c6a3eb..740a313e 100644 --- a/tests/compass-texture-cache.test.cjs +++ b/tests/compass-texture-cache.test.cjs @@ -148,7 +148,7 @@ test('heading updates reuse the texture; brightness, layout and clipping changes test('the background registers once and uses a nine-byte image reference on later frames', () => { const registered = []; - const atlas = load('app/native/texture-atlas.ts', {}, '', { + const atlas = load('app/native/texture-atlas.ts', { './java-direct-buffer': { copyToJavaByteBuffer: (bytes) => bytes } }, '', { global: { isAndroid: true }, com: { faceclaw: { app: { AndroidByteReader: class { constructor(buffer) { this.buffer = buffer; } }, ImageAtlas: { ensure: (...args) => { registered.push(args); diff --git a/tests/java-direct-buffer.test.cjs b/tests/java-direct-buffer.test.cjs index e6112c7c..d6d2f67d 100644 --- a/tests/java-direct-buffer.test.cjs +++ b/tests/java-direct-buffer.test.cjs @@ -1,7 +1,7 @@ const test = require("node:test"); const assert = require("node:assert/strict"); -const { JavaDirectBuffer } = require("../.test-build/app/native/java-direct-buffer.js"); +const { JavaDirectBuffer, copyToJavaByteBuffer } = require("../.test-build/app/native/java-direct-buffer.js"); /** * A stand-in for java.nio.ByteBuffer.allocateDirect: fixed capacity, a @@ -109,3 +109,14 @@ test("a view on a larger buffer contributes only its own bytes", () => { const view = whole.subarray(10, 30); assert.deepEqual(consume(pool.load(view), 20), whole.slice(10, 30)); }); + +test("copyToJavaByteBuffer gives each crossing its own exact-length buffer", () => { + const backend = fakeBackend(); + const whole = frame(100, 5); + const first = copyToJavaByteBuffer(whole.subarray(10, 30), backend); + const second = copyToJavaByteBuffer(frame(7, 1), backend); + assert.notEqual(first, second); + assert.deepEqual(consume(first, 20), whole.slice(10, 30)); + assert.deepEqual(consume(second, 7), frame(7, 1)); + assert.equal(consume(copyToJavaByteBuffer(new Uint8Array(0), backend), 0).length, 0); +});