| ... | @@ -3,7 +3,9 @@ import * as Crypto from "effect/Crypto"; | ... | @@ -3,7 +3,9 @@ import * as Crypto from "effect/Crypto"; |
| 3 | import * as Effect from "effect/Effect"; | 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Encoding from "effect/Encoding"; | 4 | import * as Encoding from "effect/Encoding"; |
| 5 | import * as FileSystem from "effect/FileSystem"; | 5 | import * as FileSystem from "effect/FileSystem"; |
| | 6 | import * as Option from "effect/Option"; |
| 6 | import * as Path from "effect/Path"; | 7 | import * as Path from "effect/Path"; |
| | 8 | import * as PlatformError from "effect/PlatformError"; |
| 7 | import * as Schema from "effect/Schema"; | 9 | import * as Schema from "effect/Schema"; |
| 8 | | 10 | |
| 9 | import * as ServerConfig from "../config.ts"; | 11 | import * as ServerConfig from "../config.ts"; |
| ... | @@ -18,66 +20,225 @@ const ClaudeJsonSchema = Schema.Struct({ | ... | @@ -18,66 +20,225 @@ const ClaudeJsonSchema = Schema.Struct({ |
| 18 | userID: Schema.String, | 20 | userID: Schema.String, |
| 19 | }); | 21 | }); |
| 20 | | 22 | |
| 21 | class IdentifyUserError extends Schema.TaggedErrorClass<IdentifyUserError>()("IdentifyUserError", { | 23 | export const TelemetryIdentitySource = Schema.Literals(["codex", "claude", "anonymous"]); |
| 22 | operation: Schema.Literal("hash_identifier"), | 24 | export type TelemetryIdentitySource = typeof TelemetryIdentitySource.Type; |
| 23 | algorithm: Schema.Literal("SHA-256"), | 25 | |
| 24 | cause: Schema.Defect(), | 26 | export class TelemetryIdentityReadError extends Schema.TaggedErrorClass<TelemetryIdentityReadError>()( |
| 25 | }) { | 27 | "TelemetryIdentityReadError", |
| | 28 | { |
| | 29 | source: TelemetryIdentitySource, |
| | 30 | filePath: Schema.String, |
| | 31 | cause: Schema.Defect(), |
| | 32 | }, |
| | 33 | ) { |
| | 34 | override get message(): string { |
| | 35 | return `Failed to read ${this.source} telemetry identity at '${this.filePath}'.`; |
| | 36 | } |
| | 37 | } |
| | 38 | |
| | 39 | export class TelemetryIdentityDecodeError extends Schema.TaggedErrorClass<TelemetryIdentityDecodeError>()( |
| | 40 | "TelemetryIdentityDecodeError", |
| | 41 | { |
| | 42 | source: Schema.Literals(["codex", "claude"]), |
| | 43 | filePath: Schema.String, |
| | 44 | cause: Schema.Defect(), |
| | 45 | }, |
| | 46 | ) { |
| 26 | override get message(): string { | 47 | override get message(): string { |
| 27 | return `Failed to hash telemetry identifier with ${this.algorithm}.`; | 48 | return `Failed to decode ${this.source} telemetry identity at '${this.filePath}'.`; |
| 28 | } | 49 | } |
| 29 | } | 50 | } |
| 30 | | 51 | |
| 31 | const hash = (value: string) => | 52 | export class TelemetryAnonymousIdGenerationError extends Schema.TaggedErrorClass<TelemetryAnonymousIdGenerationError>()( |
| | 53 | "TelemetryAnonymousIdGenerationError", |
| | 54 | { |
| | 55 | source: Schema.Literal("anonymous"), |
| | 56 | filePath: Schema.String, |
| | 57 | cause: Schema.Defect(), |
| | 58 | }, |
| | 59 | ) { |
| | 60 | override get message(): string { |
| | 61 | return `Failed to generate anonymous telemetry identity for '${this.filePath}'.`; |
| | 62 | } |
| | 63 | } |
| | 64 | |
| | 65 | export class TelemetryAnonymousIdPersistenceError extends Schema.TaggedErrorClass<TelemetryAnonymousIdPersistenceError>()( |
| | 66 | "TelemetryAnonymousIdPersistenceError", |
| | 67 | { |
| | 68 | source: Schema.Literal("anonymous"), |
| | 69 | filePath: Schema.String, |
| | 70 | cause: Schema.Defect(), |
| | 71 | }, |
| | 72 | ) { |
| | 73 | override get message(): string { |
| | 74 | return `Failed to persist anonymous telemetry identity at '${this.filePath}'.`; |
| | 75 | } |
| | 76 | } |
| | 77 | |
| | 78 | export class TelemetryIdentityHashError extends Schema.TaggedErrorClass<TelemetryIdentityHashError>()( |
| | 79 | "TelemetryIdentityHashError", |
| | 80 | { |
| | 81 | source: TelemetryIdentitySource, |
| | 82 | algorithm: Schema.Literal("SHA-256"), |
| | 83 | cause: Schema.Defect(), |
| | 84 | }, |
| | 85 | ) { |
| | 86 | override get message(): string { |
| | 87 | return `Failed to hash ${this.source} telemetry identity with ${this.algorithm}.`; |
| | 88 | } |
| | 89 | } |
| | 90 | |
| | 91 | type TelemetryIdentityError = |
| | 92 | | TelemetryIdentityReadError |
| | 93 | | TelemetryIdentityDecodeError |
| | 94 | | TelemetryAnonymousIdGenerationError |
| | 95 | | TelemetryAnonymousIdPersistenceError |
| | 96 | | TelemetryIdentityHashError; |
| | 97 | |
| | 98 | const decodeCodexAuthJson = Schema.decodeEffect(Schema.fromJsonString(CodexAuthJsonSchema)); |
| | 99 | const decodeClaudeJson = Schema.decodeEffect(Schema.fromJsonString(ClaudeJsonSchema)); |
| | 100 | |
| | 101 | function isNotFoundError(error: PlatformError.PlatformError): boolean { |
| | 102 | return error.reason._tag === "NotFound"; |
| | 103 | } |
| | 104 | |
| | 105 | const getTelemetryIdentityCauseAnnotations = (cause: unknown) => { |
| | 106 | if (cause instanceof PlatformError.PlatformError) { |
| | 107 | return { |
| | 108 | causeKind: "platform", |
| | 109 | platformReason: cause.reason._tag, |
| | 110 | }; |
| | 111 | } |
| | 112 | if (cause instanceof Schema.SchemaError) { |
| | 113 | return { causeKind: "schema" }; |
| | 114 | } |
| | 115 | return { causeKind: "other" }; |
| | 116 | }; |
| | 117 | |
| | 118 | const logTelemetryIdentityError = (error: TelemetryIdentityError) => |
| | 119 | Effect.logWarning(error.message).pipe( |
| | 120 | Effect.annotateLogs({ |
| | 121 | errorTag: error._tag, |
| | 122 | source: error.source, |
| | 123 | ...("filePath" in error ? { filePath: error.filePath } : {}), |
| | 124 | ...getTelemetryIdentityCauseAnnotations(error.cause), |
| | 125 | ...(error.stack === undefined ? {} : { errorStack: error.stack }), |
| | 126 | }), |
| | 127 | ); |
| | 128 | |
| | 129 | const readIdentityFile = ( |
| | 130 | fileSystem: FileSystem.FileSystem, |
| | 131 | source: TelemetryIdentitySource, |
| | 132 | filePath: string, |
| | 133 | ) => |
| | 134 | fileSystem.readFileString(filePath).pipe( |
| | 135 | Effect.map(Option.some), |
| | 136 | Effect.catchTags({ |
| | 137 | PlatformError: (cause) => |
| | 138 | isNotFoundError(cause) |
| | 139 | ? Effect.succeed(Option.none<string>()) |
| | 140 | : Effect.fail( |
| | 141 | new TelemetryIdentityReadError({ |
| | 142 | source, |
| | 143 | filePath, |
| | 144 | cause, |
| | 145 | }), |
| | 146 | ), |
| | 147 | }), |
| | 148 | ); |
| | 149 | |
| | 150 | const hash = (source: TelemetryIdentitySource, value: string) => |
| 32 | Crypto.Crypto.pipe( | 151 | Crypto.Crypto.pipe( |
| 33 | Effect.flatMap((crypto) => crypto.digest("SHA-256", new TextEncoder().encode(value))), | 152 | Effect.flatMap((crypto) => crypto.digest("SHA-256", new TextEncoder().encode(value))), |
| 34 | Effect.map(Encoding.encodeHex), | 153 | Effect.map(Encoding.encodeHex), |
| 35 | Effect.mapError( | 154 | Effect.mapError( |
| 36 | (cause) => | 155 | (cause) => |
| 37 | new IdentifyUserError({ | 156 | new TelemetryIdentityHashError({ |
| 38 | operation: "hash_identifier", | 157 | source, |
| 39 | algorithm: "SHA-256", | 158 | algorithm: "SHA-256", |
| 40 | cause, | 159 | cause, |
| 41 | }), | 160 | }), |
| 42 | ), | 161 | ), |
| 43 | ); | 162 | ); |
| 44 | | 163 | |
| 45 | const getCodexAccountId = Effect.gen(function* () { | 164 | const getCodexAccountId = Effect.fn("TelemetryIdentity.getCodexAccountId")(function* ( |
| | 165 | homeDirectory: string, |
| | 166 | ) { |
| 46 | const fileSystem = yield* FileSystem.FileSystem; | 167 | const fileSystem = yield* FileSystem.FileSystem; |
| 47 | const path = yield* Path.Path; | 168 | const path = yield* Path.Path; |
| 48 | | 169 | |
| 49 | const authJsonPath = path.join(NodeOS.homedir(), ".codex", "auth.json"); | 170 | const authJsonPath = path.join(homeDirectory, ".codex", "auth.json"); |
| 50 | const authJson = yield* Effect.flatMap( | 171 | const encoded = yield* readIdentityFile(fileSystem, "codex", authJsonPath); |
| 51 | fileSystem.readFileString(authJsonPath), | 172 | if (Option.isNone(encoded)) { |
| 52 | Schema.decodeEffect(Schema.fromJsonString(CodexAuthJsonSchema)), | 173 | return Option.none<string>(); |
| | 174 | } |
| | 175 | const authJson = yield* decodeCodexAuthJson(encoded.value).pipe( |
| | 176 | Effect.mapError( |
| | 177 | (cause) => |
| | 178 | new TelemetryIdentityDecodeError({ |
| | 179 | source: "codex", |
| | 180 | filePath: authJsonPath, |
| | 181 | cause, |
| | 182 | }), |
| | 183 | ), |
| 53 | ); | 184 | ); |
| 54 | | 185 | |
| 55 | return authJson.tokens.account_id; | 186 | return Option.some(authJson.tokens.account_id); |
| 56 | }); | 187 | }); |
| 57 | | 188 | |
| 58 | const getClaudeUserId = Effect.gen(function* () { | 189 | const getClaudeUserId = Effect.fn("TelemetryIdentity.getClaudeUserId")(function* ( |
| | 190 | homeDirectory: string, |
| | 191 | ) { |
| 59 | const fileSystem = yield* FileSystem.FileSystem; | 192 | const fileSystem = yield* FileSystem.FileSystem; |
| 60 | const path = yield* Path.Path; | 193 | const path = yield* Path.Path; |
| 61 | | 194 | |
| 62 | const claudeJsonPath = path.join(NodeOS.homedir(), ".claude.json"); | 195 | const claudeJsonPath = path.join(homeDirectory, ".claude.json"); |
| 63 | const claudeJson = yield* Effect.flatMap( | 196 | const encoded = yield* readIdentityFile(fileSystem, "claude", claudeJsonPath); |
| 64 | fileSystem.readFileString(claudeJsonPath), | 197 | if (Option.isNone(encoded)) { |
| 65 | Schema.decodeEffect(Schema.fromJsonString(ClaudeJsonSchema)), | 198 | return Option.none<string>(); |
| | 199 | } |
| | 200 | const claudeJson = yield* decodeClaudeJson(encoded.value).pipe( |
| | 201 | Effect.mapError( |
| | 202 | (cause) => |
| | 203 | new TelemetryIdentityDecodeError({ |
| | 204 | source: "claude", |
| | 205 | filePath: claudeJsonPath, |
| | 206 | cause, |
| | 207 | }), |
| | 208 | ), |
| 66 | ); | 209 | ); |
| 67 | | 210 | |
| 68 | return claudeJson.userID; | 211 | return Option.some(claudeJson.userID); |
| 69 | }); | 212 | }); |
| 70 | | 213 | |
| 71 | const upsertAnonymousId = Effect.gen(function* () { | 214 | const upsertAnonymousId = Effect.gen(function* () { |
| 72 | const fileSystem = yield* FileSystem.FileSystem; | 215 | const fileSystem = yield* FileSystem.FileSystem; |
| 73 | const { anonymousIdPath } = yield* ServerConfig.ServerConfig; | 216 | const { anonymousIdPath } = yield* ServerConfig.ServerConfig; |
| 74 | | 217 | |
| 75 | const anonymousId = yield* fileSystem.readFileString(anonymousIdPath).pipe( | 218 | const existing = yield* readIdentityFile(fileSystem, "anonymous", anonymousIdPath); |
| 76 | Effect.catch(() => | 219 | if (Option.isSome(existing)) { |
| 77 | Crypto.Crypto.pipe( | 220 | return existing.value; |
| 78 | Effect.flatMap((crypto) => crypto.randomUUIDv4), | 221 | } |
| 79 | Effect.tap((randomId) => fileSystem.writeFileString(anonymousIdPath, randomId)), | 222 | |
| 80 | ), | 223 | const anonymousId = yield* Crypto.Crypto.pipe( |
| | 224 | Effect.flatMap((crypto) => crypto.randomUUIDv4), |
| | 225 | Effect.mapError( |
| | 226 | (cause) => |
| | 227 | new TelemetryAnonymousIdGenerationError({ |
| | 228 | source: "anonymous", |
| | 229 | filePath: anonymousIdPath, |
| | 230 | cause, |
| | 231 | }), |
| | 232 | ), |
| | 233 | ); |
| | 234 | yield* fileSystem.writeFileString(anonymousIdPath, anonymousId).pipe( |
| | 235 | Effect.mapError( |
| | 236 | (cause) => |
| | 237 | new TelemetryAnonymousIdPersistenceError({ |
| | 238 | source: "anonymous", |
| | 239 | filePath: anonymousIdPath, |
| | 240 | cause, |
| | 241 | }), |
| 81 | ), | 242 | ), |
| 82 | ); | 243 | ); |
| 83 | | 244 | |
| ... | @@ -90,24 +251,53 @@ const upsertAnonymousId = Effect.gen(function* () { | ... | @@ -90,24 +251,53 @@ const upsertAnonymousId = Effect.gen(function* () { |
| 90 | * 2. ~/.claude.json userID | 251 | * 2. ~/.claude.json userID |
| 91 | * 3. ~/.t3/telemetry/anonymous-id | 252 | * 3. ~/.t3/telemetry/anonymous-id |
| 92 | */ | 253 | */ |
| 93 | export const getTelemetryIdentifier = Effect.gen(function* () { | 254 | export const getTelemetryIdentifierForHome = Effect.fn("getTelemetryIdentifierForHome")( |
| 94 | const codexAccountId = yield* Effect.result(getCodexAccountId); | 255 | function* (homeDirectory: string) { |
| 95 | if (codexAccountId._tag === "Success") { | 256 | const codexAccountId = yield* getCodexAccountId(homeDirectory).pipe( |
| 96 | return yield* hash(codexAccountId.success); | 257 | Effect.catchTags({ |
| 97 | } | 258 | TelemetryIdentityReadError: (error) => |
| | 259 | logTelemetryIdentityError(error).pipe(Effect.as(Option.none<string>())), |
| | 260 | TelemetryIdentityDecodeError: (error) => |
| | 261 | logTelemetryIdentityError(error).pipe(Effect.as(Option.none<string>())), |
| | 262 | }), |
| | 263 | ); |
| | 264 | if (Option.isSome(codexAccountId)) { |
| | 265 | return yield* hash("codex", codexAccountId.value); |
| | 266 | } |
| 98 | | 267 | |
| 99 | const claudeUserId = yield* Effect.result(getClaudeUserId); | 268 | const claudeUserId = yield* getClaudeUserId(homeDirectory).pipe( |
| 100 | if (claudeUserId._tag === "Success") { | 269 | Effect.catchTags({ |
| 101 | return yield* hash(claudeUserId.success); | 270 | TelemetryIdentityReadError: (error) => |
| 102 | } | 271 | logTelemetryIdentityError(error).pipe(Effect.as(Option.none<string>())), |
| | 272 | TelemetryIdentityDecodeError: (error) => |
| | 273 | logTelemetryIdentityError(error).pipe(Effect.as(Option.none<string>())), |
| | 274 | }), |
| | 275 | ); |
| | 276 | if (Option.isSome(claudeUserId)) { |
| | 277 | return yield* hash("claude", claudeUserId.value); |
| | 278 | } |
| 103 | | 279 | |
| 104 | const anonymousId = yield* Effect.result(upsertAnonymousId); | 280 | const anonymousId = yield* upsertAnonymousId.pipe( |
| 105 | if (anonymousId._tag === "Success") { | 281 | Effect.map(Option.some), |
| 106 | return yield* hash(anonymousId.success); | 282 | Effect.catchTags({ |
| 107 | } | 283 | TelemetryIdentityReadError: (error) => |
| | 284 | logTelemetryIdentityError(error).pipe(Effect.as(Option.none<string>())), |
| | 285 | TelemetryAnonymousIdGenerationError: (error) => |
| | 286 | logTelemetryIdentityError(error).pipe(Effect.as(Option.none<string>())), |
| | 287 | TelemetryAnonymousIdPersistenceError: (error) => |
| | 288 | logTelemetryIdentityError(error).pipe(Effect.as(Option.none<string>())), |
| | 289 | }), |
| | 290 | ); |
| | 291 | if (Option.isSome(anonymousId)) { |
| | 292 | return yield* hash("anonymous", anonymousId.value); |
| | 293 | } |
| 108 | | 294 | |
| 109 | return null; | 295 | return null; |
| 110 | }).pipe( | 296 | }, |
| 111 | Effect.tapError((error) => Effect.logWarning("Failed to get identifier", { cause: error })), | 297 | Effect.tapError(logTelemetryIdentityError), |
| 112 | Effect.orElseSucceed(() => null), | 298 | Effect.orElseSucceed(() => null), |
| 113 | ); | 299 | ); |
| | 300 | |
| | 301 | export const getTelemetryIdentifier = Effect.suspend(() => |
| | 302 | getTelemetryIdentifierForHome(NodeOS.homedir()), |
| | 303 | ); |