@north-light/api 0.3.755 → 0.3.756
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/client.d.ts +1 -2
- package/dist/api/client.js +0 -3
- package/dist/api/contract.d.ts +1 -1
- package/dist/api/contract.js +1 -1
- package/dist/api/dto/health.d.ts +0 -54
- package/dist/api/routes.d.ts +0 -1
- package/dist/api/routes.js +0 -1
- package/dist/build-root.d.ts +0 -3
- package/dist/build-root.js +0 -9
- package/dist/commands/sys/branch.js +1 -1
- package/dist/commands/sys/daemon.js +1 -1
- package/dist/commands/sys.js +1 -2
- package/dist/core/canvas/executions.d.ts +0 -3
- package/dist/core/canvas/executions.js +1 -17
- package/dist/core/canvas/pid.d.ts +0 -10
- package/dist/core/canvas/pid.js +0 -16
- package/dist/core/directory/join.js +18 -1
- package/dist/core/grants/mirror.d.ts +0 -5
- package/dist/core/grants/mirror.js +0 -7
- package/dist/core/grants/sync.js +2 -2
- package/dist/core/human/page-schema.d.ts +1 -1
- package/dist/core/io.js +0 -5
- package/dist/core/layout.d.ts +0 -2
- package/dist/core/layout.js +2 -19
- package/dist/daemon/api/map.d.ts +1 -6
- package/dist/daemon/api/map.js +0 -16
- package/dist/daemon/api/operations.js +0 -1
- package/dist/daemon/control.d.ts +0 -8
- package/dist/daemon/manage.js +1 -1
- package/package.json +1 -1
- package/dist/commands/sys/migrate.d.ts +0 -1
- package/dist/commands/sys/migrate.js +0 -79
- package/dist/core/layout-migrate/index.d.ts +0 -38
- package/dist/core/layout-migrate/index.js +0 -523
- package/dist/core/layout-migrate/paths.d.ts +0 -30
- package/dist/core/layout-migrate/paths.js +0 -118
- package/dist/core/layout-migrate/stored-paths.d.ts +0 -9
- package/dist/core/layout-migrate/stored-paths.js +0 -424
- package/dist/core/layout-migrate/stores.d.ts +0 -30
- package/dist/core/layout-migrate/stores.js +0 -543
- package/dist/core/locked-file.d.ts +0 -27
- package/dist/core/locked-file.js +0 -121
- package/dist/core/scoped-state/db.d.ts +0 -17
- package/dist/core/scoped-state/db.js +0 -271
- package/dist/core/scoped-state/legacy-profiles.d.ts +0 -11
- package/dist/core/scoped-state/legacy-profiles.js +0 -65
- package/dist/core/scoped-state/migrate.d.ts +0 -15
- package/dist/core/scoped-state/migrate.js +0 -332
- package/dist/core/scoped-state/schema.d.ts +0 -14
- package/dist/core/scoped-state/schema.js +0 -52
- package/dist/migrations/004-canvas-documents/apply.d.ts +0 -8
- package/dist/migrations/004-canvas-documents/apply.js +0 -113
- package/dist/migrations/004-canvas-documents/index.d.ts +0 -16
- package/dist/migrations/004-canvas-documents/index.js +0 -89
- package/dist/migrations/004-canvas-documents/legacy/discover.d.ts +0 -16
- package/dist/migrations/004-canvas-documents/legacy/discover.js +0 -251
- package/dist/migrations/004-canvas-documents/legacy/history.d.ts +0 -5
- package/dist/migrations/004-canvas-documents/legacy/history.js +0 -37
- package/dist/migrations/004-canvas-documents/legacy/nested.d.ts +0 -7
- package/dist/migrations/004-canvas-documents/legacy/nested.js +0 -37
- package/dist/migrations/004-canvas-documents/legacy/parse.d.ts +0 -14
- package/dist/migrations/004-canvas-documents/legacy/parse.js +0 -24
- package/dist/migrations/004-canvas-documents/legacy/types.d.ts +0 -91
- package/dist/migrations/004-canvas-documents/legacy/types.js +0 -3
- package/dist/migrations/004-canvas-documents/plan.d.ts +0 -102
- package/dist/migrations/004-canvas-documents/plan.js +0 -410
- package/dist/migrations/004-canvas-documents/pointers.d.ts +0 -23
- package/dist/migrations/004-canvas-documents/pointers.js +0 -95
- package/dist/migrations/activation.d.ts +0 -17
- package/dist/migrations/activation.js +0 -46
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { OldToNewPathMap } from './paths.js';
|
|
2
|
-
export { OldToNewPathMap, oldToNewPathMap } from './paths.js';
|
|
3
|
-
export interface LayoutMigration {
|
|
4
|
-
map: OldToNewPathMap;
|
|
5
|
-
moved: number;
|
|
6
|
-
/** Exactly the filesystem operations that changed a source path. */
|
|
7
|
-
changes: string[];
|
|
8
|
-
/** One line per leftover the migration resolved itself (a dry run lists what it would do). */
|
|
9
|
-
resolutions: string[];
|
|
10
|
-
/** Old stored path prefix → new path, rewritten as text in migrated markdown bodies. */
|
|
11
|
-
bodyPaths: Array<[string, string]>;
|
|
12
|
-
/** Credential sources that did not exist; legitimate when the old volume never held one. */
|
|
13
|
-
absentCredentials: string[];
|
|
14
|
-
dryRun: boolean;
|
|
15
|
-
}
|
|
16
|
-
export declare function isJunkFile(name: string): boolean;
|
|
17
|
-
/** Where a leftover the new layout has no place for is kept: `runtime/set-aside/<kind>/<name>`, never overwriting. */
|
|
18
|
-
export declare function setAsidePath(migration: LayoutMigration, kind: string, name: string): string;
|
|
19
|
-
/** Move a leftover aside (a rename; never a copy). */
|
|
20
|
-
export declare function setAside(from: string, kind: string, migration: LayoutMigration, name?: string): string;
|
|
21
|
-
/**
|
|
22
|
-
* What to do with a destination that already exists before a move. A file is
|
|
23
|
-
* renamed atomically, so a file present at both paths was never written by
|
|
24
|
-
* this migration: an identical copy drops the source, an empty file is
|
|
25
|
-
* removed, and anything else is kept aside under `runtime/set-aside/replaced/`.
|
|
26
|
-
*/
|
|
27
|
-
export type Claim = {
|
|
28
|
-
action: 'merge' | 'drop-source' | 'remove-destination' | 'set-aside-destination';
|
|
29
|
-
line?: string;
|
|
30
|
-
};
|
|
31
|
-
export declare function claim(from: string, to: string, migration: LayoutMigration, wholeDirectory?: boolean): Claim;
|
|
32
|
-
/** Rename within the volume; merge an existing directory on interrupted runs, never overwrite a file. */
|
|
33
|
-
export declare function movePath(from: string, to: string, migration: LayoutMigration, wholeDirectory?: boolean): void;
|
|
34
|
-
/** Dry-run lines for a move: the claims the real run would make, without touching anything. */
|
|
35
|
-
export declare function previewMove(from: string, to: string, migration: LayoutMigration, wholeDirectory?: boolean): void;
|
|
36
|
-
export declare function sameFile(from: string, to: string): boolean;
|
|
37
|
-
export declare function daemonAnswers(socketPath: string): Promise<boolean>;
|
|
38
|
-
export declare function migrateLayout(dryRun: boolean): Promise<LayoutMigration | null>;
|
|
@@ -1,523 +0,0 @@
|
|
|
1
|
-
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
-
import { randomUUID } from 'node:crypto';
|
|
3
|
-
import { symlinkSync, linkSync, chmodSync, chownSync, closeSync, cpSync, existsSync, fsyncSync, lchownSync, lstatSync, mkdirSync, openSync, readFileSync, readlinkSync, readSync, readdirSync, renameSync, rmSync, rmdirSync, unlinkSync, writeSync } from 'node:fs';
|
|
4
|
-
import { request } from 'node:http';
|
|
5
|
-
import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
6
|
-
import { LEGACY_LOCAL_PERSON } from '../canvas/legacy-person.js';
|
|
7
|
-
import { LAYOUT_VERSION, assertSupportedLayoutMarker } from '../layout.js';
|
|
8
|
-
import { InputError } from '../io.js';
|
|
9
|
-
import { syncDirectory } from '../fs-utils.js';
|
|
10
|
-
import { oldToNewPathMap } from './paths.js';
|
|
11
|
-
import { conversations, memoryAndProfiles, previewStores, profileFolders, repairMovedRepositories, worktrees } from './stores.js';
|
|
12
|
-
import { storedPaths } from './stored-paths.js';
|
|
13
|
-
export { OldToNewPathMap, oldToNewPathMap } from './paths.js';
|
|
14
|
-
const JUNK_FILES = new Set(['.DS_Store', 'Thumbs.db', 'desktop.ini']);
|
|
15
|
-
export function isJunkFile(name) { return JUNK_FILES.has(name) || name.startsWith('._'); }
|
|
16
|
-
/** Where a leftover the new layout has no place for is kept: `runtime/set-aside/<kind>/<name>`, never overwriting. */
|
|
17
|
-
export function setAsidePath(migration, kind, name) {
|
|
18
|
-
const base = join(migration.map.old.root, 'runtime', 'set-aside', kind);
|
|
19
|
-
let path = join(base, name);
|
|
20
|
-
for (let n = 2; existsSync(path); n++)
|
|
21
|
-
path = join(base, `${name}.${n}`);
|
|
22
|
-
return path;
|
|
23
|
-
}
|
|
24
|
-
/** Move a leftover aside (a rename; never a copy). */
|
|
25
|
-
export function setAside(from, kind, migration, name = basename(from)) {
|
|
26
|
-
const to = setAsidePath(migration, kind, name);
|
|
27
|
-
mkdirSync(dirname(to), { recursive: true });
|
|
28
|
-
renameSync(from, to);
|
|
29
|
-
migration.moved++;
|
|
30
|
-
migration.changes.push(`${from} -> ${to} (set aside)`);
|
|
31
|
-
return to;
|
|
32
|
-
}
|
|
33
|
-
function empty(path) {
|
|
34
|
-
const stat = lstatSync(path);
|
|
35
|
-
return stat.isDirectory() ? readdirSync(path).length === 0 : stat.isFile() && stat.size === 0;
|
|
36
|
-
}
|
|
37
|
-
export function claim(from, to, migration, wholeDirectory = false) {
|
|
38
|
-
const a = lstatSync(from);
|
|
39
|
-
const b = lstatSync(to);
|
|
40
|
-
if (a.isDirectory() && b.isDirectory() && !wholeDirectory)
|
|
41
|
-
return { action: 'merge' };
|
|
42
|
-
if (a.isFile() && b.isFile() && sameFile(from, to))
|
|
43
|
-
return { action: 'drop-source' };
|
|
44
|
-
if (empty(to))
|
|
45
|
-
return { action: 'remove-destination', line: `stray-destination: removed empty ${b.isDirectory() ? 'folder' : 'file'} ${to} that predates the migration` };
|
|
46
|
-
const aside = setAsidePath(migration, 'replaced', relative(migration.map.old.root, to));
|
|
47
|
-
return { action: 'set-aside-destination', line: `stray-destination: ${to} predates the migration; moved it to ${aside} and put ${from} in its place` };
|
|
48
|
-
}
|
|
49
|
-
/** Rename within the volume; merge an existing directory on interrupted runs, never overwrite a file. */
|
|
50
|
-
export function movePath(from, to, migration, wholeDirectory = false) {
|
|
51
|
-
if (!existsSync(from))
|
|
52
|
-
return;
|
|
53
|
-
if (from === to)
|
|
54
|
-
return;
|
|
55
|
-
if (existsSync(to)) {
|
|
56
|
-
const decision = claim(from, to, migration, wholeDirectory);
|
|
57
|
-
if (decision.line)
|
|
58
|
-
migration.resolutions.push(decision.line);
|
|
59
|
-
if (decision.action === 'merge') {
|
|
60
|
-
for (const name of readdirSync(from))
|
|
61
|
-
movePath(join(from, name), join(to, name), migration);
|
|
62
|
-
rmdirSync(from);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (decision.action === 'drop-source') {
|
|
66
|
-
rmSync(from, { recursive: true });
|
|
67
|
-
migration.changes.push(`${from} (identical to ${to}; removed)`);
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
if (decision.action === 'remove-destination') {
|
|
71
|
-
rmSync(to, { recursive: true });
|
|
72
|
-
migration.changes.push(`${to} (removed)`);
|
|
73
|
-
}
|
|
74
|
-
else
|
|
75
|
-
setAside(to, 'replaced', migration, relative(migration.map.old.root, to));
|
|
76
|
-
}
|
|
77
|
-
mkdirSync(dirname(to), { recursive: true });
|
|
78
|
-
renameSync(from, to);
|
|
79
|
-
migration.moved++;
|
|
80
|
-
migration.changes.push(`${from} -> ${to}`);
|
|
81
|
-
}
|
|
82
|
-
/** Dry-run lines for a move: the claims the real run would make, without touching anything. */
|
|
83
|
-
export function previewMove(from, to, migration, wholeDirectory = false) {
|
|
84
|
-
if (!existsSync(from) || from === to || !existsSync(to))
|
|
85
|
-
return;
|
|
86
|
-
const decision = claim(from, to, migration, wholeDirectory);
|
|
87
|
-
if (decision.line)
|
|
88
|
-
migration.resolutions.push(decision.line);
|
|
89
|
-
if (decision.action === 'merge')
|
|
90
|
-
for (const name of readdirSync(from))
|
|
91
|
-
previewMove(join(from, name), join(to, name), migration);
|
|
92
|
-
}
|
|
93
|
-
function canvasNodeApps(migration) {
|
|
94
|
-
const { map } = migration;
|
|
95
|
-
const oldNodes = join(map.old.canvas, 'nodes');
|
|
96
|
-
const oldDb = join(map.old.canvas, 'canvas.db');
|
|
97
|
-
const newDb = join(map.old.root, 'runtime', 'canvas.db');
|
|
98
|
-
const dbPath = existsSync(oldDb) ? oldDb : existsSync(newDb) && lstatSync(newDb).size > 0 ? newDb : '';
|
|
99
|
-
if (!dbPath) {
|
|
100
|
-
if (existsSync(oldNodes))
|
|
101
|
-
throw new Error('old node records exist without canvas.db');
|
|
102
|
-
return null;
|
|
103
|
-
}
|
|
104
|
-
// The daemon is offline. Checkpoint before moving the database so committed
|
|
105
|
-
// rows in a left-behind WAL remain present when the new path opens.
|
|
106
|
-
const db = new DatabaseSync(dbPath, { readOnly: migration.dryRun || dbPath !== oldDb });
|
|
107
|
-
try {
|
|
108
|
-
if (dbPath === oldDb && !migration.dryRun) {
|
|
109
|
-
const checkpoint = db.prepare('PRAGMA wal_checkpoint(TRUNCATE)').get();
|
|
110
|
-
if (checkpoint.busy !== 0)
|
|
111
|
-
throw new Error('old canvas WAL is busy; stop every database user before migrating');
|
|
112
|
-
}
|
|
113
|
-
return new Map(db.prepare('SELECT node_id, grantee FROM nodes').all()
|
|
114
|
-
.map(row => {
|
|
115
|
-
const app = row.grantee === LEGACY_LOCAL_PERSON ? 'terminal' : row.grantee.startsWith('app:') ? row.grantee.slice(4) : '';
|
|
116
|
-
if (!app)
|
|
117
|
-
throw new Error(`node ${row.node_id} has no app assignment`);
|
|
118
|
-
return [row.node_id, app];
|
|
119
|
-
}));
|
|
120
|
-
}
|
|
121
|
-
finally {
|
|
122
|
-
db.close();
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
/** A `canvas/nodes` entry with no node row (or a row whose entry is not a folder) has no place in the new layout. */
|
|
126
|
-
function orphanNodeEntries(migration, nodes) {
|
|
127
|
-
const oldNodes = join(migration.map.old.canvas, 'nodes');
|
|
128
|
-
if (!existsSync(oldNodes))
|
|
129
|
-
return [];
|
|
130
|
-
const orphans = [];
|
|
131
|
-
for (const id of readdirSync(oldNodes)) {
|
|
132
|
-
const path = join(oldNodes, id);
|
|
133
|
-
const directory = lstatSync(path).isDirectory();
|
|
134
|
-
if (nodes.has(id) && directory)
|
|
135
|
-
continue;
|
|
136
|
-
orphans.push({ path, remove: !directory && isJunkFile(id) });
|
|
137
|
-
}
|
|
138
|
-
return orphans;
|
|
139
|
-
}
|
|
140
|
-
function orphanLine(orphan, migration) {
|
|
141
|
-
return orphan.remove
|
|
142
|
-
? `node-leftover: deleted ${orphan.path} (not a node record)`
|
|
143
|
-
: `node-leftover: ${orphan.path} has no node row; moved to ${setAsidePath(migration, 'nodes', basename(orphan.path))}`;
|
|
144
|
-
}
|
|
145
|
-
function records(migration) {
|
|
146
|
-
const { map } = migration;
|
|
147
|
-
const oldNodes = join(map.old.canvas, 'nodes');
|
|
148
|
-
const nodes = canvasNodeApps(migration);
|
|
149
|
-
if (!nodes)
|
|
150
|
-
return;
|
|
151
|
-
for (const [id, app] of nodes)
|
|
152
|
-
map.node(id, app);
|
|
153
|
-
if (!existsSync(oldNodes))
|
|
154
|
-
return;
|
|
155
|
-
for (const orphan of orphanNodeEntries(migration, nodes)) {
|
|
156
|
-
migration.resolutions.push(orphanLine(orphan, migration));
|
|
157
|
-
if (orphan.remove) {
|
|
158
|
-
unlinkSync(orphan.path);
|
|
159
|
-
migration.changes.push(`${orphan.path} (removed)`);
|
|
160
|
-
continue;
|
|
161
|
-
}
|
|
162
|
-
const aside = setAside(orphan.path, 'nodes', migration);
|
|
163
|
-
const target = join(map.old.targetRoot, relative(map.old.root, aside));
|
|
164
|
-
for (const canvas of new Set([map.old.storedCanvas, map.old.storedScope, join(map.old.storedScope, 'canvas')])) {
|
|
165
|
-
map.add(join(canvas, 'nodes', basename(orphan.path)), target);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
for (const [id, app] of nodes) {
|
|
169
|
-
const source = join(oldNodes, id);
|
|
170
|
-
if (!existsSync(source))
|
|
171
|
-
continue;
|
|
172
|
-
// R23b moves context/memory and managed worktrees after this step.
|
|
173
|
-
for (const name of ['context', 'reports', 'attachments'])
|
|
174
|
-
movePath(join(source, name), join(map.old.root, 'apps', app, 'nodes', id, name), migration);
|
|
175
|
-
for (const name of readdirSync(source)) {
|
|
176
|
-
if (['worktree', 'view.sock', 'broker-launch.json', 'system-prompt.md', 'situational-context.md', 'injected-docs.json'].includes(name))
|
|
177
|
-
continue;
|
|
178
|
-
movePath(join(source, name), join(map.old.root, 'runtime', 'nodes', id, name), migration);
|
|
179
|
-
}
|
|
180
|
-
if (readdirSync(source).length === 0)
|
|
181
|
-
rmdirSync(source);
|
|
182
|
-
}
|
|
183
|
-
if (readdirSync(oldNodes).length === 0)
|
|
184
|
-
rmdirSync(oldNodes);
|
|
185
|
-
}
|
|
186
|
-
const CREDENTIALS = ['auth.json', 'anthropic-credentials.json', 'openai-codex-credentials.json', 'openai-credentials.json', 'crouter-operations.db'];
|
|
187
|
-
function copyCredential(from, to, migration) {
|
|
188
|
-
if (!existsSync(from))
|
|
189
|
-
return;
|
|
190
|
-
if (!lstatSync(from).isFile())
|
|
191
|
-
throw new Error(`credential source is not a regular file: ${from}`);
|
|
192
|
-
const bytes = readFileSync(from);
|
|
193
|
-
if (existsSync(to)) {
|
|
194
|
-
// A newer build already wrote this credential; it is current. The old file stays for rollback.
|
|
195
|
-
if (!lstatSync(to).isFile() || !readFileSync(to).equals(bytes))
|
|
196
|
-
migration.resolutions.push(`credential: kept the existing ${to}; the older ${from} differs and stays where it is`);
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
mkdirSync(dirname(to), { recursive: true });
|
|
200
|
-
const temp = join(dirname(to), `.${randomUUID()}.tmp`);
|
|
201
|
-
try {
|
|
202
|
-
const fd = openSync(temp, 'wx', 0o600);
|
|
203
|
-
try {
|
|
204
|
-
for (let offset = 0; offset < bytes.length;)
|
|
205
|
-
offset += writeSync(fd, bytes, offset, bytes.length - offset);
|
|
206
|
-
fsyncSync(fd);
|
|
207
|
-
}
|
|
208
|
-
finally {
|
|
209
|
-
closeSync(fd);
|
|
210
|
-
}
|
|
211
|
-
renameSync(temp, to);
|
|
212
|
-
if (!readFileSync(to).equals(bytes))
|
|
213
|
-
throw new Error(`credential verification failed: ${to}`);
|
|
214
|
-
}
|
|
215
|
-
finally {
|
|
216
|
-
if (existsSync(temp))
|
|
217
|
-
unlinkSync(temp);
|
|
218
|
-
}
|
|
219
|
-
migration.moved++;
|
|
220
|
-
migration.changes.push(`${from} -> ${to} (source retained)`);
|
|
221
|
-
}
|
|
222
|
-
function checkpointDb(path) {
|
|
223
|
-
if (!existsSync(path))
|
|
224
|
-
return;
|
|
225
|
-
const db = new DatabaseSync(path);
|
|
226
|
-
try {
|
|
227
|
-
const checkpoint = db.prepare('PRAGMA wal_checkpoint(TRUNCATE)').get();
|
|
228
|
-
if (checkpoint.busy !== 0)
|
|
229
|
-
throw new Error(`SQLite WAL is busy: ${path}`);
|
|
230
|
-
}
|
|
231
|
-
finally {
|
|
232
|
-
db.close();
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
export function sameFile(from, to) {
|
|
236
|
-
if (lstatSync(from).size !== lstatSync(to).size)
|
|
237
|
-
return false;
|
|
238
|
-
const a = openSync(from, 'r');
|
|
239
|
-
const b = openSync(to, 'r');
|
|
240
|
-
const first = Buffer.allocUnsafe(64 * 1024);
|
|
241
|
-
const second = Buffer.allocUnsafe(first.length);
|
|
242
|
-
try {
|
|
243
|
-
for (;;) {
|
|
244
|
-
const size = readSync(a, first, 0, first.length, null);
|
|
245
|
-
if (size === 0)
|
|
246
|
-
return true;
|
|
247
|
-
let read = 0;
|
|
248
|
-
while (read < size) {
|
|
249
|
-
const next = readSync(b, second, read, size - read, null);
|
|
250
|
-
if (next === 0)
|
|
251
|
-
return false;
|
|
252
|
-
read += next;
|
|
253
|
-
}
|
|
254
|
-
if (!first.subarray(0, size).equals(second.subarray(0, size)))
|
|
255
|
-
return false;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
finally {
|
|
259
|
-
closeSync(a);
|
|
260
|
-
closeSync(b);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
function sameTree(from, to, skipGitRecords = false) {
|
|
264
|
-
let b;
|
|
265
|
-
try {
|
|
266
|
-
b = lstatSync(to);
|
|
267
|
-
}
|
|
268
|
-
catch (error) {
|
|
269
|
-
if (error.code === 'ENOENT')
|
|
270
|
-
return false;
|
|
271
|
-
throw error;
|
|
272
|
-
}
|
|
273
|
-
const a = lstatSync(from);
|
|
274
|
-
if (a.isSymbolicLink())
|
|
275
|
-
return b.isSymbolicLink() && readlinkSync(from) === readlinkSync(to);
|
|
276
|
-
if (a.isFile())
|
|
277
|
-
return b.isFile() && sameFile(from, to);
|
|
278
|
-
if (!a.isDirectory() || !b.isDirectory())
|
|
279
|
-
return false;
|
|
280
|
-
const names = readdirSync(from).filter(name => !skipGitRecords || name !== '.git');
|
|
281
|
-
const other = readdirSync(to).filter(name => !skipGitRecords || name !== '.git');
|
|
282
|
-
const entries = new Set(other);
|
|
283
|
-
return names.length === other.length && names.every(name => entries.has(name) && sameTree(join(from, name), join(to, name), skipGitRecords));
|
|
284
|
-
}
|
|
285
|
-
/** The old hosted work directory is outside /volume; preserve its original
|
|
286
|
-
* container filesystem when copying it onto the persistent runtime volume. */
|
|
287
|
-
function copyHostedWork(from, to, migration) {
|
|
288
|
-
if (!existsSync(from))
|
|
289
|
-
return;
|
|
290
|
-
if (!lstatSync(from).isDirectory())
|
|
291
|
-
throw new Error(`hosted work is not a directory: ${from}`);
|
|
292
|
-
if (existsSync(to)) {
|
|
293
|
-
// Git repair changes the copied repository's .git records after the copy.
|
|
294
|
-
if (sameTree(from, to, true))
|
|
295
|
-
return;
|
|
296
|
-
if (!lstatSync(to).isDirectory() || readdirSync(to).length !== 0) {
|
|
297
|
-
// The copy keeps its source, so the old work tree stays for rollback.
|
|
298
|
-
migration.resolutions.push(`hosted-work: kept the existing ${to}; the old ${from} differs and stays where it is`);
|
|
299
|
-
return;
|
|
300
|
-
}
|
|
301
|
-
rmdirSync(to);
|
|
302
|
-
}
|
|
303
|
-
mkdirSync(dirname(to), { recursive: true });
|
|
304
|
-
const temp = join(dirname(to), `.work-${randomUUID()}.migrating`);
|
|
305
|
-
try {
|
|
306
|
-
cpSync(from, temp, { recursive: true, verbatimSymlinks: true, preserveTimestamps: true });
|
|
307
|
-
if (!sameTree(from, temp))
|
|
308
|
-
throw new Error(`hosted work copy verification failed: ${from}`);
|
|
309
|
-
renameSync(temp, to);
|
|
310
|
-
migration.moved++;
|
|
311
|
-
migration.changes.push(`${from} -> ${to} (source retained)`);
|
|
312
|
-
}
|
|
313
|
-
finally {
|
|
314
|
-
if (existsSync(temp))
|
|
315
|
-
rmSync(temp, { recursive: true, force: true });
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
function topLevelMoves(migration) {
|
|
319
|
-
const { old } = migration.map;
|
|
320
|
-
const runtime = join(old.root, 'runtime');
|
|
321
|
-
const scope = join(runtime, 'scope');
|
|
322
|
-
const moves = [];
|
|
323
|
-
// on-disk-migrations.json records the legacy store migrations that already ran; the canvas import reads it from runtime/.
|
|
324
|
-
for (const name of ['canvas.db', 'canvas.db-wal', 'canvas.db-shm', 'on-disk-migrations.json', 'requests', 'reviews', 'plugins', 'marketplaces', 'crtrd.log', 'crtrd.err', 'cache'])
|
|
325
|
-
moves.push([join(old.canvas, name), join(runtime, name)]);
|
|
326
|
-
// The old canvas and user scope can be the same folder. Never rename that folder wholesale.
|
|
327
|
-
for (const name of ['prompts', 'personas', 'config.json', 'profile-defaults.json', 'operations.db', 'operations.db-wal', 'operations.db-shm'])
|
|
328
|
-
moves.push([join(old.scope, name), join(scope, name)]);
|
|
329
|
-
for (const name of ['support', 'prompt-reviews', 'plugins', 'marketplaces'])
|
|
330
|
-
moves.push([join(old.scope, name), join(runtime, name)]);
|
|
331
|
-
moves.push([join(old.canvas, 'secrets'), join(runtime, 'secrets')]);
|
|
332
|
-
return moves;
|
|
333
|
-
}
|
|
334
|
-
/** A top-level entry is renamed whole, so an existing destination was made by
|
|
335
|
-
* a newer build started before the migration (e.g. an empty `runtime/canvas.db`). */
|
|
336
|
-
function topLevel(migration) {
|
|
337
|
-
const { old } = migration.map;
|
|
338
|
-
const runtime = join(old.root, 'runtime');
|
|
339
|
-
checkpointDb(join(old.scope, 'operations.db'));
|
|
340
|
-
for (const [from, to] of topLevelMoves(migration)) {
|
|
341
|
-
const links = relativeLinks(from);
|
|
342
|
-
movePath(from, to, migration, true);
|
|
343
|
-
retargetLinks(links, from, to, migration);
|
|
344
|
-
}
|
|
345
|
-
// Old provider credentials are on a separate mount. Keep them for rollback.
|
|
346
|
-
checkpointDb(join(old.agent, 'crouter-operations.db'));
|
|
347
|
-
for (const name of CREDENTIALS)
|
|
348
|
-
copyCredential(join(old.agent, name), join(runtime, 'secrets', name), migration);
|
|
349
|
-
if (process.platform === 'linux')
|
|
350
|
-
copyHostedWork(old.work, join(old.root, 'user', 'files'), migration);
|
|
351
|
-
}
|
|
352
|
-
/** The relative symlinks directly inside a folder, e.g. `plugins/<name>` installed as `../../Code/...`. */
|
|
353
|
-
function relativeLinks(dir) {
|
|
354
|
-
if (!existsSync(dir) || !lstatSync(dir).isDirectory())
|
|
355
|
-
return [];
|
|
356
|
-
const out = [];
|
|
357
|
-
for (const name of readdirSync(dir)) {
|
|
358
|
-
const path = join(dir, name);
|
|
359
|
-
if (!lstatSync(path).isSymbolicLink())
|
|
360
|
-
continue;
|
|
361
|
-
const target = readlinkSync(path);
|
|
362
|
-
if (!isAbsolute(target))
|
|
363
|
-
out.push({ name, target });
|
|
364
|
-
}
|
|
365
|
-
return out;
|
|
366
|
-
}
|
|
367
|
-
/** A folder moved to another depth breaks its relative links: point each one at what it named before the move. */
|
|
368
|
-
function retargetLinks(links, from, to, migration) {
|
|
369
|
-
if (links.length === 0 || !existsSync(to))
|
|
370
|
-
return;
|
|
371
|
-
for (const { name, target } of links) {
|
|
372
|
-
const link = join(to, name);
|
|
373
|
-
if (!lstatExists(link))
|
|
374
|
-
continue;
|
|
375
|
-
const named = migration.map.mapPath(resolve(from, target));
|
|
376
|
-
const next = relative(to, named);
|
|
377
|
-
if (next === target)
|
|
378
|
-
continue;
|
|
379
|
-
unlinkSync(link);
|
|
380
|
-
symlinkSync(next, link);
|
|
381
|
-
migration.changes.push(`${link}: link ${target} -> ${next}`);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
function lstatExists(path) {
|
|
385
|
-
try {
|
|
386
|
-
lstatSync(path);
|
|
387
|
-
return true;
|
|
388
|
-
}
|
|
389
|
-
catch {
|
|
390
|
-
return false;
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
function ownership(root) {
|
|
394
|
-
if (process.platform !== 'linux')
|
|
395
|
-
return;
|
|
396
|
-
if (process.getuid?.() !== 0)
|
|
397
|
-
throw new Error('Linux layout migration requires root for the ownership pass');
|
|
398
|
-
const passwd = readFileSync('/etc/passwd', 'utf8').split('\n').find(line => line.startsWith('crtrd:'));
|
|
399
|
-
const group = readFileSync('/etc/group', 'utf8').split('\n').find(line => line.startsWith('crtr-data:'));
|
|
400
|
-
if (!passwd || !group)
|
|
401
|
-
throw new Error('layout migration requires crtrd user and crtr-data group');
|
|
402
|
-
const uid = Number(passwd.split(':')[2]);
|
|
403
|
-
const gid = Number(group.split(':')[2]);
|
|
404
|
-
if (!Number.isInteger(uid) || !Number.isInteger(gid))
|
|
405
|
-
throw new Error('invalid migration user or group');
|
|
406
|
-
const walk = (path, data) => {
|
|
407
|
-
if (!existsSync(path))
|
|
408
|
-
return;
|
|
409
|
-
const stat = lstatSync(path);
|
|
410
|
-
if (stat.isSymbolicLink()) {
|
|
411
|
-
lchownSync(path, uid, data ? gid : stat.gid);
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
chownSync(path, uid, data ? gid : stat.gid);
|
|
415
|
-
if (stat.isDirectory()) {
|
|
416
|
-
chmodSync(path, data ? 0o2770 : 0o700);
|
|
417
|
-
for (const name of readdirSync(path))
|
|
418
|
-
walk(join(path, name), data);
|
|
419
|
-
}
|
|
420
|
-
else if (data)
|
|
421
|
-
chmodSync(path, (stat.mode & 0o111 ? 0o770 : 0o660));
|
|
422
|
-
};
|
|
423
|
-
walk(join(root, 'runtime'), false);
|
|
424
|
-
walk(join(root, 'user'), true);
|
|
425
|
-
walk(join(root, 'apps'), true);
|
|
426
|
-
}
|
|
427
|
-
export function daemonAnswers(socketPath) {
|
|
428
|
-
return new Promise(resolve => {
|
|
429
|
-
const req = request({ socketPath, path: '/healthz', method: 'GET', timeout: 1000 }, res => {
|
|
430
|
-
res.resume();
|
|
431
|
-
resolve(true); // A blocked daemon still answers and owns its files.
|
|
432
|
-
});
|
|
433
|
-
req.on('timeout', () => req.destroy(new Error('daemon probe timed out')));
|
|
434
|
-
req.on('error', error => {
|
|
435
|
-
if (error.code === 'ENOENT' || error.code === 'ECONNREFUSED')
|
|
436
|
-
resolve(false);
|
|
437
|
-
else
|
|
438
|
-
resolve(true); // Uncertain probe: do not mutate an owned volume.
|
|
439
|
-
});
|
|
440
|
-
req.end();
|
|
441
|
-
});
|
|
442
|
-
}
|
|
443
|
-
export async function migrateLayout(dryRun) {
|
|
444
|
-
const map = oldToNewPathMap();
|
|
445
|
-
const old = map.old;
|
|
446
|
-
const version = join(old.root, 'runtime', 'layout');
|
|
447
|
-
if (existsSync(version)) {
|
|
448
|
-
assertSupportedLayoutMarker(version);
|
|
449
|
-
return null;
|
|
450
|
-
}
|
|
451
|
-
// Inspect both the old and current socket; do not autostart a daemon.
|
|
452
|
-
const sockets = new Set([join(old.canvas, 'crtrd.sock'), join(old.scope, 'crtrd.sock'),
|
|
453
|
-
process.platform === 'linux' ? '/run/crtr/api.sock' : join(old.root, 'runtime', 'api.sock')]);
|
|
454
|
-
for (const socket of sockets)
|
|
455
|
-
if (await daemonAnswers(socket))
|
|
456
|
-
throw new InputError({
|
|
457
|
-
error: 'daemon_running',
|
|
458
|
-
message: `cannot migrate while a daemon answers on ${socket}`,
|
|
459
|
-
next: 'Stop the daemon that owns this volume, then run `crtr sys migrate` again.',
|
|
460
|
-
});
|
|
461
|
-
if (process.platform === 'linux' && !existsSync(old.agent))
|
|
462
|
-
throw new InputError({
|
|
463
|
-
error: 'credential_volume_missing',
|
|
464
|
-
message: `the old credential volume is not mounted at ${old.agent}`,
|
|
465
|
-
next: `Mount the old provider-auth volume at ${old.agent}, then run \`crtr sys migrate\` again.`,
|
|
466
|
-
});
|
|
467
|
-
const absentCredentials = CREDENTIALS.map(name => join(old.agent, name)).filter(path => !existsSync(path));
|
|
468
|
-
const result = { map, moved: 0, changes: [], resolutions: [], bodyPaths: [], absentCredentials, dryRun };
|
|
469
|
-
if (dryRun) {
|
|
470
|
-
// The same decisions the real run makes, computed read-only.
|
|
471
|
-
const nodes = canvasNodeApps(result);
|
|
472
|
-
if (nodes)
|
|
473
|
-
for (const orphan of orphanNodeEntries(result, nodes))
|
|
474
|
-
result.resolutions.push(orphanLine(orphan, result));
|
|
475
|
-
for (const [from, to] of topLevelMoves(result))
|
|
476
|
-
previewMove(from, to, result, true);
|
|
477
|
-
previewStores(result);
|
|
478
|
-
return result;
|
|
479
|
-
}
|
|
480
|
-
// Register every profile path before any step maps a stored path.
|
|
481
|
-
profileFolders(result);
|
|
482
|
-
// Move worktrees before the hosted repository moves so git can repair its metadata.
|
|
483
|
-
const steps = [records, worktrees, topLevel, repairMovedRepositories, memoryAndProfiles, conversations, storedPaths];
|
|
484
|
-
for (const step of steps)
|
|
485
|
-
step(result);
|
|
486
|
-
// Every old canvas entry has moved; an empty old canvas folder would name nothing.
|
|
487
|
-
if (old.canvas !== old.root && existsSync(old.canvas) && readdirSync(old.canvas).length === 0)
|
|
488
|
-
rmdirSync(old.canvas);
|
|
489
|
-
ownership(old.root);
|
|
490
|
-
// The version is the commit marker: no daemon accepts the tree until every
|
|
491
|
-
// move, stored-path rewrite and permission change has completed.
|
|
492
|
-
mkdirSync(dirname(version), { recursive: true });
|
|
493
|
-
const temporary = `${version}.${randomUUID()}.tmp`;
|
|
494
|
-
const fd = openSync(temporary, 'wx', 0o600);
|
|
495
|
-
try {
|
|
496
|
-
try {
|
|
497
|
-
if (process.platform === 'linux') {
|
|
498
|
-
const stat = lstatSync(dirname(version));
|
|
499
|
-
chownSync(temporary, stat.uid, stat.gid);
|
|
500
|
-
}
|
|
501
|
-
writeSync(fd, `${LAYOUT_VERSION}\n`);
|
|
502
|
-
fsyncSync(fd);
|
|
503
|
-
}
|
|
504
|
-
finally {
|
|
505
|
-
closeSync(fd);
|
|
506
|
-
}
|
|
507
|
-
// Link, not rename: a marker that appeared since the check above is refused, never replaced.
|
|
508
|
-
try {
|
|
509
|
-
linkSync(temporary, version);
|
|
510
|
-
}
|
|
511
|
-
catch (error) {
|
|
512
|
-
if (error.code === 'EEXIST')
|
|
513
|
-
assertSupportedLayoutMarker(version);
|
|
514
|
-
else
|
|
515
|
-
throw error;
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
finally {
|
|
519
|
-
unlinkSync(temporary);
|
|
520
|
-
}
|
|
521
|
-
syncDirectory(dirname(version));
|
|
522
|
-
return result;
|
|
523
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export interface OldLayout {
|
|
2
|
-
root: string;
|
|
3
|
-
targetRoot: string;
|
|
4
|
-
canvas: string;
|
|
5
|
-
scope: string;
|
|
6
|
-
agent: string;
|
|
7
|
-
storedCanvas: string;
|
|
8
|
-
storedScope: string;
|
|
9
|
-
storedAgent: string;
|
|
10
|
-
work: string;
|
|
11
|
-
}
|
|
12
|
-
/** Sources are physical paths; stored prefixes are paths seen by the old daemon. */
|
|
13
|
-
export declare function oldLayout(): OldLayout;
|
|
14
|
-
export declare class OldToNewPathMap {
|
|
15
|
-
readonly old: OldLayout;
|
|
16
|
-
private readonly entries;
|
|
17
|
-
private readonly retired;
|
|
18
|
-
/** Stored paths inside a removed folder with no new location; left unchanged. */
|
|
19
|
-
readonly dangling: Set<string>;
|
|
20
|
-
constructor(old: OldLayout);
|
|
21
|
-
add(from: string, to: string): void;
|
|
22
|
-
/** A path whose planned destination changed: a worktree folder set aside instead of moving into its node. */
|
|
23
|
-
redirect(from: string, to: string): void;
|
|
24
|
-
/** A removed folder: the folder itself maps to `to`, and a stored path inside it with no finer entry is refused. */
|
|
25
|
-
retire(from: string, to: string): void;
|
|
26
|
-
node(nodeId: string, app: string): void;
|
|
27
|
-
/** Rewrites only approved old prefixes; later steps register profile and session paths. */
|
|
28
|
-
mapPath(path: string): string;
|
|
29
|
-
}
|
|
30
|
-
export declare function oldToNewPathMap(): OldToNewPathMap;
|