Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions pm-stack/aggregator/src/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import type { UnmatchedCharge } from "./charges-match.ts";
import type { CheckoutDrift } from "./checkout-drift.ts";
import { isProjectPoolItem } from "./projects.ts";
import { isRepoCensus, type RepoCensus } from "./coverage.ts";
import { arrayOf, isFiniteNumber, isIsoDate, isRecord, isStringArray, isTimestampNotFuture } from "./validate.ts";
import {
arrayOf,
isFiniteNumber,
isIsoDate,
isNonNegative,
isRecord,
isStringArray,
isTimestampNotFuture,
} from "./validate.ts";
import type {
AaFeedSection,
BurnSegment,
Expand Down Expand Up @@ -404,15 +412,20 @@ export function isQuotaPool(v: unknown): v is QuotaPool {
return true;
}

/** One historical burn segment as renderQuota maps it (#2429). */
/** One historical burn segment as renderQuota maps it (#2429).
* Values, not just types (#3428): from/to must be real ISO calendar
* dates, tokens may not be negative, and the range may not be
* inverted — a segment failing any of these is not measured history
* and drops individually in sanitizeQuotaView (#2429 counting). */
export function isBurnSegment(v: unknown): v is BurnSegment {
if (!isRecord(v)) return false;
return (
typeof v.provider === "string" &&
typeof v.pool === "string" &&
typeof v.from === "string" &&
typeof v.to === "string" &&
isFiniteNumber(v.tokens)
isIsoDate(v.from) &&
isIsoDate(v.to) &&
isNonNegative(v.tokens) &&
Date.parse(v.from) <= Date.parse(v.to)
);
}

Expand Down
29 changes: 29 additions & 0 deletions pm-stack/aggregator/test/feed.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, test } from "bun:test";
import {
feedSectionStatus,
isBurnSegment,
isChargeRow,
isFleetFeed,
isProjectPoolSection,
Expand Down Expand Up @@ -224,6 +225,34 @@ describe("feed quota section gate + sanitization (#2429)", () => {
expect(out.view.burn).toEqual([seg]);
});

// #3428: value-malformed segments (negative tokens, non-date or
// impossible from/to, inverted range) used to pass the type-only
// guard and render as measured history. They now drop individually
// and count, exactly like shape failures.
test("value-malformed burn segments drop individually", () => {
const seg = {
provider: "opencode",
pool: "go",
from: "2026-10-01T00:00:00Z",
to: "2026-10-01T01:00:00Z",
tokens: 42,
};
const negative = { ...seg, tokens: -12345 };
const garbageFrom = { ...seg, from: "banana" };
const impossibleTo = { ...seg, to: "2026-02-30T01:00:00Z" };
const inverted = { ...seg, from: "2026-10-02T00:00:00Z", to: "2026-10-01T00:00:00Z" };
expect(isBurnSegment(seg)).toBe(true);
expect(isBurnSegment(negative)).toBe(false);
expect(isBurnSegment(garbageFrom)).toBe(false);
expect(isBurnSegment(impossibleTo)).toBe(false);
expect(isBurnSegment(inverted)).toBe(false);
const out = sanitizeQuotaView(
quotaWith([POOL], { burn: [seg, negative, garbageFrom, impossibleTo, inverted] }) as never,
);
expect(out.droppedBurn).toBe(4);
expect(out.view.burn).toEqual([seg]);
});

test("a fully well-formed quota view is unchanged", () => {
const q = quotaWith([POOL], { burn: [] }) as unknown as QuotaView;
const out = sanitizeQuotaView(q);
Expand Down
Loading