fix(tool-input): address review round 3 — checkbox write race and MCP enum member types

- `CheckboxList` merged onto the value captured at render, so two toggles landing
  before React rerendered both built from the same record and the second write
  dropped the first. Every option in the group now shares one key, so it reads the
  store at write time; the store updates synchronously, so it always sees the
  preceding toggle. The old per-option keys could not hit this.
- The MCP dropdown wrote back the string the combobox works in, so a numeric enum
  sent '1' for 1, 'true' for true and 'null' for null. It now persists the enum
  MEMBER, matched by its stringified form, so nothing downstream has to guess.
- Selection is decided by key presence rather than truthiness, so `0`, `false` and
  a literal `null` member render as selected instead of blank.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vikhyath MondretiandClaude Opus 5 committed 2026-08-31 21:04:36 -07:00
1 parent 17c0968573
commit 679325a899
3 files changed
+39 -11

No files matched your search

@@ -5,6 +5,7 @@ import { formatDisplayText } from '@/app/workspace/[workspaceId]/w/[workflowId]/
import { getWorkflowSearchLabelHighlight } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/workflow-search-highlight'
import { useSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-sub-block-value'
import { useActiveSearchTarget } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/providers/active-search-target-provider'
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
interface CheckboxListOption {
label: string
@@ -58,9 +59,15 @@ export function CheckboxList({
const handleChange = useCallback(
(optionId: string, checked: boolean) => {
if (isPreview || disabled) return
setStoreValue({ ...readSelections(storeValue), [optionId]: checked })
// Merge onto the value the STORE holds right now, not the one captured at render.
// Every option in the group shares one key, so two toggles landing before React
// rerenders would otherwise both build from the same stale record and the second
// write would drop the first. The store updates synchronously, so reading it here
// always sees the preceding toggle.
const current = useSubBlockStore.getState().getValue(blockId, subBlockId)
setStoreValue({ ...readSelections(current), [optionId]: checked })
},
[storeValue, setStoreValue, isPreview, disabled]
[blockId, subBlockId, setStoreValue, isPreview, disabled]
)
return (
@@ -268,11 +268,11 @@ export function McpDynamicArgs({
label: String(option),
value: String(option),
}))
// Options are stringified members, so a decoded numeric/boolean enum value has to
// be stringified back to match one. Label and value are the same string here, so
// the highlight overlay reads the same expression — a falsy member (`0`, `false`)
// is a real selection, not an empty one.
const dropdownValue = value === undefined || value === null ? '' : String(value)
// Options are stringified members, so a decoded value has to be stringified back
// to match one. Presence of the key — not truthiness — decides whether anything is
// selected, because `0`, `false` and a literal `null` enum member are all real
// selections that would otherwise render as empty.
const dropdownValue = Object.hasOwn(current, paramName) ? String(value) : ''
const selectedLabel = dropdownValue
const workflowSearchHighlight = getWorkflowSearchLabelHighlight({
activeSearchTarget,
@@ -289,11 +289,15 @@ export function McpDynamicArgs({
value={dropdownValue}
selectedValue={dropdownValue}
onChange={(selectedValue) => {
const matchedOption = dropdownOptions.find(
(opt: { label: string; value: string }) => opt.value === selectedValue
// Persist the ENUM MEMBER, not the string the combobox works in. The
// options are stringified for display, so writing `selectedValue` back
// would send '1' for `1`, 'true' for `true` and 'null' for `null` — the
// server then rejects the argument or reads it as a different value.
const memberIndex = (paramSchema.enum as unknown[]).findIndex(
(member) => String(member) === selectedValue
)
if (matchedOption) {
updateParameter(paramName, selectedValue)
if (memberIndex !== -1) {
updateParameter(paramName, (paramSchema.enum as unknown[])[memberIndex])
}
}}
placeholder={`Select ${formatParameterLabel(paramName).toLowerCase()}`}
+17
View File
@@ -568,3 +568,20 @@ describe('buildJsonSchemaParamShapes', () => {
expect(decodeToolParamValue(true, getJsonSchemaValueShape({ type: 'boolean' }))).toBe(true)
})
})
describe('enum members that are not plain strings', () => {
it('keeps a nullable enum as text so a real null member survives the decode', () => {
const shape = getJsonSchemaValueShape({ enum: ['a', null] })
expect(shape).toBe('string')
// The dropdown persists the member itself, so `null` arrives already typed and the
// decode must not touch it.
expect(decodeToolParamValue(null, shape)).toBeNull()
})
it('decodes a numeric enum member that is still stored as its stringified form', () => {
expect(
decodeToolParamValue('1', getJsonSchemaValueShape({ type: 'integer', enum: [1, 2] }))
).toBe(1)
})
})