Skip to content

Commit 215d501

Browse files
committed
fix(server): keep late and archived subagents on renamed branches
1 parent 29ebf72 commit 215d501

4 files changed

Lines changed: 96 additions & 8 deletions

File tree

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,70 @@ layer("ProviderEventIngestorV2", (it) => {
17291729
}),
17301730
);
17311731

1732+
it.effect.each(
1733+
["codex", "claude"].flatMap((driver) =>
1734+
["/repo/shared", "/repo/isolated", null].map((worktreePath) => ({ driver, worktreePath })),
1735+
),
1736+
)("records a native child with current shared workspace: %j", ({ driver, worktreePath }) =>
1737+
Effect.gen(function* () {
1738+
const now = yield* DateTime.now;
1739+
const eventSink = yield* EventSink.EventSinkV2;
1740+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
1741+
const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2;
1742+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
1743+
const rootEvent = yield* threadCreatedEvent(now);
1744+
if (rootEvent.type !== "thread.created") throw new Error("Expected thread fixture");
1745+
const childThreadId = idAllocator.derive.threadFromProviderThread({
1746+
driver: ProviderDriverKind.make(driver),
1747+
nativeThreadId: `late-native-subagent:${worktreePath}`,
1748+
});
1749+
const offered: OrchestrationV2AppThread = {
1750+
...rootEvent.payload,
1751+
id: childThreadId,
1752+
branch: "t3/abcd1234",
1753+
worktreePath,
1754+
lineage: {
1755+
parentThreadId: rootEvent.threadId,
1756+
relationshipToParent: "subagent",
1757+
rootThreadId: rootEvent.threadId,
1758+
},
1759+
};
1760+
yield* eventSink.write({
1761+
events: [
1762+
{
1763+
...rootEvent,
1764+
payload: {
1765+
...rootEvent.payload,
1766+
branch: "generated-branch",
1767+
worktreePath: "/repo/shared",
1768+
},
1769+
},
1770+
],
1771+
});
1772+
const providerSessionId = yield* idAllocator.allocate.providerSession({
1773+
providerInstanceId: modelSelection.instanceId,
1774+
threadId: rootEvent.threadId,
1775+
});
1776+
yield* ingestor.ingestNormalized({
1777+
providerSessionId,
1778+
providerInstanceId: modelSelection.instanceId,
1779+
threadId: rootEvent.threadId,
1780+
event: {
1781+
type: "app_thread.created",
1782+
driver: ProviderDriverKind.make(driver),
1783+
appThread: offered,
1784+
},
1785+
});
1786+
const child = yield* projectionStore.getThread(childThreadId);
1787+
assert.equal(
1788+
child.branch,
1789+
worktreePath === "/repo/shared" ? "generated-branch" : offered.branch,
1790+
);
1791+
assert.equal(child.worktreePath, worktreePath);
1792+
assert.deepEqual(child.lineage, offered.lineage);
1793+
}),
1794+
);
1795+
17321796
it.effect("updates provider child metadata without recreating its saved thread", () =>
17331797
Effect.gen(function* () {
17341798
const now = yield* DateTime.now;

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ export const layer: Layer.Layer<
537537
Effect.gen(function* () {
538538
switch (input.event.type) {
539539
case "app_thread.created": {
540-
const offered = input.event.appThread;
540+
let offered = input.event.appThread;
541541
const existing = yield* projections.getThread(offered.id).pipe(
542542
Effect.catchTags({
543543
ProjectionStoreThreadNotFoundError: () => Effect.succeed(null),
@@ -549,6 +549,21 @@ export const layer: Layer.Layer<
549549
modelSelectionsEqual(existing.modelSelection, offered.modelSelection)
550550
)
551551
return [];
552+
if (
553+
existing === null &&
554+
offered.lineage.parentThreadId !== null &&
555+
offered.worktreePath !== null
556+
) {
557+
const parent = yield* projections.getThread(offered.lineage.parentThreadId).pipe(
558+
Effect.catchTags({
559+
ProjectionStoreThreadNotFoundError: () => Effect.succeed(null),
560+
}),
561+
);
562+
// Native adapters may still hold the parent's turn-start branch snapshot.
563+
if (parent !== null && parent.worktreePath === offered.worktreePath) {
564+
offered = { ...offered, branch: parent.branch, worktreePath: parent.worktreePath };
565+
}
566+
}
552567
const now = yield* DateTime.now;
553568
return [
554569
yield* makeDomainEvent(input, {

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ it.effect("names the worktree itself when the client provides no branch", () =>
11471147
}),
11481148
);
11491149

1150-
it.effect("renames a temporary t3/<hash> branch off the provisioning critical path", () =>
1150+
it.effect.each([false, true])("renames shared temporary branches with archived=%s", (archived) =>
11511151
Effect.gen(function* () {
11521152
const branchNameStarted = yield* Deferred.make<void>();
11531153
const allowBranchName = yield* Deferred.make<void>();
@@ -1228,8 +1228,17 @@ it.effect("renames a temporary t3/<hash> branch off the provisioning critical pa
12281228
creationSource: "web",
12291229
});
12301230
}
1231-
const unrelated = yield* threads.getThreadProjection(otherThreadId);
1232-
const otherPath = yield* threads.getThreadProjection(otherPathThreadId);
1231+
if (archived) {
1232+
for (const threadId of [childThreadId, otherThreadId, otherPathThreadId]) {
1233+
yield* threads.dispatch({
1234+
type: "thread.archive",
1235+
commandId: CommandId.make(`command:archive:${threadId}`),
1236+
threadId,
1237+
});
1238+
}
1239+
}
1240+
const unrelatedBeforeRename = yield* threads.getThreadProjection(otherThreadId);
1241+
const otherPathBeforeRename = yield* threads.getThreadProjection(otherPathThreadId);
12331242
yield* Deferred.succeed(allowBranchName, undefined);
12341243
yield* threads.streamStoredEventsFrom({ threadId: childThreadId }).pipe(
12351244
Stream.filter(
@@ -1249,11 +1258,11 @@ it.effect("renames a temporary t3/<hash> branch off the provisioning critical pa
12491258
);
12501259
assert.deepEqual(
12511260
(yield* threads.getThreadProjection(otherThreadId)).thread,
1252-
unrelated.thread,
1261+
unrelatedBeforeRename.thread,
12531262
);
12541263
assert.deepEqual(
12551264
(yield* threads.getThreadProjection(otherPathThreadId)).thread,
1256-
otherPath.thread,
1265+
otherPathBeforeRename.thread,
12571266
);
12581267
const staleUpdate = yield* threads
12591268
.dispatch({
@@ -1268,7 +1277,7 @@ it.effect("renames a temporary t3/<hash> branch off the provisioning critical pa
12681277
assert.equal(staleUpdate._tag, "OrchestratorDispatchError");
12691278
assert.deepEqual(
12701279
(yield* threads.getThreadProjection(otherThreadId)).thread,
1271-
unrelated.thread,
1280+
unrelatedBeforeRename.thread,
12721281
);
12731282
assert.deepEqual(harness.renameBranch.mock.calls[0]?.[0], {
12741283
cwd: "/repo-worktrees/temp",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ const make = Effect.gen(function* () {
533533
const visited = new Set([threadId]);
534534
while (true) {
535535
const snapshot = yield* threads.getShellSnapshot();
536-
const sharing = snapshot.threads.filter(
536+
const sharing = [...snapshot.threads, ...snapshot.archivedThreads].filter(
537537
(thread) =>
538538
!visited.has(thread.id) &&
539539
thread.worktreePath === worktreeCwd &&

0 commit comments

Comments
 (0)