Skip to content

Commit b2027c2

Browse files
committed
fix(batch): report unknown after an executed mutating step
1 parent 6165953 commit b2027c2

3 files changed

Lines changed: 96 additions & 29 deletions

File tree

‎docs/adr/0011-interaction-guarantee-contract.md‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ Each request owns one dispatch ledger. Every bound runtime operation declares
163163
its effect once, in `RUNTIME_OPERATION_EFFECTS`, a record over the runtime
164164
operation keys, so an operation cannot bind without one. The request binding
165165
records each `mutates` operation in the request's ledger when its send
166-
returns, so no route sends a mutation outside the ledger. A nested request (a
166+
returns, so no bound runtime operation sends a mutation outside the ledger.
167+
Android ANR and blocking-dialog recovery is outside the ledger: its
168+
`AndroidObservationAdapter.tap` and `openApp` calls reach the device without a
169+
record. A nested request (a
167170
batch step, a replay action, a delegated `find` click or fill) records into a
168171
ledger of its own and moves its sends into its parent's.
169172

@@ -192,9 +195,11 @@ producer.
192195
Remaining gaps: a failure before the router's locked scope (session
193196
resolution, lock acquisition, lease and daemon-policy admission) never reaches
194197
the disclosure and carries no `dispatched`. It sends nothing, but a consumer
195-
must read the absent field as `unknown`. The public `agent-device/batch`
196-
`runBatch` has no ledger when a caller supplies its own `invoke`, so a batch run
197-
outside the daemon keeps the failing step's value.
198+
must read the absent field as `unknown`.
199+
200+
`runBatch` reports `no` only when none of its executed steps is a
201+
`'mutates-app'` command; after one, the batch failure is `unknown` for every
202+
caller, including one that supplies its own `invoke`.
198203

199204
For `responseFields`, one `buildInteractionResponseData(...)` becomes the only
200205
construction site for interaction response payloads (this deletes the class of

‎packages/command-registry/src/batch.ts‎

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
assertBatchRuntimeCommandAllowed,
2222
normalizeBatchCommandName,
2323
} from './batch-policy.ts';
24+
import { resolveCommandRecordingEffect } from './registry.ts';
2425

2526
const batchAllowedStepKeys = new Set<string>(BATCH_DAEMON_STEP_KEYS);
2627

@@ -115,31 +116,11 @@ export async function runBatch(
115116
})),
116117
});
117118
if (!stepResponse.ok) {
118-
return {
119-
ok: false,
120-
error: {
121-
code: stepResponse.error.code,
122-
message: `Batch failed at step ${stepResponse.step} (${step.command}): ${stepResponse.error.message}`,
123-
hint: stepResponse.error.hint,
124-
diagnosticId: stepResponse.error.diagnosticId,
125-
logPath: stepResponse.error.logPath,
126-
...(stepResponse.error.retriable === undefined
127-
? {}
128-
: { retriable: stepResponse.error.retriable }),
129-
...(stepResponse.error.supportedOn === undefined
130-
? {}
131-
: { supportedOn: stepResponse.error.supportedOn }),
132-
details: {
133-
...(stepResponse.error.details ?? {}),
134-
step: stepResponse.step,
135-
command: step.command,
136-
positionals: step.positionals,
137-
executed: index,
138-
total: steps.length,
139-
partialResults,
140-
},
141-
},
142-
};
119+
return batchStepFailure(stepResponse, step, {
120+
executedSteps: steps.slice(0, index),
121+
total: steps.length,
122+
partialResults,
123+
});
143124
}
144125
partialResults.push(stepResponse.result);
145126
}
@@ -159,6 +140,47 @@ export async function runBatch(
159140
}
160141
}
161142

143+
type BatchStepFailure = Extract<Awaited<ReturnType<typeof runBatchStep>>, { ok: false }>;
144+
145+
function batchStepFailure(
146+
stepResponse: BatchStepFailure,
147+
step: NormalizedBatchStep,
148+
progress: {
149+
executedSteps: readonly NormalizedBatchStep[];
150+
total: number;
151+
partialResults: BatchStepResult[];
152+
},
153+
): Extract<BatchRunResponse, { ok: false }> {
154+
const { error } = stepResponse;
155+
return {
156+
ok: false,
157+
error: {
158+
code: error.code,
159+
message: `Batch failed at step ${stepResponse.step} (${step.command}): ${error.message}`,
160+
hint: error.hint,
161+
diagnosticId: error.diagnosticId,
162+
logPath: error.logPath,
163+
...(error.retriable === undefined ? {} : { retriable: error.retriable }),
164+
...(error.supportedOn === undefined ? {} : { supportedOn: error.supportedOn }),
165+
details: {
166+
...(error.details ?? {}),
167+
...(progress.executedSteps.some(stepMutatesApp) ? { dispatched: 'unknown' } : {}),
168+
step: stepResponse.step,
169+
command: step.command,
170+
positionals: step.positionals,
171+
executed: progress.executedSteps.length,
172+
total: progress.total,
173+
partialResults: progress.partialResults,
174+
},
175+
},
176+
};
177+
}
178+
179+
/** A batch that executed a mutating step cannot promise that a resend repeats nothing. */
180+
function stepMutatesApp(step: NormalizedBatchStep): boolean {
181+
return resolveCommandRecordingEffect(step) === 'mutates-app';
182+
}
183+
162184
export function validateAndNormalizeBatchSteps(
163185
steps: unknown,
164186
maxSteps: number,

‎src/commands/batch/public.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,43 @@ test('public batch entrypoint exports daemon-compatible orchestration helpers',
3939
assert.equal(response.data.results[0]?.command, 'open');
4040
}
4141
});
42+
43+
async function runBatchFailingAtSecondStep(firstCommand: string, firstPositionals: string[]) {
44+
const req: Omit<DaemonRequest, 'token'> = {
45+
command: 'batch',
46+
positionals: [],
47+
flags: {
48+
batchSteps: [
49+
{ command: firstCommand, positionals: firstPositionals },
50+
{ command: 'press', positionals: ['label=Missing'] },
51+
],
52+
},
53+
};
54+
return await runBatch(req, 'session', async (stepReq) =>
55+
stepReq.positionals?.[0] === 'label=Missing'
56+
? {
57+
ok: false,
58+
error: {
59+
code: 'ELEMENT_NOT_FOUND',
60+
message: 'No element matches label=Missing',
61+
details: { dispatched: 'no' },
62+
},
63+
}
64+
: { ok: true, data: {} },
65+
);
66+
}
67+
68+
test('batch reports unknown when a mutating step executed before the refused step', async () => {
69+
const response = await runBatchFailingAtSecondStep('press', ['label=A']);
70+
assert.equal(response.ok, false);
71+
if (!response.ok) {
72+
assert.equal(response.error.details?.dispatched, 'unknown');
73+
assert.equal(response.error.details?.executed, 1);
74+
}
75+
});
76+
77+
test('batch keeps the refused step verdict when only reads executed before it', async () => {
78+
const response = await runBatchFailingAtSecondStep('get', ['text', 'label=A']);
79+
assert.equal(response.ok, false);
80+
if (!response.ok) assert.equal(response.error.details?.dispatched, 'no');
81+
});

0 commit comments

Comments
 (0)