Remove root-level local spaces reorg

Keep local Paperclip runtime paths under the instance root and remove the root-level spaces CLI/migration surface. Preserve plugin-scoped space concepts outside this host/runtime PR.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-05-10 06:38:53 -05:00
1 parent 6bd30232c6
commit ab22821204
36 files changed
+278 -1106

No files matched your search

+2 -3
View File
@@ -12,7 +12,6 @@ describe("applyDataDirOverride", () => {
delete process.env.PAPERCLIP_CONFIG;
delete process.env.PAPERCLIP_CONTEXT;
delete process.env.PAPERCLIP_INSTANCE_ID;
delete process.env.PAPERCLIP_SPACE_ID;
});
afterEach(() => {
@@ -30,7 +29,7 @@ describe("applyDataDirOverride", () => {
expect(home).toBe(expectedHome);
expect(process.env.PAPERCLIP_HOME).toBe(expectedHome);
expect(process.env.PAPERCLIP_CONFIG).toBe(
path.resolve(expectedHome, "instances", "default", "spaces", "default", "config.json"),
path.resolve(expectedHome, "instances", "default", "config.json"),
);
expect(process.env.PAPERCLIP_CONTEXT).toBe(path.resolve(expectedHome, "context.json"));
expect(process.env.PAPERCLIP_INSTANCE_ID).toBe("default");
@@ -47,7 +46,7 @@ describe("applyDataDirOverride", () => {
expect(home).toBe(path.resolve("/tmp/paperclip-alt"));
expect(process.env.PAPERCLIP_INSTANCE_ID).toBe("dev_1");
expect(process.env.PAPERCLIP_CONFIG).toBe(
path.resolve("/tmp/paperclip-alt", "instances", "dev_1", "spaces", "default", "config.json"),
path.resolve("/tmp/paperclip-alt", "instances", "dev_1", "config.json"),
);
});
+1 -2
View File
@@ -24,8 +24,7 @@ describe("home path resolution", () => {
const paths = describeLocalInstancePaths();
expect(paths.homeDir).toBe(home);
expect(paths.instanceId).toBe("default");
expect(paths.activeSpaceId).toBe("default");
expect(paths.configPath).toBe(path.resolve(home, "instances", "default", "spaces", "default", "config.json"));
expect(paths.configPath).toBe(path.resolve(home, "instances", "default", "config.json"));
});
it("supports PAPERCLIP_HOME and explicit instance ids", () => {
+10 -19
View File
@@ -89,7 +89,6 @@ describe("onboard", () => {
delete process.env.PAPERCLIP_HOME;
delete process.env.PAPERCLIP_CONFIG;
delete process.env.PAPERCLIP_INSTANCE_ID;
delete process.env.PAPERCLIP_SPACE_ID;
delete process.env.PAPERCLIP_BIND;
delete process.env.PAPERCLIP_BIND_HOST;
delete process.env.PAPERCLIP_TAILNET_BIND_HOST;
@@ -135,8 +134,8 @@ describe("onboard", () => {
expect(raw.server.host).toBe("127.0.0.1");
});
it("creates default-space config and data paths for a fresh PAPERCLIP_HOME", async () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-onboard-space-"));
it("creates instance-root config and data paths for a fresh PAPERCLIP_HOME", async () => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-onboard-home-"));
const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-onboard-cwd-"));
process.chdir(cwd);
process.env.PAPERCLIP_HOME = home;
@@ -144,24 +143,16 @@ describe("onboard", () => {
await onboard({ yes: true, invokedByRun: true });
const instanceRoot = path.join(home, "instances", "default");
const spaceRoot = path.join(instanceRoot, "spaces", "default");
const configPath = path.join(spaceRoot, "config.json");
const registryPath = path.join(instanceRoot, "config.json");
const configPath = path.join(instanceRoot, "config.json");
const raw = JSON.parse(fs.readFileSync(configPath, "utf8")) as PaperclipConfig;
const registry = JSON.parse(fs.readFileSync(registryPath, "utf8")) as { activeSpaceId?: string };
expect(registry.activeSpaceId).toBe("default");
expect(raw.database.embeddedPostgresDataDir).toBe(path.join(spaceRoot, "db"));
expect(raw.database.backup.dir).toBe(path.join(spaceRoot, "data", "backups"));
expect(raw.logging.logDir).toBe(path.join(spaceRoot, "logs"));
expect(raw.storage.localDisk.baseDir).toBe(path.join(spaceRoot, "data", "storage"));
expect(raw.secrets.localEncrypted.keyFilePath).toBe(path.join(spaceRoot, "secrets", "master.key"));
expect(fs.existsSync(path.join(spaceRoot, ".env"))).toBe(true);
expect(fs.existsSync(path.join(spaceRoot, "secrets", "master.key"))).toBe(true);
for (const rootName of ["db", "data", "secrets", "logs", "workspaces", "projects", "companies"]) {
expect(fs.existsSync(path.join(instanceRoot, rootName))).toBe(false);
}
expect(raw.database.embeddedPostgresDataDir).toBe(path.join(instanceRoot, "db"));
expect(raw.database.backup.dir).toBe(path.join(instanceRoot, "data", "backups"));
expect(raw.logging.logDir).toBe(path.join(instanceRoot, "logs"));
expect(raw.storage.localDisk.baseDir).toBe(path.join(instanceRoot, "data", "storage"));
expect(raw.secrets.localEncrypted.keyFilePath).toBe(path.join(instanceRoot, "secrets", "master.key"));
expect(fs.existsSync(path.join(instanceRoot, ".env"))).toBe(true);
expect(fs.existsSync(path.join(instanceRoot, "secrets", "master.key"))).toBe(true);
});
it("supports authenticated/private quickstart bind presets", async () => {
-218
View File
@@ -1,218 +0,0 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { defaultServerRunningCheck, migrateDefaultSpaceInstall } from "../commands/spaces.js";
import { readConfig, resolveConfigPath } from "../config/store.js";
import type { PaperclipConfig } from "../config/schema.js";
const ORIGINAL_ENV = { ...process.env };
const ORIGINAL_CWD = process.cwd();
function writeJson(filePath: string, value: unknown): void {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });
}
function writeText(filePath: string, value: string): void {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, value);
}
function createLegacyInstallFixture() {
const home = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-space-migrate-home-"));
const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-space-migrate-cwd-"));
process.chdir(cwd);
process.env.PAPERCLIP_HOME = home;
delete process.env.PAPERCLIP_CONFIG;
delete process.env.PAPERCLIP_INSTANCE_ID;
delete process.env.PAPERCLIP_SPACE_ID;
const instanceRoot = path.join(home, "instances", "default");
const spaceRoot = path.join(instanceRoot, "spaces", "default");
const config: PaperclipConfig = {
$meta: {
version: 1,
updatedAt: "2026-05-09T00:00:00.000Z",
source: "onboard",
},
database: {
mode: "embedded-postgres",
embeddedPostgresDataDir: path.join(instanceRoot, "db"),
embeddedPostgresPort: 54329,
backup: {
enabled: true,
intervalMinutes: 60,
retentionDays: 30,
dir: path.join(instanceRoot, "data", "backups"),
},
},
logging: {
mode: "file",
logDir: path.join(instanceRoot, "logs"),
},
server: {
deploymentMode: "local_trusted",
exposure: "private",
host: "127.0.0.1",
port: 3100,
allowedHostnames: [],
serveUi: true,
},
auth: {
baseUrlMode: "auto",
disableSignUp: false,
},
telemetry: {
enabled: true,
},
storage: {
provider: "local_disk",
localDisk: {
baseDir: path.join(instanceRoot, "data", "storage"),
},
s3: {
bucket: "paperclip",
region: "us-east-1",
prefix: "",
forcePathStyle: false,
},
},
secrets: {
provider: "local_encrypted",
strictMode: false,
localEncrypted: {
keyFilePath: path.join(instanceRoot, "secrets", "master.key"),
},
},
};
writeJson(path.join(instanceRoot, "config.json"), config);
writeText(path.join(instanceRoot, ".env"), "PAPERCLIP_AGENT_JWT_SECRET=test-secret\n");
writeText(path.join(instanceRoot, "db", "PG_VERSION"), "17\n");
writeText(path.join(instanceRoot, "data", "storage", "hello.txt"), "hello\n");
writeText(path.join(instanceRoot, "logs", "server.log"), "log\n");
writeText(path.join(instanceRoot, "secrets", "master.key"), "01234567890123456789012345678901");
writeText(path.join(instanceRoot, "workspaces", "agent-1", "README.md"), "workspace\n");
writeText(path.join(instanceRoot, "projects", "company", "project", "repo", "README.md"), "project\n");
writeText(path.join(instanceRoot, "companies", "company-1", "codex-home", "config.toml"), "model=\"x\"\n");
writeText(path.join(instanceRoot, "codex-home", "config.toml"), "model=\"shared\"\n");
return { instanceRoot, spaceRoot };
}
beforeEach(() => {
process.env = { ...ORIGINAL_ENV };
});
afterEach(() => {
process.env = { ...ORIGINAL_ENV };
process.chdir(ORIGINAL_CWD);
vi.unstubAllGlobals();
});
describe("spaces migrate-default", () => {
it("moves a legacy root-shaped install into spaces/default and rewrites config paths", async () => {
const fixture = createLegacyInstallFixture();
const result = await migrateDefaultSpaceInstall({
serverRunningCheck: async () => false,
});
expect(result.status).toBe("migrated");
expect(result.movedPaths).toEqual([
"config.json",
".env",
"db",
"data",
"logs",
"secrets",
"workspaces",
"projects",
"companies",
"codex-home",
]);
expect(fs.existsSync(path.join(fixture.instanceRoot, "db"))).toBe(false);
expect(fs.readFileSync(path.join(fixture.spaceRoot, ".env"), "utf8")).toContain("PAPERCLIP_AGENT_JWT_SECRET");
expect(fs.readFileSync(path.join(fixture.spaceRoot, "db", "PG_VERSION"), "utf8")).toBe("17\n");
expect(fs.readFileSync(path.join(fixture.spaceRoot, "data", "storage", "hello.txt"), "utf8")).toBe("hello\n");
expect(fs.readFileSync(path.join(fixture.spaceRoot, "companies", "company-1", "codex-home", "config.toml"), "utf8")).toContain("model");
expect(fs.readFileSync(path.join(fixture.spaceRoot, "codex-home", "config.toml"), "utf8")).toContain("shared");
const migratedConfig = JSON.parse(fs.readFileSync(path.join(fixture.spaceRoot, "config.json"), "utf8")) as PaperclipConfig;
expect(migratedConfig.database.embeddedPostgresDataDir).toBe(path.join(fixture.spaceRoot, "db"));
expect(migratedConfig.database.backup.dir).toBe(path.join(fixture.spaceRoot, "data", "backups"));
expect(migratedConfig.logging.logDir).toBe(path.join(fixture.spaceRoot, "logs"));
expect(migratedConfig.storage.localDisk.baseDir).toBe(path.join(fixture.spaceRoot, "data", "storage"));
expect(migratedConfig.secrets.localEncrypted.keyFilePath).toBe(path.join(fixture.spaceRoot, "secrets", "master.key"));
const marker = JSON.parse(fs.readFileSync(path.join(fixture.instanceRoot, "config.json"), "utf8")) as {
activeSpaceId?: string;
defaultSpaceMigration?: { sourceRoot?: string; destinationRoot?: string; movedPaths?: string[] };
};
expect(marker.activeSpaceId).toBe("default");
expect(marker.defaultSpaceMigration).toMatchObject({
sourceRoot: fixture.instanceRoot,
destinationRoot: fixture.spaceRoot,
movedPaths: result.movedPaths,
});
expect(resolveConfigPath()).toBe(path.join(fixture.spaceRoot, "config.json"));
expect(readConfig()?.database.embeddedPostgresDataDir).toBe(path.join(fixture.spaceRoot, "db"));
});
it("refuses to merge when destination data already exists", async () => {
const fixture = createLegacyInstallFixture();
writeText(path.join(fixture.spaceRoot, "db", "PG_VERSION"), "existing\n");
await expect(migrateDefaultSpaceInstall({
serverRunningCheck: async () => false,
})).rejects.toThrow(/destination paths already exist:[\s\S]*db/);
expect(fs.readFileSync(path.join(fixture.instanceRoot, "db", "PG_VERSION"), "utf8")).toBe("17\n");
expect(fs.readFileSync(path.join(fixture.spaceRoot, "db", "PG_VERSION"), "utf8")).toBe("existing\n");
});
it("refuses to migrate when the server appears to be running", async () => {
const fixture = createLegacyInstallFixture();
await expect(migrateDefaultSpaceInstall({
serverRunningCheck: async () => true,
})).rejects.toThrow(/server appears to be running/);
expect(fs.existsSync(path.join(fixture.instanceRoot, "config.json"))).toBe(true);
expect(fs.existsSync(path.join(fixture.spaceRoot, "config.json"))).toBe(false);
});
it("checks the destination space config when the instance root only has a space registry", async () => {
const fixture = createLegacyInstallFixture();
const destinationConfig = JSON.parse(fs.readFileSync(path.join(fixture.instanceRoot, "config.json"), "utf8")) as PaperclipConfig;
fs.rmSync(path.join(fixture.instanceRoot, "config.json"));
writeJson(path.join(fixture.instanceRoot, "config.json"), {
$meta: { version: 1, updatedAt: "2026-05-10T00:00:00.000Z", source: "system" },
activeSpaceId: "default",
spaces: [{ id: "default", root: "spaces/default", createdAt: "2026-05-10T00:00:00.000Z" }],
});
writeJson(path.join(fixture.spaceRoot, "config.json"), {
...destinationConfig,
server: {
...destinationConfig.server,
port: 43210,
},
});
const fetchMock = vi.fn(async () => ({ status: 204 }));
vi.stubGlobal("fetch", fetchMock);
await expect(defaultServerRunningCheck({
instanceId: "default",
sourceRoot: fixture.instanceRoot,
destinationRoot: fixture.spaceRoot,
legacyRuntimeConfig: false,
sourcePathNames: [],
conflicts: [],
})).resolves.toBe(true);
expect(fetchMock).toHaveBeenCalledWith("http://127.0.0.1:43210/api/health", expect.any(Object));
});
});
-4
View File
@@ -15,7 +15,6 @@ import {
resolveDefaultLogsDir,
resolvePaperclipInstanceId,
} from "../config/home.js";
import { ensureDefaultSpaceRegistry } from "@paperclipai/shared/space-paths";
import { printPaperclipCliBanner } from "../utils/banner.js";
type Section = "llm" | "database" | "logging" | "server" | "storage" | "secrets";
@@ -178,9 +177,6 @@ export async function configure(opts: {
config.$meta.updatedAt = new Date().toISOString();
config.$meta.source = "configure";
if (configPath === resolveConfigPath()) {
ensureDefaultSpaceRegistry({ source: "configure" });
}
writeConfig(config, opts.config);
p.log.success(`${SECTION_LABELS[section]} configuration updated.`);
+1 -6
View File
@@ -36,7 +36,6 @@ import {
resolveDefaultLogsDir,
resolvePaperclipInstanceId,
} from "../config/home.js";
import { ensureDefaultSpaceRegistry } from "@paperclipai/shared/space-paths";
import { bootstrapCeoInvite } from "./auth-bootstrap-ceo.js";
import { printPaperclipCliBanner } from "../utils/banner.js";
import {
@@ -334,7 +333,7 @@ export async function onboard(opts: OnboardOptions): Promise<void> {
const instance = describeLocalInstancePaths(resolvePaperclipInstanceId());
p.log.message(
pc.dim(
`Local home: ${instance.homeDir} | instance: ${instance.instanceId} | space: ${instance.activeSpaceId} | config: ${configPath}`,
`Local home: ${instance.homeDir} | instance: ${instance.instanceId} | config: ${configPath}`,
),
);
@@ -626,10 +625,6 @@ export async function onboard(opts: OnboardOptions): Promise<void> {
p.log.message(pc.dim(`Using existing local secrets key file at ${keyResult.path}`));
}
if (configPath === resolveConfigPath()) {
ensureDefaultSpaceRegistry({ source: "onboard" });
}
writeConfig(config, opts.config);
if (tc) trackInstallCompleted(tc, {
-269
View File
@@ -1,269 +0,0 @@
import fs from "node:fs";
import path from "node:path";
import { Command } from "commander";
import pc from "picocolors";
import {
DEFAULT_PAPERCLIP_SPACE_ID,
DEFAULT_SPACE_ADAPTER_LOCAL_PATH_NAMES,
DEFAULT_SPACE_OWNED_PATH_NAMES,
createDefaultSpaceRegistry,
expandHomePrefix,
isPaperclipRuntimeConfig,
resolvePaperclipInstanceConfigPath,
resolvePaperclipInstanceId,
resolvePaperclipInstanceRoot,
resolvePaperclipSpacesRoot,
} from "@paperclipai/shared/space-paths";
type JsonObject = Record<string, unknown>;
export interface DefaultSpaceMigrationPlan {
instanceId: string;
sourceRoot: string;
destinationRoot: string;
legacyRuntimeConfig: boolean;
sourcePathNames: string[];
conflicts: Array<{ pathName: string; sourcePath: string; destinationPath: string }>;
}
export interface DefaultSpaceMigrationResult {
status: "migrated" | "noop" | "dry_run";
plan: DefaultSpaceMigrationPlan;
movedPaths: string[];
markerPath: string | null;
}
export interface MigrateDefaultSpaceOptions {
instanceId?: string;
dryRun?: boolean;
skipServerCheck?: boolean;
serverRunningCheck?: (plan: DefaultSpaceMigrationPlan) => Promise<boolean>;
}
const MIGRATION_PATH_NAMES = [
...DEFAULT_SPACE_OWNED_PATH_NAMES,
...DEFAULT_SPACE_ADAPTER_LOCAL_PATH_NAMES,
] as const;
function readJsonIfPresent(filePath: string): unknown | null {
if (!fs.existsSync(filePath)) return null;
try {
return JSON.parse(fs.readFileSync(filePath, "utf8"));
} catch {
return null;
}
}
function isObject(value: unknown): value is JsonObject {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function isPathWithin(rootPath: string, candidatePath: string): boolean {
const root = path.resolve(rootPath);
const candidate = path.resolve(candidatePath);
const relative = path.relative(root, candidate);
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
}
function rewriteSpaceRootString(value: string, sourceRoot: string, destinationRoot: string): string {
const resolved = path.resolve(expandHomePrefix(value));
if (!isPathWithin(sourceRoot, resolved)) return value;
const relative = path.relative(path.resolve(sourceRoot), resolved);
return path.resolve(destinationRoot, relative);
}
function rewriteConfigPaths(value: unknown, sourceRoot: string, destinationRoot: string): unknown {
if (typeof value === "string") return rewriteSpaceRootString(value, sourceRoot, destinationRoot);
if (Array.isArray(value)) return value.map((entry) => rewriteConfigPaths(entry, sourceRoot, destinationRoot));
if (!isObject(value)) return value;
return Object.fromEntries(
Object.entries(value).map(([key, entry]) => [key, rewriteConfigPaths(entry, sourceRoot, destinationRoot)]),
);
}
function writeJson(filePath: string, value: unknown, mode = 0o600): void {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`, { mode });
}
function atomicWriteJson(filePath: string, value: unknown, mode = 0o600): void {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
const tmpPath = path.join(path.dirname(filePath), `.${path.basename(filePath)}.${process.pid}.tmp`);
writeJson(tmpPath, value, mode);
fs.renameSync(tmpPath, filePath);
}
export function buildDefaultSpaceMigrationPlan(options: Pick<MigrateDefaultSpaceOptions, "instanceId"> = {}): DefaultSpaceMigrationPlan {
const instanceId = resolvePaperclipInstanceId(options.instanceId);
const sourceRoot = resolvePaperclipInstanceRoot({ instanceId });
const destinationRoot = path.resolve(resolvePaperclipSpacesRoot({ instanceId }), DEFAULT_PAPERCLIP_SPACE_ID);
const instanceConfigPath = resolvePaperclipInstanceConfigPath({ instanceId });
const legacyRuntimeConfig = isPaperclipRuntimeConfig(readJsonIfPresent(instanceConfigPath));
const sourcePathNames = MIGRATION_PATH_NAMES.filter((pathName) => {
if (pathName === "config.json" && !legacyRuntimeConfig) return false;
return fs.existsSync(path.join(sourceRoot, pathName));
});
const conflicts = sourcePathNames
.map((pathName) => ({
pathName,
sourcePath: path.join(sourceRoot, pathName),
destinationPath: path.join(destinationRoot, pathName),
}))
.filter((entry) => fs.existsSync(entry.destinationPath));
return {
instanceId,
sourceRoot,
destinationRoot,
legacyRuntimeConfig,
sourcePathNames,
conflicts,
};
}
function readServerEndpointFromConfig(configPath: string): { host: string; port: number } | null {
const parsed = readJsonIfPresent(configPath);
if (!isObject(parsed)) return null;
const server = parsed.server;
if (!isObject(server)) return null;
const rawHost = typeof server.host === "string" && server.host.trim().length > 0
? server.host.trim()
: "127.0.0.1";
const host = rawHost === "0.0.0.0" || rawHost === "::" ? "127.0.0.1" : rawHost;
const port = typeof server.port === "number" && Number.isInteger(server.port) ? server.port : 3100;
if (port <= 0 || port > 65535) return null;
return { host, port };
}
export async function defaultServerRunningCheck(plan: DefaultSpaceMigrationPlan): Promise<boolean> {
const endpoint =
readServerEndpointFromConfig(path.join(plan.sourceRoot, "config.json")) ??
readServerEndpointFromConfig(path.join(plan.destinationRoot, "config.json"));
if (!endpoint) return false;
const host = endpoint.host.includes(":") ? `[${endpoint.host}]` : endpoint.host;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 500);
try {
const response = await fetch(`http://${host}:${endpoint.port}/api/health`, {
signal: controller.signal,
});
return response.status >= 200 && response.status < 500;
} catch {
return false;
} finally {
clearTimeout(timeout);
}
}
function assertMigrationPreflight(plan: DefaultSpaceMigrationPlan): void {
if (plan.conflicts.length === 0) return;
const details = plan.conflicts
.map((conflict) => `- ${conflict.pathName}: ${conflict.sourcePath} -> ${conflict.destinationPath}`)
.join("\n");
throw new Error(`Cannot migrate default space because destination paths already exist:\n${details}`);
}
function moveConfigWithRewrittenPaths(sourcePath: string, destinationPath: string, sourceRoot: string, destinationRoot: string): void {
const parsed = JSON.parse(fs.readFileSync(sourcePath, "utf8")) as unknown;
const rewritten = rewriteConfigPaths(parsed, sourceRoot, destinationRoot);
atomicWriteJson(destinationPath, rewritten);
fs.unlinkSync(sourcePath);
}
function writeMigrationMarker(plan: DefaultSpaceMigrationPlan, movedPaths: string[]): string {
const migratedAt = new Date().toISOString();
const registry = createDefaultSpaceRegistry("system");
registry.$meta.updatedAt = migratedAt;
registry.defaultSpaceMigration = {
migratedAt,
sourceRoot: plan.sourceRoot,
destinationRoot: plan.destinationRoot,
movedPaths,
};
const markerPath = path.join(plan.sourceRoot, "config.json");
writeJson(markerPath, registry);
return markerPath;
}
export async function migrateDefaultSpaceInstall(
options: MigrateDefaultSpaceOptions = {},
): Promise<DefaultSpaceMigrationResult> {
const plan = buildDefaultSpaceMigrationPlan(options);
if (plan.sourcePathNames.length === 0) {
return { status: "noop", plan, movedPaths: [], markerPath: null };
}
assertMigrationPreflight(plan);
const serverRunningCheck = options.serverRunningCheck ?? defaultServerRunningCheck;
if (!options.skipServerCheck && await serverRunningCheck(plan)) {
throw new Error(
`Cannot migrate default space while a Paperclip server appears to be running for ${plan.sourceRoot}. Stop Paperclip and retry.`,
);
}
if (options.dryRun) {
return { status: "dry_run", plan, movedPaths: plan.sourcePathNames, markerPath: null };
}
fs.mkdirSync(plan.destinationRoot, { recursive: true });
const movedPaths: string[] = [];
for (const pathName of plan.sourcePathNames) {
const sourcePath = path.join(plan.sourceRoot, pathName);
const destinationPath = path.join(plan.destinationRoot, pathName);
fs.mkdirSync(path.dirname(destinationPath), { recursive: true });
if (pathName === "config.json") {
moveConfigWithRewrittenPaths(sourcePath, destinationPath, plan.sourceRoot, plan.destinationRoot);
} else {
fs.renameSync(sourcePath, destinationPath);
}
movedPaths.push(pathName);
}
const markerPath = writeMigrationMarker(plan, movedPaths);
return { status: "migrated", plan, movedPaths, markerPath };
}
function printMigrationResult(result: DefaultSpaceMigrationResult): void {
if (result.status === "noop") {
console.log(pc.dim(`No legacy default-space data found under ${result.plan.sourceRoot}.`));
return;
}
const verb = result.status === "dry_run" ? "Would migrate" : "Migrated";
console.log(`${pc.green(verb)} default space:`);
console.log(` ${pc.dim("from")} ${result.plan.sourceRoot}`);
console.log(` ${pc.dim("to")} ${result.plan.destinationRoot}`);
for (const pathName of result.movedPaths) {
console.log(` ${pc.dim("-")} ${pathName}`);
}
if (result.markerPath) {
console.log(` ${pc.dim("marker")} ${result.markerPath}`);
}
}
export function registerSpacesCommands(program: Command): void {
const spaces = program.command("spaces").description("Manage local Paperclip spaces");
spaces
.command("migrate-default")
.description("Offline migration from legacy root-shaped instance data into spaces/default")
.option("-i, --instance <id>", "Local instance id (default: default)")
.option("--dry-run", "Show the migration plan without moving files", false)
.option("--skip-server-check", "Skip the local /api/health preflight", false)
.action(async (opts: { instance?: string; dryRun?: boolean; skipServerCheck?: boolean }) => {
const result = await migrateDefaultSpaceInstall({
instanceId: opts.instance,
dryRun: opts.dryRun,
skipServerCheck: opts.skipServerCheck,
});
printMigrationResult(result);
});
}
+1 -15
View File
@@ -11,26 +11,19 @@ import {
resolvePaperclipHomeDir,
resolvePaperclipInstanceId,
resolvePaperclipInstanceRoot as resolveSharedPaperclipInstanceRoot,
resolvePaperclipSpaceId,
resolvePaperclipSpaceRoot as resolveSharedPaperclipSpaceRoot,
} from "@paperclipai/shared/space-paths";
} from "@paperclipai/shared/home-paths";
export {
expandHomePrefix,
resolveHomeAwarePath,
resolvePaperclipHomeDir,
resolvePaperclipInstanceId,
resolvePaperclipSpaceId,
};
export function resolvePaperclipInstanceRoot(instanceId?: string): string {
return resolveSharedPaperclipInstanceRoot({ instanceId });
}
export function resolvePaperclipSpaceRoot(instanceId?: string): string {
return resolveSharedPaperclipSpaceRoot({ instanceId });
}
export function resolveDefaultConfigPath(instanceId?: string): string {
return resolvePaperclipConfigPathForInstance({ instanceId });
}
@@ -66,17 +59,10 @@ export function resolveDefaultBackupDir(instanceId?: string): string {
export function describeLocalInstancePaths(instanceId?: string) {
const resolvedInstanceId = resolvePaperclipInstanceId(instanceId);
const instanceRoot = resolvePaperclipInstanceRoot(resolvedInstanceId);
const activeSpaceId = resolvePaperclipSpaceId({ instanceId: resolvedInstanceId });
const activeSpaceRoot = resolveSharedPaperclipSpaceRoot({
instanceId: resolvedInstanceId,
spaceId: activeSpaceId,
});
return {
homeDir: resolvePaperclipHomeDir(),
instanceId: resolvedInstanceId,
instanceRoot,
activeSpaceId,
activeSpaceRoot,
configPath: resolveDefaultConfigPath(resolvedInstanceId),
embeddedPostgresDataDir: resolveDefaultEmbeddedPostgresDir(resolvedInstanceId),
backupDir: resolveDefaultBackupDir(resolvedInstanceId),
-2
View File
@@ -25,7 +25,6 @@ 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 { registerSpacesCommands } from "./commands/spaces.js";
import { cliVersion } from "./version.js";
const program = new Command();
@@ -153,7 +152,6 @@ registerSecretCommands(program);
registerWorktreeCommands(program);
registerEnvLabCommands(program);
registerPluginCommands(program);
registerSpacesCommands(program);
const auth = program.command("auth").description("Authentication and bootstrap utilities");