Skip to content

Commit ad34e6c

Browse files
Merge remote-tracking branch 'origin/main' into fix/issue-17812
2 parents eb37b7e + e78297d commit ad34e6c

32 files changed

Lines changed: 1510 additions & 170 deletions

‎apps/desktop/src/app/DesktopObservability.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,19 +366,19 @@ const resolveOtlpEndpoints = Effect.gen(function* () {
366366
otel,
367367
"traces",
368368
{ url: Option.getOrUndefined(environment.otlpTracesUrl), export: signalExport },
369-
persisted.otlpTracesUrl,
369+
{ bootstrapUrl: undefined, settingsUrl: persisted.otlpTracesUrl },
370370
),
371371
metrics: OtelEnvironment.resolveSignalEndpoint(
372372
otel,
373373
"metrics",
374374
{ url: Option.getOrUndefined(environment.otlpMetricsUrl), export: signalExport },
375-
persisted.otlpMetricsUrl,
375+
{ bootstrapUrl: undefined, settingsUrl: persisted.otlpMetricsUrl },
376376
),
377377
logs: OtelEnvironment.resolveSignalEndpoint(
378378
otel,
379379
"logs",
380380
{ url: Option.getOrUndefined(environment.otlpLogsUrl), export: signalExport },
381-
persisted.otlpLogsUrl,
381+
{ bootstrapUrl: undefined, settingsUrl: persisted.otlpLogsUrl },
382382
),
383383
warnings: otel.warnings,
384384
resourceAttributes: otel.resourceAttributes,

‎apps/server/src/bin.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* Every ACP agent spawns `t3 acp-mcp-bridge` while opening its session, and terminal-fallback
33
* agents run `t3 acp-mcp-call` per tool call, so their startup sits on first-message latency.
4-
* Both dispatch here before the full CLI module graph (seconds of evaluation) loads; everything
5-
* else defers to the real CLI in ./binCli.ts.
4+
* They and Codex's managed-token helper dispatch before the full CLI module graph (seconds
5+
* of evaluation) loads; everything else defers to the real CLI in ./binCli.ts.
66
*/
77
import { isEntrypoint } from "./entrypoint.ts";
88

@@ -17,7 +17,10 @@ if (
1717
// Guard sockets before either dispatch path can load outbound-capable modules.
1818
const { ignoreMacosTypeOfServiceEinval } = await import("./outboundHttp.ts");
1919
ignoreMacosTypeOfServiceEinval();
20-
if (command === "acp-mcp-bridge" || command === "acp-mcp-call") {
20+
if (command === "__managed-chatgpt-token") {
21+
const { runManagedChatGptTokenCli } = await import("./cli/managedChatGptToken.ts");
22+
await runManagedChatGptTokenCli(process.argv.slice(3));
23+
} else if (command === "acp-mcp-bridge" || command === "acp-mcp-call") {
2124
const { runAcpMcpCliFastPath } = await import("./mcp/AcpMcpStdioBridge.ts");
2225
await runAcpMcpCliFastPath(command, process.argv.slice(3));
2326
} else {

‎apps/server/src/cli/config.test.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ import { resolveServerConfig } from "./config.ts";
2929
const deriveExplicitServerPaths = (baseDir: string, devUrl: URL | undefined) =>
3030
deriveServerPaths(baseDir, devUrl, { baseDirIsExplicit: true });
3131

32+
const minimalDesktopFlags = (baseDir: string) => ({
33+
mode: Option.some("desktop" as const),
34+
port: Option.some(4888),
35+
host: Option.none<string>(),
36+
baseDir: Option.some(baseDir),
37+
cwd: Option.none<string>(),
38+
devUrl: Option.none<URL>(),
39+
noBrowser: Option.none<boolean>(),
40+
bootstrapFd: Option.none<number>(),
41+
autoBootstrapProjectFromCwd: Option.none<boolean>(),
42+
logWebSocketEvents: Option.none<boolean>(),
43+
tailscaleServeEnabled: Option.none<boolean>(),
44+
tailscaleServePort: Option.none<number>(),
45+
});
46+
3247
const encodeDesktopBootstrap = Schema.encodeEffect(Schema.fromJsonString(DesktopBackendBootstrap));
3348
const encodeUnknownJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
3449

@@ -429,6 +444,77 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
429444
}),
430445
);
431446

