Skip to content

Commit ee8fc93

Browse files
bkudiessCopilotCopilotshanselman
authored
Make A2UI dataModelUpdate.valueArray first-class (#805)
* Make A2UI dataModelUpdate.valueArray first-class The v0.8 `dataModelUpdate.valueArray` typed value was silently dropped by the WinUI parser: `DataModelEntry` had no `ValueArray` field and `ToJsonNode()` returned null for it. Only `valueString/Number/Boolean/valueMap` were handled. This broke seeding an array into a surface's data model (e.g. a multi-select `MultipleChoice` bound to a path), even though `valueArray` is part of the v0.8 protocol (docs/a2ui/protocol.md §2.2 and data-and-actions.md). Changes: - Parser (A2UIProtocol.cs): add `DataModelEntry.ValueArray`; `ToJsonNode()` emits a `JsonArray`; new `ParseValueArray`/`ParseArrayElement` handle value-typed object elements, bare primitives (`["a",1,true]`), nested maps/arrays, and preserve JSON null as a stable index slot. - DoS guard (DataModelStore.cs): the 32-deep depth bound now recurses `valueArray` as well as `valueMap`. - Security (SecretRedactor.cs): the array branch now redacts registered/ denylisted element paths instead of only recursing, so a secret seeded into an array (e.g. an obscured field bound to /codes/0) no longer leaks via canvas.a2ui.dump. - Docs (SKILL.md): correct the stale "arrays are not first-class" note. Tests: parser coverage for valueArray (strings, mixed scalars, maps, nested arrays, bare primitives, empty, null slots) plus valueMap/scalar regressions; secret-redaction regressions for secrets inside arrays; store-level base-path landing and depth-guard rejection; and an end-to-end MultipleChoice surface seeded via valueArray that asserts both the snapshot and the rendered preselection. Validation: build.ps1 (all projects), Shared.Tests, Tray.Tests, and A2UI UITests all pass. Co-authored-by: Copilot <[email protected]> * Fix valueArray redaction and notifications Reject non-canonical array pointer indices for data-model array access, notify descendant subscribers when container values are replaced, and redact registered secret descendants when action contexts or snapshots include parent paths. Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Scott Hanselman <[email protected]>
1 parent 1a07759 commit ee8fc93

10 files changed

Lines changed: 802 additions & 27 deletions

File tree

‎src/OpenClaw.Tray.WinUI/A2UI/DataModel/DataModelStore.cs‎

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public void ApplyDataModelUpdate(string surfaceId, string? basePath, IReadOnlyLi
105105
if (entry.Key.Length > MaxKeyLength) continue;
106106
if (entry.ValueString != null && entry.ValueString.Length > MaxStringValueLength) continue;
107107
if (!IsWithinDepth(entry.ValueMap, depth: 1, max: MaxValueMapDepth)) continue;
108+
if (!IsWithinDepth(entry.ValueArray, depth: 1, max: MaxValueMapDepth)) continue;
108109

109110
try
110111
{
@@ -130,12 +131,17 @@ public void ApplyDataModelUpdate(string surfaceId, string? basePath, IReadOnlyLi
130131
new DataModelObservable(model, _dispatcher).NotifyPaths(changed);
131132
}
132133

133-
private static bool IsWithinDepth(IReadOnlyList<Protocol.DataModelEntry>? map, int depth, int max)
134+
private static bool IsWithinDepth(IReadOnlyList<Protocol.DataModelEntry>? children, int depth, int max)
134135
{
135-
if (map == null) return true;
136+
if (children == null) return true;
136137
if (depth > max) return false;
137-
foreach (var e in map)
138+
foreach (var e in children)
139+
{
140+
// Both nested maps and nested arrays add a level — bound either path
141+
// so a deeply-nested valueArray can't bypass the valueMap depth cap.
138142
if (!IsWithinDepth(e.ValueMap, depth + 1, max)) return false;
143+
if (!IsWithinDepth(e.ValueArray, depth + 1, max)) return false;
144+
}
139145
return true;
140146
}
141147

@@ -241,14 +247,14 @@ public JsonObject CloneRoot()
241247
if (obj[tok] == null)
242248
{
243249
if (!createMissing) return (null, null, false, -1);
244-
var nextIsIndex = int.TryParse(tokens[i + 1], out _);
250+
var nextIsIndex = TryParseArrayIndex(tokens[i + 1], out _);
245251
obj[tok] = nextIsIndex ? new JsonArray() : new JsonObject();
246252
}
247253
cursor = obj[tok];
248254
}
249255
else if (cursor is JsonArray arr)
250256
{
251-
if (!int.TryParse(tok, out var ai)) return (null, null, false, -1);
257+
if (!TryParseArrayIndex(tok, out var ai)) return (null, null, false, -1);
252258
while (createMissing && arr.Count <= ai) arr.Add(null);
253259
if (ai < 0 || ai >= arr.Count) return (null, null, false, -1);
254260
cursor = arr[ai];
@@ -260,11 +266,28 @@ public JsonObject CloneRoot()
260266
}
261267

262268
var last = tokens[^1];
263-
if (cursor is JsonArray finalArr && int.TryParse(last, out var idx))
269+
if (cursor is JsonArray finalArr && TryParseArrayIndex(last, out var idx))
264270
return (finalArr, last, true, idx);
265271
return (cursor, last, false, -1);
266272
}
267273

274+
private static bool TryParseArrayIndex(string token, out int index)
275+
{
276+
index = -1;
277+
if (token.Length == 0) return false;
278+
if (token.Length > 1 && token[0] == '0') return false;
279+
280+
foreach (var c in token)
281+
if (c < '0' || c > '9')
282+
return false;
283+
284+
return int.TryParse(
285+
token,
286+
System.Globalization.NumberStyles.None,
287+
System.Globalization.CultureInfo.InvariantCulture,
288+
out index);
289+
}
290+
268291
private static List<string> SplitPointer(string pointer)
269292
{
270293
var p = pointer.StartsWith('/') ? pointer.Substring(1) : pointer;
@@ -356,24 +379,57 @@ internal void NotifyPaths(IEnumerable<string> paths)
356379
var current = key;
357380
while (true)
358381
{
359-
List<Action>? subs;
360-
lock (_model.Subscribers)
361-
{
362-
_model.Subscribers.TryGetValue(current, out subs);
363-
subs = subs == null ? null : new List<Action>(subs);
364-
}
365-
if (subs != null)
366-
{
367-
foreach (var s in subs)
368-
if (fired.Add(s)) Dispatch(s);
369-
}
382+
DispatchSubscribers(current, fired);
370383
if (current == "/" || string.IsNullOrEmpty(current)) break;
371384
var slash = current.LastIndexOf('/');
372385
current = slash <= 0 ? "/" : current.Substring(0, slash);
373386
}
387+
388+
// Replacing a container at /x also changes /x/... descendants. Most
389+
// component bindings subscribe to the exact leaf path they read, so
390+
// notify those subtree subscribers as well.
391+
DispatchDescendantSubscribers(key, fired);
374392
}
375393
}
376394

