@glasstrace/sdk 1.3.7 → 1.3.8
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/{chunk-YSBGMBI5.js → chunk-ADUD4PEK.js} +2 -2
- package/dist/{chunk-LW7474ZA.js → chunk-E23HN24Z.js} +7 -7
- package/dist/{chunk-V2S7BSV4.js → chunk-GYTCZSAV.js} +47 -2
- package/dist/chunk-GYTCZSAV.js.map +1 -0
- package/dist/{chunk-TUFUV43S.js → chunk-LJMZXJ45.js} +2 -2
- package/dist/{chunk-TZMYGEMB.js → chunk-NKRD3MKG.js} +2 -1
- package/dist/chunk-NKRD3MKG.js.map +1 -0
- package/dist/cli/init.cjs +2 -1
- package/dist/cli/init.cjs.map +1 -1
- package/dist/cli/init.js +5 -5
- package/dist/cli/mcp-add.cjs +1 -0
- package/dist/cli/mcp-add.cjs.map +1 -1
- package/dist/cli/mcp-add.js +2 -2
- package/dist/cli/uninit.cjs.map +1 -1
- package/dist/cli/uninit.js +2 -2
- package/dist/cli/validate.cjs.map +1 -1
- package/dist/cli/validate.js +1 -1
- package/dist/index.cjs +50 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3 -3
- package/dist/node-entry.cjs +50 -5
- package/dist/node-entry.cjs.map +1 -1
- package/dist/node-entry.js +3 -3
- package/package.json +1 -1
- package/dist/chunk-TZMYGEMB.js.map +0 -1
- package/dist/chunk-V2S7BSV4.js.map +0 -1
- /package/dist/{chunk-YSBGMBI5.js.map → chunk-ADUD4PEK.js.map} +0 -0
- /package/dist/{chunk-LW7474ZA.js.map → chunk-E23HN24Z.js.map} +0 -0
- /package/dist/{chunk-TUFUV43S.js.map → chunk-LJMZXJ45.js.map} +0 -0
package/dist/cli/uninit.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/uninit.ts","../../src/cli/constants.ts","../../src/atomic-write.ts","../../src/mcp-runtime.ts","../../src/cli/discovery-file.ts"],"sourcesContent":["import * as fs from \"node:fs\";\nimport * as os from \"node:os\";\nimport * as path from \"node:path\";\nimport { NEXT_CONFIG_NAMES } from \"./constants.js\";\nimport { readEnvLocalApiKey, isDevApiKey } from \"../mcp-runtime.js\";\nimport { atomicWriteFileSync } from \"../atomic-write.js\";\nimport {\n removeDiscoveryFile,\n relativeDiscoveryPath,\n} from \"./discovery-file.js\";\n\n/**\n * Options for the uninit command.\n */\nexport interface UninitOptions {\n projectRoot: string;\n dryRun: boolean;\n /**\n * When true, skip interactive confirmation before destructive actions\n * such as removing a claimed developer API key from `.env.local`\n * (DISC-1247 Scenario 6).\n */\n force?: boolean;\n /**\n * Optional prompt callback; when omitted, uninit uses a TTY-based\n * `readline` prompt in interactive mode and defaults to `false`\n * (abort) when no TTY is attached. Exposed for testing.\n */\n prompt?: (question: string, defaultValue: boolean) => Promise<boolean>;\n}\n\n/**\n * Result of running the uninit command.\n */\nexport interface UninitResult {\n exitCode: number;\n summary: string[];\n warnings: string[];\n errors: string[];\n}\n\n/**\n * MCP config files that init may create.\n * These are JSON files containing `mcpServers.glasstrace`.\n */\nconst MCP_CONFIG_FILES = [\".mcp.json\", \".cursor/mcp.json\", \".gemini/settings.json\"] as const;\n\n/**\n * Agent info files that may contain glasstrace marker sections.\n * Both HTML-style (`<!-- glasstrace:mcp:start -->`) and hash-style\n * (`# glasstrace:mcp:start`) markers are supported.\n */\nconst AGENT_INFO_FILES = [\n \"CLAUDE.md\",\n \"codex.md\",\n \".cursorrules\",\n] as const;\n\n/**\n * Advances past a string literal (double-quoted, single-quoted, or template\n * literal), respecting backslash escapes.\n *\n * Note: Template literals with `${...}` interpolations containing nested\n * backticks are not fully supported — the scanner stops at the first\n * unescaped backtick. This is acceptable because config files (the primary\n * use case for `findMatchingParen`/`findMatchingBrace`) do not use nested\n * template literals.\n *\n * @param text - The source text.\n * @param start - The index of the opening quote character.\n * @param quote - The quote character (`\"`, `'`, or `` ` ``).\n * @returns The index immediately after the closing quote.\n * @internal Exported for unit testing only.\n */\nexport function skipString(text: string, start: number, quote: string): number {\n let i = start + 1;\n while (i < text.length) {\n if (text[i] === \"\\\\\") {\n i += 2;\n continue;\n }\n if (text[i] === quote) {\n return i + 1;\n }\n i++;\n }\n return text.length;\n}\n\n/**\n * Finds the matching closing delimiter for an opening delimiter at the given\n * position, accounting for nesting and skipping delimiters that appear inside\n * string literals (`\"`, `'`, `` ` ``), single-line comments (`//`), and block\n * comments.\n *\n * @param text - The source text to search.\n * @param openPos - The index of the opening delimiter.\n * @param openChar - The opening delimiter character (e.g., `(` or `{`).\n * @param closeChar - The closing delimiter character (e.g., `)` or `}`).\n * @returns The index of the matching closing delimiter, or -1 if not found.\n * @internal Exported for unit testing only.\n */\nexport function findMatchingDelimiter(\n text: string,\n openPos: number,\n openChar: string,\n closeChar: string,\n): number {\n let depth = 0;\n let i = openPos;\n while (i < text.length) {\n const ch = text[i];\n\n // Skip string literals\n if (ch === '\"' || ch === \"'\" || ch === \"`\") {\n i = skipString(text, i, ch);\n continue;\n }\n\n // Skip single-line comments.\n // Note: This may misidentify regex literals containing `//` (e.g.,\n // `/api\\//`). Config files — the primary use case — do not contain\n // regex literals, so this trade-off is acceptable.\n if (ch === \"/\" && text[i + 1] === \"/\") {\n const newline = text.indexOf(\"\\n\", i);\n if (newline === -1) {\n return -1;\n }\n i = newline + 1;\n continue;\n }\n\n // Skip block comments\n if (ch === \"/\" && text[i + 1] === \"*\") {\n const end = text.indexOf(\"*/\", i + 2);\n if (end === -1) {\n return -1;\n }\n i = end + 2;\n continue;\n }\n\n if (ch === openChar) {\n depth++;\n } else if (ch === closeChar) {\n depth--;\n if (depth === 0) {\n return i;\n }\n }\n i++;\n }\n return -1;\n}\n\n/**\n * Finds the matching closing parenthesis for an opening paren at the given\n * position, accounting for nested parentheses and skipping delimiters inside\n * string literals and comments.\n *\n * @param text - The source text to search.\n * @param openPos - The index of the opening `(`.\n * @returns The index of the matching `)`, or -1 if not found.\n * @internal Exported for unit testing only.\n */\nexport function findMatchingParen(text: string, openPos: number): number {\n return findMatchingDelimiter(text, openPos, \"(\", \")\");\n}\n\n/**\n * Removes the `withGlasstraceConfig(...)` wrapper from an ESM default export,\n * restoring the inner expression.\n *\n * Before: `export default withGlasstraceConfig(innerExpr);`\n * After: `export default innerExpr;`\n *\n * @internal Exported for unit testing only.\n */\nexport function unwrapExport(content: string): { content: string; unwrapped: boolean } {\n const pattern = /export\\s+default\\s+withGlasstraceConfig\\s*\\(/;\n const match = pattern.exec(content);\n if (!match) {\n return { content, unwrapped: false };\n }\n\n // Find the opening paren of withGlasstraceConfig(\n const openParenIdx = match.index + match[0].length - 1;\n const closeParenIdx = findMatchingParen(content, openParenIdx);\n if (closeParenIdx === -1) {\n return { content, unwrapped: false };\n }\n\n const innerExpr = content.slice(openParenIdx + 1, closeParenIdx).trim();\n if (innerExpr.length === 0) {\n return { content, unwrapped: false };\n }\n\n // Everything before `export default ...`\n const before = content.slice(0, match.index);\n // Everything after the closing `)` (skip optional semicolon and trailing whitespace)\n const afterClose = content.slice(closeParenIdx + 1);\n const trailing = afterClose.replace(/^;?\\s*/, \"\");\n\n const result = before + `export default ${innerExpr};\\n` + trailing;\n\n return { content: result, unwrapped: true };\n}\n\n/**\n * Removes the `withGlasstraceConfig(...)` wrapper from a CJS module.exports,\n * restoring the inner expression.\n *\n * Before: `module.exports = withGlasstraceConfig(innerExpr);`\n * After: `module.exports = innerExpr;`\n *\n * @internal Exported for unit testing only.\n */\nexport function unwrapCJSExport(content: string): { content: string; unwrapped: boolean } {\n const pattern = /module\\.exports\\s*=\\s*withGlasstraceConfig\\s*\\(/;\n const match = pattern.exec(content);\n if (!match) {\n return { content, unwrapped: false };\n }\n\n const openParenIdx = match.index + match[0].length - 1;\n const closeParenIdx = findMatchingParen(content, openParenIdx);\n if (closeParenIdx === -1) {\n return { content, unwrapped: false };\n }\n\n const innerExpr = content.slice(openParenIdx + 1, closeParenIdx).trim();\n if (innerExpr.length === 0) {\n return { content, unwrapped: false };\n }\n\n const before = content.slice(0, match.index);\n const afterClose = content.slice(closeParenIdx + 1);\n const trailing = afterClose.replace(/^;?\\s*/, \"\");\n\n const result = before + `module.exports = ${innerExpr};\\n` + trailing;\n\n return { content: result, unwrapped: true };\n}\n\n/**\n * Removes the `import { withGlasstraceConfig } from \"@glasstrace/sdk\"` line\n * from file content. If `withGlasstraceConfig` is the only imported specifier,\n * the entire import line is removed. If other specifiers exist, only\n * `withGlasstraceConfig` is removed from the specifier list.\n *\n * @internal Exported for unit testing only.\n */\nexport function removeGlasstraceConfigImport(content: string): string {\n // ESM: import { withGlasstraceConfig } from \"@glasstrace/sdk\"\n const esmSoleImport =\n /import\\s*\\{\\s*withGlasstraceConfig\\s*\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']\\s*;?\\s*\\n?/;\n if (esmSoleImport.test(content)) {\n return content.replace(esmSoleImport, \"\");\n }\n\n // ESM with multiple specifiers — remove withGlasstraceConfig from the list\n const esmMultiImport =\n /import\\s*\\{([^}]*)\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']/;\n const multiMatch = esmMultiImport.exec(content);\n if (multiMatch) {\n const specifiers = multiMatch[1]\n .split(\",\")\n .map((s) => s.trim())\n .filter((s) => s !== \"\" && s !== \"withGlasstraceConfig\");\n if (specifiers.length === 0) {\n // All specifiers were withGlasstraceConfig — remove entire import\n return content.replace(\n /import\\s*\\{[^}]*\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']\\s*;?\\s*\\n?/,\n \"\",\n );\n }\n const newImport = `import { ${specifiers.join(\", \")} } from \"@glasstrace/sdk\"`;\n return content.replace(multiMatch[0], newImport);\n }\n\n // CJS: const { withGlasstraceConfig } = require(\"@glasstrace/sdk\")\n const cjsSoleRequire =\n /const\\s*\\{\\s*withGlasstraceConfig\\s*\\}\\s*=\\s*require\\s*\\(\\s*[\"']@glasstrace\\/sdk[\"']\\s*\\)\\s*;?\\s*\\n?/;\n if (cjsSoleRequire.test(content)) {\n return content.replace(cjsSoleRequire, \"\");\n }\n\n // CJS with multiple specifiers\n const cjsMultiRequire =\n /const\\s*\\{([^}]*)\\}\\s*=\\s*require\\s*\\(\\s*[\"']@glasstrace\\/sdk[\"']\\s*\\)/;\n const cjsMultiMatch = cjsMultiRequire.exec(content);\n if (cjsMultiMatch) {\n const specifiers = cjsMultiMatch[1]\n .split(\",\")\n .map((s) => s.trim())\n .filter((s) => s !== \"\" && s !== \"withGlasstraceConfig\");\n if (specifiers.length === 0) {\n return content.replace(\n /const\\s*\\{[^}]*\\}\\s*=\\s*require\\s*\\(\\s*[\"']@glasstrace\\/sdk[\"']\\s*\\)\\s*;?\\s*\\n?/,\n \"\",\n );\n }\n const newRequire = `const { ${specifiers.join(\", \")} } = require(\"@glasstrace/sdk\")`;\n return content.replace(cjsMultiMatch[0], newRequire);\n }\n\n return content;\n}\n\n/**\n * Removes blank lines that appear consecutively (more than one empty line\n * in a row) at the top of a file, which can occur after removing import lines.\n */\nfunction cleanLeadingBlankLines(content: string): string {\n return content.replace(/^\\n{2,}/, \"\\n\");\n}\n\n/**\n * Determines whether an instrumentation.ts file was created by `glasstrace init`\n * (i.e., contains only the standard template with no user-added code).\n *\n * A file is considered init-created if:\n * - The only import from any package is `@glasstrace/sdk`\n * - The only meaningful statement in `register()` is `registerGlasstrace()`\n * - There are no other top-level statements, exports, or declarations outside\n * the register function (prevents deleting files where users added their own code)\n *\n * @internal Exported for unit testing only.\n */\nexport function isInitCreatedInstrumentation(content: string): boolean {\n const lines = content.split(\"\\n\");\n\n // Check that all imports are from @glasstrace/sdk\n const importLines = lines.filter(\n (l) => /^\\s*import\\s/.test(l) && !l.trim().startsWith(\"//\"),\n );\n const nonGlasstraceImports = importLines.filter(\n (l) => !l.includes(\"@glasstrace/sdk\"),\n );\n if (nonGlasstraceImports.length > 0) {\n return false;\n }\n\n // Check that the register() function body only contains registerGlasstrace()\n // and comments — no other meaningful statements\n const registerFnRegex = /export\\s+(?:async\\s+)?function\\s+register\\s*\\([^)]*\\)\\s*\\{/;\n const match = registerFnRegex.exec(content);\n if (!match) {\n // No register function — not a standard init template\n return false;\n }\n\n // Extract the function body\n const afterBrace = content.slice(match.index + match[0].length);\n const closingBraceIdx = findMatchingBrace(content, match.index + match[0].length - 1);\n if (closingBraceIdx === -1) {\n return false;\n }\n\n const body = afterBrace.slice(0, closingBraceIdx - (match.index + match[0].length));\n const bodyLines = body.split(\"\\n\");\n\n // Filter out comments and blank lines — only meaningful statements remain\n const statements = bodyLines.filter((l) => {\n const trimmed = l.trim();\n return trimmed !== \"\" && !trimmed.startsWith(\"//\");\n });\n\n // The only statement should be registerGlasstrace()\n if (statements.length !== 1) {\n return false;\n }\n if (!/^\\s*registerGlasstrace\\s*\\(\\s*\\)\\s*;?\\s*$/.test(statements[0])) {\n return false;\n }\n\n // Verify no other top-level code exists outside imports and the register function.\n // Extract everything that isn't an import line or inside the register() function.\n const beforeFn = content.slice(0, match.index);\n const afterFn = content.slice(closingBraceIdx + 1);\n\n const topLevelBefore = beforeFn.split(\"\\n\").filter((l) => {\n const trimmed = l.trim();\n return (\n trimmed !== \"\" &&\n !trimmed.startsWith(\"//\") &&\n !trimmed.startsWith(\"import \") &&\n !trimmed.startsWith(\"import{\")\n );\n });\n\n const topLevelAfter = afterFn.split(\"\\n\").filter((l) => {\n const trimmed = l.trim();\n return trimmed !== \"\" && !trimmed.startsWith(\"//\");\n });\n\n return topLevelBefore.length === 0 && topLevelAfter.length === 0;\n}\n\n/**\n * Finds the matching closing brace for an opening brace at the given position,\n * skipping delimiters inside string literals and comments.\n */\nfunction findMatchingBrace(text: string, openPos: number): number {\n return findMatchingDelimiter(text, openPos, \"{\", \"}\");\n}\n\n/**\n * Removes the `registerGlasstrace()` call and its `@glasstrace/sdk` import\n * from an instrumentation.ts file, preserving all other code.\n *\n * @internal Exported for unit testing only.\n */\nexport function removeRegisterGlasstrace(content: string): string {\n let result = content;\n\n // Remove all comment-block + registerGlasstrace() call pairs.\n // The init template creates a multi-line comment block before the call:\n // // Glasstrace must be registered before Prisma instrumentation\n // // to ensure all ORM spans are captured correctly.\n // // If you use @prisma/instrumentation, import it after this call.\n // registerGlasstrace();\n // Use global flag to handle multiple occurrences.\n result = result.replace(\n /[ \\t]*\\/\\/\\s*Glasstrace must be registered[^\\n]*\\n(?:[ \\t]*\\/\\/[^\\n]*\\n)*[ \\t]*registerGlasstrace\\s*\\(\\s*\\)\\s*;?\\s*\\n?/g,\n \"\",\n );\n\n // Remove any remaining standalone registerGlasstrace() calls (global)\n result = result.replace(\n /[ \\t]*registerGlasstrace\\s*\\(\\s*\\)\\s*;?\\s*\\n?/g,\n \"\",\n );\n\n // Remove the import line for registerGlasstrace from @glasstrace/sdk\n // If it's the sole import, remove the whole line\n const soleImportPattern =\n /import\\s*\\{\\s*registerGlasstrace\\s*\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']\\s*;?\\s*\\n?/;\n if (soleImportPattern.test(result)) {\n result = result.replace(soleImportPattern, \"\");\n } else {\n // Multiple specifiers — remove only registerGlasstrace\n const multiImportPattern =\n /import\\s*\\{([^}]*)\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']/;\n const multiMatch = multiImportPattern.exec(result);\n if (multiMatch) {\n const specifiers = multiMatch[1]\n .split(\",\")\n .map((s) => s.trim())\n .filter((s) => s !== \"\" && s !== \"registerGlasstrace\");\n if (specifiers.length === 0) {\n result = result.replace(\n /import\\s*\\{[^}]*\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']\\s*;?\\s*\\n?/,\n \"\",\n );\n } else {\n const newImport = `import { ${specifiers.join(\", \")} } from \"@glasstrace/sdk\"`;\n result = result.replace(multiMatch[0], newImport);\n }\n }\n }\n\n return cleanLeadingBlankLines(result);\n}\n\n/**\n * Removes content between glasstrace marker comments from a file.\n * Supports both HTML markers (`<!-- glasstrace:mcp:start/end -->`) and\n * hash markers (`# glasstrace:mcp:start/end`).\n *\n * @internal Exported for unit testing only.\n */\nexport function removeMarkerSection(content: string): { content: string; removed: boolean } {\n const lines = content.split(\"\\n\");\n let startIdx = -1;\n let endIdx = -1;\n\n for (let i = 0; i < lines.length; i++) {\n const trimmed = lines[i].trim();\n if (\n trimmed === \"<!-- glasstrace:mcp:start -->\" ||\n trimmed === \"# glasstrace:mcp:start\"\n ) {\n startIdx = i;\n } else if (\n (trimmed === \"<!-- glasstrace:mcp:end -->\" ||\n trimmed === \"# glasstrace:mcp:end\") &&\n startIdx !== -1\n ) {\n endIdx = i;\n break;\n }\n }\n\n if (startIdx === -1 || endIdx === -1) {\n return { content, removed: false };\n }\n\n const before = lines.slice(0, startIdx);\n const after = lines.slice(endIdx + 1);\n\n // Remove trailing blank line that may have preceded the marker block\n while (before.length > 0 && before[before.length - 1].trim() === \"\") {\n before.pop();\n }\n\n const result = [...before, ...after].join(\"\\n\");\n // Ensure file ends with newline if it has content\n const trimmedResult = result.trimEnd();\n return {\n content: trimmedResult.length > 0 ? trimmedResult + \"\\n\" : \"\",\n removed: true,\n };\n}\n\n/**\n * Removes the `glasstrace` key from an MCP config JSON file's `mcpServers`\n * object. Only deletes the file when `mcpServers` is the sole top-level key\n * and `glasstrace` is the only server entry. When other top-level keys exist\n * (e.g., `$schema`, metadata), the `mcpServers` key is removed (if empty)\n * and the file is preserved.\n *\n * @returns `\"removed-key\"` if the key was removed (other data remains),\n * `\"deleted\"` if the file should be deleted (no other data),\n * or `\"skipped\"` if no glasstrace config was found.\n * @internal Exported for unit testing only.\n */\nexport function processJsonMcpConfig(content: string): {\n action: \"removed-key\" | \"deleted\" | \"skipped\";\n content?: string;\n} {\n let parsed: Record<string, unknown>;\n try {\n parsed = JSON.parse(content) as Record<string, unknown>;\n } catch {\n return { action: \"skipped\" };\n }\n\n const mcpServers = parsed[\"mcpServers\"] as Record<string, unknown> | undefined;\n if (!mcpServers || typeof mcpServers !== \"object\" || !(\"glasstrace\" in mcpServers)) {\n return { action: \"skipped\" };\n }\n\n const remainingServers = Object.keys(mcpServers).filter((k) => k !== \"glasstrace\");\n const otherTopLevelKeys = Object.keys(parsed).filter((k) => k !== \"mcpServers\");\n\n if (remainingServers.length === 0 && otherTopLevelKeys.length === 0) {\n // mcpServers.glasstrace is the only data in the file — safe to delete\n return { action: \"deleted\" };\n }\n\n // Remove the glasstrace key, keep other servers\n const { glasstrace: _, ...rest } = mcpServers;\n // Suppress unused variable lint — the destructuring intentionally discards glasstrace\n void _;\n\n if (remainingServers.length > 0) {\n // Other servers remain — keep mcpServers with glasstrace removed\n parsed[\"mcpServers\"] = rest;\n } else {\n // No servers remain but other top-level keys exist — remove mcpServers entirely\n delete parsed[\"mcpServers\"];\n }\n\n return { action: \"removed-key\", content: JSON.stringify(parsed, null, 2) + \"\\n\" };\n}\n\n/**\n * Removes the `[mcp_servers.glasstrace]` section from a TOML config file.\n * Since TOML parsing without a dependency is complex, this uses a line-based\n * approach that handles the standard format written by init.\n *\n * @returns `\"removed-section\"` if the glasstrace section was removed,\n * `\"deleted\"` if the entire file should be deleted (only contained\n * glasstrace config), or `\"skipped\"` if no glasstrace config found.\n * @internal Exported for unit testing only.\n */\nexport function processTomlMcpConfig(content: string): {\n action: \"removed-section\" | \"deleted\" | \"skipped\";\n content?: string;\n} {\n if (!content.includes(\"[mcp_servers.glasstrace]\")) {\n return { action: \"skipped\" };\n }\n\n const lines = content.split(\"\\n\");\n const startIdx = lines.findIndex(\n (l) => l.trim() === \"[mcp_servers.glasstrace]\",\n );\n if (startIdx === -1) {\n return { action: \"skipped\" };\n }\n\n // Find the end of the glasstrace section: next section header or end of file\n let endIdx = lines.length;\n for (let i = startIdx + 1; i < lines.length; i++) {\n if (/^\\s*\\[/.test(lines[i])) {\n endIdx = i;\n break;\n }\n }\n\n // Remove the section and any trailing blank lines\n const before = lines.slice(0, startIdx);\n const after = lines.slice(endIdx);\n\n // Trim trailing blank lines from the before section\n while (before.length > 0 && before[before.length - 1].trim() === \"\") {\n before.pop();\n }\n\n const result = [...before, ...after].join(\"\\n\").trimEnd();\n\n // Check if there are any remaining sections\n if (result.trim().length === 0) {\n return { action: \"deleted\" };\n }\n\n return { action: \"removed-section\", content: result + \"\\n\" };\n}\n\n/**\n * Writes the `.glasstrace/shutdown-requested` marker file atomically so\n * that a running SDK heartbeat tick (or equivalent lifecycle hook) can\n * detect that uninit has been invoked and trigger shutdown (DISC-1247\n * Scenario 1).\n *\n * Uses write-temp + rename semantics so a mid-write crash cannot leave\n * a truncated marker that the running process might misread.\n *\n * Best-effort: if `.glasstrace/` does not exist or the write fails, the\n * marker is silently skipped — uninit's cleanup is not blocked by a\n * missing running process.\n *\n * @internal Exported for unit testing only.\n */\nexport function writeShutdownMarker(projectRoot: string): boolean {\n const dirPath = path.join(projectRoot, \".glasstrace\");\n if (!fs.existsSync(dirPath)) {\n // No .glasstrace/ directory means no running SDK state is tracked —\n // nothing to signal. The filesystem removal step will handle any\n // stray artifacts.\n return false;\n }\n const markerPath = path.join(dirPath, \"shutdown-requested\");\n const body = JSON.stringify({ requestedAt: new Date().toISOString() });\n try {\n // Atomic write per SDK 2.0 §4.3: tmp + fsync(tmp) + rename +\n // fsync(parent). The helper handles tmp-file cleanup on failure\n // and swallows directory-fsync errors on platforms that do not\n // support it (e.g., Windows).\n atomicWriteFileSync(markerPath, body, { encoding: \"utf-8\", mode: 0o600 });\n return true;\n } catch {\n // Marker write was best-effort to begin with; swallow errors so\n // uninit itself never fails because of a signal-side-channel\n // write.\n return false;\n }\n}\n\n/**\n * Simple TTY prompt used when `UninitOptions.prompt` is not provided.\n * Returns `defaultValue` when stdin is not a TTY.\n */\nasync function defaultPrompt(question: string, defaultValue: boolean): Promise<boolean> {\n if (!process.stdin.isTTY) return defaultValue;\n const readline = await import(\"node:readline\");\n const rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout,\n });\n return new Promise<boolean>((resolve) => {\n const suffix = defaultValue ? \" [Y/n] \" : \" [y/N] \";\n rl.question(question + suffix, (answer) => {\n rl.close();\n const trimmed = answer.trim().toLowerCase();\n if (trimmed === \"\") {\n resolve(defaultValue);\n return;\n }\n resolve(trimmed === \"y\" || trimmed === \"yes\");\n });\n });\n}\n\n/**\n * Reverses every step of `glasstrace init`, cleanly removing all SDK artifacts\n * from a project.\n *\n * Steps (in order):\n * 1. Write `.glasstrace/shutdown-requested` marker so a running SDK can\n * drain and exit cleanly (DISC-1247 Scenario 1)\n * 2. Unwrap `withGlasstraceConfig` from next.config\n * 3. Remove `registerGlasstrace` from instrumentation.ts (or delete if init-created)\n * 4. Remove `.glasstrace/` directory\n * 4a. Remove `<staticRoot>/.well-known/glasstrace.json` (and prune the\n * enclosing `.well-known/` directory when empty)\n * 5. Remove `GLASSTRACE_*` entries from `.env.local` (with dev-key confirmation)\n * 6. Remove `.glasstrace/` from `.gitignore`\n * 7. Remove MCP config entries\n * 8. Remove info sections from agent files\n *\n * @param options - Configuration for the uninit command.\n * @returns A structured result describing what actions were taken.\n */\nexport async function runUninit(options: UninitOptions): Promise<UninitResult> {\n const { projectRoot, dryRun } = options;\n const force = options.force === true;\n const prompt = options.prompt ?? defaultPrompt;\n const summary: string[] = [];\n const warnings: string[] = [];\n const errors: string[] = [];\n const prefix = dryRun ? \"[dry run] \" : \"\";\n\n // Step 0: Signal any running SDK to shut down via a marker file.\n // Placed first so the running process has maximum time to observe\n // the marker while the remaining cleanup steps execute.\n try {\n if (!dryRun) {\n const markerWritten = writeShutdownMarker(projectRoot);\n if (markerWritten) {\n summary.push(\"Wrote .glasstrace/shutdown-requested marker\");\n }\n } else {\n const dirPath = path.join(projectRoot, \".glasstrace\");\n if (fs.existsSync(dirPath)) {\n summary.push(`${prefix}Would write .glasstrace/shutdown-requested marker`);\n }\n }\n } catch (err) {\n // Marker is best-effort; failure is not an error for uninit.\n warnings.push(\n `Shutdown marker write failed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 1: Unwrap withGlasstraceConfig from next.config\n try {\n let configHandled = false;\n for (const name of NEXT_CONFIG_NAMES) {\n const configPath = path.join(projectRoot, name);\n if (!fs.existsSync(configPath)) {\n continue;\n }\n\n const content = fs.readFileSync(configPath, \"utf-8\");\n if (!content.includes(\"withGlasstraceConfig\")) {\n continue;\n }\n\n const isESM = name.endsWith(\".ts\") || name.endsWith(\".mjs\");\n const unwrapResult = isESM\n ? unwrapExport(content)\n : unwrapCJSExport(content);\n\n if (unwrapResult.unwrapped) {\n const cleaned = removeGlasstraceConfigImport(unwrapResult.content);\n const final = cleanLeadingBlankLines(cleaned);\n if (!dryRun) {\n fs.writeFileSync(configPath, final, \"utf-8\");\n }\n summary.push(`${prefix}Unwrapped withGlasstraceConfig from ${name}`);\n configHandled = true;\n break;\n } else {\n warnings.push(\n `${name} contains withGlasstraceConfig but could not be automatically unwrapped. ` +\n \"Please remove withGlasstraceConfig() manually.\",\n );\n configHandled = true;\n break;\n }\n }\n if (!configHandled) {\n // No next.config with withGlasstraceConfig found — nothing to do\n }\n } catch (err) {\n errors.push(\n `Failed to process next.config: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 2: Remove registerGlasstrace from instrumentation.ts\n try {\n const instrPath = path.join(projectRoot, \"instrumentation.ts\");\n if (fs.existsSync(instrPath)) {\n const content = fs.readFileSync(instrPath, \"utf-8\");\n if (content.includes(\"registerGlasstrace\") || content.includes(\"@glasstrace/sdk\")) {\n if (isInitCreatedInstrumentation(content)) {\n if (!dryRun) {\n fs.unlinkSync(instrPath);\n }\n summary.push(`${prefix}Deleted instrumentation.ts (init-created)`);\n } else {\n const cleaned = removeRegisterGlasstrace(content);\n if (cleaned !== content) {\n if (!dryRun) {\n fs.writeFileSync(instrPath, cleaned, \"utf-8\");\n }\n summary.push(\n `${prefix}Removed registerGlasstrace() from instrumentation.ts`,\n );\n }\n }\n }\n }\n } catch (err) {\n errors.push(\n `Failed to process instrumentation.ts: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 3: Remove .glasstrace/ directory\n try {\n const glasstraceDir = path.join(projectRoot, \".glasstrace\");\n if (fs.existsSync(glasstraceDir)) {\n if (!dryRun) {\n fs.rmSync(glasstraceDir, { recursive: true, force: true });\n }\n summary.push(`${prefix}Removed .glasstrace/ directory`);\n }\n } catch (err) {\n errors.push(\n `Failed to remove .glasstrace/: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 3a: Remove the static discovery file at\n // `<staticRoot>/.well-known/glasstrace.json` and, when empty, the\n // enclosing `.well-known/` directory. Sibling files (e.g. a user's\n // own `security.txt`) are never touched.\n try {\n if (dryRun) {\n // Dry-run preview: simulate the removal by checking existence only.\n // `removeDiscoveryFile` is a destructive helper, so the preview path\n // replicates the existence check inline rather than invoking it.\n // This keeps dry-run accurate even if the helper is changed later.\n // Mirrors the real sweep by checking BOTH candidate layouts so an\n // orphaned file in the non-inferred directory still shows up in\n // the preview (heuristic-drift scenario from the Codex re-review).\n for (const previewLayout of [\"public\", \"static\"] as const) {\n const relPath = relativeDiscoveryPath(previewLayout);\n const absPath = path.join(projectRoot, relPath);\n if (fs.existsSync(absPath)) {\n summary.push(`${prefix}Would remove ${relPath}`);\n }\n }\n } else {\n const result = removeDiscoveryFile(projectRoot);\n if (result.action === \"removed\") {\n const relPath = relativeDiscoveryPath(result.layout);\n summary.push(`Removed ${relPath}`);\n if (result.directoryRemoved) {\n const dirRel = relPath.replace(/\\/glasstrace\\.json$/, \"/\");\n summary.push(`Removed empty ${dirRel}`);\n }\n } else if (result.action === \"failed\") {\n warnings.push(\n `Failed to remove ${relativeDiscoveryPath(result.layout)}${\n result.error !== undefined ? `: ${result.error}` : \"\"\n }`,\n );\n }\n }\n } catch (err) {\n warnings.push(\n `Failed to remove discovery file: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 4: Remove GLASSTRACE entries from .env.local\n // DISC-1247 Scenario 6: if the file contains a claimed developer key\n // (`gt_dev_*`), require explicit confirmation before removing it so\n // users don't silently lose authentication state during uninit.\n // `--force` bypasses the prompt.\n try {\n const envPath = path.join(projectRoot, \".env.local\");\n if (fs.existsSync(envPath)) {\n const content = fs.readFileSync(envPath, \"utf-8\");\n const existingKey = readEnvLocalApiKey(content);\n const hasDevKey = isDevApiKey(existingKey);\n\n // Track how the dev-key path is resolved so the summary reflects\n // what actually happened: prompt-confirmed, force-bypassed, or\n // preview-only. Using the literal \"(dev key confirmed)\" for all\n // three paths was misleading (Copilot review).\n let proceed = true;\n let devKeyPath: \"interactive-confirmed\" | \"force-bypass\" | \"dry-run-preview\" | \"none\" = \"none\";\n if (hasDevKey) {\n if (dryRun) {\n devKeyPath = \"dry-run-preview\";\n } else if (force) {\n devKeyPath = \"force-bypass\";\n } else {\n const confirmed = await prompt(\n \".env.local contains a claimed Glasstrace developer API key (gt_dev_...). \" +\n \"Removing it will require you to re-authenticate. Continue?\",\n false,\n );\n proceed = confirmed;\n if (confirmed) devKeyPath = \"interactive-confirmed\";\n }\n }\n\n if (!proceed) {\n warnings.push(\n \"Preserved GLASSTRACE_API_KEY in .env.local (claimed dev key; re-run with --force to remove)\",\n );\n } else {\n const lines = content.split(\"\\n\");\n const filtered = lines.filter((line) => {\n const trimmed = line.trim();\n // Match both commented and uncommented GLASSTRACE_ lines\n return !(\n /^\\s*#?\\s*GLASSTRACE_API_KEY\\s*=/.test(trimmed) ||\n /^\\s*#?\\s*GLASSTRACE_COVERAGE_MAP\\s*=/.test(trimmed)\n );\n });\n\n if (filtered.length !== lines.length) {\n const result = filtered.join(\"\\n\");\n // If the file is now empty (only newlines), don't write it\n if (result.trim().length === 0) {\n if (!dryRun) {\n fs.unlinkSync(envPath);\n }\n summary.push(`${prefix}Deleted .env.local (no remaining entries)`);\n } else {\n if (!dryRun) {\n fs.writeFileSync(envPath, result, \"utf-8\");\n }\n let devKeyAnnotation = \"\";\n if (devKeyPath === \"interactive-confirmed\") {\n devKeyAnnotation = \" (dev key confirmed)\";\n } else if (devKeyPath === \"force-bypass\") {\n devKeyAnnotation = \" (dev key removed via --force)\";\n } else if (devKeyPath === \"dry-run-preview\") {\n devKeyAnnotation =\n \" (dev key would be removed; real run would require confirmation)\";\n }\n summary.push(\n `${prefix}Removed GLASSTRACE entries from .env.local${devKeyAnnotation}`,\n );\n }\n }\n }\n }\n } catch (err) {\n errors.push(\n `Failed to process .env.local: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 5: Remove .glasstrace/ from .gitignore\n try {\n const gitignorePath = path.join(projectRoot, \".gitignore\");\n if (fs.existsSync(gitignorePath)) {\n const content = fs.readFileSync(gitignorePath, \"utf-8\");\n const lines = content.split(\"\\n\");\n\n // Remove lines that are exactly \".glasstrace/\" or MCP config file entries\n // added by init (e.g., \".mcp.json\", \".cursor/mcp.json\", \".gemini/settings.json\",\n // \".codex/config.toml\")\n const mcpGitignoreEntries = new Set([\n \".glasstrace/\",\n \".mcp.json\",\n \".cursor/mcp.json\",\n \".gemini/settings.json\",\n \".codex/config.toml\",\n ]);\n\n const filtered = lines.filter(\n (line) => !mcpGitignoreEntries.has(line.trim()),\n );\n\n if (filtered.length !== lines.length) {\n const result = filtered.join(\"\\n\");\n if (result.trim().length === 0) {\n if (!dryRun) {\n fs.unlinkSync(gitignorePath);\n }\n summary.push(`${prefix}Deleted .gitignore (no remaining entries)`);\n } else {\n if (!dryRun) {\n fs.writeFileSync(gitignorePath, result, \"utf-8\");\n }\n summary.push(`${prefix}Removed Glasstrace entries from .gitignore`);\n }\n }\n }\n } catch (err) {\n errors.push(\n `Failed to process .gitignore: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 6: Remove MCP config entries\n try {\n for (const configFile of MCP_CONFIG_FILES) {\n const configPath = path.join(projectRoot, configFile);\n if (!fs.existsSync(configPath)) {\n continue;\n }\n\n const content = fs.readFileSync(configPath, \"utf-8\");\n const result = processJsonMcpConfig(content);\n\n if (result.action === \"deleted\") {\n if (!dryRun) {\n fs.unlinkSync(configPath);\n }\n summary.push(`${prefix}Deleted ${configFile}`);\n } else if (result.action === \"removed-key\" && result.content !== undefined) {\n if (!dryRun) {\n fs.writeFileSync(configPath, result.content, \"utf-8\");\n }\n summary.push(`${prefix}Removed glasstrace from ${configFile}`);\n }\n }\n // Handle Codex TOML config separately\n const codexConfigPath = path.join(projectRoot, \".codex\", \"config.toml\");\n if (fs.existsSync(codexConfigPath)) {\n const content = fs.readFileSync(codexConfigPath, \"utf-8\");\n const tomlResult = processTomlMcpConfig(content);\n\n if (tomlResult.action === \"deleted\") {\n if (!dryRun) {\n fs.unlinkSync(codexConfigPath);\n }\n summary.push(`${prefix}Deleted .codex/config.toml`);\n } else if (tomlResult.action === \"removed-section\" && tomlResult.content !== undefined) {\n if (!dryRun) {\n fs.writeFileSync(codexConfigPath, tomlResult.content, \"utf-8\");\n }\n summary.push(`${prefix}Removed glasstrace from .codex/config.toml`);\n }\n }\n\n // Handle Windsurf global config at ~/.codeium/windsurf/mcp_config.json\n // Only process if the project has Windsurf markers, to avoid touching\n // global config for non-Windsurf projects\n const hasWindsurfMarkers =\n fs.existsSync(path.join(projectRoot, \".windsurfrules\")) ||\n fs.existsSync(path.join(projectRoot, \".windsurf\"));\n if (hasWindsurfMarkers) {\n const windsurfConfigPath = path.join(\n os.homedir(),\n \".codeium\",\n \"windsurf\",\n \"mcp_config.json\",\n );\n if (fs.existsSync(windsurfConfigPath)) {\n const content = fs.readFileSync(windsurfConfigPath, \"utf-8\");\n const windsurfResult = processJsonMcpConfig(content);\n\n // Display the path with ~ for the home directory to keep output\n // readable, but derive it from the actual path for accuracy.\n const home = os.homedir();\n const displayPath = windsurfConfigPath.startsWith(home)\n ? \"~\" + windsurfConfigPath.slice(home.length)\n : windsurfConfigPath;\n\n if (windsurfResult.action === \"deleted\") {\n if (!dryRun) {\n fs.unlinkSync(windsurfConfigPath);\n }\n summary.push(\n `${prefix}Deleted global Windsurf config (${displayPath})`,\n );\n } else if (\n windsurfResult.action === \"removed-key\" &&\n windsurfResult.content !== undefined\n ) {\n if (!dryRun) {\n fs.writeFileSync(windsurfConfigPath, windsurfResult.content, \"utf-8\");\n }\n summary.push(\n `${prefix}Removed glasstrace from global Windsurf config (${displayPath})`,\n );\n }\n }\n }\n } catch (err) {\n errors.push(\n `Failed to process MCP config: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 7: Remove info sections from agent files\n try {\n for (const infoFile of AGENT_INFO_FILES) {\n const filePath = path.join(projectRoot, infoFile);\n if (!fs.existsSync(filePath)) {\n continue;\n }\n\n const content = fs.readFileSync(filePath, \"utf-8\");\n const result = removeMarkerSection(content);\n\n if (result.removed) {\n if (result.content.trim().length === 0) {\n // File is now empty after removing the marker section —\n // only delete if the file was solely glasstrace content\n if (!dryRun) {\n fs.unlinkSync(filePath);\n }\n summary.push(`${prefix}Deleted ${infoFile} (only contained Glasstrace section)`);\n } else {\n if (!dryRun) {\n fs.writeFileSync(filePath, result.content, \"utf-8\");\n }\n summary.push(`${prefix}Removed Glasstrace section from ${infoFile}`);\n }\n }\n }\n } catch (err) {\n errors.push(\n `Failed to process agent info files: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n if (summary.length === 0 && errors.length === 0) {\n summary.push(\"No Glasstrace artifacts found — nothing to do.\");\n }\n\n return { exitCode: errors.length > 0 ? 1 : 0, summary, warnings, errors };\n}\n","import type { DetectedAgent } from \"../agent-detection/detect.js\";\n\n/** Next.js config file names in priority order. */\nexport const NEXT_CONFIG_NAMES = [\"next.config.ts\", \"next.config.js\", \"next.config.mjs\"] as const;\n\n/** Maps internal agent name to a human-readable display name. */\nexport function formatAgentName(name: DetectedAgent[\"name\"]): string {\n const displayNames: Record<DetectedAgent[\"name\"], string> = {\n claude: \"Claude Code\",\n codex: \"Codex\",\n gemini: \"Gemini\",\n cursor: \"Cursor\",\n windsurf: \"Windsurf\",\n generic: \"Generic helper\",\n };\n return displayNames[name];\n}\n","/**\n * Atomic file-write helper.\n *\n * Implements the durability half of the atomic-write protocol\n * (`docs/component-designs/sdk-architecture.md` §4.3 — Atomic file\n * writes; durability protocol steps 6–9):\n *\n * 1. Write the payload to a sibling temp file in the **same**\n * directory as the final target. The shared directory guarantees\n * `rename(2)` stays on the same filesystem and therefore atomic\n * per POSIX semantics.\n * 2. **fsync the temp file.** Forces data and metadata to durable\n * storage before the rename is observable.\n * 3. **rename atomically into place.** Readers see either the old\n * file contents or the new ones, never a partial write.\n * 4. **fsync the parent directory.** On POSIX, `rename(2)` durability\n * is not guaranteed until the containing directory's own metadata\n * is synced. Without this step, a power loss between rename and\n * parent-dir sync can leave the rename invisible after reboot\n * (the kernel acknowledges the syscall but the metadata never\n * reached durable storage).\n *\n * Closes the durability gap that allowed DISC-494 (anon-key unlinked\n * silently on re-init under crash interleavings).\n *\n * Out-of-scope by design:\n * - The `lstat → tmp → rename → re-lstat` TOCTOU re-check (spec\n * §4.3 steps 1–2 and 7's re-verification) is next-major scope per\n * `sdk-architecture.md` §4.3 — TOCTOU protection.\n * - The `GLASSTRACE_TEST_CRASH_AFTER` crash-injection harness is\n * next-major scope per `sdk-architecture.md` §4.3 — Crash-injection\n * harness.\n * - Structured error-with-step-number reporting is next-major scope\n * per `sdk-architecture.md` §4.3 — durability protocol step 9.\n *\n * Cross-platform behavior:\n * - On POSIX (Linux, macOS), the parent-directory fsync uses an\n * `open(O_RDONLY) → fsync → close` sequence. This is the canonical\n * way to flush directory metadata.\n * - On Windows, opening a directory for read returns `EISDIR` (and\n * `fsync` on the resulting handle would fail with `EINVAL` even\n * if the open succeeded). NTFS's rename semantics also do not\n * require an explicit directory fsync to commit the rename\n * metadata. The helper therefore swallows `EISDIR`, `EINVAL`,\n * `EPERM`, and `ENOTSUP` from the parent-dir fsync step. Any\n * other error from the open/fsync/close sequence still propagates\n * so genuine I/O failures are not silently ignored.\n *\n * Concurrency:\n * - Two processes writing the same target concurrently follow\n * last-rename-wins semantics. The helper does not lock; the\n * caller is responsible for any external mutual-exclusion. This\n * matches the existing 0.19.x behavior of every migrated call\n * site — the helper does not change concurrency guarantees, only\n * durability.\n *\n * Performance:\n * - `fsync` is intrinsically expensive on rotational media (one\n * full disk-cache flush). The sync variant is exposed for the\n * `runtime-state.ts` writer, which runs in a signal handler with\n * a strict time budget; existing callers were already issuing a\n * blocking `writeFileSync + renameSync`, so the additional cost\n * is the two `fsync` calls. On modern SSDs this remains in the\n * low-millisecond range.\n *\n * Module-load safety: `node:fs` and `node:fs/promises` are loaded\n * lazily so the module can be imported in non-Node environments\n * (Edge Runtime, browser bundles) without crashing at import time.\n * Calling any helper export in such an environment throws a clear\n * error; callers that may run on the edge must therefore probe\n * their own Node-availability before reaching this module (see\n * `init-client.ts`'s `loadFsPathAsync`).\n *\n * @internal Not re-exported from `index.ts`/`node-entry.ts`/\n * `edge-entry.ts`. Importable only from sibling SDK modules.\n */\n\nimport type { FileHandle } from \"node:fs/promises\";\n\n/**\n * Resolves the parent directory of a path without importing `node:path`,\n * so this module remains importable in non-Node environments where\n * `node:path` is unavailable. Handles both POSIX (`/`) and Windows\n * (`\\\\`) separators because Windows paths can use either form.\n *\n * Behavior matches `path.dirname` for the inputs this module receives\n * (always absolute paths produced by SDK callers): finds the last\n * separator and returns the prefix; returns `\".\"` if no separator is\n * present (a relative leaf name); preserves the root for `/foo` →\n * `/`. Edge cases like trailing-separator inputs are not exercised by\n * SDK callers so are not modeled here.\n */\nfunction parentDir(filePath: string): string {\n const lastSlash = filePath.lastIndexOf(\"/\");\n const lastBackslash = filePath.lastIndexOf(\"\\\\\");\n const lastSep = Math.max(lastSlash, lastBackslash);\n if (lastSep < 0) return \".\";\n if (lastSep === 0) return filePath.slice(0, 1); // root: \"/x\" → \"/\"\n return filePath.slice(0, lastSep);\n}\n\n/**\n * Options accepted by both `atomicWriteFile` and `atomicWriteFileSync`.\n *\n * The shape mirrors the relevant subset of `fs.writeFile`'s options\n * object. `mode` defaults to `0o600` (state files); callers writing\n * static or discoverable files (e.g., `.well-known/glasstrace.json`)\n * may pass `0o644`. `encoding` defaults to `\"utf-8\"` when the payload\n * is a string and is ignored when the payload is a `Uint8Array`.\n */\nexport interface AtomicWriteOptions {\n /**\n * POSIX file mode applied to the temp file before the rename.\n * Defaults to `0o600`. The mode applies to the temp file and is\n * carried through the rename; callers that need a different\n * post-rename mode should call `chmod` themselves after this\n * helper resolves.\n *\n * The helper re-applies this mode unconditionally via `chmod`/`chmodSync`\n * after writing, so a pre-existing temp file (e.g., residue from a\n * crashed prior run) cannot carry stale permissive bits into the\n * caller's renamed target. The fsync handle is opened read-only so\n * callers passing a read-only mode (e.g. `0o444`) are still supported.\n */\n mode?: number;\n /**\n * Encoding for string payloads. Defaults to `\"utf-8\"`. Ignored when\n * the payload is a `Uint8Array`.\n */\n encoding?: BufferEncoding;\n}\n\n/** Errno codes that the parent-dir fsync step is permitted to swallow. */\nconst PARENT_FSYNC_SWALLOWED_CODES: ReadonlySet<string> = new Set([\n \"EISDIR\",\n \"EINVAL\",\n \"EPERM\",\n \"ENOTSUP\",\n]);\n\n/**\n * Reads the `code` property off an `unknown` thrown value if present.\n * Helper avoids `as` casts on `err` and works with both plain objects\n * and `NodeJS.ErrnoException` instances.\n */\nfunction errnoCodeOf(err: unknown): string | undefined {\n if (err === null || typeof err !== \"object\") return undefined;\n const code = (err as { code?: unknown }).code;\n return typeof code === \"string\" ? code : undefined;\n}\n\n/**\n * Builds the path of the sibling temp file. The temp lives in the\n * same directory as the target so the eventual `rename(2)` stays on\n * the same filesystem.\n *\n * Callers may pre-compute their own temp paths (e.g., `<path>.tmp-\n * <pid>` for the discovery-file write to keep multi-process collisions\n * disambiguated). When they do, they call the helper's `*WithTmp`\n * variants. The default temp suffix is `.tmp` for parity with the\n * existing 0.19.x call sites.\n */\nfunction defaultTmpPath(targetPath: string): string {\n return `${targetPath}.tmp`;\n}\n\n// ---------------------------------------------------------------------------\n// Lazy module loaders\n// ---------------------------------------------------------------------------\n\nlet fsPromisesCache: typeof import(\"node:fs/promises\") | null | undefined;\nlet fsSyncCache: typeof import(\"node:fs\") | null | undefined;\n\nasync function loadFsPromises(): Promise<typeof import(\"node:fs/promises\")> {\n if (fsPromisesCache !== undefined) {\n if (fsPromisesCache === null) {\n throw new Error(\n \"node:fs/promises is unavailable in this environment; atomicWriteFile cannot be used here.\",\n );\n }\n return fsPromisesCache;\n }\n try {\n fsPromisesCache = await import(\"node:fs/promises\");\n return fsPromisesCache;\n } catch {\n fsPromisesCache = null;\n throw new Error(\n \"node:fs/promises is unavailable in this environment; atomicWriteFile cannot be used here.\",\n );\n }\n}\n\nfunction loadFsSync(): typeof import(\"node:fs\") {\n if (fsSyncCache !== undefined) {\n if (fsSyncCache === null) {\n throw new Error(\n \"node:fs is unavailable in this environment; atomicWriteFileSync cannot be used here.\",\n );\n }\n return fsSyncCache;\n }\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports, glasstrace/no-unguarded-node-require -- guarded by the surrounding try/catch which caches `null` and surfaces a clean Error on subsequent calls; consumers gate with `isSyncFsAvailable()` (DISC-1555).\n fsSyncCache = require(\"node:fs\") as typeof import(\"node:fs\");\n return fsSyncCache;\n } catch {\n fsSyncCache = null;\n throw new Error(\n \"node:fs is unavailable in this environment; atomicWriteFileSync cannot be used here.\",\n );\n }\n}\n\n// ---------------------------------------------------------------------------\n// Async variant\n// ---------------------------------------------------------------------------\n\n/**\n * Atomically writes `payload` to `targetPath` using\n * `tmp + fsync(tmp) + rename + fsync(parent)` semantics.\n *\n * On any error from the write/fsync/rename steps, the helper makes a\n * best-effort attempt to remove the temp file and rethrows the\n * original error. The parent-dir fsync step swallows\n * `EISDIR`/`EINVAL`/`EPERM`/`ENOTSUP` to support platforms where\n * directory fsync is not supported (notably Windows on NTFS).\n *\n * @param targetPath Absolute path to the final destination.\n * @param payload `string` or `Uint8Array` payload.\n * @param options See {@link AtomicWriteOptions}.\n */\nexport async function atomicWriteFile(\n targetPath: string,\n payload: string | Uint8Array,\n options: AtomicWriteOptions = {},\n): Promise<void> {\n return atomicWriteFileWithTmp(targetPath, defaultTmpPath(targetPath), payload, options);\n}\n\n/**\n * Async variant accepting an explicit `tmpPath`. The temp path MUST\n * live in the same directory as `targetPath` to preserve rename\n * atomicity. Used by `cli/discovery-file.ts` to disambiguate\n * concurrent writers via a `.tmp-<pid>` suffix.\n */\nexport async function atomicWriteFileWithTmp(\n targetPath: string,\n tmpPath: string,\n payload: string | Uint8Array,\n options: AtomicWriteOptions = {},\n): Promise<void> {\n const mode = options.mode ?? 0o600;\n const encoding = options.encoding ?? \"utf-8\";\n const fsp = await loadFsPromises();\n\n let handle: FileHandle | null = null;\n try {\n // Step 1: write payload to the temp file.\n if (typeof payload === \"string\") {\n await fsp.writeFile(tmpPath, payload, { encoding, mode });\n } else {\n await fsp.writeFile(tmpPath, payload, { mode });\n }\n\n // Step 1a: re-apply the requested mode unconditionally. `writeFile`\n // only honors `mode` when it CREATES the file; if `tmpPath` is a\n // pre-existing residue from a prior crash (or a hostile actor) the\n // existing permissions are preserved, which would silently rename a\n // world-readable temp into place. Path-based `chmod` lets the fsync\n // handle below remain read-only, so callers that pass a read-only\n // mode (e.g. 0o444) are still supported.\n await fsp.chmod(tmpPath, mode);\n\n // Step 2: fsync the temp file. Open then fsync via the\n // `FileHandle.sync()` method — `writeFile` closes its internal\n // handle immediately, so we re-open here. Read-only is sufficient\n // for `fsync` and works for callers that supply a read-only `mode`.\n handle = await fsp.open(tmpPath, \"r\");\n await handle.sync();\n await handle.close();\n handle = null;\n\n // Step 3: rename into place. POSIX-atomic on same-filesystem.\n await fsp.rename(tmpPath, targetPath);\n } catch (err) {\n if (handle !== null) {\n try {\n await handle.close();\n } catch {\n // Best-effort: the original error takes precedence.\n }\n }\n await removeTmpResidueAsync(fsp, tmpPath);\n throw err;\n }\n\n // Step 4: fsync the parent directory. Failures on platforms that\n // do not support directory fsync are swallowed; genuine I/O errors\n // still propagate.\n await fsyncParentDirAsync(targetPath, fsp);\n}\n\n/**\n * Best-effort removal of the temp file after a failed atomic-write\n * step. Tries `unlink` first (the common case where the temp is a\n * regular file). If `unlink` fails with `EISDIR`/`EPERM` — meaning the\n * temp path resolves to a directory left behind by a prior crash or\n * misconfiguration — falls back to a non-recursive `rmdir`. Any error\n * from either operation is swallowed so the caller can rethrow the\n * original I/O failure.\n */\nasync function removeTmpResidueAsync(\n fsp: typeof import(\"node:fs/promises\"),\n tmpPath: string,\n): Promise<void> {\n try {\n await fsp.unlink(tmpPath);\n return;\n } catch (err) {\n const code = errnoCodeOf(err);\n if (code !== \"EISDIR\" && code !== \"EPERM\") {\n // Tmp may not exist (ENOENT), or unlink may have failed for an\n // unrelated reason. Either way, nothing more to do — the\n // original error takes precedence in the caller.\n return;\n }\n }\n try {\n await fsp.rmdir(tmpPath);\n } catch {\n // Directory may be non-empty or otherwise unremovable; the original\n // I/O failure remains the actionable error for the caller.\n }\n}\n\nasync function fsyncParentDirAsync(\n targetPath: string,\n fsp: typeof import(\"node:fs/promises\"),\n): Promise<void> {\n const parent = parentDir(targetPath);\n let handle: FileHandle | null = null;\n try {\n handle = await fsp.open(parent, \"r\");\n await handle.sync();\n } catch (err) {\n const code = errnoCodeOf(err);\n if (code !== undefined && PARENT_FSYNC_SWALLOWED_CODES.has(code)) {\n // Platform does not support directory fsync (Windows / NTFS).\n // The rename has already returned successfully; durability\n // semantics on those filesystems do not require an explicit\n // directory sync.\n return;\n }\n throw err;\n } finally {\n if (handle !== null) {\n try {\n await handle.close();\n } catch {\n // Close errors after a successful fsync are not actionable.\n }\n }\n }\n}\n\n// ---------------------------------------------------------------------------\n// Sync variant\n// ---------------------------------------------------------------------------\n\n/**\n * Synchronous counterpart to {@link atomicWriteFile}. Exists for\n * `runtime-state.ts`, which writes from a signal handler that\n * cannot await. Otherwise prefer the async variant.\n */\nexport function atomicWriteFileSync(\n targetPath: string,\n payload: string | Uint8Array,\n options: AtomicWriteOptions = {},\n): void {\n atomicWriteFileSyncWithTmp(targetPath, defaultTmpPath(targetPath), payload, options);\n}\n\n/**\n * Sync variant accepting an explicit `tmpPath`. Mirrors\n * {@link atomicWriteFileWithTmp}.\n */\nexport function atomicWriteFileSyncWithTmp(\n targetPath: string,\n tmpPath: string,\n payload: string | Uint8Array,\n options: AtomicWriteOptions = {},\n): void {\n const mode = options.mode ?? 0o600;\n const encoding = options.encoding ?? \"utf-8\";\n const fs = loadFsSync();\n\n let fd: number | null = null;\n try {\n if (typeof payload === \"string\") {\n fs.writeFileSync(tmpPath, payload, { encoding, mode });\n } else {\n fs.writeFileSync(tmpPath, payload, { mode });\n }\n\n // Re-apply the requested mode unconditionally — see the matching\n // comment in `atomicWriteFileWithTmp` for the credential-leak\n // rationale when `tmpPath` is pre-existing residue.\n fs.chmodSync(tmpPath, mode);\n\n // Read-only is sufficient for `fsync` and works for callers that\n // supply a read-only `mode`.\n fd = fs.openSync(tmpPath, \"r\");\n fs.fsyncSync(fd);\n fs.closeSync(fd);\n fd = null;\n\n fs.renameSync(tmpPath, targetPath);\n } catch (err) {\n if (fd !== null) {\n try {\n fs.closeSync(fd);\n } catch {\n // Best-effort.\n }\n }\n removeTmpResidueSync(fs, tmpPath);\n throw err;\n }\n\n fsyncParentDirSyncWithFs(targetPath, fs);\n}\n\n/**\n * Synchronous counterpart to {@link removeTmpResidueAsync}. See the\n * async variant's JSDoc for the `EISDIR`/`EPERM` rationale.\n */\nfunction removeTmpResidueSync(\n fs: typeof import(\"node:fs\"),\n tmpPath: string,\n): void {\n try {\n fs.unlinkSync(tmpPath);\n return;\n } catch (err) {\n const code = errnoCodeOf(err);\n if (code !== \"EISDIR\" && code !== \"EPERM\") {\n return;\n }\n }\n try {\n fs.rmdirSync(tmpPath);\n } catch {\n // Directory may be non-empty; original error takes precedence.\n }\n}\n\n/**\n * Synchronously fsyncs the parent directory of `targetPath`. Errors\n * matching {@link PARENT_FSYNC_SWALLOWED_CODES} (Windows / NTFS does\n * not support directory fsync) are silently ignored; other errors\n * propagate. Exposed for callers like `cli/discovery-file.ts` that\n * need to compose the steps manually around a backup-rollback flow.\n *\n * @internal Sibling-module use only.\n */\nexport function fsyncParentDirSync(targetPath: string): void {\n fsyncParentDirSyncWithFs(targetPath, loadFsSync());\n}\n\nfunction fsyncParentDirSyncWithFs(\n targetPath: string,\n fs: typeof import(\"node:fs\"),\n): void {\n const parent = parentDir(targetPath);\n let fd: number | null = null;\n try {\n fd = fs.openSync(parent, \"r\");\n fs.fsyncSync(fd);\n } catch (err) {\n const code = errnoCodeOf(err);\n if (code !== undefined && PARENT_FSYNC_SWALLOWED_CODES.has(code)) {\n return;\n }\n throw err;\n } finally {\n if (fd !== null) {\n try {\n fs.closeSync(fd);\n } catch {\n // Close errors after a successful fsync are not actionable.\n }\n }\n }\n}\n\n/**\n * Synchronously writes `payload` to `tmpPath` and fsyncs the\n * resulting file so its data and metadata are durable on disk\n * before any rename is observable. Throws on any I/O error from\n * either step; on throw, the partially-written tmp file is left\n * in place (callers handle cleanup so they can decide between\n * `unlink` and a backup-rollback strategy).\n *\n * Steps 1 and 2 of the SDK 2.0 §4.3 protocol. Pair with a `rename`\n * (step 3) and {@link fsyncParentDirSync} (step 4) — or use\n * {@link atomicWriteFileSync}/{@link atomicWriteFileSyncWithTmp}\n * which compose all four steps internally.\n *\n * @internal Sibling-module use only.\n */\nexport function writeAndFsyncTempSync(\n tmpPath: string,\n payload: string | Uint8Array,\n options: AtomicWriteOptions = {},\n): void {\n const mode = options.mode ?? 0o600;\n const encoding = options.encoding ?? \"utf-8\";\n const fs = loadFsSync();\n if (typeof payload === \"string\") {\n fs.writeFileSync(tmpPath, payload, { encoding, mode });\n } else {\n fs.writeFileSync(tmpPath, payload, { mode });\n }\n // Re-apply the requested mode in case `tmpPath` already existed —\n // `writeFileSync` only honors `mode` when creating the file, so a\n // stale residue could otherwise carry permissive bits into the\n // caller's eventual rename. Path-based `chmodSync` keeps the fsync\n // handle below read-only.\n fs.chmodSync(tmpPath, mode);\n // Read-only is sufficient for `fsync` and works for callers that\n // pass a read-only `mode`.\n const fd = fs.openSync(tmpPath, \"r\");\n try {\n fs.fsyncSync(fd);\n } finally {\n try {\n fs.closeSync(fd);\n } catch {\n // Close errors after fsync are not actionable.\n }\n }\n}\n\n/**\n * Returns `true` when synchronous `node:fs` is reachable in the current\n * runtime, `false` otherwise. Callers that prefer a silent-skip path\n * over a thrown `node:fs is unavailable` error use this probe before\n * dispatching to {@link atomicWriteFileSync}.\n *\n * The probe shares the lazy-loader cache with the sync helpers, so a\n * `true` result also primes the cache for the subsequent write. This\n * keeps the probe overhead to one `require(\"node:fs\")` per process.\n *\n * Background: tsup's bundled `__require` shim throws \"Dynamic require\n * of \\\"node:fs\\\" is not supported\" when the SDK is loaded as an ESM\n * module from a host like Next.js (DISC-1555). The runtime is a real\n * Node process — it just lacks a working synchronous `require()`\n * binding in the ESM scope. Async helpers are unaffected because\n * `await import(\"node:fs/promises\")` is ESM-native.\n *\n * @internal Sibling-module use only.\n */\nexport function isSyncFsAvailable(): boolean {\n try {\n loadFsSync();\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Test-only: clear the cached lazy-loaded modules. Allows test suites\n * that mock `node:fs`/`node:fs/promises` to ensure the helper re-runs\n * its module probe.\n *\n * @internal Tests only.\n */\nexport function _resetModuleCacheForTesting(): void {\n fsPromisesCache = undefined;\n fsSyncCache = undefined;\n}\n","import { createHash } from \"node:crypto\";\nimport {\n AnonApiKeySchema,\n DevApiKeySchema,\n type AnonApiKey,\n type DevApiKey,\n} from \"@glasstrace/protocol\";\nimport { readAnonKey, readClaimedKey } from \"./anon-key.js\";\nimport { atomicWriteFile } from \"./atomic-write.js\";\n\n/**\n * Glasstrace MCP endpoint embedded in managed MCP configs and used by\n * the runtime claim-refresh path. Lives here (not in `cli/constants.ts`)\n * so the runtime helper can reach it without crossing the runtime/CLI\n * boundary; CLI callers import it directly from this module.\n */\nexport const MCP_ENDPOINT = \"https://api.glasstrace.dev/mcp\";\n\n/**\n * Runtime-safe MCP credential and config utilities.\n *\n * This module is loaded into user processes at SDK boot. It must not\n * import from `cli/*` or `agent-detection/*` so the runtime bundle does\n * not pull in CLI scaffolding or filesystem scanners. The boundary is\n * enforced by an import-graph guard test.\n *\n * Internal: not re-exported via `node-entry.ts` or `index.ts`.\n *\n * @module\n */\n\nlet fsPathCache:\n | { fs: typeof import(\"node:fs/promises\"); path: typeof import(\"node:path\") }\n | null\n | undefined;\n\nasync function loadFsPath(): Promise<\n | { fs: typeof import(\"node:fs/promises\"); path: typeof import(\"node:path\") }\n | null\n> {\n if (fsPathCache !== undefined) return fsPathCache;\n try {\n const [fs, path] = await Promise.all([\n import(\"node:fs/promises\"),\n import(\"node:path\"),\n ]);\n fsPathCache = { fs, path };\n return fsPathCache;\n } catch {\n fsPathCache = null;\n return null;\n }\n}\n\n/**\n * Computes a stable identity fingerprint for deduplication purposes.\n * This is NOT password hashing — the input is an opaque token used as\n * a marker identity, not a credential stored for authentication.\n *\n * @internal Exported for unit testing and for `cli/scaffolder.ts`'s\n * marker writer.\n */\nexport function identityFingerprint(token: string): string {\n return `sha256:${createHash(\"sha256\").update(token).digest(\"hex\")}`;\n}\n\n/**\n * Compares two MCP config strings for canonical-JSON equality. Returns\n * `true` when both inputs parse as JSON and produce structurally equal\n * objects after recursive key sorting; falls back to trimmed text\n * comparison for TOML and other non-JSON formats. Returns `false` on\n * parse errors that don't fall through to text comparison.\n *\n * Used to detect manually-edited MCP configs before overwriting them\n * (DISC-1247 Scenario 2c) and as the staleness signal for SDK-managed\n * configs that must be refreshed when the project's effective\n * credential changes.\n *\n * @internal Exported for unit testing only.\n */\nexport function mcpConfigMatches(\n existingContent: string,\n expectedContent: string,\n): boolean {\n const trimmedExpected = expectedContent.trim();\n\n try {\n const existingParsed: unknown = JSON.parse(existingContent);\n const expectedParsed: unknown = JSON.parse(trimmedExpected);\n return (\n JSON.stringify(canonicalize(existingParsed)) ===\n JSON.stringify(canonicalize(expectedParsed))\n );\n } catch {\n // Fall through to text comparison for TOML and other non-JSON formats.\n }\n\n return existingContent.trim() === trimmedExpected;\n}\n\nfunction canonicalize(value: unknown): unknown {\n if (Array.isArray(value)) {\n return value.map(canonicalize);\n }\n if (value !== null && typeof value === \"object\") {\n const obj = value as Record<string, unknown>;\n const sorted: Record<string, unknown> = {};\n for (const key of Object.keys(obj).sort()) {\n sorted[key] = canonicalize(obj[key]);\n }\n return sorted;\n }\n return value;\n}\n\n/**\n * Parses a `.env.local` file's text content for `GLASSTRACE_API_KEY`,\n * returning the last assignment's value. Empty values\n * (`GLASSTRACE_API_KEY=`) and the `your_key_here` placeholder are\n * filtered out. Surrounding single or double quotes are stripped.\n *\n * The resolver validates the returned value against `DevApiKeySchema`\n * before accepting it; this parser is permissive on purpose so that\n * malformed values can be flagged with a `malformed-env-local`\n * warning rather than silently dropped.\n *\n * @internal Exported for unit testing only.\n */\nexport function readEnvLocalApiKey(content: string): string | null {\n let last: string | null = null;\n const regex = /^\\s*GLASSTRACE_API_KEY\\s*=\\s*(.*)$/gm;\n let match: RegExpExecArray | null;\n while ((match = regex.exec(content)) !== null) {\n const raw = match[1].trim();\n if (raw === \"\") continue;\n const unquoted = raw.replace(/^(['\"])(.*)\\1$/, \"$2\");\n if (unquoted === \"\" || unquoted === \"your_key_here\") continue;\n last = unquoted;\n }\n return last;\n}\n\n/**\n * Returns true when the given API key value looks like a claimed\n * developer key (prefix `gt_dev_`). Defensive against leading or\n * trailing whitespace.\n *\n * **This is a prefix-only check, not strict validation.** Use it as a\n * fast path for \"looks like a claimed key, do not overwrite\". The\n * effective-credential resolver validates with\n * `DevApiKeySchema.safeParse` because a `gt_dev_` prefix alone is not\n * sufficient to authenticate against the backend.\n *\n * @internal Exported for unit testing only.\n */\nexport function isDevApiKey(value: string | null | undefined): boolean {\n if (value === null || value === undefined) return false;\n return value.trim().startsWith(\"gt_dev_\");\n}\n\n/**\n * Returns true when the given API key value is a fully-valid anonymous\n * API key (matches `AnonApiKeySchema`). Used by `registerViaCli` as a\n * runtime guard so that a `DevApiKey` cannot be passed via process\n * arguments to vendor MCP CLIs (which would expose it via `ps` on\n * multi-user hosts).\n *\n * @internal Exported for unit testing only.\n */\nexport function isAnonApiKey(value: string | null | undefined): boolean {\n if (value === null || value === undefined) return false;\n return AnonApiKeySchema.safeParse(value).success;\n}\n\n/**\n * The MCP-effective credential, tagged by which on-disk source produced\n * it. `env-local` and `claimed-key` carry a branded `DevApiKey`;\n * `anon` carries a branded `AnonApiKey`. Internal — not re-exported.\n */\nexport type EffectiveMcpCredential =\n | { source: \"env-local\"; key: DevApiKey }\n | { source: \"claimed-key\"; key: DevApiKey }\n | { source: \"anon\"; key: AnonApiKey };\n\n/**\n * Surfaced when the resolver detected a recoverable anomaly the caller\n * should inform the user about without printing key material.\n *\n * - `malformed-env-local`: `.env.local` set `GLASSTRACE_API_KEY` to a\n * value that fails `DevApiKeySchema`. The resolver fell through.\n * - `claimed-key-only`: the effective credential came from\n * `.glasstrace/claimed-key` because `.env.local` had no usable dev\n * key. Suggest the user copy the key into `.env.local`.\n */\nexport type ResolveWarning = \"malformed-env-local\" | \"claimed-key-only\";\n\n/**\n * The resolved credential plus the on-disk anon key (returned\n * separately so the staleness check does not have to re-read the\n * file) and any warnings the caller should surface to the user.\n */\nexport interface ResolveResult {\n effective: EffectiveMcpCredential | null;\n anonKey: AnonApiKey | null;\n warnings: ReadonlyArray<ResolveWarning>;\n}\n\n/**\n * Resolves the MCP-effective credential for a project, in priority\n * order: `.env.local` `GLASSTRACE_API_KEY` (validated as\n * `DevApiKeySchema`) → `.glasstrace/claimed-key` (validated as\n * `DevApiKeySchema`) → `.glasstrace/anon_key` (`AnonApiKey`). Returns\n * `null` for `effective` only when no source produced a usable key.\n *\n * The function is async because it touches the filesystem. It is\n * called only on the post-claim runtime branch and from the CLI\n * commands `glasstrace init` and `glasstrace mcp add`. It is **not**\n * on the steady-state init path.\n */\nexport async function resolveEffectiveMcpCredential(\n projectRoot?: string,\n): Promise<ResolveResult> {\n const root = projectRoot ?? process.cwd();\n const warnings: ResolveWarning[] = [];\n\n const envLocalKey = await readEnvLocalDevKey(root, warnings);\n const claimedKey = envLocalKey === null ? await readClaimedKey(root) : null;\n const anonKey = await readAnonKey(root);\n\n let effective: EffectiveMcpCredential | null = null;\n if (envLocalKey !== null) {\n effective = { source: \"env-local\", key: envLocalKey };\n } else if (claimedKey !== null) {\n effective = { source: \"claimed-key\", key: claimedKey };\n warnings.push(\"claimed-key-only\");\n } else if (anonKey !== null) {\n effective = { source: \"anon\", key: anonKey };\n }\n\n return { effective, anonKey, warnings };\n}\n\nasync function readEnvLocalDevKey(\n root: string,\n warnings: ResolveWarning[],\n): Promise<DevApiKey | null> {\n const modules = await loadFsPath();\n if (!modules) return null;\n\n const envPath = modules.path.join(root, \".env.local\");\n let content: string;\n try {\n content = await modules.fs.readFile(envPath, \"utf-8\");\n } catch {\n return null;\n }\n\n const raw = readEnvLocalApiKey(content);\n if (raw === null) return null;\n\n const parsed = DevApiKeySchema.safeParse(raw);\n if (!parsed.success) {\n warnings.push(\"malformed-env-local\");\n return null;\n }\n return parsed.data;\n}\n\n/**\n * Source label for the credential a marker file describes.\n *\n * @internal\n */\nexport type MarkerCredentialSource = \"env-local\" | \"claimed-key\" | \"anon\";\n\n/**\n * Descriptor passed to {@link writeMcpMarker} and matched by\n * {@link readMcpMarker}. `credentialHash` is the\n * `identityFingerprint` of the credential actually written into the\n * managed MCP config — never the credential itself.\n *\n * @internal\n */\nexport interface MarkerTarget {\n credentialSource: MarkerCredentialSource;\n credentialHash: string;\n}\n\n/**\n * Normalized state of a `.glasstrace/mcp-connected` marker on disk.\n *\n * - `absent`: no marker file present.\n * - `valid`: a v1 or v2 marker that parsed cleanly. v1 markers are\n * reported as `credentialSource = \"anon\"` with `credentialHash`\n * taken from the legacy `keyHash` field (the v1 schema can only\n * describe an anon credential).\n * - `unknown-version`: the marker has `version > 2`. Treat as\n * not-configured so a future SDK that wrote the marker doesn't\n * block this SDK from refreshing.\n * - `corrupted`: parse failure or schema mismatch. Treat as\n * not-configured.\n *\n * @internal\n */\nexport type MarkerState =\n | { status: \"absent\" }\n | { status: \"valid\"; credentialSource: MarkerCredentialSource; credentialHash: string }\n | { status: \"unknown-version\" }\n | { status: \"corrupted\" };\n\nconst MCP_MARKER_FILE = \"mcp-connected\";\nconst GLASSTRACE_DIR = \".glasstrace\";\n\n/**\n * Reads `.glasstrace/mcp-connected` and returns its normalized state.\n * Used by `mcp add` (marker-mismatch detection) and by\n * {@link writeMcpMarker} (skip-if-match optimization).\n *\n * Reader rules per the design (`SDK-034 D3`):\n * - `version === undefined` → v1: `{ keyHash, configuredAt }`. Mapped\n * to `credentialSource: \"anon\"`, `credentialHash: keyHash`. v1's\n * `keyHash` is itself produced by `identityFingerprint`, so the\n * format matches v2 without conversion.\n * - `version === 2` → v2 reader.\n * - `version > 2` → `unknown-version` (conservative-fail).\n * - Parse failure → `corrupted` (conservative-fail).\n *\n * @internal Exported for unit testing only.\n */\nexport async function readMcpMarker(projectRoot?: string): Promise<MarkerState> {\n const root = projectRoot ?? process.cwd();\n const modules = await loadFsPath();\n if (!modules) return { status: \"absent\" };\n\n const markerPath = modules.path.join(root, GLASSTRACE_DIR, MCP_MARKER_FILE);\n let content: string;\n try {\n content = await modules.fs.readFile(markerPath, \"utf-8\");\n } catch {\n return { status: \"absent\" };\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(content);\n } catch {\n return { status: \"corrupted\" };\n }\n\n if (parsed === null || typeof parsed !== \"object\") {\n return { status: \"corrupted\" };\n }\n\n const obj = parsed as Record<string, unknown>;\n const version = obj[\"version\"];\n\n if (version === undefined) {\n // v1: { keyHash, configuredAt }\n const keyHash = obj[\"keyHash\"];\n if (typeof keyHash !== \"string\" || keyHash === \"\") {\n return { status: \"corrupted\" };\n }\n return {\n status: \"valid\",\n credentialSource: \"anon\",\n credentialHash: keyHash,\n };\n }\n\n if (version === 2) {\n const source = obj[\"credentialSource\"];\n const hash = obj[\"credentialHash\"];\n if (\n (source !== \"env-local\" && source !== \"claimed-key\" && source !== \"anon\") ||\n typeof hash !== \"string\" ||\n hash === \"\"\n ) {\n return { status: \"corrupted\" };\n }\n return {\n status: \"valid\",\n credentialSource: source,\n credentialHash: hash,\n };\n }\n\n if (typeof version === \"number\" && version > 2) {\n return { status: \"unknown-version\" };\n }\n\n return { status: \"corrupted\" };\n}\n\n/**\n * Writes a v2 `.glasstrace/mcp-connected` marker. Returns `true` when\n * the marker was created or updated, `false` when an existing marker\n * already records the same `(credentialSource, credentialHash)` pair\n * and was left untouched.\n *\n * Writer always emits v2 with `version: 2`. The legacy `keyHash`\n * field is intentionally omitted from new writes — v1 readers ignore\n * unknown fields and the duplicate would diverge over time. v3+ and\n * corrupted markers are unconditionally overwritten.\n *\n * The directory is created with `0o700` and the file with `0o600`,\n * matching existing scaffolder behavior.\n *\n * @internal Exported for unit testing only.\n */\nexport async function writeMcpMarker(\n projectRoot: string,\n target: MarkerTarget,\n): Promise<boolean> {\n const modules = await loadFsPath();\n if (!modules) return false;\n\n const dirPath = modules.path.join(projectRoot, GLASSTRACE_DIR);\n const markerPath = modules.path.join(dirPath, MCP_MARKER_FILE);\n\n const state = await readMcpMarker(projectRoot);\n if (\n state.status === \"valid\" &&\n state.credentialSource === target.credentialSource &&\n state.credentialHash === target.credentialHash\n ) {\n return false;\n }\n\n await modules.fs.mkdir(dirPath, { recursive: true, mode: 0o700 });\n\n const body = JSON.stringify(\n {\n version: 2,\n credentialSource: target.credentialSource,\n credentialHash: target.credentialHash,\n configuredAt: new Date().toISOString(),\n },\n null,\n 2,\n );\n\n await modules.fs.writeFile(markerPath, body, { mode: 0o600 });\n // writeFile mode only applies on creation on some platforms.\n await modules.fs.chmod(markerPath, 0o600);\n return true;\n}\n\nconst MCP_CONFIG_FILE = \"mcp.json\";\n\n/**\n * The set of outcomes the runtime claim-refresh helper can produce.\n *\n * - `rewrote`: `.glasstrace/mcp.json` matched the SDK-shaped output\n * for the on-disk anon key, was rewritten with the effective\n * credential, and the marker was updated.\n * - `preserved`: `.glasstrace/mcp.json` exists but does not match the\n * SDK-shaped output for the on-disk anon key. The file is left\n * untouched (the user may have hand-edited it). The marker is not\n * touched.\n * - `absent`: `.glasstrace/mcp.json` does not exist (`ENOENT`), or\n * no anon key is on disk so there is nothing to compare against. A\n * project without an anon key never had an SDK-shaped `mcp.json`\n * written by the runtime path, so this branch is a true no-op.\n * - `skipped-anon-source`: the effective credential is `null` or its\n * source is `\"anon\"`. Either way, there is no claim transition to\n * refresh for. Caller should generally gate on\n * `effective.source !== \"anon\"` before invoking the helper; this\n * branch is the runtime-side belt-and-suspenders.\n * - `skipped-not-persisted`: never reached in practice — the caller\n * in `init-client.ts` gates on `writeClaimedKey`'s `persisted` not\n * being `\"none\"`. The variant exists so an exhaustive switch in\n * the caller stays exhaustive if the gate is removed.\n *\n * @internal\n */\nexport type RuntimeRefreshAction =\n | \"rewrote\"\n | \"preserved\"\n | \"absent\"\n | \"skipped-anon-source\"\n | \"skipped-not-persisted\";\n\nlet refreshNudgeEmitted = false;\n\n/**\n * @internal Exported for unit testing only — resets the per-process\n * \"refresh nudge already emitted\" flag.\n */\nexport function __resetRefreshNudgeForTest(): void {\n refreshNudgeEmitted = false;\n}\n\n/**\n * Emits a single redacted stderr line announcing the MCP config\n * refresh. Deduplicated per process via a module-level flag — a\n * second call within the same process is a no-op. Cross-process\n * dedup (the same user running `mcp add` in another terminal moments\n * later) is explicitly out of scope.\n */\nfunction emitRefreshNudge(persistedSource: \"env-local\" | \"claimed-key\"): void {\n if (refreshNudgeEmitted) return;\n refreshNudgeEmitted = true;\n try {\n if (persistedSource === \"claimed-key\") {\n process.stderr.write(\n \"[glasstrace] MCP config refreshed for the new credential. \" +\n \"Copy .glasstrace/claimed-key into .env.local so Codex can pick it up on next restart.\\n\",\n );\n } else {\n process.stderr.write(\n \"[glasstrace] MCP config refreshed for the new credential.\\n\",\n );\n }\n } catch {\n // stderr is best-effort; refresh outcome must not depend on it.\n }\n}\n\n/**\n * Returns the SDK-shaped JSON for `.glasstrace/mcp.json` (the generic\n * MCP config used at runtime). Inlined here — and intentionally not\n * imported from `agent-detection/configs.ts` — because the runtime\n * path must not pull `agent-detection` into the runtime bundle. The\n * shape matches what `generateMcpConfig({ name: \"generic\", ... },\n * endpoint, bearer)` would produce. If the agent-detection version\n * diverges, the staleness check stops detecting SDK-managed configs;\n * a regression test against `generateMcpConfig`'s \"generic\" branch\n * lives in `tests/unit/sdk/mcp-runtime.test.ts`.\n */\nfunction genericMcpConfigContent(endpoint: string, bearer: string): string {\n return JSON.stringify(\n {\n mcpServers: {\n glasstrace: {\n url: endpoint,\n headers: {\n Authorization: `Bearer ${bearer}`,\n },\n },\n },\n },\n null,\n 2,\n );\n}\n\n/**\n * Refreshes `.glasstrace/mcp.json` after a successful account claim\n * transition has persisted a dev/account credential to disk (via\n * `writeClaimedKey`). The file is rewritten only when its content\n * matches the SDK-shaped output for the project's on-disk anon key\n * (canonical-JSON equivalence via `mcpConfigMatches` — whitespace and\n * key order are normalised before comparison). User-edited or\n * third-party `mcp.json` content is preserved.\n *\n * Atomic write protocol: write the replacement to a sibling temp\n * path, set `0o600`, then `rename` into place. This matches the\n * existing pattern at `init-client.ts` for `.glasstrace/config`,\n * `anon-key.ts` for `.glasstrace/anon_key`, and `runtime-state.ts`.\n * The temp must be on the same filesystem as the destination for the\n * `rename` to be atomic.\n *\n * The helper is invoked only on the post-claim runtime branch (see\n * `init-client.ts` `performInit`) and never on the steady-state init\n * path. It must not throw — failures during write/chmod/rename or\n * marker update surface as `\"preserved\"` so the caller's\n * `claimResult` return is preserved. The temp file is best-effort\n * cleaned up on failure to avoid leaving stale `.tmp` siblings on\n * disk.\n *\n * @internal Exported for unit testing only; not re-exported from\n * `node-entry.ts` or `index.ts`.\n */\nexport async function refreshGenericMcpConfigAtRuntime(\n projectRoot: string,\n effective: EffectiveMcpCredential | null,\n anonKeyOnDisk: AnonApiKey | null,\n): Promise<{ action: RuntimeRefreshAction }> {\n if (effective === null || effective.source === \"anon\") {\n return { action: \"skipped-anon-source\" };\n }\n\n // Dev-key-only project (no .glasstrace/anon_key on disk): the\n // staleness check has nothing to compare against. The SDK never\n // wrote mcp.json without an anon key, so there is nothing to\n // refresh.\n if (anonKeyOnDisk === null) {\n return { action: \"absent\" };\n }\n\n const modules = await loadFsPath();\n if (!modules) return { action: \"absent\" };\n\n const dirPath = modules.path.join(projectRoot, GLASSTRACE_DIR);\n const configPath = modules.path.join(dirPath, MCP_CONFIG_FILE);\n\n let existing: string;\n try {\n existing = await modules.fs.readFile(configPath, \"utf-8\");\n } catch (err) {\n const code = (err as NodeJS.ErrnoException).code;\n if (code === \"ENOENT\") {\n return { action: \"absent\" };\n }\n return { action: \"preserved\" };\n }\n\n const expectedAnon = genericMcpConfigContent(MCP_ENDPOINT, anonKeyOnDisk);\n if (!mcpConfigMatches(existing, expectedAnon)) {\n return { action: \"preserved\" };\n }\n\n // SDK-managed and stale. Replace atomically per SDK 2.0 §4.3:\n // tmp + fsync(tmp) + rename + fsync(parent). Any failure in the\n // helper or marker update path must produce a non-throw outcome\n // so the caller's claimResult return is preserved; the helper\n // best-effort cleans up the .tmp sibling on failure.\n const replacement = genericMcpConfigContent(MCP_ENDPOINT, effective.key);\n try {\n await atomicWriteFile(configPath, replacement, { mode: 0o600 });\n\n await writeMcpMarker(projectRoot, {\n credentialSource: effective.source,\n credentialHash: identityFingerprint(effective.key),\n });\n } catch {\n return { action: \"preserved\" };\n }\n\n emitRefreshNudge(effective.source);\n\n return { action: \"rewrote\" };\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport { AnonApiKeySchema } from \"@glasstrace/protocol\";\nimport type { AnonApiKey } from \"@glasstrace/protocol\";\nimport {\n atomicWriteFileSyncWithTmp,\n fsyncParentDirSync,\n writeAndFsyncTempSync,\n} from \"../atomic-write.js\";\n\n/**\n * Standardized static discovery-file path, served at\n * `<static-root>/.well-known/glasstrace.json` (per RFC 8615) with\n * MIME type `application/json`.\n *\n * The SDK writes the file to this relative path under the\n * framework-specific static root (`public/` for Next.js, Remix, Astro;\n * `static/` for SvelteKit) and the browser extension fetches it from\n * the same path under the deployed origin.\n *\n * @drift-check RFC 8615 (https://www.rfc-editor.org/rfc/rfc8615) + ../glasstrace-product/docs/component-designs/sdk-architecture.md §7.1 Static discovery file\n */\nexport const WELL_KNOWN_GLASSTRACE_PATH = \".well-known/glasstrace.json\" as const;\n\n/**\n * Current schema version for `.well-known/glasstrace.json`. Consumers\n * (primarily the Glasstrace browser extension) MUST tolerate unknown\n * integers >= 1 per the forward-compatibility rule in the design doc\n * (\"SDK Discovery Endpoint / Static File — Component Design\", §5.3).\n */\nexport const DISCOVERY_FILE_VERSION = 1 as const;\n\n/**\n * Schema of the static discovery file written by `sdk init`.\n *\n * Version 1 defines exactly two required fields: `version` and `key`.\n * Additional fields may appear in later schema versions — consumers MUST\n * ignore unknown fields (forward-compatibility) and MUST reject files\n * whose `key` does not match `^gt_anon_[a-f0-9]{48}$`.\n */\nexport interface DiscoveryFileV1 {\n version: typeof DISCOVERY_FILE_VERSION;\n key: AnonApiKey;\n}\n\n/**\n * Detected framework-specific static root. `public` covers Next.js,\n * Remix, and Astro; `static` covers SvelteKit. No other frameworks\n * differ today per the design doc's §4.3 table.\n */\nexport type StaticRootLayout = \"public\" | \"static\";\n\n/**\n * Result returned by {@link resolveStaticRoot} so callers can report the\n * framework-specific path they targeted (used in init summary lines and\n * rollback output).\n */\nexport interface StaticRootResolution {\n /** Absolute path to the static root directory (may not exist yet). */\n absolutePath: string;\n /** Which layout was chosen. */\n layout: StaticRootLayout;\n}\n\n/**\n * Describes the outcome of a single call to {@link writeDiscoveryFile} so\n * callers can surface an accurate summary line without re-reading the\n * file. Mirrors the DISC-1247 Scenario 2 re-init preservation contract:\n * a valid file whose `key` already matches the on-disk anon key is left\n * alone rather than rewritten.\n */\nexport type WriteDiscoveryAction =\n | \"created\"\n | \"updated-stale\"\n | \"skipped-matches\"\n | \"skipped-foreign\"\n | \"failed\";\n\n/**\n * Structured result from {@link writeDiscoveryFile}.\n */\nexport interface WriteDiscoveryResult {\n action: WriteDiscoveryAction;\n /** Absolute path of the discovery file (whether or not it was written). */\n filePath: string;\n /** Static root that was resolved, useful for `.gitignore` wiring. */\n layout: StaticRootLayout;\n /**\n * When `action === \"failed\"`, a short human-readable reason. Never\n * contains anon key bytes — callers can forward it to logs safely.\n */\n error?: string;\n}\n\n/**\n * Detects the project's framework-specific static root using the ordered\n * check from §4.4 of the design doc:\n *\n * 1. Classify as SvelteKit (→ `static/`) when `package.json` declares\n * `\"type\": \"module\"` AND the project contains `svelte.config.js` (or\n * `svelte.config.ts`) OR `src/app.html`. These signals together are\n * specific enough to avoid false positives on generic ESM projects.\n * 2. Otherwise use `public/` — this covers Next.js, Remix, Astro, and\n * plain Node web apps, which all serve `public/` verbatim.\n *\n * Returns the absolute directory path and the chosen layout. Does NOT\n * create the directory; callers use {@link writeDiscoveryFile}, which\n * creates any missing parents atomically.\n *\n * @internal Exported for unit testing only.\n */\nexport function resolveStaticRoot(projectRoot: string): StaticRootResolution {\n if (isSvelteKitProject(projectRoot)) {\n return {\n absolutePath: path.join(projectRoot, \"static\"),\n layout: \"static\",\n };\n }\n return {\n absolutePath: path.join(projectRoot, \"public\"),\n layout: \"public\",\n };\n}\n\n/**\n * Heuristic for SvelteKit detection. The design doc deliberately scopes\n * the check narrowly so a plain ESM library is never misclassified —\n * `svelte.config.{js,ts}` or `src/app.html` is the SvelteKit fingerprint,\n * and both must coexist with an ESM package.json.\n */\nfunction isSvelteKitProject(projectRoot: string): boolean {\n const pkgPath = path.join(projectRoot, \"package.json\");\n let isEsm = false;\n try {\n const pkgContent = fs.readFileSync(pkgPath, \"utf-8\");\n const parsed = JSON.parse(pkgContent) as { type?: unknown };\n isEsm = parsed.type === \"module\";\n } catch {\n // Missing or malformed package.json — fall through to default layout.\n return false;\n }\n if (!isEsm) return false;\n\n const svelteConfigJs = path.join(projectRoot, \"svelte.config.js\");\n const svelteConfigTs = path.join(projectRoot, \"svelte.config.ts\");\n const appHtml = path.join(projectRoot, \"src\", \"app.html\");\n return (\n fs.existsSync(svelteConfigJs) ||\n fs.existsSync(svelteConfigTs) ||\n fs.existsSync(appHtml)\n );\n}\n\n/**\n * Returns the project-relative path of the discovery file for the given\n * layout, suitable for surfacing in summary lines and `.gitignore` entries.\n */\nexport function relativeDiscoveryPath(layout: StaticRootLayout): string {\n const rootDir = layout === \"static\" ? \"static\" : \"public\";\n return `${rootDir}/${WELL_KNOWN_GLASSTRACE_PATH}`;\n}\n\n/**\n * Parses an existing discovery file and returns its key if the schema is\n * valid, or `null` when the file is missing, unreadable, not JSON, or\n * does not match the version-1 shape. The check is deliberately strict —\n * a corrupt or third-party-authored file is treated as \"no file\" so\n * {@link writeDiscoveryFile} overwrites it with a fresh SDK-managed copy.\n *\n * Extra unknown fields are tolerated (§5.3 forward-compatibility).\n *\n * @internal Exported for unit testing only.\n */\nexport function readExistingDiscoveryFile(\n filePath: string,\n): { key: AnonApiKey; extras: Record<string, unknown> } | null {\n let raw: string;\n try {\n raw = fs.readFileSync(filePath, \"utf-8\");\n } catch {\n return null;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n return null;\n }\n\n if (\n parsed === null ||\n typeof parsed !== \"object\" ||\n Array.isArray(parsed)\n ) {\n return null;\n }\n\n const obj = parsed as Record<string, unknown>;\n const versionRaw = obj.version;\n if (\n typeof versionRaw !== \"number\" ||\n !Number.isInteger(versionRaw) ||\n versionRaw < 1\n ) {\n return null;\n }\n\n const keyResult = AnonApiKeySchema.safeParse(obj.key);\n if (!keyResult.success) {\n return null;\n }\n\n // Preserve user-added fields (extras) so re-init round-trips any custom\n // keys the consumer added. `version` and `key` are SDK-managed and\n // excluded from the extras object.\n const extras: Record<string, unknown> = {};\n for (const [k, v] of Object.entries(obj)) {\n if (k === \"version\" || k === \"key\") continue;\n extras[k] = v;\n }\n\n return { key: keyResult.data, extras };\n}\n\n/**\n * Serializes the discovery payload deterministically (pretty-printed JSON\n * with a trailing newline). Deterministic output keeps git diffs clean\n * when the file is checked in and matches the atomic-write contract:\n * byte-identical output on re-init when `extras` is unchanged.\n */\nfunction serializeDiscoveryPayload(\n key: AnonApiKey,\n extras: Record<string, unknown>,\n): string {\n // Key ordering: version, key, then extras in their original insertion\n // order. Preserves DISC-1247 Scenario 2 alignment — a user who added\n // `\"note\": \"…\"` after `\"key\"` sees the same ordering on re-init.\n const payload: Record<string, unknown> = {\n version: DISCOVERY_FILE_VERSION,\n key,\n ...extras,\n };\n return JSON.stringify(payload, null, 2) + \"\\n\";\n}\n\n/**\n * Writes the discovery file at `<staticRoot>/.well-known/glasstrace.json`\n * atomically.\n *\n * Behavior (per design doc §6.1 and §6.5):\n *\n * - When the target file does not exist, creates it with `{ version: 1,\n * key: <anonKey> }` after creating the `.well-known/` directory if\n * missing.\n * - When the target exists AND parses as a valid version-1 payload AND\n * its `key` matches the supplied `anonKey`: preserves the file (and\n * any user-added extra fields) and returns `\"skipped-matches\"`.\n * - When the target exists AND parses valid BUT its `key` does not\n * match: rewrites the file with the fresh key, preserving extras.\n * Returns `\"updated-stale\"`.\n * - When the target exists BUT fails to parse (corrupt, foreign-authored,\n * wrong schema): rewrites with a fresh SDK-managed payload and returns\n * `\"skipped-foreign\"` to signal that user content was not preserved.\n * - On any unexpected I/O error: returns `\"failed\"` with an error string.\n *\n * Uses a sibling temp file + `renameSync` for atomicity so concurrent\n * readers (e.g., a browser extension polling during dev server startup)\n * never observe a half-written file.\n *\n * @param projectRoot - Absolute path to the project root directory.\n * @param anonKey - The anon key currently on disk (see `anon-key.ts`).\n */\nexport function writeDiscoveryFile(\n projectRoot: string,\n anonKey: AnonApiKey,\n): WriteDiscoveryResult {\n const { absolutePath: staticRoot, layout } = resolveStaticRoot(projectRoot);\n const wellKnownDir = path.join(staticRoot, \".well-known\");\n const filePath = path.join(wellKnownDir, \"glasstrace.json\");\n\n let existingAction: WriteDiscoveryAction;\n let extras: Record<string, unknown> = {};\n\n if (fs.existsSync(filePath)) {\n const existing = readExistingDiscoveryFile(filePath);\n if (existing === null) {\n // Unreadable / malformed / non-SDK content — overwrite with a\n // fresh payload so the extension can discover the current key.\n // Extras are NOT preserved because we cannot safely parse them.\n existingAction = \"skipped-foreign\";\n } else if (existing.key === anonKey) {\n // Valid and already matches — leave the file alone (§6.5 step 2).\n return {\n action: \"skipped-matches\",\n filePath,\n layout,\n };\n } else {\n // Valid but stale — replace the key, preserve extras (§6.5 step 3).\n extras = existing.extras;\n existingAction = \"updated-stale\";\n }\n } else {\n existingAction = \"created\";\n }\n\n const tmpPath = `${filePath}.tmp-${process.pid}`;\n // On Windows, `renameSync` fails with EPERM/EEXIST when the\n // destination already exists. Rather than `unlink` the destination\n // first (which would cause data loss if the subsequent rename fails),\n // move the destination to a sibling backup path, commit the rename,\n // and only then delete the backup. If the rename fails, restore the\n // backup so the original file is preserved.\n const needsWindowsReplace =\n process.platform === \"win32\" && fs.existsSync(filePath);\n const backupPath = needsWindowsReplace\n ? `${filePath}.bak-${process.pid}`\n : null;\n\n try {\n fs.mkdirSync(wellKnownDir, { recursive: true });\n const payload = serializeDiscoveryPayload(anonKey, extras);\n\n if (backupPath !== null) {\n // Windows backup-rollback path. Step the SDK 2.0 §4.3 protocol\n // out manually so the backup rename can interleave between the\n // tmp-fsync and the final rename:\n // 1. write tmp + fsync(tmp)\n // 2. rename existing → backup (Windows requires destination\n // to be free)\n // 3. rename tmp → final\n // 4. fsync(parent) so the rename pair is durable\n // Mode is 0o644 for static/discoverable files per spec §4.3.\n writeAndFsyncTempSync(tmpPath, payload, {\n encoding: \"utf-8\",\n mode: 0o644,\n });\n fs.renameSync(filePath, backupPath);\n try {\n fs.renameSync(tmpPath, filePath);\n } catch (renameErr) {\n try {\n fs.renameSync(backupPath, filePath);\n } catch {\n // Restoration failed; nothing more we can do. Surface the\n // original rename error below so the caller sees the cause.\n }\n throw renameErr;\n }\n fsyncParentDirSync(filePath);\n try {\n fs.unlinkSync(backupPath);\n } catch {\n // Backup cleanup is best-effort; a stale `.bak-<pid>` is\n // preferable to a spurious failure after a successful write.\n }\n } else {\n // Non-Windows / no pre-existing target: full helper composes\n // tmp + fsync(tmp) + rename + fsync(parent) atomically.\n atomicWriteFileSyncWithTmp(filePath, tmpPath, payload, {\n encoding: \"utf-8\",\n mode: 0o644,\n });\n }\n\n return { action: existingAction, filePath, layout };\n } catch (err) {\n // Best-effort: remove the temp file if it was created before the\n // failure so a stale `.tmp-<pid>` does not clutter `.well-known/`.\n try {\n if (fs.existsSync(tmpPath)) {\n fs.unlinkSync(tmpPath);\n }\n } catch {\n // Swallow: the write has already failed; do not mask the root cause.\n }\n return {\n action: \"failed\",\n filePath,\n layout,\n error: err instanceof Error ? err.message : String(err),\n };\n }\n}\n\n/**\n * Describes the outcome of {@link removeDiscoveryFile}. `\"removed\"` means\n * the file existed and was deleted; `\"not-found\"` means there was nothing\n * to remove (no error). `\"failed\"` preserves an error string.\n */\nexport type RemoveDiscoveryAction = \"removed\" | \"not-found\" | \"failed\";\n\n/** Structured result from {@link removeDiscoveryFile}. */\nexport interface RemoveDiscoveryResult {\n action: RemoveDiscoveryAction;\n filePath: string;\n layout: StaticRootLayout;\n /** True when the enclosing `.well-known/` directory was removed too. */\n directoryRemoved: boolean;\n error?: string;\n}\n\n/**\n * Removes the discovery file written by {@link writeDiscoveryFile} if\n * present, and removes the enclosing `.well-known/` directory when it\n * becomes empty. Tolerant of missing files, missing directories, and\n * user-owned sibling content inside `.well-known/` (never deletes a\n * sibling file).\n *\n * Checks BOTH `public/.well-known/glasstrace.json` and\n * `static/.well-known/glasstrace.json` rather than only the\n * currently-inferred layout: if layout detection changes between\n * init and uninit (for example, a SvelteKit project has its\n * `package.json` modified so the heuristic no longer matches),\n * the file written under the original layout would otherwise\n * be orphaned.\n *\n * Matches the uninit contract from design doc §6.4.\n *\n * @param projectRoot - Absolute path to the project root directory.\n */\nexport function removeDiscoveryFile(\n projectRoot: string,\n): RemoveDiscoveryResult {\n const { layout: inferredLayout } = resolveStaticRoot(projectRoot);\n\n // Sweep both candidate layouts so an orphaned file in the non-inferred\n // location is still cleaned up. The returned layout describes where\n // a file was actually removed (preferring the inferred layout when a\n // file existed in both, which is not a supported state but is\n // tolerated); when neither layout had a file, the returned layout\n // mirrors the inferred one so callers surface a stable relative path.\n const layouts: StaticRootLayout[] = [\"public\", \"static\"];\n\n interface LayoutOutcome {\n layout: StaticRootLayout;\n filePath: string;\n removed: boolean;\n directoryRemoved: boolean;\n }\n const outcomes: LayoutOutcome[] = [];\n\n for (const layout of layouts) {\n const staticRoot = path.join(projectRoot, layout);\n const wellKnownDir = path.join(staticRoot, \".well-known\");\n const filePath = path.join(wellKnownDir, \"glasstrace.json\");\n\n let removed = false;\n try {\n if (fs.existsSync(filePath)) {\n fs.unlinkSync(filePath);\n removed = true;\n }\n } catch (err) {\n return {\n action: \"failed\",\n filePath,\n layout,\n directoryRemoved: false,\n error: err instanceof Error ? err.message : String(err),\n };\n }\n\n // Only attempt to prune the enclosing `.well-known/` when we actually\n // removed the discovery file from this layout. Pruning unconditionally\n // would delete a user-owned empty directory (that Glasstrace never\n // populated) as a silent side effect of `sdk uninit`.\n let directoryRemoved = false;\n if (removed) {\n try {\n if (fs.existsSync(wellKnownDir)) {\n const entries = fs.readdirSync(wellKnownDir);\n if (entries.length === 0) {\n fs.rmdirSync(wellKnownDir);\n directoryRemoved = true;\n }\n }\n } catch {\n // Best-effort cleanup; never surface as an error to uninit.\n }\n }\n\n outcomes.push({ layout, filePath, removed, directoryRemoved });\n }\n\n // Pick the outcome to report: prefer one where a file was removed. When\n // both layouts had a file (not a supported state, but tolerated),\n // prefer the inferred layout. When neither had a file, report the\n // inferred layout so callers receive a stable relative path.\n const removals = outcomes.filter((o) => o.removed);\n const chosen: LayoutOutcome = (() => {\n if (removals.length === 0) {\n return (\n outcomes.find((o) => o.layout === inferredLayout) ?? outcomes[0]!\n );\n }\n if (removals.length === 1) return removals[0]!;\n return (\n removals.find((o) => o.layout === inferredLayout) ?? removals[0]!\n );\n })();\n\n // Propagate directoryRemoved across both sweeps so the uninit summary\n // reflects every pruned directory even when only one was the primary.\n const anyDirectoryRemoved = outcomes.some((o) => o.directoryRemoved);\n\n return {\n action: removals.length > 0 ? \"removed\" : \"not-found\",\n filePath: chosen.filePath,\n layout: chosen.layout,\n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,qDAAgD;AAAA,EAC/D;AAEA,SAAO,EAAE,UAAU,OAAO,SAAS,IAAI,IAAI,GAAG,SAAS,UAAU,OAAO;AAC1E;","names":["fs","path","fs","fs"]}
|
|
1
|
+
{"version":3,"sources":["../../src/cli/uninit.ts","../../src/cli/constants.ts","../../src/atomic-write.ts","../../src/mcp-runtime.ts","../../src/cli/discovery-file.ts"],"sourcesContent":["import * as fs from \"node:fs\";\nimport * as os from \"node:os\";\nimport * as path from \"node:path\";\nimport { NEXT_CONFIG_NAMES } from \"./constants.js\";\nimport { readEnvLocalApiKey, isDevApiKey } from \"../mcp-runtime.js\";\nimport { atomicWriteFileSync } from \"../atomic-write.js\";\nimport {\n removeDiscoveryFile,\n relativeDiscoveryPath,\n} from \"./discovery-file.js\";\n\n/**\n * Options for the uninit command.\n */\nexport interface UninitOptions {\n projectRoot: string;\n dryRun: boolean;\n /**\n * When true, skip interactive confirmation before destructive actions\n * such as removing a claimed developer API key from `.env.local`\n * (DISC-1247 Scenario 6).\n */\n force?: boolean;\n /**\n * Optional prompt callback; when omitted, uninit uses a TTY-based\n * `readline` prompt in interactive mode and defaults to `false`\n * (abort) when no TTY is attached. Exposed for testing.\n */\n prompt?: (question: string, defaultValue: boolean) => Promise<boolean>;\n}\n\n/**\n * Result of running the uninit command.\n */\nexport interface UninitResult {\n exitCode: number;\n summary: string[];\n warnings: string[];\n errors: string[];\n}\n\n/**\n * MCP config files that init may create.\n * These are JSON files containing `mcpServers.glasstrace`.\n */\nconst MCP_CONFIG_FILES = [\".mcp.json\", \".cursor/mcp.json\", \".gemini/settings.json\"] as const;\n\n/**\n * Agent info files that may contain glasstrace marker sections.\n * Both HTML-style (`<!-- glasstrace:mcp:start -->`) and hash-style\n * (`# glasstrace:mcp:start`) markers are supported.\n */\nconst AGENT_INFO_FILES = [\n \"CLAUDE.md\",\n \"codex.md\",\n \".cursorrules\",\n] as const;\n\n/**\n * Advances past a string literal (double-quoted, single-quoted, or template\n * literal), respecting backslash escapes.\n *\n * Note: Template literals with `${...}` interpolations containing nested\n * backticks are not fully supported — the scanner stops at the first\n * unescaped backtick. This is acceptable because config files (the primary\n * use case for `findMatchingParen`/`findMatchingBrace`) do not use nested\n * template literals.\n *\n * @param text - The source text.\n * @param start - The index of the opening quote character.\n * @param quote - The quote character (`\"`, `'`, or `` ` ``).\n * @returns The index immediately after the closing quote.\n * @internal Exported for unit testing only.\n */\nexport function skipString(text: string, start: number, quote: string): number {\n let i = start + 1;\n while (i < text.length) {\n if (text[i] === \"\\\\\") {\n i += 2;\n continue;\n }\n if (text[i] === quote) {\n return i + 1;\n }\n i++;\n }\n return text.length;\n}\n\n/**\n * Finds the matching closing delimiter for an opening delimiter at the given\n * position, accounting for nesting and skipping delimiters that appear inside\n * string literals (`\"`, `'`, `` ` ``), single-line comments (`//`), and block\n * comments.\n *\n * @param text - The source text to search.\n * @param openPos - The index of the opening delimiter.\n * @param openChar - The opening delimiter character (e.g., `(` or `{`).\n * @param closeChar - The closing delimiter character (e.g., `)` or `}`).\n * @returns The index of the matching closing delimiter, or -1 if not found.\n * @internal Exported for unit testing only.\n */\nexport function findMatchingDelimiter(\n text: string,\n openPos: number,\n openChar: string,\n closeChar: string,\n): number {\n let depth = 0;\n let i = openPos;\n while (i < text.length) {\n const ch = text[i];\n\n // Skip string literals\n if (ch === '\"' || ch === \"'\" || ch === \"`\") {\n i = skipString(text, i, ch);\n continue;\n }\n\n // Skip single-line comments.\n // Note: This may misidentify regex literals containing `//` (e.g.,\n // `/api\\//`). Config files — the primary use case — do not contain\n // regex literals, so this trade-off is acceptable.\n if (ch === \"/\" && text[i + 1] === \"/\") {\n const newline = text.indexOf(\"\\n\", i);\n if (newline === -1) {\n return -1;\n }\n i = newline + 1;\n continue;\n }\n\n // Skip block comments\n if (ch === \"/\" && text[i + 1] === \"*\") {\n const end = text.indexOf(\"*/\", i + 2);\n if (end === -1) {\n return -1;\n }\n i = end + 2;\n continue;\n }\n\n if (ch === openChar) {\n depth++;\n } else if (ch === closeChar) {\n depth--;\n if (depth === 0) {\n return i;\n }\n }\n i++;\n }\n return -1;\n}\n\n/**\n * Finds the matching closing parenthesis for an opening paren at the given\n * position, accounting for nested parentheses and skipping delimiters inside\n * string literals and comments.\n *\n * @param text - The source text to search.\n * @param openPos - The index of the opening `(`.\n * @returns The index of the matching `)`, or -1 if not found.\n * @internal Exported for unit testing only.\n */\nexport function findMatchingParen(text: string, openPos: number): number {\n return findMatchingDelimiter(text, openPos, \"(\", \")\");\n}\n\n/**\n * Removes the `withGlasstraceConfig(...)` wrapper from an ESM default export,\n * restoring the inner expression.\n *\n * Before: `export default withGlasstraceConfig(innerExpr);`\n * After: `export default innerExpr;`\n *\n * @internal Exported for unit testing only.\n */\nexport function unwrapExport(content: string): { content: string; unwrapped: boolean } {\n const pattern = /export\\s+default\\s+withGlasstraceConfig\\s*\\(/;\n const match = pattern.exec(content);\n if (!match) {\n return { content, unwrapped: false };\n }\n\n // Find the opening paren of withGlasstraceConfig(\n const openParenIdx = match.index + match[0].length - 1;\n const closeParenIdx = findMatchingParen(content, openParenIdx);\n if (closeParenIdx === -1) {\n return { content, unwrapped: false };\n }\n\n const innerExpr = content.slice(openParenIdx + 1, closeParenIdx).trim();\n if (innerExpr.length === 0) {\n return { content, unwrapped: false };\n }\n\n // Everything before `export default ...`\n const before = content.slice(0, match.index);\n // Everything after the closing `)` (skip optional semicolon and trailing whitespace)\n const afterClose = content.slice(closeParenIdx + 1);\n const trailing = afterClose.replace(/^;?\\s*/, \"\");\n\n const result = before + `export default ${innerExpr};\\n` + trailing;\n\n return { content: result, unwrapped: true };\n}\n\n/**\n * Removes the `withGlasstraceConfig(...)` wrapper from a CJS module.exports,\n * restoring the inner expression.\n *\n * Before: `module.exports = withGlasstraceConfig(innerExpr);`\n * After: `module.exports = innerExpr;`\n *\n * @internal Exported for unit testing only.\n */\nexport function unwrapCJSExport(content: string): { content: string; unwrapped: boolean } {\n const pattern = /module\\.exports\\s*=\\s*withGlasstraceConfig\\s*\\(/;\n const match = pattern.exec(content);\n if (!match) {\n return { content, unwrapped: false };\n }\n\n const openParenIdx = match.index + match[0].length - 1;\n const closeParenIdx = findMatchingParen(content, openParenIdx);\n if (closeParenIdx === -1) {\n return { content, unwrapped: false };\n }\n\n const innerExpr = content.slice(openParenIdx + 1, closeParenIdx).trim();\n if (innerExpr.length === 0) {\n return { content, unwrapped: false };\n }\n\n const before = content.slice(0, match.index);\n const afterClose = content.slice(closeParenIdx + 1);\n const trailing = afterClose.replace(/^;?\\s*/, \"\");\n\n const result = before + `module.exports = ${innerExpr};\\n` + trailing;\n\n return { content: result, unwrapped: true };\n}\n\n/**\n * Removes the `import { withGlasstraceConfig } from \"@glasstrace/sdk\"` line\n * from file content. If `withGlasstraceConfig` is the only imported specifier,\n * the entire import line is removed. If other specifiers exist, only\n * `withGlasstraceConfig` is removed from the specifier list.\n *\n * @internal Exported for unit testing only.\n */\nexport function removeGlasstraceConfigImport(content: string): string {\n // ESM: import { withGlasstraceConfig } from \"@glasstrace/sdk\"\n const esmSoleImport =\n /import\\s*\\{\\s*withGlasstraceConfig\\s*\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']\\s*;?\\s*\\n?/;\n if (esmSoleImport.test(content)) {\n return content.replace(esmSoleImport, \"\");\n }\n\n // ESM with multiple specifiers — remove withGlasstraceConfig from the list\n const esmMultiImport =\n /import\\s*\\{([^}]*)\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']/;\n const multiMatch = esmMultiImport.exec(content);\n if (multiMatch) {\n const specifiers = multiMatch[1]\n .split(\",\")\n .map((s) => s.trim())\n .filter((s) => s !== \"\" && s !== \"withGlasstraceConfig\");\n if (specifiers.length === 0) {\n // All specifiers were withGlasstraceConfig — remove entire import\n return content.replace(\n /import\\s*\\{[^}]*\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']\\s*;?\\s*\\n?/,\n \"\",\n );\n }\n const newImport = `import { ${specifiers.join(\", \")} } from \"@glasstrace/sdk\"`;\n return content.replace(multiMatch[0], newImport);\n }\n\n // CJS: const { withGlasstraceConfig } = require(\"@glasstrace/sdk\")\n const cjsSoleRequire =\n /const\\s*\\{\\s*withGlasstraceConfig\\s*\\}\\s*=\\s*require\\s*\\(\\s*[\"']@glasstrace\\/sdk[\"']\\s*\\)\\s*;?\\s*\\n?/;\n if (cjsSoleRequire.test(content)) {\n return content.replace(cjsSoleRequire, \"\");\n }\n\n // CJS with multiple specifiers\n const cjsMultiRequire =\n /const\\s*\\{([^}]*)\\}\\s*=\\s*require\\s*\\(\\s*[\"']@glasstrace\\/sdk[\"']\\s*\\)/;\n const cjsMultiMatch = cjsMultiRequire.exec(content);\n if (cjsMultiMatch) {\n const specifiers = cjsMultiMatch[1]\n .split(\",\")\n .map((s) => s.trim())\n .filter((s) => s !== \"\" && s !== \"withGlasstraceConfig\");\n if (specifiers.length === 0) {\n return content.replace(\n /const\\s*\\{[^}]*\\}\\s*=\\s*require\\s*\\(\\s*[\"']@glasstrace\\/sdk[\"']\\s*\\)\\s*;?\\s*\\n?/,\n \"\",\n );\n }\n const newRequire = `const { ${specifiers.join(\", \")} } = require(\"@glasstrace/sdk\")`;\n return content.replace(cjsMultiMatch[0], newRequire);\n }\n\n return content;\n}\n\n/**\n * Removes blank lines that appear consecutively (more than one empty line\n * in a row) at the top of a file, which can occur after removing import lines.\n */\nfunction cleanLeadingBlankLines(content: string): string {\n return content.replace(/^\\n{2,}/, \"\\n\");\n}\n\n/**\n * Determines whether an instrumentation.ts file was created by `glasstrace init`\n * (i.e., contains only the standard template with no user-added code).\n *\n * A file is considered init-created if:\n * - The only import from any package is `@glasstrace/sdk`\n * - The only meaningful statement in `register()` is `registerGlasstrace()`\n * - There are no other top-level statements, exports, or declarations outside\n * the register function (prevents deleting files where users added their own code)\n *\n * @internal Exported for unit testing only.\n */\nexport function isInitCreatedInstrumentation(content: string): boolean {\n const lines = content.split(\"\\n\");\n\n // Check that all imports are from @glasstrace/sdk\n const importLines = lines.filter(\n (l) => /^\\s*import\\s/.test(l) && !l.trim().startsWith(\"//\"),\n );\n const nonGlasstraceImports = importLines.filter(\n (l) => !l.includes(\"@glasstrace/sdk\"),\n );\n if (nonGlasstraceImports.length > 0) {\n return false;\n }\n\n // Check that the register() function body only contains registerGlasstrace()\n // and comments — no other meaningful statements\n const registerFnRegex = /export\\s+(?:async\\s+)?function\\s+register\\s*\\([^)]*\\)\\s*\\{/;\n const match = registerFnRegex.exec(content);\n if (!match) {\n // No register function — not a standard init template\n return false;\n }\n\n // Extract the function body\n const afterBrace = content.slice(match.index + match[0].length);\n const closingBraceIdx = findMatchingBrace(content, match.index + match[0].length - 1);\n if (closingBraceIdx === -1) {\n return false;\n }\n\n const body = afterBrace.slice(0, closingBraceIdx - (match.index + match[0].length));\n const bodyLines = body.split(\"\\n\");\n\n // Filter out comments and blank lines — only meaningful statements remain\n const statements = bodyLines.filter((l) => {\n const trimmed = l.trim();\n return trimmed !== \"\" && !trimmed.startsWith(\"//\");\n });\n\n // The only statement should be registerGlasstrace()\n if (statements.length !== 1) {\n return false;\n }\n if (!/^\\s*registerGlasstrace\\s*\\(\\s*\\)\\s*;?\\s*$/.test(statements[0])) {\n return false;\n }\n\n // Verify no other top-level code exists outside imports and the register function.\n // Extract everything that isn't an import line or inside the register() function.\n const beforeFn = content.slice(0, match.index);\n const afterFn = content.slice(closingBraceIdx + 1);\n\n const topLevelBefore = beforeFn.split(\"\\n\").filter((l) => {\n const trimmed = l.trim();\n return (\n trimmed !== \"\" &&\n !trimmed.startsWith(\"//\") &&\n !trimmed.startsWith(\"import \") &&\n !trimmed.startsWith(\"import{\")\n );\n });\n\n const topLevelAfter = afterFn.split(\"\\n\").filter((l) => {\n const trimmed = l.trim();\n return trimmed !== \"\" && !trimmed.startsWith(\"//\");\n });\n\n return topLevelBefore.length === 0 && topLevelAfter.length === 0;\n}\n\n/**\n * Finds the matching closing brace for an opening brace at the given position,\n * skipping delimiters inside string literals and comments.\n */\nfunction findMatchingBrace(text: string, openPos: number): number {\n return findMatchingDelimiter(text, openPos, \"{\", \"}\");\n}\n\n/**\n * Removes the `registerGlasstrace()` call and its `@glasstrace/sdk` import\n * from an instrumentation.ts file, preserving all other code.\n *\n * @internal Exported for unit testing only.\n */\nexport function removeRegisterGlasstrace(content: string): string {\n let result = content;\n\n // Remove all comment-block + registerGlasstrace() call pairs.\n // The init template creates a multi-line comment block before the call:\n // // Glasstrace must be registered before Prisma instrumentation\n // // to ensure all ORM spans are captured correctly.\n // // If you use @prisma/instrumentation, import it after this call.\n // registerGlasstrace();\n // Use global flag to handle multiple occurrences.\n result = result.replace(\n /[ \\t]*\\/\\/\\s*Glasstrace must be registered[^\\n]*\\n(?:[ \\t]*\\/\\/[^\\n]*\\n)*[ \\t]*registerGlasstrace\\s*\\(\\s*\\)\\s*;?\\s*\\n?/g,\n \"\",\n );\n\n // Remove any remaining standalone registerGlasstrace() calls (global)\n result = result.replace(\n /[ \\t]*registerGlasstrace\\s*\\(\\s*\\)\\s*;?\\s*\\n?/g,\n \"\",\n );\n\n // Remove the import line for registerGlasstrace from @glasstrace/sdk\n // If it's the sole import, remove the whole line\n const soleImportPattern =\n /import\\s*\\{\\s*registerGlasstrace\\s*\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']\\s*;?\\s*\\n?/;\n if (soleImportPattern.test(result)) {\n result = result.replace(soleImportPattern, \"\");\n } else {\n // Multiple specifiers — remove only registerGlasstrace\n const multiImportPattern =\n /import\\s*\\{([^}]*)\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']/;\n const multiMatch = multiImportPattern.exec(result);\n if (multiMatch) {\n const specifiers = multiMatch[1]\n .split(\",\")\n .map((s) => s.trim())\n .filter((s) => s !== \"\" && s !== \"registerGlasstrace\");\n if (specifiers.length === 0) {\n result = result.replace(\n /import\\s*\\{[^}]*\\}\\s*from\\s*[\"']@glasstrace\\/sdk[\"']\\s*;?\\s*\\n?/,\n \"\",\n );\n } else {\n const newImport = `import { ${specifiers.join(\", \")} } from \"@glasstrace/sdk\"`;\n result = result.replace(multiMatch[0], newImport);\n }\n }\n }\n\n return cleanLeadingBlankLines(result);\n}\n\n/**\n * Removes content between glasstrace marker comments from a file.\n * Supports both HTML markers (`<!-- glasstrace:mcp:start/end -->`) and\n * hash markers (`# glasstrace:mcp:start/end`).\n *\n * @internal Exported for unit testing only.\n */\nexport function removeMarkerSection(content: string): { content: string; removed: boolean } {\n const lines = content.split(\"\\n\");\n let startIdx = -1;\n let endIdx = -1;\n\n for (let i = 0; i < lines.length; i++) {\n const trimmed = lines[i].trim();\n if (\n trimmed === \"<!-- glasstrace:mcp:start -->\" ||\n trimmed === \"# glasstrace:mcp:start\"\n ) {\n startIdx = i;\n } else if (\n (trimmed === \"<!-- glasstrace:mcp:end -->\" ||\n trimmed === \"# glasstrace:mcp:end\") &&\n startIdx !== -1\n ) {\n endIdx = i;\n break;\n }\n }\n\n if (startIdx === -1 || endIdx === -1) {\n return { content, removed: false };\n }\n\n const before = lines.slice(0, startIdx);\n const after = lines.slice(endIdx + 1);\n\n // Remove trailing blank line that may have preceded the marker block\n while (before.length > 0 && before[before.length - 1].trim() === \"\") {\n before.pop();\n }\n\n const result = [...before, ...after].join(\"\\n\");\n // Ensure file ends with newline if it has content\n const trimmedResult = result.trimEnd();\n return {\n content: trimmedResult.length > 0 ? trimmedResult + \"\\n\" : \"\",\n removed: true,\n };\n}\n\n/**\n * Removes the `glasstrace` key from an MCP config JSON file's `mcpServers`\n * object. Only deletes the file when `mcpServers` is the sole top-level key\n * and `glasstrace` is the only server entry. When other top-level keys exist\n * (e.g., `$schema`, metadata), the `mcpServers` key is removed (if empty)\n * and the file is preserved.\n *\n * @returns `\"removed-key\"` if the key was removed (other data remains),\n * `\"deleted\"` if the file should be deleted (no other data),\n * or `\"skipped\"` if no glasstrace config was found.\n * @internal Exported for unit testing only.\n */\nexport function processJsonMcpConfig(content: string): {\n action: \"removed-key\" | \"deleted\" | \"skipped\";\n content?: string;\n} {\n let parsed: Record<string, unknown>;\n try {\n parsed = JSON.parse(content) as Record<string, unknown>;\n } catch {\n return { action: \"skipped\" };\n }\n\n const mcpServers = parsed[\"mcpServers\"] as Record<string, unknown> | undefined;\n if (!mcpServers || typeof mcpServers !== \"object\" || !(\"glasstrace\" in mcpServers)) {\n return { action: \"skipped\" };\n }\n\n const remainingServers = Object.keys(mcpServers).filter((k) => k !== \"glasstrace\");\n const otherTopLevelKeys = Object.keys(parsed).filter((k) => k !== \"mcpServers\");\n\n if (remainingServers.length === 0 && otherTopLevelKeys.length === 0) {\n // mcpServers.glasstrace is the only data in the file — safe to delete\n return { action: \"deleted\" };\n }\n\n // Remove the glasstrace key, keep other servers\n const { glasstrace: _, ...rest } = mcpServers;\n // Suppress unused variable lint — the destructuring intentionally discards glasstrace\n void _;\n\n if (remainingServers.length > 0) {\n // Other servers remain — keep mcpServers with glasstrace removed\n parsed[\"mcpServers\"] = rest;\n } else {\n // No servers remain but other top-level keys exist — remove mcpServers entirely\n delete parsed[\"mcpServers\"];\n }\n\n return { action: \"removed-key\", content: JSON.stringify(parsed, null, 2) + \"\\n\" };\n}\n\n/**\n * Removes the `[mcp_servers.glasstrace]` section from a TOML config file.\n * Since TOML parsing without a dependency is complex, this uses a line-based\n * approach that handles the standard format written by init.\n *\n * @returns `\"removed-section\"` if the glasstrace section was removed,\n * `\"deleted\"` if the entire file should be deleted (only contained\n * glasstrace config), or `\"skipped\"` if no glasstrace config found.\n * @internal Exported for unit testing only.\n */\nexport function processTomlMcpConfig(content: string): {\n action: \"removed-section\" | \"deleted\" | \"skipped\";\n content?: string;\n} {\n if (!content.includes(\"[mcp_servers.glasstrace]\")) {\n return { action: \"skipped\" };\n }\n\n const lines = content.split(\"\\n\");\n const startIdx = lines.findIndex(\n (l) => l.trim() === \"[mcp_servers.glasstrace]\",\n );\n if (startIdx === -1) {\n return { action: \"skipped\" };\n }\n\n // Find the end of the glasstrace section: next section header or end of file\n let endIdx = lines.length;\n for (let i = startIdx + 1; i < lines.length; i++) {\n if (/^\\s*\\[/.test(lines[i])) {\n endIdx = i;\n break;\n }\n }\n\n // Remove the section and any trailing blank lines\n const before = lines.slice(0, startIdx);\n const after = lines.slice(endIdx);\n\n // Trim trailing blank lines from the before section\n while (before.length > 0 && before[before.length - 1].trim() === \"\") {\n before.pop();\n }\n\n const result = [...before, ...after].join(\"\\n\").trimEnd();\n\n // Check if there are any remaining sections\n if (result.trim().length === 0) {\n return { action: \"deleted\" };\n }\n\n return { action: \"removed-section\", content: result + \"\\n\" };\n}\n\n/**\n * Writes the `.glasstrace/shutdown-requested` marker file atomically so\n * that a running SDK heartbeat tick (or equivalent lifecycle hook) can\n * detect that uninit has been invoked and trigger shutdown (DISC-1247\n * Scenario 1).\n *\n * Uses write-temp + rename semantics so a mid-write crash cannot leave\n * a truncated marker that the running process might misread.\n *\n * Best-effort: if `.glasstrace/` does not exist or the write fails, the\n * marker is silently skipped — uninit's cleanup is not blocked by a\n * missing running process.\n *\n * @internal Exported for unit testing only.\n */\nexport function writeShutdownMarker(projectRoot: string): boolean {\n const dirPath = path.join(projectRoot, \".glasstrace\");\n if (!fs.existsSync(dirPath)) {\n // No .glasstrace/ directory means no running SDK state is tracked —\n // nothing to signal. The filesystem removal step will handle any\n // stray artifacts.\n return false;\n }\n const markerPath = path.join(dirPath, \"shutdown-requested\");\n const body = JSON.stringify({ requestedAt: new Date().toISOString() });\n try {\n // Atomic write per SDK 2.0 §4.3: tmp + fsync(tmp) + rename +\n // fsync(parent). The helper handles tmp-file cleanup on failure\n // and swallows directory-fsync errors on platforms that do not\n // support it (e.g., Windows).\n atomicWriteFileSync(markerPath, body, { encoding: \"utf-8\", mode: 0o600 });\n return true;\n } catch {\n // Marker write was best-effort to begin with; swallow errors so\n // uninit itself never fails because of a signal-side-channel\n // write.\n return false;\n }\n}\n\n/**\n * Simple TTY prompt used when `UninitOptions.prompt` is not provided.\n * Returns `defaultValue` when stdin is not a TTY.\n */\nasync function defaultPrompt(question: string, defaultValue: boolean): Promise<boolean> {\n if (!process.stdin.isTTY) return defaultValue;\n const readline = await import(\"node:readline\");\n const rl = readline.createInterface({\n input: process.stdin,\n output: process.stdout,\n });\n return new Promise<boolean>((resolve) => {\n const suffix = defaultValue ? \" [Y/n] \" : \" [y/N] \";\n rl.question(question + suffix, (answer) => {\n rl.close();\n const trimmed = answer.trim().toLowerCase();\n if (trimmed === \"\") {\n resolve(defaultValue);\n return;\n }\n resolve(trimmed === \"y\" || trimmed === \"yes\");\n });\n });\n}\n\n/**\n * Reverses every step of `glasstrace init`, cleanly removing all SDK artifacts\n * from a project.\n *\n * Steps (in order):\n * 1. Write `.glasstrace/shutdown-requested` marker so a running SDK can\n * drain and exit cleanly (DISC-1247 Scenario 1)\n * 2. Unwrap `withGlasstraceConfig` from next.config\n * 3. Remove `registerGlasstrace` from instrumentation.ts (or delete if init-created)\n * 4. Remove `.glasstrace/` directory\n * 4a. Remove `<staticRoot>/.well-known/glasstrace.json` (and prune the\n * enclosing `.well-known/` directory when empty)\n * 5. Remove `GLASSTRACE_*` entries from `.env.local` (with dev-key confirmation)\n * 6. Remove `.glasstrace/` from `.gitignore`\n * 7. Remove MCP config entries\n * 8. Remove info sections from agent files\n *\n * @param options - Configuration for the uninit command.\n * @returns A structured result describing what actions were taken.\n */\nexport async function runUninit(options: UninitOptions): Promise<UninitResult> {\n const { projectRoot, dryRun } = options;\n const force = options.force === true;\n const prompt = options.prompt ?? defaultPrompt;\n const summary: string[] = [];\n const warnings: string[] = [];\n const errors: string[] = [];\n const prefix = dryRun ? \"[dry run] \" : \"\";\n\n // Step 0: Signal any running SDK to shut down via a marker file.\n // Placed first so the running process has maximum time to observe\n // the marker while the remaining cleanup steps execute.\n try {\n if (!dryRun) {\n const markerWritten = writeShutdownMarker(projectRoot);\n if (markerWritten) {\n summary.push(\"Wrote .glasstrace/shutdown-requested marker\");\n }\n } else {\n const dirPath = path.join(projectRoot, \".glasstrace\");\n if (fs.existsSync(dirPath)) {\n summary.push(`${prefix}Would write .glasstrace/shutdown-requested marker`);\n }\n }\n } catch (err) {\n // Marker is best-effort; failure is not an error for uninit.\n warnings.push(\n `Shutdown marker write failed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 1: Unwrap withGlasstraceConfig from next.config\n try {\n let configHandled = false;\n for (const name of NEXT_CONFIG_NAMES) {\n const configPath = path.join(projectRoot, name);\n if (!fs.existsSync(configPath)) {\n continue;\n }\n\n const content = fs.readFileSync(configPath, \"utf-8\");\n if (!content.includes(\"withGlasstraceConfig\")) {\n continue;\n }\n\n const isESM = name.endsWith(\".ts\") || name.endsWith(\".mjs\");\n const unwrapResult = isESM\n ? unwrapExport(content)\n : unwrapCJSExport(content);\n\n if (unwrapResult.unwrapped) {\n const cleaned = removeGlasstraceConfigImport(unwrapResult.content);\n const final = cleanLeadingBlankLines(cleaned);\n if (!dryRun) {\n fs.writeFileSync(configPath, final, \"utf-8\");\n }\n summary.push(`${prefix}Unwrapped withGlasstraceConfig from ${name}`);\n configHandled = true;\n break;\n } else {\n warnings.push(\n `${name} contains withGlasstraceConfig but could not be automatically unwrapped. ` +\n \"Please remove withGlasstraceConfig() manually.\",\n );\n configHandled = true;\n break;\n }\n }\n if (!configHandled) {\n // No next.config with withGlasstraceConfig found — nothing to do\n }\n } catch (err) {\n errors.push(\n `Failed to process next.config: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 2: Remove registerGlasstrace from instrumentation.ts\n try {\n const instrPath = path.join(projectRoot, \"instrumentation.ts\");\n if (fs.existsSync(instrPath)) {\n const content = fs.readFileSync(instrPath, \"utf-8\");\n if (content.includes(\"registerGlasstrace\") || content.includes(\"@glasstrace/sdk\")) {\n if (isInitCreatedInstrumentation(content)) {\n if (!dryRun) {\n fs.unlinkSync(instrPath);\n }\n summary.push(`${prefix}Deleted instrumentation.ts (init-created)`);\n } else {\n const cleaned = removeRegisterGlasstrace(content);\n if (cleaned !== content) {\n if (!dryRun) {\n fs.writeFileSync(instrPath, cleaned, \"utf-8\");\n }\n summary.push(\n `${prefix}Removed registerGlasstrace() from instrumentation.ts`,\n );\n }\n }\n }\n }\n } catch (err) {\n errors.push(\n `Failed to process instrumentation.ts: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 3: Remove .glasstrace/ directory\n try {\n const glasstraceDir = path.join(projectRoot, \".glasstrace\");\n if (fs.existsSync(glasstraceDir)) {\n if (!dryRun) {\n fs.rmSync(glasstraceDir, { recursive: true, force: true });\n }\n summary.push(`${prefix}Removed .glasstrace/ directory`);\n }\n } catch (err) {\n errors.push(\n `Failed to remove .glasstrace/: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 3a: Remove the static discovery file at\n // `<staticRoot>/.well-known/glasstrace.json` and, when empty, the\n // enclosing `.well-known/` directory. Sibling files (e.g. a user's\n // own `security.txt`) are never touched.\n try {\n if (dryRun) {\n // Dry-run preview: simulate the removal by checking existence only.\n // `removeDiscoveryFile` is a destructive helper, so the preview path\n // replicates the existence check inline rather than invoking it.\n // This keeps dry-run accurate even if the helper is changed later.\n // Mirrors the real sweep by checking BOTH candidate layouts so an\n // orphaned file in the non-inferred directory still shows up in\n // the preview (heuristic-drift scenario from the Codex re-review).\n for (const previewLayout of [\"public\", \"static\"] as const) {\n const relPath = relativeDiscoveryPath(previewLayout);\n const absPath = path.join(projectRoot, relPath);\n if (fs.existsSync(absPath)) {\n summary.push(`${prefix}Would remove ${relPath}`);\n }\n }\n } else {\n const result = removeDiscoveryFile(projectRoot);\n if (result.action === \"removed\") {\n const relPath = relativeDiscoveryPath(result.layout);\n summary.push(`Removed ${relPath}`);\n if (result.directoryRemoved) {\n const dirRel = relPath.replace(/\\/glasstrace\\.json$/, \"/\");\n summary.push(`Removed empty ${dirRel}`);\n }\n } else if (result.action === \"failed\") {\n warnings.push(\n `Failed to remove ${relativeDiscoveryPath(result.layout)}${\n result.error !== undefined ? `: ${result.error}` : \"\"\n }`,\n );\n }\n }\n } catch (err) {\n warnings.push(\n `Failed to remove discovery file: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 4: Remove GLASSTRACE entries from .env.local\n // DISC-1247 Scenario 6: if the file contains a claimed developer key\n // (`gt_dev_*`), require explicit confirmation before removing it so\n // users don't silently lose authentication state during uninit.\n // `--force` bypasses the prompt.\n try {\n const envPath = path.join(projectRoot, \".env.local\");\n if (fs.existsSync(envPath)) {\n const content = fs.readFileSync(envPath, \"utf-8\");\n const existingKey = readEnvLocalApiKey(content);\n const hasDevKey = isDevApiKey(existingKey);\n\n // Track how the dev-key path is resolved so the summary reflects\n // what actually happened: prompt-confirmed, force-bypassed, or\n // preview-only. Using the literal \"(dev key confirmed)\" for all\n // three paths was misleading (Copilot review).\n let proceed = true;\n let devKeyPath: \"interactive-confirmed\" | \"force-bypass\" | \"dry-run-preview\" | \"none\" = \"none\";\n if (hasDevKey) {\n if (dryRun) {\n devKeyPath = \"dry-run-preview\";\n } else if (force) {\n devKeyPath = \"force-bypass\";\n } else {\n const confirmed = await prompt(\n \".env.local contains a claimed Glasstrace developer API key (gt_dev_...). \" +\n \"Removing it will require you to re-authenticate. Continue?\",\n false,\n );\n proceed = confirmed;\n if (confirmed) devKeyPath = \"interactive-confirmed\";\n }\n }\n\n if (!proceed) {\n warnings.push(\n \"Preserved GLASSTRACE_API_KEY in .env.local (claimed dev key; re-run with --force to remove)\",\n );\n } else {\n const lines = content.split(\"\\n\");\n const filtered = lines.filter((line) => {\n const trimmed = line.trim();\n // Match both commented and uncommented GLASSTRACE_ lines\n return !(\n /^\\s*#?\\s*GLASSTRACE_API_KEY\\s*=/.test(trimmed) ||\n /^\\s*#?\\s*GLASSTRACE_COVERAGE_MAP\\s*=/.test(trimmed)\n );\n });\n\n if (filtered.length !== lines.length) {\n const result = filtered.join(\"\\n\");\n // If the file is now empty (only newlines), don't write it\n if (result.trim().length === 0) {\n if (!dryRun) {\n fs.unlinkSync(envPath);\n }\n summary.push(`${prefix}Deleted .env.local (no remaining entries)`);\n } else {\n if (!dryRun) {\n fs.writeFileSync(envPath, result, \"utf-8\");\n }\n let devKeyAnnotation = \"\";\n if (devKeyPath === \"interactive-confirmed\") {\n devKeyAnnotation = \" (dev key confirmed)\";\n } else if (devKeyPath === \"force-bypass\") {\n devKeyAnnotation = \" (dev key removed via --force)\";\n } else if (devKeyPath === \"dry-run-preview\") {\n devKeyAnnotation =\n \" (dev key would be removed; real run would require confirmation)\";\n }\n summary.push(\n `${prefix}Removed GLASSTRACE entries from .env.local${devKeyAnnotation}`,\n );\n }\n }\n }\n }\n } catch (err) {\n errors.push(\n `Failed to process .env.local: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 5: Remove .glasstrace/ from .gitignore\n try {\n const gitignorePath = path.join(projectRoot, \".gitignore\");\n if (fs.existsSync(gitignorePath)) {\n const content = fs.readFileSync(gitignorePath, \"utf-8\");\n const lines = content.split(\"\\n\");\n\n // Remove lines that are exactly \".glasstrace/\" or MCP config file entries\n // added by init (e.g., \".mcp.json\", \".cursor/mcp.json\", \".gemini/settings.json\",\n // \".codex/config.toml\")\n const mcpGitignoreEntries = new Set([\n \".glasstrace/\",\n \".mcp.json\",\n \".cursor/mcp.json\",\n \".gemini/settings.json\",\n \".codex/config.toml\",\n ]);\n\n const filtered = lines.filter(\n (line) => !mcpGitignoreEntries.has(line.trim()),\n );\n\n if (filtered.length !== lines.length) {\n const result = filtered.join(\"\\n\");\n if (result.trim().length === 0) {\n if (!dryRun) {\n fs.unlinkSync(gitignorePath);\n }\n summary.push(`${prefix}Deleted .gitignore (no remaining entries)`);\n } else {\n if (!dryRun) {\n fs.writeFileSync(gitignorePath, result, \"utf-8\");\n }\n summary.push(`${prefix}Removed Glasstrace entries from .gitignore`);\n }\n }\n }\n } catch (err) {\n errors.push(\n `Failed to process .gitignore: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 6: Remove MCP config entries\n try {\n for (const configFile of MCP_CONFIG_FILES) {\n const configPath = path.join(projectRoot, configFile);\n if (!fs.existsSync(configPath)) {\n continue;\n }\n\n const content = fs.readFileSync(configPath, \"utf-8\");\n const result = processJsonMcpConfig(content);\n\n if (result.action === \"deleted\") {\n if (!dryRun) {\n fs.unlinkSync(configPath);\n }\n summary.push(`${prefix}Deleted ${configFile}`);\n } else if (result.action === \"removed-key\" && result.content !== undefined) {\n if (!dryRun) {\n fs.writeFileSync(configPath, result.content, \"utf-8\");\n }\n summary.push(`${prefix}Removed glasstrace from ${configFile}`);\n }\n }\n // Handle Codex TOML config separately\n const codexConfigPath = path.join(projectRoot, \".codex\", \"config.toml\");\n if (fs.existsSync(codexConfigPath)) {\n const content = fs.readFileSync(codexConfigPath, \"utf-8\");\n const tomlResult = processTomlMcpConfig(content);\n\n if (tomlResult.action === \"deleted\") {\n if (!dryRun) {\n fs.unlinkSync(codexConfigPath);\n }\n summary.push(`${prefix}Deleted .codex/config.toml`);\n } else if (tomlResult.action === \"removed-section\" && tomlResult.content !== undefined) {\n if (!dryRun) {\n fs.writeFileSync(codexConfigPath, tomlResult.content, \"utf-8\");\n }\n summary.push(`${prefix}Removed glasstrace from .codex/config.toml`);\n }\n }\n\n // Handle Windsurf global config at ~/.codeium/windsurf/mcp_config.json\n // Only process if the project has Windsurf markers, to avoid touching\n // global config for non-Windsurf projects\n const hasWindsurfMarkers =\n fs.existsSync(path.join(projectRoot, \".windsurfrules\")) ||\n fs.existsSync(path.join(projectRoot, \".windsurf\"));\n if (hasWindsurfMarkers) {\n const windsurfConfigPath = path.join(\n os.homedir(),\n \".codeium\",\n \"windsurf\",\n \"mcp_config.json\",\n );\n if (fs.existsSync(windsurfConfigPath)) {\n const content = fs.readFileSync(windsurfConfigPath, \"utf-8\");\n const windsurfResult = processJsonMcpConfig(content);\n\n // Display the path with ~ for the home directory to keep output\n // readable, but derive it from the actual path for accuracy.\n const home = os.homedir();\n const displayPath = windsurfConfigPath.startsWith(home)\n ? \"~\" + windsurfConfigPath.slice(home.length)\n : windsurfConfigPath;\n\n if (windsurfResult.action === \"deleted\") {\n if (!dryRun) {\n fs.unlinkSync(windsurfConfigPath);\n }\n summary.push(\n `${prefix}Deleted global Windsurf config (${displayPath})`,\n );\n } else if (\n windsurfResult.action === \"removed-key\" &&\n windsurfResult.content !== undefined\n ) {\n if (!dryRun) {\n fs.writeFileSync(windsurfConfigPath, windsurfResult.content, \"utf-8\");\n }\n summary.push(\n `${prefix}Removed glasstrace from global Windsurf config (${displayPath})`,\n );\n }\n }\n }\n } catch (err) {\n errors.push(\n `Failed to process MCP config: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n // Step 7: Remove info sections from agent files\n try {\n for (const infoFile of AGENT_INFO_FILES) {\n const filePath = path.join(projectRoot, infoFile);\n if (!fs.existsSync(filePath)) {\n continue;\n }\n\n const content = fs.readFileSync(filePath, \"utf-8\");\n const result = removeMarkerSection(content);\n\n if (result.removed) {\n if (result.content.trim().length === 0) {\n // File is now empty after removing the marker section —\n // only delete if the file was solely glasstrace content\n if (!dryRun) {\n fs.unlinkSync(filePath);\n }\n summary.push(`${prefix}Deleted ${infoFile} (only contained Glasstrace section)`);\n } else {\n if (!dryRun) {\n fs.writeFileSync(filePath, result.content, \"utf-8\");\n }\n summary.push(`${prefix}Removed Glasstrace section from ${infoFile}`);\n }\n }\n }\n } catch (err) {\n errors.push(\n `Failed to process agent info files: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n\n if (summary.length === 0 && errors.length === 0) {\n summary.push(\"No Glasstrace artifacts found — nothing to do.\");\n }\n\n return { exitCode: errors.length > 0 ? 1 : 0, summary, warnings, errors };\n}\n","import type { DetectedAgent } from \"../agent-detection/detect.js\";\n\n/** Next.js config file names in priority order. */\nexport const NEXT_CONFIG_NAMES = [\"next.config.ts\", \"next.config.js\", \"next.config.mjs\"] as const;\n\n/** Maps internal agent name to a human-readable display name. */\nexport function formatAgentName(name: DetectedAgent[\"name\"]): string {\n const displayNames: Record<DetectedAgent[\"name\"], string> = {\n claude: \"Claude Code\",\n codex: \"Codex\",\n gemini: \"Gemini\",\n cursor: \"Cursor\",\n windsurf: \"Windsurf\",\n generic: \"Generic helper\",\n };\n return displayNames[name];\n}\n","/**\n * Atomic file-write helper.\n *\n * Implements the durability half of the atomic-write protocol\n * (`docs/component-designs/sdk-architecture.md` §4.3 — Atomic file\n * writes; durability protocol steps 6–9):\n *\n * 1. Write the payload to a sibling temp file in the **same**\n * directory as the final target. The shared directory guarantees\n * `rename(2)` stays on the same filesystem and therefore atomic\n * per POSIX semantics.\n * 2. **fsync the temp file.** Forces data and metadata to durable\n * storage before the rename is observable.\n * 3. **rename atomically into place.** Readers see either the old\n * file contents or the new ones, never a partial write.\n * 4. **fsync the parent directory.** On POSIX, `rename(2)` durability\n * is not guaranteed until the containing directory's own metadata\n * is synced. Without this step, a power loss between rename and\n * parent-dir sync can leave the rename invisible after reboot\n * (the kernel acknowledges the syscall but the metadata never\n * reached durable storage).\n *\n * Closes the durability gap that allowed DISC-494 (anon-key unlinked\n * silently on re-init under crash interleavings).\n *\n * Out-of-scope by design:\n * - The `lstat → tmp → rename → re-lstat` TOCTOU re-check (spec\n * §4.3 steps 1–2 and 7's re-verification) is next-major scope per\n * `sdk-architecture.md` §4.3 — TOCTOU protection.\n * - The `GLASSTRACE_TEST_CRASH_AFTER` crash-injection harness is\n * next-major scope per `sdk-architecture.md` §4.3 — Crash-injection\n * harness.\n * - Structured error-with-step-number reporting is next-major scope\n * per `sdk-architecture.md` §4.3 — durability protocol step 9.\n *\n * Cross-platform behavior:\n * - On POSIX (Linux, macOS), the parent-directory fsync uses an\n * `open(O_RDONLY) → fsync → close` sequence. This is the canonical\n * way to flush directory metadata.\n * - On Windows, opening a directory for read returns `EISDIR` (and\n * `fsync` on the resulting handle would fail with `EINVAL` even\n * if the open succeeded). NTFS's rename semantics also do not\n * require an explicit directory fsync to commit the rename\n * metadata. The helper therefore swallows `EISDIR`, `EINVAL`,\n * `EPERM`, and `ENOTSUP` from the parent-dir fsync step. Any\n * other error from the open/fsync/close sequence still propagates\n * so genuine I/O failures are not silently ignored.\n *\n * Concurrency:\n * - Two processes writing the same target concurrently follow\n * last-rename-wins semantics. The helper does not lock; the\n * caller is responsible for any external mutual-exclusion. This\n * matches the existing 0.19.x behavior of every migrated call\n * site — the helper does not change concurrency guarantees, only\n * durability.\n *\n * Performance:\n * - `fsync` is intrinsically expensive on rotational media (one\n * full disk-cache flush). The sync variant is exposed for the\n * `runtime-state.ts` writer, which runs in a signal handler with\n * a strict time budget; existing callers were already issuing a\n * blocking `writeFileSync + renameSync`, so the additional cost\n * is the two `fsync` calls. On modern SSDs this remains in the\n * low-millisecond range.\n *\n * Module-load safety: `node:fs` and `node:fs/promises` are loaded\n * lazily so the module can be imported in non-Node environments\n * (Edge Runtime, browser bundles) without crashing at import time.\n * Calling any helper export in such an environment throws a clear\n * error; callers that may run on the edge must therefore probe\n * their own Node-availability before reaching this module (see\n * `init-client.ts`'s `loadFsPathAsync`).\n *\n * @internal Not re-exported from `index.ts`/`node-entry.ts`/\n * `edge-entry.ts`. Importable only from sibling SDK modules.\n */\n\nimport type { FileHandle } from \"node:fs/promises\";\n\n/**\n * Resolves the parent directory of a path without importing `node:path`,\n * so this module remains importable in non-Node environments where\n * `node:path` is unavailable. Handles both POSIX (`/`) and Windows\n * (`\\\\`) separators because Windows paths can use either form.\n *\n * Behavior matches `path.dirname` for the inputs this module receives\n * (always absolute paths produced by SDK callers): finds the last\n * separator and returns the prefix; returns `\".\"` if no separator is\n * present (a relative leaf name); preserves the root for `/foo` →\n * `/`. Edge cases like trailing-separator inputs are not exercised by\n * SDK callers so are not modeled here.\n */\nfunction parentDir(filePath: string): string {\n const lastSlash = filePath.lastIndexOf(\"/\");\n const lastBackslash = filePath.lastIndexOf(\"\\\\\");\n const lastSep = Math.max(lastSlash, lastBackslash);\n if (lastSep < 0) return \".\";\n if (lastSep === 0) return filePath.slice(0, 1); // root: \"/x\" → \"/\"\n return filePath.slice(0, lastSep);\n}\n\n/**\n * Options accepted by both `atomicWriteFile` and `atomicWriteFileSync`.\n *\n * The shape mirrors the relevant subset of `fs.writeFile`'s options\n * object. `mode` defaults to `0o600` (state files); callers writing\n * static or discoverable files (e.g., `.well-known/glasstrace.json`)\n * may pass `0o644`. `encoding` defaults to `\"utf-8\"` when the payload\n * is a string and is ignored when the payload is a `Uint8Array`.\n */\nexport interface AtomicWriteOptions {\n /**\n * POSIX file mode applied to the temp file before the rename.\n * Defaults to `0o600`. The mode applies to the temp file and is\n * carried through the rename; callers that need a different\n * post-rename mode should call `chmod` themselves after this\n * helper resolves.\n *\n * The helper re-applies this mode unconditionally via `chmod`/`chmodSync`\n * after writing, so a pre-existing temp file (e.g., residue from a\n * crashed prior run) cannot carry stale permissive bits into the\n * caller's renamed target. The fsync handle is opened read-only so\n * callers passing a read-only mode (e.g. `0o444`) are still supported.\n */\n mode?: number;\n /**\n * Encoding for string payloads. Defaults to `\"utf-8\"`. Ignored when\n * the payload is a `Uint8Array`.\n */\n encoding?: BufferEncoding;\n}\n\n/** Errno codes that the parent-dir fsync step is permitted to swallow. */\nconst PARENT_FSYNC_SWALLOWED_CODES: ReadonlySet<string> = new Set([\n \"EISDIR\",\n \"EINVAL\",\n \"EPERM\",\n \"ENOTSUP\",\n]);\n\n/**\n * Reads the `code` property off an `unknown` thrown value if present.\n * Helper avoids `as` casts on `err` and works with both plain objects\n * and `NodeJS.ErrnoException` instances.\n */\nfunction errnoCodeOf(err: unknown): string | undefined {\n if (err === null || typeof err !== \"object\") return undefined;\n const code = (err as { code?: unknown }).code;\n return typeof code === \"string\" ? code : undefined;\n}\n\n/**\n * Builds the path of the sibling temp file. The temp lives in the\n * same directory as the target so the eventual `rename(2)` stays on\n * the same filesystem.\n *\n * Callers may pre-compute their own temp paths (e.g., `<path>.tmp-\n * <pid>` for the discovery-file write to keep multi-process collisions\n * disambiguated). When they do, they call the helper's `*WithTmp`\n * variants. The default temp suffix is `.tmp` for parity with the\n * existing 0.19.x call sites.\n */\nfunction defaultTmpPath(targetPath: string): string {\n return `${targetPath}.tmp`;\n}\n\n// ---------------------------------------------------------------------------\n// Lazy module loaders\n// ---------------------------------------------------------------------------\n\nlet fsPromisesCache: typeof import(\"node:fs/promises\") | null | undefined;\nlet fsSyncCache: typeof import(\"node:fs\") | null | undefined;\n\nasync function loadFsPromises(): Promise<typeof import(\"node:fs/promises\")> {\n if (fsPromisesCache !== undefined) {\n if (fsPromisesCache === null) {\n throw new Error(\n \"node:fs/promises is unavailable in this environment; atomicWriteFile cannot be used here.\",\n );\n }\n return fsPromisesCache;\n }\n try {\n fsPromisesCache = await import(\"node:fs/promises\");\n return fsPromisesCache;\n } catch {\n fsPromisesCache = null;\n throw new Error(\n \"node:fs/promises is unavailable in this environment; atomicWriteFile cannot be used here.\",\n );\n }\n}\n\nfunction loadFsSync(): typeof import(\"node:fs\") {\n if (fsSyncCache !== undefined) {\n if (fsSyncCache === null) {\n throw new Error(\n \"node:fs is unavailable in this environment; atomicWriteFileSync cannot be used here.\",\n );\n }\n return fsSyncCache;\n }\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports, glasstrace/no-unguarded-node-require -- guarded by the surrounding try/catch which caches `null` and surfaces a clean Error on subsequent calls; consumers gate with `isSyncFsAvailable()` (DISC-1555).\n fsSyncCache = require(\"node:fs\") as typeof import(\"node:fs\");\n return fsSyncCache;\n } catch {\n fsSyncCache = null;\n throw new Error(\n \"node:fs is unavailable in this environment; atomicWriteFileSync cannot be used here.\",\n );\n }\n}\n\n// ---------------------------------------------------------------------------\n// Async variant\n// ---------------------------------------------------------------------------\n\n/**\n * Atomically writes `payload` to `targetPath` using\n * `tmp + fsync(tmp) + rename + fsync(parent)` semantics.\n *\n * On any error from the write/fsync/rename steps, the helper makes a\n * best-effort attempt to remove the temp file and rethrows the\n * original error. The parent-dir fsync step swallows\n * `EISDIR`/`EINVAL`/`EPERM`/`ENOTSUP` to support platforms where\n * directory fsync is not supported (notably Windows on NTFS).\n *\n * @param targetPath Absolute path to the final destination.\n * @param payload `string` or `Uint8Array` payload.\n * @param options See {@link AtomicWriteOptions}.\n */\nexport async function atomicWriteFile(\n targetPath: string,\n payload: string | Uint8Array,\n options: AtomicWriteOptions = {},\n): Promise<void> {\n return atomicWriteFileWithTmp(targetPath, defaultTmpPath(targetPath), payload, options);\n}\n\n/**\n * Async variant accepting an explicit `tmpPath`. The temp path MUST\n * live in the same directory as `targetPath` to preserve rename\n * atomicity. Used by `cli/discovery-file.ts` to disambiguate\n * concurrent writers via a `.tmp-<pid>` suffix.\n */\nexport async function atomicWriteFileWithTmp(\n targetPath: string,\n tmpPath: string,\n payload: string | Uint8Array,\n options: AtomicWriteOptions = {},\n): Promise<void> {\n const mode = options.mode ?? 0o600;\n const encoding = options.encoding ?? \"utf-8\";\n const fsp = await loadFsPromises();\n\n let handle: FileHandle | null = null;\n try {\n // Step 1: write payload to the temp file.\n if (typeof payload === \"string\") {\n await fsp.writeFile(tmpPath, payload, { encoding, mode });\n } else {\n await fsp.writeFile(tmpPath, payload, { mode });\n }\n\n // Step 1a: re-apply the requested mode unconditionally. `writeFile`\n // only honors `mode` when it CREATES the file; if `tmpPath` is a\n // pre-existing residue from a prior crash (or a hostile actor) the\n // existing permissions are preserved, which would silently rename a\n // world-readable temp into place. Path-based `chmod` lets the fsync\n // handle below remain read-only, so callers that pass a read-only\n // mode (e.g. 0o444) are still supported.\n await fsp.chmod(tmpPath, mode);\n\n // Step 2: fsync the temp file. Open then fsync via the\n // `FileHandle.sync()` method — `writeFile` closes its internal\n // handle immediately, so we re-open here. Read-only is sufficient\n // for `fsync` and works for callers that supply a read-only `mode`.\n handle = await fsp.open(tmpPath, \"r\");\n await handle.sync();\n await handle.close();\n handle = null;\n\n // Step 3: rename into place. POSIX-atomic on same-filesystem.\n await fsp.rename(tmpPath, targetPath);\n } catch (err) {\n if (handle !== null) {\n try {\n await handle.close();\n } catch {\n // Best-effort: the original error takes precedence.\n }\n }\n await removeTmpResidueAsync(fsp, tmpPath);\n throw err;\n }\n\n // Step 4: fsync the parent directory. Failures on platforms that\n // do not support directory fsync are swallowed; genuine I/O errors\n // still propagate.\n await fsyncParentDirAsync(targetPath, fsp);\n}\n\n/**\n * Best-effort removal of the temp file after a failed atomic-write\n * step. Tries `unlink` first (the common case where the temp is a\n * regular file). If `unlink` fails with `EISDIR`/`EPERM` — meaning the\n * temp path resolves to a directory left behind by a prior crash or\n * misconfiguration — falls back to a non-recursive `rmdir`. Any error\n * from either operation is swallowed so the caller can rethrow the\n * original I/O failure.\n */\nasync function removeTmpResidueAsync(\n fsp: typeof import(\"node:fs/promises\"),\n tmpPath: string,\n): Promise<void> {\n try {\n await fsp.unlink(tmpPath);\n return;\n } catch (err) {\n const code = errnoCodeOf(err);\n if (code !== \"EISDIR\" && code !== \"EPERM\") {\n // Tmp may not exist (ENOENT), or unlink may have failed for an\n // unrelated reason. Either way, nothing more to do — the\n // original error takes precedence in the caller.\n return;\n }\n }\n try {\n await fsp.rmdir(tmpPath);\n } catch {\n // Directory may be non-empty or otherwise unremovable; the original\n // I/O failure remains the actionable error for the caller.\n }\n}\n\nasync function fsyncParentDirAsync(\n targetPath: string,\n fsp: typeof import(\"node:fs/promises\"),\n): Promise<void> {\n const parent = parentDir(targetPath);\n let handle: FileHandle | null = null;\n try {\n handle = await fsp.open(parent, \"r\");\n await handle.sync();\n } catch (err) {\n const code = errnoCodeOf(err);\n if (code !== undefined && PARENT_FSYNC_SWALLOWED_CODES.has(code)) {\n // Platform does not support directory fsync (Windows / NTFS).\n // The rename has already returned successfully; durability\n // semantics on those filesystems do not require an explicit\n // directory sync.\n return;\n }\n throw err;\n } finally {\n if (handle !== null) {\n try {\n await handle.close();\n } catch {\n // Close errors after a successful fsync are not actionable.\n }\n }\n }\n}\n\n// ---------------------------------------------------------------------------\n// Sync variant\n// ---------------------------------------------------------------------------\n\n/**\n * Synchronous counterpart to {@link atomicWriteFile}. Exists for\n * `runtime-state.ts`, which writes from a signal handler that\n * cannot await. Otherwise prefer the async variant.\n */\nexport function atomicWriteFileSync(\n targetPath: string,\n payload: string | Uint8Array,\n options: AtomicWriteOptions = {},\n): void {\n atomicWriteFileSyncWithTmp(targetPath, defaultTmpPath(targetPath), payload, options);\n}\n\n/**\n * Sync variant accepting an explicit `tmpPath`. Mirrors\n * {@link atomicWriteFileWithTmp}.\n */\nexport function atomicWriteFileSyncWithTmp(\n targetPath: string,\n tmpPath: string,\n payload: string | Uint8Array,\n options: AtomicWriteOptions = {},\n): void {\n const mode = options.mode ?? 0o600;\n const encoding = options.encoding ?? \"utf-8\";\n const fs = loadFsSync();\n\n let fd: number | null = null;\n try {\n if (typeof payload === \"string\") {\n fs.writeFileSync(tmpPath, payload, { encoding, mode });\n } else {\n fs.writeFileSync(tmpPath, payload, { mode });\n }\n\n // Re-apply the requested mode unconditionally — see the matching\n // comment in `atomicWriteFileWithTmp` for the credential-leak\n // rationale when `tmpPath` is pre-existing residue.\n fs.chmodSync(tmpPath, mode);\n\n // Read-only is sufficient for `fsync` and works for callers that\n // supply a read-only `mode`.\n fd = fs.openSync(tmpPath, \"r\");\n fs.fsyncSync(fd);\n fs.closeSync(fd);\n fd = null;\n\n fs.renameSync(tmpPath, targetPath);\n } catch (err) {\n if (fd !== null) {\n try {\n fs.closeSync(fd);\n } catch {\n // Best-effort.\n }\n }\n removeTmpResidueSync(fs, tmpPath);\n throw err;\n }\n\n fsyncParentDirSyncWithFs(targetPath, fs);\n}\n\n/**\n * Synchronous counterpart to {@link removeTmpResidueAsync}. See the\n * async variant's JSDoc for the `EISDIR`/`EPERM` rationale.\n */\nfunction removeTmpResidueSync(\n fs: typeof import(\"node:fs\"),\n tmpPath: string,\n): void {\n try {\n fs.unlinkSync(tmpPath);\n return;\n } catch (err) {\n const code = errnoCodeOf(err);\n if (code !== \"EISDIR\" && code !== \"EPERM\") {\n return;\n }\n }\n try {\n fs.rmdirSync(tmpPath);\n } catch {\n // Directory may be non-empty; original error takes precedence.\n }\n}\n\n/**\n * Synchronously fsyncs the parent directory of `targetPath`. Errors\n * matching {@link PARENT_FSYNC_SWALLOWED_CODES} (Windows / NTFS does\n * not support directory fsync) are silently ignored; other errors\n * propagate. Exposed for callers like `cli/discovery-file.ts` that\n * need to compose the steps manually around a backup-rollback flow.\n *\n * @internal Sibling-module use only.\n */\nexport function fsyncParentDirSync(targetPath: string): void {\n fsyncParentDirSyncWithFs(targetPath, loadFsSync());\n}\n\nfunction fsyncParentDirSyncWithFs(\n targetPath: string,\n fs: typeof import(\"node:fs\"),\n): void {\n const parent = parentDir(targetPath);\n let fd: number | null = null;\n try {\n fd = fs.openSync(parent, \"r\");\n fs.fsyncSync(fd);\n } catch (err) {\n const code = errnoCodeOf(err);\n if (code !== undefined && PARENT_FSYNC_SWALLOWED_CODES.has(code)) {\n return;\n }\n throw err;\n } finally {\n if (fd !== null) {\n try {\n fs.closeSync(fd);\n } catch {\n // Close errors after a successful fsync are not actionable.\n }\n }\n }\n}\n\n/**\n * Synchronously writes `payload` to `tmpPath` and fsyncs the\n * resulting file so its data and metadata are durable on disk\n * before any rename is observable. Throws on any I/O error from\n * either step; on throw, the partially-written tmp file is left\n * in place (callers handle cleanup so they can decide between\n * `unlink` and a backup-rollback strategy).\n *\n * Steps 1 and 2 of the SDK 2.0 §4.3 protocol. Pair with a `rename`\n * (step 3) and {@link fsyncParentDirSync} (step 4) — or use\n * {@link atomicWriteFileSync}/{@link atomicWriteFileSyncWithTmp}\n * which compose all four steps internally.\n *\n * @internal Sibling-module use only.\n */\nexport function writeAndFsyncTempSync(\n tmpPath: string,\n payload: string | Uint8Array,\n options: AtomicWriteOptions = {},\n): void {\n const mode = options.mode ?? 0o600;\n const encoding = options.encoding ?? \"utf-8\";\n const fs = loadFsSync();\n if (typeof payload === \"string\") {\n fs.writeFileSync(tmpPath, payload, { encoding, mode });\n } else {\n fs.writeFileSync(tmpPath, payload, { mode });\n }\n // Re-apply the requested mode in case `tmpPath` already existed —\n // `writeFileSync` only honors `mode` when creating the file, so a\n // stale residue could otherwise carry permissive bits into the\n // caller's eventual rename. Path-based `chmodSync` keeps the fsync\n // handle below read-only.\n fs.chmodSync(tmpPath, mode);\n // Read-only is sufficient for `fsync` and works for callers that\n // pass a read-only `mode`.\n const fd = fs.openSync(tmpPath, \"r\");\n try {\n fs.fsyncSync(fd);\n } finally {\n try {\n fs.closeSync(fd);\n } catch {\n // Close errors after fsync are not actionable.\n }\n }\n}\n\n/**\n * Returns `true` when synchronous `node:fs` is reachable in the current\n * runtime, `false` otherwise. Callers that prefer a silent-skip path\n * over a thrown `node:fs is unavailable` error use this probe before\n * dispatching to {@link atomicWriteFileSync}.\n *\n * The probe shares the lazy-loader cache with the sync helpers, so a\n * `true` result also primes the cache for the subsequent write. This\n * keeps the probe overhead to one `require(\"node:fs\")` per process.\n *\n * Background: tsup's bundled `__require` shim throws \"Dynamic require\n * of \\\"node:fs\\\" is not supported\" when the SDK is loaded as an ESM\n * module from a host like Next.js (DISC-1555). The runtime is a real\n * Node process — it just lacks a working synchronous `require()`\n * binding in the ESM scope. Async helpers are unaffected because\n * `await import(\"node:fs/promises\")` is ESM-native.\n *\n * @internal Sibling-module use only.\n */\nexport function isSyncFsAvailable(): boolean {\n try {\n loadFsSync();\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Test-only: clear the cached lazy-loaded modules. Allows test suites\n * that mock `node:fs`/`node:fs/promises` to ensure the helper re-runs\n * its module probe.\n *\n * @internal Tests only.\n */\nexport function _resetModuleCacheForTesting(): void {\n fsPromisesCache = undefined;\n fsSyncCache = undefined;\n}\n","import { createHash } from \"node:crypto\";\nimport {\n AnonApiKeySchema,\n DevApiKeySchema,\n type AnonApiKey,\n type DevApiKey,\n} from \"@glasstrace/protocol\";\nimport { readAnonKey, readClaimedKey } from \"./anon-key.js\";\nimport { atomicWriteFile } from \"./atomic-write.js\";\n\n/**\n * Glasstrace MCP endpoint embedded in managed MCP configs and used by\n * the runtime claim-refresh path. Lives here (not in `cli/constants.ts`)\n * so the runtime helper can reach it without crossing the runtime/CLI\n * boundary; CLI callers import it directly from this module.\n */\nexport const MCP_ENDPOINT = \"https://api.glasstrace.dev/mcp\";\n\n/**\n * Runtime-safe MCP credential and config utilities.\n *\n * This module is loaded into user processes at SDK boot. It must not\n * import from `cli/*` or `agent-detection/*` so the runtime bundle does\n * not pull in CLI scaffolding or filesystem scanners. The boundary is\n * enforced by an import-graph guard test.\n *\n * Internal: not re-exported via `node-entry.ts` or `index.ts`.\n *\n * @module\n */\n\nlet fsPathCache:\n | { fs: typeof import(\"node:fs/promises\"); path: typeof import(\"node:path\") }\n | null\n | undefined;\n\nasync function loadFsPath(): Promise<\n | { fs: typeof import(\"node:fs/promises\"); path: typeof import(\"node:path\") }\n | null\n> {\n if (fsPathCache !== undefined) return fsPathCache;\n try {\n const [fs, path] = await Promise.all([\n import(\"node:fs/promises\"),\n import(\"node:path\"),\n ]);\n fsPathCache = { fs, path };\n return fsPathCache;\n } catch {\n fsPathCache = null;\n return null;\n }\n}\n\n/**\n * Computes a stable identity fingerprint for deduplication purposes.\n * This is NOT password hashing — the input is an opaque token used as\n * a marker identity, not a credential stored for authentication.\n *\n * @internal Exported for unit testing and for `cli/scaffolder.ts`'s\n * marker writer.\n */\nexport function identityFingerprint(token: string): string {\n return `sha256:${createHash(\"sha256\").update(token).digest(\"hex\")}`;\n}\n\n/**\n * Compares two MCP config strings for strict canonical-JSON equality.\n * Returns `true` when both inputs parse as JSON and produce structurally\n * equal objects after recursive key sorting; falls back to trimmed text\n * comparison for TOML and other non-JSON formats. Returns `false` on\n * parse errors that don't fall through to text comparison.\n *\n * Used to detect manually-edited MCP configs before overwriting them\n * (DISC-1247 Scenario 2c) and as the staleness signal for SDK-managed\n * configs that must be refreshed when the project's effective\n * credential changes.\n *\n * The matcher is intentionally strict: a hand-edited file that drops\n * any field (including `type: \"http\"` on a Claude `.mcp.json`) must\n * NOT be treated as SDK-managed and silently overwritten on re-init.\n * Callers that need backwards-compatible recognition of the legacy\n * generic `.glasstrace/mcp.json` shape (no `type: \"http\"`, written by\n * SDK versions prior to DISC-1572) should use\n * {@link genericMcpConfigOrLegacyShapeMatches} instead.\n *\n * @internal Exported for unit testing only.\n */\nexport function mcpConfigMatches(\n existingContent: string,\n expectedContent: string,\n): boolean {\n const trimmedExpected = expectedContent.trim();\n\n try {\n const existingParsed: unknown = JSON.parse(existingContent);\n const expectedParsed: unknown = JSON.parse(trimmedExpected);\n return (\n JSON.stringify(canonicalize(existingParsed)) ===\n JSON.stringify(canonicalize(expectedParsed))\n );\n } catch {\n // Fall through to text comparison for TOML and other non-JSON formats.\n }\n\n return existingContent.trim() === trimmedExpected;\n}\n\n/**\n * Generic-shape variant of {@link mcpConfigMatches} that also recognizes\n * the legacy `.glasstrace/mcp.json` shape written by SDK versions prior\n * to DISC-1572 (the same generic shape minus `type: \"http\"`).\n *\n * Scoped to callers that operate exclusively on `.glasstrace/mcp.json`\n * (currently {@link refreshMcpConfigAfterClaim}). It must not be used\n * by per-agent init paths: `decideMcpConfigAction` runs against every\n * detected agent's `.mcp.json`, and a Claude file whose `type` field\n * was hand-edited away must NOT be treated as SDK-managed and silently\n * overwritten on re-init. The shared {@link mcpConfigMatches} stays\n * strict for that reason; the legacy fallback lives here.\n *\n * The fallback is bounded: only the specific legacy generic shape\n * (the new shape minus `type: \"http\"`) is accepted; arbitrary field\n * omissions still report a mismatch.\n *\n * @internal Exported for unit testing only.\n */\nexport function genericMcpConfigOrLegacyShapeMatches(\n existingContent: string,\n expectedGenericContent: string,\n): boolean {\n if (mcpConfigMatches(existingContent, expectedGenericContent)) {\n return true;\n }\n\n try {\n const expectedParsed: unknown = JSON.parse(expectedGenericContent.trim());\n const expectedLegacy = stripTypeFromGlasstraceServer(expectedParsed);\n if (expectedLegacy === null) {\n return false;\n }\n const existingParsed: unknown = JSON.parse(existingContent);\n return (\n JSON.stringify(canonicalize(existingParsed)) ===\n JSON.stringify(canonicalize(expectedLegacy))\n );\n } catch {\n return false;\n }\n}\n\n/**\n * Returns a copy of `value` with `type: \"http\"` removed from\n * `mcpServers.glasstrace`, or `null` when `value` is not the new\n * Claude-compatible SDK shape. Used by\n * {@link genericMcpConfigOrLegacyShapeMatches} to recognize legacy\n * on-disk files written by SDK versions prior to DISC-1572.\n *\n * The helper is deliberately narrow: it only strips when `type` is\n * exactly the string `\"http\"`, and only from the `glasstrace` server\n * entry. Any structural mismatch (non-object root, missing\n * `mcpServers`, missing `glasstrace`, or `type !== \"http\"`) yields\n * `null`, leaving the strict canonical comparison's verdict in place.\n */\nfunction stripTypeFromGlasstraceServer(value: unknown): unknown | null {\n if (value === null || typeof value !== \"object\" || Array.isArray(value)) {\n return null;\n }\n const root = value as Record<string, unknown>;\n const servers = root[\"mcpServers\"];\n if (\n servers === null ||\n typeof servers !== \"object\" ||\n Array.isArray(servers)\n ) {\n return null;\n }\n const serversObj = servers as Record<string, unknown>;\n const server = serversObj[\"glasstrace\"];\n if (\n server === null ||\n typeof server !== \"object\" ||\n Array.isArray(server)\n ) {\n return null;\n }\n const serverObj = server as Record<string, unknown>;\n if (serverObj[\"type\"] !== \"http\") {\n return null;\n }\n const { type: _omittedType, ...serverWithoutType } = serverObj;\n void _omittedType;\n return {\n ...root,\n mcpServers: {\n ...serversObj,\n glasstrace: serverWithoutType,\n },\n };\n}\n\nfunction canonicalize(value: unknown): unknown {\n if (Array.isArray(value)) {\n return value.map(canonicalize);\n }\n if (value !== null && typeof value === \"object\") {\n const obj = value as Record<string, unknown>;\n const sorted: Record<string, unknown> = {};\n for (const key of Object.keys(obj).sort()) {\n sorted[key] = canonicalize(obj[key]);\n }\n return sorted;\n }\n return value;\n}\n\n/**\n * Parses a `.env.local` file's text content for `GLASSTRACE_API_KEY`,\n * returning the last assignment's value. Empty values\n * (`GLASSTRACE_API_KEY=`) and the `your_key_here` placeholder are\n * filtered out. Surrounding single or double quotes are stripped.\n *\n * The resolver validates the returned value against `DevApiKeySchema`\n * before accepting it; this parser is permissive on purpose so that\n * malformed values can be flagged with a `malformed-env-local`\n * warning rather than silently dropped.\n *\n * @internal Exported for unit testing only.\n */\nexport function readEnvLocalApiKey(content: string): string | null {\n let last: string | null = null;\n const regex = /^\\s*GLASSTRACE_API_KEY\\s*=\\s*(.*)$/gm;\n let match: RegExpExecArray | null;\n while ((match = regex.exec(content)) !== null) {\n const raw = match[1].trim();\n if (raw === \"\") continue;\n const unquoted = raw.replace(/^(['\"])(.*)\\1$/, \"$2\");\n if (unquoted === \"\" || unquoted === \"your_key_here\") continue;\n last = unquoted;\n }\n return last;\n}\n\n/**\n * Returns true when the given API key value looks like a claimed\n * developer key (prefix `gt_dev_`). Defensive against leading or\n * trailing whitespace.\n *\n * **This is a prefix-only check, not strict validation.** Use it as a\n * fast path for \"looks like a claimed key, do not overwrite\". The\n * effective-credential resolver validates with\n * `DevApiKeySchema.safeParse` because a `gt_dev_` prefix alone is not\n * sufficient to authenticate against the backend.\n *\n * @internal Exported for unit testing only.\n */\nexport function isDevApiKey(value: string | null | undefined): boolean {\n if (value === null || value === undefined) return false;\n return value.trim().startsWith(\"gt_dev_\");\n}\n\n/**\n * Returns true when the given API key value is a fully-valid anonymous\n * API key (matches `AnonApiKeySchema`). Used by `registerViaCli` as a\n * runtime guard so that a `DevApiKey` cannot be passed via process\n * arguments to vendor MCP CLIs (which would expose it via `ps` on\n * multi-user hosts).\n *\n * @internal Exported for unit testing only.\n */\nexport function isAnonApiKey(value: string | null | undefined): boolean {\n if (value === null || value === undefined) return false;\n return AnonApiKeySchema.safeParse(value).success;\n}\n\n/**\n * The MCP-effective credential, tagged by which on-disk source produced\n * it. `env-local` and `claimed-key` carry a branded `DevApiKey`;\n * `anon` carries a branded `AnonApiKey`. Internal — not re-exported.\n */\nexport type EffectiveMcpCredential =\n | { source: \"env-local\"; key: DevApiKey }\n | { source: \"claimed-key\"; key: DevApiKey }\n | { source: \"anon\"; key: AnonApiKey };\n\n/**\n * Surfaced when the resolver detected a recoverable anomaly the caller\n * should inform the user about without printing key material.\n *\n * - `malformed-env-local`: `.env.local` set `GLASSTRACE_API_KEY` to a\n * value that fails `DevApiKeySchema`. The resolver fell through.\n * - `claimed-key-only`: the effective credential came from\n * `.glasstrace/claimed-key` because `.env.local` had no usable dev\n * key. Suggest the user copy the key into `.env.local`.\n */\nexport type ResolveWarning = \"malformed-env-local\" | \"claimed-key-only\";\n\n/**\n * The resolved credential plus the on-disk anon key (returned\n * separately so the staleness check does not have to re-read the\n * file) and any warnings the caller should surface to the user.\n */\nexport interface ResolveResult {\n effective: EffectiveMcpCredential | null;\n anonKey: AnonApiKey | null;\n warnings: ReadonlyArray<ResolveWarning>;\n}\n\n/**\n * Resolves the MCP-effective credential for a project, in priority\n * order: `.env.local` `GLASSTRACE_API_KEY` (validated as\n * `DevApiKeySchema`) → `.glasstrace/claimed-key` (validated as\n * `DevApiKeySchema`) → `.glasstrace/anon_key` (`AnonApiKey`). Returns\n * `null` for `effective` only when no source produced a usable key.\n *\n * The function is async because it touches the filesystem. It is\n * called only on the post-claim runtime branch and from the CLI\n * commands `glasstrace init` and `glasstrace mcp add`. It is **not**\n * on the steady-state init path.\n */\nexport async function resolveEffectiveMcpCredential(\n projectRoot?: string,\n): Promise<ResolveResult> {\n const root = projectRoot ?? process.cwd();\n const warnings: ResolveWarning[] = [];\n\n const envLocalKey = await readEnvLocalDevKey(root, warnings);\n const claimedKey = envLocalKey === null ? await readClaimedKey(root) : null;\n const anonKey = await readAnonKey(root);\n\n let effective: EffectiveMcpCredential | null = null;\n if (envLocalKey !== null) {\n effective = { source: \"env-local\", key: envLocalKey };\n } else if (claimedKey !== null) {\n effective = { source: \"claimed-key\", key: claimedKey };\n warnings.push(\"claimed-key-only\");\n } else if (anonKey !== null) {\n effective = { source: \"anon\", key: anonKey };\n }\n\n return { effective, anonKey, warnings };\n}\n\nasync function readEnvLocalDevKey(\n root: string,\n warnings: ResolveWarning[],\n): Promise<DevApiKey | null> {\n const modules = await loadFsPath();\n if (!modules) return null;\n\n const envPath = modules.path.join(root, \".env.local\");\n let content: string;\n try {\n content = await modules.fs.readFile(envPath, \"utf-8\");\n } catch {\n return null;\n }\n\n const raw = readEnvLocalApiKey(content);\n if (raw === null) return null;\n\n const parsed = DevApiKeySchema.safeParse(raw);\n if (!parsed.success) {\n warnings.push(\"malformed-env-local\");\n return null;\n }\n return parsed.data;\n}\n\n/**\n * Source label for the credential a marker file describes.\n *\n * @internal\n */\nexport type MarkerCredentialSource = \"env-local\" | \"claimed-key\" | \"anon\";\n\n/**\n * Descriptor passed to {@link writeMcpMarker} and matched by\n * {@link readMcpMarker}. `credentialHash` is the\n * `identityFingerprint` of the credential actually written into the\n * managed MCP config — never the credential itself.\n *\n * @internal\n */\nexport interface MarkerTarget {\n credentialSource: MarkerCredentialSource;\n credentialHash: string;\n}\n\n/**\n * Normalized state of a `.glasstrace/mcp-connected` marker on disk.\n *\n * - `absent`: no marker file present.\n * - `valid`: a v1 or v2 marker that parsed cleanly. v1 markers are\n * reported as `credentialSource = \"anon\"` with `credentialHash`\n * taken from the legacy `keyHash` field (the v1 schema can only\n * describe an anon credential).\n * - `unknown-version`: the marker has `version > 2`. Treat as\n * not-configured so a future SDK that wrote the marker doesn't\n * block this SDK from refreshing.\n * - `corrupted`: parse failure or schema mismatch. Treat as\n * not-configured.\n *\n * @internal\n */\nexport type MarkerState =\n | { status: \"absent\" }\n | { status: \"valid\"; credentialSource: MarkerCredentialSource; credentialHash: string }\n | { status: \"unknown-version\" }\n | { status: \"corrupted\" };\n\nconst MCP_MARKER_FILE = \"mcp-connected\";\nconst GLASSTRACE_DIR = \".glasstrace\";\n\n/**\n * Reads `.glasstrace/mcp-connected` and returns its normalized state.\n * Used by `mcp add` (marker-mismatch detection) and by\n * {@link writeMcpMarker} (skip-if-match optimization).\n *\n * Reader rules per the design (`SDK-034 D3`):\n * - `version === undefined` → v1: `{ keyHash, configuredAt }`. Mapped\n * to `credentialSource: \"anon\"`, `credentialHash: keyHash`. v1's\n * `keyHash` is itself produced by `identityFingerprint`, so the\n * format matches v2 without conversion.\n * - `version === 2` → v2 reader.\n * - `version > 2` → `unknown-version` (conservative-fail).\n * - Parse failure → `corrupted` (conservative-fail).\n *\n * @internal Exported for unit testing only.\n */\nexport async function readMcpMarker(projectRoot?: string): Promise<MarkerState> {\n const root = projectRoot ?? process.cwd();\n const modules = await loadFsPath();\n if (!modules) return { status: \"absent\" };\n\n const markerPath = modules.path.join(root, GLASSTRACE_DIR, MCP_MARKER_FILE);\n let content: string;\n try {\n content = await modules.fs.readFile(markerPath, \"utf-8\");\n } catch {\n return { status: \"absent\" };\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(content);\n } catch {\n return { status: \"corrupted\" };\n }\n\n if (parsed === null || typeof parsed !== \"object\") {\n return { status: \"corrupted\" };\n }\n\n const obj = parsed as Record<string, unknown>;\n const version = obj[\"version\"];\n\n if (version === undefined) {\n // v1: { keyHash, configuredAt }\n const keyHash = obj[\"keyHash\"];\n if (typeof keyHash !== \"string\" || keyHash === \"\") {\n return { status: \"corrupted\" };\n }\n return {\n status: \"valid\",\n credentialSource: \"anon\",\n credentialHash: keyHash,\n };\n }\n\n if (version === 2) {\n const source = obj[\"credentialSource\"];\n const hash = obj[\"credentialHash\"];\n if (\n (source !== \"env-local\" && source !== \"claimed-key\" && source !== \"anon\") ||\n typeof hash !== \"string\" ||\n hash === \"\"\n ) {\n return { status: \"corrupted\" };\n }\n return {\n status: \"valid\",\n credentialSource: source,\n credentialHash: hash,\n };\n }\n\n if (typeof version === \"number\" && version > 2) {\n return { status: \"unknown-version\" };\n }\n\n return { status: \"corrupted\" };\n}\n\n/**\n * Writes a v2 `.glasstrace/mcp-connected` marker. Returns `true` when\n * the marker was created or updated, `false` when an existing marker\n * already records the same `(credentialSource, credentialHash)` pair\n * and was left untouched.\n *\n * Writer always emits v2 with `version: 2`. The legacy `keyHash`\n * field is intentionally omitted from new writes — v1 readers ignore\n * unknown fields and the duplicate would diverge over time. v3+ and\n * corrupted markers are unconditionally overwritten.\n *\n * The directory is created with `0o700` and the file with `0o600`,\n * matching existing scaffolder behavior.\n *\n * @internal Exported for unit testing only.\n */\nexport async function writeMcpMarker(\n projectRoot: string,\n target: MarkerTarget,\n): Promise<boolean> {\n const modules = await loadFsPath();\n if (!modules) return false;\n\n const dirPath = modules.path.join(projectRoot, GLASSTRACE_DIR);\n const markerPath = modules.path.join(dirPath, MCP_MARKER_FILE);\n\n const state = await readMcpMarker(projectRoot);\n if (\n state.status === \"valid\" &&\n state.credentialSource === target.credentialSource &&\n state.credentialHash === target.credentialHash\n ) {\n return false;\n }\n\n await modules.fs.mkdir(dirPath, { recursive: true, mode: 0o700 });\n\n const body = JSON.stringify(\n {\n version: 2,\n credentialSource: target.credentialSource,\n credentialHash: target.credentialHash,\n configuredAt: new Date().toISOString(),\n },\n null,\n 2,\n );\n\n await modules.fs.writeFile(markerPath, body, { mode: 0o600 });\n // writeFile mode only applies on creation on some platforms.\n await modules.fs.chmod(markerPath, 0o600);\n return true;\n}\n\nconst MCP_CONFIG_FILE = \"mcp.json\";\n\n/**\n * The set of outcomes the runtime claim-refresh helper can produce.\n *\n * - `rewrote`: `.glasstrace/mcp.json` matched the SDK-shaped output\n * for the on-disk anon key, was rewritten with the effective\n * credential, and the marker was updated.\n * - `preserved`: `.glasstrace/mcp.json` exists but does not match the\n * SDK-shaped output for the on-disk anon key. The file is left\n * untouched (the user may have hand-edited it). The marker is not\n * touched.\n * - `absent`: `.glasstrace/mcp.json` does not exist (`ENOENT`), or\n * no anon key is on disk so there is nothing to compare against. A\n * project without an anon key never had an SDK-shaped `mcp.json`\n * written by the runtime path, so this branch is a true no-op.\n * - `skipped-anon-source`: the effective credential is `null` or its\n * source is `\"anon\"`. Either way, there is no claim transition to\n * refresh for. Caller should generally gate on\n * `effective.source !== \"anon\"` before invoking the helper; this\n * branch is the runtime-side belt-and-suspenders.\n * - `skipped-not-persisted`: never reached in practice — the caller\n * in `init-client.ts` gates on `writeClaimedKey`'s `persisted` not\n * being `\"none\"`. The variant exists so an exhaustive switch in\n * the caller stays exhaustive if the gate is removed.\n *\n * @internal\n */\nexport type RuntimeRefreshAction =\n | \"rewrote\"\n | \"preserved\"\n | \"absent\"\n | \"skipped-anon-source\"\n | \"skipped-not-persisted\";\n\nlet refreshNudgeEmitted = false;\n\n/**\n * @internal Exported for unit testing only — resets the per-process\n * \"refresh nudge already emitted\" flag.\n */\nexport function __resetRefreshNudgeForTest(): void {\n refreshNudgeEmitted = false;\n}\n\n/**\n * Emits a single redacted stderr line announcing the MCP config\n * refresh. Deduplicated per process via a module-level flag — a\n * second call within the same process is a no-op. Cross-process\n * dedup (the same user running `mcp add` in another terminal moments\n * later) is explicitly out of scope.\n */\nfunction emitRefreshNudge(persistedSource: \"env-local\" | \"claimed-key\"): void {\n if (refreshNudgeEmitted) return;\n refreshNudgeEmitted = true;\n try {\n if (persistedSource === \"claimed-key\") {\n process.stderr.write(\n \"[glasstrace] MCP config refreshed for the new credential. \" +\n \"Copy .glasstrace/claimed-key into .env.local so Codex can pick it up on next restart.\\n\",\n );\n } else {\n process.stderr.write(\n \"[glasstrace] MCP config refreshed for the new credential.\\n\",\n );\n }\n } catch {\n // stderr is best-effort; refresh outcome must not depend on it.\n }\n}\n\n/**\n * Returns the SDK-shaped JSON for `.glasstrace/mcp.json` (the generic\n * MCP config used at runtime). Inlined here — and intentionally not\n * imported from `agent-detection/configs.ts` — because the runtime\n * path must not pull `agent-detection` into the runtime bundle. The\n * shape matches what `generateMcpConfig({ name: \"generic\", ... },\n * endpoint, bearer)` would produce. If the agent-detection version\n * diverges, the staleness check stops detecting SDK-managed configs;\n * a regression test against `generateMcpConfig`'s \"generic\" branch\n * lives in `tests/unit/sdk/mcp-runtime.test.ts`.\n */\nfunction genericMcpConfigContent(endpoint: string, bearer: string): string {\n return JSON.stringify(\n {\n mcpServers: {\n glasstrace: {\n type: \"http\",\n url: endpoint,\n headers: {\n Authorization: `Bearer ${bearer}`,\n },\n },\n },\n },\n null,\n 2,\n );\n}\n\n/**\n * Refreshes `.glasstrace/mcp.json` after a successful account claim\n * transition has persisted a dev/account credential to disk (via\n * `writeClaimedKey`). The file is rewritten only when its content\n * matches the SDK-shaped output for the project's on-disk anon key\n * (canonical-JSON equivalence via `mcpConfigMatches` — whitespace and\n * key order are normalised before comparison). User-edited or\n * third-party `mcp.json` content is preserved.\n *\n * Atomic write protocol: write the replacement to a sibling temp\n * path, set `0o600`, then `rename` into place. This matches the\n * existing pattern at `init-client.ts` for `.glasstrace/config`,\n * `anon-key.ts` for `.glasstrace/anon_key`, and `runtime-state.ts`.\n * The temp must be on the same filesystem as the destination for the\n * `rename` to be atomic.\n *\n * The helper is invoked only on the post-claim runtime branch (see\n * `init-client.ts` `performInit`) and never on the steady-state init\n * path. It must not throw — failures during write/chmod/rename or\n * marker update surface as `\"preserved\"` so the caller's\n * `claimResult` return is preserved. The temp file is best-effort\n * cleaned up on failure to avoid leaving stale `.tmp` siblings on\n * disk.\n *\n * @internal Exported for unit testing only; not re-exported from\n * `node-entry.ts` or `index.ts`.\n */\nexport async function refreshGenericMcpConfigAtRuntime(\n projectRoot: string,\n effective: EffectiveMcpCredential | null,\n anonKeyOnDisk: AnonApiKey | null,\n): Promise<{ action: RuntimeRefreshAction }> {\n if (effective === null || effective.source === \"anon\") {\n return { action: \"skipped-anon-source\" };\n }\n\n // Dev-key-only project (no .glasstrace/anon_key on disk): the\n // staleness check has nothing to compare against. The SDK never\n // wrote mcp.json without an anon key, so there is nothing to\n // refresh.\n if (anonKeyOnDisk === null) {\n return { action: \"absent\" };\n }\n\n const modules = await loadFsPath();\n if (!modules) return { action: \"absent\" };\n\n const dirPath = modules.path.join(projectRoot, GLASSTRACE_DIR);\n const configPath = modules.path.join(dirPath, MCP_CONFIG_FILE);\n\n let existing: string;\n try {\n existing = await modules.fs.readFile(configPath, \"utf-8\");\n } catch (err) {\n const code = (err as NodeJS.ErrnoException).code;\n if (code === \"ENOENT\") {\n return { action: \"absent\" };\n }\n return { action: \"preserved\" };\n }\n\n const expectedAnon = genericMcpConfigContent(MCP_ENDPOINT, anonKeyOnDisk);\n if (!genericMcpConfigOrLegacyShapeMatches(existing, expectedAnon)) {\n return { action: \"preserved\" };\n }\n\n // SDK-managed and stale. Replace atomically per SDK 2.0 §4.3:\n // tmp + fsync(tmp) + rename + fsync(parent). Any failure in the\n // helper or marker update path must produce a non-throw outcome\n // so the caller's claimResult return is preserved; the helper\n // best-effort cleans up the .tmp sibling on failure.\n const replacement = genericMcpConfigContent(MCP_ENDPOINT, effective.key);\n try {\n await atomicWriteFile(configPath, replacement, { mode: 0o600 });\n\n await writeMcpMarker(projectRoot, {\n credentialSource: effective.source,\n credentialHash: identityFingerprint(effective.key),\n });\n } catch {\n return { action: \"preserved\" };\n }\n\n emitRefreshNudge(effective.source);\n\n return { action: \"rewrote\" };\n}\n","import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport { AnonApiKeySchema } from \"@glasstrace/protocol\";\nimport type { AnonApiKey } from \"@glasstrace/protocol\";\nimport {\n atomicWriteFileSyncWithTmp,\n fsyncParentDirSync,\n writeAndFsyncTempSync,\n} from \"../atomic-write.js\";\n\n/**\n * Standardized static discovery-file path, served at\n * `<static-root>/.well-known/glasstrace.json` (per RFC 8615) with\n * MIME type `application/json`.\n *\n * The SDK writes the file to this relative path under the\n * framework-specific static root (`public/` for Next.js, Remix, Astro;\n * `static/` for SvelteKit) and the browser extension fetches it from\n * the same path under the deployed origin.\n *\n * @drift-check RFC 8615 (https://www.rfc-editor.org/rfc/rfc8615) + ../glasstrace-product/docs/component-designs/sdk-architecture.md §7.1 Static discovery file\n */\nexport const WELL_KNOWN_GLASSTRACE_PATH = \".well-known/glasstrace.json\" as const;\n\n/**\n * Current schema version for `.well-known/glasstrace.json`. Consumers\n * (primarily the Glasstrace browser extension) MUST tolerate unknown\n * integers >= 1 per the forward-compatibility rule in the design doc\n * (\"SDK Discovery Endpoint / Static File — Component Design\", §5.3).\n */\nexport const DISCOVERY_FILE_VERSION = 1 as const;\n\n/**\n * Schema of the static discovery file written by `sdk init`.\n *\n * Version 1 defines exactly two required fields: `version` and `key`.\n * Additional fields may appear in later schema versions — consumers MUST\n * ignore unknown fields (forward-compatibility) and MUST reject files\n * whose `key` does not match `^gt_anon_[a-f0-9]{48}$`.\n */\nexport interface DiscoveryFileV1 {\n version: typeof DISCOVERY_FILE_VERSION;\n key: AnonApiKey;\n}\n\n/**\n * Detected framework-specific static root. `public` covers Next.js,\n * Remix, and Astro; `static` covers SvelteKit. No other frameworks\n * differ today per the design doc's §4.3 table.\n */\nexport type StaticRootLayout = \"public\" | \"static\";\n\n/**\n * Result returned by {@link resolveStaticRoot} so callers can report the\n * framework-specific path they targeted (used in init summary lines and\n * rollback output).\n */\nexport interface StaticRootResolution {\n /** Absolute path to the static root directory (may not exist yet). */\n absolutePath: string;\n /** Which layout was chosen. */\n layout: StaticRootLayout;\n}\n\n/**\n * Describes the outcome of a single call to {@link writeDiscoveryFile} so\n * callers can surface an accurate summary line without re-reading the\n * file. Mirrors the DISC-1247 Scenario 2 re-init preservation contract:\n * a valid file whose `key` already matches the on-disk anon key is left\n * alone rather than rewritten.\n */\nexport type WriteDiscoveryAction =\n | \"created\"\n | \"updated-stale\"\n | \"skipped-matches\"\n | \"skipped-foreign\"\n | \"failed\";\n\n/**\n * Structured result from {@link writeDiscoveryFile}.\n */\nexport interface WriteDiscoveryResult {\n action: WriteDiscoveryAction;\n /** Absolute path of the discovery file (whether or not it was written). */\n filePath: string;\n /** Static root that was resolved, useful for `.gitignore` wiring. */\n layout: StaticRootLayout;\n /**\n * When `action === \"failed\"`, a short human-readable reason. Never\n * contains anon key bytes — callers can forward it to logs safely.\n */\n error?: string;\n}\n\n/**\n * Detects the project's framework-specific static root using the ordered\n * check from §4.4 of the design doc:\n *\n * 1. Classify as SvelteKit (→ `static/`) when `package.json` declares\n * `\"type\": \"module\"` AND the project contains `svelte.config.js` (or\n * `svelte.config.ts`) OR `src/app.html`. These signals together are\n * specific enough to avoid false positives on generic ESM projects.\n * 2. Otherwise use `public/` — this covers Next.js, Remix, Astro, and\n * plain Node web apps, which all serve `public/` verbatim.\n *\n * Returns the absolute directory path and the chosen layout. Does NOT\n * create the directory; callers use {@link writeDiscoveryFile}, which\n * creates any missing parents atomically.\n *\n * @internal Exported for unit testing only.\n */\nexport function resolveStaticRoot(projectRoot: string): StaticRootResolution {\n if (isSvelteKitProject(projectRoot)) {\n return {\n absolutePath: path.join(projectRoot, \"static\"),\n layout: \"static\",\n };\n }\n return {\n absolutePath: path.join(projectRoot, \"public\"),\n layout: \"public\",\n };\n}\n\n/**\n * Heuristic for SvelteKit detection. The design doc deliberately scopes\n * the check narrowly so a plain ESM library is never misclassified —\n * `svelte.config.{js,ts}` or `src/app.html` is the SvelteKit fingerprint,\n * and both must coexist with an ESM package.json.\n */\nfunction isSvelteKitProject(projectRoot: string): boolean {\n const pkgPath = path.join(projectRoot, \"package.json\");\n let isEsm = false;\n try {\n const pkgContent = fs.readFileSync(pkgPath, \"utf-8\");\n const parsed = JSON.parse(pkgContent) as { type?: unknown };\n isEsm = parsed.type === \"module\";\n } catch {\n // Missing or malformed package.json — fall through to default layout.\n return false;\n }\n if (!isEsm) return false;\n\n const svelteConfigJs = path.join(projectRoot, \"svelte.config.js\");\n const svelteConfigTs = path.join(projectRoot, \"svelte.config.ts\");\n const appHtml = path.join(projectRoot, \"src\", \"app.html\");\n return (\n fs.existsSync(svelteConfigJs) ||\n fs.existsSync(svelteConfigTs) ||\n fs.existsSync(appHtml)\n );\n}\n\n/**\n * Returns the project-relative path of the discovery file for the given\n * layout, suitable for surfacing in summary lines and `.gitignore` entries.\n */\nexport function relativeDiscoveryPath(layout: StaticRootLayout): string {\n const rootDir = layout === \"static\" ? \"static\" : \"public\";\n return `${rootDir}/${WELL_KNOWN_GLASSTRACE_PATH}`;\n}\n\n/**\n * Parses an existing discovery file and returns its key if the schema is\n * valid, or `null` when the file is missing, unreadable, not JSON, or\n * does not match the version-1 shape. The check is deliberately strict —\n * a corrupt or third-party-authored file is treated as \"no file\" so\n * {@link writeDiscoveryFile} overwrites it with a fresh SDK-managed copy.\n *\n * Extra unknown fields are tolerated (§5.3 forward-compatibility).\n *\n * @internal Exported for unit testing only.\n */\nexport function readExistingDiscoveryFile(\n filePath: string,\n): { key: AnonApiKey; extras: Record<string, unknown> } | null {\n let raw: string;\n try {\n raw = fs.readFileSync(filePath, \"utf-8\");\n } catch {\n return null;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n return null;\n }\n\n if (\n parsed === null ||\n typeof parsed !== \"object\" ||\n Array.isArray(parsed)\n ) {\n return null;\n }\n\n const obj = parsed as Record<string, unknown>;\n const versionRaw = obj.version;\n if (\n typeof versionRaw !== \"number\" ||\n !Number.isInteger(versionRaw) ||\n versionRaw < 1\n ) {\n return null;\n }\n\n const keyResult = AnonApiKeySchema.safeParse(obj.key);\n if (!keyResult.success) {\n return null;\n }\n\n // Preserve user-added fields (extras) so re-init round-trips any custom\n // keys the consumer added. `version` and `key` are SDK-managed and\n // excluded from the extras object.\n const extras: Record<string, unknown> = {};\n for (const [k, v] of Object.entries(obj)) {\n if (k === \"version\" || k === \"key\") continue;\n extras[k] = v;\n }\n\n return { key: keyResult.data, extras };\n}\n\n/**\n * Serializes the discovery payload deterministically (pretty-printed JSON\n * with a trailing newline). Deterministic output keeps git diffs clean\n * when the file is checked in and matches the atomic-write contract:\n * byte-identical output on re-init when `extras` is unchanged.\n */\nfunction serializeDiscoveryPayload(\n key: AnonApiKey,\n extras: Record<string, unknown>,\n): string {\n // Key ordering: version, key, then extras in their original insertion\n // order. Preserves DISC-1247 Scenario 2 alignment — a user who added\n // `\"note\": \"…\"` after `\"key\"` sees the same ordering on re-init.\n const payload: Record<string, unknown> = {\n version: DISCOVERY_FILE_VERSION,\n key,\n ...extras,\n };\n return JSON.stringify(payload, null, 2) + \"\\n\";\n}\n\n/**\n * Writes the discovery file at `<staticRoot>/.well-known/glasstrace.json`\n * atomically.\n *\n * Behavior (per design doc §6.1 and §6.5):\n *\n * - When the target file does not exist, creates it with `{ version: 1,\n * key: <anonKey> }` after creating the `.well-known/` directory if\n * missing.\n * - When the target exists AND parses as a valid version-1 payload AND\n * its `key` matches the supplied `anonKey`: preserves the file (and\n * any user-added extra fields) and returns `\"skipped-matches\"`.\n * - When the target exists AND parses valid BUT its `key` does not\n * match: rewrites the file with the fresh key, preserving extras.\n * Returns `\"updated-stale\"`.\n * - When the target exists BUT fails to parse (corrupt, foreign-authored,\n * wrong schema): rewrites with a fresh SDK-managed payload and returns\n * `\"skipped-foreign\"` to signal that user content was not preserved.\n * - On any unexpected I/O error: returns `\"failed\"` with an error string.\n *\n * Uses a sibling temp file + `renameSync` for atomicity so concurrent\n * readers (e.g., a browser extension polling during dev server startup)\n * never observe a half-written file.\n *\n * @param projectRoot - Absolute path to the project root directory.\n * @param anonKey - The anon key currently on disk (see `anon-key.ts`).\n */\nexport function writeDiscoveryFile(\n projectRoot: string,\n anonKey: AnonApiKey,\n): WriteDiscoveryResult {\n const { absolutePath: staticRoot, layout } = resolveStaticRoot(projectRoot);\n const wellKnownDir = path.join(staticRoot, \".well-known\");\n const filePath = path.join(wellKnownDir, \"glasstrace.json\");\n\n let existingAction: WriteDiscoveryAction;\n let extras: Record<string, unknown> = {};\n\n if (fs.existsSync(filePath)) {\n const existing = readExistingDiscoveryFile(filePath);\n if (existing === null) {\n // Unreadable / malformed / non-SDK content — overwrite with a\n // fresh payload so the extension can discover the current key.\n // Extras are NOT preserved because we cannot safely parse them.\n existingAction = \"skipped-foreign\";\n } else if (existing.key === anonKey) {\n // Valid and already matches — leave the file alone (§6.5 step 2).\n return {\n action: \"skipped-matches\",\n filePath,\n layout,\n };\n } else {\n // Valid but stale — replace the key, preserve extras (§6.5 step 3).\n extras = existing.extras;\n existingAction = \"updated-stale\";\n }\n } else {\n existingAction = \"created\";\n }\n\n const tmpPath = `${filePath}.tmp-${process.pid}`;\n // On Windows, `renameSync` fails with EPERM/EEXIST when the\n // destination already exists. Rather than `unlink` the destination\n // first (which would cause data loss if the subsequent rename fails),\n // move the destination to a sibling backup path, commit the rename,\n // and only then delete the backup. If the rename fails, restore the\n // backup so the original file is preserved.\n const needsWindowsReplace =\n process.platform === \"win32\" && fs.existsSync(filePath);\n const backupPath = needsWindowsReplace\n ? `${filePath}.bak-${process.pid}`\n : null;\n\n try {\n fs.mkdirSync(wellKnownDir, { recursive: true });\n const payload = serializeDiscoveryPayload(anonKey, extras);\n\n if (backupPath !== null) {\n // Windows backup-rollback path. Step the SDK 2.0 §4.3 protocol\n // out manually so the backup rename can interleave between the\n // tmp-fsync and the final rename:\n // 1. write tmp + fsync(tmp)\n // 2. rename existing → backup (Windows requires destination\n // to be free)\n // 3. rename tmp → final\n // 4. fsync(parent) so the rename pair is durable\n // Mode is 0o644 for static/discoverable files per spec §4.3.\n writeAndFsyncTempSync(tmpPath, payload, {\n encoding: \"utf-8\",\n mode: 0o644,\n });\n fs.renameSync(filePath, backupPath);\n try {\n fs.renameSync(tmpPath, filePath);\n } catch (renameErr) {\n try {\n fs.renameSync(backupPath, filePath);\n } catch {\n // Restoration failed; nothing more we can do. Surface the\n // original rename error below so the caller sees the cause.\n }\n throw renameErr;\n }\n fsyncParentDirSync(filePath);\n try {\n fs.unlinkSync(backupPath);\n } catch {\n // Backup cleanup is best-effort; a stale `.bak-<pid>` is\n // preferable to a spurious failure after a successful write.\n }\n } else {\n // Non-Windows / no pre-existing target: full helper composes\n // tmp + fsync(tmp) + rename + fsync(parent) atomically.\n atomicWriteFileSyncWithTmp(filePath, tmpPath, payload, {\n encoding: \"utf-8\",\n mode: 0o644,\n });\n }\n\n return { action: existingAction, filePath, layout };\n } catch (err) {\n // Best-effort: remove the temp file if it was created before the\n // failure so a stale `.tmp-<pid>` does not clutter `.well-known/`.\n try {\n if (fs.existsSync(tmpPath)) {\n fs.unlinkSync(tmpPath);\n }\n } catch {\n // Swallow: the write has already failed; do not mask the root cause.\n }\n return {\n action: \"failed\",\n filePath,\n layout,\n error: err instanceof Error ? err.message : String(err),\n };\n }\n}\n\n/**\n * Describes the outcome of {@link removeDiscoveryFile}. `\"removed\"` means\n * the file existed and was deleted; `\"not-found\"` means there was nothing\n * to remove (no error). `\"failed\"` preserves an error string.\n */\nexport type RemoveDiscoveryAction = \"removed\" | \"not-found\" | \"failed\";\n\n/** Structured result from {@link removeDiscoveryFile}. */\nexport interface RemoveDiscoveryResult {\n action: RemoveDiscoveryAction;\n filePath: string;\n layout: StaticRootLayout;\n /** True when the enclosing `.well-known/` directory was removed too. */\n directoryRemoved: boolean;\n error?: string;\n}\n\n/**\n * Removes the discovery file written by {@link writeDiscoveryFile} if\n * present, and removes the enclosing `.well-known/` directory when it\n * becomes empty. Tolerant of missing files, missing directories, and\n * user-owned sibling content inside `.well-known/` (never deletes a\n * sibling file).\n *\n * Checks BOTH `public/.well-known/glasstrace.json` and\n * `static/.well-known/glasstrace.json` rather than only the\n * currently-inferred layout: if layout detection changes between\n * init and uninit (for example, a SvelteKit project has its\n * `package.json` modified so the heuristic no longer matches),\n * the file written under the original layout would otherwise\n * be orphaned.\n *\n * Matches the uninit contract from design doc §6.4.\n *\n * @param projectRoot - Absolute path to the project root directory.\n */\nexport function removeDiscoveryFile(\n projectRoot: string,\n): RemoveDiscoveryResult {\n const { layout: inferredLayout } = resolveStaticRoot(projectRoot);\n\n // Sweep both candidate layouts so an orphaned file in the non-inferred\n // location is still cleaned up. The returned layout describes where\n // a file was actually removed (preferring the inferred layout when a\n // file existed in both, which is not a supported state but is\n // tolerated); when neither layout had a file, the returned layout\n // mirrors the inferred one so callers surface a stable relative path.\n const layouts: StaticRootLayout[] = [\"public\", \"static\"];\n\n interface LayoutOutcome {\n layout: StaticRootLayout;\n filePath: string;\n removed: boolean;\n directoryRemoved: boolean;\n }\n const outcomes: LayoutOutcome[] = [];\n\n for (const layout of layouts) {\n const staticRoot = path.join(projectRoot, layout);\n const wellKnownDir = path.join(staticRoot, \".well-known\");\n const filePath = path.join(wellKnownDir, \"glasstrace.json\");\n\n let removed = false;\n try {\n if (fs.existsSync(filePath)) {\n fs.unlinkSync(filePath);\n removed = true;\n }\n } catch (err) {\n return {\n action: \"failed\",\n filePath,\n layout,\n directoryRemoved: false,\n error: err instanceof Error ? err.message : String(err),\n };\n }\n\n // Only attempt to prune the enclosing `.well-known/` when we actually\n // removed the discovery file from this layout. Pruning unconditionally\n // would delete a user-owned empty directory (that Glasstrace never\n // populated) as a silent side effect of `sdk uninit`.\n let directoryRemoved = false;\n if (removed) {\n try {\n if (fs.existsSync(wellKnownDir)) {\n const entries = fs.readdirSync(wellKnownDir);\n if (entries.length === 0) {\n fs.rmdirSync(wellKnownDir);\n directoryRemoved = true;\n }\n }\n } catch {\n // Best-effort cleanup; never surface as an error to uninit.\n }\n }\n\n outcomes.push({ layout, filePath, removed, directoryRemoved });\n }\n\n // Pick the outcome to report: prefer one where a file was removed. When\n // both layouts had a file (not a supported state, but tolerated),\n // prefer the inferred layout. When neither had a file, report the\n // inferred layout so callers receive a stable relative path.\n const removals = outcomes.filter((o) => o.removed);\n const chosen: LayoutOutcome = (() => {\n if (removals.length === 0) {\n return (\n outcomes.find((o) => o.layout === inferredLayout) ?? outcomes[0]!\n );\n }\n if (removals.length === 1) return removals[0]!;\n return (\n removals.find((o) => o.layout === inferredLayout) ?? removals[0]!\n );\n })();\n\n // Propagate directoryRemoved across both sweeps so the uninit summary\n // reflects every pruned directory even when only one was the primary.\n const anyDirectoryRemoved = outcomes.some((o) => o.directoryRemoved);\n\n return {\n action: removals.length > 0 ? \"removed\" : \"not-found\",\n filePath: chosen.filePath,\n layout: chosen.layout,\n directoryRemoved: chosen.directoryRemoved || anyDirectoryRemoved,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,MAAoB;AACpB,SAAoB;AACpB,IAAAC,QAAsB;;;ACCf,IAAM,oBAAoB,CAAC,kBAAkB,kBAAkB,iBAAiB;;;ACyFvF,SAAS,UAAU,UAA0B;AAC3C,QAAM,YAAY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