Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
254 lines
9.3 KiB
TypeScript
254 lines
9.3 KiB
TypeScript
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
import { assert, it } from "@effect/vitest";
|
|
import * as Crypto from "effect/Crypto";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Hex from "effect/encoding/Hex";
|
|
import * as FileSystem from "effect/FileSystem";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Logger from "effect/Logger";
|
|
import * as Path from "effect/Path";
|
|
import * as PlatformError from "effect/PlatformError";
|
|
import * as References from "effect/References";
|
|
|
|
import * as ServerConfig from "../config.ts";
|
|
import * as Identify from "./Identify.ts";
|
|
|
|
interface CapturedLog {
|
|
readonly message: unknown;
|
|
readonly annotations: Readonly<Record<string, unknown>>;
|
|
}
|
|
|
|
const sha256 = Effect.fn("test.sha256")(function* (value: string) {
|
|
const crypto = yield* Crypto.Crypto;
|
|
return Hex.encode(
|
|
yield* crypto.digest("SHA-256", new TextEncoder().encode(value)).pipe(Effect.orDie),
|
|
);
|
|
});
|
|
|
|
const makeCaptureLogger = (logs: CapturedLog[]) =>
|
|
Logger.make(({ fiber, message }) => {
|
|
logs.push({
|
|
message,
|
|
annotations: fiber.getRef(References.CurrentLogAnnotations),
|
|
});
|
|
});
|
|
|
|
const findIdentityLog = (
|
|
logs: ReadonlyArray<CapturedLog>,
|
|
source: Identify.TelemetryIdentitySource,
|
|
errorTag: string,
|
|
) => logs.find((log) => log.annotations.source === source && log.annotations.errorTag === errorTag);
|
|
|
|
it.layer(NodeServices.layer)("telemetry identity", (it) => {
|
|
it.effect("uses the persisted anonymous id when provider identities are absent", () =>
|
|
Effect.gen(function* () {
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const anonymousId = "persisted-anonymous-id";
|
|
|
|
yield* fileSystem.writeFileString(config.anonymousIdPath, anonymousId);
|
|
|
|
const identifier = yield* Identify.getTelemetryIdentifierForHome(
|
|
path.join(config.baseDir, "home"),
|
|
);
|
|
|
|
assert.equal(identifier, yield* sha256(anonymousId));
|
|
}).pipe(
|
|
Effect.provide(
|
|
ServerConfig.layerTest(process.cwd(), {
|
|
prefix: "t3-telemetry-identify-anonymous-",
|
|
}),
|
|
),
|
|
),
|
|
);
|
|
|
|
it.effect("leaves no torn anonymous id behind when its write fails midway", () =>
|
|
Effect.gen(function* () {
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const tornWrites = FileSystem.FileSystem.of({
|
|
...fileSystem,
|
|
writeFileString: (filePath, data, options) =>
|
|
fileSystem
|
|
.writeFileString(filePath, data.slice(0, Math.floor(data.length / 2)), options)
|
|
.pipe(
|
|
Effect.andThen(
|
|
Effect.fail(
|
|
PlatformError.systemError({
|
|
_tag: "WriteZero",
|
|
module: "FileSystem",
|
|
method: "writeFileString",
|
|
pathOrDescriptor: filePath,
|
|
}),
|
|
),
|
|
),
|
|
),
|
|
});
|
|
|
|
const identifier = yield* Identify.getTelemetryIdentifierForHome(
|
|
path.join(config.baseDir, "home"),
|
|
).pipe(Effect.provideService(FileSystem.FileSystem, tornWrites));
|
|
|
|
assert.isNull(identifier);
|
|
assert.isFalse(yield* fileSystem.exists(config.anonymousIdPath));
|
|
}).pipe(
|
|
Effect.provide(
|
|
ServerConfig.layerTest(process.cwd(), {
|
|
prefix: "t3-telemetry-identify-torn-",
|
|
}),
|
|
),
|
|
),
|
|
);
|
|
|
|
it.effect.each([
|
|
{
|
|
login: "an API key",
|
|
secret: "sk-private-openai-api-key",
|
|
authJson: (secret: string) => `{"auth_mode":"apikey","OPENAI_API_KEY":"${secret}"}`,
|
|
},
|
|
{
|
|
login: "an agent identity",
|
|
secret: "private-agent-identity-jwt",
|
|
authJson: (secret: string) =>
|
|
`{"auth_mode":"agentIdentity","OPENAI_API_KEY":null,"agent_identity":"${secret}"}`,
|
|
},
|
|
{
|
|
login: "a personal access token",
|
|
secret: "private-personal-access-token",
|
|
authJson: (secret: string) => `{"OPENAI_API_KEY":null,"personal_access_token":"${secret}"}`,
|
|
},
|
|
])("falls back quietly when Codex authenticates with $login", ({ secret, authJson }) => {
|
|
const logs: CapturedLog[] = [];
|
|
const logger = makeCaptureLogger(logs);
|
|
|
|
return Effect.gen(function* () {
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const homeDirectory = path.join(config.baseDir, "home");
|
|
const codexAuthPath = path.join(homeDirectory, ".codex", "auth.json");
|
|
const anonymousId = "tokenless-codex-anonymous-id";
|
|
|
|
yield* fileSystem.makeDirectory(path.dirname(codexAuthPath), { recursive: true });
|
|
yield* fileSystem.writeFileString(codexAuthPath, authJson(secret));
|
|
yield* fileSystem.writeFileString(config.anonymousIdPath, anonymousId);
|
|
|
|
const identifier = yield* Identify.getTelemetryIdentifierForHome(homeDirectory);
|
|
|
|
assert.equal(identifier, yield* sha256(anonymousId));
|
|
assert.isUndefined(findIdentityLog(logs, "codex", "TelemetryIdentityDecodeError"));
|
|
assert.isUndefined(findIdentityLog(logs, "codex", "TelemetryIdentityReadError"));
|
|
const allLogs = logs
|
|
.map((log) =>
|
|
[String(log.message), ...Object.values(log.annotations).map(String)].join("\n"),
|
|
)
|
|
.join("\n");
|
|
assert.notInclude(allLogs, secret);
|
|
}).pipe(
|
|
Effect.provide(
|
|
Layer.merge(
|
|
ServerConfig.layerTest(process.cwd(), {
|
|
prefix: "t3-telemetry-identify-tokenless-",
|
|
}),
|
|
Logger.layer([logger], { mergeWithExisting: false }),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
it.effect("logs structured decode context and falls back from malformed Codex auth", () => {
|
|
const logs: CapturedLog[] = [];
|
|
const logger = makeCaptureLogger(logs);
|
|
|
|
return Effect.gen(function* () {
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const homeDirectory = path.join(config.baseDir, "home");
|
|
const codexAuthPath = path.join(homeDirectory, ".codex", "auth.json");
|
|
const anonymousId = "decode-fallback-anonymous-id";
|
|
const privateAccessToken = "private-codex-access-token";
|
|
|
|
yield* fileSystem.makeDirectory(path.dirname(codexAuthPath), { recursive: true });
|
|
yield* fileSystem.writeFileString(
|
|
codexAuthPath,
|
|
`{"tokens":{"access_token":"${privateAccessToken}"}}`,
|
|
);
|
|
yield* fileSystem.writeFileString(config.anonymousIdPath, anonymousId);
|
|
|
|
const identifier = yield* Identify.getTelemetryIdentifierForHome(homeDirectory);
|
|
|
|
assert.equal(identifier, yield* sha256(anonymousId));
|
|
const decodeLog = findIdentityLog(logs, "codex", "TelemetryIdentityDecodeError");
|
|
assert.isDefined(decodeLog);
|
|
assert.equal(
|
|
decodeLog?.message,
|
|
`Failed to decode codex telemetry identity at '${codexAuthPath}'.`,
|
|
);
|
|
|
|
assert.equal(decodeLog?.annotations.filePath, codexAuthPath);
|
|
assert.equal(decodeLog?.annotations.causeKind, "schema");
|
|
assert.notProperty(decodeLog?.annotations ?? {}, "cause");
|
|
const errorStack = decodeLog?.annotations.errorStack;
|
|
assert.isString(errorStack);
|
|
assert.include(errorStack, "Failed to decode codex telemetry identity");
|
|
const annotations = Object.values(decodeLog?.annotations ?? {})
|
|
.map(String)
|
|
.join("\n");
|
|
assert.notInclude(annotations, privateAccessToken);
|
|
}).pipe(
|
|
Effect.provide(
|
|
Layer.merge(
|
|
ServerConfig.layerTest(process.cwd(), {
|
|
prefix: "t3-telemetry-identify-decode-",
|
|
}),
|
|
Logger.layer([logger], { mergeWithExisting: false }),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
it.effect("does not overwrite the anonymous id path after a non-NotFound read failure", () => {
|
|
const logs: CapturedLog[] = [];
|
|
const logger = makeCaptureLogger(logs);
|
|
|
|
return Effect.gen(function* () {
|
|
const config = yield* ServerConfig.ServerConfig;
|
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
const homeDirectory = path.join(config.baseDir, "home");
|
|
|
|
yield* fileSystem.makeDirectory(config.anonymousIdPath);
|
|
|
|
const identifier = yield* Identify.getTelemetryIdentifierForHome(homeDirectory);
|
|
|
|
assert.isNull(identifier);
|
|
assert.deepEqual(yield* fileSystem.readDirectory(config.anonymousIdPath), []);
|
|
|
|
const readLog = findIdentityLog(logs, "anonymous", "TelemetryIdentityReadError");
|
|
assert.isDefined(readLog);
|
|
assert.equal(readLog?.annotations.filePath, config.anonymousIdPath);
|
|
assert.equal(readLog?.annotations.causeKind, "platform");
|
|
assert.notEqual(readLog?.annotations.platformReason, "NotFound");
|
|
assert.notProperty(readLog?.annotations ?? {}, "cause");
|
|
const errorStack = readLog?.annotations.errorStack;
|
|
assert.isString(errorStack);
|
|
assert.include(errorStack, "Failed to read anonymous telemetry identity");
|
|
assert.isUndefined(
|
|
findIdentityLog(logs, "anonymous", "TelemetryAnonymousIdPersistenceError"),
|
|
);
|
|
}).pipe(
|
|
Effect.provide(
|
|
Layer.merge(
|
|
ServerConfig.layerTest(process.cwd(), {
|
|
prefix: "t3-telemetry-identify-read-",
|
|
}),
|
|
Logger.layer([logger], { mergeWithExisting: false }),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
});
|