@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 109 lines 4.47 KB
Raw
1 import { isListedThreadLineage, type OrchestrationV2ThreadProjection } from "@t3tools/contracts";
2 import { COMPACTION_SUGGESTION_MIN_TOKENS } from "@t3tools/shared/claudeCompaction";
3 import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
4 import * as DateTime from "effect/DateTime";
5
6 // Claude Code's idleCompaction defaults to 200K and won't go below 100K. Below
7 // that a cold re-read is cheap, so compacting mostly loses context for little
8 // saving.
9 const MIN_CONTEXT_TOKENS = COMPACTION_SUGGESTION_MIN_TOKENS;
10 // Start this long before the cache expires: enough for the session to resume
11 // and send the compaction while the cache is still warm.
12 const LEAD_MS = 2 * 60_000;
13
14 export type IdleCompactionRecords = Pick<
15 OrchestrationV2ThreadProjection,
16 | "thread"
17 | "runs"
18 | "messages"
19 | "providerThreads"
20 | "providerTurns"
21 | "runtimeRequests"
22 | "turnItems"
23 | "contextTransfers"
24 >;
25
26 /**
27 * When an idle Claude thread should compact, or null when it should not. The
28 * window ends when the prompt cache of Claude's last request expires (1h on
29 * subscriptions, 5m on API keys); compacting after that pays a full uncached
30 * read.
31 */
32 export function idleCompactionWindow(
33 {
34 thread,
35 runs,
36 messages,
37 providerThreads,
38 providerTurns,
39 runtimeRequests,
40 turnItems,
41 contextTransfers,
42 }: IdleCompactionRecords,
43 nowMs: number,
44 ): {
45 readonly runId: (typeof runs)[number]["id"];
46 readonly startMs: number;
47 readonly endMs: number;
48 } | null {
49 if (thread.deletedAt !== null || thread.archivedAt !== null) return null;
50 if (thread.settledAt !== null || thread.settledOverride === "settled") return null;
51 if (thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs)
52 return null;
53 if (!isListedThreadLineage(thread.lineage)) return null;
54 // A removed or steered queued message leaves a cancelled run that never started.
55 const latestRun = runs
56 .filter((run) => run.status !== "cancelled" || run.startedAt !== null)
57 .reduce<(typeof runs)[number] | null>(
58 (latest, run) => (latest === null || run.ordinal > latest.ordinal ? run : latest),
59 null,
60 );
61 if (latestRun?.status !== "completed" || latestRun.completedAt === null) return null;
62 const message = messages.find((candidate) => candidate.id === latestRun.userMessageId);
63 // Never compact a compaction, so one idle spell compacts at most once.
64 if (message === undefined || message.text.trim().toLowerCase() === "/compact") return null;
65 if (
66 runtimeRequests.some((request) => request.status === "pending") ||
67 // A pending merge-back belongs to the user's next message.
68 contextTransfers.some(
69 (transfer) =>
70 transfer.type === "merge_back" &&
71 transfer.targetThreadId === thread.id &&
72 transfer.status === "pending",
73 ) ||
74 // Background work reports into the next turn; compaction would swallow it. A delegated
75 // child's result arrives later as its own message, so it does not count.
76 derivePendingBackgroundWork({
77 latestRun,
78 runs,
79 providerThreads,
80 turnItems: turnItems.filter(
81 (item) => item.type !== "subagent" || item.origin !== "app_owned",
82 ),
83 activeProviderThreadId: thread.activeProviderThreadId,
84 }).length > 0
85 )
86 return null;
87 const providerThread = providerThreads.find(
88 (candidate) => candidate.id === thread.activeProviderThreadId,
89 );
90 // The thread's current selection receives the /compact, so it must still be this Claude session.
91 if (
92 providerThread?.driver !== "claudeAgent" ||
93 providerThread.id !== latestRun.providerThreadId ||
94 providerThread.providerInstanceId !== thread.providerInstanceId ||
95 latestRun.providerInstanceId !== thread.providerInstanceId
96 )
97 return null;
98 const usages = providerTurns
99 .filter((turn) => turn.providerThreadId === providerThread.id && turn.tokenUsage !== undefined)
100 .map((turn) => turn.tokenUsage!)
101 .toSorted((left, right) => Date.parse(right.updatedAt) - Date.parse(left.updatedAt));
102 if ((usages[0]?.usedTokens ?? 0) < MIN_CONTEXT_TOKENS) return null;
103 // A request served fully from cache creates no cache entry and reports no
104 // TTL, but it still refreshes the cache. Treat it as unknown and skip.
105 const expiresAt = usages[0]?.promptCacheExpiresAt;
106 const endMs = expiresAt === undefined ? Number.NaN : Date.parse(expiresAt);
107 if (!Number.isFinite(endMs) || nowMs >= endMs) return null;
108 return { runId: latestRun.id, startMs: endMs - LEAD_MS, endMs };
109 }