Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import { type MilestoneEvent, MilestoneId, ProjectId, RunId, ThreadId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import * as Stream from "effect/Stream";
5import * as TestClock from "effect/testing/TestClock";
6
7import * as MilestoneHub from "./MilestoneHub.ts";
8
9const settled = (
10 id: string,
11 options: { readonly child?: boolean; readonly project?: string } = {},
12): MilestoneEvent => ({
13 id: MilestoneId.make(id),
14 type: "thread.run_settled",
15 occurredAt: "2026-11-20T00:00:00.000Z",
16 projectId: ProjectId.make(options.project ?? "project:a"),
17 threadId: ThreadId.make(`thread:${id}`),
18 parentThreadId: options.child === true ? ThreadId.make("thread:parent") : null,
19 actor: { type: "agent" },
20 isSubagentChild: options.child === true,
21 runId: RunId.make(`run:${id}`),
22 outcome: "completed",
23});
24const workflowEnded = (id: string, workflowRunId: string): MilestoneEvent => ({
25 id: MilestoneId.make(id),
26 type: "workflow.run_ended",
27 occurredAt: "2026-11-20T00:00:00.000Z",
28 projectId: null,
29 threadId: null,
30 parentThreadId: null,
31 actor: { type: "system" },
32 isSubagentChild: false,
33 workflowRunId,
34 workflow: "review",
35 runTitle: "Review",
36 parentWorkflowRunId: null,
37 outcome: "completed",
38 detail: null,
39});
40const ids = (events: Iterable<MilestoneEvent>) => [...events].map((event) => event.id);
41
42it.effect("fans out to every subscriber, muting subagent children unless asked for", () =>
43 Effect.gen(function* () {
44 const hub = yield* MilestoneHub.MilestoneHub;
45 const muted = yield* hub.subscribe();
46 const everything = yield* hub.subscribe({ includeSubagentChildren: true });
47 const projectB = yield* hub.subscribe({ projectIds: [ProjectId.make("project:b")] });
48 const runR1 = yield* hub.subscribe({ workflowRunIds: ["r1"] });
49
50 yield* hub.publish(settled("a"));
51 yield* hub.publish(settled("child", { child: true }));
52 // A repeat of an id is the same milestone.
53 yield* hub.publish(settled("a"));
54 yield* hub.publish(settled("b", { project: "project:b" }));
55 yield* hub.publish(workflowEnded("w0", "r0"));
56 yield* hub.publish(workflowEnded("w1", "r1"));
57
58 assert.deepStrictEqual(ids(yield* Stream.runCollect(Stream.take(muted, 4))), [
59 "a",
60 "b",
61 "w0",
62 "w1",
63 ]);
64 assert.deepStrictEqual(ids(yield* Stream.runCollect(Stream.take(everything, 3))), [
65 "a",
66 "child",
67 "b",
68 ]);
69 // A workflow milestone has no project, and a thread milestone no workflow run.
70 assert.deepStrictEqual(ids(yield* Stream.runCollect(Stream.take(projectB, 1))), ["b"]);
71 assert.deepStrictEqual(ids(yield* Stream.runCollect(Stream.take(runR1, 1))), ["w1"]);
72 assert.deepStrictEqual(ids((yield* hub.recent()).events), ["a", "b", "w0", "w1"]);
73 }).pipe(Effect.scoped, Effect.provide(MilestoneHub.layer)),
74);
75
76it.effect("pages forward from a kept id and says when the answer is cut short", () =>
77 Effect.gen(function* () {
78 const hub = yield* MilestoneHub.MilestoneHub;
79 for (let index = 0; index < 1_005; index++) yield* hub.publish(settled(`e${index}`));
80
81 const after = yield* hub.recent({ sinceId: MilestoneId.make("e1000"), limit: 3 });
82 assert.deepStrictEqual(ids(after.events), ["e1001", "e1002", "e1003"]);
83 assert.isTrue(after.truncated);
84 const rest = yield* hub.recent({ sinceId: MilestoneId.make("e1003") });
85 assert.deepStrictEqual([ids(rest.events), rest.truncated], [["e1004"], false]);
86
87 // The oldest fell out of the buffer, so a caller that last saw it missed some.
88 const gone = yield* hub.recent({ sinceId: MilestoneId.make("e2"), limit: 2 });
89 assert.deepStrictEqual([ids(gone.events), gone.truncated], [["e1003", "e1004"], true]);
90 const newest = yield* hub.recent({ limit: 1_000 });
91 assert.deepStrictEqual(
92 [newest.events.length, newest.events[0]?.id, newest.truncated],
93 [1_000, "e5", false],
94 );
95 }).pipe(Effect.provide(MilestoneHub.layer)),
96);
97
98it.effect("skips expired milestones even when they arrive after newer ones", () =>
99 Effect.gen(function* () {
100 const hub = yield* MilestoneHub.MilestoneHub;
101 yield* TestClock.adjust("10 minutes");
102 const { cursor } = yield* hub.read({ cursor: null, limit: 1 });
103 yield* hub.publish({ ...settled("fresh"), occurredAt: "1970-01-01T00:10:00.000Z" });
104 yield* hub.publish({ ...settled("expired"), occurredAt: "1970-01-01T00:08:59.000Z" });
105 yield* hub.publish({ ...settled("at-cutoff"), occurredAt: "1970-01-01T00:09:00.000Z" });
106
107 const first = yield* hub.read({ cursor, maxAgeMs: 60_000, limit: 1 });
108 assert.deepStrictEqual(ids(first.events.map(({ event }) => event)), ["fresh"]);
109 assert.isTrue(first.hasMore);
110 assert.isTrue(first.truncated);
111 const rest = yield* hub.read({ cursor: first.cursor, maxAgeMs: 60_000, limit: 1 });
112 assert.deepStrictEqual(ids(rest.events.map(({ event }) => event)), ["at-cutoff"]);
113 assert.isFalse(rest.hasMore);
114 }).pipe(Effect.provide(MilestoneHub.layer)),
115);
116
117it.effect("a follower's replay ends at the position it checked, past its last match", () =>
118 Effect.gen(function* () {
119 const hub = yield* MilestoneHub.MilestoneHub;
120 const { cursor } = yield* hub.read({ cursor: null, limit: 1 });
121 yield* hub.publish(settled("a"));
122 yield* hub.publish(settled("b", { project: "project:b" }));
123
124 const { items } = yield* hub.follow({
125 cursor,
126 filter: { projectIds: [ProjectId.make("project:a")] },
127 });
128 const [milestone, position] = yield* Stream.runCollect(Stream.take(items, 2));
129 assert.strictEqual(milestone?._tag === "Milestone" ? milestone.event.id : undefined, "a");
130 assert.strictEqual(position?._tag, "Position");
131 assert.strictEqual((yield* hub.read({ cursor: null, limit: 1 })).cursor, position?.cursor);
132 }).pipe(Effect.scoped, Effect.provide(MilestoneHub.layer)),
133);
134
135it.effect("tells a follower that fell behind the subscriber backlog where its gap is", () =>
136 Effect.gen(function* () {
137 const hub = yield* MilestoneHub.MilestoneHub;
138 const { items } = yield* hub.follow({ cursor: null });
139 // Published before the follower reads, so the backlog drops the oldest.
140 for (let index = 0; index < 300; index++) yield* hub.publish(settled(`e${index}`));
141
142 const [gap, first] = yield* Stream.runCollect(Stream.take(items, 2));
143 assert.strictEqual(gap?._tag, "Gap");
144 assert.strictEqual(first?._tag === "Milestone" ? first.event.id : undefined, "e44");
145 }).pipe(Effect.scoped, Effect.provide(MilestoneHub.layer)),
146);