@caupulican/pi-adaptative 0.81.4 → 0.81.6

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 (57) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/core/agent-session.d.ts +8 -0
  3. package/dist/core/agent-session.d.ts.map +1 -1
  4. package/dist/core/agent-session.js +125 -66
  5. package/dist/core/agent-session.js.map +1 -1
  6. package/dist/core/background-lane-controller.d.ts.map +1 -1
  7. package/dist/core/background-lane-controller.js +3 -0
  8. package/dist/core/background-lane-controller.js.map +1 -1
  9. package/dist/core/billing-failover-controller.d.ts +2 -2
  10. package/dist/core/billing-failover-controller.d.ts.map +1 -1
  11. package/dist/core/billing-failover-controller.js +2 -8
  12. package/dist/core/billing-failover-controller.js.map +1 -1
  13. package/dist/core/compaction-support.d.ts +3 -0
  14. package/dist/core/compaction-support.d.ts.map +1 -1
  15. package/dist/core/compaction-support.js +18 -4
  16. package/dist/core/compaction-support.js.map +1 -1
  17. package/dist/core/context-pipeline.d.ts +1 -0
  18. package/dist/core/context-pipeline.d.ts.map +1 -1
  19. package/dist/core/context-pipeline.js +12 -0
  20. package/dist/core/context-pipeline.js.map +1 -1
  21. package/dist/core/extensions/loader.d.ts.map +1 -1
  22. package/dist/core/extensions/loader.js +33 -9
  23. package/dist/core/extensions/loader.js.map +1 -1
  24. package/dist/core/models/local-registration.d.ts +1 -0
  25. package/dist/core/models/local-registration.d.ts.map +1 -1
  26. package/dist/core/models/local-registration.js +3 -2
  27. package/dist/core/models/local-registration.js.map +1 -1
  28. package/dist/core/research/model-fitness.d.ts +16 -0
  29. package/dist/core/research/model-fitness.d.ts.map +1 -1
  30. package/dist/core/research/model-fitness.js +74 -1
  31. package/dist/core/research/model-fitness.js.map +1 -1
  32. package/dist/core/session-tree-navigator.d.ts.map +1 -1
  33. package/dist/core/session-tree-navigator.js +1 -0
  34. package/dist/core/session-tree-navigator.js.map +1 -1
  35. package/dist/core/trust-manager.d.ts.map +1 -1
  36. package/dist/core/trust-manager.js +6 -3
  37. package/dist/core/trust-manager.js.map +1 -1
  38. package/dist/migrations.d.ts.map +1 -1
  39. package/dist/migrations.js +7 -1
  40. package/dist/migrations.js.map +1 -1
  41. package/dist/modes/interactive/interactive-mode.d.ts +2 -0
  42. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  43. package/dist/modes/interactive/interactive-mode.js +102 -91
  44. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  45. package/dist/modes/interactive/session-flow-commands.d.ts.map +1 -1
  46. package/dist/modes/interactive/session-flow-commands.js +10 -7
  47. package/dist/modes/interactive/session-flow-commands.js.map +1 -1
  48. package/docs/bug-ledger.md +24 -21
  49. package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
  50. package/examples/extensions/custom-provider-anthropic/package.json +1 -1
  51. package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
  52. package/examples/extensions/sandbox/package-lock.json +2 -2
  53. package/examples/extensions/sandbox/package.json +1 -1
  54. package/examples/extensions/with-deps/package-lock.json +2 -2
  55. package/examples/extensions/with-deps/package.json +1 -1
  56. package/npm-shrinkwrap.json +12 -12
  57. package/package.json +4 -4
