praxis-agent 0.24.1 → 0.26.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.
@@ -85,11 +85,21 @@ export declare class SessionMemoryController {
85
85
  * The persisted counters only advance once an extraction succeeds.
86
86
  */
87
87
  observeDelta(inputTokens: number, toolCalls: number, messageId: string, messages?: readonly ModelMessage[]): Promise<boolean>;
88
+ /**
89
+ * Records the observed totals and starts an extraction when the fixed
90
+ * eligibility contract is met. Returns true when an extraction is running or
91
+ * was just scheduled; callers on normal turns never await the extraction.
92
+ */
93
+ private scheduleExtraction;
88
94
  /** Safe snapshot of the loaded durable summary; empty when none exists. */
89
95
  summary(): Promise<string>;
90
96
  /** Safe snapshot of the loaded session memory state. */
91
97
  state(): Promise<SessionMemoryState>;
92
- /** Resolves when no extraction is running; rejects on failure or timeout. */
98
+ /**
99
+ * Resolves when no extraction is running; rejects on extraction failure.
100
+ * Compact callers wait softly: if extraction outlives the bounded
101
+ * `waitTimeoutMs`, this resolves so compaction can proceed anyway.
102
+ */
93
103
  waitForIdle(): Promise<void>;
94
104
  clear(): Promise<void>;
95
105
  private ensureLoaded;
@@ -2,6 +2,8 @@ import { readFile, rm } from 'node:fs/promises';
2
2
  import { join, resolve } from 'node:path';
3
3
  import { isClaudeSessionId } from '../compatibility/claude/paths.js';
4
4
  import { writeFileAtomically } from '../platform/atomic-write.js';
5
+ /** A persisted extraction this old is recovered as stale and safely re-extracted. */
6
+ const STALE_EXTRACTION_THRESHOLD_MS = 60_000;
5
7
  export class SessionMemoryStateError extends Error {
6
8
  name = 'SessionMemoryStateError';
7
9
  constructor(message) {
@@ -170,8 +172,8 @@ export class SessionMemoryController {
170
172
  this.options = options;
171
173
  this.initTokens = options.initTokens ?? 10_000;
172
174
  this.updateTokens = options.updateTokens ?? 5_000;
173
- this.updateToolCalls = options.updateToolCalls ?? 20;
174
- this.waitTimeoutMs = options.waitTimeoutMs ?? 30_000;
175
+ this.updateToolCalls = options.updateToolCalls ?? 3;
176
+ this.waitTimeoutMs = options.waitTimeoutMs ?? 15_000;
175
177
  for (const [name, value] of [
176
178
  ['initTokens', this.initTokens],
177
179
  ['updateTokens', this.updateTokens],
@@ -204,22 +206,10 @@ export class SessionMemoryController {
204
206
  toolCalls < state.lastObservedToolCalls) {
205
207
  throw new SessionMemoryStateError(`Session memory observed counters regressed (tokens ${tokens} < ${state.lastObservedTokens}, toolCalls ${toolCalls} < ${state.lastObservedToolCalls})`);
206
208
  }
207
- this.observedTokens = Math.max(this.observedTokens, tokens);
208
- this.observedToolCalls = Math.max(this.observedToolCalls, toolCalls);
209
- if (!this.isExtractionDue(this.observedTokens, this.observedToolCalls)) {
210
- return false;
211
- }
212
- if (this.inFlight === null) {
213
- const extraction = this.runExtraction(this.observedTokens, this.observedToolCalls, messageId, messages);
214
- this.inFlight = extraction;
215
- extraction
216
- .catch(() => undefined)
217
- .finally(() => {
218
- if (this.inFlight === extraction)
219
- this.inFlight = null;
220
- });
221
- }
222
- return true;
209
+ // A direct absolute observation reports a natural break when no tool calls
210
+ // have accumulated since the last successful extraction.
211
+ const naturalBreak = toolCalls === state.lastObservedToolCalls;
212
+ return this.scheduleExtraction(tokens, toolCalls, messageId, messages, naturalBreak);
223
213
  }
224
214
  /**
225
215
  * Adds non-negative deltas to the current cumulative observed totals and
@@ -241,7 +231,32 @@ export class SessionMemoryController {
241
231
  }
242
232
  this.observedTokens += inputTokens;
243
233
  this.observedToolCalls += toolCalls;
244
- return this.observe(this.observedTokens, this.observedToolCalls, messageId, messages);
234
+ // A zero-tool-call turn is a natural break: the last assistant turn made
235
+ // no tool calls.
236
+ return this.scheduleExtraction(this.observedTokens, this.observedToolCalls, messageId, messages, toolCalls === 0);
237
+ }
238
+ /**
239
+ * Records the observed totals and starts an extraction when the fixed
240
+ * eligibility contract is met. Returns true when an extraction is running or
241
+ * was just scheduled; callers on normal turns never await the extraction.
242
+ */
243
+ scheduleExtraction(tokens, toolCalls, messageId, messages, naturalBreak) {
244
+ this.observedTokens = Math.max(this.observedTokens, tokens);
245
+ this.observedToolCalls = Math.max(this.observedToolCalls, toolCalls);
246
+ if (!this.isExtractionDue(this.observedTokens, this.observedToolCalls, naturalBreak)) {
247
+ return false;
248
+ }
249
+ if (this.inFlight === null) {
250
+ const extraction = this.runExtraction(this.observedTokens, this.observedToolCalls, messageId, messages);
251
+ this.inFlight = extraction;
252
+ extraction
253
+ .catch(() => undefined)
254
+ .finally(() => {
255
+ if (this.inFlight === extraction)
256
+ this.inFlight = null;
257
+ });
258
+ }
259
+ return true;
245
260
  }
246
261
  /** Safe snapshot of the loaded durable summary; empty when none exists. */
247
262
  async summary() {
@@ -256,15 +271,19 @@ export class SessionMemoryController {
256
271
  }
257
272
  return { ...this.stateValue };
258
273
  }
259
- /** Resolves when no extraction is running; rejects on failure or timeout. */
274
+ /**
275
+ * Resolves when no extraction is running; rejects on extraction failure.
276
+ * Compact callers wait softly: if extraction outlives the bounded
277
+ * `waitTimeoutMs`, this resolves so compaction can proceed anyway.
278
+ */
260
279
  async waitForIdle() {
261
280
  await this.ensureLoaded();
262
281
  const extraction = this.inFlight;
263
282
  if (extraction === null)
264
283
  return;
265
284
  let timer;
266
- const timeout = new Promise((_, reject) => {
267
- timer = setTimeout(() => reject(new SessionMemoryTimeoutError(`Session memory extraction did not complete within ${this.waitTimeoutMs}ms`)), this.waitTimeoutMs);
285
+ const timeout = new Promise((resolve) => {
286
+ timer = setTimeout(resolve, this.waitTimeoutMs);
268
287
  });
269
288
  try {
270
289
  await Promise.race([extraction, timeout]);
@@ -307,7 +326,7 @@ export class SessionMemoryController {
307
326
  ...state,
308
327
  extractionStartedAt: null,
309
328
  extractionCompletedAt: null,
310
- extractionError: elapsed >= this.waitTimeoutMs
329
+ extractionError: elapsed >= STALE_EXTRACTION_THRESHOLD_MS
311
330
  ? `Session memory extraction is stale after ${elapsed}ms`
312
331
  : 'Session memory extraction was interrupted',
313
332
  };
@@ -321,14 +340,18 @@ export class SessionMemoryController {
321
340
  this.observedToolCalls = this.stateValue.lastObservedToolCalls;
322
341
  this.summaryValue = summary;
323
342
  }
324
- isExtractionDue(tokens, toolCalls) {
343
+ isExtractionDue(tokens, toolCalls, naturalBreak) {
325
344
  const state = this.stateValue;
326
345
  if (state === null)
327
346
  return false;
328
347
  if (!state.initialized)
329
348
  return tokens >= this.initTokens;
330
- return (tokens - state.lastObservedTokens >= this.updateTokens ||
331
- toolCalls - state.lastObservedToolCalls >= this.updateToolCalls);
349
+ // Unchanged context must not retrigger; tool-call growth alone is never
350
+ // enough without at least the update-token growth.
351
+ if (tokens - state.lastObservedTokens < this.updateTokens)
352
+ return false;
353
+ return (toolCalls - state.lastObservedToolCalls >= this.updateToolCalls ||
354
+ naturalBreak);
332
355
  }
333
356
  async runExtraction(tokens, toolCalls, messageId, messages) {
334
357
  const state = this.stateValue;
@@ -244,6 +244,13 @@ export declare class ClaudeSessionService {
244
244
  private sessionNameEntries;
245
245
  private hasSessionName;
246
246
  private sessionName;
247
+ /** Conservative selective-preservation seam for manual compact: when the
248
+ * durable session memory watermark matches an active entry, summarize the
249
+ * last good memory artifact plus the post-watermark branch and retain a
250
+ * recent suffix. Returns null to fall back to the existing full-compaction
251
+ * behavior (missing/invalid watermark, empty projection, or oversized
252
+ * projection). */
253
+ private selectMemoryPreservedCompact;
247
254
  private lastMessageUuid;
248
255
  private promptIdForToolCall;
249
256
  private assistantIdForToolCall;
@@ -13,7 +13,7 @@ import { ClaudeFileHistory } from '../compatibility/claude/file-history.js';
13
13
  import { getClaudePrLink } from '../compatibility/claude/pr-links.js';
14
14
  import { getClaudeAgentSetting, getClaudeLastPrompt, projectClaudeDisplayTranscript, projectClaudeModelMessages, } from '../compatibility/claude/projection.js';
15
15
  import { selectClaudeSchemaAdapter, } from '../compatibility/claude/schema.js';
16
- import { findUnresolvedClaudeToolCalls } from '../compatibility/claude/tool-links.js';
16
+ import { findUnresolvedClaudeToolCalls, getClaudeContentBlocks, } from '../compatibility/claude/tool-links.js';
17
17
  import { createClaudeAgentSettingEntry, createClaudeHookAttachmentEntries, createClaudeLastPromptEntry, createClaudeRuleAttachmentEntry, translateProviderEvents, } from '../compatibility/claude/translation.js';
18
18
  import { AgentRunCancelledError, AgentRuntime, } from '../core/runtime.js';
19
19
  import { BackgroundTaskRuntime, } from './background-task-runtime.js';
@@ -302,6 +302,114 @@ Update the durable session memory from the conversation so far. Preserve:
302
302
  Omit transient chatter, credentials, secrets, or personal data.
303
303
 
304
304
  Return ONLY an updated Markdown document that becomes the session's durable memory. Do not call tools. Do not include any prose outside the Markdown document.`;
305
+ /** Memory-anchored manual compact keeps a recent suffix of at least five
306
+ * text-bearing messages and approximately ten thousand tokens when the active
307
+ * branch can provide them. Larger suffixes stay conservative. */
308
+ const MEMORY_COMPACT_MIN_SUFFIX_MESSAGES = 5;
309
+ const MEMORY_COMPACT_MIN_SUFFIX_TOKENS = 10_000;
310
+ /** Above this estimated compactor projection the selective path falls back to
311
+ * the existing full-compaction behavior instead of building an oversized
312
+ * projection. */
313
+ const MEMORY_COMPACT_MAX_PROJECTION_TOKENS = 40_000;
314
+ function isTextBearingClaudeEntry(entry) {
315
+ if (typeof entry.message !== 'object' ||
316
+ entry.message === null ||
317
+ Array.isArray(entry.message)) {
318
+ return false;
319
+ }
320
+ const message = entry.message;
321
+ const content = message.content;
322
+ if (message.role === 'user') {
323
+ if (typeof content === 'string')
324
+ return content.trim().length > 0;
325
+ if (!Array.isArray(content))
326
+ return false;
327
+ return content.some((block) => typeof block === 'object' &&
328
+ block !== null &&
329
+ (block.type === 'text' ||
330
+ block.type === 'tool_result' ||
331
+ block.type === 'image' ||
332
+ block.type === 'document'));
333
+ }
334
+ if (message.role === 'assistant') {
335
+ if (!Array.isArray(content))
336
+ return false;
337
+ return content.some((block) => typeof block === 'object' &&
338
+ block !== null &&
339
+ (block.type === 'text' ||
340
+ block.type === 'tool_use' ||
341
+ block.type === 'thinking'));
342
+ }
343
+ return false;
344
+ }
345
+ function claudeToolResultIds(entry) {
346
+ const ids = new Set();
347
+ if (entry.type !== 'user')
348
+ return ids;
349
+ for (const block of getClaudeContentBlocks(entry)) {
350
+ if (block.type === 'tool_result' && typeof block.tool_use_id === 'string') {
351
+ ids.add(block.tool_use_id);
352
+ }
353
+ }
354
+ return ids;
355
+ }
356
+ function claudeAssistantHasToolUse(entry, ids) {
357
+ if (entry.type !== 'assistant')
358
+ return false;
359
+ return getClaudeContentBlocks(entry).some((block) => block.type === 'tool_use' &&
360
+ typeof block.id === 'string' &&
361
+ ids.has(block.id));
362
+ }
363
+ /** Pulls the preserved-suffix boundary backward so a tool_use assistant entry
364
+ * and its tool_result user entry stay siblings: the suffix never opens with an
365
+ * orphaned tool_result and the compacted input never ends with a dangling
366
+ * tool_use. Same-response thinking/tool blocks live in one assistant entry,
367
+ * so entry-level cutting never splits them. */
368
+ function completeClaudeToolPairBoundary(activeEntries, startIndex) {
369
+ let boundary = startIndex;
370
+ while (boundary < activeEntries.length) {
371
+ const entry = activeEntries[boundary];
372
+ if (!entry)
373
+ break;
374
+ const ids = claudeToolResultIds(entry);
375
+ if (ids.size === 0)
376
+ break;
377
+ let extended = false;
378
+ for (let index = boundary - 1; index >= 0; index -= 1) {
379
+ const candidate = activeEntries[index];
380
+ if (candidate && claudeAssistantHasToolUse(candidate, ids)) {
381
+ boundary = index;
382
+ extended = true;
383
+ break;
384
+ }
385
+ }
386
+ if (!extended)
387
+ break;
388
+ }
389
+ return boundary;
390
+ }
391
+ /** Walks back from the active tail to the inclusive start of the recent suffix
392
+ * retained verbatim, then completes sibling adjacency at the boundary. */
393
+ function memoryPreservedSuffixStart(activeEntries) {
394
+ let start = activeEntries.length;
395
+ let textBearing = 0;
396
+ let estimatedTokens = 0;
397
+ for (let index = activeEntries.length - 1; index >= 0; index -= 1) {
398
+ const entry = activeEntries[index];
399
+ if (!entry)
400
+ continue;
401
+ start = index;
402
+ if (!isTextBearingClaudeEntry(entry))
403
+ continue;
404
+ textBearing += 1;
405
+ estimatedTokens += estimateModelRequestTokens(projectClaudeModelMessages([entry]));
406
+ if (textBearing >= MEMORY_COMPACT_MIN_SUFFIX_MESSAGES &&
407
+ (estimatedTokens >= MEMORY_COMPACT_MIN_SUFFIX_TOKENS || index === 0)) {
408
+ break;
409
+ }
410
+ }
411
+ return completeClaudeToolPairBoundary(activeEntries, start);
412
+ }
305
413
  function validPromptSuggestion(value) {
306
414
  const suggestion = value.trim();
307
415
  if (!suggestion)
@@ -871,7 +979,7 @@ export class ClaudeSessionService {
871
979
  ...(onDelta ? { onTextDelta: onDelta } : {}),
872
980
  onMetrics: (recorded) => this.recordAuxiliaryMetrics(activeSessionId, recorded),
873
981
  });
874
- budget?.observeUsage(metrics.usage);
982
+ budget?.observeUsage(metrics.usage, messages);
875
983
  if (metrics.toolCalls.length > 0) {
876
984
  throw new Error('Side questions cannot call tools; press f to fork');
877
985
  }
@@ -1464,6 +1572,7 @@ export class ClaudeSessionService {
1464
1572
  let selectedEntries = activeEntries;
1465
1573
  let logicalParentUuid = this.lastMessageUuid(activeEntries);
1466
1574
  let preservedEntries = [];
1575
+ let memoryMessage;
1467
1576
  if (selection) {
1468
1577
  const targetIndex = activeEntries.findIndex((entry) => entry.uuid === selection.messageId);
1469
1578
  if (targetIndex < 0) {
@@ -1486,8 +1595,18 @@ export class ClaudeSessionService {
1486
1595
  : this.lastMessageUuid(selectedEntries);
1487
1596
  }
1488
1597
  }
1598
+ else {
1599
+ const memorySelection = await this.selectMemoryPreservedCompact(sessionId, activeEntries);
1600
+ if (memorySelection) {
1601
+ selectedEntries = [...memorySelection.compactedEntries];
1602
+ preservedEntries = [...memorySelection.preservedEntries];
1603
+ logicalParentUuid = memorySelection.logicalParentUuid;
1604
+ memoryMessage = memorySelection.memoryMessage;
1605
+ }
1606
+ }
1489
1607
  const selectedMessages = projectClaudeModelMessages(selectedEntries);
1490
1608
  const messages = [
1609
+ ...(memoryMessage === undefined ? [] : [memoryMessage]),
1491
1610
  ...selectedMessages,
1492
1611
  ...(selection?.context
1493
1612
  ? [
@@ -1588,7 +1707,11 @@ export class ClaudeSessionService {
1588
1707
  },
1589
1708
  preservedUuids: preservedEntries.flatMap((entry) => typeof entry.uuid === 'string' ? [entry.uuid] : []),
1590
1709
  }
1591
- : {}),
1710
+ : memoryMessage
1711
+ ? {
1712
+ preservedUuids: preservedEntries.flatMap((entry) => typeof entry.uuid === 'string' ? [entry.uuid] : []),
1713
+ }
1714
+ : {}),
1592
1715
  createUuid: () => uuids.shift() ?? randomUUID(),
1593
1716
  });
1594
1717
  const appendResult = await lease.appendMany(snapshot.tail, entries);
@@ -3051,7 +3174,7 @@ export class ClaudeSessionService {
3051
3174
  : undefined) ??
3052
3175
  Object.values(result.modelUsage ?? {})[0] ??
3053
3176
  result.usage;
3054
- budget?.observeUsage(observedUsage);
3177
+ budget?.observeUsage(observedUsage, runtimeRequest.messages, definitions);
3055
3178
  if (structuredCapture && structuredCapture.calls !== 1) {
3056
3179
  throw new Error(`StructuredOutput must be called exactly once (received ${structuredCapture.calls})`);
3057
3180
  }
@@ -3148,17 +3271,20 @@ export class ClaudeSessionService {
3148
3271
  }
3149
3272
  if (sessionMemory && finalLeafUuid) {
3150
3273
  const turnInputTokens = result.usage?.inputTokens ?? 0;
3274
+ const warn = (error) => this.options.eventSink?.({
3275
+ type: 'warning',
3276
+ message: `Session memory extraction failed: ${error instanceof Error ? error.message : String(error)}`,
3277
+ });
3151
3278
  try {
3152
3279
  await sessionMemory.observeDelta(turnInputTokens, currentTurnToolCalls, finalLeafUuid, projectClaudeModelMessages(snapshot.entries));
3153
- await sessionMemory.waitForIdle();
3280
+ // Normal turns schedule extraction without awaiting it; failures
3281
+ // surface as a warning while the sidecar stays retryable.
3282
+ sessionMemory.waitForIdle().catch(warn);
3154
3283
  }
3155
3284
  catch (error) {
3156
- // A failed extraction must not fail the user turn; the sidecar
3285
+ // A failed observation must not fail the user turn; the sidecar
3157
3286
  // retains a retryable error for the next observation.
3158
- this.options.eventSink?.({
3159
- type: 'warning',
3160
- message: `Session memory extraction failed: ${error instanceof Error ? error.message : String(error)}`,
3161
- });
3287
+ warn(error);
3162
3288
  }
3163
3289
  }
3164
3290
  turnCompleted = true;
@@ -3276,6 +3402,59 @@ export class ClaudeSessionService {
3276
3402
  return agentName;
3277
3403
  return null;
3278
3404
  }
3405
+ /** Conservative selective-preservation seam for manual compact: when the
3406
+ * durable session memory watermark matches an active entry, summarize the
3407
+ * last good memory artifact plus the post-watermark branch and retain a
3408
+ * recent suffix. Returns null to fall back to the existing full-compaction
3409
+ * behavior (missing/invalid watermark, empty projection, or oversized
3410
+ * projection). */
3411
+ async selectMemoryPreservedCompact(sessionId, activeEntries) {
3412
+ const controller = this.sessionMemoryController(sessionId);
3413
+ if (controller === null)
3414
+ return null;
3415
+ let watermark = null;
3416
+ let memorySummary = '';
3417
+ try {
3418
+ const [state, summary] = await Promise.all([
3419
+ controller.state(),
3420
+ controller.summary(),
3421
+ ]);
3422
+ watermark = state.lastSummarizedMessageId;
3423
+ memorySummary = summary;
3424
+ }
3425
+ catch {
3426
+ return null;
3427
+ }
3428
+ if (watermark === null || watermark.length === 0)
3429
+ return null;
3430
+ if (memorySummary.trim().length === 0)
3431
+ return null;
3432
+ const watermarkIndex = activeEntries.findIndex((entry) => entry.uuid === watermark);
3433
+ if (watermarkIndex < 0)
3434
+ return null;
3435
+ const suffixStart = memoryPreservedSuffixStart(activeEntries);
3436
+ if (suffixStart <= 0 || suffixStart <= watermarkIndex + 1)
3437
+ return null;
3438
+ const compactedEntries = activeEntries.slice(watermarkIndex + 1, suffixStart);
3439
+ const preservedEntries = activeEntries.slice(suffixStart);
3440
+ const selectedMessages = projectClaudeModelMessages(compactedEntries);
3441
+ if (selectedMessages.length === 0)
3442
+ return null;
3443
+ const memoryMessage = { role: 'user', content: memorySummary };
3444
+ if (estimateModelRequestTokens([memoryMessage, ...selectedMessages]) >
3445
+ MEMORY_COMPACT_MAX_PROJECTION_TOKENS) {
3446
+ return null;
3447
+ }
3448
+ const logicalParentUuid = this.lastMessageUuid(activeEntries.slice(0, suffixStart));
3449
+ if (logicalParentUuid === null)
3450
+ return null;
3451
+ return {
3452
+ compactedEntries,
3453
+ preservedEntries,
3454
+ memoryMessage,
3455
+ logicalParentUuid,
3456
+ };
3457
+ }
3279
3458
  lastMessageUuid(entries) {
3280
3459
  for (let index = entries.length - 1; index >= 0; index -= 1) {
3281
3460
  const entry = entries[index];
@@ -3852,6 +4031,10 @@ export class ClaudeSessionService {
3852
4031
  return new ContextBudget({
3853
4032
  contextWindowTokens,
3854
4033
  windowSource: 'capability',
4034
+ onAccountingDiagnostic: (message) => this.options.eventSink?.({
4035
+ type: 'warning',
4036
+ message: `Context usage accounting: ${message}`,
4037
+ }),
3855
4038
  ...(this.options.contextReserveTokens === undefined
3856
4039
  ? {}
3857
4040
  : { reserveTokens: this.options.contextReserveTokens }),
@@ -1461,6 +1461,10 @@ export class ClaudeSubagentExecutor {
1461
1461
  const contextBudget = options.provider.capabilities.contextWindowTokens
1462
1462
  ? new ContextBudget({
1463
1463
  contextWindowTokens: options.provider.capabilities.contextWindowTokens,
1464
+ onAccountingDiagnostic: (message) => this.options.eventSink?.({
1465
+ type: 'warning',
1466
+ message: `Context usage accounting: ${message}`,
1467
+ }),
1464
1468
  ...(this.options.contextReserveTokens === undefined
1465
1469
  ? {}
1466
1470
  : { reserveTokens: this.options.contextReserveTokens }),
@@ -1469,6 +1473,7 @@ export class ClaudeSubagentExecutor {
1469
1473
  const definitions = options.provider.capabilities.tools
1470
1474
  ? runtimeTools.definitions()
1471
1475
  : [];
1476
+ let observedMessages;
1472
1477
  const assembleMessages = async () => {
1473
1478
  const assembledContext = await this.options.contextAssembler?.assemble({
1474
1479
  cwd,
@@ -1479,6 +1484,7 @@ export class ClaudeSubagentExecutor {
1479
1484
  ...injectFirstUserMessageContext(projectClaudeModelMessages(snapshot.entries), assembledContext?.firstUserMessageContext),
1480
1485
  ...preloadedSkills,
1481
1486
  ];
1487
+ observedMessages = messages;
1482
1488
  if (contextBudget) {
1483
1489
  contextBudget.assertFits(contextBudget.evaluate(messages, definitions));
1484
1490
  }
@@ -1547,6 +1553,7 @@ export class ClaudeSubagentExecutor {
1547
1553
  : {}),
1548
1554
  ...(options.signal ? { signal: options.signal } : {}),
1549
1555
  });
1556
+ contextBudget?.observeUsage(result.usage, observedMessages ?? [], definitions);
1550
1557
  this.options.eventSink?.({
1551
1558
  type: 'task-progress',
1552
1559
  taskId: options.agentId,
@@ -181,8 +181,10 @@ export function createClaudeNativeFork({ source, sourceSessionId, sessionId, res
181
181
  (metadata.direction === 'from' ||
182
182
  metadata.direction === 'up_to'));
183
183
  });
184
+ const hasCompactHistory = source.some((entry) => entry.isCompactSummary === true ||
185
+ (entry.type === 'system' && entry.subtype === 'compact_boundary'));
184
186
  const activeSource = resumeSessionAt === undefined
185
- ? hasSelectiveSummary
187
+ ? hasCompactHistory || hasSelectiveSummary
186
188
  ? selectClaudeActiveTranscript(source)
187
189
  : source
188
190
  : selectClaudeTranscriptAtMessage(source, resumeSessionAt);
@@ -18,7 +18,11 @@ function latestLeafUuid(entries) {
18
18
  entry.isSidechain !== true);
19
19
  if (!hasNewDescendant)
20
20
  return summary.uuid;
21
- break;
21
+ // After compaction the active leaf continues from the boundary's logical
22
+ // parent. Ignore unrelated physical entries that do not descend from the
23
+ // boundary and keep the compact summary as the leaf when none do.
24
+ const continuation = latestCompactBranchLeafUuid(entries, index + 1, boundary ? entryUuid(boundary) : null, summary.uuid);
25
+ return continuation === null ? summary.uuid : continuation;
22
26
  }
23
27
  for (let index = entries.length - 1; index >= 0; index -= 1) {
24
28
  const entry = entries[index];
@@ -33,6 +37,45 @@ function latestLeafUuid(entries) {
33
37
  }
34
38
  return null;
35
39
  }
40
+ function latestCompactBranchLeafUuid(entries, fromIndex, boundaryUuid, summaryUuid) {
41
+ const byUuid = new Map();
42
+ for (const entry of entries) {
43
+ const uuid = entryUuid(entry);
44
+ if (uuid && entry.isSidechain !== true)
45
+ byUuid.set(uuid, entry);
46
+ }
47
+ for (let index = entries.length - 1; index >= fromIndex; index -= 1) {
48
+ const entry = entries[index];
49
+ if (!entry || entry.isSidechain === true)
50
+ continue;
51
+ const candidate = entry.type === 'last-prompt' && typeof entry.leafUuid === 'string'
52
+ ? entry.leafUuid
53
+ : entryUuid(entry);
54
+ if (candidate === null)
55
+ continue;
56
+ let uuid = candidate;
57
+ const seen = new Set();
58
+ while (uuid !== null) {
59
+ if (uuid === summaryUuid || uuid === boundaryUuid)
60
+ return candidate;
61
+ if (seen.has(uuid))
62
+ break;
63
+ seen.add(uuid);
64
+ const node = byUuid.get(uuid);
65
+ if (!node)
66
+ break;
67
+ uuid =
68
+ node.type === 'system' &&
69
+ node.subtype === 'compact_boundary' &&
70
+ typeof node.logicalParentUuid === 'string'
71
+ ? node.logicalParentUuid
72
+ : typeof node.parentUuid === 'string'
73
+ ? node.parentUuid
74
+ : null;
75
+ }
76
+ }
77
+ return null;
78
+ }
36
79
  function ancestryUuids(entries, leafUuid) {
37
80
  const byUuid = new Map();
38
81
  for (const entry of entries) {
@@ -72,6 +115,9 @@ function selectAncestry(entries, leafUuid) {
72
115
  const uuid = entryUuid(entry);
73
116
  if (uuid) {
74
117
  return (active.has(uuid) ||
118
+ (entry.type === 'attachment' &&
119
+ typeof entry.parentUuid === 'string' &&
120
+ active.has(entry.parentUuid)) ||
75
121
  (entry.type === 'user' &&
76
122
  typeof entry.sourceToolAssistantUUID === 'string' &&
77
123
  active.has(entry.sourceToolAssistantUUID)));
@@ -5,6 +5,9 @@ export interface ContextBudgetOptions {
5
5
  /** Declares whether the configured window came from a provider capability so
6
6
  * reports can distinguish provider-derived decisions from estimates. */
7
7
  windowSource?: 'capability' | 'estimate';
8
+ /** Receives at most one bounded diagnostic when provider accounting is
9
+ * malformed and the deterministic estimate fallback is used. */
10
+ onAccountingDiagnostic?: (message: string) => void;
8
11
  }
9
12
  export interface ContextBudgetEvaluateOptions {
10
13
  /** Most recent provider usage observation; a positive `contextWindow` is
@@ -18,6 +21,11 @@ export interface ContextBudgetEvaluateOptions {
18
21
  export type ContextBudgetSource = 'provider' | 'capability' | 'estimate';
19
22
  export interface ContextBudgetReport {
20
23
  estimatedTokens: number;
24
+ /** Total context occupancy used for overflow accounting: the actual provider
25
+ * input/cache tokens at the observation watermark plus deterministic
26
+ * estimated tokens added after that watermark. Without a usable watermark
27
+ * this equals `estimatedTokens`. */
28
+ occupancyTokens: number;
21
29
  contextWindowTokens: number;
22
30
  reserveTokens: number;
23
31
  availableTokens: number;
@@ -37,10 +45,29 @@ export declare class ContextBudget {
37
45
  readonly reserveTokens: number;
38
46
  readonly windowSource: 'capability' | 'estimate';
39
47
  private observedUsage;
48
+ /** Actual provider input/cache tokens at the most recent usable observation;
49
+ * the watermark that anchors later occupancy. */
50
+ private watermarkActualInputTokens;
51
+ /** Deterministic estimate of the request snapshot captured at observation
52
+ * time; only growth beyond this baseline is added to the watermark. */
53
+ private watermarkBaselineEstimate;
54
+ private accountingDiagnosticEmitted;
55
+ private readonly onAccountingDiagnostic;
40
56
  constructor(options: ContextBudgetOptions);
41
- observeUsage(usage: ModelUsage): void;
57
+ /** Record a completed provider request: `usage` carries the actual token
58
+ * counts and `messages`/`tools` are the exact snapshot used for that
59
+ * request. The snapshot's deterministic estimate becomes the watermark
60
+ * baseline so later evaluations add only post-watermark growth. Malformed
61
+ * usage is ignored (fail-open) and never throws; a valid `contextWindow`
62
+ * still updates the effective window through `observedUsage`. */
63
+ observeUsage(usage: ModelUsage, messages?: readonly ModelMessage[], tools?: readonly ModelToolDefinition[]): void;
42
64
  effectiveContextWindow(usage?: ModelUsage): number;
43
65
  evaluate(messages: readonly ModelMessage[], tools?: readonly ModelToolDefinition[], options?: ContextBudgetEvaluateOptions): ContextBudgetReport;
66
+ /** Occupancy anchored at the actual input/cache watermark, adding only the
67
+ * deterministic estimated growth past the observation baseline. Without a
68
+ * usable watermark this is the plain estimate fallback. */
69
+ private anchoredOccupancyTokens;
70
+ private emitAccountingDiagnostic;
44
71
  assertFits(report: ContextBudgetReport): void;
45
72
  /** Returns the positive provider-reported context window, if any. */
46
73
  private providerContextWindow;
@@ -74,11 +74,38 @@ export function estimateModelRequestTokens(messages, tools = []) {
74
74
  estimateTextTokens(JSON.stringify(tool.inputSchema)), 0);
75
75
  return messageTokens + toolTokens;
76
76
  }
77
+ /** Normalized provider input occupancy counting input and cache-read/creation
78
+ * fields without output tokens. Returns `undefined` for malformed, negative,
79
+ * or non-safe usage so accounting fails open. */
80
+ function normalizedInputAndCacheTokens(usage) {
81
+ if (!Number.isSafeInteger(usage.inputTokens) ||
82
+ usage.inputTokens < 0 ||
83
+ (usage.cacheReadInputTokens !== undefined &&
84
+ (!Number.isSafeInteger(usage.cacheReadInputTokens) ||
85
+ usage.cacheReadInputTokens < 0)) ||
86
+ (usage.cacheCreationInputTokens !== undefined &&
87
+ (!Number.isSafeInteger(usage.cacheCreationInputTokens) ||
88
+ usage.cacheCreationInputTokens < 0))) {
89
+ return undefined;
90
+ }
91
+ const candidate = (usage.inputTokens ?? 0) +
92
+ (usage.cacheReadInputTokens ?? 0) +
93
+ (usage.cacheCreationInputTokens ?? 0);
94
+ return Number.isSafeInteger(candidate) ? candidate : undefined;
95
+ }
77
96
  export class ContextBudget {
78
97
  contextWindowTokens;
79
98
  reserveTokens;
80
99
  windowSource;
81
100
  observedUsage;
101
+ /** Actual provider input/cache tokens at the most recent usable observation;
102
+ * the watermark that anchors later occupancy. */
103
+ watermarkActualInputTokens;
104
+ /** Deterministic estimate of the request snapshot captured at observation
105
+ * time; only growth beyond this baseline is added to the watermark. */
106
+ watermarkBaselineEstimate;
107
+ accountingDiagnosticEmitted = false;
108
+ onAccountingDiagnostic;
82
109
  constructor(options) {
83
110
  requirePositiveInteger(options.contextWindowTokens, 'Context window tokens');
84
111
  const defaultReserve = Math.min(8192, Math.max(1, Math.floor(options.contextWindowTokens / 10)));
@@ -90,15 +117,32 @@ export class ContextBudget {
90
117
  this.contextWindowTokens = options.contextWindowTokens;
91
118
  this.reserveTokens = reserveTokens;
92
119
  this.windowSource = options.windowSource ?? 'estimate';
120
+ this.onAccountingDiagnostic = options.onAccountingDiagnostic;
93
121
  }
94
- observeUsage(usage) {
122
+ /** Record a completed provider request: `usage` carries the actual token
123
+ * counts and `messages`/`tools` are the exact snapshot used for that
124
+ * request. The snapshot's deterministic estimate becomes the watermark
125
+ * baseline so later evaluations add only post-watermark growth. Malformed
126
+ * usage is ignored (fail-open) and never throws; a valid `contextWindow`
127
+ * still updates the effective window through `observedUsage`. */
128
+ observeUsage(usage, messages = [], tools = []) {
95
129
  this.observedUsage = usage;
130
+ const actualInputTokens = normalizedInputAndCacheTokens(usage);
131
+ if (actualInputTokens === undefined) {
132
+ this.emitAccountingDiagnostic();
133
+ return;
134
+ }
135
+ if (messages.length === 0 && tools.length === 0)
136
+ return;
137
+ this.watermarkActualInputTokens = actualInputTokens;
138
+ this.watermarkBaselineEstimate = estimateModelRequestTokens(messages, tools);
96
139
  }
97
140
  effectiveContextWindow(usage) {
98
141
  return this.providerContextWindow(usage) ?? this.contextWindowTokens;
99
142
  }
100
143
  evaluate(messages, tools = [], options = {}) {
101
144
  const estimatedTokens = estimateModelRequestTokens(messages, tools);
145
+ const occupancyTokens = this.anchoredOccupancyTokens(estimatedTokens);
102
146
  const providerWindow = this.providerContextWindow(options.lastUsage);
103
147
  const contextWindowTokens = providerWindow ?? this.contextWindowTokens;
104
148
  const outputTokens = options.outputTokens !== undefined &&
@@ -107,10 +151,11 @@ export class ContextBudget {
107
151
  ? options.outputTokens
108
152
  : 0;
109
153
  const availableTokens = Math.max(0, contextWindowTokens - this.reserveTokens);
110
- const overflowTokens = Math.max(0, estimatedTokens + outputTokens - availableTokens);
154
+ const overflowTokens = Math.max(0, occupancyTokens + outputTokens - availableTokens);
111
155
  const shouldCompact = options.promptTooLong === true || overflowTokens > 0;
112
156
  return {
113
157
  estimatedTokens,
158
+ occupancyTokens,
114
159
  contextWindowTokens,
115
160
  reserveTokens: this.reserveTokens,
116
161
  availableTokens,
@@ -119,6 +164,29 @@ export class ContextBudget {
119
164
  source: providerWindow === undefined ? this.windowSource : 'provider',
120
165
  };
121
166
  }
167
+ /** Occupancy anchored at the actual input/cache watermark, adding only the
168
+ * deterministic estimated growth past the observation baseline. Without a
169
+ * usable watermark this is the plain estimate fallback. */
170
+ anchoredOccupancyTokens(estimatedTokens) {
171
+ if (this.watermarkActualInputTokens === undefined ||
172
+ this.watermarkBaselineEstimate === undefined) {
173
+ return estimatedTokens;
174
+ }
175
+ const growthAfterWatermark = Math.max(0, estimatedTokens - this.watermarkBaselineEstimate);
176
+ return this.watermarkActualInputTokens + growthAfterWatermark;
177
+ }
178
+ emitAccountingDiagnostic() {
179
+ if (this.accountingDiagnosticEmitted)
180
+ return;
181
+ this.accountingDiagnosticEmitted = true;
182
+ try {
183
+ this.onAccountingDiagnostic?.('Provider input usage was malformed; using deterministic context estimates.');
184
+ }
185
+ catch {
186
+ // Diagnostics are strictly best-effort. A broken sink must never turn
187
+ // fail-open accounting into a healthy-turn failure.
188
+ }
189
+ }
122
190
  assertFits(report) {
123
191
  if (report.shouldCompact)
124
192
  throw new ContextOverflowError(report);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "praxis-agent",
3
- "version": "0.24.1",
3
+ "version": "0.26.0",
4
4
  "description": "Local-first, single-user general agent for the command line.",
5
5
  "license": "MIT",
6
6
  "author": "wuqisen",