Skip to content

Commit d49a893

Browse files
committed
fix(android): retire evidence after active publish rollback
1 parent 9f278dd commit d49a893

2 files changed

Lines changed: 132 additions & 7 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { expect, test } from 'vitest';
2+
import { localRuntimeOwner } from '@agent-device/contracts/platform';
3+
import {
4+
androidRecordingDevice,
5+
recordingHost,
6+
recordingInput,
7+
recordingProcess,
8+
} from './fixtures.ts';
9+
import { bindAndroidScreenRecordingRuntime } from './runtime.ts';
10+
11+
const start = async (overrides: Record<string, unknown>, signal = new AbortController().signal) =>
12+
await bindAndroidScreenRecordingRuntime({
13+
host: recordingHost(overrides),
14+
device: androidRecordingDevice,
15+
owner: localRuntimeOwner('android'),
16+
signal,
17+
});
18+
19+
test('cancellation during active manifest publication retires evidence after confirmed rollback', async () => {
20+
const controller = new AbortController();
21+
const reason = new Error('recording canceled during active manifest publication');
22+
const calls: string[] = [];
23+
let manifest = '';
24+
let writes = 0;
25+
const runtime = await start(
26+
{
27+
writeManifest: async ({ contents }: { contents: string }) => {
28+
manifest = contents;
29+
writes += 1;
30+
if (writes === 2) {
31+
controller.abort(reason);
32+
throw reason;
33+
}
34+
},
35+
readManifest: async () =>
36+
manifest ? { status: 'read' as const, contents: manifest } : { status: 'missing' as const },
37+
removeManifest: async () => {
38+
calls.push('remove-manifest');
39+
manifest = '';
40+
return true;
41+
},
42+
stop: async ({ pid }: { pid: string }) => {
43+
calls.push(`signal:${pid}`);
44+
return 'already-missing' as const;
45+
},
46+
remove: async () => {
47+
calls.push('remove-artifact');
48+
return true;
49+
},
50+
},
51+
controller.signal,
52+
);
53+
54+
await expect(runtime.screenRecordingStart(recordingInput())).rejects.toBe(reason);
55+
expect(calls).toEqual(['signal:42', 'remove-artifact', 'remove-manifest']);
56+
expect(manifest).toBe('');
57+
58+
const replacement = await start({
59+
readManifest: async () =>
60+
manifest ? { status: 'read' as const, contents: manifest } : { status: 'missing' as const },
61+
writeManifest: async ({ contents }: { contents: string }) => {
62+
manifest = contents;
63+
},
64+
});
65+
await expect(replacement.screenRecordingStart(recordingInput())).resolves.toMatchObject({
66+
envelope: expect.any(Object),
67+
});
68+
expect(manifest).not.toBe('');
69+
});
70+
71+
test('cancellation during active manifest publication retains evidence after uncertain rollback', async () => {
72+
const controller = new AbortController();
73+
const reason = new Error('recording canceled with uncertain active cleanup');
74+
let manifest = '';
75+
let writes = 0;
76+
const runtime = await start(
77+
{
78+
writeManifest: async ({ contents }: { contents: string }) => {
79+
manifest = contents;
80+
writes += 1;
81+
if (writes === 2) {
82+
controller.abort(reason);
83+
throw reason;
84+
}
85+
},
86+
readManifest: async () =>
87+
manifest ? { status: 'read' as const, contents: manifest } : { status: 'missing' as const },
88+
stop: async () => 'ownership-lost' as const,
89+
remove: async () => true,
90+
},
91+
controller.signal,
92+
);
93+
94+
await expect(runtime.screenRecordingStart(recordingInput())).rejects.toBe(reason);
95+
expect(manifest).not.toBe('');
96+
97+
const replacement = await start({
98+
readManifest: async () => ({ status: 'read' as const, contents: manifest }),
99+
start: async () => recordingProcess('43'),
100+
});
101+
await expect(replacement.screenRecordingStart(recordingInput())).rejects.toThrow(
102+
'native recovery evidence already exists',
103+
);
104+
});

‎packages/platform-android/src/recording/launch.ts‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ export async function startInitialTransaction(params: {
4949
chunk = await startChunkAt(transport, remotePath, input, signal);
5050
} catch (error) {
5151
if (signal.aborted) {
52-
// `startChunkAt` has already rolled back the child and its artifact. Retire the
53-
// pre-launch marker as well when that cleanup is confirmed; if the manifest removal
54-
// itself is unavailable, leave the marker for fenced recovery instead of authorizing a
55-
// replacement from an unproven state. The cancellation remains the primary outcome.
56-
if (!(error instanceof AndroidScreenRecordingStartRollbackUnconfirmed))
57-
await removeNativeManifest(transport, manifestPath).catch(() => {});
52+
await retireCanceledLaunchMarker(transport, manifestPath, error);
5853
throw signal.reason;
5954
}
6055
await removeFailedCandidateManifest(transport, manifestPath, error);
@@ -69,14 +64,40 @@ export async function startInitialTransaction(params: {
6964
signal,
7065
);
7166
} catch (error) {
72-
await rollbackChunks(transport, [chunk]).catch(() => {});
67+
await rollbackPublishedChunk(transport, manifestPath, chunk);
7368
throw error;
7469
}
7570
return { chunk, manifestPath };
7671
}
7772
throw last ?? new Error('Android screenrecord did not begin producing frames');
7873
}
7974

75+
async function retireCanceledLaunchMarker(
76+
transport: Transport,
77+
manifestPath: string,
78+
launchError: unknown,
79+
): Promise<void> {
80+
// `startChunkAt` has already rolled back the child and its artifact. Retire the pre-launch
81+
// marker only when that cleanup is confirmed; otherwise leave it for fenced recovery.
82+
if (launchError instanceof AndroidScreenRecordingStartRollbackUnconfirmed) return;
83+
await removeNativeManifest(transport, manifestPath).catch(() => {});
84+
}
85+
86+
async function rollbackPublishedChunk(
87+
transport: Transport,
88+
manifestPath: string,
89+
chunk: NativeChunk,
90+
): Promise<void> {
91+
try {
92+
await rollbackChunks(transport, [chunk]);
93+
} catch {
94+
// Retain the pending marker when native cleanup is uncertain so a fenced recovery can prove
95+
// ownership before a later start is admitted.
96+
return;
97+
}
98+
await removeNativeManifest(transport, manifestPath).catch(() => {});
99+
}
100+
80101
/**
81102
* A terminal marker outlives native cleanup until a later, fenced start reconciles it. Never
82103
* retire open or uncertain evidence: that would erase the only recovery authority after a crash.

0 commit comments

Comments
 (0)