wave-agent-sdk 0.19.5 → 0.19.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 (62) hide show
  1. package/dist/agent.d.ts +21 -0
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +21 -0
  4. package/dist/managers/aiManager.d.ts +14 -1
  5. package/dist/managers/aiManager.d.ts.map +1 -1
  6. package/dist/managers/aiManager.js +39 -2
  7. package/dist/managers/backgroundTaskManager.d.ts +1 -1
  8. package/dist/managers/backgroundTaskManager.d.ts.map +1 -1
  9. package/dist/managers/backgroundTaskManager.js +4 -3
  10. package/dist/managers/bangManager.d.ts.map +1 -1
  11. package/dist/managers/bangManager.js +2 -1
  12. package/dist/managers/messageManager.d.ts +7 -0
  13. package/dist/managers/messageManager.d.ts.map +1 -1
  14. package/dist/managers/messageManager.js +64 -5
  15. package/dist/managers/messageQueue.d.ts +8 -0
  16. package/dist/managers/messageQueue.d.ts.map +1 -1
  17. package/dist/managers/messageQueue.js +13 -0
  18. package/dist/managers/permissionManager.d.ts.map +1 -1
  19. package/dist/managers/permissionManager.js +3 -3
  20. package/dist/managers/subagentManager.d.ts.map +1 -1
  21. package/dist/managers/subagentManager.js +7 -1
  22. package/dist/prompts/index.d.ts +3 -1
  23. package/dist/prompts/index.d.ts.map +1 -1
  24. package/dist/prompts/index.js +2 -4
  25. package/dist/services/hook.d.ts.map +1 -1
  26. package/dist/services/hook.js +6 -0
  27. package/dist/tools/bashTool.d.ts.map +1 -1
  28. package/dist/tools/bashTool.js +7 -5
  29. package/dist/tools/enterWorktreeTool.d.ts.map +1 -1
  30. package/dist/tools/enterWorktreeTool.js +3 -8
  31. package/dist/tools/exitWorktreeTool.d.ts.map +1 -1
  32. package/dist/tools/exitWorktreeTool.js +5 -8
  33. package/dist/types/hooks.d.ts +2 -0
  34. package/dist/types/hooks.d.ts.map +1 -1
  35. package/dist/types/messaging.d.ts +2 -0
  36. package/dist/types/messaging.d.ts.map +1 -1
  37. package/dist/utils/containerSetup.d.ts.map +1 -1
  38. package/dist/utils/containerSetup.js +3 -0
  39. package/dist/utils/shellResolver.d.ts.map +1 -1
  40. package/dist/utils/shellResolver.js +156 -7
  41. package/dist/utils/worktreeSession.d.ts +6 -6
  42. package/dist/utils/worktreeSession.d.ts.map +1 -1
  43. package/dist/utils/worktreeSession.js +7 -11
  44. package/package.json +1 -1
  45. package/src/agent.ts +31 -0
  46. package/src/managers/aiManager.ts +49 -2
  47. package/src/managers/backgroundTaskManager.ts +4 -2
  48. package/src/managers/bangManager.ts +2 -1
  49. package/src/managers/messageManager.ts +72 -6
  50. package/src/managers/messageQueue.ts +17 -0
  51. package/src/managers/permissionManager.ts +6 -3
  52. package/src/managers/subagentManager.ts +13 -1
  53. package/src/prompts/index.ts +4 -4
  54. package/src/services/hook.ts +9 -0
  55. package/src/tools/bashTool.ts +11 -5
  56. package/src/tools/enterWorktreeTool.ts +4 -14
  57. package/src/tools/exitWorktreeTool.ts +5 -13
  58. package/src/types/hooks.ts +2 -0
  59. package/src/types/messaging.ts +2 -0
  60. package/src/utils/containerSetup.ts +4 -0
  61. package/src/utils/shellResolver.ts +167 -8
  62. package/src/utils/worktreeSession.ts +6 -16
@@ -38,6 +38,7 @@ import {
38
38
  buildExitedPlanModeReminder,
39
39
  } from "../prompts/planModeReminders.js";
40
40
  import { Container } from "../utils/container.js";
41
+ import type { WorktreeSession } from "../utils/worktreeSession.js";
41
42
  import { recoverTruncatedJson } from "../utils/stringUtils.js";
42
43
  import { ConfigurationService } from "../services/configurationService.js";
43
44
  import type { NotificationQueue } from "./notificationQueue.js";
@@ -260,11 +261,31 @@ export class AIManager {
260
261
 
261
262
  /**
262
263
  * Update the working directory mid-session (e.g., when entering/exiting a worktree).
263
- * Also updates process.chdir() so bash commands use the new directory.
264
+ * Only updates this session's DI container; it does NOT change the process-level
265
+ * process.cwd(), so concurrent sessions in the same stdio process are unaffected.
266
+ * Triggers `_onCwdChange` (wired by Agent to `onWorkdirChange`) so the host is
267
+ * notified of worktree switches. Unlike bash `cd`, this does NOT run CwdChanged
268
+ * hooks (worktree has its own WorktreeCreate/WorktreeRemove hooks).
264
269
  */
265
270
  public setWorkdir(newWorkdir: string): void {
266
271
  this.container.register("Workdir", newWorkdir);
267
- process.chdir(newWorkdir);
272
+ this._onCwdChange?.(newWorkdir);
273
+ }
274
+
275
+ /**
276
+ * Get this session's worktree session state (null if not in a worktree).
277
+ */
278
+ public getWorktreeSession(): WorktreeSession | null {
279
+ return (
280
+ this.container.get<WorktreeSession | null>("WorktreeSession") ?? null
281
+ );
282
+ }
283
+
284
+ /**
285
+ * Set this session's worktree session state.
286
+ */
287
+ public setWorktreeSession(session: WorktreeSession | null): void {
288
+ this.container.register("WorktreeSession", session);
268
289
  }
269
290
 
270
291
  public setOnCwdChange(callback: (newCwd: string) => void): void {
@@ -942,6 +963,7 @@ export class AIManager {
942
963
  originalWorkdir: this.getOriginalWorkdir(),
943
964
  language: this.getLanguage(),
944
965
  isSubagent: !!this.subagentType,
966
+ worktreeSession: this.getWorktreeSession(),
945
967
  autoMemory: autoMemoryOptions,
946
968
  },
947
969
  ), // Pass custom system prompt
@@ -1326,6 +1348,16 @@ export class AIManager {
1326
1348
  message: error instanceof Error ? error.message : String(error),
1327
1349
  }).catch(() => {}); // Non-blocking
1328
1350
 
1351
+ // Finalize any streaming text/reasoning blocks so the UI stops ticking
1352
+ // its in-progress timer (e.g. when the request is aborted mid-thought).
1353
+ this.messageManager.finalizeStreamingBlocks();
1354
+
1355
+ // Finalize any tool blocks stuck in start/streaming/running so the UI
1356
+ // stops showing the yellow "running" spinner (e.g. abort mid-tool-stream).
1357
+ this.messageManager.finalizeAbortedToolBlocks(
1358
+ error instanceof Error ? error.message : undefined,
1359
+ );
1360
+
1329
1361
  this.messageManager.addErrorBlock(
1330
1362
  error instanceof Error ? error.message : "Unknown error occurred",
1331
1363
  );
@@ -1502,6 +1534,20 @@ export class AIManager {
1502
1534
  }))
1503
1535
  : undefined;
1504
1536
 
1537
+ // Extract text content from the last assistant message so hooks can
1538
+ // inspect the final response without reading the transcript file.
1539
+ const allMessages = this.messageManager.getMessages();
1540
+ const lastAssistant = [...allMessages]
1541
+ .reverse()
1542
+ .find((m) => m.role === "assistant");
1543
+ const lastAssistantText = lastAssistant
1544
+ ? lastAssistant.blocks
1545
+ .filter((b) => b.type === "text")
1546
+ .map((b) => b.content)
1547
+ .join("\n")
1548
+ .trim() || undefined
1549
+ : undefined;
1550
+
1505
1551
  const context: ExtendedHookExecutionContext = {
1506
1552
  event: hookName,
1507
1553
  projectDir: this.getWorkdir(),
@@ -1512,6 +1558,7 @@ export class AIManager {
1512
1558
  subagentType: this.subagentType, // Include subagent type in hook context
1513
1559
  backgroundTasks, // Stop-only: running background tasks snapshot
1514
1560
  sessionCrons, // Stop-only: session cron jobs snapshot
1561
+ lastAssistantMessage: lastAssistantText, // Stop/SubagentStop: last assistant message text
1515
1562
  // Stop hooks don't need toolName, toolInput, toolResponse, or userPrompt
1516
1563
  env: Object.fromEntries(
1517
1564
  Object.entries(process.env).filter((e) => e[1] !== undefined),
@@ -7,6 +7,7 @@ import { stripAnsiColors } from "../utils/stringUtils.js";
7
7
  import { logger } from "../utils/globalLogger.js";
8
8
  import { Container } from "../utils/container.js";
9
9
  import { NotificationQueue } from "./notificationQueue.js";
10
+ import { resolveShellPath } from "../utils/shellResolver.js";
10
11
 
11
12
  export interface BackgroundTaskManagerCallbacks {
12
13
  onBackgroundTasksChange?: (tasks: BackgroundTask[]) => void;
@@ -64,15 +65,16 @@ export class BackgroundTaskManager {
64
65
  public startShell(
65
66
  command: string,
66
67
  timeout?: number,
68
+ cwd?: string,
67
69
  ): { id: string; child: ChildProcess; detach: () => void } {
68
70
  const id = this.generateId();
69
71
  const startTime = Date.now();
70
72
 
71
73
  const child = spawn(command, {
72
- shell: true,
74
+ shell: resolveShellPath() ?? true,
73
75
  stdio: "pipe",
74
76
  detached: true,
75
- cwd: this.workdir,
77
+ cwd: cwd ?? this.workdir,
76
78
  env: {
77
79
  ...process.env,
78
80
  },
@@ -1,6 +1,7 @@
1
1
  import { spawn, type ChildProcess } from "child_process";
2
2
  import type { MessageManager } from "./messageManager.js";
3
3
  import { Container } from "../utils/container.js";
4
+ import { resolveShellPath } from "../utils/shellResolver.js";
4
5
 
5
6
  export interface BangManagerOptions {
6
7
  workdir: string;
@@ -45,7 +46,7 @@ export class BangManager {
45
46
 
46
47
  return new Promise<number>((resolve) => {
47
48
  const child = spawn(command, {
48
- shell: true,
49
+ shell: resolveShellPath() ?? true,
49
50
  stdio: "pipe",
50
51
  cwd: this.workdir,
51
52
  env: {
@@ -16,7 +16,7 @@ import {
16
16
  type AddNotificationMessageParams,
17
17
  generateMessageId,
18
18
  } from "../utils/messageOperations.js";
19
- import type { Message, Usage } from "../types/index.js";
19
+ import type { Message, Usage, ToolBlock } from "../types/index.js";
20
20
  import { getLastApiRounds } from "../utils/groupMessagesByApiRound.js";
21
21
  import { join, isAbsolute, relative } from "path";
22
22
  import {
@@ -718,12 +718,23 @@ export class MessageManager {
718
718
  type: "text" | "reasoning";
719
719
  content: string;
720
720
  stage?: string;
721
+ startTime?: number;
721
722
  };
722
- lastMessage.blocks[index] = {
723
- type: block.type,
724
- content: block.content,
725
- stage: "end" as const,
726
- };
723
+ if (block.type === "reasoning") {
724
+ lastMessage.blocks[index] = {
725
+ type: "reasoning",
726
+ content: block.content,
727
+ stage: "end" as const,
728
+ startTime: block.startTime,
729
+ endTime: Date.now(),
730
+ };
731
+ } else {
732
+ lastMessage.blocks[index] = {
733
+ type: block.type,
734
+ content: block.content,
735
+ stage: "end" as const,
736
+ };
737
+ }
727
738
 
728
739
  // Fire incremental callback to signal finalization
729
740
  const callbackParams = {
@@ -832,10 +843,16 @@ export class MessageManager {
832
843
 
833
844
  if (reasoningBlockIndex >= 0) {
834
845
  // Update existing reasoning block
846
+ const existingStartTime = (
847
+ lastMessage.blocks[reasoningBlockIndex] as {
848
+ startTime?: number;
849
+ }
850
+ ).startTime;
835
851
  lastMessage.blocks[reasoningBlockIndex] = {
836
852
  type: "reasoning",
837
853
  content: newAccumulatedReasoning,
838
854
  stage: "streaming",
855
+ startTime: existingStartTime,
839
856
  };
840
857
  } else {
841
858
  // Add new reasoning block if none exists
@@ -843,6 +860,7 @@ export class MessageManager {
843
860
  type: "reasoning",
844
861
  content: newAccumulatedReasoning,
845
862
  stage: "streaming",
863
+ startTime: Date.now(),
846
864
  });
847
865
  }
848
866
 
@@ -878,6 +896,54 @@ export class MessageManager {
878
896
  }
879
897
  }
880
898
 
899
+ /**
900
+ * Finalize any tool blocks still in a non-terminal stage (start/streaming/running)
901
+ * by marking them as ended with an error. Called when the AI call is aborted or
902
+ * fails mid-tool-stream so the UI stops showing the "running" spinner.
903
+ * Fires onToolBlockUpdated for each finalized tool block.
904
+ */
905
+ public finalizeAbortedToolBlocks(error?: string): void {
906
+ if (this.messages.length === 0) return;
907
+ const lastMessage = this.messages[this.messages.length - 1];
908
+ if (lastMessage.role !== "assistant") return;
909
+
910
+ const errorMessage = error ?? "Tool execution was aborted";
911
+ let finalized = false;
912
+
913
+ for (let i = 0; i < lastMessage.blocks.length; i++) {
914
+ const block = lastMessage.blocks[i] as ToolBlock;
915
+ if (block.type !== "tool" || block.stage === "end") continue;
916
+
917
+ const timestamp = Date.now();
918
+ lastMessage.blocks[i] = {
919
+ ...block,
920
+ stage: "end",
921
+ success: false,
922
+ error: errorMessage,
923
+ timestamp,
924
+ };
925
+ finalized = true;
926
+
927
+ this.callbacks.onToolBlockUpdated?.({
928
+ id: block.id ?? "",
929
+ messageId: lastMessage.id ?? "",
930
+ name: block.name,
931
+ parameters: block.parameters,
932
+ parametersChunk: block.parametersChunk,
933
+ compactParams: block.compactParams,
934
+ stage: "end",
935
+ success: false,
936
+ error: errorMessage,
937
+ isManuallyBackgrounded: block.isManuallyBackgrounded,
938
+ timestamp,
939
+ });
940
+ }
941
+
942
+ if (finalized) {
943
+ this.setMessages([...this.messages]);
944
+ }
945
+ }
946
+
881
947
  /**
882
948
  * Remove the last user message from the conversation
883
949
  * Used for hook error handling when the user prompt needs to be erased
@@ -74,6 +74,23 @@ export class MessageQueue {
74
74
  return true;
75
75
  }
76
76
 
77
+ updateById(
78
+ id: string,
79
+ patch: {
80
+ content?: string;
81
+ images?: Array<{ path: string; mimeType: string }>;
82
+ type?: "message" | "bang";
83
+ },
84
+ ): boolean {
85
+ const m = this.queue.find((x) => x.id === id);
86
+ if (!m) return false;
87
+ if (patch.content !== undefined) m.content = patch.content;
88
+ if (patch.images !== undefined) m.images = patch.images;
89
+ if (patch.type !== undefined) m.type = patch.type;
90
+ this.onMessageEnqueued?.();
91
+ return true;
92
+ }
93
+
77
94
  popLastEditable(): QueuedMessage | null {
78
95
  for (let i = this.queue.length - 1; i >= 0; i--) {
79
96
  if (this.queue[i].editable !== false) {
@@ -35,7 +35,7 @@ import {
35
35
  } from "../constants/tools.js";
36
36
  import { Container } from "../utils/container.js";
37
37
  import { ConfigurationService } from "../services/configurationService.js";
38
- import { getCurrentWorktreeSession } from "../utils/worktreeSession.js";
38
+ import type { WorktreeSession } from "../utils/worktreeSession.js";
39
39
 
40
40
  const SAFE_COMMANDS = [
41
41
  "cd",
@@ -452,8 +452,11 @@ export class PermissionManager {
452
452
  }
453
453
 
454
454
  // 1.0 Check worktree safety for Write and Edit tools
455
- // Support both CLI -w sessions (container-registered) and EnterWorktree mid-session (module-level)
456
- const worktreeSession = getCurrentWorktreeSession();
455
+ // Support both CLI -w sessions (container-registered) and EnterWorktree mid-session
456
+ // (per-agent WorktreeSession stored in this session's container)
457
+ const worktreeSession = this.container.get<WorktreeSession | null>(
458
+ "WorktreeSession",
459
+ );
457
460
  const effectiveWorktreeName =
458
461
  this.worktreeName || worktreeSession?.worktreeName;
459
462
  const effectiveWorkdir = this.workdir || worktreeSession?.worktreePath;
@@ -7,7 +7,13 @@ import type { Message, Usage } from "../types/index.js";
7
7
  import { AIManager } from "./aiManager.js";
8
8
  import { MessageManager } from "./messageManager.js";
9
9
  import { ToolManager } from "./toolManager.js";
10
- import { AGENT_TOOL_NAME } from "../constants/tools.js";
10
+ import {
11
+ AGENT_TOOL_NAME,
12
+ TASK_CREATE_TOOL_NAME,
13
+ TASK_GET_TOOL_NAME,
14
+ TASK_LIST_TOOL_NAME,
15
+ TASK_UPDATE_TOOL_NAME,
16
+ } from "../constants/tools.js";
11
17
  import {
12
18
  addConsolidatedAbortListener,
13
19
  createAbortPromise,
@@ -321,6 +327,12 @@ export class SubagentManager {
321
327
  instanceDeniedRules: [
322
328
  ...(parentPermissionManager?.getInstanceDeniedRules?.() || []),
323
329
  AGENT_TOOL_NAME, // Always deny Agent tool in subagents to prevent recursion
330
+ // Deny Task tools in subagents — they share the parent's TaskManager
331
+ // and could mutate the main agent's task list
332
+ TASK_CREATE_TOOL_NAME,
333
+ TASK_GET_TOOL_NAME,
334
+ TASK_UPDATE_TOOL_NAME,
335
+ TASK_LIST_TOOL_NAME,
324
336
  ],
325
337
  additionalDirectories:
326
338
  parentPermissionManager?.getAdditionalDirectories(),
@@ -1,7 +1,7 @@
1
1
  import * as os from "node:os";
2
2
  import { ToolPlugin } from "../tools/types.js";
3
3
  import { isGitRepository } from "../utils/gitUtils.js";
4
- import { getCurrentWorktreeSession } from "../utils/worktreeSession.js";
4
+ import type { WorktreeSession } from "../utils/worktreeSession.js";
5
5
  import { buildAutoMemoryPrompt } from "./autoMemory.js";
6
6
  import {
7
7
  EXPLORE_SUBAGENT_TYPE,
@@ -252,6 +252,7 @@ export function buildSystemPrompt(
252
252
  originalWorkdir?: string;
253
253
  language?: string;
254
254
  isSubagent?: boolean;
255
+ worktreeSession?: WorktreeSession | null;
255
256
  autoMemory?: {
256
257
  directory: string;
257
258
  content: string;
@@ -292,7 +293,7 @@ export function buildSystemPrompt(
292
293
  ? "bash"
293
294
  : shell;
294
295
 
295
- const worktreeSession = getCurrentWorktreeSession();
296
+ const worktreeSession = options.worktreeSession;
296
297
 
297
298
  dynamicText += `
298
299
 
@@ -326,6 +327,7 @@ export function enhanceSystemPromptWithEnvDetails(
326
327
  existingSystemPrompt: string,
327
328
  workdir: string,
328
329
  originalWorkdir?: string,
330
+ worktreeSession?: WorktreeSession | null,
329
331
  ): string {
330
332
  const isGitRepo = isGitRepository(workdir);
331
333
  const platform = os.platform();
@@ -338,8 +340,6 @@ export function enhanceSystemPromptWithEnvDetails(
338
340
  ? "bash"
339
341
  : shell;
340
342
 
341
- const worktreeSession = getCurrentWorktreeSession();
342
-
343
343
  const notes = `Notes:
344
344
  - Agent threads always have their cwd reset between bash calls, as a result please only use absolute file paths.${worktreeSession ? `\n- You are in a git worktree at ${worktreeSession.worktreePath} (branch: ${worktreeSession.worktreeBranch}). Absolute paths from prior context may refer to the original repo at ${worktreeSession.originalCwd}; translate them to your worktree. Do NOT edit files outside this worktree.` : ""}
345
345
  - In your final response, share file paths (always absolute, never relative) that are relevant to the task. Include code snippets only when the exact text is load-bearing (e.g., a bug you found, a function signature the caller asked for) — do not recap code you merely read.
@@ -120,6 +120,15 @@ async function buildHookJsonInput(
120
120
  jsonInput.session_crons = context.sessionCrons ?? [];
121
121
  }
122
122
 
123
+ // Add last_assistant_message for Stop and SubagentStop events.
124
+ // Omitted when undefined (no text content in last assistant message).
125
+ if (
126
+ (context.event === "Stop" || context.event === "SubagentStop") &&
127
+ context.lastAssistantMessage !== undefined
128
+ ) {
129
+ jsonInput.last_assistant_message = context.lastAssistantMessage;
130
+ }
131
+
123
132
  return jsonInput;
124
133
  }
125
134
 
@@ -173,14 +173,16 @@ The working directory persists between commands. Try to maintain your current wo
173
173
  };
174
174
  }
175
175
 
176
- // Resolve shell path: on Windows, use Git Bash; on other platforms, use default
176
+ // Resolve shell path: on Windows, use Git Bash; on macOS/Linux, use bash or zsh
177
177
  const shellPath = resolveShellPath();
178
- if (process.platform === "win32" && !shellPath) {
178
+ if (!shellPath) {
179
179
  return {
180
180
  success: false,
181
181
  content: "",
182
182
  error:
183
- "Git Bash not found. Please install Git for Windows or set GIT_BASH_PATH environment variable.",
183
+ process.platform === "win32"
184
+ ? "Git Bash not found. Please install Git for Windows or set WAVE_GIT_BASH_PATH environment variable."
185
+ : "No suitable shell found. Please ensure bash or zsh is installed, or set WAVE_SHELL environment variable.",
184
186
  };
185
187
  }
186
188
 
@@ -242,7 +244,11 @@ The working directory persists between commands. Try to maintain your current wo
242
244
  };
243
245
  }
244
246
 
245
- const { id: taskId } = backgroundTaskManager.startShell(command);
247
+ const { id: taskId } = backgroundTaskManager.startShell(
248
+ command,
249
+ undefined,
250
+ context.workdir,
251
+ );
246
252
  const task = backgroundTaskManager.getTask(taskId);
247
253
  const outputPath = task?.outputPath;
248
254
  const backgroundMsg = [
@@ -273,7 +279,7 @@ The working directory persists between commands. Try to maintain your current wo
273
279
  );
274
280
 
275
281
  const child: ChildProcess = spawn(wrappedCommand, {
276
- shell: shellPath || true,
282
+ shell: shellPath,
277
283
  stdio: "pipe",
278
284
  detached: true,
279
285
  cwd: context.workdir,
@@ -4,11 +4,7 @@
4
4
  */
5
5
 
6
6
  import type { ToolPlugin, ToolResult, ToolContext } from "./types.js";
7
- import {
8
- getCurrentWorktreeSession,
9
- setCurrentWorktreeSession,
10
- type WorktreeSession,
11
- } from "../utils/worktreeSession.js";
7
+ import { type WorktreeSession } from "../utils/worktreeSession.js";
12
8
  import {
13
9
  createWorktree,
14
10
  validateWorktreeName,
@@ -72,7 +68,7 @@ export const enterWorktreeTool: ToolPlugin = {
72
68
  context: ToolContext,
73
69
  ): Promise<ToolResult> {
74
70
  // Validate not already in a worktree created by this session
75
- if (getCurrentWorktreeSession()) {
71
+ if (context.aiManager?.getWorktreeSession()) {
76
72
  return {
77
73
  success: false,
78
74
  content:
@@ -119,19 +115,13 @@ export const enterWorktreeTool: ToolPlugin = {
119
115
  originalHeadCommit: worktreeInfo.originalHeadCommit,
120
116
  };
121
117
 
122
- // Set module-level session state
123
- setCurrentWorktreeSession(session);
124
-
125
- // Update CWD via AIManager
118
+ // Set per-session worktree state and update CWD via AIManager
126
119
  const aiManager = context.aiManager;
127
120
  if (aiManager) {
121
+ aiManager.setWorktreeSession(session);
128
122
  aiManager.setWorkdir(worktreeInfo.path);
129
123
  }
130
124
 
131
- // Also update the container's Workdir entry
132
- // (Container is not directly accessible from ToolContext, but AIManager.setWorkdir
133
- // handles both its internal field and process.chdir)
134
-
135
125
  // Trigger WorktreeCreate hook if worktree is new
136
126
  let hookTriggered = false;
137
127
  if (session.isNew && context.hookManager) {
@@ -4,10 +4,6 @@
4
4
  */
5
5
 
6
6
  import type { ToolPlugin, ToolResult, ToolContext } from "./types.js";
7
- import {
8
- getCurrentWorktreeSession,
9
- setCurrentWorktreeSession,
10
- } from "../utils/worktreeSession.js";
11
7
  import {
12
8
  removeWorktree,
13
9
  countWorktreeChanges,
@@ -89,8 +85,8 @@ export const exitWorktreeTool: ToolPlugin = {
89
85
  };
90
86
  }
91
87
 
92
- // Validate: must be in an active worktree session
93
- const session = getCurrentWorktreeSession();
88
+ // Validate: must be in an active worktree session (for this session)
89
+ const session = context.aiManager?.getWorktreeSession();
94
90
  if (!session) {
95
91
  return {
96
92
  success: false,
@@ -139,11 +135,9 @@ export const exitWorktreeTool: ToolPlugin = {
139
135
 
140
136
  if (action === "keep") {
141
137
  // Clear session state
142
- setCurrentWorktreeSession(null);
143
-
144
- // Restore CWD
145
138
  const aiManager = context.aiManager;
146
139
  if (aiManager) {
140
+ aiManager.setWorktreeSession(null);
147
141
  aiManager.setWorkdir(originalCwd);
148
142
  }
149
143
 
@@ -170,12 +164,10 @@ export const exitWorktreeTool: ToolPlugin = {
170
164
 
171
165
  removeWorktree(worktreeInfo);
172
166
 
173
- // Clear session state
174
- setCurrentWorktreeSession(null);
175
-
176
- // Restore CWD
167
+ // Clear session state and restore CWD
177
168
  const aiManager = context.aiManager;
178
169
  if (aiManager) {
170
+ aiManager.setWorktreeSession(null);
179
171
  aiManager.setWorkdir(originalCwd);
180
172
  }
181
173
 
@@ -198,6 +198,7 @@ export interface HookJsonInput {
198
198
  compact_summary?: string; // Present for PostCompact events
199
199
  background_tasks?: BackgroundTaskInfo[]; // Present for Stop events: running background tasks snapshot
200
200
  session_crons?: SessionCronInfo[]; // Present for Stop events: session-scoped cron jobs snapshot
201
+ last_assistant_message?: string; // Present for Stop and SubagentStop events: text content of the last assistant message
201
202
  }
202
203
 
203
204
  // Describes one in-flight background task in the Stop hook input.
@@ -245,6 +246,7 @@ export interface ExtendedHookExecutionContext extends HookExecutionContext {
245
246
  compactSummary?: string; // Summary text for PostCompact
246
247
  backgroundTasks?: BackgroundTaskInfo[]; // Running background tasks snapshot (Stop only)
247
248
  sessionCrons?: SessionCronInfo[]; // Session-scoped cron jobs snapshot (Stop only)
249
+ lastAssistantMessage?: string; // Text content of last assistant message (Stop and SubagentStop only)
248
250
  }
249
251
 
250
252
  // Environment variables injected into hook processes
@@ -95,6 +95,8 @@ export interface ReasoningBlock {
95
95
  type: "reasoning";
96
96
  content: string;
97
97
  stage?: "streaming" | "end";
98
+ startTime?: number; // Unix ms, set when the first reasoning content arrives
99
+ endTime?: number; // Unix ms, set when stage transitions to "end"
98
100
  }
99
101
 
100
102
  export interface FileHistoryBlock {
@@ -88,6 +88,10 @@ export function setupAgentContainer(
88
88
  container.register("MainRepoRoot", getGitMainRepoRoot(workdir));
89
89
  }
90
90
 
91
+ // Per-agent worktree session state (EnterWorktree/ExitWorktree). Stored in the
92
+ // container so it is isolated per session, not shared process-wide.
93
+ container.register("WorktreeSession", null);
94
+
91
95
  const notificationQueue = new NotificationQueue();
92
96
  container.register("NotificationQueue", notificationQueue);
93
97