claude-code-infinite 1.0.9 → 1.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/README.md +230 -37
  2. package/dist/cc-request.d.ts +94 -0
  3. package/dist/cc-request.d.ts.map +1 -0
  4. package/dist/cc-request.js +194 -0
  5. package/dist/cc-request.js.map +1 -0
  6. package/dist/claude-env.d.ts +30 -0
  7. package/dist/claude-env.d.ts.map +1 -0
  8. package/dist/claude-env.js +55 -0
  9. package/dist/claude-env.js.map +1 -0
  10. package/dist/cli-args.d.ts +32 -0
  11. package/dist/cli-args.d.ts.map +1 -0
  12. package/dist/cli-args.js +72 -0
  13. package/dist/cli-args.js.map +1 -0
  14. package/dist/cli-lifecycle.d.ts +17 -0
  15. package/dist/cli-lifecycle.d.ts.map +1 -0
  16. package/dist/cli-lifecycle.js +30 -0
  17. package/dist/cli-lifecycle.js.map +1 -0
  18. package/dist/cli-main.d.ts +15 -0
  19. package/dist/cli-main.d.ts.map +1 -0
  20. package/dist/cli-main.js +464 -0
  21. package/dist/cli-main.js.map +1 -0
  22. package/dist/cli.js +13 -177
  23. package/dist/cli.js.map +1 -1
  24. package/dist/config.d.ts +3 -0
  25. package/dist/config.d.ts.map +1 -1
  26. package/dist/config.js +29 -3
  27. package/dist/config.js.map +1 -1
  28. package/dist/count-tokens.d.ts +78 -0
  29. package/dist/count-tokens.d.ts.map +1 -0
  30. package/dist/count-tokens.js +277 -0
  31. package/dist/count-tokens.js.map +1 -0
  32. package/dist/final-index.d.ts +10 -0
  33. package/dist/final-index.d.ts.map +1 -0
  34. package/dist/final-index.js +37 -0
  35. package/dist/final-index.js.map +1 -0
  36. package/dist/hooks.d.ts +239 -0
  37. package/dist/hooks.d.ts.map +1 -0
  38. package/dist/hooks.js +618 -0
  39. package/dist/hooks.js.map +1 -0
  40. package/dist/index.d.ts +9 -0
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +8 -0
  43. package/dist/index.js.map +1 -1
  44. package/dist/memtree-fetch.d.ts +24 -0
  45. package/dist/memtree-fetch.d.ts.map +1 -0
  46. package/dist/memtree-fetch.js +121 -0
  47. package/dist/memtree-fetch.js.map +1 -0
  48. package/dist/memtree-finder.d.ts +99 -0
  49. package/dist/memtree-finder.d.ts.map +1 -0
  50. package/dist/memtree-finder.js +235 -0
  51. package/dist/memtree-finder.js.map +1 -0
  52. package/dist/memtree-links.d.ts +16 -0
  53. package/dist/memtree-links.d.ts.map +1 -0
  54. package/dist/memtree-links.js +66 -0
  55. package/dist/memtree-links.js.map +1 -0
  56. package/dist/memtree-mcp-config.d.ts +28 -0
  57. package/dist/memtree-mcp-config.d.ts.map +1 -0
  58. package/dist/memtree-mcp-config.js +121 -0
  59. package/dist/memtree-mcp-config.js.map +1 -0
  60. package/dist/memtree-mcp.d.ts +265 -0
  61. package/dist/memtree-mcp.d.ts.map +1 -0
  62. package/dist/memtree-mcp.js +667 -0
  63. package/dist/memtree-mcp.js.map +1 -0
  64. package/dist/memtree-newest.d.ts +74 -0
  65. package/dist/memtree-newest.d.ts.map +1 -0
  66. package/dist/memtree-newest.js +217 -0
  67. package/dist/memtree-newest.js.map +1 -0
  68. package/dist/memtree-tools.d.ts +105 -0
  69. package/dist/memtree-tools.d.ts.map +1 -0
  70. package/dist/memtree-tools.js +408 -0
  71. package/dist/memtree-tools.js.map +1 -0
  72. package/dist/memtree.d.ts +398 -0
  73. package/dist/memtree.d.ts.map +1 -0
  74. package/dist/memtree.js +1231 -0
  75. package/dist/memtree.js.map +1 -0
  76. package/dist/notices.d.ts +177 -0
  77. package/dist/notices.d.ts.map +1 -0
  78. package/dist/notices.js +469 -0
  79. package/dist/notices.js.map +1 -0
  80. package/dist/payment-gate.d.ts +53 -0
  81. package/dist/payment-gate.d.ts.map +1 -0
  82. package/dist/payment-gate.js +97 -0
  83. package/dist/payment-gate.js.map +1 -0
  84. package/dist/project-meta.d.ts +15 -0
  85. package/dist/project-meta.d.ts.map +1 -0
  86. package/dist/project-meta.js +80 -0
  87. package/dist/project-meta.js.map +1 -0
  88. package/dist/prompt-accounting.d.ts +12 -0
  89. package/dist/prompt-accounting.d.ts.map +1 -0
  90. package/dist/prompt-accounting.js +70 -0
  91. package/dist/prompt-accounting.js.map +1 -0
  92. package/dist/proxy.d.ts +197 -0
  93. package/dist/proxy.d.ts.map +1 -0
  94. package/dist/proxy.js +4247 -0
  95. package/dist/proxy.js.map +1 -0
  96. package/dist/reqlog.d.ts +419 -0
  97. package/dist/reqlog.d.ts.map +1 -0
  98. package/dist/reqlog.js +146 -0
  99. package/dist/reqlog.js.map +1 -0
  100. package/dist/request-sizing.d.ts +52 -0
  101. package/dist/request-sizing.d.ts.map +1 -0
  102. package/dist/request-sizing.js +92 -0
  103. package/dist/request-sizing.js.map +1 -0
  104. package/dist/resume-repair-file.d.ts +17 -0
  105. package/dist/resume-repair-file.d.ts.map +1 -0
  106. package/dist/resume-repair-file.js +100 -0
  107. package/dist/resume-repair-file.js.map +1 -0
  108. package/dist/resume-repair.d.ts +27 -0
  109. package/dist/resume-repair.d.ts.map +1 -0
  110. package/dist/resume-repair.js +198 -0
  111. package/dist/resume-repair.js.map +1 -0
  112. package/dist/route-cache.d.ts +5 -0
  113. package/dist/route-cache.d.ts.map +1 -0
  114. package/dist/route-cache.js +63 -0
  115. package/dist/route-cache.js.map +1 -0
  116. package/dist/route-fallback.d.ts +29 -0
  117. package/dist/route-fallback.d.ts.map +1 -0
  118. package/dist/route-fallback.js +49 -0
  119. package/dist/route-fallback.js.map +1 -0
  120. package/dist/route-identity.d.ts +6 -0
  121. package/dist/route-identity.d.ts.map +1 -0
  122. package/dist/route-identity.js +100 -0
  123. package/dist/route-identity.js.map +1 -0
  124. package/dist/transcript-usage.d.ts +57 -0
  125. package/dist/transcript-usage.d.ts.map +1 -0
  126. package/dist/transcript-usage.js +570 -0
  127. package/dist/transcript-usage.js.map +1 -0
  128. package/dist/turns.d.ts +73 -0
  129. package/dist/turns.d.ts.map +1 -0
  130. package/dist/turns.js +298 -0
  131. package/dist/turns.js.map +1 -0
  132. package/dist/update-check.d.ts +38 -0
  133. package/dist/update-check.d.ts.map +1 -0
  134. package/dist/update-check.js +67 -0
  135. package/dist/update-check.js.map +1 -0
  136. package/package.json +6 -2
