@@ -14,7 +14,7 @@ import {
1414 type HunkDaemonCliClient ,
1515} from "./commands" ;
1616import { HUNK_DAEMON_UPGRADE_RESTART_NOTICE } from "./capabilities" ;
17- import { HUNK_SESSION_API_VERSION } from "./protocol" ;
17+ import { HUNK_SESSION_API_VERSION , HUNK_SESSION_DAEMON_VERSION } from "./protocol" ;
1818
1919function createTestListedSession ( sessionId : string ) {
2020 return buildTestListedSession ( {
@@ -58,6 +58,7 @@ function createClient(overrides: Partial<HunkDaemonCliClient>): HunkDaemonCliCli
5858 return {
5959 getCapabilities : async ( ) => ( {
6060 version : HUNK_SESSION_API_VERSION ,
61+ daemonVersion : HUNK_SESSION_DAEMON_VERSION ,
6162 actions : [
6263 "list" ,
6364 "get" ,
@@ -215,7 +216,11 @@ describe("session command compatibility checks", () => {
215216 createClient ( {
216217 getCapabilities : async ( ) => {
217218 createdClients . push ( "stale-capabilities" ) ;
218- return { version : HUNK_SESSION_API_VERSION - 1 , actions : [ "list" ] } ;
219+ return {
220+ version : HUNK_SESSION_API_VERSION - 1 ,
221+ daemonVersion : HUNK_SESSION_DAEMON_VERSION ,
222+ actions : [ "list" ] ,
223+ } ;
219224 } ,
220225 } ) ,
221226 createClient ( {
@@ -268,6 +273,92 @@ describe("session command compatibility checks", () => {
268273 }
269274 } ) ;
270275
276+ test ( "refreshes a stale daemon before running comment-add" , async ( ) => {
277+ const selector : SessionSelectorInput = { sessionId : "session-1" } ;
278+ const restartCalls : Array < { action : string ; selector ?: SessionSelectorInput } > = [ ] ;
279+ const createdClients : string [ ] = [ ] ;
280+ const notices : string [ ] = [ ] ;
281+ const originalConsoleError = console . error ;
282+ console . error = ( ...args : unknown [ ] ) => {
283+ notices . push ( args . map ( ( value ) => String ( value ) ) . join ( " " ) ) ;
284+ } ;
285+
286+ const clients = [
287+ createClient ( {
288+ getCapabilities : async ( ) => {
289+ createdClients . push ( "stale-capabilities" ) ;
290+ return null ;
291+ } ,
292+ } ) ,
293+ createClient ( {
294+ addComment : async ( input ) => {
295+ createdClients . push ( "fresh-comment-add" ) ;
296+ expect ( input . selector ) . toEqual ( selector ) ;
297+ expect ( input . filePath ) . toBe ( "README.md" ) ;
298+ expect ( input . side ) . toBe ( "new" ) ;
299+ expect ( input . line ) . toBe ( 2 ) ;
300+ expect ( input . summary ) . toBe ( "Review note" ) ;
301+ return {
302+ commentId : "comment-1" ,
303+ fileId : "file-1" ,
304+ filePath : "README.md" ,
305+ hunkIndex : 0 ,
306+ side : "new" ,
307+ line : 2 ,
308+ } ;
309+ } ,
310+ } ) ,
311+ ] ;
312+
313+ try {
314+ setSessionCommandTestHooks ( {
315+ createClient : ( ) => {
316+ const client = clients . shift ( ) ;
317+ if ( ! client ) {
318+ throw new Error ( "No fake session client remaining." ) ;
319+ }
320+
321+ return client ;
322+ } ,
323+ resolveDaemonAvailability : async ( ) => true ,
324+ restartDaemonForMissingAction : async ( action , receivedSelector ) => {
325+ restartCalls . push ( { action, selector : receivedSelector } ) ;
326+ } ,
327+ } ) ;
328+
329+ const output = await runSessionCommand ( {
330+ kind : "session" ,
331+ action : "comment-add" ,
332+ selector,
333+ filePath : "README.md" ,
334+ side : "new" ,
335+ line : 2 ,
336+ summary : "Review note" ,
337+ reveal : false ,
338+ output : "json" ,
339+ } satisfies SessionCommandInput ) ;
340+
341+ expect ( JSON . parse ( output ) ) . toMatchObject ( {
342+ result : {
343+ commentId : "comment-1" ,
344+ filePath : "README.md" ,
345+ side : "new" ,
346+ line : 2 ,
347+ } ,
348+ } ) ;
349+ expect ( restartCalls ) . toEqual ( [
350+ {
351+ action : "comment-add" ,
352+ selector,
353+ } ,
354+ ] ) ;
355+ expect ( createdClients ) . toEqual ( [ "stale-capabilities" , "fresh-comment-add" ] ) ;
356+ expect ( notices ) . toContain ( HUNK_DAEMON_UPGRADE_RESTART_NOTICE ) ;
357+ } finally {
358+ console . error = originalConsoleError ;
359+ }
360+ } ) ;
361+
271362 test ( "runs review commands through the daemon without raw patch text by default" , async ( ) => {
272363 setSessionCommandTestHooks ( {
273364 createClient : ( ) =>
@@ -674,6 +765,7 @@ describe("session command compatibility checks", () => {
674765 createClient ( {
675766 getCapabilities : async ( ) => ( {
676767 version : HUNK_SESSION_API_VERSION ,
768+ daemonVersion : HUNK_SESSION_DAEMON_VERSION ,
677769 actions : [
678770 "list" ,
679771 "get" ,
0 commit comments