GeezerVison is showing github.com — Merge pull request #129 from Yarden-zamir/fix/non-repository-launch-n… · TomCohenDev/t3code@780a235
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/git/GitWorkflowService.ts +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,11 @@ export class GitWorkflowService extends Context.Service< | 36 | 36 | GitWorkflowService, | 37 | 37 | { | 38 | 38 | readonly isRepository: (cwd: string) => Effect.Effect<boolean, GitManagerServiceError>; | | 39 | + /** | | 40 | + * True only when Git reports that no repository holds `cwd`. A repository | | 41 | + * that Git cannot read, such as one with dubious ownership, gives false. | | 42 | + */ | | 43 | + readonly isOutsideRepository: (cwd: string) => Effect.Effect<boolean, GitCommandError>; | 39 | 44 | readonly hasCommit: (input: { | 40 | 45 | readonly cwd: string; | 41 | 46 | readonly refName: string; | @@ -284,6 +289,21 @@ export const make = Effect.gen(function* () { | 284 | 289 | ensureGit(operation, input.cwd).pipe(Effect.andThen(run(input))); | 285 | 290 |
| 286 | 291 | return GitWorkflowService.of({ | | 292 | + isOutsideRepository: (cwd) => | | 293 | + git | | 294 | + .execute({ | | 295 | + operation: "GitWorkflowService.isOutsideRepository", | | 296 | + cwd, | | 297 | + args: ["rev-parse", "--git-dir"], | | 298 | + env: { ...process.env, LC_ALL: "C" }, | | 299 | + allowNonZeroExit: true, | | 300 | + timeoutMs: 5_000, | | 301 | + }) | | 302 | + .pipe( | | 303 | + Effect.map( | | 304 | + (result) => result.exitCode !== 0 && result.stderr.includes("not a git repository"), | | 305 | + ), | | 306 | + ), | 287 | 307 | isRepository: (cwd) => | 288 | 308 | registry.detect({ cwd }).pipe( | 289 | 309 | Effect.map((handle) => handle?.kind === "git"), | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/toolkits/project/handlers.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -200,7 +200,9 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, { | 200 | 200 | message: | 201 | 201 | error._tag === "ThreadLaunchBranchExistsError" | 202 | 202 | ? `${error.message} No thread was created. Pass a new branch name, or omit branch to let T3 Code name it.` | 203 | | - : `${error.message} No thread was created. Pass a branch, tag, or commit that exists there${ | | 203 | + : error._tag === "ThreadLaunchNotRepositoryError" | | 204 | + ? `${error.message} No thread was created. Use workspaceStrategy type "root" to run in the project folder.` | | 205 | + : `${error.message} No thread was created. Pass a branch, tag, or commit that exists there${ | 204 | 206 | strategy.startFromOrigin === true | 205 | 207 | ? "; the project has no origin remote to fetch it from." | 206 | 208 | : ", or set startFromOrigin:true if it exists only on origin." | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8192,6 +8192,37 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 8192 | 8192 | updatedAt: now, | 8193 | 8193 | }, | 8194 | 8194 | }); | | 8195 | + // Clients hide the preparation item, so a notice gets an item of its own. | | 8196 | + if (command.notice !== undefined) | | 8197 | + yield* emitEvent({ | | 8198 | + type: "turn-item.updated", | | 8199 | + threadId: command.threadId, | | 8200 | + runId: state.run.id, | | 8201 | + nodeId: state.rootNode.id, | | 8202 | + providerInstanceId: state.run.providerInstanceId, | | 8203 | + occurredAt: now, | | 8204 | + payload: { | | 8205 | + id: idAllocator.derive.turnItemFromProviderItem({ | | 8206 | + driver: state.providerThread.driver, | | 8207 | + nativeItemId: `workspace-preparation-notice:${state.run.id}`, | | 8208 | + }), | | 8209 | + threadId: command.threadId, | | 8210 | + runId: state.run.id, | | 8211 | + nodeId: state.rootNode.id, | | 8212 | + providerThreadId: state.providerThread.id, | | 8213 | + providerTurnId: null, | | 8214 | + nativeItemRef: null, | | 8215 | + parentItemId: null, | | 8216 | + ordinal: yield* nextTurnItemOrdinal(projection), | | 8217 | + status: "completed", | | 8218 | + title: null, | | 8219 | + startedAt: now, | | 8220 | + completedAt: now, | | 8221 | + updatedAt: now, | | 8222 | + type: "system_notice", | | 8223 | + message: command.notice, | | 8224 | + }, | | 8225 | + }); | 8195 | 8226 | yield* emitEvent({ | 8196 | 8227 | type: "run.updated", | 8197 | 8228 | threadId: command.threadId, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ThreadLaunchService.test.ts +29-2Lines changed: 29 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -182,7 +182,7 @@ function makeHarness(options: HarnessOptions = {}) { | 182 | 182 | renameBranch, | 183 | 183 | fetchRemote: options.fetchRemote ?? (() => Effect.void), | 184 | 184 | hasCommit: options.hasCommit ?? (() => Effect.succeed(false)), | 185 | | - isRepository: () => Effect.succeed(true), | | 185 | + isOutsideRepository: () => Effect.succeed(false), | 186 | 186 | remoteExists: () => Effect.succeed(true), | 187 | 187 | remoteBranchExists: () => Effect.succeed(true), | 188 | 188 | removeWorktree, | @@ -1127,7 +1127,7 @@ it.effect.each([ | 1127 | 1127 | Effect.gen(function* () { | 1128 | 1128 | // Only `projectId` stands in for the Scratch project here. | 1129 | 1129 | const harness = makeHarness({ | 1130 | | - git: { isRepository: () => Effect.succeed(false) }, | | 1130 | + git: { isOutsideRepository: () => Effect.succeed(true) }, | 1131 | 1131 | managedFolders: Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({ | 1132 | 1132 | namedProjectsRoot: "/projects", | 1133 | 1133 | folderForThread: () => Effect.succeed(testCase.scratchFolder), | @@ -1154,6 +1154,33 @@ it.effect.each([ | 1154 | 1154 | (yield* threads.getThreadProjection(launched.threadId)).thread.worktreePath, | 1155 | 1155 | Option.getOrNull(testCase.scratchFolder), | 1156 | 1156 | ); | | 1157 | + // Only the project folder case differs from the request in a way to report. | | 1158 | + yield* waitUntil(() => | | 1159 | + threads | | 1160 | + .getThreadRecords(launched.threadId, ["runs"]) | | 1161 | + .pipe(Effect.map(({ runs }) => runs[0]?.status !== "preparing")), | | 1162 | + ); | | 1163 | + const notices = (yield* threads.getThreadRecords(launched.threadId, ["turnItems"])) | | 1164 | + .turnItems.filter((item) => item.type === "system_notice"); | | 1165 | + assert.equal(notices.length, Option.isSome(testCase.scratchFolder) ? 0 : 1); | | 1166 | + }).pipe(Effect.provide(harness.layer)); | | 1167 | + }), | | 1168 | +); | | 1169 | + | | 1170 | +it.effect("refuses a new worktree in a folder outside any repository", () => | | 1171 | + Effect.gen(function* () { | | 1172 | + const harness = makeHarness({ | | 1173 | + git: { isOutsideRepository: () => Effect.succeed(true) }, | | 1174 | + }); | | 1175 | + yield* Effect.gen(function* () { | | 1176 | + const launches = yield* ThreadLaunch.ThreadLaunchService; | | 1177 | + const error = yield* launches | | 1178 | + .checkWorktreeBase({ | | 1179 | + projectId, | | 1180 | + workspaceStrategy: { type: "worktree", baseRef: "main" }, | | 1181 | + }) | | 1182 | + .pipe(Effect.flip); | | 1183 | + assert.deepInclude(error, { _tag: "ThreadLaunchNotRepositoryError", projectId }); | 1157 | 1184 | }).pipe(Effect.provide(harness.layer)); | 1158 | 1185 | }), | 1159 | 1186 | ); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ThreadLaunchService.ts +62-27Lines changed: 62 additions & 27 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -180,6 +180,20 @@ export class ThreadLaunchBranchExistsError extends Schema.TaggedError<ThreadLaun | 180 | 180 | } | 181 | 181 | } | 182 | 182 |
| | 183 | +/** A new worktree was asked for in a folder that Git reports as outside any repository. */ | | 184 | +export class ThreadLaunchNotRepositoryError extends Schema.TaggedError<ThreadLaunchNotRepositoryError>()( | | 185 | + "ThreadLaunchNotRepositoryError", | | 186 | + { | | 187 | + projectId: ProjectId, | | 188 | + projectTitle: Schema.String, | | 189 | + workspaceRoot: Schema.String, | | 190 | + }, | | 191 | +) { | | 192 | + override get message(): string { | | 193 | + return `Project "${this.projectTitle}" (${this.workspaceRoot}) is not a Git repository, so it cannot have a worktree.`; | | 194 | + } | | 195 | +} | | 196 | + | 183 | 197 | export class ThreadLaunchService extends Context.Service< | 184 | 198 | ThreadLaunchService, | 185 | 199 | { | @@ -191,13 +205,17 @@ export class ThreadLaunchService extends Context.Service< | 191 | 205 | >; | 192 | 206 | /** | 193 | 207 | * Fails when a new worktree's base ref cannot give provisioning a commit, | 194 | | - * or when its requested branch already exists. | | 208 | + * when its requested branch already exists, or when the project is not a | | 209 | + * Git repository. | 195 | 210 | * `launch` accepts such a launch and reports the failure inside the | 196 | 211 | * thread; a caller that needs it before a thread exists checks first. | 197 | 212 | */ | 198 | 213 | readonly checkWorktreeBase: ( | 199 | 214 | input: Pick<ThreadLaunchInput, "projectId" | "workspaceStrategy">, | 200 | | - ) => Effect.Effect<void, ThreadLaunchBaseRefError | ThreadLaunchBranchExistsError>; | | 215 | + ) => Effect.Effect< | | 216 | + void, | | 217 | + ThreadLaunchBaseRefError | ThreadLaunchBranchExistsError | ThreadLaunchNotRepositoryError | | 218 | + >; | 201 | 219 | /** Dispatches prepared-run.retry and prepares the run's workspace again. */ | 202 | 220 | readonly retryPreparation: (input: ThreadLaunchPreparationInput) => Effect.Effect< | 203 | 221 | Orchestrator.OrchestratorV2DispatchResult, | @@ -304,10 +322,21 @@ const make = Effect.gen(function* () { | 304 | 322 | branch, | 305 | 323 | }); | 306 | 324 | } | | 325 | + const outside = yield* Effect.gen(function* () { | | 326 | + const project = Option.getOrUndefined(yield* projects.getById(input.projectId)); | | 327 | + if (project === undefined) return undefined; | | 328 | + return (yield* git.isOutsideRepository(project.workspaceRoot)) ? project : undefined; | | 329 | + }).pipe(Effect.orElseSucceed(() => undefined)); | | 330 | + if (outside !== undefined) | | 331 | + return yield* new ThreadLaunchNotRepositoryError({ | | 332 | + projectId: outside.id, | | 333 | + projectTitle: outside.title, | | 334 | + workspaceRoot: outside.workspaceRoot, | | 335 | + }); | 307 | 336 | // `:/text` is a commit search, which cannot be asked for a commit type. | 308 | 337 | if (baseRef.startsWith(":")) return; | 309 | 338 | // Anything this cannot establish is left for the launch to report: a | 310 | | - // missing project, a folder that is not a repository, a failing Git. | | 339 | + // missing project, a repository that Git cannot read, a failing Git. | 311 | 340 | const unresolved = yield* Effect.gen(function* () { | 312 | 341 | const project = Option.getOrUndefined(yield* projects.getById(input.projectId)); | 313 | 342 | if (project === undefined) return undefined; | @@ -353,31 +382,34 @@ const make = Effect.gen(function* () { | 353 | 382 | }), | 354 | 383 | ), | 355 | 384 | ); | 356 | | - // A folder that is not a Git repository cannot hold a worktree. A worktree | 357 | | - // request there (a stale composer mode, an agent, a scheduled task, or a | 358 | | - // retry of one) runs like a root launch, as the composer already does. | 359 | | - // Scratch gives each such thread a folder of its own. | 360 | | - const input: PreparationInput = | | 385 | + // A worktree needs a Git repository. Scratch gives a worktree request (a | | 386 | + // stale composer mode, an agent, a scheduled task, or a retry of one) a | | 387 | + // folder of its own. In another folder that Git reports as outside any | | 388 | + // repository, the request runs in the project folder, as the composer | | 389 | + // does, and the thread says so. Two such runs then share that folder. | | 390 | + const scratchFolder = | | 391 | + requested.workspaceStrategy.type === "worktree" | | 392 | + ? yield* managedFolders | | 393 | + .folderForThread({ | | 394 | + projectId: requested.projectId, | | 395 | + threadId, | | 396 | + text: requested.initialMessage?.text ?? "", | | 397 | + }) | | 398 | + .pipe(Effect.mapError(mapError(requested, "provision-worktree", threadId))) | | 399 | + : Option.none<string>(); | | 400 | + const runsInProjectFolder = | 361 | 401 | requested.workspaceStrategy.type === "worktree" && | 362 | | - !(yield* git | 363 | | - .isRepository(project.workspaceRoot) | 364 | | - .pipe(Effect.mapError(mapError(requested, "provision-worktree", threadId)))) | 365 | | - ? Option.match( | 366 | | - yield* managedFolders | 367 | | - .folderForThread({ | 368 | | - projectId: requested.projectId, | 369 | | - threadId, | 370 | | - text: requested.initialMessage?.text ?? "", | 371 | | - }) | 372 | | - .pipe(Effect.mapError(mapError(requested, "provision-worktree", threadId))), | 373 | | - { | 374 | | - onNone: () => ({ ...requested, workspaceStrategy: { type: "root" } }), | 375 | | - onSome: (worktreePath) => ({ | 376 | | - ...requested, | 377 | | - workspaceStrategy: { type: "existing_worktree", worktreePath }, | 378 | | - }), | 379 | | - }, | 380 | | - ) | | 402 | + Option.isNone(scratchFolder) && | | 403 | + (yield* git | | 404 | + .isOutsideRepository(project.workspaceRoot) | | 405 | + .pipe(Effect.mapError(mapError(requested, "provision-worktree", threadId)))); | | 406 | + const input: PreparationInput = Option.isSome(scratchFolder) | | 407 | + ? { | | 408 | + ...requested, | | 409 | + workspaceStrategy: { type: "existing_worktree", worktreePath: scratchFolder.value }, | | 410 | + } | | 411 | + : runsInProjectFolder | | 412 | + ? { ...requested, workspaceStrategy: { type: "root" } } | 381 | 413 | : requested; | 382 | 414 |
| 383 | 415 | const reused = input.reusedWorktree; | @@ -713,6 +745,9 @@ const make = Effect.gen(function* () { | 713 | 745 | commandId: CommandId.make(`${input.commandId}:release`), | 714 | 746 | threadId, | 715 | 747 | runId, | | 748 | + ...(runsInProjectFolder | | 749 | + ? { notice: "Not a Git repository: running in the project folder, not a worktree" } | | 750 | + : {}), | 716 | 751 | }) | 717 | 752 | .pipe( | 718 | 753 | Effect.mapError(mapError(input, "release-run", threadId)), | |
Collapse file Copy file name to clipboardExpand all lines: packages/contracts/src/orchestrationV2.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2993,6 +2993,8 @@ export const OrchestrationV2Command = Schema.Union([ | 2993 | 2993 | commandId: CommandId, | 2994 | 2994 | threadId: ThreadId, | 2995 | 2995 | runId: RunId, | | 2996 | + /** Shown in the thread when the workspace differs from the request. */ | | 2997 | + notice: Schema.optional(Schema.String), | 2996 | 2998 | }), | 2997 | 2999 | /** Provider acceptance of a mailbox delivery; distinct from the agent reading its result. */ | 2998 | 3000 | Schema.Struct({ | |
|