diff --git a/pm-stack/aggregator/src/quota-extract.ts b/pm-stack/aggregator/src/quota-extract.ts index 074c05c660..a2d59150a5 100644 --- a/pm-stack/aggregator/src/quota-extract.ts +++ b/pm-stack/aggregator/src/quota-extract.ts @@ -156,9 +156,16 @@ function goWorkspace(name: "Personal" | "Lab"): Extractor { new RegExp(`\\*\\*${name} — monthly (\\d+(?:\\.\\d+)?)% used(?:, weekly (\\d+(?:\\.\\d+)?)%)?`, "i"), ); if (!m) return null; - // Resets appear in the same paragraph order as the workspaces. - const resets = [...s.matchAll(/monthly reset (\w{3}) (\d{1,2}), (\d{2}:\d{2}) UTC/gi)]; - const r = name === "Personal" ? resets[0] : resets[1]; + // Paragraph anchoring (#1626): a workspace's reset is the first + // 'monthly reset' in its own paragraph — the text from its percentage + // match to the next blank line (or the end of the section). A paragraph + // with no reset yields null; a reset is never borrowed from another + // workspace's paragraph or from an earlier mention elsewhere in the + // section (the old section-wide index pairing shifted both workspaces' + // resets whenever one reset line was missing or an extra one appeared). + const paraEnd = s.indexOf("\n\n", m.index); + const paragraph = s.slice(m.index, paraEnd === -1 ? undefined : paraEnd); + const r = first(paragraph, /monthly reset (\w{3}) (\d{1,2}), (\d{2}:\d{2}) UTC/i); const monthlyReset = r ? monthDayTime(r[1], r[2], r[3], now) : null; const windows: QuotaWindow[] = [usdWindow("monthly", (Number(m[1]) / 100) * 60, 60, monthlyReset)]; if (m[2] !== undefined) windows.push(usdWindow("weekly", (Number(m[2]) / 100) * 30, 30, null)); diff --git a/pm-stack/aggregator/test/quota.test.ts b/pm-stack/aggregator/test/quota.test.ts index 85015fc9e8..c5b66088ff 100644 --- a/pm-stack/aggregator/test/quota.test.ts +++ b/pm-stack/aggregator/test/quota.test.ts @@ -69,6 +69,46 @@ describe("quota extractors", () => { expect(p.wallLiftsAt).toBeUndefined(); }); + test("opencode go resets are anchored to each workspace's own paragraph (#1626)", () => { + const goView = (section: string) => "\n### OpenCode Go \u00d72 workspaces\n" + section + "\n"; + const go = (view: string) => { + const m = new Map(extractQuotaPools(view, NOW).map((p) => [p.id, p])); + return { personal: m.get("opencode-go:personal")!, lab: m.get("opencode-go:lab")! }; + }; + + // Control: both paragraphs carry their own reset. + const both = go( + goView( + "- **Personal \u2014 monthly 60% used**; monthly reset Nov 3, 03:02 UTC.\n\n" + + "- **Lab \u2014 monthly 50% used, weekly 0%**; monthly reset Nov 9, 05:55 UTC.", + ), + ); + expect(both.personal.windows[0].resetsAt).toBe("2026-11-03T03:02:00.000Z"); + expect(both.lab.windows[0].resetsAt).toBe("2026-11-09T05:55:00.000Z"); + + // Personal's paragraph has no reset: Personal gets null — never Lab's — + // and Lab still gets its own. + const missing = go( + goView( + "- **Personal \u2014 monthly 60% used**, no reset read this time.\n\n" + + "- **Lab \u2014 monthly 50% used, weekly 0%**; monthly reset Nov 9, 05:55 UTC.", + ), + ); + expect(missing.personal.windows[0].resetsAt).toBeNull(); + expect(missing.lab.windows[0].resetsAt).toBe("2026-11-09T05:55:00.000Z"); + + // A stray reset mention in an earlier paragraph is never assigned. + const stray = go( + goView( + "- Earlier note: a monthly reset Oct 5, 01:00 UTC was observed last cycle.\n\n" + + "- **Personal \u2014 monthly 60% used**; monthly reset Nov 3, 03:02 UTC.\n\n" + + "- **Lab \u2014 monthly 50% used, weekly 0%**; monthly reset Nov 9, 05:55 UTC.", + ), + ); + expect(stray.personal.windows[0].resetsAt).toBe("2026-11-03T03:02:00.000Z"); + expect(stray.lab.windows[0].resetsAt).toBe("2026-11-09T05:55:00.000Z"); + }); + test("opencode go workspaces are separate pools in USD", () => { const personal = byId.get("opencode-go:personal")!; expect(personal.windows[0]).toMatchObject({ unit: "usd", limit: 60, used: 36 });