fix(local): match run and open on the instance, require a real socket upgrade, and use a shared gap on the sign-in card

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 committed 2026-11-25 09:01:44 -06:00
1 parent 2091596af7
commit 0b7e627224
8 files changed
+44 -17

No files matched your search

+9
View File
@@ -105,6 +105,15 @@ describe("paperclipai open", () => {
expect(localBoardEntryUrl("http://127.0.0.1:3100", defaultTarget)).toBe("http://127.0.0.1:3100");
});
it("resolves the same instance as \`paperclipai run\` for the same config file", async () => {
const { resolveRunInstanceId } = await import("../commands/run.js");
const fooConfig = writeInstanceConfig("foo", "local_trusted");
expect(resolveRunInstanceId({ config: fooConfig })).toBe("foo");
expect(resolveRunInstanceId({ config: fooConfig })).toBe(resolveLocalBoardTarget({ config: fooConfig }).instanceId);
expect(resolveRunInstanceId({ config: fooConfig, instance: "bar" })).toBe("bar");
expect(resolveRunInstanceId({ config: "/elsewhere/config.json" })).toBe("default");
});
it("follows the server's deployment mode override from the environment", () => {
writeInstanceConfig("default", "local_trusted");
process.env.PAPERCLIP_DEPLOYMENT_MODE = "authenticated";
+3 -8
View File
@@ -10,7 +10,9 @@ import {
resolveLocalOperatorKeyPathForConfig,
} from "@paperclipai/shared/local-operator-credential";
import { openUrl } from "../client/board-auth.js";
import { resolveDefaultConfigPath, resolvePaperclipHomeDir, resolvePaperclipInstanceId } from "../config/home.js";
import { instanceIdForConfigPath, resolveDefaultConfigPath, resolvePaperclipInstanceId } from "../config/home.js";
export { instanceIdForConfigPath };
import { readConfig, resolveConfigPath } from "../config/store.js";
import { readRuntimeInfo } from "../runtime-info.js";
import { inferApiBaseFromConfig, normalizeApiBase } from "./client/common.js";
@@ -48,13 +50,6 @@ function processIsRunning(pid: number): boolean {
}
}
/** The instance a config file belongs to, when it sits in `<home>/instances/<id>/`. */
export function instanceIdForConfigPath(configPath: string, homeDir = resolvePaperclipHomeDir()): string | null {
const directory = path.dirname(path.resolve(configPath));
if (path.dirname(directory) !== path.join(path.resolve(homeDir), "instances")) return null;
return path.basename(directory);
}
/** The deployment mode the server resolves: the environment first, then the config file. */
export function resolveConfigDeploymentMode(configPath: string): DeploymentMode {
const fromEnv = process.env.PAPERCLIP_DEPLOYMENT_MODE;
+12 -1
View File
@@ -13,6 +13,7 @@ import type { PaperclipConfig } from "../config/schema.js";
import { readConfig } from "../config/store.js";
import {
describeLocalInstancePaths,
instanceIdForConfigPath,
resolvePaperclipHomeDir,
resolvePaperclipInstanceId,
} from "../config/home.js";
@@ -46,8 +47,18 @@ export interface StartedServer {
shutdown?: (signal?: "SIGINT" | "SIGTERM") => Promise<void>;
}
/**
* `--instance`, else the instance whose standard config file was passed, else
* the environment. `paperclipai open` resolves the same way, so it finds this
* server's runtime info and key.
*/
export function resolveRunInstanceId(opts: Pick<RunOptions, "config" | "instance">): string {
const fromConfig = !opts.instance?.trim() && opts.config ? instanceIdForConfigPath(opts.config) : null;
return resolvePaperclipInstanceId(opts.instance?.trim() || fromConfig || undefined);
}
export async function runCommand(opts: RunOptions): Promise<void> {
const instanceId = resolvePaperclipInstanceId(opts.instance);
const instanceId = resolveRunInstanceId(opts);
process.env.PAPERCLIP_INSTANCE_ID = instanceId;
if (!opts.skipServiceManagerCheck) {
await assertForegroundRunAllowed(instanceId, opts.force);
+7
View File
@@ -71,3 +71,10 @@ export function describeLocalInstancePaths(instanceId?: string) {
storageDir: resolveDefaultStorageDir(resolvedInstanceId),
};
}
/** The instance a config file belongs to, when it sits in `<home>/instances/<id>/`. */
export function instanceIdForConfigPath(configPath: string, homeDir = resolvePaperclipHomeDir()): string | null {
const directory = path.dirname(path.resolve(configPath));
if (path.dirname(directory) !== path.join(path.resolve(homeDir), "instances")) return null;
return path.basename(directory);
}
@@ -225,9 +225,12 @@ describe.skipIf(!supported)("operator browser sign-in", () => {
}).on("error", reject);
});
const socketPath = `${base}/api/plugins/p1/api/socket?operator=${encodeURIComponent(token)}`;
const upgrade = { upgrade: "websocket", connection: "keep-alive" };
const upgrade = { upgrade: "websocket", connection: "Upgrade" };
expect(await whoamiOver(socketPath, upgrade)).toEqual({ type: "board", source: "local_implicit" });
expect(await whoamiOver(socketPath, {})).toEqual({ type: "none", source: "none" });
// An Upgrade header without a real upgrade handshake is a plain request.
expect(await whoamiOver(socketPath, { upgrade: "websocket", connection: "keep-alive" }))
.toEqual({ type: "none", source: "none" });
expect(await whoamiOver(`${base}/api/plugins/p1/api/socket`, { ...upgrade, cookie }))
.toEqual({ type: "none", source: "none" });
});
+1 -1
View File
@@ -370,7 +370,7 @@ export function actorMiddleware(db: Db, opts: ActorMiddlewareOptions): RequestHa
if (req.actor.source === "local_implicit") {
const operatorPresent = !operatorCredential?.required
|| operatorCredential.hasOperatorHeader(req.headers)
|| operatorCredential.hasSocketToken(req.headers, req.query as Record<string, unknown>)
|| operatorCredential.hasSocketToken(req.method, req.headers, req.query as Record<string, unknown>)
|| operatorCredential.hasDirectReadCookie(req.method, req.path, req.headers);
const granted = operatorPresent && (await localBoundary?.grantsImplicitBoard(req.socket)) === true;
if (!granted) req.actor = { type: "none", source: "none" };
@@ -107,8 +107,10 @@ export function createLocalOperatorCredential(options: LocalOperatorCredentialOp
* A browser cannot set headers on a WebSocket, so the board page sends its
* token as a query parameter, which is accepted on upgrade requests only.
*/
function hasSocketToken(headers: IncomingHttpHeaders, query: Record<string, unknown>): boolean {
if (headers.upgrade?.toLowerCase() !== "websocket") return false;
function hasSocketToken(method: string, headers: IncomingHttpHeaders, query: Record<string, unknown>): boolean {
if (method !== "GET" || headers.upgrade?.toLowerCase() !== "websocket") return false;
const connection = headers.connection?.toLowerCase().split(",").map((part) => part.trim()) ?? [];
if (!connection.includes("upgrade")) return false;
return hasBrowserToken(query[LOCAL_OPERATOR_SOCKET_PARAM] as string | undefined);
}
+4 -4
View File
@@ -75,13 +75,13 @@ function NoBoardAccessPage() {
function LocalOperatorSignIn() {
return (
<div className="mx-auto max-w-xl py-10">
<Card className="block p-6">
<Card className="flex flex-col gap-3 p-6">
<h1 className="text-xl font-semibold">Open Paperclip from your terminal</h1>
<p className="mt-2 text-sm text-muted-foreground">
<p className="text-sm text-muted-foreground">
This board only opens in a browser you signed in from this computer. Run this command in a terminal:
</p>
<pre className="mt-3 rounded-md bg-muted px-3 py-2 text-sm"><code>paperclipai open</code></pre>
<p className="mt-3 text-sm text-muted-foreground">
<pre className="rounded-md bg-muted px-3 py-2 text-sm"><code>paperclipai open</code></pre>
<p className="text-sm text-muted-foreground">
It opens the board here and keeps this browser signed in.
</p>
</Card>