fix(local): allow agent instruction file downloads and load the selected instance's environment in open

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 committed 2026-12-02 18:56:29 -06:00
1 parent 2d2cd91b8d
commit 105b58c96e
6 files changed
+32 -6

No files matched your search

+12
View File
@@ -114,6 +114,18 @@ describe("paperclipai open", () => {
expect(resolveRunInstanceId({ config: "/elsewhere/config.json" })).toBe("default");
});
it("loads the selected instance's .env, not another instance's, before choosing the mode", async () => {
const { loadPaperclipEnvFile } = await import("../config/env.js");
const { resolveLocalBoardConfigPath } = await import("../commands/open.js");
writeInstanceConfig("default", "local_trusted");
writeFileSync(path.join(home, "instances", "default", ".env"), "PAPERCLIP_DEPLOYMENT_MODE=authenticated\n");
const fooConfig = writeInstanceConfig("foo", "local_trusted");
expect(resolveLocalBoardConfigPath({ instance: "foo" })).toBe(fooConfig);
loadPaperclipEnvFile(resolveLocalBoardConfigPath({ instance: "foo" }));
expect(process.env.PAPERCLIP_DEPLOYMENT_MODE).toBeUndefined();
expect(resolveLocalBoardTarget({ instance: "foo" }).deploymentMode).toBe("local_trusted");
});
it("follows the server's deployment mode override from the environment", () => {
writeInstanceConfig("default", "local_trusted");
process.env.PAPERCLIP_DEPLOYMENT_MODE = "authenticated";
+7 -3
View File
@@ -62,11 +62,15 @@ export function resolveConfigDeploymentMode(configPath: string): DeploymentMode
* config: `--config`, else `--instance`, else the environment and working
* directory. The key sits next to that config file.
*/
export function resolveLocalBoardConfigPath(opts: Pick<OpenOptions, "config" | "instance"> = {}): string {
if (opts.config?.trim()) return path.resolve(opts.config);
if (opts.instance?.trim()) return resolveDefaultConfigPath(resolvePaperclipInstanceId(opts.instance));
return resolveConfigPath();
}
export function resolveLocalBoardTarget(opts: Pick<OpenOptions, "config" | "instance"> = {}): LocalBoardTarget {
const instanceOverride = opts.instance?.trim() ? resolvePaperclipInstanceId(opts.instance) : null;
const configPath = opts.config?.trim()
? path.resolve(opts.config)
: instanceOverride ? resolveDefaultConfigPath(instanceOverride) : resolveConfigPath();
const configPath = resolveLocalBoardConfigPath(opts);
const instanceId = instanceOverride ?? instanceIdForConfigPath(configPath) ?? resolvePaperclipInstanceId();
return {
configPath,
+6 -2
View File
@@ -33,7 +33,7 @@ import { initTelemetryFromConfigFile, flushTelemetry } from "./telemetry.js";
import { registerWorktreeCommands } from "./commands/worktree.js";
import { registerPluginCommands } from "./commands/client/plugin.js";
import { registerClientAuthCommands } from "./commands/client/auth.js";
import { registerOpenCommand } from "./commands/open.js";
import { registerOpenCommand, resolveLocalBoardConfigPath } from "./commands/open.js";
import { registerConnectCommand } from "./commands/client/connect.js";
import { registerTokenCommands } from "./commands/client/token.js";
import { registerPromptCommands } from "./commands/client/prompt.js";
@@ -133,7 +133,11 @@ program.hook("preAction", async (_thisCommand, actionCommand) => {
hasConfigOption: optionNames.has("config"),
hasContextOption: optionNames.has("context"),
});
loadPaperclipEnvFile(options.config);
// `open --instance <id>` must load that instance's .env, not the default one,
// so its deployment mode decides whether the board opens with a sign-in link.
loadPaperclipEnvFile(actionCommand.name() === "open"
? resolveLocalBoardConfigPath(options as { config?: string; instance?: string })
: options.config);
if (actionCommand.name() === "test-drive") {
assertTestDriveDatabaseIsolation(options.config);
}
+1 -1
View File
@@ -88,7 +88,7 @@ Paperclip now treats **bind** as a separate concern from auth:
- the operator credential is a key in `secrets/local-operator.key` next to the instance's config file (`<instance>/secrets/local-operator.key` for the standard layout). The server and `paperclipai open` find the same file whether the instance was chosen with `--instance`, `--config` or the environment. The server creates it with owner-only permissions on first use and restores those permissions on an existing key. Agent environments never receive it.
- browsers sign in through a one-time link that expires after 10 minutes. `paperclipai onboard`, and any server started with `PAPERCLIP_OPEN_ON_LISTEN=true`, open the board with that link. `paperclipai open` opens a fresh link for the running server, or prints it with `--no-browser`. Use `--instance <id>` or `--config <path>` to pick a named instance. A browser that is not signed in sees a screen that asks the operator to run `paperclipai open`.
- the link gives the board page a token through the URL fragment. The page keeps the token in its own origin's storage and sends it as a header on its own requests. A browser cannot set headers on a WebSocket, so the page adds the token as a query parameter to its live-events and plugin sockets only. The server accepts that parameter on socket upgrades only and never passes it to plugin code. Pages served by other local servers cannot read it, even on the same host name.
- browsers send cookies to every port on a host, so a cookie never grants board actions. The sign-in also sets an `HttpOnly`, `SameSite=Strict` cookie, named per instance, that only lets the browser read attachments, assets and workspace files it loads directly, and plugin event streams. Any local server the browser visits can receive that cookie. Its value rotates and stops working within a day, and the board page renews it with its header token.
- browsers send cookies to every port on a host, so a cookie never grants board actions. The sign-in also sets an `HttpOnly`, `SameSite=Strict` cookie, named per instance, that only lets the browser read attachments, assets, workspace files and agent instruction files it loads directly, and plugin event streams. Any local server the browser visits can receive that cookie. Its value rotates and stops working within a day, and the board page renews it with its header token.
- the CLI and local assistants use a board key. Run `paperclipai auth login`, approve the request in the signed-in browser, and the CLI stores the key.
- agent-run code running as the same OS user can still read the operator's files. Run agents with a filesystem sandbox that hides `~/.paperclip/instances/<id>/secrets`, or as a separate OS user, when you need that boundary to hold against a deliberate attempt.
@@ -42,6 +42,8 @@ function startServer(operator: LocalOperatorCredential): Promise<{ server: Serve
app.post("/api/whoami", whoami);
app.get("/api/attachments/:id/content", whoami);
app.get("/api/issues/:id/file-resources/content", whoami);
app.get("/api/agents/:id/instructions-bundle/file", whoami);
app.put("/api/agents/:id/instructions-bundle/file", whoami);
app.get("/api/assets/:id/content", whoami);
app.get("/api/heartbeat-runs/:id/provider-trace/download", whoami);
app.get("/api/companies/:id/audit/agent-actions.csv", whoami);
@@ -184,6 +186,8 @@ describe.skipIf(!supported)("operator browser sign-in", () => {
}
const change = await fetch(`${base}/api/attachments/a1/content`, { method: "POST", headers: { cookie } });
expect(await change.json()).toEqual({ type: "none", source: "none" });
const write = await fetch(`${base}/api/agents/a1/instructions-bundle/file`, { method: "PUT", headers: { cookie } });
expect(await write.json()).toEqual({ type: "none", source: "none" });
});
it("lets the browser load a file it was shown with the cookie alone", async () => {
@@ -192,6 +196,7 @@ describe.skipIf(!supported)("operator browser sign-in", () => {
"/api/attachments/a1/content",
"/api/assets/s1/content",
"/api/issues/i1/file-resources/content?path=a.txt",
"/api/agents/a1/instructions-bundle/file?path=AGENTS.md",
"/api/plugins/p1/bridge/stream/updates?companyId=c1",
]) {
const response = await fetch(`${base}${file}`, { headers: { cookie } });
@@ -36,6 +36,7 @@ export const DIRECT_BROWSER_READ_PATHS = [
/^\/api\/attachments\/[^/]+\/content$/,
/^\/api\/assets\/[^/]+\/content$/,
/^\/api\/issues\/[^/]+\/file-resources\/content$/,
/^\/api\/agents\/[^/]+\/instructions-bundle\/file$/,
/^\/api\/plugins\/[^/]+\/bridge\/stream\/[^/]+$/,
];