@@ -40,6 +40,7 @@ import * as Ref from "effect/Ref";
4040import * as Result from "effect/Result" ;
4141import * as Stream from "effect/Stream" ;
4242import * as Schema from "effect/Schema" ;
43+ import * as SqlClient from "effect/sql/SqlClient" ;
4344import * as TestClock from "effect/testing/TestClock" ;
4445
4546import * as GitWorkflow from "../git/GitWorkflowService.ts" ;
@@ -92,6 +93,7 @@ const otherProject = {
9293 ...project ,
9394 id : otherProjectId ,
9495 title : "Other" ,
96+ workspaceRoot : "/other-repo" ,
9597} as const ;
9698
9799const adapter = {
@@ -120,6 +122,7 @@ interface HarnessOptions {
120122 readonly managedFolders ?: Layer . Layer < ManagedProjectFolders . ManagedProjectFolders > ;
121123 readonly createWorktree ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "createWorktree" ] ;
122124 readonly fetchRemote ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "fetchRemote" ] ;
125+ readonly remoteExists ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "remoteExists" ] ;
123126 readonly hasCommit ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "hasCommit" ] ;
124127 readonly renameBranch ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "renameBranch" ] ;
125128 readonly runSetup ?: ProjectSetupScriptRunner . ProjectSetupScriptRunner [ "Service" ] [ "runForThread" ] ;
@@ -197,7 +200,7 @@ function makeHarness(options: HarnessOptions = {}) {
197200 renameBranch,
198201 fetchRemote : options . fetchRemote ?? ( ( ) => Effect . void ) ,
199202 hasCommit : options . hasCommit ?? ( ( ) => Effect . succeed ( false ) ) ,
200- remoteExists : ( ) => Effect . succeed ( true ) ,
203+ remoteExists : options . remoteExists ?? ( ( ) => Effect . succeed ( true ) ) ,
201204 remoteBranchExists : ( ) => Effect . succeed ( true ) ,
202205 removeWorktree,
203206 resolveRemoteTrackingCommit : ( ) =>
@@ -216,6 +219,7 @@ function makeHarness(options: HarnessOptions = {}) {
216219 options . managedFolders ??
217220 Layer . mock ( ManagedProjectFolders . ManagedProjectFolders ) ( {
218221 namedProjectsRoot : "/projects" ,
222+ isScratchProject : ( ) => Effect . succeed ( false ) ,
219223 folderForThread : ( ) => Effect . succeed ( Option . none ( ) ) ,
220224 } ) ,
221225 ) ;
@@ -1193,6 +1197,183 @@ it.effect("runs a Scratch thread launched at the root in its own folder", () =>
11931197 } ) ,
11941198) ;
11951199
1200+ // Stands `projectId` in for the Scratch project: a plain folder, not a Git
1201+ // repository, so Git fails there with the error a real one raises.
1202+ function scratchHarness ( ) {
1203+ const claimed : Array < ThreadId > = [ ] ;
1204+ const scratchRoot = project . workspaceRoot ;
1205+ const notARepository = ( operation : string , cwd : string ) =>
1206+ new GitCommandError ( {
1207+ operation,
1208+ command : "vcs-route" ,
1209+ cwd,
1210+ detail : "Failed to resolve the VCS driver for this Git command." ,
1211+ } ) ;
1212+ const harness = makeHarness ( {
1213+ managedFolders : Layer . mock ( ManagedProjectFolders . ManagedProjectFolders ) ( {
1214+ namedProjectsRoot : "/projects" ,
1215+ isScratchProject : ( id ) => Effect . succeed ( id === projectId ) ,
1216+ folderForThread : ( input ) =>
1217+ Effect . sync ( ( ) => {
1218+ if ( input . projectId !== projectId ) return Option . none ( ) ;
1219+ claimed . push ( input . threadId ) ;
1220+ return Option . some ( `/scratch/folder-${ claimed . length } ` ) ;
1221+ } ) ,
1222+ } ) ,
1223+ remoteExists : ( input ) =>
1224+ input . cwd === scratchRoot
1225+ ? Effect . fail ( notARepository ( "GitWorkflowService.remoteExists" , input . cwd ) )
1226+ : Effect . succeed ( true ) ,
1227+ createWorktree : ( input ) =>
1228+ input . cwd === scratchRoot
1229+ ? Effect . fail ( notARepository ( "GitWorkflowService.createWorktree" , input . cwd ) )
1230+ : Effect . succeed ( {
1231+ worktree : {
1232+ path : "/other-repo-worktrees/feature" ,
1233+ refName : input . newRefName ,
1234+ headSha : "abc" ,
1235+ } ,
1236+ } as never ) ,
1237+ } ) ;
1238+ const layerScheduledTasks = ScheduledTasks . layer . pipe (
1239+ Layer . provide (
1240+ Layer . mergeAll (
1241+ harness . layer ,
1242+ NodeCrypto . layer ,
1243+ Scheduler . layer ,
1244+ Layer . mock ( SecretRequests . SecretRequests ) ( { } ) ,
1245+ ) ,
1246+ ) ,
1247+ ) ;
1248+ return {
1249+ ...harness ,
1250+ claimed,
1251+ layer : Layer . mergeAll ( harness . layer , layerScheduledTasks ) ,
1252+ } ;
1253+ }
1254+
1255+ const worktreeStrategy = { type : "worktree" , baseRef : "main" , startFromOrigin : true } as const ;
1256+
1257+ /** Settles once preparation releases the run, or fails it. */
1258+ const preparationSettled = ( threadId : ThreadId ) =>
1259+ Effect . gen ( function * ( ) {
1260+ const threads = yield * ThreadManagement . ThreadManagementService ;
1261+ yield * threads . streamStoredEventsFrom ( { threadId } ) . pipe (
1262+ Stream . filter (
1263+ ( stored ) =>
1264+ stored . event . type === "run.updated" &&
1265+ ( stored . commandId ?. endsWith ( ":release" ) === true ||
1266+ stored . event . payload . status === "failed" ) ,
1267+ ) ,
1268+ Stream . runHead ,
1269+ ) ;
1270+ return yield * threads . getThreadProjection ( threadId ) ;
1271+ } ) ;
1272+
1273+ function scheduledTaskInput ( id : string , targetProjectId : ProjectId ) {
1274+ return {
1275+ id : ScheduledTaskId . make ( id ) ,
1276+ title : "Daily summary" ,
1277+ prompt : "Summarize the day." ,
1278+ enabled : false ,
1279+ schedule : { type : "interval" as const , everyMs : 60_000 } ,
1280+ projectId : targetProjectId ,
1281+ threadId : null ,
1282+ workspaceStrategy : worktreeStrategy ,
1283+ modelSelection,
1284+ runtimeMode : "full-access" as const ,
1285+ interactionMode : "default" as const ,
1286+ } ;
1287+ }
1288+
1289+ it . effect ( "launches a worktree request for Scratch at the root, in its own folder" , ( ) => {
1290+ const harness = scratchHarness ( ) ;
1291+ return Effect . gen ( function * ( ) {
1292+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
1293+ const launched = yield * launches . launch (
1294+ launchInput ( {
1295+ command : "command:launch:scratch-worktree" ,
1296+ thread : "thread:launch:scratch-worktree" ,
1297+ message : "Tidy my notes" ,
1298+ workspace : worktreeStrategy ,
1299+ } ) ,
1300+ ) ;
1301+ const scratch = yield * preparationSettled ( launched . threadId ) ;
1302+ assert . equal ( scratch . thread . worktreePath , "/scratch/folder-1" ) ;
1303+ // Released past preparation; provider execution is disabled here.
1304+ assert . equal ( scratch . runs [ 0 ] ?. status , "starting" ) ;
1305+ assert . deepEqual ( scratch . runs [ 0 ] ?. workspacePreparation , {
1306+ type : "existing_worktree" ,
1307+ worktreePath : "/scratch/folder-1" ,
1308+ } ) ;
1309+ assert . equal ( harness . runSetup . mock . calls [ 0 ] ?. [ 0 ] ?. worktreePath , "/scratch/folder-1" ) ;
1310+ assert . equal ( harness . createWorktree . mock . calls . length , 0 ) ;
1311+
1312+ // A Git project still gets the worktree it asked for.
1313+ const other = yield * launches . launch ( {
1314+ ...launchInput ( {
1315+ command : "command:launch:git-worktree" ,
1316+ thread : "thread:launch:git-worktree" ,
1317+ message : "Build the feature" ,
1318+ workspace : worktreeStrategy ,
1319+ } ) ,
1320+ projectId : otherProjectId ,
1321+ } ) ;
1322+ const git = yield * preparationSettled ( other . threadId ) ;
1323+ assert . equal ( git . runs [ 0 ] ?. status , "starting" ) ;
1324+ assert . equal ( git . runs [ 0 ] ?. workspacePreparation ?. type , "worktree" ) ;
1325+ assert . equal ( git . thread . worktreePath , "/other-repo-worktrees/feature" ) ;
1326+ assert . equal ( harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] ?. cwd , "/other-repo" ) ;
1327+ assert . deepEqual ( harness . claimed , [ launched . threadId ] ) ;
1328+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1329+ } ) ;
1330+
1331+ it . effect ( "stores a Scratch scheduled task at the root and keeps worktrees elsewhere" , ( ) => {
1332+ const harness = scratchHarness ( ) ;
1333+ return Effect . gen ( function * ( ) {
1334+ const tasks = yield * ScheduledTasks . ScheduledTaskService ;
1335+ const scratch = yield * tasks . upsert (
1336+ scheduledTaskInput ( "scheduled-task:scratch-upsert" , projectId ) ,
1337+ ) ;
1338+ assert . deepEqual ( scratch . task . workspaceStrategy , { type : "root" } ) ;
1339+ const git = yield * tasks . upsert (
1340+ scheduledTaskInput ( "scheduled-task:git-upsert" , otherProjectId ) ,
1341+ ) ;
1342+ assert . deepEqual ( git . task . workspaceStrategy , worktreeStrategy ) ;
1343+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1344+ } ) ;
1345+
1346+ it . effect ( "runs a Scratch scheduled task saved with a worktree in its own folder" , ( ) => {
1347+ const harness = scratchHarness ( ) ;
1348+ return Effect . gen ( function * ( ) {
1349+ const tasks = yield * ScheduledTasks . ScheduledTaskService ;
1350+ const threads = yield * ThreadManagement . ThreadManagementService ;
1351+ const sql = yield * SqlClient . SqlClient ;
1352+ const { task } = yield * tasks . upsert (
1353+ scheduledTaskInput ( "scheduled-task:scratch-stored" , projectId ) ,
1354+ ) ;
1355+ // Tasks saved before Scratch was coerced still hold a worktree strategy.
1356+ yield * sql `
1357+ UPDATE scheduled_tasks
1358+ SET workspace_strategy_json = ${ JSON . stringify ( worktreeStrategy ) }
1359+ WHERE task_id = ${ task . id }
1360+ ` ;
1361+ const ran = yield * tasks . runNow ( { id : task . id } ) ;
1362+ assert . equal ( ran . task . lastRunStatus , "succeeded" ) ;
1363+ const [ thread ] = yield * threads . listProjectThreads ( { projectId, includeSubagents : false } ) ;
1364+ assert . isDefined ( thread ) ;
1365+ const projection = yield * preparationSettled ( thread ! . id ) ;
1366+ assert . equal ( projection . runs [ 0 ] ?. status , "starting" ) ;
1367+ assert . equal ( projection . thread . worktreePath , "/scratch/folder-1" ) ;
1368+ assert . deepEqual ( projection . runs [ 0 ] ?. workspacePreparation , {
1369+ type : "existing_worktree" ,
1370+ worktreePath : "/scratch/folder-1" ,
1371+ } ) ;
1372+ assert . equal ( harness . runSetup . mock . calls [ 0 ] ?. [ 0 ] ?. worktreePath , "/scratch/folder-1" ) ;
1373+ assert . equal ( harness . createWorktree . mock . calls . length , 0 ) ;
1374+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1375+ } ) ;
1376+
11961377it . effect ( "starts a thread in the main checkout without running the setup action" , ( ) =>
11971378 Effect . gen ( function * ( ) {
11981379 const harness = makeHarness ( ) ;
0 commit comments