Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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 });
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
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')],
[
'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');

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,
);
});
4 changes: 3 additions & 1 deletion packages/capture-kit/src/snapshot/snapshot-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
formatRole,
formatSnapshotLine,
stateMarkers,
valueMarker,
} from './snapshot-lines.ts';

export type SnapshotDiffResult = {
Expand All @@ -31,14 +32,15 @@ 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) ?? '';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: snapshot diff still misses value-only changes after the preview cutoff. Include the full value in the comparable key while retaining the capped marker for display.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/capture-kit/src/snapshot/snapshot-diff.ts, line 35:

<comment>`snapshot diff` still misses value-only changes after the preview cutoff. Include the full value in the comparable key while retaining the capped marker for display.</comment>

<file context>
@@ -31,14 +32,15 @@ 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);
</file context>
Suggested change
const valuePart = valueMarker(node, role) ?? '';
const valuePart = valueMarker(node, role) ? JSON.stringify(node.value ?? '') : '';

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
// well: otherwise an action-only change diffs as 'unchanged' while its text
// 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(
Expand Down
44 changes: 38 additions & 6 deletions packages/capture-kit/src/snapshot/snapshot-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -178,8 +179,37 @@ export function stateMarkers(node: SnapshotNode): string[] {
return markers;
}

function isEditableRole(type: string): boolean {
return type === 'text-field' || type === 'text-view' || type === 'search';
/**
* 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 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(node, type) || node.checked !== undefined) 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))}"`;
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
}

/**
* 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 {
Expand Down Expand Up @@ -216,13 +246,15 @@ 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);
}
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');
Expand Down
Loading