Skip to content

Commit ca4bb1f

Browse files
committed
fix(replay): mark only substituted echo placeholders live and split complex helpers
Echo protection now reports the placeholders it actually wrote into the result (occurrence counts before and after), so a registered `foo` beside `foobar` no longer marks `${FOO}` live when only `${FOOBAR}` was inserted, and a literal `${FOO}` in the same action stays escaped. `recordActionEntry`, `parseInterpolation`, and `resolveReplayString` are split into named helpers at their decision steps, below the complexity thresholds. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016qAu53rMak1bXMuR5U2gF4
1 parent 9fb746b commit ca4bb1f

4 files changed

Lines changed: 158 additions & 125 deletions

File tree

‎packages/ad-script/src/internal/vars.ts‎

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -171,28 +171,43 @@ function parseInterpolation(
171171
raw: string,
172172
start: number,
173173
): ParsedInterpolation | FailedInterpolation | null {
174-
let i = start + 2;
175-
if (i >= raw.length || !isInterpolationNameStart(raw[i]!)) return null;
176-
i += 1;
177-
while (i < raw.length && isInterpolationNameChar(raw[i]!)) i += 1;
178-
const key = raw.slice(start + 2, i);
179-
if (raw[i] === '}') return { key, fallback: undefined, end: i + 1 };
180-
if (raw[i] !== ':' || raw[i + 1] !== '-') return null;
181-
i += 2;
174+
const nameEnd = readInterpolationNameEnd(raw, start + 2);
175+
if (nameEnd === null) return null;
176+
const key = raw.slice(start + 2, nameEnd);
177+
if (raw[nameEnd] === '}') return { key, fallback: undefined, end: nameEnd + 1 };
178+
if (raw[nameEnd] !== ':' || raw[nameEnd + 1] !== '-') return null;
179+
return readInterpolationFallback(raw, key, nameEnd + 2);
180+
}
181+
182+
/** The index just past a variable name starting at `start`, or `null` when none starts there. */
183+
function readInterpolationNameEnd(raw: string, start: number): number | null {
184+
if (start >= raw.length || !isInterpolationNameStart(raw[start]!)) return null;
185+
let end = start + 1;
186+
while (end < raw.length && isInterpolationNameChar(raw[end]!)) end += 1;
187+
return end;
188+
}
189+
190+
/** The `:-fallback}` text from `start`, with `\` escapes applied. */
191+
function readInterpolationFallback(
192+
raw: string,
193+
key: string,
194+
start: number,
195+
): ParsedInterpolation | FailedInterpolation {
182196
let fallback = '';
197+
let i = start;
183198
while (i < raw.length) {
184199
const ch = raw[i]!;
185200
if (ch === '}') return { key, fallback, end: i + 1 };
186-
if (ch === '\\') {
187-
const next = raw[i + 1];
188-
if (next === undefined) return { abortEnd: raw.length };
189-
if (isLineTerminator(next)) return { abortEnd: i + 2 };
190-
fallback += next;
191-
i += 2;
201+
if (ch !== '\\') {
202+
fallback += ch;
203+
i += 1;
192204
continue;
193205
}
194-
fallback += ch;
195-
i += 1;
206+
const next = raw[i + 1];
207+
if (next === undefined) return { abortEnd: raw.length };
208+
if (isLineTerminator(next)) return { abortEnd: i + 2 };
209+
fallback += next;
210+
i += 2;
196211
}
197212
return { abortEnd: raw.length };
198213
}
@@ -226,44 +241,41 @@ export function resolveReplayString(
226241
let out = '';
227242
let i = 0;
228243
while (i < raw.length) {
229-
const ch = raw[i]!;
230-
if (ch === '\\' && raw.startsWith('${', i + 1)) {
231-
out += '${';
232-
i += 3;
233-
continue;
234-
}
235-
if (ch === '$' && raw[i + 1] === '{') {
236-
const parsed = parseInterpolation(raw, i);
237-
if (parsed !== null && 'abortEnd' in parsed) {
238-
out += raw.slice(i, parsed.abortEnd);
239-
i = parsed.abortEnd;
240-
continue;
241-
}
242-
if (parsed !== null) {
243-
const { key, fallback } = parsed;
244-
if (Object.prototype.hasOwnProperty.call(scope.values, key)) {
245-
if (isReservedNamespaceKey(key)) {
246-
(scope.expandedBuiltinNames ??= new Set()).add(key);
247-
}
248-
out += String(scope.values[key]);
249-
} else if (fallback !== undefined) {
250-
out += fallback;
251-
} else {
252-
throw new AppError(
253-
'INVALID_ARGS',
254-
`Unresolved variable \${${key}} at ${loc.file}:${loc.line}.`,
255-
);
256-
}
257-
i = parsed.end;
258-
continue;
259-
}
260-
}
261-
out += ch;
262-
i += 1;
244+
const step = readReplayStringStep(raw, i, scope, loc);
245+
out += step.text;
246+
i = step.next;
263247
}
264248
return out;
265249
}
266250

251+
/** The resolved text of the token at `i`: an escape, an interpolation, or one literal character. */
252+
function readReplayStringStep(
253+
raw: string,
254+
i: number,
255+
scope: ReplayVarScope,
256+
loc: { file: string; line: number },
257+
): { text: string; next: number } {
258+
if (raw[i] === '\\' && raw.startsWith('${', i + 1)) return { text: '${', next: i + 3 };
259+
if (raw[i] !== '$' || raw[i + 1] !== '{') return { text: raw[i]!, next: i + 1 };
260+
const parsed = parseInterpolation(raw, i);
261+
if (parsed === null) return { text: '$', next: i + 1 };
262+
if ('abortEnd' in parsed) return { text: raw.slice(i, parsed.abortEnd), next: parsed.abortEnd };
263+
return { text: resolveInterpolationValue(parsed, scope, loc), next: parsed.end };
264+
}
265+
266+
function resolveInterpolationValue(
267+
{ key, fallback }: ParsedInterpolation,
268+
scope: ReplayVarScope,
269+
loc: { file: string; line: number },
270+
): string {
271+
if (Object.prototype.hasOwnProperty.call(scope.values, key)) {
272+
if (isReservedNamespaceKey(key)) (scope.expandedBuiltinNames ??= new Set()).add(key);
273+
return String(scope.values[key]);
274+
}
275+
if (fallback !== undefined) return fallback;
276+
throw new AppError('INVALID_ARGS', `Unresolved variable \${${key}} at ${loc.file}:${loc.line}.`);
277+
}
278+
267279
export function resolveReplayAction(
268280
action: SessionAction,
269281
scope: ReplayVarScope,

‎packages/daemon/src/core/__tests__/session-action-recorder.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,27 @@ test('a stdin fill with --record-as is recorded as its ${VAR} placeholder', () =
892892
expect(session.actions[0]?.positionals).toEqual(['id="password"', '${PASSWORD}']);
893893
expect(JSON.stringify(session.actions)).not.toContain(secret);
894894
});
895+
896+
test('an action lists only the placeholders echo protection actually wrote as live', () => {
897+
const session = makeIosSession('default', { scriptPublication: authoringPublication('armed') });
898+
for (const [literal, recordAs] of [
899+
['foo', 'FOO'],
900+
['foobar', 'FOOBAR'],
901+
] as const) {
902+
recordAction(session, {
903+
command: 'fill',
904+
positionals: ['id="field"', literal],
905+
flags: { recordAs },
906+
result: { text: literal },
907+
});
908+
}
909+
const echo = recordAction(session, {
910+
command: 'click',
911+
positionals: ['@e3'],
912+
flags: {},
913+
result: { refLabel: 'Hello foobar ${FOO}' },
914+
});
915+
916+
expect(echo?.result?.refLabel).toBe('Hello ${FOOBAR} ${FOO}');
917+
expect(echo?.livePlaceholders).toEqual(['FOOBAR']);
918+
});

‎packages/daemon/src/core/__tests__/session-script-writer.test.ts‎

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ test('write() escapes recorded literal ${...} text and keeps only recorder-inser
8787
const root = mkdtempForTestSync('agent-device-script-writer-literal-interpolation-');
8888
const writer = new SessionScriptWriter(path.join(root, 'sessions'), hostCwd);
8989
const session = makeAuthoringSession('default');
90+
const targetEvidence = {
91+
role: 'button',
92+
label: 'Pay ${amount}',
93+
sibling: 0,
94+
viewportOrder: 0,
95+
ancestry: [{ role: 'form', label: 'Checkout ${step}' }],
96+
verification: 'verified' as const,
97+
};
9098
const entries = [
9199
{ command: 'type', positionals: ['cost ${amount} \\${raw}'], flags: {} },
92100
{
@@ -105,18 +113,16 @@ test('write() escapes recorded literal ${...} text and keeps only recorder-inser
105113
flags: {},
106114
result: { text: 'hunter2', selectorChain: ['label="Confirm"'] },
107115
},
108-
{
109-
command: 'click',
110-
positionals: ['@e3'],
111-
flags: {},
112-
result: { refLabel: 'Signed in with hunter2' },
113-
},
116+
{ command: 'click', positionals: ['@e3'], flags: {}, result: { refLabel: 'Hi hunter2' } },
117+
// Ref labels and annotations are matched literally and never interpolated.
118+
{ command: 'click', positionals: ['@e4'], flags: {}, result: { refLabel: 'Pay ${amount}' } },
119+
{ command: 'click', positionals: ['label="Pay ${amount}"'], flags: {}, targetEvidence },
114120
];
115121
for (const entry of entries) recordActionEntry(session, { result: {}, ...entry }, hostCwd);
116122

117123
const { script, parsed } = writeAndParse(writer, session);
118124
expect(script).not.toContain('hunter2');
119-
const scope = { values: { PASSWORD: 'secret', amount: 'WRONG', name: 'WRONG' } };
125+
const scope = { values: { PASSWORD: 'secret', amount: 'WRONG', name: 'WRONG', step: 'WRONG' } };
120126
const resolved = parsed.actions.map((entry, index) =>
121127
resolveReplayAction(entry, scope, { file: 'test.ad', line: index + 2 }),
122128
);
@@ -126,39 +132,11 @@ test('write() escapes recorded literal ${...} text and keeps only recorder-inser
126132
expect(resolved[3]?.positionals).toEqual(['${PASSWORD}']);
127133
expect(resolved[4]?.positionals).toEqual(['label="Other"', '${PASSWORD}']);
128134
expect(resolved[5]?.positionals).toEqual(['label="Confirm"', 'secret']);
129-
expect(resolved[6]?.flags.snapshotScope).toBe('Signed in with secret');
130-
});
131-
132-
test('write() keeps ref labels and target annotations verbatim because replay matches them literally', () => {
133-
const root = mkdtempForTestSync('agent-device-script-writer-literal-identity-');
134-
const writer = new SessionScriptWriter(path.join(root, 'sessions'), hostCwd);
135-
const targetEvidence = {
136-
role: 'button',
137-
label: 'Pay ${amount}',
138-
ancestry: [{ role: 'form', label: 'Checkout ${step}' }],
139-
sibling: 0,
140-
viewportOrder: 0,
141-
verification: 'verified' as const,
142-
};
143-
const session = makeAuthoringSession('default', {
144-
actions: [
145-
action({ command: 'click', positionals: ['@e3'], result: { refLabel: 'Pay ${amount}' } }),
146-
action({ command: 'click', positionals: ['label="Pay ${amount}"'], targetEvidence }),
147-
],
148-
});
149-
150-
const scope = { values: { amount: 'WRONG', step: 'WRONG' } };
151-
const [snapshot, refClick, selectorClick] = writeAndParse(writer, session).parsed.actions.map(
152-
(entry, index) => resolveReplayAction(entry, scope, { file: 'test.ad', line: index + 2 }),
153-
);
154-
// The label also scopes the recorded snapshot, a flag replay does interpolate.
155-
expect(snapshot?.flags.snapshotScope).toBe('Pay ${amount}');
156-
expect(refClick?.result?.refLabel).toBe('Pay ${amount}');
157-
expect(selectorClick?.positionals).toEqual(['label="Pay ${amount}"']);
158-
expect(selectorClick?.targetEvidence).toMatchObject({
159-
label: 'Pay ${amount}',
160-
ancestry: [{ role: 'form', label: 'Checkout ${step}' }],
161-
});
135+
expect(resolved[6]?.flags.snapshotScope).toBe('Hi secret');
136+
expect(resolved[8]?.flags.snapshotScope).toBe('Pay ${amount}');
137+
expect(resolved[9]?.result?.refLabel).toBe('Pay ${amount}');
138+
expect(resolved[10]?.positionals).toEqual(['label="Pay ${amount}"']);
139+
expect(resolved[10]?.targetEvidence).toEqual(targetEvidence);
162140
});
163141

164142
// #3197: the recording half of `scroll --until`. A recorded hunt for an

‎packages/daemon/src/core/session-action-recorder.ts‎

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -76,33 +76,15 @@ export function recordActionEntry(
7676
// very entry that introduced it. The session-wide guarantee is for LATER,
7777
// distinct actions, exactly as #1398 frames it; this entry keeps only the
7878
// existing fill-step-scoped exact-match protection below.
79-
const parameterizedEntry = parameterizeRecordedFill(entry, fillLiteral);
79+
const { entry: recordedEntry, echoedNames } = applySessionEchoProtection(
80+
session,
81+
parameterizeRecordedFill(entry, fillLiteral),
82+
);
8083
const livePlaceholders = [
81-
...new Set([
82-
...(fillLiteral ? [fillLiteral.variableName] : []),
83-
...readEchoedVariableNames(session, parameterizedEntry),
84-
]),
84+
...new Set([...(fillLiteral ? [fillLiteral.variableName] : []), ...echoedNames]),
8585
];
86-
const recordedEntry = applySessionEchoProtection(session, parameterizedEntry);
8786
if (fillLiteral) registerRecordedFillLiteral(session, fillLiteral);
88-
const action: SessionAction = {
89-
ts: Date.now(),
90-
command: recordedEntry.command,
91-
positionals: recordedEntry.positionals,
92-
runtime: recordedEntry.runtime,
93-
flags: sanitizeFlags(recordedEntry.flags),
94-
result: recordedEntry.result,
95-
...(OBSERVATION_ONLY_COMMANDS.has(recordedEntry.command)
96-
? recordedEntry.interactiveObservation === false
97-
? { publicationIntent: 'authored' as const }
98-
: recordedEntry.flags.record === true
99-
? { publicationIntent: 'explicit-record' as const }
100-
: {}
101-
: {}),
102-
...(recordedEntry.targetEvidence ? { targetEvidence: recordedEntry.targetEvidence } : {}),
103-
...(recordedEntry.targetEvidences ? { targetEvidences: recordedEntry.targetEvidences } : {}),
104-
...(livePlaceholders.length > 0 ? { livePlaceholders } : {}),
105-
};
87+
const action = buildRecordedAction(recordedEntry, livePlaceholders);
10688
session.actions.push(action);
10789
emitDiagnostic({
10890
level: 'debug',
@@ -115,6 +97,30 @@ export function recordActionEntry(
11597
return action;
11698
}
11799

100+
function buildRecordedAction(entry: RecordActionEntry, livePlaceholders: string[]): SessionAction {
101+
const publicationIntent = readPublicationIntent(entry);
102+
return {
103+
ts: Date.now(),
104+
command: entry.command,
105+
positionals: entry.positionals,
106+
runtime: entry.runtime,
107+
flags: sanitizeFlags(entry.flags),
108+
result: entry.result,
109+
...(publicationIntent ? { publicationIntent } : {}),
110+
...(entry.targetEvidence ? { targetEvidence: entry.targetEvidence } : {}),
111+
...(entry.targetEvidences ? { targetEvidences: entry.targetEvidences } : {}),
112+
...(livePlaceholders.length > 0 ? { livePlaceholders } : {}),
113+
};
114+
}
115+
116+
function readPublicationIntent(
117+
entry: RecordActionEntry,
118+
): SessionAction['publicationIntent'] | undefined {
119+
if (!OBSERVATION_ONLY_COMMANDS.has(entry.command)) return undefined;
120+
if (entry.interactiveObservation === false) return 'authored';
121+
return entry.flags.record === true ? 'explicit-record' : undefined;
122+
}
123+
118124
type SessionActionStore = {
119125
resolveCurrent(ref: SessionRef): SessionState | undefined;
120126
recordAction(ref: SessionRef, entry: RecordActionEntry): void;
@@ -225,9 +231,9 @@ function registerRecordedFillLiteral(session: SessionState, fillLiteral: FillLit
225231
function applySessionEchoProtection(
226232
session: SessionState,
227233
entry: RecordActionEntry,
228-
): RecordActionEntry {
234+
): { entry: RecordActionEntry; echoedNames: string[] } {
229235
const literals = session.recordedFillLiterals;
230-
if (!literals || literals.size === 0) return entry;
236+
if (!literals || literals.size === 0) return { entry, echoedNames: [] };
231237

232238
const result = parameterizeRecordedResultEcho(entry.result, literals);
233239
const evidenceMode = entry.targetEvidenceMode ?? 'action';
@@ -242,26 +248,39 @@ function applySessionEchoProtection(
242248
}
243249
: entry.targetEvidences;
244250

245-
return { ...entry, result, targetEvidence, targetEvidences };
251+
return {
252+
entry: { ...entry, result, targetEvidence, targetEvidences },
253+
echoedNames: readSubstitutedNames(entry.result, result, literals),
254+
};
246255
}
247256

248257
/**
249-
* The variables whose literal `applySessionEchoProtection` replaces with a placeholder in this
250-
* entry's result, the recorded field the script writer emits. Annotations are never interpolated.
258+
* The variables whose placeholders echo protection wrote into the result: the placeholders that
259+
* occur more often after it than before. Annotations are never interpolated, so only the result
260+
* counts toward the live script placeholders.
251261
*/
252-
function readEchoedVariableNames(session: SessionState, entry: RecordActionEntry): string[] {
253-
const literals = session.recordedFillLiterals;
254-
if (!literals || literals.size === 0 || !entry.result) return [];
255-
const texts = collectStrings(entry.result);
262+
function readSubstitutedNames(
263+
before: RecordActionEntry['result'],
264+
after: RecordActionEntry['result'],
265+
literals: ReadonlyMap<string, string>,
266+
): string[] {
267+
const beforeTexts = collectStrings(before);
268+
const afterTexts = collectStrings(after);
256269
const names: string[] = [];
257-
for (const [literal, placeholder] of literals) {
258-
if (!texts.some((text) => text.includes(literal))) continue;
270+
for (const placeholder of new Set(literals.values())) {
271+
if (countOccurrences(afterTexts, placeholder) <= countOccurrences(beforeTexts, placeholder)) {
272+
continue;
273+
}
259274
const name = readRecordedInputVariableName(placeholder);
260275
if (name) names.push(name);
261276
}
262277
return names;
263278
}
264279

280+
function countOccurrences(texts: readonly string[], needle: string): number {
281+
return texts.reduce((count, text) => count + text.split(needle).length - 1, 0);
282+
}
283+
265284
function collectStrings(value: unknown): string[] {
266285
if (typeof value === 'string') return [value];
267286
if (Array.isArray(value)) return value.flatMap(collectStrings);

0 commit comments

Comments
 (0)