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
12 changes: 9 additions & 3 deletions src/codex/reset-credit-auto-redeem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,15 @@ function readJournal(path: string): Journal {
}

function writeJournal(path: string, journal: Journal, now: number): void {
// Keep only entries whose credit could still matter: settled ones older than a week are noise.
const cutoff = now - 7 * 24 * 60 * 60_000;
journal.entries = journal.entries.filter(e => e.state !== "settled" || e.updatedAt > cutoff);
// Keep only entries whose credit could still matter: a settled entry stays until the later of
// a week after its last update and the credit's own expiry, so it is never dropped while the
// credit it records can still be listed. An unparseable expiry falls back to the week rule.
const week = 7 * 24 * 60 * 60_000;
journal.entries = journal.entries.filter(e => {
if (e.state !== "settled") return true;
const expiry = Date.parse(e.expiresAt);
return now < Math.max(e.updatedAt + week, Number.isNaN(expiry) ? 0 : expiry);
});
atomicWriteFile(path, JSON.stringify(journal, null, 2));
}

Expand Down
17 changes: 17 additions & 0 deletions tests/codex-integration/codex-reset-credit-auto-redeem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,23 @@ describe("reset-credit auto-redeemer runtime (#822)", () => {
expect(entries[0].updatedAt).toBe(start + 20 * MIN);
});

test("a settled entry is kept until its credit expires, even past the one-week age", async () => {
const DAY = 24 * 60 * MIN;
const journalFile = join(dir, "j.json");
const seeded = (grantedAt: string, expiresAt: string) => ({
accountKey: "seeded", grantedAt, expiresAt, redeemRequestId: `id-${grantedAt}`, state: "settled", updatedAt: T0 - 8 * DAY,
});
writeFileSync(journalFile, JSON.stringify({ version: 1, entries: [
seeded("2026-08-01T00:00:00Z", new Date(T0 + 30 * DAY).toISOString()), // old update, credit not expired yet
seeded("2026-08-02T00:00:00Z", new Date(T0 - 1 * DAY).toISOString()), // old update, credit expired
] }));
const h = harness({ journalFile, credits: () => [credit(30)] });
h.setNow(T0 + 20 * MIN);
expect((await h.redeemer.tick()).kind).toBe("dispatched");
const entries = JSON.parse(readFileSync(journalFile, "utf8")).entries as Array<{ grantedAt: string }>;
expect(entries.map(e => e.grantedAt).sort()).toEqual(["2026-08-01T00:00:00Z", "2026-09-01T00:00:00Z"]);
});

test("a persistent reservation write failure uses the idle retry interval", async () => {
const journalFile = join(dir, "journal-directory");
mkdirSync(journalFile);
Expand Down
Loading