|
| 1 | +export type PipelineHarnessPreflightBody = { |
| 2 | + repositoryFullName?: string; |
| 3 | + sourceBranch?: string; |
| 4 | + sourceCommit?: string; |
| 5 | + readId?: string; |
| 6 | + depositId?: string; |
| 7 | +}; |
| 8 | + |
| 9 | +type PipelineHarnessRuntimeEnv = Record<string, string | undefined>; |
| 10 | + |
| 11 | +export function isProductionDeployment(env: PipelineHarnessRuntimeEnv = process.env): boolean { |
| 12 | + return env.VERCEL_ENV === 'production'; |
| 13 | +} |
| 14 | + |
| 15 | +export function isDeployedRuntime(env: PipelineHarnessRuntimeEnv = process.env): boolean { |
| 16 | + return env.VERCEL === '1' || env.NODE_ENV === 'production'; |
| 17 | +} |
| 18 | + |
| 19 | +export function isEnabled(value: string | undefined): boolean { |
| 20 | + return ['1', 'true', 'yes', 'on'].includes(String(value || '').trim().toLowerCase()); |
| 21 | +} |
| 22 | + |
| 23 | +export function isPipelineHarnessRealInferenceRequired(env: PipelineHarnessRuntimeEnv = process.env): boolean { |
| 24 | + return ( |
| 25 | + isDeployedRuntime(env) || |
| 26 | + isEnabled(env.BITCODE_PIPELINE_HARNESS_REQUIRE_REAL_INFERENCE) |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +export function isUsableSupabaseUrl(value: string | undefined): boolean { |
| 31 | + if (!value) return false; |
| 32 | + try { |
| 33 | + const host = new URL(value).host; |
| 34 | + return Boolean(host && host !== 'your-project.supabase.co' && !host.includes('<')); |
| 35 | + } catch { |
| 36 | + return false; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +export function readSupabaseHost(value: string | undefined): string | null { |
| 41 | + if (!value) return null; |
| 42 | + try { |
| 43 | + const host = new URL(value).host; |
| 44 | + return host || null; |
| 45 | + } catch { |
| 46 | + return null; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export function isUsableSecretValue(value: string | undefined): boolean { |
| 51 | + return typeof value === 'string' && value.trim().length > 16 && !value.includes('<'); |
| 52 | +} |
| 53 | + |
| 54 | +export function readRealInferenceProfile(value: string | undefined): string { |
| 55 | + return String(value || '').trim().toLowerCase(); |
| 56 | +} |
| 57 | + |
| 58 | +export function assertDatabaseStreamingEnvironment(env: Record<string, string>): void { |
| 59 | + const url = env.SUPABASE_URL || env.NEXT_PUBLIC_SUPABASE_URL; |
| 60 | + const key = env.SUPABASE_SERVICE_ROLE_KEY || env.SUPABASE_SECRET_KEY || env.SUPABASE_ADMIN_KEY; |
| 61 | + if (!isUsableSupabaseUrl(url) || !isUsableSecretValue(key)) { |
| 62 | + throw new Error( |
| 63 | + 'Pipeline harness database streaming requires a non-placeholder Supabase URL and service-role key.' |
| 64 | + ); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export function assertRealInferenceEnvironment(env: Record<string, string>): void { |
| 69 | + if (!isPipelineHarnessRealInferenceRequired(env)) return; |
| 70 | + if (!isEnabled(env.BITCODE_ASSET_PACK_REAL_INFERENCE)) { |
| 71 | + throw new Error( |
| 72 | + 'Staging pipeline harness requires BITCODE_ASSET_PACK_REAL_INFERENCE=1 so Read/Fit QA cannot run deterministic bring-up branches.' |
| 73 | + ); |
| 74 | + } |
| 75 | + if (!env.OPENAI_API_KEY) { |
| 76 | + throw new Error( |
| 77 | + 'Staging pipeline harness requires OPENAI_API_KEY for real AssetPack PTRR inference.' |
| 78 | + ); |
| 79 | + } |
| 80 | + if (readRealInferenceProfile(env.BITCODE_ASSET_PACK_REAL_INFERENCE_PROFILE) !== 'bounded') { |
| 81 | + throw new Error( |
| 82 | + 'The current Terminal harness route requires BITCODE_ASSET_PACK_REAL_INFERENCE_PROFILE=bounded. Full profile runs are scoped to the later async sandbox completion gate where the sandbox pushes finished state to a server-side stream handler.' |
| 83 | + ); |
| 84 | + } |
| 85 | + const budgetMs = Number(env.BITCODE_PIPELINE_HARNESS_MAX_RUNTIME_MS || 240000); |
| 86 | + if (!Number.isFinite(budgetMs) || budgetMs <= 0) { |
| 87 | + throw new Error('BITCODE_PIPELINE_HARNESS_MAX_RUNTIME_MS must be a positive millisecond budget.'); |
| 88 | + } |
| 89 | + if (budgetMs > 600000) { |
| 90 | + throw new Error( |
| 91 | + 'BITCODE_PIPELINE_HARNESS_MAX_RUNTIME_MS must be <= 600000 when real-inference route strictness is required so the caller can still collect artifacts.' |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +export function normalizeModelEnvironment(env: Record<string, string>): void { |
| 97 | + const provider = env.BITCODE_LLM_PROVIDER?.trim().toLowerCase(); |
| 98 | + if (provider && !hasModelProviderCredential(provider, env)) { |
| 99 | + delete env.BITCODE_LLM_PROVIDER; |
| 100 | + delete env.BITCODE_LLM_MODEL; |
| 101 | + } |
| 102 | + |
| 103 | + if (!env.BITCODE_LLM_PROVIDER && env.OPENAI_API_KEY) { |
| 104 | + env.BITCODE_LLM_PROVIDER = 'openai'; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +function hasModelProviderCredential(provider: string, env: Record<string, string>): boolean { |
| 109 | + switch (provider) { |
| 110 | + case 'openai': |
| 111 | + return Boolean(env.OPENAI_API_KEY); |
| 112 | + case 'anthropic': |
| 113 | + return Boolean(env.ANTHROPIC_API_KEY); |
| 114 | + case 'google': |
| 115 | + return Boolean(env.GOOGLE_GENERATIVE_AI_API_KEY || env.GEMINI_API_KEY || env.GOOGLE_API_KEY); |
| 116 | + default: |
| 117 | + return true; |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +export function summarizeHarnessPreflight( |
| 122 | + body: PipelineHarnessPreflightBody, |
| 123 | + env: PipelineHarnessRuntimeEnv = process.env |
| 124 | +): Record<string, unknown> { |
| 125 | + const supabaseUrl = env.SUPABASE_URL || env.NEXT_PUBLIC_SUPABASE_URL; |
| 126 | + const serviceRole = |
| 127 | + env.SUPABASE_SERVICE_ROLE_KEY || |
| 128 | + env.SUPABASE_SECRET_KEY || |
| 129 | + env.SUPABASE_ADMIN_KEY; |
| 130 | + const budgetMs = Number(env.BITCODE_PIPELINE_HARNESS_MAX_RUNTIME_MS || 240000); |
| 131 | + const realInferenceProfile = readRealInferenceProfile( |
| 132 | + env.BITCODE_ASSET_PACK_REAL_INFERENCE_PROFILE |
| 133 | + ); |
| 134 | + const realInferenceRequired = isPipelineHarnessRealInferenceRequired(env); |
| 135 | + return { |
| 136 | + repositoryFullName: body.repositoryFullName || null, |
| 137 | + sourceBranch: body.sourceBranch || null, |
| 138 | + sourceCommit: body.sourceCommit || null, |
| 139 | + readId: body.readId || null, |
| 140 | + depositId: body.depositId || null, |
| 141 | + realInferenceEnabled: isEnabled(env.BITCODE_ASSET_PACK_REAL_INFERENCE), |
| 142 | + realInferenceRequired, |
| 143 | + openaiCredentialProvided: isUsableSecretValue(env.OPENAI_API_KEY), |
| 144 | + supabaseUrlProvided: isUsableSupabaseUrl(supabaseUrl), |
| 145 | + supabaseHost: readSupabaseHost(supabaseUrl), |
| 146 | + supabaseServiceRoleProvided: isUsableSecretValue(serviceRole), |
| 147 | + runtimeBudgetMs: Number.isFinite(budgetMs) ? budgetMs : null, |
| 148 | + realInferenceProfile: realInferenceProfile || (realInferenceRequired ? 'bounded' : null), |
| 149 | + fullProfileRequiresAsyncCompletion: |
| 150 | + realInferenceRequired && |
| 151 | + realInferenceProfile === 'full', |
| 152 | + deployedRuntime: isDeployedRuntime(env), |
| 153 | + }; |
| 154 | +} |
0 commit comments