28
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
29
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
30
import * as ServerActivation from "../serverActivation.ts";
31
+import * as ProjectionStore from "./ProjectionStore.ts";
32
+import * as SqlClient from "effect/sql/SqlClient";
33
+import * as SqlitePersistence from "../persistence/Sqlite.ts";
34
+import { EventId } from "@t3tools/contracts";
35
import * as Orchestrator from "./Orchestrator.ts";
36
import * as ThreadPullRequestService from "./ThreadPullRequestService.ts";
37
109
const dependencies = Layer.mergeAll(
110
Layer.mock(Orchestrator.OrchestratorV2)({
111
streamDomainEvents: Stream.fromQueue(domainEvents),
108
- getShellSnapshot: () =>
112
+ getPullRequestSnapshot: () =>
113
Effect.succeed({
114
schemaVersion: 2,
115
snapshotSequence: 1,
116
threads: [thread],
117
archivedThreads: [],
118
}),
115
- getThreadEventSequence: () => Effect.succeed(1),
116
- getThreadShell: () => Effect.succeed(thread),
119
}),
120
Layer.mock(ProjectStore.ProjectStoreV2)({
121
listShells: () => Effect.succeed([project]),
214
Effect.gen(function* () {
215
const thread = {
216
...threadShell("worktree-thread"),
217
+ settledOverride: "settled" as const,
218
+ settledAt: NOW,
219
branch: "feature/worktree",
220
worktreePath: "/workspace/worktree",
221
};
234
const dependencies = Layer.mergeAll(
235
Layer.mock(Orchestrator.OrchestratorV2)({
236
streamDomainEvents: Stream.never,
233
- getShellSnapshot: () =>
237
+ getPullRequestSnapshot: () =>
238
Deferred.succeed(sweepStarted, undefined).pipe(
239
Effect.as({
240
schemaVersion: 2,
339
const dependencies = Layer.mergeAll(
340
Layer.mock(Orchestrator.OrchestratorV2)({
341
streamDomainEvents: Stream.fromQueue(domainEvents),
338
- getShellSnapshot: () =>
339
- Queue.offer(sweeps, undefined).pipe(
340
- Effect.as({
341
- schemaVersion: 2,
342
- snapshotSequence: 1,
343
- threads: [parent, child],
344
- archivedThreads: [],
345
- }),
346
- ),
347
- getThreadEventSequence: () => Effect.succeed(1),
348
- getThreadShell: (threadId) =>
349
- Effect.succeed([parent, child].find((thread) => thread.id === threadId) ?? null),
342
+ getPullRequestSnapshot: ({ threadId }) =>
343
+ threadId !== undefined
344
+ ? Effect.succeed({ snapshotSequence: 1, threads: [child] })
345
+ : Queue.offer(sweeps, undefined).pipe(
346
+ Effect.as({
347
+ schemaVersion: 2,
348
+ snapshotSequence: 1,
349
+ threads: [parent, child],
350
+ archivedThreads: [],
351
+ }),
352
+ ),
353
}),
354
Layer.mock(ProjectStore.ProjectStoreV2)({
355
listShells: () => Effect.succeed([project]),
460
const dependencies = Layer.mergeAll(
461
Layer.mock(Orchestrator.OrchestratorV2)({
462
streamDomainEvents: Stream.never,
460
- getShellSnapshot: () =>
461
- Deferred.succeed(sweepStarted, undefined).pipe(
462
- Effect.as({
463
- schemaVersion: 2,
464
- snapshotSequence: 1,
465
- threads: [thread],
466
- archivedThreads: [],
467
- }),
463
+ getPullRequestSnapshot: (options) =>
464
+ Effect.gen(function* () {
465
+ const store = yield* ProjectionStore.ProjectionStoreV2;
466
+ yield* store.apply({
467
+ id: EventId.make(`event:${thread.id}:created`),
468
+ type: "thread.created",
469
+ threadId: thread.id,
470
+ occurredAt: NOW,
471
+ payload: { ...thread, lastVisitedAt: thread.lastVisitedAt ?? null },
472
+ });
473
+ const sql = yield* SqlClient.SqlClient;
474
+ yield* sql`INSERT INTO orchestration_events (
475
+ sequence, event_id, aggregate_kind, stream_id, stream_version, event_type,
476
+ occurred_at, actor_kind, payload_json, metadata_json, application_event_version
477
+ ) VALUES (7, 'pr-discovery', 'thread', ${thread.id}, 1, 'thread.created',
478
+ '2026-09-20T00:00:00.000Z', 'system', '{}', '{}', 2)`;
479
+ for (const table of [
480
+ "orchestration_v2_projection_turn_items",
481
+ "orchestration_v2_projection_messages",
482
+ "orchestration_v2_projection_provider_threads",
483
+ "orchestration_v2_projection_runs",
484
+ ])
485
+ yield* sql.unsafe(`DROP TABLE ${table}`);
486
+ const snapshot = yield* store.getPullRequestSnapshot(options);
487
+ yield* Deferred.succeed(sweepStarted, undefined);
488
+ return snapshot;
489
+ }).pipe(
490
+ Effect.provide(
491
+ ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
492
+ ),
493
+ Effect.orDie,
494
),
495
dispatch: (command) =>
496
Effect.sync(() => {
555
expect(commands[0]).toMatchObject({
556
type: "thread.pull-request.sync",
557
threadId: thread.id,
558
+ snapshotSequence: 7,
559
+ expected: {
560
+ workspaceRoot: project.workspaceRoot,
561
+ branch: thread.branch,
562
+ worktreePath: thread.worktreePath,
563
+ linkedPullRequest: savedReference,
564
+ branchPullRequest: savedReference,
565
+ },
566
branchPullRequest: null,
567
});
568
expect(commands[0]).not.toHaveProperty("linkedPullRequest");