@@ -24,15 +24,30 @@ const AuthFile = Schema.Struct({ "opencode-go": Schema.optionalKey(Schema.Unknow
2424const ApiAuth = Schema . Struct ( { type : Schema . Literal ( "api" ) , key : Schema . String } ) ;
2525const decodeAuthFile = Schema . decodeEffect ( Schema . fromJsonString ( AuthFile ) ) ;
2626const decodeApiAuth = Schema . decodeUnknownOption ( ApiAuth ) ;
27- // OpenCode 2's database tags a stored key "key"; auth.json tags it "api".
28- const StoredKey = Schema . Struct ( {
29- type : Schema . Literal ( "key" ) ,
30- key : Schema . String ,
31- metadata : Schema . optionalKey (
32- Schema . NullOr ( Schema . Struct ( { server : Schema . optionalKey ( Schema . Unknown ) } ) ) ,
27+ const ConsoleMetadata = Schema . optionalKey (
28+ Schema . NullOr (
29+ Schema . Struct ( {
30+ server : Schema . optionalKey ( Schema . Unknown ) ,
31+ accountID : Schema . optionalKey ( Schema . Unknown ) ,
32+ orgID : Schema . optionalKey ( Schema . Unknown ) ,
33+ } ) ,
3334 ) ,
34- } ) ;
35- const decodeStoredKey = Schema . decodeOption ( Schema . fromJsonString ( StoredKey ) ) ;
35+ ) ;
36+ // OpenCode 2's database tags a stored key "key"; auth.json tags it "api".
37+ const ConsoleCredential = Schema . Union ( [
38+ Schema . Struct ( { type : Schema . Literal ( "key" ) , key : Schema . String , metadata : ConsoleMetadata } ) ,
39+ Schema . Struct ( {
40+ type : Schema . Literal ( "oauth" ) ,
41+ access : Schema . String ,
42+ metadata : ConsoleMetadata ,
43+ } ) ,
44+ ] ) ;
45+ const decodeConsoleCredential = Schema . decodeOption ( Schema . fromJsonString ( ConsoleCredential ) ) ;
46+ // OpenCode 2 releases have signed in through both public Console addresses.
47+ const PUBLIC_CONSOLE_SERVERS = new Set ( [
48+ "https://opencode.ai/console" ,
49+ "https://console.opencode.ai" ,
50+ ] ) ;
3651const UsageWindow = Schema . Struct ( {
3752 percent : Schema . Finite ,
3853 resetsAt : Schema . DateTimeUtcFromString ,
@@ -41,9 +56,10 @@ const UsageResponse = Schema.Struct({
4156 usage : Schema . Struct ( { rolling : UsageWindow , weekly : UsageWindow , monthly : UsageWindow } ) ,
4257} ) ;
4358
44- // Select before decoding so an OAuth or self-hosted connection cannot fall
45- // through to another stored account. NULL-active rows predate the selection flag.
46- const readConsoleApiKey = ( databasePath : string ) =>
59+ // Select before decoding so a self-hosted connection cannot fall through to
60+ // another stored account. NULL-active rows predate the selection flag. T3 never
61+ // refreshes a sign-in: Console rotates refresh tokens, and OpenCode owns renewal.
62+ const readConsoleCredential = ( databasePath : string ) =>
4763 Effect . try ( ( ) => {
4864 const database = new NodeSqlite . DatabaseSync ( databasePath , { readOnly : true } ) ;
4965 try {
@@ -57,15 +73,24 @@ const readConsoleApiKey = (databasePath: string) =>
5773 . get ( ) ;
5874 const stored =
5975 typeof row ?. value === "string"
60- ? Option . getOrUndefined ( decodeStoredKey ( row . value ) )
76+ ? Option . getOrUndefined ( decodeConsoleCredential ( row . value ) )
6177 : undefined ;
62- const server = stored ?. metadata ?. server ;
78+ if ( ! stored ) return undefined ;
79+ const { server, accountID, orgID } = stored . metadata ?? { } ;
6380 if ( typeof server === "string" ) {
6481 const url = new URL ( server ) ;
65- if ( `${ url . origin } ${ url . pathname . replace ( / \/ + $ / , "" ) } ` !== "https://opencode.ai/console" )
82+ if ( ! PUBLIC_CONSOLE_SERVERS . has ( `${ url . origin } ${ url . pathname . replace ( / \/ + $ / , "" ) } ` ) )
6683 return undefined ;
6784 }
68- return stored ?. key . trim ( ) ;
85+ if ( stored . type === "key" ) return { token : stored . key . trim ( ) } ;
86+ return {
87+ token : stored . access . trim ( ) ,
88+ // Each device signs in with its own token, so only the account matches across environments.
89+ account :
90+ typeof accountID === "string"
91+ ? `${ accountID } \0${ typeof orgID === "string" ? orgID : "" } `
92+ : undefined ,
93+ } ;
6994 } finally {
7095 database . close ( ) ;
7196 }
@@ -108,11 +133,11 @@ export const readOpenCodeGoUsageLimits = Effect.fn("readOpenCodeGoUsageLimits")(
108133 // Console supplies Go credentials in OpenCode 2. Inline auth explicitly
109134 // overrides local credentials, including the Console connection.
110135 const databaseFile = env . OPENCODE_DB ?? "opencode.db" ;
111- const consoleKey =
136+ const consoleCredential =
112137 env . OPENCODE_AUTH_CONTENT || databaseFile === ":memory:"
113138 ? undefined
114- : yield * readConsoleApiKey ( path . resolve ( dataHome , "opencode" , databaseFile ) ) ;
115- let apiKey = consoleKey || goKey ;
139+ : yield * readConsoleCredential ( path . resolve ( dataHome , "opencode" , databaseFile ) ) ;
140+ let apiKey = consoleCredential ?. token || goKey ;
116141 if ( ! apiKey ) return unsupported ;
117142
118143 const client = yield * HttpClient . HttpClient ;
@@ -159,9 +184,16 @@ export const readOpenCodeGoUsageLimits = Effect.fn("readOpenCodeGoUsageLimits")(
159184 ] ;
160185 const crypto = yield * Crypto . Crypto ;
161186 // An unkeyed hash matches across environments without a shared secret. It
162- // permits offline guesses, but Go keys are randomly generated.
187+ // permits offline guesses, but Go keys are randomly generated, and a Console
188+ // sign-in hashes only account IDs, never its token.
189+ const account = apiKey === consoleCredential ?. token ? consoleCredential . account : undefined ;
163190 const credentialFingerprint = yield * crypto
164- . digest ( "SHA-256" , new TextEncoder ( ) . encode ( `opencode-go\0${ apiKey } ` ) )
191+ . digest (
192+ "SHA-256" ,
193+ new TextEncoder ( ) . encode (
194+ account ? `opencode-go\0console\0${ account } ` : `opencode-go\0${ apiKey } ` ,
195+ ) ,
196+ )
165197 . pipe ( Effect . map ( Hex . encode ) , Effect . orDie ) ;
166198 return {
167199 ...makeUsageLimits ( { checkedAt, windows } ) ,
0 commit comments