Skip to content

Conversation

@SJY0917032
Copy link

@SJY0917032 SJY0917032 commented Jan 9, 2026

Summary

  • Fix todowrite tool failing with "expected array, received string" error when LLMs pass todos as a JSON string instead of an array
  • Update tool.execute.before hook to parse string input before Zod validation

Problem

When invoking the todowrite tool, some LLM tool-calling interfaces pass the todos parameter as a JSON string instead of an array. This caused failures like:

Error: The todowrite tool was called with invalid arguments: [
  {
    "expected": "array",
    "code": "invalid_type",
    "path": ["todos"],
    "message": "Invalid input: expected array, received string"
  }
]

The root cause: OpenCode's TodoWriteTool uses Zod schema z.array(...) for validation, but the LLM sometimes sends the array as a stringified JSON.

Solution

Add a pre-validation check in tool.execute.before hook that detects when todos is a string and parses it to an array before the tool execution.

This is similar to the fix in #532 which handled skill_mcp arguments passed as object instead of string (opposite direction).

Test Plan

  • bun run typecheck passes
  • bun run build succeeds
  • Manually verified todowrite works after the fix (tested in local OpenCode session)

Summary by cubic

Fixes todowrite failures when LLMs send todos as a JSON string. The before-execute hook now parses string todos to an array (with success/error logs) before Zod validation so the tool runs correctly.

Written for commit f105ceb. Summary will update on new commits.

@github-actions
Copy link
Contributor

github-actions bot commented Jan 9, 2026

All contributors have signed the CLA. Thank you! ✅
Posted by the CLA Assistant Lite bot.

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@SJY0917032
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Jan 9, 2026
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file

Confidence score: 3/5

  • src/hooks/claude-code-hooks/index.ts assumes the parsed LLM response is always an array, so a valid JSON scalar like 'null' or '{}' would slip through and likely break downstream logic.
  • Because this is a concrete parsing/validation gap, there’s a moderate chance of user-facing errors if an LLM returns unexpected JSON.
  • Pay close attention to src/hooks/claude-code-hooks/index.ts - add post-parse validation to ensure the value is actually an array.
Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="src/hooks/claude-code-hooks/index.ts">

<violation number="1" location="src/hooks/claude-code-hooks/index.ts:207">
P2: After `JSON.parse`, the result should be validated as an array. If an LLM sends a non-array JSON string (e.g., `'null'`, `'{}'`), parsing succeeds but produces a non-array value. The log message "parsed todos string to array" would be misleading, and it's inconsistent with the pattern in `todo.ts` which uses `Array.isArray()` after parsing.</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@SJY0917032 SJY0917032 force-pushed the fix/todowrite-string-to-array branch from ea08b5a to f105ceb Compare January 9, 2026 10:09
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