Compare commits

...
Author SHA1 Message Date
jelveh 1f75ee51e0 Test native app hosts with apps still served natively
editor, markus and pdf moved to standard host routing, but the host
redirect and embed framing tests still used editor as their native app,
so they failed. They now use about, and a test checks that editor,
markus and pdf are passed through even with a folder under the native
apps root.
2026-11-07 01:27:03 -07:00
Nariman Jelveh cb214f28cc Open Contact Us as a dashboard dialog in the dashboard (#4533)
The dashboard's Contact Us now uses the dashboard form dialog instead of a
UIWindow: a centered card on desktop and a bottom sheet on phones. Files can
be picked, dropped anywhere over the dialog, or pasted into the message, and
images show a preview. Cmd/Ctrl+Enter sends. The desktop keeps
UIWindowFeedback.
2026-11-07 00:40:45 -07:00
Nariman Jelveh 8ed439e897 Stop the signup screen redrawing when it opens (#4532)
The signup screen showed its email form, then looked up the identity
providers and folded the form away behind "Sign up with email" once they
arrived, a round trip later. Every screen made its own lookup, so moving
from login to signup redrew the card each time.

The provider lookup now happens once per auth flow, so moving between
login and signup draws the providers before the next paint. When signup
is the first screen, its email form stays hidden until the lookup
answers, or for 1.5 seconds if it is slow, and shows when no providers
are offered or the form was opened prefilled.
2026-11-06 23:58:44 -07:00
jelveh 672a704193 Shorten popup Puter branding message
Updates the English translation for `popup_opener_uses_puter` to a shorter single-line message. This removes the extra explanatory text about AI/cloud features and using a Puter account, likely for a cleaner popup prompt.
2026-11-06 23:41:00 -07:00
jelveh 35ae464d86 Tighten signup terms typography
Reduce the signup terms text in the auth dialog to match the tighter dashboard layout. This is a small styling adjustment for the dialog copy to keep it balanced and readable.
2026-11-06 23:37:37 -07:00
jelveh 79f24ed3df Hide the opener notice on the popup login screen
Someone logging in already has a Puter account, so like the account
picker the login screen no longer explains that the site uses Puter.
The signup screen keeps it.
2026-11-06 23:31:49 -07:00
Nariman Jelveh f30d2134b1 Keep profile pictures in the saved account list (#4531)
The account picker reads pictures from the logged_in_users list in
localStorage, but update_auth_data saved that list with an empty profile
and only added the picture in memory once the profile loaded. A picture
reached storage only when a later account switch or sign-out rewrote the
list, so the active account usually showed just its initial.

update_auth_data now keeps the account's saved profile until the fresh
one loads, then saves the user and the account list again. The profile
loads after puter.js holds the new account's token, so a switch can no
longer fetch the previous account's profile, and a profile that arrives
after another account signed in is dropped.

The first-visit migration in globals.js now stores the account list as
JSON instead of "[object Object]".
2026-11-06 23:22:11 -07:00
Nariman Jelveh c24c8589cb Hide the opener notice on the popup account picker (#4530)
Someone choosing from accounts they've already signed in with doesn't need
to be told the site uses Puter. The login and signup screens keep it.
2026-11-06 23:02:18 -07:00
Nariman Jelveh 2f8f81e621 Bind the app browser embed to the host's user and add <puter-user-menu> (#4528)
* Show the app browser embed's session only to the host's signed-in user

/embed/apps now reads the host page's signed-in user from its fragment
(#user=<uuid>) and asks /whoami which user the GUI session belongs to.
It shows the user's apps only when the two match. A host that names no
user, a malformed one, or another user, and a session the API refuses,
get the signed-out view of recommended apps with no SDK and no app
tokens. A failed /whoami check shows the unavailable state. Changing the
fragment reloads the embed.

Hosts pass the uuid from puter.auth.getUser() and remove the iframe when
their own sign-in changes; the doc's example does both.

* Add <puter-user-menu> for first-party apps on Puter subdomains

puter.js registers a <puter-user-menu> element that follows the page's
own puter.js sign-in. Signed out it shows Sign in; signed in it shows the
user's profile picture, or their initial when there is none, with a menu
holding the username and Sign out. On a page one label under the Puter
origin, an Apps button opens /embed/apps in a modal with the signed-in
user named in the fragment, so the app browser shows the Puter session's
apps only to that user. Any change to the page's sign-in closes the app
browser. The element renders nothing outside a top-level page.

Its labels can be set with apps-label, sign-in-label, sign-out-label,
close-label and account-label, and it fires signin and signout events.
It is documented internally in components.md and the embed doc, and is
left out of the public docs because the app browser only works on Puter
subdomains.

* Show <puter-user-menu>'s app browser in a panel under its button

The Apps view and the account menu are now panels that hang under their
button with a caret, closing on Escape, an outside press or the button.
They use the top layer where the browser has one, so a host's transform,
overflow or z-index can't clip or cover them. On screens up to 600px wide
the Apps panel spans the width below the button.

Closing the Apps panel keeps the app browser loaded; a change to the
page's sign-in still discards it. A spinner shows until it loads, and its
colour scheme follows the component's theme, which follows the OS unless
the theme attribute is set.

A temporary account counts as signed out: it gets Sign in, no profile
picture request, and an app browser with no user named. The signin event
carries the signed-in user, and the component exposes the parts
apps-button, avatar, sign-in, menu and apps-panel for host styling.

* Read the user <puter-user-menu> signs in as after the SDK starts its whoami

setAuthToken notifies auth listeners before it starts the new token's
/whoami, so a sign-in on a loaded page showed '?' and opened the app
browser without naming the user.

* Close <puter-user-menu>'s account menu when Tab leaves it

Focus moved on to the page with the menu still open, where Escape no
longer reached it. Tab now closes the menu and moves on from its button.

* Move focus to Sign in after signing out from <puter-user-menu>

The focused menu and avatar disappear on sign-out, which dropped focus
to the page body.

* Show the app browser in the theme <puter-user-menu> forces

Chromium doesn't pass an iframe's color-scheme on to the page inside it,
so a forced theme that differed from the OS showed the app browser in
the OS's theme inside a panel in the forced one. The component now
passes a forced theme as theme= in the fragment, and the embed applies
it to its color-scheme rules.

* Show <puter-user-menu>'s account before its profile picture loads

The avatar and Apps button waited for the /profile request as well as
the user, so a slow profile kept them hidden. The picture is now added
when it arrives.

* Open one sign-in popup at a time from <puter-user-menu>

Clicking Sign in again while its popup is still open opened another popup each time. Further clicks are ignored until the sign-in finishes or its popup is closed. A cross-origin isolated page can't see its popup close, so it keeps the old behavior rather than locking the button.

* Keep <puter-user-menu>'s Apps button unshaded after a tap closes it

Touch screens keep :hover on the last tapped element, so tapping the Apps button to close the panel left it shaded as if the panel were still open. Hover shading now applies only where the device can hover; the open state still shades the button everywhere.

* Show <puter-user-menu>'s Sign in pressed and its account hovered

Sign in had no pressed state and the account button no hover state, so neither responded to the pointer. Sign in now takes the pressed look of Puter's other primary buttons, and the account button shows its open-menu ring on hover where the device can hover.

* Show a spinner in the app browser embed until its apps arrive

The embed stayed blank while it checked the session with /whoami and loaded the SDK. <puter-user-menu> hides its own spinner once the embed's page loads, so its panel went blank for that whole time. The empty embed now shows the same spinner in the same place until it renders its apps or its unavailable message.
2026-11-06 22:18:38 -07:00
Nariman Jelveh 7d02c7f622 Offer a custom domain when publishing a folder from the dashboard (#4527)
* Offer a custom domain when publishing a folder from the dashboard

The Files tab's Publish Website dialog has a "Use a custom domain" switch.
It appears only when the server has custom domains. Turned on, it asks for
the domain, with the monthly price and suggestions from the domains already
on the account. Picking one of those moves it to the new site instead of
adding it again.

Publishing creates the site, then adds the domain. The done screen shows the
live address and the domain's status with the DNS records to set, each with
a Copy button, and a Check Status button. A failure after the site is live
keeps the site: the address locks, and the button retries only the domain.
A domain held by another account shows its ownership record to verify.

On a plan without custom domains, the switch shows an upgrade prompt in
place of the field and opens the upgrade flow where the deployment has one.
Once the plan changes, the field unlocks in place.

The /custom-domains request, error wording, price format and status-check
merge move to helpers/customDomains.js, shared with the desktop's attach
dialog.

* Name the served and forwarding domains the right way round in the redirect note

The note reads "served on {first}, forwarded from {second}", but both the
publish dialog and the attach dialog passed the root domain first.

* Fit DNS record labels of any length in the publish dialog

The label column was a fixed 44px, so a longer translation ("Pangalan")
ran under the field beside it. Each record card is now one grid whose label
column grows to its longest label, keeping the name and value aligned.

* Run one domain action at a time on the publish done screen

Check Status and the switch to www each disabled only themselves, so both
could be in flight and a slow status check could redraw the root domain
over the www one it had just become.

* Keep focus on the custom domain switch when it shows the upgrade prompt

Turning the switch on moved focus to Upgrade, so a second Space meant to
turn it back off opened the upgrade flow instead. The prompt follows the
switch in tab order.

* Finish with the live site when the plan can't add its domain

Once the site was live, a 402 from adding the domain left the button
reading Add Domain, and pressing it said to turn the switch off "to publish
without one". The button now reads Done and finishes with the site; an
upgrade brings the add back.

* Keep the ownership record and the Verify step in step while typing

Any edit hid the claim's TXT record for good, but the button went back to
Verify and Add as soon as the claimed hostname was typed again, asking to
verify a record no longer on screen. Both now follow the typed hostname.

* Keep the publish done screen up through a stray backdrop click when it lists DNS records

The records to set at the DNS provider appear only on this screen; a click
outside the card while switching to the provider's tab threw them away.
Done, Escape and the close button still close it.

* Listen for plan changes only once the publish dialog has offered the upgrade

Every website publish dialog added a window listener that held its closed
dialog until the next plan change, which may never come. The dialog is
modal, so the plan can only change after its Upgrade button opened the flow;
the listener is added then.

* Keep the upgrade prompt's text beside its icon on phones

Wrapping put the text on its own line under the icon, leaving the icon
alone on the first row; now only the button wraps.

* Drop "After publishing" from the domain hint once the site is live

When adding the domain needed another try, the hint still promised DNS
records "after publishing" for a site already published; it now keeps
only the price.

* Announce publish domain checks in a status region that stays on screen

Each check redrew the domain section, status line included, so its result
landed in a brand-new live region that screen readers don't announce, while
the redrawn records sat in the done screen's status region. The summary is
now redrawn on its own, outside the announcements.
2026-11-06 20:06:30 -07:00
Nariman Jelveh d2bbe2b5e2 Ignore Explorer responses for a folder the window has left (#4526)
Each refresh of an item container now takes an id, and only the newest
refresh may write to the container or its window. Before, a slow stat for
the folder a window had left could point the window back at it, so New
folder, Paste and Upload went to the old folder while the window showed
the new one. A listing that arrived just before navigating away added its
rows to the new folder, and returning to a folder while its first listing
was in flight rendered every row twice. The stat in update_window_path
also skips its writes once the window has moved to another path.
2026-11-06 18:28:25 -07:00
Nariman Jelveh 3ff70ef97f Remove web apps from native host redirects
Allow the `editor`, `markus`, and `pdf` subdomains to use standard host routing.
2026-11-06 17:20:22 -07:00
Nariman Jelveh f791024b2e Support dark mode in the desktop context menu (#4525)
The desktop context menu (UIContextMenu) stayed white with dark text when
the OS was in dark mode, even though the dashboard around it turns dark.
It now follows prefers-color-scheme like the dashboard: a dark translucent
surface, light text, and dark variants for dividers, the open-submenu and
blurred highlights, the submenu chevron, monochrome icons, and the mobile
action sheet.
2026-11-06 16:01:08 -07:00
Nariman Jelveh cce9718b45 Stop the sign-in page jumping when the iOS keyboard opens
A sign-in page already fills the screen, so it no longer shrinks itself
to the area above an on-screen keyboard. On iOS that resize fought the
browser's own pan to the focused field and the form moved up and down
as the keyboard opened. Dialogs still fit above the keyboard.
2026-11-06 15:40:36 -07:00
Nariman Jelveh 9758ada36e Keep the wallpaper off sign-in pages and drop the OIDC-only signup notice
Signed out, /action/login, /action/signup, /action/password-recovery and
the sign-in page for returning visitors, app landings and share links no
longer load the wallpaper behind them, and the page covers the screen at
once instead of fading in over whatever was there.

The signup screen no longer explains that only providers are offered when
email signup is closed; it just shows the provider buttons.
2026-11-06 15:30:57 -07:00
Nariman Jelveh 37dcee78f0 Move login, signup, account picker, password recovery and 2FA to dashboard-style dialogs (#4522)
* Move login, signup, account picker, password recovery and 2FA to dashboard-style dialogs

The sign-in surfaces were separate UIWindows that opened over each other
(login, then a recovery window over it, then a 2FA window over that). They
are now screens of one dashboard-style card built on mountDashboardOverlay,
the same shell, fields, buttons and status line as the dashboard's account
dialogs. Screens replace each other in place, with Back, and keep what was
typed.

- Page presentation for cover pages and sign-in popups: an opaque page with
  a centered card, filling the screen on phones and in popups. Dialog
  presentation elsewhere: dimmed backdrop, centered card, bottom sheet on
  phones.
- Each call site keeps whether it could be dismissed, its promise contract,
  OIDC gating in popups, Turnstile, the honeypot, bonus codes, the
  post-signup verification gates, reauth auth_id, and redirect handling.
- Keys typed in the card no longer reach the desktop's document shortcuts.
- A second sign-in prompt joins the open flow instead of stacking a card.
- Server messages render as text; prefilled values are encoded.

* Show the signup screen's errors above the provider buttons

The status line lived inside the email form, which hides behind the
provider buttons (or for good, when only provider signups are open), so
an error the screen opened with, like a refused provider signup, was
never shown.

* Cover the page with a new account's checks when signup was a page

Signup as a page (a sign-in popup, /action/signup) closes its card for
the email, phone and card checks; they opened as floating windows over
an empty backdrop instead of covering the page as they did before.

* Give the auth card's Back and close bar a background once it scrolls

On a short screen the card's content scrolled under the bare Back and
close buttons, which then sat on top of it: the Back chevron covered the
edge of a provider button and took its taps. Once the showing screen is
scrolled, the bar turns opaque with a divider, as the footer has; at
rest nothing changes.

* Raise a failed session-cookie sync alert above the sign-in card

Signing in waits on this alert with the auth card still up. The card
sits above every ordinary window, so the alert opened underneath it and
the login spun forever. It now opens in the stay-on-top band.

* Keep focus where it is when a second sign-in prompt joins the card

A prompt that joins the open auth card focused the screen's first
field, so one arriving while the user typed their password sent the
rest of it into the username field. It now only brings focus back when
it has left the card.

* Keep focus in the auth card while its form is busy

Submitting disabled the focused field, which dropped focus to the page,
so keys pressed while the request was in flight (Delete, Enter, letters)
reached the desktop's document shortcuts.

* Keep focus in the auth card when its backdrop is pressed

A press on the dimmed backdrop (or a page's background) that didn't
dismiss the card moved focus to the page, so the next keys went to the
desktop's document shortcuts and Tab could leave the card.

* Point the Turnstile migration note at the new signup screen

UIWindowSignup no longer exists; the widget renders in UI/Auth/UIAuthSignup.js.

* Reload after sign-in when only the URL's hash would change

On a dashboard tab (/#files) the redirect after a login or signup was
the same page with a hash, so location.replace only scrolled: the new
account was signed in but the page never reloaded, and the card stayed
up, busy, until closed.

* Span the screen with the auth bottom sheet on wider phones

The card's 400px cap outranked the dashboard sheet's full width, so on
phones between 401 and 600px wide the sheet left gaps on both sides.

* Return focus to the auth form when a request fails

A busy card parks focus on itself while its fields are locked. When the
server refused a signup, focus stayed there, so Enter did nothing and the
user had to tab back in. Ending busy now hands focus back to the control
that had it, once that control is enabled again.

* Keep the current account when a login is dismissed mid-request

Closing the auth card while its login request was out still signed the
browser in to the other account and reloaded once the server answered,
though the caller had already been told the user dismissed it. A login
that answers after the card closed is now dropped.

* Make the auth card's logo a link where it leads somewhere

On a cover page the Puter logo was an image with a click handler: no
keyboard access, no name for screen readers, no middle-click. It is now a
link named Puter. Everywhere else the logo is marked decorative, since the
title beside it names the screen.

* Return to the login screen when signing in fails after a 2FA code

If storing the new session failed after a verified code, the error went
to the hidden login screen while the code screen stayed up, busy, with
its boxes locked. The card now goes back to the login screen first.

* Hand the session cookie back when a login is dismissed mid-request

The server sets the session cookie as soon as it accepts a login or a
second-factor code. Dropping an answer that arrived after the card closed
kept the page on its account while the cookie had moved to the other one,
so cookie-gated actions (password, 2FA) applied to that account and the
next load picked up its token. The cookie is now synced back to the
account the page is signed in as.

* Keep padded notices inside the auth page's column

On a phone-width page or in a sign-in popup, every direct child of the
auth body is stretched to the column's width. The overlay sits outside
.dashboard, so it isn't border-box, and the padded error line and bonus
notice came out wider than the fields and scrolled the page sideways.

* Keep the recovery-code field one size when it takes focus

On phones the generic dialog-input and phone input rules outranked the
recovery-code field's style until it was focused, so tapping it grew the
text from 16-17px to 20px and shifted the screen. The field's rule now
carries the same weight as theirs, like the code boxes beside it.

* Show the provider buttons' hover in dark mode

The hover shade was the sidebar background, which in dark mode is one
step off the card's, so Google, Apple and the other sign-in buttons gave
no feedback under the pointer. Dark mode now uses the hover shade.

* Theme the signup bonus notice with the auth card

The notice kept the old signup window's fixed pastel colors, which stood
out as a light box on the card in dark mode. It now uses the dashboard's
success and danger colors, which follow the color scheme.

* Keep a new account's email check from closing without an answer

A signup that needs its email confirmed takes the auth card down and
waits on the confirmation window, whose ✕ closed it without ever
answering. In a sign-in popup reached from the account picker that left
an empty popup with nothing to pick. The check now stays until the code
is confirmed, like the phone and card checks and the boot-time email
check.
2026-11-06 15:20:08 -07:00
Daniel Salazar 162398a827 test: fix coverage line mapping, puter.js coverage OOM, and flaky backend tests (#4523)
- lowerDecoratorsPlugin dropped esbuild's source map, so coverage and
  stack traces reported transformed lines. Pass the external map through.
- puter.js node runner kept a ~6 MB `__coverage__` per vm context and ran
  out of heap. Contexts now share one object; workerd merges as it goes.
  report-coverage skips its comparison when the PR leg failed.
- CI-only test/hook timeouts of 20 s / 60 s.
- setupTestServer disables the database-liveness health check, whose
  `SELECT 1` landed in query-counting tests.
- LocalWorkerService.shutdown asserts the timer shutdown arms instead of
  the global fake-timer count.
- userHandlers worker test waits for posts by label.
- AppStore stats test pins the clock so rows don't cross midnight UTC.
- Realtime backpressure test waits up to 5 s for holds to settle.
2026-11-06 12:30:36 -07:00
jelveh 6785b4d9cc Reorder recommended apps and drop terminal
Updates `RECOMMENDED_APP_NAMES` to prioritize core productivity apps (`word-processor`, `spreadsheet`, `presentation`, `whiteboard`, `pdf-editor`) immediately after `builder`, and removes `terminal` from the recommended list.
2026-11-06 09:05:33 -07:00
Nariman Jelveh d2acdafaed Show recommended apps in the app browser embed without a session (#4517)
* Show recommended apps in the app browser embed without a session

/embed/apps only worked for a signed-in user; without a session it showed
an unavailable state. The route now puts the recommended apps
(RecommendedAppsService) in the page's config as guestApps, and when the
embed finds no session it shows those, without loading puter.js or reading
installed apps, folders, or saved order. Selecting one opens it at the
same URL a signed-in launch uses, with no opener and no token on offer, so
the app signs in on its own; apps that need the desktop open /app/<name>.
Signed-in behavior is unchanged.

createAppBrowser takes a guestApps option for this, and appTabLaunch's
URL building is shared between launchAppInTab and the new
guestAppLaunchUrl.

* Show the app browser embed's error state when the session lookup fails

Only a 401 from /get-gui-token now means there is no session. A rate
limit or server error shows the unavailable state, as before, instead of
the signed-out list of recommended apps.

* Let the app browser embed find no session when storage is refused

Reading local storage can throw when the browser blocks site data. The
embed now falls back to the session cookie, as it does for empty
storage, so a visitor without a session sees the recommended apps
instead of the unavailable state.

* Fix the app browser embed doc's broken sentence about puterOrigin
2026-11-06 07:48:11 -07:00
Reynaldi Chernando 6a46363d6d reorder api sections (#4519) 2026-11-06 17:30:22 +07:00
Reynaldi Chernando 2de9d509e8 docs layout cleanup (#4518) 2026-11-06 16:22:48 +07:00
Nariman Jelveh 763742dbc0 fix(embed): read the app browser's session from the cookie when its storage is empty (#4516)
Safari keys a framed origin's local storage by the top-level origin, so the
app browser embedded on a sibling subdomain finds no auth_token_v2 and shows
"Apps are unavailable". The session cookie is still sent there, so the embed
now falls back to /get-gui-token and keeps the token in memory.
2026-11-05 23:41:38 -07:00
Daniel Salazar 7f0cd1689b fix(embed): only sibling subdomains not served by core can frame the app browser (#4515)
/embed/apps now names the referring origin in frame-ancestors only when it
is exactly one label under the domain and that label is not one the server
answers itself (api, dav, js, www, native-app hosts) or the host of
api_base_url. Nested subdomains are refused too.

The query-string redirect now checks the raw URL instead of the parsed
query, so every request carrying a query string is redirected.
2026-11-05 23:10:34 -07:00
Daniel Salazar 9d22f39928 Ask for notification permission from Puter's dialog when the click has expired (#4514)
On a website, puter.notifications.enable() threw user_gesture_required
without prompting once the calling click's activation was gone: after
puter.auth.signIn() or requestPermission() opened a popup in the same
click, after a few seconds of awaited work, or when called on load. Apps
that ignored the error showed nothing.

It now shows Puter's consent dialog in that case and asks the browser
from its Continue click; Cancel resolves false. Calls made while an ask
is open share it. PuterDialog gets an onContinue option and a show()
that launch() also uses. Inside Puter's own desktop page the call still
throws, since it always runs from a click.

The push guide also notes that Chrome stops showing the prompt after
three dismissals and that its quieter messaging shows a bell icon.
2026-11-05 23:10:06 -07:00
Nariman Jelveh ef185c04ca Open apps from the dashboard's Home tab at their own URL (#4513)
Recently used apps on the Home tab opened /app/<name> in a new tab, which
loads the desktop and launches the app from there. They now open the way
Apps tab tiles do: a new tab at the app's index_url through launch_app's
newTab, or in this page when the browser refuses the tab. The Apps tab's
open-window lookup moves to helpers/focusAppWindow.js so both share it,
and the shared launch lives in helpers/dashboardAppLaunch.js, outside the
app browser so the embed bundle doesn't pull in launch_app.
2026-11-05 21:30:52 -07:00
Nariman Jelveh dec7d1fd96 Load the app browser embed's script from beside the GUI bundle (#4511)
The embed page always loaded /dist/apps-embed.min.js from the server,
while the GUI bundle and the embed build are published to gui_bundle's
location (a CDN in production). The server's /dist copy can be stale, so
the embed ran an old build. It now loads apps-embed.min.js from
gui_bundle's directory, keeping /dist when gui_bundle isn't set.
2026-11-05 21:07:05 -07:00
Nariman Jelveh fb9e7d70c6 Open apps from the embedded app browser at their own URL (#4510)
* Open apps from the embedded app browser at their own URL

Selecting an app in the /embed/apps browser now opens it in a new tab at
its index_url, with the same launch params the dashboard's tab launch
uses, instead of the GUI's /app/<name> landing. The tab is opened inside
the click and without noopener; the app's SDK asks the embed for its
token through the opener, so the token stays out of the URL.

Apps that need the desktop (built-in and godmode apps, private apps the
user can't open, or a failed token request) still open /app/<name>, with
the tab's opener cleared. Website shortcuts are unchanged.

The app URL, the app token request and the /rao report move out of
launch_app into helpers without desktop globals (appLaunchUrl.js,
appLaunchApi.js), so launch_app and the embed build the same URL.
uuidv4 moves to a helper the embed can import.

* Tag app launches with utm_medium and a utm_campaign naming the launch surface

Apps now get utm_medium=referral and utm_campaign=desktop, dashboard, or
app_browser_embed alongside utm_source=puter.com, so their analytics can
tell where in Puter they were opened from.
2026-11-05 20:31:30 -07:00
Nariman Jelveh 819e77bb4c Open dashboard apps and files in a new tab at the app's index_url (#4508)
* Open dashboard app tiles in a new tab at the app's index_url

Clicking an app tile in the dashboard now opens the app in a new browser
tab at its index_url, with the same query params (puter.app_instance_id,
puter.app.*, puter.auth.token, puter.domain, puter.api_origin,
puter.locale, puter.args, ...) its in-page iframe gets.

launch_app takes a newTab option: it runs the normal launch (private
access, file signing, token) and sends the caller's tab to the built URL
instead of opening a window. The tile click opens the blank tab inside
the click so popup blockers allow it, and closes it if the launch fails.

* Hand an app tab its token through the opener, not the URL

An app opened in its own tab no longer gets puter.auth.token in its URL,
which kept the token in browser history, and which puter.js ignored anyway
outside an iframe.

The GUI now holds the token for the tab it opened. Once the app loads,
puter.js (standalone, with puter.app_instance_id in its URL and an opener)
asks its opener for it at the configured GUI origin, takes only a reply
from that window at that origin, and then clears window.opener. The GUI
answers once, only to that tab at the origin it sent the tab to, and
drops the offer after two minutes. Sign-in and implicit auth wait for the
handoff, then fall back to the normal sign-in flow when no token came
(opener closed, slow load, redirect to another origin).

Built-in apps join explorer in opening in-page, since they belong to the
desktop.

* Open files from the dashboard Files tab in a new tab

Opening a file in the Files tab (double-click, Enter, tap, or the Open
menu item) now opens its app in a new browser tab, the same way app
tiles do. The tab is opened inside the gesture and handed to open_item,
which passes it to launch_app or closes it when the file opens no app.
Web links reuse the tab, cut off from the dashboard. A second file of a
multi-select Enter, whose tab the popup blocker refuses, opens in-page.

The opened file's signed read/write URLs don't expire, so a tab gets
them through the opener handoff with its token instead of in its URL;
the URL keeps only puter.item.uid as a marker. puter.js delivers the
handed-over item to onLaunchedWithItems / onItemsOpened, and
wasLaunchedWithItems answers true while it's on its way.

launch_app now closes a tab it was given whenever the launch fails, so
callers don't each have to.

* Open "Open With" apps from the Files tab in a new tab

Picking an app under Open With in the dashboard Files tab now opens it
in a new browser tab, like opening the row. The tab is opened in the
menu click, or, when the "always open with" prompt comes first, right
after it's answered, since that click counts as a fresh user gesture.
If the popup blocker refuses the tab, the app opens in-page. The desktop
keeps opening in-page.

* Wait for a GUI-opened tab's token in getUser, whoami and getProfile

A tab the GUI opens for an app gets its token over postMessage moments
after puter.js loads. These calls short-circuited on a missing token, so
an app reading the user at startup failed with a 401 (or a null profile)
even though the token was on its way. They now wait for the handoff
first, the same way sign-in already does.

* Close an app's blank tab before alerting that its launch failed

A launch denied for a private app, or one whose token couldn't be
fetched, awaited its alert while the blank tab it was given stayed in
front. The alert renders on the dashboard behind it, so the user was
left on an empty tab until they went looking. The tab is now closed
before the alert shows.

* Launch an app tile in-page when the browser refuses its tab

When window.open returned null (a popup-blocking setting, or a context
that can't open tabs), clicking an app tile did nothing at all. It now
falls back to the in-page launch it had before, as opening a file from
the Files tab and Open With already do: an open window is surfaced,
otherwise the app opens maximized with the tile's open morph.

* Open no tab for a file opened from the Trash

Opening a file in the Trash only explains that it can't be opened, but
the Files tab still opened a blank tab for it first, which then closed
again: a flash of a new tab before the alert. Trashed files (by the
path a shortcut resolves to) now get no tab.

* Don't count a tab launch as one that can report interactivity

launch.ipc_tracked on app-is-ready is the denominator for the
app-interactive series: launches that could produce one. An app opened
in a new tab never signals over IPC, yet was recorded as tracked, so
every tab launch counted as an interactive span that never came. Tab
launches are now recorded as untracked.

* Leave openerLaunch_ unset when there is no GUI opener to ask

requestLaunchFromOpener resolved null asynchronously even when there was
no opener at all, so openerLaunch_ was briefly set on every boot with an
app instance id in the URL. A reloaded app tab (opener already cut off,
URL still marking the opened item with puter.item.uid) reported
wasLaunchedWithItems() as true to code running in the same task as the
boot, though no item would ever arrive. With no opener to ask it now
returns null synchronously and nothing is left pending.

* Call an app tab launch off when the user closed its tab

Closing the blank tab while the launch's fetches were in flight didn't
stop the launch: it still navigated the closed tab, offered it a token,
and recorded the app as opened (recent apps and /rao). The launch now
checks the tab before sending the app there and ends as not launched,
without an alert, when the user has closed it.

* Keep a same-origin opener instead of asking it for a GUI launch

Any top-level page with an app instance id and an opener asked that
opener for its launch, waited out the timeout when no answer came, and
then nulled window.opener. A page an app opened from its own origin (a
pop-out of itself, say) can't have the GUI as its opener, yet it waited
five seconds before its launch item and sign-in, and lost its link to
the app window that opened it. An opener readable from this page, on an
origin other than the GUI's, is now left alone and not asked.
2026-11-05 19:27:46 -07:00
Daniel Salazar 7355eaa77b feat(realtime): run the host prompt check per response and log its cost (#4507)
Each response that starts runs `ai.prompt.validate` on the text the client
sent since the previous check (instructions, message text and transcripts,
function call output), and its usage follows as `ai.prompt.cost-calculated`
under the same completion id. A refusal closes the session with
`permission_denied`. Nothing runs when no host listens for the check.
2026-11-05 18:05:43 -07:00
Nariman Jelveh d55aac1f98 Add embeddable Puter toolbar and shared app browser (#4019)
* Add embeddable Puter toolbar and shared app browser

Provide deployment-scoped iframe routes, a responsive host helper, read-only app browsing, saved-account switching, and new-tab account and upgrade links. Validate account sessions before switching and keep credentials inside the GUI origin.

Verified with 182 targeted tests, production GUI build, backend type checking, lint, and a real-backend browser smoke test. Translation checker could not run because tools/check-translations.js is missing.

* Prevent embedded toolbar from scrolling when menus open

* Stop double-encoding translated toolbar labels

i18n() already HTML-encodes its result, so wrapping it in escape() again
turned accented labels into literal entities (e.g. "Schlie&szlig;en") for
screen readers, and .text() did the same for the error message.

* Load only the preferred language in the toolbar and app browser embeds

Importing i18n.js pulled every translation into each embed, making the
toolbar bundle 1.1 MB. The embeds now bundle English and fetch the user's
language on demand (toolbar 1.12 MB -> 179 KB, app browser 1.18 MB -> 237 KB).
The desktop bundle keeps all translations.

* Keep toolbar buttons in place when a menu opens near the viewport edge

On phones the expanded frame was clamped to an 8px viewport margin, which
moved the whole toolbar row (by the header padding minus 8px) under the
user's finger. The frame now shrinks to fit beside and below the collapsed
toolbar, and only falls back to shifting when there isn't enough room.

* Don't pull focus back into the toolbar when the host page closes its menu

Clicking a host text field while a toolbar menu was open closed the menu and
then refocused the toolbar button, so the host field lost focus and typing
went nowhere. Focus is now restored only while the toolbar document has it.

* Close toolbar menus when the empty part of the expanded frame is pressed

While a menu is open the toolbar frame covers host content beside the
buttons; presses there reached neither the host nor the toolbar's outside
click handling, so the menu stayed open. They now close it.

* Omit the empty email line in the toolbar account menu

Temporary accounts and saved accounts without an email rendered an empty
element, adding a stray gap under the username.

* Stop user-hosted sites from framing the toolbar and app browser embeds

frame-ancestors allowed every *.<domain> subdomain, which on the default
self-hosted layout includes user sites (*.site.<domain>) and private apps
(*.app.<domain>), letting any hosted page clickjack sign-out and account
switching. The policy now names the embedding page exactly, from the
request's referrer, and refuses the user-content hosting domains.

* Use the dashboard danger color for the toolbar sign-out confirmation

The hardcoded red had 3:1 contrast on the dark panel, below WCAG AA for
14px text. The dashboard's danger token is 4.8:1 in light and 6:1 in dark.

* Drop the dashboard's hamburger clearance from the embedded app browser

At narrow widths the dashboard pads the search 60px down to clear its fixed
menu button. The embeds have no such button, so the toolbar's app panel
lost 90px of a ~500px frame to empty space above the search.

* Keep the toolbar's Upgrade button legible in dark mode

Upgrade was filled with the link color, which dark mode lightens to
#6b8cff: 3.1:1 under white text. Use the dashboard's primary-button fill
(4.6:1 in both themes) and its hover color, and drop the button classes
nothing styles on this page.

* Keep the toolbar's apps icon legible on hosts in the other theme

The toolbar follows the OS theme but draws on the host page, so in dark
mode on a light host its icon was #d4d4d4 on white (and the reverse on a
dark host). Give the apps toggle a surface of its own, like the avatar
beside it, and the session-unavailable notice one too.

* Let the toolbar sign out of a session that already ended

When the session was ended elsewhere (revoked, or signed out in another
tab), the anti-CSRF and logout requests answer 401 and sign-out failed
on every retry, stranding the stale token. Treat 401 as already signed
out and finish clearing it locally, as the desktop does; other failures
still keep the session and show the error.

* Keep the toolbar's slot following the host layout while a menu is open

The helper reserved the toolbar's space with a span frozen at the
frame's resolved size and margins, so a host that right-aligns it with
margin-left: auto (or sizes it in percentages or media queries) left the
open menu anchored to a stale spot, overflowing the header, after a
resize or rotation. Reserve it with an empty clone of the frame instead,
which the page's own rules keep laying out; if they target
data-puter-toolbar, which the clone drops so hosts still find one frame,
hold the frame's measured size.

* Reuse isOrgSeat for the toolbar's Upgrade check

The toolbar re-implemented the dashboard's organization-seat test
inline. Use the shared helper so both surfaces agree on who sees an
upgrade offer.

* Point comments at AppBrowser.js, where the Apps tab code now lives

The constants and methods these comments name moved out of TabApps.js
when the app browser became shared.

* Restore the license header on the new embed and toolbar files

TabApps.js lost its AGPL header when its code moved to AppBrowser.js,
and the new embed, toolbar, and host-helper modules were added without
one; every other GUI source file carries it.

* Remove the embeddable toolbar, keeping the standalone app browser

Drop /embed/toolbar, its page and host helper, their styles, strings and
tests, and the dashboard's ?upgrade=1 deep link that only the toolbar's
Upgrade button used. /embed/apps no longer allows the GUI origin to frame
it with a wildcard over deployment subdomains, which existed only for the
toolbar's nested app browser, and its bootstrap config no longer carries
the domain the toolbar read.
2026-11-05 17:19:05 -07:00
Daniel Salazar c6987d9606 feat(notifications): push unless a page is visible and focused (#4506)
A push is skipped only when a page that got the notification live was
visible and focused. Hidden, unfocused or unanswering pages no longer hold
it back, and pages no longer show browser notifications themselves, so the
push reaches every subscribed browser.

- Receipts carry the page's visibility and focus as of arrival; the settle
  gains `handled`. `showsOwn` is gone.
- Pages send `notif.handled { uid }` on the socket a notification arrived on
  when they got it while visible and focused. Core relays it as the
  node-local `notif.handled` event (60 a minute per user) for extensions.
- puter.js drops the page-shown notification path; push-sw.js drops its
  page-show message. Handlers still run live in the page.
- The desktop registers /sw.js only when push is turned on, updates it when
  subscribed, and removes one left without a subscription. Its hidden-tab
  banners are gone.
- Docs describe the focus rule.
2026-11-05 14:43:33 -07:00
Filip Kujundžić 796b42c74d Merge pull request #4475 from HeyPuter/fix/openai-cache-write-tiers 2026-11-05 21:27:46 +01:00
404oopsandClaude Opus 5.5 e2043a5e58 Merge remote-tracking branch 'origin/main' into fix/openai-cache-write-tiers
Main's docs cleanup (#4504) moved the prompt caching docs under
per-provider sections; the OpenAI paragraphs now live under
OpenAI models > Prompt Caching.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 19:55:55 +01:00
Filip Kujundžić 8b98c7ada8 Merge pull request #4503 from HeyPuter/codex/openai-image-default
feat(ai): add GPT Image 2.6 and default to Flare
2026-11-05 19:53:05 +01:00
Filip Kujundžić b27e84d181 Merge pull request #4457 from HeyPuter/fix/convo-bounded-recovery
Bound conversation recovery and report omitted replay context
2026-11-05 19:46:10 +01:00
404oopsandClaude Opus 5.5 add0e7be7d fix(convo): report routine context trimming as context_trimmed
A reconnect that left older items out of the 40-item, 48 KiB restore window
fired an error event with context_restore_failed, the code a provider
refusal uses, with the count only in the message. Every reconnect of a long
conversation raised an error that apps treating error as fatal would surface.

Trimming now fires a nonfatal context_trimmed event, before ready, with the
count in omittedItems. context_restore_failed is kept for a provider refusal
of a restored item or a failed send of the replay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 19:28:02 +01:00
404oops b5d221ede6 fix(convo): bound recovery attempts and report omitted context 2026-11-05 19:28:02 +01:00
404oops 3137a631a7 fix(realtime): reuse settled credit for post-record continuation 2026-11-05 19:28:02 +01:00
404oopsandClaude Opus 5.5 57706d8093 fix(realtime): bound unacknowledged audio by duration, not a byte cap
The per-commit cap of 1,280,000 bytes assumed G.711, so a PCM16 session
(48,000 bytes/s, the SDK's format) with transcription on was closed with
1013 after about 27 seconds without a commit: silence under server VAD,
one long utterance, or a long assistant reply.

Unacknowledged audio is now a duration at the provider-confirmed input
format (the lower rate while a requested format is unconfirmed). Without
turn detection it counts all audio since the last commit or clear; with
server VAD only speech from speech_started, plus the buffer after an
explicit commit, since the provider drops silence. Each started 160 s
claims one spare item as its hold instead of closing the session; a
commit, clear or input_audio_buffer_commit_empty returns the holds to the
pool. If the upstream closes first, every claimed item is billed. The
settle no longer commits silence.

The relay test file now expires the in-memory per-IP pre-auth window
after each test; every test connects from loopback and the added tests
pushed the file past 120 upgrades a minute.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 19:27:54 +01:00
Reynaldi ChernandoandClaude Opus 5.5 fec1d1916f Merge remote-tracking branch 'upstream/main' into codex/openai-image-default
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 01:03:08 +07:00
Juan Fernando CastroandReynaldi Chernando da369039e0 docs: complex teams recipe (PUT-1986) (#4095)
* docs: complex teams recipe (PUT-1986)

Showing which teammates have a file open, built from a folder shared with the
team plus `puter.events` watching it. There is no presence API, so the recipe
is about composing the two.

One use case per the recipe guidance. Uses the masked path from
`listShared()` so members address the owner's folder, keys presence files on
`uuid` rather than username, and re-reads the folder on each event so a missed
delivery cannot leave the list wrong.

* refine

---------

Co-authored-by: Reynaldi Chernando <[email protected]>
2026-11-06 00:53:25 +07:00
404oopsandClaude Opus 5.5 4f36152850 fix(realtime): hold transcription credit per item at the selected model
Input transcription reserved 64 items up front at the costliest model's
price and largest cost factor, about $7 before factors per session. Ordinary
subscribers couldn't enable it, and concurrent sessions were refused while
balance was left.

Each item now holds its own credit at the selected transcription model's
budget and cost factor. Two spare items are reserved before configuration or
audio is forwarded and refilled as the provider commits items, within the
64-item pending cap. A commit with no spare reserves its own item. A model
change releases the spares and reserves new ones at the new price; committed
items keep their hold. When the account can't cover another item, client
reads pause while the session's own holds may still free credit, and the
session is refused with insufficient_funds once it holds none.

Audio awaiting a commit acknowledgement is now tracked separately from pool
capacity and reset by each acknowledged commit or clear. If the upstream
closes before that acknowledgement, the audio bills as one item. Previously
the fallback required an untouched window, so a lost acknowledgement after an
earlier completed item went unbilled; the cumulative byte count could not
simply be reused without charging earlier work twice. The uncommitted-audio
cap is now 160 seconds of audio instead of a per-window total.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:49 -07:00
404oops 86db96d587 fix(realtime): reserve and settle pending transcription work 2026-11-05 10:23:49 -07:00
404oops 214ffb6605 fix(realtime): evict revoked credentials and recheck paid eligibility 2026-11-05 10:23:48 -07:00
404oops 871cc2cf87 fix(realtime): bound upgrades before credential admission 2026-11-05 10:23:48 -07:00
404oops ba002be0f7 fix(convo): reject interrupt readiness failures asynchronously 2026-11-05 10:23:47 -07:00
404oops 2832926f8d test(realtime): isolate credential attempt budgets between cases 2026-11-05 10:23:47 -07:00
404oops 1ab1c04760 fix(realtime): redact structured provider diagnostics 2026-11-05 10:23:47 -07:00
404oopsandClaude Opus 5.5 63a98f89ff test(metering): find the actor key in any meterIncr argument
The buffered write now passes the claim key too, which moves the counter
key's position. The image provider's write-ordering test matched it by
index and stopped seeing the write.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:46 -07:00
404oops d2cef27085 perf(metering): avoid retries for growing buffers 2026-11-05 10:23:46 -07:00
404oopsandClaude Opus 5.5 74b60c1227 fix(metering): type the array read's retry callback
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:46 -07:00
404oopsandClaude Opus 5.5 2ed3e78dfa fix(metering): keep buffered and store reads from missing or doubling usage
Three reads still got a flush wrong:

- A write re-sent after its reply was lost, found already applied, left
  the claim without touching a warm cached base, so balance reads stayed
  short of what the store held. The claim and the base are now dropped
  together, and the next read seeds a fresh base from the store.
- A read with no cached base took the buffer, then the store; a flush
  that wrote a batch and left its claim in between had it counted twice.
- An exact read added another deployment's claim but not the usage that
  arrived behind it, which that read can't flush, so a charge crossing
  the allowance went entirely to the allowance.

Reads that go to the store now take everything buffered (delta and
claim), read the store consistently, and read the buffer again, retrying
when it shrank in between. Single and array reads and exact reads all
use it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:46 -07:00
404oopsandClaude Opus 5.5 22ebd353b8 fix(metering): read the claim before the store in an exact read
`readExact` read the store and the outstanding claim side by side. When
a flush wrote a batch and took it off its claim between the two, the
store read had the counter from before the write and the claim read had
nothing, so the batch was missed: 10 units recorded, 0 read.

The claim is now read first. An amount gone from it by then was written
before the store read, which is consistent, so the read has it. When the
claim changes during the store read, the batch may be in both, so the
read is retried, up to three times.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:46 -07:00
404oopsandClaude Opus 5.5 7551e12370 fix(metering): count claims on the write path and read missing bases exactly
The running total `incr` returns and `readExact` also missed usage a
flush had claimed but not yet written. Both feed how a charge is split
between the allowance and purchased credit, so an account could be
charged to its allowance for usage that had already used it up. Both
now add the counter's outstanding claim.

A read or seed with no cached base asked the store with an ordinary
read, which could miss a write a settle had just made; it now reads
consistently. A write-through after a cache failure drops the cached
base, which doesn't have the write, so the next read reseeds it.

The dirty-set reconcile test no longer depends on how many flush cycles
earlier tests ran.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:46 -07:00
404oopsandClaude Opus 5.5 6e29e32fe9 fix(metering): count usage mid-flush in balance reads
A flush takes a counter's buffered amounts aside before writing them
onward, and reads only looked at the live delta and the cached base.
From the claim until the settle replaced the base, those amounts were
missing from every read: one store write normally, up to the orphan
sweep if the write failed. Balance checks, credit reservations among
them, could see an account as having more than it does.

Reads now add the counter's outstanding claim. A batch written onward
leaves the claim and joins the cached base in one step, so it stays
counted until the settle replaces the base with the store's view.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:46 -07:00
404oopsandClaude Opus 5.5 54bfc0ffe5 test(puter-js): prove microphone chunks hit the closing-socket gap
The browser test now counts the chunks the transport refused while the
socket read as closing, and requires at least one, so it can't pass
without exercising the gap. The socket stays closing until the
connection drops instead of going back to open.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:45 -07:00
404oopsandClaude Opus 5.5 c27883134b fix(puter-js): keep microphone capture when the socket closes first
A conversation still reads as connected for a moment after its socket
starts closing. A microphone chunk sent in that moment got
`session_closed`, and the capture handler treated that like a real
media failure: it stopped the tracks and the AudioContext, so the
replacement session after the reconnect had no microphone.

The built-in microphone now drops a chunk the transport refuses with
`session_closed` or `session_not_ready`, as it already did for
`queue_full`, and keeps capturing. A reconnect resumes sending; a final
close still stops capture through the conversation, and processor or
other errors still release it. Dropped chunks aren't replayed, and
public sends keep reporting the transport error.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:45 -07:00
404oopsandClaude Opus 5.5 cf4c1f6b64 fix(puter-js): end the conversation on an oversized event mid-reconnect
A server event over the 4 MiB limit closes the socket with 4009, which
ends a connected conversation. During a reconnect attempt the same close
was reported as `session_closed`, which is retried, so the conversation
tried ten more times and finally closed with the original drop code.
A replacement session opened after a restart notice was reopened again
and again for the same reason.

A 4009 close now ends the conversation during a reconnect too, reporting
4009, and a replacement session that hits it isn't reopened. Tests cover
both, a multibyte event of exactly 4 MiB, and a whole retrieved item
through the relay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:44 -07:00
404oopsandClaude Opus 5.5 305d20ab1c fix(puter-js): accept provider events up to the relay's 4 MiB cap
The conversation transport refused any server event over 64 KiB with a
terminal 4009 close, the limit meant for what the SDK sends. The relay
forwards provider events up to 4 MiB, and a session.updated echo or a
whole audio item easily passes 64 KiB, so valid conversations ended.

Incoming events now have their own 4 MiB limit, matching the relay,
counted in UTF-8 bytes (a string short enough that it can't exceed the
limit isn't encoded to check). An event over it still closes with 4009
and doesn't reconnect. Uploads keep their 64 KiB cap, and malformed
events are still ignored.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:44 -07:00
404oopsandClaude Opus 5.5 0ce6b0646a perf(metering): stop releases from churning the reservation version
Every release bumped the balance version, so an account's own short-lived
holds (each realtime session probes one and releases it at once) could
keep its other reservations reading a stale version until they were
refused. The usage a hold stands for already bumps the version when it's
recorded, before the hold is released, and a release without usage
leaves the balance as it was, so releases no longer bump it.

The admission read also uses the cached add-ons record when it shows no
purchased credit left, which can only understate the balance; it reads
consistently only while credit remains.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:44 -07:00
404oopsandClaude Opus 5.5 6b5c4f3d9e fix(metering): refuse a reservation whose balance never holds still
A reservation that found the balance changed three times in a row used
to skip the version check on its fourth attempt, which brought back the
race the check closes; an account could provoke that itself. It now
waits a short randomized time between attempts and refuses after five.

A missing version key now starts from a random value, so a version read
before the key expired can't match its successor. Credit grants,
allowance changes, manual usage corrections and a usage write that
fails partway also bump the version, and the store refuses an amount
it can't hold instead of storing a negative hold.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:44 -07:00
404oopsandClaude Opus 5.5 d6fdc5f268 fix(metering): reserve AI credit only when it fits, in one step
`reserveAiCredits` checked the balance and then took an unconditional
hold, so concurrent requests could all pass the check before any hold
was visible; two 16.2M µ¢ holds were admitted against a 20M µ¢ balance.

The hold store now takes a hold only if it fits beside the live holds,
in one Lua script on the actor's hold key. A per-user version, bumped
when usage is recorded and when a hold is released, lets the script
refuse a balance read that a settlement has since overtaken; the
service reads the balance again and retries, and skips the version
check on its last attempt rather than refuse an account whose balance
keeps moving. Both keys share the actor's hash tag, so the script runs
on Redis Cluster. The balance read for this check reads purchased
credit consistently.

`reserveCreditsIfAffordable` carries this through the service, and the
AI facade calls it once with the cost factor applied. `reserveCredits`
keeps its unconditional semantics. When the cache is unreachable, the
balance alone decides and nothing is held, as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:44 -07:00
404oopsandClaude Opus 5.5 4f3f946941 fix(realtime): judge a refusal by the plan it was decided on
A plan or funds refusal was classified by a plan lookup made after the
decision. A fallback plan is cached for only a few seconds, so when it
ran out in between, the refusal was judged against the recovered plan
and still ended the session with a terminal 1008. The plan is now also
checked just before each decision, and a refusal is retryable if either
check found a fallback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:43 -07:00
404oopsandClaude Opus 5.5 4e36672b6b test(puter-js): fail credential lookups until the relay hits one
The reconnect test made the next credential lookup fail once, but the
SDK's own socket.io connection could take that failure before the
relay's reconnect attempt did, so the relay never saw a failed lookup
and the test failed in CI. Lookups now fail until the relay has refused
an attempt on one, then recover.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:43 -07:00
404oopsandClaude Opus 5.5 6b0b530ab1 fix(realtime): retry instead of refusing when the plan lookup fails
When the billing lookup failed, metering answered with a provisional
fallback plan, so the relay refused the session with
`subscription_required` or, against the fallback's allowance,
`insufficient_funds`, both terminal 1008 closes, and the SDK showed the
upgrade prompt to a paying account. A plan or funds refusal decided on a
provisional plan now sends `internal_error` and closes with 1011, at
connection and mid-session, so the client retries once the lookup
recovers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:43 -07:00
404oopsandClaude Opus 5.5 68b8f341e9 test(realtime): cover a revoked session credential
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:43 -07:00
404oopsandClaude Opus 5.5 c3756d3ac3 fix(realtime): tell transient server failures apart from bad credentials
A credential lookup that threw (a database or cache outage) was treated
as no actor, so the session got `invalid_api_key` and a 1008 close. That
is terminal for the SDK, which then ended an established conversation
that only needed to reconnect. A failed lookup now gets `internal_error`
and closes with 1011, which the SDK retries; missing, invalid and
revoked credentials keep `invalid_api_key` and 1008. Nothing is cached
for the failed lookup, and the cause is logged server-side only.

A session whose usage records pile up behind metering now closes with
1013 instead of 1008, still with `rate_limit_exceeded`. Queued usage is
still recorded and its holds released. An unexpected setup failure now
sends `internal_error` before its 1011 close.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:43 -07:00
404oopsandClaude Opus 5.5 0c927ac5a4 fix(realtime): listen for client socket errors before refusing a session
A session refused while the server drains returned before its client
error and close listeners were registered, so a frame that errored the
socket during the close handshake (an oversized one, for example)
emitted an unhandled `error` and ended the process. The listeners now
go on as soon as the session exists, ahead of every refusal.

The 404 answer to an upgrade for another host also ends a raw socket
that has no error listener once Node hands it over; it now has one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 10:23:42 -07:00
Neal Shah 64c3b3c0ce subdomain and worker recipes complex, writing full stack applications with both (#4206) 2026-11-05 21:33:21 +07:00
Reynaldi Chernando e9a4c7a461 ai docs cleanup (#4504)
* ai docs cleanup

* fixes
2026-11-05 19:15:32 +07:00
Filip Kujundžić be9cd9eb89 feat(ai): add GPT Image 2.6 and default to Flare 2026-11-05 11:13:43 +01:00
Daniel Salazar 8b64cc5076 docs(notifications): recipes and format pass for dismissal, priority, buttons and schedule updates (#4502)
- dismiss.md is the one place that explains closing a notification on other
  devices; on/onNotification/push link to it instead of repeating it.
- Pages follow the house format: section order, error tables, playground
  examples for dismiss() and markShown(), sidebar and index listings.
- updateSchedule.md describes what a developer sees instead of internals.
- New recipes: mark notifications as read, incoming calls (high priority,
  short ttl, buttons), unread counts on the app icon.
- Existing recipes move reminders, snoozes and recurring summaries with
  updateSchedule() instead of cancel-and-recreate; the buttons recipe follows
  the recipe format.
2026-11-05 00:32:53 -07:00
Daniel Salazar d3869b7846 feat(notifications): dismissal across devices, high priority, buttons, schedule updates, docs fixes (#4501)
- Docs: status(), browser limits, events worker fetch/ack, update() across
  pages, stored-notification retries, device removal, push delivery, queue
  coalescing, existing service workers and missing error codes match the code.
- puter.notifications.markShown(uid) / dismiss(uid).
- Dismissals reach the app's live feed (on('dismiss')); pages close the
  notification on screen, and a later page load closes ones already dismissed.
  The desktop closes its own copies on notif.ack.
- Live notification postings are forwarded to sessions in other regions.
- notify({ priority: 'high' }).
- onPush() results accept requireInteraction and actions; clicks carry action.
- replaced reaches service-worker onPush() handlers.
- puter.events.updateSchedule(scheduleId, changes), with the invoker
  re-checking a schedule run's guard right before it is sent.
2026-11-05 00:17:27 -07:00
Daniel Salazar 91230697cd fix(backend): shutdown hooks don't skip each other, and final flushes wait for in-flight work (#4499) (#4500)
A throwing prepare/shutdown hook stopped the loop, so shutdown() skipped
closeAllConnections and every later hook, including the final metering
flush; each hook now runs and failures are logged. Broadcast, events
forwarding and share digests joined nothing already in flight and could
drop what was queued behind it; their final flushes now wait for it, ship
the tail, and drain once more at shutdown. Local worker disposal and the
OpenAI image metering call are awaited. settleWithin and
FINAL_FLUSH_TIMEOUT_MS bound each final flush.
2026-11-04 21:47:35 -07:00
Daniel Salazar 56e4856518 feat(backend): drain-start hooks run concurrently and can hold the listener open (#4498)
The listener closes once every onServerDrainStart hook settles, and the
drain window now starts at the signal, so time a hook spends holding the
listener open counts against drainMs instead of extending shutdown. A
failing hook no longer skips the others.
2026-11-04 19:40:54 -07:00
Reynaldi Chernando 8acd73675c use gpt 6 luna as example in docs (#4497) 2026-11-05 08:20:12 +07:00
Nariman Jelveh fc4af97885 fix(gui): the custom domain empty state holds its add button
With no domains, the only way on was the add button in the footer, which
the full-page popup pins to the bottom edge, far from the empty state the
eye lands on. The button now sits in the empty state under its hint, and
the footer keeps only Cancel until there's a domain to list.
2026-11-04 17:08:06 -07:00
Nariman Jelveh 7cec710014 feat(gui): redesign the custom domain dialog, full page in its popup (#4496)
* feat(gui): redesign the custom domain dialog, full page in its popup

- Domains list as cards with a status badge, trash icon, and inline
  Cancel/Confirm for remove and move
- DNS records as cards with copy buttons that confirm with a check
- Status card on the records step; a green check once the domain serves
  the site, after which the dialog closes
- Spinners on the button that's working, skeleton while loading, empty
  and unavailable states
- Pasted URLs are trimmed to their hostname before adding
- In the attach-custom-domain popup the dialog fills the page, with the
  actions pinned to the bottom

* fix(gui): Escape backs out of removing the chosen custom domain

Escape already put back a list row's pending confirmation. On the records
step, it closed the dialog while Remove was asking to confirm; it now puts
the button back first, like the rows.

* fix(gui): each custom domain step opens scrolled to the top

Switching steps reset the scroll of the dialog's body, which only scrolls
when the dialog fills a popup. The floating modal scrolls itself, so a
step opened from far down the list started scrolled past its status card.

* fix(gui): keyboard focus stays in the custom domain dialog

Focus on a control that got hidden or disabled fell to the page behind the
dialog, so Tab started over from there. Switching steps now focuses the
dialog, backing out of a row's confirmation returns to the button that
asked, a request gives focus back to the button that started it, and
removing a row moves focus to the next one.

* fix(gui): a removed custom domain row takes no input as it fades

Its buttons came back on once the delete finished, so a second click in
the fade sent the delete again, and Cancel brought back Use for a domain
that no longer exists. The row is inert until it's gone.

* fix(gui): DNS record copy buttons work without the clipboard API

On an origin with no navigator.clipboard (a Puter served over plain http)
or when the browser refuses the write, the copy buttons did nothing. They
now select the value and try the legacy copy, like the dashboard's form
dialog, and leave it selected to copy by hand if that fails too.

* fix(gui): custom domain rows keep the icon beside the name on phones

The name was sized to the row less 46px for the icon and gap, but the icon
is 36px wide with its border, so the row overflowed by 2px and every name
dropped below its icon. Only the actions wrap now; the name takes the rest
of the first line.

* fix(gui): hostnames in the custom domain dialog wrap whole

Every bold hostname broke at any letter, so one that would fit on the
next line was split mid-word at the end of this one ("puter.lo / calhost").
It now moves down whole and breaks only when it's wider than the line.

* fix(gui): readable Remove and Confirm buttons in dark mode

The red fill under their white labels used the danger text color, which
dark mode lightens to #f87171: 2.8:1 against white. Fills now use their
own token that stays #dc2626 (4.8:1); light mode is unchanged.

* fix(gui): the switch-to-www link shows when it's working

The link that switches a root domain to www had no disabled or loading
style, so while its request ran it still looked clickable and showed
nothing, while every other control dimmed. It now dims like them when
waiting and shows a spinner when it's the one working.
2026-11-04 16:45:56 -07:00
Daniel Salazar 48cd4ea355 fix: oidc toggle (#4494) 2026-11-04 13:13:15 -07:00
Neal ShahandClaude Opus 5.5 a110be0340 feat(gui): remove a custom domain from the attach dialog (#4493)
Each domain in the list gets a Remove button, confirmed in place like Move,
and the records pane can remove the domain just added. A removed domain is
never handed back as the dialog's answer.

Also fixes two strings that went stale: the claim prompt (an active domain
can be claimed too now) and the redirect note (root domains get two A
records).

PUT-2338

Co-authored-by: Claude Opus 5.5 <[email protected]>
2026-11-04 13:23:04 -05:00
404oopsandClaude Fable 5.1 0128e8779d fix(ai): route per-breakpoint cache lifetimes through the Responses API
Chat Completions rejects `prompt_cache_breakpoint.ttl` whatever its
value, so a request whose content parts name a breakpoint lifetime is
handed to the Responses provider like a request-level `ttl: '12h'`. The
`/openai/v1/responses` translator keeps `prompt_cache_breakpoint` on
text, image and file parts instead of dropping it with the other unknown
fields.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-11-03 00:58:29 +01:00
404oopsandClaude Fable 5.1 89f070f7d0 fix(ai): meter OpenAI cache writes by lifetime
GPT-5.6 and later write prompt-cache entries with a 30m or 12h lifetime
and bill them at 1.25x and 2.5x input; every write was metered at the
30m rate. The OpenAI calculators now split `cache_write_tokens_details`
into `cache_write_tokens` (30m) and `cache_write_12h_tokens` (12h), the
GPT-5.6+ catalog carries the 12h rate, and `usageDetails` reports
`cacheWrite30mTokens` / `cacheWrite12hTokens` instead of labelling the
writes as 5m entries.

Callers pick the lifetime with `prompt_cache_options` ({ mode, ttl }) on
`puter.ai.chat`, `/openai/v1/responses` and `/openai/v1/chat/completions`.
The Chat Completions provider hands a 12h request to the Responses
provider, the only API that writes 12h entries. `prompt_cache_retention`
is forwarded on Chat Completions too, and the hyphenated `in-memory`
spelling maps to the `in_memory` value the upstreams accept.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-11-03 00:52:33 +01:00
493 changed files with 35040 additions and 12066 deletions

No files matched your search

+6 -2
View File
@@ -176,7 +176,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v7
# Missing when the PR leg failed; `coverage (pr)` already reports that.
- name: Download PR coverage
id: pr-coverage
continue-on-error: true
uses: actions/download-artifact@v8
with:
name: puterjs-coverage-pr
@@ -186,6 +189,7 @@ jobs:
# base run) — then report without the comparison column.
- name: Download base coverage
id: base-coverage
if: steps.pr-coverage.outcome == 'success'
continue-on-error: true
uses: actions/download-artifact@v8
with:
@@ -193,7 +197,7 @@ jobs:
path: coverage-base
- name: Report coverage on PR
if: steps.base-coverage.outcome == 'success'
if: steps.pr-coverage.outcome == 'success' && steps.base-coverage.outcome == 'success'
uses: davelosert/vitest-coverage-report-action@v2
with:
name: puter.js SDK
@@ -202,7 +206,7 @@ jobs:
json-summary-compare-path: coverage-base/coverage-summary.json
- name: Report coverage on PR (no base comparison)
if: steps.base-coverage.outcome != 'success'
if: steps.pr-coverage.outcome == 'success' && steps.base-coverage.outcome != 'success'
uses: davelosert/vitest-coverage-report-action@v2
with:
name: puter.js SDK
+31 -1
View File
@@ -159,13 +159,42 @@ The reference has two kinds of page:
An overview page answers "what is this API and is it what I need?", and a function page answers "how exactly does this call behave?". A change to one function usually only touches that function's page, so a new `kv.list()` option goes in `KV/list.md` without a matching update to `KV.md`. Don't copy a function's options, limits, or edge cases onto the overview page for completeness. Where a detail goes is a judgment call, not a hard rule: update the overview when a change affects how someone would describe the API as a whole.
When you add a page, add it to [src/docs/src/sidebar.js](src/docs/src/sidebar.js) under its area, or it won't show up in the navigation. When you add a runnable example, add its HTML to `src/docs/src/playground/examples/<slug>.html`, register the slug in [src/docs/src/examples.js](src/docs/src/examples.js), and link the code block to it by putting the slug after the language with a semicolon (` ```html;kv-set `).
When you add a page, add it to [src/docs/src/sidebar.js](src/docs/src/sidebar.js) under its area, or it won't show up in the navigation.
**Sidebar order.** Order the API areas in the sidebar by how often developers use them, most common first. Overview stays first and Objects stays last. Inside an area, order the pages by use too, and keep pages that make up one feature next to each other, such as `share()`, `unshare()`, `listShared()`, and `listSharedByMe()`. A group goes where its most-used page would go. A helper that only supports another function, such as `listModels()` for `chat()`, goes by its own use. Put create, list, get, update, and delete in that order, and constants such as `MAX_KEY_SIZE` last. The function list on the area's overview page uses the sidebar's order. When you add a runnable example, add its HTML to `src/docs/src/playground/examples/<slug>.html`, register the slug in [src/docs/src/examples.js](src/docs/src/examples.js), and link the code block to it by putting the slug after the language with a semicolon (` ```html;kv-set `).
### Function page layout
A function page opens with one sentence that says what the function does, in its own paragraph, such as "Given a prompt, generate an image using AI." Put any more detail in the paragraphs after it.
After the intro, a function page has these `##` sections, in this order:
1. Syntax
2. Parameters
3. Return value
4. Errors, if the function has any
5. Sections for specific topics, if the page needs them, with `## Model-specific features` last
6. Examples
[chat.md](src/docs/src/AI/chat.md) follows this layout.
- **Options as a table.** Document an options object as a table with Option, Type, and Description columns. Give each option its own row, even when two options go together, such as `width` and `height`. Order the rows by how often developers use them, most common first, without group rows. In chat.md that's the core options, then reasoning, tools, response handling, request control, and output.
- **Common options in the table, model-specific behavior in its own section.** A page whose function works with many models or providers, such as [chat.md](src/docs/src/AI/chat.md) and [txt2img.md](src/docs/src/AI/txt2img.md), splits its options this way:
- **The options table** under Parameters lists the options that work with every model, and describes each one the way it works for every model.
- **`## Model-specific features`**, the last section before Examples, covers everything else. It has a `###` for each model family or provider, named after it, such as "OpenAI models" or "Cloudflare models". Under it go the options only some models accept, and the common options a model handles differently, such as a narrower range, other allowed values, or an option it ignores.
- **Write there only what's different.** A `###` lists an option only when that model or provider adds it or handles it differently. An option that works the same everywhere stays out, even if the provider supports it, and a model or provider with nothing different gets no `###`.
- **Model-specific features is the only place for model-specific details.** Every limit, format, allowed value, default, price, or quirk that applies to some models goes there, in that model's or provider's `###`, and nowhere else. The options table and topic sections (such as Image inputs or Video input) describe what's true for every model and link to Model-specific features for the rest. Don't add a table or sentence there that compares providers or repeats a model's limits. Each copy is another source of truth to keep in sync, and copies drift when a model changes.
- **Link the two.** Directly under the options table, put `Some models accept additional options or handle these options differently. See [Model-specific features](#model-specific-features).` A row whose option behaves differently on some models can also end with `See [Model-specific features](#model-specific-features).`
- **A long topic gets its own `##` section.** When explaining an option or feature takes more than a sentence or two, such as function calling or output dimensions, give it a section between Errors and Examples and link to it from the options table. This keeps the reference sections short.
- **Usage patterns are examples.** A way of using the API, such as stopping a stream, goes in a runnable example that the relevant section links to ("See the [stop a stream example](/playground/ai-chat-stop-stream/)."), not in a section of its own.
- **Each example has a `###` heading** under `## Examples`, so every example shows in the table of contents.
### Writing docs
These apply to reference pages and recipes alike.
- **Document the developer-facing side only.** The reader is building on top of puter.js, so cover what affects their code: signatures, defaults, return shapes, errors, limits, and behavior they can observe. Leave out internal workings (which service handles the call, storage layout, caching, retries) unless they change what the developer has to do. [rate-limits-and-quotas.md](src/docs/src/rate-limits-and-quotas.md) is the exception: it publishes every limit, including timeouts and caps that come from how the backend works.
- **Document Puter as it runs on puter.com.** The docs are for developers building on production Puter. Leave out behavior that only happens on a self-hosted or differently configured server, such as a provider that isn't set up there.
- **Describe the current state.** Docs aren't a changelog. Write how the API behaves now, without "now", "no longer", "existing code is unaffected", or other wording that compares it to an older version.
- **Verify before you document.** Run the calls you describe against a running Puter instead of inferring behavior from the source.
- **Write plainly.** Use the literal words a developer would say out loud, with contractions and everyday words. No idioms or figurative verbs ("walk away from something shared with you" → "remove your own access", "nothing to page through" → "without pagination"). No formal register ("present only while more pages remain" → "includes a `cursor` if there are more pages to load"). Prefer dev terms such as atomic, concurrent, and insertion order.
@@ -173,6 +202,7 @@ These apply to reference pages and recipes alike.
- **Describe behavior directly.** No quoted inner-voice phrases. Write "`isNew` is `true` when the person did not have access before", not "this tells 'I just gave this to someone' from 'they already had it'".
- **Keep references easy to follow.** Name what a list or code block belongs to in the sentence directly above it. Name a thing once and keep that name. Introduce an actor as "a user" before giving them a role: "A user can create a team. The user who creates the team becomes its owner."
- **Sentence mechanics.** Never start a sentence with inline code: write "To store data, use the [`set()`](/KV/set/) method." Link every inline mention of an API to its page, every time. No em dashes, and no colons or semicolons mid-sentence; a colon that introduces a code block is fine.
- **No `##` or `###` headings inside tabs.** The table of contents lists every `##` and `###` heading on the page, including the ones in a hidden tab (an `example-content` block switched by the `example-group` pills), so its links lead to content the reader can't see. Put the heading above the tabs, or use `####` inside a tab, which the table of contents skips.
- **Show the correct way.** No "don't" and no wrong-way-first examples, in recipes and playground examples alike. Mention a wrong approach only when developers already hit it in practice, and state how it fails as a fact: "`add()` reads a plain object as a path map, so the call rejects with …".
### Recipes
+147
View File
@@ -0,0 +1,147 @@
# Embedded app browser
`GET /embed/apps` on the deployment's main GUI origin renders the same app browser
as the dashboard's Apps tab, without the desktop or dashboard navigation.
It supports search, paging, saved folders, and saved ordering. It uses the same
installed-app and recommendation data as the dashboard, including the user's
removed recommendations. Editing, adding, reordering, renaming, and uninstalling
are disabled. Selecting an app opens it in a new browser tab at its own URL, the
same way the dashboard does, and the host page stays open. The app's token isn't
put in the URL: the app's SDK asks the embed for it once the tab loads, so the
embed must still be open then. If the iframe is removed first, the app falls back
to its normal sign-in. Apps that need the desktop (built-in and godmode apps,
private apps the user can't open, or a failed token request) open `/app/<name>`
on the GUI origin, cut off from the embed. Website shortcuts open their external
destination with no opener.
The embed follows the OS's light or dark setting. A host with a fixed theme adds
`theme=dark` or `theme=light` to the fragment (`#user=<uuid>&theme=dark`), since
Chromium doesn't pass an iframe's `color-scheme` on to the page inside it.
Apps opened from the embed carry `utm_source=puter.com&utm_medium=referral&utm_campaign=app_browser_embed`;
the desktop and dashboard send `utm_campaign=desktop` and `utm_campaign=dashboard`.
## Session and framing
The embed shows the GUI session only to the user the host page is signed in as.
The host names that user's `uuid` (from `puter.auth.getUser()`) in the iframe
URL's fragment, as `/embed/apps#user=<uuid>`. The embed reads the session, asks
`/whoami` which user it belongs to, and shows the signed-out view unless the two
match. So a host that names no user, a malformed one, or a user other than the
session's, gets the signed-out view, and so does a session the API refuses. A
failed `/whoami` check shows the error state. The fragment never reaches the
server and can only narrow what the embed shows: naming the session's own user
gives the same view as a direct visit to the GUI. Changing the fragment of a
loaded embed reloads it.
The embed reads the existing GUI session from local storage on its own origin.
If that storage holds no session, the embed gets the session from the GUI origin's session cookie through
`/get-gui-token`. This matters in Safari, which gives a framed origin separate
local storage under each top-level origin, so even a sibling subdomain's iframe
can't see the GUI's stored token. The session cookie is still sent, because the
host is on the same site. The embed doesn't write that token back to storage. It
does not create accounts, show a login dialog, or accept a token from its parent.
Being signed in on a subdomain alone does not create a session on the main GUI
origin.
Without a session it can show, the embed shows the recommended apps (`RecommendedAppsService`)
that the server put in the page, and nothing else: no installed apps, folders, or
saved order, and no puter.js. Selecting one opens it at the same URL a signed-in
launch uses, with no opener and no token on offer, so the app signs in on its own.
Apps that need the desktop open `/app/<name>`. If the page carries no recommended
apps, or a load fails, the embed displays an unavailable/error state.
The route's `Content-Security-Policy: frame-ancestors` allows the GUI origin and
the embedding page's origin when it is exactly one label under `config.domain`
with the scheme and port from `config.origin`. Subdomains the server answers
itself (`api`, `dav`, `js`, `www`, the native-app hosts, and the host of
`api_base_url`) and the user-content hosting domains (`static_hosting_domain`,
`private_app_hosting_domain`, and their `_alt` variants) are refused. For
example, `https://puter.com` permits `https://spreadsheet.puter.com` but not
`https://api.puter.com`, and `http://puter.localhost:4100` permits
`http://spreadsheet.puter.localhost:4100` but not
`http://someone.site.puter.localhost:4100`. The embedding page is read from the
iframe request's referrer, so the host must not suppress it (the browser default
`strict-origin-when-cross-origin` is enough). All ancestors in a nested iframe
chain must be allowed. Other GUI routes retain `X-Frame-Options: SAMEORIGIN`.
The embed accepts no query parameters. Requests with a query string redirect to
the same embed path so SDK bootstrap parameters cannot override the session or API
origin. The host needs to allow the GUI origin in its own `frame-src` CSP if it
sets one. If sandboxing the iframe, allow scripts, same-origin storage, popups,
and popups to escape the sandbox.
## Host integration
A first-party app that signs in with puter.js can place `<puter-user-menu>` from
the SDK in its header instead (see `src/puter-js/src/ui/components.md`). It opens
this embed with the signed-in user named and closes it when the app's sign-in
changes. The rest of this section is for a host that builds the iframe itself.
The iframe can be shown whether or not the user is signed in. Create it when
“More apps” is opened, naming the signed-in user in its fragment, and remove it
on close so reopening loads current apps and preferences. Remove it when the
host's own sign-in changes too, such as on sign-out, so an open embed doesn't
keep the previous user. The host owns the modal and close control;
there is no parent messaging API and no app list or credential is sent to it.
The app browser fills its iframe and responds to its size. It shows a spinner until its apps load.
This example assumes `puterOrigin` comes from the host app's deployment
settings. Translate the button and frame labels using the host app's
localization system.
```html
<button id="more-apps" type="button">More apps</button>
<dialog id="apps-dialog" aria-label="More apps">
<button id="close-apps" type="button">Close</button>
<div id="apps-frame"></div>
</dialog>
<style>
#apps-dialog { padding: 12px; width: min(900px, 90vw); }
#apps-frame iframe { width: 100%; height: min(650px, 75vh); border: 0; }
</style>
<script type="module">
const puterOrigin = 'https://puter.com'; // Use this deployment's configured origin.
const dialog = document.querySelector('#apps-dialog');
const container = document.querySelector('#apps-frame');
const trigger = document.querySelector('#more-apps');
trigger.addEventListener('click', async () => {
const url = new URL('/embed/apps', puterOrigin);
// Signed out, the embed shows the recommended apps.
const user = puter.auth.isSignedIn() ? await puter.auth.getUser().catch(() => null) : null;
if (user) url.hash = `user=${encodeURIComponent(user.uuid)}`;
const frame = document.createElement('iframe');
frame.title = 'Your apps';
frame.src = url.href;
container.replaceChildren(frame);
dialog.showModal();
});
document.querySelector('#close-apps').addEventListener('click', () => dialog.close());
dialog.addEventListener('close', () => {
container.replaceChildren();
trigger.focus();
});
</script>
```
## Implementation and checks
`createAppBrowser` in `src/gui/src/UI/Dashboard/AppBrowser.js` owns the shared UI
and data behavior; its `guestApps` option is the signed-out list. `TabApps.js`
injects desktop actions. `appsEmbed.js` supplies new-tab launching through
`launchAppInTab` (signed in) and `guestAppLaunchUrl` (signed out) in
`src/gui/src/helpers/appTabLaunch.js`, which build the same app URL as
`launch_app`, and loads only the embed entry, its SDK, and shared styles. `embedHostUser` in
`src/gui/src/helpers/embedHostUser.js` reads the host's user from the fragment, and
`embedHostTheme.js` beside it applies the host's theme.
The normal GUI webpack build emits `bundle.min.js` and `apps-embed.min.js`.
The `/embed/apps` route puts the recommended list in the page's config as
`guestApps`. No puter.js API or app-list endpoint changes are needed.
Run the GUI component/browser tests (with the SDK bundle built and Playwright
Chromium installed), and the backend route tests:
```sh
npx vitest run --config src/gui/vitest.config.js src/gui/src/UI/Dashboard/AppBrowser
npx vitest run --config src/backend/vitest.config.ts src/backend/server.test.ts
```
+7 -6
View File
@@ -1,6 +1,6 @@
# Browser push client
Puter's desktop shows pushes through `/sw.js`. Core registers it, and shows Settings → Notifications, only when the `pushEnabled` gui param is `true`; core never sets it. A backend extension that delivers pushes sets it with `services.homepage.setGuiParam('pushEnabled', true)` and can load its own code into the worker with `services.homepage.registerServiceWorkerScript(path)`.
Puter's desktop shows pushes through `/sw.js`. Core shows Settings → Notifications only when the `pushEnabled` gui param is `true`; core never sets it. The worker is registered only when push is turned on in the browser, and a load removes one left without a subscription. A backend extension that delivers pushes sets it with `services.homepage.setGuiParam('pushEnabled', true)` and can load its own code into the worker with `services.homepage.registerServiceWorkerScript(path)`.
Core subscribes the browser to a push service only when the server also publishes `pushWebPushKey` (see [Built-in Web Push client](#built-in-web-push-client)), and it has no `/push/*` routes of its own. The device, app, preference and subscription endpoints below come from that extension. On a deployment without one, such as most self-hosted servers, they answer core's plain 404 (`code: 'not_found'`), which `puter.notifications` reports as `push_unavailable` and the Notifications tab shows as unavailable. A device the extension can't find is `device_not_found`.
@@ -22,12 +22,13 @@ When the browser replaces the subscription (`pushsubscriptionchange`), `sw.js` p
Before a notification is pushed, the server asks the open clients whether they received it live. It sends `notif.receipt` with `{ uid }` on the user's sockets for the app that sent it, or on the desktop's sockets for Puter's own notifications. The SDK remembers the uids its page received through `onNotification()`, `onPush()` or the desktop's feed, and answers once its page has that notification, or with `{ received: false }` when it never gets it. The server waits up to 5 seconds (`notificationAckTimeoutMs`).
An answer carries `visibilityState`, `focused` and `showsOwn`, the last being whether the page shows the notification itself. A website page shows it itself when it is hidden or unfocused, has an `onPush()` handler and notification permission, and no other tab of the same site is focused. Inside the desktop, the desktop shows it, and an app's `onPush()` result is never shown as a browser notification.
An answer carries `visibilityState` and `focused` as they were when the page received the notification. Pages never show a browser notification themselves; pushes do.
- An answer from a page that is visible and focused, or that shows the notification itself, stops the push.
- Otherwise the notification is pushed. A website's pushes go only to the browsers that subscribed through its own worker, never to Puter's `/sw.js`. An app inside the desktop pushes through `/sw.js`, which shows the app's name and icon.
- A hidden desktop tab shows no banner, so answers only from hidden desktop tabs don't stop a push of Puter's own notifications.
- A client that can't answer, such as an older puter.js, holds the push back while it is connected.
- An answer from a page that was visible and focused stops the push (`handled` on `notif.delivery.settled`). An older client that doesn't send `focused` counts while visible.
- Otherwise the notification is pushed, to every browser the user subscribed. A website's pushes go only to the browsers that subscribed through its own worker, never to Puter's `/sw.js`. An app inside the desktop pushes through `/sw.js`, which shows the app's name and icon.
- A client that can't answer, such as an older puter.js, doesn't hold the push back.
The ask reaches only the sockets in the asking region. So that other regions hear about a page too, a page that receives a notification live while visible and focused also sends `notif.handled` with `{ uid }` on the socket it came on. Core relays it as the node-local `notif.handled` event with the socket's user and app, at most 60 a minute per user; an extension that delivers pushes decides what to do with it.
## Registering
@@ -318,3 +318,13 @@ describe('AbstractDatabaseClient — SQL generation', () => {
);
});
});
it('builds an insert for batchWrite and shares it with insert', async () => {
const client = new FixtureDatabaseClient([]);
const entry = client.insertEntry('widget', { name: "reader's", enabled: true });
expect(entry.values).toEqual(["reader's", true]);
expect(entry.statement).toContain('VALUES (?, ?)');
await client.insert('widget', { name: "reader's", enabled: true });
expect(client.writes).toEqual([{ query: entry.statement, params: entry.values }]);
});
+16 -7
View File
@@ -110,14 +110,23 @@ export class AbstractDatabaseClient extends PuterClient {
tableName: string,
data: Record<string, unknown>,
): Promise<WriteResult> {
const entry = this.insertEntry(tableName, data);
return this.write(
entry.statement + this.returningIdClause(),
entry.values,
);
}
/** An insert that can join other writes in a transaction. */
insertEntry(tableName: string, data: Record<string, unknown>): BatchEntry {
const cols = Object.keys(data);
const values = Object.values(data);
const sql =
`INSERT INTO ${this.quoteIdentifier(tableName)} ` +
`(${cols.map((c) => this.quoteIdentifier(c)).join(', ')}) ` +
`VALUES (${cols.map(() => '?').join(', ')})` +
this.returningIdClause();
return this.write(sql, values);
return {
statement:
`INSERT INTO ${this.quoteIdentifier(tableName)} ` +
`(${cols.map((c) => this.quoteIdentifier(c)).join(', ')}) ` +
`VALUES (${cols.map(() => '?').join(', ')})`,
values: Object.values(data),
};
}
/** Whether `read()` goes to a separate replica rather than the primary. */
+48 -11
View File
@@ -117,7 +117,7 @@ export type EventMap = {
model_used: string;
service_used: string;
intended_service: string;
model_details: {
model_details?: {
id: string;
provider: string;
input_cost_key: string;
@@ -200,6 +200,16 @@ export type EventMap = {
};
// ---- Auth / signup ----
/**
* Which signup methods are open, emitted via `emitAndWait` before every
* signup and whenever the GUI reads the provider list. A listener sets
* `oidcOnly` to refuse form and temp signups, leaving OIDC and team
* provisioning open; `message` is what a refused form signup is told.
*/
'puter.signup.policy': {
oidcOnly: boolean;
message: string | null;
};
'puter.signup.validate': {
allow: boolean;
email?: string;
@@ -546,6 +556,14 @@ export type EventMap = {
'kv.share.retired': { handles: string[]; userId: number };
// ---- Notifications ----
/** An app message was acknowledged after its state committed. */
'notif.acknowledged': {
userId: number;
uid: string;
type: string;
audience: string;
appUid: string;
};
/**
* A notification row exists. Emitted after the insert commits — nothing
* pushes a uid before there is a row to name — and carries the scope tuple
@@ -579,6 +597,8 @@ export type EventMap = {
value: Record<string, unknown>;
/** An earlier notification rewritten in place. */
updated?: true;
/** Replaced an earlier notification in its group. */
replaced?: true;
/** Names this one send, for a receipt asked about it. */
sendId: string;
/** The same `sendId` may be emitted again, as a delayed send is. */
@@ -616,6 +636,18 @@ export type EventMap = {
/** A peer region holds a socket for the user. */
peerRegions: boolean;
responders: NotifDeliveryResponder[];
/** A page here got it live while visible and focused. */
handled: boolean;
};
/**
* A page reported that it got a notification live while visible and
* focused. Node-local; `appUid` is the reporting socket's app, absent for
* the desktop.
*/
'notif.handled': {
userId: number;
appUid?: string;
uid: string;
};
// ---- Metering ----
@@ -841,6 +873,12 @@ export type EventMap = {
/** One access token was revoked; only its own connections should drop. */
'auth.access-token.revoked': { token_uid: string };
/** Same-cluster eviction for raw realtime WebSockets. */
'pubsub.realtime.auth-revoked': {
sessionUids?: string[];
tokenUid?: string;
};
/**
* A grant was withdrawn, so whatever was standing on it has to be settled
* rather than left to fail its next check.
@@ -1041,10 +1079,11 @@ export type EventKey = keyof EventMap & string;
// Generates a wildcard for every non-final dot-separated prefix of K.
export type WildcardPrefixes<K extends string> =
K extends `${infer Head}.${infer Tail}`
? | `${Head}.*`
| (Tail extends `${string}.${string}`
? `${Head}.${WildcardPrefixes<Tail>}`
: never)
?
| `${Head}.*`
| (Tail extends `${string}.${string}`
? `${Head}.${WildcardPrefixes<Tail>}`
: never)
: never;
export type ListenKey = EventKey | WildcardPrefixes<EventKey>;
@@ -1055,18 +1094,16 @@ export type MatchingEvents<P extends ListenKey> = P extends `${infer Prefix}.*`
/** Acked, not acked, no socket anywhere, or sockets only in a peer region. */
export type NotifDeliveryOutcome =
'received' | 'unreceived' | 'no-socket' | 'remote-only';
| 'received'
| 'unreceived'
| 'no-socket'
| 'remote-only';
/** One socket's receipt for a live notification. */
export interface NotifDeliveryResponder {
visibilityState: 'visible' | 'hidden' | 'unknown';
/** The page's window has focus. Absent from older clients. */
focused?: boolean;
/**
* The page shows its own browser notification while hidden or unfocused.
* Absent from older clients.
*/
showsOwn?: boolean;
}
export type EventMetadata = {
@@ -347,6 +347,46 @@ describe('DispatcherInvokeTransport with every connection busy', () => {
held[INVOKE_CONNECTIONS].answer();
await expect(after).resolves.toMatchObject({ status: 200 });
});
it('rechecks the guard once a connection frees, and drops a superseded call', async () => {
const { transport, held, answerAll } = await saturated();
let current = true;
const guard = vi.fn(async () => current);
const waiting = transport.send({ ...CALL, beforeSend: guard });
current = false;
held[0].answer();
await expect(waiting).resolves.toEqual({
status: null,
error: 'invocation superseded',
});
expect(guard).toHaveBeenCalledTimes(1);
expect(held).toHaveLength(INVOKE_CONNECTIONS);
await answerAll();
});
it('fails a call whose guard throws after the wait, and frees its slot', async () => {
const { transport, held, answerAll } = await saturated();
const waiting = transport.send({
...CALL,
beforeSend: async () => {
throw new Error('lease store down');
},
});
held[0].answer();
await expect(waiting).rejects.toThrow('lease store down');
expect(held).toHaveLength(INVOKE_CONNECTIONS);
await answerAll();
const after = transport.send(CALL);
await vi.waitFor(() =>
expect(held).toHaveLength(INVOKE_CONNECTIONS + 1),
);
held[INVOKE_CONNECTIONS].answer();
await expect(after).resolves.toMatchObject({ status: 200 });
});
});
describe('DispatcherInvokeTransport schedule lane', () => {
@@ -771,6 +811,138 @@ describe('DispatcherInvokeTransport batches', () => {
]);
});
it('leaves a call superseded while gathered out of the batch', async () => {
const { fetchImpl, batches, singles } = streamingFetch();
const transport = transportFor(fetchImpl, { batchWindowMs: 20 });
let current = true;
const sent = [
transport.send(CALL),
transport.send({ ...CALL, beforeSend: async () => current }),
transport.send({ ...CALL, script: 'evw-other' }),
];
current = false;
await vi.waitFor(() => expect(batches).toHaveLength(1));
expect(batches[0].body.calls.map((call) => call.script)).toEqual([
'evw-test',
'evw-other',
]);
await expect(sent[1]).resolves.toEqual({
status: null,
error: 'invocation superseded',
});
batches[0].answer({ i: 0, status: 200, handled: true });
batches[0].answer({ i: 1, status: 200, handled: true });
await expect(Promise.all([sent[0], sent[2]])).resolves.toEqual([
{ status: 200, handled: true },
{ status: 200, handled: true },
]);
expect(singles).toHaveLength(0);
});
it('sends nothing when every gathered call was superseded', async () => {
const fetchImpl = vi.fn(
stubFetch(200, { [EVENTS_HANDLED_HEADER]: '1' }),
);
const transport = transportFor(fetchImpl as unknown as FetchImpl);
const superseded = { ...CALL, beforeSend: async () => false };
await expect(
Promise.all([
transport.send(superseded),
transport.send(superseded),
]),
).resolves.toEqual([
{ status: null, error: 'invocation superseded' },
{ status: null, error: 'invocation superseded' },
]);
expect(fetchImpl).not.toHaveBeenCalled();
});
it('fails only the call whose guard throws at flush', async () => {
const { fetchImpl, batches, singles } = streamingFetch();
const transport = transportFor(fetchImpl);
const failing = transport.send({
...CALL,
beforeSend: async () => {
throw new Error('lease store down');
},
});
const kept = transport.send(CALL);
await expect(failing).rejects.toThrow('lease store down');
// One call left, so it goes alone.
await expect(kept).resolves.toEqual({ status: 200, handled: true });
expect(batches).toHaveLength(0);
expect(singles).toHaveLength(1);
});
it('rechecks the guard before resending a batch call on its own', async () => {
const urls: string[] = [];
let current = true;
const fetchImpl = (async (url: string) => {
urls.push(url);
if (url.endsWith(EVENTS_DISPATCH_BATCH_PATH)) {
// Superseded while the batch was out.
current = false;
return new Response('no-such-route', {
status: 404,
headers: {
[EVENTS_DISPATCH_ERROR_HEADER]: 'no-such-route',
},
});
}
return new Response(null, {
status: 200,
headers: { [EVENTS_HANDLED_HEADER]: '1' },
});
}) as unknown as FetchImpl;
const transport = transportFor(fetchImpl);
await expect(
Promise.all([
transport.send({ ...CALL, beforeSend: async () => current }),
transport.send(CALL),
]),
).resolves.toEqual([
{ status: null, error: 'invocation superseded' },
{ status: 200, handled: true },
]);
expect(urls.map((url) => new URL(url).pathname)).toEqual([
EVENTS_DISPATCH_BATCH_PATH,
EVENTS_DISPATCH_PATH,
]);
});
it('answers a schedule run superseded in the batch as the early check does', async () => {
const { fetchImpl, batches } = streamingFetch();
const client = new EventsWorkerInvokerClient({
events: { invokeTimeoutMs: 5_000 },
} as unknown as IConfig);
client.setTransport(transportFor(fetchImpl, { batchWindowMs: 20 }));
let current = true;
const guard = vi.fn(async () => current);
const run = client.invoke(REQUEST, {
lane: 'schedule',
beforeSend: guard,
});
const other = client.invoke(REQUEST);
const another = client.invoke(REQUEST);
await vi.waitFor(() => expect(guard).toHaveBeenCalledTimes(1));
current = false;
await vi.waitFor(() => expect(batches).toHaveLength(1));
expect(batches[0].body.calls).toHaveLength(2);
await expect(run).resolves.toEqual(
await client.invoke(REQUEST, { beforeSend: async () => false }),
);
await expect(run).resolves.toMatchObject({
outcome: 'unavailable',
error: 'invocation superseded',
});
batches[0].answer({ i: 0, status: 200, handled: true });
batches[0].answer({ i: 1, status: 200, handled: true });
await Promise.all([other, another]);
});
it('builds each body when the batch goes out', async () => {
const { fetchImpl, batches } = streamingFetch();
const transport = transportFor(fetchImpl);
@@ -827,6 +999,26 @@ describe('DispatcherInvokeTransport batches', () => {
});
describe('EventsWorkerInvokerClient invoke options', () => {
it('does not send a superseded invocation', async () => {
const client = makeClient();
const send = vi.fn(async () => ({ status: 200, handled: true }));
client.setTransport({ send });
const result = await client.invoke(REQUEST, { beforeSend: async () => false });
expect(result.outcome).toBe('unavailable');
expect(send).not.toHaveBeenCalled();
});
it('rechecks before resending after a deployment', async () => {
const client = makeClient();
let current = true;
const send = vi.fn(async () => ({ status: null, dispatchReason: 'missing' }));
client.setTransport({ send });
client.setMissHandler(async () => { current = false; return 'deployed'; });
const result = await client.invoke(REQUEST, { beforeSend: async () => current });
expect(result.outcome).toBe('unavailable');
expect(send).toHaveBeenCalledTimes(1);
});
it('stamps the event as each attempt goes out', async () => {
const bodies: string[] = [];
const client = makeClient();
@@ -282,6 +282,11 @@ export interface EventsInvokeCall {
* connection first, so a send time stamped into it is the real one.
*/
render?: () => string;
/**
* Rechecked by a transport that waits before sending; false drops the call
* with {@link supersededAnswer}.
*/
beforeSend?: () => Promise<boolean>;
}
/** What one attempt at delivering an invocation came back with. */
@@ -334,6 +339,8 @@ export interface WorkerInvokeResult {
/** How one invocation is sent. */
export interface WorkerInvokeOptions {
lane?: InvokeLane;
/** Rechecked before each send, including a resend after deployment. */
beforeSend?: () => Promise<boolean>;
/** Applied to the event as each attempt is sent. */
stampEvent?: (event: unknown) => unknown;
}
@@ -341,6 +348,12 @@ export interface WorkerInvokeOptions {
const NO_TRANSPORT =
'no events dispatcher configured (url and internal secret)';
/** What a call whose `beforeSend` said no answers instead of being sent. */
const supersededAnswer = (): EventsInvokeAnswer => ({
status: null,
error: 'invocation superseded',
});
export class EventsWorkerInvokerClient extends PuterClient {
/** Set only where invocations do not leave the process — local development. */
#transport: EventsInvokeTransport | null = null;
@@ -395,11 +408,13 @@ export class EventsWorkerInvokerClient extends PuterClient {
const margin = Math.min(EVENTS_DEADLINE_MARGIN_MS, timeoutMs / 2);
const deadline =
Date.now() + timeoutMs * EVENTS_WORKER_LEASE_FACTOR - margin;
const send = (
const send = async (
deployed: boolean,
sendTimeoutMs = timeoutMs,
): Promise<EventsInvokeAnswer> =>
transport.send({
): Promise<EventsInvokeAnswer> => {
if (options.beforeSend && !(await options.beforeSend()))
return supersededAnswer();
return transport.send({
script: request.script,
appUid: request.appUid,
...(request.holder ? { holder: request.holder } : {}),
@@ -409,7 +424,11 @@ export class EventsWorkerInvokerClient extends PuterClient {
...(deployed ? { deployed: true } : {}),
...(options.lane ? { lane: options.lane } : {}),
...(stampEvent ? { render } : {}),
...(options.beforeSend
? { beforeSend: options.beforeSend }
: {}),
});
};
// A send may wait for a connection for half its timeout before the
// run's own timeout starts, so it is given what is left divided by that.
@@ -534,6 +553,8 @@ export class EventsWorkerInvokerClient extends PuterClient {
interface BatchedCall {
call: EventsInvokeCall;
resolve: (answer: EventsInvokeAnswer) => void;
/** For a `beforeSend` that threw. */
reject: (err: unknown) => void;
/** When its wait for a connection began, for what is left of it. */
waitingSince: number;
}
@@ -604,9 +625,14 @@ export class DispatcherInvokeTransport implements EventsInvokeTransport {
error: 'no free connection to the events dispatcher',
};
try {
if (this.#batchSize === 1) return await this.#sendOne(call);
return await new Promise<EventsInvokeAnswer>((resolve) => {
this.#batch.push({ call, resolve, waitingSince });
if (this.#batchSize === 1) {
// The wait for a connection may have outlived the call.
if (call.beforeSend && !(await call.beforeSend()))
return supersededAnswer();
return await this.#sendOne(call);
}
return await new Promise<EventsInvokeAnswer>((resolve, reject) => {
this.#batch.push({ call, resolve, reject, waitingSince });
if (this.#batch.length >= this.#batchSize) this.#flush();
else
this.#batchTimer ??= setTimeout(
@@ -626,6 +652,12 @@ export class DispatcherInvokeTransport implements EventsInvokeTransport {
const batch = this.#batch;
this.#batch = [];
if (batch.length === 0) return;
if (batch.some(({ call }) => call.beforeSend))
void this.#sendStillCurrent(batch);
else this.#sendGathered(batch);
}
#sendGathered(batch: BatchedCall[]): void {
if (batch.length === 1 || Date.now() < this.#batchUnsupportedUntil) {
for (const { call, resolve } of batch)
void this.#sendOne(call).then(resolve);
@@ -634,6 +666,26 @@ export class DispatcherInvokeTransport implements EventsInvokeTransport {
void this.#sendBatch(batch);
}
/** Drops calls superseded while they were gathered, then sends the rest. */
async #sendStillCurrent(batch: BatchedCall[]): Promise<void> {
const checked = await Promise.all(
batch.map(async (entry) => {
if (!entry.call.beforeSend) return entry;
try {
if (await entry.call.beforeSend()) return entry;
entry.resolve(supersededAnswer());
} catch (err) {
entry.reject(err);
}
return null;
}),
);
const current = checked.filter(
(entry): entry is BatchedCall => entry !== null,
);
if (current.length > 0) this.#sendGathered(current);
}
/** One call on the single route. */
async #sendOne(call: EventsInvokeCall): Promise<EventsInvokeAnswer> {
try {
@@ -688,13 +740,15 @@ export class DispatcherInvokeTransport implements EventsInvokeTransport {
let open = batch.length;
const answered = batch.map(() => false);
const timers: ReturnType<typeof setTimeout>[] = [];
const settle = (i: number, answer: EventsInvokeAnswer): void => {
const finish = (i: number, done: (entry: BatchedCall) => void) => {
if (answered[i]) return;
answered[i] = true;
clearTimeout(timers[i]);
batch[i].resolve(answer);
done(batch[i]);
if (--open === 0) hangUp.abort();
};
const settle = (i: number, answer: EventsInvokeAnswer): void =>
finish(i, (entry) => entry.resolve(answer));
const settleRest = (answer: EventsInvokeAnswer): void => {
for (let i = 0; i < batch.length; i++) settle(i, answer);
};
@@ -777,8 +831,22 @@ export class DispatcherInvokeTransport implements EventsInvokeTransport {
this.#batchUnsupportedUntil =
Date.now() + BATCH_UNSUPPORTED_RETRY_MS;
batch.forEach(({ call }, i) => {
if (!answered[i])
if (answered[i]) return;
const { beforeSend } = call;
if (!beforeSend) {
void this.#sendOne(call).then((a) => settle(i, a));
return;
}
// The batch round trip may have outlived the call.
void (async () => {
try {
if (!(await beforeSend()))
return settle(i, supersededAnswer());
} catch (err) {
return finish(i, (entry) => entry.reject(err));
}
settle(i, await this.#sendOne(call));
})();
});
return;
}
@@ -236,6 +236,17 @@ export class EventsController extends PuterController {
});
}
@Post('/schedules/update', {
subdomain: 'api',
requireAuth: true,
allowAccessToken: true,
})
async updateSchedule(req: Request, res: Response): Promise<void> {
const actor = this.#requireActor(req);
const { scheduleId, changes } = bodyRecord(req);
res.json(await this.#schedules().update(actor, scheduleId, changes));
}
/**
* POST /events/schedules/remove — end one schedule. An id the caller does
* not hold reads as absent.
@@ -75,6 +75,7 @@ const fakeSchedules = () =>
total: 3,
})),
get: vi.fn(async () => view),
update: vi.fn(async () => view),
remove: vi.fn(async () => {}),
receivePeerChanges: vi.fn(async () => ({ applied: 2 })),
prepareSendAt: vi.fn(),
@@ -129,6 +130,10 @@ const routes: Array<[string, (response: Response) => Promise<void>]> = [
(r) =>
events.getSchedule(req({ params: { scheduleId: SCHEDULE_ID } }), r),
],
[
'POST /events/schedules/update',
(r) => events.updateSchedule(req({ body: { scheduleId: SCHEDULE_ID, changes: { every: '2h' } } }), r),
],
[
'POST /events/schedules/remove',
(r) =>
@@ -250,6 +255,16 @@ describe('with services.schedules', () => {
expect(sent.body).toEqual(view);
});
it('updates through update and answers its view', async () => {
const schedules = fakeSchedules();
setSchedules(schedules);
const { response, sent } = res();
const changes = { every: '2h' };
await events.updateSchedule(req({ body: { scheduleId: SCHEDULE_ID, changes } }), response);
expect(schedules.update).toHaveBeenCalledWith(actor, SCHEDULE_ID, changes);
expect(sent.body).toEqual(view);
});
it('removes through remove and answers {}', async () => {
const schedules = fakeSchedules();
setSchedules(schedules);
@@ -0,0 +1,77 @@
import type { Request, Response } from 'express';
import { describe, expect, it } from 'vitest';
import { makeActor } from '../../core/actor.js';
import type {
ScheduleServiceContract,
ScheduleView,
} from '../../core/schedules.js';
import { EventsController } from './EventsController.js';
const actor = makeActor({
user: { id: 1, uuid: 'user-1', username: 'user1' } as never,
});
const request = (body: unknown) => ({ actor, body }) as Request;
const controller = (schedules?: Pick<ScheduleServiceContract, 'update'>) =>
new EventsController(
{} as never,
{} as never,
{} as never,
{ schedules } as never,
{} as never,
);
describe('update schedule route contract', () => {
it('answers events_disabled when schedules are unavailable', async () => {
await expect(
controller().updateSchedule(
request({ scheduleId: 'id', changes: {} }),
{} as Response,
),
).rejects.toMatchObject({
legacyCode: 'events_disabled',
statusCode: 503,
noAlarm: true,
});
});
it('passes the actor, id and changes through and returns the updated view', async () => {
const view = {
scheduleId: 'id',
kind: 'recurring',
state: 'suspended',
} as ScheduleView;
const changes = { cron: '0 9 * * *', tz: 'UTC' };
const calls: unknown[][] = [];
const events = controller({
update: async (...args) => {
calls.push(args);
return view;
},
});
let sent: unknown;
await events.updateSchedule(request({ scheduleId: 'id', changes }), {
json: (value: unknown) => {
sent = value;
},
} as Response);
expect(calls).toEqual([[actor, 'id', changes]]);
expect(sent).toBe(view);
});
it('preserves validation and sending errors from the implementation', async () => {
const error = {
code: 'events_schedule_sending',
message: 'Already sending',
};
await expect(
controller({
update: async () => {
throw error;
},
}).updateSchedule(
request({ scheduleId: 'id', changes: {} }),
{} as Response,
),
).rejects.toBe(error);
});
});
@@ -27,6 +27,7 @@ import type { Actor } from '../../core/actor.js';
import { PuterRouter } from '../../core/http/PuterRouter.js';
import { PuterServer } from '../../server.js';
import { setupTestServer } from '../../testUtil.js';
import { embedFrameAncestors } from './HomepageController.js';
// ── Test harness ────────────────────────────────────────────────────
//
@@ -529,3 +530,23 @@ describe('HomepageController GET /sw.js', () => {
expect(route?.options.subdomain).toBe('');
});
});
describe('embedFrameAncestors', () => {
const config = {
origin: 'https://example.test',
domain: 'example.test',
api_base_url: 'https://backend.example.test',
};
it('names a sibling subdomain that embeds the browser', () => {
expect(
embedFrameAncestors(config, 'https://sheets.example.test/doc/1'),
).toBe("'self' https://sheets.example.test");
});
it('refuses the configured API host even off the api label', () => {
expect(
embedFrameAncestors(config, 'https://backend.example.test/x'),
).toBe("'self'");
});
});
@@ -22,6 +22,11 @@ import path from 'node:path';
import { PuterController } from '../types.js';
import { toAppShellView } from '../../util/appShellView.js';
import type { PuterRouter } from '../../core/http/PuterRouter';
import {
isReservedSubdomain,
subdomainLabelOf,
} from '../../core/http/middleware/hostRedirects.js';
import type { IConfig } from '../../types';
import type {
PuterHomepageService,
PageMeta,
@@ -43,9 +48,9 @@ const APP_LANDING_LIMIT = {
};
/**
* App descriptions can run long; search snippets and social cards want a
* short blurb. Collapses whitespace, then cuts at a word boundary with an
* ellipsis so the result never exceeds `APP_META_DESCRIPTION_MAX`.
* App descriptions can run long; search snippets and social cards want a short
* blurb. Collapses whitespace, then cuts at a word boundary with an ellipsis so
* the result never exceeds `APP_META_DESCRIPTION_MAX`.
*/
export function appMetaDescription(text: string): string {
const clean = text.replace(/\s+/g, ' ').trim();
@@ -56,6 +61,53 @@ export function appMetaDescription(text: string): string {
return `${cut.replace(/[\s,;:.!?…—-]+$/, '')}…`;
}
/**
* `frame-ancestors` for the app browser embed. A wildcard over `*.<domain>`
* would also admit user-hosted sites whenever the hosting domains sit under it,
* so the embedding page is named exactly, from the request's referrer. Only a
* single label under `<domain>` qualifies, and not one this server answers
* itself (the API, WebDAV, SDK and native-app hosts).
*/
export function embedFrameAncestors(
config: Pick<
IConfig,
| 'origin'
| 'domain'
| 'api_base_url'
| 'static_hosting_domain'
| 'static_hosting_domain_alt'
| 'private_app_hosting_domain'
| 'private_app_hosting_domain_alt'
>,
referer: string | undefined,
): string {
const gui = new URL(config.origin ?? `https://${config.domain}`);
let parent: URL;
try {
parent = new URL(referer ?? '');
} catch {
return "'self'";
}
const host = parent.hostname.toLowerCase();
const label = subdomainLabelOf(host, config.domain);
const hostingDomains = [
config.static_hosting_domain,
config.static_hosting_domain_alt,
config.private_app_hosting_domain,
config.private_app_hosting_domain_alt,
]
.map((d) => (d ?? '').toLowerCase().split(':')[0])
.filter(Boolean);
const allowed =
parent.protocol === gui.protocol &&
parent.port === gui.port &&
label !== undefined &&
!isReservedSubdomain(label) &&
host !== new URL(config.api_base_url).hostname &&
!hostingDomains.some((d) => host === d || host.endsWith(`.${d}`));
return allowed ? `'self' ${parent.origin}` : "'self'";
}
/**
* Routes that render the Puter GUI shell, plus a catch-all static fallback
* under `<gui_assets_root>/src` for non-dist/src paths (images, fonts, lib
@@ -105,6 +157,27 @@ export class HomepageController extends PuterController {
router.get('/', {}, (req, res) => sendShell(req, res));
router.get('/embed/apps', {}, async (req, res) => {
// SDK bootstrap parameters must not change this page's account or API origin.
// Checked on the raw URL; `req.query` drops pairs past the parser's limit.
if (req.originalUrl.includes('?')) {
res.redirect('/embed/apps');
return;
}
res.removeHeader('X-Frame-Options');
res.setHeader(
'Content-Security-Policy',
`frame-ancestors ${embedFrameAncestors(this.config, req.get('referer'))}`,
);
res.setHeader('Cache-Control', 'no-store');
// Shown to visitors without a session; a signed-in embed fetches
// its own list, so a failure here only costs signed-out visitors.
const guestApps = await this.services.recommendedApps
.getRecommendedApps()
.catch((): Array<Record<string, unknown>> => []);
res.send(homepage.renderAppBrowserEmbed({ guestApps }));
});
router.get('/settings', {}, (req, res) => sendShell(req, res));
router.get('/settings/*splat', {}, (req, res) => sendShell(req, res));
@@ -244,7 +244,10 @@ describe('OIDCController GET /auth/oidc/providers', () => {
await callRoute('get', '/auth/oidc/providers', makeReq({}), res);
// We configured a single `custom` provider; the live service
// walks the config and returns its id.
expect(captured.body).toEqual({ providers: ['custom'] });
expect(captured.body).toEqual({
providers: ['custom'],
signupOidcOnly: false,
});
});
});
@@ -396,9 +396,12 @@ export class OIDCController extends PuterController {
},
},
async (_req: Request, res: Response) => {
const providers =
await this.services.oidc.getEnabledProviderIds();
res.json({ providers });
const [providers, policy] = await Promise.all([
this.services.oidc.getEnabledProviderIds(),
this.services.signup.signupPolicy(),
]);
// The signup window drops its email form when this is set.
res.json({ providers, signupOidcOnly: policy.oidcOnly });
},
);
@@ -201,6 +201,7 @@ describe('PuterAIController.openaiResponses parameter forwarding', () => {
context_management: [{ type: 'compaction' }],
prompt_cache_key: 'ck',
prompt_cache_retention: '24h',
prompt_cache_options: { ttl: '12h', prewarm: true },
store: true,
truncation: 'auto',
service_tier: 'default',
@@ -226,6 +227,9 @@ describe('PuterAIController.openaiResponses parameter forwarding', () => {
context_management: [{ type: 'compaction' }],
prompt_cache_key: 'ck',
prompt_cache_retention: '24h',
// Forwarded as sent; the provider keeps only the fields it
// accepts.
prompt_cache_options: { ttl: '12h', prewarm: true },
// forced false regardless of the client's request.
store: false,
truncation: 'auto',
@@ -359,13 +363,18 @@ describe('PuterAIController.openaiResponses input normalization', () => {
role: 'user',
content: [
'a bare string part',
{ type: 'input_text', text: 'typed text' },
{
type: 'input_text',
text: 'typed text',
prompt_cache_breakpoint: { mode: 'explicit', ttl: '12h' },
},
{ type: 'output_text', text: 'echoed text' },
{
type: 'input_image',
detail: 'high',
image_url: 'https://img.test/a.png',
file_id: 'file_img',
prompt_cache_breakpoint: { mode: 'explicit' },
},
{ type: 'input_audio', input_audio: { data: 'AAA' } },
{
@@ -386,13 +395,18 @@ describe('PuterAIController.openaiResponses input normalization', () => {
role: 'user',
content: [
{ type: 'text', text: 'a bare string part' },
{ type: 'text', text: 'typed text' },
{
type: 'text',
text: 'typed text',
prompt_cache_breakpoint: { mode: 'explicit', ttl: '12h' },
},
{ type: 'text', text: 'echoed text' },
{
type: 'image_url',
detail: 'high',
image_url: { url: 'https://img.test/a.png' },
file_id: 'file_img',
prompt_cache_breakpoint: { mode: 'explicit' },
},
{ type: 'input_audio', input_audio: { data: 'AAA' } },
{
@@ -639,6 +653,33 @@ describe('PuterAIController.openaiResponses output items', () => {
// -- /openai/v1/chat/completions edges -------------------------------
describe('PuterAIController.openaiChatCompletions translation edges', () => {
it('forwards the prompt-cache fields to the driver', async () => {
const completeSpy = stubChatComplete({
message: { role: 'assistant', content: 'ok' },
});
const { res } = makeRes();
await controller.openaiChatCompletions(
makeReq({
body: {
model: 'gpt-test',
messages: [{ role: 'user', content: 'hi' }],
prompt_cache_key: 'ck',
prompt_cache_retention: 'in_memory',
prompt_cache_options: { ttl: '30m' },
},
}),
res,
);
const args = completeSpy.mock.calls[0]![0] as Record<string, unknown>;
expect(args).toMatchObject({
prompt_cache_key: 'ck',
prompt_cache_retention: 'in_memory',
prompt_cache_options: { ttl: '30m' },
});
});
it('derives tool_calls from tool_use content blocks when the message has none', async () => {
stubChatComplete({
message: {
@@ -447,6 +447,7 @@ export class PuterAIController extends PuterController {
body.reasoning_effort as ICompleteArguments['reasoning_effort'],
}
: {}),
...promptCacheArgsFromBody(body),
...openaiCompatProvider(body),
...(body.fallback === false ? { fallback: false } : {}),
};
@@ -842,15 +843,7 @@ export class PuterAIController extends PuterController {
body.compaction as ICompleteArguments['compaction'],
}
: {}),
...(body.prompt_cache_key
? { prompt_cache_key: String(body.prompt_cache_key) }
: {}),
...(body.prompt_cache_retention
? {
prompt_cache_retention:
body.prompt_cache_retention as ICompleteArguments['prompt_cache_retention'],
}
: {}),
...promptCacheArgsFromBody(body),
// forced off regardless of what the caller sent — OpenAI
// defaults `store` to true, which would persist state server-side
// on OpenAI's end that this route gives no way to retrieve.
@@ -1343,6 +1336,33 @@ const openaiCompatProvider = (
return { provider: DEFAULTS.openaiChat };
};
/**
* The prompt-cache fields both OpenAI-shaped routes forward as sent; the
* provider checks the values.
*/
const promptCacheArgsFromBody = (
body: Record<string, unknown>,
): Pick<
ICompleteArguments,
'prompt_cache_key' | 'prompt_cache_retention' | 'prompt_cache_options'
> => ({
...(body.prompt_cache_key
? { prompt_cache_key: String(body.prompt_cache_key) }
: {}),
...(body.prompt_cache_retention
? {
prompt_cache_retention:
body.prompt_cache_retention as ICompleteArguments['prompt_cache_retention'],
}
: {}),
...(body.prompt_cache_options
? {
prompt_cache_options:
body.prompt_cache_options as ICompleteArguments['prompt_cache_options'],
}
: {}),
});
const randomId = (): string => crypto.randomUUID().replace(/-/g, '');
const generateId = (prefix: string): string => `${prefix}_${randomId()}`;
@@ -1590,12 +1610,24 @@ const parseJsonMaybe = (value: unknown): unknown => {
}
};
/** An OpenAI `prompt_cache_breakpoint` marker, kept on the translated part. */
const cacheBreakpointOf = (
p: Record<string, unknown>,
): { prompt_cache_breakpoint?: unknown } =>
p.prompt_cache_breakpoint && typeof p.prompt_cache_breakpoint === 'object'
? { prompt_cache_breakpoint: p.prompt_cache_breakpoint }
: {};
const normalizeContentPart = (part: unknown): Record<string, unknown> => {
if (typeof part === 'string') return { type: 'text', text: part };
if (!part || typeof part !== 'object') return { type: 'text', text: '' };
const p = part as Record<string, unknown>;
if (p.type === 'input_text' || p.type === 'output_text') {
return { type: 'text', text: String(p.text ?? '') };
return {
type: 'text',
text: String(p.text ?? ''),
...cacheBreakpointOf(p),
};
}
if (p.type === 'input_image') {
return {
@@ -1603,6 +1635,7 @@ const normalizeContentPart = (part: unknown): Record<string, unknown> => {
...(p.detail ? { detail: p.detail } : {}),
...(p.image_url ? { image_url: { url: p.image_url } } : {}),
...(p.file_id ? { file_id: p.file_id } : {}),
...cacheBreakpointOf(p),
};
}
if (p.type === 'input_audio')
@@ -1614,6 +1647,7 @@ const normalizeContentPart = (part: unknown): Record<string, unknown> => {
...(p.file_id ? { file_id: p.file_id } : {}),
...(p.file_url ? { file_url: p.file_url } : {}),
...(p.filename ? { filename: p.filename } : {}),
...cacheBreakpointOf(p),
};
}
return p;
@@ -187,9 +187,9 @@ describe('createUserSubdomainNotFound', () => {
});
it('passes through reserved subdomains (api, js, native apps, etc.)', () => {
// `api`, `js`, `dav`, `docs`, `developer`, `editor`, `pdf`,
// `api`, `js`, `dav`, `docs`, `developer`, `about`, `apps`,
// `puter-app-icons`, etc. all pass through.
for (const sub of ['api', 'js', 'docs', 'editor', 'puter-app-icons']) {
for (const sub of ['api', 'js', 'docs', 'about', 'puter-app-icons']) {
const { out, next } = run(
createUserSubdomainNotFound(config),
makeReq({
@@ -865,13 +865,13 @@ describe('createNativeAppStatic', () => {
beforeAll(() => {
root = mkdtempSync(nodePath.join(tmpdir(), 'native-apps-'));
mkdirSync(nodePath.join(root, 'editor', 'assets'), { recursive: true });
mkdirSync(nodePath.join(root, 'about', 'assets'), { recursive: true });
writeFileSync(
nodePath.join(root, 'editor', 'index.html'),
'<h1>editor</h1>',
nodePath.join(root, 'about', 'index.html'),
'<h1>about</h1>',
);
writeFileSync(
nodePath.join(root, 'editor', 'assets', 'app.js'),
nodePath.join(root, 'about', 'assets', 'app.js'),
'console.log(1);',
);
mkdirSync(nodePath.join(root, 'docs', 'dist'), { recursive: true });
@@ -893,7 +893,7 @@ describe('createNativeAppStatic', () => {
it('leaves a native app name alone on a host outside our domains', async () => {
const req = staticReq({
subdomains: ['com', 'example', 'editor'],
subdomains: ['com', 'example', 'about'],
path: '/index.html',
});
(req as Request & { is_custom_domain?: boolean }).is_custom_domain =
@@ -907,7 +907,7 @@ describe('createNativeAppStatic', () => {
const { out, next } = await runStatic(
createNativeAppStatic(config),
staticReq({
subdomains: ['localhost', 'puter', 'editor'],
subdomains: ['localhost', 'puter', 'about'],
path: '/index.html',
}),
);
@@ -915,17 +915,24 @@ describe('createNativeAppStatic', () => {
expect(next).toHaveBeenCalledTimes(1);
});
it('passes through subdomains that are not native apps', async () => {
const { out, next } = await runStatic(
withRoot(),
staticReq({
subdomains: ['localhost', 'puter', 'api'],
path: '/index.html',
}),
);
expect(out.sent).toBeUndefined();
expect(next).toHaveBeenCalledTimes(1);
});
// `editor`, `markus` and `pdf` are web apps on standard host routing,
// even with a folder of their own under the native apps root.
it.each(['api', 'editor', 'markus', 'pdf'])(
'passes %s through, as it is not a native app',
async (sub) => {
mkdirSync(nodePath.join(root, sub), { recursive: true });
writeFileSync(nodePath.join(root, sub, 'index.html'), sub);
const { out, next } = await runStatic(
withRoot(),
staticReq({
subdomains: ['localhost', 'puter', sub],
path: '/index.html',
}),
);
expect(out.sent).toBeUndefined();
expect(next).toHaveBeenCalledTimes(1);
},
);
it('passes through when there is no subdomain at all', async () => {
const { next } = await runStatic(
@@ -939,13 +946,13 @@ describe('createNativeAppStatic', () => {
const { out, next } = await runStatic(
withRoot(),
staticReq({
subdomains: ['localhost', 'puter', 'editor'],
subdomains: ['localhost', 'puter', 'about'],
path: '/index.html',
}),
);
expect(out.sent).toEqual({
path: '/index.html',
root: nodePath.join(root, 'editor'),
root: nodePath.join(root, 'about'),
});
expect(next).not.toHaveBeenCalled();
});
@@ -954,11 +961,11 @@ describe('createNativeAppStatic', () => {
const { out } = await runStatic(
withRoot(),
staticReq({
subdomains: ['localhost', 'puter', 'EDITOR'],
subdomains: ['localhost', 'puter', 'ABOUT'],
path: '/index.html',
}),
);
expect(out.sent?.root).toBe(nodePath.join(root, 'editor'));
expect(out.sent?.root).toBe(nodePath.join(root, 'about'));
});
it('serves docs out of its dist/ subdirectory, not the app root', async () => {
@@ -979,7 +986,7 @@ describe('createNativeAppStatic', () => {
const { out, next } = await runStatic(
withRoot(),
staticReq({
subdomains: ['localhost', 'puter', 'editor'],
subdomains: ['localhost', 'puter', 'about'],
path: '/assets',
originalUrl: '/assets?v=2',
}),
@@ -995,7 +1002,7 @@ describe('createNativeAppStatic', () => {
const { out } = await runStatic(
withRoot(),
staticReq({
subdomains: ['localhost', 'puter', 'editor'],
subdomains: ['localhost', 'puter', 'about'],
path: '/assets/',
}),
);
@@ -1007,7 +1014,7 @@ describe('createNativeAppStatic', () => {
const { out, next } = await runStatic(
withRoot(),
staticReq({
subdomains: ['localhost', 'puter', 'editor'],
subdomains: ['localhost', 'puter', 'about'],
path: '/nope.html',
}),
);
@@ -1019,7 +1026,7 @@ describe('createNativeAppStatic', () => {
const { next } = await runStatic(
withRoot(),
staticReq({
subdomains: ['localhost', 'puter', 'editor'],
subdomains: ['localhost', 'puter', 'about'],
path: '/index.html',
}),
new Error('send failed'),
@@ -1032,7 +1039,7 @@ describe('createNativeAppStatic', () => {
runStatic(
withRoot(),
staticReq({
subdomains: ['localhost', 'puter', 'editor'],
subdomains: ['localhost', 'puter', 'about'],
path: '/../../etc/passwd',
}),
),
@@ -27,15 +27,7 @@ import { HttpError, isHttpError } from '../HttpError';
import { isLocalWorkerHost } from './localWorkerProxy';
/** Native-app subdomains served via `nativeAppStatic`. */
const NATIVE_APP_SUBDOMAINS = [
'about',
'developer',
'docs',
'editor',
'markus',
'pdf',
'apps',
] as const;
const NATIVE_APP_SUBDOMAINS = ['about', 'developer', 'docs', 'apps'] as const;
/** Subset served out of a `dist/` subdirectory rather than the app root. */
const NATIVE_APPS_WITH_DIST = new Set(['docs', 'developer']);
@@ -111,6 +103,10 @@ export const subdomainLabelOf = (
return label && !label.includes('.') ? label : undefined;
};
/** Whether core itself answers `<name>.<domain>` (`www` redirects to the root). */
export const isReservedSubdomain = (name: string): boolean =>
name === 'www' || RESERVED_SUBDOMAINS.has(name);
/**
* Record a claim on `<name>.<domain>`. Throws on a malformed or uppercase
* label, a subdomain core already handles, or a name that is already claimed.
@@ -123,7 +119,7 @@ export const addClaimedSubdomain = (
if (typeof name !== 'string' || !SUBDOMAIN_LABEL.test(name)) {
throw new Error(`Invalid subdomain label: ${JSON.stringify(name)}`);
}
if (name === 'www' || RESERVED_SUBDOMAINS.has(name)) {
if (isReservedSubdomain(name)) {
throw new Error(`Subdomain '${name}' is reserved`);
}
if (claims.has(name)) {
+9 -1
View File
@@ -170,8 +170,16 @@ export interface ScheduleServiceContract {
request: ScheduleListRequest,
): Promise<PageResult<ScheduleView>>;
get(actor: Actor, scheduleId: unknown): Promise<ScheduleView>;
update(
actor: Actor,
scheduleId: unknown,
changes: unknown,
): Promise<ScheduleView>;
remove(actor: Actor, scheduleId: unknown): Promise<void>;
receivePeerChanges(body: unknown): Promise<{ applied: number }>;
/** `results`: each change's index outcome, in order (`null` if dropped). */
receivePeerChanges(
body: unknown,
): Promise<{ applied: number; results?: Array<number | null> }>;
/** Checks a caller supplying `sendAt`; `events_disabled` when off. */
prepareSendAt(actor: Actor, sendAt: unknown): Promise<DeferredSendPlan>;
createDeferredAppMessage(
@@ -449,6 +449,22 @@ describe('AzureResponsesProvider.complete request shape', () => {
expect(args.service_tier).toBe('default');
});
it('sends the wire spelling of the in-memory cache retention', async () => {
const provider = makeProvider();
responsesCreateMock.mockResolvedValueOnce(okResponse);
await withTestActor(() =>
provider.complete({
model: 'gpt-5.3-codex',
messages: [{ role: 'user', content: 'hi' }],
prompt_cache_retention: 'in-memory',
} as never),
);
const [args] = responsesCreateMock.mock.calls[0]!;
expect(args.prompt_cache_retention).toBe('in_memory');
});
it.each([
['previous_response_id', 'resp_1'],
['conversation', 'conv_1'],
@@ -35,6 +35,7 @@ import * as OpenAiUtil from '../../utils/OpenAIUtil.js';
import {
clampReasoningEffort,
openAICompatParams,
promptCacheRetentionToWire,
rejectStatefulResponsesFields,
} from '../../utils/openaiParams.js';
import { buildCostsOverride } from '../../utils/pricing.js';
@@ -209,6 +210,9 @@ export class AzureResponsesProvider implements IChatProvider {
const userIdentifier = upstreamUserIdentifier(actor);
// Cache key defaults to the actor identifier; see upstreamUserIdentifier.
const cacheKey = prompt_cache_key ?? userIdentifier;
const cacheRetention = promptCacheRetentionToWire(
prompt_cache_retention,
);
// Resolve any `puter_path` content parts into inline base64 data URLs
// before the Responses API sees them.
@@ -307,8 +311,8 @@ export class AzureResponsesProvider implements IChatProvider {
...(instructions !== undefined ? { instructions } : {}),
...(metadata !== undefined ? { metadata } : {}),
...(cacheKey !== undefined ? { prompt_cache_key: cacheKey } : {}),
...(prompt_cache_retention !== undefined
? { prompt_cache_retention }
...(cacheRetention !== undefined
? { prompt_cache_retention: cacheRetention }
: {}),
...(store !== undefined ? { store } : {}),
...(max_tokens !== undefined
@@ -23,6 +23,7 @@ import { HttpError } from '../../../../core/http/HttpError.js';
import type { MeteringService } from '../../../../services/metering/MeteringService.js';
import { upstreamUserIdentifier } from '../../../util/upstreamIdentifier.js';
import type { IChatModel, ICompleteArguments } from '../../types.js';
import { promptCacheRetentionToWire } from '../../utils/openaiParams.js';
import { sdkClientOptions } from '../../utils/sdkClient.js';
import {
type ChatProviderConfig,
@@ -107,12 +108,9 @@ export class MetaProvider extends OpenAICompatProvider {
? requestedEffort
: undefined;
// Puter spells the in-memory retention with a hyphen; Meta's enum
// uses an underscore.
const cacheRetention =
args.prompt_cache_retention === 'in-memory'
? 'in_memory'
: args.prompt_cache_retention;
const cacheRetention = promptCacheRetentionToWire(
args.prompt_cache_retention,
);
// The identifier is Puter's abuse attribution, so `custom` can't
// override it. Cache key defaults to it; see upstreamUserIdentifier.
@@ -249,6 +249,130 @@ describe('OpenAiChatProvider.complete argument validation', () => {
expect(result).toEqual({ delegated: true });
});
it('delegates a 12h cache lifetime to the Responses provider, which is the only API that writes it', async () => {
const { provider } = makeProvider();
const sibling = {
complete: vi.fn().mockResolvedValue({ delegated: true }),
};
provider.setResponsesProvider(
sibling as unknown as Parameters<
typeof provider.setResponsesProvider
>[0],
);
const params = {
model: 'gpt-6-luna',
messages: [{ role: 'user', content: 'hi' }],
prompt_cache_options: { ttl: '12h' as const },
};
const result = await withTestActor(() => provider.complete(params));
expect(createMock).not.toHaveBeenCalled();
expect(sibling.complete).toHaveBeenCalledWith(params);
expect(result).toEqual({ delegated: true });
});
it.each([
['30m', { mode: 'explicit', ttl: '30m' }],
['12h', { mode: 'explicit', ttl: '12h' }],
])(
'delegates a %s breakpoint lifetime to the Responses provider, since Chat Completions rejects the field',
async (_ttl, breakpoint) => {
const { provider } = makeProvider();
const sibling = {
complete: vi.fn().mockResolvedValue({ delegated: true }),
};
provider.setResponsesProvider(
sibling as unknown as Parameters<
typeof provider.setResponsesProvider
>[0],
);
const params = {
model: 'gpt-6-luna',
messages: [
{
role: 'system',
content: [
{
type: 'text',
text: 'long prefix',
prompt_cache_breakpoint: breakpoint,
},
],
},
{ role: 'user', content: 'hi' },
],
};
const result = await withTestActor(() =>
provider.complete(params as never),
);
expect(createMock).not.toHaveBeenCalled();
expect(sibling.complete).toHaveBeenCalledWith(params);
expect(result).toEqual({ delegated: true });
},
);
it('keeps a breakpoint without a lifetime on the Chat Completions path, intact', async () => {
const { provider } = makeProvider();
createMock.mockResolvedValueOnce({
choices: [
{
message: { content: 'ok', role: 'assistant' },
finish_reason: 'stop',
},
],
usage: { prompt_tokens: 1, completion_tokens: 1 },
});
await withTestActor(() =>
provider.complete({
model: 'gpt-6-luna',
messages: [
{
role: 'system',
content: [
{
type: 'text',
text: 'long prefix',
prompt_cache_breakpoint: { mode: 'explicit' },
},
],
},
{ role: 'user', content: 'hi' },
],
} as never),
);
const [args] = createMock.mock.calls[0]!;
expect(args.messages[0]).toMatchObject({
role: 'system',
content: [
{
type: 'text',
text: 'long prefix',
prompt_cache_breakpoint: { mode: 'explicit' },
},
],
});
});
it('throws 400 for a 12h cache lifetime without a Responses sibling provider', async () => {
const { provider } = makeProvider();
await expect(
withTestActor(() =>
provider.complete({
model: 'gpt-6-luna',
messages: [{ role: 'user', content: 'hi' }],
prompt_cache_options: { ttl: '12h' },
}),
),
).rejects.toMatchObject({ statusCode: 400 });
expect(createMock).not.toHaveBeenCalled();
});
it('requestPricing delegates to the Responses sibling (web_search\'s own hold), and no-ops without one', async () => {
const { provider } = makeProvider();
const args = {
@@ -349,6 +473,31 @@ describe('OpenAiChatProvider.complete request shape', () => {
expect(args.safety_identifier).toBe('puter-u42');
});
it('forwards 30m prompt_cache_options and the wire spelling of prompt_cache_retention', async () => {
const { provider } = makeProvider();
createMock.mockResolvedValueOnce(baseCompletion);
await withTestActor(() =>
provider.complete({
model: 'gpt-6-luna',
messages: [{ role: 'user', content: 'hello' }],
prompt_cache_retention: 'in-memory',
prompt_cache_options: {
mode: 'explicit',
ttl: '30m',
prewarm: true,
} as never,
}),
);
const [args] = createMock.mock.calls[0]!;
expect(args.prompt_cache_retention).toBe('in_memory');
expect(args.prompt_cache_options).toEqual({
mode: 'explicit',
ttl: '30m',
});
});
it.each([
'gpt-6-astra',
'gpt-6.1-sol',
@@ -665,7 +814,7 @@ describe('OpenAiChatProvider.complete non-stream output', () => {
},
});
await withTestActor(() =>
const result = await withTestActor(() =>
provider.complete({
model: 'gpt-6-luna',
messages: [{ role: 'user', content: 'hi' }],
@@ -685,6 +834,13 @@ describe('OpenAiChatProvider.complete non-stream output', () => {
cached_tokens: 1000 * Number(luna.costs.cached_tokens),
cache_write_tokens: 3000 * Number(luna.costs.cache_write_tokens),
});
// Chat Completions only writes OpenAI's default 30m entries.
expect((result as { usageDetails: unknown }).usageDetails).toEqual({
inputTokens: 1000,
outputTokens: 12,
cacheReadTokens: 1000,
cacheWrite30mTokens: 3000,
});
});
it('zeroes cached_tokens when prompt_tokens_details is missing', async () => {
@@ -35,7 +35,10 @@ import { make_openai_tools } from '../../utils/FunctionCalling.js';
import * as OpenAiUtil from '../../utils/OpenAIUtil.js';
import {
clampReasoningEffort,
messagesHaveCacheBreakpointTtl,
openAICompatParams,
promptCacheOptionsToWire,
promptCacheRetentionToWire,
} from '../../utils/openaiParams.js';
import { OPEN_AI_MODELS } from './models.js';
import type { OpenAiResponsesChatProvider } from './OpenAiChatResponsesProvider.js';
@@ -126,8 +129,11 @@ export class OpenAiChatProvider implements IChatProvider {
temperature,
text,
prompt_cache_key,
prompt_cache_retention,
prompt_cache_options,
} = params;
let { messages, model } = params;
const cacheOptions = promptCacheOptionsToWire(prompt_cache_options);
if (
tools?.filter((e) => isWebSearchTool(e as Record<string, unknown>))
.length
@@ -157,6 +163,21 @@ export class OpenAiChatProvider implements IChatProvider {
}
return await this.#responsesProvider.complete(params);
}
// Only the Responses API writes 12h cache entries or takes a lifetime
// on a `prompt_cache_breakpoint`; Chat Completions rejects both.
if (
cacheOptions?.ttl === '12h' ||
messagesHaveCacheBreakpointTtl(messages)
) {
if (!this.#responsesProvider) {
throw new HttpError(
400,
'a prompt cache lifetime requires the OpenAI Responses provider, which is not configured',
{ legacyCode: 'bad_request' },
);
}
return await this.#responsesProvider.complete(params);
}
// Validate messages
if (!Array.isArray(messages)) {
throw new HttpError(400, '`messages` must be an array', {
@@ -180,6 +201,9 @@ export class OpenAiChatProvider implements IChatProvider {
const userIdentifier = upstreamUserIdentifier(actor);
// Cache key defaults to the actor identifier; see upstreamUserIdentifier.
const cacheKey = prompt_cache_key ?? userIdentifier;
const cacheRetention = promptCacheRetentionToWire(
prompt_cache_retention,
);
// Strip Anthropic-only shape a fallback-replayed message can carry
// (thinking/server-tool blocks, cache_control, citations) before the
@@ -216,6 +240,12 @@ export class OpenAiChatProvider implements IChatProvider {
user: userIdentifier,
safety_identifier: userIdentifier,
...(cacheKey !== undefined ? { prompt_cache_key: cacheKey } : {}),
...(cacheRetention !== undefined
? { prompt_cache_retention: cacheRetention }
: {}),
...(cacheOptions !== undefined
? { prompt_cache_options: cacheOptions }
: {}),
messages: messages,
model: modelUsed.id,
...(mappedTools?.length ? { tools: mappedTools } : {}),
@@ -254,28 +284,11 @@ export class OpenAiChatProvider implements IChatProvider {
);
return OpenAiUtil.handle_completion_output({
usage_calculator: ({ usage, setUsageCosts }) => {
const cachedTokens =
usage.prompt_tokens_details?.cached_tokens ?? 0;
// GPT-5.6 and later bill cache writes at 1.25x input. They're
// reported inside `prompt_tokens`, like cached reads.
// The SDK doesn't type `cache_write_tokens` yet.
const cacheWriteTokens =
(
usage.prompt_tokens_details as
{ cache_write_tokens?: number } | undefined
)?.cache_write_tokens ?? 0;
const trackedUsage = {
prompt_tokens:
(usage.prompt_tokens ?? 0) -
cachedTokens -
cacheWriteTokens,
completion_tokens: usage.completion_tokens ?? 0,
cached_tokens: cachedTokens,
...(cacheWriteTokens
? { cache_write_tokens: cacheWriteTokens }
: {}),
};
usage_calculator: ({ usage, setUsageCosts, setUsageDetails }) => {
// GPT-5.6 and later report cache writes inside
// `prompt_tokens`, like cached reads; Chat Completions only
// writes the default 30m entries, billed at 1.25x input.
const trackedUsage = OpenAiUtil.splitCachedPrompt(usage);
const metered = meterChatUsage(
this.#meteringService,
@@ -285,6 +298,11 @@ export class OpenAiChatProvider implements IChatProvider {
trackedUsage,
);
setUsageCosts(metered.costs);
setUsageDetails(
OpenAiUtil.usageDetailsFromTrackedUsage(metered.usage, {
writeTtl: '30m',
})!,
);
return metered.usage;
},
stream,
@@ -246,6 +246,7 @@ describe('OpenAiResponsesChatProvider model catalog', () => {
prompt_tokens: input,
cached_tokens: cached,
cache_write_tokens: input * 1.25,
cache_write_12h_tokens: input * 2.5,
completion_tokens: output,
},
context: 1_050_000,
@@ -844,7 +845,7 @@ describe('OpenAiResponsesChatProvider.complete non-stream output', () => {
},
});
await withTestActor(() =>
const result = await withTestActor(() =>
provider.complete({
model: 'gpt-6-luna',
messages: [{ role: 'user', content: 'hi' }],
@@ -867,6 +868,115 @@ describe('OpenAiResponsesChatProvider.complete non-stream output', () => {
cached_tokens: 1000 * Number(luna.costs.cached_tokens),
cache_write_tokens: 3000 * Number(luna.costs.cache_write_tokens),
});
// Writes without a per-lifetime breakdown are OpenAI's default 30m
// entries, and the neutral breakdown says so.
expect((result as { usageDetails: unknown }).usageDetails).toEqual({
inputTokens: 1000,
outputTokens: 20,
cacheReadTokens: 1000,
cacheWrite30mTokens: 3000,
});
});
it('bills 12h cache writes at 2.5x input and the other writes at 1.25x', async () => {
const luna = OPEN_AI_MODELS.find((m) => m.id === 'gpt-6-luna')!;
const { provider } = makeProvider();
// One request can write both lifetimes: an explicit 12h breakpoint
// plus the implicit 30m one OpenAI adds at the end of the prompt.
responsesCreateMock.mockResolvedValueOnce({
output: [{ role: 'assistant' }],
output_text: 'ok',
usage: {
input_tokens: 6026,
output_tokens: 5,
input_tokens_details: {
cached_tokens: 0,
cache_write_tokens: 6023,
cache_write_tokens_details: {
ttl_12h_tokens: 6012,
ttl_30m_tokens: 11,
},
},
},
});
const result = await withTestActor(() =>
provider.complete({
model: 'gpt-6-luna',
messages: [{ role: 'user', content: 'hi' }],
prompt_cache_options: { ttl: '12h' },
}),
);
const [usage, , , overrides] = recordSpy.mock.calls[0]!;
expect(usage).toEqual({
prompt_tokens: 3,
completion_tokens: 5,
cached_tokens: 0,
cache_write_tokens: 11,
cache_write_12h_tokens: 6012,
});
expect(luna.costs.cache_write_12h_tokens).toBe(
Number(luna.costs.prompt_tokens) * 2.5,
);
expect(overrides).toEqual({
prompt_tokens: 3 * Number(luna.costs.prompt_tokens),
completion_tokens: 5 * Number(luna.costs.completion_tokens),
cached_tokens: 0,
cache_write_tokens: 11 * Number(luna.costs.cache_write_tokens),
cache_write_12h_tokens:
6012 * Number(luna.costs.cache_write_12h_tokens),
});
expect((result as { usageDetails: unknown }).usageDetails).toEqual({
inputTokens: 3,
outputTokens: 5,
cacheWrite30mTokens: 11,
cacheWrite12hTokens: 6012,
});
});
it('forwards prompt_cache_options and the wire spelling of prompt_cache_retention', async () => {
const { provider } = makeProvider();
responsesCreateMock.mockResolvedValueOnce({
output: [{ role: 'assistant' }],
output_text: 'ok',
usage: { input_tokens: 1, output_tokens: 1 },
});
await withTestActor(() =>
provider.complete({
model: 'gpt-6-luna',
messages: [{ role: 'user', content: 'hi' }],
prompt_cache_retention: 'in-memory',
prompt_cache_options: {
mode: 'explicit',
ttl: '12h',
comparison_response_id: 'resp_1',
} as never,
}),
);
const [args] = responsesCreateMock.mock.calls[0]!;
expect(args.prompt_cache_retention).toBe('in_memory');
expect(args.prompt_cache_options).toEqual({
mode: 'explicit',
ttl: '12h',
});
});
it('rejects a cache lifetime OpenAI does not offer before calling upstream', async () => {
const { provider } = makeProvider();
await expect(
withTestActor(() =>
provider.complete({
model: 'gpt-6-luna',
messages: [{ role: 'user', content: 'hi' }],
prompt_cache_options: { ttl: '2h' } as never,
}),
),
).rejects.toMatchObject({ statusCode: 400 });
expect(responsesCreateMock).not.toHaveBeenCalled();
});
it.each([
@@ -1068,6 +1178,69 @@ describe('OpenAiResponsesChatProvider.complete streaming', () => {
});
});
it('labels streamed cache writes by lifetime on the final usage chunk', async () => {
const luna = OPEN_AI_MODELS.find((m) => m.id === 'gpt-6-luna')!;
const { provider } = makeProvider();
responsesCreateMock.mockReturnValueOnce(
asAsyncIterable([
{ type: 'response.output_text.delta', delta: 'ok' },
{
type: 'response.completed',
response: {
usage: {
input_tokens: 6026,
output_tokens: 5,
input_tokens_details: {
cached_tokens: 0,
cache_write_tokens: 6023,
cache_write_tokens_details: {
ttl_12h_tokens: 6012,
ttl_30m_tokens: 11,
},
},
},
},
},
]),
);
const result = await withTestActor(() =>
provider.complete({
model: 'gpt-6-luna',
messages: [{ role: 'user', content: 'say ok' }],
stream: true,
prompt_cache_options: { ttl: '12h' },
}),
);
const harness = makeCapturingChatStream();
await (
result as {
init_chat_stream: (p: { chatStream: unknown }) => Promise<void>;
}
).init_chat_stream({ chatStream: harness.chatStream });
const usageEvent = harness.events().find((e) => e.type === 'usage');
expect(usageEvent?.usage).toEqual({
prompt_tokens: 3,
completion_tokens: 5,
cached_tokens: 0,
cache_write_tokens: 11,
cache_write_12h_tokens: 6012,
});
expect(usageEvent?.usageDetails).toEqual({
inputTokens: 3,
outputTokens: 5,
cacheWrite30mTokens: 11,
cacheWrite12hTokens: 6012,
});
const [, , , overrides] = recordSpy.mock.calls[0]!;
expect(overrides).toMatchObject({
cache_write_tokens: 11 * Number(luna.costs.cache_write_tokens),
cache_write_12h_tokens:
6012 * Number(luna.costs.cache_write_12h_tokens),
});
});
it('emits a tool_use block when response.output_item.done arrives with a function_call', async () => {
const { provider } = makeProvider();
responsesCreateMock.mockReturnValueOnce(
@@ -32,6 +32,8 @@ import * as OpenAiUtil from '../../utils/OpenAIUtil.js';
import {
clampReasoningEffort,
openAICompatParams,
promptCacheOptionsToWire,
promptCacheRetentionToWire,
rejectStatefulResponsesFields,
} from '../../utils/openaiParams.js';
import { responseSamplingParams } from '../../utils/responseSampling.js';
@@ -150,6 +152,7 @@ export class OpenAiResponsesChatProvider implements IChatProvider {
prompt,
prompt_cache_key,
prompt_cache_retention,
prompt_cache_options,
store,
top_p,
truncation,
@@ -195,6 +198,10 @@ export class OpenAiResponsesChatProvider implements IChatProvider {
const userIdentifier = upstreamUserIdentifier(actor);
// Cache key defaults to the actor identifier; see upstreamUserIdentifier.
const cacheKey = prompt_cache_key ?? userIdentifier;
const cacheRetention = promptCacheRetentionToWire(
prompt_cache_retention,
);
const cacheOptions = promptCacheOptionsToWire(prompt_cache_options);
if (tools) {
// Unravel tools to OpenAI Responses API format
@@ -288,8 +295,11 @@ export class OpenAiResponsesChatProvider implements IChatProvider {
...(instructions !== undefined ? { instructions } : {}),
...(metadata !== undefined ? { metadata } : {}),
...(cacheKey !== undefined ? { prompt_cache_key: cacheKey } : {}),
...(prompt_cache_retention !== undefined
? { prompt_cache_retention }
...(cacheRetention !== undefined
? { prompt_cache_retention: cacheRetention }
: {}),
...(cacheOptions !== undefined
? { prompt_cache_options: cacheOptions }
: {}),
...(store !== undefined ? { store } : {}),
...(max_tokens !== undefined
@@ -310,24 +320,17 @@ export class OpenAiResponsesChatProvider implements IChatProvider {
{ signal: Context.get('upstreamSignal') },
);
return OpenAiUtil.handle_completion_output_responses_api({
usage_calculator: ({ usage, webSearchCalls, setUsageCosts }) => {
const cachedTokens =
(usage as any).input_tokens_details?.cached_tokens ?? 0;
// GPT-5.6 and later bill cache writes at 1.25x input. They're
// reported inside `input_tokens`, like cached reads.
const cacheWriteTokens =
(usage as any).input_tokens_details?.cache_write_tokens ??
0;
usage_calculator: ({
usage,
webSearchCalls,
setUsageCosts,
setUsageDetails,
}) => {
// GPT-5.6 and later report cache writes inside `input_tokens`,
// like cached reads, split by lifetime (30m and 12h entries
// cost 1.25x and 2.5x input to write).
const trackedUsage: Record<string, number> = {
prompt_tokens:
((usage as any).input_tokens ?? 0) -
cachedTokens -
cacheWriteTokens,
completion_tokens: (usage as any).output_tokens ?? 0,
cached_tokens: cachedTokens,
...(cacheWriteTokens
? { cache_write_tokens: cacheWriteTokens }
: {}),
...OpenAiUtil.splitCachedPrompt(usage),
...(webSearchCalls
? { web_search_calls: webSearchCalls }
: {}),
@@ -356,6 +359,11 @@ export class OpenAiResponsesChatProvider implements IChatProvider {
: {},
);
setUsageCosts?.(metered.costs);
setUsageDetails?.(
OpenAiUtil.usageDetailsFromTrackedUsage(metered.usage, {
writeTtl: '30m',
})!,
);
return metered.usage;
},
stream,
@@ -23,6 +23,8 @@ import type { IChatModel } from '../../types.js';
// Prompts over 272K input tokens are billed at 2x input (cached reads and
// cache writes included) and 1.5x output for the full request.
// GPT-5.6 and later bill cache writes by the lifetime requested: 1.25x input
// for the default 30m entries, 2.5x for 12h entries.
const GPT_LONG_CONTEXT_PRICING = {
threshold: 272_000,
input_multiplier: 2,
@@ -49,6 +51,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
prompt_tokens: 100,
cached_tokens: 5,
cache_write_tokens: 100 * 1.25,
cache_write_12h_tokens: 100 * 2.5,
completion_tokens: 500,
},
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
@@ -74,6 +77,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
prompt_tokens: 200,
cached_tokens: 10,
cache_write_tokens: 200 * 1.25,
cache_write_12h_tokens: 200 * 2.5,
completion_tokens: 1000,
},
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
@@ -99,6 +103,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
prompt_tokens: 200,
cached_tokens: 20,
cache_write_tokens: 200 * 1.25,
cache_write_12h_tokens: 200 * 2.5,
completion_tokens: 1000,
},
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
@@ -124,6 +129,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
prompt_tokens: 10,
cached_tokens: 1,
cache_write_tokens: 10 * 1.25,
cache_write_12h_tokens: 10 * 2.5,
completion_tokens: 50,
},
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
@@ -149,6 +155,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
prompt_tokens: 1000,
cached_tokens: 100,
cache_write_tokens: 1000 * 1.25,
cache_write_12h_tokens: 1000 * 2.5,
completion_tokens: 5000,
},
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
@@ -174,6 +181,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
prompt_tokens: 400,
cached_tokens: 40,
cache_write_tokens: 400 * 1.25,
cache_write_12h_tokens: 400 * 2.5,
completion_tokens: 2000,
},
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
@@ -198,6 +206,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
prompt_tokens: 200,
cached_tokens: 20,
cache_write_tokens: 200 * 1.25,
cache_write_12h_tokens: 200 * 2.5,
completion_tokens: 1200,
},
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
@@ -222,6 +231,7 @@ export const OPEN_AI_MODELS: IChatModel[] = [
prompt_tokens: 20,
cached_tokens: 2,
cache_write_tokens: 20 * 1.25,
cache_write_12h_tokens: 20 * 2.5,
completion_tokens: 120,
},
long_context_pricing: GPT_LONG_CONTEXT_PRICING,
+22 -2
View File
@@ -145,13 +145,19 @@ export type StopReason =
| 'model_context_window_exceeded'
| (string & {});
/** Provider-neutral usage breakdown. `inputTokens` excludes cache reads/writes. */
/**
* Provider-neutral usage breakdown. `inputTokens` excludes cache reads/writes.
* Cache writes are keyed by the lifetime the entry was written with: Claude and
* Moonshot write 5m and 1h entries, OpenAI writes 30m and 12h entries.
*/
export interface UsageDetails {
inputTokens: number;
outputTokens: number; // includes reasoning
cacheReadTokens?: number;
cacheWrite5mTokens?: number;
cacheWrite30mTokens?: number;
cacheWrite1hTokens?: number;
cacheWrite12hTokens?: number;
reasoningTokens?: number;
webSearchRequests?: number;
webFetchRequests?: number;
@@ -168,10 +174,22 @@ export interface UsageDetails {
outputTokens: number;
cacheReadTokens?: number;
cacheWrite5mTokens?: number;
cacheWrite30mTokens?: number;
cacheWrite1hTokens?: number;
cacheWrite12hTokens?: number;
}>;
}
/**
* OpenAI `prompt_cache_options`, GPT-5.6 and later. `ttl` is the minimum
* lifetime of every cache entry the request writes; `12h` entries cost twice
* what `30m` entries do to write and only the Responses API writes them.
*/
export interface PromptCacheOptions {
mode?: 'implicit' | 'explicit';
ttl?: '30m' | '12h';
}
export interface ICompleteArguments {
messages: PuterMessage[];
provider?: string;
@@ -234,7 +252,9 @@ export interface ICompleteArguments {
metadata?: Record<string, string>;
prompt?: unknown;
prompt_cache_key?: string;
prompt_cache_retention?: 'in-memory' | '24h' | undefined;
/** `in-memory` is accepted as a spelling of OpenAI's `in_memory`. */
prompt_cache_retention?: 'in-memory' | 'in_memory' | '24h' | undefined;
prompt_cache_options?: PromptCacheOptions;
store?: boolean;
top_p?: number;
truncation?: 'auto' | 'disabled' | undefined;
+78 -20
View File
@@ -481,43 +481,86 @@ export const extractMeteredUsage = (usage) => {
};
/**
* Chat Completions usage as it's metered: `prompt_tokens` reports cached reads
* inside its count, so they come out of it and are priced only once, under
* `cached_tokens`.
* @typedef {{
* cached_tokens?: number | null;
* cache_write_tokens?: number | null;
* cache_write_tokens_details?: {
* ttl_30m_tokens?: number | null;
* ttl_12h_tokens?: number | null;
* } | null;
* }} OpenAiCacheDetails
*/
/**
* OpenAI-shaped usage as it's metered. The prompt count reports cached reads
* and cache writes inside it, so they come out of it and are priced once each
* under their own keys: `cached_tokens` for reads, `cache_write_tokens` for
* writes at the model's default lifetime and `cache_write_12h_tokens` for
* writes at OpenAI's 12h lifetime. Without the per-lifetime breakdown every
* write is a default-lifetime write: Chat Completions writes no 12h entries,
* and Responses reports the breakdown whenever a request asked for them. Reads
* the Chat Completions (`prompt_tokens`) and Responses (`input_tokens`)
* spellings alike.
*
* @param {import('openai/resources/completions.mjs').CompletionUsage | undefined} usage
* @param {{
* prompt_tokens?: number;
* completion_tokens?: number;
* prompt_tokens_details?: OpenAiCacheDetails | null;
* input_tokens?: number;
* output_tokens?: number;
* input_tokens_details?: OpenAiCacheDetails | null;
* }
* | undefined} usage
*/
export const splitCachedPrompt = (usage) => {
const details = usage?.prompt_tokens_details;
const details = usage?.prompt_tokens_details ?? usage?.input_tokens_details;
const cachedTokens = details?.cached_tokens ?? 0;
// Moonshot counts the prompt tokens it wrote to its prefix cache here;
// they're billed at the model's write rate, not the plain prompt rate.
// Moonshot and GPT-5.6+ count the prompt tokens they wrote to the prefix
// cache here; they're billed at the model's write rate, not the prompt rate.
const writeTokens =
typeof details?.cache_write_tokens === 'number'
? details.cache_write_tokens
: undefined;
const reported12h = details?.cache_write_tokens_details?.ttl_12h_tokens;
const write12hTokens =
typeof reported12h === 'number'
? Math.min(reported12h, writeTokens ?? 0)
: undefined;
return {
prompt_tokens:
(usage?.prompt_tokens ?? 0) - cachedTokens - (writeTokens ?? 0),
completion_tokens: usage?.completion_tokens ?? 0,
(usage?.prompt_tokens ?? usage?.input_tokens ?? 0) -
cachedTokens -
(writeTokens ?? 0),
completion_tokens:
usage?.completion_tokens ?? usage?.output_tokens ?? 0,
cached_tokens: cachedTokens,
...(writeTokens !== undefined
? { cache_write_tokens: writeTokens }
? { cache_write_tokens: writeTokens - (write12hTokens ?? 0) }
: {}),
...(write12hTokens !== undefined
? { cache_write_12h_tokens: write12hTokens }
: {}),
};
};
/**
* The `{prompt_tokens, completion_tokens, cached_tokens, cache_write_tokens}`
* shape every OpenAI-family `usage_calculator` already returns for billing,
* mapped to the provider-neutral `UsageDetails` the end-of-stream/non-stream
* result carries. `inputTokens`/`outputTokens` are the same numbers already
* billed — this never introduces a new figure, only exposes the breakdown.
* The `{prompt_tokens, completion_tokens, cached_tokens, cache_write_tokens,
* cache_write_12h_tokens}` shape every OpenAI-family `usage_calculator` already
* returns for billing, mapped to the provider-neutral `UsageDetails` the
* end-of-stream/non-stream result carries. `inputTokens`/`outputTokens` are the
* same numbers already billed — this never introduces a new figure, only
* exposes the breakdown.
*
* @param {Record<string, number> | undefined | null} trackedUsage
* @param {{ writeTtl?: '5m' | '30m' }} [opts] The lifetime `cache_write_tokens`
* were written with: 5m unless the provider says otherwise (OpenAI's default
* entries live 30m).
* @returns {import('../types.js').UsageDetails | undefined}
*/
export const usageDetailsFromTrackedUsage = (trackedUsage) => {
export const usageDetailsFromTrackedUsage = (
trackedUsage,
{ writeTtl = '5m' } = {},
) => {
if (!trackedUsage || typeof trackedUsage !== 'object') return undefined;
// OpenRouter, Infron and Ollama track `{prompt, completion,
// input_cache_read}` instead; any other shape is left to the driver.
@@ -526,12 +569,19 @@ export const usageDetailsFromTrackedUsage = (trackedUsage) => {
if (input === undefined && output === undefined) return undefined;
const cacheRead =
trackedUsage.cached_tokens ?? trackedUsage.input_cache_read;
const cacheWrite = trackedUsage.cache_write_tokens;
return {
inputTokens: input ?? 0,
outputTokens: output ?? 0,
...(cacheRead ? { cacheReadTokens: cacheRead } : {}),
...(trackedUsage.cache_write_tokens
? { cacheWrite5mTokens: trackedUsage.cache_write_tokens }
...(cacheWrite && writeTtl === '30m'
? { cacheWrite30mTokens: cacheWrite }
: {}),
...(cacheWrite && writeTtl !== '30m'
? { cacheWrite5mTokens: cacheWrite }
: {}),
...(trackedUsage.cache_write_12h_tokens
? { cacheWrite12hTokens: trackedUsage.cache_write_12h_tokens }
: {}),
};
};
@@ -864,12 +914,16 @@ export const create_chat_stream_handler_responses_api =
// Missing usage is left undefined rather than fed to the calculator —
// see the sibling handler above, including why usage is reported
// before the block flushes.
let usageDetails;
const usage = last_usage
? usage_calculator({
usage: last_usage,
webSearchCalls,
tool_usage: toolUsage,
setUsageCosts: (c) => chatStream.setUsageCosts(c),
setUsageDetails: (d) => {
usageDetails = d;
},
})
: undefined;
chatStream.reportUsage(usage);
@@ -877,7 +931,7 @@ export const create_chat_stream_handler_responses_api =
reason:
incompleteReason ?? (sawFunctionCall ? 'tool_use' : 'end_turn'),
});
const usageDetails = usageDetailsFromTrackedUsage(usage);
usageDetails ??= usageDetailsFromTrackedUsage(usage);
if (usageDetails) chatStream.setUsageDetails(usageDetails);
textblock.end();
@@ -1015,6 +1069,7 @@ export const handle_completion_output = async (
* webSearchCalls?: number;
* tool_usage?: Record<string, unknown>;
* setUsageCosts?: (costs: Record<string, number>) => void;
* setUsageDetails?: (details: import('../types').UsageDetails) => void;
* }) => unknown} params.usage_calculator
* @param {() => Promise<void>} [params.finally_fn]
* @returns {ReturnType<import('../types').IChatProvider['complete']>}
@@ -1156,12 +1211,15 @@ export const handle_completion_output_responses_api = async ({
setUsageCosts: (c) => {
ret.usageCosts = c;
},
setUsageDetails: (d) => {
ret.usageDetails = d;
},
})
: {
input_tokens: completion.usage.input_tokens,
output_tokens: completion.usage.output_tokens,
};
if (usage_calculator) {
if (usage_calculator && ret.usageDetails === undefined) {
const usageDetails = usageDetailsFromTrackedUsage(ret.usage);
if (usageDetails) ret.usageDetails = usageDetails;
}
@@ -247,6 +247,40 @@ describe('usageDetailsFromTrackedUsage', () => {
});
});
it('labels cache writes by the lifetime the provider says it wrote them with', () => {
expect(
usageDetailsFromTrackedUsage(
{
prompt_tokens: 10,
completion_tokens: 5,
cached_tokens: 3,
cache_write_tokens: 11,
cache_write_12h_tokens: 6012,
},
{ writeTtl: '30m' },
),
).toEqual({
inputTokens: 10,
outputTokens: 5,
cacheReadTokens: 3,
cacheWrite30mTokens: 11,
cacheWrite12hTokens: 6012,
});
// Zero counts, as OpenAI reports on a cache hit, add no field.
expect(
usageDetailsFromTrackedUsage(
{
prompt_tokens: 10,
completion_tokens: 5,
cached_tokens: 3,
cache_write_tokens: 0,
cache_write_12h_tokens: 0,
},
{ writeTtl: '30m' },
),
).toEqual({ inputTokens: 10, outputTokens: 5, cacheReadTokens: 3 });
});
it('returns undefined for a missing/non-object usage', () => {
expect(usageDetailsFromTrackedUsage(undefined)).toBeUndefined();
expect(usageDetailsFromTrackedUsage(null)).toBeUndefined();
@@ -962,6 +996,66 @@ describe('splitCachedPrompt', () => {
cached_tokens: 3,
});
});
it('reads the Responses usage spelling', () => {
expect(
splitCachedPrompt({
input_tokens: 6026,
output_tokens: 5,
input_tokens_details: { cached_tokens: 0, cache_write_tokens: 6023 },
}),
).toEqual({
prompt_tokens: 3,
completion_tokens: 5,
cached_tokens: 0,
cache_write_tokens: 6023,
});
});
it('splits 12h writes out of the write count when the breakdown is reported', () => {
// One request can write both lifetimes: an explicit 12h breakpoint
// plus the implicit 30m one OpenAI adds at the end of the prompt.
expect(
splitCachedPrompt({
input_tokens: 6026,
output_tokens: 5,
input_tokens_details: {
cached_tokens: 0,
cache_write_tokens: 6023,
cache_write_tokens_details: {
ttl_12h_tokens: 6012,
ttl_30m_tokens: 11,
},
},
}),
).toEqual({
prompt_tokens: 3,
completion_tokens: 5,
cached_tokens: 0,
cache_write_tokens: 11,
cache_write_12h_tokens: 6012,
});
});
it('never reports more 12h writes than writes in total', () => {
expect(
splitCachedPrompt({
input_tokens: 100,
output_tokens: 1,
input_tokens_details: {
cached_tokens: 0,
cache_write_tokens: 40,
cache_write_tokens_details: { ttl_12h_tokens: 50 },
},
}),
).toEqual({
prompt_tokens: 60,
completion_tokens: 1,
cached_tokens: 0,
cache_write_tokens: 0,
cache_write_12h_tokens: 40,
});
});
});
// ── recorded usage costs ────────────────────────────────────────────
@@ -22,11 +22,14 @@ import { describe, expect, it } from 'vitest';
import type { ICompleteArguments } from '../types.js';
import {
clampReasoningEffort,
messagesHaveCacheBreakpointTtl,
openAICompatParams,
outputFormatFromResponseFormat,
outputFormatFromResponsesText,
outputFormatToResponseFormat,
outputFormatToResponsesText,
promptCacheOptionsToWire,
promptCacheRetentionToWire,
rejectStatefulResponsesFields,
toolChoiceFromWire,
toolChoiceToWire,
@@ -334,3 +337,109 @@ describe('rejectStatefulResponsesFields', () => {
).toThrow(/previous_response_id/);
});
});
describe('promptCacheRetentionToWire', () => {
it('spells the in-memory retention the way the upstreams expect', () => {
expect(promptCacheRetentionToWire('in-memory')).toBe('in_memory');
expect(promptCacheRetentionToWire('in_memory')).toBe('in_memory');
expect(promptCacheRetentionToWire('24h')).toBe('24h');
expect(promptCacheRetentionToWire(undefined)).toBeUndefined();
});
});
describe('promptCacheOptionsToWire', () => {
it('forwards mode and ttl and nothing else', () => {
expect(
promptCacheOptionsToWire({
mode: 'explicit',
ttl: '12h',
prewarm: true,
comparison_response_id: 'resp_1',
}),
).toEqual({ mode: 'explicit', ttl: '12h' });
expect(promptCacheOptionsToWire({ ttl: '30m' })).toEqual({
ttl: '30m',
});
});
it('sends nothing for an absent or empty object', () => {
expect(promptCacheOptionsToWire(undefined)).toBeUndefined();
expect(promptCacheOptionsToWire(null)).toBeUndefined();
expect(promptCacheOptionsToWire({})).toBeUndefined();
expect(promptCacheOptionsToWire({ prewarm: true })).toBeUndefined();
});
it('rejects a lifetime or mode OpenAI does not offer', () => {
expect(() => promptCacheOptionsToWire({ ttl: '2h' })).toThrow(
expect.objectContaining({ statusCode: 400 }),
);
expect(() => promptCacheOptionsToWire({ ttl: '24h' })).toThrow(
/30m.*12h/,
);
expect(() => promptCacheOptionsToWire({ mode: 'auto' })).toThrow(
/implicit.*explicit/,
);
expect(() => promptCacheOptionsToWire('12h')).toThrow(
/must be an object/,
);
});
});
describe('messagesHaveCacheBreakpointTtl', () => {
it('finds a breakpoint lifetime on any content part', () => {
expect(
messagesHaveCacheBreakpointTtl([
{ role: 'user', content: 'plain' },
{
role: 'system',
content: [
{ type: 'text', text: 'a' },
{
type: 'text',
text: 'b',
prompt_cache_breakpoint: {
mode: 'explicit',
ttl: '30m',
},
},
],
},
]),
).toBe(true);
expect(
messagesHaveCacheBreakpointTtl([
{
role: 'user',
content: {
type: 'text',
text: 'a',
prompt_cache_breakpoint: { ttl: '12h' },
},
},
]),
).toBe(true);
});
it('ignores breakpoints without a lifetime and odd message shapes', () => {
expect(
messagesHaveCacheBreakpointTtl([
{
role: 'system',
content: [
{
type: 'text',
text: 'a',
prompt_cache_breakpoint: { mode: 'explicit' },
},
'bare string part',
null,
],
},
{ role: 'user', content: null },
null,
]),
).toBe(false);
expect(messagesHaveCacheBreakpointTtl(undefined)).toBe(false);
expect(messagesHaveCacheBreakpointTtl('nope')).toBe(false);
});
});
@@ -29,6 +29,7 @@ import { HttpError } from '../../../core/http/HttpError.js';
import type {
ICompleteArguments,
OutputFormat,
PromptCacheOptions,
ReasoningEffort,
ToolChoice,
} from '../types.js';
@@ -305,6 +306,80 @@ export const openAICompatParams = (
return out;
};
// -- prompt caching ----------------------------------------------------------
/**
* The wire spelling of `prompt_cache_retention`. Puter also accepts the
* hyphenated `in-memory`, which every OpenAI-shaped upstream rejects.
*/
export const promptCacheRetentionToWire = (
retention: ICompleteArguments['prompt_cache_retention'],
): 'in_memory' | '24h' | undefined =>
retention === 'in-memory' ? 'in_memory' : retention;
const PROMPT_CACHE_MODES = new Set(['implicit', 'explicit']);
const PROMPT_CACHE_TTLS = new Set(['30m', '12h']);
/**
* `prompt_cache_options` as forwarded upstream: `mode` and `ttl` only, each
* checked against the values OpenAI accepts, so a bad value fails here with a
* clear message instead of as an upstream 400 the fallback loop may hide.
* `comparison_response_id` names a stored response, which Puter never keeps,
* and `prewarm` is dropped with it.
*/
export const promptCacheOptionsToWire = (
options: unknown,
): PromptCacheOptions | undefined => {
if (options === undefined || options === null) return undefined;
if (typeof options !== 'object' || Array.isArray(options)) {
throw new HttpError(400, '`prompt_cache_options` must be an object', {
legacyCode: 'bad_request',
});
}
const { mode, ttl } = options as Record<string, unknown>;
if (mode !== undefined && !PROMPT_CACHE_MODES.has(mode as string)) {
throw new HttpError(
400,
'`prompt_cache_options.mode` must be `implicit` or `explicit`',
{ legacyCode: 'bad_request' },
);
}
if (ttl !== undefined && !PROMPT_CACHE_TTLS.has(ttl as string)) {
throw new HttpError(
400,
'`prompt_cache_options.ttl` must be `30m` or `12h`',
{ legacyCode: 'bad_request' },
);
}
const wire: PromptCacheOptions = {
...(mode !== undefined
? { mode: mode as PromptCacheOptions['mode'] }
: {}),
...(ttl !== undefined ? { ttl: ttl as PromptCacheOptions['ttl'] } : {}),
};
return Object.keys(wire).length ? wire : undefined;
};
/**
* Whether any content part carries a `prompt_cache_breakpoint` with its own
* `ttl`. Chat Completions rejects the field whatever its value; only the
* Responses API honours a per-breakpoint lifetime.
*/
export const messagesHaveCacheBreakpointTtl = (messages: unknown): boolean =>
Array.isArray(messages) &&
messages.some((message) => {
const content = (message as { content?: unknown } | null)?.content;
const parts = Array.isArray(content) ? content : [content];
return parts.some(
(part) =>
(
part as {
prompt_cache_breakpoint?: { ttl?: unknown } | null;
} | null
)?.prompt_cache_breakpoint?.ttl !== undefined,
);
});
// -- Responses server-side state ---------------------------------------------
const STATEFUL_RESPONSES_FIELDS = [
@@ -463,10 +463,26 @@ describe('ImageGenerationDriver.generate provider routing', () => {
driver.generate({ prompt: 'a landscape' }),
);
expect(openaiImagesGenerateMock).toHaveBeenCalledWith(
expect.objectContaining({ model: 'gpt-image-2' }),
expect.objectContaining({ model: 'gpt-image-2.6-flare' }),
);
});
it.each(['gpt-image-2.6-flare', 'gpt-image-2.6-sunburst'])(
'routes %s and its OpenAI alias', async (model) => {
for (const id of [model, `openai/${model}`]) {
openaiImagesGenerateMock.mockResolvedValueOnce({
data: [{ url: 'https://oai/image.png' }],
});
await withDriverName('ai-image', () => driver.generate({
model: id, prompt: 'a landscape',
}));
expect(openaiImagesGenerateMock).toHaveBeenLastCalledWith(
expect.objectContaining({ model }),
);
}
},
);
it('rejects an excluded model regardless of caller casing', async () => {
await expect(withActor(() =>
driver.generate({
@@ -742,7 +758,10 @@ describe('ImageGenerationDriver.generate provider routing', () => {
}),
);
const reserveSpy = vi.spyOn(server.services.metering, 'reserveCredits');
const reserveSpy = vi.spyOn(
server.services.metering,
'reserveCreditsIfAffordable',
);
const usageSpy = vi.spyOn(
server.services.metering,
'batchIncrementUsages',
@@ -836,7 +855,7 @@ describe('ImageGenerationDriver.generate provider routing', () => {
});
const reserveSpy = vi.spyOn(
server.services.metering,
'reserveCredits',
'reserveCreditsIfAffordable',
);
// Cloudflare answers the scheduled refresh with new rates at once;
@@ -1146,8 +1165,8 @@ describe('ImageGenerationDriver.generate Puter file inputs', () => {
);
beforeEach(() => {
vi.spyOn(server.services.metering, 'hasEnoughCredits').mockResolvedValue(
true,
vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable').mockImplementation((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
);
});
@@ -1649,7 +1668,10 @@ it.each([undefined, null, '', ' ', 42, {}])(
describe('ImageGenerationDriver cost factor', () => {
it('holds and meters at the AI cost factor', async () => {
const reserveSpy = vi.spyOn(server.services.metering, 'reserveCredits');
const reserveSpy = vi.spyOn(
server.services.metering,
'reserveCreditsIfAffordable',
);
const usageSpy = vi.spyOn(
server.services.metering,
'batchIncrementUsages',
@@ -72,7 +72,7 @@ vi.mock('openai', () => {
// -- Test harness ----------------------------------------------------
let server: PuterServer;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let batchIncrementUsagesSpy: MockInstance<
MeteringService['batchIncrementUsages']
>;
@@ -107,7 +107,7 @@ const makeProvider = (
beforeEach(() => {
generateMock.mockReset();
openAICtor.mockReset();
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
batchIncrementUsagesSpy = vi.spyOn(
server.services.metering,
'batchIncrementUsages',
@@ -177,7 +177,7 @@ describe('BytePlusImageProvider.generate gates', () => {
expect(result).toBe(
'https://puter-sample-data.puter.site/image_example.png',
);
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(generateMock).not.toHaveBeenCalled();
});
@@ -196,7 +196,7 @@ describe('BytePlusImageProvider.generate gates', () => {
});
it('throws 402 BEFORE hitting ModelArk when the actor lacks credits', async () => {
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() => makeProvider().generate({ prompt: 'hi' })),
).rejects.toMatchObject({ statusCode: 402 });
@@ -713,7 +713,7 @@ it.each([
}),
),
).rejects.toMatchObject({ statusCode: 400, legacyCode: 'bad_request' });
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(generateMock).not.toHaveBeenCalled();
});
@@ -73,7 +73,7 @@ let server: PuterServer;
let globalFetchSpy: MockInstance<typeof fetch>;
let fetchSpy: Mock<typeof fetch>;
let modelSearchFetch: Mock<typeof fetch>;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let batchIncrementUsagesSpy: MockInstance<
MeteringService['batchIncrementUsages']
>;
@@ -123,7 +123,7 @@ beforeEach(() => {
? modelSearchFetch(input, init)
: fetchSpy(input, init),
);
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
batchIncrementUsagesSpy = vi.spyOn(
server.services.metering,
'batchIncrementUsages',
@@ -188,7 +188,7 @@ describe('CloudflareImageProvider.generate test_mode', () => {
expect(result).toBe(
'https://puter-sample-data.puter.site/image_example.png',
);
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
});
});
@@ -210,7 +210,7 @@ describe('CloudflareImageProvider.generate argument validation', () => {
describe('CloudflareImageProvider.generate credit gate', () => {
it('throws 402 BEFORE hitting Cloudflare when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() => provider.generate({ prompt: 'hi' })),
@@ -240,7 +240,7 @@ describe('CloudflareImageProvider.generate credit gate', () => {
actor,
);
const [, estimate] = hasCreditsSpy.mock.calls[0]!;
const [, estimate] = admitSpy.mock.calls[0]!;
expect(estimate).toBeGreaterThan(0);
expect(heldDuringCall).toBe(Math.ceil(estimate));
// The hold stays until the usage write lands.
@@ -708,7 +708,7 @@ describe('Cloudflare legacy image option', () => {
legacyCode: 'bad_request',
});
expect(fetchSpy).not.toHaveBeenCalled();
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
},
);
@@ -726,7 +726,7 @@ describe('Cloudflare legacy image option', () => {
message: 'Invalid input image (empty or not base64)',
});
expect(fetchSpy).not.toHaveBeenCalled();
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
});
});
@@ -823,12 +823,12 @@ describe('Cloudflare priced legacy models', () => {
imageSize: { kind: 'pixels', w: 512, h: 512 },
}));
expect(JSON.parse(fetchSpy.mock.calls[0]![1]!.body as string)).toMatchObject({ image: [1, 2, 3], mask: [4, 5, 6], num_steps: 20, width: 512, height: 512 });
expect(hasCreditsSpy).toHaveBeenCalledWith(expect.anything(), 0);
expect(admitSpy).toHaveBeenCalledWith(expect.anything(), 0, undefined);
});
it('rejects missing inpainting inputs before credit checks', async () => {
await expect(withTestActor(() => makeProvider().generate({ model: '@cf/runwayml/stable-diffusion-v1-5-inpainting', prompt: 'A cup' }))).rejects.toMatchObject({ statusCode: 400 });
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
});
@@ -844,7 +844,7 @@ describe('Cloudflare priced legacy models', () => {
it('charges Lucid at the price returned by the model API', async () => {
fetchSpy.mockResolvedValueOnce(new Response(new Uint8Array([1, 2, 3]), { headers: { 'content-type': 'image/png' } }));
await withTestActor(() => makeProvider().generate({ model: '@cf/leonardo/lucid-origin', prompt: 'A cup', imageSize: { kind: 'pixels', w: 512, h: 512 }, steps: 1 }));
expect(hasCreditsSpy).toHaveBeenCalledWith(expect.anything(), 713200);
expect(admitSpy).toHaveBeenCalledWith(expect.anything(), 713200, undefined);
});
});
@@ -897,9 +897,10 @@ describe('Cloudflare model-search pricing', () => {
await runPhoenix(provider);
expect(listed?.costs).toEqual({ tile_512: 1_000_000, step: 20_200 });
expect(hasCreditsSpy).toHaveBeenCalledWith(
expect(admitSpy).toHaveBeenCalledWith(
expect.anything(),
4 * 1_000_000 + 10 * 20_200,
undefined,
);
expect(batchIncrementUsagesSpy.mock.calls[0]![1]).toEqual([
expect.objectContaining({
@@ -930,9 +931,10 @@ describe('Cloudflare model-search pricing', () => {
await runPhoenix(makeProvider());
expect(modelSearchFetch).not.toHaveBeenCalled();
expect(hasCreditsSpy).toHaveBeenCalledWith(
expect(admitSpy).toHaveBeenCalledWith(
expect.anything(),
4 * 583_000 + 10 * 11_000,
undefined,
);
});
@@ -955,7 +957,7 @@ describe('Cloudflare model-search pricing', () => {
await refresh;
await runPhoenix(provider);
expect(hasCreditsSpy.mock.calls.map(([, cost]) => cost)).toEqual([
expect(admitSpy.mock.calls.map(([, cost]) => cost)).toEqual([
2_442_000, 4_202_000,
]);
});
@@ -977,9 +979,10 @@ describe('Cloudflare model-search pricing', () => {
await runPhoenix(provider);
expect(modelSearchFetch).toHaveBeenCalledTimes(2);
expect(hasCreditsSpy).toHaveBeenCalledWith(
expect(admitSpy).toHaveBeenCalledWith(
expect.anything(),
4_202_000,
undefined,
);
});
@@ -1015,7 +1018,7 @@ describe('Cloudflare model-search pricing', () => {
steps: 10,
} as never),
);
return hasCreditsSpy.mock.lastCall![1];
return admitSpy.mock.lastCall![1];
};
const settle = () => new Promise((resolve) => setImmediate(resolve));
const OLD = { tile_512: 583_000, step: 11_000 };
@@ -1069,9 +1072,10 @@ describe('Cloudflare model-search pricing', () => {
}),
);
expect(modelSearchFetch).toHaveBeenCalledOnce();
expect(hasCreditsSpy).toHaveBeenCalledWith(
expect(admitSpy).toHaveBeenCalledWith(
expect.anything(),
4 * 5280 + 4 * 10560,
undefined,
);
});
@@ -82,7 +82,7 @@ vi.mock('../../inputImage.js', async (orig) => ({
// ── Test harness ────────────────────────────────────────────────────
let server: PuterServer;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
let batchIncrementUsagesSpy: MockInstance<
MeteringService['batchIncrementUsages']
@@ -111,7 +111,7 @@ beforeEach(() => {
generateContentMock.mockReset();
toBase64DataUriMock.mockReset();
googleAICtor.mockReset();
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
batchIncrementUsagesSpy = vi.spyOn(
server.services.metering,
@@ -167,7 +167,7 @@ describe('GeminiImageProvider.generate test_mode', () => {
expect(result).toBe(
'https://puter-sample-data.puter.site/image_example.png',
);
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(generateContentMock).not.toHaveBeenCalled();
});
});
@@ -219,7 +219,7 @@ describe('GeminiImageProvider.generate argument validation', () => {
legacyCode: 'bad_request',
message: expect.stringContaining('Unsupported quality tier'),
});
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(generateContentMock).not.toHaveBeenCalled();
},
);
@@ -427,7 +427,7 @@ describe('GeminiImageProvider.generate Flash path (generateContent)', () => {
it('throws 402 BEFORE hitting Gemini when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() =>
@@ -466,7 +466,7 @@ describe('GeminiImageProvider.generate Flash path (generateContent)', () => {
actor,
);
const [, estimate] = hasCreditsSpy.mock.calls[0]!;
const [, estimate] = admitSpy.mock.calls[0]!;
expect(estimate).toBeGreaterThan(0);
expect(heldDuringCall).toBe(Math.ceil(estimate));
// The hold stays until the usage write lands.
@@ -643,7 +643,7 @@ describe('Gemini Nano Banana image generation', () => {
const request = generateContentMock.mock.calls[0][0];
expect(request.config.imageConfig.imageSize).toBe('1K');
// Input + 1120 image tokens at $30/1M + 1000 text tokens at $7.50/1M.
expect(hasCreditsSpy).toHaveBeenCalledWith(expect.anything(), 4_194_450);
expect(admitSpy).toHaveBeenCalledWith(expect.anything(), 4_194_450, undefined);
expect(batchIncrementUsagesSpy).toHaveBeenCalledWith(
expect.anything(),
expect.arrayContaining([
@@ -50,7 +50,7 @@ import { KlingImageProvider } from './KlingImageProvider.js';
let server: PuterServer;
let fetchSpy: MockInstance<typeof fetch>;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
beforeAll(async () => {
@@ -73,7 +73,7 @@ const makeProvider = () =>
beforeEach(() => {
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
incrementUsageSpy.mockResolvedValue({} as never);
});
@@ -318,7 +318,7 @@ describe('KlingImageProvider billing', () => {
});
it('rejects before calling Kling when credits run short', async () => {
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(generate({})).rejects.toMatchObject({ statusCode: 402 });
expect(fetchSpy).not.toHaveBeenCalled();
});
@@ -97,7 +97,7 @@ vi.mock('../../inputImage.js', async (orig) => ({
// ── Test harness ────────────────────────────────────────────────────
let server: PuterServer;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let batchIncrementUsagesSpy: MockInstance<
MeteringService['batchIncrementUsages']
>;
@@ -126,7 +126,7 @@ beforeEach(() => {
editMock.mockReset();
fetchImageAsBase64Mock.mockReset();
openAICtor.mockReset();
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
batchIncrementUsagesSpy = vi.spyOn(
server.services.metering,
'batchIncrementUsages',
@@ -150,9 +150,9 @@ describe('OpenAiImageProvider construction', () => {
// ── Model catalog ───────────────────────────────────────────────────
describe('OpenAiImageProvider model catalog', () => {
it('returns gpt-image-2 as the default', () => {
it('returns gpt-image-2.6-flare as the default', () => {
const provider = makeProvider();
expect(provider.getDefaultModel()).toBe('gpt-image-2');
expect(provider.getDefaultModel()).toBe('gpt-image-2.6-flare');
});
it('drops the deprecated gpt-image-1 family and chatgpt-image-latest', () => {
@@ -195,7 +195,7 @@ describe('OpenAiImageProvider.generate test_mode', () => {
expect(result).toBe(
'https://puter-sample-data.puter.site/image_example.png',
);
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(generateMock).not.toHaveBeenCalled();
});
});
@@ -235,9 +235,60 @@ describe('OpenAiImageProvider.generate argument validation', () => {
// ── Credit gate ─────────────────────────────────────────────────────
describe('OpenAiImageProvider.generate credit gate', () => {
it('waits for the real usage write before returning an image', async () => {
const actor = makeActor({
user: {
id: 62,
uuid: `image-write-${Date.now()}`,
username: 'metered',
},
});
const redis = server.clients.redis as unknown as {
meterIncr: (...args: string[]) => Promise<unknown>;
};
const realIncr = redis.meterIncr.bind(redis);
let release!: () => void;
const held = new Promise<void>((resolve) => {
release = resolve;
});
let blocked = false;
vi.spyOn(redis, 'meterIncr').mockImplementation(async (...args) => {
const actorKey = `:actor:${actor.user!.uuid}:`;
if (!blocked && args.some((arg) => arg.includes(actorKey))) {
blocked = true;
await held;
}
return realIncr(...args);
});
generateMock.mockResolvedValueOnce({
data: [{ url: 'https://image.example/result.png' }],
});
const provider = makeProvider();
let finished = false;
const generating = withTestActor(
() =>
provider.generate({
model: 'gpt-image-2',
prompt: 'an image',
ratio: { w: 1024, h: 1024 },
}),
actor,
).then((result) => {
finished = true;
return result;
});
try {
await vi.waitFor(() => expect(blocked).toBe(true));
expect(finished).toBe(false);
} finally {
release();
await generating;
}
});
it('throws 402 BEFORE hitting OpenAI when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() =>
provider.generate({
@@ -273,7 +324,7 @@ describe('OpenAiImageProvider.generate credit gate', () => {
actor,
);
const [, estimate] = hasCreditsSpy.mock.calls[0]!;
const [, estimate] = admitSpy.mock.calls[0]!;
expect(estimate).toBeGreaterThan(0);
expect(heldDuringCall).toBe(Math.ceil(estimate));
// The hold stays until the usage write lands.
@@ -677,8 +728,8 @@ describe('OpenAiImageProvider gpt-image-2.5 models', () => {
);
// Latent factor 48 vs 24 at the medium tier.
const gptImage2Estimate = hasCreditsSpy.mock.calls[0]![1];
const gptImage25Estimate = hasCreditsSpy.mock.calls[1]![1];
const gptImage2Estimate = admitSpy.mock.calls[0]![1];
const gptImage25Estimate = admitSpy.mock.calls[1]![1];
expect(gptImage25Estimate).toBeLessThan(gptImage2Estimate);
});
@@ -703,8 +754,8 @@ describe('OpenAiImageProvider gpt-image-2.5 models', () => {
}),
);
const highEstimate = hasCreditsSpy.mock.calls[0]![1];
const maxEstimate = hasCreditsSpy.mock.calls[1]![1];
const highEstimate = admitSpy.mock.calls[0]![1];
const maxEstimate = admitSpy.mock.calls[1]![1];
expect(maxEstimate).toBeGreaterThan(highEstimate);
});
});
@@ -857,3 +908,64 @@ it('keeps a landscape aspect hint landscape at gpt-image-2 sizes', async () => {
expect(w).toBeGreaterThan(h);
expect(Math.abs(w / h - 1.7)).toBeLessThan(0.1);
});
describe('OpenAiImageProvider GPT Image 2.6', () => {
it.each(['gpt-image-2.6-flare', 'gpt-image-2.6-sunburst'])(
'%s generates and edits with token-based billing', async (model) => {
const provider = makeProvider();
const response = {
data: [{ b64_json: 'AAAA' }],
usage: {
input_tokens: 100,
output_tokens: 800,
input_tokens_details: { text_tokens: 100, image_tokens: 0 },
},
};
generateMock.mockResolvedValue(response);
editMock.mockResolvedValue(response);
for (const input_image of [undefined, 'data:image/png;base64,AAAA']) {
const result = await withTestActor(() => provider.generate({
model, prompt: 'hi', quality: 'xhigh',
ratio: { w: 1024, h: 1024 }, input_image,
}));
expect(result).toBe('data:image/png;base64,AAAA');
}
for (const mock of [generateMock, editMock]) {
expect(mock.mock.calls[0]![0]).toMatchObject({
model, quality: 'xhigh', size: '1024x1024',
});
}
const [, entries] = batchIncrementUsagesSpy.mock.calls[0]!;
expect(entries).toEqual(expect.arrayContaining([
expect.objectContaining({
usageType: `openai:${model}:xhigh:1024x1024:output`,
costOverride: 2_400_000,
}),
]));
},
);
it.each([
['low', 1024, 1024, 823],
['medium', 1024, 1024, 1449],
['high', 1024, 1024, 1756],
['xhigh', 1024, 1024, 3122],
['max', 1024, 1024, 7024],
['auto', 1024, 1024, 1449],
['low', 2048, 1088, 571],
])('estimates %s at %sx%s from the 2.6 calculator', async (quality, w, h, tokens) => {
generateMock.mockResolvedValue({ data: [{ b64_json: 'AAAA' }] });
await withTestActor(() => makeProvider().generate({
model: 'gpt-image-2.6-flare', prompt: 'hi', quality: String(quality),
ratio: { w: Number(w), h: Number(h) },
}));
const [, entries] = batchIncrementUsagesSpy.mock.calls[0]!;
const output = (entries as Array<{ usageType: string; costOverride: number }>)
.find((entry) => entry.usageType.endsWith(':output'))!;
expect(output.usageType).toBe(`openai:gpt-image-2.6-flare:${quality}:${w}x${h}:output`);
// The metering conversion rounds fractional microcents upward.
expect(output.costOverride).toBeGreaterThanOrEqual(Number(tokens) * 3000);
expect(output.costOverride).toBeLessThanOrEqual(Number(tokens) * 3000 + 1);
});
});
@@ -56,8 +56,8 @@ interface OpenAIImageUsage {
/**
* OpenAI image generation provider for v2. Supports the GPT Image models
* (gpt-image-2, -2.5-sunburst, -2.5-flare), including image-to-image editing
* via `input_images` (the `images.edit` endpoint).
* (gpt-image-2 and the 2.5/2.6 Sunburst and Flare models), including
* image-to-image editing via `input_images` (the `images.edit` endpoint).
*/
export class OpenAiImageProvider implements IImageProvider {
#meteringService: AiMeteringService;
@@ -109,7 +109,7 @@ export class OpenAiImageProvider implements IImageProvider {
}
getDefaultModel(): string {
return 'gpt-image-2';
return 'gpt-image-2.6-flare';
}
async generate({
@@ -293,7 +293,10 @@ export class OpenAiImageProvider implements IImageProvider {
});
}
if (usageEntries.length) {
this.#meteringService.batchIncrementUsages(actor, usageEntries);
await this.#meteringService.batchIncrementUsages(
actor,
usageEntries,
);
}
const url =
@@ -601,17 +604,36 @@ export class OpenAiImageProvider implements IImageProvider {
height: number,
quality?: string,
): number {
const factors = model.startsWith('gpt-image-2.5')
? OpenAiImageProvider.#GPT_IMAGE_2_5_LATENT_FACTORS
: OpenAiImageProvider.#GPT_IMAGE_2_LATENT_FACTORS;
const isGptImage26 = model.startsWith('gpt-image-2.6');
const factors =
isGptImage26 || model.startsWith('gpt-image-2.5')
? OpenAiImageProvider.#GPT_IMAGE_2_5_LATENT_FACTORS
: OpenAiImageProvider.#GPT_IMAGE_2_LATENT_FACTORS;
const factor = factors[quality ?? ''] ?? factors.medium;
const longEdge = Math.max(width, height);
const shortEdge = Math.min(width, height);
const shortLatent = Math.round((factor * shortEdge) / longEdge);
const shortGrid = (factor * shortEdge) / longEdge;
const floorGrid = Math.floor(shortGrid);
const shortLatent =
isGptImage26 && shortGrid - floorGrid === 0.5
? floorGrid + (floorGrid % 2)
: Math.round(shortGrid);
const latentW = width >= height ? factor : shortLatent;
const latentH = width >= height ? shortLatent : factor;
const baseArea = latentW * latentH;
return Math.ceil((baseArea * (2_000_000 + width * height)) / 4_000_000);
const tokens = Math.ceil(
(baseArea * (2_000_000 + width * height)) / 4_000_000,
);
// GPT Image 2.6 bills more tokens at low and medium quality.
const estimatedQuality =
quality && quality in factors ? quality : 'medium';
const multiplier =
estimatedQuality === 'low'
? 4.2
: estimatedQuality === 'medium'
? 3.3
: 1;
return isGptImage26 ? Math.round(tokens * multiplier) : tokens;
}
#estimateOutputCostFromTokens(
@@ -20,6 +20,46 @@
import type { IImageModel } from '../../types.js';
export const OPEN_AI_IMAGE_GENERATION_MODELS: IImageModel[] = [
{
puterId: 'openai:openai/gpt-image-2.6-sunburst',
id: 'gpt-image-2.6-sunburst',
aliases: ['openai/gpt-image-2.6-sunburst'],
name: 'GPT Image 2.6 Sunburst',
version: '2.6',
costs_currency: 'usd-cents',
index_cost_key: 'low:1024x1024',
costs: {
// Text tokens (per 1M tokens)
text_input: 500, // $5.00
text_cached_input: 125, // $1.25
// Image tokens (per 1M tokens)
image_input: 800, // $8.00
image_cached_input: 200, // $2.00
image_output: 3000, // $30.00
'low:1024x1024': 2.469,
},
allowedQualityLevels: ['low', 'medium', 'high', 'xhigh', 'max', 'auto'],
},
{
puterId: 'openai:openai/gpt-image-2.6-flare',
id: 'gpt-image-2.6-flare',
aliases: ['openai/gpt-image-2.6-flare'],
name: 'GPT Image 2.6 Flare',
version: '2.6',
costs_currency: 'usd-cents',
index_cost_key: 'low:1024x1024',
costs: {
// Text tokens (per 1M tokens)
text_input: 500, // $5.00
text_cached_input: 125, // $1.25
// Image tokens (per 1M tokens)
image_input: 800, // $8.00
image_cached_input: 200, // $2.00
image_output: 3000, // $30.00
'low:1024x1024': 2.469,
},
allowedQualityLevels: ['low', 'medium', 'high', 'xhigh', 'max', 'auto'],
},
{
puterId: 'openai:openai/gpt-image-2.5-sunburst',
id: 'gpt-image-2.5-sunburst',
@@ -84,7 +84,7 @@ vi.mock('../../../../util/secureHttp.js', () => ({
// ── Test harness ────────────────────────────────────────────────────
let server: PuterServer;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
let batchIncrementUsagesSpy: MockInstance<
MeteringService['batchIncrementUsages']
@@ -115,7 +115,7 @@ beforeEach(() => {
cancelMock.mockReset();
replicateCtor.mockReset();
secureFetchMock.mockReset();
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
batchIncrementUsagesSpy = vi.spyOn(
server.services.metering,
@@ -177,7 +177,7 @@ describe('ReplicateImageGenerationProvider.generate test_mode', () => {
expect(result).toBe(
'https://puter-sample-data.puter.site/image_example.png',
);
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(createPredictionMock).not.toHaveBeenCalled();
});
});
@@ -199,7 +199,7 @@ describe('ReplicateImageGenerationProvider.generate argument validation', () =>
describe('ReplicateImageGenerationProvider.generate credit gate', () => {
it('throws 402 BEFORE hitting Replicate when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() => provider.generate({ prompt: 'hi' })),
).rejects.toMatchObject({ statusCode: 402 });
@@ -255,7 +255,10 @@ describe('ReplicateImageGenerationProvider.generate credit gate', () => {
it('holds credits for longer than a prediction can run', async () => {
const provider = makeProvider();
const actor = holdUser('ttl');
const reserveSpy = vi.spyOn(server.services.metering, 'reserveCredits');
const reserveSpy = vi.spyOn(
server.services.metering,
'reserveCreditsIfAffordable',
);
createPredictionMock.mockResolvedValueOnce({
id: 'ttl',
status: 'succeeded',
@@ -274,7 +277,9 @@ describe('ReplicateImageGenerationProvider.generate credit gate', () => {
const provider = makeProvider();
const actor = holdUser('surcharge');
secureFetchMock.mockResolvedValueOnce(new Response('not-an-image'));
hasCreditsSpy.mockResolvedValueOnce(true).mockResolvedValueOnce(false);
admitSpy.mockImplementationOnce((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
).mockResolvedValueOnce(false);
await expect(
withTestActor(
@@ -950,7 +955,7 @@ it('does not start a prediction for an already-aborted request', async () => {
),
).rejects.toMatchObject({ statusCode: 400, legacyCode: 'client_aborted' });
expect(createPredictionMock).not.toHaveBeenCalled();
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
});
it('stops polling and cancels at the prediction deadline', async () => {
@@ -1192,12 +1197,12 @@ it('rejects more than ten input images before fetching or pricing any', async ()
),
).rejects.toMatchObject({ statusCode: 400, legacyCode: 'bad_request' });
expect(secureFetchMock).not.toHaveBeenCalled();
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(createPredictionMock).not.toHaveBeenCalled();
});
it('checks credits on the output-only estimate before fetching input images', async () => {
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() =>
makeProvider().generate({
@@ -71,7 +71,7 @@ vi.mock('together-ai', () => {
// ── Test harness ────────────────────────────────────────────────────
let server: PuterServer;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
beforeAll(async () => {
@@ -96,7 +96,7 @@ const makeProvider = () =>
beforeEach(() => {
generateMock.mockReset();
togetherCtor.mockReset();
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
});
@@ -158,7 +158,7 @@ describe('TogetherImageProvider.generate test_mode', () => {
expect(result).toBe(
'https://puter-sample-data.puter.site/image_example.png',
);
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(generateMock).not.toHaveBeenCalled();
});
});
@@ -189,7 +189,7 @@ describe('TogetherImageProvider.generate argument validation', () => {
describe('TogetherImageProvider.generate credit gate', () => {
it('throws 402 BEFORE hitting Together when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() =>
@@ -760,7 +760,7 @@ describe('Together supported sizes and references', () => {
width: 1024,
height: 1024,
});
expect(Number.isFinite(hasCreditsSpy.mock.calls[0][1])).toBe(true);
expect(Number.isFinite(admitSpy.mock.calls[0][1])).toBe(true);
});
it('resolves provider aliases without changing the selected model', async () => {
@@ -790,7 +790,7 @@ describe('Together supported sizes and references', () => {
}),
),
).rejects.toMatchObject({ statusCode: 400 });
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(generateMock).not.toHaveBeenCalled();
});
@@ -859,7 +859,7 @@ it.each(['3K', 'unknown', ' 5k '])(
}),
),
).rejects.toMatchObject({ statusCode: 400, legacyCode: 'bad_request' });
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(generateMock).not.toHaveBeenCalled();
},
);
@@ -895,7 +895,7 @@ describe('Together current API prices', () => {
input_image: 'https://example.com/reference.png',
}),
);
expect(hasCreditsSpy).toHaveBeenCalledWith(expect.anything(), 4685000);
expect(admitSpy).toHaveBeenCalledWith(expect.anything(), 4685000, undefined);
expect(incrementUsageSpy).toHaveBeenCalledWith(
expect.anything(),
'togetherai:google/flash-image-3.1:0.5K',
@@ -85,7 +85,7 @@ vi.mock('openai', () => {
// ── Test harness ────────────────────────────────────────────────────
let server: PuterServer;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let batchIncrementUsagesSpy: MockInstance<
MeteringService['batchIncrementUsages']
>;
@@ -115,7 +115,7 @@ beforeEach(() => {
openAICtor.mockReset();
axiosGetMock.mockReset();
axiosGetMock.mockRejectedValue(new Error('Catalog unavailable'));
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
batchIncrementUsagesSpy = vi.spyOn(
server.services.metering,
'batchIncrementUsages',
@@ -182,7 +182,7 @@ describe('XAIImageProvider.generate test_mode', () => {
expect(result).toBe(
'https://puter-sample-data.puter.site/image_example.png',
);
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(generateMock).not.toHaveBeenCalled();
expect(batchIncrementUsagesSpy).not.toHaveBeenCalled();
});
@@ -231,7 +231,7 @@ describe('XAIImageProvider.generate argument validation', () => {
describe('XAIImageProvider.generate credit gate', () => {
it('throws 402 BEFORE hitting xAI when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() =>
@@ -718,7 +718,7 @@ describe('XAIImageProvider quality and reference limits', () => {
}),
),
).rejects.toMatchObject({ statusCode: 400 });
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(postMock).not.toHaveBeenCalled();
});
});
@@ -746,7 +746,7 @@ describe('xAI option validation', () => {
makeProvider().generate({ prompt: 'hi', ...options } as never),
),
).rejects.toMatchObject({ statusCode: 400 });
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(generateMock).not.toHaveBeenCalled();
});
});
+9 -9
View File
@@ -167,7 +167,7 @@ const sentRegions = () =>
let server: PuterServer;
let driver: OCRDriver;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
beforeAll(async () => {
@@ -199,7 +199,7 @@ beforeEach(() => {
mistralCtor.mockReset();
// Spy on metering — keep the real impl so its recording side runs,
// but capture calls so per-test assertions can inspect them.
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
});
@@ -335,7 +335,7 @@ describe('OCRDriver.recognize (aws-textract)', () => {
};
it('throws 402 when the actor does not have enough credits', async () => {
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
const { actor } = await makeUser();
await expect(
@@ -546,7 +546,7 @@ describe('OCRDriver.recognize (aws-textract)', () => {
describe('regional pricing', () => {
const onePage = { Blocks: [{ BlockType: 'PAGE' }] };
const checkedCosts = () =>
hasCreditsSpy.mock.calls.map(([, cost]) => cost);
admitSpy.mock.calls.map(([, cost]) => cost);
const textractCharges = () =>
incrementUsageSpy.mock.calls
.filter(([, type]) => type === TEXTRACT_USAGE)
@@ -719,7 +719,7 @@ describe('OCRDriver.recognize (mistral)', () => {
};
it('throws 402 when the actor does not have enough credits', async () => {
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
const { actor } = await makeUser();
await expect(
@@ -1033,7 +1033,7 @@ describe('OCRDriver.recognize (mistral)', () => {
const pageType = 'mistral-ocr:mistral-ocr-4-1:page';
const annotationType = 'mistral-ocr:mistral-ocr-4-1:annotations:page';
// The pre-flight covers the page plus its annotation.
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(
expect(admitSpy.mock.calls[0]![1]).toBe(
OCR_COSTS[pageType] + OCR_COSTS[annotationType],
);
const ocrCalls = incrementUsageSpy.mock.calls.filter(
@@ -1080,7 +1080,7 @@ describe('OCRDriver credit pre-flight', () => {
const pageType = 'mistral-ocr:mistral-ocr-4-1:page';
const perPage = OCR_COSTS[pageType];
const pdfSource = (buffer: Buffer) => dataUrl(buffer, 'application/pdf');
const checkedCosts = () => hasCreditsSpy.mock.calls.map(([, cost]) => cost);
const checkedCosts = () => admitSpy.mock.calls.map(([, cost]) => cost);
const outstandingHolds = (actor: Actor) =>
server.stores.creditHold.outstanding(actor.user.uuid!);
@@ -1171,8 +1171,8 @@ describe('OCRDriver credit pre-flight', () => {
),
).rejects.toMatchObject({ statusCode: 402 });
// The estimate/hold path (which calls hasEnoughCredits) never ran.
expect(hasCreditsSpy).not.toHaveBeenCalled();
// The estimate/hold path never ran.
expect(admitSpy).not.toHaveBeenCalled();
expect(mistralOcrProcessMock).not.toHaveBeenCalled();
noUsage.mockRestore();
});
@@ -72,7 +72,7 @@ vi.mock('undici', async (importOriginal) => ({
let server: PuterServer;
let driver: VoiceChangerDriver;
let fetchSpy: MockInstance<typeof fetch>;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
beforeAll(async () => {
@@ -91,7 +91,7 @@ afterAll(async () => {
beforeEach(() => {
webFetchMock.mockReset();
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
});
@@ -197,7 +197,7 @@ describe('VoiceChangerDriver.convert argument validation', () => {
describe('VoiceChangerDriver.convert credit gate', () => {
it('throws 402 BEFORE hitting ElevenLabs when the actor lacks credits', async () => {
const { actor } = await makeUser();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withActor(actor, () =>
@@ -387,7 +387,7 @@ describe('VoiceChangerDriver.convert success path', () => {
const [base, factored] = metered();
expect(factored).toBe(base! * 2);
const held = hasCreditsSpy.mock.calls.map(([, amount]) => amount);
const held = admitSpy.mock.calls.map(([, amount]) => amount);
expect(held).toEqual([base, base! * 2]);
});
@@ -122,7 +122,7 @@ vi.mock('openai', () => {
let server: PuterServer;
let driver: SpeechToTextDriver;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
beforeAll(async () => {
@@ -145,7 +145,7 @@ beforeEach(() => {
toFileMock.mockClear();
// Spy on metering — keep the real impl so its recording side runs,
// but capture calls so per-test assertions can inspect them.
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
});
@@ -360,7 +360,7 @@ describe('SpeechToTextDriver argument validation', () => {
describe('SpeechToTextDriver credit gate', () => {
it('throws 402 BEFORE hitting OpenAI when actor lacks credits', async () => {
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
const { actor } = await makeUser();
await expect(
@@ -686,7 +686,7 @@ describe('SpeechToTextDriver metering', () => {
expect(cost).toBe(SPEECH_TO_TEXT_COSTS[usageType]);
});
it('asks hasEnoughCredits for the same total it later meters', async () => {
it('reserves the same total it later meters', async () => {
const { actor } = await makeUser();
transcriptionsCreateMock.mockResolvedValueOnce({ text: 'x' });
@@ -700,7 +700,7 @@ describe('SpeechToTextDriver metering', () => {
const usageType = 'openai:gpt-transcribe:second';
const expected = SPEECH_TO_TEXT_COSTS[usageType] * 2;
const creditCall = hasCreditsSpy.mock.calls[0]!;
const creditCall = admitSpy.mock.calls[0]!;
expect(creditCall[1]).toBe(expected);
});
@@ -794,7 +794,7 @@ describe('SpeechToTextDriver metering', () => {
const [base, factored] = metered();
expect(factored).toBe(base! * 2);
const held = hasCreditsSpy.mock.calls.map(([, amount]) => amount);
const held = admitSpy.mock.calls.map(([, amount]) => amount);
expect(held).toEqual([base, base! * 2]);
});
});
@@ -72,7 +72,7 @@ const HOLD = COUNTED_TOKENS * AUDIO_RATE + 8192 * OUTPUT_RATE;
let server: PuterServer;
let sttDriver: SpeechToTextDriver;
let fetchSpy: MockInstance<typeof fetch>;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let batchSpy: MockInstance<MeteringService['batchIncrementUsages']>;
const driver = {
@@ -102,7 +102,7 @@ beforeEach(() => {
.mockRejectedValue(new Error('unexpected fetch')) as MockInstance<
typeof fetch
>;
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
batchSpy = vi.spyOn(server.services.metering, 'batchIncrementUsages');
});
@@ -220,13 +220,16 @@ const metered = (): UsageInput[] =>
/** Records each credit hold taken, its options, and its release. */
const trackHolds = (order: string[] = []) => {
const metering = server.services.metering;
const realReserve = metering.reserveCredits.bind(metering);
const realReserve = Object.getPrototypeOf(
metering,
).reserveCreditsIfAffordable.bind(metering) as typeof metering.reserveCreditsIfAffordable;
const held: { amount: number; ttlMs?: number }[] = [];
const released = vi.fn();
vi.spyOn(metering, 'reserveCredits').mockImplementation(
vi.spyOn(metering, 'reserveCreditsIfAffordable').mockImplementation(
async (actor, amount, opts) => {
held.push({ amount, ttlMs: opts?.ttlMs });
const hold = await realReserve(actor, amount, opts);
if (!hold) return null;
return {
...hold,
release: async () => {
@@ -425,7 +428,7 @@ describe('GeminiSpeechToTextProvider test_mode', () => {
model: MODEL,
});
expect(fetchSpy).not.toHaveBeenCalled();
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(batchSpy).not.toHaveBeenCalled();
});
@@ -1263,12 +1266,12 @@ describe('GeminiSpeechToTextProvider countTokens preflight', () => {
describe('GeminiSpeechToTextProvider credit hold', () => {
it('throws 402 after counting tokens, before transcribing', async () => {
const { actor } = await makeUser();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
fetchSpy.mockResolvedValueOnce(Response.json({ totalTokens: 91 }));
await expect(
withActor(actor, () => driver.transcribe({ file: dataUrl() })),
).rejects.toMatchObject({ statusCode: 402 });
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(HOLD);
expect(admitSpy.mock.calls[0]![1]).toBe(HOLD);
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(batchSpy).not.toHaveBeenCalled();
});
@@ -70,7 +70,7 @@ const driver = {
getReportedCosts: () => sttDriver.getReportedCosts(),
};
let fetchSpy: MockInstance<typeof fetch>;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
beforeAll(async () => {
@@ -88,7 +88,7 @@ afterAll(async () => {
beforeEach(() => {
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
});
@@ -193,7 +193,7 @@ describe('XAISpeechToTextProvider test_mode', () => {
expect(Array.isArray(result.words)).toBe(true);
// No file required, no actor required, no fetch / metering hit.
expect(fetchSpy).not.toHaveBeenCalled();
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(incrementUsageSpy).not.toHaveBeenCalled();
});
@@ -258,7 +258,7 @@ describe('XAISpeechToTextProvider missing API key', () => {
describe('XAISpeechToTextProvider credit gate', () => {
it('throws 402 BEFORE hitting xAI when actor lacks credits', async () => {
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
const { actor } = await makeUser();
await expect(
@@ -273,7 +273,7 @@ describe('XAISpeechToTextProvider credit gate', () => {
expect(incrementUsageSpy).not.toHaveBeenCalled();
});
it('asks hasEnoughCredits for estimated seconds × per-second ucents', async () => {
it('reserves estimated seconds × per-second ucents', async () => {
const { actor } = await makeUser();
fetchSpy.mockResolvedValueOnce(
sttResponse({ text: 'ok', duration: 1 }),
@@ -285,7 +285,7 @@ describe('XAISpeechToTextProvider credit gate', () => {
driver.transcribe({ file: dataUrl(audio, 'audio/mp3') }),
);
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(UCENTS_PER_SECOND * 2);
expect(admitSpy.mock.calls[0]![1]).toBe(UCENTS_PER_SECOND * 2);
});
it('estimates 60 seconds for URL inputs (we cannot inspect size locally)', async () => {
@@ -298,7 +298,7 @@ describe('XAISpeechToTextProvider credit gate', () => {
driver.transcribe({ file: 'https://example.com/clip.mp3' }),
);
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(UCENTS_PER_SECOND * 60);
expect(admitSpy.mock.calls[0]![1]).toBe(UCENTS_PER_SECOND * 60);
});
});
+3 -3
View File
@@ -699,9 +699,9 @@ describe('TTSDriver cost factor', () => {
server.services.metering,
'incrementUsage',
);
const hasCreditsSpy = vi.spyOn(
const admitSpy = vi.spyOn(
server.services.metering,
'hasEnoughCredits',
'reserveCreditsIfAffordable',
);
const metered = () =>
incrementUsageSpy.mock.calls.map(([, , , cost]) => cost!);
@@ -725,7 +725,7 @@ describe('TTSDriver cost factor', () => {
const [base, factored] = metered();
expect(base).toBeGreaterThan(0);
expect(factored).toBe(Math.round(base! * 2));
const held = hasCreditsSpy.mock.calls.map(([, amount]) => amount);
const held = admitSpy.mock.calls.map(([, amount]) => amount);
expect(held).toEqual([base, Math.round(base! * 2)]);
});
});
@@ -84,7 +84,7 @@ vi.mock('@aws-sdk/client-polly', async () => {
let server: PuterServer;
let priceFetch: MockInstance<typeof fetch>;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
beforeAll(async () => {
@@ -180,7 +180,7 @@ beforeEach(() => {
pollyCtor.mockReset();
priceFetch = vi.fn(servePriceLists());
vi.stubGlobal('fetch', priceFetch);
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
});
@@ -314,7 +314,7 @@ describe('AWSPollyTTSProvider.synthesize test_mode', () => {
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
content_type: 'audio',
});
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(pollySendMock).not.toHaveBeenCalled();
});
});
@@ -349,7 +349,7 @@ describe('AWSPollyTTSProvider.synthesize argument validation', () => {
describe('AWSPollyTTSProvider.synthesize credit gate', () => {
it('throws 402 BEFORE hitting Polly when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
sendDispatch();
await expect(
@@ -481,7 +481,7 @@ describe('AWSPollyTTSProvider.synthesize metering', () => {
expect(cost).toBe(expectedCost);
});
it('asks for hasEnoughCredits with the same total it later meters', async () => {
it('reserves the same total it later meters', async () => {
const provider = makeProvider();
sendDispatch();
@@ -495,7 +495,7 @@ describe('AWSPollyTTSProvider.synthesize metering', () => {
);
const expectedCost = AWS_POLLY_COSTS['generative'] * text.length;
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(expectedCost);
expect(admitSpy.mock.calls[0]![1]).toBe(expectedCost);
});
});
@@ -599,7 +599,7 @@ describe('AWSPollyTTSProvider published pricing', () => {
engine: 'generative',
});
expect(hasCreditsSpy.mock.calls.map((c) => c[1])).toEqual([
expect(admitSpy.mock.calls.map((c) => c[1])).toEqual([
1700 * 5,
3000 * 5,
]);
@@ -648,7 +648,7 @@ describe('AWSPollyTTSProvider published pricing', () => {
await readStreamResult(result);
expect(refreshedNeural).toBe(19);
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(1700 * 5);
expect(admitSpy.mock.calls[0]![1]).toBe(1700 * 5);
expect(incrementUsageSpy.mock.calls[0]![3]).toBe(1700 * 5);
});
@@ -86,8 +86,10 @@ describe('meterPerCharacter', () => {
it('holds and then meters exactly text length × price', async () => {
const check = vi
.spyOn(server.services.metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(server.services.metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
);
const record = vi.spyOn(server.services.metering, 'incrementUsage');
await drain(
@@ -101,7 +103,7 @@ describe('meterPerCharacter', () => {
),
);
expect(check).toHaveBeenCalledWith(actor, 7500);
expect(check).toHaveBeenCalledWith(actor, 7500, undefined);
expect(record).toHaveBeenCalledWith(
actor,
'openai:tts-1:character',
@@ -118,8 +120,10 @@ describe('meterPerCharacter', () => {
it('meters nothing when the upstream breaks before any audio', async () => {
vi.spyOn(
server.services.metering,
'hasEnoughCredits',
).mockResolvedValue(true);
'reserveCreditsIfAffordable',
).mockImplementation((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
);
const record = vi.spyOn(server.services.metering, 'incrementUsage');
await drain(
@@ -139,8 +143,10 @@ describe('meterPerCharacter', () => {
it('meters the full text once audio has started, even if the upstream then breaks', async () => {
vi.spyOn(
server.services.metering,
'hasEnoughCredits',
).mockResolvedValue(true);
'reserveCreditsIfAffordable',
).mockImplementation((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
);
const record = vi.spyOn(server.services.metering, 'incrementUsage');
await drain(
@@ -165,8 +171,10 @@ describe('meterPerCharacter', () => {
it('meters the full text when the caller hangs up before any audio', async () => {
vi.spyOn(
server.services.metering,
'hasEnoughCredits',
).mockResolvedValue(true);
'reserveCreditsIfAffordable',
).mockImplementation((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
);
const record = vi.spyOn(server.services.metering, 'incrementUsage');
const result = await meterPerCharacter(
@@ -192,8 +200,10 @@ describe('meterPerCharacter', () => {
it('meters nothing when synthesis fails', async () => {
vi.spyOn(
server.services.metering,
'hasEnoughCredits',
).mockResolvedValue(true);
'reserveCreditsIfAffordable',
).mockImplementation((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
);
const record = vi.spyOn(server.services.metering, 'incrementUsage');
const boom = new Error('vendor down');
@@ -213,8 +223,8 @@ describe('meterPerCharacter', () => {
it('refuses with a 402 before synthesizing when unaffordable', async () => {
vi.spyOn(
server.services.metering,
'hasEnoughCredits',
).mockResolvedValue(false);
'reserveCreditsIfAffordable',
).mockResolvedValue(null);
const synthesize = vi.fn(audio);
await expect(
@@ -57,7 +57,7 @@ import { ELEVENLABS_TTS_COSTS } from './costs.js';
let server: PuterServer;
let fetchSpy: MockInstance<typeof fetch>;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
beforeAll(async () => {
@@ -88,7 +88,7 @@ const audioResponse = (body = 'audio-bytes', contentType = 'audio/mpeg') =>
beforeEach(() => {
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
});
@@ -283,7 +283,7 @@ describe('ElevenLabsTTSProvider.synthesize test_mode', () => {
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
content_type: 'audio',
});
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
});
});
@@ -323,7 +323,7 @@ describe('ElevenLabsTTSProvider.synthesize argument validation', () => {
});
expect(fetchSpy).not.toHaveBeenCalled();
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(incrementUsageSpy).not.toHaveBeenCalled();
});
@@ -358,7 +358,7 @@ describe('ElevenLabsTTSProvider.synthesize argument validation', () => {
describe('ElevenLabsTTSProvider.synthesize credit gate', () => {
it('throws 402 BEFORE hitting ElevenLabs when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() => provider.synthesize({ text: 'hi' })),
@@ -510,7 +510,7 @@ describe('ElevenLabsTTSProvider.synthesize request shape', () => {
fields: { key, got: value },
});
expect(fetchSpy).not.toHaveBeenCalled();
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(incrementUsageSpy).not.toHaveBeenCalled();
},
);
@@ -893,7 +893,7 @@ describe('ElevenLabsTTSProvider.synthesize metering', () => {
expect(cost).toBe(expectedCost);
});
it('asks for hasEnoughCredits with the same total it later meters', async () => {
it('reserves the same total it later meters', async () => {
const provider = makeProvider();
fetchSpy.mockResolvedValueOnce(audioResponse());
@@ -904,7 +904,7 @@ describe('ElevenLabsTTSProvider.synthesize metering', () => {
const expectedCost =
ELEVENLABS_TTS_COSTS['eleven_multilingual_v2'] * text.length;
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(expectedCost);
expect(admitSpy.mock.calls[0]![1]).toBe(expectedCost);
});
});
@@ -61,7 +61,7 @@ const INTERACTIONS_URL =
let server: PuterServer;
let fetchSpy: MockInstance<typeof fetch>;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let batchIncrementUsagesSpy: MockInstance<
MeteringService['batchIncrementUsages']
>;
@@ -183,13 +183,16 @@ const payingUser = () =>
/** Spies on the credit holds taken, recording the amount and each release. */
const trackHolds = () => {
const metering = server.services.metering;
const realReserve = metering.reserveCredits.bind(metering);
const realReserve = Object.getPrototypeOf(
metering,
).reserveCreditsIfAffordable.bind(metering) as typeof metering.reserveCreditsIfAffordable;
const held: number[] = [];
const released = vi.fn();
vi.spyOn(metering, 'reserveCredits').mockImplementation(
vi.spyOn(metering, 'reserveCreditsIfAffordable').mockImplementation(
async (actor, amount, opts) => {
held.push(amount);
const hold = await realReserve(actor, amount, opts);
if (!hold) return null;
return {
...hold,
release: async () => {
@@ -214,7 +217,7 @@ beforeEach(() => {
.mockRejectedValue(new Error('unexpected fetch')) as MockInstance<
typeof fetch
>;
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
batchIncrementUsagesSpy = vi.spyOn(
server.services.metering,
'batchIncrementUsages',
@@ -332,7 +335,7 @@ describe('GeminiTTSProvider.synthesize test_mode', () => {
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
content_type: 'audio',
});
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
});
});
@@ -406,7 +409,7 @@ describe('GeminiTTSProvider.synthesize argument validation', () => {
legacyCode: 'input_too_large',
fields: { key: 'text', max: 3000 },
});
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
});
@@ -434,7 +437,7 @@ describe('GeminiTTSProvider.synthesize argument validation', () => {
legacyCode: 'input_too_large',
fields: { key: 'instructions', max: 3000 },
});
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
});
});
@@ -443,7 +446,7 @@ describe('GeminiTTSProvider.synthesize argument validation', () => {
describe('GeminiTTSProvider.synthesize credit gate', () => {
it('throws 402 before calling Gemini when the actor lacks credits', async () => {
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() => makeProvider().synthesize({ text: 'hi' })),
).rejects.toMatchObject({ statusCode: 402 });
@@ -473,9 +476,10 @@ describe('GeminiTTSProvider.synthesize credit gate', () => {
actor,
),
).rejects.toMatchObject({ statusCode: 402 });
expect(hasCreditsSpy).toHaveBeenCalledWith(
expect(admitSpy).toHaveBeenCalledWith(
actor,
FLASH_RESERVATION,
undefined,
);
expect(fetchSpy).not.toHaveBeenCalled();
expect(batchIncrementUsagesSpy).not.toHaveBeenCalled();
@@ -1365,9 +1369,10 @@ describe('GeminiTTSProvider.synthesize scheduled rates', () => {
server.clients.event.off('ai.cost.factor.*', factor);
}
expect(hasCreditsSpy).toHaveBeenCalledWith(
expect(admitSpy).toHaveBeenCalledWith(
expect.anything(),
reserved * 1.5,
undefined,
);
expect(held).toEqual([reserved * 1.5]);
expect(metered(':input').costOverride).toBe(7 * inputRate * 1.5);
@@ -84,7 +84,7 @@ vi.mock('openai', () => {
// ── Test harness ────────────────────────────────────────────────────
let server: PuterServer;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
beforeAll(async () => {
@@ -108,7 +108,7 @@ const mockAudioResponse = (bytes = 'opus-audio') => new Response(bytes);
beforeEach(() => {
speechCreateMock.mockReset();
openAICtor.mockReset();
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
});
@@ -205,7 +205,7 @@ describe('OpenAITTSProvider.synthesize test_mode', () => {
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
content_type: 'audio',
});
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(speechCreateMock).not.toHaveBeenCalled();
});
});
@@ -283,7 +283,7 @@ describe('OpenAITTSProvider.synthesize argument validation', () => {
describe('OpenAITTSProvider.synthesize credit gate', () => {
it('throws 402 BEFORE hitting OpenAI when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() => provider.synthesize({ text: 'hi' })),
@@ -298,7 +298,9 @@ describe('OpenAITTSProvider.synthesize credit gate', () => {
user: { id: 7, uuid: `tts-hold-${Date.now()}`, username: 'hold' },
});
const userId = actor.user!.uuid;
hasCreditsSpy.mockResolvedValue(true);
admitSpy.mockImplementation((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
);
let heldDuringCall = -1;
speechCreateMock.mockImplementationOnce(async () => {
heldDuringCall = await server.stores.creditHold.outstanding(userId);
@@ -324,7 +326,9 @@ describe('OpenAITTSProvider.synthesize credit gate', () => {
const actor = makeActor({
user: { id: 8, uuid: `tts-fail-${Date.now()}`, username: 'fail' },
});
hasCreditsSpy.mockResolvedValue(true);
admitSpy.mockImplementation((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
);
speechCreateMock.mockRejectedValueOnce(new Error('upstream down'));
await expect(
@@ -463,7 +467,7 @@ describe('OpenAITTSProvider.synthesize metering', () => {
expect(cost).toBe(expectedCost);
});
it('asks for hasEnoughCredits with the same total it later meters', async () => {
it('reserves the same total it later meters', async () => {
const provider = makeProvider();
speechCreateMock.mockResolvedValueOnce(mockAudioResponse());
@@ -473,8 +477,8 @@ describe('OpenAITTSProvider.synthesize metering', () => {
);
const expectedCost = OPENAI_TTS_COSTS['tts-1'] * text.length;
// First call to hasEnoughCredits should match the metered cost.
const creditCall = hasCreditsSpy.mock.calls[0]!;
// The first reservation should match the metered cost.
const creditCall = admitSpy.mock.calls[0]!;
expect(creditCall[1]).toBe(expectedCost);
});
});
@@ -56,7 +56,7 @@ import { SPEECHIFY_TTS_COSTS } from './costs.js';
let server: PuterServer;
let fetchSpy: MockInstance<typeof fetch>;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
beforeAll(async () => {
@@ -82,7 +82,7 @@ const audioResponse = (audioData = Buffer.from('audio-bytes').toString('base64')
beforeEach(() => {
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
});
@@ -159,7 +159,7 @@ describe('SpeechifyTTSProvider.synthesize test_mode', () => {
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
content_type: 'audio',
});
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
});
});
@@ -206,7 +206,7 @@ describe('SpeechifyTTSProvider.synthesize argument validation', () => {
describe('SpeechifyTTSProvider.synthesize credit gate', () => {
it('throws 402 BEFORE hitting Speechify when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() => provider.synthesize({ text: 'hi' })),
@@ -324,7 +324,7 @@ describe('SpeechifyTTSProvider.synthesize metering', () => {
expect(cost).toBe(expectedCost);
});
it('asks for hasEnoughCredits with the same total it later meters', async () => {
it('reserves the same total it later meters', async () => {
const provider = makeProvider();
fetchSpy.mockResolvedValueOnce(audioResponse());
@@ -332,7 +332,7 @@ describe('SpeechifyTTSProvider.synthesize metering', () => {
await withTestActor(() => provider.synthesize({ text }));
const expectedCost = SPEECHIFY_TTS_COSTS['simba-3.2'] * text.length;
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(expectedCost);
expect(admitSpy.mock.calls[0]![1]).toBe(expectedCost);
});
});
@@ -56,7 +56,7 @@ import { XAI_TTS_COSTS } from './costs.js';
let server: PuterServer;
let fetchSpy: MockInstance<typeof fetch>;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
beforeAll(async () => {
@@ -79,7 +79,7 @@ const audioResponse = (body = 'audio-bytes', contentType = 'audio/mpeg') =>
beforeEach(() => {
fetchSpy = vi.spyOn(globalThis, 'fetch') as MockInstance<typeof fetch>;
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
});
@@ -160,7 +160,7 @@ describe('XAITTSProvider.synthesize test_mode', () => {
url: 'https://puter-sample-data.puter.site/tts_example.mp3',
content_type: 'audio',
});
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(fetchSpy).not.toHaveBeenCalled();
});
});
@@ -195,7 +195,7 @@ describe('XAITTSProvider.synthesize argument validation', () => {
describe('XAITTSProvider.synthesize credit gate', () => {
it('throws 402 BEFORE hitting xAI when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() => provider.synthesize({ text: 'hi' })),
@@ -327,7 +327,7 @@ describe('XAITTSProvider.synthesize metering', () => {
expect(cost).toBe(expectedCost);
});
it('asks for hasEnoughCredits with the same total it later meters', async () => {
it('reserves the same total it later meters', async () => {
const provider = makeProvider();
fetchSpy.mockResolvedValueOnce(audioResponse());
@@ -335,7 +335,7 @@ describe('XAITTSProvider.synthesize metering', () => {
await withTestActor(() => provider.synthesize({ text }));
const expectedCost = XAI_TTS_COSTS['xai-tts'] * text.length;
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(expectedCost);
expect(admitSpy.mock.calls[0]![1]).toBe(expectedCost);
});
});
@@ -94,7 +94,7 @@ vi.mock('../../util/secureHttp.js', async (importOriginal) => ({
let server: PuterServer;
let driver: VideoGenerationDriver;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
beforeAll(async () => {
server = await setupTestServer({
@@ -122,8 +122,10 @@ beforeEach(() => {
togetherVideosCreateMock.mockReset();
togetherVideosRetrieveMock.mockReset();
secureFetchMock.mockReset();
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
hasCreditsSpy.mockResolvedValue(true);
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
admitSpy.mockImplementation((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
);
vi.spyOn(server.services.metering, 'getRemainingUsage').mockResolvedValue(
100_000_000_000,
);
@@ -106,7 +106,7 @@ const MP4 = Buffer.concat([
let server: PuterServer;
let videoDriver: VideoGenerationDriver;
let fetchSpy: MockInstance<typeof fetch>;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let batchSpy: MockInstance<MeteringService['batchIncrementUsages']>;
const generate = (args: Record<string, unknown> = {}) =>
@@ -139,9 +139,11 @@ beforeEach(() => {
secureFetchMock.mockReset();
secureFetchMock.mockRejectedValue(new Error('unexpected secureFetch'));
// The full hold is more than a new account's allowance.
hasCreditsSpy = vi
.spyOn(server.services.metering, 'hasEnoughCredits')
.mockResolvedValue(true);
admitSpy = vi
.spyOn(server.services.metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
);
batchSpy = vi.spyOn(server.services.metering, 'batchIncrementUsages');
});
@@ -589,7 +591,7 @@ describe('GeminiVideoProvider test_mode', () => {
it('returns the sample clip without network, credits or metering', async () => {
expect(await testMode()).toBe(SAMPLE_URL);
expect(fetchSpy).not.toHaveBeenCalled();
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(batchSpy).not.toHaveBeenCalled();
});
@@ -1028,7 +1030,7 @@ describe('GeminiVideoProvider source video', () => {
const path = await writeVideo(user, 'dog.mp4', MP4);
const read = vi.spyOn(server.stores.s3Object, 'getObjectStream');
hasCreditsSpy.mockResolvedValue(false);
admitSpy.mockResolvedValue(null);
countOnly();
await expect(
withActor(user.actor, () => generate({ input_video: path })),
@@ -1573,12 +1575,12 @@ describe('GeminiVideoProvider countTokens preflight', () => {
describe('GeminiVideoProvider credit hold', () => {
it('throws 402 after counting tokens, before generating', async () => {
const { actor } = await makeUser();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
fetchSpy.mockResolvedValueOnce(Response.json({ totalTokens: 18 }));
await expect(withActor(actor, () => generate())).rejects.toMatchObject({
statusCode: 402,
});
expect(hasCreditsSpy.mock.calls[0]![1]).toBe(HOLD);
expect(admitSpy.mock.calls[0]![1]).toBe(HOLD);
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(batchSpy).not.toHaveBeenCalled();
});
@@ -85,7 +85,7 @@ vi.mock('together-ai', () => {
// ── Test harness ────────────────────────────────────────────────────
let server: PuterServer;
let hasCreditsSpy: MockInstance<MeteringService['hasEnoughCredits']>;
let admitSpy: MockInstance<MeteringService['reserveCreditsIfAffordable']>;
let remainingUsageSpy: MockInstance<MeteringService['getRemainingUsage']>;
let incrementUsageSpy: MockInstance<MeteringService['incrementUsage']>;
@@ -117,11 +117,13 @@ beforeEach(() => {
videosCreateMock.mockReset();
videosRetrieveMock.mockReset();
togetherCtor.mockReset();
hasCreditsSpy = vi.spyOn(server.services.metering, 'hasEnoughCredits');
admitSpy = vi.spyOn(server.services.metering, 'reserveCreditsIfAffordable');
// Default to "has credits" so per-test setup only needs to override
// for the explicit credit-gate scenarios. SYSTEM_ACTOR has no uuid,
// so the live `getRemainingUsage` path can short-circuit to 0.
hasCreditsSpy.mockResolvedValue(true);
admitSpy.mockImplementation((who, amount, opts) =>
server.services.metering.reserveCredits(who, amount, opts),
);
remainingUsageSpy = vi.spyOn(server.services.metering, 'getRemainingUsage');
remainingUsageSpy.mockResolvedValue(AMPLE_CREDIT);
incrementUsageSpy = vi.spyOn(server.services.metering, 'incrementUsage');
@@ -197,7 +199,7 @@ describe('TogetherVideoProvider.generate test_mode', () => {
provider.generate({ prompt: 'hi', test_mode: true }),
);
expect(result).toBe('https://assets.puter.site/txt2vid.mp4');
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
expect(videosCreateMock).not.toHaveBeenCalled();
});
});
@@ -238,7 +240,7 @@ const DIRECTOR = 'togetherai:minimax/video-01-director';
describe('TogetherVideoProvider.generate credit gate', () => {
it('throws 402 BEFORE hitting Together when actor lacks credits', async () => {
const provider = makeProvider();
hasCreditsSpy.mockResolvedValueOnce(false);
admitSpy.mockResolvedValueOnce(null);
await expect(
withTestActor(() =>
@@ -715,7 +717,7 @@ describe('TogetherVideoProvider.generate per-second models', () => {
expect('height' in sent).toBe(false);
expect('ratio' in sent).toBe(false);
// The flat-rate gate is not consulted for per-second models.
expect(hasCreditsSpy).not.toHaveBeenCalled();
expect(admitSpy).not.toHaveBeenCalled();
const seedance = TOGETHER_VIDEO_GENERATION_MODELS.find(
(m) => m.model === 'ByteDance/Seedance-2.5',
@@ -188,7 +188,11 @@ describe('app.message with live delivery over the socket wire', () => {
captureBus(state.server, 'outer.gui.notif.message', async () => {
created = (await withActor(asApp(holder, app), () =>
state.driver.create(
message({ text: 'Body', channel: 'replies' }),
message({
text: 'Body',
priority: 'high',
channel: 'replies',
}),
),
)) as Record<string, unknown>;
}).then((seen) => (desktop = seen)),
@@ -200,7 +204,7 @@ describe('app.message with live delivery over the socket wire', () => {
title: 'Hello',
text: 'Body',
channel: 'replies',
priority: 'normal',
priority: 'high',
ttl: 3600,
app: { uid: app.uid, name: 'App message test' },
});
@@ -212,6 +216,7 @@ describe('app.message with live delivery over the socket wire', () => {
app_uid: app.uid,
audience: 'app-user',
type: 'app.message',
value: expect.objectContaining({ priority: 'high' }),
});
// Not silent, but sent for the app alone: the desktop wire stays quiet.
expect(written).toEqual([
@@ -220,7 +225,7 @@ describe('app.message with live delivery over the socket wire', () => {
uid,
type: 'app.message',
appUid: app.uid,
}),
value: expect.objectContaining({ priority: 'high' }), }),
]);
expect(desktop).toEqual([]);
});
@@ -207,6 +207,7 @@ describe('app.message to the other party of a share', () => {
const written = await captureBus('notif.written', async () => {
sent = await send(inApp(owner, app), {
title: 'New message',
priority: 'high',
to: { handle },
});
});
@@ -222,7 +223,7 @@ describe('app.message to the other party of a share', () => {
expect(row!.value).toEqual({
type: 'app.message',
title: 'New message',
priority: 'normal',
priority: 'high',
ttl: 3600,
app: { uid: app.uid, name: 'Handle App' },
});
@@ -165,6 +165,7 @@ const fakeSchedules = (plan: Partial<DeferredSendPlan>) =>
createFor: vi.fn(),
list: vi.fn(),
get: vi.fn(),
update: vi.fn(),
remove: vi.fn(),
receivePeerChanges: vi.fn(),
prepareSendAt: vi.fn(async (): Promise<DeferredSendPlan> => ({
@@ -270,7 +271,9 @@ describe('notify({ sendAt }) with services.schedules', () => {
const schedules = fakeSchedules({ sendAt: at });
setSchedules(schedules);
await expect(notify({ sendAt: at, text: 'later' })).resolves.toEqual({
await expect(
notify({ sendAt: at, text: 'later', priority: 'high' }),
).resolves.toEqual({
scheduled: { scheduleId: 'sched-1', sendAt: at },
});
expect(schedules.prepareSendAt).toHaveBeenCalledWith(
@@ -283,6 +286,7 @@ describe('notify({ sendAt }) with services.schedules', () => {
message: expect.objectContaining({
title: 'Hi',
text: 'later',
priority: 'high',
}),
},
);
@@ -318,11 +322,20 @@ describe('deferredAppMessages.write', () => {
const key = uuidv4();
const id = await claimed(token);
await expect(write(key, txFor(id, token))).resolves.toBe('delivered');
await expect(
write(key, txFor(id, token), {
payload: JSON.stringify({
message: { title: 'Later', priority: 'high' },
}),
}),
).resolves.toBe('delivered');
const rows = await mailbox();
expect(rows).toHaveLength(1);
expect(rows[0]).toMatchObject({ uid: key, send_key: key });
expect(valueOf(rows[0])).toMatchObject({ title: 'Later' });
expect(valueOf(rows[0])).toMatchObject({
title: 'Later',
priority: 'high',
});
});
it('writes nothing when the guard no longer holds: another claim took it', async () => {
@@ -859,3 +859,32 @@ describe('NotificationDriver — rows written before the scope columns', () => {
expect(await selectUids(holder.actor)).toEqual([uid]);
});
});
describe('NotificationDriver displayed notification catch-up', () => {
it('selects named ids within the app and user scope', async () => {
const box = await mailbox();
await server.services.notification.markAcknowledged(box.rows.appUser, box.holder.userId);
const rows = await withActor(box.ownAppActor, () => driver.select({
subject: 'notif:app-user',
uids: Object.values(box.rows),
limit: 50,
}));
expect(rows).toEqual([expect.objectContaining({
uid: box.rows.appUser, acknowledged: expect.any(Number),
})]);
});
it.each([Array(51).fill('one'), [''], [7], 'one', ['x'.repeat(129)]])(
'rejects an invalid or oversized id batch: %s', async (uids) => {
const { actor } = await makeUser();
await expect(withActor(actor, () => driver.select({ uids })))
.rejects.toMatchObject({ statusCode: 400 });
},
);
it('returns no rows for an empty id batch', async () => {
const { actor } = await makeUser();
expect(await withActor(actor, () => driver.select({ uids: [] }))).toEqual([]);
});
});
@@ -46,7 +46,7 @@ import {
ownedAppUids,
} from '../../services/notification/notificationAudience.js';
import {
NOTIFICATION_VALUE_MAX_BYTES,
assertNotificationValueFits,
OUTSIDE_ACCOUNT_MAILBOX_TYPES,
resolveNotificationWrite,
type NotificationTypeName,
@@ -120,19 +120,6 @@ interface MailboxScope {
appUid: string | null | undefined;
}
const assertValueFits = (value: Record<string, unknown>): void => {
if (
Buffer.byteLength(JSON.stringify(value), 'utf8') >
NOTIFICATION_VALUE_MAX_BYTES
) {
throw new HttpError(
400,
`\`value\` may not exceed ${NOTIFICATION_VALUE_MAX_BYTES} bytes`,
{ legacyCode: 'notification_value_too_large' },
);
}
};
const typeNotAllowed = (message: string): HttpError =>
new HttpError(403, message, {
legacyCode: 'forbidden',
@@ -277,7 +264,7 @@ export class NotificationDriver extends PuterDriver {
value,
);
}
assertValueFits(value);
assertNotificationValueFits(value);
if (type === APP_MESSAGE_TYPE) {
throw typeNotAllowed(`Only apps can create ${APP_MESSAGE_TYPE}`);
}
@@ -342,13 +329,34 @@ export class NotificationDriver extends PuterDriver {
: predicate;
const scope = this.#resolveScope(actor, args.subject);
const uids = args.uids;
if (
uids !== undefined &&
(!Array.isArray(uids) ||
uids.length > 50 ||
uids.some(
(uid) =>
typeof uid !== 'string' ||
uid.length === 0 ||
uid.length > 128,
))
) {
throw new HttpError(
400,
'uids must contain at most 50 notification ids',
{
legacyCode: 'bad_request',
},
);
}
const query: {
limit: number;
scope: MailboxScope | null;
filter?: string;
onlyUnacknowledged?: boolean;
excludeTypes?: readonly string[];
} = { limit, scope };
uids?: string[];
} = { limit, scope, ...(uids ? { uids: uids as string[] } : {}) };
// The whole mailbox, or a slice naming no app, is the desktop's view of
// it, which leaves out the types only an app has a use for. A slice
// naming the app still returns them.
@@ -430,7 +438,7 @@ export class NotificationDriver extends PuterDriver {
}
const handle = to === undefined ? undefined : parseNotifyTarget(to);
const message = parseAppMessage(fields);
assertValueFits(value);
assertNotificationValueFits(value);
const timing =
sendAt === undefined || sendAt === null
? null
@@ -539,9 +547,7 @@ export class NotificationDriver extends PuterDriver {
};
}
/**
* For extensions. A field, not a method, so `/drivers/call` can't reach it.
*/
/** For extensions. A field, not a method, so `/drivers/call` can't reach it. */
readonly deferredAppMessages = {
write: (input: DeferredWriteInput, tx: DeferredWriteTx) =>
this.#writeDeferredAppMessage(input, tx),
+89 -29
View File
@@ -324,8 +324,10 @@ describe('AI cost factor', () => {
it('gates and holds at the factored cost', async () => {
listen(1.3);
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
const reserve = vi.spyOn(metering, 'reserveCredits');
try {
const hold = await scoped.reserveAiCredits(
@@ -334,7 +336,11 @@ describe('AI cost factor', () => {
1000,
);
await hold?.release();
expect(check).toHaveBeenCalledWith(actor, 1300);
expect(check).toHaveBeenCalledExactlyOnceWith(
actor,
1300,
undefined,
);
expect(reserve).toHaveBeenCalledWith(actor, 1300, undefined);
} finally {
check.mockRestore();
@@ -342,10 +348,40 @@ describe('AI cost factor', () => {
}
});
it('admits one of two concurrent holds the balance only covers once', async () => {
listen(1.3);
vi.spyOn(metering, 'getAllowedUsage').mockResolvedValue({
remaining: 20_000_000,
monthUsageAllowance: 20_000_000,
addons: {},
});
const holds = await Promise.all(
[0, 1].map(() =>
scoped.reserveAiCredits(
actor,
'openai:gpt-realtime:realtime:audio_output',
12_500_000,
),
),
);
try {
expect(holds.filter((h) => h !== null)).toHaveLength(1);
// The factor applies once: 12.5M at 1.3 is 16.25M held.
expect(await metering.getOutstandingHolds(actor)).toBe(
16_250_000,
);
} finally {
await Promise.all(holds.map((h) => h?.release()));
vi.restoreAllMocks();
}
});
it('passes a hold lifetime through to the service', async () => {
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
const reserve = vi.spyOn(metering, 'reserveCredits');
try {
const hold = await scoped.reserveAiCredits(
@@ -367,8 +403,12 @@ describe('AI cost factor', () => {
it('takes no hold when the factored cost is unaffordable', async () => {
listen(1.3);
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockImplementation(async (_actor, amount) => amount <= 1000);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation(async (who, amount, opts) =>
amount <= 1000
? metering.reserveCredits(who, amount, opts)
: null,
);
const reserve = vi.spyOn(metering, 'reserveCredits');
try {
const hold = await scoped.reserveAiCredits(
@@ -409,8 +449,10 @@ describe('AI cost factor', () => {
it('holds the factored cost while run executes, then releases it', async () => {
listen(1.3);
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
try {
let heldDuringRun = 0;
const result = await withAiCreditHold(
@@ -448,8 +490,10 @@ describe('AI cost factor', () => {
};
server.clients.event.on('ai.cost.factor.*', slowFactor as never);
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
try {
await runWithContext({ actor }, () =>
withAiCreditHold(
@@ -485,8 +529,10 @@ describe('AI cost factor', () => {
it('releases the hold when the usage write fails', async () => {
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
const write = vi
.spyOn(metering, 'incrementUsage')
.mockRejectedValueOnce(new Error('ledger down'));
@@ -515,8 +561,10 @@ describe('AI cost factor', () => {
it('releases at once when nothing was recorded', async () => {
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
try {
await runWithContext({ actor }, () =>
withAiCreditHold(
@@ -535,8 +583,10 @@ describe('AI cost factor', () => {
it('releases the hold when run throws', async () => {
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
const boom = new Error('upstream down');
try {
await expect(
@@ -552,8 +602,8 @@ describe('AI cost factor', () => {
it('refuses with a 402 and never runs when the cost is unaffordable', async () => {
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(false);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockResolvedValue(null);
const run = vi.fn();
try {
await expect(
@@ -575,8 +625,10 @@ describe('AI cost factor', () => {
const relay = async (source: Readable) => {
const record = vi.fn();
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
try {
const result = await withStreamedAiCharge(
scoped,
@@ -624,8 +676,10 @@ describe('AI cost factor', () => {
landCharge = resolve;
});
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
try {
const result = await withStreamedAiCharge(
scoped,
@@ -655,8 +709,10 @@ describe('AI cost factor', () => {
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
const refresh = vi.spyOn(server.stores.creditHold, 'refresh');
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
let landCharge = () => {};
const charge = new Promise<void>((resolve) => {
landCharge = resolve;
@@ -697,8 +753,10 @@ describe('AI cost factor', () => {
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
const refresh = vi.spyOn(server.stores.creditHold, 'refresh');
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
let opened = () => {};
const opening = new Promise<void>((resolve) => {
opened = resolve;
@@ -741,8 +799,10 @@ describe('AI cost factor', () => {
it('releases the hold when recording the charge throws', async () => {
const check = vi
.spyOn(metering, 'hasEnoughCredits')
.mockResolvedValue(true);
.spyOn(metering, 'reserveCreditsIfAffordable')
.mockImplementation((who, amount, opts) =>
metering.reserveCredits(who, amount, opts),
);
try {
const result = await withStreamedAiCharge(
scoped,
+5 -3
View File
@@ -354,10 +354,12 @@ export function withAiCostFactor(
? await costFactor(actor, aiModelKey(usageType))
: 1;
const cost = factor === 1 ? amount : scaleCost(amount, factor);
if (!(await metering.hasEnoughCredits(actor, cost))) return null;
return outlastingWrites(
await metering.reserveCredits(actor, cost, opts),
const hold = await metering.reserveCreditsIfAffordable(
actor,
cost,
opts,
);
return hold && outlastingWrites(hold);
};
const reserveCredits = async (
+229
View File
@@ -336,6 +336,70 @@ describe('PuterServer host header validation', () => {
expect(api.headers['x-frame-options']).toBeUndefined();
});
it('allows deployment subdomains to frame the app browser embed', async () => {
const parent = `http://spreadsheet.puter.localhost:${port}`;
for (const path of ['/embed/apps', '/embed/apps/']) {
const res = await request(path, {
host: `puter.localhost:${port}`,
referer: `${parent}/sheet/1`,
});
expect(res.status).toBe(200);
expect(res.headers['x-frame-options']).toBeUndefined();
expect(res.headers['content-security-policy']).toBe(
`frame-ancestors 'self' ${parent}`,
);
expect(res.headers['cache-control']).toBe('no-store');
expect(res.body).toContain('/dist/apps-embed.min.js');
expect(res.body).not.toContain('/dist/bundle.min.js');
expect(res.body).toContain(`http://api.puter.localhost:${port}`);
const config = JSON.parse(
res.body.match(/id="app-browser-config">(.*?)<\/script>/)[1],
);
expect(Array.isArray(config.guestApps)).toBe(true);
}
const gui = await request('/embed/apps', {
host: `puter.localhost:${port}`,
referer: `http://puter.localhost:${port}/dashboard`,
});
expect(gui.headers['content-security-policy']).toBe("frame-ancestors 'self'");
const dashboard = await request('/dashboard', { host: `puter.localhost:${port}` });
expect(dashboard.headers['x-frame-options']).toBe('SAMEORIGIN');
const api = await request('/embed/apps', { host: `api.puter.localhost:${port}` });
expect(api.status).toBe(404);
});
it.each([
['a user-hosted site', 'http://someone.site.puter.localhost:PORT/'],
['a private app host', 'http://someone.app.puter.localhost:PORT/'],
['the API host', 'http://api.puter.localhost:PORT/'],
['the WebDAV host', 'http://dav.puter.localhost:PORT/'],
['a native app host', 'http://about.puter.localhost:PORT/'],
['a nested subdomain', 'http://a.spreadsheet.puter.localhost:PORT/'],
['another site', 'http://evil.test:PORT/'],
['another port', 'http://spreadsheet.puter.localhost:1/'],
['a suppressed referrer', undefined],
])('keeps the embed same-origin only when framed by %s', async (_label, referer) => {
const res = await request('/embed/apps', {
host: `puter.localhost:${port}`,
...(referer ? { referer: referer.replace('PORT', String(port)) } : {}),
});
expect(res.status).toBe(200);
expect(res.headers['content-security-policy']).toBe("frame-ancestors 'self'");
});
it.each([
['plain', ''],
// Past the parser's 1000-pair limit, so `req.query` comes back empty.
['padded', '&'.repeat(1000)],
])('strips %s SDK bootstrap query parameters from the embed route', async (_label, padding) => {
const res = await request(`/embed/apps?${padding}puter.app_instance_id=other&puter.api_origin=https://other.test`, {
host: `puter.localhost:${port}`,
});
expect(res.status).toBe(302);
expect(res.headers.location).toBe('/embed/apps');
expect(res.headers['x-frame-options']).toBe('SAMEORIGIN');
});
it('blocks a request the ip.validate listeners veto', async () => {
const handler = (_key: unknown, data: unknown) => {
(data as { allow: boolean }).allow = false;
@@ -1492,6 +1556,104 @@ describe('PuterServer keep-alive timeout', () => {
});
describe('PuterServer lifecycle hooks', () => {
it('fails startup before running later hooks when a start hook rejects', async () => {
const failure = new Error('startup failed');
const later = vi.fn();
class FailingStart extends JobScheduler {
override async onServerStart() {
throw failure;
}
}
class LaterStart extends JobScheduler {
override async onServerStart() {
later();
}
}
extension.registerService('failingStart', FailingStart as never);
extension.registerService('laterStart', LaterStart as never);
try {
await expect(setupTestServer()).rejects.toBe(failure);
expect(later).not.toHaveBeenCalled();
} finally {
delete (extensionStore.services as Record<string, unknown>)
.failingStart;
delete (extensionStore.services as Record<string, unknown>)
.laterStart;
}
});
it.each([false, true])(
'continues teardown after failing hooks with listener callback %s',
async (prepareFirst) => {
const port = await allocateEphemeralPort();
const server = await setupTestServer(
{ port } as unknown as IConfig,
{ listen: true },
);
const logged = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const ran: string[] = [];
class FailingHooks {
onServerPrepareShutdown() {
ran.push('prepare:failed');
throw new Error('prepare failed');
}
async onServerShutdown() {
ran.push('shutdown:failed');
throw new Error('shutdown failed');
}
}
(server.services as Record<string, unknown>).fails =
new FailingHooks();
(server.services as Record<string, unknown>).after = {
onServerPrepareShutdown: () => ran.push('prepare:after'),
onServerShutdown: () => ran.push('shutdown:after'),
};
(server.clients as Record<string, unknown>).last = {
onServerShutdown: () => ran.push('shutdown:client'),
};
try {
if (prepareFirst) {
await server.prepareShutdown();
await vi.waitFor(() =>
expect(ran).toContain('prepare:after'),
);
}
await server.shutdown();
expect(ran).toEqual([
'prepare:failed',
'prepare:after',
'shutdown:failed',
'shutdown:after',
'shutdown:client',
]);
expect(logged).toHaveBeenCalledWith(
'[server] onServerPrepareShutdown failed in FailingHooks',
expect.any(Error),
);
expect(logged).toHaveBeenCalledWith(
'[server] onServerShutdown failed in FailingHooks',
expect.any(Error),
);
await expect(
new Promise<void>((resolve, reject) => {
const socket = net.connect(port, '127.0.0.1');
socket.on('connect', () => {
socket.destroy();
resolve();
});
socket.on('error', reject);
}),
).rejects.toMatchObject({ code: 'ECONNREFUSED' });
} finally {
delete (server.services as Record<string, unknown>).fails;
await server.shutdown();
logged.mockRestore();
}
},
);
it('runs drain-start and prepare-shutdown bottom-up, once, before tearing down', async () => {
const port = await allocateEphemeralPort();
const server = await setupTestServer({ port } as unknown as IConfig, {
@@ -1541,6 +1703,73 @@ describe('PuterServer lifecycle hooks', () => {
'shutdown:client',
]);
});
it('keeps accepting connections until every drain-start hook settles, without one holding up another', async () => {
const port = await allocateEphemeralPort();
const server = await setupTestServer({ port } as unknown as IConfig, {
listen: true,
});
let release!: () => void;
const held = new Promise<void>((resolve) => (release = resolve));
let otherRan = false;
(server.services as unknown as Record<string, unknown>).holdsListener =
{ onServerDrainStart: () => held };
(server.drivers as unknown as Record<string, unknown>).other = {
onServerDrainStart: () => {
otherRan = true;
},
};
const connect = () =>
new Promise<void>((resolve, reject) => {
const socket = net.connect(port, '127.0.0.1');
socket.on('connect', () => {
socket.destroy();
resolve();
});
socket.on('error', reject);
});
const prepared = server.prepareShutdown({ drainMs: 1_000 });
await new Promise((resolve) => setTimeout(resolve, 20));
expect(otherRan).toBe(true);
await expect(connect()).resolves.toBeUndefined();
release();
await prepared;
await expect(connect()).rejects.toMatchObject({
code: 'ECONNREFUSED',
});
await server.shutdown();
});
it('runs every drain-start hook when one of them throws', async () => {
const port = await allocateEphemeralPort();
const server = await setupTestServer({ port } as unknown as IConfig, {
listen: true,
});
vi.spyOn(console, 'error').mockImplementation(() => {});
const ran: string[] = [];
(server.stores as unknown as Record<string, unknown>).fails = {
onServerDrainStart: () => {
throw new Error('drain hook failed');
},
};
(server.services as unknown as Record<string, unknown>).after = {
onServerDrainStart: () => {
ran.push('after');
},
};
await server.prepareShutdown({ drainMs: 1_000 });
await server.shutdown();
expect(ran).toEqual(['after']);
expect(console.error).toHaveBeenCalledWith(
expect.stringContaining('onServerDrainStart failed'),
expect.any(Error),
);
});
});
describe('PuterServer shutdown during prepare', () => {
+47 -13
View File
@@ -1743,12 +1743,7 @@ export class PuterServer {
*/
async #runHook(
order: 'bottomUp' | 'topDown',
hook:
| 'onServerStart'
| 'onServerDrainStart'
| 'onServerPrepareShutdown'
| 'onServerShutdown',
info?: ServerDrainInfo,
hook: 'onServerStart' | 'onServerPrepareShutdown' | 'onServerShutdown',
) {
const layers = [
this.clients,
@@ -1760,11 +1755,47 @@ export class PuterServer {
if (order === 'topDown') layers.reverse();
for (const layer of layers) {
for (const instance of Object.values(layer) as WithLifecycle[]) {
await instance[hook]?.(info);
try {
await instance[hook]?.();
} catch (err) {
if (hook === 'onServerStart') throw err;
console.error(
`[server] ${hook} failed in ${instance.constructor.name}`,
err,
);
}
}
}
}
/**
* Run every `onServerDrainStart` concurrently, so one hook holding the
* listener open doesn't delay the others, and one failing doesn't skip the
* rest.
*/
async #runDrainStartHooks(info: ServerDrainInfo) {
const instances = [
this.clients,
this.stores,
this.services,
this.controllers,
this.drivers,
].flatMap((layer) => Object.values(layer) as WithLifecycle[]);
const results = await Promise.allSettled(
instances.map(async (instance) =>
instance.onServerDrainStart?.(info),
),
);
results.forEach((result, i) => {
if (result.status === 'rejected') {
console.error(
`[server] onServerDrainStart failed in ${instances[i].constructor.name}`,
result.reason,
);
}
});
}
#prepareShutdownHooks: Promise<void> | null = null;
/**
@@ -1782,16 +1813,19 @@ export class PuterServer {
async prepareShutdown(info: ServerDrainInfo = {}) {
if (this.#server) {
try {
await this.#runHook('bottomUp', 'onServerDrainStart', info);
} catch (err) {
console.error('[server] onServerDrainStart failed', err);
}
await this.#runDrainStartHooks(info);
this.#server.close(async () => {
console.log(
'PuterServer has stopped accepting new connections',
);
await this.#runPrepareShutdownHooks();
try {
await this.#runPrepareShutdownHooks();
} catch (err) {
console.error(
'[server] prepare-shutdown hooks failed',
err,
);
}
});
}
}
@@ -27,17 +27,16 @@ import { PuterService } from '../types.js';
*/
export const RECOMMENDED_APP_NAMES = [
'builder',
'word-processor',
'spreadsheet',
'presentation',
'whiteboard',
'pdf-editor',
'contacts',
'calendar',
'meetings',
'teamchat',
'email',
'whiteboard',
'spreadsheet',
'word-processor',
'presentation',
'pdf-editor',
'terminal',
'dev-center',
'cap-table',
'invoices',
@@ -1149,3 +1149,154 @@ describe('BroadcastService — same-cluster pub/sub fan-out', () => {
}
});
});
describe('BroadcastService shutdown drain', () => {
let server: PuterServer;
let broadcast: BroadcastService;
beforeEach(async () => {
axiosRequestMock.mockReset();
axiosRequestMock.mockResolvedValue({ status: 200, data: 'ok' });
server = await setupTestServer({
broadcast: {
webhook: { peerId: SELF_PEER_ID, secret: SELF_SECRET },
peers: [
{
peerId: PEER_ID,
webhook: true,
webhook_url: PEER_URL,
webhook_secret: PEER_SECRET,
},
],
outbound_flush_ms: 10,
},
} as never);
broadcast = server.services.broadcast as unknown as BroadcastService;
});
afterEach(async () => {
vi.useRealTimers();
await broadcast.onServerPrepareShutdown();
await broadcast.onServerShutdown();
await server.shutdown();
});
const emit = (hash: string) =>
server.clients.event.emit(
'outer.fs.write-hash' as never,
{ hash, uuid: hash } as never,
{},
);
it.each(['onServerPrepareShutdown', 'onServerShutdown'] as const)(
'waits for the active flush and sends its tail in %s',
async (hook) => {
let release!: () => void;
const held = new Promise<void>((resolve) => {
release = resolve;
});
let releaseTail!: () => void;
const tailHeld = new Promise<void>((resolve) => {
releaseTail = resolve;
});
axiosRequestMock
.mockImplementationOnce(async () => {
await held;
return { status: 200, data: 'ok' };
})
.mockImplementationOnce(async () => {
await tailHeld;
return { status: 200, data: 'ok' };
});
emit('first');
await vi.waitFor(() =>
expect(axiosRequestMock).toHaveBeenCalledTimes(1),
);
emit('tail');
let finished = false;
const draining = broadcast[hook]().then(() => {
finished = true;
});
try {
await new Promise((resolve) => setTimeout(resolve, 20));
expect(finished).toBe(false);
release();
await vi.waitFor(() =>
expect(axiosRequestMock).toHaveBeenCalledTimes(2),
);
expect(finished).toBe(false);
} finally {
release();
releaseTail();
await draining;
}
expect(axiosRequestMock).toHaveBeenCalledTimes(2);
expect(axiosRequestMock.mock.calls[1][0].data).toContain('tail');
},
);
it('keeps normal timer flushing after prepare on an idle server', async () => {
await broadcast.onServerPrepareShutdown();
vi.useFakeTimers();
axiosRequestMock.mockClear();
emit('during-drain');
await vi.advanceTimersByTimeAsync(10);
expect(axiosRequestMock).toHaveBeenCalledTimes(1);
expect(axiosRequestMock.mock.calls[0][0].data).toContain(
'during-drain',
);
});
it('sends events queued after prepare during shutdown and clears the timer', async () => {
await broadcast.onServerPrepareShutdown();
vi.useFakeTimers();
axiosRequestMock.mockClear();
emit('after-prepare');
await broadcast.onServerShutdown();
expect(axiosRequestMock).toHaveBeenCalledTimes(1);
expect(axiosRequestMock.mock.calls[0][0].data).toContain(
'after-prepare',
);
axiosRequestMock.mockClear();
await vi.advanceTimersByTimeAsync(100);
expect(axiosRequestMock).not.toHaveBeenCalled();
});
it.each(['onServerPrepareShutdown', 'onServerShutdown'] as const)(
'bounds a hung flush in %s',
async (hook) => {
let release!: () => void;
const held = new Promise<void>((resolve) => {
release = resolve;
});
axiosRequestMock.mockImplementationOnce(async () => {
await held;
return { status: 200, data: 'ok' };
});
emit('hung');
await vi.waitFor(() =>
expect(axiosRequestMock).toHaveBeenCalledTimes(1),
);
vi.useFakeTimers();
emit('tail');
let finished = false;
const draining = broadcast[hook]().then(() => {
finished = true;
});
try {
await vi.advanceTimersByTimeAsync(4_999);
expect(finished).toBe(false);
await vi.advanceTimersByTimeAsync(1);
await draining;
release();
await vi.advanceTimersByTimeAsync(100);
expect(axiosRequestMock).toHaveBeenCalledTimes(
hook === 'onServerPrepareShutdown' ? 2 : 1,
);
} finally {
release();
vi.useRealTimers();
}
},
);
});
@@ -21,6 +21,8 @@ import axios from 'axios';
import { createHmac, randomUUID, timingSafeEqual } from 'node:crypto';
import { Agent as HttpsAgent } from 'node:https';
import { IBroadcastPeerConfig } from '../../types.js';
import { settleWithin } from '../../util/concurrency.js';
import { FINAL_FLUSH_TIMEOUT_MS } from '../../util/gracefulShutdown.js';
import { PuterService } from '../types.js';
// -- Wire types ------------------------------------------------------
@@ -121,7 +123,8 @@ export class BroadcastService extends PuterService {
/** Per peer, serialized events it has not accepted yet, oldest first. */
#outboundBacklogs = new Map<IBroadcastPeerConfig, string[]>();
#outboundFlushTimer: ReturnType<typeof setTimeout> | null = null;
#outboundIsFlushing = false;
#outboundFlush: Promise<void> | null = null;
#outboundDraining = false;
#webhookReplayWindowSeconds = 300;
#outboundFlushMs = 2000;
@@ -165,21 +168,40 @@ export class BroadcastService extends PuterService {
}
override async onServerPrepareShutdown(): Promise<void> {
await this.#drainOutboundEvents();
if (this.#redisSub) {
try {
await this.#redisSub.unsubscribe('pubsub');
this.#redisSub.quit();
} catch (err) {
console.warn('[broadcast] unsubscribe failed', err);
} finally {
this.#redisSub = null;
}
}
}
override async onServerShutdown(): Promise<void> {
this.#outboundDraining = true;
await this.#drainOutboundEvents();
}
async #drainOutboundEvents(): Promise<void> {
if (this.#outboundFlushTimer) {
clearTimeout(this.#outboundFlushTimer);
this.#outboundFlushTimer = null;
}
// Best-effort drain — try one final flush so events queued near
// shutdown make it out.
try {
await this.#flushOutboundEvents();
} catch (err) {
console.warn('[broadcast] final flush failed', err);
}
if (this.#redisSub) {
await this.#redisSub.unsubscribe('pubsub');
this.#redisSub.quit();
this.#redisSub = null;
const deadline = Date.now() + FINAL_FLUSH_TIMEOUT_MS;
const drain = async () => {
try {
await this.#outboundFlush;
if (Date.now() < deadline) await this.#flushOutboundEvents();
} catch (err) {
console.warn('[broadcast] final flush failed', err);
}
};
if (!(await settleWithin(drain(), FINAL_FLUSH_TIMEOUT_MS))) {
console.warn('[broadcast] final flush timed out');
}
}
@@ -453,24 +475,27 @@ export class BroadcastService extends PuterService {
}
#scheduleOutboundFlush(): void {
if (this.#outboundFlushTimer) return;
this.#outboundFlushTimer = setTimeout(() => {
if (this.#outboundDraining || this.#outboundFlushTimer) return;
this.#outboundFlushTimer = setTimeout(async () => {
this.#outboundFlushTimer = null;
void this.#flushOutboundEvents().catch((err) => {
try {
await this.#flushOutboundEvents();
} catch (err) {
console.warn('[broadcast] outbound flush failed', err);
});
}
}, this.#outboundFlushMs);
}
async #flushOutboundEvents(): Promise<void> {
if (
this.#outboundIsFlushing ||
(this.#outboundQueue.size === 0 &&
this.#outboundBacklogs.size === 0)
)
return;
#flushOutboundEvents(): Promise<void> {
if (this.#outboundFlush) return this.#outboundFlush;
if (this.#outboundQueue.size === 0 && this.#outboundBacklogs.size === 0)
return Promise.resolve();
this.#outboundIsFlushing = true;
this.#outboundFlush = this.#sendOutboundEvents();
return this.#outboundFlush;
}
async #sendOutboundEvents(): Promise<void> {
try {
const fresh = this.#outboundQueue;
this.#outboundQueue = new Set();
@@ -504,7 +529,7 @@ export class BroadcastService extends PuterService {
for (const result of flushed)
if (result.status === 'rejected') throw result.reason;
} finally {
this.#outboundIsFlushing = false;
this.#outboundFlush = null;
// Anything that arrived during flush, or is still owed to a peer,
// gets the next tick.
if (
+107 -29
View File
@@ -56,6 +56,7 @@ import {
} from '../../core/actor.js';
import { HttpError, isHttpError } from '../../core/http/HttpError.js';
import type {
SqlGuard,
ScheduleEndReason,
ScheduleSubscriptionChange,
} from '../../core/schedules.js';
@@ -246,6 +247,7 @@ import {
isNotifToken,
kvHandleFromSubject,
kvHandleMatchFromSubject,
notifAnchorToken,
notifMatchSpansEveryApp,
parseSubject,
type FsOp,
@@ -680,6 +682,7 @@ export interface KvDispatchOptions {
/** One persisted notification, as the bus reports it. */
export interface NotifDispatchInput {
dismissed?: true;
/** The recipient, whose mailbox is the anchor. */
userId: number;
userUuid: string;
@@ -915,7 +918,7 @@ const scheduleSubscription = (): HttpError =>
const scheduleImmutable = (): HttpError =>
new HttpError(
400,
"A schedule's context can't be changed; remove the schedule and create a new one",
"Use updateSchedule() to change a schedule's context or timing",
{ legacyCode: 'events_schedule_immutable' },
);
@@ -1664,6 +1667,26 @@ export class EventsService extends PuterService {
});
});
this.clients.event.on('notif.acknowledged', (_key, data) => {
if (!this.notificationsFoldIn) return;
void (async () => {
const user = await this.stores.user.getById(data.userId);
if (!user?.uuid) return;
await this.dispatchNotif({
...data,
userUuid: user.uuid,
value: {},
createdAt: Date.now(),
dismissed: true,
});
})().catch((err) => {
console.warn(
'[events] notification dismissal dispatch failed',
err,
);
});
});
this.#armPendingSweep();
if (this.enabled) this.#registerJobs();
}
@@ -2870,6 +2893,7 @@ export class EventsService extends PuterService {
override?: readonly SuspendedReason[];
/** What a schedule ended by this records instead of `reason`. */
scheduleEndReason?: ScheduleEndReason;
guard?: SqlGuard;
} = {},
): Promise<DurableSubscription[]> {
if (rows.length === 0) return [];
@@ -2877,6 +2901,7 @@ export class EventsService extends PuterService {
const { suspended, bumps } =
await this.stores.durableSubscription.suspend(rows, reason, {
override: opts.override,
guard: opts.guard,
});
const policy = backlogPolicyFor(reason);
for (const row of suspended) {
@@ -2992,8 +3017,14 @@ export class EventsService extends PuterService {
* retry path's; this is the state it drives, and the developer is told
* because it is their code that stopped working. Called by extensions.
*/
async suspendForFailures(subId: string): Promise<boolean> {
return this.#suspendOne(subId, 'failures', { notifyDeveloper: true });
async suspendForFailures(
subId: string,
guard?: SqlGuard,
): Promise<boolean> {
return this.#suspendOne(subId, 'failures', {
notifyDeveloper: true,
guard,
});
}
/**
@@ -3668,13 +3699,16 @@ export class EventsService extends PuterService {
async #suspendOne(
subId: string,
reason: SuspendedReason,
options: { notifyDeveloper: boolean },
options: { notifyDeveloper: boolean; guard?: SqlGuard },
): Promise<boolean> {
if (!this.enabled) return false;
const row = await this.stores.durableSubscription.getBySubId(subId);
if (!row || row.suspendedAt !== null) return false;
await this.suspendSubscriptions([row], reason);
const suspended = await this.#suspend([row], reason, {
guard: options.guard,
});
if (suspended.length === 0) return false;
if (options.notifyDeveloper && row.appUid && row.handlerName)
await this.#notifySuspended(scopeOfRow(row), row.handlerName, 1);
return true;
@@ -4554,27 +4588,42 @@ export class EventsService extends PuterService {
return { matched, tokens };
}
if (item.family === 'notif' && item.notif) {
const input = {
...item.notif,
userId: item.ownerUserId,
createdAt: item.ts,
};
const matched = await this.dispatchNotif(input, {
forwarded: true,
id: item.id,
});
return { matched, tokens: [notifAnchorToken(item.notif.userUuid)] };
}
return { matched: false, tokens: [] };
}
/**
* Publish one persisted notification: the desktop's wire first, because
* that is what a person is waiting on, then whatever subscriptions asked
* for this slice of the mailbox.
*
* The fan-out is one token — a notification is addressed to a person — so
* there is no tree to walk and nothing to coalesce against.
* Publish a posting or dismissal to the same mailbox audience. Postings
* also reach the desktop's wire; forwarded copies evaluate session rows
* only.
*/
async dispatchNotif(input: NotifDispatchInput): Promise<void> {
if (!this.notificationsFoldIn) return;
async dispatchNotif(
input: NotifDispatchInput,
options: { forwarded?: boolean; id?: string } = {},
): Promise<boolean> {
if (!this.notificationsFoldIn) return false;
const subject = lookupNotifSubject('notif.created');
if (!subject) return;
if (!subject) return false;
const userId = input?.userId;
if (typeof userId !== 'number' || !input.userUuid || !input.uid) return;
if (typeof userId !== 'number' || !input.userUuid || !input.uid)
return false;
const context: NotifEventContext = {
key: 'notif.created',
key: input.dismissed ? 'notif.acknowledged' : 'notif.created',
...(input.dismissed ? { dismissed: true as const } : {}),
userId,
userUuid: input.userUuid,
uid: input.uid,
@@ -4582,9 +4631,9 @@ export class EventsService extends PuterService {
audience: (input.audience ??
'account') as NotifEventContext['audience'],
appUid: input.appUid ?? null,
notification: input.value ?? {},
notification: input.dismissed ? {} : (input.value ?? {}),
...(input.replaced === true ? { replaced: true as const } : {}),
id: input.uid,
id: options.id ?? (input.dismissed ? randomUUID() : input.uid),
ts: input.createdAt || Date.now(),
handlerDepth: input.handlerDepth,
};
@@ -4592,29 +4641,51 @@ export class EventsService extends PuterService {
// The adapter is not a subscription: the desktop is delivered to
// whether or not anything subscribed, which is what keeps the wire
// contract identical with the fold-in on.
this.services.notification.deliverOverSocket(context);
if (!input.dismissed && !options.forwarded)
this.services.notification.deliverOverSocket(context);
if (!(await this.#userHasAny(userId))) return;
if (!(await this.#userHasAny(userId))) return false;
const watched = await this.stores.eventSubscription.watchedTokens(
userId,
subject.tokens(context),
);
if (watched.length === 0) return;
const { local, remote } =
await this.stores.eventSubscription.watchedFor(
userId,
subject.tokens(context),
);
if (!options.forwarded && remote.size > 0)
this.services.eventForward.forwardEvent(regionsIn(remote), {
family: 'notif',
ownerUserId: userId,
id: context.id,
ts: context.ts,
notif: {
userUuid: input.userUuid,
uid: input.uid,
type: context.type,
audience: context.audience,
appUid: context.appUid,
value: context.notification,
...(input.dismissed ? { dismissed: true as const } : {}),
...(input.replaced ? { replaced: true as const } : {}),
handlerDepth: input.handlerDepth,
},
});
if (local.length === 0) return false;
const fetched = await this.stores.eventSubscription.getForTokens(
userId,
watched,
local,
{ keep: dispatchKeeps(options.forwarded) },
);
// A subscription naming no app is the desktop's view of the mailbox.
const rows = inAccountMailbox(context.type)
? fetched
: fetched.filter((row) => !notifMatchSpansEveryApp(row.match));
if (rows.length === 0) return;
if (rows.length === 0) return true;
await this.#route(subject, context, rows, userId, (matched) =>
this.#notifStillAuthorized(matched, context),
);
return true;
}
/**
@@ -6613,8 +6684,10 @@ export class EventsService extends PuterService {
*/
async invokeForSchedule(
invocation: WorkerInvocation,
options: Pick<WorkerRunOptions, 'stampEvent'> = {},
options: Pick<WorkerRunOptions, 'stampEvent' | 'beforeSend'> = {},
): Promise<ScheduleInvokeResult> {
if (options.beforeSend && !(await options.beforeSend()))
return { outcome: 'deferred' };
if (await this.#tooDeep(invocation)) return { outcome: 'too-deep' };
if (await this.#suspendIfHandlerGone(invocation))
return { outcome: 'deferred' };
@@ -6855,7 +6928,12 @@ export class EventsService extends PuterService {
delivery: AddressedDelivery,
): void {
if (this.#ended.has(row.subId) || this.#parkedUserRow(row)) return;
this.#coalesce().push(coalesceKey(row.subId, subject), delivery);
const event = delivery.envelope.event;
const key =
event.op === 'dismiss'
? `${subject}:dismiss:${event.uid}`
: subject;
this.#coalesce().push(coalesceKey(row.subId, key), delivery);
}
async #flush(delivery: AddressedDelivery): Promise<void> {
@@ -217,6 +217,97 @@ describe('the addressed peer queue', () => {
).toEqual(['b']);
});
it.each([false, true])(
'waits for an active send and ships its tail after failure %s',
async (failFirst) => {
let release!: () => void;
const held = new Promise<void>((resolve) => {
release = resolve;
});
let releaseTail!: () => void;
const tailHeld = new Promise<void>((resolve) => {
releaseTail = resolve;
});
const sent: ForwardItem[][] = [];
const { queue } = makeQueue({
maxItems: 1,
flushMs: 60_000,
send: async (_peerId, items) => {
sent.push(items);
if (sent.length === 1) {
await held;
if (failFirst) throw new Error('peer unavailable');
} else {
await tailHeld;
}
},
});
queue.push('east', delivery('first'));
await vi.waitFor(() => expect(sent).toHaveLength(1));
queue.push('east', delivery('tail'));
let finished = false;
const draining = queue.flushAll().then(() => {
finished = true;
});
try {
await new Promise((resolve) => setTimeout(resolve, 20));
expect(finished).toBe(false);
release();
await vi.waitFor(() => expect(sent).toHaveLength(2));
expect(finished).toBe(false);
} finally {
release();
releaseTail();
await draining;
queue.stop();
}
expect(sent).toEqual([[delivery('first')], [delivery('tail')]]);
},
);
it('releases the peer after a send throws synchronously', async () => {
const send = vi.fn(() => {
throw new Error('send failed');
});
const { queue } = makeQueue({ maxItems: 1, send });
queue.push('east', delivery('first'));
await queue.flushAll();
queue.push('east', delivery('second'));
await queue.flushAll();
expect(send).toHaveBeenCalledTimes(2);
});
it('bounds a final drain while a send is hung', async () => {
vi.useFakeTimers();
let release!: () => void;
const held = new Promise<void>((resolve) => {
release = resolve;
});
const send = vi.fn(async () => {
await held;
});
const { queue } = makeQueue({ maxItems: 1, send });
try {
queue.push('east', delivery('first'));
queue.push('east', delivery('tail'));
let finished = false;
const draining = queue.flushAll().then(() => {
finished = true;
});
await vi.advanceTimersByTimeAsync(4_999);
expect(finished).toBe(false);
await vi.advanceTimersByTimeAsync(1);
await draining;
queue.stop();
release();
await vi.advanceTimersByTimeAsync(100);
expect(send).toHaveBeenCalledTimes(1);
expect(vi.getTimerCount()).toBe(0);
} finally {
release();
vi.useRealTimers();
}
});
it('sends nothing more once it is stopped', async () => {
const { queue, sent } = makeQueue();
queue.stop();
+40 -10
View File
@@ -18,6 +18,8 @@
*/
import type { EventKey, KvOp } from '../../clients/event/types.js';
import { settleWithin } from '../../util/concurrency.js';
import { FINAL_FLUSH_TIMEOUT_MS } from '../../util/gracefulShutdown.js';
import type { DeliverableEvent } from './registry.js';
/**
@@ -86,7 +88,7 @@ export interface ForwardWatch {
/** One committed change, replayed against another region's session rows. */
export interface ForwardEvent {
kind: 'event';
family: 'fs' | 'kv';
family: 'fs' | 'kv' | 'notif';
ownerUserId: number;
/** Who caused it, so `self` is right on the far side. */
actingUserId?: number;
@@ -108,6 +110,17 @@ export interface ForwardEvent {
ancestors: Array<{ uid: string; path: string }>;
};
};
notif?: {
userUuid: string;
uid: string;
type: string;
audience: string;
appUid: string | null;
value: Record<string, unknown>;
dismissed?: true;
replaced?: true;
handlerDepth?: number;
};
kv?: {
userUuid: string;
appUid: string;
@@ -204,7 +217,7 @@ interface PeerQueue {
items: ForwardItem[];
bytes: number;
timer: ReturnType<typeof setTimeout> | null;
flushing: boolean;
flushing: Promise<void> | null;
}
export class PeerForwardQueue {
@@ -272,19 +285,27 @@ export class PeerForwardQueue {
}
/** Ship whatever is held for one peer. */
async flush(peerId: string): Promise<void> {
flush(peerId: string): Promise<void> {
const queue = this.#peers.get(peerId);
if (!queue || queue.flushing || queue.items.length === 0) return;
if (!queue) return Promise.resolve();
if (queue.flushing) return queue.flushing;
if (queue.items.length === 0) return Promise.resolve();
if (queue.timer) {
clearTimeout(queue.timer);
queue.timer = null;
}
queue.flushing = true;
queue.flushing = this.#send(peerId, queue);
return queue.flushing;
}
async #send(peerId: string, queue: PeerQueue): Promise<void> {
const batch = queue.items;
queue.items = [];
queue.bytes = 0;
try {
// Register the in-flight promise before a send can throw synchronously.
await Promise.resolve();
await this.#options.send(peerId, batch);
} catch (err) {
// A lost batch is a lost at-most-once delivery, and a `single`
@@ -292,7 +313,7 @@ export class PeerForwardQueue {
// hand the client an event whose moment has passed.
console.warn(`[events] forward to ${peerId} failed`, err);
} finally {
queue.flushing = false;
queue.flushing = null;
// Anything that arrived mid-flush gets the next window.
if (queue.items.length > 0 && !queue.timer && !this.#stopped) {
queue.timer = setTimeout(() => {
@@ -305,7 +326,16 @@ export class PeerForwardQueue {
}
async flushAll(): Promise<void> {
await Promise.all([...this.#peers.keys()].map((id) => this.flush(id)));
const deadline = Date.now() + FINAL_FLUSH_TIMEOUT_MS;
const work = Promise.allSettled(
[...this.#peers.entries()].map(async ([id, queue]) => {
await queue.flushing;
if (Date.now() < deadline) await this.flush(id);
}),
);
if (!(await settleWithin(work, FINAL_FLUSH_TIMEOUT_MS))) {
console.warn('[events] final flush timed out');
}
}
stop(): void {
@@ -323,7 +353,7 @@ export class PeerForwardQueue {
items: [],
bytes: 0,
timer: null,
flushing: false,
flushing: null,
};
this.#peers.set(peerId, queue);
return queue;
@@ -349,7 +379,7 @@ const isGapMarker = (item: ForwardItem): boolean =>
*/
const shed = (queue: PeerQueue, count: number): ForwardItem[] => {
const dropped: ForwardItem[] = [];
for (let i = 0; i < queue.items.length && dropped.length < count;) {
for (let i = 0; i < queue.items.length && dropped.length < count; ) {
if (isGapMarker(queue.items[i])) {
i++;
continue;
@@ -372,7 +402,7 @@ const shed = (queue: PeerQueue, count: number): ForwardItem[] => {
const shedBytes = (queue: PeerQueue, maxBytesHeld: number): ForwardItem[] => {
const dropped: ForwardItem[] = [];
let remaining = queue.bytes;
for (let i = 0; i < queue.items.length && remaining > maxBytesHeld;) {
for (let i = 0; i < queue.items.length && remaining > maxBytesHeld; ) {
if (isGapMarker(queue.items[i])) {
i++;
continue;
+66 -1
View File
@@ -402,7 +402,7 @@ const makeRegion = (
fields: { code: 'subject_does_not_exist' },
}),
},
notification: { notify: vi.fn() },
notification: { notify: vi.fn(), deliverOverSocket: vi.fn() },
metering: {
bufferIncrementUsages: (
actor: Actor,
@@ -2401,3 +2401,68 @@ describe('a durable row created in another region', () => {
);
});
});
describe('notification dismissals across connected pages', () => {
const config = { events: { enabled: true, notificationsFoldIn: true } } as Partial<IConfig>;
const appUid = 'notification-app';
const input = (uid = 'one') => ({
userId, userUuid: `user-${userId}`, uid,
type: 'app.message', audience: 'app-user', appUid,
value: {}, createdAt: Date.now(), dismissed: true as const,
});
it('reaches local and remote session pages without content or a forwarding loop', async () => {
const west = makeRegion('west', ['east'], config);
const east = makeRegion('east', ['west'], config);
for (const [region, socket] of [[west, 'local'], [east, 'remote']] as const) {
await region.events.subscribe(actorFor(appUid), socket, { subject: 'notif:app-user' });
}
await posted(east);
await quiet(100);
await west.events.dispatchNotif({ ...input(), value: { title: 'private' } });
await arrived(west);
await arrived(east);
for (const region of [west, east]) {
expect(region.sent).toHaveLength(1);
expect(region.sent[0].event).toMatchObject({ op: 'dismiss', uid: 'one', appUid });
expect(region.sent[0].event).not.toHaveProperty('notification');
}
expect(east.sent[0].event.id).toBe(west.sent[0].event.id);
const forwarded = west.posts.flatMap((p) => p.batch.items)
.find((item): item is ForwardEvent => item.kind === 'event');
expect(forwarded?.notif?.value).toEqual({});
expect(east.posts.flatMap((p) => p.batch.items).filter((i) => i.kind === 'event')).toEqual([]);
});
it('keeps separate dismissed ids in the same coalescing window', async () => {
const region = makeRegion('west', [], config);
await region.events.subscribe(actorFor(appUid), 'local', { subject: 'notif:app-user' });
await region.events.dispatchNotif(input('one'));
await region.events.dispatchNotif(input('two'));
await arrived(region, 2);
expect(region.sent.map((item) => 'uid' in item.event && item.event.uid)).toEqual(['one', 'two']);
});
it('does not deliver another app, account audience or another user to an app page', async () => {
const region = makeRegion('west', [], config);
await region.events.subscribe(actorFor(appUid), 'local', { subject: 'notif:app-user' });
await region.events.dispatchNotif({ ...input(), appUid: 'other-app' });
await region.events.dispatchNotif({ ...input(), audience: 'account' });
await region.events.dispatchNotif({ ...input(), userId: userId + 1, userUuid: 'other-user' });
await quiet(350);
expect(region.sent).toEqual([]);
});
it('also forwards postings through the same session path', async () => {
const west = makeRegion('west', ['east'], config);
const east = makeRegion('east', ['west'], config);
await east.events.subscribe(actorFor(appUid), 'remote', { subject: 'notif:app-user' });
await posted(east);
await quiet(100);
const { dismissed, ...postedInput } = input();
await west.events.dispatchNotif({ ...postedInput, value: { title: 'Hello' } });
await arrived(east);
expect(east.sent[0].event).toMatchObject({ op: 'post', uid: 'one', notification: { title: 'Hello' } });
});
});
+24 -14
View File
@@ -93,24 +93,25 @@ export interface ProjectedKvEvent extends ProjectedEventBase {
/**
* What a notification subscriber receives.
*
* `id` is the row's own uid rather than a per-dispatch uuid: a client that
* fetched a missed notification and then received the live copy has to be able
* to tell they are the same thing, and the mailbox uid is the only identity
* both halves share. `uid` carries it under the name the mailbox verbs
* (`mark-ack`, `mark-shown`) take.
* For postings, `id` is the row's own uid: a client that fetched a missed
* notification and then received the live copy has to be able to tell they are
* the same thing, and the mailbox uid is the only identity both halves share.
* `uid` carries it under the name the mailbox verbs (`mark-ack`, `mark-shown`)
* take.
*
* `notification` is the payload the caller sent, which is exactly what the
* socket adapter puts on `notif.message` — a subscriber reading this channel
* sees no less than the desktop does, and no internal row either.
* sees no less than the desktop does, and no internal row either. Dismissals
* use a fresh dispatch id and omit the payload.
*/
export interface ProjectedNotifEvent extends ProjectedEventBase {
op: 'post';
op: 'post' | 'dismiss';
uid: string;
type: string;
audience: NotificationAudience;
/** App the notification is about; `null` for platform notifications. */
appUid: string | null;
notification: Record<string, unknown>;
notification?: Record<string, unknown>;
/**
* Present when the notification replaced an earlier one under the same uid.
* `ts` is then the replacement time, the same on live and fetched copies,
@@ -120,7 +121,9 @@ export interface ProjectedNotifEvent extends ProjectedEventBase {
}
export type ProjectedEvent =
ProjectedFsEvent | ProjectedKvEvent | ProjectedNotifEvent;
| ProjectedFsEvent
| ProjectedKvEvent
| ProjectedNotifEvent;
export type GapReason =
| 'matched_subscription_limit'
@@ -200,6 +203,7 @@ export interface KvEventContext extends EventContextBase {
* recipient's uuid because the anchor token is the mailbox.
*/
export interface NotifEventContext extends EventContextBase {
dismissed?: true;
userId: number;
userUuid: string;
uid: string;
@@ -268,7 +272,9 @@ export interface NotifPublicSubject extends SubjectSpec<
}
export type PublicSubject =
FsPublicSubject | KvPublicSubject | NotifPublicSubject;
| FsPublicSubject
| KvPublicSubject
| NotifPublicSubject;
export interface UnpublishedInternalEvent {
event: EventKey;
@@ -346,14 +352,14 @@ const notifMatchTarget = (event: NotifEventContext): string =>
const notifMatchScope = (_anchorPath: string, slice: string): string => slice;
const notifProject = (delivery: NotifDeliveryContext): ProjectedNotifEvent => ({
id: delivery.uid,
id: delivery.dismissed ? delivery.id : delivery.uid,
subject: `notif:${delivery.appUid ?? delivery.userUuid}:${delivery.audience}`,
op: 'post',
op: delivery.dismissed ? 'dismiss' : 'post',
uid: delivery.uid,
type: delivery.type,
audience: delivery.audience,
appUid: delivery.appUid,
notification: delivery.notification,
...(delivery.dismissed ? {} : { notification: delivery.notification }),
...(delivery.replaced ? { replaced: true as const } : {}),
self: delivery.self,
ts: delivery.ts,
@@ -464,7 +470,7 @@ export const PUBLIC_SUBJECTS = [
{
family: 'notif',
subject: 'notif:*',
internal: ['notif.created'],
internal: ['notif.created', 'notif.acknowledged'],
tokens: notifTokens,
matchOn: notifMatchTarget,
matchSeparator: NOTIF_MATCH_SEPARATOR,
@@ -506,6 +512,10 @@ export const UNPUBLISHED_INTERNAL_EVENTS = [
event: 'notif.delivery.settled',
reason: 'A node-local report on how the live socket copy of a notification went; the notification itself publishes through `notif.created`.',
},
{
event: 'notif.handled',
reason: 'A node-local report that a page saw a notification live, for extensions deciding whether to push it; not a change to the notification.',
},
] as const satisfies readonly UnpublishedInternalEvent[];
// Every `fs.*`/`kv.*`/`notif.*` bus key needs a decision. A new key that is in
@@ -83,6 +83,7 @@ const recording = () => {
createFor: vi.fn(),
list: vi.fn(),
get: vi.fn(),
update: vi.fn(),
remove: vi.fn(),
receivePeerChanges: vi.fn(),
prepareSendAt: vi.fn(),
+1 -2
View File
@@ -48,8 +48,7 @@ export type SubjectFamily = 'fs' | 'kv' | 'notif';
export type FsOp = 'add' | 'write' | 'move' | 'remove' | 'meta';
/** A notification is only ever posted; the mailbox verbs are their own surface. */
export type NotifOp = 'post';
export type NotifOp = 'post' | 'dismiss';
export type SubjectOp = FsOp | KvOp | NotifOp;
@@ -144,11 +144,22 @@ const createInline = async (
return String(response.body.subId);
};
const delivered = (count = 1): Promise<void> =>
vi.waitFor(() => expect(sinkPosts.length).toBeGreaterThanOrEqual(count), {
timeout: 30_000,
interval: 50,
});
/**
* The post each label made, in order, once all have arrived. Matched by label
* since earlier tests' subscriptions stay live and post to the same sink.
*/
const delivered = async (...labels: string[]): Promise<SinkPost[]> => {
await vi.waitFor(
() =>
expect(sinkPosts.map((post) => post.label)).toEqual(
expect.arrayContaining(labels),
),
{ timeout: 30_000, interval: 50 },
);
return labels.map((label) =>
sinkPosts.find((post) => post.label === label)!,
);
};
beforeAll(async () => {
env = await setupPuterTestEnv({
@@ -255,14 +266,11 @@ describe('an inline handler in a user scope, end to end', () => {
await env.server.services.fs.touch(holder.id, {
path: `${holder.anchor}/note.txt`,
});
await delivered();
const [post] = await delivered('inline');
expect(sinkPosts[0]).toMatchObject({
label: 'inline',
path: `${holder.anchor}/note.txt`,
});
expect(post.path).toBe(`${holder.anchor}/note.txt`);
const { actor } = await env.server.services.auth.authenticate(
sinkPosts[0].token!,
post.token!,
);
expect(actor!.user?.id).toBe(holder.id);
expect(actor!.effectiveApp?.uid).toBe(appUid);
@@ -297,14 +305,14 @@ describe('an inline handler in a user scope, end to end', () => {
await env.server.services.fs.touch(holder.id, {
path: `${holder.anchor}/shared-${i}.txt`,
});
await delivered(2);
const runAs = new Set<number>();
for (const post of sinkPosts)
runAs.add(
(await env.server.services.auth.authenticate(post.token!))
.actor!.user!.id,
// The first test's 'inline' handler also posts for holders[0].
const posts = await delivered('first', 'second');
for (const [i, post] of posts.entries()) {
const { actor } = await env.server.services.auth.authenticate(
post.token!,
);
expect(runAs).toEqual(new Set(holders.map((h) => h.id)));
expect(actor!.user!.id).toBe(holders[i].id);
}
create.mockRestore();
},
);
@@ -218,6 +218,7 @@ export class EventsWorkerInvoker implements WorkerInvokerSeam {
{
lane: options.lane,
stampEvent: options.stampEvent,
beforeSend: options.beforeSend,
},
);
if (result.deployBusy) return { outcome: 'deploy-busy' };
@@ -49,6 +49,64 @@ const makeReq = (over: Partial<Request> = {}): Request =>
...over,
}) as unknown as Request;
describe('app browser embed shell', () => {
it.each([
[{ env: 'dev' }, '/sdk/puter.dev.js'],
[{ env: 'prod' }, 'https://js.puter.com/v2/'],
[{ gui_puterjs_bundle: '/custom-sdk.js' }, '/custom-sdk.js'],
])('uses the deployment SDK configuration %j', (config, sdkUrl) => {
const service = makeService({
...config,
api_base_url: 'https://api.deployment.test',
domain: 'deployment.test',
});
const html = service.renderAppBrowserEmbed();
const configJson = html.match(/id="app-browser-config">(.*?)<\/script>/)![1];
expect(JSON.parse(configJson)).toEqual({
apiOrigin: 'https://api.deployment.test', sdkUrl, guestApps: [],
});
expect(html).toContain('/dist/apps-embed.min.js');
expect(html).not.toContain('/dist/bundle.min.js');
expect(html).not.toContain('gui(');
});
it.each([
[{}, '/dist/apps-embed.min.js'],
[
{ gui_bundle: 'https://gui-cdn.deployment.test/bundle.min.js' },
'https://gui-cdn.deployment.test/apps-embed.min.js',
],
[
{ gui_bundle: '/static/bundle.min.js?v=42' },
'/static/apps-embed.min.js?v=42',
],
])('loads its script from beside the GUI bundle %j', (config, script) => {
const html = makeService(config).renderAppBrowserEmbed();
expect(html).toContain(`<script defer src="${script}"></script>`);
});
it('escapes configured values without breaking its JSON bootstrap', () => {
const sdkUrl = '/sdk.js?value=</script><script>unexpected()</script>';
const html = makeService({
gui_params: { title: '<script>unexpected()</script>' },
gui_puterjs_bundle: sdkUrl,
}).renderAppBrowserEmbed();
expect(html).not.toContain('<script>unexpected()');
const configJson = html.match(/id="app-browser-config">(.*?)<\/script>/)![1];
expect(JSON.parse(configJson).sdkUrl).toBe(sdkUrl);
});
it('carries the apps shown without a session, escaped', () => {
const guestApps = [
{ name: 'notes', title: '</script><script>unexpected()</script>' },
];
const html = makeService({}).renderAppBrowserEmbed({ guestApps });
expect(html).not.toContain('<script>unexpected()');
const configJson = html.match(/id="app-browser-config">(.*?)<\/script>/)![1];
expect(JSON.parse(configJson).guestApps).toEqual(guestApps);
});
});
/** Capture what the service sends, and return the rendered HTML. */
const render = async (
service: PuterHomepageService,
@@ -167,6 +167,56 @@ export class PuterHomepageService extends PuterService {
this.#guiParams[key] = val;
}
/** `guestApps` is what the embed shows a visitor without a session. */
renderAppBrowserEmbed({
guestApps = [],
}: { guestApps?: Array<Record<string, unknown>> } = {}): string {
const configJson = JSON.stringify({
apiOrigin: this.config.api_base_url,
sdkUrl:
this.config.env === 'dev'
? '/sdk/puter.dev.js'
: (this.config.gui_puterjs_bundle ??
'https://js.puter.com/v2/'),
guestApps,
}).replace(/</g, '\\u003c');
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="robots" content="noindex">
<title>${encode(String(this.config.gui_params?.title ?? 'Puter'))}</title>
<link rel="stylesheet" href="/css/normalize.css">
<link rel="stylesheet" href="/css/dashboard.css">
<link rel="stylesheet" href="/css/apps-embed.css">
</head>
<body>
<main id="app-browser" class="dashboard"></main>
<script type="application/json" id="app-browser-config">${configJson}</script>
<script defer src="${encode(this.#appBrowserEmbedScript())}"></script>
</body>
</html>`;
}
/**
* The embed's script is built beside the GUI bundle, so it is served from
* wherever `gui_bundle` is (a CDN in production), with the same query.
*/
#appBrowserEmbedScript(): string {
const base = 'http://relative.invalid';
const bundle = new URL(
this.config.gui_bundle ?? '/dist/bundle.min.js',
base,
);
const script = new URL('apps-embed.min.js', bundle);
script.search = bundle.search;
return script.origin === base
? `${script.pathname}${script.search}`
: script.href;
}
/** `scriptPath` is a same-origin absolute path, like `/ext/push/worker.js`. */
registerServiceWorkerScript(scriptPath: string): void {
if (
@@ -0,0 +1,103 @@
import {
afterAll,
afterEach,
beforeAll,
describe,
expect,
it,
vi,
} from 'vitest';
import type { PuterServer } from '../../server.js';
import { setupTestServer } from '../../testUtil.js';
const { disposals } = vi.hoisted(() => ({
disposals: [] as Array<ReturnType<typeof vi.fn>>,
}));
vi.mock('miniflare', () => ({
Miniflare: class {
dispose = vi.fn(async () => {});
constructor() {
disposals.push(this.dispose);
}
},
}));
describe('LocalWorkerService shutdown disposal', () => {
let server: PuterServer;
beforeAll(async () => {
server = await setupTestServer({
api_base_url: 'http://api.puter.localhost',
} as never);
});
afterAll(async () => {
await server.shutdown();
});
afterEach(() => {
vi.useRealTimers();
disposals.length = 0;
});
it('awaits every disposal even if one fails', async () => {
const service = server.services.localworkerservice;
await service.cfDeployLocal('first', 'auth', 'export default {}');
await service.cfDeployLocal('second', 'auth', 'export default {}');
let release!: () => void;
const held = new Promise<void>((resolve) => {
release = resolve;
});
disposals[0].mockRejectedValueOnce(new Error('dispose failed'));
disposals[1].mockImplementationOnce(async () => {
await held;
});
let finished = false;
const draining = Promise.resolve(service.onServerShutdown()).then(
() => {
finished = true;
},
);
try {
await new Promise((resolve) => setTimeout(resolve, 20));
expect(
disposals.every((dispose) => dispose.mock.calls.length === 1),
).toBe(true);
expect(finished).toBe(false);
} finally {
release();
await draining;
}
});
it('bounds disposal of a hung worker to five seconds', async () => {
const service = server.services.localworkerservice;
await service.cfDeployLocal('hung', 'auth', 'export default {}');
let release!: () => void;
const held = new Promise<void>((resolve) => {
release = resolve;
});
disposals[0].mockImplementationOnce(async () => {
await held;
});
vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
// The test server keeps arming timers from real callbacks while fakes
// are installed, so only what shutdown arms synchronously is checked.
const arm = vi.spyOn(globalThis, 'setTimeout');
try {
let finished = false;
const draining = Promise.resolve(service.onServerShutdown()).then(
() => {
finished = true;
},
);
expect(arm.mock.calls.map(([, ms]) => ms)).toEqual([5_000]);
await vi.advanceTimersByTimeAsync(4_999);
expect(finished).toBe(false);
await vi.advanceTimersByTimeAsync(1);
await draining;
} finally {
arm.mockRestore();
release();
vi.useRealTimers();
}
});
});
@@ -10,6 +10,8 @@ import {
import { puterStores } from '../../stores';
import type { SubdomainRow } from '../../stores/subdomain/SubdomainStore';
import { LayerInstances } from '../../types';
import { settleWithin } from '../../util/concurrency.js';
import { FINAL_FLUSH_TIMEOUT_MS } from '../../util/gracefulShutdown.js';
import { PuterService } from '../types';
const MAX_SOURCE_SIZE = 10 * 1024 * 1024; // 10 MB
@@ -240,13 +242,16 @@ export class LocalWorkerService extends PuterService {
idleSweepTimer.unref?.();
}
override onServerShutdown(): void {
override async onServerShutdown(): Promise<void> {
if (idleSweepTimer) {
clearInterval(idleSweepTimer);
idleSweepTimer = null;
}
for (const name of [...activeWorkers.keys()]) {
void this.#disposeWorker(name);
const work = Promise.allSettled(
[...activeWorkers.keys()].map((name) => this.#disposeWorker(name)),
);
if (!(await settleWithin(work, FINAL_FLUSH_TIMEOUT_MS))) {
console.warn('[localworker] final flush timed out');
}
}
async reconstructDeployArgs(workerName: string, row: SubdomainRow) {
@@ -4290,6 +4290,295 @@ describe('MeteringService', () => {
});
});
describe('reserveCreditsIfAffordable', () => {
const BUDGET = 20_000_000;
const HOLD = 16_200_000;
afterEach(() => {
vi.restoreAllMocks();
});
/** Spends the fresh actor's allowance down to exactly `balance`. */
const withBalance = async (balance: number) => {
const { remaining } = await target.getAllowedUsage(actor);
if (remaining > balance) {
await target.incrementUsage(
actor,
'kv:read',
1,
remaining - balance,
);
}
expect((await target.getAllowedUsage(actor)).remaining).toBe(
balance,
);
};
it('admits exactly one of two concurrent holds that only fit alone', async () => {
await withBalance(BUDGET);
const holds = await Promise.all([
target.reserveCreditsIfAffordable(actor, HOLD),
target.reserveCreditsIfAffordable(actor, HOLD),
]);
expect(holds.filter((h) => h !== null)).toHaveLength(1);
expect(await target.getOutstandingHolds(actor)).toBe(HOLD);
await Promise.all(holds.map((h) => h?.release()));
});
it('compares against the gross balance, counting live holds once', async () => {
await withBalance(BUDGET);
const other = await target.reserveCredits(actor, 5_000_000);
expect(
await target.reserveCreditsIfAffordable(actor, 15_000_001),
).toBeNull();
const fits = await target.reserveCreditsIfAffordable(
actor,
15_000_000,
);
expect(fits).not.toBeNull();
expect(await target.getOutstandingHolds(actor)).toBe(BUDGET);
// A release gives the headroom back for a retry.
await other.release();
expect(
await target.reserveCreditsIfAffordable(actor, 5_000_000),
).not.toBeNull();
});
it('counts allowance and purchased credit together', async () => {
await withBalance(1_000_000);
await target.updateAddonCredit(actor.user.uuid!, 2_000_000);
expect(
await target.reserveCreditsIfAffordable(actor, 3_000_001),
).toBeNull();
expect(
await target.reserveCreditsIfAffordable(actor, 3_000_000),
).not.toBeNull();
});
it('reads the balance again when usage lands and a hold is released after the read', async () => {
await withBalance(BUDGET);
// A request still running, about to record its usage and release.
const running = await target.reserveCredits(actor, HOLD);
const settle = async () => {
await target.incrementUsage(actor, 'kv:read', 1, HOLD);
await running.release();
};
// The admission's balance read finishes first, then the
// settlement lands before its reservation runs.
const read = target.getAllowedUsage.bind(target);
let settled = false;
vi.spyOn(target, 'getAllowedUsage').mockImplementation(
async (...args) => {
const balance = await read(...args);
if (!settled) {
settled = true;
await settle();
}
return balance;
},
);
expect(await target.reserveCreditsIfAffordable(actor, HOLD)).toBe(
null,
);
expect(await target.getOutstandingHolds(actor)).toBe(0);
});
it('would over-admit that interleaving without the version check', async () => {
await withBalance(BUDGET);
const running = await target.reserveCredits(actor, HOLD);
const read = target.getAllowedUsage.bind(target);
let settled = false;
vi.spyOn(target, 'getAllowedUsage').mockImplementation(
async (...args) => {
const balance = await read(...args);
if (!settled) {
settled = true;
await target.incrementUsage(actor, 'kv:read', 1, HOLD);
await running.release();
}
return balance;
},
);
vi.spyOn(server.stores.creditHold, 'version').mockResolvedValue('');
const hold = await target.reserveCreditsIfAffordable(actor, HOLD);
expect(hold).not.toBeNull();
await hold?.release();
});
it('marks the balance changed once a usage write lands', async () => {
const before = await server.stores.creditHold.version(
actor.user.uuid!,
);
await target.incrementUsage(actor, 'kv:read', 1, 1_000);
expect(
await server.stores.creditHold.version(actor.user.uuid!),
).not.toBe(before);
});
it('lets the balance decide, holding nothing, when the cache is unavailable', async () => {
await withBalance(BUDGET);
vi.spyOn(server.stores.creditHold, 'reserve').mockResolvedValue({
outcome: 'unavailable',
});
const hold = await target.reserveCreditsIfAffordable(actor, HOLD);
expect(hold).not.toBeNull();
expect(await target.getOutstandingHolds(actor)).toBe(0);
expect(
await target.reserveCreditsIfAffordable(actor, BUDGET + 1),
).toBeNull();
});
it('reads the balance again while it keeps changing, then refuses', async () => {
await withBalance(BUDGET);
const reserve = vi
.spyOn(server.stores.creditHold, 'reserve')
.mockResolvedValue({ outcome: 'stale' });
expect(await target.reserveCreditsIfAffordable(actor, HOLD)).toBe(
null,
);
expect(reserve).toHaveBeenCalledTimes(5);
// Every attempt checks the version; none falls back to skipping it.
expect(reserve.mock.calls.every(([, , , v]) => v !== '')).toBe(
true,
);
expect(await target.getOutstandingHolds(actor)).toBe(0);
});
it('admits once the balance holds still', async () => {
await withBalance(BUDGET);
const reserve = vi.spyOn(server.stores.creditHold, 'reserve');
reserve
.mockResolvedValueOnce({ outcome: 'stale' })
.mockResolvedValueOnce({ outcome: 'stale' });
const hold = await target.reserveCreditsIfAffordable(actor, HOLD);
expect(hold).not.toBeNull();
expect(reserve).toHaveBeenCalledTimes(3);
await hold?.release();
});
it('reads purchased credit consistently only while some is left', async () => {
await withBalance(BUDGET);
const get = vi.spyOn(server.stores.kv, 'get');
const consistentAddonReads = () =>
get.mock.calls.filter(
([args]) =>
args.consistentRead &&
String(args.key).endsWith(':addons'),
).length;
// With none, the cached record that says so answers.
await target.getAllowedUsage(actor);
get.mockClear();
await (
await target.reserveCreditsIfAffordable(actor, 1000)
)?.release();
expect(consistentAddonReads()).toBe(0);
await target.updateAddonCredit(actor.user.uuid!, 5_000);
get.mockClear();
await (
await target.reserveCreditsIfAffordable(actor, 1000)
)?.release();
expect(consistentAddonReads()).toBe(1);
});
it('marks the balance changed on credit, allowance and manual usage changes', async () => {
const uuid = actor.user.uuid!;
const versions = [await server.stores.creditHold.version(uuid)];
await target.updateAddonCredit(uuid, 1_000);
versions.push(await server.stores.creditHold.version(uuid));
await target.setActorCurrentMonthUsageTotal(actor, 5_000);
versions.push(await server.stores.creditHold.version(uuid));
expect(new Set(versions).size).toBe(3);
});
it('treats the system actor, a userless actor and amounts it can’t hold as the plain check does', async () => {
for (const [who, amount] of [
[SYSTEM_ACTOR, HOLD],
[actor, 0],
[actor, -5],
] as const) {
const hold = await target.reserveCreditsIfAffordable(
who,
amount,
);
expect(hold).not.toBeNull();
}
for (const amount of [Number.NaN, Number.POSITIVE_INFINITY]) {
expect(
await target.reserveCreditsIfAffordable(actor, amount),
).toBeNull();
}
await expect(
target.reserveCreditsIfAffordable({ user: {} } as Actor, HOLD),
).rejects.toThrow();
expect(await target.getOutstandingHolds(actor)).toBe(0);
});
it('passes the hold lifetime through and renews it', async () => {
await withBalance(BUDGET);
const hold = await target.reserveCreditsIfAffordable(actor, 750, {
ttlMs: 500,
});
await new Promise((r) => setTimeout(r, 620));
expect(await target.getOutstandingHolds(actor)).toBe(0);
await hold?.extend?.();
expect(await target.getOutstandingHolds(actor)).toBe(750);
await hold?.release();
expect(await target.getOutstandingHolds(actor)).toBe(0);
});
});
describe('settling a charge while a flush is out', () => {
it('splits a charge over the allowance using usage a flush has claimed', async () => {
const uuid = actor.user.uuid!;
const { monthUsageAllowance: allowance } =
await target.getActorSubscription(actor);
await target.updateAddonCredit(uuid, 1_000_000);
const Service = target.constructor as typeof MeteringService;
const threshold = Service.PRECISION_THRESHOLD;
// Keeps the first charge buffered instead of read exactly.
Service.PRECISION_THRESHOLD = 2;
await target.incrementUsage(actor, 'kv:read', 1, allowance - 5);
Service.PRECISION_THRESHOLD = threshold;
// A flush claims it and stalls before writing it onward.
const kv = server.stores.kv;
const realIncr = kv.incr.bind(kv);
let release: () => void = () => {};
const gate = new Promise<void>((r) => (release = r));
const write = vi
.spyOn(kv, 'incr')
.mockImplementation(async (...args) => {
await gate;
return realIncr(...args);
});
const flushing = server.stores.meteringBuffer.flushCycle();
await vi.waitFor(() => expect(write).toHaveBeenCalled());
write.mockImplementation(realIncr);
// 5 µ¢ of this is left in the allowance; the rest is credit.
await target.incrementUsage(actor, 'kv:read', 1, 10);
release();
await flushing;
write.mockRestore();
await waitFor(async () => {
const addons = await target.getActorAddons(actor, {
consistentRead: true,
});
expect(addons.consumedPurchaseCredits).toBe(5);
});
});
});
// ── Resolver registration ────────────────────────────────────────
describe('resolver registration', () => {
@@ -698,6 +698,8 @@ export class MeteringService extends PuterService {
actorAddons,
totalBatchCost,
);
// Before any hold this usage stands for is released.
await this.stores.creditHold.bump(userId);
this.maybeAlertOveruse({
actor,
@@ -729,6 +731,10 @@ export class MeteringService extends PuterService {
? ({ ...own, ...charged } as UsageByType)
: (charged ?? own);
} catch (e) {
// Part of the usage may have landed before the failure.
if (actor?.user?.uuid) {
await this.stores.creditHold.bump(actor.user.uuid);
}
console.error('[metering] batchIncrementUsages failed', {
actor,
usages,
@@ -1432,6 +1438,7 @@ export class MeteringService extends PuterService {
}
this.invalidateActorCredits(userId);
await this.stores.creditHold.bump(userId);
await Promise.all([
this.stores.meteringBuffer.forgetSummed(actorUsageKey),
this.stores.meteringBuffer.forgetSummed(
@@ -1545,7 +1552,86 @@ export class MeteringService extends PuterService {
opts.ttlMs,
);
if (!member) return NO_CREDIT_HOLD;
return this.#holdHandle(userId, member, opts.ttlMs);
}
/**
* Commit part of an actor's budget only if it fits beside what their other
* operations hold; null when it doesn't. The check and the hold are one
* step in the cache, and a usage write or release that lands after the
* balance read sends the reservation back to read it again. When the cache
* can't answer, the balance alone decides and nothing is held.
*/
async reserveCreditsIfAffordable(
actor: Actor,
amount: number,
opts: { ttlMs?: number } = {},
): Promise<CreditHold | null> {
const userId = actor?.user?.uuid;
if (
!userId ||
isSystemActor(actor) ||
!Number.isFinite(amount) ||
!(amount > 0)
) {
// Nothing to hold; the plain check decides, as it always has.
return (await this.hasEnoughCredits(actor, amount))
? NO_CREDIT_HOLD
: null;
}
for (let attempt = 1; ; attempt++) {
const version = await this.stores.creditHold.version(userId);
// Read after the version, so a write it misses bumps the version.
const { remaining } = await this.getAllowedUsage(actor, {
exact: true,
});
const budget = remaining || 0;
const result =
version === null
? ({ outcome: 'unavailable' } as const)
: await this.stores.creditHold.reserve(
userId,
amount,
budget,
version,
opts.ttlMs,
);
switch (result.outcome) {
case 'accepted':
return this.#holdHandle(userId, result.member, opts.ttlMs);
case 'insufficient':
return null;
case 'unavailable':
return budget >= amount ? NO_CREDIT_HOLD : null;
case 'stale':
break;
}
// A balance that never holds still long enough to read is refused
// rather than checked against a read that may be out of date.
if (attempt >= MeteringService.RESERVE_ATTEMPTS) {
console.warn(
`[metering] credit reservation refused for ${userId}: balance kept changing`,
);
return null;
}
await new Promise((resolve) =>
setTimeout(
resolve,
Math.random() *
MeteringService.RESERVE_BACKOFF_MS *
attempt,
),
);
}
}
/** Reservations tried while the balance keeps changing under them. */
private static readonly RESERVE_ATTEMPTS = 5;
/** Upper bound of the randomized wait before each retry, times the attempt. */
private static readonly RESERVE_BACKOFF_MS = 20;
#holdHandle(userId: string, member: string, ttlMs?: number): CreditHold {
let released = false;
return {
release: async () => {
@@ -1555,11 +1641,7 @@ export class MeteringService extends PuterService {
},
extend: async () => {
if (released) return;
await this.stores.creditHold.refresh(
userId,
member,
opts.ttlMs,
);
await this.stores.creditHold.refresh(userId, member, ttlMs);
},
};
}
@@ -1571,14 +1653,22 @@ export class MeteringService extends PuterService {
return this.stores.creditHold.outstanding(userId);
}
async getAllowedUsage(actor: Actor): Promise<{
/**
* `exact` reads purchased credit consistently, for a spend decision that
* has to see the consumption a write just recorded. A cached record with no
* credit left still answers: it can only understate the balance.
*/
async getAllowedUsage(
actor: Actor,
{ exact = false }: { exact?: boolean } = {},
): Promise<{
remaining: number;
monthUsageAllowance: number;
addons: UsageAddons;
}> {
const [userSubscription, addons, totals] = await Promise.all([
this.getActorSubscription(actor),
this.#addonsForSettle(actor),
this.#addonsForSettle(actor, { consistentRead: exact }),
this.currentMonthTotals(actor),
]);
@@ -2245,13 +2335,16 @@ export class MeteringService extends PuterService {
}
/** `getActorAddons`, through `#spentAddons`. Shared by gates and settles. */
async #addonsForSettle(actor: Actor): Promise<UsageAddons> {
async #addonsForSettle(
actor: Actor,
{ consistentRead }: { consistentRead?: boolean } = {},
): Promise<UsageAddons> {
const uuid = actor.user?.uuid;
const cached = uuid ? this.#spentAddons.get(uuid) : undefined;
if (cached && cached.expiresAt > Date.now()) return cached.addons;
const drops = this.#spentAddonsDrops;
const addons = await this.getActorAddons(actor);
const addons = await this.getActorAddons(actor, { consistentRead });
if (!uuid) return addons;
this.#spentAddons.delete(uuid);
const creditLeft =
@@ -2382,6 +2475,7 @@ export class MeteringService extends PuterService {
// Credit that lands while the account is being turned away has to take
// effect on the next request, not at the end of the cache window.
this.invalidateActorCredits(userId);
await this.stores.creditHold.bump(userId);
}
/**
@@ -2429,6 +2523,7 @@ export class MeteringService extends PuterService {
});
// Every node drops its cached policy, and the budget answer with it.
this.invalidateActorSubscription(userUuid);
await this.stores.creditHold.bump(userUuid);
}
/**
@@ -0,0 +1,39 @@
import { expect, it } from 'vitest';
import { EventClient } from '../../clients/event/EventClient.js';
import type { EventMap } from '../../clients/event/types.js';
import { NotificationService } from './NotificationService.js';
it.each([undefined, 0, 1_700_000_000_000])(
'emits replacement metadata for replacedAt %s',
async (replacedAt) => {
const event = new EventClient({} as never);
const written: EventMap['notif.written'][] = [];
event.on('notif.written', (_key, value) => {
written.push(value);
});
const service = new NotificationService(
{} as never,
{ event } as never,
{} as never,
);
await service.emitWritten(
{
userId: 1,
uid: 'notification-1',
type: 'app.message',
audience: 'app-user',
appUid: 'app-1',
value: { title: 'A message' },
updated: replacedAt !== undefined,
replacedAt,
},
{ sendId: 'send-1' },
);
expect(written).toHaveLength(1);
if (replacedAt === undefined)
expect(written[0]).not.toHaveProperty('replaced');
else expect(written[0].replaced).toBe(true);
},
);
@@ -354,6 +354,7 @@ describe('NotificationService.notifyUpdate', () => {
appUid,
value: { title: 'two messages', type: 'app.message' },
updated: true,
replaced: true,
});
expect(desktop.seen).toEqual([]);
expect(
@@ -23,10 +23,14 @@ import { handlerDepthOf, type Actor } from '../../core/actor.js';
import { Context } from '../../core/context.js';
import type { NotifEventContext } from '../events/registry.js';
import { PuterService } from '../types.js';
import { checkRateLimit } from '../../core/http/middleware/rateLimit.js';
import {
NOTIF_HANDLED_EVENT,
NOTIF_HANDLED_PER_MINUTE,
NotificationSocketAdapter,
notificationAckTimeoutMs,
notificationsFoldInEnabled,
type NotifSocket,
type ReceiptScope,
} from './notificationSocket.js';
import {
@@ -310,6 +314,9 @@ export class NotificationService extends PuterService {
...scope,
value: structuredClone(payload),
...(updated ? { updated: true as const } : {}),
...(written.replacedAt !== undefined
? { replaced: true as const }
: {}),
sendId: opts.sendId,
...(opts.replayable ? { replayable: true as const } : {}),
},
@@ -401,6 +408,11 @@ export class NotificationService extends PuterService {
return this.#socket().receipt(scope);
}
/** Installs the `notif.handled` verb on one connection. */
attachSocket(socket: NotifSocket, actor: Actor): void {
this.#socket().attachSocket(socket, actor);
}
/**
* Drop notifications past the retention window, in batches, and report how
* many went. Deleting is all there is to it: nothing is pushed, because a
@@ -436,6 +448,12 @@ export class NotificationService extends PuterService {
ackTimeoutMs: () => notificationAckTimeoutMs(this.config),
heldInPeerRegion: (userId, appUid) =>
this.#heldInPeerRegion(userId, appUid),
allowHandled: (userId) =>
checkRateLimit(
`socket:${NOTIF_HANDLED_EVENT}:${userId}`,
NOTIF_HANDLED_PER_MINUTE,
60_000,
),
});
return this.#adapter;
}
@@ -172,13 +172,23 @@ describe('parseAppMessage', () => {
rejects({ title: 't', sensitive: 'yes' }, 'sensitive');
});
it('refuses high priority to apps', () => {
expect(() => parseAppMessage({ title: 't', priority: 'high' })).toThrow(
"'high' is not available to apps",
);
rejects({ title: 't', priority: 'urgent' }, 'priority');
it('accepts normal and high priority, defaulting to normal', () => {
expect(parseAppMessage({ title: 't' }).priority).toBe('normal');
expect(
parseAppMessage({ title: 't', priority: 'normal' }).priority,
).toBe('normal');
expect(
parseAppMessage({ title: 't', priority: 'high' }).priority,
).toBe('high');
});
it.each(['urgent', 'low', '', null, 1, true])(
'rejects priority %s',
(priority) => {
rejects({ title: 't', priority }, 'priority');
},
);
it('bounds ttl to whole seconds within a day', () => {
expect(parseAppMessage({ title: 't', ttl: 1 }).ttl).toBe(1);
rejects({ title: 't', ttl: 0 }, 'ttl');
@@ -39,7 +39,7 @@ export const APP_MESSAGE_RATE_LIMITS = {
perDay: 1000,
} as const;
export type AppMessagePriority = 'normal';
export type AppMessagePriority = 'normal' | 'high';
/** An `app.message` value as stored, after normalization. */
export type AppMessage = {
@@ -322,13 +322,11 @@ export const parseAppMessage = (input: Record<string, unknown>): AppMessage => {
message.sensitive = input.sensitive;
}
if (input.priority !== undefined && input.priority !== 'normal') {
throw invalid(
'priority',
input.priority === 'high'
? "'high' is not available to apps; use 'normal'"
: "must be 'normal'",
);
if (input.priority !== undefined) {
if (input.priority !== 'normal' && input.priority !== 'high') {
throw invalid('priority', "must be 'normal' or 'high'");
}
message.priority = input.priority;
}
return message;
@@ -32,7 +32,10 @@ import {
import type { EventMap } from '../../clients/event/types.js';
import type { PuterServer } from '../../server.js';
import { accountSocketRoom, SocketService } from '../socket/SocketService.js';
import { NOTIF_RECEIPT_EVENT } from './notificationSocket.js';
import {
NOTIF_HANDLED_EVENT,
NOTIF_RECEIPT_EVENT,
} from './notificationSocket.js';
import {
allocateEphemeralPort,
createTestUser,
@@ -309,12 +312,13 @@ describe('live notification delivery receipts', () => {
complete: true,
peerRegions: false,
responders: [{ visibilityState: 'hidden' }],
handled: false,
};
expect(settled).toEqual(expected);
expect(state.settled).toEqual([expected]);
});
it('passes a tab`s focus and own display through, typed strictly', async () => {
it('passes a tab`s focus through, typed strictly', async () => {
const answers = [
{
received: true,
@@ -326,7 +330,6 @@ describe('live notification delivery receipts', () => {
received: true,
visibilityState: 'nope',
focused: 1,
showsOwn: 'yes',
},
];
for (const answer of answers) {
@@ -341,11 +344,39 @@ describe('live notification delivery receipts', () => {
expect(settled.outcome).toBe('received');
expect(settled.responders).toEqual(
expect.arrayContaining([
{ visibilityState: 'hidden', focused: false, showsOwn: true },
{ visibilityState: 'hidden', focused: false },
{ visibilityState: 'unknown' },
]),
);
expect(settled.responders).toHaveLength(2);
expect(settled.handled).toBe(false);
});
it('counts as handled only a visible tab that is focused, or an older one that is visible', async () => {
const cases: Array<[Record<string, unknown>, boolean]> = [
[{ visibilityState: 'visible', focused: true }, true],
[{ visibilityState: 'visible', focused: false }, false],
[{ visibilityState: 'hidden', focused: true }, false],
[{ visibilityState: 'visible' }, true],
];
for (const [answer, handled] of cases) {
const tab = await connect({ acks: [NOTIF_RECEIPT_EVENT] });
tab.on(NOTIF_RECEIPT_EVENT, (_data: unknown, ack: Ack) =>
ack({ received: true, ...answer }),
);
const { settled } = await ask();
expect(settled.handled, JSON.stringify(answer)).toBe(handled);
tab.disconnect();
await vi.waitFor(() =>
expect(
state.server.services.socket.has({
room: accountSocketRoom(state.userId),
}),
).toBe(false),
);
}
});
it('settles as unreceived when every tab answers that it never got it', async () => {
@@ -549,6 +580,7 @@ describe('live receipts from an app’s own sockets', () => {
complete: true,
peerRegions: false,
responders: [{ visibilityState: 'visible' }],
handled: true,
};
expect(settled).toEqual(expected);
expect(state.settled).toEqual([expected]);
@@ -719,3 +751,59 @@ describe('app receipts where the adapter cannot count servers', () => {
});
});
});
describe('a page reporting a notification it handled', () => {
const { state, connect, appSocket } = withAppReceipts({ cluster: true });
const reports: Array<EventMap['notif.handled']> = [];
const onHandled = (_key: string, data: unknown) => {
reports.push(data as EventMap['notif.handled']);
};
beforeAll(() => {
state.server.clients.event.on('notif.handled', onHandled);
});
afterEach(() => {
reports.length = 0;
});
afterAll(() => {
state.server?.clients.event.off('notif.handled', onHandled);
});
it('relays a desktop tab`s report for its user, without an app', async () => {
const tab = await connect({});
const uid = uuidv4();
tab.emit(NOTIF_HANDLED_EVENT, { uid });
await vi.waitFor(() =>
expect(reports).toEqual([{ userId: state.userId, uid }]),
);
});
it('relays an app page`s report with the app it acts as', async () => {
const page = await appSocket();
const uid = uuidv4();
page.emit(NOTIF_HANDLED_EVENT, { uid });
await vi.waitFor(() =>
expect(reports).toEqual([
{ userId: state.userId, uid, appUid: OWN_APP },
]),
);
});
it('drops a report without a notification uid', async () => {
const tab = await connect({});
tab.emit(NOTIF_HANDLED_EVENT, { uid: 'not-a-uid' });
tab.emit(NOTIF_HANDLED_EVENT, null);
tab.emit(NOTIF_HANDLED_EVENT, { uid: uuidv4() });
await vi.waitFor(() => expect(reports).toHaveLength(1));
await new Promise((resolve) => setTimeout(resolve, 100));
expect(reports).toHaveLength(1);
});
});
@@ -17,7 +17,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { validate as validateUuid } from 'uuid';
import type { EventClient } from '../../clients/event/EventClient.js';
import type { Actor } from '../../core/actor.js';
import type {
EventMap,
NotifDeliveryOutcome,
@@ -53,7 +55,10 @@ import type { IConfig } from '../../types.js';
*/
export type NotifWire =
'notif.message' | 'notif.unreads' | 'notif.ack' | 'notif.removed';
| 'notif.message'
| 'notif.unreads'
| 'notif.ack'
| 'notif.removed';
export const NOTIF_DELIVERY_EVENT = 'outer.notif.delivery';
@@ -67,6 +72,15 @@ export const DEFAULT_NOTIFICATION_ACK_TIMEOUT_MS = 5_000;
*/
export const NOTIF_RECEIPT_EVENT = 'notif.receipt';
/**
* Sent by a page with `{ uid }` when it got that notification live while
* visible and focused, so a region that didn't ask the page can still hear it.
*/
export const NOTIF_HANDLED_EVENT = 'notif.handled';
/** `notif.handled` reports one user's sockets may send per minute. */
export const NOTIF_HANDLED_PER_MINUTE = 60;
/** One unread as the replay payload carries it. */
export interface UnreadNotification {
uid: string;
@@ -122,12 +136,16 @@ const responderOf = (response: unknown): NotifDeliveryResponder | null => {
...(typeof answer.focused === 'boolean'
? { focused: answer.focused }
: {}),
...(typeof answer.showsOwn === 'boolean'
? { showsOwn: answer.showsOwn }
: {}),
};
};
/**
* Visible and focused. An older client doesn't say whether it's focused, so it
* counts while visible.
*/
export const handledBy = (responder: NotifDeliveryResponder): boolean =>
responder.visibilityState === 'visible' && responder.focused !== false;
interface AdapterDeps {
event: EventClient;
socket: SocketService;
@@ -138,6 +156,13 @@ interface AdapterDeps {
userId: number,
appUid: string | null,
) => Promise<boolean>;
/** Spends one of the user's `notif.handled` reports; false past the limit. */
allowHandled: (userId: number) => Promise<boolean>;
}
/** The part of a socket `attachSocket` installs its verb on. */
export interface NotifSocket {
on(event: string, listener: (...args: never[]) => void): unknown;
}
type SettledDelivery = EventMap['notif.delivery.settled'];
@@ -253,6 +278,7 @@ export class NotificationSocketAdapter {
complete: asked.complete,
peerRegions,
responders,
handled: responders.some(handledBy),
};
this.#deps.event.emit(
'notif.delivery.settled',
@@ -266,6 +292,28 @@ export class NotificationSocketAdapter {
}
}
/**
* Relays a page's `notif.handled` as the node-local `notif.handled` event,
* scoped to the socket's user and app.
*/
attachSocket(socket: NotifSocket, actor: Actor): void {
const userId = actor.user?.id;
if (userId === undefined) return;
const appUid = actor.effectiveApp?.uid;
socket.on(NOTIF_HANDLED_EVENT, ((payload: unknown) => {
const uid = (payload as { uid?: unknown } | null)?.uid;
if (typeof uid !== 'string' || !validateUuid(uid)) return;
void this.#deps.allowHandled(userId).then((ok) => {
if (!ok) return;
this.#deps.event.emit(
'notif.handled',
{ userId, uid, ...(appUid ? { appUid } : {}) },
{},
);
});
}) as (...args: never[]) => void);
}
/** Whether this region holds a socket for the recipient right now. */
hasSocket(userId: number): boolean {
return this.#deps.socket.has({ room: userId });
@@ -17,6 +17,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { HttpError } from '../../core/http/HttpError.js';
/** Who the recipient is in relation to the notification. */
export type NotificationAudience = 'account' | 'developer' | 'app-user';
@@ -153,6 +155,22 @@ export type NotificationTypeName = (typeof NOTIFICATION_TYPES)[number]['type'];
/** Serialized `value` may not exceed this many bytes. */
export const NOTIFICATION_VALUE_MAX_BYTES = 16 * 1024;
export const assertNotificationValueFits = (
value: Record<string, unknown>,
): void => {
if (
Buffer.byteLength(JSON.stringify(value), 'utf8') >
NOTIFICATION_VALUE_MAX_BYTES
) {
throw new HttpError(
400,
`\`value\` may not exceed ${NOTIFICATION_VALUE_MAX_BYTES} bytes`,
{
legacyCode: 'notification_value_too_large',
},
);
}
};
const BY_NAME = new Map<string, NotificationType>(
NOTIFICATION_TYPES.map((entry) => [entry.type, entry]),
);
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+53 -3
View File
@@ -11,7 +11,10 @@ Direct clients are documented in the [Realtime WebSocket API reference](../../..
The relay forwards native OpenAI JSON events unchanged after validating the
event envelope, except that `rate_limits.updated` (Puter's provider account) is
dropped and provider organization IDs are removed from error messages. An event must be an object with a nonempty string `type` of at
dropped and provider organization IDs are removed from diagnostic messages in
`error`, `response.done.status_details.error`, and
`conversation.item.input_audio_transcription.failed.error`, and from provider
close reasons. Conversation content is preserved. An event must be an object with a nonempty string `type` of at
most 128 characters. Binary messages and malformed envelopes close the session.
Usage fields consumed by metering are validated before use. Event field order
does not affect billing.
@@ -46,7 +49,8 @@ traffic is not rate limited: OpenAI sends audio faster than real time, and
backpressure bounds what a slow client can queue. Usage is priced as each event
arrives, before any forwarding decision, and recorded serially off the
forwarding path through the AI cost-factor hook. Pricing follows the model the
session reports, falling back to the connection's; a client `session.update`
session reports. A missing model uses the last known model, while an explicitly
unpriced model closes the connection; a client `session.update`
that changes the model is refused. So are `conversation.item.retrieve` and MCP
tools in `session.update` or `response.create`, since a retrieved item or MCP
output can exceed the upstream message cap.
@@ -65,9 +69,55 @@ Each response holds a fixed amount of the account's budget
until its usage is recorded, so concurrent sessions can't overdraw it; a session
opens only if one hold fits. Input transcription is priced at the costlier of
the transcription models in force at commit and at completion. Session attempts
are limited per credential before authentication runs.
are limited per credential after signature verification. Local, bounded IP and
node admission limits protect signature verification and authentication lookups.
Upgrades use Express's configured proxy trust policy and the `ip.validate` gate.
Authentication uses `touch: false` on both initial connection and rechecks, so
opening or maintaining a realtime socket does not update session activity metadata.
Input transcription holds credit per item, at a fixed budget for the selected
transcription model and that model's cost factor. Audio and the configuration
that enables transcription are forwarded only while a reserved spare item
exists. Each provider commit takes one, and the pool refills to two spares in
the background, up to 64 pending plus spare items. A commit with no spare left
reserves its own item. A model change, requested or reported, releases the spares
and reserves new ones at the new model's price. Committed items keep their hold
and model. Disabling transcription keeps the spares until the connection ends.
If the account can't cover another item, client reads pause while the session
still holds items that may free credit, and the session is refused with
`insufficient_funds` once it holds none.
Audio the provider may commit without the relay seeing the acknowledgement is
tracked apart from the pool, as a duration at the session's input format. Without
turn detection that is all audio since the last acknowledged commit or clear.
With server turn detection it is only speech from `speech_started`, since the
provider drops silence, plus the buffer after an explicit commit. Each started
160 seconds of it claims one held item, and an acknowledged commit, a clear, or
an `input_audio_buffer_commit_empty` error returns those items to the spares.
The session is never closed for its length. If the upstream closes first, each
claimed item is billed. Each committed item retains its model
until completion, failure, or bounded settlement. Missing usage is billed the
item's fixed budget. Overflow closes the connection without evicting existing
pending identities.
The post-record continuation check asks whether settled credit remains. It
excludes outstanding holds and accepts fractional positive credit. Admission of
new billable work continues to use the atomic hold path, which includes every
outstanding reservation.
When the host listens for `ai.prompt.validate`, each response that starts runs
it on the text the client sent since the previous check: session and response
instructions, message text and transcripts, and function call output, as chat
messages. The response's cost follows as `ai.prompt.cost-calculated` under the
same completion id. A refusal closes the session with `permission_denied`; the
response may already be streaming by then. Pending text past 64 KiB is checked
on its own. Audio is not checked.
Credential checks run in batches of at most 16 with no overlapping sweeps.
Session and access-token revocation events close affected sockets and invalidate
their auth cache entries. Subscription eligibility is checked independently of
authentication and after subscription invalidations. A failed verification check after a 60-second
retry grace from the first failed lookup closes with a retryable internal error. Periodic checks remain the missed-event fallback.
When the drain starts, open sessions get a `puter.reconnect` event and new
sessions receive `server_restarting` and close with 1012. Open sessions can
finish an in-flight response for up to five seconds at shutdown, then close
@@ -0,0 +1,175 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { Actor } from '../../core/actor.js';
import type { PuterServer } from '../../server.js';
import { setupTestServer } from '../../testUtil.js';
import type { ResolvedShare } from './ShareService.js';
describe('ShareNotificationService shutdown drain', () => {
let server: PuterServer;
beforeEach(async () => {
server = await setupTestServer({
email: { from: '[email protected]', host: '127.0.0.1', port: 1 },
share_notify_limits: { emailBatchSeconds: 0.01 },
} as never);
});
afterEach(async () => {
vi.useRealTimers();
await server.services.shareNotification.onServerPrepareShutdown();
await server.services.shareNotification.onServerShutdown();
await server.shutdown();
vi.restoreAllMocks();
});
const queueInvite = async (email: string, issuerId = 7) => {
await server.services.shareNotification.notifyShared(
{
user: { id: issuerId, username: 'sender' },
effectiveApp: null,
} as Actor,
[
{
pending: true,
isNew: true,
recipientEmail: email,
name: 'shared.txt',
} as ResolvedShare,
],
);
};
it.each([false, true])(
'awaits an active timer send and its failure requeue %s',
async (failSend) => {
const email = `active-${failSend}@puter.local`;
let release!: () => void;
const held = new Promise<void>((resolve) => {
release = resolve;
});
const send = vi
.spyOn(server.clients.email, 'sendRaw')
.mockImplementation(async () => {
await held;
if (failSend) throw new Error('transport unavailable');
return null;
});
await queueInvite(email);
await vi.waitFor(() => expect(send).toHaveBeenCalledTimes(1));
const list = async () =>
server.stores.kv.list({
as: 'keys',
pattern: `share:digest:invite:${email}:`,
});
expect((await list()).res).toEqual([]);
let finished = false;
const draining = server.services.shareNotification
.onServerPrepareShutdown()
.then(() => {
finished = true;
});
try {
await new Promise((resolve) => setTimeout(resolve, 20));
expect(finished).toBe(false);
} finally {
release();
await draining;
}
expect((await list()).res).toHaveLength(failSend ? 1 : 0);
},
);
it('waits for an active send before draining entries queued meanwhile', async () => {
let releaseFirst!: () => void;
let releaseTail!: () => void;
const first = new Promise<void>((resolve) => {
releaseFirst = resolve;
});
const tail = new Promise<void>((resolve) => {
releaseTail = resolve;
});
const send = vi
.spyOn(server.clients.email, 'sendRaw')
.mockImplementationOnce(async () => {
await first;
return null;
})
.mockImplementationOnce(async () => {
await tail;
return null;
});
await queueInvite('[email protected]');
await vi.waitFor(() => expect(send).toHaveBeenCalledTimes(1));
(
server.services.shareNotification.config.share_notify_limits as {
emailBatchSeconds: number;
}
).emailBatchSeconds = 600;
await queueInvite('[email protected]', 8);
let finished = false;
const draining = server.services.shareNotification
.onServerPrepareShutdown()
.then(() => {
finished = true;
});
try {
releaseFirst();
await vi.waitFor(() => expect(send).toHaveBeenCalledTimes(2));
expect(finished).toBe(false);
} finally {
releaseFirst();
releaseTail();
await draining;
}
expect(send).toHaveBeenCalledTimes(2);
});
it.each(['onServerPrepareShutdown', 'onServerShutdown'] as const)(
'bounds %s while an active send is hung',
async (hook) => {
let release!: () => void;
const held = new Promise<void>((resolve) => {
release = resolve;
});
const send = vi
.spyOn(server.clients.email, 'sendRaw')
.mockImplementationOnce(async () => {
await held;
return null;
});
await queueInvite(`hung-${hook}@puter.local`);
await vi.waitFor(() => expect(send).toHaveBeenCalledTimes(1));
vi.useFakeTimers();
try {
let finished = false;
const draining = server.services.shareNotification[hook]().then(
() => {
finished = true;
},
);
await vi.advanceTimersByTimeAsync(4_999);
expect(finished).toBe(false);
await vi.advanceTimersByTimeAsync(1);
await draining;
} finally {
release();
vi.useRealTimers();
}
},
);
it('drains a digest queued after prepare at shutdown', async () => {
await server.services.shareNotification.onServerPrepareShutdown();
const send = vi
.spyOn(server.clients.email, 'sendRaw')
.mockResolvedValue(null);
(
server.services.shareNotification.config.share_notify_limits as {
emailBatchSeconds: number;
}
).emailBatchSeconds = 600;
await queueInvite('[email protected]');
await server.services.shareNotification.onServerShutdown();
expect(send).toHaveBeenCalledTimes(1);
expect(send.mock.calls[0][0].to).toBe('[email protected]');
await new Promise((resolve) => setTimeout(resolve, 30));
expect(send).toHaveBeenCalledTimes(1);
});
});
Loaded 100 of 493 files, more files were not shown because too many files have changed in this diff. Show more