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
38 changes: 38 additions & 0 deletions apps/server/src/orchestration-v2/PullRequestSyncReactor.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as NodeV8 from "node:v8";

import { SourceControlProviderKind } from "@t3tools/contracts";
import * as Stream from "effect/Stream";
import {
Expand All @@ -21,10 +23,12 @@ import {
type ThreadPullRequestSnapshot,
} from "@t3tools/contracts";
import { assert, describe, it } from "@effect/vitest";
import * as Cache from "effect/Cache";
import * as Clock from "effect/Clock";
import * as Crypto from "effect/Crypto";
import * as DateTime from "effect/DateTime";
import * as Deferred from "effect/Deferred";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Queue from "effect/Queue";
Expand Down Expand Up @@ -496,6 +500,40 @@ const mergingSnapshot = () =>
makeSnapshot([makeThread(MERGING_THREAD, { pullRequests: [makeLink(7, { state: "open" })] })]);

describe("PullRequestSyncReactor", () => {
it.effect("lets a finished sweep's links go while a cache keeps the fiber that read them", () =>
Effect.gen(function* () {
class SweptLink {
readonly swept = true;
}
// Like PullRequestReadCache's revisions: a lookup fiber, kept for good, forked from the reader.
const kept = yield* Cache.makeWith(() => Effect.void, {
capacity: 1,
timeToLive: () => Duration.infinity,
});
const fixture = yield* makeHarness({
snapshot: makeSnapshot([]),
summary: (input) => Cache.get(kept, undefined).pipe(Effect.as(makeSummary(input))),
});
yield* Ref.set(
fixture.snapshots,
makeSnapshot(
Array.from({ length: 30 }, (_, index) =>
makeThread(`thread-${index}`, {
pullRequests: [Object.assign(new SweptLink(), makeLink(7))],
}),
),
),
);
yield* Effect.gen(function* () {
yield* startAndSweep(fixture);
assert.strictEqual((yield* Ref.get(fixture.syncCommands)).length, 30);
yield* Ref.set(fixture.snapshots, makeSnapshot([]));

assert.strictEqual(NodeV8.queryObjects(SweptLink, { format: "count" }), 0);
}).pipe(Effect.provide(fixture.layer));
}),
);

it.effect("retries a failed stack read after the summary becomes terminal", () =>
Effect.scoped(
Effect.gen(function* () {
Expand Down
13 changes: 6 additions & 7 deletions apps/server/src/orchestration-v2/PullRequestSyncReactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ export const make = Effect.gen(function* () {
const stacksReadThisSweep = new Map<string, ThreadPullRequestStack | null>();
const persistence = yield* Semaphore.make(1);

const syncEntry = Effect.fn("PullRequestSyncReactor.syncEntry")(function* (
// Effect.fn's stack frames close over these sweep-scoped bodies and their arguments, and a
// cache lookup forked inside keeps those frames, so a plain span leaves the sweep collectable.
const syncEntry = Effect.fnUntraced(function* (
entry: LinkEntry,
fields: SnapshotFields,
fetchedStack: { readonly stack: ThreadPullRequestStack | null } | null,
Expand Down Expand Up @@ -446,12 +448,9 @@ export const make = Effect.gen(function* () {
stack: nextStack,
});
}
});
}, Effect.withSpan("PullRequestSyncReactor.syncEntry"));

const syncGroup = Effect.fn("PullRequestSyncReactor.syncGroup")(function* (
key: string,
entries: ReadonlyArray<LinkEntry>,
) {
const syncGroup = Effect.fnUntraced(function* (key: string, entries: ReadonlyArray<LinkEntry>) {
const first = entries[0]!;
const ref = {
projectId: first.thread.projectId,
Expand Down Expand Up @@ -554,7 +553,7 @@ export const make = Effect.gen(function* () {
),
{ discard: true },
);
});
}, Effect.withSpan("PullRequestSyncReactor.syncGroup"));

const skips = new Map<string, { count: number; readonly key: string }>();
const readGroup = (key: string, entries: ReadonlyArray<LinkEntry>, pauseKey: string) =>
Expand Down
Loading