GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): preview copy only forwards text from the viewer's copy keys by maria-rcks · Pull Request #19561 · pingdotgg/t3code

fix(server): preview copy only forwards text from the viewer's copy keys #19561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
maria-rcks merged 3 commits into main from t3/fix-preview-copy-keys
Oct 14, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

fix(server): preview copy only forwards text from the viewer's copy keys #19561

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
847a93d
fix(server): preview copy only forwards text from the viewer's copy keys
maria-rcks Oct 14, 2026
3661e61
fix(server): preview copy keys forward only the text they copied
maria-rcks Oct 14, 2026
5aa3a9a
fix(server): preview copy keys reach frames in their own process
maria-rcks Oct 14, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
fix(server): preview copy keys reach frames in their own process
A frame that runs in its own process, such as one sent with an
Origin-Agent-Cluster header, is out of reach of the page's CDP session,
so its copy and cut keys sent the viewer nothing. Each such frame now
gets its own session that installs the same copy recorder, is cleared
and read with the page's frames, and is dropped when its target closes.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@maria-rcks
maria-rcks committed Oct 14, 2026
commit 5aa3a9ad6741966d07762e7ea93283be531d5166

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

78 changes: 68 additions & 10 deletions 78 apps/server/src/preview/ServerBrowser.test.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,41 @@ vi.mock("./ServerBrowserContexts.ts", () => ({
},
}));