447+
it.effect("reports the desktop bootstrap as the source of endpoints it supplied", () =>
448+
Effect.gen(function* () {
449+
const fs = yield* FileSystem.FileSystem;
450+
const baseDir = yield* fs.makeTempDirectoryScoped({
451+
prefix: "t3-cli-config-otlp-bootstrap-",
452+
});
453+
const fd = yield* openBootstrapFd(
454+
makeDesktopBootstrap({
455+
otlpTracesUrl: "http://localhost:4318/v1/traces",
456+
otlpMetricsUrl: "http://localhost:4318/v1/metrics",
457+
otlpLogsUrl: "http://localhost:4318/v1/logs",
458+
}),
459+
);
460+
461+
const resolved = yield* resolveServerConfig(
462+
{ ...minimalDesktopFlags(baseDir), bootstrapFd: Option.some(fd) },
463+
Option.none(),
464+
).pipe(
465+
Effect.provide(
466+
Layer.mergeAll(
467+
ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
468+
NetService.layer,
469+
),
470+
),
471+
);
472+
473+
expect(resolved).toMatchObject({
474+
otlpTracesUrl: "http://localhost:4318/v1/traces",
475+
otlpTracesSource: "bootstrap",
476+
otlpMetricsSource: "bootstrap",
477+
otlpLogsSource: "bootstrap",
478+
});
479+
}),
480+
);
481+
482+
it.effect("reports saved settings as the source of endpoints read from settings.json", () =>
483+
Effect.gen(function* () {
484+
const fs = yield* FileSystem.FileSystem;
485+
const path = yield* Path.Path;
486+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-otlp-settings-" });
487+
const derivedPaths = yield* deriveExplicitServerPaths(baseDir, undefined);
488+
yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true });
489+
yield* fs.writeFileString(
490+
derivedPaths.settingsPath,
491+
`${JSON.stringify({
492+
observability: {
493+
otlpTracesUrl: "http://localhost:4318/v1/traces",
494+
otlpMetricsUrl: "http://localhost:4318/v1/metrics",
495+
otlpLogsUrl: "http://localhost:4318/v1/logs",
496+
},
497+
})}\n`,
498+
);
499+
500+
const resolved = yield* resolveServerConfig(minimalDesktopFlags(baseDir), Option.none()).pipe(
501+
Effect.provide(
502+
Layer.mergeAll(
503+
ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
504+
NetService.layer,
505+
),
506+
),
507+
);
508+
509+
expect(resolved).toMatchObject({
510+
otlpTracesUrl: "http://localhost:4318/v1/traces",
511+
otlpTracesSource: "settings",
512+
otlpMetricsSource: "settings",
513+
otlpLogsSource: "settings",
514+
});
515+
}),
516+
);
517+
432518
it.effect("zeroes an endpoint stored in Settings when the SDK is disabled", () =>
433519
Effect.gen(function* () {
434520
const fs = yield* FileSystem.FileSystem;
@@ -712,6 +798,11 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
712798
});
713799
expect(resolved.otlpLogsUrl).toBe("http://settings:4318/v1/logs");
714800
expect(resolved.otlpLogsExport.headers).toEqual({ "x-key": "secret" });
801+
expect(resolved).toMatchObject({
802+
otlpTracesVariable: "T3CODE_OTLP_TRACES_URL",
803+
otlpMetricsVariable: "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT",
804+
});
805+
expect(resolved.otlpLogsVariable).toBeUndefined();
715806
}),
716807
);
717808

‎apps/server/src/cli/config.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,22 +424,28 @@ export const resolveServerConfig = Effect.fn("config.resolveServerConfig")(funct
424424
otel,
425425
"traces",
426426
{ url: env.otlpTracesUrl, export: signalExport },
427-
bootstrap?.otlpTracesUrl,
428-
persistedObservabilitySettings.otlpTracesUrl,
427+
{
428+
bootstrapUrl: bootstrap?.otlpTracesUrl,
429+
settingsUrl: persistedObservabilitySettings.otlpTracesUrl,
430+
},
429431
);
430432
const metrics = OtelEnvironment.resolveSignalEndpoint(
431433
otel,
432434
"metrics",
433435
{ url: env.otlpMetricsUrl, export: signalExport },
434-
bootstrap?.otlpMetricsUrl,
435-
persistedObservabilitySettings.otlpMetricsUrl,
436+
{
437+
bootstrapUrl: bootstrap?.otlpMetricsUrl,
438+
settingsUrl: persistedObservabilitySettings.otlpMetricsUrl,
439+
},
436440
);
437441
const logs = OtelEnvironment.resolveSignalEndpoint(
438442
otel,
439443
"logs",
440444
{ url: env.otlpLogsUrl, export: signalExport },
441-
bootstrap?.otlpLogsUrl,
442-
persistedObservabilitySettings.otlpLogsUrl,
445+
{
446+
bootstrapUrl: bootstrap?.otlpLogsUrl,
447+
settingsUrl: persistedObservabilitySettings.otlpLogsUrl,
448+
},
443449
);
444450

