@caupulican/pi-adaptative 0.80.25 → 0.80.26

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.
@@ -25,6 +25,7 @@ import { calculateContextTokens, collectEntriesForBranchSummary, compact, estima
25
25
  import { DEFAULT_THINKING_LEVEL } from "./defaults.js";
26
26
  import { exportSessionToHtml } from "./export-html/index.js";
27
27
  import { createToolHtmlRenderer } from "./export-html/tool-renderer.js";
28
+ import { createCoreDiagnosticsToolDefinitions } from "./extensions/builtin.js";
28
29
  import { ExtensionRunner, wrapRegisteredTools, } from "./extensions/index.js";
29
30
  import { emitSessionShutdownEvent } from "./extensions/runner.js";
30
31
  import { compactToolResultDetailsForRetention } from "./message-retention.js";
@@ -2113,6 +2114,55 @@ export class AgentSession {
2113
2114
  }
2114
2115
  this.setActiveToolsByName([...new Set(nextActiveToolNames)]);
2115
2116
  }
2117
+ _createReloadRuntimeSnapshot() {
2118
+ return {
2119
+ extensionRunner: this._extensionRunner,
2120
+ baseToolDefinitions: this._baseToolDefinitions,
2121
+ toolRegistry: this._toolRegistry,
2122
+ toolDefinitions: this._toolDefinitions,
2123
+ toolPromptSnippets: this._toolPromptSnippets,
2124
+ toolPromptGuidelines: this._toolPromptGuidelines,
2125
+ agentTools: this.agent.state.tools,
2126
+ agentSystemPrompt: this.agent.state.systemPrompt,
2127
+ baseSystemPrompt: this._baseSystemPrompt,
2128
+ };
2129
+ }
2130
+ _restoreReloadRuntimeSnapshot(snapshot) {
2131
+ this._extensionRunner = snapshot.extensionRunner;
2132
+ this._baseToolDefinitions = snapshot.baseToolDefinitions;
2133
+ this._toolRegistry = snapshot.toolRegistry;
2134
+ this._toolDefinitions = snapshot.toolDefinitions;
2135
+ this._toolPromptSnippets = snapshot.toolPromptSnippets;
2136
+ this._toolPromptGuidelines = snapshot.toolPromptGuidelines;
2137
+ this.agent.state.tools = snapshot.agentTools;
2138
+ this.agent.state.systemPrompt = snapshot.agentSystemPrompt;
2139
+ this._baseSystemPrompt = snapshot.baseSystemPrompt;
2140
+ if (this._extensionRunnerRef) {
2141
+ this._extensionRunnerRef.current = snapshot.extensionRunner;
2142
+ }
2143
+ this._applyExtensionBindings(snapshot.extensionRunner);
2144
+ }
2145
+ _doctorReloadRuntime() {
2146
+ const extensionErrors = this._resourceLoader.getExtensions().errors;
2147
+ if (extensionErrors.length > 0) {
2148
+ const summary = extensionErrors
2149
+ .slice(0, 6)
2150
+ .map((error) => `${error.path}: ${error.error}`)
2151
+ .join("; ");
2152
+ throw new Error(`Extension reload failed doctor: ${summary}`);
2153
+ }
2154
+ const missingActiveTools = this.getActiveToolNames().filter((name) => !this._toolRegistry.has(name));
2155
+ if (missingActiveTools.length > 0) {
2156
+ throw new Error(`Extension reload failed doctor: active tool(s) missing after reload: ${missingActiveTools.join(", ")}`);
2157
+ }
2158
+ for (const tool of this.agent.state.tools) {
2159
+ if (!this._toolDefinitions.has(tool.name)) {
2160
+ throw new Error(`Extension reload failed doctor: tool ${tool.name} missing from definition registry`);
2161
+ }
2162
+ }
2163
+ this._createAgentContextSnapshot();
2164
+ this.getContextUsage();
2165
+ }
2116
2166
  _buildRuntime(options) {
2117
2167
  const autoResizeImages = this.settingsManager.getImageAutoResize();
2118
2168
  const shellCommandPrefix = this.settingsManager.getShellCommandPrefix();
@@ -2127,6 +2177,11 @@ export class AgentSession {
2127
2177
  bash: { commandPrefix: shellCommandPrefix, shellPath },
2128
2178
  });
2129
2179
  this._baseToolDefinitions = new Map(Object.entries(baseToolDefinitions).map(([name, tool]) => [name, tool]));