@@ -589,6 +589,19 @@ export class AgentSession {
589
589
  _resolveCompactionModel(sessionModel) {
590
590
  return this._compactionSupport.resolveModel(sessionModel);
591
591
  }
592
+ /**
593
+ * One bounded diagnostic clause for compaction retry warnings: which summarizer selection won
594
+ * (and why) plus the input-size estimate the capacity check consumed — the two facts every
595
+ * gate-failure post-mortem has needed (2026-07-06 field incidents).
596
+ */
597
+ _describeCompactionSummarizer() {
598
+ const reason = this._compactionSupport.getLastSelectionReason() ?? "unresolved";
599
+ const estimate = this._pipeline.estimateCurrentContextTokens(this.agent.state.messages);
600
+ return `summarizer: ${reason}, ~${Math.ceil(estimate / 1000)}k est input`;
601
+ }
602
+ _getLastCompactionSelectionReason() {
603
+ return this._compactionSupport.getLastSelectionReason();
604
+ }
592
605
  _resolveCompactionThinkingLevel(compactionModel, sessionModel) {
593
606
  return this._compactionSupport.resolveThinkingLevel(this.thinkingLevel, compactionModel, sessionModel);
594
607
  }
@@ -1523,10 +1536,19 @@ export class AgentSession {
1523
1536
  if (!msg) {
1524
1537
  return false;
1525
1538
  }
1526
- if (this._isRetryableError(msg) && (await this._prepareRetry(msg))) {
1539
+ const classified = this._classifyAssistantError(msg);
1540
+ if (classified) {
1541
+ this._failureCorpus.record({
1542
+ provider: msg.provider,
1543
+ modelId: msg.model,
1544
+ message: msg.errorMessage ?? "",
1545
+ classified,
1546
+ });
1547
+ }
1548
+ if (classified?.retryable && (await this._prepareRetry(msg))) {
1527
1549
  return true;
1528
1550
  }
1529
- if (await this._billingFailover.handleAssistantError(msg))
1551
+ if (await this._billingFailover.handleAssistantError(msg, classified))
1530
1552
  return false;
1531
1553
  if (msg.stopReason === "error" && this._retryController.attempt > 0) {
1532
1554
  this._emit({
@@ -2129,15 +2151,17 @@ export class AgentSession {
2129
2151
  if (!this.model) {
2130
2152
  throw new Error(formatNoModelSelectedMessage());
2131
2153
  }
2132
- const compactionModel = this._resolveCompactionModel(this.model);
2133
- const compactionThinkingLevel = this._resolveCompactionThinkingLevel(compactionModel, this.model);
2134
- const { apiKey, headers } = await this._getCompactionRequestAuth(compactionModel);
2135
- const pathEntries = this.sessionManager.getBranch();
2154
+ const sessionModel = this.model;
2155
+ const selectedCompactionModel = this._resolveCompactionModel(sessionModel);
2156
+ if (this._isRawStreamSimple(this.agent.streamFn)) {
2157
+ await this._getCompactionRequestAuth(selectedCompactionModel);
2158
+ }
2159
+ const selectionReason = this._getLastCompactionSelectionReason() ?? "unknown";
2136
2160
  const settings = this._getAdaptedCompactionSettings();
2137
- const preparation = prepareCompaction(pathEntries, settings);
2138
- if (!preparation) {
2139
- // Check why we can't compact
2140
- const lastEntry = pathEntries[pathEntries.length - 1];
2161
+ const initialBranch = this.sessionManager.getBranch();
2162
+ const initialPreparation = prepareCompaction(initialBranch, settings);
2163
+ if (!initialPreparation) {
2164
+ const lastEntry = initialBranch[initialBranch.length - 1];
2141
2165
  if (lastEntry?.type === "compaction") {
2142
2166
  throw new Error("Already compacted");
2143
2167
  }
@@ -2148,69 +2172,108 @@ export class AgentSession {
2148
2172
  if (this._extensionRunner.hasHandlers("session_before_compact")) {
2149
2173
  const result = (await this._extensionRunner.emit({
2150
2174
  type: "session_before_compact",
2151
- preparation,
2152
- branchEntries: pathEntries,
2175
+ preparation: initialPreparation,
2176
+ branchEntries: initialBranch,
2153
2177
  customInstructions,
2154
2178
  signal: this._compactionAbortController.signal,
2155
2179
  }));
2156
- if (result?.cancel) {
2180
+ if (result?.cancel)
2157
2181
  throw new Error("Compaction cancelled");
2158
- }
2159
2182
  if (result?.compaction) {
2160
2183
  extensionCompaction = result.compaction;
2161
2184
  fromExtension = true;
2162
2185
  }
2163
2186
  }
2164
- let summary;
2165
- let firstKeptEntryId;
2166
- let tokensBefore;
2167
- let details;
2168
2187
  if (extensionCompaction) {
2169
- // Extension provided compaction content
2170
- summary = extensionCompaction.summary;
2171
- firstKeptEntryId = extensionCompaction.firstKeptEntryId;
2172
- tokensBefore = extensionCompaction.tokensBefore;
2173
- details = extensionCompaction.details;
2174
- }
2175
- else {
2176
- // Generate compaction result
2177
- const compactionSignal = this._compactionAbortController.signal;
2178
- const result = await this._compactWithRetry(() => compact(preparation, compactionModel, apiKey, headers, customInstructions, compactionSignal, compactionThinkingLevel, this.agent.streamFn, this._buildCompactionPreDigest()), compactionSignal, compactionModel.provider);
2179
- summary = result.summary;
2180
- firstKeptEntryId = result.firstKeptEntryId;
2181
- tokensBefore = result.tokensBefore;
2182
- details = result.details;
2183
- }
2184
- if (this._compactionAbortController.signal.aborted) {
2185
- throw new Error("Compaction cancelled");
2186
- }
2187
- this.sessionManager.appendCompaction(summary, firstKeptEntryId, tokensBefore, details, fromExtension);
2188
- const newEntries = this.sessionManager.getEntries();
2189
- const sessionContext = this.sessionManager.buildSessionContext();
2190
- this.agent.state.messages = sessionContext.messages;
2191
- // Get the saved compaction entry for the extension event
2192
- const savedCompactionEntry = newEntries.find((e) => e.type === "compaction" && e.summary === summary);
2193
- if (this._extensionRunner && savedCompactionEntry) {
2194
- await this._extensionRunner.emit({
2195
- type: "session_compact",
2196
- compactionEntry: savedCompactionEntry,
2197
- fromExtension,
2188
+ this.sessionManager.appendCompaction(extensionCompaction.summary, extensionCompaction.firstKeptEntryId, extensionCompaction.tokensBefore, extensionCompaction.details, true);
2189
+ this.agent.state.messages = this.sessionManager.buildSessionContext().messages;
2190
+ const savedCompactionEntry = this.sessionManager
2191
+ .getEntries()
2192
+ .find((entry) => entry.type === "compaction" && entry.summary === extensionCompaction.summary);
2193
+ if (this._extensionRunner && savedCompactionEntry) {
2194
+ await this._extensionRunner.emit({
2195
+ type: "session_compact",
2196
+ compactionEntry: savedCompactionEntry,
2197
+ fromExtension: true,
2198
+ });
2199
+ }
2200
+ this._emit({
2201
+ type: "compaction_end",
2202
+ reason: "manual",
2203
+ result: extensionCompaction,
2204
+ aborted: false,
2205
+ willRetry: false,
2198
2206
  });
2207
+ return extensionCompaction;
2199
2208
  }
2200
- const compactionResult = {
2201
- summary,
2202
- firstKeptEntryId,
2203
- tokensBefore,
2204
- details,
2205
- };
2209
+ let appliedResult;
2210
+ const signal = this._compactionAbortController.signal;
2211
+ const outcome = await runCompactionLoop({
2212
+ measureLiveTokens: () => Math.max(this._estimateCurrentContextTokens(this.agent.state.messages), 1),
2213
+ getTriggerThreshold: () => 0,
2214
+ getMargin: () => 0,
2215
+ getBranch: () => this.sessionManager.getBranch(),
2216
+ getBaseKeepRecentTokens: () => settings.keepRecentTokens,
2217
+ resolveModelAndAuth: async (modelTier) => {
2218
+ const model = modelTier === "cheap" ? selectedCompactionModel : sessionModel;
2219
+ const { apiKey, headers } = await this._resolveCompactionModelAndAuth(model, sessionModel);
2220
+ return { model, apiKey, headers };
2221
+ },
2222
+ summarizeAndVerify: async (params, model, apiKey, headers, branch) => {
2223
+ const preparation = prepareCompaction(branch, {
2224
+ ...settings,
2225
+ keepRecentTokens: params.keepRecentTokens,
2226
+ }, { allowTrailingCompactionAsPrevious: true });
2227
+ if (!preparation)
2228
+ throw new Error("Nothing to compact (session too small)");
2229
+ if (extensionCompaction)
2230
+ return { result: extensionCompaction };
2231
+ const compactionThinkingLevel = this._resolveCompactionThinkingLevel(model, sessionModel);
2232
+ const result = await this._compactWithRetry(() => compact(preparation, model, apiKey, headers, customInstructions, signal, compactionThinkingLevel, this.agent.streamFn, this._buildCompactionPreDigest(), { chunked: params.chunked }), signal, model.provider);
2233
+ return { result };
2234
+ },
2235
+ buildDeterministicCheckpoint: () => ({ result: createDeterministicCompaction(initialPreparation) }),
2236
+ apply: async (result) => {
2237
+ if (signal.aborted)
2238
+ throw new Error("Compaction cancelled");
2239
+ this.sessionManager.appendCompaction(result.summary, result.firstKeptEntryId, result.tokensBefore, result.details, fromExtension);
2240
+ this.agent.state.messages = this.sessionManager.buildSessionContext().messages;
2241
+ const savedCompactionEntry = this.sessionManager
2242
+ .getEntries()
2243
+ .find((entry) => entry.type === "compaction" && entry.summary === result.summary);
2244
+ if (this._extensionRunner && savedCompactionEntry) {
2245
+ await this._extensionRunner.emit({
2246
+ type: "session_compact",
2247
+ compactionEntry: savedCompactionEntry,
2248
+ fromExtension,
2249
+ });
2250
+ }
2251
+ appliedResult = result;
2252
+ },
2253
+ verifyPostApplyEffect: () => false,
2254
+ onTransition: ({ cycle, cause }) => {
2255
+ this._emit({
2256
+ type: "warning",
2257
+ message: `manual compaction cycle ${cycle}: ${cause} — retrying from step 0 (${this._describeCompactionSummarizer()})`,
2258
+ });
2259
+ },
2260
+ signal,
2261
+ });
2262
+ if (outcome.kind === "failed") {
2263
+ if (outcome.reason === "aborted")
2264
+ throw new Error("Compaction cancelled");
2265
+ throw new Error(`manual compaction failed after retry ladder using ${selectedCompactionModel.provider}/${selectedCompactionModel.id} (${selectionReason}); first failure: ${outcome.reason}`);
2266
+ }
2267
+ if (outcome.kind === "skip" || !appliedResult)
2268
+ throw new Error(outcome.kind === "skip" ? outcome.reason : "Compaction failed");
2206
2269
  this._emit({
2207
2270
  type: "compaction_end",
2208
2271
  reason: "manual",
2209
- result: compactionResult,
2272
+ result: appliedResult,
2210
2273
  aborted: false,
2211
2274
  willRetry: false,
2212
2275
  });
2213
- return compactionResult;
2276
+ return appliedResult;
2214
2277
  }
2215
2278
  catch (error) {
2216
2279
  const message = error instanceof Error ? error.message : String(error);
@@ -2463,7 +2526,7 @@ export class AgentSession {
2463
2526
  onTransition: ({ cycle, cause }) => {
2464
2527
  this._emit({
2465
2528
  type: "warning",
2466
- message: `auto-compaction cycle ${cycle}: ${cause} — retrying from step 0`,
2529
+ message: `auto-compaction cycle ${cycle}: ${cause} — retrying from step 0 (${this._describeCompactionSummarizer()})`,
2467
2530
  });
2468
2531
  },
2469
2532
  signal,
@@ -2831,22 +2894,18 @@ export class AgentSession {
2831
2894
  * are terminal; context overflow is handled by compaction, not retry. The verdict comes from the
2832
2895
  * reliability kernel's classifier, fed the host-computed context-overflow flag.
2833
2896
  */
2834
- _isRetryableError(message) {
2897
+ _classifyAssistantError(message) {
2835
2898
  if (message.stopReason !== "error" || !message.errorMessage)
2836
- return false;
2899
+ return undefined;
2837
2900
  const contextWindow = this.model?.contextWindow ?? 0;
2838
- const classified = classifyFailure({
2901
+ return classifyFailure({
2839
2902
  message: message.errorMessage,
2840
2903
  contextOverflow: isContextOverflow(message, contextWindow),
2841
2904
  provider: message.provider,
2842
2905
  });
2843
- this._failureCorpus.record({
2844
- provider: message.provider,
2845
- modelId: message.model,
2846
- message: message.errorMessage,
2847
- classified,
2848
- });
2849
- return classified.retryable;
2906
+ }
2907
+ _isRetryableError(message) {
2908
+ return this._classifyAssistantError(message)?.retryable ?? false;
2850
2909
  }
2851
2910
  /**
2852
2911
  * Prepare a retryable error for continuation with exponential backoff.