Skip to content

Commit 0031a8d

Browse files
committed
fix(server): automatic compactions no longer mark read threads unread
1 parent d1f7018 commit 0031a8d

2 files changed

Lines changed: 132 additions & 2 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { expect, it } from "@effect/vitest";
2+
import type {
3+
OrchestrationV2DomainEvent,
4+
OrchestrationV2Run,
5+
OrchestrationV2ServerCommand,
6+
OrchestrationV2ThreadProjection,
7+
} from "@t3tools/contracts";
8+
import * as DateTime from "effect/DateTime";
9+
import * as Effect from "effect/Effect";
10+
import * as Layer from "effect/Layer";
11+
import * as Queue from "effect/Queue";
12+
import * as Stream from "effect/Stream";
13+
14+
import * as ServerSettings from "../serverSettings.ts";
15+
import * as IdleCompactionService from "./IdleCompactionService.ts";
16+
import * as Orchestrator from "./Orchestrator.ts";
17+
import * as ProjectionStore from "./ProjectionStore.ts";
18+
19+
const at = (minute: number) => DateTime.makeUnsafe(Date.UTC(2026, 9, 19, 12, minute));
20+
21+
function compactedThread(threadId: string, lastVisitedAt: DateTime.Utc | null) {
22+
const run = (ordinal: number, userMessageId: string, completedAt: DateTime.Utc) =>
23+
({
24+
id: `${threadId}:run-${ordinal}`,
25+
threadId,
26+
ordinal,
27+
userMessageId,
28+
status: "completed",
29+
completedAt,
30+
}) as unknown as OrchestrationV2Run;
31+
const previous = run(1, "message", at(0));
32+
const compaction = run(2, `idle-compact:${threadId}:${previous.id}`, at(58));
33+
return {
34+
records: {
35+
thread: { id: threadId, lastVisitedAt },
36+
runs: [previous, compaction],
37+
} as unknown as OrchestrationV2ThreadProjection,
38+
completed: {
39+
type: "run.updated",
40+
threadId,
41+
payload: compaction,
42+
} as unknown as OrchestrationV2DomainEvent,
43+
};
44+
}
45+
46+
it.effect("marks a caught-up thread read when its automatic compaction completes", () =>
47+
Effect.gen(function* () {
48+
const threads = [
49+
compactedThread("unseen", null),
50+
compactedThread("behind", at(-1)),
51+
compactedThread("caught-up", at(1)),
52+
];
53+
const events = yield* Queue.unbounded<OrchestrationV2DomainEvent>();
54+
const commands = yield* Queue.unbounded<OrchestrationV2ServerCommand>();
55+
const service = yield* IdleCompactionService.make.pipe(
56+
Effect.provide(
57+
Layer.mergeAll(
58+
Layer.mock(Orchestrator.OrchestratorV2)({
59+
streamDomainEvents: Stream.fromQueue(events),
60+
dispatch: (command) =>
61+
Queue.offer(commands, command).pipe(Effect.as({ sequence: 1, storedEvents: [] })),
62+
}),
63+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
64+
getThreadRecords: (threadId) =>
65+
Effect.succeed(threads.find((t) => t.records.thread.id === threadId)!.records),
66+
}),
67+
ServerSettings.layerTest(),
68+
),
69+
),
70+
);
71+
yield* service.start();
72+
73+
yield* Queue.offerAll(
74+
events,
75+
threads.map((thread) => thread.completed),
76+
);
77+
78+
expect(yield* Queue.take(commands)).toMatchObject({
79+
type: "thread.visit",
80+
threadId: "caught-up",
81+
visitedAt: DateTime.formatIso(at(58)),
82+
opened: false,
83+
});
84+
}).pipe(Effect.scoped),
85+
);

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

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
CommandId,
33
IDLE_COMPACTION_TEXT,
44
MessageId,
5+
type OrchestrationV2Run,
56
resolveIdleCompaction,
67
resolveProjectProviderInstanceEnabled,
78
type RunId,
@@ -19,6 +20,8 @@ import { idleCompactionWindow } from "./IdleCompaction.ts";
1920
import * as Orchestrator from "./Orchestrator.ts";
2021
import * as ProjectionStore from "./ProjectionStore.ts";
2122

23+
const IDLE_COMPACTION_MESSAGE_PREFIX = "idle-compact:";
24+
2225
/**
2326
* Beta: when a run completes on a Claude instance set to compact before the
2427
* cache expires, waits until shortly before its prompt cache expires and sends
@@ -69,7 +72,7 @@ export const make = Effect.gen(function* () {
6972
type: "message.dispatch",
7073
// One compaction per completed run, even across duplicate events.
7174
commandId: CommandId.make(`server:idle-compact:${threadId}:${runId}`),
72-
messageId: MessageId.make(`idle-compact:${threadId}:${runId}`),
75+
messageId: MessageId.make(`${IDLE_COMPACTION_MESSAGE_PREFIX}${threadId}:${runId}`),
7376
threadId,
7477
text: IDLE_COMPACTION_TEXT,
7578
attachments: [],
@@ -87,13 +90,55 @@ export const make = Effect.gen(function* () {
8790
),
8891
);
8992

93+
const markSeenIfCaughtUp = Effect.fn("IdleCompactionService.markSeenIfCaughtUp")(
94+
function* (run: OrchestrationV2Run) {
95+
if (run.completedAt === null || !run.userMessageId.startsWith(IDLE_COMPACTION_MESSAGE_PREFIX))
96+
return;
97+
const { thread, runs } = yield* projections.getThreadRecords(run.threadId, ["runs"]);
98+
const previous = runs.reduce<OrchestrationV2Run | null>(
99+
(latest, candidate) =>
100+
candidate.ordinal < run.ordinal && (latest === null || candidate.ordinal > latest.ordinal)
101+
? candidate
102+
: latest,
103+
null,
104+
);
105+
if (
106+
thread.lastVisitedAt === null ||
107+
previous?.completedAt == null ||
108+
DateTime.isGreaterThan(previous.completedAt, thread.lastVisitedAt)
109+
)
110+
return;
111+
yield* orchestrator.dispatch({
112+
type: "thread.visit",
113+
commandId: CommandId.make(`server:idle-compact-visit:${run.threadId}:${run.id}`),
114+
threadId: run.threadId,
115+
visitedAt: DateTime.formatIso(run.completedAt),
116+
opened: false,
117+
});
118+
},
119+
(effect, run) =>
120+
effect.pipe(
121+
Effect.catch((cause) =>
122+
Effect.logWarning("idle compaction visit skipped", { threadId: run.threadId, cause }),
123+
),
124+
),
125+
);
126+
90127
const start = Effect.fn("IdleCompactionService.start")(function* () {
91128
// Keyed by thread: a newer completed run replaces the waiting timer.
92129
const timers = yield* FiberMap.make<ThreadId>();
93130
yield* forkParked(
94131
Stream.runForEach(orchestrator.streamDomainEvents, (event) =>
95132
event.type === "run.updated" && event.payload.status === "completed"
96-
? FiberMap.run(timers, event.threadId, compactWhenDue(event.threadId, event.payload.id))
133+
? markSeenIfCaughtUp(event.payload).pipe(
134+
Effect.andThen(
135+
FiberMap.run(
136+
timers,
137+
event.threadId,
138+
compactWhenDue(event.threadId, event.payload.id),
139+
),
140+
),
141+
)
97142
: Effect.void,
98143
),
99144
);

0 commit comments

Comments
 (0)