GeezerVison is showing github.com — perf(server): port #13704 to V2 — pull request sync reads only thread… · pingdotgg/t3code@af6754b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProjectionSettlement.test.ts +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -298,6 +298,52 @@ for (const [name, testLayer] of [ | 298 | 298 | ); | 299 | 299 | } | 300 | 300 |
| | 301 | +const pullRequestLink = (number: number) => ({ | | 302 | + host: "github.com", | | 303 | + repository: "owner/repository", | | 304 | + number, | | 305 | + url: `https://github.com/owner/repository/pull/${number}`, | | 306 | + source: "manual" as const, | | 307 | + linkedAt: DateTime.formatIso(old), | | 308 | + snapshot: null, | | 309 | + stack: null, | | 310 | +}); | | 311 | + | | 312 | +for (const [name, testLayer] of [ | | 313 | + ["sql", SqlLayer], | | 314 | + ["memory", layerMemory], | | 315 | +] as const) { | | 316 | + it.effect(`${name}: lists only active threads with pull request links, oldest first`, () => | | 317 | + Effect.gen(function* () { | | 318 | + const store = yield* ProjectionStoreV2; | | 319 | + yield* createThread("no-links"); | | 320 | + yield* createThread("empty-links", { pullRequests: [] }); | | 321 | + yield* createThread("archived-link", { | | 322 | + archivedAt: old, | | 323 | + pullRequests: [pullRequestLink(1)], | | 324 | + }); | | 325 | + const settled = yield* createThread("settled-link", { | | 326 | + settledOverride: "settled", | | 327 | + settledAt: old, | | 328 | + updatedAt: DateTime.subtract(now, { days: 12 }), | | 329 | + pullRequests: [pullRequestLink(2)], | | 330 | + }); | | 331 | + const open = yield* createThread("open-link", { | | 332 | + pullRequests: [pullRequestLink(3), pullRequestLink(4)], | | 333 | + }); | | 334 | + | | 335 | + const threads = yield* store.getThreadsWithPullRequests(); | | 336 | + assert.deepEqual( | | 337 | + threads.map((thread) => [thread.id, thread.settledOverride, thread.pullRequests?.length]), | | 338 | + [ | | 339 | + [settled, "settled", 1], | | 340 | + [open, null, 2], | | 341 | + ], | | 342 | + ); | | 343 | + }).pipe(Effect.provide(testLayer)), | | 344 | + ); | | 345 | +} | | 346 | + | 301 | 347 | it.effect( | 302 | 348 | "reads settlement candidates and thread metadata without loading historical or archived payloads", | 303 | 349 | () => | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProjectionStore.ts +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -149,6 +149,12 @@ export type ProjectionLimitRecoveryCandidate = Pick< | 149 | 149 | | "snoozedUntil" | 150 | 150 | >; | 151 | 151 |
| | 152 | +/** The thread fields pull request sync reads, for a thread with at least one link. */ | | 153 | +export type ProjectionThreadPullRequests = Pick< | | 154 | + OrchestrationV2AppThread, | | 155 | + "id" | "projectId" | "settledOverride" | "settledAt" | "pullRequests" | | 156 | +>; | | 157 | + | 152 | 158 | /** Thread activity needed by settlement, without transcript or fork history. */ | 153 | 159 | export type ProjectionSettlementCandidate = Pick< | 154 | 160 | OrchestrationV2ThreadShell, | @@ -331,6 +337,14 @@ export interface ProjectionStoreV2Shape { | 331 | 337 | ReadonlyArray<ProjectionSettlementCandidate>, | 332 | 338 | ProjectionStoreV2Error | 333 | 339 | >; | | 340 | + /** | | 341 | + * Active (not deleted, not archived) threads with at least one pull request | | 342 | + * link, in shell snapshot order. Skips run, message and item reads. | | 343 | + */ | | 344 | + readonly getThreadsWithPullRequests: () => Effect.Effect< | | 345 | + ReadonlyArray<ProjectionThreadPullRequests>, | | 346 | + ProjectionStoreV2Error | | 347 | + >; | 334 | 348 | readonly getTurnStartContext: ( | 335 | 349 | threadId: ThreadId, | 336 | 350 | runId: RunId, | @@ -5029,6 +5043,29 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 5029 | 5043 | ) | 5030 | 5044 | .pipe(Effect.mapError((cause) => new ProjectionStoreSetupError({ cause }))); | 5031 | 5045 |
| | 5046 | + const getThreadsWithPullRequests: ProjectionStoreV2Shape["getThreadsWithPullRequests"] = () => | | 5047 | + Effect.gen(function* () { | | 5048 | + const rows = yield* sql<PayloadRow>` | | 5049 | + SELECT payload_json | | 5050 | + FROM orchestration_v2_projection_threads | | 5051 | + WHERE deleted_at IS NULL | | 5052 | + AND json_extract(payload_json, '$.archivedAt') IS NULL | | 5053 | + AND json_array_length(payload_json, '$.pullRequests') > 0 | | 5054 | + ORDER BY updated_at ASC, thread_id ASC | | 5055 | + `; | | 5056 | + return yield* Effect.forEach(rows, (row) => | | 5057 | + decodeThreadPayload(row.payload_json).pipe( | | 5058 | + Effect.map((thread): ProjectionThreadPullRequests => ({ | | 5059 | + id: thread.id, | | 5060 | + projectId: thread.projectId, | | 5061 | + settledOverride: thread.settledOverride, | | 5062 | + settledAt: thread.settledAt, | | 5063 | + pullRequests: thread.pullRequests ?? [], | | 5064 | + })), | | 5065 | + ), | | 5066 | + ); | | 5067 | + }).pipe(Effect.mapError((cause) => new ProjectionStoreSetupError({ cause }))); | | 5068 | + | 5032 | 5069 | const shellThreadStateFromRow = (input: { | 5033 | 5070 | readonly row: ShellThreadRow; | 5034 | 5071 | readonly runOrdinalsByThreadId: ReadonlyMap<ThreadId, Map<RunId, number>>; | @@ -5292,6 +5329,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 5292 | 5329 | getThreadShell, | 5293 | 5330 | getThread, | 5294 | 5331 | getSettlementCandidates, | | 5332 | + getThreadsWithPullRequests, | 5295 | 5333 | getThreadProjection, | 5296 | 5334 | getTurnStartContext, | 5297 | 5335 | getTurnStartHistory, | @@ -5413,6 +5451,31 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect( | 5413 | 5451 | left.id.localeCompare(right.id), | 5414 | 5452 | ); | 5415 | 5453 | }), | | 5454 | + getThreadsWithPullRequests: () => | | 5455 | + Ref.get(replayState).pipe( | | 5456 | + Effect.map((state) => | | 5457 | + [...state.projections.values()] | | 5458 | + .map(({ thread }) => thread) | | 5459 | + .filter( | | 5460 | + (thread) => | | 5461 | + thread.deletedAt === null && | | 5462 | + thread.archivedAt === null && | | 5463 | + (thread.pullRequests ?? []).length > 0, | | 5464 | + ) | | 5465 | + .toSorted( | | 5466 | + (left, right) => | | 5467 | + DateTime.toEpochMillis(left.updatedAt) - | | 5468 | + DateTime.toEpochMillis(right.updatedAt) || left.id.localeCompare(right.id), | | 5469 | + ) | | 5470 | + .map((thread): ProjectionThreadPullRequests => ({ | | 5471 | + id: thread.id, | | 5472 | + projectId: thread.projectId, | | 5473 | + settledOverride: thread.settledOverride, | | 5474 | + settledAt: thread.settledAt, | | 5475 | + pullRequests: thread.pullRequests ?? [], | | 5476 | + })), | | 5477 | + ), | | 5478 | + ), | 5416 | 5479 | getLimitRecoveryCandidates: (options) => | 5417 | 5480 | Ref.get(replayState).pipe( | 5418 | 5481 | Effect.map((state) => | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -219,6 +219,7 @@ it.effect( | 219 | 219 | getThreadShell: () => Effect.die("unused getThreadShell"), | 220 | 220 | getThread: () => Ref.get(projection).pipe(Effect.map((state) => state.thread)), | 221 | 221 | getSettlementCandidates: () => Effect.die("unused getSettlementCandidates"), | | 222 | + getThreadsWithPullRequests: () => Effect.die("unused getThreadsWithPullRequests"), | 222 | 223 | getThreadProjection: () => Effect.die("control effects must not load transcript"), | 223 | 224 | getTurnStartContext: () => Effect.die("unused"), | 224 | 225 | getTurnStartHistory: () => Effect.die("unused"), | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration/PullRequestSyncReactor.test.ts +26-9Lines changed: 26 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ import { | 16 | 16 | } from "@t3tools/contracts"; | 17 | 17 | import { assert, describe, it } from "@effect/vitest"; | 18 | 18 | import * as Crypto from "effect/Crypto"; | | 19 | +import * as DateTime from "effect/DateTime"; | 19 | 20 | import * as Deferred from "effect/Deferred"; | 20 | 21 | import * as Effect from "effect/Effect"; | 21 | 22 | import * as Layer from "effect/Layer"; | @@ -26,7 +27,7 @@ import { TestClock } from "effect/testing"; | 26 | 27 | import { PullRequestService } from "../pullRequest/PullRequestService.ts"; | 27 | 28 | import { ServerActivation } from "../serverActivation.ts"; | 28 | 29 | import { OrchestratorV2, type OrchestratorV2Shape } from "../orchestration-v2/Orchestrator.ts"; | 29 | | -import { v2PullRequestThread } from "../orchestration-v2/testkit/pullRequestFixtures.ts"; | | 30 | +import { ProjectionStoreV2 } from "../orchestration-v2/ProjectionStore.ts"; | 30 | 31 | import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts"; | 31 | 32 | import * as PullRequestSyncReactor from "./PullRequestSyncReactor.ts"; | 32 | 33 |
| @@ -164,6 +165,7 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options: | 164 | 165 | const activation = yield* Deferred.make<void>(); | 165 | 166 | const snapshots = yield* Ref.make(options.snapshot); | 166 | 167 | const snapshotReads = yield* Queue.unbounded<void>(); | | 168 | + const shellSnapshotReads = yield* Ref.make(0); | 167 | 169 | const syncCommands = yield* Ref.make<ReadonlyArray<SyncCommand>>([]); | 168 | 170 | const linkCommands = yield* Ref.make<ReadonlyArray<LinkCommand>>([]); | 169 | 171 | const summaryCalls = yield* Ref.make<ReadonlyArray<PullRequestRef>>([]); | @@ -204,16 +206,28 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options: | 204 | 206 | stack, | 205 | 207 | invalidate: options.invalidate ?? (() => Effect.void), | 206 | 208 | }), | 207 | | - Layer.mock(OrchestratorV2)({ | 208 | | - getShellSnapshot: () => | | 209 | + Layer.mock(ProjectionStoreV2)({ | | 210 | + // Mirrors the store's filter: active threads that have at least one link. | | 211 | + getThreadsWithPullRequests: () => | 209 | 212 | Queue.offer(snapshotReads, undefined).pipe( | 210 | 213 | Effect.andThen(Ref.get(snapshots)), | 211 | | - Effect.map((snapshot) => ({ | 212 | | - schemaVersion: 2, | 213 | | - snapshotSequence: snapshot.snapshotSequence, | 214 | | - threads: snapshot.threads.map(v2PullRequestThread), | 215 | | - archivedThreads: [], | 216 | | - })), | | 214 | + Effect.map((snapshot) => | | 215 | + snapshot.threads | | 216 | + .filter((thread) => thread.archivedAt === null && thread.pullRequests.length > 0) | | 217 | + .map((thread) => ({ | | 218 | + id: thread.id, | | 219 | + projectId: thread.projectId, | | 220 | + settledOverride: thread.settledOverride, | | 221 | + settledAt: thread.settledAt === null ? null : DateTime.makeUnsafe(thread.settledAt), | | 222 | + pullRequests: thread.pullRequests, | | 223 | + })), | | 224 | + ), | | 225 | + ), | | 226 | + }), | | 227 | + Layer.mock(OrchestratorV2)({ | | 228 | + getShellSnapshot: () => | | 229 | + Ref.update(shellSnapshotReads, (count) => count + 1).pipe( | | 230 | + Effect.andThen(Effect.die(new Error("pull request sync must not read the shell"))), | 217 | 231 | ), | 218 | 232 | dispatch, | 219 | 233 | streamDomainEvents: Stream.empty, | @@ -226,6 +240,7 @@ const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options: | 226 | 240 | activation, | 227 | 241 | snapshots, | 228 | 242 | snapshotReads, | | 243 | + shellSnapshotReads, | 229 | 244 | syncCommands, | 230 | 245 | linkCommands, | 231 | 246 | summaryCalls, | @@ -386,6 +401,8 @@ describe("PullRequestSyncReactor", () => { | 386 | 401 | ], | 387 | 402 | ); | 388 | 403 | assert.strictEqual((yield* Ref.get(fixture.stackCalls)).length, 1); | | 404 | + // Reads only linked threads, never the full shell snapshot of every thread. | | 405 | + assert.strictEqual(yield* Ref.get(fixture.shellSnapshotReads), 0); | 389 | 406 | }).pipe(Effect.provide(fixture.layer)); | 390 | 407 | }), | 391 | 408 | ), | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration/PullRequestSyncReactor.ts +12-9Lines changed: 12 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { siblingPullRequestUrl } from "@t3tools/shared/changeRequestUrl"; | 2 | 2 | import { | 3 | 3 | CommandId, | 4 | | - type OrchestrationV2ThreadShell, | 5 | 4 | type PullRequestSummary, | 6 | 5 | type ThreadPullRequestKey, | 7 | 6 | type ThreadPullRequestLink, | @@ -29,13 +28,17 @@ import * as Stream from "effect/Stream"; | 29 | 28 | import * as PullRequestService from "../pullRequest/PullRequestService.ts"; | 30 | 29 | import { forkParked } from "../serverActivation.ts"; | 31 | 30 | import { OrchestratorV2 } from "../orchestration-v2/Orchestrator.ts"; | | 31 | +import { | | 32 | + ProjectionStoreV2, | | 33 | + type ProjectionThreadPullRequests, | | 34 | +} from "../orchestration-v2/ProjectionStore.ts"; | 32 | 35 |
| 33 | 36 | const SLOW_SYNC_INTERVAL_MS = 15 * 60 * 1_000; | 34 | 37 |
| 35 | 38 | type SnapshotFields = Omit<ThreadPullRequestSnapshot, "syncedAt">; | 36 | 39 |
| 37 | 40 | interface LinkEntry { | 38 | | - readonly thread: OrchestrationV2ThreadShell; | | 41 | + readonly thread: ProjectionThreadPullRequests; | 39 | 42 | readonly link: ThreadPullRequestLink; | 40 | 43 | } | 41 | 44 |
| @@ -103,15 +106,15 @@ function stacksEqual( | 103 | 106 | ); | 104 | 107 | } | 105 | 108 |
| 106 | | -function isUnsettled(thread: OrchestrationV2ThreadShell): boolean { | | 109 | +function isUnsettled(thread: ProjectionThreadPullRequests): boolean { | 107 | 110 | return thread.settledOverride !== "settled" && thread.settledAt === null; | 108 | 111 | } | 109 | 112 |
| 110 | 113 | /** | 111 | 114 | * Keeps every thread ↔ pull request link's host snapshot current. One sweep a minute reads | 112 | | - * the shell snapshot, groups visible links by pull request so the host is asked once per PR | 113 | | - * no matter how many threads share it, and writes back only what changed. Native stacks the | 114 | | - * host reports are auto-linked to the thread as `source: "stack"`. | | 115 | + * only the active threads that have links, groups visible links by pull request so the host | | 116 | + * is asked once per PR no matter how many threads share it, and writes back only what | | 117 | + * changed. Native stacks the host reports are auto-linked to the thread as `source: "stack"`. | 115 | 118 | */ | 116 | 119 | export class PullRequestSyncReactor extends Context.Service< | 117 | 120 | PullRequestSyncReactor, | @@ -126,6 +129,7 @@ export class PullRequestSyncReactor extends Context.Service< | 126 | 129 | /** @public Service construction is part of the canonical Effect module API. */ | 127 | 130 | export const make = Effect.gen(function* () { | 128 | 131 | const engine = yield* OrchestratorV2; | | 132 | + const projections = yield* ProjectionStoreV2; | 129 | 133 | const pullRequests = yield* PullRequestService.PullRequestService; | 130 | 134 | const crypto = yield* Crypto.Crypto; | 131 | 135 |
| @@ -151,14 +155,13 @@ export const make = Effect.gen(function* () { | 151 | 155 | Cause.hasInterruptsOnly(cause) ? Effect.failCause(cause) : Effect.logWarning(message, fields); | 152 | 156 |
| 153 | 157 | const sweep = Effect.fn("PullRequestSyncReactor.sweep")(function* (requestedKey?: string) { | 154 | | - const snapshot = yield* engine.getShellSnapshot(); | | 158 | + const threads = yield* projections.getThreadsWithPullRequests(); | 155 | 159 | const now = yield* DateTime.now; | 156 | 160 | const nowMs = DateTime.toEpochMillis(now); | 157 | 161 | const nowIso = DateTime.formatIso(now); | 158 | 162 |
| 159 | 163 | const groups = new Map<string, Array<LinkEntry>>(); | 160 | | - for (const thread of snapshot.threads) { | 161 | | - if (thread.archivedAt !== null) continue; | | 164 | + for (const thread of threads) { | 162 | 165 | for (const link of visibleThreadPullRequests(thread.pullRequests ?? [])) { | 163 | 166 | const key = threadPullRequestKeyOf(link); | 164 | 167 | const entries = groups.get(key) ?? []; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/server.ts +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -513,7 +513,11 @@ const RuntimeCoreDependenciesBaseLive = Layer.mergeAll( | 513 | 513 | const service = yield* PullRequestSyncReactor.PullRequestSyncReactor; | 514 | 514 | yield* service.start(); | 515 | 515 | }), | 516 | | - ).pipe(Layer.provideMerge(PullRequestSyncReactor.layer), Layer.provide(PullRequestServiceLive)), | | 516 | + ).pipe( | | 517 | + Layer.provideMerge(PullRequestSyncReactor.layer), | | 518 | + Layer.provide(PullRequestServiceLive), | | 519 | + Layer.provide(ProjectionStoreV2.layer), | | 520 | + ), | 517 | 521 | // Subscribes to `account.rate-limits.updated` so usage bars track live | 518 | 522 | // telemetry instead of waiting for the next status probe. | 519 | 523 | ProviderUsageLimitsIngestionLive, | |
|