Skip to content

Commit 16041a2

Browse files
authored
refactor(replay): move target classification into replay-port (#2663) (#3016)
* refactor(replay): move target classification into replay-port * chore(gates): declare replay classification package subpaths * test(replay): preserve closure budget across moved fixtures * ci: refresh replay classification checks * chore(gates): admit only walked base sources * test(replay): assert outside-root rename mapping
1 parent 4bbbfb7 commit 16041a2

14 files changed

Lines changed: 80 additions & 28 deletions

‎packages/ad-script/src/internal/target-annotation-classification.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* than script vocabulary. In practice its only real consumers were the
1111
* daemon's RECORD-time self-check (`@agent-device/selectors/target-evidence`)
1212
* and its REPLAY-time classification wrapper
13-
* (`src/daemon/replay/internal/session-replay-target-classification.ts`) — both
13+
* (`packages/replay-port/src/daemon-port/target-classification.ts`) — both
1414
* daemon files, neither reachable through `inspectAdReplay`/`runAdReplay`.
1515
* It interprets `TargetAnnotationV1` evidence semantics shared beyond the
1616
* engine (record-time AND replay-time both need the SAME verdict by

‎packages/ad-script/src/internal/target-annotation-identity.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* prefix matching over versioned `.ad` target-binding evidence, plus the
44
* bounded diagnostic diffs built on top of it. Both the writer (over
55
* `SnapshotNode`-derived values, `@agent-device/selectors/target-evidence`) and
6-
* replay-time verification (`src/daemon/replay/internal/session-replay-target-classification.ts` and
6+
* replay-time verification (`packages/replay-port/src/daemon-port/target-classification.ts` and
77
* `src/commands/interaction/runtime/selector-wait.ts`) share this verbatim so both
88
* sides compute the SAME identity/ancestry match by construction (#1478 P5
99
* review, "genuinely shared recording vocabulary" relocated to its owner).
@@ -199,7 +199,7 @@ export function matchesAncestryPrefix(
199199
// Diagnostic diffs (decision 3): bounded, best-effort mismatch descriptions
200200
// shared by the record-time classification core and replay-time verification
201201
// (#1478 P5 stage C2a) — moved here verbatim from
202-
// `src/daemon/replay/internal/session-replay-target-classification.ts` so both
202+
// `packages/replay-port/src/daemon-port/target-classification.ts` so both
203203
// callers depend on one definition instead of two copies.
204204
// ---------------------------------------------------------------------------
205205

‎packages/replay-port/package.json‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
"description": "Daemon-side replay port: the adapter over the ad-replay, ad-script and replay-test engines.",
88
"dependencies": {
99
"@agent-device/ad-script": "workspace:*",
10+
"@agent-device/capture-kit": "workspace:*",
1011
"@agent-device/contracts": "workspace:*",
1112
"@agent-device/kernel": "workspace:*",
1213
"@agent-device/selectors": "workspace:*"
1314
},
1415
"exports": {
15-
"./target-token": {
16-
"types": "./src/daemon-port/target-token.ts",
17-
"default": "./src/daemon-port/target-token.ts"
18-
},
1916
"./repair-hint": {
2017
"types": "./src/daemon-port/repair-hint.ts",
2118
"default": "./src/daemon-port/repair-hint.ts"
@@ -27,6 +24,18 @@
2724
"./session-replay-vars": {
2825
"types": "./src/session-replay-vars.ts",
2926
"default": "./src/session-replay-vars.ts"
27+
},
28+
"./target-classification": {
29+
"types": "./src/daemon-port/target-classification.ts",
30+
"default": "./src/daemon-port/target-classification.ts"
31+
},
32+
"./target-classification-fixtures": {
33+
"types": "./src/target-classification-fixtures.ts",
34+
"default": "./src/target-classification-fixtures.ts"
35+
},
36+
"./target-token": {
37+
"types": "./src/daemon-port/target-token.ts",
38+
"default": "./src/daemon-port/target-token.ts"
3039
}
3140
}
3241
}

src/daemon/replay/internal/__tests__/session-replay-target-classification.test.ts renamed to packages/replay-port/src/daemon-port/__tests__/target-classification.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { TargetAnnotationV1 } from '@agent-device/contracts/replay';
55
import { computeTargetEvidence } from '@agent-device/selectors/target-evidence';
66
import { buildSelectorChainForNode, resolveRecordedTarget } from '@agent-device/selectors';
77
import { resolvePressRecordingTarget } from '@agent-device/selectors/press-retarget';
8-
import { classifyReplayTarget } from '../session-replay-target-classification.ts';
8+
import { classifyReplayTarget } from '../target-classification.ts';
99
import { resolveUnverifiedWrapperControl } from '@agent-device/selectors/interaction-targeting';
1010
import {
1111
ELEMENT14_DISTINCT_SUBTREE_NODES,
@@ -15,7 +15,7 @@ import {
1515
bottomTabsRealCaptureFixture,
1616
recordArticleEvidence,
1717
toSnapshotNodes,
18-
} from './session-replay-target-classification-fixtures.ts';
18+
} from '@agent-device/replay-port/target-classification-fixtures';
1919

2020
/** Verified outcomes carry the verified member + matchCount (for the post-resolution guard). */
2121
function assertVerified(
@@ -30,8 +30,8 @@ function assertVerified(
3030

3131
const PLATFORM = 'ios' as const;
3232

33-
test('classifyReplayTarget: real-capture fixture verifies by @ref when the tree is unchanged', () => {
34-
const recorded = recordArticleEvidence();
33+
test('classifyReplayTarget: real-capture fixture verifies by @ref when the tree is unchanged', async () => {
34+
const recorded = await recordArticleEvidence();
3535
const replayNodes = bottomTabsRealCaptureFixture();
3636
const winner = replayNodes.find((node) => node.label === 'Article, unselected');
3737
assert.ok(winner);
@@ -47,8 +47,8 @@ test('classifyReplayTarget: real-capture fixture verifies by @ref when the tree
4747
assertVerified(result, { winnerRef: winner.ref, matchCount: 1 });
4848
});
4949

50-
test('classifyReplayTarget: real-capture fixture — a relabeled node is identity-mismatch (path 3)', () => {
51-
const recorded = recordArticleEvidence();
50+
test('classifyReplayTarget: real-capture fixture — a relabeled node is identity-mismatch (path 3)', async () => {
51+
const recorded = await recordArticleEvidence();
5252
const replayNodes = bottomTabsRealCaptureFixture();
5353
const winner = replayNodes.find((node) => node.label === 'Article, unselected');
5454
assert.ok(winner);

src/daemon/replay/internal/session-replay-target-classification.ts renamed to packages/replay-port/src/daemon-port/target-classification.ts

File renamed without changes.

src/daemon/replay/internal/__tests__/session-replay-target-classification-fixtures.ts renamed to packages/replay-port/src/target-classification-fixtures.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import assert from 'node:assert/strict';
22
import type { RawSnapshotNode, SnapshotNode } from '@agent-device/kernel/snapshot';
3-
import { computeTargetEvidence } from '@agent-device/selectors/target-evidence';
43
import type { TargetAnnotationV1 } from '@agent-device/contracts/replay';
54

65
export function toSnapshotNodes(raw: RawSnapshotNode[]): SnapshotNode[] {
@@ -81,7 +80,8 @@ export function bottomTabsRealCaptureFixture(): SnapshotNode[] {
8180
]);
8281
}
8382

84-
export function recordArticleEvidence(): TargetAnnotationV1 {
83+
export async function recordArticleEvidence(): Promise<TargetAnnotationV1> {
84+
const { computeTargetEvidence } = await import('@agent-device/selectors/target-evidence');
8585
const nodes = bottomTabsRealCaptureFixture();
8686
const winner = nodes.find((node) => node.label === 'Article, unselected');
8787
if (!winner) throw new Error('fixture missing Article tab');

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎scripts/__tests__/committed-source-tree.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ export function renamedSince(repoRoot: string, base: string): ReadonlyMap<string
5454
return renamed;
5555
}
5656

57+
/** A carried entry must have been a source the base closure walker could actually read. */
58+
export function baseProductionPathOf(
59+
repoRoot: string,
60+
entryFile: string,
61+
baseTree: SourceTreeReader,
62+
renamedFrom: ReadonlyMap<string, string>,
63+
): string | null {
64+
const baseFile = renamedFrom.get(entryFile) ?? entryFile;
65+
return WALKED_SOURCE.test(baseFile) &&
66+
isProductionSourceFile(baseFile) &&
67+
baseTree.isFile(path.resolve(repoRoot, baseFile))
68+
? baseFile
69+
: null;
70+
}
71+
5772
/** Every path tracked at `treeish`, repo-root-relative, from ONE `git ls-tree`. */
5873
function listCommittedTree(repoRoot: string, treeish: string): string[] {
5974
const listing = git(repoRoot, ['ls-tree', '-r', '--name-only', '-z', treeish]).toString('utf8');

‎scripts/__tests__/eager-closure-budgets.test.ts‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import os from 'node:os';
55
import path from 'node:path';
66
import { eagerClosureGraphOf } from '../../src/__tests__/eager-import-closure.fixtures.ts';
77
import {
8+
baseProductionPathOf,
89
createCommittedSourceTree,
910
headCommit,
1011
mergeBaseWithMain,
@@ -378,6 +379,8 @@ function mkGitFixtureRepo(prefix: string): string {
378379
fs.writeFileSync(path.join(pkgDir, 'src/facades/top.ts'), 'export const b = 2;\n');
379380
fs.writeFileSync(path.join(pkgDir, 'src/facades/nested/deep.ts'), 'export const c = 3;\n');
380381
fs.writeFileSync(path.join(pkgDir, 'src/facades/skip.test.ts'), 'export const d = 4;\n');
382+
fs.mkdirSync(path.join(repo, 'scripts'));
383+
fs.writeFileSync(path.join(repo, 'scripts/standalone.ts'), 'export const outside = 1;\n');
381384
execFileSync('git', ['init', '-q'], { cwd: repo });
382385
execFileSync('git', ['add', '.'], { cwd: repo });
383386
execFileSync(
@@ -409,9 +412,32 @@ test('a renamed entry is followed to its path at the base, not treated as first-
409412
execFileSync('git', ['mv', 'packages/demo/src/entry.ts', 'packages/demo/src/moved.ts'], {
410413
cwd: repo,
411414
});
412-
expect(renamedSince(repo, 'HEAD').get('packages/demo/src/moved.ts')).toBe(
415+
const renamed = renamedSince(repo, 'HEAD');
416+
const baseTree = createCommittedSourceTree(repo, 'HEAD');
417+
expect(renamed.get('packages/demo/src/moved.ts')).toBe('packages/demo/src/entry.ts');
418+
expect(baseProductionPathOf(repo, 'packages/demo/src/moved.ts', baseTree, renamed)).toBe(
413419
'packages/demo/src/entry.ts',
414420
);
421+
execFileSync(
422+
'git',
423+
['mv', 'packages/demo/src/facades/skip.test.ts', 'packages/demo/src/fixture.ts'],
424+
{ cwd: repo },
425+
);
426+
const withFixtureRename = renamedSince(repo, 'HEAD');
427+
expect(withFixtureRename.get('packages/demo/src/fixture.ts')).toBe(
428+
'packages/demo/src/facades/skip.test.ts',
429+
);
430+
expect(
431+
baseProductionPathOf(repo, 'packages/demo/src/fixture.ts', baseTree, withFixtureRename),
432+
).toBe(null);
433+
execFileSync('git', ['mv', 'scripts/standalone.ts', 'packages/demo/src/standalone.ts'], {
434+
cwd: repo,
435+
});
436+
const withOutsideRename = renamedSince(repo, 'HEAD');
437+
expect(withOutsideRename.get('packages/demo/src/standalone.ts')).toBe('scripts/standalone.ts');
438+
expect(
439+
baseProductionPathOf(repo, 'packages/demo/src/standalone.ts', baseTree, withOutsideRename),
440+
).toBe(null);
415441
});
416442

417443
test('discovery is recursive and reads TRACKED files only', () => {
@@ -520,10 +546,9 @@ const baseTree = createCommittedSourceTree(repoRoot, mergeBase);
520546
const renamedFrom = renamedSince(repoRoot, mergeBase);
521547
const entries = eagerClosureEntries(repoRoot);
522548

523-
/** The entry's path in the merge-base tree (renames followed), or null when it was not there. */
549+
/** The entry's readable source path in the merge-base tree, if it had one. */
524550
function basePathOf(entryFile: string): string | null {
525-
const file = renamedFrom.get(entryFile) ?? entryFile;
526-
return baseTree.isFile(absolute(file)) ? file : null;
551+
return baseProductionPathOf(repoRoot, entryFile, baseTree, renamedFrom);
527552
}
528553

529554
const baseGraphs = new Map<string, ReadonlyMap<string, string | null>>();

‎src/daemon/replay/internal/__tests__/session-replay-dispatch-selector-miss.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
import {
4646
bottomTabsRealCaptureFixture,
4747
recordArticleEvidence,
48-
} from './session-replay-target-classification-fixtures.ts';
48+
} from '@agent-device/replay-port/target-classification-fixtures';
4949
import {
5050
captureSnapshotThroughLegacyDispatchFixture,
5151
legacyDispatchCapture,
@@ -108,7 +108,7 @@ function assertDivergenceShape(
108108
test('(a) an ANNOTATED press whose dispatch throws a selector-miss yields REPLAY_DIVERGENCE, not COMMAND_FAILED', async () => {
109109
const root = mkdtempForTestSync('agent-device-replay-dispatch-miss-annotated-');
110110
const { sessionStore, sessionName } = setupSession(root);
111-
const evidence = recordArticleEvidence();
111+
const evidence = await recordArticleEvidence();
112112
const filePath = writeReplayFile(root, [
113113
`# agent-device:target-v1 ${JSON.stringify(evidence)}`,
114114
'click id="article"',

0 commit comments

Comments
 (0)