A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.
TypeScript
Keep paid funding records out of the previous release's key and bill pre-deploy turns as before
Pranita409e2a7c365
diff --git a/lib/turn-credential.ts b/lib/turn-credential.tsindex 1abb3e0..9b9e3fc 100644--- a/lib/turn-credential.ts+++ b/lib/turn-credential.ts@@ -29,6 +29,10 @@ function password(): string {const credentialKey = (chatId: string) => prefixed(`credential:${chatId}`)const fundingKey = (chatId: string) => prefixed(`funding:${chatId}`)+// Paid turns' funding records. The code before per-turn funding also reads+// `funding:`, and refuses a chat's unpinned paid turns once it holds any record.+// Kept apart, a rollback to that code bills paid chats as it always did.+const paidFundingKey = (chatId: string) => prefixed(`paid-funding:${chatId}`)const turnCredentialKey = (chatId: string, turnId: number) => `${credentialKey(chatId)}:turn:${turnId}`const loginKey = (loginId: string) => prefixed(`login-chats:${loginId}`)const revokedLoginKey = (loginId: string) => prefixed(`revoked-login:${loginId}`)@@ -107,7 +111,7 @@ async function storeCredential(chatId: string, credential: TurnCredential, ttlSepipeline.set(turnCredentialKey(chatId, turnId), sealed, { ex: ttlSeconds })// This non-secret marker survives expiry/revocation, so a missing trial// credential can never fall back to the customer's latest paid grant.- .hset(fundingKey(chatId), { [String(turnId)]: JSON.stringify(binding) })+ .hset(sponsored ? fundingKey(chatId) : paidFundingKey(chatId), { [String(turnId)]: JSON.stringify(binding) })}await pipeline.sadd(loginKey(credential.loginId), chatId)// The index outlives no credential it lists: a login's tokens expire too.@@ -157,17 +161,22 @@ const refuse = (refused: FundingRefusal): ExecutionFunding => ({ ok: false, refu// What funds a turn's model request, from the chat's records alone. A turn with// a funding record bills that record's user, login and team, or nothing: later// messages and renewals can change only the token. A chat with no records at-// all predates per-turn funding and bills its latest user credential.+// all predates per-turn funding and bills its latest user credential, and so+// does a turn older than the chat's first record (`firstFunded`): one that was+// already running or queued when per-turn funding shipped. Every message since+// is pinned before libfx sees it, so any other turn without a record bills nothing.export function resolveFunding(records: number,turnId: number | null,binding: FundingBinding | null,exact: TurnCredential | null,latest: TurnCredential | null,+ firstFunded: number | null = null,): ExecutionFunding {- if (records === 0) return latest && !('kind' in latest) ? { ok: true, credential: latest } : refuse('unusable')+ const latestUser = (): ExecutionFunding => latest && !('kind' in latest) ? { ok: true, credential: latest } : refuse('unusable')+ if (records === 0) return latestUser()if (turnId === null) return refuse('no-turn')- if (!binding) return refuse('unbound-turn')+ if (!binding) return firstFunded !== null && turnId < firstFunded ? latestUser() : refuse('unbound-turn')const sameLogin = (credential: TurnCredential | null) =>credential?.userId === binding.userId && credential.loginId === binding.loginIdif (binding.kind === 'trial') return exact && 'kind' in exact && sameLogin(exact) ? { ok: true, credential: exact } : refuse('unusable')@@ -184,15 +193,34 @@ export function resolveFunding(// selection, on every request, and is never cached.export async function readExecutionFunding(chatId: string, turnId?: number): Promise<ExecutionFunding> {const turn = Number.isSafeInteger(turnId) && (turnId as number) > 0 ? turnId as number : null- const pipeline = redis().pipeline().hlen(fundingKey(chatId)).get(credentialKey(chatId))- if (turn !== null) pipeline.hget(fundingKey(chatId), String(turn)).get(turnCredentialKey(chatId, turn))+ const pipeline = redis().pipeline().hlen(fundingKey(chatId)).hlen(paidFundingKey(chatId)).get(credentialKey(chatId))+ if (turn !== null) pipeline.hget(paidFundingKey(chatId), String(turn)).hget(fundingKey(chatId), String(turn)).get(turnCredentialKey(chatId, turn))const values: unknown[] = await pipeline.exec()- const [count, latestSeal, bindingValue, turnSeal] = values- const [latest, exact] = await Promise.all([decodeCredential(latestSeal), decodeCredential(turnSeal)])- const funding = resolveFunding(Number(count), turn, fundingBinding(bindingValue), exact, latest)+ const [count, paidCount, latestSeal, paidBinding, binding, turnSeal] = values+ const records = Number(count) + Number(paidCount)+ const bindingValue = paidBinding ?? binding+ const [latest, exact, firstFunded] = await Promise.all([+ decodeCredential(latestSeal),+ decodeCredential(turnSeal),+ // Only a turn with no record at all needs the chat's first record.+ records > 0 && turn !== null && bindingValue == null ? firstFundedTurn(chatId) : null,+ ])+ const funding = resolveFunding(records, turn, fundingBinding(bindingValue), exact, latest, firstFunded)return funding.ok && await loginRevoked(funding.credential.loginId) ? refuse('revoked') : funding}+// The chat's first funded turn, across both kinds of record.+async function firstFundedTurn(chatId: string): Promise<number | null> {+ const [trial, paid] = await redis().pipeline().hkeys(fundingKey(chatId)).hkeys(paidFundingKey(chatId)).exec() as [unknown, unknown]+ const ids = [trial, paid].flatMap((keys) => Array.isArray(keys) ? keys.map(Number) : []).filter((id) => Number.isSafeInteger(id) && id > 0)+ return ids.length ? Math.min(...ids) : null+}++// A chat's funding records of both kinds, by turn.+function fundingRecords(trial: unknown, paid: unknown): Record<string, unknown> {+ return { ...(trial ?? {}) as Record<string, unknown>, ...(paid ?? {}) as Record<string, unknown> }+}+// What sign-out deletes in one chat, from what the chat held when read: the// credentials of the turns this login funded, and the chat's latest credential// if it was this login's, named by its exact sealed value. Funding records stay,@@ -234,11 +262,11 @@ export async function dropLoginCredentials(loginId: string): Promise<DroppedChat// with one compare-and-delete script per chat. The seal is decoded directly:// sign-out has already revoked this login.const reads = redis().pipeline()- for (const chatId of chats) reads.get(credentialKey(chatId)).hgetall(fundingKey(chatId))+ for (const chatId of chats) reads.get(credentialKey(chatId)).hgetall(fundingKey(chatId)).hgetall(paidFundingKey(chatId))const values: unknown[] = chats.length ? await reads.exec() : []const cleanups = await Promise.all(chats.map(async (chatId, index) => {- const latestSeal = values[index * 2]- const bindings = (values[index * 2 + 1] ?? {}) as Record<string, unknown>+ const latestSeal = values[index * 3]+ const bindings = fundingRecords(values[index * 3 + 1], values[index * 3 + 2])return { chatId, ...signOutCleanup(loginId, bindings, latestSeal, await decodeCredential(latestSeal)) }}))const owned = cleanups.filter(({ turnIds, latestSeal }) => turnIds.length || latestSeal !== null)@@ -255,6 +283,8 @@ export async function dropLoginCredentials(loginId: string): Promise<DroppedChat}export async function dropChatCredential(chatId: string): Promise<void> {- const bindings = await redis().hgetall<Record<string, unknown>>(fundingKey(chatId)) ?? {}- await redis().del(credentialKey(chatId), fundingKey(chatId), ...Object.keys(bindings).map(id => turnCredentialKey(chatId, Number(id))))+ const [trial, paid] = await redis().pipeline().hgetall(fundingKey(chatId)).hgetall(paidFundingKey(chatId)).exec() as [unknown, unknown]+ const bindings = fundingRecords(trial, paid)+ await redis().del(credentialKey(chatId), fundingKey(chatId), paidFundingKey(chatId),+ ...Object.keys(bindings).map(id => turnCredentialKey(chatId, Number(id))))}diff --git a/tests/fixtures/memory-redis.mjs b/tests/fixtures/memory-redis.mjsindex 4379d48..0bbc6d7 100644--- a/tests/fixtures/memory-redis.mjs+++ b/tests/fixtures/memory-redis.mjs@@ -12,6 +12,7 @@ export function memoryRedis() {hset(key, entries) { hashes.set(key, { ...(hashes.get(key) ?? {}), ...entries }); return 1 },hget(key, field) { return hashes.get(key)?.[field] ?? null },hlen(key) { return Object.keys(hashes.get(key) ?? {}).length },+ hkeys(key) { return Object.keys(hashes.get(key) ?? {}) },hgetall(key) { return hashes.get(key) ?? null },sadd(key, member) { sets.set(key, [...new Set([...(sets.get(key) ?? []), member])]); return 1 },smembers(key) { return sets.get(key) ?? [] },diff --git a/tests/turn-funding.test.mjs b/tests/turn-funding.test.mjsindex 771c4e0..d1b8e5d 100644--- a/tests/turn-funding.test.mjs+++ b/tests/turn-funding.test.mjs@@ -75,7 +75,7 @@ test('no later message or renewal changes who pays for an earlier turn', async (}})-test('a chat from before per-turn funding bills its latest credential until its first pinned message', async (t) => {+test('a turn from before per-turn funding bills as it did then, and a later turn with no record bills nothing', async (t) => {useSecret(t)const credentials = await turnCredentialWith(t, memoryRedis())// Before this change a paid message stored only the chat's latest credential.@@ -84,11 +84,44 @@ test('a chat from before per-turn funding bills its latest credential until itsassert.deepEqual(await billed(credentials, undefined), ['laptop', 'team_a', 'laptop-team_a'])await credentials.saveTurnCredential('chat', session('phone', 'team_b'), 5)- assert.equal(await billed(credentials, 4), 'unbound-turn', 'the old turn cannot bill the new sender')+ // Turn 4 was running or queued when the deploy landed: it bills the chat's+ // latest credential, as it would have before, so a follow-up never cuts it off.+ assert.deepEqual(await billed(credentials, 4), ['phone', 'team_b', 'phone-team_b'], 'the pre-deploy turn keeps running')+ assert.equal(await billed(credentials, 6), 'unbound-turn', 'every message since is pinned, so a later turn with no record bills nothing')assert.equal(await billed(credentials, undefined), 'no-turn')assert.deepEqual(await billed(credentials, 5), ['phone', 'team_b', 'phone-team_b'])})+test('paid funding records stay out of the record the code before them reads, so a rollback bills as it did', async (t) => {+ useSecret(t)+ const store = memoryRedis()+ const credentials = await turnCredentialWith(t, store)+ await credentials.saveTurnCredential('chat', session('laptop', 'team_a'), 1)+ await credentials.saveTurnCredential('chat', session('phone', 'team_b'), 2)+ // The code before per-turn funding refuses a chat's unpinned paid turns once+ // `funding:` holds any record, and it pins no paid message.+ assert.equal(store.hashes.has('test:funding:chat'), false)+ assert.deepEqual(Object.keys(store.hashes.get('test:paid-funding:chat')), ['1', '2'])+ // Sponsored turns stay where that code reads them.+ const previous = { key: process.env.AI_GATEWAY_API_KEY, enabled: process.env.FX0_FREE_PROMPTS }+ Object.assign(process.env, { AI_GATEWAY_API_KEY: 'synthetic-server-key-never-store', FX0_FREE_PROMPTS: '1' })+ t.after(() => {+ for (const [name, value] of Object.entries({ AI_GATEWAY_API_KEY: previous.key, FX0_FREE_PROMPTS: previous.enabled })) {+ if (value === undefined) delete process.env[name]; else process.env[name] = value+ }+ })+ await credentials.saveTrialCredential('chat', { kind: 'trial', userId: 'user-a', loginId: 'laptop', expiresAt: Date.now() + HOUR }, 3)+ assert.deepEqual(Object.keys(store.hashes.get('test:funding:chat')), ['3'])+ // A paid record that code wrote in `funding:` (a trial chat's) still bills its turn.+ store.hashes.set('test:funding:chat', { ...store.hashes.get('test:funding:chat'), 1: store.hashes.get('test:paid-funding:chat')[1] })+ delete store.hashes.get('test:paid-funding:chat')[1]+ assert.deepEqual(await billed(credentials, 1), ['laptop', 'team_a', 'laptop-team_a'])+ assert.deepEqual(await billed(credentials, 2), ['phone', 'team_b', 'phone-team_b'])+ // Deleting the chat removes both kinds of record and every turn credential.+ await credentials.dropChatCredential('chat')+ assert.equal(store.hashes.size + store.values.size, 0)+})+test('a paid message whose credential cannot be stored is not admitted', async (t) => {useSecret(t)const store = memoryRedis()@@ -107,7 +140,7 @@ test('sign-out drops the departing login\'s turn credentials and keeps every funassert.deepEqual(await credentials.dropLoginCredentials('laptop'), [{ chatId: 'chat', turns: 1, latest: 'not-this-login' }])assert.equal(await billed(credentials, 1), 'unusable', 'no fallback to the latest credential')assert.deepEqual(await billed(credentials, 2), ['phone', 'team_b', 'phone-team_b'])- assert.deepEqual(Object.keys(store.hashes.get('test:funding:chat')), ['1', '2'])+ assert.deepEqual(Object.keys(store.hashes.get('test:paid-funding:chat')), ['1', '2'])assert.deepEqual(await credentials.dropLoginCredentials('laptop'), [], 'a repeated sign-out finds nothing')assert.deepEqual(await credentials.dropLoginCredentials('phone'), [{ chatId: 'chat', turns: 1, latest: 'deleted' }])assert.equal(await billed(credentials, 2), 'unusable')@@ -161,11 +194,13 @@ test('resolution bills a turn\'s recorded identity and team, or nothing', asyncconst trial = (loginId) => ({ kind: 'trial', userId: 'user-a', loginId, expiresAt: Date.now() + HOUR })const credentials = [null, trial('laptop'), trial('phone'), user('laptop', 'team_a'), user('laptop', 'team_b'), user('phone', 'team_a')]const bindings = [null, { kind: 'trial', userId: 'user-a', loginId: 'laptop' }, { kind: 'user', userId: 'user-a', loginId: 'laptop', teamId: 'team_a' }]- for (const records of [0, 1]) for (const turnId of [null, 7]) for (const binding of bindings) {+ for (const records of [0, 1]) for (const turnId of [null, 7]) for (const binding of bindings) for (const firstFunded of [null, 5, 9]) {for (const exact of credentials) for (const latest of credentials) {- const where = JSON.stringify({ records, turnId, binding, exact, latest })- const funding = resolveFunding(records, turnId, binding, exact, latest)- if (records === 0) {+ const where = JSON.stringify({ records, turnId, binding, firstFunded, exact, latest })+ const funding = resolveFunding(records, turnId, binding, exact, latest, firstFunded)+ // A chat with no records, or a turn with none that is older than the chat's+ // first, bills the latest user credential.+ if (records === 0 || (turnId !== null && !binding && firstFunded !== null && turnId < firstFunded)) {assert.equal(funding.ok, !!latest && !('kind' in latest), where)if (funding.ok) assert.equal(funding.credential, latest, where)continue