ccmanager 4.1.5 → 4.1.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.
@@ -1,8 +1,13 @@
1
1
  import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
2
2
  import { EventEmitter } from 'events';
3
3
  import { spawn } from './bunTerminal.js';
4
- import { STATE_PERSISTENCE_DURATION_MS, STATE_MINIMUM_DURATION_MS, } from '../constants/statePersistence.js';
5
4
  import { Effect, Either } from 'effect';
5
+ /**
6
+ * Must match `STATE_CHECK_INTERVAL_MS` in sessionManager.ts.
7
+ * State updates are async; the next interval tick sees the new state and runs auto-approval.
8
+ */
9
+ const STATE_CHECK_INTERVAL_MS = 100;
10
+ const STATE_DETECTION_TICKS_FOR_ASYNC_UPDATE = 2;
6
11
  const detectStateMock = vi.fn();
7
12
  // Create a deferred promise pattern for controllable mock
8
13
  let verifyResolve = null;
@@ -139,17 +144,17 @@ describe('SessionManager - Auto Approval Recovery', () => {
139
144
  }));
140
145
  // Phase 1: waiting_input (auto-approval suppressed due to prior failure)
141
146
  detectStateMock.mockReturnValue('waiting_input');
142
- await vi.advanceTimersByTimeAsync(STATE_MINIMUM_DURATION_MS + STATE_PERSISTENCE_DURATION_MS);
147
+ await vi.advanceTimersByTimeAsync(STATE_CHECK_INTERVAL_MS * STATE_DETECTION_TICKS_FOR_ASYNC_UPDATE);
143
148
  expect(session.stateMutex.getSnapshot().state).toBe('waiting_input');
144
149
  expect(session.stateMutex.getSnapshot().autoApprovalFailed).toBe(true);
145
150
  // Phase 2: busy - should reset the failure flag
146
151
  detectStateMock.mockReturnValue('busy');
147
- await vi.advanceTimersByTimeAsync(STATE_MINIMUM_DURATION_MS + STATE_PERSISTENCE_DURATION_MS);
152
+ await vi.advanceTimersByTimeAsync(STATE_CHECK_INTERVAL_MS * STATE_DETECTION_TICKS_FOR_ASYNC_UPDATE);
148
153
  expect(session.stateMutex.getSnapshot().state).toBe('busy');
149
154
  expect(session.stateMutex.getSnapshot().autoApprovalFailed).toBe(false);
150
155
  // Phase 3: waiting_input again - should trigger pending_auto_approval
151
156
  detectStateMock.mockReturnValue('waiting_input');
152
- await vi.advanceTimersByTimeAsync(STATE_MINIMUM_DURATION_MS + STATE_PERSISTENCE_DURATION_MS);
157
+ await vi.advanceTimersByTimeAsync(STATE_CHECK_INTERVAL_MS * STATE_DETECTION_TICKS_FOR_ASYNC_UPDATE);
153
158
  // State should now be pending_auto_approval (waiting for verification)
154
159
  expect(session.stateMutex.getSnapshot().state).toBe('pending_auto_approval');
155
160
  expect(verifyNeedsPermissionMock).toHaveBeenCalled();
@@ -165,8 +170,6 @@ describe('SessionManager - Auto Approval Recovery', () => {
165
170
  ...data,
166
171
  state: 'pending_auto_approval',
167
172
  autoApprovalAbortController: abortController,
168
- pendingState: 'pending_auto_approval',
169
- pendingStateStart: Date.now(),
170
173
  }));
171
174
  const handler = vi.fn();
172
175
  sessionManager.on('sessionStateChanged', handler);
@@ -181,7 +184,6 @@ describe('SessionManager - Auto Approval Recovery', () => {
181
184
  expect(stateData.autoApprovalAbortController).toBeUndefined();
182
185
  expect(stateData.autoApprovalFailed).toBe(true);
183
186
  expect(stateData.state).toBe('waiting_input');
184
- expect(stateData.pendingState).toBeUndefined();
185
187
  expect(handler).toHaveBeenCalledWith(session);
186
188
  sessionManager.off('sessionStateChanged', handler);
187
189
  });
