Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c4b71cc
fix(android): rebind the test IME when its commit went to a stale inp…
okwasniewski Sep 29, 2026
7febfb4
fix(android): confirm the IME rebind and cover a pre-filled field
okwasniewski Sep 29, 2026
153e257
refactor(android): the IME retry preparation is its own step
okwasniewski Sep 29, 2026
8dd4668
fix(android): keep test IME ownership when the rebind cannot confirm …
thymikee Sep 30, 2026
bad3a6c
test(android): a fill that leaves a pre-filled field on its hint fails
thymikee Sep 30, 2026
f7e1e04
fix(android): serialize the IME rebind and record it on the device
thymikee Sep 30, 2026
580217b
fix(android): a canceled request is not an unconfirmed IME rebind
thymikee Sep 30, 2026
ad5683c
test(android): the IME lifecycle fake answers the rebind record
thymikee Sep 30, 2026
a258fbc
fix(android): fail closed when the IME rebind record cannot be read o…
thymikee Sep 30, 2026
63d6030
Merge branch 'main' into oskar/ime-rebinds-stale-input-session
thymikee Oct 2, 2026
d7f97bd
refactor(android): model the IME rebind as its own transaction over o…
thymikee Oct 2, 2026
f84df40
refactor(android): split the activation's restore-target persistence …
thymikee Oct 2, 2026
f12e917
test(android): the startup-race device double answers the rebind record
thymikee Oct 2, 2026
6622d34
fix(android): load the IME rebind on demand to keep the mechanics clo…
thymikee Oct 2, 2026
63f0ced
fix(android): fail closed on incomplete IME rebinds
thymikee Oct 6, 2026
b9b2d06
Merge remote-tracking branch 'origin/main' into fix/3061-ime-rebind
thymikee Oct 6, 2026
937eea9
refactor(android): simplify IME recovery ownership and tests
thymikee Oct 6, 2026
a8acc11
refactor(android): reduce IME lifecycle branching
thymikee Oct 6, 2026
b973ec7
fix(android): retain rebind record cleanup diagnostics
thymikee Oct 6, 2026
eef741a
test(android): synchronize competing IME restore
thymikee Oct 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/platform-android/src/__tests__/ime-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { bindAndroidAdbTestHost } from './test-utils/android-host-test-setup.ts'

const HELPER_SERVICE = 'com.callstack.agentdevice.imehelper/.TestInputMethodService';
const SETTINGS_KEY = 'agent_device_ime_helper_previous_ime';
const REBIND_DISPLACED_KEY = 'agent_device_ime_helper_rebind_displaced';
const PENDING_DIR = 'android-test-ime-pending';

