|
| 1 | +/** |
| 2 | + * Per-OS rollout switches for PAID agentic serving on Desktop. |
| 3 | + * |
| 4 | + * macOS has served paid generic offers since COD-568 with no switch of its |
| 5 | + * own. Every other Desktop OS reaches the same paid path only behind ONE |
| 6 | + * switch per OS, with exactly the same rules as macOS once it is on (Owen, |
| 7 | + * 2026-09-24: every agentic campaign follows the same rules on every OS, with |
| 8 | + * no special paths and no per-campaign lists): |
| 9 | + * |
| 10 | + * - Windows (COD-642): `FREEBUFF_SPONSORED_WINDOWS`, surface `desktop_windows`, |
| 11 | + * contained by the portable floor (`./sponsored-windows`). |
| 12 | + * - Linux (COD-655): `FREEBUFF_SPONSORED_LINUX`, surface `desktop_linux`, |
| 13 | + * contained by bubblewrap — a missing `bwrap` makes the client report no |
| 14 | + * execution capability, so it is never offered a paid task. |
| 15 | + * |
| 16 | + * One table, one reader, one capability predicate, so every consumer (Next |
| 17 | + * serving, the funded Accept route, the Convex reservation and Accept |
| 18 | + * contracts) applies the same rule to every switched OS. Only the EXACT value |
| 19 | + * `on` opens a switch. Unset, `off`, `ON`, ` on `, a typo: closed. A present |
| 20 | + * value that is neither `on` nor `off` is reported through `onUnrecognised` |
| 21 | + * so an operator's typo does not look like a working switch. |
| 22 | + */ |
| 23 | + |
| 24 | +import type { SponsoredExecutionSurface } from './sponsored-capability' |
| 25 | + |
| 26 | +/** Each switched Desktop OS: the env knob that opens it and the surface it runs on. */ |
| 27 | +export const SPONSORED_OS_SWITCHES = { |
| 28 | + windows: { |
| 29 | + env: 'FREEBUFF_SPONSORED_WINDOWS', |
| 30 | + surface: 'desktop_windows', |
| 31 | + }, |
| 32 | + linux: { |
| 33 | + env: 'FREEBUFF_SPONSORED_LINUX', |
| 34 | + surface: 'desktop_linux', |
| 35 | + }, |
| 36 | +} as const satisfies Record< |
| 37 | + string, |
| 38 | + { env: `FREEBUFF_SPONSORED_${string}`; surface: SponsoredExecutionSurface } |
| 39 | +> |
| 40 | + |
| 41 | +/** A Desktop OS whose paid agentic serving sits behind its own switch. */ |
| 42 | +export type SponsoredSwitchedOs = keyof typeof SPONSORED_OS_SWITCHES |
| 43 | +export type SponsoredSwitchedOsSurface = |
| 44 | + (typeof SPONSORED_OS_SWITCHES)[SponsoredSwitchedOs]['surface'] |
| 45 | + |
| 46 | +/** The env vars the switches are read from. */ |
| 47 | +export type SponsoredOsSwitchEnv = { |
| 48 | + [Os in SponsoredSwitchedOs as (typeof SPONSORED_OS_SWITCHES)[Os]['env']]?: string |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Non-null only when an OS's switch is exactly `on`. Carries no campaign list: |
| 53 | + * with the switch on, that OS serves every agentic campaign. Kept as a value |
| 54 | + * rather than a boolean so a caller cannot confuse "the switch" with "this |
| 55 | + * request is on that OS". |
| 56 | + */ |
| 57 | +export type SponsoredOsPolicy = Readonly<{ switch: 'on' }> |
| 58 | + |
| 59 | +/** The only policy there is: the switch, on. */ |
| 60 | +export const SPONSORED_OS_ON: SponsoredOsPolicy = Object.freeze({ |
| 61 | + switch: 'on', |
| 62 | +}) |
| 63 | + |
| 64 | +/** |
| 65 | + * How a raw switch value reads. `off` is unset, empty, or exactly `off`; `on` |
| 66 | + * is exactly `on`; anything else (`ON`, ` on `, `true`, a typo) is |
| 67 | + * `unrecognised`, which is CLOSED and worth a log line. |
| 68 | + */ |
| 69 | +export function sponsoredOsSwitchState( |
| 70 | + raw: string | undefined, |
| 71 | +): 'on' | 'off' | 'unrecognised' { |
| 72 | + if (raw === undefined || raw === '' || raw === 'off') return 'off' |
| 73 | + return raw === 'on' ? 'on' : 'unrecognised' |
| 74 | +} |
| 75 | + |
| 76 | +/** Server configuration only. Anything but the exact value `on` admits nothing. */ |
| 77 | +export function readSponsoredOsPolicy( |
| 78 | + os: SponsoredSwitchedOs, |
| 79 | + env: SponsoredOsSwitchEnv, |
| 80 | + options: { onUnrecognised?: (value: string) => void } = {}, |
| 81 | +): SponsoredOsPolicy | null { |
| 82 | + const raw = env[SPONSORED_OS_SWITCHES[os].env] |
| 83 | + const state = sponsoredOsSwitchState(raw) |
| 84 | + if (state === 'unrecognised' && raw !== undefined) |
| 85 | + options.onUnrecognised?.(raw) |
| 86 | + return state === 'on' ? SPONSORED_OS_ON : null |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * A reporter for `onUnrecognised` that says so ONCE per value per interval |
| 91 | + * (default ten minutes) instead of on every request. Each runtime owns one |
| 92 | + * per switch (module scope), and passes its own logger. The value is bounded |
| 93 | + * and JSON-quoted before it reaches the log, so whitespace is visible. |
| 94 | + */ |
| 95 | +export function throttledUnrecognisedSwitchReporter( |
| 96 | + report: (quotedValue: string) => void, |
| 97 | + options: { intervalMs?: number; now?: () => number } = {}, |
| 98 | +): (value: string) => void { |
| 99 | + const intervalMs = options.intervalMs ?? 10 * 60_000 |
| 100 | + const now = options.now ?? Date.now |
| 101 | + let last: { value: string; at: number } | null = null |
| 102 | + return (value) => { |
| 103 | + const at = now() |
| 104 | + if (last && last.value === value && at - last.at < intervalMs) return |
| 105 | + last = { value, at } |
| 106 | + report(JSON.stringify(value.slice(0, 32))) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/** The switched OS a request reports, or null for macOS, unknown and absent. */ |
| 111 | +export function sponsoredSwitchedOs( |
| 112 | + reportedOs: string | null | undefined, |
| 113 | +): SponsoredSwitchedOs | null { |
| 114 | + return reportedOs === 'windows' || reportedOs === 'linux' ? reportedOs : null |
| 115 | +} |
| 116 | + |
| 117 | +/** The switched OS a row's execution surface belongs to, or null (macOS, CLI, unrecorded). */ |
| 118 | +export function sponsoredSwitchedOsForSurface( |
| 119 | + surface: string | null | undefined, |
| 120 | +): SponsoredSwitchedOs | null { |
| 121 | + if (surface === SPONSORED_OS_SWITCHES.windows.surface) return 'windows' |
| 122 | + if (surface === SPONSORED_OS_SWITCHES.linux.surface) return 'linux' |
| 123 | + return null |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Whether a request carries the proof that its client can RUN a paid |
| 128 | + * sponsored procedure on this OS: a v2 `sponsoredCapability` that names the |
| 129 | + * OS's own Desktop surface and reports execution `available` with no reason. |
| 130 | + * On Linux that is only reported when bubblewrap is present; on Windows it is |
| 131 | + * only sent by builds that carry the floor. |
| 132 | + */ |
| 133 | +export function sponsoredOsExecutionCapable( |
| 134 | + os: SponsoredSwitchedOs, |
| 135 | + capability: |
| 136 | + | { |
| 137 | + execution: { |
| 138 | + surface: string |
| 139 | + status: string |
| 140 | + reason?: string | undefined |
| 141 | + } |
| 142 | + } |
| 143 | + | null |
| 144 | + | undefined, |
| 145 | +): boolean { |
| 146 | + return ( |
| 147 | + capability?.execution.surface === SPONSORED_OS_SWITCHES[os].surface && |
| 148 | + capability.execution.status === 'available' && |
| 149 | + capability.execution.reason === undefined |
| 150 | + ) |
| 151 | +} |
0 commit comments