rohitg00/agentmemory · error · Error

Invalid dateTo: ${filter.dateTo}

Error message

Invalid dateTo: ${filter.dateTo}

What it means

queryAudit filters audit entries by an optional dateTo bound. If the value cannot be parsed by new Date() (NaN result), the library throws instead of silently returning wrong results. Same validation family as the dateFrom error, but for the upper bound of the time range.

Source

Thrown at src/functions/audit.ts:107

  const all = await kv.list<AuditEntry>(KV.audit);
  let entries = [...all].sort(
    (a, b) => new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime(),
  );

  if (filter?.operation) {
    entries = entries.filter((e) => e.operation === filter.operation);
  }
  if (filter?.dateFrom) {
    const from = new Date(filter.dateFrom).getTime();
    if (Number.isNaN(from)) {
      throw new Error(`Invalid dateFrom: ${filter.dateFrom}`);
    }
    entries = entries.filter((e) => new Date(e.timestamp).getTime() >= from);
  }
  if (filter?.dateTo) {
    const to = new Date(filter.dateTo).getTime();
    if (Number.isNaN(to)) {
      throw new Error(`Invalid dateTo: ${filter.dateTo}`);
    }
    entries = entries.filter((e) => new Date(e.timestamp).getTime() <= to);
  }

  return entries.slice(0, filter?.limit || 100);
}

View on GitHub (pinned to e04ba88819)

Solutions

  1. Pass dateTo as an ISO 8601 string, e.g. '2024-01-31T23:59:59.999Z'.
  2. Validate parseability with new Date(value) before the call.
  3. For 'now' bounds, generate the value with new Date().toISOString() on the caller side.
  4. Log the raw value to spot silent corruption (empty string, undefined stringified).

Example fix

// before
{ dateTo: String(endMs) }
// after
{ dateTo: new Date(endMs).toISOString() }
Defensive patterns

Strategy: validation

Validate before calling

function assertIsoDate(v: string, label: string): string {
  const t = new Date(v).getTime();
  if (Number.isNaN(t)) throw new Error(`${label} is not a valid date: ${v}`);
  return new Date(t).toISOString();
}
const dateTo = assertIsoDate(input.dateTo, 'dateTo');

Type guard

function isParseableDate(v: unknown): v is string {
  return typeof v === 'string' && !Number.isNaN(new Date(v).getTime());
}

Try / catch

try {
  return await queryAudit({ dateTo });
} catch (e) {
  if (String(e.message).startsWith('Invalid date')) return { entries: [], hint: 'use ISO 8601 dates' };
  throw e;
}

Prevention

When it happens

Trigger: Calling queryAudit (via entries/entriesBefore or the audit MCP/REST surface) with filter.dateTo set to an unparseable string like 'tomorrow', '2024-13-01', or a locale-formatted date.

Common situations: Constructing ranges from user CLI input without validation; passing millisecond timestamps as numbers coerced to weird strings; i18n formatting of dates before sending.

Related errors


AI-assisted analysis of rohitg00/agentmemory@e04ba88819 (2026-08-30). Data as JSON: /api/errors/ffe596fd743f43c2. Report an issue: GitHub.