@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,118 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs';
|
|
2
|
-
import { homedir } from 'node:os';
|
|
3
|
-
import { isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
4
|
-
import { layoutRoot } from '../layout.js';
|
|
5
|
-
/** Sources are physical paths; stored prefixes are paths seen by the old daemon. */
|
|
6
|
-
export function oldLayout() {
|
|
7
|
-
const targetRoot = layoutRoot();
|
|
8
|
-
const hosted = process.platform === 'linux';
|
|
9
|
-
const root = hosted && targetRoot === '/' ? '/volume' : targetRoot;
|
|
10
|
-
const storedScope = hosted ? '/home/agent/.crouter' : root;
|
|
11
|
-
const canvas = existsSync(join(root, 'canvas')) ? join(root, 'canvas') : root;
|
|
12
|
-
// In the container the old credential volume is mounted at its old path;
|
|
13
|
-
// PI_CODING_AGENT_DIR there names the new location under /runtime.
|
|
14
|
-
const agent = hosted ? '/home/agent/.pi/agent' : resolve(process.env['PI_CODING_AGENT_DIR'] || join(homedir(), '.pi', 'agent'));
|
|
15
|
-
return {
|
|
16
|
-
root,
|
|
17
|
-
targetRoot,
|
|
18
|
-
canvas,
|
|
19
|
-
scope: root,
|
|
20
|
-
agent,
|
|
21
|
-
storedCanvas: canvas === root ? storedScope : join(storedScope, 'canvas'),
|
|
22
|
-
storedScope,
|
|
23
|
-
storedAgent: agent,
|
|
24
|
-
work: '/home/crtr/work',
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
export class OldToNewPathMap {
|
|
28
|
-
old;
|
|
29
|
-
entries = new Map();
|
|
30
|
-
retired = new Map();
|
|
31
|
-
/** Stored paths inside a removed folder with no new location; left unchanged. */
|
|
32
|
-
dangling = new Set();
|
|
33
|
-
constructor(old) {
|
|
34
|
-
this.old = old;
|
|
35
|
-
const runtime = join(old.targetRoot, 'runtime');
|
|
36
|
-
const scope = join(runtime, 'scope');
|
|
37
|
-
for (const canvas of new Set([old.storedCanvas, old.storedScope, join(old.storedScope, 'canvas')])) {
|
|
38
|
-
for (const name of ['canvas.db', 'canvas.db-wal', 'canvas.db-shm', 'requests', 'reviews', 'plugins', 'marketplaces', 'secrets', 'crtrd.log', 'crtrd.err', 'cache']) {
|
|
39
|
-
this.add(join(canvas, name), join(runtime, name));
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
for (const name of ['prompts', 'personas', 'config.json', 'profile-defaults.json', 'operations.db', 'operations.db-wal', 'operations.db-shm']) {
|
|
43
|
-
this.add(join(old.storedScope, name), join(scope, name));
|
|
44
|
-
}
|
|
45
|
-
for (const name of ['support', 'prompt-reviews', 'plugins', 'marketplaces'])
|
|
46
|
-
this.add(join(old.storedScope, name), join(runtime, name));
|
|
47
|
-
this.add(join(old.storedScope, 'memory'), join(old.targetRoot, 'user', 'memory'));
|
|
48
|
-
this.add(old.storedScope, scope);
|
|
49
|
-
this.add(old.work, join(old.targetRoot, 'user', 'files'));
|
|
50
|
-
for (const name of ['auth.json', 'anthropic-credentials.json', 'openai-codex-credentials.json', 'openai-credentials.json', 'crouter-operations.db']) {
|
|
51
|
-
this.add(join(old.storedAgent, name), join(runtime, 'secrets', name));
|
|
52
|
-
}
|
|
53
|
-
for (const canvas of new Set([old.storedCanvas, old.storedScope, join(old.storedScope, 'canvas')])) {
|
|
54
|
-
this.add(join(canvas, 'secrets', 'runtime-client.json'), join(runtime, 'secrets', 'runtime-client.json'));
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
add(from, to) {
|
|
58
|
-
if (!isAbsolute(from) || !isAbsolute(to))
|
|
59
|
-
throw new Error('migration paths must be absolute');
|
|
60
|
-
const previous = this.entries.get(from);
|
|
61
|
-
if (previous && previous !== to)
|
|
62
|
-
throw new Error(`conflicting migration path: ${from}`);
|
|
63
|
-
this.entries.set(from, to);
|
|
64
|
-
}
|
|
65
|
-
/** A path whose planned destination changed: a worktree folder set aside instead of moving into its node. */
|
|
66
|
-
redirect(from, to) {
|
|
67
|
-
if (!isAbsolute(from) || !isAbsolute(to))
|
|
68
|
-
throw new Error('migration paths must be absolute');
|
|
69
|
-
this.entries.set(from, to);
|
|
70
|
-
}
|
|
71
|
-
/** A removed folder: the folder itself maps to `to`, and a stored path inside it with no finer entry is refused. */
|
|
72
|
-
retire(from, to) {
|
|
73
|
-
if (!isAbsolute(from) || !isAbsolute(to))
|
|
74
|
-
throw new Error('migration paths must be absolute');
|
|
75
|
-
const previous = this.retired.get(from);
|
|
76
|
-
if (previous && previous !== to)
|
|
77
|
-
throw new Error(`conflicting migration path: ${from}`);
|
|
78
|
-
this.retired.set(from, to);
|
|
79
|
-
}
|
|
80
|
-
node(nodeId, app) {
|
|
81
|
-
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(nodeId) || !/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(app))
|
|
82
|
-
throw new Error('unsafe migration node or app id');
|
|
83
|
-
const records = join(this.old.targetRoot, 'runtime', 'nodes', nodeId);
|
|
84
|
-
const agent = join(this.old.targetRoot, 'apps', app, 'nodes', nodeId);
|
|
85
|
-
// Once the old canvas directory is empty it disappears; both original
|
|
86
|
-
// shapes must remain addressable on an interrupted migration's rerun.
|
|
87
|
-
for (const canvas of new Set([this.old.storedCanvas, this.old.storedScope, join(this.old.storedScope, 'canvas')])) {
|
|
88
|
-
const from = join(canvas, 'nodes', nodeId);
|
|
89
|
-
for (const name of ['job', 'messages', 'meta.json', 'inbox.jsonl', 'passive.jsonl', 'transcript.jsonl', 'session.ptr'])
|
|
90
|
-
this.add(join(from, name), join(records, name));
|
|
91
|
-
for (const name of ['context', 'reports', 'attachments', 'worktree'])
|
|
92
|
-
this.add(join(from, name), join(agent, name));
|
|
93
|
-
this.add(join(from, 'context', 'memory'), join(this.old.targetRoot, 'apps', app, 'memory', 'nodes', nodeId));
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
/** Rewrites only approved old prefixes; later steps register profile and session paths. */
|
|
97
|
-
mapPath(path) {
|
|
98
|
-
if (!isAbsolute(path))
|
|
99
|
-
return path;
|
|
100
|
-
const exact = this.retired.get(path);
|
|
101
|
-
if (exact)
|
|
102
|
-
return exact;
|
|
103
|
-
let source = '';
|
|
104
|
-
for (const key of this.entries.keys()) {
|
|
105
|
-
if ((path === key || path.startsWith(key + sep)) && key.length > source.length)
|
|
106
|
-
source = key;
|
|
107
|
-
}
|
|
108
|
-
for (const key of this.retired.keys()) {
|
|
109
|
-
// A file the new layout drops (a profile manifest or lock): the stored path stays, naming nothing.
|
|
110
|
-
if (path.startsWith(key + sep) && key.length > source.length) {
|
|
111
|
-
this.dangling.add(path);
|
|
112
|
-
return path;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return source ? resolve(this.entries.get(source), relative(source, path)) : path;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
export function oldToNewPathMap() { return new OldToNewPathMap(oldLayout()); }
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { LayoutMigration } from './index.js';
|
|
2
|
-
export declare function storedPaths(migration: LayoutMigration): void;
|
|
3
|
-
/**
|
|
4
|
-
* Markdown links a moved profile folder by its old absolute path (or its `~/`
|
|
5
|
-
* spelling); point each at the new location. Scans the documents the canvas
|
|
6
|
-
* import reads: user and app memory, profile files, and node context. A dry
|
|
7
|
-
* run scans the same documents at their old locations and writes nothing.
|
|
8
|
-
*/
|
|
9
|
-
export declare function rewriteBodyPaths(migration: LayoutMigration): void;
|
|
@@ -1,424 +0,0 @@
|
|
|
1
|
-
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
-
import { closeSync, existsSync, fsyncSync, lstatSync, openSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
3
|
-
import { basename, dirname, join, relative, resolve, sep } from 'node:path';
|
|
4
|
-
import { homedir } from 'node:os';
|
|
5
|
-
function mappedJson(value, migration) {
|
|
6
|
-
if (typeof value === 'string')
|
|
7
|
-
return migration.map.mapPath(value);
|
|
8
|
-
if (Array.isArray(value))
|
|
9
|
-
return value.map(item => mappedJson(item, migration));
|
|
10
|
-
if (value !== null && typeof value === 'object')
|
|
11
|
-
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, mappedJson(item, migration)]));
|
|
12
|
-
return value;
|
|
13
|
-
}
|
|
14
|
-
function rewriteJson(text, migration) {
|
|
15
|
-
const parsed = JSON.parse(text);
|
|
16
|
-
const mapped = mappedJson(parsed, migration);
|
|
17
|
-
return JSON.stringify(mapped) === JSON.stringify(parsed) ? text : JSON.stringify(mapped);
|
|
18
|
-
}
|
|
19
|
-
function tableExists(db, table) {
|
|
20
|
-
return db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(table) !== undefined;
|
|
21
|
-
}
|
|
22
|
-
function rewriteDb(path, columns, migration) {
|
|
23
|
-
if (!existsSync(path))
|
|
24
|
-
return;
|
|
25
|
-
const db = new DatabaseSync(path);
|
|
26
|
-
try {
|
|
27
|
-
db.exec('BEGIN IMMEDIATE');
|
|
28
|
-
try {
|
|
29
|
-
for (const [table, selected] of Object.entries(columns)) {
|
|
30
|
-
if (!tableExists(db, table))
|
|
31
|
-
continue;
|
|
32
|
-
const available = new Set(db.prepare(`PRAGMA table_info("${table}")`).all().map(row => row.name));
|
|
33
|
-
const fields = [...(selected.paths ?? []).filter(field => available.has(field)), ...(selected.json ?? []).filter(field => available.has(field))];
|
|
34
|
-
if (fields.length === 0)
|
|
35
|
-
continue;
|
|
36
|
-
const rows = db.prepare(`SELECT rowid AS migration_rowid, ${fields.map(field => `"${field}"`).join(', ')} FROM "${table}"`).all();
|
|
37
|
-
for (const row of rows) {
|
|
38
|
-
const updates = [];
|
|
39
|
-
for (const field of fields) {
|
|
40
|
-
const original = row[field];
|
|
41
|
-
if (typeof original !== 'string')
|
|
42
|
-
continue;
|
|
43
|
-
const mapped = (selected.json ?? []).includes(field) ? rewriteJson(original, migration) : migration.map.mapPath(original);
|
|
44
|
-
if (mapped !== original)
|
|
45
|
-
updates.push([field, mapped]);
|
|
46
|
-
}
|
|
47
|
-
if (updates.length) {
|
|
48
|
-
db.prepare(`UPDATE "${table}" SET ${updates.map(([field]) => `"${field}" = ?`).join(', ')} WHERE rowid = ?`)
|
|
49
|
-
.run(...updates.map(([, value]) => value), row.migration_rowid);
|
|
50
|
-
migration.moved += updates.length;
|
|
51
|
-
migration.changes.push(`${path}:${table}:${String(row.migration_rowid)} (${updates.map(([field]) => field).join(', ')})`);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
db.exec('COMMIT');
|
|
56
|
-
}
|
|
57
|
-
catch (error) {
|
|
58
|
-
db.exec('ROLLBACK');
|
|
59
|
-
throw error;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
finally {
|
|
63
|
-
db.close();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
const canvasColumns = {
|
|
67
|
-
nodes: { paths: ['cwd', 'fork_source_file', 'pi_session_file'], json: ['launch_config_json', 'review_binding_json', 'metadata_json', 'outcome_detail_json'] },
|
|
68
|
-
sessions: { paths: ['transcript_path', 'fork_source_path'] },
|
|
69
|
-
worktrees: { paths: ['path', 'repo_root'], json: ['sweep_observed_json', 'abandoned_json', 'land_intent_json', 'disposal_json'] },
|
|
70
|
-
crons: { paths: ['cwd'], json: ['env_json', 'sink'] },
|
|
71
|
-
human_action_deliveries: { paths: ['cwd'], json: ['argv_json', 'completion_json', 'last_failure_json'] },
|
|
72
|
-
deliveries: { json: ['adapter_json'] },
|
|
73
|
-
reviews: { paths: ['origin_session_file', 'review_dir', 'branch_file', 'output_path', 'file', 'presentation_dir'], json: ['result_json', 'specification_json'] },
|
|
74
|
-
human_requests: { paths: ['request_dir', 'file', 'presentation_dir'], json: ['specification_json', 'action_json', 'progress_json', 'feedback_json', 'result_json'] },
|
|
75
|
-
run_events: { json: ['body_json'] },
|
|
76
|
-
webhook_deliveries: { json: ['payload_json', 'document_json', 'last_failure_json', 'argv_json'] },
|
|
77
|
-
};
|
|
78
|
-
const scopeColumns = {
|
|
79
|
-
scope_owners: { paths: ['root'] },
|
|
80
|
-
profiles: { paths: ['home'] },
|
|
81
|
-
profile_projects: { paths: ['path'] },
|
|
82
|
-
profile_defaults: { paths: ['directory'] },
|
|
83
|
-
scope_bookkeeping: { paths: ['active_canvas'] },
|
|
84
|
-
scope_settings: { json: ['raw_json'] },
|
|
85
|
-
};
|
|
86
|
-
function rewriteFile(path, migration, transform) {
|
|
87
|
-
if (!existsSync(path))
|
|
88
|
-
return;
|
|
89
|
-
const original = readFileSync(path, 'utf8');
|
|
90
|
-
const updated = transform(original);
|
|
91
|
-
if (updated === original)
|
|
92
|
-
return;
|
|
93
|
-
const stat = lstatSync(path);
|
|
94
|
-
if (!stat.isFile())
|
|
95
|
-
throw new Error(`migration path is not a regular file: ${path}`);
|
|
96
|
-
const temporary = `${path}.migration-tmp`;
|
|
97
|
-
if (existsSync(temporary))
|
|
98
|
-
throw new Error(`migration temporary file already exists: ${temporary}`);
|
|
99
|
-
try {
|
|
100
|
-
writeFileSync(temporary, updated, { flag: 'wx', mode: stat.mode });
|
|
101
|
-
const fd = openSync(temporary, 'r');
|
|
102
|
-
try {
|
|
103
|
-
fsyncSync(fd);
|
|
104
|
-
}
|
|
105
|
-
finally {
|
|
106
|
-
closeSync(fd);
|
|
107
|
-
}
|
|
108
|
-
renameSync(temporary, path);
|
|
109
|
-
}
|
|
110
|
-
finally {
|
|
111
|
-
if (existsSync(temporary))
|
|
112
|
-
unlinkSync(temporary);
|
|
113
|
-
}
|
|
114
|
-
migration.moved++;
|
|
115
|
-
migration.changes.push(`${path} (stored paths)`);
|
|
116
|
-
}
|
|
117
|
-
function walk(root, visit) {
|
|
118
|
-
if (!existsSync(root))
|
|
119
|
-
return;
|
|
120
|
-
for (const entry of readdirSync(root, { withFileTypes: true })) {
|
|
121
|
-
const path = join(root, entry.name);
|
|
122
|
-
if (entry.isDirectory())
|
|
123
|
-
walk(path, visit);
|
|
124
|
-
else if (entry.isFile())
|
|
125
|
-
visit(path);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
function recoverSessionMappings(migration, apps, runtime) {
|
|
129
|
-
const { map } = migration;
|
|
130
|
-
const { old } = map;
|
|
131
|
-
const destinations = new Map();
|
|
132
|
-
const ambiguous = new Set();
|
|
133
|
-
const unrecovered = new Set();
|
|
134
|
-
walk(apps, path => {
|
|
135
|
-
if (!path.includes('/conversations/') || !path.endsWith('.jsonl'))
|
|
136
|
-
return;
|
|
137
|
-
const name = basename(path);
|
|
138
|
-
if (destinations.has(name))
|
|
139
|
-
ambiguous.add(name);
|
|
140
|
-
else
|
|
141
|
-
destinations.set(name, join(old.targetRoot, relative(old.root, path)));
|
|
142
|
-
});
|
|
143
|
-
const reference = (value) => {
|
|
144
|
-
if (typeof value === 'string') {
|
|
145
|
-
if (!value.startsWith(old.storedAgent + sep + 'sessions' + sep) || unrecovered.has(value) || map.mapPath(value) !== value)
|
|
146
|
-
return;
|
|
147
|
-
const name = basename(value);
|
|
148
|
-
if (ambiguous.has(name) || !destinations.has(name)) {
|
|
149
|
-
// A transcript that no longer exists anywhere: the reference is kept as written.
|
|
150
|
-
if (!unrecovered.has(value))
|
|
151
|
-
migration.resolutions.push(`lost-transcript: ${value} is referenced but ${ambiguous.has(name) ? 'matches more than one moved transcript' : 'exists nowhere'}; left the reference unchanged`);
|
|
152
|
-
unrecovered.add(value);
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
map.add(value, destinations.get(name));
|
|
156
|
-
}
|
|
157
|
-
else if (Array.isArray(value)) {
|
|
158
|
-
for (const item of value)
|
|
159
|
-
reference(item);
|
|
160
|
-
}
|
|
161
|
-
else if (value !== null && typeof value === 'object') {
|
|
162
|
-
for (const item of Object.values(value))
|
|
163
|
-
reference(item);
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
const inspect = (path) => {
|
|
167
|
-
if (path.endsWith('/meta.json') || path.endsWith('.json') && (path.includes('/tickets/') || path.includes('/requests/'))) {
|
|
168
|
-
reference(JSON.parse(readFileSync(path, 'utf8')));
|
|
169
|
-
}
|
|
170
|
-
else if (path.endsWith('.jsonl') && path.includes('/conversations/')) {
|
|
171
|
-
const text = readFileSync(path, 'utf8');
|
|
172
|
-
reference(JSON.parse(text.slice(0, text.indexOf('\n') < 0 ? undefined : text.indexOf('\n'))));
|
|
173
|
-
}
|
|
174
|
-
else if (path.endsWith('.md') && (path.includes('/memory/') || path.includes('/profiles/'))) {
|
|
175
|
-
const text = readFileSync(path, 'utf8');
|
|
176
|
-
if (!text.startsWith('---\n'))
|
|
177
|
-
return;
|
|
178
|
-
const end = text.indexOf('\n---', 4);
|
|
179
|
-
if (end < 0)
|
|
180
|
-
return;
|
|
181
|
-
for (const line of text.slice(0, end).split('\n')) {
|
|
182
|
-
const match = line.match(/^\s*session_file:\s*["']?([^"'\n]+)["']?$/);
|
|
183
|
-
if (match)
|
|
184
|
-
reference(match[1]);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
walk(apps, inspect);
|
|
189
|
-
walk(join(old.root, 'user', 'memory'), inspect);
|
|
190
|
-
walk(join(runtime, 'nodes'), inspect);
|
|
191
|
-
walk(join(runtime, 'tickets'), inspect);
|
|
192
|
-
walk(join(runtime, 'requests'), inspect);
|
|
193
|
-
const canvas = join(runtime, 'canvas.db');
|
|
194
|
-
if (existsSync(canvas)) {
|
|
195
|
-
const db = new DatabaseSync(canvas, { readOnly: true });
|
|
196
|
-
try {
|
|
197
|
-
for (const [table, fields] of Object.entries(canvasColumns)) {
|
|
198
|
-
if (!tableExists(db, table))
|
|
199
|
-
continue;
|
|
200
|
-
const columns = new Set(db.prepare(`PRAGMA table_info("${table}")`).all().map(row => row.name));
|
|
201
|
-
for (const field of [...(fields.paths ?? []), ...(fields.json ?? [])].filter(name => columns.has(name))) {
|
|
202
|
-
for (const row of db.prepare(`SELECT "${field}" AS value FROM "${table}" WHERE "${field}" LIKE ?`).all(`%${old.storedAgent}/sessions/%`)) {
|
|
203
|
-
if ((fields.json ?? []).includes(field))
|
|
204
|
-
reference(JSON.parse(row.value));
|
|
205
|
-
else
|
|
206
|
-
reference(row.value);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
finally {
|
|
212
|
-
db.close();
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
export function storedPaths(migration) {
|
|
217
|
-
const { root } = migration.map.old;
|
|
218
|
-
const runtime = join(root, 'runtime');
|
|
219
|
-
const apps = join(root, 'apps');
|
|
220
|
-
recoverSessionMappings(migration, apps, runtime);
|
|
221
|
-
rewriteDb(join(runtime, 'canvas.db'), canvasColumns, migration);
|
|
222
|
-
rewriteDb(join(runtime, 'scope', 'operations.db'), scopeColumns, migration);
|
|
223
|
-
// Project-scoped state remains with each repository. Its path is discovered
|
|
224
|
-
// from profile registrations and node cwd, including repos under hosted work.
|
|
225
|
-
const projects = new Set();
|
|
226
|
-
const dbPath = join(runtime, 'canvas.db');
|
|
227
|
-
if (existsSync(dbPath)) {
|
|
228
|
-
const db = new DatabaseSync(dbPath, { readOnly: true });
|
|
229
|
-
try {
|
|
230
|
-
if (tableExists(db, 'nodes') && db.prepare('PRAGMA table_info(nodes)').all().some(row => row.name === 'cwd')) {
|
|
231
|
-
for (const row of db.prepare('SELECT cwd FROM nodes').all())
|
|
232
|
-
projects.add(row.cwd);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
finally {
|
|
236
|
-
db.close();
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
const userDb = join(runtime, 'scope', 'operations.db');
|
|
240
|
-
if (existsSync(userDb)) {
|
|
241
|
-
const db = new DatabaseSync(userDb, { readOnly: true });
|
|
242
|
-
try {
|
|
243
|
-
if (tableExists(db, 'profile_projects'))
|
|
244
|
-
for (const row of db.prepare('SELECT path FROM profile_projects').all())
|
|
245
|
-
projects.add(row.path);
|
|
246
|
-
}
|
|
247
|
-
finally {
|
|
248
|
-
db.close();
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
for (const project of projects) {
|
|
252
|
-
// A project root can be an ancestor of cwd; inspect each ancestor's
|
|
253
|
-
// .crouter/operations.db, not unrelated files in the project.
|
|
254
|
-
const targetRoot = migration.map.old.targetRoot;
|
|
255
|
-
let directory = project.startsWith(targetRoot + '/')
|
|
256
|
-
? resolve(root, relative(targetRoot, project)) : project;
|
|
257
|
-
while (directory !== dirname(directory)) {
|
|
258
|
-
rewriteDb(join(directory, '.crouter', 'operations.db'), scopeColumns, migration);
|
|
259
|
-
directory = dirname(directory);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
walk(join(apps), path => {
|
|
263
|
-
if (path.endsWith('/.crouter/operations.db'))
|
|
264
|
-
rewriteDb(path, scopeColumns, migration);
|
|
265
|
-
if (path.endsWith('/meta.json'))
|
|
266
|
-
rewriteFile(path, migration, text => rewriteJson(text, migration));
|
|
267
|
-
if (path.endsWith('.md') && (path.includes('/memory/') || path.includes('/profiles/')))
|
|
268
|
-
rewriteMemory(path, migration);
|
|
269
|
-
if (path.endsWith('.jsonl') && path.includes('/conversations/'))
|
|
270
|
-
rewriteHeader(path, migration);
|
|
271
|
-
});
|
|
272
|
-
walk(join(root, 'user', 'memory'), path => { if (path.endsWith('.md'))
|
|
273
|
-
rewriteMemory(path, migration); });
|
|
274
|
-
rewriteBodyPaths(migration);
|
|
275
|
-
for (const path of migration.map.dangling)
|
|
276
|
-
migration.resolutions.push(`dangling-path: ${path} names a file the new layout drops; left the stored path unchanged`);
|
|
277
|
-
walk(join(root, 'user', 'files'), path => { if (path.endsWith('/.crouter/operations.db'))
|
|
278
|
-
rewriteDb(path, scopeColumns, migration); });
|
|
279
|
-
walk(join(runtime, 'nodes'), path => {
|
|
280
|
-
if (path.endsWith('/meta.json'))
|
|
281
|
-
rewriteFile(path, migration, text => rewriteJson(text, migration));
|
|
282
|
-
if (path.endsWith('/session.ptr'))
|
|
283
|
-
unlinkSync(path);
|
|
284
|
-
});
|
|
285
|
-
walk(join(runtime, 'tickets'), path => { if (path.endsWith('.json'))
|
|
286
|
-
rewriteFile(path, migration, text => rewriteJson(text, migration)); });
|
|
287
|
-
walk(join(runtime, 'requests'), path => { if (path.endsWith('.json'))
|
|
288
|
-
rewriteFile(path, migration, text => rewriteJson(text, migration)); });
|
|
289
|
-
const canvas = join(runtime, 'canvas.db');
|
|
290
|
-
if (existsSync(canvas)) {
|
|
291
|
-
const db = new DatabaseSync(canvas);
|
|
292
|
-
try {
|
|
293
|
-
db.exec('DROP TABLE IF EXISTS layout_header_rewrites');
|
|
294
|
-
}
|
|
295
|
-
finally {
|
|
296
|
-
db.close();
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
function rewriteMemory(path, migration) {
|
|
301
|
-
rewriteFile(path, migration, text => {
|
|
302
|
-
if (!text.startsWith('---\n'))
|
|
303
|
-
return text;
|
|
304
|
-
const end = text.indexOf('\n---', 4);
|
|
305
|
-
if (end < 0)
|
|
306
|
-
return text;
|
|
307
|
-
const head = text.slice(0, end);
|
|
308
|
-
return head.replace(/^(\s*session_file:\s*)([^\n]+)$/gm, (line, prefix, value) => {
|
|
309
|
-
const quoted = value.match(/^(["'])(.*)\1$/);
|
|
310
|
-
const raw = quoted ? quoted[2] : value.trim();
|
|
311
|
-
const mapped = migration.map.mapPath(raw);
|
|
312
|
-
return mapped === raw ? line : `${prefix}${quoted ? quoted[1] : ''}${mapped}${quoted ? quoted[1] : ''}`;
|
|
313
|
-
}) + text.slice(end);
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
function rewriteHeader(path, migration) {
|
|
317
|
-
const canvas = join(migration.map.old.root, 'runtime', 'canvas.db');
|
|
318
|
-
if (!existsSync(canvas)) {
|
|
319
|
-
rewriteFile(path, migration, text => {
|
|
320
|
-
const end = text.indexOf('\n');
|
|
321
|
-
const first = end < 0 ? text : text.slice(0, end);
|
|
322
|
-
return rewriteJson(first, migration) + (end < 0 ? '' : text.slice(end));
|
|
323
|
-
});
|
|
324
|
-
return;
|
|
325
|
-
}
|
|
326
|
-
const db = new DatabaseSync(canvas);
|
|
327
|
-
try {
|
|
328
|
-
db.exec('CREATE TABLE IF NOT EXISTS layout_header_rewrites (path TEXT PRIMARY KEY, old_header TEXT NOT NULL, new_header TEXT NOT NULL)');
|
|
329
|
-
const journal = db.prepare('SELECT old_header, new_header FROM layout_header_rewrites WHERE path = ?').get(path);
|
|
330
|
-
const text = readFileSync(path, 'utf8');
|
|
331
|
-
const end = text.indexOf('\n');
|
|
332
|
-
const first = end < 0 ? text : text.slice(0, end);
|
|
333
|
-
const mapped = journal?.new_header ?? rewriteJson(first, migration);
|
|
334
|
-
if (journal && first !== journal.old_header && first !== mapped)
|
|
335
|
-
throw new Error(`conversation header changed during migration: ${path}`);
|
|
336
|
-
if (!journal && mapped !== first) {
|
|
337
|
-
const targetPath = join(migration.map.old.targetRoot, relative(migration.map.old.root, path));
|
|
338
|
-
const delta = Buffer.byteLength(mapped) - Buffer.byteLength(first);
|
|
339
|
-
db.exec('BEGIN IMMEDIATE');
|
|
340
|
-
try {
|
|
341
|
-
db.prepare('INSERT INTO layout_header_rewrites (path, old_header, new_header) VALUES (?, ?, ?)').run(path, first, mapped);
|
|
342
|
-
if (delta !== 0 && tableExists(db, 'sessions')) {
|
|
343
|
-
db.prepare('UPDATE sessions SET event_offset = event_offset + ? WHERE transcript_path = ? AND event_offset > 0').run(delta, targetPath);
|
|
344
|
-
}
|
|
345
|
-
db.exec('COMMIT');
|
|
346
|
-
}
|
|
347
|
-
catch (error) {
|
|
348
|
-
db.exec('ROLLBACK');
|
|
349
|
-
throw error;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
if (first !== mapped)
|
|
353
|
-
rewriteFile(path, migration, original => {
|
|
354
|
-
const split = original.indexOf('\n');
|
|
355
|
-
const header = split < 0 ? original : original.slice(0, split);
|
|
356
|
-
if (header !== first)
|
|
357
|
-
throw new Error(`conversation header changed during migration: ${path}`);
|
|
358
|
-
return mapped + (split < 0 ? '' : original.slice(split));
|
|
359
|
-
});
|
|
360
|
-
if (journal || mapped !== first)
|
|
361
|
-
db.prepare('DELETE FROM layout_header_rewrites WHERE path = ?').run(path);
|
|
362
|
-
}
|
|
363
|
-
finally {
|
|
364
|
-
db.close();
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
function escape(text) { return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }
|
|
368
|
-
/**
|
|
369
|
-
* Markdown links a moved profile folder by its old absolute path (or its `~/`
|
|
370
|
-
* spelling); point each at the new location. Scans the documents the canvas
|
|
371
|
-
* import reads: user and app memory, profile files, and node context. A dry
|
|
372
|
-
* run scans the same documents at their old locations and writes nothing.
|
|
373
|
-
*/
|
|
374
|
-
export function rewriteBodyPaths(migration) {
|
|
375
|
-
const pairs = migration.bodyPaths;
|
|
376
|
-
if (pairs.length === 0)
|
|
377
|
-
return;
|
|
378
|
-
const home = homedir();
|
|
379
|
-
const spellings = [];
|
|
380
|
-
for (const [from, to] of pairs) {
|
|
381
|
-
spellings.push([from, to]);
|
|
382
|
-
if (from.startsWith(home + sep))
|
|
383
|
-
spellings.push([`~${from.slice(home.length)}`, to.startsWith(home + sep) ? `~${to.slice(home.length)}` : to]);
|
|
384
|
-
}
|
|
385
|
-
spellings.sort((a, b) => b[0].length - a[0].length);
|
|
386
|
-
const pattern = new RegExp(`(${spellings.map(([from]) => escape(from)).join('|')})(?![A-Za-z0-9_-])`, 'g');
|
|
387
|
-
const target = new Map(spellings);
|
|
388
|
-
const { root, scope, canvas } = migration.map.old;
|
|
389
|
-
const visit = (path) => {
|
|
390
|
-
if (!path.endsWith('.md'))
|
|
391
|
-
return;
|
|
392
|
-
if (migration.dryRun) {
|
|
393
|
-
const count = readFileSync(path, 'utf8').match(pattern)?.length ?? 0;
|
|
394
|
-
if (count)
|
|
395
|
-
migration.resolutions.push(`profile-extra-link: rewrote ${count} link(s) to moved profile files in ${path}`);
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
let count = 0;
|
|
399
|
-
rewriteFile(path, migration, text => text.replace(pattern, match => { count++; return target.get(match); }));
|
|
400
|
-
if (count)
|
|
401
|
-
migration.resolutions.push(`profile-extra-link: rewrote ${count} link(s) to moved profile files in ${path}`);
|
|
402
|
-
};
|
|
403
|
-
const nodeContexts = (nodes) => {
|
|
404
|
-
if (existsSync(nodes))
|
|
405
|
-
for (const id of readdirSync(nodes))
|
|
406
|
-
walk(join(nodes, id, 'context'), visit);
|
|
407
|
-
};
|
|
408
|
-
if (migration.dryRun) {
|
|
409
|
-
walk(join(scope, 'memory'), visit);
|
|
410
|
-
walk(join(scope, 'profiles'), visit);
|
|
411
|
-
nodeContexts(join(canvas, 'nodes'));
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
walk(join(root, 'user', 'memory'), visit);
|
|
415
|
-
const apps = join(root, 'apps');
|
|
416
|
-
if (!existsSync(apps))
|
|
417
|
-
return;
|
|
418
|
-
for (const app of readdirSync(apps)) {
|
|
419
|
-
walk(join(apps, app, 'memory'), visit);
|
|
420
|
-
walk(join(apps, app, 'profiles'), visit);
|
|
421
|
-
walk(join(apps, app, 'files', 'profiles'), visit);
|
|
422
|
-
nodeContexts(join(apps, app, 'nodes'));
|
|
423
|
-
}
|
|
424
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { profileSpace } from '../layout.js';
|
|
2
|
-
import type { LayoutMigration } from './index.js';
|
|
3
|
-
export interface ProfileFolder {
|
|
4
|
-
id: string;
|
|
5
|
-
source: string;
|
|
6
|
-
/** No scoped-state row: the whole folder moves to `files`. */
|
|
7
|
-
target: ReturnType<typeof profileSpace> | null;
|
|
8
|
-
files: string;
|
|
9
|
-
/** Entries with no slot in the new layout, moved under `files` at the same name. */
|
|
10
|
-
extras: string[];
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* A profile is its scoped-state row; the row is authoritative even when an
|
|
14
|
-
* interrupted run already removed the old profile folder. There is no profile
|
|
15
|
-
* folder in the new layout: the old folder's own path (the profile's
|
|
16
|
-
* scope_owners.root) becomes its memory folder. What else the folder holds is
|
|
17
|
-
* the profile's own files and moves to `apps/<app>/files/profiles/<id>/`; a
|
|
18
|
-
* folder with no row moves there whole, under the terminal app.
|
|
19
|
-
*/
|
|
20
|
-
export declare function profileFolders(migration: LayoutMigration): {
|
|
21
|
-
folders: ProfileFolder[];
|
|
22
|
-
junk: string[];
|
|
23
|
-
aside: string[];
|
|
24
|
-
};
|
|
25
|
-
export declare function memoryAndProfiles(migration: LayoutMigration): void;
|
|
26
|
-
export declare function conversations(migration: LayoutMigration): void;
|
|
27
|
-
export declare function repairMovedRepositories(migration: LayoutMigration): void;
|
|
28
|
-
export declare function worktrees(migration: LayoutMigration): void;
|
|
29
|
-
/** Every resolution the real run would make, computed without changing anything. */
|
|
30
|
-
export declare function previewStores(migration: LayoutMigration): void;
|