@glasstrace/sdk 0.19.0 → 0.20.1

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.
Files changed (63) hide show
  1. package/README.md +79 -0
  2. package/dist/chunk-BT2OCXCG.js +178 -0
  3. package/dist/chunk-BT2OCXCG.js.map +1 -0
  4. package/dist/{chunk-F2TZRBEH.js → chunk-DO2YPMQ5.js} +9 -181
  5. package/dist/chunk-DO2YPMQ5.js.map +1 -0
  6. package/dist/{chunk-YPXW2TN3.js → chunk-IP4NMDJK.js} +2 -2
  7. package/dist/{chunk-VN3GZDV6.js → chunk-IQN6TRMQ.js} +2 -2
  8. package/dist/{chunk-XNDHQN4S.js → chunk-LU3PPAOQ.js} +288 -54
  9. package/dist/chunk-LU3PPAOQ.js.map +1 -0
  10. package/dist/chunk-R4DAIPXD.js +4461 -0
  11. package/dist/chunk-R4DAIPXD.js.map +1 -0
  12. package/dist/{chunk-5N2IR4EO.js → chunk-TQ54WLCZ.js} +1 -1
  13. package/dist/{chunk-5N2IR4EO.js.map → chunk-TQ54WLCZ.js.map} +1 -1
  14. package/dist/chunk-Z2EGETTT.js +204 -0
  15. package/dist/chunk-Z2EGETTT.js.map +1 -0
  16. package/dist/cli/init.cjs +483 -152
  17. package/dist/cli/init.cjs.map +1 -1
  18. package/dist/cli/init.d.cts +48 -2
  19. package/dist/cli/init.d.ts +48 -2
  20. package/dist/cli/init.js +109 -7
  21. package/dist/cli/init.js.map +1 -1
  22. package/dist/cli/mcp-add.cjs.map +1 -1
  23. package/dist/cli/mcp-add.js +2 -2
  24. package/dist/cli/uninit.cjs +174 -54
  25. package/dist/cli/uninit.cjs.map +1 -1
  26. package/dist/cli/uninit.d.cts +2 -0
  27. package/dist/cli/uninit.d.ts +2 -0
  28. package/dist/cli/uninit.js +2 -1
  29. package/dist/edge-entry-Ds2fNOeh.d.ts +157 -0
  30. package/dist/edge-entry-FJFKkeFF.d.cts +157 -0
  31. package/dist/edge-entry.cjs +14939 -0
  32. package/dist/edge-entry.cjs.map +1 -0
  33. package/dist/edge-entry.d.cts +6 -0
  34. package/dist/edge-entry.d.ts +6 -0
  35. package/dist/edge-entry.js +16 -0
  36. package/dist/index.cjs +13 -4
  37. package/dist/index.cjs.map +1 -1
  38. package/dist/index.d-DgeH-pNJ.d.cts +191 -0
  39. package/dist/index.d-DgeH-pNJ.d.ts +191 -0
  40. package/dist/index.d.cts +9 -461
  41. package/dist/index.d.ts +9 -461
  42. package/dist/index.js +32 -4609
  43. package/dist/index.js.map +1 -1
  44. package/dist/node-entry.cjs +22492 -0
  45. package/dist/node-entry.cjs.map +1 -0
  46. package/dist/node-entry.d.cts +10 -0
  47. package/dist/node-entry.d.ts +10 -0
  48. package/dist/node-entry.js +101 -0
  49. package/dist/node-entry.js.map +1 -0
  50. package/dist/node-subpath.cjs +14506 -0
  51. package/dist/node-subpath.cjs.map +1 -0
  52. package/dist/node-subpath.d.cts +132 -0
  53. package/dist/node-subpath.d.ts +132 -0
  54. package/dist/node-subpath.js +30 -0
  55. package/dist/node-subpath.js.map +1 -0
  56. package/dist/{source-map-uploader-VPDZWWM2.js → source-map-uploader-YXWO6JLN.js} +3 -3
  57. package/dist/source-map-uploader-YXWO6JLN.js.map +1 -0
  58. package/package.json +4 -1
  59. package/dist/chunk-F2TZRBEH.js.map +0 -1
  60. package/dist/chunk-XNDHQN4S.js.map +0 -1
  61. /package/dist/{chunk-YPXW2TN3.js.map → chunk-IP4NMDJK.js.map} +0 -0
  62. /package/dist/{chunk-VN3GZDV6.js.map → chunk-IQN6TRMQ.js.map} +0 -0
  63. /package/dist/{source-map-uploader-VPDZWWM2.js.map → edge-entry.js.map} +0 -0
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,209 @@ 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
+ const rootDir = layout === "static" ? "static" : "public";
15989
+ return `${rootDir}/${WELL_KNOWN_GLASSTRACE_PATH}`;
15990
+ }
15991
+ function readExistingDiscoveryFile(filePath) {
15992
+ let raw;
15993
+ try {
15994
+ raw = fs4.readFileSync(filePath, "utf-8");
15995
+ } catch {
15996
+ return null;
15997
+ }
15998
+ let parsed;
15999
+ try {
16000
+ parsed = JSON.parse(raw);
16001
+ } catch {
16002
+ return null;
16003
+ }
16004
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
16005
+ return null;
16006
+ }
16007
+ const obj = parsed;
16008
+ const versionRaw = obj.version;
16009
+ if (typeof versionRaw !== "number" || !Number.isInteger(versionRaw) || versionRaw < 1) {
16010
+ return null;
16011
+ }
16012
+ const keyResult = AnonApiKeySchema.safeParse(obj.key);
16013
+ if (!keyResult.success) {
16014
+ return null;
16015
+ }
16016
+ const extras = {};
16017
+ for (const [k, v] of Object.entries(obj)) {
16018
+ if (k === "version" || k === "key") continue;
16019
+ extras[k] = v;
16020
+ }
16021
+ return { key: keyResult.data, extras };
16022
+ }
16023
+ function serializeDiscoveryPayload(key, extras) {
16024
+ const payload = {
16025
+ version: DISCOVERY_FILE_VERSION,
16026
+ key,
16027
+ ...extras
16028
+ };
16029
+ return JSON.stringify(payload, null, 2) + "\n";
16030
+ }
16031
+ function writeDiscoveryFile(projectRoot, anonKey) {
16032
+ const { absolutePath: staticRoot, layout } = resolveStaticRoot(projectRoot);
16033
+ const wellKnownDir = path4.join(staticRoot, ".well-known");
16034
+ const filePath = path4.join(wellKnownDir, "glasstrace.json");
16035
+ let existingAction;
16036
+ let extras = {};
16037
+ if (fs4.existsSync(filePath)) {
16038
+ const existing = readExistingDiscoveryFile(filePath);
16039
+ if (existing === null) {
16040
+ existingAction = "skipped-foreign";
16041
+ } else if (existing.key === anonKey) {
16042
+ return {
16043
+ action: "skipped-matches",
16044
+ filePath,
16045
+ layout
16046
+ };
16047
+ } else {
16048
+ extras = existing.extras;
16049
+ existingAction = "updated-stale";
16050
+ }
16051
+ } else {
16052
+ existingAction = "created";
16053
+ }
16054
+ const tmpPath = `${filePath}.tmp-${process.pid}`;
16055
+ const needsWindowsReplace = process.platform === "win32" && fs4.existsSync(filePath);
16056
+ const backupPath = needsWindowsReplace ? `${filePath}.bak-${process.pid}` : null;
16057
+ try {
16058
+ fs4.mkdirSync(wellKnownDir, { recursive: true });
16059
+ const payload = serializeDiscoveryPayload(anonKey, extras);
16060
+ fs4.writeFileSync(tmpPath, payload, { encoding: "utf-8" });
16061
+ if (backupPath !== null) {
16062
+ fs4.renameSync(filePath, backupPath);
16063
+ try {
16064
+ fs4.renameSync(tmpPath, filePath);
16065
+ } catch (renameErr) {
16066
+ try {
16067
+ fs4.renameSync(backupPath, filePath);
16068
+ } catch {
16069
+ }
16070
+ throw renameErr;
16071
+ }
16072
+ try {
16073
+ fs4.unlinkSync(backupPath);
16074
+ } catch {
16075
+ }
16076
+ } else {
16077
+ fs4.renameSync(tmpPath, filePath);
16078
+ }
16079
+ return { action: existingAction, filePath, layout };
16080
+ } catch (err) {
16081
+ try {
16082
+ if (fs4.existsSync(tmpPath)) {
16083
+ fs4.unlinkSync(tmpPath);
16084
+ }
16085
+ } catch {
16086
+ }
16087
+ return {
16088
+ action: "failed",
16089
+ filePath,
16090
+ layout,
16091
+ error: err instanceof Error ? err.message : String(err)
16092
+ };
16093
+ }
16094
+ }
16095
+ function removeDiscoveryFile(projectRoot) {
16096
+ const { layout: inferredLayout } = resolveStaticRoot(projectRoot);
16097
+ const layouts = ["public", "static"];
16098
+ const outcomes = [];
16099
+ for (const layout of layouts) {
16100
+ const staticRoot = path4.join(projectRoot, layout);
16101
+ const wellKnownDir = path4.join(staticRoot, ".well-known");
16102
+ const filePath = path4.join(wellKnownDir, "glasstrace.json");
16103
+ let removed = false;
16104
+ try {
16105
+ if (fs4.existsSync(filePath)) {
16106
+ fs4.unlinkSync(filePath);
16107
+ removed = true;
16108
+ }
16109
+ } catch (err) {
16110
+ return {
16111
+ action: "failed",
16112
+ filePath,
16113
+ layout,
16114
+ directoryRemoved: false,
16115
+ error: err instanceof Error ? err.message : String(err)
16116
+ };
16117
+ }
16118
+ let directoryRemoved = false;
16119
+ if (removed) {
16120
+ try {
16121
+ if (fs4.existsSync(wellKnownDir)) {
16122
+ const entries = fs4.readdirSync(wellKnownDir);
16123
+ if (entries.length === 0) {
16124
+ fs4.rmdirSync(wellKnownDir);
16125
+ directoryRemoved = true;
16126
+ }
16127
+ }
16128
+ } catch {
16129
+ }
16130
+ }
16131
+ outcomes.push({ layout, filePath, removed, directoryRemoved });
16132
+ }
16133
+ const removals = outcomes.filter((o) => o.removed);
16134
+ const chosen = (() => {
16135
+ if (removals.length === 0) {
16136
+ return outcomes.find((o) => o.layout === inferredLayout) ?? outcomes[0];
16137
+ }
16138
+ if (removals.length === 1) return removals[0];
16139
+ return removals.find((o) => o.layout === inferredLayout) ?? removals[0];
16140
+ })();
16141
+ const anyDirectoryRemoved = outcomes.some((o) => o.directoryRemoved);
16142
+ return {
16143
+ action: removals.length > 0 ? "removed" : "not-found",
16144
+ filePath: chosen.filePath,
16145
+ layout: chosen.layout,
16146
+ directoryRemoved: chosen.directoryRemoved || anyDirectoryRemoved
16147
+ };
16148
+ }
16149
+ var fs4, path4, WELL_KNOWN_GLASSTRACE_PATH, DISCOVERY_FILE_VERSION;
16150
+ var init_discovery_file = __esm({
16151
+ "src/cli/discovery-file.ts"() {
16152
+ "use strict";
16153
+ fs4 = __toESM(require("node:fs"), 1);
16154
+ path4 = __toESM(require("node:path"), 1);
16155
+ init_dist();
16156
+ WELL_KNOWN_GLASSTRACE_PATH = ".well-known/glasstrace.json";
16157
+ DISCOVERY_FILE_VERSION = 1;
16158
+ }
16159
+ });
16160
+
15958
16161
  // src/cli/uninit.ts
