Preserve external instruction authority on Boat environments

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-10 09:46:56 -05:00
1 parent d1e5e6340c
commit eaf6be127b
2 files changed
+52 -1

No files matched your search

@@ -1,7 +1,8 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import * as persistentFiles from "../services/persistent-agent-files.js";
import { agentInstructionsService } from "../services/agent-instructions.js";
type TestAgent = {
@@ -37,6 +38,7 @@ describe("agent instructions service", () => {
});
afterEach(async () => {
vi.restoreAllMocks();
if (originalPaperclipHome === undefined) delete process.env.PAPERCLIP_HOME;
else process.env.PAPERCLIP_HOME = originalPaperclipHome;
if (originalPaperclipInstanceId === undefined) delete process.env.PAPERCLIP_INSTANCE_ID;
@@ -59,6 +61,48 @@ describe("agent instructions service", () => {
if (entry === "../invalid") expect(bundle.warnings.length).toBeGreaterThan(0);
});
it.each(["explicit", "legacy"])("keeps %s external instructions on their configured root after selecting Boat", async (configuration) => {
const root = await makeTempDir("boat-external-instructions-"); cleanupDirs.add(root);
await fs.writeFile(path.join(root, "AGENTS.md"), "external authority");
await fs.writeFile(path.join(root, "NEXT.md"), "next external entry");
const agent = { ...makeAgent(configuration === "explicit"
? { instructionsBundleMode: "external", instructionsRootPath: root, instructionsEntryFile: "AGENTS.md" }
: { instructionsFilePath: path.join(root, "AGENTS.md") }), defaultEnvironmentId: "boat-environment" };
const remote = vi.spyOn(persistentFiles, "persistentAgentFiles").mockResolvedValue({ root: "/remote/personal" } as never);
const db = { transaction: vi.fn(async () => { throw new Error("Must not adopt external instructions"); }) };
const svc = agentInstructionsService(db as never);
expect((await svc.getBundle(agent)).rootPath).toBe(root);
expect((await svc.readFile(agent, "AGENTS.md")).content).toBe("external authority");
expect((await svc.exportFiles(agent)).files["AGENTS.md"]).toBe("external authority");
const updated = await svc.updateBundle(agent, { entryFile: "NEXT.md" });
expect(updated.bundle.mode).toBe("external");
expect(updated.bundle.rootPath).toBe(root);
expect(updated.bundle.entryFile).toBe("NEXT.md");
expect(remote).not.toHaveBeenCalled();
expect(db.transaction).not.toHaveBeenCalled();
});
it("previews an explicit external-to-managed migration before adopting remote personal files", async () => {
const root = await makeTempDir("boat-instructions-migration-"); cleanupDirs.add(root);
await fs.writeFile(path.join(root, "AGENTS.md"), "external migration source");
const agent = makeAgent({ instructionsBundleMode: "external", instructionsRootPath: root, instructionsEntryFile: "AGENTS.md" });
const seedBytes = vi.fn();
vi.spyOn(persistentFiles, "persistentAgentFiles").mockResolvedValue({ root: "/remote/personal", seedBytes } as never);
const db = {
select: () => ({ from: () => ({ where: async () => [] }) }),
transaction: async (fn: (tx: unknown) => Promise<unknown>) => fn({
select: () => ({ from: () => ({ where: async () => [agent] }) }),
}),
};
const svc = agentInstructionsService(db as never);
const updated = await svc.updateBundle(agent, { mode: "managed" });
expect(updated.bundle.mode).toBe("managed");
expect(updated.bundle.rootPath).toBe(updated.bundle.managedRootPath);
expect(await fs.readFile(path.join(updated.bundle.managedRootPath, "AGENTS.md"), "utf8")).toBe("external migration source");
expect(seedBytes).not.toHaveBeenCalled();
expect(await fs.readFile(path.join(root, "AGENTS.md"), "utf8")).toBe("external migration source");
});
it("lists external bundles containing large binary assets", async () => {
const root = await makeTempDir("external-large-assets-"); cleanupDirs.add(root);
await fs.writeFile(path.join(root, "AGENTS.md"), "external instructions");
@@ -491,13 +491,20 @@ export function syncInstructionsBundleConfigFromFilePath(
export function agentInstructionsService(db?: Db) {
async function remoteFiles(agent: AgentLike) {
// External instructions keep their configured authority even when the
// agent has a separate personal directory on a computer.
if (agentInstructionsBundleMode(agent) === "external") return null;
const remote = db ? await persistentAgentFiles(db, agent.companyId, agent.id) : null;
if (remote && db) {
const root = await db.transaction(async tx => {
const [current] = await tx.select().from(agents).where(and(eq(agents.id, agent.id), eq(agents.companyId, agent.companyId)));
if (!current) throw notFound("Agent not found");
// updateBundle previews the prospective managed config before the
// caller saves it. Do not adopt a still-external persisted bundle.
if (agentInstructionsBundleMode(current) === "external") return null;
return adoptAgentFiles(tx, current);
});
if (!root) return null;
await seedPersistentAgentHome(remote, root);
}
return remote;