Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,65 @@ const restartCancelledWorkSurvivesStaleRunUpdate = Effect.gen(function* () {
assert.deepEqual(updated?.restartCancelledBackgroundWork, work);
});

const threadWorktreePathsReadActiveThreads = Effect.gen(function* () {
const store = yield* ProjectionStore.ProjectionStoreV2;
const now = yield* DateTime.now;
const create = (id: string, input: { worktreePath: string | null; archived?: boolean }) => {
const threadId = ThreadId.make(id);
const thread = {
createdBy: "user" as const,
creationSource: "web" as const,
id: threadId,
projectId: ProjectId.make("project:worktree-paths"),
title: id,
providerInstanceId,
modelSelection,
runtimeMode: "full-access" as const,
interactionMode: "default" as const,
branch: input.worktreePath === null ? null : id,
worktreePath: input.worktreePath,
activeProviderThreadId: null,
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
forkedFrom: null,
createdAt: now,
updatedAt: now,
archivedAt: input.archived === true ? now : null,
settledOverride: null,
settledAt: null,
lastVisitedAt: null,
deletedAt: null,
};
return store
.apply({
id: EventId.make(`event:${id}:created`),
type: "thread.created",
threadId,
occurredAt: now,
payload: thread,
})
.pipe(Effect.as(thread));
};
yield* create("thread:active", { worktreePath: "/worktrees/active" });
yield* create("thread:archived", { worktreePath: "/worktrees/archived", archived: true });
yield* create("thread:local", { worktreePath: null });
const deleted = yield* create("thread:deleted", { worktreePath: "/worktrees/deleted" });
yield* store.apply({
id: EventId.make("event:thread:deleted:deleted"),
type: "thread.deleted",
threadId: deleted.id,
occurredAt: now,
payload: { ...deleted, deletedAt: now },
});

assert.deepEqual(yield* store.getThreadWorktreePaths(), [
{ threadId: ThreadId.make("thread:active"), worktreePath: "/worktrees/active" },
]);
});

it.effect("memory worktree path lookup reads only active threads", () =>
threadWorktreePathsReadActiveThreads.pipe(Effect.provide(ProjectionStore.layerMemory)),
);

it.effect("memory projection keeps restart-cancelled work through a stale run.updated", () =>
restartCancelledWorkSurvivesStaleRunUpdate.pipe(Effect.provide(ProjectionStore.layerMemory)),
);
Expand Down Expand Up @@ -553,6 +612,10 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
"SQL recovery selects unfinished runless native subagents",
() => runlessNativeSubagentNeedsRecovery,
);
it.effect(
"worktree path lookup reads only active threads",
() => threadWorktreePathsReadActiveThreads,
);

it.effect(
"keeps restart-cancelled work through a stale run.updated",
Expand Down
39 changes: 39 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,15 @@ export interface ProjectionStoreV2Shape {
readonly getThreadsWithPullRequests: (
threadId?: ThreadId,
) => Effect.Effect<ReadonlyArray<ProjectionThreadPullRequests>, ProjectionStoreV2Error>;
/**
* Active (not deleted, not archived) threads that have a worktree path, the
* same threads as the shell snapshot's `threads`. Reads only the thread rows,
* so callers can check who uses a path without a shell snapshot.
*/
readonly getThreadWorktreePaths: () => Effect.Effect<
ReadonlyArray<{ readonly threadId: ThreadId; readonly worktreePath: string }>,
ProjectionStoreV2Error
>;
readonly getTurnStartContext: (
threadId: ThreadId,
runId: RunId,
Expand Down Expand Up @@ -5643,6 +5652,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
);
}).pipe(Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })));

const getThreadWorktreePaths: ProjectionStoreV2Shape["getThreadWorktreePaths"] = () =>
sql<{ readonly thread_id: string; readonly worktree_path: string }>`
SELECT thread_id, json_extract(payload_json, '$.worktreePath') AS worktree_path
FROM orchestration_v2_projection_threads
WHERE deleted_at IS NULL
AND json_extract(payload_json, '$.archivedAt') IS NULL
AND json_extract(payload_json, '$.worktreePath') IS NOT NULL
`.pipe(
Effect.map((rows) =>
rows.map((row) => ({
threadId: ThreadId.make(row.thread_id),
worktreePath: row.worktree_path,
})),
),
Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })),
);

const shellThreadStateFromRow = (input: {
readonly row: ShellThreadRow;
readonly runOrdinalsByThreadId: ReadonlyMap<ThreadId, Map<RunId, number>>;
Expand Down Expand Up @@ -5979,6 +6005,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
getThread,
getSettlementCandidates,
getThreadsWithPullRequests,
getThreadWorktreePaths,
getThreadProjection,
getTurnStartContext,
getTurnStartHistory,
Expand Down Expand Up @@ -6127,6 +6154,18 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
left.id.localeCompare(right.id),
);
}),
getThreadWorktreePaths: () =>
Ref.get(replayState).pipe(
Effect.map((state) =>
[...state.projections.values()].flatMap(({ thread }) =>
thread.deletedAt === null &&
thread.archivedAt === null &&
thread.worktreePath !== null
? [{ threadId: thread.id, worktreePath: thread.worktreePath }]
: [],
),
),
),
getThreadsWithPullRequests: (threadId) =>
Ref.get(replayState).pipe(
Effect.map((state) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ it.effect(
getThread: () => Ref.get(projection).pipe(Effect.map((state) => state.thread)),
getSettlementCandidates: () => Effect.die("unused getSettlementCandidates"),
getThreadsWithPullRequests: () => Effect.die("unused getThreadsWithPullRequests"),
getThreadWorktreePaths: () => Effect.die("unused getThreadWorktreePaths"),
getThreadProjection: () => Effect.die("control effects must not load transcript"),
getTurnStartContext: () => Effect.die("unused"),
getTurnStartHistory: () => Effect.die("unused"),
Expand Down
Loading
Loading