@xenosystem/primitives 0.3.0 → 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
@@ -16,6 +16,16 @@ Authority: `XENO LOCAL HOME - SPEC.md` (LOCKED). Zero runtime dependencies. Prop
16
16
  those versions wrote into `~/.xeno` (config, data, secrets and state; cache and run are reported, not copied; links are
17
17
  never followed). `resolvePlatformNativeRoots` names the old locations for a product's own migration.
18
18
 
19
+ **Tree rules (0.3.1):** a product's migration rule may match a whole directory — `match: 'plugins/**'` with target
20
+ segments `['plugins', '{path}']` copies every file below `plugins/`, keeping the structure. Links inside are refused;
21
+ the rule must copy and name the remainder with exactly one `{path}` segment.
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
+
19
29
  ```ts
20
30
  import { createDirs } from '@xenosystem/primitives/dirs';
21
31
  import { createSecretStore, createOsKeyStore } from '@xenosystem/primitives/secrets';
@@ -26,18 +26,89 @@ function globMatch(pattern, name) {
26
26
  const re = new RegExp('^' + pattern.split('*').map((s) => s.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('.*') + '$');
27
27
  return re.test(name);
28
28
  }
29
+ /**
30
+ * A rule whose `match` ends in `/**` matches every file below that prefix, at any depth (an installed plugin is a
31
+ * tree). Its target names the remainder with one whole `{path}` segment. Links are still never followed.
32
+ */
33
+ const TREE = '**';
34
+ const PATH_TOKEN = '{path}';
29
35
  function matchRule(rule, rel) {
30
36
  const want = rule.match.split('/');
31
37
  const got = rel.split('/');
38
+ if (want[want.length - 1] === TREE) {
39
+ const prefix = want.slice(0, -1);
40
+ if (got.length <= prefix.length)
41
+ return null;
42
+ for (let i = 0; i < prefix.length; i++)
43
+ if (prefix[i] !== got[i])
44
+ return null;
45
+ return { file: got[got.length - 1], rest: got.slice(prefix.length) };
46
+ }
32
47
  if (want.length !== got.length)
33
48
  return null;
34
49
  for (let i = 0; i < want.length - 1; i++)
35
50
  if (want[i] !== got[i])
36
51
  return null;
37
52
  const last = got[got.length - 1];
38
- return globMatch(want[want.length - 1], last) ? { file: last } : null;
53
+ return globMatch(want[want.length - 1], last) ? { file: last, rest: [] } : null;
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;
39
96
  }
40
97
  const fill = (s, file) => s.split('{file}').join(file);
98
+ /** A target's segments with `{file}` filled and a whole `{path}` segment expanded into the matched remainder. */
99
+ const expand = (segments, m) => segments.flatMap((x) => (x === PATH_TOKEN ? m.rest : [fill(x, m.file)]));
100
+ function assertRuleShape(r) {
101
+ const parts = r.match.split('/');
102
+ const tree = parts[parts.length - 1] === TREE;
103
+ if (parts.slice(0, -1).includes(TREE))
104
+ throw new errors_ts_1.DirsError('invalid-argument', `rule ${r.id}: '**' is allowed only as the last segment of match`);
105
+ if (!tree)
106
+ return;
107
+ if (r.action !== 'copy')
108
+ throw new errors_ts_1.DirsError('invalid-argument', `rule ${r.id}: a tree rule ('/**') must copy; a secret is one file`);
109
+ if (r.target.segments.filter((x) => x === PATH_TOKEN).length !== 1)
110
+ throw new errors_ts_1.DirsError('invalid-argument', `rule ${r.id}: a tree rule names the remainder with exactly one '{path}' segment`);
111
+ }
41
112
  /** Deterministic id: the same plan (rules, sources, destinations) always has the same journal. */
