Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 .changeset/android-type-backspace-del.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'agent-device': patch
---

Fix Android text input typing `\b` backspaces as literal characters into fields instead of deleting characters. Sequences composed entirely of backspaces are now dispatched via `KEYCODE_DEL` keyevents across all Android input targets.
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,26 @@ test('a third-party active IME keeps ASCII text on the chunked shell path', asyn
assert.equal(calls.filter((args) => args[1] === 'input' && args[2] === 'text').length, 3);
});

test('typeAndroid sends KEYCODE_DEL keyevents instead of broadcasting backspaces through the test IME', async () => {
setAndroidTestImeActiveForTests(ANDROID_EMULATOR, true);
const calls: (readonly string[])[] = [];
await withAndroidAdbProvider(
async (args) => {
calls.push(args);
return { exitCode: 0, stdout: '', stderr: '' };
},
{ serial: ANDROID_EMULATOR.id },
async () => {
await typeAndroid(ANDROID_EMULATOR, '\b\b\b');
},
);

assert.equal(calls.filter((args) => args[1] === 'am' && args[2] === 'broadcast').length, 0);
const delCalls = calls.filter((args) => args.includes('KEYCODE_DEL'));
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
assert.equal(delCalls.length, 1);
assert.equal(delCalls[0]?.filter((arg) => arg === 'KEYCODE_DEL').length, 3);
});

/** The `android_text_injection` events a run emitted, in order, as `{action, backend}` pairs. */
async function captureTextInjectionDiagnostics(
run: () => Promise<void>,
Expand Down
16 changes: 16 additions & 0 deletions packages/platform-android/src/__tests__/text-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ test('typeAndroid leaves safe text unquoted', async () => {
);
});

test('typeAndroid sends KEYCODE_DEL keyevents for backspace sequences', async () => {
await withFakeAdb(
(args) => {
if (args.includes('KEYCODE_MOVE_END')) return undefined;
if (args.includes('KEYCODE_DEL')) return undefined;
return { stderr: `unexpected args: ${args.join(' ')}`, exitCode: 1 };
},
async ({ calls, device }) => {
await typeAndroid(device, '\b\b\b');
const delCalls = calls.filter((args) => args.includes('KEYCODE_DEL'));
assert.equal(delCalls.length, 1);
assert.equal(delCalls[0]?.filter((arg) => arg === 'KEYCODE_DEL').length, 3);
},
);
});

test('fillAndroid uses chunk-safe shell input and retries when verification still fails', async () => {
// First `input text` writes a wrong partial value, so attempt 1 fails
// verification and production retries with the smaller chunk size.
Expand Down
13 changes: 13 additions & 0 deletions packages/platform-android/src/text-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,26 @@ import type { AndroidHelperSessionOptions } from './snapshot-helper-types.ts';
*/
const ANDROID_INPUT_TEXT_CHUNK_SIZE = 8;

function isAndroidBackspaceSequence(text: string): boolean {
if (text.length === 0) return false;
for (let i = 0; i < text.length; i++) {
if (text.charCodeAt(i) !== 0x08) return false;
}
return true;
}

export async function typeAndroid(device: DeviceInfo, text: string, delayMs = 0): Promise<void> {
const providerText = resolveAndroidTextInjector(device);
if (providerText) {
await providerText({ action: 'type', text, delayMs });
emitAndroidTextDiagnostic('type', 'provider-native', text);
return;
}
if (isAndroidBackspaceSequence(text)) {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
await clearFocusedText(device, text.length);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1: This new typing path treats failed delete keyevents as success because clearFocusedText allows command failures and does not inspect exit codes. Make the delete dispatch fail closed, or check each result before returning, so callers do not believe backspaces were applied when adb rejected them.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/platform-android/src/text-input.ts, line 64:

<comment>This new typing path treats failed delete keyevents as success because `clearFocusedText` allows command failures and does not inspect exit codes. Make the delete dispatch fail closed, or check each result before returning, so callers do not believe backspaces were applied when adb rejected them.</comment>

<file context>
@@ -45,13 +45,26 @@ import type { AndroidHelperSessionOptions } from './snapshot-helper-types.ts';
     return;
   }
+  if (isAndroidBackspaceSequence(text)) {
+    await clearFocusedText(device, text.length);
+    emitAndroidTextDiagnostic('type', 'adb-shell', text);
+    return;
</file context>

Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
emitAndroidTextDiagnostic('type', 'adb-shell', text);
return;
}
const channel = await admitAndroidTextChannel(device, 'type', text);
if (channel.backend === 'test-ime') {
await typeAndroidImeHelper(device, channel.packageName, text, delayMs);
Expand Down