2180
+ if (!this._baseToolsOverride) {
2181
+ for (const definition of createCoreDiagnosticsToolDefinitions(() => this.getActiveToolNames(), () => this.getAllTools())) {
2182
+ this._baseToolDefinitions.set(definition.name, definition);
2183
+ }
2184
+ }
2130
2185
  const extensionsResult = this._resourceLoader.getExtensions();
2131
2186
  if (options.flagValues) {
2132
2187
  for (const [name, value] of options.flagValues) {
@@ -2141,7 +2196,7 @@ export class AgentSession {
2141
2196
  this._applyExtensionBindings(this._extensionRunner);
2142
2197
  const defaultActiveToolNames = this._baseToolsOverride
2143
2198
  ? Object.keys(this._baseToolsOverride)
2144
- : ["read", "bash", "edit", "write"];
2199
+ : ["read", "bash", "edit", "write", "context_audit"];
2145
2200
  const baseActiveToolNames = options.activeToolNames ?? defaultActiveToolNames;
2146
2201
  this._refreshToolRegistry({
2147
2202
  activeToolNames: baseActiveToolNames,
@@ -2149,23 +2204,54 @@ export class AgentSession {
2149
2204
  });
2150
2205
  }
2151
2206
  async reload() {
2152
- const previousFlagValues = this._extensionRunner.getFlagValues();
2153
- await emitSessionShutdownEvent(this._extensionRunner, { type: "session_shutdown", reason: "reload" });
2154
- await this.settingsManager.reload();
2155
- resetApiProviders();
2156
- await this._resourceLoader.reload();
2157
- this._buildRuntime({
2158
- activeToolNames: this.getActiveToolNames(),
2159
- flagValues: previousFlagValues,
2160
- includeAllExtensionTools: true,
2161
- });
2162
- const hasBindings = this._extensionUIContext ||
2163
- this._extensionCommandContextActions ||
2164
- this._extensionShutdownHandler ||
2165
- this._extensionErrorListener;
2166
- if (hasBindings) {
2167
- await this._extensionRunner.emit({ type: "session_start", reason: "reload" });
2168
- await this.extendResourcesFromExtensions("reload");
2207
+ const previousRunner = this._extensionRunner;
2208
+ const snapshot = this._createReloadRuntimeSnapshot();
2209
+ const activeToolNames = this.getActiveToolNames();
2210
+ const previousFlagValues = previousRunner.getFlagValues();
2211
+ const reloadErrors = [];
2212
+ let newRunner;
2213
+ try {
2214
+ await this.settingsManager.reload();
2215
+ await this._resourceLoader.reload({ failOnExtensionErrors: true, deferExtensionDispose: true });
2216
+ resetApiProviders();
2217
+ this._buildRuntime({
2218
+ activeToolNames,
2219
+ flagValues: previousFlagValues,
2220
+ includeAllExtensionTools: true,
2221
+ });
2222
+ newRunner = this._extensionRunner;
2223
+ const offDoctorErrors = newRunner.onError((error) => {
2224
+ reloadErrors.push(`${error.extensionPath} ${error.event}: ${error.error}`);
2225
+ });
2226
+ try {
2227
+ this._doctorReloadRuntime();
2228
+ const hasBindings = this._extensionUIContext ||
2229
+ this._extensionCommandContextActions ||
2230
+ this._extensionShutdownHandler ||
2231
+ this._extensionErrorListener;
2232
+ if (hasBindings) {
2233
+ await newRunner.emit({ type: "session_start", reason: "reload" });
2234
+ await this.extendResourcesFromExtensions("reload");
2235
+ this._doctorReloadRuntime();
2236
+ }
2237
+ }
2238
+ finally {
2239
+ offDoctorErrors();
2240
+ }
2241
+ if (reloadErrors.length > 0) {
2242
+ throw new Error(`Extension reload failed doctor: ${reloadErrors.slice(0, 6).join("; ")}`);
2243
+ }
2244
+ await emitSessionShutdownEvent(previousRunner, { type: "session_shutdown", reason: "reload" });
2245
+ previousRunner.invalidate();
2246
+ this._resourceLoader.commitReload?.();
2247
+ }
2248
+ catch (error) {
2249
+ if (newRunner && newRunner !== previousRunner) {
2250
+ newRunner.invalidate("This extension ctx was discarded because reload failed and Pi restored the previous valid runtime.");
2251
+ }
2252
+ this._resourceLoader.rollbackReload?.();
2253
+ this._restoreReloadRuntimeSnapshot(snapshot);
2254
+ throw error;
2169
2255
  }
2170
2256
  }
2171
2257
  // =========================================================================