@memnexus-ai/mx-agent-cli 0.1.257 → 0.1.259

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.
@@ -106,6 +106,15 @@ function writeManagedFile(destPath, content, mode) {
106
106
 
107
107
  // ── FAIL-CLOSED symlink ban (PO directive 2026-07-20) ───────────────────────
108
108
 
109
+ /**
110
+ * The ONE debris shape the destination scan tolerates: the directory Claude Code
111
+ * parks agent worktrees in, directly under `.claude/`. Everything below it is
112
+ * another checkout's business — never generator input, never agent config, never
113
+ * executed. Matched by NAME and POSITION only; see the SHELTER note below for why
114
+ * this is not matched by a `.git` marker.
115
+ */
116
+ const DEST_DEBRIS_DIR = 'worktrees';
117
+
109
118
  /**
110
119
  * Recursively assert that NO entry under `dir` is a symlink. Throws FAIL-CLOSED
111
120
  * on the first symlink found (file OR directory link — a directory symlink is
@@ -113,22 +122,65 @@ function writeManagedFile(destPath, content, mode) {
113
122
  * over the whole source tree BEFORE any write, so a smuggled link aborts the run
114
123
  * with nothing materialized.
115
124
  *
125
+ * TRAVERSAL SCOPE (KI-GENERATOR-SCAN-WALKS-FOREIGN-TREES). The scan has two
126
+ * callers with different subjects, and they must not share a traversal policy:
127
+ *
128
+ * • SOURCE (`mx-agent-system/agent-config/`, or a `git archive` of it) — the
129
+ * escape-vector protection. Scan EVERYTHING, no skips. A symlink here can be
130
+ * emitted or followed into the generated tree, so it must fail closed.
131
+ * • EXISTING DESTINATION (`.claude/`) — scanned only because carry-over copies
132
+ * unmanaged entries across the swap. `.claude/` is not a curated tree: it
133
+ * accumulates foreign debris, notably stale Claude Code agent worktrees under
134
+ * `.claude/worktrees/agent-<id>`, each of which is a full checkout with its own
135
+ * generated `.claude/` inside. Scanning INTO that debris made one orphaned
136
+ * file in the main repo hard-block every session start that needed to
137
+ * regenerate `.claude/` (customer-portal, 2026-08-01). The skipped subtree is
138
+ * still carried over verbatim by cpSync, which does not follow symlinks
139
+ * (`dereference: false`), so nothing is followed and nothing is lost.
140
+ *
141
+ * SHELTER (SF-13 / SF-14, security review of #4818). The dest skip is keyed on
142
+ * NAME + POSITION (`<dest>/worktrees`), NOT on a `.git` marker. A marker-keyed
143
+ * skip is writable by anything that can write under `.claude/`: planting a `.git`
144
+ * file next to a symlink exempted that symlink from the scan. That is not
145
+ * academic — in DEGRADE mode (`skipPermissionLayer:true`, taken by
146
+ * generateClaudeConfigForBoot when the origin fetch fails: fresh clone, offline)
147
+ * `hooks/` is NOT regenerated into staging, so carry-over copies the EXISTING
148
+ * `hooks/` wholesale, and an unscanned link lands live in `.claude/hooks/`, which
149
+ * settings.json executes. Name+position is not plantable that way: `worktrees` is
150
+ * one fixed name at one fixed depth, and MANAGED_SUBDIRS can never take it.
151
+ *
116
152
  * @param dir absolute path to scan
117
153
  * @param rootRel human-readable root label for error messages (e.g. 'base')
154
+ * @param [opts] {{ treeLabel?: string, skipForeignSubtrees?: boolean }}
155
+ * treeLabel: which tree the message names ('source tree' by
156
+ * default) — a diagnostic that names the wrong tree sends the
157
+ * next reader to the wrong place. skipForeignSubtrees: skip the
158
+ * top-level `worktrees/` debris dir (DEST ONLY).
159
+ * @param [depth] recursion depth; 0 at the scanned root.
118
160
  */
119
- function assertNoSymlinksInTree(dir, rootRel) {
161
+ function assertNoSymlinksInTree(dir, rootRel, opts = {}, depth = 0) {
162
+ const treeLabel = opts.treeLabel ?? 'source tree';
163
+ const skipForeign = Boolean(opts.skipForeignSubtrees);
120
164
  for (const ent of listDir(dir)) {
121
165
  const p = join(dir, ent.name);
122
166
  if (ent.isSymbolicLink()) {
123
167
  const rel = relative(dir, p);
124
168
  throw new Error(
125
- `generate-claude: FAIL-CLOSED — symlink found in source tree at ` +
169
+ `generate-claude: FAIL-CLOSED — symlink found in ${treeLabel} at ` +
126
170
  `${rootRel}/${rel || ent.name}; the generator refuses to emit or follow ` +
127
171
  `symlinks. Replace it with a plain file.`,
128
172
  );
129
173
  }
130
174
  if (ent.isDirectory()) {
131
- assertNoSymlinksInTree(p, `${rootRel}/${ent.name}`);
175
+ // The single tolerated debris shape: `<dest>/worktrees` and everything
176
+ // under it. Depth 0 only, exact name, and never a managed dir (belt and
177
+ // braces — MANAGED_SUBDIRS does not contain it today and must not later,
178
+ // since a managed dir carried over in degrade mode goes live unscanned).
179
+ const isDebrisDir = depth === 0
180
+ && ent.name === DEST_DEBRIS_DIR
181
+ && !MANAGED_SUBDIRS.includes(ent.name);
182
+ if (skipForeign && isDebrisDir) continue;
183
+ assertNoSymlinksInTree(p, `${rootRel}/${ent.name}`, opts, depth + 1);
132
184
  }
133
185
  }
134
186
  }
@@ -251,6 +303,13 @@ export function sweepOrphanSiblings(destRoot) {
251
303
  * the existing dest is a symlink — carry-over copies plain files only, and the
252
304
  * swap must never move a smuggled link into place. The real committed `.claude/`
253
305
  * is symlink-free, so this is a no-op in the field.
306
+ *
307
+ * EXCEPT inside the `worktrees/` debris dir: see assertNoSymlinksInTree's
308
+ * TRAVERSAL SCOPE and SHELTER notes.
309
+ * It is copied verbatim without being followed — cpSync defaults to
310
+ * `dereference: false`, so a link inside debris is copied AS a link and is never
311
+ * read through. It is preserved, not honoured, and never enters the config the
312
+ * generator emits.
254
313
  */
255
314
  function carryOverUnmanaged(destRoot, staging) {
256
315
  let st;
@@ -261,7 +320,10 @@ function carryOverUnmanaged(destRoot, staging) {
261
320
  `refusing to carry over or swap through it.`,
262
321
  );
263
322
  }
264
- assertNoSymlinksInTree(destRoot, '.claude');
323
+ assertNoSymlinksInTree(destRoot, '.claude', {
324
+ treeLabel: 'existing destination tree',
325
+ skipForeignSubtrees: true,
326
+ });
265
327
  for (const ent of listDir(destRoot)) {
266
328
  if (existsSync(join(staging, ent.name))) continue; // generator-managed — its version wins
267
329
  cpSync(join(destRoot, ent.name), join(staging, ent.name), { recursive: true });
@@ -629,7 +691,7 @@ export function generateClaudeConfig(opts) {
629
691
 
630
692
  // Defense-in-depth: the generated staging tree must contain zero symlinks
631
693
  // (generation only emits plain files). Fail closed BEFORE the swap if not.
632
- assertNoSymlinksInTree(staging, '.claude.staging');
694
+ assertNoSymlinksInTree(staging, '.claude.staging', { treeLabel: 'generated staging tree' });
633
695
 
634
696
  // Test-only crash injection (Change 3): simulate a failure between a fully
635
697
  // generated staging tree and the swap, to prove the existing `.claude/` is