Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(pages): guard owner-page changes and keep their cards

erywim12f369f2a897
diff --git a/server/pages/published-pages.test.ts b/server/pages/published-pages.test.ts
index 4fbc873af0..088ce859f1 100644
--- a/server/pages/published-pages.test.ts
+++ b/server/pages/published-pages.test.ts
@@ -47,10 +47,11 @@ function setup(options: { home?: boolean; quota?: number; selfHosted?: boolean;
...(options.liveRoots ? { liveRoots: options.liveRoots } : {}), ...(options.paused ? { paused: options.paused } : {}),
patch: (threadId, messageId, text, card) => { const shown = cards.find((each) => each.id === messageId && each.threadId === threadId); if (shown) Object.assign(shown, { text, card }); } });
const post = (threadId: string) => (text: string, card: PublishedPageCard) => { const id = `m${++ids}`; cards.push({ threadId, id, text, card }); return id; };
- const publish = (input: { path: string; title?: string; pageId?: string; botId?: string; threadId?: string; live?: boolean; visibility?: "public" | "owner" }) => pages.publisher.publish({
+ const publish = (input: { path: string; title?: string; pageId?: string; botId?: string; threadId?: string; live?: boolean; visibility?: "public" | "owner";
+ call?: { assertActive?: () => void; signal?: AbortSignal } }) => pages.publisher.publish({
botId: input.botId ?? "bot-1", threadId: input.threadId ?? "thread-1", path: input.path, title: input.title ?? "Bake sale",
...(input.pageId ? { pageId: input.pageId } : {}), ...(input.live !== undefined ? { live: input.live } : {}),
- ...(input.visibility ? { visibility: input.visibility } : {}), roots: [work], post: post(input.threadId ?? "thread-1") });
+ ...(input.visibility ? { visibility: input.visibility } : {}), ...(input.call ? { call: input.call } : {}), roots: [work], post: post(input.threadId ?? "thread-1") });
return { service, pages, work, data, cards, publish };
}
const ownerUrl = (pageId: string) => `https://cloud.example.test/api/published-pages/${pageId}/`;
@@ -343,6 +344,9 @@ describe("an owner-only published page", () => {
// The card that showed the old public link now carries the owner-only one.
expect(f.cards.map(({ card }) => [card.pageId, card.visibility])).toEqual([[publicRecord!.pageId, "owner"], [publicRecord!.pageId, "owner"]]);
expect(f.cards[0]!.card).toMatchObject({ state: "live", visibility: "owner", url: ownerUrl(publicRecord!.pageId) });
+ // Unpublishing marks both cards, including the one from before the switch.
+ expect(await f.pages.publisher.unpublish({ pageId: publicRecord!.pageId })).toMatchObject({ status: 200 });
+ expect(f.cards.map(({ card }) => card.state)).toEqual(["unpublished", "unpublished"]);
});
it("publishes again to the pages Worker with a new link when an owner-only page becomes public", async () => {
@@ -360,6 +364,52 @@ describe("an owner-only published page", () => {
expect(new PublishedPagesStore(join(f.data, "owner-pages.json")).list()).toEqual([]);
expect(f.cards.map(({ card }) => [card.pageId, card.visibility])).toEqual([[remote, undefined], [remote, undefined]]);
expect(f.cards[0]!.card).toMatchObject({ state: "live", url: `https://${remote}.mausbot.page/` });
+ // Unpublishing marks both cards, including the owner-only one from before the switch.
+ expect(await f.pages.publisher.unpublish({ pageId: remote! })).toMatchObject({ status: 200 });
+ expect(f.cards.map(({ card }) => card.state)).toEqual(["unpublished", "unpublished"]);
+ });
+
+ it("takes down a public copy this server has no record of when an explicit page id is made owner-only, and refuses when it can't", async () => {
+ const f = setup();
+ signIn();
+ writeSite(join(f.work, "site"));
+ await f.publish({ path: "site" });
+ const [page] = [...f.service.pages.keys()];
+ f.pages.store.forget(page!);
+ writeSite(join(f.work, "draft"), { "index.html": "<h1>Draft</h1>" });
+ f.service.state.down = true;
+ const refused = await f.publish({ path: "draft", pageId: page, title: "Draft", visibility: "owner" });
+ expect(refused.status).toBe(503);
+ expect(refused.text).toContain("could not be made owner-only");
+ expect(f.pages.files.has(page!)).toBe(false);
+ expect(f.pages.store.get(page!)).toBeNull();
+ f.service.state.down = false;
+ const switched = await f.publish({ path: "draft", pageId: page, title: "Draft", visibility: "owner" });
+ expect(switched.status).toBe(200);
+ expect(f.service.minted.map((call) => call.action)).toEqual(["create", "delete"]);
+ expect(f.service.pages.size).toBe(0);
+ expect(f.pages.files.read(page!, "")?.toString()).toBe("<h1>Draft</h1>");
+ expect(f.pages.store.get(page!)).toMatchObject({ visibility: "owner", title: "Draft" });
+ });
+
+ it("changes nothing when the tool call has ended before an owner-only save or unpublish", async () => {
+ const f = setup();
+ writeSite(join(f.work, "report"));
+ expect((await f.publish({ path: "report", visibility: "owner" })).status).toBe(200);
+ const [record] = f.pages.store.list();
+ writeFileSync(join(f.work, "report", "late.txt"), "later");
+ const stop = new AbortController();
+ stop.abort();
+ expect(await f.publish({ path: "report", visibility: "owner", call: { signal: stop.signal } })).toMatchObject({ status: 503 });
+ expect(f.pages.files.read(record!.pageId, "late.txt")).toBeNull();
+ expect(f.cards).toHaveLength(1);
+ await expect(f.publish({ path: "report", visibility: "owner", call: { assertActive: () => { throw new Error("the turn ended"); } } }))
+ .rejects.toThrow("the turn ended");
+ expect(f.pages.files.read(record!.pageId, "late.txt")).toBeNull();
+ expect(await f.pages.publisher.unpublish({ pageId: record!.pageId, call: { signal: stop.signal } })).toMatchObject({ status: 503 });
+ expect(f.pages.files.has(record!.pageId)).toBe(true);
+ expect(f.pages.store.get(record!.pageId)).not.toBeNull();
+ expect(f.cards.map(({ card }) => card.state)).toEqual(["live"]);
});
it("republishes a live owner-only page from its folder without touching the pages Worker", async () => {
diff --git a/server/pages/publisher.ts b/server/pages/publisher.ts
index 40618c5d17..bbfd639cc6 100644
--- a/server/pages/publisher.ts
+++ b/server/pages/publisher.ts
@@ -57,6 +57,12 @@ const LIVE_BROKEN_MS = 10 * 60_000;
const failureStatus: Record<PagesFailure["reason"], number> = { sign_in: 403, limit: 402, rate_limited: 429, gone: 410, refused: 400, unavailable: 503 };
+/** The call's turn has ended (Stop, or it settled): nothing local changes. A card's own buttons have no call and always run. */
+function stopped(call: PagesCall | undefined): PageOutcome | null {
+ call?.assertActive?.();
+ return call?.signal?.aborted ? { status: 503, text: "The request was stopped before the page changed. Ask again when you want it published." } : null;
+}
+
export function createPagePublisher(deps: {
client: PagesClient;
store: PagesStore;
@@ -155,17 +161,20 @@ export function createPagePublisher(deps: {
};
if (visibility === "owner") {
const pageId = target ?? ownerPageId(), url = ownerUrl(pageId);
+ const halted = stopped(input.call);
+ if (halted) return halted;
try {
deps.files.save(pageId, files);
} catch {
return { status: 500, text: "The page couldn't be saved on this server. Check that its data folder has room, then ask again." };
}
// A public page becomes owner-only only once its public copy is gone; until then the request is refused and the page
- // stays as it was.
- if (remembered && remembered.visibility !== "owner") {
+ // stays as it was. An explicit page id may name a public page another device published, which this server has no
+ // record of: its public copy is taken down too, or the request is refused.
+ if (remembered ? remembered.visibility !== "owner" : target !== undefined) {
let taken: Awaited<ReturnType<PagesClient["unpublish"]>>;
try {
- taken = await client.unpublish(remembered.pageId, input.call);
+ taken = await client.unpublish(remembered?.pageId ?? target!, input.call);
} catch (error) {
deps.files.remove(pageId);
throw error;
@@ -174,11 +183,12 @@ export function createPagePublisher(deps: {
deps.files.remove(pageId);
return { status: failureStatus[taken.reason], text: `The page could not be made owner-only: ${taken.message}` };
}
- store.forgetPublic(remembered.pageId);
+ if (remembered) store.forgetPublic(remembered.pageId);
}
const card: PublishedPageCard = { state: "live", title, pageId, url, visibility: "owner" };
const messageId = input.post(cardText(card), card, bundle);
store.published({ pageId, url, title, botId: remembered?.botId ?? input.botId, source: bundle.source,
+ ...(remembered && remembered.visibility !== "owner" ? { earlierCards: remembered.cards } : {}),
card: messageId ? { threadId: input.threadId, messageId } : null, visibility: "owner", live, signature });
if (remembered?.pageId !== pageId || remembered?.visibility !== "owner") patchOtherCards(card, messageId);
const updated = Boolean(target) && remembered?.visibility === "owner";
@@ -231,6 +241,7 @@ export function createPagePublisher(deps: {
const card: PublishedPageCard = { state: "live", title, pageId: result.pageId, url: result.url, usage: result.usage };
const messageId = input.post(cardText(card), card, bundle);
store.published({ pageId: result.pageId, url: result.url, title, botId: remembered?.botId ?? input.botId, source: bundle.source,
+ ...(remembered?.visibility === "owner" ? { earlierCards: remembered.cards } : {}),
card: messageId ? { threadId: input.threadId, messageId } : null, live, signature });
// The local copy of a page that was owner-only is gone now that the public page is live.
if (remembered?.visibility === "owner" && target) {
@@ -253,6 +264,8 @@ export function createPagePublisher(deps: {
async function unpublishNow(pageId: string, call: PagesCall | undefined): Promise<PageOutcome> {
const record = store.get(pageId);
if (record?.visibility === "owner" || (!record && deps.files.has(pageId))) {
+ const halted = stopped(call);
+ if (halted) return halted;
deps.files.remove(pageId);
const card = markUnpublished(pageId, null)
?? { state: "unpublished" as const, title: pageId, pageId, visibility: "owner" as const };
diff --git a/server/pages/store.ts b/server/pages/store.ts
index 5dcb2d7ed1..c25bd852ea 100644
--- a/server/pages/store.ts
+++ b/server/pages/store.ts
@@ -66,12 +66,14 @@ export class PublishedPagesStore {
return this.records;
}
- /** Records a publish (a new page or an update) and the card that shows it. `visibility` is "owner" only for the separate
- * owner-only file; a public record carries no field, so a page from before the setting reads as public. */
+ /** Records a publish and the card that shows it. `visibility` is "owner" only for the separate owner-only file; a public
+ * record carries no field, so a page from before the setting reads as public. `earlierCards` carries the ones a
+ * visibility switch leaves behind in the record this one replaces, so they stay registered with the new record too. */
published(input: { pageId: string; url: string; title: string; botId: string; source: string; card: { threadId: string; messageId: string } | null;
+ earlierCards?: PublishedPageRecord["cards"];
live?: boolean; signature?: string | null; uncertain?: boolean; visibility?: PublishedPageVisibility }, now = Date.now()): PublishedPageRecord {
const existing = this.get(input.pageId);
- const cards = [...(existing?.cards ?? []), ...(input.card ? [input.card] : [])].slice(-MAX_CARDS);
+ const cards = [...(existing?.cards ?? []), ...(input.earlierCards ?? []), ...(input.card ? [input.card] : [])].slice(-MAX_CARDS);
const record: PublishedPageRecord = { pageId: input.pageId, url: input.url, title: input.title, botId: input.botId, source: input.source, cards,
createdAt: existing?.createdAt ?? now, updatedAt: now,
...(input.visibility === "owner" ? { visibility: "owner" as const } : {}),