395+
private void DispatchSubscribers(string key, HashSet<Action> fired)
396+
{
397+
List<Action>? subs;
398+
lock (_model.Subscribers)
399+
{
400+
_model.Subscribers.TryGetValue(key, out subs);
401+
subs = subs == null ? null : new List<Action>(subs);
402+
}
403+
404+
if (subs == null) return;
405+
406+
foreach (var s in subs)
407+
if (fired.Add(s))
408+
Dispatch(s);
409+
}
410+
411+
private void DispatchDescendantSubscribers(string key, HashSet<Action> fired)
412+
{
413+
List<Action> subs = [];
414+
var prefix = key == "/" ? "/" : key + "/";
415+
416+
lock (_model.Subscribers)
417+
{
418+
foreach (var (subscriberPath, callbacks) in _model.Subscribers)
419+
{
420+
if (subscriberPath == key)
421+
continue;
422+
423+
if (key == "/" || subscriberPath.StartsWith(prefix, StringComparison.Ordinal))
424+
subs.AddRange(callbacks);
425+
}
426+
}
427+
428+
foreach (var s in subs)
429+
if (fired.Add(s))
430+
Dispatch(s);
431+
}
432+
377433
internal void NotifyAllPaths()
378434
{
379435
List<Action> all;

‎src/OpenClaw.Tray.WinUI/A2UI/Protocol/A2UIProtocol.cs‎

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ public sealed record A2UIComponentDef
7373
}
7474

