GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #129 from Yarden-zamir/fix/non-repository-launch-n… · TomCohenDev/t3code@780a235

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src
    • git
      • GitWorkflowService.ts
    • mcp/toolkits/project
      • handlers.ts
    • orchestration-v2
      • Orchestrator.ts
      • ThreadLaunchService.test.ts
      • ThreadLaunchService.ts
  • packages/contracts/src
    • orchestrationV2.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/git/GitWorkflowService.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/git/GitWorkflowService.ts
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export class GitWorkflowService extends Context.Service<
3636
GitWorkflowService,
3737
{
3838
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>;
3944
readonly hasCommit: (input: {
4045
readonly cwd: string;
4146
readonly refName: string;
@@ -284,6 +289,21 @@ export const make = Effect.gen(function* () {
284289
ensureGit(operation, input.cwd).pipe(Effect.andThen(run(input)));
285290

286291
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+
),
287307
isRepository: (cwd) =>
288308
registry.detect({ cwd }).pipe(
289309
Effect.map((handle) => handle?.kind === "git"),
Collapse file

‎apps/server/src/mcp/toolkits/project/handlers.ts‎

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 numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
200200
message:
201201
error._tag === "ThreadLaunchBranchExistsError"
202202
? `${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${
204206
strategy.startFromOrigin === true
205207
? "; the project has no origin remote to fetch it from."
206208
: ", or set startFromOrigin:true if it exists only on origin."
Collapse file

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8192,6 +8192,37 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
81928192
updatedAt: now,
81938193
},
81948194
});
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+
});
81958226
yield* emitEvent({
81968227
type: "run.updated",
81978228
threadId: command.threadId,
Collapse file

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

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 numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function makeHarness(options: HarnessOptions = {}) {
182182
renameBranch,
183183
fetchRemote: options.fetchRemote ?? (() => Effect.void),
184184
hasCommit: options.hasCommit ?? (() => Effect.succeed(false)),
185-
isRepository: () => Effect.succeed(true),
185+
isOutsideRepository: () => Effect.succeed(false),
186186
remoteExists: () => Effect.succeed(true),
187187
remoteBranchExists: () => Effect.succeed(true),
188188
removeWorktree,
@@ -1127,7 +1127,7 @@ it.effect.each([
11271127
Effect.gen(function* () {
11281128
// Only `projectId` stands in for the Scratch project here.
11291129
const harness = makeHarness({
1130-
git: { isRepository: () => Effect.succeed(false) },
1130+
git: { isOutsideRepository: () => Effect.succeed(true) },
11311131
managedFolders: Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({
11321132
namedProjectsRoot: "/projects",
11331133
folderForThread: () => Effect.succeed(testCase.scratchFolder),
@@ -1154,6 +1154,33 @@ it.effect.each([
11541154
(yield* threads.getThreadProjection(launched.threadId)).thread.worktreePath,
11551155
Option.getOrNull(testCase.scratchFolder),
11561156
);
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 });
11571184
}).pipe(Effect.provide(harness.layer));
11581185
}),
11591186
);
Collapse file

‎apps/server/src/orchestration-v2/ThreadLaunchService.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ThreadLaunchService.ts
+62-27Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ export class ThreadLaunchBranchExistsError extends Schema.TaggedError<ThreadLaun
180180
}
181181
}
182182

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+
183197
export class ThreadLaunchService extends Context.Service<
184198
ThreadLaunchService,
185199
{
@@ -191,13 +205,17 @@ export class ThreadLaunchService extends Context.Service<
191205
>;
192206
/**
193207
* 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.
195210
* `launch` accepts such a launch and reports the failure inside the
196211
* thread; a caller that needs it before a thread exists checks first.
197212
*/
198213
readonly checkWorktreeBase: (
199214
input: Pick<ThreadLaunchInput, "projectId" | "workspaceStrategy">,
200-
) => Effect.Effect<void, ThreadLaunchBaseRefError | ThreadLaunchBranchExistsError>;
215+
) => Effect.Effect<
216+
void,
217+
ThreadLaunchBaseRefError | ThreadLaunchBranchExistsError | ThreadLaunchNotRepositoryError
218+
>;
201219
/** Dispatches prepared-run.retry and prepares the run's workspace again. */
202220
readonly retryPreparation: (input: ThreadLaunchPreparationInput) => Effect.Effect<
203221
Orchestrator.OrchestratorV2DispatchResult,
@@ -304,10 +322,21 @@ const make = Effect.gen(function* () {
304322
branch,
305323
});
306324
}
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+
});
307336
// `:/text` is a commit search, which cannot be asked for a commit type.
308337
if (baseRef.startsWith(":")) return;
309338
// 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.
311340
const unresolved = yield* Effect.gen(function* () {
312341
const project = Option.getOrUndefined(yield* projects.getById(input.projectId));
313342
if (project === undefined) return undefined;
@@ -353,31 +382,34 @@ const make = Effect.gen(function* () {
353382
}),
354383
),
355384
);
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 =
361401
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" } }
381413
: requested;
382414

383415
const reused = input.reusedWorktree;
@@ -713,6 +745,9 @@ const make = Effect.gen(function* () {
713745
commandId: CommandId.make(`${input.commandId}:release`),
714746
threadId,
715747
runId,
748+
...(runsInProjectFolder
749+
? { notice: "Not a Git repository: running in the project folder, not a worktree" }
750+
: {}),
716751
})
717752
.pipe(
718753
Effect.mapError(mapError(input, "release-run", threadId)),
Collapse file

‎packages/contracts/src/orchestrationV2.ts‎

Copy file name to clipboardExpand all lines: packages/contracts/src/orchestrationV2.ts
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,6 +2993,8 @@ export const OrchestrationV2Command = Schema.Union([
29932993
commandId: CommandId,
29942994
threadId: ThreadId,
29952995
runId: RunId,
2996+
/** Shown in the thread when the workspace differs from the request. */
2997+
notice: Schema.optional(Schema.String),
29962998
}),
29972999
/** Provider acceptance of a mailbox delivery; distinct from the agent reading its result. */
29983000
Schema.Struct({

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.