@@ -2,7 +2,7 @@ import { cpSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, re
22import { join } from "node:path" ;
33import { networkInterfaces } from "node:os" ;
44import { createHash } from "node:crypto" ;
5- import { readHandoff , assertNoMaterial , secretForms , type Handoff } from "./credential-protocol" ;
5+ import { readJobHandoff , assertNoMaterial , secretForms , type Handoff } from "./credential-protocol" ;
66import type { ContainerJob } from "./container-config" ;
77import { callMeter , redact } from "./activation-driver" ;
88import { hostAdapter } from "./host-adapters" ;
@@ -69,23 +69,24 @@ export async function runContainerChild(job: ContainerJob, mock: boolean) {
6969 } finally { environ . fill ( 0 ) ; }
7070 } ;
7171 const profile : Profile = { id : "credential-transport" , family : "credential-transport" , modelRoute : `${ job . providerId ?? "mock" } /${ job . modelId } ` ,
72- priceClass : "paid" , tariff : job . tariff ?? { inputUSDPerMillion : 1 , outputUSDPerMillion : 1 , source : "Synthetic mock tariff, not provider pricing" } ,
72+ priceClass : job . priceClass ?? "paid" , tariff : job . tariff ?? { inputUSDPerMillion : 1 , outputUSDPerMillion : 1 , source : "Synthetic mock tariff, not provider pricing" } ,
7373 configurationSource : "explicit container job" , variantSource : "explicit container job" ,
7474 entry : buildTurnOneAuthorizationRequest ( { lane : job . lane === "anthropic-cache" ? "omp" : job . lane ,
7575 carrier : job . lane === "omp" ? "pi-historian-rpc" : "hidden-completion" , provider : job . providerId ?? "mock" , modelId : job . modelId ,
7676 variant : null , temperature : null , thinkingLevel : null } , { system : "credential-transport" , formatInstruction : "credential-transport" , toolSurface : [ ] , checkToolSurface : [ ] } ) . requestedIdentity } ;
7777 try {
78- handoff = await readHandoff ( process . stdin , job . grant ) ;
79- process . stdin . destroy ( ) ;
80- key = handoff . material ; handoff . material = "" ;
78+ try { handoff = await readJobHandoff ( process . stdin , job . grant ) ; }
79+ finally { process . stdin . destroy ( ) ; }
80+ if ( handoff ) { key = handoff . material ; handoff . material = "" ; }
8181 assertNoMaterial ( "reader_environ" , readFileSync ( `/proc/${ process . pid } /environ` ) , key ) ;
8282 for ( const [ name , path ] of Object . entries ( env ) ) mkdirSync ( name === "OPENCODE_DB" ? join ( path , ".." ) : path , { recursive : true , mode : 0o700 } ) ;
8383 if ( existsSync ( join ( install , "host-sdk" ) ) ) cpSync ( join ( install , "host-sdk" ) , env . XDG_CONFIG_HOME , { recursive : true } ) ;
8484 let endpoint = job . providerURL ;
8585 if ( mock ) {
8686 provider = mockProvider ( ( ) => "Mock credential transport acknowledged." , undefined , ( observed ) => {
8787 const expectedHeader = job . lane === "anthropic-cache" ? "x-api-key" : "authorization" ;
88- headers . push ( { expectedHeader, matched : observed . get ( expectedHeader ) === ( expectedHeader === "authorization" ? `Bearer ${ key } ` : key ) ,
88+ headers . push ( { expectedHeader, matched : job . grant ? observed . get ( expectedHeader ) === ( expectedHeader === "authorization" ? `Bearer ${ key } ` : key )
89+ : observed . get ( "authorization" ) === "Bearer public" && ! observed . has ( "x-api-key" ) ,
8990 authorizationMatched : observed . get ( "authorization" ) === `Bearer ${ key } ` , apiKeyMatched : observed . get ( "x-api-key" ) === key ,
9091 presentHeaders : [ "authorization" , "x-api-key" ] . filter ( ( name ) => observed . has ( name ) ) } ) ;
9192 } ) ;
@@ -133,17 +134,18 @@ export async function runContainerChild(job: ContainerJob, mock: boolean) {
133134 } else {
134135 const adapter = hostAdapter ( job . lane , { sourceRoot : install , mockProviderURL : endpoint ,
135136 modelId : job . modelId , providerId : job . providerId ?? "mock" , providerPackage : job . providerPackage ,
136- piApi : job . piApi , piAuth : job . grant . kind === "api_key" ? "apiKey" : "oauth" ,
137- outputCeiling : job . outputCeiling , providerKeyEnv : "TWO_PASS_PROVIDER_API_KEY" ,
137+ piApi : job . piApi , piAuth : job . grant ?. kind === "api_key" ? "apiKey" : "oauth" ,
138+ outputCeiling : job . outputCeiling , providerKeyEnv : job . grant ? "TWO_PASS_PROVIDER_API_KEY" : undefined ,
139+ grantFree : ! job . grant ,
138140 paidProvider : ! mock , foregroundOnly : mock , timeoutMs : 120_000 ,
139141 onProviderAttempt ( providerHost ) { console . log ( JSON . stringify ( { providerAttempt : { providerHost } } ) ) ; } ,
140142 onProcessObserved : ( pid ) => observeProcess ( pid ) ,
141143 magicContext : mock ? { enabled : false } : job . magicContext ?? { } } ) ;
142144 const entry = join ( install , job . lane === "omp" ? "packages/pi-plugin/dist/index.js" : job . lane === "opencode-2" ? "packages/plugin/dist/v2/server.js" : "packages/plugin/dist/index.js" ) ;
143- const childEnv = { ...env , TWO_PASS_PROVIDER_API_KEY : key } ;
145+ const childEnv = { ...env , ... ( job . grant ? { TWO_PASS_PROVIDER_API_KEY : key } : { } ) } ;
144146 host = await adapter . start ( { installRoot : install , pluginEntrypoint : entry , env : childEnv , meter : callMeter ( ledger , profile ) } ) ;
145- observeProcess ( host . pid , "TWO_PASS_PROVIDER_API_KEY" , true ) ;
146- childEnvironment = { pid : host . pid , allowedVariable : "TWO_PASS_PROVIDER_API_KEY" , environContainsKey : true , argvClean : true } ;
147+ observeProcess ( host . pid , "TWO_PASS_PROVIDER_API_KEY" , ! ! job . grant ) ;
148+ childEnvironment = { pid : host . pid , allowedVariable : job . grant ? "TWO_PASS_PROVIDER_API_KEY" : null , environContainsKey : ! ! job . grant , argvClean : true } ;
147149 result = await host . runSession ( job . prompt ) ;
148150 }
149151 if ( mock && ( ! headers . length || headers . some ( ( entry ) => ! entry . matched ) ) ) throw new Error ( "credential_provider_header_mismatch" ) ;
0 commit comments