15959
16162
  var uninit_exports = {};
15960
16163
  __export(uninit_exports, {
@@ -16272,24 +16475,24 @@ function processTomlMcpConfig(content) {
16272
16475
  return { action: "removed-section", content: result + "\n" };
16273
16476
  }
16274
16477
  function writeShutdownMarker(projectRoot) {
16275
- const dirPath = path4.join(projectRoot, ".glasstrace");
16276
- if (!fs4.existsSync(dirPath)) {
16478
+ const dirPath = path5.join(projectRoot, ".glasstrace");
16479
+ if (!fs5.existsSync(dirPath)) {
16277
16480
  return false;
16278
16481
  }
16279
- const markerPath = path4.join(dirPath, "shutdown-requested");
16482
+ const markerPath = path5.join(dirPath, "shutdown-requested");
16280
16483
  const tmpPath = `${markerPath}.tmp`;
16281
16484
  const body = JSON.stringify({ requestedAt: (/* @__PURE__ */ new Date()).toISOString() });
16282
16485
  try {
16283
- fs4.writeFileSync(tmpPath, body, { encoding: "utf-8", mode: 384 });
16486
+ fs5.writeFileSync(tmpPath, body, { encoding: "utf-8", mode: 384 });
16284
16487
  try {
16285
- fs4.chmodSync(tmpPath, 384);
16488
+ fs5.chmodSync(tmpPath, 384);
16286
16489
  } catch {
16287
16490
  }
16288
- fs4.renameSync(tmpPath, markerPath);
16491
+ fs5.renameSync(tmpPath, markerPath);
16289
16492
  return true;
16290
16493
  } catch {
16291
16494
  try {
16292
- fs4.unlinkSync(tmpPath);
16495
+ fs5.unlinkSync(tmpPath);
16293
16496
  } catch {
16294
16497
  }
16295
16498
  return false;
@@ -16330,8 +16533,8 @@ async function runUninit(options) {
16330
16533
  summary.push("Wrote .glasstrace/shutdown-requested marker");
16331
16534
  }
16332
16535
  } else {
16333
- const dirPath = path4.join(projectRoot, ".glasstrace");
16334
- if (fs4.existsSync(dirPath)) {
16536
+ const dirPath = path5.join(projectRoot, ".glasstrace");
16537
+ if (fs5.existsSync(dirPath)) {
16335
16538
  summary.push(`${prefix}Would write .glasstrace/shutdown-requested marker`);
16336
16539
  }
16337
16540
  }
@@ -16343,11 +16546,11 @@ async function runUninit(options) {
16343
16546
  try {
16344
16547
  let configHandled = false;
16345
16548
  for (const name of NEXT_CONFIG_NAMES) {
16346
- const configPath = path4.join(projectRoot, name);
16347
- if (!fs4.existsSync(configPath)) {
16549
+ const configPath = path5.join(projectRoot, name);
16550
+ if (!fs5.existsSync(configPath)) {
16348
16551
  continue;
16349
16552
  }
16350
- const content = fs4.readFileSync(configPath, "utf-8");
16553
+ const content = fs5.readFileSync(configPath, "utf-8");
16351
16554
  if (!content.includes("withGlasstraceConfig")) {
16352
16555
  continue;
16353
16556
  }
@@ -16357,7 +16560,7 @@ async function runUninit(options) {
16357
16560
  const cleaned = removeGlasstraceConfigImport(unwrapResult.content);
16358
16561
  const final = cleanLeadingBlankLines(cleaned);
16359
16562
  if (!dryRun) {
16360
- fs4.writeFileSync(configPath, final, "utf-8");
16563
+ fs5.writeFileSync(configPath, final, "utf-8");
16361
16564
  }
16362
16565
  summary.push(`${prefix}Unwrapped withGlasstraceConfig from ${name}`);
16363
16566
  configHandled = true;
@@ -16378,20 +16581,20 @@ async function runUninit(options) {
16378
16581
  );
16379
16582
  }
16380
16583
  try {
16381
- const instrPath = path4.join(projectRoot, "instrumentation.ts");
16382
- if (fs4.existsSync(instrPath)) {
16383
- const content = fs4.readFileSync(instrPath, "utf-8");
16584
+ const instrPath = path5.join(projectRoot, "instrumentation.ts");
16585
+ if (fs5.existsSync(instrPath)) {
16586
+ const content = fs5.readFileSync(instrPath, "utf-8");
16384
16587
  if (content.includes("registerGlasstrace") || content.includes("@glasstrace/sdk")) {
16385
16588
  if (isInitCreatedInstrumentation(content)) {
16386
16589
  if (!dryRun) {
16387
- fs4.unlinkSync(instrPath);
16590
+ fs5.unlinkSync(instrPath);
16388
16591
  }
16389
16592
  summary.push(`${prefix}Deleted instrumentation.ts (init-created)`);
16390
16593
  } else {
16391
16594
  const cleaned = removeRegisterGlasstrace(content);
16392
16595
  if (cleaned !== content) {
16393
16596
  if (!dryRun) {
16394
- fs4.writeFileSync(instrPath, cleaned, "utf-8");
16597
+ fs5.writeFileSync(instrPath, cleaned, "utf-8");
16395
16598
  }
16396
16599
  summary.push(
16397
16600
  `${prefix}Removed registerGlasstrace() from instrumentation.ts`
@@ -16406,10 +16609,10 @@ async function runUninit(options) {
16406
16609
  );
16407
16610
  }
16408
16611
  try {
16409
- const glasstraceDir = path4.join(projectRoot, ".glasstrace");
16410
- if (fs4.existsSync(glasstraceDir)) {
16612
+ const glasstraceDir = path5.join(projectRoot, ".glasstrace");
16613
+ if (fs5.existsSync(glasstraceDir)) {
16411
16614
  if (!dryRun) {
16412
- fs4.rmSync(glasstraceDir, { recursive: true, force: true });
16615
+ fs5.rmSync(glasstraceDir, { recursive: true, force: true });
16413
16616
  }
16414
16617
  summary.push(`${prefix}Removed .glasstrace/ directory`);
16415
16618
  }
@@ -16419,9 +16622,38 @@ async function runUninit(options) {
16419
16622
  );
16420
16623
  }
16421
16624
  try {
16422
- const envPath = path4.join(projectRoot, ".env.local");
16423
- if (fs4.existsSync(envPath)) {
16424
- const content = fs4.readFileSync(envPath, "utf-8");
16625
+ if (dryRun) {
16626
+ for (const previewLayout of ["public", "static"]) {
16627
+ const relPath = relativeDiscoveryPath(previewLayout);
16628
+ const absPath = path5.join(projectRoot, relPath);
16629
+ if (fs5.existsSync(absPath)) {
16630
+ summary.push(`${prefix}Would remove ${relPath}`);
16631
+ }
16632
+ }
16633
+ } else {
16634
+ const result = removeDiscoveryFile(projectRoot);
16635
+ if (result.action === "removed") {
16636
+ const relPath = relativeDiscoveryPath(result.layout);
16637
+ summary.push(`Removed ${relPath}`);
16638
+ if (result.directoryRemoved) {
16639
+ const dirRel = relPath.replace(/\/glasstrace\.json$/, "/");
16640
+ summary.push(`Removed empty ${dirRel}`);
16641
+ }
16642
+ } else if (result.action === "failed") {
16643
+ warnings.push(
16644
+ `Failed to remove ${relativeDiscoveryPath(result.layout)}${result.error !== void 0 ? `: ${result.error}` : ""}`
16645
+ );
16646
+ }
16647
+ }
16648
+ } catch (err) {
16649
+ warnings.push(
16650
+ `Failed to remove discovery file: ${err instanceof Error ? err.message : String(err)}`
16651
+ );
16652
+ }
16653
+ try {
16654
+ const envPath = path5.join(projectRoot, ".env.local");
16655
+ if (fs5.existsSync(envPath)) {
16656
+ const content = fs5.readFileSync(envPath, "utf-8");
16425
16657
  const existingKey = readEnvLocalApiKey(content);
16426
16658
  const hasDevKey = isDevApiKey(existingKey);
16427
16659
  let proceed = true;
@@ -16454,12 +16686,12 @@ async function runUninit(options) {
16454
16686
  const result = filtered.join("\n");
16455
16687
  if (result.trim().length === 0) {
16456
16688
  if (!dryRun) {
16457
- fs4.unlinkSync(envPath);
16689
+ fs5.unlinkSync(envPath);
16458
16690
  }
16459
16691
  summary.push(`${prefix}Deleted .env.local (no remaining entries)`);
16460
16692
  } else {
16461
16693
  if (!dryRun) {
16462
- fs4.writeFileSync(envPath, result, "utf-8");
16694
+ fs5.writeFileSync(envPath, result, "utf-8");
16463
16695
  }
16464
16696
  let devKeyAnnotation = "";
16465
16697
  if (devKeyPath === "interactive-confirmed") {
@@ -16482,9 +16714,9 @@ async function runUninit(options) {
16482
16714
  );
16483
16715
  }
16484
16716
  try {
16485
- const gitignorePath = path4.join(projectRoot, ".gitignore");
16486
- if (fs4.existsSync(gitignorePath)) {
16487
- const content = fs4.readFileSync(gitignorePath, "utf-8");
16717
+ const gitignorePath = path5.join(projectRoot, ".gitignore");
16718
+ if (fs5.existsSync(gitignorePath)) {
16719
+ const content = fs5.readFileSync(gitignorePath, "utf-8");
16488
16720
  const lines = content.split("\n");
16489
16721
  const mcpGitignoreEntries = /* @__PURE__ */ new Set([
16490
16722
  ".glasstrace/",
@@ -16500,12 +16732,12 @@ async function runUninit(options) {
16500
16732
  const result = filtered.join("\n");
16501
16733
  if (result.trim().length === 0) {
16502
16734
  if (!dryRun) {
16503
- fs4.unlinkSync(gitignorePath);
16735
+ fs5.unlinkSync(gitignorePath);
16504
16736
  }
16505
16737
  summary.push(`${prefix}Deleted .gitignore (no remaining entries)`);
16506
16738
  } else {
16507
16739
  if (!dryRun) {
16508
- fs4.writeFileSync(gitignorePath, result, "utf-8");
16740
+ fs5.writeFileSync(gitignorePath, result, "utf-8");
16509
16741
  }
16510
16742
  summary.push(`${prefix}Removed Glasstrace entries from .gitignore`);
16511
16743
  }
@@ -16518,63 +16750,63 @@ async function runUninit(options) {
16518
16750
  }
16519
16751
  try {
16520
16752
  for (const configFile of MCP_CONFIG_FILES) {
16521
- const configPath = path4.join(projectRoot, configFile);
16522
- if (!fs4.existsSync(configPath)) {
16753
+ const configPath = path5.join(projectRoot, configFile);
16754
+ if (!fs5.existsSync(configPath)) {
16523
16755
  continue;
16524
16756
  }
16525
- const content = fs4.readFileSync(configPath, "utf-8");
16757
+ const content = fs5.readFileSync(configPath, "utf-8");
16526
16758
  const result = processJsonMcpConfig(content);
16527
16759
  if (result.action === "deleted") {
16528
16760
  if (!dryRun) {
16529
- fs4.unlinkSync(configPath);
16761
+ fs5.unlinkSync(configPath);
16530
16762
  }
16531
16763
  summary.push(`${prefix}Deleted ${configFile}`);
16532
16764
  } else if (result.action === "removed-key" && result.content !== void 0) {
16533
16765
  if (!dryRun) {
16534
- fs4.writeFileSync(configPath, result.content, "utf-8");
16766
+ fs5.writeFileSync(configPath, result.content, "utf-8");
16535
16767
  }
16536
16768
  summary.push(`${prefix}Removed glasstrace from ${configFile}`);
16537
16769
  }
16538
16770
  }
16539
- const codexConfigPath = path4.join(projectRoot, ".codex", "config.toml");
16540
- if (fs4.existsSync(codexConfigPath)) {
16541
- const content = fs4.readFileSync(codexConfigPath, "utf-8");
16771
+ const codexConfigPath = path5.join(projectRoot, ".codex", "config.toml");
16772
+ if (fs5.existsSync(codexConfigPath)) {
16773
+ const content = fs5.readFileSync(codexConfigPath, "utf-8");
16542
16774
  const tomlResult = processTomlMcpConfig(content);
16543
16775
  if (tomlResult.action === "deleted") {
16544
16776
  if (!dryRun) {
16545
- fs4.unlinkSync(codexConfigPath);
16777
+ fs5.unlinkSync(codexConfigPath);
16546
16778
  }
16547
16779
  summary.push(`${prefix}Deleted .codex/config.toml`);
16548
16780
  } else if (tomlResult.action === "removed-section" && tomlResult.content !== void 0) {
16549
16781
  if (!dryRun) {
16550
- fs4.writeFileSync(codexConfigPath, tomlResult.content, "utf-8");
16782
+ fs5.writeFileSync(codexConfigPath, tomlResult.content, "utf-8");
16551
16783
  }
16552
16784
  summary.push(`${prefix}Removed glasstrace from .codex/config.toml`);
16553
16785
  }
16554
16786
  }
16555
- const hasWindsurfMarkers = fs4.existsSync(path4.join(projectRoot, ".windsurfrules")) || fs4.existsSync(path4.join(projectRoot, ".windsurf"));
16787
+ const hasWindsurfMarkers = fs5.existsSync(path5.join(projectRoot, ".windsurfrules")) || fs5.existsSync(path5.join(projectRoot, ".windsurf"));
16556
16788
  if (hasWindsurfMarkers) {
16557
- const windsurfConfigPath = path4.join(
16789
+ const windsurfConfigPath = path5.join(
16558
16790
  os.homedir(),
16559
16791
  ".codeium",
16560
16792
  "windsurf",
16561
16793
  "mcp_config.json"
16562
16794
  );
16563
- if (fs4.existsSync(windsurfConfigPath)) {
16564
- const content = fs4.readFileSync(windsurfConfigPath, "utf-8");
16795
+ if (fs5.existsSync(windsurfConfigPath)) {
16796
+ const content = fs5.readFileSync(windsurfConfigPath, "utf-8");
16565
16797
  const windsurfResult = processJsonMcpConfig(content);
16566
16798
  const home = os.homedir();
16567
16799
  const displayPath = windsurfConfigPath.startsWith(home) ? "~" + windsurfConfigPath.slice(home.length) : windsurfConfigPath;
16568
16800
  if (windsurfResult.action === "deleted") {
16569
16801
  if (!dryRun) {
16570
- fs4.unlinkSync(windsurfConfigPath);
16802
+ fs5.unlinkSync(windsurfConfigPath);
16571
16803
  }
16572
16804
  summary.push(
16573
16805
  `${prefix}Deleted global Windsurf config (${displayPath})`
16574
16806
  );
16575
16807
  } else if (windsurfResult.action === "removed-key" && windsurfResult.content !== void 0) {
16576
16808
  if (!dryRun) {
16577
- fs4.writeFileSync(windsurfConfigPath, windsurfResult.content, "utf-8");
16809
+ fs5.writeFileSync(windsurfConfigPath, windsurfResult.content, "utf-8");
16578
16810
  }
16579
16811
  summary.push(
16580
16812
  `${prefix}Removed glasstrace from global Windsurf config (${displayPath})`
@@ -16589,21 +16821,21 @@ async function runUninit(options) {
16589
16821
  }
16590
16822
  try {
16591
16823
  for (const infoFile of AGENT_INFO_FILES) {
16592
- const filePath = path4.join(projectRoot, infoFile);
16593
- if (!fs4.existsSync(filePath)) {
16824
+ const filePath = path5.join(projectRoot, infoFile);
16825
+ if (!fs5.existsSync(filePath)) {
16594
16826
  continue;
16595
16827
  }
16596
- const content = fs4.readFileSync(filePath, "utf-8");
16828
+ const content = fs5.readFileSync(filePath, "utf-8");
16597
16829
  const result = removeMarkerSection(content);
16598
16830
  if (result.removed) {
16599
16831
  if (result.content.trim().length === 0) {
16600
16832
  if (!dryRun) {
16601
- fs4.unlinkSync(filePath);
16833
+ fs5.unlinkSync(filePath);
16602
16834
  }
16603
16835
  summary.push(`${prefix}Deleted ${infoFile} (only contained Glasstrace section)`);
16604
16836
  } else {
16605
16837
  if (!dryRun) {
16606
- fs4.writeFileSync(filePath, result.content, "utf-8");
16838
+ fs5.writeFileSync(filePath, result.content, "utf-8");
16607
16839
  }
16608
16840
  summary.push(`${prefix}Removed Glasstrace section from ${infoFile}`);
16609
16841
  }
@@ -16619,15 +16851,16 @@ async function runUninit(options) {
16619
16851
  }
16620
16852
  return { exitCode: errors.length > 0 ? 1 : 0, summary, warnings, errors };
16621
16853
  }
16622
- var fs4, os, path4, MCP_CONFIG_FILES, AGENT_INFO_FILES;
16854
+ var fs5, os, path5, MCP_CONFIG_FILES, AGENT_INFO_FILES;
16623
16855
  var init_uninit = __esm({
16624
16856
  "src/cli/uninit.ts"() {
16625
16857
  "use strict";
16626
- fs4 = __toESM(require("node:fs"), 1);
16858
+ fs5 = __toESM(require("node:fs"), 1);
16627
16859
  os = __toESM(require("node:os"), 1);
16628
- path4 = __toESM(require("node:path"), 1);
16860
+ path5 = __toESM(require("node:path"), 1);
16629
16861
  init_constants();
16630
16862
  init_scaffolder();
16863
+ init_discovery_file();
16631
16864
  MCP_CONFIG_FILES = [".mcp.json", ".cursor/mcp.json", ".gemini/settings.json"];
16632
16865
  AGENT_INFO_FILES = [
16633
16866
  "CLAUDE.md",
@@ -16673,11 +16906,11 @@ async function registerViaCli(agent, anonKey) {
16673
16906
  MCP_ENDPOINT
16674
16907
  ]);
16675
16908
  const configPath = agent.mcpConfigPath;
16676
- if (configPath !== null && fs5.existsSync(configPath)) {
16677
- const content = fs5.readFileSync(configPath, "utf-8");
16909
+ if (configPath !== null && fs6.existsSync(configPath)) {
16910
+ const content = fs6.readFileSync(configPath, "utf-8");
16678
16911
  if (!content.includes("bearer_token_env_var")) {
16679
16912
  const appendContent = content.endsWith("\n") ? "" : "\n";
16680
- fs5.writeFileSync(
16913
+ fs6.writeFileSync(
16681
16914
  configPath,
16682
16915
  content + appendContent + 'bearer_token_env_var = "GLASSTRACE_API_KEY"\n',
16683
16916
  "utf-8"
@@ -16722,8 +16955,8 @@ async function mcpAdd(options) {
16722
16955
  messages: ["Error: Run `glasstrace init` first to generate an API key."]
16723
16956
  };
16724
16957
  }
16725
- const markerPath = path5.join(projectRoot, ".glasstrace", "mcp-connected");
16726
- if (fs5.existsSync(markerPath) && !force) {
16958
+ const markerPath = path6.join(projectRoot, ".glasstrace", "mcp-connected");
16959
+ if (fs6.existsSync(markerPath) && !force) {
16727
16960
  return {
16728
16961
  exitCode: 0,
16729
16962
  results: [],
@@ -16782,7 +17015,7 @@ async function mcpAdd(options) {
16782
17015
  try {
16783
17016
  const configContent = generateMcpConfig(agent, MCP_ENDPOINT, anonKey);
16784
17017
  await writeMcpConfig(agent, configContent, projectRoot);
16785
- if (fs5.existsSync(agent.mcpConfigPath)) {
17018
+ if (fs6.existsSync(agent.mcpConfigPath)) {
16786
17019
  const infoContent = generateInfoSection(agent, MCP_ENDPOINT);
16787
17020
  if (infoContent !== "") {
16788
17021
  await injectInfoSection(agent, infoContent, projectRoot);
@@ -16849,13 +17082,13 @@ async function mcpAdd(options) {
16849
17082
  }
16850
17083
  return { exitCode: 0, results, messages };
16851
17084
  }
16852
- var import_node_child_process2, fs5, path5, import_node_util, execFileAsync;
17085
+ var import_node_child_process2, fs6, path6, import_node_util, execFileAsync;
16853
17086
  var init_mcp_add = __esm({
16854
17087
  "src/cli/mcp-add.ts"() {
16855
17088
  "use strict";
16856
17089
  import_node_child_process2 = require("node:child_process");
16857
- fs5 = __toESM(require("node:fs"), 1);
16858
- path5 = __toESM(require("node:path"), 1);
17090
+ fs6 = __toESM(require("node:fs"), 1);
17091
+ path6 = __toESM(require("node:path"), 1);
16859
17092
  import_node_util = require("node:util");
16860
17093
  init_anon_key();
16861
17094
  init_detect();
@@ -16886,15 +17119,15 @@ function hasRegisterGlasstraceImport(content) {
16886
17119
  function runValidate(options) {
16887
17120
  const { projectRoot } = options;
16888
17121
  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");
17122
+ const glasstraceDir = path7.join(projectRoot, ".glasstrace");
17123
+ const instrumentationPath = path7.join(projectRoot, "instrumentation.ts");
17124
+ const markerPath = path7.join(glasstraceDir, "mcp-connected");
16892
17125
  const glasstraceDirExists = isDirectorySafe(glasstraceDir);
16893
- const instrumentationExists = fs6.existsSync(instrumentationPath);
17126
+ const instrumentationExists = fs7.existsSync(instrumentationPath);
16894
17127
  const instrumentationContent = instrumentationExists ? safeReadFile(instrumentationPath) : null;
16895
- const markerExists = fs6.existsSync(markerPath);
17128
+ const markerExists = fs7.existsSync(markerPath);
16896
17129
  const mcpConfigsPresent = MCP_CONFIG_CANDIDATES.filter(
16897
- (rel) => fs6.existsSync(path6.join(projectRoot, rel))
17130
+ (rel) => fs7.existsSync(path7.join(projectRoot, rel))
16898
17131
  );
16899
17132
  if (glasstraceDirExists) {
16900
17133
  if (instrumentationContent === null || !hasRegisterGlasstraceImport(instrumentationContent)) {
@@ -16944,25 +17177,25 @@ function runValidate(options) {
16944
17177
  }
16945
17178
  function safeReadFile(filePath) {
16946
17179
  try {
16947
- return fs6.readFileSync(filePath, "utf-8");
17180
+ return fs7.readFileSync(filePath, "utf-8");
16948
17181
  } catch {
16949
17182
  return null;
16950
17183
  }
16951
17184
  }
16952
17185
  function isDirectorySafe(dirPath) {
16953
17186
  try {
16954
- if (!fs6.existsSync(dirPath)) return false;
16955
- return fs6.statSync(dirPath).isDirectory();
17187
+ if (!fs7.existsSync(dirPath)) return false;
17188
+ return fs7.statSync(dirPath).isDirectory();
16956
17189
  } catch {
16957
17190
  return false;
16958
17191
  }
16959
17192
  }
16960
- var fs6, path6, MCP_CONFIG_CANDIDATES;
17193
+ var fs7, path7, MCP_CONFIG_CANDIDATES;
16961
17194
  var init_validate = __esm({
16962
17195
  "src/cli/validate.ts"() {
16963
17196
  "use strict";
16964
- fs6 = __toESM(require("node:fs"), 1);
16965
- path6 = __toESM(require("node:path"), 1);
17197
+ fs7 = __toESM(require("node:fs"), 1);
17198
+ path7 = __toESM(require("node:path"), 1);
16966
17199
  MCP_CONFIG_CANDIDATES = [
16967
17200
  ".mcp.json",
16968
17201
  ".cursor/mcp.json",
@@ -16992,8 +17225,8 @@ function runStatus(options) {
16992
17225
  }
16993
17226
  function checkInstalled(root) {
16994
17227
  try {
16995
- const pkgPath = path7.join(root, "package.json");
16996
- const content = fs7.readFileSync(pkgPath, "utf-8");
17228
+ const pkgPath = path8.join(root, "package.json");
17229
+ const content = fs8.readFileSync(pkgPath, "utf-8");
16997
17230
  const pkg = JSON.parse(content);
16998
17231
  const deps = pkg["dependencies"];
16999
17232
  const devDeps = pkg["devDependencies"];
@@ -17004,7 +17237,7 @@ function checkInstalled(root) {
17004
17237
  }
17005
17238
  function checkInitialized(root) {
17006
17239
  try {
17007
- return fs7.statSync(path7.join(root, ".glasstrace")).isDirectory();
17240
+ return fs8.statSync(path8.join(root, ".glasstrace")).isDirectory();
17008
17241
  } catch {
17009
17242
  return false;
17010
17243
  }
@@ -17012,7 +17245,7 @@ function checkInitialized(root) {
17012
17245
  function checkInstrumentation(root) {
17013
17246
  for (const name of INSTRUMENTATION_FILES) {
17014
17247
  try {
17015
- const content = fs7.readFileSync(path7.join(root, name), "utf-8");
17248
+ const content = fs8.readFileSync(path8.join(root, name), "utf-8");
17016
17249
  if (content.includes("registerGlasstrace")) {
17017
17250
  return true;
17018
17251
  }
@@ -17024,7 +17257,7 @@ function checkInstrumentation(root) {
17024
17257
  function checkConfigWrapped(root) {
17025
17258
  for (const name of NEXT_CONFIG_NAMES) {
17026
17259
  try {
17027
- const content = fs7.readFileSync(path7.join(root, name), "utf-8");
17260
+ const content = fs8.readFileSync(path8.join(root, name), "utf-8");
17028
17261
  if (content.includes("withGlasstraceConfig")) {
17029
17262
  return true;
17030
17263
  }
@@ -17035,7 +17268,7 @@ function checkConfigWrapped(root) {
17035
17268
  }
17036
17269
  function checkAnonKey(root) {
17037
17270
  try {
17038
- return fs7.statSync(path7.join(root, ".glasstrace", "anon_key")).isFile();
17271
+ return fs8.statSync(path8.join(root, ".glasstrace", "anon_key")).isFile();
17039
17272
  } catch {
17040
17273
  return false;
17041
17274
  }
@@ -17043,7 +17276,7 @@ function checkAnonKey(root) {
17043
17276
  function checkMcpConfigured(root) {
17044
17277
  for (const name of MCP_JSON_FILES) {
17045
17278
  try {
17046
- const content = fs7.readFileSync(path7.join(root, name), "utf-8");
17279
+ const content = fs8.readFileSync(path8.join(root, name), "utf-8");
17047
17280
  const parsed = JSON.parse(content);
17048
17281
  const mcpServers = parsed["mcpServers"];
17049
17282
  if (mcpServers && typeof mcpServers === "object" && "glasstrace" in mcpServers) {
@@ -17054,7 +17287,7 @@ function checkMcpConfigured(root) {
17054
17287
  }
17055
17288
  for (const name of MCP_TOML_FILES) {
17056
17289
  try {
17057
- const content = fs7.readFileSync(path7.join(root, name), "utf-8");
17290
+ const content = fs8.readFileSync(path8.join(root, name), "utf-8");
17058
17291
  if (content.includes("[mcp_servers.glasstrace]")) {
17059
17292
  return true;
17060
17293
  }
@@ -17067,7 +17300,7 @@ function checkAgents(root) {
17067
17300
  const found = [];
17068
17301
  for (const name of AGENT_INFO_FILES2) {
17069
17302
  try {
17070
- const content = fs7.readFileSync(path7.join(root, name), "utf-8");
17303
+ const content = fs8.readFileSync(path8.join(root, name), "utf-8");
17071
17304
  const hasHtmlMarkers = content.includes("<!-- glasstrace:mcp:start -->") && content.includes("<!-- glasstrace:mcp:end -->");
17072
17305
  const hasHashMarkers = content.includes("# glasstrace:mcp:start") && content.includes("# glasstrace:mcp:end");
17073
17306
  if (hasHtmlMarkers || hasHashMarkers) {
@@ -17090,8 +17323,8 @@ function readRuntimeState(root) {
17090
17323
  pid: null
17091
17324
  };
17092
17325
  try {
17093
- const filePath = path7.join(root, ".glasstrace", "runtime-state.json");
17094
- const content = fs7.readFileSync(filePath, "utf-8");
17326
+ const filePath = path8.join(root, ".glasstrace", "runtime-state.json");
17327
+ const content = fs8.readFileSync(filePath, "utf-8");
17095
17328
  const parsed = JSON.parse(content);
17096
17329
  const updatedAt = typeof parsed.updatedAt === "string" ? parsed.updatedAt : null;
17097
17330
  const pid = typeof parsed.pid === "number" ? parsed.pid : null;
@@ -17140,12 +17373,12 @@ function readRuntimeState(root) {
17140
17373
  return empty;
17141
17374
  }
17142
17375
  }
17143
- var fs7, path7, MCP_JSON_FILES, MCP_TOML_FILES, AGENT_INFO_FILES2, INSTRUMENTATION_FILES, STALE_THRESHOLD_MS;
17376
+ var fs8, path8, MCP_JSON_FILES, MCP_TOML_FILES, AGENT_INFO_FILES2, INSTRUMENTATION_FILES, STALE_THRESHOLD_MS;
17144
17377
  var init_status = __esm({
17145
17378
  "src/cli/status.ts"() {
17146
17379
  "use strict";
17147
- fs7 = __toESM(require("node:fs"), 1);
17148
- path7 = __toESM(require("node:path"), 1);
17380
+ fs8 = __toESM(require("node:fs"), 1);
17381
+ path8 = __toESM(require("node:path"), 1);
17149
17382
  init_constants();
17150
17383
  MCP_JSON_FILES = [".mcp.json", ".cursor/mcp.json", ".gemini/settings.json", ".glasstrace/mcp.json"];
17151
17384
  MCP_TOML_FILES = [".codex/config.toml"];
@@ -17170,14 +17403,15 @@ var init_status = __esm({
17170
17403
  var init_exports = {};
17171
17404
  __export(init_exports, {
17172
17405
  decideMcpConfigAction: () => decideMcpConfigAction,
17406
+ gitignoreExcludesDiscoveryFile: () => gitignoreExcludesDiscoveryFile,
17173
17407
  meetsNodeVersion: () => meetsNodeVersion,
17174
17408
  rollbackSteps: () => rollbackSteps,
17175
17409
  runInit: () => runInit,
17176
17410
  verifyAnonKeyRegistration: () => verifyAnonKeyRegistration
17177
17411
  });
17178
17412
  module.exports = __toCommonJS(init_exports);
17179
- var fs8 = __toESM(require("node:fs"), 1);
17180
- var path8 = __toESM(require("node:path"), 1);
17413
+ var fs9 = __toESM(require("node:fs"), 1);
17414
+ var path9 = __toESM(require("node:path"), 1);
17181
17415
  var readline = __toESM(require("node:readline"), 1);
17182
17416
  init_scaffolder();
17183
17417
 
@@ -17705,6 +17939,7 @@ function resolveConfig(options) {
17705
17939
  }
17706
17940
 
17707
17941
  // src/cli/init.ts
17942
+ init_discovery_file();
17708
17943
  function meetsNodeVersion(minMajor) {
17709
17944
  const [major] = process.versions.node.split(".").map(Number);
17710
17945
  return major >= minMajor;
@@ -17712,8 +17947,8 @@ function meetsNodeVersion(minMajor) {
17712
17947
  async function decideMcpConfigAction(options) {
17713
17948
  const { configPath, expectedContent, force } = options;
17714
17949
  if (configPath === null) return "write";
17715
- const exists = options.existsSync ?? fs8.existsSync;
17716
- const read = options.readFile ?? ((p) => fs8.readFileSync(p, "utf-8"));
17950
+ const exists = options.existsSync ?? fs9.existsSync;
17951
+ const read = options.readFile ?? ((p) => fs9.readFileSync(p, "utf-8"));
17717
17952
  const prompt = options.prompt ?? promptYesNo;
17718
17953
  if (!exists(configPath)) return "write";
17719
17954
  let existingContent;
@@ -17758,22 +17993,69 @@ async function promptYesNo(question, defaultValue) {
17758
17993
  function cleanLeadingBlankLines2(content) {
17759
17994
  return content.replace(/^\n{2,}/, "\n");
17760
17995
  }
17996
+ function gitignoreExcludesDiscoveryFile(gitignoreContent, layout) {
17997
+ const staticRoot = layout === "static" ? "static" : "public";
17998
+ const rootTargets = [staticRoot, `${staticRoot}/`];
17999
+ const wellKnownTargets = [
18000
+ `${staticRoot}/.well-known`,
18001
+ `${staticRoot}/.well-known/`,
18002
+ ".well-known",
18003
+ ".well-known/"
18004
+ ];
18005
+ const fileTargets = [
18006
+ `${staticRoot}/.well-known/glasstrace.json`,
18007
+ ".well-known/glasstrace.json"
18008
+ ];
18009
+ let rootIgnored = false;
18010
+ let wellKnownIgnored = false;
18011
+ let fileIgnored = false;
18012
+ for (const rawLine of gitignoreContent.split("\n")) {
18013
+ const line = rawLine.trim();
18014
+ if (line === "" || line.startsWith("#")) continue;
18015
+ const negation = line.startsWith("!");
18016
+ const pattern = negation ? line.slice(1).trim() : line;
18017
+ if (pattern === "") continue;
18018
+ const normalized = pattern.startsWith("/") ? pattern.slice(1) : pattern;
18019
+ const matchesRoot = matchesDiscoveryPath(normalized, rootTargets);
18020
+ const matchesWellKnown = matchesDiscoveryPath(normalized, wellKnownTargets);
18021
+ const matchesFile = matchesDiscoveryPath(normalized, fileTargets);
18022
+ if (!matchesRoot && !matchesWellKnown && !matchesFile) continue;
18023
+ const newState = !negation;
18024
+ if (matchesRoot) rootIgnored = newState;
18025
+ if (matchesWellKnown) wellKnownIgnored = newState;
18026
+ if (matchesFile) fileIgnored = newState;
18027
+ }
18028
+ return rootIgnored || wellKnownIgnored || fileIgnored;
18029
+ }
18030
+ function matchesDiscoveryPath(pattern, targets) {
18031
+ const bare = pattern.endsWith("/") ? pattern.slice(0, -1) : pattern;
18032
+ for (const target of targets) {
18033
+ const tBare = target.endsWith("/") ? target.slice(0, -1) : target;
18034
+ if (bare === tBare) return true;
18035
+ if (pattern.startsWith("**/")) {
18036
+ const suffix = pattern.slice(3);
18037
+ const sBare = suffix.endsWith("/") ? suffix.slice(0, -1) : suffix;
18038
+ if (tBare === sBare || tBare.endsWith(`/${sBare}`)) return true;
18039
+ }
18040
+ }
18041
+ return false;
18042
+ }
17761
18043
  async function rollbackSteps(steps, projectRoot, state) {
17762
18044
  for (const step of [...steps].reverse()) {
17763
18045
  try {
17764
18046
  switch (step) {
17765
18047
  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");
18048
+ const instrPath = state?.instrumentationPath ?? path9.join(projectRoot, "instrumentation.ts");
18049
+ if (fs9.existsSync(instrPath)) {
18050
+ const content = fs9.readFileSync(instrPath, "utf-8");
17769
18051
  if (isInitCreatedInstrumentation(content)) {
17770
- fs8.unlinkSync(instrPath);
18052
+ fs9.unlinkSync(instrPath);
17771
18053
  } else if (state?.originalInstrumentationContent !== void 0) {
17772
- fs8.writeFileSync(instrPath, state.originalInstrumentationContent, "utf-8");
18054
+ fs9.writeFileSync(instrPath, state.originalInstrumentationContent, "utf-8");
17773
18055
  } else {
17774
18056
  const cleaned = removeRegisterGlasstrace(content);
17775
18057
  if (cleaned !== content) {
17776
- fs8.writeFileSync(instrPath, cleaned, "utf-8");
18058
+ fs9.writeFileSync(instrPath, cleaned, "utf-8");
17777
18059
  }
17778
18060
  }
17779
18061
  }
@@ -17781,11 +18063,11 @@ async function rollbackSteps(steps, projectRoot, state) {
17781
18063
  }
17782
18064
  case "next-config": {
17783
18065
  for (const name of NEXT_CONFIG_NAMES) {
17784
- const configPath = path8.join(projectRoot, name);
17785
- if (!fs8.existsSync(configPath)) {
18066
+ const configPath = path9.join(projectRoot, name);
18067
+ if (!fs9.existsSync(configPath)) {
17786
18068
  continue;
17787
18069
  }
17788
- const content = fs8.readFileSync(configPath, "utf-8");
18070
+ const content = fs9.readFileSync(configPath, "utf-8");
17789
18071
  if (!content.includes("withGlasstraceConfig")) {
17790
18072
  continue;
17791
18073
  }
@@ -17793,16 +18075,16 @@ async function rollbackSteps(steps, projectRoot, state) {
17793
18075
  const unwrapResult = isESM ? unwrapExport(content) : unwrapCJSExport(content);
17794
18076
  if (unwrapResult.unwrapped) {
17795
18077
  const cleaned = removeGlasstraceConfigImport(unwrapResult.content);
17796
- fs8.writeFileSync(configPath, cleanLeadingBlankLines2(cleaned), "utf-8");
18078
+ fs9.writeFileSync(configPath, cleanLeadingBlankLines2(cleaned), "utf-8");
17797
18079
  }
17798
18080
  break;
17799
18081
  }
17800
18082
  break;
17801
18083
  }
17802
18084
  case "env-local": {
17803
- const envPath = path8.join(projectRoot, ".env.local");
17804
- if (fs8.existsSync(envPath)) {
17805
- const content = fs8.readFileSync(envPath, "utf-8");
18085
+ const envPath = path9.join(projectRoot, ".env.local");
18086
+ if (fs9.existsSync(envPath)) {
18087
+ const content = fs9.readFileSync(envPath, "utf-8");
17806
18088
  const lines = content.split("\n");
17807
18089
  const filtered = lines.filter((line) => {
17808
18090
  const trimmed = line.trim();
@@ -17811,18 +18093,18 @@ async function rollbackSteps(steps, projectRoot, state) {
17811
18093
  if (filtered.length !== lines.length) {
17812
18094
  const result = filtered.join("\n");
17813
18095
  if (result.trim().length === 0) {
17814
- fs8.unlinkSync(envPath);
18096
+ fs9.unlinkSync(envPath);
17815
18097
  } else {
17816
- fs8.writeFileSync(envPath, result, "utf-8");
18098
+ fs9.writeFileSync(envPath, result, "utf-8");
17817
18099
  }
17818
18100
  }
17819
18101
  }
17820
18102
  break;
17821
18103
  }
17822
18104
  case "gitignore": {
17823
- const gitignorePath = path8.join(projectRoot, ".gitignore");
17824
- if (fs8.existsSync(gitignorePath)) {
17825
- const content = fs8.readFileSync(gitignorePath, "utf-8");
18105
+ const gitignorePath = path9.join(projectRoot, ".gitignore");
18106
+ if (fs9.existsSync(gitignorePath)) {
18107
+ const content = fs9.readFileSync(gitignorePath, "utf-8");
17826
18108
  const lines = content.split("\n");
17827
18109
  const filtered = lines.filter(
17828
18110
  (line) => line.trim() !== ".glasstrace/"
@@ -17830,14 +18112,18 @@ async function rollbackSteps(steps, projectRoot, state) {
17830
18112
  if (filtered.length !== lines.length) {
17831
18113
  const result = filtered.join("\n");
17832
18114
  if (result.trim().length === 0) {
17833
- fs8.unlinkSync(gitignorePath);
18115
+ fs9.unlinkSync(gitignorePath);
17834
18116
  } else {
17835
- fs8.writeFileSync(gitignorePath, result, "utf-8");
18117
+ fs9.writeFileSync(gitignorePath, result, "utf-8");
17836
18118
  }
17837
18119
  }
17838
18120
  }
17839
18121
  break;
17840
18122
  }
18123
+ case "discovery-file": {
18124
+ removeDiscoveryFile(projectRoot);
18125
+ break;
18126
+ }
17841
18127
  }
17842
18128
  } catch {
17843
18129
  }
@@ -17859,8 +18145,8 @@ async function runInit(options) {
17859
18145
  errors.push(err instanceof Error ? err.message : String(err));
17860
18146
  return { exitCode: 1, summary, warnings, errors };
17861
18147
  }
17862
- const packageJsonPath = path8.join(projectRoot, "package.json");
17863
- if (!fs8.existsSync(packageJsonPath)) {
18148
+ const packageJsonPath = path9.join(projectRoot, "package.json");
18149
+ if (!fs9.existsSync(packageJsonPath)) {
17864
18150
  errors.push("No package.json found. Run this command from a Node.js project root.");
17865
18151
  return { exitCode: 1, summary, warnings, errors };
17866
18152
  }
@@ -17869,8 +18155,8 @@ async function runInit(options) {
17869
18155
  const preResolved = resolveInstrumentationTarget(projectRoot);
17870
18156
  if (!preResolved.conflict && preResolved.target !== null) {
17871
18157
  rollbackState.instrumentationPath = preResolved.target;
17872
- if (fs8.existsSync(preResolved.target)) {
17873
- rollbackState.originalInstrumentationContent = fs8.readFileSync(
18158
+ if (fs9.existsSync(preResolved.target)) {
18159
+ rollbackState.originalInstrumentationContent = fs9.readFileSync(
17874
18160
  preResolved.target,
17875
18161
  "utf-8"
17876
18162
  );
@@ -17885,7 +18171,7 @@ async function runInit(options) {
17885
18171
  if (instrResult.filePath !== void 0) {
17886
18172
  rollbackState.instrumentationPath = instrResult.filePath;
17887
18173
  }
17888
- const relativePath = instrResult.filePath !== void 0 ? path8.relative(projectRoot, instrResult.filePath) : "instrumentation.ts";
18174
+ const relativePath = instrResult.filePath !== void 0 ? path9.relative(projectRoot, instrResult.filePath) : "instrumentation.ts";
17889
18175
  switch (instrResult.action) {
17890
18176
  case "created":
17891
18177
  summary.push(`Created ${relativePath}`);
@@ -17910,8 +18196,8 @@ async function runInit(options) {
17910
18196
  );
17911
18197
  break;
17912
18198
  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";
18199
+ const primary = instrResult.filePath !== void 0 ? path9.relative(projectRoot, instrResult.filePath) : "src/instrumentation.ts";
18200
+ const competing = instrResult.conflictingPath !== void 0 ? path9.relative(projectRoot, instrResult.conflictingPath) : "instrumentation.ts";
17915
18201
  await rollbackSteps(rollbackState.steps, projectRoot, rollbackState);
17916
18202
  errors.push(
17917
18203
  `Both ${primary} and ${competing} exist. Next.js's loader behavior is undefined when both are present.
@@ -17958,10 +18244,10 @@ Then add this as the first statement in your register() function:
17958
18244
  return { exitCode: 1, summary, warnings, errors };
17959
18245
  }
17960
18246
  try {
17961
- const envPathForCheck = path8.join(projectRoot, ".env.local");
18247
+ const envPathForCheck = path9.join(projectRoot, ".env.local");
17962
18248
  let existingDevKey = false;
17963
- if (fs8.existsSync(envPathForCheck)) {
17964
- const existingContent = fs8.readFileSync(envPathForCheck, "utf-8");
18249
+ if (fs9.existsSync(envPathForCheck)) {
18250
+ const existingContent = fs9.readFileSync(envPathForCheck, "utf-8");
17965
18251
  existingDevKey = isDevApiKey(readEnvLocalApiKey(existingContent));
17966
18252
  }
17967
18253
  const envCreated = await scaffoldEnvLocal(projectRoot);
@@ -18001,11 +18287,55 @@ Then add this as the first statement in your register() function:
18001
18287
  if (preExistingAnonKey !== null) {
18002
18288
  summary.push("Preserved existing .glasstrace/anon_key");
18003
18289
  }
18290
+ try {
18291
+ const discoveryResult = writeDiscoveryFile(projectRoot, anonKey);
18292
+ const relPath = relativeDiscoveryPath(discoveryResult.layout);
18293
+ switch (discoveryResult.action) {
18294
+ case "created":
18295
+ summary.push(`Created ${relPath}`);
18296
+ rollbackState.steps.push("discovery-file");
18297
+ break;
18298
+ case "updated-stale":
18299
+ summary.push(`Updated ${relPath} (anon key had changed)`);
18300
+ break;
18301
+ case "skipped-matches":
18302
+ summary.push(`Skipped ${relPath} (already matches anon key)`);
18303
+ break;
18304
+ case "skipped-foreign":
18305
+ summary.push(
18306
+ `Rewrote ${relPath} (existing file was malformed or not SDK-managed)`
18307
+ );
18308
+ break;
18309
+ case "failed":
18310
+ warnings.push(
18311
+ `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.`
18312
+ );
18313
+ break;
18314
+ }
18315
+ const gitignorePath = path9.join(projectRoot, ".gitignore");
18316
+ if (fs9.existsSync(gitignorePath)) {
18317
+ try {
18318
+ const gitignoreContent = fs9.readFileSync(gitignorePath, "utf-8");
18319
+ if (gitignoreExcludesDiscoveryFile(gitignoreContent, discoveryResult.layout)) {
18320
+ warnings.push(
18321
+ `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 + "`)."
18322
+ );
18323
+ }
18324
+ } catch {
18325
+ }
18326
+ }
18327
+ } catch (err) {
18328
+ warnings.push(
18329
+ `Failed to write ${relativeDiscoveryPath(
18330
+ resolveStaticRoot(projectRoot).layout
18331
+ )}: ${err instanceof Error ? err.message : String(err)}`
18332
+ );
18333
+ }
18004
18334
  let anyConfigWritten = false;
18005
18335
  if (isCI) {
18006
18336
  const genericAgent = {
18007
18337
  name: "generic",
18008
- mcpConfigPath: path8.join(projectRoot, ".glasstrace", "mcp.json"),
18338
+ mcpConfigPath: path9.join(projectRoot, ".glasstrace", "mcp.json"),
18009
18339
  infoFilePath: null,
18010
18340
  cliAvailable: false,
18011
18341
  registrationCommand: null
@@ -18019,7 +18349,7 @@ Then add this as the first statement in your register() function:
18019
18349
  if (decision !== "skip") {
18020
18350
  await writeMcpConfig(genericAgent, genericConfig, projectRoot);
18021
18351
  }
18022
- if (genericAgent.mcpConfigPath !== null && fs8.existsSync(genericAgent.mcpConfigPath)) {
18352
+ if (genericAgent.mcpConfigPath !== null && fs9.existsSync(genericAgent.mcpConfigPath)) {
18023
18353
  anyConfigWritten = true;
18024
18354
  summary.push("Created .glasstrace/mcp.json (CI mode)");
18025
18355
  }
@@ -18033,14 +18363,14 @@ Then add this as the first statement in your register() function:
18033
18363
  );
18034
18364
  const genericAgent = {
18035
18365
  name: "generic",
18036
- mcpConfigPath: path8.join(projectRoot, ".glasstrace", "mcp.json"),
18366
+ mcpConfigPath: path9.join(projectRoot, ".glasstrace", "mcp.json"),
18037
18367
  infoFilePath: null,
18038
18368
  cliAvailable: false,
18039
18369
  registrationCommand: null
18040
18370
  };
18041
18371
  const genericConfig = generateMcpConfig(genericAgent, MCP_ENDPOINT, anonKey);
18042
18372
  await writeMcpConfig(genericAgent, genericConfig, projectRoot);
18043
- if (genericAgent.mcpConfigPath !== null && fs8.existsSync(genericAgent.mcpConfigPath)) {
18373
+ if (genericAgent.mcpConfigPath !== null && fs9.existsSync(genericAgent.mcpConfigPath)) {
18044
18374
  anyConfigWritten = true;
18045
18375
  }
18046
18376
  agents = [];
@@ -18058,13 +18388,13 @@ Then add this as the first statement in your register() function:
18058
18388
  summary.push(
18059
18389
  `Preserved existing ${agent.mcpConfigPath ?? agent.name} (user declined overwrite)`
18060
18390
  );
18061
- if (agent.mcpConfigPath !== null && fs8.existsSync(agent.mcpConfigPath)) {
18391
+ if (agent.mcpConfigPath !== null && fs9.existsSync(agent.mcpConfigPath)) {
18062
18392
  anyConfigWritten = true;
18063
18393
  }
18064
18394
  continue;
18065
18395
  }
18066
18396
  await writeMcpConfig(agent, configContent, projectRoot);
18067
- const configExists = agent.mcpConfigPath !== null && fs8.existsSync(agent.mcpConfigPath);
18397
+ const configExists = agent.mcpConfigPath !== null && fs9.existsSync(agent.mcpConfigPath);
18068
18398
  if (!configExists) {
18069
18399
  continue;
18070
18400
  }
@@ -18150,9 +18480,9 @@ async function verifyAnonKeyRegistration(projectRoot) {
18150
18480
  }
18151
18481
  let devKey;
18152
18482
  try {
18153
- const envPath = path8.join(projectRoot, ".env.local");
18154
- if (fs8.existsSync(envPath)) {
18155
- const envContent = fs8.readFileSync(envPath, "utf-8");
18483
+ const envPath = path9.join(projectRoot, ".env.local");
18484
+ if (fs9.existsSync(envPath)) {
18485
+ const envContent = fs9.readFileSync(envPath, "utf-8");
18156
18486
  const effective = readEnvLocalApiKey(envContent);
18157
18487
  if (effective !== null && isDevApiKey(effective)) {
18158
18488
  devKey = effective;
@@ -18162,7 +18492,7 @@ async function verifyAnonKeyRegistration(projectRoot) {
18162
18492
  }
18163
18493
  const baseConfig = resolveConfig({ apiKey: devKey });
18164
18494
  const config2 = { ...baseConfig, apiKey: devKey };
18165
- const sdkVersion = true ? "0.19.0" : "0.0.0-dev";
18495
+ const sdkVersion = true ? "0.20.1" : "0.0.0-dev";
18166
18496
  const result = await verifyInitReachable(config2, anonKey, sdkVersion);
18167
18497
  if (result.ok) {
18168
18498
  return { outcome: "verified" };
@@ -18211,7 +18541,7 @@ function parseArgs(argv) {
18211
18541
  };
18212
18542
  }
18213
18543
  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;
18544
+ var scriptBasename = scriptPath !== void 0 ? path9.basename(scriptPath) : void 0;
18215
18545
  var isDirectExecution = scriptPath !== void 0 && (scriptPath.endsWith("/cli/init.js") || scriptPath.endsWith("/cli/init.ts") || scriptBasename === "glasstrace");
18216
18546
  if (isDirectExecution) {
18217
18547
  if (!meetsNodeVersion(20)) {
@@ -18412,6 +18742,7 @@ Usage:
18412
18742
  // Annotate the CommonJS export names for ESM import in node:
18413
18743
  0 && (module.exports = {
18414
18744
  decideMcpConfigAction,
18745
+ gitignoreExcludesDiscoveryFile,
18415
18746
  meetsNodeVersion,
18416
18747
  rollbackSteps,
18417
18748
  runInit,