Skip to content

Commit 498f449

Browse files
committed
Merge remote-tracking branch 'origin/main' into perf/command-read-skips-finished-subagents
2 parents 1f21a48 + d412871 commit 498f449

6 files changed

Lines changed: 131 additions & 51 deletions

File tree

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

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ it.effect.each([
174174
{ order: "cleanup-first", restoreFiles: true },
175175
{ order: "rollback-first", restoreFiles: true },
176176
{ order: "cleanup-first", restoreFiles: false, unmanaged: true },
177-
])("locks rollback with %s", ({ order, restoreFiles, unmanaged = false }) =>
177+
{ order: "cleanup-first", restoreFiles: true, leavesFolder: true },
178+
{ order: "cleanup-first", restoreFiles: false, leavesFolder: true },
179+
{ order: "cleanup-first", restoreFiles: true, leavesFolder: true, shared: true },
180+
])("locks rollback with %s", ({ order, restoreFiles, unmanaged, leavesFolder, shared }) =>
178181
Effect.gen(function* () {
179182
const fileSystem = yield* FileSystem.FileSystem;
180183
const path = yield* Path.Path;
@@ -185,7 +188,7 @@ it.effect.each([
185188
const worktreePath = unmanaged
186189
? path.join(workspaceRoot, "elsewhere", "feature")
187190
: path.join(worktreesDir, "repo", "feature");
188-
yield* fileSystem.makeDirectory(worktreePath, { recursive: true });
191+
yield* fileSystem.makeDirectory(path.join(worktreePath, ".git"), { recursive: true });
189192
const threadId = ThreadId.make("rollback-worktree");
190193
const projectId = ProjectId.make("rollback-worktree-project");
191194
const providerThreadId = ProviderThreadId.make("rollback-worktree-provider-thread");
@@ -246,14 +249,18 @@ it.effect.each([
246249
path: worktreePath,
247250
});
248251
calls.push("create");
249-
yield* fileSystem.makeDirectory(worktreePath).pipe(Effect.orDie);
252+
yield* fileSystem
253+
.makeDirectory(path.join(worktreePath, ".git"), { recursive: true })
254+
.pipe(Effect.orDie);
250255
return {} as never;
251256
}),
252257
}),
253258
Layer.mock(CheckpointService.CheckpointServiceV2)({
254259
restore: () =>
255260
Effect.gen(function* () {
256-
assert.isTrue(yield* fileSystem.exists(worktreePath).pipe(Effect.orDie));
261+
assert.isTrue(
262+
yield* fileSystem.exists(path.join(worktreePath, ".git")).pipe(Effect.orDie),
263+
);
257264
calls.push("files");
258265
}),
259266
}),
@@ -272,16 +279,31 @@ it.effect.each([
272279
Effect.succeed({
273280
schemaVersion: 1,
274281
snapshotSequence: 0,
275-
threads: [],
282+
threads: shared
283+
? [
284+
{
285+
id: ThreadId.make("rollback-worktree-sharer"),
286+
projectId,
287+
worktreePath,
288+
deletedAt: null,
289+
lineage: {},
290+
},
291+
]
292+
: [],
276293
archivedThreads: [],
277-
}),
294+
} as never),
295+
getCheckpointContext: () => Effect.succeed({ checkpointScopes: [] } as never),
296+
getThreadProviderContext: () => Effect.succeed({ providerSessions: [] } as never),
278297
}),
279298
Layer.mock(ProviderSessionManager.ProviderSessionManagerV2)({
280299
open: (input) =>
281300
Effect.gen(function* () {
282301
assert.equal(input.runtimePolicy.cwd, worktreePath);
283302
if (!(yield* fileSystem.exists(worktreePath).pipe(Effect.orDie))) {
284-
return yield* new ProviderWorkspaceMissingError({ threadId, cwd: worktreePath });
303+
return yield* new ProviderWorkspaceMissingError({
304+
threadId,
305+
cwd: worktreePath,
306+
});
285307
}
286308
calls.push("session");
287309
yield* Deferred.succeed(openEntered, undefined);
@@ -306,6 +328,11 @@ it.effect.each([
306328
cleanupEntered = true;
307329
if (order === "cleanup-first") yield* Deferred.await(release);
308330
yield* fileSystem.remove(worktreePath, { recursive: true, force: true });
331+
// A failed `git worktree remove` can leave the folder without its checkout.
332+
if (leavesFolder) {
333+
yield* fileSystem.makeDirectory(worktreePath);
334+
yield* fileSystem.writeFileString(path.join(worktreePath, "unsaved.txt"), "keep");
335+
}
309336
calls.push("cleanup");
310337
}),
311338
);
@@ -325,23 +352,31 @@ it.effect.each([
325352
if (order === "rollback-first") assert.isFalse(cleanupEntered);
326353
yield* Deferred.succeed(release, undefined);
327354
if (cleanup !== undefined) yield* Fiber.join(cleanup);
328-
if (unmanaged) {
355+
if (unmanaged || shared) {
329356
const error = yield* Fiber.join(rollback).pipe(Effect.flip);
330357
assert.equal(error._tag, "CheckpointRollbackExecutionError");
331358
assert.deepEqual(calls, ["cleanup"]);
359+
if (shared) {
360+
assert.equal(error.reason, "shared-workspace");
361+
assert.isTrue(yield* fileSystem.exists(path.join(worktreePath, "unsaved.txt")));
362+
}
332363
return;
333364
}
334365
yield* Fiber.join(rollback);
335366
if (laterCleanup !== undefined) yield* Fiber.join(laterCleanup);
336367
const rollbackCalls = restoreFiles
337368
? ["session", "files", "projection"]
338369
: ["session", "projection"];
370+
const recreates = !leavesFolder || restoreFiles;
339371
assert.deepEqual(
340372
calls,
341373
order === "cleanup-first"
342-
? ["cleanup", "prune", "create", ...rollbackCalls]
374+
? ["cleanup", ...(recreates ? ["prune", "create"] : []), ...rollbackCalls]
343375
: [...rollbackCalls, "cleanup"],
344376
);
377+
if (!recreates) {
378+
assert.isTrue(yield* fileSystem.exists(path.join(worktreePath, "unsaved.txt")));
379+
}
345380
}).pipe(Effect.provide(testLayer));
346381
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
347382
);

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

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -217,42 +217,51 @@ export const layer: Layer.Layer<
217217
};
218218
});
219219

