GeezerVison is showing github.com — perf(server): rank queued provider snapshots by focus and combine sup… · orbitingflea/t3code@db51b67
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/EventSink.ts +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,7 @@ import { | 15 | 15 | ThreadId, | 16 | 16 | } from "@t3tools/contracts"; | 17 | 17 | import * as ProviderCommitBatch from "./ProviderCommitBatch.ts"; | | 18 | +import * as ThreadViewers from "./ThreadViewers.ts"; | 18 | 19 | import * as Context from "effect/Context"; | 19 | 20 | import * as DateTime from "effect/DateTime"; | 20 | 21 | import * as Effect from "effect/Effect"; | @@ -96,6 +97,7 @@ export interface EventSinkV2Shape { | 96 | 97 | process: (event: A) => Effect.Effect<void, E2, R2>, | 97 | 98 | after: Effect.Effect<boolean, E2, R2>, | 98 | 99 | canBatch: Effect.Effect<boolean, never, R2>, | | 100 | + priority?: ProviderCommitBatch.StreamPriority, | 99 | 101 | ) => Effect.Effect< | 100 | 102 | void, | 101 | 103 | E | E2 | ProviderCommitBatch.CommitBatchError | Schema.SchemaError, | @@ -277,10 +279,12 @@ const layerBase: Layer.Layer< | 277 | 279 | | ProjectStore.ProjectStoreV2 | 278 | 280 | | SqlClient.SqlClient | 279 | 281 | | TurnItemPositionStore.TurnItemPositionStoreV2 | | 282 | + | ThreadViewers.ThreadViewers | 280 | 283 | > = Layer.effect( | 281 | 284 | EventSinkV2, | 282 | 285 | Effect.gen(function* () { | 283 | 286 | const sql = yield* SqlClient.SqlClient; | | 287 | + const threadViewers = yield* ThreadViewers.ThreadViewers; | 284 | 288 | const commandReceipts = yield* CommandReceiptStore.CommandReceiptStoreV2; | 285 | 289 | const effectOutbox = yield* EffectOutbox.EffectOutboxV2; | 286 | 290 | const eventStore = yield* EventStore.EventStoreV2; | @@ -1174,6 +1178,7 @@ const layerBase: Layer.Layer< | 1174 | 1178 | ), | 1175 | 1179 | ); | 1176 | 1180 | }), | | 1181 | + threadViewers.isViewed, | 1177 | 1182 | ); | 1178 | 1183 |
| 1179 | 1184 | return EventSinkV2.of({ | @@ -1327,6 +1332,7 @@ export const layer: Layer.Layer< | 1327 | 1332 | EffectOutbox.layer, | 1328 | 1333 | ProjectStore.layer, | 1329 | 1334 | TurnItemPositionStore.layer, | | 1335 | + ThreadViewers.layer, | 1330 | 1336 | ), | 1331 | 1337 | ), | 1332 | 1338 | ); |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/FoundationPersistence.test.ts +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -64,6 +64,7 @@ import * as CommandReceiptStore from "./CommandReceiptStore.ts"; | 64 | 64 | import * as EffectOutbox from "./EffectOutbox.ts"; | 65 | 65 | import * as EffectWorker from "./EffectWorker.ts"; | 66 | 66 | import * as EventSink from "./EventSink.ts"; | | 67 | +import * as ThreadViewers from "./ThreadViewers.ts"; | 67 | 68 | import * as EventStore from "./EventStore.ts"; | 68 | 69 | import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; | 69 | 70 | import * as ProjectionMaintenance from "./ProjectionMaintenance.ts"; | @@ -491,7 +492,13 @@ it.effect("commits other threads' writes ahead of a thread's queued provider sna | 491 | 492 | .pipe(Effect.forkScoped({ startImmediately: true })); | 492 | 493 | yield* Queue.offer(input, snapshot); | 493 | 494 | yield* Deferred.await(started); | 494 | | - yield* Queue.offerAll(input, [snapshot, snapshot]); | | 495 | + yield* Queue.offerAll( | | 496 | + input, | | 497 | + [2, 3].map((n) => ({ | | 498 | + ...snapshot, | | 499 | + node: { ...busyNode.payload, id: NodeId.make(`node:busy:${n}`) }, | | 500 | + })), | | 501 | + ); | 495 | 502 | yield* Deferred.await(pulled); | 496 | 503 | yield* TestClock.adjust(0); | 497 | 504 | const writeTo = (event: OrchestrationV2DomainEvent, id: string) => | @@ -4310,6 +4317,7 @@ it.effect.each(["direct", "provider-batch", "cancel-provider-batch"] as const)( | 4310 | 4317 | layerCommandReceiptStoreProvided, | 4311 | 4318 | ProjectStore.layer.pipe(Layer.provide(layerDatabase)), | 4312 | 4319 | TurnItemPositionStore.layer.pipe(Layer.provide(layerDatabase)), | | 4320 | + ThreadViewers.layer, | 4313 | 4321 | ), | 4314 | 4322 | ), | 4315 | 4323 | ); | @@ -4608,6 +4616,7 @@ it.effect.each(["cancel transaction", "fail transaction", "cancel publication"] | 4608 | 4616 | layerCommandReceiptStoreProvided, | 4609 | 4617 | ProjectStore.layer.pipe(Layer.provide(layerDatabase)), | 4610 | 4618 | TurnItemPositionStore.layer.pipe(Layer.provide(layerDatabase)), | | 4619 | + ThreadViewers.layer, | 4611 | 4620 | ), | 4612 | 4621 | ), | 4613 | 4622 | ); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProviderCommitBatch.test.ts +157-1Lines changed: 157 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,7 +21,7 @@ function event(index: number, text = `delta:${index}`): ProviderAdapter.Provider | 21 | 21 | type: "message.updated", | 22 | 22 | driver, | 23 | 23 | message: { | 24 | | - id: MessageId.make("single-message"), | | 24 | + id: MessageId.make(`message:${index}`), | 25 | 25 | threadId, | 26 | 26 | runId: null, | 27 | 27 | nodeId: null, | @@ -36,6 +36,15 @@ function event(index: number, text = `delta:${index}`): ProviderAdapter.Provider | 36 | 36 | }, | 37 | 37 | }; | 38 | 38 | } | | 39 | +function snapshot(id: string, text: string, threadId = "single-thread") { | | 40 | + const base = event(0, text); | | 41 | + return base.type === "message.updated" | | 42 | + ? { | | 43 | + ...base, | | 44 | + message: { ...base.message, id: MessageId.make(id), threadId: ThreadId.make(threadId) }, | | 45 | + } | | 46 | + : base; | | 47 | +} | 39 | 48 | const control: ProviderAdapter.ProviderAdapterV2Event = { | 40 | 49 | type: "background_work.updated", | 41 | 50 | driver, | @@ -45,6 +54,7 @@ const control: ProviderAdapter.ProviderAdapterV2Event = { | 45 | 54 |
| 46 | 55 | const recordBatches = Effect.fn("test.recordProviderBatches")(function* ( | 47 | 56 | beforeCommit: Effect.Effect<void> = Effect.void, | | 57 | + isViewed?: (threadId: string) => boolean, | 48 | 58 | ) { | 49 | 59 | const output = yield* Queue.unbounded<ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>>(); | 50 | 60 | let current: ProviderAdapter.ProviderAdapterV2Event[] = []; | @@ -57,6 +67,8 @@ const recordBatches = Effect.fn("test.recordProviderBatches")(function* ( | 57 | 67 | if (current.length > 0) yield* Queue.offer(output, current); | 58 | 68 | }), | 59 | 69 | true, | | 70 | + undefined, | | 71 | + isViewed, | 60 | 72 | ); | 61 | 73 | const process = (raw: ProviderAdapter.ProviderAdapterV2Event) => | 62 | 74 | raw.type === "background_work.updated" | @@ -463,6 +475,150 @@ it.effect.each(["none", "writer", "snapshots"] as const)( | 463 | 475 | }), | 464 | 476 | ); | 465 | 477 |
| | 478 | +const blockFirstCommit = Effect.gen(function* () { | | 479 | + const started = yield* Deferred.make<void>(); | | 480 | + const release = yield* Deferred.make<void>(); | | 481 | + let first = true; | | 482 | + const block = Effect.gen(function* () { | | 483 | + if (!first) return; | | 484 | + first = false; | | 485 | + yield* Deferred.succeed(started, undefined); | | 486 | + yield* Deferred.await(release); | | 487 | + }); | | 488 | + return { started, release, block }; | | 489 | +}); | | 490 | + | | 491 | +it.effect.each([ | | 492 | + { keys: ["a", "b", "a", "b", "a", "b"], kept: [0, 4, 5] }, | | 493 | + // A first appearance between two snapshots of an entity keeps the earlier snapshot. | | 494 | + { keys: ["a", "b", "a", "b", "c", "a", "b"], kept: [0, 2, 3, 4, 5, 6] }, | | 495 | +])("combines superseded snapshots queued behind a busy commit: $keys", ({ keys, kept }) => | | 496 | + Effect.gen(function* () { | | 497 | + const writer = yield* blockFirstCommit; | | 498 | + const recorder = yield* recordBatches(writer.block); | | 499 | + const leading = yield* recorder | | 500 | + .consume(Stream.make(event(-1)), recorder.process, Effect.succeed(false)) | | 501 | + .pipe(Effect.forkScoped({ startImmediately: true })); | | 502 | + yield* Deferred.await(writer.started); | | 503 | + const snapshots = keys.map((key, index) => snapshot(key, `${key}:${index}`)); | | 504 | + const pulled = yield* Deferred.make<void>(); | | 505 | + const consumer = yield* recorder | | 506 | + .consume( | | 507 | + Stream.fromIterable(snapshots).pipe( | | 508 | + Stream.tap((raw) => | | 509 | + raw === snapshots.at(-1) ? Deferred.succeed(pulled, undefined) : Effect.void, | | 510 | + ), | | 511 | + ), | | 512 | + recorder.process, | | 513 | + Effect.succeed(false), | | 514 | + ) | | 515 | + .pipe(Effect.forkScoped({ startImmediately: true })); | | 516 | + yield* Deferred.await(pulled); | | 517 | + yield* TestClock.adjust(0); | | 518 | + yield* Deferred.succeed(writer.release, undefined); | | 519 | + yield* Fiber.join(leading); | | 520 | + yield* Fiber.join(consumer); | | 521 | + assert.deepEqual(yield* Queue.takeAll(recorder.output), [ | | 522 | + [event(-1)], | | 523 | + kept.map((index) => snapshots[index]!), | | 524 | + ]); | | 525 | + }), | | 526 | +); | | 527 | + | | 528 | +it.effect("makes streams wait to read more once the shared backlog is full", () => | | 529 | + Effect.gen(function* () { | | 530 | + const writer = yield* blockFirstCommit; | | 531 | + const recorder = yield* recordBatches(writer.block); | | 532 | + const leading = yield* recorder | | 533 | + .consume(Stream.make(event(-1)), recorder.process, Effect.succeed(false)) | | 534 | + .pipe(Effect.forkScoped({ startImmediately: true })); | | 535 | + yield* Deferred.await(writer.started); | | 536 | + let arrivals = 0; | | 537 | + const streams = Array.from({ length: 5 }, (_, stream) => | | 538 | + Array.from({ length: 64 }, (_, n) => snapshot(`${stream}:${n}`, `${stream}:${n}`)), | | 539 | + ); | | 540 | + const consumers = yield* Effect.forEach(streams, (snapshots) => | | 541 | + recorder | | 542 | + .consume( | | 543 | + Stream.fromIterable(snapshots).pipe( | | 544 | + Stream.tap(() => | | 545 | + Effect.sync(() => { | | 546 | + arrivals++; | | 547 | + }), | | 548 | + ), | | 549 | + ), | | 550 | + recorder.process, | | 551 | + Effect.succeed(false), | | 552 | + ) | | 553 | + .pipe(Effect.forkScoped({ startImmediately: true })), | | 554 | + ); | | 555 | + yield* TestClock.adjust(0); | | 556 | + // 256 queued, plus at most one read per stream waiting for room. | | 557 | + assert.isAtMost(arrivals, 256 + streams.length); | | 558 | + yield* Deferred.succeed(writer.release, undefined); | | 559 | + yield* Fiber.join(leading); | | 560 | + yield* Effect.forEach(consumers, Fiber.join, { discard: true }); | | 561 | + assert.lengthOf((yield* Queue.takeAll(recorder.output)).flat(), 1 + 5 * 64); | | 562 | + }), | | 563 | +); | | 564 | + | | 565 | +it.effect.each([false, true])( | | 566 | + "orders a backlog by focus, thread kind and a finishing control with starved=%s", | | 567 | + (starved) => | | 568 | + Effect.gen(function* () { | | 569 | + const writer = yield* blockFirstCommit; | | 570 | + const recorder = yield* recordBatches(writer.block, (threadId) => threadId === "focused"); | | 571 | + const leading = yield* recorder | | 572 | + .consume(Stream.make(event(-1)), recorder.process, Effect.succeed(false)) | | 573 | + .pipe(Effect.forkScoped({ startImmediately: true })); | | 574 | + yield* Deferred.await(writer.started); | | 575 | + const streams = [ | | 576 | + { name: "subagent", delegated: true, finishing: false }, | | 577 | + { name: "main", delegated: false, finishing: false }, | | 578 | + { name: "finishing", delegated: false, finishing: true }, | | 579 | + { name: "focused", delegated: false, finishing: false }, | | 580 | + ]; | | 581 | + const pulled = yield* Deferred.make<void>(); | | 582 | + let arrivals = 0; | | 583 | + const consumers = yield* Effect.forEach(streams, ({ name, delegated, finishing }) => | | 584 | + recorder | | 585 | + .consume( | | 586 | + // Open streams, so only the control marks a stream as finishing; EOF would too. | | 587 | + Stream.concat( | | 588 | + Stream.make(snapshot(name, name, name), ...(finishing ? [control] : [])), | | 589 | + Stream.never, | | 590 | + ).pipe( | | 591 | + Stream.tap(() => | | 592 | + ++arrivals === streams.length + 1 | | 593 | + ? Deferred.succeed(pulled, undefined) | | 594 | + : Effect.void, | | 595 | + ), | | 596 | + ), | | 597 | + recorder.process, | | 598 | + Effect.succeed(false), | | 599 | + Effect.succeed(true), | | 600 | + { threadId: name, delegated }, | | 601 | + ) | | 602 | + .pipe(Effect.forkScoped({ startImmediately: true })), | | 603 | + ); | | 604 | + yield* Deferred.await(pulled); | | 605 | + yield* TestClock.adjust(starved ? "30 seconds" : 0); | | 606 | + yield* Deferred.succeed(writer.release, undefined); | | 607 | + yield* Fiber.join(leading); | | 608 | + const outputs = yield* Queue.takeN(recorder.output, 3); | | 609 | + yield* Effect.forEach(consumers, Fiber.interrupt, { discard: true }); | | 610 | + // Waiting 30 seconds ranks a subagent like a main thread. | | 611 | + const order = starved | | 612 | + ? ["focused", "finishing", "subagent", "main"] | | 613 | + : ["focused", "finishing", "main", "subagent"]; | | 614 | + assert.deepEqual(Array.from(outputs), [ | | 615 | + [event(-1)], | | 616 | + order.map((name) => snapshot(name, name, name)), | | 617 | + [control], | | 618 | + ]); | | 619 | + }), | | 620 | +); | | 621 | + | 466 | 622 | it.effect.each(["failure", "stop"] as const)( | 467 | 623 | "flushes the prefix and respects a source %s barrier", | 468 | 624 | (mode) => | |
|