@@ -17,6 +17,7 @@ import { ensureDockerImage } from "./docker-image.js"
1717import { resolvePathFromCwd } from "./path-helpers.js"
1818import { withFsPathContext } from "./runtime.js"
1919import { autoSyncState } from "./state-repo.js"
20+ import { readFileStringIfPresent , writeFileStringEnsuringParent } from "./volatile-files.js"
2021
2122type ClaudeRuntime = FileSystem . FileSystem | Path . Path | CommandExecutor . CommandExecutor
2223type ClaudeAuthMethod = "none" | "oauth-token" | "claude-ai-session"
@@ -26,6 +27,7 @@ type ClaudeAccountContext = {
2627 readonly accountPath : string
2728 readonly cwd : string
2829 readonly fs : FileSystem . FileSystem
30+ readonly path : Path . Path
2931}
3032
3133export const claudeAuthRoot = ".docker-git/.orch/auth/claude"
@@ -46,23 +48,29 @@ const claudeNestedCredentialsPath = (accountPath: string): string =>
4648
4749const syncClaudeCredentialsFile = (
4850 fs : FileSystem . FileSystem ,
51+ path : Path . Path ,
4952 accountPath : string
5053) : Effect . Effect < void , PlatformError > =>
5154 Effect . gen ( function * ( _ ) {
5255 const nestedPath = claudeNestedCredentialsPath ( accountPath )
5356 const rootPath = claudeCredentialsPath ( accountPath )
5457 const nestedExists = yield * _ ( isRegularFile ( fs , nestedPath ) )
5558 if ( nestedExists ) {
56- yield * _ ( fs . copyFile ( nestedPath , rootPath ) )
57- yield * _ ( fs . chmod ( rootPath , 0o600 ) , Effect . orElseSucceed ( ( ) => void 0 ) )
59+ const nestedText = yield * _ ( readFileStringIfPresent ( fs , nestedPath ) )
60+ if ( nestedText !== null ) {
61+ yield * _ ( writeFileStringEnsuringParent ( fs , path , rootPath , nestedText ) )
62+ yield * _ ( fs . chmod ( rootPath , 0o600 ) , Effect . orElseSucceed ( ( ) => void 0 ) )
63+ }
5864 return
5965 }
6066
6167 const rootExists = yield * _ ( isRegularFile ( fs , rootPath ) )
6268 if ( rootExists ) {
63- const nestedDirPath = `${ accountPath } /${ claudeCredentialsDirName } `
64- yield * _ ( fs . makeDirectory ( nestedDirPath , { recursive : true } ) )
65- yield * _ ( fs . copyFile ( rootPath , nestedPath ) )
69+ const rootText = yield * _ ( readFileStringIfPresent ( fs , rootPath ) )
70+ if ( rootText === null ) {
71+ return
72+ }
73+ yield * _ ( writeFileStringEnsuringParent ( fs , path , nestedPath , rootText ) )
6674 yield * _ ( fs . chmod ( nestedPath , 0o600 ) , Effect . orElseSucceed ( ( ) => void 0 ) )
6775 }
6876 } )
@@ -108,6 +116,7 @@ const readOauthToken = (
108116
109117const resolveClaudeAuthMethod = (
110118 fs : FileSystem . FileSystem ,
119+ path : Path . Path ,
111120 accountPath : string
112121) : Effect . Effect < ClaudeAuthMethod , PlatformError > =>
113122 Effect . gen ( function * ( _ ) {
@@ -117,7 +126,7 @@ const resolveClaudeAuthMethod = (
117126 return "oauth-token"
118127 }
119128
120- yield * _ ( syncClaudeCredentialsFile ( fs , accountPath ) )
129+ yield * _ ( syncClaudeCredentialsFile ( fs , path , accountPath ) )
121130 const hasCredentials = yield * _ ( isRegularFile ( fs , claudeCredentialsPath ( accountPath ) ) )
122131 return hasCredentials ? "claude-ai-session" : "none"
123132 } )
@@ -187,7 +196,7 @@ const withClaudeAuth = <A, E>(
187196 buildLabel : "claude auth"
188197 } )
189198 )
190- return yield * _ ( run ( { accountLabel, accountPath, cwd, fs } ) )
199+ return yield * _ ( run ( { accountLabel, accountPath, cwd, fs, path } ) )
191200 } )
192201 )
193202
@@ -249,7 +258,7 @@ export const authClaudeLogin = (
249258 command : AuthClaudeLoginCommand
250259) : Effect . Effect < void , AuthError | CommandFailedError | PlatformError , ClaudeRuntime > => {
251260 const accountLabel = normalizeAccountLabel ( command . label , "default" )
252- return withClaudeAuth ( command , ( { accountPath, cwd, fs } ) =>
261+ return withClaudeAuth ( command , ( { accountPath, cwd, fs, path } ) =>
253262 Effect . gen ( function * ( _ ) {
254263 const token = yield * _ (
255264 runClaudeOauthLoginWithPrompt ( cwd , accountPath , {
@@ -259,7 +268,7 @@ export const authClaudeLogin = (
259268 )
260269 yield * _ ( fs . writeFileString ( claudeOauthTokenPath ( accountPath ) , `${ token } \n` ) )
261270 yield * _ ( fs . chmod ( claudeOauthTokenPath ( accountPath ) , 0o600 ) , Effect . orElseSucceed ( ( ) => void 0 ) )
262- yield * _ ( resolveClaudeAuthMethod ( fs , accountPath ) )
271+ yield * _ ( resolveClaudeAuthMethod ( fs , path , accountPath ) )
263272 const probeExitCode = yield * _ ( runClaudePingProbeExitCode ( cwd , accountPath , token ) )
264273 if ( probeExitCode !== 0 ) {
265274 yield * _ (
@@ -289,9 +298,9 @@ export const authClaudeLogin = (
289298export const authClaudeStatus = (
290299 command : AuthClaudeStatusCommand
291300) : Effect . Effect < void , CommandFailedError | PlatformError , ClaudeRuntime > =>
292- withClaudeAuth ( command , ( { accountLabel, accountPath, cwd, fs } ) =>
301+ withClaudeAuth ( command , ( { accountLabel, accountPath, cwd, fs, path } ) =>
293302 Effect . gen ( function * ( _ ) {
294- const method = yield * _ ( resolveClaudeAuthMethod ( fs , accountPath ) )
303+ const method = yield * _ ( resolveClaudeAuthMethod ( fs , path , accountPath ) )
295304 if ( method === "none" ) {
296305 yield * _ ( Effect . log ( `Claude not connected (${ accountLabel } ).` ) )
297306 return
0 commit comments