Skip to content

task tool crashes with "messages.map is not a function" whenever session.messages returns a non-2xx (normalizeSDKResponse preferResponseOnMissingData yields non-array) #6912

Closed

Description

@linletian

Bug

Every task(...) tool call fails immediately with:

messages.map is not a function. (In 'messages.map((message) => ({
    stored: convertSDKMessageToStoredMessage(message),
    createdAt: message.info?.time?.created ?? Number.NEGATIVE_INFINITY,
    id: typeof message.id === "string" ? message.id : ""
  }))', 'messages.map' is undefined)

No subagent session is ever created. Other tools (bash/edit/read/...) are unaffected.

Root cause (traced in dist/index.js, v4.11.1)

fetchSDKMessages (features/hook-message-injector/sdk-message-lookup.ts) does:

const response = await client.session.messages({ path: { id: sessionID } });
return normalizeSDKResponse(response, [], { preferResponseOnMissingData: true });

When the HTTP call fails with a non-2xx status, the hey-api SDK resolves (does not throw) with { error, request, response } — there is no data key. With preferResponseOnMissingData: true, normalizeSDKResponse then returns the raw response object, which is truthy and not an array. Callers (findNearestMessageWithFieldsFromSDK → findNearestMessageWithFieldsFromMessages, findFirstMessageWithAgentFromSDK, findMessageContextFromSDK) pass it straight into messages.map(...) → the crash above.

The task tool hits this on every dispatch via executeBackground → resolveMessageContext (sqlite backend → SDK path), which is why task delegation fails 100% while the rest of the session keeps working.

The same latent crash exists anywhere an array-expecting call site uses preferResponseOnMissingData: true (e.g. the todo fetch paths) — any non-2xx from session.messages/session.todo (401, 404, 502, ...) turns into a TypeError instead of graceful degradation.

Trigger in my environment (for completeness)

opencode serve 1.18.18 with OPENCODE_SERVER_PASSWORD set, plus a system HTTP_PROXY whose no_proxy used CIDR notation (127.0.0.0/8). Bun's fetch does not match CIDR entries in NO_PROXY (filed as oven-sh/bun#39352), so the plugin's loopback API calls were routed through the proxy and came back 502. Fixing the proxy env makes the crash disappear — but the missing array guard means any transient non-2xx will crash the task tool the same way.

Same symptom, different trigger: anomalyco/opencode#33114 (task() fails with messages.map is not a function over WSL).

Suggested fix

Guard the array contract after normalization, e.g. in fetchSDKMessages:

const messages = normalizeSDKResponse(response, emptyMessages, { preferResponseOnMissingData: true });
if (!Array.isArray(messages)) {
  log2("[hook-message-injector] SDK message fetch returned non-array", { sessionID });
  return null;
}
return messages;

(and the same Array.isArray check for the other preferResponseOnMissingData: true array call sites), so a failed SDK fetch degrades to the existing null fallback path instead of crashing the tool call.

Versions

  • oh-my-openagent 4.11.1
  • opencode 1.18.18 (Bun 1.3.14)
  • Linux x86_64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions