GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — perf(server): rank queued provider snapshots by focus and combine sup… · orbitingflea/t3code@db51b67

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src
    • orchestration-v2
      • EventSink.ts
      • FoundationPersistence.test.ts
      • ProviderCommitBatch.test.ts
      • ProviderCommitBatch.ts
      • RunExecutionService.ts
      • ThreadViewers.ts
      • runtimeLayer.ts
      • testkit
        • ProviderReplayHarness.ts
    • server.ts
    • ws.test.ts
    • ws.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/orchestration-v2/EventSink.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/EventSink.ts
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ThreadId,
1616
} from "@t3tools/contracts";
1717
import * as ProviderCommitBatch from "./ProviderCommitBatch.ts";
18+
import * as ThreadViewers from "./ThreadViewers.ts";
1819
import * as Context from "effect/Context";
1920
import * as DateTime from "effect/DateTime";
2021
import * as Effect from "effect/Effect";
@@ -96,6 +97,7 @@ export interface EventSinkV2Shape {
9697
process: (event: A) => Effect.Effect<void, E2, R2>,
9798
after: Effect.Effect<boolean, E2, R2>,
9899
canBatch: Effect.Effect<boolean, never, R2>,
100+
priority?: ProviderCommitBatch.StreamPriority,
99101
) => Effect.Effect<
100102
void,
101103
E | E2 | ProviderCommitBatch.CommitBatchError | Schema.SchemaError,
@@ -277,10 +279,12 @@ const layerBase: Layer.Layer<
277279
| ProjectStore.ProjectStoreV2
278280
| SqlClient.SqlClient
279281
| TurnItemPositionStore.TurnItemPositionStoreV2
282+
| ThreadViewers.ThreadViewers
280283
> = Layer.effect(
281284
EventSinkV2,
282285
Effect.gen(function* () {
283286
const sql = yield* SqlClient.SqlClient;
287+
const threadViewers = yield* ThreadViewers.ThreadViewers;
284288
const commandReceipts = yield* CommandReceiptStore.CommandReceiptStoreV2;
285289
const effectOutbox = yield* EffectOutbox.EffectOutboxV2;
286290
const eventStore = yield* EventStore.EventStoreV2;
@@ -1174,6 +1178,7 @@ const layerBase: Layer.Layer<
11741178
),
11751179
);
11761180
}),
1181+
threadViewers.isViewed,
11771182
);
11781183

11791184
return EventSinkV2.of({
@@ -1327,6 +1332,7 @@ export const layer: Layer.Layer<
13271332
EffectOutbox.layer,
13281333
ProjectStore.layer,
13291334
TurnItemPositionStore.layer,
1335+
ThreadViewers.layer,
13301336
),
13311337
),
13321338
);
Collapse file

‎apps/server/src/orchestration-v2/FoundationPersistence.test.ts‎

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 numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import * as CommandReceiptStore from "./CommandReceiptStore.ts";
6464
import * as EffectOutbox from "./EffectOutbox.ts";
6565
import * as EffectWorker from "./EffectWorker.ts";
6666
import * as EventSink from "./EventSink.ts";
67+
import * as ThreadViewers from "./ThreadViewers.ts";
6768
import * as EventStore from "./EventStore.ts";
6869
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
6970
import * as ProjectionMaintenance from "./ProjectionMaintenance.ts";
@@ -491,7 +492,13 @@ it.effect("commits other threads' writes ahead of a thread's queued provider sna
491492
.pipe(Effect.forkScoped({ startImmediately: true }));
492493
yield* Queue.offer(input, snapshot);
493494
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+
);
495502
yield* Deferred.await(pulled);
496503
yield* TestClock.adjust(0);
497504
const writeTo = (event: OrchestrationV2DomainEvent, id: string) =>
@@ -4310,6 +4317,7 @@ it.effect.each(["direct", "provider-batch", "cancel-provider-batch"] as const)(
43104317
layerCommandReceiptStoreProvided,
43114318
ProjectStore.layer.pipe(Layer.provide(layerDatabase)),
43124319
TurnItemPositionStore.layer.pipe(Layer.provide(layerDatabase)),
4320+
ThreadViewers.layer,
43134321
),
43144322
),
43154323
);
@@ -4608,6 +4616,7 @@ it.effect.each(["cancel transaction", "fail transaction", "cancel publication"]
46084616
layerCommandReceiptStoreProvided,
46094617
ProjectStore.layer.pipe(Layer.provide(layerDatabase)),
46104618
TurnItemPositionStore.layer.pipe(Layer.provide(layerDatabase)),
4619+
ThreadViewers.layer,
46114620
),
46124621
),
46134622
);
Collapse file

‎apps/server/src/orchestration-v2/ProviderCommitBatch.test.ts‎

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 numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function event(index: number, text = `delta:${index}`): ProviderAdapter.Provider
2121
type: "message.updated",
2222
driver,
2323
message: {
24-
id: MessageId.make("single-message"),
24+
id: MessageId.make(`message:${index}`),
2525
threadId,
2626
runId: null,
2727
nodeId: null,
@@ -36,6 +36,15 @@ function event(index: number, text = `delta:${index}`): ProviderAdapter.Provider
3636
},
3737
};
3838
}
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+
}
3948
const control: ProviderAdapter.ProviderAdapterV2Event = {
4049
type: "background_work.updated",
4150
driver,
@@ -45,6 +54,7 @@ const control: ProviderAdapter.ProviderAdapterV2Event = {
4554

4655
const recordBatches = Effect.fn("test.recordProviderBatches")(function* (
4756
beforeCommit: Effect.Effect<void> = Effect.void,
57+
isViewed?: (threadId: string) => boolean,
4858
) {
4959
const output = yield* Queue.unbounded<ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>>();
5060
let current: ProviderAdapter.ProviderAdapterV2Event[] = [];
@@ -57,6 +67,8 @@ const recordBatches = Effect.fn("test.recordProviderBatches")(function* (
5767
if (current.length > 0) yield* Queue.offer(output, current);
5868
}),
5969
true,
70+
undefined,
71+
isViewed,
6072
);
6173
const process = (raw: ProviderAdapter.ProviderAdapterV2Event) =>
6274
raw.type === "background_work.updated"
@@ -463,6 +475,150 @@ it.effect.each(["none", "writer", "snapshots"] as const)(
463475
}),
464476
);
465477

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+
466622
it.effect.each(["failure", "stop"] as const)(
467623
"flushes the prefix and respects a source %s barrier",
468624
(mode) =>

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.