7575
/// <summary>
76-
/// Single dataModelUpdate.contents entry. Exactly one of Value*/ValueMap is
77-
/// expected on the wire; we expose them all and let the store apply whichever
78-
/// is set.
76+
/// Single dataModelUpdate.contents entry. Exactly one of
77+
/// Value*/ValueMap/ValueArray is expected on the wire; we expose them all and
78+
/// let the store apply whichever is set.
7979
/// </summary>
8080
public sealed record DataModelEntry
8181
{
@@ -85,6 +85,12 @@ public sealed record DataModelEntry
8585
public bool? ValueBoolean { get; init; }
8686
/// <summary>An adjacency-list map: each item is itself a DataModelEntry.</summary>
8787
public IReadOnlyList<DataModelEntry>? ValueMap { get; init; }
88+
/// <summary>
89+
/// An ordered array (v0.8 <c>valueArray</c>): each item is a value-typed
90+
/// <see cref="DataModelEntry"/> whose <see cref="Key"/> is ignored. Items
91+
/// may themselves be scalars, maps, or nested arrays.
92+
/// </summary>
93+
public IReadOnlyList<DataModelEntry>? ValueArray { get; init; }
8894

8995
/// <summary>Convert this entry's value to a JsonNode for storage.</summary>
9096
public JsonNode? ToJsonNode()
@@ -99,6 +105,13 @@ public sealed record DataModelEntry
99105
obj[entry.Key] = entry.ToJsonNode();
100106
return obj;
101107
}
108+
if (ValueArray != null)
109+
{
110+
var arr = new JsonArray();
111+
foreach (var entry in ValueArray)
112+
arr.Add(entry.ToJsonNode());
113+
return arr;
114+
}
102115
return null;
103116
}
104117
}
@@ -350,6 +363,7 @@ private static string Truncate(string s, int max) =>
350363
ValueNumber = e["valueNumber"] is JsonValue jvn && jvn.TryGetValue<double>(out var n) ? n : null,
351364
ValueBoolean = e["valueBoolean"] is JsonValue jvb && jvb.TryGetValue<bool>(out var b) ? b : null,
352365
ValueMap = ParseValueMap(e["valueMap"] as JsonArray),
366+
ValueArray = ParseValueArray(e["valueArray"] as JsonArray),
353367
};
354368
return entry;
355369
}
@@ -367,6 +381,61 @@ private static string Truncate(string s, int max) =>
367381
return list;
368382
}
369383

