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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#!/usr/bin/env node
import { Project } from 'ts-morph';
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const PKG = path.resolve(__dirname, '..');
const SRC = path.join(PKG, 'src');
const ENTRY = path.join(SRC, 'index.ts');

const args = process.argv.slice(2);
const DRY = args.includes('--dry-run');
const ONLY = (args.find((a) => a.startsWith('--only=')) || '').slice('--only='.length) || null;
const ENTRY_ONLY = args.includes('--entry');
const DELETE_BARRELS = args.includes('--delete-barrels');
const LIST_REGS = args.includes('--list-registers');
const VERIFY = args.includes('--verify-coverage');

const project = new Project({ tsConfigFilePath: path.join(PKG, 'tsconfig.json') });

const relSpec = (absFile) =>
  '#/' + path.relative(SRC, absFile).split(path.sep).join('/').replace(/\.ts$/, '');

const isUnderSrc = (abs) => abs === SRC || abs.startsWith(SRC + path.sep);
const isIndexBasename = (abs) => path.basename(abs) === 'index.ts';
const isBarrelFile = (sf) => {
  const f = sf.getFilePath();
  return isUnderSrc(f) && isIndexBasename(f) && f !== ENTRY;
};
const resolvedFile = (decl) => decl.getModuleSpecifierSourceFile() || null;
const barrelOfDecl = (decl) => {
  const sf = resolvedFile(decl);
  return sf && isBarrelFile(sf) ? sf : null;
};

function resolveName(barrel, name) {
  const decls = barrel.getExportedDeclarations().get(name);
  if (!decls || decls.length === 0) return null;
  const decl = decls[0];
  const leaf = decl.getSourceFile();
  const sym = decl.getSymbol();
  let leafName = null;
  for (const [ln, lds] of leaf.getExportedDeclarations()) {
    if (lds.some((d) => d.getSymbol() === sym)) {
      leafName = ln;
      break;
    }
  }
  if (leafName === null) leafName = (sym && sym.getName()) || name;
  return { leafFile: leaf.getFilePath(), leafName };
}

function expandBarrelClauses(barrel) {
  const clauses = [];
  for (const ed of barrel.getExportDeclarations()) {
    const target = resolvedFile(ed);
    if (!target) continue;
    if (isBarrelFile(target)) {
      clauses.push(...expandBarrelClauses(target));
      continue;
    }
    const file = target.getFilePath();
    const isStar =
      ed.getNamedExports().length === 0 && !ed.getNamespaceExport();
    if (isStar) {
      clauses.push({ kind: 'star', file });
    } else if (ed.getNamespaceExport()) {
      clauses.push({ kind: 'namespace', file, name: ed.getNamespaceExport().getName() });
    } else {
      const declType = ed.isTypeOnly();
      const specs = ed.getNamedExports().map((s) => ({
        name: s.getName(),
        alias: s.getAliasNode()?.getText(),
        isTypeOnly: declType || s.isTypeOnly(),
      }));
      clauses.push({ kind: 'named', file, isTypeOnly: declType, specs });
    }
  }
  return clauses;
}

function allLeavesUnderDir(dirAbs) {
  const out = [];
  const walk = (d) => {
    for (const ent of fs.readdirSync(d, { withFileTypes: true })) {
      const p = path.join(d, ent.name);
      if (ent.isDirectory()) walk(p);
      else if (
        ent.isFile() &&
        p.endsWith('.ts') &&
        !p.endsWith('.test.ts') &&
        !p.endsWith('.d.ts') &&
        path.basename(p) !== 'index.ts'
      ) {
        out.push(p);
      }
    }
  };
  walk(dirAbs);
  return out.sort((a, b) => a.localeCompare(b));
}

