@xenosystem/primitives 0.3.1 → 0.3.2

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/README.md CHANGED
@@ -20,6 +20,12 @@ never followed). `resolvePlatformNativeRoots` names the old locations for a prod
20
20
  segments `['plugins', '{path}']` copies every file below `plugins/`, keeping the structure. Links inside are refused;
21
21
  the rule must copy and name the remainder with exactly one `{path}` segment.
22
22
 
23
+ **A tree moves whole or not at all (0.3.2).** When two sources would write into one destination tree, or the
24
+ destination tree already holds files the source would not put there (or different bytes), every file of that tree is
25
+ held back and reported as `held back: …`, and none is copied: half of two plugin stores merged is worse than either. A
26
+ repeated run over an identical copy is not held back. Built-in rule `extensions-cli-plugins` moves the CLI's
27
+ `~/.xeno-agent/plugins` into `data/extensions/plugins` under that rule.
28
+
23
29
  ```ts
24
30
  import { createDirs } from '@xenosystem/primitives/dirs';
25
31
  import { createSecretStore, createOsKeyStore } from '@xenosystem/primitives/secrets';
@@ -52,6 +52,48 @@ function matchRule(rule, rel) {
52
52
  const last = got[got.length - 1];
53
53
  return globMatch(want[want.length - 1], last) ? { file: last, rest: [] } : null;
54
54
  }
55
+ /** The destination folder a tree rule copies into: its target with the '{path}' segment and everything after dropped. */
56
+ const treeRoot = (rule) => rule.match.split('/').pop() === TREE && rule.action === 'copy'
57
+ ? `${rule.target.kind}:${[rule.target.area, ...rule.target.segments.slice(0, rule.target.segments.indexOf(PATH_TOKEN))].join('/')}`
58
+ : null;
59
+ /**
60
+ * Why an existing destination tree is not just a partial copy of these entries (null when it is, or is absent): a file
61
+ * no entry would write there, a link, or bytes different from the entry's source.
62
+ */
63
+ function destinationTreeDiffers(root, entries, sep) {
64
+ let st;
65
+ try {
66
+ st = (0, node_fs_1.lstatSync)(root);
67
+ }
68
+ catch {
69
+ return null;
70
+ }
71
+ if (!st.isDirectory() || st.isSymbolicLink())
72
+ return 'it is not a folder';
73
+ const bySource = new Map(entries.map((e) => [e.dest, e]));
74
+ const pending = [root];
75
+ while (pending.length > 0) {
76
+ const dir = pending.pop();
77
+ for (const name of (0, node_fs_1.readdirSync)(dir)) {
78
+ const abs = dir + sep + name;
79
+ const s = (0, node_fs_1.lstatSync)(abs);
80
+ if (s.isSymbolicLink())
81
+ return `a link at ${abs.slice(root.length + 1)}`;
82
+ if (s.isDirectory()) {
83
+ pending.push(abs);
84
+ continue;
85
+ }
86
+ const entry = bySource.get(abs);
87
+ if (entry === undefined)
88
+ return `${abs.slice(root.length + 1)} is not in this source`;
89
+ const same = s.size === entry.bytes
90
+ && (0, node_crypto_1.createHash)('sha256').update((0, node_fs_1.readFileSync)(abs)).digest('hex') === (0, node_crypto_1.createHash)('sha256').update((0, node_fs_1.readFileSync)(entry.source)).digest('hex');
91
+ if (!same)
92
+ return `${abs.slice(root.length + 1)} differs`;
93
+ }
94
+ }
95
+ return null;
96
+ }
55
97
  const fill = (s, file) => s.split('{file}').join(file);
56
98
  /** A target's segments with `{file}` filled and a whole `{path}` segment expanded into the matched remainder. */
57
99
  const expand = (segments, m) => segments.flatMap((x) => (x === PATH_TOKEN ? m.rest : [fill(x, m.file)]));
@@ -90,6 +132,8 @@ function planMigration(options) {
90
132
  const names = options.legacyHomes ?? contract_ts_1.INPUTS_JSON.legacyHomes;
91
133
  const homes = [];
92
134
  const entries = [];
135
+ /** The destination tree of each entry a tree rule produced. */
136
+ const trees = new Map();
93
137
  const refused = [];
94
138
  const unplaced = { count: 0, sample: [], truncated: false };
95
139
  let visited = 0;
@@ -168,7 +212,11 @@ function planMigration(options) {
168
212
  if (rule.action === 'copy') {
169
213
  const segs = expand(rule.target.segments, m);
170
214
  const dest = options.dirs.path(rule.target.kind, rule.target.area, ...segs);
171
- entries.push({ rule: rule.id, action: 'copy', legacyHome: name, sourceRel: r, source: abs, bytes: s.size, dest, destRel: [rule.target.area, ...segs].join('/'), destKind: rule.target.kind });
215
+ const entry = { rule: rule.id, action: 'copy', legacyHome: name, sourceRel: r, source: abs, bytes: s.size, dest, destRel: [rule.target.area, ...segs].join('/'), destKind: rule.target.kind };
216
+ const tree = treeRoot(rule);
217
+ if (tree !== null)
218
+ trees.set(entry, tree);
219
+ entries.push(entry);
172
220
  }
173
221
  else {
174
222
  const sn = fill(rule.target.name, m.file);
@@ -258,13 +306,42 @@ function planMigration(options) {
258
306
  const byDest = new Map();
259
307
  for (const e of entries)
260
308
  byDest.set(`${e.destKind}:${e.destRel}`, [...(byDest.get(`${e.destKind}:${e.destRel}`) ?? []), e]);
309
+ // A tree is one unit (an installed plugin store has a registry naming its folders), so a collision anywhere in a
310
+ // destination tree holds back EVERY source copying into it: half of two trees merged is worse than neither.
311
+ const heldTrees = new Map();
312
+ for (const group of byDest.values())
313
+ if (group.length > 1)
314
+ for (const e of group) {
315
+ const t = trees.get(e);
316
+ if (t !== undefined)
317
+ heldTrees.set(t, `held back: another source also copies into ${t.slice(t.indexOf(':') + 1)} and collides there, so neither tree is merged`);
318
+ }
319
+ // The same holds against what is already there: a destination tree holding files this plan would not put there, or
320
+ // different bytes, is another store, and copying into it would merge two. A resumed run (identical bytes) passes.
321
+ for (const t of new Set(trees.values())) {
322
+ if (heldTrees.has(t))
323
+ continue;
324
+ const mine = entries.filter((e) => trees.get(e) === t);
325
+ const kind = t.slice(0, t.indexOf(':'));
326
+ const root = options.dirs.root(kind) + sep + t.slice(t.indexOf(':') + 1).split('/').join(sep);
327
+ const why = destinationTreeDiffers(root, mine, sep);
328
+ if (why !== null)
329
+ heldTrees.set(t, `held back: ${t.slice(t.indexOf(':') + 1)} already holds another store (${why}), so it is not merged into`);
330
+ }
261
331
  const clean = [];
262
332
  for (const group of byDest.values()) {
263
- if (group.length === 1)
264
- clean.push(group[0]);
265
- else
333
+ if (group.length > 1) {
266
334
  for (const e of group)
267
335
  refused.push({ path: e.source, reason: `destination collision: ${group.length} sources map to ${e.destRel}` });
336
+ continue;
337
+ }
338
+ const e = group[0];
339
+ const t = trees.get(e);
340
+ const held = t === undefined ? undefined : heldTrees.get(t);
341
+ if (held !== undefined)
342
+ refused.push({ path: e.source, reason: held });
343
+ else
344
+ clean.push(e);
268
345
  }
269
346
  clean.sort((a, b) => { const x = a.legacyHome + a.sourceRel, y = b.legacyHome + b.sourceRel; return x < y ? -1 : x > y ? 1 : 0; });
270
347
  return { id: planId(clean), homes, entries: clean, refused, unplaced };
@@ -251,6 +251,21 @@ exports.LEGACY_MAP_JSON = {
251
251
  },
252
252
  "source": "XENO LOCAL HOME - SPEC.md section 5 (extensions: config, data, cache) and xeno-primitives/ADOPTION.md 0.3.1: the Agent host plugin/MCP store (xeno-agent-interface PluginMcpManagerService) moves from ~/.xeno/{plugins,mcp}; installed plugins, their registry, trust decisions and event log are one tree owned by the SDK plugin host"
253
253
  },
254
+ {
255
+ "id": "extensions-cli-plugins",
256
+ "legacyHome": ".xeno-agent",
257
+ "match": "plugins/**",
258
+ "action": "copy",
259
+ "target": {
260
+ "kind": "data",
261
+ "area": "extensions",
262
+ "segments": [
263
+ "plugins",
264
+ "{path}"
265
+ ]
266
+ },
267
+ "source": "XENO LOCAL HOME - SPEC.md section 5 and xeno-primitives/ADOPTION.md 0.3.2: xeno-agent-cli kept its SDK plugin host store in ~/.xeno-agent/plugins; from CLI 0.5.139 it uses the Agent host store, so both surfaces manage one set of plugins. Same tree format as extensions-plugins; when both legacy trees hold files, neither is merged (a tree collision holds back the whole tree)"
268
+ },
254
269
  {
255
270
  "id": "extensions-mcp-servers",
256
271
  "legacyHome": ".xeno",
@@ -1,5 +1,5 @@
1
1
  import { createHash, randomBytes } from 'node:crypto';
2
- import { createReadStream, createWriteStream, lstatSync, readdirSync, promises as fsp } from 'node:fs';
2
+ import { createReadStream, createWriteStream, lstatSync, readdirSync, readFileSync, promises as fsp } from 'node:fs';
3
3
  import { basename, dirname, join } from 'node:path';
4
4
  import { INPUTS_JSON, LEGACY_MAP_JSON } from "../generated/contract.js";
5
5
  import { atomicWriteFile } from "./atomic.js";
@@ -43,6 +43,48 @@ function matchRule(rule, rel) {
43
43
  const last = got[got.length - 1];
44
44
  return globMatch(want[want.length - 1], last) ? { file: last, rest: [] } : null;
45
45
  }
46
+ /** The destination folder a tree rule copies into: its target with the '{path}' segment and everything after dropped. */
47
+ const treeRoot = (rule) => rule.match.split('/').pop() === TREE && rule.action === 'copy'
48
+ ? `${rule.target.kind}:${[rule.target.area, ...rule.target.segments.slice(0, rule.target.segments.indexOf(PATH_TOKEN))].join('/')}`
49
+ : null;
50
+ /**
51
+ * Why an existing destination tree is not just a partial copy of these entries (null when it is, or is absent): a file
52
+ * no entry would write there, a link, or bytes different from the entry's source.
53
+ */
54
+ function destinationTreeDiffers(root, entries, sep) {
55
+ let st;
56
+ try {
57
+ st = lstatSync(root);
58
+ }
59
+ catch {
60
+ return null;
61
+ }
62
+ if (!st.isDirectory() || st.isSymbolicLink())
63
+ return 'it is not a folder';
64
+ const bySource = new Map(entries.map((e) => [e.dest, e]));
65
+ const pending = [root];
66
+ while (pending.length > 0) {
67
+ const dir = pending.pop();
68
+ for (const name of readdirSync(dir)) {
69
+ const abs = dir + sep + name;
70
+ const s = lstatSync(abs);
71
+ if (s.isSymbolicLink())
72
+ return `a link at ${abs.slice(root.length + 1)}`;
73
+ if (s.isDirectory()) {
74
+ pending.push(abs);
75
+ continue;
76
+ }
77
+ const entry = bySource.get(abs);
78
+ if (entry === undefined)
79
+ return `${abs.slice(root.length + 1)} is not in this source`;
80
+ const same = s.size === entry.bytes
81
+ && createHash('sha256').update(readFileSync(abs)).digest('hex') === createHash('sha256').update(readFileSync(entry.source)).digest('hex');
82
+ if (!same)
83
+ return `${abs.slice(root.length + 1)} differs`;
84
+ }
85
+ }
86
+ return null;
87
+ }
46
88
  const fill = (s, file) => s.split('{file}').join(file);
47
89
  /** A target's segments with `{file}` filled and a whole `{path}` segment expanded into the matched remainder. */
48
90
  const expand = (segments, m) => segments.flatMap((x) => (x === PATH_TOKEN ? m.rest : [fill(x, m.file)]));
@@ -81,6 +123,8 @@ export function planMigration(options) {
81
123
  const names = options.legacyHomes ?? INPUTS_JSON.legacyHomes;
82
124
  const homes = [];
83
125
  const entries = [];
126
+ /** The destination tree of each entry a tree rule produced. */
127
+ const trees = new Map();
84
128
  const refused = [];
85
129
  const unplaced = { count: 0, sample: [], truncated: false };
86
130
  let visited = 0;
@@ -159,7 +203,11 @@ export function planMigration(options) {
159
203
  if (rule.action === 'copy') {
160
204
  const segs = expand(rule.target.segments, m);
161
205
  const dest = options.dirs.path(rule.target.kind, rule.target.area, ...segs);
162
- entries.push({ rule: rule.id, action: 'copy', legacyHome: name, sourceRel: r, source: abs, bytes: s.size, dest, destRel: [rule.target.area, ...segs].join('/'), destKind: rule.target.kind });
206
+ const entry = { rule: rule.id, action: 'copy', legacyHome: name, sourceRel: r, source: abs, bytes: s.size, dest, destRel: [rule.target.area, ...segs].join('/'), destKind: rule.target.kind };
207
+ const tree = treeRoot(rule);
208
+ if (tree !== null)
209
+ trees.set(entry, tree);
210
+ entries.push(entry);
163
211
  }
164
212
  else {
165
213
  const sn = fill(rule.target.name, m.file);
@@ -249,13 +297,42 @@ export function planMigration(options) {
249
297
  const byDest = new Map();
250
298
  for (const e of entries)
251
299
  byDest.set(`${e.destKind}:${e.destRel}`, [...(byDest.get(`${e.destKind}:${e.destRel}`) ?? []), e]);
300
+ // A tree is one unit (an installed plugin store has a registry naming its folders), so a collision anywhere in a
301
+ // destination tree holds back EVERY source copying into it: half of two trees merged is worse than neither.
302
+ const heldTrees = new Map();
303
+ for (const group of byDest.values())
304
+ if (group.length > 1)
305
+ for (const e of group) {
306
+ const t = trees.get(e);
307
+ if (t !== undefined)
308
+ heldTrees.set(t, `held back: another source also copies into ${t.slice(t.indexOf(':') + 1)} and collides there, so neither tree is merged`);
309
+ }
310
+ // The same holds against what is already there: a destination tree holding files this plan would not put there, or
311
+ // different bytes, is another store, and copying into it would merge two. A resumed run (identical bytes) passes.
312
+ for (const t of new Set(trees.values())) {
313
+ if (heldTrees.has(t))
314
+ continue;
315
+ const mine = entries.filter((e) => trees.get(e) === t);
316
+ const kind = t.slice(0, t.indexOf(':'));
317
+ const root = options.dirs.root(kind) + sep + t.slice(t.indexOf(':') + 1).split('/').join(sep);
318
+ const why = destinationTreeDiffers(root, mine, sep);
319
+ if (why !== null)
320
+ heldTrees.set(t, `held back: ${t.slice(t.indexOf(':') + 1)} already holds another store (${why}), so it is not merged into`);
321
+ }
252
322
  const clean = [];
253
323
  for (const group of byDest.values()) {
254
- if (group.length === 1)
255
- clean.push(group[0]);
256
- else
324
+ if (group.length > 1) {
257
325
  for (const e of group)
258
326
  refused.push({ path: e.source, reason: `destination collision: ${group.length} sources map to ${e.destRel}` });
327
+ continue;
328
+ }
329
+ const e = group[0];
330
+ const t = trees.get(e);
331
+ const held = t === undefined ? undefined : heldTrees.get(t);
332
+ if (held !== undefined)
333
+ refused.push({ path: e.source, reason: held });
334
+ else
335
+ clean.push(e);
259
336
  }
260
337
  clean.sort((a, b) => { const x = a.legacyHome + a.sourceRel, y = b.legacyHome + b.sourceRel; return x < y ? -1 : x > y ? 1 : 0; });
261
338
  return { id: planId(clean), homes, entries: clean, refused, unplaced };
@@ -248,6 +248,21 @@ export const LEGACY_MAP_JSON = {
248
248
  },
249
249
  "source": "XENO LOCAL HOME - SPEC.md section 5 (extensions: config, data, cache) and xeno-primitives/ADOPTION.md 0.3.1: the Agent host plugin/MCP store (xeno-agent-interface PluginMcpManagerService) moves from ~/.xeno/{plugins,mcp}; installed plugins, their registry, trust decisions and event log are one tree owned by the SDK plugin host"
250
250
  },
251
+ {
252
+ "id": "extensions-cli-plugins",
253
+ "legacyHome": ".xeno-agent",
254
+ "match": "plugins/**",
255
+ "action": "copy",
256
+ "target": {
257
+ "kind": "data",
258
+ "area": "extensions",
259
+ "segments": [
260
+ "plugins",
261
+ "{path}"
262
+ ]
263
+ },
264
+ "source": "XENO LOCAL HOME - SPEC.md section 5 and xeno-primitives/ADOPTION.md 0.3.2: xeno-agent-cli kept its SDK plugin host store in ~/.xeno-agent/plugins; from CLI 0.5.139 it uses the Agent host store, so both surfaces manage one set of plugins. Same tree format as extensions-plugins; when both legacy trees hold files, neither is merged (a tree collision holds back the whole tree)"
265
+ },
251
266
  {
252
267
  "id": "extensions-mcp-servers",
253
268
  "legacyHome": ".xeno",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xenosystem/primitives",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "XENO back-end rung 1 primitives: dirs (the XENO local home), secrets (envelope-encrypted credentials) and updates (the gated app updater).",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",