// activateAndroidTestIme reads the bundled artifact for the service component; inject a fixture so
Expand Down Expand Up @@ -92,6 +93,7 @@ function ok(stdout = ''): FakeAdbResult {
function fakeDeviceState(initialIme: string) {
let defaultIme = initialIme;
let previousImeRecord: string | undefined;
let rebindDisplacedRecord: string | undefined;
let installed = false;
let failPersist = false;
let mismatchNextPersistReadback = false;
Expand Down Expand Up @@ -123,10 +125,12 @@ function fakeDeviceState(initialIme: string) {
return ok('com.example.stale/.Ime');
}
if (key === SETTINGS_KEY) return ok(previousImeRecord ?? 'null');
if (key === REBIND_DISPLACED_KEY) return ok(rebindDisplacedRecord ?? 'null');
throw new Error(`unexpected settings get key: ${String(key)}`);
}

function handleSettingsPut(args: readonly string[]): FakeAdbResult {
if (args[4] === REBIND_DISPLACED_KEY) rebindDisplacedRecord = args[5];
if (args[4] === SETTINGS_KEY) {
if (failPersist) return { exitCode: 1, stdout: '', stderr: 'rejected' };
previousImeRecord = args[5];
Expand All @@ -140,6 +144,7 @@ function fakeDeviceState(initialIme: string) {

function handleSettingsDelete(args: readonly string[]): FakeAdbResult {
if (args[4] === SETTINGS_KEY) previousImeRecord = undefined;
if (args[4] === REBIND_DISPLACED_KEY) rebindDisplacedRecord = undefined;
return ok();
}

Expand Down
27 changes: 27 additions & 0 deletions packages/platform-android/src/__tests__/text-input-fill.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,33 @@ test('fillAndroid returns target-bound unconfirmed evidence when an input mask r
assert.equal(calls.length, 1, 'an ambiguous committed fill must not be repeated');
});

test('fillAndroid fails, not unconfirmed, when a fill leaves a pre-filled field on its hint', async () => {
let hintShowing = false;
await withAndroidAdbProvider(
{
snapshotHelperArtifact: ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT,
exec: createAndroidSnapshotHelperExecutor({
exec: async (args) => {
throw new Error(`unexpected adb call: ${args.join(' ')}`);
},
captureXml: () =>
hintShowing
? `<?xml version="1.0" encoding="UTF-8"?><hierarchy><node package="com.example" class="android.widget.EditText" text="Phone" hint="Phone" hint-showing="true" resource-id="com.example:id/phone" focused="true" bounds="[0,0][200,100]"/></hierarchy>`
: phoneInputXml('12 123 4567'),
}),
text: async () => {
hintShowing = true;
},
},
{ serial: ANDROID_EMULATOR.id },
async () => {
await assert.rejects(fillAndroid(ANDROID_EMULATOR, 100, 50, '0501234567'), {
code: 'COMMAND_FAILED',
});
},
);
});

test('fillAndroid waits for settled app text before reporting success', async () => {
let typed = '';
let dumpCount = 0;
Expand Down
214 changes: 214 additions & 0 deletions packages/platform-android/src/__tests__/text-input-test-ime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ import {
import { fillAndroid, typeAndroid } from '../text-input.ts';
import { withAndroidAdbProvider, type AndroidAdbExecutor } from '../adb-executor.ts';
import {
isAndroidTestImeActive,
resetAndroidTestImeActivationCacheForTests,
restoreAndroidTestIme,
setAndroidTestImeActiveForTests,
} from '../ime-lifecycle.ts';
import { fakeImeDeviceAdb, type FakeImeDeviceState } from '../ime-device.fixtures.ts';
import { getAndroidTestImeOwnership } from '../ime-state.ts';
import {
readAndroidTestImeRecoveryMarkers,
writeAndroidTestImeRecoveryMarker,
} from '../ime-recovery-marker.ts';

afterEach(() => {
resetAndroidTestImeActivationCacheForTests();
Expand Down Expand Up @@ -230,6 +238,7 @@ test('fillAndroid re-focuses the target when the first helper attempt fails veri
const adb: AndroidAdbExecutor = createAndroidSnapshotHelperExecutor({
exec: async (args) => {
calls.push(args);
if (args[1] === 'settings' && args[2] === 'get') return helperSelected(args);
if (args[1] === 'am' && args[2] === 'broadcast') {
const action = args[args.indexOf('-a') + 1];
if (action === 'com.callstack.agentdevice.imehelper.ACTION_CLEAR_TEXT') {
Expand Down Expand Up @@ -257,6 +266,11 @@ test('fillAndroid re-focuses the target when the first helper attempt fails veri
);

assert.equal(currentText, 'filed the expense');
assert.deepEqual(
calls.filter((args) => args[1] === 'ime').map((args) => args[2]),
['disable', 'enable', 'set'],
'a commit that left the field on its old value may have gone to a stale session',
);
assert.equal(
calls.filter((args) => args[1] === 'input' && args[2] === 'tap').length,
2,
Expand All @@ -270,6 +284,197 @@ test('fillAndroid re-focuses the target when the first helper attempt fails veri
);
});

test('fillAndroid rebinds the helper IME before retrying a commit that left the field on its hint', async () => {
setAndroidTestImeActiveForTests(ANDROID_EMULATOR, true);
let rebound = false;
let currentText = '';
const calls: (readonly string[])[] = [];
const adb: AndroidAdbExecutor = createAndroidSnapshotHelperExecutor({
exec: async (args) => {
calls.push(args);
if (args[1] === 'settings' && args[2] === 'get') return helperSelected(args);
if (args[1] === 'ime' && args[2] === 'set') rebound = true;
if (args[1] === 'am' && args[2] === 'broadcast') {
const action = args[args.indexOf('-a') + 1];
// A stale input session: the app drops every commit until the IME is rebound.
if (rebound && action === 'com.callstack.agentdevice.imehelper.ACTION_INPUT_TEXT_B64') {
currentText += decodeBroadcastText(args);
}
}
return { exitCode: 0, stdout: '', stderr: '' };
},
captureXml: () =>
currentText
? androidInputXml({ text: currentText })
: `<?xml version="1.0" encoding="UTF-8"?><hierarchy><node package="com.example" class="android.widget.EditText" text="e.g. Jane" hint="e.g. Jane" hint-showing="true" focused="true" bounds="[0,0][200,100]"/></hierarchy>`,
});

await withAndroidAdbProvider(
{ exec: adb, snapshotHelperArtifact: ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT },
{ serial: ANDROID_EMULATOR.id },
async () => {
await fillAndroid(ANDROID_EMULATOR, 10, 10, 'Jane');
},
);

assert.equal(currentText, 'Jane');
const imeVerbs = calls.filter((args) => args[1] === 'ime').map((args) => args[2]);
assert.deepEqual(imeVerbs, ['disable', 'enable', 'set']);
const commands = calls.map((args) => `${args[1]} ${args[2]}`);
const lastImeCall = commands.lastIndexOf('ime set');
const retryTap = commands.lastIndexOf('input tap');
assert.ok(lastImeCall < retryTap, 'the rebind comes before the retry re-focuses the field');
});

test('fillAndroid does not take a pre-filled field left on its hint for app formatting', async () => {
setAndroidTestImeActiveForTests(ANDROID_EMULATOR, true);
let rebound = false;
let currentText = 'old name';
const adb: AndroidAdbExecutor = createAndroidSnapshotHelperExecutor({
exec: async (args) => {
if (args[1] === 'settings' && args[2] === 'get') return helperSelected(args);
if (args[1] === 'ime' && args[2] === 'set') rebound = true;
const action = args[args.indexOf('-a') + 1];
// The clear lands, then the session goes stale and drops the commit until the rebind.
if (action === 'com.callstack.agentdevice.imehelper.ACTION_CLEAR_TEXT') currentText = '';
if (rebound && action === 'com.callstack.agentdevice.imehelper.ACTION_INPUT_TEXT_B64') {
currentText += decodeBroadcastText(args);
}
return { exitCode: 0, stdout: '', stderr: '' };
},
captureXml: () =>
currentText
? androidInputXml({ text: currentText })
: `<?xml version="1.0" encoding="UTF-8"?><hierarchy><node package="com.example" class="android.widget.EditText" text="e.g. Jane" hint="e.g. Jane" hint-showing="true" focused="true" bounds="[0,0][200,100]"/></hierarchy>`,
});

const result = await withAndroidAdbProvider(
{ exec: adb, snapshotHelperArtifact: ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT },
{ serial: ANDROID_EMULATOR.id },
async () => await fillAndroid(ANDROID_EMULATOR, 10, 10, 'Jane'),
);

assert.equal(result, undefined, 'a verified fill, not unconfirmed evidence');
assert.equal(currentText, 'Jane');
});

test('fillAndroid stops on an unconfirmed rebind and leaves the helper for close-time restore', async () => {
const stateDir = await mkdtempForTest('agent-device-ime-rebind-');
setAndroidTestImeActiveForTests(ANDROID_EMULATOR, true, stateDir);
await writeAndroidTestImeRecoveryMarker(stateDir, ANDROID_EMULATOR.id);
const device: FakeImeDeviceState = {
settings: new Map([
['default_input_method', 'com.callstack.agentdevice.imehelper/.TestInputMethodService'],
['agent_device_ime_helper_previous_ime', 'com.samsung/.Keyboard'],
]),
// `settings get` timing out under load: the rebind cannot read back which IME is selected.
inputMethodReadFails: true,
};
const deviceAdb = fakeImeDeviceAdb(device);
let commits = 0;
const adb: AndroidAdbExecutor = createAndroidSnapshotHelperExecutor({
exec: async (args) => {
if (args[1] === 'settings' || args[1] === 'ime') return await deviceAdb(args);
if (args.includes('com.callstack.agentdevice.imehelper.ACTION_INPUT_TEXT_B64')) commits += 1;
return { exitCode: 0, stdout: '', stderr: '' };
},
captureXml: () =>
`<?xml version="1.0" encoding="UTF-8"?><hierarchy><node package="com.example" class="android.widget.EditText" text="e.g. Jane" hint="e.g. Jane" hint-showing="true" focused="true" bounds="[0,0][200,100]"/></hierarchy>`,
});

await withAndroidAdbProvider(
{ exec: adb, snapshotHelperArtifact: ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT },
{ serial: ANDROID_EMULATOR.id },
async () => {
await assert.rejects(fillAndroid(ANDROID_EMULATOR, 10, 10, 'Jane'), {
code: 'COMMAND_FAILED',
});
},
);

assert.equal(commits, 1, 'no commit goes to an IME the rebind could not confirm');
assert.equal(isAndroidTestImeActive(ANDROID_EMULATOR), true, 'ownership stays with restore');
assert.equal(
device.settings.get('agent_device_ime_helper_previous_ime'),
'com.samsung/.Keyboard',
'the rebind leaves the durable restore record alone',
);

device.inputMethodReadFails = false;
const restored = await withAndroidAdbProvider(
{ exec: adb, snapshotHelperArtifact: ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT },
{ serial: ANDROID_EMULATOR.id },
async () => await restoreAndroidTestIme(ANDROID_EMULATOR, { stateDir }),
);

assert.equal(restored.reason, 'ok');
assert.equal(device.settings.get('default_input_method'), 'com.samsung/.Keyboard');
assert.deepEqual(await readAndroidTestImeRecoveryMarkers(stateDir), []);
});

test('typeAndroid never broadcasts into a helper whose rebind went unconfirmed', async () => {
setAndroidTestImeActiveForTests(ANDROID_EMULATOR, true);
const ownership = getAndroidTestImeOwnership(ANDROID_EMULATOR);
assert.ok(ownership);
ownership.rebindUnconfirmed = true;
const device: FakeImeDeviceState = {
settings: new Map([['default_input_method', 'com.android.inputmethod.latin/.LatinIME']]),
imeSetFails: true,
};
const deviceAdb = fakeImeDeviceAdb(device);
const broadcasts: (readonly string[])[] = [];

await withAndroidAdbProvider(
async (args) => {
if (args[1] === 'am') {
broadcasts.push(args);
return { exitCode: 0, stdout: '', stderr: '' };
}
return await deviceAdb(args);
},
{ serial: ANDROID_EMULATOR.id },
async () => {
await assert.rejects(
typeAndroid(ANDROID_EMULATOR, 'Jane'),
(error: { code?: string; details?: { reason?: string } }) =>
error.code === 'COMMAND_FAILED' &&
error.details?.reason === 'android_test_ime_rebind_unconfirmed',
);
},
);

assert.deepEqual(broadcasts, []);
assert.equal(device.settings.get('agent_device_ime_helper_rebind_displaced'), '1');
});

test('typeAndroid rebinds an unconfirmed helper before it broadcasts', async () => {
setAndroidTestImeActiveForTests(ANDROID_EMULATOR, true);
const ownership = getAndroidTestImeOwnership(ANDROID_EMULATOR);
assert.ok(ownership);
ownership.rebindUnconfirmed = true;
const device: FakeImeDeviceState = {
settings: new Map([['default_input_method', 'com.android.inputmethod.latin/.LatinIME']]),
};
const deviceAdb = fakeImeDeviceAdb(device);
const calls: string[] = [];

await withAndroidAdbProvider(
async (args) => {
calls.push(`${args[1]} ${args[2]}`);
if (args[1] === 'am') return { exitCode: 0, stdout: '', stderr: '' };
return await deviceAdb(args);
},
{ serial: ANDROID_EMULATOR.id },
async () => {
await typeAndroid(ANDROID_EMULATOR, 'Jane');
},
);

assert.ok(calls.lastIndexOf('ime set') < calls.indexOf('am broadcast'));
assert.equal(ownership.rebindUnconfirmed, false);
assert.equal(device.settings.has('agent_device_ime_helper_rebind_displaced'), false);
});

// Unicode is only beyond the *shell* path. Refusing it before reading which IME is active denied
// the broadcast channel to exactly the devices that could serve it: helper active, cache empty.

Expand Down Expand Up @@ -418,3 +623,12 @@ function helperImeInputMethodDump(): string {
function androidInputXml(options: { text: string }): string {
return `<?xml version="1.0" encoding="UTF-8"?><hierarchy><node package="com.example" class="android.widget.EditText" text="${options.text}" focused="true" bounds="[0,0][200,100]"/></hierarchy>`;
}

/** Settings reads after a rebind: its device record reads back, and the helper is still selected. */
function helperSelected(args: readonly string[]) {
const stdout =
args[4] === 'agent_device_ime_helper_rebind_displaced'
? '1\n'
: 'com.callstack.agentdevice.imehelper/.TestInputMethodService\n';
return { exitCode: 0, stdout, stderr: '' };
}
2 changes: 2 additions & 0 deletions packages/platform-android/src/fill-verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ export function buildAndroidFillUnconfirmedVerification(
// and the soft-success would also skip the second, bigger delete burst.
requested.length === 0 ||
verification.reason === 'ime_capture' ||
// A field back on its hint holds none of the requested text; no formatting empties a field.
verification.actualInput?.hintShowing === true ||
!beforeTarget ||
!afterTarget ||
!actualInput ||
Expand Down
Loading
Loading