Skip to content

fix(decomposer): guard against empty content array and malformed JSON#727

Open
vishkrish200 wants to merge 2 commits intoComposioHQ:mainfrom
vishkrish200:harden-decomposer-api-response
Open

fix(decomposer): guard against empty content array and malformed JSON#727
vishkrish200 wants to merge 2 commits intoComposioHQ:mainfrom
vishkrish200:harden-decomposer-api-response

Conversation

@vishkrish200
Copy link

What changed and why

Three unsafe patterns in classifyTask() and decomposeTask() in packages/core/src/decomposer.ts:

  • res.content[0] accessed without a length check — the Anthropic API can return an empty content array on certain error conditions, which would throw an unhandled TypeError with no context. Fixed with optional chaining on block?.type.
  • Silent empty-response failure — the previous fallback of "[]" for a non-text block would pass the regex check, parse to an empty array, then throw a misleading "produced 0 subtasks" error. Changed to "" so it correctly hits the "no JSON array in response" error path.
  • JSON.parse with no try-catch — malformed JSON from the API threw a raw SyntaxError with no indication of what was being parsed. Wrapped in a try-catch that includes the offending string in the message.

How to test it

pnpm --filter @composio/ao-core test

New test file packages/core/src/__tests__/decomposer.test.ts covers:

  • All 6 pure functions (formatLineage, formatSiblings, getLeaves, getSiblings, formatPlanTree, propagateStatus)
  • classifyTask with empty content → defaults to "atomic"
  • decomposeTask with empty content → throws "no JSON array in response"
  • decomposeTask with malformed JSON → throws "invalid JSON in response: ..."
  • Happy path: valid composite response produces correct two-child tree

vishkrish200 and others added 2 commits March 26, 2026 23:19
- Use optional chaining on res.content[0] in classifyTask and
  decomposeTask so an empty content array doesn't crash with TypeError
- Change decomposeTask fallback from "[]" to "" so empty content
  hits the "no JSON array" error with a clear message
- Wrap JSON.parse in try-catch to surface malformed JSON with context
- Add decomposer.test.ts: 18 tests covering all pure functions and
  LLM error paths via a mocked Anthropic client

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant