@draht/coding-agent 2026.4.5 → 2026.4.23
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.
- package/CHANGELOG.md +790 -625
- package/README.md +105 -96
- package/dist/cli/args.d.ts +7 -10
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +42 -44
- package/dist/cli/args.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +1 -0
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +9 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +18 -0
- package/dist/config.js.map +1 -1
- package/dist/core/agent-session-runtime.d.ts +53 -106
- package/dist/core/agent-session-runtime.d.ts.map +1 -1
- package/dist/core/agent-session-runtime.js +116 -147
- package/dist/core/agent-session-runtime.js.map +1 -1
- package/dist/core/agent-session-services.d.ts +86 -0
- package/dist/core/agent-session-services.d.ts.map +1 -0
- package/dist/core/agent-session-services.js +116 -0
- package/dist/core/agent-session-services.js.map +1 -0
- package/dist/core/agent-session.d.ts +4 -12
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +122 -43
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +19 -7
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/compaction/compaction.d.ts +1 -1
- package/dist/core/compaction/compaction.d.ts.map +1 -1
- package/dist/core/compaction/compaction.js +18 -17
- package/dist/core/compaction/compaction.js.map +1 -1
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +7 -7
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +1 -0
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +4 -0
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +15 -14
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/index.d.ts +2 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +2 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/keybindings.d.ts +14 -1
- package/dist/core/keybindings.d.ts.map +1 -1
- package/dist/core/keybindings.js +13 -14
- package/dist/core/keybindings.js.map +1 -1
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +30 -1
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/package-manager.d.ts +21 -1
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +108 -43
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/prompt-templates.d.ts.map +1 -1
- package/dist/core/prompt-templates.js +1 -22
- package/dist/core/prompt-templates.js.map +1 -1
- package/dist/core/resource-loader.d.ts +0 -2
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +26 -8
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/sdk.d.ts +2 -2
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +2 -2
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-cwd.d.ts +19 -0
- package/dist/core/session-cwd.d.ts.map +1 -0
- package/dist/core/session-cwd.js +38 -0
- package/dist/core/session-cwd.js.map +1 -0
- package/dist/core/session-manager.d.ts +5 -1
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +16 -8
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-manager.d.ts +3 -1
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +5 -1
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/skills.d.ts +1 -1
- package/dist/core/skills.d.ts.map +1 -1
- package/dist/core/skills.js +3 -3
- package/dist/core/skills.js.map +1 -1
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +6 -6
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/timings.d.ts +1 -0
- package/dist/core/timings.d.ts.map +1 -1
- package/dist/core/timings.js +6 -0
- package/dist/core/timings.js.map +1 -1
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +19 -9
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/edit-diff.d.ts +23 -1
- package/dist/core/tools/edit-diff.d.ts.map +1 -1
- package/dist/core/tools/edit-diff.js +150 -57
- package/dist/core/tools/edit-diff.js.map +1 -1
- package/dist/core/tools/edit.d.ts +13 -11
- package/dist/core/tools/edit.d.ts.map +1 -1
- package/dist/core/tools/edit.js +55 -77
- package/dist/core/tools/edit.js.map +1 -1
- package/dist/core/tools/file-mutation-queue.d.ts.map +1 -1
- package/dist/core/tools/file-mutation-queue.js +4 -4
- package/dist/core/tools/file-mutation-queue.js.map +1 -1
- package/dist/core/tools/index.d.ts +9 -6
- package/dist/core/tools/index.d.ts.map +1 -1
- package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -1
- package/dist/core/tools/tool-definition-wrapper.js +2 -0
- package/dist/core/tools/tool-definition-wrapper.js.map +1 -1
- package/dist/index.d.ts +2 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -5
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +248 -450
- package/dist/main.js.map +1 -1
- package/dist/migrations.d.ts +3 -3
- package/dist/migrations.d.ts.map +1 -1
- package/dist/migrations.js +25 -5
- package/dist/migrations.js.map +1 -1
- package/dist/modes/interactive/components/assistant-message.d.ts +3 -1
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/assistant-message.js +14 -3
- package/dist/modes/interactive/components/assistant-message.js.map +1 -1
- package/dist/modes/interactive/components/earendil-announcement.d.ts +5 -0
- package/dist/modes/interactive/components/earendil-announcement.d.ts.map +1 -0
- package/dist/modes/interactive/components/earendil-announcement.js +40 -0
- package/dist/modes/interactive/components/earendil-announcement.js.map +1 -0
- package/dist/modes/interactive/components/tool-execution.d.ts +0 -1
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +2 -7
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/components/tree-selector.d.ts +4 -2
- package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/tree-selector.js +48 -15
- package/dist/modes/interactive/components/tree-selector.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +12 -3
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +220 -124
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +9 -0
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/dist/modes/print-mode.d.ts +4 -4
- package/dist/modes/print-mode.d.ts.map +1 -1
- package/dist/modes/print-mode.js +6 -2
- package/dist/modes/print-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +1 -0
- package/dist/modes/rpc/rpc-client.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts +2 -2
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +26 -15
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/package-manager-cli.d.ts +4 -0
- package/dist/package-manager-cli.d.ts.map +1 -0
- package/dist/package-manager-cli.js +234 -0
- package/dist/package-manager-cli.js.map +1 -0
- package/dist/prompts/commands/atomic-commit.md +17 -0
- package/dist/prompts/commands/discuss-phase.md +15 -0
- package/dist/prompts/commands/execute-phase.md +14 -0
- package/dist/prompts/commands/fix.md +14 -0
- package/dist/prompts/commands/init-project.md +16 -0
- package/dist/prompts/commands/map-codebase.md +16 -0
- package/dist/prompts/commands/new-project.md +16 -0
- package/dist/prompts/commands/next-milestone.md +16 -0
- package/dist/prompts/commands/pause-work.md +13 -0
- package/dist/prompts/commands/plan-phase.md +15 -0
- package/dist/prompts/commands/progress.md +13 -0
- package/dist/prompts/commands/quick.md +14 -0
- package/dist/prompts/commands/resume-work.md +13 -0
- package/dist/prompts/commands/review.md +15 -0
- package/dist/prompts/commands/verify-work.md +15 -0
- package/dist/utils/paths.d.ts +7 -0
- package/dist/utils/paths.d.ts.map +1 -0
- package/dist/utils/paths.js +19 -0
- package/dist/utils/paths.js.map +1 -0
- package/docs/compaction.md +5 -3
- package/docs/custom-provider.md +1 -1
- package/docs/development.md +3 -1
- package/docs/extensions.md +150 -54
- package/docs/json.md +5 -2
- package/docs/keybindings.md +2 -0
- package/docs/models.md +55 -4
- package/docs/rpc.md +21 -7
- package/docs/sdk.md +116 -48
- package/docs/session.md +3 -3
- package/docs/settings.md +12 -0
- package/docs/skills.md +3 -2
- package/docs/tree.md +5 -2
- package/examples/README.md +1 -1
- package/examples/extensions/README.md +1 -0
- package/examples/extensions/antigravity-image-gen.ts +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +1 -1
- package/examples/extensions/doom-overlay/doom/build.sh +2 -2
- package/examples/extensions/hidden-thinking-label.ts +53 -0
- package/examples/extensions/overlay-qa-tests.ts +2 -2
- package/examples/extensions/sandbox/index.ts +8 -4
- package/examples/extensions/subagent/index.ts +5 -5
- package/examples/extensions/trigger-compact.ts +11 -1
- package/examples/sdk/13-session-runtime.ts +30 -12
- package/examples/sdk/README.md +4 -2
- package/package.json +7 -8
- package/prompts/commands/atomic-commit.md +17 -0
- package/prompts/commands/discuss-phase.md +15 -0
- package/prompts/commands/execute-phase.md +14 -0
- package/prompts/commands/fix.md +14 -0
- package/prompts/commands/init-project.md +16 -0
- package/prompts/commands/map-codebase.md +16 -0
- package/prompts/commands/new-project.md +16 -0
- package/prompts/commands/next-milestone.md +16 -0
- package/prompts/commands/pause-work.md +13 -0
- package/prompts/commands/plan-phase.md +15 -0
- package/prompts/commands/progress.md +13 -0
- package/prompts/commands/quick.md +14 -0
- package/prompts/commands/resume-work.md +13 -0
- package/prompts/commands/review.md +15 -0
- package/prompts/commands/verify-work.md +15 -0
|
@@ -163,7 +163,7 @@ export class AgentSession {
|
|
|
163
163
|
* happens here instead of in wrappers.
|
|
164
164
|
*/
|
|
165
165
|
_installAgentToolHooks() {
|
|
166
|
-
this.agent.
|
|
166
|
+
this.agent.beforeToolCall = async ({ toolCall, args }) => {
|
|
167
167
|
const runner = this._extensionRunner;
|
|
168
168
|
if (!runner?.hasHandlers("tool_call")) {
|
|
169
169
|
return undefined;
|
|
@@ -183,8 +183,8 @@ export class AgentSession {
|
|
|
183
183
|
}
|
|
184
184
|
throw new Error(`Extension failed, blocking execution: ${String(err)}`);
|
|
185
185
|
}
|
|
186
|
-
}
|
|
187
|
-
this.agent.
|
|
186
|
+
};
|
|
187
|
+
this.agent.afterToolCall = async ({ toolCall, args, result, isError }) => {
|
|
188
188
|
const runner = this._extensionRunner;
|
|
189
189
|
if (!runner?.hasHandlers("tool_result")) {
|
|
190
190
|
return undefined;
|
|
@@ -205,7 +205,7 @@ export class AgentSession {
|
|
|
205
205
|
content: hookResult.content,
|
|
206
206
|
details: hookResult.details,
|
|
207
207
|
};
|
|
208
|
-
}
|
|
208
|
+
};
|
|
209
209
|
}
|
|
210
210
|
// =========================================================================
|
|
211
211
|
// Event Subscription
|
|
@@ -216,6 +216,13 @@ export class AgentSession {
|
|
|
216
216
|
l(event);
|
|
217
217
|
}
|
|
218
218
|
}
|
|
219
|
+
_emitQueueUpdate() {
|
|
220
|
+
this._emit({
|
|
221
|
+
type: "queue_update",
|
|
222
|
+
steering: [...this._steeringMessages],
|
|
223
|
+
followUp: [...this._followUpMessages],
|
|
224
|
+
});
|
|
225
|
+
}
|
|
219
226
|
// Track last assistant message for auto-compaction check
|
|
220
227
|
_lastAssistantMessage = undefined;
|
|
221
228
|
/** Internal handler for agent events - shared by subscribe and reconnect */
|
|
@@ -266,12 +273,14 @@ export class AgentSession {
|
|
|
266
273
|
const steeringIndex = this._steeringMessages.indexOf(messageText);
|
|
267
274
|
if (steeringIndex !== -1) {
|
|
268
275
|
this._steeringMessages.splice(steeringIndex, 1);
|
|
276
|
+
this._emitQueueUpdate();
|
|
269
277
|
}
|
|
270
278
|
else {
|
|
271
279
|
// Check follow-up queue
|
|
272
280
|
const followUpIndex = this._followUpMessages.indexOf(messageText);
|
|
273
281
|
if (followUpIndex !== -1) {
|
|
274
282
|
this._followUpMessages.splice(followUpIndex, 1);
|
|
283
|
+
this._emitQueueUpdate();
|
|
275
284
|
}
|
|
276
285
|
}
|
|
277
286
|
}
|
|
@@ -544,10 +553,10 @@ export class AgentSession {
|
|
|
544
553
|
validToolNames.push(name);
|
|
545
554
|
}
|
|
546
555
|
}
|
|
547
|
-
this.agent.
|
|
556
|
+
this.agent.state.tools = tools;
|
|
548
557
|
// Rebuild base system prompt with new tool set
|
|
549
558
|
this._baseSystemPrompt = this._rebuildSystemPrompt(validToolNames);
|
|
550
|
-
this.agent.
|
|
559
|
+
this.agent.state.systemPrompt = this._baseSystemPrompt;
|
|
551
560
|
}
|
|
552
561
|
/** Whether compaction or branch summarization is currently running */
|
|
553
562
|
get isCompacting() {
|
|
@@ -561,11 +570,11 @@ export class AgentSession {
|
|
|
561
570
|
}
|
|
562
571
|
/** Current steering mode */
|
|
563
572
|
get steeringMode() {
|
|
564
|
-
return this.agent.
|
|
573
|
+
return this.agent.steeringMode;
|
|
565
574
|
}
|
|
566
575
|
/** Current follow-up mode */
|
|
567
576
|
get followUpMode() {
|
|
568
|
-
return this.agent.
|
|
577
|
+
return this.agent.followUpMode;
|
|
569
578
|
}
|
|
570
579
|
/** Current session file path, or undefined if sessions are disabled */
|
|
571
580
|
get sessionFile() {
|
|
@@ -756,11 +765,11 @@ export class AgentSession {
|
|
|
756
765
|
}
|
|
757
766
|
// Apply extension-modified system prompt, or reset to base
|
|
758
767
|
if (result?.systemPrompt) {
|
|
759
|
-
this.agent.
|
|
768
|
+
this.agent.state.systemPrompt = result.systemPrompt;
|
|
760
769
|
}
|
|
761
770
|
else {
|
|
762
771
|
// Ensure we're using the base prompt (in case previous turn had modifications)
|
|
763
|
-
this.agent.
|
|
772
|
+
this.agent.state.systemPrompt = this._baseSystemPrompt;
|
|
764
773
|
}
|
|
765
774
|
}
|
|
766
775
|
await this.agent.prompt(messages);
|
|
@@ -865,6 +874,7 @@ export class AgentSession {
|
|
|
865
874
|
*/
|
|
866
875
|
async _queueSteer(text, images) {
|
|
867
876
|
this._steeringMessages.push(text);
|
|
877
|
+
this._emitQueueUpdate();
|
|
868
878
|
const content = [{ type: "text", text }];
|
|
869
879
|
if (images) {
|
|
870
880
|
content.push(...images);
|
|
@@ -880,6 +890,7 @@ export class AgentSession {
|
|
|
880
890
|
*/
|
|
881
891
|
async _queueFollowUp(text, images) {
|
|
882
892
|
this._followUpMessages.push(text);
|
|
893
|
+
this._emitQueueUpdate();
|
|
883
894
|
const content = [{ type: "text", text }];
|
|
884
895
|
if (images) {
|
|
885
896
|
content.push(...images);
|
|
@@ -939,7 +950,7 @@ export class AgentSession {
|
|
|
939
950
|
await this.agent.prompt(appMessage);
|
|
940
951
|
}
|
|
941
952
|
else {
|
|
942
|
-
this.agent.
|
|
953
|
+
this.agent.state.messages.push(appMessage);
|
|
943
954
|
this.sessionManager.appendCustomMessageEntry(message.customType, message.content, message.display, message.details);
|
|
944
955
|
this._emit({ type: "message_start", message: appMessage });
|
|
945
956
|
this._emit({ type: "message_end", message: appMessage });
|
|
@@ -993,6 +1004,7 @@ export class AgentSession {
|
|
|
993
1004
|
this._steeringMessages = [];
|
|
994
1005
|
this._followUpMessages = [];
|
|
995
1006
|
this.agent.clearAllQueues();
|
|
1007
|
+
this._emitQueueUpdate();
|
|
996
1008
|
return { steering, followUp };
|
|
997
1009
|
}
|
|
998
1010
|
/** Number of pending messages (includes both steering and follow-up) */
|
|
@@ -1044,7 +1056,7 @@ export class AgentSession {
|
|
|
1044
1056
|
}
|
|
1045
1057
|
const previousModel = this.model;
|
|
1046
1058
|
const thinkingLevel = this._getThinkingLevelForModelSwitch();
|
|
1047
|
-
this.agent.
|
|
1059
|
+
this.agent.state.model = model;
|
|
1048
1060
|
this.sessionManager.appendModelChange(model.provider, model.id);
|
|
1049
1061
|
this.settingsManager.setDefaultModelAndProvider(model.provider, model.id);
|
|
1050
1062
|
// Re-clamp thinking level for new model's capabilities
|
|
@@ -1076,7 +1088,7 @@ export class AgentSession {
|
|
|
1076
1088
|
const next = scopedModels[nextIndex];
|
|
1077
1089
|
const thinkingLevel = this._getThinkingLevelForModelSwitch(next.thinkingLevel);
|
|
1078
1090
|
// Apply model
|
|
1079
|
-
this.agent.
|
|
1091
|
+
this.agent.state.model = next.model;
|
|
1080
1092
|
this.sessionManager.appendModelChange(next.model.provider, next.model.id);
|
|
1081
1093
|
this.settingsManager.setDefaultModelAndProvider(next.model.provider, next.model.id);
|
|
1082
1094
|
// Apply thinking level.
|
|
@@ -1099,7 +1111,7 @@ export class AgentSession {
|
|
|
1099
1111
|
const nextIndex = direction === "forward" ? (currentIndex + 1) % len : (currentIndex - 1 + len) % len;
|
|
1100
1112
|
const nextModel = availableModels[nextIndex];
|
|
1101
1113
|
const thinkingLevel = this._getThinkingLevelForModelSwitch();
|
|
1102
|
-
this.agent.
|
|
1114
|
+
this.agent.state.model = nextModel;
|
|
1103
1115
|
this.sessionManager.appendModelChange(nextModel.provider, nextModel.id);
|
|
1104
1116
|
this.settingsManager.setDefaultModelAndProvider(nextModel.provider, nextModel.id);
|
|
1105
1117
|
// Re-clamp thinking level for new model's capabilities
|
|
@@ -1120,7 +1132,7 @@ export class AgentSession {
|
|
|
1120
1132
|
const effectiveLevel = availableLevels.includes(level) ? level : this._clampThinkingLevel(level, availableLevels);
|
|
1121
1133
|
// Only persist if actually changing
|
|
1122
1134
|
const isChanging = effectiveLevel !== this.agent.state.thinkingLevel;
|
|
1123
|
-
this.agent.
|
|
1135
|
+
this.agent.state.thinkingLevel = effectiveLevel;
|
|
1124
1136
|
if (isChanging) {
|
|
1125
1137
|
this.sessionManager.appendThinkingLevelChange(effectiveLevel);
|
|
1126
1138
|
if (this.supportsThinking() || effectiveLevel !== "off") {
|
|
@@ -1199,7 +1211,7 @@ export class AgentSession {
|
|
|
1199
1211
|
* Saves to settings.
|
|
1200
1212
|
*/
|
|
1201
1213
|
setSteeringMode(mode) {
|
|
1202
|
-
this.agent.
|
|
1214
|
+
this.agent.steeringMode = mode;
|
|
1203
1215
|
this.settingsManager.setSteeringMode(mode);
|
|
1204
1216
|
}
|
|
1205
1217
|
/**
|
|
@@ -1207,7 +1219,7 @@ export class AgentSession {
|
|
|
1207
1219
|
* Saves to settings.
|
|
1208
1220
|
*/
|
|
1209
1221
|
setFollowUpMode(mode) {
|
|
1210
|
-
this.agent.
|
|
1222
|
+
this.agent.followUpMode = mode;
|
|
1211
1223
|
this.settingsManager.setFollowUpMode(mode);
|
|
1212
1224
|
}
|
|
1213
1225
|
// =========================================================================
|
|
@@ -1222,6 +1234,7 @@ export class AgentSession {
|
|
|
1222
1234
|
this._disconnectFromAgent();
|
|
1223
1235
|
await this.abort();
|
|
1224
1236
|
this._compactionAbortController = new AbortController();
|
|
1237
|
+
this._emit({ type: "compaction_start", reason: "manual" });
|
|
1225
1238
|
try {
|
|
1226
1239
|
if (!this.model) {
|
|
1227
1240
|
throw new Error("No model selected");
|
|
@@ -1281,7 +1294,7 @@ export class AgentSession {
|
|
|
1281
1294
|
this.sessionManager.appendCompaction(summary, firstKeptEntryId, tokensBefore, details, fromExtension);
|
|
1282
1295
|
const newEntries = this.sessionManager.getEntries();
|
|
1283
1296
|
const sessionContext = this.sessionManager.buildSessionContext();
|
|
1284
|
-
this.agent.
|
|
1297
|
+
this.agent.state.messages = sessionContext.messages;
|
|
1285
1298
|
// Get the saved compaction entry for the extension event
|
|
1286
1299
|
const savedCompactionEntry = newEntries.find((e) => e.type === "compaction" && e.summary === summary);
|
|
1287
1300
|
if (this._extensionRunner && savedCompactionEntry) {
|
|
@@ -1291,12 +1304,33 @@ export class AgentSession {
|
|
|
1291
1304
|
fromExtension,
|
|
1292
1305
|
});
|
|
1293
1306
|
}
|
|
1294
|
-
|
|
1307
|
+
const compactionResult = {
|
|
1295
1308
|
summary,
|
|
1296
1309
|
firstKeptEntryId,
|
|
1297
1310
|
tokensBefore,
|
|
1298
1311
|
details,
|
|
1299
1312
|
};
|
|
1313
|
+
this._emit({
|
|
1314
|
+
type: "compaction_end",
|
|
1315
|
+
reason: "manual",
|
|
1316
|
+
result: compactionResult,
|
|
1317
|
+
aborted: false,
|
|
1318
|
+
willRetry: false,
|
|
1319
|
+
});
|
|
1320
|
+
return compactionResult;
|
|
1321
|
+
}
|
|
1322
|
+
catch (error) {
|
|
1323
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1324
|
+
const aborted = message === "Compaction cancelled" || (error instanceof Error && error.name === "AbortError");
|
|
1325
|
+
this._emit({
|
|
1326
|
+
type: "compaction_end",
|
|
1327
|
+
reason: "manual",
|
|
1328
|
+
result: undefined,
|
|
1329
|
+
aborted,
|
|
1330
|
+
willRetry: false,
|
|
1331
|
+
errorMessage: aborted ? undefined : `Compaction failed: ${message}`,
|
|
1332
|
+
});
|
|
1333
|
+
throw error;
|
|
1300
1334
|
}
|
|
1301
1335
|
finally {
|
|
1302
1336
|
this._compactionAbortController = undefined;
|
|
@@ -1352,7 +1386,8 @@ export class AgentSession {
|
|
|
1352
1386
|
if (sameModel && isContextOverflow(assistantMessage, contextWindow)) {
|
|
1353
1387
|
if (this._overflowRecoveryAttempted) {
|
|
1354
1388
|
this._emit({
|
|
1355
|
-
type: "
|
|
1389
|
+
type: "compaction_end",
|
|
1390
|
+
reason: "overflow",
|
|
1356
1391
|
result: undefined,
|
|
1357
1392
|
aborted: false,
|
|
1358
1393
|
willRetry: false,
|
|
@@ -1365,7 +1400,7 @@ export class AgentSession {
|
|
|
1365
1400
|
// but we don't want it in context for the retry)
|
|
1366
1401
|
const messages = this.agent.state.messages;
|
|
1367
1402
|
if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
|
|
1368
|
-
this.agent.
|
|
1403
|
+
this.agent.state.messages = messages.slice(0, -1);
|
|
1369
1404
|
}
|
|
1370
1405
|
await this._runAutoCompaction("overflow", true);
|
|
1371
1406
|
return;
|
|
@@ -1375,9 +1410,19 @@ export class AgentSession {
|
|
|
1375
1410
|
// This ensures sessions that hit persistent API errors (e.g. 529) can still compact.
|
|
1376
1411
|
let contextTokens;
|
|
1377
1412
|
if (assistantMessage.stopReason === "error") {
|
|
1378
|
-
const
|
|
1413
|
+
const messages = this.agent.state.messages;
|
|
1414
|
+
const estimate = estimateContextTokens(messages);
|
|
1379
1415
|
if (estimate.lastUsageIndex === null)
|
|
1380
1416
|
return; // No usage data at all
|
|
1417
|
+
// Verify the usage source is post-compaction. Kept pre-compaction messages
|
|
1418
|
+
// have stale usage reflecting the old (larger) context and would falsely
|
|
1419
|
+
// trigger compaction right after one just finished.
|
|
1420
|
+
const usageMsg = messages[estimate.lastUsageIndex];
|
|
1421
|
+
if (compactionEntry &&
|
|
1422
|
+
usageMsg.role === "assistant" &&
|
|
1423
|
+
usageMsg.timestamp <= new Date(compactionEntry.timestamp).getTime()) {
|
|
1424
|
+
return;
|
|
1425
|
+
}
|
|
1381
1426
|
contextTokens = estimate.tokens;
|
|
1382
1427
|
}
|
|
1383
1428
|
else {
|
|
@@ -1392,23 +1437,41 @@ export class AgentSession {
|
|
|
1392
1437
|
*/
|
|
1393
1438
|
async _runAutoCompaction(reason, willRetry) {
|
|
1394
1439
|
const settings = this.settingsManager.getCompactionSettings();
|
|
1395
|
-
this._emit({ type: "
|
|
1440
|
+
this._emit({ type: "compaction_start", reason });
|
|
1396
1441
|
this._autoCompactionAbortController = new AbortController();
|
|
1397
1442
|
try {
|
|
1398
1443
|
if (!this.model) {
|
|
1399
|
-
this._emit({
|
|
1444
|
+
this._emit({
|
|
1445
|
+
type: "compaction_end",
|
|
1446
|
+
reason,
|
|
1447
|
+
result: undefined,
|
|
1448
|
+
aborted: false,
|
|
1449
|
+
willRetry: false,
|
|
1450
|
+
});
|
|
1400
1451
|
return;
|
|
1401
1452
|
}
|
|
1402
1453
|
const authResult = await this._modelRegistry.getApiKeyAndHeaders(this.model);
|
|
1403
1454
|
if (!authResult.ok || !authResult.apiKey) {
|
|
1404
|
-
this._emit({
|
|
1455
|
+
this._emit({
|
|
1456
|
+
type: "compaction_end",
|
|
1457
|
+
reason,
|
|
1458
|
+
result: undefined,
|
|
1459
|
+
aborted: false,
|
|
1460
|
+
willRetry: false,
|
|
1461
|
+
});
|
|
1405
1462
|
return;
|
|
1406
1463
|
}
|
|
1407
1464
|
const { apiKey, headers } = authResult;
|
|
1408
1465
|
const pathEntries = this.sessionManager.getBranch();
|
|
1409
1466
|
const preparation = prepareCompaction(pathEntries, settings);
|
|
1410
1467
|
if (!preparation) {
|
|
1411
|
-
this._emit({
|
|
1468
|
+
this._emit({
|
|
1469
|
+
type: "compaction_end",
|
|
1470
|
+
reason,
|
|
1471
|
+
result: undefined,
|
|
1472
|
+
aborted: false,
|
|
1473
|
+
willRetry: false,
|
|
1474
|
+
});
|
|
1412
1475
|
return;
|
|
1413
1476
|
}
|
|
1414
1477
|
let extensionCompaction;
|
|
@@ -1422,7 +1485,13 @@ export class AgentSession {
|
|
|
1422
1485
|
signal: this._autoCompactionAbortController.signal,
|
|
1423
1486
|
}));
|
|
1424
1487
|
if (extensionResult?.cancel) {
|
|
1425
|
-
this._emit({
|
|
1488
|
+
this._emit({
|
|
1489
|
+
type: "compaction_end",
|
|
1490
|
+
reason,
|
|
1491
|
+
result: undefined,
|
|
1492
|
+
aborted: true,
|
|
1493
|
+
willRetry: false,
|
|
1494
|
+
});
|
|
1426
1495
|
return;
|
|
1427
1496
|
}
|
|
1428
1497
|
if (extensionResult?.compaction) {
|
|
@@ -1450,13 +1519,19 @@ export class AgentSession {
|
|
|
1450
1519
|
details = compactResult.details;
|
|
1451
1520
|
}
|
|
1452
1521
|
if (this._autoCompactionAbortController.signal.aborted) {
|
|
1453
|
-
this._emit({
|
|
1522
|
+
this._emit({
|
|
1523
|
+
type: "compaction_end",
|
|
1524
|
+
reason,
|
|
1525
|
+
result: undefined,
|
|
1526
|
+
aborted: true,
|
|
1527
|
+
willRetry: false,
|
|
1528
|
+
});
|
|
1454
1529
|
return;
|
|
1455
1530
|
}
|
|
1456
1531
|
this.sessionManager.appendCompaction(summary, firstKeptEntryId, tokensBefore, details, fromExtension);
|
|
1457
1532
|
const newEntries = this.sessionManager.getEntries();
|
|
1458
1533
|
const sessionContext = this.sessionManager.buildSessionContext();
|
|
1459
|
-
this.agent.
|
|
1534
|
+
this.agent.state.messages = sessionContext.messages;
|
|
1460
1535
|
// Get the saved compaction entry for the extension event
|
|
1461
1536
|
const savedCompactionEntry = newEntries.find((e) => e.type === "compaction" && e.summary === summary);
|
|
1462
1537
|
if (this._extensionRunner && savedCompactionEntry) {
|
|
@@ -1472,12 +1547,12 @@ export class AgentSession {
|
|
|
1472
1547
|
tokensBefore,
|
|
1473
1548
|
details,
|
|
1474
1549
|
};
|
|
1475
|
-
this._emit({ type: "
|
|
1550
|
+
this._emit({ type: "compaction_end", reason, result, aborted: false, willRetry });
|
|
1476
1551
|
if (willRetry) {
|
|
1477
1552
|
const messages = this.agent.state.messages;
|
|
1478
1553
|
const lastMsg = messages[messages.length - 1];
|
|
1479
1554
|
if (lastMsg?.role === "assistant" && lastMsg.stopReason === "error") {
|
|
1480
|
-
this.agent.
|
|
1555
|
+
this.agent.state.messages = messages.slice(0, -1);
|
|
1481
1556
|
}
|
|
1482
1557
|
setTimeout(() => {
|
|
1483
1558
|
this.agent.continue().catch(() => { });
|
|
@@ -1494,7 +1569,8 @@ export class AgentSession {
|
|
|
1494
1569
|
catch (error) {
|
|
1495
1570
|
const errorMessage = error instanceof Error ? error.message : "compaction failed";
|
|
1496
1571
|
this._emit({
|
|
1497
|
-
type: "
|
|
1572
|
+
type: "compaction_end",
|
|
1573
|
+
reason,
|
|
1498
1574
|
result: undefined,
|
|
1499
1575
|
aborted: false,
|
|
1500
1576
|
willRetry: false,
|
|
@@ -1551,7 +1627,7 @@ export class AgentSession {
|
|
|
1551
1627
|
};
|
|
1552
1628
|
this._resourceLoader.extendResources(extensionPaths);
|
|
1553
1629
|
this._baseSystemPrompt = this._rebuildSystemPrompt(this.getActiveToolNames());
|
|
1554
|
-
this.agent.
|
|
1630
|
+
this.agent.state.systemPrompt = this._baseSystemPrompt;
|
|
1555
1631
|
}
|
|
1556
1632
|
buildExtensionResourcePaths(entries) {
|
|
1557
1633
|
return entries.map((entry) => {
|
|
@@ -1593,7 +1669,7 @@ export class AgentSession {
|
|
|
1593
1669
|
if (!refreshedModel || refreshedModel === currentModel) {
|
|
1594
1670
|
return;
|
|
1595
1671
|
}
|
|
1596
|
-
this.agent.
|
|
1672
|
+
this.agent.state.model = refreshedModel;
|
|
1597
1673
|
}
|
|
1598
1674
|
_bindExtensionCore(runner) {
|
|
1599
1675
|
const getCommands = () => {
|
|
@@ -1664,6 +1740,7 @@ export class AgentSession {
|
|
|
1664
1740
|
}, {
|
|
1665
1741
|
getModel: () => this.model,
|
|
1666
1742
|
isIdle: () => !this.isStreaming,
|
|
1743
|
+
getSignal: () => this.agent.signal,
|
|
1667
1744
|
abort: () => this.abort(),
|
|
1668
1745
|
hasPendingMessages: () => this.pendingMessageCount > 0,
|
|
1669
1746
|
shutdown: () => {
|
|
@@ -1803,7 +1880,7 @@ export class AgentSession {
|
|
|
1803
1880
|
async reload() {
|
|
1804
1881
|
const previousFlagValues = this._extensionRunner?.getFlagValues();
|
|
1805
1882
|
await this._extensionRunner?.emit({ type: "session_shutdown" });
|
|
1806
|
-
this.settingsManager.reload();
|
|
1883
|
+
await this.settingsManager.reload();
|
|
1807
1884
|
resetApiProviders();
|
|
1808
1885
|
await this._resourceLoader.reload();
|
|
1809
1886
|
this._buildRuntime({
|
|
@@ -1835,8 +1912,8 @@ export class AgentSession {
|
|
|
1835
1912
|
if (isContextOverflow(message, contextWindow))
|
|
1836
1913
|
return false;
|
|
1837
1914
|
const err = message.errorMessage;
|
|
1838
|
-
// Match: overloaded_error, provider returned error, rate limit, 429, 500, 502, 503, 504, service unavailable, network/connection errors, fetch failed, terminated, retry delay exceeded
|
|
1839
|
-
return /overloaded|provider.?returned.?error|rate.?limit|too many requests|429|500|502|503|504|service.?unavailable|server.?error|internal.?error|network.?error|connection.?error|connection.?refused|other side closed|fetch failed|upstream.?connect|reset before headers|socket hang up|timed? out|timeout|terminated|retry delay/i.test(err);
|
|
1915
|
+
// Match: overloaded_error, provider returned error, rate limit, 429, 500, 502, 503, 504, service unavailable, network/connection errors, fetch failed, request ended without sending chunks, terminated, retry delay exceeded
|
|
1916
|
+
return /overloaded|provider.?returned.?error|rate.?limit|too many requests|429|500|502|503|504|service.?unavailable|server.?error|internal.?error|network.?error|connection.?error|connection.?refused|other side closed|fetch failed|upstream.?connect|reset before headers|socket hang up|ended without|timed? out|timeout|terminated|retry delay/i.test(err);
|
|
1840
1917
|
}
|
|
1841
1918
|
/**
|
|
1842
1919
|
* Handle retryable errors with exponential backoff.
|
|
@@ -1879,7 +1956,7 @@ export class AgentSession {
|
|
|
1879
1956
|
// Remove error message from agent state (keep in session for history)
|
|
1880
1957
|
const messages = this.agent.state.messages;
|
|
1881
1958
|
if (messages.length > 0 && messages[messages.length - 1].role === "assistant") {
|
|
1882
|
-
this.agent.
|
|
1959
|
+
this.agent.state.messages = messages.slice(0, -1);
|
|
1883
1960
|
}
|
|
1884
1961
|
// Wait with exponential backoff (abortable)
|
|
1885
1962
|
this._retryAbortController = new AbortController();
|
|
@@ -1922,9 +1999,11 @@ export class AgentSession {
|
|
|
1922
1999
|
* Returns immediately if no retry is in progress.
|
|
1923
2000
|
*/
|
|
1924
2001
|
async waitForRetry() {
|
|
1925
|
-
if (this._retryPromise) {
|
|
1926
|
-
|
|
2002
|
+
if (!this._retryPromise) {
|
|
2003
|
+
return;
|
|
1927
2004
|
}
|
|
2005
|
+
await this._retryPromise;
|
|
2006
|
+
await this.agent.waitForIdle();
|
|
1928
2007
|
}
|
|
1929
2008
|
/** Whether auto-retry is currently in progress */
|
|
1930
2009
|
get isRetrying() {
|
|
@@ -1991,7 +2070,7 @@ export class AgentSession {
|
|
|
1991
2070
|
}
|
|
1992
2071
|
else {
|
|
1993
2072
|
// Add to agent state immediately
|
|
1994
|
-
this.agent.
|
|
2073
|
+
this.agent.state.messages.push(bashMessage);
|
|
1995
2074
|
// Save to session
|
|
1996
2075
|
this.sessionManager.appendMessage(bashMessage);
|
|
1997
2076
|
}
|
|
@@ -2019,7 +2098,7 @@ export class AgentSession {
|
|
|
2019
2098
|
return;
|
|
2020
2099
|
for (const bashMessage of this._pendingBashMessages) {
|
|
2021
2100
|
// Add to agent state
|
|
2022
|
-
this.agent.
|
|
2101
|
+
this.agent.state.messages.push(bashMessage);
|
|
2023
2102
|
// Save to session
|
|
2024
2103
|
this.sessionManager.appendMessage(bashMessage);
|
|
2025
2104
|
}
|
|
@@ -2189,7 +2268,7 @@ export class AgentSession {
|
|
|
2189
2268
|
}
|
|
2190
2269
|
// Update agent state
|
|
2191
2270
|
const sessionContext = this.sessionManager.buildSessionContext();
|
|
2192
|
-
this.agent.
|
|
2271
|
+
this.agent.state.messages = sessionContext.messages;
|
|
2193
2272
|
// Emit session_tree event
|
|
2194
2273
|
if (this._extensionRunner) {
|
|
2195
2274
|
await this._extensionRunner.emit({
|