// @effect-diagnostics nodeBuiltinImport:off - Private local IPC framing, shared by server and lean CLI. import type * as NodeNet from "node:net"; import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; export const Request = Schema.Struct({ version: Schema.Literal(1), instanceId: Schema.NonEmptyString, environmentId: Schema.NonEmptyString, profileKey: Schema.String.check(Schema.isPattern(/^[a-f0-9]{64}$/u)), // Ties a refusal to the Codex session that asked, never to another session's 401. sessionKey: Schema.String.check(Schema.isPattern(/^[a-f0-9]{32}$/u)), }); // Messages are safe setup text that Codex shows in its own diagnostics. export const Refusal = Schema.Struct({ kind: Schema.NonEmptyString.check(Schema.isMaxLength(64)), message: Schema.NonEmptyString.check(Schema.isMaxLength(1024)), }); export type Refusal = typeof Refusal.Type; export const Response = Schema.Union([ Schema.Struct({ token: Schema.NonEmptyString }), Schema.Struct({ refusal: Refusal }), ]); // Selectors are small; tokens can be larger JWTs. Never truncate either frame // into something that could be accepted. export const REQUEST_BYTES = 1024; export const RESPONSE_BYTES = 64 * 1024; // Idle/incomplete peers and blocked response consumers must not retain sockets. export const SOCKET_IDLE_MS = 5_000; // Existing lock retry (40s), OAuth (20s), plus startup/store headroom (20s). export const COMMAND_TIMEOUT_MS = 80_000; class ProtocolError extends Schema.TaggedError()("ManagedTokenProtocolError", {}) { override get message(): string { return "The managed token frame was invalid or incomplete."; } } export const readFrame = (socket: NodeNet.Socket, maxBytes: number, timeoutMs: number) => Effect.callback((resume) => { let bytes = Buffer.alloc(0); const fail = () => resume(Effect.fail(new ProtocolError())); const onData = (chunk: Buffer) => { if (bytes.length + chunk.length > maxBytes) return fail(); bytes = Buffer.concat([bytes, chunk]); const end = bytes.indexOf(10); if (end < 0) return; if (end !== bytes.length - 1) return fail(); resume(Effect.succeed(bytes.subarray(0, end).toString("utf8"))); }; socket.on("data", onData); socket.once("error", fail); socket.once("close", fail); socket.once("end", fail); return Effect.sync(() => { socket.off("data", onData); socket.off("error", fail); socket.off("close", fail); socket.off("end", fail); }); }).pipe( Effect.timeoutOrElse({ duration: timeoutMs, orElse: () => Effect.fail(new ProtocolError()) }), ); export const writeFrame = (socket: NodeNet.Socket, frame: string, maxBytes: number) => Effect.callback((resume) => { if ( socket.destroyed || socket.writableEnded || !socket.writable || Buffer.byteLength(frame) + 1 > maxBytes ) { resume(Effect.fail(new ProtocolError())); return; } let writeFailed = false; const fail = () => resume(Effect.fail(new ProtocolError())); socket.once("error", fail); socket.once("close", fail); socket.write(`${frame}\n`, (error) => { if (error != null || socket.errored != null) { writeFailed = true; fail(); } }); socket.end(() => { if (writeFailed || socket.errored != null) fail(); else resume(Effect.void); }); return Effect.sync(() => { socket.off("error", fail); socket.off("close", fail); }); }).pipe( Effect.timeoutOrElse({ duration: SOCKET_IDLE_MS, orElse: () => Effect.fail(new ProtocolError()), }), );