Skip to content

Commit 0acaee7

Browse files
committed
fix(capture): scope cancellation and clarify invalid destinations
1 parent 2a74916 commit 0acaee7

5 files changed

Lines changed: 108 additions & 7 deletions

File tree

‎packages/command-runtime/src/commands/capture/runtime/screenshot.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,63 @@ function cropPngBuffer(
8484
}
8585
return PNG.sync.write(cropped);
8686
}
87+
88+
for (const staged of [false, true]) {
89+
test(`late cancellation ${staged ? 'refuses staged publication' : 'retains direct screenshot success'}`, async () => {
90+
const root = mkdtempForTestSync('screenshot-late-cancellation');
91+
const destination = path.join(root, 'output.png');
92+
const staging = path.join(root, 'work.png');
93+
const sentinel = Buffer.from('old screenshot');
94+
fs.writeFileSync(destination, sentinel);
95+
const controller = new AbortController();
96+
const cancellation = new Error('caller cancelled after capture');
97+
const adapter = createLocalArtifactAdapter();
98+
let publications = 0;
99+
let cleanups = 0;
100+
if (staged) {
101+
adapter.reserveOutput = async () => ({
102+
path: staging,
103+
publishedPath: destination,
104+
visibility: 'client-visible',
105+
publish: async () => {
106+
publications += 1;
107+
fs.renameSync(staging, destination);
108+
return undefined;
109+
},
110+
cleanup: async () => {
111+
cleanups += 1;
112+
fs.rmSync(staging, { force: true });
113+
},
114+
});
115+
}
116+
const device = createAgentDevice({
117+
backend: {
118+
platform: 'ios',
119+
captureScreenshot: async (_context, requestedPath) => {
120+
fs.writeFileSync(requestedPath, solidPngBuffer(8, 8));
121+
controller.abort(cancellation);
122+
},
123+
},
124+
artifacts: adapter,
125+
sessions: { get: () => undefined, set: () => {} },
126+
policy: localCommandPolicy(),
127+
});
128+
const operation = device.capture.screenshot({
129+
out: { kind: 'path', path: destination },
130+
scale: 0.5,
131+
signal: controller.signal,
132+
});
133+
if (staged) {
134+
await assert.rejects(operation, (error) => error === cancellation);
135+
assert.deepEqual(fs.readFileSync(destination), sentinel);
136+
assert.equal(fs.existsSync(staging), false);
137+
assert.equal(publications, 0);
138+
assert.equal(cleanups, 1);
139+
} else {
140+
const result = await operation;
141+
assert.equal(result.path, destination);
142+
const image = PNG.sync.read(fs.readFileSync(destination));
143+
assert.deepEqual({ width: image.width, height: image.height }, { width: 4, height: 4 });
144+
}
145+
});
146+
}

‎packages/command-runtime/src/commands/capture/runtime/screenshot.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ export const screenshotCommand: RuntimeCommand<
5656
if (options.scale !== undefined) {
5757
await resizePngFileToScale(reserved.path, options.scale);
5858
}
59-
(options.signal ?? runtime.signal)?.throwIfAborted();
59+
if (reserved.publishedPath !== undefined) {
60+
(options.signal ?? runtime.signal)?.throwIfAborted();
61+
}
6062
artifact = await reserved.publish();
6163
} catch (error) {
6264
await reserved.cleanup?.();

‎packages/daemon/src/execution/screenshot-artifact-adapter.test.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ test('a dangling destination symlink is refused without replacing it', async ()
3939
outputOptions,
4040
),
4141
).rejects.toMatchObject({
42-
code: 'COMMAND_FAILED',
43-
details: { reason: 'screenshot_publication_failed', filesystemCode: 'ENOENT' },
42+
code: 'INVALID_ARGS',
43+
message: 'Screenshot destination is a dangling symbolic link',
44+
details: { reason: 'screenshot_destination_dangling_symlink', path: destination },
4445
});
4546
expect(fs.readlinkSync(destination)).toBe('missing.png');
4647
expect(fs.existsSync(path.join(root, 'missing.png'))).toBe(false);
@@ -87,3 +88,20 @@ for (const existing of [true, false]) {
8788
expect(fs.existsSync(path.dirname(reserved.path))).toBe(false);
8889
});
8990
}
91+
92+
test('automatic crop output retains cancellation and owned-file cleanup before publication', async () => {
93+
const controller = new AbortController();
94+
const cancellation = new Error('cancelled after crop');
95+
const reserved = await createDaemonScreenshotArtifactAdapter(
96+
true,
97+
controller.signal,
98+
).reserveOutput(undefined, outputOptions);
99+
fs.writeFileSync(reserved.path, 'completed crop');
100+
controller.abort(cancellation);
101+
try {
102+
await expect(reserved.publish()).rejects.toBe(cancellation);
103+
} finally {
104+
await reserved.cleanup?.();
105+
}
106+
expect(fs.existsSync(path.dirname(reserved.path))).toBe(false);
107+
});

‎packages/daemon/src/execution/screenshot-artifact-adapter.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export function createDaemonScreenshotArtifactAdapter(
3131
return {
3232
path: outputPath,
3333
visibility: 'client-visible',
34-
publish: async () => undefined,
34+
publish: async () => {
35+
if (atomic) signal?.throwIfAborted();
36+
return undefined;
37+
},
3538
...(tempRoot
3639
? {
3740
cleanup: async () => {
@@ -113,7 +116,21 @@ async function resolveScreenshotDestination(outputPath: string): Promise<string>
113116
if (error.code === 'ENOENT') return undefined;
114117
throw error;
115118
});
116-
destination = entry?.isSymbolicLink() ? await fs.realpath(outputPath) : outputPath;
119+
destination = entry?.isSymbolicLink()
120+
? await fs.realpath(outputPath).catch((error: NodeJS.ErrnoException) => {
121+
if (error.code === 'ENOENT') {
122+
throw new AppError(
123+
'INVALID_ARGS',
124+
'Screenshot destination is a dangling symbolic link',
125+
{
126+
reason: 'screenshot_destination_dangling_symlink',
127+
path: outputPath,
128+
},
129+
);
130+
}
131+
throw error;
132+
})
133+
: outputPath;
117134
const target = await fs.stat(destination).catch((error: NodeJS.ErrnoException) => {
118135
if (error.code === 'ENOENT') return undefined;
119136
throw error;

‎packages/host-kit/src/internal/atomic-file.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ describe('publishFileSync', () => {
3939
const target = destination('clean.txt');
4040
publishFileSync({ destination: target, contents: 'body' });
4141

42-
const siblings = fs.readdirSync(directory).filter((name) => name.startsWith('clean.txt.'));
42+
const siblings = fs
43+
.readdirSync(directory)
44+
.filter((name) => name.startsWith('.clean.txt.') && name.endsWith('.tmp'));
4345
expect(siblings).toEqual([]);
4446
});
4547

@@ -85,7 +87,9 @@ describe('publishFileSync', () => {
8587
expect(
8688
fs
8789
.readdirSync(directory)
88-
.filter((name) => name.startsWith('permission-update-failed.png.')),
90+
.filter(
91+
(name) => name.startsWith('.permission-update-failed.png.') && name.endsWith('.tmp'),
92+
),
8993
).toEqual([]);
9094
} finally {
9195
chmod.mockRestore();

0 commit comments

Comments
 (0)