@@ -0,0 +1,398 @@
1
+ /**
2
+ * Client for the polychat.co MemTree API (/v1/context_memory).
3
+ *
4
+ * Two modes, mirroring the server's lazy-compression path (cc_api.py):
5
+ * - Tool turns: fire-and-forget indexing POST, off the response path.
6
+ * - User turns: blocking compression call with a hard timeout; the caller
7
+ * degrades to transparent pass-through on any failure (including 402 —
8
+ * compression is the paid feature, the user's own Anthropic call is never
9
+ * gated on it). A 402 from either mode additionally records
10
+ * payment-required state (paymentRequiredDetail) so the proxy can tell the
11
+ * user WHY MemTree is off instead of implying a transient outage; any later
12
+ * successful call clears it (the user paid mid-session).
13
+ *
14
+ * Compression work is deduped per complete request key, while background
15
+ * indexing is deduped per messages hash. Claude Code retries therefore cannot
16
+ * amplify identical HTTP calls, and budget-changing inputs never reuse a
17
+ * stale compression result.
18
+ */
19
+ import { type FinalReplyReader } from "./final-index.js";
20
+ import type { MessageTimes, MessageUsage } from "./transcript-usage.js";
21
+ import type { RequestLogSink } from "./reqlog.js";
22
+ import { type Message } from "./turns.js";
23
+ export declare const CLIENT_NAME = "cc-infinite";
24
+ export declare const CLIENT_VERSION: string;
25
+ /**
26
+ * A main-lane Stop followed by this long without a new request is taken as the
27
+ * session's end, and its final index goes out (exit sends it at once). Shorter
28
+ * pauses spend the server's once-per-30-minutes final index mid-session.
29
+ */
30
+ export declare const FINAL_INDEX_IDLE_MS: number;
31
+ /** Sessions finalized at exit at most (newest first), inside the 2 s drain. */
32
+ export declare const FINAL_INDEX_EXIT_SESSIONS = 3;
33
+ export interface MemtreeOptions {
34
+ baseUrl: string;
35
+ apiKey: string;
36
+ compressTimeoutMs?: number;
37
+ debug?: boolean;
38
+ /** Always-on JSONL diagnostics; every MemTree call logs one line. */
39
+ reqlog?: RequestLogSink;
40
+ /**
41
+ * `x-memtree-tools` on compress calls: the memtree MCP tools this Claude
42
+ * Code session has (e.g. "search,read_node,read_lines"), so the server can
43
+ * tell the model how to use them in the memory it returns. Set only when
44
+ * the `memtree` MCP server is configured for the session (memtree-mcp-config.ts).
45
+ */
46
+ memtreeTools?: string;
47
+ }
48
+ /**
49
+ * Request metadata forwarded to the server so it can resolve a model-based
50
+ * memory budget (e.g. the 800k whole-request target for Fable / Opus 5).
51
+ * Without `model` the server can only apply its static 50k fallback.
52
+ */
53
+ export interface CompressRequestMeta {
54
+ model?: string;
55
+ tools?: unknown[];
56
+ /**
57
+ * Explicit whole-request target (server `compression_target_tokens`),
58
+ * overriding the model-based budget. Set by `/memtree-compact` so a session
59
+ * is compressed even while it would still fit the model's window.
60
+ */
61
+ compressionTargetTokens?: number;
62
+ /**
63
+ * Server `compression_threshold_tokens`: compress only when the whole
64
+ * request exceeds this many tokens, and then to `compressionTargetTokens`.
65
+ * Without it the target is also the threshold. Servers that predate the
66
+ * field ignore it (they also omit `model_budget_tokens`, which is how the
67
+ * proxy tells them apart).
68
+ */
69
+ compressionThresholdTokens?: number;
70
+ /**
71
+ * Server `client_input_tokens`: the proxy's own estimate of the request's
72
+ * input tokens, the one its fallback gate refuses over-budget input by.
73
+ * Over the budget, the server compresses even when its char estimate says
74
+ * the request fits, so the two cannot disagree into a stuck session
75
+ * (2026-10-05). Part of the cache key: a different estimate can flip the
76
+ * server's verdict for the same messages.
77
+ */
78
+ clientInputTokens?: number;
79
+ /**
80
+ * Each assistant message's response usage (output, thinking, input), keyed
81
+ * by its position in the messages sent. Archived by the server for the
82
+ * MemTree page; never hashed, never part of the compression cache key.
83
+ */
84
+ messageUsage?: MessageUsage;
85
+ /**
86
+ * When Claude Code wrote each message (ISO), keyed like messageUsage. The
87
+ * server records a time range per MemTree input block; never hashed, never
88
+ * part of the compression cache key.
89
+ */
90
+ messageTimes?: MessageTimes;
91
+ /**
92
+ * Claude Code's session id, sent as `x-claude-code-session-id` so the
93
+ * server can list a session's MemTree pages by it. Also scopes cached pages.
94
+ */
95
+ sessionId?: string;
96
+ /** Sent as `x-client-meta`; stored on the usage row. Not part of the cache key. */
97
+ clientMeta?: Record<string, string>;
98
+ }
99
+ export interface CompressResult {
100
+ /** Processed (compressed) messages from the server; system role may be included. */
101
+ messages: Message[];
102
+ /**
103
+ * The server-side flatten of `messages`: exactly one user message whose
104
+ * string content is the whole compressed conversation, produced by the
105
+ * server's `flatten_to_single_user_message` (the single implementation of
106
+ * the flatten format). Returned when the request asked for `flatten: true`;
107
+ * pre-flatten servers omit it. The client treats these bytes as opaque —
108
+ * it no longer has a flatten of its own — so a result without this field
109
+ * cannot be forwarded compressed (see serverFlattenedMessages).
110
+ */
111
+ flattened_messages?: Message[];
112
+ /**
113
+ * Explicit server verdict on whether the conversation was rewritten. False
114
+ * on a passthrough (request already under the model budget, or no index
115
+ * yet), where `messages` is the conversation as-is and the server also
116
+ * omits `flattened_messages`. Older servers omit this field; callers fall
117
+ * back to the cached_tokens heuristic (see didMemtreeCompress).
118
+ */
119
+ compressed?: boolean;
120
+ /**
121
+ * The model's whole-request budget the server computed (tokens), whatever
122
+ * target the request set. Servers that predate it omit the field.
123
+ */
124
+ model_budget_tokens?: number;
125
+ /**
126
+ * Optional explicit unfolded index, consumed only by the memoryChars
127
+ * reqlog diagnostic. Older servers omit it; callers fall back to the first
128
+ * non-system processed message, which is the current server layout.
129
+ */
130
+ unfolded_memory?: string;
131
+ usage?: unknown;
132
+ /** Client-observed latency of the underlying HTTP call (survives retry dedupe). */
133
+ clientLatencyMs?: number;
134
+ /**
135
+ * The per-request MemTree page (`X-Polychat-Memtree-Url`), when the server
136
+ * sent one. `.json` on the same path is the machine-readable tree.
137
+ */
138
+ memtreeUrl?: string;
139
+ /**
140
+ * The completed index this turn was compressed against
141
+ * (`X-Polychat-Memtree-Index`); absent on index-only acks and servers that
142
+ * predate it. A new value means a new index finished and is now in use.
143
+ */
144
+ memtreeIndex?: string;
145
+ }
146
+ /**
147
+ * The server-flattened single user message of a compressed result, or null
148
+ * when the server did not provide a valid one (a pre-flatten server, or a
149
+ * malformed field). The flatten format lives server-side ONLY — the client
150
+ * must never re-derive or post-process it — so a null here means the result
151
+ * cannot be forwarded in compressed form and the caller degrades to the
152
+ * original history. Returns a fresh single-message array so callers may
153
+ * embed it in a request body without sharing structure with the result.
154
+ */
155
+ export declare function serverFlattenedMessages(result: CompressResult): Message[] | null;
156
+ /**
157
+ * Whether the server actually rewrote the conversation. A successful
158
+ * context_memory response is not enough: the endpoint returns the messages
159
+ * as-is while an index is still warming AND whenever the request already fits
160
+ * the model budget. The server's explicit `compressed` verdict is
161
+ * authoritative. The cached_tokens fallback only covers servers that predate
162
+ * it: cached_tokens is a billing prediction, not a compression signal, and is
163
+ * > 0 on most under-budget passthroughs — so on such servers those turns are
164
+ * flattened and Anthropic's prefix cache misses every human turn.
165
+ */
166
+ export declare function didMemtreeCompress(result: CompressResult): boolean;
167
+ /**
168
+ * Number of original prompt tokens covered by the index MemTree selected.
169
+ *
170
+ * Prefers `usage.indexed_tokens`: since 2026-10-04 the server bills a
171
+ * passthrough's covered input at nothing, so `cached_tokens` is 0 there even
172
+ * when a tree covers the prompt. Older servers only send `cached_tokens`.
173
+ */
174
+ export declare function cachedPromptTokenCount(result: CompressResult): number | undefined;
175
+ /** The server-reported model budget, when present and sane. */
176
+ export declare function modelBudgetTokens(result: CompressResult): number | undefined;
177
+ /**
178
+ * MemTree's informational estimate of the original, pre-consolidation prompt.
179
+ * Newer servers include images as visual-token estimates and deliberately keep
180
+ * this value separate from billable Context Memory usage.
181
+ */
182
+ export declare function rawPromptTokenCount(result: CompressResult): number | undefined;
183
+ /**
184
+ * Minimum conversation content, beyond any verbatim echo of the current turn,
185
+ * that a compressed response must carry before we will send it to Anthropic in
186
+ * place of the real history.
187
+ *
188
+ * Sized well below any useful memory (the server's own semantic floor is 15k
189
+ * chars) and well above an empty answer, so this fires only on genuine context
190
+ * loss and never on a legitimately aggressive compression.
191
+ */
192
+ export declare const MIN_RETAINED_HISTORY_CHARS = 2000;
193
+ export interface CompressedHistoryCheck {
194
+ /** Flattened conversation characters, falling back to structured messages. */
195
+ retainedChars: number;
196
+ /** Characters in the turn that prompted this request (as sent). */
197
+ currentTurnChars: number;
198
+ /** Non-system characters of prior conversation we asked it to compress. */
199
+ priorHistoryChars: number;
200
+ /** False when the response carries no usable prior conversation. */
201
+ usable: boolean;
202
+ }
203
+ /**
204
+ * Decide whether a compressed response still contains the conversation.
205
+ *
206
+ * This is deliberately NOT derived from `usage`. Every usage-derived signal —
207
+ * `cached_tokens` and the `raw - cached` tail — measures how much of the prompt
208
+ * the server's INDEX covered, not how much context it put in the response. A
209
+ * server that indexes every message and then allocates zero characters to
210
+ * memory (because fixed system+tool overhead exceeded its whole-request budget)
211
+ * reports perfect coverage while returning nothing at all, and the tail metric
212
+ * scores that empty answer as the best possible result. Only the body itself
213
+ * says what the model will actually see.
214
+ *
215
+ * "Retained prior conversation" is measured as the result's conversation
216
+ * characters minus only the part that verbatim-echoes the SENT current turn —
217
+ * not minus the current turn's full size. A turn the server itself shrank
218
+ * (e.g. a 100k-char pasted log returned as a 5k summary) leaves no verbatim
219
+ * echo, so its compressed rendering counts as retained context instead of
220
+ * sinking the score below zero and disabling compression on exactly the turns
221
+ * that need it. The failure the floor exists to catch — the server echoing the
222
+ * current turn back with essentially nothing of the prior conversation — still
223
+ * scores ~0, because every verbatim echo (whole, truncated, or embedded in a
224
+ * larger message) is fully subtracted, and a double echo — the turn embedded
225
+ * in the memory block AND replayed as the tail, or carried twice within one
226
+ * memory blob — is charged for every copy rather than netting out to a
227
+ * single turn's worth of "retained" text. An echo fragmented across
228
+ * consecutive text blocks is caught by re-checking each run of adjacent
229
+ * plain-text pieces as one coalesced passage. Fragments below a small length
230
+ * floor are never counted as echo, so a turn that quotes prior messages
231
+ * cannot sink the genuine memory that contains those same fragments. A rewritten-but-still-empty answer can
232
+ * in principle slip through; the trade is deliberate, since rewriting proves
233
+ * the server did compression work rather than dropping context.
234
+ */
235
+ export declare function checkCompressedHistory(result: CompressResult, sentMessages: Message[], minRetainedChars?: number): CompressedHistoryCheck;
236
+ /**
237
+ * Remove model-specific reasoning blocks from the copy sent to MemTree.
238
+ *
239
+ * Claude Code does not reliably replay prior-turn thinking after a model
240
+ * change or process resume. Thinking text, redacted payloads, and signatures
241
+ * are required when present in the live Anthropic request, but none is stable
242
+ * conversation identity for indexing.
243
+ *
244
+ * Scope this narrowly to assistant thinking blocks. A field named `signature`
245
+ * inside tool input/result data can be real user data and must remain intact.
246
+ * The original request is never mutated and still goes to Anthropic verbatim.
247
+ */
248
+ export declare function normalizeMessagesForMemtree(messages: Message[]): Message[];
249
+ export declare class MemtreeClient {
250
+ private baseUrl;
251
+ private apiKey;
252
+ private compressTimeoutMs;
253
+ private debug;
254
+ private reqlog;
255
+ private memtreeTools;
256
+ /** Complete compression request key → in-flight/settled promise (retry dedupe). */
257
+ private compressCache;
258
+ /** Message hashes already submitted for background indexing. */
259
+ private indexedHashes;
260
+ /** In-flight index-only calls, tracked so shutdown cannot outrun their logs. */
261
+ private backgroundIndexes;
262
+ /** Once draining begins, no later request may create another log producer. */
263
+ private backgroundClosing;
264
+ /** Session → its main lane's latest conversation, for the final index. */
265
+ private readonly mainConversations;
266
+ /** Session → its final index waiting for the session to stay idle. */
267
+ private readonly pendingFinals;
268
+ /** Session → hash of the conversation its last final index sent. */
269
+ private readonly finalIndexed;
270
+ private readonly finalPreparing;
271
+ private finalDrainDeadline;
272
+ /** FastAPI `detail` text from the most recent 402, or null while paid. */
273
+ private unpaidDetail;
274
+ /** Complete compression request key → whether its latest failure arms fuse. */
275
+ private compressFailureArming;
276
+ /**
277
+ * Non-null when the server last answered 402 (unpaid MemTree key): the
278
+ * server's human-readable detail text. Set by both compression and
279
+ * background-indexing calls; cleared by any subsequent success.
280
+ */
281
+ get paymentRequiredDetail(): string | null;
282
+ /**
283
+ * The blocking-compress abort budget. Exposed so the proxy's request log
284
+ * can label a failed compress that consumed (roughly) the whole budget as a
285
+ * timeout rather than a fast server error.
286
+ */
287
+ get compressBudgetMs(): number;
288
+ /** Change the `x-memtree-tools` value for later calls (undefined: none). */
289
+ setMemtreeTools(value: string | undefined): void;
290
+ /**
291
+ * GET a MemTree view path (`/usage/memtree/<id>[.json][?share=…]`) on the
292
+ * polychat host with this client's key. Backs the loopback `/memtree/*`
293
+ * passthrough, so an agent inside a ccc session reads the user's own tree
294
+ * through ANTHROPIC_BASE_URL without ever handling the key.
295
+ */
296
+ fetchMemTree(pathAndQuery: string, accept?: string, signal?: AbortSignal, extraHeaders?: Record<string, string>): Promise<{
297
+ status: number;
298
+ contentType: string;
299
+ body: Buffer;
300
+ }>;
301
+ constructor(opts: MemtreeOptions);
302
+ static hashMessages(messages: Message[]): string;
303
+ /**
304
+ * Whether compress(hash, limit, meta) would answer without contacting the server: an
305
+ * already-settled success, or a leg another caller has in flight. Callers
306
+ * that read a compress result as evidence about the SERVER's health must
307
+ * consult this first — a memoized answer proves only that the server was
308
+ * alive whenever that entry was created, which may be many minutes and one
309
+ * outage ago. In-flight entries count as cached here even though they are
310
+ * real round trips: conservatively discarding live evidence only forgoes an
311
+ * optimization, whereas trusting a stale one suppresses a real outage.
312
+ */
313
+ hasCachedCompress(hash: string, modelContextLimit: number, meta?: CompressRequestMeta): boolean;
314
+ /**
315
+ * Fuse classification for the most recent live compress failure of `hash`.
316
+ * The entry is recorded before the failed promise settles null, and a
317
+ * concurrent same-request retry MAY overwrite it with a different class — so
318
+ * callers must sample at their own leg's settle (a .then on the compress
319
+ * promise), not after awaiting other work; only that keeps the read paired
320
+ * with the failure it describes.
321
+ * Arming failures are evidence about the SERVER's health: no response at
322
+ * all (network error or the abort-budget timeout), any 5xx, and 402 —
323
+ * unpaid is global and persistent, so it must keep arming. Any other 4xx
324
+ * came from a responsive server rejecting this one request and must not
325
+ * open the shared fuse. Unclassified failures default to arming: muting
326
+ * the fuse needs positive evidence of a responsive server.
327
+ */
328
+ lastCompressFailureArming(hash: string, modelContextLimit: number, meta?: CompressRequestMeta): boolean;
329
+ /**
330
+ * Blocking user-turn compression. Returns null on ANY failure (timeout,
331
+ * network, 4xx/5xx including 402) — the caller must degrade to passthrough.
332
+ * On 402 the failure is additionally recorded in paymentRequiredDetail so
333
+ * the caller can distinguish "unpaid" from "outage".
334
+ */
335
+ compress(hash: string, messages: Message[], modelContextLimit: number, signal?: AbortSignal, meta?: CompressRequestMeta): Promise<CompressResult | null>;
336
+ /**
337
+ * Fire-and-forget background indexing for tool and first-user turns. Keeps
338
+ * the server index fed during tool loops; adds zero latency to the response
339
+ * path. A tool turn whose route-miss recovery got any non-null compress()
340
+ * response skips this: that call already submitted the same history. A
341
+ * recovery that returned null skips it too when the client has already
342
+ * disconnected, or on shutdown/402 — only an ordinary failure with a live
343
+ * client keeps the longer-budget background retry.
344
+ */
345
+ indexInBackground(hash: string, messages: Message[], modelContextLimit: number, sessionId?: string, clientMeta?: Record<string, string>,
346
+ /** Times for retained original messages, in the positions actually sent. */
347
+ messageTimesFor?: (messages: Message[]) => MessageTimes): void;
348
+ /**
349
+ * The main lane's latest conversation for a session, kept for its final
350
+ * index (scheduleFinalIndex). A new request also means the session did not
351
+ * end at the last Stop: its pending final index is cancelled.
352
+ */
353
+ noteMainConversation(sessionId: string | undefined, conversation: MainConversation): void;
354
+ /**
355
+ * After a main-lane Stop: once the session has been idle `delayMs` (or at
356
+ * shutdown, flushFinalIndexes), send its conversation plus the reply it
357
+ * ended with as one index-only call flagged final_index, so the server
358
+ * indexes the tail even below its 10k-token minimum (at most once per
359
+ * conversation per 30 minutes, server-side). Every per-request index call
360
+ * lacks that reply: it only appears in the next request. `readReply` reads
361
+ * it from the transcript when the call goes out, not inside the hook.
362
+ */
363
+ scheduleFinalIndex(sessionId: string | undefined, readReply: FinalReplyReader, delayMs?: number): void;
364
+ /**
365
+ * At shutdown, before drainBackground: finalize at most the three newest
366
+ * sessions, including sessions that never had a Stop (`-p`
367
+ * and non-TTY runs install no hooks). Never throws: a failure here must not
368
+ * change ccc's exit code. The calls share drainBackground's bounded wait.
369
+ */
370
+ flushFinalIndexes(timeoutMs?: number): void;
371
+ private cancelFinalIndex;
372
+ private fireFinalIndex;
373
+ private sendFinalIndex;
374
+ private submitIndexOnly;
375
+ /**
376
+ * Stop accepting background indexes and wait boundedly for those already in
377
+ * flight. Calls still running after the grace period are aborted, and this
378
+ * method does not return until their final request-log records are produced.
379
+ * The boolean is true for a graceful drain and false when abort was needed.
380
+ */
381
+ drainBackground(timeoutMs?: number): Promise<boolean>;
382
+ private compressKey;
383
+ private remember;
384
+ private callContextMemory;
385
+ private log;
386
+ }
387
+ /** What noteMainConversation keeps: the last main-lane request as sent to MemTree. */
388
+ export interface MainConversation {
389
+ messages: Message[];
390
+ modelContextLimit: number;
391
+ clientMeta?: Record<string, string>;
392
+ messageTimesFor?: (messages: Message[]) => MessageTimes;
393
+ /** The reply the session ended with (Claude Code's transcript), for a final index at exit. */
394
+ readReply?: FinalReplyReader;
395
+ }
396
+ /** The conversation plus the reply that ended it, unless it already ends with a reply. */
397
+ export declare function withFinalReply(messages: Message[], reply: Message | undefined): Message[];
398
+ //# sourceMappingURL=memtree.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memtree.d.ts","sourceRoot":"","sources":["../src/memtree.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EAAwC,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAC/F,OAAO,KAAK,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAExE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAEL,KAAK,OAAO,EACb,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,WAAW,gBAAgB,CAAC;AAGzC,eAAO,MAAM,cAAc,EAAE,MAEpB,CAAC;AAUV;;;;GAIG;AACH,e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