@@ -193,7 +195,7 @@ describe('SessionManager - Auto Approval Recovery', () => {
193
195
  sessionManager.on('sessionStateChanged', handler);
194
196
  // Phase 1: waiting_input → pending_auto_approval
195
197
  detectStateMock.mockReturnValue('waiting_input');
196
- await vi.advanceTimersByTimeAsync(STATE_MINIMUM_DURATION_MS + STATE_PERSISTENCE_DURATION_MS);
198
+ await vi.advanceTimersByTimeAsync(STATE_CHECK_INTERVAL_MS * STATE_DETECTION_TICKS_FOR_ASYNC_UPDATE);
197
199
  // State should be pending_auto_approval (waiting for verification)
198
200
  expect(session.stateMutex.getSnapshot().state).toBe('pending_auto_approval');
199
201
  expect(verifyNeedsPermissionMock).toHaveBeenCalled();
@@ -204,8 +206,6 @@ describe('SessionManager - Auto Approval Recovery', () => {
204
206
  await vi.waitFor(() => {
205
207
  expect(session.stateMutex.getSnapshot().state).toBe('busy');
206
208
  });
207
- expect(session.stateMutex.getSnapshot().pendingState).toBeUndefined();
208
- expect(session.stateMutex.getSnapshot().pendingStateStart).toBeUndefined();
209
209
  // Verify Enter key was sent to approve
210
210
  expect(mockPty.write).toHaveBeenCalledWith('\r');
211
211
  // Verify sessionStateChanged was emitted with session containing state=busy
@@ -6,7 +6,8 @@ import { spawn as childSpawn } from 'child_process';
6
6
  import { configReader } from './config/configReader.js';
7
7
  import { executeStatusHook } from '../utils/hookExecutor.js';
8
8
  import { createStateDetector } from './stateDetector/index.js';
9
- import { STATE_PERSISTENCE_DURATION_MS, STATE_CHECK_INTERVAL_MS, STATE_MINIMUM_DURATION_MS, } from '../constants/statePersistence.js';
9
+ /** Interval in milliseconds for polling terminal state detection. */
10
+ const STATE_CHECK_INTERVAL_MS = 100;
10
11
  import { Effect, Either } from 'effect';
11
12
  import { ProcessError, ConfigError } from '../types/errors.js';
12
13
  import { autoApprovalVerifier } from './autoApprovalVerifier.js';
@@ -176,9 +177,6 @@ export class SessionManager extends EventEmitter {
176
177
  await session.stateMutex.update(data => ({
177
178
  ...data,
178
179
  state: newState,
179
- pendingState: undefined,
180
- pendingStateStart: undefined,
181
- stateConfirmedAt: Date.now(),
182
180
  ...additionalUpdates,
183
181
  }));
184
182
  if (oldState !== newState) {
@@ -361,60 +359,27 @@ export class SessionManager extends EventEmitter {
361
359
  setupBackgroundHandler(session) {
362
360
  // Setup data handler
363
361
  this.setupDataHandler(session);
364
- // Set up interval-based state detection with persistence
362
+ // Set up interval-based state detection
365
363
  session.stateCheckInterval = setInterval(() => {
366
364
  const stateData = session.stateMutex.getSnapshot();
367
365
  const oldState = stateData.state;
368
366
  const detectedState = this.detectTerminalState(session);
369
- const now = Date.now();
370
- // If detected state is different from current state
371
367
  if (detectedState !== oldState) {
372
- // If this is a new pending state or the pending state changed
373
- if (stateData.pendingState !== detectedState) {
374
- void session.stateMutex.update(data => ({
375
- ...data,
376
- pendingState: detectedState,
377
- pendingStateStart: now,
378
- }));
368
+ // Cancel auto-approval verification if state is changing away from pending_auto_approval
369
+ if (stateData.autoApprovalAbortController &&
370
+ detectedState !== 'pending_auto_approval') {
371
+ this.cancelAutoApprovalVerification(session, `state changed to ${detectedState}`);
379
372
  }
380
- else if (stateData.pendingState !== undefined &&
381
- stateData.pendingStateStart !== undefined) {
382
- // Check if the pending state has persisted long enough
383
- // and that the current state has been active for the minimum duration
384
- const duration = now - stateData.pendingStateStart;
385
- const timeInCurrentState = now - stateData.stateConfirmedAt;
386
- if (duration >= STATE_PERSISTENCE_DURATION_MS &&
387
- timeInCurrentState >= STATE_MINIMUM_DURATION_MS) {
388
- // Cancel auto-approval verification if state is changing away from pending_auto_approval
389
- if (stateData.autoApprovalAbortController &&
390
- detectedState !== 'pending_auto_approval') {
391
- this.cancelAutoApprovalVerification(session, `state changed to ${detectedState}`);
392
- }
393
- // Build additional updates for auto-approval reset
394
- const additionalUpdates = {};
395
- // If we previously blocked auto-approval and have moved out of a user prompt,
396
- // allow future auto-approval attempts.
397
- if (stateData.autoApprovalFailed &&
398
- detectedState !== 'waiting_input' &&
399
- detectedState !== 'pending_auto_approval') {
400
- additionalUpdates.autoApprovalFailed = false;
401
- additionalUpdates.autoApprovalReason = undefined;
402
- }
403
- // Confirm the state change with hook execution
404
- void this.updateSessionState(session, detectedState, additionalUpdates);
405
- }
373
+ const additionalUpdates = {};
374
+ // If we previously blocked auto-approval and have moved out of a user prompt,
375
+ // allow future auto-approval attempts.
376
+ if (stateData.autoApprovalFailed &&
377
+ detectedState !== 'waiting_input' &&
378
+ detectedState !== 'pending_auto_approval') {
379
+ additionalUpdates.autoApprovalFailed = false;
380
+ additionalUpdates.autoApprovalReason = undefined;
406
381
  }
407
- }
408
- else {
409
- // Detected state matches current state, clear any pending state
410
- // and update stateConfirmedAt so the minimum duration guard
411
- // tracks "last time current state was seen" rather than "first confirmed"
412
- void session.stateMutex.update(data => ({
413
- ...data,
414
- pendingState: undefined,
415
- pendingStateStart: undefined,
416
- stateConfirmedAt: now,
417
- }));
382
+ void this.updateSessionState(session, detectedState, additionalUpdates);
418
383
  }
419
384
  // Handle auto-approval if state is pending_auto_approval and no verification is in progress.
420
385
  // This ensures auto-approval is retried when the state remains pending_auto_approval
@@ -502,8 +467,6 @@ export class SessionManager extends EventEmitter {
502
467
  ...data,
503
468
  autoApprovalFailed: true,
504
469
  autoApprovalReason: reason,
505
- pendingState: undefined,
506
- pendingStateStart: undefined,
507
470
  }));
508
471
  }
509
472
  }
@@ -3,8 +3,9 @@ import { Either } from 'effect';
3
3
  import { SessionManager } from './sessionManager.js';
4
4
  import { spawn } from './bunTerminal.js';
5
5
  import { EventEmitter } from 'events';
6
- import { STATE_PERSISTENCE_DURATION_MS, STATE_CHECK_INTERVAL_MS, STATE_MINIMUM_DURATION_MS, } from '../constants/statePersistence.js';
7
6
  import { IDLE_DEBOUNCE_MS } from './stateDetector/claude.js';
7
+ /** Must match `STATE_CHECK_INTERVAL_MS` in sessionManager.ts */
8
+ const STATE_CHECK_INTERVAL_MS = 100;
8
9
  vi.mock('./bunTerminal.js', () => ({
9
10
  spawn: vi.fn(function () {
10
11
  return null;
@@ -41,7 +42,7 @@ vi.mock('./config/configReader.js', () => ({
41
42
  setAutoApprovalEnabled: vi.fn(),
42
43
  },
43
44
  }));
44
- describe('SessionManager - State Persistence', () => {
45
+ describe('SessionManager - state detection', () => {
45
46
  let sessionManager;
46
47
  let mockPtyInstances;
47
48
  let eventEmitters;
@@ -50,7 +51,6 @@ describe('SessionManager - State Persistence', () => {
50
51
  sessionManager = new SessionManager();
51
52
  mockPtyInstances = new Map();
52
53
  eventEmitters = new Map();
53
- // Create mock PTY process factory
54
54
  spawn.mockImplementation((command, args, options) => {
55
55
  const path = options.cwd;
56
56
  const eventEmitter = new EventEmitter();
@@ -79,211 +79,35 @@ describe('SessionManager - State Persistence', () => {
79
79
  vi.useRealTimers();
80
80
  vi.clearAllMocks();
81
81
  });
82
- it('should not change state immediately when detected state changes', async () => {
82
+ it('transitions busy to idle after idle debounce and the next poll', async () => {
83
83
  const { Effect } = await import('effect');
84
84
  const session = await Effect.runPromise(sessionManager.createSessionWithPresetEffect('/test/path'));
85
85
  const eventEmitter = eventEmitters.get('/test/path');
86
- // Initial state should be busy
87
86
  expect(session.stateMutex.getSnapshot().state).toBe('busy');
88
- // Simulate output that would trigger idle state
89
87
  eventEmitter.emit('data', 'Some output without busy indicators');
90
- // Advance time past idle debounce so detector starts returning idle,
91
- // but not enough for persistence to confirm
92
88
  await vi.advanceTimersByTimeAsync(IDLE_DEBOUNCE_MS + STATE_CHECK_INTERVAL_MS);
93
- // State should still be busy, but pending state should be set
94
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
95
- expect(session.stateMutex.getSnapshot().pendingState).toBe('idle');
96
- expect(session.stateMutex.getSnapshot().pendingStateStart).toBeDefined();
97
- });
98
- it('should change state after both persistence and minimum duration are met', async () => {
99
- const { Effect } = await import('effect');
100
- const session = await Effect.runPromise(sessionManager.createSessionWithPresetEffect('/test/path'));
101
- const eventEmitter = eventEmitters.get('/test/path');
102
- const stateChangeHandler = vi.fn();
103
- sessionManager.on('sessionStateChanged', stateChangeHandler);
104
- // Initial state should be busy
105
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
106
- // Simulate output that would trigger idle state
107
- eventEmitter.emit('data', 'Some output without busy indicators');
108
- // Advance time past idle debounce but not persistence+minimum
109
- await vi.advanceTimersByTimeAsync(IDLE_DEBOUNCE_MS + STATE_CHECK_INTERVAL_MS);
110
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
111
- expect(stateChangeHandler).not.toHaveBeenCalled();
112
- // Advance time past both persistence and minimum duration
113
- await vi.advanceTimersByTimeAsync(STATE_MINIMUM_DURATION_MS);
114
- // State should now be changed
115
89
  expect(session.stateMutex.getSnapshot().state).toBe('idle');
116
- expect(session.stateMutex.getSnapshot().pendingState).toBeUndefined();
117
- expect(session.stateMutex.getSnapshot().pendingStateStart).toBeUndefined();
118
- expect(stateChangeHandler).toHaveBeenCalledWith(session);
119
90
  });
120
- it('should cancel pending state if detected state changes again before persistence', async () => {
91
+ it('transitions busy to waiting_input on the next poll without idle debounce', async () => {
121
92
  const { Effect } = await import('effect');
122
93
  const session = await Effect.runPromise(sessionManager.createSessionWithPresetEffect('/test/path'));
123
94
  const eventEmitter = eventEmitters.get('/test/path');
124
- // Initial state should be busy
125
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
126
- // Simulate output that would trigger idle state
127
- eventEmitter.emit('data', 'Some output without busy indicators');
128
- // Advance time past idle debounce so detector starts returning idle
129
- await vi.advanceTimersByTimeAsync(IDLE_DEBOUNCE_MS + STATE_CHECK_INTERVAL_MS);
130
- expect(session.stateMutex.getSnapshot().pendingState).toBe('idle');
131
- // Simulate output that would trigger waiting_input state
132
95
  eventEmitter.emit('data', 'Do you want to continue?\n❯ 1. Yes');
133
- // Advance time to trigger another check (use async to process mutex updates)
134
- await vi.advanceTimersByTimeAsync(STATE_CHECK_INTERVAL_MS);
135
- // Pending state should now be waiting_input, not idle
136
- expect(session.stateMutex.getSnapshot().state).toBe('busy'); // Still original state
137
- expect(session.stateMutex.getSnapshot().pendingState).toBe('waiting_input');
138
- });
139
- it('should clear pending state if detected state returns to current state', async () => {
140
- const { Effect } = await import('effect');
141
- const session = await Effect.runPromise(sessionManager.createSessionWithPresetEffect('/test/path'));
142
- const eventEmitter = eventEmitters.get('/test/path');
143
- // Initial state should be busy
144
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
145
- // Simulate output that would trigger idle state
146
- eventEmitter.emit('data', 'Some output without busy indicators');
147
- // Advance time past idle debounce so detector starts returning idle
148
- await vi.advanceTimersByTimeAsync(IDLE_DEBOUNCE_MS + STATE_CHECK_INTERVAL_MS);
149
- expect(session.stateMutex.getSnapshot().pendingState).toBe('idle');
150
- expect(session.stateMutex.getSnapshot().pendingStateStart).toBeDefined();
151
- // Simulate output that would trigger busy state again (back to original)
152
- eventEmitter.emit('data', 'ESC to interrupt');
153
- // Advance time to trigger another check (use async to process mutex updates)
154
96
  await vi.advanceTimersByTimeAsync(STATE_CHECK_INTERVAL_MS);
155
- // Pending state should be cleared
156
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
157
- expect(session.stateMutex.getSnapshot().pendingState).toBeUndefined();
158
- expect(session.stateMutex.getSnapshot().pendingStateStart).toBeUndefined();
97
+ expect(session.stateMutex.getSnapshot().state).toBe('waiting_input');
159
98
  });
160
- it('should not confirm state changes that do not persist long enough', async () => {
161
- const { Effect } = await import('effect');
162
- const session = await Effect.runPromise(sessionManager.createSessionWithPresetEffect('/test/path'));
163
- const eventEmitter = eventEmitters.get('/test/path');
164
- const stateChangeHandler = vi.fn();
165
- sessionManager.on('sessionStateChanged', stateChangeHandler);
166
- // Initial state should be busy
167
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
168
- // Try to change to idle
169
- eventEmitter.emit('data', 'Some idle output\n');
170
- // Wait past idle debounce so detector returns idle, then one check
171
- await vi.advanceTimersByTimeAsync(IDLE_DEBOUNCE_MS + STATE_CHECK_INTERVAL_MS);
172
- // Should have pending state but not confirmed
173
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
174
- expect(session.stateMutex.getSnapshot().pendingState).toBe('idle');
175
- // Now change to a different state before idle persists
176
- // Clear terminal first and add waiting prompt
177
- eventEmitter.emit('data', '\x1b[2J\x1b[HDo you want to continue?\n❯ 1. Yes');
178
- // Advance time to detect new state
179
- await vi.advanceTimersByTimeAsync(STATE_CHECK_INTERVAL_MS);
180
- // Pending state should have changed to waiting_input
181
- expect(session.stateMutex.getSnapshot().state).toBe('busy'); // Still original state
182
- expect(session.stateMutex.getSnapshot().pendingState).toBe('waiting_input');
183
- // Since states kept changing before persisting, no state change should have been confirmed
184
- expect(stateChangeHandler).not.toHaveBeenCalled();
185
- });
186
- it('should properly clean up pending state when session is destroyed', async () => {
187
- const { Effect } = await import('effect');
188
- const session = await Effect.runPromise(sessionManager.createSessionWithPresetEffect('/test/path'));
189
- const eventEmitter = eventEmitters.get('/test/path');
190
- // Simulate output that would trigger idle state
191
- eventEmitter.emit('data', 'Some output without busy indicators');
192
- // Advance time past idle debounce so detector returns idle
193
- await vi.advanceTimersByTimeAsync(IDLE_DEBOUNCE_MS + STATE_CHECK_INTERVAL_MS);
194
- expect(session.stateMutex.getSnapshot().pendingState).toBe('idle');
195
- expect(session.stateMutex.getSnapshot().pendingStateStart).toBeDefined();
196
- // Destroy the session
197
- sessionManager.destroySession(session.id);
198
- // Check that pending state is cleared
199
- const remainingSessions = sessionManager.getSessionsForWorktree('/test/path');
200
- expect(remainingSessions).toHaveLength(0);
201
- });
202
- it('should not transition state before minimum duration in current state has elapsed', async () => {
203
- const { Effect } = await import('effect');
204
- const session = await Effect.runPromise(sessionManager.createSessionWithPresetEffect('/test/path'));
205
- const eventEmitter = eventEmitters.get('/test/path');
206
- const stateChangeHandler = vi.fn();
207
- sessionManager.on('sessionStateChanged', stateChangeHandler);
208
- // Initial state should be busy
209
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
210
- // Simulate output that would trigger idle state
211
- eventEmitter.emit('data', 'Some output without busy indicators');
212
- // Advance past idle debounce but not past persistence + minimum duration
213
- await vi.advanceTimersByTimeAsync(IDLE_DEBOUNCE_MS +
214
- STATE_PERSISTENCE_DURATION_MS -
215
- STATE_CHECK_INTERVAL_MS);
216
- // State should still be busy because minimum duration hasn't elapsed
217
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
218
- expect(stateChangeHandler).not.toHaveBeenCalled();
219
- });
220
- it('should transition state after both persistence and minimum duration are met', async () => {
221
- const { Effect } = await import('effect');
222
- const session = await Effect.runPromise(sessionManager.createSessionWithPresetEffect('/test/path'));
223
- const eventEmitter = eventEmitters.get('/test/path');
224
- const stateChangeHandler = vi.fn();
225
- sessionManager.on('sessionStateChanged', stateChangeHandler);
226
- // Initial state should be busy
227
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
228
- // Simulate output that would trigger idle state
229
- eventEmitter.emit('data', 'Some output without busy indicators');
230
- // Advance past idle debounce + persistence/minimum duration
231
- await vi.advanceTimersByTimeAsync(IDLE_DEBOUNCE_MS + STATE_MINIMUM_DURATION_MS + STATE_CHECK_INTERVAL_MS);
232
- // State should now be idle since both durations are satisfied
233
- expect(session.stateMutex.getSnapshot().state).toBe('idle');
234
- expect(stateChangeHandler).toHaveBeenCalledWith(session);
235
- });
236
- it('should not transition during brief screen redraw even after long time in current state', async () => {
237
- const { Effect } = await import('effect');
238
- const session = await Effect.runPromise(sessionManager.createSessionWithPresetEffect('/test/path'));
239
- const eventEmitter = eventEmitters.get('/test/path');
240
- const stateChangeHandler = vi.fn();
241
- sessionManager.on('sessionStateChanged', stateChangeHandler);
242
- // Initial state should be busy
243
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
244
- // Keep busy state active for a long time (simulating normal operation)
245
- // Each check re-detects "busy" and updates stateConfirmedAt
246
- eventEmitter.emit('data', 'ESC to interrupt');
247
- await vi.advanceTimersByTimeAsync(2000); // 2 seconds of confirmed busy
248
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
249
- // Now simulate a brief screen redraw: busy indicators disappear temporarily
250
- eventEmitter.emit('data', '\x1b[2J\x1b[H'); // Clear screen
251
- // Idle debounce prevents the detector from returning idle for IDLE_DEBOUNCE_MS,
252
- // so during a brief redraw (< 1500ms), no pending idle state is set.
253
- // Advance a short time — still within the idle debounce window
254
- await vi.advanceTimersByTimeAsync(STATE_CHECK_INTERVAL_MS * 3); // 300ms
255
- // State should still be busy — idle debounce hasn't elapsed
256
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
257
- expect(stateChangeHandler).not.toHaveBeenCalled();
258
- // Simulate busy indicators coming back (screen redraw complete)
259
- eventEmitter.emit('data', 'ESC to interrupt');
260
- await vi.advanceTimersByTimeAsync(STATE_CHECK_INTERVAL_MS);
261
- // State should still be busy and pending should be cleared
262
- expect(session.stateMutex.getSnapshot().state).toBe('busy');
263
- expect(session.stateMutex.getSnapshot().pendingState).toBeUndefined();
264
- expect(stateChangeHandler).not.toHaveBeenCalled();
265
- });
266
- it('should handle multiple sessions with independent state persistence', async () => {
99
+ it('handles multiple sessions independently', async () => {
267
100
  const { Effect } = await import('effect');
268
101
  const session1 = await Effect.runPromise(sessionManager.createSessionWithPresetEffect('/test/path1'));
269
102
  const session2 = await Effect.runPromise(sessionManager.createSessionWithPresetEffect('/test/path2'));
270
103
  const eventEmitter1 = eventEmitters.get('/test/path1');
271
104
  const eventEmitter2 = eventEmitters.get('/test/path2');
272
- // Both should start as busy
273
105
  expect(session1.stateMutex.getSnapshot().state).toBe('busy');
274
106
  expect(session2.stateMutex.getSnapshot().state).toBe('busy');
275
- // Simulate different outputs for each session
276
- // Session 1 goes to idle
277
107
  eventEmitter1.emit('data', 'Idle output for session 1');
278
- // Session 2 goes to waiting_input (no idle debounce for waiting_input)
279
108
  eventEmitter2.emit('data', 'Do you want to continue?\n❯ 1. Yes');
280
- // Advance past idle debounce for session 1 + persistence/minimum for both
281
- await vi.advanceTimersByTimeAsync(IDLE_DEBOUNCE_MS + STATE_MINIMUM_DURATION_MS + STATE_CHECK_INTERVAL_MS);
282
- // Both should now be in their new states
283
- // Session 2 (waiting_input) transitions faster since it's not debounced
109
+ await vi.advanceTimersByTimeAsync(IDLE_DEBOUNCE_MS + STATE_CHECK_INTERVAL_MS);
284
110
  expect(session1.stateMutex.getSnapshot().state).toBe('idle');
285
- expect(session1.stateMutex.getSnapshot().pendingState).toBeUndefined();
286
111
  expect(session2.stateMutex.getSnapshot().state).toBe('waiting_input');
287
- expect(session2.stateMutex.getSnapshot().pendingState).toBeUndefined();
288
112
  });
289
113
  });
@@ -1,8 +1,11 @@
1
1
  import { BaseStateDetector } from './base.js';
2
- // Spinner characters used by Claude Code during active processing
3
- const SPINNER_CHARS = '✱✲✳✴✵✶✷✸✹✺✻✼✽✾✿❀❁❂❃❇❈❉❊❋✢✣✤✥✦✧✨⊛⊕⊙◉◎◍⁂⁕※⍟☼★☆';
4
- // Matches spinner activity labels like "✽ Tempering…" or "✳ Simplifying recompute_tangents…"
2
+ // Spinner / activity-prefix characters (line must still match SPINNER_ACTIVITY_PATTERN: …ing + …)
3
+ // Includes: ornament spinners; · / • / ∙ / ⋅ bullets; ⏺ (record); ▸▹ triangles; ○● circles
4
+ const SPINNER_CHARS = '✱✲✳✴✵✶✷✸✹✺✻✼✽✾✿❀❁❂❃❇❈❉❊❋✢✣✤✥✦✧✨⊛⊕⊙◉◎◍⁂⁕※⍟☼★☆·•⏺▸▹∙⋅○●';
5
+ // Matches spinner activity labels like "✽ Tempering…", "✳ Simplifying…", or "· Misting…"
5
6
  const SPINNER_ACTIVITY_PATTERN = new RegExp(`^[${SPINNER_CHARS}] \\S+ing.*\u2026`, 'm');
7
+ // Session stats above the prompt, e.g. "(9m 21s · ↓ 13.7k tokens)" — requires parens, a digit, and "tokens"
8
+ const TOKEN_STATS_LINE_PATTERN = /\([^)]*\d[^)]*tokens\s*\)/i;
6
9
  const BUSY_LOOKBACK_LINES = 5;
7
10
  // Workaround: Claude Code sometimes appears idle in terminal output while
8
11
  // still actively processing (busy). To mitigate false idle transitions,
@@ -122,6 +125,10 @@ export class ClaudeStateDetector extends BaseStateDetector {
122
125
  if (SPINNER_ACTIVITY_PATTERN.test(abovePromptBox)) {
123
126
  return 'busy';
124
127
  }
128
+ // Usage/time + token count line (often shown above the prompt while a turn is active)
129
+ if (TOKEN_STATS_LINE_PATTERN.test(abovePromptBox)) {
130
+ return 'busy';
131
+ }
125
132
  // Otherwise idle (debounced)
126
133
  return this.debounceIdle(terminal, currentState);
127
134
  }
@@ -330,6 +330,43 @@ describe('ClaudeStateDetector', () => {
330
330
  // Assert
331
331
  expect(state).toBe('busy');
332
332
  });
333
+ it('should detect busy for middle-dot activity label "· …ing…" (e.g. · Misting…)', () => {
334
+ terminal = createMockTerminal([
335
+ '· Misting…',
336
+ ' ⎿ Tip: Run /terminal-setup to enable convenient terminal integration',
337
+ '──────────────────────────────',
338
+ '❯',
339
+ '──────────────────────────────',
340
+ ]);
341
+ expect(detector.detectState(terminal, 'idle')).toBe('busy');
342
+ });
343
+ it('should detect busy when token stats line is above prompt box without interrupt or spinner', () => {
344
+ terminal = createMockTerminal([
345
+ '(9m 21s · ↓ 13.7k tokens)',
346
+ '──────────────────────────────',
347
+ '❯',
348
+ '──────────────────────────────',
349
+ ]);
350
+ expect(detector.detectState(terminal, 'idle')).toBe('busy');
351
+ });
352
+ it('should detect busy for token stats line with varied spacing and casing', () => {
353
+ terminal = createMockTerminal([
354
+ ' ( 1m · 500 TOKENS ) ',
355
+ '──────────────────────────────',
356
+ '❯',
357
+ '──────────────────────────────',
358
+ ]);
359
+ expect(detector.detectState(terminal, 'idle')).toBe('busy');
360
+ });
361
+ it('should not treat parenthetical text with "tokens" but no digit as busy', () => {
362
+ terminal = createMockTerminal([
363
+ '(see tokens in docs)',
364
+ '──────────────────────────────',
365
+ '❯',
366
+ '──────────────────────────────',
367
+ ]);
368
+ expect(detectStateAfterDebounce(detector, terminal, 'idle')).toBe('idle');
369
+ });
333
370
  it('should detect busy with various spinner characters', () => {
334
371
  const spinnerChars = [
335
372
  '✱',
@@ -38,7 +38,7 @@ export interface Session {
38
38
  /**
39
39
  * Mutex-protected session state data.
40
40
  * Access via stateMutex.runExclusive() or stateMutex.update() to ensure thread-safe operations.
41
- * Contains: state, pendingState, pendingStateStart, autoApprovalFailed, autoApprovalReason, autoApprovalAbortController
41
+ * Contains: state, autoApprovalFailed, autoApprovalReason, autoApprovalAbortController, backgroundTaskCount, teamMemberCount
42
42
  */
43
43
  stateMutex: Mutex<SessionStateData>;
44
44
  /**
@@ -42,9 +42,6 @@ export declare class Mutex<T> {
42
42
  */
43
43
  export interface SessionStateData {
44
44
  state: import('../types/index.js').SessionState;
45
- pendingState: import('../types/index.js').SessionState | undefined;
46
- pendingStateStart: number | undefined;
47
- stateConfirmedAt: number;
48
45
  autoApprovalFailed: boolean;
49
46
  autoApprovalReason: string | undefined;
50
47
  autoApprovalAbortController: AbortController | undefined;
@@ -80,9 +80,6 @@ export class Mutex {
80
80
  export function createInitialSessionStateData() {
81
81
  return {
82
82
  state: 'busy',
83
- pendingState: undefined,
84
- pendingStateStart: undefined,
85
- stateConfirmedAt: Date.now(),
86
83
  autoApprovalFailed: false,
87
84
  autoApprovalReason: undefined,
88
85
  autoApprovalAbortController: undefined,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ccmanager",
3
- "version": "4.1.5",
3
+ "version": "4.1.6",
4
4
  "description": "TUI application for managing multiple Claude Code sessions across Git worktrees",
5
5
  "license": "MIT",
6
6
  "author": "Kodai Kabasawa",
@@ -41,11 +41,11 @@
41
41
  "bin"
42
42
  ],
43
43
  "optionalDependencies": {
44
- "@kodaikabasawa/ccmanager-darwin-arm64": "4.1.5",
45
- "@kodaikabasawa/ccmanager-darwin-x64": "4.1.5",
46
- "@kodaikabasawa/ccmanager-linux-arm64": "4.1.5",
47
- "@kodaikabasawa/ccmanager-linux-x64": "4.1.5",
48
- "@kodaikabasawa/ccmanager-win32-x64": "4.1.5"
44
+ "@kodaikabasawa/ccmanager-darwin-arm64": "4.1.6",
45
+ "@kodaikabasawa/ccmanager-darwin-x64": "4.1.6",
46
+ "@kodaikabasawa/ccmanager-linux-arm64": "4.1.6",
47
+ "@kodaikabasawa/ccmanager-linux-x64": "4.1.6",
48
+ "@kodaikabasawa/ccmanager-win32-x64": "4.1.6"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@eslint/js": "^9.28.0",
@@ -1,3 +0,0 @@
1
- export declare const STATE_PERSISTENCE_DURATION_MS = 1000;
2
- export declare const STATE_CHECK_INTERVAL_MS = 100;
3
- export declare const STATE_MINIMUM_DURATION_MS = 1000;
@@ -1,9 +0,0 @@
1
- // Duration in milliseconds that a detected state must persist before being confirmed.
2
- // A higher value prevents transient flicker (e.g., brief "idle" during terminal re-renders)
3
- // at the cost of slightly slower state transitions.
4
- export const STATE_PERSISTENCE_DURATION_MS = 1000;
5
- // Check interval for state detection in milliseconds
6
- export const STATE_CHECK_INTERVAL_MS = 100;
7
- // Minimum duration in current state before allowing transition to a new state.
8
- // Prevents rapid back-and-forth flicker (e.g., busy → idle → busy).
9
- export const STATE_MINIMUM_DURATION_MS = 1000;