letting anyone change it
letting strangers change chosen pieces of a public app
for a public app where people who aren't signed in can change a few chosen pieces, each in a version only they see until an owner promotes it
import { clientOnly, exeSandbox } from "@tetraslam13/computah/server";
export default {
// ...
guests: { secret: process.env.COMPUTAH_GUEST_SECRET, forgetMs: 24 * 60 * 60 * 1000 },
trust: {
owners: ["you@example.com"],
members: "everyone",
memberPaths: ["app/playground/*.tsx"],
// each piece must stay a browser-only component that imports only react
memberCheck: clientOnly(),
memberNote: "you can change the framed parts of each page.",
// for the agent
memberRules: 'each piece keeps "use client" first, takes no props, and imports only react.',
memberDailyLimit: 10,
dailyLimit: 200,
},
// the same files again: a guest's version of one is swapped into the page with no build
pieces: ["app/playground/*.tsx"],
sandbox: { in: exeSandbox(), warm: { max: 3 } },
};the parts specific to this
guests | turns it on. opening the box makes you a guest, a random name in a cookie signed with secret so nobody can pose as another guest, and otherwise a member like any other. forgetMs deletes their version once they've been gone that long |
members: "everyone" | required, otherwise a guest is a visitor and can't do anything |
memberCheck: clientOnly() | refuses a change that stops being a client component ("use client" first), declares server code, or imports anything but react. clientOnly({ imports: ["react", "..."] }) lets more through |
pieces | the files a guest's version can swap into the page without the app being built for them. more on this below cause it's the thing that makes guests fast |
sandbox.warm | starts a sandbox when someone opens the box, so it's already there when they hit send. on sandboxes |
maxAgents | how many agents run at once across everyone, default 6, and the rest wait their turn |
guests.keep | only this many guest versions are kept, the oldest-used go first, so the disk can't fill up |
maxCopies, maxBuilds | only matter for versions that do need a build of their own: how many of those run at once, and how many builds go side by side |
memberPaths, memberNote, memberRules and the limits work like for any member (people and trust), and sandbox is on sandboxes
pieces
okay so the first version of this built the whole app again for every guest change, which is kind of silly when the rules already say a guest can only touch one small browser-only component that imports nothing but react. it worked, but a build was 14 seconds and they ran one or two at a time, so six people asking at once meant the last one waited a minute
with pieces the runner doesn't build anything for a version that only changed those files. it compiles the one changed file by itself (a few milliseconds), and the page swaps it in over the one the app was built with. so a guest's version is everyone's build plus their couple of components, and it costs a few kb and no running copy of the app
in the app, each piece gets rendered through <Piece>:
"use client";
import { Piece } from "@tetraslam13/computah";
import dynamic from "next/dynamic";
const Counter = dynamic(() => import("../playground/counter"), { ssr: false });
export function Playground() {
// `file` is the path from the root of the repo, same as in `pieces`
return <Piece file="app/playground/counter.tsx" stock={Counter} />;
}stock is the component as you built it, and it's what everyone sees until they have their own. when their change lands it shows up right there on the page they're on, with no reload. a piece that throws does it inside <Piece>, so put an error boundary around it if you want to catch that (i did)
it's still a real component running in the page, so a guest can make theirs cover the whole screen in falling cats if they want, and someone did within about an hour of me sharing the link lol
six guests asking at the same moment on this site, on an 8 cpu machine:
| first one done | last one done | |
|---|---|---|
| building for each guest, two builds at a time | 27s | 56s |
| pieces, with warm sandboxes | 9s | 15s |
what's left is pretty much all the agent thinking and typing
a few things that are good to know:
- a version only counts if every file it changed is a piece, was changed or added (not deleted), and imports only
react. anything else quietly gets a normal build of its own, so this also works for a trusted person's version that happens to only touch pieces - a piece that doesn't parse fails the same way a build would, and the agent gets the same chances to fix it
process.env.NODE_ENVis there for a piece and nothing else from the bundler is: no css imports, nonext/image, no importing your other components- it only applies when the runner serves the app. with vercel doing the serving every version still gets its own deployment
why this is safe
- a guest's code never runs on your server. you load each changeable piece with
next/dynamicandssr: false, from a wrapper outsidememberPaths, andclientOnly()rejects a change that stops being a client component, declares server code, or imports anything butreact - a guest's code runs only in their own browser. their changes live in their own version until an owner promotes one
- a guest's agent sees only a copy of the code. with a sandbox it runs on a throwaway machine, without your ignored files
- costs are capped. there are per-person and whole-app daily limits, a cap on agents running at once, and guests who don't come back get cleaned up
a fix for a broken build is held to the same memberPaths and memberCheck as the change
this site does exactly this
the "yours to change" windows are files in apps/docs/playground, shown by apps/docs/components/playground.tsx through <Piece>, with next/dynamic and ssr: false for the stock ones, inside an error boundary, so breaking yours doesn't take the card down. the config is computah.docs.config.mjs at the repo root:
guests: guestSecret ? { secret: guestSecret, forgetMs: 24 * 60 * 60 * 1000, keep: 500 } : undefined,
trust: {
owners: list(process.env.COMPUTAH_OWNERS ?? "you"),
trusted: list(process.env.COMPUTAH_TRUSTED),
members: guestSecret ? "everyone" : undefined,
memberPaths: ["apps/docs/playground/*.tsx"],
memberCheck: clientOnly(),
memberNote: "you can change the framed parts of each page. only you see your version.",
memberRules: "...", // one piece per page, "use client" first, no props, only react
memberDailyLimit: 12,
dailyLimit: Number(process.env.COMPUTAH_DAILY_LIMIT ?? 150),
},
pieces: ["apps/docs/playground/*.tsx"],
sandbox: process.env.COMPUTAH_SANDBOX === "exe" ? { in: exeSandbox(), warm: { max: 3 } } : undefined,
maxAgents: 6,