@glasstrace/sdk 0.19.0 → 0.20.0

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/cli/init.cjs CHANGED
@@ -755,10 +755,10 @@ function mergeDefs(...defs) {
755
755
  function cloneDef(schema) {
756
756
  return mergeDefs(schema._zod.def);
757
757
  }
758
- function getElementAtPath(obj, path9) {
759
- if (!path9)
758
+ function getElementAtPath(obj, path10) {
759
+ if (!path10)
760
760
  return obj;
761
- return path9.reduce((acc, key) => acc?.[key], obj);
761
+ return path10.reduce((acc, key) => acc?.[key], obj);
762
762
  }
763
763
  function promiseAllObject(promisesObj) {
764
764
  const keys = Object.keys(promisesObj);
@@ -1070,11 +1070,11 @@ function aborted(x, startIndex = 0) {
1070
1070
  }
1071
1071
  return false;
1072
1072
  }
1073
- function prefixIssues(path9, issues) {
1073
+ function prefixIssues(path10, issues) {
1074
1074
  return issues.map((iss) => {
1075
1075
  var _a2;
1076
1076
  (_a2 = iss).path ?? (_a2.path = []);
1077
- iss.path.unshift(path9);
1077
+ iss.path.unshift(path10);
1078
1078
  return iss;
1079
1079
  });
1080
1080
  }
@@ -1317,7 +1317,7 @@ function formatError(error48, mapper = (issue2) => issue2.message) {
1317
1317
  }
1318
1318
  function treeifyError(error48, mapper = (issue2) => issue2.message) {
1319
1319
  const result = { errors: [] };
1320
- const processError = (error49, path9 = []) => {
1320
+ const processError = (error49, path10 = []) => {
1321
1321
  var _a2, _b;
1322
1322
  for (const issue2 of error49.issues) {
1323
1323
  if (issue2.code === "invalid_union" && issue2.errors.length) {
@@ -1327,7 +1327,7 @@ function treeifyError(error48, mapper = (issue2) => issue2.message) {
1327
1327
  } else if (issue2.code === "invalid_element") {
1328
1328
  processError({ issues: issue2.issues }, issue2.path);
1329
1329
  } else {
1330
- const fullpath = [...path9, ...issue2.path];
1330
+ const fullpath = [...path10, ...issue2.path];
1331
1331
  if (fullpath.length === 0) {
1332
1332
  result.errors.push(mapper(issue2));
1333
1333
  continue;
@@ -1359,8 +1359,8 @@ function treeifyError(error48, mapper = (issue2) => issue2.message) {
1359
1359
  }
1360
1360
  function toDotPath(_path) {
1361
1361
  const segs = [];
1362
- const path9 = _path.map((seg) => typeof seg === "object" ? seg.key : seg);
1363
- for (const seg of path9) {
1362
+ const path10 = _path.map((seg) => typeof seg === "object" ? seg.key : seg);
1363
+ for (const seg of path10) {
1364
1364
  if (typeof seg === "number")
1365
1365
  segs.push(`[${seg}]`);
1366
1366
  else if (typeof seg === "symbol")
@@ -14124,13 +14124,13 @@ function resolveRef(ref, ctx) {
14124
14124
  if (!ref.startsWith("#")) {
14125
14125
  throw new Error("External $ref is not supported, only local refs (#/...) are allowed");
14126
14126
  }
14127
- const path9 = ref.slice(1).split("/").filter(Boolean);
14128
- if (path9.length === 0) {
14127
+ const path10 = ref.slice(1).split("/").filter(Boolean);
14128
+ if (path10.length === 0) {
14129
14129
  return ctx.rootSchema;
14130
14130
  }
14131
14131
  const defsKey = ctx.version === "draft-2020-12" ? "$defs" : "definitions";
14132
- if (path9[0] === defsKey) {
14133
- const key = path9[1];
14132
+ if (path10[0] === defsKey) {
14133
+ const key = path10[1];
14134
14134
  if (!key || !ctx.defs[key]) {
14135
14135
  throw new Error(`Reference not found: ${ref}`);
14136
14136
  }
@@ -15110,11 +15110,11 @@ var init_dist = __esm({
15110
15110
  async function loadFsPath() {
15111
15111
  if (fsPathCache !== void 0) return fsPathCache;
15112
15112
  try {
15113
- const [fs9, path9] = await Promise.all([
15113
+ const [fs10, path10] = await Promise.all([
15114
15114
  import("node:fs/promises"),
15115
15115
  import("node:path")
15116
15116
  ]);
15117
- fsPathCache = { fs: fs9, path: path9 };
15117
+ fsPathCache = { fs: fs10, path: path10 };
15118
15118
  return fsPathCache;
15119
15119
  } catch {
15120
15120
  fsPathCache = null;
@@ -15201,9 +15201,9 @@ var init_anon_key = __esm({
15201
15201
  });
15202
15202
 
15203
15203
  // src/agent-detection/detect.ts
15204
- async function pathExists(path9, mode = import_node_fs.constants.R_OK) {
15204
+ async function pathExists(path10, mode = import_node_fs.constants.R_OK) {
15205
15205
  try {
15206
- await (0, import_promises.access)(path9, mode);
15206
+ await (0, import_promises.access)(path10, mode);
15207
15207
  return true;
15208
15208
  } catch {
15209
15209
  return false;
@@ -15955,6 +15955,207 @@ var init_monorepo = __esm({
15955
15955
  }
15956
15956
  });
15957
15957
 
15958
+ // src/cli/discovery-file.ts
15959
+ function resolveStaticRoot(projectRoot) {
15960
+ if (isSvelteKitProject(projectRoot)) {
15961
+ return {
15962
+ absolutePath: path4.join(projectRoot, "static"),
15963
+ layout: "static"
15964
+ };
15965
+ }
15966
+ return {
15967
+ absolutePath: path4.join(projectRoot, "public"),
15968
+ layout: "public"
15969
+ };
15970
+ }
15971
+ function isSvelteKitProject(projectRoot) {
15972
+ const pkgPath = path4.join(projectRoot, "package.json");
15973
+ let isEsm = false;
15974
+ try {
15975
+ const pkgContent = fs4.readFileSync(pkgPath, "utf-8");
15976
+ const parsed = JSON.parse(pkgContent);
15977
+ isEsm = parsed.type === "module";
15978
+ } catch {
15979
+ return false;
15980
+ }
15981
+ if (!isEsm) return false;
15982
+ const svelteConfigJs = path4.join(projectRoot, "svelte.config.js");
15983
+ const svelteConfigTs = path4.join(projectRoot, "svelte.config.ts");
15984
+ const appHtml = path4.join(projectRoot, "src", "app.html");
15985
+ return fs4.existsSync(svelteConfigJs) || fs4.existsSync(svelteConfigTs) || fs4.existsSync(appHtml);
15986
+ }
15987
+ function relativeDiscoveryPath(layout) {
15988
+ return layout === "static" ? "static/.well-known/glasstrace.json" : "public/.well-known/glasstrace.json";
15989
+ }
15990
+ function readExistingDiscoveryFile(filePath) {
15991
+ let raw;
15992
+ try {
15993
+ raw = fs4.readFileSync(filePath, "utf-8");
15994
+ } catch {
15995
+ return null;
15996
+ }
15997
+ let parsed;
15998
+ try {
15999
+ parsed = JSON.parse(raw);
16000
+ } catch {
16001
+ return null;
16002
+ }
16003
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
16004
+ return null;
16005
+ }
16006
+ const obj = parsed;
16007
+ const versionRaw = obj.version;
16008
+ if (typeof versionRaw !== "number" || !Number.isInteger(versionRaw) || versionRaw < 1) {
16009
+ return null;
16010
+ }
16011
+ const keyResult = AnonApiKeySchema.safeParse(obj.key);
16012
+ if (!keyResult.success) {
16013
+ return null;
16014
+ }
16015
+ const extras = {};
16016
+ for (const [k, v] of Object.entries(obj)) {
16017
+ if (k === "version" || k === "key") continue;
16018
+ extras[k] = v;
16019
+ }
16020
+ return { key: keyResult.data, extras };
16021
+ }
16022
+ function serializeDiscoveryPayload(key, extras) {
16023
+ const payload = {
16024
+ version: DISCOVERY_FILE_VERSION,
16025
+ key,
16026
+ ...extras
16027
+ };
16028
+ return JSON.stringify(payload, null, 2) + "\n";
16029
+ }
16030
+ function writeDiscoveryFile(projectRoot, anonKey) {
16031
+ const { absolutePath: staticRoot, layout } = resolveStaticRoot(projectRoot);
16032
+ const wellKnownDir = path4.join(staticRoot, ".well-known");
16033
+ const filePath = path4.join(wellKnownDir, "glasstrace.json");
16034
+ let existingAction;
16035
+ let extras = {};
16036
+ if (fs4.existsSync(filePath)) {
16037
+ const existing = readExistingDiscoveryFile(filePath);
16038
+ if (existing === null) {
16039
+ existingAction = "skipped-foreign";
16040
+ } else if (existing.key === anonKey) {
16041
+ return {
16042
+ action: "skipped-matches",
16043
+ filePath,
16044
+ layout
16045
+ };
16046
+ } else {
16047
+ extras = existing.extras;
16048
+ existingAction = "updated-stale";
16049
+ }
16050
+ } else {
16051
+ existingAction = "created";
16052
+ }
16053
+ const tmpPath = `${filePath}.tmp-${process.pid}`;
16054
+ const needsWindowsReplace = process.platform === "win32" && fs4.existsSync(filePath);
16055
+ const backupPath = needsWindowsReplace ? `${filePath}.bak-${process.pid}` : null;
16056
+ try {
16057
+ fs4.mkdirSync(wellKnownDir, { recursive: true });
16058
+ const payload = serializeDiscoveryPayload(anonKey, extras);
16059
+ fs4.writeFileSync(tmpPath, payload, { encoding: "utf-8" });
16060
+ if (backupPath !== null) {
16061
+ fs4.renameSync(filePath, backupPath);
16062
+ try {
16063
+ fs4.renameSync(tmpPath, filePath);
16064
+ } catch (renameErr) {
16065
+ try {
16066
+ fs4.renameSync(backupPath, filePath);
16067
+ } catch {
16068
+ }
16069
+ throw renameErr;
16070
+ }
16071
+ try {
16072
+ fs4.unlinkSync(backupPath);
16073
+ } catch {
16074
+ }
16075
+ } else {
16076
+ fs4.renameSync(tmpPath, filePath);
16077
+ }
16078
+ return { action: existingAction, filePath, layout };
16079
+ } catch (err) {
16080
+ try {
16081
+ if (fs4.existsSync(tmpPath)) {
16082
+ fs4.unlinkSync(tmpPath);
16083
+ }
16084
+ } catch {
16085
+ }
16086
+ return {
16087
+ action: "failed",
16088
+ filePath,
16089
+ layout,
16090
+ error: err instanceof Error ? err.message : String(err)
16091
+ };
16092
+ }
16093
+ }
16094
+ function removeDiscoveryFile(projectRoot) {
16095
+ const { layout: inferredLayout } = resolveStaticRoot(projectRoot);
16096
+ const layouts = ["public", "static"];
16097
+ const outcomes = [];
16098
+ for (const layout of layouts) {
16099
+ const staticRoot = path4.join(projectRoot, layout);
16100
+ const wellKnownDir = path4.join(staticRoot, ".well-known");
16101
+ const filePath = path4.join(wellKnownDir, "glasstrace.json");
16102
+ let removed = false;
16103
+ try {
16104
+ if (fs4.existsSync(filePath)) {
16105
+ fs4.unlinkSync(filePath);
16106
+ removed = true;
16107
+ }
16108
+ } catch (err) {
16109
+ return {
16110
+ action: "failed",
16111
+ filePath,
16112
+ layout,
16113
+ directoryRemoved: false,
16114
+ error: err instanceof Error ? err.message : String(err)
16115
+ };
16116
+ }
16117
+ let directoryRemoved = false;
16118
+ if (removed) {
16119
+ try {
16120
+ if (fs4.existsSync(wellKnownDir)) {
16121
+ const entries = fs4.readdirSync(wellKnownDir);
16122
+ if (entries.length === 0) {
16123
+ fs4.rmdirSync(wellKnownDir);
16124
+ directoryRemoved = true;
16125
+ }
16126
+ }
16127
+ } catch {
16128
+ }
16129
+ }
16130
+ outcomes.push({ layout, filePath, removed, directoryRemoved });
16131
+ }
16132
+ const removals = outcomes.filter((o) => o.removed);
16133
+ const chosen = (() => {
16134
+ if (removals.length === 0) {
16135
+ return outcomes.find((o) => o.layout === inferredLayout) ?? outcomes[0];
16136
+ }
16137
+ if (removals.length === 1) return removals[0];
16138
+ return removals.find((o) => o.layout === inferredLayout) ?? removals[0];
16139
+ })();
16140
+ const anyDirectoryRemoved = outcomes.some((o) => o.directoryRemoved);
16141
+ return {
16142
+ action: removals.length > 0 ? "removed" : "not-found",
16143
+ filePath: chosen.filePath,
16144
+ layout: chosen.layout,
16145
+ directoryRemoved: chosen.directoryRemoved || anyDirectoryRemoved
16146
+ };
16147
+ }
16148
+ var fs4, path4, DISCOVERY_FILE_VERSION;
16149
+ var init_discovery_file = __esm({
16150
+ "src/cli/discovery-file.ts"() {
16151
+ "use strict";
16152
+ fs4 = __toESM(require("node:fs"), 1);
16153
+ path4 = __toESM(require("node:path"), 1);
16154
+ init_dist();
16155
+ DISCOVERY_FILE_VERSION = 1;
16156
+ }
16157
+ });
16158
+
15958
16159
  // src/cli/uninit.ts
15959
16160
  var uninit_exports = {};
15960
16161
  __export(uninit_exports, {
@@ -16272,24 +16473,24 @@ function processTomlMcpConfig(content) {
16272
16473
  return { action: "removed-section", content: result + "\n" };
16273
16474
  }
16274
16475
  function writeShutdownMarker(projectRoot) {
16275
- const dirPath = path4.join(projectRoot, ".glasstrace");
16276
- if (!fs4.existsSync(dirPath)) {
16476
+ const dirPath = path5.join(projectRoot, ".glasstrace");
16477
+ if (!fs5.existsSync(dirPath)) {
16277
16478
  return false;
16278
16479
  }
16279
- const markerPath = path4.join(dirPath, "shutdown-requested");
16480
+ const markerPath = path5.join(dirPath, "shutdown-requested");
16280
16481
  const tmpPath = `${markerPath}.tmp`;
16281
16482
  const body = JSON.stringify({ requestedAt: (/* @__PURE__ */ new Date()).toISOString() });
16282
16483
  try {
16283
- fs4.writeFileSync(tmpPath, body, { encoding: "utf-8", mode: 384 });
16484
+ fs5.writeFileSync(tmpPath, body, { encoding: "utf-8", mode: 384 });
16284
16485
  try {
16285
- fs4.chmodSync(tmpPath, 384);
16486
+ fs5.chmodSync(tmpPath, 384);
16286
16487
  } catch {
16287
16488
  }
16288
- fs4.renameSync(tmpPath, markerPath);
16489
+ fs5.renameSync(tmpPath, markerPath);
16289
16490
  return true;
16290
16491
  } catch {
16291
16492
  try {
16292
- fs4.unlinkSync(tmpPath);
16493
+ fs5.unlinkSync(tmpPath);
16293
16494
  } catch {
16294
16495
  }
16295
16496
  return false;
@@ -16330,8 +16531,8 @@ async function runUninit(options) {
16330
16531
  summary.push("Wrote .glasstrace/shutdown-requested marker");
16331
16532
  }
16332
16533
  } else {
16333
- const dirPath = path4.join(projectRoot, ".glasstrace");
16334
- if (fs4.existsSync(dirPath)) {
16534
+ const dirPath = path5.join(projectRoot, ".glasstrace");
16535
+ if (fs5.existsSync(dirPath)) {
16335
16536
  summary.push(`${prefix}Would write .glasstrace/shutdown-requested marker`);
16336
16537
  }
16337
16538
  }
@@ -16343,11 +16544,11 @@ async function runUninit(options) {
16343
16544
  try {
16344
16545
  let configHandled = false;
16345
16546
  for (const name of NEXT_CONFIG_NAMES) {
16346
- const configPath = path4.join(projectRoot, name);
16347
- if (!fs4.existsSync(configPath)) {
16547
+ const configPath = path5.join(projectRoot, name);
16548
+ if (!fs5.existsSync(configPath)) {
16348
16549
  continue;
16349
16550
  }
16350
- const content = fs4.readFileSync(configPath, "utf-8");
16551
+ const content = fs5.readFileSync(configPath, "utf-8");
16351
16552
  if (!content.includes("withGlasstraceConfig")) {
16352
16553
  continue;
16353
16554
  }
@@ -16357,7 +16558,7 @@ async function runUninit(options) {
16357
16558
  const cleaned = removeGlasstraceConfigImport(unwrapResult.content);
16358
16559
  const final = cleanLeadingBlankLines(cleaned);
16359
16560
  if (!dryRun) {
16360
- fs4.writeFileSync(configPath, final, "utf-8");
16561
+ fs5.writeFileSync(configPath, final, "utf-8");
16361
16562
  }
16362
16563
  summary.push(`${prefix}Unwrapped withGlasstraceConfig from ${name}`);
16363
16564
  configHandled = true;
@@ -16378,20 +16579,20 @@ async function runUninit(options) {
16378
16579
  );
16379
16580
  }
16380
16581
  try {
16381
- const instrPath = path4.join(projectRoot, "instrumentation.ts");
16382
- if (fs4.existsSync(instrPath)) {
16383
- const content = fs4.readFileSync(instrPath, "utf-8");
16582
+ const instrPath = path5.join(projectRoot, "instrumentation.ts");
16583
+ if (fs5.existsSync(instrPath)) {
16584
+ const content = fs5.readFileSync(instrPath, "utf-8");
16384
16585
  if (content.includes("registerGlasstrace") || content.includes("@glasstrace/sdk")) {
16385
16586
  if (isInitCreatedInstrumentation(content)) {
16386
16587
  if (!dryRun) {
16387
- fs4.unlinkSync(instrPath);
16588
+ fs5.unlinkSync(instrPath);
16388
16589
  }
16389
16590
  summary.push(`${prefix}Deleted instrumentation.ts (init-created)`);
16390
16591
  } else {
16391
16592
  const cleaned = removeRegisterGlasstrace(content);
16392
16593
  if (cleaned !== content) {
16393
16594
  if (!dryRun) {
16394
- fs4.writeFileSync(instrPath, cleaned, "utf-8");
16595
+ fs5.writeFileSync(instrPath, cleaned, "utf-8");
16395
16596
  }
16396
16597
  summary.push(
16397
16598
  `${prefix}Removed registerGlasstrace() from instrumentation.ts`
@@ -16406,10 +16607,10 @@ async function runUninit(options) {
16406
16607
  );
16407
16608
  }
16408
16609
  try {
16409
- const glasstraceDir = path4.join(projectRoot, ".glasstrace");
16410
- if (fs4.existsSync(glasstraceDir)) {
16610
+ const glasstraceDir = path5.join(projectRoot, ".glasstrace");
16611
+ if (fs5.existsSync(glasstraceDir)) {
16411
16612
  if (!dryRun) {
16412
- fs4.rmSync(glasstraceDir, { recursive: true, force: true });
16613
+ fs5.rmSync(glasstraceDir, { recursive: true, force: true });
16413
16614
  }
16414
16615
  summary.push(`${prefix}Removed .glasstrace/ directory`);
16415
16616
  }
@@ -16419,9 +16620,38 @@ async function runUninit(options) {
16419
16620
  );
16420
16621
  }
16421
16622
  try {
16422
- const envPath = path4.join(projectRoot, ".env.local");
16423
- if (fs4.existsSync(envPath)) {
16424
- const content = fs4.readFileSync(envPath, "utf-8");
16623
+ if (dryRun) {
16624
+ for (const previewLayout of ["public", "static"]) {
16625
+ const relPath = relativeDiscoveryPath(previewLayout);
16626
+ const absPath = path5.join(projectRoot, relPath);
16627
+ if (fs5.existsSync(absPath)) {
16628
+ summary.push(`${prefix}Would remove ${relPath}`);
16629
+ }
16630
+ }
16631
+ } else {
16632
+ const result = removeDiscoveryFile(projectRoot);
16633
+ if (result.action === "removed") {
16634
+ const relPath = relativeDiscoveryPath(result.layout);
16635
+ summary.push(`Removed ${relPath}`);
16636
+ if (result.directoryRemoved) {
16637
+ const dirRel = relPath.replace(/\/glasstrace\.json$/, "/");
16638
+ summary.push(`Removed empty ${dirRel}`);
16639
+ }
16640
+ } else if (result.action === "failed") {
16641
+ warnings.push(
16642
+ `Failed to remove ${relativeDiscoveryPath(result.layout)}${result.error !== void 0 ? `: ${result.error}` : ""}`
16643
+ );
16644
+ }
16645
+ }
16646
+ } catch (err) {
16647
+ warnings.push(
16648
+ `Failed to remove discovery file: ${err instanceof Error ? err.message : String(err)}`
16649
+ );
16650
+ }
16651
+ try {
16652
+ const envPath = path5.join(projectRoot, ".env.local");
16653
+ if (fs5.existsSync(envPath)) {
16654
+ const content = fs5.readFileSync(envPath, "utf-8");
16425
16655
  const existingKey = readEnvLocalApiKey(content);
16426
16656
  const hasDevKey = isDevApiKey(existingKey);
16427
16657
  let proceed = true;
@@ -16454,12 +16684,12 @@ async function runUninit(options) {
16454
16684
  const result = filtered.join("\n");
16455
16685
  if (result.trim().length === 0) {
16456
16686
  if (!dryRun) {
16457
- fs4.unlinkSync(envPath);
16687
+ fs5.unlinkSync(envPath);
16458
16688
  }
16459
16689
  summary.push(`${prefix}Deleted .env.local (no remaining entries)`);
16460
16690
  } else {
16461
16691
  if (!dryRun) {
16462
- fs4.writeFileSync(envPath, result, "utf-8");
16692
+ fs5.writeFileSync(envPath, result, "utf-8");
16463
16693
  }
16464
16694
  let devKeyAnnotation = "";
16465
16695
  if (devKeyPath === "interactive-confirmed") {
@@ -16482,9 +16712,9 @@ async function runUninit(options) {
16482
16712
  );
16483
16713
  }
16484
16714
  try {
16485
- const gitignorePath = path4.join(projectRoot, ".gitignore");
16486
- if (fs4.existsSync(gitignorePath)) {
16487
- const content = fs4.readFileSync(gitignorePath, "utf-8");
16715
+ const gitignorePath = path5.join(projectRoot, ".gitignore");
16716
+ if (fs5.existsSync(gitignorePath)) {
16717
+ const content = fs5.readFileSync(gitignorePath, "utf-8");
16488
16718
  const lines = content.split("\n");
16489
16719
  const mcpGitignoreEntries = /* @__PURE__ */ new Set([
16490
16720
  ".glasstrace/",
@@ -16500,12 +16730,12 @@ async function runUninit(options) {
16500
16730
  const result = filtered.join("\n");
16501
16731
  if (result.trim().length === 0) {
16502
16732
  if (!dryRun) {
16503
- fs4.unlinkSync(gitignorePath);
16733
+ fs5.unlinkSync(gitignorePath);
16504
16734
  }
16505
16735
  summary.push(`${prefix}Deleted .gitignore (no remaining entries)`);
16506
16736
  } else {
16507
16737
  if (!dryRun) {
16508
- fs4.writeFileSync(gitignorePath, result, "utf-8");
16738
+ fs5.writeFileSync(gitignorePath, result, "utf-8");
16509
16739
  }
16510
16740
  summary.push(`${prefix}Removed Glasstrace entries from .gitignore`);
16511
16741
  }
@@ -16518,63 +16748,63 @@ async function runUninit(options) {
16518
16748
  }
16519
16749
  try {
16520
16750
  for (const configFile of MCP_CONFIG_FILES) {
16521
- const configPath = path4.join(projectRoot, configFile);
16522
- if (!fs4.existsSync(configPath)) {
16751
+ const configPath = path5.join(projectRoot, configFile);
16752
+ if (!fs5.existsSync(configPath)) {
16523
16753
  continue;
16524
16754
  }
16525
- const content = fs4.readFileSync(configPath, "utf-8");
16755
+ const content = fs5.readFileSync(configPath, "utf-8");
16526
16756
  const result = processJsonMcpConfig(content);
16527
16757
  if (result.action === "deleted") {
16528
16758
  if (!dryRun) {
16529
- fs4.unlinkSync(configPath);
16759
+ fs5.unlinkSync(configPath);
16530
16760
  }
16531
16761
  summary.push(`${prefix}Deleted ${configFile}`);
16532
16762
  } else if (result.action === "removed-key" && result.content !== void 0) {
16533
16763
  if (!dryRun) {
16534
- fs4.writeFileSync(configPath, result.content, "utf-8");
16764
+ fs5.writeFileSync(configPath, result.content, "utf-8");
16535
16765
  }
16536
16766
  summary.push(`${prefix}Removed glasstrace from ${configFile}`);
16537
16767
  }
16538
16768
  }
16539
- const codexConfigPath = path4.join(projectRoot, ".codex", "config.toml");
16540
- if (fs4.existsSync(codexConfigPath)) {
16541
- const content = fs4.readFileSync(codexConfigPath, "utf-8");
16769
+ const codexConfigPath = path5.join(projectRoot, ".codex", "config.toml");
16770
+ if (fs5.existsSync(codexConfigPath)) {
16771
+ const content = fs5.readFileSync(codexConfigPath, "utf-8");
16542
16772
  const tomlResult = processTomlMcpConfig(content);
16543
16773
  if (tomlResult.action === "deleted") {
16544
16774
  if (!dryRun) {
16545
- fs4.unlinkSync(codexConfigPath);
16775
+ fs5.unlinkSync(codexConfigPath);
16546
16776
  }
16547
16777
  summary.push(`${prefix}Deleted .codex/config.toml`);
16548
16778
  } else if (tomlResult.action === "removed-section" && tomlResult.content !== void 0) {
16549
16779
  if (!dryRun) {
16550
- fs4.writeFileSync(codexConfigPath, tomlResult.content, "utf-8");
16780
+ fs5.writeFileSync(codexConfigPath, tomlResult.content, "utf-8");
16551
16781
  }
16552
16782
  summary.push(`${prefix}Removed glasstrace from .codex/config.toml`);
16553
16783
  }
16554
16784
  }
16555
- const hasWindsurfMarkers = fs4.existsSync(path4.join(projectRoot, ".windsurfrules")) || fs4.existsSync(path4.join(projectRoot, ".windsurf"));
16785
+ const hasWindsurfMarkers = fs5.existsSync(path5.join(projectRoot, ".windsurfrules")) || fs5.existsSync(path5.join(projectRoot, ".windsurf"));
16556
16786
  if (hasWindsurfMarkers) {
16557
- const windsurfConfigPath = path4.join(
16787
+ const windsurfConfigPath = path5.join(
16558
16788
  os.homedir(),
16559
16789
  ".codeium",
16560
16790
  "windsurf",
16561
16791
  "mcp_config.json"
16562
16792
  );
16563
- if (fs4.existsSync(windsurfConfigPath)) {
16564
- const content = fs4.readFileSync(windsurfConfigPath, "utf-8");
16793
+ if (fs5.existsSync(windsurfConfigPath)) {
16794
+ const content = fs5.readFileSync(windsurfConfigPath, "utf-8");
16565
16795
  const windsurfResult = processJsonMcpConfig(content);
16566
16796
  const home = os.homedir();
16567
16797
  const displayPath = windsurfConfigPath.startsWith(home) ? "~" + windsurfConfigPath.slice(home.length) : windsurfConfigPath;
16568
16798
  if (windsurfResult.action === "deleted") {
16569
16799
  if (!dryRun) {
16570
- fs4.unlinkSync(windsurfConfigPath);
16800
+ fs5.unlinkSync(windsurfConfigPath);
16571
16801
  }
16572
16802
  summary.push(
16573
16803
  `${prefix}Deleted global Windsurf config (${displayPath})`
16574
16804
  );
16575
16805
  } else if (windsurfResult.action === "removed-key" && windsurfResult.content !== void 0) {
16576
16806
  if (!dryRun) {
16577
- fs4.writeFileSync(windsurfConfigPath, windsurfResult.content, "utf-8");
16807
+ fs5.writeFileSync(windsurfConfigPath, windsurfResult.content, "utf-8");
16578
16808
  }
16579
16809
  summary.push(
16580
16810
  `${prefix}Removed glasstrace from global Windsurf config (${displayPath})`
@@ -16589,21 +16819,21 @@ async function runUninit(options) {
16589
16819
  }
16590
16820
  try {
16591
16821
  for (const infoFile of AGENT_INFO_FILES) {
16592
- const filePath = path4.join(projectRoot, infoFile);
16593
- if (!fs4.existsSync(filePath)) {
16822
+ const filePath = path5.join(projectRoot, infoFile);
16823
+ if (!fs5.existsSync(filePath)) {
16594
16824
  continue;
16595
16825
  }
16596
- const content = fs4.readFileSync(filePath, "utf-8");
16826
+ const content = fs5.readFileSync(filePath, "utf-8");
16597
16827
  const result = removeMarkerSection(content);
16598
16828
  if (result.removed) {
16599
16829
  if (result.content.trim().length === 0) {
16600
16830
  if (!dryRun) {
16601
- fs4.unlinkSync(filePath);
16831
+ fs5.unlinkSync(filePath);
16602
16832
  }
16603
16833
  summary.push(`${prefix}Deleted ${infoFile} (only contained Glasstrace section)`);
16604
16834
  } else {
16605
16835
  if (!dryRun) {
16606
- fs4.writeFileSync(filePath, result.content, "utf-8");
16836
+ fs5.writeFileSync(filePath, result.content, "utf-8");
16607
16837
  }
16608
16838
  summary.push(`${prefix}Removed Glasstrace section from ${infoFile}`);
16609
16839
  }
@@ -16619,15 +16849,16 @@ async function runUninit(options) {
16619
16849
  }
16620
16850
  return { exitCode: errors.length > 0 ? 1 : 0, summary, warnings, errors };
16621
16851
  }
16622
- var fs4, os, path4, MCP_CONFIG_FILES, AGENT_INFO_FILES;
16852
+ var fs5, os, path5, MCP_CONFIG_FILES, AGENT_INFO_FILES;
16623
16853
  var init_uninit = __esm({
16624
16854
  "src/cli/uninit.ts"() {
16625
16855
  "use strict";
16626
- fs4 = __toESM(require("node:fs"), 1);
16856
+ fs5 = __toESM(require("node:fs"), 1);
16627
16857
  os = __toESM(require("node:os"), 1);
16628
- path4 = __toESM(require("node:path"), 1);
16858
+ path5 = __toESM(require("node:path"), 1);
16629
16859
  init_constants();
16630
16860
  init_scaffolder();
16861
+ init_discovery_file();
16631
16862
  MCP_CONFIG_FILES = [".mcp.json", ".cursor/mcp.json", ".gemini/settings.json"];
16632
16863
  AGENT_INFO_FILES = [
16633
16864
  "CLAUDE.md",
@@ -16673,11 +16904,11 @@ async function registerViaCli(agent, anonKey) {
16673
16904
  MCP_ENDPOINT
16674
16905
  ]);
16675
16906
  const configPath = agent.mcpConfigPath;
16676
- if (configPath !== null && fs5.existsSync(configPath)) {
16677
- const content = fs5.readFileSync(configPath, "utf-8");
16907
+ if (configPath !== null && fs6.existsSync(configPath)) {
16908
+ const content = fs6.readFileSync(configPath, "utf-8");
16678
16909
  if (!content.includes("bearer_token_env_var")) {
16679
16910
  const appendContent = content.endsWith("\n") ? "" : "\n";
16680
- fs5.writeFileSync(
16911
+ fs6.writeFileSync(
16681
16912
  configPath,
16682
16913
  content + appendContent + 'bearer_token_env_var = "GLASSTRACE_API_KEY"\n',
16683
16914
  "utf-8"
@@ -16722,8 +16953,8 @@ async function mcpAdd(options) {
16722
16953
  messages: ["Error: Run `glasstrace init` first to generate an API key."]
16723
16954
  };
16724
16955
  }
16725
- const markerPath = path5.join(projectRoot, ".glasstrace", "mcp-connected");
16726
- if (fs5.existsSync(markerPath) && !force) {
16956
+ const markerPath = path6.join(projectRoot, ".glasstrace", "mcp-connected");
16957
+ if (fs6.existsSync(markerPath) && !force) {
16727
16958
  return {
16728
16959
  exitCode: 0,
16729
16960
  results: [],
@@ -16782,7 +17013,7 @@ async function mcpAdd(options) {
16782
17013
  try {
16783
17014
  const configContent = generateMcpConfig(agent, MCP_ENDPOINT, anonKey);
16784
17015
  await writeMcpConfig(agent, configContent, projectRoot);
16785
- if (fs5.existsSync(agent.mcpConfigPath)) {
17016
+ if (fs6.existsSync(agent.mcpConfigPath)) {
16786
17017
  const infoContent = generateInfoSection(agent, MCP_ENDPOINT);
16787
17018
  if (infoContent !== "") {
16788
17019
  await injectInfoSection(agent, infoContent, projectRoot);
@@ -16849,13 +17080,13 @@ async function mcpAdd(options) {
16849
17080
  }
16850
17081
  return { exitCode: 0, results, messages };
16851
17082
  }
16852
- var import_node_child_process2, fs5, path5, import_node_util, execFileAsync;
17083
+ var import_node_child_process2, fs6, path6, import_node_util, execFileAsync;
16853
17084
  var init_mcp_add = __esm({
16854
17085
  "src/cli/mcp-add.ts"() {
16855
17086
  "use strict";
16856
17087
  import_node_child_process2 = require("node:child_process");
16857
- fs5 = __toESM(require("node:fs"), 1);
16858
- path5 = __toESM(require("node:path"), 1);
17088
+ fs6 = __toESM(require("node:fs"), 1);
17089
+ path6 = __toESM(require("node:path"), 1);
16859
17090
  import_node_util = require("node:util");
16860
17091
  init_anon_key();
16861
17092
  init_detect();
@@ -16886,15 +17117,15 @@ function hasRegisterGlasstraceImport(content) {
16886
17117
  function runValidate(options) {
16887
17118
  const { projectRoot } = options;
16888
17119
  const issues = [];
16889
- const glasstraceDir = path6.join(projectRoot, ".glasstrace");
16890
- const instrumentationPath = path6.join(projectRoot, "instrumentation.ts");
16891
- const markerPath = path6.join(glasstraceDir, "mcp-connected");
17120
+ const glasstraceDir = path7.join(projectRoot, ".glasstrace");
17121
+ const instrumentationPath = path7.join(projectRoot, "instrumentation.ts");
17122
+ const markerPath = path7.join(glasstraceDir, "mcp-connected");
16892
17123
  const glasstraceDirExists = isDirectorySafe(glasstraceDir);
16893
- const instrumentationExists = fs6.existsSync(instrumentationPath);
17124
+ const instrumentationExists = fs7.existsSync(instrumentationPath);
16894
17125
  const instrumentationContent = instrumentationExists ? safeReadFile(instrumentationPath) : null;
16895
- const markerExists = fs6.existsSync(markerPath);
17126
+ const markerExists = fs7.existsSync(markerPath);
16896
17127
  const mcpConfigsPresent = MCP_CONFIG_CANDIDATES.filter(
16897
- (rel) => fs6.existsSync(path6.join(projectRoot, rel))
17128
+ (rel) => fs7.existsSync(path7.join(projectRoot, rel))
16898
17129
  );
16899
17130
  if (glasstraceDirExists) {
16900
17131
  if (instrumentationContent === null || !hasRegisterGlasstraceImport(instrumentationContent)) {
@@ -16944,25 +17175,25 @@ function runValidate(options) {
16944
17175
  }
16945
17176
  function safeReadFile(filePath) {
16946
17177
  try {
16947
- return fs6.readFileSync(filePath, "utf-8");
17178
+ return fs7.readFileSync(filePath, "utf-8");
16948
17179
  } catch {
16949
17180
  return null;
16950
17181
  }
16951
17182
  }
16952
17183
  function isDirectorySafe(dirPath) {
16953
17184
  try {
16954
- if (!fs6.existsSync(dirPath)) return false;
16955
- return fs6.statSync(dirPath).isDirectory();
17185
+ if (!fs7.existsSync(dirPath)) return false;
17186
+ return fs7.statSync(dirPath).isDirectory();
16956
17187
  } catch {
16957
17188
  return false;
16958
17189
  }
16959
17190
  }
16960
- var fs6, path6, MCP_CONFIG_CANDIDATES;
17191
+ var fs7, path7, MCP_CONFIG_CANDIDATES;
16961
17192
  var init_validate = __esm({
16962
17193
  "src/cli/validate.ts"() {
16963
17194
  "use strict";
16964
- fs6 = __toESM(require("node:fs"), 1);
16965
- path6 = __toESM(require("node:path"), 1);
17195
+ fs7 = __toESM(require("node:fs"), 1);
17196
+ path7 = __toESM(require("node:path"), 1);
16966
17197
  MCP_CONFIG_CANDIDATES = [
16967
17198
  ".mcp.json",
16968
17199
  ".cursor/mcp.json",
@@ -16992,8 +17223,8 @@ function runStatus(options) {
16992
17223
  }
16993
17224
  function checkInstalled(root) {
16994
17225
  try {
16995
- const pkgPath = path7.join(root, "package.json");
16996
- const content = fs7.readFileSync(pkgPath, "utf-8");
17226
+ const pkgPath = path8.join(root, "package.json");
17227
+ const content = fs8.readFileSync(pkgPath, "utf-8");
16997
17228
  const pkg = JSON.parse(content);
16998
17229
  const deps = pkg["dependencies"];
16999
17230
  const devDeps = pkg["devDependencies"];
@@ -17004,7 +17235,7 @@ function checkInstalled(root) {
17004
17235
  }
17005
17236
  function checkInitialized(root) {
17006
17237
  try {
17007
- return fs7.statSync(path7.join(root, ".glasstrace")).isDirectory();
17238
+ return fs8.statSync(path8.join(root, ".glasstrace")).isDirectory();
17008
17239
  } catch {
17009
17240
  return false;
17010
17241
  }
@@ -17012,7 +17243,7 @@ function checkInitialized(root) {
17012
17243
  function checkInstrumentation(root) {
17013
17244
  for (const name of INSTRUMENTATION_FILES) {
17014
17245
  try {
17015
- const content = fs7.readFileSync(path7.join(root, name), "utf-8");
17246
+ const content = fs8.readFileSync(path8.join(root, name), "utf-8");
17016
17247
  if (content.includes("registerGlasstrace")) {
17017
17248
  return true;
17018
17249
  }
@@ -17024,7 +17255,7 @@ function checkInstrumentation(root) {
17024
17255
  function checkConfigWrapped(root) {
17025
17256
  for (const name of NEXT_CONFIG_NAMES) {
17026
17257
  try {
17027
- const content = fs7.readFileSync(path7.join(root, name), "utf-8");
17258
+ const content = fs8.readFileSync(path8.join(root, name), "utf-8");
17028
17259
  if (content.includes("withGlasstraceConfig")) {
17029
17260
  return true;
17030
17261
  }
@@ -17035,7 +17266,7 @@ function checkConfigWrapped(root) {
17035
17266
  }
17036
17267
  function checkAnonKey(root) {
17037
17268
  try {
17038
- return fs7.statSync(path7.join(root, ".glasstrace", "anon_key")).isFile();
17269
+ return fs8.statSync(path8.join(root, ".glasstrace", "anon_key")).isFile();
17039
17270
  } catch {
17040
17271
  return false;
17041
17272
  }
@@ -17043,7 +17274,7 @@ function checkAnonKey(root) {
17043
17274
  function checkMcpConfigured(root) {
17044
17275
  for (const name of MCP_JSON_FILES) {
17045
17276
  try {
17046
- const content = fs7.readFileSync(path7.join(root, name), "utf-8");
17277
+ const content = fs8.readFileSync(path8.join(root, name), "utf-8");
17047
17278
  const parsed = JSON.parse(content);
17048
17279
  const mcpServers = parsed["mcpServers"];
17049
17280
  if (mcpServers && typeof mcpServers === "object" && "glasstrace" in mcpServers) {
@@ -17054,7 +17285,7 @@ function checkMcpConfigured(root) {
17054
17285
  }
17055
17286
  for (const name of MCP_TOML_FILES) {
17056
17287
  try {
17057
- const content = fs7.readFileSync(path7.join(root, name), "utf-8");
17288
+ const content = fs8.readFileSync(path8.join(root, name), "utf-8");
17058
17289
  if (content.includes("[mcp_servers.glasstrace]")) {
17059
17290
  return true;
17060
17291
  }
@@ -17067,7 +17298,7 @@ function checkAgents(root) {
17067
17298
  const found = [];
17068
17299
  for (const name of AGENT_INFO_FILES2) {
17069
17300
  try {
17070
- const content = fs7.readFileSync(path7.join(root, name), "utf-8");
17301
+ const content = fs8.readFileSync(path8.join(root, name), "utf-8");
17071
17302
  const hasHtmlMarkers = content.includes("<!-- glasstrace:mcp:start -->") && content.includes("<!-- glasstrace:mcp:end -->");
17072
17303
  const hasHashMarkers = content.includes("# glasstrace:mcp:start") && content.includes("# glasstrace:mcp:end");
17073
17304
  if (hasHtmlMarkers || hasHashMarkers) {
@@ -17090,8 +17321,8 @@ function readRuntimeState(root) {
17090
17321
  pid: null
17091
17322
  };
17092
17323
  try {
17093
- const filePath = path7.join(root, ".glasstrace", "runtime-state.json");
17094
- const content = fs7.readFileSync(filePath, "utf-8");
17324
+ const filePath = path8.join(root, ".glasstrace", "runtime-state.json");
17325
+ const content = fs8.readFileSync(filePath, "utf-8");
17095
17326
  const parsed = JSON.parse(content);
17096
17327
  const updatedAt = typeof parsed.updatedAt === "string" ? parsed.updatedAt : null;
17097
17328
  const pid = typeof parsed.pid === "number" ? parsed.pid : null;
@@ -17140,12 +17371,12 @@ function readRuntimeState(root) {
17140
17371
  return empty;
17141
17372
  }
17142
17373
  }
17143
- var fs7, path7, MCP_JSON_FILES, MCP_TOML_FILES, AGENT_INFO_FILES2, INSTRUMENTATION_FILES, STALE_THRESHOLD_MS;
17374
+ var fs8, path8, MCP_JSON_FILES, MCP_TOML_FILES, AGENT_INFO_FILES2, INSTRUMENTATION_FILES, STALE_THRESHOLD_MS;
17144
17375
  var init_status = __esm({
17145
17376
  "src/cli/status.ts"() {
17146
17377
  "use strict";
17147
- fs7 = __toESM(require("node:fs"), 1);
17148
- path7 = __toESM(require("node:path"), 1);
17378
+ fs8 = __toESM(require("node:fs"), 1);
17379
+ path8 = __toESM(require("node:path"), 1);
17149
17380
  init_constants();
17150
17381
  MCP_JSON_FILES = [".mcp.json", ".cursor/mcp.json", ".gemini/settings.json", ".glasstrace/mcp.json"];
17151
17382
  MCP_TOML_FILES = [".codex/config.toml"];
@@ -17170,14 +17401,15 @@ var init_status = __esm({
17170
17401
  var init_exports = {};
17171
17402
  __export(init_exports, {
17172
17403
  decideMcpConfigAction: () => decideMcpConfigAction,
17404
+ gitignoreExcludesDiscoveryFile: () => gitignoreExcludesDiscoveryFile,
17173
17405
  meetsNodeVersion: () => meetsNodeVersion,
17174
17406
  rollbackSteps: () => rollbackSteps,
17175
17407
  runInit: () => runInit,
17176
17408
  verifyAnonKeyRegistration: () => verifyAnonKeyRegistration
17177
17409
  });
17178
17410
  module.exports = __toCommonJS(init_exports);
17179
- var fs8 = __toESM(require("node:fs"), 1);
17180
- var path8 = __toESM(require("node:path"), 1);
17411
+ var fs9 = __toESM(require("node:fs"), 1);
17412
+ var path9 = __toESM(require("node:path"), 1);
17181
17413
  var readline = __toESM(require("node:readline"), 1);
17182
17414
  init_scaffolder();
17183
17415
 
@@ -17705,6 +17937,7 @@ function resolveConfig(options) {
17705
17937
  }
17706
17938
 
17707
17939
  // src/cli/init.ts
17940
+ init_discovery_file();
17708
17941
  function meetsNodeVersion(minMajor) {
17709
17942
  const [major] = process.versions.node.split(".").map(Number);
17710
17943
  return major >= minMajor;
@@ -17712,8 +17945,8 @@ function meetsNodeVersion(minMajor) {
17712
17945
  async function decideMcpConfigAction(options) {
17713
17946
  const { configPath, expectedContent, force } = options;
17714
17947
  if (configPath === null) return "write";
17715
- const exists = options.existsSync ?? fs8.existsSync;
17716
- const read = options.readFile ?? ((p) => fs8.readFileSync(p, "utf-8"));
17948
+ const exists = options.existsSync ?? fs9.existsSync;
17949
+ const read = options.readFile ?? ((p) => fs9.readFileSync(p, "utf-8"));
17717
17950
  const prompt = options.prompt ?? promptYesNo;
17718
17951
  if (!exists(configPath)) return "write";
17719
17952
  let existingContent;
@@ -17758,22 +17991,69 @@ async function promptYesNo(question, defaultValue) {
17758
17991
  function cleanLeadingBlankLines2(content) {
17759
17992
  return content.replace(/^\n{2,}/, "\n");
17760
17993
  }
17994
+ function gitignoreExcludesDiscoveryFile(gitignoreContent, layout) {
17995
+ const staticRoot = layout === "static" ? "static" : "public";
17996
+ const rootTargets = [staticRoot, `${staticRoot}/`];
17997
+ const wellKnownTargets = [
17998
+ `${staticRoot}/.well-known`,
17999
+ `${staticRoot}/.well-known/`,
18000
+ ".well-known",
18001
+ ".well-known/"
18002
+ ];
18003
+ const fileTargets = [
18004
+ `${staticRoot}/.well-known/glasstrace.json`,
18005
+ ".well-known/glasstrace.json"
18006
+ ];
18007
+ let rootIgnored = false;
18008
+ let wellKnownIgnored = false;
18009
+ let fileIgnored = false;
18010
+ for (const rawLine of gitignoreContent.split("\n")) {
18011
+ const line = rawLine.trim();
18012
+ if (line === "" || line.startsWith("#")) continue;
18013
+ const negation = line.startsWith("!");
18014
+ const pattern = negation ? line.slice(1).trim() : line;
18015
+ if (pattern === "") continue;
18016
+ const normalized = pattern.startsWith("/") ? pattern.slice(1) : pattern;
18017
+ const matchesRoot = matchesDiscoveryPath(normalized, rootTargets);
18018
+ const matchesWellKnown = matchesDiscoveryPath(normalized, wellKnownTargets);
18019
+ const matchesFile = matchesDiscoveryPath(normalized, fileTargets);
18020
+ if (!matchesRoot && !matchesWellKnown && !matchesFile) continue;
18021
+ const newState = !negation;
18022
+ if (matchesRoot) rootIgnored = newState;
18023
+ if (matchesWellKnown) wellKnownIgnored = newState;
18024
+ if (matchesFile) fileIgnored = newState;
18025
+ }
18026
+ return rootIgnored || wellKnownIgnored || fileIgnored;
18027
+ }
18028
+ function matchesDiscoveryPath(pattern, targets) {
18029
+ const bare = pattern.endsWith("/") ? pattern.slice(0, -1) : pattern;
18030
+ for (const target of targets) {
18031
+ const tBare = target.endsWith("/") ? target.slice(0, -1) : target;
18032
+ if (bare === tBare) return true;
18033
+ if (pattern.startsWith("**/")) {
18034
+ const suffix = pattern.slice(3);
18035
+ const sBare = suffix.endsWith("/") ? suffix.slice(0, -1) : suffix;
18036
+ if (tBare === sBare || tBare.endsWith(`/${sBare}`)) return true;
18037
+ }
18038
+ }
18039
+ return false;
18040
+ }
17761
18041
  async function rollbackSteps(steps, projectRoot, state) {
17762
18042
  for (const step of [...steps].reverse()) {
17763
18043
  try {
17764
18044
  switch (step) {
17765
18045
  case "instrumentation": {
17766
- const instrPath = state?.instrumentationPath ?? path8.join(projectRoot, "instrumentation.ts");
17767
- if (fs8.existsSync(instrPath)) {
17768
- const content = fs8.readFileSync(instrPath, "utf-8");
18046
+ const instrPath = state?.instrumentationPath ?? path9.join(projectRoot, "instrumentation.ts");
18047
+ if (fs9.existsSync(instrPath)) {
18048
+ const content = fs9.readFileSync(instrPath, "utf-8");
17769
18049
  if (isInitCreatedInstrumentation(content)) {
17770
- fs8.unlinkSync(instrPath);
18050
+ fs9.unlinkSync(instrPath);
17771
18051
  } else if (state?.originalInstrumentationContent !== void 0) {
17772
- fs8.writeFileSync(instrPath, state.originalInstrumentationContent, "utf-8");
18052
+ fs9.writeFileSync(instrPath, state.originalInstrumentationContent, "utf-8");
17773
18053
  } else {
17774
18054
  const cleaned = removeRegisterGlasstrace(content);
17775
18055
  if (cleaned !== content) {
17776
- fs8.writeFileSync(instrPath, cleaned, "utf-8");
18056
+ fs9.writeFileSync(instrPath, cleaned, "utf-8");
17777
18057
  }
17778
18058
  }
17779
18059
  }
@@ -17781,11 +18061,11 @@ async function rollbackSteps(steps, projectRoot, state) {
17781
18061
  }
17782
18062
  case "next-config": {
17783
18063
  for (const name of NEXT_CONFIG_NAMES) {
17784
- const configPath = path8.join(projectRoot, name);
17785
- if (!fs8.existsSync(configPath)) {
18064
+ const configPath = path9.join(projectRoot, name);
18065
+ if (!fs9.existsSync(configPath)) {
17786
18066
  continue;
17787
18067
  }
17788
- const content = fs8.readFileSync(configPath, "utf-8");
18068
+ const content = fs9.readFileSync(configPath, "utf-8");
17789
18069
  if (!content.includes("withGlasstraceConfig")) {
17790
18070
  continue;
17791
18071
  }
@@ -17793,16 +18073,16 @@ async function rollbackSteps(steps, projectRoot, state) {
17793
18073
  const unwrapResult = isESM ? unwrapExport(content) : unwrapCJSExport(content);
17794
18074
  if (unwrapResult.unwrapped) {
17795
18075
  const cleaned = removeGlasstraceConfigImport(unwrapResult.content);
17796
- fs8.writeFileSync(configPath, cleanLeadingBlankLines2(cleaned), "utf-8");
18076
+ fs9.writeFileSync(configPath, cleanLeadingBlankLines2(cleaned), "utf-8");
17797
18077
  }
17798
18078
  break;
17799
18079
  }
17800
18080
  break;
17801
18081
  }
17802
18082
  case "env-local": {
17803
- const envPath = path8.join(projectRoot, ".env.local");
17804
- if (fs8.existsSync(envPath)) {
17805
- const content = fs8.readFileSync(envPath, "utf-8");
18083
+ const envPath = path9.join(projectRoot, ".env.local");
18084
+ if (fs9.existsSync(envPath)) {
18085
+ const content = fs9.readFileSync(envPath, "utf-8");
17806
18086
  const lines = content.split("\n");
17807
18087
  const filtered = lines.filter((line) => {
17808
18088
  const trimmed = line.trim();
@@ -17811,18 +18091,18 @@ async function rollbackSteps(steps, projectRoot, state) {
17811
18091
  if (filtered.length !== lines.length) {
17812
18092
  const result = filtered.join("\n");
17813
18093
  if (result.trim().length === 0) {
17814
- fs8.unlinkSync(envPath);
18094
+ fs9.unlinkSync(envPath);
17815
18095
  } else {
17816
- fs8.writeFileSync(envPath, result, "utf-8");
18096
+ fs9.writeFileSync(envPath, result, "utf-8");
17817
18097
  }
17818
18098
  }
17819
18099
  }
17820
18100
  break;
17821
18101
  }
17822
18102
  case "gitignore": {
17823
- const gitignorePath = path8.join(projectRoot, ".gitignore");
17824
- if (fs8.existsSync(gitignorePath)) {
17825
- const content = fs8.readFileSync(gitignorePath, "utf-8");
18103
+ const gitignorePath = path9.join(projectRoot, ".gitignore");
18104
+ if (fs9.existsSync(gitignorePath)) {
18105
+ const content = fs9.readFileSync(gitignorePath, "utf-8");
17826
18106
  const lines = content.split("\n");
17827
18107
  const filtered = lines.filter(
17828
18108
  (line) => line.trim() !== ".glasstrace/"
@@ -17830,14 +18110,18 @@ async function rollbackSteps(steps, projectRoot, state) {
17830
18110
  if (filtered.length !== lines.length) {
17831
18111
  const result = filtered.join("\n");
17832
18112
  if (result.trim().length === 0) {
17833
- fs8.unlinkSync(gitignorePath);
18113
+ fs9.unlinkSync(gitignorePath);
17834
18114
  } else {
17835
- fs8.writeFileSync(gitignorePath, result, "utf-8");
18115
+ fs9.writeFileSync(gitignorePath, result, "utf-8");
17836
18116
  }
17837
18117
  }
17838
18118
  }
17839
18119
  break;
17840
18120
  }
18121
+ case "discovery-file": {
18122
+ removeDiscoveryFile(projectRoot);
18123
+ break;
18124
+ }
17841
18125
  }
17842
18126
  } catch {
17843
18127
  }
@@ -17859,8 +18143,8 @@ async function runInit(options) {
17859
18143
  errors.push(err instanceof Error ? err.message : String(err));
17860
18144
  return { exitCode: 1, summary, warnings, errors };
17861
18145
  }
17862
- const packageJsonPath = path8.join(projectRoot, "package.json");
17863
- if (!fs8.existsSync(packageJsonPath)) {
18146
+ const packageJsonPath = path9.join(projectRoot, "package.json");
18147
+ if (!fs9.existsSync(packageJsonPath)) {
17864
18148
  errors.push("No package.json found. Run this command from a Node.js project root.");
17865
18149
  return { exitCode: 1, summary, warnings, errors };
17866
18150
  }
@@ -17869,8 +18153,8 @@ async function runInit(options) {
17869
18153
  const preResolved = resolveInstrumentationTarget(projectRoot);
17870
18154
  if (!preResolved.conflict && preResolved.target !== null) {
17871
18155
  rollbackState.instrumentationPath = preResolved.target;
17872
- if (fs8.existsSync(preResolved.target)) {
17873
- rollbackState.originalInstrumentationContent = fs8.readFileSync(
18156
+ if (fs9.existsSync(preResolved.target)) {
18157
+ rollbackState.originalInstrumentationContent = fs9.readFileSync(
17874
18158
  preResolved.target,
17875
18159
  "utf-8"
17876
18160
  );
@@ -17885,7 +18169,7 @@ async function runInit(options) {
17885
18169
  if (instrResult.filePath !== void 0) {
17886
18170
  rollbackState.instrumentationPath = instrResult.filePath;
17887
18171
  }
17888
- const relativePath = instrResult.filePath !== void 0 ? path8.relative(projectRoot, instrResult.filePath) : "instrumentation.ts";
18172
+ const relativePath = instrResult.filePath !== void 0 ? path9.relative(projectRoot, instrResult.filePath) : "instrumentation.ts";
17889
18173
  switch (instrResult.action) {
17890
18174
  case "created":
17891
18175
  summary.push(`Created ${relativePath}`);
@@ -17910,8 +18194,8 @@ async function runInit(options) {
17910
18194
  );
17911
18195
  break;
17912
18196
  case "conflict": {
17913
- const primary = instrResult.filePath !== void 0 ? path8.relative(projectRoot, instrResult.filePath) : "src/instrumentation.ts";
17914
- const competing = instrResult.conflictingPath !== void 0 ? path8.relative(projectRoot, instrResult.conflictingPath) : "instrumentation.ts";
18197
+ const primary = instrResult.filePath !== void 0 ? path9.relative(projectRoot, instrResult.filePath) : "src/instrumentation.ts";
18198
+ const competing = instrResult.conflictingPath !== void 0 ? path9.relative(projectRoot, instrResult.conflictingPath) : "instrumentation.ts";
17915
18199
  await rollbackSteps(rollbackState.steps, projectRoot, rollbackState);
17916
18200
  errors.push(
17917
18201
  `Both ${primary} and ${competing} exist. Next.js's loader behavior is undefined when both are present.
@@ -17958,10 +18242,10 @@ Then add this as the first statement in your register() function:
17958
18242
  return { exitCode: 1, summary, warnings, errors };
17959
18243
  }
17960
18244
  try {
17961
- const envPathForCheck = path8.join(projectRoot, ".env.local");
18245
+ const envPathForCheck = path9.join(projectRoot, ".env.local");
17962
18246
  let existingDevKey = false;
17963
- if (fs8.existsSync(envPathForCheck)) {
17964
- const existingContent = fs8.readFileSync(envPathForCheck, "utf-8");
18247
+ if (fs9.existsSync(envPathForCheck)) {
18248
+ const existingContent = fs9.readFileSync(envPathForCheck, "utf-8");
17965
18249
  existingDevKey = isDevApiKey(readEnvLocalApiKey(existingContent));
17966
18250
  }
17967
18251
  const envCreated = await scaffoldEnvLocal(projectRoot);
@@ -18001,11 +18285,55 @@ Then add this as the first statement in your register() function:
18001
18285
  if (preExistingAnonKey !== null) {
18002
18286
  summary.push("Preserved existing .glasstrace/anon_key");
18003
18287
  }
18288
+ try {
18289
+ const discoveryResult = writeDiscoveryFile(projectRoot, anonKey);
18290
+ const relPath = relativeDiscoveryPath(discoveryResult.layout);
18291
+ switch (discoveryResult.action) {
18292
+ case "created":
18293
+ summary.push(`Created ${relPath}`);
18294
+ rollbackState.steps.push("discovery-file");
18295
+ break;
18296
+ case "updated-stale":
18297
+ summary.push(`Updated ${relPath} (anon key had changed)`);
18298
+ break;
18299
+ case "skipped-matches":
18300
+ summary.push(`Skipped ${relPath} (already matches anon key)`);
18301
+ break;
18302
+ case "skipped-foreign":
18303
+ summary.push(
18304
+ `Rewrote ${relPath} (existing file was malformed or not SDK-managed)`
18305
+ );
18306
+ break;
18307
+ case "failed":
18308
+ warnings.push(
18309
+ `Failed to write ${relPath}${discoveryResult.error !== void 0 ? `: ${discoveryResult.error}` : ""}. The Glasstrace browser extension will fall back to the runtime handler until the file is written.`
18310
+ );
18311
+ break;
18312
+ }
18313
+ const gitignorePath = path9.join(projectRoot, ".gitignore");
18314
+ if (fs9.existsSync(gitignorePath)) {
18315
+ try {
18316
+ const gitignoreContent = fs9.readFileSync(gitignorePath, "utf-8");
18317
+ if (gitignoreExcludesDiscoveryFile(gitignoreContent, discoveryResult.layout)) {
18318
+ warnings.push(
18319
+ `Your .gitignore excludes ${relPath} (directly or via a parent rule). The discovery file must be committed for the Glasstrace browser extension to find it in deployed builds. Remove the matching line from .gitignore or add an explicit negation (e.g. \`!` + relPath + "`)."
18320
+ );
18321
+ }
18322
+ } catch {
18323
+ }
18324
+ }
18325
+ } catch (err) {
18326
+ warnings.push(
18327
+ `Failed to write ${relativeDiscoveryPath(
18328
+ resolveStaticRoot(projectRoot).layout
18329
+ )}: ${err instanceof Error ? err.message : String(err)}`
18330
+ );
18331
+ }
18004
18332
  let anyConfigWritten = false;
18005
18333
  if (isCI) {
18006
18334
  const genericAgent = {
18007
18335
  name: "generic",
18008
- mcpConfigPath: path8.join(projectRoot, ".glasstrace", "mcp.json"),
18336
+ mcpConfigPath: path9.join(projectRoot, ".glasstrace", "mcp.json"),
18009
18337
  infoFilePath: null,
18010
18338
  cliAvailable: false,
18011
18339
  registrationCommand: null
@@ -18019,7 +18347,7 @@ Then add this as the first statement in your register() function:
18019
18347
  if (decision !== "skip") {
18020
18348
  await writeMcpConfig(genericAgent, genericConfig, projectRoot);
18021
18349
  }
18022
- if (genericAgent.mcpConfigPath !== null && fs8.existsSync(genericAgent.mcpConfigPath)) {
18350
+ if (genericAgent.mcpConfigPath !== null && fs9.existsSync(genericAgent.mcpConfigPath)) {
18023
18351
  anyConfigWritten = true;
18024
18352
  summary.push("Created .glasstrace/mcp.json (CI mode)");
18025
18353
  }
@@ -18033,14 +18361,14 @@ Then add this as the first statement in your register() function:
18033
18361
  );
18034
18362
  const genericAgent = {
18035
18363
  name: "generic",
18036
- mcpConfigPath: path8.join(projectRoot, ".glasstrace", "mcp.json"),
18364
+ mcpConfigPath: path9.join(projectRoot, ".glasstrace", "mcp.json"),
18037
18365
  infoFilePath: null,
18038
18366
  cliAvailable: false,
18039
18367
  registrationCommand: null
18040
18368
  };
18041
18369
  const genericConfig = generateMcpConfig(genericAgent, MCP_ENDPOINT, anonKey);
18042
18370
  await writeMcpConfig(genericAgent, genericConfig, projectRoot);
18043
- if (genericAgent.mcpConfigPath !== null && fs8.existsSync(genericAgent.mcpConfigPath)) {
18371
+ if (genericAgent.mcpConfigPath !== null && fs9.existsSync(genericAgent.mcpConfigPath)) {
18044
18372
  anyConfigWritten = true;
18045
18373
  }
18046
18374
  agents = [];
@@ -18058,13 +18386,13 @@ Then add this as the first statement in your register() function:
18058
18386
  summary.push(
18059
18387
  `Preserved existing ${agent.mcpConfigPath ?? agent.name} (user declined overwrite)`
18060
18388
  );
18061
- if (agent.mcpConfigPath !== null && fs8.existsSync(agent.mcpConfigPath)) {
18389
+ if (agent.mcpConfigPath !== null && fs9.existsSync(agent.mcpConfigPath)) {
18062
18390
  anyConfigWritten = true;
18063
18391
  }
18064
18392
  continue;
18065
18393
  }
18066
18394
  await writeMcpConfig(agent, configContent, projectRoot);
18067
- const configExists = agent.mcpConfigPath !== null && fs8.existsSync(agent.mcpConfigPath);
18395
+ const configExists = agent.mcpConfigPath !== null && fs9.existsSync(agent.mcpConfigPath);
18068
18396
  if (!configExists) {
18069
18397
  continue;
18070
18398
  }
@@ -18150,9 +18478,9 @@ async function verifyAnonKeyRegistration(projectRoot) {
18150
18478
  }
18151
18479
  let devKey;
18152
18480
  try {
18153
- const envPath = path8.join(projectRoot, ".env.local");
18154
- if (fs8.existsSync(envPath)) {
18155
- const envContent = fs8.readFileSync(envPath, "utf-8");
18481
+ const envPath = path9.join(projectRoot, ".env.local");
18482
+ if (fs9.existsSync(envPath)) {
18483
+ const envContent = fs9.readFileSync(envPath, "utf-8");
18156
18484
  const effective = readEnvLocalApiKey(envContent);
18157
18485
  if (effective !== null && isDevApiKey(effective)) {
18158
18486
  devKey = effective;
@@ -18162,7 +18490,7 @@ async function verifyAnonKeyRegistration(projectRoot) {
18162
18490
  }
18163
18491
  const baseConfig = resolveConfig({ apiKey: devKey });
18164
18492
  const config2 = { ...baseConfig, apiKey: devKey };
18165
- const sdkVersion = true ? "0.19.0" : "0.0.0-dev";
18493
+ const sdkVersion = true ? "0.20.0" : "0.0.0-dev";
18166
18494
  const result = await verifyInitReachable(config2, anonKey, sdkVersion);
18167
18495
  if (result.ok) {
18168
18496
  return { outcome: "verified" };
@@ -18211,7 +18539,7 @@ function parseArgs(argv) {
18211
18539
  };
18212
18540
  }
18213
18541
  var scriptPath = typeof process !== "undefined" && process.argv[1] !== void 0 ? process.argv[1].replace(/\\/g, "/") : void 0;
18214
- var scriptBasename = scriptPath !== void 0 ? path8.basename(scriptPath) : void 0;
18542
+ var scriptBasename = scriptPath !== void 0 ? path9.basename(scriptPath) : void 0;
18215
18543
  var isDirectExecution = scriptPath !== void 0 && (scriptPath.endsWith("/cli/init.js") || scriptPath.endsWith("/cli/init.ts") || scriptBasename === "glasstrace");
18216
18544
  if (isDirectExecution) {
18217
18545
  if (!meetsNodeVersion(20)) {
@@ -18412,6 +18740,7 @@ Usage:
18412
18740
  // Annotate the CommonJS export names for ESM import in node:
18413
18741
  0 && (module.exports = {
18414
18742
  decideMcpConfigAction,
18743
+ gitignoreExcludesDiscoveryFile,
18415
18744
  meetsNodeVersion,
18416
18745
  rollbackSteps,
18417
18746
  runInit,