Skip to content

Commit 253ee6c

Browse files
Sync public snapshot from freebuff-private
Source: CodebuffAI/freebuff-private@fb3388a9e0869ce1066f3ecbd3f3342aecbf16fb
1 parent 52fa3fb commit 253ee6c

2 files changed

Lines changed: 180 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { describe, expect, it } from 'bun:test'
2+
3+
import {
4+
FREEBUFF_FREE_PROMOTION_SPEND_WINDOWS,
5+
isFreePromotionSpendAt,
6+
} from '../freebuff-free-promotions'
7+
import { SOLAR_PRICE_CHANGES } from '../freebuff-solar-promo'
8+
import { FREEBUFF_SOLAR_PRO_4_MODEL_ID } from '../freebuff-model-entitlements'
9+
10+
describe('free-promotion spend windows', () => {
11+
it('matches the offer it is derived from, to the boundary', () => {
12+
// The window has to be exactly the span the user is charged 0 for. A
13+
// window WIDER than the offer exempts spend from a paid day; a NARROWER
14+
// one charges a user for a model we told them was free — which is the
15+
// complaint this was built for.
16+
const free = SOLAR_PRICE_CHANGES.find((c) => c.price === 0)!
17+
const back = SOLAR_PRICE_CHANGES.find(
18+
(c) => c.price !== 0 && Date.parse(c.at) > Date.parse(free.at),
19+
)!
20+
expect(FREEBUFF_FREE_PROMOTION_SPEND_WINDOWS).toEqual([
21+
{
22+
modelId: FREEBUFF_SOLAR_PRO_4_MODEL_ID,
23+
from: new Date(Date.parse(free.at)).toISOString(),
24+
to: new Date(Date.parse(back.at)).toISOString(),
25+
},
26+
])
27+
})
28+
29+
it('is half-open, so the day the price returns is charged again', () => {
30+
const solar = FREEBUFF_SOLAR_PRO_4_MODEL_ID
31+
expect(isFreePromotionSpendAt(solar, new Date('2026-09-04T23:59:59-07:00'))).toBe(false)
32+
expect(isFreePromotionSpendAt(solar, new Date('2026-09-05T00:00:00-07:00'))).toBe(true)
33+
expect(isFreePromotionSpendAt(solar, new Date('2026-09-07T23:59:59-07:00'))).toBe(true)
34+
// The instant the price goes back to 5, spend counts again — with no
35+
// deploy, which is the point of deriving this from the schedule.
36+
expect(isFreePromotionSpendAt(solar, new Date('2026-09-08T00:00:00-07:00'))).toBe(false)
37+
})
38+
39+
it('exempts nothing else', () => {
40+
for (const modelId of [
41+
'deepseek/deepseek-v4-flash',
42+
'z-ai/glm-5.3-flash',
43+
'openai/gpt-5.6-luna',
44+
]) {
45+
expect(
46+
isFreePromotionSpendAt(modelId, new Date('2026-09-06T12:00:00-07:00')),
47+
).toBe(false)
48+
}
49+
})
50+
51+
it('never leaves a window open when the price returns', () => {
52+
// A window that never closes makes a weekend promotion permanent. Every
53+
// span here must end at a real transition unless the schedule genuinely
54+
// leaves the price at zero.
55+
for (const window of FREEBUFF_FREE_PROMOTION_SPEND_WINDOWS) {
56+
const returns = SOLAR_PRICE_CHANGES.some(
57+
(c) => c.modelId === window.modelId && c.price !== 0 &&
58+
Date.parse(c.at) > Date.parse(window.from),
59+
)
60+
if (returns) {
61+
expect(Date.parse(window.to)).toBeLessThan(
62+
Date.parse('2099-01-01T00:00:00.000Z'),
63+
)
64+
}
65+
expect(Date.parse(window.to)).toBeGreaterThan(Date.parse(window.from))
66+
}
67+
})
68+
})
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { SOLAR_PRICE_CHANGES } from './freebuff-solar-promo'
2+
3+
/**
4+
* Spans in which a model is free to the user, and whose provider cost must
5+
* therefore not be charged against that user's daily dollar allowance.
6+
*
7+
* ## Why this exists
8+
*
9+
* A promotion that makes a row free, while its spend still consumes the one
10+
* budget deciding whether the account can start anything at all, is an offer
11+
* working against itself: the user is handed a model at no price and then
12+
* locked out of the rest of the catalog for the day by using it. Reported by
13+
* users on Solar Pro 4's 2026-09-05 weekend, which is what this was built for.
14+
*
15+
* ## Derived, not written down
16+
*
17+
* The windows come from the price schedule the offer is actually made from
18+
* (`SOLAR_PRICE_CHANGES`), so the span that is free to the user and the span
19+
* that is exempt from their allowance cannot drift apart. Hand-copying the
20+
* dates would put the two facts in two files and guarantee that a future
21+
* extension moves one and not the other.
22+
*
23+
* A window opens at every transition to price 0 and closes at the next
24+
* transition to any other price. An offer left open-ended closes at the end of
25+
* time, which is deliberate: while the price is zero the exemption holds.
26+
*
27+
* ## What it must not be used for
28+
*
29+
* ONLY the per-user allowance. `message.cost` is untouched, because we really
30+
* do pay for these tokens — the ledger, the dashboards and the provider
31+
* accounting all have to keep saying so. This decides whose budget it comes
32+
* out of, not whether it was spent.
33+
*/
34+
export interface FreePromotionSpendWindow {
35+
readonly modelId: string
36+
/** Inclusive ISO instant the model became free. */
37+
readonly from: string
38+
/** Exclusive ISO instant it stopped being free. */
39+
readonly to: string
40+
}
41+
42+
/** Far enough out to mean "still running", and a real date so the SQL
43+
* comparison stays a plain range check rather than a null branch. */
44+
const OPEN_ENDED = '2099-01-01T00:00:00.000Z'
45+
46+
function windowsFromPriceSchedule(
47+
changes: ReadonlyArray<{
48+
readonly at: string
49+
readonly modelId: string
50+
readonly price: number
51+
}>,
52+
): FreePromotionSpendWindow[] {
53+
const byModel = new Map<string, typeof changes>()
54+
for (const change of changes) {
55+
byModel.set(change.modelId, [
56+
...(byModel.get(change.modelId) ?? []),
57+
change,
58+
])
59+
}
60+
const windows: FreePromotionSpendWindow[] = []
61+
for (const [modelId, entries] of byModel) {
62+
const ordered = [...entries].sort(
63+
(a, b) => Date.parse(a.at) - Date.parse(b.at),
64+
)
65+
ordered.forEach((change, index) => {
66+
if (change.price !== 0) return
67+
// The next transition at a DIFFERENT price closes it. A second free
68+
// transition (a re-announcement, or an extension) extends rather than
69+
// splits, so a window is never cut short by a restatement of itself.
70+
const end = ordered
71+
.slice(index + 1)
72+
.find((later) => later.price !== 0)
73+
const from = new Date(Date.parse(change.at)).toISOString()
74+
const to = end ? new Date(Date.parse(end.at)).toISOString() : OPEN_ENDED
75+
// Merge into the previous window when this free transition sits inside
76+
// one already open, so the restatement case emits one span.
77+
const previous = windows[windows.length - 1]
78+
if (
79+
previous &&
80+
previous.modelId === modelId &&
81+
Date.parse(from) <= Date.parse(previous.to)
82+
) {
83+
windows[windows.length - 1] = {
84+
modelId,
85+
from: previous.from,
86+
to: Date.parse(to) > Date.parse(previous.to) ? to : previous.to,
87+
}
88+
return
89+
}
90+
windows.push({ modelId, from, to })
91+
})
92+
}
93+
return windows
94+
}
95+
96+
export const FREEBUFF_FREE_PROMOTION_SPEND_WINDOWS: readonly FreePromotionSpendWindow[] =
97+
Object.freeze(windowsFromPriceSchedule(SOLAR_PRICE_CHANGES))
98+
99+
/** Whether `modelId` was free to the user at `at` — the same question the SQL
100+
* predicate asks, for callers that have a row rather than a query. */
101+
export function isFreePromotionSpendAt(
102+
modelId: string,
103+
at: number | Date,
104+
): boolean {
105+
const t = typeof at === 'number' ? at : at.getTime()
106+
return FREEBUFF_FREE_PROMOTION_SPEND_WINDOWS.some(
107+
(window) =>
108+
window.modelId === modelId &&
109+
t >= Date.parse(window.from) &&
110+
t < Date.parse(window.to),
111+
)
112+
}

0 commit comments

Comments
 (0)