function rewriteConsumerFile(sf, onlyBarrelPath) {
  const report = { imports: 0, exports: 0, manuals: [], sideEffects: 0 };

  for (const decl of sf.getImportDeclarations()) {
    const barrel = barrelOfDecl(decl);
    if (!barrel) continue;
    if (onlyBarrelPath && barrel.getFilePath() !== onlyBarrelPath) continue;

    if (decl.getNamespaceImport()) {
      report.manuals.push({ sf: sf.getFilePath(), text: decl.getText(), why: 'namespace import' });
      continue;
    }
    const hasDefault = !!decl.getDefaultImport();
    const named = decl.getNamedImports();
    if (!hasDefault && named.length === 0) {
      const leaves = [...new Set(expandBarrelClauses(barrel).map((c) => c.file))];
      const idx = sf.getImportDeclarations().indexOf(decl);
      sf.insertImportDeclarations(
        idx,
        leaves.map((leaf) => ({ moduleSpecifier: relSpec(leaf) })),
      );
      decl.remove();
      report.sideEffects += leaves.length;
      report.imports++;
      continue;
    }

    const declType = decl.isTypeOnly();
    const groups = new Map();
    const add = (leaf, spec) => {
      if (!groups.has(leaf)) groups.set(leaf, []);
      groups.get(leaf).push(spec);
    };

    if (hasDefault) {
      const r = resolveName(barrel, 'default');
      if (!r) report.manuals.push({ sf: sf.getFilePath(), text: decl.getText(), why: 'default import unresolved' });
      else add(r.leafFile, { default: decl.getDefaultImport().getText() });
    }
    for (const s of named) {
      const lookup = s.getName();
      const local = s.getAliasNode()?.getText() || s.getName();
      const r = resolveName(barrel, lookup);
      if (!r) {
        report.manuals.push({ sf: sf.getFilePath(), text: s.getText(), why: 'named import unresolved' });
        continue;
      }
      add(r.leafFile, {
        name: r.leafName,
        alias: local !== r.leafName ? local : undefined,
        isTypeOnly: declType || s.isTypeOnly(),
      });
    }
    const structures = buildImportStructures(groups);
    const idx = sf.getImportDeclarations().indexOf(decl);
    if (structures.length) sf.insertImportDeclarations(idx, structures);
    decl.remove();
    report.imports++;
  }

  for (const decl of sf.getExportDeclarations()) {
    const barrel = barrelOfDecl(decl);
    if (!barrel) continue;
    if (onlyBarrelPath && barrel.getFilePath() !== onlyBarrelPath) continue;

    const isStar = decl.getNamedExports().length === 0 && !decl.getNamespaceExport();
    if (isStar) {
      const clauses = expandBarrelClauses(barrel);
      decl.replaceWithText(clauses.map(exportClauseToText).join('\n'));
      report.exports++;
      continue;
    }
    if (decl.getNamespaceExport()) {
      report.manuals.push({ sf: sf.getFilePath(), text: decl.getText(), why: 'namespace export' });
      continue;
    }
    const declType = decl.isTypeOnly();
    const groups = new Map();
    for (const s of decl.getNamedExports()) {
      const lookup = s.getName();
      const exportedAs = s.getAliasNode()?.getText() || s.getName();
      const r = resolveName(barrel, lookup);
      if (!r) {
        report.manuals.push({ sf: sf.getFilePath(), text: s.getText(), why: 'named export unresolved' });
        continue;
      }
      if (!groups.has(r.leafFile)) groups.set(r.leafFile, { specs: [], allType: true });
      const g = groups.get(r.leafFile);
      const t = declType || s.isTypeOnly();
      g.allType = g.allType && t;
      g.specs.push({
        name: r.leafName,
        alias: exportedAs !== r.leafName ? exportedAs : undefined,
        isTypeOnly: t,
      });
    }
    const lines = [];
    for (const [leaf, { specs, allType }] of groups) {
      lines.push(renderNamedExport(relSpec(leaf), specs, allType));
    }
    decl.replaceWithText(lines.join('\n'));
    report.exports++;
  }
  return report;
}

function buildImportStructures(groups) {
  const structures = [];
  for (const [leaf, specs] of groups) {
    const spec = relSpec(leaf);
    const defaults = specs.filter((s) => s.default);
    const namedSpecs = specs.filter((s) => !s.default);
    for (const d of defaults) {
      structures.push({ moduleSpecifier: spec, defaultImport: d.default });
    }
    const values = namedSpecs.filter((s) => !s.isTypeOnly);
    const types = namedSpecs.filter((s) => s.isTypeOnly);
    if (values.length)
      structures.push({
        moduleSpecifier: spec,
        namedImports: values.map((v) => ({ name: v.name, alias: v.alias })),
      });
    if (types.length)
      structures.push({
        moduleSpecifier: spec,
        isTypeOnly: true,
        namedImports: types.map((t) => ({ name: t.name, alias: t.alias })),
      });
  }
  return structures;
}

