{"record":{"id":"502a518f6c614769","repo":"Hmbown/CodeWhale","slug":"set-value-failed-j-code","errorCode":null,"errorMessage":"set_value failed: ${j.code}","messagePattern":"set_value failed: (.+?)","errorType":"exception","errorClass":"ExecError","httpStatus":null,"severity":"error","filePath":"crates/tui/plugins/computer-use/src/backends/win32.mjs","lineNumber":487,"sourceCode":"$val = [Text.Encoding]::Unicode.GetString([Convert]::FromBase64String('${b64val}'));\n$indices = $textPath | ConvertFrom-Json;\n$root = [System.Windows.Automation.AutomationElement]::RootElement;\n$targets = $root.FindAll([System.Windows.Automation.TreeScope]::Children, [System.Windows.Automation.Condition]::TrueCondition);\n$cur = $null;\nforeach ($t in $targets) { $cur = $t; break }\nif ($cur -eq $null) { Write-Output '{\"ok\": false, \"code\": \"app_not_found\"}'; exit 0 }\nforeach ($i in $indices) {\n  if ($i -eq 0) { continue }\n  $kids = $cur.FindAll([System.Windows.Automation.TreeScope]::Children, [System.Windows.Automation.Condition]::TrueCondition);\n  if ($i -ge $kids.Count) { Write-Output '{\"ok\": false, \"code\": \"element_stale\"}'; exit 0 }\n  $cur = $kids[$i];\n}\ntry {\n  $vp = $cur.GetCurrentPattern([System.Windows.Automation.ValuePattern]::Pattern);\n  $vp.SetValue($val);\n  Write-Output '{\"ok\": true}';\n} catch { Write-Output ('{\"ok\": false, \"code\": \"' + $_.Exception.Message.Replace('\"','') + '\"}') }`, { timeoutMs: 45_000 });\n      if (!j.ok) throw new ExecError(`set_value failed: ${j.code}`);\n      return { action_sent: true, strategy: \"a11y\" };\n    },\n    select_text: async () => { throw new ExecError(\"select_text is not implemented on the win32 backend yet — fail-closed\"); },\n    perform_action: async ({ target, action }) => {\n      assertUntargetedElement(target);\n      const b64path = Buffer.from(JSON.stringify(target.path ?? []), \"utf8\").toString(\"base64\");\n      const act = String(action ?? \"Invoke\").replace(/'/g, \"\");\n      const j = await psJson(`Add-Type -AssemblyName UIAutomationClient; Add-Type -AssemblyName UIAutomationTypes;\n$path = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('${b64path}'));\n$indices = $path | ConvertFrom-Json;\n$root = [System.Windows.Automation.AutomationElement]::RootElement;\n$targets = $root.FindAll([System.Windows.Automation.TreeScope]::Children, [System.Windows.Automation.Condition]::TrueCondition);\n$cur = $null;\nforeach ($t in $targets) { $cur = $t; break }\nforeach ($i in $indices) {\n  if ($i -eq 0) { continue }\n  $kids = $cur.FindAll([System.Windows.Automation.TreeScope]::Children, [System.Windows.Automation.Condition]::TrueCondition);\n  if ($i -ge $kids.Count) { Write-Output '{\"ok\": false, \"code\": \"element_stale\"}'; exit 0 }","sourceCodeStart":469,"sourceCodeEnd":505,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/crates/tui/plugins/computer-use/src/backends/win32.mjs#L469-L505","documentation":"set_value on the win32 backend drives a UI element through UI Automation's ValuePattern via a PowerShell script. The script itself catches any .NET exception inside PowerShell and emits {ok:false, code:\"<exception message>\"} instead of crashing; the Node layer then throws ExecError('set_value failed: <code>'). The <code> is therefore the raw UIAutomation exception message (e.g. element no longer exists, pattern unsupported, value rejected by the control).","triggerScenarios":"Calling set_value({ target, value }) on the win32 backend when: the resolved UIA element does not support ValuePattern ($vp is null so SetValue is called on null), the element has disappeared or its UIA runtime id path is stale, the control rejects the value (read-only field, validation), or the element is in a non-value-able state.","commonSituations":"Targeting a stale element snapshot taken before the UI re-rendered (dialog closed, list re-populated); attempting to type into a custom-drawn control (canvas, browser canvas widgets) that exposes no ValuePattern; setting a value on a read-only or disabled edit box; 45-second timeout expiring because the target app's UI thread is blocked (UIA calls are synchronous with the target process).","solutions":["Read the code embedded in the message: 'Unsupported Pattern' / null-pattern means the control has no ValuePattern — use keyboard input (type into the focused element) instead of a11y set_value.","Re-acquire the accessibility tree snapshot immediately before set_value so the element path is fresh; retry once with the new path if the message indicates the element is gone.","Confirm the target control is enabled and not read-only (inspect via the a11y snapshot's element properties).","If the target app's UI thread is hung (timeout-flavored message), unblock or restart the app, then retry.","Fall back to an event strategy: focus the element (click) and send the text as keystrokes, which works for controls without ValuePattern."],"exampleFix":"// before\nawait backend.set_value({ target: staleTarget, value: \"hello\" });\n// after\nconst fresh = await backend.accessibility_tree(); // re-snapshot\nconst target = findMatchingElement(fresh, staleTarget);\nif (!target) throw new Error('element vanished; re-locate before set_value');\nawait backend.set_value({ target, value: \"hello\" });","handlingStrategy":"try-catch","validationCode":"function canSetValue(elementSnapshot) {\n  return elementSnapshot &&\n    Array.isArray(elementSnapshot.path) && elementSnapshot.path.length > 0 &&\n    !elementSnapshot.readOnly && elementSnapshot.enabled !== false &&\n    (elementSnapshot.patterns ?? []).includes(\"ValuePattern\");\n}","typeGuard":"function isElementTarget(t) {\n  return t != null && typeof t === \"object\" && Array.isArray(t.path) && t.path.length > 0;\n}","tryCatchPattern":"try {\n  await backend.set_value({ target, value });\n} catch (e) {\n  if (e instanceof ExecError && e.message.startsWith(\"set_value failed:\")) {\n    // refresh the tree and retry once; fall back to keystrokes\n    const fresh = await refreshTarget(target);\n    if (fresh) return backend.set_value({ target: fresh, value });\n    return typeViaKeyboard(value); // controls without ValuePattern\n  }\n  throw e;\n}","preventionTips":["Re-snapshot the accessibility tree immediately before set_value; never cache element paths across UI changes.","Confirm the control exposes ValuePattern and is enabled/editable in the snapshot first.","Prefer keyboard input for custom/canvas controls known to lack ValuePattern.","Watch for the 45s timeout as a sign the target app's UI thread is blocked."],"tags":["windows","uiautomation","accessibility","set-value"],"backgroundTag":"api-error-response","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-22T16:17:23.217Z"}