Skip to content

Commit c941d7c

Browse files
committed
refactor(platforms): keep platform parsing with its family; give snapshot shaping its capture owner
ADR-0019's amendment forbids satisfying R13 by moving implementation into contracts, so this wave's two relocations invert instead of sink: - The Android dumpsys foreground parser returns to @agent-device/platform-android with its owning test, and contracts/android-observation goes back to observation vocabulary only. src/platforms/android/app-helpers exposes createAndroidAppStateReader(parseForegroundApp) and never imports upward; the composition seam in src/sdk/android-adb.ts injects the root-composed parser, keeping the published getAndroidAppStateWithAdb(adb) signature intact. - The desktop snapshot projection moves to @agent-device/capture-kit/snapshot-desktop-projection beside the rest of the capture-side snapshot behavior; contracts exports the snapshot-scope vocabulary it consumes. The #1832 history narration in the projection test is gone; the test name and golden fixture carry the invariant. Co-Authored-By: Claude <[email protected]> Claude-Session: https://claude.ai/code/session_018VngeKZH6zBuJzNBk5YzUH
1 parent 2f63aa9 commit c941d7c

18 files changed

Lines changed: 103 additions & 69 deletions

‎packages/capture-kit/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
"types": "./src/screenshot-diff-pixels.ts",
4747
"default": "./src/screenshot-diff-pixels.ts"
4848
},
49+
"./snapshot-desktop-projection": {
50+
"types": "./src/snapshot-desktop-projection.ts",
51+
"default": "./src/snapshot-desktop-projection.ts"
52+
},
4953
"./snapshot-occlusion": {
5054
"types": "./src/snapshot-occlusion.ts",
5155
"default": "./src/snapshot-occlusion.ts"

packages/contracts/src/snapshot-desktop-projection.test.ts renamed to packages/capture-kit/src/snapshot-desktop-projection.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import fs from 'node:fs';
33
import path from 'node:path';
44
import { scopeSnapshotNodes } from './snapshot-desktop-projection.ts';
55

6-
// Golden scope-policy leg (#1832 C2): the post-wire pass must agree with
7-
// contracts/fixtures/snapshot-scope-policy.json — the same table the Android projection and the
8-
// shared predicate are asserted against. Fixture position rides in `rect.x` so the check is
9-
// text-independent.
106
test('scopeSnapshotNodes agrees with every golden scope-policy table case', () => {
117
const cases = JSON.parse(
128
fs.readFileSync(

packages/contracts/src/snapshot-desktop-projection.ts renamed to packages/capture-kit/src/snapshot-desktop-projection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { RawSnapshotNode } from '@agent-device/kernel/snapshot';
2-
import type { SnapshotOptions, SnapshotResult } from './interactor-types.ts';
2+
import type { SnapshotOptions, SnapshotResult } from '@agent-device/contracts/interactor-types';
33
import {
44
findSnapshotScopeRange,
55
normalizeSnapshotScope,
66
reindexSnapshotNodes,
7-
} from './snapshot-scope.ts';
7+
} from '@agent-device/contracts/snapshot-scope';
88

99
const INTERACTIVE_ROLE_TOKENS = [
1010
'button',
@@ -34,7 +34,7 @@ export function shapeDesktopSurfaceSnapshot(
3434
return { ...data, nodes };
3535
}
3636

37-
/** The shared scope specification applied post-wire (`./snapshot-scope.ts`). */
37+
/** The shared scope specification applied post-wire (contracts' `snapshot-scope`). */
3838
export function scopeSnapshotNodes(
3939
nodes: RawSnapshotNode[],
4040
scope: string,

‎packages/contracts/package.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,6 @@
419419
"types": "./src/facades/snapshot.ts",
420420
"default": "./src/facades/snapshot.ts"
421421
},
422-
"./snapshot-desktop-projection": {
423-
"types": "./src/snapshot-desktop-projection.ts",
424-
"default": "./src/snapshot-desktop-projection.ts"
425-
},
426422
"./snapshot-presentation": {
427423
"types": "./src/snapshot-presentation.ts",
428424
"default": "./src/snapshot-presentation.ts"
@@ -431,6 +427,10 @@
431427
"types": "./src/snapshot-runtime.ts",
432428
"default": "./src/snapshot-runtime.ts"
433429
},
430+
"./snapshot-scope": {
431+
"types": "./src/snapshot-scope.ts",
432+
"default": "./src/snapshot-scope.ts"
433+
},
434434
"./snapshot-timeout-evidence": {
435435
"types": "./src/snapshot-timeout-evidence.ts",
436436
"default": "./src/snapshot-timeout-evidence.ts"

‎packages/contracts/src/android-observation.ts‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,3 @@ export type AndroidObservationAdapter = Readonly<{
4848
readScreenSize(device: DeviceInfo): Promise<Readonly<{ width: number; height: number }>>;
4949
isPermissionPackage(packageName: string): Promise<boolean>;
5050
}>;
51-
52-
const ANDROID_FOCUS_MARKERS = [
53-
'mCurrentFocus=Window{',
54-
'mFocusedApp=AppWindowToken{',
55-
'mResumedActivity:',
56-
'ResumedActivity:',
57-
] as const;
58-
59-
/** Extracts the foreground package/activity from `dumpsys window`/`activity` output. */
60-
export function parseAndroidForegroundApp(text: string): AppStateRuntimeResult | null {
61-
const lines = text.split('\n');
62-
for (const marker of ANDROID_FOCUS_MARKERS) {
63-
for (const line of lines) {
64-
const markerIndex = line.indexOf(marker);
65-
if (markerIndex === -1) continue;
66-
const parsed = parseAndroidComponentFromSegment(line.slice(markerIndex + marker.length));
67-
if (parsed) return parsed;
68-
}
69-
}
70-
return null;
71-
}
72-
73-
function parseAndroidComponentFromSegment(segment: string): AppStateRuntimeResult | null {
74-
const match = segment.match(/\b([A-Za-z][A-Za-z0-9_]*(?:\.[A-Za-z0-9_]+)+)\/([A-Za-z0-9_.$]+)/);
75-
return match?.[1] && match[2] ? { package: match[1], activity: match[2] } : null;
76-
}

packages/contracts/src/android-observation.test.ts renamed to packages/platform-android/src/app-state.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from 'vitest';
2-
import { parseAndroidForegroundApp } from './android-observation.ts';
2+
import { parseAndroidForegroundApp } from './app-state.ts';
33

44
test('parses Android window and activity foreground markers', () => {
55
expect(

‎packages/platform-android/src/app-state.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type {
44
AppStateRuntimeResult,
55
} from '@agent-device/contracts/app-state-runtime';
66
import type { DeviceInfo } from '@agent-device/kernel/device';
7-
import { parseAndroidForegroundApp } from '@agent-device/contracts/android-observation';
87

98
export type AndroidAppStateHost = Readonly<{
109
run(
@@ -22,6 +21,12 @@ const ACTIVITY_COMMANDS = [
2221
['shell', 'dumpsys', 'activity', 'activities'],
2322
['shell', 'dumpsys', 'activity'],
2423
] as const;
24+
const ANDROID_FOCUS_MARKERS = [
25+
'mCurrentFocus=Window{',
26+
'mFocusedApp=AppWindowToken{',
27+
'mResumedActivity:',
28+
'ResumedActivity:',
29+
] as const;
2530

2631
export async function readAndroidAppState(
2732
host: AndroidAppStateHost,
@@ -36,6 +41,19 @@ export async function readAndroidAppState(
3641
return {};
3742
}
3843

44+
export function parseAndroidForegroundApp(text: string): AppStateRuntimeResult | null {
45+
const lines = text.split('\n');
46+
for (const marker of ANDROID_FOCUS_MARKERS) {
47+
for (const line of lines) {
48+
const markerIndex = line.indexOf(marker);
49+
if (markerIndex === -1) continue;
50+
const parsed = parseAndroidComponentFromSegment(line.slice(markerIndex + marker.length));
51+
if (parsed) return parsed;
52+
}
53+
}
54+
return null;
55+
}
56+
3957
async function readAndroidFocus(
4058
host: AndroidAppStateHost,
4159
device: DeviceInfo,
@@ -51,3 +69,8 @@ async function readAndroidFocus(
5169
}
5270
return null;
5371
}
72+
73+
function parseAndroidComponentFromSegment(segment: string): AppStateRuntimeResult | null {
74+
const match = segment.match(/\b([A-Za-z][A-Za-z0-9_]*(?:\.[A-Za-z0-9_]+)+)\/([A-Za-z0-9_.$]+)/);
75+
return match?.[1] && match[2] ? { package: match[1], activity: match[2] } : null;
76+
}

‎packages/platform-android/src/index.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ export async function readAndroidAppState(
5555
return await read(host, device, signal);
5656
}
5757

58+
export async function parseAndroidForegroundApp(
59+
text: string,
60+
): Promise<Readonly<{ package?: string; activity?: string }> | null> {
61+
const { parseAndroidForegroundApp: parse } = await import('./app-state.ts');
62+
return parse(text);
63+
}
64+
5865
export const runtimeModule = Object.freeze({
5966
...metadata,
6067
loadRuntime: async (host) => {

‎scripts/layering/package-boundaries.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ const CONTRACT_EXPORTS = [
149149
'@agent-device/contracts/settings',
150150
'@agent-device/contracts/settings-runtime',
151151
'@agent-device/contracts/snapshot',
152-
'@agent-device/contracts/snapshot-desktop-projection',
153152
'@agent-device/contracts/snapshot-presentation',
154153
'@agent-device/contracts/snapshot-runtime',
154+
'@agent-device/contracts/snapshot-scope',
155155
'@agent-device/contracts/snapshot-timeout-evidence',
156156
'@agent-device/contracts/startup-recovery-fence',
157157
'@agent-device/contracts/touch-runtime',
@@ -465,6 +465,7 @@ test('the real tree parses, declares, and passes R11', () => {
465465
'@agent-device/capture-kit/png-worker-client',
466466
'@agent-device/capture-kit/screenshot-density',
467467
'@agent-device/capture-kit/screenshot-diff-pixels',
468+
'@agent-device/capture-kit/snapshot-desktop-projection',
468469
'@agent-device/capture-kit/snapshot-occlusion',
469470
'@agent-device/capture-kit/snapshot-quality-backend-capabilities',
470471
'@agent-device/capture-kit/snapshot-quality-verdict',

‎src/__tests__/eager-closure-budgets.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export const FACADE_BUDGETS: Readonly<Record<string, number>> = Object.freeze({
130130
'packages/capture-kit/src/screenshot-density.ts': 6,
131131
'packages/capture-kit/src/screenshot-diff-pixels.ts': 1,
132132
'packages/capture-kit/src/mobile-snapshot-semantics.ts': 10,
133+
'packages/capture-kit/src/snapshot-desktop-projection.ts': 2,
133134
'packages/capture-kit/src/snapshot-occlusion.ts': 10,
134135
'packages/capture-kit/src/snapshot-quality-backend-capabilities.ts': 1,
135136
'packages/capture-kit/src/snapshot-quality-verdict.ts': 2,
@@ -238,9 +239,9 @@ export const FACADE_BUDGETS: Readonly<Record<string, number>> = Object.freeze({
238239
'packages/contracts/src/scroll-runtime.ts': 4,
239240
'packages/contracts/src/selector-observation-runtime.ts': 1,
240241
'packages/contracts/src/settings.ts': 3,
241-
'packages/contracts/src/snapshot-desktop-projection.ts': 2,
242242
'packages/contracts/src/snapshot-presentation.ts': 2,
243243
'packages/contracts/src/snapshot-runtime.ts': 3,
244+
'packages/contracts/src/snapshot-scope.ts': 1,
244245
'packages/contracts/src/snapshot-timeout-evidence.ts': 1,
245246
'packages/contracts/src/startup-recovery-fence.ts': 1,
246247
'packages/contracts/src/tv-remote.ts': 3,

0 commit comments

Comments
 (0)