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
6 changes: 6 additions & 0 deletions apps/server/src/orchestration-v2/EventSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ThreadId,
} from "@t3tools/contracts";
import * as ProviderCommitBatch from "./ProviderCommitBatch.ts";
import * as ThreadViewers from "./ThreadViewers.ts";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
Expand Down Expand Up @@ -96,6 +97,7 @@ export interface EventSinkV2Shape {
process: (event: A) => Effect.Effect<void, E2, R2>,
after: Effect.Effect<boolean, E2, R2>,
canBatch: Effect.Effect<boolean, never, R2>,
priority?: ProviderCommitBatch.StreamPriority,
) => Effect.Effect<
void,
E | E2 | ProviderCommitBatch.CommitBatchError | Schema.SchemaError,
Expand Down Expand Up @@ -277,10 +279,12 @@ const layerBase: Layer.Layer<
| ProjectStore.ProjectStoreV2
| SqlClient.SqlClient
| TurnItemPositionStore.TurnItemPositionStoreV2
| ThreadViewers.ThreadViewers
> = Layer.effect(
EventSinkV2,
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
const threadViewers = yield* ThreadViewers.ThreadViewers;
const commandReceipts = yield* CommandReceiptStore.CommandReceiptStoreV2;
const effectOutbox = yield* EffectOutbox.EffectOutboxV2;
const eventStore = yield* EventStore.EventStoreV2;
Expand Down Expand Up @@ -1174,6 +1178,7 @@ const layerBase: Layer.Layer<
),
);
}),
threadViewers.isViewed,
);

return EventSinkV2.of({
Expand Down Expand Up @@ -1327,6 +1332,7 @@ export const layer: Layer.Layer<
EffectOutbox.layer,
ProjectStore.layer,
TurnItemPositionStore.layer,
ThreadViewers.layer,
),
),
);
11 changes: 10 additions & 1 deletion apps/server/src/orchestration-v2/FoundationPersistence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import * as CommandReceiptStore from "./CommandReceiptStore.ts";
import * as EffectOutbox from "./EffectOutbox.ts";
import * as EffectWorker from "./EffectWorker.ts";
import * as EventSink from "./EventSink.ts";
import * as ThreadViewers from "./ThreadViewers.ts";
import * as EventStore from "./EventStore.ts";
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
import * as ProjectionMaintenance from "./ProjectionMaintenance.ts";
Expand Down Expand Up @@ -491,7 +492,13 @@ it.effect("commits other threads' writes ahead of a thread's queued provider sna
.pipe(Effect.forkScoped({ startImmediately: true }));
yield* Queue.offer(input, snapshot);
yield* Deferred.await(started);
yield* Queue.offerAll(input, [snapshot, snapshot]);
yield* Queue.offerAll(
input,
[2, 3].map((n) => ({
...snapshot,
node: { ...busyNode.payload, id: NodeId.make(`node:busy:${n}`) },
})),
);
yield* Deferred.await(pulled);
yield* TestClock.adjust(0);
const writeTo = (event: OrchestrationV2DomainEvent, id: string) =>
Expand Down Expand Up @@ -4310,6 +4317,7 @@ it.effect.each(["direct", "provider-batch", "cancel-provider-batch"] as const)(
layerCommandReceiptStoreProvided,
ProjectStore.layer.pipe(Layer.provide(layerDatabase)),
TurnItemPositionStore.layer.pipe(Layer.provide(layerDatabase)),
ThreadViewers.layer,
),
),
);
Expand Down Expand Up @@ -4608,6 +4616,7 @@ it.effect.each(["cancel transaction", "fail transaction", "cancel publication"]
layerCommandReceiptStoreProvided,
ProjectStore.layer.pipe(Layer.provide(layerDatabase)),
TurnItemPositionStore.layer.pipe(Layer.provide(layerDatabase)),
ThreadViewers.layer,
),
),
);
Expand Down
158 changes: 157 additions & 1 deletion apps/server/src/orchestration-v2/ProviderCommitBatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function event(index: number, text = `delta:${index}`): ProviderAdapter.Provider
type: "message.updated",
driver,
message: {
id: MessageId.make("single-message"),
id: MessageId.make(`message:${index}`),
threadId,
runId: null,
nodeId: null,
Expand All @@ -36,6 +36,15 @@ function event(index: number, text = `delta:${index}`): ProviderAdapter.Provider
},
};
}
function snapshot(id: string, text: string, threadId = "single-thread") {
const base = event(0, text);
return base.type === "message.updated"
? {
...base,
message: { ...base.message, id: MessageId.make(id), threadId: ThreadId.make(threadId) },
}
: base;
}
const control: ProviderAdapter.ProviderAdapterV2Event = {
type: "background_work.updated",
driver,
Expand All @@ -45,6 +54,7 @@ const control: ProviderAdapter.ProviderAdapterV2Event = {

const recordBatches = Effect.fn("test.recordProviderBatches")(function* (
beforeCommit: Effect.Effect<void> = Effect.void,
isViewed?: (threadId: string) => boolean,
) {
const output = yield* Queue.unbounded<ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>>();
let current: ProviderAdapter.ProviderAdapterV2Event[] = [];
Expand All @@ -57,6 +67,8 @@ const recordBatches = Effect.fn("test.recordProviderBatches")(function* (
if (current.length > 0) yield* Queue.offer(output, current);
}),
true,
undefined,
isViewed,
);
const process = (raw: ProviderAdapter.ProviderAdapterV2Event) =>
raw.type === "background_work.updated"
Expand Down Expand Up @@ -463,6 +475,150 @@ it.effect.each(["none", "writer", "snapshots"] as const)(
}),
);

const blockFirstCommit = Effect.gen(function* () {
const started = yield* Deferred.make<void>();
const release = yield* Deferred.make<void>();
let first = true;
const block = Effect.gen(function* () {
if (!first) return;
first = false;
yield* Deferred.succeed(started, undefined);
yield* Deferred.await(release);
});
return { started, release, block };
});

it.effect.each([
{ keys: ["a", "b", "a", "b", "a", "b"], kept: [0, 4, 5] },
// A first appearance between two snapshots of an entity keeps the earlier snapshot.
{ keys: ["a", "b", "a", "b", "c", "a", "b"], kept: [0, 2, 3, 4, 5, 6] },
])("combines superseded snapshots queued behind a busy commit: $keys", ({ keys, kept }) =>
Effect.gen(function* () {
const writer = yield* blockFirstCommit;
const recorder = yield* recordBatches(writer.block);
const leading = yield* recorder
.consume(Stream.make(event(-1)), recorder.process, Effect.succeed(false))
.pipe(Effect.forkScoped({ startImmediately: true }));
yield* Deferred.await(writer.started);
const snapshots = keys.map((key, index) => snapshot(key, `${key}:${index}`));
const pulled = yield* Deferred.make<void>();
const consumer = yield* recorder
.consume(
Stream.fromIterable(snapshots).pipe(
Stream.tap((raw) =>
raw === snapshots.at(-1) ? Deferred.succeed(pulled, undefined) : Effect.void,
),
),
recorder.process,
Effect.succeed(false),
)
.pipe(Effect.forkScoped({ startImmediately: true }));
yield* Deferred.await(pulled);
yield* TestClock.adjust(0);
yield* Deferred.succeed(writer.release, undefined);
yield* Fiber.join(leading);
yield* Fiber.join(consumer);
assert.deepEqual(yield* Queue.takeAll(recorder.output), [
[event(-1)],
kept.map((index) => snapshots[index]!),
]);
}),
);

it.effect("makes streams wait to read more once the shared backlog is full", () =>
Effect.gen(function* () {
const writer = yield* blockFirstCommit;
const recorder = yield* recordBatches(writer.block);
const leading = yield* recorder
.consume(Stream.make(event(-1)), recorder.process, Effect.succeed(false))
.pipe(Effect.forkScoped({ startImmediately: true }));
yield* Deferred.await(writer.started);
let arrivals = 0;
const streams = Array.from({ length: 5 }, (_, stream) =>
Array.from({ length: 64 }, (_, n) => snapshot(`${stream}:${n}`, `${stream}:${n}`)),
);
const consumers = yield* Effect.forEach(streams, (snapshots) =>
recorder
.consume(
Stream.fromIterable(snapshots).pipe(
Stream.tap(() =>
Effect.sync(() => {
arrivals++;
}),
),
),
recorder.process,
Effect.succeed(false),
)
.pipe(Effect.forkScoped({ startImmediately: true })),
);
yield* TestClock.adjust(0);
// 256 queued, plus at most one read per stream waiting for room.
assert.isAtMost(arrivals, 256 + streams.length);
yield* Deferred.succeed(writer.release, undefined);
yield* Fiber.join(leading);
yield* Effect.forEach(consumers, Fiber.join, { discard: true });
assert.lengthOf((yield* Queue.takeAll(recorder.output)).flat(), 1 + 5 * 64);
}),
);

it.effect.each([false, true])(
"orders a backlog by focus, thread kind and a finishing control with starved=%s",
(starved) =>
Effect.gen(function* () {
const writer = yield* blockFirstCommit;
const recorder = yield* recordBatches(writer.block, (threadId) => threadId === "focused");
const leading = yield* recorder
.consume(Stream.make(event(-1)), recorder.process, Effect.succeed(false))
.pipe(Effect.forkScoped({ startImmediately: true }));
yield* Deferred.await(writer.started);
const streams = [
{ name: "subagent", delegated: true, finishing: false },
{ name: "main", delegated: false, finishing: false },
{ name: "finishing", delegated: false, finishing: true },
{ name: "focused", delegated: false, finishing: false },
];
const pulled = yield* Deferred.make<void>();
let arrivals = 0;
const consumers = yield* Effect.forEach(streams, ({ name, delegated, finishing }) =>
recorder
.consume(
// Open streams, so only the control marks a stream as finishing; EOF would too.
Stream.concat(
Stream.make(snapshot(name, name, name), ...(finishing ? [control] : [])),
Stream.never,
).pipe(
Stream.tap(() =>
++arrivals === streams.length + 1
? Deferred.succeed(pulled, undefined)
: Effect.void,
),
),
recorder.process,
Effect.succeed(false),
Effect.succeed(true),
{ threadId: name, delegated },
)
.pipe(Effect.forkScoped({ startImmediately: true })),
);
yield* Deferred.await(pulled);
yield* TestClock.adjust(starved ? "30 seconds" : 0);
yield* Deferred.succeed(writer.release, undefined);
yield* Fiber.join(leading);
const outputs = yield* Queue.takeN(recorder.output, 3);
yield* Effect.forEach(consumers, Fiber.interrupt, { discard: true });
// Waiting 30 seconds ranks a subagent like a main thread.
const order = starved
? ["focused", "finishing", "subagent", "main"]
: ["focused", "finishing", "main", "subagent"];
assert.deepEqual(Array.from(outputs), [
[event(-1)],
order.map((name) => snapshot(name, name, name)),
[control],
]);
}),
);

it.effect.each(["failure", "stop"] as const)(
"flushes the prefix and respects a source %s barrier",
(mode) =>
Expand Down
Loading
Loading