Files
OmniRoute/open-sse/services/antigravityIdentity.ts
Rouzbeh† 8fa3e314c8 fix(sse): unpin static Antigravity sessionId and add DNS retry classification (#10443) (#11177)
Validated on the combined batch board + this branch: antigravity-dynamic-session-id + proxy-fetch-dns-retry green; file-size gate green with the proxyFetch 1244 frozen entry (dated annotation for the +5 retry-classification lines, owner-authorized). Static per-account sessionId unpinning ends the concurrent-turn 429s and EmptyStreamError drops on the Hermes→Antigravity path; EAI_AGAIN/ENOTFOUND/ETIMEDOUT now classified retryable. Conflict with the tip was only stale provider-count docs. Resolves the remaining #10443 root causes. Thank you @rqzbeh!
2026-08-23 01:05:08 -03:00

82 lines
2.5 KiB
TypeScript

import crypto from "node:crypto";
export type AntigravityCredentialsLike = {
accessToken?: string | null;
connectionId?: string | null;
email?: string | null;
projectId?: string | null;
providerSpecificData?: Record<string, unknown> | null;
};
const FNV_OFFSET_I64 = -3750763034362895579n;
const FNV_PRIME_I64 = 1099511628211n;
function toNonEmptyString(value: unknown): string | null {
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
}
function getProviderDataString(
credentials: AntigravityCredentialsLike | null | undefined,
key: string
): string | null {
const data = credentials?.providerSpecificData;
return data && typeof data === "object" ? toNonEmptyString(data[key]) : null;
}
export function getAntigravityAccountKey(
credentials?: AntigravityCredentialsLike | null
): string | null {
return (
toNonEmptyString(credentials?.email) ||
getProviderDataString(credentials, "email") ||
getProviderDataString(credentials, "accountId") ||
toNonEmptyString(credentials?.connectionId) ||
null
);
}
export function getAntigravityEnvelopeUserAgent(
_credentials?: AntigravityCredentialsLike | null
): "antigravity" {
return "antigravity";
}
export function generateAntigravityRequestId(): string {
return `agent/${Date.now()}/${crypto.randomBytes(4).toString("hex")}`;
}
export function generateAntigravitySessionId(): string {
const max = 18446744073709551615n; // 2^64 - 1
const target = 9_000_000_000_000_000_000n;
// Rejection sampling: discard values in [limit, max] that would cause modulo bias.
// Accepted range [0, limit) divides evenly by target, so value % target is uniform.
const limit = max - (max % target);
let value: bigint;
do {
value = crypto.randomBytes(8).readBigUInt64BE();
} while (value >= limit);
// lgtm[js/biased-cryptographic-random] — rejection sampling above eliminates bias
return `-${(value % target).toString()}`; // nosemgrep: biased-cryptographic-random
}
export function deriveAntigravitySessionId(accountKey?: string | null): string | null {
const key = toNonEmptyString(accountKey);
if (!key) return null;
let hash = FNV_OFFSET_I64;
for (const byte of Buffer.from(key, "utf8")) {
hash = BigInt.asIntN(64, hash ^ BigInt(byte));
hash = BigInt.asIntN(64, hash * FNV_PRIME_I64);
}
return hash.toString();
}
export function getAntigravitySessionId(
credentials?: AntigravityCredentialsLike | null,
fallback?: unknown
): string {
return (
toNonEmptyString(fallback) ||
generateAntigravitySessionId()
);
}