Skip to content

[BUG REPORT] - ai.js crashes on malformed LLM cluster instead of retrying #2

Description

@SegoCode

Description

In code/js/ai.js, cluster validation assumes every LLM response item has a defined source field. When the model returns a malformed cluster (missing source or source: undefined), c.source.length throws a TypeError and kills the entire GitHub Actions workflow.

This should be treated as an invalid response and trigger the existing retry logic (lines 30-76), not abort execution.

Stack trace:

file:///home/runner/work/NewsBucket/NewsBucket/code/js/ai.js:72
    clusters.every((c) => c.count === c.source.length);
                                               ^

TypeError: Cannot read properties of undefined (reading 'length')
    at file:///home/runner/work/NewsBucket/NewsBucket/code/js/ai.js:72:47
    at Array.every (<anonymous>)
    at file:///home/runner/work/NewsBucket/NewsBucket/code/js/ai.js:72:13

Affected code (code/js/ai.js, lines 71-75):

const ok = clusters.length > 0 &&
    clusters.every((c) => c.count === c.source.length);
if (ok) break;
const why = clusters.length === 0 ? "empty" : "count mismatch";
if (!attempt) console.warn(`  ↻ retry 1/1 (${why})`);

Steps to reproduce

  1. Run pnpm ai (or the Update news GitHub Actions workflow) against a feed with enough articles from the last 24 hours.
  2. Wait for the LLM to return parseable JSON where at least one cluster is missing source or has a non-array source.
  3. Observe that validation at line 72 throws TypeError before ok can evaluate to false.

Expected behavior

  • A malformed cluster (source missing, null, or not an array) should fail validation safely.
  • The pipeline should enter the retry path (↻ retry 1/1), same as for empty responses or count mismatches.
  • Only after retries are exhausted should it throw an explicit error, never an unhandled TypeError during validation.

Actual behavior

  • Array.every() calls c.source.length without checking that source exists.
  • A single malformed cluster causes TypeError: Cannot read properties of undefined (reading 'length').
  • The GitHub Actions job fails completely; no retry runs and remaining feeds are never processed.

OS version

Linux Other

Confirmation

  • I performed a search of the issue tracker to avoid opening a duplicate issue
  • I understand that not filling out this template correctly may lead to the issue being closed

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions