Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(channel): run side by side in a directory that cannot host a worktree (#23655)

Arvin Xuec238133addd
diff --git a/apps/server/src/services/channel/ChannelCoordinator.advance.test.ts b/apps/server/src/services/channel/ChannelCoordinator.advance.test.ts
index c60b5bccc35..c1b4df313c8 100644
--- a/apps/server/src/services/channel/ChannelCoordinator.advance.test.ts
+++ b/apps/server/src/services/channel/ChannelCoordinator.advance.test.ts
@@ -387,7 +387,7 @@ describe('Channel concurrent startup', () => {
expect(execAgent).toHaveBeenCalledTimes(1);
});
- it('keeps waiting when the directory cannot host a worktree', async () => {
+ it('retries in the shared directory when it cannot host a worktree', async () => {
executions.claim.mockResolvedValue(null);
executions.worktreeCandidate.mockResolvedValue(candidate);
createChannelWorktree.mockResolvedValue({ error: 'not a git repository' });
@@ -399,7 +399,7 @@ describe('Channel concurrent startup', () => {
candidate,
'not a git repository',
);
- expect(executions.claim).toHaveBeenCalledTimes(1);
+ expect(executions.claim).toHaveBeenCalledTimes(2);
expect(execAgent).not.toHaveBeenCalled();
});
diff --git a/apps/server/src/services/channel/ChannelCoordinator.ts b/apps/server/src/services/channel/ChannelCoordinator.ts
index ce37d4d6b4f..632a7aa5eb9 100644
--- a/apps/server/src/services/channel/ChannelCoordinator.ts
+++ b/apps/server/src/services/channel/ChannelCoordinator.ts
@@ -273,8 +273,9 @@ export class ChannelCoordinator {
/**
* A job held back only because another run uses its member's directory moves its branch into
- * a worktree of that directory instead of waiting. Returns whether the branch got one; a
- * directory that cannot host one (not a git repo, device too old) keeps the job waiting.
+ * a worktree of that directory instead of waiting. A directory that cannot host one (not a
+ * git repo, device too old) is shared instead: the job runs in it alongside the other run.
+ * Returns whether the claim is worth retrying.
*/
private async moveToWorktree(
channelId: string,
@@ -288,7 +289,7 @@ export class ChannelCoordinator {
const created = await createChannelWorktree(candidate, workspaceId);
if (created.path === undefined) {
await this.executions.worktreeUnavailable(channelId, candidate, created.error);
- return false;
+ return true;
}
return this.executions.assignWorktree(channelId, candidate, created.path);
}
diff --git a/packages/database/src/repositories/channel/__tests__/worktree.test.ts b/packages/database/src/repositories/channel/__tests__/worktree.test.ts
index 18102ca7ebb..c03cb83fcfc 100644
--- a/packages/database/src/repositories/channel/__tests__/worktree.test.ts
+++ b/packages/database/src/repositories/channel/__tests__/worktree.test.ts
@@ -111,16 +111,18 @@ describe('channel worktrees', () => {
expect(claim?.job.workingDirectory).toBe('/other');
});
- it('leaves a directory that cannot host a worktree alone for a while', async () => {
+ it('runs side by side in a directory that cannot host a worktree', async () => {
const { channel, secondJob } = await sharedDirectory();
const candidate = (await execution.worktreeCandidate(channel.id, secondJob.id))!;
await execution.worktreeUnavailable(channel.id, candidate, 'not a git repository');
expect(await execution.worktreeCandidate(channel.id, secondJob.id)).toBeUndefined();
- expect(await jobById(db, secondJob.id)).toMatchObject({
- blockedCode: 'directory_locked',
- status: 'queued',
+ const claim = await execution.claim(channel.id, secondJob.id, lease);
+ expect(claim?.job).toMatchObject({
+ blockedCode: null,
+ status: 'running',
+ workingDirectory: candidate.sourceDirectory,
});
});
diff --git a/packages/database/src/repositories/channel/claim.ts b/packages/database/src/repositories/channel/claim.ts
index ac89204f60f..473e1ec4a68 100644
--- a/packages/database/src/repositories/channel/claim.ts
+++ b/packages/database/src/repositories/channel/claim.ts
@@ -22,7 +22,7 @@ import { findMergeableRequests, mergeJobs } from './merge';
import type { ChannelTopicScope } from './topic';
import { continuationTopic, resolveTopic, resumableTopic } from './topic';
import { audit } from './transaction';
-import { assignedWorktree } from './worktree';
+import { assignedWorktree, sharesDirectory } from './worktree';
export interface ChannelClaimOptions {
/** How long the claimer may go without a heartbeat before reconcile takes over. */
@@ -476,7 +476,9 @@ export const claimJob = async (
const blocked =
(await deviceBlock(tx, channel, member, job)) ??
- (await directoryBlock(tx, member.config.deviceId, executionConfig.workingDirectory));
+ (sharesDirectory(member)
+ ? undefined
+ : await directoryBlock(tx, member.config.deviceId, executionConfig.workingDirectory));
if (blocked) {
await block(tx, job, blocked.code, blocked.reason);
return null;
diff --git a/packages/database/src/repositories/channel/worktree.ts b/packages/database/src/repositories/channel/worktree.ts
index b18a169d1fe..0778ebceae5 100644
--- a/packages/database/src/repositories/channel/worktree.ts
+++ b/packages/database/src/repositories/channel/worktree.ts
@@ -61,6 +61,22 @@ export const assignedWorktree = (
return worktree;
};
+/**
+ * The member's directory recently failed to host a worktree (not a git repo, device too old),
+ * so its branches run side by side in the directory itself rather than wait for it. Once the
+ * marker goes stale the next claim waits again just long enough to retry the worktree.
+ */
+export const sharesDirectory = (member: Pick<ChannelMemberItem, 'config' | 'metadata'>) => {
+ const { deviceId, workingDirectory } = member.config ?? {};
+ const unavailable = (member.metadata as ChannelMemberMetadata | null)?.worktreeUnavailable;
+ return (
+ !!deviceId &&
+ unavailable?.deviceId === deviceId &&
+ unavailable.sourceDirectory === workingDirectory &&
+ Date.now() - new Date(unavailable.at).getTime() < WORKTREE_RETRY_MS
+ );
+};
+
export interface ChannelWorktreeCandidate {
branch: string;
deviceId: string;
@@ -98,13 +114,7 @@ export const findWorktreeCandidate = async (
const { deviceId, deviceOwnerId, workingDirectory } = member.config ?? {};
if (!deviceId || !workingDirectory || assignedWorktree(member, job.threadId)) return;
- const unavailable = (member.metadata as ChannelMemberMetadata | null)?.worktreeUnavailable;
- if (
- unavailable?.deviceId === deviceId &&
- unavailable.sourceDirectory === workingDirectory &&
- Date.now() - new Date(unavailable.at).getTime() < WORKTREE_RETRY_MS
- )
- return;
+ if (sharesDirectory(member)) return;
const [requester] = await tx
.select({ userId: channelMembers.userId })
@@ -176,7 +186,7 @@ export const assignWorktree = async (
return true;
};
-/** The directory could not host a worktree; the job keeps waiting for the directory. */
+/** The directory could not host a worktree; the job runs in the directory itself instead. */
export const markWorktreeUnavailable = async (
tx: Transaction,
channelId: string,