@@ -37,6 +37,7 @@ import * as Option from "effect/Option";
3737import * as Ref from "effect/Ref" ;
3838import * as Stream from "effect/Stream" ;
3939import * as Schema from "effect/Schema" ;
40+ import * as SqlClient from "effect/sql/SqlClient" ;
4041import * as TestClock from "effect/testing/TestClock" ;
4142
4243import * as GitWorkflow from "../git/GitWorkflowService.ts" ;
@@ -86,6 +87,7 @@ const otherProject = {
8687 ...project ,
8788 id : otherProjectId ,
8889 title : "Other" ,
90+ workspaceRoot : "/other-repo" ,
8991} as const ;
9092
9193const adapter = {
@@ -100,6 +102,7 @@ interface HarnessOptions {
100102 readonly managedFolders ?: Layer . Layer < ManagedProjectFolders . ManagedProjectFolders > ;
101103 readonly createWorktree ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "createWorktree" ] ;
102104 readonly fetchRemote ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "fetchRemote" ] ;
105+ readonly remoteExists ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "remoteExists" ] ;
103106 readonly hasCommit ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "hasCommit" ] ;
104107 readonly renameBranch ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "renameBranch" ] ;
105108 readonly runSetup ?: ProjectSetupScriptRunner . ProjectSetupScriptRunner [ "Service" ] [ "runForThread" ] ;
@@ -170,7 +173,7 @@ function makeHarness(options: HarnessOptions = {}) {
170173 renameBranch,
171174 fetchRemote : options . fetchRemote ?? ( ( ) => Effect . void ) ,
172175 hasCommit : options . hasCommit ?? ( ( ) => Effect . succeed ( false ) ) ,
173- remoteExists : ( ) => Effect . succeed ( true ) ,
176+ remoteExists : options . remoteExists ?? ( ( ) => Effect . succeed ( true ) ) ,
174177 remoteBranchExists : ( ) => Effect . succeed ( true ) ,
175178 removeWorktree,
176179 resolveRemoteTrackingCommit : ( ) =>
@@ -188,6 +191,7 @@ function makeHarness(options: HarnessOptions = {}) {
188191 options . managedFolders ??
189192 Layer . mock ( ManagedProjectFolders . ManagedProjectFolders ) ( {
190193 namedProjectsRoot : "/projects" ,
194+ isScratchProject : ( ) => Effect . succeed ( false ) ,
191195 folderForThread : ( ) => Effect . succeed ( Option . none ( ) ) ,
192196 } ) ,
193197 ) ;
@@ -1104,6 +1108,183 @@ it.effect("runs a Scratch thread launched at the root in its own folder", () =>
11041108 } ) ,
11051109) ;
11061110
1111+ // Stands `projectId` in for the Scratch project: a plain folder, not a Git
1112+ // repository, so Git fails there with the error a real one raises.
1113+ function scratchHarness ( ) {
1114+ const claimed : Array < ThreadId > = [ ] ;
1115+ const scratchRoot = project . workspaceRoot ;
1116+ const notARepository = ( operation : string , cwd : string ) =>
1117+ new GitCommandError ( {
1118+ operation,
1119+ command : "vcs-route" ,
1120+ cwd,
1121+ detail : "Failed to resolve the VCS driver for this Git command." ,
1122+ } ) ;
1123+ const harness = makeHarness ( {
1124+ managedFolders : Layer . mock ( ManagedProjectFolders . ManagedProjectFolders ) ( {
1125+ namedProjectsRoot : "/projects" ,
1126+ isScratchProject : ( id ) => Effect . succeed ( id === projectId ) ,
1127+ folderForThread : ( input ) =>
1128+ Effect . sync ( ( ) => {
1129+ if ( input . projectId !== projectId ) return Option . none ( ) ;
1130+ claimed . push ( input . threadId ) ;
1131+ return Option . some ( `/scratch/folder-${ claimed . length } ` ) ;
1132+ } ) ,
1133+ } ) ,
1134+ remoteExists : ( input ) =>
1135+ input . cwd === scratchRoot
1136+ ? Effect . fail ( notARepository ( "GitWorkflowService.remoteExists" , input . cwd ) )
1137+ : Effect . succeed ( true ) ,
1138+ createWorktree : ( input ) =>
1139+ input . cwd === scratchRoot
1140+ ? Effect . fail ( notARepository ( "GitWorkflowService.createWorktree" , input . cwd ) )
1141+ : Effect . succeed ( {
1142+ worktree : {
1143+ path : "/other-repo-worktrees/feature" ,
1144+ refName : input . newRefName ,
1145+ headSha : "abc" ,
1146+ } ,
1147+ } as never ) ,
1148+ } ) ;
1149+ const layerScheduledTasks = ScheduledTasks . layer . pipe (
1150+ Layer . provide (
1151+ Layer . mergeAll (
1152+ harness . layer ,
1153+ NodeCrypto . layer ,
1154+ Scheduler . layer ,
1155+ Layer . mock ( SecretRequests . SecretRequests ) ( { } ) ,
1156+ ) ,
1157+ ) ,
1158+ ) ;
1159+ return {
1160+ ...harness ,
1161+ claimed,
1162+ layer : Layer . mergeAll ( harness . layer , layerScheduledTasks ) ,
1163+ } ;
1164+ }
1165+
1166+ const worktreeStrategy = { type : "worktree" , baseRef : "main" , startFromOrigin : true } as const ;
1167+
1168+ /** Settles once preparation releases the run, or fails it. */
1169+ const preparationSettled = ( threadId : ThreadId ) =>
1170+ Effect . gen ( function * ( ) {
1171+ const threads = yield * ThreadManagement . ThreadManagementService ;
1172+ yield * threads . streamStoredEventsFrom ( { threadId } ) . pipe (
1173+ Stream . filter (
1174+ ( stored ) =>
1175+ stored . event . type === "run.updated" &&
1176+ ( stored . commandId ?. endsWith ( ":release" ) === true ||
1177+ stored . event . payload . status === "failed" ) ,
1178+ ) ,
1179+ Stream . runHead ,
1180+ ) ;
1181+ return yield * threads . getThreadProjection ( threadId ) ;
1182+ } ) ;
1183+
1184+ function scheduledTaskInput ( id : string , targetProjectId : ProjectId ) {
1185+ return {
1186+ id : ScheduledTaskId . make ( id ) ,
1187+ title : "Daily summary" ,
1188+ prompt : "Summarize the day." ,
1189+ enabled : false ,
1190+ schedule : { type : "interval" as const , everyMs : 60_000 } ,
1191+ projectId : targetProjectId ,
1192+ threadId : null ,
1193+ workspaceStrategy : worktreeStrategy ,
1194+ modelSelection,
1195+ runtimeMode : "full-access" as const ,
1196+ interactionMode : "default" as const ,
1197+ } ;
1198+ }
1199+
1200+ it . effect ( "launches a worktree request for Scratch at the root, in its own folder" , ( ) => {
1201+ const harness = scratchHarness ( ) ;
1202+ return Effect . gen ( function * ( ) {
1203+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
1204+ const launched = yield * launches . launch (
1205+ launchInput ( {
1206+ command : "command:launch:scratch-worktree" ,
1207+ thread : "thread:launch:scratch-worktree" ,
1208+ message : "Tidy my notes" ,
1209+ workspace : worktreeStrategy ,
1210+ } ) ,
1211+ ) ;
1212+ const scratch = yield * preparationSettled ( launched . threadId ) ;
1213+ assert . equal ( scratch . thread . worktreePath , "/scratch/folder-1" ) ;
1214+ // Released past preparation; provider execution is disabled here.
1215+ assert . equal ( scratch . runs [ 0 ] ?. status , "starting" ) ;
1216+ assert . deepEqual ( scratch . runs [ 0 ] ?. workspacePreparation , {
1217+ type : "existing_worktree" ,
1218+ worktreePath : "/scratch/folder-1" ,
1219+ } ) ;
1220+ assert . equal ( harness . runSetup . mock . calls [ 0 ] ?. [ 0 ] ?. worktreePath , "/scratch/folder-1" ) ;
1221+ assert . equal ( harness . createWorktree . mock . calls . length , 0 ) ;
1222+
1223+ // A Git project still gets the worktree it asked for.
1224+ const other = yield * launches . launch ( {
1225+ ...launchInput ( {
1226+ command : "command:launch:git-worktree" ,
1227+ thread : "thread:launch:git-worktree" ,
1228+ message : "Build the feature" ,
1229+ workspace : worktreeStrategy ,
1230+ } ) ,
1231+ projectId : otherProjectId ,
1232+ } ) ;
1233+ const git = yield * preparationSettled ( other . threadId ) ;
1234+ assert . equal ( git . runs [ 0 ] ?. status , "starting" ) ;
1235+ assert . equal ( git . runs [ 0 ] ?. workspacePreparation ?. type , "worktree" ) ;
1236+ assert . equal ( git . thread . worktreePath , "/other-repo-worktrees/feature" ) ;
1237+ assert . equal ( harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] ?. cwd , "/other-repo" ) ;
1238+ assert . deepEqual ( harness . claimed , [ launched . threadId ] ) ;
1239+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1240+ } ) ;
1241+
1242+ it . effect ( "stores a Scratch scheduled task at the root and keeps worktrees elsewhere" , ( ) => {
1243+ const harness = scratchHarness ( ) ;
1244+ return Effect . gen ( function * ( ) {
1245+ const tasks = yield * ScheduledTasks . ScheduledTaskService ;
1246+ const scratch = yield * tasks . upsert (
1247+ scheduledTaskInput ( "scheduled-task:scratch-upsert" , projectId ) ,
1248+ ) ;
1249+ assert . deepEqual ( scratch . task . workspaceStrategy , { type : "root" } ) ;
1250+ const git = yield * tasks . upsert (
1251+ scheduledTaskInput ( "scheduled-task:git-upsert" , otherProjectId ) ,
1252+ ) ;
1253+ assert . deepEqual ( git . task . workspaceStrategy , worktreeStrategy ) ;
1254+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1255+ } ) ;
1256+
1257+ it . effect ( "runs a Scratch scheduled task saved with a worktree in its own folder" , ( ) => {
1258+ const harness = scratchHarness ( ) ;
1259+ return Effect . gen ( function * ( ) {
1260+ const tasks = yield * ScheduledTasks . ScheduledTaskService ;
1261+ const threads = yield * ThreadManagement . ThreadManagementService ;
1262+ const sql = yield * SqlClient . SqlClient ;
1263+ const { task } = yield * tasks . upsert (
1264+ scheduledTaskInput ( "scheduled-task:scratch-stored" , projectId ) ,
1265+ ) ;
1266+ // Tasks saved before Scratch was coerced still hold a worktree strategy.
1267+ yield * sql `
1268+ UPDATE scheduled_tasks
1269+ SET workspace_strategy_json = ${ JSON . stringify ( worktreeStrategy ) }
1270+ WHERE task_id = ${ task . id }
1271+ ` ;
1272+ const ran = yield * tasks . runNow ( { id : task . id } ) ;
1273+ assert . equal ( ran . task . lastRunStatus , "succeeded" ) ;
1274+ const [ thread ] = yield * threads . listProjectThreads ( { projectId, includeSubagents : false } ) ;
1275+ assert . isDefined ( thread ) ;
1276+ const projection = yield * preparationSettled ( thread ! . id ) ;
1277+ assert . equal ( projection . runs [ 0 ] ?. status , "starting" ) ;
1278+ assert . equal ( projection . thread . worktreePath , "/scratch/folder-1" ) ;
1279+ assert . deepEqual ( projection . runs [ 0 ] ?. workspacePreparation , {
1280+ type : "existing_worktree" ,
1281+ worktreePath : "/scratch/folder-1" ,
1282+ } ) ;
1283+ assert . equal ( harness . runSetup . mock . calls [ 0 ] ?. [ 0 ] ?. worktreePath , "/scratch/folder-1" ) ;
1284+ assert . equal ( harness . createWorktree . mock . calls . length , 0 ) ;
1285+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1286+ } ) ;
1287+
11071288it . effect ( "names the worktree itself when the client provides no branch" , ( ) =>
11081289 Effect . gen ( function * ( ) {
11091290 const harness = makeHarness ( ) ;
0 commit comments