11// @ts -nocheck
22import { randomUUID } from "node:crypto" ;
3- import { basename , dirname } from "node:path" ;
3+ import { basename } from "node:path" ;
44import { asc , eq } from "drizzle-orm" ;
55import {
66 DEFAULT_WORKSPACE_MODEL_GROUPS ,
@@ -11,7 +11,6 @@ import {
1111import { getActorRuntimeContext } from "../context.js" ;
1212import { getOrCreateOrganization , getOrCreateTaskSandbox , getOrCreateUser , getTaskSandbox , selfTask } from "../handles.js" ;
1313import { logActorInfo , logActorWarning , resolveErrorMessage } from "../logging.js" ;
14- import { SANDBOX_REPO_CWD } from "../sandbox/index.js" ;
1514import { resolveSandboxProviderId } from "../../sandbox-config.js" ;
1615import { getBetterAuthService } from "../../services/better-auth.js" ;
1716import { resolveOrganizationGithubAuth } from "../../services/github-auth.js" ;
@@ -183,9 +182,9 @@ async function injectGitCredentials(sandbox: any, login: string, email: string,
183182 "set -euo pipefail" ,
184183 `git config --global user.name ${ JSON . stringify ( login ) } ` ,
185184 `git config --global user.email ${ JSON . stringify ( email ) } ` ,
186- `git config --global credential.helper 'store --file=/home/sandbox /.git-token'` ,
187- `printf '%s\\n' ${ JSON . stringify ( `https://${ login } :${ token } @github.com` ) } > /home/sandbox /.git-token` ,
188- `chmod 600 /home/sandbox /.git-token` ,
185+ `git config --global credential.helper 'store --file=$HOME /.git-token'` ,
186+ `printf '%s\\n' ${ JSON . stringify ( `https://${ login } :${ token } @github.com` ) } > $HOME /.git-token` ,
187+ `chmod 600 $HOME /.git-token` ,
189188 ] ;
190189 const result = await sandbox . runProcess ( {
191190 command : "bash" ,
@@ -576,6 +575,10 @@ async function getTaskSandboxRuntime(
576575 const sandbox = await getOrCreateTaskSandbox ( c , c . state . organizationId , sandboxId , { } ) ;
577576 const actorId = typeof sandbox . resolve === "function" ? await sandbox . resolve ( ) . catch ( ( ) => null ) : null ;
578577 const switchTarget = sandboxProviderId === "local" ? `sandbox://local/${ sandboxId } ` : `sandbox://e2b/${ sandboxId } ` ;
578+
579+ // Resolve the actual repo CWD from the sandbox's $HOME (differs by provider).
580+ const repoCwdResult = await sandbox . repoCwd ( ) ;
581+ const cwd = repoCwdResult ?. cwd ?? "$HOME/repo" ;
579582 const now = Date . now ( ) ;
580583
581584 await c . db
@@ -585,7 +588,7 @@ async function getTaskSandboxRuntime(
585588 sandboxProviderId,
586589 sandboxActorId : typeof actorId === "string" ? actorId : null ,
587590 switchTarget,
588- cwd : SANDBOX_REPO_CWD ,
591+ cwd,
589592 createdAt : now ,
590593 updatedAt : now ,
591594 } )
@@ -595,7 +598,7 @@ async function getTaskSandboxRuntime(
595598 sandboxProviderId,
596599 sandboxActorId : typeof actorId === "string" ? actorId : null ,
597600 switchTarget,
598- cwd : SANDBOX_REPO_CWD ,
601+ cwd,
599602 updatedAt : now ,
600603 } ,
601604 } )
@@ -606,7 +609,7 @@ async function getTaskSandboxRuntime(
606609 . set ( {
607610 activeSandboxId : sandboxId ,
608611 activeSwitchTarget : switchTarget ,
609- activeCwd : SANDBOX_REPO_CWD ,
612+ activeCwd : cwd ,
610613 updatedAt : now ,
611614 } )
612615 . where ( eq ( taskRuntime . id , 1 ) )
@@ -617,7 +620,7 @@ async function getTaskSandboxRuntime(
617620 sandboxId,
618621 sandboxProviderId,
619622 switchTarget,
620- cwd : SANDBOX_REPO_CWD ,
623+ cwd,
621624 } ;
622625}
623626
@@ -648,15 +651,15 @@ async function ensureSandboxRepo(c: any, sandbox: any, record: any, opts?: { ski
648651 logActorInfo ( "task.sandbox" , "resolveAuth+metadata" , { durationMs : Math . round ( performance . now ( ) - t0 ) } ) ;
649652
650653 const baseRef = metadata . defaultBranch ?? "main" ;
651- const sandboxRepoRoot = dirname ( SANDBOX_REPO_CWD ) ;
654+ // Use $HOME inside the shell script so the path resolves correctly regardless
655+ // of which user the sandbox runs as (E2B: "user", local Docker: "sandbox").
652656 const script = [
653657 "set -euo pipefail" ,
654- `mkdir -p ${ JSON . stringify ( sandboxRepoRoot ) } ` ,
658+ 'REPO_DIR="$HOME/repo"' ,
659+ 'mkdir -p "$HOME"' ,
655660 "git config --global credential.helper '!f() { echo username=x-access-token; echo password=${GH_TOKEN:-$GITHUB_TOKEN}; }; f'" ,
656- `if [ ! -d ${ JSON . stringify ( `${ SANDBOX_REPO_CWD } /.git` ) } ]; then rm -rf ${ JSON . stringify ( SANDBOX_REPO_CWD ) } && git clone ${ JSON . stringify (
657- metadata . remoteUrl ,
658- ) } ${ JSON . stringify ( SANDBOX_REPO_CWD ) } ; fi`,
659- `cd ${ JSON . stringify ( SANDBOX_REPO_CWD ) } ` ,
661+ `if [ ! -d "$REPO_DIR/.git" ]; then rm -rf "$REPO_DIR" && git clone ${ JSON . stringify ( metadata . remoteUrl ) } "$REPO_DIR"; fi` ,
662+ 'cd "$REPO_DIR"' ,
660663 "git fetch origin --prune" ,
661664 `if git show-ref --verify --quiet refs/remotes/origin/${ JSON . stringify ( record . branchName ) . slice ( 1 , - 1 ) } ; then target_ref=${ JSON . stringify (
662665 `origin/${ record . branchName } ` ,
0 commit comments