function makeSession() {
/** A frame in its own process. Only its own session reaches the copy it holds. */
function makeFrame() {
const frame = {
copy: "",
detached: false,
isDetached: () => frame.detached,
recording: Promise.withResolvers<void>(),
};
return frame;
}

function makeSession(frame?: ReturnType<typeof makeFrame>) {
const events = new NodeEvents.EventEmitter();
return {
on: vi.fn(),
on: vi.fn((name: string, listener: () => void) => events.on(name, listener)),
emit: (name: string) => events.emit(name),
detach: vi.fn(async () => {}),
send: vi.fn(async (method: string, input?: unknown): Promise<Record<string, unknown>> => {
if (method === "Page.getNavigationHistory") return { currentIndex: 0, entries: [{}] };
if (method === "Page.getLayoutMetrics") return { cssVisualViewport: { pageX: 0, pageY: 0 } };
if (method === "Page.captureScreenshot") return { data: "ZnJhbWU=" };
if (method === "Page.getFrameTree") return { frameTree: { frame: { id: "main" } } };
if (method === "Page.createIsolatedWorld") return { executionContextId: 7 };
// A copy or cut key copies keyCopy; one that copies nothing leaves the frames as they were.
if (method === "Input.dispatchKeyEvent" && (input as { commands?: unknown }).commands)
pageCopy = keyCopy || pageCopy;
// The clipboard world hands over what the page's frames copied, once.
if (method === "Page.addScriptToEvaluateOnNewDocument") frame?.recording.resolve();
// A copy or cut key copies keyCopy in the focused frame, or the page's process if none;
// one that copies nothing leaves the frames as they were.
if (method === "Input.dispatchKeyEvent" && (input as { commands?: unknown }).commands) {
if (focusedFrame) focusedFrame.copy = keyCopy || focusedFrame.copy;
else pageCopy = keyCopy || pageCopy;
}
// The clipboard world hands over what the session's frames copied, once.
if (method === "Runtime.evaluate" && (input as { contextId?: number }).contextId === 7) {
const value = pageCopy;
pageCopy = "";
const value = frame ? frame.copy : pageCopy;
if (frame) frame.copy = "";
else pageCopy = "";
return { result: { value } };
}
return { result: { value: "evaluated" } };
Expand All @@ -102,6 +120,7 @@ function makeContext(onClose?: (context: BrowserContext) => void) {
setDefaultNavigationTimeout: vi.fn(),
context: () => context,
mainFrame: () => page,
frames: () => [page],
url: () => url,
title: vi.fn(async () => "test page"),
viewportSize: () => viewport,
Expand Down Expand Up @@ -141,8 +160,10 @@ function makeContext(onClose?: (context: BrowserContext) => void) {
sessions,
newPage: async () => page as unknown as Page,
grantPermissions: vi.fn(async () => {}),
newCDPSession: async () => {
const session = makeSession();
newCDPSession: async (target: unknown) => {
const session = makeSession(
target === page ? undefined : (target as ReturnType<typeof makeFrame>),
);
sessions.push(session);
return session;
},
Expand All @@ -164,6 +185,8 @@ let contextGate: PromiseWithResolvers<void> | null = null;
let pageCopy = "";
/** The text the next copy or cut key copies. */
let keyCopy = "";
/** The frame in its own process that has focus; otherwise keys copy in the page's process. */
let focusedFrame: ReturnType<typeof makeFrame> | null = null;
let contextFailure: Error | null = null;
/** Server tabs the fake desktop renders, and the endpoints the server connected to. */
let desktopRendersNext = false;
Expand Down Expand Up @@ -283,6 +306,7 @@ beforeEach(() => {
contextFailure = null;
pageCopy = "";
keyCopy = "";
focusedFrame = null;
desktopTabs.clear();
desktopRendersNext = false;
releasedDesktopTabs.length = 0;
Expand Down Expand Up @@ -1057,6 +1081,40 @@ it.live("only the controlling viewer's copy and cut keys reach its clipboard", (
).pipe(Effect.provide(layer)),
);

it.live("copy keys reach the viewer from a frame in its own process", () =>
Effect.scoped(
Effect.gen(function* () {
const { browser, tabId } = yield* ready;
const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
yield* viewer.input({ type: "takeControl" });
const copy = () =>
viewer.input({ type: "key", action: "down", key: "c", code: "KeyC", modifiers: 4 }).pipe(
Effect.andThen(Queue.clear(viewer.output)),
Effect.map((items) => items.filter((item) => item._tag === "clipboard")),
);
const frame = makeFrame();
contexts[0]!.page.emit("framenavigated", frame);
yield* Effect.promise(() => frame.recording.promise);
const frameSession = contexts[0]!.sessions.at(-1)!;
focusedFrame = frame;
keyCopy = "framed";
expect(yield* copy()).toEqual([{ _tag: "clipboard", text: "framed" }]);
// What the frame kept from before is cleared, not sent by a key elsewhere.
frame.copy = "left over";
focusedFrame = null;
keyCopy = "";
expect(yield* copy()).toEqual([]);
expect(frame.copy).toBe("");
// A frame that went away is no longer read.
frame.detached = true;
frame.copy = "gone";
frameSession.emit("close");
expect(yield* copy()).toEqual([]);
expect(frame.copy).toBe("gone");
}),
).pipe(Effect.provide(layer)),
);

it.live("a page download is saved, offered to the controller, and listed for the agent", () =>
Effect.scoped(
Effect.gen(function* () {
Expand Down
63 changes: 53 additions & 10 deletions 63 apps/server/src/preview/ServerBrowser.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import type {
Dialog,
Download,
FileChooser,
Frame,
Page,
} from "playwright-core";

Expand Down Expand Up @@ -288,6 +289,8 @@ interface ServerTab {
readonly tabId: string;
readonly page: Page;
readonly cdp: CDPSession;
/** Sessions of the page's frames that run in their own process. */
readonly frameSessions: Map<Frame, CDPSession>;
readonly createdAt: number;
/** The agent's latest request on this tab; idle agent tabs close. */
usedAt: number;
Expand Down Expand Up @@ -786,6 +789,7 @@ const make = Effect.gen(function* () {
tabId: snapshot.tabId,
page,
cdp,
frameSessions: new Map(),
createdAt: Date.now(),
usedAt: Date.now(),
viewers: new Set(),
Expand Down Expand Up @@ -834,7 +838,10 @@ const make = Effect.gen(function* () {
page.on("framenavigated", (frame) => {
// Same-document navigations (SPA routes) fire no load event.
if (frame === page.mainFrame() && !tab.loading) void reportLoaded(tab);
if (!desktop) void recordFrameCopies(tab, frame);
});
// An adopted popup's frames may have loaded before the tab listened.
if (adopted) for (const frame of page.frames()) void recordFrameCopies(tab, frame);
page.on("requestfailed", (request) => {
const errorText = request.failure()?.errorText ?? "";
pushBounded(tab.networkEntries, {
Expand Down Expand Up @@ -1137,7 +1144,7 @@ const make = Effect.gen(function* () {
await context.grantPermissions(["clipboard-write"]);
};

/** Records the copies made in each of a headless page's frames. */
/** Records the copies made in each frame a session reaches. */
const recordCopies = async (cdp: CDPSession) => {
// Scripts for new documents run only while this session's Page domain is on.
await cdp.send("Page.enable");
Expand All @@ -1148,19 +1155,48 @@ const make = Effect.gen(function* () {
});
};

/** Clears every frame's copy and returns what the key just handled copied or cut. */
const takeCopiedText = async (tab: ServerTab) => {
const { frameTree } = await tab.cdp.send("Page.getFrameTree");
const frameIds = (node: typeof frameTree): Array<string> => [
/**
* A frame in its own process, such as one with an `Origin-Agent-Cluster`
* header, is out of the page session's reach, so it records copies through
* its own session until that target closes.
*/
const recordFrameCopies = async (tab: ServerTab, frame: Frame) => {
if (frame === tab.page.mainFrame() || tab.frameSessions.has(frame)) return;
// A frame in its parent's process has no session of its own.
const cdp = await tab.page
.context()
.newCDPSession(frame)
.catch(() => null);
if (!cdp) return;
if (tab.frameSessions.has(frame) || frame.isDetached()) {
await cdp.detach().catch(constVoid);
return;
}
tab.frameSessions.set(frame, cdp);
cdp.on("close", () => {
if (tab.frameSessions.get(frame) !== cdp) return;
tab.frameSessions.delete(frame);
// A frame that moved to another process records through its new target.
if (!frame.isDetached()) void recordFrameCopies(tab, frame);
});
await recordCopies(cdp).catch(constVoid);
};

/** Clears and returns the copy of each frame a session reaches. */
const takeSessionCopies = async (cdp: CDPSession) => {
// An out-of-process frame that closed meanwhile copied nothing.
const tree = await cdp.send("Page.getFrameTree").catch(() => null);
if (!tree) return [];
const frameIds = (node: typeof tree.frameTree): Array<string> => [
node.frame.id,
...(node.childFrames ?? []).flatMap(frameIds),
];
const copies = await Promise.all(
frameIds(frameTree).map((frameId) =>
tab.cdp
return Promise.all(
frameIds(tree.frameTree).map((frameId) =>
cdp
.send("Page.createIsolatedWorld", { frameId, worldName: CLIPBOARD_WORLD })
.then(({ executionContextId }) =>
tab.cdp.send("Runtime.evaluate", {
cdp.send("Runtime.evaluate", {
contextId: executionContextId,
expression: "globalThis.takeCopy?.()",
returnByValue: true,
Expand All @@ -1173,7 +1209,14 @@ const make = Effect.gen(function* () {
),
),
);
return copies.find((text) => text.length > 0);
};

/** Clears every frame's copy and returns what the key just handled copied or cut. */
const takeCopiedText = async (tab: ServerTab) => {
const copies = await Promise.all(
[tab.cdp, ...tab.frameSessions.values()].map(takeSessionCopies),
);
return copies.flat().find((text) => text.length > 0);
};

const adoptPopup = async (opener: ServerTab, popup: Page) => {
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.