42
113
  function planId(entries) {
43
114
  const lines = entries.map((e) => `${e.rule}\t${e.legacyHome}\t${e.sourceRel}\t${e.destRel}`).sort().join('\n');
@@ -56,10 +127,13 @@ function planMigration(options) {
56
127
  if (seen.has(r.id))
57
128
  throw new errors_ts_1.DirsError('invalid-argument', `duplicate migration rule id: ${r.id}`);
58
129
  seen.add(r.id);
130
+ assertRuleShape(r);
59
131
  }
60
132
  const names = options.legacyHomes ?? contract_ts_1.INPUTS_JSON.legacyHomes;
61
133
  const homes = [];
62
134
  const entries = [];
135
+ /** The destination tree of each entry a tree rule produced. */
136
+ const trees = new Map();
63
137
  const refused = [];
64
138
  const unplaced = { count: 0, sample: [], truncated: false };
65
139
  let visited = 0;
@@ -136,9 +210,13 @@ function planMigration(options) {
136
210
  const { rule, m } = hits[0];
137
211
  try {
138
212
  if (rule.action === 'copy') {
139
- const segs = rule.target.segments.map((x) => fill(x, m.file));
213
+ const segs = expand(rule.target.segments, m);
140
214
  const dest = options.dirs.path(rule.target.kind, rule.target.area, ...segs);
141
- 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);
142
220
  }
143
221
  else {
144
222
  const sn = fill(rule.target.name, m.file);
@@ -228,13 +306,42 @@ function planMigration(options) {
228
306
  const byDest = new Map();
229
307
  for (const e of entries)
230
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
+ }
231
331
  const clean = [];
232
332
  for (const group of byDest.values()) {
233
- if (group.length === 1)
234
- clean.push(group[0]);
235
- else
333
+ if (group.length > 1) {
236
334
  for (const e of group)
237
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);
238
345
  }
239
346
  clean.sort((a, b) => { const x = a.legacyHome + a.sourceRel, y = b.legacyHome + b.sourceRel; return x < y ? -1 : x > y ? 1 : 0; });
240
347
  return { id: planId(clean), homes, entries: clean, refused, unplaced };
@@ -220,7 +220,7 @@ exports.INPUTS_JSON = {
220
220
  "$comment2": "secretsKeyService/Account name the single OS-store entry that holds the per-machine master key (spec section 6). The service equals the locked bundle identifier."
221
221
  };
222
222
  exports.LEGACY_MAP_JSON = {
223
- "$comment": "Built-in migration rules: legacy file -> registered location. Deliberately minimal: only mappings the ecosystem documents explicitly. Each product owner supplies the rest as rules; anything unmatched is reported as unplaced, never guessed (XENO LOCAL HOME - SPEC.md section 10.6). A 'copy' rule names kind+area+segments; a 'secret' rule names area+name and needs a secret sink. '{file}' stands for the matched file name; only the last path segment of 'match' may contain '*'.",
223
+ "$comment": "Built-in migration rules: legacy file -> registered location. Deliberately minimal: only mappings the ecosystem documents explicitly. Each product owner supplies the rest as rules; anything unmatched is reported as unplaced, never guessed (XENO LOCAL HOME - SPEC.md section 10.6). A 'copy' rule names kind+area+segments; a 'secret' rule names area+name and needs a secret sink. '{file}' stands for the matched file name; only the last path segment of 'match' may contain '*'. A 'match' ending in '/**' (0.3.1) is a tree rule: it matches every file below that prefix at any depth, must be a copy, and names the remainder with exactly one whole '{path}' target segment; links inside the tree are refused, never followed.",
224
224
  "rules": [
225
225
  {
226
226
  "id": "shell-window-state",
@@ -235,6 +235,81 @@ exports.LEGACY_MAP_JSON = {
235
235
  ]
236
236
  },
237
237
  "source": "xeno-primitives/ADOPTION.md, xeno-shell row (~/.xeno/shell/state.json -> apps/shell); window state is kind state (LOCAL HOME spec section 5)"
238
+ },
239
+ {
240
+ "id": "extensions-plugins",
241
+ "legacyHome": ".xeno",
242
+ "match": "plugins/**",
243
+ "action": "copy",
244
+ "target": {
245
+ "kind": "data",
246
+ "area": "extensions",
247
+ "segments": [
248
+ "plugins",
249
+ "{path}"
250
+ ]
251
+ },
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
+ },
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
+ },
269
+ {
270
+ "id": "extensions-mcp-servers",
271
+ "legacyHome": ".xeno",
272
+ "match": "mcp/servers.json",
273
+ "action": "copy",
274
+ "target": {
275
+ "kind": "config",
276
+ "area": "extensions",
277
+ "segments": [
278
+ "mcp",
279
+ "servers.json"
280
+ ]
281
+ },
282
+ "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}; MCP server definitions and policies are settings"
283
+ },
284
+ {
285
+ "id": "extensions-mcp-sdk-approvals",
286
+ "legacyHome": ".xeno",
287
+ "match": "mcp/sdk-approvals.json",
288
+ "action": "copy",
289
+ "target": {
290
+ "kind": "config",
291
+ "area": "extensions",
292
+ "segments": [
293
+ "mcp",
294
+ "sdk-approvals.json"
295
+ ]
296
+ },
297
+ "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}; hash-bound approvals for SDK-configured servers are settings"
298
+ },
299
+ {
300
+ "id": "extensions-mcp-approval-log",
301
+ "legacyHome": ".xeno",
302
+ "match": "mcp/approvals.jsonl",
303
+ "action": "copy",
304
+ "target": {
305
+ "kind": "data",
306
+ "area": "extensions",
307
+ "segments": [
308
+ "mcp",
309
+ "approvals.jsonl"
310
+ ]
311
+ },
312
+ "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}; the approval audit history cannot be re-created"
238
313
  }
239
314
  ]
240
315
  };
@@ -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";
@@ -17,18 +17,89 @@ function globMatch(pattern, name) {
17
17
  const re = new RegExp('^' + pattern.split('*').map((s) => s.replace(/[.+?^${}()|[\]\\]/g, '\\$&')).join('.*') + '$');
18
18
  return re.test(name);
19
19
  }
20
+ /**
21
+ * A rule whose `match` ends in `/**` matches every file below that prefix, at any depth (an installed plugin is a
22
+ * tree). Its target names the remainder with one whole `{path}` segment. Links are still never followed.
23
+ */
24
+ const TREE = '**';
25
+ const PATH_TOKEN = '{path}';
20
26
  function matchRule(rule, rel) {
21
27
  const want = rule.match.split('/');
22
28
  const got = rel.split('/');
29
+ if (want[want.length - 1] === TREE) {
30
+ const prefix = want.slice(0, -1);
31
+ if (got.length <= prefix.length)
32
+ return null;
33
+ for (let i = 0; i < prefix.length; i++)
34
+ if (prefix[i] !== got[i])
35
+ return null;
36
+ return { file: got[got.length - 1], rest: got.slice(prefix.length) };
37
+ }
23
38
  if (want.length !== got.length)
24
39
  return null;
25
40
  for (let i = 0; i < want.length - 1; i++)
26
41
  if (want[i] !== got[i])
27
42
  return null;
28
43
  const last = got[got.length - 1];
29
- return globMatch(want[want.length - 1], last) ? { file: last } : null;
44
+ return globMatch(want[want.length - 1], last) ? { file: last, rest: [] } : null;
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;
30
87
  }
31
88
  const fill = (s, file) => s.split('{file}').join(file);
89
+ /** A target's segments with `{file}` filled and a whole `{path}` segment expanded into the matched remainder. */
90
+ const expand = (segments, m) => segments.flatMap((x) => (x === PATH_TOKEN ? m.rest : [fill(x, m.file)]));
91
+ function assertRuleShape(r) {
92
+ const parts = r.match.split('/');
93
+ const tree = parts[parts.length - 1] === TREE;
94
+ if (parts.slice(0, -1).includes(TREE))
95
+ throw new DirsError('invalid-argument', `rule ${r.id}: '**' is allowed only as the last segment of match`);
96
+ if (!tree)
97
+ return;
98
+ if (r.action !== 'copy')
99
+ throw new DirsError('invalid-argument', `rule ${r.id}: a tree rule ('/**') must copy; a secret is one file`);
100
+ if (r.target.segments.filter((x) => x === PATH_TOKEN).length !== 1)
101
+ throw new DirsError('invalid-argument', `rule ${r.id}: a tree rule names the remainder with exactly one '{path}' segment`);
102
+ }
32
103
  /** Deterministic id: the same plan (rules, sources, destinations) always has the same journal. */
33
104
  export function planId(entries) {
34
105
  const lines = entries.map((e) => `${e.rule}\t${e.legacyHome}\t${e.sourceRel}\t${e.destRel}`).sort().join('\n');
@@ -47,10 +118,13 @@ export function planMigration(options) {
47
118
  if (seen.has(r.id))
48
119
  throw new DirsError('invalid-argument', `duplicate migration rule id: ${r.id}`);
49
120
  seen.add(r.id);
121
+ assertRuleShape(r);
50
122
  }
51
123
  const names = options.legacyHomes ?? INPUTS_JSON.legacyHomes;
52
124
  const homes = [];
53
125
  const entries = [];
126
+ /** The destination tree of each entry a tree rule produced. */
127
+ const trees = new Map();
54
128
  const refused = [];
55
129
  const unplaced = { count: 0, sample: [], truncated: false };
56
130
  let visited = 0;
@@ -127,9 +201,13 @@ export function planMigration(options) {
127
201
  const { rule, m } = hits[0];
128
202
  try {
129
203
  if (rule.action === 'copy') {
130
- const segs = rule.target.segments.map((x) => fill(x, m.file));
204
+ const segs = expand(rule.target.segments, m);
131
205
  const dest = options.dirs.path(rule.target.kind, rule.target.area, ...segs);
132
- 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);
133
211
  }
134
212
  else {
135
213
  const sn = fill(rule.target.name, m.file);
@@ -219,13 +297,42 @@ export function planMigration(options) {
219
297
  const byDest = new Map();
220
298
  for (const e of entries)
221
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
+ }
222
322
  const clean = [];
223
323
  for (const group of byDest.values()) {
224
- if (group.length === 1)
225
- clean.push(group[0]);
226
- else
324
+ if (group.length > 1) {
227
325
  for (const e of group)
228
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);
229
336
  }
230
337
  clean.sort((a, b) => { const x = a.legacyHome + a.sourceRel, y = b.legacyHome + b.sourceRel; return x < y ? -1 : x > y ? 1 : 0; });
231
338
  return { id: planId(clean), homes, entries: clean, refused, unplaced };
@@ -217,7 +217,7 @@ export const INPUTS_JSON = {
217
217
  "$comment2": "secretsKeyService/Account name the single OS-store entry that holds the per-machine master key (spec section 6). The service equals the locked bundle identifier."
218
218
  };
219
219
  export const LEGACY_MAP_JSON = {
220
- "$comment": "Built-in migration rules: legacy file -> registered location. Deliberately minimal: only mappings the ecosystem documents explicitly. Each product owner supplies the rest as rules; anything unmatched is reported as unplaced, never guessed (XENO LOCAL HOME - SPEC.md section 10.6). A 'copy' rule names kind+area+segments; a 'secret' rule names area+name and needs a secret sink. '{file}' stands for the matched file name; only the last path segment of 'match' may contain '*'.",
220
+ "$comment": "Built-in migration rules: legacy file -> registered location. Deliberately minimal: only mappings the ecosystem documents explicitly. Each product owner supplies the rest as rules; anything unmatched is reported as unplaced, never guessed (XENO LOCAL HOME - SPEC.md section 10.6). A 'copy' rule names kind+area+segments; a 'secret' rule names area+name and needs a secret sink. '{file}' stands for the matched file name; only the last path segment of 'match' may contain '*'. A 'match' ending in '/**' (0.3.1) is a tree rule: it matches every file below that prefix at any depth, must be a copy, and names the remainder with exactly one whole '{path}' target segment; links inside the tree are refused, never followed.",
221
221
  "rules": [
222
222
  {
223
223
  "id": "shell-window-state",
@@ -232,6 +232,81 @@ export const LEGACY_MAP_JSON = {
232
232
  ]
233
233
  },
234
234
  "source": "xeno-primitives/ADOPTION.md, xeno-shell row (~/.xeno/shell/state.json -> apps/shell); window state is kind state (LOCAL HOME spec section 5)"
235
+ },
236
+ {
237
+ "id": "extensions-plugins",
238
+ "legacyHome": ".xeno",
239
+ "match": "plugins/**",
240
+ "action": "copy",
241
+ "target": {
242
+ "kind": "data",
243
+ "area": "extensions",
244
+ "segments": [
245
+ "plugins",
246
+ "{path}"
247
+ ]
248
+ },
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
+ },
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
+ },
266
+ {
267
+ "id": "extensions-mcp-servers",
268
+ "legacyHome": ".xeno",
269
+ "match": "mcp/servers.json",
270
+ "action": "copy",
271
+ "target": {
272
+ "kind": "config",
273
+ "area": "extensions",
274
+ "segments": [
275
+ "mcp",
276
+ "servers.json"
277
+ ]
278
+ },
279
+ "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}; MCP server definitions and policies are settings"
280
+ },
281
+ {
282
+ "id": "extensions-mcp-sdk-approvals",
283
+ "legacyHome": ".xeno",
284
+ "match": "mcp/sdk-approvals.json",
285
+ "action": "copy",
286
+ "target": {
287
+ "kind": "config",
288
+ "area": "extensions",
289
+ "segments": [
290
+ "mcp",
291
+ "sdk-approvals.json"
292
+ ]
293
+ },
294
+ "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}; hash-bound approvals for SDK-configured servers are settings"
295
+ },
296
+ {
297
+ "id": "extensions-mcp-approval-log",
298
+ "legacyHome": ".xeno",
299
+ "match": "mcp/approvals.jsonl",
300
+ "action": "copy",
301
+ "target": {
302
+ "kind": "data",
303
+ "area": "extensions",
304
+ "segments": [
305
+ "mcp",
306
+ "approvals.jsonl"
307
+ ]
308
+ },
309
+ "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}; the approval audit history cannot be re-created"
235
310
  }
236
311
  ]
237
312
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xenosystem/primitives",
3
- "version": "0.3.0",
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",