index.test.ts 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. import { describe, expect, mock, test } from 'bun:test';
  2. import { SLIM_INTERNAL_INITIATOR_MARKER } from '../../utils';
  3. import { createTodoContinuationHook } from './index';
  4. describe('createTodoContinuationHook', () => {
  5. function createMockContext(overrides?: {
  6. todoResult?: {
  7. data?: Array<{
  8. id: string;
  9. content: string;
  10. status: string;
  11. priority: string;
  12. }>;
  13. };
  14. messagesResult?: {
  15. data?: Array<{
  16. info?: { role?: string };
  17. parts?: Array<{ type?: string; text?: string }>;
  18. }>;
  19. };
  20. }) {
  21. return {
  22. client: {
  23. session: {
  24. todo: mock(async () => overrides?.todoResult ?? { data: [] }),
  25. messages: mock(async () => overrides?.messagesResult ?? { data: [] }),
  26. prompt: mock(async () => ({})),
  27. },
  28. },
  29. } as any;
  30. }
  31. async function delay(ms: number): Promise<void> {
  32. await new Promise((resolve) => setTimeout(resolve, ms));
  33. }
  34. // Notification prompts (noReply:true, no marker) fire immediately when
  35. // scheduling a continuation. These helpers check only for actual
  36. // continuation prompts (with SLIM_INTERNAL_INITIATOR_MARKER).
  37. function hasContinuation(m: ReturnType<typeof mock>): boolean {
  38. return m.mock.calls.some((c: any[]) =>
  39. (c[0]?.body?.parts as any[])?.some((p: any) =>
  40. p.text?.includes(SLIM_INTERNAL_INITIATOR_MARKER),
  41. ),
  42. );
  43. }
  44. function contCount(m: ReturnType<typeof mock>): number {
  45. return m.mock.calls.filter((c: any[]) =>
  46. (c[0]?.body?.parts as any[])?.some((p: any) =>
  47. p.text?.includes(SLIM_INTERNAL_INITIATOR_MARKER),
  48. ),
  49. ).length;
  50. }
  51. function contCall(m: ReturnType<typeof mock>): any[] {
  52. const call = m.mock.calls.find((c: any[]) =>
  53. (c[0]?.body?.parts as any[])?.some((p: any) =>
  54. p.text?.includes(SLIM_INTERNAL_INITIATOR_MARKER),
  55. ),
  56. );
  57. if (!call) {
  58. throw new Error('No continuation call found');
  59. }
  60. return call;
  61. }
  62. describe('tool toggle', () => {
  63. test('calling auto_continue execute with { enabled: true } sets state', async () => {
  64. const ctx = createMockContext();
  65. const hook = createTodoContinuationHook(ctx);
  66. const result = await hook.tool.auto_continue.execute({ enabled: true });
  67. expect(result).toContain('Auto-continue enabled');
  68. expect(result).toContain('up to 5');
  69. });
  70. test('calling auto_continue execute with { enabled: false } disables', async () => {
  71. const ctx = createMockContext();
  72. const hook = createTodoContinuationHook(ctx);
  73. const result = await hook.tool.auto_continue.execute({ enabled: false });
  74. expect(result).toBe('Auto-continue disabled.');
  75. });
  76. });
  77. describe('continuation scheduling', () => {
  78. test('session idle + enabled + incomplete todos → schedules continuation', async () => {
  79. const ctx = createMockContext({
  80. todoResult: {
  81. data: [
  82. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  83. { id: '2', content: 'todo2', status: 'completed', priority: 'low' },
  84. ],
  85. },
  86. messagesResult: {
  87. data: [
  88. {
  89. info: { role: 'assistant' },
  90. parts: [{ type: 'text', text: 'Here is the result' }],
  91. },
  92. ],
  93. },
  94. });
  95. const hook = createTodoContinuationHook(ctx, {
  96. maxContinuations: 5,
  97. cooldownMs: 50,
  98. });
  99. // Enable auto-continue
  100. await hook.tool.auto_continue.execute({ enabled: true });
  101. // Fire session.idle event
  102. await hook.handleEvent({
  103. event: {
  104. type: 'session.idle',
  105. properties: { sessionID: 'session-123' },
  106. },
  107. });
  108. // Wait for cooldown
  109. await delay(60);
  110. // Verify session.prompt was called with continuation prompt
  111. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  112. const promptCall = contCall(ctx.client.session.prompt);
  113. expect(promptCall[0].path.id).toBe('session-123');
  114. expect(promptCall[0].body.parts[0].text).toContain(
  115. '[Auto-continue: enabled - there are incomplete todos remaining.',
  116. );
  117. expect(promptCall[0].body.parts[0].text).toContain(
  118. SLIM_INTERNAL_INITIATOR_MARKER,
  119. );
  120. });
  121. test('disabled → no continuation', async () => {
  122. const ctx = createMockContext({
  123. todoResult: {
  124. data: [
  125. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  126. ],
  127. },
  128. messagesResult: {
  129. data: [
  130. {
  131. info: { role: 'assistant' },
  132. parts: [{ type: 'text', text: 'Done' }],
  133. },
  134. ],
  135. },
  136. });
  137. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  138. // Do NOT enable auto-continue
  139. // Fire session.idle event
  140. await hook.handleEvent({
  141. event: {
  142. type: 'session.idle',
  143. properties: { sessionID: 'session-123' },
  144. },
  145. });
  146. // Wait for cooldown
  147. await delay(60);
  148. // Verify session.prompt was NOT called
  149. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  150. });
  151. test('last message is a question → skip', async () => {
  152. const ctx = createMockContext({
  153. todoResult: {
  154. data: [
  155. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  156. ],
  157. },
  158. messagesResult: {
  159. data: [
  160. {
  161. info: { role: 'assistant' },
  162. parts: [
  163. { type: 'text', text: 'Should I proceed with the next step?' },
  164. ],
  165. },
  166. ],
  167. },
  168. });
  169. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  170. // Enable auto-continue
  171. await hook.tool.auto_continue.execute({ enabled: true });
  172. // Fire session.idle event
  173. await hook.handleEvent({
  174. event: {
  175. type: 'session.idle',
  176. properties: { sessionID: 'session-123' },
  177. },
  178. });
  179. // Wait for cooldown
  180. await delay(60);
  181. // Verify continuation NOT scheduled
  182. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  183. });
  184. test('question detection with question mark → skip', async () => {
  185. const ctx = createMockContext({
  186. todoResult: {
  187. data: [
  188. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  189. ],
  190. },
  191. messagesResult: {
  192. data: [
  193. {
  194. info: { role: 'assistant' },
  195. parts: [{ type: 'text', text: 'Ready to continue?' }],
  196. },
  197. ],
  198. },
  199. });
  200. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  201. await hook.tool.auto_continue.execute({ enabled: true });
  202. await hook.handleEvent({
  203. event: {
  204. type: 'session.idle',
  205. properties: { sessionID: 'session-123' },
  206. },
  207. });
  208. await delay(60);
  209. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  210. });
  211. test('question detection with "would you like" phrase → skip', async () => {
  212. const ctx = createMockContext({
  213. todoResult: {
  214. data: [
  215. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  216. ],
  217. },
  218. messagesResult: {
  219. data: [
  220. {
  221. info: { role: 'assistant' },
  222. parts: [
  223. {
  224. type: 'text',
  225. text: 'Would you like me to proceed?',
  226. },
  227. ],
  228. },
  229. ],
  230. },
  231. });
  232. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  233. await hook.tool.auto_continue.execute({ enabled: true });
  234. await hook.handleEvent({
  235. event: {
  236. type: 'session.idle',
  237. properties: { sessionID: 'session-123' },
  238. },
  239. });
  240. await delay(60);
  241. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  242. });
  243. test('max continuations reached → skip', async () => {
  244. const ctx = createMockContext({
  245. todoResult: {
  246. data: [
  247. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  248. ],
  249. },
  250. messagesResult: {
  251. data: [
  252. {
  253. info: { role: 'assistant' },
  254. parts: [{ type: 'text', text: 'Working...' }],
  255. },
  256. ],
  257. },
  258. });
  259. const hook = createTodoContinuationHook(ctx, {
  260. maxContinuations: 2,
  261. cooldownMs: 50,
  262. });
  263. await hook.tool.auto_continue.execute({ enabled: true });
  264. // Fire idle events up to maxContinuations
  265. for (let i = 0; i < 2; i++) {
  266. await hook.handleEvent({
  267. event: {
  268. type: 'session.idle',
  269. properties: { sessionID: 'session-123' },
  270. },
  271. });
  272. await delay(60);
  273. }
  274. // Reset mock for the 3rd attempt
  275. ctx.client.session.prompt.mockClear();
  276. // On the N+1th idle, verify no continuation scheduled
  277. await hook.handleEvent({
  278. event: {
  279. type: 'session.idle',
  280. properties: { sessionID: 'session-123' },
  281. },
  282. });
  283. await delay(60);
  284. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  285. });
  286. test('abort suppress window → skip', async () => {
  287. const ctx = createMockContext({
  288. todoResult: {
  289. data: [
  290. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  291. ],
  292. },
  293. messagesResult: {
  294. data: [
  295. {
  296. info: { role: 'assistant' },
  297. parts: [{ type: 'text', text: 'Working...' }],
  298. },
  299. ],
  300. },
  301. });
  302. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  303. // Seed orchestrator session
  304. await hook.handleEvent({
  305. event: {
  306. type: 'session.idle',
  307. properties: { sessionID: 'session-123' },
  308. },
  309. });
  310. await hook.tool.auto_continue.execute({ enabled: true });
  311. // Fire session.error with MessageAbortedError
  312. await hook.handleEvent({
  313. event: {
  314. type: 'session.error',
  315. properties: {
  316. sessionID: 'session-123',
  317. error: { name: 'MessageAbortedError' },
  318. },
  319. },
  320. });
  321. // Immediately fire session.idle
  322. await hook.handleEvent({
  323. event: {
  324. type: 'session.idle',
  325. properties: { sessionID: 'session-123' },
  326. },
  327. });
  328. // Wait less than suppress window (5s) - just enough to verify it's working
  329. await delay(100);
  330. // Verify no continuation within suppress window
  331. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  332. });
  333. test('session busy → cancel pending timer', async () => {
  334. const ctx = createMockContext({
  335. todoResult: {
  336. data: [
  337. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  338. ],
  339. },
  340. messagesResult: {
  341. data: [
  342. {
  343. info: { role: 'assistant' },
  344. parts: [{ type: 'text', text: 'Working...' }],
  345. },
  346. ],
  347. },
  348. });
  349. const hook = createTodoContinuationHook(ctx, {
  350. maxContinuations: 5,
  351. cooldownMs: 100,
  352. });
  353. await hook.tool.auto_continue.execute({ enabled: true });
  354. // Schedule a continuation
  355. await hook.handleEvent({
  356. event: {
  357. type: 'session.idle',
  358. properties: { sessionID: 'session-123' },
  359. },
  360. });
  361. // Before cooldown expires, fire session.status with busy
  362. await delay(50);
  363. await hook.handleEvent({
  364. event: {
  365. type: 'session.status',
  366. properties: {
  367. sessionID: 'session-123',
  368. status: { type: 'busy' },
  369. },
  370. },
  371. });
  372. // Advance past original cooldown
  373. await delay(60);
  374. // Verify timer was cancelled and prompt NOT called
  375. expect(hasContinuation(ctx.client.session.prompt)).toBe(false);
  376. });
  377. test('sub-agent session.busy does NOT cancel orchestrator timer', async () => {
  378. const ctx = createMockContext({
  379. todoResult: {
  380. data: [
  381. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  382. ],
  383. },
  384. messagesResult: {
  385. data: [
  386. {
  387. info: { role: 'assistant' },
  388. parts: [{ type: 'text', text: 'Working...' }],
  389. },
  390. ],
  391. },
  392. });
  393. const hook = createTodoContinuationHook(ctx, {
  394. maxContinuations: 5,
  395. cooldownMs: 100,
  396. });
  397. await hook.tool.auto_continue.execute({ enabled: true });
  398. // Schedule a continuation for orchestrator session
  399. await hook.handleEvent({
  400. event: {
  401. type: 'session.idle',
  402. properties: { sessionID: 'session-123' },
  403. },
  404. });
  405. // A sub-agent (different session) goes busy
  406. await delay(50);
  407. await hook.handleEvent({
  408. event: {
  409. type: 'session.status',
  410. properties: {
  411. sessionID: 'sub-agent-456',
  412. status: { type: 'busy' },
  413. },
  414. },
  415. });
  416. // Advance past original cooldown
  417. await delay(60);
  418. // Orchestrator timer should still fire — prompt was called
  419. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  420. });
  421. test('all todos complete → skip', async () => {
  422. const ctx = createMockContext({
  423. todoResult: {
  424. data: [
  425. {
  426. id: '1',
  427. content: 'todo1',
  428. status: 'completed',
  429. priority: 'high',
  430. },
  431. { id: '2', content: 'todo2', status: 'cancelled', priority: 'low' },
  432. ],
  433. },
  434. messagesResult: {
  435. data: [
  436. {
  437. info: { role: 'assistant' },
  438. parts: [{ type: 'text', text: 'All done' }],
  439. },
  440. ],
  441. },
  442. });
  443. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  444. await hook.tool.auto_continue.execute({ enabled: true });
  445. await hook.handleEvent({
  446. event: {
  447. type: 'session.idle',
  448. properties: { sessionID: 'session-123' },
  449. },
  450. });
  451. await delay(60);
  452. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  453. });
  454. test('non-orchestrator session → skip', async () => {
  455. const ctx = createMockContext({
  456. todoResult: {
  457. data: [
  458. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  459. ],
  460. },
  461. messagesResult: {
  462. data: [
  463. {
  464. info: { role: 'assistant' },
  465. parts: [{ type: 'text', text: 'Working...' }],
  466. },
  467. ],
  468. },
  469. });
  470. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  471. await hook.tool.auto_continue.execute({ enabled: true });
  472. // First idle from session A (becomes orchestrator)
  473. await hook.handleEvent({
  474. event: {
  475. type: 'session.idle',
  476. properties: { sessionID: 'session-A' },
  477. },
  478. });
  479. await delay(60);
  480. // Verify prompt was called for session A
  481. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  482. // Reset mock
  483. ctx.client.session.prompt.mockClear();
  484. // Second idle from session B (different sessionID)
  485. await hook.handleEvent({
  486. event: {
  487. type: 'session.idle',
  488. properties: { sessionID: 'session-B' },
  489. },
  490. });
  491. await delay(60);
  492. // Verify no continuation for session B
  493. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  494. });
  495. test('cooldownMs from config', async () => {
  496. const customCooldownMs = 100;
  497. const ctx = createMockContext({
  498. todoResult: {
  499. data: [
  500. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  501. ],
  502. },
  503. messagesResult: {
  504. data: [
  505. {
  506. info: { role: 'assistant' },
  507. parts: [{ type: 'text', text: 'Working...' }],
  508. },
  509. ],
  510. },
  511. });
  512. const hook = createTodoContinuationHook(ctx, {
  513. maxContinuations: 5,
  514. cooldownMs: customCooldownMs,
  515. });
  516. await hook.tool.auto_continue.execute({ enabled: true });
  517. await hook.handleEvent({
  518. event: {
  519. type: 'session.idle',
  520. properties: { sessionID: 'session-123' },
  521. },
  522. });
  523. // Advance timer by less than custom cooldown
  524. await delay(customCooldownMs - 1);
  525. // Verify prompt not called yet
  526. expect(hasContinuation(ctx.client.session.prompt)).toBe(false);
  527. // Advance timer by remaining 1ms
  528. await delay(1);
  529. // Now prompt should be called
  530. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  531. });
  532. });
  533. describe('event handling - session.error', () => {
  534. test('MessageAbortedError sets suppress window', async () => {
  535. const ctx = createMockContext({
  536. todoResult: {
  537. data: [
  538. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  539. ],
  540. },
  541. messagesResult: {
  542. data: [
  543. {
  544. info: { role: 'assistant' },
  545. parts: [{ type: 'text', text: 'Working...' }],
  546. },
  547. ],
  548. },
  549. });
  550. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  551. // Seed orchestrator session
  552. await hook.handleEvent({
  553. event: {
  554. type: 'session.idle',
  555. properties: { sessionID: 'session-123' },
  556. },
  557. });
  558. await hook.tool.auto_continue.execute({ enabled: true });
  559. // Fire session.error with MessageAbortedError
  560. await hook.handleEvent({
  561. event: {
  562. type: 'session.error',
  563. properties: {
  564. sessionID: 'session-123',
  565. error: { name: 'MessageAbortedError' },
  566. },
  567. },
  568. });
  569. await hook.handleEvent({
  570. event: {
  571. type: 'session.idle',
  572. properties: { sessionID: 'session-123' },
  573. },
  574. });
  575. // Wait less than suppress window
  576. await delay(100);
  577. // Verify no continuation within suppress window
  578. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  579. });
  580. test('AbortError sets suppress window', async () => {
  581. const ctx = createMockContext({
  582. todoResult: {
  583. data: [
  584. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  585. ],
  586. },
  587. messagesResult: {
  588. data: [
  589. {
  590. info: { role: 'assistant' },
  591. parts: [{ type: 'text', text: 'Working...' }],
  592. },
  593. ],
  594. },
  595. });
  596. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  597. // Seed orchestrator session (disabled, so no continuation fires)
  598. await hook.handleEvent({
  599. event: {
  600. type: 'session.idle',
  601. properties: { sessionID: 'session-123' },
  602. },
  603. });
  604. await hook.tool.auto_continue.execute({ enabled: true });
  605. await hook.handleEvent({
  606. event: {
  607. type: 'session.error',
  608. properties: {
  609. sessionID: 'session-123',
  610. error: { name: 'AbortError' },
  611. },
  612. },
  613. });
  614. await hook.handleEvent({
  615. event: {
  616. type: 'session.idle',
  617. properties: { sessionID: 'session-123' },
  618. },
  619. });
  620. // Wait less than suppress window
  621. await delay(100);
  622. // Verify no continuation within suppress window
  623. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  624. });
  625. test('other errors do not set suppress window', async () => {
  626. const ctx = createMockContext({
  627. todoResult: {
  628. data: [
  629. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  630. ],
  631. },
  632. messagesResult: {
  633. data: [
  634. {
  635. info: { role: 'assistant' },
  636. parts: [{ type: 'text', text: 'Working...' }],
  637. },
  638. ],
  639. },
  640. });
  641. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  642. await hook.tool.auto_continue.execute({ enabled: true });
  643. await hook.handleEvent({
  644. event: {
  645. type: 'session.error',
  646. properties: {
  647. sessionID: 'session-123',
  648. error: { name: 'NetworkError' },
  649. },
  650. },
  651. });
  652. await hook.handleEvent({
  653. event: {
  654. type: 'session.idle',
  655. properties: { sessionID: 'session-123' },
  656. },
  657. });
  658. await delay(60);
  659. // Prompt should be called immediately (no suppress window)
  660. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  661. });
  662. });
  663. describe('event handling - session.deleted', () => {
  664. test('clears pending timer on session delete', async () => {
  665. const ctx = createMockContext({
  666. todoResult: {
  667. data: [
  668. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  669. ],
  670. },
  671. messagesResult: {
  672. data: [
  673. {
  674. info: { role: 'assistant' },
  675. parts: [{ type: 'text', text: 'Working...' }],
  676. },
  677. ],
  678. },
  679. });
  680. const hook = createTodoContinuationHook(ctx, {
  681. maxContinuations: 5,
  682. cooldownMs: 100,
  683. });
  684. await hook.tool.auto_continue.execute({ enabled: true });
  685. // Schedule continuation
  686. await hook.handleEvent({
  687. event: {
  688. type: 'session.idle',
  689. properties: { sessionID: 'session-123' },
  690. },
  691. });
  692. // Delete session before timer fires
  693. await delay(50);
  694. await hook.handleEvent({
  695. event: {
  696. type: 'session.deleted',
  697. properties: {
  698. sessionID: 'session-123',
  699. },
  700. },
  701. });
  702. // Advance past original cooldown
  703. await delay(60);
  704. // Verify timer was cancelled and prompt NOT called
  705. expect(hasContinuation(ctx.client.session.prompt)).toBe(false);
  706. });
  707. test('sub-agent session.deleted does NOT cancel orchestrator timer', async () => {
  708. const ctx = createMockContext({
  709. todoResult: {
  710. data: [
  711. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  712. ],
  713. },
  714. messagesResult: {
  715. data: [
  716. {
  717. info: { role: 'assistant' },
  718. parts: [{ type: 'text', text: 'Working...' }],
  719. },
  720. ],
  721. },
  722. });
  723. const hook = createTodoContinuationHook(ctx, {
  724. maxContinuations: 5,
  725. cooldownMs: 100,
  726. });
  727. await hook.tool.auto_continue.execute({ enabled: true });
  728. // Schedule continuation for orchestrator session
  729. await hook.handleEvent({
  730. event: {
  731. type: 'session.idle',
  732. properties: { sessionID: 'session-123' },
  733. },
  734. });
  735. // A sub-agent (different session) gets deleted
  736. await delay(50);
  737. await hook.handleEvent({
  738. event: {
  739. type: 'session.deleted',
  740. properties: {
  741. sessionID: 'sub-agent-456',
  742. },
  743. },
  744. });
  745. // Advance past original cooldown
  746. await delay(60);
  747. // Orchestrator timer should still fire — prompt was called
  748. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  749. });
  750. test('resets orchestrator session when deleted session matches', async () => {
  751. const ctx = createMockContext({
  752. todoResult: {
  753. data: [
  754. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  755. ],
  756. },
  757. messagesResult: {
  758. data: [
  759. {
  760. info: { role: 'assistant' },
  761. parts: [{ type: 'text', text: 'Working...' }],
  762. },
  763. ],
  764. },
  765. });
  766. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  767. await hook.tool.auto_continue.execute({ enabled: true });
  768. // First idle sets orchestrator
  769. await hook.handleEvent({
  770. event: {
  771. type: 'session.idle',
  772. properties: { sessionID: 'session-A' },
  773. },
  774. });
  775. await delay(60);
  776. // Delete orchestrator session
  777. await hook.handleEvent({
  778. event: {
  779. type: 'session.deleted',
  780. properties: {
  781. sessionID: 'session-A',
  782. },
  783. },
  784. });
  785. // Second idle from new session should become orchestrator
  786. ctx.client.session.prompt.mockClear();
  787. await hook.handleEvent({
  788. event: {
  789. type: 'session.idle',
  790. properties: { sessionID: 'session-B' },
  791. },
  792. });
  793. await delay(60);
  794. // Prompt should be called for session-B (new orchestrator)
  795. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  796. const promptCall = contCall(ctx.client.session.prompt);
  797. expect(promptCall[0].path.id).toBe('session-B');
  798. });
  799. });
  800. describe('error handling', () => {
  801. test('fetch todos failure → skips continuation', async () => {
  802. const ctx = createMockContext({
  803. todoResult: undefined as any,
  804. });
  805. ctx.client.session.todo = mock(async () => {
  806. throw new Error('Failed to fetch todos');
  807. });
  808. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  809. await hook.tool.auto_continue.execute({ enabled: true });
  810. await hook.handleEvent({
  811. event: {
  812. type: 'session.idle',
  813. properties: { sessionID: 'session-123' },
  814. },
  815. });
  816. await delay(60);
  817. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  818. });
  819. test('fetch messages failure → skips continuation', async () => {
  820. const ctx = createMockContext({
  821. todoResult: {
  822. data: [
  823. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  824. ],
  825. },
  826. });
  827. ctx.client.session.messages = mock(async () => {
  828. throw new Error('Failed to fetch messages');
  829. });
  830. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  831. await hook.tool.auto_continue.execute({ enabled: true });
  832. await hook.handleEvent({
  833. event: {
  834. type: 'session.idle',
  835. properties: { sessionID: 'session-123' },
  836. },
  837. });
  838. await delay(60);
  839. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  840. });
  841. });
  842. describe('command.execute.before interception', () => {
  843. test('unrelated command → no interception', async () => {
  844. const ctx = createMockContext();
  845. const hook = createTodoContinuationHook(ctx);
  846. const output = { parts: [] as Array<{ type: string; text?: string }> };
  847. await hook.handleCommandExecuteBefore(
  848. { command: 'help', sessionID: 'session-123', arguments: '' },
  849. output,
  850. );
  851. expect(output.parts).toHaveLength(0);
  852. });
  853. test('/auto-continue enables and injects continuation when incomplete todos', async () => {
  854. const ctx = createMockContext({
  855. todoResult: {
  856. data: [
  857. {
  858. id: '1',
  859. content: 'todo1',
  860. status: 'pending',
  861. priority: 'high',
  862. },
  863. ],
  864. },
  865. });
  866. const hook = createTodoContinuationHook(ctx);
  867. const output = { parts: [] as Array<{ type: string; text?: string }> };
  868. await hook.handleCommandExecuteBefore(
  869. { command: 'auto-continue', sessionID: 'session-123', arguments: '' },
  870. output,
  871. );
  872. expect(output.parts).toHaveLength(1);
  873. expect(output.parts[0].text).toContain(
  874. '[Auto-continue: enabled - there are incomplete todos remaining.',
  875. );
  876. expect(output.parts[0].text).toContain(SLIM_INTERNAL_INITIATOR_MARKER);
  877. });
  878. test('/auto-continue enables but no continuation when all todos complete', async () => {
  879. const ctx = createMockContext({
  880. todoResult: {
  881. data: [
  882. {
  883. id: '1',
  884. content: 'todo1',
  885. status: 'completed',
  886. priority: 'high',
  887. },
  888. ],
  889. },
  890. });
  891. const hook = createTodoContinuationHook(ctx);
  892. const output = { parts: [] as Array<{ type: string; text?: string }> };
  893. await hook.handleCommandExecuteBefore(
  894. { command: 'auto-continue', sessionID: 'session-123', arguments: '' },
  895. output,
  896. );
  897. expect(output.parts).toHaveLength(1);
  898. expect(output.parts[0].text).toContain('No incomplete todos right now');
  899. });
  900. test('/auto-continue toggles off when already enabled', async () => {
  901. const ctx = createMockContext();
  902. const hook = createTodoContinuationHook(ctx);
  903. const output = { parts: [] as Array<{ type: string; text?: string }> };
  904. // Enable via tool
  905. await hook.tool.auto_continue.execute({ enabled: true });
  906. // Toggle off via command
  907. await hook.handleCommandExecuteBefore(
  908. { command: 'auto-continue', sessionID: 'session-123', arguments: '' },
  909. output,
  910. );
  911. expect(output.parts).toHaveLength(1);
  912. expect(output.parts[0].text).toContain('disabled by user command');
  913. });
  914. test('/auto-continue resets consecutive continuations on toggle', async () => {
  915. const ctx = createMockContext({
  916. todoResult: {
  917. data: [
  918. {
  919. id: '1',
  920. content: 'todo1',
  921. status: 'pending',
  922. priority: 'high',
  923. },
  924. ],
  925. },
  926. messagesResult: {
  927. data: [
  928. {
  929. info: { role: 'assistant' },
  930. parts: [{ type: 'text', text: 'Working...' }],
  931. },
  932. ],
  933. },
  934. });
  935. const hook = createTodoContinuationHook(ctx, {
  936. maxContinuations: 2,
  937. cooldownMs: 50,
  938. });
  939. // Enable and run up to max
  940. await hook.tool.auto_continue.execute({ enabled: true });
  941. for (let i = 0; i < 2; i++) {
  942. await hook.handleEvent({
  943. event: {
  944. type: 'session.idle',
  945. properties: { sessionID: 'session-123' },
  946. },
  947. });
  948. await delay(60);
  949. }
  950. // Toggle off then on via command (resets count)
  951. const outputOff = {
  952. parts: [] as Array<{ type: string; text?: string }>,
  953. };
  954. await hook.handleCommandExecuteBefore(
  955. { command: 'auto-continue', sessionID: 'session-123', arguments: '' },
  956. outputOff,
  957. );
  958. expect(outputOff.parts[0].text).toContain('disabled');
  959. const outputOn = {
  960. parts: [] as Array<{ type: string; text?: string }>,
  961. };
  962. await hook.handleCommandExecuteBefore(
  963. { command: 'auto-continue', sessionID: 'session-123', arguments: '' },
  964. outputOn,
  965. );
  966. // Should have continuation prompt again (count was reset)
  967. expect(outputOn.parts[0].text).toContain(
  968. '[Auto-continue: enabled - there are incomplete todos remaining.',
  969. );
  970. });
  971. test('/auto-continue with todo fetch failure → enables without continuation', async () => {
  972. const ctx = createMockContext();
  973. ctx.client.session.todo = mock(async () => {
  974. throw new Error('Network error');
  975. });
  976. const hook = createTodoContinuationHook(ctx);
  977. const output = { parts: [] as Array<{ type: string; text?: string }> };
  978. await hook.handleCommandExecuteBefore(
  979. { command: 'auto-continue', sessionID: 'session-123', arguments: '' },
  980. output,
  981. );
  982. // Should still enable but skip continuation (no todos fetched)
  983. expect(output.parts).toHaveLength(1);
  984. expect(output.parts[0].text).toContain('No incomplete todos right now');
  985. });
  986. });
  987. describe('config defaults', () => {
  988. test('default config: maxContinuations = 5, cooldownMs = 3000', async () => {
  989. const ctx = createMockContext({
  990. todoResult: {
  991. data: [
  992. { id: '1', content: 'todo1', status: 'pending', priority: 'high' },
  993. ],
  994. },
  995. messagesResult: {
  996. data: [
  997. {
  998. info: { role: 'assistant' },
  999. parts: [{ type: 'text', text: 'Working...' }],
  1000. },
  1001. ],
  1002. },
  1003. });
  1004. const hook = createTodoContinuationHook(ctx); // No config passed
  1005. const result = await hook.tool.auto_continue.execute({ enabled: true });
  1006. expect(result).toContain('up to 5');
  1007. // Test default cooldown - we'll just verify it waits before calling
  1008. await hook.handleEvent({
  1009. event: {
  1010. type: 'session.idle',
  1011. properties: { sessionID: 'session-123' },
  1012. },
  1013. });
  1014. // Wait less than default cooldown
  1015. await delay(100);
  1016. expect(hasContinuation(ctx.client.session.prompt)).toBe(false);
  1017. // Wait past default cooldown
  1018. await delay(2900);
  1019. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  1020. });
  1021. });
  1022. describe('council review findings', () => {
  1023. describe('CRITICAL-1: counter bypass via session.status→busy', () => {
  1024. test('counter persists when busy fires during auto-injection', async () => {
  1025. let promptResolve!: () => void;
  1026. const ctx = createMockContext({
  1027. todoResult: {
  1028. data: [
  1029. {
  1030. id: '1',
  1031. content: 't1',
  1032. status: 'pending',
  1033. priority: 'high',
  1034. },
  1035. ],
  1036. },
  1037. messagesResult: {
  1038. data: [
  1039. {
  1040. info: { role: 'assistant' },
  1041. parts: [{ type: 'text', text: 'Work' }],
  1042. },
  1043. ],
  1044. },
  1045. });
  1046. // Make prompt hang so isAutoInjecting stays true
  1047. ctx.client.session.prompt = mock(async () => {
  1048. await new Promise<void>((r) => {
  1049. promptResolve = r;
  1050. });
  1051. });
  1052. const hook = createTodoContinuationHook(ctx, {
  1053. maxContinuations: 2,
  1054. cooldownMs: 50,
  1055. });
  1056. await hook.tool.auto_continue.execute({ enabled: true });
  1057. // Cycle 1: idle → timer → prompt hangs
  1058. await hook.handleEvent({
  1059. event: {
  1060. type: 'session.idle',
  1061. properties: { sessionID: 's1' },
  1062. },
  1063. });
  1064. await delay(60);
  1065. // Session goes busy from prompt — isAutoInjecting is true,
  1066. // so counter should NOT be reset
  1067. await hook.handleEvent({
  1068. event: {
  1069. type: 'session.status',
  1070. properties: {
  1071. sessionID: 's1',
  1072. status: { type: 'busy' },
  1073. },
  1074. },
  1075. });
  1076. // Resolve prompt → counter = 1
  1077. promptResolve();
  1078. await delay(10);
  1079. // Cycle 2: idle → timer → prompt hangs
  1080. await hook.handleEvent({
  1081. event: {
  1082. type: 'session.idle',
  1083. properties: { sessionID: 's1' },
  1084. },
  1085. });
  1086. await delay(60);
  1087. // Session goes busy again — counter still not reset
  1088. await hook.handleEvent({
  1089. event: {
  1090. type: 'session.status',
  1091. properties: {
  1092. sessionID: 's1',
  1093. status: { type: 'busy' },
  1094. },
  1095. },
  1096. });
  1097. // Resolve prompt → counter = 2
  1098. promptResolve();
  1099. await delay(10);
  1100. // Cycle 3: counter = 2 >= maxContinuations = 2 → BLOCKED
  1101. ctx.client.session.prompt = mock(async () => ({}));
  1102. await hook.handleEvent({
  1103. event: {
  1104. type: 'session.idle',
  1105. properties: { sessionID: 's1' },
  1106. },
  1107. });
  1108. await delay(60);
  1109. expect(hasContinuation(ctx.client.session.prompt)).toBe(false);
  1110. });
  1111. });
  1112. describe('CRITICAL-2: disable cancels pending timer', () => {
  1113. test('tool disable during cooldown prevents injection', async () => {
  1114. const ctx = createMockContext({
  1115. todoResult: {
  1116. data: [
  1117. {
  1118. id: '1',
  1119. content: 't1',
  1120. status: 'pending',
  1121. priority: 'high',
  1122. },
  1123. ],
  1124. },
  1125. messagesResult: {
  1126. data: [
  1127. {
  1128. info: { role: 'assistant' },
  1129. parts: [{ type: 'text', text: 'Work' }],
  1130. },
  1131. ],
  1132. },
  1133. });
  1134. const hook = createTodoContinuationHook(ctx, { cooldownMs: 100 });
  1135. await hook.tool.auto_continue.execute({ enabled: true });
  1136. // Fire idle → timer scheduled (100ms cooldown)
  1137. await hook.handleEvent({
  1138. event: {
  1139. type: 'session.idle',
  1140. properties: { sessionID: 's1' },
  1141. },
  1142. });
  1143. // Disable before timer fires
  1144. await delay(50);
  1145. await hook.tool.auto_continue.execute({ enabled: false });
  1146. // Wait past original cooldown
  1147. await delay(60);
  1148. expect(hasContinuation(ctx.client.session.prompt)).toBe(false);
  1149. });
  1150. test('command disable during cooldown prevents injection', async () => {
  1151. const ctx = createMockContext({
  1152. todoResult: {
  1153. data: [
  1154. {
  1155. id: '1',
  1156. content: 't1',
  1157. status: 'pending',
  1158. priority: 'high',
  1159. },
  1160. ],
  1161. },
  1162. messagesResult: {
  1163. data: [
  1164. {
  1165. info: { role: 'assistant' },
  1166. parts: [{ type: 'text', text: 'Work' }],
  1167. },
  1168. ],
  1169. },
  1170. });
  1171. const hook = createTodoContinuationHook(ctx, { cooldownMs: 100 });
  1172. // Enable via command
  1173. const outputOn = {
  1174. parts: [] as Array<{ type: string; text?: string }>,
  1175. };
  1176. await hook.handleCommandExecuteBefore(
  1177. {
  1178. command: 'auto-continue',
  1179. sessionID: 's1',
  1180. arguments: 'on',
  1181. },
  1182. outputOn,
  1183. );
  1184. // Fire idle → timer scheduled
  1185. await hook.handleEvent({
  1186. event: {
  1187. type: 'session.idle',
  1188. properties: { sessionID: 's1' },
  1189. },
  1190. });
  1191. // Disable via command before timer fires
  1192. await delay(50);
  1193. const outputOff = {
  1194. parts: [] as Array<{ type: string; text?: string }>,
  1195. };
  1196. await hook.handleCommandExecuteBefore(
  1197. {
  1198. command: 'auto-continue',
  1199. sessionID: 's1',
  1200. arguments: 'off',
  1201. },
  1202. outputOff,
  1203. );
  1204. // Wait past original cooldown
  1205. await delay(60);
  1206. expect(hasContinuation(ctx.client.session.prompt)).toBe(false);
  1207. });
  1208. });
  1209. describe('MAJOR-1: session.deleted resets counter', () => {
  1210. test('deleted orchestrator session resets counter for next session', async () => {
  1211. const ctx = createMockContext({
  1212. todoResult: {
  1213. data: [
  1214. {
  1215. id: '1',
  1216. content: 't1',
  1217. status: 'pending',
  1218. priority: 'high',
  1219. },
  1220. ],
  1221. },
  1222. messagesResult: {
  1223. data: [
  1224. {
  1225. info: { role: 'assistant' },
  1226. parts: [{ type: 'text', text: 'Work' }],
  1227. },
  1228. ],
  1229. },
  1230. });
  1231. const hook = createTodoContinuationHook(ctx, {
  1232. maxContinuations: 2,
  1233. cooldownMs: 50,
  1234. });
  1235. await hook.tool.auto_continue.execute({ enabled: true });
  1236. // Cycle 1: idle → inject → counter = 1
  1237. await hook.handleEvent({
  1238. event: {
  1239. type: 'session.idle',
  1240. properties: { sessionID: 's1' },
  1241. },
  1242. });
  1243. await delay(60);
  1244. // Delete orchestrator session → counter should reset
  1245. await hook.handleEvent({
  1246. event: {
  1247. type: 'session.deleted',
  1248. properties: { sessionID: 's1' },
  1249. },
  1250. });
  1251. // New session becomes orchestrator — counter starts from 0
  1252. ctx.client.session.prompt.mockClear();
  1253. await hook.handleEvent({
  1254. event: {
  1255. type: 'session.idle',
  1256. properties: { sessionID: 's2' },
  1257. },
  1258. });
  1259. await delay(60); // counter = 1
  1260. // One more cycle → counter = 2 (reaches max)
  1261. ctx.client.session.prompt.mockClear();
  1262. await hook.handleEvent({
  1263. event: {
  1264. type: 'session.idle',
  1265. properties: { sessionID: 's2' },
  1266. },
  1267. });
  1268. await delay(60);
  1269. // Third cycle blocked (counter = 2 >= max = 2)
  1270. ctx.client.session.prompt.mockClear();
  1271. await hook.handleEvent({
  1272. event: {
  1273. type: 'session.idle',
  1274. properties: { sessionID: 's2' },
  1275. },
  1276. });
  1277. await delay(60);
  1278. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  1279. });
  1280. });
  1281. describe('MAJOR-2: suppressUntil cleared on re-enable', () => {
  1282. test('tool re-enable clears suppress window', async () => {
  1283. const ctx = createMockContext({
  1284. todoResult: {
  1285. data: [
  1286. {
  1287. id: '1',
  1288. content: 't1',
  1289. status: 'pending',
  1290. priority: 'high',
  1291. },
  1292. ],
  1293. },
  1294. messagesResult: {
  1295. data: [
  1296. {
  1297. info: { role: 'assistant' },
  1298. parts: [{ type: 'text', text: 'Work' }],
  1299. },
  1300. ],
  1301. },
  1302. });
  1303. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  1304. await hook.tool.auto_continue.execute({ enabled: true });
  1305. // Fire abort → sets suppress window
  1306. await hook.handleEvent({
  1307. event: {
  1308. type: 'session.error',
  1309. properties: {
  1310. sessionID: 's1',
  1311. error: { name: 'AbortError' },
  1312. },
  1313. },
  1314. });
  1315. // Re-enable within suppress window → clears suppressUntil
  1316. await hook.tool.auto_continue.execute({ enabled: true });
  1317. // Fire idle → should NOT be suppressed
  1318. await hook.handleEvent({
  1319. event: {
  1320. type: 'session.idle',
  1321. properties: { sessionID: 's1' },
  1322. },
  1323. });
  1324. await delay(60);
  1325. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  1326. });
  1327. test('command re-enable clears suppress window', async () => {
  1328. const ctx = createMockContext({
  1329. todoResult: {
  1330. data: [
  1331. {
  1332. id: '1',
  1333. content: 't1',
  1334. status: 'pending',
  1335. priority: 'high',
  1336. },
  1337. ],
  1338. },
  1339. messagesResult: {
  1340. data: [
  1341. {
  1342. info: { role: 'assistant' },
  1343. parts: [{ type: 'text', text: 'Work' }],
  1344. },
  1345. ],
  1346. },
  1347. });
  1348. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  1349. await hook.tool.auto_continue.execute({ enabled: true });
  1350. // Fire abort → sets suppress window
  1351. await hook.handleEvent({
  1352. event: {
  1353. type: 'session.error',
  1354. properties: {
  1355. sessionID: 's1',
  1356. error: { name: 'AbortError' },
  1357. },
  1358. },
  1359. });
  1360. // Re-enable via command → clears suppressUntil
  1361. const output = {
  1362. parts: [] as Array<{ type: string; text?: string }>,
  1363. };
  1364. await hook.handleCommandExecuteBefore(
  1365. {
  1366. command: 'auto-continue',
  1367. sessionID: 's1',
  1368. arguments: 'on',
  1369. },
  1370. output,
  1371. );
  1372. // Fire idle → should NOT be suppressed
  1373. await hook.handleEvent({
  1374. event: {
  1375. type: 'session.idle',
  1376. properties: { sessionID: 's1' },
  1377. },
  1378. });
  1379. await delay(60);
  1380. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  1381. });
  1382. });
  1383. describe('error paths', () => {
  1384. test('prompt failure in timer callback is handled gracefully', async () => {
  1385. const ctx = createMockContext({
  1386. todoResult: {
  1387. data: [
  1388. {
  1389. id: '1',
  1390. content: 't1',
  1391. status: 'pending',
  1392. priority: 'high',
  1393. },
  1394. ],
  1395. },
  1396. messagesResult: {
  1397. data: [
  1398. {
  1399. info: { role: 'assistant' },
  1400. parts: [{ type: 'text', text: 'Work' }],
  1401. },
  1402. ],
  1403. },
  1404. });
  1405. ctx.client.session.prompt = mock(async () => {
  1406. throw new Error('API error');
  1407. });
  1408. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  1409. // Seed orchestrator session
  1410. await hook.handleEvent({
  1411. event: {
  1412. type: 'session.idle',
  1413. properties: { sessionID: 's1' },
  1414. },
  1415. });
  1416. await hook.tool.auto_continue.execute({ enabled: true });
  1417. await hook.handleEvent({
  1418. event: {
  1419. type: 'session.idle',
  1420. properties: { sessionID: 's1' },
  1421. },
  1422. });
  1423. await delay(60);
  1424. // Error caught; isAutoInjecting should be cleared via finally.
  1425. // Verify by checking a second idle still works.
  1426. ctx.client.session.prompt = mock(async () => ({}));
  1427. await hook.handleEvent({
  1428. event: {
  1429. type: 'session.idle',
  1430. properties: { sessionID: 's1' },
  1431. },
  1432. });
  1433. await delay(60);
  1434. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  1435. });
  1436. });
  1437. describe('edge cases', () => {
  1438. test('session.idle with missing sessionID returns early', async () => {
  1439. const ctx = createMockContext();
  1440. const hook = createTodoContinuationHook(ctx);
  1441. await hook.tool.auto_continue.execute({ enabled: true });
  1442. // Fire idle without sessionID — should not throw
  1443. await hook.handleEvent({
  1444. event: { type: 'session.idle', properties: {} },
  1445. });
  1446. expect(ctx.client.session.todo).not.toHaveBeenCalled();
  1447. });
  1448. test('session.deleted with properties.info.id path', async () => {
  1449. const ctx = createMockContext({
  1450. todoResult: {
  1451. data: [
  1452. {
  1453. id: '1',
  1454. content: 't1',
  1455. status: 'pending',
  1456. priority: 'high',
  1457. },
  1458. ],
  1459. },
  1460. messagesResult: {
  1461. data: [
  1462. {
  1463. info: { role: 'assistant' },
  1464. parts: [{ type: 'text', text: 'Work' }],
  1465. },
  1466. ],
  1467. },
  1468. });
  1469. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  1470. await hook.tool.auto_continue.execute({ enabled: true });
  1471. // Set orchestrator via idle
  1472. await hook.handleEvent({
  1473. event: {
  1474. type: 'session.idle',
  1475. properties: { sessionID: 's1' },
  1476. },
  1477. });
  1478. await delay(60);
  1479. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  1480. // Delete via info.id path (alternative shape from session store)
  1481. await hook.handleEvent({
  1482. event: {
  1483. type: 'session.deleted',
  1484. properties: { info: { id: 's1' } },
  1485. },
  1486. });
  1487. // New session should become orchestrator
  1488. ctx.client.session.prompt.mockClear();
  1489. await hook.handleEvent({
  1490. event: {
  1491. type: 'session.idle',
  1492. properties: { sessionID: 's2' },
  1493. },
  1494. });
  1495. await delay(60);
  1496. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  1497. expect(contCall(ctx.client.session.prompt)[0].path.id).toBe('s2');
  1498. });
  1499. test('cooldownMs = 0 fires on next tick', async () => {
  1500. const ctx = createMockContext({
  1501. todoResult: {
  1502. data: [
  1503. {
  1504. id: '1',
  1505. content: 't1',
  1506. status: 'pending',
  1507. priority: 'high',
  1508. },
  1509. ],
  1510. },
  1511. messagesResult: {
  1512. data: [
  1513. {
  1514. info: { role: 'assistant' },
  1515. parts: [{ type: 'text', text: 'Work' }],
  1516. },
  1517. ],
  1518. },
  1519. });
  1520. const hook = createTodoContinuationHook(ctx, {
  1521. cooldownMs: 0,
  1522. maxContinuations: 5,
  1523. });
  1524. await hook.tool.auto_continue.execute({ enabled: true });
  1525. await hook.handleEvent({
  1526. event: {
  1527. type: 'session.idle',
  1528. properties: { sessionID: 's1' },
  1529. },
  1530. });
  1531. await delay(10);
  1532. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  1533. });
  1534. });
  1535. describe('MAJOR-3: double-fire prevention', () => {
  1536. test('rapid idle events during prompt delivery — single continuation', async () => {
  1537. let promptResolve!: () => void;
  1538. const ctx = createMockContext({
  1539. todoResult: {
  1540. data: [
  1541. {
  1542. id: '1',
  1543. content: 't1',
  1544. status: 'pending',
  1545. priority: 'high',
  1546. },
  1547. ],
  1548. },
  1549. messagesResult: {
  1550. data: [
  1551. {
  1552. info: { role: 'assistant' },
  1553. parts: [{ type: 'text', text: 'Work' }],
  1554. },
  1555. ],
  1556. },
  1557. });
  1558. ctx.client.session.prompt = mock(async () => {
  1559. await new Promise<void>((r) => {
  1560. promptResolve = r;
  1561. });
  1562. });
  1563. const hook = createTodoContinuationHook(ctx, { cooldownMs: 50 });
  1564. await hook.tool.auto_continue.execute({ enabled: true });
  1565. // Fire idle → timer → prompt hangs (isAutoInjecting = true)
  1566. await hook.handleEvent({
  1567. event: {
  1568. type: 'session.idle',
  1569. properties: { sessionID: 's1' },
  1570. },
  1571. });
  1572. await delay(60);
  1573. // Fire another idle while prompt is in flight
  1574. await hook.handleEvent({
  1575. event: {
  1576. type: 'session.idle',
  1577. properties: { sessionID: 's1' },
  1578. },
  1579. });
  1580. // Only one prompt call (blocked by isAutoInjecting gate)
  1581. expect(contCount(ctx.client.session.prompt)).toBe(1);
  1582. // Resolve prompt
  1583. promptResolve();
  1584. await delay(10);
  1585. // Now idle should schedule a new timer
  1586. ctx.client.session.prompt = mock(async () => ({}));
  1587. await hook.handleEvent({
  1588. event: {
  1589. type: 'session.idle',
  1590. properties: { sessionID: 's1' },
  1591. },
  1592. });
  1593. await delay(60);
  1594. expect(contCount(ctx.client.session.prompt)).toBe(1);
  1595. });
  1596. });
  1597. describe('MAJOR-4: command explicit on|off arguments', () => {
  1598. test('command "on" keeps enabled state when already enabled', async () => {
  1599. const ctx = createMockContext();
  1600. const hook = createTodoContinuationHook(ctx);
  1601. // Enable via tool
  1602. await hook.tool.auto_continue.execute({ enabled: true });
  1603. // /auto-continue on → should KEEP enabled (not toggle to off)
  1604. const output = {
  1605. parts: [] as Array<{ type: string; text?: string }>,
  1606. };
  1607. await hook.handleCommandExecuteBefore(
  1608. {
  1609. command: 'auto-continue',
  1610. sessionID: 's1',
  1611. arguments: 'on',
  1612. },
  1613. output,
  1614. );
  1615. expect(output.parts[0].text).not.toContain('disabled');
  1616. });
  1617. test('command "off" keeps disabled state when already disabled', async () => {
  1618. const ctx = createMockContext();
  1619. const hook = createTodoContinuationHook(ctx);
  1620. // Start disabled (default)
  1621. const output = {
  1622. parts: [] as Array<{ type: string; text?: string }>,
  1623. };
  1624. await hook.handleCommandExecuteBefore(
  1625. {
  1626. command: 'auto-continue',
  1627. sessionID: 's1',
  1628. arguments: 'off',
  1629. },
  1630. output,
  1631. );
  1632. expect(output.parts[0].text).toContain('disabled');
  1633. });
  1634. test('command with no argument toggles state', async () => {
  1635. const ctx = createMockContext();
  1636. const hook = createTodoContinuationHook(ctx);
  1637. // First toggle: disabled → enabled
  1638. const output1 = {
  1639. parts: [] as Array<{ type: string; text?: string }>,
  1640. };
  1641. await hook.handleCommandExecuteBefore(
  1642. {
  1643. command: 'auto-continue',
  1644. sessionID: 's1',
  1645. arguments: '',
  1646. },
  1647. output1,
  1648. );
  1649. expect(output1.parts[0].text).not.toContain('disabled');
  1650. // Second toggle: enabled → disabled
  1651. const output2 = {
  1652. parts: [] as Array<{ type: string; text?: string }>,
  1653. };
  1654. await hook.handleCommandExecuteBefore(
  1655. {
  1656. command: 'auto-continue',
  1657. sessionID: 's1',
  1658. arguments: '',
  1659. },
  1660. output2,
  1661. );
  1662. expect(output2.parts[0].text).toContain('disabled');
  1663. });
  1664. });
  1665. });
  1666. describe('auto-enable on todo count', () => {
  1667. function createAutoEnableCtx(
  1668. todos: Array<{
  1669. id: string;
  1670. content: string;
  1671. status: string;
  1672. priority: string;
  1673. }>,
  1674. ) {
  1675. return createMockContext({
  1676. todoResult: { data: todos },
  1677. messagesResult: {
  1678. data: [
  1679. {
  1680. info: { role: 'assistant' },
  1681. parts: [{ type: 'text', text: 'Working...' }],
  1682. },
  1683. ],
  1684. },
  1685. });
  1686. }
  1687. test('autoEnable=true, todos >= threshold → auto-enables and continues', async () => {
  1688. const ctx = createAutoEnableCtx([
  1689. { id: '1', content: 't1', status: 'pending', priority: 'high' },
  1690. { id: '2', content: 't2', status: 'pending', priority: 'high' },
  1691. { id: '3', content: 't3', status: 'pending', priority: 'high' },
  1692. { id: '4', content: 't4', status: 'pending', priority: 'high' },
  1693. ]);
  1694. const hook = createTodoContinuationHook(ctx, {
  1695. maxContinuations: 5,
  1696. cooldownMs: 50,
  1697. autoEnable: true,
  1698. autoEnableThreshold: 4,
  1699. });
  1700. // Do NOT manually enable — auto-enable should trigger
  1701. await hook.handleEvent({
  1702. event: {
  1703. type: 'session.idle',
  1704. properties: { sessionID: 's1' },
  1705. },
  1706. });
  1707. await delay(60);
  1708. // Should have scheduled continuation (auto-enabled)
  1709. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  1710. });
  1711. test('autoEnable=true, todos < threshold → does NOT auto-enable', async () => {
  1712. const ctx = createAutoEnableCtx([
  1713. { id: '1', content: 't1', status: 'pending', priority: 'high' },
  1714. { id: '2', content: 't2', status: 'pending', priority: 'high' },
  1715. { id: '3', content: 't3', status: 'pending', priority: 'high' },
  1716. ]);
  1717. const hook = createTodoContinuationHook(ctx, {
  1718. maxContinuations: 5,
  1719. cooldownMs: 50,
  1720. autoEnable: true,
  1721. autoEnableThreshold: 4,
  1722. });
  1723. await hook.handleEvent({
  1724. event: {
  1725. type: 'session.idle',
  1726. properties: { sessionID: 's1' },
  1727. },
  1728. });
  1729. await delay(60);
  1730. // Should NOT auto-enable or continue
  1731. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  1732. });
  1733. test('autoEnable=false (default) → never auto-enables regardless of todo count', async () => {
  1734. const ctx = createAutoEnableCtx(
  1735. Array.from({ length: 10 }, (_, i) => ({
  1736. id: String(i),
  1737. content: `t${i}`,
  1738. status: 'pending',
  1739. priority: 'high',
  1740. })),
  1741. );
  1742. const hook = createTodoContinuationHook(ctx, {
  1743. maxContinuations: 5,
  1744. cooldownMs: 50,
  1745. // autoEnable defaults to false
  1746. });
  1747. await hook.handleEvent({
  1748. event: {
  1749. type: 'session.idle',
  1750. properties: { sessionID: 's1' },
  1751. },
  1752. });
  1753. await delay(60);
  1754. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  1755. });
  1756. test('auto-enable does not re-enable if already manually enabled', async () => {
  1757. const ctx = createAutoEnableCtx([
  1758. { id: '1', content: 't1', status: 'pending', priority: 'high' },
  1759. { id: '2', content: 't2', status: 'pending', priority: 'high' },
  1760. ]);
  1761. const hook = createTodoContinuationHook(ctx, {
  1762. maxContinuations: 5,
  1763. cooldownMs: 50,
  1764. autoEnable: true,
  1765. autoEnableThreshold: 4,
  1766. });
  1767. // Manually enable first
  1768. await hook.tool.auto_continue.execute({ enabled: true });
  1769. // Only 2 todos (< threshold) — but already enabled, so should continue
  1770. await hook.handleEvent({
  1771. event: {
  1772. type: 'session.idle',
  1773. properties: { sessionID: 's1' },
  1774. },
  1775. });
  1776. await delay(60);
  1777. // Continues because already manually enabled (auto-enable check skipped)
  1778. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  1779. });
  1780. test('auto-enable respects custom threshold', async () => {
  1781. const ctx = createAutoEnableCtx([
  1782. { id: '1', content: 't1', status: 'pending', priority: 'high' },
  1783. { id: '2', content: 't2', status: 'pending', priority: 'high' },
  1784. ]);
  1785. const hook = createTodoContinuationHook(ctx, {
  1786. maxContinuations: 5,
  1787. cooldownMs: 50,
  1788. autoEnable: true,
  1789. autoEnableThreshold: 2,
  1790. });
  1791. await hook.handleEvent({
  1792. event: {
  1793. type: 'session.idle',
  1794. properties: { sessionID: 's1' },
  1795. },
  1796. });
  1797. await delay(60);
  1798. // 2 todos >= threshold 2 → auto-enables
  1799. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  1800. });
  1801. test('auto-enable skipped for non-orchestrator session', async () => {
  1802. const ctx = createAutoEnableCtx([
  1803. { id: '1', content: 't1', status: 'pending', priority: 'high' },
  1804. { id: '2', content: 't2', status: 'pending', priority: 'high' },
  1805. { id: '3', content: 't3', status: 'pending', priority: 'high' },
  1806. { id: '4', content: 't4', status: 'pending', priority: 'high' },
  1807. ]);
  1808. const hook = createTodoContinuationHook(ctx, {
  1809. maxContinuations: 5,
  1810. cooldownMs: 50,
  1811. autoEnable: true,
  1812. autoEnableThreshold: 4,
  1813. });
  1814. // First idle sets orchestrator to session-A
  1815. await hook.handleEvent({
  1816. event: {
  1817. type: 'session.idle',
  1818. properties: { sessionID: 'session-A' },
  1819. },
  1820. });
  1821. await delay(60);
  1822. // Reset mock
  1823. ctx.client.session.prompt.mockClear();
  1824. // Second idle from session-B — not orchestrator, should skip
  1825. await hook.handleEvent({
  1826. event: {
  1827. type: 'session.idle',
  1828. properties: { sessionID: 'session-B' },
  1829. },
  1830. });
  1831. await delay(60);
  1832. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  1833. });
  1834. test('auto-enable with todo fetch failure → no auto-enable, no crash', async () => {
  1835. const ctx = createMockContext();
  1836. ctx.client.session.todo = mock(async () => {
  1837. throw new Error('Network error');
  1838. });
  1839. const hook = createTodoContinuationHook(ctx, {
  1840. maxContinuations: 5,
  1841. cooldownMs: 50,
  1842. autoEnable: true,
  1843. autoEnableThreshold: 4,
  1844. });
  1845. // Should not throw
  1846. await hook.handleEvent({
  1847. event: {
  1848. type: 'session.idle',
  1849. properties: { sessionID: 's1' },
  1850. },
  1851. });
  1852. await delay(60);
  1853. // No auto-enable, no continuation
  1854. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  1855. });
  1856. test('auto-enable resets consecutive counter and suppress window', async () => {
  1857. const ctx = createAutoEnableCtx([
  1858. { id: '1', content: 't1', status: 'pending', priority: 'high' },
  1859. { id: '2', content: 't2', status: 'pending', priority: 'high' },
  1860. { id: '3', content: 't3', status: 'pending', priority: 'high' },
  1861. { id: '4', content: 't4', status: 'pending', priority: 'high' },
  1862. ]);
  1863. const hook = createTodoContinuationHook(ctx, {
  1864. maxContinuations: 5,
  1865. cooldownMs: 50,
  1866. autoEnable: true,
  1867. autoEnableThreshold: 4,
  1868. });
  1869. // Manually enable, run a continuation, disable
  1870. await hook.tool.auto_continue.execute({ enabled: true });
  1871. await hook.handleEvent({
  1872. event: {
  1873. type: 'session.idle',
  1874. properties: { sessionID: 's1' },
  1875. },
  1876. });
  1877. await delay(60);
  1878. // Fire abort to set suppress window
  1879. await hook.handleEvent({
  1880. event: {
  1881. type: 'session.error',
  1882. properties: {
  1883. sessionID: 's1',
  1884. error: { name: 'AbortError' },
  1885. },
  1886. },
  1887. });
  1888. // Disable
  1889. await hook.tool.auto_continue.execute({ enabled: false });
  1890. // Reset mock
  1891. ctx.client.session.prompt.mockClear();
  1892. // Fire idle again — auto-enable should trigger (4 todos >= 4),
  1893. // resetting counter and suppress window
  1894. await hook.handleEvent({
  1895. event: {
  1896. type: 'session.idle',
  1897. properties: { sessionID: 's1' },
  1898. },
  1899. });
  1900. await delay(60);
  1901. // Should continue (suppressed window was cleared by auto-enable)
  1902. expect(hasContinuation(ctx.client.session.prompt)).toBe(true);
  1903. });
  1904. test('auto-enable counts incomplete todos only, not completed', async () => {
  1905. const ctx = createAutoEnableCtx([
  1906. { id: '1', content: 't1', status: 'completed', priority: 'high' },
  1907. { id: '2', content: 't2', status: 'completed', priority: 'high' },
  1908. { id: '3', content: 't3', status: 'pending', priority: 'high' },
  1909. { id: '4', content: 't4', status: 'pending', priority: 'high' },
  1910. ]);
  1911. const hook = createTodoContinuationHook(ctx, {
  1912. maxContinuations: 5,
  1913. cooldownMs: 50,
  1914. autoEnable: true,
  1915. autoEnableThreshold: 4,
  1916. });
  1917. await hook.handleEvent({
  1918. event: {
  1919. type: 'session.idle',
  1920. properties: { sessionID: 's1' },
  1921. },
  1922. });
  1923. await delay(60);
  1924. // Only 2 incomplete todos < threshold 4 → does NOT auto-enable
  1925. expect(ctx.client.session.prompt).not.toHaveBeenCalled();
  1926. });
  1927. });
  1928. });