From 042ee779e72207beb7675eb9e798b30c181d99a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 30 Oct 2026 15:52:58 +0100 Subject: [PATCH 1/4] test(ios): fail when snapshot bridge bounds drift between host and native Expose kMaximumRequestLevels and kMaximumRequests through SnapshotBridgeCapture.h so the native fixtures use them instead of literals, and add limits.test.ts, which reads the bridge sources and asserts the host bounds equal the native ones. Refs #4089 Co-Authored-By: Claude Opus 5.5 --- apple/snapshot-bridge/SnapshotBridgeCapture.h | 5 +++ apple/snapshot-bridge/SnapshotBridgeCapture.m | 8 ++-- .../fixtures/foreground-owner.m | 10 ++--- .../src/snapshot-source/limits.test.ts | 41 +++++++++++++++++++ .../src/snapshot-source/limits.ts | 4 +- 5 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 packages/platform-apple/src/snapshot-source/limits.test.ts diff --git a/apple/snapshot-bridge/SnapshotBridgeCapture.h b/apple/snapshot-bridge/SnapshotBridgeCapture.h index 6f1cf4a6e5..2d1d02fe71 100644 --- a/apple/snapshot-bridge/SnapshotBridgeCapture.h +++ b/apple/snapshot-bridge/SnapshotBridgeCapture.h @@ -2,6 +2,11 @@ NS_ASSUME_NONNULL_BEGIN +/// Native levels one request reads at most; deeper levels arrive through continuations. +extern const NSUInteger kMaximumRequestLevels; +/// Requests one acquisition may issue before it fails as continuation-budget-exhausted. +extern const NSUInteger kMaximumRequests; + typedef id _Nullable (^SnapshotElementReader)(id element, NSUInteger depth, NSUInteger nodes, NSError **error); /// Native request accounting for one acquisition: requests issued, requests the accessibility diff --git a/apple/snapshot-bridge/SnapshotBridgeCapture.m b/apple/snapshot-bridge/SnapshotBridgeCapture.m index cbc1c0cbde..ea8e647c76 100644 --- a/apple/snapshot-bridge/SnapshotBridgeCapture.m +++ b/apple/snapshot-bridge/SnapshotBridgeCapture.m @@ -9,10 +9,10 @@ // trees the AX server rejects a request that would return more than about 62 levels (measured on // iOS 26.5), so there the first request is rejected once and the halving ladder settles at 32, // which the host then sends as a depth hint. -static const NSUInteger maximumRequestLevels = 65; +const NSUInteger kMaximumRequestLevels = 65; // Sized for the daemon's default depth bound (128 levels). Each root-to-leaf path needs one // continuation per accepted-depth window, so the budget scales with the depth it must cover. -static const NSUInteger maximumRequests = 64; +const NSUInteger kMaximumRequests = 64; @interface SnapshotTreeCapture : NSObject @property(nonatomic, copy) SnapshotElementReader reader; @@ -31,7 +31,7 @@ - (nullable NSDictionary *)read:(id)element depth:(NSUInteger)depth error:(NSErr { NSUInteger attemptDepth = MIN(depth, self.acceptedDepth); for (NSUInteger retries = 0;; retries++) { - if (self.requests >= maximumRequests) { + if (self.requests >= kMaximumRequests) { if (error) *error = [NSError errorWithDomain:@"agent-device.snapshot" code:1 userInfo:@{NSLocalizedDescriptionKey: @"snapshot continuation request budget exhausted"}]; return nil; @@ -130,7 +130,7 @@ - (nullable NSDictionary *)materialize:(NSDictionary *)tree depth:(NSUInteger)de { SnapshotTreeCapture *capture = [SnapshotTreeCapture new]; capture.reader = reader; - NSUInteger firstRequestLevels = nativeLevelsHint > 0 ? MIN(nativeLevelsHint, maximumRequestLevels) : maximumRequestLevels; + NSUInteger firstRequestLevels = nativeLevelsHint > 0 ? MIN(nativeLevelsHint, kMaximumRequestLevels) : kMaximumRequestLevels; capture.acceptedDepth = MIN(maxDepth + 1, firstRequestLevels); capture.remainingNodes = maxNodes; capture.maximumNodes = maxNodes; diff --git a/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m b/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m index 4497510bfb..afddd048d2 100644 --- a/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m +++ b/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m @@ -151,7 +151,7 @@ int main(int argc, const char *argv[]) NSString *scenario = @(argv[1]); if ([scenario isEqual:@"identity"] || [scenario isEqual:@"request-budget"] || [scenario hasPrefix:@"api-depth-"]) { BOOL budget = [scenario isEqual:@"request-budget"]; - NSUInteger depth = [scenario hasPrefix:@"api-depth-"] ? [[scenario substringFromIndex:10] integerValue] : 64; + NSUInteger depth = [scenario hasPrefix:@"api-depth-"] ? [[scenario substringFromIndex:10] integerValue] : kMaximumRequestLevels - 1; NSMutableArray *children = [NSMutableArray array]; for (NSUInteger i = 0; i < (budget ? 80 : 2); i++) { [children addObject:@{@"UIAccessibilitySnapshotKeyAttributes": @{}, @"UIAccessibilitySnapshotKeyChildren": @[], @@ -165,7 +165,7 @@ int main(int argc, const char *argv[]) requests++; if ([scenario hasPrefix:@"api-depth-"]) { NSUInteger start = [element isKindOfClass:NSNumber.class] ? [element unsignedIntegerValue] : 0; - require(levels == MIN(depth + 1 - start, 65), @"native levels must include the root exactly once, at most 65 per request"); + require(levels == MIN(depth + 1 - start, kMaximumRequestLevels), @"native levels must include the root exactly once, at most kMaximumRequestLevels per request"); NSUInteger end = MIN(start + levels, depth + 1); NSDictionary *tree = nil; for (NSUInteger level = end; level-- > start;) tree = @{@"UIAccessibilitySnapshotKeyAttributes": @{}, @"UIAccessibilitySnapshotKeyElement": @(level), @"UIAccessibilitySnapshotKeyChildrenCount": [scenario isEqual:@"api-depth-unknown"] ? [NSNull null] : @(level < depth ? 1 : 0), @"UIAccessibilitySnapshotKeyChildren": tree ? @[tree] : @[]}; @@ -174,12 +174,12 @@ int main(int argc, const char *argv[]) if ([element isEqual:@"root"]) return root; return @{@"UIAccessibilitySnapshotKeyAttributes": @{}, @"UIAccessibilitySnapshotKeyChildren": @[@{@"UIAccessibilitySnapshotKeyAttributes": @{}, @"UIAccessibilitySnapshotKeyChildren": @[]}]}; }, &truncated, NULL, &failure); - if (budget) require(!result && failure.code == 1 && requests == 64, @"request budget must fail without publishing partial content"); + if (budget) require(!result && failure.code == 1 && requests == kMaximumRequests, @"request budget must fail without publishing partial content"); else if ([scenario isEqual:@"identity"]) require(result == root && requests == 1, @"healthy capture must reuse the native tree"); else { NSUInteger count = 0; for (NSDictionary *node = result; node; node = [node[@"UIAccessibilitySnapshotKeyChildren"] firstObject]) count++; - require(count == depth + 1 && truncated == [scenario isEqual:@"api-depth-unknown"] && requests == (depth + 1 > 65 ? 2 : 1), @"every requested depth must include root plus permitted descendants"); + require(count == depth + 1 && truncated == [scenario isEqual:@"api-depth-unknown"] && requests == (depth + 1 > kMaximumRequestLevels ? 2 : 1), @"every requested depth must include root plus permitted descendants"); } return 0; } @@ -193,7 +193,7 @@ int main(int argc, const char *argv[]) NSString *expectedCode = nil; NSUInteger expectedCaptures = 0; if ([scenario isEqual:@"runtime-budget"]) { - expectedCaptures = 64; + expectedCaptures = kMaximumRequests; expectedCode = @"continuation-budget-exhausted"; } else if ([scenario hasPrefix:@"wide-"] || [scenario isEqual:@"zero-depth"]) { expectedCaptures = [scenario isEqual:@"wide-continuation"] ? 2 : 1; diff --git a/packages/platform-apple/src/snapshot-source/limits.test.ts b/packages/platform-apple/src/snapshot-source/limits.test.ts new file mode 100644 index 0000000000..b4a7c1fb9e --- /dev/null +++ b/packages/platform-apple/src/snapshot-source/limits.test.ts @@ -0,0 +1,41 @@ +import assert from 'node:assert/strict'; +import { readFile } from 'node:fs/promises'; +import path from 'node:path'; +import { test } from 'vitest'; +import { MAXIMUM_REQUEST_LEVELS, resolveSnapshotSourceLimits } from './limits.ts'; +import type { SnapshotSourceLimits } from './types.ts'; + +// The host and the native bridge share these bounds only through source, so this guard reads +// each native literal and fails if either side drifts. +const nativeSource = ( + await Promise.all( + ['SnapshotBridgeRuntime.m', 'SnapshotBridgeCapture.m'].map((fileName) => + readFile( + path.join(import.meta.dirname, '../../../../apple/snapshot-bridge', fileName), + 'utf8', + ), + ), + ) +).join('\n'); + +function nativeConstant(name: string): number { + const match = new RegExp(`^const NSUInteger ${name} = (\\d+);$`, 'm').exec(nativeSource); + assert.ok(match, `the native bridge no longer declares ${name}`); + return Number(match[1]); +} + +test('the first-request window matches the native bridge', () => { + assert.equal(MAXIMUM_REQUEST_LEVELS, nativeConstant('kMaximumRequestLevels')); +}); + +test.each([ + ['maxTraversalDepth', 'kMaximumDepth'], + ['maxNodes', 'kMaximumNodes'], + ['maxDurationMs', 'kMaximumDurationMs'], +] as const)('%s accepts the native %s and rejects one more', (limit, native) => { + const bound = nativeConstant(native); + const resolve = (value: number) => + resolveSnapshotSourceLimits({ [limit]: value } satisfies Partial); + assert.equal(resolve(bound)[limit], bound); + assert.throws(() => resolve(bound + 1), { code: 'INVALID_ARGS' }); +}); diff --git a/packages/platform-apple/src/snapshot-source/limits.ts b/packages/platform-apple/src/snapshot-source/limits.ts index 0e88b6c16e..a88d63589e 100644 --- a/packages/platform-apple/src/snapshot-source/limits.ts +++ b/packages/platform-apple/src/snapshot-source/limits.ts @@ -4,9 +4,9 @@ import type { SnapshotSourceLimits } from './types.ts'; const FRAME_HEADER_BYTES = 4; const MAXIMUM_FRAME_BYTES = 16 * 1024 * 1024; const MAXIMUM_DURATION_MS = 120_000; -// Mirrors the native bridge's kMaximumDepth. +// Mirrors the native bridge's kMaximumDepth; limits.test.ts fails on drift. const MAXIMUM_DEPTH = 128; -// Mirrors the native bridge's maximumRequestLevels: one native request reads at most this many +// Mirrors the native bridge's kMaximumRequestLevels: one native request reads at most this many // levels, and deeper levels arrive through continuations. export const MAXIMUM_REQUEST_LEVELS = 65; const MAXIMUM_NODES = 10_000; From 281c5ae35eb351b631daebe2e03a53ba73327196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 30 Oct 2026 15:55:29 +0100 Subject: [PATCH 2/4] test(ios): guard the frame bound and derive the fixture request count Co-Authored-By: Claude Opus 5.5 --- .../src/snapshot-source/fixtures/foreground-owner.m | 3 ++- .../platform-apple/src/snapshot-source/limits.test.ts | 11 ++++++++--- packages/platform-apple/src/snapshot-source/limits.ts | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m b/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m index afddd048d2..26b608f674 100644 --- a/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m +++ b/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m @@ -179,7 +179,8 @@ int main(int argc, const char *argv[]) else { NSUInteger count = 0; for (NSDictionary *node = result; node; node = [node[@"UIAccessibilitySnapshotKeyChildren"] firstObject]) count++; - require(count == depth + 1 && truncated == [scenario isEqual:@"api-depth-unknown"] && requests == (depth + 1 > kMaximumRequestLevels ? 2 : 1), @"every requested depth must include root plus permitted descendants"); + // Each continuation re-reads its root, so one request adds at most kMaximumRequestLevels - 1 edges. + require(count == depth + 1 && truncated == [scenario isEqual:@"api-depth-unknown"] && requests == MAX(1, (depth + kMaximumRequestLevels - 2) / (kMaximumRequestLevels - 1)), @"every requested depth must include root plus permitted descendants"); } return 0; } diff --git a/packages/platform-apple/src/snapshot-source/limits.test.ts b/packages/platform-apple/src/snapshot-source/limits.test.ts index b4a7c1fb9e..a3a8bfaee7 100644 --- a/packages/platform-apple/src/snapshot-source/limits.test.ts +++ b/packages/platform-apple/src/snapshot-source/limits.test.ts @@ -19,9 +19,13 @@ const nativeSource = ( ).join('\n'); function nativeConstant(name: string): number { - const match = new RegExp(`^const NSUInteger ${name} = (\\d+);$`, 'm').exec(nativeSource); - assert.ok(match, `the native bridge no longer declares ${name}`); - return Number(match[1]); + const match = new RegExp( + `^const (?:NSUInteger|uint32_t) ${name} = (\\d+(?: \\* \\d+)*);$`, + 'm', + ).exec(nativeSource); + const value = match?.[1]; + assert.ok(value, `the native bridge no longer declares ${name}`); + return value.split(' * ').reduce((product, factor) => product * Number(factor), 1); } test('the first-request window matches the native bridge', () => { @@ -29,6 +33,7 @@ test('the first-request window matches the native bridge', () => { }); test.each([ + ['maxResponseBytes', 'kMaximumFrameBytes'], ['maxTraversalDepth', 'kMaximumDepth'], ['maxNodes', 'kMaximumNodes'], ['maxDurationMs', 'kMaximumDurationMs'], diff --git a/packages/platform-apple/src/snapshot-source/limits.ts b/packages/platform-apple/src/snapshot-source/limits.ts index a88d63589e..2e34bffecc 100644 --- a/packages/platform-apple/src/snapshot-source/limits.ts +++ b/packages/platform-apple/src/snapshot-source/limits.ts @@ -4,7 +4,7 @@ import type { SnapshotSourceLimits } from './types.ts'; const FRAME_HEADER_BYTES = 4; const MAXIMUM_FRAME_BYTES = 16 * 1024 * 1024; const MAXIMUM_DURATION_MS = 120_000; -// Mirrors the native bridge's kMaximumDepth; limits.test.ts fails on drift. +// Mirrors the native bridge's kMaximumDepth. const MAXIMUM_DEPTH = 128; // Mirrors the native bridge's kMaximumRequestLevels: one native request reads at most this many // levels, and deeper levels arrive through continuations. From 26a51ec14d355f2d24b480869a37ab19805aa2f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 30 Oct 2026 16:09:41 +0100 Subject: [PATCH 3/4] test(ios): print the request window in the fixture failure message Co-Authored-By: Claude Opus 5.5 --- .../src/snapshot-source/fixtures/foreground-owner.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m b/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m index 26b608f674..c935dd6f5f 100644 --- a/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m +++ b/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m @@ -165,7 +165,7 @@ int main(int argc, const char *argv[]) requests++; if ([scenario hasPrefix:@"api-depth-"]) { NSUInteger start = [element isKindOfClass:NSNumber.class] ? [element unsignedIntegerValue] : 0; - require(levels == MIN(depth + 1 - start, kMaximumRequestLevels), @"native levels must include the root exactly once, at most kMaximumRequestLevels per request"); + require(levels == MIN(depth + 1 - start, kMaximumRequestLevels), [NSString stringWithFormat:@"native levels must include the root exactly once, at most %lu per request", (unsigned long)kMaximumRequestLevels]); NSUInteger end = MIN(start + levels, depth + 1); NSDictionary *tree = nil; for (NSUInteger level = end; level-- > start;) tree = @{@"UIAccessibilitySnapshotKeyAttributes": @{}, @"UIAccessibilitySnapshotKeyElement": @(level), @"UIAccessibilitySnapshotKeyChildrenCount": [scenario isEqual:@"api-depth-unknown"] ? [NSNull null] : @(level < depth ? 1 : 0), @"UIAccessibilitySnapshotKeyChildren": tree ? @[tree] : @[]}; From 3c242c98b02285ac83c1bcbd65a2a3a94b8d020a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 30 Oct 2026 16:22:06 +0100 Subject: [PATCH 4/4] refactor(ios): name the per-capture request budget kMaximumRequestsPerCapture Co-Authored-By: Claude Opus 5.5 --- apple/snapshot-bridge/SnapshotBridgeCapture.h | 2 +- apple/snapshot-bridge/SnapshotBridgeCapture.m | 4 ++-- .../src/snapshot-source/fixtures/foreground-owner.m | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/apple/snapshot-bridge/SnapshotBridgeCapture.h b/apple/snapshot-bridge/SnapshotBridgeCapture.h index 2d1d02fe71..555c941ad3 100644 --- a/apple/snapshot-bridge/SnapshotBridgeCapture.h +++ b/apple/snapshot-bridge/SnapshotBridgeCapture.h @@ -5,7 +5,7 @@ NS_ASSUME_NONNULL_BEGIN /// Native levels one request reads at most; deeper levels arrive through continuations. extern const NSUInteger kMaximumRequestLevels; /// Requests one acquisition may issue before it fails as continuation-budget-exhausted. -extern const NSUInteger kMaximumRequests; +extern const NSUInteger kMaximumRequestsPerCapture; typedef id _Nullable (^SnapshotElementReader)(id element, NSUInteger depth, NSUInteger nodes, NSError **error); diff --git a/apple/snapshot-bridge/SnapshotBridgeCapture.m b/apple/snapshot-bridge/SnapshotBridgeCapture.m index ea8e647c76..f8f6c01c6e 100644 --- a/apple/snapshot-bridge/SnapshotBridgeCapture.m +++ b/apple/snapshot-bridge/SnapshotBridgeCapture.m @@ -12,7 +12,7 @@ const NSUInteger kMaximumRequestLevels = 65; // Sized for the daemon's default depth bound (128 levels). Each root-to-leaf path needs one // continuation per accepted-depth window, so the budget scales with the depth it must cover. -const NSUInteger kMaximumRequests = 64; +const NSUInteger kMaximumRequestsPerCapture = 64; @interface SnapshotTreeCapture : NSObject @property(nonatomic, copy) SnapshotElementReader reader; @@ -31,7 +31,7 @@ - (nullable NSDictionary *)read:(id)element depth:(NSUInteger)depth error:(NSErr { NSUInteger attemptDepth = MIN(depth, self.acceptedDepth); for (NSUInteger retries = 0;; retries++) { - if (self.requests >= kMaximumRequests) { + if (self.requests >= kMaximumRequestsPerCapture) { if (error) *error = [NSError errorWithDomain:@"agent-device.snapshot" code:1 userInfo:@{NSLocalizedDescriptionKey: @"snapshot continuation request budget exhausted"}]; return nil; diff --git a/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m b/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m index c935dd6f5f..d7a96831a1 100644 --- a/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m +++ b/packages/platform-apple/src/snapshot-source/fixtures/foreground-owner.m @@ -174,7 +174,7 @@ int main(int argc, const char *argv[]) if ([element isEqual:@"root"]) return root; return @{@"UIAccessibilitySnapshotKeyAttributes": @{}, @"UIAccessibilitySnapshotKeyChildren": @[@{@"UIAccessibilitySnapshotKeyAttributes": @{}, @"UIAccessibilitySnapshotKeyChildren": @[]}]}; }, &truncated, NULL, &failure); - if (budget) require(!result && failure.code == 1 && requests == kMaximumRequests, @"request budget must fail without publishing partial content"); + if (budget) require(!result && failure.code == 1 && requests == kMaximumRequestsPerCapture, @"request budget must fail without publishing partial content"); else if ([scenario isEqual:@"identity"]) require(result == root && requests == 1, @"healthy capture must reuse the native tree"); else { NSUInteger count = 0; @@ -194,7 +194,7 @@ int main(int argc, const char *argv[]) NSString *expectedCode = nil; NSUInteger expectedCaptures = 0; if ([scenario isEqual:@"runtime-budget"]) { - expectedCaptures = kMaximumRequests; + expectedCaptures = kMaximumRequestsPerCapture; expectedCode = @"continuation-budget-exhausted"; } else if ([scenario hasPrefix:@"wide-"] || [scenario isEqual:@"zero-depth"]) { expectedCaptures = [scenario isEqual:@"wide-continuation"] ? 2 : 1;