445451
const config: ServerConfig.ServerConfig["Service"] = {
@@ -455,6 +461,12 @@ export const resolveServerConfig = Effect.fn("config.resolveServerConfig")(funct
455461
otlpTracesExport: traces?.export ?? signalExport,
456462
otlpMetricsExport: metrics?.export ?? signalExport,
457463
otlpLogsExport: logs?.export ?? signalExport,
464+
...(traces !== undefined ? { otlpTracesSource: traces.source } : {}),
465+
...(traces?.variable !== undefined ? { otlpTracesVariable: traces.variable } : {}),
466+
...(metrics !== undefined ? { otlpMetricsSource: metrics.source } : {}),
467+
...(metrics?.variable !== undefined ? { otlpMetricsVariable: metrics.variable } : {}),
468+
...(logs !== undefined ? { otlpLogsSource: logs.source } : {}),
469+
...(logs?.variable !== undefined ? { otlpLogsVariable: logs.variable } : {}),
458470
otelEnvironment: otel,
459471
mode,
460472
port,
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Lean self-invoked local IPC client; no auth/store graph.
2+
import * as NodeNet from "node:net";
3+
import * as Effect from "effect/Effect";
4+
import * as Exit from "effect/Exit";
5+
import * as Schema from "effect/Schema";
6+
import * as Protocol from "../provider/CodexManagedTokenProtocol.ts";
7+
8+
const decodeArguments = Schema.decodeUnknownEffect(
9+
Schema.Tuple([
10+
Schema.NonEmptyString,
11+
Protocol.Request.fields.instanceId,
12+
Protocol.Request.fields.environmentId,
13+
Protocol.Request.fields.profileKey,
14+
]),
15+
);
16+
const encodeRequest = Schema.encodeSync(Schema.fromJsonString(Protocol.Request));
17+
const decodeResponse = Schema.decodeUnknownEffect(Schema.fromJsonString(Protocol.Response), {
18+
onExcessProperty: "error",
19+
});
20+
21+
/** Codex's auth.command protocol is a bare token on stdout, never a CLI diagnostic. */
22+
export async function runManagedChatGptTokenCli(args: ReadonlyArray<string>): Promise<void> {
23+
const program = Effect.gen(function* () {
24+
const [endpoint, instanceId, environmentId, profileKey] = yield* decodeArguments(args);
25+
const socket = yield* Effect.acquireRelease(
26+
Effect.sync(() => NodeNet.createConnection(endpoint)),
27+
(socket) => Effect.sync(() => socket.destroy()),
28+
);
29+
socket.on("error", () => {});
30+
socket.once("connect", () =>
31+
socket.write(encodeRequest({ version: 1, instanceId, environmentId, profileKey }) + "\n"),
32+
);
33+
return yield* Protocol.readFrame(
34+
socket,
35+
Protocol.RESPONSE_BYTES,
36+
Protocol.COMMAND_TIMEOUT_MS,
37+
).pipe(Effect.flatMap(decodeResponse));
38+
}).pipe(Effect.scoped);
39+
const result = await Effect.runPromiseExit(program);
40+
if (Exit.isFailure(result)) {
41+
process.stderr.write(
42+
"Could not access the managed ChatGPT connection. Reconnect in T3 Code.\n",
43+
);
44+
process.exitCode = 1;
45+
return;
46+
}
47+
process.stdout.write(result.value.token);
48+
}

‎apps/server/src/config.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as Path from "effect/Path";
88
import type * as Redacted from "effect/Redacted";
99
import * as Schema from "effect/Schema";
1010

11+
import type { OtlpEndpointSource } from "@t3tools/contracts";
1112
import { sweepStalePendingAttachments } from "./attachmentStore.ts";
1213
import { DEFAULT_SIGNAL_EXPORT, type SignalExport } from "@t3tools/shared/observability";
1314
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
@@ -61,6 +62,12 @@ export class ServerConfig extends Context.Service<
6162
readonly otlpTracesExport: SignalExport;
6263
readonly otlpMetricsExport: SignalExport;
6364
readonly otlpLogsExport: SignalExport;
65+
readonly otlpTracesSource?: OtlpEndpointSource;
66+
readonly otlpTracesVariable?: string;
67+
readonly otlpMetricsSource?: OtlpEndpointSource;
68+
readonly otlpMetricsVariable?: string;
69+
readonly otlpLogsSource?: OtlpEndpointSource;
70+
readonly otlpLogsVariable?: string;
6471
readonly otelEnvironment: OtelEnvironment.OtelEnvironment;
6572
readonly mode: RuntimeMode;
6673
readonly port: number;

‎apps/server/src/observability/OtlpEndpointCheck.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ describe("OtlpEndpointCheck", () => {
189189
...OtelEnvironment.none,
190190
traces: OtelEnvironment.OtelSignal.Export({
191191
url: otelUrl,
192+
variable: "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT",
192193
protocol: otelExport.protocol,
193194
headers: otelExport.headers,
194195
}),

0 commit comments

Comments
 (0)