When 429/500 errors occur during tool execution, empty assistant messages
with stopReason='error' and content=[] get persisted to the session file.
These break the tool_use -> tool_result chain that Claude/Gemini require:
- Claude expects every tool_use block to have a matching tool_result
- Empty error messages inserted mid-sequence violate this invariant
- Results in: 'tool_use ids were found without tool_result blocks'
This patch filters out empty error messages when building session context,
allowing sessions to recover gracefully from transient API errors.
Evidence from production:
- 113 of 170 sessions had empty error messages
- Session 30764430 demonstrated recovery: 429 at 14:30:11 IST,
resumed successfully at 14:30:22, completed at 14:30:34
Sorry Peter, one more patch! 🙈
49 lines
2.1 KiB
Diff
49 lines
2.1 KiB
Diff
diff --git a/dist/core/sdk.js b/dist/core/sdk.js
|
|
index 0000000..1111111 100644
|
|
--- a/dist/core/sdk.js
|
|
+++ b/dist/core/sdk.js
|
|
@@ -441,6 +441,8 @@ export async function createAgentSession(options = {}) {
|
|
}
|
|
return key;
|
|
},
|
|
+ // PATCH: Pass extraParams through for provider-specific features (e.g., GLM-4.7 thinking mode)
|
|
+ extraParams: options.extraParams,
|
|
});
|
|
time("createAgent");
|
|
// Restore messages if session has existing data
|
|
diff --git a/dist/core/sdk.d.ts b/dist/core/sdk.d.ts
|
|
index 0000000..1111111 100644
|
|
--- a/dist/core/sdk.d.ts
|
|
+++ b/dist/core/sdk.d.ts
|
|
@@ -79,6 +79,10 @@ export interface CreateAgentSessionOptions {
|
|
sessionManager?: SessionManager;
|
|
/** Settings manager. Default: SettingsManager.create(cwd, agentDir) */
|
|
settingsManager?: SettingsManager;
|
|
+ /**
|
|
+ * Extra params to pass to the provider API (e.g., Z.AI GLM thinking mode params).
|
|
+ */
|
|
+ extraParams?: Record<string, unknown>;
|
|
}
|
|
/** Result from createAgentSession */
|
|
export interface CreateAgentSessionResult {
|
|
diff --git a/dist/core/session-manager.js b/dist/core/session-manager.js
|
|
index b2aba5280d002253b0938b75aedbb9e6e6c4dcf8..67464efff535dbd7a8e6ed825aab2b305ca2aee2 100644
|
|
--- a/dist/core/session-manager.js
|
|
+++ b/dist/core/session-manager.js
|
|
@@ -161,6 +161,15 @@ export function buildSessionContext(entries, leafId, byId) {
|
|
const messages = [];
|
|
const appendMessage = (entry) => {
|
|
if (entry.type === "message") {
|
|
+ // PATCH: Filter out empty error assistant messages to prevent session corruption
|
|
+ // When 429/500 errors occur during tool execution, empty error messages get persisted
|
|
+ // to the session file. These break the tool_use -> tool_result chain for Claude/Gemini.
|
|
+ const msg = entry.message;
|
|
+ if (msg.role === "assistant" &&
|
|
+ msg.stopReason === "error" &&
|
|
+ (!msg.content || msg.content.length === 0)) {
|
|
+ return; // Skip empty error messages
|
|
+ }
|
|
messages.push(entry.message);
|
|
}
|
|
else if (entry.type === "custom_message") {
|