@@ -193,17 +193,14 @@ export const OpencodePlugin = define<
193193 const skills = yield * Skill . Service
194194 const cache = yield * SharedCache . Service
195195 const scope = yield * Scope . Scope
196- const settings = yield * ConsoleAccess . Options
197196 const loading = Semaphore . makeUnsafe ( 1 )
198197 type ActiveConnection = Effect . Success < ReturnType < typeof ctx . integration . connection . active > >
199198 let snapshot : {
200199 // The Console's regular OpenCode config, merged as an integration config layer.
201200 remote : typeof ConsoleConfig . Type | undefined
202201 connection : ActiveConnection
203202 organization : string | undefined
204- // Fingerprint of an API key, so a key replaced in place counts as a new sign-in.
205- access : string | undefined
206- } = { remote : undefined , connection : undefined , organization : undefined , access : undefined }
203+ } = { remote : undefined , connection : undefined , organization : undefined }
207204 let instructions : InstructionDiscovery . File [ ] = [ ]
208205 // Tagged with the account and workspace they were downloaded for, so a switch hides them right away.
209206 let librarySkills : { owner : string ; items : LibrarySkill [ ] } = { owner : "" , items : [ ] }
@@ -216,18 +213,11 @@ export const OpencodePlugin = define<
216213 let explainers : Effect . Success < ReturnType < typeof ctx . session . hook > > [ ] = [ ]
217214
218215 // Every project signed in to the same workspace shares one fetch, reused while it is younger than `maxAge`.
219- // A failed load reports only its connection: what it falls back to depends on the snapshot, which is read under
220- // `loading` when the outcome is applied.
221- const load = Effect . fn ( "OpencodePlugin.load" ) ( function * (
222- maxAge : Duration . Input = Duration . minutes ( 1 ) ,
223- ) : Effect . fn . Return <
224- | { readonly _tag : "Loaded" ; readonly next : typeof snapshot }
225- | { readonly _tag : "Failed" ; readonly connection : ActiveConnection }
226- > {
216+ const load = Effect . fn ( "OpencodePlugin.load" ) ( function * ( maxAge : Duration . Input = Duration . minutes ( 1 ) ) {
227217 const connection = yield * ctx . integration . connection . active ( "opencode" )
228218 if ( ! connection ) {
229219 reported = undefined
230- return { _tag : "Loaded" , next : { remote : undefined , connection, organization : undefined , access : undefined } }
220+ return { remote : undefined , connection, organization : undefined }
231221 }
232222 const status = ( status : IntegrationConnection . Status | undefined ) => {
233223 reported = status
@@ -236,7 +226,7 @@ export const OpencodePlugin = define<
236226 return yield * ctx . integration . connection . resolve ( connection ) . pipe (
237227 Effect . flatMap ( ( credential ) => {
238228 if ( ! credential || credential . type === "external" )
239- return Effect . succeed ( { remote : undefined , connection, organization : undefined , access : undefined } )
229+ return Effect . succeed ( { remote : undefined , connection, organization : undefined } )
240230 return cache
241231 . get ( {
242232 key : `opencode-console:${ workspaceKey ( connection , credential ) } ` ,
@@ -258,29 +248,25 @@ export const OpencodePlugin = define<
258248 remote,
259249 connection,
260250 organization : organizationName ( credential ) ,
261- access : accessFingerprint ( credential ) ,
262251 } ) ) ,
263252 )
264253 } ) ,
265254 Effect . tap ( ( ) => status ( undefined ) ) ,
266- Effect . map ( ( next ) => ( { _tag : "Loaded" as const , next } ) ) ,
267255 Effect . catch ( ( cause ) =>
268256 Effect . gen ( function * ( ) {
269257 // Only well-formed Console errors change the connection status; anything else may be transient.
270258 const next = connectionStatus ( cause )
271259 if ( next ) yield * status ( next )
272260 yield * Effect . logWarning ( "failed to load OpenCode provider config" , { cause } )
273- return { _tag : "Failed" as const , connection }
261+ // A load that fails for the connection already in place keeps its last config: dropping it
262+ // would lift organization policy while personal credentials keep working.
263+ return IntegrationConnection . key ( connection ) === IntegrationConnection . key ( snapshot . connection )
264+ ? { remote : snapshot . remote , connection, organization : snapshot . organization }
265+ : { remote : undefined , connection, organization : undefined }
274266 } ) ,
275267 ) ,
276268 )
277269 } )
278- // A load that fails for the connection already in place keeps its last config: dropping it would lift
279- // organization policy while personal credentials keep working.
280- const fallback = ( connection : ActiveConnection ) : typeof snapshot =>
281- IntegrationConnection . key ( connection ) === IntegrationConnection . key ( snapshot . connection )
282- ? { ...snapshot , connection }
283- : { remote : undefined , connection, organization : undefined , access : undefined }
284270 // The Console document as config layered it. Its providers apply at this plugin's position in the provider chain,
285271 // while the rest of the document merges through config.
286272 let integrated : ConfigIntegration . Document | undefined
@@ -422,8 +408,7 @@ export const OpencodePlugin = define<
422408 editor . method . update ( { integrationID : "opencode" , method : { type : "key" , label : "API key (service account)" } } )
423409 } )
424410
425- const initial = yield * load ( )
426- snapshot = initial . _tag === "Loaded" ? initial . next : fallback ( initial . connection )
411+ snapshot = yield * load ( )
427412 yield * publish ( snapshot )
428413 yield * ctx . provider . transform ( ( providers ) => {
429414 if ( integrated ) ConfigProviderPlugin . integrate ( providers , integrated )
@@ -478,27 +463,15 @@ export const OpencodePlugin = define<
478463 { concurrency : 5 , discard : true } ,
479464 )
480465 } )
466+ const refresh = ( ) => loading . withPermit ( load ( ) . pipe ( Effect . andThen ( apply ) ) )
481467 // Annotated: `apply` re-registers the hooks that call `check`, which would otherwise make inference circular.
482- // Loads run outside `loading`, so a slow one never holds up another check. `wait` bounds the load: one still
483- // running carries on in `SharedCache`, retries included, for the next check, while the current snapshot stays.
484- const check = ( maxAge ?: Duration . Input , wait ?: Duration . Input ) : Effect . Effect < void > =>
485- Effect . gen ( function * ( ) {
486- const outcome =
487- wait === undefined
488- ? yield * load ( maxAge )
489- : Option . getOrUndefined ( yield * load ( maxAge ) . pipe ( Effect . timeoutOption ( wait ) ) )
490- if ( ! outcome ) return
491- yield * loading . withPermit (
492- Effect . gen ( function * ( ) {
493- // A switch while this load ran makes it stale; the switch's own check applies the new connection.
494- const loaded = outcome . _tag === "Loaded" ? outcome . next . connection : outcome . connection
495- const active = yield * ctx . integration . connection . active ( "opencode" )
496- if ( IntegrationConnection . key ( loaded ) !== IntegrationConnection . key ( active ) ) return
497- const next = outcome . _tag === "Loaded" ? outcome . next : fallback ( outcome . connection )
498- if ( ! Equal . equals ( snapshot , next ) ) yield * apply ( next )
499- } ) ,
500- )
501- } )
468+ const check = ( maxAge ?: Duration . Input ) : Effect . Effect < void > =>
469+ loading . withPermit (
470+ Effect . gen ( function * ( ) {
471+ const next = yield * load ( maxAge )
472+ if ( ! Equal . equals ( snapshot , next ) ) yield * apply ( next )
473+ } ) ,
474+ )
502475
503476 // Console inference rejects a missing SSO sign-in with a generic 401/403. Re-check the Console config, which
504477 // reports the reason, and replace the response with instructions when the connection needs the user.
@@ -529,24 +502,18 @@ export const OpencodePlugin = define<
529502 )
530503 } )
531504 yield * guard ( snapshot )
532- // A prompt after the machine slept or the host suspended refreshes stale Console config, and the MCP gateway
533- // tokens it carries, before the turn's tool calls run. An unreachable Console delays the prompt by at most
534- // 3 seconds; the turn then runs on the config it has.
535- yield * ctx . session . hook ( "prompt" , ( ) => check ( Duration . minutes ( 1 ) , Duration . seconds ( 3 ) ) )
536505
537506 // Console config can change independently of local credential activity, so re-fetch
538507 // periodically and only rebuild the catalog and search providers when the snapshot differs.
539- // Only while connected: a signed-out location runs no timer and no credential lookups. Hosts that suspend
540- // between prompts turn the timer off with the `console.poll` server option and rely on the prompt hook.
508+ // Only while connected: a signed-out location runs no timer and no credential lookups.
541509 const polling = yield * FiberHandle . make < void , never > ( )
542510 const poll = Effect . fnUntraced ( function * ( ) {
543- if ( ! snapshot . connection || ! settings . poll ) return yield * FiberHandle . clear ( polling )
511+ if ( ! snapshot . connection ) return yield * FiberHandle . clear ( polling )
544512 yield * FiberHandle . run ( polling , Effect . sleep ( Duration . minutes ( 1 ) ) . pipe ( Effect . andThen ( check ( ) ) , Effect . forever ) , {
545513 onlyIfMissing : true ,
546514 } )
547515 } )
548- // A switch applies only a changed snapshot, so one a prompt already applied doesn't reload MCP servers mid-turn.
549- yield * ProviderRefresh . onSwitch ( "opencode" , check ( ) . pipe ( Effect . andThen ( poll ( ) ) ) )
516+ yield * ProviderRefresh . onSwitch ( "opencode" , refresh ( ) . pipe ( Effect . andThen ( poll ( ) ) ) )
550517 yield * poll ( )
551518 } ) ,
552519} )
@@ -581,8 +548,6 @@ function fetchConfig(http: HttpClient.HttpClient, value: Credential.Key | Creden
581548 } ) ,
582549 ) ,
583550 Effect . scoped ,
584- // A transient failure, which `SharedCache` retries.
585- Effect . timeout ( Duration . seconds ( 5 ) ) ,
586551 )
587552}
588553
@@ -620,14 +585,9 @@ function workspaceKey(
620585 ConsoleAccess . server ( credential ) ,
621586 IntegrationConnection . key ( connection ) ,
622587 credential . metadata ?. orgID ,
623- accessFingerprint ( credential ) ,
624588 ] )
625589}
626590
627- function accessFingerprint ( credential : Credential . Key | Credential . OAuth ) {
628- return credential . type === "key" ? SharedCache . fingerprint ( credential . key ) : undefined
629- }
630-
631591function organizationName ( credential : Credential . Value ) {
632592 return typeof credential . metadata ?. orgName === "string" ? credential . metadata . orgName : undefined
633593}
0 commit comments