Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
85 changes: 85 additions & 0 deletions src/core/__tests__/interaction-wire-projection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, expect, test } from 'vitest';
import {
interactionWireEchoFromInput,
projectInteractionWireData,
} from '../interaction-wire-projection.ts';
import { resolveCommandWireProjection } from '../command-descriptor/registry.ts';

describe('interaction wire projection', () => {
test('reads command-owned wire echo specs from descriptors', () => {
expect(resolveCommandWireProjection('press')?.wireEcho).toEqual({
count: { defaultValue: 1, mode: 'omit-default' },
intervalMs: { defaultValue: 0, mode: 'omit-default' },
holdMs: { defaultValue: 0, mode: 'omit-default' },
jitterPx: { defaultValue: 0, mode: 'omit-default' },
doubleTap: { defaultValue: false, mode: 'omit-default' },
});
expect(resolveCommandWireProjection('fill')?.wireEcho.delayMs).toEqual({ defaultValue: 0 });
expect(resolveCommandWireProjection('longpress')).toBeUndefined();
});

test('omits default press repeat values', () => {
expect(
interactionWireEchoFromInput('press', {
count: 1,
intervalMs: 0,
holdMs: 0,
jitterPx: 0,
doubleTap: false,
}),
).toEqual({});
});

test('echoes non-default press repeat values', () => {
expect(
interactionWireEchoFromInput('press', {
count: 2,
intervalMs: 25,
holdMs: 10,
jitterPx: 1,
doubleTap: true,
}),
).toEqual({
count: 2,
intervalMs: 25,
holdMs: 10,
jitterPx: 1,
doubleTap: true,
});
});

test('echoes the normalized fill delay default', () => {
expect(interactionWireEchoFromInput('fill', {})).toEqual({ delayMs: 0 });
});

test('preserves backend fields while deriving command echo defaults', () => {
expect(
projectInteractionWireData(
'press',
{},
{
count: 1,
videoPath: '/tmp/demo.mp4',
},
),
).toEqual({ videoPath: '/tmp/demo.mp4' });
});

test('removes default touch repeat echoes from fill backend data', () => {
expect(
projectInteractionWireData(
'fill',
{},
{
count: 1,
delayMs: 0,
doubleTap: false,
holdMs: 0,
intervalMs: 0,
jitterPx: 0,
text: 'Hello',
},
),
).toEqual({ delayMs: 0, text: 'Hello' });
});
});
34 changes: 34 additions & 0 deletions src/core/command-descriptor/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {
import type { CommandCapability } from '../capabilities.ts';
import type { DaemonRequest } from '../../daemon/types.ts';
import { resolveWaitBudgetMs } from '../wait-positionals.ts';
import {
FILL_DELAY_WIRE_ECHO,
INTERACTION_REPEAT_WIRE_ECHO_FIELDS,
type CommandWireProjection,
} from '../interaction-wire-echo.ts';
import {
DEFAULT_TIMEOUT_POLICY,
INSTALL_REQUEST_TIMEOUT_MS,
Expand Down Expand Up @@ -125,6 +130,19 @@ const SETTLE_FLAG_PRESERVE_DAEMON_TIMEOUT_POLICY: CommandTimeoutPolicy = {
onTimeout: 'preserve-daemon',
};

const TOUCH_INTERACTION_WIRE_PROJECTION = {
wireEcho: {
...INTERACTION_REPEAT_WIRE_ECHO_FIELDS,
},
} as const satisfies CommandWireProjection;

const FILL_INTERACTION_WIRE_PROJECTION = {
wireEcho: {
...INTERACTION_REPEAT_WIRE_ECHO_FIELDS,
delayMs: FILL_DELAY_WIRE_ECHO,
},
} as const satisfies CommandWireProjection;

function interactionTimeoutPolicy(command: string): CommandTimeoutPolicy {
return resolvePostActionObservationSupport(command) !== undefined
? SETTLE_FLAG_PRESERVE_DAEMON_TIMEOUT_POLICY
Expand Down Expand Up @@ -516,6 +534,7 @@ const RAW_COMMAND_DESCRIPTORS = [
capability: { apple: APPLE_SIM_AND_DEVICE, android: ANDROID_ALL, linux: LINUX_DEVICE },
timeoutPolicy: interactionTimeoutPolicy(PUBLIC_COMMANDS.click),
postActionObservation: postActionObservation(PUBLIC_COMMANDS.click),
wireProjection: TOUCH_INTERACTION_WIRE_PROJECTION,
batchable: true,
},
{
Expand All @@ -524,6 +543,7 @@ const RAW_COMMAND_DESCRIPTORS = [
capability: { apple: APPLE_SIM_AND_DEVICE, android: ANDROID_ALL, linux: LINUX_DEVICE },
timeoutPolicy: interactionTimeoutPolicy(PUBLIC_COMMANDS.fill),
postActionObservation: postActionObservation(PUBLIC_COMMANDS.fill),
wireProjection: FILL_INTERACTION_WIRE_PROJECTION,
batchable: true,
},
{
Expand All @@ -540,6 +560,7 @@ const RAW_COMMAND_DESCRIPTORS = [
capability: { apple: APPLE_SIM_AND_DEVICE, android: ANDROID_ALL, linux: LINUX_DEVICE },
timeoutPolicy: interactionTimeoutPolicy(PUBLIC_COMMANDS.press),
postActionObservation: postActionObservation(PUBLIC_COMMANDS.press),
wireProjection: TOUCH_INTERACTION_WIRE_PROJECTION,
batchable: true,
},
{
Expand Down Expand Up @@ -775,6 +796,12 @@ const TIMEOUT_POLICY_BY_COMMAND: ReadonlyMap<string, CommandTimeoutPolicy> = new
commandDescriptors.map((descriptor) => [descriptor.name, descriptor.timeoutPolicy]),
);

const WIRE_PROJECTION_BY_COMMAND: ReadonlyMap<string, CommandWireProjection> = new Map(
Array.from(COMMAND_DESCRIPTOR_BY_NAME.values()).flatMap((descriptor) =>
descriptor.wireProjection ? [[descriptor.name, descriptor.wireProjection] as const] : [],
),
);

export function resolveCommandPostActionObservationSupport(
command: string | undefined,
): PostActionObservationSupport | undefined {
Expand All @@ -800,3 +827,10 @@ export function resolveCommandTimeoutPolicy(command: string | undefined): Comman
if (command === undefined) return DEFAULT_TIMEOUT_POLICY;
return TIMEOUT_POLICY_BY_COMMAND.get(command) ?? DEFAULT_TIMEOUT_POLICY;
}

export function resolveCommandWireProjection(
command: string | undefined,
): CommandWireProjection | undefined {
if (command === undefined) return undefined;
return WIRE_PROJECTION_BY_COMMAND.get(command);
}
5 changes: 5 additions & 0 deletions src/core/command-descriptor/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CommandCapability } from '../capabilities.ts';
import type { CommandWireProjection } from '../interaction-wire-echo.ts';
import type { DaemonCommandDescriptor } from '../../daemon/daemon-command-registry.ts';
import type { PostActionObservationSupport } from './post-action-observation.ts';

Expand Down Expand Up @@ -81,6 +82,9 @@ export type CommandTimeoutPolicy = {
* commands that support `--settle`/`--verify`; consumed by
* command surfaces and timeout policy instead of repeated
* command-name lists.
* - `wireProjection` — optional response projection metadata for command-owned
* echoes in daemon responses. This keeps response shaping on
* the same descriptor surface as other command traits.
*
* The registry started dormant (proven byte-equal to the hand tables by the
* parity tests) and is now the live source: the daemon registry, capability
Expand All @@ -95,4 +99,5 @@ export type CommandDescriptor = {
mcpExposed: boolean;
timeoutPolicy: CommandTimeoutPolicy;
postActionObservation?: PostActionObservationSupport;
wireProjection?: CommandWireProjection;
};
22 changes: 22 additions & 0 deletions src/core/interaction-wire-echo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type WireEchoMode = 'always' | 'omit-default';

export type WireEchoOptions = {
defaultValue?: unknown;
mode?: WireEchoMode;
};

export type CommandWireProjection = {
wireEcho: Record<string, WireEchoOptions>;
};

export const INTERACTION_REPEAT_WIRE_ECHO_FIELDS = {
count: { defaultValue: 1, mode: 'omit-default' },
intervalMs: { defaultValue: 0, mode: 'omit-default' },
holdMs: { defaultValue: 0, mode: 'omit-default' },
jitterPx: { defaultValue: 0, mode: 'omit-default' },
doubleTap: { defaultValue: false, mode: 'omit-default' },
} as const satisfies Record<string, WireEchoOptions>;

export const FILL_DELAY_WIRE_ECHO = {
defaultValue: 0,
} as const satisfies WireEchoOptions;
46 changes: 46 additions & 0 deletions src/core/interaction-wire-projection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { resolveCommandWireProjection } from './command-descriptor/registry.ts';
import type { WireEchoOptions } from './interaction-wire-echo.ts';

export type InteractionWireEchoCommand = 'click' | 'press' | 'fill';

export function interactionWireEchoFromInput(
command: InteractionWireEchoCommand,
input: Record<string, unknown> | undefined,
): Record<string, unknown> {
return projectWireEchoSpecsFromInput(readCommandWireEchoSpecs(command), input ?? {});
}

export function projectInteractionWireData(
command: InteractionWireEchoCommand,
input: Record<string, unknown> | undefined,
base: Record<string, unknown> | undefined,
): Record<string, unknown> {
return projectWireEchoSpecsFromInput(readCommandWireEchoSpecs(command), input ?? {}, base);
}

function readCommandWireEchoSpecs(
command: InteractionWireEchoCommand,
): Record<string, WireEchoOptions> {
const projection = resolveCommandWireProjection(command);
if (!projection) {
throw new Error(`Missing wire projection descriptor for ${command}`);
}
return projection.wireEcho;
}

function projectWireEchoSpecsFromInput(
specs: Record<string, WireEchoOptions>,
input: Record<string, unknown>,
base: Record<string, unknown> = {},
): Record<string, unknown> {
const projected = { ...base };
for (const [key, spec] of Object.entries(specs)) {
const value = input[key] === undefined ? spec.defaultValue : input[key];
if (value === undefined || (spec.mode === 'omit-default' && value === spec.defaultValue)) {
delete projected[key];
continue;
}
projected[key] = value;
}
return Object.fromEntries(Object.entries(projected).filter(([, value]) => value !== undefined));
}
37 changes: 4 additions & 33 deletions src/daemon/handlers/interaction-touch-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ export type InteractionResponseSource =
| {
kind: 'runtime';
result: InteractionRuntimeResult;
wireData?: Record<string, unknown>;
}
| {
// Direct iOS selector dispatch: no runtime result exists, only the raw
// runner payload; identity extras are a declared gap on that path.
kind: 'runner-payload';
targetKind: InteractionRuntimeResult['kind'];
data: Record<string, unknown>;
wireData?: Record<string, unknown>;
point: { x: number; y: number };
};

Expand Down Expand Up @@ -84,7 +86,7 @@ export function buildInteractionResponseData(params: {
extra: commonExtra,
});
const responseData = buildTouchPayload({
data: sanitizeWireBackendData(source.data),
data: source.wireData,
fallbackX: source.point.x,
fallbackY: source.point.y,
referenceFrame,
Expand All @@ -109,7 +111,7 @@ export function buildInteractionResponseData(params: {
extra: commonExtra,
});
const responseData = buildTouchPayload({
data: sanitizeWireBackendData(result.backendResult),
data: source.wireData,
fallbackX: result.point?.x,
fallbackY: result.point?.y,
referenceFrame,
Expand Down Expand Up @@ -169,37 +171,6 @@ function stripUndefinedFields(data: Record<string, unknown>): Record<string, unk
return Object.fromEntries(Object.entries(data).filter(([, value]) => value !== undefined));
}

function sanitizeWireBackendData(
data: Record<string, unknown> | undefined,
): Record<string, unknown> | undefined {
if (!data) return undefined;
const sanitized = Object.fromEntries(
Object.entries(data).filter(([key, value]) => shouldKeepWireBackendField(key, value)),
);
return Object.keys(sanitized).length > 0 ? sanitized : undefined;
}

const OMITTED_WIRE_BACKEND_FIELDS = new Set([
'gestureStartUptimeMs',
'gestureEndUptimeMs',
'currentUptimeMs',
'sequenceResults',
]);

const DEFAULT_WIRE_BACKEND_FIELD_VALUES = new Map<string, unknown>([
['count', 1],
['intervalMs', 0],
['holdMs', 0],
['jitterPx', 0],
['doubleTap', false],
]);

function shouldKeepWireBackendField(key: string, value: unknown): boolean {
if (OMITTED_WIRE_BACKEND_FIELDS.has(key)) return false;
if (!DEFAULT_WIRE_BACKEND_FIELD_VALUES.has(key)) return true;
return value !== DEFAULT_WIRE_BACKEND_FIELD_VALUES.get(key);
}

function buildTouchMessage(
extra: Record<string, unknown> | undefined,
x: number | undefined,
Expand Down
Loading
Loading