function renderNamedExport(spec, specs, allType) {
  const body = specs
    .map((s) => `${allType ? '' : s.isTypeOnly ? 'type ' : ''}${s.name}${s.alias ? ' as ' + s.alias : ''}`)
    .join(', ');
  return `${allType ? 'export type' : 'export'} { ${body} } from '${spec}';`;
}

function exportClauseToText(c) {
  if (c.kind === 'star') return `export * from '${relSpec(c.file)}';`;
  if (c.kind === 'namespace') return `export * as ${c.name} from '${relSpec(c.file)}';`;
  return renderNamedExport(relSpec(c.file), c.specs, c.isTypeOnly);
}

function regenerateEntry() {
  const entrySf = project.getSourceFileOrThrow(ENTRY);
  const original = entrySf.getFullText();
  const headerMatch = original.match(/^\s*\/\*\*[\s\S]*?\*\//);
  const header = headerMatch ? headerMatch[0] : '/** agent-core-v2 public surface. */';

  const refs = [];
  for (const decl of [...entrySf.getExportDeclarations(), ...entrySf.getImportDeclarations()]) {
    const barrel = barrelOfDecl(decl);
    if (!barrel) continue;
    let mode;
    if (decl.getKindName() === 'ImportDeclaration') mode = 'side';
    else {
      const isStar = decl.getNamedExports().length === 0 && !decl.getNamespaceExport();
      mode = isStar ? 'star' : 'named';
    }
    refs.push({ decl, barrel, mode });
  }

  const publicLines = [];
  const loadingLines = [];
  const processed = new Set();

  for (const { decl, barrel, mode } of refs) {
    const bf = barrel.getFilePath();
    const dirAbs = path.dirname(bf);
    const allLeaves = allLeavesUnderDir(dirAbs);
    const clauses = expandBarrelClauses(barrel);
    const starLeaves = new Set(clauses.filter((c) => c.kind === 'star').map((c) => c.file));

    if (mode === 'star') {
      for (const c of clauses) publicLines.push(exportClauseToText(c));
    } else if (mode === 'named') {
      const declType = decl.isTypeOnly();
      const groups = new Map();
      for (const s of decl.getNamedExports()) {
        const lookup = s.getName();
        const exportedAs = s.getAliasNode()?.getText() || s.getName();
        const r = resolveName(barrel, lookup);
        if (!r) continue;
        if (!groups.has(r.leafFile)) groups.set(r.leafFile, { specs: [], allType: true });
        const g = groups.get(r.leafFile);
        const t = declType || s.isTypeOnly();
        g.allType = g.allType && t;
        g.specs.push({
          name: r.leafName,
          alias: exportedAs !== r.leafName ? exportedAs : undefined,
          isTypeOnly: t,
        });
      }
      for (const [leaf, { specs, allType }] of groups) {
        publicLines.push(renderNamedExport(relSpec(leaf), specs, allType));
      }
    }
    for (const leaf of allLeaves) {
      const key = leaf;
      if (starLeaves.has(leaf)) continue;
      if (processed.has(key)) continue;
      processed.add(key);
      loadingLines.push(`import '${relSpec(leaf)}';`);
    }
  }

  const body = [
    header,
    '',
    '// Public surface — precise re-exports of each domain leaf (no barrels).',
    ...publicLines,
    '',
    '// Side-effect loading — ensure every domain leaf (and its top-level',
    '// `register*` calls) is evaluated when the package is imported.',
    ...loadingLines,
    '',
  ].join('\n');

  if (!DRY) fs.writeFileSync(ENTRY, body);
  return { publicLines: publicLines.length, loadingLines: loadingLines.length };
}

const REGISTER_NAMES = new Set([
  'registerScopedService',
  'registerAgentToolService',
  'registerErrorDomain',
  'registerConfigSection',
  'registerAgentProfile',
  'registerFlagDefinition',
]);

function isModuleScoped(call) {
  let n = call.getParent();
  while (n) {
    const k = n.getKindName();
    if (
      k === 'FunctionDeclaration' ||
      k === 'FunctionExpression' ||
      k === 'ArrowFunction' ||
      k === 'MethodDeclaration' ||
      k === 'Constructor' ||
      k === 'ClassDeclaration'
    ) {
      return false;
    }
    n = n.getParent();
  }
  return true;
}

function findRegisterFiles() {
  const files = [];
  for (const sf of project.getSourceFiles()) {
    const f = sf.getFilePath();
    if (!isUnderSrc(f) || f.endsWith('.test.ts')) continue;
    let hit = false;
    sf.forEachDescendant((node) => {
      if (hit) return;
      if (node.getKindName() !== 'CallExpression') return;
      const expr = node.getExpression();
      if (expr.getKindName() !== 'Identifier') return;
      if (!REGISTER_NAMES.has(expr.getText())) return;
      if (isModuleScoped(node)) hit = true;
    });
    if (hit) files.push(f);
  }
  return files.sort();
}

function reachedFromEntry() {
  const reached = new Set();
  const visit = (sf) => {
    const f = sf.getFilePath();
    if (reached.has(f)) return;
    reached.add(f);
    if (!isUnderSrc(f)) return;
    const edges = [...sf.getImportDeclarations(), ...sf.getExportDeclarations()];
    for (const d of edges) {
      if (d.isTypeOnly && d.isTypeOnly()) continue;
      const t = resolvedFile(d);
      if (t && isUnderSrc(t.getFilePath())) visit(t);
    }
  };
  visit(project.getSourceFileOrThrow(ENTRY));
  return reached;
}

function verifyCoverage() {
  const regs = findRegisterFiles();
  const reached = reachedFromEntry();
  const missing = regs.filter((f) => !reached.has(f));
  console.log(`register files: ${regs.length}; reachable from entry: ${reached.size}; missing: ${missing.length}`);
  if (missing.length) {
    console.log('MISSING (not reachable from src/index.ts):');
    for (const m of missing) console.log('  ' + path.relative(PKG, m));
    return false;
  }
  return true;
}

function deleteBarrels() {
  let n = 0;
  for (const sf of project.getSourceFiles()) {
    if (!isBarrelFile(sf)) continue;
    const f = sf.getFilePath();
    if (!DRY) fs.unlinkSync(f);
    n++;
  }
  return n;
}

function main() {
  if (LIST_REGS) {
    for (const f of findRegisterFiles()) console.log(path.relative(PKG, f));
    return;
  }
  if (VERIFY) {
    const ok = verifyCoverage();
    process.exit(ok ? 0 : 1);
  }
  if (ENTRY_ONLY) {
    const r = regenerateEntry();
    console.log(`entry regenerated: ${r.publicLines} public lines, ${r.loadingLines} loading lines${DRY ? ' (dry-run)' : ''}`);
    return;
  }

  let onlyBarrelPath = null;
  if (ONLY) {
    onlyBarrelPath = path.join(SRC, ONLY, 'index.ts');
    if (!fs.existsSync(onlyBarrelPath)) {
      console.error(`--only target not a barrel: ${path.relative(PKG, onlyBarrelPath)}`);
      process.exit(2);
    }
  }

  const totals = { files: 0, imports: 0, exports: 0, sideEffects: 0, manuals: [] };
  for (const sf of project.getSourceFiles()) {
    const f = sf.getFilePath();
    if (!isUnderSrc(f) && !f.startsWith(path.join(PKG, 'test') + path.sep)) continue;
    if (f === ENTRY) continue;
    const before = sf.getFullText();
    const r = rewriteConsumerFile(sf, onlyBarrelPath);
    if (sf.getFullText() !== before) {
      totals.files++;
      totals.imports += r.imports;
      totals.exports += r.exports;
      totals.sideEffects += r.sideEffects;
    }
    totals.manuals.push(...r.manuals);
  }

  if (!DRY) project.saveSync();

  console.log(
    `rewrote ${totals.files} files: ${totals.imports} barrel imports, ${totals.exports} barrel exports, ${totals.sideEffects} side-effect loads${DRY ? ' (dry-run)' : ''}`,
  );
  if (totals.manuals.length) {
    console.log(`MANUAL (${totals.manuals.length}) — could not auto-split:`);
    for (const m of totals.manuals)
      console.log(`  ${path.relative(PKG, m.sf)} :: ${m.why} :: ${m.text.replace(/\s+/g, ' ').slice(0, 120)}`);
  }

  if (DELETE_BARRELS) {
    const n = deleteBarrels();
    console.log(`deleted ${n} domain barrels${DRY ? ' (dry-run)' : ''}`);
  }
}

main();