{"record":{"id":"176e0675b03fc20f","repo":"Hmbown/CodeWhale","slug":"set-value-needs-an-element-target-type-element-index-from","errorCode":null,"errorMessage":"set_value needs an element target — {type:'element',index} from get_app_state","messagePattern":"set_value needs an element target — (.+?) from get_app_state","errorType":"exception","errorClass":"ExecError","httpStatus":null,"severity":"error","filePath":"crates/tui/plugins/computer-use/src/backends/darwin.mjs","lineNumber":1153,"sourceCode":"      for (let i = 0; i < n; i++) {\n        const press = await withPressedKey(code, flags, () => {});\n        if (Number.isFinite(press?.yield_ms)) yieldMs = Math.max(yieldMs, press.yield_ms);\n        if (i < n - 1) await wait(30);\n      }\n      return { action_sent: true, key, code, keyboard_delivery: state.foregroundInput ? \"foreground-guarded\" : \"process\", repeat: n,\n        ...(yieldMs > 0 ? { yield_ms: yieldMs } : {}),\n        ...(flags !== 0 && !state.foregroundInput ? { note: \"process delivery (no focus lease was taken); menu key equivalents can be dropped without a key window. Verify the effect before retrying, or use invoke_menu for app menu commands.\" } : {}) };\n    },\n    hold_key: async ({ text, duration } = {}) => {\n      const { flags, code, key } = parseChord(text);\n      if (flags !== 0) requireFocusControl();\n      const d = Math.max(0.05, Math.min(30, Number(duration) || 1));\n      const press = await withPressedKey(code, flags, () => wait(d * 1000));\n      return { action_sent: true, key, keyboard_delivery: state.foregroundInput ? \"foreground-guarded\" : \"process\", heldSec: d,\n        ...(Number.isFinite(press?.yield_ms) && press.yield_ms > 0 ? { yield_ms: press.yield_ms } : {}) };\n    },\n    set_value: async (args = {}) => {\n      if (args.target?.type !== \"element\") throw new ExecError(\"set_value needs an element target — {type:'element',index} from get_app_state\");\n      try {\n        return await native(\"set_value\", args);\n      } catch (error) {\n        // Web text controls ignore AXValue writes, so the native side refuses\n        // before dispatch. The replacement path is focus + select-all + type\n        // with a read-back verify — the same shape kimi-cu uses, with the\n        // value proven rather than asserted.\n        if (!/web area/i.test(error.message)) throw error;\n        if (args.target?.type !== \"element\") throw error;\n        requireFocusControl();\n        const value = String(args.value ?? \"\");\n        await native(\"focus_element\", { target: args.target });\n        // cmd+a through the record channel: menu key equivalents only\n        // validate against a key window, which the lease provides. bg_key\n        // posts a complete press (down and up); the `down` field is unused.\n        await native(\"bg_key\", { code: 0, flags: 1 << 20 });\n        await new Promise((r) => setTimeout(r, 60));\n        await native(\"type\", { text: value });","sourceCodeStart":1135,"sourceCodeEnd":1171,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/plugins/computer-use/src/backends/darwin.mjs#L1135-L1171","documentation":"set_value writes an accessibility value directly, which only works for element targets backed by the native AX API. If args.target is missing or its type is not 'element', the backend refuses before dispatch — coordinate or key-based targets have no AXValue to set. The intended workflow is to obtain {type:'element',index} from get_app_state.","triggerScenarios":"Calling set_value with a coordinate target ({type:'point',...}), with no target at all, or with a hand-built target object whose type field is not 'element' instead of the element descriptor returned by get_app_state.","commonSituations":"Confusing set_value with a type-by-coordinates API; constructing the target from a stale or differently-shaped snapshot; passing the element object itself rather than the {type:'element',index} wrapper the backend expects.","solutions":["Call get_app_state, find the field's element entry, and pass { type: 'element', index: el.index } as target.","Do not pass coordinate targets to set_value — use the focus + select-all + type fallback path (or the advertised replacement action) for controls without AXValue support.","Validate the target object's shape ({ type: 'element', index: number }) before calling."],"exampleFix":"// before: coordinate target is rejected\nawait set_value({ target: { type: 'point', x: 120, y: 80 }, value: 'hello' });\n\n// after: element target from a fresh snapshot\nconst state = await get_app_state();\nconst field = state.elements.find(e => e.role === 'AXTextField');\nawait set_value({ target: { type: 'element', index: field.index }, value: 'hello' });","handlingStrategy":"type-guard","validationCode":"function assertElementTarget(target) {\n  if (!target || target.type !== 'element' || !Number.isInteger(target.index)) {\n    throw new Error(\"set_value target must be {type:'element',index} from get_app_state\");\n  }\n}","typeGuard":"function isElementTarget(t) {\n  return t != null && t.type === 'element' && Number.isInteger(t.index) && t.index >= 0;\n}","tryCatchPattern":"try {\n  return await set_value({ target, value });\n} catch (e) {\n  if (String(e.message).includes('set_value needs an element target')) {\n    const state = await get_app_state();\n    const el = state.elements.find(e2 => e2.role === 'AXTextField');\n    return await set_value({ target: { type: 'element', index: el.index }, value });\n  }\n  throw e;\n}","preventionTips":["Obtain element targets exclusively from a fresh get_app_state call; never synthesize coordinates for set_value.","Validate the target shape ({type:'element', index}) before every set_value call.","For web text controls that ignore AXValue, expect the backend's focus + select-all + type fallback and verify via read-back.","Re-fetch the snapshot if the element index may have shifted after UI changes."],"tags":["accessibility","macos","argument-validation","set-value"],"backgroundTag":"invalid-argument-value","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-22T21:17:16.096Z"}