|
| 1 | +/** |
| 2 | + * #1271 stage 2 (ADR 0012 decision 6 amendment): the repair-segment default |
| 3 | + * exclusion of observation-only commands, and the `--record` opt-in that |
| 4 | + * forces one back in. |
| 5 | + * |
| 6 | + * Provider-backed rather than a handler unit test on purpose: the exclusion is |
| 7 | + * a DAEMON-side recording contract every surface inherits, so it has to be |
| 8 | + * proven through the real request router, session store, replay runtime, and |
| 9 | + * script writer — only the device provider is faked. The counterpart |
| 10 | + * `--no-record` coverage lives in `android-lifecycle.test.ts`. |
| 11 | + */ |
| 12 | +import assert from 'node:assert/strict'; |
| 13 | +import fs from 'node:fs'; |
| 14 | +import os from 'node:os'; |
| 15 | +import path from 'node:path'; |
| 16 | +import { test } from 'vitest'; |
| 17 | +import { ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT } from '../../../src/__tests__/test-utils/index.ts'; |
| 18 | +import type { AndroidAdbProvider } from '../../../src/platforms/android/adb-executor.ts'; |
| 19 | +import { assertRpcError, assertRpcOk } from './assertions.ts'; |
| 20 | +import { androidSettingsXml, androidSnapshotHelperOutput } from './android-world.ts'; |
| 21 | +import { PROVIDER_SCENARIO_ANDROID } from './fixtures.ts'; |
| 22 | +import { createProviderScenarioHarness } from './harness.ts'; |
| 23 | + |
| 24 | +const SEARCH_SELECTOR = 'id=com.android.settings:id/search'; |
| 25 | + |
| 26 | +test('Provider-backed integration: a repair-armed segment excludes diagnostic reads by default and --record forces the corrective read into the heal', async () => { |
| 27 | + const adbProvider: AndroidAdbProvider = { |
| 28 | + snapshotHelperArtifact: ANDROID_SNAPSHOT_HELPER_FIXTURE_ARTIFACT, |
| 29 | + exec: async (args) => androidRepairAdbResult(args), |
| 30 | + }; |
| 31 | + const daemon = await createProviderScenarioHarness({ |
| 32 | + androidAdbProvider: () => adbProvider, |
| 33 | + deviceInventoryProvider: async () => [PROVIDER_SCENARIO_ANDROID], |
| 34 | + }); |
| 35 | + const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-repair-record-exclusion-')); |
| 36 | + |
| 37 | + try { |
| 38 | + const client = daemon.client(); |
| 39 | + const selection = { platform: 'android' as const, serial: PROVIDER_SCENARIO_ANDROID.id }; |
| 40 | + |
| 41 | + const open = await client.apps.open({ app: 'settings', ...selection }); |
| 42 | + assert.equal(open.device?.id, PROVIDER_SCENARIO_ANDROID.id); |
| 43 | + |
| 44 | + // Step 2 diverges: a selector that matches nothing in the current tree. |
| 45 | + const repairPath = path.join(tempRoot, 'repair.ad'); |
| 46 | + fs.writeFileSync( |
| 47 | + repairPath, |
| 48 | + ['snapshot -i', 'press label="NoSuchControl"', `get text ${SEARCH_SELECTOR}`, ''].join('\n'), |
| 49 | + ); |
| 50 | + |
| 51 | + // --- Arm the repair transaction (R1): `replay --save-script` before step 1. --- |
| 52 | + const divergenceError = await client.replay |
| 53 | + .run({ path: repairPath, saveScript: true, ...selection }) |
| 54 | + .then( |
| 55 | + () => null, |
| 56 | + (error: unknown) => error as { code?: string; details?: Record<string, unknown> }, |
| 57 | + ); |
| 58 | + assert.ok(divergenceError, 'expected the armed replay to diverge on the missing selector'); |
| 59 | + assert.equal(divergenceError.code, 'REPLAY_DIVERGENCE'); |
| 60 | + const divergenceReport = divergenceError.details?.divergence as { |
| 61 | + resume: { allowed: boolean; from: number; planDigest: string; repairSessionHeld?: boolean }; |
| 62 | + }; |
| 63 | + assert.equal(divergenceReport.resume.allowed, true); |
| 64 | + // R7/C1: the divergence reports the repair transaction as held — the exact |
| 65 | + // signal the #1271 stage-1 guidance clause is gated on. |
| 66 | + assert.equal(divergenceReport.resume.repairSessionHeld, true); |
| 67 | + // The session is repair-armed: `saveScriptBoundary` is the boundary the |
| 68 | + // exclusion keys off (an ordinary `open --save-script` never sets it). |
| 69 | + const armedBoundary = daemon.session()?.saveScriptBoundary; |
| 70 | + assert.equal(typeof armedBoundary, 'number'); |
| 71 | + |
| 72 | + // --- The exclusion contrast: the SAME observation-only command, against the |
| 73 | + // same selector, run twice inside the repair segment — differing only in |
| 74 | + // `--record`. --- |
| 75 | + const actionsBeforeReads = daemon.session()?.actions.length ?? 0; |
| 76 | + |
| 77 | + // (a) A diagnostic read used to LOCATE the target: excluded by default, |
| 78 | + // with no `--no-record` needed (the #1271 stage-1 foot-gun). |
| 79 | + const diagnosticRead = await daemon.callCommand('get', ['text', SEARCH_SELECTOR], { |
| 80 | + ...selection, |
| 81 | + }); |
| 82 | + assertRpcOk(diagnosticRead); |
| 83 | + assert.equal( |
| 84 | + daemon.session()?.actions.length, |
| 85 | + actionsBeforeReads, |
| 86 | + 'a diagnostic read inside a repair segment must not be recorded', |
| 87 | + ); |
| 88 | + |
| 89 | + // (b) The corrective read (the wave-3 E3 shape: the diverged step is itself |
| 90 | + // a read), forced into the heal with `--record`. |
| 91 | + const correctiveRead = await daemon.callCommand('get', ['text', SEARCH_SELECTOR], { |
| 92 | + ...selection, |
| 93 | + record: true, |
| 94 | + }); |
| 95 | + assertRpcOk(correctiveRead); |
| 96 | + const armedActions = daemon.session()?.actions ?? []; |
| 97 | + assert.equal(armedActions.length, actionsBeforeReads + 1); |
| 98 | + assert.equal(armedActions.at(-1)?.command, 'get'); |
| 99 | + |
| 100 | + // (c) `--record` and `--no-record` are opposite intents for one action. |
| 101 | + const conflictingFlags = await daemon.callCommand('get', ['text', SEARCH_SELECTOR], { |
| 102 | + ...selection, |
| 103 | + record: true, |
| 104 | + noRecord: true, |
| 105 | + }); |
| 106 | + assertRpcError(conflictingFlags, 'INVALID_ARGS', /--record and --no-record are mutually/); |
| 107 | + |
| 108 | + // --- Resume past the diverged step, completing the plan (transaction COMPLETE). --- |
| 109 | + const resumed = await client.replay.run({ |
| 110 | + path: repairPath, |
| 111 | + resumeFrom: divergenceReport.resume.from + 1, |
| 112 | + resumePlanDigest: divergenceReport.resume.planDigest, |
| 113 | + ...selection, |
| 114 | + }); |
| 115 | + assert.equal(resumed.replayed, 1); |
| 116 | + |
| 117 | + // --- Commit the heal and read what actually landed on disk. --- |
| 118 | + const close = await daemon.callCommand('close', [], { saveScript: true }); |
| 119 | + assertRpcOk(close); |
| 120 | + const healedPath = path.join(tempRoot, 'repair.healed.ad'); |
| 121 | + assert.equal(fs.existsSync(healedPath), true, 'the completed repair must publish a healed .ad'); |
| 122 | + const healedScript = fs.readFileSync(healedPath, 'utf8'); |
| 123 | + |
| 124 | + // The heal carries EXACTLY ONE `get text` line: the `--record`ed corrective |
| 125 | + // read. The identical diagnostic read that ran first is absent — the whole |
| 126 | + // point of the amendment, and the reason a blanket read-exclusion would be |
| 127 | + // unsafe (it would drop this line too, silently). |
| 128 | + const getLines = healedScript |
| 129 | + .split('\n') |
| 130 | + .filter((line) => line.trim().startsWith('get ') || line.trim().startsWith('get\t')); |
| 131 | + assert.equal(getLines.length, 1, `expected exactly one recorded get, got: ${healedScript}`); |
| 132 | + assert.match(getLines[0]!, /^get\s+"?text"?\s+/); |
| 133 | + assert.match(getLines[0]!, /com\.android\.settings:id\/search/); |
| 134 | + } finally { |
| 135 | + fs.rmSync(tempRoot, { recursive: true, force: true }); |
| 136 | + await daemon.close(); |
| 137 | + } |
| 138 | +}); |
| 139 | + |
| 140 | +function androidRepairAdbResult(args: string[]): { |
| 141 | + stdout: string; |
| 142 | + stderr: string; |
| 143 | + exitCode: number; |
| 144 | +} { |
| 145 | + if (args.join(' ') === 'shell getprop sys.boot_completed') { |
| 146 | + return { stdout: '1\n', stderr: '', exitCode: 0 }; |
| 147 | + } |
| 148 | + if (args.join(' ') === 'shell dumpsys window windows') { |
| 149 | + return { |
| 150 | + stdout: 'mCurrentFocus=Window{42 u0 com.android.settings/.Settings}\n', |
| 151 | + stderr: '', |
| 152 | + exitCode: 0, |
| 153 | + }; |
| 154 | + } |
| 155 | + if (args.join(' ').startsWith('shell am instrument ')) { |
| 156 | + return { |
| 157 | + stdout: androidSnapshotHelperOutput(androidSettingsXml('Display')), |
| 158 | + stderr: '', |
| 159 | + exitCode: 0, |
| 160 | + }; |
| 161 | + } |
| 162 | + return { stdout: '', stderr: '', exitCode: 0 }; |
| 163 | +} |
0 commit comments