220-
const { worktreePath, branch } = context.thread;
221-
// Recreated only where the server would have made it, as turn start does.
222-
if (
223-
worktreePath != null &&
224-
branch != null &&
225-
!(yield* fileSystem.exists(worktreePath)) &&
226-
(yield* isManagedWorktreePath(worktreePath))
227-
) {
228-
const project = yield* projects.get(context.thread.projectId);
229-
if (Option.isSome(project)) {
230-
yield* gitWorkflow.pruneWorktrees({ cwd: project.value.workspaceRoot });
231-
yield* gitWorkflow.createWorktree({
232-
cwd: project.value.workspaceRoot,
233-
refName: branch,
234-
path: worktreePath,
235-
});
236-
}
237-
}
238-
239-
if (
240-
input.restoreFiles !== false &&
241-
!(yield* isCheckpointRestoreIsolated(context.thread, scope, {
242-
fileSystem,
243-
projections,
244-
projects,
245-
path,
246-
}))
247-
) {
248-
return yield* new CheckpointRollbackExecutionError({
220+
const restoreFiles = input.restoreFiles !== false;
221+
const isolated = isCheckpointRestoreIsolated(context.thread, scope, {
222+
fileSystem,
223+
projections,
224+
projects,
225+
path,
226+
});
227+
const sharedWorkspace = () =>
228+
new CheckpointRollbackExecutionError({
249229
reason: "shared-workspace",
250230
threadId: input.threadId,
251231
providerThreadId: input.providerThreadId,
252232
checkpointId: input.checkpointId,
253233
});
234+
235+
const { worktreePath, branch } = context.thread;
236+
// Recreated only where the server would have made it, as turn start does.
237+
if (worktreePath != null && branch != null) {
238+
// A failed removal can leave the folder without its checkout; only a file restore replaces it.
239+
const leftover =
240+
restoreFiles &&
241+
(yield* fileSystem.exists(worktreePath)) &&
242+
!(yield* fileSystem.exists(path.join(worktreePath, ".git")));
243+
if (
244+
(leftover || !(yield* fileSystem.exists(worktreePath))) &&
245+
(yield* isManagedWorktreePath(worktreePath))
246+
) {
247+
const project = yield* projects.get(context.thread.projectId);
248+
if (Option.isSome(project)) {
249+
if (leftover) {
250+
if (!(yield* isolated)) return yield* sharedWorkspace();
251+
yield* fileSystem.remove(worktreePath, { recursive: true, force: true });
252+
}
253+
yield* gitWorkflow.pruneWorktrees({ cwd: project.value.workspaceRoot });
254+
yield* gitWorkflow.createWorktree({
255+
cwd: project.value.workspaceRoot,
256+
refName: branch,
257+
path: worktreePath,
258+
});
259+
}
260+
}
254261
}
255262

263+
if (restoreFiles && !(yield* isolated)) return yield* sharedWorkspace();
264+
256265
const modelSelection = context.thread.modelSelection;
257266
const resolvedRuntimePolicy = yield* runtimePolicy.resolve({
258267
thread: context.thread,

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,8 @@ export interface ActiveShellIndexEntry extends Pick<
442442
> {
443443
/** False only when no run, request, work item, or watch can make the shell live. */
444444
readonly mayBeLive: boolean;
445+
/** Whether it delegated a task to a linked environment, whose clients nest that task under it. */
446+
readonly hasRemoteTasks: boolean;
445447
}
446448

447449
export interface OlderSettledThreadsQuery {
@@ -7790,6 +7792,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
77907792
readonly pinned_at: string | null;
77917793
readonly snoozed_until: string | null;
77927794
readonly may_be_live: number;
7795+
readonly has_remote_tasks: number;
77937796
}>`
77947797
SELECT
77957798
t.thread_id,
@@ -7835,7 +7838,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
78357838
SELECT 1 FROM json_each(t.payload_json, '$.pullRequests') link
78367839
WHERE json_type(link.value, '$.watch') IS NOT NULL
78377840
)
7838-
) AS may_be_live
7841+
) AS may_be_live,
7842+
-- One list for the whole read, found through the child_thread_id index. A lookup per
7843+
-- thread would parse the payload of every subagent row.
7844+
t.thread_id IN (
7845+
SELECT subagent.thread_id FROM orchestration_v2_projection_subagents subagent
7846+
WHERE subagent.origin = 'app_owned'
7847+
AND subagent.child_thread_id IS NULL
7848+
AND json_type(subagent.payload_json, '$.remoteChild') = 'object'
7849+
) AS has_remote_tasks
78397850
FROM orchestration_v2_projection_threads t
78407851
WHERE t.deleted_at IS NULL
78417852
AND json_extract(t.payload_json, '$.archivedAt') IS NULL
@@ -7862,6 +7873,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
78627873
pinnedAt: row.pinned_at === null ? null : DateTime.makeUnsafe(row.pinned_at),
78637874
snoozedUntil: row.snoozed_until === null ? null : DateTime.makeUnsafe(row.snoozed_until),
78647875
mayBeLive: row.may_be_live === 1,
7876+
hasRemoteTasks: row.has_remote_tasks === 1,
78657877
}));
78667878
}, Effect.mapError(shellSnapshotReadError));
78677879

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ it.layer(layerTest)("a delegated task in a linked environment", (it) => {
222222
assert.deepEqual(item?.type === "subagent" ? item.remoteChild : undefined, remoteChild);
223223
// No thread was created here for the child.
224224
assert.equal(yield* orchestrator.getThreadShell(remoteChild.threadId), null);
225+
const other = yield* seedLiveParent("records-no-task");
226+
const index = yield* (yield* ProjectionStore.ProjectionStoreV2).listActiveShellIndex();
227+
assert.isTrue(index.find((entry) => entry.id === parent.threadId)?.hasRemoteTasks);
228+
assert.isFalse(index.find((entry) => entry.id === other.threadId)?.hasRemoteTasks);
225229
}),
226230
);
227231

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function buildActiveShellSnapshot(input: {
7575
type ShellTrimRow = Pick<
7676
OrchestrationV2ThreadShell,
7777
"id" | "projectId" | "lineage" | "settledOverride" | "settledAt"
78-
>;
78+
> & { readonly hasRemoteTasks?: boolean };
7979

8080
/**
8181
* What a subscriber's trims read across every active row. Computed from full shells or from the
@@ -84,6 +84,8 @@ type ShellTrimRow = Pick<
8484
interface ShellTrimFacts {
8585
readonly subagentParentIds: ReadonlySet<string>;
8686
readonly subagentThreadIds: Set<string>;
87+
/** Only the store's index knows these; shells alone leave it empty. */
88+
readonly remoteTaskParentIds: ReadonlySet<string>;
8789
/** Undefined when no thread is settled, so none is older. */
8890
readonly olderSettledBeforeMs: number | undefined;
8991
readonly olderSettledCounts: ReadonlyArray<OrchestrationV2OlderSettledCount>;
@@ -94,8 +96,10 @@ const NEWEST_SETTLED_KEPT = 25;
9496
function shellTrimFacts(rows: ReadonlyArray<ShellTrimRow>): ShellTrimFacts {
9597
const subagentParentIds = new Set<string>();
9698
const subagentThreadIds = new Set<string>();
99+
const remoteTaskParentIds = new Set<string>();
97100
const settledTimes: Array<number> = [];
98101
for (const row of rows) {
102+
if (row.hasRemoteTasks === true) remoteTaskParentIds.add(row.id);
99103
if (row.lineage.relationshipToParent === "subagent") {
100104
subagentThreadIds.add(row.id);
101105
if (row.lineage.parentThreadId) subagentParentIds.add(row.lineage.parentThreadId);
@@ -121,6 +125,7 @@ function shellTrimFacts(rows: ReadonlyArray<ShellTrimRow>): ShellTrimFacts {
121125
return {
122126
subagentParentIds,
123127
subagentThreadIds,
128+
remoteTaskParentIds,
124129
olderSettledBeforeMs,
125130
olderSettledCounts: Array.from(counts, ([projectId, count]) => ({ projectId, count })),
126131
};
@@ -140,13 +145,14 @@ function hiddenChildAncestorIds(
140145

141146
function omitIdleSubagentShellsWith(
142147
threads: ReadonlyArray<OrchestrationV2ThreadShell>,
143-
facts: Pick<ShellTrimFacts, "subagentParentIds">,
148+
facts: Pick<ShellTrimFacts, "subagentParentIds" | "remoteTaskParentIds">,
144149
): ReadonlyArray<OrchestrationV2ThreadShell> {
145150
return threads.filter(
146151
(thread) =>
147152
thread.lineage.relationshipToParent !== "subagent" ||
148153
isLiveSubagentShell(thread) ||
149-
facts.subagentParentIds.has(thread.id),
154+
facts.subagentParentIds.has(thread.id) ||
155+
facts.remoteTaskParentIds.has(thread.id),
150156
);
151157
}
152158

@@ -156,7 +162,10 @@ function omitOlderSettledShellsWith(
156162
readonly snapshotSequence: number;
157163
readonly now: DateTime.Utc;
158164
},
159-
facts: Pick<ShellTrimFacts, "olderSettledBeforeMs" | "olderSettledCounts">,
165+
facts: Pick<
166+
ShellTrimFacts,
167+
"olderSettledBeforeMs" | "olderSettledCounts" | "remoteTaskParentIds"
168+
>,
160169
): {
161170
readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
162171
readonly olderSettled: OrchestrationV2OlderSettledThreads;
@@ -170,7 +179,8 @@ function omitOlderSettledShellsWith(
170179
thread.pinnedAt != null ||
171180
(thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs) ||
172181
isLiveSubagentShell(thread) ||
173-
ancestorIds.has(thread.id),
182+
ancestorIds.has(thread.id) ||
183+
facts.remoteTaskParentIds.has(thread.id),
174184
);
175185
return {
176186
threads,
@@ -185,7 +195,8 @@ function omitOlderSettledShellsWith(
185195
/**
186196
* Idle subagent children have no sidebar row, so clients that opt in read them from their thread
187197
* family instead. A child stays while it is live or the parent of another child, so the request
188-
* and working walks over shell rows never meet a gap in a nested chain.
198+
* and working walks over shell rows never meet a gap in a nested chain. It also stays as the
199+
* parent of a task in a linked environment, whose clients nest that task under it.
189200
*/
190201
export function omitIdleSubagentShells(
191202
threads: ReadonlyArray<OrchestrationV2ThreadShell>,
@@ -214,8 +225,8 @@ export function activeShellRowsForSubscriber(
214225
/**
215226
* Opted-in snapshots keep the 25 newest settled top-level threads (and ties) and count the rest per
216227
* project in `olderSettled`; clients page them with `getOlderSettledThreads`. Older rows stay while
217-
* pinned, snoozed, live, or above a kept subagent or side chat, so walks over shell rows still find
218-
* them.
228+
* pinned, snoozed, live, above a kept subagent or side chat, or the parent of a task in a linked
229+
* environment, so walks over shell rows and other environments' tasks still find them.
219230
*/
220231
export function omitOlderSettledShells(input: {
221232
readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
@@ -261,7 +272,10 @@ function shellSubscriberThreadIds(
261272
const subagents = index.filter(
262273
(entry) =>
263274
entry.lineage.relationshipToParent === "subagent" &&
264-
(!options.omitIdleSubagents || entry.mayBeLive || facts.subagentParentIds.has(entry.id)),
275+
(!options.omitIdleSubagents ||
276+
entry.mayBeLive ||
277+
entry.hasRemoteTasks ||
278+
facts.subagentParentIds.has(entry.id)),
265279
);
266280
const ancestorIds = hiddenChildAncestorIds([
267281
...subagents,
@@ -280,6 +294,7 @@ function shellSubscriberThreadIds(
280294
entry.pinnedAt != null ||
281295
entry.snoozedUntil != null ||
282296
entry.mayBeLive ||
297+
entry.hasRemoteTasks ||
283298
ancestorIds.has(entry.id)
284299
? [entry.id]
285300
: [],

0 commit comments

Comments
 (0)