Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions apps/server/src/routes/threads/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,49 @@ import {
threadTabsSchema,
typedRoutes,
type PublicApiSchema,
type ThreadTab,
type ThreadTabsResponse,
type ThreadTabsWireResponse,
} from "@bb/server-contract";
import type { Hono } from "hono";
import { ApiError } from "../../errors.js";
import { requirePublicThread } from "../../services/lib/entity-lookup.js";
import type { AppDeps } from "../../types.js";

type WireThreadTab = ThreadTabsWireResponse["tabs"][number];

/**
* Omit `hostId` from the wire when it is `null`. The field arrived with a
* `.default(null)` after mobile builds had already shipped a `.strict()` copy
* of this schema, and installed apps cannot update with the server: an
* unknown key makes their SDK reject every tabs response, and the panel
* stops syncing ("Couldn't sync tabs"). Current clients parse the omission
* back to `null` through the default, so nothing is lost on the wire.
*/
function toWireThreadTab(tab: ThreadTab): WireThreadTab {
if (tab.kind === "host-file-preview") {
const { hostId, ...rest } = tab;
return hostId === null ? rest : tab;
}
if (
tab.kind === "plugin-panel" &&
tab.fileOpenerOwner?.kind === "host-file-preview"
) {
const { hostId, ...owner } = tab.fileOpenerOwner;
return hostId === null ? { ...tab, fileOpenerOwner: owner } : tab;
}
return tab;
}

function toWireThreadTabsResponse(
response: ThreadTabsResponse,
): ThreadTabsWireResponse {
return {
revision: response.revision,
tabs: response.tabs.map(toWireThreadTab),
};
}

function readThreadTabs(deps: AppDeps, threadId: string): ThreadTabsResponse {
const stored = getStoredThreadTabs(deps.db, threadId);
if (!stored) {
Expand All @@ -33,7 +69,9 @@ export function registerThreadTabRoutes(app: Hono, deps: AppDeps): void {

get(routes.tabs, (context) => {
const thread = requirePublicThread(deps.db, context.req.param("id"));
return context.json(readThreadTabs(deps, thread.id));
return context.json(
toWireThreadTabsResponse(readThreadTabs(deps, thread.id)),
);
});

put(routes.updateTabs, (context, payload) => {
Expand All @@ -52,6 +90,11 @@ export function registerThreadTabRoutes(app: Hono, deps: AppDeps): void {
);
}
deps.hub.notifyThread(thread.id, ["tabs-changed"]);
return context.json({ revision: result.revision, tabs: payload.tabs });
return context.json(
toWireThreadTabsResponse({
revision: result.revision,
tabs: payload.tabs,
}),
);
});
}
63 changes: 63 additions & 0 deletions apps/server/test/public/public-thread-tabs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,69 @@ describe("public thread tabs", () => {
});
});

it("omits a null hostId on the wire so strict older clients still parse", async () => {
await withTestHarness(async (harness) => {
const { thread } = seedThreadFixture(harness);
const tabs: readonly ThreadTab[] = [
{
environmentId: "env_1",
hostId: null,
id: "host-file-default",
kind: "host-file-preview",
lineRange: null,
path: "/tmp/default.png",
threadId: thread.id,
},
{
environmentId: null,
hostId: "host_1",
id: "host-file-explicit",
kind: "host-file-preview",
lineRange: null,
path: "/tmp/explicit.png",
threadId: null,
},
{
actionId: "inspect",
fileOpenerOwner: {
environmentId: "env_1",
hostId: null,
kind: "host-file-preview",
tab: { lineRange: null, path: "/tmp/owned.png" },
threadId: thread.id,
},
id: "plugin-panel",
kind: "plugin-panel",
paramsJson: null,
pluginId: "example",
title: "Inspector",
},
];

const updateResponse = await putTabs(harness, thread.id, {
expectedRevision: 0,
tabs,
});
expect(updateResponse.status).toBe(200);
const getResponse = await getTabs(harness, thread.id);
expect(getResponse.status).toBe(200);

for (const body of [
await readJson(updateResponse),
await readJson(getResponse),
]) {
const wire = body as {
tabs: Array<Record<string, unknown> & { fileOpenerOwner?: object }>;
};
expect(wire.tabs[0]).not.toHaveProperty("hostId");
expect(wire.tabs[1]).toHaveProperty("hostId", "host_1");
expect(wire.tabs[2]?.fileOpenerOwner).not.toHaveProperty("hostId");
// Current clients still read the omission back as null.
expect(threadTabsResponseSchema.parse(body).tabs).toEqual(tabs);
}
});
});

it("rejects duplicate tab ids and unknown threads", async () => {
await withTestHarness(async (harness) => {
const { thread } = seedThreadFixture(harness);
Expand Down
5 changes: 5 additions & 0 deletions packages/server-contract/src/api/thread-tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ export const threadTabsResponseSchema = z
})
.strict();
export type ThreadTabsResponse = z.infer<typeof threadTabsResponseSchema>;
/**
* The JSON the tabs routes send: defaulted fields (`hostId`) may be absent.
* Clients parse it with `threadTabsResponseSchema`, which fills them in.
*/
export type ThreadTabsWireResponse = z.input<typeof threadTabsResponseSchema>;

export const updateThreadTabsRequestSchema = z
.object({
Expand Down
6 changes: 3 additions & 3 deletions packages/server-contract/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ import type {
WorkspacePathListResponse,
} from "./api-types.js";
import type {
ThreadTabsResponse,
ThreadTabsWireResponse,
UpdateThreadTabsRequest,
} from "./api/thread-tabs.js";
import { updateThreadTabsRequestSchema } from "./api/thread-tabs.js";
Expand Down Expand Up @@ -1106,7 +1106,7 @@ export const publicApiRoutes = {
path: "/threads/:id/tabs",
method: "get",
request: noRequest<PathId>(),
response: jsonResponse<ThreadTabsResponse>(),
response: jsonResponse<ThreadTabsWireResponse>(),
}),
updateTabs: defineRoute({
path: "/threads/:id/tabs",
Expand All @@ -1115,7 +1115,7 @@ export const publicApiRoutes = {
updateThreadTabsRequestSchema,
),
response: [
jsonResponse<ThreadTabsResponse>(),
jsonResponse<ThreadTabsWireResponse>(),
jsonResponse<ApiError>({ status: 409 }),
],
}),
Expand Down
Loading