@shanepadgett/tau-agent 0.18.1 → 0.19.0

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.
@@ -0,0 +1,11 @@
1
+ # Cache Diagnostics
2
+
3
+ Cache Diagnostics records compact fingerprints of Tau's final provider requests. It helps separate local prompt changes from unexplained provider cache misses without storing prompts, source code, or credentials.
4
+
5
+ The extension runs automatically. Logs are written under:
6
+
7
+ ```text
8
+ ~/.pi/agent/cache-diagnostics/
9
+ ```
10
+
11
+ Run `/cache-debug` after suspicious misses. Tau writes a bounded report under `~/.pi/agent/cache-diagnostics/reports/` for later investigation. Logs and reports older than 30 days are removed when a session starts.
@@ -0,0 +1,655 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { appendFile, type FileHandle, mkdir, open, readdir, rename, stat, unlink, writeFile } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { getAgentDir, type BuildSystemPromptOptions, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
5
+
6
+ const RETENTION_MS = 30 * 24 * 60 * 60 * 1000;
7
+ const CACHE_NOISE_FLOOR = 1_024;
8
+ const MAX_MEMORY_RECORDS = 500;
9
+ const MAX_REPORT_INCIDENTS = 20;
10
+ const RECENT_REPORT_REQUESTS = 30;
11
+ const MAX_LOG_TAIL_BYTES = 10 * 1_024 * 1_024;
12
+
13
+ interface HashFingerprint {
14
+ hash: string;
15
+ bytes: number;
16
+ }
17
+
18
+ interface ItemFingerprint extends HashFingerprint {
19
+ type: string | null;
20
+ role: string | null;
21
+ name: string | null;
22
+ }
23
+
24
+ interface ToolFingerprint extends HashFingerprint {
25
+ name: string | null;
26
+ }
27
+
28
+ interface FieldFingerprint extends HashFingerprint {
29
+ name: string;
30
+ }
31
+
32
+ interface PromptState {
33
+ hash: string;
34
+ systemPromptHash: string;
35
+ selectedTools: string[];
36
+ toolSnippetsHash: string | null;
37
+ promptGuidelinesHash: string | null;
38
+ appendSystemPromptHash: string | null;
39
+ customPromptHash: string | null;
40
+ contextFiles: Array<{ path: string; hash: string }>;
41
+ skills: Array<{ name: string | null; path: string | null }>;
42
+ }
43
+
44
+ interface PayloadFingerprint {
45
+ model: string | null;
46
+ promptCacheKeyHash: string | null;
47
+ instructionsHash: string | null;
48
+ toolsHash: string | null;
49
+ nonSequenceHash: string;
50
+ sequenceField: "input" | "messages" | "none";
51
+ fields: FieldFingerprint[];
52
+ tools: ToolFingerprint[];
53
+ items: ItemFingerprint[];
54
+ }
55
+
56
+ interface RequestRecord {
57
+ kind: "request";
58
+ version: 2;
59
+ timestamp: string;
60
+ id: string;
61
+ turnIndex: number | null;
62
+ previousRequestId: string | null;
63
+ model: string | null;
64
+ promptCacheKeyHash: string | null;
65
+ instructionsHash: string | null;
66
+ toolsHash: string | null;
67
+ nonSequenceHash: string;
68
+ sequenceField: "input" | "messages" | "none";
69
+ fields: FieldFingerprint[];
70
+ tools: ToolFingerprint[];
71
+ items: ItemFingerprint[];
72
+ previousItems: number;
73
+ commonPrefixItems: number;
74
+ stableEnvelope: boolean;
75
+ previousExactPrefix: boolean;
76
+ previousPromptTokens: number;
77
+ changes: {
78
+ envelopeFields: string[];
79
+ toolsAdded: string[];
80
+ toolsRemoved: string[];
81
+ toolsChanged: string[];
82
+ firstChangedItem: number | null;
83
+ promptStateChanged: boolean;
84
+ };
85
+ promptState: PromptState | null;
86
+ }
87
+
88
+ interface ResponseRecord {
89
+ kind: "response";
90
+ version: 2;
91
+ timestamp: string;
92
+ id: string;
93
+ status: number;
94
+ headers: Record<string, string>;
95
+ }
96
+
97
+ interface ResultRecord {
98
+ kind: "result";
99
+ version: 2;
100
+ timestamp: string;
101
+ id: string;
102
+ provider: string;
103
+ model: string;
104
+ stopReason: string;
105
+ usage: {
106
+ input: number;
107
+ cacheRead: number;
108
+ cacheWrite: number;
109
+ promptTokens: number;
110
+ };
111
+ previousExactPrefix: boolean;
112
+ missedTokens: number;
113
+ cacheMiss: boolean;
114
+ baselinePromoted: boolean;
115
+ }
116
+
117
+ interface MarkerRecord {
118
+ kind: "marker";
119
+ version: 2;
120
+ timestamp: string;
121
+ name: string;
122
+ details: Record<string, string | number | boolean | null>;
123
+ }
124
+
125
+ interface Attempt {
126
+ id: string;
127
+ fingerprint: PayloadFingerprint;
128
+ request: RequestRecord;
129
+ responseStatus: number | undefined;
130
+ }
131
+
132
+ export default function cacheDiagnosticsExtension(pi: ExtensionAPI): void {
133
+ const directory = join(getAgentDir(), "cache-diagnostics");
134
+ const reportsDirectory = join(directory, "reports");
135
+ const runtimeId = randomUUID();
136
+ let logFile = "";
137
+ let cwd = "";
138
+ let sessionId = "";
139
+ let sessionFile: string | null = null;
140
+ let requestSequence = 0;
141
+ let turnIndex: number | null = null;
142
+ let latestPromptState: PromptState | null = null;
143
+ let previousPromptState: PromptState | null = null;
144
+ let previousFingerprint: PayloadFingerprint | undefined;
145
+ let previousRequestId: string | null = null;
146
+ let previousPromptTokens = 0;
147
+ let cacheActivitySeen = false;
148
+ let awaitingResponses: Attempt[] = [];
149
+ let turnAttempts: Attempt[] = [];
150
+ let requests: RequestRecord[] = [];
151
+ let responses: ResponseRecord[] = [];
152
+ let results: ResultRecord[] = [];
153
+ let markers: MarkerRecord[] = [];
154
+
155
+ const appendRecord = async (record: object): Promise<void> => {
156
+ const path = logFile;
157
+ if (!path) return;
158
+ await appendFile(path, `${JSON.stringify(record)}\n`, "utf8");
159
+ };
160
+ const addMarker = async (
161
+ name: string,
162
+ details: Record<string, string | number | boolean | null> = {},
163
+ ): Promise<void> => {
164
+ const marker: MarkerRecord = {
165
+ kind: "marker",
166
+ version: 2,
167
+ timestamp: new Date().toISOString(),
168
+ name,
169
+ details,
170
+ };
171
+ markers = keepRecent([...markers, marker]);
172
+ await appendRecord(marker);
173
+ };
174
+ const resetComparison = () => {
175
+ previousFingerprint = undefined;
176
+ previousRequestId = null;
177
+ previousPromptState = null;
178
+ previousPromptTokens = 0;
179
+ cacheActivitySeen = false;
180
+ };
181
+
182
+ pi.registerCommand("cache-debug", {
183
+ description: "Write a bounded prompt-cache diagnostic report for this session",
184
+ async handler(_args, ctx) {
185
+ await mkdir(reportsDirectory, { recursive: true });
186
+ const incidentResults = results.filter((result) => result.cacheMiss).slice(-MAX_REPORT_INCIDENTS);
187
+ const selectedIds = new Set<string>();
188
+ for (const result of incidentResults) {
189
+ selectedIds.add(result.id);
190
+ const request = requests.find((candidate) => candidate.id === result.id);
191
+ if (request?.previousRequestId) selectedIds.add(request.previousRequestId);
192
+ }
193
+ for (const request of requests.slice(-RECENT_REPORT_REQUESTS)) selectedIds.add(request.id);
194
+ const selectedRequests = requests.filter((request) => selectedIds.has(request.id));
195
+ const selectedResponses = responses.filter((response) => selectedIds.has(response.id));
196
+ const selectedResults = results.filter((result) => selectedIds.has(result.id));
197
+ const createdAt = new Date().toISOString();
198
+ const safeSessionId = (sessionId || "ephemeral").replaceAll(/[^a-zA-Z0-9_-]/g, "_");
199
+ const reportFile = join(reportsDirectory, `${safeSessionId}-${createdAt.replaceAll(/[:.]/g, "-")}.json`);
200
+ const temporaryFile = `${reportFile}.${randomUUID()}.tmp`;
201
+ const report = {
202
+ kind: "tau-cache-debug",
203
+ version: 1,
204
+ createdAt,
205
+ cwd: cwd || ctx.cwd,
206
+ runtimeId,
207
+ sessionId: sessionId || null,
208
+ sessionFile,
209
+ sourceLog: logFile || null,
210
+ summary: {
211
+ requestsObserved: requests.length,
212
+ resultsObserved: results.length,
213
+ cacheMissesObserved: results.filter((result) => result.cacheMiss).length,
214
+ requestsIncluded: selectedRequests.length,
215
+ models: [...new Set(results.map((result) => `${result.provider}/${result.model}`))],
216
+ },
217
+ selection: {
218
+ maxIncidents: MAX_REPORT_INCIDENTS,
219
+ recentRequests: RECENT_REPORT_REQUESTS,
220
+ },
221
+ markers,
222
+ requests: selectedRequests,
223
+ responses: selectedResponses,
224
+ results: selectedResults,
225
+ };
226
+ try {
227
+ await writeFile(temporaryFile, `${JSON.stringify(report, null, "\t")}\n`, { encoding: "utf8", flag: "wx" });
228
+ await rename(temporaryFile, reportFile);
229
+ } catch (error) {
230
+ await unlink(temporaryFile).catch(() => undefined);
231
+ throw error;
232
+ }
233
+ ctx.ui.notify(`Cache debug written: ${reportFile}`, "info");
234
+ },
235
+ });
236
+
237
+ pi.on("session_start", async (_event, ctx) => {
238
+ cwd = ctx.cwd;
239
+ sessionId = ctx.sessionManager.getSessionId();
240
+ sessionFile = ctx.sessionManager.getSessionFile() ?? null;
241
+ requestSequence = 0;
242
+ turnIndex = null;
243
+ latestPromptState = null;
244
+ awaitingResponses = [];
245
+ turnAttempts = [];
246
+ resetComparison();
247
+ await mkdir(reportsDirectory, { recursive: true });
248
+ logFile = join(directory, `${sessionId.replaceAll(/[^a-zA-Z0-9_-]/g, "_")}.jsonl`);
249
+ const cutoff = Date.now() - RETENTION_MS;
250
+ for (const parent of [directory, reportsDirectory]) {
251
+ for (const entry of await readdir(parent, { withFileTypes: true })) {
252
+ if (
253
+ !entry.isFile() ||
254
+ (!entry.name.endsWith(".jsonl") && !entry.name.endsWith(".json") && !entry.name.endsWith(".tmp"))
255
+ )
256
+ continue;
257
+ const path = join(parent, entry.name);
258
+ if ((await stat(path)).mtimeMs < cutoff) await unlink(path);
259
+ }
260
+ }
261
+ const persisted = await readRecentLogRecords(logFile);
262
+ requests = keepRecent(
263
+ persisted
264
+ .filter((record) => record.kind === "request" && record.version === 2)
265
+ .map((record) => record as unknown as RequestRecord),
266
+ );
267
+ responses = keepRecent(
268
+ persisted
269
+ .filter((record) => record.kind === "response" && record.version === 2)
270
+ .map((record) => record as unknown as ResponseRecord),
271
+ );
272
+ results = keepRecent(
273
+ persisted
274
+ .filter((record) => record.kind === "result" && record.version === 2)
275
+ .map((record) => record as unknown as ResultRecord),
276
+ );
277
+ markers = keepRecent(
278
+ persisted
279
+ .filter((record) => record.kind === "marker" && record.version === 2)
280
+ .map((record) => record as unknown as MarkerRecord),
281
+ );
282
+ let latestModelSelectIndex = -1;
283
+ for (let index = persisted.length - 1; index >= 0; index -= 1) {
284
+ const record = persisted[index];
285
+ if (record?.kind !== "marker" || record.version !== 2 || record.name !== "model-select") continue;
286
+ latestModelSelectIndex = index;
287
+ break;
288
+ }
289
+ const comparisonRecords = persisted.slice(latestModelSelectIndex + 1);
290
+ const comparisonResults = comparisonRecords
291
+ .filter((record) => record.kind === "result" && record.version === 2)
292
+ .map((record) => record as unknown as ResultRecord);
293
+ const latestPromotedResult = [...comparisonResults].reverse().find((result) => result.baselinePromoted);
294
+ const latestRequest = latestPromotedResult
295
+ ? requests.find((request) => request.id === latestPromotedResult.id)
296
+ : undefined;
297
+ if (latestPromotedResult && latestRequest) {
298
+ previousFingerprint = payloadFingerprintFromRequest(latestRequest);
299
+ previousRequestId = latestRequest.id;
300
+ previousPromptState = latestRequest.promptState;
301
+ previousPromptTokens = latestPromotedResult.usage.promptTokens;
302
+ cacheActivitySeen = comparisonResults.some(
303
+ (result) => result.baselinePromoted && result.usage.cacheRead + result.usage.cacheWrite > 0,
304
+ );
305
+ }
306
+ await appendRecord({
307
+ kind: "runtime",
308
+ version: 2,
309
+ timestamp: new Date().toISOString(),
310
+ runtimeId,
311
+ cwd,
312
+ sessionId,
313
+ sessionFile,
314
+ });
315
+ });
316
+
317
+ pi.on("before_agent_start", (event) => {
318
+ latestPromptState = fingerprintPromptState(event.systemPrompt, event.systemPromptOptions);
319
+ });
320
+
321
+ pi.on("turn_start", (event) => {
322
+ turnIndex = event.turnIndex;
323
+ turnAttempts = [];
324
+ });
325
+
326
+ pi.on("before_provider_request", async (event) => {
327
+ const unresolvedIds = new Set(
328
+ turnAttempts.filter((attempt) => attempt.responseStatus === undefined).map((attempt) => attempt.id),
329
+ );
330
+ if (unresolvedIds.size > 0) {
331
+ awaitingResponses = awaitingResponses.filter((attempt) => !unresolvedIds.has(attempt.id));
332
+ }
333
+ requestSequence += 1;
334
+ const id = `${runtimeId}:${requestSequence}`;
335
+ const fingerprint = fingerprintPayload(event.payload);
336
+ let commonPrefixItems = 0;
337
+ if (previousFingerprint) {
338
+ const limit = Math.min(previousFingerprint.items.length, fingerprint.items.length);
339
+ while (
340
+ commonPrefixItems < limit &&
341
+ previousFingerprint.items[commonPrefixItems]?.hash === fingerprint.items[commonPrefixItems]?.hash
342
+ ) {
343
+ commonPrefixItems += 1;
344
+ }
345
+ }
346
+ const stableEnvelope = previousFingerprint?.nonSequenceHash === fingerprint.nonSequenceHash;
347
+ const previousExactPrefix =
348
+ previousFingerprint !== undefined &&
349
+ stableEnvelope &&
350
+ previousFingerprint.sequenceField === fingerprint.sequenceField &&
351
+ commonPrefixItems === previousFingerprint.items.length;
352
+ const request: RequestRecord = {
353
+ kind: "request",
354
+ version: 2,
355
+ timestamp: new Date().toISOString(),
356
+ id,
357
+ turnIndex,
358
+ previousRequestId,
359
+ model: fingerprint.model,
360
+ promptCacheKeyHash: fingerprint.promptCacheKeyHash,
361
+ instructionsHash: fingerprint.instructionsHash,
362
+ toolsHash: fingerprint.toolsHash,
363
+ nonSequenceHash: fingerprint.nonSequenceHash,
364
+ sequenceField: fingerprint.sequenceField,
365
+ fields: fingerprint.fields,
366
+ tools: fingerprint.tools,
367
+ items: fingerprint.items,
368
+ previousItems: previousFingerprint?.items.length ?? 0,
369
+ commonPrefixItems,
370
+ stableEnvelope,
371
+ previousExactPrefix,
372
+ previousPromptTokens,
373
+ changes: compareFingerprints(previousFingerprint, fingerprint, previousPromptState, latestPromptState),
374
+ promptState: latestPromptState,
375
+ };
376
+ const attempt: Attempt = { id, fingerprint, request, responseStatus: undefined };
377
+ awaitingResponses.push(attempt);
378
+ turnAttempts.push(attempt);
379
+ requests = keepRecent([...requests, request]);
380
+ await appendRecord(request);
381
+ });
382
+
383
+ pi.on("after_provider_response", async (event) => {
384
+ const attempt = awaitingResponses.shift();
385
+ if (!attempt) return;
386
+ attempt.responseStatus = event.status;
387
+ const selectedHeaders = Object.fromEntries(
388
+ Object.entries(event.headers).filter(([name]) => {
389
+ const normalized = name.toLowerCase();
390
+ return normalized.includes("request-id") || normalized === "cf-ray";
391
+ }),
392
+ );
393
+ const response: ResponseRecord = {
394
+ kind: "response",
395
+ version: 2,
396
+ timestamp: new Date().toISOString(),
397
+ id: attempt.id,
398
+ status: event.status,
399
+ headers: selectedHeaders,
400
+ };
401
+ responses = keepRecent([...responses, response]);
402
+ await appendRecord(response);
403
+ });
404
+
405
+ pi.on("message_end", async (event) => {
406
+ if (event.message.role !== "assistant") return;
407
+ const attempt = turnAttempts.at(-1);
408
+ const completedAttemptIds = new Set(turnAttempts.map((candidate) => candidate.id));
409
+ awaitingResponses = awaitingResponses.filter((candidate) => !completedAttemptIds.has(candidate.id));
410
+ turnAttempts = [];
411
+ if (!attempt) return;
412
+ const usage = event.message.usage;
413
+ const promptTokens = usage.input + usage.cacheRead + usage.cacheWrite;
414
+ const baselinePromoted =
415
+ event.message.stopReason !== "error" &&
416
+ event.message.stopReason !== "aborted" &&
417
+ (attempt.responseStatus === undefined || (attempt.responseStatus >= 200 && attempt.responseStatus < 300));
418
+ const missedTokens =
419
+ baselinePromoted && attempt.request.previousExactPrefix
420
+ ? Math.max(0, Math.min(attempt.request.previousPromptTokens, promptTokens) - usage.cacheRead)
421
+ : 0;
422
+ const cacheMiss = baselinePromoted && cacheActivitySeen && missedTokens > CACHE_NOISE_FLOOR;
423
+ const result: ResultRecord = {
424
+ kind: "result",
425
+ version: 2,
426
+ timestamp: new Date().toISOString(),
427
+ id: attempt.id,
428
+ provider: event.message.provider,
429
+ model: event.message.model,
430
+ stopReason: event.message.stopReason,
431
+ usage: {
432
+ input: usage.input,
433
+ cacheRead: usage.cacheRead,
434
+ cacheWrite: usage.cacheWrite,
435
+ promptTokens,
436
+ },
437
+ previousExactPrefix: attempt.request.previousExactPrefix,
438
+ missedTokens,
439
+ cacheMiss,
440
+ baselinePromoted,
441
+ };
442
+ results = keepRecent([...results, result]);
443
+ if (baselinePromoted) {
444
+ previousFingerprint = attempt.fingerprint;
445
+ previousRequestId = attempt.id;
446
+ previousPromptState = attempt.request.promptState;
447
+ previousPromptTokens = promptTokens;
448
+ cacheActivitySeen ||= usage.cacheRead + usage.cacheWrite > 0;
449
+ }
450
+ await appendRecord(result);
451
+ });
452
+
453
+ pi.on("model_select", async (event) => {
454
+ resetComparison();
455
+ await addMarker("model-select", {
456
+ provider: event.model.provider,
457
+ model: event.model.id,
458
+ previousProvider: event.previousModel?.provider ?? null,
459
+ previousModel: event.previousModel?.id ?? null,
460
+ source: event.source,
461
+ });
462
+ });
463
+ pi.on("thinking_level_select", (event) =>
464
+ addMarker("thinking-level-select", { level: event.level, previousLevel: event.previousLevel }),
465
+ );
466
+ pi.on("session_compact", (event) =>
467
+ addMarker("session-compact", {
468
+ reason: event.reason,
469
+ fromExtension: event.fromExtension,
470
+ willRetry: event.willRetry,
471
+ }),
472
+ );
473
+ pi.on("session_tree", () => addMarker("session-tree"));
474
+ pi.on("tool_execution_end", (event) => {
475
+ if (event.toolName !== "context_prune" && event.toolName !== "load_tools") return;
476
+ return addMarker("cache-affecting-tool", { tool: event.toolName, isError: event.isError });
477
+ });
478
+ }
479
+
480
+ async function readRecentLogRecords(path: string): Promise<Array<Record<string, unknown>>> {
481
+ let handle: FileHandle | undefined;
482
+ try {
483
+ handle = await open(path, "r");
484
+ const size = (await handle.stat()).size;
485
+ const start = Math.max(0, size - MAX_LOG_TAIL_BYTES);
486
+ const buffer = Buffer.alloc(size - start);
487
+ await handle.read(buffer, 0, buffer.length, start);
488
+ let text = buffer.toString("utf8");
489
+ if (start > 0) {
490
+ const firstNewline = text.indexOf("\n");
491
+ text = firstNewline < 0 ? "" : text.slice(firstNewline + 1);
492
+ }
493
+ const records: Array<Record<string, unknown>> = [];
494
+ for (const line of text.split("\n")) {
495
+ if (!line) continue;
496
+ try {
497
+ const value: unknown = JSON.parse(line);
498
+ if (isRecord(value)) records.push(value);
499
+ } catch {
500
+ // Ignore a partial final record left by an interrupted append.
501
+ }
502
+ }
503
+ return records;
504
+ } catch (error) {
505
+ if (isRecord(error) && error.code === "ENOENT") return [];
506
+ throw error;
507
+ } finally {
508
+ await handle?.close();
509
+ }
510
+ }
511
+
512
+ function payloadFingerprintFromRequest(request: RequestRecord): PayloadFingerprint {
513
+ return {
514
+ model: request.model,
515
+ promptCacheKeyHash: request.promptCacheKeyHash,
516
+ instructionsHash: request.instructionsHash,
517
+ toolsHash: request.toolsHash,
518
+ nonSequenceHash: request.nonSequenceHash,
519
+ sequenceField: request.sequenceField,
520
+ fields: request.fields,
521
+ tools: request.tools,
522
+ items: request.items,
523
+ };
524
+ }
525
+
526
+ function fingerprintPromptState(systemPrompt: string, options: BuildSystemPromptOptions): PromptState {
527
+ const state = {
528
+ systemPromptHash: hashJson(systemPrompt).hash,
529
+ selectedTools: options.selectedTools ?? [],
530
+ toolSnippetsHash: valueHash(options.toolSnippets),
531
+ promptGuidelinesHash: valueHash(options.promptGuidelines),
532
+ appendSystemPromptHash: valueHash(options.appendSystemPrompt),
533
+ customPromptHash: valueHash(options.customPrompt),
534
+ contextFiles: (options.contextFiles ?? []).map((file) => ({
535
+ path: file.path,
536
+ hash: hashJson(file.content).hash,
537
+ })),
538
+ skills: (options.skills ?? []).map((skill) => {
539
+ const record = skill as unknown as Record<string, unknown>;
540
+ return {
541
+ name: typeof record.name === "string" ? record.name : null,
542
+ path: typeof record.path === "string" ? record.path : null,
543
+ };
544
+ }),
545
+ };
546
+ return { hash: hashJson(state).hash, ...state };
547
+ }
548
+
549
+ function fingerprintPayload(payload: unknown): PayloadFingerprint {
550
+ const record = isRecord(payload) ? payload : {};
551
+ const sequenceField = Array.isArray(record.input) ? "input" : Array.isArray(record.messages) ? "messages" : "none";
552
+ const sequence = sequenceField === "none" ? [] : (record[sequenceField] as unknown[]);
553
+ const nonSequence = Object.fromEntries(Object.entries(record).filter(([key]) => key !== sequenceField));
554
+ const tools = Array.isArray(record.tools)
555
+ ? record.tools.map((tool) => {
556
+ const fingerprint = hashJson(tool);
557
+ return { ...fingerprint, name: toolName(tool) };
558
+ })
559
+ : [];
560
+ return {
561
+ model: typeof record.model === "string" ? record.model : null,
562
+ promptCacheKeyHash: valueHash(record.prompt_cache_key),
563
+ instructionsHash: valueHash(record.instructions ?? record.system),
564
+ toolsHash: valueHash(record.tools),
565
+ nonSequenceHash: hashJson(nonSequence).hash,
566
+ sequenceField,
567
+ fields: Object.entries(nonSequence)
568
+ .map(([name, value]) => ({ name, ...hashJson(value) }))
569
+ .sort((left, right) => left.name.localeCompare(right.name)),
570
+ tools,
571
+ items: sequence.map((item) => {
572
+ const fingerprint = hashJson(item);
573
+ const itemRecord = isRecord(item) ? item : {};
574
+ return {
575
+ ...fingerprint,
576
+ type: typeof itemRecord.type === "string" ? itemRecord.type : null,
577
+ role: typeof itemRecord.role === "string" ? itemRecord.role : null,
578
+ name: typeof itemRecord.name === "string" ? itemRecord.name : null,
579
+ };
580
+ }),
581
+ };
582
+ }
583
+
584
+ function compareFingerprints(
585
+ previous: PayloadFingerprint | undefined,
586
+ current: PayloadFingerprint,
587
+ previousPromptState: PromptState | null,
588
+ currentPromptState: PromptState | null,
589
+ ): RequestRecord["changes"] {
590
+ const previousFields = new Map(previous?.fields.map((field) => [field.name, field.hash]) ?? []);
591
+ const currentFields = new Map(current.fields.map((field) => [field.name, field.hash]));
592
+ const envelopeFields = [...new Set([...previousFields.keys(), ...currentFields.keys()])]
593
+ .filter((name) => previousFields.get(name) !== currentFields.get(name))
594
+ .sort();
595
+ const previousTools = namedToolHashes(previous?.tools ?? []);
596
+ const currentTools = namedToolHashes(current.tools);
597
+ const toolsAdded = [...currentTools.keys()].filter((name) => !previousTools.has(name));
598
+ const toolsRemoved = [...previousTools.keys()].filter((name) => !currentTools.has(name));
599
+ const toolsChanged = [...currentTools.keys()].filter(
600
+ (name) => previousTools.has(name) && previousTools.get(name) !== currentTools.get(name),
601
+ );
602
+ let firstChangedItem: number | null = null;
603
+ if (previous) {
604
+ const limit = Math.max(previous.items.length, current.items.length);
605
+ for (let index = 0; index < limit; index += 1) {
606
+ if (previous.items[index]?.hash === current.items[index]?.hash) continue;
607
+ firstChangedItem = index;
608
+ break;
609
+ }
610
+ }
611
+ return {
612
+ envelopeFields,
613
+ toolsAdded,
614
+ toolsRemoved,
615
+ toolsChanged,
616
+ firstChangedItem,
617
+ promptStateChanged:
618
+ previousPromptState !== null &&
619
+ currentPromptState !== null &&
620
+ previousPromptState.hash !== currentPromptState.hash,
621
+ };
622
+ }
623
+
624
+ function namedToolHashes(tools: ToolFingerprint[]): Map<string, string> {
625
+ const named = new Map<string, string>();
626
+ for (const [index, tool] of tools.entries()) named.set(tool.name ?? `<unnamed:${index}>`, tool.hash);
627
+ return named;
628
+ }
629
+
630
+ function toolName(value: unknown): string | null {
631
+ if (!isRecord(value)) return null;
632
+ if (typeof value.name === "string") return value.name;
633
+ if (isRecord(value.function) && typeof value.function.name === "string") return value.function.name;
634
+ return null;
635
+ }
636
+
637
+ function valueHash(value: unknown): string | null {
638
+ return value === undefined ? null : hashJson(value).hash;
639
+ }
640
+
641
+ function hashJson(value: unknown): HashFingerprint {
642
+ const serialized = JSON.stringify(value) ?? "undefined";
643
+ return {
644
+ hash: createHash("sha256").update(serialized).digest("hex"),
645
+ bytes: Buffer.byteLength(serialized, "utf8"),
646
+ };
647
+ }
648
+
649
+ function keepRecent<T>(items: T[]): T[] {
650
+ return items.length <= MAX_MEMORY_RECORDS ? items : items.slice(-MAX_MEMORY_RECORDS);
651
+ }
652
+
653
+ function isRecord(value: unknown): value is Record<string, unknown> {
654
+ return typeof value === "object" && value !== null;
655
+ }
@@ -1,22 +1,135 @@
1
1
  # Context Pruning
2
2
 
3
- Context Pruning removes stale tool evidence from the context sent to the model while leaving the session transcript intact. The agent can create a prune anchor after broad exploration has converged or enough irrelevant evidence has accumulated.
3
+ Context Pruning removes old tool calls and their results from the messages sent to the model. It does not delete them from the saved conversation. Branch switching, transcript review, and normal Pi compaction still see the original records.
4
4
 
5
- Before calling `context_prune`, the agent records durable conclusions and its next action in visible prose. It can retain exact tool exchanges, retain current knowledge of selected files, and defer files until a stated condition applies. The tool prepares any required file snapshots before applying the anchor, skips attempts with low estimated savings, and continues the current agent run automatically.
5
+ The feature can also remove old full-file autoread messages and old private reasoning. User messages, visible assistant prose, branch summaries, compaction summaries, shell records, and unrelated custom messages remain.
6
6
 
7
- After tool-using turns, Tau occasionally adds a compact context marker. At lower usage it is informational. Under pressure it asks the agent to finish a coherent step and prune only when the savings are worthwhile. The full steering text stays hidden, and the agent is told not to discuss context management.
7
+ ## The important distinction
8
8
 
9
- Run `/prune` with no arguments to ask the agent to create an anchor and continue its unfinished work. The same validation and minimum-savings rules still apply, so a low-value attempt is skipped.
9
+ Context usage and pruning are separate decisions.
10
10
 
11
- Prune anchors, automatic marker boundaries, and warning-colored Tau tool rows follow the active session branch. Switching branches restores that branch's state. Pi's manual, threshold, and overflow compaction continue to work normally.
11
+ - The context percentage decides when Tau gives the agent a pruning hint.
12
+ - A `context_prune` call decides what old evidence the agent wants to remove or retain.
13
+ - Tau then checks whether that exact removal is safe and whether it saves enough tokens.
12
14
 
13
- Manual UI check: Tau-owned tool rows and autoreads should turn warning-colored when pruned. Native Pi rows and fallback rows that don't use Tau's row-state renderer won't change color; pruning and read-cache behavior still apply to them.
15
+ Being at 70% context does not force Tau to apply a prune. With the default settings, 70% is high enough for a pressure hint, but every prune still has to pass the checks below.
16
+
17
+ ## What happens when the agent calls `context_prune`
18
+
19
+ The agent supplies three required lists:
20
+
21
+ - `keepFiles`: files whose complete current contents must remain available.
22
+ - `keepToolCalls`: exact earlier tool calls and results that must remain available.
23
+ - `deferFiles`: files that are currently irrelevant but may matter under a stated condition.
24
+
25
+ Unless retained by one of the first two lists, every complete earlier tool exchange and every autoread currently eligible for pruning is selected for removal. The current `context_prune` call is the anchor: later messages and tool results are unaffected by that anchor.
26
+
27
+ The agent is instructed to put durable conclusions, conditional information, and its next action in visible prose immediately before the call. That prose survives pruning. Tau enforces that `context_prune` is the only tool call in its assistant message, but it does not judge whether the preceding prose is complete or useful.
28
+
29
+ Tau prepares the result without changing the active context. It estimates the token count before and after the proposed removal, including any replacement file contents and deferred-file note. It applies the prune only when the estimated saving is at least `minimumReclaimTokens`.
30
+
31
+ After a successful call, Tau removes the selected evidence from future model input. The saved conversation remains unchanged. The tool result records exactly which tool exchanges and autoreads were removed, retained, or refreshed.
32
+
33
+ ## Why a prune is skipped
34
+
35
+ A skipped prune removes nothing and publishes no deferred-file note or replacement file contents. Tau skips when any of these checks fails:
36
+
37
+ 1. Context Pruning is disabled.
38
+ 2. Tau does not have a usable copy of the exact messages most recently sent to the model.
39
+ 3. The `context_prune` call is missing from the active branch.
40
+ 4. Another tool call appears beside `context_prune` in the same assistant message.
41
+ 5. The current model input contains duplicate, mismatched, or orphaned tool records that Tau cannot safely remove. An unmatched call from an explicitly aborted assistant response is discarded and does not block pruning.
42
+ 6. A retained tool-call ID is duplicated, missing, or is not a complete exchange in the current model input.
43
+ 7. Two selected file paths resolve to the same file, including aliases and symbolic links across `keepFiles` and `deferFiles`.
44
+ 8. A retained file does not have an earlier complete-file read, has malformed read evidence, cannot currently be read as UTF-8, or exceeds the 1 MiB complete-file limit.
45
+ 9. The estimated saving is below `minimumReclaimTokens`.
46
+
47
+ Cancellation, a session lifecycle change during preparation, and failures while publishing an already prepared prune are reported as tool failures rather than skipped prunes.
48
+
49
+ ### “The latest provider-context projection is unavailable”
50
+
51
+ This error uses an internal term. In plain language, Tau did not have a usable copy of the exact message list the model had just received.
52
+
53
+ Tau normally saves that list while Pi prepares a model turn. Tau clears it on session start, branch changes, compaction, and shutdown. It also refuses to save a list with invalid tool-call/result pairing. If the list is missing or belongs to an earlier session state, `context_prune` stops before checking the agent’s selections or estimating savings.
54
+
55
+ Pi can save an aborted assistant response that requested a tool but never received a result. Tau discards that abandoned call when preparing later model input. Other unmatched calls and results still fail validation because Tau cannot prove whether their evidence is incomplete.
56
+
57
+ For example, a row that says `context_prune 29 selections` followed by this error means:
58
+
59
+ - the agent supplied 29 items across the three selection lists;
60
+ - Tau did not inspect or apply those selections;
61
+ - no old tool calls were removed;
62
+ - the context percentage did not cause the rejection;
63
+ - the current implementation does not reconstruct the missing list during that call.
64
+
65
+ The response tells the agent not to retry immediately because repeated calls in the same state would usually fail for the same reason. A later model turn should normally give Tau a new list. Repeated occurrences indicate a feature defect or a session history that Tau cannot safely process.
66
+
67
+ ## Retaining files
68
+
69
+ `keepFiles` preserves complete current file knowledge, not necessarily the original read row.
70
+
71
+ Each selected file must have been read completely earlier in the current model input. A partial read is insufficient. Tau then reads the file from disk again and chooses the cheaper valid representation:
72
+
73
+ - Keep an existing complete snapshot when it still matches the file.
74
+ - Keep a baseline plus later read diffs when that chain reconstructs the current file and costs no more than a fresh snapshot.
75
+ - Add one fresh complete snapshot when the earlier evidence is stale, broken, or more expensive.
76
+
77
+ The entire prune is skipped if any selected file fails. Every retained file must currently exist, be valid UTF-8, and fit within the 1 MiB complete-file snapshot limit. Read the complete file before selecting it for retention.
78
+
79
+ Paths are resolved from the session working directory. Paths outside it are stored as absolute paths. Existing symbolic links and other aliases are resolved before duplicate checks.
80
+
81
+ The required `relevance` text explains the agent’s selection. It does not loosen any validation rule.
82
+
83
+ ## Retaining tool exchanges
84
+
85
+ `keepToolCalls` retains both sides of an earlier exchange: the assistant’s tool call and the matching tool result. The ID must occur exactly once, the tool names must match, and the result must follow the call.
86
+
87
+ Retention is exact. Selecting one call does not retain nearby calls from the same assistant message. Tau can remove one parallel tool exchange while keeping another, then removes the empty assistant message if no content remains.
88
+
89
+ The required `relevance` text explains why the exchange matters. Tau does not use that text to infer additional exchanges to retain.
90
+
91
+ ## Deferring files
92
+
93
+ `deferFiles` does not read or preserve file contents. It adds a hidden advisory note telling the model why each file was deferred and when to reconsider it. The latest successful anchor replaces the previous deferred-file list.
94
+
95
+ A deferred path may be missing. It still participates in canonical path and duplicate checks. Its `reason` and `relevantWhen` text are passed to the model as written.
96
+
97
+ ## Automatic hints
98
+
99
+ Tau checks context usage only after a turn that produced at least one tool result. It emits at most one marker for the strongest newly crossed boundary.
100
+
101
+ With the defaults:
102
+
103
+ - `nudgeEveryPercent: 20` allows markers after enough context growth to cross 20% intervals.
104
+ - `pressurePercent: 50` makes a marker a pressure hint only when raw usage is greater than 50%.
105
+
106
+ Below the pressure threshold, the hidden instruction says no prune is required unless broad exploration has converged or substantial evidence is already irrelevant. Above it, the instruction asks the agent to finish the current coherent step and prune when the projected saving is substantial. A hint never calls the tool or bypasses its checks.
107
+
108
+ After a successful prune, the first later tool-using turn records a new usage baseline and emits no automatic marker. Tau waits for context usage to grow by another `nudgeEveryPercent` from that baseline. The baseline and crossed boundaries are stored with the active branch so compaction and branch navigation do not immediately repeat hints.
109
+
110
+ The visible marker is compact. The full instruction stays hidden, and the agent is told not to discuss internal context management.
111
+
112
+ ## Manual requests
113
+
114
+ Run `/prune` with no arguments to ask the agent to create an anchor and continue unfinished work. The command starts an agent turn immediately. It does not force a successful prune or bypass file, tool-pairing, current-message, lifecycle, or minimum-savings checks.
115
+
116
+ Extra arguments produce `Usage: /prune`. When Context Pruning is disabled, the command reports that state and does not start a prune turn.
117
+
118
+ Asking the agent to prune in ordinary chat has the same execution boundaries once it calls `context_prune`.
119
+
120
+ ## Branches, compaction, and display
121
+
122
+ Applied anchors belong to the active session branch. Switching branches rebuilds the removed-evidence set, deferred-file list, automatic-hint baseline, and warning-colored rows from that branch’s valid tool results. A skipped or malformed prune result does not become an anchor.
123
+
124
+ Context Pruning does not cancel or replace Pi’s manual, threshold, or overflow compaction. Compaction clears Tau’s saved model-input list; Tau must receive another model-input event before a later prune can use it.
125
+
126
+ Tau-owned tool rows and autoreads turn warning-colored when their evidence has been pruned. Native Pi rows and fallback rows that do not use Tau’s row-state renderer do not change color. Their evidence is still removed from model input.
14
127
 
15
128
  ## Settings
16
129
 
17
130
  Settings live under `extensions.contextPruning` in Tau settings.
18
131
 
19
- - `enabled`: enables the tool, `/prune`, automatic markers, branch replay, context projection, and pruned-row display. Defaults to `true`.
20
- - `nudgeEveryPercent`: context-growth interval between automatic markers. Defaults to `20`.
21
- - `pressurePercent`: usage above which a marker suggests pruning after the current coherent step. Defaults to `50`.
22
- - `minimumReclaimTokens`: minimum estimated saving required to apply a prune. Defaults to `8000`.
132
+ - `enabled`: enables the tool, `/prune`, automatic markers, branch replay, context filtering, and pruned-row display. Defaults to `true`.
133
+ - `nudgeEveryPercent`: integer context-growth interval between automatic hints. Allowed range: `1` through `100`. Defaults to `20`.
134
+ - `pressurePercent`: integer usage percentage above which hints use pressure wording. Allowed range: `1` through `99`. Defaults to `50`.
135
+ - `minimumReclaimTokens`: positive integer minimum estimated saving required to apply a prune. Defaults to `8000`.
@@ -10,7 +10,8 @@ export function projectContext(
10
10
  messages: readonly ContextMessage[],
11
11
  state: ActiveContextPruningState,
12
12
  ): ContextMessage[] {
13
- const inputPairs = indexToolPairs(messages);
13
+ const abandonedToolCallIds = new Set<string>();
14
+ const inputPairs = indexToolPairs(messages, abandonedToolCallIds);
14
15
  const anchorBoundary = visibleAnchorBoundary(messages, state.latestAnchorToolCallId, inputPairs);
15
16
  const prunedToolCallIds = state.latestAnchorToolCallId === undefined ? new Set<string>() : state.prunedToolCallIds;
16
17
  const prunedAutoreadRowIds =
@@ -43,7 +44,7 @@ export function projectContext(
43
44
  const content = message.content.filter((block) => {
44
45
  const remove =
45
46
  (removeThinking && block.type === "thinking") ||
46
- (block.type === "toolCall" && prunedToolCallIds.has(block.id));
47
+ (block.type === "toolCall" && (prunedToolCallIds.has(block.id) || abandonedToolCallIds.has(block.id)));
47
48
  if (remove) changed = true;
48
49
  return !remove;
49
50
  });
@@ -77,8 +78,11 @@ function visibleAnchorBoundary(
77
78
  return block?.type === "toolCall" && block.name === "context_prune" ? pair.resultIndex : undefined;
78
79
  }
79
80
 
80
- function indexToolPairs(messages: readonly ContextMessage[]): Map<string, { callIndex: number; resultIndex: number }> {
81
- const calls = new Map<string, number>();
81
+ function indexToolPairs(
82
+ messages: readonly ContextMessage[],
83
+ abandonedToolCallIds?: Set<string>,
84
+ ): Map<string, { callIndex: number; resultIndex: number }> {
85
+ const calls = new Map<string, { index: number; aborted: boolean }>();
82
86
  const results = new Map<string, number>();
83
87
  for (let index = 0; index < messages.length; index += 1) {
84
88
  const message = messages[index];
@@ -86,7 +90,7 @@ function indexToolPairs(messages: readonly ContextMessage[]): Map<string, { call
86
90
  for (const block of message.content) {
87
91
  if (block.type !== "toolCall") continue;
88
92
  if (calls.has(block.id)) throw new Error(`Duplicate tool call in projected context: ${block.id}`);
89
- calls.set(block.id, index);
93
+ calls.set(block.id, { index, aborted: message.stopReason === "aborted" });
90
94
  }
91
95
  } else if (message.role === "toolResult") {
92
96
  if (results.has(message.toolCallId))
@@ -96,10 +100,16 @@ function indexToolPairs(messages: readonly ContextMessage[]): Map<string, { call
96
100
  }
97
101
 
98
102
  const pairs = new Map<string, { callIndex: number; resultIndex: number }>();
99
- for (const [id, callIndex] of calls) {
103
+ for (const [id, call] of calls) {
100
104
  const resultIndex = results.get(id);
101
- if (resultIndex === undefined) throw new Error(`Orphaned tool call in projected context: ${id}`);
102
- pairs.set(id, { callIndex, resultIndex });
105
+ if (resultIndex === undefined) {
106
+ if (call.aborted && abandonedToolCallIds) {
107
+ abandonedToolCallIds.add(id);
108
+ continue;
109
+ }
110
+ throw new Error(`Orphaned tool call in projected context: ${id}`);
111
+ }
112
+ pairs.set(id, { callIndex: call.index, resultIndex });
103
113
  }
104
114
  for (const id of results.keys()) {
105
115
  if (!calls.has(id)) throw new Error(`Orphaned tool result in projected context: ${id}`);
@@ -18,6 +18,10 @@ Names sessions from their first request so saved sessions remain findable.
18
18
 
19
19
  Adds `/branch` to create and switch Git branches from the TUI.
20
20
 
21
+ ## cache-diagnostics
22
+
23
+ Records private prompt-cache fingerprints without storing prompt content. Run `/cache-debug` after suspicious cache misses to write a bounded investigation report under `~/.pi/agent/cache-diagnostics/reports/`.
24
+
21
25
  ## clear-screen
22
26
 
23
27
  Adds `/clear-screen` to clear terminal output without changing the session.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shanepadgett/tau-agent",
3
- "version": "0.18.1",
3
+ "version": "0.19.0",
4
4
  "description": "Tau is a custom agentic harness built with pi extensions",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -28,7 +28,7 @@
28
28
  "README.md"
29
29
  ],
30
30
  "dependencies": {
31
- "@shanepadgett/tau-tui": "0.18.1",
31
+ "@shanepadgett/tau-tui": "0.19.0",
32
32
  "@toon-format/toon": "2.3.0",
33
33
  "smol-toml": "1.7.0"
34
34
  },