From 7e499c3a62e1c5dd1a9f0a8d22f2e9e260fd139d Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 5 Dec 2026 18:47:52 -0400 Subject: [PATCH 1/2] fix(snapshot): print the value of labeled non-editable nodes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A labeled node prints its label and drops its value unless its role is editable, so a slider's percentage or a picker wheel's selection never reaches the text snapshot, and a value-only change diffs as +0 -0. Emit [value:"…"] when the value differs from the label, and key the diff on the same marker. Co-Authored-By: Claude Sonnet 5.5 --- .../__tests__/snapshot-lines-value.test.ts | 57 +++++++++++++++++++ .../capture-kit/src/snapshot/snapshot-diff.ts | 4 +- .../src/snapshot/snapshot-lines.ts | 18 ++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 packages/capture-kit/src/snapshot/__tests__/snapshot-lines-value.test.ts diff --git a/packages/capture-kit/src/snapshot/__tests__/snapshot-lines-value.test.ts b/packages/capture-kit/src/snapshot/__tests__/snapshot-lines-value.test.ts new file mode 100644 index 00000000000..aecfe033bb9 --- /dev/null +++ b/packages/capture-kit/src/snapshot/__tests__/snapshot-lines-value.test.ts @@ -0,0 +1,57 @@ +import assert from 'node:assert/strict'; +import { test } from 'vitest'; +import type { SnapshotNode } from '@agent-device/kernel/snapshot'; +import { formatRole } from '@agent-device/kernel/snapshot'; +import { buildSnapshotDiff } from '../snapshot-diff.ts'; +import { formatSnapshotLine } from '../snapshot-lines.ts'; + +function slider(value?: string, label = 'Brightness', type = 'Other'): SnapshotNode { + return { + ref: 'e3', + index: 0, + depth: 0, + type, + kind: formatRole(type), + label, + ...(value === undefined ? {} : { value }), + enabled: true, + hittable: true, + }; +} + +test.each([[false], [true]])( + 'a labeled non-editable node prints its differing value (summarize=%s)', + (summarizeTextSurfaces) => { + const line = formatSnapshotLine(slider('60%'), 0, false, undefined, { summarizeTextSurfaces }); + assert.equal(line, '@e3 [other] "Brightness" [value:"60%"]'); + }, +); + +test.each([ + ['no value', slider()], + ['value equal to the label', slider('Brightness')], + ['unlabeled node, value already shown as the label', slider('60%', '')], + ['editable node, value already shown as the label', slider('hello', 'Name', 'TextField')], +])('no value marker for %s', (_name, node) => { + const line = formatSnapshotLine(node, 0, false, undefined, { summarizeTextSurfaces: true }); + assert.doesNotMatch(line, /\[value:/); +}); + +test('a value-only change diffs as changed lines that read differently', () => { + const diff = buildSnapshotDiff([slider('60%')], [slider('96%')]); + const changed = diff.lines.filter((line) => line.kind !== 'unchanged'); + + assert.equal(diff.summary.unchanged, 0); + assert.equal(diff.summary.additions, 1); + assert.equal(diff.summary.removals, 1); + assert.match(changed[0]!.text, /\[value:"60%"\]/); + assert.match(changed.at(-1)!.text, /\[value:"96%"\]/); +}); + +test('a still value diffs as unchanged', () => { + const diff = buildSnapshotDiff([slider('60%')], [slider('60%')]); + assert.equal( + diff.lines.every((line) => line.kind === 'unchanged'), + true, + ); +}); diff --git a/packages/capture-kit/src/snapshot/snapshot-diff.ts b/packages/capture-kit/src/snapshot/snapshot-diff.ts index d494d76089e..a83b99f632c 100644 --- a/packages/capture-kit/src/snapshot/snapshot-diff.ts +++ b/packages/capture-kit/src/snapshot/snapshot-diff.ts @@ -6,6 +6,7 @@ import { formatRole, formatSnapshotLine, stateMarkers, + valueMarker, } from './snapshot-lines.ts'; export type SnapshotDiffResult = { @@ -31,6 +32,7 @@ function snapshotNodeToComparableLine(node: SnapshotNode, depthOverride?: number // The states the rendered line prints are the states the key compares, from one function, so a // fact the diff weighs is always visible in the line it prints. const statePart = stateMarkers(node).join(','); + const valuePart = valueMarker(node, role) ?? ''; const hittablePart = node.hittable === true ? 'hittable' : 'not-hittable'; const depthPart = String(depthOverride ?? node.depth ?? 0); // The rendered line carries the actions list, so the comparable key has to as @@ -38,7 +40,7 @@ function snapshotNodeToComparableLine(node: SnapshotNode, depthOverride?: number // silently differs from the baseline's. JSON-encoded because the names are // app-authored and may contain the field separator. const actionsPart = node.actions ? JSON.stringify(node.actions) : ''; - return [depthPart, role, textPart, statePart, hittablePart, actionsPart].join('|'); + return [depthPart, role, textPart, statePart, valuePart, hittablePart, actionsPart].join('|'); } export function buildSnapshotDiff( diff --git a/packages/capture-kit/src/snapshot/snapshot-lines.ts b/packages/capture-kit/src/snapshot/snapshot-lines.ts index b607222117b..ecd79d660c9 100644 --- a/packages/capture-kit/src/snapshot/snapshot-lines.ts +++ b/packages/capture-kit/src/snapshot/snapshot-lines.ts @@ -178,6 +178,22 @@ export function stateMarkers(node: SnapshotNode): string[] { return markers; } +/** + * A labeled node prints its label, so a non-editable node's `value` (a slider's percentage, a picker + * wheel's selection) would otherwise never reach the text line. It rides along as metadata when it + * says something the label does not. Editable roles already print the value as their label, and an + * unlabeled node falls back to its value, so neither gets a second copy. Shared with the diff key so + * a value-only change reads as a changed pair, not as `+0 -0`. + */ +export function valueMarker(node: SnapshotNode, type: string): string | undefined { + if (isEditableRole(type)) return undefined; + const label = node.label?.trim(); + if (!label || shouldSuppressScrollContainerLabel(type, label)) return undefined; + const value = collapseControlCharacters(node.value ?? ''); + if (!value || value === collapseControlCharacters(label)) return undefined; + return `value:"${escapePreviewText(buildTextPreview(value))}"`; +} + function isEditableRole(type: string): boolean { return type === 'text-field' || type === 'text-view' || type === 'search'; } @@ -216,6 +232,8 @@ function buildLineMetadata( textSurface: { text: string; isLargeSurface: boolean; shouldSummarize: boolean }, ): string[] { const metadata = [...stateMarkers(node), ...(node.presentationHints ?? [])]; + const value = valueMarker(node, type); + if (value) metadata.push(value); if (!options.summarizeTextSurfaces) { return uniqueMetadata(metadata); } From d2cba62c48913beab1593551dce6ad4ad9e1e95d Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 5 Dec 2026 19:34:09 -0400 Subject: [PATCH 2/2] fix(snapshot): never print a secure field's value, skip checkable values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit valueMarker skipped only the text-field, text-view and search display roles, so an iOS SecureTextField (role securetextfield) or any node the producer flags password:true printed [value:"…"], leaking the secret's masked length and a diff line per keystroke. Make isEditableRole the one predicate for displayLabel and valueMarker, and have it cover securetextfield and the editable/password facts. displayLabel prints a secure field's label instead of its value. A checkable control already prints its state, so it gets no value marker either. Co-Authored-By: Claude Sonnet 5.5 --- .../__tests__/snapshot-lines-value.test.ts | 28 +++++++++++++++ .../src/snapshot/snapshot-lines.ts | 36 +++++++++++++------ 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/packages/capture-kit/src/snapshot/__tests__/snapshot-lines-value.test.ts b/packages/capture-kit/src/snapshot/__tests__/snapshot-lines-value.test.ts index aecfe033bb9..90aede79a93 100644 --- a/packages/capture-kit/src/snapshot/__tests__/snapshot-lines-value.test.ts +++ b/packages/capture-kit/src/snapshot/__tests__/snapshot-lines-value.test.ts @@ -32,11 +32,39 @@ test.each([ ['value equal to the label', slider('Brightness')], ['unlabeled node, value already shown as the label', slider('60%', '')], ['editable node, value already shown as the label', slider('hello', 'Name', 'TextField')], + [ + 'checkable node, value already shown as its state', + { ...slider('1', 'Wi-Fi', 'Switch'), checked: true }, + ], ])('no value marker for %s', (_name, node) => { const line = formatSnapshotLine(node, 0, false, undefined, { summarizeTextSurfaces: true }); assert.doesNotMatch(line, /\[value:/); }); +test('a long value with quotes and backslashes is escaped and truncated', () => { + const value = `say "hi" C:\\dir ${'x'.repeat(60)}`; + const line = formatSnapshotLine(slider(value), 0, false, undefined, {}); + assert.equal( + line, + `@e3 [other] "Brightness" [value:"say \\"hi\\" C:\\\\dir ${'x'.repeat(29)}..."]`, + ); +}); + +test.each([ + [ + 'a labeled SecureTextField', + { ...slider('••••••••', 'Password', 'SecureTextField'), password: true }, + ], + ['a password node under a non-field role', { ...slider('hunter2', 'PIN'), password: true }], +])('%s never prints its value', (_name, node) => { + for (const summarizeTextSurfaces of [false, true]) { + const line = formatSnapshotLine(node, 0, false, undefined, { summarizeTextSurfaces }); + assert.doesNotMatch(line, /\[value:|••|hunter2/); + } + const diff = buildSnapshotDiff([node], [{ ...node, value: `${node.value}x` }]); + assert.equal(diff.summary.unchanged, 1); +}); + test('a value-only change diffs as changed lines that read differently', () => { const diff = buildSnapshotDiff([slider('60%')], [slider('96%')]); const changed = diff.lines.filter((line) => line.kind !== 'unchanged'); diff --git a/packages/capture-kit/src/snapshot/snapshot-lines.ts b/packages/capture-kit/src/snapshot/snapshot-lines.ts index ecd79d660c9..66c8567c657 100644 --- a/packages/capture-kit/src/snapshot/snapshot-lines.ts +++ b/packages/capture-kit/src/snapshot/snapshot-lines.ts @@ -145,8 +145,9 @@ export function displayLabel(node: SnapshotNode, type: string): string { return ''; } const value = node.value?.trim(); - if (isEditableRole(type)) { - if (value) return value; + if (isEditableRole(node, type)) { + // A secure field's value is the secret, or its masked length: the label stands in for it. + if (value && node.password !== true) return value; if (label) return label; } else if (label) { return label; @@ -180,13 +181,14 @@ export function stateMarkers(node: SnapshotNode): string[] { /** * A labeled node prints its label, so a non-editable node's `value` (a slider's percentage, a picker - * wheel's selection) would otherwise never reach the text line. It rides along as metadata when it - * says something the label does not. Editable roles already print the value as their label, and an - * unlabeled node falls back to its value, so neither gets a second copy. Shared with the diff key so - * a value-only change reads as a changed pair, not as `+0 -0`. + * wheel's selection) would otherwise never reach the text line. It rides along as metadata only when + * nothing else in the line carries it: editable nodes print the value as their label, an unlabeled + * node falls back to its value, a checkable control prints its state, and a secure field's value is + * never printed. Shared with the diff key so a value-only change reads as a changed pair, not as + * `+0 -0`. */ export function valueMarker(node: SnapshotNode, type: string): string | undefined { - if (isEditableRole(type)) return undefined; + if (isEditableRole(node, type) || node.checked !== undefined) return undefined; const label = node.label?.trim(); if (!label || shouldSuppressScrollContainerLabel(type, label)) return undefined; const value = collapseControlCharacters(node.value ?? ''); @@ -194,8 +196,20 @@ export function valueMarker(node: SnapshotNode, type: string): string | undefine return `value:"${escapePreviewText(buildTextPreview(value))}"`; } -function isEditableRole(type: string): boolean { - return type === 'text-field' || type === 'text-view' || type === 'search'; +/** + * The one predicate for "this node's value is its text": `displayLabel` prints the value first for + * these nodes and `valueMarker` skips them, so the two cannot drift. Secure fields count, by role + * or by the producer's `password` / `editable` facts, whatever display role their type maps to. + */ +function isEditableRole(node: SnapshotNode, type: string): boolean { + return ( + type === 'text-field' || + type === 'text-view' || + type === 'search' || + type === 'securetextfield' || + node.editable === true || + node.password === true + ); } function shouldSuppressScrollContainerLabel(type: string, label: string): boolean { @@ -238,9 +252,9 @@ function buildLineMetadata( return uniqueMetadata(metadata); } if (node.focused === true) metadata.push('focused'); - if (isEditableRole(type)) metadata.push('editable'); + if (isEditableRole(node, type)) metadata.push('editable'); const placeholder = collapseControlCharacters(node.placeholder ?? ''); - if (isEditableRole(type) && !trimText(node.label) && !trimText(node.value) && placeholder) { + if (isEditableRole(node, type) && !trimText(node.label) && !trimText(node.value) && placeholder) { metadata.push(`placeholder:"${escapePreviewText(placeholder)}"`); } if (looksScrollable(node, type)) metadata.push('scrollable');