Merge branch 'main' into ring-health-fixes

This commit is contained in:
James Babcock committed 2026-09-18 00:08:02 -07:00
commit edef7138fd
29 files changed
+1089 -234

No files matched your search

+30 -2
View File
@@ -125,7 +125,7 @@ test('synchronous provider errors do not leave a permanently active turn', () =>
assert.equal(session.status, 'failed');
});
function draftEnv({ prepare = async () => true, start = async () => {}, initialStatus = 'Ready' } = {}) {
function draftEnv({ prepare = async () => true, start = async () => {}, stop = () => {}, initialStatus = 'Ready' } = {}) {
const transcripts = new Set(), statuses = new Set(), timers = new Map();
const sent = [];
let starts = 0, stops = 0, mic = false, timerId = 0;
@@ -139,7 +139,7 @@ function draftEnv({ prepare = async () => true, start = async () => {}, initialS
setTimeout(cb) { timers.set(++timerId, cb); return timerId; },
clearTimeout(id) { timers.delete(id); },
});
const draft = new VoiceDraft({ startVoiceCapture: async () => { starts++; await start(); }, stopVoiceCapture: () => { stops++; } }, prepare, () => {}, (text) => sent.push(text));
const draft = new VoiceDraft({ startVoiceCapture: async () => { starts++; await start(); }, stopVoiceCapture: () => { stops++; return stop(); } }, prepare, () => {}, (text) => sent.push(text));
return { draft, sent, starts: () => starts, stops: () => stops, mic: () => mic,
transcript(text, isFinal = false) { for (const cb of [...transcripts]) cb({ text, isFinal }); },
status(status) { for (const cb of [...statuses]) cb({ status }); },
@@ -174,6 +174,34 @@ test('release before permission resolves never starts a microphone', async () =>
assert.deepEqual(env.sent, []);
});
test('chat waits past its fallback deadline for native recognition', async () => {
let complete;
const stopped = new Promise(resolve => { complete = resolve; });
const env = draftEnv({ stop: () => stopped });
await env.draft.start();
env.transcript('first segment');
env.draft.release(); env.timeout();
assert.deepEqual(env.sent, []);
assert.equal(env.draft.phase, 'finishing');
env.transcript('first segment and the rest', true);
complete(); await stopped; env.timeout();
assert.deepEqual(env.sent, ['first segment and the rest']);
});
test('a cancelled chat capture cannot finish a newer capture when native stop resolves', async () => {
let complete;
const stopped = new Promise(resolve => { complete = resolve; });
const env = draftEnv({ stop: () => stopped });
await env.draft.start();
env.draft.release(); env.draft.cancel();
await env.draft.start();
env.transcript('new speech');
complete(); await stopped; env.timeout();
assert.deepEqual(env.sent, []);
assert.equal(env.draft.phase, 'listening');
env.draft.cancel();
});
test('release during async mic startup stops capture after it starts', async () => {
let started;
const env = draftEnv({ start: () => new Promise((resolve) => { started = resolve; }) });
+58
View File
@@ -0,0 +1,58 @@
package com.faceclaw.app;
import java.lang.reflect.*;
import java.util.*;
public class TextureCacheProtocolTest {
static int u16(byte[] b, int p) { return (b[p]&255) | (b[p+1]&255)<<8; }
static int u32(byte[] b, int p) { return u16(b,p) | u16(b,p+2)<<16; }
public static void main(String[] args) throws Exception {
TextureCacheState cache = new TextureCacheState();
byte[] pixels = new byte[255*255];
for (int i=0;i<pixels.length;i++) pixels[i]=(byte)(i%15+1);
ImageAtlas.Entry big = new ImageAtlas.Entry(255,255,pixels);
for (int i=0;i<3;i++) assert cache.ensureImage(i,big)>=0;
ImageAtlas.Entry tiny = new ImageAtlas.Entry(1,1,new byte[]{15});
int imageOffset = cache.ensureImage(100,tiny);
assert imageOffset>65535;
GlyphAtlas.Glyph glyph = new GlyphAtlas.Glyph(1,1,0,1,0,null,new byte[]{15});
int glyphOffset = cache.ensureGlyph(42,'A',glyph);
int table = cache.fontTableOffset(42);
assert table>65535 && glyphOffset==table+96*4;
byte[] memory = new byte[262144];
for (byte[] upload: cache.drainUploadPayloads(3600)) {
assert upload[0]==18;
int pos=1;
while (pos<upload.length) {
int offset=u32(upload,pos), n=u16(upload,pos+4);
System.arraycopy(upload,pos+6,memory,offset,n); pos+=6+n;
}
assert pos==upload.length;
}
assert memory[imageOffset]==1 && memory[imageOffset+2]==0x1f;
assert u32(memory,table+('A'-32)*4)==glyphOffset;
assert !cache.hasPendingUploads();
// Exercise the production planner encoders with offsets above 64 KiB.
Class<?> selected=Class.forName("com.faceclaw.app.TexturePlanner$Selected");
Constructor<?> ctor=selected.getDeclaredConstructors()[0]; ctor.setAccessible(true);
Object image=ctor.newInstance(SurfaceCompositor.ScreenDraw.image(100,321,123),null,tiny,321,15);
Method encode=TexturePlanner.class.getDeclaredMethod("encodeImageDraw",TextureCacheState.class,selected);
encode.setAccessible(true);
byte[] draw=(byte[])encode.invoke(null,cache,image);
assert draw.length==10 && draw[0]==19 && u32(draw,1)==imageOffset;
assert u16(draw,5)==321 && u16(draw,7)==123 && draw[9]==31;
Object letter=ctor.newInstance(SurfaceCompositor.ScreenDraw.glyph(42,'A',321,123,255),glyph,null,321,15);
Method runs=TexturePlanner.class.getDeclaredMethod("buildRuns",TextureCacheState.class,List.class,List.class);
runs.setAccessible(true); List<byte[]> out=new ArrayList<>();
runs.invoke(null,cache,Collections.singletonList(letter),out);
byte[] run=out.get(0);
assert run.length==12 && run[0]==20 && u32(run,1)==table;
assert u16(run,5)==321 && u16(run,7)==123 && run[9]==31 && run[10]==1 && run[11]=='A';
int id=200;
while (cache.ensureImage(id++,big)>=0) {}
int before=cache.usedBytes();
assert cache.ensureImage(id,big)==-1 && cache.usedBytes()==before;
cache.reset(); assert cache.usedBytes()==0 && !cache.hasPendingUploads();
assert cache.ensureImage(id,tiny)==0;
}
}
+3 -2
View File
@@ -47,15 +47,16 @@ test("dismiss hides from any state", () => {
assert.deepEqual(reduceGlance(shown, { type: "dismiss" }, T0), GLANCE_HIDDEN);
});
test("gesture mapping: taps and head-tilt press, long-press holds, double-tap dismisses only a visible board", () => {
test("gesture mapping: taps and head-tilt press, both long-press gestures hold, double-tap dismisses only a visible board", () => {
assert.deepEqual(glanceEventForGesture("click", false), { type: "press" });
assert.deepEqual(glanceEventForGesture("head-tilt", false), { type: "press" });
assert.deepEqual(glanceEventForGesture("long-press", false), { type: "hold" });
assert.deepEqual(glanceEventForGesture("short-then-long-press", false), { type: "hold" });
assert.deepEqual(glanceEventForGesture("short-then-long-press", true), { type: "hold" });
assert.deepEqual(glanceEventForGesture("long-press-release", true), { type: "release" });
assert.equal(glanceEventForGesture("double-click", false), null);
assert.deepEqual(glanceEventForGesture("double-click", true), { type: "dismiss" });
assert.equal(glanceEventForGesture("scroll-up", true), null);
assert.equal(glanceEventForGesture("short-then-long-press", true), null);
});
test("the press timeout is the host's when supplied", () => {
+22
View File
@@ -57,3 +57,25 @@ test("dividers: left edges of the right column, top edges of the bottom row, min
{ x0: 288, y0: 144, x1: 575, y1: 144 },
]);
});
test("2x3 adds a bottom row without moving or resizing the first four slots", () => {
const { SIX_SLOT_LAYOUT } = require("../.test-build/app/apps/glanceboard/layout.js");
assert.equal(SIX_SLOT_LAYOUT.width, 576);
assert.equal(SIX_SLOT_LAYOUT.height, 432);
assert.deepEqual(SIX_SLOT_LAYOUT.slots.slice(0, 4).map((slot) => slot.rect), QUADRANT_LAYOUT.slots.map((slot) => slot.rect));
assert.deepEqual(SIX_SLOT_LAYOUT.slots.slice(4).map((slot) => slot.rect), [
{ x: 0, y: 288, width: 288, height: 144 },
{ x: 288, y: 288, width: 288, height: 144 },
]);
const regions = resolveGlanceRegions(SIX_SLOT_LAYOUT, ["none", "none", "terminal", "none", "terminal", "music"], spans);
assert.deepEqual(regions, [
{ choice: "terminal", rect: { x: 0, y: 144, width: 288, height: 288 }, slots: [2, 4] },
{ choice: "music", rect: { x: 288, y: 288, width: 288, height: 144 }, slots: [5] },
]);
const { slotDividers } = require("../.test-build/app/apps/glanceboard/layout.js");
const bottomDividers = slotDividers(SIX_SLOT_LAYOUT, regions).filter((line) => line.y0 === 288);
assert.deepEqual(bottomDividers, [
{ x0: 288, y0: 288, x1: 288, y1: 431 },
{ x0: 288, y0: 288, x1: 575, y1: 288 },
]);
});
+99
View File
@@ -0,0 +1,99 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
const layout = require('../.test-build/app/apps/glanceboard/layout.js');
function load(file, dependencies) {
const context = { exports: {}, console, require: (name) => {
assert.ok(name in dependencies, name);
return dependencies[name];
} };
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
return context.exports;
}
function harness() {
const values = new Map();
const listeners = new Set();
class Setting {
constructor(options) { Object.assign(this, options); }
get() { return values.get(this.storageKey) ?? this.defaultValue; }
set(value) { values.set(this.storageKey, value); }
}
const config = {
ConfigSettingBoolean: Setting, ConfigSettingEnum: Setting,
onAnySettingChanged: (listener) => { listeners.add(listener); return () => listeners.delete(listener); },
};
const widgets = {
glanceWidgetSpans: (choice) => choice === 'calendar' || choice === 'terminal',
findGlanceWidget: () => ({ create: () => ({ start() {}, stop() {}, paint() {} }) }),
};
const settings = load('app/apps/glanceboard/glanceboard-settings.ts', {
'../../ui/dashboard-settings': config, './layout': layout, './widgets': widgets,
});
class GrayImage {
constructor(width, height) { this.width = width; this.height = height; }
drawLine() {}
composeInto() {}
}
const { GlanceBoard } = load('app/apps/glanceboard/board.ts', {
'../../graphics/image': { GrayImage }, '../../ui/dashboard-settings': config,
'./glanceboard-settings': settings, './layout': layout, './widgets': widgets,
});
return { settings, GlanceBoard, values, listeners, notify: () => [...listeners].forEach((listener) => listener()) };
}
test('layout defaults to 2x2; expanding preserves stored slots and remembers the extra row', () => {
const { settings: s, values } = harness();
values.set('glanceboard.quadrants.slot.0', 'compass');
assert.equal(s.glanceLayoutSetting.get(), '2x2');
assert.equal(s.glanceLayout(), layout.QUADRANT_LAYOUT);
s.glanceLayoutSetting.set('2x3');
assert.equal(s.glanceLayout(), layout.SIX_SLOT_LAYOUT);
assert.deepEqual(Array.from(s.glanceSlotSettings(), (slot) => slot.get()),
['compass', 'calendar', 'music', 'calendar', 'none', 'none']);
s.glanceSlotSettings()[5].set('nightscout');
s.glanceLayoutSetting.set('2x2');
assert.equal(s.glanceSlotSettings().length, 4);
s.glanceLayoutSetting.set('2x3');
assert.equal(s.glanceSlotSettings()[5].get(), 'nightscout');
});
test('a spanning widget in the middle row keeps only one adjacent partner', () => {
const { settings: s } = harness();
s.glanceLayoutSetting.set('2x3');
const slots = s.glanceSlotSettings();
for (const index of [0, 2, 4]) slots[index].set('terminal');
s.clearConflictingSlots(s.glanceLayout(), 2, 'terminal');
assert.deepEqual(Array.from(slots, (slot) => slot.get()),
['terminal', 'calendar', 'terminal', 'calendar', 'none', 'none']);
slots[4].set('terminal');
s.clearConflictingSlots(s.glanceLayout(), 4, 'terminal');
assert.equal(slots[0].get(), 'none');
assert.equal(slots[2].get(), 'terminal');
});
test('an active empty board resizes and requests a frame when the layout changes', () => {
const h = harness();
for (const slot of h.settings.glanceSlotSettings(layout.SIX_SLOT_LAYOUT)) slot.set('none');
let renders = 0;
const board = new h.GlanceBoard(() => renders++);
board.start();
assert.equal(board.paint().height, 288);
h.settings.glanceLayoutSetting.set('2x3');
h.notify();
assert.equal(board.paint().width, 576);
assert.equal(board.paint().height, 432);
assert.equal(renders, 1);
h.settings.glanceLayoutSetting.set('2x2');
h.notify();
assert.equal(board.paint().height, 288);
assert.equal(renders, 2);
board.stop();
assert.equal(h.listeners.size, 0);
});
+23
View File
@@ -0,0 +1,23 @@
const test = require('node:test');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { execFileSync } = require('node:child_process');
test('256 KiB cache uploads and planner draws preserve wide image and glyph offsets', () => {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'faceclaw-texture-java-'));
const javaBin = (name) => process.env.JAVA_HOME ? path.join(process.env.JAVA_HOME, 'bin', name) : name;
const root = path.join(__dirname, '../App_Resources/Android/src/main/java/com/faceclaw/app');
try {
const clock = path.join(directory, 'SystemClock.java');
const log = path.join(directory, 'Log.java');
fs.writeFileSync(clock, 'package android.os; public class SystemClock { public static long elapsedRealtime() { return 0; } }');
fs.writeFileSync(log, 'package android.util; public class Log { public static int i(String tag, String msg) { return 0; } }');
const sources = ['GlyphAtlas.java', 'ImageAtlas.java', 'FwGlyphAtlas.java', 'SurfaceCompositor.java',
'util/BmpUtil.java', 'util/CollectionUtils.java', 'g2protocol/BleProtocol.java',
'g2protocol/BleImageOptimizer.java', 'g2protocol/TextureCacheState.java', 'g2protocol/TexturePlanner.java'];
execFileSync(javaBin('javac'), ['-d', directory, ...sources.map(file => path.join(root, file)),
clock, log, path.join(__dirname, 'fixtures/TextureCacheProtocolTest.java')], { stdio: 'pipe' });
execFileSync(javaBin('java'), ['-ea', '-cp', directory, 'com.faceclaw.app.TextureCacheProtocolTest'], { stdio: 'pipe' });
} finally { fs.rmSync(directory, { recursive: true, force: true }); }
});
+166
View File
@@ -0,0 +1,166 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');
function load(file, deps, globals = {}) {
const exports = {};
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, { exports, console, ...globals, require: name => deps[name] ?? {} });
return exports;
}
function dialog({ autoSend = false, deferred = true, synchronousFinal } = {}) {
const listeners = new Set(), timers = new Map(), sent = [], refinements = [];
let complete, timerId = 0, starts = 0, frame;
let completion = new Promise(resolve => { complete = resolve; });
const { VoiceInputLayer } = load('app/ui/shell/voice-input.ts', {
'../../native/voice-control': { voiceControlBridge: {
onTranscript(cb) { listeners.add(cb); return () => listeners.delete(cb); },
onStatus() { return () => {}; },
} },
'../../native/anthropic': { refineDictation(options) { refinements.push(options); return { cancel() {} }; } },
'../dashboard-settings': { anthropicApiKeySetting: { get: () => 'key' } },
'../gestures': { gestureHints: () => '' },
'./input-dialog': { paintInputDialog(_image, options) { frame = options; } },
}, {
setTimeout(cb) { const id = ++timerId; timers.set(id, cb); return id; },
clearTimeout(id) { timers.delete(id); },
});
const transcript = (text, isFinal = false) => { for (const cb of [...listeners]) cb({ text, isFinal }); };
const layer = new VoiceInputLayer({ autoSend, onClosed() {}, dismiss: () => layer.onRemoved(),
actions: { requestRender() {}, startVoiceCapture() { starts++; }, stopVoiceCapture() {
if (synchronousFinal) transcript(synchronousFinal, true);
return deferred ? completion : undefined;
} },
sendTargets: [{ id: 'send', label: 'Send', onSend: text => sent.push(text) }],
});
layer.startCapture();
return { layer, sent, refinements, transcript, starts: () => starts,
input: type => layer.handleInput({ type }, {}),
frame() { layer.paint({}, () => ({})); return frame; },
timeout() { for (const [id, cb] of [...timers]) { timers.delete(id); cb(); } },
async complete() { complete(); await completion; await Promise.resolve(); },
nextCapture() { completion = new Promise(resolve => { complete = resolve; }); },
timers: () => timers.size,
};
}
for (const partial of ['', 'Set a timer']) {
test(`auto-send waits for slow native recognition with ${partial ? 'a committed segment' : 'no partial'}`, async () => {
const env = dialog({ autoSend: true });
if (partial) env.transcript(partial);
env.layer.endCapture();
env.timeout(); // well beyond the old fallback deadline
assert.deepEqual(env.sent, []);
assert.equal(env.timers(), 0);
env.transcript('Set a timer for five minutes', true);
await env.complete(); env.timeout();
assert.deepEqual(env.sent, ['Set a timer for five minutes']);
assert.equal(env.timers(), 0);
});
}
async function continuation() {
const env = dialog();
env.layer.endCapture();
env.transcript('Set a timer for five minutes', true);
await env.complete();
env.nextCapture();
env.input('scroll-down'); env.input('click'); // Continue
env.input('click'); // stop follow-up
return env;
}
test('Continue refines the complete late result instead of appending an editing instruction', async () => {
const env = await continuation();
env.timeout();
assert.equal(env.refinements.length, 0);
env.transcript('Change five to ten', true);
await env.complete(); env.timeout();
assert.equal(env.refinements.length, 1);
assert.equal(env.refinements[0].original, 'Set a timer for five minutes');
assert.equal(env.refinements[0].followup, 'Change five to ten');
});
test('Send and Continue cannot consume partial text while native recognition is pending', async () => {
const env = dialog();
env.transcript('Set a timer'); env.layer.endCapture();
assert.equal(env.frame().status, 'Finishing transcription...');
env.input('click'); env.input('scroll-down'); env.input('click');
assert.deepEqual(env.sent, []);
assert.equal(env.starts(), 1);
env.transcript('Set a timer for five minutes', true); await env.complete();
env.input('scroll-up'); env.input('click');
assert.deepEqual(env.sent, ['Set a timer for five minutes']);
});
test('cancelling a pending continuation ignores its late transcript', async () => {
const env = await continuation();
env.input('double-click');
env.transcript('Change five to ten', true); await env.complete(); env.timeout();
assert.equal(env.refinements.length, 0);
assert.equal(env.frame().text, 'Set a timer for five minutes');
});
test('closing while native recognition is pending never schedules a fallback or sends', async () => {
const env = dialog({ autoSend: true });
env.layer.endCapture(); env.layer.onRemoved();
env.transcript('late', true); await env.complete(); env.timeout();
assert.deepEqual(env.sent, []);
assert.equal(env.timers(), 0);
});
test('failed or empty native captures return to the menu without sending', async () => {
const env = dialog({ autoSend: true });
env.layer.endCapture(); await env.complete(); env.timeout();
assert.deepEqual(env.sent, []);
assert.equal(env.frame().status, 'Send, continue, or discard?');
});
test('cloud fallback and a synchronous final still send exactly once', () => {
for (const synchronousFinal of [undefined, 'complete']) {
const env = dialog({ autoSend: true, deferred: false, synchronousFinal });
env.transcript('partial'); env.layer.endCapture(); env.timeout();
assert.deepEqual(env.sent, [synchronousFinal ?? 'partial']);
}
});
test('native completion is capture-specific, follows the final, and does not wait for shared capture', async () => {
let controller;
class Controller {
constructor() { controller = this; }
setListener(listener) { this.listener = listener; }
setCommunicator() {} setUsePhoneMic() {} setSaveRecordings() {} setEndpointing() {}
setNoiseSuppression() {} setBeamFilter() {} clearSpeakerVerification() {} setOnboardModelKind() {}
start(_mode, id) { this.captureId = id; this.active = true; }
stop() { this.active = false; }
isCapturing() { return this.active; }
}
const { FaceclawVoiceControlBridge } = load('app/native/voice-control.ts', {
'@nativescript/core': { Utils: { android: { getApplicationContext: () => ({}) } } },
'./speech-pause': { SpeechPauseDetector: class { reset() {} } },
}, { global: { isAndroid: true }, com: { faceclaw: { app: {
FaceclawVoiceController: Controller,
FaceclawVoiceControllerListener: function (listener) { return listener; },
} } } });
const bridge = new FaceclawVoiceControlBridge();
const options = { provider: 'onboard-whisper' };
const events = [];
bridge.onTranscript(event => events.push(event.text));
bridge.startPushToTalk(options);
const stopped = bridge.stopPushToTalk().then(() => events.push('stopped'));
controller.listener.onStopped(0); // an older raw/cloud capture's callback
await Promise.resolve(); assert.deepEqual(events, []);
controller.listener.onTranscript('complete', true);
assert.deepEqual(events, ['complete']);
controller.listener.onStopped(controller.captureId); await stopped;
assert.deepEqual(events, ['complete', 'stopped']);
bridge.startContinuousCapture(options); bridge.startPushToTalk(options);
assert.equal(bridge.stopPushToTalk(), undefined);
assert.equal(controller.active, true);
bridge.stopContinuousCapture(); controller.listener.onStopped(controller.captureId);
});