Skip to content

Commit 6be4a4f

Browse files
fix(selectors): omit target annotation when payload still exceeds ceiling after rect drop
Remeasure after dropping rect; docs and changeset updated.
1 parent d4eb753 commit 6be4a4f

5 files changed

Lines changed: 40 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'agent-device': patch
3+
---
4+
5+
Fix the target annotation writer emitting a payload over the 4 KiB ceiling (rejected by its own parser) for escape-heavy labels. After dropping `rect`, the writer now measures again and omits the annotation if it is still over the limit; that action replays without a target check.

‎docs/adr/0012-interactive-replay.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ An inserted or removed wrapper ancestor changes identity.
6666
Replay classifies every annotated step before dispatch through six exact verification paths.
6767
Only a verified result sends the action.
6868
Anything that the evidence cannot isolate is an `identity-unverifiable` divergence, never a silent pick.
69-
The recorder must never emit a payload that its parser rejects. The writer downgrades oversized evidence to `verification: "unverifiable"` and drops its rect. Escape-heavy values can still exceed the serialized limit; the implementation does not recheck after dropping the rect.
69+
The recorder must never emit a payload that its parser rejects. The writer downgrades oversized evidence to `verification: "unverifiable"` and drops its rect. If the payload still exceeds the serialized limit after dropping the rect (escape-heavy values), the writer measures again and writes no annotation; that action replays without a target check.
7070
The format is additive. Old readers ignore the comment, and new readers run unannotated actions without the check.
7171
An annotation binds only to the next physical action line.
7272

‎packages/ad-script/docs/target-annotation.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ If per-field truncation still leaves more than 4 KiB, the writer removes `ancest
6666
This reduction is deterministic and uses the existing ancestry truncation direction.
6767
If only `ancestry[0]` remains and the payload still exceeds the limit, the writer sets `verification: "unverifiable"`.
6868
The limit measures serialized UTF-8 bytes, while field limits apply before JSON escaping.
69-
The writer drops `rect` if the downgraded payload still exceeds the limit, but does not recheck afterward.
70-
Escape-heavy values can therefore still produce an over-limit annotation that the parser rejects.
69+
The writer drops `rect` if the downgraded payload still exceeds the limit, then measures again.
70+
If the payload is still over the limit (escape-heavy values), the writer writes no annotation, and that action replays without a target check (see Parsing and binding).
7171
When the reduced tuple fits, the record-time self-check uses that tuple.
7272
A `verified` claim therefore applies to exactly the written evidence.
7373

‎packages/selectors/src/target-evidence.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,27 @@ test('computeTargetEvidence: max-size labels across K=8 ancestors reduce ancestr
221221
assert.deepEqual(reparsed, evidence);
222222
});
223223

224+
test('computeTargetEvidence: escape-heavy labels that exceed 4 KiB even after the rect drop yield no annotation instead of an unparseable one', () => {
225+
// Each field is capped at 256 raw bytes, but a control character serializes
226+
// as a 6-byte `\\u0001` escape, so the parent-only payload is over the ceiling
227+
// once JSON-escaped.
228+
const heavy = '\u0001'.repeat(TARGET_ANNOTATION_MAX_FIELD_BYTES);
229+
const nodes = toSnapshotNodes([
230+
{ index: 0, type: heavy, identifier: heavy, label: heavy, depth: 0 },
231+
{
232+
index: 1,
233+
type: heavy,
234+
identifier: `${heavy}-leaf`,
235+
label: `${heavy}!`,
236+
rect: { x: 0, y: 0, width: 10, height: 10 },
237+
depth: 1,
238+
parentIndex: 0,
239+
},
240+
]);
241+
const evidence = computeTargetEvidence({ node: nodes[1]!, preActionNodes: nodes });
242+
assert.equal(evidence, undefined);
243+
});
244+
224245
test('computeTargetEvidence: a root node with no ancestors has empty ancestry and is still verified', () => {
225246
const nodes = toSnapshotNodes([
226247
{

‎packages/selectors/src/target-evidence.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,23 @@ function fitsPayloadCeiling(candidate: TargetAnnotationV1): boolean {
151151
);
152152
}
153153

154-
/** Decision 3's terminal fail-closed downgrade; rect is diagnostic-only and dropped before emitting an over-cap payload. */
155-
function finalizeOversizedCandidate(candidate: TargetAnnotationV1): TargetAnnotationV1 {
154+
/**
155+
* Decision 3's terminal fail-closed downgrade; rect is diagnostic-only and dropped before emitting an over-cap payload.
156+
* The field caps apply before JSON escaping, so escape-heavy labels can leave even the parent-only payload over the
157+
* ceiling: measure again after the drop and emit no annotation rather than one the parser would reject.
158+
*/
159+
function finalizeOversizedCandidate(candidate: TargetAnnotationV1): TargetAnnotationV1 | undefined {
156160
candidate.verification = 'unverifiable';
157161
if (
158162
utf8ByteLength(serializeTargetAnnotationV1(candidate)) > TARGET_ANNOTATION_MAX_PAYLOAD_BYTES
159163
) {
160164
delete candidate.rect;
161165
}
166+
if (
167+
utf8ByteLength(serializeTargetAnnotationV1(candidate)) > TARGET_ANNOTATION_MAX_PAYLOAD_BYTES
168+
) {
169+
return undefined;
170+
}
162171
return candidate;
163172
}
164173

0 commit comments

Comments
 (0)