@caupulican/pi-adaptative 0.80.61 → 0.80.62

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.
@@ -32,6 +32,7 @@ import { ExtensionRunner, wrapRegisteredTools, } from "./extensions/index.js";
32
32
  import { disposeExtensionEventSubscriptions } from "./extensions/loader.js";
33
33
  import { emitSessionShutdownEvent } from "./extensions/runner.js";
34
34
  import { decideDemand, ReflectionEngine, } from "./learning/reflection-engine.js";
35
+ import { EffectivenessTracker } from "./memory/effectiveness-tracker.js";
35
36
  import { MemoryManager } from "./memory/memory-manager.js";
36
37
  import { FileStoreProvider } from "./memory/providers/file-store.js";
37
38
  import { TranscriptRecallProvider } from "./memory/providers/transcript-recall.js";
@@ -120,6 +121,8 @@ export class AgentSession {
120
121
  _isExplicitThinking;
121
122
  /** Plug-and-play memory subsystem. Recreated on each (re)initialize so reload is safe. */
122
123
  _memoryManager = new MemoryManager();
124
+ /** R4: tracks whether injected recall is actually used, to adapt the recall gate. */
125
+ _effectivenessTracker = new EffectivenessTracker();
123
126
  _isChildSession;
124
127
  /** Memory providers registered by extensions via pi.registerMemoryProvider, applied on (re)init. */
125
128
  _pendingMemoryProviders = [];
@@ -971,13 +974,21 @@ export class AgentSession {
971
974
  if (t.length < 12 || t.startsWith("/"))
972
975
  return false;
973
976
  const words = t.split(/\s+/).filter((w) => w.length >= 3);
974
- return words.length >= 3;
977
+ // R4 adaptive gate: if recall has rarely been used lately (enough samples to trust the signal),
978
+ // raise the bar so we only recall on clearly substantial turns — and relax it again once recall
979
+ // starts paying off. Never fully disabled, so the loop can recover.
980
+ const recallRarelyUseful = this._effectivenessTracker.sampleCount >= 5 && this._effectivenessTracker.usefulLately() < 0.15;
981
+ return words.length >= (recallRarelyUseful ? 6 : 3);
975
982
  }
976
983
  async _promptUnserialized(text, options) {
977
984
  const expandPromptTemplates = options?.expandPromptTemplates ?? true;
978
985
  const processSlashCommands = options?.processSlashCommands ?? expandPromptTemplates;
979
986
  const preflightResult = options?.preflightResult;
980
987
  let messages;
988
+ // R4 effectiveness feedback: remember the recall page + the query so we can score, after the
989
+ // response, whether the agent actually used the recalled context.
990
+ let injectedRecall = "";
991
+ let recallQuery = "";
981
992
  try {
982
993
  // Handle extension commands first. Programmatic extension messages may opt
983
994
  // into command handling; if the agent is currently streaming, queue the
@@ -1066,6 +1077,8 @@ export class AgentSession {
1066
1077
  try {
1067
1078
  const recall = await this._memoryManager.prefetch(expandedText);
1068
1079
  if (recall) {
1080
+ injectedRecall = recall;
1081
+ recallQuery = expandedText;
1069
1082
  messages.push({ role: "user", content: [{ type: "text", text: recall }], timestamp: Date.now() });
1070
1083
  }
1071
1084
  }
@@ -1121,6 +1134,19 @@ export class AgentSession {
1121
1134
  }
1122
1135
  preflightResult?.(true);
1123
1136
  await this._runAgentPrompt(messages);
1137
+ // R4: score whether the agent actually used the recalled context, so the recall gate can adapt.
1138
+ if (injectedRecall) {
1139
+ const response = this._findLastAssistantMessage();
1140
+ const responseText = response
1141
+ ? response.content
1142
+ .filter((c) => c.type === "text")
1143
+ .map((c) => c.text)
1144
+ .join(" ")
1145
+ : "";
1146
+ if (responseText) {
1147
+ this._effectivenessTracker.recordRecallOutcome(injectedRecall, recallQuery, responseText);
1148
+ }
1149
+ }
1124
1150
  }
1125
1151
  /**
1126
1152
  * Try to execute an extension command. Returns true if command was found and executed.