384+
/// <summary>
385+
/// Parse a v0.8 <c>valueArray</c>. Each element is a value-typed object
386+
/// with no key (e.g. <c>{ "valueString": "admin" }</c>). For robustness we
387+
/// also tolerate bare primitives (<c>["a", 1, true]</c>) that an agent may
388+
/// emit. A JSON <c>null</c> element is preserved as an explicit null slot so
389+
/// array indices stay stable for position-sensitive consumers — matching how
390+
/// a value-less wrapped object (<c>{}</c>) round-trips. Elements of an
391+
/// unsupported kind are dropped, matching <see cref="ParseValueMap"/>'s
392+
/// skip-bad-item tolerance.
393+
/// </summary>
394+
private static IReadOnlyList<DataModelEntry>? ParseValueArray(JsonArray? arr)
395+
{
396+
if (arr == null) return null;
397+
var list = new List<DataModelEntry>(arr.Count);
398+
foreach (var item in arr)
399+
{
400+
// A JSON null element surfaces as a C# null inside the JsonArray.
401+
// Preserve it as a value-less entry (ToJsonNode → null) rather than
402+
// dropping it, so [a, null, b] stays length 3.
403+
if (item is null) { list.Add(NullArrayElement); continue; }
404+
var element = ParseArrayElement(item);
405+
if (element != null) list.Add(element);
406+
}
407+
return list;
408+
}
409+
410+
/// <summary>Shared value-less entry representing a JSON null array slot.</summary>
411+
private static readonly DataModelEntry NullArrayElement = new() { Key = string.Empty };
412+
413+
private static DataModelEntry? ParseArrayElement(JsonNode? item)
414+
{
415+
if (item is JsonObject e)
416+
{
417+
return new DataModelEntry
418+
{
419+
Key = string.Empty,
420+
ValueString = OptionalString(e, "valueString"),
421+
ValueNumber = e["valueNumber"] is JsonValue jvn && jvn.TryGetValue<double>(out var n) ? n : null,
422+
ValueBoolean = e["valueBoolean"] is JsonValue jvb && jvb.TryGetValue<bool>(out var b) ? b : null,
423+
ValueMap = ParseValueMap(e["valueMap"] as JsonArray),
424+
ValueArray = ParseValueArray(e["valueArray"] as JsonArray),
425+
};
426+
}
427+
// Bare primitive tolerance. Check string first (a JSON number/bool does
428+
// not satisfy TryGetValue<string>), then bool (a JSON number does not
429+
// satisfy TryGetValue<bool>), then number.
430+
if (item is JsonValue v)
431+
{
432+
if (v.TryGetValue<string>(out var s)) return new DataModelEntry { Key = string.Empty, ValueString = s };
433+
if (v.TryGetValue<bool>(out var bl)) return new DataModelEntry { Key = string.Empty, ValueBoolean = bl };
434+
if (v.TryGetValue<double>(out var d)) return new DataModelEntry { Key = string.Empty, ValueNumber = d };
435+
}
436+
return null;
437+
}
438+
370439
private static string RequireString(JsonObject o, string key)
371440
{
372441
if (o[key] is JsonValue jv && jv.TryGetValue<string>(out var s) && !string.IsNullOrEmpty(s))

‎src/OpenClaw.Tray.WinUI/A2UI/Rendering/IComponentRenderer.cs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,12 @@ public void MarkSecretPath(string? path)
164164
{
165165
if (SecretPaths == null) return;
166166
if (string.IsNullOrEmpty(path)) return;
167-
SecretPaths.Add(NormalizePath(path));
167+
var normalized = NormalizePath(path);
168+
SecretPaths.Add(normalized);
169+
170+
var canonical = SecretRedactor.CanonicalizeLenientArrayIndices(normalized);
171+
if (!string.Equals(canonical, normalized, StringComparison.Ordinal))
172+
SecretPaths.Add(canonical);
168173
}
169174

170175
/// <summary>True if <paramref name="path"/> is a secret path (registered or matches denylist).</summary>
@@ -210,7 +215,11 @@ public bool IsSecretPath(string? path)
210215
var path = val.Path!;
211216
if (!IsAllowedPath(path, allowed)) continue;
212217
if (IsSecretPath(path)) continue;
213-
result[key] = DataModel.Read(path)?.DeepClone();
218+
var clone = DataModel.Read(path)?.DeepClone();
219+
var registered = SecretPaths == null
220+
? System.Collections.Frozen.FrozenSet<string>.Empty
221+
: (IReadOnlySet<string>)SecretPaths;
222+
result[key] = SecretRedactor.RedactInPlace(clone, path, registered);
214223
}
215224
}
216225
return result;

0 commit comments

Comments
 (0)