Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

IdleCompaction.test.ts

156 lines4.8 KB

1import { describe, expect, it } from "@effect/vitest";
2import {
3 MessageId,
4 NodeId,
5 ProjectId,
6 ProviderDriverKind,
7 ProviderInstanceId,
8 ProviderThreadId,
9 ProviderTurnId,
10 RunId,
11 ThreadId,
12} from "@t3tools/contracts";
13import * as DateTime from "effect/DateTime";
14​
15import { idleCompactionWindow, type IdleCompactionRecords } from "./IdleCompaction.ts";
16​
17const NOW_MS = Date.parse("2026-10-19T12:00:00.000Z");
18const MINUTE_MS = 60_000;
19const at = (offset = 0) => DateTime.makeUnsafe(NOW_MS + offset);
20const threadId = ThreadId.make("idle-thread");
21const runId = RunId.make("run-1");
22const instanceId = ProviderInstanceId.make("claudeAgent");
23const providerThreadId = ProviderThreadId.make("provider-thread");
24const modelSelection = { instanceId, model: "claude-opus-5-5" };
25​
26function records(
27 usage: { readonly usedTokens?: number; readonly cacheExpiresInMs?: number } = {},
28): { -readonly [Key in keyof IdleCompactionRecords]: IdleCompactionRecords[Key] } {
29 const messageId = MessageId.make("message-1");
30 const nodeId = NodeId.make("root-node");
31 return {
32 thread: {
33 id: threadId,
34 projectId: ProjectId.make("project"),
35 title: "Idle thread",
36 createdBy: "user",
37 creationSource: "web",
38 providerInstanceId: instanceId,
39 modelSelection,
40 runtimeMode: "full-access",
41 interactionMode: "default",
42 branch: null,
43 worktreePath: null,
44 activeProviderThreadId: providerThreadId,
45 lineage: { rootThreadId: threadId, parentThreadId: null, relationshipToParent: null },
46 forkedFrom: null,
47 createdAt: at(-MINUTE_MS),
48 updatedAt: at(),
49 archivedAt: null,
50 settledOverride: null,
51 settledAt: null,
52 lastVisitedAt: at(),
53 deletedAt: null,
54 },
55 runs: [
56 {
57 id: runId,
58 threadId,
59 ordinal: 1,
60 providerInstanceId: instanceId,
61 modelSelection,
62 providerThreadId,
63 userMessageId: messageId,
64 rootNodeId: nodeId,
65 activeAttemptId: null,
66 status: "completed",
67 requestedAt: at(-MINUTE_MS),
68 startedAt: at(-MINUTE_MS),
69 completedAt: at(),
70 checkpointId: null,
71 contextHandoffId: null,
72 },
73 ],
74 messages: [
75 {
76 id: messageId,
77 threadId,
78 runId,
79 nodeId,
80 role: "user",
81 text: "Explain this code",
82 attachments: [],
83 streaming: false,
84 createdBy: "user",
85 creationSource: "web",
86 createdAt: at(-MINUTE_MS),
87 updatedAt: at(-MINUTE_MS),
88 },
89 ],
90 providerThreads: [
91 {
92 id: providerThreadId,
93 driver: ProviderDriverKind.make("claudeAgent"),
94 providerInstanceId: instanceId,
95 providerSessionId: null,
96 appThreadId: threadId,
97 ownerNodeId: nodeId,
98 nativeThreadRef: null,
99 nativeConversationHeadRef: null,
100 status: "idle",
101 firstRunOrdinal: 1,
102 lastRunOrdinal: 1,
103 handoffIds: [],
104 forkedFrom: null,
105 pendingBackgroundTasks: [],
106 createdAt: at(-MINUTE_MS),
107 updatedAt: at(),
108 },
109 ],
110 providerTurns: [
111 {
112 id: ProviderTurnId.make("provider-turn-1"),
113 providerThreadId,
114 nodeId,
115 runAttemptId: null,
116 nativeTurnRef: null,
117 ordinal: 1,
118 status: "completed",
119 startedAt: at(-MINUTE_MS),
120 completedAt: at(),
121 tokenUsage: {
122 usedTokens: usage.usedTokens ?? 250_000,
123 ...(usage.cacheExpiresInMs === undefined
124 ? {}
125 : { promptCacheExpiresAt: DateTime.formatIso(at(usage.cacheExpiresInMs)) }),
126 updatedAt: DateTime.formatIso(at()),
127 },
128 },
129 ],
130 runtimeRequests: [],
131 turnItems: [],
132 contextTransfers: [],
133 };
135​
136describe("idleCompactionWindow", () => {
137 it("skips small contexts, unknown expiry, other providers, switched threads, and compactions", () => {
138 const cache = { cacheExpiresInMs: 60 * MINUTE_MS };
139 expect(idleCompactionWindow(records({ ...cache, usedTokens: 199_999 }), NOW_MS)).toBeNull();
140 expect(idleCompactionWindow(records(), NOW_MS)).toBeNull();
141 const codex = records(cache);
142 codex.providerThreads = [
143 { ...codex.providerThreads[0]!, driver: ProviderDriverKind.make("codex") },
144 ];
145 expect(idleCompactionWindow(codex, NOW_MS)).toBeNull();
146 const switched = records(cache);
147 switched.thread = { ...switched.thread, providerInstanceId: ProviderInstanceId.make("codex") };
148 expect(idleCompactionWindow(switched, NOW_MS)).toBeNull();
149 const compacted = records(cache);
150 compacted.messages = [{ ...compacted.messages[0]!, text: "/compact" }];
151 expect(idleCompactionWindow(compacted, NOW_MS)).toBeNull();
152 const archived = records(cache);
153 archived.thread = { ...archived.thread, archivedAt: at() };
154 expect(idleCompactionWindow(archived, NOW_MS)).toBeNull();
155 });
156});